diff --git a/.github/workflows/cancel-on-close.yml b/.github/workflows/cancel-on-close.yml new file mode 100644 index 000000000..9a6e44965 --- /dev/null +++ b/.github/workflows/cancel-on-close.yml @@ -0,0 +1,27 @@ +name: cancel-on-close +# A closed or merged pull request's queued and running checks free the runners at once. +# pull_request_target so the token can cancel runs of pull requests from forks too; +# it checks out nothing and runs no code from the pull request. +on: + pull_request_target: + types: [closed] +permissions: + actions: write +jobs: + cancel: + runs-on: ubuntu-latest + steps: + - name: Cancel the pull request's unfinished runs + env: + GH_TOKEN: ${{ github.token }} + REPO: ${{ github.repository }} + BRANCH: ${{ github.event.pull_request.head.ref }} + SHA: ${{ github.event.pull_request.head.sha }} + run: | + for status in queued in_progress waiting pending requested; do + gh run list -R "$REPO" --branch "$BRANCH" --status "$status" --limit 100 \ + --json databaseId,event,headSha \ + -q ".[] | select(.event == \"pull_request\" and .headSha == \"$SHA\") | .databaseId" + done | sort -u | while read -r id; do + gh run cancel -R "$REPO" "$id" || true + done diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5d5e285c5..da23874b8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -56,6 +56,8 @@ jobs: run: | cargo +stable fmt --all -- --check cargo +stable clippy --workspace --all-targets -- -D warnings + # The server build (released as horizon-engine): the game without sound or controllers. + cargo +stable clippy -p horizon-client --all-targets --no-default-features -- -D warnings - name: Test Rust core and voxel kernel correctness if: steps.rust.outputs.required != 'false' run: cargo +stable test --workspace diff --git a/.github/workflows/launcher.yml b/.github/workflows/launcher.yml index 2ab491207..c902acf32 100644 --- a/.github/workflows/launcher.yml +++ b/.github/workflows/launcher.yml @@ -1,7 +1,6 @@ name: launcher # Builds the launcher on Linux, macOS and Windows, and releases it by itself (docs/release-flow.md): after a -# push to main that changes the launcher or what it builds in (crates/horizon-prepare, crates/horizon-theme, -# crates/horizon-telemetry), +# push to main that changes the launcher or a crate it builds in (the paths below), # every 10 minutes (a release waiting for merges to settle goes out) and by hand, tools/release_train.py decides as # for the game. # A release is launcher-vX.Y.Z: the patch after the last launcher tag, or the next minor when a merged pull @@ -14,10 +13,15 @@ on: push: branches: [main, launcher, 'launcher-*'] paths: + # Every crate the launcher builds in, as tools/release_train.py's LAUNCHER_PATHS. - 'launcher/**' + - 'crates/horizon-bench/**' + - 'crates/horizon-friends/**' + - 'crates/horizon-mods/**' + - 'crates/horizon-port-pack/**' - 'crates/horizon-prepare/**' - - 'crates/horizon-theme/**' - 'crates/horizon-telemetry/**' + - 'crates/horizon-theme/**' - '.github/workflows/launcher.yml' schedule: - cron: '7-59/10 * * * *' @@ -138,7 +142,8 @@ jobs: echo "::notice::No Metal toolchain on this runner: GPUI compiles its shaders at run time" echo "LAUNCHER_FEATURES=gpui/runtime_shaders" >> "$GITHUB_ENV" fi - - run: cargo test --release --locked --lib ${LAUNCHER_FEATURES:+--features "$LAUNCHER_FEATURES"} + # Every test target (the library, the binary, tests/), on each system the launcher ships for. + - run: cargo test --release --locked ${LAUNCHER_FEATURES:+--features "$LAUNCHER_FEATURES"} - run: cargo build --release --locked ${LAUNCHER_FEATURES:+--features "$LAUNCHER_FEATURES"} - name: The version is the release's run: | diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 27636b0eb..bd916514b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -76,6 +76,9 @@ jobs: os: ubuntu-22.04 dir: horizon-linux-x86_64 asset: horizon-linux-x86_64.tar.gz + # The server build too: packaging/server/install.sh installs it on Linux. + server_dir: horizon-server-linux-x86_64 + server_asset: horizon-server-linux-x86_64.tar.gz - name: windows os: windows-latest dir: horizon-windows-x64 @@ -174,7 +177,14 @@ jobs: case "${{ matrix.asset }}" in *.tar.gz) tar czf "${{ matrix.asset }}" "${{ matrix.dir }}" ;; *) if [[ "$RUNNER_OS" == Windows ]]; then 7z a -tzip "${{ matrix.asset }}" "${{ matrix.dir }}" > /dev/null - else ditto -c -k --keepParent "${{ matrix.dir }}" "${{ matrix.asset }}"; fi ;; + else + # Without resource forks and extended attributes: plain ditto adds an + # AppleDouble `._` file beside every file that has any. + ditto -c -k --norsrc --noextattr --noqtn --keepParent "${{ matrix.dir }}" "${{ matrix.asset }}" + if unzip -Z1 "${{ matrix.asset }}" | grep -E '(^|/)(\._|__MACOSX/)'; then + echo "::error::macOS metadata files in ${{ matrix.asset }}"; exit 1 + fi + fi ;; esac ls -l "${{ matrix.asset }}" - uses: actions/upload-artifact@v4 @@ -183,6 +193,29 @@ jobs: path: dist/${{ matrix.asset }} if-no-files-found: error retention-days: 3 + - name: The server build (the game without sound or controllers, as horizon-engine) + if: matrix.server_asset + run: | + set -euo pipefail + # After the client is packaged: target/release/horizon-client is rebuilt without the audio and + # controllers features, so it needs neither libasound nor libudev (minimal server images lack them). + cargo build --release --locked -p horizon-client --no-default-features --bin horizon-client + cargo run --locked -p xtask -- package-server target/release/horizon-client "dist/${{ matrix.server_dir }}" + folder="dist/${{ matrix.server_dir }}" + python3 tools/check_imports.py --server "$folder/horizon-engine" "$folder/horizon-server" "$folder/horizon-prepare" + "$folder/horizon-engine" --help | grep -q -- '--resources' + "$folder/horizon-server" --version + cd dist + rm -f "${{ matrix.server_asset }}" + tar czf "${{ matrix.server_asset }}" "${{ matrix.server_dir }}" + ls -l "${{ matrix.server_asset }}" + - uses: actions/upload-artifact@v4 + if: matrix.server_asset + with: + name: ${{ matrix.server_asset }} + path: dist/${{ matrix.server_asset }} + if-no-files-found: error + retention-days: 3 publish: needs: [decide, build] @@ -213,7 +246,7 @@ jobs: run: | gh release create "$TAG" --target "${{ needs.decide.outputs.head }}" --title "$(cat notes/title.txt)" \ --notes-file notes/body.md --latest dist/horizon-linux-x86_64.tar.gz dist/horizon-windows-x64.zip \ - dist/horizon-macos-arm64.zip + dist/horizon-macos-arm64.zip dist/horizon-server-linux-x86_64.tar.gz - name: Dry run, the release that would be published if: needs.decide.outputs.dry_run == 'true' run: | diff --git a/.gitignore b/.gitignore index eb29ad10d..7fffd6a99 100644 --- a/.gitignore +++ b/.gitignore @@ -28,5 +28,6 @@ /horizon-player.txt /servers.dat /screenshots/ +/.horizon-cache/ /crash-reports/ /debug/ diff --git a/Cargo.lock b/Cargo.lock index 1d5897d11..ea7bc6449 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1293,6 +1293,7 @@ dependencies = [ "log", "presser", "thiserror 2.0.21", + "windows 0.62.2", ] [[package]] @@ -1429,12 +1430,14 @@ dependencies = [ "horizon-shaderpack", "horizon-telemetry", "horizon-theme", + "horizon-threads", "horizon-world", "log", "miniz_oxide 0.8.9", "naga", "png", "pollster", + "rustix 1.1.5", "serde", "serde_json", "sha2", @@ -1539,6 +1542,7 @@ dependencies = [ "horizon-world", "serde_json", "sha1", + "sha2", ] [[package]] @@ -1554,13 +1558,16 @@ dependencies = [ name = "horizon-server" version = "0.1.0" dependencies = [ + "chrono", "horizon-bench", "horizon-server-files", "libc", "serde", "serde_json", + "sha2", "toml", "toml_edit", + "ureq", "windows-sys 0.61.2", "zip", ] @@ -1569,10 +1576,12 @@ dependencies = [ name = "horizon-server-files" version = "0.1.0" dependencies = [ + "base64", "horizon-core", "horizon-protocol", "horizon-world", "log", + "ureq", ] [[package]] @@ -1600,6 +1609,14 @@ dependencies = [ name = "horizon-theme" version = "0.1.0" +[[package]] +name = "horizon-threads" +version = "0.1.0" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + [[package]] name = "horizon-world" version = "0.1.0" @@ -1608,7 +1625,10 @@ dependencies = [ "horizon-core", "libm", "lz4_flex", + "postcard", + "serde", "serde_json", + "sha2", "xxhash-rust", ] @@ -3010,6 +3030,12 @@ dependencies = [ "getrandom 0.2.17", ] +[[package]] +name = "range-alloc" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca45419789ae5a7899559e9512e58ca889e41f04f1f2445e9f4b290ceccd1d08" + [[package]] name = "raw-window-handle" version = "0.6.2" @@ -4584,12 +4610,11 @@ 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", "ash", + "bit-set 0.10.0", "bitflags 2.13.2", "block2 0.6.2", "bytemuck", @@ -4608,11 +4633,13 @@ dependencies = [ "objc2-foundation 0.3.2", "objc2-metal 0.3.2", "objc2-quartz-core 0.3.2", + "once_cell", "ordered-float", "parking_lot", "portable-atomic", "portable-atomic-util", "profiling", + "range-alloc", "raw-window-handle", "raw-window-metal", "renderdoc-sys", @@ -4622,6 +4649,7 @@ dependencies = [ "wgpu-naga-bridge", "wgpu-types", "windows 0.62.2", + "windows-core 0.62.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 30a30386d..409bf4689 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] -exclude = ["launcher", "crates/horizon-telemetry-server"] -members = ["crates/horizon-core", "crates/horizon-dualsense", "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"] +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" [workspace.package] @@ -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] diff --git a/README.md b/README.md index f4f3094ae..a1e6a38f6 100644 --- a/README.md +++ b/README.md @@ -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 @@ -65,7 +67,7 @@ Captured in the Rust client itself, with vanilla assets and no mods or shaders. - +
Horizon's title screen: the mark and the pages on the left, the Horizon title, Singleplayer and Multiplayer, and the last world played and the faint wordmark, over a veiled snowy landscape
The Singleplayer page: three worlds with their dates and game modes, and Create world and Play buttonsThe Singleplayer page: two worlds with their dates and game modes, and Create world and Play buttons The Video settings in their Simple view: a preset, render distance, field of view, framerate, VSync and GUI scale, each with what it costs
@@ -79,17 +81,17 @@ The vanilla menus are one setting away: Options → Interface → Menu style → Sunset over a savanna river valley and a plains village - A plains village with farms, an iron golem and animals - Survival at dusk: a zombie attacks near the village + A plains village among savanna hills, with farms, villagers and an iron golem + Survival at dusk: a zombie walks up, with the hotbar, health, hunger and experience A crimson forest in the Nether with a zombified piglin Moonrise over a torch-lit village and river - Salmon swimming in a river + Tropical fish over a warm-ocean coral reef - Vanilla world generation ported to Rust (terrain, aquifers, carvers, features, villages and the Nether), every block model with smooth lighting and biome tints, the vanilla sky and fog, mobs, the HUD, chat and advancements: everything in these frames is drawn by Horizon. + Vanilla world generation ported to Rust (terrain, aquifers, carvers, features, villages and the Nether), every block model with smooth lighting and biome tints, the vanilla sky and fog, mobs and the HUD: everything in these frames is drawn by Horizon. diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index 585224f8f..368813ecf 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -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`). diff --git a/benchmarks/server-2026-10-17/README.md b/benchmarks/server-2026-10-17/README.md new file mode 100644 index 000000000..4a3cd3dcc --- /dev/null +++ b/benchmarks/server-2026-10-17/README.md @@ -0,0 +1,174 @@ +# Dedicated server against vanilla 26.3, with bots (2026-10-17) + +`horizon-client --dedicated` (run by `horizon-server`) against the pinned vanilla 26.3 `server.jar`, on `main` +at `7bbbf6e9` (in release v2026.10.17.17). That build has the movement checks and chunk pacing of #586, the +equal-work method of #670, and the server performance PRs #701–#707 and #713. + +This replaces the data first published here, which came from a Mac mini overloaded by other work (load 55–141 on +14 threads). An earlier run of this method, on `main` at `002f14ce`, is kept in `earlier-002f14ce/` and compared +at the end. + +## Setup + +- **Server machine:** ThinkPad, Intel i7-7500U (2 cores, 4 threads), 32 GB, NixOS. Nothing else heavy ran on it: + its load average was 0.4 when idle. Both servers ran under `nice -n 19`. +- **Bots:** `horizon-bots` on another machine (Mac mini M4 Pro), over the tailnet. That machine was shared with + other work (load 36–170 on 14 threads, mean 78). The bots need little CPU, but a busy bot host can answer later. + Both servers saw the same conditions, since they took turns at each bot count. +- **Horizon:** release build of `7bbbf6e9` (`horizon-server`, `horizon-client`). +- **Vanilla:** the pinned 26.3 `server.jar` on OpenJDK 25.0.4.1, with `-Xms4g -Xmx4g`. +- **Properties:** + - `level-seed=12345`, view and simulation distance 10, creative (forced); + - `online-mode=false`, `difficulty=easy`, `max-players=200`; + - default game rules on both (the locator bar is on). +- **Each run:** a fresh server, bots joining one per second, a 60 s warm-up, then 180 s measured. +- **Commands** (on the server machine): + ``` + python3 tools/server_bench.py --servers horizon,vanilla --nice 19 --warmup 60 --seconds 180 \ + --bots-via "ssh " --bots-bin --connect \ + --bots 1,10,25,50,100 --behaviour walk,explore + python3 tools/server_bench.py ...same... --bots 1,10,25,50 --behaviour explore --explore-speed 0.8 + ``` +- **Columns:** + - **MSPT** (mean) and **P95** come from `/tick query`, in ms. **TPS** is the tick rate. + - **RSS** is the mean over the run. **CPU** is the whole process tree, as a percentage of one core. + - **Chunks/s** is what the bots received. **New chunks/s** counts each chunk once per bot. + - **Pulled back** counts the times the server moved a bot back (vanilla's "moved too quickly" and similar). + - **Load** is the mean load average during that run: the server machine (which includes the server's own + threads) and the bot host. + +## Results + +### Equal work: bots explore at the same pace (`--explore-speed 0.8`) + +Each bot flies outwards at 0.8 blocks a second, which both servers keep up with. They send the same chunks, so +the tick times compare the same work. + +| Bots | Server | MSPT | P95 | TPS | RSS | CPU | Chunks/s | New chunks/s | Pulled back | Load (server / bots) | +|---:|---|---:|---:|---:|---:|---:|---:|---:|---:|---:| +| 1 | Horizon | 3.16 | 5.78 | 20.0 | 0.76 GB | 22 % | 2 | 1.27 | 0 | 2.8 / 63 | +| 1 | Vanilla | 16.82 | 27.3 | 20.0 | 3.37 GB | 69 % | 2 | 1.35 | 0 | 1.8 / 94 | +| 10 | Horizon | 9.83 | 19.01 | 20.0 | 0.86 GB | 77 % | 14 | 12.31 | 1051 | 2.2 / 74 | +| 10 | Vanilla | 24.38 | 39.58 | 20.0 | 3.57 GB | 132 % | 14 | 12.32 | 1027 | 2.5 / 64 | +| 25 | Horizon | 16.87 | 30.88 | 20.0 | 0.89 GB | 111 % | 34 | 29.92 | 3770 | 5.1 / 51 | +| 25 | Vanilla | 30.29 | 43.47 | 20.0 | 3.61 GB | 176 % | 33 | 29.29 | 5139 | 5.2 / 55 | +| 50 | Horizon | 23.57 | 47.53 | 20.0 | 0.92 GB | 127 % | 67 | 59.42 | 7434 | 8.5 / 64 | +| 50 | Vanilla | 42.44 | 54.84 | 20.0 | 3.67 GB | 201 % | 65 | 58.11 | 10604 | 5.1 / 59 | + +### `walk`: bots walk around the spawn + +| Bots | Server | MSPT | P95 | TPS | RSS | CPU | Chunks/s | New chunks/s | Pulled back | Load (server / bots) | +|---:|---|---:|---:|---:|---:|---:|---:|---:|---:|---:| +| 1 | Horizon | 4.01 | 8.19 | 20.0 | 0.78 GB | 59 % | 10 | 0.43 | 0 | 1.4 / 70 | +| 1 | Vanilla | 18.22 | 29.34 | 20.0 | 3.43 GB | 69 % | 10 | 0.47 | 0 | 1.9 / 63 | +| 10 | Horizon | 8.79 | 17.35 | 20.0 | 0.78 GB | 119 % | 95 | 3.58 | 0 | 2.9 / 69 | +| 10 | Vanilla | 18.67 | 38.26 | 20.0 | 3.50 GB | 101 % | 101 | 4.38 | 5 | 3.2 / 59 | +| 25 | Horizon | 15.01 | 31.12 | 20.0 | 0.80 GB | 161 % | 234 | 8.49 | 0 | 6.3 / 43 | +| 25 | Vanilla | 18.78 | 28.16 | 20.0 | 3.52 GB | 155 % | 252 | 11.89 | 10 | 6.7 / 57 | +| 50 | Horizon | 22.72 | 60.19 | 20.0 | 0.81 GB | 180 % | 465 | 16.94 | 0 | 14.7 / 50 | +| 50 | Vanilla | 34.3 | 55.55 | 20.0 | 3.58 GB | 267 % | 497 | 26.74 | 39 | 9.5 / 56 | +| 100 | Horizon | 63.33 | 125.04 | 15.79 | 0.87 GB | 246 % | 678 | 31.8 | 0 | 32.9 / 95 | +| 100 | Vanilla | stopped by its watchdog (a 60 s tick) | | | | | | | | 15.1 / 120 | + +Both servers sent each walking bot the same number of distinct chunks over the whole run (680–690). Horizon's +"new chunks/s" inside the measured window is lower, so compare the totals, not that column. + +### `explore` at full speed: unequal work + +Bots fly outwards as fast as chunks arrive (`horizon-bots`' default, up to 22 blocks a second). Both servers now pull them back ("moved too quickly"), but Horizon sends +more new terrain, so this pass doesn't compare equal work. The server-machine load of Horizon's 1-bot run is +estimated from the time between the runs around it: its log was overwritten before it was read. + +| Bots | Server | MSPT | P95 | TPS | RSS | CPU | Chunks/s | New chunks/s | Pulled back | Load (server / bots) | +|---:|---|---:|---:|---:|---:|---:|---:|---:|---:|---:| +| 1 | Horizon | 9.73 | 20.99 | 20.0 | 0.84 GB | 330 % | 41 | 35.96 | 0 | 4.7 / 129 | +| 1 | Vanilla | 9.71 | 20.37 | 20.0 | 3.61 GB | 247 % | 35 | 33.61 | 0 | 4.3 / 156 | +| 10 | Horizon | 21.27 | 39.15 | 20.0 | 1.18 GB | 322 % | 46 | 46.11 | 9856 | 5.0 / 114 | +| 10 | Vanilla | 16.94 | 30.28 | 20.0 | 3.95 GB | 254 % | 25 | 25.01 | 11123 | 5.5 / 89 | +| 25 | Horizon | 25.76 | 45.01 | 20.0 | 1.17 GB | 314 % | 61 | 60.7 | 31264 | 6.6 / 82 | +| 25 | Vanilla | 24.93 | 37.09 | 20.0 | 4.04 GB | 305 % | 38 | 37.84 | 33467 | 6.1 / 91 | +| 50 | Horizon | 39.31 | 65.33 | 20.0 | 1.25 GB | 325 % | 115 | 112.25 | 64163 | 6.9 / 95 | +| 50 | Vanilla | stopped by its watchdog (a 60 s tick) | | | | | | | | 7.7 / 86 | +| 100 | Horizon | 52.7 | 85.73 | 18.97 | 1.36 GB | 328 % | 154 | 151.83 | 116161 | 13.4 / 79 | +| 100 | Vanilla | stopped by its watchdog (a 60 s tick) | | | | | | | | 10.0 / 72 | + +Vanilla's watchdog stopped it after a 60-second tick in three runs (walk 100, explore 50 and 100). Each time its +server thread was in entity tracking (`ChunkMap$TrackedEntity.updatePlayer`). + +## What it shows + +- **Same work, 1 to 50 players:** Horizon's mean tick is 44–81 % shorter than vanilla's, its 95th percentile is + lower, and its process uses 32–63 % of vanilla's CPU. +- **Walking, 1 to 50 players:** Horizon's mean tick is shorter at every count (4.0 against 18.2 ms with 1 bot, + 22.7 against 34.3 ms with 50). +- **Where vanilla wins:** + - With 25 and 50 walking bots, vanilla's 95th percentile is lower (28.2 against 31.1 ms, 55.6 against + 60.2 ms). Horizon's slow ticks are spikier. + - In the full-speed explore pass, vanilla's mean tick is shorter with 10 and 25 bots (16.9 against 21.3 ms, + 24.9 against 25.8 ms), while Horizon sends 1.6–1.8 times as many new chunks. + - With 50 walking bots, Horizon makes the server machine busier (load 14.7 against 9.5). Its connection + writer threads compress every packet (below). +- **100 players:** Horizon keeps 15.8 TPS walking and 19.0 TPS exploring at full speed. Vanilla's watchdog + stopped it in both. +- **Memory:** Horizon used 0.76–0.92 GB in every walk and equal-work run, and 1.36 GB at most when 100 bots + explored at full speed. Vanilla used 3.4–4.0 GB with its 4 GB heap. +- **Start:** Horizon's fresh world was ready in 9.2–9.9 s, vanilla's in 14.9–17.2 s. + +## Where Horizon's time goes (50 walking bots) + +`perf record -e cycles:u --call-graph lbr` on the engine for 30 s, during a separate 50-bot `walk` run on the same +build and machine (MSPT 21.7 ms, 178 % CPU). Shares of all the engine's CPU samples: + +| Thread | Share | Mostly | +|---|---:|---| +| Connection writers (`horizon-lan-out`) | 55 % | 53 % in `miniz_oxide` deflate, from `horizon_protocol::frame::encode` | +| World generation workers | 33 % | noise, surface rules, aquifers | +| Server thread (the tick) | 12 % | see below | + +Inside the tick: +- the locator bar (`Waypoints::tick`): a third of the tick; +- `Hub::tick`: a quarter; +- block entities, creatures, chunk encoding (`ChunkEncoder::encode_in`) and `Streaming::update`: 7–10 % each; +- SipHash of `u128` keys (the default `HashMap` hasher): about 10 % of the tick. + +**The next biggest cost is compressing packets.** Every chunk packet is compressed again for each player who gets +it, with `flate2`'s default `miniz_oxide` backend at level 6. On this 4-thread CPU that competes with the tick +and with world generation. Two fixes, which can be combined: +- the `zlib-rs` backend for `flate2`: 2.2–10 times faster than `miniz_oxide` at level 6 in a quick test, with + output about 3 % larger; +- compressing a chunk packet once and sending the same bytes to every player who needs it. + +## Before and after the performance PRs + +The earlier run, on `main` at `002f14ce` with #670's bench changes (build `a9ee56a8`), before #586, #701–#707 and +#713. Same machines and settings. MSPT mean / P95, in ms: + +Walk: + +| Bots | Horizon before | Horizon now | Vanilla before | Vanilla now | +|---:|---|---|---|---| +| 1 | 5.77 / 11.04 | 4.01 / 8.19 | 18.84 / 29.55 | 18.22 / 29.34 | +| 10 | 16.23 / 29.32 | 8.79 / 17.35 | 18.02 / 37.62 | 18.67 / 38.26 | +| 25 | 23.98 / 49.18 | 15.01 / 31.12 | 19.19 / 33.84 | 18.78 / 28.16 | +| 50 | 35.07 / 73.24 | 22.72 / 60.19 | 35.23 / 60.87 | 34.3 / 55.55 | +| 100 | 104.8 / 158.8 | 63.33 / 125.04 | watchdog | watchdog | + +Equal work: + +| Bots | Horizon before | Horizon now | Vanilla before | Vanilla now | +|---:|---|---|---|---| +| 1 | 4.78 / 7.3 | 3.16 / 5.78 | 16.63 / 26.23 | 16.82 / 27.3 | +| 10 | 15.19 / 25.66 | 9.83 / 19.01 | 24.65 / 40.81 | 24.38 / 39.58 | +| 25 | 23.37 / 42.59 | 16.87 / 30.88 | 30.28 / 44.81 | 30.29 / 43.47 | +| 50 | 30.59 / 67.94 | 23.57 / 47.53 | 43.52 / 56.18 | 42.44 / 54.84 | + +Vanilla's numbers repeat within a few percent, so the runs are comparable. In the earlier run, 25 walking bots +were faster on vanilla (19.2 against 24.0 ms). A profile then put a third of the tick in advancement updates, +which #701 fixed. + +## Files + +- `walk-explore.json`: the `walk` and full-speed `explore` passes. `explore-equal-rate.json`: the equal-work pass. +- `load.txt`: both machines' load averages every 30 s during the runs. +- `earlier-002f14ce/`: the same files for the earlier run. +- The server's tailnet address is replaced by `` in the JSON. diff --git a/benchmarks/server-2026-10-17/earlier-002f14ce/explore-equal-rate.json b/benchmarks/server-2026-10-17/earlier-002f14ce/explore-equal-rate.json new file mode 100644 index 000000000..404f8d032 --- /dev/null +++ b/benchmarks/server-2026-10-17/earlier-002f14ce/explore-equal-rate.json @@ -0,0 +1,341 @@ +{ + "kind": "horizon-server-benchmark", + "date": "2026-10-17T15:37:03+00:00", + "machine": { + "system": "Linux", + "machine": "x86_64", + "cpus": 4 + }, + "commit": "a9ee56a8b143999c9bc7056850ffcd622a9214aa", + "java": [ + "openjdk 25.0.4.1 2026-08-18" + ], + "settings": { + "servers": "horizon,vanilla", + "bots": "1,10,25,50", + "behaviour": "explore", + "join_every": 1.0, + "warmup": 60, + "seconds": 180, + "seed": 12345, + "explore_speed": 0.8, + "gamerule": "", + "heap": "4g", + "out": "/home/ludovic/code/horizon/.claude/worktrees/server-host-4/.local/bench-out/explore-equal-rate.json", + "nice": 19, + "bots_via": "ssh macmini", + "bots_bin": "/Users/ludovicmarie/code/horizon/.claude/worktrees/server-host-4/target/release/horizon-bots", + "connect": "", + "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": 1, + "behaviour": "explore", + "start_seconds": 9.21, + "mspt": { + "average": 4.78, + "p50": 4.59, + "p95": 7.3, + "p99": 13.53 + }, + "mspt_worst_p99": 36.5, + "tps": 20.0, + "rss_mb": { + "mean": 759.91, + "max": 771.3 + }, + "cpu_percent": 24.49, + "chunks_per_second": 1.64, + "unique_per_second": 1.27, + "forgotten_per_second": 1.54, + "waited_ticks_per_second": 0.0, + "chunks_total": 841, + "chunks_per_bot": { + "explore": 841.0 + }, + "unique_chunks_per_bot": { + "explore": 781.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 1, + "behaviour": "explore", + "start_seconds": 14.35, + "mspt": { + "average": 16.63, + "p50": 16.73, + "p95": 26.23, + "p99": 35.39 + }, + "mspt_worst_p99": 68.5, + "tps": 20.0, + "rss_mb": { + "mean": 3431.12, + "max": 3459.2 + }, + "cpu_percent": 69.76, + "chunks_per_second": 1.66, + "unique_per_second": 1.37, + "forgotten_per_second": 1.67, + "waited_ticks_per_second": 0.0, + "chunks_total": 840, + "chunks_per_bot": { + "explore": 840.0 + }, + "unique_chunks_per_bot": { + "explore": 780.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 10, + "behaviour": "explore", + "start_seconds": 9.31, + "mspt": { + "average": 15.19, + "p50": 14.45, + "p95": 25.66, + "p99": 38.89 + }, + "mspt_worst_p99": 57.6, + "tps": 20.0, + "rss_mb": { + "mean": 859.86, + "max": 920.2 + }, + "cpu_percent": 84.95, + "chunks_per_second": 14.22, + "unique_per_second": 12.7, + "forgotten_per_second": 14.11, + "waited_ticks_per_second": 0.0, + "chunks_total": 7962, + "chunks_per_bot": { + "explore": 796.2 + }, + "unique_chunks_per_bot": { + "explore": 768.2 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "explore", + "start_seconds": 14.74, + "mspt": { + "average": 24.65, + "p50": 21.82, + "p95": 40.81, + "p99": 70.23 + }, + "mspt_worst_p99": 119.1, + "tps": 20.0, + "rss_mb": { + "mean": 3528.77, + "max": 3643.2 + }, + "cpu_percent": 133.1, + "chunks_per_second": 13.62, + "unique_per_second": 12.32, + "forgotten_per_second": 13.62, + "waited_ticks_per_second": 0.0, + "chunks_total": 7746, + "chunks_per_bot": { + "explore": 774.6 + }, + "unique_chunks_per_bot": { + "explore": 748.7 + }, + "resent": 0, + "pulled_back": 1150, + "moved_too_quickly": 5, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "explore", + "start_seconds": 9.42, + "mspt": { + "average": 23.37, + "p50": 21.61, + "p95": 42.59, + "p99": 60.21 + }, + "mspt_worst_p99": 102.1, + "tps": 20.0, + "rss_mb": { + "mean": 889.21, + "max": 950.7 + }, + "cpu_percent": 117.59, + "chunks_per_second": 35.34, + "unique_per_second": 31.62, + "forgotten_per_second": 35.12, + "waited_ticks_per_second": 0.0, + "chunks_total": 20137, + "chunks_per_bot": { + "explore": 805.48 + }, + "unique_chunks_per_bot": { + "explore": 774.52 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "explore", + "start_seconds": 15.14, + "mspt": { + "average": 30.28, + "p50": 28.17, + "p95": 44.81, + "p99": 71.77 + }, + "mspt_worst_p99": 158.1, + "tps": 20.0, + "rss_mb": { + "mean": 3574.32, + "max": 3692.7 + }, + "cpu_percent": 173.43, + "chunks_per_second": 32.76, + "unique_per_second": 29.38, + "forgotten_per_second": 33.02, + "waited_ticks_per_second": 0.0, + "chunks_total": 19417, + "chunks_per_bot": { + "explore": 776.68 + }, + "unique_chunks_per_bot": { + "explore": 747.36 + }, + "resent": 0, + "pulled_back": 4728, + "moved_too_quickly": 15, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "explore", + "start_seconds": 9.11, + "mspt": { + "average": 30.59, + "p50": 25.19, + "p95": 67.94, + "p99": 90.77 + }, + "mspt_worst_p99": 122.1, + "tps": 20.0, + "rss_mb": { + "mean": 915.93, + "max": 978.0 + }, + "cpu_percent": 132.13, + "chunks_per_second": 71.31, + "unique_per_second": 63.56, + "forgotten_per_second": 71.19, + "waited_ticks_per_second": 0.0, + "chunks_total": 41365, + "chunks_per_bot": { + "explore": 827.3 + }, + "unique_chunks_per_bot": { + "explore": 793.14 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 50, + "behaviour": "explore", + "start_seconds": 15.35, + "mspt": { + "average": 43.52, + "p50": 41.69, + "p95": 56.18, + "p99": 84.55 + }, + "mspt_worst_p99": 155.1, + "tps": 20.0, + "rss_mb": { + "mean": 3801.29, + "max": 3920.7 + }, + "cpu_percent": 198.97, + "chunks_per_second": 65.86, + "unique_per_second": 58.63, + "forgotten_per_second": 66.12, + "waited_ticks_per_second": 0.0, + "chunks_total": 39790, + "chunks_per_bot": { + "explore": 795.8 + }, + "unique_chunks_per_bot": { + "explore": 764.48 + }, + "resent": 0, + "pulled_back": 8392, + "moved_too_quickly": 38, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 35 + } + ] +} diff --git a/benchmarks/server-2026-10-17/earlier-002f14ce/load.txt b/benchmarks/server-2026-10-17/earlier-002f14ce/load.txt new file mode 100644 index 000000000..853252809 --- /dev/null +++ b/benchmarks/server-2026-10-17/earlier-002f14ce/load.txt @@ -0,0 +1,278 @@ +2026-10-17T13:17:13Z thinkpad: 2.76 2.26 2.04 | macmini (bots): { 73.08 70.73 75.98 } +2026-10-17T13:17:43Z thinkpad: 2.90 2.33 2.07 | macmini (bots): { 75.66 71.62 76.12 } +2026-10-17T13:18:13Z thinkpad: 3.02 2.43 2.11 | macmini (bots): { 65.77 69.61 75.24 } +2026-10-17T13:18:44Z thinkpad: 2.15 2.27 2.07 | macmini (bots): { 59.85 67.75 74.37 } +2026-10-17T13:19:14Z thinkpad: 1.45 2.09 2.02 | macmini (bots): { 63.86 67.87 74.17 } +2026-10-17T13:19:44Z thinkpad: 1.28 1.98 1.98 | macmini (bots): { 63.51 67.54 73.84 } +2026-10-17T13:20:14Z thinkpad: 1.14 1.89 1.95 | macmini (bots): { 65.82 67.85 73.71 } +2026-10-17T13:20:45Z thinkpad: 0.90 1.77 1.91 | macmini (bots): { 63.62 67.08 73.22 } +2026-10-17T13:21:15Z thinkpad: 0.55 1.60 1.85 | macmini (bots): { 62.45 66.37 72.75 } +2026-10-17T13:21:45Z thinkpad: 0.79 1.54 1.82 | macmini (bots): { 67.04 67.04 72.76 } +2026-10-17T13:22:15Z thinkpad: 1.96 1.75 1.88 | macmini (bots): { 65.53 66.60 72.40 } +2026-10-17T13:22:46Z thinkpad: 1.82 1.75 1.88 | macmini (bots): { 61.52 65.55 71.82 } +2026-10-17T13:23:16Z thinkpad: 1.29 1.63 1.83 | macmini (bots): { 62.36 65.36 71.53 } +2026-10-17T13:23:46Z thinkpad: 0.86 1.49 1.78 | macmini (bots): { 68.76 66.51 71.73 } +2026-10-17T13:24:16Z thinkpad: 1.29 1.52 1.78 | macmini (bots): { 77.68 68.77 72.36 } +2026-10-17T13:24:46Z thinkpad: 1.82 1.62 1.80 | macmini (bots): { 74.03 68.88 72.28 } +2026-10-17T13:25:17Z thinkpad: 1.50 1.56 1.78 | macmini (bots): { 71.53 68.76 72.12 } +2026-10-17T13:25:47Z thinkpad: 0.96 1.42 1.72 | macmini (bots): { 74.46 69.85 72.40 } +2026-10-17T13:26:17Z thinkpad: 0.87 1.35 1.69 | macmini (bots): { 80.85 71.79 73.01 } +2026-10-17T13:26:47Z thinkpad: 3.95 2.02 1.90 | macmini (bots): { 74.77 71.31 72.81 } +2026-10-17T13:27:18Z thinkpad: 2.98 1.97 1.89 | macmini (bots): { 70.52 70.52 72.46 } +2026-10-17T13:27:48Z thinkpad: 3.78 2.26 1.99 | macmini (bots): { 67.62 69.91 72.18 } +2026-10-17T13:28:18Z thinkpad: 3.05 2.23 1.99 | macmini (bots): { 72.51 70.83 72.43 } +2026-10-17T13:28:48Z thinkpad: 2.05 2.07 1.94 | macmini (bots): { 79.07 72.64 73.03 } +2026-10-17T13:29:19Z thinkpad: 2.09 2.11 1.96 | macmini (bots): { 90.42 75.94 74.21 } +2026-10-17T13:29:49Z thinkpad: 1.61 1.99 1.93 | macmini (bots): { 102.68 80.15 75.78 } +2026-10-17T13:30:19Z thinkpad: 2.31 2.12 1.97 | macmini (bots): { 111.15 84.19 77.39 } +2026-10-17T13:30:49Z thinkpad: 4.14 2.61 2.14 | macmini (bots): { 115.67 87.82 78.94 } +2026-10-17T13:31:20Z thinkpad: 4.84 2.92 2.26 | macmini (bots): { 122.95 92.31 80.88 } +2026-10-17T13:31:50Z thinkpad: 6.84 3.60 2.51 | macmini (bots): { 136.25 98.41 83.48 } +2026-10-17T13:32:20Z thinkpad: 4.85 3.43 2.49 | macmini (bots): { 128.59 100.39 84.73 } +2026-10-17T13:32:50Z thinkpad: 3.48 3.25 2.46 | macmini (bots): { 111.05 98.73 84.66 } +2026-10-17T13:33:20Z thinkpad: 3.09 3.15 2.45 | macmini (bots): { 102.40 98.03 84.98 } +2026-10-17T13:33:51Z thinkpad: 2.47 2.99 2.42 | macmini (bots): { 94.57 96.54 84.89 } +2026-10-17T13:34:21Z thinkpad: 2.42 2.94 2.42 | macmini (bots): { 86.26 94.36 84.50 } +2026-10-17T13:34:51Z thinkpad: 2.95 3.04 2.47 | macmini (bots): { 76.90 91.56 83.84 } +2026-10-17T13:35:21Z thinkpad: 2.66 2.96 2.46 | macmini (bots): { 65.57 87.42 82.61 } +2026-10-17T13:35:52Z thinkpad: 2.47 2.88 2.45 | macmini (bots): { 64.13 85.05 81.92 } +2026-10-17T13:36:22Z thinkpad: 3.89 3.17 2.56 | macmini (bots): { 58.65 81.74 80.83 } +2026-10-17T13:36:52Z thinkpad: 6.43 3.83 2.80 | macmini (bots): { 66.32 81.32 80.71 } +2026-10-17T13:37:22Z thinkpad: 8.73 4.60 3.09 | macmini (bots): { 67.30 80.14 80.30 } +2026-10-17T13:37:53Z thinkpad: 9.03 5.10 3.30 | macmini (bots): { 63.66 78.24 79.62 } +2026-10-17T13:38:23Z thinkpad: 9.49 5.57 3.52 | macmini (bots): { 61.25 76.23 78.84 } +2026-10-17T13:38:53Z thinkpad: 7.55 5.50 3.56 | macmini (bots): { 62.30 74.88 78.25 } +2026-10-17T13:39:23Z thinkpad: 8.62 5.99 3.79 | macmini (bots): { 57.34 72.56 77.29 } +2026-10-17T13:39:53Z thinkpad: 6.65 5.81 3.80 | macmini (bots): { 51.24 69.62 76.06 } +2026-10-17T13:40:24Z thinkpad: 5.55 5.62 3.80 | macmini (bots): { 48.87 67.26 74.98 } +2026-10-17T13:40:54Z thinkpad: 6.11 5.71 3.89 | macmini (bots): { 51.16 66.08 74.29 } +2026-10-17T13:41:24Z thinkpad: 8.93 6.43 4.19 | macmini (bots): { 48.67 64.00 73.24 } +2026-10-17T13:41:54Z thinkpad: 9.05 6.74 4.36 | macmini (bots): { 46.47 61.96 72.18 } +2026-10-17T13:42:25Z thinkpad: 7.70 6.65 4.41 | macmini (bots): { 44.24 60.05 71.14 } +2026-10-17T13:42:55Z thinkpad: 5.64 6.24 4.34 | macmini (bots): { 49.30 59.83 70.68 } +2026-10-17T13:43:25Z thinkpad: 4.83 6.02 4.33 | macmini (bots): { 47.39 58.24 69.72 } +2026-10-17T13:43:55Z thinkpad: 3.57 5.61 4.25 | macmini (bots): { 47.32 57.23 68.96 } +2026-10-17T13:44:25Z thinkpad: 3.41 5.40 4.23 | macmini (bots): { 47.86 56.49 68.28 } +2026-10-17T13:44:55Z thinkpad: 3.37 5.21 4.20 | macmini (bots): { 50.10 56.19 67.77 } +2026-10-17T13:45:26Z thinkpad: 3.78 5.16 4.22 | macmini (bots): { 51.91 55.98 67.28 } +2026-10-17T13:45:56Z thinkpad: 4.34 5.12 4.23 | macmini (bots): { 49.16 54.85 66.42 } +2026-10-17T13:46:26Z thinkpad: 10.04 6.46 4.71 | macmini (bots): { 45.01 53.33 65.47 } +2026-10-17T13:46:56Z thinkpad: 18.10 8.73 5.52 | macmini (bots): { 46.41 52.87 64.88 } +2026-10-17T13:47:27Z thinkpad: 14.99 8.91 5.68 | macmini (bots): { 51.12 53.21 64.57 } +2026-10-17T13:47:57Z thinkpad: 13.46 9.27 5.92 | macmini (bots): { 53.84 53.81 64.40 } +2026-10-17T13:48:27Z thinkpad: 12.76 9.41 6.07 | macmini (bots): { 51.94 53.27 63.84 } +2026-10-17T13:48:58Z thinkpad: 10.20 9.11 6.08 | macmini (bots): { 50.70 52.83 63.31 } +2026-10-17T13:49:28Z thinkpad: 9.95 9.25 6.23 | macmini (bots): { 51.51 52.89 62.97 } +2026-10-17T13:49:58Z thinkpad: 11.98 9.87 6.53 | macmini (bots): { 50.98 52.73 62.57 } +2026-10-17T13:50:29Z thinkpad: 14.96 10.76 6.94 | macmini (bots): { 49.68 52.31 62.08 } +2026-10-17T13:50:59Z thinkpad: 10.54 10.09 6.83 | macmini (bots): { 53.34 53.01 62.00 } +2026-10-17T13:51:29Z thinkpad: 10.21 10.01 6.91 | macmini (bots): { 51.61 52.69 61.57 } +2026-10-17T13:52:00Z thinkpad: 11.86 10.42 7.14 | macmini (bots): { 48.94 51.89 60.97 } +2026-10-17T13:52:30Z thinkpad: 10.88 10.33 7.22 | macmini (bots): { 48.46 51.44 60.49 } +2026-10-17T13:53:01Z thinkpad: 10.94 10.41 7.35 | macmini (bots): { 54.27 52.41 60.47 } +2026-10-17T13:53:31Z thinkpad: 10.77 10.42 7.45 | macmini (bots): { 57.13 53.30 60.51 } +2026-10-17T13:54:02Z thinkpad: 10.62 10.40 7.53 | macmini (bots): { 53.78 52.84 60.09 } +2026-10-17T13:54:32Z thinkpad: 10.85 10.48 7.66 | macmini (bots): { 52.01 52.51 59.73 } +2026-10-17T13:55:02Z thinkpad: 10.36 10.39 7.73 | macmini (bots): { 53.44 52.83 59.59 } +2026-10-17T13:55:32Z thinkpad: 8.58 9.95 7.67 | macmini (bots): { 58.66 54.12 59.82 } +2026-10-17T13:56:03Z thinkpad: 7.29 9.52 7.60 | macmini (bots): { 60.98 55.05 59.96 } +2026-10-17T13:56:33Z thinkpad: 5.02 8.75 7.40 | macmini (bots): { 65.14 56.71 60.39 } +2026-10-17T13:57:03Z thinkpad: 5.03 8.38 7.32 | macmini (bots): { 57.51 55.73 59.91 } +2026-10-17T13:57:34Z thinkpad: 7.39 8.66 7.45 | macmini (bots): { 52.57 54.73 59.41 } +2026-10-17T13:58:04Z thinkpad: 10.56 9.45 7.76 | macmini (bots): { 53.11 54.60 59.20 } +2026-10-17T13:58:34Z thinkpad: 28.87 14.20 9.41 | macmini (bots): { 54.53 54.93 59.16 } +2026-10-17T13:59:05Z thinkpad: 39.33 17.79 10.73 | macmini (bots): { 62.67 56.86 59.71 } +2026-10-17T13:59:35Z thinkpad: 34.07 18.33 11.12 | macmini (bots): { 61.78 57.22 59.75 } +2026-10-17T14:00:06Z thinkpad: 34.42 19.72 11.80 | macmini (bots): { 53.10 55.55 59.03 } +2026-10-17T14:00:36Z thinkpad: 33.60 20.94 12.46 | macmini (bots): { 48.70 54.23 58.43 } +2026-10-17T14:01:07Z thinkpad: 29.29 20.99 12.74 | macmini (bots): { 50.25 54.00 58.20 } +2026-10-17T14:01:37Z thinkpad: 20.83 19.79 12.61 | macmini (bots): { 50.48 53.74 57.96 } +2026-10-17T14:02:08Z thinkpad: 21.74 20.44 13.11 | macmini (bots): { 48.65 53.04 57.56 } +2026-10-17T14:02:38Z thinkpad: 17.68 19.67 13.09 | macmini (bots): { 54.17 53.85 57.69 } +2026-10-17T14:03:08Z thinkpad: 13.97 18.55 12.93 | macmini (bots): { 53.39 53.84 57.56 } +2026-10-17T14:03:39Z thinkpad: 14.10 18.17 12.98 | macmini (bots): { 50.71 53.09 57.16 } +2026-10-17T14:04:09Z thinkpad: 13.60 17.65 12.97 | macmini (bots): { 52.30 53.28 57.08 } +2026-10-17T14:04:39Z thinkpad: 12.09 16.91 12.88 | macmini (bots): { 51.52 53.03 56.87 } +2026-10-17T14:05:10Z thinkpad: 10.15 15.98 12.69 | macmini (bots): { 54.53 53.57 56.91 } +2026-10-17T14:05:42Z thinkpad: 7.77 14.86 12.42 | macmini (bots): { 63.58 55.55 57.49 } +2026-10-17T14:06:12Z thinkpad: 6.58 13.89 12.18 | macmini (bots): { 75.29 59.41 58.84 } +2026-10-17T14:06:42Z thinkpad: 5.96 13.02 11.94 | macmini (bots): { 78.06 61.64 59.67 } +2026-10-17T14:07:12Z thinkpad: 4.86 12.08 11.66 | macmini (bots): { 72.26 61.85 59.81 } +2026-10-17T14:07:42Z thinkpad: 4.49 11.18 11.38 | macmini (bots): { 67.84 61.73 59.84 } +2026-10-17T14:08:13Z thinkpad: 4.37 10.51 11.15 | macmini (bots): { 64.25 61.45 59.80 } +2026-10-17T14:08:43Z thinkpad: 4.57 9.97 10.94 | macmini (bots): { 57.86 60.14 59.38 } +2026-10-17T14:09:13Z thinkpad: 4.60 9.46 10.74 | macmini (bots): { 60.21 60.39 59.49 } +2026-10-17T14:09:43Z thinkpad: 4.20 8.89 10.51 | macmini (bots): { 57.11 59.57 59.22 } +2026-10-17T14:10:14Z thinkpad: 3.80 8.36 10.28 | macmini (bots): { 66.05 61.48 59.92 } +2026-10-17T14:10:44Z thinkpad: 4.19 8.00 10.10 | macmini (bots): { 78.51 64.91 61.22 } +2026-10-17T14:11:14Z thinkpad: 4.27 7.65 9.92 | macmini (bots): { 74.15 65.33 61.51 } +2026-10-17T14:11:44Z thinkpad: 4.35 7.35 9.74 | macmini (bots): { 62.47 63.48 60.98 } +2026-10-17T14:12:14Z thinkpad: 4.98 7.22 9.62 | macmini (bots): { 56.73 61.86 60.46 } +2026-10-17T14:12:45Z thinkpad: 4.60 6.91 9.44 | macmini (bots): { 57.78 61.49 60.37 } +2026-10-17T14:13:15Z thinkpad: 4.15 6.57 9.25 | macmini (bots): { 56.21 60.71 60.12 } +2026-10-17T14:13:45Z thinkpad: 3.87 6.25 9.05 | macmini (bots): { 54.75 60.00 59.89 } +2026-10-17T14:14:15Z thinkpad: 3.86 6.02 8.89 | macmini (bots): { 52.21 58.88 59.49 } +2026-10-17T14:14:46Z thinkpad: 3.14 5.64 8.67 | macmini (bots): { 58.81 59.59 59.71 } +2026-10-17T14:15:16Z thinkpad: 3.76 5.54 8.54 | macmini (bots): { 64.13 60.86 60.16 } +2026-10-17T14:15:46Z thinkpad: 3.91 5.41 8.40 | macmini (bots): { 59.02 59.97 59.87 } +2026-10-17T14:16:16Z thinkpad: 4.33 5.35 8.28 | macmini (bots): { 64.48 60.97 60.22 } +2026-10-17T14:16:46Z thinkpad: 5.20 5.44 8.21 | macmini (bots): { 79.56 64.98 61.70 } +2026-10-17T14:17:17Z thinkpad: 4.88 5.36 8.10 | macmini (bots): { 94.21 69.99 63.63 } +2026-10-17T14:17:47Z thinkpad: 4.80 5.30 8.00 | macmini (bots): { 85.75 70.51 64.08 } +2026-10-17T14:18:17Z thinkpad: 4.82 5.25 7.89 | macmini (bots): { 82.33 71.25 64.57 } +2026-10-17T14:18:48Z thinkpad: 4.50 5.12 7.75 | macmini (bots): { 77.67 71.11 64.75 } +2026-10-17T14:19:18Z thinkpad: 3.76 4.88 7.58 | macmini (bots): { 77.29 71.67 65.17 } +2026-10-17T14:19:48Z thinkpad: 3.68 4.75 7.45 | macmini (bots): { 71.16 70.71 65.05 } +2026-10-17T14:20:18Z thinkpad: 4.25 4.82 7.39 | macmini (bots): { 66.97 69.63 64.85 } +2026-10-17T14:20:49Z thinkpad: 4.72 4.89 7.33 | macmini (bots): { 59.23 67.50 64.24 } +2026-10-17T14:21:19Z thinkpad: 5.12 4.98 7.28 | macmini (bots): { 52.38 65.10 63.48 } +2026-10-17T14:21:49Z thinkpad: 5.69 5.13 7.26 | macmini (bots): { 59.43 65.53 63.69 } +2026-10-17T14:22:19Z thinkpad: 5.78 5.19 7.21 | macmini (bots): { 62.28 65.65 63.80 } +2026-10-17T14:22:50Z thinkpad: 5.34 5.14 7.13 | macmini (bots): { 57.40 64.12 63.30 } +2026-10-17T14:23:20Z thinkpad: 4.89 5.04 7.03 | macmini (bots): { 58.39 63.61 63.14 } +2026-10-17T14:23:50Z thinkpad: 5.00 5.07 6.98 | macmini (bots): { 64.88 64.63 63.52 } +2026-10-17T14:24:20Z thinkpad: 3.88 4.80 6.83 | macmini (bots): { 80.70 68.30 64.88 } +2026-10-17T14:24:51Z thinkpad: 2.90 4.47 6.65 | macmini (bots): { 94.09 72.75 66.61 } +2026-10-17T14:25:21Z thinkpad: 2.63 4.25 6.51 | macmini (bots): { 93.13 74.91 67.63 } +2026-10-17T14:25:51Z thinkpad: 6.66 5.07 6.71 | macmini (bots): { 72.87 71.72 66.72 } +2026-10-17T14:26:21Z thinkpad: 7.15 5.34 6.74 | macmini (bots): { 67.03 70.37 66.40 } +2026-10-17T14:26:51Z thinkpad: 6.20 5.29 6.68 | macmini (bots): { 58.68 68.08 65.71 } +2026-10-17T14:27:22Z thinkpad: 6.11 5.35 6.66 | macmini (bots): { 55.26 66.28 65.14 } +2026-10-17T14:27:52Z thinkpad: 5.45 5.26 6.59 | macmini (bots): { 56.51 65.74 64.99 } +2026-10-17T14:28:22Z thinkpad: 6.11 5.45 6.61 | macmini (bots): { 52.24 63.77 64.30 } +2026-10-17T14:28:52Z thinkpad: 6.06 5.50 6.59 | macmini (bots): { 55.56 63.38 64.13 } +2026-10-17T14:29:23Z thinkpad: 6.11 5.58 6.58 | macmini (bots): { 59.66 63.63 64.20 } +2026-10-17T14:29:53Z thinkpad: 5.52 5.54 6.53 | macmini (bots): { 63.71 64.30 64.42 } +2026-10-17T14:30:23Z thinkpad: 4.79 5.35 6.43 | macmini (bots): { 69.84 65.61 64.89 } +2026-10-17T14:30:53Z thinkpad: 6.21 5.63 6.49 | macmini (bots): { 62.76 64.46 64.50 } +2026-10-17T14:31:23Z thinkpad: 8.36 6.21 6.66 | macmini (bots): { 53.51 62.09 63.64 } +2026-10-17T14:31:54Z thinkpad: 8.49 6.45 6.72 | macmini (bots): { 52.80 60.98 63.18 } +2026-10-17T14:32:24Z thinkpad: 8.28 6.59 6.77 | macmini (bots): { 48.98 59.20 62.45 } +2026-10-17T14:32:54Z thinkpad: 8.02 6.70 6.79 | macmini (bots): { 48.95 58.30 62.02 } +2026-10-17T14:33:25Z thinkpad: 7.02 6.59 6.76 | macmini (bots): { 51.06 57.70 61.66 } +2026-10-17T14:33:55Z thinkpad: 7.09 6.62 6.76 | macmini (bots): { 70.89 61.81 63.00 } +2026-10-17T14:34:25Z thinkpad: 6.18 6.44 6.69 | macmini (bots): { 69.38 62.46 63.21 } +2026-10-17T14:34:55Z thinkpad: 6.67 6.52 6.72 | macmini (bots): { 76.87 64.84 64.04 } +2026-10-17T14:35:25Z thinkpad: 4.76 6.08 6.56 | macmini (bots): { 86.19 68.04 65.21 } +2026-10-17T14:35:56Z thinkpad: 3.47 5.64 6.40 | macmini (bots): { 101.03 73.28 67.21 } +2026-10-17T14:36:26Z thinkpad: 2.68 5.24 6.24 | macmini (bots): { 106.05 77.09 68.79 } +2026-10-17T14:36:56Z thinkpad: 2.41 4.91 6.10 | macmini (bots): { 107.57 80.20 70.20 } +2026-10-17T14:37:26Z thinkpad: 3.74 4.99 6.08 | macmini (bots): { 101.16 81.38 70.98 } +2026-10-17T14:37:57Z thinkpad: 5.96 5.39 6.18 | macmini (bots): { 86.51 79.61 70.70 } +2026-10-17T14:38:27Z thinkpad: 7.91 5.98 6.35 | macmini (bots): { 81.13 79.06 70.81 } +2026-10-17T14:38:57Z thinkpad: 8.45 6.25 6.43 | macmini (bots): { 79.56 79.01 71.08 } +2026-10-17T14:39:27Z thinkpad: 8.75 6.52 6.52 | macmini (bots): { 71.92 77.22 70.71 } +2026-10-17T14:39:58Z thinkpad: 8.86 6.76 6.60 | macmini (bots): { 71.38 76.58 70.70 } +2026-10-17T14:40:28Z thinkpad: 8.74 6.98 6.68 | macmini (bots): { 71.25 76.12 70.74 } +2026-10-17T14:40:58Z thinkpad: 7.99 6.96 6.68 | macmini (bots): { 70.49 75.51 70.71 } +2026-10-17T14:41:28Z thinkpad: 7.64 7.01 6.70 | macmini (bots): { 68.93 74.58 70.56 } +2026-10-17T14:41:59Z thinkpad: 6.57 6.84 6.66 | macmini (bots): { 68.35 73.75 70.39 } +2026-10-17T14:42:29Z thinkpad: 5.83 6.62 6.59 | macmini (bots): { 63.57 72.20 69.95 } +2026-10-17T14:42:59Z thinkpad: 7.40 6.91 6.69 | macmini (bots): { 56.77 69.69 69.12 } +2026-10-17T14:43:29Z thinkpad: 9.07 7.37 6.85 | macmini (bots): { 49.53 66.71 68.06 } +2026-10-17T14:44:00Z thinkpad: 9.39 7.63 6.96 | macmini (bots): { 43.53 63.68 66.91 } +2026-10-17T14:44:30Z thinkpad: 9.16 7.73 7.01 | macmini (bots): { 41.12 61.23 65.92 } +2026-10-17T14:45:00Z thinkpad: 7.96 7.57 6.98 | macmini (bots): { 40.62 59.23 65.03 } +2026-10-17T14:45:30Z thinkpad: 6.34 7.24 6.89 | macmini (bots): { 38.17 56.80 63.94 } +2026-10-17T14:46:01Z thinkpad: 6.39 7.18 6.88 | macmini (bots): { 40.35 55.54 63.24 } +2026-10-17T14:46:31Z thinkpad: 5.63 6.95 6.82 | macmini (bots): { 48.66 56.25 63.24 } +2026-10-17T14:47:01Z thinkpad: 4.73 6.60 6.70 | macmini (bots): { 46.68 55.20 62.62 } +2026-10-17T14:47:31Z thinkpad: 9.18 7.55 7.02 | macmini (bots): { 41.45 53.19 61.64 } +2026-10-17T14:48:01Z thinkpad: 10.40 8.02 7.19 | macmini (bots): { 40.76 51.87 60.87 } +2026-10-17T14:48:32Z thinkpad: 8.24 7.73 7.12 | macmini (bots): { 44.46 51.67 60.48 } +2026-10-17T14:49:02Z thinkpad: 9.60 8.09 7.26 | macmini (bots): { 38.75 49.66 59.45 } +2026-10-17T14:49:32Z thinkpad: 8.90 8.10 7.29 | macmini (bots): { 34.93 47.72 58.41 } +2026-10-17T14:50:02Z thinkpad: 11.17 8.66 7.50 | macmini (bots): { 35.70 46.68 57.66 } +2026-10-17T14:50:33Z thinkpad: 12.06 9.19 7.72 | macmini (bots): { 43.94 47.43 57.54 } +2026-10-17T14:51:03Z thinkpad: 12.50 9.49 7.87 | macmini (bots): { 40.76 46.36 56.80 } +2026-10-17T14:51:33Z thinkpad: 10.35 9.24 7.83 | macmini (bots): { 44.81 46.69 56.55 } +2026-10-17T14:52:03Z thinkpad: 9.32 9.09 7.83 | macmini (bots): { 54.94 48.97 57.04 } +2026-10-17T14:52:34Z thinkpad: 7.07 8.58 7.70 | macmini (bots): { 54.59 49.54 56.92 } +2026-10-17T14:53:04Z thinkpad: 5.33 8.01 7.54 | macmini (bots): { 67.48 53.27 58.02 } +2026-10-17T14:53:34Z thinkpad: 7.22 8.20 7.62 | macmini (bots): { 57.82 52.33 57.52 } +2026-10-17T14:54:04Z thinkpad: 9.69 8.73 7.81 | macmini (bots): { 46.67 50.20 56.57 } +2026-10-17T14:54:35Z thinkpad: 9.14 8.66 7.82 | macmini (bots): { 49.88 50.61 56.49 } +2026-10-17T14:55:05Z thinkpad: 9.75 8.87 7.91 | macmini (bots): { 46.88 49.95 56.06 } +2026-10-17T14:55:35Z thinkpad: 8.51 8.67 7.88 | macmini (bots): { 42.90 48.80 55.43 } +2026-10-17T14:56:06Z thinkpad: 8.46 8.62 7.89 | macmini (bots): { 49.23 49.72 55.53 } +2026-10-17T14:56:36Z thinkpad: 6.19 8.07 7.73 | macmini (bots): { 54.41 50.86 55.74 } +2026-10-17T14:57:06Z thinkpad: 4.69 7.53 7.56 | macmini (bots): { 60.26 52.69 56.24 } +2026-10-17T14:57:36Z thinkpad: 3.21 6.90 7.35 | macmini (bots): { 67.66 55.21 57.03 } +2026-10-17T14:58:06Z thinkpad: 1.94 6.24 7.11 | macmini (bots): { 75.73 58.41 58.13 } +2026-10-17T14:58:37Z thinkpad: 1.18 5.64 6.89 | macmini (bots): { 74.69 59.90 58.68 } +2026-10-17T14:59:07Z thinkpad: 0.78 5.12 6.67 | macmini (bots): { 77.10 61.82 59.42 } +2026-10-17T14:59:37Z thinkpad: 0.88 4.72 6.49 | macmini (bots): { 87.05 65.59 60.86 } +2026-10-17T15:00:07Z thinkpad: 0.53 4.27 6.28 | macmini (bots): { 78.18 65.51 60.99 } +2026-10-17T15:00:38Z thinkpad: 0.38 3.88 6.09 | macmini (bots): { 79.38 67.00 61.69 } +2026-10-17T15:01:08Z thinkpad: 1.50 3.80 5.99 | macmini (bots): { 78.36 67.86 62.18 } +2026-10-17T15:01:38Z thinkpad: 2.97 3.93 5.95 | macmini (bots): { 74.18 67.90 62.39 } +2026-10-17T15:02:08Z thinkpad: 2.61 3.77 5.83 | macmini (bots): { 68.63 67.00 62.25 } +2026-10-17T15:02:39Z thinkpad: 2.10 3.55 5.69 | macmini (bots): { 69.84 67.50 62.60 } +2026-10-17T15:03:09Z thinkpad: 1.44 3.25 5.53 | macmini (bots): { 67.10 67.06 62.61 } +2026-10-17T15:03:39Z thinkpad: 1.39 3.08 5.40 | macmini (bots): { 60.83 65.50 62.19 } +2026-10-17T15:04:09Z thinkpad: 1.28 2.88 5.26 | macmini (bots): { 75.40 68.61 63.46 } +2026-10-17T15:04:40Z thinkpad: 2.30 2.99 5.22 | macmini (bots): { 106.08 76.54 66.50 } +2026-10-17T15:05:10Z thinkpad: 2.65 3.02 5.15 | macmini (bots): { 111.96 81.09 68.52 } +2026-10-17T15:05:40Z thinkpad: 2.41 2.92 5.05 | macmini (bots): { 104.19 82.08 69.31 } +2026-10-17T15:06:10Z thinkpad: 2.71 2.95 4.99 | macmini (bots): { 116.20 87.04 71.54 } +2026-10-17T15:06:40Z thinkpad: 2.51 2.87 4.90 | macmini (bots): { 136.68 94.24 74.66 } +2026-10-17T15:07:11Z thinkpad: 3.68 3.11 4.91 | macmini (bots): { 132.76 97.43 76.50 } +2026-10-17T15:07:41Z thinkpad: 3.77 3.19 4.88 | macmini (bots): { 132.73 100.97 78.51 } +2026-10-17T15:08:11Z thinkpad: 3.00 3.06 4.79 | macmini (bots): { 116.92 100.33 79.07 } +2026-10-17T15:08:41Z thinkpad: 2.40 2.91 4.68 | macmini (bots): { 109.83 100.07 79.70 } +2026-10-17T15:09:12Z thinkpad: 2.18 2.81 4.59 | macmini (bots): { 113.21 101.71 81.00 } +2026-10-17T15:09:42Z thinkpad: 1.67 2.62 4.47 | macmini (bots): { 131.87 107.11 83.66 } +2026-10-17T15:10:12Z thinkpad: 1.90 2.58 4.39 | macmini (bots): { 133.45 109.92 85.49 } +2026-10-17T15:10:42Z thinkpad: 4.00 3.02 4.48 | macmini (bots): { 130.14 111.67 86.99 } +2026-10-17T15:11:13Z thinkpad: 4.77 3.32 4.54 | macmini (bots): { 118.78 110.28 87.32 } +2026-10-17T15:11:43Z thinkpad: 3.86 3.24 4.47 | macmini (bots): { 124.01 112.36 88.87 } +2026-10-17T15:12:13Z thinkpad: 3.61 3.26 4.44 | macmini (bots): { 122.21 112.94 89.88 } +2026-10-17T15:12:43Z thinkpad: 3.12 3.17 4.36 | macmini (bots): { 111.62 111.10 90.00 } +2026-10-17T15:13:14Z thinkpad: 3.07 3.14 4.31 | macmini (bots): { 122.97 113.95 91.77 } +2026-10-17T15:13:44Z thinkpad: 3.33 3.20 4.30 | macmini (bots): { 102.64 109.89 91.07 } +2026-10-17T15:14:14Z thinkpad: 2.85 3.10 4.23 | macmini (bots): { 94.46 107.15 90.72 } +2026-10-17T15:14:44Z thinkpad: 2.66 3.03 4.17 | macmini (bots): { 94.94 106.07 90.90 } +2026-10-17T15:15:14Z thinkpad: 2.54 2.96 4.11 | macmini (bots): { 94.52 104.93 91.01 } +2026-10-17T15:15:45Z thinkpad: 3.20 3.08 4.11 | macmini (bots): { 84.00 101.47 90.25 } +2026-10-17T15:16:15Z thinkpad: 5.56 3.61 4.25 | macmini (bots): { 83.16 99.76 90.02 } +2026-10-17T15:16:45Z thinkpad: 5.66 3.84 4.30 | macmini (bots): { 69.71 94.66 88.56 } +2026-10-17T15:17:15Z thinkpad: 5.04 3.90 4.31 | macmini (bots): { 59.31 89.78 87.00 } +2026-10-17T15:17:46Z thinkpad: 4.29 3.87 4.29 | macmini (bots): { 58.57 86.47 85.89 } +2026-10-17T15:18:16Z thinkpad: 4.16 3.91 4.29 | macmini (bots): { 56.43 83.29 84.75 } +2026-10-17T15:18:46Z thinkpad: 2.80 3.60 4.18 | macmini (bots): { 50.60 79.22 83.22 } +2026-10-17T15:19:16Z thinkpad: 1.85 3.28 4.05 | macmini (bots): { 54.97 77.50 82.46 } +2026-10-17T15:19:47Z thinkpad: 1.82 3.14 3.98 | macmini (bots): { 56.82 75.80 81.67 } +2026-10-17T15:20:17Z thinkpad: 2.19 3.10 3.94 | macmini (bots): { 59.30 74.46 80.97 } +2026-10-17T15:20:47Z thinkpad: 4.44 3.54 4.06 | macmini (bots): { 55.22 72.11 79.90 } +2026-10-17T15:21:17Z thinkpad: 5.99 4.03 4.21 | macmini (bots): { 49.41 69.01 78.50 } +2026-10-17T15:21:48Z thinkpad: 4.85 3.95 4.18 | macmini (bots): { 46.89 66.58 77.29 } +2026-10-17T15:22:18Z thinkpad: 4.31 3.92 4.16 | macmini (bots): { 45.13 64.31 76.10 } +2026-10-17T15:22:48Z thinkpad: 4.28 3.95 4.16 | macmini (bots): { 51.82 63.98 75.56 } +2026-10-17T15:23:18Z thinkpad: 4.75 4.11 4.21 | macmini (bots): { 50.35 62.51 74.63 } +2026-10-17T15:23:48Z thinkpad: 4.08 4.02 4.17 | macmini (bots): { 44.62 60.01 73.31 } +2026-10-17T15:24:19Z thinkpad: 3.93 4.00 4.16 | macmini (bots): { 46.02 58.72 72.37 } +2026-10-17T15:24:49Z thinkpad: 3.67 3.92 4.13 | macmini (bots): { 47.23 57.90 71.60 } +2026-10-17T15:25:19Z thinkpad: 2.79 3.68 4.04 | macmini (bots): { 50.80 57.78 71.09 } +2026-10-17T15:25:49Z thinkpad: 2.79 3.58 3.99 | macmini (bots): { 47.66 56.35 70.11 } +2026-10-17T15:26:19Z thinkpad: 5.04 3.97 4.11 | macmini (bots): { 45.15 54.84 69.08 } +2026-10-17T15:26:50Z thinkpad: 8.46 4.86 4.40 | macmini (bots): { 43.59 53.51 68.10 } +2026-10-17T15:27:20Z thinkpad: 6.84 4.81 4.39 | macmini (bots): { 42.38 52.39 67.19 } +2026-10-17T15:27:50Z thinkpad: 8.24 5.33 4.58 | macmini (bots): { 49.87 53.02 66.82 } +2026-10-17T15:28:21Z thinkpad: 7.11 5.34 4.61 | macmini (bots): { 61.30 55.60 67.28 } +2026-10-17T15:28:51Z thinkpad: 6.11 5.27 4.61 | macmini (bots): { 67.60 57.74 67.66 } +2026-10-17T15:29:21Z thinkpad: 5.33 5.17 4.59 | macmini (bots): { 63.72 57.79 67.33 } +2026-10-17T15:29:51Z thinkpad: 3.57 4.75 4.47 | macmini (bots): { 60.80 57.62 66.94 } +2026-10-17T15:30:22Z thinkpad: 4.74 4.88 4.52 | macmini (bots): { 59.69 57.64 66.62 } +2026-10-17T15:30:52Z thinkpad: 3.44 4.54 4.42 | macmini (bots): { 66.89 59.49 66.97 } +2026-10-17T15:31:22Z thinkpad: 4.57 4.69 4.47 | macmini (bots): { 83.43 64.16 68.40 } +2026-10-17T15:31:52Z thinkpad: 7.17 5.30 4.68 | macmini (bots): { 77.62 64.77 68.49 } +2026-10-17T15:32:23Z thinkpad: 8.55 5.82 4.88 | macmini (bots): { 74.20 65.09 68.47 } +2026-10-17T15:32:53Z thinkpad: 7.49 5.85 4.92 | macmini (bots): { 82.07 67.70 69.29 } +2026-10-17T15:33:23Z thinkpad: 5.65 5.59 4.86 | macmini (bots): { 79.44 68.47 69.51 } +2026-10-17T15:33:54Z thinkpad: 5.73 5.64 4.91 | macmini (bots): { 78.94 69.26 69.76 } +2026-10-17T15:34:24Z thinkpad: 6.84 5.92 5.02 | macmini (bots): { 76.33 69.71 69.91 } +2026-10-17T15:34:54Z thinkpad: 5.11 5.59 4.94 | macmini (bots): { 86.09 72.63 70.95 } +2026-10-17T15:35:24Z thinkpad: 6.68 5.93 5.08 | macmini (bots): { 72.77 70.70 70.31 } +2026-10-17T15:35:55Z thinkpad: 5.75 5.75 5.04 | macmini (bots): { 71.50 70.49 70.24 } +2026-10-17T15:36:25Z thinkpad: 4.31 5.41 4.95 | macmini (bots): { 80.75 72.67 71.03 } +2026-10-17T15:36:55Z thinkpad: 3.15 5.02 4.83 | macmini (bots): { 89.83 75.59 72.14 } diff --git a/benchmarks/server-2026-10-17/earlier-002f14ce/walk-explore.json b/benchmarks/server-2026-10-17/earlier-002f14ce/walk-explore.json new file mode 100644 index 000000000..39dcd268c --- /dev/null +++ b/benchmarks/server-2026-10-17/earlier-002f14ce/walk-explore.json @@ -0,0 +1,695 @@ +{ + "kind": "horizon-server-benchmark", + "date": "2026-10-17T14:56:24+00:00", + "machine": { + "system": "Linux", + "machine": "x86_64", + "cpus": 4 + }, + "commit": "a9ee56a8b143999c9bc7056850ffcd622a9214aa", + "java": [ + "openjdk 25.0.4.1 2026-08-18" + ], + "settings": { + "servers": "horizon,vanilla", + "bots": "1,10,25,50,100", + "behaviour": "walk,explore", + "join_every": 1.0, + "warmup": 60, + "seconds": 180, + "seed": 12345, + "explore_speed": 0, + "gamerule": "", + "heap": "4g", + "out": "/home/ludovic/code/horizon/.claude/worktrees/server-host-4/.local/bench-out/walk-explore.json", + "nice": 19, + "bots_via": "ssh macmini", + "bots_bin": "/Users/ludovicmarie/code/horizon/.claude/worktrees/server-host-4/target/release/horizon-bots", + "connect": "", + "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": 1, + "behaviour": "walk", + "start_seconds": 9.51, + "mspt": { + "average": 5.77, + "p50": 5.15, + "p95": 11.04, + "p99": 28.99 + }, + "mspt_worst_p99": 45.9, + "tps": 20.0, + "rss_mb": { + "mean": 777.78, + "max": 779.8 + }, + "cpu_percent": 59.02, + "chunks_per_second": 9.94, + "unique_per_second": 0.45, + "forgotten_per_second": 9.99, + "waited_ticks_per_second": 0.0, + "chunks_total": 2763, + "chunks_per_bot": { + "walk": 2763.0 + }, + "unique_chunks_per_bot": { + "walk": 697.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 1, + "behaviour": "walk", + "start_seconds": 15.35, + "mspt": { + "average": 18.84, + "p50": 18.87, + "p95": 29.55, + "p99": 51.32 + }, + "mspt_worst_p99": 456.4, + "tps": 20.0, + "rss_mb": { + "mean": 3442.06, + "max": 3462.1 + }, + "cpu_percent": 70.63, + "chunks_per_second": 10.47, + "unique_per_second": 0.47, + "forgotten_per_second": 10.53, + "waited_ticks_per_second": 0.0, + "chunks_total": 2763, + "chunks_per_bot": { + "walk": 2763.0 + }, + "unique_chunks_per_bot": { + "walk": 697.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 10, + "behaviour": "walk", + "start_seconds": 9.31, + "mspt": { + "average": 16.23, + "p50": 15.6, + "p95": 29.32, + "p99": 44.43 + }, + "mspt_worst_p99": 100.6, + "tps": 20.0, + "rss_mb": { + "mean": 781.84, + "max": 782.6 + }, + "cpu_percent": 136.59, + "chunks_per_second": 93.82, + "unique_per_second": 3.33, + "forgotten_per_second": 93.61, + "waited_ticks_per_second": 0.0, + "chunks_total": 26481, + "chunks_per_bot": { + "walk": 2648.1 + }, + "unique_chunks_per_bot": { + "walk": 679.5 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "walk", + "start_seconds": 14.65, + "mspt": { + "average": 18.02, + "p50": 15.22, + "p95": 37.62, + "p99": 54.5 + }, + "mspt_worst_p99": 73.8, + "tps": 20.0, + "rss_mb": { + "mean": 3408.62, + "max": 3423.6 + }, + "cpu_percent": 100.57, + "chunks_per_second": 101.12, + "unique_per_second": 4.25, + "forgotten_per_second": 101.17, + "waited_ticks_per_second": 0.0, + "chunks_total": 26763, + "chunks_per_bot": { + "walk": 2676.3 + }, + "unique_chunks_per_bot": { + "walk": 687.8 + }, + "resent": 0, + "pulled_back": 7, + "moved_too_quickly": 7, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "walk", + "start_seconds": 9.31, + "mspt": { + "average": 23.98, + "p50": 20.89, + "p95": 49.18, + "p99": 65.87 + }, + "mspt_worst_p99": 109.8, + "tps": 20.0, + "rss_mb": { + "mean": 786.18, + "max": 786.5 + }, + "cpu_percent": 159.81, + "chunks_per_second": 235.75, + "unique_per_second": 7.84, + "forgotten_per_second": 235.72, + "waited_ticks_per_second": 0.0, + "chunks_total": 67251, + "chunks_per_bot": { + "walk": 2690.04 + }, + "unique_chunks_per_bot": { + "walk": 683.92 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "walk", + "start_seconds": 14.95, + "mspt": { + "average": 19.19, + "p50": 17.34, + "p95": 33.84, + "p99": 56.21 + }, + "mspt_worst_p99": 79.2, + "tps": 20.0, + "rss_mb": { + "mean": 3531.46, + "max": 3582.1 + }, + "cpu_percent": 153.78, + "chunks_per_second": 252.35, + "unique_per_second": 11.73, + "forgotten_per_second": 251.58, + "waited_ticks_per_second": 0.0, + "chunks_total": 66759, + "chunks_per_bot": { + "walk": 2670.36 + }, + "unique_chunks_per_bot": { + "walk": 689.76 + }, + "resent": 0, + "pulled_back": 12, + "moved_too_quickly": 12, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "walk", + "start_seconds": 9.21, + "mspt": { + "average": 35.07, + "p50": 30.55, + "p95": 73.24, + "p99": 99.38 + }, + "mspt_worst_p99": 117.2, + "tps": 20.0, + "rss_mb": { + "mean": 816.58, + "max": 818.5 + }, + "cpu_percent": 187.42, + "chunks_per_second": 469.11, + "unique_per_second": 15.92, + "forgotten_per_second": 468.62, + "waited_ticks_per_second": 0.0, + "chunks_total": 136962, + "chunks_per_bot": { + "walk": 2739.24 + }, + "unique_chunks_per_bot": { + "walk": 685.68 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 50, + "behaviour": "walk", + "start_seconds": 15.95, + "mspt": { + "average": 35.23, + "p50": 31.39, + "p95": 60.87, + "p99": 104.2 + }, + "mspt_worst_p99": 443.7, + "tps": 20.0, + "rss_mb": { + "mean": 3562.57, + "max": 3598.9 + }, + "cpu_percent": 263.27, + "chunks_per_second": 497.51, + "unique_per_second": 30.67, + "forgotten_per_second": 495.7, + "waited_ticks_per_second": 0.0, + "chunks_total": 131116, + "chunks_per_bot": { + "walk": 2622.32 + }, + "unique_chunks_per_bot": { + "walk": 687.78 + }, + "resent": 0, + "pulled_back": 96, + "moved_too_quickly": 96, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 100, + "behaviour": "walk", + "start_seconds": 9.21, + "mspt": { + "average": 104.8, + "p50": 100.6, + "p95": 158.8, + "p99": 198.61 + }, + "mspt_worst_p99": 259.8, + "tps": 9.54, + "rss_mb": { + "mean": 850.05, + "max": 851.2 + }, + "cpu_percent": 198.95, + "chunks_per_second": 371.72, + "unique_per_second": 26.27, + "forgotten_per_second": 365.57, + "waited_ticks_per_second": 0.0, + "chunks_total": 144465, + "chunks_per_bot": { + "walk": 1444.65 + }, + "unique_chunks_per_bot": { + "walk": 571.76 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 100, + "bots_failed": [], + "tick_queries": 34 + }, + { + "server": "vanilla", + "bots": 100, + "behaviour": "walk", + "start_seconds": 15.25, + "error": "[Errno 32] Broken pipe" + }, + { + "server": "horizon", + "bots": 1, + "behaviour": "explore", + "start_seconds": 9.41, + "mspt": { + "average": 11.42, + "p50": 10.15, + "p95": 24.66, + "p99": 37.27 + }, + "mspt_worst_p99": 60.3, + "tps": 20.0, + "rss_mb": { + "mean": 838.61, + "max": 888.0 + }, + "cpu_percent": 326.85, + "chunks_per_second": 41.83, + "unique_per_second": 36.39, + "forgotten_per_second": 41.75, + "waited_ticks_per_second": 0.0, + "chunks_total": 9845, + "chunks_per_bot": { + "explore": 9845.0 + }, + "unique_chunks_per_bot": { + "explore": 8718.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 1, + "behaviour": "explore", + "start_seconds": 15.44, + "mspt": { + "average": 9.83, + "p50": 7.72, + "p95": 20.36, + "p99": 84.65 + }, + "mspt_worst_p99": 385.9, + "tps": 20.0, + "rss_mb": { + "mean": 3598.02, + "max": 3658.9 + }, + "cpu_percent": 244.86, + "chunks_per_second": 35.28, + "unique_per_second": 33.34, + "forgotten_per_second": 35.13, + "waited_ticks_per_second": 0.0, + "chunks_total": 7881, + "chunks_per_bot": { + "explore": 7881.0 + }, + "unique_chunks_per_bot": { + "explore": 7540.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 10, + "behaviour": "explore", + "start_seconds": 9.52, + "mspt": { + "average": 23.54, + "p50": 21.18, + "p95": 43.4, + "p99": 60.85 + }, + "mspt_worst_p99": 109.1, + "tps": 20.0, + "rss_mb": { + "mean": 1139.51, + "max": 1301.0 + }, + "cpu_percent": 331.74, + "chunks_per_second": 43.86, + "unique_per_second": 43.09, + "forgotten_per_second": 43.82, + "waited_ticks_per_second": 172.35, + "chunks_total": 14140, + "chunks_per_bot": { + "explore": 1414.0 + }, + "unique_chunks_per_bot": { + "explore": 1395.9 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "explore", + "start_seconds": 15.78, + "mspt": { + "average": 16.42, + "p50": 14.01, + "p95": 30.29, + "p99": 96.17 + }, + "mspt_worst_p99": 1144.3, + "tps": 20.0, + "rss_mb": { + "mean": 3964.49, + "max": 4248.3 + }, + "cpu_percent": 256.86, + "chunks_per_second": 25.43, + "unique_per_second": 25.33, + "forgotten_per_second": 25.96, + "waited_ticks_per_second": 76.05, + "chunks_total": 7991, + "chunks_per_bot": { + "explore": 799.1 + }, + "unique_chunks_per_bot": { + "explore": 797.7 + }, + "resent": 0, + "pulled_back": 11028, + "moved_too_quickly": 17, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "explore", + "start_seconds": 9.31, + "mspt": { + "average": 36.87, + "p50": 33.92, + "p95": 63.19, + "p99": 84.17 + }, + "mspt_worst_p99": 124.2, + "tps": 20.0, + "rss_mb": { + "mean": 1236.2, + "max": 1438.4 + }, + "cpu_percent": 345.03, + "chunks_per_second": 91.07, + "unique_per_second": 88.7, + "forgotten_per_second": 92.51, + "waited_ticks_per_second": 443.88, + "chunks_total": 30035, + "chunks_per_bot": { + "explore": 1201.4 + }, + "unique_chunks_per_bot": { + "explore": 1176.84 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "explore", + "start_seconds": 14.55, + "mspt": { + "average": 24.31, + "p50": 21.76, + "p95": 36.55, + "p99": 155.88 + }, + "mspt_worst_p99": 1535.8, + "tps": 20.0, + "rss_mb": { + "mean": 4047.76, + "max": 4459.2 + }, + "cpu_percent": 305.24, + "chunks_per_second": 37.94, + "unique_per_second": 37.82, + "forgotten_per_second": 38.87, + "waited_ticks_per_second": 261.79, + "chunks_total": 16604, + "chunks_per_bot": { + "explore": 664.16 + }, + "unique_chunks_per_bot": { + "explore": 660.92 + }, + "resent": 0, + "pulled_back": 33246, + "moved_too_quickly": 20, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "explore", + "start_seconds": 9.21, + "mspt": { + "average": 60.06, + "p50": 57.31, + "p95": 88.38, + "p99": 112.89 + }, + "mspt_worst_p99": 172.4, + "tps": 16.65, + "rss_mb": { + "mean": 1192.47, + "max": 1372.5 + }, + "cpu_percent": 362.42, + "chunks_per_second": 157.28, + "unique_per_second": 154.2, + "forgotten_per_second": 156.07, + "waited_ticks_per_second": 890.09, + "chunks_total": 54551, + "chunks_per_bot": { + "explore": 1091.02 + }, + "unique_chunks_per_bot": { + "explore": 1078.5 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 50, + "behaviour": "explore", + "start_seconds": 15.64, + "error": "[Errno 32] Broken pipe" + }, + { + "server": "horizon", + "bots": 100, + "behaviour": "explore", + "start_seconds": 9.21, + "mspt": { + "average": 124.25, + "p50": 120.1, + "p95": 168.33, + "p99": 204.64 + }, + "mspt_worst_p99": 262.2, + "tps": 8.05, + "rss_mb": { + "mean": 1283.26, + "max": 1418.6 + }, + "cpu_percent": 334.14, + "chunks_per_second": 185.47, + "unique_per_second": 184.54, + "forgotten_per_second": 179.79, + "waited_ticks_per_second": 1864.45, + "chunks_total": 78556, + "chunks_per_bot": { + "explore": 785.56 + }, + "unique_chunks_per_bot": { + "explore": 784.88 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 100, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 100, + "behaviour": "explore", + "start_seconds": 15.44, + "error": "[Errno 32] Broken pipe" + } + ] +} diff --git a/benchmarks/server-2026-10-17/explore-equal-rate.json b/benchmarks/server-2026-10-17/explore-equal-rate.json new file mode 100644 index 000000000..1356529e3 --- /dev/null +++ b/benchmarks/server-2026-10-17/explore-equal-rate.json @@ -0,0 +1,341 @@ +{ + "kind": "horizon-server-benchmark", + "date": "2026-10-17T18:12:19+00:00", + "machine": { + "system": "Linux", + "machine": "x86_64", + "cpus": 4 + }, + "commit": "7bbbf6e97fb53683eae0a01ce2c134e53cb649a7", + "java": [ + "openjdk 25.0.4.1 2026-08-18" + ], + "settings": { + "servers": "horizon,vanilla", + "bots": "1,10,25,50", + "behaviour": "explore", + "join_every": 1.0, + "warmup": 60, + "seconds": 180, + "seed": 12345, + "explore_speed": 0.8, + "gamerule": "", + "heap": "4g", + "out": "/home/ludovic/code/horizon/.claude/worktrees/server-host-4/.local/bench-out/explore-equal-rate.json", + "nice": 19, + "bots_via": "ssh macmini", + "bots_bin": "/Users/ludovicmarie/code/horizon/.claude/worktrees/server-host-4/target/release/horizon-bots", + "connect": "", + "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": 1, + "behaviour": "explore", + "start_seconds": 9.42, + "mspt": { + "average": 3.16, + "p50": 2.98, + "p95": 5.78, + "p99": 12.17 + }, + "mspt_worst_p99": 38.1, + "tps": 20.0, + "rss_mb": { + "mean": 759.26, + "max": 769.5 + }, + "cpu_percent": 21.74, + "chunks_per_second": 1.64, + "unique_per_second": 1.27, + "forgotten_per_second": 1.54, + "waited_ticks_per_second": 0.0, + "chunks_total": 841, + "chunks_per_bot": { + "explore": 841.0 + }, + "unique_chunks_per_bot": { + "explore": 781.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 1, + "behaviour": "explore", + "start_seconds": 17.17, + "mspt": { + "average": 16.82, + "p50": 16.67, + "p95": 27.3, + "p99": 52.16 + }, + "mspt_worst_p99": 453.4, + "tps": 20.0, + "rss_mb": { + "mean": 3373.16, + "max": 3405.9 + }, + "cpu_percent": 69.22, + "chunks_per_second": 1.66, + "unique_per_second": 1.35, + "forgotten_per_second": 1.54, + "waited_ticks_per_second": 0.0, + "chunks_total": 838, + "chunks_per_bot": { + "explore": 838.0 + }, + "unique_chunks_per_bot": { + "explore": 779.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 10, + "behaviour": "explore", + "start_seconds": 9.82, + "mspt": { + "average": 9.83, + "p50": 8.65, + "p95": 19.01, + "p99": 32.9 + }, + "mspt_worst_p99": 64.0, + "tps": 20.0, + "rss_mb": { + "mean": 861.49, + "max": 922.2 + }, + "cpu_percent": 77.43, + "chunks_per_second": 13.71, + "unique_per_second": 12.31, + "forgotten_per_second": 13.59, + "waited_ticks_per_second": 0.0, + "chunks_total": 7831, + "chunks_per_bot": { + "explore": 783.1 + }, + "unique_chunks_per_bot": { + "explore": 757.1 + }, + "resent": 0, + "pulled_back": 1051, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "explore", + "start_seconds": 15.06, + "mspt": { + "average": 24.38, + "p50": 21.55, + "p95": 39.58, + "p99": 66.64 + }, + "mspt_worst_p99": 107.1, + "tps": 20.0, + "rss_mb": { + "mean": 3569.41, + "max": 3685.1 + }, + "cpu_percent": 132.09, + "chunks_per_second": 13.54, + "unique_per_second": 12.32, + "forgotten_per_second": 13.62, + "waited_ticks_per_second": 0.0, + "chunks_total": 7753, + "chunks_per_bot": { + "explore": 775.3 + }, + "unique_chunks_per_bot": { + "explore": 749.2 + }, + "resent": 0, + "pulled_back": 1027, + "moved_too_quickly": 6, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "explore", + "start_seconds": 9.31, + "mspt": { + "average": 16.87, + "p50": 15.4, + "p95": 30.88, + "p99": 46.73 + }, + "mspt_worst_p99": 84.9, + "tps": 20.0, + "rss_mb": { + "mean": 887.11, + "max": 945.5 + }, + "cpu_percent": 111.2, + "chunks_per_second": 33.51, + "unique_per_second": 29.92, + "forgotten_per_second": 33.33, + "waited_ticks_per_second": 0.0, + "chunks_total": 19718, + "chunks_per_bot": { + "explore": 788.72 + }, + "unique_chunks_per_bot": { + "explore": 759.08 + }, + "resent": 0, + "pulled_back": 3770, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "explore", + "start_seconds": 14.94, + "mspt": { + "average": 30.29, + "p50": 28.25, + "p95": 43.47, + "p99": 68.56 + }, + "mspt_worst_p99": 165.2, + "tps": 20.0, + "rss_mb": { + "mean": 3608.97, + "max": 3732.0 + }, + "cpu_percent": 176.28, + "chunks_per_second": 32.98, + "unique_per_second": 29.29, + "forgotten_per_second": 32.84, + "waited_ticks_per_second": 0.0, + "chunks_total": 19442, + "chunks_per_bot": { + "explore": 777.68 + }, + "unique_chunks_per_bot": { + "explore": 748.24 + }, + "resent": 0, + "pulled_back": 5139, + "moved_too_quickly": 11, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "explore", + "start_seconds": 9.41, + "mspt": { + "average": 23.57, + "p50": 20.43, + "p95": 47.53, + "p99": 67.23 + }, + "mspt_worst_p99": 106.2, + "tps": 20.0, + "rss_mb": { + "mean": 924.41, + "max": 984.3 + }, + "cpu_percent": 127.14, + "chunks_per_second": 66.56, + "unique_per_second": 59.42, + "forgotten_per_second": 66.43, + "waited_ticks_per_second": 0.0, + "chunks_total": 39950, + "chunks_per_bot": { + "explore": 799.0 + }, + "unique_chunks_per_bot": { + "explore": 767.56 + }, + "resent": 0, + "pulled_back": 7434, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 50, + "behaviour": "explore", + "start_seconds": 15.04, + "mspt": { + "average": 42.44, + "p50": 40.71, + "p95": 54.84, + "p99": 94.23 + }, + "mspt_worst_p99": 212.3, + "tps": 20.0, + "rss_mb": { + "mean": 3667.85, + "max": 3780.8 + }, + "cpu_percent": 200.85, + "chunks_per_second": 65.17, + "unique_per_second": 58.11, + "forgotten_per_second": 65.22, + "waited_ticks_per_second": 0.0, + "chunks_total": 39772, + "chunks_per_bot": { + "explore": 795.44 + }, + "unique_chunks_per_bot": { + "explore": 764.02 + }, + "resent": 0, + "pulled_back": 10604, + "moved_too_quickly": 38, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 35 + } + ] +} diff --git a/benchmarks/server-2026-10-17/load.txt b/benchmarks/server-2026-10-17/load.txt new file mode 100644 index 000000000..33d8b191f --- /dev/null +++ b/benchmarks/server-2026-10-17/load.txt @@ -0,0 +1,279 @@ +2026-10-17T15:51:54Z thinkpad: 1.30 2.47 3.85 | macmini (bots): { 67.44 76.34 77.44 } +2026-10-17T15:52:25Z thinkpad: 2.07 2.54 3.83 | macmini (bots): { 64.78 74.76 76.82 } +2026-10-17T15:52:55Z thinkpad: 2.08 2.51 3.78 | macmini (bots): { 62.50 73.29 76.22 } +2026-10-17T15:53:25Z thinkpad: 1.46 2.31 3.67 | macmini (bots): { 63.70 72.67 75.90 } +2026-10-17T15:53:55Z thinkpad: 1.12 2.15 3.58 | macmini (bots): { 69.06 73.27 76.01 } +2026-10-17T15:54:26Z thinkpad: 1.34 2.09 3.51 | macmini (bots): { 72.55 73.63 76.04 } +2026-10-17T15:54:56Z thinkpad: 1.11 1.97 3.42 | macmini (bots): { 77.53 74.77 76.37 } +2026-10-17T15:55:26Z thinkpad: 1.02 1.87 3.34 | macmini (bots): { 77.61 74.90 76.36 } +2026-10-17T15:55:56Z thinkpad: 1.09 1.81 3.27 | macmini (bots): { 71.56 73.69 75.86 } +2026-10-17T15:56:26Z thinkpad: 2.23 1.99 3.29 | macmini (bots): { 65.21 71.93 75.15 } +2026-10-17T15:56:57Z thinkpad: 2.60 2.12 3.29 | macmini (bots): { 58.16 69.40 74.10 } +2026-10-17T15:57:27Z thinkpad: 2.57 2.18 3.27 | macmini (bots): { 57.54 68.26 73.52 } +2026-10-17T15:57:57Z thinkpad: 2.37 2.17 3.23 | macmini (bots): { 56.15 66.85 72.83 } +2026-10-17T15:58:27Z thinkpad: 1.90 2.08 3.17 | macmini (bots): { 59.28 66.57 72.52 } +2026-10-17T15:58:58Z thinkpad: 1.71 2.02 3.11 | macmini (bots): { 55.64 64.94 71.72 } +2026-10-17T15:59:28Z thinkpad: 1.66 1.97 3.06 | macmini (bots): { 66.06 66.49 72.04 } +2026-10-17T15:59:58Z thinkpad: 1.18 1.83 2.98 | macmini (bots): { 78.15 69.43 72.92 } +2026-10-17T16:00:28Z thinkpad: 0.83 1.68 2.89 | macmini (bots): { 70.77 68.57 72.49 } +2026-10-17T16:00:58Z thinkpad: 1.27 1.70 2.86 | macmini (bots): { 71.95 68.97 72.49 } +2026-10-17T16:01:29Z thinkpad: 1.85 1.79 2.85 | macmini (bots): { 83.43 72.22 73.56 } +2026-10-17T16:01:59Z thinkpad: 2.51 1.95 2.86 | macmini (bots): { 70.49 70.18 72.77 } +2026-10-17T16:02:29Z thinkpad: 1.98 1.87 2.81 | macmini (bots): { 64.00 68.64 72.12 } +2026-10-17T16:02:59Z thinkpad: 1.77 1.84 2.77 | macmini (bots): { 59.65 67.09 71.44 } +2026-10-17T16:03:30Z thinkpad: 4.04 2.35 2.91 | macmini (bots): { 62.67 67.15 71.31 } +2026-10-17T16:04:00Z thinkpad: 4.94 2.72 3.01 | macmini (bots): { 67.63 67.82 71.40 } +2026-10-17T16:04:30Z thinkpad: 4.22 2.78 3.02 | macmini (bots): { 70.41 68.39 71.48 } +2026-10-17T16:05:00Z thinkpad: 3.81 2.80 3.02 | macmini (bots): { 66.59 67.83 71.18 } +2026-10-17T16:05:31Z thinkpad: 3.20 2.75 3.00 | macmini (bots): { 72.53 69.01 71.48 } +2026-10-17T16:06:01Z thinkpad: 4.22 3.04 3.08 | macmini (bots): { 71.30 69.27 71.50 } +2026-10-17T16:06:31Z thinkpad: 5.43 3.47 3.23 | macmini (bots): { 68.37 68.63 71.19 } +2026-10-17T16:07:01Z thinkpad: 4.35 3.42 3.22 | macmini (bots): { 60.20 66.68 70.39 } +2026-10-17T16:07:31Z thinkpad: 3.47 3.31 3.19 | macmini (bots): { 58.03 65.51 69.84 } +2026-10-17T16:08:02Z thinkpad: 2.30 3.04 3.11 | macmini (bots): { 59.05 65.13 69.55 } +2026-10-17T16:08:32Z thinkpad: 1.88 2.86 3.04 | macmini (bots): { 51.77 62.75 68.53 } +2026-10-17T16:09:02Z thinkpad: 1.38 2.65 2.97 | macmini (bots): { 49.05 61.01 67.70 } +2026-10-17T16:09:32Z thinkpad: 3.10 2.95 3.06 | macmini (bots): { 48.91 59.82 67.03 } +2026-10-17T16:10:02Z thinkpad: 2.91 2.91 3.04 | macmini (bots): { 47.56 58.44 66.25 } +2026-10-17T16:10:33Z thinkpad: 2.58 2.83 3.01 | macmini (bots): { 49.23 57.69 65.70 } +2026-10-17T16:11:03Z thinkpad: 8.38 4.15 3.44 | macmini (bots): { 44.16 55.72 64.71 } +2026-10-17T16:11:33Z thinkpad: 9.94 4.92 3.72 | macmini (bots): { 44.50 54.71 64.03 } +2026-10-17T16:12:03Z thinkpad: 10.08 5.42 3.92 | macmini (bots): { 42.15 53.21 63.17 } +2026-10-17T16:12:33Z thinkpad: 6.79 5.06 3.85 | macmini (bots): { 36.13 50.73 61.92 } +2026-10-17T16:13:04Z thinkpad: 4.61 4.72 3.77 | macmini (bots): { 41.28 50.64 61.51 } +2026-10-17T16:13:34Z thinkpad: 3.81 4.50 3.73 | macmini (bots): { 42.92 50.15 60.95 } +2026-10-17T16:14:04Z thinkpad: 3.39 4.35 3.71 | macmini (bots): { 41.56 49.16 60.22 } +2026-10-17T16:14:35Z thinkpad: 7.53 5.17 4.00 | macmini (bots): { 47.48 49.82 60.07 } +2026-10-17T16:15:05Z thinkpad: 9.73 6.01 4.32 | macmini (bots): { 51.43 50.41 59.92 } +2026-10-17T16:15:35Z thinkpad: 8.07 5.94 4.35 | macmini (bots): { 69.31 54.81 61.18 } +2026-10-17T16:16:05Z thinkpad: 9.27 6.45 4.57 | macmini (bots): { 71.85 56.92 61.73 } +2026-10-17T16:16:35Z thinkpad: 8.69 6.59 4.68 | macmini (bots): { 64.77 56.72 61.50 } +2026-10-17T16:17:06Z thinkpad: 6.11 6.17 4.60 | macmini (bots): { 57.92 55.86 61.02 } +2026-10-17T16:17:36Z thinkpad: 6.79 6.33 4.70 | macmini (bots): { 51.44 54.51 60.36 } +2026-10-17T16:18:06Z thinkpad: 5.16 5.99 4.64 | macmini (bots): { 51.33 54.23 60.05 } +2026-10-17T16:18:36Z thinkpad: 5.30 5.93 4.66 | macmini (bots): { 55.22 54.87 60.08 } +2026-10-17T16:19:07Z thinkpad: 4.78 5.72 4.63 | macmini (bots): { 51.62 53.94 59.56 } +2026-10-17T16:19:37Z thinkpad: 5.25 5.78 4.69 | macmini (bots): { 48.77 53.04 59.04 } +2026-10-17T16:20:07Z thinkpad: 4.63 5.60 4.67 | macmini (bots): { 54.59 53.92 59.14 } +2026-10-17T16:20:37Z thinkpad: 5.46 5.67 4.72 | macmini (bots): { 59.07 55.03 59.36 } +2026-10-17T16:21:07Z thinkpad: 8.07 6.19 4.91 | macmini (bots): { 52.53 53.83 58.77 } +2026-10-17T16:21:38Z thinkpad: 15.01 8.05 5.57 | macmini (bots): { 48.85 52.78 58.19 } +2026-10-17T16:22:08Z thinkpad: 19.08 9.69 6.20 | macmini (bots): { 54.73 53.61 58.29 } +2026-10-17T16:22:39Z thinkpad: 27.72 12.63 7.29 | macmini (bots): { 54.23 53.61 58.13 } +2026-10-17T16:23:09Z thinkpad: 23.10 12.88 7.53 | macmini (bots): { 51.63 53.06 57.77 } +2026-10-17T16:23:39Z thinkpad: 20.51 13.53 7.96 | macmini (bots): { 45.29 51.38 57.00 } +2026-10-17T16:24:10Z thinkpad: 12.70 12.30 7.72 | macmini (bots): { 45.61 50.91 56.63 } +2026-10-17T16:24:40Z thinkpad: 8.67 11.35 7.55 | macmini (bots): { 45.18 50.35 56.23 } +2026-10-17T16:25:10Z thinkpad: 6.35 10.53 7.40 | macmini (bots): { 41.10 48.88 55.50 } +2026-10-17T16:25:40Z thinkpad: 6.26 10.16 7.38 | macmini (bots): { 49.86 50.24 55.76 } +2026-10-17T16:26:11Z thinkpad: 7.71 10.11 7.45 | macmini (bots): { 53.93 51.11 55.88 } +2026-10-17T16:26:41Z thinkpad: 9.50 10.32 7.61 | macmini (bots): { 47.61 49.86 55.26 } +2026-10-17T16:27:11Z thinkpad: 10.26 10.39 7.72 | macmini (bots): { 46.43 49.36 54.89 } +2026-10-17T16:27:42Z thinkpad: 10.61 10.48 7.83 | macmini (bots): { 48.88 49.66 54.80 } +2026-10-17T16:28:12Z thinkpad: 10.39 10.45 7.91 | macmini (bots): { 58.42 51.74 55.37 } +2026-10-17T16:28:42Z thinkpad: 10.36 10.44 7.99 | macmini (bots): { 59.59 52.67 55.58 } +2026-10-17T16:29:13Z thinkpad: 9.63 10.25 8.00 | macmini (bots): { 55.30 52.38 55.38 } +2026-10-17T16:29:43Z thinkpad: 10.33 10.37 8.12 | macmini (bots): { 57.79 53.13 55.52 } +2026-10-17T16:30:13Z thinkpad: 9.68 10.18 8.12 | macmini (bots): { 57.83 53.60 55.61 } +2026-10-17T16:30:43Z thinkpad: 9.72 10.17 8.19 | macmini (bots): { 78.94 59.18 57.56 } +2026-10-17T16:31:14Z thinkpad: 6.60 9.36 7.98 | macmini (bots): { 87.29 62.98 58.99 } +2026-10-17T16:31:44Z thinkpad: 6.77 9.08 7.93 | macmini (bots): { 81.82 64.16 59.57 } +2026-10-17T16:32:14Z thinkpad: 13.15 10.41 8.41 | macmini (bots): { 81.20 65.92 60.37 } +2026-10-17T16:32:45Z thinkpad: 27.80 14.12 9.71 | macmini (bots): { 75.41 66.22 60.69 } +2026-10-17T16:33:15Z thinkpad: 44.59 19.25 11.54 | macmini (bots): { 66.92 65.08 60.47 } +2026-10-17T16:33:46Z thinkpad: 44.87 21.89 12.67 | macmini (bots): { 61.47 63.94 60.22 } +2026-10-17T16:34:16Z thinkpad: 38.98 22.67 13.23 | macmini (bots): { 62.13 63.78 60.28 } +2026-10-17T16:34:47Z thinkpad: 47.64 26.08 14.65 | macmini (bots): { 81.23 68.06 61.94 } +2026-10-17T16:35:17Z thinkpad: 47.83 28.02 15.65 | macmini (bots): { 116.14 78.43 65.98 } +2026-10-17T16:35:48Z thinkpad: 40.43 28.18 16.10 | macmini (bots): { 125.61 84.41 68.58 } +2026-10-17T16:36:19Z thinkpad: 41.34 29.71 17.00 | macmini (bots): { 143.61 92.86 72.20 } +2026-10-17T16:36:49Z thinkpad: 34.40 29.19 17.29 | macmini (bots): { 156.05 100.88 75.83 } +2026-10-17T16:37:20Z thinkpad: 25.34 27.65 17.17 | macmini (bots): { 151.90 105.26 78.29 } +2026-10-17T16:37:50Z thinkpad: 18.36 25.71 16.85 | macmini (bots): { 141.10 107.25 79.96 } +2026-10-17T16:38:20Z thinkpad: 16.19 24.47 16.72 | macmini (bots): { 131.21 107.92 81.13 } +2026-10-17T16:38:51Z thinkpad: 13.36 23.00 16.48 | macmini (bots): { 122.08 107.78 82.00 } +2026-10-17T16:39:21Z thinkpad: 11.27 21.57 16.21 | macmini (bots): { 107.07 105.50 82.07 } +2026-10-17T16:39:51Z thinkpad: 11.19 20.56 16.04 | macmini (bots): { 96.38 103.12 82.02 } +2026-10-17T16:40:22Z thinkpad: 10.06 19.37 15.79 | macmini (bots): { 89.06 100.79 81.91 } +2026-10-17T16:40:52Z thinkpad: 7.38 17.86 15.40 | macmini (bots): { 79.35 97.41 81.34 } +2026-10-17T16:41:23Z thinkpad: 6.18 16.57 15.05 | macmini (bots): { 81.70 96.20 81.46 } +2026-10-17T16:41:53Z thinkpad: 5.56 15.44 14.72 | macmini (bots): { 86.51 95.88 81.84 } +2026-10-17T16:42:23Z thinkpad: 4.90 14.33 14.37 | macmini (bots): { 99.20 97.69 83.05 } +2026-10-17T16:42:53Z thinkpad: 5.01 13.47 14.08 | macmini (bots): { 116.29 101.87 85.07 } +2026-10-17T16:43:24Z thinkpad: 4.34 12.50 13.74 | macmini (bots): { 131.32 106.77 87.42 } +2026-10-17T16:43:54Z thinkpad: 4.08 11.65 13.42 | macmini (bots): { 155.44 114.77 90.98 } +2026-10-17T16:44:24Z thinkpad: 4.00 10.90 13.11 | macmini (bots): { 167.86 121.62 94.28 } +2026-10-17T16:44:54Z thinkpad: 3.98 10.13 12.77 | macmini (bots): { 160.36 124.31 96.20 } +2026-10-17T16:45:25Z thinkpad: 4.20 9.59 12.50 | macmini (bots): { 163.23 128.59 98.74 } +2026-10-17T16:45:55Z thinkpad: 4.54 9.15 12.27 | macmini (bots): { 169.93 133.73 101.65 } +2026-10-17T16:46:25Z thinkpad: 4.44 8.69 12.01 | macmini (bots): { 162.40 135.22 103.29 } +2026-10-17T16:46:55Z thinkpad: 4.06 8.20 11.74 | macmini (bots): { 164.16 138.31 105.52 } +2026-10-17T16:47:26Z thinkpad: 3.96 7.78 11.49 | macmini (bots): { 157.92 139.31 107.02 } +2026-10-17T16:47:56Z thinkpad: 3.71 7.35 11.23 | macmini (bots): { 155.72 140.55 108.59 } +2026-10-17T16:48:26Z thinkpad: 4.93 7.32 11.09 | macmini (bots): { 153.39 141.52 110.05 } +2026-10-17T16:48:56Z thinkpad: 4.61 7.03 10.88 | macmini (bots): { 137.54 138.87 110.18 } +2026-10-17T16:49:27Z thinkpad: 4.01 6.65 10.63 | macmini (bots): { 136.53 138.36 110.98 } +2026-10-17T16:49:57Z thinkpad: 3.19 6.19 10.35 | macmini (bots): { 141.12 139.18 112.22 } +2026-10-17T16:50:27Z thinkpad: 5.31 6.43 10.30 | macmini (bots): { 122.76 135.27 111.75 } +2026-10-17T16:50:57Z thinkpad: 5.95 6.48 10.19 | macmini (bots): { 114.26 132.02 111.39 } +2026-10-17T16:51:28Z thinkpad: 5.76 6.41 10.05 | macmini (bots): { 116.04 130.87 111.69 } +2026-10-17T16:51:58Z thinkpad: 5.11 6.18 9.86 | macmini (bots): { 115.06 129.17 111.74 } +2026-10-17T16:52:28Z thinkpad: 5.09 6.07 9.70 | macmini (bots): { 109.91 126.59 111.41 } +2026-10-17T16:52:58Z thinkpad: 4.74 5.90 9.53 | macmini (bots): { 101.90 123.11 110.67 } +2026-10-17T16:53:29Z thinkpad: 4.89 5.84 9.39 | macmini (bots): { 83.29 116.38 108.74 } +2026-10-17T16:53:59Z thinkpad: 5.10 5.82 9.27 | macmini (bots): { 119.16 122.10 111.09 } +2026-10-17T16:54:29Z thinkpad: 4.57 5.67 9.10 | macmini (bots): { 114.87 120.76 110.98 } +2026-10-17T16:55:00Z thinkpad: 4.74 5.58 8.96 | macmini (bots): { 129.84 123.74 112.39 } +2026-10-17T16:55:30Z thinkpad: 6.40 5.90 8.96 | macmini (bots): { 100.10 117.29 110.46 } +2026-10-17T16:56:00Z thinkpad: 6.96 6.08 8.92 | macmini (bots): { 80.18 110.75 108.32 } +2026-10-17T16:56:30Z thinkpad: 6.07 5.95 8.79 | macmini (bots): { 74.41 106.39 106.82 } +2026-10-17T16:57:01Z thinkpad: 6.63 6.13 8.75 | macmini (bots): { 81.71 104.99 106.29 } +2026-10-17T16:57:31Z thinkpad: 6.39 6.14 8.68 | macmini (bots): { 101.10 107.55 107.18 } +2026-10-17T16:58:01Z thinkpad: 6.32 6.16 8.60 | macmini (bots): { 94.45 105.66 106.52 } +2026-10-17T16:58:31Z thinkpad: 5.19 5.90 8.44 | macmini (bots): { 78.76 100.83 104.75 } +2026-10-17T16:59:02Z thinkpad: 5.24 5.83 8.33 | macmini (bots): { 78.19 98.42 103.73 } +2026-10-17T16:59:32Z thinkpad: 3.95 5.48 8.13 | macmini (bots): { 77.57 96.42 102.82 } +2026-10-17T17:00:02Z thinkpad: 3.12 5.13 7.93 | macmini (bots): { 85.23 96.29 102.54 } +2026-10-17T17:00:32Z thinkpad: 3.17 4.93 7.77 | macmini (bots): { 99.84 99.07 103.35 } +2026-10-17T17:01:02Z thinkpad: 6.09 5.50 7.88 | macmini (bots): { 80.56 94.49 101.54 } +2026-10-17T17:01:33Z thinkpad: 9.23 6.29 8.06 | macmini (bots): { 79.70 93.03 100.77 } +2026-10-17T17:02:03Z thinkpad: 9.09 6.54 8.09 | macmini (bots): { 76.63 90.94 99.74 } +2026-10-17T17:02:33Z thinkpad: 7.86 6.52 8.04 | macmini (bots): { 93.08 93.50 100.36 } +2026-10-17T17:03:03Z thinkpad: 7.81 6.67 8.04 | macmini (bots): { 83.14 91.09 99.25 } +2026-10-17T17:03:34Z thinkpad: 6.69 6.51 7.94 | macmini (bots): { 79.37 89.64 98.45 } +2026-10-17T17:04:04Z thinkpad: 5.63 6.27 7.81 | macmini (bots): { 73.68 87.23 97.27 } +2026-10-17T17:04:34Z thinkpad: 5.75 6.21 7.75 | macmini (bots): { 75.89 86.23 96.54 } +2026-10-17T17:05:04Z thinkpad: 5.05 6.01 7.63 | macmini (bots): { 79.29 86.13 96.10 } +2026-10-17T17:05:35Z thinkpad: 3.88 5.62 7.44 | macmini (bots): { 76.07 84.79 95.28 } +2026-10-17T17:06:05Z thinkpad: 6.47 6.07 7.53 | macmini (bots): { 70.59 82.62 94.13 } +2026-10-17T17:06:35Z thinkpad: 8.20 6.53 7.63 | macmini (bots): { 69.09 81.18 93.21 } +2026-10-17T17:07:05Z thinkpad: 7.59 6.54 7.60 | macmini (bots): { 77.55 82.11 93.13 } +2026-10-17T17:07:36Z thinkpad: 6.93 6.49 7.55 | macmini (bots): { 84.23 83.41 93.22 } +2026-10-17T17:08:06Z thinkpad: 7.21 6.61 7.56 | macmini (bots): { 84.31 83.68 92.99 } +2026-10-17T17:08:36Z thinkpad: 6.73 6.57 7.51 | macmini (bots): { 81.04 82.99 92.42 } +2026-10-17T17:09:06Z thinkpad: 6.12 6.43 7.44 | macmini (bots): { 86.55 83.77 92.35 } +2026-10-17T17:09:36Z thinkpad: 6.77 6.58 7.46 | macmini (bots): { 95.19 86.31 92.99 } +2026-10-17T17:10:07Z thinkpad: 7.13 6.68 7.46 | macmini (bots): { 111.30 91.11 94.50 } +2026-10-17T17:10:37Z thinkpad: 5.09 6.23 7.29 | macmini (bots): { 118.51 95.17 95.87 } +2026-10-17T17:11:07Z thinkpad: 4.00 5.86 7.13 | macmini (bots): { 121.12 98.01 96.88 } +2026-10-17T17:11:38Z thinkpad: 3.50 5.57 6.99 | macmini (bots): { 109.57 97.67 96.81 } +2026-10-17T17:12:08Z thinkpad: 2.92 5.22 6.83 | macmini (bots): { 108.43 98.44 97.11 } +2026-10-17T17:12:38Z thinkpad: 3.42 5.12 6.74 | macmini (bots): { 101.61 97.86 96.95 } +2026-10-17T17:13:09Z thinkpad: 5.64 5.51 6.82 | macmini (bots): { 101.66 98.17 97.09 } +2026-10-17T17:13:39Z thinkpad: 7.06 5.89 6.91 | macmini (bots): { 88.43 95.36 96.11 } +2026-10-17T17:14:09Z thinkpad: 7.61 6.13 6.96 | macmini (bots): { 79.03 92.54 95.07 } +2026-10-17T17:14:39Z thinkpad: 9.06 6.68 7.12 | macmini (bots): { 92.55 94.57 95.72 } +2026-10-17T17:15:10Z thinkpad: 9.81 7.06 7.22 | macmini (bots): { 93.71 94.49 95.64 } +2026-10-17T17:15:40Z thinkpad: 9.98 7.41 7.34 | macmini (bots): { 92.26 93.86 95.35 } +2026-10-17T17:16:10Z thinkpad: 7.60 7.08 7.23 | macmini (bots): { 88.61 92.91 94.96 } +2026-10-17T17:16:40Z thinkpad: 6.81 6.93 7.17 | macmini (bots): { 96.96 94.47 95.45 } +2026-10-17T17:17:11Z thinkpad: 6.31 6.81 7.12 | macmini (bots): { 102.92 96.14 96.02 } +2026-10-17T17:17:41Z thinkpad: 4.81 6.38 6.97 | macmini (bots): { 91.20 94.16 95.32 } +2026-10-17T17:18:11Z thinkpad: 7.36 6.81 7.09 | macmini (bots): { 93.60 94.31 95.32 } +2026-10-17T17:18:41Z thinkpad: 9.42 7.38 7.27 | macmini (bots): { 90.86 93.57 95.02 } +2026-10-17T17:19:11Z thinkpad: 9.95 7.72 7.39 | macmini (bots): { 88.94 92.94 94.75 } +2026-10-17T17:19:42Z thinkpad: 9.51 7.85 7.45 | macmini (bots): { 94.97 93.93 95.03 } +2026-10-17T17:20:12Z thinkpad: 8.97 7.90 7.48 | macmini (bots): { 80.46 90.39 93.71 } +2026-10-17T17:20:42Z thinkpad: 7.56 7.65 7.41 | macmini (bots): { 74.38 87.95 92.71 } +2026-10-17T17:21:12Z thinkpad: 6.39 7.36 7.32 | macmini (bots): { 77.48 87.26 92.28 } +2026-10-17T17:21:43Z thinkpad: 5.21 6.98 7.19 | macmini (bots): { 82.05 87.48 92.19 } +2026-10-17T17:22:13Z thinkpad: 3.87 6.49 7.02 | macmini (bots): { 88.15 88.45 92.38 } +2026-10-17T17:22:43Z thinkpad: 3.86 6.22 6.91 | macmini (bots): { 100.23 91.12 93.19 } +2026-10-17T17:23:13Z thinkpad: 8.32 7.05 7.16 | macmini (bots): { 84.55 88.39 92.15 } +2026-10-17T17:23:43Z thinkpad: 13.84 8.37 7.59 | macmini (bots): { 73.74 85.38 90.92 } +2026-10-17T17:24:14Z thinkpad: 21.02 10.71 8.40 | macmini (bots): { 71.36 83.72 90.13 } +2026-10-17T17:24:44Z thinkpad: 20.73 11.82 8.85 | macmini (bots): { 82.27 85.04 90.38 } +2026-10-17T17:25:14Z thinkpad: 16.80 11.77 8.94 | macmini (bots): { 78.63 83.92 89.78 } +2026-10-17T17:25:44Z thinkpad: 14.62 11.68 8.99 | macmini (bots): { 82.33 84.06 89.61 } +2026-10-17T17:26:15Z thinkpad: 14.51 11.94 9.17 | macmini (bots): { 74.13 81.89 88.63 } +2026-10-17T17:26:45Z thinkpad: 12.48 11.74 9.20 | macmini (bots): { 75.65 81.46 88.24 } +2026-10-17T17:27:15Z thinkpad: 15.24 12.56 9.56 | macmini (bots): { 71.35 79.78 87.35 } +2026-10-17T17:27:45Z thinkpad: 15.70 12.87 9.76 | macmini (bots): { 68.21 78.17 86.51 } +2026-10-17T17:28:16Z thinkpad: 12.80 12.47 9.72 | macmini (bots): { 79.74 80.10 86.92 } +2026-10-17T17:28:46Z thinkpad: 9.05 11.58 9.51 | macmini (bots): { 101.01 85.27 88.56 } +2026-10-17T17:29:16Z thinkpad: 9.38 11.41 9.53 | macmini (bots): { 93.76 85.18 88.43 } +2026-10-17T17:29:46Z thinkpad: 11.08 11.62 9.65 | macmini (bots): { 74.91 81.56 87.01 } +2026-10-17T17:30:17Z thinkpad: 10.98 11.53 9.69 | macmini (bots): { 61.10 77.50 85.34 } +2026-10-17T17:30:47Z thinkpad: 9.83 11.21 9.64 | macmini (bots): { 51.19 73.56 83.64 } +2026-10-17T17:31:18Z thinkpad: 9.65 11.03 9.63 | macmini (bots): { 49.64 71.03 82.37 } +2026-10-17T17:31:48Z thinkpad: 7.22 10.33 9.44 | macmini (bots): { 53.62 69.90 81.56 } +2026-10-17T17:32:18Z thinkpad: 5.79 9.68 9.25 | macmini (bots): { 61.07 70.36 81.34 } +2026-10-17T17:32:48Z thinkpad: 3.70 8.80 8.97 | macmini (bots): { 60.39 69.24 80.55 } +2026-10-17T17:33:19Z thinkpad: 2.72 8.08 8.73 | macmini (bots): { 57.06 67.51 79.53 } +2026-10-17T17:33:49Z thinkpad: 1.97 7.37 8.47 | macmini (bots): { 65.72 68.41 79.42 } +2026-10-17T17:34:19Z thinkpad: 1.25 6.68 8.20 | macmini (bots): { 62.60 67.40 78.67 } +2026-10-17T17:34:49Z thinkpad: 0.94 6.09 7.96 | macmini (bots): { 71.83 69.24 78.95 } +2026-10-17T17:35:20Z thinkpad: 0.72 5.54 7.71 | macmini (bots): { 66.16 68.07 78.19 } +2026-10-17T17:35:50Z thinkpad: 0.44 5.01 7.47 | macmini (bots): { 70.96 68.91 78.13 } +2026-10-17T17:36:20Z thinkpad: 1.70 4.79 7.30 | macmini (bots): { 79.74 71.45 78.74 } +2026-10-17T17:36:50Z thinkpad: 3.03 4.80 7.22 | macmini (bots): { 79.71 72.75 78.95 } +2026-10-17T17:37:21Z thinkpad: 3.07 4.67 7.10 | macmini (bots): { 85.13 74.60 79.40 } +2026-10-17T17:37:51Z thinkpad: 2.32 4.33 6.91 | macmini (bots): { 84.85 75.70 79.64 } +2026-10-17T17:38:21Z thinkpad: 1.83 4.03 6.73 | macmini (bots): { 86.47 76.79 79.88 } +2026-10-17T17:38:51Z thinkpad: 1.11 3.64 6.51 | macmini (bots): { 106.96 82.74 81.94 } +2026-10-17T17:39:22Z thinkpad: 1.03 3.37 6.33 | macmini (bots): { 114.52 87.01 83.52 } +2026-10-17T17:39:52Z thinkpad: 1.11 3.16 6.16 | macmini (bots): { 103.99 87.05 83.65 } +2026-10-17T17:40:22Z thinkpad: 1.31 3.02 6.02 | macmini (bots): { 100.27 87.59 83.95 } +2026-10-17T17:40:52Z thinkpad: 1.36 2.87 5.87 | macmini (bots): { 98.17 88.44 84.40 } +2026-10-17T17:41:22Z thinkpad: 2.01 2.88 5.78 | macmini (bots): { 96.27 88.67 84.60 } +2026-10-17T17:41:53Z thinkpad: 1.96 2.77 5.65 | macmini (bots): { 96.31 89.44 85.03 } +2026-10-17T17:42:23Z thinkpad: 3.06 2.97 5.62 | macmini (bots): { 80.49 86.30 84.04 } +2026-10-17T17:42:53Z thinkpad: 2.38 2.81 5.49 | macmini (bots): { 66.40 82.24 82.64 } +2026-10-17T17:43:24Z thinkpad: 2.44 2.81 5.40 | macmini (bots): { 56.70 78.37 81.22 } +2026-10-17T17:43:54Z thinkpad: 1.93 2.65 5.26 | macmini (bots): { 61.56 77.39 80.77 } +2026-10-17T17:44:24Z thinkpad: 1.96 2.59 5.16 | macmini (bots): { 58.25 75.14 79.83 } +2026-10-17T17:44:54Z thinkpad: 2.25 2.60 5.08 | macmini (bots): { 56.32 73.21 78.98 } +2026-10-17T17:45:24Z thinkpad: 1.79 2.46 4.95 | macmini (bots): { 62.87 73.14 78.75 } +2026-10-17T17:45:55Z thinkpad: 2.78 2.64 4.93 | macmini (bots): { 62.58 72.22 78.23 } +2026-10-17T17:46:25Z thinkpad: 4.82 3.14 5.03 | macmini (bots): { 58.89 70.44 77.38 } +2026-10-17T17:46:55Z thinkpad: 3.10 2.90 4.88 | macmini (bots): { 61.68 70.08 77.01 } +2026-10-17T17:47:25Z thinkpad: 2.27 2.71 4.75 | macmini (bots): { 70.43 71.40 77.25 } +2026-10-17T17:47:56Z thinkpad: 1.71 2.53 4.62 | macmini (bots): { 70.51 71.56 77.12 } +2026-10-17T17:48:26Z thinkpad: 1.59 2.42 4.52 | macmini (bots): { 68.98 70.98 76.71 } +2026-10-17T17:48:56Z thinkpad: 2.09 2.47 4.47 | macmini (bots): { 67.75 70.60 76.38 } +2026-10-17T17:49:26Z thinkpad: 2.14 2.44 4.39 | macmini (bots): { 61.53 69.01 75.58 } +2026-10-17T17:49:56Z thinkpad: 2.22 2.43 4.33 | macmini (bots): { 58.49 67.45 74.78 } +2026-10-17T17:50:27Z thinkpad: 1.98 2.36 4.24 | macmini (bots): { 66.15 68.33 74.84 } +2026-10-17T17:50:57Z thinkpad: 2.38 2.42 4.20 | macmini (bots): { 61.65 67.22 74.22 } +2026-10-17T17:51:27Z thinkpad: 3.62 2.72 4.24 | macmini (bots): { 54.05 64.88 73.13 } +2026-10-17T17:51:57Z thinkpad: 3.25 2.70 4.19 | macmini (bots): { 49.14 62.62 72.02 } +2026-10-17T17:52:28Z thinkpad: 2.45 2.55 4.09 | macmini (bots): { 43.72 60.06 70.77 } +2026-10-17T17:52:58Z thinkpad: 2.85 2.62 4.06 | macmini (bots): { 42.53 58.22 69.73 } +2026-10-17T17:53:28Z thinkpad: 8.44 3.97 4.46 | macmini (bots): { 45.21 57.34 69.01 } +2026-10-17T17:53:58Z thinkpad: 10.75 4.91 4.76 | macmini (bots): { 44.86 56.01 68.11 } +2026-10-17T17:54:29Z thinkpad: 8.36 4.90 4.76 | macmini (bots): { 51.73 56.60 67.91 } +2026-10-17T17:54:59Z thinkpad: 7.41 4.98 4.79 | macmini (bots): { 55.80 57.05 67.67 } +2026-10-17T17:55:29Z thinkpad: 5.45 4.73 4.71 | macmini (bots): { 50.40 55.67 66.81 } +2026-10-17T17:55:59Z thinkpad: 6.26 4.97 4.79 | macmini (bots): { 57.22 56.88 66.87 } +2026-10-17T17:56:30Z thinkpad: 8.22 5.52 4.98 | macmini (bots): { 48.94 54.86 65.78 } +2026-10-17T17:57:00Z thinkpad: 6.71 5.42 4.96 | macmini (bots): { 50.54 54.70 65.35 } +2026-10-17T17:57:30Z thinkpad: 4.94 5.14 4.89 | macmini (bots): { 58.02 56.25 65.55 } +2026-10-17T17:58:01Z thinkpad: 4.54 5.04 4.86 | macmini (bots): { 53.98 55.38 64.90 } +2026-10-17T17:58:31Z thinkpad: 4.65 5.04 4.87 | macmini (bots): { 49.96 54.23 64.16 } +2026-10-17T17:59:01Z thinkpad: 5.29 5.16 4.92 | macmini (bots): { 49.63 53.68 63.61 } +2026-10-17T17:59:31Z thinkpad: 4.23 4.94 4.85 | macmini (bots): { 57.15 55.10 63.78 } +2026-10-17T18:00:01Z thinkpad: 3.57 4.72 4.78 | macmini (bots): { 61.70 56.26 63.89 } +2026-10-17T18:00:32Z thinkpad: 3.16 4.52 4.71 | macmini (bots): { 62.10 56.86 63.79 } +2026-10-17T18:01:02Z thinkpad: 2.81 4.29 4.63 | macmini (bots): { 76.29 60.79 64.98 } +2026-10-17T18:01:32Z thinkpad: 7.77 5.26 4.93 | macmini (bots): { 75.36 62.16 65.34 } +2026-10-17T18:02:02Z thinkpad: 11.60 6.45 5.34 | macmini (bots): { 70.83 62.28 65.27 } +2026-10-17T18:02:33Z thinkpad: 11.54 6.97 5.56 | macmini (bots): { 65.66 61.87 65.01 } +2026-10-17T18:03:03Z thinkpad: 10.80 7.33 5.73 | macmini (bots): { 63.09 61.54 64.78 } +2026-10-17T18:03:33Z thinkpad: 10.52 7.56 5.85 | macmini (bots): { 60.31 61.12 64.52 } +2026-10-17T18:04:04Z thinkpad: 10.27 7.76 5.97 | macmini (bots): { 60.99 61.21 64.44 } +2026-10-17T18:04:34Z thinkpad: 8.77 7.69 6.01 | macmini (bots): { 56.72 60.19 63.96 } +2026-10-17T18:05:05Z thinkpad: 5.77 7.06 5.85 | macmini (bots): { 56.14 59.70 63.65 } +2026-10-17T18:05:35Z thinkpad: 5.16 6.79 5.80 | macmini (bots): { 51.25 58.17 62.95 } +2026-10-17T18:06:05Z thinkpad: 4.16 6.36 5.69 | macmini (bots): { 52.03 57.64 62.59 } +2026-10-17T18:06:35Z thinkpad: 4.28 6.18 5.65 | macmini (bots): { 64.66 60.00 63.27 } +2026-10-17T18:07:06Z thinkpad: 7.47 6.76 5.86 | macmini (bots): { 56.20 58.45 62.59 } +2026-10-17T18:07:36Z thinkpad: 6.95 6.74 5.89 | macmini (bots): { 51.92 57.20 62.00 } +2026-10-17T18:08:06Z thinkpad: 7.70 6.99 6.00 | macmini (bots): { 51.14 56.65 61.64 } +2026-10-17T18:08:36Z thinkpad: 5.83 6.60 5.90 | macmini (bots): { 48.08 55.33 60.98 } +2026-10-17T18:09:07Z thinkpad: 4.89 6.30 5.82 | macmini (bots): { 53.80 56.00 61.00 } +2026-10-17T18:09:37Z thinkpad: 4.93 6.19 5.80 | macmini (bots): { 60.49 57.24 61.27 } +2026-10-17T18:10:07Z thinkpad: 4.61 5.98 5.74 | macmini (bots): { 58.95 57.21 61.12 } +2026-10-17T18:10:37Z thinkpad: 4.74 5.91 5.73 | macmini (bots): { 60.84 57.65 61.13 } +2026-10-17T18:11:07Z thinkpad: 3.96 5.61 5.63 | macmini (bots): { 64.23 58.82 61.43 } +2026-10-17T18:11:38Z thinkpad: 4.16 5.53 5.61 | macmini (bots): { 75.59 62.26 62.60 } +2026-10-17T18:12:08Z thinkpad: 2.97 5.11 5.46 | macmini (bots): { 75.25 63.65 63.10 } diff --git a/benchmarks/server-2026-10-17/walk-explore.json b/benchmarks/server-2026-10-17/walk-explore.json new file mode 100644 index 000000000..a9479f455 --- /dev/null +++ b/benchmarks/server-2026-10-17/walk-explore.json @@ -0,0 +1,695 @@ +{ + "kind": "horizon-server-benchmark", + "date": "2026-10-17T17:31:40+00:00", + "machine": { + "system": "Linux", + "machine": "x86_64", + "cpus": 4 + }, + "commit": "7bbbf6e97fb53683eae0a01ce2c134e53cb649a7", + "java": [ + "openjdk 25.0.4.1 2026-08-18" + ], + "settings": { + "servers": "horizon,vanilla", + "bots": "1,10,25,50,100", + "behaviour": "walk,explore", + "join_every": 1.0, + "warmup": 60, + "seconds": 180, + "seed": 12345, + "explore_speed": 0, + "gamerule": "", + "heap": "4g", + "out": "/home/ludovic/code/horizon/.claude/worktrees/server-host-4/.local/bench-out/walk-explore.json", + "nice": 19, + "bots_via": "ssh macmini", + "bots_bin": "/Users/ludovicmarie/code/horizon/.claude/worktrees/server-host-4/target/release/horizon-bots", + "connect": "", + "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": 1, + "behaviour": "walk", + "start_seconds": 9.62, + "mspt": { + "average": 4.01, + "p50": 3.54, + "p95": 8.19, + "p99": 25.59 + }, + "mspt_worst_p99": 47.5, + "tps": 20.0, + "rss_mb": { + "mean": 782.52, + "max": 785.3 + }, + "cpu_percent": 58.87, + "chunks_per_second": 9.6, + "unique_per_second": 0.43, + "forgotten_per_second": 9.65, + "waited_ticks_per_second": 0.0, + "chunks_total": 2697, + "chunks_per_bot": { + "walk": 2697.0 + }, + "unique_chunks_per_bot": { + "walk": 693.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 1, + "behaviour": "walk", + "start_seconds": 14.85, + "mspt": { + "average": 18.22, + "p50": 17.99, + "p95": 29.34, + "p99": 50.06 + }, + "mspt_worst_p99": 395.5, + "tps": 20.0, + "rss_mb": { + "mean": 3429.06, + "max": 3446.3 + }, + "cpu_percent": 68.81, + "chunks_per_second": 10.47, + "unique_per_second": 0.47, + "forgotten_per_second": 10.53, + "waited_ticks_per_second": 0.0, + "chunks_total": 2755, + "chunks_per_bot": { + "walk": 2755.0 + }, + "unique_chunks_per_bot": { + "walk": 697.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 10, + "behaviour": "walk", + "start_seconds": 9.21, + "mspt": { + "average": 8.79, + "p50": 8.05, + "p95": 17.35, + "p99": 30.47 + }, + "mspt_worst_p99": 48.8, + "tps": 20.0, + "rss_mb": { + "mean": 781.88, + "max": 782.8 + }, + "cpu_percent": 119.16, + "chunks_per_second": 94.63, + "unique_per_second": 3.58, + "forgotten_per_second": 94.31, + "waited_ticks_per_second": 0.0, + "chunks_total": 26003, + "chunks_per_bot": { + "walk": 2600.3 + }, + "unique_chunks_per_bot": { + "walk": 679.9 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "walk", + "start_seconds": 15.14, + "mspt": { + "average": 18.67, + "p50": 15.8, + "p95": 38.26, + "p99": 55.93 + }, + "mspt_worst_p99": 101.8, + "tps": 20.0, + "rss_mb": { + "mean": 3497.35, + "max": 3499.4 + }, + "cpu_percent": 100.8, + "chunks_per_second": 101.1, + "unique_per_second": 4.38, + "forgotten_per_second": 101.15, + "waited_ticks_per_second": 0.0, + "chunks_total": 26537, + "chunks_per_bot": { + "walk": 2653.7 + }, + "unique_chunks_per_bot": { + "walk": 687.5 + }, + "resent": 0, + "pulled_back": 5, + "moved_too_quickly": 5, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "walk", + "start_seconds": 9.82, + "mspt": { + "average": 15.01, + "p50": 13.12, + "p95": 31.12, + "p99": 50.62 + }, + "mspt_worst_p99": 101.6, + "tps": 20.0, + "rss_mb": { + "mean": 799.52, + "max": 799.7 + }, + "cpu_percent": 161.02, + "chunks_per_second": 234.3, + "unique_per_second": 8.49, + "forgotten_per_second": 234.35, + "waited_ticks_per_second": 0.0, + "chunks_total": 65926, + "chunks_per_bot": { + "walk": 2637.04 + }, + "unique_chunks_per_bot": { + "walk": 683.96 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "walk", + "start_seconds": 14.95, + "mspt": { + "average": 18.78, + "p50": 17.47, + "p95": 28.16, + "p99": 51.03 + }, + "mspt_worst_p99": 107.6, + "tps": 20.0, + "rss_mb": { + "mean": 3520.17, + "max": 3573.8 + }, + "cpu_percent": 154.9, + "chunks_per_second": 252.39, + "unique_per_second": 11.89, + "forgotten_per_second": 251.75, + "waited_ticks_per_second": 0.0, + "chunks_total": 66469, + "chunks_per_bot": { + "walk": 2658.76 + }, + "unique_chunks_per_bot": { + "walk": 689.68 + }, + "resent": 0, + "pulled_back": 10, + "moved_too_quickly": 10, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "walk", + "start_seconds": 9.52, + "mspt": { + "average": 22.72, + "p50": 16.98, + "p95": 60.19, + "p99": 84.16 + }, + "mspt_worst_p99": 105.4, + "tps": 20.0, + "rss_mb": { + "mean": 813.5, + "max": 815.8 + }, + "cpu_percent": 179.85, + "chunks_per_second": 464.75, + "unique_per_second": 16.94, + "forgotten_per_second": 464.69, + "waited_ticks_per_second": 0.0, + "chunks_total": 134839, + "chunks_per_bot": { + "walk": 2696.78 + }, + "unique_chunks_per_bot": { + "walk": 685.48 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 50, + "behaviour": "walk", + "start_seconds": 16.36, + "mspt": { + "average": 34.3, + "p50": 31.07, + "p95": 55.55, + "p99": 92.32 + }, + "mspt_worst_p99": 452.1, + "tps": 20.0, + "rss_mb": { + "mean": 3577.02, + "max": 3611.2 + }, + "cpu_percent": 267.05, + "chunks_per_second": 496.77, + "unique_per_second": 26.74, + "forgotten_per_second": 496.2, + "waited_ticks_per_second": 0.0, + "chunks_total": 133505, + "chunks_per_bot": { + "walk": 2670.1 + }, + "unique_chunks_per_bot": { + "walk": 688.8 + }, + "resent": 0, + "pulled_back": 39, + "moved_too_quickly": 39, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 100, + "behaviour": "walk", + "start_seconds": 9.51, + "mspt": { + "average": 63.33, + "p50": 58.89, + "p95": 125.04, + "p99": 175.12 + }, + "mspt_worst_p99": 261.5, + "tps": 15.79, + "rss_mb": { + "mean": 868.12, + "max": 869.9 + }, + "cpu_percent": 245.64, + "chunks_per_second": 678.18, + "unique_per_second": 31.8, + "forgotten_per_second": 676.4, + "waited_ticks_per_second": 0.0, + "chunks_total": 219787, + "chunks_per_bot": { + "walk": 2197.87 + }, + "unique_chunks_per_bot": { + "walk": 637.91 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 100, + "bots_failed": [], + "tick_queries": 34 + }, + { + "server": "vanilla", + "bots": 100, + "behaviour": "walk", + "start_seconds": 15.14, + "error": "[Errno 32] Broken pipe" + }, + { + "server": "horizon", + "bots": 1, + "behaviour": "explore", + "start_seconds": 9.72, + "mspt": { + "average": 9.73, + "p50": 8.75, + "p95": 20.99, + "p99": 34.17 + }, + "mspt_worst_p99": 73.9, + "tps": 20.0, + "rss_mb": { + "mean": 836.18, + "max": 891.0 + }, + "cpu_percent": 329.64, + "chunks_per_second": 41.11, + "unique_per_second": 35.96, + "forgotten_per_second": 41.08, + "waited_ticks_per_second": 0.16, + "chunks_total": 9460, + "chunks_per_bot": { + "explore": 9460.0 + }, + "unique_chunks_per_bot": { + "explore": 8479.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 1, + "behaviour": "explore", + "start_seconds": 15.54, + "mspt": { + "average": 9.71, + "p50": 7.86, + "p95": 20.37, + "p99": 76.15 + }, + "mspt_worst_p99": 388.6, + "tps": 20.0, + "rss_mb": { + "mean": 3614.97, + "max": 3688.5 + }, + "cpu_percent": 247.15, + "chunks_per_second": 35.47, + "unique_per_second": 33.61, + "forgotten_per_second": 35.45, + "waited_ticks_per_second": 0.0, + "chunks_total": 7912, + "chunks_per_bot": { + "explore": 7912.0 + }, + "unique_chunks_per_bot": { + "explore": 7580.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 10, + "behaviour": "explore", + "start_seconds": 9.31, + "mspt": { + "average": 21.27, + "p50": 19.5, + "p95": 39.15, + "p99": 54.23 + }, + "mspt_worst_p99": 105.5, + "tps": 20.0, + "rss_mb": { + "mean": 1177.66, + "max": 1392.9 + }, + "cpu_percent": 321.97, + "chunks_per_second": 46.47, + "unique_per_second": 46.11, + "forgotten_per_second": 43.12, + "waited_ticks_per_second": 111.25, + "chunks_total": 14103, + "chunks_per_bot": { + "explore": 1410.3 + }, + "unique_chunks_per_bot": { + "explore": 1402.4 + }, + "resent": 0, + "pulled_back": 9856, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "explore", + "start_seconds": 15.24, + "mspt": { + "average": 16.94, + "p50": 14.24, + "p95": 30.28, + "p99": 109.7 + }, + "mspt_worst_p99": 1861.2, + "tps": 20.0, + "rss_mb": { + "mean": 3953.29, + "max": 4232.0 + }, + "cpu_percent": 254.11, + "chunks_per_second": 24.99, + "unique_per_second": 25.01, + "forgotten_per_second": 25.33, + "waited_ticks_per_second": 77.93, + "chunks_total": 7707, + "chunks_per_bot": { + "explore": 770.7 + }, + "unique_chunks_per_bot": { + "explore": 769.5 + }, + "resent": 0, + "pulled_back": 11123, + "moved_too_quickly": 20, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "explore", + "start_seconds": 9.92, + "mspt": { + "average": 25.76, + "p50": 23.58, + "p95": 45.01, + "p99": 61.43 + }, + "mspt_worst_p99": 108.8, + "tps": 20.0, + "rss_mb": { + "mean": 1172.1, + "max": 1482.3 + }, + "cpu_percent": 314.47, + "chunks_per_second": 61.38, + "unique_per_second": 60.7, + "forgotten_per_second": 55.11, + "waited_ticks_per_second": 304.54, + "chunks_total": 24085, + "chunks_per_bot": { + "explore": 963.4 + }, + "unique_chunks_per_bot": { + "explore": 954.76 + }, + "resent": 0, + "pulled_back": 31264, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "explore", + "start_seconds": 15.76, + "mspt": { + "average": 24.93, + "p50": 22.04, + "p95": 37.09, + "p99": 168.46 + }, + "mspt_worst_p99": 1724.8, + "tps": 20.0, + "rss_mb": { + "mean": 4040.42, + "max": 4447.6 + }, + "cpu_percent": 305.47, + "chunks_per_second": 37.91, + "unique_per_second": 37.84, + "forgotten_per_second": 38.39, + "waited_ticks_per_second": 258.66, + "chunks_total": 16865, + "chunks_per_bot": { + "explore": 674.6 + }, + "unique_chunks_per_bot": { + "explore": 671.28 + }, + "resent": 0, + "pulled_back": 33467, + "moved_too_quickly": 23, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "explore", + "start_seconds": 9.41, + "mspt": { + "average": 39.31, + "p50": 36.98, + "p95": 65.33, + "p99": 88.36 + }, + "mspt_worst_p99": 157.6, + "tps": 20.0, + "rss_mb": { + "mean": 1250.32, + "max": 1517.1 + }, + "cpu_percent": 324.77, + "chunks_per_second": 114.99, + "unique_per_second": 112.25, + "forgotten_per_second": 104.41, + "waited_ticks_per_second": 543.77, + "chunks_total": 50402, + "chunks_per_bot": { + "explore": 1008.04 + }, + "unique_chunks_per_bot": { + "explore": 997.42 + }, + "resent": 0, + "pulled_back": 64163, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 50, + "behaviour": "explore", + "start_seconds": 14.55, + "error": "[Errno 32] Broken pipe" + }, + { + "server": "horizon", + "bots": 100, + "behaviour": "explore", + "start_seconds": 9.31, + "mspt": { + "average": 52.7, + "p50": 49.49, + "p95": 85.73, + "p99": 115.34 + }, + "mspt_worst_p99": 219.0, + "tps": 18.97, + "rss_mb": { + "mean": 1358.32, + "max": 3047.2 + }, + "cpu_percent": 327.95, + "chunks_per_second": 154.2, + "unique_per_second": 151.83, + "forgotten_per_second": 141.65, + "waited_ticks_per_second": 1132.9, + "chunks_total": 84433, + "chunks_per_bot": { + "explore": 844.33 + }, + "unique_chunks_per_bot": { + "explore": 839.47 + }, + "resent": 0, + "pulled_back": 116161, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 100, + "bots_failed": [], + "tick_queries": 35 + }, + { + "server": "vanilla", + "bots": 100, + "behaviour": "explore", + "start_seconds": 16.47, + "error": "[Errno 32] Broken pipe" + } + ] +} diff --git a/benchmarks/server-equal-mixed-2026-10-18/README.md b/benchmarks/server-equal-mixed-2026-10-18/README.md new file mode 100644 index 000000000..5849e72ba --- /dev/null +++ b/benchmarks/server-equal-mixed-2026-10-18/README.md @@ -0,0 +1,137 @@ +# Horizon's server against vanilla 26.3: mixed bots, equal work (2026-10-18) + +This reruns `../server-mixed-2026-10-18/` with the bots' load made the same on both servers. In that run the +`explore` bots flew as fast as each server sent them chunks. Horizon kept up and vanilla didn't, so Horizon +sent 2.9 to 5.9 times as many chunks a second, and vanilla's walking bots got a fifth of the chunks Horizon's +got. The comparison of tick times wasn't fair. + +## Summary + +- **The work was equal** at 1 and 10 bots. At 25 bots it was equal in chunks sent a second (158.6 against 157.6); + vanilla was still catching up on chunks its bots hadn't had yet, see "Equal work" below. Every bot joined, none + was dropped, no chunk was sent twice, and no bot ever waited for the chunk it stood in. +- **Tick time (MSPT), same work:** + - 1 bot: 1.8 ms on Horizon against 11.3 on vanilla; + - 10 bots: 4.7 against 34.3 ms; + - 25 bots: 11.7 against 50.5 ms. Vanilla dropped just under 20 TPS (19.8) and its worst 5-second P99 was + 1388 ms, against Horizon's 130; + - 50 bots: Horizon ran at 23.8 ms (20 TPS). Vanilla's watchdog stopped its server 30 s after the last bot + joined ("A single server tick took 60.01 seconds"). +- **Memory:** Horizon's RSS was 4.1 to 4.9 times smaller: 0.69 against 3.40 GB with one bot, 0.95 against + 3.87 GB with 25, and 1.03 GB with 50. Vanilla's heap is fixed at 4 GB, so its RSS is mostly that heap. +- **CPU** (100 % is one of the machine's 4 threads): 30 against 128 % at 10 bots, 58 against 192 % at 25. +- **What changed since the first run:** the first run's vanilla held 50 bots at 29.7 ms because its walking + bots got 640 to 1100 chunks each. With the explore bots paced, vanilla serves the walking bots in full + (5400 to 6100 chunks each, against Horizon's 6100 to 6600), and that is the load it can't carry at 50. + +## Results + +MSPT is milliseconds per tick from each server's own `/tick query` (50 is the limit for 20 TPS). P95 and P99 +are over the 300 measured seconds. "Worst P99" is the highest P99 of a single 5-second 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). "Waited" is ticks a second that bots stood still waiting for their own chunk. "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 | Waited | Pulled back | +|---:|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:| +| 1 | Horizon | **1.77** | 4.21 | 13.04 | 47.5 | 20 | **0.69 / 0.69 GB** | 28 % | 19.7 s | 9.94 | 0.01 | 0 | 0 | +| 1 | vanilla | 11.34 | 17.12 | 33.22 | 119.1 | 20 | 3.40 / 3.42 GB | 36 % | 20.4 s | 9.98 | 0.01 | 0 | 0 | +| 10 | Horizon | **4.74** | 10.48 | 19.69 | 120.1 | 20 | **0.86 / 0.88 GB** | 30 % | 19.5 s | 61.35 | 1.79 | 0 | 4159 | +| 10 | vanilla | 34.30 | 53.72 | 89.08 | 375.8 | 20 | 3.71 / 3.75 GB | 128 % | 19.9 s | 61.66 | 1.93 | 0 | 3976 | +| 25 | Horizon | **11.72** | 24.45 | 43.79 | 129.8 | 20 | **0.95 / 1.09 GB** | 58 % | 9.6 s | 158.57 | 5.78 | 0 | 3496 | +| 25 | vanilla | 50.45 | 74.09 | 191.21 | 1388.1 | 19.82 | 3.87 / 4.75 GB | 192 % | 37.8 s | 157.63 | 8.25 | 0 | 3629 | +| 50 | Horizon | **23.82** | 50.98 | 74.64 | 195.9 | 20 | **1.03 / 1.20 GB** | 98 % | 14.9 s | 302.81 | 10.96 | 0 | 7565 | +| 50 | vanilla | stopped by its watchdog | | | | | | | 11.2 s | | | | | + +Chunks each bot received over the whole run, by behaviour (mean): + +| Bots | Server | walk | explore | idle | +|---:|---|---:|---:|---:| +| 1 | Horizon | 6092 | | | +| 1 | vanilla | 6084 | | | +| 10 | Horizon | 6248 | 1165 | 473 | +| 10 | vanilla | 5729 | 1124 | 473 | +| 25 | Horizon | 6629 | 1267 | 473 | +| 25 | vanilla | 5387 | 1119 | 473 | +| 50 | Horizon | 6497 | 1256 | 473 | + +Both servers pulled bots back about as often (`moved too quickly` 20 and 296 times on vanilla at 10 and 25 +bots). + +## Equal work + +`tools/server_bench.py` now prints a work table after the runs. At each bot count it compares every server's +chunks sent a second and first-time chunks a second with the busiest server's. Work counts as equal when +both are within 10 % (or within a chunk a second when there are few). The report keeps the table as `work`. + +| Bots | Chunks/s (Horizon, vanilla) | First-time chunks/s (Horizon, vanilla) | Equal | +|---:|---|---|---| +| 1 | 9.94, 9.98 | 0.01, 0.01 | yes | +| 10 | 61.35, 61.66 | 1.79, 1.93 | yes | +| 25 | 158.57, 157.63 | 5.78, 8.25 | no: vanilla sent 2.5 more first-time chunks a second | + +Two settings made the load the same: + +- **Explore bots paced at 0.8 blocks a second** (`--explore-speed 0.8`), the rate the 2026-10-17 run showed + both servers keep up with. Every bot then follows the same path on both servers. The path is deterministic + per bot, and a bot only stops while the chunk it stands in hasn't arrived, which never happened here + (`Waited` 0). So both servers had the same chunks to send. +- **A 300-second warm-up** before the 300 measured seconds. A first pass with the old 60-second warm-up + (aborted after 10 bots) sent the same chunks a second on both (63.1 against 61.4). But vanilla was still + delivering the bots' first views during the measurement (12.9 first-time chunks a second against Horizon's + 4.1), so its window held more work than Horizon's. With 300 seconds both were in steady state at 1 and 10 + bots. At 25, vanilla still sent 2.5 more first-time chunks a second than Horizon: 1.6 % of all it sent, and + its walking bots ended the run with 19 % fewer chunks. Read the 25-bot tick times with that in mind: the + extra first-time chunks are work vanilla had still to do because it fell behind. + +## Method + +`tools/server_bench.py` ran each configuration on a fresh server: + +- the same `server.properties` on both: seed 12345, view and simulation distance 10, offline, creative, + difficulty easy, `sync-chunk-writes=true`, white-list off; +- `gamerule respawn_radius 0`, and otherwise vanilla's default game rules (the locator bar on for both); +- 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, and `idle` hovers. + +**Machine.** The ThinkPad: Intel Core i7-7500U (2 cores, 4 threads), 32 GB, NixOS. The desktop (24 threads) +was meant to run it, but it didn't wake (`~/fleet/power wake desktop` timed out, as it did for the first run). +No other session ran on the ThinkPad during the runs (`load.txt`, a sample a minute). The servers ran under +`nice -n 19`. The bots ran on the Mac mini (M4 Pro, shared with other work at 70 to 90 % CPU) and reached the +ThinkPad over the tailnet. + +**What ran.** + +- Horizon: the server build from #916 (`horizon-client --no-default-features`, no sound or controllers) at + 519ec121, packaged with `xtask package-server` and installed with `install.sh --archive`, as Linux owners + will get it. `horizon-server` ran it as `horizon-engine`. The game files were prepared from the owner's own + Minecraft. `horizon-bots` and `server_bench.py` came from this PR. +- Vanilla: the pinned 26.3 server jar (the owner's own copy) on OpenJDK 25.0.4.1, `-Xms4g -Xmx4g`. + +`server_bench.py` stopped after vanilla's 50-bot server crashed and before it wrote its report. +`mixed-equal.json` holds the run lines it printed. The vanilla 50 entry and its error come from that server's +log, and the `work` table was computed with `server_bench.work`. `load.txt` is the fleet monitor's view of the +machines during the runs. + +## 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 --bots 1,10,25,50 --behaviour mixed \ + --explore-speed 0.8 --warmup 300 --nice 19 \ + --bots-via 'ssh ' --bots-bin /path/to/horizon-bots --connect \ + --out mixed-equal.json + +## Next + +- One pass per configuration ran. Vanilla at 50 bots, in particular, needs a second pass, ideally on the + desktop (24 threads), where neither server is CPU-bound. +- 100 bots, and Paper and Fabric + Lithium (docs/server.md section 9). +- A longer warm-up, or measuring from when the first-time chunk rate settles, so the 25-bot work is equal too. diff --git a/benchmarks/server-equal-mixed-2026-10-18/load.txt b/benchmarks/server-equal-mixed-2026-10-18/load.txt new file mode 100644 index 000000000..2992b4a26 --- /dev/null +++ b/benchmarks/server-equal-mixed-2026-10-18/load.txt @@ -0,0 +1,702 @@ +2026-10-18T19:32:39Z +ludothinkpad ok: 27.2 GB free, 4 threads 17% busy, 0 sessions +desktop offline +macmini ok: 25.9 GB free, 14 threads 73% busy, 1 sessions +macbook offline + RAM 27.0 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 42% busy (agents 0.0, rest 1.6 cores) · 80°C · load 5.9 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:33:44Z +ludothinkpad ok: 26.7 GB free, 4 threads 19% busy, 0 sessions +desktop offline +macmini busy: 26.7 GB free, 14 threads 93% busy, 1 sessions +macbook offline + RAM 26.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 34% busy (agents 0.0, rest 1.7 cores) · 81°C · load 4.0 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:34:48Z +ludothinkpad ok: 26.4 GB free, 4 threads 56% busy, 0 sessions +desktop offline +macmini busy: 26.9 GB free, 14 threads 90% busy, 1 sessions +macbook offline + RAM 26.4 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 54% busy (agents 0.0, rest 2.2 cores) · 79°C · load 2.8 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:35:52Z +ludothinkpad ok: 26.7 GB free, 4 threads 27% busy, 0 sessions +desktop offline +macmini ok: 25.4 GB free, 14 threads 85% busy, 1 sessions +macbook offline + RAM 26.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 30% busy (agents 0.0, rest 1.1 cores) · 82°C · load 2.2 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:36:57Z +ludothinkpad ok: 26.7 GB free, 4 threads 25% busy, 0 sessions +desktop offline +macmini busy: 25.4 GB free, 14 threads 93% busy, 1 sessions +macbook offline + RAM 26.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 37% busy (agents 0.0, rest 1.2 cores) · 80°C · load 1.5 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:38:01Z +ludothinkpad ok: 26.6 GB free, 4 threads 54% busy, 0 sessions +desktop offline +macmini ok: 26.7 GB free, 14 threads 64% busy, 1 sessions +macbook offline + RAM 26.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 21% busy (agents 0.0, rest 1.2 cores) · 81°C · load 1.9 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:39:05Z +ludothinkpad ok: 26.7 GB free, 4 threads 20% busy, 0 sessions +desktop offline +macmini ok: 26.1 GB free, 14 threads 70% busy, 1 sessions +macbook offline + RAM 26.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 28% busy (agents 0.0, rest 1.0 cores) · 81°C · load 1.3 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:40:10Z +ludothinkpad ok: 26.7 GB free, 4 threads 39% busy, 0 sessions +desktop offline +macmini ok: 29.2 GB free, 14 threads 73% busy, 1 sessions +macbook offline + RAM 26.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 32% busy (agents 0.0, rest 1.7 cores) · 80°C · load 0.9 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:41:14Z +ludothinkpad ok: 26.7 GB free, 4 threads 31% busy, 0 sessions +desktop offline +macmini ok: 28.8 GB free, 14 threads 77% busy, 1 sessions +macbook offline + RAM 26.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 15% busy (agents 0.0, rest 1.1 cores) · 82°C · load 1.3 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:42:19Z +ludothinkpad ok: 26.5 GB free, 4 threads 26% busy, 0 sessions +desktop offline +macmini busy: 26.1 GB free, 14 threads 94% busy, 1 sessions +macbook offline + RAM 26.2 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 30% busy (agents 0.0, rest 1.3 cores) · 78°C · load 1.1 + · disk 289.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:43:23Z +ludothinkpad busy: 26.7 GB free, 4 threads 99% busy, 0 sessions +desktop offline +macmini ok: 28.2 GB free, 14 threads 83% busy, 1 sessions +macbook offline + RAM 26.3 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 99% busy (agents 0.0, rest 3.8 cores) · 86°C · load 2.4 + · disk 289.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:44:27Z +ludothinkpad busy: 25.2 GB free, 4 threads 98% busy, 0 sessions +desktop offline +macmini ok: 29.0 GB free, 14 threads 73% busy, 1 sessions +macbook offline + RAM 24.9 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 83% busy (agents 0.0, rest 3.7 cores) · 83°C · load 5.5 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:45:31Z +ludothinkpad ok: 24.5 GB free, 4 threads 39% busy, 0 sessions +desktop offline +macmini busy: 27.0 GB free, 14 threads 93% busy, 1 sessions +macbook offline + RAM 24.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 34% busy (agents 0.0, rest 1.5 cores) · 84°C · load 3.5 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:46:35Z +ludothinkpad ok: 23.7 GB free, 4 threads 69% busy, 0 sessions +desktop offline +macmini busy: 26.2 GB free, 14 threads 90% busy, 1 sessions +macbook offline + RAM 23.8 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 75% busy (agents 0.0, rest 2.8 cores) · 87°C · load 2.7 + · disk 289.5 GB free · battery 96% · Ludovic: away + +2026-10-18T19:47:40Z +ludothinkpad ok: 23.8 GB free, 4 threads 66% busy, 0 sessions +desktop offline +macmini busy: 25.9 GB free, 14 threads 85% busy, 1 sessions +macbook offline + RAM 23.8 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 59% busy (agents 0.0, rest 2.5 cores) · 83°C · load 2.9 + · disk 291.7 GB free · battery 96% · Ludovic: away + +2026-10-18T19:48:44Z +ludothinkpad ok: 24.2 GB free, 4 threads 61% busy, 0 sessions +desktop offline +macmini busy: 29.0 GB free, 14 threads 90% busy, 1 sessions +macbook offline + RAM 24.3 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 62% busy (agents 0.0, rest 2.5 cores) · 84°C · load 3.2 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:49:48Z +ludothinkpad ok: 25.1 GB free, 4 threads 59% busy, 0 sessions +desktop offline +macmini ok: 31.1 GB free, 14 threads 81% busy, 1 sessions +macbook offline + RAM 25.1 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 57% busy (agents 0.0, rest 2.4 cores) · 85°C · load 2.9 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:50:52Z +ludothinkpad ok: 25.7 GB free, 4 threads 39% busy, 0 sessions +desktop offline +macmini busy: 28.4 GB free, 14 threads 88% busy, 2 sessions +macbook offline + RAM 25.8 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 18% busy (agents 0.0, rest 1.4 cores) · 67°C · load 2.4 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:51:57Z +ludothinkpad ok: 23.7 GB free, 4 threads 40% busy, 0 sessions +desktop offline +macmini ok: 27.9 GB free, 14 threads 83% busy, 1 sessions +macbook offline + RAM 23.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 23% busy (agents 0.0, rest 1.4 cores) · 81°C · load 1.8 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:53:01Z +ludothinkpad ok: 25.3 GB free, 4 threads 25% busy, 0 sessions +desktop offline +macmini ok: 27.1 GB free, 14 threads 84% busy, 1 sessions +macbook offline + RAM 25.1 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 31% busy (agents 0.0, rest 1.2 cores) · 80°C · load 1.8 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:54:05Z +ludothinkpad ok: 26.5 GB free, 4 threads 75% busy, 0 sessions +desktop offline +macmini busy: 27.1 GB free, 14 threads 91% busy, 1 sessions +macbook offline + RAM 26.5 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 69% busy (agents 0.0, rest 2.5 cores) · 78°C · load 2.3 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:55:09Z +ludothinkpad busy: 26.4 GB free, 4 threads 89% busy, 0 sessions +desktop offline +macmini busy: 26.3 GB free, 14 threads 86% busy, 1 sessions +macbook offline + RAM 26.3 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 32% busy (agents 0.0, rest 2.4 cores) · 79°C · load 2.9 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:56:14Z +ludothinkpad ok: 26.4 GB free, 4 threads 39% busy, 0 sessions +desktop offline +macmini ok: 27.1 GB free, 14 threads 68% busy, 1 sessions +macbook offline + RAM 26.4 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 63% busy (agents 0.0, rest 1.8 cores) · 82°C · load 2.3 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:57:18Z +ludothinkpad ok: 26.3 GB free, 4 threads 20% busy, 0 sessions +desktop offline +macmini busy: 29.2 GB free, 14 threads 85% busy, 1 sessions +macbook offline + RAM 26.3 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 19% busy (agents 0.0, rest 1.0 cores) · 80°C · load 1.8 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:58:22Z +ludothinkpad ok: 26.3 GB free, 4 threads 28% busy, 0 sessions +desktop offline +macmini busy: 27.3 GB free, 14 threads 88% busy, 1 sessions +macbook offline + RAM 26.1 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 93% busy (agents 0.0, rest 2.5 cores) · 80°C · load 2.0 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T19:59:27Z +ludothinkpad ok: 26.2 GB free, 4 threads 52% busy, 0 sessions +desktop offline +macmini ok: 35.0 GB free, 14 threads 85% busy, 1 sessions +macbook offline + RAM 26.2 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 22% busy (agents 0.0, rest 1.8 cores) · 80°C · load 3.6 + · disk 291.4 GB free · battery 96% · Ludovic: away + +2026-10-18T20:00:31Z +ludothinkpad busy: 25.8 GB free, 4 threads 87% busy, 0 sessions +desktop offline +macmini busy: 33.0 GB free, 14 threads 87% busy, 1 sessions +macbook offline + RAM 27.5 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 51% busy (agents 0.0, rest 2.9 cores) · 79°C · load 3.6 + · disk 291.5 GB free · battery 96% · Ludovic: away + +2026-10-18T20:01:35Z +ludothinkpad ok: 25.6 GB free, 4 threads 35% busy, 0 sessions +desktop offline +macmini ok: 32.4 GB free, 14 threads 81% busy, 1 sessions +macbook ok: 7.2 GB free, 12 threads 32% busy, 0 sessions + RAM 25.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 34% busy (agents 0.0, rest 1.3 cores) · 81°C · load 2.5 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T20:02:40Z +ludothinkpad ok: 26.6 GB free, 4 threads 35% busy, 0 sessions +desktop offline +macmini ok: 29.2 GB free, 14 threads 79% busy, 1 sessions +macbook ok: 6.5 GB free, 12 threads 39% busy, 0 sessions + RAM 26.5 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 33% busy (agents 0.0, rest 1.3 cores) · 81°C · load 2.7 + · disk 291.6 GB free · battery 96% · Ludovic: away + +2026-10-18T20:03:44Z +ludothinkpad ok: 25.5 GB free, 4 threads 56% busy, 0 sessions +desktop offline +macmini busy: 31.1 GB free, 14 threads 89% busy, 1 sessions +macbook ok: 6.8 GB free, 12 threads 6% busy, 0 sessions + RAM 25.2 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 66% busy (agents 0.0, rest 2.5 cores) · 87°C · load 2.5 + · disk 291.3 GB free · battery 96% · Ludovic: away + +2026-10-18T20:04:48Z +ludothinkpad busy: 26.2 GB free, 4 threads 99% busy, 0 sessions +desktop offline +macmini busy: 30.0 GB free, 14 threads 89% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 31% busy, 0 sessions + RAM 25.8 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 84% busy (agents 0.0, rest 3.7 cores) · 85°C · load 3.4 + · disk 291.3 GB free · battery 96% · Ludovic: away + +2026-10-18T20:05:52Z +ludothinkpad busy: 24.5 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini busy: 34.1 GB free, 14 threads 89% busy, 1 sessions +macbook ok: 6.6 GB free, 12 threads 10% busy, 0 sessions + RAM 24.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 100% busy (agents 0.0, rest 4.0 cores) · 83°C · load 9.0 + · disk 291.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:06:57Z +ludothinkpad busy: 23.5 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini busy: 28.3 GB free, 14 threads 88% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 5% busy, 0 sessions + RAM 23.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 99% busy (agents 0.0, rest 4.0 cores) · 82°C · load 10.1 + · disk 309.2 GB free · battery 96% · Ludovic: away + +2026-10-18T20:08:01Z +ludothinkpad busy: 23.4 GB free, 4 threads 88% busy, 0 sessions +desktop offline +macmini ok: 29.6 GB free, 14 threads 83% busy, 1 sessions +macbook ok: 6.6 GB free, 12 threads 2% busy, 0 sessions + RAM 23.4 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 77% busy (agents 0.0, rest 3.4 cores) · 84°C · load 7.5 + · disk 309.2 GB free · battery 96% · Ludovic: away + +2026-10-18T20:09:05Z +ludothinkpad busy: 24.0 GB free, 4 threads 89% busy, 0 sessions +desktop offline +macmini ok: 25.9 GB free, 14 threads 83% busy, 1 sessions +macbook ok: 7.1 GB free, 12 threads 19% busy, 0 sessions + RAM 23.9 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 91% busy (agents 0.0, rest 3.6 cores) · 82°C · load 5.9 + · disk 309.1 GB free · battery 96% · Ludovic: away + +2026-10-18T20:10:09Z +ludothinkpad busy: 24.0 GB free, 4 threads 98% busy, 0 sessions +desktop offline +macmini busy: 27.1 GB free, 14 threads 88% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 19% busy, 0 sessions + RAM 24.0 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 92% busy (agents 0.0, rest 3.7 cores) · 84°C · load 6.6 + · disk 309.1 GB free · battery 96% · Ludovic: away + +2026-10-18T20:11:14Z +ludothinkpad busy: 24.3 GB free, 4 threads 92% busy, 0 sessions +desktop offline +macmini ok: 26.3 GB free, 14 threads 84% busy, 1 sessions +macbook ok: 6.6 GB free, 12 threads 18% busy, 0 sessions + RAM 24.4 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 99% busy (agents 0.0, rest 3.7 cores) · 85°C · load 6.5 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:12:18Z +ludothinkpad busy: 24.7 GB free, 4 threads 89% busy, 0 sessions +desktop offline +macmini ok: 28.6 GB free, 14 threads 78% busy, 1 sessions +macbook ok: 7.3 GB free, 12 threads 20% busy, 0 sessions + RAM 24.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 82% busy (agents 0.0, rest 3.4 cores) · 84°C · load 6.1 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:13:22Z +ludothinkpad ok: 23.4 GB free, 4 threads 53% busy, 0 sessions +desktop offline +macmini ok: 28.1 GB free, 14 threads 77% busy, 1 sessions +macbook ok: 7.3 GB free, 12 threads 20% busy, 0 sessions + RAM 23.4 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 48% busy (agents 0.0, rest 2.4 cores) · 80°C · load 4.2 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:14:27Z +ludothinkpad ok: 24.0 GB free, 4 threads 44% busy, 0 sessions +desktop offline +macmini ok: 28.0 GB free, 14 threads 71% busy, 1 sessions +macbook ok: 6.9 GB free, 12 threads 2% busy, 0 sessions + RAM 24.1 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 23% busy (agents 0.0, rest 2.0 cores) · 77°C · load 4.3 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:15:31Z +ludothinkpad ok: 22.7 GB free, 4 threads 31% busy, 0 sessions +desktop offline +macmini busy: 27.2 GB free, 14 threads 86% busy, 1 sessions +macbook ok: 6.9 GB free, 12 threads 19% busy, 0 sessions + RAM 22.5 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 30% busy (agents 0.0, rest 1.3 cores) · 77°C · load 2.6 + · disk 309.1 GB free · battery 96% · Ludovic: away + +2026-10-18T20:16:35Z +ludothinkpad ok: 26.8 GB free, 4 threads 37% busy, 0 sessions +desktop offline +macmini ok: 28.6 GB free, 14 threads 82% busy, 1 sessions +macbook ok: 6.8 GB free, 12 threads 9% busy, 0 sessions + RAM 26.8 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 42% busy (agents 0.0, rest 1.7 cores) · 81°C · load 2.3 + · disk 309.1 GB free · battery 96% · Ludovic: away + +2026-10-18T20:17:39Z +ludothinkpad ok: 26.3 GB free, 4 threads 63% busy, 0 sessions +desktop offline +macmini ok: 26.8 GB free, 14 threads 78% busy, 1 sessions +macbook ok: 7.0 GB free, 12 threads 19% busy, 0 sessions + RAM 26.4 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 64% busy (agents 0.0, rest 2.6 cores) · 87°C · load 1.9 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:18:43Z +ludothinkpad ok: 26.9 GB free, 4 threads 37% busy, 0 sessions +desktop offline +macmini busy: 32.2 GB free, 14 threads 87% busy, 1 sessions +macbook ok: 7.0 GB free, 12 threads 10% busy, 0 sessions + RAM 26.9 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 56% busy (agents 0.0, rest 1.9 cores) · 84°C · load 3.9 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:19:48Z +ludothinkpad ok: 26.0 GB free, 4 threads 77% busy, 0 sessions +desktop offline +macmini ok: 29.2 GB free, 14 threads 83% busy, 1 sessions +macbook ok: 7.0 GB free, 12 threads 10% busy, 0 sessions + RAM 26.0 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 66% busy (agents 0.0, rest 2.9 cores) · 86°C · load 3.2 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:20:52Z +ludothinkpad ok: 25.9 GB free, 4 threads 80% busy, 0 sessions +desktop offline +macmini ok: 26.8 GB free, 14 threads 84% busy, 1 sessions +macbook ok: 7.0 GB free, 12 threads 17% busy, 0 sessions + RAM 25.9 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 68% busy (agents 0.0, rest 3.2 cores) · 86°C · load 4.4 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:21:56Z +ludothinkpad ok: 26.3 GB free, 4 threads 63% busy, 0 sessions +desktop offline +macmini ok: 26.3 GB free, 14 threads 77% busy, 1 sessions +macbook ok: 6.8 GB free, 12 threads 14% busy, 0 sessions + RAM 26.3 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 66% busy (agents 0.0, rest 2.8 cores) · 84°C · load 3.1 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:23:00Z +ludothinkpad ok: 26.8 GB free, 4 threads 74% busy, 0 sessions +desktop offline +macmini ok: 26.1 GB free, 14 threads 82% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 11% busy, 0 sessions + RAM 26.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 65% busy (agents 0.0, rest 2.8 cores) · 84°C · load 5.0 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:24:05Z +ludothinkpad ok: 26.9 GB free, 4 threads 64% busy, 0 sessions +desktop offline +macmini busy: 28.3 GB free, 14 threads 90% busy, 1 sessions +macbook ok: 6.8 GB free, 12 threads 8% busy, 0 sessions + RAM 26.9 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 64% busy (agents 0.0, rest 2.7 cores) · 86°C · load 4.9 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:25:09Z +ludothinkpad ok: 26.0 GB free, 4 threads 56% busy, 0 sessions +desktop offline +macmini busy: 26.2 GB free, 14 threads 88% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 12% busy, 0 sessions + RAM 26.0 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 49% busy (agents 0.0, rest 2.1 cores) · 72°C · load 5.5 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:26:13Z +ludothinkpad ok: 25.8 GB free, 4 threads 47% busy, 0 sessions +desktop offline +macmini ok: 27.8 GB free, 14 threads 78% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 8% busy, 0 sessions + RAM 25.8 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 56% busy (agents 0.0, rest 2.1 cores) · 78°C · load 3.1 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:27:18Z +ludothinkpad busy: 25.5 GB free, 4 threads 94% busy, 0 sessions +desktop offline +macmini ok: 26.6 GB free, 14 threads 82% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 17% busy, 0 sessions + RAM 25.4 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 100% busy (agents 0.0, rest 3.9 cores) · 78°C · load 4.7 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:28:22Z +ludothinkpad busy: 24.6 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini ok: 26.4 GB free, 14 threads 80% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 8% busy, 0 sessions + RAM 24.2 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 99% busy (agents 0.0, rest 4.0 cores) · 77°C · load 10.5 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:29:27Z +ludothinkpad busy: 24.3 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini ok: 29.8 GB free, 14 threads 80% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 8% busy, 0 sessions + RAM 24.0 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 99% busy (agents 0.0, rest 4.0 cores) · 82°C · load 10.0 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:30:31Z +ludothinkpad busy: 23.7 GB free, 4 threads 91% busy, 0 sessions +desktop offline +macmini ok: 30.1 GB free, 14 threads 71% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 9% busy, 0 sessions + RAM 23.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 94% busy (agents 0.0, rest 3.8 cores) · 80°C · load 10.3 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:31:36Z +ludothinkpad busy: 23.6 GB free, 4 threads 93% busy, 0 sessions +desktop offline +macmini ok: 28.0 GB free, 14 threads 81% busy, 1 sessions +macbook ok: 6.6 GB free, 12 threads 14% busy, 0 sessions + RAM 23.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 95% busy (agents 0.0, rest 3.7 cores) · 80°C · load 10.7 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:32:40Z +ludothinkpad busy: 23.6 GB free, 4 threads 94% busy, 0 sessions +desktop offline +macmini ok: 26.9 GB free, 14 threads 76% busy, 1 sessions +macbook ok: 6.8 GB free, 12 threads 22% busy, 0 sessions + RAM 23.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 84% busy (agents 0.0, rest 3.5 cores) · 80°C · load 9.7 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:33:45Z +ludothinkpad ok: 25.2 GB free, 4 threads 51% busy, 0 sessions +desktop offline +macmini ok: 27.1 GB free, 14 threads 82% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 17% busy, 0 sessions + RAM 25.2 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 48% busy (agents 0.0, rest 2.1 cores) · 87°C · load 6.5 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:34:49Z +ludothinkpad ok: 25.1 GB free, 4 threads 56% busy, 0 sessions +desktop offline +macmini busy: 28.2 GB free, 14 threads 87% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 17% busy, 0 sessions + RAM 24.5 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 78% busy (agents 0.0, rest 2.7 cores) · 88°C · load 4.0 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:35:53Z +ludothinkpad busy: 23.6 GB free, 4 threads 86% busy, 0 sessions +desktop offline +macmini busy: 27.9 GB free, 14 threads 86% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 17% busy, 0 sessions + RAM 23.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 88% busy (agents 0.0, rest 3.3 cores) · 85°C · load 4.9 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:36:58Z +ludothinkpad ok: 23.6 GB free, 4 threads 75% busy, 0 sessions +desktop offline +macmini ok: 29.4 GB free, 14 threads 84% busy, 1 sessions +macbook ok: 6.5 GB free, 12 threads 18% busy, 0 sessions + RAM 23.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 85% busy (agents 0.0, rest 3.2 cores) · 85°C · load 5.1 + · disk 309.0 GB free · battery 96% · Ludovic: away + +2026-10-18T20:38:02Z +ludothinkpad ok: 22.6 GB free, 4 threads 55% busy, 0 sessions +desktop offline +macmini ok: 26.5 GB free, 14 threads 80% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 10% busy, 0 sessions + RAM 22.3 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 54% busy (agents 0.0, rest 2.2 cores) · 85°C · load 4.5 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:39:06Z +ludothinkpad ok: 27.2 GB free, 4 threads 55% busy, 0 sessions +desktop offline +macmini busy: 29.5 GB free, 14 threads 85% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 9% busy, 0 sessions + RAM 27.3 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 48% busy (agents 0.0, rest 2.2 cores) · 87°C · load 4.0 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:40:10Z +ludothinkpad ok: 26.2 GB free, 4 threads 72% busy, 0 sessions +desktop offline +macmini ok: 27.7 GB free, 14 threads 84% busy, 1 sessions +macbook ok: 6.6 GB free, 12 threads 19% busy, 0 sessions + RAM 26.3 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 80% busy (agents 0.0, rest 3.1 cores) · 86°C · load 10.8 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:41:15Z +ludothinkpad busy: 26.4 GB free, 4 threads 86% busy, 0 sessions +desktop offline +macmini ok: 28.5 GB free, 14 threads 79% busy, 1 sessions +macbook ok: 6.6 GB free, 12 threads 10% busy, 0 sessions + RAM 26.4 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 90% busy (agents 0.0, rest 3.4 cores) · 86°C · load 12.8 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:42:19Z +ludothinkpad ok: 26.2 GB free, 4 threads 82% busy, 0 sessions +desktop offline +macmini ok: 26.9 GB free, 14 threads 78% busy, 1 sessions +macbook ok: 6.6 GB free, 12 threads 11% busy, 0 sessions + RAM 26.2 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 74% busy (agents 0.0, rest 3.3 cores) · 85°C · load 8.8 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:43:23Z +ludothinkpad busy: 26.7 GB free, 4 threads 93% busy, 0 sessions +desktop offline +macmini ok: 27.3 GB free, 14 threads 81% busy, 1 sessions +macbook ok: 6.6 GB free, 12 threads 10% busy, 0 sessions + RAM 26.7 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 88% busy (agents 0.0, rest 3.4 cores) · 85°C · load 5.4 + · disk 308.9 GB free · battery 96% · Ludovic: away + +2026-10-18T20:44:27Z +ludothinkpad ok: 27.0 GB free, 4 threads 82% busy, 0 sessions +desktop offline +macmini busy: 26.9 GB free, 14 threads 87% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 13% busy, 0 sessions + RAM 27.0 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 75% busy (agents 0.0, rest 3.3 cores) · 82°C · load 9.8 + · disk 308.8 GB free · battery 96% · Ludovic: away + +2026-10-18T20:45:32Z +ludothinkpad ok: 27.2 GB free, 4 threads 76% busy, 0 sessions +desktop offline +macmini ok: 26.8 GB free, 14 threads 80% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 10% busy, 0 sessions + RAM 27.2 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 84% busy (agents 0.0, rest 3.2 cores) · 85°C · load 6.9 + · disk 308.8 GB free · battery 96% · Ludovic: away + +2026-10-18T20:46:36Z +ludothinkpad busy: 27.1 GB free, 4 threads 87% busy, 0 sessions +desktop offline +macmini busy: 26.8 GB free, 14 threads 92% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 9% busy, 0 sessions + RAM 26.8 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 85% busy (agents 0.0, rest 3.2 cores) · 82°C · load 4.4 + · disk 308.8 GB free · battery 96% · Ludovic: away + +2026-10-18T20:47:40Z +ludothinkpad busy: 25.3 GB free, 4 threads 86% busy, 0 sessions +desktop offline +macmini ok: 26.5 GB free, 14 threads 72% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 9% busy, 0 sessions + RAM 25.5 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 85% busy (agents 0.0, rest 3.4 cores) · 86°C · load 3.7 + · disk 308.8 GB free · battery 96% · Ludovic: away + +2026-10-18T20:48:45Z +ludothinkpad busy: 26.1 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini ok: 28.5 GB free, 14 threads 84% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 15% busy, 0 sessions + RAM 26.1 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 98% busy (agents 0.0, rest 3.9 cores) · 80°C · load 6.5 + · disk 308.8 GB free · battery 96% · Ludovic: away + +2026-10-18T20:49:50Z +ludothinkpad ok: 26.8 GB free, 4 threads 71% busy, 0 sessions +desktop offline +macmini busy: 27.7 GB free, 14 threads 93% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 10% busy, 0 sessions + RAM 26.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 92% busy (agents 0.0, rest 3.4 cores) · 81°C · load 5.2 + · disk 308.7 GB free · battery 96% · Ludovic: away + +2026-10-18T20:50:54Z +ludothinkpad busy: 25.8 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini busy: 31.3 GB free, 14 threads 95% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 11% busy, 0 sessions + RAM 25.8 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 99% busy (agents 0.0, rest 4.0 cores) · 82°C · load 8.1 + · disk 308.7 GB free · battery 96% · Ludovic: away + +2026-10-18T20:51:59Z +ludothinkpad ok: 27.2 GB free, 4 threads 12% busy, 0 sessions +desktop offline +macmini ok: 31.2 GB free, 14 threads 76% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 8% busy, 0 sessions + RAM 27.2 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 15% busy (agents 0.0, rest 1.3 cores) · 81°C · load 7.9 + · disk 308.7 GB free · battery 96% · Ludovic: away + +2026-10-18T20:53:03Z +ludothinkpad ok: 27.3 GB free, 4 threads 42% busy, 0 sessions +desktop offline +macmini busy: 27.8 GB free, 14 threads 87% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 9% busy, 0 sessions + RAM 27.2 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 14% busy (agents 0.0, rest 0.8 cores) · 82°C · load 3.1 + · disk 308.7 GB free · battery 96% · Ludovic: away + +2026-10-18T20:54:08Z +ludothinkpad ok: 28.9 GB free, 4 threads 4% busy, 0 sessions +desktop offline +macmini ok: 29.2 GB free, 14 threads 80% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 9% busy, 0 sessions + RAM 28.9 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 2% busy (agents 0.0, rest 0.3 cores) · 82°C · load 1.3 + · disk 308.7 GB free · battery 96% · Ludovic: away + +2026-10-18T20:55:12Z +ludothinkpad ok: 27.7 GB free, 4 threads 27% busy, 0 sessions +desktop offline +macmini ok: 31.2 GB free, 14 threads 70% busy, 1 sessions +macbook ok: 6.7 GB free, 12 threads 10% busy, 0 sessions + RAM 27.6 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 26% busy (agents 0.0, rest 1.2 cores) · 69°C · load 1.0 + · disk 308.8 GB free · battery 96% · Ludovic: away + diff --git a/benchmarks/server-equal-mixed-2026-10-18/mixed-equal.json b/benchmarks/server-equal-mixed-2026-10-18/mixed-equal.json new file mode 100644 index 000000000..c494f3b24 --- /dev/null +++ b/benchmarks/server-equal-mixed-2026-10-18/mixed-equal.json @@ -0,0 +1,421 @@ +{ + "kind": "horizon-server-benchmark", + "date": "2026-10-18T19:32:00+00:00", + "machine": { + "system": "Linux", + "machine": "x86_64", + "cpus": 4, + "name": "ThinkPad, Intel Core i7-7500U, 32 GB, NixOS" + }, + "commit": "f1d5bf520986b00a5b968a84d85809fca1a039fa", + "engine": "horizon-engine: the server build of #916 (horizon-client --no-default-features) at 519ec121, as install.sh --archive installed it", + "java": [ + "openjdk 25.0.4.1 2026-08-18" + ], + "settings": { + "servers": "horizon,vanilla", + "bots": "1,10,25,50", + "behaviour": "mixed", + "join_every": 1.0, + "warmup": 300, + "seconds": 300, + "seed": 12345, + "explore_speed": 0.8, + "gamerule": "", + "heap": "4g", + "nice": 19, + "bots_via": "ssh macmini", + "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" + } + }, + "note": "server_bench.py was stopped after vanilla's 50-bot server crashed, before it wrote this file; the runs are the lines it printed, and the vanilla 50 entry is taken from that server's log.", + "runs": [ + { + "server": "horizon", + "bots": 1, + "behaviour": "mixed", + "start_seconds": 19.73, + "mspt": { + "average": 1.77, + "p50": 1.34, + "p95": 4.21, + "p99": 13.04 + }, + "mspt_worst_p99": 47.5, + "tps": 20.0, + "rss_mb": { + "mean": 686.54, + "max": 686.7 + }, + "cpu_percent": 27.92, + "chunks_per_second": 9.94, + "unique_per_second": 0.01, + "forgotten_per_second": 9.9, + "waited_ticks_per_second": 0.0, + "chunks_total": 6092, + "chunks_per_bot": { + "walk": 6092.0 + }, + "unique_chunks_per_bot": { + "walk": 699.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": 20.45, + "mspt": { + "average": 11.34, + "p50": 10.6, + "p95": 17.12, + "p99": 33.22 + }, + "mspt_worst_p99": 119.1, + "tps": 20.0, + "rss_mb": { + "mean": 3404.16, + "max": 3416.3 + }, + "cpu_percent": 35.93, + "chunks_per_second": 9.98, + "unique_per_second": 0.01, + "forgotten_per_second": 9.93, + "waited_ticks_per_second": 0.0, + "chunks_total": 6084, + "chunks_per_bot": { + "walk": 6084.0 + }, + "unique_chunks_per_bot": { + "walk": 699.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "horizon", + "bots": 10, + "behaviour": "mixed", + "start_seconds": 19.54, + "mspt": { + "average": 4.74, + "p50": 3.83, + "p95": 10.48, + "p99": 19.69 + }, + "mspt_worst_p99": 120.1, + "tps": 20.0, + "rss_mb": { + "mean": 855.26, + "max": 882.7 + }, + "cpu_percent": 29.78, + "chunks_per_second": 61.35, + "unique_per_second": 1.79, + "forgotten_per_second": 61.47, + "waited_ticks_per_second": 0.0, + "chunks_total": 40762, + "chunks_per_bot": { + "walk": 6247.67, + "explore": 1165.0, + "idle": 473.0 + }, + "unique_chunks_per_bot": { + "walk": 701.83, + "explore": 1086.0, + "idle": 473.0 + }, + "resent": 0, + "pulled_back": 4159, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "mixed", + "start_seconds": 19.95, + "mspt": { + "average": 34.3, + "p50": 32.2, + "p95": 53.72, + "p99": 89.08 + }, + "mspt_worst_p99": 375.8, + "tps": 20.0, + "rss_mb": { + "mean": 3713.5, + "max": 3754.9 + }, + "cpu_percent": 128.07, + "chunks_per_second": 61.66, + "unique_per_second": 1.93, + "forgotten_per_second": 61.71, + "waited_ticks_per_second": 0.0, + "chunks_total": 37568, + "chunks_per_bot": { + "walk": 5729.0, + "explore": 1124.0, + "idle": 473.0 + }, + "unique_chunks_per_bot": { + "walk": 701.17, + "explore": 1055.0, + "idle": 473.0 + }, + "resent": 0, + "pulled_back": 3976, + "moved_too_quickly": 20, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "mixed", + "start_seconds": 9.61, + "mspt": { + "average": 11.72, + "p50": 9.88, + "p95": 24.45, + "p99": 43.79 + }, + "mspt_worst_p99": 129.8, + "tps": 20.0, + "rss_mb": { + "mean": 947.35, + "max": 1086.1 + }, + "cpu_percent": 57.83, + "chunks_per_second": 158.57, + "unique_per_second": 5.78, + "forgotten_per_second": 158.45, + "waited_ticks_per_second": 0.0, + "chunks_total": 108126, + "chunks_per_bot": { + "walk": 6628.53, + "explore": 1266.6, + "idle": 473.0 + }, + "unique_chunks_per_bot": { + "walk": 704.67, + "explore": 1189.2, + "idle": 473.0 + }, + "resent": 0, + "pulled_back": 3496, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "mixed", + "start_seconds": 37.78, + "mspt": { + "average": 50.45, + "p50": 46.39, + "p95": 74.09, + "p99": 191.21 + }, + "mspt_worst_p99": 1388.1, + "tps": 19.82, + "rss_mb": { + "mean": 3866.18, + "max": 4745.0 + }, + "cpu_percent": 192.42, + "chunks_per_second": 157.63, + "unique_per_second": 8.25, + "forgotten_per_second": 156.12, + "waited_ticks_per_second": 0.0, + "chunks_total": 88763, + "chunks_per_bot": { + "walk": 5387.0, + "explore": 1118.6, + "idle": 473.0 + }, + "unique_chunks_per_bot": { + "walk": 699.0, + "explore": 1075.0, + "idle": 473.0 + }, + "resent": 2, + "pulled_back": 3629, + "moved_too_quickly": 296, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "mixed", + "start_seconds": 14.93, + "mspt": { + "average": 23.82, + "p50": 20.11, + "p95": 50.98, + "p99": 74.64 + }, + "mspt_worst_p99": 195.9, + "tps": 20.0, + "rss_mb": { + "mean": 1031.41, + "max": 1201.2 + }, + "cpu_percent": 98.21, + "chunks_per_second": 302.81, + "unique_per_second": 10.96, + "forgotten_per_second": 303.47, + "waited_ticks_per_second": 0.0, + "chunks_total": 212181, + "chunks_per_bot": { + "walk": 6496.53, + "explore": 1255.5, + "idle": 473.0 + }, + "unique_chunks_per_bot": { + "walk": 701.9, + "explore": 1184.7, + "idle": 473.0 + }, + "resent": 0, + "pulled_back": 7565, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 50, + "behaviour": "mixed", + "start_seconds": 11.16, + "error": "stopped by its watchdog 30 s after the last bot joined: \"A single server tick took 60.01 seconds (should be max 0.05)\" (Player.tick)" + } + ], + "work": [ + { + "behaviour": "mixed", + "bots": 1, + "servers": { + "horizon": { + "chunks_per_second": { + "value": 9.94, + "share": 0.996 + }, + "unique_per_second": { + "value": 0.01, + "share": 1.0 + }, + "waited_ticks_per_second": 0.0 + }, + "vanilla": { + "chunks_per_second": { + "value": 9.98, + "share": 1.0 + }, + "unique_per_second": { + "value": 0.01, + "share": 1.0 + }, + "waited_ticks_per_second": 0.0 + } + }, + "equal": true + }, + { + "behaviour": "mixed", + "bots": 10, + "servers": { + "horizon": { + "chunks_per_second": { + "value": 61.35, + "share": 0.995 + }, + "unique_per_second": { + "value": 1.79, + "share": 0.927 + }, + "waited_ticks_per_second": 0.0 + }, + "vanilla": { + "chunks_per_second": { + "value": 61.66, + "share": 1.0 + }, + "unique_per_second": { + "value": 1.93, + "share": 1.0 + }, + "waited_ticks_per_second": 0.0 + } + }, + "equal": true + }, + { + "behaviour": "mixed", + "bots": 25, + "servers": { + "horizon": { + "chunks_per_second": { + "value": 158.57, + "share": 1.0 + }, + "unique_per_second": { + "value": 5.78, + "share": 0.701 + }, + "waited_ticks_per_second": 0.0 + }, + "vanilla": { + "chunks_per_second": { + "value": 157.63, + "share": 0.994 + }, + "unique_per_second": { + "value": 8.25, + "share": 1.0 + }, + "waited_ticks_per_second": 0.0 + } + }, + "equal": false + } + ] +} diff --git a/benchmarks/server-equal-work-2026-10-17/README.md b/benchmarks/server-equal-work-2026-10-17/README.md new file mode 100644 index 000000000..737b05858 --- /dev/null +++ b/benchmarks/server-equal-work-2026-10-17/README.md @@ -0,0 +1,168 @@ +# Horizon's server against vanilla 26.3, equal work (2026-10-17) + +The same bots on Horizon's dedicated server and on vanilla 26.3's, one server at a time, on the same +machine, with the work each server does made the same before the numbers are compared. This replaces the +first run in docs/server.md section 9, where the work wasn't equal. + +## What was made equal + +The first run (PR #556) had three differences that made the comparison unfair. Each is fixed in the code +that ran here: + +- **Refused moves.** Vanilla pulls a player back when a move is too fast ("moved too quickly") or ends + inside a block ("moved wrongly"). Horizon now runs both checks the way vanilla does (PR #586), so a bot + that flies too fast is pulled back on both servers. +- **Chunk sending.** Vanilla paces chunks by the client's acknowledgements (PlayerChunkSender: one + unacknowledged batch at first, then ten, each at most the client's desired chunks per tick). Horizon used + to send twice as many per batch; it now paces them the same way (PR #586), and the bots acknowledge + batches and ask for a rate the way vanilla's client does, so both servers send at the pace the client + asks for. Chunks sent again while the bot still held + them (`resent`) are counted: 0 on both servers in every run. +- **Movement gated on chunks.** A bot doesn't move while the chunk it stands in hasn't arrived (vanilla's + client waits the same way), so a slower server isn't asked to teleport bots into terrain it hasn't sent. + +One difference isn't in Horizon yet: vanilla 26.3's locator bar (waypoints), which costs vanilla O(players²) +work per move. Every vanilla run here sets `gamerule locator_bar false`, so neither server does it. + +## Method + +`tools/server_bench.py` runs each configuration on a fresh server: + +- the same `server.properties` on both (seed 12345, view and simulation distance 10, offline, creative, + difficulty easy, `sync-chunk-writes=true`), and `gamerule respawn_radius 0`; +- the server waits for `Done`, the bots join one a second, a 60-second warm-up, then 300 seconds of + measurement; +- MSPT (average, P50, P95, P99) comes from each server's own `/tick query`, asked every 5 seconds on the + console, so both measure themselves the same way (vanilla's ServerTickRateManager numbers, which Horizon + reproduces); TPS is 20 while the average fits in 50 ms, else 1000 / MSPT; +- RSS and CPU are the server's whole process tree, once a second (100 % is one core); +- chunk counts come from the bots: chunks received per second, chunks no bot had before (new terrain), and + pull-backs (position packets that move a bot back). + +The bots are `horizon-bots` (creative, flying 24 blocks above spawn, 20 moves a second): + +- `walk`: flies to random points within 48 blocks of spawn at sprint speed (5.6 blocks a second); +- `explore`: flies straight outwards, by default at about 22 blocks a second, as fast as the server sends + chunks. + +Machine: the ThinkPad (Intel Core i7-7500U, 2 cores / 4 threads, 32 GB, NixOS), servers under `nice -n 19` +because it also runs other services; vanilla on OpenJDK 25.0.4.1 with a 4 GB heap (`-Xms4g -Xmx4g`). The +bots ran on the Mac mini and reached the ThinkPad over the tailnet, so they took none of the server's CPU. + +What ran: Horizon from `origin/main` at bf851bfe with #586 (move checks and chunk pacing), the bots and +`server_bench.py` from this PR, #623 (sections encoded once, its first commit) and #649 (a tick's packets +handed to each connection together). The ThinkPad copy wasn't a git checkout, so the JSON reports have an +empty `commit`. Vanilla: the pinned 26.3 server jar. + +## Results + +MSPT is the average milliseconds per tick (50 is the limit for 20 TPS); P95 is the 95th percentile. CPU is +the whole process tree. "New chunks/s" are chunks no bot had before. + +### Walk (the same work on both servers) + +| Bots | Server | MSPT | P95 | TPS | RSS | CPU | Chunks/s | New chunks/s | +|---:|---|---:|---:|---:|---:|---:|---:|---:| +| 1 | Horizon | 5.5 | 12.1 | 20.0 | 1.37 GB | 57 % | 9 | 0.3 | +| 1 | Vanilla | 18.3 | 27.3 | 20.0 | 3.35 GB | 66 % | 10 | 0.3 | +| 10 | Horizon | 11.5 | 22.1 | 20.0 | 1.41 GB | 124 % | 94 | 2.4 | +| 10 | Vanilla | 16.8 | 30.5 | 20.0 | 3.41 GB | 84 % | 100 | 2.8 | +| 25 | Horizon | 20.7 | 40.4 | 20.0 | 1.45 GB | 170 % | 236 | 5.7 | +| 25 | Vanilla | 16.0 | 26.4 | 20.0 | 3.47 GB | 115 % | 252 | 6.7 | +| 50 | Horizon | 31.4 | 79.0 | 20.0 | 1.52 GB | 173 % | 451 | 10.3 | +| 50 | Vanilla | 20.5 | 29.7 | 20.0 | 3.55 GB | 194 % | 502 | 12.2 | +| 100 | Horizon | 114.5 | 256.2 | 8.7 | 1.33 GB | 181 % | 484 | 30.8 | +| 100 | Vanilla | stopped by its watchdog | | | | | | | + +Both servers sent the same chunks (about 10 a second per bot, the churn of flying around) and sent none +twice. Vanilla pulled back a few bots for moving too quickly when a tick ran long (5 to 33 per run); +Horizon none. At 100 bots vanilla's watchdog stopped the server (a tick ran over 60 s, in +`ChunkMap$TrackedEntity.updatePlayer`); Horizon kept running at 8.7 TPS. + +### Explore at a rate both servers keep up with (equal work) + +The bots fly outwards at 0.8 blocks a second (`--explore-speed 0.8`), slow enough that neither server makes +them wait for terrain, so both generate and send the same new chunks. + +| Bots | Server | MSPT | P95 | TPS | RSS | CPU | Chunks/s | New chunks/s | +|---:|---|---:|---:|---:|---:|---:|---:|---:| +| 1 | Horizon | 4.0 | 6.6 | 20.0 | 1.36 GB | 25 % | 2 | 1.3 | +| 1 | Vanilla | 18.1 | 27.1 | 20.0 | 3.32 GB | 73 % | 2 | 1.4 | +| 10 | Horizon | 13.5 | 24.8 | 20.0 | 1.64 GB | 89 % | 13 | 11.8 | +| 10 | Vanilla | 29.2 | 43.0 | 20.0 | 3.52 GB | 143 % | 13 | 11.9 | +| 25 | Horizon | 20.2 | 36.8 | 20.0 | 1.74 GB | 121 % | 32 | 28.9 | +| 25 | Vanilla | 37.0 | 49.4 | 20.0 | 3.73 GB | 169 % | 32 | 28.9 | + +### Explore as fast as each server sends chunks (not equal work) + +The default 22 blocks a second is faster than either server can generate, so each server sets the pace and +a faster server is given more work: from 10 bots Horizon generated 1.5 to 2 times vanilla's new chunks a +second. Read it as throughput, not as a like-for-like tick time. + +| Bots | Server | MSPT | P95 | TPS | RSS | CPU | Chunks/s | New chunks/s | +|---:|---|---:|---:|---:|---:|---:|---:|---:| +| 1 | Horizon | 10.6 | 24.6 | 20.0 | 1.34 GB | 322 % | 39 | 34.6 | +| 1 | Vanilla | 9.9 | 21.3 | 20.0 | 3.48 GB | 245 % | 36 | 33.4 | +| 10 | Horizon | 30.4 | 52.3 | 20.0 | 2.18 GB | 345 % | 47 | 46.7 | +| 10 | Vanilla | 17.0 | 29.6 | 20.0 | 4.04 GB | 242 % | 22 | 22.4 | +| 25 | Horizon | 50.7 | 89.3 | 19.7 | 2.30 GB | 286 % | 50 | 49.7 | +| 25 | Vanilla | 24.2 | 36.5 | 20.0 | 4.40 GB | 320 % | 34 | 33.7 | +| 50 | Horizon | 50.7 | 102.7 | 19.7 | 1.72 GB | 213 % | 97 | 96.5 | +| 50 | Vanilla | stopped by its watchdog | | | | | | | +| 100 | Horizon | 92.9 | 139.6 | 10.8 | 1.70 GB | 322 % | 114 | 113.6 | +| 100 | Vanilla | stopped by its watchdog | | | | | | | + +Vanilla's watchdog stopped it at 50 bots (in `ClientboundLightUpdatePacketData`, copying light for a chunk +packet) and at 100 (in `Entity.move` from a move packet). + +## Findings + +- Horizon's RSS is 1.9 to 2.6 times smaller in every run, and it starts in 11 s against vanilla's 15 s. + Vanilla's heap is fixed at 4 GB (`-Xms4g`), so part of its RSS is that setting rather than need. +- With 1 bot Horizon's tick is 3.3 times shorter walking and 4.5 times shorter exploring at equal work; + at 10 bots it is still shorter in both. +- At equal exploring work Horizon's tick is about half of vanilla's at 10 and 25 bots. +- When the bots only fly around spawn (`walk`), vanilla's tick hardly grows with players (16 to 20 ms from + 1 to 50 bots) while Horizon's grows by about 0.5 ms per bot, so vanilla is ahead from about 20 bots: 16.0 + against 20.7 ms at 25 and 20.5 against 31.4 ms at 50. Both keep 20 TPS up to 50. +- With 100 bots vanilla's watchdog stops it in every pass, and Horizon keeps running at 9 to 11 TPS. + +### Where Horizon's walk tick goes at 25 bots + +Profiled on the ThinkPad with `perf` (the server thread, 20 s, self time): + +| Share | What | Whose | +|---:|---|---| +| 18 % | `Streaming::update`: every tick it walks all loaded columns and proto chunks and tests each against every player's area (unloading, dropping protos, finishing and decoration candidates), so the cost grows with players times chunks | streaming / engine tick | +| 20 % | `PalettedContainer::get` and `World::block`, mostly from block entity ticks: on the Mac profile `tick_hoppers` looks up the block under every block entity in the loaded world each tick and compares names as strings, and spawners rebuild their display id | engine tick | +| 8 % | `light_data` when a chunk is sent: the light layers were built twice per chunk; a follow-up commit on #623 builds them once and keeps each section's packed layers | server (this work) | +| 7 % | encoding chunk sections and heightmaps for the chunks each bot newly sees | server | +| 4 % | `Hub::tick` (moves, tracking) | server | +| 2 % | the moved-wrongly collision check | server | + +The first two grow with the number of players but aren't specific to the server: they are the +engine's tick and streaming, reported to perf-server-2. Before #623 and #649, encoding every chunk section +once per player and waking each connection's writer thread once per packet cost more than everything above +(at 50 walking bots the tick was 281 ms; see the earlier runs). + +## Earlier runs (same machine and method) + +- `run-1-before-chunk-cache.json`: before #623, with vanilla's locator bar on. Horizon's walk tick was + 27.5, 40.7, 281 and 467 ms at 10, 25, 50 and 100 bots, and vanilla's 33.2 ms at 50 (the locator bar). +- `run-2-chunk-cache.json`: Horizon with #623 only: 13.2, 23.5, 31.4 and 84.4 ms walking at 10 to 100 + bots. + +## Files + +- `horizon.json`, `vanilla.json`: the walk and explore passes (vanilla with `locator_bar=false`). +- `explore-equal-rate.json`: the explore pass at 0.8 blocks a second, both servers. +- `run-1-before-chunk-cache.json`, `run-2-chunk-cache.json`: the earlier runs. + +To run it again (bots on another machine reached at `--connect`): + + cargo build --release -p horizon-client --bin horizon-client -p horizon-server -p horizon-bots + python3 tools/server_bench.py --servers horizon,vanilla --bots 1,10,25,50,100 --behaviour walk,explore \ + --gamerule locator_bar=false --nice 19 --bots-via 'ssh macmini' --bots-bin /path/to/horizon-bots \ + --connect --out results.json + python3 tools/server_bench.py --servers horizon,vanilla --bots 1,10,25 --behaviour explore \ + --explore-speed 0.8 --gamerule locator_bar=false --nice 19 ... --out explore-equal-rate.json diff --git a/benchmarks/server-equal-work-2026-10-17/explore-equal-rate.json b/benchmarks/server-equal-work-2026-10-17/explore-equal-rate.json new file mode 100644 index 000000000..bd097e43a --- /dev/null +++ b/benchmarks/server-equal-work-2026-10-17/explore-equal-rate.json @@ -0,0 +1,267 @@ +{ + "kind": "horizon-server-benchmark", + "date": "2026-10-17T05:47:12+00:00", + "machine": { + "system": "Linux", + "machine": "x86_64", + "cpus": 4 + }, + "commit": "", + "java": [ + "openjdk 25.0.4.1 2026-08-18" + ], + "settings": { + "servers": "horizon,vanilla", + "bots": "1,10,25", + "behaviour": "explore", + "join_every": 1.0, + "warmup": 60, + "seconds": 300, + "seed": 12345, + "explore_speed": 0.8, + "gamerule": "locator_bar=false", + "heap": "4g", + "out": "/home/ludovic/scratch/sh2-bench5.json", + "nice": 19, + "bots_via": "ssh -o BatchMode=yes macmini", + "bots_bin": "/tmp/sh2-horizon-bots", + "connect": "100.72.51.8", + "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": 1, + "behaviour": "explore", + "start_seconds": 10.91, + "mspt": { + "average": 4.02, + "p50": 3.75, + "p95": 6.63, + "p99": 14.55 + }, + "mspt_worst_p99": 63.3, + "tps": 20.0, + "rss_mb": { + "mean": 1392.42, + "max": 1746.1 + }, + "cpu_percent": 24.52, + "chunks_per_second": 1.69, + "unique_per_second": 1.34, + "forgotten_per_second": 1.62, + "waited_ticks_per_second": 0.0, + "chunks_total": 1048, + "chunks_per_bot": { + "explore": 1048.0 + }, + "unique_chunks_per_bot": { + "explore": 952.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": "explore", + "start_seconds": 15.13, + "mspt": { + "average": 18.12, + "p50": 18.78, + "p95": 27.07, + "p99": 60.81 + }, + "mspt_worst_p99": 1397.6, + "tps": 20.0, + "rss_mb": { + "mean": 3397.31, + "max": 3430.2 + }, + "cpu_percent": 72.99, + "chunks_per_second": 1.69, + "unique_per_second": 1.38, + "forgotten_per_second": 1.62, + "waited_ticks_per_second": 0.0, + "chunks_total": 1048, + "chunks_per_bot": { + "explore": 1048.0 + }, + "unique_chunks_per_bot": { + "explore": 952.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": "explore", + "start_seconds": 11.12, + "mspt": { + "average": 13.49, + "p50": 12.09, + "p95": 24.81, + "p99": 40.9 + }, + "mspt_worst_p99": 101.4, + "tps": 20.0, + "rss_mb": { + "mean": 1684.04, + "max": 2660.6 + }, + "cpu_percent": 89.04, + "chunks_per_second": 13.08, + "unique_per_second": 11.84, + "forgotten_per_second": 13.02, + "waited_ticks_per_second": 0.0, + "chunks_total": 9277, + "chunks_per_bot": { + "explore": 927.7 + }, + "unique_chunks_per_bot": { + "explore": 889.9 + }, + "resent": 0, + "pulled_back": 3381, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "explore", + "start_seconds": 14.84, + "mspt": { + "average": 29.18, + "p50": 26.76, + "p95": 43.02, + "p99": 80.16 + }, + "mspt_worst_p99": 425.5, + "tps": 20.0, + "rss_mb": { + "mean": 3609.51, + "max": 3791.6 + }, + "cpu_percent": 142.79, + "chunks_per_second": 13.1, + "unique_per_second": 11.88, + "forgotten_per_second": 13.12, + "waited_ticks_per_second": 0.0, + "chunks_total": 9245, + "chunks_per_bot": { + "explore": 924.5 + }, + "unique_chunks_per_bot": { + "explore": 885.3 + }, + "resent": 0, + "pulled_back": 3388, + "moved_too_quickly": 6, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "explore", + "start_seconds": 11.02, + "mspt": { + "average": 20.25, + "p50": 18.73, + "p95": 36.78, + "p99": 55.76 + }, + "mspt_worst_p99": 92.3, + "tps": 20.0, + "rss_mb": { + "mean": 1782.26, + "max": 2741.9 + }, + "cpu_percent": 120.8, + "chunks_per_second": 32.22, + "unique_per_second": 28.89, + "forgotten_per_second": 32.19, + "waited_ticks_per_second": 0.0, + "chunks_total": 23357, + "chunks_per_bot": { + "explore": 934.28 + }, + "unique_chunks_per_bot": { + "explore": 890.32 + }, + "resent": 0, + "pulled_back": 11580, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "explore", + "start_seconds": 14.54, + "mspt": { + "average": 37.04, + "p50": 34.77, + "p95": 49.42, + "p99": 101.67 + }, + "mspt_worst_p99": 735.2, + "tps": 20.0, + "rss_mb": { + "mean": 3815.46, + "max": 4202.0 + }, + "cpu_percent": 169.15, + "chunks_per_second": 32.34, + "unique_per_second": 28.93, + "forgotten_per_second": 32.31, + "waited_ticks_per_second": 0.0, + "chunks_total": 23306, + "chunks_per_bot": { + "explore": 932.24 + }, + "unique_chunks_per_bot": { + "explore": 886.96 + }, + "resent": 0, + "pulled_back": 11539, + "moved_too_quickly": 6, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 59 + } + ] +} diff --git a/benchmarks/server-equal-work-2026-10-17/horizon.json b/benchmarks/server-equal-work-2026-10-17/horizon.json new file mode 100644 index 000000000..d0b162c44 --- /dev/null +++ b/benchmarks/server-equal-work-2026-10-17/horizon.json @@ -0,0 +1,415 @@ +{ + "kind": "horizon-server-benchmark", + "date": "2026-10-17T05:04:57+00:00", + "machine": { + "system": "Linux", + "machine": "x86_64", + "cpus": 4 + }, + "commit": "", + "java": [ + "openjdk 25.0.4.1 2026-08-18" + ], + "settings": { + "servers": "horizon", + "bots": "1,10,25,50,100", + "behaviour": "walk,explore", + "join_every": 1.0, + "warmup": 60, + "seconds": 300, + "seed": 12345, + "explore_speed": 0, + "gamerule": "locator_bar=false", + "heap": "4g", + "out": "/home/ludovic/scratch/sh2-bench4.json", + "nice": 19, + "bots_via": "ssh -o BatchMode=yes macmini", + "bots_bin": "/tmp/sh2-horizon-bots", + "connect": "100.72.51.8", + "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": 1, + "behaviour": "walk", + "start_seconds": 11.02, + "mspt": { + "average": 5.45, + "p50": 4.63, + "p95": 12.14, + "p99": 30.63 + }, + "mspt_worst_p99": 91.5, + "tps": 20.0, + "rss_mb": { + "mean": 1398.5, + "max": 1708.4 + }, + "cpu_percent": 57.03, + "chunks_per_second": 9.17, + "unique_per_second": 0.26, + "forgotten_per_second": 9.2, + "waited_ticks_per_second": 0.0, + "chunks_total": 3708, + "chunks_per_bot": { + "walk": 3708.0 + }, + "unique_chunks_per_bot": { + "walk": 694.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": "walk", + "start_seconds": 11.02, + "mspt": { + "average": 11.51, + "p50": 10.6, + "p95": 22.13, + "p99": 36.92 + }, + "mspt_worst_p99": 71.9, + "tps": 20.0, + "rss_mb": { + "mean": 1440.2, + "max": 1832.4 + }, + "cpu_percent": 124.19, + "chunks_per_second": 94.04, + "unique_per_second": 2.38, + "forgotten_per_second": 93.95, + "waited_ticks_per_second": 0.0, + "chunks_total": 37269, + "chunks_per_bot": { + "walk": 3726.9 + }, + "unique_chunks_per_bot": { + "walk": 688.9 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "walk", + "start_seconds": 10.91, + "mspt": { + "average": 20.74, + "p50": 19.27, + "p95": 40.42, + "p99": 57.12 + }, + "mspt_worst_p99": 102.0, + "tps": 20.0, + "rss_mb": { + "mean": 1485.4, + "max": 1871.9 + }, + "cpu_percent": 169.52, + "chunks_per_second": 235.81, + "unique_per_second": 5.68, + "forgotten_per_second": 235.83, + "waited_ticks_per_second": 0.0, + "chunks_total": 94456, + "chunks_per_bot": { + "walk": 3778.24 + }, + "unique_chunks_per_bot": { + "walk": 692.96 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "walk", + "start_seconds": 11.12, + "mspt": { + "average": 31.41, + "p50": 25.39, + "p95": 78.97, + "p99": 110.73 + }, + "mspt_worst_p99": 192.9, + "tps": 20.0, + "rss_mb": { + "mean": 1558.34, + "max": 1915.3 + }, + "cpu_percent": 173.27, + "chunks_per_second": 450.85, + "unique_per_second": 10.3, + "forgotten_per_second": 451.86, + "waited_ticks_per_second": 0.0, + "chunks_total": 186885, + "chunks_per_bot": { + "walk": 3737.7 + }, + "unique_chunks_per_bot": { + "walk": 688.9 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 100, + "behaviour": "walk", + "start_seconds": 18.16, + "mspt": { + "average": 114.55, + "p50": 101.37, + "p95": 256.19, + "p99": 338.64 + }, + "mspt_worst_p99": 442.6, + "tps": 8.73, + "rss_mb": { + "mean": 1357.65, + "max": 1358.4 + }, + "cpu_percent": 181.21, + "chunks_per_second": 484.16, + "unique_per_second": 30.76, + "forgotten_per_second": 478.77, + "waited_ticks_per_second": 0.0, + "chunks_total": 212947, + "chunks_per_bot": { + "walk": 2129.47 + }, + "unique_chunks_per_bot": { + "walk": 583.02 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 100, + "bots_failed": [], + "tick_queries": 57 + }, + { + "server": "horizon", + "bots": 1, + "behaviour": "explore", + "start_seconds": 17.45, + "mspt": { + "average": 10.62, + "p50": 9.2, + "p95": 24.61, + "p99": 38.18 + }, + "mspt_worst_p99": 81.1, + "tps": 20.0, + "rss_mb": { + "mean": 1372.73, + "max": 1568.7 + }, + "cpu_percent": 322.38, + "chunks_per_second": 39.21, + "unique_per_second": 34.6, + "forgotten_per_second": 38.55, + "waited_ticks_per_second": 0.51, + "chunks_total": 12549, + "chunks_per_bot": { + "explore": 12549.0 + }, + "unique_chunks_per_bot": { + "explore": 11168.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": "explore", + "start_seconds": 11.32, + "mspt": { + "average": 30.38, + "p50": 27.84, + "p95": 52.31, + "p99": 77.89 + }, + "mspt_worst_p99": 158.3, + "tps": 20.0, + "rss_mb": { + "mean": 2231.56, + "max": 4022.6 + }, + "cpu_percent": 344.99, + "chunks_per_second": 47.34, + "unique_per_second": 46.73, + "forgotten_per_second": 44.57, + "waited_ticks_per_second": 112.27, + "chunks_total": 19870, + "chunks_per_bot": { + "explore": 1987.0 + }, + "unique_chunks_per_bot": { + "explore": 1966.5 + }, + "resent": 0, + "pulled_back": 16130, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "explore", + "start_seconds": 11.42, + "mspt": { + "average": 50.67, + "p50": 47.61, + "p95": 89.3, + "p99": 119.15 + }, + "mspt_worst_p99": 222.3, + "tps": 19.73, + "rss_mb": { + "mean": 2358.4, + "max": 4273.7 + }, + "cpu_percent": 286.32, + "chunks_per_second": 50.39, + "unique_per_second": 49.67, + "forgotten_per_second": 47.45, + "waited_ticks_per_second": 311.85, + "chunks_total": 28954, + "chunks_per_bot": { + "explore": 1158.16 + }, + "unique_chunks_per_bot": { + "explore": 1147.56 + }, + "resent": 0, + "pulled_back": 36319, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "explore", + "start_seconds": 20.06, + "mspt": { + "average": 50.69, + "p50": 47.48, + "p95": 102.67, + "p99": 133.27 + }, + "mspt_worst_p99": 274.4, + "tps": 19.73, + "rss_mb": { + "mean": 1764.01, + "max": 2542.0 + }, + "cpu_percent": 212.83, + "chunks_per_second": 97.33, + "unique_per_second": 96.47, + "forgotten_per_second": 76.74, + "waited_ticks_per_second": 750.49, + "chunks_total": 38538, + "chunks_per_bot": { + "explore": 770.76 + }, + "unique_chunks_per_bot": { + "explore": 763.84 + }, + "resent": 0, + "pulled_back": 39880, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 100, + "behaviour": "explore", + "start_seconds": 11.82, + "mspt": { + "average": 92.93, + "p50": 87.84, + "p95": 139.6, + "p99": 178.38 + }, + "mspt_worst_p99": 272.9, + "tps": 10.76, + "rss_mb": { + "mean": 1736.38, + "max": 1855.7 + }, + "cpu_percent": 321.53, + "chunks_per_second": 113.89, + "unique_per_second": 113.56, + "forgotten_per_second": 101.68, + "waited_ticks_per_second": 1179.4, + "chunks_total": 84617, + "chunks_per_bot": { + "explore": 846.17 + }, + "unique_chunks_per_bot": { + "explore": 845.01 + }, + "resent": 0, + "pulled_back": 128104, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 100, + "bots_failed": [], + "tick_queries": 58 + } + ] +} diff --git a/benchmarks/server-equal-work-2026-10-17/run-1-before-chunk-cache.json b/benchmarks/server-equal-work-2026-10-17/run-1-before-chunk-cache.json new file mode 100644 index 000000000..d79b3f179 --- /dev/null +++ b/benchmarks/server-equal-work-2026-10-17/run-1-before-chunk-cache.json @@ -0,0 +1,693 @@ +{ + "kind": "horizon-server-benchmark", + "date": "2026-10-17T00:37:35+00:00", + "machine": { + "system": "Linux", + "machine": "x86_64", + "cpus": 4 + }, + "commit": "", + "java": [ + "openjdk 25.0.4.1 2026-08-18" + ], + "settings": { + "servers": "horizon,vanilla", + "bots": "1,10,25,50,100", + "behaviour": "walk,explore", + "join_every": 1.0, + "warmup": 60, + "seconds": 300, + "seed": 12345, + "heap": "4g", + "out": "/home/ludovic/scratch/sh2-bench.json", + "nice": 19, + "bots_via": "ssh -o BatchMode=yes macmini", + "bots_bin": "/tmp/sh2-horizon-bots", + "connect": "100.72.51.8", + "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": 1, + "behaviour": "walk", + "start_seconds": 11.12, + "mspt": { + "average": 7.63, + "p50": 5.51, + "p95": 17.46, + "p99": 36.79 + }, + "mspt_worst_p99": 79.6, + "tps": 20.0, + "rss_mb": { + "mean": 1391.94, + "max": 1700.7 + }, + "cpu_percent": 60.18, + "chunks_per_second": 9.15, + "unique_per_second": 0.29, + "forgotten_per_second": 9.18, + "waited_ticks_per_second": 0.0, + "chunks_total": 3682, + "chunks_per_bot": { + "walk": 3682.0 + }, + "unique_chunks_per_bot": { + "walk": 692.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": "walk", + "start_seconds": 15.35, + "mspt": { + "average": 18.08, + "p50": 18.66, + "p95": 28.05, + "p99": 46.94 + }, + "mspt_worst_p99": 456.4, + "tps": 20.0, + "rss_mb": { + "mean": 3455.72, + "max": 3471.7 + }, + "cpu_percent": 65.67, + "chunks_per_second": 9.89, + "unique_per_second": 0.29, + "forgotten_per_second": 9.92, + "waited_ticks_per_second": 0.0, + "chunks_total": 3827, + "chunks_per_bot": { + "walk": 3827.0 + }, + "unique_chunks_per_bot": { + "walk": 699.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": "walk", + "start_seconds": 11.22, + "mspt": { + "average": 27.53, + "p50": 24.93, + "p95": 53.93, + "p99": 77.03 + }, + "mspt_worst_p99": 111.4, + "tps": 20.0, + "rss_mb": { + "mean": 1433.85, + "max": 1831.9 + }, + "cpu_percent": 138.65, + "chunks_per_second": 95.98, + "unique_per_second": 2.38, + "forgotten_per_second": 95.78, + "waited_ticks_per_second": 0.0, + "chunks_total": 38041, + "chunks_per_bot": { + "walk": 3804.1 + }, + "unique_chunks_per_bot": { + "walk": 690.8 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "walk", + "start_seconds": 14.83, + "mspt": { + "average": 18.92, + "p50": 16.02, + "p95": 39.0, + "p99": 56.13 + }, + "mspt_worst_p99": 180.3, + "tps": 20.0, + "rss_mb": { + "mean": 3479.45, + "max": 3535.7 + }, + "cpu_percent": 99.39, + "chunks_per_second": 99.93, + "unique_per_second": 2.94, + "forgotten_per_second": 100.04, + "waited_ticks_per_second": 0.0, + "chunks_total": 38411, + "chunks_per_bot": { + "walk": 3841.1 + }, + "unique_chunks_per_bot": { + "walk": 697.1 + }, + "resent": 0, + "pulled_back": 7, + "moved_too_quickly": 7, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "walk", + "start_seconds": 11.52, + "mspt": { + "average": 40.69, + "p50": 37.28, + "p95": 81.16, + "p99": 108.42 + }, + "mspt_worst_p99": 149.9, + "tps": 20.0, + "rss_mb": { + "mean": 1444.12, + "max": 1882.6 + }, + "cpu_percent": 176.94, + "chunks_per_second": 244.11, + "unique_per_second": 6.51, + "forgotten_per_second": 243.96, + "waited_ticks_per_second": 0.0, + "chunks_total": 95219, + "chunks_per_bot": { + "walk": 3808.76 + }, + "unique_chunks_per_bot": { + "walk": 695.64 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "walk", + "start_seconds": 15.23, + "mspt": { + "average": 18.81, + "p50": 17.26, + "p95": 30.16, + "p99": 53.9 + }, + "mspt_worst_p99": 390.0, + "tps": 20.0, + "rss_mb": { + "mean": 3517.36, + "max": 3555.7 + }, + "cpu_percent": 148.52, + "chunks_per_second": 252.34, + "unique_per_second": 7.75, + "forgotten_per_second": 251.95, + "waited_ticks_per_second": 0.0, + "chunks_total": 96645, + "chunks_per_bot": { + "walk": 3865.8 + }, + "unique_chunks_per_bot": { + "walk": 698.4 + }, + "resent": 0, + "pulled_back": 11, + "moved_too_quickly": 11, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "walk", + "start_seconds": 12.72, + "mspt": { + "average": 281.42, + "p50": 278.28, + "p95": 343.64, + "p99": 384.23 + }, + "mspt_worst_p99": 475.1, + "tps": 3.55, + "rss_mb": { + "mean": 1334.32, + "max": 1335.4 + }, + "cpu_percent": 197.11, + "chunks_per_second": 391.5, + "unique_per_second": 21.24, + "forgotten_per_second": 388.33, + "waited_ticks_per_second": 0.0, + "chunks_total": 153136, + "chunks_per_bot": { + "walk": 3062.72 + }, + "unique_chunks_per_bot": { + "walk": 661.82 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 57 + }, + { + "server": "vanilla", + "bots": 50, + "behaviour": "walk", + "start_seconds": 15.74, + "mspt": { + "average": 33.21, + "p50": 29.9, + "p95": 53.37, + "p99": 109.14 + }, + "mspt_worst_p99": 833.3, + "tps": 20.0, + "rss_mb": { + "mean": 3568.07, + "max": 3594.9 + }, + "cpu_percent": 266.59, + "chunks_per_second": 496.3, + "unique_per_second": 18.64, + "forgotten_per_second": 495.77, + "waited_ticks_per_second": 0.0, + "chunks_total": 191965, + "chunks_per_bot": { + "walk": 3839.3 + }, + "unique_chunks_per_bot": { + "walk": 696.22 + }, + "resent": 0, + "pulled_back": 184, + "moved_too_quickly": 183, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 100, + "behaviour": "walk", + "start_seconds": 11.22, + "mspt": { + "average": 467.37, + "p50": 464.62, + "p95": 524.07, + "p99": 548.77 + }, + "mspt_worst_p99": 581.1, + "tps": 2.14, + "rss_mb": { + "mean": 1353.68, + "max": 1354.9 + }, + "cpu_percent": 191.96, + "chunks_per_second": 377.62, + "unique_per_second": 55.8, + "forgotten_per_second": 348.88, + "waited_ticks_per_second": 0.0, + "chunks_total": 165719, + "chunks_per_bot": { + "walk": 1657.19 + }, + "unique_chunks_per_bot": { + "walk": 549.8 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 100, + "bots_failed": [], + "tick_queries": 56 + }, + { + "server": "vanilla", + "bots": 100, + "behaviour": "walk", + "start_seconds": 14.84, + "error": "[Errno 32] Broken pipe" + }, + { + "server": "horizon", + "bots": 1, + "behaviour": "explore", + "start_seconds": 11.12, + "mspt": { + "average": 17.07, + "p50": 16.6, + "p95": 32.82, + "p99": 50.3 + }, + "mspt_worst_p99": 113.9, + "tps": 20.0, + "rss_mb": { + "mean": 1391.72, + "max": 1566.6 + }, + "cpu_percent": 333.8, + "chunks_per_second": 39.42, + "unique_per_second": 34.77, + "forgotten_per_second": 39.34, + "waited_ticks_per_second": 0.0, + "chunks_total": 13860, + "chunks_per_bot": { + "explore": 13860.0 + }, + "unique_chunks_per_bot": { + "explore": 12379.0 + }, + "resent": 0, + "pulled_back": 192, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 1, + "behaviour": "explore", + "start_seconds": 16.03, + "mspt": { + "average": 9.9, + "p50": 8.04, + "p95": 20.41, + "p99": 63.62 + }, + "mspt_worst_p99": 351.1, + "tps": 20.0, + "rss_mb": { + "mean": 3709.92, + "max": 3776.2 + }, + "cpu_percent": 246.64, + "chunks_per_second": 36.06, + "unique_per_second": 33.33, + "forgotten_per_second": 35.73, + "waited_ticks_per_second": 0.0, + "chunks_total": 12369, + "chunks_per_bot": { + "explore": 12369.0 + }, + "unique_chunks_per_bot": { + "explore": 11559.0 + }, + "resent": 0, + "pulled_back": 222, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "horizon", + "bots": 10, + "behaviour": "explore", + "start_seconds": 11.42, + "mspt": { + "average": 33.72, + "p50": 31.08, + "p95": 61.07, + "p99": 89.09 + }, + "mspt_worst_p99": 142.6, + "tps": 20.0, + "rss_mb": { + "mean": 2239.97, + "max": 4108.8 + }, + "cpu_percent": 350.83, + "chunks_per_second": 46.49, + "unique_per_second": 46.08, + "forgotten_per_second": 44.33, + "waited_ticks_per_second": 111.28, + "chunks_total": 19915, + "chunks_per_bot": { + "explore": 1991.5 + }, + "unique_chunks_per_bot": { + "explore": 1974.4 + }, + "resent": 0, + "pulled_back": 17516, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "explore", + "start_seconds": 15.15, + "mspt": { + "average": 16.25, + "p50": 13.67, + "p95": 28.92, + "p99": 115.9 + }, + "mspt_worst_p99": 1202.1, + "tps": 20.0, + "rss_mb": { + "mean": 4202.72, + "max": 4502.7 + }, + "cpu_percent": 247.47, + "chunks_per_second": 22.3, + "unique_per_second": 22.25, + "forgotten_per_second": 22.64, + "waited_ticks_per_second": 78.89, + "chunks_total": 10092, + "chunks_per_bot": { + "explore": 1009.2 + }, + "unique_chunks_per_bot": { + "explore": 1007.4 + }, + "resent": 0, + "pulled_back": 16286, + "moved_too_quickly": 12, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "explore", + "start_seconds": 11.82, + "mspt": { + "average": 48.03, + "p50": 44.99, + "p95": 83.06, + "p99": 109.63 + }, + "mspt_worst_p99": 191.8, + "tps": 20.0, + "rss_mb": { + "mean": 2214.16, + "max": 4598.6 + }, + "cpu_percent": 356.03, + "chunks_per_second": 64.2, + "unique_per_second": 63.5, + "forgotten_per_second": 60.9, + "waited_ticks_per_second": 301.65, + "chunks_total": 32247, + "chunks_per_bot": { + "explore": 1289.88 + }, + "unique_chunks_per_bot": { + "explore": 1279.4 + }, + "resent": 0, + "pulled_back": 42159, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "explore", + "start_seconds": 14.73, + "mspt": { + "average": 25.73, + "p50": 22.75, + "p95": 37.72, + "p99": 201.98 + }, + "mspt_worst_p99": 4051.5, + "tps": 20.0, + "rss_mb": { + "mean": 4355.9, + "max": 4983.2 + }, + "cpu_percent": 309.3, + "chunks_per_second": 33.33, + "unique_per_second": 33.21, + "forgotten_per_second": 34.22, + "waited_ticks_per_second": 258.68, + "chunks_total": 19426, + "chunks_per_bot": { + "explore": 777.04 + }, + "unique_chunks_per_bot": { + "explore": 772.92 + }, + "resent": 0, + "pulled_back": 53918, + "moved_too_quickly": 93, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "explore", + "start_seconds": 11.01, + "mspt": { + "average": 108.61, + "p50": 107.37, + "p95": 151.96, + "p99": 177.41 + }, + "mspt_worst_p99": 271.6, + "tps": 9.21, + "rss_mb": { + "mean": 1775.98, + "max": 1956.9 + }, + "cpu_percent": 359.7, + "chunks_per_second": 100.32, + "unique_per_second": 98.91, + "forgotten_per_second": 95.42, + "waited_ticks_per_second": 518.15, + "chunks_total": 55956, + "chunks_per_bot": { + "explore": 1119.12 + }, + "unique_chunks_per_bot": { + "explore": 1111.42 + }, + "resent": 0, + "pulled_back": 69967, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 50, + "behaviour": "explore", + "start_seconds": 14.74, + "error": "[Errno 32] Broken pipe" + }, + { + "server": "horizon", + "bots": 100, + "behaviour": "explore", + "start_seconds": 11.12, + "mspt": { + "average": 244.43, + "p50": 243.58, + "p95": 308.39, + "p99": 360.12 + }, + "mspt_worst_p99": 470.2, + "tps": 4.09, + "rss_mb": { + "mean": 1806.85, + "max": 2084.5 + }, + "cpu_percent": 310.46, + "chunks_per_second": 186.87, + "unique_per_second": 182.99, + "forgotten_per_second": 161.75, + "waited_ticks_per_second": 1131.65, + "chunks_total": 100389, + "chunks_per_bot": { + "explore": 1003.89 + }, + "unique_chunks_per_bot": { + "explore": 991.9 + }, + "resent": 0, + "pulled_back": 56791, + "moved_too_quickly": 344, + "moved_wrongly": 0, + "bots_joined": 100, + "bots_failed": [], + "tick_queries": 57 + }, + { + "server": "vanilla", + "bots": 100, + "behaviour": "explore", + "start_seconds": 14.83, + "error": "[Errno 32] Broken pipe" + } + ] +} diff --git a/benchmarks/server-equal-work-2026-10-17/run-2-chunk-cache.json b/benchmarks/server-equal-work-2026-10-17/run-2-chunk-cache.json new file mode 100644 index 000000000..4c182395c --- /dev/null +++ b/benchmarks/server-equal-work-2026-10-17/run-2-chunk-cache.json @@ -0,0 +1,413 @@ +{ + "kind": "horizon-server-benchmark", + "date": "2026-10-17T02:22:59+00:00", + "machine": { + "system": "Linux", + "machine": "x86_64", + "cpus": 4 + }, + "commit": "", + "java": [ + "openjdk 25.0.4.1 2026-08-18" + ], + "settings": { + "servers": "horizon", + "bots": "1,10,25,50,100", + "behaviour": "walk,explore", + "join_every": 1.0, + "warmup": 60, + "seconds": 300, + "seed": 12345, + "heap": "4g", + "out": "/home/ludovic/scratch/sh2-bench2.json", + "nice": 19, + "bots_via": "ssh -o BatchMode=yes macmini", + "bots_bin": "/tmp/sh2-horizon-bots", + "connect": "100.72.51.8", + "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": 1, + "behaviour": "walk", + "start_seconds": 11.12, + "mspt": { + "average": 5.41, + "p50": 4.63, + "p95": 11.61, + "p99": 30.5 + }, + "mspt_worst_p99": 64.6, + "tps": 20.0, + "rss_mb": { + "mean": 1398.84, + "max": 1708.5 + }, + "cpu_percent": 56.84, + "chunks_per_second": 9.19, + "unique_per_second": 0.27, + "forgotten_per_second": 9.22, + "waited_ticks_per_second": 0.0, + "chunks_total": 3713, + "chunks_per_bot": { + "walk": 3713.0 + }, + "unique_chunks_per_bot": { + "walk": 694.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": "walk", + "start_seconds": 10.92, + "mspt": { + "average": 13.18, + "p50": 11.74, + "p95": 26.5, + "p99": 44.82 + }, + "mspt_worst_p99": 87.6, + "tps": 20.0, + "rss_mb": { + "mean": 1439.83, + "max": 1831.6 + }, + "cpu_percent": 132.94, + "chunks_per_second": 93.87, + "unique_per_second": 2.38, + "forgotten_per_second": 93.77, + "waited_ticks_per_second": 0.0, + "chunks_total": 37240, + "chunks_per_bot": { + "walk": 3724.0 + }, + "unique_chunks_per_bot": { + "walk": 688.7 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "walk", + "start_seconds": 10.92, + "mspt": { + "average": 23.52, + "p50": 21.92, + "p95": 44.7, + "p99": 63.12 + }, + "mspt_worst_p99": 119.1, + "tps": 20.0, + "rss_mb": { + "mean": 1485.82, + "max": 1872.0 + }, + "cpu_percent": 173.82, + "chunks_per_second": 235.63, + "unique_per_second": 5.64, + "forgotten_per_second": 235.72, + "waited_ticks_per_second": 0.0, + "chunks_total": 94549, + "chunks_per_bot": { + "walk": 3781.96 + }, + "unique_chunks_per_bot": { + "walk": 692.8 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "walk", + "start_seconds": 10.91, + "mspt": { + "average": 31.36, + "p50": 26.08, + "p95": 70.6, + "p99": 92.83 + }, + "mspt_worst_p99": 125.5, + "tps": 20.0, + "rss_mb": { + "mean": 1556.81, + "max": 1913.0 + }, + "cpu_percent": 192.16, + "chunks_per_second": 468.69, + "unique_per_second": 10.43, + "forgotten_per_second": 468.78, + "waited_ticks_per_second": 0.0, + "chunks_total": 193547, + "chunks_per_bot": { + "walk": 3870.94 + }, + "unique_chunks_per_bot": { + "walk": 692.84 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 100, + "behaviour": "walk", + "start_seconds": 11.02, + "mspt": { + "average": 84.38, + "p50": 81.5, + "p95": 132.92, + "p99": 168.38 + }, + "mspt_worst_p99": 245.5, + "tps": 11.85, + "rss_mb": { + "mean": 1395.64, + "max": 1941.3 + }, + "cpu_percent": 271.28, + "chunks_per_second": 850.05, + "unique_per_second": 21.79, + "forgotten_per_second": 851.1, + "waited_ticks_per_second": 0.0, + "chunks_total": 368464, + "chunks_per_bot": { + "walk": 3684.64 + }, + "unique_chunks_per_bot": { + "walk": 680.58 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 100, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 1, + "behaviour": "explore", + "start_seconds": 11.02, + "mspt": { + "average": 11.63, + "p50": 10.27, + "p95": 24.52, + "p99": 40.53 + }, + "mspt_worst_p99": 99.9, + "tps": 20.0, + "rss_mb": { + "mean": 1413.81, + "max": 1596.1 + }, + "cpu_percent": 329.84, + "chunks_per_second": 39.47, + "unique_per_second": 34.81, + "forgotten_per_second": 39.42, + "waited_ticks_per_second": 0.0, + "chunks_total": 13798, + "chunks_per_bot": { + "explore": 13798.0 + }, + "unique_chunks_per_bot": { + "explore": 12330.0 + }, + "resent": 0, + "pulled_back": 185, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 10, + "behaviour": "explore", + "start_seconds": 11.02, + "mspt": { + "average": 28.49, + "p50": 26.2, + "p95": 48.58, + "p99": 72.04 + }, + "mspt_worst_p99": 125.7, + "tps": 20.0, + "rss_mb": { + "mean": 2231.06, + "max": 4077.8 + }, + "cpu_percent": 344.95, + "chunks_per_second": 46.31, + "unique_per_second": 45.88, + "forgotten_per_second": 44.14, + "waited_ticks_per_second": 111.95, + "chunks_total": 19722, + "chunks_per_bot": { + "explore": 1972.2 + }, + "unique_chunks_per_bot": { + "explore": 1957.0 + }, + "resent": 0, + "pulled_back": 18533, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "explore", + "start_seconds": 11.12, + "mspt": { + "average": 40.04, + "p50": 37.61, + "p95": 62.39, + "p99": 83.64 + }, + "mspt_worst_p99": 147.0, + "tps": 20.0, + "rss_mb": { + "mean": 2466.85, + "max": 4504.5 + }, + "cpu_percent": 340.13, + "chunks_per_second": 60.61, + "unique_per_second": 59.62, + "forgotten_per_second": 56.28, + "waited_ticks_per_second": 299.57, + "chunks_total": 32877, + "chunks_per_bot": { + "explore": 1315.08 + }, + "unique_chunks_per_bot": { + "explore": 1300.72 + }, + "resent": 0, + "pulled_back": 46272, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "explore", + "start_seconds": 11.32, + "mspt": { + "average": 61.82, + "p50": 59.21, + "p95": 90.46, + "p99": 115.58 + }, + "mspt_worst_p99": 320.9, + "tps": 16.18, + "rss_mb": { + "mean": 2634.54, + "max": 4883.1 + }, + "cpu_percent": 356.96, + "chunks_per_second": 95.26, + "unique_per_second": 94.31, + "forgotten_per_second": 85.86, + "waited_ticks_per_second": 528.08, + "chunks_total": 59251, + "chunks_per_bot": { + "explore": 1185.02 + }, + "unique_chunks_per_bot": { + "explore": 1173.96 + }, + "resent": 0, + "pulled_back": 99493, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 100, + "behaviour": "explore", + "start_seconds": 10.81, + "mspt": { + "average": 103.24, + "p50": 100.03, + "p95": 144.77, + "p99": 178.8 + }, + "mspt_worst_p99": 258.6, + "tps": 9.69, + "rss_mb": { + "mean": 1784.48, + "max": 1922.0 + }, + "cpu_percent": 336.94, + "chunks_per_second": 115.06, + "unique_per_second": 114.86, + "forgotten_per_second": 109.43, + "waited_ticks_per_second": 1127.57, + "chunks_total": 93839, + "chunks_per_bot": { + "explore": 938.39 + }, + "unique_chunks_per_bot": { + "explore": 935.28 + }, + "resent": 0, + "pulled_back": 133404, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 100, + "bots_failed": [], + "tick_queries": 58 + } + ] +} diff --git a/benchmarks/server-equal-work-2026-10-17/vanilla.json b/benchmarks/server-equal-work-2026-10-17/vanilla.json new file mode 100644 index 000000000..014554cee --- /dev/null +++ b/benchmarks/server-equal-work-2026-10-17/vanilla.json @@ -0,0 +1,324 @@ +{ + "kind": "horizon-server-benchmark", + "date": "2026-10-17T03:29:30+00:00", + "machine": { + "system": "Linux", + "machine": "x86_64", + "cpus": 4 + }, + "commit": "", + "java": [ + "openjdk 25.0.4.1 2026-08-18" + ], + "settings": { + "servers": "vanilla", + "bots": "1,10,25,50,100", + "behaviour": "walk,explore", + "join_every": 1.0, + "warmup": 60, + "seconds": 300, + "seed": 12345, + "gamerule": "locator_bar=false", + "heap": "4g", + "out": "/home/ludovic/scratch/sh2-bench3.json", + "nice": 19, + "bots_via": "ssh -o BatchMode=yes macmini", + "bots_bin": "/tmp/sh2-horizon-bots", + "connect": "100.72.51.8", + "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": "vanilla", + "bots": 1, + "behaviour": "walk", + "start_seconds": 15.43, + "mspt": { + "average": 18.32, + "p50": 19.26, + "p95": 27.34, + "p99": 45.48 + }, + "mspt_worst_p99": 393.3, + "tps": 20.0, + "rss_mb": { + "mean": 3428.43, + "max": 3437.7 + }, + "cpu_percent": 66.47, + "chunks_per_second": 9.92, + "unique_per_second": 0.29, + "forgotten_per_second": 9.95, + "waited_ticks_per_second": 0.0, + "chunks_total": 3850, + "chunks_per_bot": { + "walk": 3850.0 + }, + "unique_chunks_per_bot": { + "walk": 699.0 + }, + "resent": 0, + "pulled_back": 0, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "walk", + "start_seconds": 14.76, + "mspt": { + "average": 16.8, + "p50": 14.55, + "p95": 30.52, + "p99": 50.01 + }, + "mspt_worst_p99": 205.2, + "tps": 20.0, + "rss_mb": { + "mean": 3490.15, + "max": 3580.7 + }, + "cpu_percent": 83.7, + "chunks_per_second": 100.01, + "unique_per_second": 2.83, + "forgotten_per_second": 100.04, + "waited_ticks_per_second": 0.0, + "chunks_total": 38623, + "chunks_per_bot": { + "walk": 3862.3 + }, + "unique_chunks_per_bot": { + "walk": 697.2 + }, + "resent": 0, + "pulled_back": 5, + "moved_too_quickly": 5, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "walk", + "start_seconds": 14.84, + "mspt": { + "average": 16.01, + "p50": 14.55, + "p95": 26.39, + "p99": 51.26 + }, + "mspt_worst_p99": 397.8, + "tps": 20.0, + "rss_mb": { + "mean": 3550.02, + "max": 3593.5 + }, + "cpu_percent": 114.96, + "chunks_per_second": 251.62, + "unique_per_second": 6.72, + "forgotten_per_second": 251.16, + "waited_ticks_per_second": 0.0, + "chunks_total": 98701, + "chunks_per_bot": { + "walk": 3948.04 + }, + "unique_chunks_per_bot": { + "walk": 698.84 + }, + "resent": 0, + "pulled_back": 10, + "moved_too_quickly": 10, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "vanilla", + "bots": 50, + "behaviour": "walk", + "start_seconds": 14.43, + "mspt": { + "average": 20.52, + "p50": 19.55, + "p95": 29.72, + "p99": 49.04 + }, + "mspt_worst_p99": 600.4, + "tps": 20.0, + "rss_mb": { + "mean": 3633.12, + "max": 3658.7 + }, + "cpu_percent": 193.84, + "chunks_per_second": 501.86, + "unique_per_second": 12.15, + "forgotten_per_second": 501.8, + "waited_ticks_per_second": 0.0, + "chunks_total": 203429, + "chunks_per_bot": { + "walk": 4068.58 + }, + "unique_chunks_per_bot": { + "walk": 698.66 + }, + "resent": 0, + "pulled_back": 33, + "moved_too_quickly": 32, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "vanilla", + "bots": 100, + "behaviour": "walk", + "start_seconds": 15.34, + "error": "[Errno 32] Broken pipe" + }, + { + "server": "vanilla", + "bots": 1, + "behaviour": "explore", + "start_seconds": 15.55, + "mspt": { + "average": 9.93, + "p50": 8.12, + "p95": 21.33, + "p99": 60.19 + }, + "mspt_worst_p99": 373.0, + "tps": 20.0, + "rss_mb": { + "mean": 3564.11, + "max": 3655.0 + }, + "cpu_percent": 245.18, + "chunks_per_second": 35.93, + "unique_per_second": 33.35, + "forgotten_per_second": 35.64, + "waited_ticks_per_second": 0.0, + "chunks_total": 12340, + "chunks_per_bot": { + "explore": 12340.0 + }, + "unique_chunks_per_bot": { + "explore": 11569.0 + }, + "resent": 0, + "pulled_back": 261, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 1, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "explore", + "start_seconds": 15.03, + "mspt": { + "average": 16.99, + "p50": 14.46, + "p95": 29.61, + "p99": 132.18 + }, + "mspt_worst_p99": 1244.0, + "tps": 20.0, + "rss_mb": { + "mean": 4135.2, + "max": 4453.1 + }, + "cpu_percent": 242.32, + "chunks_per_second": 22.39, + "unique_per_second": 22.41, + "forgotten_per_second": 22.78, + "waited_ticks_per_second": 78.78, + "chunks_total": 10177, + "chunks_per_bot": { + "explore": 1017.7 + }, + "unique_chunks_per_bot": { + "explore": 1016.2 + }, + "resent": 0, + "pulled_back": 17314, + "moved_too_quickly": 19, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 59 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "explore", + "start_seconds": 14.84, + "mspt": { + "average": 24.19, + "p50": 21.22, + "p95": 36.51, + "p99": 174.82 + }, + "mspt_worst_p99": 1532.8, + "tps": 20.0, + "rss_mb": { + "mean": 4504.55, + "max": 4988.9 + }, + "cpu_percent": 319.55, + "chunks_per_second": 33.78, + "unique_per_second": 33.66, + "forgotten_per_second": 34.64, + "waited_ticks_per_second": 269.22, + "chunks_total": 20522, + "chunks_per_bot": { + "explore": 820.88 + }, + "unique_chunks_per_bot": { + "explore": 815.76 + }, + "resent": 0, + "pulled_back": 46246, + "moved_too_quickly": 28, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 50, + "behaviour": "explore", + "start_seconds": 14.73, + "error": "[Errno 32] Broken pipe" + }, + { + "server": "vanilla", + "bots": 100, + "behaviour": "explore", + "start_seconds": 14.54, + "error": "[Errno 32] Broken pipe" + } + ] +} diff --git a/benchmarks/server-four-way-2026-10-19/README.md b/benchmarks/server-four-way-2026-10-19/README.md new file mode 100644 index 000000000..570f5bb24 --- /dev/null +++ b/benchmarks/server-four-way-2026-10-19/README.md @@ -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-bin /path/to/horizon-bots --connect \ + --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. diff --git a/benchmarks/server-four-way-2026-10-19/load.txt b/benchmarks/server-four-way-2026-10-19/load.txt new file mode 100644 index 000000000..786b933b6 --- /dev/null +++ b/benchmarks/server-four-way-2026-10-19/load.txt @@ -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 diff --git a/benchmarks/server-four-way-2026-10-19/report-25-50.json b/benchmarks/server-four-way-2026-10-19/report-25-50.json new file mode 100644 index 000000000..dacda8397 --- /dev/null +++ b/benchmarks/server-four-way-2026-10-19/report-25-50.json @@ -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 + } + ] +} diff --git a/benchmarks/server-four-way-2026-10-19/report-paper-10.json b/benchmarks/server-four-way-2026-10-19/report-paper-10.json new file mode 100644 index 000000000..3408fd419 --- /dev/null +++ b/benchmarks/server-four-way-2026-10-19/report-paper-10.json @@ -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": [] +} diff --git a/benchmarks/server-four-way-2026-10-19/runs.jsonl b/benchmarks/server-four-way-2026-10-19/runs.jsonl new file mode 100644 index 000000000..04a1a6fb8 --- /dev/null +++ b/benchmarks/server-four-way-2026-10-19/runs.jsonl @@ -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} diff --git a/benchmarks/server-mixed-2026-10-18/README.md b/benchmarks/server-mixed-2026-10-18/README.md new file mode 100644 index 000000000..3cbe3897f --- /dev/null +++ b/benchmarks/server-mixed-2026-10-18/README.md @@ -0,0 +1,125 @@ +# Horizon's server against vanilla 26.3: mixed bots (2026-10-18) + +The first benchmark from docs/server.md section 10 (item 4): the same bots on Horizon's dedicated server, as +`install.sh` installs it today, and on vanilla 26.3's, one server at a time on the same quiet machine. +There were 1, 10, 25 and 50 bots, all with the `mixed` behaviour. + +## Summary + +- **Both servers held 20 TPS in every run.** +- **Memory:** Horizon's RSS was 3.7 to 5.2 times smaller: 0.65 GB against 3.4 GB with one player, and + 1.0 GB against 4.0 GB with 50. Vanilla runs with the usual fixed heap (`-Xms4g -Xmx4g`), so its RSS + is mostly that heap. +- **Tick time (MSPT):** + - with 1, 10 and 25 players, Horizon's average tick was shorter (2.4 against 17.9 ms, 12.4 against 34.5, + 24.0 against 31.6); + - with 50, it was longer (35.2 against 29.7 ms); + - Horizon's P99 was lower at every count, and so were its worst 5-second windows (241 ms at most, + against 424 to 2320 ms for vanilla). +- **The work was not equal, and the difference counts against Horizon.** In `mixed`, 20 % of the bots + fly straight out at 22 blocks a second, as fast as each server sends them chunks. Horizon kept up: + - with 10 or more players it sent 2.9 to 5.9 times as many chunks a second, and generated 1.9 to 2.9 + times as much new terrain a second; + - its walking bots each received 3300 to 4000 chunks, against 640 to 1100 on vanilla once 10 or more + were online. + + So at 50 players Horizon's tick is 18 % longer while it generates twice the new terrain. The + equal-rate explore pass of `../server-equal-work-2026-10-17/` is the fair comparison for exploring. +- **Start (fresh world, to `Done`):** 10 to 20 s for Horizon, 14.5 to 28.6 s for vanilla. + +## Results + +MSPT is milliseconds per tick from each server's own `/tick query` (50 is the limit for 20 TPS). P95 and +P99 are over the whole run. "Worst P99" is the highest P99 of a single 5-second query. RSS and CPU cover +the server's whole process tree (100 % is one thread; the machine has 4). "New chunks/s" counts chunks +no bot had before. "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 | New chunks/s | Pulled back | +|---:|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:| +| 1 | Horizon | **2.4** | 5.2 | 18.2 | 45.6 | 20 | **0.65 / 0.66 GB** | 25 % | 10.9 s | 9.9 | 0.3 | 0 | +| 1 | vanilla | 17.9 | 27.6 | 47.3 | 423.8 | 20 | 3.38 / 3.39 GB | 64 % | 14.8 s | 9.9 | 0.3 | 0 | +| 10 | Horizon | **12.4** | 36.0 | 65.9 | 117.1 | 20 | **0.94 / 1.01 GB** | 210 % | 10.2 s | 98.2 | 36.5 | 6267 | +| 10 | vanilla | 34.5 | 63.5 | 138.6 | 884.7 | 20 | 3.59 / 3.66 GB | 217 % | 14.5 s | 24.3 | 19.1 | 5500 | +| 25 | Horizon | **24.0** | 49.7 | 80.6 | 168.8 | 20 | **1.01 / 1.18 GB** | 192 % | 14.7 s | 178.2 | 31.9 | 5532 | +| 25 | vanilla | 31.6 | 70.0 | 222.7 | 2319.6 | 20 | 3.74 / 4.06 GB | 218 % | 28.6 s | 30.4 | 10.8 | 4524 | +| 50 | Horizon | 35.2 | 69.0 | 96.6 | 240.8 | 20 | **1.01 / 1.25 GB** | 218 % | 19.8 s | 273.2 | 34.6 | 8369 | +| 50 | vanilla | **29.7** | 62.3 | 137.4 | 489.3 | 20 | 4.00 / 4.60 GB | 294 % | 20.6 s | 94.4 | 17.8 | 12034 | + +Chunks each bot received, by behaviour (mean): + +| Bots | Server | walk | explore | idle | +|---:|---|---:|---:|---:| +| 10 | Horizon | 3992 | 7360 | 473 | +| 10 | vanilla | 680 | 2824 | 358 | +| 25 | Horizon | 3928 | 2475 | 473 | +| 25 | vanilla | 640 | 738 | 180 | +| 50 | Horizon | 3300 | 1192 | 460 | +| 50 | vanilla | 1105 | 629 | 220 | + +No bot failed to join or was dropped, and no chunk was sent twice (`resent` 0), on either server. Vanilla +also logged "moved too quickly" 12, 324 and 2193 times at 10, 25 and 50 bots. + +## Method + +`tools/server_bench.py`, unchanged, runs each configuration on a fresh server: + +- the same `server.properties` on both: seed 12345, view and simulation distance 10, offline, creative, + difficulty easy, `sync-chunk-writes=true`, white-list off; +- `gamerule respawn_radius 0`, and otherwise vanilla's default game rules on both. The locator bar is on + for both: Horizon's server has run it since v2026.10.18.13. The 2026-10-17 runs turned it off on vanilla + because Horizon had none then; +- the server waits for `Done`, then the bots join one a second, warm up for 60 seconds, and are measured + for 300 seconds, 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 their spawn: + +- `walk`: random points within 48 blocks at 5.6 blocks a second; +- `explore`: straight out at up to 22 blocks a second, as fast as chunks come; +- `idle`: hovers. + +A bot doesn't move while the chunk it stands in hasn't arrived, and it acknowledges chunk batches as +vanilla's client does. + +**Machine.** The ThinkPad: Intel Core i7-7500U (2 cores, 4 threads), 32 GB, NixOS (Linux 6.18). No other +session ran on it (`load.txt`: the fleet monitor shows 0 sessions throughout). The busy periods are the +servers themselves, which used 2 to 3 of its 4 threads from 10 bots up, so both servers were CPU-bound +there. The servers ran under `nice -n 19`. The desktop was meant to run this (24 threads) but didn't +wake. The bots ran on the Mac mini (M4 Pro) and reached the ThinkPad over the tailnet, so they took none +of the servers' CPU. The Mac mini was shared with other work (70 to 90 % busy), which only affects the +bots' timing. + +**What ran.** + +- Horizon: the published v2026.10.18.15 release, `horizon-client` (the engine) at 6c078b28, as + `install.sh` installs it. Its game files were prepared from the owner's own Minecraft by the release's + `horizon-prepare`. `horizon-server` (the supervisor, which takes no part in the tick) came from #898 + (4ea2243e), because the release's copy can't find game files prepared without sounds (#893). + `horizon-bots` came from main. +- Vanilla: the pinned 26.3 server jar (the owner's own copy) on OpenJDK 25.0.4.1, `-Xms4g -Xmx4g`. + +On Linux the release's engine needs `libasound.so.2` and `libudev.so.1`. The ThinkPad has them only in +the Nix store, so the run set `LD_LIBRARY_PATH` to them (see #898). + +## 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= \ + python3 tools/server_bench.py --servers horizon,vanilla --bots 1,10,25,50 --behaviour mixed --nice 19 \ + --bots-via 'ssh ' --bots-bin /path/to/horizon-bots --connect \ + --out mixed.json + +`mixed.json` is the report (`horizon-server-benchmark`: machine, versions, settings and every run), and +`load.txt` is the ThinkPad's load before and during the run. + +## Next + +- An equal-work `mixed` pass (`--explore-speed` at a rate both servers keep up with), then 100 bots. +- The same run on a bigger machine, so neither server is CPU-bound at 25 to 50 bots. +- Paper and Fabric + Lithium (docs/server.md section 9). +- At 50 players Horizon's tick is longer than vanilla's while it generates and sends far more. The + profile of the 2026-10-17 run (streaming and block entity ticks growing with players) is + perf-server's to take on. diff --git a/benchmarks/server-mixed-2026-10-18/load.txt b/benchmarks/server-mixed-2026-10-18/load.txt new file mode 100644 index 000000000..3468f9f86 --- /dev/null +++ b/benchmarks/server-mixed-2026-10-18/load.txt @@ -0,0 +1,286 @@ +ludothinkpad — OK + RAM 29.1 GB free of 31.2 GB · swap 0.3 GB/15.6 GB + CPU 4 threads, 2% busy (agents 0.0, rest 0.1 cores) · 67°C · load 2.1 + · disk 295.5 GB free · battery 96% + +Sessions, most important first: + (finished session) 0.0 GB leftovers: node 0.0 GB + +Other machines (≤ 1 min old): desktop offline · macbook offline · macmini ok: 26.6 GB free, 14 threads 69% busy, 1 sessions + +# During the run, once a minute (uptime, then ~/fleet/load fleet) +18:25:30 + 18:25:30 up 8 days 3:08, 0 users, load average: 2.13, 1.53, 1.29 +ludothinkpad ok: 29.1 GB free, 4 threads 2% busy, 0 sessions +desktop offline +macmini busy: 27.6 GB free, 14 threads 87% busy, 1 sessions +18:26:34 + 18:26:34 up 8 days 3:09, 0 users, load average: 1.70, 1.56, 1.31 +ludothinkpad ok: 28.5 GB free, 4 threads 23% busy, 0 sessions +desktop offline +macmini busy: 26.4 GB free, 14 threads 85% busy, 1 sessions +18:27:38 + 18:27:38 up 8 days 3:10, 0 users, load average: 1.10, 1.43, 1.28 +ludothinkpad ok: 28.5 GB free, 4 threads 4% busy, 0 sessions +desktop offline +macmini ok: 27.7 GB free, 14 threads 80% busy, 1 sessions +18:28:42 + 18:28:42 up 8 days 3:11, 0 users, load average: 0.93, 1.29, 1.24 +ludothinkpad ok: 28.5 GB free, 4 threads 22% busy, 0 sessions +desktop offline +macmini ok: 26.0 GB free, 14 threads 82% busy, 1 sessions +18:29:46 + 18:29:46 up 8 days 3:12, 0 users, load average: 0.62, 1.12, 1.18 +ludothinkpad ok: 28.5 GB free, 4 threads 12% busy, 0 sessions +desktop offline +macmini ok: 25.7 GB free, 14 threads 81% busy, 1 sessions +18:30:50 + 18:30:51 up 8 days 3:13, 0 users, load average: 0.79, 1.05, 1.15 +ludothinkpad ok: 28.5 GB free, 4 threads 7% busy, 0 sessions +desktop offline +macmini busy: 27.0 GB free, 14 threads 88% busy, 1 sessions +18:31:55 + 18:31:55 up 8 days 3:14, 0 users, load average: 0.67, 0.96, 1.11 +ludothinkpad ok: 28.6 GB free, 4 threads 73% busy, 0 sessions +desktop offline +macmini ok: 27.3 GB free, 14 threads 79% busy, 1 sessions +18:32:59 + 18:32:59 up 8 days 3:15, 0 users, load average: 4.01, 1.91, 1.44 +ludothinkpad ok: 25.9 GB free, 4 threads 24% busy, 0 sessions +desktop offline +macmini ok: 26.4 GB free, 14 threads 85% busy, 1 sessions +18:34:03 + 18:34:03 up 8 days 3:16, 0 users, load average: 2.24, 1.84, 1.44 +ludothinkpad ok: 25.9 GB free, 4 threads 21% busy, 0 sessions +desktop offline +macmini busy: 29.2 GB free, 14 threads 86% busy, 1 sessions +18:35:07 + 18:35:07 up 8 days 3:17, 0 users, load average: 1.28, 1.63, 1.40 +ludothinkpad ok: 25.8 GB free, 4 threads 21% busy, 0 sessions +desktop offline +macmini busy: 31.0 GB free, 14 threads 86% busy, 1 sessions +18:36:11 + 18:36:11 up 8 days 3:18, 0 users, load average: 0.79, 1.41, 1.33 +ludothinkpad ok: 25.9 GB free, 4 threads 18% busy, 0 sessions +desktop offline +macmini busy: 26.3 GB free, 14 threads 93% busy, 1 sessions +18:37:15 + 18:37:15 up 8 days 3:20, 0 users, load average: 1.44, 1.47, 1.36 +ludothinkpad ok: 25.9 GB free, 4 threads 23% busy, 0 sessions +desktop offline +macmini busy: 26.8 GB free, 14 threads 86% busy, 1 sessions +18:38:19 + 18:38:19 up 8 days 3:21, 0 users, load average: 1.37, 1.45, 1.36 +ludothinkpad ok: 25.9 GB free, 4 threads 13% busy, 0 sessions +desktop offline +macmini busy: 26.7 GB free, 14 threads 86% busy, 1 sessions +18:39:24 + 18:39:24 up 8 days 3:22, 0 users, load average: 2.27, 1.66, 1.44 +ludothinkpad ok: 28.3 GB free, 4 threads 62% busy, 0 sessions +desktop offline +macmini ok: 27.1 GB free, 14 threads 70% busy, 1 sessions +18:40:28 + 18:40:28 up 8 days 3:23, 0 users, load average: 1.95, 1.72, 1.48 +ludothinkpad ok: 28.3 GB free, 4 threads 63% busy, 0 sessions +desktop offline +macmini ok: 26.7 GB free, 14 threads 84% busy, 1 sessions +18:41:32 + 18:41:32 up 8 days 3:24, 0 users, load average: 3.32, 2.22, 1.67 +ludothinkpad ok: 28.2 GB free, 4 threads 61% busy, 0 sessions +desktop offline +macmini busy: 26.8 GB free, 14 threads 85% busy, 1 sessions +18:42:36 + 18:42:36 up 8 days 3:25, 0 users, load average: 3.13, 2.33, 1.75 +ludothinkpad ok: 28.2 GB free, 4 threads 60% busy, 0 sessions +desktop offline +macmini busy: 27.1 GB free, 14 threads 91% busy, 1 sessions +18:43:40 + 18:43:40 up 8 days 3:26, 0 users, load average: 3.27, 2.48, 1.83 +ludothinkpad busy: 28.1 GB free, 4 threads 90% busy, 0 sessions +desktop offline +macmini busy: 28.3 GB free, 14 threads 92% busy, 1 sessions +18:44:44 + 18:44:44 up 8 days 3:27, 0 users, load average: 2.14, 2.32, 1.82 +ludothinkpad ok: 28.1 GB free, 4 threads 55% busy, 0 sessions +desktop offline +macmini busy: 30.4 GB free, 14 threads 88% busy, 1 sessions +18:45:48 + 18:45:48 up 8 days 3:28, 0 users, load average: 4.94, 3.06, 2.11 +ludothinkpad busy: 25.9 GB free, 4 threads 86% busy, 0 sessions +desktop offline +macmini ok: 28.7 GB free, 14 threads 84% busy, 1 sessions +18:46:53 + 18:46:53 up 8 days 3:29, 0 users, load average: 6.55, 3.92, 2.47 +ludothinkpad busy: 25.4 GB free, 4 threads 99% busy, 0 sessions +desktop offline +macmini ok: 29.3 GB free, 14 threads 81% busy, 1 sessions +18:47:57 + 18:47:57 up 8 days 3:30, 0 users, load average: 8.50, 4.96, 2.93 +ludothinkpad busy: 24.0 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini busy: 27.9 GB free, 14 threads 90% busy, 1 sessions +18:49:01 + 18:49:01 up 8 days 3:31, 0 users, load average: 8.59, 5.68, 3.31 +ludothinkpad busy: 22.7 GB free, 4 threads 99% busy, 0 sessions +desktop offline +macmini ok: 28.2 GB free, 14 threads 85% busy, 1 sessions +18:50:05 + 18:50:05 up 8 days 3:32, 0 users, load average: 7.81, 6.07, 3.61 +ludothinkpad busy: 24.5 GB free, 4 threads 90% busy, 0 sessions +desktop offline +macmini busy: 28.6 GB free, 14 threads 96% busy, 1 sessions +18:51:09 + 18:51:09 up 8 days 3:33, 0 users, load average: 8.29, 6.50, 3.93 +ludothinkpad busy: 24.3 GB free, 4 threads 92% busy, 0 sessions +desktop offline +macmini busy: 28.0 GB free, 14 threads 94% busy, 1 sessions +18:52:14 + 18:52:14 up 8 days 3:35, 0 users, load average: 5.58, 6.06, 3.95 +ludothinkpad busy: 26.0 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini busy: 26.3 GB free, 14 threads 87% busy, 1 sessions +18:53:18 + 18:53:18 up 8 days 3:36, 0 users, load average: 4.53, 5.69, 3.97 +ludothinkpad busy: 27.2 GB free, 4 threads 89% busy, 0 sessions +desktop offline +macmini busy: 26.8 GB free, 14 threads 87% busy, 1 sessions +18:54:22 + 18:54:22 up 8 days 3:37, 0 users, load average: 5.31, 5.64, 4.07 +ludothinkpad busy: 26.3 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini busy: 29.3 GB free, 14 threads 91% busy, 1 sessions +18:55:26 + 18:55:26 up 8 days 3:38, 0 users, load average: 10.47, 6.99, 4.63 +ludothinkpad busy: 26.1 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini busy: 26.7 GB free, 14 threads 90% busy, 1 sessions +18:56:30 + 18:56:30 up 8 days 3:39, 0 users, load average: 9.27, 7.45, 4.96 +ludothinkpad busy: 26.2 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini busy: 28.5 GB free, 14 threads 92% busy, 1 sessions +18:57:35 + 18:57:35 up 8 days 3:40, 0 users, load average: 7.79, 7.39, 5.10 +ludothinkpad busy: 26.1 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini ok: 27.1 GB free, 14 threads 80% busy, 1 sessions +18:58:39 + 18:58:39 up 8 days 3:41, 0 users, load average: 8.06, 7.55, 5.32 +ludothinkpad busy: 26.4 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini busy: 31.8 GB free, 14 threads 95% busy, 1 sessions +18:59:43 + 18:59:43 up 8 days 3:42, 0 users, load average: 8.44, 7.65, 5.50 +ludothinkpad busy: 26.2 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini busy: 31.4 GB free, 14 threads 94% busy, 1 sessions +19:00:47 + 19:00:47 up 8 days 3:43, 0 users, load average: 11.04, 8.66, 6.00 +ludothinkpad busy: 24.9 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini busy: 31.8 GB free, 14 threads 88% busy, 1 sessions +19:01:51 + 19:01:51 up 8 days 3:44, 0 users, load average: 11.91, 9.37, 6.41 +ludothinkpad busy: 24.4 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini ok: 32.9 GB free, 14 threads 77% busy, 1 sessions +19:02:56 + 19:02:56 up 8 days 3:45, 0 users, load average: 11.64, 9.85, 6.79 +ludothinkpad ok: 25.5 GB free, 4 threads 70% busy, 0 sessions +desktop offline +macmini busy: 30.9 GB free, 14 threads 91% busy, 1 sessions +19:04:00 + 19:04:00 up 8 days 3:46, 0 users, load average: 11.42, 10.06, 7.06 +ludothinkpad busy: 23.4 GB free, 4 threads 99% busy, 0 sessions +desktop offline +macmini busy: 28.4 GB free, 14 threads 94% busy, 1 sessions +19:05:04 + 19:05:04 up 8 days 3:47, 0 users, load average: 9.83, 9.96, 7.24 +ludothinkpad busy: 23.2 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini ok: 31.4 GB free, 14 threads 80% busy, 1 sessions +19:06:09 + 19:06:09 up 8 days 3:48, 0 users, load average: 8.38, 9.68, 7.34 +ludothinkpad ok: 22.7 GB free, 4 threads 17% busy, 0 sessions +desktop offline +macmini busy: 29.8 GB free, 14 threads 86% busy, 1 sessions +19:07:13 + 19:07:13 up 8 days 3:49, 0 users, load average: 4.51, 8.27, 7.00 +ludothinkpad ok: 25.7 GB free, 4 threads 75% busy, 0 sessions +desktop offline +macmini busy: 26.5 GB free, 14 threads 89% busy, 1 sessions +19:08:17 + 19:08:17 up 8 days 3:51, 0 users, load average: 6.77, 8.13, 7.04 +ludothinkpad busy: 25.8 GB free, 4 threads 95% busy, 0 sessions +desktop offline +macmini busy: 30.8 GB free, 14 threads 91% busy, 1 sessions +19:09:22 + 19:09:22 up 8 days 3:52, 0 users, load average: 8.27, 8.25, 7.14 +ludothinkpad busy: 25.2 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini busy: 28.3 GB free, 14 threads 91% busy, 1 sessions +19:10:26 + 19:10:26 up 8 days 3:53, 0 users, load average: 8.88, 8.57, 7.34 +ludothinkpad busy: 24.8 GB free, 4 threads 99% busy, 0 sessions +desktop offline +macmini ok: 32.4 GB free, 14 threads 72% busy, 1 sessions +19:11:30 + 19:11:30 up 8 days 3:54, 0 users, load average: 13.33, 10.05, 7.94 +ludothinkpad busy: 24.7 GB free, 4 threads 97% busy, 0 sessions +desktop offline +macmini busy: 30.1 GB free, 14 threads 87% busy, 1 sessions +19:12:35 + 19:12:35 up 8 days 3:55, 0 users, load average: 10.92, 10.13, 8.12 +ludothinkpad ok: 26.4 GB free, 4 threads 76% busy, 0 sessions +desktop offline +macmini busy: 29.3 GB free, 14 threads 86% busy, 1 sessions +19:13:39 + 19:13:39 up 8 days 3:56, 0 users, load average: 9.98, 9.89, 8.16 +ludothinkpad busy: 25.8 GB free, 4 threads 90% busy, 0 sessions +desktop offline +macmini busy: 28.8 GB free, 14 threads 93% busy, 1 sessions +19:14:43 + 19:14:43 up 8 days 3:57, 0 users, load average: 8.95, 9.63, 8.19 +ludothinkpad busy: 25.1 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini ok: 29.6 GB free, 14 threads 71% busy, 1 sessions +19:15:47 + 19:15:47 up 8 days 3:58, 0 users, load average: 12.02, 10.38, 8.55 +ludothinkpad busy: 24.5 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini busy: 28.1 GB free, 14 threads 93% busy, 1 sessions +19:16:52 + 19:16:52 up 8 days 3:59, 0 users, load average: 13.82, 11.21, 8.95 +ludothinkpad busy: 23.9 GB free, 4 threads 100% busy, 0 sessions +desktop offline +macmini ok: 28.4 GB free, 14 threads 75% busy, 1 sessions +19:17:56 + 19:17:56 up 8 days 4:00, 0 users, load average: 11.95, 11.20, 9.10 +ludothinkpad busy: 25.0 GB free, 4 threads 99% busy, 0 sessions +desktop offline +macmini ok: 28.4 GB free, 14 threads 81% busy, 1 sessions +19:19:00 + 19:19:00 up 8 days 4:01, 0 users, load average: 11.46, 11.14, 9.22 +ludothinkpad busy: 24.6 GB free, 4 threads 95% busy, 0 sessions +desktop offline +macmini ok: 26.4 GB free, 14 threads 83% busy, 1 sessions +19:20:04 + 19:20:04 up 8 days 4:02, 0 users, load average: 11.11, 10.97, 9.28 +ludothinkpad busy: 24.6 GB free, 4 threads 93% busy, 0 sessions +desktop offline +macmini ok: 26.7 GB free, 14 threads 85% busy, 1 sessions +19:21:08 + 19:21:08 up 8 days 4:03, 0 users, load average: 9.81, 10.60, 9.27 +ludothinkpad busy: 24.4 GB free, 4 threads 91% busy, 0 sessions +desktop offline +macmini ok: 27.8 GB free, 14 threads 75% busy, 1 sessions +19:22:13 + 19:22:13 up 8 days 4:04, 0 users, load average: 5.98, 9.51, 9.00 +ludothinkpad ok: 23.8 GB free, 4 threads 18% busy, 0 sessions +desktop offline +macmini busy: 29.7 GB free, 14 threads 85% busy, 1 sessions +19:23:17 + 19:23:17 up 8 days 4:06, 0 users, load average: 4.01, 8.27, 8.60 +ludothinkpad ok: 23.1 GB free, 4 threads 56% busy, 0 sessions +desktop offline +macmini ok: 28.2 GB free, 14 threads 79% busy, 1 sessions diff --git a/benchmarks/server-mixed-2026-10-18/mixed.json b/benchmarks/server-mixed-2026-10-18/mixed.json new file mode 100644 index 000000000..36b21c327 --- /dev/null +++ b/benchmarks/server-mixed-2026-10-18/mixed.json @@ -0,0 +1,367 @@ +{ + "kind": "horizon-server-benchmark", + "date": "2026-10-18T17:23:30+00:00", + "machine": { + "system": "Linux", + "machine": "x86_64", + "cpus": 4, + "note": "ThinkPad: Intel Core i7-7500U (2 cores, 4 threads), 32 GB, NixOS, Linux 6.18; the bots on the Mac mini (M4 Pro) over the tailnet" + }, + "commit": "4ea2243e4d1a44f910857bd759845db70e3690a8", + "java": [ + "openjdk 25.0.4.1 2026-08-18" + ], + "settings": { + "servers": "horizon,vanilla", + "bots": "1,10,25,50", + "behaviour": "mixed", + "join_every": 1.0, + "warmup": 60, + "seconds": 300, + "seed": 12345, + "explore_speed": 0, + "gamerule": "", + "heap": "4g", + "out": "mixed.json", + "nice": 19, + "bots_via": "ssh macmini", + "bots_bin": "horizon-bots (on the Mac mini)", + "connect": "", + "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": 1, + "behaviour": "mixed", + "start_seconds": 10.92, + "mspt": { + "average": 2.38, + "p50": 1.97, + "p95": 5.17, + "p99": 18.2 + }, + "mspt_worst_p99": 45.6, + "tps": 20.0, + "rss_mb": { + "mean": 653.7, + "max": 659.2 + }, + "cpu_percent": 24.51, + "chunks_per_second": 9.9, + "unique_per_second": 0.3, + "forgotten_per_second": 9.93, + "waited_ticks_per_second": 0.0, + "chunks_total": 3951, + "chunks_per_bot": { + "walk": 3951.0 + }, + "unique_chunks_per_bot": { + "walk": 699.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.75, + "mspt": { + "average": 17.9, + "p50": 18.39, + "p95": 27.58, + "p99": 47.28 + }, + "mspt_worst_p99": 423.8, + "tps": 20.0, + "rss_mb": { + "mean": 3382.06, + "max": 3394.1 + }, + "cpu_percent": 64.02, + "chunks_per_second": 9.92, + "unique_per_second": 0.29, + "forgotten_per_second": 9.95, + "waited_ticks_per_second": 0.0, + "chunks_total": 3846, + "chunks_per_bot": { + "walk": 3846.0 + }, + "unique_chunks_per_bot": { + "walk": 699.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": 10.22, + "mspt": { + "average": 12.39, + "p50": 8.33, + "p95": 35.99, + "p99": 65.9 + }, + "mspt_worst_p99": 117.1, + "tps": 20.0, + "rss_mb": { + "mean": 935.85, + "max": 1008.6 + }, + "cpu_percent": 210.37, + "chunks_per_second": 98.22, + "unique_per_second": 36.49, + "forgotten_per_second": 98.25, + "waited_ticks_per_second": 0.0, + "chunks_total": 39618, + "chunks_per_bot": { + "walk": 3992.17, + "explore": 7359.5, + "idle": 473.0 + }, + "unique_chunks_per_bot": { + "walk": 696.83, + "explore": 6869.0, + "idle": 473.0 + }, + "resent": 0, + "pulled_back": 6267, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 10, + "behaviour": "mixed", + "start_seconds": 14.54, + "mspt": { + "average": 34.47, + "p50": 29.67, + "p95": 63.52, + "p99": 138.58 + }, + "mspt_worst_p99": 884.7, + "tps": 20.0, + "rss_mb": { + "mean": 3594.26, + "max": 3661.7 + }, + "cpu_percent": 217.13, + "chunks_per_second": 24.34, + "unique_per_second": 19.13, + "forgotten_per_second": 20.12, + "waited_ticks_per_second": 0.62, + "chunks_total": 10443, + "chunks_per_bot": { + "walk": 679.83, + "explore": 2824.0, + "idle": 358.0 + }, + "unique_chunks_per_bot": { + "walk": 389.17, + "explore": 2824.0, + "idle": 358.0 + }, + "resent": 0, + "pulled_back": 5500, + "moved_too_quickly": 12, + "moved_wrongly": 0, + "bots_joined": 10, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "horizon", + "bots": 25, + "behaviour": "mixed", + "start_seconds": 14.73, + "mspt": { + "average": 24.0, + "p50": 21.07, + "p95": 49.72, + "p99": 80.57 + }, + "mspt_worst_p99": 168.8, + "tps": 20.0, + "rss_mb": { + "mean": 1013.07, + "max": 1179.4 + }, + "cpu_percent": 192.47, + "chunks_per_second": 178.23, + "unique_per_second": 31.85, + "forgotten_per_second": 177.93, + "waited_ticks_per_second": 60.59, + "chunks_total": 73657, + "chunks_per_bot": { + "walk": 3927.87, + "explore": 2474.8, + "idle": 473.0 + }, + "unique_chunks_per_bot": { + "walk": 691.73, + "explore": 2447.8, + "idle": 473.0 + }, + "resent": 0, + "pulled_back": 5532, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 58 + }, + { + "server": "vanilla", + "bots": 25, + "behaviour": "mixed", + "start_seconds": 28.57, + "mspt": { + "average": 31.59, + "p50": 25.18, + "p95": 70.03, + "p99": 222.74 + }, + "mspt_worst_p99": 2319.6, + "tps": 20.0, + "rss_mb": { + "mean": 3742.51, + "max": 4055.0 + }, + "cpu_percent": 218.34, + "chunks_per_second": 30.37, + "unique_per_second": 10.84, + "forgotten_per_second": 29.44, + "waited_ticks_per_second": 50.91, + "chunks_total": 14193, + "chunks_per_bot": { + "walk": 640.2, + "explore": 738.0, + "idle": 180.0 + }, + "unique_chunks_per_bot": { + "walk": 211.33, + "explore": 737.6, + "idle": 180.0 + }, + "resent": 0, + "pulled_back": 4524, + "moved_too_quickly": 324, + "moved_wrongly": 0, + "bots_joined": 25, + "bots_failed": [], + "tick_queries": 56 + }, + { + "server": "horizon", + "bots": 50, + "behaviour": "mixed", + "start_seconds": 19.84, + "mspt": { + "average": 35.2, + "p50": 32.12, + "p95": 69.04, + "p99": 96.56 + }, + "mspt_worst_p99": 240.8, + "tps": 20.0, + "rss_mb": { + "mean": 1013.2, + "max": 1249.1 + }, + "cpu_percent": 217.68, + "chunks_per_second": 273.18, + "unique_per_second": 34.58, + "forgotten_per_second": 268.19, + "waited_ticks_per_second": 138.29, + "chunks_total": 115527, + "chunks_per_bot": { + "walk": 3300.1, + "explore": 1192.4, + "idle": 460.0 + }, + "unique_chunks_per_bot": { + "walk": 632.67, + "explore": 1186.9, + "idle": 460.0 + }, + "resent": 0, + "pulled_back": 8369, + "moved_too_quickly": 0, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 57 + }, + { + "server": "vanilla", + "bots": 50, + "behaviour": "mixed", + "start_seconds": 20.58, + "mspt": { + "average": 29.74, + "p50": 23.6, + "p95": 62.25, + "p99": 137.41 + }, + "mspt_worst_p99": 489.3, + "tps": 20.0, + "rss_mb": { + "mean": 3995.71, + "max": 4597.8 + }, + "cpu_percent": 294.31, + "chunks_per_second": 94.36, + "unique_per_second": 17.76, + "forgotten_per_second": 94.69, + "waited_ticks_per_second": 101.48, + "chunks_total": 41637, + "chunks_per_bot": { + "walk": 1104.97, + "explore": 628.8, + "idle": 220.0 + }, + "unique_chunks_per_bot": { + "walk": 279.4, + "explore": 626.4, + "idle": 220.0 + }, + "resent": 0, + "pulled_back": 12034, + "moved_too_quickly": 2193, + "moved_wrongly": 0, + "bots_joined": 50, + "bots_failed": [], + "tick_queries": 57 + } + ], + "horizon_build": "v2026.10.18.15 release binaries (horizon-client, the engine, at 6c078b28); horizon-server from #898 (4ea2243e, the supervisor only)" +} \ No newline at end of file diff --git a/crates/horizon-bots/src/main.rs b/crates/horizon-bots/src/main.rs index d76da5e84..090daeb4e 100644 --- a/crates/horizon-bots/src/main.rs +++ b/crates/horizon-bots/src/main.rs @@ -9,6 +9,11 @@ //! horizon-bots --server 127.0.0.1:25565 --bots 25 --behaviour mixed --seconds 360 --report bots.json //! //! It prints one JSON line a second (bots online, chunks received) and writes a summary with `--report`. +//! A bot moves only while it holds the chunk it stands in: vanilla's client stops ticking its player in a chunk +//! it hasn't received, so a player can't outrun the server's chunk sending, on any server. +//! Each bot also keeps the chunks it holds, as a client's chunk cache does, so the report tells chunks that +//! entered the view apart from chunks sent again while the bot still held them (`resent`), and counts the +//! times the server pulled a bot back (`pulled_back`: a teleport after the first, as for "moved too quickly"). use horizon_protocol::{ PROTOCOL_VERSION, @@ -25,6 +30,7 @@ use horizon_protocol::{ }; use serde_json::json; use std::{ + collections::HashSet, sync::{ Arc, Mutex, atomic::{AtomicBool, AtomicU64, Ordering}, @@ -43,8 +49,9 @@ horizon-bots: load-test bots for a Minecraft Java Edition 26.3 server (offline m --seconds N how long to run after the first bot joins; default 60 --view-distance N what the bots ask for; default 10 --seed N default 12345 + --explore-speed B blocks a second an explore bot flies; default 22 (as fast as chunks come) --name PREFIX bot names: PREFIX0, PREFIX1...; default Bot - --report FILE a JSON summary at the end + --report FILE a JSON summary at the end (- prints it after the per-second lines) "; #[derive(Clone, Copy, Debug, PartialEq)] @@ -73,6 +80,7 @@ struct Options { seconds: u64, view_distance: i8, seed: u64, + explore_speed: f64, name: String, report: Option, } @@ -87,6 +95,7 @@ fn options() -> Result { seconds: 60, view_distance: 10, seed: 12345, + explore_speed: 22.0, name: "Bot".into(), report: None, }; @@ -110,6 +119,9 @@ fn options() -> Result { "--seconds" => o.seconds = value.parse().map_err(|_| number("seconds"))?, "--view-distance" => o.view_distance = value.parse().map_err(|_| number("2 to 32"))?, "--seed" => o.seed = value.parse().map_err(|_| number("a number"))?, + "--explore-speed" => { + o.explore_speed = value.parse().map_err(|_| number("blocks a second"))?; + } "--name" => o.name = value, "--report" => o.report = Some(value), _ => return Err(format!("unknown option {arg}; --help lists them")), @@ -129,6 +141,17 @@ fn options() -> Result { struct Bot { online: AtomicBool, chunks: AtomicU64, + /// Chunks received while the bot already held them (no ForgetLevelChunk in between). + resent: AtomicU64, + /// Different chunks received over the run. + unique: AtomicU64, + forgotten: AtomicU64, + /// Teleports after the spawn one: the server refused a move. + pulled_back: AtomicU64, + /// Ticks the bot couldn't move because it didn't hold its chunk yet. + waited: AtomicU64, + /// The chunks the bot holds (ClientChunkCache). + held: Mutex>, /// Where the server last put the bot (a teleport), to start from. teleport: Mutex>, error: Mutex>, @@ -268,6 +291,36 @@ fn join(o: &Options, name: &str) -> Result { } } +/// ChunkBatchSizeCalculator: the chunks per tick a vanilla client asks for, from how long its batches take +/// to arrive. +struct BatchSize { + nanos_per_chunk: f64, + old_samples_weight: u32, + started: Instant, +} + +impl BatchSize { + fn new() -> Self { + Self { + nanos_per_chunk: 2_000_000.0, + old_samples_weight: 1, + started: Instant::now(), + } + } + fn finished(&mut self, size: i32) { + if size > 0 { + let per_chunk = self.started.elapsed().as_nanos() as f64 / f64::from(size); + let clamped = per_chunk.clamp(self.nanos_per_chunk / 3.0, self.nanos_per_chunk * 3.0); + let weight = f64::from(self.old_samples_weight); + self.nanos_per_chunk = (self.nanos_per_chunk * weight + clamped) / (weight + 1.0); + self.old_samples_weight = (self.old_samples_weight + 1).min(49); + } + } + fn desired_chunks_per_tick(&self) -> f32 { + (7_000_000.0 / self.nanos_per_chunk) as f32 + } +} + /// Reads the play state: answers what a client must answer, counts chunks, records teleports. fn read( mut reader: horizon_protocol::connection::ConnReader, @@ -277,6 +330,9 @@ fn read( use ids::play::clientbound as cb; use ids::play::serverbound as sb; let send = |body: Vec| writer.lock().unwrap().send(&body); + let mut seen = HashSet::new(); + let mut batch = BatchSize::new(); + let mut teleports = 0u64; loop { let (id, body) = match reader.recv() { Ok(packet) => packet, @@ -299,14 +355,38 @@ fn read( }, cb::LEVEL_CHUNK_WITH_LIGHT => { bot.chunks.fetch_add(1, Ordering::Relaxed); + if let (Ok(x), Ok(z)) = (r.i32(), r.i32()) { + if !bot.held.lock().unwrap().insert((x, z)) { + bot.resent.fetch_add(1, Ordering::Relaxed); + } + if seen.insert((x, z)) { + bot.unique.fetch_add(1, Ordering::Relaxed); + } + } Ok(()) } - cb::CHUNK_BATCH_FINISHED => send( - play::Serverbound::ChunkBatchReceived(play::ChunkBatchReceived { - chunks_per_tick: 25.0, - }) - .encode(), - ), + cb::FORGET_LEVEL_CHUNK => { + if let Ok(play::Clientbound::ForgetLevelChunk(p)) = + play::Clientbound::decode(id, &body) + && bot.held.lock().unwrap().remove(&(p.pos[0], p.pos[1])) + { + bot.forgotten.fetch_add(1, Ordering::Relaxed); + } + Ok(()) + } + cb::CHUNK_BATCH_START => { + batch.started = Instant::now(); + Ok(()) + } + cb::CHUNK_BATCH_FINISHED => { + batch.finished(r.varint().unwrap_or(0)); + send( + play::Serverbound::ChunkBatchReceived(play::ChunkBatchReceived { + chunks_per_tick: batch.desired_chunks_per_tick(), + }) + .encode(), + ) + } cb::PLAYER_POSITION => match play::Clientbound::decode(id, &body) { Ok(play::Clientbound::PlayerPosition(p)) => { let mut at = bot.teleport.lock().unwrap(); @@ -320,6 +400,10 @@ fn read( } }); *at = Some(pos); + teleports += 1; + if teleports > 1 { + bot.pulled_back.fetch_add(1, Ordering::Relaxed); + } send( play::Serverbound::AcceptTeleportation(play::AcceptTeleportation { id: p.teleport, @@ -416,7 +500,10 @@ fn run_bot( if let Some(at) = bot.teleport.lock().unwrap().take() { pos = at; } - if pos[1] < cruise { + let here = ((pos[0].floor() as i32) >> 4, (pos[2].floor() as i32) >> 4); + if !bot.held.lock().unwrap().contains(&here) { + bot.waited.fetch_add(1, Ordering::Relaxed); + } else if pos[1] < cruise { pos[1] = (pos[1] + 0.5).min(cruise); } else { match behaviour { @@ -440,9 +527,11 @@ fn run_bot( } } Behaviour::Explore => { - // Flying fast in creative: about 22 blocks a second, which keeps new chunks coming. - pos[0] += heading.cos() * 1.1; - pos[2] += heading.sin() * 1.1; + // Flying in creative, by default about 22 blocks a second, which keeps new + // chunks coming as fast as the server sends them. + let step = o.explore_speed / 20.0; + pos[0] += heading.cos() * step; + pos[2] += heading.sin() * step; } } } @@ -520,6 +609,9 @@ fn main() { .filter(|b| b.online.load(Ordering::SeqCst)) .count(); let chunks: u64 = bots.iter().map(|b| b.chunks.load(Ordering::Relaxed)).sum(); + let total = |f: fn(&Bot) -> &AtomicU64| -> u64 { + bots.iter().map(|b| f(b).load(Ordering::Relaxed)).sum() + }; let failed = bots .iter() .filter(|b| b.error.lock().unwrap().is_some()) @@ -536,6 +628,11 @@ fn main() { "failed": failed, "chunks": chunks, "chunks_per_second": chunks - previous_chunks, + "unique": total(|b| &b.unique), + "forgotten": total(|b| &b.forgotten), + "resent": total(|b| &b.resent), + "pulled_back": total(|b| &b.pulled_back), + "waited_ticks": total(|b| &b.waited), }) ); previous_chunks = chunks; @@ -554,19 +651,30 @@ fn main() { "bots": o.bots, "behaviour": o.behaviour, "seed": o.seed, + "explore_speed": o.explore_speed, "view_distance": o.view_distance, "seconds": o.seconds, "joined": bots.iter().filter(|b| b.joined_after_ms.load(Ordering::SeqCst) > 0).count(), "chunks": bots.iter().map(|b| b.chunks.load(Ordering::Relaxed)).sum::(), + "resent": bots.iter().map(|b| b.resent.load(Ordering::Relaxed)).sum::(), + "pulled_back": bots.iter().map(|b| b.pulled_back.load(Ordering::Relaxed)).sum::(), + "waited_ticks": bots.iter().map(|b| b.waited.load(Ordering::Relaxed)).sum::(), "per_bot": bots.iter().enumerate().map(|(i, b)| json!({ "name": format!("{}{i}", o.name), "behaviour": behaviour_of(&o.behaviour, i).name(), "joined_after_ms": b.joined_after_ms.load(Ordering::SeqCst), "chunks": b.chunks.load(Ordering::Relaxed), + "unique_chunks": b.unique.load(Ordering::Relaxed), + "resent": b.resent.load(Ordering::Relaxed), + "forgotten": b.forgotten.load(Ordering::Relaxed), + "pulled_back": b.pulled_back.load(Ordering::Relaxed), + "waited_ticks": b.waited.load(Ordering::Relaxed), "error": *b.error.lock().unwrap(), })).collect::>(), }); - if let Some(path) = &o.report { + if o.report.as_deref() == Some("-") { + println!("{summary:#}"); + } else if let Some(path) = &o.report { if let Err(e) = std::fs::write(path, format!("{summary:#}\n")) { eprintln!("horizon-bots: {path}: {e}"); } diff --git a/crates/horizon-client/Cargo.toml b/crates/horizon-client/Cargo.toml index 29a02553f..1a7a4d25c 100644 --- a/crates/horizon-client/Cargo.toml +++ b/crates/horizon-client/Cargo.toml @@ -10,6 +10,8 @@ description = "Horizon native development client" [dependencies] horizon-assets = { path = "../horizon-assets" } horizon-core = { path = "../horizon-core" } +# The server thread ahead of the workers (MinecraftServer.spin's Thread.setPriority). +horizon-threads = { path = "../horizon-threads" } horizon-world = { path = "../horizon-world" } horizon-protocol = { path = "../horizon-protocol" } # server.properties, eula.txt and the user lists, shared with horizon-server. @@ -31,33 +33,54 @@ toml = "1.1" naga = { version = "30.0.1", features = ["wgsl-in"] } # Java (OptiFine/Iris) shader packs, translated to WGSL. horizon-shaderpack = { path = "../horizon-shaderpack" } -cpal = "0.16" +# Sound (ALSA on Linux); the `audio` feature. +cpal = { version = "0.16", optional = true } symphonia = { version = "0.5", default-features = false, features = ["ogg", "vorbis"] } miniz_oxide = "0.8" ab_glyph = "0.2.32" log = "0.4" chrono = { version = "0.4.42", default-features = false, features = ["clock", "std"] } -flate2 = { version = "1.1.10", default-features = false, features = ["rust_backend"] } +flate2 = { version = "1.1.10", default-features = false, features = ["zlib-rs", "runtime_detection"] } horizon-theme = { path = "../horizon-theme" } # Opt-in data collection: the game keeps, the launcher sends (docs/telemetry.md). horizon-telemetry = { path = "../horizon-telemetry", default-features = false } horizon-bench = { path = "../horizon-bench" } arboard = { version = "3.6.1", default-features = false, features = ["wayland-data-control"] } zip = { version = "2", default-features = false, features = ["deflate"] } -# Controllers (evdev + libudev on Linux, IOKit on macOS, Windows.Gaming.Input). -gilrs = "0.11" +# Controllers (evdev + libudev on Linux, IOKit on macOS, Windows.Gaming.Input); the `controllers` feature. +gilrs = { version = "0.11", optional = true } # The DualSense's rumble, adaptive triggers and lights over HID (gilrs only reads it). -horizon-dualsense = { path = "../horizon-dualsense" } +horizon-dualsense = { path = "../horizon-dualsense", optional = true } # HTTPS to the Minecraft services (online-mode session join). ureq = { version = "2", default-features = false, features = ["tls"] } +[target.'cfg(unix)'.dependencies] +# The server thread's CPU time per tick (`HORIZON_SPIKES`, spans.rs). +rustix = { version = "1", default-features = false, features = ["std", "time"] } + +[target.'cfg(windows)'.dependencies] +# DirectX 12, where Vulkan has no adapter or device, or by choice (`graphicsApi`, graphics.rs). +# Shaders compile with FXC (d3dcompiler_47.dll, part of Windows), or DXC when dxcompiler.dll is +# on the PATH. +wgpu = { version = "30.0.1", default-features = false, features = ["dx12"] } + [target.'cfg(windows)'.build-dependencies] # The icon resource in the .exe (build.rs). winresource = "0.1" [features] +default = ["audio", "controllers"] +# Sound output. Without it (and `controllers`) the program links neither libasound nor libudev on +# Linux: the server build (`--no-default-features`, released as horizon-engine; docs/server.md). +audio = ["dep:cpal"] +# Controllers: gilrs, and the DualSense's own output. Without it only `horizon-ctl pad`'s virtual pad. +controllers = ["dep:gilrs", "dep:horizon-dualsense"] # Records the call sites of mob random draws (`mob_ai_probe` traces). rng-trace = ["horizon-core/rng-trace"] [lints] workspace = true + +[dev-dependencies] +# Every WGSL shader translated as wgpu's DirectX 12 backend does (gfx.rs tests). +naga = { version = "30.0.1", features = ["wgsl-in", "hlsl-out"] } diff --git a/crates/horizon-client/examples/arrow_probe.rs b/crates/horizon-client/examples/arrow_probe.rs index 20c0883e3..63ff2df32 100644 --- a/crates/horizon-client/examples/arrow_probe.rs +++ b/crates/horizon-client/examples/arrow_probe.rs @@ -58,11 +58,12 @@ fn main() -> Result<()> { let root = PathBuf::from(&args[0]); let dir = PathBuf::from(&args[1]); let assets = Assets::load(&root)?; - let mut scene = World::test_scene(&assets, 1)?; + let render = assets.reload(&root)?; + let mut scene = World::test_scene(&assets, &render, 1)?; fs::write( dir.join("scene.json"), serde_json::to_vec( - &serde_json::json!({"radius":1,"resident_chunks":scene.chunks.len(),"model_gallery":assets.model_gallery}), + &serde_json::json!({"radius":1,"resident_chunks":scene.chunks.len(),"model_gallery":render.model_gallery}), )?, )?; if args[2] == "--describe" { diff --git a/crates/horizon-client/examples/auto_jump_probe.rs b/crates/horizon-client/examples/auto_jump_probe.rs index e4a9848e8..d8dee95df 100644 --- a/crates/horizon-client/examples/auto_jump_probe.rs +++ b/crates/horizon-client/examples/auto_jump_probe.rs @@ -31,13 +31,14 @@ fn main() -> Result<()> { }; let root = PathBuf::from(root); let assets = Assets::load(&root)?; - let mut scene = World::test_scene(&assets, 1)?; + let render = assets.reload(&root)?; + let mut scene = World::test_scene(&assets, &render, 1)?; let binding = collisions::load(&root, assets.registry)?; let resources = MotionResources::load(&root, &assets)?; fs::write( scene_json, serde_json::to_vec(&serde_json::json!({ - "radius": 1, "resident_chunks": scene.chunks.len(), "model_gallery": assets.model_gallery, + "radius": 1, "resident_chunks": scene.chunks.len(), "model_gallery": render.model_gallery, }))?, )?; let mut out = BufWriter::new(fs::File::create(output)?); diff --git a/crates/horizon-client/examples/block_entity_tickers_fixture.rs b/crates/horizon-client/examples/block_entity_tickers_fixture.rs new file mode 100644 index 000000000..f08c333f6 --- /dev/null +++ b/crates/horizon-client/examples/block_entity_tickers_fixture.rs @@ -0,0 +1,99 @@ +//! The Rust side of tools/test_block_entity_tickers_parity.py: the ops of +//! `VanillaBlockEntityTickerOracle` on `BlockEntities`, as `World` drives it +//! (a state write calls `state_changed`), printing the tick order. +use horizon_client::{ + block_entities::{BlockEntities, Pos, Ticking}, + containers::BlockEntity, +}; +use std::collections::HashMap; + +/// State ids of the fixture: 0 air, 1 a block whose block entity ticks, 2 a +/// jukebox without a record, 3 a block entity that never ticks (a chest). +fn state(name: &str) -> u32 { + match name { + "chest" | "barrel" => 3, + "jukebox" => 2, + _ => 1, + } +} + +fn entity(name: &str) -> BlockEntity { + if name.starts_with("jukebox") { + // The game makes a jukebox's block entity when a record goes in. + return BlockEntity::Jukebox(Default::default()); + } + BlockEntity::for_block(&format!("minecraft:{name}")) + .unwrap_or_else(|| panic!("no block entity for {name}")) +} + +fn main() { + let args: Vec = std::env::args().collect(); + let input = std::fs::read_to_string(&args[1]).expect("input"); + let mut out = String::new(); + let mut entities = BlockEntities::default(); + let mut states: HashMap = HashMap::new(); + let mut kinds: HashMap = HashMap::new(); + let table = vec![Ticking::No, Ticking::Yes, Ticking::Idle, Ticking::No]; + for line in input.lines() { + let w: Vec<&str> = line.split_whitespace().collect(); + let pos = |i: usize| -> Pos { + let n = |j: usize| w[i + j].parse::().expect("coordinate"); + (n(0), n(1), n(2)) + }; + match w[0] { + "reset" => { + entities = BlockEntities::default(); + entities.set_states(table.clone()); + states.clear(); + kinds.clear(); + } + "load" => { + let mut chunk = Vec::new(); + for i in (3..w.len()).step_by(4) { + let p = pos(i + 1); + states.insert(p, state(w[i])); + kinds.insert(p, w[i].to_owned()); + chunk.push((p, entity(w[i]))); + } + entities.load_chunk(chunk); + } + "place" => { + let p = pos(2); + // World::set, then the game adds the block entity. + let old = states.insert(p, state(w[1])).unwrap_or(0); + kinds.insert(p, w[1].to_owned()); + entities.state_changed(p, old, state(w[1])); + entities.insert(p, entity(w[1])); + } + "replace" => { + let p = pos(1); + entities.insert(p, entity(&kinds[&p])); + } + "remove" => { + let p = pos(1); + entities.remove(&p); + let old = states.insert(p, 0).unwrap_or(0); + entities.state_changed(p, old, 0); + } + "record" => { + let p = pos(1); + let s = if w[4] == "1" { 1 } else { 2 }; + let old = states.insert(p, s).unwrap_or(0); + entities.state_changed(p, old, s); + } + "unload" => { + let c = (w[1].parse::().unwrap(), w[2].parse::().unwrap()); + entities.retain(|p, _| (p.0 >> 4, p.2 >> 4) != c); + } + "tick" => { + out.push_str("tick"); + for (p, _) in entities.tickers(|p| states.get(&p).copied().unwrap_or(0)) { + out.push_str(&format!(" {},{},{}", p.0, p.1, p.2)); + } + out.push('\n'); + } + other => panic!("unknown op {other}"), + } + } + std::fs::write(&args[2], out).expect("output"); +} diff --git a/crates/horizon-client/examples/block_particles_fixture.rs b/crates/horizon-client/examples/block_particles_fixture.rs index e9c207a7e..63ef242b7 100644 --- a/crates/horizon-client/examples/block_particles_fixture.rs +++ b/crates/horizon-client/examples/block_particles_fixture.rs @@ -12,9 +12,11 @@ fn main() -> Result<(), Box> { let args: Vec<_> = std::env::args().collect(); let root = Path::new(&args[1]); let assets = Assets::load(root)?; + let render = assets.reload(root)?; let facts = BlockFacts::load(root, assets.registry)?; let tables = MeshTables::new( &assets, + &render, &facts, models::load_visibility(root, assets.registry)?, )?; diff --git a/crates/horizon-client/examples/breaking_fixture.rs b/crates/horizon-client/examples/breaking_fixture.rs index ec8f7a68b..2016fd613 100644 --- a/crates/horizon-client/examples/breaking_fixture.rs +++ b/crates/horizon-client/examples/breaking_fixture.rs @@ -18,9 +18,11 @@ fn main() -> Result<(), Box> { let args: Vec<_> = std::env::args().collect(); let root = Path::new(&args[1]); let assets = Assets::load(root)?; + let render = assets.reload(root)?; let facts = BlockFacts::load(root, assets.registry)?; let tables = MeshTables::new( &assets, + &render, &facts, models::load_visibility(root, assets.registry)?, )?; @@ -33,7 +35,7 @@ fn main() -> Result<(), Box> { let mut vertices = Vec::new(); let mut translucent = false; if facts.get(state).render_shape == RenderShape::Model { - let models = assets + let models = render .model_catalog .collect(state, BlockPos::new(p[0], p[1], p[2]))?; let offset = tables.offset(state, p[0], p[2]).unwrap_or([0.0; 3]); diff --git a/crates/horizon-client/examples/chunk_bench.rs b/crates/horizon-client/examples/chunk_bench.rs index 6ad2cbd99..be619245f 100644 --- a/crates/horizon-client/examples/chunk_bench.rs +++ b/crates/horizon-client/examples/chunk_bench.rs @@ -31,10 +31,12 @@ fn main() -> Result<()> { }; let (seed, size) = (flag("--seed", 12345), flag("--size", 8) as i32); let assets = Assets::load(&root)?; + let render = assets.reload(&root)?; let facts = BlockFacts::load(&root, assets.registry)?; let biomes = Biomes::load(&root)?; let tables = MeshTables::new( &assets, + &render, &facts, models::load_visibility(&root, assets.registry)?, )?; @@ -132,6 +134,7 @@ fn main() -> Result<()> { let mut mesher = Mesher { nether_shading: false, assets: &assets, + render: &render, tables: &tables, biomes: &biomes, visibility: tables.visibility.clone(), diff --git a/crates/horizon-client/examples/game_loop_probe.rs b/crates/horizon-client/examples/game_loop_probe.rs index 56da454d9..66a428162 100644 --- a/crates/horizon-client/examples/game_loop_probe.rs +++ b/crates/horizon-client/examples/game_loop_probe.rs @@ -233,22 +233,32 @@ fn mouse(c: &Value, out: &mut impl Write) -> Result<()> { fn scroll(c: &Value, out: &mut impl Write) -> Result<()> { let mut handler = MouseLook::default(); let mut slot = c["slot"].as_i64().unwrap() as i32; + let spectator = c["spectator"].as_bool().unwrap_or(false); + let mut speed = 0.05; // OptionInstance.set quantises the wheel sensitivity. let mut file = OptionsFile::default(); file.set("mouseWheelSensitivity", V::Double(db(&c["sens"]))); let sensitivity = file.double("mouseWheelSensitivity"); writeln!(out, "scroll")?; for op in c["ops"].as_array().unwrap() { - if let Some((wheel, _)) = + if let Some((wheel, (_, y))) = handler.on_scroll(db(&op[0]), db(&op[1]), b(&c["discrete"]), sensitivity) { - slot = horizon_client::mouse::ScrollWheelHandler::next_selection( - f64::from(wheel), - slot, - 9, - ); + if spectator { + speed = horizon_client::mouse::spectator_flying_speed(speed, y); + } else { + slot = horizon_client::mouse::ScrollWheelHandler::next_selection( + f64::from(wheel), + slot, + 9, + ); + } + } + if spectator { + writeln!(out, "{slot} {}", fb(speed))?; + } else { + writeln!(out, "{slot}")?; } - writeln!(out, "{slot}")?; } Ok(()) } diff --git a/crates/horizon-client/examples/interaction_input_fixture.rs b/crates/horizon-client/examples/interaction_input_fixture.rs new file mode 100644 index 000000000..bb408dd88 --- /dev/null +++ b/crates/horizon-client/examples/interaction_input_fixture.rs @@ -0,0 +1,122 @@ +use horizon_client::interaction_input::{ + Button, InteractionInput, InteractionWorld, TargetUseWorld, use_on_target, +}; +use serde_json::Value; +use std::io::{BufRead, Write}; + +struct World { + using: bool, + destroying: bool, + entity: bool, + dig_on_attack: bool, + begin_using: bool, + delay: u32, + events: Vec, + passing: bool, +} +impl InteractionWorld for World { + fn using_item(&self) -> bool { + self.using + } + fn release_using(&mut self) { + self.events.push("release".into()); + self.using = false; + } + fn start_attack(&mut self) -> bool { + self.events.push("attack".into()); + if !self.entity { + self.destroying = self.dig_on_attack; + } + false + } + fn start_use(&mut self) { + if self.destroying { + return; + } + self.delay = 4; + let entity = self.entity; + use_on_target(self, entity); + } + fn pick(&mut self) { + self.events.push("pick".into()); + } + fn use_delay(&self) -> u32 { + self.delay + } + fn continue_attack(&mut self, down: bool) { + if self.using { + return; + } + let down = down && !self.entity; + self.events.push(format!("continue {down}")); + if !down { + self.destroying = false; + } + } +} + +impl TargetUseWorld for World { + fn use_entity(&mut self) -> bool { + self.events + .push(if self.passing { "entity" } else { "use" }.into()); + if self.passing { + false + } else { + self.using = self.begin_using; + true + } + } + fn use_block(&mut self) -> bool { + self.events + .push(if self.passing { "block" } else { "use" }.into()); + if self.passing { + false + } else { + self.using = self.begin_using; + true + } + } + fn use_item(&mut self) { + self.events.push("item".into()); + self.using = self.begin_using; + } +} + +fn main() -> Result<(), Box> { + let args: Vec<_> = std::env::args().collect(); + let input = std::io::BufReader::new(std::fs::File::open(&args[1])?); + let mut out = std::io::BufWriter::new(std::fs::File::create(&args[2])?); + for (i, line) in input.lines().enumerate() { + let row: Value = serde_json::from_str(&line?)?; + let flag = |key: &str| row[key].as_bool().unwrap(); + let mut world = World { + using: flag("using"), + destroying: flag("destroying"), + entity: flag("entity"), + dig_on_attack: flag("dig_on_attack"), + begin_using: flag("begin_using"), + delay: row["delay"].as_u64().unwrap() as u32, + events: vec![], + passing: row["passing"].as_bool().unwrap_or(false), + }; + let mut input = InteractionInput::default(); + for button in row["clicks"].as_array().unwrap() { + input.press(match button.as_u64().unwrap() { + 0 => Button::Attack, + 1 => Button::Use, + _ => Button::Pick, + }); + } + input.tick(&mut world, flag("attack_down"), flag("use_down")); + writeln!(out, "== {i}")?; + for event in world.events { + writeln!(out, "{event}")?; + } + writeln!( + out, + "state {} {} {}", + world.using, world.destroying, world.delay + )?; + } + Ok(()) +} diff --git a/crates/horizon-client/examples/lan_flat.rs b/crates/horizon-client/examples/lan_flat.rs index 16fc36b95..283c3a22b 100644 --- a/crates/horizon-client/examples/lan_flat.rs +++ b/crates/horizon-client/examples/lan_flat.rs @@ -22,12 +22,7 @@ fn main() -> Result<(), Box> { let port: u16 = args.next().ok_or("port")?.parse()?; let assets = Assets::load(&root)?; let facts = BlockFacts::load(&root, assets.registry)?; - let state = |name: &str| { - assets - .registry - .default_state(name) - .map_or(0, |s| s.0 as u16) - }; + let state = |name: &str| assets.registry.default_state(name).map_or(0, |s| s.0); let sync = Arc::new(SyncData::load(&root)?); let biomes: Vec = sync .dynamic @@ -106,6 +101,8 @@ fn main() -> Result<(), Box> { sprinting: false, equipment: Default::default(), health: 20.0, + attributes: Vec::new(), + living_flags: 0, }; loop { world.game_time += 1; @@ -121,6 +118,7 @@ fn main() -> Result<(), Box> { clock_rate: 1.0, tick_rate: 20.0, tick_frozen: false, + collision: None, }; for event in hub.tick(&mut ctx) { match event { @@ -132,7 +130,9 @@ fn main() -> Result<(), Box> { eprintln!("[lan] {packet:?}"); } } - HostEvent::Chat(line) => println!("{line}"), + HostEvent::Chat { line, .. } | HostEvent::Warn(line) | HostEvent::Error(line) => { + println!("{line}") + } HostEvent::Left(_) => {} } } diff --git a/crates/horizon-client/examples/light_parity.rs b/crates/horizon-client/examples/light_parity.rs index afc3e3156..f660189a2 100644 --- a/crates/horizon-client/examples/light_parity.rs +++ b/crates/horizon-client/examples/light_parity.rs @@ -206,7 +206,7 @@ fn incremental( sky_light: bool, edits: usize, ) { - use horizon_client::light::relight; + use horizon_client::light::{LightChanges, relight}; use std::sync::Arc; let mut world: HashMap<(i32, i32), Arc> = HashMap::new(); let inner: Vec<(i32, i32)> = { @@ -232,7 +232,7 @@ fn incremental( c.lit = true; world.insert((x, z), Arc::new(c)); } - let palette: Vec = [ + let palette: Vec = [ "minecraft:air", "minecraft:stone", "minecraft:torch", @@ -242,7 +242,7 @@ fn incremental( "minecraft:glass", ] .iter() - .filter_map(|n| registry.default_state(n).map(|s| s.0 as u16)) + .filter_map(|n| registry.default_state(n).map(|s| s.0)) .chain( facts .states @@ -257,7 +257,7 @@ fn incremental( ] .contains(&&*f.name) }) - .map(|(i, _)| i as u16) + .map(|(i, _)| i as u32) .step_by(3), ) .collect(); @@ -285,7 +285,14 @@ fn incremental( if old == state || !tables.differs(old, state) { continue; } - relight(&mut world, tables, sky_light, &[(x, y, z)], false); + relight( + &mut world, + tables, + sky_light, + &[(x, y, z)], + false, + &mut LightChanges::default(), + ); for dz in -1..=1 { for dx in -1..=1 { let (kx, kz) = (cx + dx, cz + dz); diff --git a/crates/horizon-client/examples/mob_ai_probe.rs b/crates/horizon-client/examples/mob_ai_probe.rs index 087fd3edf..2a4969221 100644 --- a/crates/horizon-client/examples/mob_ai_probe.rs +++ b/crates/horizon-client/examples/mob_ai_probe.rs @@ -999,6 +999,7 @@ fn main() -> horizon_client::Result<()> { hero: false, uuid: PLAYER_UUID, allies: Default::default(), + remote: false, } }); let mut scratch = LegacyRandom::new(0); @@ -1023,6 +1024,7 @@ fn main() -> horizon_client::Result<()> { collision: &collision, data: &data, player: player.clone(), + nearest_player: player.as_ref().filter(|p| !p.spectator).map(|p| p.position), others: $snapshot, others_grid: $grid, daylight: (world.day_time.rem_euclid(24000)) < 12000, diff --git a/crates/horizon-client/examples/model_library_bench.rs b/crates/horizon-client/examples/model_library_bench.rs index d3aba51e0..2089e39e1 100644 --- a/crates/horizon-client/examples/model_library_bench.rs +++ b/crates/horizon-client/examples/model_library_bench.rs @@ -56,10 +56,11 @@ fn main() -> horizon_client::Result<()> { let initial = LIVE.load(Relaxed); let start = Instant::now(); let assets = Assets::load(root)?; + let render = assets.reload(root)?; let block_ms = start.elapsed().as_secs_f64() * 1000.; let block_retained = LIVE.load(Relaxed) - initial; let start_items = Instant::now(); - let items = ItemRenderer::load_with_library(root, assets.model_library.clone())?; + let items = ItemRenderer::load_with_library(root, render.model_library.clone())?; let item_ms = start_items.elapsed().as_secs_f64() * 1000.; let total_ms = start.elapsed().as_secs_f64() * 1000.; let retained = LIVE.load(Relaxed) - initial; @@ -74,7 +75,7 @@ fn main() -> horizon_client::Result<()> { horizon_core::position::BlockPos::new(0, 0, 0), horizon_core::position::BlockPos::new(-17, 65, 31), ] { - if let Ok(selected) = assets.model_catalog.collect(id, pos) { + if let Ok(selected) = render.model_catalog.collect(id, pos) { for (variant, model) in selected { format!("{variant:?}").hash(&mut hash); model.ambient_occlusion.hash(&mut hash); diff --git a/crates/horizon-client/examples/options_probe.rs b/crates/horizon-client/examples/options_probe.rs index 1cdf8c8fb..f186af994 100644 --- a/crates/horizon-client/examples/options_probe.rs +++ b/crates/horizon-client/examples/options_probe.rs @@ -1,7 +1,10 @@ #![forbid(unsafe_code)] //! Loads and saves option files like `Options.load`/`save`, for the //! differential harness (tools/test_options_parity.py). -use horizon_client::{Result, keybinds, options_file::OptionsFile}; +use horizon_client::{ + Result, keybinds, + options_file::{OptionsFile, V}, +}; use std::{fs, io::Write}; fn main() -> Result<()> { @@ -14,11 +17,16 @@ fn main() -> Result<()> { let mut first = true; for path in names { let name = path.file_name().unwrap().to_string_lossy().into_owned(); - let file = if name == "absent.txt" { + let mut file = if name == "absent.txt" { OptionsFile::default() } else { OptionsFile::parse(&String::from_utf8_lossy(&fs::read(&path)?)) }; + // Horizon extends the slider to 64; vanilla rejects 33..64 to its default. + // Compare that deliberate extension as vanilla would serialize it. + if file.int("renderDistance") > 32 { + file.set("renderDistance", V::Int(12)); + } write!(out, "== {name}\n{}", file.serialize())?; if first { first = false; diff --git a/crates/horizon-client/examples/player_sync_fixture.rs b/crates/horizon-client/examples/player_sync_fixture.rs new file mode 100644 index 000000000..b50a672af --- /dev/null +++ b/crates/horizon-client/examples/player_sync_fixture.rs @@ -0,0 +1,72 @@ +use horizon_client::player_sync::{PositionSender, teleport_velocity}; +use horizon_core::geometry::Vec3; +use horizon_protocol::packets::play::{PlayerPosition, Serverbound as S}; +use serde_json::Value; +use std::io::{BufRead, Write}; + +fn number(value: &Value) -> f64 { + f64::from_bits(u64::from_str_radix(value.as_str().unwrap(), 16).unwrap()) +} + +fn main() -> Result<(), Box> { + let args: Vec<_> = std::env::args().collect(); + let input = std::io::BufReader::new(std::fs::File::open(&args[1])?); + let mut out = std::io::BufWriter::new(std::fs::File::create(&args[2])?); + for line in input.lines() { + let row: Value = serde_json::from_str(&line?)?; + if row["kind"] == "movement" { + let mut sender = PositionSender::default(); + for op in row["ops"].as_array().unwrap() { + let pos = std::array::from_fn(|i| number(&op[i])); + let rot = (number(&op[3]) as f32, number(&op[4]) as f32); + if let Some(packet) = + sender.update(pos, rot, op[5].as_bool().unwrap(), op[6].as_bool().unwrap()) + { + let name = match packet { + S::MovePlayerPos(_) => "pos", + S::MovePlayerPosRot(_) => "posrot", + S::MovePlayerRot(_) => "rot", + S::MovePlayerStatus(_) => "status", + _ => unreachable!(), + }; + write!(out, "{name} ")?; + for byte in &packet.encode()[1..] { + write!(out, "{byte:02x}")?; + } + writeln!(out)?; + } else { + writeln!(out, "-")?; + } + } + } else { + let vector = |key: &str| { + Vec3::new( + number(&row[key][0]), + number(&row[key][1]), + number(&row[key][2]), + ) + }; + let rotation = |key: &str| (number(&row[key][0]) as f32, number(&row[key][1]) as f32); + let old = rotation("old"); + let new = rotation("new"); + let delta = vector("delta"); + let packet = PlayerPosition { + teleport: 0, + position: [0.0; 3], + delta: [delta.x, delta.y, delta.z], + yaw: new.0, + pitch: new.1, + relatives: row["flags"].as_i64().unwrap() as i32, + }; + let v = teleport_velocity(vector("velocity"), old, new, &packet); + writeln!( + out, + "{:016x} {:016x} {:016x}", + v.x.to_bits(), + v.y.to_bits(), + v.z.to_bits() + )?; + } + } + Ok(()) +} diff --git a/crates/horizon-client/examples/portal_scan_bench.rs b/crates/horizon-client/examples/portal_scan_bench.rs new file mode 100644 index 000000000..158b6a440 --- /dev/null +++ b/crates/horizon-client/examples/portal_scan_bench.rs @@ -0,0 +1,263 @@ +//! The `entity_portals` section scan only: `may_hold_portal` over synthetic +//! entity reaches, with the per-tick palette map against the resident cache. +//! Run with prepared client resources: +//! cargo run --profile fast -p horizon-client --example portal_scan_bench -- RESOURCES +use horizon_client::{ + blocks::BlockFacts, + chunk::{Chunk, MIN_Y, SECTIONS, Section}, + fast_hash::FxHashMap, + portal_sections::PortalSections, +}; +use horizon_core::{ + geometry::{Aabb, Vec3}, + position::BlockPos, +}; +use horizon_world::{level::Behaviours, registry::BlockStateId}; +use std::{hint::black_box, path::Path, sync::Arc, time::Instant}; + +/// `Game::swept_portal`'s reach (`Entity.checkInsideBlocks`'s swept box). +fn reach(from: Vec3, to: Vec3, width: f32, height: f32) -> Aabb { + let half = f64::from(width) / 2.0; + let bounds = Aabb::new( + Vec3::new(to.x - half, to.y, to.z - half), + Vec3::new(to.x + half, to.y + f64::from(height), to.z + half), + ) + .deflate(Vec3::new(1.0e-5, 1.0e-5, 1.0e-5)); + bounds + .union_bounds(bounds.move_by(from.subtract(to))) + .inflate(Vec3::new(1.0, 1.0, 1.0)) +} + +fn sections(reach: Aabb) -> (i64, i32, i32, i32, i32, i32, i32) { + let (min, max) = (reach.min_position(), reach.max_position()); + let (min, max) = (BlockPos::containing(min), BlockPos::containing(max)); + let top = SECTIONS as i32 - 1; + let (x0, x1) = (min.x >> 4, max.x >> 4); + let (z0, z1) = (min.z >> 4, max.z >> 4); + let (y0, y1) = ( + ((min.y - MIN_Y) >> 4).clamp(0, top), + ((max.y - MIN_Y) >> 4).clamp(0, top), + ); + let count = (i64::from(x1) - i64::from(x0) + 1) + * (i64::from(y1) - i64::from(y0) + 1) + * (i64::from(z1) - i64::from(z0) + 1); + (count, x0, x1, y0, y1, z0, z1) +} + +/// The original `Game::may_hold_portal`: per-tick `seen`, palette per section. +fn may_hold_portal_baseline( + reach: Aabb, + world: &FxHashMap<(i32, i32), Arc>, + seen: &mut FxHashMap<(i32, i32, i32), bool>, + behaviours: &Behaviours, +) -> bool { + let (count, x0, x1, y0, y1, z0, z1) = sections(reach); + if count > 64 { + return true; + } + (x0..=x1).any(|cx| { + (z0..=z1).any(|cz| { + (y0..=y1).any(|sy| { + *seen.entry((cx, sy, cz)).or_insert_with(|| { + world + .get(&(cx, cz)) + .and_then(|c| c.sections.get(sy as usize)) + .is_some_and(|s| { + s.blocks + .maybe_has(|v| behaviours.portal_axis(BlockStateId(v)).is_some()) + }) + }) + }) + }) + }) +} + +/// The cached `Game::may_hold_portal`: one mask per resident chunk. +fn may_hold_portal_cached( + reach: Aabb, + world: &FxHashMap<(i32, i32), Arc>, + cache: &mut PortalSections, + behaviours: &Behaviours, +) -> bool { + let (count, x0, x1, y0, y1, z0, z1) = sections(reach); + if count > 64 { + return true; + } + let mut y_mask = 0u32; + for sy in y0..=y1 { + y_mask |= 1 << sy; + } + (x0..=x1).any(|cx| { + (z0..=z1).any(|cz| { + world.get(&(cx, cz)).is_some_and(|chunk| { + cache.get((cx, cz), chunk, |v| { + behaviours.portal_axis(BlockStateId(v)).is_some() + }) & y_mask + != 0 + }) + }) + }) +} + +fn main() -> horizon_client::Result<()> { + let root = std::env::args() + .nth(1) + .ok_or("expected prepared client resource directory")?; + let start = Instant::now(); + let registry = horizon_world::level::data::load_registry(Path::new(&root))?; + let (facts, behaviours) = BlockFacts::load_pair(Path::new(&root), ®istry)?; + println!( + "resource_load_ms={:.3}", + start.elapsed().as_secs_f64() * 1000.0 + ); + let len = registry.len() as u32; + let portal: Vec = (0..len) + .filter(|&id| behaviours.portal_axis(BlockStateId(id)).is_some()) + .collect(); + let inactive_states: Vec = (0..len) + .filter(|&id| behaviours.portal_axis(BlockStateId(id)).is_none()) + .collect(); + let _ = &facts; + let portal_state = *portal + .first() + .ok_or("no nether portal block state in the registry")?; + println!("portal_states={}", portal.len()); + + // 500 entities spread over a 17x17 chunk world. Their reaches touch 1-4 + // sections, as an item or a mob standing in or stepping into a portal does. + let grid = 17; + let base = -(grid / 2) * 16; + let entities: Vec<(Vec3, Vec3, f32, f32)> = (0..500) + .map(|i| { + let x = base + (i % grid) * 16 + (i % 5); + let z = base + (i / grid % grid) * 16 + (i % 7); + let y = 64 + (i % 3) * 16; + let from = Vec3::new(f64::from(x), f64::from(y), f64::from(z)); + let to = from.add_vector(Vec3::new(0.1, 0.0, 0.07)); + (from, to, 0.25, 0.25) + }) + .collect(); + let reaches: Vec = entities + .iter() + .map(|&(from, to, w, h)| reach(from, to, w, h)) + .collect(); + + for (name, palette, portals_every) in [ + ("sparse", 1usize, 0usize), + ("dense", 128, 0), + ("global", 300, 0), + ("dense_portal", 128, 5), + ] { + let start = Instant::now(); + let mut world: FxHashMap<(i32, i32), Arc> = FxHashMap::default(); + for czi in 0..grid { + for cxi in 0..grid { + let (cx, cz) = (-(grid / 2) + cxi, -(grid / 2) + czi); + let mut chunk = Chunk::empty(cx, cz); + for (i, section) in chunk.sections.iter_mut().enumerate() { + let values: [u32; 4096] = std::array::from_fn(|j| { + if portals_every > 0 && i % portals_every == 0 && j == 0 { + portal_state + } else { + inactive_states[(j * 7 + i) % palette.min(inactive_states.len())] + } + }); + Arc::make_mut(section).blocks = Section::from_blocks(&values); + } + world.insert((cx, cz), Arc::new(chunk)); + } + } + println!( + "{name} fixture_ms={:.3}", + start.elapsed().as_secs_f64() * 1000.0 + ); + let keys: Vec<_> = world.keys().copied().collect(); + let portal_chunks: Vec<(i32, i32)> = keys + .iter() + .copied() + .filter(|&(cx, cz)| { + world[&(cx, cz)] + .sections + .iter() + .any(|s| s.blocks.maybe_has(|v| v == portal_state)) + }) + .collect(); + println!("{name} portal_chunks={}", portal_chunks.len()); + // The global palette's maybe_has walks the whole ID range, so rescanning + // every chunk each tick (cold/reload/edit_all) is intractable; those rows + // are only meaningful for the palettes whose scans are bounded. + let workloads: &[(&str, usize)] = if name == "global" { + &[("steady", 0), ("edit_1pct", 3)] + } else { + &[ + ("steady", 0), + ("cold", 0), + ("reload", 0), + ("edit_1pct", 3), + ("edit_all", 289), + ] + }; + for &(workload, changed) in workloads { + let mut baseline = Vec::new(); + let mut candidate = Vec::new(); + let mut cache = PortalSections::default(); + let (mut base_hits, mut cand_hits) = (0u64, 0u64); + for sample in 0..8 { + for cached in if sample % 2 == 0 { + [false, true] + } else { + [true, false] + } { + let start = Instant::now(); + let mut result = 0u64; + for tick in 0..300 { + if cached && workload == "cold" { + cache = PortalSections::default(); + } + if cached && workload == "reload" { + cache.clear(); + } + for key in keys.iter().take(changed) { + Arc::make_mut(world.get_mut(key).unwrap()).set( + 0, + MIN_Y, + 0, + inactive_states[tick % inactive_states.len()], + ); + } + let mut seen = FxHashMap::default(); + for reach in &reaches { + let hit = if cached { + may_hold_portal_cached(*reach, &world, &mut cache, &behaviours) + } else { + may_hold_portal_baseline(*reach, &world, &mut seen, &behaviours) + }; + result += hit as u64; + } + } + if cached { + cache = PortalSections::default(); + cand_hits += black_box(result); + } else { + base_hits += black_box(result); + } + if sample > 0 { + let us = start.elapsed().as_secs_f64() * 1e6 / 300.0; + if cached { + candidate.push(us); + } else { + baseline.push(us); + } + } + } + } + assert_eq!( + base_hits, cand_hits, + "{name} {workload}: cached may_hold_portal diverged" + ); + println!("{name} {workload} baseline_us={baseline:.3?} candidate_us={candidate:.3?}"); + } + } + println!("chunk_wrapper_bytes={}", std::mem::size_of::()); + Ok(()) +} diff --git a/crates/horizon-client/examples/random_tick_eligibility_bench.rs b/crates/horizon-client/examples/random_tick_eligibility_bench.rs index 856d7d520..49c32a8f7 100644 --- a/crates/horizon-client/examples/random_tick_eligibility_bench.rs +++ b/crates/horizon-client/examples/random_tick_eligibility_bench.rs @@ -56,16 +56,16 @@ fn main() -> horizon_client::Result<()> { "resource_load_ms={:.3}", start.elapsed().as_secs_f64() * 1000.0 ); - let inactive: Vec = facts + let inactive: Vec = facts .states .iter() .enumerate() .filter(|(_, f)| !f.random_ticks) .take(128) - .map(|(i, _)| i as u16) + .map(|(i, _)| i as u32) .collect(); - let active = registry.default_state("minecraft:grass_block").unwrap().0 as u16; - assert!(facts.get(BlockStateId(u32::from(active))).random_ticks); + let active = registry.default_state("minecraft:grass_block").unwrap().0; + assert!(facts.get(BlockStateId(active)).random_ticks); for (name, palette) in [("sparse", 1), ("dense", 128)] { let start = Instant::now(); let mut chunks = FxHashMap::default(); diff --git a/crates/horizon-client/examples/resource_memory.rs b/crates/horizon-client/examples/resource_memory.rs new file mode 100644 index 000000000..0408b138b --- /dev/null +++ b/crates/horizon-client/examples/resource_memory.rs @@ -0,0 +1,153 @@ +//! Resident memory of each resource loader, in the game's start order: what a +//! larger registry (content mods, docs/modpacks.md) costs, loader by loader. +//! +//! resource_memory RESOURCES +//! +//! RESOURCES is a prepared root or a content-mod root (`.horizon-cache/mod-content`). +//! Prints, after each step, the live heap (a counting allocator: what the +//! step keeps), the heap's peak during the step (what it parsed on the way) +//! and the process RSS (what the allocator holds on to). On macOS it ends +//! with `footprint`'s physical footprint, the number the OS charges: RSS also +//! counts freed pages the allocator marked reusable. +use horizon_client::{ + assets::Assets, biome::Biomes, blocks::BlockFacts, collisions, crafting::Recipes, items::Items, + light::LightTables, mesher::MeshTables, models, motion::MotionResources, + vanillagen::VanillaTerrain, +}; +use std::{ + alloc::{GlobalAlloc, Layout, System}, + sync::atomic::{AtomicUsize, Ordering::Relaxed}, +}; +use std::{path::PathBuf, process::Command}; +type Result = std::result::Result>; + +struct Counting; +static LIVE: AtomicUsize = AtomicUsize::new(0); +static PEAK: AtomicUsize = AtomicUsize::new(0); +// SAFETY: forwards to the system allocator, only counting sizes. +unsafe impl GlobalAlloc for Counting { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + let p = unsafe { System.alloc(layout) }; + if !p.is_null() { + let now = LIVE.fetch_add(layout.size(), Relaxed) + layout.size(); + PEAK.fetch_max(now, Relaxed); + } + p + } + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + unsafe { System.dealloc(ptr, layout) }; + LIVE.fetch_sub(layout.size(), Relaxed); + } + unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new: usize) -> *mut u8 { + let p = unsafe { System.realloc(ptr, layout, new) }; + if !p.is_null() { + if new >= layout.size() { + let now = LIVE.fetch_add(new - layout.size(), Relaxed) + new - layout.size(); + PEAK.fetch_max(now, Relaxed); + } else { + LIVE.fetch_sub(layout.size() - new, Relaxed); + } + } + p + } +} +#[global_allocator] +static ALLOC: Counting = Counting; +const MB: f64 = 1048576.0; + +fn rss_mb() -> f64 { + let out = Command::new("ps") + .args(["-o", "rss=", "-p", &std::process::id().to_string()]) + .output() + .ok(); + out.and_then(|o| String::from_utf8(o.stdout).ok()) + .and_then(|s| s.trim().parse::().ok()) + .map_or(0.0, |kb| kb / 1024.0) +} + +fn main() -> Result<()> { + let root = PathBuf::from( + std::env::args() + .nth(1) + .ok_or("usage: resource_memory RESOURCES")?, + ); + println!( + "{:<24} {:>8} {:>8} {:>10} {:>8}", + "step", "live MB", "+live", "step peak", "RSS MB" + ); + let mut last = 0usize; + let mut step = |name: &str| { + let live = LIVE.load(Relaxed); + let peak = PEAK.swap(live, Relaxed); + println!( + "{name:<24} {:>8.0} {:>+8.0} {:>10.0} {:>8.0}", + live as f64 / MB, + (live as f64 - last as f64) / MB, + peak as f64 / MB, + rss_mb() + ); + last = live; + }; + step("start"); + let registry = horizon_world::level::data::load_registry(&root)?; + step("registry alone"); + drop(registry); + step("registry dropped"); + let assets = Assets::load(&root)?; + let render = assets.reload(&root)?; + step("assets"); + println!( + " sprites: {} layers of {}px, {:.0} MB of pixels", + render.texture_layers, + render.sprite_size, + render.pixels.len() as f64 / MB + ); + // The game decodes gameplay facts once for both tables (BlockFacts::load_pair). + let (facts, behaviours) = BlockFacts::load_pair(&root, assets.registry)?; + step("block facts+behaviours"); + let items = Items::load(&root, assets.registry, &facts)?; + step("items"); + let recipes = Recipes::load(&root, &items)?; + step("recipes"); + let biomes = Biomes::load(&root)?; + step("biomes"); + let light = LightTables::load(&root, assets.registry, &facts)?; + step("light tables"); + let tables = MeshTables::new( + &assets, + &render, + &facts, + models::load_visibility(&root, assets.registry)?, + )?; + step("mesh tables"); + let collision = collisions::load(&root, assets.registry)?; + step("collision"); + let motion = MotionResources::load_with_facts(&root, &assets, &facts)?; + step("motion"); + let terrain = VanillaTerrain::new(&root.join("worldgen"), assets.registry, &biomes, 1234)?; + step("worldgen"); + println!( + "{} block states; live {:.0} MB, RSS {:.0} MB", + facts.states.len(), + LIVE.load(Relaxed) as f64 / MB, + rss_mb() + ); + if let Ok(out) = Command::new("footprint") + .arg(std::process::id().to_string()) + .output() + { + let text = String::from_utf8_lossy(&out.stdout); + for line in text + .lines() + .filter(|l| l.contains("Footprint") || l.contains("phys_footprint")) + { + println!(" {}", line.trim()); + } + } + // Keep everything alive until the end. + drop(( + terrain, behaviours, motion, collision, tables, light, biomes, recipes, items, facts, + assets, + )); + Ok(()) +} diff --git a/crates/horizon-client/examples/save_batch_bench.rs b/crates/horizon-client/examples/save_batch_bench.rs index 11c4f64ed..d35ca8bbb 100644 --- a/crates/horizon-client/examples/save_batch_bench.rs +++ b/crates/horizon-client/examples/save_batch_bench.rs @@ -4,7 +4,7 @@ //! Exact NBT equality is always validated. No GPU, compression or disk I/O timed. use horizon_client::{ biome::Biomes, - chunk::{BLOCK_STRATEGY, Chunk}, + chunk::{Chunk, block_strategy}, containers::BlockEntity, items::ItemStack, save::{self, ChunkExtras}, @@ -64,9 +64,8 @@ fn fixture(per_chunk: usize) -> (Vec>, Entities, Names) { }; let mut template = Chunk::empty(0, 0); for s in &mut template.sections[..8] { - Arc::make_mut(s).blocks = PalettedContainer::new(BLOCK_STRATEGY, 1); + Arc::make_mut(s).blocks = PalettedContainer::new(block_strategy(), 1); } - template.heights = Arc::new([64; 256]); for i in 0..256 { let x = i % 16 - 8; let z = i / 16 - 8; diff --git a/crates/horizon-client/examples/save_bench.rs b/crates/horizon-client/examples/save_bench.rs new file mode 100644 index 000000000..2e3084290 --- /dev/null +++ b/crates/horizon-client/examples/save_bench.rs @@ -0,0 +1,128 @@ +//! The region writer on real chunks, without a window: how long a world's +//! chunks take to be encoded, compressed and written by `SaveDir`, against +//! the same encoding and compression done one chunk after another (what the +//! writer thread did alone before it had helpers). +//! +//! save_bench RESOURCES DIMENSION_DIR [--rounds N] +//! +//! DIMENSION_DIR is a saved dimension folder (`saves/W/dimensions/minecraft/ +//! overworld`); its chunks are read once, then each round saves them all into +//! a scratch folder with `save_later` + `flush`, and times the one-thread +//! baseline. The written chunks are checked byte for byte against the +//! baseline's compressed records. +use horizon_client::{ + assets::Assets, + biome::Biomes, + blocks::BlockFacts, + save::{SaveDir, SaveTables, encode_with}, +}; +use horizon_core::position::ChunkPos; +use horizon_world::{ + compression::Compression, + nbt::Limits, + region::{ChunkRecord, RegionFile}, +}; +use std::{path::PathBuf, sync::Arc, time::Instant}; + +type Result = std::result::Result>; + +fn main() -> Result<()> { + let args: Vec = std::env::args().skip(1).collect(); + let usage = "usage: save_bench RESOURCES DIMENSION_DIR [--rounds N]"; + let root = PathBuf::from(args.first().ok_or(usage)?); + let dimension = PathBuf::from(args.get(1).ok_or(usage)?); + let rounds: usize = args + .iter() + .position(|a| a == "--rounds") + .and_then(|i| args.get(i + 1)) + .and_then(|v| v.parse().ok()) + .unwrap_or(5); + + let assets = Assets::load(&root)?; + let facts = BlockFacts::load(&root, assets.registry)?; + let biomes = Arc::new(Biomes::load(&root)?); + let tables = Arc::new(SaveTables::new(&assets).with_block_entities(&facts)); + let registry = assets.registry; + + let source = SaveDir::new(dimension.clone()); + let _ = source.tables.set(tables.clone()); + let mut chunks = Vec::new(); + for entry in std::fs::read_dir(dimension.join("region"))? { + let path = entry?.path(); + if path.extension().is_none_or(|e| e != "mca") { + continue; + } + let region = RegionFile::open(&path)?; + for (pos, _) in region.chunks() { + if let Some(chunk) = source.load(pos.x, pos.z, registry, &biomes) { + chunks.push(Arc::new(chunk)); + } + } + } + chunks.sort_by_key(|c| (c.x, c.z)); + println!("{} chunks from {}", chunks.len(), dimension.display()); + + let scratch = std::env::temp_dir().join(format!("save_bench_{}", std::process::id())); + // Encoding and compressing on one thread (what the writer did alone), + // then SaveDir's writer with its helpers. + let mut times = [Vec::new(), Vec::new(), Vec::new()]; + let mut written = Vec::new(); + for round in 0..rounds { + let started = Instant::now(); + let tags: Vec<_> = chunks + .iter() + .map(|c| encode_with(c, registry, &biomes, Some(&tables), true)) + .collect(); + times[0].push(started.elapsed().as_secs_f64()); + let started = Instant::now(); + let baseline: Vec = tags + .iter() + .map(|tag| ChunkRecord::from_nbt(tag, Compression::Zlib, 0, Limits::default())) + .collect::>()?; + times[1].push(started.elapsed().as_secs_f64()); + drop(tags); + + let _ = std::fs::remove_dir_all(&scratch); + let save = SaveDir::new(scratch.clone()); + let _ = save.tables.set(tables.clone()); + let started = Instant::now(); + save.save_later(&chunks, registry, &biomes)?; + save.flush(); + times[2].push(started.elapsed().as_secs_f64()); + drop(save); + + if round == 0 { + for (chunk, want) in chunks.iter().zip(&baseline) { + let path = scratch.join(format!("region/r.{}.{}.mca", chunk.x >> 5, chunk.z >> 5)); + let got = RegionFile::open(&path)? + .read(ChunkPos::new(chunk.x, chunk.z))? + .ok_or("a saved chunk is missing")?; + if got.compressed != want.compressed { + return Err(format!("chunk {} {} differs", chunk.x, chunk.z).into()); + } + } + written = baseline.iter().map(|r| r.compressed.len()).collect(); + } + } + let _ = std::fs::remove_dir_all(&scratch); + println!( + "{} KiB compressed, identical to the one-thread records", + written.iter().sum::() / 1024 + ); + let per_chunk = 1000.0 / chunks.len() as f64; + let names = [ + "one thread: encode", + "one thread: compress", + "SaveDir (encode, compress, write)", + ]; + for (name, t) in names.iter().zip(&mut times) { + t.sort_by(f64::total_cmp); + println!( + "{name}: best {:.0} ms, median {:.0} ms of {rounds} ({:.3} ms per chunk at best)", + t[0] * 1000.0, + t[t.len() / 2] * 1000.0, + t[0] * per_chunk + ); + } + Ok(()) +} diff --git a/crates/horizon-client/examples/save_parity.rs b/crates/horizon-client/examples/save_parity.rs index ba2c4b0fa..3954e3bb3 100644 --- a/crates/horizon-client/examples/save_parity.rs +++ b/crates/horizon-client/examples/save_parity.rs @@ -257,9 +257,9 @@ fn main() -> horizon_client::Result<()> { ); Ok(()) } -fn tables_air(assets: &Assets) -> u16 { +fn tables_air(assets: &Assets) -> u32 { assets .registry .default_state("minecraft:air") - .map_or(0, |s| s.0 as u16) + .map_or(0, |s| s.0) } diff --git a/crates/horizon-client/examples/scene_collision_probe.rs b/crates/horizon-client/examples/scene_collision_probe.rs index c1a1e77ad..ec20eee74 100644 --- a/crates/horizon-client/examples/scene_collision_probe.rs +++ b/crates/horizon-client/examples/scene_collision_probe.rs @@ -40,7 +40,8 @@ fn main() -> Result<()> { return Err("radius must be 1..16".into()); } let assets = assets::Assets::load(&resources)?; - let world = world::World::test_scene(&assets, radius)?; + let render = assets.reload(&resources)?; + let world = world::World::test_scene(&assets, &render, radius)?; let binding = collisions::load(&resources, assets.registry)?; let mut scene = collisions::SceneCollisions { world: &world, @@ -50,7 +51,7 @@ fn main() -> Result<()> { fs::write( dir.join("scene.json"), serde_json::to_vec(&serde_json::json!({ - "radius":radius,"resident_chunks":world.chunks.len(),"model_gallery":assets.model_gallery, + "radius":radius,"resident_chunks":world.chunks.len(),"model_gallery":render.model_gallery, }))?, )?; let mut output = std::io::BufWriter::new(fs::File::create(dir.join("rust.txt"))?); diff --git a/crates/horizon-client/examples/scene_motion_probe.rs b/crates/horizon-client/examples/scene_motion_probe.rs index 6caa51463..f4ff07ebf 100644 --- a/crates/horizon-client/examples/scene_motion_probe.rs +++ b/crates/horizon-client/examples/scene_motion_probe.rs @@ -243,13 +243,14 @@ fn main() -> Result<()> { let root = PathBuf::from(&args[0]); let dir = PathBuf::from(&args[1]); let assets = Assets::load(&root)?; - let mut scene = World::test_scene(&assets, 1)?; + let render = assets.reload(&root)?; + let mut scene = World::test_scene(&assets, &render, 1)?; let binding = collisions::load(&root, assets.registry)?; let resources = MotionResources::load(&root, &assets)?; fs::write( dir.join("scene.json"), serde_json::to_vec( - &serde_json::json!({"radius":1,"resident_chunks":scene.chunks.len(),"model_gallery":assets.model_gallery}), + &serde_json::json!({"radius":1,"resident_chunks":scene.chunks.len(),"model_gallery":render.model_gallery}), )?, )?; if args[2] == "--describe" { diff --git a/crates/horizon-client/examples/section_mesh_fixture.rs b/crates/horizon-client/examples/section_mesh_fixture.rs index a42448794..13f1a0f1a 100644 --- a/crates/horizon-client/examples/section_mesh_fixture.rs +++ b/crates/horizon-client/examples/section_mesh_fixture.rs @@ -3,7 +3,7 @@ //! the oracle's canonical form (vanilla ARGB colours and packed light coords). #![forbid(unsafe_code)] use horizon_client::{ - assets::Assets, + assets::{Assets, RenderAssets}, biome::{BiomeInfo, Biomes, GrassModifier}, blocks::BlockFacts, chunk::{Chunk, Light, MIN_Y, index}, @@ -48,6 +48,7 @@ fn chunk_index(x: i32, z: i32) -> Result { fn scene( row: &Value, assets: &Assets, + render: &RenderAssets, tables: &MeshTables, biomes: &mut Biomes, pristine: &[BiomeInfo], @@ -83,7 +84,7 @@ fn scene( for block in row["blocks"].as_array().ok_or("missing blocks")? { let (x, y, z) = (int(&block[0])?, int(&block[1])?, int(&block[2])?); let i = chunk_index(x, z)?; - chunks[i].set(x & 15, y, z & 15, u16::try_from(int(&block[3])?)?); + chunks[i].set(x & 15, y, z & 15, u32::try_from(int(&block[3])?)?); } for cell in row["lights"].as_array().ok_or("missing lights")? { let (x, y, z) = (int(&cell[0])?, int(&cell[1])?, int(&cell[2])?); @@ -127,6 +128,7 @@ fn scene( let mut mesher = Mesher { nether_shading: row["nether"].as_bool().unwrap_or(false), assets, + render, tables, biomes: &*biomes, visibility: tables.visibility.clone(), @@ -159,15 +161,8 @@ fn scene( let vertices: Vec = quad .iter() .map(|v| { - json!([ - v.pos[0], - v.pos[1], - v.pos[2], - v.uv[0], - v.uv[1], - argb(v.color), - light_coords(v.light) - ]) + let ([x, y, z], [u, w]) = (v.pos, v.uv()); + json!([x, y, z, u, w, argb(v.color), light_coords(v.light)]) }) .collect(); quads.push(json!({"layer": layer, "sprite": sprite, "vertices": vertices})); @@ -184,11 +179,12 @@ fn main() -> Result<()> { } let root = PathBuf::from(&args[0]); let assets = Assets::load(&root)?; + let render = assets.reload(&root)?; let facts = BlockFacts::load(&root, assets.registry)?; let mut biomes = Biomes::load(&root)?; let visibility = models::load_visibility(&root, assets.registry)?; - let tables = MeshTables::new(&assets, &facts, visibility)?; - let names: BTreeMap = assets + let tables = MeshTables::new(&assets, &render, &facts, visibility)?; + let names: BTreeMap = render .sprite_layers .iter() .map(|(name, layer)| (*layer, name.as_str())) @@ -222,6 +218,7 @@ fn main() -> Result<()> { let result = scene( &row, &assets, + &render, &tables, &mut biomes, &pristine, diff --git a/crates/horizon-client/examples/streaming_bench.rs b/crates/horizon-client/examples/streaming_bench.rs index f99c594bb..9f8e99899 100644 --- a/crates/horizon-client/examples/streaming_bench.rs +++ b/crates/horizon-client/examples/streaming_bench.rs @@ -144,10 +144,12 @@ fn main() -> Result<()> { let hz = flag("--hz", 240.0); let assets = Arc::new(Assets::load(&root)?); + let render = Arc::new(assets.reload(&root)?); let facts = BlockFacts::load(&root, assets.registry)?; let biomes = Arc::new(Biomes::load(&root)?); let tables = Arc::new(MeshTables::new( &assets, + &render, &facts, models::load_visibility(&root, assets.registry)?, )?); @@ -160,6 +162,7 @@ fn main() -> Result<()> { )?); let context = Arc::new(WorkerContext { assets, + render: render.clone(), tables, biomes, light, diff --git a/crates/horizon-client/examples/tooltip_display_fixture.rs b/crates/horizon-client/examples/tooltip_display_fixture.rs new file mode 100644 index 000000000..ec61159cd --- /dev/null +++ b/crates/horizon-client/examples/tooltip_display_fixture.rs @@ -0,0 +1,40 @@ +//! Tooltip display visibility oracle companion for tools/test_tooltip_display_parity.py. +use horizon_client::{item_tooltip::TooltipDisplay, items::ItemStack}; +use horizon_world::nbt::Tag; +use std::{error::Error, fmt::Write}; + +fn main() -> Result<(), Box> { + let args: Vec = std::env::args().collect(); + let mut output = String::new(); + for line in std::fs::read_to_string(&args[1])?.lines() { + let fields: Vec<&str> = line.split('\t').collect(); + let hide_tooltip: bool = fields[0].parse()?; + let creative: bool = fields[1].parse()?; + let hide_attributes: bool = fields[2].parse()?; + let mut component = Vec::new(); + if hide_tooltip { + component.push(("hide_tooltip", Tag::Byte(1))); + } + if hide_attributes { + component.push(( + "hidden_components", + Tag::List(vec![Tag::String("minecraft:attribute_modifiers".into())]), + )); + } + let mut stack = ItemStack::new("minecraft:diamond_sword", 1); + if !component.is_empty() { + stack + .data_mut() + .set_other("minecraft:tooltip_display", Some(Tag::compound(component))); + } + let display = TooltipDisplay::for_stack(&stack); + writeln!( + output, + "{}\t{}", + display.name_visible(creative), + display.shows("minecraft:attribute_modifiers") + )?; + } + std::fs::write(&args[2], output)?; + Ok(()) +} diff --git a/crates/horizon-client/src/advancements.rs b/crates/horizon-client/src/advancements.rs index 4a2666ded..16028e220 100644 --- a/crates/horizon-client/src/advancements.rs +++ b/crates/horizon-client/src/advancements.rs @@ -175,6 +175,53 @@ fn parse(id: &str, v: &Value) -> Option { }) } +/// `CriterionProgress`'s date as saved, "yyyy-MM-dd HH:mm:ss Z", in UTC. +pub fn date_text(unix_secs: i64) -> String { + let days = unix_secs.div_euclid(86400); + let rem = unix_secs.rem_euclid(86400); + // Civil from days (Howard Hinnant's algorithm). + let z = days + 719_468; + let era = z.div_euclid(146_097); + let doe = z - era * 146_097; + let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146_096) / 365; + let y = yoe + era * 400; + let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); + let mp = (5 * doy + 2) / 153; + let d = doy - (153 * mp + 2) / 5 + 1; + let m = if mp < 10 { mp + 3 } else { mp - 9 }; + let y = if m <= 2 { y + 1 } else { y }; + format!( + "{y:04}-{m:02}-{d:02} {:02}:{:02}:{:02} +0000", + rem / 3600, + rem % 3600 / 60, + rem % 60 + ) +} + +/// A saved date back to milliseconds since the epoch (the network form of +/// `CriterionProgress`), the zone offset applied. +pub fn date_millis(text: &str) -> Option { + let (date, rest) = text.trim().split_once(' ')?; + let (time, zone) = rest.split_once(' ').unwrap_or((rest, "+0000")); + let mut d = date.split('-').map(|v| v.parse::().ok()); + let (y, m, day) = (d.next()??, d.next()??, d.next()??); + let mut t = time.split(':').map(|v| v.parse::().ok()); + let (h, min, sec) = (t.next()??, t.next()??, t.next()??); + let sign = if zone.starts_with('-') { -1 } else { 1 }; + let zone = zone.trim_start_matches(['+', '-']); + let offset = + zone.get(0..2)?.parse::().ok()? * 3600 + zone.get(2..4)?.parse::().ok()? * 60; + // Days from civil (Howard Hinnant's algorithm). + let y = if m <= 2 { y - 1 } else { y }; + let era = y.div_euclid(400); + let yoe = y - era * 400; + let mp = (m + 9) % 12; + let doy = (153 * mp + 2) / 5 + day - 1; + let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; + let days = era * 146_097 + doe - 719_468; + Some((days * 86400 + h * 3600 + min * 60 + sec - sign * offset) * 1000) +} + /// The player's progress: advancement -> criterion -> obtained date. #[derive(Default, Clone)] pub struct Progress { @@ -560,6 +607,29 @@ pub struct Entities<'a> { pub tool: Option<&'a ItemView>, pub block_pos: Option<[i32; 3]>, } +/// Conditions shared by item/entity interactions and thrown-item pickups. +pub fn item_entity( + v: &Value, + player: &EntityView, + target: &EntityView, + stack: &ItemView, + w: &dyn World, +) -> bool { + let pctx = Entities { + this: Some(player), + origin: player.position, + tool: None, + block_pos: None, + }; + let ectx = Entities { + this: Some(target), + ..pctx + }; + conditions(&v["player"], &pctx, w) + && conditions(&v["entity"], &ectx, w) + && item(&v["item"], stack, w) +} + /// A `ContextAwarePredicate`: a list of loot conditions (or one condition). pub fn conditions(v: &Value, ctx: &Entities, w: &dyn World) -> bool { match v { @@ -949,7 +1019,75 @@ mod tests { false } fn in_tag(&self, _: &str, tag: &str, id: &str) -> bool { - tag == "minecraft:stone_tool_materials" && id == "minecraft:cobblestone" + (tag == "minecraft:stone_tool_materials" && id == "minecraft:cobblestone") + || (tag == "minecraft:sulfur_cube_archetype/explosive" && id == "minecraft:tnt") + } + } + #[test] + fn uh_oh_accepts_either_tnt_event_only_for_adult_sulfur_cubes() { + let path = Path::new(env!("CARGO_MANIFEST_DIR")).join( + "../../.local/horizon-client/26.3/data/minecraft/advancement/husbandry/uh_oh.json", + ); + if !path.exists() { + return; + } + let json = serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap(); + let advancement = parse("minecraft:husbandry/uh_oh", &json).unwrap(); + assert_eq!(advancement.criteria.len(), 2); + let player = EntityView { + kind: "minecraft:player".into(), + ..Default::default() + }; + let mut target = EntityView { + kind: "minecraft:sulfur_cube".into(), + ..Default::default() + }; + let mut stack = ItemView { + id: "minecraft:tnt".into(), + count: 1, + ..Default::default() + }; + for criterion in &advancement.criteria { + assert!(item_entity( + &criterion.conditions, + &player, + &target, + &stack, + &Flat + )); + let mut progress = Progress::default(); + progress.grant(&advancement.id, &criterion.name, "test"); + assert!( + progress.done(&advancement), + "either criterion must award Uh Oh" + ); + target.baby = true; + assert!(!item_entity( + &criterion.conditions, + &player, + &target, + &stack, + &Flat + )); + target.baby = false; + target.kind = "minecraft:pig".into(); + assert!(!item_entity( + &criterion.conditions, + &player, + &target, + &stack, + &Flat + )); + target.kind = "minecraft:sulfur_cube".into(); + stack.id = "minecraft:stone".into(); + assert!(!item_entity( + &criterion.conditions, + &player, + &target, + &stack, + &Flat + )); + stack.id = "minecraft:tnt".into(); } } #[test] @@ -1059,4 +1197,15 @@ mod tests { assert_eq!(l[4], Some((1, 2.0))); assert_eq!(l[1], Some((2, 2.0))); } + #[test] + fn saved_dates_read_back_to_their_instant() { + for secs in [0, 951_782_400, 1_792_216_800, 4_107_542_399] { + assert_eq!(date_millis(&date_text(secs)), Some(secs * 1000)); + } + assert_eq!( + date_millis("2026-10-17 07:30:00 +0200"), + date_millis("2026-10-17 05:30:00 +0000") + ); + assert_eq!(date_millis("not a date"), None); + } } diff --git a/crates/horizon-client/src/assets.rs b/crates/horizon-client/src/assets.rs index b8551a059..beafa96fd 100644 --- a/crates/horizon-client/src/assets.rs +++ b/crates/horizon-client/src/assets.rs @@ -2,14 +2,13 @@ use crate::Result; use horizon_world::{ DATA_VERSION, heightmap::HeightmapState, - nbt::Tag, registry::{BlockRegistry, BlockStateId}, worldgen::{tags::TagDefinitions, terrain::TerrainBlockStates}, }; use serde_json::Value; use std::{ collections::BTreeSet, - fs::{self, File}, + fs::File, io::{self, BufReader}, path::Path, sync::Arc, @@ -43,17 +42,46 @@ impl HeightmapState for StateFacts { } #[derive(Clone)] pub struct Properties(Arc>); +#[cfg(test)] +impl Properties { + /// Heightmap facts for tests, per state: (air, has a fluid, blocks + /// motion, blocks motion but is not leaves). State 0 is minecraft:air. + pub(crate) fn for_tests(states: &[(bool, bool, bool, bool)]) -> Self { + Self(Arc::new( + states + .iter() + .enumerate() + .map(|(i, &(air, fluid, motion, no_leaves))| StateFacts { + air, + fluid, + air_block: i == 0, + motion, + no_leaves, + }) + .collect(), + )) + } +} impl TerrainBlockStates for Properties { type State = StateFacts; fn heightmap_state(&self, id: BlockStateId) -> StateFacts { self.0[id.0 as usize] } } +/// The block registry and the facts read from `facts.json` and the block tags: +/// everything world generation and collisions need, with none of the block +/// models or baked sprites. pub struct Assets { /// Leaked once per process: shared by world generation threads. pub registry: &'static BlockRegistry, pub properties: Properties, pub material_states: [BlockStateId; 6], + /// Block names in `#minecraft:blocks_fluid_flow` (fluid flow vectors). + pub blocks_fluid_flow: BTreeSet, +} +/// The block models and their baked sprites, read from the pack at `root` over +/// an existing block registry: everything the mesher and renderer need. +pub struct RenderAssets { pub sprite_size: u32, pub sprite_minimum_size: u32, pub pixels: Vec, @@ -68,30 +96,10 @@ pub struct Assets { pub translucent_layers: Vec, /// Each layer's `MipmapGenerator` settings. pub sprite_mips: Vec, - /// Block names in `#minecraft:blocks_fluid_flow` (fluid flow vectors). - pub blocks_fluid_flow: BTreeSet, } fn invalid(message: &str) -> io::Error { io::Error::new(io::ErrorKind::InvalidData, message) } -fn tag(value: &Value) -> Result { - Ok(match value { - Value::String(value) => Tag::String(value.as_str().into()), - Value::Number(value) => Tag::Int(i32::try_from( - value - .as_i64() - .ok_or_else(|| invalid("invalid registry number"))?, - )?), - Value::Array(values) => Tag::List(values.iter().map(tag).collect::>()?), - Value::Object(values) => Tag::Compound( - values - .iter() - .map(|(name, value)| Ok((name.as_str().into(), tag(value)?))) - .collect::>()?, - ), - _ => return Err(invalid("unsupported registry value").into()), - }) -} impl Assets { pub fn load(root: &Path) -> Result { let manifest: Value = serde_json::from_reader(BufReader::new(File::open( @@ -100,23 +108,8 @@ impl Assets { if manifest["data_version"].as_i64() != Some(i64::from(DATA_VERSION)) { return Err(invalid("resources do not match Horizon's pinned data version").into()); } - let facts: Value = serde_json::from_slice(&fs::read(root.join("facts.json"))?)?; - let states = facts["states"] - .as_array() - .ok_or_else(|| invalid("missing registry state facts"))?; - let registry = BlockRegistry::from_nbt(&Tag::compound([ - ("DataVersion", Tag::Int(DATA_VERSION)), - ( - "states", - Tag::List( - states - .iter() - .map(|state| tag(&state[4])) - .collect::>()?, - ), - ), - ("defaults", tag(&facts["defaults"])?), - ]))?; + let (registry, flags) = + BlockRegistry::from_facts_json_with_flags(File::open(root.join("facts.json"))?)?; let bundle: Value = serde_json::from_reader(BufReader::new(File::open(root.join("block-tags.json"))?))?; let mut definitions = TagDefinitions::default(); @@ -132,7 +125,7 @@ impl Assets { )?; } let built = - definitions.build(|name, _| facts["defaults"].get(name).map(|_| name.to_owned()))?; + definitions.build(|name, _| registry.default_state(name).map(|_| name.to_owned()))?; if !built.missing.is_empty() { return Err(invalid("unresolved block tags").into()); } @@ -158,27 +151,20 @@ impl Assets { .cloned() .collect(); let properties = Properties(Arc::new( - states + flags .iter() - .map(|state| { + .enumerate() + .map(|(i, &(air, fluid))| { + let name = ®istry + .state(BlockStateId(i as u32)) + .ok_or_else(|| invalid("missing registry state"))? + .name; Ok(StateFacts { - air: state[1] - .as_bool() - .ok_or_else(|| invalid("missing air fact"))?, - fluid: state[2] - .as_bool() - .ok_or_else(|| invalid("missing fluid fact"))?, - air_block: state[0].as_str() == Some("minecraft:air"), - motion: motion.contains( - state[0] - .as_str() - .ok_or_else(|| invalid("missing state name"))?, - ), - no_leaves: no_leaves.contains( - state[0] - .as_str() - .ok_or_else(|| invalid("missing state name"))?, - ), + air, + fluid, + air_block: name == "minecraft:air", + motion: motion.contains(name.as_str()), + no_leaves: no_leaves.contains(name.as_str()), }) }) .collect::>()?, @@ -189,34 +175,27 @@ impl Assets { .default_state(&format!("minecraft:{name}")) .ok_or_else(|| invalid("missing test-scene material"))?; } + // Chunks hold every state of this registry: content mods can take it + // past vanilla's 16-bit global palette. + crate::chunk::set_block_states(registry.len()); let registry: &'static BlockRegistry = Box::leak(Box::new(registry)); - Self::with_models( - root, + Ok(Self { registry, properties, material_states, blocks_fluid_flow, - ) + }) } - /// The same blocks with the models and sprites of another resource root + /// The models and sprites of another resource root over these blocks /// (resource packs changed): the registry and block facts come from the /// game data, which resource packs do not touch. - pub fn reload(&self, root: &Path) -> Result { - Self::with_models( - root, - self.registry, - self.properties.clone(), - self.material_states, - self.blocks_fluid_flow.clone(), - ) + pub fn reload(&self, root: &Path) -> Result { + RenderAssets::load(root, self) } - fn with_models( - root: &Path, - registry: &'static BlockRegistry, - properties: Properties, - material_states: [BlockStateId; 6], - blocks_fluid_flow: BTreeSet, - ) -> Result { +} +impl RenderAssets { + pub fn load(root: &Path, assets: &Assets) -> Result { + let registry = assets.registry; let mut sprites = crate::sprites::Sprites::default(); for name in MATERIALS { sprites.insert(root, &format!("minecraft:block/{name}"))?; @@ -240,9 +219,6 @@ impl Assets { } sprites.finish(); Ok(Self { - registry, - properties, - material_states, sprite_size: sprites.size, sprite_minimum_size: sprites.minimum_size, texture_layers: u32::try_from(sprites.count())?, @@ -255,7 +231,6 @@ impl Assets { sprite_layers: sprites.layers, translucent_layers: sprites.translucent, sprite_mips: sprites.mips, - blocks_fluid_flow, }) } } diff --git a/crates/horizon-client/src/audio.rs b/crates/horizon-client/src/audio.rs index 54bd3bda8..250babf22 100644 --- a/crates/horizon-client/src/audio.rs +++ b/crates/horizon-client/src/audio.rs @@ -9,6 +9,7 @@ use crate::sound_engine::{ Sounds, TickContext, }; use crate::sound_events::{Sound, SoundRegistry}; +#[cfg(feature = "audio")] use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use horizon_core::random::{LegacyRandom, RandomSource}; use std::{ @@ -44,6 +45,7 @@ struct Mixer { channels: HashMap, listener: [f64; 3], listener_yaw: f32, + limiter_peak: f32, } /// A sound the player could have heard (`SoundEventListener.onPlaySound`): /// what the subtitle overlay needs. @@ -133,8 +135,14 @@ fn attach(mixer: &Mutex, channel: ChannelId, samples: Samples, looping: b c.looping |= looping; } } +/// The open output stream. A build without the `audio` feature (the server build, which must not +/// need libasound) never has one. +#[cfg(feature = "audio")] +type Stream = cpal::Stream; +#[cfg(not(feature = "audio"))] +type Stream = std::convert::Infallible; pub struct Audio { - _stream: Option, + _stream: Option, pub engine: SoundEngine, backend: MixerBackend, /// Record `Heard` sounds (the subtitle overlay is listening). @@ -332,6 +340,8 @@ fn distance_gain(linear: Option, distance: f32) -> f32 { Some(_) => 0.0, } } +// Only the output stream mixes; a build without one (no `audio` feature) never does. +#[cfg_attr(not(feature = "audio"), allow(dead_code))] fn mix(mixer: &mut Mixer, data: &mut [f32], channels: usize, rate: u32) { data.fill(0.0); let listener = mixer.listener; @@ -397,14 +407,24 @@ fn mix(mixer: &mut Mixer, data: &mut [f32], channels: usize, rate: u32) { c.position += step; } } - for s in data.iter_mut() { - *s = s.clamp(-1.0, 1.0); + // Limit the whole buffer together, preserving waveform and stereo balance + // instead of flattening each overloaded sample; release over 50 ms. + let peak = data.iter().fold(1.0f32, |peak, s| peak.max(s.abs())); + mixer.limiter_peak = mixer.limiter_peak.max(peak); + let release = (-1.0 / (0.05 * rate as f32)).exp(); + for frame in data.chunks_mut(channels) { + let gain = mixer.limiter_peak.recip(); + for s in frame { + *s *= gain; + } + mixer.limiter_peak = (mixer.limiter_peak * release).max(peak); } } /// Why the game should not open an output, if it should not: /// `HORIZON_NO_AUDIO`, or a virtual screen (`vscreen`), where agents and /// harnesses run the game and must stay off the user's speakers. /// `HORIZON_AUDIO=1` opens the output anyway (testing into a null sink). +#[cfg(any(feature = "audio", test))] fn output_disabled(var: impl Fn(&str) -> Option) -> Option { if var("HORIZON_NO_AUDIO").is_some() { return Some("audio off: HORIZON_NO_AUDIO is set".into()); @@ -420,6 +440,7 @@ fn output_disabled(var: impl Fn(&str) -> Option) -> Option>, @@ -440,6 +461,7 @@ fn open_device( stream.play()?; Ok((stream, config)) } +#[cfg(feature = "audio")] fn build_stream>( device: &cpal::Device, config: &cpal::SupportedStreamConfig, @@ -513,7 +535,12 @@ impl Audio { capture: None, } } - fn open(mixer: Arc>) -> Result { + #[cfg(not(feature = "audio"))] + fn open(_: Arc>) -> Result { + Err("this build has no sound output (built without the `audio` feature)".into()) + } + #[cfg(feature = "audio")] + fn open(mixer: Arc>) -> Result { if let Some(reason) = output_disabled(|name| std::env::var_os(name)) { return Err(reason.into()); } @@ -862,6 +889,46 @@ mod tests { assert_eq!(distance_gain(Some(16.0), 40.0), 0.0); } + #[test] + fn overlapping_voices_are_limited_without_flattening_or_changing_stereo_balance() { + let mut m = Mixer::default(); + let wave = vec![0.25, 0.5, 0.75, 1.0, -1.0, -0.75, -0.5, -0.25, 0.0]; + for id in 1..=4 { + m.channels + .insert(id, channel(wave.clone(), None, [-1.0, 0.0, 0.0], false)); + } + let mut data = [0.0; 16]; + mix(&mut m, &mut data, 2, 48_000); + for (frame, sample) in data.as_chunks::<2>().0.iter().zip(wave) { + assert!((frame[0] - sample * 0.4).abs() < 1e-6, "{frame:?}"); + assert!((frame[1] - sample).abs() < 1e-6, "{frame:?}"); + } + assert!(data.iter().all(|s| s.abs() <= 1.0)); + } + + #[test] + fn limiter_leaves_quiet_audio_alone_and_recovers_across_callbacks() { + let mut m = Mixer::default(); + m.channels + .insert(1, channel(vec![0.25; 2], None, [0.0; 3], true)); + let mut data = [0.0; 16]; + mix(&mut m, &mut data, 2, 48_000); + assert_eq!(data, [0.25; 16]); + + for id in 2..=5 { + m.channels + .insert(id, channel(vec![1.0; 2], None, [0.0; 3], true)); + } + mix(&mut m, &mut data, 2, 48_000); + assert!(data.iter().all(|s| s.abs() <= 1.0)); + m.channels.retain(|&id, _| id == 1); + let mut recovery = vec![0.0; 2 * 48_000]; + mix(&mut m, &mut recovery, 2, 48_000); + assert!(recovery[0] > 0.0 && recovery[0] < 0.1); + assert!(recovery.windows(2).all(|w| w[1] >= w[0])); + assert_eq!(*recovery.last().unwrap(), 0.25); + } + #[test] fn sources_play_attenuated_then_stop_or_loop() { let mut m = Mixer::default(); diff --git a/crates/horizon-client/src/auth.rs b/crates/horizon-client/src/auth.rs index 27339259f..a6dfab1ee 100644 --- a/crates/horizon-client/src/auth.rs +++ b/crates/horizon-client/src/auth.rs @@ -232,6 +232,87 @@ pub fn join_server(session: &AuthSession, server_hash: &str) -> Result<(), JoinE } } +/// What the session service says about a player joining this server +/// (MinecraftServicesSessionService.hasJoinedServer). +#[derive(Clone, Debug, PartialEq)] +pub struct JoinedProfile { + pub uuid: u128, + pub properties: Vec, +} + +/// HttpDiscoveryService.buildQuery: `URLEncoder.encode(value, UTF-8)`. +fn url_encode(text: &str) -> String { + let mut out = String::new(); + for b in text.bytes() { + match b { + b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'.' | b'-' | b'*' | b'_' => { + out.push(char::from(b)); + } + b' ' => out.push('+'), + _ => out.push_str(&format!("%{b:02X}")), + } + } + out +} + +/// The session service's verify endpoint, asked by an online-mode server +/// whether `name` joined with `server_hash` (and from `ip`, when the server +/// prevents proxy connections). `Ok(None)`: it didn't, or the service said +/// no; `Err(Unavailable)`: no answer. +pub fn has_joined( + name: &str, + server_hash: &str, + ip: Option, +) -> Result, JoinError> { + let doc = discovery()?; + let url = endpoint(&doc, "session", "verify").ok_or(JoinError::Unavailable)?; + let mut query = format!( + "username={}&serverId={}", + url_encode(name), + url_encode(server_hash) + ); + if let Some(ip) = ip { + query.push_str(&format!("&ip={}", url_encode(&ip.to_string()))); + } + let separator = if url.contains('?') { '&' } else { '?' }; + let body = match agent().get(&format!("{url}{separator}{query}")).call() { + Ok(response) => response.into_string().unwrap_or_default(), + Err(ureq::Error::Status(status, response)) => { + let error = + JoinError::from_response(status, &response.into_string().unwrap_or_default()); + return match error { + JoinError::Unavailable => Err(error), + _ => Ok(None), + }; + } + Err(ureq::Error::Transport(_)) => return Err(JoinError::Unavailable), + }; + Ok(parse_joined(&body)) +} + +/// HasJoinedMinecraftServerResponse: `id` and `properties`; no id is no profile. +pub fn parse_joined(body: &str) -> Option { + let v: serde_json::Value = serde_json::from_str(body).ok()?; + let uuid = parse_uuid(v.get("id")?.as_str()?)?; + let properties = v + .get("properties") + .and_then(|p| p.as_array()) + .into_iter() + .flatten() + .filter_map(|p| { + Some(horizon_protocol::packets::login::Property { + name: p.get("name")?.as_str()?.to_owned(), + value: p.get("value")?.as_str()?.to_owned(), + signature: p + .get("signature") + .and_then(|s| s.as_str()) + .map(str::to_owned), + }) + }) + .collect(); + Some(JoinedProfile { uuid, properties }) +} + #[cfg(test)] mod tests { use super::*; @@ -250,6 +331,20 @@ mod tests { path } + #[test] + fn verify_answers() { + assert_eq!(url_encode("Tester_1"), "Tester_1"); + assert_eq!(url_encode("-5a f"), "-5a+f"); + assert_eq!(url_encode("::1"), "%3A%3A1"); + let body = r#"{"id":"069a79f444e94726a5befca90e38aaf5","name":"Notch","properties":[{"name":"textures","value":"e30=","signature":"c2ln"}]}"#; + let p = parse_joined(body).unwrap(); + assert_eq!(p.uuid, UUID); + assert_eq!(p.properties.len(), 1); + assert_eq!(p.properties[0].signature.as_deref(), Some("c2ln")); + assert_eq!(parse_joined(""), None); + assert_eq!(parse_joined(r#"{"name":"Notch"}"#), None); + } + #[test] fn session_files_are_read_once_and_must_be_private() { let dir = std::env::temp_dir().join(format!("horizon-auth-test-{}", std::process::id())); diff --git a/crates/horizon-client/src/bin/horizon-ctl/commands.rs b/crates/horizon-client/src/bin/horizon-ctl/commands.rs index c5e716a03..8f64705c6 100644 --- a/crates/horizon-client/src/bin/horizon-ctl/commands.rs +++ b/crates/horizon-client/src/bin/horizon-ctl/commands.rs @@ -187,6 +187,10 @@ pub const COMMANDS: &[Cmd] = &[ &[opt("radius", Ty::Num, "search radius in blocks (default 32)"), opt("type", Ty::Str, "entity type filter")], mcp: true, local: false), cmd!("block", Observe, "block X Y Z", "the block state at a position", "", ["horizon-ctl block 10 64 -3"], &BLOCK_XYZ, mcp: true, local: false), + cmd!("light", Observe, "light X1 Y1 Z1 X2 Y2 Z2", "the stored sky and block light of a box", + "At most 32768 blocks per request. One hex digit per block in `sky` and `block`, x fastest, then z, then y; `.` where no chunk is loaded.", + ["horizon-ctl light 0 64 0 15 70 15"], + &[p("x1", Ty::Int, "corner X"), p("y1", Ty::Int, "corner Y"), p("z1", Ty::Int, "corner Z"), p("x2", Ty::Int, "opposite X"), p("y2", Ty::Int, "opposite Y"), p("z2", Ty::Int, "opposite Z")], mcp: true, local: false), cmd!("blocks", Observe, "blocks X1 Y1 Z1 X2 Y2 Z2", "the non-air blocks of a box, with counts per block", "At most 32768 blocks per request.", ["horizon-ctl blocks 0 60 0 15 70 15"], &[p("x1", Ty::Int, "corner X"), p("y1", Ty::Int, "corner Y"), p("z1", Ty::Int, "corner Z"), p("x2", Ty::Int, "opposite X"), p("y2", Ty::Int, "opposite Y"), p("z2", Ty::Int, "opposite Z")], mcp: true, local: false), @@ -339,7 +343,7 @@ pub const COMMANDS: &[Cmd] = &[ cmd!("bench", Scripting, "bench SECONDS", "reset frame stats, wait, reply with the summary", "", ["horizon-ctl bench 10"], &[], mcp: false, local: false), cmd!("stats", Scripting, "stats [reset]", "frame-time summary since the last reset", "", ["horizon-ctl stats"], &[], mcp: false, local: false), cmd!("fly", Scripting, "fly X1 Y1 Z1 X2 Y2 Z2 SECONDS [PITCH [raw]]", "fly the camera along a line, then reply with the frame summary", - "With raw, the reply also lists every frame time in milliseconds (frame_ms).", + "The reply's tick_ms and tick_ms_max are the world's smoothed tick time over the flight (the server thread's when single player runs on one). With raw, the reply also lists every frame time in milliseconds (frame_ms).", ["horizon-ctl fly 0 100 0 500 100 0 20"], &[], mcp: false, local: false), ]; diff --git a/crates/horizon-client/src/bin/horizon-ctl/main.rs b/crates/horizon-client/src/bin/horizon-ctl/main.rs index 2e1b1bb56..27d9d5ba1 100644 --- a/crates/horizon-client/src/bin/horizon-ctl/main.rs +++ b/crates/horizon-client/src/bin/horizon-ctl/main.rs @@ -215,7 +215,12 @@ impl Session { let deadline = Instant::now() + Duration::from_secs_f64(limit.max(0.0)); loop { let status = self.game("status")?; - if !status["player"].is_null() && status["pending_chunks"] == 0 { + // On a server (single player's server thread too), also the + // level around the player. + if !status["player"].is_null() + && status["pending_chunks"] == 0 + && status["server"]["loaded"] != false + { return Ok(status); } if Instant::now() > deadline { diff --git a/crates/horizon-client/src/block_entities.rs b/crates/horizon-client/src/block_entities.rs new file mode 100644 index 000000000..93abb3711 --- /dev/null +++ b/crates/horizon-client/src/block_entities.rs @@ -0,0 +1,395 @@ +//! The world's block entities and the order they tick in: vanilla's +//! `Level.blockEntityTickers`, kept beside the block entity map so a tick +//! visits only the block entities that tick, in vanilla's order, instead of +//! scanning the whole map once per kind. +//! +//! The list follows `LevelChunk.updateBlockEntityTicker` and +//! `Level.tickBlockEntities`: +//! - a block entity whose block state has a ticker gets a slot at the end of +//! the list (through `pendingBlockEntityTickers`, so a slot made during a +//! tick first ticks on the next one); +//! - replacing the block entity at a position (`setBlockEntity`), or changing +//! its block state to another state with a ticker, keeps its slot +//! (`RebindableTickingBlockEntityWrapper.rebind`); +//! - removing it (`removeBlockEntity`), unloading its chunk +//! (`clearAllBlockEntities`) or changing its state to one without a ticker +//! (a jukebox losing its record) frees the slot; a later ticker there is +//! appended again; +//! - a loaded chunk's block entities are added in the iteration order of its +//! `Object2ObjectOpenHashMap` +//! (`registerAllBlockEntitiesAfterLevelLoad`), filled in the order the chunk +//! listed them. +//! +//! Which states tick is a table by state id, built by the game from the block +//! registry; whether a slot is made is decided when the slot is promoted at +//! the start of the next tick, from the block state then. +use crate::containers::BlockEntity; +use crate::fast_hash::{FxHashMap, FxHashSet}; +use std::collections::HashMap; + +pub type Pos = (i32, i32, i32); + +/// What a block state's block entity does each tick (`BlockState.getTicker`). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum Ticking { + /// No ticker, and the block has no block entity that could have one. + #[default] + No, + /// A ticker. + Yes, + /// The block's block entity, but this state has no ticker (a jukebox + /// without a record): a state change to it frees the slot. + Idle, +} + +#[derive(Default)] +pub struct BlockEntities { + map: HashMap, + /// `Level.blockEntityTickers`: positions and the slot each was given. + tickers: Vec<(Pos, u32)>, + /// `LevelChunk.tickersInLevel`: the live slot of each ticking position. + live: FxHashMap, + /// Positions that may need a slot, in the order vanilla would have made + /// it (`pendingBlockEntityTickers`), each with a token; decided at the + /// next promotion from the block state then. + added: Vec<(Pos, u32)>, + /// The token of each position's pending entry: an entry whose token is + /// not here any more was cancelled. + pending: FxHashMap, + next_slot: u32, + /// Whether a slot was freed since the list was last swept: otherwise + /// every slot in it is still live, unless its block entity is `out`. + freed: bool, + /// Positions whose block entity `take` took and `put` has not brought + /// back. + out: FxHashSet, + /// `Ticking` by block state id; empty until the game builds it. + states: Vec, +} + +impl std::ops::Deref for BlockEntities { + type Target = HashMap; + fn deref(&self) -> &Self::Target { + &self.map + } +} + +impl<'a> IntoIterator for &'a BlockEntities { + type Item = (&'a Pos, &'a BlockEntity); + type IntoIter = std::collections::hash_map::Iter<'a, Pos, BlockEntity>; + fn into_iter(self) -> Self::IntoIter { + self.map.iter() + } +} + +/// `Vec3i.hashCode`. +fn java_hash((x, y, z): Pos) -> i32 { + y.wrapping_add(z.wrapping_mul(31)) + .wrapping_mul(31) + .wrapping_add(x) +} +/// `HashCommon.mix`. +fn mix(x: i32) -> usize { + let h = (x as u32).wrapping_mul(0x9E37_79B9); + (h ^ (h >> 16)) as usize +} + +/// The order `values().forEach` visits a `new Object2ObjectOpenHashMap<>()` +/// keyed by these positions, put in this order (repeats keep their first +/// slot): open addressing with linear probing, a rehash to twice the size +/// past three quarters full, and iteration from the last slot down. +pub fn chunk_map_order(positions: impl IntoIterator) -> Vec { + // HashCommon.arraySize(16, .75f), and maxFill of each size. + let mut slots: Vec> = vec![None; 32]; + let max_fill = |n: usize| (n * 3).div_ceil(4).min(n - 1); + let mut size = 0; + for p in positions { + let mask = slots.len() - 1; + let mut at = mix(java_hash(p)) & mask; + let mut known = false; + while let Some(k) = slots[at] { + if k == p { + known = true; + break; + } + at = (at + 1) & mask; + } + if known { + continue; + } + slots[at] = Some(p); + // `if (size++ >= maxFill) rehash(arraySize(size + 1, f))`, which + // puts the keys back from the last slot down. + size += 1; + if size > max_fill(slots.len()) { + let n = ((size + 1) * 4).div_ceil(3).next_power_of_two(); + let old = std::mem::replace(&mut slots, vec![None; n]); + for k in old.into_iter().rev().flatten() { + let mut at = mix(java_hash(k)) & (n - 1); + while slots[at].is_some() { + at = (at + 1) & (n - 1); + } + slots[at] = Some(k); + } + } + } + slots.into_iter().rev().flatten().collect() +} + +impl BlockEntities { + /// Whether the ticking table has been built. + pub fn has_states(&self) -> bool { + !self.states.is_empty() + } + pub fn set_states(&mut self, states: Vec) { + self.states = states; + } + pub fn take_states(&mut self) -> Vec { + std::mem::take(&mut self.states) + } + pub fn ticking(&self, state: u32) -> Ticking { + self.states.get(state as usize).copied().unwrap_or_default() + } + + fn next(&mut self) -> u32 { + let n = self.next_slot; + self.next_slot = n.wrapping_add(1); + n + } + /// A ticker may be made for `pos` now (`updateBlockEntityTicker`). + fn add(&mut self, pos: Pos) { + let token = self.next(); + self.pending.insert(pos, token); + self.added.push((pos, token)); + } + /// `removeBlockEntityTicker`: no slot, made or pending. + fn cancel(&mut self, pos: &Pos) { + self.freed |= self.live.remove(pos).is_some(); + self.pending.remove(pos); + } + + pub fn get_mut(&mut self, pos: &Pos) -> Option<&mut BlockEntity> { + self.map.get_mut(pos) + } + pub fn iter_mut(&mut self) -> impl Iterator { + self.map.iter_mut() + } + pub fn values_mut(&mut self) -> impl Iterator { + self.map.values_mut() + } + /// `LevelChunk.setBlockEntity` (+ `addAndRegisterBlockEntity`): a new + /// block entity may get a slot; one replacing another keeps its slot. + pub fn insert(&mut self, pos: Pos, entity: BlockEntity) -> Option { + let old = self.map.insert(pos, entity); + if old.is_none() { + self.add(pos); + } + old + } + /// The block entity at `pos`, made by `make` when there is none. + pub fn get_or_insert_with( + &mut self, + pos: Pos, + make: impl FnOnce() -> BlockEntity, + ) -> &mut BlockEntity { + if !self.map.contains_key(&pos) { + self.add(pos); + } + self.map.entry(pos).or_insert_with(make) + } + /// `LevelChunk.removeBlockEntity`: the block entity and its slot. + pub fn remove(&mut self, pos: &Pos) -> Option { + self.cancel(pos); + self.map.remove(pos) + } + /// The block entity at `pos`, out of the map for a while: its slot stays + /// for `put` to bring it back (a tick working on it). + pub fn take(&mut self, pos: &Pos) -> Option { + let taken = self.map.remove(pos); + if taken.is_some() { + self.out.insert(*pos); + } + taken + } + /// Brings back what `take` took, in its slot. + pub fn put(&mut self, pos: Pos, entity: BlockEntity) { + self.out.remove(&pos); + if self.map.insert(pos, entity).is_none() + && !self.live.contains_key(&pos) + && !self.pending.contains_key(&pos) + { + self.add(pos); + } + } + /// Removes the block entities `keep` refuses, and their slots + /// (`clearAllBlockEntities` of unloaded chunks). + pub fn retain(&mut self, mut keep: impl FnMut(&Pos, &mut BlockEntity) -> bool) { + let (live, pending, freed) = (&mut self.live, &mut self.pending, &mut self.freed); + self.map.retain(|p, e| { + let kept = keep(p, e); + if !kept { + *freed |= live.remove(p).is_some(); + pending.remove(p); + } + kept + }); + } + pub fn clear(&mut self) { + self.map.clear(); + self.tickers.clear(); + self.live.clear(); + self.added.clear(); + self.pending.clear(); + self.out.clear(); + self.freed = false; + } + /// A loaded or generated chunk's block entities, in the order the chunk + /// lists them; they tick in its block entity map's order + /// (`registerAllBlockEntitiesAfterLevelLoad`). + pub fn load_chunk(&mut self, entities: Vec<(Pos, BlockEntity)>) { + let order = chunk_map_order(entities.iter().map(|(p, _)| *p)); + for (p, e) in entities { + self.map.insert(p, e); + } + for p in order { + self.add(p); + } + } + /// `LevelChunk.setBlockState` on a block entity's position, from state + /// `old` to `new`: a ticker made, kept or dropped. + pub fn state_changed(&mut self, pos: Pos, old: u32, new: u32) { + if self.states.is_empty() || !self.map.contains_key(&pos) { + return; + } + match self.ticking(new) { + // A new ticker, unless one was made or is pending (rebound). + Ticking::Yes => { + if self.ticking(old) != Ticking::Yes && !self.live.contains_key(&pos) { + self.add(pos); + } + } + Ticking::Idle => self.cancel(&pos), + // Another block: the game removes or replaces the block entity. + Ticking::No => {} + } + } + /// The start of `Level.tickBlockEntities`: pending slots join the list + /// and freed ones leave it. `state` reads the block state at a position. + /// Returns the slots to tick, in order; check each with `is_live` before + /// ticking it, as the ticks before it can free it. + pub fn tickers(&mut self, state: impl Fn(Pos) -> u32) -> Vec<(Pos, u32)> { + for (pos, token) in std::mem::take(&mut self.added) { + if self.pending.get(&pos) != Some(&token) { + continue; + } + self.pending.remove(&pos); + if self.live.contains_key(&pos) + || !self.map.contains_key(&pos) + || self.ticking(state(pos)) != Ticking::Yes + { + continue; + } + let slot = self.next(); + self.live.insert(pos, slot); + self.tickers.push((pos, slot)); + } + if !std::mem::take(&mut self.freed) && self.out.is_empty() { + return self.tickers.clone(); + } + self.out.clear(); + let (live, map) = (&mut self.live, &self.map); + self.tickers.retain(|(p, slot)| { + let mine = live.get(p) == Some(slot); + if mine && !map.contains_key(p) { + live.remove(p); + } + mine && map.contains_key(p) + }); + self.tickers.clone() + } + /// Whether a slot from `tickers` still ticks. + pub fn is_live(&self, (pos, slot): (Pos, u32)) -> bool { + self.live.get(&pos) == Some(&slot) && self.map.contains_key(&pos) + } + /// Frees the slot at `pos` (the block entity stays). + pub fn forget(&mut self, pos: &Pos) { + self.cancel(pos); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn hopper() -> BlockEntity { + BlockEntity::Chest(vec![None; 5]) + } + /// States: 0 air, 1 ticks, 2 idle. + fn level() -> BlockEntities { + let mut b = BlockEntities::default(); + b.set_states(vec![Ticking::No, Ticking::Yes, Ticking::Idle]); + b + } + fn order(b: &mut BlockEntities, states: &HashMap) -> Vec { + b.tickers(|p| states.get(&p).copied().unwrap_or(0)) + .into_iter() + .map(|(p, _)| p) + .collect() + } + + #[test] + fn slots_follow_vanilla_lifecycle() { + let mut b = level(); + let mut s = HashMap::new(); + for p in [(0, 0, 0), (1, 0, 0), (2, 0, 0)] { + s.insert(p, 1); + b.insert(p, hopper()); + } + assert_eq!(order(&mut b, &s), [(0, 0, 0), (1, 0, 0), (2, 0, 0)]); + // Replacing keeps the slot; removing and placing again appends. + b.insert((0, 0, 0), hopper()); + b.remove(&(1, 0, 0)); + b.insert((1, 0, 0), hopper()); + assert_eq!(order(&mut b, &s), [(0, 0, 0), (2, 0, 0), (1, 0, 0)]); + // Take and put keep it; an idle state frees it, ticking again appends. + let e = b.take(&(0, 0, 0)).unwrap(); + b.put((0, 0, 0), e); + assert_eq!(order(&mut b, &s), [(0, 0, 0), (2, 0, 0), (1, 0, 0)]); + s.insert((2, 0, 0), 2); + b.state_changed((2, 0, 0), 1, 2); + assert_eq!(order(&mut b, &s), [(0, 0, 0), (1, 0, 0)]); + s.insert((2, 0, 0), 1); + b.state_changed((2, 0, 0), 2, 1); + assert_eq!(order(&mut b, &s), [(0, 0, 0), (1, 0, 0), (2, 0, 0)]); + // A slot freed during a tick is not live any more. + let slots = b.tickers(|p| s.get(&p).copied().unwrap_or(0)); + b.retain(|p, _| *p != (1, 0, 0)); + assert!(b.is_live(slots[0]) && !b.is_live(slots[1])); + // A block entity taken and never brought back loses its slot. + b.take(&(0, 0, 0)); + assert_eq!(order(&mut b, &s), [(2, 0, 0)]); + assert_eq!(order(&mut b, &s), [(2, 0, 0)]); + } + + #[test] + fn chunk_loads_in_open_hash_map_order() { + // A few keys: their mixed hashes over 32 slots, read from the top. + let positions = [ + (0, 64, 0), + (1, 64, 0), + (0, 64, 1), + (15, -64, 15), + (3, 70, 9), + ]; + let slot = |p: &Pos| mix(java_hash(*p)) & 31; + let mut expected = positions.to_vec(); + expected.sort_by_key(|p| std::cmp::Reverse(slot(p))); + let distinct: std::collections::HashSet = positions.iter().map(slot).collect(); + assert_eq!(distinct.len(), positions.len()); + assert_eq!(chunk_map_order(positions), expected); + // Past 24 keys the table doubles; every key stays, once. + let many: Vec = (0..100).map(|i| (i % 16, i / 16, 3)).collect(); + let order = chunk_map_order(many.iter().copied().chain(many.iter().copied())); + assert_eq!(order.len(), 100); + } +} diff --git a/crates/horizon-client/src/block_entity_anim.rs b/crates/horizon-client/src/block_entity_anim.rs index eefd1ba63..2a35c9835 100644 --- a/crates/horizon-client/src/block_entity_anim.rs +++ b/crates/horizon-client/src/block_entity_anim.rs @@ -12,11 +12,11 @@ //! (blocks, entities, game time) as closures, so the parity fixture can drive //! them with scripted inputs (`tools/test_block_entity_anim_parity.py`). use crate::block_entity_render::BlockAnim; +use crate::fast_hash::FxHashMap; use horizon_core::{ geometry::trig::{self, Mth}, random::{LegacyRandom, RandomSource}, }; -use std::collections::HashMap; /// `Direction` names by `get3DDataValue`. pub const DIRECTIONS: [&str; 6] = ["down", "up", "north", "south", "west", "east"]; @@ -832,38 +832,44 @@ pub fn ticker(name: &str, prop: impl Fn(&str) -> Option) -> Option, - pub books: HashMap<[i32; 3], EnchantingBook>, + pub chests: FxHashMap<[i32; 3], ChestLid>, + pub books: FxHashMap<[i32; 3], EnchantingBook>, /// `EnchantingTableBlockEntity.RANDOM`, shared by every table. pub book_random: LegacyRandom, - pub shulkers: HashMap<[i32; 3], Shulker>, - pub bells: HashMap<[i32; 3], Bell>, - pub conduits: HashMap<[i32; 3], Conduit>, - pub skulls: HashMap<[i32; 3], Skull>, - pub pots: HashMap<[i32; 3], PotWobble>, - pub gateways: HashMap<[i32; 3], EndGateway>, + pub shulkers: FxHashMap<[i32; 3], Shulker>, + pub bells: FxHashMap<[i32; 3], Bell>, + pub conduits: FxHashMap<[i32; 3], Conduit>, + pub skulls: FxHashMap<[i32; 3], Skull>, + pub pots: FxHashMap<[i32; 3], PotWobble>, + pub gateways: FxHashMap<[i32; 3], EndGateway>, /// The integrated server's copies of the gateways (`portalTick`). - pub gateway_servers: HashMap<[i32; 3], EndGateway>, + pub gateway_servers: FxHashMap<[i32; 3], EndGateway>, /// The client level's random (conduit particles). pub random: LegacyRandom, /// [`Ticker`] by block state id (see [`BlockEntityAnims::classify`]). pub kinds: Vec>, + /// The drawn block entities with a ticker, sorted by position, and the + /// renderer's block entity revision they were read at. + pub present: Vec<([i32; 3], Ticker)>, + pub present_revision: Option, } impl Default for BlockEntityAnims { fn default() -> Self { Self { - chests: HashMap::new(), - books: HashMap::new(), + chests: FxHashMap::default(), + books: FxHashMap::default(), book_random: LegacyRandom::new(0x626f6f6b), - shulkers: HashMap::new(), - bells: HashMap::new(), - conduits: HashMap::new(), - skulls: HashMap::new(), - pots: HashMap::new(), - gateways: HashMap::new(), - gateway_servers: HashMap::new(), + shulkers: FxHashMap::default(), + bells: FxHashMap::default(), + conduits: FxHashMap::default(), + skulls: FxHashMap::default(), + pots: FxHashMap::default(), + gateways: FxHashMap::default(), + gateway_servers: FxHashMap::default(), random: LegacyRandom::new(0x6c6576656c), kinds: Vec::new(), + present: Vec::new(), + present_revision: None, } } } @@ -877,15 +883,20 @@ impl BlockEntityAnims { .and_then(|s| ticker(&s.name, |k| s.properties.get(k).cloned())) }) .collect(); + self.present_revision = None; } /// The ticker of a block state. pub fn ticker(&self, state: u32) -> Option { self.kinds.get(state as usize).copied().flatten() } /// Drops the state of block entities that are gone or changed kind: - /// `present` maps the loaded block entities to their tickers. - pub fn retain_present(&mut self, present: &HashMap<[i32; 3], Ticker>) { - let is = |p: &[i32; 3], f: fn(&Ticker) -> bool| present.get(p).is_some_and(f); + /// `present` holds the loaded block entities' tickers, sorted by position. + pub fn retain_present(&mut self, present: &[([i32; 3], Ticker)]) { + let is = |p: &[i32; 3], f: fn(&Ticker) -> bool| { + present + .binary_search_by_key(p, |(q, _)| *q) + .is_ok_and(|i| f(&present[i].1)) + }; self.chests.retain(|p, _| is(p, |t| *t == Ticker::Lid)); self.books.retain(|p, _| is(p, |t| *t == Ticker::Book)); self.shulkers diff --git a/crates/horizon-client/src/block_entity_render.rs b/crates/horizon-client/src/block_entity_render.rs index 9c90e8ebf..db6bf7b79 100644 --- a/crates/horizon-client/src/block_entity_render.rs +++ b/crates/horizon-client/src/block_entity_render.rs @@ -737,6 +737,8 @@ pub struct Scene<'a> { pub struct BlockEntityRenderer { models: HashMap<&'static str, ModelPart>, kinds: Vec, + // Closed chest geometry: dimension lighting, chest part, horizontal facing. + closed_chests: [[[Vec; 4]; 3]; 2], /// Item id to its `PROVIDES_POTTERY_PATTERN` side texture (`Sheets.DECORATED_POT_MAPPER`). pot_patterns: HashMap, /// `Block.shouldRenderFace` for End gateway faces (its occlusion pair cache needs a lock). @@ -817,6 +819,43 @@ impl BlockEntityRenderer { Ok(renderer) } fn new(models: HashMap<&'static str, ModelPart>, registry: &BlockRegistry) -> Self { + let closed_chests = [false, true].map(|nether| { + ["chest", "double_chest_left", "double_chest_right"].map(|name| { + [0.0f32, 90.0, 180.0, 270.0].map(|y_rot| { + let mut vertices = Vec::new(); + if let Some(model) = models.get(name) { + draw_part( + model, + "root", + around( + Mat4::from_rotation_y((-y_rot).to_radians()), + Vec3::new(0.5, 0.0, 0.5), + ), + &|name, p| { + if name == "lid" || name == "lock" { + p[3] = crate::block_entity_anim::chest_lid_x_rot(0.0); + } + }, + Paint { + sprite: Rect { + u0: 0.0, + v0: 0.0, + u1: 1.0, + v1: 1.0, + w: 16, + h: 16, + }, + tint: 0xFFFFFF, + light: 0, + lights: diffuse_lights(nether), + }, + &mut vertices, + ); + } + vertices + }) + }) + }); let kinds = (0..registry.len() as u32) .map(|id| { registry.state(BlockStateId(id)).map_or(Kind::None, |s| { @@ -831,6 +870,7 @@ impl BlockEntityRenderer { Self { models, kinds, + closed_chests, pot_patterns: HashMap::new(), visibility: None, } @@ -876,7 +916,7 @@ impl BlockEntityRenderer { .as_vec3(); let base = Mat4::from_translation(origin); let packed = scene.world.light(pos[0], pos[1], pos[2]); - self.render_one(scene, *pos, kind, base, packed, lights, false, out); + self.render_one(scene, *pos, kind, base, packed, lights, false, true, out); } } /// One block entity at `pos` drawn under `base` with the given packed light @@ -892,7 +932,7 @@ impl BlockEntityRenderer { ) { let kind = self.kind(state); let lights = diffuse_lights(scene.nether); - self.render_one(scene, pos, kind, base, packed, lights, false, out); + self.render_one(scene, pos, kind, base, packed, lights, false, false, out); } /// Whether this block state has a block-entity renderer here. pub fn renders(&self, state: u32) -> bool { @@ -915,6 +955,7 @@ impl BlockEntityRenderer { 0xff, diffuse_lights(scene.nether), true, + false, out, ); crate::breaking::entity_geometry(&mut out[start..], origin); @@ -991,7 +1032,17 @@ impl BlockEntityRenderer { let kind = self.kind(state); if !matches!(kind, Kind::None) { let lights = diffuse_lights(scene.nether); - self.render_one(scene, [i32::MAX; 3], kind, base, packed, lights, false, out); + self.render_one( + scene, + [i32::MAX; 3], + kind, + base, + packed, + lights, + false, + false, + out, + ); } } #[allow(clippy::too_many_arguments)] @@ -1004,6 +1055,7 @@ impl BlockEntityRenderer { packed: u8, lights: (Vec3, Vec3), crumbling: bool, + world_chests: bool, out: &mut Vec, ) { let sprite = |name: &str| scene.sprites.get(name).copied(); @@ -1107,6 +1159,23 @@ impl BlockEntityRenderer { let l = scene.world.light(other[0], other[1], other[2]); packed = (packed >> 4).max(l >> 4) << 4 | (packed & 15).max(l & 15); } + if world_chests && open == 0.0 { + if let Some(paint) = paint(texture, 0xFFFFFF, packed) { + let vertices = &self.closed_chests[usize::from(scene.nether)] + [*part as usize][*y_rot as usize / 90]; + let origin = base.w_axis.truncate(); + out.extend(vertices.iter().map(|v| Vertex { + pos: (Vec3::from_array(v.pos) + origin).to_array(), + uv: [ + paint.sprite.u0 + (paint.sprite.u1 - paint.sprite.u0) * v.uv[0], + paint.sprite.v0 + (paint.sprite.v1 - paint.sprite.v0) * v.uv[1], + ], + light: paint.light, + ..*v + })); + } + return; + } let m = base * around( Mat4::from_rotation_y((-y_rot).to_radians()), @@ -1791,16 +1860,18 @@ mod tests { ( name.to_owned(), Rect { - u0: 0.0, - v0: 0.0, - u1: 1.0, - v1: 1.0, + u0: 0.25, + v0: 0.125, + u1: 0.375, + v1: 0.25, w: 16, h: 16, }, ) }) .collect(); + let cache_open = std::cell::Cell::new(0.0); + let cache_lid = |_| cache_open.get(); let mut scene = Scene { camera: DVec3::ZERO, orientation: Quat::IDENTITY, @@ -1817,6 +1888,56 @@ mod tests { banner: &|_| Vec::new(), glyph: &|_| (None, 0.0), }; + // Compare the cached world path with the original model renderer, + // including atlas replacement, current light and animated fallback. + scene.lid = &cache_lid; + for nether in [false, true] { + scene.nether = nether; + for part in [ChestPart::Single, ChestPart::Left, ChestPart::Right] { + for y_rot in [0.0, 90.0, 180.0, 270.0] { + let kind = Kind::Chest { + block: "chest".into(), + texture: "entity/chest/normal", + part, + y_rot, + double_with: None, + }; + for (open, packed) in [(0.0, 0xF0), (0.0, 0x7F), (0.5, 0x7F)] { + cache_open.set(open); + let base = Mat4::from_translation(Vec3::new(3.25, -8.5, 12.75)); + let mut cached = Vec::new(); + let mut original = Vec::new(); + for (use_cache, out) in [(true, &mut cached), (false, &mut original)] { + renderer.render_one( + &scene, + [0; 3], + &kind, + base, + packed, + diffuse_lights(nether), + false, + use_cache, + out, + ); + } + assert!(!cached.is_empty()); + assert_eq!(cached.len(), original.len()); + for (a, b) in cached.iter().zip(&original) { + assert!( + (Vec3::from_array(a.pos) - Vec3::from_array(b.pos)).length() < 3e-6 + ); + assert_eq!(a.uv, b.uv); + assert_eq!(a.color, b.color); + assert_eq!(a.light, b.light); + assert_eq!(a.normal, b.normal); + assert_eq!(a.layer, b.layer); + } + } + } + } + } + scene.nether = false; + scene.lid = &|_| 0.0; for state in 0..names.len() as u32 { let mut base = Vec::new(); renderer.render_block(&scene, [0; 3], state, Mat4::IDENTITY, 0xff, &mut base); diff --git a/crates/horizon-client/src/chat.rs b/crates/horizon-client/src/chat.rs index 6156a041d..7c51a40f1 100644 --- a/crates/horizon-client/src/chat.rs +++ b/crates/horizon-client/src/chat.rs @@ -386,6 +386,12 @@ pub struct ChatScreen { parse: Option, /// The completions at the cursor (`pendingSuggestions`): range start (bytes) and list. pending: Option<(usize, Vec)>, + /// Completions waiting for the server's (an `ask_server` node at the + /// cursor): `pendingSuggestions` is not done until they come. + awaiting: Option, + /// The request to send (`ServerboundCommandSuggestionPacket`): id and + /// the command up to the cursor. + request: Option<(i32, String)>, suggestions: Option, usage: Vec, usage_position: i32, @@ -394,6 +400,16 @@ pub struct ChatScreen { keep_suggestions: bool, pub auto_suggestions: bool, } +/// Local completions waiting for the server's reply to request `id`. +struct Awaiting { + id: i32, + /// The command up to the cursor, as sent. + truncated: String, + local: Vec, +} +/// `ClientSuggestionProvider.pendingSuggestionsId`: one counter for the +/// connection, so a reply to an older request never matches. +static SUGGESTION_ID: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0); /// `CommandSuggestions`' fill colour. const FILL: i32 = -805306368; fn byte_at(s: &str, chars: usize) -> usize { @@ -442,6 +458,8 @@ impl ChatScreen { history_pos: history_len, parse: None, pending: None, + awaiting: None, + request: None, suggestions: None, usage: Vec::new(), usage_position: 0, @@ -518,24 +536,84 @@ impl ChatScreen { } if cursor >= 1 && (self.suggestions.is_none() || !self.keep_suggestions) { let parse = self.parse.as_ref().expect("parsed above"); - let (range, list) = c - .dispatcher - .client_suggestions(parse, cursor_b, c.registries); - self.pending = Some((range.0, list)); - self.update_usage_info(m, c); + let (range, list, ask) = + c.dispatcher + .client_suggestions_asking(parse, cursor_b, c.registries); + if ask { + // ClientSuggestionProvider.customSuggestion: the + // suggestions are done when the server answers. + let id = SUGGESTION_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1; + let truncated = command[..cursor_b].to_owned(); + self.request = Some((id, truncated.clone())); + self.awaiting = Some(Awaiting { + id, + truncated, + local: list, + }); + self.pending = None; + } else { + self.awaiting = None; + self.pending = Some((range.0, list)); + self.update_usage_info(m, c); + } } } else if !command.chars().all(char::is_whitespace) { let partial = &command[..cursor_b]; let mut builder = SuggestionsBuilder::new(partial, last_word_index(partial)); builder.suggest_matching(c.registries.players.iter().map(String::as_str)); let (range, list) = merge_suggestions(partial, builder.out); + self.awaiting = None; self.pending = Some((range.0, list)); self.usage_width = 0; self.usage_position = 0; } else { + self.awaiting = None; self.pending = None; } } + /// The completion request to send to the server, if one is waiting. + pub fn take_request(&mut self) -> Option<(i32, String)> { + self.request.take() + } + /// `ClientSuggestionProvider.completeCustomSuggestions`: the server's + /// completions for request `id`, with `range` in UTF-16 units of the + /// command sent. They join the local ones (`Suggestions.merge`) and the + /// pending suggestions are done. + pub fn server_suggestions( + &mut self, + m: &Measure, + c: &Commands, + id: i32, + range: (usize, usize), + list: Vec<(String, Option)>, + ) { + let Some(awaiting) = self.awaiting.take_if(|a| a.id == id) else { + return; + }; + let input = &awaiting.truncated; + let byte = |units: usize| { + let mut n = 0; + input + .char_indices() + .find(|(_, ch)| { + let past = n >= units; + n += ch.len_utf16(); + past + }) + .map_or(input.len(), |(i, _)| i) + }; + let range = (byte(range.0), byte(range.0 + range.1)); + let mut all = awaiting.local; + all.extend(list.into_iter().map(|(text, tooltip)| Suggestion { + range, + text, + tooltip, + int_value: None, + })); + let (range, list) = merge_suggestions(input, all); + self.pending = Some((range.0, list)); + self.update_usage_info(m, c); + } /// `CommandSuggestions.updateUsageInfo`. fn update_usage_info(&mut self, m: &Measure, c: &Commands) { let Some(parse) = &self.parse else { return }; diff --git a/crates/horizon-client/src/chat_tests.rs b/crates/horizon-client/src/chat_tests.rs index 3cbac071b..e1bf2feda 100644 --- a/crates/horizon-client/src/chat_tests.rs +++ b/crates/horizon-client/src/chat_tests.rs @@ -230,3 +230,58 @@ fn mod_commands_are_coloured_and_complete_like_vanilla_ones() { screen.key(&m, &c, &[], winit::keyboard::KeyCode::Tab, false, false); assert_eq!(screen.value(), "/coords"); } + +#[test] +fn ask_server_nodes_wait_for_the_servers_completions() { + // ClientSuggestionProvider.customSuggestion: an `ask_server` node at the + // cursor sends the command so far, and the list is done with the reply. + let font = font(); + let m = Measure(&font); + let registries = Registries { + permission: 4, + ..Registries::default() + }; + let mut d = crate::command::vanilla::game_dispatcher().clone(); + let child = |d: &crate::command::Dispatcher, n: usize, name: &str| { + d.nodes[n] + .children + .iter() + .copied() + .find(|&c| d.nodes[c].name() == name) + .expect(name) + }; + let id = child(&d, child(&d, child(&d, 0, "bossbar"), "get"), "id"); + d.nodes[id].suggests = Some(crate::command::Suggests::AskServer); + let c = Commands { + dispatcher: &d, + registries: ®istries, + }; + let tab = |screen: &mut ChatScreen| { + screen.key(&m, &c, &[], winit::keyboard::KeyCode::Tab, false, false); + }; + let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360); + screen.type_text(&m, &c, "/bossbar get pt"); + let (request, sent) = screen.take_request().expect("the server is asked"); + assert_eq!(sent, "/bossbar get pt"); + assert_eq!(screen.take_request(), None); + // Nothing to complete until the reply. + tab(&mut screen); + assert_eq!(screen.value(), "/bossbar get pt"); + // A reply to an older request is dropped; the range is in UTF-16 units. + let reply = |text: &str| vec![(text.to_owned(), None)]; + screen.server_suggestions(&m, &c, request - 1, (13, 2), reply("pt6:old")); + tab(&mut screen); + assert_eq!(screen.value(), "/bossbar get pt"); + screen.server_suggestions(&m, &c, request, (13, 2), reply("pt6:check")); + tab(&mut screen); + assert_eq!(screen.value(), "/bossbar get pt6:check"); + // Without the server's provider the local list is done at once. + let local = crate::command::vanilla::game_dispatcher(); + let c = Commands { + dispatcher: local, + registries: ®istries, + }; + let mut screen = ChatScreen::new(&m, &c, "", 0, 640, 360); + screen.type_text(&m, &c, "/bossbar get pt"); + assert_eq!(screen.take_request(), None); +} diff --git a/crates/horizon-client/src/chunk.rs b/crates/horizon-client/src/chunk.rs index b0b2ef262..857372045 100644 --- a/crates/horizon-client/src/chunk.rs +++ b/crates/horizon-client/src/chunk.rs @@ -3,17 +3,32 @@ //! (copy-on-write through `Arc::make_mut`). use horizon_core::paletted::{PalettedContainer, Strategy}; use horizon_world::registry::BlockStateId; -use std::sync::Arc; +use std::sync::{Arc, OnceLock}; pub const MIN_Y: i32 = -64; pub const HEIGHT: i32 = 384; pub const SECTIONS: usize = (HEIGHT / 16) as usize; -/// Block states are `PalettedContainer`s over the pinned registry (35,723 -/// states: a 16-bit global palette); biomes are 4x4x4 containers over the -/// biome table (67 entries: a 7-bit global palette). -pub const BLOCK_STRATEGY: Strategy = Strategy::blocks(16); +/// Biomes are 4x4x4 containers over the biome table (67 entries: a 7-bit +/// global palette). pub const BIOME_STRATEGY: Strategy = Strategy::biomes(7); + +/// Bits of the block states' global palette: 16 for vanilla's 35,723 states +/// (what vanilla servers and clients send), more when content mods add states +/// past 65,536 (docs/modpacks.md). Set once, before the first chunk. +static BLOCK_BITS: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(16); + +/// Block states are `PalettedContainer`s over the registry: vanilla's 16-bit +/// global palette, wider with enough content mods. +pub fn block_strategy() -> Strategy { + Strategy::blocks(BLOCK_BITS.load(std::sync::atomic::Ordering::Relaxed)) +} + +/// Sizes the block states' global palette for a registry of `states`. +pub fn set_block_states(states: usize) { + let bits = horizon_core::paletted::ceil_log2(states).max(16); + BLOCK_BITS.store(bits, std::sync::atomic::Ordering::Relaxed); +} /// Packed light per block: sky light in the high nibble, block light in the low one. #[derive(Clone)] pub enum Light { @@ -30,6 +45,10 @@ impl Light { } } #[inline] +fn column(x: i32, z: i32) -> usize { + ((z & 15) << 4 | (x & 15)) as usize +} +#[inline] pub fn index(x: i32, y: i32, z: i32) -> usize { ((y & 15) << 8 | (z & 15) << 4 | (x & 15)) as usize } @@ -43,7 +62,7 @@ pub struct Section { impl Default for Section { fn default() -> Self { Self { - blocks: PalettedContainer::new(BLOCK_STRATEGY, 0), + blocks: PalettedContainer::new(block_strategy(), 0), biomes: PalettedContainer::new(BIOME_STRATEGY, 0), light: Light::Uniform(0xF0), } @@ -51,27 +70,26 @@ impl Default for Section { } impl Section { #[inline] - pub fn get(&self, i: usize) -> u16 { - self.blocks.get(i) as u16 + pub fn get(&self, i: usize) -> u32 { + self.blocks.get(i) } - pub fn set(&mut self, i: usize, v: u16) -> u16 { - self.blocks.set(i, u32::from(v)) as u16 + pub fn set(&mut self, i: usize, v: u32) -> u32 { + self.blocks.set(i, v) } /// All 4,096 block states in index order. - pub fn unpack(&self, out: &mut [u16; 4096]) { - self.blocks.unpack_u16(out); + pub fn unpack(&self, out: &mut [u32; 4096]) { + self.blocks.unpack(out); } /// The block states of a whole section at once. - pub fn from_blocks(values: &[u16; 4096]) -> PalettedContainer { - let wide: Vec = values.iter().map(|&v| u32::from(v)).collect(); - PalettedContainer::from_values(BLOCK_STRATEGY, &wide) + pub fn from_blocks(values: &[u32; 4096]) -> PalettedContainer { + PalettedContainer::from_values(block_strategy(), values) } #[inline] pub fn light(&self, i: usize) -> u8 { self.light.get(i) } - pub fn is_uniform(&self, v: u16) -> bool { - self.blocks.is_uniform(u32::from(v)) + pub fn is_uniform(&self, v: u32) -> bool { + self.blocks.is_uniform(v) } /// Heap bytes held by the block, biome and light storage (benchmark accounting). pub fn heap_bytes(&self) -> usize { @@ -104,8 +122,10 @@ pub struct Chunk { pub x: i32, pub z: i32, pub sections: Vec>, - /// Highest non-air, light-blocking-or-fluid Y per column (MOTION_BLOCKING-like), for rain/sky. - pub heights: Arc<[i16; 256]>, + /// `Heightmap.Types.WORLD_SURFACE` per column (`z << 4 | x`): one above + /// the highest non-air block, or `MIN_Y`. Built on the first read; a + /// `set` that can move a column's surface drops it. + surface: OnceLock>, pub lit: bool, /// Light read from a save with `isLightOn`: no light job is needed. pub light_loaded: bool, @@ -134,7 +154,7 @@ impl Chunk { sections: (0..SECTIONS) .map(|_| Arc::new(Section::default())) .collect(), - heights: Arc::new([MIN_Y as i16; 256]), + surface: OnceLock::new(), lit: false, light_loaded: false, fresh: false, @@ -147,7 +167,7 @@ impl Chunk { } } #[inline] - pub fn get(&self, x: i32, y: i32, z: i32) -> u16 { + pub fn get(&self, x: i32, y: i32, z: i32) -> u32 { let s = (y - MIN_Y) >> 4; if !(0..SECTIONS as i32).contains(&s) { return 0; @@ -156,11 +176,11 @@ impl Chunk { } #[inline] pub fn block(&self, x: i32, y: i32, z: i32) -> BlockStateId { - BlockStateId(u32::from(self.get(x, y, z))) + BlockStateId(self.get(x, y, z)) } /// The highest y where `matches`, scanning down. Sections holding only /// `skip` are passed over whole: `matches` must be false for it. - pub fn highest(&self, skip: u16, mut matches: impl FnMut(i32) -> bool) -> Option { + pub fn highest(&self, skip: u32, mut matches: impl FnMut(i32) -> bool) -> Option { self.sections .iter() .enumerate() @@ -171,12 +191,57 @@ impl Chunk { (base..base + 16).rev().find(|&y| matches(y)) }) } - pub fn set(&mut self, x: i32, y: i32, z: i32, v: u16) -> u16 { + pub fn set(&mut self, x: i32, y: i32, z: i32, v: u32) -> u32 { let s = (y - MIN_Y) >> 4; if !(0..SECTIONS as i32).contains(&s) { return v; } - Arc::make_mut(&mut self.sections[s as usize]).set(index(x, y, z), v) + let previous = Arc::make_mut(&mut self.sections[s as usize]).set(index(x, y, z), v); + // `Heightmap.update`: a write two or more below the surface can't + // move it. Any other change rebuilds the map on the next read. + if previous != v + && let Some(surface) = self.surface.get() + && y >= i32::from(surface[column(x, z)]) - 1 + { + self.surface = OnceLock::new(); + } + previous + } + /// `getHeight(Heightmap.Types.WORLD_SURFACE, x, z)` (`x`, `z` within the + /// chunk): one above the highest block that isn't `air`, or `MIN_Y`. + /// Sections must change only through `set` once this has been read. + pub fn world_surface(&self, x: i32, z: i32, air: impl Fn(u32) -> bool) -> i32 { + let surface = self.surface.get_or_init(|| self.prime_surface(air)); + i32::from(surface[column(x, z)]) + } + /// `Heightmap.primeHeightmaps` for WORLD_SURFACE: each section from the + /// top, skipping those of one air state, until every column is found. + fn prime_surface(&self, air: impl Fn(u32) -> bool) -> Box<[i16; 256]> { + let mut surface = Box::new([MIN_Y as i16; 256]); + let mut found = [false; 256]; + let mut left = 256; + let mut values = [0u32; 4096]; + for (s, section) in self.sections.iter().enumerate().rev() { + if left == 0 { + break; + } + if section.blocks.uniform().is_some_and(&air) { + continue; + } + section.unpack(&mut values); + let base = MIN_Y + s as i32 * 16; + for i in 0..256 { + if found[i] { + continue; + } + if let Some(dy) = (0..16).rev().find(|&dy| !air(values[dy << 8 | i])) { + surface[i] = (base + dy as i32 + 1) as i16; + found[i] = true; + left -= 1; + } + } + } + surface } #[inline] pub fn light(&self, x: i32, y: i32, z: i32) -> u8 { @@ -228,15 +293,15 @@ mod tests { Arc::make_mut(&mut chunk.sections[5]).set(i, cave_air); } chunk.set(3, 150, 4, stone); - let full = |c: &Chunk, matches: &dyn Fn(u16) -> bool| { + let full = |c: &Chunk, matches: &dyn Fn(u32) -> bool| { (MIN_Y..MIN_Y + HEIGHT) .rev() .find(|&y| matches(c.get(3, y, 4))) }; for matches in [ - &(|v: u16| v != air) as &dyn Fn(u16) -> bool, - &|v: u16| v == stone, - &|v: u16| v == cave_air, + &(|v: u32| v != air) as &dyn Fn(u32) -> bool, + &|v: u32| v == stone, + &|v: u32| v == cave_air, ] { let fast = chunk.highest(air, |y| matches(chunk.get(3, y, 4))); assert_eq!(fast, full(&chunk, matches)); @@ -247,4 +312,50 @@ mod tests { ); assert_eq!(Chunk::empty(0, 0).highest(air, |_| unreachable!()), None); } + /// The kept WORLD_SURFACE heights match a scan of every column after + /// each of a few thousand writes (air, cave air, stone, whole sections). + #[test] + fn world_surface_follows_writes() { + let (air, cave_air, stone) = (0, 7, 1); + let is_air = |v: u32| v == air || v == cave_air; + let scan = |c: &Chunk, x: i32, z: i32| { + (MIN_Y..MIN_Y + HEIGHT) + .rev() + .find(|&y| !is_air(c.get(x, y, z))) + .map_or(MIN_Y, |y| y + 1) + }; + let mut chunk = Chunk::empty(0, 0); + assert_eq!(chunk.world_surface(5, 5, is_air), MIN_Y); + for y in MIN_Y..MIN_Y + 70 { + for i in 0..256 { + chunk.set(i & 15, y, i >> 4, stone); + } + } + for i in 0..4096 { + Arc::make_mut(&mut chunk.sections[8]).set(i, cave_air); + } + let mut random = horizon_core::random::LegacyRandom::new(9); + use horizon_core::random::RandomSource; + for step in 0..4000 { + let x = random.next_int_bound(16).unwrap(); + let z = random.next_int_bound(16).unwrap(); + let y = MIN_Y + 40 + random.next_int_bound(120).unwrap(); + let v = [air, cave_air, stone][random.next_int_bound(3).unwrap() as usize]; + chunk.set(x, y, z, v); + if step % 7 == 0 { + for i in 0..256 { + assert_eq!( + chunk.world_surface(i & 15, i >> 4, is_air), + scan(&chunk, i & 15, i >> 4) + ); + } + } + } + // Out-of-range writes and unchanged states keep the map. + let before = chunk.world_surface(3, 3, is_air); + chunk.set(3, MIN_Y + HEIGHT, 3, stone); + chunk.set(3, before - 1, 3, chunk.get(3, before - 1, 3)); + assert!(chunk.surface.get().is_some()); + assert_eq!(chunk.world_surface(3, 3, |_| unreachable!()), before); + } } diff --git a/crates/horizon-client/src/chunk_map.rs b/crates/horizon-client/src/chunk_map.rs index 81e06f020..ed25491c5 100644 --- a/crates/horizon-client/src/chunk_map.rs +++ b/crates/horizon-client/src/chunk_map.rs @@ -76,7 +76,7 @@ impl<'a> IntoIterator for &'a ChunkMap { } } impl crate::light::LightWorld for ChunkMap { - fn state(&self, x: i32, y: i32, z: i32) -> Option { + fn state(&self, x: i32, y: i32, z: i32) -> Option { self.columns.state(x, y, z) } fn light(&self, x: i32, y: i32, z: i32) -> Option { diff --git a/crates/horizon-client/src/clock.rs b/crates/horizon-client/src/clock.rs index 865a348bc..accf07fc9 100644 --- a/crates/horizon-client/src/clock.rs +++ b/crates/horizon-client/src/clock.rs @@ -298,6 +298,9 @@ pub struct FixedStep { tick_times: [i64; SAMPLES], aggregated: i64, tick_count: i64, + /// A server's client: its level's tick rate only slows the timer down + /// (`Minecraft.getTickTargetMillis`), since the server runs the ticks. + client: bool, } impl Default for FixedStep { @@ -312,11 +315,20 @@ impl Default for FixedStep { tick_times: [0; SAMPLES], aggregated: 0, tick_count: 0, + client: false, } } } impl FixedStep { + /// The clock of a client connected to a server: the `ClientLevel`'s + /// `TickRateManager`, set by the server's ticking packets. + pub fn client() -> Self { + Self { + client: true, + ..Self::default() + } + } fn now_ms(&self) -> i64 { self.now.as_millis() as i64 } @@ -341,9 +353,14 @@ impl FixedStep { } let runs = self.manager.base.runs_normally(); let mspt = self.manager.base.milliseconds_per_tick(); + let client = self.client; let ticks = self .timer - .advance_game_time(now_ms, |default| if runs { mspt } else { default }); + .advance_game_time(now_ms, |default| match (runs, client) { + (true, true) => default.max(mspt), + (true, false) => mspt, + (false, _) => default, + }); ticks.clamp(0, 10) as u32 } /// Restart pacing from now (after loading, so no backlog replays). @@ -400,6 +417,10 @@ impl FixedStep { pub fn set_frozen(&mut self, frozen: bool) { self.manager.base.set_frozen(frozen); } + /// `TickRateManager.setFrozenTicksToRun` (ClientboundTickingStepPacket). + pub fn set_frozen_ticks_to_run(&mut self, ticks: i32) { + self.manager.base.set_frozen_ticks_to_run(ticks); + } pub fn sprinting(&self) -> bool { self.manager.is_sprinting() } @@ -484,4 +505,21 @@ mod tests { m.base.tick(); assert!(!m.base.runs_normally()); } + #[test] + fn a_servers_tick_rate_only_slows_its_client_down() { + let quarter = std::time::Duration::from_millis(250); + let mut client = FixedStep::client(); + client.set_rate(40.0); + assert_eq!(client.advance(quarter), 5, "still 20 a second"); + let mut slow = FixedStep::client(); + slow.set_rate(10.0); + assert_eq!(slow.advance(quarter * 2), 5, "10 a second"); + let mut local = FixedStep::default(); + local.set_rate(40.0); + assert_eq!( + local.advance(quarter), + 10, + "the integrated loop runs at the rate" + ); + } } diff --git a/crates/horizon-client/src/collisions.rs b/crates/horizon-client/src/collisions.rs index d3e00daca..a52f25ce7 100644 --- a/crates/horizon-client/src/collisions.rs +++ b/crates/horizon-client/src/collisions.rs @@ -15,24 +15,18 @@ use horizon_world::{ entity_obstruction::EntityIdentity, fluid::{FluidKind, FluidState}, liquid_collision::LiquidCollisionWorld, - nbt::{self, Limits}, powder_snow_collision::{PowderSnowEntity, PowderSnowWorld}, - registry::BlockRegistry, + registry::{BlockRegistry, BlockTableFile}, world_movement::{ CollisionCheckRequest, EntityMovementWorld, MovementRequest, no_world_collision, resolve_world_movement, }, }; -use std::{fs, io, path::Path, sync::Arc}; +use std::{io, path::Path, sync::Arc}; pub fn load<'r>(root: &Path, registry: &'r BlockRegistry) -> Result> { - let data = nbt::read_named( - &fs::read(root.join("cached-collision.nbt"))?, - Limits::tables(), - )? - .0 - .tag; - let cached = CachedCollisionRegistry::from_nbt(&data, registry)?; + let file = BlockTableFile::open(&root.join("cached-collision.nbt"), registry)?; + let cached = CachedCollisionRegistry::from_table(file.table("collision")?)?; if !cached.has_canonical_identities() { return Err("scene collisions require canonical shape identities".into()); } @@ -137,12 +131,25 @@ impl SceneCollisions<'_, '_> { movement: Vec3, max_step: f32, on_ground: bool, + ) -> Result { + self.resolve_with_context(bounds, movement, max_step, on_ground, self.context(bounds)) + } + /// Resolve movement with the entity's captured collision context. Most + /// callers only need the position context; entities such as falling blocks + /// also affect contextual block shapes (for example powder snow). + pub fn resolve_with_context( + &mut self, + bounds: Aabb, + movement: Vec3, + max_step: f32, + on_ground: bool, + context: CollisionContext<'_>, ) -> Result { Ok(resolve_world_movement( self, MovementRequest { entity: EntityIdentity(0), - context: self.context(bounds), + context, bounds, movement, max_step, diff --git a/crates/horizon-client/src/command/mod.rs b/crates/horizon-client/src/command/mod.rs index 52e76100a..e9aeaae51 100644 --- a/crates/horizon-client/src/command/mod.rs +++ b/crates/horizon-client/src/command/mod.rs @@ -8,6 +8,7 @@ pub mod block; pub mod codec; pub mod component; pub mod item; +pub mod network; pub mod packrat; pub mod selector; pub mod snbt; @@ -311,6 +312,9 @@ pub enum Suggests { /// ScoreboardCommand.suggestTriggers: trigger objectives some of the /// parsed `targets` have no score in, or a locked one. EnableTriggers, + /// SuggestionProviders.ASK_SERVER: the server completes the node + /// (ClientSuggestionProvider.customSuggestion); nothing locally. + AskServer, } impl Node { pub fn name(&self) -> &str { @@ -690,7 +694,7 @@ impl Suggests { Suggests::Triggers => { builder.suggest_matching(reg.trigger_objectives.iter().map(String::as_str)) } - Suggests::Nothing => {} + Suggests::Nothing | Suggests::AskServer => {} } } } @@ -954,7 +958,8 @@ impl Dispatcher { cursor: usize, reg: &Registries, ) -> (Range, Vec) { - self.suggestions_within(parse, cursor, reg, u8::MAX) + let (range, list, _) = self.suggestions_within(parse, cursor, reg, u8::MAX); + (range, list) } /// The suggestions the player's chat shows: the client's tree only holds /// the nodes the player may use (Commands.sendCommands prunes by canUse). @@ -964,6 +969,18 @@ impl Dispatcher { cursor: usize, reg: &Registries, ) -> (Range, Vec) { + let (range, list, _) = self.suggestions_within(parse, cursor, reg, reg.permission); + (range, list) + } + /// [`Self::client_suggestions`], and whether a node at the cursor asks + /// the server (`ask_server`): its completions come with the server's + /// reply, to be merged with these. + pub fn client_suggestions_asking( + &self, + parse: &ParseResults, + cursor: usize, + reg: &Registries, + ) -> (Range, Vec, bool) { self.suggestions_within(parse, cursor, reg, reg.permission) } fn suggestions_within( @@ -972,15 +989,17 @@ impl Dispatcher { cursor: usize, reg: &Registries, permission: u8, - ) -> (Range, Vec) { + ) -> (Range, Vec, bool) { let (parent, start, context) = parse.context.suggestion_context(cursor); let start = start.min(cursor); let truncated = &parse.input[..cursor.min(parse.input.len())]; let mut all = Vec::new(); + let mut ask = false; for &child in &self.nodes[parent].children { if self.nodes[child].permission > permission { continue; } + ask |= matches!(self.nodes[child].suggests, Some(Suggests::AskServer)); let mut builder = SuggestionsBuilder::new(truncated, start); match &self.nodes[child].kind { NodeKind::Literal(l) => { @@ -996,7 +1015,8 @@ impl Dispatcher { } all.extend(builder.out); } - merge_suggestions(truncated, all) + let (range, list) = merge_suggestions(truncated, all); + (range, list, ask) } fn smart_usage_of(&self, node: usize, optional: bool, deep: bool, level: u8) -> Option { let n = &self.nodes[node]; diff --git a/crates/horizon-client/src/command/network.rs b/crates/horizon-client/src/command/network.rs new file mode 100644 index 000000000..8fed97db0 --- /dev/null +++ b/crates/horizon-client/src/command/network.rs @@ -0,0 +1,403 @@ +//! The command tree a server sends (ClientboundCommandsPacket) as a +//! dispatcher the chat parses, colours and completes with +//! (ClientPacketListener.handleCommands). +//! +//! The packet holds each argument's parser and its network properties. A +//! node the client's own vanilla tree has at the same path takes that node's +//! argument type and suggestion provider (the parsers' suggestion lists and +//! the custom providers are the same code on both sides); any other node +//! (another server's or a mod's) gets the type its parser and properties +//! describe. +use super::{ArgType, Dispatcher, Node, NodeKind, Suggests}; +use horizon_protocol::packets::play::{ARGUMENT_TYPES, ArgumentProperties, Commands}; +use std::{ + collections::{HashSet, VecDeque}, + sync::{Mutex, OnceLock}, +}; + +/// The command id of every executable node of a server's tree: the chat +/// sends the command, the server runs it. +pub const SERVER_COMMAND: &str = "server"; + +/// The dispatcher for a server's tree. `local` is the client's own tree +/// (vanilla's), whose matching nodes lend their types and providers. +pub fn from_packet(packet: &Commands, local: &Dispatcher) -> Dispatcher { + let count = packet.nodes.len(); + let root = usize::try_from(packet.root) + .ok() + .filter(|&r| r < count) + .unwrap_or(0); + // The packet's root becomes node 0, the others follow in packet order. + let index = |i: usize| -> usize { + if i == root { + 0 + } else if i < root { + i + 1 + } else { + i + } + }; + let valid = |c: &i32| usize::try_from(*c).ok().filter(|&c| c < count); + // The local node at the same path, found breadth first from the roots + // along children (redirects are not paths of their own). + let mut matched: Vec> = vec![None; count]; + matched[root] = Some(0); + let mut queue = VecDeque::from([root]); + let mut seen = HashSet::from([root]); + while let Some(n) = queue.pop_front() { + for c in packet.nodes[n].children.iter().filter_map(valid) { + if !seen.insert(c) { + continue; + } + let node = &packet.nodes[c]; + matched[c] = matched[n].and_then(|l| { + local.nodes[l].children.iter().copied().find(|&lc| { + let ln = &local.nodes[lc]; + ln.name() == node.name + && matches!( + (&ln.kind, node.kind), + (NodeKind::Literal(_), 1) | (NodeKind::Argument { .. }, 2) + ) + }) + }); + queue.push_back(c); + } + } + let mut nodes = vec![None; count]; + for (i, node) in packet.nodes.iter().enumerate() { + let local_node = matched[i].map(|l| &local.nodes[l]); + let kind = match node.kind { + 1 => NodeKind::Literal(node.name.clone()), + 2 => { + let ty = match local_node.map(|l| &l.kind) { + Some(NodeKind::Argument { ty, .. }) => ty.clone(), + _ => node + .parser + .as_ref() + .map_or(ArgType::Word, |(id, props)| argument_type(*id, props)), + }; + NodeKind::Argument { + name: node.name.clone(), + ty, + } + } + _ => NodeKind::Root, + }; + // The server's providers, as the client knows them + // (ArgumentNodeStub.build): its own custom ones arrive as `ask_server`. + let suggests = node.suggestions.as_deref().and_then(provider); + nodes[index(i)] = Some(Node { + kind: if i == root { NodeKind::Root } else { kind }, + children: node.children.iter().filter_map(valid).map(index).collect(), + command: node.executable.then_some(SERVER_COMMAND), + redirect: node.redirect.as_ref().and_then(valid).map(index), + // The server sent only what the player may use. + permission: 0, + suggests, + }); + } + let mut d = Dispatcher::new(); + if count > 0 { + d.nodes = nodes.into_iter().flatten().collect(); + } + d +} + +/// SuggestionProviders by id (`SuggestionProviders.getProvider`). +fn provider(id: &str) -> Option { + match id { + "minecraft:ask_server" => Some(Suggests::AskServer), + "minecraft:summonable_entities" => Some(Suggests::SummonableEntities), + _ => None, + } +} + +/// A registry name kept for the life of the game (argument types hold +/// `&'static str`): each distinct name is stored once. +fn intern(name: &str) -> &'static str { + static NAMES: OnceLock>> = OnceLock::new(); + let mut names = NAMES + .get_or_init(Default::default) + .lock() + .unwrap_or_else(|e| e.into_inner()); + if let Some(&n) = names.get(name) { + return n; + } + let n: &'static str = Box::leak(name.to_owned().into_boxed_str()); + names.insert(n); + n +} + +/// The argument type a parser and its properties describe +/// (ArgumentTypeInfo.Template.instantiate). +pub fn argument_type(id: i32, props: &[u8]) -> ArgType { + let Some(name) = usize::try_from(id).ok().and_then(|i| ARGUMENT_TYPES.get(i)) else { + return ArgType::Word; + }; + let p = ArgumentProperties::decode(id, props).unwrap_or(ArgumentProperties::None); + let registry = || match &p { + ArgumentProperties::Registry(r) => intern(r), + _ => "", + }; + match *name { + "brigadier:bool" => ArgType::Bool, + "brigadier:integer" => match p { + ArgumentProperties::Integer { min, max } => ArgType::Integer { + min: min.unwrap_or(i32::MIN), + max: max.unwrap_or(i32::MAX), + }, + _ => ArgType::Integer { + min: i32::MIN, + max: i32::MAX, + }, + }, + // No long type of its own: a double over the same bounds reads + // the same digits. + "brigadier:long" => match p { + ArgumentProperties::Long { min, max } => ArgType::Double { + min: min.map_or(f64::MIN, |v| v as f64), + max: max.map_or(f64::MAX, |v| v as f64), + }, + _ => ArgType::Double { + min: f64::MIN, + max: f64::MAX, + }, + }, + "brigadier:float" => match p { + ArgumentProperties::Float { min, max } => ArgType::Float { + min: min.unwrap_or(f32::MIN), + max: max.unwrap_or(f32::MAX), + }, + _ => ArgType::Float { + min: f32::MIN, + max: f32::MAX, + }, + }, + "brigadier:double" => match p { + ArgumentProperties::Double { min, max } => ArgType::Double { + min: min.unwrap_or(f64::MIN), + max: max.unwrap_or(f64::MAX), + }, + _ => ArgType::Double { + min: f64::MIN, + max: f64::MAX, + }, + }, + "brigadier:string" => match p { + ArgumentProperties::String(1) => ArgType::Str, + ArgumentProperties::String(2) => ArgType::Greedy, + _ => ArgType::Word, + }, + "minecraft:entity" => match p { + ArgumentProperties::Entity { + single, + players_only, + } => ArgType::Entity { + single, + players: players_only, + }, + _ => ArgType::Entity { + single: false, + players: false, + }, + }, + "minecraft:game_profile" => ArgType::GameProfile, + "minecraft:block_pos" => ArgType::BlockPos, + "minecraft:column_pos" => ArgType::ColumnPos, + "minecraft:vec3" => ArgType::Vec3 { center: true }, + "minecraft:vec2" => ArgType::Vec2, + "minecraft:block_state" => ArgType::BlockState, + "minecraft:block_predicate" => ArgType::BlockPredicate, + "minecraft:item_stack" => ArgType::Item, + "minecraft:item_predicate" => ArgType::ItemPredicate, + "minecraft:team_color" => ArgType::TeamColor, + "minecraft:hex_color" => ArgType::HexColor, + "minecraft:component" => ArgType::Component, + "minecraft:style" => ArgType::Style, + "minecraft:message" => ArgType::Message, + "minecraft:nbt_compound_tag" => ArgType::Nbt, + "minecraft:nbt_tag" => ArgType::NbtTag, + "minecraft:nbt_path" => ArgType::NbtPath, + "minecraft:objective" => ArgType::Objective, + "minecraft:objective_criteria" => ArgType::Criteria, + "minecraft:operation" => ArgType::Operation, + "minecraft:particle" => ArgType::Particle, + "minecraft:angle" => ArgType::Angle, + "minecraft:rotation" => ArgType::Rotation, + "minecraft:scoreboard_slot" => ArgType::ScoreboardSlot, + "minecraft:score_holder" => ArgType::ScoreHolder { + multiple: matches!(p, ArgumentProperties::ScoreHolder { multiple: true }), + }, + "minecraft:swizzle" => ArgType::Swizzle, + "minecraft:team" => ArgType::Team, + "minecraft:item_slot" => ArgType::Slot, + "minecraft:item_slots" => ArgType::Slots, + "minecraft:resource_location" => ArgType::Id("none"), + "minecraft:function" => ArgType::Function, + "minecraft:entity_anchor" => ArgType::Anchor, + "minecraft:int_range" => ArgType::IntRange, + "minecraft:float_range" => ArgType::FloatRange, + "minecraft:dimension" => ArgType::Dimension, + "minecraft:gamemode" => ArgType::GameMode, + "minecraft:time" => ArgType::Time { + min: match p { + ArgumentProperties::Time { min } => min, + _ => 0, + }, + }, + "minecraft:resource_or_tag" => ArgType::ResOrTag(registry()), + "minecraft:resource_or_tag_key" => ArgType::ResOrTagKey(registry()), + "minecraft:resource" => ArgType::Res(registry()), + "minecraft:resource_key" => ArgType::ResKey(registry()), + "minecraft:resource_selector" => ArgType::ResourceSelector(registry()), + "minecraft:template_mirror" => ArgType::Enum(&["none", "front_back", "left_right"]), + "minecraft:template_rotation" => { + ArgType::Enum(&["none", "clockwise_90", "180", "counterclockwise_90"]) + } + "minecraft:heightmap" => ArgType::Enum(&[ + "world_surface", + "motion_blocking", + "motion_blocking_no_leaves", + "ocean_floor", + ]), + "minecraft:swing_animation" => ArgType::Enum(&["none", "whack", "stab"]), + "minecraft:loot_table" => ArgType::ResourceOrId("minecraft:loot_table"), + "minecraft:loot_predicate" => ArgType::ResourceOrId("minecraft:predicate"), + "minecraft:loot_modifier" => ArgType::ResourceOrId("minecraft:item_modifier"), + "minecraft:context_float_provider" => { + ArgType::ResourceOrId("minecraft:context_float_provider") + } + "minecraft:context_int_provider" => ArgType::ResourceOrId("minecraft:context_int_provider"), + "minecraft:slot_source" => ArgType::SlotSource, + "minecraft:dialog" => ArgType::ResourceOrId("minecraft:dialog"), + "minecraft:feature" => ArgType::ResourceOrId("minecraft:worldgen/feature"), + "minecraft:uuid" => ArgType::Uuid, + _ => ArgType::Word, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use horizon_protocol::{ + buf::{Codec, Reader, Writer}, + packets::play::CommandNode, + }; + + fn literal(name: &str, children: Vec, executable: bool) -> CommandNode { + CommandNode { + kind: 1, + executable, + restricted: false, + children, + redirect: None, + name: name.into(), + parser: None, + suggestions: None, + } + } + + fn argument(name: &str, parser: &str, props: Vec, executable: bool) -> CommandNode { + let id = ARGUMENT_TYPES.iter().position(|p| *p == parser).unwrap() as i32; + CommandNode { + kind: 2, + executable, + restricted: false, + children: Vec::new(), + redirect: None, + name: name.into(), + parser: Some((id, props)), + suggestions: Some("minecraft:ask_server".into()), + } + } + + /// A tree with the root last (as vanilla's numbering need not put it + /// first), a vanilla command and one the client does not know. + fn packet() -> Commands { + let mut range = Writer::new(); + range.u8(3); + range.i32(1); + range.i32(64); + Commands { + nodes: vec![ + literal("give", vec![2], false), + literal("warp", vec![3], false), + argument("targets", "minecraft:entity", vec![2], false), + argument("count", "brigadier:integer", range.data, true), + CommandNode { + kind: 0, + executable: false, + restricted: false, + children: vec![0, 1], + redirect: None, + name: String::new(), + parser: None, + suggestions: None, + }, + ], + root: 4, + } + } + + #[test] + fn argument_nodes_round_trip() { + let p = packet(); + let mut w = Writer::new(); + Commands::write(&p, &mut w); + let back = Commands::read(&mut Reader::new(&w.data)).unwrap(); + assert_eq!(back, p); + } + + #[test] + fn server_tree_becomes_a_dispatcher() { + let local = super::super::vanilla::dispatcher(); + let d = from_packet(&packet(), local); + assert!(matches!(d.nodes[0].kind, NodeKind::Root)); + let warp = d.find("warp").expect("warp"); + let count = d.nodes[warp].children[0]; + match &d.nodes[count].kind { + NodeKind::Argument { + ty: ArgType::Integer { min, max }, + .. + } => assert_eq!((*min, *max), (1, 64)), + other => panic!("count is {other:?}"), + } + assert_eq!(d.nodes[count].command, Some(SERVER_COMMAND)); + // `give ` is vanilla's: it keeps the client's type. + let give = d.find("give").expect("give"); + let targets = d.nodes[give].children[0]; + assert!(matches!( + d.nodes[targets].kind, + NodeKind::Argument { + ty: ArgType::Entity { + single: false, + players: true + }, + .. + } + )); + } + + #[test] + fn every_argument_type_decodes() { + for (id, name) in ARGUMENT_TYPES.iter().enumerate() { + let mut w = Writer::new(); + match *name { + "brigadier:integer" | "brigadier:long" | "brigadier:float" | "brigadier:double" => { + w.u8(0) + } + "brigadier:string" => w.varint(2), + "minecraft:entity" | "minecraft:score_holder" => w.u8(1), + "minecraft:time" => w.i32(0), + n if n.starts_with("minecraft:resource") && n != "minecraft:resource_location" => { + w.string("minecraft:item") + } + _ => {} + } + let mut r = Reader::new(&w.data); + ArgumentProperties::read(&mut r, id as i32).unwrap(); + assert_eq!(r.remaining(), 0, "{name}"); + let _ = argument_type(id as i32, &w.data); + } + } +} diff --git a/crates/horizon-client/src/containers.rs b/crates/horizon-client/src/containers.rs index 9cdac1463..6a135799c 100644 --- a/crates/horizon-client/src/containers.rs +++ b/crates/horizon-client/src/containers.rs @@ -45,6 +45,9 @@ pub struct Furnace { pub cooking_total: i32, pub speed: f32, } +/// A hopper's slots: the only plain container with five. +pub const HOPPER_SLOTS: usize = 5; + #[derive(Clone, Debug)] pub enum BlockEntity { Chest(Vec>), @@ -242,7 +245,7 @@ impl BlockEntity { let size = match name { "minecraft:chest" | "minecraft:trapped_chest" | "minecraft:barrel" => 27, n if n.ends_with("copper_chest") => 27, - "minecraft:hopper" => 5, + "minecraft:hopper" => HOPPER_SLOTS, "minecraft:dispenser" | "minecraft:dropper" => 9, n if n.ends_with("shulker_box") => 27, _ => return None, @@ -270,7 +273,7 @@ impl BlockEntity { pub fn id(&self) -> &'static str { match self { Self::Chest(items) => match items.len() { - 5 => "minecraft:hopper", + HOPPER_SLOTS => "minecraft:hopper", 9 => "minecraft:dispenser", _ => "minecraft:chest", }, diff --git a/crates/horizon-client/src/crafting.rs b/crates/horizon-client/src/crafting.rs index 457ddf497..0b8f2856b 100644 --- a/crates/horizon-client/src/crafting.rs +++ b/crates/horizon-client/src/crafting.rs @@ -1209,7 +1209,15 @@ impl Recipes { for (prefix, path) in files { let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or(""); let name = format!("{prefix}{stem}"); - let v: Value = serde_json::from_slice(&fs::read(&path)?)?; + // Vanilla's RecipeManager logs a file that does not parse and + // goes on ("Couldn't parse data file"). + let v: Value = match serde_json::from_slice(&fs::read(&path)?) { + Ok(v) => v, + Err(e) => { + log::warn!("Horizon: recipe {name} not loaded: {e}"); + continue; + } + }; let text = |k: &str| v[k].as_str().unwrap_or("").to_owned(); meta.insert(name.clone(), (text("category"), text("group"))); let ing = |k: &str| Ingredient::parse(&v[k], items); diff --git a/crates/horizon-client/src/effects.rs b/crates/horizon-client/src/effects.rs index 2e600333a..17dce93ab 100644 --- a/crates/horizon-client/src/effects.rs +++ b/crates/horizon-client/src/effects.rs @@ -336,67 +336,8 @@ impl PlayerEffects { pub fn modifiers(&self) -> Vec<(&'static str, &'static str, f64, &'static str)> { let mut out = Vec::new(); for e in &self.list { - let level = f64::from(e.amplifier + 1); - let m = match e.id { - "speed" => Some(( - "minecraft:movement_speed", - "minecraft:effect.speed", - 0.2, - "add_multiplied_total", - )), - "slowness" => Some(( - "minecraft:movement_speed", - "minecraft:effect.slowness", - -0.15, - "add_multiplied_total", - )), - "haste" => Some(( - "minecraft:attack_speed", - "minecraft:effect.haste", - 0.1, - "add_multiplied_total", - )), - "mining_fatigue" => Some(( - "minecraft:attack_speed", - "minecraft:effect.mining_fatigue", - -0.1, - "add_multiplied_total", - )), - "strength" => Some(( - "minecraft:attack_damage", - "minecraft:effect.strength", - 3.0, - "add_value", - )), - "weakness" => Some(( - "minecraft:attack_damage", - "minecraft:effect.weakness", - -4.0, - "add_value", - )), - "jump_boost" => Some(( - "minecraft:safe_fall_distance", - "minecraft:effect.jump_boost", - 1.0, - "add_value", - )), - "health_boost" => Some(( - "minecraft:max_health", - "minecraft:effect.health_boost", - 4.0, - "add_value", - )), - "luck" => Some(("minecraft:luck", "minecraft:effect.luck", 1.0, "add_value")), - "unluck" => Some(( - "minecraft:luck", - "minecraft:effect.unluck", - -1.0, - "add_value", - )), - _ => None, - }; - if let Some((a, id, amount, op)) = m { - out.push((a, id, amount * level, op)); + if let Some((a, id, amount, op)) = effect_modifier(e.id) { + out.push((a, id, amount * f64::from(e.amplifier + 1), op)); } } out @@ -439,6 +380,76 @@ impl StatusFlags { } } +/// The attribute modifier an effect adds at amplifier 0 +/// (`MobEffect.addAttributeModifier`): (attribute, id, amount, operation). +/// Each level multiplies the amount (`amplifier + 1`). +pub fn effect_modifier(effect: &str) -> Option<(&'static str, &'static str, f64, &'static str)> { + match effect { + "speed" => Some(( + "minecraft:movement_speed", + "minecraft:effect.speed", + 0.2, + "add_multiplied_total", + )), + "slowness" => Some(( + "minecraft:movement_speed", + "minecraft:effect.slowness", + -0.15, + "add_multiplied_total", + )), + "haste" => Some(( + "minecraft:attack_speed", + "minecraft:effect.haste", + 0.1, + "add_multiplied_total", + )), + "mining_fatigue" => Some(( + "minecraft:attack_speed", + "minecraft:effect.mining_fatigue", + -0.1, + "add_multiplied_total", + )), + "strength" => Some(( + "minecraft:attack_damage", + "minecraft:effect.strength", + 3.0, + "add_value", + )), + "weakness" => Some(( + "minecraft:attack_damage", + "minecraft:effect.weakness", + -4.0, + "add_value", + )), + "jump_boost" => Some(( + "minecraft:safe_fall_distance", + "minecraft:effect.jump_boost", + 1.0, + "add_value", + )), + "health_boost" => Some(( + "minecraft:max_health", + "minecraft:effect.health_boost", + 4.0, + "add_value", + )), + "absorption" => Some(( + "minecraft:max_absorption", + "minecraft:effect.absorption", + 4.0, + "add_value", + )), + "luck" => Some(("minecraft:luck", "minecraft:effect.luck", 1.0, "add_value")), + "unluck" => Some(( + "minecraft:luck", + "minecraft:effect.unluck", + -1.0, + "add_value", + )), + _ => None, + } +} + #[cfg(test)] mod tests { use super::*; @@ -538,4 +549,29 @@ mod tests { assert!((fx.mining_speed_factor() - 1.4).abs() < 1e-6); assert_eq!(fx.modifiers().len(), 1); } + + #[test] + fn absorption_modifier_scales_with_amplifier() { + let mut fx = PlayerEffects::default(); + fx.add(EffectInstance::new("absorption", 2400, 0)); + assert_eq!( + fx.modifiers(), + vec![( + "minecraft:max_absorption", + "minecraft:effect.absorption", + 4.0, + "add_value", + )] + ); + fx.add(EffectInstance::new("absorption", 2400, 3)); + assert_eq!( + fx.modifiers(), + vec![( + "minecraft:max_absorption", + "minecraft:effect.absorption", + 16.0, + "add_value", + )] + ); + } } diff --git a/crates/horizon-client/src/entities.rs b/crates/horizon-client/src/entities.rs index 5e6a3336a..b9c8ee896 100644 --- a/crates/horizon-client/src/entities.rs +++ b/crates/horizon-client/src/entities.rs @@ -16,6 +16,7 @@ pub struct ItemEntity { pub fire_ticks: i32, pub id: u32, pub stack: ItemStack, + pub thrower: Option<[i32; 4]>, pub position: Vec3, pub previous: Vec3, pub velocity: Vec3, @@ -178,6 +179,8 @@ impl ItemEntity { /// ExperienceOrb: gravity 0.03, homing towards the player within 8 blocks. #[derive(Clone, Debug)] pub struct ExperienceOrb { + /// The entity id servers track it by. + pub id: u32, pub position: Vec3, pub previous: Vec3, pub velocity: Vec3, @@ -532,15 +535,16 @@ impl ArmorStand { Vec3::new(p.x + w, p.y + h, p.z + w), ) } - /// LivingEntity.travel with gravity 0.08 and 0.98 drag, falling until it - /// lands (armor stands have no other movement). + /// LivingEntity.travelInAir (armor stands have no other movement): move + /// by the velocity (a landing stops the fall, + /// `Block.updateEntityMovementAfterFallOn`), then gravity 0.08 and the + /// 0.98 drag, so a stand at rest on the ground keeps -0.0784 as vanilla's. pub fn tick(&mut self, scene: &mut crate::collisions::SceneCollisions) { if self.marker { return; } - let mut v = self.velocity; - v.y -= 0.08; - let wanted = v; + let wanted = self.velocity; + let mut y = wanted.y; if let Ok(moved) = scene.resolve(self.bounds(), wanted, 0.0, self.on_ground) { self.position = Vec3::new( self.position.x + moved.x, @@ -548,9 +552,11 @@ impl ArmorStand { self.position.z + moved.z, ); self.on_ground = wanted.y < 0.0 && moved.y > wanted.y; - v = Vec3::new(0.0, if moved.y != wanted.y { 0.0 } else { v.y * 0.98 }, 0.0); + if moved.y != wanted.y { + y = 0.0; + } } - self.velocity = v; + self.velocity = Vec3::new(0.0, (y - 0.08) * f64::from(0.98f32), 0.0); } } @@ -596,9 +602,10 @@ mod fire_environment_tests { return; } let assets = crate::assets::Assets::load(&root).unwrap(); + let render = assets.reload(&root).unwrap(); let facts = BlockFacts::load(&root, assets.registry).unwrap(); let items = crate::items::Items::load(&root, assets.registry, &facts).unwrap(); - let mut world = World::test_scene(&assets, 3).unwrap(); + let mut world = World::test_scene(&assets, &render, 3).unwrap(); let at = Vec3::new(20.5, 4.0, 20.5); world .set( @@ -610,6 +617,7 @@ mod fire_environment_tests { .unwrap(); let make = |id: &str| ItemEntity { id: 1, + thrower: None, stack: ItemStack { item: std::sync::Arc::from(id), count: 1, diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index adeb1da25..ee86211a1 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -840,9 +840,12 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap>, id: u32) - m.species(), crate::mobs::Species::Slime | crate::mobs::Species::MagmaCube ) { - let health = m.health; + // AbstractCubeMob.readAdditionalSaveData: the size's attributes, then + // the saved health, the max health when there is none. crate::mobs::cube::set_size(&mut m, number(tag.get("Size")).unwrap_or(0) as i32 + 1); - m.health = health; + if let Some(h) = float(tag.get("Health")) { + m.health = h as f32; + } m.extra.flag2 = flag(tag, "wasOnGround"); } // Zombie, Piglin and Zoglin.readAdditionalSaveData: IsBaby (the baby @@ -863,7 +866,7 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap>, id: u32) - Some(m) } -/// `ItemEntity`: Health, Age, PickupDelay, Item. +/// `ItemEntity`: Health, Age, PickupDelay, Item, Thrower. pub fn item_entity_to_nbt(e: &ItemEntity, uuid: [i32; 4], raw: Option<&Tag>) -> Tag { let base = Base { id: "minecraft:item", @@ -887,6 +890,11 @@ pub fn item_entity_to_nbt(e: &ItemEntity, uuid: [i32; 4], raw: Option<&Tag>) -> ] { put(&mut tag, key, value); } + if let Some(thrower) = e.thrower { + put(&mut tag, "Thrower", Tag::IntArray(thrower.to_vec())); + } else if let Tag::Compound(map) = &mut tag { + map.remove(&"Thrower".into()); + } tag } pub fn item_entity_from_nbt(tag: &Tag, id: u32, bob_offset: f32) -> Option { @@ -897,6 +905,7 @@ pub fn item_entity_from_nbt(tag: &Tag, id: u32, bob_offset: f32) -> Option Vec { }; let count = number(tag.get("Count")).unwrap_or(1).clamp(1, 64) as usize; let orb = ExperienceOrb { + id: 0, position, previous: position, velocity: motion(tag), @@ -1529,6 +1539,7 @@ mod tests { let e = ItemEntity { health: 2, fire_ticks: 73, + thrower: Some([5, 6, 7, 8]), id: 7, stack: ItemStack { item: Arc::from("minecraft:diamond_pickaxe"), @@ -1555,6 +1566,11 @@ mod tests { (120, 10, true) ); assert_eq!(back.position.x, 100.5); + assert_eq!(back.thrower, e.thrower); + let mut cleared = back; + cleared.thrower = None; + let cleared = item_entity_to_nbt(&cleared, [1, 2, 3, 4], Some(&tag)); + assert!(cleared.get("Thrower").is_none()); } #[test] fn unknown_fields_survive() { @@ -1575,6 +1591,7 @@ mod tests { #[test] fn orbs_expand_their_count() { let o = ExperienceOrb { + id: 0, position: Vec3::new(1.0, 2.0, 3.0), previous: Vec3::ZERO, velocity: Vec3::ZERO, diff --git a/crates/horizon-client/src/flatgen.rs b/crates/horizon-client/src/flatgen.rs index d7764c30e..339f1fca1 100644 --- a/crates/horizon-client/src/flatgen.rs +++ b/crates/horizon-client/src/flatgen.rs @@ -4,8 +4,9 @@ use horizon_world::{nbt::Tag, registry::BlockRegistry}; /// The layers of a flat generator, bottom up from the world's minimum Y. pub struct FlatTerrain { - layers: Vec, + layers: Vec, biome: u16, + surface: Option, } impl FlatTerrain { /// The overworld generator of `world_gen_settings` (`data.dimensions`), if it @@ -30,13 +31,44 @@ impl FlatTerrain { for layer in list { let block = crate::save::text(layer.get("block"))?; let height = crate::save::number(layer.get("height")).unwrap_or(0).max(0); - let state = registry.default_state(&block)?.0 as u16; + let state = registry.default_state(&block)?.0; 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. @@ -44,9 +76,119 @@ impl FlatTerrain { crate::chunk::MIN_Y + self.layers.len().min(crate::chunk::HEIGHT as usize) as i32 } } +/// DimensionType.Y_SIZE: the most layers FlatLevelGeneratorSettings takes. +const Y_SIZE: i64 = 4064; + +/// FlatLevelGeneratorSettings.CODEC reading a dedicated server's +/// `generator-settings` (DedicatedServerProperties.WorldDimensionData for +/// the `minecraft:flat` preset): the settings as `world_gen_settings` stores +/// them, and the errors vanilla logs, in order (the constructor's, then +/// `resultOrPartial`'s). No settings means the preset's own layers; a layer +/// that doesn't parse is left out, as the codec's partial result leaves it. +/// `is_block` says whether a block id is registered, `is_biome` a biome id. +pub fn settings_from_json( + json: &crate::server::json::Json, + is_block: impl Fn(&str) -> bool, + is_biome: impl Fn(&str) -> bool, +) -> (Option, Vec) { + use crate::server::json::Json; + let id = |s: &str| { + if s.contains(':') { + s.to_owned() + } else { + format!("minecraft:{s}") + } + }; + let Some(Json::Array(list)) = json.get("layers") else { + let error = match json.get("layers") { + None => format!("No key layers in MapLike[{}]", json.compact()), + Some(v) => format!("Not a list: {}", v.compact()), + }; + return (None, vec![error]); + }; + let mut errors = Vec::new(); + let mut layers = Vec::new(); + let mut total = 0; + for layer in list { + let height = layer.get("height").and_then(Json::as_string); + let height = height.as_deref().and_then(|h| h.parse::().ok()); + let block = layer.get("block").and_then(Json::as_string).map(|b| id(&b)); + match (height, block) { + (Some(h), _) if !(0..=Y_SIZE).contains(&h) => { + errors.push(format!("Value {h} outside of range [0:{Y_SIZE}]")); + } + (Some(h), Some(b)) if is_block(&b) => { + total += h; + layers.push(Tag::compound([ + ("height", Tag::Int(h as i32)), + ("block", Tag::String(b.as_str().into())), + ])); + } + (Some(_), Some(b)) => errors.push(format!( + "Unknown registry key in ResourceKey[minecraft:root / minecraft:block]: {b}" + )), + (None, _) => errors.push(format!("No key height in MapLike[{}]", layer.compact())), + (_, None) => errors.push(format!("No key block in MapLike[{}]", layer.compact())), + } + } + // The constructor: Biome.CODEC is lenient, and a biome that is missing + // or unknown is Plains. + let biome = json + .get("biome") + .and_then(Json::as_string) + .map(|b| id(&b)) + .filter(|b| is_biome(b)); + let mut logged = Vec::new(); + if biome.is_none() { + logged.push("Unknown biome, defaulting to plains".to_owned()); + } + let biome = biome.unwrap_or_else(|| "minecraft:plains".to_owned()); + // validateHeight. + if total > Y_SIZE { + errors.push(format!("Sum of layer heights is > {Y_SIZE}")); + logged.push(errors.join("; ")); + return (None, logged); + } + let flag = |key: &str| match json.get(key) { + Some(Json::Bool(b)) => *b, + Some(Json::Number(n)) => n.parse::().is_ok_and(|v| v != 0.0), + _ => false, + }; + let mut settings = Vec::new(); + // RegistryCodecs.holderSet, lenient too: a tag or a list of ids. + match json.get("structure_overrides") { + Some(Json::String(s)) => { + settings.push(("structure_overrides", Tag::String(s.as_str().into()))) + } + Some(Json::Array(ids)) if ids.iter().all(|i| matches!(i, Json::String(_))) => settings + .push(( + "structure_overrides", + Tag::List( + ids.iter() + .filter_map(Json::as_string) + .map(|i| Tag::String(id(&i).as_str().into())) + .collect(), + ), + )), + _ => {} + } + settings.extend([ + ("layers", Tag::List(layers)), + ("lakes", Tag::Byte(i8::from(flag("lakes")))), + ("features", Tag::Byte(i8::from(flag("features")))), + ("biome", Tag::String(biome.as_str().into())), + ]); + if !errors.is_empty() { + logged.push(errors.join("; ")); + } + (Some(Tag::compound(settings)), logged) +} impl ChunkGenerator for FlatTerrain { type Worker = (); fn worker(&self) {} + fn lod_surface(&self, _: &mut (), _: i32, _: i32) -> Option { + 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 { @@ -75,3 +217,71 @@ impl ChunkGenerator for FlatTerrain { *proto.downcast::().expect("flat terrain proto") } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::save::{number, text}; + use crate::server::json::{self, Json}; + + fn read(text: &str) -> (Option, Vec) { + let json: Json = json::parse(text, false).unwrap(); + settings_from_json( + &json, + |b| ["minecraft:stone", "minecraft:bedrock"].contains(&b), + |b| ["minecraft:plains", "minecraft:desert"].contains(&b), + ) + } + + #[test] + fn generator_settings_read_as_flat_level_generator_settings() { + // server.properties' default: the preset's own layers, and the codec's error. + assert_eq!( + read("{}"), + (None, vec!["No key layers in MapLike[{}]".into()]) + ); + let (settings, errors) = read( + r#"{"layers":[{"block":"bedrock","height":1},{"block":"minecraft:stone","height":3}],"biome":"desert","features":true}"#, + ); + assert!(errors.is_empty()); + let settings = settings.unwrap(); + let Some(Tag::List(layers)) = settings.get("layers") else { + panic!("{settings:?}") + }; + assert_eq!(layers.len(), 2); + assert_eq!( + text(layers[0].get("block")).as_deref(), + Some("minecraft:bedrock") + ); + assert_eq!(number(layers[1].get("height")), Some(3)); + assert_eq!( + text(settings.get("biome")).as_deref(), + Some("minecraft:desert") + ); + assert_eq!(number(settings.get("features")), Some(1)); + assert_eq!(number(settings.get("lakes")), Some(0)); + // A layer that doesn't parse is left out; an unknown biome is Plains, and logged first. + let (settings, errors) = read( + r#"{"layers":[{"block":"nope","height":2},{"block":"stone","height":2}],"biome":"x"}"#, + ); + assert_eq!(errors.len(), 2); + assert_eq!(errors[0], "Unknown biome, defaulting to plains"); + assert!(errors[1].contains("minecraft:nope")); + let settings = settings.unwrap(); + let Some(Tag::List(layers)) = settings.get("layers") else { + panic!("{settings:?}") + }; + assert_eq!(layers.len(), 1); + assert_eq!( + text(settings.get("biome")).as_deref(), + Some("minecraft:plains") + ); + // validateHeight. + assert_eq!( + read( + r#"{"layers":[{"block":"stone","height":4000},{"block":"stone","height":100}],"biome":"plains"}"# + ), + (None, vec!["Sum of layer heights is > 4064".into()]) + ); + } +} diff --git a/crates/horizon-client/src/fog.rs b/crates/horizon-client/src/fog.rs index 83f1a8bf5..942bae556 100644 --- a/crates/horizon-client/src/fog.rs +++ b/crates/horizon-client/src/fog.rs @@ -32,6 +32,7 @@ pub struct FogData { } /// Per-biome water fog end multipliers (`visual/water_fog_end_distance`). +#[derive(Default)] pub struct FogTables { water_fog_end: Vec, } @@ -180,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, @@ -336,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`. @@ -368,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, @@ -446,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, diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 117b83bc5..225c1a5e2 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -10,6 +10,7 @@ mod banners; mod beacon; mod beehives; mod block_entity_anims; +mod block_entity_ticks; mod block_items; mod blockctl; mod boats; @@ -24,6 +25,7 @@ mod command_blocks; mod command_harness; mod commands; mod composter; +mod consume; mod control; mod copper_golems; mod creaking; @@ -63,13 +65,18 @@ mod item_particles; mod jukebox; mod lan; mod lan_actions; +mod lan_attributes; +mod lan_chat; mod lan_crafting; +mod lan_entities; mod lan_mobs; mod lan_pvp; mod lan_screen; mod lan_trading; +mod lan_waypoints; mod leads; mod level_particles; +mod levels; mod lightning; mod llamas; mod loom; @@ -105,6 +112,8 @@ mod powder_snow; mod raids; mod recipe_book; mod remote; +mod remote_objects; +mod remote_projectiles; mod render_control; mod render_tick; mod respawn_anchor; @@ -112,12 +121,14 @@ mod scoreboard; mod screens; mod sculk; mod sculk_fixture; +mod server_travel; mod shader_packs; mod shield; mod sign_edit; mod sniffers; mod spawner; mod spill; +mod startup_menu; mod station_control; mod stations; mod stats; @@ -143,7 +154,7 @@ mod wither; mod zombie_villagers; use horizon_client::{ Result, - assets::Assets, + assets::{Assets, RenderAssets}, audio::Audio, biome::Biomes, blocks::{BlockFacts, Fluid}, @@ -185,6 +196,8 @@ use horizon_core::{ use horizon_core::{geometry::Vec3, random::RandomSource}; use horizon_world::{ block_collision::BlockCollisionRegistry, + collision_context::{CollisionContext, CollisionEntity, CollisionItem}, + entity_obstruction::{EntityIdentity, EntityObstructionWorld, ObstructionEntity}, level::{ Behaviours, Dimension as LevelDimension, EntityQuery, Landing, LevelAccess, LevelEvent, PlaceContext, PlaceOutcome, PlayerContext, PlayerEffect, Ticks, flags as level_flags, @@ -194,7 +207,12 @@ use horizon_world::{ use std::{ collections::{HashMap, HashSet}, path::PathBuf, - sync::Arc, + sync::{ + Arc, + atomic::{AtomicU32, Ordering}, + mpsc::{Receiver, TryRecvError}, + }, + thread::JoinHandle, time::{Duration, Instant}, }; use winit::{ @@ -433,17 +451,30 @@ struct Hit { face: [i32; 3], point: [f64; 3], } +/// A guest's right click comes as one packet per hand and part +/// (`Minecraft.startUseItem`): `ServerGamePacketListenerImpl.handleUseItemOn` +/// does the block's and `ItemStack.useOn`'s part, `handleUseItem` the +/// item's own (`Item.use`). Running both for either would use the item twice. +#[derive(Clone, Copy)] +enum GuestUse { + OnBlock(Hand), + Item(Hand), +} /// The client world as seen by the vanilla block-behaviour library. struct GameLevel<'w> { world: &'w mut World, facts: &'w BlockFacts, + collision: &'w BlockCollisionRegistry<'w>, events: &'w mut Vec, dimension: LevelDimension, darken: i32, raining: bool, thundering: bool, player_box: Option<([f64; 3], [f64; 3])>, + /// The LAN guests in this level who trigger blocks (not spectators). + guest_boxes: Vec<([f64; 3], [f64; 3])>, + player_collision: Option, /// Minecart boxes (detector rails). cart_boxes: Vec<([f64; 3], [f64; 3])>, biomes: &'w Biomes, @@ -458,8 +489,39 @@ struct GameLevel<'w> { rand_sequences: &'w mut horizon_client::random_sequences::RandomSequences, pending: &'w mut spill::Pending, spill: &'w mut spill::Spill, + /// Where the chunk tickets let blocks tick. + tickets: horizon_client::ticket_levels::Levels, +} +struct PlayerCollision { + y: f64, + descending: bool, + fall_distance: f64, + main_hand_item: Arc, + main_hand_count: i32, + leather_boots: bool, +} +struct PlayerObstruction(Aabb); +impl EntityObstructionWorld for PlayerObstruction { + type Error = std::convert::Infallible; + fn obstruction_candidates( + &mut self, + _excluded: Option, + _bounds: Aabb, + ) -> std::result::Result, Self::Error> { + Ok(vec![ObstructionEntity { + identity: EntityIdentity(0), + root_vehicle: EntityIdentity(0), + bounds: self.0, + removed: false, + spectator: false, + blocks_building: true, + }]) + } } impl LevelAccess for GameLevel<'_> { + fn world_surface(&self, x: i32, z: i32) -> i32 { + self.world.world_surface(x, z, |s| self.facts.is_air(s)) + } fn precipitation(&self, pos: BlockPos) -> Option { self.behaviours.precipitation_info(self, pos) } @@ -571,24 +633,71 @@ impl LevelAccess for GameLevel<'_> { fn dimension(&self) -> LevelDimension { self.dimension } - /// Level.isUnobstructed against the player for full-collision blocks. + /// `Level.isUnobstructed`: the placed state's collision shape against the player. fn is_unobstructed(&self, pos: BlockPos, state: BlockStateId) -> bool { let Some((lo, hi)) = self.player_box else { return true; }; - if !self.facts.get(state).full_collision { + let player = self.player_collision.as_ref(); + let context = player.map_or_else( + || CollisionContext::position(lo[1]), + |player| { + CollisionContext::for_entity( + CollisionEntity { + identity: EntityIdentity(0), + y: player.y, + descending: player.descending, + main_hand: Some(CollisionItem { + item: &player.main_hand_item, + count: player.main_hand_count, + }), + }, + false, + ) + }, + ); + let mut scene = collisions::SceneCollisions { + world: &*self.world, + binding: self.collision, + subject: player.map(|player| horizon_client::collisions::PowderSnowSubject { + fall_distance: player.fall_distance, + falling_block: false, + leather_boots: player.leather_boots, + walkable_type: false, + lava_walker: false, + }), + }; + let Ok(shape) = self + .collision + .collision_shape_with_context(&mut scene, state, pos, &context) + else { + return false; + }; + if shape.is_empty() { return true; } - let (x, y, z) = (f64::from(pos.x), f64::from(pos.y), f64::from(pos.z)); - !(x < hi[0] - 1e-7 - && x + 1.0 > lo[0] + 1e-7 - && y < hi[1] - 1e-7 - && y + 1.0 > lo[1] + 1e-7 - && z < hi[2] - 1e-7 - && z + 1.0 > lo[2] + 1e-7) + let shape = shape.moved(horizon_core::geometry::Vec3::new( + f64::from(pos.x), + f64::from(pos.y), + f64::from(pos.z), + )); + horizon_world::entity_obstruction::is_unobstructed( + &mut PlayerObstruction(Aabb::new( + horizon_core::geometry::Vec3::new(lo[0], lo[1], lo[2]), + horizon_core::geometry::Vec3::new(hi[0], hi[1], hi[2]), + )), + None, + &shape, + ) + .unwrap_or(false) + } + /// `ServerLevel.isPositionTickingWithEntitiesLoaded`: block ticking, and + /// a ticking chunk. + fn is_ticking_chunk(&self, chunk: horizon_core::position::ChunkPos) -> bool { + self.tickets.block_ticking(chunk.x, chunk.z) && ticking_chunk(self.world, chunk.x, chunk.z) } /// Entities that trigger pressure plates, tripwires and detector rails: - /// the player (a mob) and the minecarts. + /// the players (the host and the LAN guests, mobs) and the minecarts. fn count_entities(&self, area: Aabb, query: EntityQuery) -> usize { let hit = |(lo, hi): &([f64; 3], [f64; 3])| { area.intersects(Aabb::new( @@ -597,10 +706,17 @@ impl LevelAccess for GameLevel<'_> { )) }; let carts = || self.cart_boxes.iter().filter(|b| hit(b)).count(); + let players = || { + self.player_box + .iter() + .chain(&self.guest_boxes) + .filter(|b| hit(b)) + .count() + }; match query { EntityQuery::Minecart => carts(), - EntityQuery::Mobs => self.player_box.iter().filter(|b| hit(b)).count(), - EntityQuery::Everything => self.player_box.iter().filter(|b| hit(b)).count() + carts(), + EntityQuery::Mobs => players(), + EntityQuery::Everything => players() + carts(), } } fn emit(&mut self, event: LevelEvent) { @@ -639,6 +755,8 @@ impl LevelAccess for GameLevel<'_> { /// FallingBlockEntity carrying a block state. #[derive(Clone, Debug)] struct FallingBlock { + /// The entity id a server tracks it by (0 until it is tracked). + id: u32, state: BlockStateId, /// `DATA_START_POS`: spawn position used to select weighted block models. start_pos: BlockPos, @@ -661,12 +779,23 @@ struct FallingBlock { /// recipes, biomes, mobs, collision and motion. Read-only and `Send + Sync`, /// so the client and an integrated server thread can share one copy. pub struct Resources { + core: WorldCore, + rest: std::sync::OnceLock, +} + +struct WorldCore { facts: BlockFacts, + biomes: Arc, + environment: Environment, + collision: BlockCollisionRegistry<'static>, + motion: MotionResources, + threads: usize, +} + +struct WorldRest { items: Items, recipes: Recipes, transformers: horizon_client::block_transform::Transformers, - biomes: Arc, - environment: Environment, mob_types: HashMap>, mob_data: horizon_client::mobs::MobData, providers: Providers, @@ -678,14 +807,105 @@ pub struct Resources { nether_environment: Environment, end_environment: Environment, behaviours: Behaviours, - collision: BlockCollisionRegistry<'static>, - motion: MotionResources, - threads: usize, player_arm: Option, player_type: Option>, - /// Enchantments, workstation recipes and brewing (game/stations.rs). stations: horizon_client::stations::StationData, } + +impl Resources { + fn rest(&self) -> &WorldRest { + self.rest + .get() + .expect("world resources joined before entry") + } + fn facts(&self) -> &BlockFacts { + &self.core.facts + } + fn items(&self) -> &Items { + &self.rest().items + } + fn recipes(&self) -> &Recipes { + &self.rest().recipes + } + fn transformers(&self) -> &horizon_client::block_transform::Transformers { + &self.rest().transformers + } + fn biomes(&self) -> &Arc { + &self.core.biomes + } + fn environment(&self) -> &Environment { + &self.core.environment + } + fn mob_types(&self) -> &HashMap> { + &self.rest().mob_types + } + fn mob_data(&self) -> &horizon_client::mobs::MobData { + &self.rest().mob_data + } + fn providers(&self) -> &Providers { + &self.rest().providers + } + fn block_entity_render(&self) -> &horizon_client::block_entity_render::BlockEntityRenderer { + &self.rest().block_entity_render + } + fn animate(&self) -> &horizon_client::animate_tick::AnimateTables { + &self.rest().animate + } + fn fog(&self) -> &horizon_client::fog::FogTables { + &self.rest().fog + } + fn music(&self) -> &horizon_client::music::MusicTables { + &self.rest().music + } + fn ambient(&self) -> &horizon_client::ambience::AmbientTables { + &self.rest().ambient + } + fn nether_environment(&self) -> &Environment { + &self.rest().nether_environment + } + fn end_environment(&self) -> &Environment { + &self.rest().end_environment + } + fn behaviours(&self) -> &Behaviours { + &self.rest().behaviours + } + fn collision(&self) -> &BlockCollisionRegistry<'static> { + &self.core.collision + } + fn motion(&self) -> &MotionResources { + &self.core.motion + } + fn threads(&self) -> usize { + self.core.threads + } + fn player_arm(&self) -> &Option { + &self.rest().player_arm + } + fn player_type(&self) -> &Option> { + &self.rest().player_type + } + fn stations(&self) -> &horizon_client::stations::StationData { + &self.rest().stations + } +} + +/// Everything the title and menus draw: the GUI atlas with its font and the +/// title panorama. Kept apart from the world bundle so the title can appear +/// before the block sprites, models and gameplay tables are ready. +pub struct UiResources { + /// The GUI, font, particle, map and skin atlas. + pub atlas: HudAtlas, + /// The title screen's panorama faces. + pub panorama: Arc, +} +impl UiResources { + pub fn load(root: &std::path::Path) -> Result { + Ok(Self { + atlas: HudAtlas::build(root)?, + panorama: Arc::new(horizon_client::gfx::panorama::Faces::load(root)), + }) + } +} /// What a resource pack reload replaces (`Minecraft.reloadResourcePacks`): /// block models and sprites with their meshing tables, item models, /// translations and the textures read on the CPU. Each game holds its own; @@ -693,9 +913,9 @@ pub struct Resources { #[derive(Clone)] pub struct PackResources { assets: Arc, + render: Arc, context: Arc, layer_names: Arc>, - panorama: Arc, /// Item definitions, baked item models and special renderers. item_renderer: Arc, names: Arc, @@ -705,23 +925,25 @@ pub struct PackResources { forcefield: Arc<(u32, u32, Vec)>, } impl PackResources { - /// Everything a pack overlay at `root` provides, over `assets` loaded from it. + /// Everything a pack overlay at `root` provides, over `assets` and `render` + /// loaded from it. fn load( root: &std::path::Path, assets: Arc, + render: Arc, facts: &BlockFacts, biomes: Arc, light: Arc, ) -> Result { let visibility = models::load_visibility(root, assets.registry)?; - let tables = Arc::new(MeshTables::new(&assets, facts, visibility)?); + let tables = Arc::new(MeshTables::new(&assets, &render, facts, visibility)?); let item_renderer = horizon_client::item_render::ItemRenderer::load_with_library( root, - assets.model_library.clone(), + render.model_library.clone(), )?; Ok(Self { layer_names: Arc::new( - assets + render .sprite_layers .iter() .map(|(name, layer)| (*layer, name.trim_start_matches("minecraft:").to_owned())) @@ -729,12 +951,13 @@ impl PackResources { ), context: Arc::new(WorkerContext { assets: assets.clone(), + render: render.clone(), tables, biomes, light, }), assets, - panorama: Arc::new(horizon_client::gfx::panorama::Faces::load(root)), + render, item_renderer: Arc::new(item_renderer), names: Arc::new(Names::load(root)), clouds: Arc::new(load_clouds(root)), @@ -749,6 +972,12 @@ struct DragView { left: Option, } /// Ctrl+Q reads the physical keyboard, independently of sprint key mappings. +/// A chunk whose ticking future can complete (`ChunkMap.prepareTickingChunk`): +/// lit, with its neighbours loaded, as `postprocess_chunks` waits for. +fn ticking_chunk(world: &World, x: i32, z: i32) -> bool { + world.chunks.get(&(x, z)).is_some_and(|c| c.lit) + && (-1..=1).all(|dz| (-1..=1).all(|dx| world.chunks.contains_key(&(x + dx, z + dz)))) +} fn drop_whole_stack(physical_keys: &HashSet) -> bool { physical_keys.contains(&KeyCode::ControlLeft) || physical_keys.contains(&KeyCode::ControlRight) } @@ -767,17 +996,24 @@ fn select_creative_tab( struct Game<'a> { /// Shared with an integrated server; `collision` and `motion` borrow from it. res: Arc, + rest_loader: Option, + pending_entry: Option, + pending_pack_reload: bool, + world_renderer_ready: bool, collision: &'a BlockCollisionRegistry<'a>, motion: &'a MotionResources, /// This game's resource packs (`reload_packs` replaces them). pack: PackResources, - /// The GUI, font, particle, map and skin atlas. - atlas: HudAtlas, + /// The title and menu bundle (GUI atlas, font and panorama). + gui: UiResources, /// `FogRenderer`'s evolving state. fog: horizon_client::fog::FogState, screen_fx: horizon_client::screen_effects::ScreenEffects, world: World, streaming: Streaming, + lod: Option, + lod_source_attempted: bool, + lod_observe: horizon_client::fast_hash::FxHashSet<(i32, i32)>, options: Options, player: Option, spawn: Option, @@ -966,12 +1202,23 @@ struct Game<'a> { /// `ClientAvatarState`: walked distance and view bob. avatar: horizon_client::camera::AvatarBob, flash: u32, - /// Sleeping in a bed: (bed head position, ticks asleep). - sleeping: Option<([i32; 3], u32)>, + /// The head of the bed while sleeping; the visual timer also survives waking. + sleeping: Option<[i32; 3]>, + sleep_timer: u32, dim: Dim, world_root: PathBuf, /// A player carried through a portal, re-placed once the destination loads. travelling: Option<(PlayerMotion, Vec3)>, + /// A server's players on their way to another level, each waiting for + /// the destination to load around them. + server_trips: Vec, + /// Players a command sends to another level, once it has run. + level_teleports: Vec, + /// A server's levels other than the one in place (game/levels.rs). + levels: levels::Levels, + /// Which pass of a server tick runs (`tick_server`): the first one reads + /// the network, the last one ends the players' tick. + level_pass: levels::Pass, /// Nether portals: the player's, items' and mobs' (game/nether_portal.rs). portals: nether_portal::Portals, /// The pending travel goes through an End portal (EndPortalBlock destinations). @@ -986,6 +1233,7 @@ struct Game<'a> { end: end::EndState, block_ticks: Ticks, tick_chunks: horizon_client::tick_chunk_membership::TickChunkMembership, + tickets: horizon_client::ticket_levels::ChunkTickets, random_tick_sections: horizon_client::random_tick_sections::RandomTickSections, level_events: Vec, falling: Vec, @@ -1115,14 +1363,13 @@ struct Game<'a> { saved_for_exit: bool, /// The block a guest is acting on (game/lan_actions.rs). forced_hit: Option, - /// A guest's `ServerboundUseItemPacket`: the held item alone, no block - /// or entity (`ServerGamePacketListenerImpl.handleUseItem`). - lan_item_only: bool, + /// The part of a right click a guest's packet asks for (game/lan_actions.rs). + guest_use: Option, /// Level events of this tick for the guests, and the guest acting now. lan_events: Vec<(i32, [i32; 3], i32, Option)>, /// Block events (position, b0, b1, state) and entity events (id, event, /// position) for the guests. - lan_block_events: Vec<([i32; 3], i32, i32, u16)>, + lan_block_events: Vec<([i32; 3], i32, i32, u32)>, lan_entity_events: Vec<(i32, i8, [f64; 3])>, /// `ContainerOpenersCounter`: how many players (the host and the guests) /// have each chest open. @@ -1134,6 +1381,16 @@ struct Game<'a> { lan_acting_uuid: Option, /// Command feedback to forward to a guest. chat_capture: Option>, + /// Packets for the guest acting now, sent once its action returns. + guest_packets: Vec, + /// `broadcastToAdmins` lines a guest's command queued, sent once it + /// returns: the line, whether operators get it and whether the console + /// logs it. + admin_feedback: Vec<(horizon_client::text::Text, bool, bool)>, + /// The server console is running a command: whether its feedback goes to + /// the operators (`DedicatedServer.shouldInformAdmins`, + /// `broadcast-console-to-ops`). + console_broadcast: Option, /// State of `/spectate`, `/posteffect`, `/debug`, `/waypoint` and friends. cmds: commands::extra::CommandExtras, ui: chat::UiState, @@ -1153,7 +1410,7 @@ struct Game<'a> { tab_list: horizon_client::tab_list::TabList, subtitles: subtitle_hud::SubtitleState, adv: advancements::AdvState, - restore_player_environment: bool, + restore_player_state: bool, stats_ui: stats_screen::StatsUi, adv_ui: advancements_screen::AdvUi, camera_type: perspective::CameraType, @@ -1161,7 +1418,7 @@ struct Game<'a> { /// The local player as a model (third person, the inventory). player_model: Option, } -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] enum Dim { Overworld, Nether, @@ -1182,6 +1439,21 @@ impl Dim { _ => Self::Overworld, } } + /// `Level.getMinY`: the dimension type's minimum y. + fn min_y(self) -> i32 { + match self { + Self::Overworld => MIN_Y, + Self::Nether | Self::End => 0, + } + } + /// `DimensionType.logicalHeight`. + fn logical_height(self) -> i32 { + match self { + Self::Overworld => HEIGHT, + Self::Nether => 128, + Self::End => 256, + } + } /// `Level.getMaxY`: the dimension type's minimum y plus height, less one. fn max_y(self) -> i32 { match self { @@ -1240,7 +1512,7 @@ impl Game<'_> { /// A block's outline boxes in world space (`getShape` plus the random offset). fn outline_boxes(&self, pos: [i32; 3]) -> Vec<[f64; 6]> { let state = self.world.block(pos[0], pos[1], pos[2]); - let f = self.res.facts.get(state); + let f = self.res.facts().get(state); let o = if f.outline_offset { self.pack .context @@ -1302,8 +1574,8 @@ impl Game<'_> { let mut t = 0.0; while t <= reach { let state = self.world.block(pos[0], pos[1], pos[2]); - let f = self.res.facts.get(state); - if !self.res.facts.is_air(state) + let f = self.res.facts().get(state); + if !self.res.facts().is_air(state) && !(f.fluid != Fluid::None && !f.waterlogged && &*f.name != "minecraft:kelp" @@ -1405,7 +1677,11 @@ impl Game<'_> { } /// Inventory.add: returns the stack that did not fit. fn give(&mut self, mut stack: ItemStack) -> Option { - let max = self.res.items.get(&stack.item).map_or(64, |i| i.max_stack); + let max = self + .res + .items() + .get(&stack.item) + .map_or(64, |i| i.max_stack); // Prefer the selected slot, then the hotbar, then the main inventory. let order: Vec = std::iter::once(self.selected) .chain((0..36).filter(|&i| i != self.selected)) @@ -1441,7 +1717,7 @@ impl Game<'_> { fn particle_sprite(&self, state: BlockStateId) -> Option<(String, u32)> { let models = self .pack - .assets + .render .model_catalog .collect(state, horizon_core::position::BlockPos::new(0, 0, 0)) .ok()?; @@ -1456,7 +1732,7 @@ impl Game<'_> { .or_else(|| models.iter().find_map(|(_, model)| model.particle))?; let name = self.pack.layer_names.get(&layer)?.clone(); let tint = if face.is_some_and(|face| face.quad.tint_index >= 0) { - let n = &self.res.facts.get(state).name; + let n = &self.res.facts().get(state).name; if n.contains("leaves") || n.contains("vine") { 0x48B518 } else { @@ -1468,7 +1744,7 @@ impl Game<'_> { Some((name, tint)) } fn particle_shape(&self, pos: [i32; 3], state: BlockStateId) -> Vec<[f64; 6]> { - let f = self.res.facts.get(state); + let f = self.res.facts().get(state); let offset = if f.outline_offset { self.pack .context @@ -1524,6 +1800,7 @@ impl Game<'_> { self.dropped.push(ItemEntity { health: 5, fire_ticks: 0, + thrower: None, id: self.next_entity, stack, position: p, @@ -1545,14 +1822,14 @@ impl Game<'_> { ) -> Vec { let registry = self.pack.assets.registry; let dynamic = self.pending.dynamic(pos); - match self.res.items.block_loot_sequence(registry, state) { + match self.res.items().block_loot_sequence(registry, state) { Some(sequence) => { let random = self.rand_sequences.get(&sequence, self.level.seed); self.res - .items + .items() .block_drops_with(registry, state, held, &dynamic, random) } - None => self.res.items.block_drops_with( + None => self.res.items().block_drops_with( registry, state, held, @@ -1606,6 +1883,10 @@ impl Game<'_> { yaw.cos() * pitch.cos() * 0.3 + spread.sin() * strength, ]; self.spawn_item(thrown, [eye[0], eye[1] - 0.3, eye[2]], v, 40); + if self.lan_acting.is_none() { + let uuid = self.level.ensure_player_uuid(); + self.dropped.last_mut().unwrap().thrower = Some(uuid); + } } fn tick_items(&mut self) { let mut items = std::mem::take(&mut self.dropped); @@ -1618,14 +1899,14 @@ impl Game<'_> { ) { return true; } - if !item.tick(&self.world, &self.res.facts, self.collision) { + if !item.tick(&self.world, self.res.facts(), self.collision) { return false; } let environment = item.environment_tick( &self.world, - &self.res.facts, + self.res.facts(), self.pack.assets.registry, - &self.res.items, + self.res.items(), ); // shortcut: age is the fresh-drop clock; use entity ticks for saved/frozen-age phase parity. if environment.should_play_lava_hurt_sound(item.age) { @@ -1728,6 +2009,22 @@ impl Game<'_> { ], ) }); + let player_collision = self.player.as_ref().filter(|_| !spectator).map(|p| { + let main_hand = self.inventory.get(self.selected).and_then(Option::as_ref); + PlayerCollision { + y: p.position.y, + descending: p.velocity.y < 0.0, + fall_distance: p.fall_distance, + main_hand_item: main_hand.map_or_else( + || Arc::from("minecraft:air"), + |stack| Arc::clone(&stack.item), + ), + main_hand_count: main_hand.map_or(0, |stack| stack.count as i32), + leather_boots: self.armor[3] + .as_ref() + .is_some_and(|stack| stack.item.as_ref() == "minecraft:leather_boots"), + } + }); let cart_boxes = self .carts .iter() @@ -1739,6 +2036,7 @@ impl Game<'_> { ) }) .collect(); + let guest_boxes = self.lan_block_trigger_boxes(); let dimension = match self.dim { Dim::Overworld => LevelDimension::Overworld, Dim::Nether => LevelDimension::Nether, @@ -1748,25 +2046,29 @@ impl Game<'_> { let mut level = GameLevel { cart_boxes, world: &mut self.world, - facts: &self.res.facts, + facts: self.res.facts(), + collision: self.collision, events: &mut self.level_events, dimension, darken, raining: rain > 0.2, thundering: thunder > 0.9, player_box, - biomes: &self.res.biomes, + guest_boxes, + player_collision, + biomes: self.res.biomes(), seed: self.level.seed, - behaviours: &self.res.behaviours, - items: &self.res.items, + behaviours: self.res.behaviours(), + items: self.res.items(), loot: horizon_client::structure_blocks::loot_data(&self.options.resources), registry: self.pack.assets.registry, rules, rand_sequences: &mut self.rand_sequences, pending: &mut self.pending, spill: &mut self.spill, + tickets: self.tickets.levels(), }; - let result = f(&self.res.behaviours, &mut level, &mut self.block_ticks); + let result = f(self.res.behaviours(), &mut level, &mut self.block_ticks); self.flush_spill(); result } @@ -1801,27 +2103,68 @@ impl Game<'_> { } for &key in self.tick_chunks.removed() { self.random_tick_sections.remove(&key); + self.portals.sections.remove(&key); self.block_ticks .remove_chunk(horizon_core::position::ChunkPos::new(key.0, key.1)); } self.tick_chunks.recycle_added(added); } - /// ServerLevel.tick block phases: scheduled ticks, random ticks within the - /// simulation distance, block events and moving pistons. + /// `DistanceManager`'s tickets of the level in place, from every player in + /// it (the local player, LAN guests, a server's players) and its forced + /// chunks. A game with neither a server nor a player (the headless + /// fixtures) ticks everywhere. + fn update_chunk_tickets(&mut self) { + let mut players: Vec<(i32, i32)> = self.lan.as_ref().map_or_else(Vec::new, |h| { + h.hub + .guests + .iter() + .filter(|g| h.hub.here(g)) + .map(|g| g.chunk()) + .collect() + }); + if let Some(p) = &self.player { + players.push(( + (p.position.x.floor() as i32) >> 4, + (p.position.z.floor() as i32) >> 4, + )); + } + let sources = (self.options.dedicated.is_some() || !players.is_empty()).then(|| { + horizon_client::ticket_levels::Sources { + players, + forced: self.streaming.forced.iter().copied().collect(), + view_distance: self.settings.render_distance.clamp(2, 32), + simulation_distance: self.settings.simulation_distance.max(2), + } + }); + self.tickets.update(sources); + } + /// `ServerLevel.startTickingChunk`: a loaded chunk's saved ticks are + /// scheduled from the game time at which it becomes a ticking chunk + /// (block ticking, with its neighbours loaded). Chunks become ticking + /// between ticks in vanilla: before this tick's clock advanced. + fn start_ticking_chunks(&mut self) { + let levels = self.tickets.levels(); + let world = &self.world; + let started = self + .tick_chunks + .start_ticking(|(x, z)| levels.loaded_ticking(x, z) && ticking_chunk(world, x, z)); + let time = self.world.game_time - 1; + for (x, z) in started { + let pos = horizon_core::position::ChunkPos::new(x, z); + self.block_ticks.blocks.unpack_chunk(pos, time); + self.block_ticks.fluids.unpack_chunk(pos, time); + } + } + /// ServerLevel.tick block phases: scheduled ticks, random ticks in the + /// chunks the tickets simulate, block events and moving pistons. fn tick_blocks(&mut self) -> Result<()> { self.sync_tick_chunks(); - let Some(p) = self.player.as_ref() else { - return Ok(()); - }; - let (pcx, pcz) = ( - (p.position.x.floor() as i32) >> 4, - (p.position.z.floor() as i32) >> 4, - ); - let simulation = self.settings.render_distance.min(8); + self.start_ticking_chunks(); + let levels = self.tickets.levels(); let mut ticking = Vec::new(); - let facts = &self.res.facts; + let facts = self.res.facts(); for (&(x, z), chunk) in &self.world.chunks { - if (x - pcx).abs() > simulation || (z - pcz).abs() > simulation || !chunk.lit { + if !levels.random_ticking(x, z) || !ticking_chunk(&self.world, x, z) { continue; } // Capture eligibility before inside-block and scheduled updates, as @@ -1831,8 +2174,9 @@ impl Game<'_> { ticking.push((x, z, sections)); } // Entity.checkInsideBlocks for the plates, tripwires and detector rails - // under the player and the minecarts. + // under the players (the host and the LAN guests) and the minecarts. let mut inside = Vec::new(); + let guests = self.lan_block_trigger_boxes(); let boxes = self .player .iter() @@ -1851,7 +2195,8 @@ impl Game<'_> { [p.x - 0.49, p.y, p.z - 0.49], [p.x + 0.49, p.y + 0.7, p.z + 0.49], ) - })); + })) + .chain(guests); for (lo, hi) in boxes { for x in (lo[0] + 1e-7).floor() as i32..=(hi[0] - 1e-7).floor() as i32 { for y in (lo[1] + 1e-7).floor() as i32..=(hi[1] - 1e-7).floor() as i32 { @@ -1921,10 +2266,10 @@ impl Game<'_> { let state = BlockStateId(data as u32); let p = [pos.x, pos.y, pos.z]; self.destroy_particles(p, state); - if self.res.facts.is_air(state) { + if self.res.facts().is_air(state) { continue; } - let sounds = self.res.facts.get(state).sounds.clone(); + let sounds = self.res.facts().get(state).sounds.clone(); let c = [ f64::from(pos.x) + 0.5, f64::from(pos.y) + 0.5, @@ -1951,6 +2296,11 @@ impl Game<'_> { pos, data, } => self.dragon_egg_particles([pos.x, pos.y, pos.z], data), + LevelEvent::LevelEvent { + id: 2017, + pos, + data, + } => self.consume_teleport_particles([pos.x, pos.y, pos.z], data), LevelEvent::LevelEvent { id: 2013, pos, @@ -2026,6 +2376,7 @@ impl Game<'_> { f64::from(pos.z) + 0.5, ); self.falling.push(FallingBlock { + id: 0, state, start_pos: pos, position: p, @@ -2035,7 +2386,7 @@ impl Game<'_> { no_gravity: false, drop_item: true, fall_distance: 0.0, - damage: if self.res.facts.get(state).name.ends_with("anvil") { + damage: if self.res.facts().get(state).name.ends_with("anvil") { horizon_client::falling_block::Damage::anvil() } else { Default::default() @@ -2148,7 +2499,7 @@ impl Game<'_> { fn open_menu(&mut self, pos: [i32; 3]) { let name = self .res - .facts + .facts() .get(self.world.block(pos[0], pos[1], pos[2])) .name .clone(); @@ -2175,13 +2526,13 @@ impl Game<'_> { self.audio .play("minecraft:block.ender_chest.open", Some(at), 0.5, pitch); } else if &*name == "minecraft:beacon" && self.open_station(pos, &name) { - self.world.block_entities.entry(key).or_insert_with(|| { + self.world.block_entities.get_or_insert_with(key, || { BlockEntity::for_block(&name).unwrap_or(BlockEntity::Chest(Vec::new())) }); } else if storage::is_shulker_box(&name) && !self.shulker_can_open(pos) { // ShulkerBoxBlock.canOpen: the lid has no room; nothing opens. } else if let Some(entity) = BlockEntity::for_block(&name) { - self.world.block_entities.entry(key).or_insert(entity); + self.world.block_entities.get_or_insert_with(key, || entity); let centre = [ f64::from(key.0) + 0.5, f64::from(key.1) + 0.5, @@ -2255,65 +2606,92 @@ impl Game<'_> { let mut falling = std::mem::take(&mut self.falling); let mut landed = Vec::new(); let mut dropped = Vec::new(); + let mut collision_error = None; let entity_drops = self.ui.rules.bool("entity_drops"); + // ServerLevel.tick: only entities in entity-ticking chunks tick. + let ticking = self.entity_ticking_area(); falling.retain_mut(|f| { + if !ticking(f.position.x.floor() as i32, f.position.z.floor() as i32) { + return true; + } + let original = f.clone(); f.previous = f.position; f.age = f.age.wrapping_add(1); if !f.no_gravity { f.velocity.y -= 0.04; } + let wanted = f.velocity; + let mut scene = collisions::SceneCollisions { + world: &self.world, + binding: self.collision, + subject: Some(collisions::PowderSnowSubject { + fall_distance: f.fall_distance, + falling_block: true, + ..Default::default() + }), + }; + let context = CollisionContext::for_entity( + CollisionEntity { + identity: EntityIdentity(0), + y: f.position.y, + descending: wanted.y < 0.0, + main_hand: None, + }, + false, + ); + let movement = match scene.resolve_with_context( + horizon_client::falling_block::bounds(f.position), + wanted, + 0.0, + false, + context, + ) { + Ok(movement) => movement, + Err(error) => { + *f = original; + collision_error.get_or_insert(error); + return true; + } + }; let next = Vec3::new( - f.position.x + f.velocity.x, - f.position.y + f.velocity.y, - f.position.z + f.velocity.z, + f.position.x + movement.x, + f.position.y + movement.y, + f.position.z + movement.z, ); - let below = self.world.block( - next.x.floor() as i32, - next.y.floor() as i32, - next.z.floor() as i32, - ); - let below_facts = self.res.facts.get(below); - // PowderSnowBlock.getCollisionShape for a FallingBlockEntity: the - // whole block, or 0.9 high after a fall of more than 2.5 blocks, - // where the block it rests in is the snow itself. - let powder_snow = &*below_facts.name == "minecraft:powder_snow"; - let sunk = powder_snow - && f.fall_distance > 2.5 - && next.y - next.y.floor() < f64::from(0.9_f32); - let solid = - (below_facts.full_collision || powder_snow && f.fall_distance <= 2.5 || sunk) - && f.velocity.y < 0.0; + let on_ground = wanted.y < 0.0 && movement.y != wanted.y; // FallingBlockEntity.tick: off the ground, it gives up after 600 // ticks (100 outside the world) and drops itself as an item. let y = next.y.floor() as i32; let outside = y <= horizon_client::chunk::MIN_Y || y >= horizon_client::chunk::MIN_Y + 384; - if !solid && (f.age > 100 && outside || f.age > 600) { + if !on_ground && (f.age > 100 && outside || f.age > 600) { if f.drop_item && entity_drops { dropped.push((f.state, next)); } return false; } - if solid { - let pos = [ - next.x.floor() as i32, - next.y.floor() as i32 + i32::from(!sunk), - next.z.floor() as i32, - ]; - let landing_y = if sunk { - next.y.floor() + f64::from(0.9_f32) - } else { - f64::from(pos[1]) - }; - f.fall_distance += (f.position.y - landing_y).max(0.0); - f.position = Vec3::new(next.x, landing_y, next.z); - landed.push((f.clone(), pos)); - return false; - } if next.y < f.position.y { f.fall_distance += f.position.y - next.y; } f.position = next; + if on_ground { + let pos = [ + f.position.x.floor() as i32, + f.position.y.floor() as i32, + f.position.z.floor() as i32, + ]; + landed.push((f.clone(), pos)); + return false; + } + if movement.x != wanted.x { + f.velocity.x = 0.0; + } + if movement.y != wanted.y { + f.velocity.y = 0.0; + } + if movement.z != wanted.z { + f.velocity.z = 0.0; + } f.velocity = Vec3::new( f.velocity.x * 0.98, f.velocity.y * 0.98, @@ -2324,7 +2702,7 @@ impl Game<'_> { self.falling = falling; for (state, at) in dropped { // Entity.spawnAtLocation: an item entity where the block was. - let item = self.res.facts.get(state).item.clone(); + let item = self.res.facts().get(state).item.clone(); if &*item != "minecraft:air" { let r = &mut self.world.random; let v = [ @@ -2344,7 +2722,80 @@ impl Game<'_> { for (block, pos) in landed { self.finish_falling_block(block, pos); } - self.process_level_events() + self.process_level_events()?; + if let Some(error) = collision_error { + return Err(error); + } + Ok(()) + } + /// `DecoratedPotBlock.playerWillDestroy`: a pot mined with an item in + /// `breaks_decorated_pots`, unless an enchantment prevents shattering, is + /// cracked first (flags 260), so it shatters into its sherds. + fn crack_mined_pot(&mut self, pos: [i32; 3], state: BlockStateId) -> BlockStateId { + let registry = &self.pack.assets.registry; + let Some(mut pot) = registry + .state(state) + .filter(|s| s.name == "minecraft:decorated_pot") + .cloned() + else { + return state; + }; + let Some(held) = self.held() else { + return state; + }; + let breaks = self + .res + .items() + .item_tags + .get("minecraft:breaks_decorated_pots") + .is_some_and(|t| t.contains(&*held.item)); + let enchantments = &self.res.stations().enchantments; + let kept = held + .enchantments() + .iter() + .any(|(e, _)| enchantments.in_tag("minecraft:prevents_decorated_pot_shattering", e)); + if !breaks || kept { + return state; + } + pot.properties.insert("cracked".into(), "true".into()); + let Ok(cracked) = registry.resolve(&pot) else { + return state; + }; + let at = BlockPos::new(pos[0], pos[1], pos[2]); + self.with_level(|b, level, ticks| b.set_block(level, ticks, at, cracked, 260)); + cracked + } + /// `ItemStack.mineBlock` for the held stack once a survival player has + /// mined the block `name`: an item with a `tool` component counts as used + /// and loses its `damage_per_block` on a block that doesn't break + /// instantly (`ShearsItem`: on any block but fire). + fn mine_block_with_held(&mut self, name: &str, hardness: f32) { + let Some(stack) = self.inventory[self.selected].clone() else { + return; + }; + let Some((per_block, max)) = self + .res + .items() + .get(&stack.item) + .and_then(|i| Some((i.tool_damage_per_block?, i.max_damage))) + else { + return; + }; + let wears = if &*stack.item == "minecraft:shears" { + !self.res.items().block_in_tag("minecraft:fire", name) + } else { + hardness != 0.0 + }; + if wears && per_block > 0 && max > 0 { + let loss = self.durability_loss(&stack, per_block); + if let Some(held) = self.inventory[self.selected].as_mut() { + held.damage += loss; + if held.damage >= max { + self.inventory[self.selected] = None; + } + } + } + self.stats.add("used", &stack.item, 1); } fn break_block(&mut self, pos: [i32; 3]) -> Result<()> { self.with_player_actor(|game| game.break_block_as_player(pos)) @@ -2353,7 +2804,8 @@ impl Game<'_> { self.pad_rumble(horizon_client::gamepad::Rumble::BlockBroken); // ServerPlayerGameMode.destroyBlock. let state = self.world.block(pos[0], pos[1], pos[2]); - let facts = self.res.facts.get(state).clone(); + let facts = self.res.facts().get(state).clone(); + let state = self.crack_mined_pot(pos, state); // Block.playerWillDestroy: mining their gold angers all nearby piglins. if self.guarded_by_piglins(&facts.name) { self.anger_piglins(false); @@ -2385,26 +2837,12 @@ impl Game<'_> { }); if removed && !creative { let held = self.held().cloned(); - // ItemStack.mineBlock: tools lose durability on blocks with nonzero hardness. - if facts.hardness > 0.0 - && let Some(stack) = self.inventory[self.selected].clone() - && let Some(max) = self - .res - .items - .get(&stack.item) - .filter(|i| i.max_damage > 0 && !i.tool.is_empty()) - .map(|i| i.max_damage) - { - let loss = self.durability_loss(&stack, 1); - if let Some(stack) = self.inventory[self.selected].as_mut() { - stack.damage += loss; - if stack.damage >= max { - self.inventory[self.selected] = None; - } - } - } + self.mine_block_with_held(&facts.name, facts.hardness); let harvest = !facts.requires_tool - || self.res.items.correct_for_drops(held.as_ref(), &facts.name); + || self + .res + .items() + .correct_for_drops(held.as_ref(), &facts.name); if harvest { // Block.playerDestroy: the mined statistic, the exhaustion and the drops. self.stats.add("mined", &facts.name, 1); @@ -2459,13 +2897,18 @@ impl Game<'_> { return Ok(None); } // A carrot or warped fungus on a stick boosts the ridden pig or strider. - if self.riding.is_some() + // A guest's use packets name one hand and touch no entity: the client + // sends one per hand and part of its click, and Interact for entities. + let guest = self.guest_use; + let on_block = matches!(guest, Some(GuestUse::OnBlock(_))); + if !on_block + && self.riding.is_some() && let Some(item) = self.held().map(|s| s.item.clone()) && self.use_steering_stick(&item) { return Ok(Some(Hand::Main)); } - if !self.lan_item_only + if guest.is_none() && (self.interact_frame() || self.interact_armor_stand() || self.interact_boat() @@ -2476,12 +2919,16 @@ impl Game<'_> { return Ok(Some(Hand::Main)); } // An EntityHitResult may fall through to Item.use, never to useItemOn. - let block_hit = if self.lan_item_only || self.entity_targeted() { + let block_hit = if matches!(guest, Some(GuestUse::Item(_))) || self.entity_targeted() { None } else { self.pick() }; - for hand in Hand::BOTH { + let hands = match guest { + Some(GuestUse::OnBlock(hand) | GuestUse::Item(hand)) => vec![hand], + None => Hand::BOTH.to_vec(), + }; + for hand in hands { if self.with_hand(hand, |game| game.use_hand(block_hit.as_ref()))? { return Ok(Some(hand)); } @@ -2541,17 +2988,7 @@ impl Game<'_> { [-1, 0, 0] => Direction::West, _ => Direction::East, }; - let player = PlayerContext { - yaw: self.yaw, - pitch: self.pitch, - may_build: self.may_build(), - can_eat: self.creative - || self - .player - .as_ref() - .is_some_and(|p| p.health.food.food < 20), - game_master: self.can_use_game_master_blocks(), - }; + let player = self.use_context(); let at = Vec3::new(hit.point[0], hit.point[1], hit.point[2]); let item = held.as_ref().map(|s| s.item.to_string()); let result = self.with_level(|b, level, ticks| { @@ -2575,12 +3012,16 @@ impl Game<'_> { let Some(stack) = self.held().cloned() else { return Ok(false); }; - let Some(info) = self.res.items.get(&stack.item).cloned() else { + let Some(info) = self.res.items().get(&stack.item).cloned() else { return Ok(false); }; if block_hit.is_some() && (self.use_bone_meal(&stack)? || self.ignite_tnt(&stack)?) { return Ok(true); } + // A guest's UseItemOn stops at ItemStack.useOn: what Item.use does + // (throw, drink, eat, raise, equip) waits for its UseItem. + let on_block = matches!(self.guest_use, Some(GuestUse::OnBlock(_))); + let spawn_egg = stack.item.ends_with("_spawn_egg"); use horizon_client::first_person::UseResult; let swings = self.swings_started; match self.use_tool(&stack, block_hit.is_some())? { @@ -2593,30 +3034,33 @@ impl Game<'_> { UseResult::Fail => return Ok(true), UseResult::Pass => {} } - if self.use_potion_item(&stack)? - || ((block_hit.is_some() || !stack.item.ends_with("_spawn_egg")) + if (!on_block && self.use_potion_item(&stack)?) + || ((block_hit.is_some() || !spawn_egg) + && (!on_block || spawn_egg) && self.throw_item(&stack)?) { return Ok(true); } - if self.raise_shield(self.held_slot()) - || self.use_fishing_rod(&stack) + if !on_block + && (self.raise_shield(self.held_slot()) + || self.use_fishing_rod(&stack) || self.use_crossbow(&stack) || self.use_trident(&stack) || self.boost_firework(&stack) || self.use_empty_map(&stack) - || self.use_book(&stack) - // Block items (heads, carved pumpkins) aimed at a block are - // placed first (useItemOn before useItem). - || (!(info.block.is_some() && block_hit.is_some()) && self.equip_from_hand(&stack)) + || self.use_book(&stack) + // Block items (heads, carved pumpkins) aimed at a block are + // placed first (useItemOn before useItem). + || (!(info.block.is_some() && block_hit.is_some()) + && self.equip_from_hand(&stack))) { return Ok(true); } - if &*stack.item == "minecraft:spyglass" { + if !on_block && &*stack.item == "minecraft:spyglass" { self.use_spyglass(); return Ok(true); } - if &*stack.item == "minecraft:bow" { + if !on_block && &*stack.item == "minecraft:bow" { if self.using.is_none() && (self.creative || self.projectile_slot("minecraft:bow").is_some()) { @@ -2625,11 +3069,11 @@ impl Game<'_> { return Ok(true); } // Block foods attempt placement before starting consumption. - if !(info.block.is_some() && block_hit.is_some()) && self.start_eating(&info) { + if !on_block && !(info.block.is_some() && block_hit.is_some()) && self.start_eating(&info) { return Ok(true); } // BoatItem.use also performs its own fluid/block raycast. - if self.place_boat(&stack) { + if !on_block && self.place_boat(&stack) { return Ok(true); } if let Some(hit) = block_hit @@ -2653,7 +3097,7 @@ impl Game<'_> { } if !self.may_build() { // ItemStack.useOn passes in adventure mode; useItem eats. - return Ok(self.start_eating(&info)); + return Ok(!on_block && self.start_eating(&info)); } let face = match hit.face { [0, 1, 0] => Direction::Up, @@ -2686,7 +3130,7 @@ impl Game<'_> { return Ok(true); }; let target = [pos.x, pos.y, pos.z]; - if let Some(entity) = BlockEntity::for_block(&self.res.facts.get(state).name) { + if let Some(entity) = BlockEntity::for_block(&self.res.facts().get(state).name) { let key = (target[0], target[1], target[2]); self.world.block_entities.insert(key, entity); let game_master = self.can_use_game_master_blocks(); @@ -2730,15 +3174,29 @@ impl Game<'_> { } Ok(true) } - /// `Consumable.startConsuming` for held food the player can eat - /// (`Player.canEat`): the right click is held for `consume_seconds`. + /// The player as a block's `useItemOn` / `useWithoutItem` sees them. + fn use_context(&self) -> PlayerContext { + PlayerContext { + yaw: self.yaw, + pitch: self.pitch, + may_build: self.may_build(), + can_eat: self.creative + || self + .player + .as_ref() + .is_some_and(|p| p.health.food.food < 20), + game_master: self.can_use_game_master_blocks(), + } + } + /// `Consumable.startConsuming` for a held consumable: food needs + /// `Player.canEat` (`Consumable.canConsume`), anything else is always + /// consumed. The right click is held for `consume_seconds`. fn start_eating(&mut self, info: &horizon_client::items::ItemInfo) -> bool { - if !info.consumable - || info.food.is_none() - || !self - .player - .as_ref() - .is_some_and(|p| p.health.food.food < 20 || self.creative) + if info.consumable.is_none() + || (info.food.is_some() + && !self.player.as_ref().is_some_and(|p| { + info.can_always_eat || p.health.food.food < 20 || self.creative + })) { return false; } @@ -2747,6 +3205,60 @@ impl Game<'_> { } true } + fn apply_consume_effect(&mut self, effect: &horizon_client::items::ConsumeEffect) { + use horizon_client::items::ConsumeEffect; + match effect { + ConsumeEffect::ApplyEffects { + effects, + probability, + } => { + // ApplyStatusEffectsConsumeEffect.apply: the user's random + // (the level's stands in, as for every player draw) is drawn + // even at probability 1. + if self.world.random.next_float() < *probability { + for eff in effects { + if let Some(def) = horizon_client::brewing::effect(&eff.id) { + if def.instant { + if let Some(a) = + horizon_client::effects::instant(def.id, eff.amplifier, 1.0) + { + self.apply_effect_action(a); + } + } else { + self.add_player_effect( + horizon_client::effects::EffectInstance::new( + def.id, + eff.duration, + eff.amplifier, + ), + ); + } + } + } + } + } + ConsumeEffect::ClearAllEffects => { + self.potions.effects.clear(); + self.apply_effect_status(); + } + ConsumeEffect::RemoveEffects(to_remove) => { + for target in to_remove { + let id = target.trim_start_matches("minecraft:"); + self.potions.effects.list.retain(|e| e.id != id); + } + self.apply_effect_status(); + } + ConsumeEffect::TeleportRandomly(teleport) => self.teleport_randomly(teleport), + ConsumeEffect::PlaySound(sound) => { + // PlaySoundConsumeEffect: at the user's block position. + let at = self.player.as_ref().map(|p| { + let b = horizon_core::position::BlockPos::containing(p.position); + [b.x, b.y, b.z].map(|v| f64::from(v) + 0.5) + }); + self.audio.play(sound, at, 1.0, 1.0); + } + } + } fn consume_held(&mut self) { if self.creative { return; @@ -2791,7 +3303,7 @@ impl Game<'_> { self.using = None; return; } - let Some(info) = self.res.items.get(&stack.item).cloned() else { + let Some(info) = self.res.items().get(&stack.item).cloned() else { self.using = None; return; }; @@ -2825,46 +3337,21 @@ impl Game<'_> { } return; } - let duration = 32; - // Consumable.shouldEmitParticlesAndSounds(useItemRemainingTicks), with - // the remaining ticks before this tick counts down: every 4 ticks once - // more than 21.875% of the time has passed (none on the last tick). + let Some(consumable) = info.consumable.as_ref() else { + self.using = None; + return; + }; + // ItemStack.onUseTick: Consumable.shouldEmitParticlesAndSounds with + // the remaining ticks before this tick counts them down. + let duration = consumable.ticks().max(0) as u32; let remaining = duration - ticks.min(duration); let ticks = ticks + 1; - let eye = self.eye_position().unwrap_or_default(); - if duration - remaining > (duration as f32 * 0.21875) as u32 && remaining % 4 == 0 { - let pitch = - (self.world.random.next_float() - self.world.random.next_float()) * 0.2 + 1.0; - self.audio.play( - "minecraft:entity.generic.eat", - Some(eye), - 0.5 + 0.5 * self.world.random.next_int_bound(2).unwrap_or(0) as f32, - pitch, - ); - self.eat_particles(&stack, 5); + if consumable.should_emit(remaining as i32) { + self.emit_consume(consumable, &stack, 5); } if ticks >= duration { self.using = None; - // Consumable.onConsume: emitParticlesAndSounds(..., 16). - self.eat_particles(&stack, 16); - // Consumable.onConsume. - self.post_entity_event("minecraft:eat", horizon_client::mobs::Who::Player); - if let Some((nutrition, saturation)) = info.food - && let Some(p) = self.player.as_mut() - { - // FoodData.eat(nutrition, saturation) - let food = &mut p.health.food; - food.food = (food.food + nutrition as i32).clamp(0, 20); - food.saturation = (food.saturation + saturation).clamp(0.0, food.food as f32); - self.audio.play( - "minecraft:entity.player.burp", - Some(eye), - 0.5, - self.world.random.next_float() * 0.1 + 0.9, - ); - self.trigger_item("minecraft:consume_item", &stack); - self.consume_held(); - } + self.finish_consuming(&info, consumable, &stack); } else { self.using = Some((slot, ticks)); } @@ -2905,13 +3392,13 @@ impl Game<'_> { }); } let state = self.world.block(hit.pos[0], hit.pos[1], hit.pos[2]); - let facts = self.res.facts.get(state); + let facts = self.res.facts().get(state); if facts.hardness < 0.0 { return Ok(()); } // Player.getDestroySpeed / BlockBehaviour.getDestroyProgress. let held = self.held(); - let mut speed = self.res.items.mining_speed(held, &facts.name); + let mut speed = self.res.items().mining_speed(held, &facts.name); speed = self.modify_destroy_speed(speed); let player = self.player.as_ref().ok_or("no player")?; if !player.ground.on_ground { @@ -2920,7 +3407,7 @@ impl Game<'_> { let eye = self.eye_position().unwrap_or_default(); if self .res - .facts + .facts() .get(self.world.block( eye[0].floor() as i32, eye[1].floor() as i32, @@ -2931,7 +3418,7 @@ impl Game<'_> { { speed *= self.submerged_mining_speed(); } - let harvest = !facts.requires_tool || self.res.items.correct_for_drops(held, &facts.name); + let harvest = !facts.requires_tool || self.res.items().correct_for_drops(held, &facts.name); let progress = if facts.hardness == 0.0 { 1.0 } else { @@ -2971,11 +3458,11 @@ impl Game<'_> { if self.dim == Dim::Nether { // Below the bedrock ceiling: a solid floor with two air blocks above. for y in (32..120).rev() { - let floor = self.res.facts.get(self.world.block(x, y - 1, z)); + let floor = self.res.facts().get(self.world.block(x, y - 1, z)); if floor.full_collision && floor.fluid == Fluid::None - && self.res.facts.is_air(self.world.block(x, y, z)) - && self.res.facts.is_air(self.world.block(x, y + 1, z)) + && self.res.facts().is_air(self.world.block(x, y, z)) + && self.res.facts().is_air(self.world.block(x, y + 1, z)) { return Some(y); } @@ -2985,8 +3472,8 @@ impl Game<'_> { let mut y = MIN_Y + HEIGHT - 1; while y > MIN_Y { let s = self.world.block(x, y, z); - if !self.res.facts.is_air(s) { - let f = self.res.facts.get(s); + if !self.res.facts().is_air(s) { + let f = self.res.facts().get(s); let ok = f.full_collision && f.fluid == Fluid::None && !f.name.contains("leaves") @@ -3072,14 +3559,16 @@ impl Game<'_> { }; let mut player = PlayerMotion::new(self.motion, feet)?; player.yaw = self.yaw; - player.load_environment_once( - &mut self.restore_player_environment, - self.level.player_extra.as_ref(), - ); + player.load_saved_state_once(&mut self.restore_player_state, &self.level)?; let spectator = self.mode == horizon_client::command::args::GameMode::Spectator; player.may_fly = self.creative || spectator; player.invulnerable = self.creative || spectator; player.spectator = spectator; + if spectator { + player.flying = true; + } else if !player.may_fly { + player.flying = false; + } player.respiration = self.armor[0] .as_ref() .map_or(0, |s| s.enchantment_level("minecraft:respiration") as i32); @@ -3087,14 +3576,16 @@ impl Game<'_> { // LivingEntity.canGlideUsing: a glider in its slot that will not break next. player.glider = self.armor[1].as_ref().is_some_and(|s| { self.res - .items + .items() .get(&s.item) .is_some_and(|i| i.glider && (i.max_damage == 0 || s.damage + 1 < i.max_damage)) }); - if self.level.health > 0.0 && self.ticks < 2 { - player.set_health(self.level.health, false)?; - player.health.food.food = self.level.food; - player.health.food.saturation = self.level.saturation; + // A saved death already dropped inventory/XP and counted the death. + if player.health.dead_or_dying() { + self.screen = Screen::Dead; + self.ui.died_at = Some(Instant::now()); + self.ui.death_text = None; + self.capture(false); } self.previous_position = feet; self.player = Some(player); @@ -3179,7 +3670,7 @@ impl Game<'_> { if !self.streaming.is_lit(cx, cz) { return Ok(()); } - let playing = self.screen == Screen::Playing; + let playing = self.screen == Screen::Playing && self.sleeping.is_none(); let held = |k| playing && self.keys.contains(&k); let keys = Keys { forward: held(KeyCode::KeyW), @@ -3209,7 +3700,7 @@ impl Game<'_> { // LivingEntity.canGlideUsing: a glider in its slot that will not break next. player.glider = self.armor[1].as_ref().is_some_and(|s| { self.res - .items + .items() .get(&s.item) .is_some_and(|i| i.glider && (i.max_damage == 0 || s.damage + 1 < i.max_damage)) }); @@ -3223,7 +3714,7 @@ impl Game<'_> { self.inventory.get(slot)?.as_ref() } }) - .and_then(|s| self.res.items.get(&s.item)) + .and_then(|s| self.res.items().get(&s.item)) .map(|i| i.use_speed); // PowderSnowBlock.canEntityWalkOnPowderSnow and LivingEntity.canFreeze. player.leather_boots = self.armor[3] @@ -3231,7 +3722,7 @@ impl Game<'_> { .is_some_and(|s| &*s.item == "minecraft:leather_boots"); let immune = self .res - .items + .items() .item_tags .get("minecraft:freeze_immune_wearables"); player.freeze_immune = self @@ -3273,7 +3764,12 @@ impl Game<'_> { let feet = [player.position.x, player.position.y, player.position.z]; let player_velocity = [player.velocity.x, player.velocity.y, player.velocity.z]; self.previous_eye = self.eye; - self.eye += (camera_eye.unwrap_or(player.dimensions.eye_height) - self.eye) * 0.5; + let eye_height = if self.sleeping.is_some() { + 0.2 + } else { + player.dimensions.eye_height + }; + self.eye += (camera_eye.unwrap_or(eye_height) - self.eye) * 0.5; let crouching = player.pose == horizon_client::pose::Pose::Crouching; self.tick_avatar(before, riding); self.record_combat(&events); @@ -3283,7 +3779,7 @@ impl Game<'_> { MotionEvent::Emission { state, in_water, .. } if !crouching => { - let sounds = self.res.facts.get(state).sounds.clone(); + let sounds = self.res.facts().get(state).sounds.clone(); // Entity.playStepSound: the block's step sound at 0.15 volume, // muffled under the swim sound in water. let (volume, pitch) = if in_water { @@ -3458,6 +3954,10 @@ impl Game<'_> { } self.audio .play("minecraft:entity.player.death", Some(at), 1.0, 1.0); + // LivingEntity.die: the LAN guests see the host's death animation. + if let Some(host) = &self.lan { + host.hub.entity_event(host.host_entity, 3, feet); + } self.screen = Screen::Dead; self.capture(false); } @@ -3466,6 +3966,7 @@ impl Game<'_> { /// What the tick runs after the local player's own update: effects, the /// world's entities and blocks, and the network hub. fn tick_world(&mut self, running: bool) -> Result<()> { + self.update_chunk_tickets(); horizon_client::spans::time("tick_effects", || self.tick_effects()); horizon_client::spans::time("tick_using", || self.tick_using()); horizon_client::spans::time("tick_hud", || self.tick_hud()); @@ -3526,7 +4027,6 @@ impl Game<'_> { horizon_client::spans::time("tick_block_entities", || self.tick_block_entities())?; horizon_client::spans::time("tick_sculk_entities", || self.tick_sculk_entities()); horizon_client::spans::time("tick_sculk_blocks", || self.tick_sculk_blocks())?; - horizon_client::spans::time("tick_brewing_stands", || self.tick_brewing_stands())?; } if running { // ClientLevel.tickBlockEntities (not while paused or frozen): @@ -3536,7 +4036,11 @@ impl Game<'_> { }); horizon_client::spans::time("tick_particles", || self.tick_particles()); } - if self.right && self.use_delay == 0 && self.screen == Screen::Playing { + if self.right + && self.use_delay == 0 + && self.screen == Screen::Playing + && self.sleeping.is_none() + { self.use_with_hand()?; self.use_delay = 4; } @@ -3663,6 +4167,11 @@ impl Game<'_> { ) || self.book.is_some()) } fn capture(&mut self, capture: bool) { + // A guest's action (`as_guest`) opens and closes screens on the + // guest's own client: the window's pointer stays the host's (#687). + if self.lan_acting.is_some() { + return; + } // MouseHandler.grabMouse never captures an inactive window. if capture && !self.focused { return; @@ -3688,10 +4197,7 @@ impl Game<'_> { /// LevelLoadingScreen.onClose -> Gui.setScreen(null) -> grabMouse. fn capture_loaded_world(&mut self) { // Loading may finish after focus was lost or another screen opened. - if self.title.is_none() - && self.screen == Screen::Playing - && !self.remote.as_ref().is_some_and(|r| r.chat_input.is_some()) - { + if self.title.is_none() && self.screen == Screen::Playing { self.capture(true); } } @@ -3717,7 +4223,7 @@ impl Game<'_> { let (stack, capped) = match drag.and_then(|d| d.slots.get(&slot)) { Some(None) => return, Some(Some((count, capped))) => { - batch.fill(&self.atlas, x, y, 16.0, 16.0, rgba(255, 255, 255, 128)); + batch.fill(&self.gui.atlas, x, y, 16.0, 16.0, rgba(255, 255, 255, 128)); let carried = self .carried .clone() @@ -3796,7 +4302,7 @@ impl Game<'_> { // moved to (x, y). Horizon tracks no item cooldowns to shade yet. let max_damage = self .res - .items + .items() .get(&stack.item) .map_or(0, |info| info.max_damage as i32); let decorations = hud_overlays::ItemDecorations { @@ -3812,10 +4318,10 @@ impl Game<'_> { 0, 0, &decorations, - &self.atlas.font, + &self.gui.atlas.font, )); draws.push(hud_layout::GuiDraw::Pop); - hud_layout::paint(batch, &self.atlas, &draws); + hud_layout::paint(batch, &self.gui.atlas, &draws); } fn hud(&mut self, size: [u32; 2]) -> HudBatch { let mut batch = self.hud_layers(size); @@ -3823,7 +4329,7 @@ impl Game<'_> { // and the toasts, so it is over everything, menus included. self.draw_debug_overlay(&mut batch, size); self.frame_graph - .draw(self.settings.horizon.overlay, &mut batch, &self.atlas); + .draw(self.settings.horizon.overlay, &mut batch, &self.gui.atlas); self.draw_pad_cursor(&mut batch); batch } @@ -3844,12 +4350,12 @@ impl Game<'_> { if horizon_client::ui_theme::horizon() { let fade = self.backdrop_fade(); let mut ui = - horizon_client::ui_theme::Ui::new(&self.atlas, &mut batch, self.cursor); + horizon_client::ui_theme::Ui::new(&self.gui.atlas, &mut batch, self.cursor); title.draw_horizon(&mut ui, fade); ui.finish(); } else { title.draw( - &self.atlas, + &self.gui.atlas, &mut batch, self.cursor, self.settings.file.bool("hideSplashTexts"), @@ -3857,7 +4363,7 @@ impl Game<'_> { } return batch; } - let atlas_ptr: *const HudAtlas = &self.atlas; + let atlas_ptr: *const HudAtlas = &self.gui.atlas; let _ = atlas_ptr; if self.remote_overlay(&mut batch) { return batch; @@ -3888,7 +4394,7 @@ impl Game<'_> { hud_layout::full_attack_indicator(a, delay, self.crosshair_living()) }); let draws = hud_layout::crosshair(wi, hi, attack, full); - hud_layout::paint(&mut batch, &self.atlas, &draws); + hud_layout::paint(&mut batch, &self.gui.atlas, &draws); } if !spectator { let inputs = hud_layout::HotbarInputs { @@ -3900,7 +4406,7 @@ impl Game<'_> { attack: (indicator == 2).then_some(strength), }; let draws = hud_layout::hotbar(wi, hi, &inputs); - hud_layout::paint(&mut batch, &self.atlas, &draws); + hud_layout::paint(&mut batch, &self.gui.atlas, &draws); for d in &draws { if let hud_layout::GuiDraw::Item { slot, x, y, .. } = *d { let stack = if slot == 40 { @@ -3919,15 +4425,7 @@ impl Game<'_> { self.draw_boss_bars(&mut batch); self.draw_extras(&mut batch); } - // Hud.extractSleepOverlay, under the sidebar, titles and chat. - if let Some((_, ticks)) = self.sleeping { - let draws = hud_overlays::sleep_overlay(wi, hi, ticks as i32); - hud_layout::paint(&mut batch, &self.atlas, &draws); - } if show_hud { - self.draw_titles(&mut batch); - self.draw_scoreboard(&mut batch); - self.draw_subtitles(&mut batch); // The mount's hearts (and the jump bar that replaces the experience bar). let (vehicle_hearts, jump_bar) = if spectator { (0, false) @@ -3938,7 +4436,7 @@ impl Game<'_> { && !spectator && let Some(p) = &mut self.player { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let width = |s: &str| HudBatch::text_width(atlas, s) as i32; let draws = hud_layout::experience( wi, @@ -3962,7 +4460,7 @@ impl Game<'_> { let eye = p.position.y + f64::from(p.dimensions.eye_height); let under_water = self .res - .facts + .facts() .get(self.world.block( p.position.x.floor() as i32, eye.floor() as i32, @@ -3999,7 +4497,15 @@ impl Game<'_> { self.draw_selected_name(&mut batch, !self.creative); } } - let atlas = &self.atlas; + // Hud.extractSleepOverlay: over all health/food bars, under titles and chat. + let draws = hud_overlays::sleep_overlay(wi, hi, self.sleep_timer as i32); + hud_layout::paint(&mut batch, &self.gui.atlas, &draws); + if show_hud { + self.draw_titles(&mut batch); + self.draw_scoreboard(&mut batch); + self.draw_subtitles(&mut batch); + } + let atlas = &self.gui.atlas; if show_hud && let Some((text, at)) = &self.message && at.elapsed() < Duration::from_secs(3) @@ -4016,9 +4522,9 @@ impl Game<'_> { // PauseScreen(false): only its title, at the top. Screen::Paused => { let title = self.translate("menu.paused", "Game Paused"); - let tw = HudBatch::text_width(&self.atlas, &title); + let tw = HudBatch::text_width(&self.gui.atlas, &title); batch.text( - &self.atlas, + &self.gui.atlas, &title, ((batch.width / 2.0).floor() - (tw / 2.0).floor()).floor(), 10.0, @@ -4217,7 +4723,7 @@ impl Game<'_> { /// `RecipeCraftingHolder.awardUsedRecipes`: a crafted recipe becomes known /// (special ones never do). fn award_crafted(&mut self, recipes: &[String]) { - let index = &self.res.stations.recipe_index; + let index = &self.res.stations().recipe_index; let (_, unlocked) = self.level.recipe_book.add_recipes(index, recipes); self.recipe_unlocked(&unlocked); } @@ -4226,7 +4732,7 @@ impl Game<'_> { let grid: Vec> = (0..n * n).map(|i| self.craft_grid[i].clone()).collect(); let scale = |id: i32| self.maps.data.get(&id).map(|m| m.scale); self.res - .recipes + .recipes() .find_with(&grid, n, &scale) .map(|(name, result)| (name.to_owned(), result)) } @@ -4236,7 +4742,7 @@ impl Game<'_> { let (sx, sy) = self.slot_position(i); if i == 504 && self.offhand.is_none() { batch.sprite( - &self.atlas, + &self.gui.atlas, "gui/sprites/container/slot/shield", ox + sx, oy + sy, @@ -4245,7 +4751,7 @@ impl Game<'_> { if (500..504).contains(&i) && self.armor[i - 500].is_none() { let icon = ["helmet", "chestplate", "leggings", "boots"][i - 500]; batch.sprite( - &self.atlas, + &self.gui.atlas, &format!("gui/sprites/container/slot/{icon}"), ox + sx, oy + sy, @@ -4261,7 +4767,7 @@ impl Game<'_> { } let (ox, oy) = self.inventory_origin(batch); { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; batch.fill( atlas, 0.0, @@ -4385,7 +4891,7 @@ impl Game<'_> { if let Some(slot) = self.slot_at(batch, self.cursor.0, self.cursor.1) { let (sx, sy) = self.slot_position(slot); batch.fill( - &self.atlas, + &self.gui.atlas, ox + sx, oy + sy, 16.0, @@ -4426,7 +4932,7 @@ impl Game<'_> { ); let name = self .res - .facts + .facts() .get(self.world.block(key.0, key.1, key.2)) .name .clone(); @@ -4499,7 +5005,7 @@ impl Game<'_> { let items = self.creative_screen.items(&self.pack.names); let first = self.creative_screen.first_row(&self.pack.names); let shown = items.get(first * 9 + slot - Creative::DISPLAY); - let catalog = &self.res.items; + let catalog = self.res.items(); let max = |s: &ItemStack| catalog.get(&s.item).map_or(64, |i| i.max_stack); match click_display(&mut self.carried, shown, max, button, input) { DisplayClick::Hotbar(i, stack) => self.inventory[i] = Some(stack), @@ -4510,6 +5016,10 @@ impl Game<'_> { /// Several clicks on one menu instance (a quick craft drag is three). fn menu_ops(&mut self, ops: &[(Option, i32, horizon_client::menu::Input)]) -> bool { use horizon_client::menu::Input; + if self.remote.is_some() && self.screen == Screen::Inventory { + // On a server the clicks are the server's: predicted and sent. + return self.remote_menu_ops(ops); + } if let [(Some(slot), button, input)] = ops { self.crafter_slot_clicked(*slot, *button, *input); } @@ -4655,7 +5165,7 @@ impl Game<'_> { Some(BlockEntity::Chest(_)) => { let shulker = self.container.is_some_and(|k| { let id = self.world.block(k.0, k.1, k.2); - storage::is_shulker_box(&self.res.facts.get(id).name) + storage::is_shulker_box(&self.res.facts().get(id).name) }); let kind = match size { 5 => Kind::Hopper, @@ -4665,6 +5175,9 @@ impl Game<'_> { }; (kind, size) } + // A server's BrewingStandMenu; a local stand's clicks are the + // station screen's. + Some(BlockEntity::Brewing(_)) if self.remote.is_some() => (Kind::Brewing, 5), Some(BlockEntity::Furnace(f)) => ( Kind::Furnace( match f.kind.recipe_type().trim_start_matches("minecraft:") { @@ -4684,7 +5197,7 @@ impl Game<'_> { plain: false, }; (kind, map) - } else if self.station.is_none() && !self.mount_active() && self.remote.is_none() { + } else if self.station.is_none() && !self.mount_active() { let table = self.craft_size == 3; let map = Map { container: 0, @@ -4748,15 +5261,15 @@ impl Game<'_> { next_id: std::cell::Cell::new(next_map), }; let trade_env = horizon_client::merchant::TradeEnv { - trades: &self.res.stations.trades, - enchantments: &self.res.stations.enchantments, + trades: &self.res.stations().trades, + enchantments: &self.res.stations().enchantments, maps: Some(&maps), }; let env = Env { - items: &self.res.items, - recipes: &self.res.recipes, - brewing: &self.res.stations.brewing, - banners: &self.res.stations.banners, + items: self.res.items(), + recipes: self.res.recipes(), + brewing: &self.res.stations().brewing, + banners: &self.res.stations().banners, trade: Some(&trade_env), }; menu.creative = creative; @@ -4797,7 +5310,12 @@ impl Game<'_> { Some(false) => return true, Some(true) => {} } - self.register_exploration_maps(made); + // On a server the clicks only predict: the server drops, crafts and + // makes maps itself. + let predicting = self.remote.is_some(); + if !predicting { + self.register_exploration_maps(made); + } if let Some(s) = menu.merchant.take() { self.merchant_sync_out(s); } @@ -4815,10 +5333,12 @@ impl Game<'_> { if !offhand_mapped { self.offhand = menu.player_item(40).cloned(); } - for stack in dropped { - self.player_drop(stack); + if !predicting { + for stack in dropped { + self.player_drop(stack); + } + self.award_crafted(&crafted); } - self.award_crafted(&crafted); if kind == Kind::Loom { if let Some(s) = self.station.as_mut() { s.loom.selected = usize::try_from(menu.data[0]).ok(); @@ -4844,7 +5364,7 @@ impl Game<'_> { .and_then(|k| self.world.block_entities.get_mut(&k)) { // AbstractFurnaceBlockEntity.setItem: only a new input restarts cooking. - f.input_clicked(&self.res.recipes, before[0].clone()); + f.input_clicked(self.res.recipes(), before[0].clone()); } self.apply_equipment(); true @@ -4950,9 +5470,13 @@ impl Game<'_> { return; } if let Some(moved) = self.last_quick_moved.clone() { - self.menu_session(|menu, env, to_menu| { - Some(menu.quick_move_all(env, to_menu(slot)?, &moved).is_ok()) - }); + if self.remote.is_some() { + self.remote_quick_move_all(slot, &moved); + } else { + self.menu_session(|menu, env, to_menu| { + Some(menu.quick_move_all(env, to_menu(slot)?, &moved).is_ok()) + }); + } } self.last_inv_click = None; self.inv_clicks.skip_release(); @@ -5243,7 +5767,7 @@ impl Game<'_> { None => self.carried = Some(result), Some(c) if c.item == result.item - && c.count + result.count <= max(&self.res.items, c) => + && c.count + result.count <= max(self.res.items(), c) => { c.count += result.count } @@ -5259,7 +5783,7 @@ impl Game<'_> { while let Some((recipe, result)) = self.craft_recipe() { let made = (result.item.clone(), result.count); let cells = self.craft_size * self.craft_size; - let remainders = self.res.recipes.remainders( + let remainders = self.res.recipes().remainders( &recipe, &self.craft_grid[..cells], self.craft_size, @@ -5273,7 +5797,7 @@ impl Game<'_> { None => self.carried = Some(result), Some(c) if c.item == result.item - && c.count + result.count <= max(&self.res.items, c) => + && c.count + result.count <= max(self.res.items(), c) => { c.count += result.count } @@ -5315,7 +5839,7 @@ impl Game<'_> { let wanted = ["head", "chest", "legs", "feet"][slot - 500]; let fits = self.carried.as_ref().is_none_or(|c| { self.res - .items + .items() .get(&c.item) .and_then(|i| i.equip_slot.as_deref()) == Some(wanted) @@ -5329,7 +5853,7 @@ impl Game<'_> { } let carried = self.carried.take(); let current = self.slot_mut(slot).take(); - let items = &self.res.items; + let items = self.res.items(); let (new_slot, new_carried) = match (carried, current) { (None, Some(mut s)) if right => { let half = s.count.div_ceil(2); @@ -5370,7 +5894,7 @@ impl Game<'_> { .container .and_then(|k| self.world.block_entities.get_mut(&k)) { - f.input_clicked(&self.res.recipes, before); + f.input_clicked(self.res.recipes(), before); } } /// `Camera.setRotation`: the camera's orientation, turned round in @@ -5411,7 +5935,7 @@ impl Game<'_> { m.height(), camera, fire_yaw, - &self.atlas.sprites, + &self.gui.atlas.sprites, self.world.light( at.x.floor() as i32, at.y.floor() as i32, @@ -5433,7 +5957,7 @@ impl Game<'_> { p.dimensions.height, camera, fire_yaw, - &self.atlas.sprites, + &self.gui.atlas.sprites, self.world.light( feet.x.floor() as i32, feet.y.floor() as i32, @@ -5446,7 +5970,7 @@ impl Game<'_> { if item.fire_ticks > 0 && !self .res - .items + .items() .resists_fire(&item.stack, "minecraft:in_fire") { let at = item.previous.lerp(item.position, alpha); @@ -5456,7 +5980,7 @@ impl Game<'_> { 0.25, camera, fire_yaw, - &self.atlas.sprites, + &self.gui.atlas.sprites, self.world.light( at.x.floor() as i32, at.y.floor() as i32, @@ -5475,7 +5999,7 @@ impl Game<'_> { 1.975 * scale, camera, fire_yaw, - &self.atlas.sprites, + &self.gui.atlas.sprites, self.world.light( stand.position.x.floor() as i32, stand.position.y.floor() as i32, @@ -5500,7 +6024,7 @@ impl Game<'_> { self.name_tag_geometry(camera, alpha, &mut out, &mut see_through); self.see_through_vertices = see_through; // Entity shadows (EntityRenderer.extractShadow). - if let Some(sprite) = self.atlas.sprites.get("misc/shadow").copied() { + if let Some(sprite) = self.gui.atlas.sprites.get("misc/shadow").copied() { let ambient = if self.dim == Dim::Nether { 0.1 } else { 0.0 }; let darken = self.sky_darken(); let a = alpha; @@ -5530,7 +6054,7 @@ impl Game<'_> { let (radius, strength) = horizon_client::shadows::shadow_params(id); horizon_client::shadows::shadow( &self.world, - &self.res.facts, + self.res.facts(), sprite, camera, pos, @@ -5555,7 +6079,7 @@ impl Game<'_> { game_time: self.world.game_time, max_y: self.dim.max_y(), world: &self.world, - sprites: &self.atlas.sprites, + sprites: &self.gui.atlas.sprites, nether: self.dim == Dim::Nether, lid: &lid, sign: &|p| match self.world.block_entities.get(&(p[0], p[1], p[2])) { @@ -5563,7 +6087,7 @@ impl Game<'_> { _ => None, }, banner: &|p| self.banner_layers_at(p), - glyph: &|c| self.atlas.glyph_cell(c), + glyph: &|c| self.gui.atlas.glyph_cell(c), }; // Block entities outside the view frustum are skipped (with a // block of margin for models larger than their block). @@ -5571,7 +6095,7 @@ impl Game<'_> { // Beams and structure outlines reach far outside their block // (BlockEntityRenderer.shouldRenderOffScreen): never culled. let off_screen = |state: u32| { - let name = &*self.res.facts.get(BlockStateId(state)).name; + let name = &*self.res.facts().get(BlockStateId(state)).name; matches!( name, "minecraft:beacon" @@ -5581,6 +6105,9 @@ impl Game<'_> { ) }; let visible = r.block_entities().filter(|(p, state)| { + if !self.res.block_entity_render().renders(*state) { + return false; + } off_screen(*state) || view.is_none_or(|v| { let o = @@ -5594,7 +6121,7 @@ impl Game<'_> { for block in visible { let start = out.len(); self.res - .block_entity_render + .block_entity_render() .render(&scene, std::iter::once(block), &mut out); if !off_screen(block.1) { self.cull_entity_vertices(&mut out, start); @@ -5602,12 +6129,12 @@ impl Game<'_> { } } else { self.res - .block_entity_render + .block_entity_render() .render(&scene, visible, &mut out); } if let Some(mining) = &self.mining && mining.progress > 0.0 - && self.atlas.sprites.contains_key(&format!( + && self.gui.atlas.sprites.contains_key(&format!( "block/destroy_stage_{}", ((mining.progress * 10.0) as u32).min(9) )) @@ -5616,12 +6143,13 @@ impl Game<'_> { .world .block(mining.pos[0], mining.pos[1], mining.pos[2]); self.res - .block_entity_render + .block_entity_render() .render_breaking(&scene, mining.pos, state.0, &mut out); } } // Experience orbs (ExperienceOrbRenderer): pulsing billboards from a 4x4 icon sheet. if let Some(r) = self + .gui .atlas .sprites .get("entity/experience/experience_orb") @@ -5702,9 +6230,10 @@ impl Game<'_> { let start = out.len(); self.block_model_vertices( f.state, - f.start_pos, + horizon_core::random::position_seed(f.start_pos), glam::Mat4::from_translation(o), packed, + false, &mut out, ); let tint = horizon_client::mobs::render::abgr(tint); @@ -5733,7 +6262,7 @@ impl Game<'_> { &|b| world.light(b[0], b[1], b[2]), &mut out, ); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; // Breaking progress overlay (destroy_stage_0..9). if let Some(m) = &self.mining && m.progress > 0.0 @@ -5750,11 +6279,11 @@ impl Game<'_> { ) - camera) .as_vec3(); let state = self.world.block(m.pos[0], m.pos[1], m.pos[2]); - if self.res.facts.get(state).render_shape + if self.res.facts().get(state).render_shape == horizon_client::blocks::RenderShape::Model && let Ok(models) = self .pack - .assets + .render .model_catalog .collect(state, BlockPos::new(m.pos[0], m.pos[1], m.pos[2])) { @@ -5784,7 +6313,7 @@ impl Game<'_> { if self.dim == Dim::End { let sky = self .res - .end_environment + .end_environment() .sample(self.world.day_time, None, None); return (15.0 - 15.0 * sky.sky_light_level).round() as i32; } @@ -5796,7 +6325,10 @@ impl Game<'_> { { return darken; } - let mut sky = self.res.environment.sample(self.world.day_time, None, None); + let mut sky = self + .res + .environment() + .sample(self.world.day_time, None, None); horizon_client::environment::apply_weather(&mut sky, rain, thunder); // Level.updateSkyBrightness: darkening from the sky light level. let darken = (15.0 - 15.0 * sky.sky_light_level).round() as i32; @@ -5828,7 +6360,7 @@ impl Game<'_> { let hand_damage = 1.0 + self .held() - .and_then(|h| self.res.items.get(&h.item)) + .and_then(|h| self.res.items().get(&h.item)) .map_or(0.0, |i| i.attack_damage); let damage = hand_damage + self.effect_attack_bonus(); let scale = self.attack_strength_scale(); @@ -5851,11 +6383,43 @@ impl Game<'_> { fn attack_strength_scale(&self) -> f32 { let speed = self .held() - .and_then(|h| self.res.items.get(&h.item)) + .and_then(|h| self.res.items().get(&h.item)) .map_or(0.0, |i| i.attack_speed); let delay = 20.0 / (4.0 + speed).max(0.1); ((self.attack_ticks as f32 + 0.5) / delay).clamp(0.0, 1.0) } + /// `Player.isSweepAttack`: a charged, grounded sword hit below the movement limit. + pub(super) fn sweep_attack_position(&self, scale: f32, crit: bool) -> Option<[f64; 3]> { + let p = self.player.as_ref()?; + let sword = self.held().is_some_and(|s| { + self.res + .items() + .item_tags + .get("minecraft:swords") + .is_some_and(|tag| tag.contains(&*s.item)) + }); + if !p.is_sweep_attack(scale, crit, sword) { + return None; + } + let yaw = f64::from(self.yaw.to_radians()); + Some([ + p.position.x - yaw.sin(), + p.position.y + f64::from(p.dimensions.height) * 0.5, + p.position.z + yaw.cos(), + ]) + } + /// The visual and sound part of `Player.doSweepAttack` after a successful hit. + pub(super) fn sweep_attack(&mut self, at: [f64; 3], from: Option<[f64; 3]>) { + self.particles.add( + &horizon_client::particles::ParticleOptions::simple( + horizon_client::particles::ParticleType::SweepAttack, + ), + at, + [0.0; 3], + ); + self.audio + .play("minecraft:entity.player.attack.sweep", from, 1.0, 1.0); + } /// `Player.attack`'s sounds when a vehicle takes the hit (it always does, /// and can't be hit critically): knockback when sprinting at full /// strength, then strong or weak. Before `swing_arm` resets the charge. @@ -5872,7 +6436,11 @@ impl Game<'_> { } else { "minecraft:entity.player.attack.weak" }; - self.audio.play(sound, from, 1.0, 1.0); + if let Some(at) = self.sweep_attack_position(self.attack_strength_scale(), false) { + self.sweep_attack(at, from); + } else { + self.audio.play(sound, from, 1.0, 1.0); + } } /// Player.attack with the attack-strength charge, crits and knockback. fn attack(&mut self) -> bool { @@ -5897,7 +6465,7 @@ impl Game<'_> { let hand_damage = 1.0 + self .held() - .and_then(|h| self.res.items.get(&h.item)) + .and_then(|h| self.res.items().get(&h.item)) .map_or(0.0, |i| i.attack_damage); if self.attack_frame() || self.attack_armor_stand() @@ -5924,6 +6492,7 @@ impl Game<'_> { let target_kind = self.mobs[index].kind.id.clone(); let target_kind = format!("minecraft:{}", target_kind.trim_start_matches("minecraft:")); let (amount, scale, crit) = self.melee_damage(&target_kind); + let sweep = self.sweep_attack_position(scale, crit); self.attack_ticks = 0; if let Some(p) = self.player.as_mut() && !self.creative @@ -5983,7 +6552,11 @@ impl Game<'_> { "minecraft:entity.player.attack.weak" }; // Player.playServerSideSound: at the player. - self.audio.play(sound, from, 1.0, 1.0); + if let Some(at) = sweep { + self.sweep_attack(at, from); + } else { + self.audio.play(sound, from, 1.0, 1.0); + } // The hurt or death sound the mob raised (getHurtSound / getDeathSound, // its voice pitch from its own random). let queued = std::mem::take(&mut self.mobs[index].outbox); @@ -6011,38 +6584,6 @@ impl Game<'_> { false, ) } - fn tick_block_entities(&mut self) -> Result<()> { - self.tick_hoppers()?; - self.tick_crafters(); - self.tick_mount_menu(); - self.tick_jukeboxes(); - self.tick_beacons(); - self.tick_creaking_hearts()?; - self.tick_spawners()?; - self.tick_trial_spawners()?; - self.tick_vaults()?; - self.tick_beehives(); - let mut lit_changes = Vec::new(); - for (pos, entity) in self.world.block_entities.iter_mut() { - if let BlockEntity::Furnace(f) = entity - && let Some(lit) = f.tick(&self.res.recipes, &self.res.items, &self.res.providers) - { - lit_changes.push((*pos, lit)); - } - } - for ((x, y, z), lit) in lit_changes { - let state = self.world.block(x, y, z); - if let Some(mut s) = self.pack.assets.registry.state(state).cloned() - && s.properties.contains_key("lit") - { - s.properties.insert("lit".into(), lit.to_string()); - if let Ok(id) = self.pack.assets.registry.resolve(&s) { - self.world.set(x, y, z, id)?; - } - } - } - Ok(()) - } /// Copies of a save batch carrying its live block entities and save data. fn with_entities<'a>( &self, @@ -6077,7 +6618,7 @@ impl Game<'_> { /// SkyRenderer.renderSunMoonAndStars: bodies on a frame rotated Y -90 then X by angle. fn celestial_geometry(&self, sky: &horizon_client::environment::Sky) -> Vec { let mut out = Vec::new(); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let frame = |angle: f32| { glam::Mat4::from_rotation_y((-90.0f32).to_radians()) * glam::Mat4::from_rotation_x(angle.to_radians()) @@ -6174,6 +6715,16 @@ impl Game<'_> { creative: bool, create: Option<&horizon_client::title::WorldOptions>, ) -> Result<()> { + if self.defer_world_entry(PendingWorldEntry::Local { + folder: folder.to_owned(), + name: name.map(str::to_owned), + seed, + creative, + create: create.cloned().map(Box::new), + })? { + return Ok(()); + } + self.join_world_rest()?; let save_root = std::env::current_dir()?.join("saves").join(folder); if integrated::wanted(&self.options, &save_root) { return self.open_integrated(folder, name, seed, creative, create); @@ -6187,7 +6738,8 @@ impl Game<'_> { self.fns = Default::default(); self.bosses = Default::default(); reset_world_progress(&mut self.adv, &mut self.stats); - self.restore_player_environment = true; + self.prefetch_advancement_tree(); + self.restore_player_state = true; // RandomSequences: the world's data/minecraft/random_sequences.dat. self.rand_sequences = horizon_client::save::read_dat(&save_root.join("data/minecraft/random_sequences.dat")) @@ -6234,7 +6786,7 @@ impl Game<'_> { let dim = Dim::from_id(&level.dimension); let (mut streaming, save) = self.make_streaming(&save_root, dim, level.seed)?; streaming.radius = self.settings.render_distance; - let mut world = World::new(self.res.facts.air, level.seed); + let mut world = World::new(self.res.facts().air, level.seed); world.fast_lava = dim == Dim::Nether; world.game_time = level.time; world.day_time = self.options.time.unwrap_or(level.day_time); @@ -6312,7 +6864,7 @@ impl Game<'_> { self.save = save; self.level = level; // ServerRecipeBook.loadUntrusted: recipes the manager does not have are dropped. - let index = &self.res.stations.recipe_index; + let index = &self.res.stations().recipe_index; let book = &mut self.level.recipe_book; book.recipes.retain(|r| index.contains(r)); book.to_be_displayed.retain(|r| index.contains(r)); @@ -6324,6 +6876,8 @@ impl Game<'_> { self.clear_entities(); self.travelling = None; self.player = None; + self.sleeping = None; + self.sleep_timer = 0; self.spawn = None; self.respawn_travel = None; self.respawn_config = @@ -6358,6 +6912,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(); } @@ -6390,7 +6946,7 @@ impl Game<'_> { horizon_client::flatgen::FlatTerrain::from_settings( &s, pack.assets.registry, - &res.biomes, + res.biomes(), ) }); let mut streaming = if let Some(inner) = flat { @@ -6399,25 +6955,28 @@ impl Game<'_> { inner: Arc::new(inner), save: save.clone(), registry: pack.assets.clone(), - biomes: res.biomes.clone(), + biomes: res.biomes().clone(), }), pack.context.clone(), - res.threads, + res.threads(), ) } else if self.options.test_terrain && dim == Dim::Overworld { - let inner = Arc::new(TestTerrain::new(pack.assets.registry, &res.biomes)?); + let inner = Arc::new(TestTerrain::new(pack.assets.registry, res.biomes())?); Streaming::new( Arc::new(Persistent { inner, save: save.clone(), registry: pack.assets.clone(), - biomes: res.biomes.clone(), + biomes: res.biomes().clone(), }), pack.context.clone(), - res.threads, + res.threads(), ) } else { - let inner = Arc::new(VanillaTerrain::for_dimension(&self.options.resources.join("worldgen"), pack.assets.registry, &res.biomes, seed, dim.id()).map_err(|e| { + // WorldOptions.generateStructures, from the world's settings. + let structures = + horizon_client::save::WorldGen::read(world_root).is_none_or(|w| w.structures); + let inner = Arc::new(VanillaTerrain::for_world(&self.options.resources.join("worldgen"), pack.assets.registry, res.biomes(), seed, dim.id(), structures).map_err(|e| { format!("{e}\nPrepare the worldgen bundle with the launcher, or: horizon-prepare --prepare") })?); self.end.set_locator(Some(inner.clone())); @@ -6426,10 +6985,10 @@ impl Game<'_> { inner, save: save.clone(), registry: pack.assets.clone(), - biomes: res.biomes.clone(), + biomes: res.biomes().clone(), }), pack.context.clone(), - res.threads, + res.threads(), ) }; streaming.radius = self.settings.render_distance; @@ -6440,8 +6999,9 @@ impl Game<'_> { streaming.biome_zoom = BiomeZoom::new(obfuscate_seed(seed)); streaming.sky_light = dim != Dim::Nether; streaming.nether_shading = dim == Dim::Nether; - // The integrated server draws nothing (the dedicated server keeps its behaviour). - streaming.mesh = !self.options.integrated; + // A server draws nothing: neither the integrated server nor the + // dedicated one meshes sections. + streaming.mesh = !self.options.integrated && self.options.dedicated.is_none(); Ok((streaming, save)) } /// Change dimension through a portal (Entity.changeDimension with @@ -6450,12 +7010,23 @@ impl Game<'_> { let Some(player) = self.player.take() else { return Ok(()); }; + let from = self.dim; + let destination = self.switch_level(target, player.position)?; + self.trigger_dimension(from, target); + self.travelling = Some((player, destination)); + self.end_travel = (target == Dim::End || from == Dim::End) && self.end_portal_travel; + Ok(()) + } + /// The level swap under a dimension change: save the departing level, + /// open `target`'s, and return where a player leaving from `from` goes + /// (scaled coordinates, or the End portal's fixed destination). + pub(super) fn switch_level(&mut self, target: Dim, from: Vec3) -> Result { // Save the departing dimension's changed chunks. self.streaming.save(&mut self.world, true); let keys: Vec<_> = self.world.modified.drain().collect(); let chunks = self.with_entities(keys.iter().filter_map(|k| self.world.chunks.get(k))); self.save - .save(&chunks, self.pack.assets.registry, &self.res.biomes)?; + .save(&chunks, self.pack.assets.registry, self.res.biomes())?; self.save_dimension_entities()?; // Coordinates scale by 8 between the Overworld and the Nether. let scale = match (self.dim, target) { @@ -6465,24 +7036,26 @@ impl Game<'_> { _ => 1.0, }; let destination = self.end_destination(target).unwrap_or(Vec3::new( - player.position.x * scale, - player.position.y, - player.position.z * scale, + from.x * scale, + from.y, + from.z * scale, )); - let from_end = self.dim == Dim::End; let (streaming, save) = self.make_streaming(&self.world_root.clone(), target, self.level.seed)?; - let mut world = World::new(self.res.facts.air, self.level.seed); + let mut world = World::new(self.res.facts().air, self.level.seed); world.fast_lava = target == Dim::Nether; world.game_time = self.world.game_time; world.day_time = self.world.day_time; world.weather = self.world.weather; + // An open hub keeps hearing the new level's block changes. + world.block_log = self.world.block_log.as_ref().map(|_| Vec::new()); + world.light_log = self.world.light_log.as_ref().map(|_| Default::default()); + // The sculk tables' watched blocks are built once per game. + world.watched = std::mem::take(&mut self.world.watched); self.streaming = streaming; self.world = world; self.save = save; - let from = self.dim; self.dim = target; - self.trigger_dimension(from, target); self.level.dimension = target.id().to_owned(); self.block_ticks = Ticks::new(self.level.seed as i32); self.tick_chunks.clear(); @@ -6495,19 +7068,19 @@ impl Game<'_> { self.pickups.clear(); self.orbs.clear(); self.mining = None; - self.travelling = Some((player, destination)); - self.end_travel = (target == Dim::End || from_end) && self.end_portal_travel; + self.clear_lod(); + self.pending_uploads.clear(); if let Some(r) = self.renderer.as_mut() { r.clear_sections(); } self.apply_settings(); - Ok(()) + Ok(destination) } /// PortalShape: an obsidian frame around 2..=21 by 3..=21 empty blocks. fn portal_shape(&self, start: [i32; 3], x_axis: bool) -> Option<([i32; 3], i32, i32)> { let name = |p: [i32; 3]| { self.res - .facts + .facts() .get(self.world.block(p[0], p[1], p[2])) .name .clone() @@ -6669,10 +7242,14 @@ impl Game<'_> { horizon_client::crash::clear_detail("World"); self.debug_charts.reset(); self.streaming.paused = true; - self.world = World::new(self.res.facts.air, 0); + self.world = World::new(self.res.facts().air, 0); self.player = None; + self.sleeping = None; + 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(); } @@ -6730,7 +7307,7 @@ impl Game<'_> { .collect(); equipped.push((self.inventory[self.selected].as_ref(), "mainhand")); for (stack, slot) in equipped { - let Some(info) = stack.and_then(|s| self.res.items.get(&s.item)) else { + let Some(info) = stack.and_then(|s| self.res.items().get(&s.item)) else { continue; }; for (attribute, id, amount, op, group) in &info.modifiers { @@ -6765,7 +7342,7 @@ impl Game<'_> { .collect(); for (slot, amount) in self.armor.iter_mut().zip(losses) { if let Some(stack) = slot.as_mut() - && let Some(info) = self.res.items.get(&stack.item) + && let Some(info) = self.res.items().get(&stack.item) && info.max_damage > 0 { stack.damage += amount; @@ -6802,7 +7379,7 @@ impl Game<'_> { let b = p.map(|v| v.floor() as i32); if b != last { let state = self.world.block(b[0], b[1], b[2]); - let f = self.res.facts.get(state); + let f = self.res.facts().get(state); let face = [last[0] - b[0], last[1] - b[1], last[2] - b[2]]; if f.fluid != Fluid::None && f.fluid_level == 8 @@ -6816,7 +7393,7 @@ impl Game<'_> { point: p, }); } - if !self.res.facts.is_air(state) && f.fluid == Fluid::None { + if !self.res.facts().is_air(state) && f.fluid == Fluid::None { return Some(Hit { pos: b, face, @@ -6873,7 +7450,7 @@ impl Game<'_> { let Some(held) = self.held() else { return Ok(false); }; - let removes_dye = cauldron::removes_dye(&self.res.items, &held.item); + let removes_dye = cauldron::removes_dye(self.res.items(), &held.item); let above = self.world.block(pos[0], pos[1] + 1, pos[2]); let under_water = registry .state(above) @@ -6929,7 +7506,7 @@ impl Game<'_> { } let Some(id) = self .res - .items + .items() .get(&stack.item) .and_then(|i| i.block_transformer.clone()) else { @@ -6940,7 +7517,7 @@ impl Game<'_> { let shield = self .offhand .as_ref() - .and_then(|s| self.res.items.get(&s.item)) + .and_then(|s| self.res.items().get(&s.item)) .is_some_and(|i| i.blocks_attacks.is_some()); if shield && !self.keys.contains(&KeyCode::ShiftLeft) { return Ok(false); @@ -6962,8 +7539,8 @@ impl Game<'_> { }; let Some((_, t, state)) = self.res - .transformers - .apply(&self.res.items, registry, &id, face_name, &around) + .transformers() + .apply(self.res.items(), registry, &id, face_name, &around) else { return Ok(false); }; @@ -7034,7 +7611,7 @@ impl Game<'_> { }; let max = self .held() - .and_then(|s| self.res.items.get(&s.item)) + .and_then(|s| self.res.items().get(&s.item)) .map_or(0, |i| i.max_damage); let cell = self.held_mut(); if let Some(stack) = cell.as_mut() @@ -7077,6 +7654,13 @@ impl Game<'_> { { return Ok(UseResult::Success { swing: true }); } + // BucketItem has no useOn: a guest's UseItemOn leaves it to UseItem. + let bucket = horizon_client::items::bucket_contents(&stack.item); + if matches!(self.guest_use, Some(GuestUse::OnBlock(_))) + && (item == "bucket" || bucket.is_some()) + { + return Ok(UseResult::Pass); + } if item == "bucket" { // getPlayerPOVHitResult(SOURCE_ONLY): powder snow's outline before // a fluid source behind it. @@ -7099,7 +7683,7 @@ impl Game<'_> { return Ok(UseResult::Pass); }; let state = self.world.block(hit.pos[0], hit.pos[1], hit.pos[2]); - let f = self.res.facts.get(state).clone(); + let f = self.res.facts().get(state).clone(); if f.fluid == Fluid::None || f.fluid_level != 8 || f.fluid_falling { return Ok(UseResult::Pass); } @@ -7135,7 +7719,6 @@ impl Game<'_> { return Ok(UseResult::Success { swing: true }); } // BucketItem.use has its own ray, even when the crosshair hits an entity. - let bucket = horizon_client::items::bucket_contents(&stack.item); if !allow_block_actions && bucket.is_none() { return Ok(UseResult::Pass); } @@ -7147,7 +7730,7 @@ impl Game<'_> { if !self.may_build() { return Ok(UseResult::Fail); } - let target = if self.res.facts.get(clicked).replaceable { + let target = if self.res.facts().get(clicked).replaceable { hit.pos } else { [ @@ -7189,7 +7772,7 @@ impl Game<'_> { if item == "honeycomb" { let registry = self.pack.assets.registry; let old = registry.state(clicked).cloned(); - if let Some(waxed) = old.and_then(|old| self.res.transformers.waxed(registry, &old)) + if let Some(waxed) = old.and_then(|old| self.res.transformers().waxed(registry, &old)) && let Ok(state) = registry.resolve(&waxed) { self.level_set(hit.pos, state)?; @@ -7210,7 +7793,7 @@ impl Game<'_> { if !self.may_build() { return Ok(UseResult::Fail); } - let name = self.res.facts.get(clicked).name.clone(); + let name = self.res.facts().get(clicked).name.clone(); if self.can_light(clicked, &name) { let registry = self.pack.assets.registry; let mut state = registry @@ -7282,7 +7865,9 @@ impl Game<'_> { let mut r = dispenser::entity_random(); let _yaw = r.next_float(); let p = Vec3::new(at[0], at[1], at[2]); + self.next_entity += 1; self.orbs.push(ExperienceOrb { + id: self.next_entity, position: p, previous: p, velocity: Vec3::new( @@ -7346,13 +7931,29 @@ impl Game<'_> { .player .as_ref() .filter(|p| !p.health.dead_or_dying() && !spectator); - let target = player.map(|p| { - [ - p.position.x, - p.position.y + f64::from(p.dimensions.eye_height) / 2.0, - p.position.z, - ] - }); + // ExperienceOrb.followNearbyPlayer: each orb goes for the nearest + // living player within 8 blocks, the host's own or a guest. + let mut followed: Vec<([f64; 3], [f64; 3])> = player + .map(|p| { + let feet = [p.position.x, p.position.y, p.position.z]; + let eye = f64::from(p.dimensions.eye_height); + (feet, [feet[0], feet[1] + eye / 2.0, feet[2]]) + }) + .into_iter() + .collect(); + if let Some(host) = self.lan.as_ref() { + followed.extend( + host.hub + .guests + .iter() + .filter(|g| g.placed && g.loaded && !g.dead && g.health > 0.0) + .filter(|g| !g.spectator()) + .map(|g| { + let p = g.position; + (p, [p[0], p[1] + g.eye_height() / 2.0, p[2]]) + }), + ); + } let bounds = player.map(|p| { let w = f64::from(p.dimensions.width) / 2.0; ( @@ -7365,7 +7966,19 @@ impl Game<'_> { ) }); let mut orbs = std::mem::take(&mut self.orbs); - orbs.retain_mut(|o| o.tick(&self.world, &self.res.facts, self.collision, target)); + orbs.retain_mut(|o| { + let distance = |feet: &[f64; 3]| -> f64 { + (feet[0] - o.position.x).powi(2) + + (feet[1] - o.position.y).powi(2) + + (feet[2] - o.position.z).powi(2) + }; + let target = followed + .iter() + .filter(|(feet, _)| distance(feet) < 64.0) + .min_by(|a, b| distance(&a.0).total_cmp(&distance(&b.0))) + .map(|(_, target)| *target); + o.tick(&self.world, self.res.facts(), self.collision, target) + }); let mut gained = 0; let mut taken = Vec::new(); if let Some((min, max)) = bounds { @@ -7399,19 +8012,25 @@ impl Game<'_> { (self.world.random.next_float() - self.world.random.next_float()) * 0.35 + 0.9; self.audio .play("minecraft:entity.experience_orb.pickup", at, 0.1, pitch); - if self.experience.give(gained as i32) - && self.experience.level % 5 == 0 - && self.ticks > self.last_level_up + 100 - { - self.last_level_up = self.ticks; - let volume = if self.experience.level > 30 { - 1.0 - } else { - self.experience.level as f32 / 30.0 - }; - self.audio - .play("minecraft:entity.player.levelup", at, volume * 0.75, 1.0); - } + self.give_orb_points(gained); + } + } + /// `Player.giveExperiencePoints`, and `giveExperienceLevels`' level-up + /// sound every fifth level (at most every 100 ticks). + pub(super) fn give_orb_points(&mut self, gained: u32) { + let at = self.eye_position(); + if self.experience.give(gained as i32) + && self.experience.level % 5 == 0 + && self.ticks > self.last_level_up + 100 + { + self.last_level_up = self.ticks; + let volume = if self.experience.level > 30 { + 1.0 + } else { + self.experience.level as f32 / 30.0 + }; + self.audio + .play("minecraft:entity.player.levelup", at, volume * 0.75, 1.0); } } /// BowItem.releaseUsing: power from charge time, arrows at power * 3. @@ -7500,6 +8119,7 @@ impl Game<'_> { // GameRenderer.renderItemInHand: F1 hides the hand with the HUD. if self.spectator() || self.ui.hide_gui + || self.sleeping.is_some() || !self.camera_type.first_person() || self .player @@ -7558,10 +8178,16 @@ impl Game<'_> { if hand == Hand::Off { continue; } - let Some(arm) = self.res.player_arm.clone() else { + let Some(arm) = self.res.player_arm().clone() else { continue; }; - let Some(r) = self.atlas.sprites.get("entity/player/wide/steve").copied() else { + let Some(r) = self + .gui + .atlas + .sprites + .get("entity/player/wide/steve") + .copied() + else { continue; }; // renderPlayerArm (right arm) @@ -7685,38 +8311,25 @@ impl Game<'_> { } out } - /// The renderer for the window, with the current block sprites, shared + /// The renderer for the window, with the current block sprites, GUI /// atlas and world border texture. fn new_renderer(&self, window: Arc) -> Result { - self.renderer_for(window, &self.pack, &self.atlas) + self.renderer_for(window, &self.pack, &self.gui) } fn renderer_for( &self, window: Arc, pack: &PackResources, - atlas: &HudAtlas, + gui: &UiResources, ) -> Result { - let assets = &pack.assets; - let mut renderer = pollster::block_on(Renderer::new( + build_renderer( window, self.options.offscreen, - &assets.pixels, - assets.sprite_size, - assets.sprite_minimum_size, - assets.texture_layers, - &assets.sprite_mips, - &assets.animations, - (&atlas.pixels, atlas.width, atlas.height), - (&pack.forcefield.2, pack.forcefield.0), - &pack.panorama, + pack, + gui, self.renderer.as_ref(), - ))?; - renderer.hud.fire_animations = atlas - .fire_animations - .iter() - .map(|(rect, animation)| (*rect, animation.clone(), usize::MAX)) - .collect(); - Ok(renderer) + self.settings.horizon.api, + ) } fn apply_settings(&mut self) { if let Some(r) = &self.renderer { @@ -7764,7 +8377,7 @@ impl Game<'_> { /// base temperature lowered above y 80) or none (no precipitation). fn precipitation(&self, x: i32, y: i32, z: i32) -> Option { let chunk = self.world.chunks.get(&(x >> 4, z >> 4))?; - let b = self.res.biomes.get(chunk.biome(x, y, z)); + let b = self.res.biomes().get(chunk.biome(x, y, z)); if !b.precipitation { return None; } @@ -7780,7 +8393,7 @@ impl Game<'_> { let mut y = from; while y > MIN_Y { let s = self.world.block(x, y, z); - let f = self.res.facts.get(s); + let f = self.res.facts().get(s); if f.full_collision || f.fluid != Fluid::None || f.name.contains("leaves") { return y + 1; } @@ -7831,7 +8444,7 @@ impl Game<'_> { if intensity <= 0.0 { return; } - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let (Some(rain_tex), Some(snow_tex)) = ( atlas.sprites.get("environment/rain").copied(), atlas.sprites.get("environment/snow").copied(), @@ -7975,44 +8588,59 @@ impl Game<'_> { return; } self.message = Some(("Respawn point set".into(), Instant::now())); - self.sleeping = Some((head, 0)); + self.stop_mining(); + self.left = false; + self.right = false; + self.using = None; + self.sleeping = Some(head); + self.sleep_timer = 0; self.stats.custom("sleep_in_bed", 1); self.trigger_player("minecraft:slept_in_bed"); self.stats.reset("custom", "minecraft:time_since_rest"); } + fn wake_up(&mut self) { + if self.remote.is_some() { + // InBedChatScreen.sendWakeUp: the server wakes the player. + self.remote_stop_sleeping(); + return; + } + self.sleeping = None; + self.sleep_timer = 100; + } fn tick_sleep(&mut self) { - let Some((head, ticks)) = self.sleeping else { - return; - }; - if self.keys.contains(&KeyCode::ShiftLeft) - || self - .player - .as_ref() - .is_none_or(|p| p.health.dead_or_dying()) - { - self.sleeping = None; - return; + self.sleep_timer = tick_sleep_timer(self.sleep_timer, self.sleeping.is_some()); + if let Some(head) = self.sleeping { + if self.keys.contains(&KeyCode::ShiftLeft) + || self + .player + .as_ref() + .is_none_or(|p| p.health.dead_or_dying()) + { + self.wake_up(); + return; + } + if let Some(p) = self.player.as_mut() { + p.position = Vec3::new( + f64::from(head[0]) + 0.5, + f64::from(head[1]) + 0.5625, + f64::from(head[2]) + 0.5, + ); + p.velocity = Vec3::ZERO; + } } - if let Some(p) = self.player.as_mut() { - p.position = Vec3::new( - f64::from(head[0]) + 0.5, - f64::from(head[1]) + 0.5625, - f64::from(head[2]) + 0.5, - ); - p.velocity = Vec3::ZERO; - } - let ticks = ticks + 1; - // ServerLevel: once everyone has slept 100 ticks, skip to morning and clear weather. - if ticks >= 100 { + // ServerLevel.tick: once enough of the players have slept 100 ticks, + // skip to morning, clear the weather and wake everyone. + if self.enough_slept() { let t = self.world.day_time; self.world.day_time = t + 24000 - t.rem_euclid(24000); self.world.weather.raining = false; self.world.weather.thundering = false; self.world.weather.rain_time = 0; self.world.weather.thunder_time = 0; - self.sleeping = None; - } else { - self.sleeping = Some((head, ticks)); + if self.sleeping.is_some() { + self.wake_up(); + } + self.wake_guests(); } } fn creative_origin(batch: &HudBatch) -> (f32, f32) { @@ -8020,7 +8648,7 @@ impl Game<'_> { } /// Vanilla tooltip: dark box with a purple gradient border. fn tooltip(&self, batch: &mut HudBatch, text: &str, x: f32, y: f32) { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let w = HudBatch::text_width(atlas, text); let (x, y) = (x + 12.0, y - 12.0); batch.fill(atlas, x - 3.0, y - 4.0, w + 6.0, 16.0, rgba(16, 0, 16, 240)); @@ -8042,7 +8670,7 @@ impl Game<'_> { ) }; batch.fill( - &self.atlas, + &self.gui.atlas, 0.0, 0.0, batch.width, @@ -8053,7 +8681,7 @@ impl Game<'_> { for (i, t) in tabs.iter().enumerate() { if i != selected { let (tx, ty) = Creative::tab_position(t); - batch.sprite(&self.atlas, &tab_sprite(t, false), ox + tx, oy + ty); + batch.sprite(&self.gui.atlas, &tab_sprite(t, false), ox + tx, oy + ty); } } let background = if tab.search { @@ -8063,13 +8691,13 @@ impl Game<'_> { } else { "gui/container/creative_inventory/tab_items" }; - if let Some(mut r) = self.atlas.sprites.get(background).copied() { + if let Some(mut r) = self.gui.atlas.sprites.get(background).copied() { r.u1 = r.u0 + (r.u1 - r.u0) * Creative::WIDTH / r.w as f32; r.v1 = r.v0 + (r.v1 - r.v0) * Creative::HEIGHT / r.h as f32; batch.rect_uv(ox, oy, Creative::WIDTH, Creative::HEIGHT, r, u32::MAX); } let (tx, ty) = Creative::tab_position(&tab); - batch.sprite(&self.atlas, &tab_sprite(&tab, true), ox + tx, oy + ty); + batch.sprite(&self.gui.atlas, &tab_sprite(&tab, true), ox + tx, oy + ty); for t in &tabs { let (tx, ty) = Creative::tab_position(t); let stack = ItemStack { @@ -8085,7 +8713,7 @@ impl Game<'_> { oy + ty + if t.top { 9.0 } else { 7.0 }, ); } - let atlas = &self.atlas; + let atlas = &self.gui.atlas; if !tab.inventory { batch.text( atlas, @@ -8127,7 +8755,7 @@ impl Game<'_> { if Self::creative_trash_hovered(batch, self.cursor) { let (x, y) = Creative::TRASH_POSITION; batch.fill( - &self.atlas, + &self.gui.atlas, ox + x, oy + y, 16.0, @@ -8138,7 +8766,7 @@ impl Game<'_> { if let Some(slot) = self.slot_at(batch, self.cursor.0, self.cursor.1) { let (x, y) = self.slot_position(slot); batch.fill( - &self.atlas, + &self.gui.atlas, ox + x, oy + y, 16.0, @@ -8161,7 +8789,7 @@ impl Game<'_> { && self.cursor.1 >= y && self.cursor.1 < y + 16.0 { - batch.fill(&self.atlas, x, y, 16.0, 16.0, rgba(255, 255, 255, 128)); + batch.fill(&self.gui.atlas, x, y, 16.0, 16.0, rgba(255, 255, 255, 128)); hovered = Some(stack.clone()); } } @@ -8187,7 +8815,7 @@ impl Game<'_> { "gui/sprites/container/creative_inventory/scroller_disabled" }; batch.sprite( - &self.atlas, + &self.gui.atlas, scroller, ox + Creative::SCROLL_X, oy + Creative::SCROLL_Y + self.creative_screen.scroller_offset(), @@ -8315,6 +8943,7 @@ impl Game<'_> { level.food = p.health.food.food; level.saturation = p.health.food.saturation; + level.flying = p.flying; } level.inventory = self .inventory @@ -8370,18 +8999,10 @@ impl Game<'_> { if self.remote.is_some() { return Ok(()); } - self.streaming.save(&mut self.world, wait); - let keys: Vec<_> = self.world.modified.drain().collect(); - let chunks = self.with_entities(keys.iter().filter_map(|k| self.world.chunks.get(k))); - self.save - .save_later(&chunks, self.pack.assets.registry, &self.res.biomes)?; - if wait { - self.save.flush(); - } - self.save_entities(wait)?; + self.save_level(wait)?; + self.save_parked_levels(wait)?; self.save_maps(); self.save_random_sequences(); - self.save_raids(); self.save_fight(); self.save_wandering_trader(); self.save_portal_arrivals(); @@ -8398,7 +9019,34 @@ impl Game<'_> { self.message = Some(("Saved the game".into(), Instant::now())); Ok(()) } + /// Save the level in place: its changed chunks, its entities and its raids. + fn save_level(&mut self, wait: bool) -> Result<()> { + self.streaming.save(&mut self.world, wait); + let keys: Vec<_> = self.world.modified.drain().collect(); + let chunks = self.with_entities(keys.iter().filter_map(|k| self.world.chunks.get(k))); + self.save + .save_later(&chunks, self.pack.assets.registry, self.res.biomes())?; + if wait { + self.save.flush(); + } + self.save_entities(wait)?; + self.save_raids(); + Ok(()) + } fn render(&mut self) -> Result { + if self.pending_entry.is_some() { + if let (Some(renderer), Some(loader)) = (&mut self.renderer, &self.rest_loader) { + let mut hud = HudBatch::new([renderer.config.width, renderer.config.height]); + horizon_client::level_loading::draw_progress( + &mut hud, + &self.gui.atlas, + loader.progress.line(), + loader.progress.fraction(), + ); + return renderer.render(&loading_frame(&hud)); + } + return Ok(false); + } self.poll_shader_pack(); let Some(size) = self .renderer @@ -8419,7 +9067,7 @@ impl Game<'_> { let s = self.world .block(c.x.floor() as i32, c.y.floor() as i32, c.z.floor() as i32); - (c, self.res.facts.get(s).fluid == Fluid::Water, yaw, pitch) + (c, self.res.facts().get(s).fluid == Fluid::Water, yaw, pitch) } None => ( glam::DVec3::new(0.5, 100.0, 0.5), @@ -8435,13 +9083,29 @@ impl Game<'_> { camera.y.floor() as i32, camera.z.floor() as i32, ]; + // The camera inside an opaque block sees nothing through it: disable. + let cave_culling = self.settings.horizon.cave_culling + && !self + .res + .facts() + .get(self.world.block(block[0], block[1], block[2])) + .solid_render; + // The cave culling search runs on its worker while the frame is prepared. + if cave_culling + && self.title.is_none() + && let Some(renderer) = self.renderer.as_mut() + { + horizon_client::spans::time("occlusion", || { + renderer.start_occlusion(camera, (self.streaming.radius * 16) as f32) + }); + } let biome = self .world .chunks .get(&(block[0] >> 4, block[2] >> 4)) .map(|c| c.biome(block[0], block[1], block[2])) .unwrap_or(0); - let info = self.res.biomes.get(biome).clone(); + let info = self.res.biomes().get(biome).clone(); let environment = self.dim_environment(); let mut sky = environment.sample(self.world.day_time, info.sky, info.fog); let (rain, thunder) = if self.dim == Dim::Overworld { @@ -8482,10 +9146,10 @@ impl Game<'_> { self.pick() .map(|h| { let state = self.world.block(h.pos[0], h.pos[1], h.pos[2]); - let f = self.res.facts.get(state); + let f = self.res.facts().get(state); outline_after_terrain = self .pack - .assets + .render .model_catalog .collect(state, BlockPos::new(h.pos[0], h.pos[1], h.pos[2])) .is_ok_and(|models| { @@ -8538,9 +9202,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() @@ -8617,24 +9282,25 @@ impl Game<'_> { bob, sky, distance, + detail_distance: (self.streaming.radius * 16) as f32, depth_far, underwater, title: title && !live, skybox: self.dim == Dim::Overworld && self.settings.horizon.sky, end_sky: self.dim == Dim::End && self.settings.horizon.sky, environment_fog: (self.dim == Dim::Nether).then_some((sky.fog_start, sky.fog_end)), - // The camera inside an opaque block sees nothing through it: disable. - cave_culling: self.settings.horizon.cave_culling - && !self - .res - .facts - .get(self.world.block(block[0], block[1], block[2])) - .solid_render, + cave_culling, water_fog, 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); } @@ -8650,7 +9316,7 @@ impl Game<'_> { clouds: ((!title || live) && self.dim == Dim::Overworld && horizon_client::clouds::visible(&self.settings.clouds, sky.cloud_color[3])) - .then(|| self.atlas.sprites.get("horizon/white")) + .then(|| self.gui.atlas.sprites.get("horizon/white")) .flatten() .map(|white| horizon_client::clouds::Input { texture: &self.pack.clouds, @@ -8680,14 +9346,15 @@ impl Game<'_> { .mining .as_ref() .and_then(|m| { - self.atlas.sprites.get(&format!( + self.gui.atlas.sprites.get(&format!( "block/destroy_stage_{}", ((m.progress * 10.0) as u32).min(9) )) }) .map_or([0.0; 4], |r| [r.u0, r.v0, r.u1 - r.u0, r.v1 - r.v0]), end_portal_sprites: ["environment/end_sky", "entity/end_portal/end_portal"].map(|n| { - self.atlas + self.gui + .atlas .sprites .get(n) .map_or([0.0; 4], |r| [r.u0, r.v0, r.u1 - r.u0, r.v1 - r.v0]) @@ -8747,7 +9414,7 @@ impl Game<'_> { reason: horizon_client::level_loading::Reason, ) { use horizon_client::level_loading::{self, Reason, STATUS_RADIUS}; - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let (w, h) = (batch.width, batch.height); match reason { // The panorama and its blur are drawn by the renderer. @@ -8801,7 +9468,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) @@ -8820,6 +9487,18 @@ impl Game<'_> { } /// A key press while a world is open (keyboard or control socket). fn key_pressed(&mut self, key: KeyCode) -> Result<()> { + if self.sleeping.is_some() + && self.screen == Screen::Playing + && !matches!( + key, + KeyCode::F1 | KeyCode::F2 | KeyCode::F11 | KeyCode::KeyT | KeyCode::Slash + ) + { + if matches!(key, KeyCode::Escape | KeyCode::ShiftLeft) { + self.wake_up(); + } + return Ok(()); + } if self.sort_key_press(key) { return Ok(()); } @@ -8950,6 +9629,9 @@ impl Game<'_> { } /// A mouse button while playing (window or control socket). fn play_mouse(&mut self, button: MouseButton, pressed: bool) -> Result<()> { + if self.sleeping.is_some() { + return Ok(()); + } if self.remote.is_some() { return self.remote_mouse(button, pressed); } @@ -8993,10 +9675,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, @@ -9005,6 +9739,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))) @@ -9025,7 +9776,7 @@ impl Game<'_> { if !modified.is_empty() && let Err(e) = horizon_client::spans::time("save_chunks", || { self.save - .save_later(&modified, self.pack.assets.registry, &self.res.biomes) + .save_later(&modified, self.pack.assets.registry, self.res.biomes()) }) { log::warn!("Horizon: failed to save unloaded chunks: {e}"); @@ -9042,14 +9793,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))); @@ -9063,7 +9818,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::()) + as u64; if (limit > 0 && count >= limit) || (budget > 0 && bytes > 0 && bytes + size > budget) { @@ -9080,6 +9837,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); + } + } } } } @@ -9112,7 +9896,10 @@ impl ApplicationHandler for Game<'_> { } }; match self.new_renderer(window.clone()) { - Ok(r) => self.renderer = Some(r), + Ok(r) => { + self.renderer = Some(r); + self.world_renderer_ready = true; + } Err(e) => { self.error = Some(e); event_loop.exit(); @@ -9161,7 +9948,6 @@ impl ApplicationHandler for Game<'_> { event: key_event, .. } = &event && let PhysicalKey::Code(key) = key_event.physical_key - && self.title.is_none() { use horizon_client::debug_keys::KeyAction; let action = match (key_event.state, key_event.repeat) { @@ -9191,6 +9977,25 @@ impl ApplicationHandler for Game<'_> { // The press state of Horizon-style buttons. horizon_client::ui_theme::set_pressed(*state == ElementState::Pressed); } + if self.pending_entry.is_some() + && matches!(&event, WindowEvent::KeyboardInput { event, .. } + if event.state == ElementState::Pressed + && event.physical_key == PhysicalKey::Code(KeyCode::Escape)) + { + self.pending_entry = None; + self.perf.cancel_world_opening(); + return; + } + if self.pending_entry.is_some() + && !matches!( + &event, + WindowEvent::CloseRequested + | WindowEvent::Resized(_) + | WindowEvent::RedrawRequested + ) + { + return; + } let result: Result<()> = (|| { match event { WindowEvent::CloseRequested => { @@ -9243,7 +10048,7 @@ impl ApplicationHandler for Game<'_> { && !horizon_client::ui_theme::horizon() { title.drag(self.cursor, (b.width, b.height)); - title.drag_field(&self.atlas, self.cursor.0); + title.drag_field(&self.gui.atlas, self.cursor.0); } if self.dragging && self.screen == Screen::Options { let _ = b; @@ -9279,17 +10084,28 @@ impl ApplicationHandler for Game<'_> { title.scroll_worlds(d, self.cursor, (batch.width, batch.height)); } if self.screen == Screen::Playing + && self.sleeping.is_none() && self.player.is_some() && !self.extras_scroll(d) - && let Some((wheel, _)) = + && let Some((wheel, (_, vertical_steps))) = self.mouse .on_scroll(raw_x, raw_y, discrete, wheel_sensitivity) { - self.selected = horizon_client::mouse::ScrollWheelHandler::next_selection( - f64::from(wheel), - self.selected as i32, - 9, - ) as usize; + if self.spectator() { + if let Some(player) = &mut self.player { + player.flying_speed = horizon_client::mouse::spectator_flying_speed( + player.flying_speed, + vertical_steps, + ); + } + } else { + self.selected = + horizon_client::mouse::ScrollWheelHandler::next_selection( + f64::from(wheel), + self.selected as i32, + 9, + ) as usize; + } } if self.screen == Screen::Chat && d != 0.0 { self.chat_scroll(d); @@ -9347,7 +10163,7 @@ impl ApplicationHandler for Game<'_> { .map(|r| [r.config.width, r.config.height]) .unwrap_or([1, 1]); let mut b = HudBatch::new(size); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let shift = self.has_shift_down(); let (action, sound) = self.title.as_mut().map_or((Action::None, false), |t| { if horizon_client::ui_theme::horizon() { @@ -9552,9 +10368,6 @@ impl ApplicationHandler for Game<'_> { self.station_text(event.text.as_deref(), backspace); } } - WindowEvent::KeyboardInput { event, .. } if self.remote_chat_open() => { - self.remote_chat_key(&event) - } WindowEvent::KeyboardInput { event, .. } if self.screen == Screen::WorldOptions && !matches!(event.physical_key, PhysicalKey::Code(KeyCode::Escape)) => @@ -9661,7 +10474,6 @@ impl ApplicationHandler for Game<'_> { && self.title.is_none() && self.sign_edit.is_none() && self.book.is_none() - && !self.remote_chat_open() { let turn = |k| f32::from(u8::from(self.keys.contains(&k))); let secs = elapsed.as_secs_f32().min(0.1); @@ -9756,6 +10568,10 @@ impl ApplicationHandler for Game<'_> { self.pause = self.pauses_game(); if let Some(server) = &self.integrated { server.set_paused(self.pause); + server.set_distances( + self.settings.render_distance, + self.settings.simulation_distance, + ); } self.clock.end_frame(self.pause); let finished = self @@ -9836,6 +10652,11 @@ impl ApplicationHandler for Game<'_> { } } fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) { + if let Err(error) = self.resume_world_entry() { + self.error = Some(error); + event_loop.exit(); + return; + } self.poll_control(event_loop); self.poll_pads(event_loop); if self.options.frames.is_some() && self.started.elapsed() > self.options.timeout { @@ -9891,7 +10712,7 @@ impl<'a> Game<'a> { fn new( res: &'a Arc, pack: PackResources, - atlas: HudAtlas, + gui: UiResources, options: Options, settings: Settings, audio: Audio, @@ -9900,7 +10721,7 @@ impl<'a> Game<'a> { let registry = pack.assets.registry; // Until a world is opened the streamer is idle. let mut streaming = Streaming::new( - Arc::new(TestTerrain::new(registry, &res.biomes)?), + Arc::new(TestTerrain::new(registry, res.biomes())?), pack.context.clone(), 1, ); @@ -9920,22 +10741,29 @@ impl<'a> Game<'a> { minecarts::CartModels::load(document.as_ref()), ) }; - let world = World::new(res.facts.air, 0); + let world = World::new(res.facts().air, 0); let creative = options.creative; let inventory = vec![None; 36]; let now = Instant::now(); let look = options.look; - let particles = horizon_client::particles::ParticleEngine::new(&root, &atlas.sprites); + let particles = horizon_client::particles::ParticleEngine::new(&root, &gui.atlas.sprites); Ok(Game { res: res.clone(), - collision: &res.collision, - motion: &res.motion, + rest_loader: None, + pending_entry: None, + pending_pack_reload: false, + world_renderer_ready: false, + collision: res.collision(), + motion: res.motion(), pack, - atlas, + gui, fog: Default::default(), screen_fx: Default::default(), world, streaming, + lod: None, + lod_source_attempted: false, + lod_observe: Default::default(), options, player: None, spawn: None, @@ -10070,9 +10898,14 @@ impl<'a> Game<'a> { avatar: Default::default(), flash: 0, sleeping: None, + sleep_timer: 0, dim: Dim::Overworld, world_root: PathBuf::new(), travelling: None, + server_trips: Vec::new(), + level_teleports: Vec::new(), + levels: Default::default(), + level_pass: Default::default(), portals: Default::default(), end_travel: false, respawn_config: None, @@ -10081,6 +10914,7 @@ impl<'a> Game<'a> { end: Default::default(), block_ticks: Ticks::new(0), tick_chunks: Default::default(), + tickets: Default::default(), random_tick_sections: Default::default(), level_events: Vec::new(), falling: Vec::new(), @@ -10169,15 +11003,18 @@ impl<'a> Game<'a> { text_selection: Default::default(), saved_for_exit: false, forced_hit: None, - lan_item_only: false, + guest_use: None, lan_events: Vec::new(), lan_block_events: Vec::new(), lan_entity_events: Vec::new(), lid_openers: HashMap::new(), lan_acting: None, + guest_packets: Vec::new(), lan_acting_name: None, lan_acting_uuid: None, chat_capture: None, + admin_feedback: Vec::new(), + console_broadcast: None, cmds: Default::default(), ui: Default::default(), combat: Default::default(), @@ -10194,7 +11031,7 @@ impl<'a> Game<'a> { tab_list: Default::default(), subtitles: Default::default(), adv: Default::default(), - restore_player_environment: true, + restore_player_state: true, stats_ui: Default::default(), adv_ui: Default::default(), camera_type: Default::default(), @@ -10204,38 +11041,233 @@ impl<'a> Game<'a> { } } -pub fn run() -> Result<()> { - let Some(mut options) = Options::read()? else { - return Ok(()); - }; - if options.dedicated.is_some() && !dedicated::preflight(&mut options)? { - return Ok(()); +/// The data the background loader produces for `Game::new`. +struct Loaded { + root: PathBuf, + pack_error: Option, + resources: Arc, + rest_loader: Option, + pack: PackResources, + /// The menu bundle, for the synchronous launch; the loading screen receives + /// it earlier through its own channel. + gui: Option, +} + +/// What the background loader publishes: the menu bundle as soon as it is +/// ready, then the core bundle with a handle to the rest worker. +enum LoaderMsg { + Ui(UiResources, PathBuf), + Done(std::result::Result), +} + +enum PendingWorldEntry { + Local { + folder: String, + name: Option, + seed: Option, + creative: bool, + create: Option>, + }, + Server { + address: String, + name: String, + }, +} + +struct WorldRestLoader { + progress: Progress, + handle: JoinHandle>, +} + +impl WorldRestLoader { + fn ready(&self) -> Result { + // No time limit: the title stays usable while this loads, and a slow + // disk or a debug build can take minutes. A failure ends the thread + // and is reported by `finish`. + Ok(self.handle.is_finished()) } - let started = Instant::now(); - horizon_client::spans::enable(options.spikes.is_some()); - let vanilla_root = options.resources.clone(); - let early_settings = Settings::load(&std::env::current_dir()?.join("options.txt")); + + fn finish(self) -> Result<()> { + self.handle + .join() + .map_err(|_| "world resource loader panicked")? + .map_err(Into::into) + } +} + +impl Game<'_> { + fn defer_world_entry(&mut self, entry: PendingWorldEntry) -> Result { + self.poll_world_rest()?; + if self.rest_loader.is_none() { + return Ok(false); + } + self.perf.world_opening(); + if self.title.is_none() { + self.title = Some(Title::new( + std::env::current_dir()?.join("saves"), + &self.options.resources, + )); + } + self.pending_entry = Some(entry); + log::info!("Horizon: world entry waiting for world resources"); + Ok(true) + } + + fn resume_world_entry(&mut self) -> Result<()> { + self.poll_world_rest()?; + if self.rest_loader.is_some() { + return Ok(()); + } + if std::mem::take(&mut self.pending_pack_reload) { + self.reload_packs(); + } + match self.pending_entry.take() { + Some(PendingWorldEntry::Local { + folder, + name, + seed, + creative, + create, + }) => { + self.open_world(&folder, name.as_deref(), seed, creative, create.as_deref())?; + } + Some(PendingWorldEntry::Server { address, name }) => { + self.connect_server(&address, &name)?; + } + None => {} + } + Ok(()) + } + + fn poll_world_rest(&mut self) -> Result<()> { + if let Some(loader) = &self.rest_loader + && loader.ready()? + { + self.rest_loader + .take() + .ok_or("missing world loader")? + .finish()?; + } + Ok(()) + } + + fn join_world_rest(&mut self) -> Result<()> { + if self.rest_loader.is_some() { + let waited = Instant::now(); + log::info!("Horizon: waiting for world resources"); + while self.rest_loader.is_some() { + self.poll_world_rest()?; + if self.rest_loader.is_some() { + std::thread::sleep(Duration::from_millis(10)); + } + } + log::info!( + "Horizon: world resources joined after {} ms", + waited.elapsed().as_millis() + ); + } + if self.res.rest.get().is_none() { + return Err("world resources are unavailable".into()); + } + Ok(()) + } +} + +const STAGES: u32 = 10; +const STAGE_LINES: [&str; 10] = [ + "Loading menu", + "Loading assets", + "Loading block facts", + "Loading biomes", + "Loading pack tables", + "Loading collision", + "Loading items", + "Loading recipes", + "Loading world resources", + "World resources ready", +]; + +/// The background loader's checkpoint, read by the loading screen each frame. +#[derive(Clone)] +struct Progress(Arc); +impl Progress { + fn new() -> Self { + Self(Arc::new(AtomicU32::new(0))) + } + fn set(&self, stage: u32) { + self.0.store(stage, Ordering::Relaxed); + } + fn fraction(&self) -> f32 { + self.0.load(Ordering::Relaxed) as f32 / (STAGES - 1) as f32 + } + fn line(&self) -> &'static str { + STAGE_LINES[(self.0.load(Ordering::Relaxed) as usize).min(STAGE_LINES.len() - 1)] + } +} + +/// Harness and dedicated runs keep the fully-synchronous startup so their +/// output stays byte-identical; only an ordinary launch shows the window first. +fn synchronous_startup(options: &Options) -> bool { + let classic = std::env::var("HORIZON_MENU_STYLE").ok().as_deref() == Some("classic"); + options.frames.is_some() + || options.world_frames.is_some() + || options.benchmark.is_some() + || options.control.is_some() + || options.dedicated.is_some() + || classic + || std::env::var_os("HORIZON_DISPENSE_FIXTURE").is_some() + || std::env::var_os("HORIZON_SCULK_FIXTURE").is_some() + || std::env::var_os("HORIZON_COMMAND_SCRIPT").is_some() + || std::env::var_os("HORIZON_NET_REPORT").is_some() +} + +/// The synchronous launch loads both phases; an ordinary launch returns the +/// core bundle while the rest worker keeps loading. +fn load_resources( + mut root: PathBuf, + vanilla_root: &std::path::Path, + settings: &Settings, + started: Instant, + menu: bool, + progress: &Progress, + publish: &mut dyn FnMut(UiResources, &std::path::Path), +) -> Result { + let requested_root = root.clone(); + let threads = settings.horizon.worker_threads(); + // Stage A: the menu bundle (atlas and panorama). With a loading screen it is + // published at once so the title backdrop can appear while the world bundle + // loads; the synchronous launch instead carries it in `Loaded`. + let mut loaded_gui = None; + let mut ui_ready = false; + if let Ok(ui) = UiResources::load(&root) { + if menu { + horizon_client::telemetry::mark_ui_ready(); + log::info!( + "Horizon: UI bundle ready after {} ms", + started.elapsed().as_millis() + ); + publish(ui, &root); + } else { + loaded_gui = Some(ui); + } + ui_ready = true; + } + progress.set(0); let mut pack_error = None; - options.resources = packs::startup_root(&vanilla_root, &early_settings.resource_packs) - .unwrap_or_else(|e| { - log::warn!("Horizon: resource packs not applied: {e}"); - pack_error = Some(e.to_string()); - vanilla_root.clone() - }); - options.resources = mods::content_root( - &options.resources, - &early_settings.mods.enabled, - &options.dev_mods, - ); - let assets = match Assets::load(&options.resources) { - Ok(assets) => assets, + let (assets, render) = match Assets::load(&root).and_then(|assets| { + let render = assets.reload(&root)?; + Ok((assets, render)) + }) { + Ok(bundle) => bundle, // Minecraft.rollbackResourcePacks: the packs break loading, start // with the game's own resources. - Err(e) if options.resources != vanilla_root => { + Err(e) if root != vanilla_root => { log::warn!("Horizon: resource packs failed to load ({e}); using the default resources"); pack_error = Some(e.to_string()); - options.resources = vanilla_root.clone(); - Assets::load(&vanilla_root)? + root = vanilla_root.to_path_buf(); + let assets = Assets::load(vanilla_root)?; + let render = assets.reload(vanilla_root)?; + (assets, render) } Err(e) => { return Err(format!( @@ -10244,42 +11276,178 @@ pub fn run() -> Result<()> { .into()); } }; - let root = options.resources.clone(); let assets = Arc::new(assets); + let render = Arc::new(render); + // The menu bundle must come from the same root as the world bundle. + if !ui_ready || root != requested_root { + let ui = UiResources::load(&root)?; + if menu { + horizon_client::telemetry::mark_ui_ready(); + log::info!( + "Horizon: UI bundle ready after {} ms", + started.elapsed().as_millis() + ); + publish(ui, &root); + } else { + loaded_gui = Some(ui); + } + } log::info!("load assets: {:.2}s", started.elapsed().as_secs_f32()); + progress.set(1); let behaviour_json: serde_json::Value = serde_json::from_reader(std::io::BufReader::new( std::fs::File::open(root.join("block-behaviours.json"))?, ))?; + // The block facts, the biomes and the environment are independent reads: + // run them together, within the worker budget the settings allow. + let registry = assets.registry; + let load_facts = || { + BlockFacts::load_with_projection(&root, registry, &behaviour_json) + .map_err(|e| e.to_string()) + }; + let load_biomes = || Biomes::load(&root).map_err(|e| e.to_string()); + let load_environment = || Environment::load(&root).map_err(|e| e.to_string()); + let (facts, biomes, environment) = if threads > 1 { + std::thread::scope(|scope| { + let facts = scope.spawn(load_facts); + let biomes = scope.spawn(load_biomes); + let environment = scope.spawn(load_environment); + ( + facts.join().unwrap(), + biomes.join().unwrap(), + environment.join().unwrap(), + ) + }) + } else { + (load_facts(), load_biomes(), load_environment()) + }; let (facts, gameplay_projection) = - BlockFacts::load_with_projection(&root, assets.registry, &behaviour_json)?; - let mob_data = horizon_client::mobs::MobData::load(&root, &facts); - horizon_client::mobs::trial_spawner::load_registry(&root); + facts.map_err(|e| -> Box { e.into() })?; + let biomes = Arc::new(biomes.map_err(|e| -> Box { e.into() })?); + let environment = environment.map_err(|e| -> Box { e.into() })?; log::info!("load facts: {:.2}s", started.elapsed().as_secs_f32()); - let items = Items::load(&root, assets.registry, &facts)?; - log::info!("load items: {:.2}s", started.elapsed().as_secs_f32()); - let recipes = Recipes::load(&root, &items)?; - let transformers = horizon_client::block_transform::Transformers::load(&root, assets.registry)?; - let biomes = Arc::new(Biomes::load(&root)?); - let environment = Environment::load(&root)?; + progress.set(2); log::info!("load biomes+env: {:.2}s", started.elapsed().as_secs_f32()); + progress.set(3); let light = Arc::new(LightTables::load(&root, assets.registry, &facts)?); - let pack = PackResources::load(&root, assets.clone(), &facts, biomes.clone(), light)?; + let pack = PackResources::load(&root, assets.clone(), render, &facts, biomes.clone(), light)?; log::info!("load pack tables: {:.2}s", started.elapsed().as_secs_f32()); - let atlas = HudAtlas::build(&root)?; - horizon_client::ui::load_metadata(&root); - horizon_client::lang::set(horizon_client::lang::Language::load( - &[&root], - &early_settings.language, - )); - log::info!("load atlas: {:.2}s", started.elapsed().as_secs_f32()); + progress.set(4); + if !menu { + horizon_client::ui::load_metadata(&root); + horizon_client::lang::set(horizon_client::lang::Language::load( + &[&root], + &settings.language, + )); + } let registry_owner = assets.clone(); let collision = collisions::load(&root, registry_owner.registry)?; - let motion = MotionResources::load(&root, ®istry_owner)?; + let motion = MotionResources::load_with_facts(&root, ®istry_owner, &facts)?; log::info!( "load collision+motion: {:.2}s", started.elapsed().as_secs_f32() ); - let threads = early_settings.horizon.worker_threads(); + progress.set(5); + let resources = Resources { + core: WorldCore { + facts, + biomes: biomes.clone(), + environment, + collision, + motion, + threads, + }, + rest: std::sync::OnceLock::new(), + }; + let resources = Arc::new(resources); + if menu { + horizon_client::telemetry::mark_core_ready(); + log::info!( + "Horizon: core-ready after {} ms", + started.elapsed().as_millis() + ); + } + let rest_loader = if menu { + let resources = resources.clone(); + let root = root.clone(); + let rest_progress = progress.clone(); + let progress = progress.clone(); + let handle = std::thread::Builder::new() + .name("world-rest-loader".into()) + .spawn(move || { + let rest = load_world_rest( + &root, + &resources, + &assets, + &behaviour_json, + &gameplay_projection, + &progress, + started, + ) + .map_err(|error| error.to_string())?; + resources + .rest + .set(rest) + .map_err(|_| "world resources already loaded".to_owned())?; + progress.set(9); + horizon_client::telemetry::mark_rest_ready(); + log::info!( + "Horizon: rest-ready after {} ms", + started.elapsed().as_millis() + ); + Ok(()) + })?; + Some(WorldRestLoader { + progress: rest_progress, + handle, + }) + } else { + let rest = load_world_rest( + &root, + &resources, + &assets, + &behaviour_json, + &gameplay_projection, + progress, + started, + )?; + resources + .rest + .set(rest) + .map_err(|_| "world resources already loaded")?; + None + }; + Ok(Loaded { + root, + pack_error, + resources, + rest_loader, + pack, + gui: loaded_gui, + }) +} + +fn load_world_rest( + root: &std::path::Path, + resources: &Resources, + assets: &Assets, + behaviour_json: &serde_json::Value, + gameplay_projection: &[horizon_client::blocks::GameplayProjection], + progress: &Progress, + started: Instant, +) -> Result { + let facts = resources.facts(); + let biomes = resources.biomes(); + let threads = resources.threads(); + let registry_owner = assets; + progress.set(6); + let mob_data = horizon_client::mobs::MobData::load(root, facts); + horizon_client::mobs::trial_spawner::load_registry(root); + + let items = Items::load(root, assets.registry, facts)?; + log::info!("load items: {:.2}s", started.elapsed().as_secs_f32()); + progress.set(7); + let recipes = Recipes::load(root, &items)?; + let transformers = horizon_client::block_transform::Transformers::load(root, assets.registry)?; log::info!( "Horizon: resources loaded in {:.1}s; {} block states, {} items, {} biomes; {} worker threads", started.elapsed().as_secs_f32(), @@ -10288,57 +11456,194 @@ pub fn run() -> Result<()> { biomes.list.len(), threads ); - let audio = if options.dedicated.is_some() { - Audio::silent(&root) - } else { - Audio::new(&root) - }; - let stations = horizon_client::stations::StationData::load(&root, &items); + progress.set(8); + let stations = horizon_client::stations::StationData::load(root, &items); let animate = - horizon_client::animate_tick::AnimateTables::load(&root, registry_owner.registry, &biomes)?; - let fog = horizon_client::fog::FogTables::load(&root, &biomes); - let resources = Arc::new(Resources { + horizon_client::animate_tick::AnimateTables::load(root, registry_owner.registry, biomes)?; + let fog = horizon_client::fog::FogTables::load(root, biomes); + Ok(WorldRest { stations, - facts, items, recipes, transformers, - biomes, - environment, - collision, - motion, - providers: Providers::load(&root), + providers: Providers::load(root), block_entity_render: horizon_client::block_entity_render::BlockEntityRenderer::load( - &root, + root, registry_owner.registry, )?, animate, fog, - music: horizon_client::music::MusicTables::load(&root), - ambient: horizon_client::ambience::AmbientTables::load(&root), - nether_environment: Environment::load_dimension(&root, "the_nether", false)?, - end_environment: Environment::load_dimension(&root, "the_end", false)?, + music: horizon_client::music::MusicTables::load(root), + ambient: horizon_client::ambience::AmbientTables::load(root), + nether_environment: Environment::load_dimension(root, "the_nether", false)?, + end_environment: Environment::load_dimension(root, "the_end", false)?, behaviours: Behaviours::load_with_gameplay( registry_owner.registry, - &root, - &behaviour_json, - &gameplay_projection, + root, + behaviour_json, + gameplay_projection, )?, - mob_types: MobTypes::load(&root)? + mob_types: MobTypes::load(root)? .types .into_iter() .map(|(k, v)| (k, Arc::new(v))) .collect(), mob_data, - threads, - player_type: perspective::player_type(&root), - player_arm: horizon_client::mobs::load_model(&root, "minecraft:player#main") + player_type: perspective::player_type(root), + player_arm: horizon_client::mobs::load_model(root, "minecraft:player#main") .and_then(|m| m.children.into_iter().find(|c| c.name == "right_arm")), - }); - // Both inputs are needed only while constructing the behaviour tables. - // Release their owned JSON/Arc buffers before entering the long-lived game loop. - drop(gameplay_projection); - drop(behaviour_json); + }) +} + +/// The renderer from a pack's block sprites, its shared atlas and its border +/// texture; `previous` shares the device and window surface with an old one. +fn build_renderer( + window: Arc, + offscreen: bool, + pack: &PackResources, + gui: &UiResources, + previous: Option<&Renderer>, + api: horizon_client::graphics::GraphicsApi, +) -> Result { + let assets = &pack.render; + let atlas = &gui.atlas; + let mut renderer = pollster::block_on(Renderer::new( + window, + offscreen, + &assets.pixels, + assets.sprite_size, + assets.sprite_minimum_size, + assets.texture_layers, + &assets.sprite_mips, + &assets.animations, + (&atlas.pixels, atlas.width, atlas.height), + (&pack.forcefield.2, pack.forcefield.0), + &gui.panorama, + previous, + api, + ))?; + renderer.hud.fire_animations = atlas + .fire_animations + .iter() + .map(|(rect, animation)| (*rect, animation.clone(), usize::MAX)) + .collect(); + Ok(renderer) +} + +/// The loading screen's renderer: the real HUD atlas and font over one opaque +/// white block-sprite layer, an empty border texture and the title panorama +/// once the menu bundle is ready. +fn loading_renderer( + window: Arc, + offscreen: bool, + gui: Option<&UiResources>, + previous: Option<&Renderer>, + api: horizon_client::graphics::GraphicsApi, +) -> Result { + static FORCEFIELD: [u8; 4] = [0; 4]; + static EMPTY_HUD: [u8; 4] = [0, 0, 0, 255]; + let empty_panorama = horizon_client::gfx::panorama::Faces { + size: 1, + pixels: [0u8; 4].repeat(6), + }; + let panorama = gui.map_or(&empty_panorama, |gui| gui.panorama.as_ref()); + let atlas = gui.map(|gui| &gui.atlas); + let (pixels, width, height) = match atlas { + Some(atlas) => (atlas.pixels.as_slice(), atlas.width, atlas.height), + None => (EMPTY_HUD.as_slice(), 1, 1), + }; + let mut renderer = pollster::block_on(Renderer::new( + window, + offscreen, + &[255, 255, 255, 255], + 1, + 1, + 1, + &[], + &[], + (pixels, width, height), + (&FORCEFIELD[..], 1), + panorama, + previous, + api, + ))?; + if let Some(atlas) = atlas { + renderer.hud.fire_animations = atlas + .fire_animations + .iter() + .map(|(rect, animation)| (*rect, animation.clone(), usize::MAX)) + .collect(); + } + Ok(renderer) +} + +/// A title-shaped frame: black clear and panorama, only the HUD is drawn. +fn loading_frame(hud: &HudBatch) -> Frame<'_> { + Frame { + camera: glam::DVec3::new(0.5, 100.0, 0.5), + yaw: 0.0, + pitch: 10.0, + fov_degrees: 85.0, + 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, + title: true, + skybox: false, + end_sky: false, + environment_fog: None, + water_fog: [0.0; 3], + fog: None, + dark_disc: false, + outline: Vec::new(), + outline_after_terrain: false, + debug_lines: &[], + debug_lines_on_top: &[], + debug_crosshair: &[], + hud, + nether_lighting: false, + entities: &[], + clouds: None, + additive: &[], + see_through: &[], + celestial: &[], + hand: &[], + hand_fov_degrees: 70.0, + screen: &[], + border: None, + game_time: 0.0, + shader_game_time: 0.0, + end_portal_sprites: [[0.0; 4]; 2], + crumbling_sprite: [0.0; 4], + ticks: 0, + darkness: (0.0, 0.0), + night_vision: 0.0, + block_flicker: 0.0, + post: None, + menu_blur: None, + shader: Default::default(), + } +} + +/// The post-load setup once the resources are ready: `Game::new` and the +/// options, packs, mods, control socket, benchmark and world selection the +/// old `run` did after loading. `None` means a harness or dedicated run +/// finished here instead. +#[allow(clippy::too_many_arguments)] +fn start_game<'a>( + res: &'a Arc, + pack: PackResources, + gui: UiResources, + options: Options, + pack_error: Option, + audio: Audio, + vanilla_root: PathBuf, + started: Instant, + rest_loader: Option, +) -> Result>> { let mut settings = Settings::load(&std::env::current_dir()?.join("options.txt")); horizon_client::ui_theme::load_preferences(&std::env::current_dir()?); // Scripted and frame-limited runs (the parity harnesses) see vanilla's @@ -10351,7 +11656,8 @@ pub fn run() -> Result<()> { if let Some(d) = options.distance_override { settings.render_distance = d; } - let mut game = Game::new(&resources, pack, atlas, options, settings, audio)?; + let mut game = Game::new(res, pack, gui, options, settings, audio)?; + game.rest_loader = rest_loader; game.ui.vanilla_root = vanilla_root; if let Some(error) = pack_error { game.open_options_at(true, horizon_client::options_ui::Page::Packs); @@ -10401,13 +11707,16 @@ pub fn run() -> Result<()> { ); } if let Ok(spec) = std::env::var("HORIZON_DISPENSE_FIXTURE") { - return game.dispense_fixture(&spec); + game.dispense_fixture(&spec)?; + return Ok(None); } if let Ok(spec) = std::env::var("HORIZON_SCULK_FIXTURE") { - return game.sculk_fixture(&spec); + game.sculk_fixture(&spec)?; + return Ok(None); } if game.options.dedicated.is_some() { - return game.run_dedicated(); + game.run_dedicated()?; + return Ok(None); } match game.options.world.clone() { _ if game.options.server.is_some() => { @@ -10417,7 +11726,8 @@ pub fn run() -> Result<()> { ); game.connect_server(&address, &name)?; if let Some(report) = std::env::var_os("HORIZON_NET_REPORT") { - return game.run_net_harness(report.as_ref()); + game.run_net_harness(report.as_ref())?; + return Ok(None); } } Some(folder) => { @@ -10443,9 +11753,13 @@ pub fn run() -> Result<()> { std::env::var_os("HORIZON_COMMAND_SCRIPT"), std::env::var_os("HORIZON_COMMAND_OUTPUT"), ) { - return game.run_command_harness(script.as_ref(), output.as_ref()); + game.run_command_harness(script.as_ref(), output.as_ref())?; + return Ok(None); } - EventLoop::new()?.run_app(&mut game)?; + Ok(Some(game)) +} + +fn write_report(game: &Game<'_>) -> Result<()> { if let Some(path) = &game.options.report { let report = serde_json::json!({ "frames": game.frames, @@ -10458,12 +11772,433 @@ pub fn run() -> Result<()> { }); std::fs::write(path, serde_json::to_vec_pretty(&report)?)?; } - if let Some(e) = game.error { + Ok(()) +} + +fn run_event_loop(mut game: Game<'_>) -> Result<()> { + EventLoop::new()?.run_app(&mut game)?; + write_report(&game)?; + if let Some(e) = game.error.take() { return Err(e); } Ok(()) } +/// The loading screen's state while the worker loads the resources. +struct Loading { + window: Option>, + renderer: Option, + gui: Option, + settings: Settings, + fullscreen: horizon_client::window::FullscreenState, + options: Option, + vanilla_root: PathBuf, + pack_error: Option, + started: Instant, + progress: Progress, + receiver: Receiver, + loaded: Option>, + handle: Option>, + shown: bool, + menu: startup_menu::StartupMenu, + error: Option>, +} +impl Loading { + fn resumed(&mut self, event_loop: &ActiveEventLoop) { + if self.window.is_some() { + return; + } + // The Window constructor: created fullscreen when options.txt says so. + let fullscreen = self.settings.fullscreen; + self.fullscreen = horizon_client::window::FullscreenState::new(fullscreen); + let attributes = Window::default_attributes() + .with_title("Horizon") + .with_window_icon(horizon_client::window::icon()); + // Wayland names the window by its app id, which the horizon.desktop entry matches. + #[cfg(target_os = "linux")] + let attributes = winit::platform::wayland::WindowAttributesExtWayland::with_name( + attributes, "horizon", "horizon", + ); + let window = match event_loop.create_window( + attributes + .with_inner_size(winit::dpi::LogicalSize::new(1280.0, 720.0)) + .with_fullscreen(fullscreen.then_some(winit::window::Fullscreen::Borderless(None))), + ) { + Ok(w) => Arc::new(w), + Err(e) => { + self.error = Some(e.into()); + event_loop.exit(); + return; + } + }; + let offscreen = self.options.as_ref().is_some_and(|o| o.offscreen); + match loading_renderer( + window.clone(), + offscreen, + self.gui.as_ref(), + None, + self.settings.horizon.api, + ) { + Ok(r) => self.renderer = Some(r), + Err(e) => { + self.error = Some(e); + event_loop.exit(); + return; + } + } + if let Some(r) = &self.renderer { + self.settings + .clamp_gui_scale([r.config.width as i32, r.config.height as i32]); + } + horizon_client::hud::GUI_SCALE.store(self.settings.gui_scale, Ordering::Relaxed); + self.window = Some(window); + } + fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) { + if self.window.as_ref().is_none_or(|w| w.id() != id) { + return; + } + if let Err(error) = self.menu_event(event_loop, event) { + self.error = Some(error); + event_loop.exit(); + } + } + /// The menu bundle arrived: rebuild the loading screen with the real atlas + /// and title panorama. + fn set_ui(&mut self, gui: UiResources, root: PathBuf) { + log::info!( + "Horizon: loading the title artwork after {} ms", + self.started.elapsed().as_millis() + ); + self.menu.set_root(root, &self.settings); + self.gui = Some(gui); + horizon_client::ui_theme::reset_atlas(); + self.apply_menu_settings(); + let Some(window) = self.window.clone() else { + return; + }; + let offscreen = self.options.as_ref().is_some_and(|o| o.offscreen); + if let Some(renderer) = &mut self.renderer { + renderer.release_surface(); + } + match loading_renderer( + window, + offscreen, + self.gui.as_ref(), + self.renderer.as_ref(), + self.settings.horizon.api, + ) { + Ok(r) => { + self.settings + .clamp_gui_scale([r.config.width as i32, r.config.height as i32]); + horizon_client::hud::GUI_SCALE.store(self.settings.gui_scale, Ordering::Relaxed); + self.renderer = Some(r); + } + Err(e) => self.error = Some(e), + } + } + fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) { + if let Err(error) = self.poll_menu_resources() { + self.error = Some(error); + event_loop.exit(); + return; + } + if let Some(w) = &self.window { + if self.options.as_ref().is_some_and(|o| o.offscreen) { + self.draw(); + } else { + w.request_redraw(); + } + } + event_loop.set_control_flow(ControlFlow::Poll); + } + fn draw(&mut self) { + let Some(size) = self + .renderer + .as_ref() + .map(|r| [r.config.width, r.config.height]) + else { + return; + }; + let mut batch = HudBatch::new(size); + self.draw_menu(&mut batch); + let frame = loading_frame(&batch); + if let Some(renderer) = &mut self.renderer { + match renderer.render(&frame) { + Ok(true) if self.gui.is_some() && self.menu.pending.is_none() => { + self.menu.perf.title_shown() + } + Ok(_) => {} + Err(e) => log::warn!("Horizon: loading screen frame failed: {e}"), + } + } + if !self.shown { + self.shown = true; + log::info!( + "Horizon: loading screen shown after {} ms", + self.started.elapsed().as_millis() + ); + } + } + fn finish(&mut self, loaded: Loaded) -> Result>> { + let mut options = self.options.take().ok_or("loading already finished")?; + options.resources = loaded.root.clone(); + // StartupMenu owns world selection until it can hand it to Game. + options.world = None; + options.server = None; + let gui = self.gui.take().ok_or("missing menu bundle")?; + let window = self.window.clone().ok_or("window closed")?; + // Keep the loader's device and swap in the real textures in one step. + if let Some(renderer) = &mut self.renderer { + renderer.release_surface(); + } + let renderer = build_renderer( + window.clone(), + options.offscreen, + &loaded.pack, + &gui, + self.renderer.as_ref(), + self.settings.horizon.api, + )?; + log::info!( + "Horizon: world renderer ready after {} ms", + self.started.elapsed().as_millis() + ); + // Audio is created on the main thread, after the core is ready. + let audio = if options.dedicated.is_some() { + Audio::silent(&options.resources) + } else { + Audio::new(&options.resources) + }; + let pack_error = loaded.pack_error.or_else(|| self.pack_error.take()); + // The game borrows its shared resources for the process's lifetime, + // like the single copy an integrated server would share. + let res: &'static Arc = Box::leak(Box::new(loaded.resources)); + let Some(mut game) = start_game( + res, + loaded.pack, + gui, + options, + pack_error, + audio, + self.vanilla_root.clone(), + self.started, + loaded.rest_loader, + )? + else { + return Ok(None); + }; + game.window = Some(window); + game.renderer = Some(renderer); + game.world_renderer_ready = true; + game.fullscreen = self.fullscreen; + self.menu.handoff(&mut game); + game.settings = self.settings.clone(); + game.apply_settings(); + game.previous = Instant::now(); + Ok(Some(game)) + } +} + +/// The application: the loading screen until the resources are ready, then +/// the game itself, on the same event loop and window. +enum App { + Loading(Box), + Ready(Box>), +} +impl ApplicationHandler for App { + fn resumed(&mut self, event_loop: &ActiveEventLoop) { + match self { + App::Loading(loading) => loading.resumed(event_loop), + App::Ready(game) => game.resumed(event_loop), + } + } + fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) { + match self { + App::Loading(loading) => loading.window_event(event_loop, id, event), + App::Ready(game) => game.window_event(event_loop, id, event), + } + } + fn device_event(&mut self, event_loop: &ActiveEventLoop, id: DeviceId, event: DeviceEvent) { + if let App::Ready(game) = self { + game.device_event(event_loop, id, event); + } + } + fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) { + enum Step { + Waiting, + Ready(Box>), + Done, + Failed(Box), + } + let step = match self { + App::Ready(game) => { + game.about_to_wait(event_loop); + return; + } + App::Loading(loading) => { + loading.about_to_wait(event_loop); + if loading.error.is_some() { + event_loop.exit(); + return; + } + match if loading.menu.reload_packs { + None + } else { + loading.loaded.take() + } { + None => Step::Waiting, + Some(Err(e)) => Step::Failed(e.into()), + Some(Ok(loaded)) => match loading.finish(loaded) { + Ok(Some(game)) => Step::Ready(Box::new(game)), + Ok(None) => Step::Done, + Err(e) => Step::Failed(e), + }, + } + } + }; + match step { + Step::Waiting => {} + Step::Ready(game) => *self = App::Ready(game), + Step::Done => event_loop.exit(), + Step::Failed(e) => { + if let App::Loading(loading) = self { + loading.error = Some(e); + } + event_loop.exit(); + } + } + } + fn exiting(&mut self, event_loop: &ActiveEventLoop) { + if let App::Ready(game) = self { + game.exiting(event_loop); + } + } +} + +/// The ordinary launch: load the resources on a worker while the window shows +/// the loading screen, then swap in the game on the main thread. +fn run_loading( + options: Options, + settings: Settings, + vanilla_root: PathBuf, + pack_error: Option, + started: Instant, +) -> Result<()> { + horizon_client::ui_theme::load_preferences(&std::env::current_dir()?); + let progress = Progress::new(); + let (receiver, loader) = + startup_menu::spawn_loader(&options, &settings, &vanilla_root, started, &progress)?; + let menu = startup_menu::StartupMenu::new(&options, pack_error.as_deref())?; + let loading = Loading { + window: None, + renderer: None, + gui: None, + settings, + fullscreen: horizon_client::window::FullscreenState::new(false), + options: Some(options), + vanilla_root, + pack_error, + started, + progress, + receiver, + loaded: None, + handle: Some(loader), + shown: false, + menu, + error: None, + }; + let mut app = App::Loading(Box::new(loading)); + EventLoop::new()?.run_app(&mut app)?; + match app { + App::Ready(mut game) => { + write_report(&game)?; + if let Some(e) = game.error.take() { + return Err(e); + } + } + App::Loading(loading) => { + if let Some(e) = loading.error { + return Err(e); + } + } + } + Ok(()) +} + +pub fn run() -> Result<()> { + let Some(mut options) = Options::read()? else { + return Ok(()); + }; + if options.dedicated.is_some() && !dedicated::preflight(&mut options)? { + return Ok(()); + } + let started = Instant::now(); + horizon_client::spans::enable(options.spikes.is_some()); + let vanilla_root = options.resources.clone(); + let early_settings = Settings::load(&std::env::current_dir()?.join("options.txt")); + // Snapshots of derived tables live in the game folder, never beside the + // shared resources. + Behaviours::set_cache_dir(std::env::current_dir()?.join(".horizon-cache")); + if !synchronous_startup(&options) { + return run_loading(options, early_settings, vanilla_root, None, started); + } + let mut pack_error = None; + options.resources = packs::startup_root(&vanilla_root, &early_settings.resource_packs) + .unwrap_or_else(|e| { + log::warn!("Horizon: resource packs not applied: {e}"); + pack_error = Some(e.to_string()); + vanilla_root.clone() + }); + options.resources = mods::content_root( + &options.resources, + &early_settings.mods.enabled, + &options.dev_mods, + ); + let loaded = load_resources( + options.resources.clone(), + &vanilla_root, + &early_settings, + started, + false, + &Progress::new(), + &mut |_, _| {}, + )?; + let gui = loaded.gui.ok_or("missing menu bundle")?; + options.resources = loaded.root.clone(); + let audio = if options.dedicated.is_some() { + Audio::silent(&options.resources) + } else { + Audio::new(&options.resources) + }; + let resources = loaded.resources; + let game = start_game( + &resources, + loaded.pack, + gui, + options, + loaded.pack_error.or(pack_error), + audio, + vanilla_root, + started, + loaded.rest_loader, + )?; + if let Some(game) = game { + run_event_loop(game)?; + } + Ok(()) +} + +// Player.tick: cap the sleeping fade at 100, then fade out over 10 waking ticks. +fn tick_sleep_timer(timer: u32, sleeping: bool) -> u32 { + if sleeping { + (timer + 1).min(100) + } else if (1..109).contains(&timer) { + timer + 1 + } else { + 0 + } +} + fn reset_world_progress(adv: &mut advancements::AdvState, stats: &mut stats::Stats) { *adv = Default::default(); *stats = Default::default(); @@ -10498,6 +12233,93 @@ mod tests { use horizon_client::container_screen::{Button, Clicks, Release, Target}; use horizon_client::keymapping::{KeyAction, SPRINT, ToggleKeys}; + #[test] + fn sleep_fade_lasts_ten_ticks_after_waking() { + let alpha = |timer| match hud_overlays::sleep_overlay(320, 240, timer).first() { + Some(hud_layout::GuiDraw::Fill { color, .. }) => color >> 24, + None => 0, + _ => panic!("sleep overlay must be a full-screen fill"), + }; + assert_eq!(tick_sleep_timer(0, false), 0); + let mut timer = 0; + for _ in 0..50 { + timer = tick_sleep_timer(timer, true); + } + assert_eq!(alpha(timer as i32), 110); + for _ in 0..100 { + timer = tick_sleep_timer(timer, true); + } + assert_eq!(timer, 100); + assert_eq!(alpha(timer as i32), 220); + for _ in 0..5 { + timer = tick_sleep_timer(timer, false); + } + assert_eq!(alpha(timer as i32), 110); + for _ in 0..5 { + timer = tick_sleep_timer(timer, false); + } + assert_eq!(timer, 0); + assert!(hud_overlays::sleep_overlay(320, 240, timer as i32).is_empty()); + } + + #[test] + fn world_rest_poll_does_not_wait_for_a_blocked_worker() { + let (release, wait) = std::sync::mpsc::channel(); + let loader = WorldRestLoader { + progress: Progress::new(), + handle: std::thread::spawn(move || { + wait.recv().unwrap(); + Ok(()) + }), + }; + assert!(!loader.ready().unwrap()); + release.send(()).unwrap(); + loader.finish().unwrap(); + } + + #[test] + fn world_rest_poll_waits_for_a_slow_worker() { + let (release, wait) = std::sync::mpsc::channel(); + let loader = WorldRestLoader { + progress: Progress::new(), + handle: std::thread::spawn(move || { + wait.recv().unwrap(); + Ok(()) + }), + }; + // However long it takes, a worker still loading is not an error. + assert!(!loader.ready().unwrap()); + release.send(()).unwrap(); + while !loader.ready().unwrap() { + std::thread::sleep(Duration::from_millis(1)); + } + loader.finish().unwrap(); + } + + #[test] + fn world_rest_worker_failure_reaches_the_client() { + let loader = WorldRestLoader { + progress: Progress::new(), + handle: std::thread::spawn(|| Err("missing world table".to_owned())), + }; + assert_eq!( + loader.finish().unwrap_err().to_string(), + "missing world table" + ); + } + + #[test] + fn world_rest_worker_panic_reaches_the_client() { + let loader = WorldRestLoader { + progress: Progress::new(), + handle: std::thread::spawn(|| panic!("broken loader")), + }; + assert_eq!( + loader.finish().unwrap_err().to_string(), + "world resource loader panicked" + ); + } + /// An integrated server thread shares the loaded resources with the client. #[test] fn shared_resources_can_cross_threads() { diff --git a/crates/horizon-client/src/game/advancements.rs b/crates/horizon-client/src/game/advancements.rs index 980b5769e..d13ed6b13 100644 --- a/crates/horizon-client/src/game/advancements.rs +++ b/crates/horizon-client/src/game/advancements.rs @@ -11,7 +11,7 @@ use horizon_client::{ }; use serde_json::Value; use std::{ - collections::{BTreeMap, VecDeque}, + collections::{BTreeMap, BTreeSet, VecDeque}, sync::Arc, time::Instant, }; @@ -72,13 +72,25 @@ impl Toast { #[derive(Default)] pub(super) struct AdvState { pub(super) tree: Arc, + /// `ClientAdvancements`: a server's tree carries each advancement's + /// place on its tab (`DisplayInfo` x, y), in tree order. + pub(super) positions: Option>, pub(super) progress: Progress, pub(super) loaded: bool, pub(super) toasts: VecDeque, /// Awards skip the toast (`/advancement grant ... everything`). pub(super) quiet: bool, /// The inventory last seen by `inventory_changed`. - last_inventory: u64, + pub(super) last_inventory: u64, + /// While the game acts for a network player (`as_guest`): the + /// advancements whose progress changed (`progressChanged`), the recipes + /// unlocked and the announcements, for their client and everyone's. + pub(super) changed: BTreeSet, + pub(super) unlocked: Vec, + pub(super) announced: Vec, + /// The tree being read on another thread since the world opened + /// (`prefetch_advancement_tree`), taken by the first `advancement_tree`. + pub(super) pending_tree: Option>, } /// The game's world for predicates. struct View<'a, 'g>(&'a Game<'g>); @@ -94,7 +106,7 @@ impl adv::World for View<'_, '_> { .map(|c| { self.0 .res - .biomes + .biomes() .get(c.biome(p[0], p[1], p[2])) .name .clone() @@ -106,7 +118,7 @@ impl adv::World for View<'_, '_> { } fn block(&self, p: [i32; 3]) -> (String, BTreeMap) { let state = self.0.world.block(p[0], p[1], p[2]); - let name = self.0.res.facts.get(state).name.to_string(); + let name = self.0.res.facts().get(state).name.to_string(); let props = self .0 .pack @@ -126,7 +138,7 @@ impl adv::World for View<'_, '_> { match self .0 .res - .facts + .facts() .get(self.0.world.block(p[0], p[1], p[2])) .fluid { @@ -140,7 +152,7 @@ impl adv::World for View<'_, '_> { !self .0 .res - .facts + .facts() .get(self.0.world.block(p[0], y, p[2])) .full_collision }) @@ -153,47 +165,28 @@ impl adv::World for View<'_, '_> { (1..=5).any(|d| { self.0 .res - .facts + .facts() .get(self.0.world.block(p[0], p[1] - d, p[2])) .name .ends_with("campfire") }) } fn in_tag(&self, registry: &str, tag: &str, id: &str) -> bool { - let items = &self.0.res.items; + let items = &self.0.res.items(); match registry { "item" => items.item_tags.get(tag).is_some_and(|t| t.contains(id)), "block" => items.block_tags.get(tag).is_some_and(|t| t.contains(id)), - "entity_type" => self.0.res.mob_data.entity_has(tag, id), - "worldgen/biome" => self.0.res.mob_data.biome_has(tag, id), + "entity_type" => self.0.res.mob_data().entity_has(tag, id), + "worldgen/biome" => self.0.res.mob_data().biome_has(tag, id), _ => false, } } } fn now_text() -> String { - // "yyyy-MM-dd HH:mm:ss Z" in UTC. let secs = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map_or(0, |d| d.as_secs()) as i64; - let days = secs.div_euclid(86400); - let rem = secs.rem_euclid(86400); - // Civil from days (Howard Hinnant's algorithm). - let z = days + 719_468; - let era = z.div_euclid(146_097); - let doe = z - era * 146_097; - let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146_096) / 365; - let y = yoe + era * 400; - let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); - let mp = (5 * doy + 2) / 153; - let d = doy - (153 * mp + 2) / 5 + 1; - let m = if mp < 10 { mp + 3 } else { mp - 9 }; - let y = if m <= 2 { y + 1 } else { y }; - format!( - "{y:04}-{m:02}-{d:02} {:02}:{:02}:{:02} +0000", - rem / 3600, - rem % 3600 / 60, - rem % 60 - ) + adv::date_text(secs) } impl Game<'_> { /// `ItemPredicate.test` (the `item_filter` of a filtered item modifier). @@ -217,30 +210,47 @@ pub(super) fn item_view(s: &ItemStack) -> ItemView { } impl Game<'_> { fn adv_path(&self) -> Option { - let uuid = self.level.player_uuid?; - let hex: String = uuid.iter().map(|v| format!("{:08x}", *v as u32)).collect(); - let text = format!( - "{}-{}-{}-{}-{}", - &hex[0..8], - &hex[8..12], - &hex[12..16], - &hex[16..20], - &hex[20..32] - ); - Some( - self.world_root - .join("players/advancements") - .join(format!("{text}.json")), - ) + Some(super::stats::player_json( + &self.world_root, + "advancements", + self.level.player_uuid?, + )) + } + /// A server with no player of its own and nobody it acts for: there is + /// no player for advancements and statistics. + pub(super) fn no_player_here(&self) -> bool { + self.options.dedicated.is_some() && self.lan_acting.is_none() + } + /// The advancement tree, loaded once. + pub(super) fn advancement_tree(&mut self) -> Arc { + if self.adv.tree.list.is_empty() { + let tree = match self.adv.pending_tree.take().map(|h| h.join()) { + Some(Ok(tree)) => tree, + _ => Tree::load(&self.options.resources.join("data")), + }; + self.adv.tree = Arc::new(tree); + } + self.adv.tree.clone() + } + /// Start reading the advancement files on another thread while the world + /// loads. The first tick's first criterion trigger used to read and parse + /// all of them on the game thread (about 60 ms). + pub(super) fn prefetch_advancement_tree(&mut self) { + if !self.adv.tree.list.is_empty() || self.adv.pending_tree.is_some() { + return; + } + let data = self.options.resources.join("data"); + self.adv.pending_tree = std::thread::Builder::new() + .name("horizon-advancements".into()) + .spawn(move || Tree::load(&data)) + .ok(); } pub(super) fn load_advancements(&mut self) { - if self.adv.loaded || self.remote.is_some() { + if self.adv.loaded || self.remote.is_some() || self.no_player_here() { return; } self.adv.loaded = true; - if self.adv.tree.list.is_empty() { - self.adv.tree = Arc::new(Tree::load(&self.options.resources.join("data"))); - } + self.advancement_tree(); let progress = self .adv_path() .and_then(|p| std::fs::read_to_string(p).ok()) @@ -254,7 +264,7 @@ impl Game<'_> { self.ui.registries.advancements = tree.list.iter().map(|a| a.id.clone()).collect(); } pub(super) fn save_advancements(&self) { - if !self.adv.loaded || self.remote.is_some() { + if !self.adv.loaded || self.remote.is_some() || self.no_player_here() { return; } let Some(path) = self.adv_path() else { @@ -376,7 +386,7 @@ impl Game<'_> { /// `SimpleCriterionTrigger.trigger`: test each open criterion of the /// trigger and award what it completes. pub(super) fn trigger(&mut self, name: &str, test: impl Fn(&Value, &dyn adv::World) -> bool) { - if self.remote.is_some() { + if self.remote.is_some() || self.no_player_here() { return; } self.load_advancements(); @@ -398,20 +408,35 @@ impl Game<'_> { } } } + if hits.is_empty() { + return; + } let date = now_text(); for (ai, ci) in hits { let a = &tree.list[ai]; let was = self.adv.progress.done(a); self.adv.progress.grant(&a.id, &a.criteria[ci].name, &date); + self.progress_changed(ai); if !was && self.adv.progress.done(a) { self.award(ai); } } } + /// `PlayerAdvancements.progressChanged`, kept for a network player's client. + fn progress_changed(&mut self, index: usize) { + if self.lan_acting.is_some() { + self.adv.changed.insert(index); + } + } /// After `ServerRecipeBook.addRecipes`: the toast and the /// `recipe_unlocked` trigger of each recipe now known. pub(super) fn recipe_unlocked(&mut self, unlocked: &[String]) { - self.recipe_toast(unlocked); + if self.lan_acting.is_some() { + // A network player's client gets them in ClientboundRecipeBookAddPacket. + self.adv.unlocked.extend_from_slice(unlocked); + } else { + self.recipe_toast(unlocked); + } for r in unlocked { let r = r.clone(); self.trigger("minecraft:recipe_unlocked", move |c, _| { @@ -432,17 +457,17 @@ impl Game<'_> { .rewards .recipes .iter() - .filter(|r| self.res.stations.recipe_index.contains(r)) + .filter(|r| self.res.stations().recipe_index.contains(r)) .cloned() .collect(); let (_, unlocked) = self .level .recipe_book - .add_recipes(&self.res.stations.recipe_index, &rewards); + .add_recipes(&self.res.stations().recipe_index, &rewards); for table in &a.rewards.loot { let items = self .res - .items + .items() .loot_table(table, false, None, &mut self.world.random); for s in items { if let Some(rest) = self.give(s) @@ -453,7 +478,8 @@ impl Game<'_> { } } if let Some(d) = &a.display { - if d.show_toast && !self.adv.quiet { + // A network player's client shows its own toast. + if d.show_toast && !self.adv.quiet && self.lan_acting.is_none() { self.adv.toasts.push_back(Toast { icon: d.icon.clone(), frame: d.frame, @@ -471,10 +497,21 @@ impl Game<'_> { Frame::Challenge => "chat.type.advancement.challenge", }; let name = adv_name(a); - self.system_message(Text::translate( - key, - &[Text::literal(super::chat::PLAYER_NAME), name], - )); + // PlayerList.broadcastSystemMessage: everyone hears of a + // network player's advancement. + match self.lan_acting_name.clone() { + // Player.getDisplayName. + Some(_) => { + let player = self.target_name(super::commands::Target::Player); + self.adv + .announced + .push(Text::translate(key, &[player, name])); + } + None => self.system_message(Text::translate( + key, + &[Text::literal(super::chat::PLAYER_NAME), name], + )), + } } } self.recipe_unlocked(&unlocked); @@ -518,7 +555,7 @@ impl Game<'_> { .filter_map(|s| s.as_ref()) .filter(|s| { self.res - .items + .items() .get(&s.item) .is_some_and(|i| s.count >= i.max_stack) }) @@ -611,6 +648,19 @@ impl Game<'_> { adv::conditions(&c["player"], &pctx, w) && adv::item(&c["item"], &it, w) }); } + pub(super) fn trigger_item_entity( + &mut self, + name: &str, + stack: &ItemStack, + target: &EntityView, + ) { + if self.lan_acting.is_some() { + return; + } + let me = self.player_entity(); + let it = item_view(stack); + self.trigger(name, |c, w| adv::item_entity(c, &me, target, &it, w)); + } /// `EnterBlockTrigger`: the block, its state properties and the player. pub(super) fn trigger_enter_block(&mut self, state: horizon_world::registry::BlockStateId) { let Some(definition) = self.pack.assets.registry.state(state).cloned() else { @@ -746,11 +796,11 @@ impl Game<'_> { if let Some((station, item)) = recipe { // RecipeToast: black description under a purple title, the // station at 0.6 scale in the corner over the cycling result. - let atlas = &self.atlas; + let atlas = &self.gui.atlas; batch.sprite(atlas, "gui/sprites/toast/recipe", x, 0.0); let title = self.translate("recipe.toast.title", "New Recipe(s) Unlocked!"); let description = self.translate("recipe.toast.description", "Check your recipe book"); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; batch.text(atlas, &title, x + 30.0, 7.0, 0xFF50_0050, false); batch.text(atlas, &description, x + 30.0, 18.0, 0xFF00_0000, false); let start = batch.vertices.len(); @@ -759,7 +809,7 @@ impl Game<'_> { self.draw_item_model(batch, &item, x + 8.0, 8.0); return; } - let atlas = &self.atlas; + let atlas = &self.gui.atlas; batch.sprite(atlas, "gui/sprites/toast/advancement", x, 0.0); let (key, color) = match frame { Frame::Task => ("advancements.toast.task", 0xFF00_FFFF), @@ -788,18 +838,25 @@ pub(super) fn adv_name(a: &adv::Advancement) -> Text { Frame::Challenge => text::DARK_PURPLE, _ => text::GREEN, }; - let tooltip = text_of(&d.title) - .with_color(color) + // Advancement.decorateName: the title in the frame's colour (styles + // merged into it), a new line and the description. + let coloured = text_of(&d.title).with_color(color); + let mut component = coloured.component(); + if let Some(o) = component.as_object_mut() { + o.insert( + "extra".into(), + serde_json::json!(["\n", text_of(&d.description).component()]), + ); + } + let tooltip = coloured .append("\n") - .append(text_of(&d.description)); + .append(text_of(&d.description)) + .with_component(component); let title = text_of(&d.title).with_style(&Style { hover: Some(Box::new(tooltip)), ..Style::default() }); - Text::literal("[") - .append(title) - .append("]") - .with_color(color) + Text::translate("chat.square_brackets", &[title]).with_color(color) } impl Game<'_> { /// `AdvancementCommands.Action.GRANT` on one advancement (or one of @@ -820,6 +877,7 @@ impl Game<'_> { let was = self.adv.progress.done(a); if self.adv.progress.grant(&a.id, c, &date) { changed = true; + self.progress_changed(index); if !was && self.adv.progress.done(a) { self.award(index); } @@ -831,6 +889,7 @@ impl Game<'_> { pub(super) fn revoke_advancement(&mut self, index: usize, criterion: Option<&str>) -> bool { let tree = self.adv.tree.clone(); let a = &tree.list[index]; + self.progress_changed(index); match criterion { Some(c) => self.adv.progress.revoke(&a.id, c), None => { @@ -843,6 +902,110 @@ impl Game<'_> { } } } +impl Game<'_> { + /// `ClientAdvancements.update` (ClientboundUpdateAdvancementsPacket): + /// the server's advancements replace or join the tree, each changed one + /// takes the progress sent, and one just done shows its toast. `icons` + /// are the added advancements' icon items by name. + pub(super) fn remote_advancements( + &mut self, + p: horizon_protocol::packets::play::UpdateAdvancements, + icons: Vec, + ) { + use horizon_client::scoreboard::tag_to_json; + let mut list: Vec = Vec::new(); + let mut positions: Vec<(f32, f32)> = Vec::new(); + if !p.reset { + let old = self.adv.positions.take().unwrap_or_default(); + for (i, a) in self.adv.tree.list.iter().enumerate() { + if !p.removed.contains(&a.id) { + list.push(a.clone()); + positions.push(old.get(i).copied().unwrap_or((0.0, 0.0))); + } + } + } else { + self.adv.progress = Progress::default(); + } + for id in &p.removed { + self.adv.progress.criteria.remove(id); + } + for (a, icon) in p.added.into_iter().zip(icons) { + list.retain(|o| o.id != a.id); + let display = a.display.map(|d| adv::Display { + icon, + title: tag_to_json(&d.title), + description: tag_to_json(&d.description), + frame: match d.frame { + 1 => Frame::Challenge, + 2 => Frame::Goal, + _ => Frame::Task, + }, + background: d.background, + show_toast: d.show_toast, + announce: false, + hidden: d.hidden, + }); + let mut criteria: Vec = Vec::new(); + for name in a.requirements.iter().flatten() { + if !criteria.iter().any(|c| &c.name == name) { + criteria.push(adv::Criterion { + name: name.clone(), + trigger: String::new(), + conditions: Value::Null, + }); + } + } + list.push(adv::Advancement { + id: a.id, + parent: a.parent, + display, + criteria, + requirements: a.requirements, + rewards: Default::default(), + }); + positions.push((a.x, a.y)); + } + let mut tree = Tree { + list, + ..Tree::default() + }; + for (i, a) in tree.list.iter().enumerate() { + tree.by_id.insert(a.id.clone(), i); + } + self.ui.registries.advancements = tree.list.iter().map(|a| a.id.clone()).collect(); + self.adv.tree = Arc::new(tree); + self.adv.positions = Some(positions); + let tree = self.adv.tree.clone(); + for (id, criteria) in p.progress { + let Some(&i) = tree.by_id.get(&id) else { + continue; + }; + // The client only shows whether a criterion is obtained, not when. + let obtained: BTreeMap = criteria + .into_iter() + .filter(|(_, at)| at.is_some()) + .map(|(name, _)| (name, String::new())) + .collect(); + self.adv.progress.criteria.insert(id, obtained); + let a = &tree.list[i]; + if !p.reset + && p.show + && self.adv.progress.done(a) + && let Some(d) = a.display.as_ref().filter(|d| d.show_toast) + { + self.adv.toasts.push_back(Toast { + icon: d.icon.clone(), + frame: d.frame, + title: text_of(&d.title), + slide: Slide::default(), + recipes: Vec::new(), + changed: false, + last_changed: 0, + }); + } + } + } +} /// A text component from advancement JSON (`translate` or `text`). pub(super) fn text_of(v: &Value) -> Text { if let Some(key) = v["translate"].as_str() { diff --git a/crates/horizon-client/src/game/advancements_screen.rs b/crates/horizon-client/src/game/advancements_screen.rs index 5e1c0343b..211bab362 100644 --- a/crates/horizon-client/src/game/advancements_screen.rs +++ b/crates/horizon-client/src/game/advancements_screen.rs @@ -163,11 +163,18 @@ impl Game<'_> { } let tree = self.adv.tree.clone(); let kids = tree.children(); - let pos = tree - .layout() - .into_iter() - .map(|p| p.map(|(x, y)| (x * 28, (y * 27.0).floor() as i32))) - .collect(); + // A server's advancements come placed (`DisplayInfo` x, y). + let pos = match &self.adv.positions { + Some(server) => server + .iter() + .map(|&(x, y)| Some(((x * 28.0).floor() as i32, (y * 27.0).floor() as i32))) + .collect(), + None => tree + .layout() + .into_iter() + .map(|p| p.map(|(x, y)| (x * 28, (y * 27.0).floor() as i32))) + .collect(), + }; self.adv_ui.cache = Some(Cache { tree, kids, pos }); } fn adv_done(&self, i: usize) -> bool { @@ -184,7 +191,12 @@ impl Game<'_> { }; }; let tree = &c.tree; - let visible = tree.visible(&c.kids, |i| self.adv.progress.done(&tree.list[i])); + // A server sends only what the player may see. + let visible = if self.adv.positions.is_some() { + vec![true; tree.list.len()] + } else { + tree.visible(&c.kids, |i| self.adv.progress.done(&tree.list[i])) + }; // AdvancementTab.create: a displayed root with a background. let mut tabs: Vec = (0..tree.list.len()) .filter(|&i| { @@ -257,7 +269,7 @@ impl Game<'_> { } /// `AdvancementWidget`'s title lines, description lines and width. fn adv_widget_text(&self, i: usize) -> (Vec, Vec, f32) { - let font = &self.atlas.font; + let font = &self.gui.atlas.font; let a = &self.adv.tree.list[i]; let Some(d) = &a.display else { return (Vec::new(), Vec::new(), 0.0); @@ -320,7 +332,7 @@ impl Game<'_> { let (sx, sy) = self.adv_ui.scroll.unwrap_or_default(); let (sx, sy) = (sx.floor() as i32, sy.floor() as i32); self.adv_tick_hover(&view, sx, sy, left, top); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; ui::menu_background(batch, atlas, 0.0, 0.0, w, h, true); let title = Text::translate("gui.advancements", &[]); let tw = atlas.font.width_text(&title); @@ -432,7 +444,7 @@ impl Game<'_> { } let (x, y) = pos(i); let (x, y) = ((ox + x) as f32, (oy + y) as f32); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; batch.sprite_scaled( atlas, frame_sprite(d.frame, done), @@ -449,7 +461,7 @@ impl Game<'_> { } } // The window, tabs and title. - let atlas = &self.atlas; + let atlas = &self.gui.atlas; batch.sprite_region( atlas, "gui/advancements/window", @@ -463,7 +475,7 @@ impl Game<'_> { let (x, y) = tab_pos(ty, i); let sprite = tab_sprite(ty, i, ty.3, Some(t) == tab); batch.sprite_scaled( - &self.atlas, + &self.gui.atlas, &sprite, left + x, top + y, @@ -482,7 +494,7 @@ impl Game<'_> { } } } - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let title = tab.and_then(|t| tree.list[t].display.as_ref()).map_or_else( || Text::translate("gui.advancements", &[]), |d| text_of(&d.title), @@ -497,7 +509,7 @@ impl Game<'_> { } } // Done. - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let (bx, by) = ((w / 2.0 - 100.0).floor(), h - FOOTER + 6.0); let hover = ui::hovered(self.cursor, bx, by, 200.0, 20.0); ui::button( @@ -520,7 +532,7 @@ impl Game<'_> { let (cx, cy) = self.cursor; if cx > x && cx < x + ty.1 && cy > y && cy < y + ty.2 { if let Some(d) = &tree.list[t].display { - ui::tooltip(batch, &self.atlas, &[text_of(&d.title)], self.cursor); + ui::tooltip(batch, &self.gui.atlas, &[text_of(&d.title)], self.cursor); } break; } @@ -604,7 +616,7 @@ impl Game<'_> { ], ) }); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let progress_w = progress.as_ref().map_or(0.0, |t| atlas.font.width_text(t)) as i32; let top_side = title_bottom + desc_h >= INSIDE_H; let amount = if a.criteria.is_empty() || total == 0 { diff --git a/crates/horizon-client/src/game/agent.rs b/crates/horizon-client/src/game/agent.rs index 48b24bbb3..5d7d294dd 100644 --- a/crates/horizon-client/src/game/agent.rs +++ b/crates/horizon-client/src/game/agent.rs @@ -4,7 +4,13 @@ //! take time run as a task advanced every frame and reply when they finish or //! time out, so `horizon-ctl break 10 64 3` returns what the block dropped. use super::{Game, Screen}; -use horizon_client::{Result, block_entity_anim::Ticker, items::ItemStack, widgets}; +use horizon_client::{ + Result, + block_entity_anim::Ticker, + chunk::{MIN_Y, SECTIONS}, + items::ItemStack, + widgets, +}; use horizon_core::geometry::Vec3; use horizon_world::registry::BlockStateId; use serde_json::{Value, json}; @@ -27,6 +33,7 @@ pub(super) const HELP: &[&str] = &[ "entities [RADIUS] [TYPE] mobs, dropped items and orbs near the player, nearest first", "block X Y Z the block state at a position", "blocks X1 Y1 Z1 X2 Y2 Z2 the non-air blocks of a box (at most 32768 blocks)", + "light X1 Y1 Z1 X2 Y2 Z2 the stored sky and block light of a box (at most 32768 blocks)", "find-block BLOCK [RADIUS] the nearest loaded blocks of a kind (default radius 32)", "inventory hotbar, main inventory, armor, offhand and carried stack", "screen the open screen or menu, its slots and its buttons", @@ -318,7 +325,7 @@ impl Game<'_> { let state = self.world.block(pos[0], pos[1], pos[2]); let mut v = json!({ "pos": pos, - "block": &*self.res.facts.get(state).name, + "block": &*self.res.facts().get(state).name, "state": self.state_string(state), "loaded": self.world.chunks.contains_key(&(pos[0] >> 4, pos[2] >> 4)), }); @@ -466,9 +473,18 @@ impl Game<'_> { }), "dimension": self.dim.id(), "gamemode": format!("{:?}", self.mode).to_lowercase(), + // The bed (its head) the player sleeps in. + "sleeping": self.sleeping, + // The item in use: its slot and for how many ticks. + "using": self.using.map(|(slot, ticks)| json!({ "slot": slot, "ticks": ticks })), + // The arm swings started (LivingEntity.swing). + "swings": self.swings_started, "time": { "day_time": self.world.day_time, "day": self.world.day_time.div_euclid(24000), "time_of_day": self.world.day_time.rem_euclid(24000) }, "weather": { "raining": self.world.weather.raining, "thundering": self.world.weather.thundering }, "paused": self.pause, + // Mouse movement turns the camera (`apply_mouse_look`): the real + // pointer is grabbed, or an agent's `mouse-move` stands in for it. + "pointer_grabbed": self.captured || self.agent_pointer, "player": player, "selected": self.selected + 1, "held": self.held().map(|s| self.stack_value(s)), @@ -565,7 +581,7 @@ impl Game<'_> { push( [m.position.x, m.position.y, m.position.z], m.kind.id.as_ref(), - json!({ "id": m.id, "kind": "mob", "health": m.health, "baby": m.is_baby() }), + json!({ "id": m.id, "kind": "mob", "health": m.health, "baby": m.is_baby(), "hurt_time": m.hurt_time }), ); } for item in &self.dropped { @@ -613,6 +629,9 @@ impl Game<'_> { "screen": self.screen_label(), "buttons": buttons, }); + if let Some(chat) = &self.ui.chat_screen { + v["chat_input"] = json!(chat.input.value); + } if self.title.is_some() { v["menu"] = self.title_value(); } @@ -631,6 +650,15 @@ impl Game<'_> { v["craft_size"] = json!(self.craft_size); v["station"] = json!(self.station.as_ref().map(|s| format!("{:?}", s.kind))); v["container"] = json!(self.container); + // The recipe book's buttons (GUI pixels): the book button, then + // the recipe buttons of the page, page arrows, filter and tabs. + let book = self.recipe_book_targets(); + if !book.is_empty() { + v["recipe_book"] = json!({ + "targets": book.iter().map(|t| t.rect).collect::>(), + "known": self.recipe_book_known(), + }); + } v["slot_ids"] = json!( "0-8 hotbar, 9-35 inventory, 100+ crafting grid (row-major), 200 crafting result, 300+ container, 500-503 armor, 504 offhand, 600+ station" ); @@ -651,17 +679,25 @@ impl Game<'_> { /// Whether a block stops movement, for the path finder. fn blocks_motion(&self, pos: [i32; 3]) -> bool { - let f = self.res.facts.get(self.world.block(pos[0], pos[1], pos[2])); + let f = self + .res + .facts() + .get(self.world.block(pos[0], pos[1], pos[2])); f.full_collision || f.legacy_solid } fn walkable(&self, n: Node) -> bool { let below = self.world.block(n[0], n[1] - 1, n[2]); let lava = |p: [i32; 3]| { - &*self.res.facts.get(self.world.block(p[0], p[1], p[2])).name == "minecraft:lava" + &*self + .res + .facts() + .get(self.world.block(p[0], p[1], p[2])) + .name + == "minecraft:lava" }; self.world.chunks.contains_key(&(n[0] >> 4, n[2] >> 4)) && (self.blocks_motion([n[0], n[1] - 1, n[2]]) - || self.res.facts.get(below).fluid != horizon_client::blocks::Fluid::None) + || self.res.facts().get(below).fluid != horizon_client::blocks::Fluid::None) && !self.blocks_motion(n) && !self.blocks_motion([n[0], n[1] + 1, n[2]]) && !lava(n) @@ -722,7 +758,11 @@ impl Game<'_> { if !clear || !self.walkable(m) { continue; } - let water = self.res.facts.get(self.world.block(m[0], m[1], m[2])).fluid + let water = self + .res + .facts() + .get(self.world.block(m[0], m[1], m[2])) + .fluid != horizon_client::blocks::Fluid::None; let step = 10 + if dy != 0 { 5 } else { 0 } + if water { 30 } else { 0 }; let c = base + step; @@ -932,10 +972,10 @@ impl Game<'_> { for z in lo[2]..=hi[2] { for x in lo[0]..=hi[0] { let state = self.world.block(x, y, z); - if self.res.facts.is_air(state) { + if self.res.facts().is_air(state) { continue; } - let name = self.res.facts.get(state).name.to_string(); + let name = self.res.facts().get(state).name.to_string(); *census.entry(name).or_insert(0) += 1; list.push(json!([x, y, z, self.state_string(state)])); } @@ -943,6 +983,35 @@ impl Game<'_> { } ok(json!({ "from": lo, "to": hi, "counts": census, "blocks": list })) } + "light" => { + self.need_world()?; + let (p, q) = (a.block_pos(0)?, a.block_pos(3)?); + let lo = [0, 1, 2].map(|i| p[i].min(q[i])); + let hi = [0, 1, 2].map(|i| p[i].max(q[i])); + let volume: i64 = (0..3).map(|i| i64::from(hi[i] - lo[i] + 1)).product(); + if volume > 32768 { + return Err(format!("{volume} blocks; at most 32768 per request").into()); + } + // One hex digit per block, x fastest, then z, then y; `.` + // outside the loaded chunks. + let (mut sky, mut block) = (String::new(), String::new()); + for y in lo[1]..=hi[1] { + for z in lo[2]..=hi[2] { + for x in lo[0]..=hi[0] { + let packed = self + .world + .chunks + .get(&(x >> 4, z >> 4)) + .filter(|_| (MIN_Y..MIN_Y + SECTIONS as i32 * 16).contains(&y)) + .map(|c| c.light(x & 15, y, z & 15)); + let digit = |v: u8| char::from_digit(u32::from(v), 16).unwrap_or('.'); + sky.push(packed.map_or('.', |v| digit(v >> 4))); + block.push(packed.map_or('.', |v| digit(v & 15))); + } + } + } + ok(json!({ "from": lo, "to": hi, "sky": sky, "block": block })) + } "find-block" => { self.need_world()?; let want = namespaced(a.get(0, "BLOCK")?); @@ -958,7 +1027,7 @@ impl Game<'_> { for z in c[2] - radius..=c[2] + radius { for x in c[0] - radius..=c[0] + radius { let state = self.world.block(x, y, z); - if *self.res.facts.get(state).name == *want { + if *self.res.facts().get(state).name == *want { let d = dist(eye, [x, y, z].map(|v| f64::from(v) + 0.5)); found.push((d, [x, y, z])); } @@ -1166,7 +1235,7 @@ impl Game<'_> { self.playing()?; let pos = a.block_pos(0)?; let before = self.world.block(pos[0], pos[1], pos[2]); - if self.res.facts.is_air(before) { + if self.res.facts().is_air(before) { return Err(format!("there is no block at {pos:?}").into()); } self.aim_block(pos, None)?; @@ -1195,24 +1264,24 @@ impl Game<'_> { .ok_or("nothing in hand to place; select an item")?; let held = held.item.clone(); let before = self.world.block(pos[0], pos[1], pos[2]); - if !self.res.facts.get(before).replaceable && !self.res.facts.is_air(before) { + if !self.res.facts().get(before).replaceable && !self.res.facts().is_air(before) { return Err(format!( "{pos:?} holds {}; break it first", - self.res.facts.get(before).name + self.res.facts().get(before).name ) .into()); } // A replaceable block there (grass, snow) is clicked itself, as // a player would; otherwise the face of a neighbour touching it. - let mut aimed = !self.res.facts.is_air(before) - && !self.res.facts.get(before).outline.is_empty() + let mut aimed = !self.res.facts().is_air(before) + && !self.res.facts().get(before).outline.is_empty() && self.aim_block(pos, None).is_ok(); for (_, d) in FACES.iter().filter(|_| !aimed) { let d = *d; let support = add(pos, d); let state = self.world.block(support[0], support[1], support[2]); - let f = self.res.facts.get(state); - if self.res.facts.is_air(state) + let f = self.res.facts().get(state); + if self.res.facts().is_air(state) || f.replaceable || f.fluid != horizon_client::blocks::Fluid::None && !f.waterlogged { @@ -1479,6 +1548,13 @@ impl Game<'_> { return Err("command TEXT".into()); } let command = format!("/{text}"); + // Single player's console is its server thread's. + if let Some(server) = &self.integrated { + let chat = server + .call(move |g| g.owner_console_command(&command)) + .ok_or("the integrated server stopped")?; + return ok(json!({ "ok": true, "chat": chat })); + } if self.remote.is_some() { self.send_chat(&command); return ok(json!({ "ok": true })); @@ -1878,7 +1954,10 @@ impl Game<'_> { // Step up onto the next block, or out of water. let in_water = { let b = p.map(|v| v.floor() as i32); - self.res.facts.get(self.world.block(b[0], b[1], b[2])).fluid + self.res + .facts() + .get(self.world.block(b[0], b[1], b[2])) + .fluid != horizon_client::blocks::Fluid::None }; if aim[1] > p[1] + 0.5 && horizontal(p, aim) < 1.6 && on_ground { diff --git a/crates/horizon-client/src/game/allays.rs b/crates/horizon-client/src/game/allays.rs index a2a5842b5..52dfa7e1c 100644 --- a/crates/horizon-client/src/game/allays.rs +++ b/crates/horizon-client/src/game/allays.rs @@ -29,7 +29,11 @@ impl Game<'_> { continue; }; let carried = self.mobs[i].extra.ints[1]; - let max = self.res.items.get(&held).map_or(64, |i| i.max_stack as i32); + let max = self + .res + .items() + .get(&held) + .map_or(64, |i| i.max_stack as i32); // GoToWantedItem / wantsToPickUp: the same item within 32. let wanted = if griefing && carried < max && self.mobs[i].extra.ints[3] <= 0 { self.dropped diff --git a/crates/horizon-client/src/game/armor_stands.rs b/crates/horizon-client/src/game/armor_stands.rs index 03a60faca..58cd27494 100644 --- a/crates/horizon-client/src/game/armor_stands.rs +++ b/crates/horizon-client/src/game/armor_stands.rs @@ -111,7 +111,12 @@ impl Game<'_> { let Some(item) = item else { continue; }; - let Some(asset) = self.res.items.get(item).and_then(|i| i.equip_asset.clone()) else { + let Some(asset) = self + .res + .items() + .get(item) + .and_then(|i| i.equip_asset.clone()) + else { continue; }; let kind = if layer == "leggings" { @@ -124,7 +129,7 @@ impl Game<'_> { continue; }; let (Some(r), Some(part)) = ( - self.atlas.sprites.get(texture.as_str()), + self.gui.atlas.sprites.get(texture.as_str()), assets.player.get(layer), ) else { continue; @@ -144,7 +149,7 @@ impl Game<'_> { // BlockPlaceContext: a replaceable clicked block is replaced. let clicked = self .res - .facts + .facts() .get(self.world.block(hit.pos[0], hit.pos[1], hit.pos[2])); let pos = if clicked.replaceable { hit.pos @@ -230,7 +235,7 @@ impl Game<'_> { fn equipment_slot(&self, stack: &ItemStack) -> usize { match self .res - .items + .items() .get(&stack.item) .and_then(|i| i.equip_slot.as_deref()) { @@ -377,9 +382,14 @@ impl Game<'_> { subject: None, }; let mut dead = Vec::new(); + // ServerLevel.tick: only entities in entity-ticking chunks tick. + let ticking = self.entity_ticking_area(); for (i, a) in self.armor_stands.iter_mut().enumerate() { + if !ticking(a.position.x.floor() as i32, a.position.z.floor() as i32) { + continue; + } a.tick(&mut scene); - if !a.environment_tick(&self.world, &self.res.facts, self.pack.assets.registry) { + if !a.environment_tick(&self.world, self.res.facts(), self.pack.assets.registry) { dead.push(i); } } @@ -403,7 +413,7 @@ impl Game<'_> { out: &mut Vec, ) { let assets = &self.armor_stand_assets; - let sprites = &self.atlas.sprites; + let sprites = &self.gui.atlas.sprites; let uv_of = |name: &str| sprites.get(name).map(|r| [r.u0, r.v0, r.u1, r.v1]); let Some(wood) = uv_of("entity/armorstand/armorstand") else { return; @@ -413,6 +423,32 @@ impl Game<'_> { if a.invisible { continue; } + // ArmorStand.shouldRenderAtSqrDistance: 256 times the box's mean + // edge (4 for a marker's empty box), whatever the entity distance. + // LivingEntityRenderer grows the culling box by 0.5 for a dragon head. + let (width, height) = match (a.marker, a.small) { + (true, _) => (0.0, 0.0), + (false, true) => (0.25, 0.9875), + (false, false) => (0.5, 1.975), + }; + let edge = (width + height + width) / 3.0 * 4.0; + let range = if edge == 0.0 { 4.0 } else { edge } * 64.0; + let dragon = a.equipment[5] + .as_ref() + .is_some_and(|i| &*i.item == "minecraft:dragon_head"); + let position = super::entity_culling::at(a.position); + if !self.standing_entity_in_view( + camera, + position, + position, + alpha, + width, + height, + range, + if dragon { 0.5 } else { 0.0 }, + ) { + continue; + } let Some(model) = (if a.small { &assets.small } else { &assets.main }) else { continue; }; @@ -490,7 +526,7 @@ impl Game<'_> { }; let Some(asset) = self .res - .items + .items() .get(&stack.item) .and_then(|i| i.equip_asset.clone()) else { @@ -542,7 +578,7 @@ impl Game<'_> { /// `Equippable.swapWithEquipmentSlot`: right-clicking armor swaps it into /// its slot (the worn piece comes back to the hand). pub(super) fn equip_from_hand(&mut self, stack: &ItemStack) -> bool { - let Some(info) = self.res.items.get(&stack.item) else { + let Some(info) = self.res.items().get(&stack.item) else { return false; }; let index = match info.equip_slot.as_deref() { @@ -673,7 +709,7 @@ impl Game<'_> { &serde_json::Value::Null, false, pose, - &self.atlas.sprites, + &self.gui.atlas.sprites, &self.item_host(), &mut sink, ); @@ -683,7 +719,7 @@ impl Game<'_> { if let Some(stack) = &a.equipment[5] && self .res - .items + .items() .get(&stack.item) .is_none_or(|i| i.equip_asset.is_none()) && let Some(head) = part_matrix(model, base, poses, "head", 0.0) @@ -700,7 +736,7 @@ impl Game<'_> { } } /// `AbstractSkullBlock.getType` of a skull item. -fn skull_kind(item: &str) -> Option<&'static str> { +pub(super) fn skull_kind(item: &str) -> Option<&'static str> { Some(match item { "minecraft:skeleton_skull" => "skeleton", "minecraft:wither_skeleton_skull" => "wither_skeleton", diff --git a/crates/horizon-client/src/game/backdrop.rs b/crates/horizon-client/src/game/backdrop.rs index 149cddefa..3a5ec81d6 100644 --- a/crates/horizon-client/src/game/backdrop.rs +++ b/crates/horizon-client/src/game/backdrop.rs @@ -71,8 +71,10 @@ impl Game<'_> { } /// Called every frame while the title is up. pub(super) fn update_backdrop(&mut self) -> Result<()> { - let wanted = self.title.is_some() + let wanted = self.pending_entry.is_none() + && self.title.is_some() && horizon_client::ui_theme::horizon() + && self.world_renderer_ready && self.renderer.is_some() && self.options.frames.is_none(); if !wanted { @@ -127,6 +129,10 @@ impl Game<'_> { .is_some_and(|r| r.section_count() > 40) { self.backdrop.ready = Some(Instant::now()); + log::info!( + "Horizon: title backdrop ready; world rest ready: {}", + self.res.rest.get().is_some() + ); } Ok(()) } @@ -139,10 +145,14 @@ impl Game<'_> { } } fn surface_y(&self, x: i32, z: i32) -> Option { - let air = self.res.facts.air; + let air = self.res.facts().air; (-60..320).rev().find(|&y| self.world.block(x, y, z) != air) } fn start_backdrop(&mut self) -> Result<()> { + log::info!( + "Horizon: starting title backdrop after {} ms", + self.started.elapsed().as_millis() + ); let seed = seed(); let root = std::env::current_dir()? .join("saves") @@ -155,7 +165,7 @@ impl Game<'_> { VanillaTerrain::new( &self.options.resources.join("worldgen"), pack.assets.registry, - &res.biomes, + res.biomes(), seed, ) .map_err(|e| format!("title backdrop: {e}"))?, @@ -165,10 +175,10 @@ impl Game<'_> { inner: inner.clone(), save: save.clone(), registry: pack.assets.clone(), - biomes: res.biomes.clone(), + biomes: res.biomes().clone(), }), pack.context.clone(), - res.threads.min(6), + res.threads().min(6), ); streaming.radius = self.settings.render_distance.clamp(4, 10); streaming.biome_blend = self.settings.biome_blend; @@ -179,7 +189,7 @@ impl Game<'_> { streaming.paused = true; self.streaming = streaming; self.save = save; - let mut world = World::new(self.res.facts.air, seed); + let mut world = World::new(self.res.facts().air, seed); world.day_time = DAY_TIME; world.game_time = 6000; // Light snow: the weather renders as snow in frozen biomes. @@ -237,7 +247,7 @@ impl Game<'_> { } fn stop_backdrop(&mut self) { self.streaming.paused = true; - self.world = World::new(self.res.facts.air, 0); + self.world = World::new(self.res.facts().air, 0); if let Some(r) = self.renderer.as_mut() { r.clear_sections(); } diff --git a/crates/horizon-client/src/game/banners.rs b/crates/horizon-client/src/game/banners.rs index 6875fcb7f..05beda3f8 100644 --- a/crates/horizon-client/src/game/banners.rs +++ b/crates/horizon-client/src/game/banners.rs @@ -14,7 +14,7 @@ impl Game<'_> { else { return Vec::new(); }; - layer_textures(&self.res.stations.banners, &b.layers) + layer_textures(&self.res.stations().banners, &b.layers) } } diff --git a/crates/horizon-client/src/game/beacon.rs b/crates/horizon-client/src/game/beacon.rs index 15a7de79d..79cf14541 100644 --- a/crates/horizon-client/src/game/beacon.rs +++ b/crates/horizon-client/src/game/beacon.rs @@ -99,30 +99,16 @@ impl Game<'_> { } /// `getHeight(WORLD_SURFACE)`: above the highest non-air block. pub(super) fn world_surface(&self, x: i32, z: i32) -> i32 { - let (min, max) = (super::MIN_Y, super::MIN_Y + super::HEIGHT); - (min..max) - .rev() - .find(|&y| !self.res.facts.is_air(self.world.block(x, y, z))) - .map_or(min, |y| y + 1) + self.world + .world_surface(x, z, |id| self.res.facts().is_air(id)) } - /// `BeaconBlockEntity.tick` for every beacon. - pub(super) fn tick_beacons(&mut self) { - let keys: Vec<(i32, i32, i32)> = self - .world - .block_entities - .iter() - .filter(|(_, e)| matches!(e, BlockEntity::Beacon(_))) - .map(|(k, _)| *k) - .collect(); - for key in keys { - let Some(BlockEntity::Beacon(mut b)) = self.world.block_entities.remove(&key) else { - continue; - }; - self.tick_beacon(key, &mut b); - self.world - .block_entities - .insert(key, BlockEntity::Beacon(b)); - } + /// `BeaconBlockEntity.tick` of the beacon at `key`. + pub(super) fn tick_beacon_at(&mut self, key: (i32, i32, i32)) { + let Some(BlockEntity::Beacon(mut b)) = self.world.block_entities.take(&key) else { + return; + }; + self.tick_beacon(key, &mut b); + self.world.block_entities.put(key, BlockEntity::Beacon(b)); } fn tick_beacon(&mut self, (x, y, z): (i32, i32, i32), b: &mut Beacon) { let mut check = if b.last_check_y < y { @@ -136,7 +122,7 @@ impl Game<'_> { let mut i = 0; while i < 10 && check <= surface { let state = self.world.block(x, check, z); - let f = self.res.facts.get(state); + let f = self.res.facts().get(state); if let Some(color) = self.beam_color(&f.name) { if b.checking.len() <= 1 { b.checking.push((color, 1)); @@ -197,7 +183,7 @@ impl Game<'_> { fn beacon_base(&self, x: i32, y: i32, z: i32) -> i32 { let base = self .res - .items + .items() .block_tags .get("minecraft:beacon_base_blocks"); let mut levels = 0; @@ -208,7 +194,7 @@ impl Game<'_> { } let ok = (x - step..=x + step).all(|lx| { (z - step..=z + step).all(|lz| { - let name = &self.res.facts.get(self.world.block(lx, ly, lz)).name; + let name = &self.res.facts().get(self.world.block(lx, ly, lz)).name; base.is_some_and(|t| t.contains(&**name)) }) }); @@ -220,7 +206,11 @@ impl Game<'_> { levels } /// `applyEffects`: the powers for players within `levels * 10 + 10` (any height above). + /// `BeaconBlockEntity.applyEffects`: every player in range. fn beacon_effects(&mut self, p: [i32; 3], b: &Beacon) { + self.for_each_player(|g| g.beacon_effects_on_player(p, b)); + } + fn beacon_effects_on_player(&mut self, p: [i32; 3], b: &Beacon) { let Some(primary) = b.primary.as_deref().and_then(effect_id) else { return; }; @@ -385,7 +375,7 @@ impl Game<'_> { let levels = self.beacon_levels(); let sel = self.station.as_ref().map_or([None; 2], |s| s.beacon); let paid = self.station.as_ref().is_some_and(|s| s.inputs[0].is_some()); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; batch.fill( atlas, 0.0, @@ -406,7 +396,7 @@ impl Game<'_> { ("block.minecraft.beacon.secondary", "Secondary Power", 169.0), ] { let text = self.translate(key, fallback); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let w = HudBatch::text_width(atlas, &text); batch.text( atlas, @@ -421,7 +411,7 @@ impl Game<'_> { let x = [20.0, 41.0, 63.0, 86.0, 108.0][i]; self.draw_item(batch, &ItemStack::new(item, 1), bx + x, by + 109.0); } - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let mut tooltip = None; let button = |batch: &mut HudBatch, x: f32, y: f32, active: bool, selected: bool, icon: &str| { @@ -503,7 +493,7 @@ impl Game<'_> { if let Some(slot) = self.slot_at(batch, self.cursor.0, self.cursor.1) { let (sx, sy) = self.slot_position(slot); batch.fill( - &self.atlas, + &self.gui.atlas, ox + sx, oy + sy, 16.0, @@ -524,7 +514,13 @@ impl Game<'_> { } /// `BeaconRenderer`: each section's rotating solid beam and its glow. pub(super) fn beacon_geometry(&self, camera: glam::DVec3, alpha: f64, out: &mut Vec) { - let Some(s) = self.atlas.sprites.get("entity/beacon/beacon_beam").copied() else { + let Some(s) = self + .gui + .atlas + .sprites + .get("entity/beacon/beacon_beam") + .copied() + else { return; }; let time = (self.world.game_time.rem_euclid(40)) as f32 + alpha as f32; diff --git a/crates/horizon-client/src/game/beehives.rs b/crates/horizon-client/src/game/beehives.rs index 0520e4367..b7f7efb1a 100644 --- a/crates/horizon-client/src/game/beehives.rs +++ b/crates/horizon-client/src/game/beehives.rs @@ -41,7 +41,7 @@ impl HiveHost for GameHive<'_, '_> { if !self .game .res - .mob_data + .mob_data() .entity_has("minecraft:beehive_inhabitors", &kind) { return None; @@ -72,10 +72,10 @@ impl Game<'_> { /// The hive's honey level and front, from its block state. pub(super) fn hive_state(&self, at: [i32; 3]) -> Option<(Option, [i32; 2])> { let state = self.world.block(at[0], at[1], at[2]); - let name = &self.res.facts.get(state).name; + let name = &self.res.facts().get(state).name; if !self .res - .items + .items() .block_tags .get("minecraft:beehives") .is_some_and(|t| t.contains(&**name)) @@ -87,48 +87,37 @@ impl Game<'_> { let facing = beehive::facing_step(props.get("facing").map(String::as_str)); Some((honey, facing)) } - /// `BeehiveBlockEntity.serverTick` for every hive, after an emergency - /// exit for those with fire beside them (`BeehiveBlock.updateShape`). - pub(super) fn tick_beehives(&mut self) { - let mut keys: Vec<(i32, i32, i32)> = self - .world - .block_entities - .iter() - .filter(|(_, e)| matches!(e, BlockEntity::Beehive(_))) - .map(|(k, _)| *k) - .collect(); - keys.sort_unstable(); - for key in keys { - let at = [key.0, key.1, key.2]; - let Some((honey, facing)) = self.hive_state(at) else { - continue; - }; - let Some(BlockEntity::Beehive(mut hive)) = self.world.block_entities.remove(&key) - else { - continue; - }; - let fire = [ - [1, 0, 0], - [-1, 0, 0], - [0, 1, 0], - [0, -1, 0], - [0, 0, 1], - [0, 0, -1], - ] - .iter() - .any(|d| { - let state = self.world.block(at[0] + d[0], at[1] + d[1], at[2] + d[2]); - &*self.res.facts.get(state).name == "minecraft:fire" - }); - let mut host = GameHive { game: self }; - if fire && !hive.bees.is_empty() { - hive.release_all(at, facing, honey, Release::Emergency, &mut host); - } - hive.server_tick(at, facing, honey, &mut host); - self.world - .block_entities - .insert(key, BlockEntity::Beehive(hive)); + /// `BeehiveBlockEntity.serverTick` of the hive at `key`, after an + /// emergency exit if fire is beside it (`BeehiveBlock.updateShape`). + pub(super) fn tick_beehive(&mut self, key: (i32, i32, i32)) { + let at = [key.0, key.1, key.2]; + let Some((honey, facing)) = self.hive_state(at) else { + return; + }; + let Some(BlockEntity::Beehive(mut hive)) = self.world.block_entities.take(&key) else { + return; + }; + let fire = [ + [1, 0, 0], + [-1, 0, 0], + [0, 1, 0], + [0, -1, 0], + [0, 0, 1], + [0, 0, -1], + ] + .iter() + .any(|d| { + let state = self.world.block(at[0] + d[0], at[1] + d[1], at[2] + d[2]); + &*self.res.facts().get(state).name == "minecraft:fire" + }); + let mut host = GameHive { game: self }; + if fire && !hive.bees.is_empty() { + hive.release_all(at, facing, honey, Release::Emergency, &mut host); } + hive.server_tick(at, facing, honey, &mut host); + self.world + .block_entities + .put(key, BlockEntity::Beehive(hive)); } /// `BeehiveBlockEntity.addOccupant` for a bee its `BeeEnterHiveGoal` /// sent in: off its lead, its tag stored, the bee gone. @@ -152,13 +141,13 @@ impl Game<'_> { self.mobs[i].extra.mount = None; let tag = self.mob_tree_nbt(i); let flower = bee::get(&self.mobs[i]).and_then(|b| b.flower); - let Some(BlockEntity::Beehive(mut hive)) = self.world.block_entities.remove(&key) else { + let Some(BlockEntity::Beehive(mut hive)) = self.world.block_entities.take(&key) else { return; }; let stored = hive.add_occupant(at, tag, flower, &mut GameHive { game: self }); self.world .block_entities - .insert(key, BlockEntity::Beehive(hive)); + .put(key, BlockEntity::Beehive(hive)); if stored && let Some(m) = self.mobs.iter_mut().find(|m| m.id == mob) { m.removed = true; } @@ -166,13 +155,13 @@ impl Game<'_> { /// `BeehiveBlockEntity.emptyAllLivingFromHive` with no player about. pub(super) fn empty_hive(&mut self, at: [i32; 3], facing: [i32; 2], status: Release) { let key = (at[0], at[1], at[2]); - let Some(BlockEntity::Beehive(mut hive)) = self.world.block_entities.remove(&key) else { + let Some(BlockEntity::Beehive(mut hive)) = self.world.block_entities.take(&key) else { return; }; hive.release_all(at, facing, None, status, &mut GameHive { game: self }); self.world .block_entities - .insert(key, BlockEntity::Beehive(hive)); + .put(key, BlockEntity::Beehive(hive)); } /// `BeehiveBlock.playerDestroy` without silk touch: the bees come out /// angry at the player unless smoke calms them, then diff --git a/crates/horizon-client/src/game/block_entity_anims.rs b/crates/horizon-client/src/game/block_entity_anims.rs index d377aeb45..619fcd009 100644 --- a/crates/horizon-client/src/game/block_entity_anims.rs +++ b/crates/horizon-client/src/game/block_entity_anims.rs @@ -13,7 +13,6 @@ use horizon_client::{ }; use horizon_core::geometry::{Aabb, Vec3}; use horizon_world::nbt::Tag; -use std::collections::HashMap; /// The handle of the local player among a bell's gathered entities. const PLAYER: u64 = u64::MAX; @@ -174,18 +173,27 @@ impl Game<'_> { let Some(r) = &self.renderer else { return; }; - let present: HashMap<[i32; 3], Ticker> = r - .block_entities() - .filter_map(|(p, s)| Some((*p, self.be_anims.ticker(*s)?))) - .collect(); - self.be_anims.retain_present(&present); + // Sorted by position: the order they tick in, and a quick lookup. + // Read again only when the drawn block entities changed. + let revision = r.block_entity_revision(); + if self.be_anims.present_revision != Some(revision) { + let mut present: Vec<([i32; 3], Ticker)> = r + .block_entities() + .filter_map(|(p, s)| Some((*p, self.be_anims.ticker(*s)?))) + .collect(); + present.sort_unstable_by_key(|(p, _)| *p); + self.be_anims.retain_present(&present); + self.be_anims.present = present; + self.be_anims.present_revision = Some(revision); + } + let positions = std::mem::take(&mut self.be_anims.present); let players: Vec<[f64; 3]> = match &self.player { Some(p) if !self.spectator() => vec![[p.position.x, p.position.y, p.position.z]], _ => Vec::new(), }; let raiders = self .res - .stations + .stations() .enchantments .entity_tags .get("minecraft:raiders") @@ -193,14 +201,12 @@ impl Game<'_> { .unwrap_or_default(); let frames = self .res - .items + .items() .block_tags .get("minecraft:conduit_effect_block") .cloned() .unwrap_or_default(); - let mut positions: Vec<([i32; 3], Ticker)> = present.into_iter().collect(); - positions.sort_unstable_by_key(|(p, _)| *p); - for (pos, ticker) in positions { + for &(pos, ticker) in &positions { match ticker { Ticker::Lid => self.be_anims.chests.entry(pos).or_default().tick(), Ticker::Book => { @@ -279,7 +285,7 @@ impl Game<'_> { } Ticker::Conduit => { let world = &self.world; - let facts = &self.res.facts; + let facts = self.res.facts(); let registry = self.pack.assets.registry; let is_water = |p: [i32; 3]| { facts.get(world.block(p[0], p[1], p[2])).fluid @@ -322,6 +328,7 @@ impl Game<'_> { } } } + self.be_anims.present = positions; } /// `EndGatewayBlock.entityInside` on the server: whether the gateway at /// `pos` let the entity in (`triggerCooldown`, with its block event). @@ -365,7 +372,7 @@ impl Game<'_> { /// pot's saved `sherds` (back, left, right, front). pub(super) fn block_anim(&self, pos: [i32; 3], partial: f32) -> BlockAnim { let state = self.world.block(pos[0], pos[1], pos[2]); - let sherds = if &*self.res.facts.get(state).name == "minecraft:decorated_pot" { + let sherds = if &*self.res.facts().get(state).name == "minecraft:decorated_pot" { let sherds = self.saved_tag(pos).and_then(|t| t.get("sherds")); ["back", "left", "right", "front"].map(|side| match sherds.and_then(|s| s.get(side)) { // An item id, or the item stack the processed templates hold. diff --git a/crates/horizon-client/src/game/block_entity_ticks.rs b/crates/horizon-client/src/game/block_entity_ticks.rs new file mode 100644 index 000000000..bb1cadd15 --- /dev/null +++ b/crates/horizon-client/src/game/block_entity_ticks.rs @@ -0,0 +1,131 @@ +//! `Level.tickBlockEntities` for the block entities ticked together here +//! (hoppers, crafters, jukeboxes, beacons, creaking hearts, spawners, trial +//! spawners, vaults, beehives and furnaces): only those whose state has a +//! ticker, in vanilla's list order (`horizon_client::block_entities`), in +//! chunks that tick blocks. Sculk, brewing stands and pistons tick in their +//! own passes. +use super::Game; +use horizon_client::{ + Result, + block_entities::{Pos, Ticking}, + containers::{BlockEntity, FurnaceKind, HOPPER_SLOTS}, +}; +use horizon_world::registry::BlockStateId; + +impl Game<'_> { + pub(super) fn tick_block_entities(&mut self) -> Result<()> { + self.tick_mount_menu(); + self.load_entity_types(); + if !self.world.block_entities.has_states() { + let states = self.ticking_states(); + self.world.block_entities.set_states(states); + } + self.forget_gone_hoppers(); + let ticking = self.block_ticking_area(); + let mut spawners = Vec::new(); + let mut trial_spawners = Vec::new(); + for slot in self.world.block_entity_tickers() { + let pos = slot.0; + // Ticks before this one can remove it (`TickingBlockEntity.isRemoved`). + if !self.world.block_entities.is_live(slot) + || !ticking(pos.0, pos.2) + || !self.border.contains(f64::from(pos.0), f64::from(pos.2)) + { + continue; + } + match self.world.block_entities.get(&pos) { + Some(BlockEntity::Chest(items)) if items.len() == HOPPER_SLOTS => { + self.tick_hopper(pos) + } + Some(BlockEntity::Crafter(_)) => self.tick_crafter(pos), + Some(BlockEntity::Jukebox(_)) => self.tick_jukebox(pos), + Some(BlockEntity::Beacon(_)) => self.tick_beacon_at(pos), + Some(BlockEntity::CreakingHeart(_)) => self.tick_creaking_heart(pos)?, + Some(BlockEntity::Spawner(_)) => { + self.tick_spawner(pos)?; + spawners.push(pos); + } + Some(BlockEntity::TrialSpawner(_)) => { + self.tick_trial_spawner(pos)?; + trial_spawners.push(pos); + } + Some(BlockEntity::Vault(_)) => self.tick_vault(pos)?, + Some(BlockEntity::Beehive(_)) => self.tick_beehive(pos), + Some(BlockEntity::Furnace(_)) => self.tick_furnace(pos)?, + Some(BlockEntity::Brewing(_)) => self.tick_brewing_stand(pos)?, + _ => {} + } + } + self.update_song_volumes(); + self.scan_for_hearts(); + // The client ticks (particles, sounds, the turning mob) are for a + // window; a dedicated or integrated server has none. + if self.options.dedicated.is_none() { + self.client_tick_spawners(&spawners); + for key in trial_spawners { + self.client_tick_trial_spawner(key); + } + } + Ok(()) + } + /// `BlockState.getTicker` of every state, for the block entities ticked + /// here. A creaking heart keeps its slot while uprooted (see + /// `tick_creaking_heart`). + fn ticking_states(&self) -> Vec { + let registry = self.pack.assets.registry; + (0..registry.len()) + .map(|i| { + let state = BlockStateId(i as u32); + let name: &str = &self.res.facts().get(state).name; + match name { + "minecraft:hopper" + | "minecraft:crafter" + | "minecraft:beacon" + | "minecraft:creaking_heart" + | "minecraft:spawner" + | "minecraft:trial_spawner" + | "minecraft:vault" + | "minecraft:beehive" + | "minecraft:bee_nest" + | "minecraft:brewing_stand" => Ticking::Yes, + "minecraft:jukebox" => { + let record = registry + .state(state) + .and_then(|s| s.properties.get("has_record")) + .is_some_and(|v| v == "true"); + if record { Ticking::Yes } else { Ticking::Idle } + } + n if FurnaceKind::from_block(n).is_some() => Ticking::Yes, + _ => Ticking::No, + } + }) + .collect() + } + /// `ServerLevel.shouldTickBlocksAt` in a block-ticking chunk + /// (`LevelChunk.isTicking`, block coordinates), from every player's + /// tickets (`update_chunk_tickets`). + fn block_ticking_area(&self) -> impl Fn(i32, i32) -> bool + use<> { + let levels = self.tickets.levels(); + move |x, z| levels.block_ticking_at(x, z) + } + /// `AbstractFurnaceBlockEntity.serverTick` of the furnace at `pos`, and + /// its block's `lit` following it. + fn tick_furnace(&mut self, pos: Pos) -> Result<()> { + let Some(BlockEntity::Furnace(f)) = self.world.block_entities.get_mut(&pos) else { + return Ok(()); + }; + let Some(lit) = f.tick(self.res.recipes(), self.res.items(), self.res.providers()) else { + return Ok(()); + }; + let state = self.world.block(pos.0, pos.1, pos.2); + if let Some(mut s) = self.pack.assets.registry.state(state).cloned() + && s.properties.contains_key("lit") + { + s.properties.insert("lit".into(), lit.to_string()); + if let Ok(id) = self.pack.assets.registry.resolve(&s) { + self.world.set(pos.0, pos.1, pos.2, id)?; + } + } + Ok(()) + } +} diff --git a/crates/horizon-client/src/game/block_items.rs b/crates/horizon-client/src/game/block_items.rs index b8012cce1..199c39a78 100644 --- a/crates/horizon-client/src/game/block_items.rs +++ b/crates/horizon-client/src/game/block_items.rs @@ -22,7 +22,7 @@ impl Game<'_> { let entity = self.world.block_entities.get(&key)?; let block = &self .res - .facts + .facts() .get(self.world.block(key.0, key.1, key.2)) .name; let mut tag = save::encode_block_entity(key, entity, block); @@ -37,7 +37,7 @@ impl Game<'_> { pub(super) fn picked_stack(&self, pos: [i32; 3], include_data: bool) -> Option { let key = (pos[0], pos[1], pos[2]); let state = self.world.block(pos[0], pos[1], pos[2]); - let facts = self.res.facts.get(state); + let facts = self.res.facts().get(state); if &*facts.item == "minecraft:air" { return None; } diff --git a/crates/horizon-client/src/game/blockctl.rs b/crates/horizon-client/src/game/blockctl.rs index 993e0789e..719ce8dd2 100644 --- a/crates/horizon-client/src/game/blockctl.rs +++ b/crates/horizon-client/src/game/blockctl.rs @@ -47,7 +47,7 @@ impl Game<'_> { format!("{}[{}]", s.name, props.join(",")) } fn put_block(&mut self, pos: [i32; 3], state: BlockStateId) -> Result<()> { - let name = self.res.facts.get(state).name.clone(); + let name = self.res.facts().get(state).name.clone(); self.world.block_entities.remove(&(pos[0], pos[1], pos[2])); self.level_set(pos, state)?; if let Some(entity) = BlockEntity::for_block(&name) { diff --git a/crates/horizon-client/src/game/boats.rs b/crates/horizon-client/src/game/boats.rs index a5a609cac..f9740598e 100644 --- a/crates/horizon-client/src/game/boats.rs +++ b/crates/horizon-client/src/game/boats.rs @@ -2,7 +2,7 @@ //! paddling controls, collisions, riding (`positionRider`, `clampRotation`, //! dismounting), `BoatItem` placement, `VehicleEntity.hurtServer` breaking, //! saving and `AbstractBoatRenderer` with animated paddles. -use super::{Game, Keys, view_vector}; +use super::{Game, Keys, entity_culling::at, view_vector}; use horizon_client::{ blocks::Fluid, collisions::SceneCollisions, @@ -180,11 +180,11 @@ impl BoatModels { } impl Game<'_> { fn fluid_height(&self, x: i32, y: i32, z: i32) -> Option<(f32, bool)> { - let f = self.res.facts.get(self.world.block(x, y, z)); + let f = self.res.facts().get(self.world.block(x, y, z)); if f.fluid != Fluid::Water { return None; } - let above = self.res.facts.get(self.world.block(x, y + 1, z)); + let above = self.res.facts().get(self.world.block(x, y + 1, z)); // FluidState.getHeight: 1 under the same fluid, else amount / 9. let height = if above.fluid == Fluid::Water { 1.0 @@ -277,7 +277,7 @@ impl Game<'_> { && f64::from(x + 1) > lo.x && f64::from(z) < hi.z && f64::from(z + 1) > lo.z; - let f = self.res.facts.get(self.world.block(x, y0, z)); + let f = self.res.facts().get(self.world.block(x, y0, z)); if inside && (f.full_collision || f.legacy_solid) && !f.has_class("LilyPadBlock") { friction += f.friction; count += 1; @@ -443,8 +443,13 @@ impl Game<'_> { } /// Every boat but the ridden one (which ticks with its rider). pub(super) fn tick_boats(&mut self) { + // ServerLevel.tick: only entities in entity-ticking chunks tick. + let ticking = self.entity_ticking_area(); for i in 0..self.boats.len() { - if Some(self.boats[i].id) == self.riding { + let p = self.boats[i].position; + if Some(self.boats[i].id) == self.riding + || !ticking(p.x.floor() as i32, p.z.floor() as i32) + { continue; } let b = self.boats[i].clone(); @@ -495,8 +500,8 @@ impl Game<'_> { ); let top = b.bounds().max_position().y; let (bx, by, bz) = (x.floor() as i32, top.floor() as i32, z.floor() as i32); - let below = self.res.facts.get(self.world.block(bx, by - 1, bz)); - let here = self.res.facts.get(self.world.block(bx, by, bz)); + let below = self.res.facts().get(self.world.block(bx, by - 1, bz)); + let here = self.res.facts().get(self.world.block(bx, by, bz)); let target = if below.fluid != Fluid::Water && !here.full_collision && (below.full_collision || below.legacy_solid) @@ -700,8 +705,19 @@ impl Game<'_> { /// AbstractBoatRenderer. pub(super) fn boat_geometry(&self, camera: glam::DVec3, alpha: f64, out: &mut Vec) { let a = alpha as f32; + let range = super::entity_culling::default_range( + glam::DVec3::new(WIDTH, HEIGHT, WIDTH), + self.settings.entity_distance, + ); for b in &self.boats { let entity_start = out.len(); + // EntityRenderer.shouldRender. + let (previous, position) = (at(b.previous), at(b.position)); + if !self.standing_entity_in_view( + camera, previous, position, alpha, WIDTH, HEIGHT, range, 0.0, + ) { + continue; + } let models = if b.chest { &self.boat_models.chest_models } else { @@ -712,6 +728,7 @@ impl Game<'_> { }; let folder = if b.chest { "chest_boat" } else { "boat" }; let Some(uv) = self + .gui .atlas .sprites .get(&format!("entity/{folder}/{}", b.wood)) diff --git a/crates/horizon-client/src/game/books.rs b/crates/horizon-client/src/game/books.rs index 5603e8369..8745c6f35 100644 --- a/crates/horizon-client/src/game/books.rs +++ b/crates/horizon-client/src/game/books.rs @@ -45,7 +45,7 @@ pub(super) enum BookScreen { } impl Game<'_> { fn book_lines(&self, page: &str) -> Vec { - self.atlas.font.split(&Text::literal(page), 114.0) + self.gui.atlas.font.split(&Text::literal(page), 114.0) } /// `WritableBookItem.use` / `WrittenBookItem.use`. pub(super) fn use_book(&mut self, stack: &ItemStack) -> bool { @@ -242,7 +242,7 @@ impl Game<'_> { /// the page (`MultilineTextField.insertText`: line breaks kept, cut to /// the 1024 characters left, rejected when it overflows the 14 lines). fn book_shortcut(&mut self, shortcut: horizon_client::clipboard::EditShortcut) { - let font = &self.atlas.font; + let font = &self.gui.atlas.font; match self.book.as_mut() { Some(BookScreen::Edit { signing: true, @@ -397,7 +397,7 @@ impl Game<'_> { let key = (pos[0], pos[1], pos[2]); if &*self .res - .facts + .facts() .get(self.world.block(pos[0], pos[1], pos[2])) .name != "minecraft:lectern" @@ -549,7 +549,7 @@ impl Game<'_> { } pub(super) fn draw_book(&self, batch: &mut HudBatch) { let Some(book) = &self.book else { return }; - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let (w, h) = (batch.width, batch.height); let (mx, my) = self.cursor; let hover = diff --git a/crates/horizon-client/src/game/breezes.rs b/crates/horizon-client/src/game/breezes.rs index 52e3b36a9..466469110 100644 --- a/crates/horizon-client/src/game/breezes.rs +++ b/crates/horizon-client/src/game/breezes.rs @@ -40,7 +40,7 @@ impl Game<'_> { m.position.z.floor() as i32, ); let mut state = self.world.block(x, y, z); - if self.res.facts.is_air(state) { + if self.res.facts().is_air(state) { state = self.world.block(x, (m.position.y - 0.2).floor() as i32, z); } let Some((sprite, tint)) = self.particle_sprite(state) else { diff --git a/crates/horizon-client/src/game/cartography.rs b/crates/horizon-client/src/game/cartography.rs index 86d5d5395..4a5738f6e 100644 --- a/crates/horizon-client/src/game/cartography.rs +++ b/crates/horizon-client/src/game/cartography.rs @@ -89,17 +89,17 @@ impl Game<'_> { if is_locked { locked = true; if scaling || locking { - batch.sprite(&self.atlas, error, ox + 35.0, oy + 31.0); + batch.sprite(&self.gui.atlas, error, ox + 35.0, oy + 31.0); } } if scaling && scale >= 4 { locked = true; - batch.sprite(&self.atlas, error, ox + 35.0, oy + 31.0); + batch.sprite(&self.gui.atlas, error, ox + 35.0, oy + 31.0); } } let sprite = |batch: &mut HudBatch, game: &Self, name: &str, x: f32, y: f32| { batch.sprite( - &game.atlas, + &game.gui.atlas, &format!("gui/sprites/container/cartography_table/{name}"), ox + x, oy + y, diff --git a/crates/horizon-client/src/game/chat.rs b/crates/horizon-client/src/game/chat.rs index bcf6426e1..20aeeb7fd 100644 --- a/crates/horizon-client/src/game/chat.rs +++ b/crates/horizon-client/src/game/chat.rs @@ -10,6 +10,18 @@ use horizon_client::{ text::{ClickEvent, Style, Text}, }; use std::collections::BTreeMap; + +/// The tree the chat parses and completes with: a server's, once it has +/// sent one (ClientPacketListener.commands), else the game's own. +fn chat_tree<'a>( + remote: &'a Option, + mods: &'a super::mods::ModsState, +) -> &'a horizon_client::command::Dispatcher { + match remote.as_ref().and_then(|r| r.commands.as_ref()) { + Some(tree) => tree, + None => mods.dispatcher(), + } +} use winit::{ event::KeyEvent, keyboard::{KeyCode, PhysicalKey}, @@ -207,12 +219,18 @@ impl Game<'_> { .map(|a| format!("minecraft:{a}")) .collect(); let reg = &mut self.ui.registries; - let mut items: Vec = self.res.items.order.iter().map(|s| s.to_string()).collect(); + let mut items: Vec = self + .res + .items() + .order + .iter() + .map(|s| s.to_string()) + .collect(); items.sort(); reg.items = items; reg.item_tags = self .res - .items + .items() .item_tags .keys() .map(|k| horizon_client::command::args::normalize(k)) @@ -220,7 +238,7 @@ impl Game<'_> { reg.item_tags.sort(); reg.block_tags = self .res - .items + .items() .block_tags .keys() .map(|k| horizon_client::command::args::normalize(k)) @@ -247,7 +265,7 @@ impl Game<'_> { .collect(); let mut summonable: Vec = self .res - .mob_types + .mob_types() .keys() .map(|k| horizon_client::command::args::normalize(k)) .collect(); @@ -378,6 +396,10 @@ impl Game<'_> { /// The block the player is looking at, for coordinate suggestions. fn refresh_dynamic_registries(&mut self) { self.ui.registries.target_block = self.pick().map(|h| h.pos); + // ClientPacketListener.getOnlinePlayerNames on a server. + if let Some(names) = self.remote_online_names() { + self.ui.registries.players = names; + } } pub(super) fn open_chat(&mut self, initial: &str) { if self.player.is_none() { @@ -389,9 +411,9 @@ impl Game<'_> { .as_ref() .map_or([1280, 720], |r| [r.config.width, r.config.height]); let b = HudBatch::new(size); - let m = Measure(&self.atlas.font); + let m = Measure(&self.gui.atlas.font); let c = Commands { - dispatcher: self.mods.dispatcher(), + dispatcher: chat_tree(&self.remote, &self.mods), registries: &self.ui.registries, }; let mut screen = ChatScreen::new( @@ -434,7 +456,7 @@ impl Game<'_> { let focused = self.screen == Screen::Chat; let heard = self.mods.mods.is_running().then(|| text.plain()); self.ui.chat.add( - &self.atlas, + &self.gui.atlas, text, Some(SYSTEM_INDICATOR), self.ticks, @@ -447,9 +469,9 @@ impl Game<'_> { /// Keys while the chat screen is open. pub(super) fn chat_key(&mut self, event: &KeyEvent) { if let Some(shortcut) = self.edit_shortcut(event) { - let m = Measure(&self.atlas.font); + let m = Measure(&self.gui.atlas.font); let c = Commands { - dispatcher: self.mods.dispatcher(), + dispatcher: chat_tree(&self.remote, &self.mods), registries: &self.ui.registries, }; if let Some(screen) = self.ui.chat_screen.as_mut() { @@ -493,9 +515,9 @@ impl Game<'_> { let Some(mut screen) = self.ui.chat_screen.take() else { return; }; - let m = Measure(&self.atlas.font); + let m = Measure(&self.gui.atlas.font); let c = Commands { - dispatcher: self.mods.dispatcher(), + dispatcher: chat_tree(&self.remote, &self.mods), registries: &self.ui.registries, }; match screen.key(&m, &c, &self.ui.chat.recent, key, shift, ctrl) { @@ -517,11 +539,28 @@ impl Game<'_> { } } } + /// The server's completions for an `ask_server` request + /// (ClientPacketListener.handleCommandSuggestions). + pub(super) fn chat_server_suggestions( + &mut self, + id: i32, + range: (usize, usize), + list: Vec<(String, Option)>, + ) { + let m = Measure(&self.gui.atlas.font); + let c = Commands { + dispatcher: chat_tree(&self.remote, &self.mods), + registries: &self.ui.registries, + }; + if let Some(screen) = self.ui.chat_screen.as_mut() { + screen.server_suggestions(&m, &c, id, range, list); + } + } /// Text typed into the chat box (control socket `chat`). pub(super) fn chat_type(&mut self, text: &str) { - let m = Measure(&self.atlas.font); + let m = Measure(&self.gui.atlas.font); let c = Commands { - dispatcher: self.mods.dispatcher(), + dispatcher: chat_tree(&self.remote, &self.mods), registries: &self.ui.registries, }; if let Some(screen) = self.ui.chat_screen.as_mut() { @@ -531,6 +570,11 @@ impl Game<'_> { /// ChatScreen.handleChatInput: history, then a command or a chat line. pub(super) fn handle_chat_input(&mut self, message: &str) { self.ui.chat.add_recent(message); + // On a server (single player's own included) the server runs it. + if self.remote.is_some() { + self.send_chat(message); + return; + } if let Some(command) = message.strip_prefix('/') { self.run_command(command); } else { @@ -542,7 +586,7 @@ impl Game<'_> { let heard = self.mods.mods.is_running().then(|| line.plain()); self.ui .chat - .add(&self.atlas, line, None, self.ticks, focused); + .add(&self.gui.atlas, line, None, self.ticks, focused); if let Some(line) = heard { self.mods_chat(&line); } @@ -570,9 +614,9 @@ impl Game<'_> { let height = self.renderer.as_ref().map_or(720.0, |r| { HudBatch::new([r.config.width, r.config.height]).height }); - let m = Measure(&self.atlas.font); + let m = Measure(&self.gui.atlas.font); let c = Commands { - dispatcher: self.mods.dispatcher(), + dispatcher: chat_tree(&self.remote, &self.mods), registries: &self.ui.registries, }; if let Some(screen) = self.ui.chat_screen.as_mut() { @@ -583,7 +627,7 @@ impl Game<'_> { return; } match self.ui.chat.click_at( - &self.atlas.font, + &self.gui.atlas.font, height as i32, cursor.0 as f64, cursor.1 as f64, @@ -609,15 +653,17 @@ impl Game<'_> { if self.ui.hide_gui && !focused { return; } - self.ui.chat.draw(batch, &self.atlas, self.ticks, focused); + self.ui + .chat + .draw(batch, &self.gui.atlas, self.ticks, focused); } pub(super) fn draw_chat_screen(&mut self, batch: &mut HudBatch) { let Some(mut screen) = self.ui.chat_screen.take() else { return; }; - let m = Measure(&self.atlas.font); + let m = Measure(&self.gui.atlas.font); let c = Commands { - dispatcher: self.mods.dispatcher(), + dispatcher: chat_tree(&self.remote, &self.mods), registries: &self.ui.registries, }; screen.resize(&m, &c, batch.width as i32, batch.height as i32); @@ -633,7 +679,7 @@ impl Game<'_> { .chat_opened .map_or(0, |t| t.elapsed().as_millis() as u64); let draws = screen.draw(&m, mouse, blink, background); - horizon_client::screen_draw::paint(batch, &self.atlas, &draws); + horizon_client::screen_draw::paint(batch, &self.gui.atlas, &draws); self.ui.chat_screen = Some(screen); } /// Advance the overworld clock (ServerClockManager.tick) when @@ -651,6 +697,16 @@ impl Game<'_> { c.partial -= whole; self.world.day_time += whole as i64; } + /// `ServerClockInstance.packNetworkState`: the rate players' clients + /// advance the overworld clock at, 0 while it is paused (`/time pause`) + /// or `advance_time` is off. + pub(super) fn network_clock_rate(&self) -> f32 { + if self.ui.rules.bool("advance_time") && !self.ui.clock.paused { + self.ui.clock.rate + } else { + 0.0 + } + } /// The engine-side rule values (block behaviour and health). pub(super) fn level_rules(&self) -> horizon_world::level::GameRules { let r = &self.ui.rules; @@ -825,11 +881,11 @@ impl Game<'_> { let focused = self.screen == Screen::Chat; self.ui .chat - .add(&self.atlas, message, None, self.ticks, focused); + .add(&self.gui.atlas, message, None, self.ticks, focused); } #[allow(dead_code)] fn chat_debug_fill(&self, batch: &mut HudBatch) { - batch.fill(&self.atlas, 0.0, 0.0, 1.0, 1.0, rgba(0, 0, 0, 0)); + batch.fill(&self.gui.atlas, 0.0, 0.0, 1.0, 1.0, rgba(0, 0, 0, 0)); } } /// `yyyy-MM-dd_HH.mm.ss[_n].png` (Util.getFilenameFormattedDateTime, local time diff --git a/crates/horizon-client/src/game/combat.rs b/crates/horizon-client/src/game/combat.rs index abe67c6c6..da3851d9a 100644 --- a/crates/horizon-client/src/game/combat.rs +++ b/crates/horizon-client/src/game/combat.rs @@ -114,7 +114,7 @@ impl Game<'_> { let location = if let Some(c) = p.last_climbable { let name = self .res - .facts + .facts() .get(self.world.block(c.x, c.y, c.z)) .name .clone(); @@ -127,7 +127,7 @@ impl Game<'_> { _ => "other_climbable", }) } else { - let f = self.res.facts.get(self.world.block( + let f = self.res.facts().get(self.world.block( p.position.x.floor() as i32, (p.position.y + 0.1).floor() as i32, p.position.z.floor() as i32, diff --git a/crates/horizon-client/src/game/command_blocks.rs b/crates/horizon-client/src/game/command_blocks.rs index 5a5fa11c8..76fc6d48c 100644 --- a/crates/horizon-client/src/game/command_blocks.rs +++ b/crates/horizon-client/src/game/command_blocks.rs @@ -356,7 +356,8 @@ impl Game<'_> { eyes: false, silent: false, store: None, - elsewhere: false, + dimension: self.dim, + level: None, block: Some(pos), }; // performPrefixedCommand: a leading slash is allowed. @@ -458,7 +459,7 @@ impl Game<'_> { self.close_command_block(); return; } - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let (w, h) = (batch.width, batch.height); let mid = (w / 2.0).floor(); ui::menu_background(batch, atlas, 0.0, 0.0, w, h, true); @@ -598,13 +599,13 @@ impl Game<'_> { if let Ok(id) = registry.resolve(&wanted) && wanted != *current { - let entity = self.world.block_entities.remove(&pos); + let entity = self.world.block_entities.take(&pos); let at = BlockPos::new(pos.0, pos.1, pos.2); self.with_level(|b, level, ticks| { b.set_block(level, ticks, at, id, flags::UPDATE_CLIENTS) }); if let Some(entity) = entity { - self.world.block_entities.insert(pos, entity); + self.world.block_entities.put(pos, entity); } let _ = self.process_level_events(); } diff --git a/crates/horizon-client/src/game/command_harness.rs b/crates/horizon-client/src/game/command_harness.rs index ed3f8b336..096ba1c74 100644 --- a/crates/horizon-client/src/game/command_harness.rs +++ b/crates/horizon-client/src/game/command_harness.rs @@ -4,7 +4,8 @@ //! command as the player (Commands.performPrefixedCommand) and records every //! system message it produced, as styled spans, with the results reported to //! the player's `CommandResultCallback`; `tick` advances the game; `settle` -//! waits until the terrain around the player is loaded. +//! waits until the terrain and entities around the player are loaded; +//! `portal` sends the player through the nether portal it stands in. use super::Game; use horizon_client::{Result, text::Text}; use serde_json::{Value, json}; @@ -40,6 +41,19 @@ pub(super) fn spans(text: &Text) -> Value { } impl Game<'_> { + /// The player's portal process finished in the nether portal it stands + /// in (the oracle's player is not ticked, so neither is the wait). + fn harness_portal(&mut self) -> Result<()> { + let Some(p) = &self.player else { + return Err("command harness: portal without a player".into()); + }; + let entry = horizon_core::position::BlockPos::containing(p.position); + let block = self.world.block(entry.x, entry.y, entry.z); + if &*self.res.facts().get(block).name != "minecraft:nether_portal" { + return Err("command harness: the player is not in a nether portal".into()); + } + self.player_through_nether_portal(entry) + } /// Load terrain until the player stands in lit chunks. fn harness_settle(&mut self, deadline: Instant) -> Result<()> { loop { @@ -51,6 +65,10 @@ impl Game<'_> { (p.position.z.floor() as i32) >> 4, ); if (-1..=1).all(|dz| (-1..=1).all(|dx| self.streaming.is_lit(cx + dx, cz + dz))) { + // The oracle's settle also waits for the entities saved + // in those chunks (areEntitiesActuallyLoadedAndTicking): + // after a dimension change they come back from the save. + self.spawn_loaded_entities(); return Ok(()); } } @@ -130,6 +148,7 @@ impl Game<'_> { } } "settle" => self.harness_settle(deadline)?, + "portal" => self.harness_portal()?, other => return Err(format!("command harness: unknown op {other}").into()), } } diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs index 6c4817f63..b970414a6 100644 --- a/crates/horizon-client/src/game/commands.rs +++ b/crates/horizon-client/src/game/commands.rs @@ -1,5 +1,6 @@ //! Command execution (the `execute` side of each vanilla command class): //! entity selectors resolved against the game, vanilla feedback and errors. +mod admin; pub(super) mod compute; mod data; mod effects; @@ -8,6 +9,7 @@ pub(super) mod extra; mod items; mod misc; mod place; +mod players; mod raid; mod terrain_cmds; mod text_commands; @@ -32,7 +34,11 @@ pub(super) type R = Result; /// An entity a selector found. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub(super) enum Target { + /// The player in place: the host's, or the guest whose turn it is. Player, + /// Another player of a hosted world or server: a guest by connection + /// (game/commands/players.rs). + Guest(u32), Mob(u32), Item(u32), Orb(usize), @@ -160,8 +166,70 @@ impl Game<'_> { return; } if self.ui.rules.bool("send_command_feedback") { - self.system_message(message); + self.system_message(message.clone()); } + self.broadcast_to_admins(message); + } + /// CommandSourceStack.broadcastToAdmins (`sendSuccess` with `broadcast`) + /// for a guest's command, the host's in a world open to LAN and the + /// dedicated server console's when `broadcast-console-to-ops` is on: + /// `[name: message]`, gray and italic, for the other operators when + /// `send_command_feedback`, and for a player's command on a dedicated + /// server the console when `log_admin_commands`. Queued while the + /// command runs; `send_admin_feedback` sends it. + pub(super) fn broadcast_to_admins(&mut self, message: Text) { + let informs = match self.console_broadcast { + Some(broadcast) => broadcast, + // While a guest acts the host's LanHost is out of `self.lan`. + None => self.lan_acting.is_some() || self.lan.is_some(), + }; + if !informs + || message + .translation_key() + .is_some_and(|k| admin::QUIET.contains(&k)) + { + return; + } + let ops = self.ui.rules.bool("send_command_feedback"); + // MinecraftServer.sendSystemMessage, unless the server is the source. + let console = self.console_broadcast.is_none() + && self.lan_acting.is_some() + && self.options.dedicated.is_some() + && self.ui.rules.bool("log_admin_commands"); + if !ops && !console { + return; + } + // getDisplayName: the entity the command runs as (`execute as`), or + // the server's own source, "Server". + let who = match self.command_source.and_then(|s| s.entity) { + Some(entity) => self.target_name(entity), + None if self.console_broadcast.is_some() => Text::literal("Server".to_owned()), + None => self.player_display_name(), + }; + let line = Text::translate("chat.type.admin", &[who, message]) + .with_color(text::GRAY) + .italic(); + self.admin_feedback.push((line, ops, console)); + } + /// Player.getDisplayName: the name (team-formatted), suggesting a + /// `/tell`, showing the player on hover, inserting the name. + fn player_display_name(&self) -> Text { + let name = self.acting_player_name().to_owned(); + let Some(uuid) = self.lan_acting_uuid else { + return self.target_name(Target::Player); + }; + self.target_name(Target::Player).with_component_fields(vec![ + ( + "click_event".into(), + serde_json::json!({"action": "suggest_command", "command": format!("/tell {name} ")}), + ), + ( + "hover_event".into(), + serde_json::json!({"action": "show_entity", "id": "minecraft:player", + "uuid": horizon_protocol::crypto::uuid_string(uuid), "name": name}), + ), + ("insertion".into(), name.into()), + ]) } fn source_position(&self) -> [f64; 3] { if let Some(s) = &self.command_source { @@ -169,9 +237,16 @@ impl Game<'_> { } match &self.player { Some(p) => [p.position.x, p.position.y, p.position.z], - None => self.spawn.map_or([0.0, 64.0, 0.0], |s| [s.x, s.y, s.z]), + None => self.server_position(), } } + /// MinecraftServer.createCommandSourceStack's position: the corner of + /// the respawn block (`Vec3.atLowerCornerOf`). + pub(super) fn server_position(&self) -> [f64; 3] { + self.spawn.map_or([0.0, 64.0, 0.0], |s| { + [s.x.floor(), s.y.floor(), s.z.floor()] + }) + } fn source_rotation(&self) -> [f32; 2] { self.command_source .map_or([self.yaw, self.pitch], |s| s.rot) @@ -200,8 +275,8 @@ impl Game<'_> { .ok_or_else(|| err("argument.pos.missing.int", &[]))?; // BlockPos.containing(getPosition) let p = self.resolve_coords(&c).map(|v| v.floor() as i32); - let elsewhere = self.command_source.is_some_and(|s| s.elsewhere); - if elsewhere || !self.world.chunks.contains_key(&(p[0] >> 4, p[2] >> 4)) { + let dimension = self.command_source.map_or(self.dim, |s| s.dimension); + if dimension != self.dim || !self.world.chunks.contains_key(&(p[0] >> 4, p[2] >> 4)) { return Err(err("argument.pos.unloaded", &[])); } if p[1] < horizon_client::chunk::MIN_Y @@ -225,6 +300,7 @@ impl Game<'_> { .player .as_ref() .map(|p| [p.position.x, p.position.y, p.position.z]), + Target::Guest(conn) => self.command_guest(conn).map(|g| g.position), Target::Mob(id) => self .mobs .iter() @@ -284,7 +360,7 @@ impl Game<'_> { } pub(super) fn target_type(&self, t: Target) -> String { match t { - Target::Player => "minecraft:player".into(), + Target::Player | Target::Guest(_) => "minecraft:player".into(), Target::Mob(id) => self .mobs .iter() @@ -321,18 +397,29 @@ impl Game<'_> { .map_or_else(String::new, |o| o.kind.clone()), } } + /// The name of the player the game acts for: a guest's own while their + /// command or action runs (`Player.getName`). + pub(super) fn acting_player_name(&self) -> &str { + self.lan_acting_name.as_deref().unwrap_or(PLAYER_NAME) + } /// Entity.getDisplayName. pub(super) fn target_name(&self, t: Target) -> Text { match t { Target::Player => { - // A guest's commands name the guest. - let name = self.lan_acting_name.as_deref().unwrap_or(PLAYER_NAME); + let name = self.acting_player_name(); + if let Some(uuid) = self.lan_acting_uuid { + return self.player_text(name, uuid); + } match self.score.board.players_team(name) { // PlayerTeam.formatNameForTeam. Some(team) => team.formatted_name(Text::literal(name)), None => Text::literal(name), } } + Target::Guest(conn) => match self.command_guest(conn) { + Some(g) => self.player_text(&g.name, g.uuid), + None => Text::empty(), + }, Target::Item(id) => { let item = self .dropped @@ -379,11 +466,10 @@ impl Game<'_> { /// EntitySelector.findEntities. pub(super) fn select(&mut self, s: &Selector) -> Vec { if let Some(name) = &s.player_name { - return if name.eq_ignore_ascii_case(PLAYER_NAME) && self.player.is_some() { - vec![Target::Player] - } else { - Vec::new() - }; + return self.player_named(name).into_iter().collect(); + } + if let Some(uuid) = s.uuid { + return self.target_by_uuid(uuid).into_iter().collect(); } let origin = { let p = self.source_position(); @@ -397,10 +483,15 @@ impl Game<'_> { match (s.kind, self.command_source) { // @s: the executing entity (none for a server source). ('s', Some(source)) => candidates.extend(source.entity), - _ if self.player.is_some() => candidates.push(Target::Player), + ('s', None) => candidates.extend(self.source_entity()), + // A guest in place is among the players, in join order. + _ if self.player.is_some() && self.lan_acting.is_none() => { + candidates.push(Target::Player); + } _ => {} } if s.kind != 's' { + candidates.extend(self.player_candidates(s)); candidates.extend( self.mobs .iter() @@ -444,7 +535,7 @@ impl Game<'_> { } } if !s.include_entities - && t != Target::Player + && !t.is_player() && s.types.iter().all(|(w, n)| *n || w != &ty) && s.kind != 'e' && s.kind != 'n' @@ -460,16 +551,20 @@ impl Game<'_> { if !passing.contains(&t) { return None; } + let player = match t { + Target::Player => Some((level, if creative { "creative" } else { "survival" })), + Target::Guest(conn) => self.guest_level_and_mode(conn), + _ => None, + }; if let Some((lo, hi)) = s.level - && (t != Target::Player - || lo.is_some_and(|l| level < l) - || hi.is_some_and(|h| level > h)) + && player.is_none_or(|(level, _)| { + lo.is_some_and(|l| level < l) || hi.is_some_and(|h| level > h) + }) { return None; } for (mode, neg) in &s.gamemodes { - let current = if creative { "creative" } else { "survival" }; - if t != Target::Player || (current == mode) == *neg { + if player.is_none_or(|(_, current)| (current == mode) == *neg) { return None; } } @@ -528,12 +623,12 @@ impl Game<'_> { /// EntityArgument.getEntities / getPlayers / getEntity / getPlayer. pub(super) fn entities(&mut self, ctx: &Context, name: &str, players: bool) -> R> { let Some(Arg::Entity(s)) = ctx.get(name).cloned() else { - return Ok(vec![Target::Player]); + return Ok(vec![self.implied_player()]); }; let found: Vec = self .select(&s) .into_iter() - .filter(|t| !players || *t == Target::Player) + .filter(|t| !players || t.is_player()) .collect(); if found.is_empty() { return Err(err( @@ -739,63 +834,63 @@ impl Game<'_> { let targets = if ctx.get("target").is_some() { self.entities(ctx, "target", true)? } else { - vec![Target::Player] + vec![self.implied_player()] }; let mut count = 0; for t in targets { - if t != Target::Player || !self.set_game_mode(mode) { + let mode_name = Text::translate(&format!("gameMode.{}", mode.name()), &[]); + // GameModeCommand.logGamemodeChange: the source's own entity. + let own = self.source_entity() == Some(t); + let told = !own && self.ui.rules.bool("send_command_feedback"); + let changed = self.for_player(t, |g| { + if !g.set_game_mode(mode) { + return false; + } + if told { + g.system_message(Text::translate( + "gameMode.changed", + std::slice::from_ref(&mode_name), + )); + } + true + }); + if changed != Some(true) { continue; } count += 1; - let mode_name = Text::translate(&format!("gameMode.{}", mode.name()), &[]); - // GameModeCommand.logGamemodeChange: the source's own entity. - let own = match self.command_source { - None => true, - Some(s) => s.entity == Some(t), - }; if own { self.feedback(Text::translate( "commands.gamemode.success.self", &[mode_name], )); } else { - if self.ui.rules.bool("send_command_feedback") { - self.system_message(Text::translate( - "gameMode.changed", - std::slice::from_ref(&mode_name), - )); - } self.feedback(Text::translate( "commands.gamemode.success.other", - &[Text::literal(PLAYER_NAME), mode_name], + &[self.target_name(t), mode_name], )); } } Ok(count) } - /// ItemStack.getDisplayName: "[Name]" in the rarity colour. + /// ItemStack.getDisplayName of one `item` with no components. pub(super) fn item_display_name(&self, item: &str) -> Text { - let rarity = self.item_rarity(item); - let color = match rarity.as_str() { - "uncommon" => text::YELLOW, - "rare" => text::AQUA, - "epic" => text::LIGHT_PURPLE, - _ => text::WHITE, - }; - Text::literal("[") - .append(Text::literal(self.pack.names.item(item))) - .append("]") - .with_color(color) + self.stack_display_name(&ItemStack { + item: item.into(), + count: 1, + damage: 0, + extra: None, + }) } /// `ItemStack.getDisplayName`: the hover name (custom name in italics, - /// a written book's title, a potion's effect name...) in brackets, in the - /// rarity colour unless the stack is empty. + /// a written book's title, a potion's effect name, else the item's + /// translated name) in brackets, in the rarity colour and showing the + /// stack on hover unless it is empty. pub(super) fn stack_display_name(&self, s: &ItemStack) -> Text { let item = &*s.item; let empty = item == "minecraft:air" || s.count == 0; let custom = s.custom_name(); let hover = if let Some(c) = custom { - Text::literal(c).italic() + Text::literal(c) } else if let Some(title) = s .data() .and_then(|d| d.written.as_ref()) @@ -816,9 +911,22 @@ impl Game<'_> { &[], ) } else { - Text::literal(self.pack.names.item(item)) + // Item.getDescriptionId: a block item's is the block's. + let (namespace, path) = item.split_once(':').unwrap_or(("minecraft", item)); + let key = format!("item.{namespace}.{path}"); + let key = if self.pack.names.get(&key).is_some() { + key + } else { + format!("block.{namespace}.{path}") + }; + Text::translate(&key, &[]) }; - let wrapped = Text::translate("chat.square_brackets", &[hover]); + // Component.empty().append(getHoverName()), italic for a custom name. + let mut name = Text::empty().append(hover); + if custom.is_some() { + name = name.italic(); + } + let wrapped = Text::translate("chat.square_brackets", &[name]); if empty { return wrapped; } @@ -836,7 +944,14 @@ impl Game<'_> { "epic" => text::LIGHT_PURPLE, _ => text::WHITE, }; - wrapped.with_color(color) + // HoverEvent.ShowItem: the count only when it is not one. + let mut shown = serde_json::json!({"action": "show_item", "id": item}); + if s.count != 1 { + shown["count"] = s.count.into(); + } + wrapped + .with_color(color) + .with_component_fields(vec![("hover_event".into(), shown)]) } pub(super) fn item_rarity(&self, item: &str) -> String { static RARITY: std::sync::OnceLock> = @@ -874,7 +989,7 @@ impl Game<'_> { }; let count = Self::int(ctx, "count").unwrap_or(1); let targets = self.entities(ctx, "targets", true)?; - let max_stack = self.res.items.get(&id).map_or(64, |i| i.max_stack) as i32; + let max_stack = self.res.items().get(&id).map_or(64, |i| i.max_stack) as i32; if count > max_stack * 100 { return Err(CommandError::new(Text::translate( "commands.give.failed.toomanyitems", @@ -886,33 +1001,38 @@ impl Game<'_> { } // An air stack is empty: nothing is given (but the message still is). let empty = id == "minecraft:air"; - for _ in targets.iter().filter(|_| !empty) { - let mut remaining = count; - while remaining > 0 { - let size = remaining.min(max_stack); - remaining -= size; - let left = self.give(ItemStack { - item: id.as_str().into(), - count: size as u32, - damage, - extra: extra.clone(), - }); - if let Some(rest) = left { - // Inventory full: drop the rest at the player's feet without pickup delay. - let p = self.source_position(); - self.spawn_item(rest, [p[0], p[1] + 1.32, p[2]], [0.0, 0.0, 0.0], 0); - } else { - let r = &mut self.world.random; - let pitch = ((r.next_float() - r.next_float()) * 0.7 + 1.0) * 2.0; - let p = self.source_position(); - self.audio - .play("minecraft:entity.item.pickup", Some(p), 0.2, pitch); + for &t in targets.iter().filter(|_| !empty) { + self.for_player(t, |g| { + let mut remaining = count; + while remaining > 0 { + let size = remaining.min(max_stack); + remaining -= size; + let left = g.give(ItemStack { + item: id.as_str().into(), + count: size as u32, + damage, + extra: extra.clone(), + }); + // At the player who gets them. + let p = g + .target_position(Target::Player) + .unwrap_or(g.source_position()); + if let Some(rest) = left { + // Inventory full: drop the rest at the player's feet without pickup delay. + g.spawn_item(rest, [p[0], p[1] + 1.32, p[2]], [0.0, 0.0, 0.0], 0); + } else { + let r = &mut g.world.random; + let pitch = ((r.next_float() - r.next_float()) * 0.7 + 1.0) * 2.0; + g.audio + .play("minecraft:entity.item.pickup", Some(p), 0.2, pitch); + } } - } + }); } + // The prototype stack: one item. let name = self.stack_display_name(&ItemStack { item: id.as_str().into(), - count: count.max(1) as u32, + count: 1, damage, extra: extra.clone(), }); @@ -922,7 +1042,7 @@ impl Game<'_> { &[ Text::literal(count.to_string()), name, - Text::literal(PLAYER_NAME), + self.target_name(targets[0]), ], ) } else { @@ -941,6 +1061,9 @@ impl Game<'_> { fn teleport_to(&mut self, t: Target, pos: [f64; 3], rotation: Option<[f32; 2]>) { let v = Vec3::new(pos[0], pos[1], pos[2]); match t { + Target::Guest(_) => { + self.for_player(t, |g| g.teleport_to(Target::Player, pos, rotation)); + } Target::Player => { if let Some(p) = self.player.as_mut() { p.position = v; @@ -1021,17 +1144,96 @@ impl Game<'_> { Target::Frame(_) | Target::Painting(_) => {} } } + /// Teleport the local player into the command source's dimension. Unlike + /// portal travel, `/tp` keeps the coordinates given by the command. + fn teleport_player_to_dimension( + &mut self, + dimension: Dim, + pos: [f64; 3], + rotation: Option<[f32; 2]>, + ) -> R<()> { + let Some(mut player) = self.player.take() else { + return Ok(()); + }; + let from = self.dim; + if self.switch_level(dimension, player.position).is_err() { + self.player = Some(player); + return Err(err("command.failed", &[])); + } + self.trigger_dimension(from, dimension); + let target = Vec3::new(pos[0], pos[1], pos[2]); + player.position = target; + player.velocity = Vec3::ZERO; + player.fall_distance = 0.0; + player.ground.on_ground = false; + if let Some([yaw, pitch]) = rotation { + let pitch = pitch.clamp(-90.0, 90.0); + self.yaw = yaw; + self.pitch = pitch; + player.yaw = yaw; + player.head_yaw = yaw; + player.pitch = pitch; + } + self.travelling = Some((player, target)); + self.portals.travel = None; + self.end_travel = false; + self.end_portal_travel = false; + Ok(()) + } + + /// A server's player teleported into another level than theirs + /// (ServerPlayer.teleport): queued to move after the command. False + /// when `t` is not a player in another level. + fn teleport_across( + &mut self, + t: Target, + level: super::Dim, + pos: [f64; 3], + rotation: Option<[f32; 2]>, + ) -> bool { + let (conn, at) = match t { + Target::Player => (self.lan_acting, self.dim), + Target::Guest(conn) => ( + Some(conn), + self.command_guest(conn) + .map_or(self.dim, |g| super::Dim::from_id(&g.dimension)), + ), + _ => (None, self.dim), + }; + let Some(conn) = conn.filter(|_| at != level && self.options.dedicated.is_some()) else { + return false; + }; + self.level_teleports + .push(super::server_travel::LevelTeleport { + conn, + dim: level, + at: pos, + rotation, + }); + true + } fn cmd_teleport(&mut self, id: &str, ctx: &Context) -> R { let targets = if id.starts_with("teleport.targets") { self.entities(ctx, "targets", false)? } else { - vec![Target::Player] + vec![self.implied_player()] }; if id.ends_with("destination") { let dest = self.entities(ctx, "destination", false)?[0]; let pos = self.target_position(dest).unwrap_or(self.source_position()); + // Into the destination's level. + let level = match dest { + Target::Guest(conn) => self + .command_guest(conn) + .map_or(self.dim, |g| super::Dim::from_id(&g.dimension)), + _ => self.dim, + }; + // TeleportCommand.teleportToEntity: the destination's rotation too. + let rotation = Some(self.target_rotation(dest)); for &t in &targets { - self.teleport_to(t, pos, None); + if !self.teleport_across(t, level, pos, rotation) { + self.teleport_to(t, pos, rotation); + } } let message = if targets.len() == 1 { Text::translate( @@ -1089,8 +1291,24 @@ impl Game<'_> { (-(dy.atan2(horizontal).to_degrees())) as f32, ]); } + // A server's player in another level than the source's (`execute + // in`, the console's Overworld) moves there (ServerPlayer.teleport + // into another level), after the command. + let source_level = match self.command_source.and_then(|s| s.level) { + Some(level) => level, + None if self.source_entity().is_none() => super::Dim::Overworld, + None => self.dim, + }; for &t in &targets { - self.teleport_to(t, pos, rotation); + if self.teleport_across(t, source_level, pos, rotation) { + continue; + } + let dimension = self.command_source.map_or(self.dim, |s| s.dimension); + if t == Target::Player && dimension != self.dim { + self.teleport_player_to_dimension(dimension, pos, rotation)?; + } else { + self.teleport_to(t, pos, rotation); + } } let [x, y, z] = pos.map(|v| Text::literal(format_double(v))); let message = if targets.len() == 1 { @@ -1290,6 +1508,9 @@ impl Game<'_> { } fn kill_target(&mut self, t: Target) { match t { + Target::Guest(_) => { + self.for_player(t, |g| g.kill_target(Target::Player)); + } Target::Player => { if let Some(p) = self.player.as_mut() { let _ = p.set_health(0.0, false); @@ -1332,7 +1553,7 @@ impl Game<'_> { let targets = if ctx.get("targets").is_some() { self.entities(ctx, "targets", false)? } else { - vec![Target::Player] + vec![self.implied_player()] }; let names: Vec = targets.iter().map(|&t| self.target_name(t)).collect(); // Remove indexed entities from the back so earlier indices stay valid. @@ -1385,7 +1606,7 @@ impl Game<'_> { if self.ui.difficulty == 0 && self .res - .mob_types + .mob_types() .get(&format!("minecraft:{short}")) .is_some_and(|t| t.category == "monster") { @@ -1402,7 +1623,7 @@ impl Game<'_> { let name; if let Some(t) = self .res - .mob_types + .mob_types() .get(&format!("minecraft:{short}")) .cloned() { @@ -1418,7 +1639,9 @@ impl Game<'_> { return Err(err("commands.summon.failed", &[])); } "experience_orb" => { + self.next_entity += 1; self.orbs.push(horizon_client::entities::ExperienceOrb { + id: self.next_entity, position: Vec3::new(pos[0], pos[1], pos[2]), previous: Vec3::new(pos[0], pos[1], pos[2]), velocity: Vec3::new(0.0, 0.0, 0.0), @@ -1444,7 +1667,13 @@ impl Game<'_> { self.carts.push(cart); name = Text::translate(&format!("entity.minecraft.{k}"), &[]); } - _ => return Err(err("commands.summon.failed", &[])), + // SummonCommand.createEntity: any other type from an empty compound. + _ => { + let Some(target) = self.summon_with_nbt(&kind, pos, "{}")? else { + return Err(err("commands.summon.failed", &[])); + }; + name = self.target_name(target); + } } } self.feedback(Text::translate("commands.summon.success", &[name])); @@ -1470,10 +1699,10 @@ impl Game<'_> { let name_ok = if b.tag { let tag = b.id.trim_start_matches("minecraft:"); self.res - .items + .items() .block_tags .get(tag) - .or_else(|| self.res.items.block_tags.get(&b.id)) + .or_else(|| self.res.items().block_tags.get(&b.id)) .is_some_and(|set| { set.contains(&s.name) || set.contains(s.name.trim_start_matches("minecraft:")) }) @@ -1498,8 +1727,8 @@ impl Game<'_> { } // A different block replaces the block entity (Level.setBlock). let (old, new) = ( - self.res.facts.get(previous).name.clone(), - self.res.facts.get(state).name.clone(), + self.res.facts().get(previous).name.clone(), + self.res.facts().get(state).name.clone(), ); // The command clears the old block entity without its side effects // (UPDATE_SKIP_BLOCK_ENTITY_SIDEEFFECTS); the new block gets a fresh one. @@ -1794,7 +2023,7 @@ impl Game<'_> { ), _ => None, }; - let player = Text::literal(PLAYER_NAME); + let player = Text::literal(self.acting_player_name()); let players = targets.len(); if let (Some(i), Some(Arg::Str(criterion))) = (index, ctx.get("criterion")) { let a = &tree.list[i]; @@ -1904,7 +2133,10 @@ impl Game<'_> { }; self.system_message(Text::translate( &format!("commands.experience.query.{unit}"), - &[Text::literal(PLAYER_NAME), Text::literal(value.to_string())], + &[ + Text::literal(self.acting_player_name()), + Text::literal(value.to_string()), + ], )); return Ok(value); } @@ -1941,7 +2173,7 @@ impl Game<'_> { &key, &[ Text::literal(amount.to_string()), - Text::literal(PLAYER_NAME), + Text::literal(self.acting_player_name()), ], )); Ok(targets.len() as i32) @@ -2090,10 +2322,10 @@ impl Game<'_> { let tag = predicate.id.trim_start_matches("minecraft:"); return self .res - .items + .items() .item_tags .get(tag) - .or_else(|| self.res.items.item_tags.get(&predicate.id)) + .or_else(|| self.res.items().item_tags.get(&predicate.id)) .is_some_and(|s| { s.contains(item) || s.contains(item.trim_start_matches("minecraft:")) }); @@ -2248,7 +2480,7 @@ impl Game<'_> { return Err(if targets.len() == 1 { CommandError::new(Text::translate( "clear.failed.single", - &[Text::literal(PLAYER_NAME)], + &[Text::literal(self.acting_player_name())], )) } else { err("clear.failed.multiple", &[&targets.len()]) @@ -2259,7 +2491,7 @@ impl Game<'_> { &format!("commands.clear.{kind}.single"), &[ Text::literal(removed.to_string()), - Text::literal(PLAYER_NAME), + Text::literal(self.acting_player_name()), ], )); Ok(removed) @@ -2312,7 +2544,7 @@ impl Game<'_> { }; let from = self.source_position().map(|v| v.floor() as i32); const RANGE: i32 = 256; - let mut cache: std::collections::HashMap> = + let mut cache: std::collections::HashMap> = std::collections::HashMap::new(); let mut best: Option<(i64, [i32; 3], &'static str)> = None; let (cx0, cz0) = ((from[0] - RANGE) >> 4, (from[2] - RANGE) >> 4); @@ -2323,15 +2555,12 @@ impl Game<'_> { continue; }; for (i, section) in chunk.sections.iter().enumerate() { - let mut wanted_state = |v: u32| { - let state = v as u16; + let mut wanted_state = |state: u32| { cache .entry(state) .or_insert_with(|| { - self.poi_type(horizon_world::registry::BlockStateId(u32::from( - state, - ))) - .filter(|t| wanted.iter().any(|w| w == t)) + self.poi_type(horizon_world::registry::BlockStateId(state)) + .filter(|t| wanted.iter().any(|w| w == t)) }) .is_some() }; @@ -2513,7 +2742,7 @@ impl Game<'_> { let name = match self.command_source.map(|s| s.entity) { Some(Some(t)) => self.target_name(t), Some(None) => Text::literal("Server"), - None => Text::literal(PLAYER_NAME), + None => Text::literal(self.acting_player_name()), }; let line = match id { "say" => Text::translate("chat.type.announcement", &[name, message]), @@ -2523,14 +2752,14 @@ impl Game<'_> { // Whispering to yourself: the incoming and outgoing lines. let out = Text::translate( "commands.message.display.outgoing", - &[Text::literal(PLAYER_NAME), message.clone()], + &[Text::literal(self.acting_player_name()), message.clone()], ) .with_color(text::GRAY) .italic(); self.system_message(out); Text::translate( "commands.message.display.incoming", - &[Text::literal(PLAYER_NAME), message], + &[Text::literal(self.acting_player_name()), message], ) .with_color(text::GRAY) .italic() @@ -2542,7 +2771,7 @@ impl Game<'_> { let focused = self.screen == Screen::Chat; self.ui .chat - .add(&self.atlas, line, None, self.ticks, focused); + .add(&self.gui.atlas, line, None, self.ticks, focused); Ok(1) } /// MessageArgument: `@p`-style selectors in a message become names. diff --git a/crates/horizon-client/src/game/commands/admin.rs b/crates/horizon-client/src/game/commands/admin.rs new file mode 100644 index 000000000..1a5115f4d --- /dev/null +++ b/crates/horizon-client/src/game/commands/admin.rs @@ -0,0 +1,92 @@ +//! Which command feedback vanilla tells the other operators and the console +//! about (`CommandSourceStack.sendSuccess`'s `broadcast`). + +/// The replies vanilla sends with `broadcast` false: queries and lists, which +/// only the player who asked sees. Every other success message is broadcast. +pub(super) const QUIET: &[&str] = &[ + "commands.attribute.base_value.get.success", + "commands.attribute.base_value.reset.success", + "commands.attribute.base_value.set.success", + "commands.attribute.modifier.add.success", + "commands.attribute.modifier.remove.success", + "commands.attribute.modifier.value.get.success", + "commands.attribute.value.get.success", + "commands.banlist.entry", + "commands.banlist.list", + "commands.banlist.none", + "commands.bossbar.list.bars.none", + "commands.bossbar.list.bars.some", + "commands.data.block.get", + "commands.data.block.query", + "commands.data.entity.get", + "commands.data.entity.query", + "commands.data.storage.get", + "commands.data.storage.query", + "commands.datapack.list.available.none", + "commands.datapack.list.available.success", + "commands.datapack.list.enabled.none", + "commands.datapack.list.enabled.success", + "commands.difficulty.query", + "commands.execute.conditional.pass", + "commands.execute.conditional.pass_count", + "commands.experience.query.levels", + "commands.experience.query.points", + "commands.fetchprofile.copy_component", + "commands.fetchprofile.copy_text", + "commands.fetchprofile.give_item", + "commands.fetchprofile.summon_mannequin", + "commands.forceload.list.multiple", + "commands.forceload.list.single", + "commands.forceload.query.success", + "commands.gamerule.query", + "commands.jfr.started", + "commands.jfr.stopped", + "commands.list.players", + "commands.locate.biome.success", + "commands.locate.poi.success", + "commands.locate.structure.success", + "commands.perf.started", + "commands.perf.stopped", + "commands.posteffect.list.empty", + "commands.posteffect.list.success", + "commands.random.reset.all.success", + "commands.random.reset.success", + "commands.random.sample.success", + "commands.save.saving", + "commands.scoreboard.objectives.list.empty", + "commands.scoreboard.objectives.list.success", + "commands.scoreboard.players.get.success", + "commands.scoreboard.players.list.empty", + "commands.scoreboard.players.list.entity.empty", + "commands.scoreboard.players.list.entity.entry", + "commands.scoreboard.players.list.entity.success", + "commands.scoreboard.players.list.success", + "commands.seed.success", + "commands.spectate.success.started", + "commands.spectate.success.stopped", + "commands.team.list.members.empty", + "commands.team.list.members.success", + "commands.team.list.teams.empty", + "commands.team.list.teams.success", + "commands.team.option.prefix.success", + "commands.team.option.suffix.success", + "commands.tick.query.percentiles", + "commands.tick.query.rate.running", + "commands.tick.query.rate.sprinting", + "commands.tick.status.frozen", + "commands.tick.status.lagging", + "commands.tick.status.running", + "commands.tick.status.sprinting", + "commands.time.query.absolute", + "commands.time.query.gametime", + "commands.time.query.timeline", + "commands.time.query.timeline.repetitions", + "commands.waypoint.list.empty", + "commands.waypoint.list.success", + "commands.waypoint.modify.color", + "commands.waypoint.modify.color.reset", + "commands.waypoint.modify.style", + "commands.whitelist.list", + "commands.whitelist.none", + "commands.worldborder.get", +]; diff --git a/crates/horizon-client/src/game/commands/compute.rs b/crates/horizon-client/src/game/commands/compute.rs index 24552011d..c7b2355e3 100644 --- a/crates/horizon-client/src/game/commands/compute.rs +++ b/crates/horizon-client/src/game/commands/compute.rs @@ -502,7 +502,7 @@ impl Game<'_> { Some(Value::String(b)) => match b.strip_prefix('#') { Some(tag) => self .res - .items + .items() .block_tags .get(tag.trim_start_matches("minecraft:")) .is_some_and(|set| { diff --git a/crates/horizon-client/src/game/commands/data.rs b/crates/horizon-client/src/game/commands/data.rs index 1d2321a1a..89e3d6397 100644 --- a/crates/horizon-client/src/game/commands/data.rs +++ b/crates/horizon-client/src/game/commands/data.rs @@ -35,6 +35,11 @@ impl Game<'_> { pub(in crate::game) fn entity_nbt(&mut self, t: Target) -> Option { let player_uuid = self.level.player_uuid; Some(match t { + Target::Guest(_) => { + return self + .for_player(t, |g| g.entity_nbt(Target::Player)) + .flatten(); + } Target::Player => { self.player.as_ref()?; self.sync_level(); @@ -142,7 +147,7 @@ impl Game<'_> { set_key(&mut tag, "id", Tag::String(k.as_str().into())); } let new = - entity_nbt::mob_from_nbt(&tag, &self.res.mob_types, id).ok_or_else(stale)?; + entity_nbt::mob_from_nbt(&tag, self.res.mob_types(), id).ok_or_else(stale)?; if let Some(slot) = self.mobs.iter_mut().find(|m| m.id == id) { // Runtime-only state (AI, animation) carries over. let old = std::mem::replace(slot, new); @@ -661,13 +666,20 @@ impl Game<'_> { /// Spawn an entity from its compound (`EntityType.loadEntityRecursive`); /// returns what was created. pub(super) fn spawn_entity_from_nbt(&mut self, tag: Tag) -> Option { - let before = self.next_entity; let key = { let p = entity_nbt::position(&tag)?; ((p.x.floor() as i32) >> 4, (p.z.floor() as i32) >> 4) }; + // Only this stack spawns now: the entities of chunks that finished + // loading wait for the level's entity tick, as in vanilla + // (PersistentEntitySectionManager.processPendingLoads), and must not + // take the ids counted as this stack's. + let mut waiting = std::mem::take(&mut self.world.loaded_entities); + let before = self.next_entity; self.world.loaded_entities.push((key, vec![tag])); self.spawn_loaded_entities(); + waiting.append(&mut self.world.loaded_entities); + self.world.loaded_entities = waiting; if self.next_entity == before { return None; } diff --git a/crates/horizon-client/src/game/commands/effects.rs b/crates/horizon-client/src/game/commands/effects.rs index 3e04f7179..65d6592f7 100644 --- a/crates/horizon-client/src/game/commands/effects.rs +++ b/crates/horizon-client/src/game/commands/effects.rs @@ -59,7 +59,7 @@ impl Game<'_> { if ctx.get("targets").is_some() { self.entities(ctx, "targets", false) } else { - Ok(vec![Target::Player]) + Ok(vec![self.implied_player()]) } } /// `EffectCommands.giveEffect`. @@ -83,10 +83,11 @@ impl Game<'_> { let mut tracker = Tracker::default(); for t in self.living_targets(ctx)? { let changed = match t { - Target::Player => { + Target::Player | Target::Guest(_) => { let mut instance = EffectInstance::new(effect.id, duration, amplifier); instance.visible = particles; - self.potions.effects.add(instance) + self.for_player(t, |g| g.add_player_effect(instance)) + .unwrap_or(false) } Target::Mob(id) => match self.mobs.iter_mut().find(|m| m.id == id) { Some(m) => { @@ -131,16 +132,22 @@ impl Game<'_> { let mut tracker = Tracker::default(); for t in self.living_targets(ctx)? { let changed = match (t, effect) { - (Target::Player, None) => { - let any = !self.potions.effects.list.is_empty(); - self.potions.effects.clear(); - any - } - (Target::Player, Some(e)) => { - let before = self.potions.effects.list.len(); - self.potions.effects.list.retain(|x| x.id != e.id); - self.potions.effects.list.len() != before - } + (Target::Player | Target::Guest(_), None) => self + .for_player(t, |g| { + let any = !g.potions.effects.list.is_empty(); + g.potions.effects.clear(); + g.apply_effect_status(); + any + }) + .unwrap_or(false), + (Target::Player | Target::Guest(_), Some(e)) => self + .for_player(t, |g| { + let before = g.potions.effects.list.len(); + g.potions.effects.list.retain(|x| x.id != e.id); + g.apply_effect_status(); + g.potions.effects.list.len() != before + }) + .unwrap_or(false), (Target::Mob(id), e) => match self.mobs.iter_mut().find(|m| m.id == id) { Some(m) => { let before = m.extra.effects.len(); @@ -185,7 +192,7 @@ impl Game<'_> { return Ok(0); }; let level = Self::int(ctx, "level").unwrap_or(1); - let enchantments = &self.res.stations.enchantments; + let enchantments = &self.res.stations().enchantments; let Some(def) = enchantments.get(&id) else { return Err(err( "argument.resource.not_found", @@ -211,8 +218,8 @@ impl Game<'_> { } continue; }; - let e = &self.res.stations.enchantments; - let compatible = e.can_enchant(&self.res.items, &id, &stack.item) + let e = &self.res.stations().enchantments; + let compatible = e.can_enchant(self.res.items(), &id, &stack.item) && stack .crafting_enchantments() .iter() @@ -263,7 +270,7 @@ impl Game<'_> { } let (name, curse) = self .res - .stations + .stations() .enchantments .full_name(&id, level.max(0) as u32); let style = Style { diff --git a/crates/horizon-client/src/game/commands/execute.rs b/crates/horizon-client/src/game/commands/execute.rs index 1fa67c8b6..221ebb238 100644 --- a/crates/horizon-client/src/game/commands/execute.rs +++ b/crates/horizon-client/src/game/commands/execute.rs @@ -1,7 +1,7 @@ //! `/execute` (ExecuteCommand): walk the parsed redirect chain, forking and //! filtering command sources, then run the final command once per source, //! reporting each result to the `store` callbacks of its source. -use super::{Arg, Context, Game, R, Target, data::DataRef, err}; +use super::{Arg, Context, Dim, Game, R, Target, data::DataRef, err}; use horizon_client::{ command::{CommandError, vanilla}, nbt_path::NbtPath, @@ -23,9 +23,12 @@ pub(in crate::game) struct Source { pub silent: bool, /// Index of the newest `store` in `ExecState::stores`. pub store: Option, - /// `execute in` another dimension than the one Horizon has loaded: none - /// of its chunks are loaded. - pub elsewhere: bool, + /// The level this command source uses. Horizon has only this dimension's + /// chunks loaded in the single-player client. + pub dimension: Dim, + /// The dimension `execute in` named, if any: a server teleports its + /// players there (`TeleportCommand` into the source's level). + pub level: Option, /// The command block whose output this source is /// (`BaseCommandBlock.CloseableCommandBlockSource`). pub block: Option<(i32, i32, i32)>, @@ -70,13 +73,15 @@ impl Game<'_> { /// The source a command typed by the player starts from. pub(in crate::game) fn player_source(&self) -> Source { Source { - entity: Some(Target::Player), + // The server console has no entity (CommandSourceStack of the server). + entity: self.player.is_some().then_some(Target::Player), pos: self.source_position(), rot: self.source_rotation(), eyes: false, silent: false, store: None, - elsewhere: false, + dimension: self.dim, + level: None, block: None, } } @@ -84,18 +89,22 @@ impl Game<'_> { pub(in crate::game) fn server_source(&self) -> Source { Source { entity: None, - pos: self.spawn.map_or([0.0, 64.0, 0.0], |s| [s.x, s.y, s.z]), + pos: self.server_position(), rot: [0.0, 0.0], eyes: false, silent: true, store: None, - elsewhere: false, + dimension: self.dim, + level: None, block: None, } } pub(super) fn target_rotation(&self, t: Target) -> [f32; 2] { match t { Target::Player => [self.yaw, self.pitch], + Target::Guest(conn) => self + .command_guest(conn) + .map_or([0.0; 2], |g| [g.yaw, g.pitch]), Target::Mob(id) => self .mobs .iter() @@ -130,6 +139,7 @@ impl Game<'_> { .player .as_ref() .map_or(1.62, |p| f64::from(p.dimensions.eye_height)), + Target::Guest(conn) => self.command_guest(conn).map_or(1.62, |g| g.eye_height()), Target::Mob(id) => self .mobs .iter() @@ -440,7 +450,8 @@ impl Game<'_> { Some(Source { pos: self.target_position(t)?, rot: self.target_rotation(t), - elsewhere: false, + dimension: self.dim, + level: None, ..s }) }) @@ -531,8 +542,8 @@ impl Game<'_> { ..s }], "in" => { - // Horizon keeps one dimension loaded: in another one, no - // chunk is loaded (as in vanilla without a player there). + // CommandSourceStack.withLevel scales the source position; + // absolute coordinates passed to /tp are unaffected. let Some(Arg::Resource(dim)) = c.get("dimension") else { return Ok(vec![s]); }; @@ -542,8 +553,13 @@ impl Game<'_> { ) { return Err(err("argument.dimension.invalid", &[dim])); } + let dimension = Dim::from_id(dim); + let coordinate_scale = |dim| if dim == Dim::Nether { 8.0 } else { 1.0 }; + let scale = coordinate_scale(s.dimension) / coordinate_scale(dimension); vec![Source { - elsewhere: dim != self.dim.id(), + pos: [s.pos[0] * scale, s.pos[1], s.pos[2] * scale], + dimension, + level: Some(super::super::Dim::from_id(dim)), ..s }] } @@ -626,7 +642,7 @@ impl Game<'_> { }) .collect() } - fn target_with_uuid(&self, uuid: [i32; 4]) -> Option { + pub(super) fn target_with_uuid(&self, uuid: [i32; 4]) -> Option { if Some(uuid) == self.level.player_uuid { return Some(Target::Player); } @@ -649,7 +665,7 @@ impl Game<'_> { if state == self.world.air { continue; } - let facts = self.res.facts.get(state); + let facts = self.res.facts().get(state); let blocks = facts.legacy_solid || facts.fluid != horizon_client::blocks::Fluid::None; let leaves = self .pack @@ -785,14 +801,16 @@ impl Game<'_> { let p = self .coords(c, "pos") .map(|co| co.block(self.source_position(), self.source_rotation())); - return Ok(flag(p.is_some_and(|p| { - self.world.chunks.contains_key(&(p[0] >> 4, p[2] >> 4)) - }))); + let dimension = self.command_source.map_or(self.dim, |s| s.dimension); + return Ok(flag( + dimension == self.dim + && p.is_some_and(|p| self.world.chunks.contains_key(&(p[0] >> 4, p[2] >> 4))), + )); } if c.has("dimension") { - return Ok(flag( - matches!(c.get("dimension"), Some(Arg::Resource(d)) if d == self.dim.id()), - )); + let dimension = self.command_source.map_or(self.dim, |s| s.dimension); + return Ok(flag(matches!(c.get("dimension"), Some(Arg::Resource(d)) + if d.as_str() == dimension.id()))); } if c.has("block") { let p = self.block_pos_arg(c, "pos")?; @@ -941,7 +959,7 @@ impl Game<'_> { let chunk = self.world.chunks.get(&(p[0] >> 4, p[2] >> 4))?; Some( self.res - .biomes + .biomes() .get(chunk.biome(p[0], p[1], p[2])) .name .clone(), @@ -964,7 +982,7 @@ impl Game<'_> { .resources .join(format!("data/{ns}/tags/worldgen/biome/{path}.json")); let Ok(bytes) = std::fs::read(file) else { - return self.res.mob_data.biome_has(tag, biome); + return self.res.mob_data().biome_has(tag, biome); }; let v: serde_json::Value = serde_json::from_slice(&bytes).unwrap_or_default(); v["values"].as_array().is_some_and(|a| { diff --git a/crates/horizon-client/src/game/commands/extra.rs b/crates/horizon-client/src/game/commands/extra.rs index a90648043..7a4accb0d 100644 --- a/crates/horizon-client/src/game/commands/extra.rs +++ b/crates/horizon-client/src/game/commands/extra.rs @@ -586,7 +586,7 @@ impl Game<'_> { ); let hit = traverse_blocks(eye, end, 256, |b: BlockPos| { let state = self.world.block(b.x, b.y, b.z); - (!self.res.facts.is_air(state)).then_some([b.x, b.y, b.z]) + (!self.res.facts().is_air(state)).then_some([b.x, b.y, b.z]) }); hit.ok().flatten().unwrap_or([ end.x.floor() as i32, diff --git a/crates/horizon-client/src/game/commands/items.rs b/crates/horizon-client/src/game/commands/items.rs index c71eebc68..fde97764d 100644 --- a/crates/horizon-client/src/game/commands/items.rs +++ b/crates/horizon-client/src/game/commands/items.rs @@ -375,7 +375,11 @@ impl Game<'_> { } else { 0 }; - let max = self.res.items.get(&stack.item).map_or(64, |i| i.max_stack); + let max = self + .res + .items() + .get(&stack.item) + .map_or(64, |i| i.max_stack); stack.count = (base + n).clamp(0, max as i32) as u32; } "minecraft:limit_count" => { @@ -389,7 +393,11 @@ impl Game<'_> { } } "minecraft:set_damage" => { - let max = self.res.items.get(&stack.item).map_or(0, |i| i.max_damage); + let max = self + .res + .items() + .get(&stack.item) + .map_or(0, |i| i.max_damage); if max > 0 { let f = match m["damage"].as_f64() { Some(f) => f as f32, @@ -587,7 +595,7 @@ impl Game<'_> { if let Some(Tag::Int(n)) = s.data().and_then(|d| d.other("minecraft:max_stack_size")) { return (*n).clamp(1, 99) as u32; } - self.res.items.get(&s.item).map_or(64, |i| i.max_stack) + self.res.items().get(&s.item).map_or(64, |i| i.max_stack) } /// `ResourceOrIdArgument.getItemModifier`: an inline modifier (SNBT) or /// a registered one. diff --git a/crates/horizon-client/src/game/commands/place.rs b/crates/horizon-client/src/game/commands/place.rs index 3e447a7fb..016f6e2fe 100644 --- a/crates/horizon-client/src/game/commands/place.rs +++ b/crates/horizon-client/src/game/commands/place.rs @@ -53,13 +53,7 @@ impl Game<'_> { let Ok(raw) = Compression::Gzip.decode(&bytes, 1 << 28) else { continue; }; - let Ok((named, _)) = nbt::read_named( - &raw, - Limits { - max_bytes: 1 << 28, - ..Limits::default() - }, - ) else { + let Ok((named, _)) = nbt::read_named(&raw, Limits::templates()) else { continue; }; return Some(Arc::new(StructureTemplate::from_nbt( @@ -79,10 +73,10 @@ impl Game<'_> { } /// `PlaceCommand.checkLoaded`: every chunk of the inclusive range. fn place_check_loaded(&self, min: [i32; 2], max: [i32; 2]) -> R<()> { - let elsewhere = self.command_source.is_some_and(|s| s.elsewhere); + let dimension = self.command_source.map_or(self.dim, |s| s.dimension); for cx in min[0]..=max[0] { for cz in min[1]..=max[1] { - if elsewhere || !self.world.chunks.contains_key(&(cx, cz)) { + if dimension != self.dim || !self.world.chunks.contains_key(&(cx, cz)) { return Err(err("argument.pos.unloaded", &[])); } } diff --git a/crates/horizon-client/src/game/commands/players.rs b/crates/horizon-client/src/game/commands/players.rs new file mode 100644 index 000000000..d71c219a8 --- /dev/null +++ b/crates/horizon-client/src/game/commands/players.rs @@ -0,0 +1,210 @@ +//! Commands acting on the other players of a hosted world or a server +//! (EntitySelector.findPlayers and EntityArgument's player targets). The +//! player in place (the host's, or the guest whose turn it is) is +//! `Target::Player`; every other guest is `Target::Guest`, found by name, +//! UUID or selector, and what a command does to them runs with their state +//! swapped in (`for_player`), as their own turn, what it tells them going to +//! their chat. +use super::{Game, Target}; +use horizon_client::{command::args::Selector, lan::guest::Guest, text::Text}; +use horizon_protocol::packets::play::{self, Clientbound as C}; + +impl Target { + /// A player, in place or not (`instanceof ServerPlayer`). + pub(in crate::game) fn is_player(self) -> bool { + matches!(self, Target::Player | Target::Guest(_)) + } +} + +/// GameType.getName for a guest's game mode id. +fn mode_name(id: i32) -> &'static str { + match id { + 1 => "creative", + 2 => "adventure", + 3 => "spectator", + _ => "survival", + } +} + +impl Game<'_> { + /// The guest `conn`, while the hub is in `self.lan`. + pub(in crate::game) fn command_guest(&self, conn: u32) -> Option<&Guest> { + self.lan.as_ref()?.hub.guest(conn) + } + + /// `PlayerList.getPlayers` but the player in place: the guests who + /// joined the world. + fn command_players(&self) -> impl Iterator { + let acting = self.lan_acting; + self.lan + .iter() + .flat_map(|h| h.hub.guests.iter()) + .filter(move |g| g.placed && Some(g.conn) != acting) + } + + /// The entity the command runs as (`CommandSourceStack.getEntity`): a + /// command typed by the player in place runs as them, the server + /// console's as nobody. + pub(in crate::game) fn source_entity(&self) -> Option { + match self.command_source { + Some(s) => s.entity, + None => self.player.is_some().then_some(Target::Player), + } + } + + /// `CommandSourceStack.getPlayerOrException` for the commands whose + /// player argument is optional: the source's player (the player in + /// place when it has none, as before). + pub(in crate::game) fn implied_player(&self) -> Target { + self.source_entity() + .filter(|t| t.is_player()) + .unwrap_or(Target::Player) + } + + /// `PlayerList.getPlayerByName`: names are not case sensitive. + pub(super) fn player_named(&self, name: &str) -> Option { + if self.player.is_some() && name.eq_ignore_ascii_case(self.acting_player_name()) { + return Some(Target::Player); + } + self.command_players() + .find(|g| g.name.eq_ignore_ascii_case(name)) + .map(|g| Target::Guest(g.conn)) + } + + /// `EntitySelector.findEntities` with a UUID: the player with it, else + /// the entity. + pub(super) fn target_by_uuid(&self, uuid: u128) -> Option { + let own = self.lan_acting_uuid.or_else(|| { + self.level.player_uuid.map(|[a, b, c, d]| { + (u128::from(a as u32) << 96) + | (u128::from(b as u32) << 64) + | (u128::from(c as u32) << 32) + | u128::from(d as u32) + }) + }); + if self.player.is_some() && own == Some(uuid) { + return Some(Target::Player); + } + if let Some(g) = self.command_players().find(|g| g.uuid == uuid) { + return Some(Target::Guest(g.conn)); + } + let words = [96, 64, 32, 0].map(|shift| (uuid >> shift) as u32 as i32); + self.target_with_uuid(words) + } + + /// The guests a selector looks at, in join order (`PlayerList.getPlayers`), + /// the one in place as `Target::Player`: all of them, or those in the + /// source's level when it limits the search to a place + /// (`isWorldLimited`); `@e` and `@n` skip the dead (`Entity::isAlive`). + pub(super) fn player_candidates(&self, s: &Selector) -> Vec { + let limited = s.distance.is_some() + || s.pos.iter().any(Option::is_some) + || s.delta.iter().any(Option::is_some); + let alive = matches!(s.kind, 'e' | 'n'); + let here = self.dim.id(); + let in_place = self.lan_acting.filter(|_| self.player.is_some()); + self.lan + .iter() + .flat_map(|h| h.hub.guests.iter()) + .filter(|g| g.placed) + .filter(|g| !limited || g.dimension == here || Some(g.conn) == in_place) + .filter(|g| !alive || (!g.dead && g.health > 0.0)) + .map(|g| { + if Some(g.conn) == in_place { + Target::Player + } else { + Target::Guest(g.conn) + } + }) + .collect() + } + + /// A guest's experience level and game mode, for `level=` and `gamemode=`. + pub(super) fn guest_level_and_mode(&self, conn: u32) -> Option<(i32, &'static str)> { + self.command_guest(conn) + .map(|g| (g.xp_level, mode_name(g.game_mode))) + } + + /// `Player.getDisplayName`: the name as its team shows it, suggesting a + /// `/tell`, showing the player on hover, inserting the name. + pub(in crate::game) fn player_text(&self, name: &str, uuid: u128) -> Text { + let shown = match self.score.board.players_team(name) { + Some(team) => team.formatted_name(Text::literal(name)), + None => Text::literal(name), + }; + shown.with_component_fields(vec![ + ( + "click_event".into(), + serde_json::json!({"action": "suggest_command", "command": format!("/tell {name} ")}), + ), + ( + "hover_event".into(), + serde_json::json!({"action": "show_entity", "id": "minecraft:player", + "uuid": horizon_protocol::crypto::uuid_string(uuid), "name": name}), + ), + ("insertion".into(), name.into()), + ]) + } + + /// Run `f` for the player `t` (None for anything else): in place, or + /// with the guest's state swapped in, in their level, as their own turn. + /// What `f` tells the player (`system_message`) goes to their chat; the + /// command's own feedback is the caller's to send. + pub(in crate::game) fn for_player( + &mut self, + t: Target, + f: impl FnOnce(&mut Self) -> T, + ) -> Option { + let conn = match t { + Target::Player => return Some(f(self)), + Target::Guest(conn) if self.lan_acting == Some(conn) => return Some(f(self)), + Target::Guest(conn) => conn, + _ => return None, + }; + let mut host = self.lan.take()?; + let back = self.dim; + let theirs = host + .hub + .guest(conn) + .map(|g| super::super::Dim::from_id(&g.dimension)); + if let Some(dim) = theirs.filter(|d| *d != back) + && let Err(e) = self.enter_level(Some(&mut host), dim) + { + log::error!("could not open {}: {e}", dim.id()); + } + let Some(swap) = self.enter_guest(&mut host, conn) else { + if self.dim != back + && let Err(e) = self.enter_level(Some(&mut host), back) + { + log::error!("could not reopen {}: {e}", back.id()); + } + self.lan = Some(host); + return None; + }; + self.lan = Some(host); + let source = self.chat_capture.replace(Vec::new()); + let out = f(self); + let told = std::mem::replace(&mut self.chat_capture, source).unwrap_or_default(); + let mut host = self + .lan + .take() + .expect("the hub is back after a player's turn"); + self.leave_guest(&mut host, swap); + for line in told { + host.hub.send_to( + conn, + &C::SystemChat(play::SystemChat { + content: horizon_client::lan::data::component_nbt(&line.component()), + overlay: false, + }), + ); + } + if self.dim != back + && let Err(e) = self.enter_level(Some(&mut host), back) + { + log::error!("could not reopen {}: {e}", back.id()); + } + self.lan = Some(host); + Some(out) + } +} diff --git a/crates/horizon-client/src/game/commands/terrain_cmds.rs b/crates/horizon-client/src/game/commands/terrain_cmds.rs index cee7a1d72..46df0ce7d 100644 --- a/crates/horizon-client/src/game/commands/terrain_cmds.rs +++ b/crates/horizon-client/src/game/commands/terrain_cmds.rs @@ -174,7 +174,7 @@ impl Game<'_> { if volume > i64::from(limit) { return Err(err("commands.fillbiome.toobig", &[&limit, &volume])); } - let Some(target) = self.res.biomes.list.iter().position(|b| b.name == biome) else { + let Some(target) = self.res.biomes().list.iter().position(|b| b.name == biome) else { return Err(err( "argument.resource.not_found", &[&biome, &"minecraft:worldgen/biome"], @@ -201,7 +201,7 @@ impl Game<'_> { continue; }; let current = chunk.biome(x, y, z); - let current_name = &self.res.biomes.list[usize::from(current)].name; + let current_name = &self.res.biomes().list[usize::from(current)].name; if let Some(f) = &filter && !self.biome_matches(f, current_name) { @@ -474,11 +474,11 @@ impl Game<'_> { let json = horizon_client::scoreboard::tag_to_json(&tag); let drops = self .res - .items + .items() .inline_loot_table(&json, &mut self.world.random); (drops, None) } else { - if !self.res.items.has_loot_table(&table) { + if !self.res.items().has_loot_table(&table) { return Err(err("argument.resource_or_id.failed_to_parse", &[&table])); } let held = if id == "loot.fish" { @@ -486,14 +486,14 @@ impl Game<'_> { } else { None }; - let drops = match self.res.items.loot_sequence(&table) { + let drops = match self.res.items().loot_sequence(&table) { Some(seq) => { let random = self.rand_sequences.get(&seq, self.level.seed); self.res - .items + .items() .loot_table(&table, false, held.as_ref(), random) } - None => self.res.items.loot_table( + None => self.res.items().loot_table( &table, false, held.as_ref(), @@ -510,7 +510,7 @@ impl Game<'_> { }; let ty = self.target_type(t); let table = format!("minecraft:entities/{}", ty.trim_start_matches("minecraft:")); - if t == Target::Player || !self.res.items.has_loot_table(&table) { + if t == Target::Player || !self.res.items().has_loot_table(&table) { return Err(err( "commands.drop.no_loot_table.entity", &[&self.target_name(t).plain()], @@ -521,14 +521,14 @@ impl Game<'_> { None | Some(Some(Target::Player)) ); let held = self.slot_get(&super::items::Slot::Inv(self.selected)); - let drops = match self.res.items.loot_sequence(&table) { + let drops = match self.res.items().loot_sequence(&table) { Some(seq) => { let random = self.rand_sequences.get(&seq, self.level.seed); self.res - .items + .items() .entity_drops(&ty, by_player, held.as_ref(), random) } - None => self.res.items.entity_drops( + None => self.res.items().entity_drops( &ty, by_player, held.as_ref(), @@ -543,21 +543,21 @@ impl Game<'_> { let state = self.world.block(p[0], p[1], p[2]); let name = self.data_block_name(p); let table = format!("minecraft:blocks/{}", name.trim_start_matches("minecraft:")); - if !self.res.items.has_loot_table(&table) { + if !self.res.items().has_loot_table(&table) { return Err(err("commands.drop.no_loot_table.block", &[&name])); } let tool = self.tool_stack(ctx)?; - let drops = match self.res.items.loot_sequence(&table) { + let drops = match self.res.items().loot_sequence(&table) { Some(seq) => { let random = self.rand_sequences.get(&seq, self.level.seed); - self.res.items.block_drops( + self.res.items().block_drops( self.pack.assets.registry, state, tool.as_ref(), random, ) } - None => self.res.items.block_drops( + None => self.res.items().block_drops( self.pack.assets.registry, state, tool.as_ref(), @@ -624,7 +624,7 @@ impl Game<'_> { && cur.damage == left.damage && cur.extra == left.extra => { - let max = self.res.items.get(&cur.item).map_or(64, |i| i.max_stack); + let max = self.res.items().get(&cur.item).map_or(64, |i| i.max_stack); let take = left.count.min(max.saturating_sub(cur.count)); if take > 0 { cur.count += take; diff --git a/crates/horizon-client/src/game/commands/text_commands.rs b/crates/horizon-client/src/game/commands/text_commands.rs index da8485849..dead07d71 100644 --- a/crates/horizon-client/src/game/commands/text_commands.rs +++ b/crates/horizon-client/src/game/commands/text_commands.rs @@ -1,6 +1,7 @@ //! `/tellraw` (TellRawCommand) and `/title` (TitleCommand). use super::{Arg, Context, Game, R, Target, err}; -use horizon_client::text::Text; +use horizon_client::{hud_overlays::Titles, lan::data::component_nbt, text::Text}; +use horizon_protocol::packets::play::{self, Clientbound as C}; impl Game<'_> { /// ComponentArgument with selectors resolved for the source @@ -153,30 +154,45 @@ impl Game<'_> { let name = self.target_name(Target::Player); let single = n == 1; let key = if ctx.has("clear") { - self.titles.clear(); + self.titles_or_packet(Titles::clear, C::ClearTitles(play::Bool1 { value: false })); "cleared" } else if ctx.has("reset") { - self.titles.clear(); - self.titles.reset_times(); + self.titles_or_packet( + |t| { + t.clear(); + t.reset_times(); + }, + C::ClearTitles(play::Bool1 { value: true }), + ); "reset" } else if ctx.has("times") { let get = |k: &str| match ctx.get(k) { Some(Arg::Int(v)) => *v, _ => -1, }; - self.titles - .set_times(get("fadeIn"), get("stay"), get("fadeOut")); + let (fade_in, stay, fade_out) = (get("fadeIn"), get("stay"), get("fadeOut")); + self.titles_or_packet( + |t| t.set_times(fade_in, stay, fade_out), + C::SetTitlesAnimation(play::TitlesAnimation { + fade_in, + stay, + fade_out, + }), + ); "times" } else { let text = self.component_arg(ctx, "title")?; + let sent = play::Text { + text: component_nbt(&text.component()), + }; if ctx.has("subtitle") { - self.titles.set_subtitle(text); + self.titles_or_packet(|t| t.set_subtitle(text), C::SetSubtitleText(sent)); "show.subtitle" } else if ctx.has("actionbar") { - self.titles.set_overlay(text, false); + self.titles_or_packet(|t| t.set_overlay(text, false), C::SetActionBarText(sent)); "show.actionbar" } else { - self.titles.set_title(text); + self.titles_or_packet(|t| t.set_title(text), C::SetTitleText(sent)); "show.title" } }; @@ -191,4 +207,13 @@ impl Game<'_> { self.feedback(message); Ok(n as i32) } + /// The player the title is for: the guest acting now is sent the packet + /// (TitleCommand's `connection.send`), the window's own player shows it. + fn titles_or_packet(&mut self, local: impl FnOnce(&mut Titles), packet: C) { + if self.lan_acting.is_some() { + self.guest_packets.push(packet); + } else { + local(&mut self.titles); + } + } } diff --git a/crates/horizon-client/src/game/commands/world_cmds.rs b/crates/horizon-client/src/game/commands/world_cmds.rs index 578153247..ef2adf80c 100644 --- a/crates/horizon-client/src/game/commands/world_cmds.rs +++ b/crates/horizon-client/src/game/commands/world_cmds.rs @@ -1,7 +1,6 @@ //! `/recipe`, `/damage`, `/rotate`, `/swing`, `/playsound`, `/stopsound`, //! `/particle`, `/attribute`, `/ride` and `/spreadplayers`. use super::{Arg, Context, Game, R, Target, err}; -use crate::game::chat::PLAYER_NAME; use horizon_client::{ mobs::{Damage, DamageKind, Who}, scoreboard_cmd::{Holder, Tracker}, @@ -32,7 +31,7 @@ impl Game<'_> { /// Every recipe id of the recipe manager (what `/recipe` resolves and suggests). pub(super) fn learnable_recipes(&self) -> Vec { - self.res.stations.recipe_index.ids().cloned().collect() + self.res.stations().recipe_index.ids().cloned().collect() } pub(super) fn cmd_recipe(&mut self, id: &str, ctx: &Context) -> R { let targets = self.entities(ctx, "targets", true)?; @@ -53,7 +52,7 @@ impl Game<'_> { // displays (a transmute recipe has one per material count). let mut n = 0; // ServerPlayer.awardRecipes / resetRecipes: the displays changed. - let index = &self.res.stations.recipe_index; + let index = &self.res.stations().recipe_index; n += if give { let (count, unlocked) = self.level.recipe_book.add_recipes(index, &recipes); self.recipe_unlocked(&unlocked); @@ -313,7 +312,7 @@ impl Game<'_> { (pos, volume) }; self.audio.play_on(source, &sound, Some(at), v, pitch); - tracker.mark(&holder(Text::literal(PLAYER_NAME))); + tracker.mark(&holder(Text::literal(self.acting_player_name()))); } let name = Text::literal(sound.clone()); let (message, total) = tracker.message( @@ -1268,7 +1267,7 @@ impl Game<'_> { fn spread_spawn_y(&self, p: [f64; 2], max_y: i32) -> i32 { let (x, z) = (p[0].floor() as i32, p[1].floor() as i32); let air = |y: i32| { - let name: &str = &self.res.facts.get(self.world.block(x, y, z)).name; + let name: &str = &self.res.facts().get(self.world.block(x, y, z)).name; matches!( name, "minecraft:air" | "minecraft:cave_air" | "minecraft:void_air" @@ -1293,16 +1292,16 @@ impl Game<'_> { fn spread_safe(&self, p: [f64; 2], max_y: i32) -> bool { let y = self.spread_spawn_y(p, max_y) - 1; let (x, z) = (p[0].floor() as i32, p[1].floor() as i32); - let name = self.res.facts.get(self.world.block(x, y, z)).name.clone(); + let name = self.res.facts().get(self.world.block(x, y, z)).name.clone(); let liquid = matches!(&*name, "minecraft:water" | "minecraft:lava"); let teleportable = self .res - .items + .items() .block_tags .get("entities_can_teleport_to") .or_else(|| { self.res - .items + .items() .block_tags .get("minecraft:entities_can_teleport_to") }) diff --git a/crates/horizon-client/src/game/composter.rs b/crates/horizon-client/src/game/composter.rs index 3e4cb7169..3f57229ad 100644 --- a/crates/horizon-client/src/game/composter.rs +++ b/crates/horizon-client/src/game/composter.rs @@ -21,7 +21,7 @@ impl Game<'_> { } /// The item's `compostable` layers provider. fn compostable(&self, stack: &ItemStack) -> Option { - self.res.items.get(&stack.item)?.compostable.clone() + self.res.items().get(&stack.item)?.compostable.clone() } /// `ComposterBlock.addLayer` and level event 1500: the provider is /// evaluated in the composter's block-interaction loot context (its diff --git a/crates/horizon-client/src/game/consume.rs b/crates/horizon-client/src/game/consume.rs new file mode 100644 index 000000000..50b1fa814 --- /dev/null +++ b/crates/horizon-client/src/game/consume.rs @@ -0,0 +1,330 @@ +//! Finishing a consumable (`Consumable.onConsume` and the use remainder), +//! its particles and sounds, and `TeleportRandomlyConsumeEffect` (chorus +//! fruit) for the local player. The level random stands in for the player's +//! own, as for every other player draw. +use super::{Dim, Game}; +use horizon_client::{ + items::{ConsumeEffect, ItemInfo, ItemStack}, + mobs::Who, + particles::{ParticleOptions, ParticleType}, +}; +use horizon_core::{ + geometry::Vec3, position::BlockPos, random::LegacyRandom, random::RandomSource, +}; +use horizon_world::{ + consumable::{Consumable, EmissionWorld, UseAnimation}, + teleport_consume::{TeleportConsume, TeleportConsumeWorld}, +}; +use std::convert::Infallible; + +type ItemConsumable = Consumable; + +impl Game<'_> { + /// `Consumable.emitParticlesAndSounds(user.getRandom(), user, stack, count)`. + pub(super) fn emit_consume( + &mut self, + consumable: &ItemConsumable, + stack: &ItemStack, + count: i32, + ) { + let Ok(()) = consumable.emit(&mut Emitter(self), (), stack.clone(), count); + } + /// `LivingEntity.completeUsingItem` for a consumable: `Consumable.onConsume`, + /// then `ItemStack.applyAfterUseComponentSideEffects`' use remainder. + pub(super) fn finish_consuming( + &mut self, + info: &ItemInfo, + consumable: &ItemConsumable, + stack: &ItemStack, + ) { + self.emit_consume(consumable, stack, 16); + // ServerPlayer: the used statistic and the consume_item trigger. + self.stats.add("used", &stack.item, 1); + self.trigger_item("minecraft:consume_item", stack); + // FoodProperties.onConsume, the player's one ConsumableListener. + if let Some((nutrition, saturation)) = info.food { + let at = self.feet(); + let pitch = self.world.random.triangle_float(1.0, 0.4); + self.audio.play(&consumable.sound, at, 1.0, pitch); + if let Some(p) = self.player.as_mut() { + // FoodData.eat(nutrition, saturation) + let food = &mut p.health.food; + food.food = (food.food + nutrition as i32).clamp(0, 20); + food.saturation = (food.saturation + saturation).clamp(0.0, food.food as f32); + } + // Mth.randomBetween(random, 0.9, 1.0) + let pitch = self.world.random.next_float() * (1.0f32 - 0.9f32) + 0.9; + self.audio + .play("minecraft:entity.player.burp", self.feet(), 0.5, pitch); + } + for effect in &consumable.effects { + self.apply_consume_effect(effect); + } + let event = if consumable.animation == UseAnimation::Drink { + "minecraft:drink" + } else { + "minecraft:eat" + }; + self.post_entity_event(event, Who::Player); + // ItemStack.consume, then UseRemainder.convertIntoRemainder: nothing + // with infinite materials, the hand when emptied, else the inventory + // (ServerPlayer.handleExtraItemsCreatedOnUse) or a drop. + self.consume_held(); + if let Some(remainder) = &info.use_remainder + && !self.creative + { + let remainder = ItemStack::new(remainder, 1); + if self.held().is_none() { + *self.held_mut() = Some(remainder); + } else if let Some(rest) = self.give(remainder) { + self.player_drop(rest); + } + } + } + fn feet(&self) -> Option<[f64; 3]> { + self.player + .as_ref() + .map(|p| [p.position.x, p.position.y, p.position.z]) + } + /// `TeleportRandomlyConsumeEffect.apply` for the local player. + pub(super) fn teleport_randomly(&mut self, teleport: &TeleportConsume) { + if self.player.is_none() { + return; + } + let dim = self.dim; + let Ok(_) = teleport.apply(&mut Teleporter(self), dim); + } + /// `LivingEntity.randomTeleport(x, y, z, true, avoid)`: down to the first + /// block entities can teleport onto, inside the border, in a loaded chunk. + fn random_teleport(&mut self, target: Vec3, avoid: &str) -> bool { + let [min_x, min_z, max_x, max_z] = self.border.bounds(); + // WorldBorder.clampVec3ToBound (Mth.clamp, 1.0E-5F inside the far edge). + let clamp = |v: f64, low: f64, high: f64| if v < low { low } else { v.min(high) }; + let x = clamp(target.x, min_x, max_x - f64::from(1.0e-5_f32)); + let z = clamp(target.z, min_z, max_z - f64::from(1.0e-5_f32)); + let mut y = target.y; + let mut pos = BlockPos::containing(Vec3::new(x, y, z)); + if !self.world.loaded(pos.x, pos.z) { + return false; + } + while pos.y > self.dim.min_y() { + pos.y -= 1; + let state = self.world.block(pos.x, pos.y, pos.z); + if self.state_in_tag("minecraft:entities_can_teleport_to", state) { + return self.check_position_and_teleport(Vec3::new(x, y, z), state, avoid); + } + y -= 1.0; + } + false + } + /// `LivingEntity.checkPositionAndTeleport`, then `ServerPlayer.teleportTo` + /// (the motion and rotation kept) and entity event 46. + fn check_position_and_teleport( + &mut self, + at: Vec3, + ground: horizon_world::registry::BlockStateId, + avoid: &str, + ) -> bool { + if self.state_in_tag(avoid, ground) { + return false; + } + let entities = self.happy_ghast_player_collisions(); + let Some(player) = self.player.as_mut() else { + return false; + }; + let bounds = player.dimensions.bounds(at); + let free = horizon_client::motion::MotionWorld { + entities: &entities, + player, + scene: &mut self.world, + assets: &self.pack.assets, + collision_binding: self.collision, + resources: self.motion, + } + .no_player_collision(bounds) + .unwrap_or(false); + // LevelReader.containsAnyLiquid and findBlocksIn: the blocks from the + // box's minimum corner to the one holding its maximum corner. + let (lo, hi) = ( + BlockPos::containing(bounds.min_position()), + BlockPos::containing(bounds.max_position()), + ); + let blocks = (lo.x..=hi.x) + .flat_map(|x| (lo.y..=hi.y).flat_map(move |y| (lo.z..=hi.z).map(move |z| (x, y, z)))); + let states: Vec<_> = blocks.map(|(x, y, z)| self.world.block(x, y, z)).collect(); + let liquid = states + .iter() + .any(|&s| self.res.facts().get(s).fluid != horizon_client::blocks::Fluid::None); + if !free || liquid || states.iter().any(|&s| self.state_in_tag(avoid, s)) { + return false; + } + if let Some(p) = self.player.as_mut() { + p.position = at; + } + // The position packet moves the old position along (absolute xyz). + self.previous_position = at; + self.portal_particles(); + true + } + fn state_in_tag(&self, tag: &str, state: horizon_world::registry::BlockStateId) -> bool { + let name = &self.res.facts().get(state).name; + self.res + .items() + .block_tags + .get(tag) + .is_some_and(|s| s.contains(&**name)) + } + /// `LivingEntity.handleEntityEvent(46)`: 128 portal particles from the old + /// to the new position, from the client player's random (the particle + /// engine's here). + fn portal_particles(&mut self) { + let Some(p) = self.player.as_ref() else { + return; + }; + let (from, to) = (self.previous_position, p.position); + let (width, height) = ( + f64::from(p.dimensions.width), + f64::from(p.dimensions.height), + ); + let options = ParticleOptions::simple(ParticleType::Portal); + for i in 0..128 { + let d = f64::from(i) / 127.0; + let r = self.particles.random(); + let v = [0; 3].map(|_| f64::from((r.next_float() - 0.5) * 0.2)); + let x = from.x + d * (to.x - from.x) + (r.next_double() - 0.5) * width * 2.0; + let y = from.y + d * (to.y - from.y) + r.next_double() * height; + let z = from.z + d * (to.z - from.z) + (r.next_double() - 0.5) * width * 2.0; + self.particles.add(&options, [x, y, z], v); + } + } + /// `LivingEntity.stopRiding`. + pub(super) fn stop_riding(&mut self) { + let Some(id) = self.riding else { + return; + }; + if let Some(i) = self.boats.iter().position(|b| b.id == id) { + self.dismount(i); + } else if let Some(i) = self.carts.iter().position(|c| c.id == id) { + self.dismount_cart(i); + } else { + self.dismount_mount(); + } + } +} + +/// `Consumable.emitParticlesAndSounds` against the local player. +struct Emitter<'a, 'g>(&'a mut Game<'g>); +impl EmissionWorld for Emitter<'_, '_> { + type Error = Infallible; + type Stack = ItemStack; + type Sound = String; + type Random = (); + type Source = LegacyRandom; + fn random_source(&mut self, (): ()) -> &mut LegacyRandom { + &mut self.0.world.random + } + /// `LivingEntity.spawnItemParticles`. + fn spawn_item_particles(&mut self, stack: ItemStack, count: i32) -> Result<(), Infallible> { + self.0.eat_particles(&stack, count.max(0) as u32); + Ok(()) + } + fn overrides_consume_sound(&self) -> bool { + false + } + fn consume_sound(&mut self, _: ItemStack) -> Result { + unreachable!("players play the consumable's own sound") + } + /// `Entity.playSound`: at the player's position. + fn play_sound(&mut self, sound: String, volume: f32, pitch: f32) -> Result<(), Infallible> { + let at = self.0.feet(); + self.0.audio.play(&sound, at, volume, pitch); + Ok(()) + } +} + +/// The local player as `TeleportRandomlyConsumeEffect`'s user. +struct Teleporter<'a, 'g>(&'a mut Game<'g>); +impl Teleporter<'_, '_> { + fn at(&self) -> Vec3 { + self.0.player.as_ref().map_or(Vec3::ZERO, |p| p.position) + } +} +impl TeleportConsumeWorld for Teleporter<'_, '_> { + type Error = Infallible; + type Level = Dim; + type Random = (); + type Source = LegacyRandom; + fn position_field(&self) -> Vec3 { + self.at() + } + fn get_random(&mut self) -> Result<(), Infallible> { + Ok(()) + } + fn random_source(&mut self, (): ()) -> &mut LegacyRandom { + &mut self.0.world.random + } + fn min_y(&mut self, level: Dim) -> Result { + Ok(level.min_y()) + } + fn logical_height(&mut self, level: Dim) -> Result { + Ok(level.logical_height()) + } + fn is_passenger(&mut self) -> Result { + Ok(self.0.riding.is_some()) + } + fn stop_riding(&mut self) -> Result<(), Infallible> { + self.0.stop_riding(); + Ok(()) + } + fn position(&mut self) -> Result { + Ok(self.at()) + } + fn random_teleport(&mut self, target: Vec3) -> Result { + Ok(self + .0 + .random_teleport(target, "minecraft:consumable_does_not_teleport_to")) + } + fn teleport_event(&mut self, _: Dim, old: Vec3) -> Result<(), Infallible> { + let ctx = self.0.who_context(Some(Who::Player), None); + self.0 + .post_game_event("minecraft:teleport", [old.x, old.y, old.z], ctx); + Ok(()) + } + fn is_fox(&self) -> bool { + false + } + fn teleport_sound(&mut self, _: Dim, at: Vec3, _: bool) -> Result<(), Infallible> { + self.0.audio.play( + "minecraft:item.chorus_fruit.teleport", + Some([at.x, at.y, at.z]), + 1.0, + 1.0, + ); + Ok(()) + } + fn block_position(&mut self) -> Result { + Ok(BlockPos::containing(self.at())) + } + fn directional_particles( + &mut self, + _: Dim, + origin: BlockPos, + offset: i32, + ) -> Result<(), Infallible> { + self.0 + .level_event_at(2017, [origin.x, origin.y, origin.z], offset); + Ok(()) + } + fn reset_fall_distance(&mut self) -> Result<(), Infallible> { + if let Some(p) = self.0.player.as_mut() { + p.fall_distance = 0.0; + } + Ok(()) + } + fn reset_impulse(&mut self) -> Result<(), Infallible> { + if let Some(p) = self.0.player.as_mut() { + p.impulse.reset(); + } + Ok(()) + } +} diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index cf7510065..212ec0505 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -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", @@ -54,12 +55,23 @@ pub struct Flight { /// Reply with every frame time too (`fly ... PITCH raw`). raw: bool, reply: Sender, + /// The world's smoothed tick time each frame (the F3 server line): the + /// server thread's when single player runs on one. + tick_ms: Vec, } impl Game<'_> { /// Move the camera along the running flight; reply when it ends. pub(super) fn advance_flight(&mut self) { - let Some(flight) = &self.flight else { return }; + let tick_ms = self + .integrated + .as_ref() + .map_or(self.debug_charts.smoothed_tick_ms, |s| s.smoothed_tick_ms()); + let Some(flight) = self.flight.as_mut() else { + return; + }; + flight.tick_ms.push(tick_ms); + let flight = &*flight; let t = (flight.started.elapsed().as_secs_f64() / flight.seconds).min(1.0); let (a, b) = (flight.from, flight.to); let d = [b.x - a.x, b.y - a.y, b.z - a.z]; @@ -79,6 +91,9 @@ impl Game<'_> { && let Some(flight) = self.flight.take() { let mut summary = self.frame_stats.summary(); + let n = flight.tick_ms.len().max(1) as f32; + summary["tick_ms"] = (flight.tick_ms.iter().sum::() / n).into(); + summary["tick_ms_max"] = flight.tick_ms.iter().copied().fold(0.0, f32::max).into(); if flight.raw { summary["frame_ms"] = self.frame_stats.frame_millis().into(); summary["cpu_ms"] = self.frame_stats.cpu_millis().into(); @@ -173,6 +188,26 @@ impl Game<'_> { .chain(super::pad::HELP) .collect::>() }))), "setblock" | "fill" | "particle" | "signedit" => self.render_command(&words), + "section-draws" => { + let r = self.renderer.as_mut().ok_or("no renderer")?; + match words.get(1).copied() { + None => {} + Some("flat") => r.set_flat_draws(true)?, + Some("indirect") => r.set_flat_draws(false)?, + Some(other) => return Err(format!("unknown section draw mode {other}").into()), + } + Ok(Some(json!({ "ok": true, "flat": r.flat_draws() }))) + } + "section-cull" => { + let r = self.renderer.as_mut().ok_or("no renderer")?; + match words.get(1).copied() { + None => {} + Some("on") => r.set_section_cull(true)?, + Some("off") => r.set_section_cull(false)?, + Some(other) => return Err(format!("unknown section cull mode {other}").into()), + } + Ok(Some(json!({ "ok": true, "cull": r.section_cull() }))) + } "sculk" => Ok(Some(self.sculk_command(&words)?)), "status" => Ok(Some(self.control_status())), "pad" => Ok(Some(self.pad_command(&words)?)), @@ -335,10 +370,18 @@ impl Game<'_> { }; let info = self .res - .items + .items() .get(&item) .ok_or_else(|| format!("unknown item {item}"))?; let count: u32 = words.get(2).map_or(Ok(1), |c| c.parse())?; + // Single player's inventory is its server thread's player's. + if let Some(server) = &self.integrated { + let command = format!("give @s {item} {count}"); + server + .call(move |g| g.owner_console_command(&command)) + .ok_or("the integrated server stopped")?; + return Ok(Some(json!({ "ok": true, "given": count }))); + } let max = info.max_stack; let mut given = 0; // Stop once the inventory is full. @@ -368,8 +411,24 @@ 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" => { - self.world.day_time = arg(1)?.parse()?; + let time: i64 = arg(1)?.parse()?; + self.world.day_time = time; + // Single player's clock is its server thread's. + if let Some(server) = &self.integrated { + server + .call(move |g| g.world.day_time = time) + .ok_or("the integrated server stopped")?; + } ok() } "summon" => { @@ -427,7 +486,7 @@ impl Game<'_> { match self.screen { Screen::Chat => self.chat_type(text), Screen::CommandBlock => self.command_block_type(text), - Screen::Options => self.ui.options.type_text(&self.atlas, text), + Screen::Options => self.ui.options.type_text(&self.gui.atlas, text), // The port, or the game rules' search and number boxes. Screen::WorldOptions if text == "\\b" => self.world_options_text(None), Screen::WorldOptions => self.world_options_text(Some(text)), @@ -517,6 +576,7 @@ impl Game<'_> { seconds: n(7)?.max(0.1), raw: words.get(9) == Some(&"raw"), reply: reply.clone(), + tick_ms: Vec::new(), }); Ok(None) } @@ -606,18 +666,6 @@ impl Game<'_> { fn control_key_down(&mut self, key: KeyCode) -> Result<()> { self.physical_keys.insert(key); - // A server's chat input takes the keys first, as remote_chat_key does. - if self.remote_chat_open() { - use winit::keyboard::NamedKey; - let named = match key { - KeyCode::Escape => Some(NamedKey::Escape), - KeyCode::Enter | KeyCode::NumpadEnter => Some(NamedKey::Enter), - KeyCode::Backspace => Some(NamedKey::Backspace), - _ => None, - }; - self.remote_chat_input(named, None); - return Ok(()); - } if self.screen == Screen::Chat { self.chat_key_code(key, None); return Ok(()); @@ -663,7 +711,7 @@ impl Game<'_> { }); let target = self.pick().map(|hit| { let state = self.world.block(hit.pos[0], hit.pos[1], hit.pos[2]); - json!({ "pos": hit.pos, "block": &*self.res.facts.get(state).name }) + json!({ "pos": hit.pos, "block": &*self.res.facts().get(state).name }) }); let renderer = self.renderer.as_ref().map(|r| { let [arena_bytes, section_vertex_bytes] = r.section_vertex_bytes(); @@ -684,6 +732,9 @@ impl Game<'_> { "size": [r.config.width, r.config.height], "present_mode": format!("{:?}", r.config.present_mode), "drawn_sections": r.drawn_sections, + "section_draws": r.section_draws, + "section_draw_calls": r.section_draw_calls, + "culled_sections": r.culled_sections(), "uploaded_bytes": r.uploaded_bytes, "resident_sections": r.resident_sections(), "arena_bytes": arena_bytes, @@ -699,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": { @@ -757,7 +818,16 @@ 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()); + // Primed TNT, falling blocks, projectiles, tridents and end crystals. + status["projectiles"] = json!(self.projectiles_status()); status } } diff --git a/crates/horizon-client/src/game/copper_golems.rs b/crates/horizon-client/src/game/copper_golems.rs index a12fbd416..2f98e1c7e 100644 --- a/crates/horizon-client/src/game/copper_golems.rs +++ b/crates/horizon-client/src/game/copper_golems.rs @@ -106,7 +106,7 @@ impl Game<'_> { fn golem_chest_sound(&mut self, p: [i32; 3], open: bool) { let name = self .res - .facts + .facts() .get(self.world.block(p[0], p[1], p[2])) .name .clone(); @@ -292,7 +292,7 @@ impl Game<'_> { pub(super) fn golem_shearable(&self, index: usize) -> bool { self.mobs[index].saddle_item().is_some_and(|a| { self.res - .items + .items() .item_tags .get("minecraft:shearable_from_copper_golem") .is_some_and(|t| t.contains(&*a.item)) diff --git a/crates/horizon-client/src/game/creaking.rs b/crates/horizon-client/src/game/creaking.rs index 1caa419ff..e7a1386f9 100644 --- a/crates/horizon-client/src/game/creaking.rs +++ b/crates/horizon-client/src/game/creaking.rs @@ -110,7 +110,7 @@ impl Game<'_> { let mut next_entity = self.next_entity; let mut random = self.world.random.clone(); let spawning = self.spawning_monsters(); - let creaking = self.res.mob_types.get("minecraft:creaking").cloned(); + let creaking = self.res.mob_types().get("minecraft:creaking").cloned(); let player_uuid = self.level.player_uuid; let snapshot: Vec<_> = mobs.iter().map(|m| seen(m, player_uuid)).collect(); let mut events = Vec::new(); @@ -164,7 +164,7 @@ impl Game<'_> { return; } let key = (home[0], home[1], home[2]); - let Some(BlockEntity::CreakingHeart(mut heart)) = self.world.block_entities.remove(&key) + let Some(BlockEntity::CreakingHeart(mut heart)) = self.world.block_entities.take(&key) else { return; }; @@ -173,7 +173,7 @@ impl Game<'_> { }); self.world .block_entities - .insert(key, BlockEntity::CreakingHeart(heart)); + .put(key, BlockEntity::CreakingHeart(heart)); } /// `Creaking.tearDown`: the crumble particles. pub(super) fn creaking_tear_down(&mut self, center: [f64; 3], spread: [f64; 3]) { @@ -220,7 +220,13 @@ impl Game<'_> { /// death and a natural heart drops experience. pub(super) fn creaking_heart_broken(&mut self, pos: [i32; 3]) { let key = (pos[0], pos[1], pos[2]); - let Some(BlockEntity::CreakingHeart(mut heart)) = self.world.block_entities.remove(&key) + if !matches!( + self.world.block_entities.get(&key), + Some(BlockEntity::CreakingHeart(_)) + ) { + return; + } + let Some(BlockEntity::CreakingHeart(mut heart)) = self.world.block_entities.take(&key) else { return; }; @@ -248,43 +254,35 @@ impl Game<'_> { } self.world .block_entities - .insert(key, BlockEntity::CreakingHeart(heart)); + .put(key, BlockEntity::CreakingHeart(heart)); } - /// `CreakingHeartBlockEntity.serverTick` for every heart. - pub(super) fn tick_creaking_hearts(&mut self) -> Result<()> { - let keys: Vec = self - .world - .block_entities - .iter() - .filter(|(_, e)| matches!(e, BlockEntity::CreakingHeart(_))) - .map(|(k, _)| *k) - .collect(); - for key in keys { - let Some(BlockEntity::CreakingHeart(mut heart)) = - self.world.block_entities.remove(&key) - else { - continue; - }; - let state = self.world.block(key.0, key.1, key.2); - if &*self.res.facts.get(state).name != "minecraft:creaking_heart" { - // The block is gone: preRemoveSideEffects. - self.with_heart(key, &mut heart, |h, _, _, host, out| { - creaking_heart::remove_protector(h, host, false, out); - })?; - continue; - } - // getTicker: none once uprooted. - if self.heart_state_name(state) != "uprooted" { - let pos = [key.0, key.1, key.2]; - self.with_heart(key, &mut heart, |h, w, lr, host, out| { - creaking_heart::server_tick(h, pos, w, lr, host, out); - })?; - } - self.world - .block_entities - .insert(key, BlockEntity::CreakingHeart(heart)); + /// `CreakingHeartBlockEntity.serverTick` of the heart at `key`. Its slot + /// stays while uprooted, so that a heart whose block went away is still + /// found here and lets go of its protector. + pub(super) fn tick_creaking_heart(&mut self, key: Pos) -> Result<()> { + let Some(BlockEntity::CreakingHeart(mut heart)) = self.world.block_entities.take(&key) + else { + return Ok(()); + }; + let state = self.world.block(key.0, key.1, key.2); + if &*self.res.facts().get(state).name != "minecraft:creaking_heart" { + // The block is gone: preRemoveSideEffects. + self.world.block_entities.forget(&key); + self.with_heart(key, &mut heart, |h, _, _, host, out| { + creaking_heart::remove_protector(h, host, false, out); + })?; + return Ok(()); } - self.scan_for_hearts(); + // getTicker: none once uprooted. + if self.heart_state_name(state) != "uprooted" { + let pos = [key.0, key.1, key.2]; + self.with_heart(key, &mut heart, |h, w, lr, host, out| { + creaking_heart::server_tick(h, pos, w, lr, host, out); + })?; + } + self.world + .block_entities + .put(key, BlockEntity::CreakingHeart(heart)); Ok(()) } /// `ServerLevel.isSpawningMonsters`. @@ -295,7 +293,7 @@ impl Game<'_> { } /// Hearts that came with generated or loaded chunks get their block /// entity (`newBlockEntity`) the first time their chunk is seen. - fn scan_for_hearts(&mut self) { + pub(super) fn scan_for_hearts(&mut self) { let Some(heart) = self .pack .assets @@ -319,7 +317,7 @@ impl Game<'_> { if self.creaking.hearts.is_empty() { self.creaking.hearts = self .res - .facts + .facts() .states .iter() .map(|f| &*f.name == "minecraft:creaking_heart") @@ -327,13 +325,16 @@ impl Game<'_> { } let hearts = &self.creaking.hearts; let _ = heart; + let is_heart = |id: u32| hearts.get(id as usize).copied().unwrap_or(false); for (si, section) in chunk.sections.iter().enumerate() { - if section.is_uniform(0) { + // Only a section whose palette holds a heart state can have + // one: the others are skipped without reading their 4,096 blocks. + if !section.blocks.maybe_has(is_heart) { continue; } for i in 0..4096 { let id = section.get(i); - if !hearts.get(usize::from(id)).copied().unwrap_or(false) { + if !is_heart(id) { continue; } let (lx, lz, ly) = ((i & 15) as i32, ((i >> 4) & 15) as i32, (i >> 8) as i32); @@ -344,8 +345,7 @@ impl Game<'_> { ); self.world .block_entities - .entry(pos) - .or_insert_with(|| BlockEntity::CreakingHeart(Box::default())); + .get_or_insert_with(pos, || BlockEntity::CreakingHeart(Box::default())); } } } @@ -362,3 +362,11 @@ pub(super) struct CreakingState { /// Which block states are creaking hearts, by state id. hearts: Vec, } + +impl CreakingState { + /// Exchange the chunks scanned for creaking hearts with another level's + /// (game/levels.rs). + pub(super) fn swap_level(&mut self, other: &mut Self) { + std::mem::swap(&mut self.scanned, &mut other.scanned); + } +} diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 076c2fbef..db793d993 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -196,6 +196,7 @@ impl Game<'_> { hero: self.potions.effects.get("hero_of_the_village").is_some(), uuid: self.level.player_uuid.unwrap_or_default(), allies: std::sync::Arc::new(self.player_allies()), + remote: false, }) } /// Biome facts at a position for finalizeSpawn and variant rules. @@ -203,8 +204,8 @@ impl Game<'_> { let Some(chunk) = self.world.chunks.get(&(x >> 4, z >> 4)) else { return BiomeFacts::default(); }; - let b = self.res.biomes.get(chunk.biome(x, y, z)); - let tags = &self.res.mob_data; + let b = self.res.biomes().get(chunk.biome(x, y, z)); + let tags = self.res.mob_data(); let has = |t: &str| tags.biome_has(t, &b.name); let climate = if has("minecraft:spawns_warm_variant_farm_animals") { Climate::Warm @@ -254,7 +255,7 @@ impl Game<'_> { } else { format!("minecraft:{kind}") }; - let t = self.res.mob_types.get(&kind).cloned()?; + let t = self.res.mob_types().get(&kind).cloned()?; self.next_entity += 1; let yaw = self.world.random.next_float() * 360.0; let mut mob = Mob::new(self.next_entity, t, Vec3::new(at[0], at[1], at[2]), yaw); @@ -367,7 +368,7 @@ impl Game<'_> { pub(super) fn motion_top(&self, x: i32, z: i32) -> i32 { let top = if self.dim == Dim::Nether { 127 } else { 319 }; for y in (horizon_client::chunk::MIN_Y..=top).rev() { - let f = self.res.facts.get(self.world.block(x, y, z)); + let f = self.res.facts().get(self.world.block(x, y, z)); if (f.full_collision || f.fluid != Fluid::None) && !f.name.ends_with("leaves") { return y + 1; } @@ -376,14 +377,16 @@ impl Game<'_> { } pub(super) fn mob_world_base<'s>(&'s self, others: &'s [Seen]) -> MobWorld<'s> { let (rain, thunder) = self.world.weather.levels(1.0); + let player = self.player_view(); MobWorld { world: &self.world, registry: self.pack.assets.registry, - facts: &self.res.facts, + facts: self.res.facts(), movement: &self.motion.blocks, collision: self.collision, - data: &self.res.mob_data, - player: self.player_view(), + data: self.res.mob_data(), + nearest_player: player.as_ref().filter(|p| !p.spectator).map(|p| p.position), + player, others, others_grid: None, daylight: self.dim != Dim::Nether && self.sky_darken() < 4, @@ -392,14 +395,14 @@ impl Game<'_> { raining: rain > 0.2, thundering: thunder > 0.9, nether: self.dim == Dim::Nether, - block_tags: &self.res.items.block_tags, - biomes: &self.res.biomes, + block_tags: &self.res.items().block_tags, + biomes: self.res.biomes(), mob_griefing: self.ui.rules.bool("mob_griefing"), end: self.dim == Dim::End, world_seed: self.level.seed, sea_level: if self.dim == Dim::Nether { 32 } else { 63 }, teams: self.mob_teams(), - items: Some(&self.res.items), + items: Some(self.res.items()), difficulty: self.local_difficulty( self.player .as_ref() @@ -428,25 +431,6 @@ impl Game<'_> { }; spawning::LocalDifficulty::new(base, self.world.day_time, inhabited, moon) } - /// `ServerChunkCache.tickSpawningChunk`: chunks whose centre is within - /// 128 blocks of the player gain inhabited time. - fn tick_inhabited(&mut self) { - let Some(p) = self.player.as_ref().map(|p| p.position) else { - return; - }; - let (pcx, pcz) = (p.x.floor() as i32 >> 4, p.z.floor() as i32 >> 4); - for dx in -8..=8 { - for dz in -8..=8 { - let key = (pcx + dx, pcz + dz); - let (cx, cz) = (f64::from(key.0 * 16 + 8), f64::from(key.1 * 16 + 8)); - if (cx - p.x).powi(2) + (cz - p.z).powi(2) < 128.0 * 128.0 - && self.world.chunks.contains_key(&key) - { - *self.world.inhabited.entry(key).or_insert(0) += 1; - } - } - } - } /// Advance every mob one tick, then apply their events. pub(super) fn tick_creatures(&mut self) -> Result<()> { self.attack_ticks = self.attack_ticks.saturating_add(1); @@ -463,7 +447,6 @@ impl Game<'_> { } self.spawn_generation_creatures(key.0, key.1); } - self.tick_inhabited(); self.tick_leads(); self.spawn_natural(); // ServerChunkCache.tickChunks: the custom spawners tick only with spawn_mobs. @@ -474,24 +457,11 @@ impl Game<'_> { self.tick_raids(); self.tick_creature_entities() } - /// `DistanceManager.inEntityTickingRange`: chunks within the simulation - /// distance (Chebyshev, in chunks) of the player or of a guest. + /// `DistanceManager.inEntityTickingRange` in an entity-ticking chunk + /// (block coordinates), from every player's tickets (`update_chunk_tickets`). pub(super) fn entity_ticking_area(&self) -> impl Fn(i32, i32) -> bool + use<> { - let d = self.settings.simulation_distance.max(2); - let mut centres: Vec<(i32, i32)> = self.streaming.extra.iter().map(|&(c, _)| c).collect(); - if let Some(p) = &self.player { - centres.push(( - (p.position.x.floor() as i32) >> 4, - (p.position.z.floor() as i32) >> 4, - )); - } - let everywhere = centres.is_empty(); - move |x, z| { - everywhere - || centres - .iter() - .any(|&(cx, cz)| ((x >> 4) - cx).abs().max(((z >> 4) - cz).abs()) <= d) - } + let levels = self.tickets.levels(); + move |x, z| levels.entity_ticking_at(x, z) } /// The entity phase of `tick_creatures`: every mob's tick, deaths, and the /// projectiles and effects that follow from them. @@ -517,6 +487,7 @@ impl Game<'_> { let ticking = self.entity_ticking_area(); // With guests on, each mob sees its nearest player (game/lan_mobs.rs). let guests = self.lan_guest_views(); + let players = self.level_players(); let mut bindings = match self.lan.as_mut() { Some(host) if !guests.is_empty() => std::mem::take(&mut host.mob_players), _ => Default::default(), @@ -535,6 +506,13 @@ impl Game<'_> { let mut w = self.mob_world_base(&snapshot); w.others_grid = Some(&grid); let m = &mut mobs[i]; + w.nearest_player = super::natural_spawning::nearest_player( + &players, + m.position.x, + m.position.y, + m.position.z, + ) + .map(|p| p.position); let seen_guest = if guests.is_empty() { None } else { @@ -983,7 +961,7 @@ impl Game<'_> { if effect { let old = self.world.block(pos[0], pos[1], pos[2]); self.destroy_particles(pos, old); - let s = self.res.facts.get(old).sounds.break_sound.clone(); + let s = self.res.facts().get(old).sounds.break_sound.clone(); self.audio.play( &s, Some([ @@ -1003,7 +981,7 @@ impl Game<'_> { let old = self.world.block(pos[0], pos[1], pos[2]); self.destroy_particles(pos, old); if drop { - let drops = self.res.items.block_drops_in( + let drops = self.res.items().block_drops_in( self.pack.assets.registry, old, None, @@ -1157,7 +1135,7 @@ impl Game<'_> { MobEvent::GiftLoot { table, at } => { let drops = self.res - .items + .items() .loot_table(table, false, None, &mut self.world.random); for d in drops { self.spawn_item(d, at, [0.0, 0.2, 0.0], 10); @@ -1358,25 +1336,52 @@ impl Game<'_> { /// mining fatigue III for five minutes unless a stronger or longer one /// is on, with the curse sound and the elder's apparition. fn elder_curse(&mut self, at: [f64; 3]) { + let mut cursed = Vec::new(); + self.for_each_player(|g| { + if g.elder_curse_player(at) { + cursed.push(g.lan_acting); + } + }); + // A guest's client shows the apparition and plays the curse itself + // (ClientboundGameEventPacket.GUARDIAN_ELDER_EFFECT). + if let Some(host) = self.lan.as_ref() { + for conn in cursed.into_iter().flatten() { + host.hub.send_to( + conn, + &horizon_protocol::packets::play::Clientbound::GameEvent( + horizon_protocol::packets::play::GameEvent { + event: horizon_client::lan::hub::game_event::GUARDIAN_ELDER_EFFECT, + param: 1.0, + }, + ), + ); + } + } + } + /// The curse on the player in place; whether it took. + fn elder_curse_player(&mut self, at: [f64; 3]) -> bool { if self.creative || self.spectator() { - return; + return false; } let Some(p) = self.player.as_ref() else { - return; + return false; }; let d = p.position.subtract(Vec3::new(at[0], at[1], at[2])); if d.x * d.x + d.y * d.y + d.z * d.z >= 2500.0 || p.health.dead_or_dying() { - return; + return false; } let active = self.potions.effects.get("minecraft:mining_fatigue"); if active.is_some_and(|e| e.amplifier >= 2 && e.duration >= 1200) { - return; + return false; } let at = [p.position.x, p.position.y, p.position.z]; self.give_player_effect("minecraft:mining_fatigue", 6000, 2); - self.audio - .play("minecraft:entity.elder_guardian.curse", Some(at), 1.0, 1.0); - self.creatures.elder_apparition = super::mob_blocks::APPARITION_TICKS; + if self.lan_acting.is_none() { + self.audio + .play("minecraft:entity.elder_guardian.curse", Some(at), 1.0, 1.0); + self.creatures.elder_apparition = super::mob_blocks::APPARITION_TICKS; + } + true } /// Damage of a given type to the player from a point. pub(super) fn mob_hits_player_kind(&mut self, amount: f32, from: [f64; 3], kind: &str) { @@ -1393,7 +1398,7 @@ impl Game<'_> { } let projectile = self .res - .stations + .stations() .enchantments .damage_tags .get("minecraft:is_projectile") @@ -1656,10 +1661,48 @@ impl Game<'_> { } self.creatures.projectiles.append(&mut projectiles); for (p, hit) in hits { - self.projectile_hit(&p, hit)?; + // A guest's projectile lands as theirs: a pearl takes them, what + // it hurts answers to them. One hitting the host (`Who::Player` + // here) stays the host's hit. + match p.guest { + Some(conn) + if !matches!( + hit, + Hit::Entity { + who: Who::Player, + .. + } + ) => + { + self.guest_projectile_hit(conn, &p, hit)?; + } + _ => self.projectile_hit(&p, hit)?, + } } Ok(()) } + /// `projectile_hit` for a projectile the guest `conn` shot, run as them; + /// as the host's when they have left. + fn guest_projectile_hit(&mut self, conn: u32, p: &Projectile, hit: Hit) -> Result<()> { + let Some(mut host) = self.lan.take() else { + return self.projectile_hit(p, hit); + }; + let result = self.as_guest(&mut host, conn, |g| g.projectile_hit(p, hit)); + // The mob hit now answers to the guest (LivingEntity.lastHurtByPlayer). + if result.is_some() + && let Hit::Entity { + who: Who::Mob(id), .. + } = hit + && self.mobs.iter().any(|m| m.id == id) + { + host.mob_players.insert(id, conn); + } + self.lan = Some(host); + match result { + Some(r) => r, + None => self.projectile_hit(p, hit), + } + } /// What an arrow's tick asked for: its particles and sounds. fn arrow_event(&mut self, at: Vec3, event: horizon_client::arrows::ArrowEvent) { use horizon_client::arrows::{ArrowEvent, ArrowParticle, ArrowSound, BlockReaction}; @@ -1753,7 +1796,7 @@ impl Game<'_> { horizon_client::arrows::ArrowScene { world: &self.world, registry: self.pack.assets.registry, - facts: &self.res.facts, + facts: self.res.facts(), collision: self.collision, fluids: &self.motion.fluids, raining: rain, @@ -1963,9 +2006,15 @@ impl Game<'_> { m.damage(0.0, source); } if p.owner == Some(Who::Player) && self.player.is_some() { - // ThrownEnderpearl: 5% endermite, teleport, 5 damage. - if self.world.random.next_float() < 0.05 { - self.spawn_mob("minecraft:endermite", atp, false); + // ThrownEnderpearl: 5% endermite where the player was + // (when monsters spawn), teleport, 5 damage. + if self.world.random.next_float() < 0.05 + && self.ui.rules.bool("spawn_monsters") + && self.ui.difficulty != 0 + && let Some(pl) = self.player.as_ref() + { + let from = pl.position; + self.spawn_mob("minecraft:endermite", [from.x, from.y, from.z], false); } if let Some(pl) = self.player.as_mut() { pl.position = at; @@ -2005,7 +2054,7 @@ impl Game<'_> { let fire = [pos[0] + face[0], pos[1] + face[1], pos[2] + face[2]]; if self .res - .facts + .facts() .is_air(self.world.block(fire[0], fire[1], fire[2])) && let Some(state) = self.pack.assets.registry.default_state("minecraft:fire") @@ -2253,6 +2302,10 @@ impl Game<'_> { return Ok(true); } if self.interact_sulfur_cube(index, &item) { + if let Some(stack) = &held { + let target = self.mob_entity(&self.mobs[index]); + self.trigger_item_entity("minecraft:player_interacted_with_entity", stack, &target); + } self.swing_arm(); return Ok(true); } @@ -2327,7 +2380,7 @@ impl Game<'_> { Species::Sheep if item == "minecraft:shears" && !m.extra.sheared && !m.is_baby() => { m.extra.sheared = true; let color = horizon_client::mobs::render::DYE_NAMES[m.extra.color as usize & 15]; - let stacks = self.res.items.loot_table( + let stacks = self.res.items().loot_table( &format!("minecraft:shearing/sheep/{color}"), false, None, @@ -2359,7 +2412,7 @@ impl Game<'_> { Species::Wolf if m.extra.tame && is_food && m.health < m.attrs.max_health => { let heal = self .res - .items + .items() .get(&item) .and_then(|i| i.food.as_ref()) .map_or(2.0, |f| f.0 as f32); @@ -2463,7 +2516,7 @@ impl Game<'_> { Species::Cat if m.extra.tame && is_food && m.health < m.attrs.max_health => { let heal = self .res - .items + .items() .get(&item) .and_then(|i| i.food.as_ref()) .map_or(1.0, |f| f.0 as f32); @@ -2630,7 +2683,7 @@ impl Game<'_> { return; } if let Some(stack) = self.inventory[self.selected].as_mut() - && let Some(info) = self.res.items.get(&stack.item) + && let Some(info) = self.res.items().get(&stack.item) && info.max_damage > 0 { stack.damage += 1; @@ -2703,7 +2756,7 @@ impl Game<'_> { // EntityType.canSpawn: no monsters in peaceful (FAIL ends the click). let id = format!("minecraft:{}", kind.trim_start_matches("minecraft:")); if self.ui.difficulty == 0 - && let Some(t) = self.res.mob_types.get(&id) + && let Some(t) = self.res.mob_types().get(&id) && !horizon_client::mobs::spawn_rules::allowed_in_peaceful(t.species) { return Ok(true); @@ -2767,7 +2820,7 @@ impl Game<'_> { } /// Mob models and projectiles for the entity pass. pub(super) fn mob_geometry(&self, camera: glam::DVec3, alpha: f64, out: &mut Vec) { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let lookup = |name: &str| atlas.sprites.get(name).map(|r| [r.u0, r.v0, r.u1, r.v1]); let scale = f64::from(self.settings.entity_distance); for mob in &self.mobs { @@ -2802,6 +2855,17 @@ impl Game<'_> { if mob.fire_ticks > 0 || matches!(mob.species(), Species::Blaze | Species::MagmaCube) { packed = (15 * 16) | ((15 * 16) << 8); } + if mob.species() == Species::SulfurCube + && !horizon_client::mobs::trader::is_invisible(mob) + { + self.sulfur_cube_body_geometry( + mob, + alpha as f32, + horizon_client::mobs::render::base_transform(mob, alpha as f32, camera), + packed, + out, + ); + } // Other players on a server: PlayerRenderer's HumanoidArmorLayer. let armor = if &*mob.kind.id == "minecraft:player" { self.worn_armor_layers(mob) @@ -2814,7 +2878,7 @@ impl Game<'_> { camera, &lookup, packed, - &self.res.mob_data, + self.res.mob_data(), &armor, out, ); @@ -2973,7 +3037,7 @@ impl Game<'_> { } /// Mob.getBaseExperienceReward /// The damage type id of a Horizon damage kind. -fn damage_type(kind: horizon_client::mobs::DamageKind, player: bool) -> &'static str { +pub(super) fn damage_type(kind: horizon_client::mobs::DamageKind, player: bool) -> &'static str { use horizon_client::mobs::DamageKind as K; match kind { K::PlayerAttack => "minecraft:player_attack", @@ -3058,13 +3122,17 @@ impl Game<'_> { return; } let mut y = pos[1]; - while &*self.res.facts.get(self.world.block(pos[0], y, pos[2])).name + while &*self + .res + .facts() + .get(self.world.block(pos[0], y, pos[2])) + .name == "minecraft:nether_portal" { y -= 1; } // isValidSpawn: a sturdy top face (portal frames emit no light). - let below = self.res.facts.get(self.world.block(pos[0], y, pos[2])); + let below = self.res.facts().get(self.world.block(pos[0], y, pos[2])); if below.sturdy_full & (1 << 1) == 0 { return; } diff --git a/crates/horizon-client/src/game/debug_keys.rs b/crates/horizon-client/src/game/debug_keys.rs index 36761a85e..a2e1b0ce6 100644 --- a/crates/horizon-client/src/game/debug_keys.rs +++ b/crates/horizon-client/src/game/debug_keys.rs @@ -51,6 +51,9 @@ impl DebugHost for Host<'_, '_> { impl Game<'_> { fn open_screen(&self) -> OpenScreen { + if self.title.is_some() { + return OpenScreen::Other; + } match self.screen { Screen::GameModeSwitcher => OpenScreen::GameModeSwitcher, Screen::Playing if self.book.is_none() && self.sign_edit.is_none() => OpenScreen::None, @@ -88,8 +91,7 @@ impl Game<'_> { || self.book.is_some() || (self.screen == Screen::Creative && self.creative_screen.tab().search) || self.recipe_search_focused() - || self.station_typing() - || self.remote_chat_open(); + || self.station_typing(); // Container screens use Escape, the inventory key and the hotbar and // drop keys first; any other screen closes on Escape. let screen_key = match self.screen { @@ -438,7 +440,7 @@ impl Game<'_> { } } let (left, right) = d.columns(&self.debug_chart_lines()); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let mut draws = debug_overlay::draw(&atlas.font, [&left, &right], scale, width); let charts = self.debug_charts.draw( &atlas.font, diff --git a/crates/horizon-client/src/game/decorated_pot.rs b/crates/horizon-client/src/game/decorated_pot.rs index 3ac43566a..af2eaacb2 100644 --- a/crates/horizon-client/src/game/decorated_pot.rs +++ b/crates/horizon-client/src/game/decorated_pot.rs @@ -63,7 +63,7 @@ impl Game<'_> { /// pot without), or None when no pot is there. fn pot_tag(&mut self, pos: [i32; 3]) -> Option { let state = self.world.block(pos[0], pos[1], pos[2]); - if &*self.res.facts.get(state).name != "minecraft:decorated_pot" { + if &*self.res.facts().get(state).name != "minecraft:decorated_pot" { return None; } let mut tag = spill::saved_entity(&self.world, pos).unwrap_or_else(|| { diff --git a/crates/horizon-client/src/game/dedicated.rs b/crates/horizon-client/src/game/dedicated.rs index 314ac2428..8495ea129 100644 --- a/crates/horizon-client/src/game/dedicated.rs +++ b/crates/horizon-client/src/game/dedicated.rs @@ -9,11 +9,11 @@ use super::{Game, Options, lan::LanSettings}; use horizon_client::{ Result, - lan::{LoginGate, NetConfig, hub::HubConfig}, + lan::{LoginGate, NetConfig, NetLog, OnlineMode, hub::HubConfig}, server::{ - eula, - java_properties::now_millis, + eula, profiles, properties::{GAME_MODES, ServerProperties, SystemEntropy}, + user_cache::UserCache, user_lists::{ BanEntry, BanList, IpBanList, NameAndId, OpEntry, OpList, WhiteEntry, WhiteList, format_date, @@ -57,26 +57,41 @@ struct Setup { started: Instant, } static SETUP: OnceLock>> = OnceLock::new(); +/// usercache.json (Services.nameToIdCache): used by the server thread only. +static USER_CACHE: Mutex> = Mutex::new(None); + +/// PlayerList.placeNewPlayer: a player who joined is cached (and the file saved). +pub(super) fn player_joined(id: u128, name: &str) { + if let Some(cache) = USER_CACHE.lock().unwrap().as_mut() { + cache.add(NameAndId { + id, + name: name.to_owned(), + }); + } +} /// Log4j's console pattern: `[HH:mm:ss] [thread/LEVEL]: message`. pub(super) fn log(level: &str, message: &str) { + log_from("Server thread", level, message); +} + +/// A line from another of vanilla's threads (the login's User Authenticator). +fn log_from(thread: &str, level: &str, message: &str) { let file_level = match level { "WARN" => ::log::Level::Warn, "ERROR" => ::log::Level::Error, _ => ::log::Level::Info, }; - horizon_client::logging::write_only(file_level, message); + horizon_client::logging::write_as(thread, file_level, message); CAPTURE.with_borrow_mut(|lines| { if let Some(lines) = lines { lines.push(message.to_owned()); } }); - let secs = now_millis().div_euclid(1000).rem_euclid(86_400); + // Log4j's %d{HH:mm:ss}: the local time, as in logs/latest.log. println!( - "[{:02}:{:02}:{:02}] [Server thread/{level}]: {message}", - secs / 3600, - secs / 60 % 60, - secs % 60 + "[{}] [{thread}/{level}]: {message}", + chrono::Local::now().format("%H:%M:%S") ); } @@ -271,6 +286,78 @@ impl Lists { } } +/// The world presets of 26.3's `worldgen/world_preset`. +const WORLD_PRESETS: [&str; 6] = [ + "minecraft:normal", + "minecraft:flat", + "minecraft:large_biomes", + "minecraft:amplified", + "minecraft:single_biome_surface", + "minecraft:debug_all_block_states", +]; + +/// Identifier.tryParse: `namespace:path`, or a path in `minecraft`. +fn parse_identifier(text: &str) -> Option { + let (namespace, path) = text.split_once(':').unwrap_or(("minecraft", text)); + let valid = |s: &str, extra: &[char]| { + s.chars() + .all(|c| matches!(c, 'a'..='z' | '0'..='9' | '_' | '.' | '-') || extra.contains(&c)) + }; + (valid(namespace, &[]) && valid(path, &['/'])).then(|| format!("{namespace}:{path}")) +} + +impl Game<'_> { + /// DedicatedServerProperties.createDimensions for a world that doesn't + /// exist yet: `level-type`'s preset, a flat world's `generator-settings` + /// and `generate-structures`, written before the world opens so that + /// streaming reads them. Horizon generates the normal and flat presets. + fn write_world_gen(&self, dir: &std::path::Path, props: &ServerProperties) -> Result<()> { + let level_type = &props.level_type; + let preset = parse_identifier(level_type) + .filter(|id| WORLD_PRESETS.contains(&id.as_str())) + .unwrap_or_else(|| { + log_from( + "Worker-Main-1", + "WARN", + &format!( + "Failed to parse level-type {level_type}, defaulting to minecraft:normal" + ), + ); + "minecraft:normal".to_owned() + }); + let flat = preset == "minecraft:flat"; + if !flat && preset != "minecraft:normal" { + log_from( + "Worker-Main-1", + "WARN", + &format!( + "Horizon doesn't generate the {preset} world preset yet: using minecraft:normal" + ), + ); + } + let settings = if flat { + let biomes = self.res.biomes(); + let (settings, errors) = horizon_client::flatgen::settings_from_json( + &props.generator_settings, + |b| self.pack.assets.registry.default_state(b).is_some(), + |b| biomes.get(biomes.id(b)).name == b, + ); + for e in errors { + log_from("Worker-Main-1", "ERROR", &e); + } + settings + } else { + None + }; + let options = horizon_client::save::WorldGen { + flat, + structures: props.generate_structures, + bonus_chest: false, + }; + horizon_client::save::write_world_gen_settings_with(dir, props.seed, options, settings) + } +} + /// PlayerList.BAN_DATE_FORMAT ("yyyy-MM-dd 'at' HH:mm:ss z"), in UTC. fn ban_date(millis: i64) -> String { let s = format_date(millis); @@ -311,11 +398,38 @@ struct ServerState { /// When the last passes of the loop ended, for the tick rate horizon-server shows. ticks: std::collections::VecDeque, paused: bool, + /// `broadcast-console-to-ops`. + broadcast_console: bool, } +/// The dedicated-only commands' replies vanilla sends with `broadcast` +/// (`sendSuccess(..., true)`): the operators hear of them. +const BROADCAST: &[&str] = &[ + "commands.ban.success", + "commands.banip.info", + "commands.banip.success", + "commands.deop.success", + "commands.kick.success", + "commands.op.success", + "commands.pardon.success", + "commands.pardonip.success", + "commands.save.disabled", + "commands.save.enabled", + "commands.save.success", + "commands.stop.stopping", + "commands.whitelist.add.success", + "commands.whitelist.disabled", + "commands.whitelist.enabled", + "commands.whitelist.reloaded", + "commands.whitelist.remove.success", +]; + impl Game<'_> { /// DedicatedServer.initServer and MinecraftServer.runServer. pub(super) fn run_dedicated(&mut self) -> Result<()> { + // This thread ticks the server (MinecraftServer.spin's priority). + horizon_threads::favour_current(); + self.join_world_rest()?; let Setup { props, universe, @@ -326,6 +440,13 @@ impl Game<'_> { .get() .and_then(|m| m.lock().unwrap().take()) .ok_or("dedicated server not prepared")?; + // Main's WorldLoader: a new world's dimensions come from + // server.properties before the server starts. + let world_dir = std::env::current_dir()?.join(&universe).join(&level_name); + let new_world = !world_dir.join("level.dat").exists(); + if new_world { + self.write_world_gen(&world_dir, &props)?; + } // The console thread starts first, as in initServer. let (tx, console) = mpsc::channel(); std::thread::Builder::new() @@ -343,6 +464,26 @@ impl Game<'_> { line.clear(); } })?; + // Main: Services.create loads the universe's usercache.json. + let (mut cache, warnings) = UserCache::load(universe.join("usercache.json")); + for warning in warnings { + log_from("main", "WARN", &warning); + } + // DedicatedServer.initServer: names nobody has resolve offline only without authentication. + cache.resolve_offline_users = !props.online_mode; + *USER_CACHE.lock().unwrap() = Some(cache); + // Services.create: the services' public keys, fetched in the + // background (they check players' chat keys). + let secure_chat = Arc::new(horizon_client::lan::SecureChat::new( + props.enforce_secure_profile && props.online_mode, + || { + let discovery = profiles::discovery_url(); + profiles::fetch_profile_key_keys(&discovery).unwrap_or_else(|e| { + ::log::error!("{e}"); + Vec::new() + }) + }, + )); log("INFO", "Starting minecraft server version 26.3"); log("INFO", "Loading properties"); let game_mode = props.game_mode; @@ -354,6 +495,19 @@ impl Game<'_> { ), ); let port = port.unwrap_or(props.server_port as u16); + // MinecraftServer.initializeKeyPair (only online mode uses it). + log("INFO", "Generating keypair"); + let online = if props.online_mode { + let key = horizon_protocol::rsa::RsaPrivateKey::generate(1024) + .map_err(|e| format!("Failed to generate key pair: {e}"))?; + Some(Arc::new(OnlineMode { + public_der: key.public_der(), + key, + prevent_proxy: props.prevent_proxy_connections, + })) + } else { + None + }; log( "INFO", &format!( @@ -365,35 +519,27 @@ impl Game<'_> { } ), ); - if props.online_mode { - // Authentication needs Mojang's session servers (an HTTPS client - // and RSA), which Horizon does not have yet: offline mode. + if online.is_none() { + log("WARN", "**** SERVER IS RUNNING IN OFFLINE/INSECURE MODE!"); log( "WARN", - "online-mode=true is not supported by Horizon yet: running in offline mode", + "The server will make no attempt to authenticate usernames. Beware.", + ); + log( + "WARN", + "While this makes the game possible to play without internet access, it also opens up the ability for hackers to connect with any username they choose.", + ); + log( + "WARN", + "To change this, set \"online-mode\" to \"true\" in the server.properties file.", ); } - log("WARN", "**** SERVER IS RUNNING IN OFFLINE/INSECURE MODE!"); - log( - "WARN", - "The server will make no attempt to authenticate usernames. Beware.", - ); - log( - "WARN", - "While this makes the game possible to play without internet access, it also opens up the ability for hackers to connect with any username they choose.", - ); - log( - "WARN", - "To change this, set \"online-mode\" to \"true\" in the server.properties file.", - ); let mut lists = Lists::load(); lists.use_whitelist = props.white_list; lists.enforce_whitelist = props.enforce_whitelist; let lists = Arc::new(Mutex::new(lists)); log("INFO", &format!("Preparing level \"{level_name}\"")); - let world_dir = std::env::current_dir()?.join(&universe).join(&level_name); let folder = world_dir.to_string_lossy().into_owned(); - let new_world = !world_dir.join("level.dat").exists(); self.open_world( &folder, Some(&level_name), @@ -436,8 +582,7 @@ impl Game<'_> { enable_status: props.enable_status, hide_online_players: props.hide_online_players, favicon, - // DedicatedServer.enforceSecureProfile needs online mode. - enforces_secure_chat: false, + secure_chat: Some(secure_chat), accepts_transfers: props.accepts_transfers, announce: false, listen: true, @@ -451,6 +596,12 @@ impl Game<'_> { .any(|e| e.user.id == uuid && e.bypasses_player_limit) })), gate: Some(gate), + online, + log: Some(NetLog { + write: Arc::new(log_from), + lang: Arc::new(horizon_client::net::load_language(&self.options.resources)), + log_ips: props.log_ips, + }), }; let config = HubConfig { default_game_mode: game_mode, @@ -495,6 +646,7 @@ impl Game<'_> { empty_ticks: 0, ticks: Default::default(), paused: false, + broadcast_console: props.broadcast_console_to_ops, }; let pause_after = props.pause_when_empty_seconds * 20; let idle_timeout = props.player_idle_timeout; @@ -502,7 +654,7 @@ impl Game<'_> { while !state.stop { let tick_start = Instant::now(); while let Ok(line) = state.console.try_recv() { - self.console_command(&mut state, &line); + self.console_line(&mut state, &line); } self.poll_server_control(&mut state); let players = self.lan.as_ref().map_or(0, |h| h.hub.guests.len()); @@ -525,18 +677,12 @@ impl Game<'_> { ); self.save_game()?; } - self.update_dedicated_streaming(); - self.update_world(); - if paused { - // MinecraftServer.tickServer: only the connections run. - self.tick_lan(); - } else { - if !state.autosave { - // `save-off`: the periodic save inside the tick is skipped. - self.ticks = self.ticks.max(1); - } - self.tick()?; + if !paused && !state.autosave { + // `save-off`: the periodic save inside the tick is skipped. + self.ticks = self.ticks.max(1); } + // MinecraftServer.tickServer: while paused only the connections run. + self.tick_server_timed(paused)?; if idle_timeout > 0 { self.kick_idle(idle_timeout); } @@ -577,7 +723,13 @@ impl Game<'_> { let center = self .lan .as_ref() - .and_then(|h| h.hub.guests.iter().find(|g| g.placed).map(|g| g.chunk())) + .and_then(|h| { + h.hub + .guests + .iter() + .find(|g| h.hub.here(g)) + .map(|g| g.chunk()) + }) .or_else(|| { self.spawn .map(|s| ((s.x.floor() as i32) >> 4, (s.z.floor() as i32) >> 4)) @@ -623,28 +775,64 @@ impl Game<'_> { host.hub.resend_commands(); } - /// A known player by name: online first, else the offline profile. - fn resolve(&self, name: &str) -> NameAndId { - self.lan - .as_ref() - .and_then(|h| { - h.hub - .guests - .iter() - .find(|g| g.name.eq_ignore_ascii_case(name)) - }) - .map_or_else( - || NameAndId::offline(name), - |g| NameAndId { - id: g.uuid, - name: g.name.clone(), - }, - ) + /// A player on the server, by name. + fn online_player(&self, name: &str) -> Option { + let hub = &self.lan.as_ref()?.hub; + let g = hub + .guests + .iter() + .find(|g| g.name.eq_ignore_ascii_case(name))?; + Some(NameAndId { + id: g.uuid, + name: g.name.clone(), + }) } - /// MinecraftServer.sendSystemMessage: what the console is told. - fn console_reply(&self, text: Text) { + /// GameProfileArgument for a name: the user cache, else the profile + /// service, else (offline) the offline player. None, told to the console: + /// no such player. + fn resolve(&self, name: &str) -> Option { + let found = USER_CACHE.lock().unwrap().as_mut().and_then(|cache| { + cache.get_by_name(name, |lower| { + let found = profiles::find_by_name(&profiles::discovery_url(), lower); + if found.is_none() { + log("WARN", &format!("Couldn't find profile with name: {lower}")); + } + found + }) + }); + if found.is_none() { + let unknown = Text::translate("argument.player.unknown", &[]); + log("INFO", &format!("System chat: {}", unknown.plain())); + } + found + } + + /// MinecraftServer.sendSystemMessage: what the console is told; while + /// the console runs a command, the replies vanilla broadcasts go to the + /// operators too. + fn console_reply(&mut self, text: Text) { log("INFO", &format!("System chat: {}", text.plain())); + if self.console_broadcast.is_some() + && text + .translation_key() + .is_some_and(|k| BROADCAST.contains(&k)) + { + self.broadcast_to_admins(text); + } + } + + /// One console line, then the operators' share of its feedback. + fn console_line(&mut self, state: &mut ServerState, line: &str) { + self.console_broadcast = Some(state.broadcast_console); + self.console_command(state, line); + self.console_broadcast = None; + if let Some(mut host) = self.lan.take() { + self.run_level_teleports(&mut host); + self.send_admin_feedback(&mut host, None); + self.lan = Some(host); + } + self.admin_feedback.clear(); } /// DedicatedServer.handleConsoleInputs: one console line, run as the @@ -722,7 +910,9 @@ impl Game<'_> { )); } "op" | "deop" if !rest.is_empty() => { - let user = self.resolve(rest[0]); + let Some(user) = self.resolve(rest[0]) else { + return; + }; let level = 4; let changed = { let mut l = state.lists.lock().unwrap(); @@ -752,7 +942,9 @@ impl Game<'_> { } "whitelist" => self.console_whitelist(state, &rest), "ban" if !rest.is_empty() => { - let user = self.resolve(rest[0]); + let Some(user) = self.resolve(rest[0]) else { + return; + }; let reason = (rest.len() > 1).then(|| rest[1..].join(" ")); let added = state.lists.lock().unwrap().bans.add(BanEntry::new( user.clone(), @@ -842,7 +1034,9 @@ impl Game<'_> { } } "pardon" if !rest.is_empty() => { - let user = self.resolve(rest[0]); + let Some(user) = self.resolve(rest[0]) else { + return; + }; let removed = state.lists.lock().unwrap().bans.remove(&user); let key = if removed { "commands.pardon.success" @@ -908,15 +1102,10 @@ impl Game<'_> { } } "kick" if !rest.is_empty() => { - let user = self.resolve(rest[0]); - let online = self - .lan - .as_ref() - .is_some_and(|h| h.hub.guests.iter().any(|g| g.uuid == user.id)); - if !online { + let Some(user) = self.online_player(rest[0]) else { self.console_reply(Text::translate("argument.entity.notfound.player", &[])); return; - } + }; let reason = if rest.len() > 1 { Tag::String(JavaString::from(rest[1..].join(" ").as_str())) } else { @@ -996,7 +1185,9 @@ impl Game<'_> { self.console_reply(Text::translate("commands.whitelist.reloaded", &[])); } ["add", name] | ["remove", name] => { - let user = self.resolve(name); + let Some(user) = self.resolve(name) else { + return; + }; let mut l = state.lists.lock().unwrap(); let (ok, key) = if rest[0] == "add" { if l.white.contains(&user) { @@ -1073,6 +1264,13 @@ impl Game<'_> { for g in &h.hub.guests { horizon_client::lan::playerdata::save(g, &self.world_root); } + } + if let Some(h) = self.lan.as_ref() { + for g in &h.hub.guests { + self.save_guest_records(h, g); + } + } + if let Some(h) = self.lan.as_mut() { let conns: Vec = h.hub.guests.iter().map(|g| g.conn).collect(); for conn in conns { h.hub.kick( diff --git a/crates/horizon-client/src/game/dispense_fixture.rs b/crates/horizon-client/src/game/dispense_fixture.rs index ee8170bc5..ab7a6cf48 100644 --- a/crates/horizon-client/src/game/dispense_fixture.rs +++ b/crates/horizon-client/src/game/dispense_fixture.rs @@ -114,9 +114,9 @@ impl Game<'_> { } fn fixture_reset(&mut self, seed: i64) { - self.world = World::new(self.res.facts.air, seed); + self.world = World::new(self.res.facts().air, seed); // The oracle level is plains everywhere (bone meal's flowers, freezing). - let plains = u32::from(self.res.biomes.id("minecraft:plains")); + let plains = u32::from(self.res.biomes().id("minecraft:plains")); for cx in -2..=2 { for cz in -2..=2 { let mut chunk = Chunk::empty(cx, cz); @@ -198,7 +198,7 @@ impl Game<'_> { let state = BlockStateId(cell[3].as_u64().unwrap_or(0) as u32); self.world.set(p.0, p.1, p.2, state)?; before.insert(p, state.0); - if let Some(entity) = BlockEntity::for_block(&self.res.facts.get(state).name) { + if let Some(entity) = BlockEntity::for_block(&self.res.facts().get(state).name) { self.world.block_entities.insert(p, entity); } } @@ -284,7 +284,7 @@ impl Game<'_> { let state = self.world.block(origin.x, origin.y, origin.z); let mut used = Vec::new(); if let Some(op) = case.get("remove") { - self.fixture_remove(origin, op)?; + used = self.fixture_remove(origin, op)?; } else if let Some(op) = case.get("use") { used = self.fixture_use(origin, op)?; } else if let Some(op) = case.get("use_item") { @@ -318,8 +318,18 @@ impl Game<'_> { } else { self.dispense_from(origin, state)?; // CrafterBlockEntity.serverTick, the crafting animation running down. + let mut crafters: Vec<(i32, i32, i32)> = self + .world + .block_entities + .iter() + .filter(|(_, e)| matches!(e, BlockEntity::Crafter(_))) + .map(|(p, _)| *p) + .collect(); + crafters.sort_unstable(); for _ in 0..case["ticks"].as_u64().unwrap_or(0) { - self.tick_crafters(); + for &p in &crafters { + self.tick_crafter(p); + } } } @@ -329,7 +339,7 @@ impl Game<'_> { changed.dedup(); for p in changed { let now = self.world.block(p.0, p.1, p.2).0; - if before.get(&p).copied().unwrap_or(self.res.facts.air.0) != now { + if before.get(&p).copied().unwrap_or(self.res.facts().air.0) != now { out.push(format!("set {},{},{} {now}", p.0, p.1, p.2)); } } @@ -696,7 +706,7 @@ impl Game<'_> { /// lectern's book, or the items of a block entity Horizon keeps as saved. fn fixture_fill(&mut self, p: (i32, i32, i32), slot: usize, stack: Option) { let jukebox = - &*self.res.facts.get(self.world.block(p.0, p.1, p.2)).name == "minecraft:jukebox"; + &*self.res.facts().get(self.world.block(p.0, p.1, p.2)).name == "minecraft:jukebox"; match self.world.block_entities.get_mut(&p) { Some(BlockEntity::Jukebox(j)) => j.record = stack, Some(BlockEntity::Lectern(l)) => l.book = stack, @@ -725,7 +735,7 @@ impl Game<'_> { use horizon_world::nbt::Tag; let name = self .res - .facts + .facts() .get(self.world.block(p.0, p.1, p.2)) .name .clone(); @@ -778,8 +788,9 @@ impl Game<'_> { other => *other = Tag::List(vec![item]), } } - /// `Level.setBlock`, `destroyBlock` or `removeBlock` on the case's block. - fn fixture_remove(&mut self, origin: BlockPos, op: &Value) -> Result<()> { + /// `Level.setBlock`, `destroyBlock` or `removeBlock` on the case's block, or + /// a player mining it (`fixture_mine`). + fn fixture_remove(&mut self, origin: BlockPos, op: &Value) -> Result> { match op["op"].as_str().unwrap_or("") { "set" => { let state = BlockStateId(op["state"].as_u64().unwrap_or(0) as u32); @@ -793,9 +804,61 @@ impl Game<'_> { "remove" => { self.with_level(|b, level, ticks| b.remove_block(level, ticks, origin, false)); } + "mine" => return self.fixture_mine(origin, op), other => return Err(format!("unknown removal {other}").into()), } - self.process_level_events() + self.process_level_events()?; + Ok(Vec::new()) + } + + /// A survival player three blocks south of `pos` mines it holding the + /// case's item (`break_block`, which every player's mining goes through): + /// the statistics and the held stack, as `VanillaDispenserOracle.mine` + /// prints them. + fn fixture_mine(&mut self, pos: BlockPos, op: &Value) -> Result> { + let stack = parse_stack(op["item"].as_str().unwrap_or("-"))?; + self.mode = GameMode::Survival; + self.creative = false; + self.player = Some(PlayerMotion::new( + self.motion, + horizon_core::geometry::Vec3::new( + f64::from(pos.x) + 0.5, + f64::from(pos.y) + 1.0, + f64::from(pos.z) + 3.5, + ), + )?); + self.yaw = 0.0; + self.pitch = 90.0; + self.keys.clear(); + self.inventory[self.selected] = stack.clone(); + self.offhand = None; + let state = self.world.block(pos.x, pos.y, pos.z); + let block = self.res.facts().get(state).name.clone(); + let item = stack.as_ref().map(|s| s.item.clone()); + let before = |game: &Self| { + let used = item.as_ref().map_or(0, |i| game.stats.get("used", i)); + (used, game.stats.get("mined", &block)) + }; + let (used, mined) = before(self); + self.break_block([pos.x, pos.y, pos.z])?; + let (used_after, mined_after) = before(self); + let mut out = Vec::new(); + if let Some(item) = &item + && used_after != used + { + out.push(format!("stat {item} minecraft:used {}", used_after - used)); + } + if mined_after != mined { + out.push(format!( + "stat {block} minecraft:mined {}", + mined_after - mined + )); + } + out.push(format!( + "mine held {}", + canon_stack(self.inventory[self.selected].as_ref()) + )); + Ok(out) } /// `EntityType.create` and `snapTo` for one target: no spawn finalization. @@ -819,7 +882,7 @@ impl Game<'_> { } let t = self .res - .mob_types + .mob_types() .get(kind) .cloned() .ok_or_else(|| format!("no mob type {kind}"))?; diff --git a/crates/horizon-client/src/game/dispenser.rs b/crates/horizon-client/src/game/dispenser.rs index cb28aa1cc..0f59e5300 100644 --- a/crates/horizon-client/src/game/dispenser.rs +++ b/crates/horizon-client/src/game/dispenser.rs @@ -328,18 +328,18 @@ impl Game<'_> { self.world.block(p.0, p.1, p.2) } fn dispenser_block_name(&self, p: Pos) -> std::sync::Arc { - self.res.facts.get(self.block_at(p)).name.clone() + self.res.facts().get(self.block_at(p)).name.clone() } fn in_block_tag(&self, tag: &str, block: &str) -> bool { self.res - .items + .items() .block_tags .get(tag) .is_some_and(|s| s.contains(block)) } fn water_at(&self, p: Pos) -> bool { matches!( - self.res.behaviours.fluid_kind(self.block_at(p)), + self.res.behaviours().fluid_kind(self.block_at(p)), FluidKind::Water | FluidKind::FlowingWater ) } @@ -520,7 +520,7 @@ impl Game<'_> { if let Some(entity) = self.world.block_entities.get_mut(&pos) { let max = self .res - .items + .items() .get(&rest.item) .map_or(64, |i| i.max_stack) .max(1); @@ -555,7 +555,11 @@ impl Game<'_> { } /// `ItemStack.hurtAndBreak(amount)` without an entity. fn hurt_and_break(&mut self, mut stack: ItemStack, amount: u32) -> Option { - let max = self.res.items.get(&stack.item).map_or(0, |i| i.max_damage); + let max = self + .res + .items() + .get(&stack.item) + .map_or(0, |i| i.max_damage); if max == 0 { return Some(stack); } @@ -637,7 +641,7 @@ impl Game<'_> { ) -> Result { let equippable = self .res - .items + .items() .get(&stack.item) .is_some_and(|i| i.equip_slot.is_some()); if equippable { @@ -649,7 +653,7 @@ impl Game<'_> { } let swallowable = self .res - .items + .items() .item_tags .get("minecraft:sulfur_cube_swallowable") .is_some_and(|t| t.contains(&*stack.item)); @@ -748,7 +752,7 @@ impl Game<'_> { } else { format!("minecraft:{kind}") }; - let t = self.res.mob_types.get(&name)?.clone(); + let t = self.res.mob_types().get(&name)?.clone(); let y_off = if try_move_down { self.drop_offset(t.width, t.height, p) } else { @@ -768,7 +772,7 @@ impl Game<'_> { /// `EquipmentDispenseItemBehavior.dispenseEquipment`: the first living /// entity in front that `canEquipWithDispenser` gets one of the item. fn dispense_equipment(&mut self, pos: Pos, facing: Direction, stack: &mut ItemStack) -> bool { - let Some(info) = self.res.items.get(&stack.item) else { + let Some(info) = self.res.items().get(&stack.item) else { return false; }; let (Some(slot), true) = ( @@ -1078,7 +1082,7 @@ impl Game<'_> { let below = (front.0, front.1 - 1, front.2); let y_offset = if self.water_at(front) { 1.0 - } else if self.res.facts.is_air(self.block_at(front)) && self.water_at(below) { + } else if self.res.facts().is_air(self.block_at(front)) && self.water_at(below) { 0.0 } else { return (self.dispense_default(pos, facing, stack), CLICK); @@ -1116,7 +1120,7 @@ impl Game<'_> { let y_offset = if let Some(rail) = self.rail(front.0, front.1, front.2) { if rail.shape.is_slope() { 0.6 } else { 0.1 } } else { - if !self.res.facts.is_air(self.block_at(front)) { + if !self.res.facts().is_air(self.block_at(front)) { return (self.dispense_default(pos, facing, stack), CLICK); } let below = (front.0, front.1 - 1, front.2); @@ -1389,7 +1393,7 @@ impl Game<'_> { stack: ItemStack, ) -> Result { let target = offset(pos, facing); - let free = self.res.facts.is_air(self.block_at(target)) + let free = self.res.facts().is_air(self.block_at(target)) && target.1 >= horizon_client::chunk::MIN_Y + 2 && self.ui.difficulty != 0 && self.can_build_wither(target); @@ -1443,7 +1447,7 @@ impl Game<'_> { "minecraft:soul_sand" | "minecraft:soul_soil" ) }; - let air = |p: Pos| self.res.facts.is_air(self.block_at(p)); + let air = |p: Pos| self.res.facts().is_air(self.block_at(p)); for (ax, az) in [(1, 0), (0, 1)] { for col in 0..3 { let left = (head.0 - ax * col, head.1, head.2 - az * col); @@ -1466,7 +1470,7 @@ impl Game<'_> { stack: ItemStack, ) -> Result { let target = offset(pos, facing); - if self.res.facts.is_air(self.block_at(target)) + if self.res.facts().is_air(self.block_at(target)) && self.can_spawn_golem([target.0, target.1, target.2]) { if let Some(pumpkin) = self @@ -1511,7 +1515,7 @@ impl Game<'_> { let front = offset(pos, facing); let below_empty = self .res - .facts + .facts() .is_air(self.block_at((front.0, front.1 - 1, front.2))); let clicked_face = if below_empty { facing } else { Direction::Up }; let ctx = PlaceContext { @@ -1539,7 +1543,7 @@ impl Game<'_> { return Ok((Some(stack), FAIL)); }; let target = (placed.x, placed.y, placed.z); - if let Some(entity) = BlockEntity::for_block(&self.res.facts.get(state).name) { + if let Some(entity) = BlockEntity::for_block(&self.res.facts().get(state).name) { self.world.block_entities.insert(target, entity); self.apply_placed_item([target.0, target.1, target.2], &stack, false); } @@ -1689,14 +1693,14 @@ impl Game<'_> { table: &str, held: Option<&ItemStack>, ) -> Vec { - match self.res.items.loot_sequence(parent) { + match self.res.items().loot_sequence(parent) { Some(sequence) => { let random = self.rand_sequences.get(&sequence, self.level.seed); - self.res.items.loot_table_from(table, false, held, random) + self.res.items().loot_table_from(table, false, held, random) } None => self .res - .items + .items() .loot_table(table, false, held, &mut self.world.random), } } @@ -1717,7 +1721,7 @@ impl Game<'_> { /// random draws) keeps the name, flags, persistence and age; the old one /// is discarded. pub(super) fn convert_mob(&mut self, index: usize, kind: &str) -> Option { - let t = self.res.mob_types.get(kind).cloned()?; + let t = self.res.mob_types().get(kind).cloned()?; let old = &self.mobs[index]; self.next_entity += 1; let mut mob = Mob::new(self.next_entity, t, old.position, old.yaw); diff --git a/crates/horizon-client/src/game/dragon.rs b/crates/horizon-client/src/game/dragon.rs index ff91df583..cbfad527b 100644 --- a/crates/horizon-client/src/game/dragon.rs +++ b/crates/horizon-client/src/game/dragon.rs @@ -74,6 +74,8 @@ impl Respawn { } #[derive(Clone, Debug)] pub(super) struct Crystal { + /// The entity id a server tracks it by (0 until it is tracked). + pub id: u32, pub position: Vec3, pub show_bottom: bool, /// EndCrystal.time (the bob and spin), starting random. @@ -445,9 +447,9 @@ impl Game<'_> { } fn end_portal_active(&self) -> bool { (0..256).any(|y| { - &*self.res.facts.get(self.world.block(0, y, 0)).name == "minecraft:bedrock" + &*self.res.facts().get(self.world.block(0, y, 0)).name == "minecraft:bedrock" && (-3..=3).any(|d| { - &*self.res.facts.get(self.world.block(d, y, 0)).name == "minecraft:end_portal" + &*self.res.facts().get(self.world.block(d, y, 0)).name == "minecraft:end_portal" }) }) } @@ -553,6 +555,7 @@ impl Game<'_> { } let time = self.world.random.next_int_bound(100_000).unwrap_or(0); self.crystals.push(Crystal { + id: 0, position: at, show_bottom: true, time, @@ -588,7 +591,7 @@ impl Game<'_> { let p = self.crystals[i].position; let b = [p.x.floor() as i32, p.y.floor() as i32, p.z.floor() as i32]; if let Some(fire) = fire - && self.res.facts.is_air(self.world.block(b[0], b[1], b[2])) + && self.res.facts().is_air(self.world.block(b[0], b[1], b[2])) && self.world.loaded(b[0], b[2]) { let _ = self.level_set(b, fire); @@ -761,7 +764,7 @@ impl Game<'_> { let held = self.held().cloned(); let modifiers = held .as_ref() - .and_then(|h| self.res.items.get(&h.item)) + .and_then(|h| self.res.items().get(&h.item)) .map(|i| (i.attack_damage, i.attack_speed)) .unwrap_or((0.0, 0.0)); let speed = (4.0 + modifiers.1).max(0.1); @@ -810,7 +813,7 @@ impl Game<'_> { (0..256) .rev() .find(|&y| { - let f = self.res.facts.get(self.world.block(x, y, z)); + let f = self.res.facts().get(self.world.block(x, y, z)); (f.full_collision || f.legacy_solid || f.fluid != horizon_client::blocks::Fluid::None) @@ -1254,7 +1257,7 @@ impl Game<'_> { let (x, z) = (head.x + look.x * 2.5, head.z + look.y * 2.5); let mut y = head.y + 0.5; while y > 0.0 - && self.res.facts.is_air(self.world.block( + && self.res.facts().is_air(self.world.block( x.floor() as i32, y.floor() as i32, z.floor() as i32, @@ -1591,7 +1594,7 @@ impl Game<'_> { return; }; let boxes = [d.part_box(0), d.part_box(1), d.part_box(2)]; - let tags = &self.res.items.block_tags; + let tags = &self.res.items().block_tags; let transparent = tags .get("minecraft:dragon_transparent") .cloned() @@ -1607,8 +1610,8 @@ impl Game<'_> { for y in lo.y.floor() as i32..=hi.y.floor() as i32 { for z in lo.z.floor() as i32..=hi.z.floor() as i32 { let state = self.world.block(x, y, z); - let name = &self.res.facts.get(state).name; - if self.res.facts.is_air(state) || transparent.contains(&**name) { + let name = &self.res.facts().get(state).name; + if self.res.facts().is_air(state) || transparent.contains(&**name) { continue; } if griefing && !immune.contains(&**name) { @@ -1710,7 +1713,7 @@ impl Game<'_> { let y = (0..256) .rev() .find(|&y| { - let f = self.res.facts.get(self.world.block(0, y, 0)); + let f = self.res.facts().get(self.world.block(0, y, 0)); f.full_collision || f.legacy_solid }) .map_or(64, |y| y + 1); @@ -1764,7 +1767,7 @@ impl Game<'_> { next.y.floor() as i32, next.z.floor() as i32, ]; - let f = self.res.facts.get(self.world.block(p[0], p[1], p[2])); + let f = self.res.facts().get(self.world.block(p[0], p[1], p[2])); f.full_collision || f.legacy_solid }; let hit_player = player.is_some_and(|pb| { @@ -1866,9 +1869,9 @@ impl Game<'_> { batch.width as i32, batch.height as i32, &bars, - &self.atlas.font, + &self.gui.atlas.font, ); - hud_layout::paint(batch, &self.atlas, &draws); + hud_layout::paint(batch, &self.gui.atlas, &draws); } /// The dragon fight's boss bar shows (`EnderDragonFight.dragonEvent`, /// with `setCreateWorldFog`). @@ -1906,7 +1909,13 @@ impl Game<'_> { return; }; let a = alpha as f32; - let sprite = |n: &str| self.atlas.sprites.get(n).map(|r| [r.u0, r.v0, r.u1, r.v1]); + let sprite = |n: &str| { + self.gui + .atlas + .sprites + .get(n) + .map(|r| [r.u0, r.v0, r.u1, r.v1]) + }; let (Some(skin), Some(eyes)) = ( sprite("entity/enderdragon/dragon"), sprite("entity/enderdragon/dragon_eyes"), @@ -2072,7 +2081,7 @@ impl Game<'_> { } /// `submitRays`: the death rays from a fixed seed, fading in white to magenta. fn dragon_rays(&self, base: glam::Mat4, t: f32, out: &mut Vec) { - let white = self.atlas.font.white(); + let white = self.gui.atlas.font.white(); let uv = [white.u0, white.v0]; let over = if t > 0.8 { ((t - 0.8) / 0.2).min(1.0) @@ -2128,6 +2137,7 @@ impl Game<'_> { out: &mut Vec, ) { let Some(s) = self + .gui .atlas .sprites .get("entity/end_crystal/end_crystal_beam") @@ -2189,6 +2199,7 @@ impl Game<'_> { out: &mut Vec, ) { let Some(s) = self + .gui .atlas .sprites .get("entity/enderdragon/dragon_fireball") @@ -2301,6 +2312,7 @@ impl Crystal { pub(super) fn from_nbt(tag: &Tag, time: i32) -> Option { let (position, _, _) = horizon_client::entity_nbt::boat_from_nbt(tag)?; Some(Self { + id: 0, position, show_bottom: !matches!(tag.get("ShowBottom"), Some(Tag::Byte(0))), time, @@ -2377,7 +2389,7 @@ impl Game<'_> { } let name = self .res - .facts + .facts() .get(self.world.block(hit.pos[0], hit.pos[1], hit.pos[2])) .name .clone(); @@ -2387,7 +2399,7 @@ impl Game<'_> { let above = [hit.pos[0], hit.pos[1] + 1, hit.pos[2]]; if !self .res - .facts + .facts() .is_air(self.world.block(above[0], above[1], above[2])) { return false; @@ -2403,6 +2415,7 @@ impl Game<'_> { } let time = self.world.random.next_int_bound(100_000).unwrap_or(0); self.crystals.push(Crystal { + id: 0, position: Vec3::new(x + 0.5, y, z + 0.5), show_bottom: false, time, @@ -2507,7 +2520,7 @@ impl Game<'_> { } fn block_name_at(&self, p: [i32; 3]) -> std::sync::Arc { self.res - .facts + .facts() .get(self.world.block(p[0], p[1], p[2])) .name .clone() @@ -2609,7 +2622,7 @@ impl Game<'_> { for y in h - 10..=h + 10 { for z in cz - 10..=cz + 10 { for x in cx - 10..=cx + 10 { - if !self.res.facts.is_air(self.world.block(x, y, z)) { + if !self.res.facts().is_air(self.world.block(x, y, z)) { let _ = self.level_set([x, y, z], air); } } @@ -2631,7 +2644,7 @@ impl Game<'_> { if &*self.block_name_at([x, y, z]) != "minecraft:obsidian" { let _ = self.set_state_named([x, y, z], "minecraft:obsidian", &[]); } - } else if y > 65 && !self.res.facts.is_air(self.world.block(x, y, z)) { + } else if y > 65 && !self.res.facts().is_air(self.world.block(x, y, z)) { let _ = self.level_set([x, y, z], air); } } @@ -2665,6 +2678,7 @@ impl Game<'_> { .retain(|c| c.position.distance_squared(at) > 0.01); let time = self.world.random.next_int_bound(100_000).unwrap_or(0); self.crystals.push(Crystal { + id: 0, position: at, show_bottom: true, time, diff --git a/crates/horizon-client/src/game/enchant_effects.rs b/crates/horizon-client/src/game/enchant_effects.rs index 1e36b00f0..0aa3138f6 100644 --- a/crates/horizon-client/src/game/enchant_effects.rs +++ b/crates/horizon-client/src/game/enchant_effects.rs @@ -16,7 +16,7 @@ impl Game<'_> { let held = self.inventory[self.selected].as_ref(); speed += self .res - .stations + .stations() .enchantments .attribute_add(held, "minecraft:mining_efficiency") as f32; } @@ -26,7 +26,7 @@ impl Game<'_> { pub(super) fn submerged_mining_speed(&self) -> f32 { let mut total = 0.0; for stack in self.armor.iter().flatten() { - for (attr, _, amount, op) in self.res.stations.enchantments.attributes(stack) { + for (attr, _, amount, op) in self.res.stations().enchantments.attributes(stack) { if attr == "minecraft:submerged_mining_speed" && op == "add_multiplied_total" { total += amount; } @@ -36,13 +36,13 @@ impl Game<'_> { } /// `EnchantmentHelper.processDurabilityChange` (unbreaking) for `amount` points. pub(super) fn durability_loss(&mut self, stack: &ItemStack, amount: u32) -> u32 { - let e = &self.res.stations.enchantments; + let e = &self.res.stations().enchantments; if !e.has_component(stack, "minecraft:item_damage") { return amount; } let ctx = EffectContext { tool: Some(&stack.item), - items: Some(&self.res.items), + items: Some(self.res.items()), ..Default::default() }; e.modify_value( @@ -65,10 +65,10 @@ impl Game<'_> { direct_attacker: Some("minecraft:player"), is_direct: true, damage_type: Some("minecraft:player_attack"), - items: Some(&self.res.items), + items: Some(self.res.items()), ..Default::default() }; - self.res.stations.enchantments.modify_value( + self.res.stations().enchantments.modify_value( Some(&held), "minecraft:damage", &ctx, @@ -96,10 +96,10 @@ impl Game<'_> { attacker: Some("minecraft:player"), direct_attacker: Some("minecraft:player"), is_direct: true, - items: Some(&self.res.items), + items: Some(self.res.items()), ..Default::default() }; - let knockback = self.res.stations.enchantments.modify_value( + let knockback = self.res.stations().enchantments.modify_value( Some(&held), "minecraft:knockback", &ctx, @@ -130,7 +130,7 @@ impl Game<'_> { // minecraft:post_attack ignite effects of the attacker's weapon (fire aspect). let mut ignite = 0.0f32; for (id, level) in held.enchantments() { - let Some(def) = self.res.stations.enchantments.get(id) else { + let Some(def) = self.res.stations().enchantments.get(id) else { continue; }; for effect in def.effects["minecraft:post_attack"] @@ -145,7 +145,7 @@ impl Game<'_> { let mut random = self.world.random.clone(); let ok = effect.get("requirements").is_none_or(|r| { self.res - .stations + .stations() .enchantments .condition(r, *level, &ctx, &mut random) }); @@ -182,13 +182,13 @@ impl Game<'_> { attacker: Some("minecraft:player"), direct_attacker: Some("minecraft:player"), is_direct: true, - items: Some(&self.res.items), + items: Some(self.res.items()), direct_motion: Some(motion), ..Default::default() }; let mut bursts = Vec::new(); for (id, level) in held.enchantments() { - let Some(def) = self.res.stations.enchantments.get(id) else { + let Some(def) = self.res.stations().enchantments.get(id) else { continue; }; for effect in def.effects["minecraft:post_attack"] @@ -205,7 +205,7 @@ impl Game<'_> { continue; } let ok = effect.get("requirements").is_none_or(|r| { - self.res.stations.enchantments.condition( + self.res.stations().enchantments.condition( r, *level, &ctx, @@ -254,16 +254,16 @@ impl Game<'_> { if level == 0 { continue; } - let Some(def) = self.res.stations.enchantments.get("minecraft:thorns") else { + let Some(def) = self.res.stations().enchantments.get("minecraft:thorns") else { continue; }; let effect = def.effects["minecraft:post_attack"][0].clone(); let ctx = EffectContext { - items: Some(&self.res.items), + items: Some(self.res.items()), ..Default::default() }; let mut random = self.world.random.clone(); - let ok = self.res.stations.enchantments.condition( + let ok = self.res.stations().enchantments.condition( &effect["requirements"], level, &ctx, @@ -286,7 +286,7 @@ impl Game<'_> { s.damage += 2; if self .res - .items + .items() .get(&s.item) .is_some_and(|i| s.damage >= i.max_damage) { @@ -330,10 +330,10 @@ impl Game<'_> { }; let Some(stack) = stack else { return amount }; let ctx = EffectContext { - items: Some(&self.res.items), + items: Some(self.res.items()), ..Default::default() }; - let to_repair_xp = self.res.stations.enchantments.modify_value( + let to_repair_xp = self.res.stations().enchantments.modify_value( Some(&stack), "minecraft:repair_with_xp", &ctx, @@ -364,11 +364,11 @@ impl Game<'_> { let Some(bow) = self.inventory[self.selected].clone() else { return (0.0, false); }; - let e = &self.res.stations.enchantments; + let e = &self.res.stations().enchantments; let ctx = EffectContext { direct_attacker: Some("minecraft:arrow"), attacker: Some("minecraft:player"), - items: Some(&self.res.items), + items: Some(self.res.items()), ..Default::default() }; let power = e.modify_value( @@ -380,7 +380,7 @@ impl Game<'_> { ); let ammo_ctx = EffectContext { tool: Some("minecraft:arrow"), - items: Some(&self.res.items), + items: Some(self.res.items()), ..Default::default() }; let infinity = e.has_component(&bow, "minecraft:ammo_use") @@ -399,12 +399,12 @@ impl Game<'_> { let bow = self.inventory[self.selected].clone()?; let ctx = EffectContext { this: Some("minecraft:arrow"), - items: Some(&self.res.items), + items: Some(self.res.items()), ..Default::default() }; let mut ignite: Option = None; for (id, level) in bow.enchantments() { - let Some(def) = self.res.stations.enchantments.get(id) else { + let Some(def) = self.res.stations().enchantments.get(id) else { continue; }; for effect in def.effects["minecraft:projectile_spawned"] @@ -416,7 +416,7 @@ impl Game<'_> { continue; } let ok = effect.get("requirements").is_none_or(|r| { - self.res.stations.enchantments.condition( + self.res.stations().enchantments.condition( r, *level, &ctx, @@ -436,7 +436,7 @@ impl Game<'_> { pub(super) fn apply_enchantment_attributes(&mut self) { let mut wanted: Vec<(String, String, f64, String)> = Vec::new(); for stack in self.armor.iter().flatten() { - wanted.extend(self.res.stations.enchantments.attributes(stack)); + wanted.extend(self.res.stations().enchantments.attributes(stack)); } let Some(p) = self.player.as_mut() else { return; diff --git a/crates/horizon-client/src/game/end.rs b/crates/horizon-client/src/game/end.rs index d02904872..a6d91d563 100644 --- a/crates/horizon-client/src/game/end.rs +++ b/crates/horizon-client/src/game/end.rs @@ -132,9 +132,9 @@ impl Game<'_> { /// The environment attributes of the current dimension. pub(super) fn dim_environment(&self) -> &Environment { match self.dim { - Dim::Overworld => &self.res.environment, - Dim::Nether => &self.res.nether_environment, - Dim::End => &self.res.end_environment, + Dim::Overworld => self.res.environment(), + Dim::Nether => self.res.nether_environment(), + Dim::End => self.res.end_environment(), } } @@ -142,7 +142,7 @@ impl Game<'_> { /// tinted 0x282828. The atlas does not wrap, so each face is 16×16 quads. pub(super) fn end_sky_geometry(&self) -> Vec { let mut out = Vec::with_capacity(6 * 256 * 4); - let Some(r) = self.atlas.sprites.get("environment/end_sky").copied() else { + let Some(r) = self.gui.atlas.sprites.get("environment/end_sky").copied() else { return out; }; use std::f32::consts::FRAC_PI_2; @@ -184,6 +184,7 @@ impl Game<'_> { let intensity = self.end.flash.intensity(self.clock.fraction() as f32); if intensity > 1e-5 && let Some(r) = self + .gui .atlas .sprites .get("environment/celestial/end_flash") @@ -222,9 +223,9 @@ impl Game<'_> { } } - fn block_name(&self, p: [i32; 3]) -> std::sync::Arc { + pub(super) fn block_name(&self, p: [i32; 3]) -> std::sync::Arc { self.res - .facts + .facts() .get(self.world.block(p[0], p[1], p[2])) .name .clone() @@ -242,6 +243,10 @@ impl Game<'_> { (state.name == "minecraft:end_portal_frame").then(|| (hit, state.clone())) }); let Some((hit, state)) = target else { + // EnderEyeItem.useOn passes off a frame: a guest's UseItem throws it. + if matches!(self.guest_use, Some(super::GuestUse::OnBlock(_))) { + return Ok(false); + } // EnderEyeItem.use: throw the eye towards the nearest stronghold. self.throw_ender_eye(); return Ok(true); @@ -366,31 +371,11 @@ impl Game<'_> { if self.travelling.is_some() || p.health.dead_or_dying() { return Ok(()); } - let w = f64::from(p.dimensions.width) / 2.0; - let lo = [p.position.x - w, p.position.y, p.position.z - w]; - let hi = [ - p.position.x + w, - p.position.y + f64::from(p.dimensions.height), - p.position.z + w, - ]; - let mut portal = false; - let mut gateway = None; - for x in lo[0].floor() as i32..=hi[0].floor() as i32 { - for y in lo[1].floor() as i32..=hi[1].floor() as i32 { - for z in lo[2].floor() as i32..=hi[2].floor() as i32 { - let name = self.block_name([x, y, z]); - // EndPortalBlock entity-inside shape: y 6/16..12/16. - if &*name == "minecraft:end_portal" - && lo[1] < f64::from(y) + 0.75 - && hi[1] > f64::from(y) + 0.375 - { - portal = true; - } else if &*name == "minecraft:end_gateway" && self.dim == Dim::End { - gateway = Some([x, y, z]); - } - } - } - } + let (portal, gateway) = self.inside_end_portals( + p.position, + f64::from(p.dimensions.width), + f64::from(p.dimensions.height), + ); if portal { if self.portals.player.cooldown == 0 { let target = if self.dim == Dim::End { @@ -417,6 +402,38 @@ impl Game<'_> { Ok(()) } + /// Whether a `width` by `height` box at `position` is inside an End + /// portal (its entity-inside shape), and the End gateway it touches. + pub(super) fn inside_end_portals( + &self, + position: Vec3, + width: f64, + height: f64, + ) -> (bool, Option<[i32; 3]>) { + let w = width / 2.0; + let lo = [position.x - w, position.y, position.z - w]; + let hi = [position.x + w, position.y + height, position.z + w]; + let mut portal = false; + let mut gateway = None; + for x in lo[0].floor() as i32..=hi[0].floor() as i32 { + for y in lo[1].floor() as i32..=hi[1].floor() as i32 { + for z in lo[2].floor() as i32..=hi[2].floor() as i32 { + let name = self.block_name([x, y, z]); + // EndPortalBlock entity-inside shape: y 6/16..12/16. + if &*name == "minecraft:end_portal" + && lo[1] < f64::from(y) + 0.75 + && hi[1] > f64::from(y) + 0.375 + { + portal = true; + } else if &*name == "minecraft:end_gateway" && self.dim == Dim::End { + gateway = Some([x, y, z]); + } + } + } + } + (portal, gateway) + } + /// EndPortalBlock.getPortalDestination for the pending enter_dimension. pub(super) fn end_destination(&self, target: Dim) -> Option { if !self.end_portal_travel { @@ -451,10 +468,36 @@ impl Game<'_> { self.previous_eye = 1.62; self.clock.reset(); } - let mut position = target; - if let Some(job) = self.end.job.take() { - position = self.resolve_gateway(job, target)?; - } else if self.dim == Dim::End { + let position = if let Some(job) = self.end.job.take() { + self.resolve_gateway(job, target)? + } else { + let (position, yaw) = self.end_arrival(target)?; + if let Some(yaw) = yaw { + self.yaw = yaw; + player.yaw = yaw; + player.head_yaw = yaw; + } + position + }; + player.position = position; + player.velocity = Vec3::ZERO; + player.fall_distance = 0.0; + self.previous_position = player.position; + self.player = Some(player); + self.pad.player_replaced(); + self.sway = Default::default(); + self.apply_equipment(); + let pitch = self.world.random.next_float() * 0.4 + 0.8; + self.audio + .play("minecraft:block.portal.travel", None, 0.25, pitch); + Ok(()) + } + + /// Where an End portal's traveller lands once `target` has loaded, and + /// the yaw it faces there: the platform below the End's spawn point, + /// facing west, or the top of the Overworld's spawn column. + pub(super) fn end_arrival(&mut self, target: Vec3) -> Result<(Vec3, Option)> { + if self.dim == Dim::End { // EndPlatformFeature.createEndPlatform below the spawn point. let origin = [END_SPAWN[0], END_SPAWN[1] - 1, END_SPAWN[2]]; for dz in -2..=2 { @@ -473,28 +516,13 @@ impl Game<'_> { } } // Direction.WEST.toYRot(). - self.yaw = 90.0; - player.yaw = 90.0; - player.head_yaw = 90.0; - } else { - // ServerPlayer.findRespawnPositionAndUseSpawnBlock: onto the spawn column. - let y = self - .land_column(target.x.floor() as i32, target.z.floor() as i32) - .map_or(target.y, f64::from); - position = Vec3::new(target.x, y, target.z); + return Ok((target, Some(90.0))); } - player.position = position; - player.velocity = Vec3::ZERO; - player.fall_distance = 0.0; - self.previous_position = player.position; - self.player = Some(player); - self.pad.player_replaced(); - self.sway = Default::default(); - self.apply_equipment(); - let pitch = self.world.random.next_float() * 0.4 + 0.8; - self.audio - .play("minecraft:block.portal.travel", None, 0.25, pitch); - Ok(()) + // ServerPlayer.findRespawnPositionAndUseSpawnBlock: onto the spawn column. + let y = self + .land_column(target.x.floor() as i32, target.z.floor() as i32) + .map_or(target.y, f64::from); + Ok((Vec3::new(target.x, y, target.z), None)) } /// Dying away from the Overworld respawns at the world spawn there. @@ -538,7 +566,7 @@ impl Game<'_> { let mut y = (0..256) .rev() .find(|&y| { - let f = self.res.facts.get(self.world.block(0, y, 0)); + let f = self.res.facts().get(self.world.block(0, y, 0)); (f.full_collision || f.fluid != horizon_client::blocks::Fluid::None) && !f.name.ends_with("_leaves") }) @@ -760,7 +788,7 @@ impl Game<'_> { let mut y = 255; while y > floor { let p = [around[0] + xd, y, around[2] + zd]; - let f = self.res.facts.get(self.world.block(p[0], p[1], p[2])); + let f = self.res.facts().get(self.world.block(p[0], p[1], p[2])); if f.full_collision && (allow_bedrock || &*f.name != "minecraft:bedrock") { tallest = Some(p); break; @@ -816,12 +844,12 @@ impl Game<'_> { if &*self.block_name([x, y, z]) != "minecraft:end_stone" || self .res - .facts + .facts() .get(self.world.block(x, y + 1, z)) .full_collision || self .res - .facts + .facts() .get(self.world.block(x, y + 2, z)) .full_collision { @@ -897,7 +925,8 @@ impl Game<'_> { fn end_crystal_geometry(&self, camera: glam::DVec3, out: &mut Vec) { let (Some(model), Some(r)) = ( self.crystal_model.as_ref(), - self.atlas + self.gui + .atlas .sprites .get("entity/end_crystal/end_crystal") .copied(), @@ -1050,7 +1079,7 @@ impl Game<'_> { /// ThrownItemRenderer: the ender eye item facing the camera. fn eye_geometry(&self, camera: glam::DVec3, out: &mut Vec) { - let Some(r) = self.atlas.sprites.get("item/ender_eye").copied() else { + let Some(r) = self.gui.atlas.sprites.get("item/ender_eye").copied() else { return; }; let alpha = self.clock.fraction(); diff --git a/crates/horizon-client/src/game/entity_culling.rs b/crates/horizon-client/src/game/entity_culling.rs index 71ab13378..3abf92835 100644 --- a/crates/horizon-client/src/game/entity_culling.rs +++ b/crates/horizon-client/src/game/entity_culling.rs @@ -21,7 +21,7 @@ impl Game<'_> { && self.settings.resource_packs.iter().all(|id| id == "vanilla") && self.player.is_some() && !self.spectator() - && !opaque(self.res.facts.get(self.world.block(at[0], at[1], at[2])))) + && !opaque(self.res.facts().get(self.world.block(at[0], at[1], at[2])))) .then(|| Culler::new(camera.to_array(), 8_192)); } @@ -31,7 +31,7 @@ impl Game<'_> { if let Some(culler) = &self.entity_culler { culler.cull_vertices(vertices, start, |pos| { let state = self.world.block(pos[0], pos[1], pos[2]); - if !opaque(self.res.facts.get(state)) { + if !opaque(self.res.facts().get(state)) { return false; } let Some(y) = pos[1].checked_sub(MIN_Y) else { @@ -49,3 +49,64 @@ impl Game<'_> { } } } + +/// Entity.shouldRenderAtSqrDistance's default range: 64 times the bounding +/// box's mean edge, scaled by the entity distance option. +pub(super) fn default_range(size: glam::DVec3, view_scale: f32) -> f64 { + let edge = (size.x + size.y + size.z) / 3.0; + let edge = if edge.is_nan() { 1.0 } else { edge }; + edge * 64.0 * f64::from(view_scale) +} + +impl Game<'_> { + /// EntityRenderer.shouldRender: the entity within `range` blocks of the + /// camera, measured from its current `position`, and its culling box + /// (`lo`..`hi`, interpolated, in world space) grown by 0.5 in the view. + pub(super) fn entity_in_view( + &self, + camera: glam::DVec3, + position: glam::DVec3, + range: f64, + lo: glam::DVec3, + hi: glam::DVec3, + ) -> bool { + if (position - camera).length_squared() >= range * range { + return false; + } + self.view_frustum.as_ref().is_none_or(|view| { + view.intersects( + (lo - camera - glam::DVec3::splat(0.5)).as_vec3(), + (hi - camera + glam::DVec3::splat(0.5)).as_vec3(), + ) + }) + } +} + +/// An entity's position as a `DVec3`. +pub(super) fn at(p: horizon_core::geometry::Vec3) -> glam::DVec3 { + glam::DVec3::new(p.x, p.y, p.z) +} + +impl Game<'_> { + /// `entity_in_view` for an entity whose box stands on its position, + /// `width` wide and `height` tall, interpolated by `alpha` and grown by + /// `grow` (a renderer's getBoundingBoxForCulling). + #[allow(clippy::too_many_arguments)] + pub(super) fn standing_entity_in_view( + &self, + camera: glam::DVec3, + previous: glam::DVec3, + position: glam::DVec3, + alpha: f64, + width: f64, + height: f64, + range: f64, + grow: f64, + ) -> bool { + let p = previous.lerp(position, alpha); + let half = glam::DVec3::new(width / 2.0 + grow, grow, width / 2.0 + grow); + let lo = p - half; + let hi = p + glam::DVec3::new(half.x, height + grow, half.z); + self.entity_in_view(camera, position, range, lo, hi) + } +} diff --git a/crates/horizon-client/src/game/events.rs b/crates/horizon-client/src/game/events.rs index 0ffff1b59..8a4207a7f 100644 --- a/crates/horizon-client/src/game/events.rs +++ b/crates/horizon-client/src/game/events.rs @@ -167,7 +167,11 @@ impl Game<'_> { Snapshot { screen: self.screen_label(), dimension: self.dim.id().to_string(), - chat: self.ui.chat.received, + // A remote client counts the server's lines itself. + chat: self + .remote + .as_ref() + .map_or(self.ui.chat.received, |r| r.chat_received), counts: if world.is_some() { self.inventory_counts() } else { @@ -189,8 +193,19 @@ impl Game<'_> { if let Some(before) = &self.events.last && *before != now { - let new_chat = (now.chat - before.chat).min(100) as usize; - let lines = self.ui.chat.recent_lines(new_chat); + // Joining or leaving a server starts another count. + let new_chat = now.chat.saturating_sub(before.chat).min(100) as usize; + // Newest first; the remote client keeps its lines oldest first. + let lines = match &self.remote { + Some(r) => r + .chat + .iter() + .rev() + .take(new_chat) + .map(|(t, _)| t.clone()) + .collect(), + None => self.ui.chat.recent_lines(new_chat), + }; for event in diff(before, &now, &lines) { self.events.push(self.ticks, event); } diff --git a/crates/horizon-client/src/game/explosion.rs b/crates/horizon-client/src/game/explosion.rs index 4ed2772c7..c6df8d9e2 100644 --- a/crates/horizon-client/src/game/explosion.rs +++ b/crates/horizon-client/src/game/explosion.rs @@ -196,7 +196,7 @@ impl Game<'_> { break; } let state = self.world.block(pos[0], pos[1], pos[2]); - let f = self.res.facts.get(state); + let f = self.res.facts().get(state); let fluid = if f.fluid != Fluid::None { 100.0f32 } else { @@ -206,7 +206,7 @@ impl Game<'_> { Some((at, resistance)) if at == pos => { power -= (resistance + 0.3) * 0.3; } - _ if !self.res.facts.is_air(state) || f.fluid != Fluid::None => { + _ if !self.res.facts().is_air(state) || f.fluid != Fluid::None => { power -= (f.explosion_resistance.max(fluid) + 0.3) * 0.3; } _ => {} @@ -450,11 +450,11 @@ impl Game<'_> { if self.world.random.next_int_bound(3).unwrap_or(1) == 0 && self .res - .facts + .facts() .is_air(self.world.block(pos[0], pos[1], pos[2])) && self .res - .facts + .facts() .get(self.world.block(pos[0], pos[1] - 1, pos[2])) .solid_render { @@ -475,7 +475,7 @@ impl Game<'_> { } else { targets.len() }; - let (world, facts) = (&self.world, &self.res.facts); + let (world, facts) = (&self.world, self.res.facts()); self.particles.explosion(center, radius, block_count, &|b| { facts.is_air(world.block(b[0], b[1], b[2])) }); @@ -490,10 +490,10 @@ impl Game<'_> { stacks: &mut Vec<([i32; 3], ItemStack)>, ) -> Result<()> { let state = self.world.block(pos[0], pos[1], pos[2]); - if self.res.facts.is_air(state) { + if self.res.facts().is_air(state) { return Ok(()); } - let name = self.res.facts.get(state).name.clone(); + let name = self.res.facts().get(state).name.clone(); let is_tnt = &*name == "minecraft:tnt"; // Block.onExplosionHit: the drops copy from the block entity, which // then spills its contents as the block turns to air. @@ -510,7 +510,7 @@ impl Game<'_> { // tables' survives_explosion and explosion_decay draw from their // random sequence. let decay = (interaction == BlockInteraction::DestroyWithDecay).then_some(radius); - let drops = self.res.items.block_drops_exploded( + let drops = self.res.items().block_drops_exploded( self.pack.assets.registry, state, None, diff --git a/crates/horizon-client/src/game/extras.rs b/crates/horizon-client/src/game/extras.rs index 712796a81..c16c54447 100644 --- a/crates/horizon-client/src/game/extras.rs +++ b/crates/horizon-client/src/game/extras.rs @@ -188,7 +188,7 @@ impl Game<'_> { let radius = ((reach / 16.0).ceil() as i32 + 1).min(self.settings.render_distance.max(2)); self.extras.minimap.update( &self.world, - &self.res.facts, + self.res.facts(), (bx >> 4, bz >> 4), radius, layer, @@ -197,10 +197,10 @@ impl Game<'_> { if !self.extras.minimap.compose(view) { return; } - let Some(rect) = self.atlas.sprites.get(MINIMAP_SPRITE).copied() else { + let Some(rect) = self.gui.atlas.sprites.get(MINIMAP_SPRITE).copied() else { return; }; - let (w, h) = (self.atlas.width as f32, self.atlas.height as f32); + let (w, h) = (self.gui.atlas.width as f32, self.gui.atlas.height as f32); self.map_patches.push(horizon_client::font::Patch { x: (rect.u0 * w).round() as u32, y: (rect.v0 * h).round() as u32, @@ -246,7 +246,7 @@ impl Game<'_> { let (map_corner, map_size) = (e.minimap_corner, f32::from(e.minimap_size)); let visible = |corner| !(self.debug && corner == Corner::TopLeft); let mut corners = Corners::new(batch.width, batch.height, self.effect_rows_height()); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; if let Some(view) = self.minimap_view() && visible(map_corner) && !self.extras.minimap.image.is_empty() @@ -290,7 +290,7 @@ impl Game<'_> { use_: self.extras.mouse[1], cps, }; - extras_hud::draw_keystrokes(batch, &self.atlas, &mut corners, keys_corner, &k); + extras_hud::draw_keystrokes(batch, &self.gui.atlas, &mut corners, keys_corner, &k); } if mod_panels { self.draw_mod_panels(batch, &mut corners); diff --git a/crates/horizon-client/src/game/falling_damage.rs b/crates/horizon-client/src/game/falling_damage.rs index 16a003c04..8eec28d1d 100644 --- a/crates/horizon-client/src/game/falling_damage.rs +++ b/crates/horizon-client/src/game/falling_damage.rs @@ -6,7 +6,7 @@ use horizon_world::level::LevelEvent; impl Game<'_> { pub(super) fn finish_falling_block(&mut self, mut block: FallingBlock, pos: [i32; 3]) { - let anvil = self.res.facts.get(block.state).name.ends_with("anvil"); + let anvil = self.res.facts().get(block.state).name.ends_with("anvil"); if let Some(impact) = block.damage.impact(block.fall_distance) { if impact.amount > 0.0 { falling_block::hurt_mobs(&mut self.mobs, block.position, impact.amount); @@ -23,7 +23,7 @@ impl Game<'_> { .guests .iter() .filter(|g| { - g.placed + host.hub.here(g) && g.loaded && !g.dead && !g.creative() @@ -80,7 +80,7 @@ impl Game<'_> { && block.drop_item && self.ui.rules.bool("entity_drops") { - let item = self.res.facts.get(block.state).item.clone(); + let item = self.res.facts().get(block.state).item.clone(); if &*item != "minecraft:air" { self.pop_resource( pos, diff --git a/crates/horizon-client/src/game/fangs.rs b/crates/horizon-client/src/game/fangs.rs index dd2b9bde6..0939f6490 100644 --- a/crates/horizon-client/src/game/fangs.rs +++ b/crates/horizon-client/src/game/fangs.rs @@ -108,6 +108,7 @@ impl Game<'_> { return; }; let Some(r) = self + .gui .atlas .sprites .get("entity/illager/evoker_fangs") diff --git a/crates/horizon-client/src/game/fireworks.rs b/crates/horizon-client/src/game/fireworks.rs index 0e074025f..85fccc2de 100644 --- a/crates/horizon-client/src/game/fireworks.rs +++ b/crates/horizon-client/src/game/fireworks.rs @@ -18,21 +18,46 @@ use horizon_core::{ }; pub(super) struct Rocket { - position: Vec3, + /// The entity id a server tracks it by (0 until it is tracked). + pub id: u32, + pub(super) position: Vec3, previous: Vec3, - velocity: Vec3, + pub(super) velocity: Vec3, life: i32, lifetime: i32, explosions: Vec, /// Boosting the player (attachedToEntity). - attached: bool, + pub(super) attached: bool, /// `DATA_SHOT_AT_ANGLE` (dispensers): constant velocity, exploding on /// the first block it hits. - angled: bool, + pub(super) angled: bool, /// `DATA_ID_FIREWORKS_ITEM`: the stack it was launched from. - item: ItemStack, + pub(super) item: ItemStack, } impl Rocket { + /// A rocket a server sent (`handleAddEntity`): placed and moved by it. + pub(super) fn remote(id: u32, position: Vec3, velocity: Vec3) -> Self { + let item = ItemStack::new("minecraft:firework_rocket", 1); + let (_, explosions) = fireworks_of(&item); + Self { + id, + position, + previous: position, + velocity, + life: 0, + lifetime: i32::MAX, + explosions, + attached: false, + angled: false, + item, + } + } + /// One tick of a server's client: the previous position, then `step`. + pub(super) fn remote_step(&mut self, step: impl FnOnce(Vec3) -> Vec3) { + self.previous = self.position; + self.life += 1; + self.position = step(self.position); + } /// Position and motion (the dispenser parity fixture). pub(super) fn motion(&self) -> (Vec3, Vec3) { (self.position, self.velocity) @@ -56,6 +81,7 @@ impl Game<'_> { + r.next_int_bound(6).unwrap_or(0) + r.next_int_bound(7).unwrap_or(0); Rocket { + id: 0, position: at, previous: at, velocity, @@ -253,6 +279,20 @@ impl Game<'_> { pub(super) fn rocket_geometry(&self, camera: glam::DVec3, alpha: f64, out: &mut Vec) { let orientation = glam::Mat4::from_quat(self.camera_orientation()); for r in self.rockets.iter().filter(|r| !r.attached) { + // FireworkRocketEntity.shouldRenderAtSqrDistance: within 64 blocks; + // EntityRenderer.shouldRender: its 0.25 box in the view. + if !self.standing_entity_in_view( + camera, + super::entity_culling::at(r.previous), + super::entity_culling::at(r.position), + alpha, + 0.25, + 0.25, + 64.0, + 0.0, + ) { + continue; + } let p = glam::DVec3::new( r.previous.x + (r.position.x - r.previous.x) * alpha, r.previous.y + (r.position.y - r.previous.y) * alpha, diff --git a/crates/horizon-client/src/game/fishing.rs b/crates/horizon-client/src/game/fishing.rs index 8a63f2cfe..94067e7f9 100644 --- a/crates/horizon-client/src/game/fishing.rs +++ b/crates/horizon-client/src/game/fishing.rs @@ -226,14 +226,18 @@ impl Game<'_> { fn biome_name_at(&self, p: Vec3) -> Option { let (x, y, z) = (p.x.floor() as i32, p.y.floor() as i32, p.z.floor() as i32); let c = self.world.chunks.get(&(x >> 4, z >> 4))?; - Some(self.res.biomes.list[c.biome(x, y, z) as usize].name.clone()) + Some( + self.res.biomes().list[c.biome(x, y, z) as usize] + .name + .clone(), + ) } fn water_height(&self, x: i32, y: i32, z: i32) -> Option<(f32, bool)> { - let f = self.res.facts.get(self.world.block(x, y, z)); + let f = self.res.facts().get(self.world.block(x, y, z)); if f.fluid != Fluid::Water { return None; } - let above = self.res.facts.get(self.world.block(x, y + 1, z)); + let above = self.res.facts().get(self.world.block(x, y + 1, z)); let h = if above.fluid == Fluid::Water { 1.0 } else { @@ -243,7 +247,7 @@ impl Game<'_> { } /// getOpenWaterTypeForBlock: 0 above water, 1 inside water, 2 invalid. fn open_water_type(&self, x: i32, y: i32, z: i32) -> u8 { - let f = self.res.facts.get(self.world.block(x, y, z)); + let f = self.res.facts().get(self.world.block(x, y, z)); if self.world.is_air(self.world.block(x, y, z)) || &*f.name == "minecraft:lily_pad" { return 0; } @@ -390,7 +394,7 @@ impl Game<'_> { (fish[1] - 1.0).floor() as i32, fish[2].floor() as i32, ); - &*self.res.facts.get(state).name == "minecraft:water" + &*self.res.facts().get(state).name == "minecraft:water" } /// `ServerLevel.sendParticles` of a simple particle. fn send_particles( @@ -565,6 +569,7 @@ impl Game<'_> { return; }; let Some(r) = self + .gui .atlas .sprites .get("entity/fishing/fishing_hook") diff --git a/crates/horizon-client/src/game/fov.rs b/crates/horizon-client/src/game/fov.rs index 7825d6514..37796c8c3 100644 --- a/crates/horizon-client/src/game/fov.rs +++ b/crates/horizon-client/src/game/fov.rs @@ -55,7 +55,13 @@ impl Game<'_> { .fov_player() .map_or(1.0, |p| field_of_view_modifier(&p, first_person, effect)); let target = self.zoomed_fov_target(target); - let eye = self.player.as_ref().map(|p| p.dimensions.eye_height); + let eye = self.player.as_ref().map(|p| { + if self.sleeping.is_some() { + 0.2 + } else { + p.dimensions.eye_height + } + }); self.view.tick(eye, target); } /// The player's `deathTime` while dead or dying. @@ -69,7 +75,7 @@ impl Game<'_> { } /// The fluid at the camera, as `Camera.getFluidInCamera` reports it. fn camera_fluid(&self, camera: [f64; 3]) -> CameraFluid { - match fog_type(&self.world, &self.res.facts, camera, self.forward()) { + match fog_type(&self.world, self.res.facts(), camera, self.forward()) { FogType::Water => CameraFluid::Water, FogType::Lava => CameraFluid::Lava, FogType::PowderSnow => CameraFluid::PowderSnow, @@ -106,7 +112,7 @@ impl Game<'_> { let hurt = &p.health.hurt; let state = BobState { living: Some(HurtBob { - hurt_time: hurt.hurt_time as f32 - partial, + hurt_time: p.health.hurt_animation_time as f32 - partial, hurt_duration: hurt.hurt_duration, hurt_dir: 0.0, death_time: self.death_time().map(|t| t as f32 + partial), @@ -171,6 +177,6 @@ impl Game<'_> { } let draws = hud_overlays::spyglass(batch.width as i32, batch.height as i32, self.scope_scale); - hud_layout::paint(batch, &self.atlas, &draws); + hud_layout::paint(batch, &self.gui.atlas, &draws); } } diff --git a/crates/horizon-client/src/game/frames.rs b/crates/horizon-client/src/game/frames.rs index 3ffa0e67e..7f5e6c26a 100644 --- a/crates/horizon-client/src/game/frames.rs +++ b/crates/horizon-client/src/game/frames.rs @@ -110,7 +110,7 @@ impl Game<'_> { return false; } let s = f.step(); - let behind = self.res.facts.get(self.world.block( + let behind = self.res.facts().get(self.world.block( f.pos[0] - s[0], f.pos[1] - s[1], f.pos[2] - s[2], @@ -282,8 +282,15 @@ impl Game<'_> { } /// BlockAttachedEntity.tick for frames. pub(super) fn tick_item_frames(&mut self) { + // ServerLevel.tick: only entities in entity-ticking chunks tick. + let ticking = self.entity_ticking_area(); let mut i = 0; while i < self.item_frames.len() { + let at = center(&self.item_frames[i]); + if !ticking(at[0].floor() as i32, at[2].floor() as i32) { + i += 1; + continue; + } self.item_frames[i].check_interval += 1; if self.item_frames[i].check_interval >= 100 { self.item_frames[i].check_interval = 0; @@ -301,12 +308,30 @@ impl Game<'_> { } /// ItemFrameRenderer: the frame model, then the item half size on its face. pub(super) fn frame_geometry(&mut self, camera: glam::DVec3, out: &mut Vec) { - let sprites = &self.atlas.sprites; + let sprites = &self.gui.atlas.sprites; let mut maps: Vec<(i32, glam::Mat4, u32)> = Vec::new(); let mut items: Vec<(ItemStack, u32, glam::Mat4, u8)> = Vec::new(); + // ItemFrame.shouldRenderAtSqrDistance: 1024 blocks at 100%. + let range = 16.0 * 64.0 * f64::from(self.settings.entity_distance); for f in &self.item_frames { let entity_start = out.len(); let map = f.item.as_ref().and_then(super::maps::map_id); + // EntityRenderer.shouldRender: its box (a block wide with a map), + // measured from the box's centre. + let (lo, hi) = f.bounds(); + let (mut lo, mut hi) = (glam::DVec3::from(lo), glam::DVec3::from(hi)); + if map.is_some() { + let s = f.step(); + for i in 0..3 { + if s[i] == 0 { + let c = (lo[i] + hi[i]) / 2.0; + (lo[i], hi[i]) = (c - 0.5, c + 0.5); + } + } + } + if !self.entity_in_view(camera, (lo + hi) / 2.0, range, lo, hi) { + continue; + } let s = f.step(); // Pose: to the block centre, then X/Y rotation by facing. let (x_rot, y_rot) = if s[1] == 0 { diff --git a/crates/horizon-client/src/game/gamemode_switcher.rs b/crates/horizon-client/src/game/gamemode_switcher.rs index 5f73080f4..db057ad25 100644 --- a/crates/horizon-client/src/game/gamemode_switcher.rs +++ b/crates/horizon-client/src/game/gamemode_switcher.rs @@ -39,7 +39,7 @@ impl Game<'_> { }; switcher.hover(w, h, (self.cursor.0 as i32, self.cursor.1 as i32)); let selected = switcher.selected; - let atlas = &self.atlas; + let atlas = &self.gui.atlas; batch.sprite_region( atlas, "gui/container/gamemode_switcher", @@ -62,14 +62,14 @@ impl Game<'_> { for (index, mode) in MODES.into_iter().enumerate() { let (x, y) = slot_origin(w, h, index); batch.sprite( - &self.atlas, + &self.gui.atlas, "gui/sprites/gamemode_switcher/slot", x as f32, y as f32, ); if mode == selected { batch.sprite( - &self.atlas, + &self.gui.atlas, "gui/sprites/gamemode_switcher/selection", x as f32, y as f32, diff --git a/crates/horizon-client/src/game/golems.rs b/crates/horizon-client/src/game/golems.rs index 2bfeda70d..da204eee6 100644 --- a/crates/horizon-client/src/game/golems.rs +++ b/crates/horizon-client/src/game/golems.rs @@ -8,7 +8,7 @@ use horizon_client::Result; impl Game<'_> { fn golem_block(&self, p: [i32; 3]) -> &str { let state = self.world.block(p[0], p[1], p[2]); - &self.res.facts.get(state).name + &self.res.facts().get(state).name } fn is_golem_head(&self, p: [i32; 3]) -> bool { matches!( @@ -33,7 +33,7 @@ impl Game<'_> { for (ax, az) in [(1, 0), (0, 1)] { let side = |s: i32, row: i32| [head[0] + ax * s, head[1] - row, head[2] + az * s]; let arms = (-1..=1).all(|s| self.golem_block(side(s, 1)) == "minecraft:iron_block"); - let air = |p: [i32; 3]| self.res.facts.is_air(self.world.block(p[0], p[1], p[2])); + let air = |p: [i32; 3]| self.res.facts().is_air(self.world.block(p[0], p[1], p[2])); if arms && self.golem_block(side(0, 2)) == "minecraft:iron_block" && [-1, 1].iter().all(|&s| air(side(s, 0)) && air(side(s, 2))) @@ -58,7 +58,7 @@ impl Game<'_> { let below = [head[0], head[1] - 1, head[2]]; let name = self.golem_block(below); self.res - .items + .items() .block_tags .get("minecraft:copper") .is_some_and(|t| t.contains(name)) @@ -88,7 +88,7 @@ impl Game<'_> { /// `EntityType.create(level, TRIGGERED)` and `snapTo`: a built golem or /// wither has no spawn finalization and draws nothing from the level. pub(super) fn create_triggered(&mut self, kind: &str, at: [f64; 3], yaw: f32) -> Option { - let t = self.res.mob_types.get(kind).cloned()?; + let t = self.res.mob_types().get(kind).cloned()?; self.next_entity += 1; let mut mob = horizon_client::mobs::Mob::new( self.next_entity, @@ -181,9 +181,9 @@ impl Game<'_> { }); if let Some(chest) = chest { self.with_level(|b, level, ticks| b.set_block(level, ticks, pos, chest, 2)); - if let Some(entity) = - horizon_client::containers::BlockEntity::for_block(&self.res.facts.get(chest).name) - { + if let Some(entity) = horizon_client::containers::BlockEntity::for_block( + &self.res.facts().get(chest).name, + ) { self.world .block_entities .insert((below[0], below[1], below[2]), entity); diff --git a/crates/horizon-client/src/game/growth.rs b/crates/horizon-client/src/game/growth.rs index 18591b0fe..a6e062e24 100644 --- a/crates/horizon-client/src/game/growth.rs +++ b/crates/horizon-client/src/game/growth.rs @@ -10,7 +10,7 @@ impl Game<'_> { /// Load the configured/placed feature registry used by saplings, fungi /// and bone meal (once per process; shares the generator's bundle). pub(super) fn install_live_features(&self) { - if self.res.behaviours.live_features().is_some() { + if self.res.behaviours().live_features().is_some() { return; } let dir = self.options.resources.join("worldgen"); @@ -20,7 +20,7 @@ impl Game<'_> { .and_then(|data| LiveFeatures::new(&data).map_err(|e| e.to_string())); match features { Ok(Some(features)) => { - self.res.behaviours.set_live_features(Arc::new(features)); + self.res.behaviours().set_live_features(Arc::new(features)); log::info!("Horizon: live features ready in {:?}", started.elapsed()); } Ok(None) => { diff --git a/crates/horizon-client/src/game/hand.rs b/crates/horizon-client/src/game/hand.rs index a932913af..6e11febbd 100644 --- a/crates/horizon-client/src/game/hand.rs +++ b/crates/horizon-client/src/game/hand.rs @@ -39,7 +39,7 @@ impl Game<'_> { }); self.clicks.destroying = destroying; self.attack_clicks = 0; - self.stop_remote_mining(); + self.request_remote_mining_stop(); self.mining = None; } /// `LivingEntity.swing` with `getModifiedSwingDuration`. @@ -67,7 +67,7 @@ impl Game<'_> { /// attack clicks since the last tick and the held attack button. pub(super) fn tick_local_hand(&mut self) -> Result<()> { let attack_clicks = std::mem::take(&mut self.attack_clicks); - if self.screen == Screen::Playing && self.title.is_none() { + if self.screen == Screen::Playing && self.title.is_none() && self.sleeping.is_none() { let mut clicks = std::mem::take(&mut self.clicks); // keyAttack.isDown(); playing implies the grabbed mouse here // (scripts under --control hold buttons without grabbing it). @@ -90,7 +90,7 @@ impl Game<'_> { let (pos, first) = (d.pos, (d.ticks as u32 - 1).is_multiple_of(4)); let state = self.world.block(pos[0], pos[1], pos[2]); if first { - let sounds = self.res.facts.get(state).sounds.clone(); + let sounds = self.res.facts().get(state).sounds.clone(); self.audio.play( &sounds.hit, Some(pos.map(|v| f64::from(v) + 0.5)), @@ -117,17 +117,17 @@ impl Game<'_> { /// `BlockBehaviour.getDestroyProgress` with `Player.getDestroySpeed`. pub(super) fn destroy_progress(&self, pos: [i32; 3]) -> f32 { let state = self.world.block(pos[0], pos[1], pos[2]); - let facts = self.res.facts.get(state); + let facts = self.res.facts().get(state); if facts.hardness == -1.0 { return 0.0; } let held = self.held(); - let mut speed = self.res.items.mining_speed(held, &facts.name); + let mut speed = self.res.items().mining_speed(held, &facts.name); speed = self.modify_destroy_speed(speed); if let Some(eye) = self.eye_position() && self .res - .facts + .facts() .get(self.world.block( eye[0].floor() as i32, eye[1].floor() as i32, @@ -141,7 +141,7 @@ impl Game<'_> { if self.player.as_ref().is_some_and(|p| !p.ground.on_ground) { speed /= 5.0; } - let harvest = !facts.requires_tool || self.res.items.correct_for_drops(held, &facts.name); + let harvest = !facts.requires_tool || self.res.items().correct_for_drops(held, &facts.name); speed / facts.hardness / if harvest { 30.0 } else { 100.0 } } /// Whether an attack would hit an entity rather than the block behind @@ -287,7 +287,7 @@ impl Game<'_> { horizon_client::attack_strength::BASE_ATTACK_SPEED + self .held() - .and_then(|h| self.res.items.get(&h.item)) + .and_then(|h| self.res.items().get(&h.item)) .map_or(0.0, |i| f64::from(i.attack_speed)) } /// `evaluateWhichHandsToRender`: which of the main and off hand show. @@ -351,7 +351,7 @@ impl HandWorld for GameHands<'_, '_> { } fn is_air(&mut self, pos: [i32; 3]) -> bool { let state = self.game.world.block(pos[0], pos[1], pos[2]); - self.game.res.facts.get(state).name.ends_with("air") + self.game.res.facts().get(state).name.ends_with("air") } fn creative(&self) -> bool { self.game.creative diff --git a/crates/horizon-client/src/game/initial_spawn.rs b/crates/horizon-client/src/game/initial_spawn.rs index 5fcd90517..3f7a071bb 100644 --- a/crates/horizon-client/src/game/initial_spawn.rs +++ b/crates/horizon-client/src/game/initial_spawn.rs @@ -83,7 +83,7 @@ impl Game<'_> { horizon_client::flatgen::FlatTerrain::from_settings( &s, self.pack.assets.registry, - &self.res.biomes, + self.res.biomes(), ) }) { diff --git a/crates/horizon-client/src/game/integrated.rs b/crates/horizon-client/src/game/integrated.rs index 1ecdbff28..bfbc8d66c 100644 --- a/crates/horizon-client/src/game/integrated.rs +++ b/crates/horizon-client/src/game/integrated.rs @@ -8,11 +8,9 @@ //! //! Opt-in for now (`--server-thread` or `HORIZON_SERVER_THREAD=1`): the //! player joins as a network player, and the server side of a network player -//! does not do everything single player does yet. Worlds whose player is -//! outside the Overworld open the usual way, since network players stay in -//! the Overworld so far. +//! does not do everything single player does yet. use super::{ - Dim, Game, Options, PackResources, Resources, + Game, Options, PackResources, Resources, UiResources, lan::{LanSettings, Owner}, }; use horizon_client::{ @@ -20,7 +18,6 @@ use horizon_client::{ audio::Audio, hud::HudAtlas, lan::{Lan, MAX_PLAYERS, NetConfig, hub::HubConfig}, - save::Level, settings::Settings, title::WorldOptions, }; @@ -31,7 +28,7 @@ use std::{ path::Path, sync::{ Arc, - atomic::{AtomicBool, AtomicU16, AtomicU32, Ordering}, + atomic::{AtomicBool, AtomicI32, AtomicU16, AtomicU32, Ordering}, mpsc::{Receiver, Sender, channel, sync_channel}, }, thread::JoinHandle, @@ -48,6 +45,10 @@ struct Flags { /// while it is not. published: AtomicU16, smoothed_tick_ms: AtomicU32, + /// `options.renderDistance` and `options.simulationDistance`, which the + /// server follows while it runs (IntegratedServer.tickServer). + render_distance: AtomicI32, + simulation_distance: AtomicI32, } /// Work the window hands its server thread (`MinecraftServer.execute`). @@ -69,6 +70,13 @@ impl Integrated { pub fn set_paused(&self, paused: bool) { self.flags.paused.store(paused, Ordering::Relaxed); } + /// The window's render and simulation distances, for the server to follow. + pub fn set_distances(&self, render: i32, simulation: i32) { + self.flags.render_distance.store(render, Ordering::Relaxed); + self.flags + .simulation_distance + .store(simulation, Ordering::Relaxed); + } /// `IntegratedServer.getPort` while `isPublished`. pub fn published(&self) -> Option { Some(self.flags.published.load(Ordering::Relaxed)).filter(|&p| p != 0) @@ -137,12 +145,9 @@ struct Launch { player: String, } -/// Whether this world opens on a server thread: asked for, and its player -/// (if any yet) in the Overworld. -pub(super) fn wanted(options: &Options, world: &Path) -> bool { - options.server_thread - && options.dedicated.is_none() - && Level::read(world).is_none_or(|l| Dim::from_id(&l.dimension) == Dim::Overworld) +/// Whether this world opens on a server thread: asked for. +pub(super) fn wanted(options: &Options, _world: &Path) -> bool { + options.server_thread && options.dedicated.is_none() } impl Game<'_> { @@ -196,14 +201,23 @@ impl Game<'_> { options.server = None; options.world = None; options.dev_mods.clear(); + self.join_world_rest()?; let (res, pack, settings) = (self.res.clone(), self.pack.clone(), self.settings.clone()); let flags = Arc::new(Flags::default()); + flags + .render_distance + .store(settings.render_distance, Ordering::Relaxed); + flags + .simulation_distance + .store(settings.simulation_distance, Ordering::Relaxed); let (ready_tx, ready) = channel(); let (tasks, server_tasks) = channel(); let server_flags = flags.clone(); let thread = std::thread::Builder::new() .name("Server thread".into()) .spawn(move || { + // MinecraftServer.spin's Thread.setPriority(8). + horizon_threads::favour_current(); let result = serve( &res, pack, @@ -255,7 +269,9 @@ impl Game<'_> { launch.create.as_ref(), )?; let saved = Box::new(self.level.clone()); - let view_distance = self.settings.render_distance.clamp(2, 32); + // Math.max(2, options.renderDistance): past vanilla's 32 when the + // window's render distance is (Horizon's options go to 64). + let view_distance = self.settings.render_distance.max(2); let config = HubConfig { default_game_mode: self.level.mode(), // Only the owner until it is opened to the LAN. @@ -263,7 +279,7 @@ impl Game<'_> { force_game_mode: false, hardcore: self.level.hardcore, view_distance, - simulation_distance: self.settings.simulation_distance.clamp(2, 32), + simulation_distance: self.settings.simulation_distance.max(2), dimension: self.dim.id().to_owned(), seed: self.level.seed, max_players: MAX_PLAYERS, @@ -303,6 +319,7 @@ impl Game<'_> { // The owner is an operator when the world allows commands // (`IntegratedPlayerList`, `MinecraftServer.getProfilePermissions`). host.hub.ops.insert(owner, if commands { 4 } else { 0 }); + host.hub.singleplayer_owner = Some(owner); host.owner = Some(Owner { uuid: owner, saved: Some(saved), @@ -320,14 +337,13 @@ impl Game<'_> { let mut next = Instant::now(); while !flags.stop.load(Ordering::Relaxed) { self.run_tasks(); - self.update_dedicated_streaming(); - self.update_world(); if flags.paused.load(Ordering::Relaxed) { // Only the connections run. - self.tick_lan(); + self.tick_server_timed(true)?; } else { + self.follow_distances(&flags); let started = Instant::now(); - self.tick()?; + self.tick_server_timed(false)?; self.debug_charts.log_tick(started, started.elapsed()); flags.smoothed_tick_ms.store( self.debug_charts.smoothed_tick_ms.to_bits(), @@ -397,6 +413,60 @@ impl Game<'_> { pub(super) fn serving(&self) -> bool { self.serving.is_some() } + /// The window's agent runs a command as single player's console does + /// (`as_console`, as without a server thread): as the world's owner, in + /// the owner's level, at the owner permission level. The feedback goes to + /// the owner's chat and is returned. + pub(super) fn owner_console_command(&mut self, command: &str) -> Vec { + let Some(owner) = self.serving.as_ref().map(|s| s.owner) else { + return Vec::new(); + }; + let Some(mut host) = self.lan.take() else { + return Vec::new(); + }; + let mut out = Vec::new(); + if let Some(g) = host.hub.guests.iter().find(|g| g.uuid == owner && g.placed) { + let conn = g.conn; + // Commands.performCommand runs in the player's level. + let (back, theirs) = (self.dim, super::Dim::from_id(&g.dimension)); + let entered = theirs == back || self.enter_level(Some(&mut host), theirs).is_ok(); + if entered { + let command = command.trim_start_matches('/').to_owned(); + let lines = self.as_guest(&mut host, conn, |g| { + g.as_console(|g| { + g.chat_capture = Some(Vec::new()); + g.run_command(&command); + g.chat_capture.take().unwrap_or_default() + }) + }); + // A teleport into another level happens before its feedback is sent. + self.run_level_teleports(&mut host); + for line in lines.unwrap_or_default() { + out.push(line.plain()); + host.hub.send_to( + conn, + &horizon_protocol::packets::play::Clientbound::SystemChat( + horizon_protocol::packets::play::SystemChat { + content: horizon_client::lan::data::component_nbt( + &line.component(), + ), + overlay: false, + }, + ), + ); + } + self.send_admin_feedback(&mut host, Some(conn)); + } + if theirs != back + && let Err(e) = self.enter_level(Some(&mut host), back) + { + log::error!("could not reopen {}: {e}", back.id()); + } + } + self.lan = Some(host); + out + } + /// The window's requests, between two ticks. fn run_tasks(&mut self) { let tasks: Vec = match &self.serving { @@ -446,6 +516,28 @@ impl Game<'_> { } /// Publish or unpublish for real, once the hub is in its place (a /// player's `/publish` runs while it is out). + /// IntegratedServer.tickServer: the server follows the window's render + /// and simulation distances when they change. + fn follow_distances(&mut self, flags: &Flags) { + let Some(host) = self.lan.as_mut() else { + return; + }; + let view = flags.render_distance.load(Ordering::Relaxed).max(2); + let old = host.hub.config.view_distance; + if view != old { + log::info!("Changing view distance to {view}, from {old}"); + host.hub.set_view_distance(view); + self.settings.render_distance = view; + } + let simulation = flags.simulation_distance.load(Ordering::Relaxed).max(2); + let old = host.hub.config.simulation_distance; + if simulation != old { + log::info!("Changing simulation distance to {simulation}, from {old}"); + host.hub.set_simulation_distance(simulation); + self.settings.simulation_distance = simulation; + } + } + fn apply_publish(&mut self) { if self.lan.is_none() { return; @@ -560,7 +652,14 @@ fn serve( ) -> Result<()> { // It draws nothing: only the font, which books and chat measure with. let atlas = HudAtlas::fonts(&options.resources)?; + let gui = UiResources { + atlas, + panorama: Arc::new(horizon_client::gfx::panorama::Faces { + size: 1, + pixels: [0u8; 4].repeat(6), + }), + }; let audio = Audio::silent(&options.resources); - let mut server = Game::new(res, pack, atlas, options, settings, audio)?; + let mut server = Game::new(res, pack, gui, options, settings, audio)?; server.run_integrated(launch, flags, tasks, ready) } diff --git a/crates/horizon-client/src/game/item_draw.rs b/crates/horizon-client/src/game/item_draw.rs index aae200b50..8e7176a70 100644 --- a/crates/horizon-client/src/game/item_draw.rs +++ b/crates/horizon-client/src/game/item_draw.rs @@ -176,7 +176,7 @@ impl Game<'_> { } pub(super) fn item_host(&self) -> Host<'_> { Host { - banners: &self.res.stations.banners, + banners: &self.res.stations().banners, } } /// The client level as item models read it. @@ -213,7 +213,7 @@ impl Game<'_> { owner.is_local_player = true; owner.is_view_entity = true; if let (Some((slot, ticks)), Some(stack)) = (self.using, stack) - && self.inventory.get(slot).and_then(Option::as_ref) == Some(stack) + && self.stack_in(slot) == Some(stack) { let view = item_render::stack_view(stack); let duration = use_duration(&self.pack.item_renderer.models.components(&view)); @@ -255,7 +255,7 @@ impl Game<'_> { item_render::gui::draw( &state, &self.pack.item_renderer, - &self.atlas.sprites, + &self.gui.atlas.sprites, &self.item_host(), batch, x, @@ -279,7 +279,7 @@ impl Game<'_> { }; state.submit( &self.pack.item_renderer, - &self.atlas.sprites, + &self.gui.atlas.sprites, &self.item_host(), pose, &mut sink, @@ -300,7 +300,7 @@ impl Game<'_> { }; state.submit( &self.pack.item_renderer, - &self.atlas.sprites, + &self.gui.atlas.sprites, &self.item_host(), pose, &mut sink, diff --git a/crates/horizon-client/src/game/jukebox.rs b/crates/horizon-client/src/game/jukebox.rs index 235ad7c24..c3ed75ebb 100644 --- a/crates/horizon-client/src/game/jukebox.rs +++ b/crates/horizon-client/src/game/jukebox.rs @@ -51,7 +51,7 @@ impl Game<'_> { pub(super) fn use_jukebox(&mut self, pos: [i32; 3], held: Option<&ItemStack>) -> bool { if &*self .res - .facts + .facts() .get(self.world.block(pos[0], pos[1], pos[2])) .name != "minecraft:jukebox" @@ -65,7 +65,7 @@ impl Game<'_> { let Some(stack) = held else { return false }; let Some(song) = self .res - .items + .items() .get(&stack.item) .and_then(|i| i.jukebox_song.clone()) else { @@ -95,7 +95,7 @@ impl Game<'_> { /// Level event 1010: the song from the jukebox, and the "Now Playing" overlay. fn start_song(&mut self, pos: [i32; 3], song: &str) { self.stop_song(pos); - let Some(s) = self.res.items.songs.get(song).cloned() else { + let Some(s) = self.res.items().songs.get(song).cloned() else { return; }; // LevelRenderer.playJukeboxSong: SimpleSoundInstance.forJukeboxSong. @@ -140,63 +140,53 @@ impl Game<'_> { pub(super) fn jukebox_removed(&mut self, pos: [i32; 3]) { self.stop_song(pos); } - /// `JukeboxSongPlayer.tick` for every playing jukebox. - pub(super) fn tick_jukeboxes(&mut self) { - let playing: Vec<((i32, i32, i32), String, i64)> = self - .world - .block_entities - .iter() - .filter_map(|(k, e)| match e { - BlockEntity::Jukebox(j) if j.playing => { - let song = self - .res - .items - .get(&j.record.as_ref()?.item)? - .jukebox_song - .clone()?; - Some((*k, song, j.ticks)) - } - _ => None, - }) - .collect(); - for (key, song, ticks) in playing { - let pos = [key.0, key.1, key.2]; - let Some(s) = self.res.items.songs.get(&song).cloned() else { - continue; - }; - if s.finished(ticks) { - if let Some(BlockEntity::Jukebox(j)) = self.world.block_entities.get_mut(&key) { - j.playing = false; - j.ticks = 0; - } - self.stop_song(pos); - self.jukebox_changed(pos); - continue; - } - if !self.jukebox_sounds.contains_key(&key) && ticks == 0 { - self.start_song(pos, &song); - } - if ticks % 20 == 0 { - let color = f64::from(self.world.random.next_int_bound(4).unwrap_or(0)) / 24.0; - let at = [ - f64::from(pos[0]) + 0.5, - f64::from(pos[1]) + 1.2, - f64::from(pos[2]) + 0.5, - ]; - self.particles.add( - &ParticleOptions::simple(ParticleType::Note), - at, - [color, 0.0, 0.0], - ); - } + /// `JukeboxSongPlayer.tick` of the jukebox at `key`, while it plays. + pub(super) fn tick_jukebox(&mut self, key: (i32, i32, i32)) { + let playing = match self.world.block_entities.get(&key) { + Some(BlockEntity::Jukebox(j)) if j.playing => j.record.as_ref().and_then(|r| { + let song = self.res.items().get(&r.item)?.jukebox_song.clone()?; + Some((song, j.ticks)) + }), + _ => None, + }; + let Some((song, ticks)) = playing else { + return; + }; + let pos = [key.0, key.1, key.2]; + let Some(s) = self.res.items().songs.get(&song).cloned() else { + return; + }; + if s.finished(ticks) { if let Some(BlockEntity::Jukebox(j)) = self.world.block_entities.get_mut(&key) { - j.ticks += 1; + j.playing = false; + j.ticks = 0; } + self.stop_song(pos); + self.jukebox_changed(pos); + return; + } + if !self.jukebox_sounds.contains_key(&key) && ticks == 0 { + self.start_song(pos, &song); + } + if ticks % 20 == 0 { + let color = f64::from(self.world.random.next_int_bound(4).unwrap_or(0)) / 24.0; + let at = [ + f64::from(pos[0]) + 0.5, + f64::from(pos[1]) + 1.2, + f64::from(pos[2]) + 0.5, + ]; + self.particles.add( + &ParticleOptions::simple(ParticleType::Note), + at, + [color, 0.0, 0.0], + ); + } + if let Some(BlockEntity::Jukebox(j)) = self.world.block_entities.get_mut(&key) { + j.ticks += 1; } - self.update_song_volumes(); } /// Let go of songs that ended or whose jukebox stopped. - fn update_song_volumes(&mut self) { + pub(super) fn update_song_volumes(&mut self) { let sounds: Vec<_> = self .jukebox_sounds .iter() diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index f6fc1e860..57c44cb07 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -2,7 +2,7 @@ //! server threads and the play-state hub, feeds it the world's entities each //! tick and turns what the guests do into gameplay. A world that is not open //! costs one `Option` check per tick. -use super::Game; +use super::{Game, shield::OFFHAND_SLOT}; use horizon_client::{ Result, items::ItemStack, @@ -42,6 +42,8 @@ pub(super) struct LanHost { pub hub: Hub, pub settings: LanSettings, pub host_entity: i32, + /// The host's swings the guests were told of (`Game::swings_started`). + pub host_swings: u32, pub host_uuid: u128, /// The host's profile name (the account's, or the offline name). pub host_name: String, @@ -52,11 +54,18 @@ pub(super) struct LanHost { /// Blocks broken this tick (their sound is a level event for the guests). pub broken: Vec<[f64; 3]>, /// Hurt and death state of the mobs guests see. - mob_state: HashMap, + mob_state: HashMap>, /// The guest each mob sees as its player, when not the host (game/lan_mobs.rs). pub mob_players: HashMap, /// An integrated server's owner (game/integrated.rs). pub owner: Option, + /// The players' locator bars (game/lan_waypoints.rs), and the guests who + /// are dead, whose respawn replaces their waypoint. + pub waypoints: lan::waypoints::Waypoints, + pub waypoint_dead: std::collections::HashSet, + /// Guests' commands waiting for the hub to be back in `self.lan`, where + /// a command can reach every player (game/lan_actions.rs). + pub commands: Vec, } /// The player who owns an integrated server's world: a guest whose state is @@ -69,7 +78,7 @@ pub(super) struct Owner { } /// A stable UUID for an entity that has none. -fn entity_uuid(id: u32) -> u128 { +pub(super) fn entity_uuid(id: u32) -> u128 { 0x4856_5a4e_0000_0000_0000_0000_0000_0000u128 | u128::from(id) } @@ -140,11 +149,15 @@ fn mob_meta(m: &Mob, hub: &Hub) -> Vec<(u8, i32, Value)> { meta.push((3, serializer::BOOLEAN, Value::Bool(true))); } } - if m.health < m.kind.max_health { - meta.push((9, serializer::FLOAT, Value::Float(m.health.max(0.0)))); + // LivingEntity.DATA_HEALTH_ID: defined as 1, then set to the max health. + let health = m.health.max(0.0); + if health != 1.0 { + meta.push((9, serializer::FLOAT, Value::Float(health))); } - if kind == "minecraft:rabbit" && m.no_ai { - meta.push((15, serializer::BYTE, Value::Byte(1))); + // Mob.DATA_MOB_FLAGS_ID: no AI 1, left-handed 2. + let flags = i8::from(m.no_ai) | i8::from(m.extra.left_handed) << 1; + if flags != 0 { + meta.push((15, serializer::BYTE, Value::Byte(flags))); } if m.is_baby() && let Some(index) = baby_index(kind) @@ -258,7 +271,7 @@ fn mob_meta(m: &Mob, hub: &Hub) -> Vec<(u8, i32, Value)> { meta.push(( 16, serializer::OPTIONAL_BLOCK_STATE, - Value::Id(i32::from(state.0 as u16)), + Value::Id(state.0 as i32), )); } } @@ -359,6 +372,12 @@ impl Game<'_> { "open": true, "host": h.host_name, "port": h.settings.port, + // The server's own world: its finished chunks and how many it + // has generated or loaded so far. + "world": { + "chunks": self.world.chunks.len(), + "generated": self.streaming.generated_total, + }, "guests": h.hub.guests.iter().map(|g| json!({ "name": g.name, "position": g.position, @@ -366,6 +385,7 @@ impl Game<'_> { "loaded": g.loaded, "game_mode": g.game_mode, "sneaking": g.sneaking, + "sleeping": g.bed, "health": g.health, // FoodData: food level, saturation and exhaustion. "food": [ @@ -380,6 +400,10 @@ impl Game<'_> { [&m.extra.head, &m.extra.chest, &m.extra.legs, &m.extra.feet] .map(|s| s.as_deref()) }), + // The host draws this guest's arm swinging. + "model_swinging": h.models.get(&g.conn).map(|m| m.ai.swinging), + // For how many ticks the host draws this guest using an item. + "model_using": h.models.get(&g.conn).map(|m| m.extra.using_item), })).collect::>(), }), _ => json!({ "open": false }), @@ -452,7 +476,7 @@ impl Game<'_> { .clone()?; let biome_names: Vec = self .res - .biomes + .biomes() .list .iter() .map(|b| b.name.clone()) @@ -465,7 +489,7 @@ impl Game<'_> { .unwrap_or_default(); let mut encoder = ChunkEncoder::new( self.res - .facts + .facts() .states .iter() .map(|s| (s.name.clone(), s.fluid_level > 0 || s.waterlogged)), @@ -489,10 +513,12 @@ impl Game<'_> { let hub = Hub::new(lan, sync, encoder, config); let port = hub.lan.port; self.world.block_log = Some(Vec::new()); + self.world.light_log = Some(Default::default()); self.lan = Some(LanHost { hub, settings: LanSettings { port, ..settings }, host_entity, + host_swings: self.swings_started, host_uuid, host_name: self.options.player_name(), models: HashMap::new(), @@ -502,6 +528,9 @@ impl Game<'_> { mob_state: HashMap::new(), mob_players: HashMap::new(), owner: None, + waypoints: Default::default(), + waypoint_dead: Default::default(), + commands: Vec::new(), }); self.audio.capture = Some(Vec::new()); Ok(()) @@ -536,6 +565,7 @@ impl Game<'_> { // Give the writer threads a moment to flush the disconnect packets. std::thread::sleep(std::time::Duration::from_millis(100)); self.world.block_log = None; + self.world.light_log = None; self.audio.capture = None; self.lan_events.clear(); self.streaming.extra.clear(); @@ -557,6 +587,75 @@ impl Game<'_> { } else { playerdata::save(guest, &self.world_root); } + self.save_guest_records(host, guest); + } + /// The file name of a player's advancements and statistics: an + /// integrated server's owner keeps single player's. + fn records_uuid(&self, host: &LanHost, uuid: u128) -> Option<[i32; 4]> { + if host.owner.as_ref().is_some_and(|o| o.uuid == uuid) { + self.level.player_uuid + } else { + Some(playerdata::uuid_ints(uuid)) + } + } + /// `PlayerAdvancements.save` and `ServerStatsCounter.save` of a network player. + pub(super) fn save_guest_records( + &self, + host: &LanHost, + guest: &horizon_client::lan::guest::Guest, + ) { + if !guest.placed || self.adv.tree.list.is_empty() { + return; + } + let Some(uuid) = self.records_uuid(host, guest.uuid) else { + return; + }; + let path = super::stats::player_json(&self.world_root, "advancements", uuid); + let json = guest + .advancements + .to_json(&self.adv.tree, horizon_world::DATA_VERSION); + if let Some(dir) = path.parent() { + let _ = std::fs::create_dir_all(dir); + } + if let Ok(text) = serde_json::to_string_pretty(&json) { + let _ = std::fs::write(path, text); + } + if let Some(play) = host.play.get(&guest.conn) { + play.stats + .write(&super::stats::player_json(&self.world_root, "stats", uuid)); + } + } + /// `PlayerAdvancements.load` and `ServerStatsCounter.parseLocal` of a + /// network player who joined; every statistic goes out on the first + /// request (`PlayerList.placeNewPlayer`'s `markAllDirty`). + fn load_guest_records(&mut self, host: &mut LanHost, conn: u32) { + let Some(uuid) = host + .hub + .guest(conn) + .and_then(|g| self.records_uuid(host, g.uuid)) + else { + return; + }; + let tree = self.advancement_tree(); + let path = super::stats::player_json(&self.world_root, "advancements", uuid); + let saved = std::fs::read_to_string(path) + .ok() + .and_then(|t| serde_json::from_str::(&t).ok()); + if let Some(g) = host.hub.guest_mut(conn) + && let Some(json) = saved + { + let mut progress = horizon_client::advancements::Progress::from_json(&json); + // PlayerAdvancements.applyFrom: advancements this server lacks are dropped. + progress + .criteria + .retain(|id, _| tree.by_id.contains_key(id)); + g.advancements = progress; + g.advancements_loaded = true; + } + let mut stats = + super::stats::Stats::read(&super::stats::player_json(&self.world_root, "stats", uuid)); + stats.mark_all_dirty(); + host.play.entry(conn).or_default().stats = stats; } /// The integrated server's owner into the world's player, for `Level::write`. pub(super) fn store_owner(&mut self) { @@ -568,6 +667,7 @@ impl Game<'_> { }; if let Some(g) = host.hub.guests.iter().find(|g| g.uuid == uuid && g.placed) { playerdata::store(g, &mut self.level); + self.save_guest_records(host, g); } } @@ -633,16 +733,19 @@ impl Game<'_> { sprinting: p.sprinting, equipment, health: p.health.health(), + attributes: horizon_client::lan::attributes::syncable(&p.attributes), + living_flags: living_flags(self.using), }) } - /// Mobs and items as the entities guests track. - fn lan_entities(&self, hub: &mut Hub) -> Vec { - let mut out = Vec::with_capacity(self.mobs.len() + self.dropped.len()); + /// The entities guests track: mobs, items and the rest (game/lan_entities.rs). + fn lan_entities(&self, host_entity: Option, hub: &mut Hub) -> Vec { + let mut out = Vec::with_capacity(self.mobs.len() + self.dropped.len() + self.orbs.len()); for m in self.mobs.iter().filter(|m| !m.removed) { let kind = m.kind.id.as_str(); let meta = mob_meta(m, hub); let equipment = mob_equipment(m, hub); + let attributes = self.mob_attributes(m); let (yaw, pitch) = (m.yaw, m.head_pitch); out.push(EntityState { id: m.id as i32, @@ -657,6 +760,8 @@ impl Game<'_> { data: 0, meta, equipment, + attributes, + precise: false, }); } for d in &self.dropped { @@ -674,8 +779,30 @@ impl Game<'_> { data: 0, meta: vec![(8, serializer::ITEM_STACK, Value::Item(stack))], equipment: Vec::new(), + attributes: Vec::new(), + precise: false, }); } + for o in &self.orbs { + out.push(EntityState { + id: o.id as i32, + uuid: entity_uuid(o.id), + kind: "minecraft:experience_orb".to_owned(), + position: [o.position.x, o.position.y, o.position.z], + yaw: 0.0, + pitch: 0.0, + head_yaw: 0.0, + on_ground: o.on_ground, + velocity: [o.velocity.x, o.velocity.y, o.velocity.z], + data: 0, + // ExperienceOrb.DATA_VALUE. + meta: vec![(8, serializer::INT, Value::Int(o.value as i32))], + equipment: Vec::new(), + attributes: Vec::new(), + precise: false, + }); + } + self.lan_object_states(host_entity, hub, &mut out); out } @@ -686,17 +813,29 @@ impl Game<'_> { }; // What the tick sends a guest goes to its connection at the end. let holding = lan::Holding::start(); + // The host's own commands' (and command blocks') players moving + // to another level, and their broadcastToAdmins lines. + if !self.level_teleports.is_empty() { + self.run_level_teleports(&mut host); + } + if !self.admin_feedback.is_empty() { + self.send_admin_feedback(&mut host, None); + } let state = self.lan_host_state(&host); // A dedicated server runs the hub without a host player. if state.is_some() || host.hub.config.dedicated { let has_guests = !host.hub.guests.is_empty(); let entities = if has_guests { - self.lan_entities(&mut host.hub) + let host_entity = state.as_ref().map(|s| s.entity_id); + self.lan_assign_ids(); + self.lan_riding(host_entity, &mut host.hub.riding); + self.lan_entities(host_entity, &mut host.hub) } else { Vec::new() }; let spawn = self.spawn.map_or([0.5, 80.0, 0.5], |s| [s.x, s.y, s.z]); let events = { + let clock_rate = self.network_clock_rate(); let mut ctx = TickCtx { world: &mut self.world, host: state.as_ref(), @@ -705,15 +844,28 @@ impl Game<'_> { spawn, difficulty: i32::from(self.ui.difficulty), difficulty_locked: self.level.difficulty_locked, - clock_rate: if self.ui.rules.bool("advance_time") { - self.ui.clock.rate - } else { - 0.0 - }, + clock_rate, tick_rate: self.clock.rate(), tick_frozen: self.clock.frozen(), + collision: Some(self.collision), }; - host.hub.tick(&mut ctx) + host.hub.movement_checks = ( + self.ui.rules.bool("player_movement_check"), + self.ui.rules.bool("elytra_movement_check"), + ); + // A server with several levels runs a pass for each + // (`tick_server`): the network is read once, before the first. + let pass = self.level_pass; + let mut events = if pass.first { + host.hub.poll(&mut ctx) + } else { + Vec::new() + }; + events.extend(host.hub.tick_level(&mut ctx)); + if pass.last { + host.hub.end_tick(); + } + events }; if let Some(state) = &state && host.host_mode != state.game_mode @@ -723,12 +875,16 @@ impl Game<'_> { } for event in events { match event { + event @ (HostEvent::Chat { .. } | HostEvent::Warn(_) | HostEvent::Error(_)) => { + self.lan_log_event(event); + } HostEvent::Joined(conn) => { self.lan_guest_joined(&mut host, conn, state.as_ref(), &entities, spawn); if self.options.dedicated.is_some() && let Some(g) = host.hub.guest(conn) { // PlayerList.placeNewPlayer, then the broadcast join message. + super::dedicated::player_joined(g.uuid, &g.name); let [x, y, z] = g.position; super::dedicated::log( "INFO", @@ -753,11 +909,20 @@ impl Game<'_> { if let Some(pos) = guest.menu.as_ref().and_then(|m| m.pos) && !guest.spectator() { - self.container_openers_changed(pos, false); + // In the level they were in. + let back = self.dim; + let theirs = super::Dim::from_id(&guest.dimension); + if self.enter_level(Some(&mut host), theirs).is_ok() { + self.container_openers_changed(pos, false); + } + if let Err(e) = self.enter_level(Some(&mut host), back) { + log::error!("could not reopen {}: {e}", back.id()); + } } self.lan_crafting_left(&mut host, &mut guest); self.save_guest(&host, &guest); host.models.remove(&guest.conn); + host.play.remove(&guest.conn); if self.options.dedicated.is_some() && guest.placed { super::dedicated::log( "INFO", @@ -769,26 +934,27 @@ impl Game<'_> { ); } } - // MinecraftServer.logChatMessage of an unsigned message. - HostEvent::Chat(line) if self.options.dedicated.is_some() => { - super::dedicated::log("INFO", &format!("[Not Secure] {line}")); - } - HostEvent::Chat(line) => self.system_message(Text::literal(line)), HostEvent::Packet(conn, packet) => { self.lan_guest_packet(&mut host, conn, *packet); + if !host.commands.is_empty() { + self.lan = Some(host); + self.run_guest_commands(); + host = self.lan.take().expect("the hub is back after the commands"); + } } } } } self.lan_flush_effects(&mut host); self.tick_guests(&mut host); + self.lan_living_flags(&mut host); + self.tick_server_travel(&mut host); self.lan_swings(&mut host); self.lan_update_streaming(&host); self.lan_update_models(&mut host); + self.lan_update_waypoints(&mut host, state.as_ref()); drop(holding); - for g in &host.hub.guests { - g.sender.flush(); - } + lan::flush_all(host.hub.guests.iter().map(|g| &g.sender)); self.lan = Some(host); } @@ -803,6 +969,7 @@ impl Game<'_> { let default_mode = host.hub.config.default_game_mode; let force = host.hub.config.force_game_mode; let root = self.world_root.clone(); + let in_place = host.hub.config.dimension.clone(); let owner = host.owner.as_mut(); let Some(g) = host.hub.guest_mut(conn) else { return; @@ -825,7 +992,30 @@ impl Game<'_> { g.previous_position = spawn; g.health = 20.0; g.food = 20; + g.dimension = super::Dim::Overworld.id().to_owned(); } + // A server places each player in their own level + // (`PlayerList.placeNewPlayer`); the in-frame game has one. + let back = self.dim; + let level = super::Dim::from_id(&g.dimension); + let elsewhere = self.options.dedicated.is_some() && level != back; + if !elsewhere { + g.dimension = in_place; + } + self.load_guest_records(host, conn); + let theirs; + let entities = if elsewhere { + if let Err(e) = self.enter_level(Some(host), level) { + log::error!("could not open {}: {e}", level.id()); + return; + } + // A dedicated server has no host player. + theirs = self.lan_entities(None, &mut host.hub); + &theirs[..] + } else { + entities + }; + let clock_rate = self.network_clock_rate(); let mut ctx = TickCtx { world: &mut self.world, host: state, @@ -834,11 +1024,15 @@ impl Game<'_> { spawn, difficulty: i32::from(self.ui.difficulty), difficulty_locked: self.level.difficulty_locked, - clock_rate: self.ui.clock.rate, + clock_rate, tick_rate: self.clock.rate(), tick_frozen: self.clock.frozen(), + collision: Some(self.collision), }; host.hub.place_new_player(conn, &mut ctx); + if elsewhere && let Err(e) = self.enter_level(Some(host), back) { + log::error!("could not reopen {}: {e}", back.id()); + } } /// Level events the game queued, for the guests (`process_level_events`). @@ -859,7 +1053,7 @@ impl Game<'_> { /// `Level.blockEvent` for the guests (`ClientboundBlockEventPacket`). pub(super) fn lan_block_event(&mut self, pos: [i32; 3], b0: i32, b1: i32) { if self.audio.capture.is_some() { - let state = self.world.block(pos[0], pos[1], pos[2]).0 as u16; + let state = self.world.block(pos[0], pos[1], pos[2]).0; self.lan_block_events.push((pos, b0, b1, state)); } } @@ -893,35 +1087,67 @@ impl Game<'_> { let mut seen = HashMap::new(); for m in &self.mobs { let dead = !m.alive(); - let old = host.mob_state.get(&m.id).copied(); + let old = host + .mob_state + .get(&self.dim) + .and_then(|s| s.get(&m.id)) + .copied(); let at = [m.position.x, m.position.y, m.position.z]; - if m.hurt_time > old.map_or(0, |o| o.0) && !dead { - host.hub.entity_event(m.id as i32, 2, at); + // LivingEntity.hurtServer: a hurt that restarted the hurt time. + if m.hurt_time > old.map_or(0, |o| o.0) + && !dead + && let Some((source, _)) = m.last_damage + { + use horizon_client::mobs::{DamageKind as K, Who}; + let player = source.attacker == Some(Who::Player); + let kind = super::creatures::damage_type(source.kind, player); + // The player a mob sees is the guest acting, else the host. + let cause = source.attacker.map(|who| match who { + Who::Player => host + .mob_players + .get(&m.id) + .and_then(|conn| host.hub.guest(*conn)) + .map_or(host.host_entity, |g| g.entity_id), + Who::Mob(id) => id as i32, + }); + // A projectile is the direct entity of its hit: not tracked here. + let melee = matches!( + source.kind, + K::PlayerAttack | K::MaceSmash | K::MobAttack | K::Sting | K::SonicBoom + ); + let direct = cause.filter(|_| melee); + host.hub.damage_event(m.id as i32, kind, cause, direct, at); } if dead && !old.is_some_and(|o| o.1) { host.hub.entity_event(m.id as i32, 3, at); } seen.insert(m.id, (m.hurt_time, dead)); } - host.mob_state = seen; + host.mob_state.insert(self.dim, seen); } /// Keep the chunks around every guest loaded. fn lan_update_streaming(&mut self, host: &LanHost) { + // DistanceManager.PlayerTicketTracker: every chunk within the server's + // view distance (Chebyshev) of a player holds a PLAYER_LOADING ticket + // at the entity-ticking level (31), whatever the player asked for; + // levels rise by one a chunk, so FULL (33) reaches two chunks past + // it. An extra area finishes its chunks out to its radius + 1. That + // covers what the player tracks (`ChunkTrackingView`, within its view + // distance + 1) and those chunks' lit neighbours. + let radius = host.hub.config.view_distance + 1; self.streaming.extra = host .hub .guests .iter() - .filter(|g| g.placed) - // The tracked square's corners and their lit neighbours lie - // within (view distance + 2) * √2 of the player's chunk. - .map(|g| (g.chunk(), (g.view_distance + 2) * 3 / 2 + 1)) + .filter(|g| host.hub.here(g)) + .map(|g| (g.chunk(), radius)) .collect(); } /// The guests' models, for the host to see them. fn lan_update_models(&mut self, host: &mut LanHost) { - let Some(kind) = self.res.player_type.clone() else { + let Some(kind) = self.res.player_type().clone() else { return; }; host.models @@ -979,6 +1205,14 @@ impl Game<'_> { .each_ref() .map(|s| s.as_ref().map(|s| s.item.clone())); m.extra.crouching = g.sneaking; + // LivingEntity.updateSwingTime: the arm swing lan_swings started. + m.anim.attack_anim_o = m.anim.attack_anim; + horizon_client::mobs::ai::tick_swing(m); + // LivingEntity.onSyncedDataUpdated(DATA_LIVING_ENTITY_FLAGS): the + // item in use and its hand (the arm poses of a bow, a shield...). + let using = host.play.get(&g.conn).and_then(|p| p.using()); + m.extra.using_item = using.map_or(0, |(_, ticks)| ticks as i32 + 1); + m.extra.using_off_hand = using.is_some_and(|(slot, _)| slot == OFFHAND_SLOT); } } @@ -992,7 +1226,7 @@ impl Game<'_> { let Some(host) = &self.lan else { return; }; - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let lookup = |name: &str| atlas.sprites.get(name).map(|r| [r.u0, r.v0, r.u1, r.v1]); for m in host.models.values() { let entity_start = out.len(); @@ -1008,7 +1242,7 @@ impl Game<'_> { camera, &lookup, packed, - &self.res.mob_data, + self.res.mob_data(), &self.worn_armor_layers(m), out, ); @@ -1026,3 +1260,30 @@ impl Game<'_> { } } } + +impl Game<'_> { + /// A hub's chat and log lines: a dedicated server logs them + /// (MinecraftServer.logChatMessage, `[Not Secure]` for unsigned or + /// expired messages); a LAN host shows the chat. + pub(super) fn lan_log_event(&mut self, event: HostEvent) { + let dedicated = self.options.dedicated.is_some(); + match event { + HostEvent::Chat { line, secure } if dedicated => { + let tag = if secure { "" } else { "[Not Secure] " }; + super::dedicated::log("INFO", &format!("{tag}{line}")); + } + HostEvent::Chat { line, .. } => self.system_message(Text::literal(line)), + HostEvent::Warn(line) if dedicated => super::dedicated::log("WARN", &line), + HostEvent::Warn(line) => log::warn!("{line}"), + HostEvent::Error(line) if dedicated => super::dedicated::log("ERROR", &line), + HostEvent::Error(line) => log::error!("{line}"), + _ => {} + } + } +} + +/// `LivingEntity.DATA_LIVING_ENTITY_FLAGS` for an item in use: 1, and 2 when +/// it is the off hand's. +pub(super) fn living_flags(using: Option<(usize, u32)>) -> i8 { + using.map_or(0, |(slot, _)| if slot == OFFHAND_SLOT { 3 } else { 1 }) +} diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 11809653a..6c0fa4abf 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -5,11 +5,12 @@ //! blocks, attacking and the commands all behave exactly as they do for the //! host, and every world change they cause reaches all players through the //! block log, the level events and the entity tracker. -use super::{Game, Hit, Mining, Screen, lan::LanHost}; +use super::{Game, GuestUse, Hit, Mining, Screen, lan::LanHost}; use horizon_client::{ command::vanilla, containers::BlockEntity, entities::Experience, + first_person::{Hand, SwingAnimation}, items::ItemStack, lan::{ guest::SPECTATOR, @@ -18,7 +19,7 @@ use horizon_client::{ }, motion::{MotionEvent, MotionWorld, PlayerMotion}, }; -use horizon_core::geometry::Vec3; +use horizon_core::{geometry::Vec3, random::RandomSource}; use horizon_protocol::{ packets::play::{self, Clientbound as C, Serverbound as S}, text::literal, @@ -43,14 +44,96 @@ pub(super) struct GuestPlay { /// its progress since `dig_start` is complete (ServerPlayerGameMode.hasDelayedDestroy). delayed: Option<([i32; 3], u64)>, last_progress: i32, - /// Hand movements the client announced and the host animated. - swung: bool, + /// Swings the guest's actions started this tick, for the players around + /// (`LivingEntity.swing` sends ClientboundSwingAnimationPacket). + swings: u32, /// The trading screen the guest has open. pub(super) merchant: Option, /// The inventory menu and the crafting table the guest crafts in. pub(super) crafting: super::lan_crafting::GuestCrafting, /// What the guest's body was given for its effects and enchantments. potions: super::potions::BodyPotions, + /// `ServerPlayer.getStats` and the inventory `inventory_changed` last saw. + pub(super) stats: super::stats::Stats, + last_inventory: u64, + /// `Entity.portalProcess` and `portalCooldown` (game/server_travel.rs). + pub(super) portal: horizon_client::nether_portal::PortalState, + /// `Player.takeXpDelay`: ticks until the next orb can be taken. + xp_delay: u32, + /// What last killed the guest outright (`/kill`): the death message's key. + death_cause: Option, +} + +/// A guest's command waiting for the hub (`LanHost::commands`). +pub(super) struct GuestCommand { + conn: u32, + command: String, + permission: u8, + /// An integrated server's: no admin feedback or level teleports (yet). + integrated: bool, +} + +/// The game's own state while a guest's is swapped in (`enter_guest`), and +/// the guest's records that live outside `Guest`. +pub(super) struct GuestSwap { + conn: u32, + i: usize, + play: GuestPlay, + player: Option, + /// The guest's eye height and the game's. + eye: (f32, f32), + yaw: f32, + pitch: f32, + selected: usize, + mode: horizon_client::command::args::GameMode, + creative: bool, + xp: Experience, + host_keys: std::collections::HashSet, + screen: Screen, + host_sign: Option, + host_book: Option, + host_command_block: Option, + host_mount_menu: super::mount_menu::MountMenu, + craft_size: usize, + craft_grid: Vec>, + container: Option<(i32, i32, i32)>, + mining: Option, + destroy_delay: u32, + using: Option<(usize, u32)>, + use_delay: u32, + attack_ticks: u32, + swing: horizon_client::first_person::SwingState, + left: bool, + right: bool, + riding: Option, + host_station: Option, + sleeping: Option<[i32; 3]>, + sleep_timer: u32, + spawn: Option, + respawn: ( + Option, + Option<[f64; 3]>, + ), + message: Option<(String, std::time::Instant)>, + host_ender: Option<(i32, i32, i32)>, + adv_loaded: bool, + last_inventory: u64, + death_cause: Option, + /// The guest whose turn this one nests in, if any. + acting: (Option, Option, Option), + /// The packets queued for that guest. + packets: Vec, + /// The host's count of swings started, kept while the guest acts. + swings: u32, + /// How many projectiles there were: those added since are the guest's. + projectiles: usize, +} + +impl GuestPlay { + /// The item in use: its slot and for how many ticks. + pub(super) fn using(&self) -> Option<(usize, u32)> { + self.using + } } struct ItemRules<'a>(&'a horizon_client::items::Items); @@ -88,6 +171,24 @@ impl Game<'_> { conn: u32, f: impl FnOnce(&mut Self) -> R, ) -> Option { + let swap = self.enter_guest(host, conn)?; + let result = f(self); + self.leave_guest(host, swap); + Some(result) + } + + /// The first half of `as_guest`: the guest's state in place of the + /// game's, which the returned swap keeps until `leave_guest`. Between the + /// two the host may go back into `self.lan`, so that what the guest does + /// can reach the other players (their commands, `for_player`), and a + /// guest's turn can nest in another's: the acting guest and the packets + /// queued for them are kept and restored too. + pub(super) fn enter_guest(&mut self, host: &mut LanHost, conn: u32) -> Option { + // The guest whose turn it is is in place already (their record holds + // the game's state until their turn ends). + if self.lan_acting == Some(conn) { + return None; + } let i = host.hub.guests.iter().position(|g| g.conn == conn)?; if host.hub.guests[i].motion.is_none() { let p = Vec3::new( @@ -137,13 +238,20 @@ impl Game<'_> { total: g.xp_total, }, ); - let sneak = self.keys.contains(&KeyCode::ShiftLeft); + // The host's held keys stay theirs: the guest holds only sneak, and a + // screen the guest opens (`keys.clear()`) leaves the host walking. + let host_keys = std::mem::take(&mut self.keys); if g.sneaking { self.keys.insert(KeyCode::ShiftLeft); - } else { - self.keys.remove(&KeyCode::ShiftLeft); } + // The window's screens are the host's: a sign, book, command block or + // mount menu the guest's action opens is not drawn over the host's + // world (#687). The guest's client opens its own. let screen = std::mem::replace(&mut self.screen, Screen::Playing); + let host_sign = self.sign_edit.take(); + let host_book = self.book.take(); + let host_command_block = self.command_block_screen.take(); + let host_mount_menu = std::mem::take(&mut self.mount_menu); // The host's crafting grid stays theirs; a crafting table the guest // opens shows as the 3x3 size. let craft_size = std::mem::replace(&mut self.craft_size, 2); @@ -160,19 +268,148 @@ impl Game<'_> { self.right = play.using.is_some() || self.using.is_some(); let riding = self.riding.take(); let host_station = self.station.take(); + // The guest's bed, sleep counter and respawn point; the host's + // respawn point, anchor and action bar stay theirs. + let sleeping = std::mem::replace(&mut self.sleeping, g.bed); + let sleep_timer = std::mem::replace(&mut self.sleep_timer, g.sleep_timer); + let spawn = std::mem::replace( + &mut self.spawn, + g.spawn.map(|s| Vec3::new(s[0], s[1], s[2])), + ); + let respawn = (self.respawn_config.take(), self.level.spawn); + let message = self.message.take(); // The guest's own ender inventory; one the host has open goes back // into the host's while the guest acts. let host_ender = self.ender_open; self.close_ender_chest(); std::mem::swap(&mut self.ender_items, &mut g.ender_items); - self.lan_acting = Some(conn); - self.lan_acting_name = Some(g.name.clone()); - self.lan_acting_uuid = Some(g.uuid); + // The guest's advancements, recipe book and statistics. + self.advancement_tree(); + std::mem::swap(&mut self.adv.progress, &mut g.advancements); + std::mem::swap(&mut self.adv.changed, &mut g.advancements_changed); + std::mem::swap(&mut self.adv.unlocked, &mut g.recipes_unlocked); + std::mem::swap(&mut self.level.recipe_book, &mut g.recipe_book); + std::mem::swap(&mut self.stats, &mut play.stats); + let adv_loaded = std::mem::replace(&mut self.adv.loaded, true); + let last_inventory = std::mem::replace(&mut self.adv.last_inventory, play.last_inventory); + let death_cause = std::mem::replace(&mut self.ui.death_cause, play.death_cause.take()); + let acting = ( + self.lan_acting.replace(conn), + self.lan_acting_name.replace(g.name.clone()), + self.lan_acting_uuid.replace(g.uuid), + ); + let packets = std::mem::take(&mut self.guest_packets); + let swings = self.swings_started; + let projectiles = self.creatures.projectiles.len(); + Some(GuestSwap { + conn, + i, + play, + player, + eye, + yaw, + pitch, + selected, + mode, + creative, + xp, + host_keys, + screen, + host_sign, + host_book, + host_command_block, + host_mount_menu, + craft_size, + craft_grid, + container, + mining, + destroy_delay, + using, + use_delay, + attack_ticks, + swing, + left, + right, + riding, + host_station, + sleeping, + sleep_timer, + spawn, + respawn, + message, + host_ender, + adv_loaded, + last_inventory, + death_cause, + acting, + packets, + swings, + projectiles, + }) + } - let result = f(self); - self.lan_acting = None; - self.lan_acting_name = None; - self.lan_acting_uuid = None; + /// The second half of `as_guest`: the guest's state back into their + /// record, the game's back in place, and what the turn changed sent. + pub(super) fn leave_guest(&mut self, host: &mut LanHost, swap: GuestSwap) { + let GuestSwap { + conn, + i, + mut play, + mut player, + eye, + yaw, + pitch, + selected, + mode, + creative, + xp, + host_keys, + screen, + host_sign, + host_book, + host_command_block, + host_mount_menu, + craft_size, + craft_grid, + container, + mining, + destroy_delay, + using, + use_delay, + attack_ticks, + swing, + left, + right, + riding, + host_station, + sleeping, + sleep_timer, + spawn, + respawn, + message, + host_ender, + adv_loaded, + last_inventory, + death_cause, + acting, + packets, + swings, + projectiles, + } = swap; + // What the guest shot is theirs (Projectile.setOwner). + for p in self.creatures.projectiles.iter_mut().skip(projectiles) { + if p.owner == Some(horizon_client::mobs::Who::Player) && p.guest.is_none() { + p.guest = Some(conn); + } + } + // The guest's swings are theirs: the host's own count stays the host's. + play.swings += self.swings_started.wrapping_sub(swings); + self.swings_started = swings; + for packet in std::mem::replace(&mut self.guest_packets, packets) { + host.hub.send_to(conn, &packet); + } + (self.lan_acting, self.lan_acting_name, self.lan_acting_uuid) = acting; + play.death_cause = std::mem::replace(&mut self.ui.death_cause, death_cause); // An ender chest the guest opened: the menu shows their inventory // itself (lan_open_container), not a stand-in block entity. self.close_ender_chest(); @@ -182,6 +419,12 @@ impl Game<'_> { let opened_table = std::mem::replace(&mut self.craft_size, craft_size) == 3; self.craft_grid = craft_grid; let opened_station = std::mem::replace(&mut self.station, host_station); + let bed = std::mem::replace(&mut self.sleeping, sleeping); + let guest_sleep_timer = std::mem::replace(&mut self.sleep_timer, sleep_timer); + let guest_spawn = std::mem::replace(&mut self.spawn, spawn).map(|s| [s.x, s.y, s.z]); + (self.respawn_config, self.level.spawn) = respawn; + // ServerPlayer.displayClientMessage(_, true): the guest's action bar. + let guest_message = std::mem::replace(&mut self.message, message); self.riding = riding; self.left = left; self.right = right; @@ -193,13 +436,31 @@ impl Game<'_> { play.swing = std::mem::replace(&mut self.swing, swing); self.container = container; self.screen = screen; - if sneak { - self.keys.insert(KeyCode::ShiftLeft); - } else { - self.keys.remove(&KeyCode::ShiftLeft); - } + self.sign_edit = host_sign; + self.book = host_book; + self.command_block_screen = host_command_block; + self.mount_menu = host_mount_menu; + self.keys = host_keys; let g = &mut host.hub.guests[i]; + let fell_asleep = bed.is_some() && g.bed.is_none(); + (g.bed, g.sleep_timer, g.spawn) = (bed, guest_sleep_timer, guest_spawn); + g.sleeping = g.bed.is_some(); + if let Some((text, _)) = guest_message { + g.send(&C::SystemChat(play::SystemChat { + content: literal(&text), + overlay: true, + })); + } std::mem::swap(&mut self.ender_items, &mut g.ender_items); + std::mem::swap(&mut self.adv.progress, &mut g.advancements); + std::mem::swap(&mut self.adv.changed, &mut g.advancements_changed); + std::mem::swap(&mut self.adv.unlocked, &mut g.recipes_unlocked); + std::mem::swap(&mut self.level.recipe_book, &mut g.recipe_book); + std::mem::swap(&mut self.stats, &mut play.stats); + self.adv.loaded = adv_loaded; + play.last_inventory = std::mem::replace(&mut self.adv.last_inventory, last_inventory); + let stat_events = std::mem::take(&mut play.stats.events); + let name = g.name.clone(); self.potions.swap_player(&mut g.effects, &mut play.potions); if let Some(key) = host_ender { self.world @@ -221,6 +482,9 @@ impl Game<'_> { std::mem::swap(&mut self.armor, &mut g.armor); std::mem::swap(&mut self.offhand, &mut g.offhand); std::mem::swap(&mut self.carried, &mut g.carried); + // A teleport with a rotation (commands) turns the guest. + let turned = [self.yaw, self.pitch] != [g.yaw, g.pitch]; + [g.yaw, g.pitch] = [self.yaw, self.pitch]; self.yaw = yaw; self.pitch = pitch; self.eye = eye.1; @@ -234,16 +498,30 @@ impl Game<'_> { m.sprinting = false; let moved: f64 = (0..3).map(|k| (at[k] - g.position[k]).powi(2)).sum(); g.motion = Some(Box::new(m)); - if moved > 0.0001 { + if moved > 0.0001 || turned { host.hub.teleport(i, Some(at)); } } let g = &mut host.hub.guests[i]; + if let Some(head) = g.bed.filter(|_| fell_asleep) { + // LivingEntity.startSleeping: in the bed, still. + let at = [ + f64::from(head[0]) + 0.5, + f64::from(head[1]) + 0.5625, + f64::from(head[2]) + 0.5, + ]; + g.position = at; + if let Some(m) = g.motion.as_mut() { + m.position = Vec3::new(at[0], at[1], at[2]); + m.velocity = Vec3::ZERO; + } + } if new_mode != g.game_mode { let conn = g.conn; host.hub.set_game_mode(conn, new_mode); } host.play.insert(conn, play); + self.stat_criteria_changed(&name, stat_events); if let Some(pos) = opened && !self.lan_open_container(host, conn, pos) && !host.hub.guests[i].spectator() @@ -257,7 +535,32 @@ impl Game<'_> { if opened_table { self.lan_open_crafting(host, conn); } - Some(result) + } + + /// `ServerLevel.players()` in turn: the host's own player, then each + /// guest in the level in place with their state swapped in (`as_guest`). + /// What reaches the players around a point (a beacon's powers, a splash + /// potion, a warden's darkness, an elder guardian's curse) lands on each + /// of them, not only on the host. A server has no player of its own. + /// Inside a guest's action (the hub taken) only that player is in place. + pub(super) fn for_each_player(&mut self, mut f: impl FnMut(&mut Self)) { + if self.player.is_some() { + f(self); + } + let Some(mut host) = self.lan.take() else { + return; + }; + let conns: Vec = host + .hub + .guests + .iter() + .filter(|g| host.hub.here(g) && g.loaded && !g.dead) + .map(|g| g.conn) + .collect(); + for conn in conns { + self.as_guest(&mut host, conn, &mut f); + } + self.lan = Some(host); } /// The game mode fields without `set_mode`'s side effects on the host. @@ -277,7 +580,7 @@ impl Game<'_> { fn lan_open_container(&mut self, host: &mut LanHost, conn: u32, pos: (i32, i32, i32)) -> bool { let name = &self .res - .facts + .facts() .get(self.world.block(pos.0, pos.1, pos.2)) .name; let (menu, title, slots) = if &**name == "minecraft:ender_chest" { @@ -295,6 +598,13 @@ impl Game<'_> { _ => ("minecraft:furnace", "container.furnace"), }; (menu, title, Slots::Block(pos)) + } else if let Some(BlockEntity::Brewing(_)) = self.world.block_entities.get(&pos) { + // BrewingStandBlock.useWithoutItem: BrewingStandMenu. + ( + "minecraft:brewing_stand", + "container.brewing", + Slots::Block(pos), + ) } else if let Some((other, right)) = self.chest_partner(pos) { // ChestBlock.combine: the right half's slots come first. let halves = if right { @@ -357,7 +667,15 @@ impl Game<'_> { let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) else { return; }; - if host.hub.guests[gi].dead && !matches!(packet, S::ClientCommand(_)) { + // A dead player acts no more, but the death screen still respawns and chats: its + // commands run as anyone's (ServerGamePacketListenerImpl.handleChatCommand doesn't + // look at the player's health). + if host.hub.guests[gi].dead + && !matches!( + packet, + S::ClientCommand(_) | S::ChatCommand(_) | S::ChatCommandSigned(_) + ) + { return; } let spectator = host.hub.guests[gi].spectator(); @@ -442,6 +760,16 @@ impl Game<'_> { } } } + // ServerGamePacketListenerImpl.handlePunch: the main hand swings + // with the held item's attack animation (mining, attacking, a + // click on nothing), seen by the players around, and the attack + // strength starts again. + S::Punch(_) => { + self.as_guest(host, conn, |g| { + g.swing_hand(Hand::Main, SwingAnimation::DEFAULT); + g.reset_attack_strength(); + }); + } S::UseItemOn(p) => { host.hub.ack(conn, p.sequence); if spectator { @@ -457,11 +785,17 @@ impl Game<'_> { f64::from(p.pos[2]) + f64::from(p.cursor[2]), ], }; - self.as_guest(host, conn, |g| { + let hand = if p.hand == 1 { Hand::Off } else { Hand::Main }; + let swung = self.as_guest(host, conn, |g| { g.forced_hit = Some(hit); + g.guest_use = Some(GuestUse::OnBlock(hand)); + let swings = g.swings_started; let _ = g.use_item(); g.forced_hit = None; + g.guest_use = None; + g.swings_started != swings }); + self.lan_swing_back(host, conn, swung); // The client predicted a result: correct it with what really is. for pos in [ p.pos, @@ -477,22 +811,31 @@ impl Game<'_> { if spectator { return; } - // handleUseItem: the client's rotation, then the item alone. What it - // looked at a moment ago (a block, a mob) is UseItemOn's or Interact's. - self.as_guest(host, conn, |g| { - (g.yaw, g.pitch) = (p.yaw, p.pitch); - g.lan_item_only = true; + // handleUseItem: the client's rotation (absSnapRotationTo, which + // sends nothing back), then the item alone. What it looked at a + // moment ago (a block, a mob) is UseItemOn's or Interact's. + let hand = if p.hand == 1 { Hand::Off } else { Hand::Main }; + let g = &mut host.hub.guests[gi]; + (g.yaw, g.pitch) = (p.yaw, p.pitch); + let swung = self.as_guest(host, conn, |g| { + g.guest_use = Some(GuestUse::Item(hand)); + let swings = g.swings_started; let _ = g.use_item(); - g.lan_item_only = false; + g.guest_use = None; + g.swings_started != swings }); + self.lan_swing_back(host, conn, swung); } S::Attack(p) => self.guest_attack(host, conn, p.id), S::Interact(p) => { if let Some((position, eye)) = self.look_at_entity(host, gi, p.id) { let _ = (position, eye); - self.as_guest(host, conn, |g| { + let swung = self.as_guest(host, conn, |g| { + let swings = g.swings_started; let _ = g.use_item(); + g.swings_started != swings }); + self.lan_swing_back(host, conn, swung); } } S::SetCreativeModeSlot(p) => { @@ -506,7 +849,7 @@ impl Game<'_> { S::ContainerClick(p) if self.lan_merchant_click(host, conn, &p) => {} S::ContainerClick(p) if self.lan_inventory_click(host, conn, &p) => {} S::ContainerClick(p) if self.lan_crafting_click(host, conn, &p) => {} - S::ContainerClick(p) if self.lan_furnace_click(host, conn, &p) => {} + S::ContainerClick(p) if self.lan_machine_click(host, conn, &p) => {} S::SelectTrade(p) => self.lan_select_trade(host, conn, p.item), S::ContainerClose(p) if self.lan_merchant_closed(host, conn, Some(p.container)) => { let g = &mut host.hub.guests[gi]; @@ -521,7 +864,7 @@ impl Game<'_> { } } S::ContainerClick(p) => { - let rules = ItemRules(&self.res.items); + let rules = ItemRules(self.res.items()); let drops = host.hub.container_click(conn, &p, &mut self.world, &rules); self.drop_guest_items(host, gi, drops); } @@ -539,7 +882,8 @@ impl Game<'_> { self.drop_guest_items(host, gi, vec![c]); } } - S::ChatCommand(c) => self.guest_command(host, conn, &c.command), + S::ChatCommand(c) => self.guest_chat_command(host, conn, &c.command), + S::ChatCommandSigned(p) => self.guest_signed_command(host, conn, p), S::ChangeDifficulty(p) if host.hub.guests[gi].permission >= 2 => { if !self.level.difficulty_locked && (0..=3).contains(&p.difficulty) { self.ui.difficulty = if self.level.hardcore { @@ -628,12 +972,7 @@ impl Game<'_> { }), ); } - } - } - S::Unknown(8, payload) => { - // A signed command: the text comes first. - if let Ok(text) = horizon_protocol::buf::Reader::new(&payload).string(32767) { - self.guest_command(host, conn, &text); + self.send_admin_feedback(host, Some(conn)); } } S::Unknown(id, payload) @@ -641,10 +980,20 @@ impl Game<'_> { { self.guest_suggestions(host, conn, &payload); } + S::PlaceRecipe(p) => self.lan_place_recipe(host, conn, &p), S::ClientCommand(c) if c.action == 0 => self.guest_respawn(host, conn), + // ServerStatsCounter.sendStats. + S::ClientCommand(c) if c.action == 1 => { + let stats = host + .play + .get_mut(&conn) + .map(|p| p.stats.take_dirty()) + .unwrap_or_default(); + host.hub.send_stats(conn, &stats); + } S::PickItemFromBlock(p) => { let state = self.world.block(p.pos[0], p.pos[1], p.pos[2]); - let item = self.res.facts.get(state).item.clone(); + let item = self.res.facts().get(state).item.clone(); let g = &mut host.hub.guests[gi]; if g.creative() && &*item != "minecraft:air" { let slot = g.selected; @@ -750,7 +1099,18 @@ impl Game<'_> { } /// Commands.performCommand for a guest with command access. - fn guest_command(&mut self, host: &mut LanHost, conn: u32, command: &str) { + /// Commands.performCommand for a guest, with the message arguments it + /// signed (none after an unsigned command packet). + pub(super) fn guest_command( + &mut self, + host: &mut LanHost, + conn: u32, + command: &str, + signed: Option>, + ) { + if self.guest_message_command(host, conn, command, signed.as_ref()) { + return; + } // ListPlayersCommand needs no permission and lists every player. let words: Vec<&str> = command.trim_start_matches('/').split(' ').collect(); if words[0] == "list" && (words.len() == 1 || words[1..] == ["uuids"]) { @@ -767,34 +1127,12 @@ impl Game<'_> { return; } // An integrated server runs every player's commands at their own - // permission level (`CommandSourceStack.permissions`). - if host.hub.config.integrated { - let Some(level) = host.hub.guest(conn).map(|g| g.permission) else { - return; - }; - let command = command.trim_start_matches('/').to_owned(); - let lines = self.as_guest(host, conn, |g| { - let console = std::mem::replace(&mut g.ui.registries.permission, level); - g.chat_capture = Some(Vec::new()); - g.run_command(&command); - g.ui.registries.permission = console; - g.chat_capture.take().unwrap_or_default() - }); - for line in lines.unwrap_or_default() { - host.hub.send_to( - conn, - &C::SystemChat(play::SystemChat { - content: horizon_client::lan::data::component_nbt(&line.component()), - overlay: false, - }), - ); - } - return; - } - // A LAN world follows its Allow Commands; a dedicated server leaves - // it to the ops list (the guest's permission level). + // permission level (`CommandSourceStack.permissions`); a LAN world + // follows its Allow Commands, and a dedicated server leaves it to + // the ops list (the guest's permission level). + let integrated = host.hub.config.integrated; let allowed = self.options.dedicated.is_some() || self.allow_commands(); - if !host.settings.guest_commands || !allowed { + if !integrated && (!host.settings.guest_commands || !allowed) { host.hub.send_to( conn, &C::SystemChat(play::SystemChat { @@ -804,30 +1142,95 @@ impl Game<'_> { ); return; } - let command = command.trim_start_matches('/').to_owned(); - // The guest's own level (Commands.performCommand with the player's source): an - // operator's on a dedicated server, never the host's. - let permission = host.hub.guest(conn).map_or(0, |g| g.permission); - let lines = self.as_guest(host, conn, |g| { - g.chat_capture = Some(Vec::new()); - let level = std::mem::replace(&mut g.ui.registries.permission, permission); - g.run_command(&command); - g.ui.registries.permission = level; - g.chat_capture - .take() - .unwrap_or_default() - .iter() - .map(|t| horizon_client::lan::data::component_nbt(&t.component())) - .collect::>() + // The guest's own level (Commands.performCommand with the player's + // source): an operator's on a dedicated server, never the host's. + let Some(permission) = host.hub.guest(conn).map(|g| g.permission) else { + return; + }; + host.commands.push(GuestCommand { + conn, + command: command.trim_start_matches('/').to_owned(), + permission, + integrated, }); - for content in lines.unwrap_or_default() { - host.hub.send_to( - conn, - &C::SystemChat(play::SystemChat { - content, - overlay: false, - }), - ); + } + + /// The guests' queued commands, run with the hub in `self.lan` so that + /// they reach the other players too (`/give Bob`, `/tp Bob`, `/kick`). + pub(super) fn run_guest_commands(&mut self) { + loop { + let Some(host) = self.lan.as_mut() else { + return; + }; + if host.commands.is_empty() { + return; + } + let c = host.commands.remove(0); + let mut host = self.lan.take().expect("checked above"); + let Some(swap) = self.enter_guest(&mut host, c.conn) else { + self.lan = Some(host); + continue; + }; + self.lan = Some(host); + self.chat_capture = Some(Vec::new()); + let level = std::mem::replace(&mut self.ui.registries.permission, c.permission); + self.run_command(&c.command); + self.ui.registries.permission = level; + let lines = self.chat_capture.take().unwrap_or_default(); + let mut host = self.lan.take().expect("a command puts the hub back"); + self.leave_guest(&mut host, swap); + host.hub.sync_all_effects(); + if !c.integrated { + // A teleport into another level happens before its feedback is sent. + self.run_level_teleports(&mut host); + } + for line in lines { + host.hub.send_to( + c.conn, + &C::SystemChat(play::SystemChat { + content: horizon_client::lan::data::component_nbt(&line.component()), + overlay: false, + }), + ); + } + if !c.integrated { + self.send_admin_feedback(&mut host, Some(c.conn)); + } + self.lan = Some(host); + } + } + + /// The `broadcastToAdmins` lines a command queued (`source`: the guest + /// whose it was; none for the host's or the console's): every other + /// operator gets them, and the console logs them as the server's own + /// system messages (MinecraftServer.sendSystemMessage). + pub(super) fn send_admin_feedback(&mut self, host: &mut LanHost, source: Option) { + // PlayerList.isOp for the host of a world open to LAN: the owner when + // the world allows commands, everyone when the LAN does. + let host_op = source.is_some() + && !host.hub.config.dedicated + && (self.allow_commands() || host.hub.config.guest_commands); + for (line, ops, console) in std::mem::take(&mut self.admin_feedback) { + if ops { + let content = horizon_client::lan::data::component_nbt(&line.component()); + for g in host + .hub + .guests + .iter() + .filter(|g| Some(g.conn) != source && g.permission > 0) + { + g.send(&C::SystemChat(play::SystemChat { + content: content.clone(), + overlay: false, + })); + } + if host_op { + self.system_message(line.clone()); + } + } + if console { + super::dedicated::log("INFO", &format!("System chat: {}", line.plain())); + } } } @@ -872,13 +1275,34 @@ impl Game<'_> { } fn guest_respawn(&mut self, host: &mut LanHost, conn: u32) { - let spawn = self.spawn.map_or([0.5, 80.0, 0.5], |s| [s.x, s.y, s.z]); let Some(i) = host.hub.guests.iter().position(|g| g.conn == conn) else { return; }; + // The guest's own bed, else the world spawn: never the host's bed. + let world = self + .level + .world_spawn + .map(|[x, y, z]| [f64::from(x) + 0.5, f64::from(y), f64::from(z) + 0.5]); + let spawn = host.hub.guests[i] + .spawn + .or(world) + .or(self.spawn.map(|s| [s.x, s.y, s.z])) + .unwrap_or([0.5, 80.0, 0.5]); if !host.hub.guests[i].dead { return; } + // PlayerList.respawn: a server's player comes back in the Overworld, + // where the world spawn is (respawn anchors and beds aside). + let back = self.dim; + let away = self.options.dedicated.is_some() + && host.hub.guests[i].dimension != super::Dim::Overworld.id(); + if away { + self.server_trips.retain(|t| !t.is(conn)); + if let Err(e) = self.enter_level(Some(host), super::Dim::Overworld) { + log::error!("could not open the Overworld: {e}"); + return; + } + } let g = &mut host.hub.guests[i]; g.dead = false; g.health = 20.0; @@ -897,6 +1321,7 @@ impl Game<'_> { if let Some(play) = host.play.get_mut(&conn) { play.potions = Default::default(); } + let clock_rate = self.network_clock_rate(); let ctx = TickCtx { world: &mut self.world, host: None, @@ -905,32 +1330,34 @@ impl Game<'_> { spawn, difficulty: i32::from(self.ui.difficulty), difficulty_locked: self.level.difficulty_locked, - clock_rate: if self.ui.rules.bool("advance_time") { - self.ui.clock.rate - } else { - 0.0 - }, + clock_rate, tick_rate: self.clock.rate(), tick_frozen: self.clock.frozen(), + collision: Some(self.collision), }; host.hub.respawn(i, spawn, &ctx); + if away && let Err(e) = self.enter_level(Some(host), back) { + log::error!("could not reopen {}: {e}", back.id()); + } } /// Per tick, for every guest: dig, use items, pick up drops, die. pub(super) fn tick_guests(&mut self, host: &mut LanHost) { self.lan_merchant_tick(host); + self.announce_guest_advancements(host); let conns: Vec = host .hub .guests .iter() - .filter(|g| g.placed && g.loaded) + .filter(|g| host.hub.here(g) && g.loaded) .map(|g| g.conn) .collect(); for conn in conns { let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) else { continue; }; - if host.hub.guests[gi].dead { + self.guest_sleep_tick(host, gi); + if host.hub.guests[gi].dead || self.on_server_trip(conn) { continue; } let play = host.play.entry(conn).or_default(); @@ -962,8 +1389,18 @@ impl Game<'_> { } self.guest_delayed_destroy(host, conn); self.guest_server_tick(host, conn); + // Player.tick's statistics and ServerPlayer.tick's triggers, then + // PlayerAdvancements.flushDirty. + let sleeping = host.hub.guests[gi].sleeping; + self.as_guest(host, conn, |g| { + g.count_tick_stats(sleeping); + g.tick_advancements(); + g.update_player_attributes(); + }); + host.hub.flush_progress(conn); self.guest_dig_feedback(host, conn); self.guest_pickups(host, gi); + self.guest_orbs(host, gi); let g = &mut host.hub.guests[gi]; if g.health <= 0.0 && !g.dead { g.dead = true; @@ -975,7 +1412,34 @@ impl Game<'_> { total: g.xp_total, } .death_reward(self.ui.rules.bool("keep_inventory") || g.game_mode == SPECTATOR); - let message = literal(&format!("{} died", g.name)); + let (name, uuid) = (g.name.clone(), g.uuid); + let cause = host.play.get_mut(&conn).and_then(|p| p.death_cause.take()); + let message = match cause { + // ServerPlayer.die: the death message, to everyone unless + // the player's team hides it. + Some(key) if self.ui.rules.bool("show_death_messages") => { + let text = horizon_client::text::Text::translate( + &key, + &[self.player_text(&name, uuid)], + ); + let message = horizon_client::lan::data::component_nbt(&text.component()); + let team = self.score.board.players_team(&name); + if horizon_client::scoreboard::own_death_message_shown(team) { + host.hub.system_message(message.clone()); + if self.options.dedicated.is_some() { + super::dedicated::log( + "INFO", + &format!("System chat: {}", text.plain()), + ); + } else if self.player.is_some() { + self.system_message(text); + } + } + message + } + Some(_) => literal(""), + None => literal(&format!("{name} died")), + }; host.hub.send_to( conn, &C::PlayerCombatKill(play::PlayerCombatKill { id, message }), @@ -987,6 +1451,74 @@ impl Game<'_> { } } + /// `Player.tick`'s sleep counter, and `LivingEntity.tick`'s wake-up of a + /// sleeper who died or whose bed is gone. + fn guest_sleep_tick(&mut self, host: &mut LanHost, gi: usize) { + let g = &mut host.hub.guests[gi]; + g.sleep_timer = super::tick_sleep_timer(g.sleep_timer, g.bed.is_some()); + if let Some(head) = g.bed { + let block = self.world.block(head[0], head[1], head[2]); + let bed = self.res.facts().get(block).name.ends_with("_bed"); + if g.dead || g.health <= 0.0 || !bed { + host.hub.wake(gi); + } + } + } + + /// `SleepStatus.areEnoughSleeping` and `areEnoughDeepSleeping` over the + /// host's player and the guests: of the players not in spectator mode, + /// players_sleeping_percentage of them (at least one) are in bed, and as + /// many have slept 100 ticks. + pub(super) fn enough_slept(&self) -> bool { + let (mut active, mut sleeping, mut deep) = (0i64, 0i64, 0i64); + let mut count = |in_bed: bool, timer: u32| { + active += 1; + if in_bed { + sleeping += 1; + if timer >= 100 { + deep += 1; + } + } + }; + if self.player.is_some() && !self.spectator() { + count(self.sleeping.is_some(), self.sleep_timer); + } + if let Some(host) = self.lan.as_ref() { + for g in host + .hub + .guests + .iter() + .filter(|g| g.placed && !g.spectator()) + { + count(g.bed.is_some(), g.sleep_timer); + } + } + let percent = i64::from(self.ui.rules.int("players_sleeping_percentage").max(0)); + let needed = ((active * percent + 99) / 100).max(1); + sleeping >= needed && deep >= needed + } + + /// `ServerLevel.wakeUpAllPlayers` for the guests. + pub(super) fn wake_guests(&mut self) { + if let Some(host) = self.lan.as_mut() { + for i in 0..host.hub.guests.len() { + host.hub.wake(i); + } + } + } + + /// `PlayerList.broadcastSystemMessage` of the advancements guests made + /// (their own actions, commands and ticks). + fn announce_guest_advancements(&mut self, host: &mut LanHost) { + for text in std::mem::take(&mut self.adv.announced) { + host.hub + .system_message(horizon_client::lan::data::component_nbt(&text.component())); + if self.options.dedicated.is_none() { + self.system_message(text); + } + } + } + /// The guest's body on the server each tick: ServerPlayer.tick's damage /// clock and Entity.baseTick's fluid phase (burning), then for each move /// the guest made ServerGamePacketListenerImpl.handleMovePlayer's jump @@ -1034,6 +1566,7 @@ impl Game<'_> { w.player.health.advance_clock(); let _ = w.fluid_base_tick(); w.player.ground.on_ground = from_ground; + let travelled: Vec<[f64; 3]> = moves.iter().map(|m| m.0).collect(); for (moved, on_ground) in moves { let movement = Vec3::new(moved[0], moved[1], moved[2]); if w.player.ground.on_ground && !on_ground && moved[1] > 0.0 { @@ -1058,6 +1591,11 @@ impl Game<'_> { let _ = w.apply_fire_contact(); let _ = w.server_hazards_tick(in_wall); let mut events = std::mem::take(&mut w.player.events); + // ServerGamePacketListenerImpl.handleMovePlayer: + // Player.checkMovementStatistics for each move. + for d in travelled { + game.movement_stats(Vec3::new(d[0], d[1], d[2])); + } // LivingEntity.baseTick ends with tickEffects. game.tick_mob_effects(); let Some(player) = game.player.as_mut() else { @@ -1088,7 +1626,7 @@ impl Game<'_> { let range = |v: f64| (v - half).floor() as i32..=(v + half).floor() as i32; range(at[0]).any(|x| { range(at[2]).any(|z| { - let f = self.res.facts.get(self.world.block(x, y, z)); + let f = self.res.facts().get(self.world.block(x, y, z)); f.full_collision && f.solid_render }) }) @@ -1106,7 +1644,7 @@ impl Game<'_> { play.mining = None; let start = play.dig_start; let state = self.world.block(pos[0], pos[1], pos[2]); - if !dug || self.res.facts.is_air(state) { + if !dug || self.res.facts().is_air(state) { return; } let progress = self @@ -1130,7 +1668,7 @@ impl Game<'_> { }; let ticks = host.hub.ticks; let state = self.world.block(pos[0], pos[1], pos[2]); - let progress = if self.res.facts.is_air(state) { + let progress = if self.res.facts().is_air(state) { None } else { self.as_guest(host, conn, |g| g.destroy_progress(pos)) @@ -1247,6 +1785,79 @@ impl Game<'_> { } } + /// `Player.tick`'s takeXpDelay and `Player.aiStep`'s touch of the + /// experience orbs in the guest's box grown by 1, 0.5, 1: one of them at + /// random, at most every 2 ticks (`ExperienceOrb.playerTouch`), repairs + /// with Mending and gives the rest as points. Everyone near hears it. + fn guest_orbs(&mut self, host: &mut LanHost, gi: usize) { + let g = &host.hub.guests[gi]; + // Player's standing and crouching boxes: 1.8 and 1.5 high. + let height = if g.sneaking { 1.5 } else { 1.8 }; + let (conn, id, at) = (g.conn, g.entity_id, g.position); + let unable = g.spectator() || g.dead || g.health <= 0.0; + let play = host.play.entry(conn).or_default(); + if play.xp_delay > 0 { + play.xp_delay -= 1; + } + if unable || play.xp_delay > 0 { + return; + } + let (min, max) = ( + [at[0] - 1.3, at[1] - 0.5, at[2] - 1.3], + [at[0] + 1.3, at[1] + height + 0.5, at[2] + 1.3], + ); + let touching: Vec = self + .orbs + .iter() + .enumerate() + .filter(|(_, o)| { + o.position.x + 0.25 > min[0] + && o.position.x - 0.25 < max[0] + && o.position.y + 0.5 > min[1] + && o.position.y < max[1] + && o.position.z + 0.25 > min[2] + && o.position.z - 0.25 < max[2] + }) + .map(|(i, _)| i) + .collect(); + if touching.is_empty() { + return; + } + // Util.getRandom(orbs, random). + let pick = self + .world + .random + .next_int_bound(touching.len() as i32) + .unwrap_or(0) as usize; + let orb = self.orbs.remove(touching[pick]); + play.xp_delay = 2; + host.hub.take_item(orb.id as i32, id, 1, at); + // ClientPacketListener.handleTakeItemEntity's sound, at the orb. + let pitch = (self.world.random.next_float() - self.world.random.next_float()) * 0.35 + 0.9; + let p = orb.position; + self.audio.play( + "minecraft:entity.experience_orb.pickup", + Some([p.x, p.y, p.z]), + 0.1, + pitch, + ); + let value = orb.value; + if let Some(target) = super::pickups::guest_target(host, id as u32) { + use super::pickups::{Collector, Taken}; + self.add_pickup_toward( + Taken::Orb(Box::new(orb)), + Collector::Guest(id as u32), + target, + ); + } + self.as_guest(host, conn, |game| { + let rest = game.mending(value); + if rest > 0 { + game.give_orb_points(rest); + } + }); + } + /// The host's own `ItemPickupParticle` toward the guest's model. fn guest_pickup( &mut self, @@ -1282,20 +1893,72 @@ impl Game<'_> { } } - /// Guests' arm swings for everyone else. + /// The guests' items in use, for the players around (`LivingEntity.startUsingItem` + /// and `stopUsingItem` set DATA_LIVING_ENTITY_FLAGS). + pub(super) fn lan_living_flags(&mut self, host: &mut LanHost) { + for g in &mut host.hub.guests { + let using = host.play.get(&g.conn).and_then(GuestPlay::using); + g.living_flags = super::lan::living_flags(using); + } + } + + /// A use the server ran swung the guest's hand: their own client is told + /// too (`swing(hand, true)` for a SwingSource.SERVER result). Horizon's + /// client does not run uses itself, so it does not predict the swing; a + /// client that did (a block placed) is still in the first half of its + /// swing and ignores it (`SwingState.startIfAble`). + fn lan_swing_back(&mut self, host: &mut LanHost, conn: u32, swung: Option) { + if swung != Some(true) { + return; + } + let (Some(g), Some(play)) = (host.hub.guest(conn), host.play.get(&conn)) else { + return; + }; + if let Some(c) = play.swing.current { + host.hub + .send_to(conn, &swing_packet(g.entity_id, c.hand, c.animation)); + } + } + + /// The tick's arm swings for the players around (the guests' to everyone + /// else, the host's to the guests), and `LivingEntity.updateSwingTime` + /// for the guests' bodies here. pub(super) fn lan_swings(&mut self, host: &mut LanHost) { let conns: Vec = host.play.keys().copied().collect(); for conn in conns { let Some(g) = host.hub.guests.iter().find(|g| g.conn == conn) else { continue; }; - let (id, at) = (g.entity_id, g.position); + let (id, at, here) = (g.entity_id, g.position, host.hub.here(g)); let play = host.play.get_mut(&conn).expect("play"); - let swinging = play.swing.swinging() && play.swing.ticks == 0; - if swinging && !play.swung { - host.hub.swing(id, 0, at, Some(conn)); + let swung = std::mem::take(&mut play.swings) > 0; + if let (true, true, Some(c)) = (swung, here, play.swing.current) { + let packet = swing_packet(id, c.hand, c.animation); + host.hub.broadcast_near(at, 128.0, &packet, Some(conn)); + if let Some(m) = host.models.get_mut(&conn) { + m.swing(); + } + } + play.swing.tick(); + } + if self.swings_started != host.host_swings { + host.host_swings = self.swings_started; + if let (Some(p), Some(c)) = (self.player.as_ref(), self.swing.current) { + let at = [p.position.x, p.position.y, p.position.z]; + let packet = swing_packet(host.host_entity, c.hand, c.animation); + host.hub.broadcast_near(at, 128.0, &packet, None); } - play.swung = swinging; } } } + +/// ClientboundSwingAnimationPacket: the hand and the animation (its type and +/// base duration; each client applies the swinger's haste and fatigue). +fn swing_packet(id: i32, hand: Hand, animation: SwingAnimation) -> C { + C::SwingAnimation(play::SwingAnimation { + id, + hand: i32::from(hand == Hand::Off), + kind: i32::from(animation.kind), + duration: animation.duration, + }) +} diff --git a/crates/horizon-client/src/game/lan_attributes.rs b/crates/horizon-client/src/game/lan_attributes.rs new file mode 100644 index 000000000..72d06c2ef --- /dev/null +++ b/crates/horizon-client/src/game/lan_attributes.rs @@ -0,0 +1,274 @@ +//! The attribute instances a server syncs for the living entities it tracks +//! (`ClientboundUpdateAttributesPacket`, lan/attributes.rs): the ones +//! vanilla's `AttributeMap` would have created by then, since only those are +//! sent, never the type's other defaults. +use super::Game; +use super::commands::Target; +use horizon_client::{ + effects, + lan::attributes::{add_modifier, generic_syncable, instance, operation_named}, + mobs::{ + Mob, + finalize::{self, Attr, Op}, + }, +}; +use horizon_protocol::packets::play::{AttributeModifier, AttributeSnapshot}; +use std::sync::Arc; + +/// `EquipmentSlotGroup.test`: whether a modifier's slot group covers a slot. +fn slot_in_group(group: &str, slot: &str) -> bool { + group == slot + || group == "any" + || (group == "hand" && matches!(slot, "mainhand" | "offhand")) + || (group == "armor" && matches!(slot, "feet" | "legs" | "chest" | "head" | "body")) +} + +impl Game<'_> { + /// The registry id of a client-syncable attribute the entity type has + /// (`AttributeMap.getInstance` is null for the others), by name. + fn synced_attribute(&self, entity: &str, name: &str) -> Option { + let catalog = &self.motion.attributes; + let id = catalog.id(name)?; + (catalog.attribute(id).syncable && catalog.default_base(entity, id).is_some()) + .then(|| id.registry_id()) + } + + /// The type's base value of an attribute, as the instance starts. + fn attribute_default(&self, entity: &str, name: &str) -> f64 { + let catalog = &self.motion.attributes; + catalog + .id(name) + .and_then(|id| catalog.default_base(entity, id)) + .unwrap_or(0.0) + } + + /// `LivingEntity.collectEquipmentChanges`: each worn or held item's + /// attribute modifiers, slot by slot in `EquipmentSlot` order, create + /// their instances (an instance without a modifier keeps its base). + fn equipment_attributes<'a>( + &self, + entity: &str, + base: impl Fn(&str) -> Option, + slots: impl IntoIterator>)>, + out: &mut Vec, + ) { + for (slot, item) in slots { + let Some(info) = item.and_then(|i| self.res.items().get(i)) else { + continue; + }; + for (attribute, id, amount, op, group) in &info.modifiers { + if !slot_in_group(group, slot) { + continue; + } + let Some(a) = self.synced_attribute(entity, attribute) else { + continue; + }; + let start = + base(attribute).unwrap_or_else(|| self.attribute_default(entity, attribute)); + add_modifier( + out, + a, + start, + AttributeModifier { + id: id.clone(), + amount: *amount, + operation: operation_named(op), + }, + ); + } + } + } + + /// A mob's synced instances: movement speed (its first `aiStep` reads it, + /// `LivingEntity.removeFrost`), what finalizeSpawn set, the baby speed + /// modifier, its effects' modifiers, its equipment's, and what + /// `/attribute` changed. + pub(super) fn mob_attributes(&self, m: &Mob) -> Vec { + let kind = m.kind.id.as_str(); + let mut out = Vec::new(); + if let Some(a) = self.synced_attribute(kind, "minecraft:movement_speed") { + instance(&mut out, a, finalize::base_value(m, Attr::MovementSpeed)); + } + if let Some(s) = m.extra.attributes.as_deref() { + for (attr, v) in &s.bases { + if let Some(a) = self.synced_attribute(kind, &format!("minecraft:{}", attr.name())) + { + instance(&mut out, a, *v).base = *v; + } + } + for x in &s.modifiers { + if let Some(a) = + self.synced_attribute(kind, &format!("minecraft:{}", x.attr.name())) + { + add_modifier( + &mut out, + a, + finalize::base_value(m, x.attr), + AttributeModifier { + id: x.id.to_owned(), + amount: x.amount, + operation: match x.op { + Op::AddValue => 0, + Op::AddMultipliedBase => 1, + Op::AddMultipliedTotal => 2, + }, + }, + ); + } + } + } + if let (Some(a), Some((amount, Op::AddMultipliedBase))) = ( + self.synced_attribute(kind, "minecraft:movement_speed"), + finalize::transient(m, Attr::MovementSpeed), + ) { + add_modifier( + &mut out, + a, + finalize::base_value(m, Attr::MovementSpeed), + AttributeModifier { + id: "minecraft:baby".into(), + amount, + operation: 1, + }, + ); + } + let base = |name: &str| { + Attr::ALL + .into_iter() + .find(|a| name.strip_prefix("minecraft:") == Some(a.name())) + .map(|a| finalize::base_value(m, a)) + }; + // LivingEntity.onEffectAdded: MobEffect.addAttributeModifiers. + for &(effect, _, amplifier) in &m.extra.effects { + let Some((attribute, id, amount, op)) = + effects::effect_modifier(effect.trim_start_matches("minecraft:")) + else { + continue; + }; + let Some(a) = self.synced_attribute(kind, attribute) else { + continue; + }; + let start = base(attribute).unwrap_or_else(|| self.attribute_default(kind, attribute)); + add_modifier( + &mut out, + a, + start, + AttributeModifier { + id: id.to_owned(), + amount: amount * f64::from(amplifier + 1), + operation: operation_named(op), + }, + ); + } + let e = &m.extra; + self.equipment_attributes( + kind, + base, + [ + ("mainhand", e.main_hand.as_ref()), + ("offhand", e.off_hand.as_ref()), + ("feet", e.feet.as_ref()), + ("legs", e.legs.as_ref()), + ("chest", e.chest.as_ref()), + ("head", e.head.as_ref()), + ], + &mut out, + ); + self.generic_attributes_of(Target::Mob(m.id), &mut out); + out + } + + /// An armor stand's synced instances: movement speed (its first + /// `aiStep`), its equipment's modifiers and what `/attribute` changed. + pub(super) fn stand_attributes( + &self, + id: u32, + equipment: &[Option; 6], + ) -> Vec { + const KIND: &str = "minecraft:armor_stand"; + let mut out = Vec::new(); + if let Some(a) = self.synced_attribute(KIND, "minecraft:movement_speed") { + instance( + &mut out, + a, + self.attribute_default(KIND, "minecraft:movement_speed"), + ); + } + let slots = horizon_client::entities::ArmorStand::SLOTS + .into_iter() + .zip(equipment.iter().map(|s| s.as_ref().map(|s| &s.item))); + self.equipment_attributes(KIND, |_| None, slots, &mut out); + self.generic_attributes_of(Target::Stand(id), &mut out); + out + } + + /// The instances a server player's tick creates, and + /// `ServerPlayer.updatePlayerAttributes`: a creative player reaches 0.5 + /// further to blocks and 2 to entities. Its client reads the reach from + /// these synced modifiers. (The crouching player's waypoint range is + /// server-side only: lan_waypoints.rs hides it.) + pub(super) fn update_player_attributes(&mut self) { + let creative = self.mode_id() == 1; + let Some(p) = self.player.as_mut() else { + return; + }; + // LivingEntity.aiStep's removeFrost reads the movement speed instance + // every tick, which creates it (and its first sync). + if let Some(key) = p.attributes.key("minecraft:movement_speed") { + let _ = p.attributes.get_instance(Some(key)); + } + for (attribute, id, amount) in [ + ( + "minecraft:block_interaction_range", + "minecraft:creative_mode_block_range", + 0.5, + ), + ( + "minecraft:entity_interaction_range", + "minecraft:creative_mode_entity_range", + 2.0, + ), + ] { + let Some(key) = p.attributes.key(attribute) else { + continue; + }; + // getAttribute creates the instance, a creative player's or not. + if !matches!(p.attributes.get_instance(Some(key.clone())), Ok(Some(_))) { + continue; + } + // addOrUpdateTransientModifier with the same modifier and + // removeModifier of a missing one leave the instance clean. + let has = p.attributes.has_modifier(Some(key), id).unwrap_or(false); + if has != creative { + let _ = p.set_modifier(attribute, id, amount, "add_value", creative); + } + } + } + + /// The instances `/attribute` created in an entity's generic map + /// replace the ones derived from its state. + fn generic_attributes_of(&self, t: Target, out: &mut Vec) { + let Some(map) = self.cmds.attributes.get(&t) else { + return; + }; + for s in generic_syncable(map) { + let (attribute, base) = (s.attribute, s.base); + *instance(out, attribute, base) = s; + } + } +} + +#[cfg(test)] +mod tests { + use super::slot_in_group; + + #[test] + fn slot_groups_cover_their_slots() { + assert!(slot_in_group("head", "head")); + assert!(slot_in_group("armor", "chest")); + assert!(!slot_in_group("armor", "mainhand")); + assert!(slot_in_group("hand", "offhand")); + assert!(slot_in_group("any", "feet")); + assert!(!slot_in_group("mainhand", "offhand")); + } +} diff --git a/crates/horizon-client/src/game/lan_chat.rs b/crates/horizon-client/src/game/lan_chat.rs new file mode 100644 index 000000000..f678480ab --- /dev/null +++ b/crates/horizon-client/src/game/lan_chat.rs @@ -0,0 +1,194 @@ +//! A guest's commands that carry chat: the unsigned and signed command +//! packets (ServerGamePacketListenerImpl.handleChatCommand and +//! handleSignedChatCommand), and the message commands whose text they sign +//! (SayCommand, EmoteCommands, MsgCommand), relayed by the hub as chat. +use super::{Game, lan::LanHost}; +use horizon_client::{ + command::{Arg, ArgType, NodeKind, ParseResults, vanilla}, + lan::{ + data::component_nbt, + hub::{HostEvent, chat::ChatMessage}, + }, + text::{self, Text}, +}; +use horizon_protocol::packets::play::{self, Clientbound as C}; + +/// The signing context of a command: each message argument as the chain +/// let it through, by argument name (CommandSigningContext.SignedArguments). +type Signed = Vec<(String, ChatMessage)>; + +impl Game<'_> { + /// CommandDispatcher.parse with the guest's permission level. + fn parse_as(&mut self, permission: u8, command: &str) -> ParseResults { + let level = std::mem::replace(&mut self.ui.registries.permission, permission); + let parse = vanilla::game_dispatcher().parse(command, &self.ui.registries); + self.ui.registries.permission = level; + parse + } + + /// SignableCommand.of: the command's message arguments, by name, with + /// their text, not past a redirect to the root (`execute ... run`). + fn signable_arguments(&mut self, permission: u8, command: &str) -> Vec<(String, String)> { + let parse = self.parse_as(permission, command); + let nodes = &vanilla::game_dispatcher().nodes; + let root = parse.context.root; + let mut out = Vec::new(); + let mut context = Some(&parse.context); + while let Some(c) = context { + for &(node, (start, end)) in &c.nodes { + if let NodeKind::Argument { + ty: ArgType::Message, + name, + } = &nodes[node].kind + && let Some(text) = command.get(start..end) + { + out.push((name.clone(), text.to_owned())); + } + } + context = c.child.as_deref().filter(|child| child.root != root); + } + out + } + + /// handleChatCommand (performUnsignedChatCommand): refused when the + /// server enforces secure chat and the command has a message to sign. + pub(super) fn guest_chat_command(&mut self, host: &mut LanHost, conn: u32, command: &str) { + let Some(i) = host.hub.guests.iter().position(|g| g.conn == conn) else { + return; + }; + if !host.hub.try_handle_chat(i, command, true) { + return; + } + let permission = host.hub.guests[i].permission; + if host.hub.enforces_secure_chat() + && !self.signable_arguments(permission, command).is_empty() + { + let name = host.hub.guests[i].name.clone(); + self.lan_log_event(HostEvent::Error(format!( + "Received unsigned command packet from {name}, but the command requires signable arguments: {command}" + ))); + let failure = Text::translate("chat.disabled.invalid_command_signature", &[]) + .with_color(text::RED); + host.hub.guests[i].send(&C::SystemChat(play::SystemChat { + content: component_nbt(&failure.component()), + overlay: false, + })); + return; + } + self.guest_command(host, conn, command, None); + } + + /// handleSignedChatCommand: the acknowledgements, then each message + /// argument through the sender's chain, then the command with them. + pub(super) fn guest_signed_command( + &mut self, + host: &mut LanHost, + conn: u32, + p: play::ChatCommandSigned, + ) { + let Some(i) = host.hub.guests.iter().position(|g| g.conn == conn) else { + return; + }; + let mut events = Vec::new(); + let mut signed = None; + if let Some(last_seen) = host.hub.apply_last_seen(i, &p.last_seen, &mut events) + && host.hub.try_handle_chat(i, &p.command, true) + { + let permission = host.hub.guests[i].permission; + let parsed = self.signable_arguments(permission, &p.command); + match host + .hub + .signed_arguments(i, &p, &parsed, &last_seen, &mut events) + { + Ok(arguments) => signed = Some(arguments), + Err(e) => host.hub.decode_failure(i, e, &mut events), + } + } + for event in events { + self.lan_log_event(event); + } + if let Some(signed) = signed { + self.guest_command(host, conn, &p.command, Some(signed)); + } + } + + /// SayCommand, EmoteCommands and MsgCommand for a guest: chat to the + /// players, signed when the guest signed it (a disguised chat after an + /// unsigned command packet). False when the command is another one, or + /// one this can't run (selectors other than `@s` and `@a`), for the + /// game's dispatcher to handle. + pub(super) fn guest_message_command( + &mut self, + host: &mut LanHost, + conn: u32, + command: &str, + signed: Option<&Signed>, + ) -> bool { + let Some(i) = host.hub.guests.iter().position(|g| g.conn == conn) else { + return false; + }; + let parse = self.parse_as(host.hub.guests[i].permission, command); + let Ok((context, id)) = parse.executable() else { + return false; + }; + let argument = if id == "me" { "action" } else { "message" }; + let (Some(Arg::Str(text)), true) = + (context.get(argument), matches!(id, "say" | "me" | "msg")) + else { + return false; + }; + let message = signed.and_then(|s| { + s.iter() + .find(|(n, _)| n == argument) + .map(|(_, m)| m.clone()) + }); + let mut events = Vec::new(); + if id == "msg" { + let Some(Arg::Entity(selector)) = context.get("targets") else { + return false; + }; + let raw = context + .args + .iter() + .rev() + .find(|a| a.name == "targets") + .and_then(|a| command.get(a.range.0..a.range.1)); + let guests = &host.hub.guests; + let placed = |k: &usize| guests[*k].placed; + let targets: Vec = match (raw, &selector.player_name, selector.uuid) { + (Some("@s"), ..) => vec![i], + (Some("@a"), ..) => (0..guests.len()).filter(placed).collect(), + (_, Some(name), _) => (0..guests.len()) + .filter(|&k| placed(&k) && guests[k].name.eq_ignore_ascii_case(name)) + .collect(), + (_, None, Some(uuid)) => (0..guests.len()) + .filter(|&k| placed(&k) && guests[k].uuid == uuid) + .collect(), + _ => return false, + }; + if targets.is_empty() { + let failure = Text::empty() + .append(Text::translate("argument.entity.notfound.player", &[])) + .with_color(text::RED); + host.hub.guests[i].send(&C::SystemChat(play::SystemChat { + content: component_nbt(&failure.component()), + overlay: false, + })); + return true; + } + host.hub.whisper(i, &targets, message, text); + } else { + let chat_type = if id == "say" { + "say_command" + } else { + "emote_command" + }; + host.hub + .broadcast_command_chat(i, chat_type, message, text, &mut events); + } + for event in events { + self.lan_log_event(event); + } + true + } +} diff --git a/crates/horizon-client/src/game/lan_crafting.rs b/crates/horizon-client/src/game/lan_crafting.rs index 9841fa879..de26e84aa 100644 --- a/crates/horizon-client/src/game/lan_crafting.rs +++ b/crates/horizon-client/src/game/lan_crafting.rs @@ -21,9 +21,10 @@ pub(super) struct GuestCrafting { inventory: Option>, /// The open crafting table: its window, state id and menu. table: Option<(i32, i32, Box)>, - /// The open furnace's window and menu (`AbstractFurnaceMenu`); its slots live in - /// the furnace block entity and the hub's container menu keeps the client in sync. - furnace: Option<(i32, Box)>, + /// The open furnace's or brewing stand's window and menu (`AbstractFurnaceMenu`, + /// `BrewingStandMenu`); its slots live in the block entity and the hub's + /// container menu keeps the client in sync. + machine: Option<(i32, Box)>, } /// ServerboundContainerClickPacket's input. @@ -84,10 +85,10 @@ fn place_back(menu: &mut Menu, env: &Env, stacks: Vec) { impl Game<'_> { fn menu_env(&self) -> Env<'_> { Env { - items: &self.res.items, - recipes: &self.res.recipes, - brewing: &self.res.stations.brewing, - banners: &self.res.stations.banners, + items: self.res.items(), + recipes: self.res.recipes(), + brewing: &self.res.stations().brewing, + banners: &self.res.stations().banners, trade: None, } } @@ -157,10 +158,11 @@ impl Game<'_> { true } - /// A click on the guest's open furnace, blast furnace or smoker: the host's - /// `AbstractFurnaceMenu` (fuel slot, take-only result, quick moves by recipe and - /// fuel) over the block entity's slots. False for other windows. - pub(super) fn lan_furnace_click( + /// A click on the guest's open furnace, blast furnace, smoker or brewing stand: + /// the host's `AbstractFurnaceMenu` (fuel slot, take-only result, quick moves by + /// recipe and fuel) or `BrewingStandMenu` (one bottle per slot, ingredient and + /// blaze powder slots) over the block entity's slots. False for other windows. + pub(super) fn lan_machine_click( &mut self, host: &mut LanHost, conn: u32, @@ -180,15 +182,18 @@ impl Game<'_> { else { return false; }; - let Some(BlockEntity::Furnace(f)) = self.world.block_entities.get(&pos) else { - return false; + let (kind, items) = match self.world.block_entities.get(&pos) { + Some(BlockEntity::Furnace(f)) => { + let recipe = match f.kind.recipe_type() { + "minecraft:blasting" => "blasting", + "minecraft:smoking" => "smoking", + _ => "smelting", + }; + (Kind::Furnace(recipe), f.items.to_vec()) + } + Some(BlockEntity::Brewing(b)) => (Kind::Brewing, b.items.to_vec()), + _ => return false, }; - let recipe = match f.kind.recipe_type() { - "minecraft:blasting" => "blasting", - "minecraft:smoking" => "smoking", - _ => "smelting", - }; - let items = f.items.clone(); if host.hub.guests[gi].spectator() { return true; } @@ -197,10 +202,10 @@ impl Game<'_> { .entry(conn) .or_default() .crafting - .furnace + .machine .take() .filter(|(window, _)| *window == p.container) - .map_or_else(|| Box::new(Menu::new(Kind::Furnace(recipe))), |(_, m)| m); + .map_or_else(|| Box::new(Menu::new(kind)), |(_, m)| m); let changed = self.guest_menu_op(host, gi, &mut menu, |menu, env| { for (k, s) in items.iter().enumerate() { menu.set_slot(env, k, s.clone()); @@ -213,18 +218,107 @@ impl Game<'_> { .filter(|&s| menu.slot(s) != before[s].as_ref()) .collect::>() }); - if let Some(BlockEntity::Furnace(f)) = self.world.block_entities.get_mut(&pos) { - for k in 0..3 { - f.items[k] = menu.slot(k).cloned(); + match self.world.block_entities.get_mut(&pos) { + Some(BlockEntity::Furnace(f)) => { + for k in 0..3 { + f.items[k] = menu.slot(k).cloned(); + } + // AbstractFurnaceBlockEntity.setItem: only a new input restarts cooking. + f.input_clicked(self.res.recipes(), items[0].clone()); } - // AbstractFurnaceBlockEntity.setItem: only a new input restarts cooking. - f.input_clicked(&self.res.recipes, items[0].clone()); + // BrewingStandBlockEntity.serverTick notices a new ingredient itself. + Some(BlockEntity::Brewing(b)) => { + for k in 0..5 { + b.items[k] = menu.slot(k).cloned(); + } + } + _ => {} } - host.play.entry(conn).or_default().crafting.furnace = Some((p.container, menu)); + host.play.entry(conn).or_default().crafting.machine = Some((p.container, menu)); host.hub.inventory_clicked(conn, p, &self.world, &changed); true } + /// ServerGamePacketListenerImpl.handlePlaceRecipe: the guest's recipe + /// book places a known recipe in their inventory grid or crafting table + /// (`ServerPlaceRecipe`), and answers with a ghost recipe when the items + /// are missing. + pub(super) fn lan_place_recipe( + &mut self, + host: &mut LanHost, + conn: u32, + p: &horizon_protocol::packets::recipes::PlaceRecipe, + ) { + use horizon_client::menu::place::PostPlace; + let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) else { + return; + }; + let g = &host.hub.guests[gi]; + if g.spectator() { + return; + } + let Some(id) = self + .res + .stations() + .recipe_index + .recipe_of_display(p.recipe) + .map(str::to_owned) + else { + return; + }; + // An unknown or impossible recipe is refused outright. + if !g.recipe_book.knows(&id) + || self + .res + .recipes() + .placement(&id) + .is_none_or(|placement| placement.impossible()) + { + return; + } + let creative = g.creative(); + let placed = if p.container == 0 { + let crafting = host.play.entry(conn).or_default(); + let mut menu = crafting + .crafting + .inventory + .take() + .unwrap_or_else(|| Box::new(Menu::new(Kind::Inventory))); + let grid = host.hub.guests[gi].craft_grid.clone(); + let placed = self.guest_menu_op(host, gi, &mut menu, |menu, env| { + for (k, s) in grid.into_iter().enumerate() { + menu.set_slot(env, 1 + k, s); + } + menu.place_recipe(env, &id, p.use_max, creative, None) + }); + let g = &mut host.hub.guests[gi]; + for k in 0..4 { + g.craft_grid[k] = menu.slot(1 + k).cloned(); + } + g.craft_result = menu.slot(0).cloned(); + host.play.entry(conn).or_default().crafting.inventory = Some(menu); + host.hub.send_inventory(gi); + placed + } else { + let Some((window, state, mut menu)) = host + .play + .get_mut(&conn) + .and_then(|play| play.crafting.table.take_if(|(w, _, _)| *w == p.container)) + else { + return; + }; + let placed = self.guest_menu_op(host, gi, &mut menu, |menu, env| { + menu.place_recipe(env, &id, p.use_max, creative, None) + }); + host.play.entry(conn).or_default().crafting.table = Some((window, state, menu)); + self.lan_send_table_content(host, conn); + placed + }; + if placed == PostPlace::Ghost { + host.hub.send_ghost_recipe(conn, p.container, &id); + } + } + /// The guest opened a crafting table (`CraftingTableBlock.getMenuProvider`). pub(super) fn lan_open_crafting(&mut self, host: &mut LanHost, conn: u32) { self.lan_crafting_closed(host, conn, None); diff --git a/crates/horizon-client/src/game/lan_entities.rs b/crates/horizon-client/src/game/lan_entities.rs new file mode 100644 index 000000000..020ec6adc --- /dev/null +++ b/crates/horizon-client/src/game/lan_entities.rs @@ -0,0 +1,455 @@ +//! The entities beyond mobs and items that a server tracks to its clients +//! (`ChunkMap.TrackedEntity`, `ServerEntity`): boats, minecarts, armor +//! stands, item frames, paintings and the entities Horizon keeps without +//! simulating them, each as vanilla pairs it (`getAddEntityPacket` and the +//! synched data that differs from the defaults), and who rides what. +use super::Game; +use horizon_client::lan::{ + entity::{EntityState, Riding}, + hub::Hub, +}; +use horizon_protocol::entity_data::{Value, serializer}; +use horizon_world::nbt::Tag; + +/// `ArmorStand`'s default poses (head, body, left arm, right arm, left leg, +/// right leg), DATA_HEAD_POSE to DATA_RIGHT_LEG_POSE (entries 16 to 21), +/// under their `Pose` keys. +const STAND_POSES: [(&str, [f32; 3]); 6] = [ + ("Head", [0.0, 0.0, 0.0]), + ("Body", [0.0, 0.0, 0.0]), + ("LeftArm", [-10.0, 0.0, -10.0]), + ("RightArm", [-15.0, 0.0, 10.0]), + ("LeftLeg", [-1.0, 0.0, -1.0]), + ("RightLeg", [1.0, 0.0, 1.0]), +]; + +fn state(id: u32, kind: &str, position: [f64; 3], yaw: f32, on_ground: bool) -> EntityState { + EntityState { + id: id as i32, + uuid: super::lan::entity_uuid(id), + kind: kind.to_owned(), + position, + yaw, + pitch: 0.0, + head_yaw: 0.0, + on_ground, + velocity: [0.0; 3], + data: 0, + meta: Vec::new(), + equipment: Vec::new(), + attributes: Vec::new(), + precise: false, + } +} + +/// `VehicleEntity`'s DATA_ID_HURT, DATA_ID_HURTDIR and DATA_ID_DAMAGE +/// (entries 8 to 10) when they differ from their defaults (0, 1, 0). +fn vehicle_meta(hurt_time: i32, hurt_dir: i32, damage: f32) -> Vec<(u8, i32, Value)> { + let mut meta = Vec::new(); + if hurt_time != 0 { + meta.push((8, serializer::INT, Value::Int(hurt_time))); + } + if hurt_dir != 1 { + meta.push((9, serializer::INT, Value::Int(hurt_dir))); + } + if damage != 0.0 { + meta.push((10, serializer::FLOAT, Value::Float(damage))); + } + meta +} + +/// An armor stand's pose from its compound (`ArmorStandPose`), each part +/// that differs from the default. +fn stand_poses(raw: Option<&Tag>) -> Vec<(u8, i32, Value)> { + let Some(pose) = raw.and_then(|t| t.get("Pose")) else { + return Vec::new(); + }; + let mut meta = Vec::new(); + for (i, (key, default)) in STAND_POSES.iter().enumerate() { + let Some(Tag::List(values)) = pose.get(key) else { + continue; + }; + let mut r = *default; + for (k, v) in values.iter().take(3).enumerate() { + if let Tag::Float(bits) = v { + r[k] = f32::from_bits(*bits); + } + } + if r != *default { + meta.push(( + 16 + i as u8, + serializer::ROTATIONS, + Value::Floats(r.to_vec()), + )); + } + } + meta +} + +impl Game<'_> { + /// Who rides what (`Entity.getPassengers`): mobs on mobs, and the + /// host in its boat or minecart. + pub(super) fn lan_riding(&self, host_entity: Option, riding: &mut Riding) { + riding.clear(); + for m in self.mobs.iter().filter(|m| !m.removed) { + if let Some(vehicle) = m.extra.mount { + riding.add(vehicle as i32, m.id as i32); + } + } + if let (Some(host), Some(vehicle)) = (host_entity, self.riding) { + riding.add(vehicle as i32, host); + } + } + + /// Boats, minecarts, armor stands, item frames, paintings and the + /// entities kept unsimulated, as guests track them. + pub(super) fn lan_object_states( + &self, + host_entity: Option, + hub: &mut Hub, + out: &mut Vec, + ) { + for b in &self.boats { + let p = b.position; + let mut e = state(b.id, &b.entity_type(), [p.x, p.y, p.z], b.yaw, b.on_ground); + e.velocity = [b.velocity.x, b.velocity.y, b.velocity.z]; + e.meta = vehicle_meta(b.hurt_time, b.hurt_dir, b.damage); + // AbstractBoat.DATA_ID_PADDLE_LEFT and DATA_ID_PADDLE_RIGHT. + for (k, paddling) in b.paddling.iter().enumerate() { + if *paddling { + e.meta + .push((11 + k as u8, serializer::BOOLEAN, Value::Bool(true))); + } + } + out.push(e); + } + for c in &self.carts { + let p = c.position; + let mut e = state(c.id, c.kind.id(), [p.x, p.y, p.z], c.yaw, c.on_ground()); + e.velocity = [c.velocity.x, c.velocity.y, c.velocity.z]; + e.meta = vehicle_meta(c.hurt_time, c.hurt_dir, c.damage); + match c.kind { + // MinecartFurnace.DATA_ID_FUEL. + super::minecarts::CartKind::Furnace if c.fuel > 0 => { + e.meta.push((13, serializer::BOOLEAN, Value::Bool(true))); + } + // MinecartCommandBlock.DATA_ID_COMMAND_NAME. + super::minecarts::CartKind::CommandBlock => { + if let Some(Tag::String(command)) = + self.persist.raw.get(&c.id).and_then(|t| t.get("Command")) + { + let command = command.to_string_lossy(); + if !command.is_empty() { + e.meta + .push((13, serializer::STRING, Value::String(command))); + } + } + } + _ => {} + } + out.push(e); + } + for a in &self.armor_stands { + let p = a.position; + let mut e = state( + a.id, + "minecraft:armor_stand", + [p.x, p.y, p.z], + a.yaw, + a.on_ground, + ); + e.head_yaw = a.yaw; + e.velocity = [a.velocity.x, a.velocity.y, a.velocity.z]; + if a.fire_ticks > 0 { + e.meta.push((0, serializer::BYTE, Value::Byte(1))); + } + // LivingEntity.DATA_HEALTH_ID: 1 until the stand's max health is set. + e.meta.push((9, serializer::FLOAT, Value::Float(a.health))); + // ArmorStand.DATA_CLIENT_FLAGS: small 1, arms 4, no base plate 8, marker 16. + let flags = i8::from(a.small) + | i8::from(a.show_arms) << 2 + | i8::from(a.no_base_plate) << 3 + | i8::from(a.marker) << 4; + if flags != 0 { + e.meta.push((15, serializer::BYTE, Value::Byte(flags))); + } + e.meta.extend(stand_poses(self.persist.raw.get(&a.id))); + // Stored main hand, off hand, feet, legs, chest, head: the wire's slots 0 to 5. + for (slot, item) in a.equipment.iter().enumerate() { + if item.is_some() { + e.equipment.push((slot as u8, hub.stack(item))); + } + } + e.attributes = self.stand_attributes(a.id, &a.equipment); + out.push(e); + } + for f in &self.item_frames { + // ItemFrame.getAddEntityPacket: the block it hangs in and its direction. + let mut e = state(f.id, f.kind(), f.pos.map(f64::from), 0.0, false); + e.pitch = match f.facing { + 0 => 90.0, + 1 => -90.0, + _ => 0.0, + }; + e.yaw = match f.facing { + 2 => 180.0, + 4 => 90.0, + 5 => 270.0, + _ => 0.0, + }; + e.data = i32::from(f.facing); + // HangingEntity.DATA_DIRECTION, south by default. + if f.facing != 3 { + e.meta + .push((8, serializer::DIRECTION, Value::Id(i32::from(f.facing)))); + } + if f.item.is_some() { + e.meta + .push((9, serializer::ITEM_STACK, Value::Item(hub.stack(&f.item)))); + } + if f.rotation != 0 { + e.meta + .push((10, serializer::INT, Value::Int(i32::from(f.rotation)))); + } + out.push(e); + } + for p in &self.paintings { + let mut e = state(p.id, "minecraft:painting", p.pos.map(f64::from), 0.0, false); + // Direction.get2DDataValue (south 0, west 1, north 2, east 3) to its + // 3D data value and yaw. + let facing = [3, 4, 2, 5][usize::from(p.facing & 3)]; + e.yaw = f32::from(p.facing & 3) * 90.0; + e.data = facing; + if facing != 3 { + e.meta.push((8, serializer::DIRECTION, Value::Id(facing))); + } + // Painting.DATA_PAINTING_VARIANT_ID, a registry holder (id + 1), + // set unless it is the registry's first variant (VariantUtils.getAny). + if let Some(id) = hub.registry_id("minecraft:painting_variant", &p.variant) + && id != 0 + { + e.meta + .push((9, serializer::PAINTING_VARIANT, Value::Id(id + 1))); + } + out.push(e); + } + self.lan_projectile_states(host_entity, hub, out); + for o in &self.others { + let p = o.position; + let mut e = state(o.id, &o.kind, [p.x, p.y, p.z], o.yaw, false); + e.pitch = o.pitch; + e.head_yaw = o.yaw; + out.push(e); + } + } +} + +impl Game<'_> { + /// Gives the entities Horizon keeps without an id one (`Entity.id`, from + /// the same counter as the rest) before they are first tracked. + pub(super) fn lan_assign_ids(&mut self) { + let next = &mut self.next_entity; + let mut give = |id: &mut u32| { + if *id == 0 { + *next += 1; + *id = *next; + } + }; + self.tnts.iter_mut().for_each(|t| give(&mut t.id)); + self.falling.iter_mut().for_each(|f| give(&mut f.id)); + self.creatures + .projectiles + .iter_mut() + .for_each(|p| give(&mut p.id)); + self.tridents.iter_mut().for_each(|t| give(&mut t.id)); + self.rockets.iter_mut().for_each(|r| give(&mut r.id)); + self.crystals.iter_mut().for_each(|c| give(&mut c.id)); + } + + /// Primed TNT, falling blocks, projectiles, tridents, firework rockets and + /// end crystals, as guests track them. + fn lan_projectile_states( + &self, + host_entity: Option, + hub: &mut Hub, + out: &mut Vec, + ) { + let owner = |who: Option| match who { + Some(horizon_client::mobs::Who::Mob(id)) => id as i32, + Some(horizon_client::mobs::Who::Player) => host_entity.unwrap_or(0), + None => 0, + }; + let at = |p: horizon_core::geometry::Vec3| [p.x, p.y, p.z]; + for t in self.tnts.iter().filter(|t| t.id != 0) { + let mut e = state(t.id, "minecraft:tnt", at(t.position), 0.0, t.on_ground); + e.velocity = at(t.velocity); + // PrimedTnt.DATA_FUSE_ID, 80 by default; requiresPrecisePosition + // while it touches a block. + if t.fuse != 80 { + e.meta.push((8, serializer::INT, Value::Int(t.fuse))); + } + e.precise = t.on_ground; + out.push(e); + } + for f in self.falling.iter().filter(|f| f.id != 0) { + let mut e = state(f.id, "minecraft:falling_block", at(f.position), 0.0, false); + e.velocity = at(f.velocity); + // getAddEntityPacket: the block state; DATA_START_POS. + e.data = f.state.0 as i32; + if f.no_gravity { + e.meta.push((5, serializer::BOOLEAN, Value::Bool(true))); + } + let start = [f.start_pos.x, f.start_pos.y, f.start_pos.z]; + if start != [0; 3] { + e.meta.push((8, serializer::BLOCK_POS, Value::Pos(start))); + } + out.push(e); + } + for p in self.creatures.projectiles.iter().filter(|p| p.id != 0) { + use horizon_client::projectiles::ProjectileKind as K; + let kind = match &p.kind { + K::Arrow { .. } => "minecraft:arrow", + K::Snowball => "minecraft:snowball", + K::Egg => "minecraft:egg", + K::EnderPearl => "minecraft:ender_pearl", + K::SmallFireball => "minecraft:small_fireball", + K::LargeFireball { .. } => "minecraft:fireball", + K::LlamaSpit => "minecraft:llama_spit", + K::WindCharge => "minecraft:wind_charge", + K::BreezeWindCharge => "minecraft:breeze_wind_charge", + K::ShulkerBullet { .. } => "minecraft:shulker_bullet", + K::WitherSkull { .. } => "minecraft:wither_skull", + K::Potion { .. } => "minecraft:splash_potion", + }; + let mut e = state(p.id, kind, at(p.position), p.yaw, false); + e.pitch = p.pitch; + e.velocity = at(p.velocity); + // Projectile.getAddEntityPacket: the owner's id. + e.data = match p.guest.and_then(|conn| hub.guest(conn)) { + Some(g) => g.entity_id, + None => owner(p.owner), + }; + match &p.kind { + K::Arrow { critical, .. } => { + let a = p.arrow.as_deref(); + e.meta.extend(arrow_meta( + *critical, + a.is_some_and(|a| a.no_physics), + a.map_or(0, |a| a.pierce), + a.is_some_and(|a| a.in_ground) || p.stuck, + )); + } + // WitherSkull.DATA_DANGEROUS. + K::WitherSkull { dangerous: true } => { + e.meta.push((8, serializer::BOOLEAN, Value::Bool(true))); + } + _ => {} + } + out.push(e); + } + for t in self.tridents.iter().filter(|t| t.id != 0) { + let mut e = state(t.id, "minecraft:trident", at(t.position), t.yaw, false); + e.pitch = t.pitch; + e.velocity = at(t.velocity); + e.meta = arrow_meta(false, t.no_physics, 0, t.in_ground); + // ThrownTrident.ID_LOYALTY and ID_FOIL. + if t.loyalty != 0 { + e.meta + .push((11, serializer::BYTE, Value::Byte(t.loyalty as i8))); + } + if t.stack.has_any_enchantments() { + e.meta.push((12, serializer::BOOLEAN, Value::Bool(true))); + } + out.push(e); + } + for r in self.rockets.iter().filter(|r| r.id != 0) { + let mut e = state( + r.id, + "minecraft:firework_rocket", + at(r.position), + 0.0, + false, + ); + e.velocity = at(r.velocity); + // DATA_ID_FIREWORKS_ITEM once set from a stack, DATA_ATTACHED_TO_TARGET, + // DATA_SHOT_AT_ANGLE. + let plain = &*r.item.item == "minecraft:firework_rocket" + && r.item.count == 1 + && r.item.extra.is_none(); + if !plain { + e.meta.push(( + 8, + serializer::ITEM_STACK, + Value::Item(hub.stack(&Some(r.item.clone()))), + )); + } + if r.attached + && let Some(host) = host_entity + { + e.meta + .push((9, serializer::OPTIONAL_UNSIGNED_INT, Value::Id(host + 1))); + } + if r.angled { + e.meta.push((10, serializer::BOOLEAN, Value::Bool(true))); + } + out.push(e); + } + for c in self.crystals.iter().filter(|c| c.id != 0) { + let mut e = state(c.id, "minecraft:end_crystal", at(c.position), 0.0, false); + // EndCrystal.DATA_BEAM_TARGET and DATA_SHOW_BOTTOM (true by default). + if c.beam.is_some() { + e.meta + .push((8, serializer::OPTIONAL_BLOCK_POS, Value::OptPos(c.beam))); + } + if !c.show_bottom { + e.meta.push((9, serializer::BOOLEAN, Value::Bool(false))); + } + out.push(e); + } + } +} + +/// `AbstractArrow`'s ID_FLAGS (critical 1, no physics 2), PIERCE_LEVEL and +/// IN_GROUND, when they differ from the defaults. +fn arrow_meta( + critical: bool, + no_physics: bool, + pierce: u8, + in_ground: bool, +) -> Vec<(u8, i32, Value)> { + let mut meta = Vec::new(); + let flags = i8::from(critical) | i8::from(no_physics) << 1; + if flags != 0 { + meta.push((8, serializer::BYTE, Value::Byte(flags))); + } + if pierce != 0 { + meta.push((9, serializer::BYTE, Value::Byte(pierce as i8))); + } + if in_ground { + meta.push((10, serializer::BOOLEAN, Value::Bool(true))); + } + meta +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn armor_stand_poses_send_only_what_differs() { + let tag = + horizon_world::nbt::parse_snbt("{Pose:{Head:[10f,20f,0f],LeftArm:[-10f,0f,-10f]}}") + .unwrap() + .0; + let meta = stand_poses(Some(&tag)); + assert_eq!( + meta, + vec![( + 16, + serializer::ROTATIONS, + Value::Floats(vec![10.0, 20.0, 0.0]) + )] + ); + assert!(stand_poses(None).is_empty()); + } +} diff --git a/crates/horizon-client/src/game/lan_mobs.rs b/crates/horizon-client/src/game/lan_mobs.rs index 7cef7cda6..0ee991737 100644 --- a/crates/horizon-client/src/game/lan_mobs.rs +++ b/crates/horizon-client/src/game/lan_mobs.rs @@ -92,7 +92,7 @@ impl Game<'_> { .hub .guests .iter() - .filter(|g| g.placed && g.loaded && !g.dead) + .filter(|g| host.hub.here(g) && g.loaded && !g.dead) .map(|g| { let moved = [0, 1, 2].map(|k| g.position[k] - g.previous_position[k]); (g.conn, g.uuid, moved) @@ -107,6 +107,7 @@ impl Game<'_> { v.velocity = Vec3::new(moved[0], moved[1], moved[2]); v.uuid = horizon_client::lan::playerdata::uuid_ints(uuid); v.allies = Default::default(); + v.remote = true; views.push((conn, v)); } } @@ -130,6 +131,26 @@ impl Game<'_> { self.lan = Some(host); } + /// The guests' boxes for the blocks entities trigger (pressure plates, + /// tripwires, detector rails): `Entity.checkInsideBlocks` and + /// `Level.getEntities` see every player in the level, but not spectators + /// (`Entity.isIgnoringBlockTriggers`). + pub(super) fn lan_block_trigger_boxes(&self) -> Vec<([f64; 3], [f64; 3])> { + let Some(host) = self.lan.as_ref() else { + return Vec::new(); + }; + host.hub + .guests + .iter() + .filter(|g| host.hub.here(g) && g.loaded && !g.dead && !g.spectator()) + .map(|g| { + let [x, y, z] = g.position; + let height = if g.sneaking { 1.5 } else { 1.8 }; + ([x - 0.3, y, z - 0.3], [x + 0.3, y + height, z + 0.3]) + }) + .collect() + } + /// The guests a projectile can hit, under their entity ids. pub(super) fn lan_projectile_targets(&self) -> Vec<(Who, Aabb)> { let Some(host) = self.lan.as_ref() else { @@ -138,7 +159,7 @@ impl Game<'_> { host.hub .guests .iter() - .filter(|g| g.placed && g.loaded && !g.dead && !g.spectator()) + .filter(|g| host.hub.here(g) && g.loaded && !g.dead && !g.spectator()) .map(|g| (Who::Mob(g.entity_id as u32), guest_bounds(g))) .collect() } @@ -209,9 +230,12 @@ pub(super) struct Present { impl Game<'_> { /// Every guest in the world (`ServerLevel.getPlayers` past the host). pub(super) fn lan_present(&self) -> Vec { - let guests = self.lan.iter().flat_map(|h| &h.hub.guests); + let guests = self + .lan + .iter() + .flat_map(|h| h.hub.guests.iter().filter(|g| h.hub.here(g))); guests - .filter(|g| g.placed && g.loaded) + .filter(|g| g.loaded) .map(|g| { let [x, y, z] = g.position; Present { @@ -271,6 +295,7 @@ mod tests { hero: false, uuid: [0; 4], allies: Default::default(), + remote: false, } } diff --git a/crates/horizon-client/src/game/lan_pvp.rs b/crates/horizon-client/src/game/lan_pvp.rs index dc91744de..dba2ed8e6 100644 --- a/crates/horizon-client/src/game/lan_pvp.rs +++ b/crates/horizon-client/src/game/lan_pvp.rs @@ -19,6 +19,9 @@ pub(super) struct Strike { scale: f32, crit: bool, from: [f64; 3], + sweep: Option<[f64; 3]>, + /// The attacker's team, for the victim's `Player.canHarmPlayer`. + team: Option, } impl Game<'_> { @@ -31,7 +34,7 @@ impl Game<'_> { host.hub .guests .iter() - .filter(|g| g.placed && g.loaded && !g.dead && !g.spectator()) + .filter(|g| host.hub.here(g) && g.loaded && !g.dead && !g.spectator()) .filter_map(|g| Some((g.conn, ray_box(eye, dir, reach, guest_bounds(g))?))) .min_by(|a, b| a.1.total_cmp(&b.1)) } @@ -63,7 +66,7 @@ impl Game<'_> { host.hub .guests .iter() - .find(|g| g.entity_id == id && g.conn != conn && g.placed && !g.dead) + .find(|g| g.entity_id == id && g.conn != conn && host.hub.here(g) && !g.dead) .map(|g| Victim::Guest(g.conn)) } @@ -73,6 +76,7 @@ impl Game<'_> { let p = self.player.as_ref()?; let from = [p.position.x, p.position.y, p.position.z]; let (amount, scale, crit) = self.melee_damage("minecraft:player"); + let sweep = self.sweep_attack_position(scale, crit); self.attack_ticks = 0; if let Some(p) = self.player.as_mut() && !self.creative @@ -85,12 +89,25 @@ impl Game<'_> { scale, crit, from, + sweep, + team: self.player_team().cloned(), }) } /// The hit landing on the victim, then the attacker's attack sound. pub(super) fn pvp_land(&mut self, host: &mut LanHost, victim: Victim, s: &Strike) { + // ServerPlayer.canHarmPlayer: the pvp game rule, then the victim's + // team (Player.canHarmPlayer: allies only with friendly fire on). + let team = match victim { + Victim::Host => self.player_team(), + Victim::Guest(conn) => host + .hub + .guest(conn) + .and_then(|g| self.score.board.players_team(&g.name)), + }; + let harm = horizon_client::scoreboard::teams_allow_damage(team, s.team.as_ref()); let hurt = self.ui.rules.bool("pvp") + && harm && match victim { Victim::Host => self.mob_hits_player(s.amount, s.from, "minecraft:player_attack"), Victim::Guest(conn) => self @@ -105,6 +122,10 @@ impl Game<'_> { (true, false, true) => "minecraft:entity.player.attack.strong", (true, false, false) => "minecraft:entity.player.attack.weak", }; - self.audio.play(sound, Some(s.from), 1.0, 1.0); + if let Some(at) = s.sweep.filter(|_| hurt) { + self.sweep_attack(at, Some(s.from)); + } else { + self.audio.play(sound, Some(s.from), 1.0, 1.0); + } } } diff --git a/crates/horizon-client/src/game/lan_screen.rs b/crates/horizon-client/src/game/lan_screen.rs index 6be6fc7fa..335b5fbae 100644 --- a/crates/horizon-client/src/game/lan_screen.rs +++ b/crates/horizon-client/src/game/lan_screen.rs @@ -596,11 +596,11 @@ impl Game<'_> { View::Restrictions => self.draw_restrictions(batch), View::GameRules => { if let Some(rules) = o.rules.as_mut() { - rules.draw(batch, &self.atlas, self.cursor); + rules.draw(batch, &self.gui.atlas, self.cursor); } else { ui::menu_background( batch, - &self.atlas, + &self.gui.atlas, 0.0, 0.0, batch.width, @@ -614,7 +614,7 @@ impl Game<'_> { } fn draw_lock_question(&self, o: &WorldOptions, batch: &mut HudBatch) { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let (w, h) = (batch.width, batch.height); // Screen.extractTransparentBackground. batch.fill_gradient( @@ -656,7 +656,7 @@ impl Game<'_> { } fn draw_restrictions(&self, batch: &mut HudBatch) { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let (w, h) = (batch.width, batch.height); ui::menu_background(batch, atlas, 0.0, 0.0, w, h, true); let title = Text::translate("restrictions_screen.title", &[]); @@ -687,7 +687,7 @@ impl Game<'_> { } fn draw_options_view(&self, o: &mut WorldOptions, batch: &mut HudBatch, cursor: (f32, f32)) { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let (w, h) = (batch.width, batch.height); o.scroll = o.scroll.clamp(0.0, o.scroll_limit(h)); // extractMenuBackground: the in-world background, the list @@ -810,7 +810,7 @@ impl Game<'_> { match o.view { View::Options => {} View::GameRules => { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let exit = o.rules.as_mut().and_then(|rules| { if rules.clicks_button(atlas, self.cursor, (w, h)) { self.audio.play_button_click(); @@ -824,7 +824,7 @@ impl Game<'_> { return; } View::LockQuestion => { - let p = Popup::lock_question(&self.atlas.font, o.difficulty, (w, h)); + let p = Popup::lock_question(&self.gui.atlas.font, o.difficulty, (w, h)); let hit = p .buttons .iter() @@ -842,7 +842,8 @@ impl Game<'_> { return; } View::Restrictions => { - let r = Restrictions::new(&self.atlas.font, self.settings.chat_visibility, (w, h)); + let r = + Restrictions::new(&self.gui.atlas.font, self.settings.chat_visibility, (w, h)); let in_body = self.cursor.1 >= r.area.0 && self.cursor.1 < r.area.1; let action = r.action.is_some_and(|(ax, ay)| { in_body && ui::hovered(self.cursor, ax, ay, 200.0, 20.0) diff --git a/crates/horizon-client/src/game/lan_trading.rs b/crates/horizon-client/src/game/lan_trading.rs index d6bfc4141..c7f8a94a6 100644 --- a/crates/horizon-client/src/game/lan_trading.rs +++ b/crates/horizon-client/src/game/lan_trading.rs @@ -133,7 +133,7 @@ impl Game<'_> { if let Some(name) = &state.name { return name.clone(); } - let e = &self.res.stations.enchantments; + let e = &self.res.stations().enchantments; let profession = state.session.profession.trim_start_matches("minecraft:"); if state.session.villager { e.translate(&format!("entity.minecraft.villager.{profession}")) @@ -245,15 +245,15 @@ impl Game<'_> { next_id: std::cell::Cell::new(next_map), }; let trade_env = merchant::TradeEnv { - trades: &self.res.stations.trades, - enchantments: &self.res.stations.enchantments, + trades: &self.res.stations().trades, + enchantments: &self.res.stations().enchantments, maps: Some(&maps), }; let env = Env { - items: &self.res.items, - recipes: &self.res.recipes, - brewing: &self.res.stations.brewing, - banners: &self.res.stations.banners, + items: self.res.items(), + recipes: self.res.recipes(), + brewing: &self.res.stations().brewing, + banners: &self.res.stations().banners, trade: Some(&trade_env), }; let mut menu = Menu::new(Kind::Merchant); @@ -372,7 +372,7 @@ impl Game<'_> { .hub .guests .iter() - .find(|g| g.conn == conn) + .find(|g| g.conn == conn && host.hub.here(g)) .map(|g| uuid_words(g.uuid)) else { continue; diff --git a/crates/horizon-client/src/game/lan_waypoints.rs b/crates/horizon-client/src/game/lan_waypoints.rs new file mode 100644 index 000000000..0d0ab69e4 --- /dev/null +++ b/crates/horizon-client/src/game/lan_waypoints.rs @@ -0,0 +1,95 @@ +//! The locator bar of the players on a served world (lan/waypoints.rs): what +//! each player looks like to the waypoint manager this tick. +use super::{Game, lan::LanHost}; +use horizon_client::{ + items::ItemStack, + lan::{ + hub::HostState, + waypoints::{self, HIDING_HEADS, Icon, PLAYER_RANGE}, + }, +}; + +/// `waypoint_transmit_range`: hidden by crouching, invisibility or a head worn. +fn transmit_range(crouching: bool, invisible: bool, head: Option<&ItemStack>) -> f64 { + let hiding_head = head.is_some_and(|s| HIDING_HEADS.contains(&&*s.item)); + if crouching || invisible || hiding_head { + 0.0 + } else { + PLAYER_RANGE + } +} + +impl Game<'_> { + /// The player's team and its colour, as the locator bar shows it. + fn waypoint_team(&self, name: &str) -> Option<(String, Option)> { + let team = self.score.board.players_team(name)?; + let color = team.color.as_deref().and_then(waypoints::team_rgb); + Some((team.name.clone(), color)) + } + + pub(super) fn lan_update_waypoints(&mut self, host: &mut LanHost, state: Option<&HostState>) { + let mut players = Vec::new(); + if let Some(h) = state { + let icon = self + .cmds + .waypoint_icons + .get(&super::commands::Target::Player) + .map(|i| Icon { + style: i.style.clone(), + color: i.color, + }) + .unwrap_or_default(); + players.push(waypoints::Player { + uuid: h.uuid, + conn: None, + position: h.position, + spectator: self.spectator(), + transmit_range: transmit_range( + h.sneaking, + self.potions.effects.get("invisibility").is_some(), + h.equipment[5].as_ref(), + ), + receive_range: PLAYER_RANGE, + first_tick: false, + view_distance: host.hub.config.view_distance, + team: self.waypoint_team(&h.name), + icon, + }); + } + let mut respawned = Vec::new(); + for g in host.hub.guests.iter().filter(|g| g.placed) { + // PlayerList.respawn makes a new ServerPlayer. + if g.dead { + host.waypoint_dead.insert(g.uuid); + } else if host.waypoint_dead.remove(&g.uuid) { + respawned.push(g.uuid); + } + // Player.updatePlayerPose: the shift key crouches unless flying. + let crouching = g.sneaking && !g.flying && !g.fall_flying && !g.sleeping; + players.push(waypoints::Player { + uuid: g.uuid, + conn: Some(g.conn), + position: g.position, + spectator: g.spectator(), + transmit_range: transmit_range( + crouching, + g.effects.get("invisibility").is_some(), + g.armor[0].as_ref(), + ), + receive_range: PLAYER_RANGE, + first_tick: g.ticks == 0, + view_distance: g.view_distance, + team: self.waypoint_team(&g.name), + icon: Icon::default(), + }); + } + host.waypoint_dead + .retain(|uuid| players.iter().any(|p| p.uuid == *uuid)); + let locator_bar = self.ui.rules.bool("locator_bar"); + for (conn, packet) in host.waypoints.tick(&players, &respawned, locator_bar) { + if let Some(g) = host.hub.guest(conn) { + g.send(&packet); + } + } + } +} diff --git a/crates/horizon-client/src/game/leads.rs b/crates/horizon-client/src/game/leads.rs index 3b8fc29c4..ae5d87fff 100644 --- a/crates/horizon-client/src/game/leads.rs +++ b/crates/horizon-client/src/game/leads.rs @@ -220,11 +220,11 @@ impl Game<'_> { }; let name = self .res - .facts + .facts() .get(self.world.block(hit.pos[0], hit.pos[1], hit.pos[2])); if !self .res - .items + .items() .block_tags .get("minecraft:fences") .is_some_and(|s| s.contains(&*name.name)) @@ -283,7 +283,7 @@ impl Game<'_> { } /// `LeashFeatureRenderer` for every lead and `LeashKnotRenderer` for the knots. pub(super) fn leash_geometry(&self, camera: glam::DVec3, alpha: f64, out: &mut Vec) { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let white = atlas.font.white(); let uv = [ [white.u0, white.v1], diff --git a/crates/horizon-client/src/game/level_particles.rs b/crates/horizon-client/src/game/level_particles.rs index 4516ce7dd..47d06254d 100644 --- a/crates/horizon-client/src/game/level_particles.rs +++ b/crates/horizon-client/src/game/level_particles.rs @@ -1,6 +1,7 @@ //! Level events whose client handler (`LevelEventHandler`) spawns particles -//! on a block: dragon egg teleportation (2015), electric sparks (3002), -//! waxing (3003), wax off (3004) and scraping (3005). +//! on a block: dragon egg teleportation (2015), a consumable's teleport +//! (2017), electric sparks (3002), waxing (3003), wax off (3004) and +//! scraping (3005). use super::Game; use horizon_client::particles::{self, ParticleOptions, ParticleType}; use horizon_core::position::BlockPos; @@ -9,7 +10,12 @@ use horizon_world::level::LevelEvent; impl Game<'_> { pub(super) fn dragon_egg_particles(&mut self, pos: [i32; 3], data: i32) { self.particles - .block_teleportation(pos, data, self.settings.particles); + .block_teleportation(pos, data, [16, 8, 16], self.settings.particles); + } + /// Event 2017: `TeleportRandomlyConsumeEffect`'s directional particles. + pub(super) fn consume_teleport_particles(&mut self, pos: [i32; 3], data: i32) { + self.particles + .block_teleportation(pos, data, [127, 127, 127], self.settings.particles); } /// `LevelEventHandler.levelEvent` 3002 to 3005, drawing from the client /// level's random. diff --git a/crates/horizon-client/src/game/levels.rs b/crates/horizon-client/src/game/levels.rs new file mode 100644 index 000000000..3225408f5 --- /dev/null +++ b/crates/horizon-client/src/game/levels.rs @@ -0,0 +1,385 @@ +//! A server's levels, one per dimension (`MinecraftServer.levels`). +//! +//! The game's tick code reads one level: the one in place, held in `Game`'s +//! own fields (the world, its streaming and save, block ticks, entities). +//! A server (the dedicated server and single player's server thread) keeps +//! the other dimensions' levels here and ticks each one that has players by +//! swapping it into place (`enter_level`), the way `as_guest` swaps a player +//! in, so the tick code runs unchanged, one level at a time. The in-frame +//! game holds one level, which follows its player (`enter_dimension`). +//! +//! Shared by every level, as vanilla's derived levels share the +//! Overworld's level data: the game time, the day time and the weather, the +//! game rules, the scoreboard, the maps and the entities waiting to arrive +//! through a portal. + +use super::{ + Dim, ExperienceOrb, FallingBlock, Game, ItemEntity, Mob, boats, commands, + creaking::CreakingState, dragon, fireworks, lan::LanHost, lightning, machines, minecarts, + ominous_item_spawner, persist, raids::Raiding, sculk::SculkState, tnt, trident, +}; +use horizon_client::{ + Result, block_entity_anim::BlockEntityAnims, random_tick_sections::RandomTickSections, + save::SaveDir, streaming::Streaming, tick_chunk_membership::TickChunkMembership, + ticket_levels::ChunkTickets, world::World, +}; +use horizon_world::level::{LevelEvent, Ticks}; +use std::{collections::HashMap, sync::Arc, time::Instant}; + +/// Server ticks a level other than the Overworld stays loaded with no player +/// in it, before it is saved and closed (vanilla unloads a level's chunks +/// once no ticket holds them; the Overworld stays loaded). +const IDLE_TICKS: u32 = 300; + +/// A level that is not in place: every per-level field of `Game`. +pub(super) struct Parked { + world: World, + streaming: Streaming, + save: Arc, + block_ticks: Ticks, + tick_chunks: TickChunkMembership, + tickets: ChunkTickets, + random_tick_sections: RandomTickSections, + level_events: Vec, + falling: Vec, + paintings: Vec, + item_frames: Vec, + armor_stands: Vec, + others: Vec, + boats: Vec, + carts: Vec, + jukebox_sounds: HashMap<(i32, i32, i32), u64>, + bolts: Vec, + crystals: Vec, + dragon: Option, + dragon_fireballs: Vec, + breath_clouds: Vec, + tridents: Vec, + rockets: Vec, + ominous_spawners: Vec, + tnts: Vec, + hoppers: HashMap<(i32, i32, i32), machines::HopperState>, + mobs: Vec, + projectiles: Vec, + dropped: Vec, + orbs: Vec, + persist: persist::EntityMemory, + lectern_pulses: Vec<((i32, i32, i32), i32)>, + lid_openers: HashMap<(i32, i32, i32), i32>, + lan_events: Vec<(i32, [i32; 3], i32, Option)>, + lan_block_events: Vec<([i32; 3], i32, i32, u32)>, + lan_entity_events: Vec<(i32, i8, [f64; 3])>, + /// Only the per-level parts of these are used (`swap_level`). + anims: BlockEntityAnims, + raiding: Raiding, + sculk: SculkState, + creaking: CreakingState, + /// Server ticks since a player was last in the level. + idle: u32, +} + +/// Which passes of the server tick the level in place runs: the first +/// reads the network (`Hub::poll`), the last ends the tick for every player +/// (`Hub::end_tick`). A tick with one level is both. +#[derive(Clone, Copy)] +pub(super) struct Pass { + pub first: bool, + pub last: bool, +} + +impl Default for Pass { + fn default() -> Self { + Self { + first: true, + last: true, + } + } +} + +/// The levels not in place, by dimension. +#[derive(Default)] +pub(super) struct Levels { + parked: HashMap, +} + +impl Game<'_> { + /// A new level for `dim`, opened from the save (`MinecraftServer.createLevels`). + fn open_level(&self, dim: Dim) -> Result { + let (streaming, save) = + self.make_streaming(&self.world_root.clone(), dim, self.level.seed)?; + let mut world = World::new(self.res.facts().air, self.level.seed); + world.fast_lava = dim == Dim::Nether; + Ok(Parked { + world, + streaming, + save, + block_ticks: Ticks::new(self.level.seed as i32), + tick_chunks: Default::default(), + tickets: Default::default(), + random_tick_sections: Default::default(), + level_events: Vec::new(), + falling: Vec::new(), + paintings: Vec::new(), + item_frames: Vec::new(), + armor_stands: Vec::new(), + others: Vec::new(), + boats: Vec::new(), + carts: Vec::new(), + jukebox_sounds: HashMap::new(), + bolts: Vec::new(), + crystals: Vec::new(), + dragon: None, + dragon_fireballs: Vec::new(), + breath_clouds: Vec::new(), + tridents: Vec::new(), + rockets: Vec::new(), + ominous_spawners: Vec::new(), + tnts: Vec::new(), + hoppers: HashMap::new(), + mobs: Vec::new(), + projectiles: Vec::new(), + dropped: Vec::new(), + orbs: Vec::new(), + persist: Default::default(), + lectern_pulses: Vec::new(), + lid_openers: HashMap::new(), + lan_events: Vec::new(), + lan_block_events: Vec::new(), + lan_entity_events: Vec::new(), + anims: Default::default(), + raiding: Default::default(), + sculk: Default::default(), + creaking: Default::default(), + idle: 0, + }) + } + + /// Exchange the level in place with `p`. + fn swap_level(&mut self, p: &mut Parked) { + use std::mem::swap; + swap(&mut self.world, &mut p.world); + swap(&mut self.streaming, &mut p.streaming); + swap(&mut self.save, &mut p.save); + swap(&mut self.block_ticks, &mut p.block_ticks); + swap(&mut self.tick_chunks, &mut p.tick_chunks); + swap(&mut self.tickets, &mut p.tickets); + swap(&mut self.random_tick_sections, &mut p.random_tick_sections); + swap(&mut self.level_events, &mut p.level_events); + swap(&mut self.falling, &mut p.falling); + swap(&mut self.paintings, &mut p.paintings); + swap(&mut self.item_frames, &mut p.item_frames); + swap(&mut self.armor_stands, &mut p.armor_stands); + swap(&mut self.others, &mut p.others); + swap(&mut self.boats, &mut p.boats); + swap(&mut self.carts, &mut p.carts); + swap(&mut self.jukebox_sounds, &mut p.jukebox_sounds); + swap(&mut self.bolts, &mut p.bolts); + swap(&mut self.crystals, &mut p.crystals); + swap(&mut self.dragon, &mut p.dragon); + swap(&mut self.dragon_fireballs, &mut p.dragon_fireballs); + swap(&mut self.breath_clouds, &mut p.breath_clouds); + swap(&mut self.tridents, &mut p.tridents); + swap(&mut self.rockets, &mut p.rockets); + swap(&mut self.ominous_spawners, &mut p.ominous_spawners); + swap(&mut self.tnts, &mut p.tnts); + swap(&mut self.hoppers, &mut p.hoppers); + swap(&mut self.mobs, &mut p.mobs); + swap(&mut self.creatures.projectiles, &mut p.projectiles); + swap(&mut self.dropped, &mut p.dropped); + swap(&mut self.orbs, &mut p.orbs); + swap(&mut self.persist, &mut p.persist); + swap(&mut self.lectern_pulses, &mut p.lectern_pulses); + swap(&mut self.lid_openers, &mut p.lid_openers); + swap(&mut self.lan_events, &mut p.lan_events); + swap(&mut self.lan_block_events, &mut p.lan_block_events); + swap(&mut self.lan_entity_events, &mut p.lan_entity_events); + let (a, b) = (&mut self.be_anims, &mut p.anims); + swap(&mut a.chests, &mut b.chests); + swap(&mut a.books, &mut b.books); + swap(&mut a.shulkers, &mut b.shulkers); + swap(&mut a.bells, &mut b.bells); + swap(&mut a.conduits, &mut b.conduits); + swap(&mut a.skulls, &mut b.skulls); + swap(&mut a.pots, &mut b.pots); + swap(&mut a.gateways, &mut b.gateways); + swap(&mut a.gateway_servers, &mut b.gateway_servers); + self.raiding.swap_level(&mut p.raiding); + self.sculk.swap_level(&mut p.sculk); + self.creaking.swap_level(&mut p.creaking); + // What only the client draws starts over. + self.particles.clear(); + self.pickups.clear(); + } + + /// Put `target`'s level in place, opening it first if it is not loaded. + /// The level that was in place is kept with the others; the shared + /// clock and weather go with the swap. `host` is the hub when the caller + /// holds it (`self.lan` is taken during the network tick). + pub(super) fn enter_level(&mut self, host: Option<&mut LanHost>, target: Dim) -> Result<()> { + // The in-frame game keeps its one level (`enter_dimension`). + if self.options.dedicated.is_none() { + return Ok(()); + } + if target != self.dim { + let (mut p, opened) = match self.levels.parked.remove(&target) { + Some(p) => (p, false), + None => (self.open_level(target)?, true), + }; + p.world.game_time = self.world.game_time; + p.world.day_time = self.world.day_time; + p.world.weather = self.world.weather; + // The sculk tables' watched blocks are built once per game. + p.world.watched = std::mem::take(&mut self.world.watched); + self.swap_level(&mut p); + p.idle = 0; + let left = std::mem::replace(&mut self.dim, target); + self.levels.parked.insert(left, p); + if opened { + // The new streaming's radius and meshing options. + self.apply_settings(); + } + } + let host = match host { + Some(h) => Some(h), + None => self.lan.as_mut(), + }; + if let Some(h) = host { + h.hub.set_dimension(target.id()); + } + Ok(()) + } + + /// One server tick (`MinecraftServer.tickServer`): each level with + /// players in turn (`tickChildren`), its chunks streamed and loaded and + /// its world ticked. The first pass reads the network and runs the + /// tick's shared parts (the clock, the weather, functions, the autosave); + /// the next ones only their level. While `paused`, only the connections + /// run. + pub(super) fn tick_server(&mut self, paused: bool) -> Result<()> { + let dims = self.levels_to_tick(); + let n = dims.len(); + let mut result = Ok(()); + for (k, dim) in dims.into_iter().enumerate() { + if let Err(e) = self.enter_level(None, dim) { + result = Err(e); + break; + } + self.level_pass = Pass { + first: k == 0, + last: k + 1 == n, + }; + self.update_dedicated_streaming(); + self.update_world(); + result = if paused { + self.tick_lan(); + Ok(()) + } else if k == 0 { + self.tick() + } else { + let running = self.clock.world_runs(); + if running { + self.tick_weather_effects(); + } + self.tick_world(running) + }; + if result.is_err() { + break; + } + } + self.level_pass = Pass::default(); + result?; + self.close_idle_levels() + } + + /// `tick_server` as a server thread runs it. With `HORIZON_SPIKES=MS`, a + /// tick longer than MS is logged with its thread CPU time and spans. + pub(super) fn tick_server_timed(&mut self, paused: bool) -> Result<()> { + let Some(threshold) = self.options.spikes else { + return self.tick_server(paused); + }; + let (started, cpu) = (Instant::now(), horizon_client::spans::thread_cpu()); + let result = self.tick_server(paused); + let wall = started.elapsed().as_secs_f32() * 1000.0; + // Taken every tick: nothing else takes this thread's spans. + let spans = horizon_client::spans::take(); + if wall > threshold { + let cpu = horizon_client::spans::thread_cpu() + .zip(cpu) + .map_or_else(String::new, |(end, start)| { + format!(", cpu {:.1} ms", (end - start).as_secs_f32() * 1000.0) + }); + let list: Vec = spans + .iter() + .filter(|(_, ms)| *ms >= 0.5) + .map(|(n, ms)| format!("{n} {ms:.1}")) + .collect(); + log::info!( + "Horizon: slow tick {wall:.1} ms{cpu} ({} mobs): {}", + self.mobs.len(), + list.join(", ") + ); + } + result + } + + /// The levels this server tick runs, in `MinecraftServer.levels` order + /// (the Overworld, the Nether, the End): every level with a player in + /// it, or the Overworld while nobody is online. + pub(super) fn levels_to_tick(&self) -> Vec { + let guests = self + .lan + .as_ref() + .map_or(&[][..], |h| h.hub.guests.as_slice()); + let mut dims: Vec = [Dim::Overworld, Dim::Nether, Dim::End] + .into_iter() + .filter(|d| guests.iter().any(|g| g.dimension == d.id())) + .collect(); + if dims.is_empty() { + dims.push(Dim::Overworld); + } + dims + } + + /// Count the parked levels' empty ticks, and save and close the ones + /// (other than the Overworld) nobody has been in for a while. + pub(super) fn close_idle_levels(&mut self) -> Result<()> { + let occupied = self.levels_to_tick(); + let mut closing = Vec::new(); + for (dim, p) in &mut self.levels.parked { + if occupied.contains(dim) { + p.idle = 0; + continue; + } + p.idle += 1; + if *dim != Dim::Overworld && p.idle >= IDLE_TICKS { + closing.push(*dim); + } + } + if closing.is_empty() { + return Ok(()); + } + let back = self.dim; + for dim in closing { + self.enter_level(None, dim)?; + self.save_level(true)?; + self.enter_level(None, back)?; + self.levels.parked.remove(&dim); + } + Ok(()) + } + + /// Save every parked level (`MinecraftServer.saveAllChunks`); the level + /// in place is the caller's to save. + pub(super) fn save_parked_levels(&mut self, wait: bool) -> Result<()> { + if self.levels.parked.is_empty() { + return Ok(()); + } + let back = self.dim; + let dims: Vec = self.levels.parked.keys().copied().collect(); + for dim in dims { + self.enter_level(None, dim)?; + self.save_level(wait)?; + } + self.enter_level(None, back) + } +} diff --git a/crates/horizon-client/src/game/lightning.rs b/crates/horizon-client/src/game/lightning.rs index 834925c57..6c4dd933f 100644 --- a/crates/horizon-client/src/game/lightning.rs +++ b/crates/horizon-client/src/game/lightning.rs @@ -24,14 +24,18 @@ pub(super) struct Bolt { converted: Vec, } impl Game<'_> { - /// One `tickThunder` roll per loaded chunk. + /// `ServerChunkCache.tickSpawningChunk`: one `tickThunder` roll per + /// spawning chunk (within 128 blocks of a player) that is in entity + /// ticking range. pub(super) fn tick_thunder(&mut self) { // isRaining (rain level > 0.2) and isThundering (> 0.9, sky light, no ceiling). let (rain, thunder) = self.world.weather.levels(1.0); if rain <= 0.2 || thunder <= 0.9 || self.dim != super::Dim::Overworld { return; } - let chunks: Vec<(i32, i32)> = self.world.chunks.keys().copied().collect(); + let (_, mut chunks) = self.spawning_chunks(&self.level_players()); + let levels = self.tickets.levels(); + chunks.retain(|&(x, z)| levels.random_ticking(x, z)); for (cx, cz) in chunks { if self.world.random.next_int_bound(100_000).unwrap_or(1) != 0 { continue; @@ -50,7 +54,7 @@ impl Game<'_> { let difficulty = f64::from(self.ui.difficulty) * 0.75; let below = &self .res - .facts + .facts() .get(self.world.block(pos[0], pos[1] - 1, pos[2])) .name; let trap = self.ui.rules.bool("spawn_mobs") @@ -160,11 +164,11 @@ impl Game<'_> { } for p in spots { let state = self.world.block(p[0], p[1], p[2]); - if !self.res.facts.is_air(state) { + if !self.res.facts().is_air(state) { continue; } // BaseFireBlock.getState: soul fire on soul soil and sand. - let below = self.res.facts.get(self.world.block(p[0], p[1] - 1, p[2])); + let below = self.res.facts().get(self.world.block(p[0], p[1] - 1, p[2])); let name = if matches!(&*below.name, "minecraft:soul_soil" | "minecraft:soul_sand") { "minecraft:soul_fire" } else { @@ -236,7 +240,7 @@ impl Game<'_> { at[2].floor() as i32, ]; let state = self.world.block(p[0], p[1], p[2]); - if !self.res.facts.get(state).name.ends_with("lightning_rod") { + if !self.res.facts().get(state).name.ends_with("lightning_rod") { return; } let registry = self.pack.assets.registry; @@ -348,7 +352,7 @@ impl Game<'_> { /// `LightningBoltRenderer`: 4 layers of a trunk and two branches, 8 /// segments of 16 blocks, from the bolt's seed. pub(super) fn bolt_geometry(&self, camera: glam::DVec3, out: &mut Vec) { - let white = self.atlas.font.white(); + let white = self.gui.atlas.font.white(); let uv = [white.u0, white.v0]; // ARGB 0.45, 0.45, 0.5 at 0.3 alpha, packed RGBA. let color = (76u32 << 24) | (127 << 16) | (114 << 8) | 114; diff --git a/crates/horizon-client/src/game/loom.rs b/crates/horizon-client/src/game/loom.rs index 541e4e6a9..f1ff1a300 100644 --- a/crates/horizon-client/src/game/loom.rs +++ b/crates/horizon-client/src/game/loom.rs @@ -192,14 +192,14 @@ impl Game<'_> { base: &str, layers: &[BannerLayer], ) { - let banners = &self.res.stations.banners; + let banners = &self.res.stations().banners; let layers: Vec<(String, String)> = layers .iter() .map(|l| (banners.banner_texture(&l.pattern), l.color.to_string())) .collect(); horizon_client::item_render::gui::draw_banner_result( &self.pack.item_renderer, - &self.atlas.sprites, + &self.gui.atlas.sprites, batch, x, y, @@ -217,7 +217,7 @@ impl Game<'_> { draws: &[ScreenDraw], background: bool, ) -> Option> { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let mut tooltip = None; let mut sheet: Option = None; for d in draws { @@ -317,7 +317,7 @@ impl Game<'_> { let l = &s.loom; let dye = s.inputs[1] .as_ref() - .and_then(|d| self.res.items.get(&d.item)) + .and_then(|d| self.res.items().get(&d.item)) .and_then(|i| i.dye.clone()) .unwrap_or_else(|| "white".to_owned()); let result = s @@ -342,7 +342,7 @@ impl Game<'_> { dye: &dye, mouse: local, }; - let draws = screen_layout::loom(&view, &self.res.stations.banners); + let draws = screen_layout::loom(&view, &self.res.stations().banners); self.paint_screen(batch, ox, oy, &draws, false) } } diff --git a/crates/horizon-client/src/game/mace.rs b/crates/horizon-client/src/game/mace.rs index 062d04525..46091a773 100644 --- a/crates/horizon-client/src/game/mace.rs +++ b/crates/horizon-client/src/game/mace.rs @@ -84,11 +84,11 @@ impl Game<'_> { direct_attacker: Some("minecraft:player"), is_direct: true, damage_type: Some("minecraft:mace_smash"), - items: Some(&self.res.items), + items: Some(self.res.items()), ..Default::default() }; // EnchantmentHelper.modifyFallBasedDamage(..., 0) - let per_block = self.res.stations.enchantments.modify_value( + let per_block = self.res.stations().enchantments.modify_value( held.as_ref(), "minecraft:smash_damage_per_fallen_block", &ctx, @@ -111,10 +111,10 @@ impl Game<'_> { horizon_client::mobs::DamageKind::MaceSmash => "minecraft:mace_smash", _ => "minecraft:player_attack", }), - items: Some(&self.res.items), + items: Some(self.res.items()), ..Default::default() }; - let enchantments = &self.res.stations.enchantments; + let enchantments = &self.res.stations().enchantments; let random = &mut self.world.random; // CombatRules.getDamageAfterAbsorb: EnchantmentHelper.modifyArmorEffectiveness. let mut breach = |fraction: f32| { @@ -143,7 +143,7 @@ impl Game<'_> { }; let weapon = self .res - .items + .items() .get(&stack.item) .and_then(|i| i.weapon_damage.map(|d| (d, i.max_damage))); let smash = self.smash_attack(); diff --git a/crates/horizon-client/src/game/machines.rs b/crates/horizon-client/src/game/machines.rs index 0d334d248..c05c7295f 100644 --- a/crates/horizon-client/src/game/machines.rs +++ b/crates/horizon-client/src/game/machines.rs @@ -7,7 +7,7 @@ use super::Game; mod crafter; use horizon_client::{ Result, - containers::BlockEntity, + containers::{BlockEntity, HOPPER_SLOTS}, items::{ItemStack, Items}, world::World, }; @@ -361,10 +361,10 @@ impl Game<'_> { fn containers(&self) -> Containers<'_> { Containers { world: &self.world, - behaviours: &self.res.behaviours, - items: &self.res.items, + behaviours: self.res.behaviours(), + items: self.res.items(), registry: self.pack.assets.registry, - brewing: Some(&self.res.stations.brewing), + brewing: Some(&self.res.stations().brewing), bone_meal: bone_meal(), } } @@ -497,36 +497,35 @@ impl Game<'_> { } } } - /// Every hopper's `pushItemsTick`, in a stable position order. - pub(super) fn tick_hoppers(&mut self) -> Result<()> { + /// `HopperBlockEntity.pushItemsTick` of the hopper at `pos`, if its block + /// is still a hopper (`BlockEntityType.isValid`). + pub(super) fn tick_hopper(&mut self, pos: Pos) { let hopper = self - .pack - .assets - .registry - .default_state("minecraft:hopper") - .map(|s| self.res.facts.get(s).name.clone()); - let Some(hopper) = hopper else { return Ok(()) }; - let mut positions: Vec = self - .world - .block_entities - .keys() - .copied() - .filter(|p| self.res.facts.get(self.world.block(p.0, p.1, p.2)).name == hopper) - .collect(); - positions.sort_unstable(); - self.hoppers - .retain(|p, _| positions.binary_search(p).is_ok()); - let time = self.world.game_time; - for pos in positions { - let state = self.hoppers.entry(pos).or_default(); - state.cooldown -= 1; - state.ticked = time; - if state.cooldown <= 0 { - state.cooldown = 0; - self.hopper_try_move_items(pos); - } + .res + .facts() + .get(self.world.block(pos.0, pos.1, pos.2)) + .name + .as_ref() + == "minecraft:hopper"; + if !hopper { + self.hoppers.remove(&pos); + return; } - Ok(()) + let time = self.world.game_time; + let state = self.hoppers.entry(pos).or_default(); + state.cooldown -= 1; + state.ticked = time; + if state.cooldown <= 0 { + state.cooldown = 0; + self.hopper_try_move_items(pos); + } + } + /// Drops the cooldowns of hoppers whose block entity is gone. + pub(super) fn forget_gone_hoppers(&mut self) { + let entities = &self.world.block_entities; + self.hoppers.retain(|p, _| { + matches!(entities.get(p), Some(BlockEntity::Chest(items)) if items.len() == HOPPER_SLOTS) + }); } /// `HopperBlockEntity.tryMoveItems` with `suckInItems`. pub(super) fn hopper_try_move_items(&mut self, pos: Pos) { @@ -627,12 +626,12 @@ impl Game<'_> { return false; } let above_state = self.world.block(above.0, above.1, above.2); - let blocked = self.res.facts.get(above_state).full_collision + let blocked = self.res.facts().get(above_state).full_collision && !self .res - .behaviours + .behaviours() .tag("minecraft:does_not_block_hoppers") - .is_some_and(|t| t.contains(self.res.behaviours.block(above_state))); + .is_some_and(|t| t.contains(self.res.behaviours().block(above_state))); if blocked { return false; } @@ -679,7 +678,7 @@ impl Game<'_> { /// `DispenserBlock.dispenseFrom` / `DropperBlock.dispenseFrom`. pub(super) fn dispense_from(&mut self, pos: BlockPos, state: BlockStateId) -> Result<()> { let key = (pos.x, pos.y, pos.z); - let name = self.res.facts.get(state).name.clone(); + let name = self.res.facts().get(state).name.clone(); if &*name == "minecraft:crafter" { return self.crafter_dispense(pos, state); } @@ -771,12 +770,20 @@ enum Screen { impl Game<'_> { fn machine_screen(&self) -> Option { let key = self.container?; + // A server's menu shows by its type, whatever block (or minecart) holds it. + if let Some(menu) = self.remote_menu_type() { + return match menu { + "minecraft:hopper" => Some(Screen::Hopper), + "minecraft:generic_3x3" => Some(Screen::Dispenser), + _ => None, + }; + } if key.1 == super::minecarts::CART_KEY_Y { return (self.open_cart_kind() == Some(super::minecarts::CartKind::Hopper)) .then_some(Screen::Hopper); } let state = self.world.block(key.0, key.1, key.2); - match &*self.res.facts.get(state).name { + match &*self.res.facts().get(state).name { "minecraft:hopper" => Some(Screen::Hopper), "minecraft:dispenser" => Some(Screen::Dispenser), "minecraft:dropper" => Some(Screen::Dropper), @@ -833,7 +840,7 @@ impl Game<'_> { let Some(screen) = self.machine_screen() else { return false; }; - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let key_is_cart = self .container .is_some_and(|k| k.1 == super::minecarts::CART_KEY_Y); @@ -853,7 +860,9 @@ impl Game<'_> { batch.rect_uv(ox, oy, 176.0, height, r, u32::MAX); } let grey = horizon_client::hud::rgba(64, 64, 64, 255); - let title = &self.container_title(title.to_owned()); + let title = &self + .remote_menu_title() + .unwrap_or_else(|| self.container_title(title.to_owned())); let title_x = if screen == Screen::Hopper { 8.0 } else { diff --git a/crates/horizon-client/src/game/machines/crafter.rs b/crates/horizon-client/src/game/machines/crafter.rs index 11ca70d5d..d87646fe0 100644 --- a/crates/horizon-client/src/game/machines/crafter.rs +++ b/crates/horizon-client/src/game/machines/crafter.rs @@ -56,44 +56,33 @@ impl Game<'_> { _ => None, } } - /// `CrafterBlockEntity.serverTick` for every crafter, and the powered - /// flag following the block state. - pub(in crate::game) fn tick_crafters(&mut self) { - let positions: Vec = self - .world - .block_entities - .iter() - .filter(|(_, e)| matches!(e, BlockEntity::Crafter(_))) - .map(|(p, _)| *p) - .collect(); - let mut positions = positions; - positions.sort_unstable(); - for pos in positions { - let state = self.world.block(pos.0, pos.1, pos.2); - let triggered = self.containers().prop(state, "triggered") == Some("true"); - let mut finished = false; - if let Some(BlockEntity::Crafter(c)) = self.world.block_entities.get_mut(&pos) { - // CrafterBlock.neighborChanged sets the flag with the state; a - // loaded crafter keeps its saved flag until then. - if c.state_triggered.is_some_and(|t| t != triggered) { - c.triggered = triggered; - } - c.state_triggered = Some(triggered); - let remaining = c.crafting_ticks - 1; - if remaining >= 0 { - c.crafting_ticks = remaining; - finished = remaining == 0; - } + /// `CrafterBlockEntity.serverTick` of the crafter at `pos`, and the + /// powered flag following the block state. + pub(in crate::game) fn tick_crafter(&mut self, pos: Pos) { + let state = self.world.block(pos.0, pos.1, pos.2); + let triggered = self.containers().prop(state, "triggered") == Some("true"); + let mut finished = false; + if let Some(BlockEntity::Crafter(c)) = self.world.block_entities.get_mut(&pos) { + // CrafterBlock.neighborChanged sets the flag with the state; a + // loaded crafter keeps its saved flag until then. + if c.state_triggered.is_some_and(|t| t != triggered) { + c.triggered = triggered; } - if finished { - self.set_bool_prop( - pos, - "crafting", - false, - horizon_world::level::flags::UPDATE_ALL, - ); + c.state_triggered = Some(triggered); + let remaining = c.crafting_ticks - 1; + if remaining >= 0 { + c.crafting_ticks = remaining; + finished = remaining == 0; } } + if finished { + self.set_bool_prop( + pos, + "crafting", + false, + horizon_world::level::flags::UPDATE_ALL, + ); + } } /// `CrafterBlock.dispenseFrom` (the scheduled tick after a redstone pulse). pub(super) fn crafter_dispense(&mut self, pos: BlockPos, state: BlockStateId) -> Result<()> { @@ -105,7 +94,7 @@ impl Game<'_> { // RecipeCache.get: the first crafting recipe that matches, assembled. let found = self .res - .recipes + .recipes() .find(&grid, 3) .map(|(name, result)| (name.to_owned(), result)) .filter(|(_, result)| result.count > 0); @@ -131,7 +120,7 @@ impl Game<'_> { horizon_world::level::flags::UPDATE_CLIENTS, ); // The remainders come from the grid as it was before anything moved. - let remainders = self.res.recipes.remainders(&recipe, &grid, 3); + let remainders = self.res.recipes().remainders(&recipe, &grid, 3); self.crafter_eject(key, facing, result, &recipe); for rest in remainders.into_iter().flatten() { self.crafter_eject(key, facing, rest, &recipe); @@ -213,7 +202,7 @@ impl Game<'_> { pub(in crate::game) fn crafter_result(&self) -> Option { let key = self.container?; let c = self.crafter_at(key)?; - self.res.recipes.craft(&c.items, 3) + self.res.recipes().craft(&c.items, 3) } fn crafter_open(&self) -> bool { matches!(self.container_kind(), Some(BlockEntity::Crafter(_))) @@ -254,7 +243,7 @@ impl Game<'_> { // The background goes first, then the labels, then the slot decorations. let (background, rest) = draws.split_at(2.min(draws.len())); self.paint_screen(batch, ox, oy, background, true); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let grey = rgba(64, 64, 64, 255); let title = self.container_title(self.translate("container.crafter", "Crafter")); let tw = HudBatch::text_width(atlas, &title); diff --git a/crates/horizon-client/src/game/maps.rs b/crates/horizon-client/src/game/maps.rs index 2d7639260..855ef941a 100644 --- a/crates/horizon-client/src/game/maps.rs +++ b/crates/horizon-client/src/game/maps.rs @@ -673,17 +673,18 @@ impl Game<'_> { } else { loop { y -= 1; - color = self.res.facts.get(self.world.block(x, y, z)).map_color; + color = + self.res.facts().get(self.world.block(x, y, z)).map_color; if color != 0 || y <= min_y { break; } } - let f = self.res.facts.get(self.world.block(x, y, z)); + let f = self.res.facts().get(self.world.block(x, y, z)); if y > min_y && f.fluid != Fluid::None { let mut solid = y - 1; loop { let below = - self.res.facts.get(self.world.block(x, solid, z)); + self.res.facts().get(self.world.block(x, solid, z)); solid -= 1; water_depth += 1; if solid <= min_y || below.fluid == Fluid::None { @@ -789,6 +790,7 @@ impl Game<'_> { } let slot = self.maps.slots.last()?.1; let rect = self + .gui .atlas .sprites .get(&format!("horizon/map{slot}")) @@ -797,7 +799,7 @@ impl Game<'_> { if m.texture_dirty { m.texture_dirty = false; let rgba: Vec = m.colors.iter().flat_map(|&c| packed_rgba(c)).collect(); - let (w, h) = (self.atlas.width as f32, self.atlas.height as f32); + let (w, h) = (self.gui.atlas.width as f32, self.gui.atlas.height as f32); self.map_patches.push(horizon_client::font::Patch { x: (rect.u0 * w).round() as u32, y: (rect.v0 * h).round() as u32, @@ -855,7 +857,7 @@ impl Game<'_> { .map(|m| m.decorations.clone()) .unwrap_or_default(); for (i, d) in decorations.iter().enumerate() { - let Some(s) = self.atlas.sprites.get(d.sprite).copied() else { + let Some(s) = self.gui.atlas.sprites.get(d.sprite).copied() else { continue; }; let dm = glam::Mat4::from_translation(glam::Vec3::new( @@ -931,7 +933,7 @@ impl Game<'_> { } else { "map/map_background" }; - if let Some(r) = self.atlas.sprites.get(background).copied() { + if let Some(r) = self.gui.atlas.sprites.get(background).copied() { for (c, uv) in [ ([-7.0, 135.0, 0.0], [r.u0, r.v1]), ([135.0, 135.0, 0.0], [r.u1, r.v1]), diff --git a/crates/horizon-client/src/game/minecarts.rs b/crates/horizon-client/src/game/minecarts.rs index de42c4907..367dc6aaf 100644 --- a/crates/horizon-client/src/game/minecarts.rs +++ b/crates/horizon-client/src/game/minecarts.rs @@ -17,7 +17,6 @@ use horizon_client::{ }; use horizon_core::{ geometry::{Aabb, Vec3}, - position::BlockPos, random::RandomSource, }; use horizon_world::registry::BlockStateId; @@ -91,6 +90,10 @@ pub(super) struct Minecart { } const WIDTH: f64 = 0.98; const HEIGHT: f64 = 0.7; +/// AbstractMinecart.getDefaultDisplayOffset, in sixteenths of a block. +fn display_offset(kind: CartKind) -> f64 { + if kind == CartKind::Hopper { 1.0 } else { 6.0 } +} /// The minecart's passenger attachment minus Player.DEFAULT_VEHICLE_ATTACHMENT. const SEAT: f64 = 0.1875 - 0.6; /// The placeholder container key under which an open cart's items live. @@ -154,6 +157,10 @@ pub(super) struct Rail { powered: bool, } impl Minecart { + /// `Entity.onGround`. + pub(super) fn on_ground(&self) -> bool { + self.on_ground + } pub fn new(id: u32, kind: CartKind, position: Vec3) -> Self { Self { id, @@ -211,7 +218,7 @@ impl CartModels { impl Game<'_> { pub(super) fn rail(&self, x: i32, y: i32, z: i32) -> Option { let state = self.world.block(x, y, z); - let name = self.res.facts.get(state).name.clone(); + let name = self.res.facts().get(state).name.clone(); if !matches!( &*name, "minecraft:rail" @@ -240,11 +247,14 @@ impl Game<'_> { } } fn conductor(&self, x: i32, y: i32, z: i32) -> bool { - self.res.facts.get(self.world.block(x, y, z)).full_collision + self.res + .facts() + .get(self.world.block(x, y, z)) + .full_collision } fn cart_in_water(&self, c: &Minecart) -> bool { let p = c.position; - let f = self.res.facts.get(self.world.block( + let f = self.res.facts().get(self.world.block( p.x.floor() as i32, (p.y + 0.1).floor() as i32, p.z.floor() as i32, @@ -654,7 +664,7 @@ impl Game<'_> { stack.count = 0; } Some(s) if s.same_item_same_components(&stack) => { - let max = self.res.items.get(&s.item).map_or(64, |i| i.max_stack); + let max = self.res.items().get(&s.item).map_or(64, |i| i.max_stack); let moved = stack.count.min(max.saturating_sub(s.count)); s.count += moved; stack.count -= moved; @@ -882,9 +892,9 @@ impl Game<'_> { (-1, 1), ] { let (x, y, z) = (bx + dx, by + dy, bz + dz); - let below = self.res.facts.get(self.world.block(x, y - 1, z)); - let here = self.res.facts.get(self.world.block(x, y, z)); - let head = self.res.facts.get(self.world.block(x, y + 1, z)); + let below = self.res.facts().get(self.world.block(x, y - 1, z)); + let here = self.res.facts().get(self.world.block(x, y, z)); + let head = self.res.facts().get(self.world.block(x, y + 1, z)); if (below.full_collision || below.legacy_solid) && !here.full_collision && !head.full_collision @@ -1139,16 +1149,17 @@ impl Game<'_> { } } } - /// A block model's faces under `m` (block space 0..1), lit flat. + /// Baked block faces with terrain shade or block-item diffuse lighting. pub(super) fn block_model_vertices( &self, state: BlockStateId, - seed_pos: BlockPos, + seed: i64, m: glam::Mat4, light: u32, + diffuse: bool, out: &mut Vec, ) { - let Ok(models) = self.pack.assets.model_catalog.collect(state, seed_pos) else { + let Ok(models) = self.pack.render.model_catalog.collect_seeded(state, seed) else { return; }; for (_, model) in &models { @@ -1157,7 +1168,7 @@ impl Game<'_> { .pack .layer_names .get(&face.layer) - .and_then(|n| self.atlas.sprites.get(n)) + .and_then(|n| self.gui.atlas.sprites.get(n)) else { continue; }; @@ -1168,12 +1179,30 @@ impl Game<'_> { D::North | D::South => 204, D::West | D::East => 153, }; - let color = 0xFF00_0000 | shade << 16 | shade << 8 | shade; + let color = if diffuse { + u32::MAX + } else { + 0xFF00_0000 | shade << 16 | shade << 8 | shade + }; + let normal = if diffuse { + use horizon_client::item_render::{direction_vector, transform_normal}; + let n = transform_normal(m, direction_vector(face.quad.facing)); + let n = n.to_array().map(|v| (v.clamp(-1.0, 1.0) * 127.0) as i8); + [n[0], n[1], n[2], 127] + } else { + [0; 4] + }; + let light = if diffuse { + let emission = u32::from(face.quad.light_emission) * 16; + (light & 0xFF) | (light >> 8 & 0xFF).max(emission) << 8 + } else { + light + }; for k in 0..4 { let p = face.quad.positions[k]; let uv = face.quad.uv[k]; out.push(Vertex { - normal: [0; 4], + normal, pos: m.transform_point3(glam::Vec3::from_array(p)).to_array(), uv: [s.u0 + uv[0] * (s.u1 - s.u0), s.v0 + uv[1] * (s.v1 - s.v0)], layer: 0, @@ -1212,24 +1241,53 @@ impl Game<'_> { /// front and back points, pitched on slopes, with the display block. pub(super) fn cart_geometry(&self, camera: glam::DVec3, alpha: f64, out: &mut Vec) { let a = alpha as f32; + let Some(uv) = self + .gui + .atlas + .sprites + .get("entity/minecart/minecart") + .map(|r| [r.u0, r.v0, r.u1, r.v1]) + else { + return; + }; + // Entity.shouldRenderAtSqrDistance: 64 times the bounding box's mean + // edge, scaled by the entity distance option. + let range = + (WIDTH + HEIGHT + WIDTH) / 3.0 * 64.0 * f64::from(self.settings.entity_distance); for c in &self.carts { let entity_start = out.len(); let Some(model) = self.cart_models.models.get(&c.kind) else { continue; }; - let Some(uv) = self - .atlas - .sprites - .get("entity/minecart/minecart") - .map(|r| [r.u0, r.v0, r.u1, r.v1]) - else { - continue; - }; let (x, y, z) = ( c.previous.x + (c.position.x - c.previous.x) * alpha, c.previous.y + (c.position.y - c.previous.y) * alpha, c.previous.z + (c.position.z - c.previous.z) * alpha, ); + // LevelExtractor.isEntityVisible: the cart the player rides always draws. + if self.riding != Some(c.id) { + let d = glam::DVec3::new(c.position.x, c.position.y, c.position.z) - camera; + if d.length_squared() >= range * range { + continue; + } + // EntityRenderer.shouldRender: the interpolated box grown by 0.5; + // AbstractMinecartRenderer.getBoundingBoxForCulling stretches it + // up by the display block's offset. + if let Some(view) = &self.view_frustum { + let top = HEIGHT + + if c.kind == CartKind::Rideable { + 0.0 + } else { + display_offset(c.kind) * 0.75 / 16.0 + }; + let p = glam::DVec3::new(x, y, z) - camera; + let lo = p - glam::DVec3::new(WIDTH / 2.0 + 0.5, 0.5, WIDTH / 2.0 + 0.5); + let hi = p + glam::DVec3::new(WIDTH / 2.0 + 0.5, top + 0.5, WIDTH / 2.0 + 0.5); + if !view.intersects(lo.as_vec3(), hi.as_vec3()) { + continue; + } + } + } let mut yaw = c.previous_yaw + ((c.yaw - c.previous_yaw + 180.0).rem_euclid(360.0) - 180.0) * a; let mut pitch = 0.0f32; @@ -1274,7 +1332,11 @@ impl Game<'_> { if let Some(state) = self.display_state(c) { let display = base * glam::Mat4::from_scale(glam::Vec3::splat(0.75)) - * glam::Mat4::from_translation(glam::Vec3::new(-0.5, (6.0 - 8.0) / 16.0, 0.5)) + * glam::Mat4::from_translation(glam::Vec3::new( + -0.5, + (display_offset(c.kind) as f32 - 8.0) / 16.0, + 0.5, + )) * glam::Mat4::from_rotation_y(90f32.to_radians()); if c.kind == CartKind::Chest { self.cart_chest(state, display, l, out); @@ -1297,7 +1359,7 @@ impl Game<'_> { * glam::Mat4::from_translation(glam::Vec3::splat(-0.5)); let flash = c.kind == CartKind::Tnt && c.fuse > -1 && (c.fuse / 5) % 2 == 0; let start = out.len(); - self.block_model_vertices(state, BlockPos::default(), m, packed, out); + self.block_model_vertices(state, 0, m, packed, false, out); if flash { // submitWhiteSolidBlock: OverlayTexture.u(1.0) on the cart's own light. for v in &mut out[start..] { @@ -1324,15 +1386,15 @@ impl Game<'_> { game_time: self.world.game_time, max_y: self.dim.max_y(), world: &self.world, - sprites: &self.atlas.sprites, + sprites: &self.gui.atlas.sprites, nether: self.dim == super::Dim::Nether, lid: &lid, sign: &|_| None, banner: &|_| Vec::new(), - glyph: &|c| self.atlas.glyph_cell(c), + glyph: &|c| self.gui.atlas.glyph_cell(c), }; self.res - .block_entity_render + .block_entity_render() .render_state(&scene, state.0, m, light, out); } } @@ -1410,3 +1472,18 @@ impl Minecart { Some(c) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn display_offset_matches_vanilla_for_each_cart_kind() { + assert_eq!(display_offset(CartKind::Hopper), 1.0); + assert_eq!(display_offset(CartKind::Chest), 6.0); + assert_eq!(display_offset(CartKind::Furnace), 6.0); + assert_eq!(display_offset(CartKind::Tnt), 6.0); + assert_eq!(display_offset(CartKind::CommandBlock), 6.0); + assert_eq!(display_offset(CartKind::Rideable), 6.0); + } +} diff --git a/crates/horizon-client/src/game/mob_blocks.rs b/crates/horizon-client/src/game/mob_blocks.rs index c2246cf5b..27a60ca19 100644 --- a/crates/horizon-client/src/game/mob_blocks.rs +++ b/crates/horizon-client/src/game/mob_blocks.rs @@ -75,10 +75,10 @@ impl Game<'_> { if left <= 0 { return; } - let Some(kind) = self.res.mob_types.get("minecraft:elder_guardian") else { + let Some(kind) = self.res.mob_types().get("minecraft:elder_guardian") else { return; }; - let Some(r) = self.atlas.sprites.get("entity/guardian_elder").copied() else { + let Some(r) = self.gui.atlas.sprites.get("entity/guardian_elder").copied() else { return; }; let age = (APPARITION_TICKS - left) as f32 + alpha as f32; @@ -142,12 +142,12 @@ impl Game<'_> { return; }; let Some(head) = - render::part_transform(mob, alpha as f32, camera, &self.res.mob_data, "head") + render::part_transform(mob, alpha as f32, camera, self.res.mob_data(), "head") else { return; }; let m = banner_matrix(head); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let uv = |name: &str| atlas.sprites.get(name).map(|r| [r.u0, r.v0, r.u1, r.v1]); // BannerRenderer: the flag sways with a per-banner phase. let phase = @@ -187,7 +187,7 @@ impl Game<'_> { ) { for (corners, shade, side) in FACES { let name = cube.sprites[usize::from(side)]; - let Some(r) = self.atlas.sprites.get(name).copied() else { + let Some(r) = self.gui.atlas.sprites.get(name).copied() else { continue; }; let tint = if side == 0 { cube.tint } else { 0xFF_FFFF }; @@ -257,20 +257,36 @@ impl Game<'_> { out: &mut Vec, ) { // ItemInHandLayer submits with OverlayTexture.NO_OVERLAY: no hurt tint. - for (item, left) in [(&mob.extra.main_hand, false), (&mob.extra.off_hand, true)] { + for (item, off_hand) in [(&mob.extra.main_hand, false), (&mob.extra.off_hand, true)] { let Some(item) = item else { continue }; + let left = off_hand != mob.extra.main_hand_left; let Some(t) = - render::held_item_transform(mob, alpha as f32, camera, &self.res.mob_data, left) + render::held_item_transform(mob, alpha as f32, camera, self.res.mob_data(), left) else { continue; }; - let slot = if left { + let slot = if off_hand { horizon_client::mobs::gear::OFF } else { horizon_client::mobs::gear::MAIN }; - let mut stack = ItemStack::new(item, 1); - if horizon_client::mobs::gear::enchanted(mob, slot) { + let local_player = self + .player_model + .as_ref() + .is_some_and(|p| p.id == mob.id && p.kind.id == mob.kind.id); + let mut stack = if local_player { + let Some(stack) = self.stack_in(if off_hand { + super::shield::OFFHAND_SLOT + } else { + self.selected + }) else { + continue; + }; + stack.clone() + } else { + ItemStack::new(item, 1) + }; + if !local_player && horizon_client::mobs::gear::enchanted(mob, slot) { // The gear's enchantments are not tracked, only that it has some. stack.enchant("minecraft:unbreaking", 1); } @@ -284,12 +300,13 @@ impl Game<'_> { ); // The local player's rod while its hook is out. owner.fishing_cast_with_this_item = self.cast_rod().is_some() - && self.main_hand_rod() != left + && self.main_hand_rod() != off_hand && self .player_model .as_ref() .is_some_and(|p| std::ptr::eq(p, mob)); - if !left && mob.extra.using_item > 0 { + owner.main_arm_left = mob.extra.main_hand_left; + if off_hand == mob.extra.using_off_hand && mob.extra.using_item > 0 { owner.using_this_item = true; owner.use_duration = 72_000; owner.use_item_remaining_ticks = 72_000 - mob.extra.using_item; diff --git a/crates/horizon-client/src/game/mob_gear.rs b/crates/horizon-client/src/game/mob_gear.rs index c4a77e38c..809b250dc 100644 --- a/crates/horizon-client/src/game/mob_gear.rs +++ b/crates/horizon-client/src/game/mob_gear.rs @@ -91,9 +91,9 @@ impl Game<'_> { let cost = random .next_int_between_inclusive(MIN_COST, top) .unwrap_or(MIN_COST); - let e = &self.res.stations.enchantments; + let e = &self.res.stations().enchantments; let pool = e.tag("minecraft:on_mob_spawn_equipment"); - let picked = e.select(&self.res.items, random, item, cost, pool); + let picked = e.select(self.res.items(), random, item, cost, pool); if let Some(m) = self.mobs.iter_mut().find(|m| m.id == id) { for (enchantment, level) in picked { gear::upgrade(m, slot, &enchantment, level); diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs index 5a1131c3b..b6abf38ef 100644 --- a/crates/horizon-client/src/game/mods.rs +++ b/crates/horizon-client/src/game/mods.rs @@ -284,7 +284,7 @@ impl Game<'_> { let focused = self.screen == Screen::Chat; self.ui .chat - .add(&self.atlas, line, None, self.ticks, focused); + .add(&self.gui.atlas, line, None, self.ticks, focused); } } /// The mods' HUD panels, stacked with the extras in their corners. @@ -305,7 +305,7 @@ impl Game<'_> { .iter() .map(|l| (l.text.as_str(), l.color)) .collect(); - extras_hud::draw_lines(batch, &self.atlas, corners, corner, &lines); + extras_hud::draw_lines(batch, &self.gui.atlas, corners, corner, &lines); } } } diff --git a/crates/horizon-client/src/game/mount_menu.rs b/crates/horizon-client/src/game/mount_menu.rs index 55e9dde11..1db2210d3 100644 --- a/crates/horizon-client/src/game/mount_menu.rs +++ b/crates/horizon-client/src/game/mount_menu.rs @@ -283,7 +283,7 @@ impl Game<'_> { let Some(open) = self.mount_menu.open.as_ref() else { return; }; - let atlas = &self.atlas; + let atlas = &self.gui.atlas; batch.sprite_region( atlas, "gui/container/horse", @@ -376,7 +376,7 @@ impl Game<'_> { m.hurt_time = 0; m.death_time = 0; let camera = glam::DVec3::new(m.position.x, m.position.y, m.position.z); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let lookup = |name: &str| atlas.sprites.get(name).map(|r| [r.u0, r.v0, r.u1, r.v1]); let mut verts = Vec::new(); horizon_client::mobs::render::render_with( @@ -385,7 +385,7 @@ impl Game<'_> { camera, &lookup, 240 | 240 << 8, - &self.res.mob_data, + self.res.mob_data(), &[], &mut verts, ); diff --git a/crates/horizon-client/src/game/mounts.rs b/crates/horizon-client/src/game/mounts.rs index 869364176..42eac1e9b 100644 --- a/crates/horizon-client/src/game/mounts.rs +++ b/crates/horizon-client/src/game/mounts.rs @@ -90,7 +90,11 @@ impl Game<'_> { && let Some(stack) = self.inventory[self.selected].as_mut() { stack.damage += wear; - let max = self.res.items.get(&stack.item).map_or(25, |i| i.max_damage); + let max = self + .res + .items() + .get(&stack.item) + .map_or(25, |i| i.max_damage); if stack.damage >= max { self.inventory[self.selected] = Some(ItemStack::new("minecraft:fishing_rod", 1)); } @@ -406,7 +410,7 @@ impl Game<'_> { .find(|c| { let f = |dy: i32| { self.res - .facts + .facts() .get(self.world.block( c.x.floor() as i32, c.y.floor() as i32 + dy, @@ -457,7 +461,7 @@ impl Game<'_> { return (0, false); }; let m = &self.mobs[i]; - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let hearts = horizon_client::hud_layout::vehicle_max_hearts(m.attrs.max_health); let draws = horizon_client::hud_layout::vehicle_health(w, h, hearts, m.health); horizon_client::hud_layout::paint(batch, atlas, &draws); diff --git a/crates/horizon-client/src/game/music.rs b/crates/horizon-client/src/game/music.rs index 381af21cf..9198a78a1 100644 --- a/crates/horizon-client/src/game/music.rs +++ b/crates/horizon-client/src/game/music.rs @@ -50,7 +50,7 @@ impl Game<'_> { let (background, probe_volume) = match eye { Some(eye) => { let (dimension, biome) = (self.dim.id(), self.biome_at(eye)); - let tables = &self.res.music; + let tables = self.res.music(); ( tables.background(dimension, biome), tables.volume(dimension, biome), @@ -81,7 +81,7 @@ impl Game<'_> { .chunks .get(&(x >> 4, z >> 4)) .map_or("minecraft:plains", |c| { - self.res.biomes.list[c.biome(x, y, z) as usize] + self.res.biomes().list[c.biome(x, y, z) as usize] .name .as_str() }) @@ -89,11 +89,11 @@ impl Game<'_> { /// `isEyeInFluid(WATER)`: the eye inside the water of its block column. fn eye_in_water(&self, x: f64, eye_y: f64, z: f64) -> bool { let (bx, by, bz) = (x.floor() as i32, eye_y.floor() as i32, z.floor() as i32); - let f = self.res.facts.get(self.world.block(bx, by, bz)); + let f = self.res.facts().get(self.world.block(bx, by, bz)); if f.fluid != Fluid::Water { return false; } - let above = self.res.facts.get(self.world.block(bx, by + 1, bz)); + let above = self.res.facts().get(self.world.block(bx, by + 1, bz)); let height = if above.fluid == Fluid::Water { 1.0 } else { @@ -111,7 +111,7 @@ impl Game<'_> { }; let feet = [p.position.x, p.position.y, p.position.z]; let eye_y = feet[1] + f64::from(p.dimensions.eye_height); - let sounds = self.res.ambient.get(self.dim.id(), self.biome_at(feet)); + let sounds = self.res.ambient().get(self.dim.id(), self.biome_at(feet)); let underwater = self.eye_in_water(feet[0], eye_y, feet[2]); let world = &self.world; let light = |b: [i32; 3]| { @@ -123,7 +123,7 @@ impl Game<'_> { f64::from(p.dimensions.height), ); let registry = &self.pack.assets.registry; - let facts = &self.res.facts; + let facts = self.res.facts(); let bubble_column = horizon_client::ambience::bubble_column_in( [ feet[0] - half, diff --git a/crates/horizon-client/src/game/names.rs b/crates/horizon-client/src/game/names.rs index 368c5c0f2..707b9df96 100644 --- a/crates/horizon-client/src/game/names.rs +++ b/crates/horizon-client/src/game/names.rs @@ -218,7 +218,7 @@ impl Game<'_> { let forward = glam::DVec3::from_array(view_vector(self.yaw, self.pitch)); let right = forward.cross(glam::DVec3::Y).normalize_or_zero(); let up = right.cross(forward).normalize_or_zero(); - let white = self.atlas.font.white(); + let white = self.gui.atlas.font.white(); let away = forward.as_vec3() * 0.002; for (text, lift) in lines { let o = (o + glam::DVec3::new(0.0, *lift, 0.0)).as_vec3(); @@ -240,7 +240,7 @@ impl Game<'_> { .collect(); let width: f32 = chars .iter() - .map(|(c, _)| self.atlas.glyph(*c).map_or(6.0, |g| g.1)) + .map(|(c, _)| self.gui.atlas.glyph(*c).map_or(6.0, |g| g.1)) .sum(); let x0 = -(width / 2.0).trunc(); let y0 = if text.plain() == "deadmau5" { @@ -278,7 +278,7 @@ impl Game<'_> { } let mut x = x0; for (ch, color) in chars { - let Some((g, advance)) = self.atlas.glyph(ch) else { + let Some((g, advance)) = self.gui.atlas.glyph(ch) else { x += 6.0; continue; }; diff --git a/crates/horizon-client/src/game/natural_spawning.rs b/crates/horizon-client/src/game/natural_spawning.rs index 571bbf5e0..9bf7023af 100644 --- a/crates/horizon-client/src/game/natural_spawning.rs +++ b/crates/horizon-client/src/game/natural_spawning.rs @@ -23,10 +23,54 @@ use horizon_core::{ use horizon_world::registry::BlockStateId; use std::collections::HashMap; +/// A player in the level in place, as spawning and despawning see it. +pub(super) struct LevelPlayer { + pub(super) position: Vec3, + pub(super) bounds: Aabb, + pub(super) alive: bool, + pub(super) creative: bool, + pub(super) spectator: bool, +} +impl LevelPlayer { + fn distance_sqr(&self, x: f64, y: f64, z: f64) -> f64 { + (self.position.x - x).powi(2) + + (self.position.y - y).powi(2) + + (self.position.z - z).powi(2) + } +} +/// `ChunkMap.euclideanDistanceSquared` below 128 blocks +/// (`playerIsCloseEnoughForSpawning`). +fn close_enough_for_spawning(p: &LevelPlayer, (cx, cz): (i32, i32)) -> bool { + let (x, z) = ( + f64::from(cx * 16 + 8) - p.position.x, + f64::from(cz * 16 + 8) - p.position.z, + ); + !p.spectator && x * x + z * z < 16384.0 +} +/// `EntityGetter.getNearestPlayer(x, y, z, -1, false)`: the nearest +/// non-spectator, alive or not. +pub(super) fn nearest_player( + players: &[LevelPlayer], + x: f64, + y: f64, + z: f64, +) -> Option<&LevelPlayer> { + let mut best: Option<(&LevelPlayer, f64)> = None; + for p in players.iter().filter(|p| !p.spectator) { + let d = p.distance_sqr(x, y, z); + if best.is_none_or(|(_, b)| d < b) { + best = Some((p, d)); + } + } + best.map(|(p, _)| p) +} + /// The game level as natural spawning reads it. pub(super) struct GameLevel<'g, 'a> { pub(super) game: &'g Game<'a>, pub(super) w: MobWorld<'g>, + /// Every player in the level ([`Game::level_players`]). + pub(super) players: Vec, /// Boxes of the mobs this pass created, not yet in the game's list. pub(super) pending: std::cell::RefCell>, /// The types and boxes of what a spawner created this tick. @@ -35,7 +79,7 @@ pub(super) struct GameLevel<'g, 'a> { impl GameLevel<'_, '_> { fn biome(&self, x: i32, y: i32, z: i32) -> Option<&BiomeInfo> { let chunk = self.game.world.chunks.get(&(x >> 4, z >> 4))?; - Some(self.game.res.biomes.get(chunk.biome(x, y, z))) + Some(self.game.res.biomes().get(chunk.biome(x, y, z))) } } impl SpawnLevel for GameLevel<'_, '_> { @@ -84,8 +128,11 @@ impl SpawnLevel for GameLevel<'_, '_> { fn world_seed(&self) -> i64 { self.w.world_seed } + /// `getNearestPlayer(x, y, z, distance, true)`, over every player. fn player_near(&self, x: f64, y: f64, z: f64, distance: f64) -> bool { - self.w.player_near(x, y, z, distance) + self.players.iter().any(|p| { + p.alive && !p.creative && !p.spectator && p.distance_sqr(x, y, z) < distance * distance + }) } } impl NaturalLevel for GameLevel<'_, '_> { @@ -106,7 +153,7 @@ impl NaturalLevel for GameLevel<'_, '_> { self.game.dim == Dim::Nether } fn mob_type(&self, entity: &str) -> Option<(Species, f32, f32, Category)> { - let t = self.game.res.mob_types.get(entity)?; + let t = self.game.res.mob_types().get(entity)?; Some(( t.species, t.width as f32, @@ -138,12 +185,7 @@ impl NaturalLevel for GameLevel<'_, '_> { StructureSpawns::shared(&self.game.options.resources) } fn nearest_player_distance_sqr(&self, x: f64, y: f64, z: f64) -> Option { - let p = self.game.player.as_ref()?; - if self.game.spectator() { - return None; - } - let d = [p.position.x - x, p.position.y - y, p.position.z - z]; - Some(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]) + nearest_player(&self.players, x, y, z).map(|p| p.distance_sqr(x, y, z)) } fn respawn(&self) -> Option<[i32; 3]> { // The world spawn point lives in the overworld. @@ -184,9 +226,9 @@ impl NaturalLevel for GameLevel<'_, '_> { } fn unobstructed(&self, bounds: Aabb) -> bool { let player = self - .game - .player_view() - .is_some_and(|p| p.alive && !p.spectator && p.bounds.intersects(bounds)); + .players + .iter() + .any(|p| p.alive && !p.spectator && p.bounds.intersects(bounds)); !player && !self.pending.borrow().iter().any(|b| b.intersects(bounds)) && !self @@ -198,15 +240,19 @@ impl NaturalLevel for GameLevel<'_, '_> { fn spawn_mobs(&self) -> bool { self.game.ui.rules.bool("spawn_mobs") } - /// Scanned down the column: sections of air alone are skipped (air counts - /// for no heightmap), and an unloaded column reads as air. + /// WORLD_SURFACE (every spawn attempt's) is the chunk's kept heightmap. + /// The others are scanned down the column: sections of air alone are + /// skipped (air counts for no heightmap). An unloaded column reads as air. fn heightmap(&self, kind: Heightmap, x: i32, z: i32) -> i32 { let world = self.w.world; + if kind == Heightmap::WorldSurface { + return self.w.world_surface(x, z); + } world .chunks .get(&(x >> 4, z >> 4)) .and_then(|chunk| { - chunk.highest(world.air.0 as u16, |y| { + chunk.highest(world.air.0, |y| { spawn_selection::heightmap_blocks(self, kind, x, y, z) }) }) @@ -217,24 +263,17 @@ impl NaturalLevel for GameLevel<'_, '_> { } } impl horizon_client::mobs::spawner::SpawnerLevel for GameLevel<'_, '_> { + /// `Level.hasNearbyAlivePlayer`, over every player. fn alive_player_near(&self, x: f64, y: f64, z: f64, range: f64) -> bool { - let Some(p) = self.game.player_view() else { - return false; - }; - if !p.alive || p.spectator { - return false; - } - let at = self.game.player.as_ref().map(|p| p.position); - at.is_some_and(|a| { - let d = (a.x - x).powi(2) + (a.y - y).powi(2) + (a.z - z).powi(2); - range < 0.0 || d < range * range + self.players.iter().any(|p| { + p.alive && !p.spectator && (range < 0.0 || p.distance_sqr(x, y, z) < range * range) }) } fn spawner_blocks_work(&self) -> bool { self.game.ui.rules.bool("spawner_blocks_work") } fn entity_exists(&self, id: &str) -> bool { - self.game.res.mob_types.contains_key(id) || self.game.entity_type_exists(id) + self.game.res.mob_types().contains_key(id) || self.game.entity_type_exists(id) } fn count_entities(&self, id: &str, bounds: Aabb) -> usize { let mobs = self @@ -257,6 +296,7 @@ impl<'a> Game<'a> { GameLevel { game: self, w: self.mob_world_base(&[]), + players: self.level_players(), pending: Default::default(), pending_ids: Default::default(), } @@ -264,7 +304,7 @@ impl<'a> Game<'a> { /// Creates a mob the spawner accepted, finalized the game's way; the /// caller adds it with [`Game::add_spawned`]. fn finalize_spawned(&self, r: &SpawnRequest, pending: &mut Vec) -> Option { - let kind = self.res.mob_types.get(r.entity)?.clone(); + let kind = self.res.mob_types().get(r.entity)?.clone(); let [x, y, z] = r.position; let mut mob = Mob::new(0, kind, Vec3::new(x, y, z), r.yaw); let facts = self.biome_facts(x.floor() as i32, y.floor() as i32, z.floor() as i32); @@ -295,25 +335,82 @@ impl<'a> Game<'a> { } } } + /// Every player in the level in place: the local player, and the LAN + /// guests or server players in it. + pub(super) fn level_players(&self) -> Vec { + let mut players: Vec = self.lan.as_ref().map_or_else(Vec::new, |h| { + h.hub + .guests + .iter() + .filter(|g| h.hub.here(g)) + .map(|g| LevelPlayer { + position: Vec3::new(g.position[0], g.position[1], g.position[2]), + bounds: super::lan_mobs::guest_bounds(g), + alive: !g.dead, + creative: g.creative(), + spectator: g.spectator(), + }) + .collect() + }); + if let Some(p) = self.player_view() { + players.insert( + 0, + LevelPlayer { + position: p.position, + bounds: p.bounds, + alive: p.alive, + creative: p.creative, + spectator: p.spectator, + }, + ); + } + players + } + /// `ChunkMap.collectSpawningChunks`: the natural spawn chunk tracker's + /// chunks (within 8 chunks of a player, `getSpawnCandidateChunks`) that + /// are ticking and within 128 blocks of a non-spectator. Also returns how + /// many chunks the tracker holds (`getNaturalSpawnChunkCount`). + pub(super) fn spawning_chunks(&self, players: &[LevelPlayer]) -> (i32, Vec<(i32, i32)>) { + let mut candidates: Vec<(i32, i32)> = Vec::with_capacity(289 * players.len()); + let mut seen = horizon_client::fast_hash::FxHashSet::default(); + for p in players { + let (pcx, pcz) = ( + p.position.x.floor() as i32 >> 4, + p.position.z.floor() as i32 >> 4, + ); + for dx in -8..=8 { + for dz in -8..=8 { + let key = (pcx + dx, pcz + dz); + if seen.insert(key) { + candidates.push(key); + } + } + } + } + let count = candidates.len() as i32; + let levels = self.tickets.levels(); + candidates.retain(|&(cx, cz)| { + levels.loaded_ticking(cx, cz) + && super::ticking_chunk(&self.world, cx, cz) + && players + .iter() + .any(|p| close_enough_for_spawning(p, (cx, cz))) + }); + (count, candidates) + } /// `ServerChunkCache.tickChunks`: the spawn state over the loaded mobs, /// the categories under their global caps (creatures every 400 ticks), - /// then `spawnForChunk` in the shuffled chunks within 128 blocks of the - /// player. + /// then `tickSpawningChunk` in the shuffled spawning chunks of every + /// player: inhabited time, then `spawnForChunk`. pub(super) fn spawn_natural(&mut self) { - let Some(p) = self.player.as_ref().map(|p| p.position) else { + let players = self.level_players(); + if players.is_empty() { return; - }; - let (pcx, pcz) = (p.x.floor() as i32 >> 4, p.z.floor() as i32 >> 4); - // ChunkMap.collectSpawningChunks: tracked chunks whose centre is - // within 128 blocks of a non-spectator. - let spectator = self.spectator(); - let mut chunks: Vec<(i32, i32)> = (-8..=8) - .flat_map(|dx| (-8..=8).map(move |dz| (pcx + dx, pcz + dz))) - .filter(|&(cx, cz)| { - let (x, z) = (f64::from(cx * 16 + 8) - p.x, f64::from(cz * 16 + 8) - p.z); - !spectator && x * x + z * z < 16384.0 && self.world.chunks.contains_key(&(cx, cz)) - }) - .collect(); + } + let (spawnable, mut chunks) = self.spawning_chunks(&players); + for &key in &chunks { + *self.world.inhabited.entry(key).or_insert(0) += 1; + } let spawn_mobs = self.ui.rules.bool("spawn_mobs"); let spawn_enemies = self.ui.rules.bool("spawn_monsters"); let mut random = std::mem::replace(&mut self.world.random, LegacyRandom::new(0)); @@ -335,12 +432,14 @@ impl<'a> Game<'a> { persistent: m.persistent, }) .collect(); - let players = vec![SpawnPlayer { - position: [p.x, p.y, p.z], - spectator, - }]; - // A single player's natural spawn chunk tracker covers 17 x 17 chunks. - let mut state = SpawnState::new(&level, 289, players, &counted, |cx, cz| { + let players = players + .iter() + .map(|p| SpawnPlayer { + position: [p.position.x, p.position.y, p.position.z], + spectator: p.spectator, + }) + .collect(); + let mut state = SpawnState::new(&level, spawnable, players, &counted, |cx, cz| { self.world.chunks.contains_key(&(cx, cz)) }); let categories = if spawn_mobs { diff --git a/crates/horizon-client/src/game/nautili.rs b/crates/horizon-client/src/game/nautili.rs index c039b7d9a..e7f16ca18 100644 --- a/crates/horizon-client/src/game/nautili.rs +++ b/crates/horizon-client/src/game/nautili.rs @@ -11,7 +11,7 @@ use horizon_core::random::RandomSource; impl Game<'_> { fn item_tagged(&self, tag: &str, item: &str) -> bool { self.res - .items + .items() .item_tags .get(tag) .is_some_and(|t| t.contains(item)) @@ -43,7 +43,7 @@ impl Game<'_> { } let taming: Vec = self .res - .items + .items() .item_tags .get("minecraft:nautilus_taming_items") .map(|t| t.iter().cloned().collect()) diff --git a/crates/horizon-client/src/game/nether_portal.rs b/crates/horizon-client/src/game/nether_portal.rs index 01a657b0b..a30cf822a 100644 --- a/crates/horizon-client/src/game/nether_portal.rs +++ b/crates/horizon-client/src/game/nether_portal.rs @@ -65,6 +65,8 @@ pub(super) struct Portals { arrivals: Vec, loaded: bool, dirty: bool, + /// Per-chunk portal palette masks, kept across ticks (portal_sections). + pub(super) sections: horizon_client::portal_sections::PortalSections, } fn rotate_y(v: Vec3, degrees: f32) -> Vec3 { @@ -92,13 +94,13 @@ impl Game<'_> { } } /// `ServerLevel.isAllowedToEnterPortal`. - fn may_enter(&self, target: Dim) -> bool { + pub(super) fn may_enter(&self, target: Dim) -> bool { target != Dim::Nether || self.ui.rules.bool("allow_entering_nether_using_portals") } /// The plan of an entity of `width` by `height` at `at` going through the /// portal at `entry` (`getDimensionTransitionFromExit` and the scaled, /// clamped exit of `getPortalDestination`). - fn portal_plan( + pub(super) fn portal_plan( &self, entry: BlockPos, at: Vec3, @@ -112,7 +114,7 @@ impl Game<'_> { Dim::Nether }; let state = self.world.block(entry.x, entry.y, entry.z); - let b = &self.res.behaviours; + let b = self.res.behaviours(); let (entry_x_axis, offset) = match b.portal_axis(state) { Some(x_axis) => { let area = largest_rectangle_around( @@ -150,7 +152,7 @@ impl Game<'_> { /// (the points of interest `PortalForcer` searches): loaded chunks as /// they are, the others as saved; chunks never generated have none. fn portal_blocks(&self, center: BlockPos, radius: i32) -> Vec { - let b = &self.res.behaviours; + let b = self.res.behaviours(); let chunk_radius = radius.div_euclid(16) + 1; let (cx, cz) = (center.x >> 4, center.z >> 4); let mut out = Vec::new(); @@ -161,7 +163,7 @@ impl Game<'_> { Some(c) => &**c, None => match self .save - .load(x, z, self.pack.assets.registry, &self.res.biomes) + .load(x, z, self.pack.assets.registry, self.res.biomes()) { Some(c) => { saved = c; @@ -195,7 +197,7 @@ impl Game<'_> { } /// The exit portal of `plan` in the loaded dimension: the closest one /// within reach, else one built by `PortalForcer.createPortal`. - fn nether_exit_portal(&mut self, plan: &Plan) -> Option { + pub(super) fn nether_exit_portal(&mut self, plan: &Plan) -> Option { let to_nether = plan.dim == Dim::Nether; let radius = if to_nether { 16 } else { 128 }; let border = self.border_bounds(); @@ -211,7 +213,7 @@ impl Game<'_> { ); if let Some(found) = found { let state = self.world.block(found.x, found.y, found.z); - let x_axis = self.res.behaviours.portal_axis(state).unwrap_or(true); + let x_axis = self.res.behaviours().portal_axis(state).unwrap_or(true); return Some(largest_rectangle_around( found, if x_axis { Axis::X } else { Axis::Z }, @@ -231,8 +233,8 @@ impl Game<'_> { } /// `NetherPortalBlock.createDimensionTransition`: the arrival position /// and the yaw turned by. - fn arrival(&self, plan: &Plan, exit: FoundRectangle) -> (Vec3, f32) { - let b = &self.res.behaviours; + pub(super) fn arrival(&self, plan: &Plan, exit: FoundRectangle) -> (Vec3, f32) { + let b = self.res.behaviours(); let corner = exit.min_corner; let exit_x_axis = b .portal_axis(self.world.block(corner.x, corner.y, corner.z)) @@ -296,6 +298,12 @@ impl Game<'_> { else { return Ok(()); }; + self.player_through_nether_portal(entry) + } + /// `Entity.handlePortal` once the player's portal process at `entry` + /// finished: `NetherPortalBlock.getPortalDestination`, then + /// `teleportToPortalDestination` (`isAllowedToEnterPortal`). + pub(super) fn player_through_nether_portal(&mut self, entry: BlockPos) -> Result<()> { let Some(p) = self.player.as_ref() else { return Ok(()); }; @@ -312,8 +320,16 @@ impl Game<'_> { self.portals.travel = Some(plan); self.end_portal_travel = false; self.enter_dimension(plan.dim)?; - // The destination is all on disk now: load around the portal its - // points of interest lead to, or the approximate exit. + let centre = self.arrival_centre(&plan); + if let Some((_, target)) = self.travelling.as_mut() { + *target = centre; + } + Ok(()) + } + /// Where to wait for the destination of `plan` to load, once its level + /// is open: the portal its points of interest lead to, or the + /// approximate exit. + pub(super) fn arrival_centre(&self, plan: &Plan) -> Vec3 { let to_nether = plan.dim == Dim::Nether; let portals = self.portal_blocks(plan.approximate, if to_nether { 16 } else { 128 }); let (min_section, max_section) = if to_nether { (0, 15) } else { (-4, 19) }; @@ -327,14 +343,11 @@ impl Game<'_> { max_section, ); let centre = found.unwrap_or(plan.approximate); - if let Some((_, target)) = self.travelling.as_mut() { - *target = Vec3::new( - f64::from(centre.x) + 0.5, - f64::from(centre.y), - f64::from(centre.z) + 0.5, - ); - } - Ok(()) + Vec3::new( + f64::from(centre.x) + 0.5, + f64::from(centre.y), + f64::from(centre.z) + 0.5, + ) } /// The player's arrival once the destination has loaded around the /// approximate exit: the waiting entities first, then the exit portal @@ -347,10 +360,23 @@ impl Game<'_> { ) -> Result<()> { self.resolve_arrivals(); let plan = self.portals.travel.take(); - let exit = plan.as_ref().and_then(|plan| self.nether_exit_portal(plan)); - let (position, rotation) = match (plan, exit) { - (Some(plan), Some(exit)) => self.arrival(&plan, exit), - _ => (target, 0.0), + let Some(plan) = plan else { + // A command teleport can change dimensions without going through + // a portal. It has no portal placement, rotation, or sound effect. + player.position = target; + player.velocity = Vec3::ZERO; + player.fall_distance = 0.0; + self.previous_position = target; + self.player = Some(player); + self.pad.player_replaced(); + self.sway = Default::default(); + self.apply_equipment(); + return Ok(()); + }; + let exit = self.nether_exit_portal(&plan); + let (position, rotation) = match exit { + Some(exit) => self.arrival(&plan, exit), + None => (target, 0.0), }; player.position = position; // Relative.ROTATION and Relative.DELTA: the yaw turns, the motion @@ -481,12 +507,14 @@ impl Game<'_> { .filter(|m| self.mob_can_use_portal(m)) .map(|m| (m.id, m.position, m.width(), m.height())) .collect(); + let mut portal_sections = std::mem::take(&mut self.portals.sections); for (id, to, width, height) in items.into_iter().chain(mobs) { let from = self.portals.before.get(&id).copied().unwrap_or(to); - if let Some(pos) = self.swept_portal(from, to, width, height) { + if let Some(pos) = self.swept_portal(from, to, width, height, &mut portal_sections) { inside.push((id, pos)); } } + self.portals.sections = portal_sections; for (id, pos) in inside { self.portals .entities @@ -497,7 +525,14 @@ impl Game<'_> { } /// The first nether portal block an entity box swept through /// (`Entity.checkInsideBlocks`, whose first portal sets the entry). - fn swept_portal(&self, from: Vec3, to: Vec3, width: f32, height: f32) -> Option { + fn swept_portal( + &self, + from: Vec3, + to: Vec3, + width: f32, + height: f32, + portal_sections: &mut horizon_client::portal_sections::PortalSections, + ) -> Option { let half = f64::from(width) / 2.0; let bounds = horizon_core::geometry::Aabb::new( Vec3::new(to.x - half, to.y, to.z - half), @@ -508,6 +543,13 @@ impl Game<'_> { f64::from(1.0e-5_f32), f64::from(1.0e-5_f32), )); + // Every block the sweep visits lies in the box covering both ends. + let reach = bounds + .union_bounds(bounds.move_by(from.subtract(to))) + .inflate(Vec3::new(1.0, 1.0, 1.0)); + if !self.may_hold_portal(reach, portal_sections) { + return None; + } let mut found = None; let _ = horizon_core::geometry::sweep_intersected_blocks( from, @@ -517,7 +559,7 @@ impl Game<'_> { |p, _| { if self .res - .behaviours + .behaviours() .portal_axis(self.world.block(p.x, p.y, p.z)) .is_some() { @@ -529,6 +571,53 @@ impl Game<'_> { ); found } + /// Whether a section `reach` touches has a nether portal state in its + /// palette: without one, no block there is a portal. Palette answers are + /// kept per chunk across ticks (`portal_sections`); a box over many + /// sections is not checked. + fn may_hold_portal( + &self, + reach: horizon_core::geometry::Aabb, + sections: &mut horizon_client::portal_sections::PortalSections, + ) -> bool { + use horizon_client::chunk::{MIN_Y, SECTIONS}; + let (min, max) = (reach.min_position(), reach.max_position()); + if !min.is_finite() || !max.is_finite() { + return true; + } + let (min, max) = (BlockPos::containing(min), BlockPos::containing(max)); + let top = SECTIONS as i32 - 1; + let (x0, x1) = (min.x >> 4, max.x >> 4); + let (z0, z1) = (min.z >> 4, max.z >> 4); + let (y0, y1) = ( + ((min.y - MIN_Y) >> 4).clamp(0, top), + ((max.y - MIN_Y) >> 4).clamp(0, top), + ); + let count = (i64::from(x1) - i64::from(x0) + 1) + * (i64::from(y1) - i64::from(y0) + 1) + * (i64::from(z1) - i64::from(z0) + 1); + if count > 64 { + return true; + } + let mut y_mask = 0u32; + for sy in y0..=y1 { + y_mask |= 1 << sy; + } + let behaviours = self.res.behaviours(); + let chunks = &self.world.chunks; + (x0..=x1).any(|cx| { + (z0..=z1).any(|cz| { + chunks.get(&(cx, cz)).is_some_and(|chunk| { + sections.get((cx, cz), chunk, |v| { + behaviours + .portal_axis(horizon_world::registry::BlockStateId(v)) + .is_some() + }) & y_mask + != 0 + }) + }) + }) + } /// `Entity.teleportCrossDimension` away from the loaded dimension: the /// entity is saved and removed, and waits for its dimension to load. fn send_through_portal(&mut self, id: u32, entry: BlockPos) { diff --git a/crates/horizon-client/src/game/options.rs b/crates/horizon-client/src/game/options.rs index ed0dccb06..12efde5be 100644 --- a/crates/horizon-client/src/game/options.rs +++ b/crates/horizon-client/src/game/options.rs @@ -111,7 +111,7 @@ impl Game<'_> { } pub(super) fn draw_options(&mut self, batch: &mut HudBatch) { let mut ui = std::mem::take(&mut self.ui.options); - ui.draw(&self.settings, batch, &self.atlas, self.cursor); + ui.draw(&self.settings, batch, &self.gui.atlas, self.cursor); self.ui.options = ui; } pub(super) fn options_mouse(&mut self, pressed: bool, right: bool) -> Result<()> { @@ -133,7 +133,7 @@ impl Game<'_> { let mut ui = std::mem::take(&mut self.ui.options); let events = ui.click( &mut self.settings, - &self.atlas, + &self.gui.atlas, self.cursor.0, self.cursor.1, size, @@ -152,7 +152,7 @@ impl Game<'_> { let mut ui = std::mem::take(&mut self.ui.options); let events = ui.click( &mut self.settings, - &self.atlas, + &self.gui.atlas, self.cursor.0, self.cursor.1, size, @@ -165,7 +165,7 @@ impl Game<'_> { } pub(super) fn options_key(&mut self, key: KeyCode, text: Option<&str>) -> Result<()> { let mut ui = std::mem::take(&mut self.ui.options); - let events = ui.key(&mut self.settings, &self.atlas, key, text); + let events = ui.key(&mut self.settings, &self.gui.atlas, key, text); self.ui.options = ui; self.handle_option_events(events) } @@ -176,7 +176,7 @@ impl Game<'_> { ) -> bool { self.ui .options - .shortcut(&self.atlas, shortcut, &mut self.clipboard) + .shortcut(&self.gui.atlas, shortcut, &mut self.clipboard) } pub(super) fn options_scroll(&mut self, amount: f32) { let size = self.gui_size(); @@ -225,6 +225,11 @@ impl Game<'_> { let _ = std::fs::create_dir_all(&dir); super::packs::open_folder(&dir); } + Event::OpenModFolder => { + let dir = Self::options_path().with_file_name(horizon_client::mod_list::DIR); + let _ = std::fs::create_dir_all(&dir); + super::packs::open_folder(&dir); + } } } Ok(()) @@ -233,7 +238,7 @@ impl Game<'_> { pub(super) fn load_language(&mut self, code: &str) { let root = self.options.resources.clone(); lang::set(lang::Language::load(&[&root], code)); - self.ui.chat.rescale(&self.atlas); + self.ui.chat.rescale(&self.gui.atlas); } /// Settings that Horizon applies beyond render distance, volume and GUI scale. pub(super) fn apply_ui_settings(&mut self) { @@ -264,14 +269,14 @@ impl Game<'_> { }; self.ui.chat.options = chat; if rescale { - self.ui.chat.rescale(&self.atlas); + self.ui.chat.rescale(&self.gui.atlas); } - let font = &mut self.atlas.font; + let font = &mut self.gui.atlas.font; if font.uniform != s.force_unicode || font.japanese != s.japanese_glyphs { font.uniform = s.force_unicode; font.japanese = s.japanese_glyphs; font.reset_cache(); - self.ui.chat.rescale(&self.atlas); + self.ui.chat.rescale(&self.gui.atlas); } // A new renderer (resource reload, first window) takes the loaded pack. let stale = self diff --git a/crates/horizon-client/src/game/packs.rs b/crates/horizon-client/src/game/packs.rs index 0b95dd2ad..a5605977c 100644 --- a/crates/horizon-client/src/game/packs.rs +++ b/crates/horizon-client/src/game/packs.rs @@ -74,6 +74,15 @@ fn vanilla_info() -> PackInfo { fixed: false, } } +/// Shared by the startup menu, which has no world resources yet. +pub(super) fn pack_infos(vanilla: &Path) -> Vec { + let mut infos: Vec = packs::discover(vanilla, &packs_dir()) + .iter() + .map(info) + .collect(); + infos.push(vanilla_info()); + infos +} impl Game<'_> { fn vanilla_root(&self) -> PathBuf { if self.ui.vanilla_root.as_os_str().is_empty() { @@ -85,9 +94,7 @@ impl Game<'_> { /// The packs the pack screen offers and the current selection /// (`PackSelectionScreen` makes its `PackSelectionModel` from them). pub(super) fn refresh_pack_lists(&mut self) { - let all = packs::discover(&self.vanilla_root(), &packs_dir()); - let mut infos: Vec = all.iter().map(info).collect(); - infos.push(vanilla_info()); + let infos = pack_infos(&self.vanilla_root()); let o = &mut self.ui.options; o.pack_infos = infos; o.pack_selection = self.settings.resource_packs.clone(); @@ -100,6 +107,12 @@ impl Game<'_> { /// particle and font textures), item models, sounds, languages, GUI /// metadata and the cloud and world border textures. pub(super) fn reload_packs(&mut self) { + // Pack reload updates tail-owned mob metadata. Keep menus responsive + // until those resources exist, then apply the latest selection once. + if self.rest_loader.is_some() { + self.pending_pack_reload = true; + return; + } let vanilla = self.vanilla_root(); if self.ui.vanilla_root.as_os_str().is_empty() { self.ui.vanilla_root = vanilla.clone(); @@ -119,14 +132,19 @@ impl Game<'_> { } } fn reload_resources(&mut self, root: &Path) -> horizon_client::Result<()> { - let assets = std::sync::Arc::new(self.pack.assets.reload(root)?); + let render = std::sync::Arc::new(self.pack.assets.reload(root)?); let mut atlas = HudAtlas::build(root)?; atlas.font.uniform = self.settings.force_unicode; atlas.font.japanese = self.settings.japanese_glyphs; + let gui = super::UiResources { + atlas, + panorama: std::sync::Arc::new(horizon_client::gfx::panorama::Faces::load(root)), + }; let pack = super::PackResources::load( root, - assets, - &self.res.facts, + self.pack.assets.clone(), + render, + self.res.facts(), self.pack.context.biomes.clone(), self.pack.context.light.clone(), )?; @@ -137,7 +155,7 @@ impl Game<'_> { let renderer = match self .window .clone() - .map(|window| self.renderer_for(window, &pack, &atlas)) + .map(|window| self.renderer_for(window, &pack, &gui)) .transpose() { Ok(renderer) => renderer, @@ -154,15 +172,16 @@ impl Game<'_> { }; // Everything loaded: switch over. self.random_tick_sections.clear(); + self.portals.sections.clear(); self.options.resources = root.to_path_buf(); let context = pack.context.clone(); self.pack = pack; - self.atlas = atlas; + self.gui = gui; horizon_client::ui_theme::reset_atlas(); // Villager hats come from the pack's texture metadata (shared data, swapped in place). *self .res - .mob_data + .mob_data() .villager_hats .write() .unwrap_or_else(std::sync::PoisonError::into_inner) = @@ -170,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 @@ -178,7 +198,7 @@ impl Game<'_> { self.renderer = Some(renderer); self.apply_ui_settings(); } - self.ui.chat.rescale(&self.atlas); + self.ui.chat.rescale(&self.gui.atlas); Ok(()) } } diff --git a/crates/horizon-client/src/game/pad.rs b/crates/horizon-client/src/game/pad.rs index 62d481b11..9a71bdc75 100644 --- a/crates/horizon-client/src/game/pad.rs +++ b/crates/horizon-client/src/game/pad.rs @@ -6,12 +6,12 @@ use super::{Game, Screen}; use horizon_client::{ Result, - gamepad::{self, Button, Edges, GlyphStyle, HealthWatch, PadState, Rumble, TriggerFeel}, + gamepad::{self, Button, Edges, GlyphStyle, HealthWatch, PadState, Rumble}, keybinds::{KeyBinds, key_code, mouse_button}, mouse::Rotation, }; use serde_json::{Value, json}; -use std::time::{Duration, Instant}; +use std::time::Instant; use winit::{event_loop::ActiveEventLoop, keyboard::KeyCode}; pub(super) const HELP: &[&str] = &[ @@ -25,10 +25,9 @@ pub(super) const HELP: &[&str] = &[ /// The controllers and the state the game last acted on. #[derive(Default)] pub(super) struct Pads { - gilrs: Option, - started: bool, - /// The pad last touched; only its input counts. - active: Option, + /// The real controllers and the DualSense's output. + #[cfg(feature = "controllers")] + devices: hardware::Devices, /// The virtual pad of `horizon-ctl pad`. injected: PadState, /// The virtual pad was in use at the last poll. @@ -48,49 +47,38 @@ pub(super) struct Pads { pub(super) style: GlyphStyle, /// The cursor the pad drives in menus. pub(super) menu: super::pad_menu::MenuCursor, - /// Vibrations playing (an effect stops when dropped) and when they end. - rumbles: Vec<(gilrs::ff::Effect, Instant)>, /// The last vibration asked for, for `pad status`. last_rumble: Option<(&'static str, gamepad::Pulse)>, /// The player's health between polls (a drop is damage taken). health: HealthWatch, - /// The active pad is a DualSense. - dualsense: bool, - /// Its own output over HID (rumble here; gilrs only reads it), started once this window - /// has the pad in hand; dropped, it releases the pad. - output: Option, - /// Its HD haptics, on USB: looked for once each time the output opens. - haptics: Option, - haptics_tried: bool, - /// What it was last told to show. - shown: Option, } -fn gilrs_button(button: Button) -> gilrs::Button { - use gilrs::Button as G; - match button { - Button::South => G::South, - Button::East => G::East, - Button::West => G::West, - Button::North => G::North, - Button::LeftBumper => G::LeftTrigger, - Button::RightBumper => G::RightTrigger, - Button::LeftTrigger => G::LeftTrigger2, - Button::RightTrigger => G::RightTrigger2, - Button::Select => G::Select, - Button::Start => G::Start, - Button::LeftStick => G::LeftThumb, - Button::RightStick => G::RightThumb, - Button::DPadUp => G::DPadUp, - Button::DPadDown => G::DPadDown, - Button::DPadLeft => G::DPadLeft, - Button::DPadRight => G::DPadRight, +#[cfg(feature = "controllers")] +mod hardware; + +/// A build without the `controllers` feature (the server build, which must not need libudev): no +/// hardware pads, only `horizon-ctl pad`'s virtual one. +#[cfg(not(feature = "controllers"))] +mod hardware { + use super::{super::Game, Pads}; + use horizon_client::gamepad::{PadState, Pulse, Rumble}; + use serde_json::{Value, json}; + + impl Pads { + pub(super) fn read(&mut self) -> PadState { + PadState::default() + } } -} -fn is_dualsense(pad: &gilrs::Gamepad) -> bool { - matches!((pad.vendor_id(), pad.product_id()), - (Some(vendor), Some(product)) if horizon_dualsense::is_dualsense(vendor, product)) + impl Game<'_> { + pub(super) fn pad_output_off(&mut self) {} + pub(super) fn pad_output_frame(&mut self) {} + pub(in crate::game) fn pad_close_output(&mut self) {} + pub(super) fn pad_vibrate(&mut self, _: Rumble, _: Pulse) {} + pub(super) fn pad_devices_status(&self) -> Value { + json!({ "backend": false, "controllers": [] }) + } + } } impl Pads { @@ -99,93 +87,6 @@ impl Pads { pub(super) fn player_replaced(&mut self) { self.health.forget(); } - /// Open gilrs on first use. A platform without it (or a Linux without - /// udev) leaves controllers off; the game runs as before. - fn start(&mut self) { - if self.started { - return; - } - self.started = true; - self.gilrs = match gilrs::Gilrs::new() { - Ok(g) => { - for (_, pad) in g.gamepads() { - log::info!("Controller connected: {}", pad.name()); - } - Some(g) - } - Err(gilrs::Error::NotImplemented(_)) => None, - Err(e) => { - log::warn!("Horizon: controllers are unavailable: {e}"); - None - } - }; - } - - /// Drain gilrs' events and read the active pad. - fn read(&mut self) -> PadState { - let Some(gilrs) = self.gilrs.as_mut() else { - return PadState::default(); - }; - while let Some(gilrs::Event { id, event, .. }) = gilrs.next_event() { - use gilrs::EventType as E; - match event { - E::Connected => { - log::info!("Controller connected: {}", gilrs.gamepad(id).name()); - if let Some(output) = &self.output { - output.scan(); - } - } - E::Disconnected => { - log::info!("Controller disconnected: {}", gilrs.gamepad(id).name()); - if self.active == Some(id) { - self.active = None; - } - if let Some(output) = &self.output { - output.scan(); - } - } - E::ButtonPressed(..) => self.active = Some(id), - E::AxisChanged(_, value, _) | E::ButtonChanged(_, value, _) - if value.abs() > 0.5 => - { - self.active = Some(id) - } - _ => {} - } - } - let Some(pad) = self.active.and_then(|id| gilrs.connected_gamepad(id)) else { - // Before any pad is touched (the virtual pad playing), a DualSense still shows it. - self.dualsense = gilrs.gamepads().any(|(_, pad)| is_dualsense(&pad)); - return PadState::default(); - }; - self.style = GlyphStyle::of_vendor(pad.vendor_id()); - self.dualsense = is_dualsense(&pad); - let mut state = PadState::default(); - for button in Button::ALL { - if !matches!(button, Button::LeftTrigger | Button::RightTrigger) { - state.set(button, pad.is_pressed(gilrs_button(button))); - } - } - let axis = |a| pad.value(a); - use gilrs::Axis as A; - state.left = [axis(A::LeftStickX), axis(A::LeftStickY)]; - state.right = [axis(A::RightStickX), axis(A::RightStickY)]; - // Triggers arrive as analog buttons on most mappings, as plain - // buttons on some. - let trigger = |b: gilrs::Button| { - let value = pad.button_data(b).map_or(0.0, |d| d.value()); - if pad.is_pressed(b) { - value.max(1.0) - } else { - value - } - }; - state.triggers = [ - trigger(gilrs::Button::LeftTrigger2), - trigger(gilrs::Button::RightTrigger2), - ]; - state - } } impl Game<'_> { @@ -193,9 +94,7 @@ impl Game<'_> { pub(super) fn poll_pads(&mut self, event_loop: &ActiveEventLoop) { let settings = &self.settings.controller; if !settings.enabled { - // Dropping the output frees the triggers. - self.pad.output = None; - self.pad.shown = None; + self.pad_output_off(); if self.pad.state != PadState::default() { self.pad.state = PadState::default(); for (button, down) in self.pad.edges.release_all() { @@ -204,7 +103,6 @@ impl Game<'_> { } return; } - self.pad.start(); let hardware = self.pad.read(); self.ui.options.pad_style = self.settings.controller.glyphs.resolve(self.pad.style); let state = hardware.merge(&self.pad.injected); @@ -219,11 +117,7 @@ impl Game<'_> { self.pad.in_use = true; } self.pad.state = state; - if self.pad.dualsense && self.pad.in_use && self.focused && self.pad.output.is_none() { - self.pad.output = Some(horizon_dualsense::Link::start()); - } - self.pad_haptics(); - self.pad_show(); + self.pad_output_frame(); // Damage from any source, here or on a server; nothing counts on // the title screen. let health = self @@ -234,8 +128,6 @@ impl Game<'_> { if let Some(damage) = self.pad.health.observe(health) { self.pad_rumble(Rumble::Hurt(damage)); } - let now = Instant::now(); - self.pad.rumbles.retain(|(_, end)| *end > now); let ticking = self.title.is_none() && self.screen == Screen::Playing && !self.pause; for button in std::mem::take(&mut self.pad.late_releases) { if !ticking || self.pad.pressed_at[button as usize] != Some(self.ticks) { @@ -365,13 +257,6 @@ impl Game<'_> { self.close_book(); return Ok(()); } - if let Some(remote) = self.remote.as_mut() - && remote.chat_input.is_some() - { - remote.chat_input = None; - self.capture(true); - return Ok(()); - } self.key_pressed(KeyCode::Escape) } @@ -411,150 +296,8 @@ impl Game<'_> { self.pitch = rotation.x_rot; } - /// The DualSense's HID output, while it is the active pad, open, and this window has focus - /// (another game on the same pad keeps it otherwise). - fn pad_output(&self) -> Option<&horizon_dualsense::Link> { - let output = self.pad.output.as_ref()?; - (self.pad.dualsense && self.focused && output.open().is_some()).then_some(output) - } - - /// What the DualSense shows: while playing, its triggers feel what their actions do; in - /// menus, paused, unfocused or with another pad active, they are free. - fn pad_show(&mut self) { - if self.pad.output.is_none() { - return; - } - let playing = self.pad.dualsense - && self.focused - && self.title.is_none() - && self.screen == Screen::Playing - && !self.pause; - let (using, attacking) = if playing { - self.trigger_feels() - } else { - (TriggerFeel::Free, TriggerFeel::Free) - }; - // The lightbar follows the player; menus keep the last colour. - let in_world = self.title.is_none(); - let lightbar = match self.player.as_mut().filter(|_| in_world) { - Some(p) => { - let max = p.attribute("minecraft:max_health").unwrap_or(20.0) as f32; - let health = p.health.health() / max.max(1.0); - self.settings - .controller - .lightbar - .colour(health, self.dim.id()) - } - None => self.pad.shown.and_then(|s| s.lightbar), - }; - let lights = self.settings.controller.lightbar != gamepad::Lightbar::Off; - let state = horizon_dualsense::device::State { - triggers: self.settings.controller.trigger_effects(using, attacking), - lightbar: lightbar.filter(|_| lights), - player_leds: lights.then_some(horizon_dualsense::PLAYER_1), - }; - if self.pad.shown != Some(state) - && let Some(output) = &self.pad.output - { - output.set(state); - self.pad.shown = Some(state); - } - } - - /// What `key.use` and `key.attack` are doing, for the triggers bound to them. - fn trigger_feels(&self) -> (TriggerFeel, TriggerFeel) { - let using = match self.using { - Some((slot, ticks)) => match self.stack_in(slot) { - Some(s) if &*s.item == "minecraft:bow" => { - TriggerFeel::Draw(gamepad::bow_power(ticks)) - } - // TridentItem.THROW_THRESHOLD_TIME. - Some(s) if &*s.item == "minecraft:trident" => { - TriggerFeel::Draw(ticks as f32 / 10.0) - } - Some(s) if &*s.item == "minecraft:crossbow" => { - TriggerFeel::Draw(ticks as f32 / Self::charge_ticks(s).max(1) as f32) - } - Some(s) if self.blocks_attacks(s).is_some() => TriggerFeel::Brace, - _ => TriggerFeel::Free, - }, - None => self.idle_use_feel(), - }; - // Creative breaks at once; blocks that can't be broken have a negative hardness. - let attacking = match &self.mining { - Some(m) if !self.creative => { - let state = self.world.block(m.pos[0], m.pos[1], m.pos[2]); - TriggerFeel::Texture(self.res.facts.get(state).hardness) - } - _ => TriggerFeel::Free, - }; - (using, attacking) - } - - /// The use trigger while nothing is in use: a loaded crossbow clicks, an item that boosts (a - /// rocket while gliding, the stick of the mount being ridden) catches, a shield weighs, and - /// locks while an axe has disabled it. - fn idle_use_feel(&self) -> TriggerFeel { - let gliding = self.player.as_ref().is_some_and(|p| p.fall_flying); - let mount = self - .riding - .and_then(|id| self.mobs.iter().find(|m| m.id == id)); - let mount = mount.map_or("", |m| m.kind.id.as_str()); - let feel = |stack: Option<&horizon_client::items::ItemStack>| match stack - .map(|s| (s, &*s.item)) - { - Some((s, "minecraft:crossbow")) if s.data().is_some_and(|d| !d.charged.is_empty()) => { - TriggerFeel::Click - } - Some((_, "minecraft:firework_rocket")) if gliding => TriggerFeel::Boost, - Some((_, "minecraft:carrot_on_a_stick")) if mount == "minecraft:pig" => { - TriggerFeel::Boost - } - Some((_, "minecraft:warped_fungus_on_a_stick")) if mount == "minecraft:strider" => { - TriggerFeel::Boost - } - Some((s, _)) if self.blocks_attacks(s).is_some() => match self.shield_cooldown { - 0 => TriggerFeel::Brace, - _ => TriggerFeel::Locked, - }, - _ => TriggerFeel::Free, - }; - // Use tries the main hand first. - match feel(self.stack_in(self.selected)) { - TriggerFeel::Free => feel(self.offhand.as_ref()), - main => main, - } - } - - /// Open the pad's haptics once its output is open over USB (its audio device is there - /// only then); close them when it goes. Opt-in with `HORIZON_DUALSENSE_HAPTICS=1` until - /// they are proven on a pad (docs/controllers.md); never for games without sound (tests). - fn pad_haptics(&mut self) { - let usb = self.pad.output.as_ref().and_then(|o| o.open()) - == Some(horizon_dualsense::Transport::Usb); - if !usb { - self.pad.haptics = None; - self.pad.haptics_tried = false; - } else if !self.pad.haptics_tried { - self.pad.haptics_tried = true; - if std::env::var_os("HORIZON_DUALSENSE_HAPTICS").is_some_and(|v| v == "1") - && std::env::var_os("HORIZON_NO_AUDIO").is_none() - { - self.pad.haptics = horizon_dualsense::haptics::Haptics::open(); - } - } - } - - /// Release the DualSense's output (the game is quitting). - pub(super) fn pad_close_output(&mut self) { - self.pad.haptics = None; - self.pad.output = None; - } - /// Vibrate the active pad, when vibration is on and the pad is the one - /// playing (a pad lying idle while the mouse plays stays still): a - /// DualSense through its own report, else gilrs' force feedback, which - /// platforms without it (gilrs on macOS) skip. + /// playing (a pad lying idle while the mouse plays stays still). pub(super) fn pad_rumble(&mut self, rumble: Rumble) { let settings = &self.settings.controller; if !settings.enabled || !settings.vibration || !self.pad.in_use { @@ -564,68 +307,7 @@ impl Game<'_> { return; }; self.pad.last_rumble = Some((rumble.name(), pulse)); - let level = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8; - if let Some(haptics) = self - .pad - .haptics - .as_ref() - .filter(|_| self.pad_output().is_some()) - { - let steps = rumble.shape().into_iter().map(|step| { - ( - step.strong, - step.weak, - Duration::from_millis(u64::from(step.millis)), - ) - }); - haptics.play(&steps.collect::>()); - return; - } - if let Some(output) = self.pad_output() { - let steps = rumble.shape().into_iter().map(|step| { - let length = Duration::from_millis(u64::from(step.millis)); - ([level(step.strong), level(step.weak)], length) - }); - output.rumble(steps.collect()); - return; - } - let (Some(gilrs), Some(id)) = (self.pad.gilrs.as_mut(), self.pad.active) else { - return; - }; - if !gilrs - .connected_gamepad(id) - .is_some_and(|g| g.is_ff_supported()) - { - return; - } - use gilrs::ff::{BaseEffect, BaseEffectType, EffectBuilder, Repeat, Replay, Ticks}; - let length = Ticks::from_ms(pulse.millis); - let motor = |kind| BaseEffect { - kind, - scheduling: Replay { - play_for: length, - ..Default::default() - }, - envelope: Default::default(), - }; - let magnitude = |v: f32| (v.clamp(0.0, 1.0) * f32::from(u16::MAX)) as u16; - let effect = EffectBuilder::new() - .add_effect(motor(BaseEffectType::Strong { - magnitude: magnitude(pulse.strong), - })) - .add_effect(motor(BaseEffectType::Weak { - magnitude: magnitude(pulse.weak), - })) - .repeat(Repeat::For(length)) - .gamepads(&[id]) - .finish(gilrs); - match effect.and_then(|e| e.play().map(|()| e)) { - Ok(effect) => { - let end = Instant::now() + Duration::from_millis(u64::from(pulse.millis) + 50); - self.pad.rumbles.push((effect, end)); - } - Err(e) => log::debug!("Horizon: controller vibration: {e}"), - } + self.pad_vibrate(rumble, pulse); } /// `horizon-ctl pad ...`. @@ -646,32 +328,15 @@ impl Game<'_> { }; match arg(1)? { "status" => { - let pads: Vec = self.pad.gilrs.as_ref().map_or(Vec::new(), |g| { - g.gamepads() - .map(|(id, p)| { - json!({ - "name": p.name(), - "vendor": p.vendor_id(), - "product": p.product_id(), - "active": self.pad.active == Some(id), - }) - }) - .collect() - }); let s = &self.pad.state; let held: Vec<&str> = Button::ALL .into_iter() .filter(|b| self.pad.edges.is_held(*b)) .map(Button::name) .collect(); - Ok(json!({ + let mut status = json!({ "enabled": self.settings.controller.enabled, - "backend": self.pad.gilrs.is_some(), - "controllers": pads, "glyphs": self.pad.style.name(), - "dualsense_output": self.pad.output.as_ref().and_then(|o| o.open()) - .map(|t| format!("{t:?}")), - "trigger_effects": self.pad.shown.map(|s| s.triggers), "last_rumble": self.pad.last_rumble.map(|(why, p)| json!({ "reason": why, "strong": p.strong, "weak": p.weak, "ms": p.millis, })), @@ -681,7 +346,13 @@ impl Game<'_> { "left": s.left, "right": s.right, "triggers": s.triggers, - })) + }); + if let (Some(status), Value::Object(devices)) = + (status.as_object_mut(), self.pad_devices_status()) + { + status.extend(devices); + } + Ok(status) } "release" => { self.pad.injected = PadState::default(); diff --git a/crates/horizon-client/src/game/pad/hardware.rs b/crates/horizon-client/src/game/pad/hardware.rs new file mode 100644 index 000000000..b464ef3ee --- /dev/null +++ b/crates/horizon-client/src/game/pad/hardware.rs @@ -0,0 +1,409 @@ +//! The real controllers (the `controllers` feature): gilrs' pads, and the DualSense's own output +//! over HID: its rumble, adaptive triggers, lights and HD haptics. A build without the feature (the +//! server build) has the stub in pad.rs instead, and only `horizon-ctl pad`'s virtual pad. +use super::{ + super::{Game, Screen}, + Pads, +}; +use horizon_client::gamepad::{self, Button, GlyphStyle, PadState, Rumble, TriggerFeel}; +use serde_json::{Value, json}; +use std::time::{Duration, Instant}; + +/// The controllers gilrs sees and the DualSense's output. +#[derive(Default)] +pub(super) struct Devices { + gilrs: Option, + started: bool, + /// The pad last touched; only its input counts. + active: Option, + /// Vibrations playing (an effect stops when dropped) and when they end. + rumbles: Vec<(gilrs::ff::Effect, Instant)>, + /// The active pad is a DualSense. + dualsense: bool, + /// Its own output over HID (rumble here; gilrs only reads it), started once this window + /// has the pad in hand; dropped, it releases the pad. + output: Option, + /// Its HD haptics, on USB: looked for once each time the output opens. + haptics: Option, + haptics_tried: bool, + /// What it was last told to show. + shown: Option, +} + +fn gilrs_button(button: Button) -> gilrs::Button { + use gilrs::Button as G; + match button { + Button::South => G::South, + Button::East => G::East, + Button::West => G::West, + Button::North => G::North, + Button::LeftBumper => G::LeftTrigger, + Button::RightBumper => G::RightTrigger, + Button::LeftTrigger => G::LeftTrigger2, + Button::RightTrigger => G::RightTrigger2, + Button::Select => G::Select, + Button::Start => G::Start, + Button::LeftStick => G::LeftThumb, + Button::RightStick => G::RightThumb, + Button::DPadUp => G::DPadUp, + Button::DPadDown => G::DPadDown, + Button::DPadLeft => G::DPadLeft, + Button::DPadRight => G::DPadRight, + } +} + +fn is_dualsense(pad: &gilrs::Gamepad) -> bool { + matches!((pad.vendor_id(), pad.product_id()), + (Some(vendor), Some(product)) if horizon_dualsense::is_dualsense(vendor, product)) +} + +impl Pads { + /// Open gilrs on first use. A platform without it (or a Linux without + /// udev) leaves controllers off; the game runs as before. + fn start(&mut self) { + if self.devices.started { + return; + } + self.devices.started = true; + self.devices.gilrs = match gilrs::Gilrs::new() { + Ok(g) => { + for (_, pad) in g.gamepads() { + log::info!("Controller connected: {}", pad.name()); + } + Some(g) + } + Err(gilrs::Error::NotImplemented(_)) => None, + Err(e) => { + log::warn!("Horizon: controllers are unavailable: {e}"); + None + } + }; + } + + /// Drain gilrs' events and read the active pad (opening gilrs the first time). + pub(super) fn read(&mut self) -> PadState { + self.start(); + let Some(gilrs) = self.devices.gilrs.as_mut() else { + return PadState::default(); + }; + while let Some(gilrs::Event { id, event, .. }) = gilrs.next_event() { + use gilrs::EventType as E; + match event { + E::Connected => { + log::info!("Controller connected: {}", gilrs.gamepad(id).name()); + if let Some(output) = &self.devices.output { + output.scan(); + } + } + E::Disconnected => { + log::info!("Controller disconnected: {}", gilrs.gamepad(id).name()); + if self.devices.active == Some(id) { + self.devices.active = None; + } + if let Some(output) = &self.devices.output { + output.scan(); + } + } + E::ButtonPressed(..) => self.devices.active = Some(id), + E::AxisChanged(_, value, _) | E::ButtonChanged(_, value, _) + if value.abs() > 0.5 => + { + self.devices.active = Some(id) + } + _ => {} + } + } + let Some(pad) = self + .devices + .active + .and_then(|id| gilrs.connected_gamepad(id)) + else { + // Before any pad is touched (the virtual pad playing), a DualSense still shows it. + self.devices.dualsense = gilrs.gamepads().any(|(_, pad)| is_dualsense(&pad)); + return PadState::default(); + }; + self.style = GlyphStyle::of_vendor(pad.vendor_id()); + self.devices.dualsense = is_dualsense(&pad); + let mut state = PadState::default(); + for button in Button::ALL { + if !matches!(button, Button::LeftTrigger | Button::RightTrigger) { + state.set(button, pad.is_pressed(gilrs_button(button))); + } + } + let axis = |a| pad.value(a); + use gilrs::Axis as A; + state.left = [axis(A::LeftStickX), axis(A::LeftStickY)]; + state.right = [axis(A::RightStickX), axis(A::RightStickY)]; + // Triggers arrive as analog buttons on most mappings, as plain + // buttons on some. + let trigger = |b: gilrs::Button| { + let value = pad.button_data(b).map_or(0.0, |d| d.value()); + if pad.is_pressed(b) { + value.max(1.0) + } else { + value + } + }; + state.triggers = [ + trigger(gilrs::Button::LeftTrigger2), + trigger(gilrs::Button::RightTrigger2), + ]; + state + } +} + +impl Game<'_> { + /// Controllers are turned off: dropping the DualSense's output frees its triggers. + pub(super) fn pad_output_off(&mut self) { + self.pad.devices.output = None; + self.pad.devices.shown = None; + } + + /// After each poll: the DualSense's output opens once this window has the pad in hand, then + /// shows what the game is doing; vibrations that are over stop. + pub(super) fn pad_output_frame(&mut self) { + if self.pad.devices.dualsense + && self.pad.in_use + && self.focused + && self.pad.devices.output.is_none() + { + self.pad.devices.output = Some(horizon_dualsense::Link::start()); + } + self.pad_haptics(); + self.pad_show(); + let now = Instant::now(); + self.pad.devices.rumbles.retain(|(_, end)| *end > now); + } + + /// The DualSense's HID output, while it is the active pad, open, and this window has focus + /// (another game on the same pad keeps it otherwise). + fn pad_output(&self) -> Option<&horizon_dualsense::Link> { + let output = self.pad.devices.output.as_ref()?; + (self.pad.devices.dualsense && self.focused && output.open().is_some()).then_some(output) + } + + /// What the DualSense shows: while playing, its triggers feel what their actions do; in + /// menus, paused, unfocused or with another pad active, they are free. + fn pad_show(&mut self) { + if self.pad.devices.output.is_none() { + return; + } + let playing = self.pad.devices.dualsense + && self.focused + && self.title.is_none() + && self.screen == Screen::Playing + && !self.pause; + let (using, attacking) = if playing { + self.trigger_feels() + } else { + (TriggerFeel::Free, TriggerFeel::Free) + }; + // The lightbar follows the player; menus keep the last colour. + let in_world = self.title.is_none(); + let lightbar = match self.player.as_mut().filter(|_| in_world) { + Some(p) => { + let max = p.attribute("minecraft:max_health").unwrap_or(20.0) as f32; + let health = p.health.health() / max.max(1.0); + self.settings + .controller + .lightbar + .colour(health, self.dim.id()) + } + None => self.pad.devices.shown.and_then(|s| s.lightbar), + }; + let lights = self.settings.controller.lightbar != gamepad::Lightbar::Off; + let state = horizon_dualsense::device::State { + triggers: self.settings.controller.trigger_effects(using, attacking), + lightbar: lightbar.filter(|_| lights), + player_leds: lights.then_some(horizon_dualsense::PLAYER_1), + }; + if self.pad.devices.shown != Some(state) + && let Some(output) = &self.pad.devices.output + { + output.set(state); + self.pad.devices.shown = Some(state); + } + } + + /// What `key.use` and `key.attack` are doing, for the triggers bound to them. + fn trigger_feels(&self) -> (TriggerFeel, TriggerFeel) { + let using = match self.using { + Some((slot, ticks)) => match self.stack_in(slot) { + Some(s) if &*s.item == "minecraft:bow" => { + TriggerFeel::Draw(gamepad::bow_power(ticks)) + } + // TridentItem.THROW_THRESHOLD_TIME. + Some(s) if &*s.item == "minecraft:trident" => { + TriggerFeel::Draw(ticks as f32 / 10.0) + } + Some(s) if &*s.item == "minecraft:crossbow" => { + TriggerFeel::Draw(ticks as f32 / Self::charge_ticks(s).max(1) as f32) + } + Some(s) if self.blocks_attacks(s).is_some() => TriggerFeel::Brace, + _ => TriggerFeel::Free, + }, + None => self.idle_use_feel(), + }; + // Creative breaks at once; blocks that can't be broken have a negative hardness. + let attacking = match &self.mining { + Some(m) if !self.creative => { + let state = self.world.block(m.pos[0], m.pos[1], m.pos[2]); + TriggerFeel::Texture(self.res.facts().get(state).hardness) + } + _ => TriggerFeel::Free, + }; + (using, attacking) + } + + /// The use trigger while nothing is in use: a loaded crossbow clicks, an item that boosts (a + /// rocket while gliding, the stick of the mount being ridden) catches, a shield weighs, and + /// locks while an axe has disabled it. + fn idle_use_feel(&self) -> TriggerFeel { + let gliding = self.player.as_ref().is_some_and(|p| p.fall_flying); + let mount = self + .riding + .and_then(|id| self.mobs.iter().find(|m| m.id == id)); + let mount = mount.map_or("", |m| m.kind.id.as_str()); + let feel = |stack: Option<&horizon_client::items::ItemStack>| match stack + .map(|s| (s, &*s.item)) + { + Some((s, "minecraft:crossbow")) if s.data().is_some_and(|d| !d.charged.is_empty()) => { + TriggerFeel::Click + } + Some((_, "minecraft:firework_rocket")) if gliding => TriggerFeel::Boost, + Some((_, "minecraft:carrot_on_a_stick")) if mount == "minecraft:pig" => { + TriggerFeel::Boost + } + Some((_, "minecraft:warped_fungus_on_a_stick")) if mount == "minecraft:strider" => { + TriggerFeel::Boost + } + Some((s, _)) if self.blocks_attacks(s).is_some() => match self.shield_cooldown { + 0 => TriggerFeel::Brace, + _ => TriggerFeel::Locked, + }, + _ => TriggerFeel::Free, + }; + // Use tries the main hand first. + match feel(self.stack_in(self.selected)) { + TriggerFeel::Free => feel(self.offhand.as_ref()), + main => main, + } + } + + /// Open the pad's haptics once its output is open over USB (its audio device is there + /// only then); close them when it goes. Opt-in with `HORIZON_DUALSENSE_HAPTICS=1` until + /// they are proven on a pad (docs/controllers.md); never for games without sound (tests). + fn pad_haptics(&mut self) { + let usb = self.pad.devices.output.as_ref().and_then(|o| o.open()) + == Some(horizon_dualsense::Transport::Usb); + if !usb { + self.pad.devices.haptics = None; + self.pad.devices.haptics_tried = false; + } else if !self.pad.devices.haptics_tried { + self.pad.devices.haptics_tried = true; + if std::env::var_os("HORIZON_DUALSENSE_HAPTICS").is_some_and(|v| v == "1") + && std::env::var_os("HORIZON_NO_AUDIO").is_none() + { + self.pad.devices.haptics = horizon_dualsense::haptics::Haptics::open(); + } + } + } + + /// Release the DualSense's output (the game is quitting). + pub(in crate::game) fn pad_close_output(&mut self) { + self.pad.devices.haptics = None; + self.pad.devices.output = None; + } + + /// Play a vibration on the active pad: a DualSense through its own report, else gilrs' force + /// feedback, which platforms without it (gilrs on macOS) skip. + pub(super) fn pad_vibrate(&mut self, rumble: Rumble, pulse: gamepad::Pulse) { + let level = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8; + if let Some(haptics) = self + .pad + .devices + .haptics + .as_ref() + .filter(|_| self.pad_output().is_some()) + { + let steps = rumble.shape().into_iter().map(|step| { + ( + step.strong, + step.weak, + Duration::from_millis(u64::from(step.millis)), + ) + }); + haptics.play(&steps.collect::>()); + return; + } + if let Some(output) = self.pad_output() { + let steps = rumble.shape().into_iter().map(|step| { + let length = Duration::from_millis(u64::from(step.millis)); + ([level(step.strong), level(step.weak)], length) + }); + output.rumble(steps.collect()); + return; + } + let (Some(gilrs), Some(id)) = (self.pad.devices.gilrs.as_mut(), self.pad.devices.active) + else { + return; + }; + if !gilrs + .connected_gamepad(id) + .is_some_and(|g| g.is_ff_supported()) + { + return; + } + use gilrs::ff::{BaseEffect, BaseEffectType, EffectBuilder, Repeat, Replay, Ticks}; + let length = Ticks::from_ms(pulse.millis); + let motor = |kind| BaseEffect { + kind, + scheduling: Replay { + play_for: length, + ..Default::default() + }, + envelope: Default::default(), + }; + let magnitude = |v: f32| (v.clamp(0.0, 1.0) * f32::from(u16::MAX)) as u16; + let effect = EffectBuilder::new() + .add_effect(motor(BaseEffectType::Strong { + magnitude: magnitude(pulse.strong), + })) + .add_effect(motor(BaseEffectType::Weak { + magnitude: magnitude(pulse.weak), + })) + .repeat(Repeat::For(length)) + .gamepads(&[id]) + .finish(gilrs); + match effect.and_then(|e| e.play().map(|()| e)) { + Ok(effect) => { + let end = Instant::now() + Duration::from_millis(u64::from(pulse.millis) + 50); + self.pad.devices.rumbles.push((effect, end)); + } + Err(e) => log::debug!("Horizon: controller vibration: {e}"), + } + } + + /// The hardware part of `pad status`. + pub(super) fn pad_devices_status(&self) -> Value { + let devices = &self.pad.devices; + let pads: Vec = devices.gilrs.as_ref().map_or(Vec::new(), |g| { + g.gamepads() + .map(|(id, p)| { + json!({ + "name": p.name(), + "vendor": p.vendor_id(), + "product": p.product_id(), + "active": devices.active == Some(id), + }) + }) + .collect() + }); + json!({ + "backend": devices.gilrs.is_some(), + "controllers": pads, + "dualsense_output": devices.output.as_ref().and_then(|o| o.open()).map(|t| format!("{t:?}")), + "trigger_effects": devices.shown.map(|s| s.triggers), + }) + } +} diff --git a/crates/horizon-client/src/game/pad_menu.rs b/crates/horizon-client/src/game/pad_menu.rs index 8f34d2b3a..257586572 100644 --- a/crates/horizon-client/src/game/pad_menu.rs +++ b/crates/horizon-client/src/game/pad_menu.rs @@ -31,7 +31,7 @@ use winit::{ dpi::PhysicalPosition, event::{DeviceId, ElementState, MouseButton, MouseScrollDelta, TouchPhase, WindowEvent}, event_loop::ActiveEventLoop, - keyboard::{KeyCode, NamedKey}, + keyboard::KeyCode, }; /// D-pad auto-repeat: the first step at once, the next after a pause. @@ -859,7 +859,6 @@ impl Game<'_> { || (self.screen == Screen::Creative && self.creative_screen.tab().search) || self.recipe_search_focused() || self.station_typing() - || self.remote_chat_open() } /// Open the on-screen keyboard over the focused field: Horizon's, or @@ -890,7 +889,7 @@ impl Game<'_> { && self.pad.in_use && menu.open_polls >= 3 && self.pad_focus_mode() - && (self.screen == Screen::Chat || self.sign_edit.is_some() || self.remote_chat_open()) + && (self.screen == Screen::Chat || self.sign_edit.is_some()) { self.pad.menu.osk_dismissed = true; self.pad_open_keyboard(); @@ -901,10 +900,10 @@ impl Game<'_> { /// the field that has focus. fn pad_osk_output(&mut self, out: osk::Output) { use osk::Output; - let (key, named, text) = match &out { - Output::Text(t) => (None, None, Some(t.as_str())), - Output::Backspace => (Some(KeyCode::Backspace), Some(NamedKey::Backspace), None), - Output::Enter => (Some(KeyCode::Enter), Some(NamedKey::Enter), None), + let (key, text) = match &out { + Output::Text(t) => (None, Some(t.as_str())), + Output::Backspace => (Some(KeyCode::Backspace), None), + Output::Enter => (Some(KeyCode::Enter), None), Output::Close => { self.pad.menu.osk = None; self.pad.menu.osk_dismissed = true; @@ -933,8 +932,6 @@ impl Game<'_> { if key != Some(KeyCode::Enter) { self.station_text(text, backspace); } - } else if self.remote_chat_open() { - self.remote_chat_input(named, text); } else if self.screen == Screen::WorldOptions { if key != Some(KeyCode::Enter) { self.world_options_text(text); @@ -942,7 +939,7 @@ impl Game<'_> { } else if self.screen == Screen::Options { let result = match text { Some(t) => { - self.ui.options.type_text(&self.atlas, t); + self.ui.options.type_text(&self.gui.atlas, t); Ok(()) } None => self.options_key(key.expect("a key"), None), @@ -985,9 +982,8 @@ impl Game<'_> { .menu .focus .is_some_and(|f| f.center().1 > batch.height / 2.0) - || self.screen == Screen::Chat - || self.remote_chat_open(); - let mut ui = horizon_client::ui_theme::Ui::new(&self.atlas, batch, self.cursor); + || self.screen == Screen::Chat; + let mut ui = horizon_client::ui_theme::Ui::new(&self.gui.atlas, batch, self.cursor); keyboard.draw(&mut ui, at_top, self.ui.options.pad_style); ui.finish(); return; @@ -1002,7 +998,7 @@ impl Game<'_> { return; } // An arrow, outlined, pointing up-left at (x, y). - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let (x, y) = (x.floor(), y.floor()); const ROWS: [u8; 10] = [1, 2, 3, 4, 5, 6, 7, 4, 2, 1]; for (i, &w) in ROWS.iter().enumerate() { @@ -1047,7 +1043,7 @@ impl Game<'_> { }; if horizon { use horizon_client::ui_theme::{self as theme, Ui, color}; - let mut ui = Ui::new(&self.atlas, batch, self.cursor); + let mut ui = Ui::new(&self.gui.atlas, batch, self.cursor); let k = ui.batch.scale / ui.density; let (x, y, w, h) = (x * k, y * k, w * k, h * k); let radius = match focus.kind { @@ -1070,7 +1066,7 @@ impl Game<'_> { return; } if matches!(focus.kind, Kind::Row | Kind::Field) { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let white = rgba(255, 255, 255, 255); batch.fill(atlas, x, y, w, 1.0, white); batch.fill(atlas, x, y + h - 1.0, w, 1.0, white); diff --git a/crates/horizon-client/src/game/paintings.rs b/crates/horizon-client/src/game/paintings.rs index 2e5542ccc..321e59f7d 100644 --- a/crates/horizon-client/src/game/paintings.rs +++ b/crates/horizon-client/src/game/paintings.rs @@ -94,7 +94,7 @@ impl Game<'_> { for x in slo[0].floor() as i32..=shi[0].floor() as i32 { for y in slo[1].floor() as i32..=shi[1].floor() as i32 { for z in slo[2].floor() as i32..=shi[2].floor() as i32 { - let f = self.res.facts.get(self.world.block(x, y, z)); + let f = self.res.facts().get(self.world.block(x, y, z)); if !(f.legacy_solid || f.has_class("DiodeBlock")) { return false; } @@ -232,8 +232,16 @@ impl Game<'_> { } /// BlockAttachedEntity.tick: check survival every 100 ticks. pub(super) fn tick_paintings(&mut self) { + // ServerLevel.tick: only entities in entity-ticking chunks tick. + let ticking = self.entity_ticking_area(); let mut i = 0; while i < self.paintings.len() { + let (lo, hi) = self.paintings[i].bounds(); + let centre = [0, 2].map(|k| ((lo[k] + hi[k]) / 2.0).floor() as i32); + if !ticking(centre[0], centre[1]) { + i += 1; + continue; + } self.paintings[i].check_interval += 1; if self.paintings[i].check_interval >= 100 { self.paintings[i].check_interval = 0; @@ -249,12 +257,21 @@ impl Game<'_> { /// PaintingRenderer: the variant on the front, the back texture behind and /// on the edges. pub(super) fn painting_geometry(&self, camera: glam::DVec3, out: &mut Vec) { - let sprites = &self.atlas.sprites; + let sprites = &self.gui.atlas.sprites; let Some(back) = sprites.get("painting/back").copied() else { return; }; for p in &self.paintings { let entity_start = out.len(); + // EntityRenderer.shouldRender: the default range and its box, + // measured from the box's centre (HangingEntity's position). + let (lo, hi) = p.bounds(); + let (lo, hi) = (glam::DVec3::from(lo), glam::DVec3::from(hi)); + let range = + super::entity_culling::default_range(hi - lo, self.settings.entity_distance); + if !self.entity_in_view(camera, (lo + hi) / 2.0, range, lo, hi) { + continue; + } let Some(v) = self.painting_variants.get(&p.variant) else { continue; }; diff --git a/crates/horizon-client/src/game/passengers.rs b/crates/horizon-client/src/game/passengers.rs index 27d8a4af6..4e6ba339d 100644 --- a/crates/horizon-client/src/game/passengers.rs +++ b/crates/horizon-client/src/game/passengers.rs @@ -186,9 +186,9 @@ impl Game<'_> { at[2].floor() as i32, ); let frequent = self.world.chunks.get(&(x >> 4, z >> 4)).is_some_and(|c| { - let b = self.res.biomes.get(c.biome(x, y, z)); + let b = self.res.biomes().get(c.biome(x, y, z)); self.res - .mob_data + .mob_data() .biome_has("minecraft:more_frequent_drowned_spawns", &b.name) }); if frequent { diff --git a/crates/horizon-client/src/game/persist.rs b/crates/horizon-client/src/game/persist.rs index 96d879f13..7a967a024 100644 --- a/crates/horizon-client/src/game/persist.rs +++ b/crates/horizon-client/src/game/persist.rs @@ -105,7 +105,9 @@ impl Game<'_> { chunk.saved = Some(Arc::new(extras)); } /// Register a newly resident chunk with the tick scheduler, with the - /// block and fluid ticks it was saved with. + /// block and fluid ticks it was saved with (`LevelChunk.registerTickContainerInLevel` + /// at FULL). They are unpacked when the chunk starts ticking + /// (`start_ticking_chunks`). pub(super) fn register_chunk_ticks(&mut self, key: (i32, i32)) { let pos = ChunkPos::new(key.0, key.1); let saved = self.world.chunks.get(&key).and_then(|c| c.saved.clone()); @@ -130,9 +132,7 @@ impl Game<'_> { .is_ok() && loaded { - let time = self.world.game_time; - self.block_ticks.blocks.unpack_chunk(pos, time); - self.block_ticks.fluids.unpack_chunk(pos, time); + self.tick_chunks.wait(key); } // The saved list is consumed: later saves take the scheduler's ticks. if loaded && let Some(c) = self.world.chunks.get_mut(&key) { @@ -176,7 +176,13 @@ impl Game<'_> { self.dropped.push(e); } } - "minecraft:experience_orb" => self.orbs.extend(entity_nbt::orbs_from_nbt(&tag)), + "minecraft:experience_orb" => { + for mut orb in entity_nbt::orbs_from_nbt(&tag) { + self.next_entity += 1; + orb.id = self.next_entity; + self.orbs.push(orb); + } + } "minecraft:arrow" => { if let Some((mut p, in_block)) = entity_nbt::arrow_from_nbt( &tag, @@ -196,6 +202,7 @@ impl Game<'_> { { let (no_gravity, drop_item) = entity_nbt::falling_block_flags(&tag); self.falling.push(FallingBlock { + id: 0, state, // DATA_START_POS is not persisted by vanilla. start_pos: horizon_core::position::BlockPos::default(), @@ -208,12 +215,29 @@ impl Game<'_> { fall_distance, damage: horizon_client::falling_block::Damage::from_nbt( &tag, - self.res.facts.get(state).name.ends_with("anvil"), + self.res.facts().get(state).name.ends_with("anvil"), ), saved: uuid.map(|u| Box::new((u, tag.clone()))), }); } } + // PrimedTnt.readAdditionalSaveData: its fuse (80 when absent) and motion. + "minecraft:tnt" => { + if let Some(position) = entity_nbt::position(&tag) { + let fuse = match tag.get("fuse") { + Some(Tag::Short(f)) => i32::from(*f), + _ => 80, + }; + self.tnts.push(super::tnt::PrimedTnt { + id: 0, + position, + previous: position, + velocity: entity_nbt::motion(&tag), + fuse, + on_ground: matches!(tag.get("OnGround"), Some(Tag::Byte(1))), + }); + } + } k if super::boats::any_boat_wood(k) .is_some_and(|(wood, chest)| self.boat_models.has(wood, chest)) => { @@ -297,7 +321,7 @@ impl Game<'_> { self.persist.unknown.entry(key).or_default().push(tag); } } - _ if self.res.mob_types.contains_key(&kind) => { + _ if self.res.mob_types().contains_key(&kind) => { self.load_mob(tag, None); } _ if !kind.is_empty() && entity_nbt::position(&tag).is_some() => { @@ -322,7 +346,7 @@ impl Game<'_> { if let Some(Tag::List(list)) = c.remove(&"Passengers".into()) { for p in list { let known = - entity_nbt::kind(&p).is_some_and(|k| self.res.mob_types.contains_key(&k)); + entity_nbt::kind(&p).is_some_and(|k| self.res.mob_types().contains_key(&k)); if known { riders.push(p); } else { @@ -334,7 +358,7 @@ impl Game<'_> { c.insert("Passengers".into(), Tag::List(unknown)); } } - let mut m = entity_nbt::mob_from_nbt(&tag, &self.res.mob_types, self.next_entity)?; + let mut m = entity_nbt::mob_from_nbt(&tag, self.res.mob_types(), self.next_entity)?; m.extra.mount = mount; m.extra.passenger = mount.is_some(); if finalize { diff --git a/crates/horizon-client/src/game/perspective.rs b/crates/horizon-client/src/game/perspective.rs index bb463547f..cdc7fa2d8 100644 --- a/crates/horizon-client/src/game/perspective.rs +++ b/crates/horizon-client/src/game/perspective.rs @@ -201,7 +201,7 @@ impl Game<'_> { ) }; let p = self.player.as_ref()?; - let sleeping = self.sleeping.map(|(head, _)| { + let sleeping = self.sleeping.map(|head| { let state = self.world.block(head[0], head[1], head[2]); let props = self .pack @@ -266,7 +266,7 @@ impl Game<'_> { binding: self.collision, subject: None, }, - facts: &self.res.facts, + facts: self.res.facts(), context, }; view.align_with_entity(&entity, partial, kind, |from, to| { @@ -296,7 +296,7 @@ impl Game<'_> { return; }; if self.player_model.is_none() { - let Some(kind) = self.res.player_type.clone() else { + let Some(kind) = self.res.player_type().clone() else { return; }; self.player_model = Some(Mob::new(0, kind, p.position, self.yaw)); @@ -305,6 +305,13 @@ impl Game<'_> { let (position, flying) = (p.position, p.flying || p.fall_flying); let (fall_flying, fly_ticks, velocity) = (p.fall_flying, p.fall_fly_ticks, p.velocity); let using = self.using.map(|(_, ticks)| ticks as i32 + 1).unwrap_or(0); + let using_off_hand = self + .using + .is_some_and(|(slot, _)| slot == super::shield::OFFHAND_SLOT); + let blocking = self + .using + .and_then(|(slot, _)| self.stack_in(slot)) + .is_some_and(|stack| self.blocks_attacks(stack).is_some()); let charged = self.inventory[self.selected] .as_ref() .is_some_and(|s| s.data().is_some_and(|d| !d.charged.is_empty())); @@ -313,13 +320,22 @@ impl Game<'_> { .map_or(25, |s| Self::charge_ticks(s).max(1)) as i32; let attack = (self.swing.old_animation, self.swing.animation); let swinging = self.swing.swinging(); + // currentSwing.hand().asArm(mainArm). + let swing_right_arm = swinging.then(|| { + (self.swing.attack_hand() == horizon_client::first_person::Hand::Main) + != self.settings.main_hand_left + }); + // LivingEntity.isVisuallySwimming and Entity.isInWater. + let visually_swimming = + p.pose == Pose::Swimming || (!p.fall_flying && p.pose == Pose::FallFlying); + let in_water = p.fluids.was_touching_water; let (yaw, pitch) = (self.yaw, self.pitch); let main = self.inventory[self.selected] .as_ref() .map(|s| s.item.clone()); let off = self.offhand.as_ref().map(|s| s.item.clone()); let riding = self.riding.is_some(); - let hurt = p.health.hurt.hurt_time; + let hurt = p.health.hurt_animation_time; let Some(m) = self.player_model.as_mut() else { return; }; @@ -375,9 +391,13 @@ impl Game<'_> { m.extra.fall_fly_ticks = fly_ticks; m.extra.main_hand_left = self.settings.main_hand_left; m.extra.using_item = using; + m.extra.using_off_hand = using_off_hand; + m.extra.blocking = blocking; m.extra.charged = charged; m.extra.ints[5] = charge_duration; m.extra.elytra.tick(fall_flying, crouching, velocity); + m.extra.swim.tick(visually_swimming, in_water); + m.extra.swing_right_arm = swing_right_arm; m.hurt_time = hurt; } /// Whether the player's own model shows in the world view. @@ -410,7 +430,7 @@ impl Game<'_> { m.position.z.floor() as i32, ); let packed = (u32::from(light >> 4) * 16) | ((u32::from(light & 15) * 16) << 8); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let lookup = |name: &str| atlas.sprites.get(name).map(|r| [r.u0, r.v0, r.u1, r.v1]); horizon_client::mobs::render::render_with( m, @@ -418,7 +438,7 @@ impl Game<'_> { camera, &lookup, packed, - &self.res.mob_data, + self.res.mob_data(), &self.player_armor_layers(), out, ); @@ -464,7 +484,7 @@ impl Game<'_> { m.position.y += 0.125 * f64::from(u8::from(m.extra.crouching)); m.previous = m.position; let camera = glam::DVec3::new(m.position.x, m.position.y, m.position.z); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let lookup = |name: &str| atlas.sprites.get(name).map(|r| [r.u0, r.v0, r.u1, r.v1]); let mut verts = Vec::new(); horizon_client::mobs::render::render_with( @@ -473,7 +493,7 @@ impl Game<'_> { camera, &lookup, 240 | 240 << 8, - &self.res.mob_data, + self.res.mob_data(), &self.player_armor_layers(), &mut verts, ); @@ -526,7 +546,7 @@ impl Game<'_> { ) { let Some(asset) = self.armor[1] .as_ref() - .and_then(|s| self.res.items.get(&s.item)) + .and_then(|s| self.res.items().get(&s.item)) .and_then(|i| i.equip_asset.as_ref()) else { return; @@ -535,9 +555,10 @@ impl Game<'_> { let Some((texture, _)) = assets.equipment.get(asset).and_then(|k| k.get("wings")) else { return; }; - let (Some(model), Some(rect)) = - (assets.player.get("elytra"), self.atlas.sprites.get(texture)) - else { + let (Some(model), Some(rect)) = ( + assets.player.get("elytra"), + self.gui.atlas.sprites.get(texture), + ) else { return; }; let base = horizon_client::mobs::render::base_transform(m, partial, camera) diff --git a/crates/horizon-client/src/game/pickups.rs b/crates/horizon-client/src/game/pickups.rs index 2729dc98f..1e19bcfc3 100644 --- a/crates/horizon-client/src/game/pickups.rs +++ b/crates/horizon-client/src/game/pickups.rs @@ -148,6 +148,7 @@ mod tests { fn orb_at(x: f64, y: f64, z: f64) -> Taken { let at = horizon_core::geometry::Vec3::new(x, y, z); Taken::Orb(Box::new(ExperienceOrb { + id: 0, position: at, previous: at, velocity: horizon_core::geometry::Vec3::new(0.0, 0.0, 0.0), diff --git a/crates/horizon-client/src/game/piglins.rs b/crates/horizon-client/src/game/piglins.rs index 3515b380b..9df5311aa 100644 --- a/crates/horizon-client/src/game/piglins.rs +++ b/crates/horizon-client/src/game/piglins.rs @@ -11,14 +11,18 @@ use horizon_core::random::RandomSource; impl Game<'_> { fn piglin_loved(&self, item: &str) -> bool { self.res - .items + .items() .item_tags .get("minecraft:piglin_loved") .is_some_and(|t| t.contains(item)) } /// `PiglinAi.isWearingSafeArmor`. pub(super) fn wearing_gold(&self) -> bool { - let tag = self.res.items.item_tags.get("minecraft:piglin_safe_armor"); + let tag = self + .res + .items() + .item_tags + .get("minecraft:piglin_safe_armor"); self.armor .iter() .flatten() @@ -119,7 +123,7 @@ impl Game<'_> { let m = &self.mobs[i]; let from = [m.position.x, m.position.y + 1.3, m.position.z]; let items = if &*item == "minecraft:gold_ingot" && !m.is_baby() { - self.res.items.loot_table( + self.res.items().loot_table( "minecraft:gameplay/piglin_bartering", false, None, @@ -191,7 +195,7 @@ impl Game<'_> { ]; !self .res - .facts + .facts() .get(self.world.block( p[0].floor() as i32, p[1].floor() as i32, @@ -203,7 +207,7 @@ impl Game<'_> { /// Whether breaking or opening this block angers piglins. pub(super) fn guarded_by_piglins(&self, name: &str) -> bool { self.res - .items + .items() .block_tags .get("minecraft:guarded_by_piglins") .is_some_and(|t| t.contains(name)) diff --git a/crates/horizon-client/src/game/potions.rs b/crates/horizon-client/src/game/potions.rs index a60926256..51cb1fc0a 100644 --- a/crates/horizon-client/src/game/potions.rs +++ b/crates/horizon-client/src/game/potions.rs @@ -169,7 +169,7 @@ impl Game<'_> { return Ok(false); }; let state = self.world.block(hit.pos[0], hit.pos[1], hit.pos[2]); - if self.res.facts.get(state).fluid != horizon_client::blocks::Fluid::Water { + if self.res.facts().get(state).fluid != horizon_client::blocks::Fluid::Water { return Ok(false); } let at = [ @@ -228,7 +228,7 @@ impl Game<'_> { } let (lasting, now) = PlayerEffects::potion_effects(stack.potion(), 1.0); for e in lasting { - self.potions.effects.add(e); + self.add_player_effect(e); } for a in now { self.apply_effect_action(a); @@ -249,7 +249,7 @@ impl Game<'_> { } true } - fn apply_effect_action(&mut self, action: EffectAction) { + pub(super) fn apply_effect_action(&mut self, action: EffectAction) { let creative = self.creative; let rules = self.world.health_rules; let Some(p) = self.player.as_mut() else { @@ -329,9 +329,24 @@ impl Game<'_> { } self.apply_effect_status(); } + /// Add an active effect to the player and update attributes / absorption. + pub(super) fn add_player_effect(&mut self, effect: EffectInstance) -> bool { + let id = effect.id; + let amp = effect.amplifier; + let changed = self.potions.effects.add(effect); + self.apply_effect_status(); + if id == "absorption" + && let Some(p) = self.player.as_mut() + { + let target = 4.0 * (amp + 1) as f32; + let current = p.health.absorption(); + let _ = p.set_health(current.max(target), true); + } + changed + } /// The effects' attribute modifiers, armor protection and status flags /// on `self.player`. - fn apply_effect_status(&mut self) { + pub(super) fn apply_effect_status(&mut self) { let modifiers = self.potions.effects.modifiers(); if modifiers != self.potions.applied && let Some(p) = self.player.as_mut() @@ -344,15 +359,21 @@ impl Game<'_> { } self.potions.applied = modifiers; } + if let Some(p) = self.player.as_mut() + && self.potions.effects.amplifier("absorption").is_none() + && p.health.absorption() > 0.0 + { + let _ = p.set_health(0.0, true); + } // EnchantmentHelper.getDamageProtection per damage type, recomputed on armor change. let armor: Vec> = self.armor.to_vec(); if self.potions.protection_for.as_ref() != Some(&armor) { let mut table = Vec::new(); - let e = &self.res.stations.enchantments; + let e = &self.res.stations().enchantments; let mut random = horizon_core::random::LegacyRandom::new(0); for (_, def) in self.motion.health.damage.entries() { let ctx = horizon_client::enchant::EffectContext { - items: Some(&self.res.items), + items: Some(self.res.items()), damage_type: Some(&def.name), this: Some("minecraft:player"), ..Default::default() @@ -408,7 +429,7 @@ impl Game<'_> { let state = self.world .block(p.x.floor() as i32, p.y.floor() as i32, p.z.floor() as i32); - if self.res.facts.get(state).full_collision + if self.res.facts().get(state).full_collision || self.mobs.iter().any(|m| { m.alive() && (m.position.x - p.x).abs() < m.kind.width / 2.0 + 0.125 @@ -467,6 +488,41 @@ impl Game<'_> { self.splash(&stack, at); } } + /// A splash potion's effects on the player in place, by their distance. + fn splash_on_player( + &mut self, + effects: &'static [(&'static str, i32, i32)], + at: Vec3, + scale: f64, + ) { + if let Some(p) = self.player.as_ref() { + let center = Vec3::new( + p.position.x, + p.position.y + f64::from(p.dimensions.height) / 2.0, + p.position.z, + ); + let d2 = + (center.x - at.x).powi(2) + (center.y - at.y).powi(2) + (center.z - at.z).powi(2); + if d2 < 16.0 && (center.y - at.y).abs() <= 2.0 + f64::from(p.dimensions.height) { + let intensity = 1.0 - d2.sqrt() / 4.0; + for (id, duration, amp) in effects { + let instant = brewing::effect(id).is_some_and(|e| e.instant); + if instant { + if let Some(a) = horizon_client::effects::instant(id, *amp, intensity) { + self.apply_effect_action(a); + } + } else { + let d = (intensity * f64::from(*duration) * scale + 0.5) as i32; + if d > 20 + && let Some(e) = brewing::effect(id) + { + self.add_player_effect(EffectInstance::new(e.id, d, *amp)); + } + } + } + } + } + } /// `ThrownSplashPotion.onHitAsPotion` (lingering clouds apply as a weaker splash). fn splash(&mut self, stack: &ItemStack, at: Vec3) { if &*stack.item == "minecraft:experience_bottle" { @@ -527,36 +583,11 @@ impl Game<'_> { let Some(def) = stack.potion().and_then(brewing::potion) else { return; }; - // The player. - if let Some(p) = self.player.as_ref() { - let center = Vec3::new( - p.position.x, - p.position.y + f64::from(p.dimensions.height) / 2.0, - p.position.z, - ); - let d2 = - (center.x - at.x).powi(2) + (center.y - at.y).powi(2) + (center.z - at.z).powi(2); - if d2 < 16.0 && (center.y - at.y).abs() <= 2.0 + f64::from(p.dimensions.height) { - let intensity = 1.0 - d2.sqrt() / 4.0; - for (id, duration, amp) in def.effects { - let instant = brewing::effect(id).is_some_and(|e| e.instant); - if instant { - if let Some(a) = horizon_client::effects::instant(id, *amp, intensity) { - self.apply_effect_action(a); - } - } else { - let d = (intensity * f64::from(*duration) * scale + 0.5) as i32; - if d > 20 - && let Some(e) = brewing::effect(id) - { - self.potions.effects.add(EffectInstance::new(e.id, d, *amp)); - } - } - } - } - } + // Every player in range (`ThrownSplashPotion.applySplash` over + // `LivingEntity`s, the players among them). + self.for_each_player(|g| g.splash_on_player(def.effects, at, scale)); // Mobs take the instant effects (undead invert healing and harming). - let undead = &self.res.stations.enchantments.entity_tags; + let undead = &self.res.stations().enchantments.entity_tags; let mut hits = Vec::new(); for (i, m) in self.mobs.iter().enumerate() { let d2 = (m.position.x - at.x).powi(2) @@ -613,7 +644,7 @@ impl Game<'_> { .map(hud_overlays::EffectIcon::from) .collect(); let draws = hud_overlays::effects(batch.width as i32, false, &icons); - hud_layout::paint(batch, &self.atlas, &draws); + hud_layout::paint(batch, &self.gui.atlas, &draws); } /// `EffectsInInventory`: panels to the right of survival/creative inventory. pub(super) fn draw_inventory_effects( @@ -629,11 +660,11 @@ impl Game<'_> { &self.potions.effects.list, self.clock.rate(), &|key| self.pack.names.get(key).map(str::to_owned), - &|text| self.atlas.font.width(text).ceil() as i32, + &|text| self.gui.atlas.font.width(text).ceil() as i32, ); if let Some(lines) = inventory_effects::paint( batch, - &self.atlas, + &self.gui.atlas, &panels, [self.cursor.0 as i32, self.cursor.1 as i32], ) { diff --git a/crates/horizon-client/src/game/powder_snow.rs b/crates/horizon-client/src/game/powder_snow.rs index 7d0a7770d..6a904706f 100644 --- a/crates/horizon-client/src/game/powder_snow.rs +++ b/crates/horizon-client/src/game/powder_snow.rs @@ -68,7 +68,7 @@ impl Game<'_> { pub(super) fn is_powder_snow(&self, pos: [i32; 3]) -> bool { &*self .res - .facts + .facts() .get(self.world.block(pos[0], pos[1], pos[2])) .name == "minecraft:powder_snow" diff --git a/crates/horizon-client/src/game/raids.rs b/crates/horizon-client/src/game/raids.rs index 63f45c1c5..8c31ca25b 100644 --- a/crates/horizon-client/src/game/raids.rs +++ b/crates/horizon-client/src/game/raids.rs @@ -39,6 +39,17 @@ pub(super) struct Raiding { homes: HashMap>)>, } +impl Raiding { + /// Exchange the level-held parts (`ServerLevel.getRaids`) with another + /// level's (game/levels.rs). + pub(super) fn swap_level(&mut self, other: &mut Self) { + std::mem::swap(&mut self.raids, &mut other.raids); + std::mem::swap(&mut self.loaded, &mut other.loaded); + std::mem::swap(&mut self.bar_hurts, &mut other.bar_hurts); + std::mem::swap(&mut self.homes, &mut other.homes); + } +} + /// `SectionPos` of a block position. pub(super) fn section(p: [i32; 3]) -> (i32, i32, i32) { (p[0] >> 4, p[1] >> 4, p[2] >> 4) @@ -89,7 +100,7 @@ impl RaidLevel for Lvl<'_, '_> { let w = g.mob_world_base(&[]); spawn_rules::is_spawn_position_ok(Species::Ravager, &w, x, y, z) || w.block_name(x, y - 1, z) == "minecraft:snow" - && g.res.facts.is_air(g.world.block(x, y, z)) + && g.res.facts().is_air(g.world.block(x, y, z)) } fn player(&self) -> Option { let p = self.game.player.as_ref()?; @@ -210,7 +221,7 @@ impl Game<'_> { let top = if self.dim == Dim::Nether { 127 } else { 319 }; (horizon_client::chunk::MIN_Y..=top) .rev() - .find(|&y| !self.res.facts.is_air(self.world.block(x, y, z))) + .find(|&y| !self.res.facts().is_air(self.world.block(x, y, z))) .map_or(horizon_client::chunk::MIN_Y, |y| y + 1) } /// Every claimed village point of interest (job sites, beds, bells). @@ -345,7 +356,7 @@ impl Game<'_> { .get(&(at[0] >> 4, at[2] >> 4)) .map(|c| { self.res - .biomes + .biomes() .get(c.biome(at[0], at[1], at[2])) .name .clone() @@ -613,17 +624,17 @@ impl Game<'_> { raider::state_mut(m).ticks_outside_raid = ticks; } } - Action::Victory { heroes, amplifier } => { - let me = self.level.player_uuid; - if me.is_some_and(|u| heroes.contains(&u)) && !self.spectator() { + // Raid.tick on a victory: each hero who is still alive. + Action::Victory { heroes, amplifier } => self.for_each_player(|g| { + if heroes.contains(&g.acting_uuid()) && !g.spectator() { let mut effect = EffectInstance::new("hero_of_the_village", raid::HERO_DURATION, amplifier); effect.visible = false; - self.potions.effects.add(effect); - self.stats.custom("raid_win", 1); - self.trigger_player("minecraft:hero_of_the_village"); + g.potions.effects.add(effect); + g.stats.custom("raid_win", 1); + g.trigger_player("minecraft:hero_of_the_village"); } - } + }), } } /// `Raid.playSound`: the horn, 13 blocks away from the player toward the @@ -705,7 +716,7 @@ impl Game<'_> { leader: bool, reason: raider::Reason, ) -> Option { - let t = self.res.mob_types.get(kind).cloned()?; + let t = self.res.mob_types().get(kind).cloned()?; self.next_entity += 1; let mut mob = Mob::new(self.next_entity, t, Vec3::new(at[0], at[1], at[2]), 0.0); if leader { @@ -971,11 +982,11 @@ impl Game<'_> { for z in c[2] - 64..=c[2] + 64 { for x in c[0] - 64..=c[0] + 64 { let state = self.world.block(x, y, z); - if self.res.facts.is_air(state) { + if self.res.facts().is_air(state) { continue; } let bed = *known.entry(state.0).or_insert_with(|| { - self.res.facts.get(state).name.ends_with("_bed") + self.res.facts().get(state).name.ends_with("_bed") && self.pack.assets.registry.state(state).is_some_and(|s| { s.properties.get("part").map(String::as_str) == Some("head") }) @@ -1110,7 +1121,7 @@ impl Game<'_> { pub(super) fn throw_gift(&mut self, table: &str, from: [f64; 3], to: [f64; 3]) { let drops = self .res - .items + .items() .loot_table(table, false, None, &mut self.world.random); let d = Vec3::new(to[0] - from[0], to[1] - from[1], to[2] - from[2]).normalize(); for stack in drops { @@ -1152,7 +1163,7 @@ impl Game<'_> { .get(&(at[0] >> 4, at[2] >> 4)) .map(|c| { game.res - .biomes + .biomes() .get(c.biome(at[0], at[1], at[2])) .name .clone() diff --git a/crates/horizon-client/src/game/recipe_book.rs b/crates/horizon-client/src/game/recipe_book.rs index 67fc46b68..843c01476 100644 --- a/crates/horizon-client/src/game/recipe_book.rs +++ b/crates/horizon-client/src/game/recipe_book.rs @@ -110,7 +110,7 @@ fn sprite(name: &str) -> String { impl Game<'_> { /// The book the open screen has, if any. fn active_book(&self) -> Option { - if self.screen != Screen::Inventory || self.remote.is_some() || self.station.is_some() { + if self.screen != Screen::Inventory || self.station.is_some() { return None; } if self.mount_active() { @@ -205,7 +205,7 @@ impl Game<'_> { return; } self.ensure_book(); - let index = &self.res.stations.recipe_index; + let index = &self.res.stations().recipe_index; let book = self.recipe_ui.book.as_ref(); // SmithingTrimDemoSlotDisplay draws from a random of its own. let mut random = super::dispenser::entity_random(); @@ -224,8 +224,8 @@ impl Game<'_> { }) .or_else(|| index.result(id).map(|r| ItemStack::new(r, 1))) .or_else(|| { - self.res.stations.recipes.trim_demo( - &self.res.stations.enchantments, + self.res.stations().recipes.trim_demo( + &self.res.stations().enchantments, id, &mut |n| { horizon_core::random::RandomSource::next_int_bound( @@ -262,6 +262,33 @@ impl Game<'_> { /// The items the book counts (`RecipeBookComponent.updateStackedContents`): /// the inventory and the menu's crafting slots, as the menu engine counts them. fn book_available(&mut self, _book: BookType) -> Stacked { + if self.remote.is_some() { + // A server's menu: the slots it last sent, counted the same way. + use horizon_client::menu::{Env, Kind, Menu}; + let table = self.craft_size == 3; + let grid: Vec<_> = (0..self.craft_size * self.craft_size) + .map(|k| self.slot_mut(100 + k).clone()) + .collect(); + let mut menu = Menu::new(if table { + Kind::Crafting + } else { + Kind::Inventory + }); + let env = Env { + items: self.res.items(), + recipes: self.res.recipes(), + brewing: &self.res.stations().brewing, + banners: &self.res.stations().banners, + trade: None, + }; + for i in 0..36 { + menu.set_player_item(i, self.inventory[i].clone()); + } + for (k, stack) in grid.into_iter().enumerate() { + menu.set_slot(&env, 1 + k, stack); + } + return menu.stacked_contents(&env); + } let mut out = Stacked::default(); let stock = &mut out; self.menu_session(|menu, env, _| { @@ -274,8 +301,12 @@ impl Game<'_> { /// replacing batch (vanilla's server iterates an identity hash set; this /// one goes in id order), then each unlock and removal. fn sync_client_book(&mut self) { + // On a server the book is the server's packets' (remote_recipe_book_*). + if self.remote.is_some() { + return; + } let known = &self.level.recipe_book.recipes; - let index = &self.res.stations.recipe_index; + let index = &self.res.stations().recipe_index; let ui = &mut self.recipe_ui; match ui.client_known.as_ref() { Some(previous) if previous == known => return, @@ -305,8 +336,8 @@ impl Game<'_> { } fn ensure_book(&mut self) { if self.recipe_ui.book.is_none() { - let mut book = Book::build(&self.res.recipes, &self.res.items); - book.set_display_ids(&self.res.stations.recipe_index); + let mut book = Book::build(self.res.recipes(), self.res.items()); + book.set_display_ids(&self.res.stations().recipe_index); self.recipe_ui.book = Some(book); } if self.recipe_ui.search.is_none() { @@ -466,7 +497,7 @@ impl Game<'_> { && !book.collection_matches(&c.entries, &query, &|stack| { horizon_client::item_tooltip::lines( stack, - self.res.items.get(&stack.item), + self.res.items().get(&stack.item), &|k| self.pack.names.get(k).map(str::to_owned), ) }) @@ -629,7 +660,7 @@ impl Game<'_> { search.focused = true; search.reset_blink(); if !magnifier { - let idx = search.index_at(&self.atlas, cursor.0); + let idx = search.index_at(&self.gui.atlas, cursor.0); search.set_cursor(idx, false); } return true; @@ -660,6 +691,10 @@ impl Game<'_> { } false } + /// How many displays the client's book holds. + pub(super) fn recipe_book_known(&self) -> usize { + self.recipe_ui.client.displays().len() + } /// The recipe book's buttons a controller can focus: the book button, /// recipes, page arrows, the craftable filter and the tabs (GUI pixels; /// the search box registers itself as a field). @@ -800,6 +835,18 @@ impl Game<'_> { else { return; }; + if self.remote.is_some() { + // The server places it and answers with the slots, or a ghost. + let container = self.remote_menu_id(); + self.send(horizon_protocol::packets::play::Serverbound::PlaceRecipe( + horizon_protocol::packets::recipes::PlaceRecipe { + container, + recipe: entry.display_id, + use_max, + }, + )); + return; + } let creative = self.creative; let mut post = None; let answer = &mut post; @@ -828,6 +875,80 @@ impl Game<'_> { } } + // ---- a server's recipe book ---- + + /// ClientboundRecipeBookAddPacket (`ClientRecipeBook.add`): the + /// displays join the book, highlighted when new, and the recipes whose + /// entries ask for it show the toast. + pub(super) fn remote_recipe_book_add( + &mut self, + p: horizon_protocol::packets::recipes::RecipeBookAdd, + ) { + let ui = &mut self.recipe_ui; + if p.replace { + ui.client.clear(); + } + let mut toast = Vec::new(); + for e in &p.entries { + ui.client.add(e.id); + if e.flags & 2 != 0 { + ui.client.highlight.insert(e.id); + } + if e.flags & 1 != 0 + && let Some(id) = self.res.stations().recipe_index.recipe_of_display(e.id) + && !toast.iter().any(|t| t == id) + { + toast.push(id.to_owned()); + } + } + // A local world opened later syncs its book from scratch. + ui.client_known = None; + ui.client_version += 1; + self.recipe_toast(&toast); + } + /// ClientboundRecipeBookRemovePacket. + pub(super) fn remote_recipe_book_remove( + &mut self, + p: horizon_protocol::packets::recipes::RecipeBookRemove, + ) { + for id in p.ids { + self.recipe_ui.client.remove(id); + } + self.recipe_ui.client_version += 1; + } + /// ClientboundRecipeBookSettingsPacket: each book's open and filter flags. + pub(super) fn remote_recipe_book_settings( + &mut self, + p: horizon_protocol::packets::recipes::RecipeBookSettings, + ) { + for (i, (open, filtering)) in p.books.into_iter().enumerate() { + self.level.recipe_book.open[i] = open; + self.level.recipe_book.filtering[i] = filtering; + } + } + /// ClientboundPlaceGhostRecipePacket: the recipe last placed, faded into + /// the open menu's grid. + pub(super) fn remote_ghost_recipe( + &mut self, + p: horizon_protocol::packets::recipes::PlaceGhostRecipe, + ) { + let open = self.remote_menu_id(); + let Some(book_type) = self.active_book() else { + return; + }; + if p.container != open { + return; + } + let entry = self.recipe_ui.last_placed.as_ref().and_then(|id| { + let book = self.recipe_ui.book.as_ref()?; + book.entry(id).cloned() + }); + if let Some(entry) = entry { + self.recipe_ui.ghost.clear(); + self.fill_ghost(book_type, &entry); + } + } + // ---- ghost recipe ---- /// `fillGhostRecipe`: the recipe's ingredients faded into the slots. @@ -838,7 +959,7 @@ impl Game<'_> { return; }; let ghost = book - .ghost(entry, book_type, grid, fuel_empty, &self.res.items) + .ghost(entry, book_type, grid, fuel_empty, self.res.items()) .into_iter() .map(|g| Ghost { slot: match (g.target, book_type) { @@ -871,7 +992,7 @@ impl Game<'_> { let (bx, by) = self.book_button_pos(book_type, ox, batch); let hover = inside(cursor, bx, by, 20.0, 18.0); batch.sprite( - &self.atlas, + &self.gui.atlas, &sprite(if hover { "button_highlighted" } else { @@ -919,7 +1040,7 @@ impl Game<'_> { fn draw_book_panel(&mut self, batch: &mut HudBatch, book_type: BookType) { let (xo, yo) = self.book_origin(batch); let cursor = self.cursor; - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let search = match self.recipe_ui.search.as_mut() { Some(edit) => { // The box's place, for clicks and typing. @@ -1042,7 +1163,7 @@ impl Game<'_> { let mut tooltip: Option> = None; for d in calls { let start = batch.vertices.len(); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; match &d.call { Call::Blit { x, y, w, h, u, v, .. diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index f066c9755..c10c3f439 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -7,22 +7,29 @@ //! client predicts its own movement; the server stays authoritative for //! everything else. use super::{Dim, Game, Mining, Screen}; +use horizon_client::first_person::Hand; use horizon_client::{ Result, - chunk::{BIOME_STRATEGY, BLOCK_STRATEGY, Chunk, Light, MIN_Y, SECTIONS, Section}, + chunk::{BIOME_STRATEGY, Chunk, Light, MIN_Y, SECTIONS, Section, block_strategy}, containers::{BlockEntity, Furnace, FurnaceKind}, - entities::{Experience, ItemEntity}, + entities::{Experience, ExperienceOrb, ItemEntity}, hud::{HudBatch, rgba}, items::ItemStack, - mobs::{Mob, MobType}, + menu::Input, + mobs::{Mob, MobType, Who}, motion::{MotionWorld, PlayerMotion}, net::{Event, Session}, save::SaveDir, sprint::Keys, streaming::{ChunkGenerator, Proto, Streaming}, + tab_list::SPECTATOR, world::World, }; -use horizon_core::{geometry::Vec3, paletted::PalettedContainer, random::RandomSource}; +use horizon_core::{ + geometry::Vec3, + paletted::{PalettedContainer, Strategy}, + random::RandomSource, +}; use horizon_protocol::{ entity_data::{ITEM_ENTITY_ITEM, Value}, item::{Hashed, Stack}, @@ -34,13 +41,18 @@ use horizon_protocol::{ text::{Language, format_java, plain}, }; use horizon_world::{nbt::Tag, registry::BlockStateId}; -use std::{ - collections::HashMap, - sync::Arc, - time::{Duration, Instant}, -}; +use std::{collections::HashMap, sync::Arc, time::Instant}; use winit::{event::MouseButton, keyboard::KeyCode}; +/// A text component the server sent, for the HUD. +fn remote_text(tag: &Tag) -> horizon_client::text::Text { + use horizon_client::scoreboard::{comp_text, tag_to_json}; + comp_text(&tag_to_json(tag)) +} + +/// A section's sky and block light layers from the wire. +type LightLayers = (Option>, Option>); + /// Streaming needs a generator; a server world never generates. struct NoTerrain; impl ChunkGenerator for NoTerrain { @@ -61,8 +73,11 @@ struct Tracked { } /// Smooth an entity toward a new position over three ticks (InterpolationHandler). #[derive(Clone, Copy)] +/// `LinearInterpolationHandler`'s target: a position and rotation (yRot, +/// xRot) reached in `steps` ticks. struct Lerp { target: [f64; 3], + rotation: Option<(f32, f32)>, steps: u32, } @@ -71,7 +86,7 @@ pub(super) struct Remote { pub brand: String, builtin: Arc, lang: Arc, - registries: Arc, + pub(super) registries: Arc, joined: bool, status: String, entity_id: i32, @@ -80,15 +95,13 @@ pub(super) struct Remote { biome_bits: u8, view_distance: i32, sequence: i32, - last_pos: [f64; 3], - last_rot: (f32, f32), - last_on_ground: bool, - last_collision: bool, - reminder: u32, + position_sender: horizon_client::player_sync::PositionSender, last_input: u8, was_sprinting: bool, carried: Option, loaded_sent: bool, + /// The view distance last sent in ClientInformation. + sent_view: i8, /// BlockStatePredictionHandler: blocks changed ahead of the server (a predicted break), /// with the sequence of the last action that changed them and the state the server /// last said they have. Until the server acknowledges that sequence its block updates @@ -102,13 +115,17 @@ pub(super) struct Remote { use_target: Option<[i32; 3]>, entities: HashMap, lerps: HashMap, + /// `LivingEntity.lerpHeadTo`: the head yaw and the ticks left to reach it. + head_lerps: HashMap, /// Item entities waiting for their item data. pending_items: HashMap, health: (f32, i32, f32), game_mode: i32, pub chat: Vec<(String, Instant)>, - pub chat_input: Option, - action_bar: Option<(String, Instant)>, + /// Lines received since joining, for the control socket's events. + pub chat_received: u64, + /// The server's command tree (ClientboundCommandsPacket). + pub commands: Option, players: HashMap, /// `ClientPacketListener.playerInfoMap` as player-list entries, and /// which of them are listed. @@ -124,6 +141,8 @@ pub(super) struct Remote { profile: u128, destroying: Option<([i32; 3], u8, f32)>, destroy_delay: u32, + input: horizon_client::interaction_input::InteractionInput, + stop_mining: bool, placing: Option<([i32; 3], u64)>, death_message: Option, player_type: Option>, @@ -131,7 +150,14 @@ pub(super) struct Remote { synced: Vec>, decode_errors: u32, clock: Option, + /// `ClientClockManager.ClientClockInstance`'s rate and partial tick for + /// the level's clock (its total is `world.day_time`), and + /// `lastTickGameTime`. clock_rate: f32, + clock_partial: f32, + clock_game_time: i64, + /// `ClientChunkCache.Storage`'s view centre (ClientboundSetChunkCacheCenterPacket). + chunk_center: (i32, i32), } fn face_direction(face: [i32; 3]) -> u8 { @@ -159,6 +185,13 @@ fn number_format(f: &play::NumberFormat) -> horizon_client::scoreboard::NumberFo impl Game<'_> { /// Leave the title for a server (ConnectScreen): an empty world fed by the connection. pub(super) fn connect_server(&mut self, address: &str, name: &str) -> Result<()> { + if self.defer_world_entry(super::PendingWorldEntry::Server { + address: address.to_owned(), + name: name.to_owned(), + })? { + return Ok(()); + } + self.join_world_rest()?; if let Err(error) = self.join_server(address, name, None) { log::warn!("Horizon: could not join server: {error}"); if self.title.is_none() { @@ -194,7 +227,7 @@ impl Game<'_> { .and_then(|p| Builtin::load(&p).ok()) .ok_or("Resources are incomplete: registries.json is missing or invalid. Click \"Prepare again\" in the launcher (or run horizon-prepare), then try again.")?; let lang = Arc::new(horizon_client::net::load_language(&root)); - let view = self.settings.render_distance.clamp(2, 32) as i8; + let view = client_view_distance(self.settings.render_distance); if local.is_none() { horizon_client::crash::set_detail( "Server", @@ -215,12 +248,12 @@ impl Game<'_> { let mut streaming = Streaming::new( Arc::new(NoTerrain), self.pack.context.clone(), - self.res.threads, + self.res.threads(), ); streaming.remote = true; streaming.radius = self.settings.render_distance; self.streaming = streaming; - self.world = World::new(self.res.facts.air, 0); + self.world = World::new(self.res.facts().air, 0); self.save = Arc::new(SaveDir::new( std::env::temp_dir().join("horizon-remote-unused"), )); @@ -231,6 +264,8 @@ impl Game<'_> { self.player = None; self.spawn = None; self.mobs.clear(); + self.clear_remote_objects(); + self.clear_remote_projectiles(); self.creatures.projectiles.clear(); self.dropped.clear(); self.particles.clear(); @@ -242,6 +277,7 @@ impl Game<'_> { self.container = None; self.carried = None; self.using = None; + self.clicks.miss_time = 0; self.set_mode_id(0); self.experience = Experience { level: 0, @@ -249,9 +285,12 @@ impl Game<'_> { total: 0, }; self.ui.registries.permission = 0; + self.ui.chat.clear(); 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(); } @@ -270,15 +309,12 @@ impl Game<'_> { biome_bits: 7, view_distance: self.settings.render_distance, sequence: 0, - last_pos: [0.0; 3], - last_rot: (0.0, 0.0), - last_on_ground: false, - last_collision: false, - reminder: 0, + position_sender: Default::default(), last_input: 0, was_sprinting: false, carried: None, loaded_sent: false, + sent_view: view, predictions: HashMap::new(), menu: None, menu_title: None, @@ -286,12 +322,13 @@ impl Game<'_> { use_target: None, entities: HashMap::new(), lerps: HashMap::new(), + head_lerps: HashMap::new(), pending_items: HashMap::new(), health: (20.0, 20, 5.0), game_mode: 0, chat: Vec::new(), - chat_input: None, - action_bar: None, + chat_received: 0, + commands: None, players: HashMap::new(), tab_players: HashMap::new(), skin_textures: HashMap::new(), @@ -301,6 +338,8 @@ impl Game<'_> { profile: 0, destroying: None, destroy_delay: 0, + input: Default::default(), + stop_mining: false, placing: None, death_message: None, player_type, @@ -308,7 +347,15 @@ impl Game<'_> { decode_errors: 0, clock: None, clock_rate: 1.0, + clock_partial: 0.0, + clock_game_time: 0, + chunk_center: (0, 0), }); + // The level's TickRateManager follows the server's ticking packets. + self.clock = horizon_client::clock::FixedStep::client(); + // The player's advancements and statistics are the server's. + self.adv = Default::default(); + self.stats = Default::default(); Ok(()) } @@ -331,14 +378,19 @@ impl Game<'_> { None => false, }; horizon_client::crash::clear_detail("Server"); + self.clock = Default::default(); + self.adv = Default::default(); + self.stats = Default::default(); self.debug_charts.reset(); self.tab_list.reset(); self.score.board = Default::default(); self.streaming = Streaming::new(Arc::new(NoTerrain), self.pack.context.clone(), 1); self.streaming.paused = true; - self.world = World::new(self.res.facts.air, 0); + self.world = World::new(self.res.facts().air, 0); self.player = None; self.mobs.clear(); + self.clear_remote_objects(); + self.clear_remote_projectiles(); self.dropped.clear(); self.particles.clear(); self.pickups.clear(); @@ -347,6 +399,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(); } @@ -421,7 +475,7 @@ impl Game<'_> { .and_then(|i| i.id(name)) .unwrap_or(0) } - fn to_stack(&self, s: &Option) -> Option { + pub(super) fn to_stack(&self, s: &Option) -> Option { let s = s.as_ref()?; let mut out = ItemStack { item: self.item_name(s.item), @@ -451,14 +505,36 @@ impl Game<'_> { } pub(super) fn chat_line(&mut self, text: String) { log::info!("[chat] {text}"); + self.ui.chat.add( + &self.gui.atlas, + horizon_client::text::Text::literal(text.clone()), + None, + self.ticks, + self.screen == Screen::Chat, + ); if let Some(r) = self.remote.as_mut() { r.chat.push((text, Instant::now())); + r.chat_received += 1; if r.chat.len() > 100 { r.chat.remove(0); } } } + /// The names in the server's player list, sorted. + pub(super) fn remote_online_names(&self) -> Option> { + let r = self.remote.as_ref()?; + let mut names: Vec = r.players.values().cloned().collect(); + names.sort(); + Some(names) + } + /// The open menu's window id (0: the inventory). + pub(super) fn remote_menu_id(&self) -> i32 { + self.remote + .as_ref() + .and_then(|r| r.menu.as_ref()) + .map_or(0, |m| m.0) + } /// The server's last `n` chat lines, newest first. pub(super) fn remote_chat(&self, n: usize) -> Option> { let r = self.remote.as_ref()?; @@ -520,15 +596,65 @@ impl Game<'_> { break; } } + self.send_suggestion_request(); Ok(()) } + /// ClientSuggestionProvider.customSuggestion: the chat's completion + /// request for an `ask_server` node goes to the server. + fn send_suggestion_request(&mut self) { + if self.remote.is_none() { + return; + } + let Some((id, command)) = self.ui.chat_screen.as_mut().and_then(|s| s.take_request()) + else { + return; + }; + let mut w = horizon_protocol::buf::Writer::new(); + w.varint(id); + w.string(&command); + self.send(S::Unknown( + horizon_protocol::ids::play::serverbound::COMMAND_SUGGESTION, + w.data, + )); + } + /// ClientboundCommandSuggestionsPacket: id, range start and length, then + /// each suggestion's text and optional tooltip. + fn handle_command_suggestions(&mut self, data: &[u8]) { + let lang = self + .remote + .as_ref() + .map(|r| r.lang.clone()) + .unwrap_or_default(); + let mut r = horizon_protocol::buf::Reader::new(data); + let decoded = (|| { + let id = r.varint()?; + let start = r.varint()?; + let length = r.varint()?; + let n = r.count(usize::MAX)?; + let mut list = Vec::with_capacity(n.min(1000)); + for _ in 0..n { + let text = r.string(32767)?; + let tooltip = if r.bool()? { + Some(plain(&r.nbt()?, &lang)) + } else { + None + }; + list.push((text, tooltip)); + } + horizon_protocol::Result::Ok((id, start, length, list)) + })(); + if let Ok((id, start, length, list)) = decoded { + let range = (start.max(0) as usize, length.max(0) as usize); + self.chat_server_suggestions(id, range, list); + } + } fn set_registries(&mut self, registries: Arc) { let biomes: Vec = registries .registries .get("minecraft:worldgen/biome") .map(|list| { list.iter() - .map(|(name, _)| self.res.biomes.id(name)) + .map(|(name, _)| self.res.biomes().id(name)) .collect() }) .unwrap_or_default(); @@ -548,18 +674,16 @@ impl Game<'_> { r.dimension = dimension; r.game_mode = spawn.game_type; r.loaded_sent = false; + // A replacement LocalPlayer has a fresh sendPosition baseline. + r.position_sender = Default::default(); r.predictions.clear(); self.set_mode_id(spawn.game_type); - self.dim = if spawn.dimension == "minecraft:the_nether" { - Dim::Nether - } else { - Dim::Overworld - }; + self.dim = Dim::from_id(&spawn.dimension); if !same { let mut streaming = Streaming::new( Arc::new(NoTerrain), self.pack.context.clone(), - self.res.threads, + self.res.threads(), ); streaming.remote = true; streaming.radius = self.streaming.radius; @@ -569,19 +693,26 @@ impl Game<'_> { self.world.day_time, self.world.weather, ); - self.world = World::new(self.res.facts.air, spawn.seed); + self.world = World::new(self.res.facts().air, spawn.seed); self.world.game_time = time; self.world.day_time = day; self.world.weather = weather; self.mobs.clear(); + self.clear_remote_objects(); + self.clear_remote_projectiles(); self.dropped.clear(); self.particles.clear(); self.pickups.clear(); if let Some(r) = self.remote.as_mut() { r.entities.clear(); r.lerps.clear(); + r.head_lerps.clear(); r.pending_items.clear(); + // 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(); } @@ -624,11 +755,31 @@ impl Game<'_> { if !(0..SECTIONS as i32).contains(&target) { continue; } - let strategy = BLOCK_STRATEGY; + // A global section is as wide as the server's registry: 16 bits + // from a vanilla server, wider from a Horizon host with the same + // content mods. Read it at that width, then widen it to ours. + let ours = block_strategy(); + let strategy = if ours.wire_has_palette(s.states.bits) { + ours + } else { + Strategy::blocks(s.states.bits) + }; let palette = s.states.palette.iter().map(|&p| p as u32).collect(); let words = s.states.data.iter().map(|&w| w as u64).collect(); - let blocks = PalettedContainer::from_wire(strategy, s.states.bits, palette, words) + let mut blocks = PalettedContainer::from_wire(strategy, s.states.bits, palette, words) .map_err(|e| e.to_string())?; + if strategy.global_bits != ours.global_bits { + let mut values = [0u32; 4096]; + blocks.unpack(&mut values); + // Ids past our registry (a host with mods we lack) read as air. + let limit = 1u64 << ours.global_bits; + for v in &mut values { + if u64::from(*v) >= limit { + *v = 0; + } + } + blocks = PalettedContainer::from_values(ours, &values); + } let mut section = Section { blocks, biomes: PalettedContainer::from_values(BIOME_STRATEGY, &cells), @@ -655,11 +806,190 @@ impl Game<'_> { PalettedContainer::from_values(BIOME_STRATEGY, &cells); } } + // ClientPacketListener.applyLightData: the column keeps the light + // the server sent; nothing is relit. + let (layers, above) = self.wire_light(&p.light); + let sky = r.dimension.has_skylight; + horizon_client::light::light_from_layers(&mut chunk, &layers, above, sky); Ok(chunk) } + /// `ClientPacketListener.readSectionList`: the sky and block layers of a + /// light packet, by section of the client's column, and the sky light + /// above the column's top section (from the light section just above + /// it, else open sky). + fn wire_light( + &self, + light: &horizon_protocol::chunk::LightData, + ) -> (Vec, [u8; 256]) { + let mut layers: Vec = vec![(None, None); SECTIONS]; + let Some(r) = self.remote.as_ref() else { + return (layers, [0; 256]); + }; + let sky = r.dimension.has_skylight; + let mut above = [if sky { 15 } else { 0 }; 256]; + // Light section b is the dimension's section b - 1. + let first = (r.dimension.min_y - MIN_Y).div_euclid(16) - 1; + for (b, layer) in light + .layers(r.dimension.sections() + 2) + .into_iter() + .enumerate() + { + let si = first + b as i32; + if (0..SECTIONS as i32).contains(&si) { + layers[si as usize] = layer; + } else if si == SECTIONS as i32 + && sky + && let Some(l) = &layer.0 + { + for (i, a) in above.iter_mut().enumerate() { + *a = (l[i >> 1] >> ((i & 1) * 4)) & 15; + } + } + } + (layers, above) + } + /// `handleLightUpdatePacket`: the sections it names take its light. + fn remote_light_update(&mut self, p: &play::LightUpdate) { + let Some(sky) = self.remote.as_ref().map(|r| r.dimension.has_skylight) else { + return; + }; + if !self.world.chunks.contains_key(&(p.x, p.z)) { + return; + } + let (layers, _) = self.wire_light(&p.light); + let Some(chunk) = self.world.chunks.get_mut(&(p.x, p.z)) else { + return; + }; + let changed = + horizon_client::light::update_light_layers(Arc::make_mut(chunk), &layers, sky); + Streaming::light_updated(&mut self.world, p.x, p.z, &changed); + } + /// `ClientChunkCache.Storage.inRange`: within the storage around the + /// view centre, `calculateStorageRange` (the server's radius, at least + /// two, plus three) chunks each way. + fn remote_chunk_in_range(&self, x: i32, z: i32) -> bool { + self.remote.as_ref().is_none_or(|r| { + let range = r.view_distance.max(2) + 3; + (x - r.chunk_center.0).abs() <= range && (z - r.chunk_center.1).abs() <= range + }) + } + /// The columns a moved or resized storage no longer holds. + fn drop_remote_chunks_out_of_range(&mut self) { + let gone: Vec<(i32, i32)> = self + .world + .chunks + .keys() + .copied() + .filter(|&(x, z)| !self.remote_chunk_in_range(x, z)) + .collect(); + for (x, z) in gone { + self.streaming.forget(&mut self.world, x, z); + self.world + .block_entities + .retain(|k, _| (k.0 >> 4, k.2 >> 4) != (x, z)); + if let Some(renderer) = self.renderer.as_mut() { + for sy in 0..SECTIONS as i32 { + renderer.remove((x, sy, z)); + } + } + } + } + /// `handleBlockEntityData`: the block entity at `pos`, when it is of the + /// packet's type, loads the tag (`loadWithComponents`); an empty tag + /// changes nothing. + fn remote_block_entity_data(&mut self, pos: [i32; 3], kind: i32, tag: &Tag) { + let key = (pos[0], pos[1], pos[2]); + let Some(name) = self + .remote + .as_ref() + .and_then(|r| r.builtin.get("minecraft:block_entity_type")?.name(kind)) + else { + return; + }; + let Some(entity) = self.world.block_entities.get(&key) else { + return; + }; + let Tag::Compound(fields) = tag else { + return; + }; + if entity.id() != name || fields.is_empty() { + return; + } + let mut full = fields.clone(); + full.insert("id".into(), Tag::String(name.into())); + for (k, v) in ["x", "y", "z"].into_iter().zip(pos) { + full.insert(k.into(), Tag::Int(v)); + } + if let Some((_, loaded)) = horizon_client::save::decode_block_entity(&Tag::Compound(full)) { + self.world.block_entities.insert(key, loaded); + // The section is drawn again with what the block entity shows. + let state = self.world.block(pos[0], pos[1], pos[2]); + let _ = self.world.set(pos[0], pos[1], pos[2], state); + } + } + /// `handleAnimate`: a player wakes up, or an entity gets the critical + /// hit (`CRIT`) or magic critical hit (`ENCHANTED_HIT`) emitter. + fn remote_animate(&mut self, id: i32, action: u8) { + use horizon_client::particles::{ParticleOptions, ParticleType}; + let me = self.remote.as_ref().is_some_and(|r| r.entity_id == id); + let body = if me { + self.player.as_ref().map(|p| { + let d = p.dimensions; + ( + [p.position.x, p.position.y, p.position.z], + f64::from(d.width), + f64::from(d.height), + ) + }) + } else { + self.mobs.iter().find(|m| m.id == id as u32).map(|m| { + ( + [m.position.x, m.position.y, m.position.z], + m.kind.width, + m.kind.height, + ) + }) + }; + let Some((feet, width, height)) = body else { + return; + }; + let particle = match action { + // Player.stopSleepInBed. + 0 => { + if me { + self.sleeping = None; + self.sleep_timer = 100; + } + return; + } + 1 => ParticleType::Crit, + 2 => ParticleType::EnchantedHit, + _ => return, + }; + self.particles + .tracking_emitter(ParticleOptions::simple(particle), feet, width, height); + } + /// `handleServerData`: the server list entry of the server joined keeps + /// the icon the server sends (`ServerList.saveSingleServer`). Single + /// player has no entry. + fn remote_server_data(&mut self, icon: Option<&[u8]>) { + let (Some(r), Some(png)) = (self.remote.as_ref(), icon) else { + return; + }; + if self.integrated.is_some() { + return; + } + if let Ok(dir) = std::env::current_dir() { + horizon_client::multiplayer::save_server_icon( + &dir.join("saves"), + &r.session.address, + png, + ); + } + } /// Keep the renderable block entities (chests, furnaces) in step with blocks. fn sync_block_entity(&mut self, pos: (i32, i32, i32), state: BlockStateId) { - let name = self.res.facts.get(state).name.clone(); + let name = self.res.facts().get(state).name.clone(); match BlockEntity::for_block(&name) { Some(entity) => { let same = self @@ -722,9 +1052,9 @@ impl Game<'_> { // A block we just placed: play its place sound (the server skips the placer). if let Some((target, _)) = self.remote.as_ref().and_then(|r| r.placing) && target == pos - && !self.res.facts.is_air(state) + && !self.res.facts().is_air(state) { - let s = self.res.facts.get(state).sounds.clone(); + let s = self.res.facts().get(state).sounds.clone(); let at = [ f64::from(pos[0]) + 0.5, f64::from(pos[1]) + 0.5, @@ -737,17 +1067,33 @@ impl Game<'_> { } } } - fn spawn_entity(&mut self, id: i32, kind: &str, position: [f64; 3], yaw: f32, head: f32) { + /// `recreateFromPacket`: yRot and xRot from the packet, the body and head + /// both at its head yaw (LivingEntity). + fn spawn_entity(&mut self, id: i32, kind: &str, position: [f64; 3], rotation: [f32; 3]) { + let [yaw, pitch, head] = rotation; let at = Vec3::new(position[0], position[1], position[2]); let mob_type = if kind == "minecraft:player" { self.remote.as_ref().and_then(|r| r.player_type.clone()) } else { - self.res.mob_types.get(kind).cloned() + self.res.mob_types().get(kind).cloned() }; + if kind == "minecraft:experience_orb" { + // Its value (ExperienceOrb.DATA_VALUE) follows in SetEntityData. + self.orbs.retain(|o| o.id != id as u32); + self.orbs.push(ExperienceOrb { + id: id as u32, + position: at, + previous: at, + velocity: Vec3::ZERO, + value: 1, + age: 0, + on_ground: false, + }); + } if let Some(t) = mob_type { let mut mob = Mob::new(id as u32, t, at, head); - mob.body_yaw = yaw; - mob.previous_body_yaw = yaw; + (mob.yaw, mob.previous_yaw) = (yaw, yaw); + (mob.head_pitch, mob.previous_head_pitch) = (pitch, pitch); mob.persistent = true; self.mobs.retain(|m| m.id != id as u32); self.mobs.push(mob); @@ -772,14 +1118,21 @@ impl Game<'_> { rotation: Option<(f32, f32)>, on_ground: bool, ) { + let mob = self.mobs.iter_mut().find(|m| m.id == id as u32); if let Some(r) = self.remote.as_mut() { if let Some(t) = r.entities.get_mut(&id) { t.position = position; } + // AbstractInterpolationHandler.interpolateTo: without a rotation, + // the one it was heading for (or has). + let rotation = rotation + .or_else(|| r.lerps.get(&id).and_then(|l| l.rotation)) + .or_else(|| mob.as_ref().map(|m| (m.yaw, m.head_pitch))); r.lerps.insert( id, Lerp { target: position, + rotation, steps: 3, }, ); @@ -787,34 +1140,38 @@ impl Game<'_> { *p = position; } } - if let Some(m) = self.mobs.iter_mut().find(|m| m.id == id as u32) { + if let Some(m) = mob { m.on_ground = on_ground; - if let Some((yaw, pitch)) = rotation { - m.body_yaw = yaw; - m.head_pitch = pitch; - } } + self.turn_remote_object(id, rotation.map(|(yaw, _)| yaw), on_ground); + self.turn_remote_projectile(id, rotation); } fn remove_entity(&mut self, id: i32) { self.mobs.retain(|m| m.id != id as u32); + self.orbs.retain(|o| o.id != id as u32); self.dropped.retain(|d| d.id != id as u32); + self.remove_remote_object(id as u32); + self.remove_remote_projectile(id as u32); if let Some(r) = self.remote.as_mut() { r.entities.remove(&id); r.lerps.remove(&id); + r.head_lerps.remove(&id); r.pending_items.remove(&id); } } /// Map a menu slot to Horizon's inventory screen slot ids. fn menu_slot(menu: &str, slot: i32) -> Option { let s = usize::try_from(slot).ok()?; - let rows = |n: usize| -> Option { + // A container of `n` slots, then the inventory and the hotbar. + let slots = |n: usize| -> Option { match s { - _ if s < n * 9 => Some(300 + s), - _ if s < n * 9 + 27 => Some(9 + s - n * 9), - _ if s < n * 9 + 36 => Some(s - n * 9 - 27), + _ if s < n => Some(300 + s), + _ if s < n + 27 => Some(9 + s - n), + _ if s < n + 36 => Some(s - n - 27), _ => None, } }; + let rows = |n: usize| slots(n * 9); match menu { "inventory" => match s { 0 => Some(200), @@ -852,6 +1209,10 @@ impl Game<'_> { "minecraft:generic_9x4" => rows(4), "minecraft:generic_9x5" => rows(5), "minecraft:generic_9x6" => rows(6), + // HopperMenu; DispenserMenu (dispensers and droppers). + "minecraft:hopper" => slots(5), + "minecraft:generic_3x3" => slots(9), + "minecraft:brewing_stand" => slots(5), _ => None, } } @@ -877,6 +1238,9 @@ impl Game<'_> { fn handle_packet(&mut self, packet: C) -> Result<()> { match packet { + // ClientCommonPacketListenerImpl.handlePing runs on the game + // thread, after all preceding packets (especially teleports). + C::Ping(p) => self.send(S::Pong(p)), // PingDebugMonitor.onPongReceived. C::PongResponse(p) => { let delay = self.debug_millis() - p.time; @@ -892,10 +1256,12 @@ impl Game<'_> { self.streaming.radius = self.settings.render_distance.min(p.chunk_radius + 1).max(2); self.enter_remote_dimension(&p.spawn, false); + let view = client_view_distance(self.settings.render_distance); + if let Some(r) = self.remote.as_mut() { + r.sent_view = view; + } self.send(S::ClientInformation( - horizon_protocol::packets::configuration::ClientInformation::new( - self.settings.render_distance.clamp(2, 32) as i8, - ), + horizon_protocol::packets::configuration::ClientInformation::new(view), )); } C::Respawn(p) => { @@ -923,6 +1289,15 @@ impl Game<'_> { }) .clamp(-90.0, 90.0); } + // ClientChunkCache.replaceWithPacketData: a column outside the + // storage around the view centre is ignored. + C::LevelChunkWithLight(p) if !self.remote_chunk_in_range(p.x, p.z) => { + log::warn!( + "Ignoring chunk since it's not in the view range: {}, {}", + p.x, + p.z + ); + } C::LevelChunkWithLight(p) => match self.remote_chunk(&p) { Ok(chunk) => { let (x, z) = (p.x, p.z); @@ -938,10 +1313,13 @@ impl Game<'_> { ); let state = self.world.block(pos.0, pos.1, pos.2); self.sync_block_entity(pos, state); + // LevelChunk.replaceWithPacketData: the block entity loads its update tag. + self.remote_block_entity_data([pos.0, pos.1, pos.2], info.kind, &info.tag); } } Err(e) => log::warn!("Horizon: chunk {} {}: {e}", p.x, p.z), }, + C::LightUpdate(p) => self.remote_light_update(&p), C::ForgetLevelChunk(p) => { let [x, z] = p.pos; self.streaming.forget(&mut self.world, x, z); @@ -978,7 +1356,7 @@ impl Game<'_> { // shulker boxes, bells) when the block is still the one it names. C::BlockEvent(p) => { let state = self.world.block(p.pos[0], p.pos[1], p.pos[2]); - let block = &self.res.facts.get(state).name; + let block = &self.res.facts().get(state).name; let named = self.remote.as_ref().and_then(|r| { r.builtin .get("minecraft:block")? @@ -992,10 +1370,13 @@ impl Game<'_> { C::LevelEvent(p) if p.kind == 2015 => { self.dragon_egg_particles(p.pos, p.data); } + C::LevelEvent(p) if p.kind == 2017 => { + self.consume_teleport_particles(p.pos, p.data); + } C::LevelEvent(p) if p.kind == 2001 => { // LevelEvent.PARTICLES_DESTROY_BLOCK: someone else broke a block. let state = BlockStateId(p.data.max(0) as u32); - let s = self.res.facts.get(state).sounds.clone(); + let s = self.res.facts().get(state).sounds.clone(); let at = [ f64::from(p.pos[0]) + 0.5, f64::from(p.pos[1]) + 0.5, @@ -1039,8 +1420,8 @@ impl Game<'_> { .map(str::to_owned) }) .unwrap_or_default(); - let yaw = horizon_protocol::buf::unpack_degrees(p.yaw); - let head = horizon_protocol::buf::unpack_degrees(p.head_yaw); + let rotation = + [p.yaw, p.pitch, p.head_yaw].map(horizon_protocol::buf::unpack_degrees); if let Some(r) = self.remote.as_mut() { if kind == "minecraft:player" { r.level_players.insert(p.id, p.uuid); @@ -1048,7 +1429,17 @@ impl Game<'_> { r.level_players.remove(&p.id); } } - self.spawn_entity(p.id, &kind, p.position, yaw, head); + self.spawn_entity(p.id, &kind, p.position, rotation); + if !self.spawn_remote_object(p.id, &kind, p.position, rotation[0], p.data) { + self.spawn_remote_projectile( + p.id, + &kind, + p.position, + (rotation[0], rotation[1]), + p.velocity, + p.data, + ); + } } C::RemoveEntities(p) => { for id in p.ids { @@ -1099,9 +1490,11 @@ impl Game<'_> { ]; self.move_entity(p.id, position, Some((p.yaw, p.pitch)), p.on_ground); } + // ClientPacketListener.handleRotateMob: lerpHeadTo(yHeadRot, 3). C::RotateHead(p) => { - if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) { - m.yaw = horizon_protocol::buf::unpack_degrees(p.head_yaw); + if let Some(r) = self.remote.as_mut() { + let yaw = horizon_protocol::buf::unpack_degrees(p.head_yaw); + r.head_lerps.insert(p.id, (yaw, 3)); } } C::SetEntityData(p) => { @@ -1129,17 +1522,72 @@ impl Game<'_> { } } } + // LivingEntity.onSyncedDataUpdated(DATA_LIVING_ENTITY_FLAGS): + // another player starts using the item in a hand (eating, a + // bow drawn, a shield up), or stops. + let other = self + .remote + .as_ref() + .is_some_and(|r| r.level_players.contains_key(&p.id) && r.entity_id != p.id); + if other && let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) { + for (index, _, value) in &p.values { + if let (8, Value::Byte(flags)) = (index, value) { + m.extra.using_off_hand = flags & 2 != 0; + if flags & 1 == 0 { + m.extra.using_item = 0; + } else if m.extra.using_item == 0 { + m.extra.using_item = 1; + } + } + } + } + // LivingEntity.onSyncedDataUpdated(SLEEPING_POS_ID): this + // player lies down in the bed, or gets up. + if self.remote.as_ref().is_some_and(|r| r.entity_id == p.id) { + for (index, _, value) in &p.values { + if let (14, Value::OptPos(bed)) = (index, value) { + if bed.is_some() && self.sleeping.is_none() { + self.sleep_timer = 0; + } + self.sleeping = *bed; + } + } + } + // Entity.DATA_SHARED_FLAGS_ID's crouching bit: another + // player's crouch (their model and Entity.isDiscrete's name tag). + if self + .remote + .as_ref() + .is_some_and(|r| r.level_players.contains_key(&p.id)) + && let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) + { + for (index, _, value) in &p.values { + if let (0, Value::Byte(flags)) = (index, value) { + m.extra.crouching = flags & 2 != 0; + } + } + } for (index, _, value) in &p.values { if *index == ITEM_ENTITY_ITEM && let Value::Item(stack) = value { self.remote_item_data(p.id, stack); } + // ExperienceOrb.DATA_VALUE (the icon's size). + if *index == 8 + && let Value::Int(v) = value + && let Some(o) = self.orbs.iter_mut().find(|o| o.id == p.id as u32) + { + o.value = (*v).max(0) as u32; + } } + self.remote_object_data(p.id, &p.values); + self.remote_projectile_data(p.id, &p.values); } // ClientPacketListener.handleSetEquipment: what a mob or another // player holds and wears (EquipmentSlot ordinals). C::SetEquipment(p) => { + self.remote_object_equipment(p.id, &p.slots); let items: Vec<(u8, Option>)> = p .slots .iter() @@ -1161,10 +1609,13 @@ impl Game<'_> { } } C::TakeItemEntity(p) => { - if self - .remote - .as_ref() - .is_some_and(|r| r.entity_id == p.player) + // An orb's sound comes from the server, at the orb. + let orb = self.orbs.iter().any(|o| o.id == p.item as u32); + if !orb + && self + .remote + .as_ref() + .is_some_and(|r| r.entity_id == p.player) && let Some(eye) = self.eye_position() { let pitch = ((self.world.random.next_float() - self.world.random.next_float()) @@ -1175,17 +1626,20 @@ impl Game<'_> { .play("minecraft:entity.item.pickup", Some(eye), 0.2, pitch); } // ItemPickupParticle toward the collector (this player, another one or a mob). + let collector = if self + .remote + .as_ref() + .is_some_and(|r| r.entity_id == p.player) + { + super::pickups::Collector::Player + } else { + super::pickups::Collector::Mob(p.player as u32) + }; if let Some(item) = self.dropped.iter().find(|d| d.id == p.item as u32).cloned() { - let collector = if self - .remote - .as_ref() - .is_some_and(|r| r.entity_id == p.player) - { - super::pickups::Collector::Player - } else { - super::pickups::Collector::Mob(p.player as u32) - }; self.add_pickup(super::pickups::Taken::Item(Box::new(item)), collector); + } else if let Some(orb) = self.orbs.iter().find(|o| o.id == p.item as u32).cloned() + { + self.add_pickup(super::pickups::Taken::Orb(Box::new(orb)), collector); } self.remove_entity(p.item); } @@ -1195,6 +1649,15 @@ impl Game<'_> { pl.velocity = Vec3::new(p.velocity[0], p.velocity[1], p.velocity[2]); } } + // ClientPacketListener.handleDamageEvent: LivingEntity.handleDamageEvent + // jolts the walk animation and restarts the hurt flash. Its hurt + // sound comes as a sound from Horizon's host, which plays it itself. + C::DamageEvent(p) => { + if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) { + m.limb_speed = 1.5; + m.hurt_time = 10; + } + } C::HurtAnimation(p) => { if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) { m.hurt_time = 10; @@ -1221,7 +1684,21 @@ impl Game<'_> { m.health = 0.0; } } - C::Animate(p) if p.action == 0 => {} + C::Animate(p) => self.remote_animate(p.id, p.action), + // ClientPacketListener.handleSwingAnimation: the entity swings + // (LivingEntity.swing, which a swing in its first half ignores). + C::SwingAnimation(p) => { + let hand = if p.hand == 1 { Hand::Off } else { Hand::Main }; + let animation = horizon_client::first_person::SwingAnimation { + kind: p.kind.clamp(0, 255) as u8, + duration: p.duration, + }; + if self.remote.as_ref().is_some_and(|r| r.entity_id == p.id) { + self.swing_hand(hand, animation); + } else if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) { + m.swing(); + } + } C::UpdateMobEffect(p) => self.remote_effect(&p), C::RemoveMobEffect(p) => { if self.remote.as_ref().is_some_and(|r| r.entity_id == p.id) @@ -1253,9 +1730,16 @@ impl Game<'_> { .map(|r| r.lang.clone()) .unwrap_or_default(); let message = plain(&p.message, &lang); + let me = self.remote.as_ref().is_some_and(|r| r.entity_id == p.id); if let Some(r) = self.remote.as_mut() { r.death_message = Some(message); } + // ClientPacketListener.handlePlayerCombatKill: the death + // screen, whatever health was last sent. + if me && self.screen != Screen::Dead && self.player.is_some() { + self.screen = Screen::Dead; + self.capture(false); + } } C::SetExperience(p) => { self.experience = Experience { @@ -1264,8 +1748,12 @@ impl Game<'_> { total: p.total, } } + // ClientPacketListener.handleSetTime: the game time, then + // ClientClockManager.handleUpdates: the clock catches up with the + // game time and takes the server's state. C::SetTime(p) => { self.world.game_time = p.game_time; + self.remote_clock_tick(); let clock = { let r = self.remote.as_ref(); let names = @@ -1286,10 +1774,26 @@ impl Game<'_> { if let Some(r) = self.remote.as_mut() { r.clock = Some(c.clock); r.clock_rate = c.rate; + r.clock_partial = c.partial_tick; } } } C::GameEvent(p) => match p.event { + // GUARDIAN_ELDER_EFFECT: the apparition, and the curse sound + // unless the elder guardian is silent. + 10 => { + self.creatures.elder_apparition = super::mob_blocks::APPARITION_TICKS; + if p.param == 1.0 + && let Some(at) = self.player.as_ref().map(|m| m.position) + { + self.audio.play( + "minecraft:entity.elder_guardian.curse", + Some([at.x, at.y, at.z]), + 1.0, + 1.0, + ); + } + } 1 => { self.world.weather.raining = true; self.world.weather.rain_level = (0.0, 0.0); @@ -1315,7 +1819,12 @@ impl Game<'_> { } _ => {} }, - C::PlayerAbilities(p) => self.creative = p.flags & 8 != 0, + C::PlayerAbilities(p) => { + self.creative = p.flags & 8 != 0; + if let Some(player) = &mut self.player { + player.flying_speed = p.flying_speed; + } + } C::SetHeldSlot(p) => { self.selected = p.slot.clamp(0, 8) as usize; if let Some(r) = self.remote.as_mut() { @@ -1354,20 +1863,34 @@ impl Game<'_> { self.remote_synced(); } C::ContainerSetData(p) => { - if let Some(key) = self.container - && let Some(BlockEntity::Furnace(f)) = self.world.block_entities.get_mut(&key) + let v = i32::from(p.value); + match self + .container + .and_then(|key| self.world.block_entities.get_mut(&key)) { - let v = i32::from(p.value); - match p.id { + Some(BlockEntity::Furnace(f)) => match p.id { 0 => f.lit_remaining = v, 1 => f.lit_total = v, 2 => f.cooking_timer = v, 3 => f.cooking_total = v, _ => {} - } + }, + // BrewingStandBlockEntity.dataAccess. + Some(BlockEntity::Brewing(b)) => match p.id { + 0 => b.brew_time = v, + 1 => b.fuel = v, + 2 => b.total_brew_time = v, + 3 => b.total_fuel = v, + _ => {} + }, + _ => {} } } C::OpenScreen(p) => self.remote_open_screen(p), + C::RecipeBookAdd(p) => self.remote_recipe_book_add(p), + C::RecipeBookRemove(p) => self.remote_recipe_book_remove(p), + C::RecipeBookSettings(p) => self.remote_recipe_book_settings(p), + C::PlaceGhostRecipe(p) => self.remote_ghost_recipe(p), C::MerchantOffers(p) => self.remote_merchant_offers(p), C::ContainerClose(_) => { if self.screen == Screen::Inventory { @@ -1376,6 +1899,20 @@ impl Game<'_> { self.capture(true); } } + C::Unknown(id, data) + if id == horizon_protocol::ids::play::clientbound::COMMAND_SUGGESTIONS => + { + self.handle_command_suggestions(&data); + } + C::Commands(p) => { + let tree = horizon_client::command::network::from_packet( + &p, + horizon_client::command::vanilla::dispatcher(), + ); + if let Some(r) = self.remote.as_mut() { + r.commands = Some(tree); + } + } C::SystemChat(p) => { let text = plain( &p.content, @@ -1385,10 +1922,9 @@ impl Game<'_> { .map(|r| r.lang.clone()) .unwrap_or_default(), ); + // ChatListener.handleOverlay. if p.overlay { - if let Some(r) = self.remote.as_mut() { - r.action_bar = Some((text, Instant::now())); - } + self.titles.set_overlay(remote_text(&p.content), false); } else { self.chat_line(text); } @@ -1556,16 +2092,79 @@ impl Game<'_> { if let Some(r) = self.remote.as_mut() { r.view_distance = p.distance; } + // ClientChunkCache.updateViewRadius: a new storage of that size. + self.drop_remote_chunks_out_of_range(); } - C::SetActionBarText(p) | C::SetTitleText(p) => { - let lang = self - .remote - .as_ref() - .map(|r| r.lang.clone()) - .unwrap_or_default(); - let text = plain(&p.text, &lang); + C::SetChunkCacheCenter(p) => { if let Some(r) = self.remote.as_mut() { - r.action_bar = Some((text, Instant::now())); + r.chunk_center = (p.x, p.z); + } + self.drop_remote_chunks_out_of_range(); + } + C::BlockEntityData(p) => self.remote_block_entity_data(p.pos, p.kind, &p.tag), + // handleSetSpawn: ClientLevel.setRespawnData, which spawn compasses point at. + C::SetDefaultSpawnPosition(p) => { + let [x, y, z] = p.pos.pos; + self.spawn = (p.pos.dimension == "minecraft:overworld") + .then(|| Vec3::new(f64::from(x) + 0.5, f64::from(y), f64::from(z) + 0.5)); + } + C::ServerData(p) => self.remote_server_data(p.icon.as_deref()), + // handleTickingState / handleTickingStep: the level's TickRateManager. + C::TickingState(p) => { + self.clock.set_rate(p.rate); + self.clock.set_frozen(p.frozen); + } + C::TickingStep(p) => self.clock.set_frozen_ticks_to_run(p.steps), + C::UpdateAdvancements(p) => { + let icons = p + .added + .iter() + .map(|a| { + a.display + .as_ref() + .map_or_else(String::new, |d| self.item_name(d.icon.item).to_string()) + }) + .collect(); + self.remote_advancements(p, icons); + } + // handleAwardStats: StatsCounter.setValue for each. + C::AwardStats(p) => { + let Some(r) = self.remote.as_ref() else { + return Ok(()); + }; + let mut known = Vec::new(); + for (ty, value, count) in p.stats { + let Some(ty) = r + .builtin + .get("minecraft:stat_type") + .and_then(|t| t.name(ty)) + else { + continue; + }; + let values = match ty { + "minecraft:mined" => "minecraft:block", + "minecraft:killed" | "minecraft:killed_by" => "minecraft:entity_type", + "minecraft:custom" => "minecraft:custom_stat", + _ => "minecraft:item", + }; + if let Some(key) = r.builtin.get(values).and_then(|v| v.name(value)) { + known.push((ty.to_owned(), key.to_owned(), count)); + } + } + for (ty, key, count) in known { + self.stats.values.entry(ty).or_default().insert(key, count); + } + } + // ClientPacketListener.setActionBarText, setTitleText, + // setSubtitleText, setTitlesAnimation and handleTitlesClear. + C::SetActionBarText(p) => self.titles.set_overlay(remote_text(&p.text), false), + C::SetTitleText(p) => self.titles.set_title(remote_text(&p.text)), + C::SetSubtitleText(p) => self.titles.set_subtitle(remote_text(&p.text)), + C::SetTitlesAnimation(p) => self.titles.set_times(p.fade_in, p.stay, p.fade_out), + C::ClearTitles(p) => { + self.titles.clear(); + if p.value { + self.titles.reset_times(); } } _ => {} @@ -1702,10 +2301,11 @@ impl Game<'_> { self.skin_slots.len() } else { let (old, slot) = self.skin_slots.remove(0); - self.atlas.sprites.remove(&old); + self.gui.atlas.sprites.remove(&old); slot }; let Some(rect) = self + .gui .atlas .sprites .get(&format!("horizon/skin{slot}")) @@ -1713,7 +2313,7 @@ impl Game<'_> { else { return; }; - let (w, h) = (self.atlas.width as f32, self.atlas.height as f32); + let (w, h) = (self.gui.atlas.width as f32, self.gui.atlas.height as f32); self.map_patches.push(horizon_client::font::Patch { x: (rect.u0 * w).round() as u32, y: (rect.v0 * h).round() as u32, @@ -1721,7 +2321,7 @@ impl Game<'_> { height: 64, rgba, }); - self.atlas.sprites.insert(location.clone(), rect); + self.gui.atlas.sprites.insert(location.clone(), rect); self.skin_slots.push((location, slot)); } /// Whether a downloaded skin is in the atlas, marking it recently shown. @@ -1790,6 +2390,7 @@ impl Game<'_> { self.dropped.push(ItemEntity { health: 5, fire_ticks: 0, + thrower: None, id: id as u32, stack, position: at, @@ -1843,10 +2444,11 @@ impl Game<'_> { p.pitch }) .clamp(-90.0, 90.0); - let delta = Vec3::new( - pick(rel::DELTA_X, velocity.x, p.delta[0]), - pick(rel::DELTA_Y, velocity.y, p.delta[1]), - pick(rel::DELTA_Z, velocity.z, p.delta[2]), + let delta = horizon_client::player_sync::teleport_velocity( + velocity, + (self.yaw, self.pitch), + (yaw, pitch), + p, ); match self.player.as_mut() { Some(player) => { @@ -1869,8 +2471,8 @@ impl Game<'_> { self.yaw = yaw; self.pitch = pitch; if let Some(r) = self.remote.as_mut() { - r.last_pos = [position.x, position.y, position.z]; - r.last_rot = (yaw, pitch); + // handleMovePlayer does not change LocalPlayer.sendPosition's + // baseline: the next tick must still report the corrected state. r.destroying = None; } self.mining = None; @@ -1887,11 +2489,34 @@ impl Game<'_> { /// One client tick while connected (Minecraft.tick with a ClientLevel). pub(super) fn tick_remote(&mut self) -> Result<()> { + self.tick_remote_level()?; + // Minecraft.tick sends this even when the player cannot tick yet, + // including while waiting for chunks. Keep it outside early returns. + if self.remote.as_ref().is_some_and(|r| r.joined) { + self.send(S::ClientTickEnd(Empty)); + } + Ok(()) + } + + fn tick_remote_level(&mut self) -> Result<()> { self.ticks += 1; - self.world.game_time += 1; - let rate = self.remote.as_ref().map_or(1.0, |r| r.clock_rate); - if rate > 0.0 { - self.world.day_time += rate.round() as i64; + // Options.broadcastOptions: a changed render distance is sent again. + let view = client_view_distance(self.settings.render_distance); + if let Some(r) = self.remote.as_mut() + && r.joined + && r.sent_view != view + { + r.sent_view = view; + self.send(S::ClientInformation( + horizon_protocol::packets::configuration::ClientInformation::new(view), + )); + } + // Minecraft.tick: TickRateManager.tick; ClientLevel.tickTime advances + // the time only while the level runs normally. + let running = self.clock.world_runs(); + if running { + self.world.game_time += 1; + self.remote_clock_tick(); } // Weather levels drift toward their targets between server updates. let w = &mut self.world.weather; @@ -1900,15 +2525,24 @@ impl Game<'_> { if self.use_delay > 0 { self.use_delay -= 1; } + // Minecraft.handleKeybinds precedes entity ticking and sendChanges. + // Window callbacks only queue clicks, so an interaction cannot leak + // between CLIENT_TICK_END and a later pong. + self.remote_handle_input(); // ClientLevel.tick: the border's size change. self.border.tick(); - self.tick_remote_entities(); + self.tick_remote_entities(running); // ClientLevel.tickBlockEntities: chest lids, shulker boxes, bells. self.tick_block_entity_anims(); self.tick_hand(); self.tick_particles(); self.tick_client_effects(); + // Player.tick's sleep counter (the darkening and the wake-up fade). + self.sleep_timer = super::tick_sleep_timer(self.sleep_timer, self.sleeping.is_some()); + // Gui.tick: the titles, the action bar and the held item's name. + self.tick_hud(); let ghast_collisions = self.happy_ghast_player_collisions(); + let pushed = self.remote_pushes(); let Some(player) = self.player.as_mut() else { return Ok(()); }; @@ -1917,6 +2551,15 @@ impl Game<'_> { (player.position.z.floor() as i32) >> 4, ); if !self.streaming.is_lit(cx, cz) { + // LocalPlayer.tick can wait for chunks, but Minecraft.tick still + // calls sendChanges: rotations and the 20-tick position reminder + // must continue, as well as moves into the unloaded area. + if let Some(r) = self.remote.as_ref() + && r.loaded_sent + { + let input = r.last_input; + self.remote_send_state(input); + } return Ok(()); } if self.remote.as_ref().is_some_and(|r| !r.loaded_sent) { @@ -1931,8 +2574,7 @@ impl Game<'_> { let Some(player) = self.player.as_mut() else { return Ok(()); }; - let chatting = self.remote.as_ref().is_some_and(|r| r.chat_input.is_some()); - let playing = self.screen == Screen::Playing && !chatting; + let playing = self.screen == Screen::Playing; let held = |k| playing && self.keys.contains(&k); let keys = Keys { forward: held(KeyCode::KeyW), @@ -1960,7 +2602,16 @@ impl Game<'_> { ); player.yaw = self.yaw; let dead = self.screen == Screen::Dead; - if !dead { + // LivingEntity.isImmobile: a sleeper lies still in the bed. + if let Some(head) = self.sleeping { + player.position = Vec3::new( + f64::from(head[0]) + 0.5, + f64::from(head[1]) + 0.5625, + f64::from(head[2]) + 0.5, + ); + player.velocity = Vec3::ZERO; + } else if !dead { + player.velocity = player.velocity.add_vector(pushed); MotionWorld { entities: &ghast_collisions, player, @@ -1981,7 +2632,7 @@ impl Game<'_> { horizon_client::motion::MotionEvent::Emission { state, in_water, .. } if !crouching => { - let sounds = self.res.facts.get(state).sounds.clone(); + let sounds = self.res.facts().get(state).sounds.clone(); // Player.playStepSound: muffled under the swim sound in water. let (volume, pitch) = if in_water { (sounds.volume * 0.05, sounds.pitch * 0.8) @@ -2003,25 +2654,95 @@ impl Game<'_> { // The player's own model (third person, the inventory's preview). self.tick_player_model(); if !dead { - self.remote_mining(); - if self.right && self.use_delay == 0 && playing { - self.remote_use(); - self.use_delay = 4; - } + self.remote_tick_using(); } self.remote_send_state(input); - self.send(S::ClientTickEnd(Empty)); Ok(()) } - fn tick_remote_entities(&mut self) { + /// The local player's share of every touching entity's + /// `LivingEntity.pushEntities`: on a client `EntitySelector.pushableBy` + /// lets entities push only the local player, so the server never sends + /// these pushes. `doPush(player)` is `player.push(entity)`. + fn remote_pushes(&self) -> Vec3 { + let (Some(p), Some(r)) = (self.player.as_ref(), self.remote.as_ref()) else { + return Vec3::ZERO; + }; + // LivingEntity.isPushable: alive, no spectator, off ladders. + let feet = [ + p.position.x.floor() as i32, + p.position.y.floor() as i32, + p.position.z.floor() as i32, + ]; + if self.screen == Screen::Dead + || p.spectator + || self.block_in_tag("minecraft:climbable", &self.block_name(feet)) + { + return Vec3::ZERO; + } + let bounds = p.bounds(); + let teams = self.mob_teams(); + let mut push = Vec3::ZERO; + for m in &self.mobs { + let spectator = r + .level_players + .get(&(m.id as i32)) + .and_then(|uuid| r.tab_players.get(uuid)) + .is_some_and(|t| t.game_mode == SPECTATOR); + // Bats never push; a spectator has no physics; passengers of the + // same vehicle never push each other. + if !m.alive() + || spectator + || m.species() == horizon_client::mobs::Species::Bat + || self + .riding + .is_some_and(|v| v == m.id || m.extra.mount == Some(v)) + || !m.bounds().intersects(bounds) + || !horizon_client::scoreboard::pushable_by( + teams.of(Who::Mob(m.id)), + teams.of(Who::Player), + true, + ) + { + continue; + } + // Entity.push(Entity), `this` the player. + let (mut xa, mut za) = (m.position.x - p.position.x, m.position.z - p.position.z); + let mut dd = xa.abs().max(za.abs()); + if dd < f64::from(0.01_f32) { + continue; + } + dd = dd.sqrt(); + xa /= dd; + za /= dd; + let pow = (1.0 / dd).min(1.0); + xa *= pow; + za *= pow; + xa *= f64::from(0.05_f32); + za *= f64::from(0.05_f32); + push = Vec3::new(push.x - xa, push.y, push.z - za); + } + push + } + /// ClientLevel.tickEntities; a frozen level only ticks the players + /// (`TickRateManager.isEntityFrozen`). + fn tick_remote_entities(&mut self, running: bool) { let Some(r) = self.remote.as_mut() else { return; }; let mut lerps = std::mem::take(&mut r.lerps); + let head_lerps = &mut r.head_lerps; + let players = &r.level_players; + let mut using = Vec::new(); for m in &mut self.mobs { + let player = players.contains_key(&(m.id as i32)); + if !running && !player { + continue; + } m.previous = m.position; m.previous_yaw = m.yaw; m.previous_body_yaw = m.body_yaw; + m.previous_head_yaw = m.head_yaw; + m.previous_head_pitch = m.head_pitch; m.previous_limb_speed = m.limb_speed; m.age += 1; if m.species() == horizon_client::mobs::Species::Rabbit { @@ -2036,6 +2757,10 @@ impl Game<'_> { if !m.alive() { m.death_time += 1; } + // LivingEntity.updateSwingTime. + m.anim.attack_anim_o = m.anim.attack_anim; + horizon_client::mobs::ai::tick_swing(m); + // Entity.commonTick: LinearInterpolationHandler.interpolate. if let Some(l) = lerps.get_mut(&(m.id as i32)) && l.steps > 0 { @@ -2045,8 +2770,49 @@ impl Game<'_> { m.position.y + (l.target[1] - m.position.y) / n, m.position.z + (l.target[2] - m.position.z) / n, ); + if let Some((yaw, pitch)) = l.rotation { + let alpha = 1.0 / l.steps as f32; + m.yaw += wrap_degrees(yaw - m.yaw) * alpha; + m.head_pitch += (pitch - m.head_pitch) * alpha; + } l.steps -= 1; } + // LivingEntity.aiStep: the head's own interpolation. + if let Some((yaw, steps)) = head_lerps.get_mut(&(m.id as i32)) + && *steps > 0 + { + m.head_yaw += wrap_degrees(*yaw - m.head_yaw) / *steps as f32; + *steps -= 1; + } + // LivingEntity.updatingUsingItem for another player: the ticks the + // item has been used before this one. + if player && m.extra.using_item > 0 { + let item = if m.extra.using_off_hand { + m.extra.off_hand.clone() + } else { + m.extra.main_hand.clone() + }; + if let Some(item) = item { + let eye = if m.extra.crouching { 1.27 } else { 1.62 }; + let at = [m.position.x, m.position.y, m.position.z]; + using.push(( + item, + m.extra.using_item as u32 - 1, + at, + eye, + m.head_pitch, + m.yaw, + )); + } + m.extra.using_item += 1; + } + // LivingEntity.tick's tickHeadTurn: a mob's BodyRotationControl, + // a player's body following its walk and look. + if player { + remote_player_head_turn(m); + } else if m.alive() { + m.body_rotation_tick(); + } // WalkAnimationState.update from the horizontal distance moved. let (dx, dz) = (m.position.x - m.previous.x, m.position.z - m.previous.z); let target = (((dx * dx + dz * dz).sqrt() * 4.0) as f32).min(1.0); @@ -2056,7 +2822,22 @@ impl Game<'_> { horizon_client::mobs::rabbit::post_tick(m); } } - for d in &mut self.dropped { + for o in self.orbs.iter_mut().filter(|_| running) { + o.previous = o.position; + o.age += 1; + if let Some(l) = lerps.get_mut(&(o.id as i32)) + && l.steps > 0 + { + let n = f64::from(l.steps); + o.position = Vec3::new( + o.position.x + (l.target[0] - o.position.x) / n, + o.position.y + (l.target[1] - o.position.y) / n, + o.position.z + (l.target[2] - o.position.z) / n, + ); + l.steps -= 1; + } + } + for d in self.dropped.iter_mut().filter(|_| running) { d.previous = d.position; d.age += 1; if let Some(l) = lerps.get_mut(&(d.id as i32)) @@ -2071,6 +2852,28 @@ impl Game<'_> { l.steps -= 1; } } + head_lerps.retain(|_, (_, steps)| *steps > 0); + for (item, ticks, at, eye, pitch, yaw) in using { + self.remote_player_eating(&item, ticks, at, eye, pitch, yaw); + } + if running { + // Boats, minecarts, armor stands, projectiles and the rest + // interpolate like mobs. + let mut step = |id: u32, at: Vec3| match lerps.get_mut(&(id as i32)) { + Some(l) if l.steps > 0 => { + let n = f64::from(l.steps); + l.steps -= 1; + Vec3::new( + at.x + (l.target[0] - at.x) / n, + at.y + (l.target[1] - at.y) / n, + at.z + (l.target[2] - at.z) / n, + ) + } + _ => at, + }; + self.tick_remote_objects(&mut step); + self.tick_remote_projectiles(&mut step); + } lerps.retain(|_, l| l.steps > 0); if let Some(r) = self.remote.as_mut() { for (id, l) in lerps { @@ -2078,6 +2881,47 @@ impl Game<'_> { } } } + /// `Consumable.onConsumeTick` for another player using a consumable: the + /// crumbs at their mouth every 4 ticks once 21.875% of its use has passed + /// (`shouldEmitParticlesAndSounds`; the sounds come from the server). + fn remote_player_eating( + &mut self, + item: &str, + ticks: u32, + at: [f64; 3], + eye: f64, + pitch: f32, + yaw: f32, + ) { + let Some(consumable) = self + .res + .items() + .get(item) + .and_then(|i| i.consumable.as_ref()) + .filter(|c| c.particles) + else { + return; + }; + let duration = consumable.ticks().max(0) as u32; + let remaining = duration - ticks.min(duration); + if !consumable.should_emit(remaining as i32) { + return; + } + let options = self.item_particle(&ItemStack::new(item, 1)); + let mut spawned = Vec::new(); + horizon_client::particles::item_use_particles( + &mut self.world.random, + pitch, + yaw, + at, + at[1] + eye, + 5, + |p, v| spawned.push((p, v)), + ); + for (p, v) in spawned { + self.particles.add(&options, p, v); + } + } /// LocalPlayer.sendPosition plus input, sprint and held-slot updates. fn remote_send_state(&mut self, input: u8) { let Some(p) = self.player.as_ref() else { @@ -2112,41 +2956,12 @@ impl Game<'_> { data: 0, })); } - r.reminder += 1; - let d = [ - pos[0] - r.last_pos[0], - pos[1] - r.last_pos[1], - pos[2] - r.last_pos[2], - ]; - let moved = d[0] * d[0] + d[1] * d[1] + d[2] * d[2] > 2.0e-4 * 2.0e-4 || r.reminder >= 20; - let rotated = yaw != r.last_rot.0 || pitch != r.last_rot.1; - let flags = u8::from(on_ground) | u8::from(collision) << 1; - let [x, y, z] = pos; - if moved && rotated { - out.push(S::MovePlayerPosRot(play::MovePlayerPosRot { - x, - y, - z, - yaw, - pitch, - flags, - })); - } else if moved { - out.push(S::MovePlayerPos(play::MovePlayerPos { x, y, z, flags })); - } else if rotated { - out.push(S::MovePlayerRot(play::MovePlayerRot { yaw, pitch, flags })); - } else if r.last_on_ground != on_ground || r.last_collision != collision { - out.push(S::MovePlayerStatus(play::MovePlayerStatus { flags })); + if let Some(packet) = r + .position_sender + .update(pos, (yaw, pitch), on_ground, collision) + { + out.push(packet); } - if moved { - r.last_pos = pos; - r.reminder = 0; - } - if rotated { - r.last_rot = (yaw, pitch); - } - r.last_on_ground = on_ground; - r.last_collision = collision; // Creative inventory edits are sent slot by slot (SetCreativeModeSlot). let mut creative_slots = Vec::new(); if self.creative { @@ -2189,17 +3004,12 @@ impl Game<'_> { } /// MultiPlayerGameMode.startDestroyBlock / continueDestroyBlock. - fn remote_mining(&mut self) { + fn remote_mining(&mut self, start: bool) { let Some(r) = self.remote.as_mut() else { return; }; - if r.destroy_delay > 0 { + if !start && r.destroy_delay > 0 { r.destroy_delay -= 1; - } - let chatting = r.chat_input.is_some(); - if !self.left || self.screen != Screen::Playing || chatting { - self.stop_remote_mining(); - self.mining = None; return; } let Some(hit) = self.pick() else { @@ -2208,14 +3018,9 @@ impl Game<'_> { }; let face = face_direction(hit.face); let state = self.world.block(hit.pos[0], hit.pos[1], hit.pos[2]); - if self.res.facts.is_air(state) { + if self.res.facts().is_air(state) { return; } - if self.remote.as_ref().is_some_and(|r| r.destroy_delay > 0) { - return; - } - self.swing_arm(); - self.send(S::Punch(Empty)); if self.creative { let seq = self.next_sequence(); self.predict_break(hit.pos, state, seq); @@ -2230,14 +3035,17 @@ impl Game<'_> { } return; } - let facts = self.res.facts.get(state).clone(); + let facts = self.res.facts().get(state).clone(); let held = self.held().cloned(); - let mut speed = self.res.items.mining_speed(held.as_ref(), &facts.name); + let mut speed = self.res.items().mining_speed(held.as_ref(), &facts.name); if self.player.as_ref().is_some_and(|p| !p.ground.on_ground) { speed /= 5.0; } - let harvest = - !facts.requires_tool || self.res.items.correct_for_drops(held.as_ref(), &facts.name); + let harvest = !facts.requires_tool + || self + .res + .items() + .correct_for_drops(held.as_ref(), &facts.name); let progress = if facts.hardness < 0.0 { 0.0 } else if facts.hardness == 0.0 { @@ -2250,6 +3058,9 @@ impl Game<'_> { .as_ref() .and_then(|r| r.destroying) .is_some_and(|(p, _, _)| p == hit.pos); + if start && same { + return; + } if !same { if let Some((old, _, _)) = self.remote.as_mut().and_then(|r| r.destroying.take()) { self.send(S::PlayerAction(play::PlayerAction { @@ -2344,7 +3155,7 @@ impl Game<'_> { .and_modify(|p| p.0 = sequence) .or_insert((sequence, state.0 as i32)); } - let s = self.res.facts.get(state).sounds.clone(); + let s = self.res.facts().get(state).sounds.clone(); let at = [ f64::from(pos[0]) + 0.5, f64::from(pos[1]) + 0.5, @@ -2364,15 +3175,25 @@ impl Game<'_> { } self.mining = None; } - /// Minecraft.startUseItem: use on the targeted block, then the item itself. + /// Minecraft.startUseItem: use one crosshair target, then the item itself. fn remote_use(&mut self) { - let held_block = self - .held() - .and_then(|h| self.res.items.get(&h.item)) - .is_some_and(|i| i.block.is_some()); + // Minecraft.startUseItem returns before resetting rightClickDelay + // while a survival block break is active. + if self.remote.as_ref().is_some_and(|r| r.destroying.is_some()) { + return; + } + self.use_delay = 4; + // Resolve the crosshair once. A passing entity interaction keeps + // that target and may only fall back to using the item itself. + let entity = self.entity_targeted(); + horizon_client::interaction_input::use_on_target(&mut RemoteUseWorld(self), entity); + } + + fn remote_use_entity(&mut self) -> bool { let sneaking = self.keys.contains(&KeyCode::ShiftLeft); - // Entities first (interact), like the hit result order. - if let Some((i, _)) = self.mob_hit() { + if let Some((i, distance)) = self.mob_hit() + && self.before_block(distance) + { let id = self.mobs[i].id as i32; self.send(S::Interact(play::Interact { id, @@ -2380,10 +3201,30 @@ impl Game<'_> { location: [0.0, 1.0, 0.0], secondary: sneaking, })); - self.swing_arm(); - return; + // Player.interactOn: another player passes, and the held item is + // used instead (Minecraft.startUseItem). + let player = self + .remote + .as_ref() + .is_some_and(|r| r.level_players.contains_key(&id)); + if !player { + self.swing_arm(); + return true; + } } + false + } + + /// MultiPlayerGameMode.useItemOn for `hand`: whether the click ends there + /// (the block reacts, or a block item is placed). + fn remote_use_block(&mut self, hand: Hand) -> bool { + let held = self.with_hand(hand, |g| g.held().cloned()); + let held_block = held + .as_ref() + .and_then(|h| self.res.items().get(&h.item)) + .is_some_and(|i| i.block.is_some()); if let Some(hit) = self.pick() { + let reacts = hand == Hand::Main && self.remote_block_reacts(&hit, held.as_ref()); let seq = self.next_sequence(); let cursor = [ (hit.point[0] - f64::from(hit.pos[0])) as f32, @@ -2391,7 +3232,7 @@ impl Game<'_> { (hit.point[2] - f64::from(hit.pos[2])) as f32, ]; self.send(S::UseItemOn(play::UseItemOn { - hand: 0, + hand: hand_id(hand), pos: hit.pos, face: i32::from(face_direction(hit.face)), cursor, @@ -2410,66 +3251,257 @@ impl Game<'_> { r.placing = Some((target, ticks)); } if held_block { - self.swing_arm(); - return; + self.with_hand(hand, Game::swing_arm); + return true; } + return reacts; } - if self.held().is_some() { - let seq = self.next_sequence(); - self.send(S::UseItem(play::UseItem { - hand: 0, - sequence: seq, - yaw: self.yaw, - pitch: self.pitch, - })); + false + } + + /// Whether the block hit takes a main-hand right click with `held` + /// (`BlockState.useItemOn`, then `useWithoutItem`): a door, a button, a + /// container. Vanilla's client runs it on its own level first; this runs + /// it on a copy that keeps no change. + fn remote_block_reacts(&self, hit: &super::Hit, held: Option<&ItemStack>) -> bool { + // Player.isSecondaryUseActive with something in either hand. + let hands_full = self.stack_in(self.selected).is_some() || self.offhand.is_some(); + if self.keys.contains(&KeyCode::ShiftLeft) && hands_full { + return false; + } + use horizon_core::geometry::Direction; + let face = match hit.face { + [0, 1, 0] => Direction::Up, + [0, -1, 0] => Direction::Down, + [0, 0, -1] => Direction::North, + [0, 0, 1] => Direction::South, + [-1, 0, 0] => Direction::West, + _ => Direction::East, + }; + let mut level = DryLevel { + world: &self.world, + changed: HashMap::new(), + random: horizon_core::random::LegacyRandom::new(0), + }; + self.res + .behaviours() + .use_item_on( + &mut level, + &mut horizon_world::level::Ticks::default(), + horizon_core::position::BlockPos::new(hit.pos[0], hit.pos[1], hit.pos[2]), + face, + horizon_core::geometry::Vec3::new(hit.point[0], hit.point[1], hit.point[2]), + self.use_context(), + held.map(|s| &*s.item), + ) + .consumes_action() + } + + /// MultiPlayerGameMode.useItem for `hand`, if it holds something: whether + /// the click ends there, as far as this client can tell (`item_has_use`). + fn remote_use_item(&mut self, hand: Hand) -> bool { + let Some(held) = self.with_hand(hand, |g| g.held().cloned()) else { + return false; + }; + let seq = self.next_sequence(); + self.send(S::UseItem(play::UseItem { + hand: hand_id(hand), + sequence: seq, + yaw: self.yaw, + pitch: self.pitch, + })); + let Some(info) = self.res.items().get(&held.item).cloned() else { + return false; + }; + // MultiPlayerGameMode.useItem runs the use here too: food starts + // being eaten (Consumable.startConsuming). + self.with_hand(hand, |g| g.start_eating(&info)); + item_has_use(&info) + } + + /// The rest of Minecraft.startUseItem once the main hand's target passed: + /// the main hand's item, then the off hand's turn (its block, its item). + /// The server does each packet's part with that packet's hand only. + fn remote_use_hands(&mut self) { + if self.remote_use_item(Hand::Main) { + return; + } + if !self.entity_targeted() && self.remote_use_block(Hand::Off) { + return; + } + self.remote_use_item(Hand::Off); + } + /// `LivingEntity.updatingUsingItem` for a consumable on a server client: the + /// eating animation and its crumbs. The server eats it and sends the + /// stack, the hunger and the sounds. + fn remote_tick_using(&mut self) { + let Some((slot, ticks)) = self.using else { + return; + }; + let stack = self.stack_in(slot).cloned(); + let consumable = stack + .as_ref() + .and_then(|s| self.res.items().get(&s.item)) + .and_then(|i| i.consumable.clone()); + let (Some(stack), Some(consumable)) = (stack, consumable) else { + self.using = None; + return; + }; + // Button release belongs to the next handleKeybinds phase, which + // sends RELEASE_USE_ITEM. Do not silently clear a use begun by a + // quick press/release between ticks. + if slot != self.selected { + self.using = None; + return; + } + // Consumable.shouldEmitParticlesAndSounds, as in tick_used_item. + let duration = consumable.ticks().max(0) as u32; + let remaining = duration - ticks.min(duration); + if consumable.particles && consumable.should_emit(remaining as i32) { + self.eat_particles(&stack, 5); + } + if ticks + 1 >= duration { + self.using = None; + if consumable.particles { + self.eat_particles(&stack, 16); + } + } else { + self.using = Some((slot, ticks + 1)); } } - /// Left click: attack the targeted entity (MultiPlayerGameMode.attack). + /// MouseHandler updates KeyMapping; handleKeybinds sends packets on a tick. pub(super) fn remote_mouse(&mut self, button: MouseButton, pressed: bool) -> Result<()> { - if self.remote.as_ref().is_some_and(|r| r.chat_input.is_some()) { - return Ok(()); - } - match button { + use horizon_client::interaction_input::Button; + let input = match button { MouseButton::Left => { - if pressed && let Some((i, _)) = self.mob_hit() { - let id = self.mobs[i].id as i32; - self.send(S::Attack(play::Attack { id })); - self.send(S::Punch(Empty)); - self.swing_arm(); - return Ok(()); - } - if pressed && self.pick().is_none() { - self.send(S::Punch(Empty)); - self.swing_arm(); - } - self.left = pressed; + self.toggles + .button(2, pressed, false, self.settings.toggle_attack); + self.left = self.toggles.is_down(2); + Some(Button::Attack) } MouseButton::Right => { - self.right = pressed; - if pressed { - self.remote_use(); - self.use_delay = 4; - } else { - self.send(S::PlayerAction(play::PlayerAction { - action: 6, - pos: [0; 3], - direction: 0, - sequence: 0, - })); - } + self.toggles + .button(3, pressed, false, self.settings.toggle_use); + self.right = self.toggles.is_down(3); + Some(Button::Use) } - MouseButton::Middle if pressed => { - if let Some(hit) = self.pick() { - self.send(S::PickItemFromBlock(play::PickItemFromBlock { - pos: hit.pos, - include_data: self.has_control_down(), - })); - } - } - _ => {} + MouseButton::Middle => Some(Button::Pick), + _ => None, + }; + if pressed + && let Some(button) = input + && let Some(remote) = self.remote.as_mut() + { + remote.input.press(button); } Ok(()) } + + pub(super) fn request_remote_mining_stop(&mut self) { + if let Some(r) = self.remote.as_mut() { + r.stop_mining = true; + } + } + + fn remote_handle_input(&mut self) { + let Some(r) = self.remote.as_mut() else { + return; + }; + let mut input = std::mem::take(&mut r.input); + let stop = std::mem::take(&mut r.stop_mining); + let ready = r.loaded_sent && self.screen == Screen::Playing && self.sleeping.is_none(); + if stop || !ready { + self.stop_remote_mining(); + } + if !ready { + return; + } + // MultiPlayerGameMode.tick syncs the carried slot before keybinds. + let carried_changed = self.remote.as_mut().is_some_and(|r| { + if r.carried == Some(self.selected) { + false + } else { + r.carried = Some(self.selected); + true + } + }); + if carried_changed { + self.send(S::SetCarriedItem(play::SetCarriedItem { + slot: self.selected as i16, + })); + } + let (attack, use_item) = (self.left, self.right); + input.tick(&mut RemoteInputWorld(self), attack, use_item); + if self.clicks.miss_time > 0 { + self.clicks.miss_time -= 1; + } + if let Some(r) = self.remote.as_mut() { + r.input = input; + } + } + + fn remote_start_attack(&mut self) -> bool { + if self.clicks.miss_time > 0 { + return false; + } + if self.spectator() { + return true; + } + let mut instant = false; + if let Some((i, distance)) = self.mob_hit() + && self.before_block(distance) + { + self.send(S::Attack(play::Attack { + id: self.mobs[i].id as i32, + })); + self.reset_attack_strength(); + } else if !self.entity_targeted() + && let Some(hit) = self.pick() + { + self.remote_mining(true); + instant = self + .res + .facts() + .is_air(self.world.block(hit.pos[0], hit.pos[1], hit.pos[2])); + } else { + if !self.creative { + self.clicks.miss_time = 10; + } + self.reset_attack_strength(); + } + self.swing_arm(); + self.send(S::Punch(Empty)); + instant + } + + fn remote_continue_attack(&mut self, down: bool) { + if !down { + self.clicks.miss_time = 0; + } + if self.clicks.miss_time > 0 || self.using.is_some() { + return; + } + let entity = self.entity_targeted(); + if down + && !entity + && let Some(hit) = self.pick() + { + let state = self.world.block(hit.pos[0], hit.pos[1], hit.pos[2]); + if !self.res.facts().is_air(state) { + self.remote_mining(false); + self.swing_arm(); + self.send(S::Punch(Empty)); + } else { + if let Some(r) = self.remote.as_mut() { + r.destroying = None; + } + self.mining = None; + } + } else { + self.stop_remote_mining(); + self.mining = None; + } + } /// Keys with a multiplayer meaning; returns true when consumed. pub(super) fn remote_key(&mut self, key: KeyCode) -> Result { if self.screen != Screen::Playing { @@ -2485,14 +3517,8 @@ impl Game<'_> { } match key { KeyCode::KeyT | KeyCode::Slash | KeyCode::Enter => { - let text = if key == KeyCode::Slash { "/" } else { "" }; - if let Some(r) = self.remote.as_mut() { - r.chat_input = Some(text.to_owned()); - } - self.keys.clear(); - self.left = false; - self.right = false; - self.capture(false); + // ChatScreen, over the server's command tree. + self.open_chat(if key == KeyCode::Slash { "/" } else { "" }); Ok(true) } KeyCode::KeyQ => { @@ -2529,84 +3555,6 @@ impl Game<'_> { _ => Ok(false), } } - pub(super) fn remote_chat_open(&self) -> bool { - self.remote.as_ref().is_some_and(|r| r.chat_input.is_some()) - } - /// ChatScreen: typing, Enter to send, Escape to cancel. - pub(super) fn remote_chat_key(&mut self, event: &winit::event::KeyEvent) { - use winit::keyboard::Key; - if event.state != winit::event::ElementState::Pressed { - return; - } - let shortcut = self.edit_shortcut(event); - let Some(r) = self.remote.as_mut() else { - return; - }; - let Some(input) = r.chat_input.as_mut() else { - return; - }; - // ChatScreen's input: an EditBox of 256. - const INPUT: horizon_client::clipboard::FieldId = ("remote.chat", 0); - if let Some(shortcut) = shortcut { - Self::append_shortcut( - &mut self.text_selection, - &mut self.clipboard, - INPUT, - input, - shortcut, - 256, - ); - return; - } - // Logical keys: the text, not the scancode, decides (layouts, virtual keyboards). - let named = match &event.logical_key { - Key::Named(n) => Some(*n), - _ => None, - }; - self.remote_chat_input(named, event.text.as_deref()); - } - /// A named key or typed text in the chat input (the keyboard, or a - /// controller's on-screen keyboard). - pub(super) fn remote_chat_input( - &mut self, - named: Option, - text: Option<&str>, - ) { - use winit::keyboard::NamedKey; - const INPUT: horizon_client::clipboard::FieldId = ("remote.chat", 0); - let Some(r) = self.remote.as_mut() else { - return; - }; - let Some(input) = r.chat_input.as_mut() else { - return; - }; - match named { - Some(NamedKey::Escape) => { - r.chat_input = None; - self.capture(true); - } - Some(NamedKey::Backspace) => { - if !self.text_selection.take(INPUT, input) { - input.pop(); - } - } - Some(NamedKey::Enter) => { - let message = r.chat_input.take().unwrap_or_default(); - self.send_chat(&message); - self.capture(true); - } - _ => { - if let Some(text) = text { - self.text_selection.take(INPUT, input); - for c in text.chars().filter(|c| !c.is_control()) { - if input.chars().count() < 256 { - input.push(c); - } - } - } - } - } - } /// ClientPacketListener.sendChat / sendCommand (unsigned). pub(super) fn send_chat(&mut self, message: &str) { let message = message.trim(); @@ -2658,17 +3606,108 @@ impl Game<'_> { let Some(internal) = self.slot_at(&batch, self.cursor.0, self.cursor.1) else { return; }; - let input = if self.has_shift_down() { 1 } else { 0 }; - self.remote_slot_click(internal, i8::from(right), input); + let input = if self.has_shift_down() { + Input::QuickMove + } else { + Input::Pickup + }; + self.remote_menu_ops(&[(Some(internal), i32::from(right), input)]); + } + /// The server's open menu: its window id and menu type (0, "inventory" + /// when none is open). + fn remote_menu(&self) -> (i32, String) { + match self.remote.as_ref().and_then(|r| r.menu.clone()) { + Some((id, name)) => (id, name), + None => (0, "inventory".to_owned()), + } + } + /// `MultiPlayerGameMode.handleContainerInput` for each click of `ops` on + /// the server's open menu (a quick craft drag is several): the verified + /// menu engine predicts them on this client's copy, then each goes out as + /// a `ContainerClick` with the slots it changed and the carried stack + /// after it; the server corrects anything it sees differently. A menu the + /// engine doesn't model sends the clicks unpredicted. False when a click's + /// slot isn't one of the menu's. + pub(super) fn remote_menu_ops(&mut self, ops: &[(Option, i32, Input)]) -> bool { + let (container, menu) = self.remote_menu(); + let mut clicks = Vec::with_capacity(ops.len()); + for &(slot, button, input) in ops { + let slot = match slot { + None => -999, + Some(internal) => { + match (0..64).find(|&s| Self::menu_slot(&menu, s) == Some(internal)) { + Some(s) => s as i16, + None => return false, + } + } + }; + clicks.push((slot, button, input)); + } + // Unpredicted: nothing changed, the carried stack as it is. + let mut sent: Vec = clicks + .iter() + .map(|&(slot, button, input)| (slot, button, input, Vec::new(), self.carried.clone())) + .collect(); + // The engine's slot indices are the menu's own, as on the network. + self.menu_session(|m, env, _| { + let mut predicted = Vec::with_capacity(clicks.len()); + for &(slot, button, input) in &clicks { + let before = menu_items(m); + if m.click(env, i32::from(slot), button, input).is_err() { + // The server throws on it too: nothing is predicted. + return Some(false); + } + let changed = changed_slots(&before, m); + predicted.push((slot, button, input, changed, m.carried.clone())); + } + sent = predicted; + Some(true) + }); + self.send_clicks(container, sent); + true + } + /// A Shift double click on a server (`AbstractContainerScreen.mouseReleased`): + /// a predicted QUICK_MOVE for every slot the engine moves, each sent. + pub(super) fn remote_quick_move_all(&mut self, slot: usize, moved: &ItemStack) { + let (container, _) = self.remote_menu(); + let mut sent: Vec = Vec::new(); + self.menu_session(|m, env, to_menu| { + let clicked = to_menu(slot)?; + let mut before = menu_items(m); + let mut each = |m: &horizon_client::menu::Menu, t: usize| { + let changed = changed_slots(&before, m); + sent.push((t as i16, 0, Input::QuickMove, changed, m.carried.clone())); + before = menu_items(m); + }; + Some( + m.quick_move_all_each(env, clicked, moved, &mut each) + .is_ok(), + ) + }); + self.send_clicks(container, sent); + } + fn send_clicks(&mut self, container: i32, clicks: Vec) { + let state = self.remote.as_ref().map_or(0, |r| r.state_id); + let hashed = |g: &Self, s: &Option| g.to_network(s).map(|s| Hashed::of(&s)); + for (slot, button, input, changed, carried) in clicks { + let changed = changed.iter().map(|(i, s)| (*i, hashed(self, s))).collect(); + let carried = hashed(self, &carried); + self.send(S::ContainerClick(play::ContainerClick { + container, + state, + slot, + button: button as i8, + input: input_ordinal(input), + changed, + carried, + })); + } } /// A `ContainerClick` on game slot `internal` of the server's open menu /// (`input` is the ContainerInput ordinal). False when the menu has no /// such slot. pub(super) fn remote_slot_click(&mut self, internal: usize, button: i8, input: i32) -> bool { - let (container, menu) = match self.remote.as_ref().and_then(|r| r.menu.clone()) { - Some((id, name)) => (id, name), - None => (0, "inventory".to_owned()), - }; + let (container, menu) = self.remote_menu(); let slot = (0..64).find(|&s| Self::menu_slot(&menu, s) == Some(internal)); let Some(slot) = slot else { return false }; let carried = self.to_network(&self.carried).map(|s| Hashed::of(&s)); @@ -2853,6 +3892,14 @@ impl Game<'_> { speed: 1.0, })) } + // HopperMenu, and DispenserMenu (MenuType.GENERIC_3x3: dispensers + // and droppers). + "minecraft:hopper" => Some(BlockEntity::Chest(vec![None; 5])), + // BrewingStandMenu: its times come in ContainerSetData. + "minecraft:brewing_stand" => Some(BlockEntity::Brewing(Box::new( + horizon_client::brewing::BrewingStand::new(), + ))), + "minecraft:generic_3x3" => Some(BlockEntity::Chest(vec![None; 9])), m if m.starts_with("minecraft:generic_9x") || m == "minecraft:shulker_box" => { let rows = m .strip_prefix("minecraft:generic_9x") @@ -2888,7 +3935,7 @@ impl Game<'_> { // an ender chest's or a double chest's slots are no one block's, // so they go under a key no block has. let holds = |k: &(i32, i32, i32)| { - let name = &self.res.facts.get(self.world.block(k.0, k.1, k.2)).name; + let name = &self.res.facts().get(self.world.block(k.0, k.1, k.2)).name; BlockEntity::for_block(name).is_some_and(|e| { e.id() == entity.id() && e.slots().len() == entity.slots().len() }) @@ -2900,6 +3947,14 @@ impl Game<'_> { self.screen = Screen::Inventory; self.capture(false); } + /// The type of the menu a server opened (`MenuType`'s id). + pub(super) fn remote_menu_type(&self) -> Option<&str> { + Some(self.remote.as_ref()?.menu.as_ref()?.1.as_str()) + } + /// The title the server gave the menu it opened. + pub(super) fn remote_menu_title(&self) -> Option { + self.remote.as_ref()?.menu_title.clone() + } /// A server's chest-like menu (`MenuType.GENERIC_9xN`): its rows and title. pub(super) fn remote_chest_menu(&self) -> Option<(usize, String)> { let r = self.remote.as_ref()?; @@ -2912,6 +3967,15 @@ impl Game<'_> { .ok()?; Some((rows, r.menu_title.clone().unwrap_or_default())) } + /// ServerboundPlayerCommandPacket STOP_SLEEPING. + pub(super) fn remote_stop_sleeping(&mut self) { + let id = self.remote.as_ref().map_or(0, |r| r.entity_id); + self.send(S::PlayerCommand(play::PlayerCommand { + id, + action: 0, + data: 0, + })); + } pub(super) fn remote_respawn(&mut self) { self.send(S::ClientCommand(play::ClientCommand { action: 0 })); } @@ -2922,7 +3986,7 @@ impl Game<'_> { if self.player.is_some() { return false; } - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let (w, h) = (batch.width, batch.height); batch.fill(atlas, 0.0, 0.0, w, h, rgba(20, 20, 20, 255)); let shown = horizon_client::net::shown_address(&r.session.address); @@ -2944,59 +4008,11 @@ impl Game<'_> { let _ = r.joined; true } - /// Chat (ChatComponent), the chat input line and the action bar. + /// The server's death message; chat uses the shared ChatComponent. pub(super) fn remote_hud(&self, batch: &mut HudBatch) { let Some(r) = &self.remote else { return }; - let atlas = &self.atlas; - let (w, h) = (batch.width, batch.height); - let open = r.chat_input.is_some(); - let now = Instant::now(); - let mut y = h - 48.0; - // Container screens cover the chat (vanilla draws them over the HUD). - let shown = if matches!(self.screen, Screen::Playing | Screen::Dead) { - if open { 20 } else { 10 } - } else { - 0 - }; - for (text, at) in r.chat.iter().rev().take(shown) { - let age = now.duration_since(*at); - // Lines fade out after ten seconds unless the chat is open. - let alpha = if open { - 1.0 - } else { - (1.0 - (age.as_secs_f32() - 9.0).max(0.0)).clamp(0.0, 1.0) - }; - if alpha <= 0.0 { - break; - } - let a = (alpha * 255.0) as u8; - batch.fill( - atlas, - 2.0, - y - 1.0, - 320.0, - 9.0, - rgba(0, 0, 0, (alpha * 128.0) as u8), - ); - batch.text(atlas, text, 4.0, y, rgba(255, 255, 255, a), true); - y -= 9.0; - } - if let Some(input) = &r.chat_input { - batch.fill(atlas, 2.0, h - 14.0, w - 4.0, 12.0, rgba(0, 0, 0, 128)); - let blink = (self.started.elapsed().as_millis() / 300).is_multiple_of(2); - let shown = if blink { - format!("{input}_") - } else { - input.clone() - }; - batch.text(atlas, &shown, 4.0, h - 12.0, rgba(224, 224, 224, 255), true); - } - if let Some((text, at)) = &r.action_bar - && now.duration_since(*at) < Duration::from_secs(3) - { - let tw = HudBatch::text_width(atlas, text); - batch.text(atlas, text, (w - tw) / 2.0, h - 68.0, u32::MAX, true); - } + let atlas = &self.gui.atlas; + let w = batch.width; if self.screen == Screen::Dead && let Some(message) = &r.death_message { @@ -3017,12 +4033,29 @@ impl Game<'_> { .filter_map(|(id, uuid)| Some((*id as u32, r.players.get(uuid)?.clone()))) .collect() } + /// `ClientClockManager.tick`: the clock moves on by the game time that + /// passed since its last tick, at its rate. A game time the server sets + /// forward (its sync every second) moves the clock with it. + fn remote_clock_tick(&mut self) { + let Some(r) = self.remote.as_mut() else { + return; + }; + let delta = self.world.game_time - r.clock_game_time; + r.clock_game_time = self.world.game_time; + let partial = f64::from(r.clock_partial) + delta as f64 * f64::from(r.clock_rate); + let full = partial.floor(); + r.clock_partial = (partial - full) as f32; + self.world.day_time += full as i64; + } pub(super) fn remote_status(&self) -> Option { let r = self.remote.as_ref()?; Some(serde_json::json!({ "address": horizon_client::net::shown_address(&r.session.address), "name": r.session.name, "joined": r.joined, + // ServerboundPlayerLoadedPacket sent: the level around the + // player is ready, and the client ticks and sends its moves. + "loaded": r.loaded_sent, "status": r.status, "entity_id": r.entity_id, "dimension": r.dimension.name, @@ -3044,6 +4077,8 @@ impl Game<'_> { let armor = [&e.head, &e.chest, &e.legs, &e.feet].map(|s| s.as_deref()); serde_json::json!({ "id": m.id, + "swinging": m.ai.swinging, + "using": m.extra.using_item, "armor": armor, "main_hand": e.main_hand.as_deref(), "off_hand": e.off_hand.as_deref(), @@ -3056,6 +4091,13 @@ impl Game<'_> { .filter(|t| t.kind == "minecraft:player") .map(|t| t.position) .collect::>(), + // The other players this client draws dead (lying down, after EntityEvent DEATH). + "dead_players": self + .mobs + .iter() + .filter(|m| r.level_players.contains_key(&(m.id as i32)) && !m.alive()) + .map(|m| m.id) + .collect::>(), // The player's own model exists (third person and the inventory draw it). "player_model": self.player_model.is_some(), "health": r.health.0, @@ -3066,6 +4108,224 @@ impl Game<'_> { } } +/// `Mth.wrapDegrees`. +fn wrap_degrees(a: f32) -> f32 { + let a = a % 360.0; + if a >= 180.0 { + a - 360.0 + } else if a < -180.0 { + a + 360.0 + } else { + a + } +} + +/// `LivingEntity.tick`'s body target and `tickHeadTurn` for another player: +/// the body turns toward where it walks (backwards when walking back), or to +/// the look while the arm swings, and stays within 50 degrees of the look. +fn remote_player_head_turn(m: &mut Mob) { + let (dx, dz) = (m.position.x - m.previous.x, m.position.z - m.previous.z); + let mut target = m.body_yaw; + if (dx * dx + dz * dz) as f32 > 0.002_500_000_2 { + let walk = (dz.atan2(dx) as f32).to_degrees() - 90.0; + let facing = (wrap_degrees(m.yaw) - walk).abs(); + target = if 95.0 < facing && facing < 265.0 { + walk - 180.0 + } else { + walk + }; + } + if m.anim.attack_anim > 0.0 { + target = m.yaw; + } + m.body_yaw += wrap_degrees(target - m.body_yaw) * 0.3; + let head = wrap_degrees(m.yaw - m.body_yaw); + if head.abs() > 50.0 { + m.body_yaw += head - head.signum() * 50.0; + } +} + +/// A click sent to a server: its slot, button and input, the slots it +/// changed and the carried stack after it. +type Predicted = ( + i16, + i32, + Input, + Vec<(i16, Option)>, + Option, +); + +fn menu_items(m: &horizon_client::menu::Menu) -> Vec> { + (0..m.slot_count()).map(|i| m.slot(i).cloned()).collect() +} + +/// The slots whose stack is no longer `before`'s (`ItemStack.matches`). +fn changed_slots( + before: &[Option], + m: &horizon_client::menu::Menu, +) -> Vec<(i16, Option)> { + (0..m.slot_count()) + .filter(|&i| m.slot(i) != before.get(i).and_then(Option::as_ref)) + .map(|i| (i as i16, m.slot(i).cloned())) + .collect() +} + +/// ServerboundContainerClickPacket's `ContainerInput` ordinal. +pub(super) fn input_ordinal(input: Input) -> i32 { + match input { + Input::Pickup => 0, + Input::QuickMove => 1, + Input::Swap => 2, + Input::Clone => 3, + Input::Throw => 4, + Input::QuickCraft => 5, + Input::PickupAll => 6, + } +} + +/// The client's world for a guess at what a click does: reads go to the +/// world, writes stay here and are dropped. +struct DryLevel<'w> { + world: &'w horizon_client::world::World, + changed: HashMap, + random: horizon_core::random::LegacyRandom, +} + +impl horizon_world::level::LevelAccess for DryLevel<'_> { + fn block_state(&self, pos: horizon_core::position::BlockPos) -> BlockStateId { + self.changed + .get(&pos) + .copied() + .unwrap_or_else(|| self.world.block(pos.x, pos.y, pos.z)) + } + fn set_raw( + &mut self, + pos: horizon_core::position::BlockPos, + state: BlockStateId, + ) -> Option { + let previous = self.block_state(pos); + self.changed.insert(pos, state); + Some(previous) + } + fn is_loaded(&self, pos: horizon_core::position::BlockPos) -> bool { + self.world.loaded(pos.x, pos.z) + } + fn random(&mut self) -> &mut horizon_core::random::LegacyRandom { + &mut self.random + } + fn game_time(&self) -> i64 { + self.world.game_time + } +} + +/// `InteractionHand` on the wire. +fn hand_id(hand: Hand) -> i32 { + match hand { + Hand::Main => 0, + Hand::Off => 1, + } +} + +/// Whether `Item.use` usually acts on this item (eats, drinks, equips, raises, +/// throws, shoots, fills or places), which ends `Minecraft.startUseItem` at +/// its hand. Tools and materials pass, and the off hand gets its turn. The +/// server decides; this is the client's guess of whether to go on. +fn item_has_use(info: &horizon_client::items::ItemInfo) -> bool { + let id = info.id.trim_start_matches("minecraft:"); + info.consumable.is_some() + || info.equip_slot.is_some() + || info.blocks_attacks.is_some() + || horizon_client::items::bucket_contents(&info.id).is_some() + || id.ends_with("_boat") + || id.ends_with("_raft") + || id.ends_with("_bundle") + || matches!( + id, + "bow" + | "crossbow" + | "trident" + | "spyglass" + | "fishing_rod" + | "snowball" + | "egg" + | "blue_egg" + | "brown_egg" + | "ender_pearl" + | "ender_eye" + | "wind_charge" + | "splash_potion" + | "lingering_potion" + | "experience_bottle" + | "bucket" + | "glass_bottle" + | "map" + | "writable_book" + | "written_book" + | "goat_horn" + | "bundle" + | "brush" + | "carrot_on_a_stick" + | "warped_fungus_on_a_stick" + ) +} + +/// The view distance a client asks for: its render distance, which Horizon +/// lets go past vanilla's 32 (a vanilla server still caps it at its own). +fn client_view_distance(render_distance: i32) -> i8 { + render_distance.clamp(2, i32::from(i8::MAX)) as i8 +} + +struct RemoteInputWorld<'g, 'a>(&'g mut Game<'a>); + +struct RemoteUseWorld<'g, 'a>(&'g mut Game<'a>); + +impl horizon_client::interaction_input::TargetUseWorld for RemoteUseWorld<'_, '_> { + fn use_entity(&mut self) -> bool { + self.0.remote_use_entity() + } + fn use_block(&mut self) -> bool { + self.0.remote_use_block(Hand::Main) + } + fn use_item(&mut self) { + self.0.remote_use_hands(); + } +} + +impl horizon_client::interaction_input::InteractionWorld for RemoteInputWorld<'_, '_> { + fn using_item(&self) -> bool { + self.0.using.is_some() + } + fn release_using(&mut self) { + self.0.send(S::PlayerAction(play::PlayerAction { + action: 6, + pos: [0; 3], + direction: 0, + sequence: 0, + })); + self.0.using = None; + } + fn start_attack(&mut self) -> bool { + self.0.remote_start_attack() + } + fn start_use(&mut self) { + self.0.remote_use(); + } + fn pick(&mut self) { + if let Some(hit) = self.0.pick() { + self.0.send(S::PickItemFromBlock(play::PickItemFromBlock { + pos: hit.pos, + include_data: self.0.has_control_down(), + })); + } + } + fn use_delay(&self) -> u32 { + self.0.use_delay + } + fn continue_attack(&mut self, down: bool) { + self.0.remote_continue_attack(down); + } +} + #[cfg(test)] mod tests { use super::Game; diff --git a/crates/horizon-client/src/game/remote_objects.rs b/crates/horizon-client/src/game/remote_objects.rs new file mode 100644 index 000000000..12a15bc34 --- /dev/null +++ b/crates/horizon-client/src/game/remote_objects.rs @@ -0,0 +1,270 @@ +//! Boats, minecarts, armor stands, item frames and paintings on a server's +//! client (`ClientPacketListener.handleAddEntity`, `handleSetEntityData`, +//! `handleSetEquipment`): the server simulates them, the client places them +//! where it says and interpolates their moves like a mob's. +use super::Game; +use horizon_client::entities::{ArmorStand, ItemFrame, Painting}; +use horizon_core::geometry::Vec3; +use horizon_protocol::entity_data::Value; +use std::sync::Arc; + +impl Game<'_> { + /// `handleAddEntity` for an object type; false when `kind` isn't one. + /// Hanging entities come at their block, with their direction as the + /// spawn data (`Direction.from3DDataValue`). + pub(super) fn spawn_remote_object( + &mut self, + id: i32, + kind: &str, + position: [f64; 3], + yaw: f32, + data: i32, + ) -> bool { + let id = id as u32; + let at = Vec3::new(position[0], position[1], position[2]); + let block = position.map(|v| v.floor() as i32); + self.remove_remote_object(id); + if let Some(cart) = super::minecarts::CartKind::from_id(kind) { + let mut c = super::minecarts::Minecart::new(id, cart, at); + c.yaw = yaw; + c.previous_yaw = yaw; + self.carts.push(c); + } else if let Some((wood, chest)) = super::boats::any_boat_wood(kind) + && self.boat_models.has(wood, chest) + { + let mut b = super::boats::Boat::new(id, Arc::from(wood), at, yaw); + if chest { + b = b.with_chest(); + } + self.boats.push(b); + } else if kind == "minecraft:armor_stand" { + self.armor_stands.push(ArmorStand::new(id, at, yaw)); + } else if kind == "minecraft:item_frame" || kind == "minecraft:glow_item_frame" { + self.item_frames.push(ItemFrame { + id, + pos: block, + facing: data.clamp(0, 5) as u8, + glow: kind == "minecraft:glow_item_frame", + item: None, + rotation: 0, + drop_chance: 1.0, + fixed: false, + invisible: false, + check_interval: 0, + }); + } else if kind == "minecraft:painting" { + // The variant comes with the entity data; until then the + // registry's first one (VariantUtils.getAny). + let (variant, width, height) = self + .remote_painting_variant(0) + .map_or((Arc::from("minecraft:kebab"), 1, 1), |v| { + (v.id, v.width, v.height) + }); + // Direction 3D data value (north 2, south 3, west 4, east 5) to 2D. + let facing = match data { + 2 => 2, + 4 => 1, + 5 => 3, + _ => 0, + }; + self.paintings.push(Painting { + id, + pos: block, + facing, + variant, + width, + height, + check_interval: 0, + }); + } else { + return false; + } + true + } + + /// A `painting_variant` by its network id, from the server's registry. + fn remote_painting_variant(&self, id: i32) -> Option { + let r = self.remote.as_ref()?; + let name = r.registries.name("minecraft:painting_variant", id)?; + self.painting_variants.get(name).cloned() + } + + /// `handleSetEntityData` for an object: what changes how it looks. + pub(super) fn remote_object_data(&mut self, id: i32, values: &[(u8, i32, Value)]) { + let id = id as u32; + for (index, _, value) in values { + if self.item_frames.iter().any(|f| f.id == id) { + // HangingEntity.DATA_DIRECTION, ItemFrame.DATA_ITEM and DATA_ROTATION. + let item = match (index, value) { + (9, Value::Item(stack)) => Some(self.to_stack(stack)), + _ => None, + }; + let Some(f) = self.item_frames.iter_mut().find(|f| f.id == id) else { + continue; + }; + match (index, value) { + (8, Value::Id(d)) => f.facing = (*d).clamp(0, 5) as u8, + (10, Value::Int(r)) => f.rotation = r.rem_euclid(8) as u8, + _ => {} + } + if let Some(item) = item { + f.item = item; + } + } else if self.paintings.iter().any(|p| p.id == id) { + // Painting.DATA_PAINTING_VARIANT_ID: a registry holder (id + 1). + if let (9, Value::Id(holder)) = (index, value) + && let Some(v) = self.remote_painting_variant(holder - 1) + && let Some(p) = self.paintings.iter_mut().find(|p| p.id == id) + { + p.variant = v.id; + p.width = v.width; + p.height = v.height; + } + } else if let Some(a) = self.armor_stands.iter_mut().find(|a| a.id == id) { + match (index, value) { + (0, Value::Byte(flags)) => a.invisible = flags & 0x20 != 0, + (9, Value::Float(health)) => a.health = *health, + // ArmorStand.DATA_CLIENT_FLAGS. + (15, Value::Byte(flags)) => { + a.small = flags & 1 != 0; + a.show_arms = flags & 4 != 0; + a.no_base_plate = flags & 8 != 0; + a.marker = flags & 16 != 0; + } + _ => {} + } + } else if let Some(b) = self.boats.iter_mut().find(|b| b.id == id) { + // VehicleEntity's hurt, direction and damage; the paddles. + match (index, value) { + (8, Value::Int(v)) => b.hurt_time = *v, + (9, Value::Int(v)) => b.hurt_dir = *v, + (10, Value::Float(v)) => b.damage = *v, + (11, Value::Bool(v)) => b.paddling[0] = *v, + (12, Value::Bool(v)) => b.paddling[1] = *v, + _ => {} + } + } else if let Some(c) = self.carts.iter_mut().find(|c| c.id == id) { + match (index, value) { + (8, Value::Int(v)) => c.hurt_time = *v, + (9, Value::Int(v)) => c.hurt_dir = *v, + (10, Value::Float(v)) => c.damage = *v, + _ => {} + } + } + } + } + + /// `handleSetEquipment` for an armor stand (EquipmentSlot ordinals 0 to + /// 5: main hand, off hand, feet, legs, chest, head, its storage order). + pub(super) fn remote_object_equipment( + &mut self, + id: i32, + slots: &[(u8, Option)], + ) { + if !self.armor_stands.iter().any(|a| a.id == id as u32) { + return; + } + let items: Vec<_> = slots + .iter() + .map(|(slot, stack)| (usize::from(*slot), self.to_stack(stack))) + .collect(); + if let Some(a) = self.armor_stands.iter_mut().find(|a| a.id == id as u32) { + for (slot, item) in items { + if let Some(s) = a.equipment.get_mut(slot) { + *s = item; + } + } + } + } + + /// A move's rotation and ground state for an object. + pub(super) fn turn_remote_object(&mut self, id: i32, yaw: Option, on_ground: bool) { + let id = id as u32; + if let Some(b) = self.boats.iter_mut().find(|b| b.id == id) { + if let Some(yaw) = yaw { + b.yaw = yaw; + } + b.on_ground = on_ground; + } else if let Some(c) = self.carts.iter_mut().find(|c| c.id == id) { + if let Some(yaw) = yaw { + c.yaw = yaw; + } + } else if let Some(a) = self.armor_stands.iter_mut().find(|a| a.id == id) { + if let Some(yaw) = yaw { + a.yaw = yaw; + } + a.on_ground = on_ground; + } + } + + /// One tick of interpolation: `step` moves a position a step toward + /// the entity's last sent one, if it has a move in progress. + pub(super) fn tick_remote_objects(&mut self, mut step: impl FnMut(u32, Vec3) -> Vec3) { + for c in &mut self.carts { + c.previous = c.position; + c.previous_yaw = c.yaw; + c.position = step(c.id, c.position); + } + for b in &mut self.boats { + b.previous = b.position; + b.previous_yaw = b.yaw; + b.position = step(b.id, b.position); + } + for a in &mut self.armor_stands { + a.position = step(a.id, a.position); + } + } + + /// The boats, minecarts, armor stands, item frames and paintings in the + /// level (a server's client: the ones it was sent), for `status`. + pub(super) fn objects_status(&self) -> Vec { + use serde_json::json; + let at = |p: Vec3| [p.x, p.y, p.z]; + let mut out = Vec::new(); + for b in &self.boats { + out.push(json!({"id": b.id, "kind": b.entity_type(), "position": at(b.position), "yaw": b.yaw})); + } + for c in &self.carts { + out.push( + json!({"id": c.id, "kind": c.kind.id(), "position": at(c.position), "yaw": c.yaw}), + ); + } + for a in &self.armor_stands { + let items: Vec<_> = a + .equipment + .iter() + .map(|i| i.as_ref().map(|i| &*i.item)) + .collect(); + out.push(json!({"id": a.id, "kind": "minecraft:armor_stand", "position": at(a.position), "yaw": a.yaw, + "small": a.small, "arms": a.show_arms, "no_base_plate": a.no_base_plate, "equipment": items})); + } + for f in &self.item_frames { + out.push( + json!({"id": f.id, "kind": f.kind(), "position": f.pos, "facing": f.facing, + "item": f.item.as_ref().map(|i| &*i.item), "rotation": f.rotation}), + ); + } + for p in &self.paintings { + out.push(json!({"id": p.id, "kind": "minecraft:painting", "position": p.pos, "facing": p.facing, + "variant": &*p.variant})); + } + out + } + + /// Every object a server sent (a new connection, level or dimension). + pub(super) fn clear_remote_objects(&mut self) { + self.carts.clear(); + self.boats.clear(); + self.armor_stands.clear(); + self.item_frames.clear(); + self.paintings.clear(); + } + + pub(super) fn remove_remote_object(&mut self, id: u32) { + self.carts.retain(|c| c.id != id); + self.boats.retain(|b| b.id != id); + self.armor_stands.retain(|a| a.id != id); + self.item_frames.retain(|f| f.id != id); + self.paintings.retain(|p| p.id != id); + } +} diff --git a/crates/horizon-client/src/game/remote_projectiles.rs b/crates/horizon-client/src/game/remote_projectiles.rs new file mode 100644 index 000000000..23cbb1913 --- /dev/null +++ b/crates/horizon-client/src/game/remote_projectiles.rs @@ -0,0 +1,229 @@ +//! Primed TNT, falling blocks, projectiles, tridents, firework rockets and +//! end crystals on a server's client (`ClientPacketListener.handleAddEntity` +//! and `handleSetEntityData`): the server simulates them, the client draws +//! them where it says and interpolates their moves. +use super::{FallingBlock, Game}; +use horizon_client::projectiles::{Projectile, ProjectileKind}; +use horizon_core::{geometry::Vec3, position::BlockPos}; +use horizon_protocol::entity_data::Value; +use horizon_world::registry::BlockStateId; + +/// The projectile a type id names, as Horizon models it. +fn projectile_kind(kind: &str) -> Option { + Some(match kind { + "minecraft:arrow" | "minecraft:spectral_arrow" => ProjectileKind::Arrow { + base_damage: 2.0, + critical: false, + effect: None, + }, + "minecraft:snowball" => ProjectileKind::Snowball, + "minecraft:egg" => ProjectileKind::Egg, + "minecraft:ender_pearl" => ProjectileKind::EnderPearl, + "minecraft:small_fireball" => ProjectileKind::SmallFireball, + "minecraft:fireball" => ProjectileKind::LargeFireball { power: 1 }, + "minecraft:llama_spit" => ProjectileKind::LlamaSpit, + "minecraft:wind_charge" => ProjectileKind::WindCharge, + "minecraft:breeze_wind_charge" => ProjectileKind::BreezeWindCharge, + "minecraft:wither_skull" => ProjectileKind::WitherSkull { dangerous: false }, + _ => return None, + }) +} + +impl Game<'_> { + /// `handleAddEntity` for these types; false when `kind` isn't one. + pub(super) fn spawn_remote_projectile( + &mut self, + id: i32, + kind: &str, + position: [f64; 3], + rotation: (f32, f32), + velocity: [f64; 3], + data: i32, + ) -> bool { + let id = id as u32; + let at = Vec3::new(position[0], position[1], position[2]); + let velocity = Vec3::new(velocity[0], velocity[1], velocity[2]); + let (yaw, pitch) = rotation; + self.remove_remote_projectile(id); + match kind { + "minecraft:tnt" => self.tnts.push(super::tnt::PrimedTnt { + id, + position: at, + previous: at, + velocity, + fuse: 80, + on_ground: false, + }), + // FallingBlockEntity.recreateFromPacket: the block state is the data. + "minecraft:falling_block" => self.falling.push(FallingBlock { + id, + state: BlockStateId(data.max(0) as u32), + start_pos: BlockPos::default(), + position: at, + previous: at, + velocity, + age: 0, + no_gravity: false, + drop_item: false, + fall_distance: 0.0, + damage: Default::default(), + saved: None, + }), + "minecraft:trident" => { + let mut t = super::trident::ThrownTrident::remote(id, at, yaw, pitch); + t.velocity = velocity; + self.tridents.push(t); + } + "minecraft:firework_rocket" => { + self.rockets + .push(super::fireworks::Rocket::remote(id, at, velocity)); + } + "minecraft:end_crystal" => self.crystals.push(super::dragon::Crystal { + id, + position: at, + show_bottom: true, + time: 0, + beam: None, + invulnerable: false, + }), + _ => { + let Some(k) = projectile_kind(kind) else { + return false; + }; + let mut p = Projectile::new(k, at, None); + p.id = id; + p.velocity = velocity; + p.yaw = yaw; + p.pitch = pitch; + self.creatures.projectiles.push(p); + } + } + true + } + + /// `handleSetEntityData` for these types: what changes how they look. + pub(super) fn remote_projectile_data(&mut self, id: i32, values: &[(u8, i32, Value)]) { + let id = id as u32; + for (index, _, value) in values { + if let Some(t) = self.tnts.iter_mut().find(|t| t.id == id) { + // PrimedTnt.DATA_FUSE_ID. + if let (8, Value::Int(fuse)) = (index, value) { + t.fuse = *fuse; + } + } else if let Some(f) = self.falling.iter_mut().find(|f| f.id == id) { + // FallingBlockEntity.DATA_START_POS (the model's seed). + if let (8, Value::Pos(p)) = (index, value) { + f.start_pos = BlockPos::new(p[0], p[1], p[2]); + } + } else if let Some(c) = self.crystals.iter_mut().find(|c| c.id == id) { + // EndCrystal.DATA_BEAM_TARGET and DATA_SHOW_BOTTOM. + match (index, value) { + (8, Value::OptPos(beam)) => c.beam = *beam, + (9, Value::Bool(show)) => c.show_bottom = *show, + _ => {} + } + } else if let Some(p) = self.creatures.projectiles.iter_mut().find(|p| p.id == id) { + match (&mut p.kind, index, value) { + // AbstractArrow.ID_FLAGS (critical) and IN_GROUND. + (ProjectileKind::Arrow { critical, .. }, 8, Value::Byte(flags)) => { + *critical = flags & 1 != 0; + } + (ProjectileKind::Arrow { .. }, 10, Value::Bool(in_ground)) => { + p.stuck = *in_ground; + } + // WitherSkull.DATA_DANGEROUS. + (ProjectileKind::WitherSkull { dangerous }, 8, Value::Bool(d)) => { + *dangerous = *d; + } + _ => {} + } + } + } + } + + /// A move's rotation for a projectile or trident. + pub(super) fn turn_remote_projectile(&mut self, id: i32, rotation: Option<(f32, f32)>) { + let (id, Some((yaw, pitch))) = (id as u32, rotation) else { + return; + }; + if let Some(p) = self.creatures.projectiles.iter_mut().find(|p| p.id == id) { + p.yaw = yaw; + p.pitch = pitch; + } else if let Some(t) = self.tridents.iter_mut().find(|t| t.id == id) { + t.yaw = yaw; + t.pitch = pitch; + } + } + + /// One tick of interpolation, as `tick_remote_objects`. + pub(super) fn tick_remote_projectiles(&mut self, mut step: impl FnMut(u32, Vec3) -> Vec3) { + for t in &mut self.tnts { + t.previous = t.position; + t.position = step(t.id, t.position); + } + for f in &mut self.falling { + f.previous = f.position; + f.age += 1; + f.position = step(f.id, f.position); + } + for p in &mut self.creatures.projectiles { + p.previous = p.position; + p.age += 1; + p.position = step(p.id, p.position); + } + for t in &mut self.tridents { + let id = t.id; + t.remote_step(|at| step(id, at)); + } + for r in &mut self.rockets { + let id = r.id; + r.remote_step(|at| step(id, at)); + } + for c in &mut self.crystals { + c.time += 1; + c.position = step(c.id, c.position); + } + } + + pub(super) fn remove_remote_projectile(&mut self, id: u32) { + self.tnts.retain(|t| t.id != id); + self.falling.retain(|f| f.id != id); + self.creatures.projectiles.retain(|p| p.id != id); + self.tridents.retain(|t| t.id != id); + self.rockets.retain(|r| r.id != id); + self.crystals.retain(|c| c.id != id); + } + + /// Every one a server sent (a new connection, level or dimension). + pub(super) fn clear_remote_projectiles(&mut self) { + self.tnts.clear(); + self.falling.clear(); + self.creatures.projectiles.clear(); + self.tridents.clear(); + self.rockets.clear(); + self.crystals.clear(); + } + + /// Their kind and place, for `status`. + pub(super) fn projectiles_status(&self) -> Vec { + use serde_json::json; + let at = |p: Vec3| [p.x, p.y, p.z]; + let mut out = Vec::new(); + for t in &self.tnts { + out.push(json!({"id": t.id, "kind": "minecraft:tnt", "position": at(t.position), "fuse": t.fuse})); + } + for f in &self.falling { + out.push(json!({"id": f.id, "kind": "minecraft:falling_block", "position": at(f.position), "state": f.state.0})); + } + for c in &self.crystals { + out.push(json!({"id": c.id, "kind": "minecraft:end_crystal", "position": at(c.position), "show_bottom": c.show_bottom})); + } + for t in &self.tridents { + out.push(json!({"id": t.id, "kind": "minecraft:trident", "position": at(t.position)})); + } + for p in &self.creatures.projectiles { + out.push(json!({"id": p.id, "kind": format!("{:?}", p.kind).split([' ', '{']).next().unwrap_or(""), "position": at(p.position)})); + } + out + } +} diff --git a/crates/horizon-client/src/game/render_control.rs b/crates/horizon-client/src/game/render_control.rs index 10add4668..31a7a33a0 100644 --- a/crates/horizon-client/src/game/render_control.rs +++ b/crates/horizon-client/src/game/render_control.rs @@ -11,6 +11,8 @@ pub const HELP: &[&str] = &[ "fill X1 Y1 Z1 X2 Y2 Z2 STATE fill a box with a block state without updates", "particle NAME X Y Z [DX DY DZ] [COUNT] spawn particles (vanilla /particle spread)", "signedit X Y Z [front|back] open the sign editor for the sign at X Y Z", + "section-draws [flat|indirect] how terrain sections are drawn (A/B checks); replies the mode", + "section-cull [on|off] occlusion culling of solid sections (A/B checks); replies the mode", ]; impl Game<'_> { @@ -119,14 +121,14 @@ impl Game<'_> { let key = (int(1)?, int(2)?, int(3)?); let name = self .res - .facts + .facts() .get(self.world.block(key.0, key.1, key.2)) .name .clone(); let entity = horizon_client::containers::BlockEntity::for_block(&name) .filter(|e| matches!(e, horizon_client::containers::BlockEntity::Sign(_))) .ok_or("no sign there")?; - self.world.block_entities.entry(key).or_insert(entity); + self.world.block_entities.get_or_insert_with(key, || entity); self.open_sign_editor(key, words.get(4) != Some(&"back")); Ok(Some(json!({ "ok": true }))) } diff --git a/crates/horizon-client/src/game/render_tick.rs b/crates/horizon-client/src/game/render_tick.rs index 4ee10e6d2..ba07755b9 100644 --- a/crates/horizon-client/src/game/render_tick.rs +++ b/crates/horizon-client/src/game/render_tick.rs @@ -66,15 +66,21 @@ impl Game<'_> { ) -> FogData { let forward = self.forward(); let at = [camera.x, camera.y, camera.z]; - let fog_type = fog_type(&self.world, &self.res.facts, at, forward); - let water = self.res.biomes.get(biome).water_fog; + let fog_type = fog_type(&self.world, self.res.facts(), at, forward); + let water = self.res.biomes().get(biome).water_fog; let (rain, thunder) = if self.dim == Dim::Overworld { self.world.weather.levels(self.clock.fraction() as f32) } else { (0.0, 0.0) }; + let title_fog = horizon_client::fog::FogTables::default(); + let tables = if self.title.is_some() { + &title_fog + } else { + self.res.fog() + }; horizon_client::fog::setup_fog( - &self.res.fog, + tables, &self.fog, &FogInputs { fog_type, @@ -85,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. @@ -117,7 +124,7 @@ impl Game<'_> { horizon_client::gfx::ShaderInputs { rain, thunder, - submerged: match fog_type(&self.world, &self.res.facts, at, self.forward()) { + submerged: match fog_type(&self.world, self.res.facts(), at, self.forward()) { FogType::Atmospheric => 0, FogType::Water => 1, FogType::Lava => 2, @@ -139,7 +146,7 @@ impl Game<'_> { camera.y < 63.0 && fog_type( &self.world, - &self.res.facts, + self.res.facts(), [camera.x, camera.y, camera.z], self.forward(), ) != FogType::Water @@ -170,7 +177,7 @@ impl Game<'_> { t[1].floor() as i32, t[2].floor() as i32, ); - let f = self.res.facts.get(state); + let f = self.res.facts().get(state); if f.render_shape != horizon_client::blocks::RenderShape::Invisible && f.solid_render && f.full_collision @@ -180,7 +187,7 @@ impl Game<'_> { } } let in_water = - fog_type(&self.world, &self.res.facts, eye, self.forward()) == FogType::Water; + fog_type(&self.world, self.res.facts(), eye, self.forward()) == FogType::Water; let water = (in_water && !spectator).then(|| (self.eye_brightness(eye), self.yaw, self.pitch)); // Hud.extractCameraOverlays; the spyglass scope (and its easing) is @@ -211,7 +218,7 @@ impl Game<'_> { let gui_overlays = hud_overlays::camera_overlays(size[0] as i32, size[1] as i32, &overlays, &mut scope); self.screen_fx.build(&ScreenInputs { - sprites: &self.atlas.sprites, + sprites: &self.gui.atlas.sprites, aspect: size[0] as f32 / size[1].max(1) as f32, partial, block_overlay, @@ -243,7 +250,7 @@ impl Game<'_> { self.screen_fx.tick(brightness, inside_portal); // LocalPlayer water vision and AtmosphericFogEnvironment rain fog. let in_water = - fog_type(&self.world, &self.res.facts, e, self.forward()) == FogType::Water; + fog_type(&self.world, self.res.facts(), e, self.forward()) == FogType::Water; self.fog.tick(in_water); let b = [ e[0].floor() as i32, @@ -260,7 +267,7 @@ impl Game<'_> { .chunks .get(&(b[0] >> 4, b[2] >> 4)) .map_or(0, |c| c.biome(b[0], b[1], b[2])); - let rains = self.res.biomes.get(biome).precipitation; + let rains = self.res.biomes().get(biome).precipitation; update_rain_fog( &mut self.fog, rain, @@ -278,8 +285,8 @@ impl Game<'_> { (0.0, 0.0) }; let world = &self.world; - let facts = &self.res.facts; - let biomes = &self.res.biomes; + let facts = self.res.facts(); + let biomes = self.res.biomes(); let raining = rain > 0.2; let raining_at = |p: [i32; 3]| { raining @@ -300,7 +307,7 @@ impl Game<'_> { e[1].floor() as i32, e[2].floor() as i32, ]; - animate_tick(&self.res.animate, &mut self.particles, &ctx, block); + animate_tick(self.res.animate(), &mut self.particles, &ctx, block); if rain > 0.0 { // ClientLevel.tickWeatherEffects: rain splashes (weather radius 10). let mut random = LegacyRandom::new(self.world.game_time.wrapping_mul(312_987_231)); @@ -336,7 +343,7 @@ impl Game<'_> { let smoke = f.fluid == Fluid::Lava || &*f.name == "minecraft:magma_block" || (f.name.ends_with("campfire") - && self.res.animate.is_lit_campfire(state.0)); + && self.res.animate().is_lit_campfire(state.0)); let ty = if smoke { ParticleType::Smoke } else { @@ -354,7 +361,7 @@ impl Game<'_> { } if let Some(r) = &self.renderer { campfire_particles( - &self.res.animate, + self.res.animate(), &mut self.particles, &ctx, r.block_entities(), diff --git a/crates/horizon-client/src/game/respawn_anchor.rs b/crates/horizon-client/src/game/respawn_anchor.rs index 5a31dfd5c..4604c076f 100644 --- a/crates/horizon-client/src/game/respawn_anchor.rs +++ b/crates/horizon-client/src/game/respawn_anchor.rs @@ -63,7 +63,7 @@ pub(super) enum AnchorUse { impl Game<'_> { fn is_anchor(&self, state: BlockStateId) -> bool { - &*self.res.facts.get(state).name == "minecraft:respawn_anchor" + &*self.res.facts().get(state).name == "minecraft:respawn_anchor" } fn anchor_charges(&self, state: BlockStateId) -> i32 { self.pack @@ -177,7 +177,7 @@ impl Game<'_> { } /// `RespawnAnchorBlock.isWaterThatWouldFlow`. fn water_would_flow(&self, p: [i32; 3]) -> bool { - let f = self.res.facts.get(self.world.block(p[0], p[1], p[2])); + let f = self.res.facts().get(self.world.block(p[0], p[1], p[2])); if f.fluid != Fluid::Water { return false; } @@ -188,7 +188,7 @@ impl Game<'_> { return false; } self.res - .facts + .facts() .get(self.world.block(p[0], p[1] - 1, p[2])) .fluid != Fluid::Water @@ -203,7 +203,7 @@ impl Game<'_> { .any(|d| self.water_would_flow([pos[0] + d[0], pos[1], pos[2] + d[2]])); let above = self .res - .facts + .facts() .get(self.world.block(pos[0], pos[1] + 1, pos[2])) .fluid == Fluid::Water; @@ -225,7 +225,7 @@ impl Game<'_> { /// halves and explodes; `StrawBedBlock` just falls apart. pub(super) fn destroy_bed_on_use(&mut self, head: [i32; 3]) -> Result<()> { let state = self.world.block(head[0], head[1], head[2]); - let name = self.res.facts.get(state).name.clone(); + let name = self.res.facts().get(state).name.clone(); let center = [ f64::from(head[0]) + 0.5, f64::from(head[1]) + 0.5, @@ -259,7 +259,7 @@ impl Game<'_> { let foot = [head[0] + back[0], head[1], head[2] + back[2]]; if self .res - .facts + .facts() .get(self.world.block(foot[0], foot[1], foot[2])) .name == name @@ -297,7 +297,7 @@ impl Game<'_> { /// climbable blocks and open trapdoors. fn non_climbable_shape(&self, p: [i32; 3]) -> VoxelShape { let state = self.world.block(p[0], p[1], p[2]); - let name = &self.res.facts.get(state).name; + let name = &self.res.facts().get(state).name; let open_trapdoor = name.ends_with("_trapdoor") && self .pack @@ -324,9 +324,9 @@ impl Game<'_> { ) .unwrap_or_else(|_| VoxelShape::empty()) } - fn block_in_tag(&self, tag: &str, block: &str) -> bool { + pub(super) fn block_in_tag(&self, tag: &str, block: &str) -> bool { self.res - .items + .items() .block_tags .get(tag) .is_some_and(|s| s.contains(block)) @@ -334,7 +334,7 @@ impl Game<'_> { /// `EntityType.isBlockDangerous` for the player. fn dangerous_for_player(&self, p: [i32; 3]) -> bool { let state = self.world.block(p[0], p[1], p[2]); - let name = &*self.res.facts.get(state).name; + let name = &*self.res.facts().get(state).name; let lit_campfire = (name == "minecraft:campfire" || name == "minecraft:soul_campfire") && self .pack @@ -397,7 +397,7 @@ impl Game<'_> { } let name = |q: [i32; 3]| { self.res - .facts + .facts() .get(self.world.block(q[0], q[1], q[2])) .name .clone() diff --git a/crates/horizon-client/src/game/scoreboard.rs b/crates/horizon-client/src/game/scoreboard.rs index ff1f602a4..99dae39a5 100644 --- a/crates/horizon-client/src/game/scoreboard.rs +++ b/crates/horizon-client/src/game/scoreboard.rs @@ -130,7 +130,8 @@ impl Game<'_> { /// `ScoreHolder.getScoreboardName`: the player's name, an entity's UUID. pub(super) fn holder_name(&mut self, t: Target) -> Option { match t { - Target::Player => Some(PLAYER_NAME.to_owned()), + Target::Player => Some(self.acting_player_name().to_owned()), + Target::Guest(conn) => self.command_guest(conn).map(|g| g.name.clone()), Target::Mob(id) | Target::Item(id) => Some(uuid_text(self.uuid_for(id))), _ => None, } @@ -146,7 +147,7 @@ impl Game<'_> { } /// The entity with the given scoreboard name. fn target_of_holder(&self, name: &str) -> Option { - if name == PLAYER_NAME { + if name == self.acting_player_name() { return Some(Target::Player); } let uuid = uuid_from_text(name).filter(|_| name.len() == 36)?; @@ -179,7 +180,7 @@ impl Game<'_> { | Target::Cart(id) | Target::Other(id) => self.persist.raw.get(&id), Target::Player => self.level.player_extra.as_ref(), - Target::Orb(_) | Target::Arrow(_) => None, + Target::Guest(_) | Target::Orb(_) | Target::Arrow(_) => None, }; match raw.and_then(|r| r.get("Tags")) { Some(Tag::List(l)) => l @@ -303,20 +304,33 @@ impl Game<'_> { // ---- criteria ---------------------------------------------------------- + /// `ServerStatsCounter.setValue`'s scoreboard side: the objectives of + /// each changed statistic's criteria follow it for the player `name`. + pub(super) fn stat_criteria_changed( + &mut self, + name: &str, + events: Vec<(String, String, Option)>, + ) { + let board = &mut self.score.board; + if board.objectives.is_empty() { + return; + } + for (ty, key, amount) in events { + let criteria = sb::stat_criteria(&ty, &key); + if board.has_objectives_for(&criteria) { + board.for_all_objectives(&criteria, name, |v| { + amount.map_or(0, |n| v.wrapping_add(n)) + }); + } + } + } /// `ServerPlayer.doTick` score updates and the statistic criteria. pub(super) fn tick_scoreboard(&mut self) { self.load_scoreboard(); let events = std::mem::take(&mut self.stats.events); + self.stat_criteria_changed(PLAYER_NAME, events); let board = &mut self.score.board; if !board.objectives.is_empty() { - for (ty, key, amount) in events { - let criteria = sb::stat_criteria(&ty, &key); - if board.has_objectives_for(&criteria) { - board.for_all_objectives(&criteria, PLAYER_NAME, |v| { - amount.map_or(0, |n| v.wrapping_add(n)) - }); - } - } let display = self.player_display_comp(); if let Some(p) = self.player.as_mut() { let health_and_absorption = p.health.health() + p.health.absorption(); @@ -366,8 +380,9 @@ impl Game<'_> { /// `ServerPlayer.die`: the death count. pub(super) fn score_player_died(&mut self) { let display = self.player_display_comp(); + let me = self.acting_player_name().to_owned(); let board = &mut self.score.board; - board.for_all_objectives_with(sb::CRITERIA_DEATH_COUNT, PLAYER_NAME, Some(&display), |v| { + board.for_all_objectives_with(sb::CRITERIA_DEATH_COUNT, &me, Some(&display), |v| { v.wrapping_add(1) }); // A respawned player has everything to record again. @@ -377,25 +392,21 @@ impl Game<'_> { pub(super) fn score_player_killed(&mut self, victim: Target) { let victim_name = self.holder_name(victim); let display = self.player_display_comp(); + let me = self.acting_player_name().to_owned(); let board = &mut self.score.board; let inc = |v: i32| v.wrapping_add(1); - board.for_all_objectives_with(sb::CRITERIA_TOTAL_KILLS, PLAYER_NAME, Some(&display), inc); + board.for_all_objectives_with(sb::CRITERIA_TOTAL_KILLS, &me, Some(&display), inc); if victim == Target::Player { - board.for_all_objectives_with( - sb::CRITERIA_PLAYER_KILLS, - PLAYER_NAME, - Some(&display), - inc, - ); + board.for_all_objectives_with(sb::CRITERIA_PLAYER_KILLS, &me, Some(&display), inc); } let color_of = |board: &Scoreboard, name: &str| board.players_team(name).and_then(|t| t.color.clone()); if let Some(victim_name) = victim_name { if let Some(color) = color_of(board, &victim_name) { let criteria = format!("teamkill.{color}"); - board.for_all_objectives_with(&criteria, PLAYER_NAME, Some(&display), inc); + board.for_all_objectives_with(&criteria, &me, Some(&display), inc); } - if let Some(color) = color_of(board, PLAYER_NAME) { + if let Some(color) = color_of(board, &me) { let criteria = format!("killedByTeam.{color}"); board.for_all_objectives(&criteria, &victim_name, inc); } @@ -417,7 +428,7 @@ impl Game<'_> { self.score.board.players_team(&uuid_text(*uuid)) } pub(super) fn player_team(&self) -> Option<&sb::Team> { - self.score.board.players_team(PLAYER_NAME) + self.score.board.players_team(self.acting_player_name()) } /// The teams of the player and of the mobs on one (empty without teams). pub(super) fn mob_teams(&self) -> horizon_client::mobs::Teams { @@ -451,16 +462,17 @@ impl Game<'_> { } /// `Player.getDisplayName`: the name with the team's prefix, suffix and colour. pub(super) fn player_display_comp(&self) -> Comp { - let name = literal(PLAYER_NAME); + let name = literal(self.acting_player_name()); match self.player_team() { Some(t) => t.formatted_name_comp(&name), None => name, } } pub(super) fn player_display_text(&self) -> Text { + let name = Text::literal(self.acting_player_name()); match self.player_team() { - Some(t) => t.formatted_name(Text::literal(PLAYER_NAME)), - None => Text::literal(PLAYER_NAME), + Some(t) => t.formatted_name(name), + None => name, } } @@ -573,11 +585,11 @@ impl Game<'_> { batch.height as i32, board, objective, - &self.atlas.font, + &self.gui.atlas.font, self.settings.text_background(0.3), self.settings.text_background(0.4), ); - hud_layout::paint(batch, &self.atlas, &draws); + hud_layout::paint(batch, &self.gui.atlas, &draws); } } @@ -587,9 +599,7 @@ impl Game<'_> { /// objective. pub(super) fn draw_tab_list(&mut self, batch: &mut HudBatch) { use horizon_client::tab_list::{self, TabInputs}; - let chatting = self.remote.is_some() && self.remote_chat_open(); let key = self.screen == super::Screen::Playing - && !chatting && self.keys.contains(&winit::keyboard::KeyCode::Tab); let (players, online_mode, upside_down) = match self.remote_tab_players() { Some(remote) => remote, @@ -639,8 +649,8 @@ impl Game<'_> { ), upside_down: &up, }; - let draws = self.tab_list.layout(&inputs, &self.atlas.font); - horizon_client::hud_layout::paint(batch, &self.atlas, &draws); + let draws = self.tab_list.layout(&inputs, &self.gui.atlas.font); + horizon_client::hud_layout::paint(batch, &self.gui.atlas, &draws); } } @@ -675,7 +685,7 @@ impl Env for GameEnv<'_, '_> { .collect() } fn named(&mut self, name: &str) -> Holder { - if name.eq_ignore_ascii_case(PLAYER_NAME) && self.g.player.is_some() { + if name.eq_ignore_ascii_case(self.g.acting_player_name()) && self.g.player.is_some() { return self .g .holder_for(Target::Player) @@ -692,7 +702,7 @@ impl Env for GameEnv<'_, '_> { None => Some(Target::Player), }?; let name = match entity { - Target::Player => PLAYER_NAME.to_owned(), + Target::Player => self.g.acting_player_name().to_owned(), Target::Mob(id) | Target::Item(id) => uuid_text(self.g.uuid_for(id)), _ => return None, }; @@ -735,6 +745,7 @@ mod tab_list_tests { conn: uuid as u32, uuid, name: name.into(), + properties: Vec::new(), info: ClientInformation::new(8), brand: "test".into(), sender: PlayerSender::new(tx, Arc::default()), diff --git a/crates/horizon-client/src/game/screens.rs b/crates/horizon-client/src/game/screens.rs index 621541691..cecd8d46f 100644 --- a/crates/horizon-client/src/game/screens.rs +++ b/crates/horizon-client/src/game/screens.rs @@ -92,7 +92,7 @@ impl Game<'_> { let (world, quit) = self.pause_labels(); self.tick_friends(); let friends = self.friends_view(); - let mut ui = horizon_client::ui_theme::Ui::new(&self.atlas, batch, self.cursor); + let mut ui = horizon_client::ui_theme::Ui::new(&self.gui.atlas, batch, self.cursor); horizon_client::front::pause::draw( &mut ui, &world, @@ -104,7 +104,7 @@ impl Game<'_> { ui.finish(); return; } - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let (w, h) = (batch.width, batch.height); ui::menu_background(batch, atlas, 0.0, 0.0, w, h, true); let title = Text::translate("menu.game", &[]); @@ -156,7 +156,7 @@ impl Game<'_> { let mut b = HudBatch::new(size); let (world, quit) = self.pause_labels(); horizon_client::front::pause::click( - &self.atlas, + &self.gui.atlas, &mut b, self.cursor.0, self.cursor.1, @@ -197,7 +197,7 @@ impl Game<'_> { } /// DeathScreen: gradient, the title at double size, the cause and score. pub(super) fn draw_death_screen(&mut self, batch: &mut HudBatch) { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let (w, h) = (batch.width, batch.height); // fillGradient(0x60500000 -> 0xA0803030). let rows = h.ceil() as i32; @@ -288,6 +288,13 @@ impl Game<'_> { if hit == Some(0) && self.remote.is_none() { let keep = self.ui.rules.bool("keep_inventory") || self.spectator(); self.experience.respawn(keep); + // The guests are sent the respawned host as a new entity, not the dead body. + if let Some(host) = self.lan.as_mut() { + let id = host.host_entity; + host.hub.respawned(id); + } + // Autosaves keep a login position; death must resolve the respawn point instead. + self.level.position = None; } match hit { Some(0) if self.remote.is_some() => { diff --git a/crates/horizon-client/src/game/sculk.rs b/crates/horizon-client/src/game/sculk.rs index 6701181e7..84078c9cf 100644 --- a/crates/horizon-client/src/game/sculk.rs +++ b/crates/horizon-client/src/game/sculk.rs @@ -6,13 +6,14 @@ use super::{Game, Result}; use horizon_client::{ containers::BlockEntity, + fast_hash::{FxHashMap, FxHashSet}, mobs::{Mob, Species, Who, warden}, particles::{ParticleOptions, ParticleType}, sculk::{self, Context, Info, SculkEntity, SculkKind, WardenSpawnTracker}, }; use horizon_core::{geometry::Direction, position::BlockPos, random::RandomSource}; use horizon_world::registry::BlockStateId; -use std::collections::{HashMap, HashSet}; +use std::collections::HashSet; use winit::keyboard::KeyCode; pub const HELP: &[&str] = &[ @@ -70,8 +71,9 @@ pub(super) struct SculkState { /// The sculk block entities in the order they were added. index: Index, pub known: HashSet<(i32, i32)>, - steps: HashMap, - damaged: HashMap, + // Keyed by mob ids the server gives out: the fast hasher is safe. + steps: FxHashMap, + damaged: FxHashMap, last_health: Option, /// The darkness effect's blend factor, previous and current tick. darkness: (f32, f32), @@ -80,6 +82,15 @@ pub(super) struct SculkState { ticks: u32, pub tracker_loaded: bool, } + +impl SculkState { + /// Exchange the level-held parts (its sculk block entities and the + /// chunks scanned for them) with another level's (game/levels.rs). + pub(super) fn swap_level(&mut self, other: &mut Self) { + std::mem::swap(&mut self.index, &mut other.index); + std::mem::swap(&mut self.known, &mut other.known); + } +} /// An insertion-ordered set of block positions (`Level.blockEntityTickers` /// keeps the order block entities were added in). #[derive(Default)] @@ -106,7 +117,7 @@ impl Index { } fn retain(&mut self, mut keep: impl FnMut(&(i32, i32, i32)) -> bool) { self.order.retain(|k| keep(k)); - self.set = self.order.iter().copied().collect(); + self.set.retain(|k| keep(k)); } } const KIND_SENSOR: u8 = 1; @@ -266,7 +277,7 @@ impl Game<'_> { let n = registry.len(); let tag = |name: &str| { self.res - .items + .items() .block_tags .get(name) .cloned() @@ -285,7 +296,7 @@ impl Game<'_> { resonator: vec![false; n], }; for i in 0..n { - let facts = self.res.facts.get(BlockStateId(i as u32)); + let facts = self.res.facts().get(BlockStateId(i as u32)); let name: &str = &facts.name; t.kinds[i] = match name { "minecraft:sculk_sensor" => KIND_SENSOR, @@ -301,11 +312,11 @@ impl Game<'_> { self.world.watched = t.kinds.iter().map(|k| *k != 0).collect(); self.sculk.tables = t; } - fn kind_of_state(&self, state: u16) -> u8 { + fn kind_of_state(&self, state: u32) -> u8 { self.sculk .tables .kinds - .get(usize::from(state)) + .get(state as usize) .copied() .unwrap_or(0) } @@ -327,12 +338,17 @@ impl Game<'_> { // Chunks that arrived or left (every half second is plenty). if self.sculk.ticks.is_multiple_of(10) { let loaded: HashSet<(i32, i32)> = self.world.chunks.keys().copied().collect(); - for key in loaded.difference(&self.sculk.known.clone()) { - self.sculk_scan_chunk(*key); + let arrived: Vec<(i32, i32)> = loaded.difference(&self.sculk.known).copied().collect(); + for key in arrived { + self.sculk_scan_chunk(key); } - let gone: Vec<(i32, i32)> = self.sculk.known.difference(&loaded).copied().collect(); - for key in gone { - self.sculk.index.retain(|p| (p.0 >> 4, p.2 >> 4) != key); + // The blocks of every chunk that left, in one pass: a player + // leaving unloads thousands of chunks at once. + let gone: HashSet<(i32, i32)> = self.sculk.known.difference(&loaded).copied().collect(); + if !gone.is_empty() { + self.sculk + .index + .retain(|p| !gone.contains(&(p.0 >> 4, p.2 >> 4))); } self.sculk.known = loaded; } @@ -345,10 +361,10 @@ impl Game<'_> { // SculkShriekerBlockEntity.preRemoveSideEffects. if old_kind == KIND_SHRIEKER && let Some(BlockEntity::Sculk(e)) = self.world.block_entities.get(&key) - && self.shrieker_shrieking(BlockStateId(u32::from(old))) + && self.shrieker_shrieking(BlockStateId(old)) { let warning = e.warning_level; - self.shrieker_respond(key, BlockStateId(u32::from(old)), warning); + self.shrieker_respond(key, BlockStateId(old), warning); } if matches!( self.world.block_entities.get(&key), @@ -373,11 +389,16 @@ impl Game<'_> { let mut found = Vec::new(); for (s, section) in chunk.sections.iter().enumerate() { let y0 = MIN_Y + s as i32 * 16; + // Without a sculk state in its palette, no block of the section + // is one: most sections are passed over without reading a block. + if !section.blocks.maybe_has(|v| self.kind_of_state(v) != 0) { + continue; + } if section.blocks.uniform().is_some() { // A uniform section of sculk blocks does not exist. continue; } - let mut a = [0u16; 4096]; + let mut a = [0u32; 4096]; section.unpack(&mut a); for (i, v) in a.iter().enumerate() { if self.kind_of_state(*v) != 0 { @@ -598,7 +619,7 @@ impl Game<'_> { }; !own && !calibrated_out && frequency != 0 - && self.res.behaviours.sculk_sensor_can_activate(state) + && self.res.behaviours().sculk_sensor_can_activate(state) } SculkKind::Shrieker => !self.shrieker_shrieking(state) && ctx.player, SculkKind::Catalyst => false, @@ -689,18 +710,10 @@ impl Game<'_> { } Ok(()) } - /// `ServerLevel.shouldTickBlocksAt`, as `tick_blocks` decides it. + /// `ServerLevel.shouldTickBlocksAt` in a block-ticking chunk, from every + /// player's tickets (`update_chunk_tickets`), once the chunk is lit. fn chunk_ticks(&self, cx: i32, cz: i32) -> bool { - let Some(p) = self.player.as_ref() else { - return false; - }; - let (pcx, pcz) = ( - (p.position.x.floor() as i32) >> 4, - (p.position.z.floor() as i32) >> 4, - ); - let simulation = self.settings.render_distance.min(8); - (cx - pcx).abs() <= simulation - && (cz - pcz).abs() <= simulation + self.tickets.levels().block_ticking(cx, cz) && self.world.chunks.get(&(cx, cz)).is_some_and(|c| c.lit) } fn tick_sculk_block(&mut self, key: (i32, i32, i32)) -> Result<()> { @@ -759,7 +772,7 @@ impl Game<'_> { match kind { SculkKind::Sensor | SculkKind::Calibrated => { let state = self.world.block(key.0, key.1, key.2); - if self.res.behaviours.sculk_sensor_can_activate(state) { + if self.res.behaviours().sculk_sensor_can_activate(state) { let frequency = sculk::frequency(info.event); if let Some(e) = self.sculk_entity(key) { e.last_frequency = frequency; @@ -847,12 +860,12 @@ impl Game<'_> { let state = self.world.block(key.0, key.1, key.2); let is_warden = matches!(who, Who::Mob(id) if self.mobs.iter().any(|m| m.id == id && m.species() == Species::Warden)); - match self.kind_of_state(state.0 as u16) { + match self.kind_of_state(state.0) { KIND_SENSOR | KIND_CALIBRATED => { - if is_warden || !self.res.behaviours.sculk_sensor_can_activate(state) { + if is_warden || !self.res.behaviours().sculk_sensor_can_activate(state) { continue; } - let calibrated = self.kind_of_state(state.0 as u16) == KIND_CALIBRATED; + let calibrated = self.kind_of_state(state.0) == KIND_CALIBRATED; if calibrated { let back = self.calibrated_signal(key); if back != 0 && back != 1 { @@ -992,7 +1005,7 @@ impl Game<'_> { if warning < 4 { return false; } - let Some(kind) = self.res.mob_types.get("minecraft:warden").cloned() else { + let Some(kind) = self.res.mob_types().get("minecraft:warden").cloned() else { return false; }; for _ in 0..20 { @@ -1035,7 +1048,7 @@ impl Game<'_> { /// `SpawnUtil.moveToPossibleSpawnPosition` with `ON_TOP_OF_COLLIDER`. fn spawn_position(&self, x: i32, y: i32, z: i32, range: i32) -> Option<(i32, i32, i32)> { use horizon_client::mobs::data; - let flags = |b: BlockStateId| self.res.mob_data.state(b); + let flags = |b: BlockStateId| self.res.mob_data().state(b); let mut search = y; let mut above = self.world.block(x, search, z); for _ in (-range..=range).rev() { @@ -1154,7 +1167,7 @@ impl Game<'_> { && let Some(eye) = self.eye_position() { let in_water = - horizon_client::fog::fog_type(&self.world, &self.res.facts, eye, self.forward()) + horizon_client::fog::fog_type(&self.world, self.res.facts(), eye, self.forward()) == FogType::Water; return self.fog.water_vision(in_water); } @@ -1163,6 +1176,9 @@ impl Game<'_> { /// `Warden.applyDarknessAround` / `MobEffectUtil.addEffectToPlayersAround`. pub(super) fn apply_darkness(&mut self, at: [f64; 3], radius: f64) { + self.for_each_player(|g| g.darkness_on_player(at, radius)); + } + fn darkness_on_player(&mut self, at: [f64; 3], radius: f64) { if self.creative || self.spectator() { return; } @@ -1316,7 +1332,7 @@ impl Game<'_> { .play("minecraft:block.sculk.charge", Some(c), 1.0, 1.0); let solid = self .res - .facts + .facts() .get(self.world.block(pos.x, pos.y, pos.z)) .full_collision; let (particles, spread) = if solid { (40, 0.45f32) } else { (20, 0.25f32) }; @@ -1511,7 +1527,7 @@ impl Game<'_> { } } } - let alive: HashSet = self.mobs.iter().map(|m| m.id).collect(); + let alive: FxHashSet = self.mobs.iter().map(|m| m.id).collect(); self.sculk.damaged.retain(|id, _| alive.contains(id)); self.sculk.steps.retain(|who, _| match who { Who::Player => true, @@ -1524,7 +1540,8 @@ impl Game<'_> { at[1].floor() as i32, at[2].floor() as i32, ); - self.res.facts.get(self.world.block(x, y, z)).fluid == horizon_client::blocks::Fluid::Water + self.res.facts().get(self.world.block(x, y, z)).fluid + == horizon_client::blocks::Fluid::Water } /// `Entity.move`'s emission: footsteps, swimming, landing and splashes. fn entity_motion_events( @@ -1571,13 +1588,13 @@ impl Game<'_> { at[2].floor() as i32, ); let state = self.world.block(block.0, block.1, block.2); - let air = self.res.facts.is_air(state); + let air = self.res.facts().is_air(state); let climbable = self .res - .items + .items() .block_tags .get("minecraft:climbable") - .is_some_and(|t| t.contains(&*self.res.facts.get(state).name)); + .is_some_and(|t| t.contains(&*self.res.facts().get(state).name)); let travelled = if climbable { ((dx * dx + dy * dy + dz * dz).sqrt() * f64::from(0.6f32)) as f32 } else { @@ -1623,6 +1640,21 @@ impl Game<'_> { mod tests { use super::*; + /// Retaining keeps the insertion order and the membership in step. + #[test] + fn the_index_retains_in_order() { + let mut index = Index::default(); + for key in [(5, 0, 0), (40, 1, 0), (6, 2, 17), (-3, 3, 0), (41, 4, 0)] { + index.insert(key); + } + index.retain(|p| p.0 >> 4 != 2 && p.2 >> 4 != 1); + assert_eq!(index.order, [(5, 0, 0), (-3, 3, 0)]); + assert!(index.contains(&(5, 0, 0)) && !index.contains(&(41, 4, 0))); + assert_eq!(index.set.len(), 2); + index.insert((41, 4, 0)); + assert_eq!(index.iter().last(), Some(&(41, 4, 0))); + } + #[test] fn centres_and_blocks_agree() { assert_eq!(center((1, 2, 3)), [1.5, 2.5, 3.5]); diff --git a/crates/horizon-client/src/game/sculk_fixture.rs b/crates/horizon-client/src/game/sculk_fixture.rs index 2ebdbcb2b..890e571b8 100644 --- a/crates/horizon-client/src/game/sculk_fixture.rs +++ b/crates/horizon-client/src/game/sculk_fixture.rs @@ -303,8 +303,8 @@ impl Game<'_> { } fn sculk_fixture_reset(&mut self, s: &Scenario) -> Result<()> { - self.world = World::new(self.res.facts.air, 0); - let plains = u32::from(self.res.biomes.id("minecraft:plains")); + self.world = World::new(self.res.facts().air, 0); + let plains = u32::from(self.res.biomes().id("minecraft:plains")); let layers = [ self.fixture_state("minecraft:bedrock")?, self.fixture_state("minecraft:dirt")?, @@ -439,7 +439,7 @@ impl Game<'_> { for spec in &s.mobs { let kind = self .res - .mob_types + .mob_types() .get(&spec.kind) .cloned() .ok_or_else(|| format!("unknown mob {}", spec.kind))?; @@ -696,6 +696,8 @@ impl Game<'_> { seed_fresh(m, 0x5eed + (named + i + 1) as i64); } } + // The player's tickets, as `tick_world` takes them. + self.update_chunk_tickets(); // Entities: the player's warden spawn tracker, then the mobs. self.tick_sculk_entities(); // (ServerChunkCache.tick, natural spawning and its chunk shuffle, is not run.) diff --git a/crates/horizon-client/src/game/server_travel.rs b/crates/horizon-client/src/game/server_travel.rs new file mode 100644 index 000000000..56fdbaca5 --- /dev/null +++ b/crates/horizon-client/src/game/server_travel.rs @@ -0,0 +1,322 @@ +//! A server's players between dimensions. +//! +//! A server holds a level per dimension (game/levels.rs). A player who steps +//! into a Nether or End portal moves to the destination's level, which opens +//! if nobody was there: the swap is the in-frame path's portal plans and +//! arrival search, and the player hears it as `ServerPlayer.teleport` into +//! another level: a Respawn packet keeping all data, then a teleport to the +//! exit once the destination has loaded around them. + +use super::nether_portal::{PLAYER_DELAY, Plan}; +use super::{Dim, Game, lan::LanHost}; +use horizon_client::lan::hub::TickCtx; +use horizon_core::{geometry::Vec3, position::BlockPos}; + +/// A trip under way: the player is in the destination's level, whose +/// chunks are loading around `target`. +pub(super) struct Trip { + conn: u32, + dim: Dim, + target: Vec3, + /// Through an End portal. + end: bool, + /// The Nether portal plan, whose exit is found or built on arrival. + plan: Option, + /// Sent by a command: the player is already where it goes. + placed: bool, +} + +/// `TeleportCommand` into another level for a server's player: where, and +/// the rotation it sets (none keeps the player's). +pub(super) struct LevelTeleport { + pub conn: u32, + pub dim: Dim, + pub at: [f64; 3], + pub rotation: Option<[f32; 2]>, +} + +/// A player's box: Player's standing dimensions. +const WIDTH: f64 = 0.6; +const HEIGHT: f64 = 1.8; + +impl Trip { + pub(super) fn is(&self, conn: u32) -> bool { + self.conn == conn + } +} + +impl Game<'_> { + /// Whether `conn` is between two levels: the server leaves its body + /// alone until it arrives. + pub(super) fn on_server_trip(&self, conn: u32) -> bool { + self.server_trips.iter().any(|t| t.conn == conn) + } + + /// Per server tick in each level, after the guests' own: the arrivals of + /// the trips into this level, then `Entity.handlePortal` for each player + /// in it (Nether portals count up their transition time, End portals + /// take them at once). + pub(super) fn tick_server_travel(&mut self, host: &mut LanHost) { + if self.options.dedicated.is_none() { + return; + } + self.arrive_server_trips(host); + let conns: Vec = host + .hub + .guests + .iter() + .filter(|g| host.hub.here(g) && g.loaded && !g.dead && !g.spectator()) + .map(|g| g.conn) + .collect(); + for conn in conns { + if self.on_server_trip(conn) { + continue; + } + // `Entity.portalProcess` and `portalCooldown` are the player's own. + let mut portal = host.play.entry(conn).or_default().portal; + std::mem::swap(&mut self.portals.player, &mut portal); + self.handle_server_portal(host, conn); + std::mem::swap(&mut self.portals.player, &mut portal); + host.play.entry(conn).or_default().portal = portal; + } + } + + /// `Entity.handlePortal` for one player, whose portal state is in place. + fn handle_server_portal(&mut self, host: &mut LanHost, conn: u32) { + let Some(g) = host.hub.guest(conn) else { + return; + }; + let (at, invulnerable, sleeping) = ( + Vec3::new(g.position[0], g.position[1], g.position[2]), + g.creative(), + g.sleeping, + ); + // Entity.checkInsideBlocks: the Nether portal blocks the box is in. + if let Some(pos) = self.nether_portal_inside(at) { + self.portals.player.set_inside(pos, PLAYER_DELAY); + } + let (end_portal, _) = self.inside_end_portals(at, WIDTH, HEIGHT); + if end_portal { + if self.portals.player.cooldown == 0 { + let target = if self.dim == Dim::End { + Dim::Overworld + } else { + Dim::End + }; + self.start_server_trip(host, conn, target, None); + } + // Entity.setAsInsidePortal while cooling down restarts the cooldown. + self.portals.player.cooldown = 10; + return; + } + let rule = if invulnerable { + "players_nether_portal_creative_delay" + } else { + "players_nether_portal_default_delay" + }; + let transition = self.ui.rules.int(rule).max(0); + let Some(entry) = self + .portals + .player + .handle(!sleeping, transition, PLAYER_DELAY) + else { + return; + }; + let plan = self.portal_plan(entry, at, WIDTH as f32, HEIGHT as f32, true); + if !self.may_enter(plan.dim) { + return; + } + self.start_server_trip(host, conn, plan.dim, Some(plan)); + } + + /// A Nether portal block a player's box is in. + fn nether_portal_inside(&self, at: Vec3) -> Option { + let w = WIDTH / 2.0 - 1.0e-5; + let lo = [at.x - w, at.y + 1.0e-5, at.z - w]; + let hi = [at.x + w, at.y + HEIGHT - 1.0e-5, at.z + w]; + for x in lo[0].floor() as i32..=hi[0].floor() as i32 { + for y in lo[1].floor() as i32..=hi[1].floor() as i32 { + for z in lo[2].floor() as i32..=hi[2].floor() as i32 { + if &*self.block_name([x, y, z]) == "minecraft:nether_portal" { + return Some(BlockPos::new(x, y, z)); + } + } + } + } + None + } + + /// The player moves to `target`'s level (opened if nobody was there) + /// and is told it is there, at the approximate exit, while it loads. The + /// level whose tick is running is put back in place afterwards. + fn start_server_trip( + &mut self, + host: &mut LanHost, + conn: u32, + target: Dim, + plan: Option, + ) { + let Some(i) = host.hub.guests.iter().position(|g| g.conn == conn) else { + return; + }; + let g = &host.hub.guests[i]; + let from = Vec3::new(g.position[0], g.position[1], g.position[2]); + let yaw = g.yaw; + let source = self.dim; + let end = plan.is_none(); + // Coordinates scale by 8 between the Overworld and the Nether; an + // End portal has fixed destinations. + let scale = match (source, target) { + (Dim::Overworld, Dim::Nether) | (Dim::End, Dim::Nether) => 1.0 / 8.0, + (Dim::Nether, Dim::Overworld) => 8.0, + _ => 1.0, + }; + self.end_portal_travel = end; + let scaled = self.end_destination(target).unwrap_or(Vec3::new( + from.x * scale, + from.y, + from.z * scale, + )); + self.end_portal_travel = false; + if let Err(e) = self.enter_level(Some(host), target) { + log::error!("could not open {}: {e}", target.id()); + return; + } + let destination = match plan { + Some(plan) => self.arrival_centre(&plan), + None => scaled, + }; + let at = [destination.x, destination.y, destination.z]; + let ctx = self.server_ctx(); + // TeleportTransition.PLAY_PORTAL_SOUND. + host.hub.change_level(i, at, yaw, None, true, &ctx); + self.as_guest(host, conn, |g| g.trigger_dimension(source, target)); + self.server_trips.push(Trip { + conn, + dim: target, + target: destination, + end, + plan, + placed: false, + }); + if let Err(e) = self.enter_level(Some(host), source) { + log::error!("could not reopen {}: {e}", source.id()); + } + } + + /// The trips into the level in place whose destination has loaded + /// around their target: the exit (the Nether portal found or built, the + /// End platform, the Overworld's spawn column) and the teleport there. + fn arrive_server_trips(&mut self, host: &mut LanHost) { + let mut k = 0; + while k < self.server_trips.len() { + let trip = &self.server_trips[k]; + let Some(i) = host.hub.guests.iter().position(|g| g.conn == trip.conn) else { + // They left on the way. + self.server_trips.remove(k); + continue; + }; + let (cx, cz) = ( + (trip.target.x.floor() as i32) >> 4, + (trip.target.z.floor() as i32) >> 4, + ); + if trip.dim != self.dim + || !(-2..=2).all(|dz| (-2..=2).all(|dx| self.streaming.is_lit(cx + dx, cz + dz))) + { + k += 1; + continue; + } + let trip = self.server_trips.remove(k); + if trip.placed { + continue; + } + self.resolve_arrivals(); + let yaw = host.hub.guests[i].yaw; + let (position, yaw) = if trip.end { + match self.end_arrival(trip.target) { + Ok((at, turned)) => (at, turned.unwrap_or(yaw)), + Err(e) => { + log::error!("End arrival: {e}"); + (trip.target, yaw) + } + } + } else { + let exit = trip + .plan + .as_ref() + .and_then(|plan| self.nether_exit_portal(plan)); + match (trip.plan, exit) { + (Some(plan), Some(exit)) => { + let (at, rotation) = self.arrival(&plan, exit); + (at, yaw + rotation) + } + _ => (trip.target, yaw), + } + }; + // Player.getDimensionChangingDelay. + host.play.entry(trip.conn).or_default().portal.cooldown = 10; + let g = &mut host.hub.guests[i]; + g.yaw = yaw; + // A new body at the exit: no fall distance or motion carried over. + g.motion = None; + host.hub + .teleport(i, Some([position.x, position.y, position.z])); + } + } + + /// The players the command just run sends to another level + /// (`ServerPlayer.teleport` from TeleportCommand): a Respawn packet + /// keeping all data and the teleport, with no portal sound. The server + /// leaves the body alone until the level has loaded around it. + pub(super) fn run_level_teleports(&mut self, host: &mut LanHost) { + for t in std::mem::take(&mut self.level_teleports) { + let Some(i) = host.hub.guests.iter().position(|g| g.conn == t.conn) else { + continue; + }; + let source = self.dim; + if let Err(e) = self.enter_level(Some(host), t.dim) { + log::error!("could not open {}: {e}", t.dim.id()); + continue; + } + let g = &mut host.hub.guests[i]; + let [yaw, pitch] = t.rotation.unwrap_or([g.yaw, g.pitch]); + // A new body there: no fall distance or motion carried over. + g.motion = None; + let ctx = self.server_ctx(); + host.hub + .change_level(i, t.at, yaw, Some(pitch), false, &ctx); + self.as_guest(host, t.conn, |g| g.trigger_dimension(source, t.dim)); + self.server_trips.retain(|trip| !trip.is(t.conn)); + self.server_trips.push(Trip { + conn: t.conn, + dim: t.dim, + target: Vec3::new(t.at[0], t.at[1], t.at[2]), + end: false, + plan: None, + placed: true, + }); + if let Err(e) = self.enter_level(Some(host), source) { + log::error!("could not reopen {}: {e}", source.id()); + } + } + } + + /// The hub's view of the server for packets sent outside its own tick. + pub(super) fn server_ctx(&mut self) -> TickCtx<'_> { + let spawn = self.spawn.map_or([0.5, 80.0, 0.5], |s| [s.x, s.y, s.z]); + let clock_rate = self.network_clock_rate(); + TickCtx { + world: &mut self.world, + host: None, + entities: &[], + next_entity: &mut self.next_entity, + spawn, + difficulty: i32::from(self.ui.difficulty), + difficulty_locked: self.level.difficulty_locked, + clock_rate, + tick_rate: self.clock.rate(), + tick_frozen: self.clock.frozen(), + collision: Some(self.collision), + } + } +} diff --git a/crates/horizon-client/src/game/shield.rs b/crates/horizon-client/src/game/shield.rs index 849c62958..c4f7f98d1 100644 --- a/crates/horizon-client/src/game/shield.rs +++ b/crates/horizon-client/src/game/shield.rs @@ -34,7 +34,7 @@ impl Game<'_> { &self, stack: &ItemStack, ) -> Option<&horizon_client::items::BlocksAttacks> { - self.res.items.get(&stack.item)?.blocks_attacks.as_ref() + self.res.items().get(&stack.item)?.blocks_attacks.as_ref() } /// `ShieldItem.use` (ItemUtils.startUsingInstantly) for the hand at `slot`; /// false when that hand holds no blocking item or it is on cooldown. @@ -64,7 +64,7 @@ impl Game<'_> { if (ticks as i32) < (component.delay_seconds * 20.0).round() as i32 { return None; } - let tags = &self.res.stations.enchantments.damage_tags; + let tags = &self.res.stations().enchantments.damage_tags; let matches = |spec: &str| match spec.strip_prefix('#') { Some(tag) => tags.get(tag).is_some_and(|t| t.contains(damage_type)), None => spec == damage_type, @@ -150,7 +150,11 @@ impl Game<'_> { }; if item_damage > 0 && !self.creative { let loss = self.durability_loss(&stack, item_damage as u32); - let max = self.res.items.get(&stack.item).map_or(0, |i| i.max_damage); + let max = self + .res + .items() + .get(&stack.item) + .map_or(0, |i| i.max_damage); let cell = self.stack_in_mut(slot); if let Some(s) = cell.as_mut() && max > 0 @@ -182,7 +186,7 @@ impl Game<'_> { .extra .main_hand .as_deref() - .and_then(|item| self.res.items.get(item)) + .and_then(|item| self.res.items().get(item)) .map_or(0.0, |i| i.disable_blocking_seconds); // Ravager.blockedByItem: stunned half the time (the defender pushed // apart), else the defender flung away; no roar in progress. diff --git a/crates/horizon-client/src/game/sign_edit.rs b/crates/horizon-client/src/game/sign_edit.rs index c0a780063..c679bad69 100644 --- a/crates/horizon-client/src/game/sign_edit.rs +++ b/crates/horizon-client/src/game/sign_edit.rs @@ -88,7 +88,7 @@ impl Game<'_> { return; }; let line = edit.line; - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let width = |s: &str| HudBatch::text_width(atlas, s); let Some(key) = self.sign_edit.as_ref().map(|e| e.pos) else { return; @@ -130,7 +130,7 @@ impl Game<'_> { } else { &mut sign.back }; - let atlas = &self.atlas; + let atlas = &self.gui.atlas; self.text_selection.apply( sign_line_id(front, line), &mut text.lines[line], @@ -163,7 +163,7 @@ impl Game<'_> { let Some(BlockEntity::Sign(sign)) = self.world.block_entities.get(&edit.pos) else { return; }; - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let (w, h) = (batch.width, batch.height); batch.fill(atlas, 0.0, 0.0, w, h, rgba(16, 16, 16, 160)); let title = if sign.hanging { @@ -175,7 +175,7 @@ impl Game<'_> { batch.text(atlas, title, (w - tw) / 2.0, 40.0, u32::MAX, true); let name = self .res - .facts + .facts() .get(self.world.block(edit.pos.0, edit.pos.1, edit.pos.2)) .name .clone(); diff --git a/crates/horizon-client/src/game/sniffers.rs b/crates/horizon-client/src/game/sniffers.rs index 61325ede6..a30c0ba5c 100644 --- a/crates/horizon-client/src/game/sniffers.rs +++ b/crates/horizon-client/src/game/sniffers.rs @@ -93,7 +93,7 @@ impl Game<'_> { self.particles.push(particle); } if self.mobs[i].age.is_multiple_of(10) { - let hit = self.res.facts.get(below).sounds.hit.clone(); + let hit = self.res.facts().get(below).sounds.hit.clone(); self.audio.play(&hit, Some(at), 0.5, 0.5); } } diff --git a/crates/horizon-client/src/game/spawner.rs b/crates/horizon-client/src/game/spawner.rs index ba7bd66a1..538cb39e7 100644 --- a/crates/horizon-client/src/game/spawner.rs +++ b/crates/horizon-client/src/game/spawner.rs @@ -45,7 +45,7 @@ impl Game<'_> { .as_ref() .is_some_and(|t| t.contains(id)) } - fn load_entity_types(&mut self) { + pub(super) fn load_entity_types(&mut self) { if self.spawners.entity_types.is_some() { return; } @@ -63,32 +63,27 @@ impl Game<'_> { .unwrap_or_default(); self.spawners.entity_types = Some(types); } - /// Every spawner's server tick, then its client tick. - pub(super) fn tick_spawners(&mut self) -> Result<()> { - self.load_entity_types(); - let keys: Vec = self - .world - .block_entities - .iter() - .filter(|(_, e)| matches!(e, BlockEntity::Spawner(_))) - .map(|(k, _)| *k) - .collect(); - for key in &keys { - let state = self.world.block(key.0, key.1, key.2); - if &*self.res.facts.get(state).name != "minecraft:spawner" { - continue; - } - let Some(BlockEntity::Spawner(mut spawner)) = self.world.block_entities.remove(key) - else { - continue; - }; - let result = self.server_tick_spawner(*key, &mut spawner); - self.world - .block_entities - .insert(*key, BlockEntity::Spawner(spawner)); - result?; + /// The server tick of the spawner at `key`, if its block is still a + /// spawner. Its client tick comes after every block entity's + /// (`client_tick_spawners`). + pub(super) fn tick_spawner(&mut self, key: Pos) -> Result<()> { + let state = self.world.block(key.0, key.1, key.2); + if &*self.res.facts().get(state).name != "minecraft:spawner" { + return Ok(()); } - for key in keys { + let Some(BlockEntity::Spawner(mut spawner)) = self.world.block_entities.take(&key) else { + return Ok(()); + }; + let result = self.server_tick_spawner(key, &mut spawner); + self.world + .block_entities + .put(key, BlockEntity::Spawner(spawner)); + result + } + /// The client ticks of these spawners, then the shown mobs of spawners + /// that are gone go. + pub(super) fn client_tick_spawners(&mut self, keys: &[Pos]) { + for &key in keys { self.client_tick_spawner(key); } let entities = &self.world.block_entities; @@ -98,7 +93,6 @@ impl Game<'_> { Some(BlockEntity::Spawner(_) | BlockEntity::TrialSpawner(_)) ) }); - Ok(()) } fn server_tick_spawner(&mut self, key: Pos, spawner: &mut Spawner) -> Result<()> { let pos = [key.0, key.1, key.2]; @@ -151,7 +145,7 @@ impl Game<'_> { r: &SpawnerSpawn, planned: &mut Vec, ) -> Option { - let kind = self.res.mob_types.get(r.id)?.clone(); + let kind = self.res.mob_types().get(r.id)?.clone(); let [x, y, z] = r.position; let at = Vec3::new(x, y, z); let bounds = if r.finalize { @@ -201,7 +195,7 @@ impl Game<'_> { tag.insert("PersistenceRequired".into(), Tag::Byte(1)); } let tag = Tag::Compound(tag); - let mob = horizon_client::entity_nbt::mob_from_nbt(&tag, &self.res.mob_types, 0)?; + let mob = horizon_client::entity_nbt::mob_from_nbt(&tag, self.res.mob_types(), 0)?; let bounds = mob.bounds(); planned.push(Planned::Loaded(tag)); bounds @@ -337,7 +331,7 @@ impl Game<'_> { .get(&key) .is_none_or(|(k, _)| *k != kind) { - let Some(t) = self.res.mob_types.get(&kind).cloned() else { + let Some(t) = self.res.mob_types().get(&kind).cloned() else { return false; }; let mob = Mob::new(u32::MAX, t, Vec3::ZERO, 0.0); @@ -357,7 +351,7 @@ impl Game<'_> { } _ => return None, }; - self.res.mob_types.contains_key(&id).then_some(id) + self.res.mob_types().contains_key(&id).then_some(id) } /// `SpawnEggItem.useOn` on a spawner: the egg sets its mob. None when the /// clicked block has no spawner. @@ -399,7 +393,7 @@ impl Game<'_> { alpha: f64, out: &mut Vec, ) { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let lookup = |name: &str| atlas.sprites.get(name).map(|r| [r.u0, r.v0, r.u1, r.v1]); for (key, (_, mob)) in &self.spawners.displays { let at = glam::DVec3::new(f64::from(key.0), f64::from(key.1), f64::from(key.2)); @@ -427,13 +421,22 @@ impl Game<'_> { * glam::Mat4::from_scale(glam::Vec3::splat(scale)); let light = self.world.light(key.0, key.1, key.2); let packed = (u32::from(light >> 4) * 16) | ((u32::from(light & 15) * 16) << 8); + if mob.species() == horizon_client::mobs::Species::SulfurCube { + self.sulfur_cube_body_geometry( + mob, + alpha as f32, + horizon_client::mobs::render::base_transform_at(mob, alpha as f32, origin), + packed, + out, + ); + } horizon_client::mobs::render::render_posed( mob, alpha as f32, origin, &lookup, packed, - &self.res.mob_data, + self.res.mob_data(), out, ); } diff --git a/crates/horizon-client/src/game/startup_menu.rs b/crates/horizon-client/src/game/startup_menu.rs new file mode 100644 index 000000000..9531b0f38 --- /dev/null +++ b/crates/horizon-client/src/game/startup_menu.rs @@ -0,0 +1,782 @@ +//! The interactive title before world resources exist. Uses only the menu +//! atlas/panorama; transfers the actual screen and queued entry to Game. +use super::*; +use horizon_client::{ + clipboard::{self, AppendSelection, SystemClipboard}, + options_ui::{Event, OptionsUi, Page}, +}; +use winit::{event::KeyEvent, keyboard::ModifiersState}; + +#[derive(Default)] +pub(super) struct StartupMenu { + title: Option, + options: OptionsUi, + options_open: bool, + root: PathBuf, + cursor: (f32, f32), + pointer: (f64, f64), + modifiers: ModifiersState, + physical_keys: HashSet<KeyCode>, + dragging: bool, + clipboard: SystemClipboard, + selection: AppendSelection, + pub pending: Option<PendingWorldEntry>, + pub reload_packs: bool, + pub perf: horizon_client::telemetry::Session, +} + +impl StartupMenu { + pub fn new(options: &Options, pack_error: Option<&str>) -> Result<Self> { + let pending = if let Some(address) = &options.server { + Some(PendingWorldEntry::Server { + address: address.clone(), + name: options.player_name(), + }) + } else { + options + .world + .as_ref() + .map(|folder| PendingWorldEntry::Local { + folder: folder.clone(), + name: None, + seed: Some(options.seed), + creative: options.creative, + create: None, + }) + }; + let mut menu = Self { + pending, + ..Default::default() + }; + menu.root = options.resources.clone(); + if let Some(error) = pack_error { + menu.options.open_at(false, Page::Packs); + menu.options.resource_error = Some(error.to_owned()); + menu.options_open = true; + } + if menu.pending.is_some() { + menu.perf.world_opening(); + } + Ok(menu) + } + + pub(super) fn set_root(&mut self, root: PathBuf, settings: &Settings) { + horizon_client::ui::load_metadata(&root); + horizon_client::lang::set(horizon_client::lang::Language::load( + &[&root], + &settings.language, + )); + self.root = root; + if self.title.is_none() { + let mut title = Title::new( + std::env::current_dir().unwrap_or_default().join("saves"), + &self.root, + ); + title.last_server = settings.file.string("lastServer"); + if horizon_client::ui_theme::horizon() { + title + .opening + .set(Some(horizon_client::front::Opening::new())); + } + self.title = Some(title); + } + } + + pub(super) fn handoff(&mut self, game: &mut Game<'_>) { + game.title = self.title.take(); + game.pending_entry = self.pending.take(); + game.cursor = self.cursor; + game.modifiers = self.modifiers; + game.physical_keys = std::mem::take(&mut self.physical_keys); + game.clipboard = std::mem::take(&mut self.clipboard); + game.text_selection = std::mem::take(&mut self.selection); + game.perf = std::mem::take(&mut self.perf); + // Preserve an existing resource-error screen opened by start_game. + if self.options_open { + let resource_error = game.ui.options.resource_error.take(); + game.ui.options = std::mem::take(&mut self.options); + game.ui.options.resource_error = + game.ui.options.resource_error.take().or(resource_error); + game.screen = Screen::Options; + game.options_from_title = true; + game.dragging = self.dragging; + } + } + + fn queue_entry(&mut self, action: Action, player_name: String) { + let entry = match action { + Action::Play { folder } => PendingWorldEntry::Local { + folder, + name: None, + seed: None, + creative: false, + create: None, + }, + Action::Create { + folder, + name, + seed, + creative, + options, + } => PendingWorldEntry::Local { + folder, + name: Some(name), + seed, + creative, + create: Some(Box::new(options)), + }, + Action::Connect { address } => PendingWorldEntry::Server { + address, + name: player_name, + }, + _ => return, + }; + self.pending = Some(entry); + self.perf.world_opening(); + } + + fn shortcut(&mut self, event: &KeyEvent) -> Option<clipboard::EditShortcut> { + let edit = if cfg!(target_os = "macos") { + self.modifiers.super_key() + } else { + self.modifiers.control_key() + }; + clipboard::edit_shortcut( + clipboard::shortcut_letter(&event.logical_key, event.physical_key), + edit, + self.modifiers.shift_key(), + self.modifiers.alt_key(), + ) + } + + fn title_key(&mut self, event: &KeyEvent) { + let shortcut = self.shortcut(event); + let Some(title) = &mut self.title else { return }; + if title.skip_opening() { + return; + } + if let Some(shortcut) = shortcut { + if title.has_caret_field() { + title.field_shortcut(shortcut, &mut self.clipboard); + } else if let Some((id, value, max)) = title.focused_field() + && self + .selection + .apply(id, value, shortcut, &mut self.clipboard, |v, t| { + clipboard::append_limited(v, t, max) + }) + { + title.search_changed(); + } + return; + } + match event.physical_key { + PhysicalKey::Code(KeyCode::Escape) => title.escape(), + PhysicalKey::Code(KeyCode::Tab) => title.tab(), + PhysicalKey::Code(KeyCode::Enter | KeyCode::NumpadEnter) => title.confirm_edit(), + PhysicalKey::Code( + key @ (KeyCode::Backspace + | KeyCode::Delete + | KeyCode::ArrowLeft + | KeyCode::ArrowRight + | KeyCode::Home + | KeyCode::End), + ) if title.has_caret_field() => { + title.field_key( + key, + self.modifiers.control_key(), + self.modifiers.shift_key(), + ); + } + _ => { + let cleared = !title.has_caret_field() + && title + .focused_field() + .is_some_and(|(id, value, _)| self.selection.take(id, value)); + if cleared { + title.search_changed(); + } + if event.physical_key == PhysicalKey::Code(KeyCode::Backspace) { + if !cleared { + title.backspace(); + } + } else if let Some(text) = &event.text { + title.type_text(text); + } + } + } + } +} + +/// Each generation has its own channel and progress. Pack changes wait for the +/// old tail before starting another generation, including cache/global writes. +pub(super) fn spawn_loader( + options: &Options, + settings: &Settings, + vanilla_root: &std::path::Path, + started: Instant, + progress: &Progress, +) -> Result<(Receiver<LoaderMsg>, JoinHandle<()>)> { + let (sender, receiver) = std::sync::mpsc::channel(); + let settings = settings.clone(); + let vanilla_root = vanilla_root.to_path_buf(); + let dev_mods = options.dev_mods.clone(); + let progress = progress.clone(); + let handle = std::thread::Builder::new() + .name("resource-loader".into()) + .spawn(move || { + let mut pack_error = None; + let root = packs::startup_root(&vanilla_root, &settings.resource_packs).unwrap_or_else( + |error| { + pack_error = Some(error.to_string()); + vanilla_root.clone() + }, + ); + let root = mods::content_root(&root, &settings.mods.enabled, &dev_mods); + let result = load_resources( + root, + &vanilla_root, + &settings, + started, + true, + &progress, + &mut |gui, root| { + let _ = sender.send(LoaderMsg::Ui(gui, root.to_path_buf())); + }, + ) + .map(|mut loaded| { + loaded.pack_error = loaded.pack_error.or(pack_error); + loaded + }) + .map_err(|error| error.to_string()); + let _ = sender.send(LoaderMsg::Done(result)); + })?; + Ok((receiver, handle)) +} + +impl Loading { + pub(super) fn poll_menu_resources(&mut self) -> Result<()> { + if self.loaded.is_none() { + loop { + match self.receiver.try_recv() { + Ok(LoaderMsg::Ui(gui, root)) => self.set_ui(gui, root), + Ok(LoaderMsg::Done(result)) => { + self.loaded = Some(result); + break; + } + Err(TryRecvError::Empty) => break, + Err(TryRecvError::Disconnected) => { + if let Some(handle) = self.handle.take() { + let _ = handle.join(); + } + return Err("resource loader stopped before finishing".into()); + } + } + } + } + self.restart_menu_resources() + } + pub(super) fn apply_menu_settings(&mut self) { + if let Some(renderer) = &mut self.renderer { + self.settings + .clamp_gui_scale([renderer.config.width as i32, renderer.config.height as i32]); + renderer.gamma = self.settings.gamma; + renderer.set_present_mode(self.settings.vsync, self.settings.horizon.present_mode); + } + horizon_client::hud::GUI_SCALE.store(self.settings.gui_scale, Ordering::Relaxed); + if let Some(gui) = &mut self.gui { + gui.atlas.font.uniform = self.settings.force_unicode; + gui.atlas.font.japanese = self.settings.japanese_glyphs; + } + if let Some(window) = &self.window + && let Some(want) = self.fullscreen.request(self.settings.fullscreen) + { + window.set_fullscreen(want.then_some(winit::window::Fullscreen::Borderless(None))); + } + self.update_menu_cursor(); + } + + fn update_menu_cursor(&mut self) { + let batch = HudBatch::new(self.menu_size()); + self.menu.cursor = ( + self.menu.pointer.0 as f32 / batch.scale, + self.menu.pointer.1 as f32 / batch.scale, + ); + } + + fn menu_size(&self) -> [u32; 2] { + self.renderer + .as_ref() + .map_or([1280, 720], |r| [r.config.width, r.config.height]) + } + + fn save_menu_address(&mut self) { + if self + .menu + .title + .as_ref() + .is_some_and(|title| title.last_server != self.settings.file.string("lastServer")) + { + self.save_menu_settings(); + } + } + + fn save_menu_settings(&mut self) { + if let Some(title) = &self.menu.title { + self.settings.file.set( + "lastServer", + horizon_client::options_file::V::Str(title.last_server.clone()), + ); + } + self.settings.save(&Game::options_path()); + } + + fn open_menu_options(&mut self, page: Page) { + self.menu.options.open_at(false, page); + self.menu.options.languages = horizon_client::lang::available(&[&self.menu.root]); + self.menu.options.pack_infos = packs::pack_infos(&self.vanilla_root); + self.menu.options.pack_selection = self.settings.resource_packs.clone(); + self.menu.options.pack_screen = None; + self.menu.options_open = true; + } + + fn menu_option_events(&mut self, events: Vec<Event>) { + for event in events { + match event { + Event::Changed => { + self.save_menu_settings(); + self.apply_menu_settings(); + } + Event::Close if self.menu.options.resource_error.is_some() => {} + Event::Close => { + self.menu.options_open = false; + self.menu.dragging = false; + self.save_menu_settings(); + } + Event::Language(code) => { + horizon_client::lang::set(horizon_client::lang::Language::load( + &[&self.menu.root], + &code, + )); + self.save_menu_settings(); + } + Event::Packs(_) => { + self.save_menu_settings(); + self.menu.reload_packs = true; + } + Event::OpenPackFolder => { + let dir = packs::packs_dir(); + let _ = std::fs::create_dir_all(&dir); + packs::open_folder(&dir); + } + Event::OpenModFolder => { + let dir = Game::options_path().with_file_name(horizon_client::mod_list::DIR); + let _ = std::fs::create_dir_all(&dir); + packs::open_folder(&dir); + } + Event::ButtonClick => {} + } + } + } + + fn menu_options_click(&mut self, drag: bool, right: bool) { + let size = self.menu_size(); + let batch = HudBatch::new(size); + let Some(gui) = &self.gui else { return }; + let events = self.menu.options.click( + &mut self.settings, + &gui.atlas, + self.menu.cursor.0, + self.menu.cursor.1, + (batch.width, batch.height), + [size[0] as i32, size[1] as i32], + drag, + right, + ); + self.menu_option_events(events); + } + + fn menu_action(&mut self, event_loop: &ActiveEventLoop, action: Action) { + match action { + Action::Quit => event_loop.exit(), + Action::Options => self.open_menu_options(Page::Main), + Action::Accessibility => self.open_menu_options(Page::Accessibility), + Action::Language => self.open_menu_options(Page::Language), + action => { + let name = self + .options + .as_ref() + .map_or_else(|| "Player".into(), |o| o.player_name()); + self.menu.queue_entry(action, name); + } + } + self.save_menu_address(); + } + + pub(super) fn menu_event( + &mut self, + event_loop: &ActiveEventLoop, + event: WindowEvent, + ) -> Result<()> { + if let WindowEvent::MouseInput { + state, + button: MouseButton::Left, + .. + } = &event + { + horizon_client::ui_theme::set_pressed(*state == ElementState::Pressed); + } + if let WindowEvent::KeyboardInput { event, .. } = &event + && let PhysicalKey::Code(key) = event.physical_key + { + if event.state == ElementState::Pressed { + self.menu.physical_keys.insert(key); + } else { + self.menu.physical_keys.remove(&key); + } + } + match event { + WindowEvent::CloseRequested => { + self.save_menu_settings(); + event_loop.exit(); + } + WindowEvent::Resized(size) => { + if let Some(r) = &mut self.renderer { + r.resize(size.width, size.height); + } + self.apply_menu_settings(); + } + WindowEvent::RedrawRequested => self.draw(), + WindowEvent::ModifiersChanged(modifiers) => self.menu.modifiers = modifiers.state(), + WindowEvent::Focused(false) => { + self.menu.modifiers = ModifiersState::empty(); + self.menu.physical_keys.clear(); + horizon_client::ui_theme::set_pressed(false); + self.menu.dragging = false; + if let Some(title) = &mut self.menu.title { + title.release(); + title.release_field(); + } + let events = self.menu.options.release(); + self.menu_option_events(events); + } + WindowEvent::KeyboardInput { event, .. } + if event.state == ElementState::Pressed && self.menu.pending.is_some() => + { + if event.physical_key == PhysicalKey::Code(KeyCode::Escape) { + self.menu.pending = None; + self.menu.perf.cancel_world_opening(); + } + } + _ if self.gui.is_none() || self.menu.pending.is_some() => {} + WindowEvent::CursorMoved { position, .. } => { + self.menu.pointer = (position.x, position.y); + self.update_menu_cursor(); + if self.menu.options_open && self.menu.dragging { + self.menu_options_click(true, false); + } else if let Some(title) = &mut self.menu.title { + let size = self + .renderer + .as_ref() + .map_or([1280, 720], |r| [r.config.width, r.config.height]); + let b = HudBatch::new(size); + title.drag(self.menu.cursor, (b.width, b.height)); + title.drag_field(&self.gui.as_ref().unwrap().atlas, self.menu.cursor.0); + } + } + WindowEvent::MouseInput { state, button, .. } if self.menu.options_open => { + if state == ElementState::Pressed && self.menu.options.selected_key.is_some() { + let events = self.menu.options.bind_mouse(&mut self.settings, button); + self.menu_option_events(events); + } else if matches!(button, MouseButton::Left | MouseButton::Right) { + if state == ElementState::Pressed { + self.menu.dragging = button == MouseButton::Left; + self.menu_options_click(false, button == MouseButton::Right); + } else if button == MouseButton::Left { + self.menu.dragging = false; + let events = self.menu.options.release(); + self.menu_option_events(events); + } + } + } + WindowEvent::MouseInput { + state, + button: MouseButton::Left, + .. + } => { + let mut batch = HudBatch::new(self.menu_size()); + if let (Some(title), Some(gui)) = (&mut self.menu.title, &self.gui) { + if state == ElementState::Released { + title.release(); + title.release_field(); + } else { + let action = if horizon_client::ui_theme::horizon() { + let action = title.click_horizon( + &gui.atlas, + &mut batch, + self.menu.cursor.0, + self.menu.cursor.1, + ); + // Horizon's deeper world-management pages use classic fields. + title.press_field( + &gui.atlas, + self.menu.cursor, + (batch.width, batch.height), + self.menu.modifiers.shift_key(), + ); + action + } else { + let (action, _) = title.click( + self.menu.cursor.0, + self.menu.cursor.1, + (batch.width, batch.height), + ); + title.press_field( + &gui.atlas, + self.menu.cursor, + (batch.width, batch.height), + self.menu.modifiers.shift_key(), + ); + action + }; + self.menu_action(event_loop, action); + } + } + } + WindowEvent::MouseWheel { delta, .. } => { + let raw = match delta { + MouseScrollDelta::LineDelta(_, y) => y, + MouseScrollDelta::PixelDelta(p) => (p.y / 30.0) as f32, + }; + let amount = if self.settings.discrete_scroll { + horizon_client::mouse::java_signum(f64::from(raw)) as f32 + } else { + raw + } * self.settings.mouse_wheel_sensitivity; + let batch = HudBatch::new(self.menu_size()); + if self.menu.options_open { + self.menu.options.scroll_at( + &self.settings, + (batch.width, batch.height), + self.menu.cursor, + amount, + ); + } else if let Some(title) = &mut self.menu.title { + title.scroll_worlds(amount, self.menu.cursor, (batch.width, batch.height)); + } + } + WindowEvent::KeyboardInput { event, .. } if event.state == ElementState::Pressed => { + if event.physical_key == PhysicalKey::Code(KeyCode::F11) { + self.settings.fullscreen = !self.settings.fullscreen; + self.apply_menu_settings(); + self.save_menu_settings(); + } else if self.menu.options_open { + let shortcut = self.menu.shortcut(&event); + let atlas = &self.gui.as_ref().unwrap().atlas; + let handled = shortcut.is_some_and(|s| { + self.menu + .options + .shortcut(atlas, s, &mut self.menu.clipboard) + }); + if !handled && let PhysicalKey::Code(key) = event.physical_key { + let events = self.menu.options.key( + &mut self.settings, + atlas, + key, + event.text.as_deref(), + ); + self.menu_option_events(events); + } + } else { + self.menu.title_key(&event); + self.save_menu_address(); + } + } + _ => {} + } + Ok(()) + } + + pub(super) fn draw_menu(&mut self, batch: &mut HudBatch) { + let Some(gui) = &self.gui else { return }; + if self.menu.pending.is_some() { + horizon_client::level_loading::draw_progress( + batch, + &gui.atlas, + self.progress.line(), + self.progress.fraction(), + ); + } else if self.menu.options_open { + self.menu + .options + .draw(&self.settings, batch, &gui.atlas, self.menu.cursor); + } else if let Some(title) = &mut self.menu.title { + title.poll_backup(); + let _ = title.update_toast(self.settings.notification_display_time); + if horizon_client::ui_theme::horizon() { + let mut ui = horizon_client::ui_theme::Ui::new(&gui.atlas, batch, self.menu.cursor); + title.draw_horizon(&mut ui, 0.0); + ui.finish(); + } else { + title.draw( + &gui.atlas, + batch, + self.menu.cursor, + self.settings.file.bool("hideSplashTexts"), + ); + } + } + } + + pub(super) fn restart_menu_resources(&mut self) -> Result<()> { + if !self.menu.reload_packs || self.loaded.is_none() { + return Ok(()); + } + if let Some(Ok(loaded)) = &mut self.loaded { + if let Some(loader) = &loaded.rest_loader + && !loader.ready()? + { + return Ok(()); + } + if let Some(loader) = loaded.rest_loader.take() + && let Err(error) = loader.finish() + { + log::warn!("Horizon: replaced resource generation failed: {error}"); + } + } + self.loaded = None; + if let Some(handle) = self.handle.take() { + handle.join().map_err(|_| "resource loader panicked")?; + } + self.progress = Progress::new(); + let options = self.options.as_ref().ok_or("loading already finished")?; + let (receiver, handle) = spawn_loader( + options, + &self.settings, + &self.vanilla_root, + self.started, + &self.progress, + )?; + self.receiver = receiver; + self.handle = Some(handle); + self.menu.reload_packs = false; + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn gui(root: &std::path::Path) -> UiResources { + UiResources { + atlas: HudAtlas::fonts(root).unwrap(), + panorama: Arc::new(horizon_client::gfx::panorama::Faces { + size: 1, + pixels: [96, 110, 111, 255].repeat(6), + }), + } + } + + fn loading(receiver: Receiver<LoaderMsg>) -> Loading { + Loading { + window: None, + renderer: None, + gui: None, + settings: Settings::default(), + fullscreen: horizon_client::window::FullscreenState::new(false), + options: None, + vanilla_root: PathBuf::new(), + pack_error: None, + started: Instant::now(), + progress: Progress::new(), + receiver, + loaded: None, + handle: None, + shown: false, + menu: StartupMenu::default(), + error: None, + } + } + + #[test] + fn menu_is_usable_while_the_core_worker_is_blocked() { + let root = std::env::temp_dir().join("horizon-startup-menu-no-world-resources"); + let (sender, receiver) = std::sync::mpsc::channel(); + let (release, wait) = std::sync::mpsc::channel(); + let (published, published_rx) = std::sync::mpsc::channel(); + let worker_root = root.clone(); + let worker = std::thread::spawn(move || { + sender + .send(LoaderMsg::Ui(gui(&worker_root), worker_root)) + .unwrap(); + published.send(()).unwrap(); + wait.recv().unwrap(); + sender + .send(LoaderMsg::Done(Err("core failure".into()))) + .unwrap(); + }); + published_rx.recv().unwrap(); + let mut loading = loading(receiver); + loading.poll_menu_resources().unwrap(); + assert!(loading.gui.is_some()); + assert!(loading.loaded.is_none()); + assert!(!worker.is_finished()); + // Navigate and enter a world name without a registry, biomes, pack + // tables or a Game. Replacing fallback artwork preserves this state. + let title = loading.menu.title.as_mut().unwrap(); + title.opening.set(None); + title.mode = horizon_client::title::Mode::Create { + name: String::new(), + seed: "42".into(), + creative: true, + focus: 0, + options: Default::default(), + }; + title.type_text("My world"); + loading.set_ui(gui(&root), root); + let action = loading.menu.title.as_ref().unwrap().create_action(); + loading.menu.queue_entry(action, "Player".into()); + assert!( + matches!(&loading.menu.pending, Some(PendingWorldEntry::Local { + name: Some(name), seed: Some(42), creative: true, create: Some(_), .. + }) if name == "My world") + ); + let mut batch = HudBatch::new([1280, 720]); + loading.draw_menu(&mut batch); + assert!( + !batch.vertices.is_empty(), + "queued entry draws loading progress" + ); + release.send(()).unwrap(); + worker.join().unwrap(); + loading.poll_menu_resources().unwrap(); + assert!(matches!(loading.loaded, Some(Err(ref error)) if error == "core failure")); + } + + #[test] + fn queued_server_keeps_address_and_player_identity() { + let mut menu = StartupMenu::default(); + menu.queue_entry( + Action::Connect { + address: "localhost:25565".into(), + }, + "Guest".into(), + ); + assert!( + matches!(menu.pending.take(), Some(PendingWorldEntry::Server { address, name }) + if address == "localhost:25565" && name == "Guest") + ); + assert!(menu.pending.take().is_none(), "an entry is consumed once"); + } + + #[test] + fn disconnected_core_worker_is_reported_instead_of_waiting_forever() { + let (sender, receiver) = std::sync::mpsc::channel(); + drop(sender); + let mut loading = loading(receiver); + assert_eq!( + loading.poll_menu_resources().unwrap_err().to_string(), + "resource loader stopped before finishing" + ); + } +} diff --git a/crates/horizon-client/src/game/station_control.rs b/crates/horizon-client/src/game/station_control.rs index 680939a4f..75ad9225e 100644 --- a/crates/horizon-client/src/game/station_control.rs +++ b/crates/horizon-client/src/game/station_control.rs @@ -126,7 +126,7 @@ impl Game<'_> { "enchant" => { let e = id(arg(1)?); let level: u32 = arg(2)?.parse()?; - if self.res.stations.enchantments.get(&e).is_none() { + if self.res.stations().enchantments.get(&e).is_none() { return Err(format!("unknown enchantment {e}").into()); } let held = self.inventory[self.selected] diff --git a/crates/horizon-client/src/game/stations.rs b/crates/horizon-client/src/game/stations.rs index 8cd751548..e8872b159 100644 --- a/crates/horizon-client/src/game/stations.rs +++ b/crates/horizon-client/src/game/stations.rs @@ -263,7 +263,7 @@ fn argb(c: i32) -> u32 { impl Game<'_> { fn data(&self) -> &stations::StationData { - &self.res.stations + self.res.stations() } pub(super) fn translate(&self, key: &str, fallback: &str) -> String { self.data() @@ -376,9 +376,13 @@ impl Game<'_> { } } fn count_bookshelves(&self, pos: [i32; 3]) -> i32 { - let tags = &self.res.items.block_tags; + let tags = &self.res.items().block_tags; let is = |p: [i32; 3], tag: &str| { - let name = &self.res.facts.get(self.world.block(p[0], p[1], p[2])).name; + let name = &self + .res + .facts() + .get(self.world.block(p[0], p[1], p[2])) + .name; tags.get(tag).is_some_and(|t| t.contains(&**name)) }; stations::bookshelf_offsets() @@ -412,8 +416,8 @@ impl Game<'_> { return; }; let creative = self.creative; - let data = &self.res.stations; - let items = &self.res.items; + let data = self.res.stations(); + let items = self.res.items(); match s.kind { Kind::Enchanting => { s.bookcases = self.count_bookshelves(s.pos); @@ -494,7 +498,10 @@ impl Game<'_> { self.station = Some(s); } pub(super) fn max_stack(&self, stack: &ItemStack) -> u32 { - self.res.items.get(&stack.item).map_or(64, |i| i.max_stack) + self.res + .items() + .get(&stack.item) + .map_or(64, |i| i.max_stack) } /// `Slot.mayPlace` and `getMaxStackSize` for station and brewing slots. fn slot_rules(&self, slot: usize, stack: &ItemStack) -> (bool, u32) { @@ -518,7 +525,7 @@ impl Game<'_> { 3 => (data.brewing.is_reagent(stack), 64), _ => ( self.res - .items + .items() .get(&stack.item) .is_some_and(|i| i.brewing_fuel.is_some()), 64, @@ -548,7 +555,7 @@ impl Game<'_> { 64, ), Kind::Enchanting => (&*stack.item == "minecraft:lapis_lazuli", 64), - Kind::Grindstone => (stations::grindstone_accepts(&self.res.items, stack), 64), + Kind::Grindstone => (stations::grindstone_accepts(self.res.items(), stack), 64), Kind::Smithing => (data.recipes.smithing_accepts(i, stack), 64), _ => (true, 64), } @@ -667,7 +674,7 @@ impl Game<'_> { let data = self.data(); let fuel = self .res - .items + .items() .get(&stack.item) .is_some_and(|i| i.brewing_fuel.is_some()); let (reagent, bottle) = ( @@ -818,7 +825,7 @@ impl Game<'_> { if !self.creative && self.world.random.next_float() < 0.12 { // AnvilBlock.damage let state = self.world.block(pos[0], pos[1], pos[2]); - let name = self.res.facts.get(state).name.clone(); + let name = self.res.facts().get(state).name.clone(); let next = match &*name { "minecraft:anvil" => Some("minecraft:chipped_anvil"), "minecraft:chipped_anvil" => Some("minecraft:damaged_anvil"), @@ -848,7 +855,7 @@ impl Game<'_> { let (a, b) = (s.inputs[0].take(), s.inputs[1].take()); let mut random = LegacyRandom::new(self.world.random.next_long()); let xp = stations::grindstone_experience( - &self.res.stations.enchantments, + &self.res.stations().enchantments, &mut random, a.as_ref(), b.as_ref(), @@ -945,7 +952,7 @@ impl Game<'_> { Some(c) if c.same_item_same_components(&result) && c.count + result.count - <= self.res.items.get(&c.item).map_or(64, |i| i.max_stack) => + <= self.res.items().get(&c.item).map_or(64, |i| i.max_stack) => { c.count += result.count } @@ -988,8 +995,8 @@ impl Game<'_> { return; } let Some(enchanted) = stations::apply_enchantment( - &self.res.stations.enchantments, - &self.res.items, + &self.res.stations().enchantments, + self.res.items(), self.level.xp_seed, button, cost, @@ -1051,7 +1058,8 @@ impl Game<'_> { } } pub(super) fn station_click(&mut self, right: bool) -> bool { - if self.station.is_none() && !self.brewing_open() { + // A server's brewing stand: its slots are the server's. + if self.station.is_none() && (!self.brewing_open() || self.remote.is_some()) { return false; } let Some(size) = self @@ -1249,7 +1257,7 @@ impl Game<'_> { } /// Text in the Standard Galactic alphabet (`minecraft:alt`), cut to `max` pixels. fn galactic(&self, batch: &mut HudBatch, text: &str, x: f32, y: f32, max: f32, color: u32) { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let Some(sheet) = atlas.sprites.get("font/ascii_sga").copied() else { batch.text(atlas, text, x, y, color, false); return; @@ -1325,7 +1333,7 @@ impl Game<'_> { let local = (self.cursor.0 - ox, self.cursor.1 - oy); let mut tooltip: Option<Vec<(String, u32)>> = None; { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; batch.fill( atlas, 0.0, @@ -1342,7 +1350,7 @@ impl Game<'_> { }; let (fuel, total_fuel, brew, total_brew) = (b.fuel, b.total_fuel, b.brew_time, b.total_brew_time); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; Self::part( batch, atlas, @@ -1396,6 +1404,7 @@ impl Game<'_> { .as_ref() .and_then(|s| self.world.block_names.get(&(s.pos[0], s.pos[1], s.pos[2]))) .cloned() + .or_else(|| self.remote_menu_title()) .unwrap_or(default); let tw = HudBatch::text_width(atlas, &title); batch.text( @@ -1417,7 +1426,7 @@ impl Game<'_> { if self.slot_mut(i).is_none() { let (sx, sy) = self.station_slot_position(i).unwrap_or_default(); batch.sprite( - &self.atlas, + &self.gui.atlas, &format!("gui/sprites/container/slot/{icon}"), ox + sx, oy + sy, @@ -1427,7 +1436,7 @@ impl Game<'_> { } else if let Some(kind) = self.station.as_ref().map(|s| s.kind) { let title = self.translate(kind.title_key(), ""); { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; Self::part( batch, atlas, @@ -1446,7 +1455,7 @@ impl Game<'_> { if kind == Kind::Loom { tooltip = self.draw_loom(batch, ox, oy, local); } - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let s = self.station.as_ref().expect("open station"); let has_input = s.inputs.iter().any(Option::is_some); match kind { @@ -1552,7 +1561,7 @@ impl Game<'_> { ); -8323296 }; - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let cw = HudBatch::text_width(atlas, &cost_text); batch.text( atlas, @@ -1566,7 +1575,7 @@ impl Game<'_> { if inside(local, bx, by, bw, 17.0) && let Some((id, l)) = &offers.clues[i] { - let e = &self.res.stations.enchantments; + let e = &self.res.stations().enchantments; let (name, _) = e.full_name(id, *l); let clue = self .translate("container.enchant.clue", "%s . . . ?") @@ -1679,7 +1688,7 @@ impl Game<'_> { ) }; if let Some(line) = line { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let tw = HudBatch::text_width(atlas, &line); let tx = 176.0 - 8.0 - tw - 2.0; batch.fill( @@ -1766,7 +1775,7 @@ impl Game<'_> { i, ItemStack { count: 1, - ..self.res.stations.recipes.stonecutting[s.recipes[i]] + ..self.res.stations().recipes.stonecutting[s.recipes[i]] .result .clone() }, @@ -1788,7 +1797,7 @@ impl Game<'_> { "recipe" }; batch.sprite_scaled( - &self.atlas, + &self.gui.atlas, &format!("gui/sprites/container/stonecutter/{sprite}"), ox + x, oy + y - 1.0, @@ -1816,7 +1825,7 @@ impl Game<'_> { if let Some(slot) = self.slot_at(batch, self.cursor.0, self.cursor.1) { let (sx, sy) = self.slot_position(slot); batch.fill( - &self.atlas, + &self.gui.atlas, ox + sx, oy + sy, 16.0, @@ -1858,7 +1867,7 @@ impl Game<'_> { /// `ItemStack.getTooltipLines` subset: name in rarity colour, potion /// effects, enchantments (gray, curses red), stored enchantments, trims. pub(super) fn stack_tooltip(&self, stack: &ItemStack) -> Vec<(String, u32)> { - let e: &Enchantments = &self.res.stations.enchantments; + let e: &Enchantments = &self.res.stations().enchantments; let props = e.props(&stack.item); let rarity = horizon_client::item_tooltip::rarity( stack, @@ -1885,17 +1894,27 @@ impl Game<'_> { } else { self.pack.names.item(&stack.item) }; - let mut lines = vec![(name, color)]; + let display = horizon_client::item_tooltip::TooltipDisplay::for_stack(stack); + let mut lines = Vec::new(); + if display.name_visible(self.screen == Screen::Creative) { + lines.push((name, color)); + } let translate = |k: &str| e.translate(k).map(str::to_owned); // ItemContainerContents.addToTooltip. let hover_name = |s: &ItemStack| { s.custom_name() .map_or_else(|| self.pack.names.item(&s.item), str::to_owned) }; - for line in horizon_client::item_tooltip::container_lines(stack, &hover_name, &translate) { - lines.push((line, u32::MAX)); + if display.shows("minecraft:container") { + for line in + horizon_client::item_tooltip::container_lines(stack, &hover_name, &translate) + { + lines.push((line, u32::MAX)); + } } - if stack.item.ends_with("potion") || &*stack.item == "minecraft:tipped_arrow" { + if display.shows("minecraft:potion_contents") + && (stack.item.ends_with("potion") || &*stack.item == "minecraft:tipped_arrow") + { let scale = match &*stack.item { "minecraft:lingering_potion" => 0.25, "minecraft:tipped_arrow" => 0.125, @@ -1912,7 +1931,9 @@ impl Game<'_> { lines.push((line, c)); } } - if let Some((material, pattern)) = stack.data().and_then(|d| d.trim.clone()) { + if display.shows("minecraft:trim") + && let Some((material, pattern)) = stack.data().and_then(|d| d.trim.clone()) + { lines.push(( e.translate("item.minecraft.smithing_template.upgrade") .unwrap_or("Upgrade:") @@ -1929,30 +1950,44 @@ impl Game<'_> { lines.push((format!(" {p}"), rgba(170, 170, 170, 255))); lines.push((format!(" {m}"), rgba(170, 170, 170, 255))); } - for (text, curse) in e - .tooltip(stack.stored_enchantments()) - .into_iter() - .chain(e.tooltip(stack.enchantments())) - { - lines.push(( - text, - if curse { - rgba(255, 85, 85, 255) - } else { - rgba(170, 170, 170, 255) - }, - )); + if display.shows("minecraft:stored_enchantments") { + for (text, curse) in e.tooltip(stack.stored_enchantments()) { + lines.push(( + text, + if curse { + rgba(255, 85, 85, 255) + } else { + rgba(170, 170, 170, 255) + }, + )); + } + } + if display.shows("minecraft:enchantments") { + for (text, curse) in e.tooltip(stack.enchantments()) { + lines.push(( + text, + if curse { + rgba(255, 85, 85, 255) + } else { + rgba(170, 170, 170, 255) + }, + )); + } } // BannerPatternLayers.addToTooltip: up to six layer names. - for layer in horizon_client::banner::tooltip_layers(stack).iter().take(6) { - let key = self.res.stations.banners.layer_key(layer); - lines.push((self.translate(&key, &key), rgba(170, 170, 170, 255))); + if display.shows("minecraft:banner_patterns") { + for layer in horizon_client::banner::tooltip_layers(stack).iter().take(6) { + let key = self.res.stations().banners.layer_key(layer); + lines.push((self.translate(&key, &key), rgba(170, 170, 170, 255))); + } } // Spawner.appendHoverText: what a spawner or trial spawner item spawns. - if matches!( - &*stack.item, - "minecraft:spawner" | "minecraft:trial_spawner" - ) { + if display.shows("minecraft:block_entity_data") + && matches!( + &*stack.item, + "minecraft:spawner" | "minecraft:trial_spawner" + ) + { let gray = rgba(170, 170, 170, 255); match block_entity_item::spawner_entity(stack) { Some(id) => { @@ -1969,7 +2004,9 @@ impl Game<'_> { } } } - if let Some(info) = self.res.items.get(&stack.item) { + if display.shows("minecraft:attribute_modifiers") + && let Some(info) = self.res.items().get(&stack.item) + { let base = |name: &str, default| { self.player .as_ref() @@ -1997,7 +2034,8 @@ impl Game<'_> { &translate, )); } - if let Some(info) = self.res.items.get(&stack.item) + if display.shows("minecraft:damage") + && let Some(info) = self.res.items().get(&stack.item) && info.max_damage > 0 && stack.damage > 0 && self.options_advanced_tooltips() @@ -2039,7 +2077,7 @@ impl Game<'_> { _ => serde_json::Value::Null, }; }; - let recipes = &self.res.stations.recipes.stonecutting; + let recipes = &self.res.stations().recipes.stonecutting; serde_json::json!({ "costs": s.offers.costs, "clues": s.offers.clues.iter().map(|c| c.as_ref().map(|(e, l)| format!("{e} {l}"))).collect::<Vec<_>>(), @@ -2065,7 +2103,7 @@ impl Game<'_> { if lines.is_empty() { return; } - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let w = lines .iter() .map(|(t, _)| HudBatch::text_width(atlas, t)) @@ -2144,22 +2182,22 @@ impl Game<'_> { // ---------------------------------------------------------------- brewing stand - /// Ticks every brewing stand (`BrewingStandBlockEntity.serverTick`). - pub(super) fn tick_brewing_stands(&mut self) -> horizon_client::Result<()> { - let mut changes = Vec::new(); - for (pos, entity) in self.world.block_entities.iter_mut() { - if let BlockEntity::Brewing(b) = entity { - let out = b.tick( - &self.res.stations.brewing, - &self.res.items, - &self.res.providers, - ); - if out != brewing::BrewTick::default() { - changes.push((*pos, out)); - } - } - } - for ((x, y, z), out) in changes { + /// `BrewingStandBlockEntity.serverTick` of the brewing stand at `pos`, in + /// its place among the level's block entity tickers. + pub(super) fn tick_brewing_stand( + &mut self, + pos: (i32, i32, i32), + ) -> horizon_client::Result<()> { + let Some(BlockEntity::Brewing(b)) = self.world.block_entities.get_mut(&pos) else { + return Ok(()); + }; + let out = b.tick( + &self.res.stations().brewing, + self.res.items(), + self.res.providers(), + ); + if out != brewing::BrewTick::default() { + let (x, y, z) = pos; self.world.modified.insert((x >> 4, z >> 4)); if out.brewed { self.play_at("minecraft:block.brewing_stand.brew", [x, y, z], 1.0, 1.0); diff --git a/crates/horizon-client/src/game/stats.rs b/crates/horizon-client/src/game/stats.rs index e66e68355..7a97416ca 100644 --- a/crates/horizon-client/src/game/stats.rs +++ b/crates/horizon-client/src/game/stats.rs @@ -3,8 +3,12 @@ //! killed by, saved to `players/stats/<uuid>.json`, plus the phantom //! spawner that reads the time since rest (`PhantomSpawner`). use super::{Dim, Game}; +use horizon_core::geometry::Vec3; use horizon_core::random::RandomSource; -use std::collections::BTreeMap; +use std::{ + collections::{BTreeMap, BTreeSet}, + path::{Path, PathBuf}, +}; /// `stats[type][key]`, with `type` and `key` namespaced ids. #[derive(Default)] @@ -16,18 +20,50 @@ pub(super) struct Stats { /// Statistic changes (type, key, amount; `None` = reset) for the /// scoreboard criteria to follow. pub(super) events: Vec<(String, String, Option<i32>)>, + /// Changed since the client was last sent them (`StatsCounter`'s dirty + /// set), by type. + dirty: BTreeMap<String, BTreeSet<String>>, } + +/// `minecraft:` and `name`, given to `f` from the stack when short: some +/// counters go up every tick for every player, and they are found without +/// a string made for each. +fn namespaced<T>(name: &str, f: impl FnOnce(&str) -> T) -> T { + const NAMESPACE: &str = "minecraft:"; + let mut buf = [0u8; 96]; + let len = NAMESPACE.len() + name.len(); + if len > buf.len() { + return f(&format!("{NAMESPACE}{name}")); + } + buf[..NAMESPACE.len()].copy_from_slice(NAMESPACE.as_bytes()); + buf[NAMESPACE.len()..len].copy_from_slice(name.as_bytes()); + // Two strings one after the other are one. + f(std::str::from_utf8(&buf[..len]).unwrap_or_default()) +} + +/// The map under `key`, made when missing, found without an owned key +/// when there. +fn entry<'a, V: Default>(map: &'a mut BTreeMap<String, V>, key: &str) -> &'a mut V { + if !map.contains_key(key) { + map.insert(key.to_owned(), V::default()); + } + map.get_mut(key).expect("just inserted") +} + impl Stats { pub(super) fn add(&mut self, ty: &str, key: &str, n: i32) { - let v = self - .values - .entry(format!("minecraft:{ty}")) - .or_default() - .entry(key.to_owned()) - .or_insert(0); - *v = v.saturating_add(n); - self.events - .push((format!("minecraft:{ty}"), key.to_owned(), Some(n))); + namespaced(ty, |ty| { + let v = entry(entry(&mut self.values, ty), key); + *v = v.saturating_add(n); + self.events.push((ty.to_owned(), key.to_owned(), Some(n))); + self.mark_dirty(ty, key); + }); + } + fn mark_dirty(&mut self, ty: &str, key: &str) { + let keys = entry(&mut self.dirty, ty); + if !keys.contains(key) { + keys.insert(key.to_owned()); + } } pub(super) fn get(&self, ty: &str, key: &str) -> i32 { self.values @@ -40,52 +76,51 @@ impl Stats { if let Some(m) = self.values.get_mut(&format!("minecraft:{ty}")) { m.insert(key.to_owned(), 0); } + self.mark_dirty(&format!("minecraft:{ty}"), key); self.events .push((format!("minecraft:{ty}"), key.to_owned(), None)); } pub(super) fn custom(&mut self, key: &str, n: i32) { - self.add("custom", &format!("minecraft:{key}"), n); + namespaced(key, |key| self.add("custom", key, n)); } -} -impl Game<'_> { - fn stats_path(&self) -> Option<std::path::PathBuf> { - let uuid = self.level.player_uuid?; - let hex: String = uuid.iter().map(|v| format!("{:08x}", *v as u32)).collect(); - let text = format!( - "{}-{}-{}-{}-{}", - &hex[0..8], - &hex[8..12], - &hex[12..16], - &hex[16..20], - &hex[20..32] - ); - Some( - self.world_root - .join("players/stats") - .join(format!("{text}.json")), - ) + /// `ServerStatsCounter.markAllDirty`: everything goes out on the next request. + pub(super) fn mark_all_dirty(&mut self) { + self.dirty = self + .values + .iter() + .map(|(ty, m)| (ty.clone(), m.keys().cloned().collect())) + .collect(); } - /// `ServerStatsCounter.parseLocal`. - pub(super) fn load_stats(&mut self) { - if self.stats.loaded || self.remote.is_some() { - return; - } - self.stats.loaded = true; - let Some(path) = self.stats_path() else { - return; + /// `ServerStatsCounter.sendStats`' entries: the dirty statistics and + /// their values, the set cleared. + pub(super) fn take_dirty(&mut self) -> Vec<(String, String, i32)> { + std::mem::take(&mut self.dirty) + .into_iter() + .flat_map(|(ty, keys)| keys.into_iter().map(move |key| (ty.clone(), key))) + .map(|(ty, key)| { + let v = self.values.get(&ty).and_then(|m| m.get(&key)).copied(); + (ty, key, v.unwrap_or(0)) + }) + .collect() + } + /// `ServerStatsCounter.parseLocal`: the counters of a stats file. + pub(super) fn read(path: &Path) -> Self { + let mut stats = Self { + loaded: true, + ..Self::default() }; - let Ok(text) = std::fs::read_to_string(path) else { - return; + let Some(json) = std::fs::read_to_string(path) + .ok() + .and_then(|t| serde_json::from_str::<serde_json::Value>(&t).ok()) + else { + return stats; }; - let Ok(json) = serde_json::from_str::<serde_json::Value>(&text) else { - return; - }; - if let Some(stats) = json.get("stats").and_then(|s| s.as_object()) { - for (ty, entries) in stats { + if let Some(all) = json.get("stats").and_then(|s| s.as_object()) { + for (ty, entries) in all { let Some(entries) = entries.as_object() else { continue; }; - let map = self.stats.values.entry(ty.clone()).or_default(); + let map = stats.values.entry(ty.clone()).or_default(); for (key, v) in entries { if let Some(n) = v.as_i64() { map.insert(key.clone(), n as i32); @@ -93,17 +128,11 @@ impl Game<'_> { } } } + stats } /// `ServerStatsCounter.save`. - pub(super) fn save_stats(&self) { - if !self.stats.loaded || self.remote.is_some() { - return; - } - let Some(path) = self.stats_path() else { - return; - }; + pub(super) fn write(&self, path: &Path) { let stats: serde_json::Map<String, serde_json::Value> = self - .stats .values .iter() .filter(|(_, m)| !m.is_empty()) @@ -124,9 +153,55 @@ impl Game<'_> { let _ = std::fs::write(path, text); } } +} +/// `players/<dir>/<uuid>.json` of a world (advancements, stats). +pub(super) fn player_json(world: &Path, dir: &str, uuid: [i32; 4]) -> PathBuf { + let hex: String = uuid.iter().map(|v| format!("{:08x}", *v as u32)).collect(); + let text = format!( + "{}-{}-{}-{}-{}", + &hex[0..8], + &hex[8..12], + &hex[12..16], + &hex[16..20], + &hex[20..32] + ); + world.join("players").join(dir).join(format!("{text}.json")) +} +impl Game<'_> { + fn stats_path(&self) -> Option<PathBuf> { + Some(player_json( + &self.world_root, + "stats", + self.level.player_uuid?, + )) + } + /// `ServerStatsCounter.parseLocal`. + pub(super) fn load_stats(&mut self) { + if self.stats.loaded || self.remote.is_some() || self.no_player_here() { + return; + } + self.stats.loaded = true; + if let Some(path) = self.stats_path() { + self.stats.values = Stats::read(&path).values; + } + } + /// `ServerStatsCounter.save`. + pub(super) fn save_stats(&self) { + if !self.stats.loaded || self.remote.is_some() || self.no_player_here() { + return; + } + if let Some(path) = self.stats_path() { + self.stats.write(&path); + } + } /// The per-tick counters (`Player.tick`/`ServerPlayer.tick` awardStat). pub(super) fn tick_stats(&mut self) { self.load_stats(); + self.count_tick_stats(self.sleeping.is_some()); + self.tick_movement_stats(); + } + /// `Player.tick`'s counters for the player in `self.player`. + pub(super) fn count_tick_stats(&mut self, sleeping: bool) { let Some(p) = self.player.as_ref() else { return; }; @@ -137,21 +212,26 @@ impl Game<'_> { s.custom("total_world_time", 1); if alive { s.custom("time_since_death", 1); - if self.sleeping.is_none() { + if !sleeping { s.custom("time_since_rest", 1); } } if crouching { s.custom("sneak_time", 1); } - self.tick_movement_stats(); } - /// `Player.checkMovementStatistics` / `checkRidingStatistics`. fn tick_movement_stats(&mut self) { let Some(p) = self.player.as_ref() else { return; }; let d = p.position.subtract(self.previous_position); + self.movement_stats(d); + } + /// `Player.checkMovementStatistics` / `checkRidingStatistics` for one move. + pub(super) fn movement_stats(&mut self, d: Vec3) { + let Some(p) = self.player.as_ref() else { + return; + }; let horizontal = ((d.x * d.x + d.z * d.z).sqrt() * 100.0).round() as i32; let total = ((d.x * d.x + d.y * d.y + d.z * d.z).sqrt() * 100.0).round() as i32; // Teleports are not travel. @@ -164,14 +244,14 @@ impl Game<'_> { p.position.y.floor() as i32, p.position.z.floor() as i32, ]; - let eye_in_water = self.res.facts.get(self.world.block( + let eye_in_water = self.res.facts().get(self.world.block( feet[0], (p.position.y + f64::from(p.dimensions.eye_height)).floor() as i32, feet[2], )); let in_water = self .res - .facts + .facts() .get(self.world.block(feet[0], feet[1], feet[2])) .fluid == horizon_client::blocks::Fluid::Water; @@ -265,7 +345,11 @@ impl Game<'_> { pos[1] + 20 + r.next_int_bound(15).unwrap_or(0), pos[2] - 10 + r.next_int_bound(21).unwrap_or(10), ]; - if !self.res.facts.is_air(self.world.block(at[0], at[1], at[2])) { + if !self + .res + .facts() + .is_air(self.world.block(at[0], at[1], at[2])) + { return; } let group = 1 + self.world.random.next_int_bound(3).unwrap_or(0); @@ -283,8 +367,59 @@ impl Game<'_> { } fn can_see_sky(&self, pos: [i32; 3]) -> bool { (pos[1] + 1..pos[1] + 256).all(|y| { - let f = self.res.facts.get(self.world.block(pos[0], y, pos[2])); + let f = self.res.facts().get(self.world.block(pos[0], y, pos[2])); !f.full_collision && f.fluid == horizon_client::blocks::Fluid::None }) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn counters_events_and_dirty_entries() { + let mut s = Stats::default(); + s.custom("play_time", 1); + s.custom("play_time", 1); + s.add("mined", "minecraft:stone", 2); + s.custom(&"x".repeat(100), 3); + assert_eq!(s.get("custom", "minecraft:play_time"), 2); + assert_eq!(s.get("mined", "minecraft:stone"), 2); + assert_eq!( + s.get("custom", &format!("minecraft:{}", "x".repeat(100))), + 3 + ); + assert_eq!(s.events.len(), 4); + assert_eq!( + s.events[1], + ( + "minecraft:custom".into(), + "minecraft:play_time".into(), + Some(1) + ) + ); + let long = format!("minecraft:{}", "x".repeat(100)); + let sent = |v: &[(&str, &str, i32)]| -> Vec<(String, String, i32)> { + v.iter() + .map(|&(t, k, n)| (t.to_owned(), k.to_owned(), n)) + .collect() + }; + assert_eq!( + s.take_dirty(), + sent(&[ + ("minecraft:custom", "minecraft:play_time", 2), + ("minecraft:custom", &long, 3), + ("minecraft:mined", "minecraft:stone", 2), + ]) + ); + assert!(s.take_dirty().is_empty()); + s.reset("mined", "minecraft:stone"); + assert_eq!( + s.take_dirty(), + sent(&[("minecraft:mined", "minecraft:stone", 0)]) + ); + s.mark_all_dirty(); + assert_eq!(s.take_dirty().len(), 3); + } +} diff --git a/crates/horizon-client/src/game/stats_screen.rs b/crates/horizon-client/src/game/stats_screen.rs index f023f30b7..5505d423d 100644 --- a/crates/horizon-client/src/game/stats_screen.rs +++ b/crates/horizon-client/src/game/stats_screen.rs @@ -173,6 +173,12 @@ const FOOTER: f32 = 33.0; impl Game<'_> { pub(super) fn open_stats(&mut self) { self.load_stats(); + // StatsScreen.init: a server's client asks for its statistics. + if self.remote.is_some() { + self.send(horizon_protocol::packets::play::Serverbound::ClientCommand( + horizon_protocol::packets::play::ClientCommand { action: 1 }, + )); + } self.stats_ui = StatsUi::default(); self.screen = Screen::Stats; } @@ -216,7 +222,7 @@ impl Game<'_> { } pub(super) fn draw_stats(&mut self, batch: &mut HudBatch) { let (w, h) = (batch.width, batch.height); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; ui::menu_background(batch, atlas, 0.0, 0.0, w, h, true); let (top, bottom) = (TAB_H, h - FOOTER); ui::separators(batch, atlas, top, bottom, true); @@ -327,13 +333,13 @@ impl Game<'_> { } let stack = ItemStack::new(item, 1); batch.sprite( - &self.atlas, + &self.gui.atlas, "gui/sprites/container/slot", left + 1.0, y + 1.0, ); self.draw_item(batch, &stack, left + 2.0, y + 2.0); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let color = if (i + 1) % 2 == 0 { u32::MAX } else { @@ -341,8 +347,12 @@ impl Game<'_> { }; for (c, (ty, _)) in COLUMNS.iter().enumerate() { let x = left + 75.0 + 40.0 * c as f32; - let block = - c == 0 && self.res.items.get(item).is_some_and(|i| i.block.is_some()); + let block = c == 0 + && self + .res + .items() + .get(item) + .is_some_and(|i| i.block.is_some()); let text = if c == 0 && !block { Text::translate("stats.none", &[]).plain() } else { @@ -394,7 +404,7 @@ impl Game<'_> { } } } - let atlas = &self.atlas; + let atlas = &self.gui.atlas; ui::scrollbar( batch, atlas, @@ -407,7 +417,7 @@ impl Game<'_> { self.draw_stats_footer(batch, w, h); } fn draw_stats_footer(&self, batch: &mut HudBatch, w: f32, h: f32) { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let (bx, by) = ((w / 2.0 - 100.0).floor(), h - FOOTER + 6.0); let hover = ui::hovered(self.cursor, bx, by, 200.0, 20.0); ui::button( diff --git a/crates/horizon-client/src/game/storage.rs b/crates/horizon-client/src/game/storage.rs index 44b62e276..9b28ce26b 100644 --- a/crates/horizon-client/src/game/storage.rs +++ b/crates/horizon-client/src/game/storage.rs @@ -22,7 +22,7 @@ impl Game<'_> { (300..400).contains(&slot) && is_shulker_box(&stack.item) && self.container.is_some_and(|k| { - is_shulker_box(&self.res.facts.get(self.world.block(k.0, k.1, k.2)).name) + is_shulker_box(&self.res.facts().get(self.world.block(k.0, k.1, k.2)).name) }) } /// Opening an ender chest shows the player's ender inventory in its place. @@ -160,7 +160,7 @@ impl Game<'_> { let k = self.container?; Some( self.res - .facts + .facts() .get(self.world.block(k.0, k.1, k.2)) .name .clone(), @@ -211,7 +211,7 @@ impl Game<'_> { oy: f32, ) { use horizon_client::hud::rgba; - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let grey = rgba(64, 64, 64, 255); if self .container_block() diff --git a/crates/horizon-client/src/game/subtitle_hud.rs b/crates/horizon-client/src/game/subtitle_hud.rs index cf89a4b15..f54ca874d 100644 --- a/crates/horizon-client/src/game/subtitle_hud.rs +++ b/crates/horizon-client/src/game/subtitle_hud.rs @@ -47,7 +47,7 @@ impl Game<'_> { width: batch.width as i32, height: batch.height as i32, }; - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let draws = self .subtitles .overlay diff --git a/crates/horizon-client/src/game/sulfur_cubes.rs b/crates/horizon-client/src/game/sulfur_cubes.rs index 4606f6859..d1bf9def9 100644 --- a/crates/horizon-client/src/game/sulfur_cubes.rs +++ b/crates/horizon-client/src/game/sulfur_cubes.rs @@ -5,11 +5,77 @@ use super::Game; use horizon_client::{ items::ItemStack, + mesher::Vertex, mobs::{Mob, Species, sulfur_cube}, }; use horizon_core::random::RandomSource; +fn block_transform(base: glam::Mat4, baby: bool) -> glam::Mat4 { + base * glam::Mat4::from_scale(glam::Vec3::new(1.0, -1.0, -1.0)) + * glam::Mat4::from_scale(glam::Vec3::splat(if baby { 0.5 } else { 1.0 })) + * glam::Mat4::from_translation(glam::Vec3::new(-0.5, -0.518, -0.5)) +} + +fn block_overlay(fuse_ticks: i32, alpha: f32, hurt: bool) -> u32 { + let fuse = if fuse_ticks >= 0 { + fuse_ticks as f32 - alpha + 1.0 + } else { + 0.0 + }; + if fuse > 0.0 && (fuse / 5.0) as i32 % 2 == 0 { + horizon_client::overlay::living(1.0, false) + } else { + horizon_client::overlay::living(0.0, hurt) + } +} + +fn block_light(light: u32, emission: u8) -> u32 { + let block = (light >> 8 & 0xFF).max(u32::from(emission) * 16); + (light & 0xFF) | block << 8 +} + impl Game<'_> { + /// The swallowed block replaces the opaque core and precedes the shell. + pub(super) fn sulfur_cube_body_geometry( + &self, + mob: &Mob, + alpha: f32, + base: glam::Mat4, + light: u32, + out: &mut Vec<Vertex>, + ) { + let Some(item) = mob.extra.head.as_deref() else { + return; + }; + if super::armor_stands::skull_kind(item).is_some() { + return; + } + // shortcut: the body slot retains only an item id; preserve the stack + // before supporting its block-state components here. + let Some(state) = self.res.items().get(item).and_then(|item| item.block) else { + return; + }; + let start = out.len(); + self.block_model_vertices( + state, + 42, + block_transform(base, mob.is_baby()), + block_light(light, self.res.facts().get(state).emission), + true, + out, + ); + let overlay = block_overlay( + sulfur_cube::fuse(mob), + alpha, + mob.hurt_time > 0 || mob.death_time > 0, + ); + if overlay != 0 { + for vertex in &mut out[start..] { + vertex.layer = overlay; + } + } + } + fn sulfur_sound(&mut self, index: usize, sound: &str) { let at = self.mobs[index].center(); self.audio @@ -126,6 +192,11 @@ impl Game<'_> { continue; } let item = e.stack.item.clone(); + let thrown_by_player = e + .thrower + .is_some_and(|uuid| Some(uuid) == self.level.player_uuid); + let mut swallowed = e.stack.clone(); + swallowed.count = 1; let (taken_item, id) = (e.clone(), self.mobs[i].id); self.add_pickup( super::pickups::Taken::Item(Box::new(taken_item)), @@ -140,6 +211,14 @@ impl Game<'_> { m.extra.head = Some(item); m.extra.point = None; self.sulfur_sound(i, "entity.sulfur_cube.absorb"); + if thrown_by_player { + let target = self.mob_entity(&self.mobs[i]); + self.trigger_item_entity( + "minecraft:thrown_item_picked_up_by_entity", + &swallowed, + &target, + ); + } } } /// `AbstractCubeMob.remove`: a dead adult splits into two babies (none @@ -167,3 +246,46 @@ impl Game<'_> { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn swallowed_block_uses_vanilla_lift_and_baby_scale() { + let base = glam::Mat4::from_translation(glam::Vec3::new(3.0, 4.0, -2.0)) + * glam::Mat4::from_rotation_y(0.7) + * glam::Mat4::from_scale(glam::Vec3::new(-1.0, -1.0, 1.0)); + for baby in [false, true] { + let size = if baby { 0.5 } else { 1.0 }; + let block = block_transform(base, baby); + for (point, expected) in [ + (glam::Vec3::ZERO, glam::Vec3::new(-0.5, 0.518, 0.5)), + (glam::Vec3::ONE, glam::Vec3::new(0.5, -0.482, -0.5)), + ] { + let actual = block.transform_point3(point); + let expected = base.transform_point3(expected * size); + assert!(actual.abs_diff_eq(expected, 1e-5), "baby={baby}"); + } + assert!(block.transform_vector3(glam::Vec3::Y).y > 0.0); + } + } + + #[test] + fn swallowed_tnt_flashes_white_over_hurt_using_the_interpolated_fuse() { + let white = horizon_client::overlay::WHITE + 15; + assert_eq!(block_overlay(-1, 0.0, false), 0); + assert_eq!(block_overlay(-1, 0.0, true), 1); + assert_eq!(block_overlay(9, 0.0, true), white); + assert_eq!(block_overlay(9, 0.5, false), 0); + assert_eq!(block_overlay(9, 0.5, true), 1); + assert_eq!(block_overlay(0, 0.5, false), white); + } + + #[test] + fn contained_block_emission_preserves_brighter_light_and_the_sky_channel() { + assert_eq!(block_light(0x2050, 1), 0x2050); + assert_eq!(block_light(0x2050, 7), 0x7050); + assert_eq!(block_light(0x0070, 0), 0x0070); + } +} diff --git a/crates/horizon-client/src/game/titles.rs b/crates/horizon-client/src/game/titles.rs index 459aab503..70ade5595 100644 --- a/crates/horizon-client/src/game/titles.rs +++ b/crates/horizon-client/src/game/titles.rs @@ -16,10 +16,10 @@ impl Game<'_> { let (w, h) = (batch.width as i32, batch.height as i32); let partial = self.clock.fraction() as f32; let background = self.settings.text_background(0.0); - let font = &self.atlas.font; + let font = &self.gui.atlas.font; let mut draws = self.titles.overlay_message(w, h, partial, font, background); draws.extend(self.titles.title(w, h, partial, font, background)); - hud_layout::paint(batch, &self.atlas, &draws); + hud_layout::paint(batch, &self.gui.atlas, &draws); } /// The selected stack as `Hud.tick` compares it for the name highlight. fn held_name(&self) -> Option<HeldName> { @@ -54,9 +54,9 @@ impl Game<'_> { batch.width as i32, batch.height as i32, can_hurt, - &self.atlas.font, + &self.gui.atlas.font, self.settings.text_background(0.0), ); - hud_layout::paint(batch, &self.atlas, &draws); + hud_layout::paint(batch, &self.gui.atlas, &draws); } } diff --git a/crates/horizon-client/src/game/tnt.rs b/crates/horizon-client/src/game/tnt.rs index 78f107599..80ad97d7f 100644 --- a/crates/horizon-client/src/game/tnt.rs +++ b/crates/horizon-client/src/game/tnt.rs @@ -12,6 +12,8 @@ use horizon_world::level::flags as level_flags; #[derive(Clone, Debug)] pub(super) struct PrimedTnt { + /// The entity id a server tracks it by (0 until it is tracked). + pub id: u32, pub position: Vec3, pub previous: Vec3, pub velocity: Vec3, @@ -140,6 +142,7 @@ impl Game<'_> { f64::from(pos.z) + 0.5, ); self.tnts.push(PrimedTnt { + id: 0, position, previous: position, velocity: Vec3::new(-rot.sin() * 0.02, f64::from(0.2f32), -rot.cos() * 0.02), @@ -153,13 +156,19 @@ impl Game<'_> { pub(super) fn tick_tnt(&mut self) -> Result<()> { let mut count = self.tnts.len(); let mut i = 0; + // ServerLevel.tick: only entities in entity-ticking chunks tick. + let ticking = self.entity_ticking_area(); while i < count { let t = &mut self.tnts[i]; + if !ticking(t.position.x.floor() as i32, t.position.z.floor() as i32) { + i += 1; + continue; + } t.tick( &self.world, self.collision, &self.pack.assets, - &self.res.facts, + self.res.facts(), )?; if t.fuse <= 0 { let center = [ @@ -186,7 +195,7 @@ impl Game<'_> { return Ok(false); }; let state = self.world.block(hit.pos[0], hit.pos[1], hit.pos[2]); - if &*self.res.facts.get(state).name != "minecraft:tnt" { + if &*self.res.facts().get(state).name != "minecraft:tnt" { return Ok(false); } let pos = BlockPos::new(hit.pos[0], hit.pos[1], hit.pos[2]); @@ -216,7 +225,7 @@ impl Game<'_> { // Face sprites from the block model: up, down, north, south, west, east. let Ok(models) = self .pack - .assets + .render .model_catalog .collect(state, BlockPos::new(0, 0, 0)) else { @@ -238,10 +247,27 @@ impl Game<'_> { .pack .layer_names .get(&face.layer) - .and_then(|name| self.atlas.sprites.get(name).copied()); + .and_then(|name| self.gui.atlas.sprites.get(name).copied()); } } + let range = super::entity_culling::default_range( + glam::DVec3::splat(PrimedTnt::SIZE), + self.settings.entity_distance, + ); for t in &self.tnts { + // EntityRenderer.shouldRender. + if !self.standing_entity_in_view( + camera, + super::entity_culling::at(t.previous), + super::entity_culling::at(t.position), + alpha, + PrimedTnt::SIZE, + PrimedTnt::SIZE, + range, + 0.0, + ) { + continue; + } let fuse = t.fuse as f32 - alpha as f32 + 1.0; let scale = if fuse < 10.0 { let g = (1.0 - fuse / 10.0).clamp(0.0, 1.0); @@ -467,9 +493,10 @@ mod tests { return; } let assets = horizon_client::assets::Assets::load(&root).unwrap(); + let render = assets.reload(&root).unwrap(); let facts = horizon_client::blocks::BlockFacts::load(&root, assets.registry).unwrap(); let collision = horizon_client::collisions::load(&root, assets.registry).unwrap(); - let mut world = horizon_client::world::World::test_scene(&assets, 3).unwrap(); + let mut world = horizon_client::world::World::test_scene(&assets, &render, 3).unwrap(); let stone = assets.registry.default_state("minecraft:stone").unwrap(); let water = assets.registry.default_state("minecraft:water").unwrap(); let mut state = assets.registry.state(water).unwrap().clone(); @@ -484,6 +511,7 @@ mod tests { world.set(21, 4, 20, flowing).unwrap(); let at = Vec3::new(20.5, 4.0, 20.5); let mut tnt = PrimedTnt { + id: 0, position: at, previous: at, velocity: Vec3::ZERO, diff --git a/crates/horizon-client/src/game/totem.rs b/crates/horizon-client/src/game/totem.rs index 805360c8d..47b67d9f8 100644 --- a/crates/horizon-client/src/game/totem.rs +++ b/crates/horizon-client/src/game/totem.rs @@ -53,9 +53,7 @@ impl Game<'_> { ("fire_resistance", 800, 0), ] { if let Some(e) = horizon_client::brewing::effect(id) { - self.potions - .effects - .add(EffectInstance::new(e.id, ticks, amp)); + self.add_player_effect(EffectInstance::new(e.id, ticks, amp)); } } self.audio diff --git a/crates/horizon-client/src/game/trading.rs b/crates/horizon-client/src/game/trading.rs index ce389ac84..87e0c18c6 100644 --- a/crates/horizon-client/src/game/trading.rs +++ b/crates/horizon-client/src/game/trading.rs @@ -147,7 +147,7 @@ impl Game<'_> { variant: &str, origin: [f64; 3], ) -> Vec<MerchantOffer> { - let Some(sequence) = self.res.stations.trades.sequence(set) else { + let Some(sequence) = self.res.stations().trades.sequence(set) else { return Vec::new(); }; let mut random = self.rand_sequences.lend(&sequence, self.level.seed); @@ -160,15 +160,15 @@ impl Game<'_> { next_id: std::cell::Cell::new(next_map), }; let ctx = TradeContext { - items: &self.res.items, - enchantments: &self.res.stations.enchantments, + items: self.res.items(), + enchantments: &self.res.stations().enchantments, variant, origin, maps: Some(&maps), }; let offers = self .res - .stations + .stations() .trades .offers_from_set(&ctx, set, &mut random); let made = maps.made.into_inner(); @@ -205,7 +205,7 @@ impl Game<'_> { let profession = PROFESSIONS[m.extra.variant2 as usize % PROFESSIONS.len()]; let level = i32::from(m.extra.variant3.max(1)); let set = merchant::Trades::set_for(profession, level); - if self.res.stations.trades.sets.contains_key(&set) { + if self.res.stations().trades.sets.contains_key(&set) { self.offers_from_set(&set, &variant, origin) } else { Vec::new() @@ -284,7 +284,7 @@ impl Game<'_> { session.reputation = self.villager_reputation(id); session.hero = self.hero_amplifier(); for seq in merchant::Session::sequences_needed( - &self.res.stations.trades, + &self.res.stations().trades, &session.profession, level, ) { @@ -547,8 +547,8 @@ impl Game<'_> { m.focused, ); let offers = m.session.offers.clone(); - let costs: Vec<ItemStack> = offers.iter().map(|o| o.cost_a(&self.res.items)).collect(); - let atlas = &self.atlas; + let costs: Vec<ItemStack> = offers.iter().map(|o| o.cost_a(self.res.items())).collect(); + let atlas = &self.gui.atlas; batch.fill( atlas, 0.0, @@ -563,7 +563,7 @@ impl Game<'_> { r.v1 = r.v0 + 166.0 * dv; batch.rect_uv(ox + SHIFT, oy, 276.0, 166.0, r, u32::MAX); } - let e = &self.res.stations.enchantments; + let e = &self.res.stations().enchantments; let tr = |k: &str, f: &str| e.translate(k).unwrap_or(f).to_owned(); let name = custom.unwrap_or_else(|| { if progress { @@ -687,7 +687,7 @@ impl Game<'_> { } else { "gui/sprites/widget/button" }; - batch.sprite_scaled(&self.atlas, sprite, bx, by, 88.0, 20.0, u32::MAX); + batch.sprite_scaled(&self.gui.atlas, sprite, bx, by, 88.0, 20.0, u32::MAX); let y = oy + 16.0 + 1.0 + 20.0 * row as f32 + 2.0; let base = offer.base_cost_a.stack.clone(); let cost_a = costs[index].clone(); @@ -705,7 +705,7 @@ impl Game<'_> { x, y, ); - let atlas = &self.atlas; + let atlas = &self.gui.atlas; let t = base.count.to_string(); batch.text( atlas, @@ -740,7 +740,7 @@ impl Game<'_> { "trade_arrow" }; batch.sprite( - &self.atlas, + &self.gui.atlas, &format!("gui/sprites/container/villager/{arrow}"), ox + SHIFT + 60.0, y + 3.0, @@ -770,7 +770,7 @@ impl Game<'_> { if let Some(slot) = self.slot_at(batch, self.cursor.0, self.cursor.1) { let (sx, sy) = self.slot_position(slot); batch.fill( - &self.atlas, + &self.gui.atlas, ox + sx, oy + sy, 16.0, @@ -794,7 +794,7 @@ impl Game<'_> { } else if deprecated { let text = self .res - .stations + .stations() .enchantments .translate("merchant.deprecated") .unwrap_or("Villagers restock up to two times per day.") diff --git a/crates/horizon-client/src/game/trial_spawner.rs b/crates/horizon-client/src/game/trial_spawner.rs index 4107e0859..42a26c6e4 100644 --- a/crates/horizon-client/src/game/trial_spawner.rs +++ b/crates/horizon-client/src/game/trial_spawner.rs @@ -106,34 +106,21 @@ impl TrialSpawnerLevel for GameLevel<'_, '_> { } impl Game<'_> { - /// Every trial spawner's server tick. - pub(super) fn tick_trial_spawners(&mut self) -> Result<()> { - let keys: Vec<Pos> = self - .world + /// The server tick of the trial spawner at `key`. Its client tick comes + /// after every block entity's. + pub(super) fn tick_trial_spawner(&mut self, key: Pos) -> Result<()> { + let Some((current, ominous)) = self.trial_spawner_block(key) else { + return Ok(()); + }; + let Some(BlockEntity::TrialSpawner(mut spawner)) = self.world.block_entities.take(&key) + else { + return Ok(()); + }; + let result = self.server_tick_trial_spawner(key, &mut spawner, current, ominous); + self.world .block_entities - .iter() - .filter(|(_, e)| matches!(e, BlockEntity::TrialSpawner(_))) - .map(|(k, _)| *k) - .collect(); - for &key in &keys { - let Some((current, ominous)) = self.trial_spawner_block(key) else { - continue; - }; - let Some(BlockEntity::TrialSpawner(mut spawner)) = - self.world.block_entities.remove(&key) - else { - continue; - }; - let result = self.server_tick_trial_spawner(key, &mut spawner, current, ominous); - self.world - .block_entities - .insert(key, BlockEntity::TrialSpawner(spawner)); - result?; - } - for key in keys { - self.client_tick_trial_spawner(key); - } - Ok(()) + .put(key, BlockEntity::TrialSpawner(spawner)); + result } /// The trial spawner block at `key`: its state and ominous flag. pub(super) fn trial_spawner_block(&self, key: Pos) -> Option<(TrialState, bool)> { diff --git a/crates/horizon-client/src/game/trial_visuals.rs b/crates/horizon-client/src/game/trial_visuals.rs index d32957fe1..cac28b5cc 100644 --- a/crates/horizon-client/src/game/trial_visuals.rs +++ b/crates/horizon-client/src/game/trial_visuals.rs @@ -237,7 +237,7 @@ impl Game<'_> { let shows = vault.display_item.is_some(); self.world .block_entities - .insert(key, BlockEntity::Vault(vault)); + .put(key, BlockEntity::Vault(vault)); let pos = [key.0, key.1, key.2]; if self.world.game_time % 20 == 0 { self.vault_connection_particles(pos); diff --git a/crates/horizon-client/src/game/trident.rs b/crates/horizon-client/src/game/trident.rs index 8c372fa46..b24f4540e 100644 --- a/crates/horizon-client/src/game/trident.rs +++ b/crates/horizon-client/src/game/trident.rs @@ -15,20 +15,48 @@ use horizon_core::{ }; pub(super) struct ThrownTrident { - stack: ItemStack, - position: Vec3, + /// The entity id a server tracks it by (0 until it is tracked). + pub id: u32, + pub(super) stack: ItemStack, + pub(super) position: Vec3, previous: Vec3, - velocity: Vec3, - yaw: f32, - pitch: f32, - in_ground: bool, + pub(super) velocity: Vec3, + pub(super) yaw: f32, + pub(super) pitch: f32, + pub(super) in_ground: bool, in_ground_time: u32, dealt_damage: bool, - loyalty: u32, - no_physics: bool, + pub(super) loyalty: u32, + pub(super) no_physics: bool, returning: u32, creative_only: bool, } +impl ThrownTrident { + /// A trident a server sent (`handleAddEntity`): placed and moved by it. + pub(super) fn remote(id: u32, position: Vec3, yaw: f32, pitch: f32) -> Self { + Self { + id, + stack: ItemStack::new("minecraft:trident", 1), + position, + previous: position, + velocity: Vec3::ZERO, + yaw, + pitch, + in_ground: false, + in_ground_time: 0, + dealt_damage: false, + loyalty: 0, + no_physics: false, + returning: 0, + creative_only: false, + } + } + /// One tick of a server's client: the previous position, then `step`. + pub(super) fn remote_step(&mut self, step: impl FnOnce(Vec3) -> Vec3) { + self.previous = self.position; + self.position = step(self.position); + } +} const HALF: f64 = 0.25; fn bounds(p: Vec3) -> Aabb { Aabb::new( @@ -70,7 +98,7 @@ impl Game<'_> { } let max = self .res - .items + .items() .get(&stack.item) .map_or(250, |i| i.max_damage); let riptide = stack.enchantment_level("minecraft:riptide") > 0; @@ -89,7 +117,7 @@ impl Game<'_> { p.position.y.floor() as i32, p.position.z.floor() as i32, ); - let water = self.res.facts.get(self.world.block(x, y, z)).fluid + let water = self.res.facts().get(self.world.block(x, y, z)).fluid == horizon_client::blocks::Fluid::Water; let sky = self.world.light(x, y + 1, z) >> 4 == 15; water || (self.world.weather.raining && sky) @@ -163,6 +191,7 @@ impl Game<'_> { on_ground, ); self.tridents.push(ThrownTrident { + id: 0, loyalty: thrown.enchantment_level("minecraft:loyalty"), stack: thrown, position: start, @@ -343,6 +372,7 @@ impl Game<'_> { return; }; let Some(uv) = self + .gui .atlas .sprites .get("entity/trident/trident") @@ -350,7 +380,26 @@ impl Game<'_> { else { return; }; + // AbstractArrow.shouldRenderAtSqrDistance: ten times the default. + let range = 10.0 + * super::entity_culling::default_range( + glam::DVec3::splat(2.0 * HALF), + self.settings.entity_distance, + ); for t in &self.tridents { + // EntityRenderer.shouldRender; ThrownTridentRenderer grows the box by 1.5. + if !self.standing_entity_in_view( + camera, + super::entity_culling::at(t.previous), + super::entity_culling::at(t.position), + alpha, + 2.0 * HALF, + 2.0 * HALF, + range, + 1.5, + ) { + continue; + } let p = glam::DVec3::new( t.previous.x + (t.position.x - t.previous.x) * alpha, t.previous.y + (t.position.y - t.previous.y) * alpha, diff --git a/crates/horizon-client/src/game/tweaks.rs b/crates/horizon-client/src/game/tweaks.rs index 018b6640b..681368cab 100644 --- a/crates/horizon-client/src/game/tweaks.rs +++ b/crates/horizon-client/src/game/tweaks.rs @@ -70,15 +70,7 @@ impl Game<'_> { /// One ordinary click on a game slot. False when nothing took it. pub(super) fn tweak_click(&mut self, slot: usize, button: i32, input: Input) -> bool { if self.remote.is_some() { - let ordinal = match input { - Input::Pickup => 0, - Input::QuickMove => 1, - Input::Swap => 2, - Input::Clone => 3, - Input::Throw => 4, - Input::QuickCraft => 5, - Input::PickupAll => 6, - }; + let ordinal = super::remote::input_ordinal(input); return self.remote_slot_click(slot, button as i8, ordinal); } if !self.menu_click(Some(slot), button, input) { @@ -331,7 +323,7 @@ impl Game<'_> { /// The sort buttons: a bordered square with three bars, lighter under /// the cursor. pub(super) fn draw_sort_buttons(&self, batch: &mut HudBatch, ox: f32, oy: f32) { - let atlas = &self.atlas; + let atlas = &self.gui.atlas; for ((x, y), _) in self.sort_buttons() { let (x, y) = (ox + x, oy + y); let (cx, cy) = self.cursor; diff --git a/crates/horizon-client/src/game/vault.rs b/crates/horizon-client/src/game/vault.rs index 31d7d3ea3..f3edfddb3 100644 --- a/crates/horizon-client/src/game/vault.rs +++ b/crates/horizon-client/src/game/vault.rs @@ -61,7 +61,7 @@ impl Game<'_> { .and_then(|s| VaultState::from_name(s)) .unwrap_or_default(); let ominous = block.properties.get("ominous").is_some_and(|v| v == "true"); - let Some(BlockEntity::Vault(vault)) = self.world.block_entities.remove(&key) else { + let Some(BlockEntity::Vault(vault)) = self.world.block_entities.take(&key) else { return None; }; Some((vault, current, ominous)) @@ -118,26 +118,20 @@ impl Game<'_> { self.rand_sequences = sequences; self.world .block_entities - .insert(key, BlockEntity::Vault(vault)); + .put(key, BlockEntity::Vault(vault)); Some(self.vault_events(key, events).map(|()| out)) } - /// Every vault's server tick, then its client tick. - pub(super) fn tick_vaults(&mut self) -> Result<()> { - let keys: Vec<Pos> = self - .world - .block_entities - .iter() - .filter(|(_, e)| matches!(e, BlockEntity::Vault(_))) - .map(|(k, _)| *k) - .collect(); - for key in keys { - let pos = [key.0, key.1, key.2]; - let ticked = self.with_vault(key, |v, l, random, state, ominous, roll, events| { - v.server_tick(l, random, pos, state, ominous, roll, events); - }); - if let Some(result) = ticked { - result?; - } + /// The server tick of the vault at `key`, then its client tick when there + /// is a window. + pub(super) fn tick_vault(&mut self, key: Pos) -> Result<()> { + let pos = [key.0, key.1, key.2]; + let ticked = self.with_vault(key, |v, l, random, state, ominous, roll, events| { + v.server_tick(l, random, pos, state, ominous, roll, events); + }); + if let Some(result) = ticked { + result?; + } + if self.options.dedicated.is_none() { self.client_tick_vault(key); } Ok(()) diff --git a/crates/horizon-client/src/game/villagers.rs b/crates/horizon-client/src/game/villagers.rs index 72fa2839a..5c6bed394 100644 --- a/crates/horizon-client/src/game/villagers.rs +++ b/crates/horizon-client/src/game/villagers.rs @@ -40,7 +40,7 @@ impl Game<'_> { .position(|m| m.id == id && m.species() == Species::Villager) } fn item_max_stack(&self, item: &str) -> u32 { - self.res.items.get(item).map_or(64, |i| i.max_stack) + self.res.items().get(item).map_or(64, |i| i.max_stack) } /// `BehaviorUtils.throwItem`: toss a stack from a villager's hand toward /// a point. @@ -57,7 +57,7 @@ impl Game<'_> { return; }; let now = self.world.game_time; - let items = &self.res.items; + let items = self.res.items(); let meeting = villager::meet(&mut self.mobs, ia, ib, now, |item| { items.get(item).map_or(64, |i| i.max_stack) }); @@ -121,13 +121,13 @@ impl Game<'_> { for _ in 0..=12 { y -= 1; let here = self.world.block(x, y, z); - let f = self.res.facts.get(here); - let above_f = self.res.facts.get(above); - let air_or_liquid = self.res.facts.is_air(above) + let f = self.res.facts().get(here); + let above_f = self.res.facts().get(above); + let air_or_liquid = self.res.facts().is_air(above) || above_f.fluid != horizon_client::blocks::Fluid::None; - let solid = self.res.mob_data.state(here).flags & horizon_client::mobs::data::SOLID - != 0 - || f.name.as_ref() == "minecraft:powder_snow"; + let solid = + self.res.mob_data().state(here).flags & horizon_client::mobs::data::SOLID != 0 + || f.name.as_ref() == "minecraft:powder_snow"; if golem_floor_ok(&f.name) && air_or_liquid && solid { found = Some(y + 1); break; @@ -137,7 +137,7 @@ impl Game<'_> { let Some(y) = found else { continue }; // IronGolem.checkSpawnRules: a floor and three free blocks. let free = |g: &Self, yy: i32| { - g.res.mob_data.state(g.world.block(x, yy, z)).flags + g.res.mob_data().state(g.world.block(x, yy, z)).flags & horizon_client::mobs::data::VALID_EMPTY_SPAWN != 0 }; @@ -260,7 +260,7 @@ impl Game<'_> { } let picks_up = self .res - .items + .items() .item_tags .get("minecraft:villager_picks_up") .cloned(); diff --git a/crates/horizon-client/src/game/wandering_trader.rs b/crates/horizon-client/src/game/wandering_trader.rs index b336cdf0b..87ca5753b 100644 --- a/crates/horizon-client/src/game/wandering_trader.rs +++ b/crates/horizon-client/src/game/wandering_trader.rs @@ -6,7 +6,7 @@ use horizon_client::{ mobs::{Species, leash::Holder, spawning, trader}, save::{read_dat, write_dat}, }; -use horizon_core::random::RandomSource; +use horizon_core::{geometry::Vec3, random::RandomSource}; use horizon_world::nbt::Tag; const TICK_DELAY: i32 = 1200; @@ -83,10 +83,7 @@ impl Game<'_> { } /// `WanderingTraderSpawner.tick`. pub(super) fn tick_wandering_trader(&mut self) { - if self.dim != Dim::Overworld - || self.player.is_none() - || !self.ui.rules.bool("spawn_wandering_traders") - { + if self.dim != Dim::Overworld || !self.ui.rules.bool("spawn_wandering_traders") { return; } self.load_trader_data(); @@ -110,9 +107,23 @@ impl Game<'_> { } /// `WanderingTraderSpawner.spawn`. fn spawn_trader(&mut self) -> bool { - let Some(p) = self.player.as_ref().map(|p| p.position) else { + // ServerLevel.getRandomPlayer: one of the living players, drawn + // from the level random. + let alive: Vec<Vec3> = self + .level_players() + .into_iter() + .filter(|p| p.alive) + .map(|p| p.position) + .collect(); + if alive.is_empty() { return true; - }; + } + let pick = self + .world + .random + .next_int_bound(alive.len() as i32) + .unwrap_or(0); + let p = alive[pick as usize]; if self.world.random.next_int_bound(10).unwrap_or(1) != 0 { return false; } @@ -126,14 +137,14 @@ impl Game<'_> { } let biome = self.world.chunks.get(&(pos[0] >> 4, pos[2] >> 4)).map(|c| { self.res - .biomes + .biomes() .get(c.biome(pos[0], pos[1], pos[2])) .name .clone() }); if biome.is_some_and(|b| { self.res - .mob_data + .mob_data() .biome_has("minecraft:without_wandering_trader_spawns", &b) }) { return false; @@ -214,7 +225,7 @@ impl Game<'_> { (0..=1).all(|dz| { !self .res - .facts + .facts() .get(self.world.block(pos[0] + dx, pos[1] + dy, pos[2] + dz)) .full_collision }) @@ -230,7 +241,7 @@ impl Game<'_> { continue; } for y in (from[1] - range).max(horizon_client::chunk::MIN_Y)..=from[1] + range { - if &*self.res.facts.get(self.world.block(x, y, z)).name != "minecraft:bell" { + if &*self.res.facts().get(self.world.block(x, y, z)).name != "minecraft:bell" { continue; } let d = (x - from[0]).pow(2) + (y - from[1]).pow(2) + (z - from[2]).pow(2); diff --git a/crates/horizon-client/src/game/wind.rs b/crates/horizon-client/src/game/wind.rs index 07759944f..355b5b877 100644 --- a/crates/horizon-client/src/game/wind.rs +++ b/crates/horizon-client/src/game/wind.rs @@ -141,7 +141,7 @@ impl Game<'_> { fn wind_positions(&mut self, center: [f64; 3], radius: f32) -> Vec<[i32; 3]> { let stops = self .res - .items + .items() .block_tags .get("minecraft:blocks_wind_charge_explosions") .cloned() @@ -176,7 +176,7 @@ impl Game<'_> { } let name = &self .res - .facts + .facts() .get(self.world.block(pos[0], pos[1], pos[2])) .name; if stops.contains(&**name) { @@ -402,7 +402,7 @@ impl Game<'_> { /// buttons, levers and bells react as if used; candles go out. fn wind_trigger_block(&mut self, pos: [i32; 3]) -> Result<()> { let state = self.world.block(pos[0], pos[1], pos[2]); - let name = self.res.facts.get(state).name.clone(); + let name = self.res.facts().get(state).name.clone(); let prop = |game: &Self, key: &str| { game.pack .assets @@ -478,6 +478,7 @@ impl Game<'_> { }); let Some(model) = model else { return }; let Some(r) = self + .gui .atlas .sprites .get("entity/projectiles/wind_charge") diff --git a/crates/horizon-client/src/game/wither.rs b/crates/horizon-client/src/game/wither.rs index dfd215814..7b86dc527 100644 --- a/crates/horizon-client/src/game/wither.rs +++ b/crates/horizon-client/src/game/wither.rs @@ -10,14 +10,22 @@ use horizon_client::{ impl Game<'_> { fn is_wither_skull(&self, p: [i32; 3]) -> bool { - let name = &*self.res.facts.get(self.world.block(p[0], p[1], p[2])).name; + let name = &*self + .res + .facts() + .get(self.world.block(p[0], p[1], p[2])) + .name; matches!( name, "minecraft:wither_skeleton_skull" | "minecraft:wither_skeleton_wall_skull" ) } fn is_soul_base(&self, p: [i32; 3]) -> bool { - let name = &*self.res.facts.get(self.world.block(p[0], p[1], p[2])).name; + let name = &*self + .res + .facts() + .get(self.world.block(p[0], p[1], p[2])) + .name; matches!(name, "minecraft:soul_sand" | "minecraft:soul_soil") } /// After a wither skeleton skull is placed: the T of soul sand (or soul @@ -36,7 +44,7 @@ impl Game<'_> { && self.is_soul_base(at(1, 2)) && [0, 2].iter().all(|&c| { let p = at(c, 2); - self.res.facts.is_air(self.world.block(p[0], p[1], p[2])) + self.res.facts().is_air(self.world.block(p[0], p[1], p[2])) }); if !ok { continue; diff --git a/crates/horizon-client/src/gamepad.rs b/crates/horizon-client/src/gamepad.rs index a250f5e8c..220f74a5b 100644 --- a/crates/horizon-client/src/gamepad.rs +++ b/crates/horizon-client/src/gamepad.rs @@ -1009,6 +1009,7 @@ pub enum TriggerFeel { /// Mining a block of this hardness: a buzz, stronger and slower on harder blocks. Texture(f32), } +#[cfg(feature = "controllers")] impl TriggerFeel { /// The effect, for a trigger that presses its action at `threshold` (0..1) of its travel. pub fn effect(self, threshold: f32) -> horizon_dualsense::TriggerEffect { @@ -1044,6 +1045,7 @@ impl TriggerFeel { } } } +#[cfg(feature = "controllers")] impl PadSettings { /// The left and right triggers' effects: each feels the action bound to it, `key.use` or /// `key.attack`. @@ -1404,6 +1406,7 @@ mod tests { assert_eq!(w.observe(Some(1.0)), Some(1.0)); } + #[cfg(feature = "controllers")] #[test] fn triggers_feel_their_action() { use horizon_dualsense::{TRIGGER_OFF, trigger}; diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 7391ad8b9..31a1c9e72 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -4,6 +4,7 @@ use crate::{ Result, environment::Sky, + graphics::GraphicsApi, hud::{HudBatch, HudRenderer}, mesher::{SectionMesh, TerrainVertex, Vertex}, sprites::SpriteAnimation, @@ -24,7 +25,7 @@ const CRUMBLING_BLEND: wgpu::BlendState = wgpu::BlendState { }; /// `TerrainVertex` for `vs_terrain`: position, UV, layer, light, colour. const TERRAIN_ATTRIBUTES: [wgpu::VertexAttribute; 5] = - wgpu::vertex_attr_array![0=>Float32x3,1=>Float32x2,2=>Uint16,4=>Uint8x2,3=>Unorm8x4]; + wgpu::vertex_attr_array![0=>Float32x3,1=>Uint16x2,2=>Uint16,4=>Uint8x2,3=>Unorm8x4]; /// entity.wgsl after the OverlayTexture constants it reads. fn entity_shader() -> String { @@ -58,7 +59,10 @@ pub fn outline_vertices(edges: &[[f64; 6]], camera: glam::DVec3) -> Vec<[f32; 6] } mod arena; +mod cull; +mod flat; mod java_stages; +mod lod; mod occlusion; mod pack_stages; pub mod panorama; @@ -68,6 +72,22 @@ 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); + NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed) +} + #[repr(C)] #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] struct Globals { @@ -101,9 +121,17 @@ struct SectionGpu { faces: crate::mesher::FaceGroups, sort: translucency::SortState, sorted_indices: Option<(u32, u64)>, + /// Its id for flat draws (`flat::Flat::add`), when they are on. + flat_id: Option<u32>, } /// Line segments the hit outline can hold. const MAX_OUTLINE_EDGES: u64 = 256; +/// Staging buffer size for section meshes: a frame of uploads while +/// exploring fits in a few. +const SECTION_UPLOAD_CHUNK: u64 = 4 << 20; +/// Section copies are submitted early past this many staged bytes, which +/// bounds the staging memory frames that are not drawn can hold. +const SECTION_COPY_LIMIT: u64 = 64 << 20; pub struct Frame<'a> { pub camera: DVec3, pub yaw: f32, @@ -115,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, @@ -234,6 +264,9 @@ pub struct Renderer { offscreen: Option<wgpu::Texture>, /// Offscreen submissions still on the GPU: at most two frames run ahead. in_flight: std::collections::VecDeque<wgpu::SubmissionIndex>, + /// Which steps of the first frame on this device still go to the log + /// (see `startup_step`). + first_frame: FirstFrame, gpu_timer: Option<timing::GpuTimer>, /// GPU frame times (ms) read back since the caller last drained them. pub gpu_times: Vec<f32>, @@ -251,6 +284,13 @@ pub struct Renderer { pub config: wgpu::SurfaceConfiguration, solid: wgpu::RenderPipeline, translucent: wgpu::RenderPipeline, + /// Flat section draws (`flat`), where multi-draw is emulated. + flat: Option<flat::Flat>, + /// `flat` draws this frame's sections (off: the per-section draws). + flat_on: bool, + /// Occlusion culling of solid section draws, where multi-draw is native. + cull: Option<cull::Cull>, + cull_on: bool, sky: wgpu::RenderPipeline, dark_disc: wgpu::RenderPipeline, lines: wgpu::RenderPipeline, @@ -312,6 +352,13 @@ pub struct Renderer { lightmap_sampler: wgpu::Sampler, animations: AnimatedSprites, uploads: upload::Uploads, + /// Section meshes are staged through their own belt into + /// `section_copies`, which the next frame records its commands after: + /// `Queue::write_buffer` would make a staging buffer per write. + section_uploads: upload::Uploads, + section_copies: Option<wgpu::CommandEncoder>, + /// Bytes staged in `section_copies`. + section_copy_bytes: u64, /// Whether `lightmap` holds `lightmap_pixels` yet. lightmap_uploaded: bool, sprite_minimum_size: u32, @@ -322,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, @@ -333,14 +382,22 @@ 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. draws: Vec<[u32; 5]>, /// Vertex arena page and optional sorted index arena page of each draw. draw_pages: Vec<(u32, Option<u32>)>, + /// Solid draws left to occlusion culling's phase 2, while the list is + /// built, and their pages. + phase2_draws: Vec<[u32; 5]>, + phase2_pages: Vec<(u32, Option<u32>)>, /// Block-entity blocks per meshed section (world position, state id). block_entities: HashMap<SectionKey, Vec<([i32; 3], u32)>>, + /// Changes whenever `block_entities` does (unique across renderers). + block_entity_revision: u64, /// VisGraph face connectivity per meshed section (cave culling). occlusion: occlusion::Occlusion, /// Resolution scale and FXAA (Horizon settings). @@ -355,6 +412,10 @@ pub struct Renderer { pub info: wgpu::AdapterInfo, pub suspended: bool, pub drawn_sections: usize, + /// Last frame's section draws (face ranges and translucent sections), + /// and the draw calls the backend issued for them. + pub section_draws: usize, + pub section_draw_calls: usize, pub uploaded_bytes: u64, started: Instant, /// Write the next presented frame to this PNG path (control socket). @@ -385,7 +446,7 @@ fn sprite_split() -> u32 { .unwrap_or(SPRITE_SPLIT) } /// Sprite arrays terrain.wgsl binds (`sprites`, `sprites_1`...). -const SPRITE_ARRAYS: u32 = 4; +const SPRITE_ARRAYS: u32 = 8; /// The views of terrain.wgsl's sprite arrays, the unused ones a 1-layer /// stand-in. fn sprite_views( @@ -441,6 +502,10 @@ fn terrain_group( view(5, &sprites[1]), view(6, &sprites[2]), view(7, &sprites[3]), + view(8, &sprites[4]), + view(9, &sprites[5]), + view(10, &sprites[6]), + view(11, &sprites[7]), ], }) } @@ -492,28 +557,210 @@ fn mip_chain( /// bounds by construction: index counts within `indices`, vertices within /// the section's arena range, first instances below the origins written that /// frame. Debug builds keep validating them; `WGPU_VALIDATION_INDIRECT_CALL=1` -/// turns it back on. -fn instance_descriptor() -> wgpu::InstanceDescriptor { +/// turns it back on. DirectX 12 keeps it too: there wgpu-core's validation +/// pass is also what passes each draw's first instance to shaders reading +/// `instance_index`, and DirectX 12 is the fallback, where working matters +/// more than CPU time. +fn instance_descriptor(backends: wgpu::Backends) -> wgpu::InstanceDescriptor { let mut descriptor = wgpu::InstanceDescriptor::new_without_display_handle(); - if !cfg!(debug_assertions) { + descriptor.backends = backends.with_env(); + let dx12 = cfg!(windows) && descriptor.backends.contains(wgpu::Backends::DX12); + if !cfg!(debug_assertions) && !dx12 { descriptor .flags .remove(wgpu::InstanceFlags::VALIDATION_INDIRECT_CALL); } descriptor.with_env() } +/// The backends to open a device on, each tried in turn until one gives a +/// device. `api` is the player's choice (`GraphicsApi::with_env` applied); +/// `wgpu_backend`: whether `WGPU_BACKEND` is set, which picks the backends +/// itself. Only Windows has two: Auto tries Vulkan, then DirectX 12 when +/// Vulkan has no adapter (no Vulkan driver) or no device. A native crash in +/// the driver can't be caught in the game; the launcher switches the setting +/// to DirectX 12 after one at the renderer's start (#746). +fn backend_order(api: GraphicsApi, windows: bool, wgpu_backend: bool) -> Vec<wgpu::Backends> { + if !windows || wgpu_backend { + return vec![wgpu::Backends::default()]; + } + match api { + GraphicsApi::Auto => vec![wgpu::Backends::VULKAN, wgpu::Backends::DX12], + GraphicsApi::Vulkan => vec![wgpu::Backends::VULKAN], + GraphicsApi::Dx12 => vec![wgpu::Backends::DX12], + } +} +fn backends_name(backends: wgpu::Backends) -> &'static str { + match backends { + wgpu::Backends::VULKAN => "Vulkan", + wgpu::Backends::DX12 => "DirectX 12", + _ => "graphics", + } +} +/// A device and what it came from. +struct Opened { + instance: wgpu::Instance, + surface: Option<wgpu::Surface<'static>>, + adapter: wgpu::Adapter, + device: wgpu::Device, + queue: wgpu::Queue, +} +/// Logs the adapter and records it for telemetry and crash reports. +fn describe_adapter(adapter: &wgpu::Adapter) { + let info = adapter.get_info(); + log::info!( + "Horizon renderer: {} ({:?}, {:?})", + info.name, + info.backend, + info.device_type + ); + crate::telemetry::set_gpu( + &info.name, + format!("{} {}", info.driver, info.driver_info) + .trim() + .to_owned(), + format!("{:?}", info.backend), + ); + crate::crash::set_detail( + "Graphics Adapter", + format!( + "{} ({:?}, {:?}), driver {} {}", + info.name, info.backend, info.device_type, info.driver, info.driver_info + ), + ); +} +/// Whether an adapter's texture arrays hold the block textures Horizon puts +/// in one array. +fn check_layers(limits: &wgpu::Limits, layers: u32) -> Result<()> { + let needed = layers.min(sprite_split()); + if limits.max_texture_array_layers < needed { + return Err(format!( + "GPU supports {} texture layers; {needed} required", + limits.max_texture_array_layers + ) + .into()); + } + Ok(()) +} +/// Opens a device on the first of `backend_order`'s backends that gives one. +/// Offscreen rendering needs no surface, so it can use a GPU the window's +/// display cannot present from (a headless compositor). +async fn open_device( + window: &Arc<Window>, + offscreen: bool, + api: GraphicsApi, + layers: u32, +) -> Result<Opened> { + let order = backend_order( + api.with_env(), + cfg!(windows), + std::env::var_os("WGPU_BACKEND").is_some(), + ); + let mut failures = Vec::new(); + for (attempt, backends) in order.iter().copied().enumerate() { + let name = backends_name(backends); + if attempt > 0 { + log::warn!("Horizon renderer: trying {name} ({})", failures.join("; ")); + } + let instance = wgpu::Instance::new(instance_descriptor(backends)); + let surface = if offscreen { + None + } else { + Some(instance.create_surface(window.clone())?) + }; + let adapter = match instance + .request_adapter(&wgpu::RequestAdapterOptions { + power_preference: wgpu::PowerPreference::HighPerformance, + compatible_surface: surface.as_ref(), + force_fallback_adapter: false, + ..Default::default() + }) + .await + { + Ok(adapter) => adapter, + Err(e) => { + failures.push(format!("{name}: {e}")); + continue; + } + }; + describe_adapter(&adapter); + let info = adapter.get_info(); + let limits = adapter.limits(); + if let Err(e) = check_layers(&limits, layers) { + failures.push(format!("{name}: {e}")); + continue; + } + // Mappable primary buffers: section pages that no longer fit in + // video memory go to system memory. + let features = timing::GpuTimer::features(&adapter) + | cull::Cull::features(&adapter) + | (adapter.features() + & (wgpu::Features::INDIRECT_FIRST_INSTANCE + | wgpu::Features::MAPPABLE_PRIMARY_BUFFERS)); + let driver = format!("{} {}", info.driver, info.driver_info); + startup_step(format_args!( + "requesting the device (driver {}) with features [{features}] and the adapter's \ + limits (buffers up to {} bytes, storage bindings up to {} bytes, {} texture layers)", + match driver.trim() { + "" => "unnamed", + driver => driver, + }, + limits.max_buffer_size, + limits.max_storage_buffer_binding_size, + limits.max_texture_array_layers, + )); + match adapter + .request_device(&wgpu::DeviceDescriptor { + label: Some("Horizon device"), + required_features: features, + required_limits: limits, + ..Default::default() + }) + .await + { + Ok((device, queue)) => { + return Ok(Opened { + instance, + surface, + adapter, + device, + queue, + }); + } + Err(e) => failures.push(format!("{name} device: {e}")), + } + } + Err(format!("{NO_GPU_DRIVER} ({})", failures.join("; ")).into()) +} /// What to tell a player whose system offers no usable graphics adapter. /// Vulkan is loaded at run time (the game links no graphics library), so a /// missing driver shows here rather than as a missing-DLL dialog. #[cfg(target_os = "windows")] -const NO_GPU_DRIVER: &str = "No Vulkan graphics driver found: Horizon needs a GPU driver with Vulkan \ - (vulkan-1.dll comes with it). Install the latest driver for your NVIDIA, AMD or Intel GPU \ - and start the game again"; +const NO_GPU_DRIVER: &str = "No graphics driver with Vulkan or DirectX 12 found: Horizon needs a GPU \ + driver with one of them. Install the latest driver for your NVIDIA, AMD or Intel GPU and \ + start the game again"; #[cfg(target_os = "macos")] const NO_GPU_DRIVER: &str = "No Metal graphics device found"; #[cfg(not(any(target_os = "windows", target_os = "macos")))] const NO_GPU_DRIVER: &str = "No Vulkan graphics driver found: Horizon needs the Vulkan loader \ (libvulkan.so.1) and a Vulkan driver for your GPU (Mesa's, or NVIDIA's)"; +/// Logs a step of the renderer's start, for a native crash in the graphics +/// driver: it leaves no panic or crash report, so the log's last line is the +/// only sign of where the game stopped. The log file is written unbuffered; +/// the flush is for loggers that buffer. +fn startup_step(step: std::fmt::Arguments) { + log::info!("Horizon renderer: {step}"); + log::logger().flush(); +} +/// How far the first frame on a new device got, for `startup_step`. +#[derive(Clone, Copy, PartialEq, Eq)] +enum FirstFrame { + Acquire, + Submit, + Drawn, +} +fn on_off(on: bool) -> &'static str { + if on { "on" } else { "off" } +} impl Renderer { /// Sections with GPU buffers, for soak tests watching residency. pub fn resident_sections(&self) -> usize { @@ -526,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] @@ -545,54 +793,8 @@ impl Renderer { forcefield: (&[u8], u32), panorama_faces: &panorama::Faces, previous: Option<&Self>, + api: GraphicsApi, ) -> Result<Self> { - // Reload resources on the same device; replacing live Vulkan devices - // can invalidate the driver's swapchain state on the same window. - let instance = previous.map_or_else( - || wgpu::Instance::new(instance_descriptor()), - |renderer| renderer.instance.clone(), - ); - // Offscreen rendering needs no surface, so it can use a GPU the - // window's display cannot present from (a headless compositor). - let surface = if offscreen { - None - } else { - Some(instance.create_surface(window.clone())?) - }; - let adapter = match previous { - Some(renderer) => renderer.adapter.clone(), - None => instance - .request_adapter(&wgpu::RequestAdapterOptions { - power_preference: wgpu::PowerPreference::HighPerformance, - compatible_surface: surface.as_ref(), - force_fallback_adapter: false, - ..Default::default() - }) - .await - .map_err(|e| format!("{NO_GPU_DRIVER} ({e})"))?, - }; - let info = adapter.get_info(); - log::info!( - "Horizon renderer: {} ({:?}, {:?})", - info.name, - info.backend, - info.device_type - ); - crate::telemetry::set_gpu( - &info.name, - format!("{} {}", info.driver, info.driver_info) - .trim() - .to_owned(), - format!("{:?}", info.backend), - ); - crate::crash::set_detail( - "Graphics Adapter", - format!( - "{} ({:?}, {:?}), driver {} {}", - info.name, info.backend, info.device_type, info.driver, info.driver_info - ), - ); - let limits = adapter.limits(); let split = sprite_split(); if layers > split * SPRITE_ARRAYS { return Err(format!( @@ -601,28 +803,34 @@ impl Renderer { ) .into()); } - if limits.max_texture_array_layers < layers.min(split) { - return Err(format!( - "GPU supports {} texture layers; {} required", - limits.max_texture_array_layers, - layers.min(split) - ) - .into()); - } - let (device, queue) = match previous { - Some(renderer) => (renderer.device.clone(), renderer.queue.clone()), - None => { - adapter - .request_device(&wgpu::DeviceDescriptor { - label: Some("Horizon device"), - required_features: timing::GpuTimer::features(&adapter) - | (adapter.features() & wgpu::Features::INDIRECT_FIRST_INSTANCE), - required_limits: limits.clone(), - ..Default::default() - }) - .await? + let Opened { + instance, + surface, + adapter, + device, + queue, + } = match previous { + // Reload resources on the same device; replacing live Vulkan + // devices can invalidate the driver's swapchain state on the same + // window. + Some(renderer) => { + describe_adapter(&renderer.adapter); + check_layers(&renderer.adapter.limits(), layers)?; + Opened { + surface: if offscreen { + None + } else { + Some(renderer.instance.create_surface(window.clone())?) + }, + instance: renderer.instance.clone(), + adapter: renderer.adapter.clone(), + device: renderer.device.clone(), + queue: renderer.queue.clone(), + } } + None => open_device(&window, offscreen, api, layers).await?, }; + let info = adapter.get_info(); let size = window.inner_size(); let (config, present_modes) = if let Some(surface) = &surface { let mut config = surface @@ -651,6 +859,12 @@ impl Renderer { wgpu::PresentMode::Fifo } }); + if previous.is_none() { + startup_step(format_args!( + "device ready; configuring the surface ({}x{} {:?}, {:?})", + config.width, config.height, config.format, config.present_mode + )); + } surface.configure(&device, &config); (config, caps.present_modes.clone()) } else { @@ -671,6 +885,9 @@ impl Renderer { let offscreen = surface .is_none() .then(|| Self::offscreen_target(&device, &config)); + if previous.is_none() { + startup_step(format_args!("creating the pipelines")); + } let gpu_timer = timing::GpuTimer::new(&device, &queue); let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("Horizon terrain"), @@ -739,6 +956,10 @@ impl Renderer { entry(5, vf, sprite_array), entry(6, vf, sprite_array), entry(7, vf, sprite_array), + entry(8, vf, sprite_array), + entry(9, vf, sprite_array), + entry(10, vf, sprite_array), + entry(11, vf, sprite_array), ], }); let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { @@ -773,18 +994,19 @@ impl Renderer { step_mode: wgpu::VertexStepMode::Instance, attributes: &debug_line_attributes, })]; - let pipeline = |label: &str, - vertex: &str, - fragment: &str, - buffers: &[Option<wgpu::VertexBufferLayout<'_>>], - topology: wgpu::PrimitiveTopology, - cull: Option<wgpu::Face>, - depth_write: bool, - depth_test: bool, - blend: Option<wgpu::BlendState>| { + let pipeline_in = |layout: &wgpu::PipelineLayout, + label: &str, + vertex: &str, + fragment: &str, + buffers: &[Option<wgpu::VertexBufferLayout<'_>>], + topology: wgpu::PrimitiveTopology, + cull: Option<wgpu::Face>, + depth_write: bool, + depth_test: bool, + blend: Option<wgpu::BlendState>| { device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: Some(label), - layout: Some(&layout), + layout: Some(layout), vertex: wgpu::VertexState { module: &shader, entry_point: Some(vertex), @@ -825,6 +1047,28 @@ impl Renderer { cache: None, }) }; + let pipeline = |label: &str, + vertex: &str, + fragment: &str, + buffers: &[Option<wgpu::VertexBufferLayout<'_>>], + topology: wgpu::PrimitiveTopology, + cull: Option<wgpu::Face>, + depth_write: bool, + depth_test: bool, + blend: Option<wgpu::BlendState>| { + pipeline_in( + &layout, + label, + vertex, + fragment, + buffers, + topology, + cull, + depth_write, + depth_test, + blend, + ) + }; let tri = wgpu::PrimitiveTopology::TriangleList; let solid = pipeline( "Terrain solid", @@ -1033,7 +1277,9 @@ impl Renderer { let instances = device.create_buffer(&wgpu::BufferDescriptor { label: Some("Section origins"), size: instance_capacity * 16, - usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, + usage: wgpu::BufferUsages::VERTEX + | wgpu::BufferUsages::COPY_DST + | wgpu::BufferUsages::STORAGE, mapped_at_creation: false, }); let outline = device.create_buffer(&wgpu::BufferDescriptor { @@ -1641,7 +1887,34 @@ impl Renderer { std::mem::size_of::<TerrainVertex>() as u64, 1 << 20, ); - let sorted_indices = arena::Arena::indices(&device, 1 << 16); + // Sections as one index list per frame where multi-draw is emulated. + let flat = flat::wanted(&adapter, &device).then(|| { + let buffers = [Some(wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::<TerrainVertex>() as u64, + step_mode: wgpu::VertexStepMode::Vertex, + attributes: &TERRAIN_ATTRIBUTES, + })]; + flat::Flat::new( + &device, + &bindings, + arena.page_vertices(), + |layout, label, fragment, blend| { + pipeline_in( + layout, + label, + "vs_terrain_flat", + fragment, + &buffers, + tri, + Some(wgpu::Face::Back), + true, + true, + blend, + ) + }, + ) + }); + let sorted_indices = arena::Arena::indices(&device, 1 << 16, flat.is_some()); let indirect = device.create_buffer(&wgpu::BufferDescriptor { label: Some("Section draws"), size: 1024 * 20, @@ -1651,20 +1924,43 @@ impl Renderer { let multi_draw = device .features() .contains(wgpu::Features::INDIRECT_FIRST_INSTANCE); + // HORIZON_GPU_CULL=0 turns occlusion culling off, `all` culls every + // frame however little it hides (A/B checks). + let cull_env = std::env::var("HORIZON_GPU_CULL"); + let cull = (cull_env.as_deref() != Ok("0") && cull::Cull::supported(&adapter, &device)) + .then(|| { + let mut cull = cull::Cull::new(&device, config.format.remove_srgb_suffix()); + cull.always = cull_env.as_deref() == Ok("all"); + cull + }); let world_post = world_post::Post::new(&device, config.format.remove_srgb_suffix()); let uploads = upload::Uploads::new(&device); + let section_uploads = upload::Uploads::with_chunk(&device, SECTION_UPLOAD_CHUNK); let panorama = panorama::Panorama::new( &device, &queue, config.format.remove_srgb_suffix(), panorama_faces, ); + if previous.is_none() { + startup_step(format_args!( + "pipelines ready (multi-draw {}, occlusion culling {}, GPU timer {})", + on_off(multi_draw), + on_off(cull.is_some()), + on_off(gpu_timer.is_some()), + )); + } Ok(Self { instance, adapter, surface, offscreen, in_flight: std::collections::VecDeque::new(), + first_frame: if previous.is_none() { + FirstFrame::Acquire + } else { + FirstFrame::Drawn + }, gpu_timer, gpu_times: Vec::new(), lightmap_pixels: [0; 16 * 16 * 4], @@ -1676,6 +1972,10 @@ impl Renderer { config, solid, translucent, + flat_on: flat.is_some(), + cull_on: cull.is_some(), + cull, + flat, sky, dark_disc, lines, @@ -1725,6 +2025,9 @@ impl Renderer { lightmap_sampler, animations: AnimatedSprites::new(animations, sprite_size, sprite_mips), uploads, + section_uploads, + section_copies: None, + section_copy_bytes: 0, lightmap_uploaded: false, sprite_minimum_size, indices, @@ -1734,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(), @@ -1742,10 +2047,15 @@ 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(), + phase2_draws: Vec::new(), + phase2_pages: Vec::new(), block_entities: HashMap::new(), + block_entity_revision: next_block_entity_revision(), occlusion: occlusion::Occlusion::new(crate::chunk::HEIGHT / 16), world_post, pack: None, @@ -1755,6 +2065,8 @@ impl Renderer { info, suspended: size.width == 0 || size.height == 0, drawn_sections: 0, + section_draws: 0, + section_draw_calls: 0, uploaded_bytes: 0, started: Instant::now(), screenshot: None, @@ -1835,30 +2147,12 @@ impl Renderer { }) .create_view(&Default::default()) } - /// VSync on: tear-free presentation (Mailbox, else Fifo); off: Immediate when available. - /// VSync, unless Horizon's present-mode setting names a supported mode. + /// VSync on presents with Fifo, capped to the display's refresh rate in a + /// window and in fullscreen alike; Horizon's present-mode setting applies + /// only with VSync off (`window::chosen_present_mode`). pub fn set_present_mode(&mut self, vsync: bool, mode: crate::graphics::PresentMode) { use crate::graphics::PresentMode as P; - let wanted = match mode { - P::Auto => None, - P::Fifo => Some(wgpu::PresentMode::Fifo), - P::Mailbox => Some(wgpu::PresentMode::Mailbox), - P::Immediate => Some(wgpu::PresentMode::Immediate), - }; - match wanted.filter(|m| self.present_modes.contains(m)) { - Some(m) => { - if self.config.present_mode != m - && let Some(surface) = &self.surface - { - self.config.present_mode = m; - surface.configure(&self.device, &self.config); - } - } - None => self.set_vsync(vsync), - } - } - pub fn set_vsync(&mut self, vsync: bool) { - use crate::window::{PresentMode, vsync_present_mode}; + use crate::window::{PresentMode, chosen_present_mode}; let supported: Vec<PresentMode> = self .present_modes .iter() @@ -1869,8 +2163,13 @@ impl Renderer { _ => None, }) .collect(); - // GpuSurface.PresentMode.getSupportedVsyncMode. - let mode = match vsync_present_mode(&supported, vsync) { + let setting = match mode { + P::Auto => None, + P::Fifo => Some(PresentMode::Fifo), + P::Mailbox => Some(PresentMode::Mailbox), + P::Immediate => Some(PresentMode::Immediate), + }; + let mode = match chosen_present_mode(&supported, vsync, setting) { Some(PresentMode::Mailbox) => wgpu::PresentMode::Mailbox, Some(PresentMode::Immediate) => wgpu::PresentMode::Immediate, _ => wgpu::PresentMode::Fifo, @@ -1896,24 +2195,79 @@ impl Renderer { } self.depth = Self::depth(&self.device, &self.config); } + /// Whether sections are drawn as one index list per frame (`flat`). + pub fn flat_draws(&self) -> bool { + self.flat_on + } + /// Switches between flat and per-section draws, for A/B checks. + pub fn set_flat_draws(&mut self, on: bool) -> Result<()> { + if on && self.flat.is_none() { + return Err("flat section draws are off on this device".into()); + } + self.flat_on = on; + Ok(()) + } + /// Whether solid sections are occlusion culled (`cull`). + pub fn section_cull(&self) -> bool { + self.cull_on + } + /// Switches occlusion culling, for A/B checks. + pub fn set_section_cull(&mut self, on: bool) -> Result<()> { + if on && self.cull.is_none() { + return Err("occlusion culling is off on this device".into()); + } + self.cull_on = on; + Ok(()) + } + /// Of the boxes occlusion culling's last test read back drew, how many + /// it did not see and how many it drew, and the share of solid indices + /// it hid. + pub fn culled_sections(&self) -> Option<([usize; 2], f32)> { + self.cull + .as_ref() + .filter(|_| self.cull_on) + .map(|c| c.hidden) + } pub fn section_count(&self) -> usize { self.sections.len() } pub fn has_section(&self, key: SectionKey) -> bool { self.sections.contains(key) } + /// Bring the cave culling graph up to date for `camera` without waiting: + /// a search this starts runs on its worker while the frame is prepared, + /// and `render` takes it if it has finished. + pub fn start_occlusion(&mut self, camera: DVec3, distance: f32) { + self.occlusion + .update(camera_section(camera), (distance / 16.0) as i32 + 2); + } pub fn upload(&mut self, key: SectionKey, mesh: &SectionMesh) { self.occlusion.set(key, mesh.visibility); - if mesh.block_entities.is_empty() { - self.block_entities.remove(&key); + let changed = if mesh.block_entities.is_empty() { + self.block_entities.remove(&key).is_some() } else { - self.block_entities.insert(key, mesh.block_entities.clone()); + let old = self.block_entities.insert(key, mesh.block_entities.clone()); + old.as_deref() != Some(&mesh.block_entities[..]) + }; + 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 { @@ -1921,48 +2275,93 @@ impl Renderer { self.indices = Self::quad_indices(&self.device, self.index_quads); } let vertices = (mesh.solid.len() + mesh.translucent.len()) as u64; - let (page, start) = self.arena.alloc(&self.device, &self.queue, vertices); - let sorted_indices = (translucent > 0).then(|| { - self.sorted_indices - .alloc(&self.device, &self.queue, u64::from(translucent) * 6) + let encoder = self.section_copies.get_or_insert_with(|| { + self.device + .create_command_encoder(&wgpu::CommandEncoderDescriptor { + label: Some("Section uploads"), + }) + }); + // With video and system memory both full, the section is not drawn. + 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 None; + } + indices + } else { + None + }; + let flat_id = self.flat.as_mut().map(|f| { + f.add( + &self.device, + encoder, + &mut self.section_uploads, + page, + start, + vertices, + ) }); let stride = std::mem::size_of::<TerrainVertex>() as u64; for (offset, part) in [(0, &mesh.solid), (mesh.solid.len(), &mesh.translucent)] { if !part.is_empty() { let bytes: &[u8] = bytemuck::cast_slice(part); self.uploaded_bytes += bytes.len() as u64; - self.queue.write_buffer( + self.section_copy_bytes += bytes.len() as u64; + self.section_uploads.buffer( + encoder, self.arena.buffer(page), (start + offset as u64) * stride, bytes, ); } } - 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, - }, - ); + if self.section_copy_bytes >= SECTION_COPY_LIMIT + && let Some(copies) = self.take_section_copies() + { + self.queue.submit([copies]); + } + 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> { + let encoder = self.section_copies.take()?; + self.section_copy_bytes = 0; + self.section_uploads.finish(&encoder); + Some(encoder.finish()) } /// Draws `count` section draws from `first` with the arena and section /// origins bound: one indirect multi-draw, or a loop without it. /// Consecutive draws in one arena page share a vertex buffer binding, /// keeping the list's order (translucent sections go back to front). - fn draw_sections(&self, pass: &mut wgpu::RenderPass<'_>, first: u32, count: u32) { + /// `indirect` holds the list's draws from `base`. + fn draw_sections( + &self, + pass: &mut wgpu::RenderPass<'_>, + indirect: &wgpu::Buffer, + base: u32, + first: u32, + count: u32, + ) { let (mut i, end) = (first as usize, (first + count) as usize); while i < end { let (page, index_page) = self.draw_pages[i]; @@ -1974,7 +2373,8 @@ impl Renderer { let indices = index_page.map_or(&self.indices, |p| self.sorted_indices.buffer(p)); pass.set_index_buffer(indices.slice(..), wgpu::IndexFormat::Uint32); if self.multi_draw { - pass.multi_draw_indexed_indirect(&self.indirect, i as u64 * 20, run as u32); + let offset = (i - base as usize) as u64 * 20; + pass.multi_draw_indexed_indirect(indirect, offset, run as u32); } else { for d in &self.draws[i..i + run] { pass.draw_indexed(d[2]..d[2] + d[0], d[3] as i32, d[4]..d[4] + 1); @@ -1985,28 +2385,58 @@ impl Renderer { } fn release_section(&mut self, key: SectionKey) { if let Some(s) = self.sections.remove(key) { - 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() } + /// A value that changes whenever `block_entities` gives something else. + pub fn block_entity_revision(&self) -> u64 { + self.block_entity_revision + } pub fn clear_sections(&mut self) { 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(); + } self.sorted_indices.clear(); 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) { - self.block_entities.remove(&key); + 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(); + } self.occlusion.remove(key); self.release_section(key); } @@ -2181,10 +2611,28 @@ impl Renderer { ); } pub fn render(&mut self, frame: &Frame) -> Result<bool> { + let rendered = self.draw_frame(frame); + // A frame that was not drawn still submits the section copies. + if let Some(copies) = self.take_section_copies() { + self.queue.submit([copies]); + } + rendered + } + fn draw_frame(&mut self, frame: &Frame) -> Result<bool> { + self.arena.trim(); + self.sorted_indices.trim(); + if let Some(flat) = &mut self.flat { + flat.trim(|page| self.arena.has_page(page)); + } if self.suspended { return Ok(false); } let started = Instant::now(); + if self.first_frame == FirstFrame::Acquire { + // Once: a minimized window acquires nothing for a while. + startup_step(format_args!("acquiring the first frame")); + self.first_frame = FirstFrame::Submit; + } let acquire = Instant::now(); let acquired = self.surface.as_ref().map(|s| s.get_current_texture()); self.last_wait = acquire.elapsed(); @@ -2213,14 +2661,21 @@ impl Renderer { Some(f) => f.texture.clone(), None => self.offscreen.clone().ok_or("no offscreen target")?, }; - let mut encoder = self - .device - .create_command_encoder(&wgpu::CommandEncoderDescriptor { - label: Some("Horizon frame"), - }); + // One command buffer for the frame, after the section copies staged + // since the last one: Metal commits every command buffer wgpu-core + // makes (one per run of copies and two per pass) on its own. + let section_copies = self.section_copies.take(); + let has_section_copies = section_copies.is_some(); + let mut encoder = section_copies.unwrap_or_else(|| { + self.device + .create_command_encoder(&wgpu::CommandEncoderDescriptor { + label: Some("Horizon frame"), + }) + }); if self.animated_textures { self.animate(&mut encoder, frame.ticks); - self.hud.animate_fire(&self.queue, frame.ticks); + self.hud + .animate_fire(&mut self.uploads, &mut encoder, frame.ticks); } self.update_lightmap( &mut encoder, @@ -2363,33 +2818,46 @@ impl Renderer { planes: frustum(view_proj), }; let camera = frame.camera; - let camera_section = ( - (camera.x.floor() as i32) >> 4, - (camera.y.floor() as i32 - crate::chunk::MIN_Y) >> 4, - (camera.z.floor() as i32) >> 4, - ); + let camera_section = camera_section(camera); let culling = !frame.title && frame.cave_culling; if culling { - let radius = (frame.distance / 16.0) as i32 + 2; - self.occlusion.update(camera_section, radius); + crate::spans::time("occlusion", || { + self.start_occlusion(camera, frame.detail_distance) + }); } let reachable = &self.occlusion; let mut visible = std::mem::take(&mut self.visible); visible.clear(); - self.sections.visible( - camera, - (frame.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(); + crate::spans::time("visibility", || { + self.sections.visible( + camera, + (frame.detail_distance / 16.0) as i32, + &view, + |k| !culling || reachable.contains(k), + &mut visible, + ) + }); + 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, - usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, + usage: wgpu::BufferUsages::VERTEX + | wgpu::BufferUsages::COPY_DST + | wgpu::BufferUsages::STORAGE, mapped_at_creation: false, }); } @@ -2397,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( @@ -2407,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(); @@ -2418,19 +2884,63 @@ impl Renderer { let eye = (frame.bob * crate::camera::view_matrix(frame.yaw, frame.pitch)) .inverse() .transform_point3(Vec3::ZERO); + // Occlusion culling leaves the sections its last result did not see + // to phase 2, after the others, with instance counts of 0. + let mut cull = self + .cull + .as_mut() + .filter(|_| { + self.cull_on + && !frame.title + && self.multi_draw + && !(self.flat_on && self.flat.is_some()) + }) + .and_then(|cull| cull.begin(&self.device).then_some(cull)); + self.phase2_draws.clear(); + self.phase2_pages.clear(); for (i, v) in visible.iter().enumerate() { let s = self.sections.get(v.slot); + let first_phase = cull.as_ref().is_none_or(|c| c.first(v.slot)); + let (draws, pages) = if first_phase { + (&mut self.draws, &mut self.draw_pages) + } else { + (&mut self.phase2_draws, &mut self.phase2_pages) + }; + let start = draws.len(); + let instances = u32::from(first_phase); + let mut indices = 0; // Only the solid groups that can face the eye. 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, i as u32]); + draws.push([quads * 6, instances, 0, first as u32, i as u32]); + pages.push((s.page, None)); + indices += quads * 6; + }); + match &mut cull { + Some(c) if first_phase => c.drawn(i, v.slot, indices), + Some(c) => c.deferred(i, v.slot, start, draws.len() - start, indices), + 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); + let culling = cull.is_some(); 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() { @@ -2458,35 +2968,83 @@ 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; - if self.multi_draw && !self.draws.is_empty() { - if self.draws.len() as u64 > self.indirect_capacity { - self.indirect_capacity = (self.draws.len() as u64).next_power_of_two(); - self.indirect = self.device.create_buffer(&wgpu::BufferDescriptor { - label: Some("Section draws"), - size: self.indirect_capacity * 20, - usage: wgpu::BufferUsages::INDIRECT | wgpu::BufferUsages::COPY_DST, - mapped_at_creation: false, - }); - } - self.uploads.buffer( - &mut encoder, - &self.indirect, - 0, - bytemuck::cast_slice(&self.draws), - ); - } + self.section_draws = self.draws.len(); + // Metal replays a multi-draw as one draw call per entry. + self.section_draw_calls = if self.multi_draw && self.info.backend != wgpu::Backend::Metal { + (0..self.draws.len()) + .filter(|&i| { + i == 0 + || i == phase1_draws as usize + || i == solid_draws as usize + || self.draw_pages[i] != self.draw_pages[i - 1] + }) + .count() + } else { + self.draws.len() + }; let debug_depth = frame.debug_lines.len() as u32; let debug_top = frame.debug_lines_on_top.len() as u32; let crosshair = frame.debug_crosshair.len() as u32; @@ -2623,6 +3181,95 @@ impl Renderer { bytemuck::bytes_of(&uniform), ); } + // Every upload before the frame's first pass: a pass ends wgpu-core's + // command buffer, so copies between passes would need one more. + if !frame.screen.is_empty() { + let vertices: Vec<crate::screen_effects::ScreenVertex> = + frame.screen.iter().flat_map(|q| q.vertices).collect(); + if vertices.len() as u64 > self.screen_capacity { + self.screen_capacity = (vertices.len() as u64).next_power_of_two(); + self.screen_buffer = self.device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Screen effect vertices"), + size: self.screen_capacity + * std::mem::size_of::<crate::screen_effects::ScreenVertex>() as u64, + usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + } + self.uploads.buffer( + &mut encoder, + &self.screen_buffer, + 0, + bytemuck::cast_slice(&vertices), + ); + } + self.hud.prepare( + &self.device, + &mut self.uploads, + &mut encoder, + frame.hud, + [self.config.width, self.config.height], + ); + let flat_frame = match &mut self.flat { + Some(flat) + 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( + &self.device, + &mut encoder, + &mut self.uploads, + &self.draws, + &self.draw_pages, + solid_draws as usize, + visible + .iter() + .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(), + ) + }) + } + _ => false, + }; + if flat_frame { + self.section_draw_calls = self.flat.as_ref().map_or(0, |f| f.calls.len()); + } else if self.multi_draw && !self.draws.is_empty() { + if self.draws.len() as u64 > self.indirect_capacity { + self.indirect_capacity = (self.draws.len() as u64).next_power_of_two(); + self.indirect = self.device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Section draws"), + size: self.indirect_capacity * 20, + usage: wgpu::BufferUsages::INDIRECT | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + } + self.uploads.buffer( + &mut encoder, + &self.indirect, + 0, + bytemuck::cast_slice(&self.draws), + ); + if culling && let Some(cull) = &mut self.cull { + cull.upload( + &self.device, + &mut self.uploads, + &mut encoder, + &self.draws[phase1_draws as usize..solid_draws as usize], + &self.instances, + &self.globals, + ); + } + } let view_target = target_texture.create_view(&wgpu::TextureViewDescriptor { format: Some(self.config.format.remove_srgb_suffix()), ..Default::default() @@ -2639,6 +3286,7 @@ impl Renderer { Some((color, depth)) => (color, depth), None => (&view_target, &self.depth), }; + let cull = self.cull.as_ref().filter(|_| culling && solid_draws > 0); { let attachments = [Some(wgpu::RenderPassColorAttachment { view: world_view, @@ -2667,8 +3315,9 @@ impl Renderer { view: world_depth, depth_ops: Some(wgpu::Operations { load: wgpu::LoadOp::Clear(0.0), - // A shader pack reads the world's depth. - store: if pack { + // A shader pack reads the world's depth, and + // phase 2 of occlusion culling draws over it. + store: if pack || cull.is_some() { wgpu::StoreOp::Store } else { wgpu::StoreOp::Discard @@ -2705,10 +3354,74 @@ impl Renderer { pass.draw(0..3, 0..1); } } - pass.set_pipeline(&self.solid); - pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); - pass.set_vertex_buffer(1, self.instances.slice(..)); - self.draw_sections(&mut pass, 0, solid_draws); + match self.flat.as_ref().filter(|_| flat_frame) { + Some(flat) => { + pass.set_pipeline(&flat.solid); + flat.draw(&mut pass, &flat.calls[..flat.solid_calls], |p| { + self.arena.buffer(p) + }); + pass.set_bind_group(0, &self.group, &[]); + pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); + } + None => { + pass.set_pipeline(&self.solid); + pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); + pass.set_vertex_buffer(1, self.instances.slice(..)); + match cull { + Some(cull) => { + // Phase 1 and the boxes against its depth, then + // what they found in sight, in a pass that loads + // both targets (it reads the test's results). + self.draw_sections(&mut pass, &self.indirect, 0, 0, phase1_draws); + cull.test(&mut pass); + drop(pass); + let attachments = [Some(wgpu::RenderPassColorAttachment { + view: world_view, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Load, + store: wgpu::StoreOp::Store, + }, + })]; + pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Horizon world, after culling"), + color_attachments: &attachments, + depth_stencil_attachment: Some( + wgpu::RenderPassDepthStencilAttachment { + view: world_depth, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Load, + store: if pack { + wgpu::StoreOp::Store + } else { + wgpu::StoreOp::Discard + }, + }), + stencil_ops: None, + }, + ), + ..Default::default() + }); + pass.set_bind_group(0, &self.group, &[]); + pass.set_pipeline(&self.solid); + pass.set_index_buffer( + self.indices.slice(..), + wgpu::IndexFormat::Uint32, + ); + pass.set_vertex_buffer(1, self.instances.slice(..)); + self.draw_sections( + &mut pass, + &cull.phase2, + phase1_draws, + phase1_draws, + solid_draws - phase1_draws, + ); + } + None => self.draw_sections(&mut pass, &self.indirect, 0, 0, solid_draws), + } + } + } if entity_quads > 0 { pass.set_pipeline(&self.entity_pipeline); pass.set_bind_group(0, &self.entity_group, &[]); @@ -2745,10 +3458,28 @@ impl Renderer { pass.set_vertex_buffer(0, self.debug_line_buffer.slice(..)); pass.draw(0..4, debug_depth..debug_depth + debug_top); } - pass.set_pipeline(&self.translucent); - pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); - pass.set_vertex_buffer(1, self.instances.slice(..)); - self.draw_sections(&mut pass, solid_draws, translucent_draws); + match self.flat.as_ref().filter(|_| flat_frame) { + Some(flat) => { + pass.set_pipeline(&flat.translucent); + flat.draw(&mut pass, &flat.calls[flat.solid_calls..], |p| { + self.arena.buffer(p) + }); + pass.set_bind_group(0, &self.group, &[]); + pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); + } + None => { + pass.set_pipeline(&self.translucent); + pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); + pass.set_vertex_buffer(1, self.instances.slice(..)); + self.draw_sections( + &mut pass, + &self.indirect, + 0, + solid_draws, + translucent_draws, + ); + } + } // LevelRenderer.executeClassicTransparency: translucent targets' // outlines follow terrain, so its blending cannot wash them out. if outline_edges > 0 && frame.outline_after_terrain { @@ -2862,24 +3593,6 @@ impl Renderer { ); } if !frame.screen.is_empty() { - let vertices: Vec<crate::screen_effects::ScreenVertex> = - frame.screen.iter().flat_map(|q| q.vertices).collect(); - if vertices.len() as u64 > self.screen_capacity { - self.screen_capacity = (vertices.len() as u64).next_power_of_two(); - self.screen_buffer = self.device.create_buffer(&wgpu::BufferDescriptor { - label: Some("Screen effect vertices"), - size: self.screen_capacity - * std::mem::size_of::<crate::screen_effects::ScreenVertex>() as u64, - usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, - mapped_at_creation: false, - }); - } - self.uploads.buffer( - &mut encoder, - &self.screen_buffer, - 0, - bytemuck::cast_slice(&vertices), - ); let attachments = [Some(wgpu::RenderPassColorAttachment { view: &view_target, resolve_target: None, @@ -2972,15 +3685,15 @@ impl Renderer { ); } self.hud.draw( - &self.device, - &self.queue, - &mut self.uploads, &mut encoder, &view_target, &self.depth, frame.hud, [self.config.width, self.config.height], ); + if culling && let Some(cull) = &mut self.cull { + cull.end(&self.device, &mut encoder); + } if let Some(timer) = &mut self.gpu_timer { timer.end(&mut encoder); } @@ -2989,7 +3702,20 @@ impl Renderer { .take() .map(|path| (path, self.copy_frame(&mut encoder, &target_texture))); self.uploads.finish(&encoder); - let submission = self.queue.submit([encoder.finish()]); + if has_section_copies { + self.section_copy_bytes = 0; + self.section_uploads.finish(&encoder); + } + let commands = crate::spans::time("encode", || encoder.finish()); + let first_frame = self.first_frame == FirstFrame::Submit; + if first_frame { + startup_step(format_args!("submitting the first frame")); + self.first_frame = FirstFrame::Drawn; + } + let submission = crate::spans::time("submit", || self.queue.submit([commands])); + if let Some(cull) = &mut self.cull { + cull.submitted(); + } if let Some(timer) = &mut self.gpu_timer { timer.submitted(); timer.collect(&self.device, &mut self.gpu_times); @@ -3008,6 +3734,9 @@ impl Renderer { if self.config.present_mode == wgpu::PresentMode::Fifo { self.window.pre_present_notify(); } + if first_frame { + startup_step(format_args!("presenting the first frame")); + } self.queue.present(surface_frame); if reconfigure && let Some(surface) = &self.surface { surface.configure(&self.device, &self.config); @@ -3027,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) } } @@ -3193,6 +3923,15 @@ enum Pack { Java(java_stages::JavaStages), } +/// The section the camera is in. +fn camera_section(camera: DVec3) -> SectionKey { + ( + (camera.x.floor() as i32) >> 4, + (camera.y.floor() as i32 - crate::chunk::MIN_Y) >> 4, + (camera.z.floor() as i32) >> 4, + ) +} + fn world_projection(fov_degrees: f32, aspect: f32, far: f32) -> Mat4 { Mat4::perspective_rh(fov_degrees.to_radians(), aspect, far, 0.05) } @@ -3243,6 +3982,84 @@ mod tests { ); } #[test] + fn windows_tries_vulkan_then_directx_12() { + use super::{GraphicsApi, backend_order}; + use wgpu::Backends; + assert_eq!( + backend_order(GraphicsApi::Auto, true, false), + [Backends::VULKAN, Backends::DX12] + ); + assert_eq!( + backend_order(GraphicsApi::Vulkan, true, false), + [Backends::VULKAN] + ); + assert_eq!( + backend_order(GraphicsApi::Dx12, true, false), + [Backends::DX12] + ); + // WGPU_BACKEND picks the backends itself; other systems have one. + for api in GraphicsApi::ALL { + assert_eq!(backend_order(api, true, true), [Backends::default()]); + assert_eq!(backend_order(api, false, false), [Backends::default()]); + } + } + /// Every WGSL shader file translates to HLSL as wgpu's DirectX 12 backend + /// writes it for FXC (shader model 5.1), entry point by entry point. + #[test] + fn shaders_translate_to_hlsl_for_directx_12() { + fn wgsl_files(dir: &std::path::Path, out: &mut Vec<std::path::PathBuf>) { + for entry in std::fs::read_dir(dir).unwrap() { + let path = entry.unwrap().path(); + if path.is_dir() { + wgsl_files(&path, out); + } else if path.extension().is_some_and(|e| e == "wgsl") { + out.push(path); + } + } + } + let mut files = Vec::new(); + wgsl_files( + std::path::Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/src")), + &mut files, + ); + assert!(files.len() >= 10, "{files:?}"); + for path in files { + let text = std::fs::read_to_string(&path).unwrap(); + // entity.wgsl comes after the overlay's constants (entity_shader). + let module = naga::front::wgsl::parse_str(&text) + .or_else(|_| { + naga::front::wgsl::parse_str(&(crate::overlay::wgsl_constants() + &text)) + }) + .unwrap_or_else(|e| panic!("{}: {e}", path.display())); + let info = naga::valid::Validator::new( + naga::valid::ValidationFlags::all(), + naga::valid::Capabilities::all(), + ) + .validate(&module) + .unwrap_or_else(|e| panic!("{}: {e:?}", path.display())); + for entry in &module.entry_points { + let (module, info) = naga::back::pipeline_constants::process_overrides( + &module, + &info, + Some((entry.stage, &entry.name)), + &Default::default(), + ) + .unwrap(); + let mut hlsl = String::new(); + naga::back::hlsl::Writer::new( + &mut hlsl, + &naga::back::hlsl::Options::default(), + &naga::back::hlsl::PipelineOptions { + entry_point: Some((entry.stage, entry.name.clone())), + }, + ) + .write(&module, &info, None) + .unwrap_or_else(|e| panic!("{} {}: {e}", path.display(), entry.name)); + assert!(!hlsl.is_empty()); + } + } + } + #[test] fn entity_shader_validates_with_its_overlay_constants() { let module = naga::front::wgsl::parse_str(&super::entity_shader()).unwrap(); naga::valid::Validator::new( @@ -3267,7 +4084,7 @@ mod tests { (3, offset_of!(TerrainVertex, color)), ] ); - assert_eq!(std::mem::size_of::<TerrainVertex>(), 28); + assert_eq!(std::mem::size_of::<TerrainVertex>(), 24); } #[test] fn cracks_multiply_instead_of_covering_the_block() { @@ -3302,12 +4119,20 @@ mod tests { assert_eq!(super::outline_width(3840), 5.0); } #[test] - fn indirect_draws_are_validated_only_in_debug_builds() { - if std::env::var_os("WGPU_VALIDATION_INDIRECT_CALL").is_none() { - let flags = super::instance_descriptor().flags; + fn indirect_draws_are_validated_only_in_debug_builds_and_on_directx_12() { + let validated = |backends| { + super::instance_descriptor(backends) + .flags + .contains(wgpu::InstanceFlags::VALIDATION_INDIRECT_CALL) + }; + if std::env::var_os("WGPU_VALIDATION_INDIRECT_CALL").is_none() + && std::env::var_os("WGPU_BACKEND").is_none() + { + assert_eq!(validated(wgpu::Backends::default()), cfg!(debug_assertions)); + assert_eq!(validated(wgpu::Backends::VULKAN), cfg!(debug_assertions)); assert_eq!( - flags.contains(wgpu::InstanceFlags::VALIDATION_INDIRECT_CALL), - cfg!(debug_assertions) + validated(wgpu::Backends::DX12), + cfg!(debug_assertions) || cfg!(windows) ); } } @@ -3490,13 +4315,39 @@ fn mix_frames(current: &mut [u8], next: &[u8], millis: u32) { let t = millis as f32 / 1000.0; for (c, n) in current.iter_mut().zip(next) { let (a, b) = (f32::from(*c) / 255.0, f32::from(*n) / 255.0); - *c = ((a * (1.0 - t) + b * t) * 255.0).round() as u8; + *c = round_unorm8((a * (1.0 - t) + b * t) * 255.0); } } +/// `x.round() as u8` for `x >= 0`, without the `roundf` library call that +/// x86-64 makes for every `f32::round` (it has no rounding instruction before +/// SSE4.1): the fraction `x - trunc(x)` is exact below 2^23, so rounding half +/// away from zero is adding one when it is at least a half. +#[inline] +fn round_unorm8(x: f32) -> u8 { + let whole = x as u8; + whole.saturating_add(u8::from(x - f32::from(whole) >= 0.5)) +} #[cfg(test)] mod sprite_upload_tests { use super::*; #[test] + fn blended_frames_round_like_f32_round() { + for millis in (0..1000).step_by(7).chain([999]) { + let t = millis as f32 / 1000.0; + for a in 0..=255u8 { + for b in 0..=255u8 { + let x = (f32::from(a) / 255.0 * (1.0 - t) + f32::from(b) / 255.0 * t) * 255.0; + assert_eq!(round_unorm8(x), x.round() as u8, "{a} {b} {millis}"); + } + } + } + for x in [ + 0.0, 0.49999997, 0.5, 1.5, 2.5, 254.49998, 254.5, 255.0, 255.00002, 300.0, + ] { + assert_eq!(round_unorm8(x), x.round() as u8, "{x}"); + } + } + #[test] fn real_rail_mips_do_not_change_with_larger_array_layers() { let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) .join("../../.local/horizon-client/26.3"); diff --git a/crates/horizon-client/src/gfx/arena.rs b/crates/horizon-client/src/gfx/arena.rs index 233333e04..8da835aa2 100644 --- a/crates/horizon-client/src/gfx/arena.rs +++ b/crates/horizon-client/src/gfx/arena.rs @@ -2,8 +2,11 @@ //! a frame binds it once and draws every section with indirect multi-draw. use std::collections::BTreeMap; -/// Ranges are rounded up to this many vertices to limit fragmentation. -const GRAIN: u64 = 256; +/// Ranges are rounded up to this many vertices to limit fragmentation, and +/// start at multiples of it (`flat` names sections by these blocks). +pub const GRAIN: u64 = 256; +/// The largest page, in bytes. +pub const PAGE_BYTES: u64 = 256 << 20; /// Free vertex ranges: start -> length, never adjacent. #[derive(Debug, PartialEq)] @@ -49,18 +52,30 @@ struct Page { /// Terrain vertices in pages of at most `page_limit` vertices (devices cap /// buffer sizes): a section's vertices sit in one page. pub struct Arena { - pages: Vec<Page>, + pages: Vec<Option<Page>>, stride: u64, page_limit: u64, + initial_capacity: u64, usage: wgpu::BufferUsages, + /// A page had to go to system memory (logged once). + spilled: bool, + /// Tests: GPU-local pages fail as if video memory were full. + #[cfg(test)] + video_memory_full: bool, } impl Arena { pub fn new(device: &wgpu::Device, stride: u64, capacity: u64) -> Self { Self::with_usage(device, stride, capacity, wgpu::BufferUsages::VERTEX) } - pub fn indices(device: &wgpu::Device, capacity: u64) -> Self { - Self::with_usage(device, 4, capacity, wgpu::BufferUsages::INDEX) + /// Sorted translucent indices; `storage` when compute passes read them. + pub fn indices(device: &wgpu::Device, capacity: u64, storage: bool) -> Self { + let usage = if storage { + wgpu::BufferUsages::INDEX | wgpu::BufferUsages::STORAGE + } else { + wgpu::BufferUsages::INDEX + }; + Self::with_usage(device, 4, capacity, usage) } fn with_usage( device: &wgpu::Device, @@ -69,31 +84,53 @@ impl Arena { usage: wgpu::BufferUsages, ) -> Self { // 256 MiB pages, or less where the device's buffers are smaller. - let bytes = device.limits().max_buffer_size.min(256 << 20); + let bytes = device.limits().max_buffer_size.min(PAGE_BYTES); let page_limit = (bytes / stride) / GRAIN * GRAIN; let capacity = capacity.next_multiple_of(GRAIN).min(page_limit); Self { - pages: vec![Page { - buffer: Self::create(device, stride, capacity, usage), - capacity, - free: FreeList::new(capacity), - }], + pages: Vec::new(), stride, page_limit, + initial_capacity: capacity, usage, + spilled: false, + #[cfg(test)] + video_memory_full: false, } } - fn create( - device: &wgpu::Device, - stride: u64, - capacity: u64, - usage: wgpu::BufferUsages, - ) -> wgpu::Buffer { - device.create_buffer(&wgpu::BufferDescriptor { - label: Some("Section buffer arena"), - size: capacity * stride, - usage: usage | wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC, - mapped_at_creation: false, + /// A buffer of `capacity` vertices in video memory or, when that is full + /// and the device allows it, in system memory: Vulkan drivers fail an + /// allocation rather than page like OpenGL ones, and wgpu's default error + /// handler panics. `None` when neither has room. + fn create(&mut self, device: &wgpu::Device, capacity: u64) -> Option<wgpu::Buffer> { + let usage = self.usage | wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC; + #[cfg(test)] + let local = (!self.video_memory_full) + .then(|| try_create(device, capacity * self.stride, usage)) + .flatten(); + #[cfg(not(test))] + let local = try_create(device, capacity * self.stride, usage); + local.or_else(|| { + // Mappable buffers are placed in host-visible memory, which the + // allocator takes from system memory once video memory is full. + if !device + .features() + .contains(wgpu::Features::MAPPABLE_PRIMARY_BUFFERS) + { + return None; + } + let buffer = try_create( + device, + capacity * self.stride, + usage | wgpu::BufferUsages::MAP_WRITE, + )?; + if !std::mem::replace(&mut self.spilled, true) { + log::warn!( + "Horizon: video memory is full; more terrain is kept in system memory \ + (lower the render distance if frames slow down)" + ); + } + Some(buffer) }) } /// The rounded length a request of `vertices` occupies. @@ -102,77 +139,274 @@ impl Arena { } /// Bytes of every page's buffer. pub fn bytes(&self) -> u64 { - self.pages.iter().map(|p| p.capacity).sum::<u64>() * self.stride + self.pages.iter().flatten().map(|p| p.capacity).sum::<u64>() * self.stride + } + /// The most vertices a page holds. + pub fn page_vertices(&self) -> u64 { + self.page_limit + } + pub fn has_page(&self, page: u32) -> bool { + self.pages.get(page as usize).is_some_and(Option::is_some) } pub fn buffer(&self, page: u32) -> &wgpu::Buffer { - &self.pages[page as usize].buffer + &self.pages[page as usize] + .as_ref() + .expect("live arena page") + .buffer } /// A range of `vertices`: (page, first vertex). Grows the newest page - /// (copying its contents on the GPU) or starts a new one when nothing - /// free is large enough. + /// (copying its contents on the GPU, in `encoder`) or starts a new one + /// when nothing free is large enough; `None` when no memory is left for + /// it. Writes to the arena belong in `encoder` too, so that those + /// recorded before a grow reach the grown page. pub fn alloc( &mut self, device: &wgpu::Device, - queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, vertices: u64, - ) -> (u32, u64) { + ) -> Option<(u32, u64)> { let len = Self::span(vertices).min(self.page_limit); for (i, page) in self.pages.iter_mut().enumerate() { - if let Some(start) = page.free.take(len) { - return (i as u32, start); + if let Some(page) = page + && let Some(start) = page.free.take(len) + { + return Some((i as u32, start)); } } - let last = self.pages.len() - 1; - let current = self.pages[last].capacity; - if current < self.page_limit { + let last = self.pages.len().saturating_sub(1); + if let Some(page) = self.pages.last().and_then(Option::as_ref) + && page.capacity < self.page_limit + { + let current = page.capacity; let grown = (current * 2).max(current + len).min(self.page_limit); - if grown - current >= len || grown == self.page_limit { - self.grow(device, queue, last, grown); - if let Some(start) = self.pages[last].free.take(len) { - return (last as u32, start); - } + if (grown - current >= len || grown == self.page_limit) + && self.grow(device, encoder, last, grown) + && let Some(start) = self.pages[last].as_mut().unwrap().free.take(len) + { + return Some((last as u32, start)); } } - let capacity = (1 << 20u64).max(len).min(self.page_limit); + let capacity = if self.pages.is_empty() { + self.initial_capacity + } else { + 1 << 20 + } + .max(len) + .min(self.page_limit); + // A page just large enough when memory is short. + let (buffer, capacity) = match self.create(device, capacity) { + Some(buffer) => (buffer, capacity), + None if capacity > len => (self.create(device, len)?, len), + None => return None, + }; let mut free = FreeList::new(capacity); let start = free.take(len).expect("a new page fits one section"); - self.pages.push(Page { - buffer: Self::create(device, self.stride, capacity, self.usage), + let page = Page { + buffer, capacity, free, - }); - ((self.pages.len() - 1) as u32, start) + }; + let index = self + .pages + .iter() + .position(Option::is_none) + .unwrap_or_else(|| { + self.pages.push(None); + self.pages.len() - 1 + }); + self.pages[index] = Some(page); + Some((index as u32, start)) } pub fn release(&mut self, page: u32, start: u64, vertices: u64) { let len = Self::span(vertices).min(self.page_limit); - self.pages[page as usize].free.give(start, len); + self.pages[page as usize] + .as_mut() + .expect("live arena page") + .free + .give(start, len); } - fn grow(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, page: usize, capacity: u64) { + /// Moves a page into a larger buffer; false (the page unchanged) when + /// there is no memory for it. + fn grow( + &mut self, + device: &wgpu::Device, + encoder: &mut wgpu::CommandEncoder, + page: usize, + capacity: u64, + ) -> bool { let capacity = capacity.next_multiple_of(GRAIN).min(self.page_limit); - let buffer = Self::create(device, self.stride, capacity, self.usage); - let old = &mut self.pages[page]; - let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { - label: Some("Grow section arena"), - }); + let Some(buffer) = self.create(device, capacity) else { + return false; + }; + let old = self.pages[page].as_mut().expect("live arena page"); + // Copies recorded for the old buffer run before this one. encoder.copy_buffer_to_buffer(&old.buffer, 0, &buffer, 0, old.capacity * self.stride); - // Writes queued for the old buffer run before this copy. - queue.submit([encoder.finish()]); let previous = old.capacity; old.buffer = buffer; old.capacity = capacity; old.free.give(previous, capacity - previous); + true } - pub fn clear(&mut self) { + /// Reclaim after an upload batch, so replacing a section can reuse its range. + pub fn trim(&mut self) { for page in &mut self.pages { - page.free = FreeList::new(page.capacity); + if let Some(p) = page + && p.free.0.len() == 1 + && p.free.0.get(&0) == Some(&p.capacity) + { + *page = None; + } + } + // Keep live page indices stable; later allocations reuse interior holes. + while self.pages.last().is_some_and(Option::is_none) { + self.pages.pop(); } } + pub fn clear(&mut self) { + self.pages.clear(); + } +} + +/// `create_buffer` with an out-of-memory failure returned instead of raised +/// (raised errors are fatal). +fn try_create(device: &wgpu::Device, size: u64, usage: wgpu::BufferUsages) -> Option<wgpu::Buffer> { + let scope = device.push_error_scope(wgpu::ErrorFilter::OutOfMemory); + let buffer = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Section buffer arena"), + size, + usage, + mapped_at_creation: false, + }); + // Native error scopes resolve at once. + pollster::block_on(scope.pop()).is_none().then_some(buffer) } #[cfg(test)] mod tests { use super::*; + fn device() -> Option<(wgpu::Device, wgpu::Queue)> { + device_with(wgpu::Features::empty()) + } + + /// A device with those of `features` the adapter has. + fn device_with(features: wgpu::Features) -> Option<(wgpu::Device, wgpu::Queue)> { + let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle()); + let adapter = pollster::block_on(instance.request_adapter(&Default::default())).ok()?; + eprintln!("arena GPU regression: {}", adapter.get_info().name); + let required_features = adapter.features() & features; + pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor { + required_features, + ..Default::default() + })) + .ok() + } + + /// `Arena::alloc`, with the copies of a grow submitted at once. + fn alloc( + arena: &mut Arena, + device: &wgpu::Device, + queue: &wgpu::Queue, + vertices: u64, + ) -> Option<(u32, u64)> { + let mut encoder = device.create_command_encoder(&Default::default()); + let range = arena.alloc(device, &mut encoder, vertices); + queue.submit([encoder.finish()]); + range + } + + #[test] + fn writes_recorded_before_a_grow_reach_the_grown_page() { + let Some((device, queue)) = device() else { + eprintln!("no GPU adapter: skipped"); + return; + }; + let mut arena = Arena::indices(&device, GRAIN, false); + let mut uploads = super::super::upload::Uploads::new(&device); + let mut encoder = device.create_command_encoder(&Default::default()); + let (page, start) = arena.alloc(&device, &mut encoder, GRAIN).unwrap(); + uploads.buffer( + &mut encoder, + arena.buffer(page), + start * 4, + &7u32.to_le_bytes(), + ); + // The page is full: the next range grows it, in the same encoder. + let (grown, next) = arena.alloc(&device, &mut encoder, GRAIN).unwrap(); + assert_eq!(grown, page); + uploads.buffer( + &mut encoder, + arena.buffer(grown), + next * 4, + &9u32.to_le_bytes(), + ); + uploads.finish(&encoder); + queue.submit([encoder.finish()]); + assert_eq!(read(&device, &queue, arena.buffer(page), start * 4), 7); + assert_eq!(read(&device, &queue, arena.buffer(page), next * 4), 9); + } + + #[test] + fn empty_pages_are_reclaimed_without_moving_live_ranges() { + let Some((device, queue)) = device() else { + eprintln!("no GPU adapter: skipped"); + return; + }; + let mut arena = Arena::new(&device, 4, GRAIN); + arena.page_limit = 2 * GRAIN; + assert_eq!(arena.bytes(), 0); + let (first, a) = alloc(&mut arena, &device, &queue, GRAIN).unwrap(); + let (_, b) = alloc(&mut arena, &device, &queue, GRAIN).unwrap(); + let (live, c) = alloc(&mut arena, &device, &queue, 2 * GRAIN).unwrap(); + let buffer = arena.buffer(live).clone(); + let data = [1u8, 2, 3, 4]; + queue.write_buffer(&buffer, c * 4, &data); + assert_eq!(arena.bytes(), 4 * GRAIN * 4); + arena.release(first, a, GRAIN); + arena.trim(); + assert_eq!(arena.bytes(), 4 * GRAIN * 4); + arena.release(first, b, GRAIN); + arena.trim(); + assert_eq!(arena.bytes(), 2 * GRAIN * 4); + assert_eq!(arena.buffer(live), &buffer); + let (reused, d) = alloc(&mut arena, &device, &queue, GRAIN).unwrap(); + assert_eq!(reused, first); + + let (tx, rx) = std::sync::mpsc::channel(); + wgpu::util::DownloadBuffer::read_buffer( + &device, + &queue, + &arena.buffer(live).slice(c * 4..c * 4 + 4), + move |result| tx.send(result.unwrap().to_vec()).unwrap(), + ); + device.poll(wgpu::PollType::wait_indefinitely()).unwrap(); + assert_eq!(rx.recv().unwrap(), data); + arena.release(reused, d, GRAIN); + arena.release(live, c, 2 * GRAIN); + arena.trim(); + assert_eq!(arena.bytes(), 0); + assert!(arena.pages.is_empty()); + } + + #[test] + fn clearing_an_arena_releases_buffers_and_can_allocate_again() { + let Some((device, queue)) = device() else { + eprintln!("no GPU adapter: skipped"); + return; + }; + for mut arena in [ + Arena::new(&device, 28, GRAIN), + Arena::indices(&device, GRAIN, false), + ] { + alloc(&mut arena, &device, &queue, GRAIN).unwrap(); + assert_eq!(arena.bytes(), arena.stride * GRAIN); + arena.clear(); + assert_eq!(arena.bytes(), 0); + assert_eq!(alloc(&mut arena, &device, &queue, GRAIN), Some((0, 0))); + assert_eq!(arena.bytes(), arena.stride * GRAIN); + } + } + #[test] fn free_ranges_coalesce() { let mut free = FreeList::new(4 * GRAIN); @@ -197,4 +431,63 @@ mod tests { free.give(a, GRAIN); assert_eq!(free.take(GRAIN), Some(a)); } + + fn read(device: &wgpu::Device, queue: &wgpu::Queue, buffer: &wgpu::Buffer, offset: u64) -> u32 { + let (tx, rx) = std::sync::mpsc::channel(); + wgpu::util::DownloadBuffer::read_buffer( + device, + queue, + &buffer.slice(offset..offset + 4), + move |result| tx.send(result.unwrap().to_vec()).unwrap(), + ); + device.poll(wgpu::PollType::wait_indefinitely()).unwrap(); + u32::from_le_bytes(rx.recv().unwrap()[..4].try_into().unwrap()) + } + + #[test] + fn full_video_memory_spills_to_system_memory() { + let Some((device, queue)) = device_with(wgpu::Features::MAPPABLE_PRIMARY_BUFFERS) else { + return; + }; + if !device + .features() + .contains(wgpu::Features::MAPPABLE_PRIMARY_BUFFERS) + { + return; + } + let mut arena = Arena::indices(&device, GRAIN, false); + let (page, start) = alloc(&mut arena, &device, &queue, GRAIN).unwrap(); + queue.write_buffer(arena.buffer(page), start * 4, &7u32.to_le_bytes()); + arena.video_memory_full = true; + // The full page can't grow in video memory: it moves to system + // memory with its contents, and new ranges come from there. + let (grown, next) = alloc(&mut arena, &device, &queue, GRAIN).unwrap(); + assert!(arena.spilled); + assert_eq!(grown, page); + assert!( + arena + .buffer(page) + .usage() + .contains(wgpu::BufferUsages::MAP_WRITE) + ); + queue.write_buffer(arena.buffer(grown), next * 4, &9u32.to_le_bytes()); + assert_eq!(read(&device, &queue, arena.buffer(page), start * 4), 7); + assert_eq!(read(&device, &queue, arena.buffer(grown), next * 4), 9); + } + + #[test] + fn no_memory_left_is_not_an_error() { + let Some((device, queue)) = device() else { + return; + }; + let mut arena = Arena::indices(&device, GRAIN, false); + alloc(&mut arena, &device, &queue, GRAIN).unwrap(); + arena.video_memory_full = true; + // Without mappable primary buffers nothing can spill: the request + // fails, and the arena keeps what it has. + assert_eq!(alloc(&mut arena, &device, &queue, GRAIN), None); + assert_eq!(arena.bytes(), GRAIN * 4); + arena.release(0, 0, GRAIN); + assert_eq!(alloc(&mut arena, &device, &queue, GRAIN), Some((0, 0))); + } } diff --git a/crates/horizon-client/src/gfx/cull.rs b/crates/horizon-client/src/gfx/cull.rs new file mode 100644 index 000000000..bcb738de3 --- /dev/null +++ b/crates/horizon-client/src/gfx/cull.rs @@ -0,0 +1,513 @@ +//! Occlusion culling of solid section draws, in two phases. +//! +//! Frustum and cave culling leave many sections that terrain in front hides +//! entirely (about half of the solid vertices drawn from the ground at render +//! distance 16, more at 32), and drawing fewer vertices is what helps an +//! integrated GPU. Phase 1 draws the sections that the last test read back +//! saw. Then, in the same pass, the box of every other section (and of a +//! share of phase 1's, to notice when they go out of sight) is drawn against +//! phase 1's depth with no writes but to the test's results. Phase 2, in a +//! second pass, draws the sections whose box was seen; the others' draws have +//! an instance count of 0. +//! +//! The picture stays the one without culling: the boxes (grown by a block, +//! the farthest a block model reaches out of its cell) are drawn front faces +//! only, so a fragment is the box's nearest point on its pixel, and the +//! fragment shader runs only where the depth test passes (forced early), so a +//! section is left out only where all of its fragments would fail the depth +//! test against the final depth, which is only nearer. What the CPU knows of +//! the results only sorts sections between the phases: a stale result draws +//! a hidden section in phase 1, or tests a section that is in sight. +//! +//! Every solid draw is an entry of one of the two lists, so the CPU records +//! as many draws as without culling: drivers that unroll an indirect +//! multi-draw on the CPU (Mesa's on Intel Gen9) pay per entry. Phase 2's +//! counts come from the GPU, which needs a native multi-draw and forced +//! early depth tests: Vulkan. Translucent draws are not culled. +use std::num::NonZeroU64; +use std::sync::{ + Arc, + atomic::{AtomicU8, Ordering}, +}; + +const WORDS: u64 = 5 * 4; +/// Phase 1's sections are each tested every `RETEST` frames. +const RETEST: u32 = 8; +/// Culling runs while the last result read back hid at least this share of +/// the frame's solid indices: below it the test and the second pass cost +/// more than they save. Else one frame in `PROBE` tests every section. +const MIN_SHARE: f32 = 0.1; +const PROBE: u32 = 64; +/// Boxes over more than this share of the screen count as seen untested. +const BIG: f32 = 0.25; +/// Results read back at once, some frames late so the CPU never waits. +const READBACKS: usize = 3; +const FREE: u8 = 0; +const PENDING: u8 = 1; +const MAPPED: u8 = 2; + +#[repr(C)] +#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] +struct Params { + stamp: u32, + big: f32, + pad: [u32; 2], +} + +/// One frame's results on their way back. +struct Readback { + buffer: wgpu::Buffer, + capacity: u64, + state: Arc<AtomicU8>, + stamp: u32, + /// The resident slot of each test, and its section's solid indices. + slots: Vec<u32>, + indices: Vec<u32>, + /// The frame's solid indices. + total: u64, +} + +/// The bind group and the buffers from outside it was made with. +struct Group { + origins: wgpu::Buffer, + globals: wgpu::Buffer, + group: wgpu::BindGroup, +} + +pub struct Cull { + pipeline: wgpu::RenderPipeline, + layout: wgpu::BindGroupLayout, + group: Option<Group>, + params: wgpu::Buffer, + /// Phase 2's draws: the solid draws after phase 1's in the frame's list. + pub phase2: wgpu::Buffer, + phase2_capacity: u64, + tests: wgpu::Buffer, + tests_capacity: u64, + /// This frame's tests: visible index, first phase 2 draw, phase 2 draws. + test_data: Vec<[u32; 4]>, + test_slots: Vec<u32>, + test_indices: Vec<u32>, + total: u64, + /// The frame tests every section, as culling was off. + probe: bool, + results: wgpu::Buffer, + readbacks: Vec<Readback>, + next: usize, + current: Option<usize>, + /// Per resident slot: whether the last result read back saw it. + seen: Vec<bool>, + stamp: u32, + /// Of the boxes the last test read back drew, how many it did not see + /// and how many it drew, and the share of solid indices hidden. + pub hidden: ([usize; 2], f32), + /// Cull every frame, whatever the hidden share (A/B checks). + pub always: bool, +} + +fn buffer( + device: &wgpu::Device, + label: &str, + size: u64, + usage: wgpu::BufferUsages, +) -> wgpu::Buffer { + device.create_buffer(&wgpu::BufferDescriptor { + label: Some(label), + size, + usage, + mapped_at_creation: false, + }) +} + +const PHASE2_USAGE: wgpu::BufferUsages = wgpu::BufferUsages::INDIRECT + .union(wgpu::BufferUsages::STORAGE) + .union(wgpu::BufferUsages::COPY_DST); +const TESTS_USAGE: wgpu::BufferUsages = + wgpu::BufferUsages::STORAGE.union(wgpu::BufferUsages::COPY_DST); +const RESULTS_USAGE: wgpu::BufferUsages = + wgpu::BufferUsages::STORAGE.union(wgpu::BufferUsages::COPY_SRC); +const READBACK_USAGE: wgpu::BufferUsages = + wgpu::BufferUsages::MAP_READ.union(wgpu::BufferUsages::COPY_DST); + +impl Cull { + /// The device feature culling needs, where the adapter has it. + pub fn features(adapter: &wgpu::Adapter) -> wgpu::Features { + adapter.features() & wgpu::Features::SHADER_EARLY_DEPTH_TEST + } + /// Whether the device draws an indirect multi-draw natively and runs + /// fragment shaders that write storage only past an early depth test. + pub fn supported(adapter: &wgpu::Adapter, device: &wgpu::Device) -> bool { + adapter.get_info().backend == wgpu::Backend::Vulkan + && device.features().contains( + wgpu::Features::SHADER_EARLY_DEPTH_TEST | wgpu::Features::INDIRECT_FIRST_INSTANCE, + ) + && adapter.get_downlevel_capabilities().flags.contains( + wgpu::DownlevelFlags::VERTEX_STORAGE + | wgpu::DownlevelFlags::FRAGMENT_WRITABLE_STORAGE, + ) + } + /// `color` is the world pass's colour format. + pub fn new(device: &wgpu::Device, color: wgpu::TextureFormat) -> Self { + let entry = |binding, visibility, ty| wgpu::BindGroupLayoutEntry { + binding, + visibility, + ty, + count: None, + }; + let storage = |read_only| wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Storage { read_only }, + has_dynamic_offset: false, + min_binding_size: None, + }; + let uniform = wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }; + let both = wgpu::ShaderStages::VERTEX_FRAGMENT; + let vertex = wgpu::ShaderStages::VERTEX; + let fragment = wgpu::ShaderStages::FRAGMENT; + let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("Section culling"), + entries: &[ + entry(0, both, storage(true)), + entry(1, fragment, storage(false)), + entry(2, fragment, storage(false)), + entry(3, vertex, storage(true)), + entry(4, vertex, uniform), + entry(5, both, uniform), + ], + }); + let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("Section culling"), + source: wgpu::ShaderSource::Wgsl(include_str!("cull.wgsl").into()), + }); + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("Section culling"), + bind_group_layouts: &[Some(&layout)], + ..Default::default() + }); + let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("Section culling"), + layout: Some(&pipeline_layout), + vertex: wgpu::VertexState { + module: &module, + entry_point: Some("vs_box"), + compilation_options: Default::default(), + buffers: &[], + }, + primitive: wgpu::PrimitiveState { + cull_mode: Some(wgpu::Face::Back), + ..Default::default() + }, + // The world pass's reversed depth test, without writes. + depth_stencil: Some(wgpu::DepthStencilState { + format: wgpu::TextureFormat::Depth32Float, + depth_write_enabled: Some(false), + depth_compare: Some(wgpu::CompareFunction::GreaterEqual), + stencil: Default::default(), + bias: Default::default(), + }), + multisample: Default::default(), + fragment: Some(wgpu::FragmentState { + module: &module, + entry_point: Some("fs_box"), + compilation_options: Default::default(), + targets: &[Some(wgpu::ColorTargetState { + format: color, + blend: None, + write_mask: wgpu::ColorWrites::empty(), + })], + }), + multiview_mask: None, + cache: None, + }); + Self { + pipeline, + layout, + group: None, + params: buffer( + device, + "Section culling parameters", + std::mem::size_of::<Params>() as u64, + wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + ), + phase2: buffer(device, "Section draws, phase 2", WORDS, PHASE2_USAGE), + phase2_capacity: 1, + tests: buffer(device, "Section tests", 16, TESTS_USAGE), + tests_capacity: 1, + test_data: Vec::new(), + test_slots: Vec::new(), + test_indices: Vec::new(), + total: 0, + probe: false, + results: buffer(device, "Section test results", 4, RESULTS_USAGE), + readbacks: (0..READBACKS) + .map(|_| Readback { + buffer: buffer(device, "Section test readback", 4, READBACK_USAGE), + capacity: 1, + state: Arc::new(AtomicU8::new(FREE)), + stamp: 0, + slots: Vec::new(), + indices: Vec::new(), + total: 0, + }) + .collect(), + next: 0, + current: None, + seen: Vec::new(), + stamp: 0, + hidden: ([0; 2], 1.0), + always: false, + } + } + + /// Start a frame: take in the results that came back, and tell whether + /// the frame culls. + pub fn begin(&mut self, device: &wgpu::Device) -> bool { + let _ = device.poll(wgpu::PollType::Poll); + for i in 0..READBACKS { + let r = &mut self.readbacks[(self.next + i) % READBACKS]; + if r.state.load(Ordering::Acquire) != MAPPED { + continue; + } + let mapped = r.buffer.slice(..r.slots.len() as u64 * 4); + if let Ok(data) = mapped.get_mapped_range() { + // The mapping isn't guaranteed to be 4-byte aligned (a Vega 11 + // on Windows panicked in cast_slice), so copy the words out. + let results: Vec<u32> = bytemuck::pod_collect_to_vec(&data); + let (mut hidden, mut indices) = (0, 0u64); + for (&result, &n) in results.iter().zip(&r.indices) { + if result != r.stamp { + hidden += 1; + indices += u64::from(n); + } + } + let share = indices as f32 / r.total.max(1) as f32; + self.hidden = ([hidden, results.len()], share); + for (&slot, &result) in r.slots.iter().zip(&results) { + let slot = slot as usize; + if slot >= self.seen.len() { + self.seen.resize(slot + 1, false); + } + self.seen[slot] = result == r.stamp; + } + } + r.buffer.unmap(); + r.state.store(FREE, Ordering::Release); + } + self.stamp = self.stamp.wrapping_add(1).max(1); + self.test_data.clear(); + self.test_slots.clear(); + self.test_indices.clear(); + self.total = 0; + self.probe = !self.always && self.hidden.1 < MIN_SHARE; + !self.probe || self.stamp.is_multiple_of(PROBE) + } + + /// Whether phase 1 draws the section in resident `slot`. + pub fn first(&self, slot: u32) -> bool { + self.seen.get(slot as usize).copied().unwrap_or(false) + } + + /// A section phase 1 draws, `index` in the visible list, with `indices` + /// solid indices: tested in turn. + pub fn drawn(&mut self, index: usize, slot: u32, indices: u32) { + self.total += u64::from(indices); + if indices > 0 && (self.probe || slot.wrapping_add(self.stamp).is_multiple_of(RETEST)) { + self.push([index as u32, 0, 0, 0], slot, indices); + } + } + + /// A section left to phase 2: its `draws` from phase 2's `first`. + pub fn deferred(&mut self, index: usize, slot: u32, first: usize, draws: usize, indices: u32) { + self.total += u64::from(indices); + if draws > 0 { + self.push([index as u32, first as u32, draws as u32, 0], slot, indices); + } + } + + fn push(&mut self, test: [u32; 4], slot: u32, indices: u32) { + self.test_data.push(test); + self.test_slots.push(slot); + self.test_indices.push(indices); + } + + /// Stage this frame's inputs: phase 2's draws, with instance counts of 0, + /// and the tests, which read the frame's camera-relative `origins` and + /// view projection (the start of `globals`). + pub fn upload( + &mut self, + device: &wgpu::Device, + uploads: &mut super::upload::Uploads, + encoder: &mut wgpu::CommandEncoder, + phase2: &[[u32; 5]], + origins: &wgpu::Buffer, + globals: &wgpu::Buffer, + ) { + let draws = phase2.len() as u64; + if draws > self.phase2_capacity { + self.phase2_capacity = draws.next_power_of_two(); + self.phase2 = buffer( + device, + "Section draws, phase 2", + self.phase2_capacity * WORDS, + PHASE2_USAGE, + ); + self.group = None; + } + let tests = self.test_data.len() as u64; + if tests > self.tests_capacity { + self.tests_capacity = tests.next_power_of_two(); + self.tests = buffer( + device, + "Section tests", + self.tests_capacity * 16, + TESTS_USAGE, + ); + self.results = buffer( + device, + "Section test results", + self.tests_capacity * 4, + RESULTS_USAGE, + ); + self.group = None; + } + if draws > 0 { + uploads.buffer(encoder, &self.phase2, 0, bytemuck::cast_slice(phase2)); + } + if tests > 0 { + uploads.buffer( + encoder, + &self.tests, + 0, + bytemuck::cast_slice(&self.test_data), + ); + } + let params = Params { + stamp: self.stamp, + big: BIG, + pad: [0; 2], + }; + uploads.buffer(encoder, &self.params, 0, bytemuck::bytes_of(¶ms)); + if !self + .group + .as_ref() + .is_some_and(|g| g.origins == *origins && g.globals == *globals) + { + self.group = Some(self.make_group(device, origins, globals)); + } + } + + /// In the world pass after phase 1: draw the tested boxes against its + /// depth. Leaves the pass's pipeline and group 0 changed. + pub fn test(&self, pass: &mut wgpu::RenderPass<'_>) { + let Some(Group { group, .. }) = &self.group else { + return; + }; + if self.test_data.is_empty() { + return; + } + pass.set_pipeline(&self.pipeline); + pass.set_bind_group(0, group, &[]); + pass.draw(0..36, 0..self.test_data.len() as u32); + } + + /// After the world passes: copy the results for reading back, when a + /// readback buffer is free. + pub fn end(&mut self, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder) { + let tests = self.test_data.len() as u64; + let r = &mut self.readbacks[self.next]; + if tests == 0 || r.state.load(Ordering::Acquire) != FREE { + return; + } + if tests > r.capacity { + r.capacity = tests.next_power_of_two(); + r.buffer = buffer( + device, + "Section test readback", + r.capacity * 4, + READBACK_USAGE, + ); + } + encoder.copy_buffer_to_buffer(&self.results, 0, &r.buffer, 0, tests * 4); + r.stamp = self.stamp; + r.slots.clear(); + r.slots.extend_from_slice(&self.test_slots); + r.indices.clear(); + r.indices.extend_from_slice(&self.test_indices); + r.total = self.total; + self.current = Some(self.next); + } + + /// After the frame's submission: start reading its results back. + pub fn submitted(&mut self) { + let Some(i) = self.current.take() else { + return; + }; + let r = &self.readbacks[i]; + let state = r.state.clone(); + state.store(PENDING, Ordering::Release); + r.buffer + .slice(..r.slots.len() as u64 * 4) + .map_async(wgpu::MapMode::Read, move |result| { + state.store( + if result.is_ok() { MAPPED } else { FREE }, + Ordering::Release, + ); + }); + self.next = (i + 1) % READBACKS; + } + + fn make_group( + &self, + device: &wgpu::Device, + origins: &wgpu::Buffer, + globals: &wgpu::Buffer, + ) -> Group { + fn whole(binding: u32, buffer: &wgpu::Buffer) -> wgpu::BindGroupEntry<'_> { + wgpu::BindGroupEntry { + binding, + resource: buffer.as_entire_binding(), + } + } + let group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Section culling"), + layout: &self.layout, + entries: &[ + whole(0, &self.tests), + whole(1, &self.results), + whole(2, &self.phase2), + whole(3, origins), + wgpu::BindGroupEntry { + binding: 4, + resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { + buffer: globals, + offset: 0, + size: NonZeroU64::new(64), + }), + }, + whole(5, &self.params), + ], + }); + Group { + origins: origins.clone(), + globals: globals.clone(), + group, + } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn shader_validates() { + let module = naga::front::wgsl::parse_str(include_str!("cull.wgsl")).unwrap(); + naga::valid::Validator::new( + naga::valid::ValidationFlags::all(), + naga::valid::Capabilities::all(), + ) + .validate(&module) + .unwrap(); + } +} diff --git a/crates/horizon-client/src/gfx/cull.wgsl b/crates/horizon-client/src/gfx/cull.wgsl new file mode 100644 index 000000000..faa495caf --- /dev/null +++ b/crates/horizon-client/src/gfx/cull.wgsl @@ -0,0 +1,101 @@ +// Occlusion culling of solid section draws (gfx/cull.rs). + +struct Params { + // This frame's test: a test that sees its box stores the number. + stamp: u32, + // Boxes covering more than this share of the screen count as seen. + big: f32, +} +// The leading member of terrain.wgsl's Globals. +struct Globals { + view_proj: mat4x4<f32>, +} + +// One test per section: its visible index (which names its origin), its +// first draw in `phase2` and how many draws it has there (0: phase 1 drew +// it, and the test only tells whether it is still in sight). +@group(0) @binding(0) var<storage, read> tests: array<vec4<u32>>; +// Per test: the stamp of the frame whose test saw the box. +@group(0) @binding(1) var<storage, read_write> results: array<u32>; +// Phase 2's draws, 5 words each, with instance counts of 0 until seen. +@group(0) @binding(2) var<storage, read_write> phase2: array<u32>; +// Camera-relative origin of each visible section. +@group(0) @binding(3) var<storage, read> origins: array<vec4<f32>>; +@group(0) @binding(4) var<uniform> globals: Globals; +@group(0) @binding(5) var<uniform> params: Params; + +// The corners (bit 0: x, bit 1: y, bit 2: z) of the box's 12 triangles, +// counter-clockwise seen from outside. +const CORNERS = array<u32, 36>( + 0u, 6u, 2u, 0u, 4u, 6u, 1u, 3u, 7u, 1u, 7u, 5u, 0u, 1u, 5u, 0u, 5u, 4u, + 2u, 7u, 3u, 2u, 6u, 7u, 0u, 3u, 1u, 0u, 2u, 3u, 4u, 5u, 7u, 4u, 7u, 6u, +); + +struct Box { + @builtin(position) position: vec4<f32>, + @location(0) @interpolate(flat) test: u32, +} + +fn corner(origin: vec3<f32>, c: u32) -> vec3<f32> { + return origin + vec3( + select(-1.0, 17.0, (c & 1u) != 0u), + select(-1.0, 17.0, (c & 2u) != 0u), + select(-1.0, 17.0, (c & 4u) != 0u), + ); +} + +// Each tested section's box, grown by a block (block models reach one block +// out of their cell), drawn after phase 1 against its depth with back faces +// culled: a front face fragment that passes the depth test is the box's +// nearest point on that pixel in front of phase 1's depth, so the section +// may show there. A box failing everywhere would have every fragment of its +// section fail the depth test against the final depth, which is only nearer. +@vertex +fn vs_box(@builtin(vertex_index) v: u32, @builtin(instance_index) t: u32) -> Box { + let origin = origins[tests[t].x].xyz; + var out: Box; + out.test = t; + // The box's screen rectangle, from the corners in front of the camera. + var lo = vec2(1e30); + var hi = vec2(-1e30); + var behind = false; + for (var c = 0u; c < 8u; c++) { + let p = globals.view_proj * vec4(corner(origin, c), 1.0); + behind = behind || p.w <= 0.0; + let q = p.xy / max(p.w, 1e-6); + lo = min(lo, q); + hi = max(hi, q); + } + let size = max(min(hi, vec2(1.0)) - max(lo, vec2(-1.0)), vec2(0.0)); + // A box within a block of the camera may cross the near plane, which + // clips its front faces away, and a box over a large part of the screen + // is nearly always in sight and costly to test: both count as seen, + // through a small triangle in the top left corner at the nearest depth. + let near = all(origin < vec3(2.0)) && all(origin > vec3(-18.0)); + if near || (!behind && size.x * size.y > params.big * 4.0) { + var p = vec2(-1.0, 1.0); + if v == 1u { + p.y -= 0.05; + } else if v == 2u { + p.x += 0.05; + } + out.position = select(vec4(0.0, 0.0, 0.0, 1.0), vec4(p, 1.0, 1.0), v < 3u); + return out; + } + out.position = globals.view_proj * vec4(corner(origin, CORNERS[v]), 1.0); + return out; +} + +// Runs only for fragments that pass the depth test (forced early). +@fragment +@early_depth_test(force) +fn fs_box(in: Box) -> @location(0) vec4<f32> { + if results[in.test] != params.stamp { + results[in.test] = params.stamp; + let test = tests[in.test]; + for (var k = 0u; k < test.z; k++) { + phase2[(test.y + k) * 5u + 1u] = 1u; + } + } + return vec4(0.0); +} diff --git a/crates/horizon-client/src/gfx/flat.rs b/crates/horizon-client/src/gfx/flat.rs new file mode 100644 index 000000000..a1ae20d09 --- /dev/null +++ b/crates/horizon-client/src/gfx/flat.rs @@ -0,0 +1,664 @@ +//! Flat section draws: where indirect multi-draw is emulated, as on Metal +//! (wgpu replays it as one draw call per indirect entry), the frame's section +//! draws are copied by a compute pass into one index list, in draw order, and +//! drawn with one draw call per run of sections in the same arena page. +//! +//! The picture is the one the per-section draws give: the same triangles +//! from the same vertices in the same order, through the same vertex and +//! fragment code. Only where a vertex finds its section's origin changes: +//! from the instance attribute to a table. A section's vertices fill whole +//! blocks of `GRAIN` vertices of its page (the arena rounds every range up), +//! so each block names one section: its id here, which indexes the frame's +//! camera-relative origins (computed on the CPU exactly as for the instance +//! attribute). +use super::arena::GRAIN; + +/// One section draw for `flatten.wgsl`: index count, first sorted index (or +/// `QUADS` for a solid draw), base vertex, first index in the list. +type Range = [u32; 4]; +const QUADS: u32 = u32::MAX; +const GROUP_LIMIT: u32 = 65535; + +struct PageTable { + /// Section id of every block of the page. + blocks: wgpu::Buffer, + group: Option<wgpu::BindGroup>, +} + +/// A run of the frame's index list drawn from one arena page. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Call { + pub page: u32, + pub first: u32, + pub count: u32, +} + +pub struct Flat { + pub solid: wgpu::RenderPipeline, + pub translucent: wgpu::RenderPipeline, + flatten: wgpu::ComputePipeline, + compute_layout: wgpu::BindGroupLayout, + draw_layout: wgpu::BindGroupLayout, + blocks_per_page: u64, + pages: Vec<Option<PageTable>>, + free_ids: Vec<u32>, + next_id: u32, + origins: wgpu::Buffer, + origin_capacity: u64, + origin_data: Vec<[f32; 4]>, + /// The frame's index list. + pub indices: wgpu::Buffer, + index_capacity: u64, + ranges: wgpu::Buffer, + range_capacity: u64, + range_data: Vec<Range>, + /// Segments of `range_data` by index source: (sorted index page or + /// `None` for solid draws, first range, ranges). + segments: Vec<(Option<u32>, u64, u64)>, + empty: wgpu::Buffer, + /// This frame's draw calls: solid ones, then translucent ones. + pub calls: Vec<Call>, + pub solid_calls: usize, + /// Frames with fewer section draws keep the per-section draws. + pub min_draws: usize, + range_align: u64, +} + +/// Section draws from which a frame is drawn flat. Below it the index list +/// costs the GPU more than the draw calls cost the CPU: on an M4 Pro at +/// render distance 16 (2,900 draws) the copy and the list add about 0.4 ms +/// of GPU time for 0.5–0.8 ms less CPU time; at 32 (9,300 draws) the CPU +/// saves 1.3 ms. +const MIN_DRAWS: usize = 4096; + +/// Whether flat draws apply: the backend emulates multi-draw, and vertex +/// shaders can read storage buffers large enough for an index page. +/// `HORIZON_FLAT_DRAWS=1` draws every frame flat, `0` none. +pub fn wanted(adapter: &wgpu::Adapter, device: &wgpu::Device) -> bool { + match std::env::var("HORIZON_FLAT_DRAWS").as_deref() { + Ok("0") => return false, + Ok("1") => return supported(adapter, device), + _ => {} + } + adapter.get_info().backend == wgpu::Backend::Metal && supported(adapter, device) +} + +fn supported(adapter: &wgpu::Adapter, device: &wgpu::Device) -> bool { + let flags = adapter.get_downlevel_capabilities().flags; + let limits = device.limits(); + flags.contains(wgpu::DownlevelFlags::VERTEX_STORAGE | wgpu::DownlevelFlags::COMPUTE_SHADERS) + && limits.max_storage_buffers_per_shader_stage >= 3 + && limits.max_storage_buffer_binding_size >= super::arena::PAGE_BYTES +} + +fn storage(binding: u32, stage: wgpu::ShaderStages, read_only: bool) -> wgpu::BindGroupLayoutEntry { + wgpu::BindGroupLayoutEntry { + binding, + visibility: stage, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Storage { read_only }, + has_dynamic_offset: false, + min_binding_size: None, + }, + count: None, + } +} + +fn buffer( + device: &wgpu::Device, + label: &str, + size: u64, + usage: wgpu::BufferUsages, +) -> wgpu::Buffer { + device.create_buffer(&wgpu::BufferDescriptor { + label: Some(label), + size: size.max(16), + usage: usage | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }) +} + +/// Lays out the frame's index list: each draw's range (destinations in draw +/// order) grouped by index source into `ranges`, every group padded to +/// `align` entries for binding, with `segments` naming each group; and the +/// draw calls, one per run of draws in the same vertex page, solid and +/// translucent apart. Returns the first translucent call and the list's +/// length. +fn plan( + draws: &[[u32; 5]], + pages: &[(u32, Option<u32>)], + solid_draws: usize, + align: u64, + calls: &mut Vec<Call>, + ranges: &mut Vec<Range>, + segments: &mut Vec<(Option<u32>, u64, u64)>, +) -> (usize, u64) { + calls.clear(); + ranges.clear(); + segments.clear(); + let mut solid_calls = None; + let mut total = 0u64; + let mut by_source: Vec<(Option<u32>, Vec<Range>)> = Vec::new(); + for (i, (d, &(page, sorted))) in draws.iter().zip(pages).enumerate() { + let first = if sorted.is_some() { d[2] } else { QUADS }; + let range = [d[0], first, d[3], total as u32]; + match by_source.iter_mut().find(|(s, _)| *s == sorted) { + Some((_, list)) => list.push(range), + None => by_source.push((sorted, vec![range])), + } + if i == solid_draws { + solid_calls = Some(calls.len()); + } + match calls.last_mut() { + Some(last) if last.page == page && i != solid_draws => last.count += d[0], + _ => calls.push(Call { + page, + first: total as u32, + count: d[0], + }), + } + total += u64::from(d[0]); + } + for (source, list) in by_source { + segments.push((source, ranges.len() as u64, list.len() as u64)); + ranges.extend(list); + let padded = (ranges.len() as u64).next_multiple_of(align); + ranges.resize(padded as usize, [0; 4]); + } + (solid_calls.unwrap_or(calls.len()), total) +} + +impl Flat { + /// `pipeline(layout, label, vertex entry, fragment entry, blend)` builds a + /// terrain pipeline as the per-section ones, without the origin buffer. + pub fn new( + device: &wgpu::Device, + terrain_layout: &wgpu::BindGroupLayout, + page_vertices: u64, + pipeline: impl Fn( + &wgpu::PipelineLayout, + &str, + &str, + Option<wgpu::BlendState>, + ) -> wgpu::RenderPipeline, + ) -> Self { + let vertex = wgpu::ShaderStages::VERTEX; + let compute = wgpu::ShaderStages::COMPUTE; + let draw_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("Flat section draws"), + entries: &[storage(0, vertex, true), storage(1, vertex, true)], + }); + let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("Flat terrain layout"), + bind_group_layouts: &[Some(terrain_layout), Some(&draw_layout)], + ..Default::default() + }); + let compute_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("Flatten section draws"), + entries: &[ + storage(0, compute, true), + storage(1, compute, true), + storage(2, compute, false), + ], + }); + let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("Flatten section draws"), + source: wgpu::ShaderSource::Wgsl(include_str!("flatten.wgsl").into()), + }); + let flatten = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { + label: Some("Flatten section draws"), + layout: Some( + &device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("Flatten section draws"), + bind_group_layouts: &[Some(&compute_layout)], + ..Default::default() + }), + ), + module: &module, + entry_point: Some("flatten"), + compilation_options: Default::default(), + cache: None, + }); + let storage_usage = wgpu::BufferUsages::STORAGE; + Self { + solid: pipeline(&layout, "Terrain solid (flat)", "fs_solid", None), + translucent: pipeline( + &layout, + "Terrain translucent (flat)", + "fs_translucent", + Some(wgpu::BlendState::ALPHA_BLENDING), + ), + flatten, + compute_layout, + draw_layout, + blocks_per_page: page_vertices.div_ceil(GRAIN), + pages: Vec::new(), + free_ids: Vec::new(), + next_id: 0, + origins: buffer(device, "Section origins by id", 16, storage_usage), + origin_capacity: 1, + origin_data: Vec::new(), + indices: buffer( + device, + "Flat section indices", + 16, + wgpu::BufferUsages::INDEX | storage_usage, + ), + index_capacity: 4, + ranges: buffer(device, "Flat section ranges", 16, storage_usage), + range_capacity: 1, + range_data: Vec::new(), + segments: Vec::new(), + empty: buffer(device, "No sorted indices", 16, storage_usage), + calls: Vec::new(), + solid_calls: 0, + min_draws: if std::env::var("HORIZON_FLAT_DRAWS").as_deref() == Ok("1") { + 0 + } else { + MIN_DRAWS + }, + range_align: (u64::from(device.limits().min_storage_buffer_offset_alignment) / 16) + .max(1), + } + } + /// A section uploaded to `vertices` vertices from `start` of `page`: its + /// id, which its blocks now name (written through `uploads` in `encoder`). + pub fn add( + &mut self, + device: &wgpu::Device, + encoder: &mut wgpu::CommandEncoder, + uploads: &mut super::upload::Uploads, + page: u32, + start: u64, + vertices: u64, + ) -> u32 { + let id = self.free_ids.pop().unwrap_or_else(|| { + self.next_id += 1; + self.next_id - 1 + }); + let page = page as usize; + if self.pages.len() <= page { + self.pages.resize_with(page + 1, || None); + } + let table = self.pages[page].get_or_insert_with(|| PageTable { + blocks: buffer( + device, + "Section ids by block", + self.blocks_per_page * 4, + wgpu::BufferUsages::STORAGE, + ), + group: None, + }); + let first = start / GRAIN; + let ids = vec![id; vertices.div_ceil(GRAIN) as usize]; + uploads.buffer( + encoder, + &table.blocks, + first * 4, + bytemuck::cast_slice(&ids), + ); + id + } + pub fn remove(&mut self, id: u32) { + self.free_ids.push(id); + } + pub fn clear(&mut self) { + self.pages.clear(); + self.free_ids.clear(); + self.next_id = 0; + } + /// Forgets the tables of pages the arena released. + pub fn trim(&mut self, live: impl Fn(u32) -> bool) { + for (i, page) in self.pages.iter_mut().enumerate() { + if page.is_some() && !live(i as u32) { + *page = None; + } + } + } + /// Builds the frame's index list from `draws` (`DrawIndexedIndirectArgs`: + /// index count, instances, first index, base vertex, first instance), + /// solid ones first, with their (vertex page, sorted index page) and + /// the origins of the sections they draw by id. Records the copy into + /// `encoder`; `calls` then lists the frame's draw calls. False when the + /// frame cannot be drawn flat (the per-section draws then apply). + #[allow(clippy::too_many_arguments)] + pub fn prepare( + &mut self, + device: &wgpu::Device, + encoder: &mut wgpu::CommandEncoder, + uploads: &mut super::upload::Uploads, + draws: &[[u32; 5]], + pages: &[(u32, Option<u32>)], + solid_draws: usize, + origins: impl Iterator<Item = (u32, [f32; 4])>, + sorted_page: impl Fn(u32) -> wgpu::Buffer, + ) -> bool { + // Origins by section id. + self.origin_data.clear(); + for (id, o) in origins { + let id = id as usize; + if self.origin_data.len() <= id { + self.origin_data.resize(id + 1, [0.0; 4]); + } + 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(); + self.origins = buffer( + device, + "Section origins by id", + self.origin_capacity * 16, + wgpu::BufferUsages::STORAGE, + ); + for page in self.pages.iter_mut().flatten() { + page.group = None; + } + } + if !self.origin_data.is_empty() { + uploads.buffer( + encoder, + &self.origins, + 0, + bytemuck::cast_slice(&self.origin_data), + ); + } + let (solid_calls, total) = plan( + draws, + pages, + solid_draws, + self.range_align, + &mut self.calls, + &mut self.range_data, + &mut self.segments, + ); + self.solid_calls = solid_calls; + if total == 0 || total * 4 > device.limits().max_storage_buffer_binding_size { + self.calls.clear(); + self.solid_calls = 0; + return false; + } + if self.range_data.len() as u64 > self.range_capacity { + self.range_capacity = (self.range_data.len() as u64).next_power_of_two(); + self.ranges = buffer( + device, + "Flat section ranges", + self.range_capacity * 16, + wgpu::BufferUsages::STORAGE, + ); + } + if total > self.index_capacity { + self.index_capacity = total.next_power_of_two(); + self.indices = buffer( + device, + "Flat section indices", + self.index_capacity * 4, + wgpu::BufferUsages::INDEX | wgpu::BufferUsages::STORAGE, + ); + } + uploads.buffer( + encoder, + &self.ranges, + 0, + bytemuck::cast_slice(&self.range_data), + ); + let groups: Vec<(wgpu::BindGroup, u32)> = self + .segments + .iter() + .map(|&(source, first, count)| { + let sorted = source.map(&sorted_page); + let group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Flatten section draws"), + layout: &self.compute_layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { + buffer: &self.ranges, + offset: first * 16, + size: wgpu::BufferSize::new(count * 16), + }), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: sorted.as_ref().unwrap_or(&self.empty).as_entire_binding(), + }, + wgpu::BindGroupEntry { + binding: 2, + resource: self.indices.as_entire_binding(), + }, + ], + }); + (group, count as u32) + }) + .collect(); + let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor { + label: Some("Flatten section draws"), + timestamp_writes: None, + }); + pass.set_pipeline(&self.flatten); + for (group, count) in &groups { + pass.set_bind_group(0, group, &[]); + pass.dispatch_workgroups((*count).min(GROUP_LIMIT), count.div_ceil(GROUP_LIMIT), 1); + } + drop(pass); + for page in self.calls.iter().map(|c| c.page) { + let table = self.pages[page as usize] + .as_mut() + .expect("a drawn page has its block table"); + if table.group.is_none() { + table.group = Some(device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Flat section draws"), + layout: &self.draw_layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: table.blocks.as_entire_binding(), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: self.origins.as_entire_binding(), + }, + ], + })); + } + } + true + } + /// Draws `calls` with a flat pipeline set and `vertices` giving each + /// page's vertex buffer. + pub fn draw<'a>( + &self, + pass: &mut wgpu::RenderPass<'_>, + calls: &[Call], + vertices: impl Fn(u32) -> &'a wgpu::Buffer, + ) { + pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); + for call in calls { + let table = self.pages[call.page as usize] + .as_ref() + .and_then(|t| t.group.as_ref()) + .expect("prepared page"); + pass.set_vertex_buffer(0, vertices(call.page).slice(..)); + pass.set_bind_group(1, table, &[]); + pass.draw_indexed(call.first..call.first + call.count, 0, 0..1); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Draws as `render` writes them: solid (page, quads at a base vertex) + /// first, then translucent (page, sorted page, count, first sorted index, + /// base vertex). + fn sample() -> (Vec<[u32; 5]>, Vec<(u32, Option<u32>)>, usize) { + let draws = vec![ + [6 * 3, 1, 0, 0, 0], + [6 * 2, 1, 0, 512, 1], + [6, 1, 0, 256, 2], + [6 * 4, 1, 0, 1024, 3], + [6 * 2, 1, 6, 768, 1], + [6, 1, 0, 40, 0], + ]; + let pages = vec![ + (0, None), + (0, None), + (1, None), + (0, None), + (0, Some(0)), + (0, Some(1)), + ]; + (draws, pages, 4) + } + + #[test] + fn plan_draws_runs_of_a_page_in_order() { + let (draws, pages, solid) = sample(); + let (mut calls, mut ranges, mut segments) = (Vec::new(), Vec::new(), Vec::new()); + let (solid_calls, total) = plan( + &draws, + &pages, + solid, + 4, + &mut calls, + &mut ranges, + &mut segments, + ); + assert_eq!(total, 6 * 13); + let call = |page, first, count| Call { page, first, count }; + // Pages 0, 0 | 1 | 0, then translucent 0, 0 (never merged with solid). + assert_eq!( + calls, + [ + call(0, 0, 30), + call(1, 30, 6), + call(0, 36, 24), + call(0, 60, 18) + ] + ); + assert_eq!(solid_calls, 3); + // Solid ranges, then each sorted page's, every group padded to 4. + assert_eq!(segments, [(None, 0, 4), (Some(0), 4, 1), (Some(1), 8, 1)]); + assert_eq!(ranges[3], [24, QUADS, 1024, 36]); + assert_eq!(ranges[4], [12, 6, 768, 60]); + assert_eq!(ranges[8], [6, 0, 40, 72]); + assert_eq!(ranges.len(), 12); + } + + /// The compute pass writes exactly the indices the per-section draws + /// read: quads 0 1 2 0 2 3 for solid draws, sorted indices otherwise, + /// plus the base vertex. + #[test] + fn flatten_writes_the_indices_of_the_per_section_draws() { + let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle()); + let Ok(adapter) = pollster::block_on(instance.request_adapter(&Default::default())) else { + eprintln!("no GPU adapter: skipped"); + return; + }; + let Ok((device, queue)) = pollster::block_on(adapter.request_device(&Default::default())) + else { + eprintln!("no GPU device: skipped"); + return; + }; + use wgpu::util::DeviceExt; + let (draws, pages, solid) = sample(); + let align = (u64::from(device.limits().min_storage_buffer_offset_alignment) / 16).max(1); + let (mut calls, mut ranges, mut segments) = (Vec::new(), Vec::new(), Vec::new()); + let (_, total) = plan( + &draws, + &pages, + solid, + align, + &mut calls, + &mut ranges, + &mut segments, + ); + let sorted: [Vec<u32>; 2] = [ + (0..32).map(|i| (i * 7) % 32).collect(), + (0..8).rev().collect(), + ]; + let mut expected = Vec::new(); + for (d, (_, page)) in draws.iter().zip(&pages) { + for k in 0..d[0] { + let i = match page { + None => (k / 6) * 4 + [0, 1, 2, 0, 2, 3][(k % 6) as usize], + Some(p) => sorted[*p as usize][(d[2] + k) as usize], + }; + expected.push(d[3] + i); + } + } + let storage = |entries: &[u32]| { + device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: None, + contents: bytemuck::cast_slice(entries), + usage: wgpu::BufferUsages::STORAGE, + }) + }; + let range_buffer = storage(bytemuck::cast_slice(&ranges)); + let sorted_buffers = [storage(&sorted[0]), storage(&sorted[1]), storage(&[0; 4])]; + let out = device.create_buffer(&wgpu::BufferDescriptor { + label: None, + size: total * 4, + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC, + mapped_at_creation: false, + }); + let read = device.create_buffer(&wgpu::BufferDescriptor { + label: None, + size: total * 4, + usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: None, + source: wgpu::ShaderSource::Wgsl(include_str!("flatten.wgsl").into()), + }); + let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { + label: None, + layout: None, + module: &module, + entry_point: Some("flatten"), + compilation_options: Default::default(), + cache: None, + }); + let mut encoder = device.create_command_encoder(&Default::default()); + { + let mut pass = encoder.begin_compute_pass(&Default::default()); + pass.set_pipeline(&pipeline); + for &(source, first, count) in &segments { + let group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: None, + layout: &pipeline.get_bind_group_layout(0), + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { + buffer: &range_buffer, + offset: first * 16, + size: wgpu::BufferSize::new(count * 16), + }), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: sorted_buffers[source.map_or(2, |p| p as usize)] + .as_entire_binding(), + }, + wgpu::BindGroupEntry { + binding: 2, + resource: out.as_entire_binding(), + }, + ], + }); + pass.set_bind_group(0, &group, &[]); + pass.dispatch_workgroups(count as u32, 1, 1); + } + } + encoder.copy_buffer_to_buffer(&out, 0, &read, 0, total * 4); + queue.submit([encoder.finish()]); + read.slice(..).map_async(wgpu::MapMode::Read, |_| {}); + device.poll(wgpu::PollType::wait_indefinitely()).unwrap(); + let got: Vec<u32> = + bytemuck::cast_slice(&read.slice(..).get_mapped_range().unwrap()).to_vec(); + assert_eq!(got, expected); + } +} diff --git a/crates/horizon-client/src/gfx/flatten.wgsl b/crates/horizon-client/src/gfx/flatten.wgsl new file mode 100644 index 000000000..2f57f8775 --- /dev/null +++ b/crates/horizon-client/src/gfx/flatten.wgsl @@ -0,0 +1,35 @@ +// Flat section draws (gfx/flat.rs): copies each section draw's indices into +// one index list, in draw order, offset by the draw's base vertex. Solid +// draws index quads (0 1 2 0 2 3 per quad, as Renderer::quad_indices); +// translucent ones read their sorted indices. +struct Range { + count: u32, + // First sorted index, or QUADS for a solid draw. + first: u32, + base: u32, + dst: u32, +} +const QUADS: u32 = 0xffffffffu; +const GROUP: u32 = 64u; +@group(0) @binding(0) var<storage, read> ranges: array<Range>; +@group(0) @binding(1) var<storage, read> sorted: array<u32>; +@group(0) @binding(2) var<storage, read_write> out: array<u32>; +@compute @workgroup_size(64) +fn flatten(@builtin(workgroup_id) group: vec3<u32>, + @builtin(local_invocation_index) lane: u32) { + let r = group.y * 65535u + group.x; + if r >= arrayLength(&ranges) { + return; + } + let d = ranges[r]; + for (var k = lane; k < d.count; k += GROUP) { + var i: u32; + if d.first == QUADS { + let c = k % 6u; + i = (k / 6u) * 4u + select(select(c - 2u, 0u, c == 3u), c, c < 3u); + } else { + i = sorted[d.first + k]; + } + out[d.dst + k] = d.base + i; + } +} diff --git a/crates/horizon-client/src/gfx/lod.rs b/crates/horizon-client/src/gfx/lod.rs new file mode 100644 index 000000000..6699a62a3 --- /dev/null +++ b/crates/horizon-client/src/gfx/lod.rs @@ -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()); + } +} diff --git a/crates/horizon-client/src/gfx/occlusion.rs b/crates/horizon-client/src/gfx/occlusion.rs index e871c50c0..b63e7eae5 100644 --- a/crates/horizon-client/src/gfx/occlusion.rs +++ b/crates/horizon-client/src/gfx/occlusion.rs @@ -2,11 +2,18 @@ //! faces. Breadth-first from the camera section through the faces each //! section's VisGraph connects, never stepping back towards the camera. //! -//! The search runs over dense arrays around the camera rather than hash sets, -//! so it stays cheap enough to redo whenever a section is meshed. +//! As in vanilla, the full search runs off the render thread: a worker keeps +//! its own copy of every section's connectivity and searches over dense +//! arrays around the camera. Frames use the last finished graph and never wait +//! for the next one. A section meshed in between propagates into the graph the +//! frame has (vanilla's partial update), so what it opens shows at once; what +//! it closes goes at the next full search. use super::SectionKey; +use crate::mesher::ALL_VISIBLE; use std::collections::{HashMap, VecDeque}; +use std::sync::mpsc::{self, Receiver, Sender, TryRecvError}; +/// Vanilla's `Direction` order: down, up, north, south, west, east. const STEPS: [(i32, i32, i32); 6] = [ (0, -1, 0), (0, 1, 0), @@ -15,74 +22,92 @@ const STEPS: [(i32, i32, i32); 6] = [ (-1, 0, 0), (1, 0, 0), ]; +/// In `Graph::directions`: the cell has a node. +const REACHED: u8 = 1 << 7; +/// In `Graph::entered`: the camera's own section, entered through no face. const NONE: u8 = 6; -pub struct Occlusion { - /// VisGraph connectivity per column, one entry per section (bottom first). - columns: HashMap<(i32, i32), Box<[u64]>>, +/// VisGraph connectivity per column, one entry per section (bottom first). +/// A missing column or section connects every face. +struct Columns { sections: i32, - dirty: bool, - from: Option<SectionKey>, + map: HashMap<(i32, i32), Box<[u64]>>, +} + +impl Columns { + fn new(sections: i32) -> Self { + Self { + sections, + map: HashMap::new(), + } + } + /// Whether the section's connectivity changed. + fn set(&mut self, key: SectionKey, vis: u64) -> bool { + if !(0..self.sections).contains(&key.1) { + return false; + } + let column = match self.map.get_mut(&(key.0, key.2)) { + Some(column) => column, + None if vis == ALL_VISIBLE => return false, + None => self + .map + .entry((key.0, key.2)) + .or_insert_with(|| vec![ALL_VISIBLE; self.sections as usize].into()), + }; + let slot = &mut column[key.1 as usize]; + if *slot == vis { + return false; + } + *slot = vis; + if vis == ALL_VISIBLE && column.iter().all(|&v| v == ALL_VISIBLE) { + self.map.remove(&(key.0, key.2)); + } + true + } + fn get(&self, key: SectionKey) -> u64 { + self.map + .get(&(key.0, key.2)) + .and_then(|column| column.get(usize::try_from(key.1).ok()?)) + .copied() + .unwrap_or(ALL_VISIBLE) + } + fn apply(&mut self, edit: Edit) { + match edit { + Edit::Set(key, vis) => { + self.set(key, vis); + } + Edit::Clear => self.map.clear(), + } + } +} + +#[derive(Clone, Copy)] +enum Edit { + Set(SectionKey, u64), + Clear, +} + +/// One search's result over the box around the camera: vanilla's +/// `GraphStorage.sectionToNodeMap`, as dense arrays. +#[derive(Default)] +struct Graph { + from: SectionKey, radius: i32, /// The searched box: lowest section y and its height in sections. y0: i32, height: i32, - reached: Vec<bool>, - vis: Vec<u64>, - queue: VecDeque<(u32, u8, u8)>, + /// Per cell: `REACHED`, and the directions travelled from the camera to + /// it (`Node.directions`). + directions: Vec<u8>, + /// Per cell: the face it was first entered through (`NONE` for the + /// camera's). Vanilla's `Node.sourceDirections` also keeps the faces it is + /// reached through later; Horizon keeps the first only. + entered: Vec<u8>, } -impl Occlusion { - pub fn new(sections: i32) -> Self { - Self { - columns: HashMap::new(), - sections, - dirty: true, - from: None, - radius: 0, - y0: 0, - height: 0, - reached: Vec::new(), - vis: Vec::new(), - queue: VecDeque::new(), - } - } - pub fn set(&mut self, key: SectionKey, vis: u64) { - if !(0..self.sections).contains(&key.1) { - return; - } - let sections = self.sections as usize; - let column = self - .columns - .entry((key.0, key.2)) - .or_insert_with(|| vec![crate::mesher::ALL_VISIBLE; sections].into()); - let slot = &mut column[key.1 as usize]; - if *slot != vis { - *slot = vis; - self.dirty = true; - } - } - pub fn remove(&mut self, key: SectionKey) { - let Some(column) = self.columns.get_mut(&(key.0, key.2)) else { - return; - }; - if let Some(slot) = column.get_mut(key.1 as usize) - && *slot != crate::mesher::ALL_VISIBLE - { - *slot = crate::mesher::ALL_VISIBLE; - self.dirty = true; - } - if column.iter().all(|&v| v == crate::mesher::ALL_VISIBLE) { - self.columns.remove(&(key.0, key.2)); - } - } - pub fn clear(&mut self) { - self.columns.clear(); - self.dirty = true; - } +impl Graph { fn index(&self, key: SectionKey) -> Option<usize> { - let from = self.from?; - let (dx, dz, dy) = (key.0 - from.0, key.2 - from.2, key.1 - self.y0); + let (dx, dz, dy) = (key.0 - self.from.0, key.2 - self.from.2, key.1 - self.y0); let side = 2 * self.radius + 1; if dx.abs() > self.radius || dz.abs() > self.radius || !(0..self.height).contains(&dy) { return None; @@ -90,86 +115,331 @@ impl Occlusion { Some((((dz + self.radius) * side + dx + self.radius) * self.height + dy) as usize) } fn key(&self, index: usize) -> SectionKey { - let from = self.from.expect("searched"); let side = 2 * self.radius + 1; let i = index as i32; - let dy = i % self.height; let column = i / self.height; ( - from.0 + column % side - self.radius, - self.y0 + dy, - from.2 + column / side - self.radius, + self.from.0 + column % side - self.radius, + self.y0 + i % self.height, + self.from.2 + column / side - self.radius, ) } - pub fn contains(&self, key: SectionKey) -> bool { - self.index(key).is_some_and(|i| self.reached[i]) + fn contains(&self, key: SectionKey) -> bool { + self.index(key) + .is_some_and(|i| self.directions[i] & REACHED != 0) } - /// Search again when sections changed or the camera moved to another section. - pub fn update(&mut self, from: SectionKey, radius: i32) { - if !self.dirty && self.from == Some(from) && self.radius == radius { - return; - } - self.from = Some(from); + /// The full search from `from` (vanilla's `scheduleFullUpdate`). + fn search( + &mut self, + columns: &Columns, + from: SectionKey, + radius: i32, + vis: &mut Vec<u64>, + queue: &mut VecDeque<u32>, + ) { + self.from = from; self.radius = radius; self.y0 = from.1.min(-1); - self.height = from.1.max(self.sections) - self.y0 + 1; + self.height = from.1.max(columns.sections) - self.y0 + 1; let side = (2 * radius + 1) as usize; let size = side * side * self.height as usize; - self.reached.clear(); - self.reached.resize(size, false); - self.vis.clear(); - self.vis.resize(size, crate::mesher::ALL_VISIBLE); + self.directions.clear(); + self.directions.resize(size, 0); + self.entered.clear(); + self.entered.resize(size, NONE); + vis.clear(); + vis.resize(size, ALL_VISIBLE); for dz in -radius..=radius { for dx in -radius..=radius { - let Some(column) = self.columns.get(&(from.0 + dx, from.2 + dz)) else { + let Some(column) = columns.map.get(&(from.0 + dx, from.2 + dz)) else { continue; }; let base = self .index((from.0 + dx, 0, from.2 + dz)) .expect("column inside the searched box"); - self.vis[base..base + column.len()].copy_from_slice(column); + vis[base..base + column.len()].copy_from_slice(column); } } let start = self.index(from).expect("camera inside the searched box"); - self.reached[start] = true; - self.queue.clear(); - self.queue.push_back((start as u32, NONE, 0)); - while let Some((index, entered, travelled)) = self.queue.pop_front() { - let vis = self.vis[index as usize]; - let key = self.key(index as usize); + self.directions[start] = REACHED; + queue.clear(); + queue.push_back(start as u32); + self.propagate(queue, columns.sections, |i, _| vis[i]); + } + /// Vanilla's `runUpdates`: take nodes from the queue and step through + /// every face their entry face connects to, away from the camera. + fn propagate( + &mut self, + queue: &mut VecDeque<u32>, + sections: i32, + vis: impl Fn(usize, SectionKey) -> u64, + ) { + while let Some(index) = queue.pop_front() { + let i = index as usize; + let key = self.key(i); + let directions = self.directions[i]; + let entered = self.entered[i]; + let exits = if entered == NONE { + 0x3f + } else { + (vis(i, key) >> (u32::from(entered) * 6)) as u8 & 0x3f + }; for (d, step) in STEPS.iter().enumerate() { - let opposite = d ^ 1; - if travelled & (1 << opposite) != 0 { - continue; - } - if entered != NONE && vis & (1 << (usize::from(entered) * 6 + d)) == 0 { + if exits & (1 << d) == 0 || directions & (1 << (d ^ 1)) != 0 { continue; } let next = (key.0 + step.0, key.1 + step.1, key.2 + step.2); - if next.1 < -1 || next.1 > self.sections { + if next.1 < -1 || next.1 > sections { continue; } let Some(n) = self.index(next) else { continue }; - if !self.reached[n] { - self.reached[n] = true; - self.queue - .push_back((n as u32, opposite as u8, travelled | (1 << d))); + if self.directions[n] & REACHED == 0 { + self.directions[n] = directions | 1 << d; + self.entered[n] = (d ^ 1) as u8; + queue.push_back(n as u32); } } } + } +} + +struct Job { + edits: Vec<Edit>, + from: SectionKey, + radius: i32, + /// Buffers to reuse. + graph: Graph, +} + +/// The thread that runs full searches over its own copy of the columns. +struct Worker { + jobs: Sender<Job>, + done: Receiver<(Graph, Vec<Edit>)>, +} + +impl Worker { + fn spawn(sections: i32) -> Option<Self> { + let (jobs, job_rx) = mpsc::channel::<Job>(); + let (done_tx, done) = mpsc::channel(); + std::thread::Builder::new() + .name("occlusion".into()) + .spawn(move || { + let mut columns = Columns::new(sections); + let (mut vis, mut queue) = (Vec::new(), VecDeque::new()); + while let Ok(mut job) = job_rx.recv() { + for edit in job.edits.drain(..) { + columns.apply(edit); + } + job.graph + .search(&columns, job.from, job.radius, &mut vis, &mut queue); + if done_tx.send((job.graph, job.edits)).is_err() { + break; + } + } + }) + .ok()?; + Some(Self { jobs, done }) + } +} + +pub struct Occlusion { + /// The render thread's copy, for partial updates (and searches when the + /// worker is gone). + columns: Columns, + /// Edits the worker has not been sent yet. + edits: Vec<Edit>, + /// A full search is due: sections changed since the last one started. + dirty: bool, + /// The last finished search; `None` until one finishes, or after the + /// columns were cleared. + graph: Option<Graph>, + spare: Graph, + worker: Option<Worker>, + /// The camera section and radius of the running search. + running: Option<(SectionKey, i32)>, + /// The running search started before the columns were cleared: drop it. + discard: bool, + /// The camera section and radius `graph`'s search started from (or is + /// starting from). + searched: Option<(SectionKey, i32)>, + /// Sections changed since `graph`'s search started, to propagate from. + changed: Vec<SectionKey>, + /// Sections changed since the running search started: they propagate into + /// its graph when it lands. + changed_late: Vec<SectionKey>, + vis: Vec<u64>, + queue: VecDeque<u32>, +} + +impl Occlusion { + pub fn new(sections: i32) -> Self { + Self { + columns: Columns::new(sections), + edits: Vec::new(), + dirty: true, + graph: None, + spare: Graph::default(), + worker: Worker::spawn(sections), + running: None, + discard: false, + searched: None, + changed: Vec::new(), + changed_late: Vec::new(), + vis: Vec::new(), + queue: VecDeque::new(), + } + } + pub fn set(&mut self, key: SectionKey, vis: u64) { + if self.columns.set(key, vis) { + self.edits.push(Edit::Set(key, vis)); + self.dirty = true; + self.changed.push(key); + if self.running.is_some() { + self.changed_late.push(key); + } + } + } + pub fn remove(&mut self, key: SectionKey) { + self.set(key, ALL_VISIBLE); + } + pub fn clear(&mut self) { + self.columns.map.clear(); + self.edits.clear(); + self.edits.push(Edit::Clear); + self.dirty = true; + // Another world: the old graph says nothing about it. + if let Some(graph) = self.graph.take() { + self.spare = graph; + } + self.searched = None; + self.discard = self.running.is_some(); + self.changed.clear(); + self.changed_late.clear(); + } + /// Whether `key` is visible from the camera. Without a usable graph + /// (before the first search lands, or with the camera somewhere the graph + /// never reached) every section is. + pub fn contains(&self, key: SectionKey) -> bool { + self.graph.as_ref().is_none_or(|g| g.contains(key)) + } + /// Take a finished search, propagate changed sections into the graph, + /// and start a search when sections changed or the camera moved to + /// another section. Never waits for the worker. + pub fn update(&mut self, from: SectionKey, radius: i32) { + self.collect(false); + self.propagate_changes(); + if self.running.is_none() && (self.dirty || self.searched != Some((from, radius))) { + self.start(from, radius); + } + // A graph that never reached the camera's section hides everything + // around it: until the new one lands, cull by the frustum alone. + if self + .graph + .as_ref() + .is_some_and(|g| g.from != from && !g.contains(from)) + && let Some(graph) = self.graph.take() + { + self.spare = graph; + } + } + fn start(&mut self, from: SectionKey, radius: i32) { self.dirty = false; + self.searched = Some((from, radius)); + let graph = std::mem::take(&mut self.spare); + if let Some(worker) = &self.worker { + let job = Job { + edits: std::mem::take(&mut self.edits), + from, + radius, + graph, + }; + if worker.jobs.send(job).is_ok() { + self.running = Some((from, radius)); + self.changed_late.clear(); + return; + } + log::warn!("Horizon: the occlusion worker stopped; culling on the render thread"); + self.worker = None; + } + self.edits.clear(); + let mut graph = Graph::default(); + graph.search(&self.columns, from, radius, &mut self.vis, &mut self.queue); + self.land(graph); + } + fn collect(&mut self, wait: bool) { + let Some(worker) = &self.worker else { return }; + if self.running.is_none() { + return; + } + let result = if wait { + worker.done.recv().map_err(|_| TryRecvError::Disconnected) + } else { + worker.done.try_recv() + }; + match result { + Ok((graph, edits)) => { + self.running = None; + if self.edits.is_empty() { + self.edits = edits; + } + if std::mem::take(&mut self.discard) { + self.spare = graph; + return; + } + self.land(graph); + self.changed.clear(); + self.changed.append(&mut self.changed_late); + } + Err(TryRecvError::Empty) => {} + Err(TryRecvError::Disconnected) => { + // The edits it held are lost with it: search here from now on. + self.worker = None; + self.running = None; + self.discard = false; + self.dirty = true; + } + } + } + fn land(&mut self, graph: Graph) { + if let Some(old) = self.graph.replace(graph) { + self.spare = old; + } + } + /// Vanilla's `runPartialUpdate`: what changed sections now connect to. + fn propagate_changes(&mut self) { + let Some(graph) = &mut self.graph else { + self.changed.clear(); + return; + }; + self.queue.clear(); + for key in self.changed.drain(..) { + if let Some(i) = graph.index(key) + && graph.directions[i] & REACHED != 0 + { + self.queue.push_back(i as u32); + } + } + let columns = &self.columns; + graph.propagate(&mut self.queue, columns.sections, |_, key| columns.get(key)); + } + /// Wait for the running search: tests, and A/B timing of the old + /// blocking search. + #[cfg(test)] + fn settle(&mut self, from: SectionKey, radius: i32) { + self.update(from, radius); + self.collect(true); + self.propagate_changes(); } } #[cfg(test)] mod tests { use super::*; - use crate::mesher::ALL_VISIBLE; #[test] fn open_space_reaches_the_whole_box() { let mut o = Occlusion::new(4); - o.update((0, 1, 0), 2); + o.settle((0, 1, 0), 2); for x in -2..=2 { for z in -2..=2 { for y in -1..=4 { @@ -186,11 +456,155 @@ mod tests { for (dx, dy, dz) in STEPS { o.set((dx, 1 + dy, dz), 0); } - o.update((0, 1, 0), 3); + o.settle((0, 1, 0), 3); assert!(o.contains((1, 1, 0)), "a neighbour is seen"); assert!(!o.contains((2, 1, 0)), "nothing shows through it"); o.set((1, 1, 0), ALL_VISIBLE); - o.update((0, 1, 0), 3); + o.settle((0, 1, 0), 3); assert!(o.contains((2, 1, 0))); } + + #[test] + fn frames_never_wait_and_partial_updates_open_at_once() { + let mut o = Occlusion::new(4); + for (dx, dy, dz) in STEPS { + o.set((dx, 1 + dy, dz), 0); + } + o.settle((0, 1, 0), 3); + assert!(!o.contains((2, 1, 0))); + // Opening the wall shows what is behind it in the same frame, from the + // graph the frame has, whether or not the full search has finished. + o.set((1, 1, 0), ALL_VISIBLE); + o.update((0, 1, 0), 3); + assert!(o.contains((2, 1, 0))); + assert!(o.contains((3, 1, 0))); + // Closing it again lasts until the next full search lands. + o.set((1, 1, 0), 0); + o.settle((0, 1, 0), 3); + o.settle((0, 1, 0), 3); + assert!(!o.contains((2, 1, 0))); + } + + #[test] + fn a_graph_that_lost_the_camera_culls_nothing() { + let mut o = Occlusion::new(4); + for (dx, dy, dz) in STEPS { + o.set((dx, 1 + dy, dz), 0); + } + o.settle((0, 1, 0), 3); + assert!(!o.contains((3, 1, 0))); + // The camera jumped past the wall; the next search is still running. + let o = &mut o; + o.update((3, 1, 0), 3); + if o.running.is_some() { + assert!(o.contains((3, 1, 0))); + } + o.settle((3, 1, 0), 3); + assert!(o.contains((3, 1, 0))); + assert!( + !o.contains((0, 1, 0)), + "the old camera section is walled in" + ); + } + + /// The search before it moved to the worker (main at ce38c8d7), over the + /// same box: whether each cell is reached. + fn reference(columns: &Columns, from: SectionKey, radius: i32) -> Vec<bool> { + let y0 = from.1.min(-1); + let height = from.1.max(columns.sections) - y0 + 1; + let side = 2 * radius + 1; + let index = |k: SectionKey| { + let (dx, dz, dy) = (k.0 - from.0, k.2 - from.2, k.1 - y0); + (dx.abs() <= radius && dz.abs() <= radius && (0..height).contains(&dy)) + .then(|| (((dz + radius) * side + dx + radius) * height + dy) as usize) + }; + let size = (side * side * height) as usize; + let mut reached = vec![false; size]; + let start = index(from).unwrap(); + reached[start] = true; + let mut queue = VecDeque::from([(from, NONE, 0u8)]); + while let Some((key, entered, travelled)) = queue.pop_front() { + let vis = columns.get(key); + for (d, step) in STEPS.iter().enumerate() { + let opposite = d ^ 1; + if travelled & (1 << opposite) != 0 { + continue; + } + if entered != NONE && vis & (1 << (usize::from(entered) * 6 + d)) == 0 { + continue; + } + let next = (key.0 + step.0, key.1 + step.1, key.2 + step.2); + if next.1 < -1 || next.1 > columns.sections { + continue; + } + let Some(n) = index(next) else { continue }; + if !reached[n] { + reached[n] = true; + queue.push_back((next, opposite as u8, travelled | (1 << d))); + } + } + } + reached + } + + fn random_world(o: &mut Occlusion, seed: u64, count: usize) { + let mut seed = seed; + let mut next = || { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + seed + }; + for _ in 0..count { + let r = next(); + let key = ( + (r % 13) as i32 - 6, + ((r >> 8) % 6) as i32, + ((r >> 16) % 13) as i32 - 6, + ); + // Open, half open or nearly closed sections. + let vis = match r >> 40 & 3 { + 0 => next() & next() & next(), + 1 => next(), + _ => next() | next(), + }; + o.set(key, vis & ALL_VISIBLE); + } + } + + /// The graph is the search the render thread did before, cell for cell. + #[test] + fn same_sections_as_before() { + let mut hidden = 0; + for seed in [0x9e37_79b9, 7, 0xdead_beef] { + let mut o = Occlusion::new(6); + random_world(&mut o, seed, 900); + for from in [(0, 2, 0), (3, 0, -2), (-5, 5, 4), (1, -1, 1), (0, 7, 0)] { + o.settle(from, 6); + o.settle(from, 6); + let graph = o.graph.as_ref().expect("searched"); + let reached: Vec<bool> = + graph.directions.iter().map(|&d| d & REACHED != 0).collect(); + assert_eq!(reached, reference(&o.columns, from, 6), "{seed} {from:?}"); + hidden += reached.iter().filter(|&&r| !r).count(); + } + } + assert!(hidden > 1000, "the worlds hide sections ({hidden})"); + } + + /// The worker's graph is the one the render thread would compute. + #[test] + fn the_worker_matches_a_search_on_the_render_thread() { + let mut o = Occlusion::new(6); + random_world(&mut o, 0x9e37_79b9, 800); + for from in [(0, 2, 0), (3, 0, -2), (-5, 5, 4)] { + o.settle(from, 6); + o.settle(from, 6); + let mut local = Graph::default(); + local.search(&o.columns, from, 6, &mut Vec::new(), &mut VecDeque::new()); + let graph = o.graph.as_ref().expect("searched"); + assert_eq!(graph.directions, local.directions); + assert_eq!(graph.entered, local.entered); + } + } } diff --git a/crates/horizon-client/src/gfx/timing.rs b/crates/horizon-client/src/gfx/timing.rs index b3daeeaa4..8f8eee6ee 100644 --- a/crates/horizon-client/src/gfx/timing.rs +++ b/crates/horizon-client/src/gfx/timing.rs @@ -1,5 +1,9 @@ //! GPU frame time from timestamp queries, read back a few frames late so the //! CPU never waits for it. Frames whose readback slot is still busy go untimed. +//! +//! Where the GPU can only time pass boundaries (Apple GPUs on Metal), the +//! frame is bracketed by two one-thread compute passes that write the +//! timestamps (Metal leaves an empty pass's timestamps at zero). use std::sync::{ Arc, atomic::{AtomicU8, Ordering}, @@ -23,24 +27,18 @@ pub struct GpuTimer { current: Option<usize>, next: usize, period_ns: f32, + /// Timestamps between commands, or else this pass's boundaries. + marker: Option<wgpu::ComputePipeline>, } impl GpuTimer { - /// The features timing needs; `None` when the adapter lacks them. + /// The features timing uses that the adapter has (none: no timing). pub fn features(adapter: &wgpu::Adapter) -> wgpu::Features { - let wanted = - wgpu::Features::TIMESTAMP_QUERY | wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS; - if adapter.features().contains(wanted) { - wanted - } else { - wgpu::Features::empty() - } + adapter.features() + & (wgpu::Features::TIMESTAMP_QUERY | wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS) } pub fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Option<Self> { - if !device - .features() - .contains(wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS) - { + if !device.features().contains(wgpu::Features::TIMESTAMP_QUERY) { return None; } let queries = device.create_query_set(&wgpu::QuerySetDescriptor { @@ -72,21 +70,56 @@ impl GpuTimer { current: None, next: 0, period_ns: queue.get_timestamp_period(), + marker: (!device + .features() + .contains(wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS)) + .then(|| { + let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("Frame timestamp"), + source: wgpu::ShaderSource::Wgsl( + "@compute @workgroup_size(1) fn mark() {}".into(), + ), + }); + device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { + label: Some("Frame timestamp"), + layout: None, + module: &module, + entry_point: Some("mark"), + compilation_options: Default::default(), + cache: None, + }) + }), }) } pub fn begin(&mut self, encoder: &mut wgpu::CommandEncoder) { let slot = self.next; self.current = (self.slots[slot].state.load(Ordering::Acquire) == FREE).then_some(slot); if self.current.is_some() { - encoder.write_timestamp(&self.queries, 0); + self.stamp(encoder, 0); } } pub fn end(&mut self, encoder: &mut wgpu::CommandEncoder) { let Some(slot) = self.current else { return }; - encoder.write_timestamp(&self.queries, 1); + self.stamp(encoder, 1); encoder.resolve_query_set(&self.queries, 0..2, &self.resolve, 0); encoder.copy_buffer_to_buffer(&self.resolve, 0, &self.slots[slot].buffer, 0, 16); } + fn stamp(&self, encoder: &mut wgpu::CommandEncoder, index: u32) { + let Some(marker) = &self.marker else { + encoder.write_timestamp(&self.queries, index); + return; + }; + let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor { + label: Some("Frame timestamp"), + timestamp_writes: Some(wgpu::ComputePassTimestampWrites { + query_set: &self.queries, + beginning_of_pass_write_index: (index == 0).then_some(0), + end_of_pass_write_index: (index == 1).then_some(1), + }), + }); + pass.set_pipeline(marker); + pass.dispatch_workgroups(1, 1, 1); + } /// After the frame's submission: start reading its timestamps back. pub fn submitted(&mut self) { let Some(slot) = self.current.take() else { @@ -114,7 +147,8 @@ impl GpuTimer { continue; } if let Ok(data) = slot.buffer.slice(..).get_mapped_range() { - let stamps: &[u64] = bytemuck::cast_slice(&data); + // Read unaligned: the mapping isn't guaranteed to be 8-byte aligned. + let stamps: [u64; 2] = bytemuck::pod_read_unaligned(&data[..16]); let ticks = stamps[1].saturating_sub(stamps[0]); out.push((ticks as f64 * f64::from(self.period_ns) / 1e6) as f32); } diff --git a/crates/horizon-client/src/gfx/translucency.rs b/crates/horizon-client/src/gfx/translucency.rs index 927152a39..4c1ad6683 100644 --- a/crates/horizon-client/src/gfx/translucency.rs +++ b/crates/horizon-client/src/gfx/translucency.rs @@ -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 + ); + } } diff --git a/crates/horizon-client/src/gfx/upload.rs b/crates/horizon-client/src/gfx/upload.rs index d42958862..0d56c73a0 100644 --- a/crates/horizon-client/src/gfx/upload.rs +++ b/crates/horizon-client/src/gfx/upload.rs @@ -14,8 +14,13 @@ pub struct Uploads { impl Uploads { pub fn new(device: &wgpu::Device) -> Self { + Self::with_chunk(device, CHUNK) + } + /// A belt whose staging buffers hold `chunk` bytes (larger writes get + /// a buffer of their own). + pub fn with_chunk(device: &wgpu::Device, chunk: u64) -> Self { Self { - belt: wgpu::util::StagingBelt::new(device.clone(), CHUNK), + belt: wgpu::util::StagingBelt::new(device.clone(), chunk), } } /// Copy tightly packed RGBA8 `pixels` (`width` by `height`) into one mip @@ -31,7 +36,9 @@ impl Uploads { let row = width as usize * 4; debug_assert_eq!(pixels.len(), row * height as usize); let padded = row.next_multiple_of(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT as usize); - let size = NonZeroU64::new((padded * height as usize) as u64).expect("non-empty texture"); + let Some(size) = NonZeroU64::new((padded * height as usize) as u64) else { + return; + }; let alignment = NonZeroU64::new(u64::from(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT)) .expect("non-zero alignment"); let slice = self.belt.allocate(size, alignment); diff --git a/crates/horizon-client/src/graphics.rs b/crates/horizon-client/src/graphics.rs index f10c6b3b3..e3ce8fe6e 100644 --- a/crates/horizon-client/src/graphics.rs +++ b/crates/horizon-client/src/graphics.rs @@ -53,7 +53,9 @@ impl Preset { /// How the swapchain presents frames. #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] pub enum PresentMode { - /// Follow vanilla's VSync option (Mailbox or Fifo on, Immediate off). + /// Vanilla's choice: Fifo with VSync on, else the first of Immediate, + /// Mailbox and Fifo the surface supports. The other modes apply only + /// with VSync off: VSync on always presents with Fifo. #[default] Auto, Fifo, @@ -91,6 +93,48 @@ impl PresentMode { } } +/// The graphics API the renderer runs on. Only Windows has a choice: the +/// other systems always use their one backend (Vulkan on Linux, Metal on +/// macOS). Applies at the next start. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] +pub enum GraphicsApi { + /// Vulkan, and DirectX 12 when Vulkan has no adapter or device. + #[default] + Auto, + Vulkan, + Dx12, +} +impl GraphicsApi { + pub const ALL: [GraphicsApi; 3] = [GraphicsApi::Auto, GraphicsApi::Vulkan, GraphicsApi::Dx12]; + pub fn name(self) -> &'static str { + match self { + GraphicsApi::Auto => "auto", + GraphicsApi::Vulkan => "vulkan", + GraphicsApi::Dx12 => "dx12", + } + } + pub fn label(self) -> &'static str { + match self { + GraphicsApi::Auto => "Auto", + GraphicsApi::Vulkan => "Vulkan", + GraphicsApi::Dx12 => "DirectX 12", + } + } + /// The API a name stands for, ignoring case; `None` for anything else. + pub fn from_name(s: &str) -> Option<Self> { + Self::ALL + .into_iter() + .find(|api| api.name().eq_ignore_ascii_case(s.trim())) + } + /// `HORIZON_GRAPHICS_API` (auto, vulkan or dx12) if set, else the setting. + pub fn with_env(self) -> Self { + std::env::var("HORIZON_GRAPHICS_API") + .ok() + .and_then(|v| Self::from_name(&v)) + .unwrap_or(self) + } +} + /// What the frame limiter does in the last half millisecond before a frame. #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] pub enum Pacing { @@ -115,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. @@ -147,11 +193,14 @@ pub struct Graphics { /// The Horizon shader pack in `shaderpacks/` (its folder name); empty: /// none, the vanilla picture (docs/shader-packs.md). pub shader_pack: String, + /// The graphics API (Windows only; see `GraphicsApi`). + pub api: GraphicsApi, } impl Default for Graphics { fn default() -> Self { Self { preset: Preset::Custom, + lod_distance: 0, cave_culling: true, cull_leaves: false, entity_culling: false, @@ -170,6 +219,7 @@ impl Default for Graphics { advanced: false, battery: false, shader_pack: String::new(), + api: GraphicsApi::Auto, } } } @@ -197,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), @@ -220,6 +271,7 @@ impl Graphics { "level" => g.advanced = value == "advanced", "batteryMode" => g.battery = flag(g.battery), "shaderPack" => g.shader_pack = value.to_string(), + "graphicsApi" => g.api = GraphicsApi::from_name(value).unwrap_or_default(), "overlay" => { g.overlay = match value { "fps" => Overlay::Fps, @@ -234,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:{}\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), @@ -261,6 +314,7 @@ impl Graphics { if self.advanced { "advanced" } else { "simple" }, bool_text(self.battery), self.shader_pack, + self.api.name(), ) } pub fn save(&self, dir: &Path) { @@ -271,9 +325,14 @@ impl Graphics { if self.workers > 0 { return self.workers as usize; } + // The pool never takes every core. The render thread and the + // integrated server thread (single player runs on its own server + // thread by default) each need one, and a pool that fills every core + // starves them: the frame rate collapses to single digits while the + // workers saturate the CPU and its memory bandwidth. let auto = std::thread::available_parallelism() .map_or(2, |n| n.get()) - .saturating_sub(1) + .saturating_sub(2) .max(1); if self.battery { auto.div_ceil(2) } else { auto } } @@ -330,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, @@ -348,6 +408,7 @@ mod tests { advanced: true, battery: true, shader_pack: "horizon-sample".into(), + api: GraphicsApi::Dx12, }; assert_eq!(Graphics::parse(&g.serialize()), g); assert_eq!(Graphics::parse(""), Graphics::default()); @@ -357,6 +418,30 @@ 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(); + assert_eq!(g.workers, 0); + let cores = std::thread::available_parallelism().map_or(2, |n| n.get()); + assert_eq!(g.worker_threads(), cores.saturating_sub(2).max(1)); + // A manual count is the player's own choice, kept as asked. + let manual = Graphics { workers: 3, ..g }; + assert_eq!(manual.worker_threads(), 3); + } + #[test] fn battery_mode_only_lowers_the_frame_cap() { let mut g = Graphics::default(); diff --git a/crates/horizon-client/src/graphics_ui.rs b/crates/horizon-client/src/graphics_ui.rs index 745cd5d80..1b9b1cec6 100644 --- a/crates/horizon-client/src/graphics_ui.rs +++ b/crates/horizon-client/src/graphics_ui.rs @@ -2,7 +2,7 @@ //! they show, in what groups, with what description and cost, and their //! input. How they look comes from `graphics_style`. use crate::{ - graphics::{Overlay, Pacing, PresentMode, Preset}, + graphics::{GraphicsApi, Overlay, Pacing, PresentMode, Preset}, graphics_style::{Cost, STYLE}, hud::{HudAtlas, HudBatch}, options_ui::Opt, @@ -21,6 +21,7 @@ pub enum Level { #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum H { Preset, + LodDistance, CaveCulling, CullLeaves, EntityCulling, @@ -38,6 +39,7 @@ pub enum H { Antialiasing, BatteryMode, ShaderPack, + GraphicsApi, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -76,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, @@ -99,11 +106,12 @@ pub fn rows(level: Level) -> Vec<Row> { None, "Sets the options below together. Changing one makes it Custom.", ); - match level { - Level::Simple => vec![preset, render, fov, fps, vsync, gui], + let mut rows = match level { + Level::Simple => vec![preset, render, lod, fov, fps, vsync, gui], Level::Advanced => vec![ Row::Header("Rendering"), render, + lod, v( Opt::SimulationDistance, Medium, @@ -211,7 +219,7 @@ pub fn rows(level: Level) -> Vec<Row> { h( H::PresentMode, None, - "How frames reach the screen. Auto follows VSync.", + "How frames reach the screen while VSync is off.", ), h( H::Pacing, @@ -248,6 +256,11 @@ pub fn rows(level: Level) -> Vec<Row> { None, "Threads that generate, light and mesh chunks. Next start.", ), + h( + H::GraphicsApi, + None, + "Vulkan, or DirectX 12 where Vulkan fails. Next start.", + ), h( H::UploadBudget, Low, @@ -260,7 +273,14 @@ pub fn rows(level: Level) -> Vec<Row> { "Frame rate and frame times over the game, with a graph.", ), ], + }; + // Only Windows has more than one graphics API. + if !cfg!(windows) { + rows.retain( + |row| !matches!(row, Row::Setting(s) if s.item == Item::Horizon(H::GraphicsApi)), + ); } + rows } const UPLOAD_STEPS: [u32; 8] = [0, 256, 512, 1024, 2048, 4096, 8192, 16384]; @@ -311,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)), @@ -342,6 +366,7 @@ impl H { H::ResolutionScale => labelled("Resolution Scale", format!("{}%", g.resolution_scale)), H::Antialiasing => labelled("Anti-aliasing", if g.fxaa { "FXAA" } else { "Off" }), H::BatteryMode => labelled("Battery Mode", on_off(g.battery)), + H::GraphicsApi => labelled("Graphics API", g.api.label()), H::ShaderPack => match g.shader_pack.as_str() { "" => labelled("Shader Pack", "Off"), id => labelled("Shader Pack", id), @@ -383,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, @@ -393,6 +421,7 @@ impl H { H::PresentMode => g.present_mode = step(&PresentMode::ALL, g.present_mode, back), H::Antialiasing => g.fxaa = !g.fxaa, H::BatteryMode => g.battery = !g.battery, + H::GraphicsApi => g.api = step(&GraphicsApi::ALL, g.api, back), H::ShaderPack => { // Off, then every pack in shaderpacks/ by folder name. let dir = std::env::current_dir() @@ -467,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), } @@ -497,6 +530,7 @@ impl Item { | H::Workers | H::BatteryMode | H::ShaderPack + | H::GraphicsApi ) | Item::Vanilla( Opt::GuiScale | Opt::Fullscreen @@ -923,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(); diff --git a/crates/horizon-client/src/health.rs b/crates/horizon-client/src/health.rs index db0677d5f..493488732 100644 --- a/crates/horizon-client/src/health.rs +++ b/crates/horizon-client/src/health.rs @@ -161,6 +161,8 @@ pub struct PlayerHealth { pub ids: PlayerFloatIds, pub food: FoodData, pub hurt: State, + /// Client hurt animation; fully blocked hits only refresh the server's hurt state. + pub hurt_animation_time: i32, pub dead: bool, pub sync_velocity: bool, pub game_time: i64, @@ -210,6 +212,7 @@ impl PlayerHealth { last_source: None, last_stamp: 0, }, + hurt_animation_time: 0, dead: false, sync_velocity: false, game_time: 0, @@ -242,6 +245,9 @@ impl PlayerHealth { if self.hurt.hurt_time > 0 { self.hurt.hurt_time -= 1; } + if self.hurt_animation_time > 0 { + self.hurt_animation_time -= 1; + } } } impl PlayerMotion { @@ -300,8 +306,11 @@ impl PlayerMotion { sound: environment.block_sound, }; if environment.totem_fired { - self.health.totem_used = true; - self.health.totem_ready = false; + h.totem_used = true; + h.totem_ready = false; + } + if outcome.hurt { + h.hurt_animation_time = h.hurt.hurt_time; } Ok(outcome) } diff --git a/crates/horizon-client/src/hud.rs b/crates/horizon-client/src/hud.rs index be4bdb52e..0cc99b07b 100644 --- a/crates/horizon-client/src/hud.rs +++ b/crates/horizon-client/src/hud.rs @@ -977,7 +977,12 @@ impl HudRenderer { capacity, }) } - pub fn animate_fire(&mut self, queue: &wgpu::Queue, tick: u64) { + pub fn animate_fire( + &mut self, + uploads: &mut crate::gfx::upload::Uploads, + encoder: &mut wgpu::CommandEncoder, + tick: u64, + ) { let Some(texture) = &self.atlas_texture else { return; }; @@ -987,7 +992,8 @@ impl HudRenderer { continue; } let pixels = animation.resized_pixels(frame, rect.w, rect.h); - queue.write_texture( + uploads.texture( + encoder, wgpu::TexelCopyTextureInfo { texture, mip_level: 0, @@ -998,35 +1004,27 @@ impl HudRenderer { }, aspect: wgpu::TextureAspect::All, }, + rect.w, + rect.h, &pixels, - wgpu::TexelCopyBufferLayout { - offset: 0, - bytes_per_row: Some(rect.w * 4), - rows_per_image: Some(rect.h), - }, - wgpu::Extent3d { - width: rect.w, - height: rect.h, - depth_or_array_layers: 1, - }, ); *previous = frame; } } - pub fn draw( + /// Stage the batch's atlas patches, vertices and uniform: before the + /// frame's first pass, so the copies share one command buffer. + pub fn prepare( &mut self, device: &wgpu::Device, - queue: &wgpu::Queue, uploads: &mut crate::gfx::upload::Uploads, encoder: &mut wgpu::CommandEncoder, - target: &wgpu::TextureView, - depth: &wgpu::TextureView, batch: &HudBatch, size: [u32; 2], ) { if let Some(texture) = &self.atlas_texture { for p in &batch.patches { - queue.write_texture( + uploads.texture( + encoder, wgpu::TexelCopyTextureInfo { texture, mip_level: 0, @@ -1037,17 +1035,9 @@ impl HudRenderer { }, aspect: wgpu::TextureAspect::All, }, + p.width, + p.height, &p.rgba, - wgpu::TexelCopyBufferLayout { - offset: 0, - bytes_per_row: Some(p.width * 4), - rows_per_image: None, - }, - wgpu::Extent3d { - width: p.width, - height: p.height, - depth_or_array_layers: 1, - }, ); } } @@ -1078,6 +1068,19 @@ impl HudRenderer { size: [size[0] as f32, size[1] as f32, batch.scale, 0.0], }), ); + } + /// Draw the batch `prepare` staged. + pub fn draw( + &self, + encoder: &mut wgpu::CommandEncoder, + target: &wgpu::TextureView, + depth: &wgpu::TextureView, + batch: &HudBatch, + size: [u32; 2], + ) { + if batch.vertices.is_empty() || size.contains(&0) { + return; + } let attachments = [Some(wgpu::RenderPassColorAttachment { view: target, resolve_target: None, diff --git a/crates/horizon-client/src/hunger.rs b/crates/horizon-client/src/hunger.rs index 90b11f18d..fa1b62a5e 100644 --- a/crates/horizon-client/src/hunger.rs +++ b/crates/horizon-client/src/hunger.rs @@ -55,6 +55,7 @@ impl PlayerMotion { data: &mut h.data, ids: h.ids, hurt: &mut h.hurt, + hurt_animation_time: &mut h.hurt_animation_time, dead: &mut h.dead, sync_velocity: &mut h.sync_velocity, game_time: h.game_time, @@ -70,6 +71,7 @@ struct TickWorld<'a> { data: &'a mut SyncedFloats, ids: PlayerFloatIds, hurt: &'a mut State, + hurt_animation_time: &'a mut i32, dead: &'a mut bool, sync_velocity: &'a mut bool, game_time: i64, @@ -142,8 +144,11 @@ impl FoodWorld for TickWorld<'_> { totem: false, totem_fired: false, }; - ServerPlayerCombat::new(self.attributes, self.data, food, self.ids, &mut environment)? - .hurt(self.hurt, amount)?; + if ServerPlayerCombat::new(self.attributes, self.data, food, self.ids, &mut environment)? + .hurt(self.hurt, amount)? + { + *self.hurt_animation_time = self.hurt.hurt_time; + } Ok(()) } } diff --git a/crates/horizon-client/src/interaction_input.rs b/crates/horizon-client/src/interaction_input.rs new file mode 100644 index 000000000..d17cb938c --- /dev/null +++ b/crates/horizon-client/src/interaction_input.rs @@ -0,0 +1,186 @@ +//! The mouse-key portion of Minecraft.handleKeybinds. Events only count clicks; +//! the client tick consumes them before player movement and CLIENT_TICK_END. +#[derive(Default)] +pub struct InteractionInput { + clicks: [u32; 3], +} + +#[derive(Clone, Copy)] +pub enum Button { + Attack, + Use, + Pick, +} + +pub trait InteractionWorld { + fn using_item(&self) -> bool; + fn release_using(&mut self); + fn start_attack(&mut self) -> bool; + fn start_use(&mut self); + fn pick(&mut self); + fn use_delay(&self) -> u32; + fn continue_attack(&mut self, down: bool); +} + +/// Minecraft.startUseItem dispatches one hit result per hand. A passing +/// entity interaction falls back to the item, without trying a block behind it. +pub trait TargetUseWorld { + fn use_entity(&mut self) -> bool; + fn use_block(&mut self) -> bool; + fn use_item(&mut self); +} + +pub fn use_on_target(world: &mut impl TargetUseWorld, entity: bool) { + let consumed = if entity { + world.use_entity() + } else { + world.use_block() + }; + if !consumed { + world.use_item(); + } +} + +impl InteractionInput { + pub fn press(&mut self, button: Button) { + self.clicks[button as usize] += 1; + } + + pub fn tick(&mut self, world: &mut impl InteractionWorld, attack_down: bool, use_down: bool) { + let [attack, use_item, pick] = std::mem::take(&mut self.clicks); + let mut instant = false; + if world.using_item() { + if !use_down { + world.release_using(); + } + // Vanilla drains all three click queues while using an item, + // including the tick that releases it. + } else { + for _ in 0..attack { + instant |= world.start_attack(); + } + for _ in 0..use_item { + world.start_use(); + } + for _ in 0..pick { + world.pick(); + } + } + if use_down && world.use_delay() == 0 && !world.using_item() { + world.start_use(); + } + world.continue_attack(!instant && attack_down); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[derive(Default)] + struct World { + using: bool, + destroying: bool, + delay: u32, + begin_using: bool, + events: Vec<&'static str>, + } + impl InteractionWorld for World { + fn using_item(&self) -> bool { + self.using + } + fn release_using(&mut self) { + self.events.push("release"); + self.using = false; + } + fn start_attack(&mut self) -> bool { + self.events.push("attack"); + false + } + fn start_use(&mut self) { + if !self.destroying { + self.events.push("use"); + self.delay = 4; + self.using = self.begin_using; + } + } + fn pick(&mut self) { + self.events.push("pick"); + } + fn use_delay(&self) -> u32 { + self.delay + } + fn continue_attack(&mut self, _: bool) { + self.events.push("continue"); + } + } + + #[test] + fn quick_clicks_wait_for_a_tick_and_follow_keybind_order() { + let mut input = InteractionInput::default(); + let mut world = World::default(); + input.press(Button::Use); + input.press(Button::Pick); + input.press(Button::Attack); + assert!(world.events.is_empty()); + // Both edges can arrive between ticks; the press still acts once. + input.tick(&mut world, false, false); + assert_eq!(world.events, ["attack", "use", "pick", "continue"]); + world.events.clear(); + input.tick(&mut world, false, false); + assert_eq!( + world.events, + ["continue"], + "no spurious release or repeated click" + ); + } + + #[test] + fn releasing_actual_item_use_discards_clicks_for_that_tick() { + let mut input = InteractionInput::default(); + let mut world = World { + using: true, + ..Default::default() + }; + for button in [Button::Attack, Button::Use, Button::Pick] { + input.press(button); + } + input.tick(&mut world, true, false); + assert_eq!(world.events, ["release", "continue"]); + } + + #[test] + fn holding_use_respects_the_delay_and_active_digging() { + let mut input = InteractionInput::default(); + let mut world = World { + destroying: true, + ..Default::default() + }; + input.press(Button::Use); + input.tick(&mut world, true, true); + assert_eq!(world.events, ["continue"]); + world.destroying = false; + world.events.clear(); + input.tick(&mut world, false, true); + assert_eq!(world.events, ["use", "continue"]); + world.events.clear(); + input.tick(&mut world, false, true); + assert_eq!(world.events, ["continue"]); + } + + #[test] + fn a_quick_use_click_releases_on_the_following_tick() { + let mut input = InteractionInput::default(); + let mut world = World { + begin_using: true, + ..Default::default() + }; + input.press(Button::Use); + input.tick(&mut world, false, false); + assert_eq!(world.events, ["use", "continue"]); + assert!(world.using); + world.events.clear(); + input.tick(&mut world, false, false); + assert_eq!(world.events, ["release", "continue"]); + } +} diff --git a/crates/horizon-client/src/item_data.rs b/crates/horizon-client/src/item_data.rs index d54564b7a..adc4e0f18 100644 --- a/crates/horizon-client/src/item_data.rs +++ b/crates/horizon-client/src/item_data.rs @@ -110,6 +110,16 @@ pub const OMINOUS_LAYERS: [(&str, &str); 8] = [ ("circle", "light_gray"), ("border", "black"), ]; + +/// The raid captain banner's default tooltip setting, stored by vanilla as a +/// component but represented by `StackData::ominous_banner` in Horizon. +fn ominous_tooltip_display() -> Tag { + Tag::compound([( + "hidden_components", + Tag::List(vec![Tag::String("minecraft:banner_patterns".into())]), + )]) +} + /// `WrittenBookContent` with the pages as plain text. #[derive(Clone, Debug, Default, PartialEq)] pub struct WrittenBook { @@ -610,13 +620,9 @@ pub fn components_tag(damage: u32, data: Option<&StackData>) -> Option<Tag> { "minecraft:banner_patterns", Tag::List(OMINOUS_LAYERS.iter().map(|(p, c)| layer(p, c)).collect()), )); - entries.push(( - "minecraft:tooltip_display", - Tag::compound([( - "hidden_components", - Tag::List(vec![Tag::String("minecraft:banner_patterns".into())]), - )]), - )); + if d.other("minecraft:tooltip_display").is_none() { + entries.push(("minecraft:tooltip_display", ominous_tooltip_display())); + } entries.push(( "minecraft:item_name", Tag::compound([( @@ -793,10 +799,8 @@ pub fn decode_components(components: Option<&Tag>) -> (u32, Option<Box<StackData for (k, v) in map { let key = k.to_string_lossy(); let ominous_part = data.ominous_banner - && matches!( - key.as_str(), - "minecraft:item_name" | "minecraft:rarity" | "minecraft:tooltip_display" - ); + && (matches!(key.as_str(), "minecraft:item_name" | "minecraft:rarity") + || (key == "minecraft:tooltip_display" && v == &ominous_tooltip_display())); if !MODELLED.contains(&key.as_str()) && !ominous_part { data.other.push((key, v.clone())); } @@ -882,7 +886,19 @@ mod tests { let tag = encode_components(&banner).unwrap(); assert!(tag.get("minecraft:banner_patterns").is_some()); let (_, back) = decode_components(Some(&tag)); - assert!(back.unwrap().ominous_banner); + let back = back.unwrap(); + assert!(back.ominous_banner); + assert!(back.other("minecraft:tooltip_display").is_none()); + + banner.data_mut().set_other( + "minecraft:tooltip_display", + Some(Tag::Compound(Default::default())), + ); + let (_, custom_display) = decode_components(encode_components(&banner).as_ref()); + assert_eq!( + custom_display.unwrap().other("minecraft:tooltip_display"), + Some(&Tag::Compound(Default::default())) + ); let mut bottle = ItemStack::new("minecraft:ominous_bottle", 1); bottle.data_mut().ominous_amplifier = Some(3); let (_, back) = decode_components(encode_components(&bottle).as_ref()); diff --git a/crates/horizon-client/src/item_tooltip.rs b/crates/horizon-client/src/item_tooltip.rs index 77abbf8cf..114a57ad0 100644 --- a/crates/horizon-client/src/item_tooltip.rs +++ b/crates/horizon-client/src/item_tooltip.rs @@ -4,6 +4,64 @@ use crate::{ hud::rgba, items::{ItemInfo, ItemStack}, }; +use horizon_world::nbt::Tag; + +/// The `minecraft:tooltip_display` component applied by `ItemStack` while it +/// builds a hover tooltip. +pub struct TooltipDisplay<'a> { + component: Option<&'a Tag>, + ominous_banner: bool, +} + +impl<'a> TooltipDisplay<'a> { + pub fn for_stack(stack: &'a ItemStack) -> Self { + Self { + component: stack + .data() + .and_then(|data| data.other("minecraft:tooltip_display")), + ominous_banner: stack.data().is_some_and(|data| data.ominous_banner), + } + } + + fn hides_tooltip(&self) -> bool { + matches!( + self.component.and_then(|display| display.get("hide_tooltip")), + Some(Tag::Byte(value)) if *value != 0 + ) + } + + /// `ItemStack.getTooltipLines`: `hide_tooltip` suppresses the name unless + /// the tooltip flag is creative. + pub fn name_visible(&self, creative: bool) -> bool { + creative || !self.hides_tooltip() + } + + /// `TooltipDisplay.shows`: whether details for a data component are shown. + pub fn shows(&self, component: &str) -> bool { + if self.hides_tooltip() { + return false; + } + // `item_data::decode_components` models the raid captain banner's + // default hidden pattern tooltip as `ominous_banner` instead of + // retaining its redundant `tooltip_display` component. + if self.ominous_banner + && self.component.is_none() + && component == "minecraft:banner_patterns" + { + return false; + } + let Some(Tag::List(hidden)) = self + .component + .and_then(|display| display.get("hidden_components")) + else { + return true; + }; + let component = horizon_world::nbt::JavaString::from(component); + !hidden + .iter() + .any(|tag| matches!(tag, Tag::String(id) if id == &component)) + } +} /// `EquipmentSlotGroup` in declaration order, the order tooltips list them in. const SLOT_GROUPS: [&str; 11] = [ @@ -348,6 +406,49 @@ pub fn lines( mod tests { use super::*; + #[test] + fn tooltip_display_controls_name_and_component_visibility() { + let mut stack = ItemStack::new("minecraft:diamond_sword", 1); + let display = TooltipDisplay::for_stack(&stack); + assert!(display.name_visible(false)); + assert!(display.shows("minecraft:attribute_modifiers")); + + stack.data_mut().set_other( + "minecraft:tooltip_display", + Some(Tag::compound([( + "hidden_components", + Tag::List(vec![Tag::String("minecraft:attribute_modifiers".into())]), + )])), + ); + let display = TooltipDisplay::for_stack(&stack); + assert!(display.name_visible(false)); + assert!(!display.shows("minecraft:attribute_modifiers")); + assert!(display.shows("minecraft:enchantments")); + + stack.data_mut().set_other( + "minecraft:tooltip_display", + Some(Tag::compound([("hide_tooltip", Tag::Byte(1))])), + ); + let display = TooltipDisplay::for_stack(&stack); + assert!(!display.name_visible(false)); + assert!(display.name_visible(true)); + assert!(!display.shows("minecraft:attribute_modifiers")); + } + + #[test] + fn ominous_banner_keeps_its_default_hidden_pattern_tooltip() { + let mut banner = crate::mobs::raider::ominous_banner(); + let display = TooltipDisplay::for_stack(&banner); + assert!(!display.shows("minecraft:banner_patterns")); + + banner.data_mut().set_other( + "minecraft:tooltip_display", + Some(Tag::Compound(Default::default())), + ); + let display = TooltipDisplay::for_stack(&banner); + assert!(display.shows("minecraft:banner_patterns")); + } + #[test] fn ominous_banner_name_and_rarity_use_the_stack() { let translate = |key: &str| match key { diff --git a/crates/horizon-client/src/items.rs b/crates/horizon-client/src/items.rs index 54a505ab9..7f2782c72 100644 --- a/crates/horizon-client/src/items.rs +++ b/crates/horizon-client/src/items.rs @@ -39,6 +39,26 @@ pub struct ToolRule { pub speed: Option<f32>, pub correct: Option<bool>, } +#[derive(Clone, Debug, PartialEq)] +pub struct ConsumeEffectSpec { + pub id: String, + pub duration: i32, + pub amplifier: i32, +} + +#[derive(Clone, Debug, PartialEq)] +pub enum ConsumeEffect { + ApplyEffects { + effects: Vec<ConsumeEffectSpec>, + probability: f32, + }, + ClearAllEffects, + RemoveEffects(Vec<String>), + /// `TeleportRandomlyConsumeEffect`. + TeleportRandomly(horizon_world::teleport_consume::TeleportConsume), + PlaySound(String), +} + #[derive(Clone, Debug)] pub struct ItemInfo { pub fire_resistant: bool, @@ -48,8 +68,15 @@ pub struct ItemInfo { pub block: Option<BlockStateId>, pub tool: Vec<ToolRule>, pub tool_default_speed: f32, + /// The `tool` component's `damage_per_block`, when the item has the + /// component (a mace or trident has one without rules). + pub tool_damage_per_block: Option<u32>, pub food: Option<(u32, f32)>, - pub consumable: bool, + pub can_always_eat: bool, + /// `consumable`: its timing, sound, particles and `on_consume_effects`. + pub consumable: Option<horizon_world::consumable::Consumable<String, ConsumeEffect>>, + /// `use_remainder`: what a finished use leaves (`UseRemainder`). + pub use_remainder: Option<String>, pub model: String, /// Main-hand `attack_damage` / `attack_speed` modifier amounts. pub attack_damage: f32, @@ -421,6 +448,90 @@ impl Items { f["saturation"].as_f64().unwrap_or(0.0) as f32, ) }); + let can_always_eat = c + .get("minecraft:food") + .and_then(|f| f.get("can_always_eat")) + .and_then(|v| v.as_bool()) + .unwrap_or(false); + let mut consume_effects = Vec::new(); + if let Some(effects) = c + .get("minecraft:consumable") + .and_then(|cons| cons.get("on_consume_effects")) + .and_then(|e| e.as_array()) + { + for effect in effects { + match effect.get("type").and_then(|t| t.as_str()) { + Some("minecraft:apply_effects") => { + let probability = effect + .get("probability") + .and_then(|p| p.as_f64()) + .map_or(1.0, |p| p as f32); + let mut specs = Vec::new(); + if let Some(list) = effect.get("effects").and_then(|l| l.as_array()) { + for e in list { + if let Some(id) = e.get("id").and_then(|i| i.as_str()) { + let duration = + e.get("duration").and_then(|d| d.as_i64()).unwrap_or(0) + as i32; + let amplifier = e + .get("amplifier") + .and_then(|a| a.as_i64()) + .unwrap_or(0) + as i32; + specs.push(ConsumeEffectSpec { + id: id.to_string(), + duration, + amplifier, + }); + } + } + } + consume_effects.push(ConsumeEffect::ApplyEffects { + effects: specs, + probability, + }); + } + Some("minecraft:clear_all_effects") => { + consume_effects.push(ConsumeEffect::ClearAllEffects); + } + Some("minecraft:remove_effects") => { + let mut targets = Vec::new(); + if let Some(eff) = effect.get("effects") { + if let Some(s) = eff.as_str() { + targets.push(s.to_string()); + } else if let Some(arr) = eff.as_array() { + for item in arr { + if let Some(s) = item.as_str() { + targets.push(s.to_string()); + } + } + } + } + consume_effects.push(ConsumeEffect::RemoveEffects(targets)); + } + Some("minecraft:teleport_randomly") => { + consume_effects.push(ConsumeEffect::TeleportRandomly( + horizon_world::teleport_consume::TeleportConsume { + diameter: effect + .get("diameter") + .and_then(Value::as_f64) + .map_or(16.0, |d| d as f32), + directional_particles: effect + .get("directional_particles") + .and_then(Value::as_bool) + .unwrap_or(true), + }, + )); + } + Some("minecraft:play_sound") => { + if let Some(sound) = effect.get("sound").and_then(|s| s.as_str()) { + consume_effects.push(ConsumeEffect::PlaySound(sound.to_string())); + } + } + _ => {} + } + } + } items.insert( id.clone(), ItemInfo { @@ -434,8 +545,27 @@ impl Items { tool_default_speed: c["minecraft:tool"]["default_mining_speed"] .as_f64() .unwrap_or(1.0) as f32, + tool_damage_per_block: c + .get("minecraft:tool") + .map(|t| t["damage_per_block"].as_u64().unwrap_or(1) as u32), food, - consumable: c.get("minecraft:consumable").is_some(), + can_always_eat, + // Consumable.CODEC's defaults. + consumable: c.get("minecraft:consumable").map(|cons| { + horizon_world::consumable::Consumable { + seconds: cons["consume_seconds"].as_f64().map_or(1.6, |s| s as f32), + animation: crate::first_person::consumable_animation(cons), + sound: cons["sound"] + .as_str() + .unwrap_or("minecraft:entity.generic.eat") + .to_owned(), + particles: cons["has_consume_particles"].as_bool().unwrap_or(true), + effects: std::mem::take(&mut consume_effects), + } + }), + use_remainder: c["minecraft:use_remainder"]["id"] + .as_str() + .map(str::to_owned), model: c["minecraft:item_model"] .as_str() .unwrap_or(name) @@ -598,6 +728,10 @@ impl Items { songs: HashMap::new(), } } + /// Whether `block` is in the block tag `tag` (`minecraft:fire`, no `#`). + pub fn block_in_tag(&self, tag: &str, block: &str) -> bool { + self.block_tags.get(tag).is_some_and(|s| s.contains(block)) + } fn block_matches(&self, spec: &str, block: &str) -> bool { match spec.strip_prefix('#') { Some(tag) => self.block_tags.get(tag).is_some_and(|s| s.contains(block)), @@ -1496,6 +1630,79 @@ mod tests { .is_empty() ); } + + #[test] + fn always_edible_and_consume_effects_loaded() { + let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../../.local/horizon-client/26.3"); + if !root.join("items.json").exists() { + return; + } + let assets = crate::assets::Assets::load(&root).unwrap(); + let facts = crate::blocks::BlockFacts::load(&root, assets.registry).unwrap(); + let items = Items::load(&root, assets.registry, &facts).unwrap(); + + let apple = items.get("minecraft:golden_apple").expect("golden apple"); + assert!(apple.can_always_eat); + assert!(apple.food.is_some()); + assert!(!apple.consumable.as_ref().unwrap().effects.is_empty()); + + let enchanted = items + .get("minecraft:enchanted_golden_apple") + .expect("enchanted golden apple"); + assert!(enchanted.can_always_eat); + assert!(enchanted.food.is_some()); + assert!(!enchanted.consumable.as_ref().unwrap().effects.is_empty()); + + let beef = items.get("minecraft:cooked_beef").expect("cooked beef"); + assert!(!beef.can_always_eat); + assert!(beef.food.is_some()); + + // Consumables without food, their style and their remainders. + use horizon_world::consumable::UseAnimation; + let milk = items.get("minecraft:milk_bucket").expect("milk bucket"); + let drink = milk.consumable.as_ref().unwrap(); + assert!(milk.food.is_none()); + assert_eq!(drink.effects, [ConsumeEffect::ClearAllEffects]); + assert_eq!((drink.ticks(), drink.animation), (32, UseAnimation::Drink)); + assert_eq!(drink.sound, "minecraft:entity.generic.drink"); + assert!(!drink.particles); + assert_eq!(milk.use_remainder.as_deref(), Some("minecraft:bucket")); + let honey = items.get("minecraft:honey_bottle").expect("honey bottle"); + let sip = honey.consumable.as_ref().unwrap(); + assert_eq!(sip.ticks(), 40); + assert_eq!(sip.sound, "minecraft:item.honey_bottle.drink"); + assert_eq!( + sip.effects, + [ConsumeEffect::RemoveEffects(vec![ + "minecraft:poison".into() + ])] + ); + assert_eq!( + honey.use_remainder.as_deref(), + Some("minecraft:glass_bottle") + ); + let kelp = items.get("minecraft:dried_kelp").unwrap(); + let bite = kelp.consumable.as_ref().unwrap(); + assert_eq!((bite.ticks(), bite.animation), (16, UseAnimation::Eat)); + assert_eq!(bite.sound, "minecraft:entity.generic.eat"); + assert!(bite.particles && kelp.use_remainder.is_none()); + let chorus = items.get("minecraft:chorus_fruit").unwrap(); + assert_eq!( + chorus.consumable.as_ref().unwrap().effects, + [ConsumeEffect::TeleportRandomly( + horizon_world::teleport_consume::TeleportConsume { + diameter: 16.0, + directional_particles: true, + } + )] + ); + let chicken = items.get("minecraft:chicken").unwrap(); + assert!(matches!( + chicken.consumable.as_ref().unwrap().effects[..], + [ConsumeEffect::ApplyEffects { probability, .. }] if probability == 0.3 + )); + } } pub fn take_held_drop(slot: &mut Option<ItemStack>, all: bool) -> Option<ItemStack> { diff --git a/crates/horizon-client/src/lan/advancements.rs b/crates/horizon-client/src/lan/advancements.rs index f3170d485..2e478e2b8 100644 --- a/crates/horizon-client/src/lan/advancements.rs +++ b/crates/horizon-client/src/lan/advancements.rs @@ -4,11 +4,14 @@ //! ClientboundRecipeBookAddPacket) and the advancements the player may see go //! out in ClientboundUpdateAdvancementsPacket (PlayerAdvancements.flushDirty). use super::data::{SyncData, json_to_nbt}; -use crate::{advancements::Tree, recipe_book::RecipeIndex}; +use crate::{ + advancements::{Progress, Tree}, + recipe_book::{RecipeIndex, Settings}, +}; use horizon_protocol::buf::Writer; use serde_json::Value; use std::{ - collections::{BTreeMap, HashMap}, + collections::{BTreeMap, BTreeSet, HashMap}, path::Path, }; @@ -54,8 +57,16 @@ impl ServerAdvancements { } } - /// The first tick of a player without advancements. - pub fn first_tick(&self, sync: &SyncData, now_millis: i64) -> FirstTick { + /// The first tick of a player without advancements: the criteria + /// `minecraft:tick` meets go into `progress` and the recipes their + /// advancements reward into `book`. + pub fn first_tick( + &self, + sync: &SyncData, + now_millis: i64, + progress: &mut Progress, + book: &mut Settings, + ) -> FirstTick { // CriteriaTriggers.TICK: the criteria it meets, and what they complete. let mut obtained: Vec<Vec<&str>> = vec![Vec::new(); self.tree.list.len()]; for (i, a) in self.tree.list.iter().enumerate() { @@ -77,25 +88,24 @@ impl ServerAdvancements { .all(|group| group.iter().any(|c| got.contains(&c.as_str()))) }) .collect(); + let date = crate::advancements::date_text(now_millis.div_euclid(1000)); + for (a, got) in self.tree.list.iter().zip(&obtained) { + for c in got { + progress.grant(&a.id, c, &date); + } + } // AdvancementRewards.grant: the recipes, in the advancements' order. let mut entries = Vec::new(); for (i, a) in self.tree.list.iter().enumerate() { if !done[i] { continue; } + book.add_recipes(&self.displays, &a.rewards.recipes); for id in &a.rewards.recipes { - entries.extend(self.book_entries(id, sync)); + entries.extend(self.book_entries(id, sync, NOTIFY_AND_HIGHLIGHT)); } } - let recipe_book_add = (!entries.is_empty()).then(|| { - let mut w = Writer::new(); - w.varint(entries.len() as i32); - for e in entries { - w.data.extend(e); - } - w.bool(false); // not a replacement - w.data - }); + let recipe_book_add = (!entries.is_empty()).then(|| book_add(entries, false)); // flushDirty: every advancement the player may see, and its progress. let visible = self.tree.visible(&self.kids, |i| done[i]); let shown: Vec<usize> = (0..self.tree.list.len()).filter(|i| visible[*i]).collect(); @@ -103,60 +113,14 @@ impl ServerAdvancements { w.bool(true); // the first packet resets the client's advancements w.varint(shown.len() as i32); for &i in &shown { - let a = &self.tree.list[i]; - w.string(&a.id); - w.bool(a.parent.is_some()); - if let Some(p) = &a.parent { - w.string(p); - } - w.bool(a.display.is_some()); - if let Some(d) = &a.display { - w.nbt(&json_to_nbt(&d.title)); - w.nbt(&json_to_nbt(&d.description)); - w.varint(sync.id("minecraft:item", &d.icon).unwrap_or(0)); - w.varint(1); - w.varint(0); - w.varint(0); - w.varint(match d.frame { - crate::advancements::Frame::Task => 0, - crate::advancements::Frame::Challenge => 1, - crate::advancements::Frame::Goal => 2, - }); - w.i32( - i32::from(d.background.is_some()) - | i32::from(d.show_toast) << 1 - | i32::from(d.hidden) << 2, - ); - if let Some(b) = &d.background { - w.string(b); - } - } - w.varint(a.requirements.len() as i32); - for group in &a.requirements { - w.varint(group.len() as i32); - for c in group { - w.string(c); - } - } - w.bool(false); // sends_telemetry_event - let (x, y) = self.layout[i].unwrap_or((0, 0.0)); - w.f32(x as f32); - w.f32(y); + self.write_advancement(&mut w, i, sync); } w.varint(0); // nothing removed w.varint(shown.len() as i32); for &i in &shown { - let a = &self.tree.list[i]; - w.string(&a.id); - w.varint(a.criteria.len() as i32); - for c in &a.criteria { - w.string(&c.name); - let got = obtained[i].contains(&c.name.as_str()); - w.bool(got); - if got { - w.i64(now_millis); - } - } + self.write_progress(&mut w, i, |c| { + obtained[i].contains(&c).then_some(now_millis) + }); } w.bool(true); // show the advancements FirstTick { @@ -165,9 +129,183 @@ impl ServerAdvancements { } } + /// `PlayerAdvancements.flushDirty`: the advancements that became visible + /// or hidden since `visible` (which it updates; empty before the first + /// packet, which resets the client's and carries everything visible) and + /// the progress of the `changed` ones still visible. None when there is + /// nothing to send. + pub fn update( + &self, + sync: &SyncData, + progress: &Progress, + visible: &mut Vec<bool>, + changed: &BTreeSet<usize>, + show: bool, + ) -> Option<Vec<u8>> { + let n = self.tree.list.len(); + let first = visible.len() != n; + // flushDirty does nothing unless it is the first packet or progress + // changed: visibility follows progress alone, and every grant and + // revoke is in `changed`. This skips walking the whole tree for + // every player every tick. + if !first && changed.is_empty() { + return None; + } + let now = self + .tree + .visible(&self.kids, |i| progress.done(&self.tree.list[i])); + let was = |i: usize| !first && visible[i]; + let added: Vec<usize> = (0..n).filter(|&i| now[i] && !was(i)).collect(); + let removed: Vec<usize> = (0..n).filter(|&i| !now[i] && was(i)).collect(); + let progressed: Vec<usize> = if first { + (0..n).filter(|&i| now[i]).collect() + } else { + changed + .iter() + .copied() + .filter(|&i| i < n && now[i]) + .collect() + }; + *visible = now; + if !first && added.is_empty() && removed.is_empty() && progressed.is_empty() { + return None; + } + let mut w = Writer::new(); + w.bool(first); + w.varint(added.len() as i32); + for &i in &added { + self.write_advancement(&mut w, i, sync); + } + w.varint(removed.len() as i32); + for &i in &removed { + w.string(&self.tree.list[i].id); + } + w.varint(progressed.len() as i32); + for &i in &progressed { + let id = &self.tree.list[i].id; + self.write_progress(&mut w, i, |c| { + progress + .criteria + .get(id) + .and_then(|m| m.get(c)) + .map(|date| crate::advancements::date_millis(date).unwrap_or(0)) + }); + } + w.bool(show); + Some(w.data) + } + + /// Which advancements a player with `progress` may see. + pub fn visibility(&self, progress: &Progress) -> Vec<bool> { + self.tree + .visible(&self.kids, |i| progress.done(&self.tree.list[i])) + } + + /// `ClientboundUpdateAdvancementsPacket.PositionedAdvancement`. + fn write_advancement(&self, w: &mut Writer, i: usize, sync: &SyncData) { + let a = &self.tree.list[i]; + w.string(&a.id); + w.bool(a.parent.is_some()); + if let Some(p) = &a.parent { + w.string(p); + } + w.bool(a.display.is_some()); + if let Some(d) = &a.display { + w.nbt(&json_to_nbt(&d.title)); + w.nbt(&json_to_nbt(&d.description)); + w.varint(sync.id("minecraft:item", &d.icon).unwrap_or(0)); + w.varint(1); + w.varint(0); + w.varint(0); + w.varint(match d.frame { + crate::advancements::Frame::Task => 0, + crate::advancements::Frame::Challenge => 1, + crate::advancements::Frame::Goal => 2, + }); + w.i32( + i32::from(d.background.is_some()) + | i32::from(d.show_toast) << 1 + | i32::from(d.hidden) << 2, + ); + if let Some(b) = &d.background { + w.string(b); + } + } + w.varint(a.requirements.len() as i32); + for group in &a.requirements { + w.varint(group.len() as i32); + for c in group { + w.string(c); + } + } + w.bool(false); // sends_telemetry_event + let (x, y) = self.layout[i].unwrap_or((0, 0.0)); + w.f32(x as f32); + w.f32(y); + } + + /// An `AdvancementProgress`: each criterion and when it was obtained. + fn write_progress(&self, w: &mut Writer, i: usize, obtained: impl Fn(&str) -> Option<i64>) { + let a = &self.tree.list[i]; + w.string(&a.id); + w.varint(a.criteria.len() as i32); + for c in &a.criteria { + w.string(&c.name); + let at = obtained(&c.name); + w.bool(at.is_some()); + if let Some(at) = at { + w.i64(at); + } + } + } + + /// `ServerRecipeBook.addRecipes`' packet for recipes just unlocked: + /// each display, with its notification and highlighted. None when no + /// recipe has a display. + pub fn recipes_added(&self, ids: &[String], sync: &SyncData) -> Option<Vec<u8>> { + let entries: Vec<Vec<u8>> = ids + .iter() + .flat_map(|id| self.book_entries(id, sync, NOTIFY_AND_HIGHLIGHT)) + .collect(); + (!entries.is_empty()).then(|| book_add(entries, false)) + } + + /// `ServerRecipeBook.sendInitialRecipeBook`: every known recipe, without + /// notifications, highlighted while not yet displayed, replacing the + /// client's book. + pub fn initial_recipe_book(&self, book: &Settings, sync: &SyncData) -> Vec<u8> { + let entries: Vec<Vec<u8>> = book + .recipes + .iter() + .flat_map(|id| { + let highlight = if book.to_be_displayed.contains(id) { + 2 + } else { + 0 + }; + self.book_entries(id, sync, highlight) + }) + .collect(); + book_add(entries, true) + } + + /// The recipe's display for ClientboundPlaceGhostRecipePacket (the + /// recipes the book lists). + pub fn ghost_display( + &self, + id: &str, + sync: &SyncData, + ) -> Option<horizon_protocol::packets::recipes::RecipeDisplay> { + let bytes = self.book_entries(id, sync, 0).into_iter().next()?; + let mut r = horizon_protocol::buf::Reader::new(&bytes); + horizon_protocol::packets::recipes::RecipeBookEntry::read(&mut r) + .ok() + .map(|e| e.display) + } + /// ClientboundRecipeBookAddPacket.Entry for each display of a recipe: /// highlighted, and with a notification unless the recipe turns it off. - fn book_entries(&self, id: &str, sync: &SyncData) -> Vec<Vec<u8>> { + fn book_entries(&self, id: &str, sync: &SyncData, flags: i8) -> Vec<Vec<u8>> { let Some(r) = self.recipes.get(id) else { return Vec::new(); }; @@ -245,12 +383,27 @@ impl ServerAdvancements { for v in placed { holder_set(&mut w, v, &item); } + // The notification only where the recipe shows one. let notify = r["show_notification"].as_bool().unwrap_or(true); - w.i8(i8::from(notify) | 2); + w.i8(if notify { flags } else { flags & !1 }); vec![w.data] } } +/// ClientboundRecipeBookAddPacket.Entry flags: notification, highlight. +const NOTIFY_AND_HIGHLIGHT: i8 = 1 | 2; + +/// ClientboundRecipeBookAddPacket: the entries, then whether they replace the book. +fn book_add(entries: Vec<Vec<u8>>, replace: bool) -> Vec<u8> { + let mut w = Writer::new(); + w.varint(entries.len() as i32); + for e in entries { + w.data.extend(e); + } + w.bool(replace); + w.data +} + /// ShapedRecipePattern.shrink: the pattern without blank border rows and columns. fn shrink(rows: &[Value]) -> Vec<Vec<char>> { let rows: Vec<Vec<char>> = rows diff --git a/crates/horizon-client/src/lan/attributes.rs b/crates/horizon-client/src/lan/attributes.rs new file mode 100644 index 000000000..bc208b6f9 --- /dev/null +++ b/crates/horizon-client/src/lan/attributes.rs @@ -0,0 +1,143 @@ +//! `ClientboundUpdateAttributesPacket`: the attribute instances a server syncs +//! to its clients. `ServerEntity.sendPairingData` sends every client-syncable +//! instance an entity has (`AttributeMap.getSyncableAttributes`: the instances +//! created so far, not the type's defaults), `sendDirtyEntityData` the ones +//! changed since (`getAttributesToSync`), to the trackers and the player itself. +use horizon_protocol::packets::play::{AttributeModifier, AttributeSnapshot}; +use horizon_world::attributes::{ + Instance, Operation, + entity::{AttributeId, EntityAttributes}, + live::LiveAttributes, +}; + +/// `AttributeModifier.Operation.id`. +pub fn operation_id(op: Operation) -> i32 { + match op { + Operation::AddValue => 0, + Operation::AddMultipliedBase => 1, + Operation::AddMultipliedTotal => 2, + } +} + +/// An `Operation`'s serialized name (`add_value` ...) as its id. +pub fn operation_named(name: &str) -> i32 { + match name { + "add_multiplied_base" => 1, + "add_multiplied_total" => 2, + _ => 0, + } +} + +/// `AttributeSnapshot`: the base and every modifier, in the order they were added. +pub fn snapshot(attribute: AttributeId, instance: &Instance) -> AttributeSnapshot { + AttributeSnapshot { + attribute: attribute.registry_id(), + base: instance.base_value(), + modifiers: instance + .modifiers() + .iter() + .map(|m| AttributeModifier { + id: m.id().to_owned(), + amount: m.amount(), + operation: operation_id(m.operation()), + }) + .collect(), + } +} + +/// `AttributeMap.getSyncableAttributes` of a live map (a player's). +pub fn syncable<G: FnMut() -> i32>(map: &LiveAttributes<G>) -> Vec<AttributeSnapshot> { + let catalog = map.catalog(); + map.instances() + .filter(|(_, key, _)| catalog.attribute(key.attribute()).syncable) + .map(|(_, key, instance)| snapshot(key.attribute(), instance)) + .collect() +} + +/// `getAttributesToSync`, cleared once read (`ServerEntity.sendDirtyEntityData`). +pub fn take_dirty<G: FnMut() -> i32>(map: &mut LiveAttributes<G>) -> Vec<AttributeSnapshot> { + if map.sync().is_empty() { + return Vec::new(); + } + let handles: Vec<_> = map.sync().values().flatten().cloned().collect(); + map.sync_mut().clear(); + handles + .iter() + .filter_map(|h| { + let key = map.attribute(h).ok()?; + Some(snapshot(key.attribute(), map.instance(h).ok()?)) + }) + .collect() +} + +/// The client-syncable instances of a generic attribute map. +pub fn generic_syncable(map: &EntityAttributes) -> impl Iterator<Item = AttributeSnapshot> + '_ { + let catalog = map.catalog(); + catalog + .attributes() + .filter(|(_, a)| a.syncable) + .filter_map(move |(id, _)| map.instance(id).map(|i| snapshot(id, i))) +} + +/// Adds a modifier to the snapshot of `attribute`, creating it with `base`. +pub fn add_modifier( + out: &mut Vec<AttributeSnapshot>, + attribute: i32, + base: f64, + modifier: AttributeModifier, +) { + let snapshot = instance(out, attribute, base); + match snapshot.modifiers.iter_mut().find(|m| m.id == modifier.id) { + Some(m) => *m = modifier, + None => snapshot.modifiers.push(modifier), + } +} + +/// The snapshot of `attribute`, created with `base` if it isn't there yet. +pub fn instance( + out: &mut Vec<AttributeSnapshot>, + attribute: i32, + base: f64, +) -> &mut AttributeSnapshot { + let i = match out.iter().position(|a| a.attribute == attribute) { + Some(i) => i, + None => { + out.push(AttributeSnapshot { + attribute, + base, + modifiers: Vec::new(), + }); + out.len() - 1 + } + }; + &mut out[i] +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn modifiers_replace_by_id_and_instances_are_created_once() { + let mut out = Vec::new(); + let m = |id: &str, amount| AttributeModifier { + id: id.into(), + amount, + operation: 0, + }; + add_modifier(&mut out, 1, 0.0, m("minecraft:armor.helmet", 3.0)); + add_modifier(&mut out, 1, 5.0, m("minecraft:armor.chestplate", 8.0)); + add_modifier(&mut out, 1, 5.0, m("minecraft:armor.helmet", 2.0)); + assert_eq!(out.len(), 1); + assert_eq!(out[0].base, 0.0); + assert_eq!( + out[0].modifiers, + [ + m("minecraft:armor.helmet", 2.0), + m("minecraft:armor.chestplate", 8.0) + ] + ); + instance(&mut out, 26, 0.7); + assert_eq!(out.len(), 2); + } +} diff --git a/crates/horizon-client/src/lan/chunks.rs b/crates/horizon-client/src/lan/chunks.rs index 6ca321edd..65b092881 100644 --- a/crates/horizon-client/src/lan/chunks.rs +++ b/crates/horizon-client/src/lan/chunks.rs @@ -4,15 +4,24 @@ use crate::{ chunk::{Chunk, Light, MIN_Y, SECTIONS, Section}, containers::BlockEntity, + fast_hash::FxHashMap, }; use horizon_protocol::{ - buf::set_bit, - chunk::{BIOMES, BLOCKS, BlockEntityInfo, ChunkData, LightData, Paletted, Strategy}, + buf::{BitSet, Codec, List, Writer, set_bit}, + chunk::{ + BIOMES, BLOCKS, BlockEntityInfo, ChunkData, Heightmaps, LightData, Paletted, Strategy, + }, packets::play::LevelChunkWithLight, registry::{Builtin, ceil_log2}, }; use horizon_world::nbt::Tag; -use std::{collections::HashMap, sync::Arc}; +use std::{ + collections::HashMap, + sync::{ + Arc, OnceLock, + atomic::{AtomicUsize, Ordering}, + }, +}; const NON_AIR: u8 = 1; const FLUID: u8 = 2; @@ -80,16 +89,295 @@ pub fn block_entity_type(name: &str) -> Option<&'static str> { /// Per-state facts and registry ids for encoding chunks. pub struct ChunkEncoder { - flags: Vec<u8>, + /// What encoding a section needs, shared with the warm-up threads. + coder: Arc<SectionCoder>, /// Block entity type id per state (-1 for none). entity_types: Vec<i16>, - /// Network biome id per internal biome index. - pub biome_ids: Vec<i32>, - biome_bits: u8, /// The block registry id of each state's block, for BlockEvent. pub block_ids: Vec<i32>, /// The save's tables, for the heightmaps. pub tables: Option<Arc<crate::save::SaveTables>>, + /// Sections already looked at, by address (see `Encoded`). + cache: std::sync::Mutex<FxHashMap<usize, Encoded>>, + /// Each column's heightmaps, with the sections they were made from. + heights: std::sync::Mutex<HashMap<(i32, i32), Heights>>, + /// The level's sections among a column's: all of them in the Overworld, + /// y 0 to 255 in the Nether and the End (`DimensionType` min_y and height). + sections: std::ops::Range<usize>, + /// Whether the level has sky light (`DimensionType.hasSkyLight`). + sky: bool, +} + +/// Per-state flags and biome ids: all a section's encoding reads. +struct SectionCoder { + flags: Vec<u8>, + /// Network biome id per internal biome index. + biome_ids: Vec<i32>, + biome_bits: u8, +} + +/// A column's client heightmaps, valid while it holds the same sections. +struct Heights { + sections: Vec<std::sync::Weak<Section>>, + maps: ColumnHeightmaps, +} + +/// Heightmap type ids with their packed long arrays. +type ColumnHeightmaps = Arc<Vec<(i32, Vec<i64>)>>; + +/// A column's cached heightmaps, while it holds the sections they were made +/// from. +fn cached_heights( + heights: &HashMap<(i32, i32), Heights>, + chunk: &Chunk, +) -> Option<ColumnHeightmaps> { + let h = heights.get(&(chunk.x, chunk.z))?; + let same = h.sections.len() == chunk.sections.len() + && h.sections + .iter() + .zip(&chunk.sections) + .all(|(w, s)| std::ptr::eq(w.as_ptr(), Arc::as_ptr(s))); + same.then(|| h.maps.clone()) +} + +/// Cache a column's heightmaps with the sections they were made from, +/// dropping the columns whose sections are gone once there are many. +fn keep_heights( + heights: &mut HashMap<(i32, i32), Heights>, + key: (i32, i32), + sections: &[Arc<Section>], + maps: &ColumnHeightmaps, +) { + if heights.len() >= CACHE_SWEEP / 4 { + heights.retain(|_, h| h.sections.iter().all(|w| w.strong_count() > 0)); + if heights.len() >= CACHE_SWEEP / 8 { + heights.clear(); + } + } + heights.insert( + key, + Heights { + sections: sections.iter().map(Arc::downgrade).collect(), + maps: maps.clone(), + }, + ); +} + +/// What a resident section gives the wire, kept while the section is +/// unchanged. Resident sections are immutable behind `Arc` (edits go through +/// `Arc::make_mut`, which moves an edited section to a new address while a +/// `Weak` points at the old one), and the `Weak` keeps the old address from +/// being reused: the same address is the same, unchanged section. +struct Encoded { + section: std::sync::Weak<Section>, + non_air: bool, + /// LevelChunkSection.write, and its block entity blocks. + wire: Option<(Arc<[u8]>, Entities)>, + /// The section's light as the wire's two DataLayers, when not uniform. + light: Option<Arc<Layers>>, +} + +/// A section's blocks that have a block entity type, as (index, state) in +/// index order (y, then z, then x). +type Entities = Arc<[(u16, u32)]>; + +/// One section's light as DataLayers (two blocks per byte, the even one in +/// the low nibble), and whether each holds any light. +struct Layers { + sky: Vec<u8>, + block: Vec<u8>, + sky_any: bool, + block_any: bool, +} +impl Layers { + fn of(light: &Light) -> Self { + let (mut sky, mut block) = (vec![0u8; 2048], vec![0u8; 2048]); + let (sky_any, block_any) = match light { + Light::Uniform(v) => { + let (sk, bl) = (v >> 4, v & 15); + sky.fill(sk | sk << 4); + block.fill(bl | bl << 4); + (sk != 0, bl != 0) + } + Light::Full(a) => { + for ((pair, sky), block) in + a.as_chunks::<2>().0.iter().zip(&mut sky).zip(&mut block) + { + let [even, odd] = *pair; + *sky = even >> 4 | (odd & 0xF0); + *block = (even & 15) | odd << 4; + } + let all = a.iter().fold(0u8, |acc, &v| acc | v); + (all & 0xF0 != 0, all & 15 != 0) + } + }; + Self { + sky, + block, + sky_any, + block_any, + } + } +} + +impl SectionCoder { + /// LevelChunkSection.write, and its block entity blocks. + fn wire(&self, section: &Section) -> (Arc<[u8]>, Entities) { + let (encoded, entities) = self.section(section); + let mut w = horizon_protocol::buf::Writer::new(); + encoded.write(&mut w); + (w.data.into(), entities.into()) + } + + fn section(&self, section: &Section) -> (horizon_protocol::chunk::Section, Vec<(u16, u32)>) { + let mut values = Vec::with_capacity(BLOCKS); + let (mut non_empty, mut fluids, mut entities) = (0i16, 0i16, Vec::new()); + let mut count = |state: u32, n: i16| { + let f = self.flags.get(state as usize).copied().unwrap_or(0); + if f & NON_AIR != 0 { + non_empty += n; + if f & FLUID != 0 { + fluids += n; + } + } + f & ENTITY != 0 + }; + // One block everywhere (all air, mostly): what from_values would give. + let uniform = section.blocks.uniform(); + if let Some(v) = uniform { + if count(v, BLOCKS as i16) { + entities = (0..BLOCKS as u16).map(|i| (i, v)).collect(); + } + } else { + let mut unpacked = [0u32; BLOCKS]; + section.unpack(&mut unpacked); + for (i, &v) in unpacked.iter().enumerate() { + if count(v, 1) { + entities.push((i as u16, v)); + } + values.push(v as i32); + } + } + let biome_values: Vec<i32> = (0..BIOMES) + .map(|j| { + let internal = section.biomes.get(j) as u16; + self.biome_ids + .get(usize::from(internal)) + .copied() + .unwrap_or(0) + }) + .collect(); + ( + horizon_protocol::chunk::Section { + non_empty, + fluids, + states: match uniform { + Some(v) => Paletted { + bits: 0, + palette: vec![v as i32], + data: Vec::new(), + }, + None => Paletted::from_values( + &values, + Strategy::Blocks, + crate::chunk::block_strategy().global_bits, + ), + }, + biomes: Paletted::from_values(&biome_values, Strategy::Biomes, self.biome_bits), + }, + entities, + ) + } +} + +/// Sections kept before the ones no longer resident are dropped. +const CACHE_SWEEP: usize = 1 << 15; + +/// Uncached sections below which a batch is encoded on the tick's thread +/// alone: waking the warm-up threads costs more than a few sections. +const WARM_MIN: usize = 16; + +/// The work a batch needs, each piece taken by whichever thread is free +/// first: the heightmaps of its columns not cached yet, then its sections +/// not cached yet, and what each gave (a section: its wire bytes and, with +/// varying light, its layers). +struct Warm { + coder: Arc<SectionCoder>, + /// The heightmaps' tables and the level's (min_y, height). + heightmaps: Option<(Arc<crate::save::SaveTables>, i32, i32)>, + columns: Vec<((i32, i32), Vec<Arc<Section>>)>, + heights: Vec<OnceLock<ColumnHeightmaps>>, + sections: Vec<Arc<Section>>, + next: AtomicUsize, + done: Vec<OnceLock<Warmed>>, +} +type Warmed = ((Arc<[u8]>, Entities), Option<Arc<Layers>>); +impl Warm { + /// Take work until none is left. + fn run(&self) { + loop { + let i = self.next.fetch_add(1, Ordering::Relaxed); + if let Some((_, sections)) = self.columns.get(i) { + if let Some((tables, min_y, height)) = &self.heightmaps { + let maps = crate::save::client_heightmaps_in(sections, tables, *min_y, *height); + let _ = self.heights[i].set(Arc::new(maps)); + } + continue; + } + let i = i - self.columns.len(); + let Some(s) = self.sections.get(i) else { + return; + }; + let light = matches!(s.light, Light::Full(_)).then(|| Arc::new(Layers::of(&s.light))); + let _ = self.done[i].set((self.coder.wire(s), light)); + } + } +} + +/// The warm-up threads (one per core besides the tick's) and the job they +/// take: its number, and the job while it has sections left to take. +struct WarmPool { + job: std::sync::Mutex<(u64, Option<Arc<Warm>>)>, + ready: std::sync::Condvar, +} + +/// The warm-up threads, started the first time; none on a single core or +/// when no thread could start. +fn warm_pool() -> Option<&'static WarmPool> { + static POOL: OnceLock<Option<Arc<WarmPool>>> = OnceLock::new(); + POOL.get_or_init(|| { + let threads = std::thread::available_parallelism().map_or(1, |n| n.get()) - 1; + let pool = Arc::new(WarmPool { + job: Default::default(), + ready: Default::default(), + }); + let started = (0..threads) + .filter(|i| { + let pool = pool.clone(); + std::thread::Builder::new() + .name(format!("horizon-chunk-encode-{i}")) + .spawn(move || { + let mut done = 0; + loop { + let job = { + let mut slot = pool.job.lock().unwrap(); + while slot.0 == done { + slot = pool.ready.wait(slot).unwrap(); + } + done = slot.0; + slot.1.clone() + }; + if let Some(job) = job { + job.run(); + } + } + }) + .is_ok() + }) + .count(); + (started > 0).then_some(pool) + }) + .as_deref() } impl ChunkEncoder { /// `biome_names`: the internal biome list in order; `biome_registry`: @@ -137,85 +425,139 @@ impl ChunkEncoder { }) .collect(); Self { - flags, + coder: Arc::new(SectionCoder { + flags, + biome_ids, + biome_bits: ceil_log2(biome_registry.len()), + }), entity_types, - biome_ids, - biome_bits: ceil_log2(biome_registry.len()), block_ids, tables: None, + cache: Default::default(), + heights: Default::default(), + sections: 0..SECTIONS, + sky: true, } } - pub fn entity_type(&self, state: u16) -> Option<i32> { + + /// The client heightmaps (Heightmap.prime over the column), from the + /// cache while the column's sections are unchanged. + fn heightmaps(&self, chunk: &Chunk, tables: &crate::save::SaveTables) -> ColumnHeightmaps { + let mut heights = self.heights.lock().unwrap(); + if let Some(maps) = cached_heights(&heights, chunk) { + return maps; + } + let (min_y, height) = self.level(); + let maps = Arc::new(crate::save::client_heightmaps_in( + &chunk.sections, + tables, + min_y, + height, + )); + keep_heights(&mut heights, (chunk.x, chunk.z), &chunk.sections, &maps); + maps + } + + /// The level's min_y and height (`DimensionType`), the heightmaps' range. + fn level(&self) -> (i32, i32) { + ( + MIN_Y + 16 * self.sections.start as i32, + 16 * self.sections.len() as i32, + ) + } + + /// The cached entry for a section, made with `non_air` when missing. + fn cached<T>(&self, section: &Arc<Section>, read: impl FnOnce(&mut Encoded) -> T) -> T { + read(self.entry(&mut self.cache.lock().unwrap(), section)) + } + + /// A section's entry in the locked cache, made with `non_air` when missing. + fn entry<'a>( + &self, + cache: &'a mut FxHashMap<usize, Encoded>, + section: &Arc<Section>, + ) -> &'a mut Encoded { + if cache.len() >= CACHE_SWEEP { + cache.retain(|_, e| e.section.strong_count() > 0); + if cache.len() >= CACHE_SWEEP / 2 { + cache.clear(); + } + } + cache + .entry(Arc::as_ptr(section) as usize) + .or_insert_with(|| Encoded { + section: Arc::downgrade(section), + non_air: self.non_air(section), + wire: None, + light: None, + }) + } + + /// LevelChunkSection.hasOnlyAir, negated. + fn non_air(&self, section: &Section) -> bool { + let non_air = + |v: u32| self.coder.flags.get(v as usize).copied().unwrap_or(0) & NON_AIR != 0; + // The palette first: most sections are all air, or one block. + if !section.blocks.maybe_has(non_air) { + false + } else if let Some(v) = section.blocks.uniform() { + non_air(v) + } else { + // Stops at the first block, usually near the bottom. + (0..BLOCKS).any(|i| non_air(section.blocks.get(i))) + } + } + + /// A section on the wire, from the cache or encoded now. + fn section_wire(&self, section: &Arc<Section>) -> (Arc<[u8]>, Entities) { + if let Some(wire) = self.cached(section, |e| e.wire.clone()) { + return wire; + } + let wire = self.coder.wire(section); + self.cached(section, |e| e.wire = Some(wire.clone())); + wire + } + /// Encode for a level of `min_y` and `height` (multiples of 16 within + /// the column), with or without sky light. + pub fn set_dimension(&mut self, min_y: i32, height: i32, sky: bool) { + let lo = usize::try_from((min_y - MIN_Y) >> 4) + .unwrap_or(0) + .min(SECTIONS); + let n = usize::try_from(height >> 4).unwrap_or(SECTIONS); + self.sections = lo..(lo + n).min(SECTIONS); + self.sky = sky; + // The heightmaps depend on the level's height. + self.heights.get_mut().unwrap().clear(); + } + pub fn entity_type(&self, state: u32) -> Option<i32> { self.entity_types - .get(usize::from(state)) + .get(state as usize) .filter(|t| **t >= 0) .map(|t| i32::from(*t)) } - pub fn is_air(&self, state: u16) -> bool { - self.flags - .get(usize::from(state)) + pub fn is_air(&self, state: u32) -> bool { + self.coder + .flags + .get(state as usize) .is_none_or(|f| f & NON_AIR == 0) } - fn section(&self, section: &Section) -> (horizon_protocol::chunk::Section, bool) { - let mut values = Vec::with_capacity(BLOCKS); - let (mut non_empty, mut fluids, mut entities) = (0i16, 0i16, false); - let mut count = |state: u16, n: i16| { - let f = self.flags.get(usize::from(state)).copied().unwrap_or(0); - if f & NON_AIR != 0 { - non_empty += n; - if f & FLUID != 0 { - fluids += n; - } - } - f & ENTITY != 0 - }; - let mut unpacked = [0u16; BLOCKS]; - section.unpack(&mut unpacked); - for &v in &unpacked { - entities |= count(v, 1); - values.push(i32::from(v)); - } - let biome_values: Vec<i32> = (0..BIOMES) - .map(|j| { - let internal = section.biomes.get(j) as u16; - self.biome_ids - .get(usize::from(internal)) - .copied() - .unwrap_or(0) - }) - .collect(); - ( - horizon_protocol::chunk::Section { - non_empty, - fluids, - states: Paletted::from_values(&values, Strategy::Blocks, 16), - biomes: Paletted::from_values(&biome_values, Strategy::Biomes, self.biome_bits), - }, - entities, - ) - } - /// The chunk column with its light and block entities. /// The non-air sections of a column as bits (LevelChunkSection.hasOnlyAir). fn occupied(&self, chunk: &Chunk) -> u64 { let mut bits = 0u64; - for (i, s) in chunk.sections.iter().enumerate() { - let mut unpacked = [0u16; BLOCKS]; - s.unpack(&mut unpacked); - let any = unpacked - .iter() - .any(|&v| self.flags.get(usize::from(v)).copied().unwrap_or(0) & NON_AIR != 0); - if any { + // One lock for the column: body_in asks for nine columns' sections. + let mut cache = self.cache.lock().unwrap(); + for (i, s) in chunk.sections[self.sections.clone()].iter().enumerate() { + if self.entry(&mut cache, s).non_air { bits |= 1 << i; } } bits } - /// The chunk column with its light and block entities. `world` gives the - /// neighbouring columns, whose sections decide which light layers exist. - pub fn encode_in(&self, chunk: &Chunk, world: &crate::world::World) -> LevelChunkWithLight { + /// The non-air sections of the column and its neighbours, as bits. + fn around(&self, chunk: &Chunk, world: &crate::world::World) -> u64 { let mut around = 0u64; for dz in -1..=1 { for dx in -1..=1 { @@ -224,9 +566,180 @@ impl ChunkEncoder { } } } - let mut packet = self.encode(chunk, &world.block_entities); - packet.light = light_data(chunk, around); - packet + around + } + + /// The chunk column with its light and block entities, as the body + /// `Clientbound::encode` gives for its LevelChunkWithLight. `world` gives + /// the neighbouring columns, whose sections decide which light layers + /// exist. It is written once into a buffer of its final size: the cached + /// sections and light layers go straight to it, with no packet built and + /// copied again first. + pub fn body_in(&self, chunk: &Chunk, world: &crate::world::World) -> Vec<u8> { + self.body(chunk, &world.block_entities, self.around(chunk, world)) + } + + /// Make what these columns need and is not cached yet (their heightmaps + /// and sections) before their bodies are made, shared out to the warm-up + /// threads: a batch of new terrain costs mostly its sections' first + /// encoding. This thread takes work too until none is left, then caches + /// what is done; it never waits for the other threads (they keep their + /// results to themselves until then), and what one of them is still on + /// is made again by `body_in`, the same. + pub fn warm(&self, columns: &[&Chunk]) { + let Some(job) = self.job(columns) else { + return; + }; + let Some(pool) = warm_pool() else { + return; + }; + let job = Arc::new(job); + { + let mut slot = pool.job.lock().unwrap(); + slot.0 += 1; + slot.1 = Some(job.clone()); + } + pool.ready.notify_all(); + job.run(); + // Everything is claimed: the job leaves the slot. + { + let mut slot = pool.job.lock().unwrap(); + if slot.1.as_ref().is_some_and(|j| Arc::ptr_eq(j, &job)) { + slot.1 = None; + } + } + self.keep(&job); + } + + /// The work `warm` shares out: the heightmaps of the columns and the + /// sections not cached yet; none when there is too little to wake the + /// warm-up threads for. + fn job(&self, columns: &[&Chunk]) -> Option<Warm> { + let stale: Vec<((i32, i32), Vec<Arc<Section>>)> = match &self.tables { + Some(_) => { + let heights = self.heights.lock().unwrap(); + let mut seen = crate::fast_hash::FxHashSet::default(); + columns + .iter() + .filter(|c| seen.insert((c.x, c.z)) && cached_heights(&heights, c).is_none()) + .map(|c| ((c.x, c.z), c.sections.clone())) + .collect() + } + None => Vec::new(), + }; + let sections: Vec<Arc<Section>> = { + let cache = self.cache.lock().unwrap(); + columns + .iter() + .flat_map(|c| &c.sections[self.sections.clone()]) + .filter(|s| { + cache + .get(&(Arc::as_ptr(s) as usize)) + .is_none_or(|e| e.wire.is_none()) + }) + .cloned() + .collect() + }; + if sections.len() + stale.len() < WARM_MIN { + return None; + } + let (min_y, height) = self.level(); + Some(Warm { + coder: self.coder.clone(), + heightmaps: self.tables.clone().map(|t| (t, min_y, height)), + heights: stale.iter().map(|_| OnceLock::new()).collect(), + columns: stale, + done: sections.iter().map(|_| OnceLock::new()).collect(), + sections, + next: AtomicUsize::new(0), + }) + } + + /// Cache what a job's threads have finished. + fn keep(&self, job: &Warm) { + { + let mut heights = self.heights.lock().unwrap(); + for ((key, sections), done) in job.columns.iter().zip(&job.heights) { + if let Some(maps) = done.get() { + keep_heights(&mut heights, *key, sections, maps); + } + } + } + let mut cache = self.cache.lock().unwrap(); + for (s, done) in job.sections.iter().zip(&job.done) { + if let Some((wire, light)) = done.get() { + let e = self.entry(&mut cache, s); + e.wire = Some(wire.clone()); + if light.is_some() { + e.light = light.clone(); + } + } + } + } + + fn body( + &self, + chunk: &Chunk, + block_entities: &HashMap<(i32, i32, i32), BlockEntity>, + around: u64, + ) -> Vec<u8> { + let (wires, entities) = self.blocks(chunk, block_entities); + let heightmaps = self.heightmaps_of(chunk); + let lights = self.light_sections(chunk, around); + let buffer: usize = wires.iter().map(|w| w.len()).sum(); + // Before the light: their tags' size is only known once written. + let mut tags = Writer::new(); + List::<BlockEntityInfo>::write(&entities, &mut tags); + // The masks, counts and lengths fit the 64 bytes. + let size = 64 + + heightmaps + .iter() + .map(|(_, h)| 10 + 8 * h.len()) + .sum::<usize>() + + buffer + + tags.data.len() + + lights.len() * 2 * (2048 + 3); + let mut w = Writer { + data: Vec::with_capacity(size), + }; + w.varint(horizon_protocol::ids::play::clientbound::LEVEL_CHUNK_WITH_LIGHT); + w.i32(chunk.x); + w.i32(chunk.z); + Heightmaps::write(&*heightmaps, &mut w); + w.varint(buffer as i32); + for wire in &wires { + w.bytes(wire); + } + w.bytes(&tags.data); + write_light(&lights, self.sky, &mut w); + w.data + } + + /// ClientboundLightUpdatePacket for the light sections over `changed` (a + /// bit per section of the column, `sectionLightChanged`), as vanilla's + /// `ClientboundLightUpdatePacketData` writes them, with both kinds of + /// each section (Horizon's light engine doesn't tell them apart; the + /// client keeps the same light either way). A section changed as its + /// last block went (light dimming around a removed block) is sent too: + /// a client not tracking the block would otherwise keep its old light. + pub fn light_update(&self, chunk: &Chunk, changed: u32) -> Vec<u8> { + let first = self.sections.start; + let lights: Vec<(usize, SectionLight)> = self + .light_sections(chunk, u64::MAX) + .into_iter() + .filter(|&(b, _)| { + // Light section b holds the column's section first + b - 1. + (1..=self.sections.len()).contains(&b) && changed >> (first + b - 1) & 1 != 0 + }) + .collect(); + horizon_protocol::packets::play::Clientbound::LightUpdate( + horizon_protocol::packets::play::LightUpdate { + x: chunk.x, + z: chunk.z, + light: light_data_of(&lights, self.sky), + }, + ) + .encode() } pub fn encode( @@ -234,34 +747,86 @@ impl ChunkEncoder { chunk: &Chunk, block_entities: &HashMap<(i32, i32, i32), BlockEntity>, ) -> LevelChunkWithLight { - let mut sections = Vec::with_capacity(SECTIONS); + let mut packet = self.encode_blocks(chunk, block_entities); + packet.light = self.light_data(chunk, u64::MAX); + packet + } + + /// The packet without its light. + fn encode_blocks( + &self, + chunk: &Chunk, + block_entities: &HashMap<(i32, i32, i32), BlockEntity>, + ) -> LevelChunkWithLight { + let (wires, block_entities) = self.blocks(chunk, block_entities); + LevelChunkWithLight { + x: chunk.x, + z: chunk.z, + chunk: ChunkData { + heightmaps: self.heightmaps_of(chunk).to_vec(), + buffer: wires.concat(), + block_entities, + }, + light: LightData { + sky_mask: Vec::new(), + block_mask: Vec::new(), + empty_sky_mask: Vec::new(), + empty_block_mask: Vec::new(), + sky: Vec::new(), + block: Vec::new(), + }, + } + } + + fn heightmaps_of(&self, chunk: &Chunk) -> ColumnHeightmaps { + self.tables + .as_ref() + .map(|t| self.heightmaps(chunk, t)) + .unwrap_or_default() + } + + /// The level's sections on the wire, and the column's block entities in + /// the packet's order. + fn blocks( + &self, + chunk: &Chunk, + block_entities: &HashMap<(i32, i32, i32), BlockEntity>, + ) -> (Vec<Arc<[u8]>>, Vec<BlockEntityInfo>) { + let mut wires = Vec::with_capacity(self.sections.len()); let mut entity_infos = Vec::new(); - for (i, s) in chunk.sections.iter().enumerate() { - let (encoded, has_entities) = self.section(s); - if has_entities { - for y in 0..16 { - for z in 0..16 { - for x in 0..16 { - let state = s.get(crate::chunk::index(x, y, z)); - if let Some(kind) = self.entity_type(state) { - let wy = MIN_Y + (i as i32) * 16 + y; - let pos = (chunk.x * 16 + x, wy, chunk.z * 16 + z); - entity_infos.push(BlockEntityInfo { - packed_xz: ((x << 4) | z) as i8, - y: wy as i16, - kind, - // BlockEntityInfo.create: an empty update tag is not sent. - tag: block_entities - .get(&pos) - .and_then(entity_tag) - .unwrap_or(Tag::End), - }); - } - } - } - } + for (i, s) in chunk + .sections + .iter() + .enumerate() + .skip(self.sections.start) + .take(self.sections.len()) + { + let (encoded, entities) = self.section_wire(s); + // The section's block entity blocks, kept with its bytes, in the + // order a scan of every block would find them. + for &(index, state) in entities.iter() { + let Some(kind) = self.entity_type(state) else { + continue; + }; + let (x, y, z) = ( + i32::from(index & 15), + i32::from(index >> 8), + i32::from(index >> 4 & 15), + ); + let wy = MIN_Y + (i as i32) * 16 + y; + let pos = (chunk.x * 16 + x, wy, chunk.z * 16 + z); + entity_infos.push(BlockEntityInfo { + packed_xz: ((x << 4) | z) as i8, + y: wy as i16, + kind, + // BlockEntityInfo.create: an empty update tag is not sent. + tag: block_entities + .get(&pos) + .and_then(entity_tag) + .unwrap_or(Tag::End), + }); } - sections.push(encoded); + wires.push(encoded); } // A loaded chunk's block entities enter its maps in the save's order // (ChunkAccess.pendingBlockEntities, then LevelChunk.blockEntities): @@ -286,20 +851,29 @@ impl ChunkEncoder { chunk.z * 16 + i32::from(e.packed_xz as u8 & 15), ) }); - LevelChunkWithLight { - x: chunk.x, - z: chunk.z, - chunk: ChunkData { - heightmaps: self - .tables - .as_ref() - .map(|t| crate::save::client_heightmaps(chunk, t)) - .unwrap_or_default(), - buffer: ChunkData::from_sections(§ions), - block_entities: entity_infos, - }, - light: light_data(chunk, u64::MAX), - } + (wires, entity_infos) + } + + /// ClientboundLightUpdatePacketData: the light layers of the sections + /// that exist around the column (`around`: the non-air sections of it + /// and its neighbours, as bits). A resident section's layers come from + /// the cache while it is unchanged (uniform light is quicker to fill than + /// to keep). + fn light_data(&self, chunk: &Chunk, around: u64) -> LightData { + light_data_of(&self.light_sections(chunk, around), self.sky) + } + + fn light_sections(&self, chunk: &Chunk, around: u64) -> Vec<(usize, SectionLight)> { + light_sections(&chunk.sections[self.sections.clone()], around, |s| { + if let Light::Uniform(v) = s.light { + return SectionLight::Uniform(v); + } + SectionLight::Layers(self.cached(s, |e| e.light.clone()).unwrap_or_else(|| { + let layers = Arc::new(Layers::of(&s.light)); + self.cached(s, |e| e.light = Some(layers.clone())); + layers + })) + }) } } @@ -373,70 +947,125 @@ pub fn entity_tag(entity: &BlockEntity) -> Option<Tag> { /// non-air section of this column or a neighbouring one (`around`, bit i for /// section i; LayerLightSectionStorage.updateSectionStatus). An existing /// layer is sent, or marked empty when all dark; the others are left out. -fn light_data(chunk: &Chunk, around: u64) -> LightData { - let mut data = LightData { - sky_mask: Vec::new(), - block_mask: Vec::new(), - empty_sky_mask: Vec::new(), - empty_block_mask: Vec::new(), - sky: Vec::new(), - block: Vec::new(), - }; - // Light section b holds chunk section b - 1. - let exists = |b: usize| { - (b.saturating_sub(2)..=b.min(SECTIONS - 1)).any(|s| s + 2 >= b && around >> s & 1 != 0) - }; - let full_sky = Light::Uniform(0xF0); - for b in 0..SECTIONS + 2 { - if !exists(b) { - continue; - } - let light = match b { - // Below the world nothing is lit; above it the sky is full. - 0 => &Light::Uniform(0), - _ if b == SECTIONS + 1 => &full_sky, - _ => &chunk.sections[b - 1].light, - }; - let (mut sky, mut block) = (vec![0u8; 2048], vec![0u8; 2048]); - let (mut sky_any, mut block_any) = (false, false); - match light { - Light::Uniform(v) => { - let (sk, bl) = (v >> 4, v & 15); - sky_any = sk != 0; - block_any = bl != 0; - let (sb, bb) = (sk | sk << 4, bl | bl << 4); - sky.fill(sb); - block.fill(bb); - } - Light::Full(a) => { - for (j, &v) in a.iter().enumerate() { - let (sk, bl) = (v >> 4, v & 15); - sky_any |= sk != 0; - block_any |= bl != 0; - if j & 1 == 0 { - sky[j >> 1] |= sk; - block[j >> 1] |= bl; - } else { - sky[j >> 1] |= sk << 4; - block[j >> 1] |= bl << 4; - } - } - } - } - if sky_any { - set_bit(&mut data.sky_mask, b); - data.sky.push(sky); - } else { - set_bit(&mut data.empty_sky_mask, b); - } - if block_any { - set_bit(&mut data.block_mask, b); - data.block.push(block); - } else { - set_bit(&mut data.empty_block_mask, b); +#[cfg(test)] +fn light_data(sections: &[Arc<Section>], around: u64) -> LightData { + let lights = light_sections(sections, around, |s| { + SectionLight::Layers(Arc::new(Layers::of(&s.light))) + }); + light_data_of(&lights, true) +} + +/// A light section's two DataLayers: a resident section's from the cache, +/// or one value everywhere, which is quicker to write than to keep. +enum SectionLight { + Layers(Arc<Layers>), + /// Sky light in the high nibble, block light in the low one. + Uniform(u8), +} +impl SectionLight { + /// Whether the sky and the block layer hold any light. + fn any(&self) -> (bool, bool) { + match self { + Self::Layers(l) => (l.sky_any, l.block_any), + Self::Uniform(v) => (v >> 4 != 0, v & 15 != 0), + } + } + /// Append the sky or the block layer's 2048 bytes. + fn put(&self, sky: bool, out: &mut Vec<u8>) { + match self { + Self::Layers(l) => out.extend_from_slice(if sky { &l.sky } else { &l.block }), + Self::Uniform(v) => { + let n = if sky { v >> 4 } else { v & 15 }; + out.resize(out.len() + 2048, n | n << 4); + } + } + } +} + +/// The existing light sections, by index from below the world, with their +/// layers (`section` giving a section of the world's). +fn light_sections( + sections: &[Arc<Section>], + around: u64, + section: impl Fn(&Arc<Section>) -> SectionLight, +) -> Vec<(usize, SectionLight)> { + let n = sections.len(); + // Light section b holds chunk section b - 1. + let exists = + |b: usize| (b.saturating_sub(2)..=b.min(n - 1)).any(|s| s + 2 >= b && around >> s & 1 != 0); + (0..n + 2) + .filter(|&b| exists(b)) + .map(|b| { + let light = match b { + // Below the world nothing is lit; above it the sky is full. + 0 => SectionLight::Uniform(0), + _ if b == n + 1 => SectionLight::Uniform(0xF0), + _ => section(§ions[b - 1]), + }; + (b, light) + }) + .collect() +} + +/// The four masks of ClientboundLightUpdatePacketData (sky, block, empty +/// sky, empty block); a level without sky light has no sky storage at all. +fn light_masks(lights: &[(usize, SectionLight)], sky: bool) -> [Vec<u8>; 4] { + let mut masks: [Vec<u8>; 4] = Default::default(); + for (b, light) in lights { + let (sky_any, block_any) = light.any(); + if sky { + set_bit(&mut masks[if sky_any { 0 } else { 2 }], *b); + } + set_bit(&mut masks[if block_any { 1 } else { 3 }], *b); + } + masks +} + +/// The layers sent of one kind: those with any light. +fn sent_layers( + lights: &[(usize, SectionLight)], + sky: bool, +) -> impl Iterator<Item = &SectionLight> + Clone { + lights.iter().map(|(_, l)| l).filter(move |l| { + let (sky_any, block_any) = l.any(); + if sky { sky_any } else { block_any } + }) +} + +fn light_data_of(lights: &[(usize, SectionLight)], sky: bool) -> LightData { + let [sky_mask, block_mask, empty_sky_mask, empty_block_mask] = light_masks(lights, sky); + let layers = |kind: bool| -> Vec<Vec<u8>> { + sent_layers(lights, kind) + .map(|l| { + let mut layer = Vec::with_capacity(2048); + l.put(kind, &mut layer); + layer + }) + .collect() + }; + LightData { + sky_mask, + block_mask, + empty_sky_mask, + empty_block_mask, + sky: if sky { layers(true) } else { Vec::new() }, + block: layers(false), + } +} + +/// LightData.write, from the layers themselves. +fn write_light(lights: &[(usize, SectionLight)], sky: bool, w: &mut Writer) { + for mask in light_masks(lights, sky) { + BitSet::write(&mask, w); + } + for kind in [true, false] { + let layers = sent_layers(lights, kind).filter(|_| sky || !kind); + w.varint(layers.clone().count() as i32); + for l in layers { + w.varint(2048); + l.put(kind, &mut w.data); } } - data } #[cfg(test)] @@ -460,16 +1089,335 @@ mod tests { assert_eq!(block_entity_type("minecraft:stone"), None); } + #[test] + fn block_entities_come_from_the_cached_section_in_scan_order() { + let builtin = Builtin::parse( + r#"{"minecraft:block_entity_type": {"entries": { + "minecraft:chest": {"protocol_id": 1}, + "minecraft:sign": {"protocol_id": 7}}}}"#, + ) + .unwrap(); + let states = [ + "minecraft:air", + "minecraft:stone", + "minecraft:chest", + "minecraft:oak_sign", + ] + .map(|n| (Arc::<str>::from(n), false)); + let encoder = ChunkEncoder::new(states, &builtin, &[], &[]); + let mut chunk = Chunk::empty(2, -3); + for (x, y, z, state) in [(15, 3, 0, 2), (0, 3, 15, 3), (4, 3, 4, 1), (7, 40, 2, 2)] { + chunk.set(x, MIN_Y + y, z, state); + } + // Every block of a section looked at, as the scan did before the cache. + let scan = |chunk: &Chunk| { + let mut found = Vec::new(); + for (i, s) in chunk.sections.iter().enumerate() { + for y in 0..16 { + for z in 0..16 { + for x in 0..16 { + let state = s.get(crate::chunk::index(x, y, z)); + if let Some(kind) = encoder.entity_type(state) { + found.push((x, MIN_Y + i as i32 * 16 + y, z, kind)); + } + } + } + } + } + // Then the order the packet gives them, as encode_blocks does. + java_hash_order(&mut found, |e| { + (chunk.x * 16 + e.0, e.1, chunk.z * 16 + e.2) + }); + found + }; + let sent = |chunk: &Chunk| { + let packet = encoder.encode(chunk, &HashMap::new()); + let infos = packet.chunk.block_entities; + infos + .iter() + .map(|e| { + let xz = e.packed_xz as u8; + ( + i32::from(xz >> 4), + i32::from(e.y), + i32::from(xz & 15), + e.kind, + ) + }) + .collect::<Vec<_>>() + }; + let expected = scan(&chunk); + assert_eq!(expected.len(), 3); + assert_eq!(sent(&chunk), expected); + // From the cache the second time, and after an edit. + assert_eq!(sent(&chunk), expected); + chunk.set(15, MIN_Y + 3, 0, 0); + chunk.set(9, MIN_Y + 3, 9, 3); + assert_eq!(sent(&chunk), scan(&chunk)); + } + + fn encoder() -> ChunkEncoder { + let states = ["minecraft:air", "minecraft:stone", "minecraft:water"] + .map(|n| (Arc::<str>::from(n), n.ends_with("water"))); + ChunkEncoder::new(states, &Builtin::default(), &[], &[]) + } + + /// The bytes on the wire for a section, encoded afresh. + fn fresh(encoder: &ChunkEncoder, section: &Section) -> Vec<u8> { + let mut w = horizon_protocol::buf::Writer::new(); + encoder.coder.section(section).0.write(&mut w); + w.data + } + + #[test] + fn cached_sections_follow_edits() { + let encoder = encoder(); + let mut chunk = Chunk::empty(0, 0); + chunk.set(1, MIN_Y + 2, 3, 1); + let before = encoder.encode(&chunk, &HashMap::new()); + assert_eq!(encoder.occupied(&chunk), 1); + assert_eq!( + &*encoder.section_wire(&chunk.sections[0]).0, + fresh(&encoder, &chunk.sections[0]) + ); + // An edit moves the section: the cache sees a new one. + chunk.set(4, MIN_Y + 20, 4, 2); + chunk.set(1, MIN_Y + 2, 3, 0); + assert_eq!(encoder.occupied(&chunk), 2); + let after = encoder.encode(&chunk, &HashMap::new()); + assert_ne!(before.chunk.buffer, after.chunk.buffer); + let mut expected = Vec::new(); + for s in &chunk.sections { + expected.extend(fresh(&encoder, s)); + } + assert_eq!(after.chunk.buffer, expected); + } + + #[test] + fn light_layers_pack_two_blocks_per_byte() { + let mut chunk = Chunk::empty(0, 0); + let mut light = Box::new([0u8; 4096]); + for (i, v) in light.iter_mut().enumerate() { + *v = (i * 37 % 251) as u8; + } + Arc::make_mut(&mut chunk.sections[3]).light = Light::Full(light.clone()); + let data = light_data(&chunk.sections, u64::MAX); + // Layer 4 holds section 3. Below the world (layer 0) there is no sky + // light, so the sky layers listed start at layer 1; section 3 has the + // only block light. + let (sky, block) = (&data.sky[3], &data.block[0]); + for j in 0..4096 { + let (s, b) = if j & 1 == 0 { + (sky[j >> 1] & 15, block[j >> 1] & 15) + } else { + (sky[j >> 1] >> 4, block[j >> 1] >> 4) + }; + assert_eq!((s, b), (light[j] >> 4, light[j] & 15), "block {j}"); + } + } + + #[test] + fn cached_light_follows_edits() { + let encoder = encoder(); + let mut chunk = Chunk::empty(0, 0); + let mut light = Box::new([0x5Au8; 4096]); + Arc::make_mut(&mut chunk.sections[2]).light = Light::Full(light.clone()); + let first = encoder.encode(&chunk, &HashMap::new()).light; + assert_eq!(first, light_data(&chunk.sections, u64::MAX)); + // Relighting moves the section, so the cached layers aren't reused. + light[7] = 0xF3; + Arc::make_mut(&mut chunk.sections[2]).light = Light::Full(light); + let second = encoder.encode(&chunk, &HashMap::new()).light; + assert_ne!(first, second); + assert_eq!(second, light_data(&chunk.sections, u64::MAX)); + } + #[test] fn light_masks_cover_the_world_plus_borders() { let chunk = Chunk::empty(0, 0); // A column with nothing around it has no light layers at all. - assert!(light_data(&chunk, 0).sky.is_empty()); - let data = light_data(&chunk, u64::MAX); + assert!(light_data(&chunk.sections, 0).sky.is_empty()); + let data = light_data(&chunk.sections, u64::MAX); // Default sections are full sky light and no block light. assert_eq!(data.sky.len(), SECTIONS + 1); assert_eq!(data.block.len(), 0); assert_eq!(data.sky[0][0], 0xFF); assert!(horizon_protocol::buf::bit(&data.empty_block_mask, 5)); } + + #[test] + fn the_body_is_the_packet_encoded() { + let builtin = Builtin::parse( + r#"{"minecraft:block_entity_type": {"entries": { + "minecraft:chest": {"protocol_id": 1}, + "minecraft:sign": {"protocol_id": 7}}}}"#, + ) + .unwrap(); + let states = [ + "minecraft:air", + "minecraft:stone", + "minecraft:chest", + "minecraft:oak_sign", + ] + .map(|n| (Arc::<str>::from(n), false)); + let mut encoder = ChunkEncoder::new(states, &builtin, &[], &[]); + let mut chunk = Chunk::empty(-7, 12); + // Every kind of light section: uniform dark, uniform lit, sky and + // block light varying, none at all. + let mut light = Box::new([0u8; 4096]); + for (i, v) in light.iter_mut().enumerate() { + *v = (i * 53 % 256) as u8; + } + Arc::make_mut(&mut chunk.sections[4]).light = Light::Full(light); + Arc::make_mut(&mut chunk.sections[5]).light = Light::Uniform(0x07); + Arc::make_mut(&mut chunk.sections[6]).light = Light::Uniform(0); + Arc::make_mut(&mut chunk.sections[7]).light = Light::Full(Box::new([0x30; 4096])); + for (x, y, z, state) in [ + (3, 70, 9, 1), + (5, 70, 5, 2), + (0, -60, 15, 2), + (15, 300, 0, 1), + (8, 64, 1, 3), + ] { + chunk.set(x, y, z, state); + } + // A sign's update tag goes with the column. + let mut entities = HashMap::new(); + entities.insert( + (-7 * 16 + 8, 64, 12 * 16 + 1), + BlockEntity::Sign(crate::signs::SignData::default()), + ); + let packet = |encoder: &ChunkEncoder, around: u64| { + let mut packet = encoder.encode_blocks(&chunk, &entities); + packet.light = encoder.light_data(&chunk, around); + horizon_protocol::packets::play::Clientbound::LevelChunkWithLight(packet).encode() + }; + let sent = encoder + .encode_blocks(&chunk, &entities) + .chunk + .block_entities; + assert_eq!(sent.len(), 3); + assert!(sent.iter().any(|e| e.tag != Tag::End)); + for around in [0, 1, 0b1010_0000, 1 << 23, u64::MAX] { + assert_eq!( + encoder.body(&chunk, &entities, around), + packet(&encoder, around), + "around {around:#b}" + ); + } + encoder.set_dimension(0, 256, false); + assert_eq!( + encoder.body(&chunk, &entities, u64::MAX), + packet(&encoder, u64::MAX) + ); + } + + /// Columns whose sections and heightmaps the warm-up threads made give + /// the bytes each gives on its own, also with a column twice and its + /// neighbours' sections deciding the light layers. + #[test] + fn warmed_columns_give_the_same_bodies() { + let builtin = Builtin::parse( + r#"{"minecraft:block_entity_type": {"entries": { + "minecraft:sign": {"protocol_id": 7}}}}"#, + ) + .unwrap(); + let states = [ + "minecraft:air", + "minecraft:stone", + "minecraft:water", + "minecraft:oak_sign", + ] + .map(|n| (Arc::<str>::from(n), n.ends_with("water"))); + let mut world = crate::world::World::new(horizon_world::registry::BlockStateId(0), 0); + for x in -2..2 { + for z in -2..2 { + let mut chunk = Chunk::empty(x, z); + let n = (x * 7 + z * 13).rem_euclid(17); + for i in 0..40 + n * 30 { + let (bx, bz) = ((i * 5 + n) % 16, (i * 11) % 16); + chunk.set(bx, MIN_Y + (i * 37 + n * 50) % 384, bz, 1 + (i % 3) as u32); + } + let mut light = Box::new([0u8; 4096]); + for (i, v) in light.iter_mut().enumerate() { + *v = (i * 31 + n as usize) as u8; + } + Arc::make_mut(&mut chunk.sections[(n % 24) as usize]).light = Light::Full(light); + world.block_entities.insert( + (x * 16 + 2, 70, z * 16 + 3), + BlockEntity::Sign(crate::signs::SignData::default()), + ); + chunk.set(2, 70, 3, 3); + world.chunks.insert((x, z), Arc::new(chunk)); + } + } + let mut keys: Vec<(i32, i32)> = + (-2..2).flat_map(|x| (-2..2).map(move |z| (x, z))).collect(); + keys.push(keys[5]); + let chunks: Vec<&Chunk> = keys + .iter() + .map(|k| &**world.chunks.get(k).unwrap()) + .collect(); + // (air, fluid, blocks motion, blocks motion and is not leaves) per state. + let tables = Arc::new(crate::save::SaveTables::for_heightmaps( + crate::assets::Properties::for_tests(&[ + (true, false, false, false), + (false, false, true, true), + (false, true, true, true), + (false, false, false, false), + ]), + )); + let mut alone = ChunkEncoder::new(states.clone(), &builtin, &[], &[]); + alone.tables = Some(tables.clone()); + let expected: Vec<Vec<u8>> = chunks.iter().map(|c| alone.body_in(c, &world)).collect(); + let mut warmed = ChunkEncoder::new(states.clone(), &builtin, &[], &[]); + warmed.tables = Some(tables); + let bodies = |encoder: &ChunkEncoder| -> Vec<Vec<u8>> { + chunks.iter().map(|c| encoder.body_in(c, &world)).collect() + }; + assert!(chunks.len() * SECTIONS >= WARM_MIN); + warmed.warm(&chunks); + assert_eq!(bodies(&warmed), expected); + // Run on this thread alone, the job makes every column's heightmaps + // (once for the column there twice) and sections. + let mut cold = ChunkEncoder::new(states, &builtin, &[], &[]); + cold.tables = alone.tables.clone(); + let job = cold.job(&chunks).unwrap(); + assert_eq!((job.columns.len(), job.sections.len()), (16, 17 * SECTIONS)); + job.run(); + cold.keep(&job); + assert_eq!(cold.heights.lock().unwrap().len(), 16); + assert_eq!(bodies(&cold), expected); + // Again, from the cache, with nothing left to warm. + warmed.warm(&chunks); + assert_eq!(bodies(&warmed), expected); + warmed.warm(&[]); + } + + #[test] + fn a_nether_column_sends_its_sixteen_sections_without_sky_light() { + let chunk = Chunk::empty(0, 0); + let mut encoder = ChunkEncoder::new( + [("minecraft:air".into(), false)], + &Builtin::default(), + &[], + &[], + ); + let overworld = encoder.encode(&chunk, &HashMap::new()); + encoder.set_dimension(0, 256, false); + let nether = encoder.encode(&chunk, &HashMap::new()); + // An all-air section is the same bytes wherever it is. + let per_section = overworld.chunk.buffer.len() / SECTIONS; + assert_eq!(nether.chunk.buffer.len(), per_section * 16); + assert!(nether.light.sky.is_empty() && nether.light.sky_mask.is_empty()); + // Block light layers: one below and one above the sixteen sections. + assert!(horizon_protocol::buf::bit( + &nether.light.empty_block_mask, + 17 + )); + assert!(!horizon_protocol::buf::bit( + &nether.light.empty_block_mask, + 18 + )); + } } diff --git a/crates/horizon-client/src/lan/conn.rs b/crates/horizon-client/src/lan/conn.rs index fa31f2275..2805cf1da 100644 --- a/crates/horizon-client/src/lan/conn.rs +++ b/crates/horizon-client/src/lan/conn.rs @@ -1,4 +1,5 @@ -//! One accepted connection: handshake, status ping, offline-mode login and the +//! One accepted connection: handshake, status ping, login (offline, or online +//! with encryption and the session service's verdict) and the //! configuration phase (ServerStatusPacketListenerImpl, //! ServerLoginPacketListenerImpl, ServerConfigurationPacketListenerImpl). A //! player that completes configuration continues in the play state: the @@ -12,7 +13,7 @@ use super::{ use horizon_protocol::{ PROTOCOL_VERSION, connection::{ConnReader, ConnWriter, Connection}, - crypto::offline_uuid, + crypto::{offline_uuid, server_hash, uuid_string}, packets::{ Empty, configuration::{self as cfg, ClientInformation, KnownPack}, @@ -25,7 +26,7 @@ use std::{ net::TcpStream, sync::{ Arc, Mutex, PoisonError, - atomic::{AtomicI32, AtomicI64, AtomicUsize, Ordering}, + atomic::{AtomicI32, AtomicI64, AtomicU32, AtomicUsize, Ordering}, mpsc::{Receiver, RecvTimeoutError, Sender, channel}, }, time::{Duration, Instant}, @@ -149,6 +150,8 @@ pub struct PlayerSender { pub tx: Sender<Out>, pub pending: Arc<AtomicUsize>, held: Arc<Mutex<Vec<Vec<u8>>>>, + /// What [`flush_all`] gave the post thread and it hasn't passed on yet. + posted: Arc<AtomicUsize>, } impl PlayerSender { pub fn new(tx: Sender<Out>, pending: Arc<AtomicUsize>) -> Self { @@ -156,6 +159,7 @@ impl PlayerSender { tx, pending, held: Arc::default(), + posted: Arc::default(), } } fn held(&self) -> std::sync::MutexGuard<'_, Vec<Vec<u8>>> { @@ -167,21 +171,104 @@ impl PlayerSender { self.held().push(body); } else { self.flush(); - let _ = self.tx.send(Out::Packet(body)); + self.deliver(Out::Packet(body)); + } + } + /// Several packets in order, as [`Self::send`] each: under one lock + /// while the tick holds them. + pub fn send_many(&self, bodies: impl IntoIterator<Item = Vec<u8>>) { + if !HOLDING.get() { + bodies.into_iter().for_each(|body| self.send(body)); + return; + } + let mut held = self.held(); + for body in bodies { + self.pending.fetch_add(body.len(), Ordering::Relaxed); + held.push(body); } } /// Hand the held packets to the writer thread. pub fn flush(&self) { let held = std::mem::take(&mut *self.held()); if !held.is_empty() { - let _ = self.tx.send(Out::Packets(held)); + self.deliver(Out::Packets(held)); } } /// Send a play disconnect with this reason after what was sent before. pub fn disconnect(&self, reason: horizon_world::nbt::Tag) { self.flush(); - let _ = self.tx.send(Out::Disconnect(reason)); + self.deliver(Out::Disconnect(reason)); } + /// To the writer thread, after anything still with the post thread. + fn deliver(&self, out: Out) { + if self.posted.load(Ordering::Acquire) == 0 { + let _ = self.tx.send(out); + } else { + post(vec![self.posting(out)]); + } + } + fn posting(&self, out: Out) -> Posted { + self.posted.fetch_add(1, Ordering::AcqRel); + Posted { + tx: self.tx.clone(), + out, + posted: self.posted.clone(), + } + } +} + +/// Hand every player's held packets to their writer threads. Waking a writer +/// costs a system call, and on a busy machine often the rest of the core's +/// time slice; one thread of its own wakes them, so the game thread wakes one +/// thread per tick, not one per player. Each player's packets keep their +/// order, also with what is sent to it next. +pub fn flush_all<'a>(senders: impl IntoIterator<Item = &'a PlayerSender>) { + let batch: Vec<Posted> = senders + .into_iter() + .filter_map(|s| { + let held = std::mem::take(&mut *s.held()); + (!held.is_empty()).then(|| s.posting(Out::Packets(held))) + }) + .collect(); + if !batch.is_empty() { + post(batch); + } +} + +/// Packets on their way through the post thread. +struct Posted { + tx: Sender<Out>, + out: Out, + posted: Arc<AtomicUsize>, +} +impl Posted { + fn hand_over(self) { + let _ = self.tx.send(self.out); + self.posted.fetch_sub(1, Ordering::AcqRel); + } +} + +/// Give a batch to the post thread, started the first time; without one +/// (the thread could not start) the batch is handed over here. +fn post(batch: Vec<Posted>) { + static POST: std::sync::OnceLock<Option<Sender<Vec<Posted>>>> = std::sync::OnceLock::new(); + let post = POST.get_or_init(|| { + let (tx, rx) = channel::<Vec<Posted>>(); + std::thread::Builder::new() + .name("horizon-lan-post".to_owned()) + .spawn(move || { + for batch in rx { + batch.into_iter().for_each(Posted::hand_over); + } + }) + .ok() + .map(|_| tx) + }); + let unsent = match post { + Some(tx) => tx.send(batch).err().map(|e| e.0), + None => Some(batch), + }; + unsent.into_iter().flatten().for_each(Posted::hand_over); } /// LegacyQueryHandler: the pre-1.7 server list ping. What the first read @@ -354,6 +441,145 @@ fn serve_status(c: &mut Connection, shared: &Shared) -> Result<(), String> { } } +/// ServerLoginPacketListenerImpl.getUserName: the name and the address +/// (Connection.getLoggableAddress, `/ip:port` as InetSocketAddress prints it). +fn user_name(name: &str, address: std::net::SocketAddr, log: &super::NetLog) -> String { + if log.log_ips { + format!("{name} (/{address})") + } else { + format!("{name} (<ip address withheld>)") + } +} + +/// ServerLoginPacketListenerImpl.disconnect: log the reason's text and send +/// it. Returns the text, for the "lost connection" line that follows. +fn refuse( + c: &mut Connection, + shared: &Shared, + thread: &str, + user: &str, + reason: &horizon_world::nbt::Tag, +) -> String { + let text = shared + .config + .log + .as_ref() + .map(|log| horizon_protocol::text::plain(reason, &log.lang)) + .unwrap_or_default(); + if let Some(log) = &shared.config.log { + (log.write)(thread, "INFO", &format!("Disconnecting {user}: {text}")); + } + login_disconnect(c, reason); + text +} + +/// ServerLoginPacketListenerImpl.onDisconnect, once the server thread sees +/// the connection closed: by a refusal, or by the client ("Disconnected"). +fn lost(shared: &Shared, user: &str, text: &str) { + if let Some(log) = &shared.config.log { + (log.write)( + "Server thread", + "INFO", + &format!("{user} lost connection: {text}"), + ); + } +} + +/// UNIQUE_THREAD_ID: the User Authenticator threads are numbered from 1. +static AUTHENTICATORS: AtomicU32 = AtomicU32::new(0); + +/// ServerLoginPacketListenerImpl.handleKey and the User Authenticator +/// thread: check the client's answer to the encryption request, encrypt the +/// connection, and ask the session service whether the player joined with +/// this server's hash. The profile, or the text of the refusal. +fn authenticate( + c: &mut Connection, + shared: &Shared, + online: &super::OnlineMode, + name: &str, + address: std::net::SocketAddr, + user: &str, +) -> Result<Result<(u128, Vec<login::Property>), String>, String> { + let e = |e: horizon_protocol::Error| e.to_string(); + let mut challenge = [0u8; 4]; + horizon_protocol::crypto::random_bytes(&mut challenge).map_err(e)?; + c.send( + &login::Clientbound::Hello(login::Hello { + server_id: String::new(), + public_key: online.public_der.clone(), + challenge: challenge.to_vec(), + should_authenticate: true, + }) + .encode(), + ) + .map_err(e)?; + let (id, payload) = c.recv().map_err(e)?; + let key = match login::Serverbound::decode(id, &payload).map_err(e)? { + login::Serverbound::Key(k) => k, + _ => return Err("Unexpected packet during login".into()), + }; + // ServerboundKeyPacket.isChallengeValid and getSecretKey. + let protocol_error = || "Protocol error".to_owned(); + if online.key.decrypt(&key.challenge).ok().as_deref() != Some(&challenge[..]) { + return Err(protocol_error()); + } + let secret: [u8; 16] = online + .key + .decrypt(&key.key) + .ok() + .and_then(|s| s.try_into().ok()) + .ok_or_else(protocol_error)?; + let digest = server_hash("", &secret, &online.public_der); + c.enable_encryption(&secret); + let thread = format!( + "User Authenticator #{}", + AUTHENTICATORS.fetch_add(1, Ordering::Relaxed) + 1 + ); + let log = |level: &str, line: &str| { + if let Some(log) = &shared.config.log { + (log.write)(&thread, level, line); + } + }; + let ip = online.prevent_proxy.then(|| address.ip()); + match crate::auth::has_joined(name, &digest, ip) { + Ok(Some(profile)) => { + log( + "INFO", + &format!("UUID of player {name} is {}", uuid_string(profile.uuid)), + ); + Ok(Ok((profile.uuid, profile.properties))) + } + Ok(None) => { + let text = refuse( + c, + shared, + &thread, + user, + &translatable("multiplayer.disconnect.unverified_username", &[]), + ); + log( + "ERROR", + &format!("Username '{name}' tried to join with an invalid session"), + ); + Ok(Err(text)) + } + Err(_) => { + let text = refuse( + c, + shared, + &thread, + user, + &translatable("multiplayer.disconnect.authservers_down", &[]), + ); + log( + "ERROR", + "Couldn't verify username because servers are unavailable", + ); + Ok(Err(text)) + } + } +} + fn serve_login( mut c: Connection, address: std::net::SocketAddr, @@ -372,14 +598,36 @@ fn serve_login( ); return Ok(()); } - // Offline mode: the profile is derived from the name (PlayerList.canPlayerLogin - // then rejects duplicates and a full server). - let uuid = offline_uuid(&hello.name); + let user = shared + .config + .log + .as_ref() + .map(|log| user_name(&hello.name, address, log)) + .unwrap_or_default(); + // handleHello: online mode authenticates first; offline mode derives the + // profile from the name. + let (uuid, properties) = match &shared.config.online { + Some(online) => match authenticate(&mut c, shared, online, &hello.name, address, &user) { + Ok(Ok(profile)) => profile, + Ok(Err(refused)) => { + lost(shared, &user, &refused); + return Ok(()); + } + Err(err) => { + lost(shared, &user, "Disconnected"); + return Err(err); + } + }, + None => (offline_uuid(&hello.name), Vec::new()), + }; + // verifyLoginAndFinishConnectionSetup: PlayerList.canPlayerLogin, then + // duplicates and a full server. if let Some(reason) = shared.try_admit(address, uuid, &hello.name) { - login_disconnect(&mut c, &reason); + let text = refuse(&mut c, shared, "Server thread", &user, &reason); + lost(shared, &user, &text); return Ok(()); } - let result = login_and_configure(&mut c, shared, uuid, &hello.name); + let result = login_and_configure(&mut c, shared, uuid, &hello.name, properties); match result { Ok(Some(parts)) => { play(c, shared, parts, address); @@ -391,6 +639,9 @@ fn serve_login( } Err(err) => { shared.release(uuid); + // ServerConfigurationPacketListenerImpl.onDisconnect. + let profile = format!("{} ({})", hello.name, uuid_string(uuid)); + lost(shared, &profile, "Disconnected"); Err(err) } } @@ -399,6 +650,7 @@ fn serve_login( struct Parts { uuid: u128, name: String, + properties: Vec<login::Property>, info: ClientInformation, brand: String, } @@ -408,6 +660,7 @@ fn login_and_configure( shared: &Arc<Shared>, uuid: u128, name: &str, + properties: Vec<login::Property>, ) -> Result<Option<Parts>, String> { let e = |e: horizon_protocol::Error| e.to_string(); // ServerLoginPacketListenerImpl: memory connections are not compressed. @@ -422,7 +675,7 @@ fn login_and_configure( profile: login::GameProfile { uuid, name: name.to_owned(), - properties: Vec::new(), + properties: properties.clone(), }, session: uuid, }) @@ -501,6 +754,7 @@ fn login_and_configure( Ok(Some(Parts { uuid, name: name.to_owned(), + properties, info, brand, })) @@ -527,6 +781,7 @@ fn play(c: Connection, shared: &Arc<Shared>, parts: Parts, address: std::net::So conn: conn_id, uuid: parts.uuid, name: parts.name, + properties: parts.properties, info: parts.info, brand: parts.brand, sender, @@ -579,6 +834,82 @@ fn read_loop( } } +/// Packet bodies at least this large are compressed once for every +/// connection: chunk columns, mostly, which every player near them receives. +const SHARED_FRAME_MIN: usize = 8 << 10; +/// The compressed frames kept, in bytes; the oldest go first. +const SHARED_FRAME_BYTES: usize = 32 << 20; + +/// Compressed frame contents by the body they were made from: its length, +/// two independently keyed 64-bit hashes of it, and the threshold. +struct SharedFrames { + hashers: (std::hash::RandomState, std::hash::RandomState), + frames: Mutex<FrameCache>, + /// [`SHARED_FRAME_BYTES`]. + limit: usize, +} +type FrameKey = (usize, u64, u64, usize); +#[derive(Default)] +struct FrameCache { + frames: crate::fast_hash::FxHashMap<FrameKey, Arc<[u8]>>, + order: std::collections::VecDeque<FrameKey>, + bytes: usize, +} +impl SharedFrames { + fn get() -> &'static Self { + static FRAMES: std::sync::OnceLock<SharedFrames> = std::sync::OnceLock::new(); + FRAMES.get_or_init(|| Self::new(SHARED_FRAME_BYTES)) + } + fn new(limit: usize) -> Self { + Self { + hashers: Default::default(), + frames: Mutex::default(), + limit, + } + } + /// `frame::contents(body, Some(threshold))`, compressed only when no + /// connection has sent the same body lately. + fn contents(&self, body: &[u8], threshold: usize) -> horizon_protocol::Result<Arc<[u8]>> { + use std::hash::BuildHasher; + let key = ( + body.len(), + self.hashers.0.hash_one(body), + self.hashers.1.hash_one(body), + threshold, + ); + let cache = || self.frames.lock().unwrap_or_else(PoisonError::into_inner); + if let Some(contents) = cache().frames.get(&key) { + return Ok(contents.clone()); + } + let contents: Arc<[u8]> = horizon_protocol::frame::contents(body, Some(threshold))?.into(); + let mut cache = cache(); + if cache.frames.insert(key, contents.clone()).is_none() { + cache.order.push_back(key); + cache.bytes += contents.len(); + while cache.bytes > self.limit + && let Some(old) = cache.order.pop_front() + { + if let Some(c) = cache.frames.remove(&old) { + cache.bytes -= c.len(); + } + } + } + Ok(contents) + } +} + +/// Send one packet body: a large one compressed through [`SharedFrames`], +/// so a chunk column sent to fifty players is compressed once, not fifty +/// times. The bytes on the wire are the same. +fn send_body(writer: &mut ConnWriter, body: &[u8]) -> horizon_protocol::Result<()> { + match writer.threshold { + Some(t) if body.len() >= t.max(SHARED_FRAME_MIN) => { + writer.send_contents(&SharedFrames::get().contents(body, t)?) + } + _ => writer.send(body), + } +} + fn write_loop( mut writer: ConnWriter, rx: Receiver<Out>, @@ -592,7 +923,7 @@ fn write_loop( loop { let mut write = |body: Vec<u8>| { pending.fetch_sub(body.len(), Ordering::Relaxed); - writer.send(&body).is_ok() && pending.load(Ordering::Relaxed) <= MAX_PENDING + send_body(&mut writer, &body).is_ok() && pending.load(Ordering::Relaxed) <= MAX_PENDING }; match rx.recv_timeout(Duration::from_millis(200)) { Ok(Out::Packet(body)) => { @@ -650,15 +981,90 @@ fn write_loop( mod tests { use super::*; + fn unpack(out: Out) -> Vec<Vec<u8>> { + match out { + Out::Packet(body) => vec![body], + Out::Packets(bodies) => bodies, + Out::Disconnect(_) => vec![b"disconnect".to_vec()], + Out::Close => vec![b"close".to_vec()], + } + } + fn bodies(rx: &Receiver<Out>) -> Vec<Vec<Vec<u8>>> { - rx.try_iter() - .map(|out| match out { - Out::Packet(body) => vec![body], - Out::Packets(bodies) => bodies, - Out::Disconnect(_) => vec![b"disconnect".to_vec()], - Out::Close => vec![b"close".to_vec()], - }) - .collect() + rx.try_iter().map(unpack).collect() + } + + #[test] + fn packets_sent_after_a_posted_tick_follow_it() { + let (tx, rx) = channel(); + let (tx2, rx2) = channel(); + let sender = PlayerSender::new(tx, Arc::default()); + let other = PlayerSender::new(tx2, Arc::default()); + for round in 0..50u8 { + let holding = Holding::start(); + sender.send(vec![round, 1]); + sender.send(vec![round, 2]); + other.send(vec![round]); + drop(holding); + flush_all([&sender, &other]); + // Straight after the tick, while the post thread may still hold it. + sender.send(vec![round, 3]); + let next = || unpack(rx.recv_timeout(Duration::from_secs(10)).unwrap()); + assert_eq!(next(), [vec![round, 1], vec![round, 2]]); + assert_eq!(next(), [vec![round, 3]]); + let got = unpack(rx2.recv_timeout(Duration::from_secs(10)).unwrap()); + assert_eq!(got, [vec![round]]); + } + let holding = Holding::start(); + sender.send(vec![9]); + drop(holding); + flush_all([&sender]); + sender.disconnect(horizon_world::nbt::Tag::Byte(0)); + let next = || unpack(rx.recv_timeout(Duration::from_secs(10)).unwrap()); + assert_eq!(next(), [vec![9]]); + assert_eq!(next(), [b"disconnect".to_vec()]); + // Nothing sent twice, nothing left with the post thread. + assert!(rx.try_recv().is_err()); + let deadline = Instant::now() + Duration::from_secs(10); + while sender.posted.load(Ordering::Acquire) != 0 { + assert!(Instant::now() < deadline); + std::thread::yield_now(); + } + } + + #[test] + fn shared_frames_are_the_frames_compressed_here() { + use horizon_protocol::frame; + let frames = SharedFrames::new(usize::MAX); + let a: Vec<u8> = (0..40_000u32).map(|i| (i * 7 % 13) as u8).collect(); + let mut b = a.clone(); + b[20_000] ^= 1; + for (body, threshold) in [(&a, 256), (&b, 256), (&a, 256), (&a, 64), (&b, 256)] { + let shared = frames.contents(body, threshold).unwrap(); + assert_eq!(*shared, *frame::contents(body, Some(threshold)).unwrap()); + } + // One frame per body and threshold. + assert_eq!(frames.frames.lock().unwrap().frames.len(), 3); + } + + #[test] + fn shared_frames_keep_the_newest_within_their_limit() { + let first = SharedFrames::new(0) + .contents(&[1; 9000], 256) + .unwrap() + .len(); + let frames = SharedFrames::new(first * 2); + for n in 0..5u8 { + frames.contents(&[n; 9000], 256).unwrap(); + } + let cache = frames.frames.lock().unwrap(); + assert!((1..=2).contains(&cache.frames.len())); + assert_eq!(cache.order.len(), cache.frames.len()); + assert_eq!( + cache.bytes, + cache.frames.values().map(|c| c.len()).sum::<usize>() + ); + assert!(cache.bytes <= first * 2); } #[test] diff --git a/crates/horizon-client/src/lan/containers.rs b/crates/horizon-client/src/lan/containers.rs index 0fa47510a..66fbfe4b6 100644 --- a/crates/horizon-client/src/lan/containers.rs +++ b/crates/horizon-client/src/lan/containers.rs @@ -25,7 +25,7 @@ impl Hub { for g in self .guests .iter() - .filter(|g| g.placed && Some(g.conn) != except) + .filter(|g| self.here(g) && Some(g.conn) != except) { let d: f64 = (0..3).map(|k| (g.position[k] - at[k]).powi(2)).sum(); if d <= range * range { @@ -79,11 +79,11 @@ impl Hub { } /// ClientboundBlockEventPacket (`PlayerList.broadcast` within 64 blocks). - pub fn block_event(&self, pos: [i32; 3], b0: i32, b1: i32, state: u16) { + pub fn block_event(&self, pos: [i32; 3], b0: i32, b1: i32, state: u32) { let block = self .encoder .block_ids - .get(usize::from(state)) + .get(state as usize) .copied() .unwrap_or(0); let at = [f64::from(pos[0]), f64::from(pos[1]), f64::from(pos[2])]; @@ -109,6 +109,33 @@ impl Hub { ); } + /// `ServerLevel.broadcastDamageEvent`: the clients around show the hurt + /// entity's flash and hear its hurt sound. + pub fn damage_event( + &self, + id: i32, + kind: &str, + cause: Option<i32>, + direct: Option<i32>, + at: [f64; 3], + ) { + let Some(source_type) = self.sync.id("minecraft:damage_type", kind) else { + return; + }; + self.broadcast_near( + at, + 128.0, + &C::DamageEvent(play::DamageEvent { + id, + source_type, + cause, + direct, + position: None, + }), + None, + ); + } + pub fn swing(&self, id: i32, hand: i32, at: [f64; 3], except: Option<u32>) { self.broadcast_near( at, @@ -428,15 +455,17 @@ impl Hub { let Slots::Block(pos) = m.slots else { return; }; - let Some(BlockEntity::Furnace(f)) = world.block_entities.get(&pos) else { - return; - }; - let data = [ - f.lit_remaining, - f.lit_total, - f.cooking_timer, - f.cooking_total, - ] + // AbstractFurnaceBlockEntity.dataAccess, BrewingStandBlockEntity.dataAccess. + let data = match world.block_entities.get(&pos) { + Some(BlockEntity::Furnace(f)) => [ + f.lit_remaining, + f.lit_total, + f.cooking_timer, + f.cooking_total, + ], + Some(BlockEntity::Brewing(b)) => [b.brew_time, b.fuel, b.total_brew_time, b.total_fuel], + _ => return, + } .map(|v| v as i16); m.sent_data.resize(data.len(), i16::MIN); let window = m.window; @@ -464,7 +493,7 @@ impl Hub { let Some(menu) = self.guests[i] .menu .as_ref() - .filter(|_| self.guests[i].placed) + .filter(|_| self.here(&self.guests[i])) else { continue; }; diff --git a/crates/horizon-client/src/lan/entity.rs b/crates/horizon-client/src/lan/entity.rs index 3b926b372..9082e01fa 100644 --- a/crates/horizon-client/src/lan/entity.rs +++ b/crates/horizon-client/src/lan/entity.rs @@ -1,20 +1,38 @@ //! Entity tracking (ChunkMap.TrackedEntity / ServerEntity): the game describes -//! every entity it simulates as an `EntityState` each tick; a `Tracker` -//! remembers what one client has been told and produces the spawn, movement, -//! motion, metadata, equipment and removal packets that bring it up to date, -//! at the entity type's update interval (`tracking`). +//! every entity it simulates as an `EntityState` each tick. A level's +//! `Entities` remember what its clients were last told about each entity, +//! once for all of them as vanilla's ServerEntity does, and produce the +//! movement, motion, metadata and equipment packets that bring them up to +//! date at the entity type's update interval (`tracking`); each client's +//! `Tracker` is the set of entities it has been paired with. use super::tracking; use horizon_protocol::{ buf::pack_degrees, - entity_data::Value, + entity_data::{Value, serializer}, item::Stack, packets::play::{ - self, AddEntity, EntityPositionSync, MoveEntity, MoveEntityPos, MoveEntityPosRot, - MoveEntityRot, PositionPath, RotateHead, SetEntityData, SetEntityMotion, SetEquipment, - VecDelta, + self, AddEntity, AttributeSnapshot, EntityPositionSync, MoveEntity, MoveEntityPos, + MoveEntityPosRot, MoveEntityRot, PositionPath, RotateHead, SetEntityData, SetEntityMotion, + SetEquipment, SetPassengers, UpdateAttributes, VecDelta, }, }; +/// The attribute instances of `now` that differ from what `sent` held (an +/// instance is never removed: one that disappears is left as it was sent). +pub fn changed_attributes( + now: &[AttributeSnapshot], + sent: &[AttributeSnapshot], +) -> Vec<AttributeSnapshot> { + now.iter() + .filter(|a| { + sent.iter() + .find(|b| b.attribute == a.attribute) + .is_none_or(|b| b != *a) + }) + .cloned() + .collect() +} + /// What the game says about one entity this tick. #[derive(Clone, Debug)] pub struct EntityState { @@ -35,10 +53,79 @@ pub struct EntityState { /// EquipmentSlot ordinals (0 main hand, 1 off hand, 2 feet, 3 legs, /// 4 chest, 5 head, 6 body, 7 saddle) and their stacks. pub equipment: Vec<(u8, Option<Stack>)>, + /// A living entity's client-syncable attribute instances + /// (`AttributeMap.getSyncableAttributes`), by registry id. + pub attributes: Vec<AttributeSnapshot>, + /// `Entity.getRequiresPrecisePosition` (primed TNT and falling blocks + /// touching a block): moves are sent as full position syncs. + pub precise: bool, } -/// What a client was last told about one entity (ServerEntity's fields). -struct Sent { +/// `AbstractArrow`: every update sends its position and rotation. +fn is_arrow(kind: &str) -> bool { + matches!( + kind, + "minecraft:arrow" | "minecraft:spectral_arrow" | "minecraft:trident" + ) +} + +/// `AbstractHurtingProjectile.accelerationPower` (0.1, none for wind +/// charges), sent with each velocity update. +fn acceleration_power(kind: &str) -> Option<f64> { + match kind { + "minecraft:fireball" + | "minecraft:small_fireball" + | "minecraft:dragon_fireball" + | "minecraft:wither_skull" => Some(0.1), + "minecraft:wind_charge" | "minecraft:breeze_wind_charge" => Some(0.0), + _ => None, + } +} + +/// Who rides what this tick (`Entity.getPassengers`, `isPassenger`). +#[derive(Default)] +pub struct Riding { + /// Each vehicle's passengers, in order. + pub passengers: crate::fast_hash::FxHashMap<i32, Vec<i32>>, + /// Each passenger's vehicle. + pub vehicle: crate::fast_hash::FxHashMap<i32, i32>, +} + +impl Riding { + pub fn clear(&mut self) { + self.passengers.clear(); + self.vehicle.clear(); + } + /// `passenger` rides `vehicle`, after the ones already riding it. + pub fn add(&mut self, vehicle: i32, passenger: i32) { + self.passengers.entry(vehicle).or_default().push(passenger); + self.vehicle.insert(passenger, vehicle); + } + pub fn passengers(&self, id: i32) -> &[i32] { + self.passengers.get(&id).map_or(&[], Vec::as_slice) + } + fn set_passengers(&self, vehicle: i32) -> play::Clientbound { + play::Clientbound::SetPassengers(SetPassengers { + vehicle, + passengers: self.passengers(vehicle).to_vec(), + }) + } +} + +/// What a level's clients were last told about one entity (ServerEntity's +/// fields), shared by every client that tracks it. +struct ServerEntity { + /// The registry id of its type (None: a type the clients don't know, + /// never paired). + kind: Option<i32>, + /// The type's tracking range in chunks, update interval (0: never), + /// whether velocity is tracked, whether it is an `AbstractArrow` and its + /// `AbstractHurtingProjectile.accelerationPower`. + range: i32, + interval: u32, + track_deltas: bool, + arrow: bool, + power: Option<f64>, /// VecDeltaCodec's base: the last position sent. base: [f64; 3], /// lastSentYRot / lastSentXRot / lastSentYHeadRot. @@ -46,19 +133,31 @@ struct Sent { head: i8, /// lastSentMovement. velocity: [f64; 3], + /// trackedDataValues: the entity data the clients hold. meta: Vec<(u8, i32, Value)>, equipment: Vec<(u8, Option<Stack>)>, + /// The attribute instances the clients hold. + attributes: Vec<AttributeSnapshot>, was_on_ground: bool, tick: u32, teleport_delay: u32, - /// The type's update interval (0: never) and whether velocity is tracked. - interval: u32, - track_deltas: bool, + /// lastPassengers, and wasRiding: a passenger's position isn't synced, + /// and the first update after it dismounts is a full position sync. + passengers: Vec<i32>, + was_riding: bool, + /// The level tick it was last seen in, its place in that tick's list, and + /// whether it was added in it (its first update is the next tick's). + seen: u32, + index: usize, + added: bool, + /// TrackedEntity.lastSectionPos: its pairs are checked again when it + /// moves to another section. + section: [i32; 3], } -#[derive(Default)] -pub struct Tracker { - sent: std::collections::HashMap<i32, Sent>, +/// SectionPos.of(entity). +fn section_of(position: [f64; 3]) -> [i32; 3] { + position.map(|v| (v.floor() as i32) >> 4) } /// VecDeltaCodec.encode. @@ -66,98 +165,127 @@ fn encode(v: f64) -> i64 { (v * 4096.0).round() as i64 } -/// The horizontal range within which a client sees an entity of `kind` -/// (TrackedEntity.updatePlayer: the type's range, capped by the view distance). -pub fn visible_range(kind: &str, view_distance: i32) -> f64 { - let chunks = i32::from(tracking::settings(kind).0); - f64::from(chunks.min(view_distance) * 16) -} - -impl Tracker { - pub fn is_tracking(&self, id: i32) -> bool { - self.sent.contains_key(&id) - } - pub fn ids(&self) -> Vec<i32> { - self.sent.keys().copied().collect() - } - pub fn forget(&mut self, id: i32) { - self.sent.remove(&id); - } - pub fn clear(&mut self) { - self.sent.clear(); - } - pub fn len(&self) -> usize { - self.sent.len() - } - pub fn is_empty(&self) -> bool { - self.sent.is_empty() +impl ServerEntity { + /// `new ServerEntity`: the entity as it is when it joins the level. + fn new(e: &EntityState, kind: Option<i32>, riding: &Riding) -> Self { + let (range, interval, track_deltas) = tracking::settings(&e.kind); + Self { + kind, + range: i32::from(range), + interval, + track_deltas, + arrow: is_arrow(&e.kind), + power: acceleration_power(&e.kind), + base: e.position, + rotation: (pack_degrees(e.pitch), pack_degrees(e.yaw)), + head: pack_degrees(e.head_yaw), + velocity: e.velocity, + meta: e.meta.clone(), + equipment: e.equipment.clone(), + attributes: e.attributes.clone(), + was_on_ground: e.on_ground, + tick: 0, + teleport_delay: 0, + passengers: riding.passengers(e.id).to_vec(), + was_riding: false, + seen: 0, + index: 0, + added: true, + section: section_of(e.position), + } } - /// The packets that introduce an entity (ServerEntity.sendPairingData). - pub fn spawn(&mut self, e: &EntityState, entity_type: i32) -> Vec<play::Clientbound> { - let rotation = (pack_degrees(e.pitch), pack_degrees(e.yaw)); - let head = pack_degrees(e.head_yaw); - let (_, interval, track_deltas) = tracking::settings(&e.kind); - let mut out = vec![play::Clientbound::AddEntity(AddEntity { + /// ServerEntity.sendPairingData: the packets that introduce the entity + /// to a client that starts tracking it. + fn pairing( + &self, + e: &EntityState, + kind: i32, + riding: &Riding, + out: &mut Vec<play::Clientbound>, + ) { + out.push(play::Clientbound::AddEntity(AddEntity { id: e.id, uuid: e.uuid, - kind: entity_type, + kind, position: e.position, velocity: e.velocity, - pitch: rotation.0, - yaw: rotation.1, - head_yaw: head, + pitch: pack_degrees(e.pitch), + yaw: pack_degrees(e.yaw), + head_yaw: pack_degrees(e.head_yaw), data: e.data, - })]; - if !e.meta.is_empty() { + })); + if !self.meta.is_empty() { out.push(play::Clientbound::SetEntityData(SetEntityData { id: e.id, - values: e.meta.clone(), + values: self.meta.clone(), })); } - if !e.equipment.is_empty() { + // getSyncableAttributes: the instances as they are now. + if !e.attributes.is_empty() { + out.push(play::Clientbound::UpdateAttributes(UpdateAttributes { + id: e.id, + values: e.attributes.clone(), + })); + } + if !self.equipment.is_empty() { out.push(play::Clientbound::SetEquipment(SetEquipment { id: e.id, - slots: e.equipment.clone(), + slots: self.equipment.clone(), })); } - self.sent.insert( - e.id, - Sent { - base: e.position, - rotation, - head, - velocity: e.velocity, - meta: e.meta.clone(), - equipment: e.equipment.clone(), - was_on_ground: e.on_ground, - tick: 0, - teleport_delay: 0, - interval, - track_deltas, - }, - ); - out + if !riding.passengers(e.id).is_empty() { + out.push(riding.set_passengers(e.id)); + } + if let Some(&vehicle) = riding.vehicle.get(&e.id) { + out.push(riding.set_passengers(vehicle)); + } } /// Changes since the last update (ServerEntity.sendChanges): movement and /// rotation every update interval (positions at least every 60 ticks, a /// full position sync every 400 or when the entity lands or leaves the /// ground), velocity for types that track it, changed data at once. - pub fn update(&mut self, e: &EntityState) -> Vec<play::Clientbound> { - let Some(s) = self.sent.get_mut(&e.id) else { - return Vec::new(); - }; - let mut out = Vec::new(); - let dirty = e.meta != s.meta; - let due = s.interval != 0 && s.tick.is_multiple_of(s.interval); + fn send_changes(&mut self, e: &EntityState, riding: &Riding, out: &mut Vec<play::Clientbound>) { + let passengers = riding.passengers(e.id); + if passengers != self.passengers.as_slice() { + out.push(riding.set_passengers(e.id)); + self.passengers.clear(); + self.passengers.extend_from_slice(passengers); + } + // SynchedEntityData.isDirty: once for every client. + let dirty = e.meta != self.meta; + let due = self.interval != 0 && self.tick.is_multiple_of(self.interval); if due || dirty { let rotation = (pack_degrees(e.pitch), pack_degrees(e.yaw)); let turned = |a: i8, b: i8| (i32::from(a) - i32::from(b)).abs() >= 1; - let rotate = turned(rotation.1, s.rotation.1) || turned(rotation.0, s.rotation.0); - s.teleport_delay += 1; - let d: f64 = (0..3).map(|k| (e.position[k] - s.base[k]).powi(2)).sum(); - let send_position = d >= 7.629_394_5e-6 || s.tick.is_multiple_of(60); + let rotate = self.arrow + || turned(rotation.1, self.rotation.1) + || turned(rotation.0, self.rotation.0); + if riding.vehicle.contains_key(&e.id) { + // A passenger: its rotation alone (its vehicle places it). + if rotate { + out.push(play::Clientbound::MoveEntityRot(MoveEntityRot( + MoveEntity { + id: e.id, + delta: None, + rotation: Some((rotation.1, rotation.0)), + on_ground: e.on_ground, + }, + ))); + self.rotation = rotation; + } + self.base = e.position; + self.was_riding = true; + self.send_dirty(e, dirty, out); + self.send_head(e, out); + self.tick += 1; + self.send_equipment(e, out); + return; + } + self.teleport_delay += 1; + let d: f64 = (0..3).map(|k| (e.position[k] - self.base[k]).powi(2)).sum(); + let send_position = self.arrow || d >= 7.629_394_5e-6 || self.tick.is_multiple_of(60); let sync = EntityPositionSync { id: e.id, path: PositionPath::Linear(e.position), @@ -166,12 +294,17 @@ impl Tracker { on_ground: e.on_ground, }; // ServerEntity.createMovePacket. - let packet = if s.teleport_delay > 400 || s.was_on_ground != e.on_ground { - s.was_on_ground = e.on_ground; - s.teleport_delay = 0; + let packet = if e.precise + || self.teleport_delay > 400 + || self.was_riding + || self.was_on_ground != e.on_ground + { + self.was_on_ground = e.on_ground; + self.teleport_delay = 0; + self.was_riding = false; Some(play::Clientbound::EntityPositionSync(sync)) } else if send_position { - let delta = [0, 1, 2].map(|k| encode(e.position[k]) - encode(s.base[k])); + let delta = [0, 1, 2].map(|k| encode(e.position[k]) - encode(self.base[k])); if delta.iter().any(|v| i16::try_from(*v).is_err()) { Some(play::Clientbound::EntityPositionSync(sync)) } else { @@ -199,17 +332,30 @@ impl Tracker { } else { None }; - if s.track_deltas { + if self.track_deltas { let diff: f64 = (0..3) - .map(|k| (e.velocity[k] - s.velocity[k]).powi(2)) + .map(|k| (e.velocity[k] - self.velocity[k]).powi(2)) .sum(); let still = e.velocity.iter().all(|v| *v == 0.0); if diff > 1.0e-7 || diff > 0.0 && still { - s.velocity = e.velocity; - out.push(play::Clientbound::SetEntityMotion(SetEntityMotion { + self.velocity = e.velocity; + let motion = play::Clientbound::SetEntityMotion(SetEntityMotion { id: e.id, velocity: e.velocity, - })); + }); + match self.power { + // A bundle: the motion and the projectile's power. + Some(power) => out.extend([ + play::Clientbound::BundleDelimiter(horizon_protocol::packets::Empty), + motion, + play::Clientbound::ProjectilePower(play::ProjectilePower { + id: e.id, + power, + }), + play::Clientbound::BundleDelimiter(horizon_protocol::packets::Empty), + ]), + None => out.push(motion), + } } } if let Some(packet) = packet { @@ -221,48 +367,85 @@ impl Tracker { }; out.push(packet); if position { - s.base = e.position; + self.base = e.position; } if rotated { - s.rotation = rotation; + self.rotation = rotation; } } - // ServerEntity.sendDirtyEntityData. - if dirty { - let changed: Vec<_> = e - .meta - .iter() - .filter(|m| !s.meta.contains(m)) - .cloned() - .collect(); - if !changed.is_empty() { - out.push(play::Clientbound::SetEntityData(SetEntityData { - id: e.id, - values: changed, - })); - } - s.meta = e.meta.clone(); - } - let head = pack_degrees(e.head_yaw); - if turned(head, s.head) { - out.push(play::Clientbound::RotateHead(RotateHead { - id: e.id, - head_yaw: head, - })); - s.head = head; - } + self.send_dirty(e, dirty, out); + self.send_head(e, out); } - s.tick += 1; - // LivingEntity.detectEquipmentUpdates: changed slots every tick. - if e.equipment != s.equipment { + self.tick += 1; + self.send_equipment(e, out); + } + + /// ServerEntity.sendDirtyEntityData: the changed entity data, then the + /// attribute instances that changed since they were last sent + /// (`AttributeMap.getAttributesToSync`). + fn send_dirty(&mut self, e: &EntityState, dirty: bool, out: &mut Vec<play::Clientbound>) { + if dirty { + let mut changed: Vec<_> = e + .meta + .iter() + .filter(|m| !self.meta.contains(m)) + .cloned() + .collect(); + // A value back at its default is left out of the state, but the + // clients still hold the old one: Entity's shared flags and pose go + // back to 0 (a player standing up again, a mob no longer on fire). + for (index, kind, _) in &self.meta { + let default = match (*index, *kind) { + (0, serializer::BYTE) => Value::Byte(0), + (6, serializer::POSE) => Value::Id(0), + _ => continue, + }; + if !e.meta.iter().any(|m| m.0 == *index) { + changed.push((*index, *kind, default)); + } + } + if !changed.is_empty() { + out.push(play::Clientbound::SetEntityData(SetEntityData { + id: e.id, + values: changed, + })); + } + self.meta.clone_from(&e.meta); + } + if e.attributes != self.attributes { + let changed = changed_attributes(&e.attributes, &self.attributes); + if !changed.is_empty() { + out.push(play::Clientbound::UpdateAttributes(UpdateAttributes { + id: e.id, + values: changed, + })); + } + self.attributes.clone_from(&e.attributes); + } + } + + fn send_head(&mut self, e: &EntityState, out: &mut Vec<play::Clientbound>) { + let head = pack_degrees(e.head_yaw); + if (i32::from(head) - i32::from(self.head)).abs() >= 1 { + out.push(play::Clientbound::RotateHead(RotateHead { + id: e.id, + head_yaw: head, + })); + self.head = head; + } + } + + /// LivingEntity.detectEquipmentUpdates: changed slots every tick. + fn send_equipment(&mut self, e: &EntityState, out: &mut Vec<play::Clientbound>) { + if e.equipment != self.equipment { // Cleared slots are sent as empty stacks. let mut slots: Vec<(u8, Option<Stack>)> = e .equipment .iter() - .filter(|m| !s.equipment.contains(m)) + .filter(|m| !self.equipment.contains(m)) .cloned() .collect(); - for (slot, _) in &s.equipment { + for (slot, _) in &self.equipment { if !e.equipment.iter().any(|(k, _)| k == slot) { slots.push((*slot, None)); } @@ -273,9 +456,314 @@ impl Tracker { slots, })); } - s.equipment = e.equipment.clone(); + self.equipment.clone_from(&e.equipment); } - out + } +} + +/// The entities one client tracks (`TrackedEntity.seenBy`, from the +/// client's side). +#[derive(Default)] +pub struct Tracker { + seen: crate::fast_hash::FxHashSet<i32>, +} + +impl Tracker { + pub fn is_tracking(&self, id: i32) -> bool { + self.seen.contains(&id) + } + pub fn ids(&self) -> Vec<i32> { + self.seen.iter().copied().collect() + } + pub fn clear(&mut self) { + self.seen.clear(); + } + /// Stop tracking `id` without a packet: the caller removes it from the + /// client, and the next tick pairs it again if it is still in range. + pub fn forget(&mut self, id: i32) -> bool { + self.seen.remove(&id) + } + pub fn len(&self) -> usize { + self.seen.len() + } + pub fn is_empty(&self) -> bool { + self.seen.is_empty() + } +} + +/// Who a level's entities are tracked by this tick: a client's position, +/// view distance and chunks. +pub struct Viewer<'a> { + pub entity_id: i32, + /// It accepted a move since the last tick (ChunkMap.move): every pair + /// of it is checked again. + pub moved: bool, + pub position: [f64; 3], + pub view_distance: i32, + pub chunks: &'a crate::fast_hash::FxHashSet<(i32, i32)>, + pub tracker: &'a mut Tracker, + /// What the tick sends it, in order. + pub out: &'a mut Vec<Vec<u8>>, +} + +/// One entity as this tick's range checks read it: where it is +/// horizontally, its id, its type's tracking range in chunks and the chunk +/// it is in (an index into the tick's chunks). +#[derive(Clone, Copy)] +struct Probe { + x: f64, + z: f64, + id: i32, + range: i32, + chunk: u32, +} + +/// TrackedEntity.updatePlayer: within the type's range (capped by the view +/// distance) horizontally, in a chunk the client has. `held` remembers for +/// each of the tick's chunks whether the client has it (0: not looked up +/// yet, 1: no, 2: yes), so each chunk is looked up once per client. +fn sees(v: &Viewer, p: &Probe, chunks: &[(i32, i32)], held: &mut [u8]) -> bool { + let range = f64::from(p.range.min(v.view_distance) * 16); + let (dx, dz) = (v.position[0] - p.x, v.position[2] - p.z); + dx * dx + dz * dz <= range * range && { + let held = &mut held[p.chunk as usize]; + if *held == 0 { + *held = 1 + u8::from(v.chunks.contains(&chunks[p.chunk as usize])); + } + *held == 2 + } +} + +/// One level's tracked entities (ChunkMap.entityMap). +#[derive(Default)] +pub struct Entities { + states: crate::fast_hash::FxHashMap<i32, ServerEntity>, + /// The level tick, stamped on every entity still in the level. + tick: u32, + /// This tick's entities as the range checks read them, the chunks they + /// are in (and each one's index), whether the client being checked has + /// each chunk, and what each client makes of each entity ([`SEES`], + /// [`TRACKS`]), kept between ticks. + probes: Vec<Probe>, + chunks: Vec<(i32, i32)>, + chunk_index: crate::fast_hash::FxHashMap<(i32, i32), u32>, + held: Vec<u8>, + flags: Vec<Vec<u8>>, + /// Whether a client starts tracking each entity this tick. + pairs: Vec<bool>, + /// The entities that appeared or moved to another section this tick + /// (their pairs with every client are checked again), in order and as a + /// flag per entity. + moved: Vec<usize>, + is_moved: Vec<bool>, + /// One entity's packets and their bodies, kept between entities. + packets: Vec<play::Clientbound>, + bodies: Vec<Vec<u8>>, +} + +/// The client sees the entity this tick, and already tracked it. +const SEES: u8 = 1; +const TRACKS: u8 = 2; + +fn encode_all(packets: &[play::Clientbound], bodies: &mut Vec<Vec<u8>>) { + bodies.clear(); + bodies.extend(packets.iter().map(play::Clientbound::encode)); +} + +impl Entities { + pub fn len(&self) -> usize { + self.states.len() + } + pub fn is_empty(&self) -> bool { + self.states.is_empty() + } + + /// One tick of ChunkMap.tick for the entities in the level now and the + /// clients in it: each client first loses the entities that left its + /// range or the level (one ClientboundRemoveEntitiesPacket), then each + /// entity's changes are made once and go to every client tracking it, + /// and the clients that start tracking it are paired with it. A client + /// never tracks its own entity. `kind` names a type's registry id. + pub fn tick( + &mut self, + entities: &[&EntityState], + riding: &Riding, + viewers: &mut [Viewer], + mut kind: impl FnMut(&str) -> Option<i32>, + ) { + self.tick = self.tick.wrapping_add(1); + let now = self.tick; + self.probes.clear(); + self.chunks.clear(); + self.chunk_index.clear(); + self.moved.clear(); + self.is_moved.clear(); + self.is_moved.resize(entities.len(), false); + for (index, e) in entities.iter().enumerate() { + let mut added = false; + let s = self.states.entry(e.id).or_insert_with(|| { + added = true; + ServerEntity::new(e, kind(&e.kind), riding) + }); + s.seen = now; + s.index = index; + // ChunkMap.addEntity, and ChunkMap.tick's section change: + // updatePlayers. + let section = section_of(e.position); + if added || section != s.section { + s.section = section; + self.moved.push(index); + self.is_moved[index] = true; + } + let at = ( + (e.position[0].floor() as i32) >> 4, + (e.position[2].floor() as i32) >> 4, + ); + let next = self.chunks.len() as u32; + let chunk = *self.chunk_index.entry(at).or_insert(next); + if chunk == next { + self.chunks.push(at); + } + self.probes.push(Probe { + x: e.position[0], + z: e.position[2], + id: e.id, + range: s.range, + chunk, + }); + } + self.flags.resize_with(viewers.len(), Vec::new); + self.pairs.clear(); + self.pairs.resize(entities.len(), false); + for (v, flags) in viewers.iter_mut().zip(&mut self.flags) { + flags.clear(); + self.held.clear(); + self.held.resize(self.chunks.len(), 0); + let (chunks, held, pairs) = (&self.chunks, &mut self.held, &mut self.pairs); + let mut kept = 0; + // A client that moved (ChunkMap.move), or whose own entity + // appeared or changed section (ChunkMap.tick's movedPlayers), has + // every pair checked again. + let all = v.moved + || self + .states + .get(&v.entity_id) + .is_none_or(|s| s.seen != now || self.is_moved[s.index]); + if all { + flags.extend(self.probes.iter().zip(pairs.iter_mut()).map(|(p, pairs)| { + if p.id == v.entity_id || !sees(v, p, chunks, held) { + 0 + } else if v.tracker.seen.contains(&p.id) { + kept += 1; + SEES | TRACKS + } else { + *pairs = true; + SEES + } + })); + } else if v.tracker.seen.len() * 4 > self.probes.len() { + // Otherwise its pairs stay as they were, but for the entities + // that appeared or changed section; a client tracking much + // of the level goes through it in order. + let is_moved = &self.is_moved; + flags.extend(self.probes.iter().zip(pairs.iter_mut()).zip(is_moved).map( + |((p, pairs), &moved)| { + let tracks = v.tracker.seen.contains(&p.id); + if p.id == v.entity_id || (moved && !sees(v, p, chunks, held)) { + 0 + } else if tracks { + kept += 1; + SEES | TRACKS + } else if moved { + *pairs = true; + SEES + } else { + 0 + } + }, + )); + } else { + // One tracking little of it, from what it tracks. + flags.resize(self.probes.len(), 0); + for id in &v.tracker.seen { + if let Some(s) = self.states.get(id).filter(|s| s.seen == now) { + flags[s.index] = SEES | TRACKS; + kept += 1; + } + } + for &index in &self.moved { + let p = &self.probes[index]; + let tracks = flags[index] & TRACKS != 0; + if p.id != v.entity_id && sees(v, p, chunks, held) { + if !tracks { + flags[index] = SEES; + pairs[index] = true; + } + } else if tracks { + flags[index] = 0; + kept -= 1; + } + } + } + // Usually the client still sees all it tracks. + let mut removed = Vec::new(); + if kept != v.tracker.seen.len() { + let states = &self.states; + v.tracker.seen.retain(|id| match states.get(id) { + Some(s) if s.seen == now && flags[s.index] & TRACKS != 0 => true, + _ => { + removed.push(*id); + false + } + }); + } + if !removed.is_empty() { + v.out.push( + play::Clientbound::RemoveEntities(play::RemoveEntities { ids: removed }) + .encode(), + ); + } + } + for (index, e) in entities.iter().enumerate() { + let Some(s) = self.states.get_mut(&e.id) else { + continue; + }; + self.packets.clear(); + if !s.added { + s.send_changes(e, riding, &mut self.packets); + } + s.added = false; + // Nothing to send: no changes, and nobody starts tracking it. + if self.packets.is_empty() && !self.pairs[index] { + continue; + } + let mut encoded = false; + let mut pairing: Option<Vec<Vec<u8>>> = None; + for (v, flags) in viewers.iter_mut().zip(&self.flags) { + let f = flags[index]; + if f & TRACKS != 0 { + if self.packets.is_empty() { + continue; + } + if !encoded { + encode_all(&self.packets, &mut self.bodies); + encoded = true; + } + v.out.extend(self.bodies.iter().cloned()); + } else if f & SEES != 0 + && let Some(kind) = s.kind + { + let bodies = pairing.get_or_insert_with(|| { + let mut packets = Vec::new(); + s.pairing(e, kind, riding, &mut packets); + packets.iter().map(play::Clientbound::encode).collect() + }); + v.out.extend(bodies.iter().cloned()); + v.tracker.seen.insert(e.id); + } + } + } + self.states.retain(|_, s| s.seen == now); } } @@ -297,57 +785,176 @@ mod tests { data: 0, meta: Vec::new(), equipment: Vec::new(), + attributes: Vec::new(), + precise: false, } } + /// The entity as added this tick, then its updates (one a tick). + fn added(e: &EntityState) -> ServerEntity { + ServerEntity::new(e, Some(100), &Riding::default()) + } + fn changes(s: &mut ServerEntity, e: &EntityState) -> Vec<play::Clientbound> { + let mut out = Vec::new(); + s.send_changes(e, &Riding::default(), &mut out); + out + } + #[test] fn moves_go_out_every_update_interval() { - let mut t = Tracker::default(); - let spawn = t.spawn(&state(0.0), 100); - assert!(matches!(spawn[0], play::Clientbound::AddEntity(_))); + let mut s = added(&state(0.0)); + let mut spawn = Vec::new(); + s.pairing(&state(0.0), 100, &Riding::default(), &mut spawn); + assert!(matches!(spawn[..], [play::Clientbound::AddEntity(_)])); // Tick 0 is due and a multiple of 60: the position goes out unchanged. assert!(matches!( - t.update(&state(0.0))[..], + changes(&mut s, &state(0.0))[..], [play::Clientbound::MoveEntityPos(_)] )); // A zombie updates every third tick. - assert!(t.update(&state(0.5)).is_empty()); - assert!(t.update(&state(0.5)).is_empty()); + assert!(changes(&mut s, &state(0.5)).is_empty()); + assert!(changes(&mut s, &state(0.5)).is_empty()); assert!(matches!( - t.update(&state(0.5))[..], + changes(&mut s, &state(0.5))[..], [play::Clientbound::MoveEntityPos(_)] )); for _ in 0..2 { - t.update(&state(40.0)); + changes(&mut s, &state(40.0)); } - let jump = t.update(&state(40.0)); + let jump = changes(&mut s, &state(40.0)); assert!(matches!(jump[0], play::Clientbound::EntityPositionSync(_))); } #[test] - fn landing_sends_a_position_sync() { - let mut t = Tracker::default(); + fn fireballs_bundle_their_motion_with_their_power() { let mut e = state(0.0); - t.spawn(&e, 100); - t.update(&e); + e.kind = "minecraft:fireball".into(); e.on_ground = false; - t.update(&e); - t.update(&e); + let mut s = added(&e); + e.velocity = [0.0, 0.0, 0.5]; + let out = changes(&mut s, &e); assert!(matches!( - t.update(&e)[..], + out[..], + [ + play::Clientbound::BundleDelimiter(_), + play::Clientbound::SetEntityMotion(_), + play::Clientbound::ProjectilePower(play::ProjectilePower { power, .. }), + play::Clientbound::BundleDelimiter(_), + play::Clientbound::MoveEntityPos(_), + ] if power == 0.1 + )); + } + + #[test] + fn arrows_send_position_and_rotation_every_update() { + let mut e = state(0.0); + e.kind = "minecraft:arrow".into(); + let mut s = added(&e); + assert!(matches!( + changes(&mut s, &e)[..], + [play::Clientbound::MoveEntityPosRot(_)] + )); + } + + #[test] + fn precise_entities_sync_their_position() { + let mut e = state(0.0); + e.kind = "minecraft:tnt".into(); + e.precise = true; + let mut s = added(&e); + assert!(matches!( + changes(&mut s, &e)[..], [play::Clientbound::EntityPositionSync(_)] )); } #[test] - fn metadata_and_equipment_send_only_the_changes() { - let mut t = Tracker::default(); + fn landing_sends_a_position_sync() { let mut e = state(0.0); - t.spawn(&e, 100); - t.update(&e); + let mut s = added(&e); + changes(&mut s, &e); + e.on_ground = false; + changes(&mut s, &e); + changes(&mut s, &e); + assert!(matches!( + changes(&mut s, &e)[..], + [play::Clientbound::EntityPositionSync(_)] + )); + } + + #[test] + fn attributes_pair_whole_then_send_the_changed_instances_when_due() { + let speed = |base| AttributeSnapshot { + attribute: 26, + base, + modifiers: Vec::new(), + }; + let armor = AttributeSnapshot { + attribute: 1, + base: 0.0, + modifiers: Vec::new(), + }; + let mut e = state(0.0); + e.attributes = vec![speed(0.23), armor]; + let mut s = added(&e); + let mut spawn = Vec::new(); + s.pairing(&e, 100, &Riding::default(), &mut spawn); + let Some(play::Clientbound::UpdateAttributes(p)) = spawn.get(1) else { + panic!("expected the attributes after the add"); + }; + assert_eq!(p.values, e.attributes); + changes(&mut s, &e); + e.attributes[0] = speed(0.3); + // Not due (a zombie's interval is 3): sendDirtyEntityData waits for the update. + assert!(changes(&mut s, &e).is_empty()); + assert!(changes(&mut s, &e).is_empty()); + let out = changes(&mut s, &e); + let Some(play::Clientbound::UpdateAttributes(p)) = out + .iter() + .find(|p| matches!(p, play::Clientbound::UpdateAttributes(_))) + else { + panic!("expected the changed attribute"); + }; + assert_eq!(p.values, vec![speed(0.3)]); + } + + #[test] + fn cleared_flags_and_pose_go_back_to_their_default() { + let mut e = state(0.0); + let mut s = added(&e); + changes(&mut s, &e); + // A crouching player: the crouch flag and the crouching pose. + e.meta = vec![ + (0, serializer::BYTE, Value::Byte(2)), + (6, serializer::POSE, Value::Id(5)), + ]; + changes(&mut s, &e); + e.meta.clear(); + let data = changes(&mut s, &e) + .into_iter() + .find_map(|p| match p { + play::Clientbound::SetEntityData(d) => Some(d), + _ => None, + }) + .expect("standing up is sent"); + assert_eq!( + data.values, + vec![ + (0, serializer::BYTE, Value::Byte(0)), + (6, serializer::POSE, Value::Id(0)), + ] + ); + assert!(changes(&mut s, &e).is_empty()); + } + + #[test] + fn metadata_and_equipment_send_only_the_changes() { + let mut e = state(0.0); + let mut s = added(&e); + changes(&mut s, &e); e.meta.push((0, 0, Value::Byte(1))); e.equipment.push((0, Some(Stack::new(5, 1)))); - let out = t.update(&e); + let out = changes(&mut s, &e); assert!( out.iter() .any(|p| matches!(p, play::Clientbound::SetEntityData(_))) @@ -356,12 +963,210 @@ mod tests { out.iter() .any(|p| matches!(p, play::Clientbound::SetEquipment(_))) ); - assert!(t.update(&e).is_empty()); + assert!(changes(&mut s, &e).is_empty()); e.equipment.clear(); - let out = t.update(&e); + let out = changes(&mut s, &e); let Some(play::Clientbound::SetEquipment(eq)) = out.first() else { panic!("expected equipment"); }; assert_eq!(eq.slots, vec![(0, None)]); } + + /// Two clients at the origin; the chunks they hold. + struct Clients { + chunks: crate::fast_hash::FxHashSet<(i32, i32)>, + trackers: [Tracker; 2], + out: [Vec<Vec<u8>>; 2], + positions: [[f64; 3]; 2], + moved: [bool; 2], + } + + impl Clients { + fn new() -> Self { + Self { + chunks: (-4..4).flat_map(|x| (-4..4).map(move |z| (x, z))).collect(), + trackers: Default::default(), + out: Default::default(), + positions: [[0.0, 64.0, 0.0]; 2], + moved: [false; 2], + } + } + fn tick(&mut self, level: &mut Entities, entities: &[&EntityState]) { + for out in &mut self.out { + out.clear(); + } + let [t0, t1] = &mut self.trackers; + let [o0, o1] = &mut self.out; + let mut viewers = [ + Viewer { + entity_id: 1, + moved: self.moved[0], + position: self.positions[0], + view_distance: 10, + chunks: &self.chunks, + tracker: t0, + out: o0, + }, + Viewer { + entity_id: 2, + moved: self.moved[1], + position: self.positions[1], + view_distance: 10, + chunks: &self.chunks, + tracker: t1, + out: o1, + }, + ]; + level.tick(entities, &Riding::default(), &mut viewers, |_| Some(100)); + } + } + + fn body(p: play::Clientbound) -> Vec<u8> { + p.encode() + } + + /// ChunkMap checks a pair again only when one side moves: a zombie + /// walking into range within its section isn't paired with a client + /// standing still until the client moves, and one walking out stays + /// tracked until it changes section. + #[test] + fn pairs_are_checked_again_when_a_side_moves() { + let mut level = Entities::default(); + let mut clients = Clients::new(); + clients.positions[0] = [-108.0, 64.0, 0.0]; + let mut me = state(-108.0); + (me.id, me.kind) = (1, "minecraft:player".into()); + // 128.5 blocks away: past a zombie's 8 chunks. + let tracks = |clients: &Clients| clients.trackers[0].is_tracking(7); + clients.tick(&mut level, &[&me, &state(20.5)]); + assert!(!tracks(&clients)); + // 127.5 blocks away, in the same section: not checked again. + clients.tick(&mut level, &[&me, &state(19.5)]); + assert!(!tracks(&clients)); + // The client's move checks every pair of it. + clients.moved[0] = true; + clients.tick(&mut level, &[&me, &state(19.5)]); + assert!(tracks(&clients)); + clients.moved[0] = false; + clients.tick(&mut level, &[&me, &state(20.5)]); + assert!(tracks(&clients)); + // Into the next section, out of range: no longer tracked. + clients.tick(&mut level, &[&me, &state(40.0)]); + assert!(!tracks(&clients)); + // The other client (at the origin, its entity not in the level) + // checks every pair each tick. + assert!(clients.trackers[1].is_tracking(7)); + } + + #[test] + fn every_client_gets_the_same_changes_and_late_ones_are_paired() { + let mut level = Entities::default(); + let mut c = Clients::new(); + // The second client is out of range at first. + c.positions[1] = [500.0, 64.0, 0.0]; + let mut e = state(0.0); + c.tick(&mut level, &[&e]); + assert!(c.trackers[0].is_tracking(7) && !c.trackers[1].is_tracking(7)); + assert!(c.out[1].is_empty()); + // Tick 0's forced position goes to the client tracking it. + c.tick(&mut level, &[&e]); + assert_eq!(c.out[0].len(), 1); + // The second client comes in range: paired with the entity as it is, + // then sent the same changes as the first. + c.positions[1] = [0.0, 64.0, 0.0]; + e.position[0] = 1.0; + c.tick(&mut level, &[&e]); + assert!(c.trackers[1].is_tracking(7)); + assert!(c.out[0].is_empty()); + assert_eq!(c.out[1].len(), 1); + c.tick(&mut level, &[&e]); + e.position[0] = 2.0; + c.tick(&mut level, &[&e]); + assert!(!c.out[0].is_empty()); + assert_eq!(c.out[0], c.out[1]); + } + + #[test] + fn clients_lose_what_leaves_range_or_the_level_and_never_track_themselves() { + let mut level = Entities::default(); + let mut c = Clients::new(); + let mut me = state(0.0); + me.id = 1; + me.kind = "minecraft:player".into(); + let mut e = state(0.0); + c.tick(&mut level, &[&me, &e]); + assert!(!c.trackers[0].is_tracking(1) && c.trackers[1].is_tracking(1)); + assert!(c.trackers[0].is_tracking(7)); + // Out of the zombie's 8 chunks. + e.position[0] = 200.0; + c.tick(&mut level, &[&me, &e]); + assert!(!c.trackers[0].is_tracking(7)); + let removed = body(play::Clientbound::RemoveEntities(play::RemoveEntities { + ids: vec![7], + })); + assert_eq!(c.out[0].first(), Some(&removed)); + // Gone from the level: the other client loses it too, and so does + // the level. + e.position[0] = 0.0; + c.tick(&mut level, &[&me, &e]); + assert!(c.trackers[0].is_tracking(7)); + c.tick(&mut level, &[&me]); + assert!(!c.trackers[0].is_tracking(7) && !c.trackers[1].is_tracking(7)); + assert_eq!(c.out[0].first(), Some(&removed)); + assert_eq!(level.len(), 1); + } + + #[test] + fn entities_in_chunks_a_client_lacks_wait_for_them() { + let mut level = Entities::default(); + let mut c = Clients::new(); + // Two zombies in range in chunk (4, 0), which the clients don't have + // yet, and one in chunk (0, 0). + let mut far = state(70.0); + let mut near = state(66.0); + near.id = 8; + let mut home = state(1.0); + home.id = 9; + c.tick(&mut level, &[&far, &near, &home]); + for t in &c.trackers { + assert!(!t.is_tracking(7) && !t.is_tracking(8) && t.is_tracking(9)); + } + // The chunk arrives: both are paired, in order. + c.chunks.insert((4, 0)); + c.tick(&mut level, &[&far, &near, &home]); + for t in &c.trackers { + assert!(t.is_tracking(7) && t.is_tracking(8) && t.is_tracking(9)); + } + // One walks back into chunk (3, 0) and the other out of range. + far.position[0] = 200.0; + near.position[0] = 60.0; + c.tick(&mut level, &[&far, &near, &home]); + for t in &c.trackers { + assert!(!t.is_tracking(7) && t.is_tracking(8) && t.is_tracking(9)); + } + } + + #[test] + fn a_forgotten_entity_is_paired_again_at_the_next_tick() { + // A respawned player: the hub removes it from the other clients and + // forgets it, and the next tick pairs it again as a new entity. + let mut level = Entities::default(); + let mut c = Clients::new(); + let mut player = state(0.0); + player.id = 1; + player.kind = "minecraft:player".into(); + c.tick(&mut level, &[&player]); + c.tick(&mut level, &[&player]); + assert!(c.trackers[1].forget(1)); + assert!(!c.trackers[1].forget(1)); + c.tick(&mut level, &[&player]); + assert!(c.trackers[1].is_tracking(1)); + let paired = c.out[1] + .first() + .and_then(|p| play::Clientbound::decode(i32::from(p[0]), &p[1..]).ok()); + assert!( + matches!(paired, Some(play::Clientbound::AddEntity(ref a)) if a.id == 1), + "{paired:?}" + ); + } } diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index 108e0c2c7..91bf2709f 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -8,7 +8,7 @@ use horizon_protocol::{ packets::{configuration::ClientInformation, play}, }; use std::{ - collections::{HashSet, VecDeque}, + collections::VecDeque, sync::{Arc, atomic::AtomicI32}, }; @@ -37,6 +37,12 @@ pub struct Guest { pub last_action_tick: u64, /// The next ClientboundPlayerChatPacket.globalIndex for this connection. pub chat_index: i32, + /// Secure chat (ServerGamePacketListenerImpl): the player's message + /// chain, keyed once it sends a chat session; the messages its + /// acknowledgements cover; the signatures its client caches. + pub chain: horizon_protocol::secure_chat::Chain, + pub last_seen: horizon_protocol::secure_chat::LastSeenValidator, + pub signatures: horizon_protocol::secure_chat::SignatureCache, /// The hub tick the player was placed on: chunks go out from the tick /// after next, behind the first doTick's packets, as vanilla's do. pub placed_at: u64, @@ -46,6 +52,8 @@ pub struct Guest { pub left_reason: String, pub uuid: u128, pub name: String, + /// The profile's properties, for the player list (skins). + pub properties: Vec<horizon_protocol::packets::login::Property>, pub entity_id: i32, pub sender: PlayerSender, pub latency: Arc<AtomicI32>, @@ -67,19 +75,49 @@ pub struct Guest { pub fall_flying: bool, pub flying: bool, pub sleeping: bool, + /// `LivingEntity.DATA_LIVING_ENTITY_FLAGS`: 1 while using an item, 2 if + /// it is in the off hand. + pub living_flags: i8, + /// The bed (its head) the player sleeps in (`LivingEntity.getSleepingPos`), + /// `Player.sleepCounter`, and the bed last told to the player itself. + pub bed: Option<[i32; 3]>, + pub sleep_timer: u32, + pub sent_bed: Option<[i32; 3]>, + /// Where the player respawns (a bed's spot; `ServerPlayer.respawnConfig`), + /// None for the world spawn. + pub spawn: Option<[f64; 3]>, /// Pending ClientboundPlayerPositionPacket id; movement is ignored until /// the client accepts it (ServerGamePacketListenerImpl.awaitingPositionFromClient). pub awaiting_teleport: Option<(i32, [f64; 3])>, pub teleport_counter: i32, + /// The hub tick the pending teleport was (re)sent on (awaitingTeleportTime). + pub teleport_tick: u64, + /// Where the player was when this tick's packets began (firstGoodX/Y/Z), + /// for the moved-too-quickly check. + pub first_good: [f64; 3], + /// Position moves received in all and by the start of this tick + /// (receivedMovePacketCount, knownMovePacketCount). + pub received_moves: i32, + pub known_moves: i32, + /// A position move arrived since the client's last ClientTickEnd + /// (receivedPositionThisTick): a second one is a protocol error. + pub position_this_tick: bool, + /// It accepted a move since the last entity range checks + /// (ServerChunkCache.move), which then check all of its pairs again. + pub moved: bool, /// The client finished loading terrain (ServerboundPlayerLoadedPacket). pub loaded: bool, // Chunks. pub center: Option<(i32, i32)>, - pub sent_chunks: HashSet<(i32, i32)>, + pub sent_chunks: crate::fast_hash::FxHashSet<(i32, i32)>, pub chunk_queue: Vec<(i32, i32)>, + /// PlayerChunkSender: batches sent but not yet acknowledged, how many + /// may be (1 until the client's first acknowledgement, then 10), the + /// rate the client asks for and the chunks the next batch may hold. pub unacknowledged: u32, + pub max_unacknowledged: u32, pub chunks_per_tick: f32, - pub chunk_credit: f32, + pub batch_quota: f32, // Entities. pub tracker: Tracker, // Player state the gameplay code reads and writes. @@ -111,7 +149,8 @@ pub struct Guest { /// What the client believes the inventory menu holds (for change detection). pub menu_sent: Vec<Option<ItemStack>>, pub menu_state: i32, - pub sent_health: Option<(f32, i32, f32)>, + /// `lastSentHealth`, `lastSentFood` and `lastFoodSaturationZero`. + pub sent_health: Option<(f32, i32, bool)>, pub sent_xp: Option<(f32, i32, i32)>, /// Where the player last died (Player.lastDeathLocation), sent with the respawn. pub last_death: Option<[i32; 3]>, @@ -129,6 +168,9 @@ pub struct Guest { pub dead: bool, /// The join packets were sent (PlayerList.placeNewPlayer ran). pub placed: bool, + /// The level the player is in (`ServerPlayer.level().dimension()`): the + /// hub's level passes only reach the players in theirs. + pub dimension: String, /// The open block container, if any. pub menu: Option<super::menu::Menu>, pub next_window: i32, @@ -136,6 +178,19 @@ pub struct Guest { pub drag: Option<(u8, Vec<usize>)>, /// The compound this player was loaded from, for fields Horizon does not model. pub saved: Option<horizon_world::nbt::Tag>, + /// `ServerPlayer.recipeBook`. + pub recipe_book: crate::recipe_book::Settings, + /// `PlayerAdvancements`: the criteria obtained, whether they came from + /// the player's file, the advancements the client was last told it may + /// see (empty before the first packet), and those whose progress + /// changed since (`progressChanged`). + pub advancements: crate::advancements::Progress, + pub advancements_loaded: bool, + pub advancements_visible: Vec<bool>, + pub advancements_changed: std::collections::BTreeSet<usize>, + /// Recipes unlocked since the client was last told + /// (`ServerRecipeBook.addRecipes` sends them). + pub recipes_unlocked: Vec<String>, } impl Guest { @@ -145,11 +200,15 @@ impl Guest { address: j.address, last_action_tick: 0, chat_index: 0, + chain: Default::default(), + last_seen: Default::default(), + signatures: Default::default(), placed_at: 0, center_sent: None, left_reason: String::new(), uuid: j.uuid, name: j.name, + properties: j.properties, entity_id, sender: j.sender, latency: j.latency, @@ -169,15 +228,27 @@ impl Guest { fall_flying: false, flying: false, sleeping: false, + living_flags: 0, + bed: None, + sleep_timer: 0, + sent_bed: None, + spawn: None, awaiting_teleport: None, teleport_counter: 0, + teleport_tick: 0, + first_good: position, + received_moves: 0, + known_moves: 0, + position_this_tick: false, + moved: false, loaded: false, center: None, - sent_chunks: HashSet::new(), + sent_chunks: Default::default(), chunk_queue: Vec::new(), unacknowledged: 0, + max_unacknowledged: 1, chunks_per_tick: 9.0, - chunk_credit: 0.0, + batch_quota: 0.0, tracker: Tracker::default(), motion: None, inventory: vec![None; 36], @@ -210,10 +281,17 @@ impl Guest { moves_from_ground: false, dead: false, placed: false, + dimension: "minecraft:overworld".into(), menu: None, next_window: 0, drag: None, saved: None, + recipe_book: Default::default(), + advancements: Default::default(), + advancements_loaded: false, + advancements_visible: Vec::new(), + advancements_changed: Default::default(), + recipes_unlocked: Vec::new(), } } pub fn send(&self, packet: &play::Clientbound) { diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index e9255d822..625f18eff 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -8,30 +8,41 @@ use super::{ Lan, NetEvent, chunks::ChunkEncoder, data::SyncData, - entity::EntityState, + entity::{Entities, EntityState, Viewer}, guest::{Guest, SentEffect, stack_to_network}, }; use crate::{ + chunk::Chunk, + collisions::SceneCollisions, effects::{EffectInstance, PlayerEffects}, items::ItemStack, world::World, }; +use horizon_core::geometry::{Aabb, ShapeOp, Vec3, VoxelShape}; use horizon_protocol::ids::play::clientbound as ids; use horizon_protocol::{ entity_data::{Value, serializer}, packets::play::{self, Clientbound as C, Serverbound as S}, text::{literal, translatable}, }; -use horizon_world::nbt::{JavaString, Tag}; +use horizon_world::{ + block_collision::BlockCollisionRegistry, + block_collision_scan::BlockCollisionScan, + collision_context::CollisionContext, + nbt::{JavaString, Tag}, +}; use std::sync::{Arc, atomic::Ordering}; /// GameEvent types (ClientboundGameEventPacket). +pub mod chat; + pub mod game_event { pub const START_RAINING: u8 = 1; pub const STOP_RAINING: u8 = 2; pub const CHANGE_GAME_MODE: u8 = 3; pub const RAIN_LEVEL: u8 = 7; pub const THUNDER_LEVEL: u8 = 8; + pub const GUARDIAN_ELDER_EFFECT: u8 = 10; pub const LEVEL_CHUNKS_LOAD_START: u8 = 13; } @@ -70,6 +81,10 @@ pub struct HostState { pub sprinting: bool, pub equipment: [Option<ItemStack>; 6], pub health: f32, + /// The host player's client-syncable attribute instances. + pub attributes: Vec<play::AttributeSnapshot>, + /// `LivingEntity.DATA_LIVING_ENTITY_FLAGS` (the item in use). + pub living_flags: i8, } /// Everything the hub needs from the game for one tick. @@ -88,6 +103,9 @@ pub struct TickCtx<'a> { /// TickRateManager: the tick rate and whether the game is frozen. pub tick_rate: f32, pub tick_frozen: bool, + /// Block collisions, for ServerGamePacketListenerImpl's moved-wrongly + /// check (none: moves are not checked against blocks). + pub collision: Option<&'a BlockCollisionRegistry<'a>>, } /// What the gameplay code has to do for a guest. @@ -97,8 +115,16 @@ pub enum HostEvent { /// A guest left (state to save, entities to drop). Left(Box<Guest>), Packet(u32, Box<S>), - /// A chat line to show on the host (already broadcast to the guests). - Chat(String), + /// A chat line to show on the host (already broadcast to the guests); + /// a dedicated server logs it, `[Not Secure]` unless signed and fresh. + Chat { + line: String, + secure: bool, + }, + /// A server log warning (ServerGamePacketListenerImpl's LOGGER.warn). + Warn(String), + /// A server log error (LOGGER.error). + Error(String), } pub struct Hub { @@ -108,10 +134,21 @@ pub struct Hub { pub guests: Vec<Guest>, pub(super) sync: Arc<SyncData>, pub ticks: u64, + /// The `player_movement_check` and `elytra_movement_check` game rules, + /// set by the game before each tick. + pub movement_checks: (bool, bool), + /// An integrated server's owner (`MinecraftServer.isSingleplayerOwner`), + /// whose moves are never checked. + pub singleplayer_owner: Option<u128>, + /// Who rides what, set by the game before each tick. + pub riding: super::entity::Riding, last_clock: i64, last_rate: f32, ticking: (f32, bool), weather: (bool, f32, f32), + /// The clock and weather last sent in the levels not in place, by + /// dimension (`set_dimension` swaps them with the fields above). + level_sync: std::collections::HashMap<String, LevelSync>, /// Operators and their permission levels (dedicated servers). pub ops: std::collections::HashMap<u128, u8>, chat_type: i32, @@ -119,6 +156,10 @@ pub struct Hub { clock_ids: Vec<i32>, item_ids: std::collections::HashMap<Arc<str>, i32>, entity_types: std::collections::HashMap<String, i32>, + /// Each level's tracked entities, by dimension. + entities: std::collections::HashMap<String, Entities>, + /// What one tick of entity tracking sends each guest, kept between ticks. + entity_out: Vec<Vec<Vec<u8>>>, } fn string(s: &str) -> Tag { @@ -172,6 +213,20 @@ fn yellow_names(key: &str, names: Vec<Tag>) -> Tag { ]) } +/// An entity type's registry id, remembered once looked up. +fn entity_type( + types: &mut std::collections::HashMap<String, i32>, + sync: &SyncData, + kind: &str, +) -> Option<i32> { + if let Some(id) = types.get(kind) { + return Some(*id); + } + let id = sync.builtin.get("minecraft:entity_type")?.id(kind)?; + types.insert(kind.to_owned(), id); + Some(id) +} + /// ChunkTrackingView.contains: the chunks a player at (cx, cz) with view /// distance `r` is sent (isWithinDistance with the neighbours' buffer). fn tracked(cx: i32, cz: i32, r: i32, x: i32, z: i32) -> bool { @@ -180,15 +235,36 @@ fn tracked(cx: i32, cz: i32, r: i32, x: i32, z: i32) -> bool { dx * dx + dz * dz < i64::from(r) * i64::from(r) } +/// What a level's players were last told of its clock and weather. +#[derive(Clone, Copy)] +struct LevelSync { + last_clock: i64, + last_rate: f32, + weather: (bool, f32, f32), +} + +impl Default for LevelSync { + fn default() -> Self { + Self { + last_clock: i64::MIN, + last_rate: 0.0, + weather: (false, 0.0, 0.0), + } + } +} + impl Hub { pub fn new(lan: Lan, sync: Arc<SyncData>, encoder: ChunkEncoder, config: HubConfig) -> Self { let chat_type = sync .id("minecraft:chat_type", "minecraft:chat") .unwrap_or(0); let dimension_type = sync - .id("minecraft:dimension_type", "minecraft:overworld") + .id("minecraft:dimension_type", &config.dimension) .unwrap_or(0); let clock_ids = (0..sync.dynamic.len("minecraft:world_clock") as i32).collect(); + let mut encoder = encoder; + let d = horizon_protocol::registry::DimensionType::vanilla(&config.dimension); + encoder.set_dimension(d.min_y, d.height, d.has_skylight); Self { lan, config, @@ -196,16 +272,22 @@ impl Hub { guests: Vec::new(), sync, ticks: 0, + movement_checks: (true, true), + singleplayer_owner: None, + riding: Default::default(), last_clock: i64::MIN, last_rate: 0.0, ticking: (20.0, false), ops: Default::default(), weather: (false, 0.0, 0.0), + level_sync: Default::default(), chat_type, dimension_type, clock_ids, item_ids: Default::default(), entity_types: Default::default(), + entities: Default::default(), + entity_out: Vec::new(), } } @@ -239,14 +321,6 @@ impl Hub { let sync = self.sync.clone(); stack_to_network(|_| id, |name| sync.id("minecraft:enchantment", name), s) } - fn entity_type(&mut self, kind: &str) -> Option<i32> { - if let Some(id) = self.entity_types.get(kind) { - return Some(*id); - } - let id = self.sync.builtin.get("minecraft:entity_type")?.id(kind)?; - self.entity_types.insert(kind.to_owned(), id); - Some(id) - } pub fn broadcast(&self, packet: &C) { let body = packet.encode(); @@ -254,6 +328,34 @@ impl Hub { g.sender.send(body.clone()); } } + /// PlayerList.setViewDistance: every player is told the new radius, + /// and each one's chunks follow it, up to what they asked for + /// (ChunkMap.getPlayerViewDistance). + pub fn set_view_distance(&mut self, distance: i32) { + self.config.view_distance = distance; + self.broadcast(&C::SetChunkCacheRadius(play::Distance { distance })); + for g in &mut self.guests { + g.view_distance = i32::from(g.info.view_distance).clamp(2, distance); + g.center = None; + } + } + /// PlayerList.setSimulationDistance. + pub fn set_simulation_distance(&mut self, distance: i32) { + self.config.simulation_distance = distance; + self.broadcast(&C::SetSimulationDistance(play::Distance { distance })); + } + /// Whether a guest is placed in the level in place (`config.dimension`). + pub fn here(&self, g: &Guest) -> bool { + g.placed && g.dimension == self.config.dimension + } + /// `PlayerList.broadcastAll(packet, dimension)`: the players in the + /// level in place. + pub fn broadcast_level(&self, packet: &C) { + let body = packet.encode(); + for g in self.guests.iter().filter(|g| self.here(g)) { + g.sender.send(body.clone()); + } + } pub fn broadcast_except(&self, conn: u32, packet: &C) { let body = packet.encode(); for g in self.guests.iter().filter(|g| g.placed && g.conn != conn) { @@ -354,51 +456,23 @@ impl Hub { }, })); } - /// PlayerList.broadcastChatMessage of a guest's unsigned message - /// (PlayerChatMessage.unsigned): a ClientboundPlayerChatPacket to every - /// player, each connection numbering its messages. - pub fn player_chat(&mut self, from: usize, text: &str) { - let (uuid, name) = (self.guests[from].uuid, self.guests[from].name.clone()); - let timestamp = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .map_or(0, |d| d.as_millis() as i64); - let bound = play::ChatTypeBound { - chat_type: self.chat_type, - name: display_name(uuid, &name), - target: None, - }; - for g in self.guests.iter_mut().filter(|g| g.placed) { - let packet = C::PlayerChat(play::PlayerChat { - global_index: g.chat_index, - sender: uuid, - index: 0, - signature: None, - content: text.to_owned(), - timestamp, - salt: 0, - last_seen: Vec::new(), - unsigned_content: None, - filter: (0, Vec::new()), - chat_type: bound.clone(), - }); - g.chat_index += 1; - g.send(&packet); - } - } - /// Disconnect a guest with a reason (ServerGamePacketListenerImpl.disconnect). pub fn kick(&mut self, conn: u32, reason: Tag) { - if let Some(g) = self.guest(conn) { + // The log's "lost connection" line gives the reason, as vanilla's. + let text = self.english(&reason); + if let Some(g) = self.guest_mut(conn) { + g.left_reason = text; g.sender.disconnect(reason); } } fn player_info(&self, uuid: u128, name: &str, mode: i32, latency: i32) -> play::PlayerInfo { + let guest = self.guests.iter().find(|g| g.uuid == uuid); play::PlayerInfo { uuid, name: Some(name.to_owned()), - properties: Vec::new(), - chat_session: None, + properties: guest.map(|g| g.properties.clone()).unwrap_or_default(), + chat_session: guest.and_then(|g| g.chain.session()).map(|s| s.data()), game_mode: Some(mode), listed: Some(true), latency: Some(latency), @@ -440,6 +514,7 @@ impl Hub { guest.joined_tick = self.ticks; guest.position = ctx.spawn; guest.previous_position = ctx.spawn; + guest.dimension = self.config.dimension.clone(); self.guests.push(guest); conn } @@ -490,8 +565,9 @@ impl Hub { show_death_screen: true, limited_crafting: false, spawn: self.spawn_info(mode), - online_mode: false, - enforces_secure_chat: false, + // MinecraftServer.usesAuthentication, enforceSecureProfile. + online_mode: self.lan.shared.config.online.is_some(), + enforces_secure_chat: self.enforces_secure_chat(), })); g.send(&C::ChangeDifficulty(play::ChangeDifficulty { difficulty: ctx.difficulty, @@ -509,12 +585,18 @@ impl Hub { g.send(&raw(ids::UPDATE_RECIPES, self.sync.recipes.clone())); // PlayerList.sendPlayerPermissionLevel, then the recipe book. self.send_permission_level(i); + // ServerRecipeBook.sendInitialRecipeBook: each book's open and + // filtering flags, then every known recipe. let g = &self.guests[i]; + let book = &g.recipe_book; + let settings = (0..4) + .flat_map(|b| [u8::from(book.open[b]), u8::from(book.filtering[b])]) + .collect(); + g.send(&raw(ids::RECIPE_BOOK_SETTINGS, settings)); g.send(&raw( - ids::RECIPE_BOOK_SETTINGS, - vec![0; 8], // four books, each closed and unfiltered + ids::RECIPE_BOOK_ADD, + self.sync.advancements.initial_recipe_book(book, &self.sync), )); - g.send(&raw(ids::RECIPE_BOOK_ADD, vec![0, 1])); // no entries, replace // The others hear of the newcomer before it joins the list. self.broadcast_except( conn, @@ -609,6 +691,16 @@ impl Hub { /// onEffectsRemoved, from what changed since the client was told: an /// effect that only counted down is not sent again, except at every /// 600 ticks left (`LivingEntity.tickEffects`). + /// `sync_effects` for every player at once: what a command changed + /// leaves with its feedback (ServerPlayer.onEffectAdded sends at once), + /// not after the tick's countdown. + pub fn sync_all_effects(&mut self) { + for i in 0..self.guests.len() { + if self.guests[i].placed { + self.sync_effects(i); + } + } + } fn sync_effects(&mut self, i: usize) { let g = &self.guests[i]; if g.effects.list.is_empty() && g.sent_effects.is_empty() { @@ -713,7 +805,9 @@ impl Hub { /// ServerGamePacketListenerImpl.teleport: move the client and wait for its confirmation. pub fn teleport(&mut self, i: usize, position: Option<[f64; 3]>) { + let ticks = self.ticks; let g = &mut self.guests[i]; + g.teleport_tick = ticks; if let Some(p) = position { g.position = p; g.previous_position = p; @@ -731,11 +825,12 @@ impl Hub { })); } - /// ClientboundSetDefaultSpawnPositionPacket for the level's respawn data. + /// ClientboundSetDefaultSpawnPositionPacket for the level's respawn data + /// (the world spawn, in the Overworld wherever the player is). fn default_spawn(&self, spawn: [f64; 3]) -> C { C::SetDefaultSpawnPosition(play::SetDefaultSpawnPosition { pos: play::GlobalPos { - dimension: self.config.dimension.clone(), + dimension: "minecraft:overworld".into(), pos: [ spawn[0].floor() as i32, spawn[1].floor() as i32, @@ -823,6 +918,20 @@ impl Hub { } /// ServerPlayer.initInventoryMenu / sendAllDataToRemote for the inventory menu. + /// ClientboundPlaceGhostRecipePacket: the recipe `id` faded into the + /// guest's menu `container`, whose items were missing. + pub fn send_ghost_recipe(&mut self, conn: u32, container: i32, id: &str) { + let Some(display) = self.sync.advancements.ghost_display(id, &self.sync) else { + return; + }; + self.send_to( + conn, + &C::PlaceGhostRecipe(horizon_protocol::packets::recipes::PlaceGhostRecipe { + container, + display, + }), + ); + } pub fn send_inventory(&mut self, i: usize) { let items: Vec<Option<ItemStack>> = (0..46).map(|s| self.guests[i].menu_slot(s)).collect(); let net: Vec<_> = items.iter().map(|s| self.stack(s)).collect(); @@ -842,7 +951,19 @@ impl Hub { // ---- per tick ------------------------------------------------------ + /// A whole tick with one level: the network, the level, then the + /// players' latencies. pub fn tick(&mut self, ctx: &mut TickCtx) -> Vec<HostEvent> { + let mut events = self.poll(ctx); + events.extend(self.tick_level(ctx)); + self.end_tick(); + events + } + + /// The network since the last tick, once per server tick before any + /// level ticks: joins (into the level in place), leaves, and each guest's + /// packets queued for its own level's pass. + pub fn poll(&mut self, ctx: &mut TickCtx) -> Vec<HostEvent> { self.ticks += 1; let mut events = Vec::new(); for event in self.lan.poll() { @@ -878,31 +999,50 @@ impl Hub { } } } + events + } + + /// The level in place's part of the tick (`ServerLevel.tick`'s chunk + /// map and the connections of the players in it): their packets, then + /// the chunks, block changes, containers and entities they see. + pub fn tick_level(&mut self, ctx: &mut TickCtx) -> Vec<HostEvent> { + let mut events = Vec::new(); for i in 0..self.guests.len() { - if self.guests[i].placed { + if self.here(&self.guests[i]) { self.process_inbox(i, ctx, &mut events); } } - if !self.guests.iter().any(|g| g.placed) { - if self.guests.is_empty() { + if !self.guests.iter().any(|g| self.here(g)) { + // ServerTickRateManager broadcasts a change to the players there + // are; one who joins later is sent the state as it is then + // (`updateJoiningPlayer`). + if !self.guests.iter().any(|g| g.placed) { + self.ticking = (ctx.tick_rate, ctx.tick_frozen); + } + if !self + .guests + .iter() + .any(|g| g.dimension == self.config.dimension) + { let _ = ctx.world.block_log.take(); + let _ = ctx.world.light_log.take(); } return events; } if ctx.world.block_log.is_none() { ctx.world.block_log = Some(Vec::new()); } - self.sync_world(ctx); - for i in 0..self.guests.len() { - if self.guests[i].placed { - self.update_chunks(i, ctx); - } + if ctx.world.light_log.is_none() { + ctx.world.light_log = Some(Default::default()); } + self.sync_world(ctx); + crate::spans::time("lan_chunks", || self.update_chunks(ctx)); + self.broadcast_light_changes(ctx.world); self.broadcast_block_changes(ctx.world); // ServerGamePacketListenerImpl.tick: the actions handled since the last tick are // acknowledged after the block changes they made were sent, so a client's predicted // blocks settle on the server's states. - for g in &self.guests { + for g in self.guests.iter().filter(|g| self.here(g)) { let sequence = g.ack_up_to.replace(-1); if sequence >= 0 { g.send(&C::BlockChangedAck(play::BlockChangedAck { sequence })); @@ -911,17 +1051,24 @@ impl Hub { self.sync_containers(ctx.world); self.track_entities(ctx); for i in 0..self.guests.len() { - if self.guests[i].placed { + if self.here(&self.guests[i]) { self.sync_player_state(i); self.sync_effects(i); } } + events + } + + /// After every level: `PlayerList.tick`. + pub fn end_tick(&mut self) { let latencies: Vec<_> = self .guests .iter() .map(|g| (g.uuid, g.latency.load(Ordering::Relaxed))) .collect(); - if self.ticks.is_multiple_of(100) && !latencies.is_empty() { + // PlayerList.tick: UPDATE_LATENCY once `++sendAllPlayerInfoIn > 600`, + // so on the 601st tick, then every 601. + if self.ticks.is_multiple_of(601) && !latencies.is_empty() { let entries = latencies .iter() .map(|(uuid, latency)| play::PlayerInfo { @@ -935,12 +1082,13 @@ impl Hub { entries, })); } - events } fn remove_guest(&mut self, i: usize, reason: &str) -> Guest { let mut g = self.guests.remove(i); - g.left_reason = reason.to_owned(); + if g.left_reason.is_empty() { + g.left_reason = reason.to_owned(); + } if std::env::var_os("HORIZON_NET_DEBUG").is_some() { log::info!("[lan] {} left: {reason}", g.name); } @@ -966,7 +1114,7 @@ impl Hub { // MinecraftServer.tickServer: the game time every 20 ticks // (forceGameTimeSynchronization, no clocks). if self.ticks.is_multiple_of(20) { - self.broadcast(&C::SetTime(play::SetTime { + self.broadcast_level(&C::SetTime(play::SetTime { game_time: world.game_time, clocks: Vec::new(), })); @@ -982,7 +1130,7 @@ impl Hub { .sync .id("minecraft:world_clock", "minecraft:overworld") .unwrap_or(0); - self.broadcast(&C::SetTime(play::SetTime { + self.broadcast_level(&C::SetTime(play::SetTime { game_time: world.game_time, clocks: vec![play::ClockState { clock: overworld, @@ -1001,7 +1149,7 @@ impl Hub { let w = &world.weather; let (raining, rain, thunder) = self.weather; if w.raining != raining { - self.broadcast(&C::GameEvent(play::GameEvent { + self.broadcast_level(&C::GameEvent(play::GameEvent { event: if w.raining { game_event::START_RAINING } else { @@ -1011,13 +1159,13 @@ impl Hub { })); } if (w.rain_level.1 - rain).abs() > f32::EPSILON { - self.broadcast(&C::GameEvent(play::GameEvent { + self.broadcast_level(&C::GameEvent(play::GameEvent { event: game_event::RAIN_LEVEL, param: w.rain_level.1, })); } if (w.thunder_level.1 - thunder).abs() > f32::EPSILON { - self.broadcast(&C::GameEvent(play::GameEvent { + self.broadcast_level(&C::GameEvent(play::GameEvent { event: game_event::THUNDER_LEVEL, param: w.thunder_level.1, })); @@ -1027,86 +1175,140 @@ impl Hub { // ---- chunks -------------------------------------------------------- - fn update_chunks(&mut self, i: usize, ctx: &TickCtx) { + /// Every player's chunks here: their views, then the next batch of each + /// (PlayerChunkSender.sendNextChunks). The batches are chosen first, the + /// columns in them encoded once each, their new sections on the warm-up + /// threads too (`ChunkEncoder::warm`), then sent: nothing changes the + /// level in between, and each player's packets keep their order. + fn update_chunks(&mut self, ctx: &TickCtx) { + let world = &*ctx.world; + let mut batches = Vec::new(); + for i in 0..self.guests.len() { + if self.here(&self.guests[i]) { + self.update_view(i); + if let Some(batch) = self.next_batch(i, world) { + batches.push((i, batch)); + } + } + } + if batches.is_empty() { + return; + } + // Each column once, however many players it goes to, with the + // number of batches that take it. + let (mut columns, mut uses): (Vec<&Chunk>, Vec<usize>) = Default::default(); + let mut index = crate::fast_hash::FxHashMap::default(); + let batches: Vec<(usize, Vec<usize>, Vec<usize>)> = batches + .into_iter() + .map(|(i, batch)| { + let (taken, bodies) = batch + .into_iter() + .map(|(idx, chunk)| { + let j = *index.entry((chunk.x, chunk.z)).or_insert_with(|| { + columns.push(chunk); + uses.push(0); + columns.len() - 1 + }); + uses[j] += 1; + (idx, j) + }) + .unzip(); + (i, taken, bodies) + }) + .collect(); + self.encoder.warm(&columns); + let mut bodies: Vec<Vec<u8>> = columns + .iter() + .map(|c| self.encoder.body_in(c, world)) + .collect(); + for (i, taken, batch) in batches { + let g = &mut self.guests[i]; + let size = batch.len(); + g.batch_quota -= size as f32; + g.send(&C::ChunkBatchStart(horizon_protocol::packets::Empty)); + g.sender.send_many(batch.into_iter().map(|j| { + uses[j] -= 1; + if uses[j] == 0 { + std::mem::take(&mut bodies[j]) + } else { + bodies[j].clone() + } + })); + g.send(&C::ChunkBatchFinished(play::ChunkBatchFinished { + size: size as i32, + })); + g.unacknowledged += 1; + for idx in taken.into_iter().rev() { + let key = g.chunk_queue.remove(idx); + g.sent_chunks.insert(key); + } + } + } + + /// ChunkMap.move's view change: the new centre, the chunks that left + /// the view forgotten and the ones that entered it queued, closest first. + fn update_view(&mut self, i: usize) { let (cx, cz) = self.guests[i].chunk(); let r = self.guests[i].view_distance; let g = &mut self.guests[i]; - if g.center != Some((cx, cz)) { - g.center = Some((cx, cz)); - if g.center_sent.take() != Some((cx, cz)) { - g.send(&C::SetChunkCacheCenter(play::SetChunkCacheCenter { - x: cx, - z: cz, - })); - } - // Chunks that left the view. - let forget: Vec<_> = g - .sent_chunks - .iter() - .copied() - .filter(|&(x, z)| !tracked(cx, cz, r, x, z)) - .collect(); - for (x, z) in forget { - g.sent_chunks.remove(&(x, z)); - g.send(&C::ForgetLevelChunk(play::ForgetLevelChunk { pos: [x, z] })); - } - g.chunk_queue.retain(|&(x, z)| tracked(cx, cz, r, x, z)); - for dz in -r - 2..=r + 2 { - for dx in -r - 2..=r + 2 { - let key = (cx + dx, cz + dz); - if tracked(cx, cz, r, key.0, key.1) - && !g.sent_chunks.contains(&key) - && !g.chunk_queue.contains(&key) - { - g.chunk_queue.push(key); - } + if g.center == Some((cx, cz)) { + return; + } + g.center = Some((cx, cz)); + if g.center_sent.take() != Some((cx, cz)) { + g.send(&C::SetChunkCacheCenter(play::SetChunkCacheCenter { + x: cx, + z: cz, + })); + } + // Chunks that left the view. + let forget: Vec<_> = g + .sent_chunks + .iter() + .copied() + .filter(|&(x, z)| !tracked(cx, cz, r, x, z)) + .collect(); + for (x, z) in forget { + g.sent_chunks.remove(&(x, z)); + g.send(&C::ForgetLevelChunk(play::ForgetLevelChunk { pos: [x, z] })); + } + g.chunk_queue.retain(|&(x, z)| tracked(cx, cz, r, x, z)); + for dz in -r - 2..=r + 2 { + for dx in -r - 2..=r + 2 { + let key = (cx + dx, cz + dz); + if tracked(cx, cz, r, key.0, key.1) + && !g.sent_chunks.contains(&key) + && !g.chunk_queue.contains(&key) + { + g.chunk_queue.push(key); } } - g.chunk_queue - .sort_by_key(|&(x, z)| i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2)); } - // Send ready chunks, closest first, in batches. - if g.chunk_queue.is_empty() || g.unacknowledged >= 10 || self.ticks <= g.placed_at + 1 { - return; + g.chunk_queue + .sort_by_key(|&(x, z)| i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2)); + } + + /// PlayerChunkSender.sendNextChunks' choice: ready chunks, closest first, + /// as many as the batch quota the client's acknowledgements pace allows, + /// with their places in the queue. + fn next_batch<'w>(&mut self, i: usize, world: &'w World) -> Option<Vec<(usize, &'w Chunk)>> { + let g = &mut self.guests[i]; + if self.ticks <= g.placed_at + 1 || g.unacknowledged >= g.max_unacknowledged { + return None; } - g.chunk_credit = (g.chunk_credit + g.chunks_per_tick).min(g.chunks_per_tick.max(1.0) * 2.0); - let budget = g.chunk_credit.floor() as usize; - if budget == 0 { - return; - } - let mut packets = Vec::new(); - let mut taken = Vec::new(); - for (idx, key) in g.chunk_queue.iter().enumerate() { - if packets.len() >= budget { - break; - } - let Some(chunk) = ctx.world.chunks.get(key) else { - continue; - }; - if !chunk.lit { - continue; - } - packets.push(C::LevelChunkWithLight( - self.encoder.encode_in(chunk, ctx.world), - )); - taken.push(idx); - } - if packets.is_empty() { - return; - } - g.chunk_credit -= packets.len() as f32; - g.send(&C::ChunkBatchStart(horizon_protocol::packets::Empty)); - for p in &packets { - g.send(p); - } - g.send(&C::ChunkBatchFinished(play::ChunkBatchFinished { - size: packets.len() as i32, - })); - g.unacknowledged += 1; - for idx in taken.into_iter().rev() { - let key = g.chunk_queue.remove(idx); - g.sent_chunks.insert(key); + g.batch_quota = (g.batch_quota + g.chunks_per_tick).min(g.chunks_per_tick.max(1.0)); + if g.batch_quota < 1.0 || g.chunk_queue.is_empty() { + return None; } + let budget = g.batch_quota.floor() as usize; + let batch: Vec<_> = g + .chunk_queue + .iter() + .enumerate() + .filter_map(|(idx, key)| Some((idx, &**world.chunks.get(key).filter(|c| c.lit)?))) + .take(budget) + .collect(); + (!batch.is_empty()).then_some(batch) } /// Resend a chunk to everyone who has it (a block entity changed, relight). @@ -1114,8 +1316,7 @@ impl Hub { let Some(chunk) = world.chunks.get(&(x, z)) else { return; }; - let packet = C::LevelChunkWithLight(self.encoder.encode_in(chunk, world)); - let body = packet.encode(); + let body = self.encoder.body_in(chunk, world); for g in self .guests .iter() @@ -1125,6 +1326,42 @@ impl Hub { } } + /// `ChunkHolder.broadcastChanges`' light part: the light sections that + /// changed in a column go to the players for whom it is on the border of + /// what they track (`ChunkMap.isChunkOnTrackedBorder`); the others light + /// the blocks they are sent themselves. + fn broadcast_light_changes(&mut self, world: &mut World) { + let Some(log) = world.light_log.as_mut() else { + return; + }; + if log.is_empty() { + return; + } + let mut changes: Vec<((i32, i32), u32)> = log.drain().collect(); + changes.sort_unstable(); + for ((x, z), sections) in changes { + let tracked = |g: &Guest, x: i32, z: i32| g.sent_chunks.contains(&(x, z)); + let viewers: Vec<usize> = (0..self.guests.len()) + .filter(|&i| { + let g = &self.guests[i]; + self.here(g) + && tracked(g, x, z) + && (-1..=1).any(|dz| (-1..=1).any(|dx| !tracked(g, x + dx, z + dz))) + }) + .collect(); + if viewers.is_empty() { + continue; + } + let Some(chunk) = world.chunks.get(&(x, z)) else { + continue; + }; + let body = self.encoder.light_update(chunk, sections); + for &i in &viewers { + self.guests[i].sender.send(body.clone()); + } + } + } + fn broadcast_block_changes(&mut self, world: &mut World) { let Some(log) = world.block_log.as_mut() else { return; @@ -1147,7 +1384,7 @@ impl Hub { for ((sx, sy, sz), positions) in sections { let viewers: Vec<usize> = (0..self.guests.len()) .filter(|&i| { - self.guests[i].placed && self.guests[i].sent_chunks.contains(&(sx, sz)) + self.here(&self.guests[i]) && self.guests[i].sent_chunks.contains(&(sx, sz)) }) .collect(); if viewers.is_empty() { @@ -1157,7 +1394,7 @@ impl Hub { let (x, y, z) = positions[0]; C::BlockUpdate(play::BlockUpdate { pos: [x, y, z], - state: i32::from(world.block(x, y, z).0 as u16), + state: world.block(x, y, z).0 as i32, }) } else { C::SectionBlocksUpdate(play::SectionBlocksUpdate { @@ -1186,7 +1423,7 @@ impl Hub { let Some(tag) = super::chunks::entity_tag(entity) else { continue; }; - let state = world.block(x, y, z).0 as u16; + let state = world.block(x, y, z).0; let Some(kind) = self.encoder.entity_type(state) else { continue; }; @@ -1213,9 +1450,11 @@ impl Hub { rot: (f32, f32, f32), on_ground: bool, flags: (bool, bool, bool, bool), + living_flags: i8, skin: u8, health: f32, equipment: Vec<(u8, Option<horizon_protocol::item::Stack>)>, + attributes: Vec<play::AttributeSnapshot>, ) -> EntityState { let (sneaking, sprinting, gliding, sleeping) = flags; let mut shared = 0i8; @@ -1244,6 +1483,9 @@ impl Hub { if pose != 0 { meta.push((6, serializer::POSE, Value::Id(pose))); } + // Always there: the trackers send the entries that changed, and an + // item no longer in use is a change back to 0. + meta.push((8, serializer::BYTE, Value::Byte(living_flags))); if (health - 1.0).abs() > f32::EPSILON && health != 20.0 { meta.push((9, serializer::FLOAT, Value::Float(health))); } @@ -1263,6 +1505,8 @@ impl Hub { data: 0, meta, equipment, + attributes, + precise: false, } } @@ -1306,13 +1550,15 @@ impl Hub { (h.yaw, h.pitch, h.head_yaw), h.on_ground, (h.sneaking, h.sprinting, false, false), + h.living_flags, 0x7F, h.health, equipment, + h.attributes.clone(), )); } let placed: Vec<usize> = (0..self.guests.len()) - .filter(|&i| self.guests[i].placed) + .filter(|&i| self.here(&self.guests[i])) .collect(); let guest_states: Vec<_> = placed .into_iter() @@ -1335,71 +1581,93 @@ impl Hub { (g.yaw, g.pitch, g.head_yaw), g.on_ground, (g.sneaking, g.sprinting, g.fall_flying, g.sleeping), + g.living_flags, g.info.model_customisation, g.health, equipment, + g.motion + .as_ref() + .map(|m| super::attributes::syncable(&m.attributes)) + .unwrap_or_default(), ) }) .collect(); all.extend(guest_states); - all.extend(ctx.entities.iter().cloned()); - let by_id: std::collections::HashMap<i32, usize> = - all.iter().enumerate().map(|(i, e)| (e.id, i)).collect(); - for gi in 0..self.guests.len() { - if !self.guests[gi].placed { - continue; - } - let view = self.guests[gi].view_distance; - let me = self.guests[gi].entity_id; - let origin = self.guests[gi].position; - // TrackedEntity.updatePlayer: within the type's range (capped by the - // view distance) horizontally, in a chunk the client has. - let visible = |g: &Guest, e: &EntityState| { - let range = super::entity::visible_range(&e.kind, view); - let (dx, dz) = (origin[0] - e.position[0], origin[2] - e.position[2]); - let chunk = ( - (e.position[0].floor() as i32) >> 4, - (e.position[2].floor() as i32) >> 4, - ); - dx * dx + dz * dz <= range * range && g.sent_chunks.contains(&chunk) - }; - // Forget what disappeared or left range. - let mut removed = Vec::new(); - for id in self.guests[gi].tracker.ids() { - match by_id.get(&id) { - Some(&ei) if visible(&self.guests[gi], &all[ei]) => {} - _ => removed.push(id), - } - } - if !removed.is_empty() { - for id in &removed { - self.guests[gi].tracker.forget(*id); - } - self.guests[gi].send(&C::RemoveEntities(play::RemoveEntities { ids: removed })); - } - for e in &all { - if e.id == me { - continue; - } - if self.guests[gi].tracker.is_tracking(e.id) { - let packets = self.guests[gi].tracker.update(e); - self.guests[gi].send_all(&packets); - } else if visible(&self.guests[gi], e) { - let Some(kind) = self.entity_type(&e.kind) else { - continue; - }; - // Players need their list entry first; guests got it at join. - let packets = self.guests[gi].tracker.spawn(e, kind); - self.guests[gi].send_all(&packets); - } - } + let entities: Vec<&EntityState> = all.iter().chain(ctx.entities).collect(); + // ChunkMap.tick: the level's entities, once for all its players. + let mut level = self + .entities + .remove(&self.config.dimension) + .unwrap_or_default(); + let mut outs = std::mem::take(&mut self.entity_out); + let dimension = &self.config.dimension; + let here = |g: &Guest| g.placed && g.dimension == *dimension; + outs.resize_with(self.guests.iter().filter(|g| here(g)).count(), Vec::new); + let mut viewers: Vec<Viewer> = self + .guests + .iter_mut() + .filter(|g| here(g)) + .zip(outs.iter_mut()) + .map(|(g, out)| Viewer { + entity_id: g.entity_id, + moved: std::mem::take(&mut g.moved), + position: g.position, + view_distance: g.view_distance, + chunks: &g.sent_chunks, + tracker: &mut g.tracker, + out, + }) + .collect(); + let (types, sync) = (&mut self.entity_types, &self.sync); + level.tick(&entities, &self.riding, &mut viewers, |kind| { + entity_type(types, sync, kind) + }); + drop(viewers); + for (g, out) in self.guests.iter().filter(|g| here(g)).zip(outs.iter_mut()) { + g.sender.send_many(out.drain(..)); } + self.entity_out = outs; + self.entities.insert(self.config.dimension.clone(), level); + } + + /// ServerPlayer.stopSleepInBed: the wake-up animation goes to the player + /// and those around, the pose is standing again and the sleep counter + /// starts the wake-up fade (100). + pub fn wake(&mut self, i: usize) { + let g = &mut self.guests[i]; + if g.bed.take().is_none() && !g.sleeping { + return; + } + g.sleeping = false; + g.sleep_timer = 100; + let (id, at) = (g.entity_id, g.position); + // ClientboundAnimatePacket.WAKE_UP. + self.broadcast_near( + at, + 128.0, + &C::Animate(play::Animate { id, action: 0 }), + None, + ); } // ---- per-player state ------------------------------------------------ /// ServerPlayer.doTick: health, food, experience and the inventory menu. fn sync_player_state(&mut self, i: usize) { + // LivingEntity.startSleeping / stopSleeping: the pose and the bed go + // to the player too (its client lies down in the bed from them). + let g = &mut self.guests[i]; + if g.bed != g.sent_bed { + g.sent_bed = g.bed; + let pose = if g.bed.is_some() { 2 } else { 0 }; + g.send(&C::SetEntityData(play::SetEntityData { + id: g.entity_id, + values: vec![ + (6, serializer::POSE, Value::Id(pose)), + (14, serializer::OPTIONAL_BLOCK_POS, Value::OptPos(g.bed)), + ], + })); + } // ServerEntity.sendDirtyEntityData on the player's first tick: the // health its creation set is still dirty, and goes to the player too. if self.guests[i].sent_health.is_none() { @@ -1408,27 +1676,52 @@ impl Hub { id: g.entity_id, values: vec![(9, serializer::FLOAT, Value::Float(g.health))], })); - // ServerPlayer.tick's CriteriaTriggers.TICK, then flushDirty. - let now = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .map_or(0, |d| d.as_millis() as i64); - let first = self.sync.advancements.first_tick(&self.sync, now); - if let Some(add) = first.recipe_book_add { - g.send(&raw(ids::RECIPE_BOOK_ADD, add)); + // ServerPlayer.tick's CriteriaTriggers.TICK, then flushDirty. A + // player who has been here has had the tick's criteria already. + if !g.advancements_loaded { + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |d| d.as_millis() as i64); + let sync = self.sync.clone(); + let g = &mut self.guests[i]; + let first = sync.advancements.first_tick( + &sync, + now, + &mut g.advancements, + &mut g.recipe_book, + ); + if let Some(add) = first.recipe_book_add { + g.send(&raw(ids::RECIPE_BOOK_ADD, add)); + } + g.send(&raw(ids::UPDATE_ADVANCEMENTS, first.update_advancements)); + g.advancements_visible = sync.advancements.visibility(&g.advancements); + g.advancements_changed.clear(); + } else { + self.flush_advancements(i, true); } - g.send(&raw(ids::UPDATE_ADVANCEMENTS, first.update_advancements)); } - let health = { - let g = &self.guests[i]; - (g.health, g.food, g.saturation) - }; - // A dead player's health isn't sent (the death screen shows from the combat kill). - if self.guests[i].sent_health != Some(health) && !self.guests[i].dead { - let g = &mut self.guests[i]; + // ServerEntity.sendDirtyEntityData: the player's changed attribute + // instances go to the player too (its trackers get them from + // track_entities). + let g = &mut self.guests[i]; + if let Some(m) = g.motion.as_mut() { + let values = super::attributes::take_dirty(&mut m.attributes); + if !values.is_empty() { + g.send(&C::UpdateAttributes(play::UpdateAttributes { + id: g.entity_id, + values, + })); + } + } + // Sent when the health or the food level changes, or the saturation + // reaches or leaves zero; a dead player's health of 0 too. + let g = &mut self.guests[i]; + let health = (g.health, g.food, g.saturation == 0.0); + if g.sent_health != Some(health) { g.send(&C::SetHealth(play::SetHealth { - health: health.0, - food: health.1, - saturation: health.2, + health: g.health, + food: g.food, + saturation: g.saturation, })); g.sent_health = Some(health); } @@ -1467,6 +1760,65 @@ impl Hub { } } + /// `PlayerAdvancements.flushDirty` and the recipes the player unlocked + /// since (`ServerRecipeBook.addRecipes`), once the player's first tick + /// has sent their advancements. + pub fn flush_progress(&mut self, conn: u32) { + if let Some(i) = self.guests.iter().position(|g| g.conn == conn) + && self.guests[i].sent_health.is_some() + { + self.flush_advancements(i, true); + } + } + fn flush_advancements(&mut self, i: usize, show: bool) { + let sync = self.sync.clone(); + let g = &mut self.guests[i]; + let unlocked = std::mem::take(&mut g.recipes_unlocked); + if let Some(add) = sync.advancements.recipes_added(&unlocked, &sync) { + g.send(&raw(ids::RECIPE_BOOK_ADD, add)); + } + let changed = std::mem::take(&mut g.advancements_changed); + if let Some(update) = sync.advancements.update( + &sync, + &g.advancements, + &mut g.advancements_visible, + &changed, + show, + ) { + g.send(&raw(ids::UPDATE_ADVANCEMENTS, update)); + } + } + + /// ClientboundAwardStatsPacket (`ServerStatsCounter.sendStats`): each + /// statistic as its type and value by network id (`Stat.STREAM_CODEC`), + /// then its count. Statistics the registries don't know are left out. + pub fn send_stats(&self, conn: u32, stats: &[(String, String, i32)]) { + let known: Vec<(i32, i32, i32)> = stats + .iter() + .filter_map(|(ty, key, count)| { + let values = match ty.as_str() { + "minecraft:mined" => "minecraft:block", + "minecraft:killed" | "minecraft:killed_by" => "minecraft:entity_type", + "minecraft:custom" => "minecraft:custom_stat", + _ => "minecraft:item", + }; + Some(( + self.sync.id("minecraft:stat_type", ty)?, + self.sync.id(values, key)?, + *count, + )) + }) + .collect(); + let mut w = horizon_protocol::buf::Writer::new(); + w.varint(known.len() as i32); + for (ty, value, count) in known { + w.varint(ty); + w.varint(value); + w.varint(count); + } + self.send_to(conn, &raw(ids::AWARD_STATS, w.data)); + } + // ---- incoming ------------------------------------------------------ /// The squared movement past 1.0E-5F (handlePlayerKnownMovement). @@ -1477,6 +1829,11 @@ impl Hub { } fn process_inbox(&mut self, i: usize, ctx: &mut TickCtx, events: &mut Vec<HostEvent>) { + // ServerGamePacketListenerImpl.tickPlayer ran between the last + // tick's packets and these: resetPosition, and the moves it counted. + let g = &mut self.guests[i]; + g.first_good = g.position; + g.known_moves = g.received_moves; let mut budget = 400; while budget > 0 { let Some(packet) = self.guests[i].inbox.pop_front() else { @@ -1505,23 +1862,42 @@ impl Hub { self.guests[i].last_action_tick = self.ticks; } match packet { + // handleAcceptTeleportPacket. S::AcceptTeleportation(p) => { let g = &mut self.guests[i]; - if g.awaiting_teleport.is_some_and(|(id, _)| id == p.id) { + if let Some((id, at)) = g.awaiting_teleport + && id == p.id + { + if p.x.is_nan() + || p.y.is_nan() + || p.z.is_nan() + || !p.yaw.is_finite() + || !p.pitch.is_finite() + { + self.kick(conn, invalid_movement()); + continue; + } + g.position = at; g.awaiting_teleport = None; + let rot = (p.yaw, p.pitch); + self.position_change(i, [p.x, p.y, p.z], rot, 0, ctx, events); } } S::MovePlayerPos(p) => { - self.move_player(i, Some([p.x, p.y, p.z]), None, p.flags); + self.move_player(i, Some([p.x, p.y, p.z]), None, p.flags, ctx, events); } S::MovePlayerPosRot(p) => { - self.move_player(i, Some([p.x, p.y, p.z]), Some((p.yaw, p.pitch)), p.flags); + let (pos, rot) = ([p.x, p.y, p.z], (p.yaw, p.pitch)); + self.move_player(i, Some(pos), Some(rot), p.flags, ctx, events); } S::MovePlayerRot(p) => { - self.move_player(i, None, Some((p.yaw, p.pitch)), p.flags); + self.move_player(i, None, Some((p.yaw, p.pitch)), p.flags, ctx, events); + } + S::MovePlayerStatus(p) => { + self.move_player(i, None, None, p.flags, ctx, events); } - S::MovePlayerStatus(p) => self.move_player(i, None, None, p.flags), S::PlayerLoaded(_) => self.guests[i].loaded = true, + // PlayerChunkSender.onChunkBatchReceivedByClient. S::ChunkBatchReceived(p) => { let g = &mut self.guests[i]; g.unacknowledged = g.unacknowledged.saturating_sub(1); @@ -1530,7 +1906,12 @@ impl Hub { } else { p.chunks_per_tick.clamp(0.01, 64.0) }; + if g.unacknowledged == 0 { + g.batch_quota = 1.0; + } + g.max_unacknowledged = 10; } + S::ClientTickEnd(_) => self.guests[i].position_this_tick = false, S::ClientInformation(info) => { let g = &mut self.guests[i]; g.view_distance = @@ -1546,63 +1927,219 @@ impl Hub { 1 => self.guests[i].sprinting = true, 2 => self.guests[i].sprinting = false, 6 => self.guests[i].fall_flying = true, - 0 => self.guests[i].sleeping = false, + // STOP_SLEEPING. + 0 => self.wake(i), _ => {} }, - S::ClientTickEnd(_) => {} - S::Chat(c) => { - let text: String = c.message.chars().filter(|c| !c.is_control()).collect(); - if !text.is_empty() { - let name = self.guests[i].name.clone(); - self.player_chat(i, &text); - events.push(HostEvent::Chat(format!("<{name}> {text}"))); - } - } + S::Chat(c) => self.handle_chat(i, c, events), + S::ChatAck(p) => self.chat_ack(i, p.offset, events), + S::ChatSessionUpdate(p) => self.chat_session_update(i, p, events), other => events.push(HostEvent::Packet(conn, Box::new(other))), } } - let _ = ctx; } /// ServerGamePacketListenerImpl.handleMovePlayer. - fn move_player(&mut self, i: usize, pos: Option<[f64; 3]>, rot: Option<(f32, f32)>, flags: u8) { - let g = &mut self.guests[i]; - if g.awaiting_teleport.is_some() || !g.loaded && g.ticks == 0 && pos.is_none() { + fn move_player( + &mut self, + i: usize, + pos: Option<[f64; 3]>, + rot: Option<(f32, f32)>, + flags: u8, + ctx: &TickCtx, + events: &mut Vec<HostEvent>, + ) { + let conn = self.guests[i].conn; + // containsInvalidValues, on the packet's values (0 where it has none). + let (yaw, pitch) = rot.unwrap_or_default(); + if pos.is_some_and(|p| p.iter().any(|v| v.is_nan())) + || !yaw.is_finite() + || !pitch.is_finite() + { + self.kick(conn, invalid_movement()); return; } - if let Some(p) = pos { - if p.iter().any(|v| !v.is_finite() || v.abs() > 3.0e7) { + let g = &mut self.guests[i]; + if pos.is_some() { + if g.position_this_tick { + self.kick(conn, invalid_movement()); return; } - // A jump of more than a hundred blocks is a rejected move. - let d2: f64 = (0..3).map(|k| (p[k] - g.position[k]).powi(2)).sum(); - if d2 > 100.0 * 100.0 { - let at = g.position; - self.teleport(i, Some(at)); - return; - } - g.previous_position = g.position; - g.position = p; + g.position_this_tick = true; } - let moved = pos.map_or([0.0; 3], |_| { - [0, 1, 2].map(|k| g.position[k] - g.previous_position[k]) - }); + if !g.loaded && g.ticks == 0 && pos.is_none() { + return; + } + // updateAwaitingTeleport: moves wait for the client to accept the + // teleport, which is sent again after 20 ticks without an answer. + if let Some((_, at)) = g.awaiting_teleport { + if self.ticks - g.teleport_tick > 20 { + self.teleport(i, Some(at)); + } + return; + } + let to = pos.unwrap_or(g.position); + let rot = rot.unwrap_or((g.yaw, g.pitch)); + self.position_change(i, to, rot, flags, ctx, events); + } + + /// ServerGamePacketListenerImpl.handlePlayerPositionChange. + fn position_change( + &mut self, + i: usize, + to: [f64; 3], + (yaw, pitch): (f32, f32), + flags: u8, + ctx: &TickCtx, + events: &mut Vec<HostEvent>, + ) { + let (frozen, (check, elytra_check)) = (self.ticking.1, self.movement_checks); + let g = &mut self.guests[i]; + let owner = self.singleplayer_owner == Some(g.uuid); + let to = [ + to[0].clamp(-3.0E7, 3.0E7), + to[1].clamp(-2.0E7, 2.0E7), + to[2].clamp(-3.0E7, 3.0E7), + ]; + let (yaw, pitch) = (wrap_degrees(yaw), wrap_degrees(pitch)); + let d = [0, 1, 2].map(|k| to[k] - g.first_good[k]); + let moved = d[0] * d[0] + d[1] * d[1] + d[2] * d[2]; + if g.sleeping { + if moved > 1.0 { + (g.yaw, g.pitch) = (yaw, pitch); + self.teleport(i, None); + } + return; + } + // TickRateManager.runsNormally: not frozen (a frozen game's steps + // are not counted either; Horizon's hub does not see them). + if !frozen { + g.received_moves += 1; + let mut packets = g.received_moves - g.known_moves; + if packets > 5 { + packets = 1; + } + // shouldCheckPlayerMovement (never the single-player owner). + // The server holds no velocity for a guest (getDeltaMovement), + // so nothing is expected. + if !owner && check && (!g.fall_flying || elytra_check) { + let limit = if g.fall_flying { 300.0 } else { 100.0 }; + if moved > limit * f64::from(packets) { + events.push(HostEvent::Warn(format!( + "{} moved too quickly! {},{},{}", + g.name, + crate::java_text::double_to_string(d[0]), + crate::java_text::double_to_string(d[1]), + crate::java_text::double_to_string(d[2]) + ))); + self.teleport(i, None); + return; + } + } + } + let start = g.position; + let (wrongly, refused) = self.collide(i, to, ctx); + if wrongly { + let line = format!("{} moved wrongly!", self.guests[i].name); + events.push(HostEvent::Warn(line)); + } + if refused { + let g = &mut self.guests[i]; + (g.yaw, g.pitch) = (yaw, pitch); + self.teleport(i, Some(start)); + return; + } + let g = &mut self.guests[i]; + g.previous_position = g.position; + g.position = to; + // ServerChunkCache.move: its pairs are checked again. + g.moved = true; + let moved = [0, 1, 2].map(|k| g.position[k] - g.previous_position[k]); if g.moves.is_empty() { g.moves_from_ground = g.on_ground; } g.moves.push((moved, flags & 1 != 0)); - if let Some((yaw, pitch)) = rot - && yaw.is_finite() - && pitch.is_finite() - { - g.yaw = yaw % 360.0; - g.pitch = pitch.clamp(-90.0, 90.0); - g.head_yaw = g.yaw; - } + g.yaw = yaw; + g.pitch = pitch.clamp(-90.0, 90.0); + g.head_yaw = g.yaw; g.on_ground = flags & 1 != 0; g.ticks = g.ticks.max(1); } + /// The collision part of handlePlayerPositionChange: whether the move + /// counts as moved wrongly (logged), and whether it is refused (the + /// player is sent back where they were). + fn collide(&self, i: usize, to: [f64; 3], ctx: &TickCtx) -> (bool, bool) { + let g = &self.guests[i]; + // noPhysics (spectators) and sleepers move freely. + let Some(binding) = ctx.collision else { + return (false, false); + }; + if g.spectator() || g.sleeping { + return (false, false); + } + let height = if g.fall_flying { + 0.6 + } else if g.sneaking { + 1.5 + } else { + 1.8 + }; + let at = |p: [f64; 3]| { + Aabb::new( + Vec3::new(p[0] - 0.3, p[1], p[2] - 0.3), + Vec3::new(p[0] + 0.3, p[1] + height, p[2] + 0.3), + ) + }; + let (old, new) = (at(g.position), at(to)); + let mut scene = SceneCollisions { + world: ctx.world, + binding, + subject: None, + }; + // player.move, then how far from the target it ended (vanilla drops + // the vertical part: `yDist > -0.5 || yDist < 0.5` always holds). + let movement = Vec3::new( + to[0] - g.position[0], + to[1] - g.position[1], + to[2] - g.position[2], + ); + let fail = !g.creative() + && scene + .resolve(old, movement, 0.6, g.on_ground) + .is_ok_and(|done| { + let (dx, dz) = (movement.x - done.x, movement.z - done.z); + dx * dx + dz * dz > 0.0625 + }); + if fail && scene.unobstructed(old).unwrap_or(true) { + return (true, true); + } + // isEntityCollidingWithAnythingNew: a block the old box did not + // already touch. + let shrink = f64::from(1.0E-5_f32); + let shrink = Vec3::new(shrink, shrink, shrink); + let Ok(old_shape) = VoxelShape::from_box(old.deflate(shrink)) else { + return (fail, false); + }; + let context = CollisionContext::position(g.position[1]); + let Ok(scan) = + BlockCollisionScan::new(&mut scene, context, new.deflate(shrink), false, 1 << 20) + else { + return (fail, false); + }; + for hit in scan { + let Ok(hit) = hit else { break }; + if !hit + .shape + .join_is_not_empty(&old_shape, ShapeOp::AND) + .unwrap_or(true) + { + return (fail, true); + } + } + (fail, false) + } + /// Acknowledge a block interaction (ClientboundBlockChangedAckPacket). /// ServerGamePacketListenerImpl.ackBlockChangesUpTo: sent with the next tick's block /// changes (`tick`). @@ -1675,6 +2212,18 @@ impl Hub { .map(str::to_owned) } + /// ServerLevel.removePlayerImmediately, then addRespawnedPlayer: the other players + /// lose the dead player's entity (ServerEntity.removePairing) and are sent the + /// respawned one as a new entity at the next tick. Their clients would otherwise keep + /// the dead body, lying down, wherever the player goes. + pub fn respawned(&mut self, id: i32) { + for g in &mut self.guests { + if g.entity_id != id && g.tracker.forget(id) { + g.send(&C::RemoveEntities(play::RemoveEntities { ids: vec![id] })); + } + } + } + /// PlayerList.respawn: back to `at` with a fresh start. The vanilla client waits on its /// level loading screen from the Respawn packet until the level info's "level chunks load /// start" event and the chunk under the player, so the packets follow vanilla's order. @@ -1682,12 +2231,15 @@ impl Hub { let mode = self.guests[i].game_mode; let mut info = self.spawn_info(mode); // Player.getLastDeathLocation, set where the player died. + // The level they died in: theirs until now. info.last_death = self.guests[i].last_death.map(|pos| play::GlobalPos { - dimension: self.config.dimension.clone(), + dimension: self.guests[i].dimension.clone(), pos, }); self.close_container_for(i); let g = &mut self.guests[i]; + // PlayerList.respawn: into the level in place (the respawn level). + g.dimension = self.config.dimension.clone(); // ServerLevel.removePlayerImmediately: every entity paired with the dead player is // removed (ServerEntity.removePairing, one packet each). Its chunks are dropped // without ForgetLevelChunk (PlayerChunkSender.dropChunk skips dead players): the @@ -1698,6 +2250,9 @@ impl Hub { g.tracker.clear(); g.sent_chunks.clear(); g.chunk_queue.clear(); + let id = g.entity_id; + self.respawned(id); + let g = &mut self.guests[i]; // ServerPlayer.restoreFrom: the game mode again (onUpdateAbilities). g.send(&C::PlayerAbilities(play::PlayerAbilities { flags: g.ability_flags(), @@ -1752,9 +2307,129 @@ impl Hub { // ServerPlayer.initInventoryMenu. self.send_inventory(i); } + + /// The level the players are in: the server holds one at a time, and the + /// hub's packets name it. + pub fn set_dimension(&mut self, dimension: &str) { + if self.config.dimension == dimension { + return; + } + // What this level's players were told stays with it. + let leaving = LevelSync { + last_clock: self.last_clock, + last_rate: self.last_rate, + weather: self.weather, + }; + let entering = self.level_sync.remove(dimension).unwrap_or_default(); + self.level_sync + .insert(std::mem::take(&mut self.config.dimension), leaving); + (self.last_clock, self.last_rate, self.weather) = + (entering.last_clock, entering.last_rate, entering.weather); + self.config.dimension = dimension.to_owned(); + self.dimension_type = self + .sync + .id("minecraft:dimension_type", dimension) + .unwrap_or(0); + let d = horizon_protocol::registry::DimensionType::vanilla(dimension); + self.encoder + .set_dimension(d.min_y, d.height, d.has_skylight); + } + + /// ServerPlayer.teleport(TeleportTransition) into the level the hub now + /// names (`set_dimension`): ClientboundRespawnPacket keeping all data, + /// then vanilla's packet order. Nothing tracked in the old level is + /// removed one by one: the client drops its whole level. + pub fn change_level( + &mut self, + i: usize, + at: [f64; 3], + yaw: f32, + pitch: Option<f32>, + portal_sound: bool, + ctx: &TickCtx, + ) { + let mode = self.guests[i].game_mode; + let info = self.spawn_info(mode); + self.close_container_for(i); + let g = &mut self.guests[i]; + g.dimension = self.config.dimension.clone(); + g.tracker.clear(); + g.sent_chunks.clear(); + g.chunk_queue.clear(); + g.center = None; + g.yaw = yaw; + if let Some(pitch) = pitch { + g.pitch = pitch; + } + g.position = at; + g.previous_position = at; + g.send(&C::Respawn(play::Respawn { + spawn: info, + // ClientboundRespawnPacket.KEEP_ALL_DATA. + keep: 3, + })); + g.send(&C::ChangeDifficulty(play::ChangeDifficulty { + difficulty: ctx.difficulty, + locked: ctx.difficulty_locked, + })); + self.send_permission_level(i); + self.teleport(i, None); + let g = &mut self.guests[i]; + g.send(&C::PlayerAbilities(play::PlayerAbilities { + flags: g.ability_flags(), + flying_speed: 0.05, + walking_speed: 0.1, + })); + self.send_level_info(i, ctx); + // PlayerList.sendAllPlayerInfo: the inventory and the held slot. + self.send_inventory(i); + let g = &mut self.guests[i]; + g.send(&C::SetHeldSlot(play::SetHeldSlot { + slot: g.selected as i32, + })); + self.send_active_effects(i); + let g = &mut self.guests[i]; + // ServerPlayer.sendPostEffects. + g.send(&raw(ids::POST_EFFECTS, vec![0])); + // TeleportTransition.PLAY_PORTAL_SOUND. + if portal_sound { + g.send(&C::LevelEvent(play::LevelEvent { + kind: 1032, + pos: [0, 0, 0], + data: 0, + global: false, + })); + } + // lastSentExp, lastSentHealth and lastSentFood. + g.sent_health = None; + g.sent_xp = None; + let (cx, cz) = g.chunk(); + g.send(&C::SetChunkCacheCenter(play::SetChunkCacheCenter { + x: cx, + z: cz, + })); + g.center_sent = Some((cx, cz)); + } } /// A packet Horizon sends as raw bytes (no fields worth a type). +/// multiplayer.disconnect.invalid_player_movement. +/// Mth.wrapDegrees. +fn wrap_degrees(degrees: f32) -> f32 { + let mut wrapped = degrees % 360.0; + if wrapped >= 180.0 { + wrapped -= 360.0; + } + if wrapped < -180.0 { + wrapped += 360.0; + } + wrapped +} + +fn invalid_movement() -> Tag { + translatable("multiplayer.disconnect.invalid_player_movement", &[]) +} + fn raw(id: i32, payload: Vec<u8>) -> C { C::Unknown(id, payload) } diff --git a/crates/horizon-client/src/lan/hub/chat.rs b/crates/horizon-client/src/lan/hub/chat.rs new file mode 100644 index 000000000..7e75c84c5 --- /dev/null +++ b/crates/horizon-client/src/lan/hub/chat.rs @@ -0,0 +1,500 @@ +//! Chat on the server side, as ServerGamePacketListenerImpl and PlayerList +//! handle it: chat sessions validated against the services' keys, signed +//! messages checked link by link, the acknowledged window, and messages +//! relayed with each connection's numbering and signature cache. +use super::{HostEvent, Hub, display_name}; +use horizon_protocol::{ + packets::play::{self, Clientbound as C}, + secure_chat::{self, Body, Chain, ChatSession, DecodeError, ProfileKey, Signature, Unpacked}, + text::{literal, translatable}, +}; +use horizon_world::nbt::{JavaString, Tag}; + +/// Now, in epoch milliseconds (Instant.now()). +pub fn now_millis() -> i64 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |d| d.as_millis() as i64) +} + +/// StringUtil.isAllowedChatCharacter. +fn allowed(c: char) -> bool { + c != '\u{a7}' && c >= ' ' && c != '\u{7f}' +} + +/// A translatable message in red. +fn red(key: &str) -> Tag { + let mut t = translatable(key, &[]); + if let Tag::Compound(map) = &mut t { + map.insert( + JavaString::from("color"), + Tag::String(JavaString::from("red")), + ); + } + t +} + +/// PlayerChatMessage as the server relays it. +#[derive(Clone, Debug, PartialEq)] +pub struct ChatMessage { + pub sender: u128, + /// The link's index in the sender's chain (0 when unsigned). + pub index: i32, + pub signature: Option<Signature>, + pub content: String, + /// Epoch milliseconds. + pub timestamp: i64, + pub salt: i64, + pub last_seen: Vec<Signature>, + /// What clients show instead of the signed text (withUnsignedContent). + pub unsigned_content: Option<Tag>, +} + +impl ChatMessage { + /// The message a chain let through: a signed one keeps its body; + /// PlayerChatMessage.unsigned takes the time now, no salt, nothing seen. + pub fn new(sender: u128, unpacked: Unpacked, body: &Body, now: i64) -> Self { + let (index, signature, timestamp, salt, last_seen) = match unpacked { + Unpacked::Unsigned => (0, None, now, 0, Vec::new()), + Unpacked::Signed { index, signature } => ( + index, + Some(signature), + body.timestamp, + body.salt, + body.last_seen.to_vec(), + ), + }; + Self { + sender, + index, + signature, + content: body.content.to_owned(), + timestamp, + salt, + last_seen, + unsigned_content: None, + } + } + /// hasExpiredServer. + pub fn expired(&self, now: i64) -> bool { + now > self.timestamp + secure_chat::EXPIRES_AFTER_SERVER + } + /// PlayerList.verifyChatTrusted: signed and not expired. + pub fn trusted(&self, now: i64) -> bool { + self.signature.is_some() && !self.expired(now) + } +} + +impl Hub { + /// MinecraftServer.enforceSecureProfile. + pub fn enforces_secure_chat(&self) -> bool { + self.lan.shared.config.enforces_secure_chat() + } + + /// Component.getString in the server's language, for the log. + pub(crate) fn english(&self, tag: &Tag) -> String { + match &self.lan.shared.config.log { + Some(log) => horizon_protocol::text::plain(tag, &log.lang), + None => horizon_protocol::text::plain(tag, &Default::default()), + } + } + + /// tryHandleChat: an illegal character disconnects; with chat hidden a + /// message (not a command) is refused. Whether to go on. + pub fn try_handle_chat(&mut self, i: usize, message: &str, command: bool) -> bool { + let conn = self.guests[i].conn; + if !message.chars().all(allowed) { + self.kick( + conn, + translatable("multiplayer.disconnect.illegal_characters", &[]), + ); + return false; + } + // ChatVisiblity.HIDDEN. + if !command && self.guests[i].info.chat_visibility == 2 { + self.guests[i].send(&C::SystemChat(play::SystemChat { + content: red("chat.disabled.options"), + overlay: false, + })); + return false; + } + true + } + + /// unpackAndApplyLastSeen: the acknowledged messages, or a disconnect. + pub fn apply_last_seen( + &mut self, + i: usize, + update: &play::LastSeenUpdate, + events: &mut Vec<HostEvent>, + ) -> Option<Vec<Signature>> { + let g = &mut self.guests[i]; + match g + .last_seen + .apply_update(update.offset, update.acknowledged, update.checksum) + { + Ok(seen) => Some(seen), + // Vanilla logs the ValidationException's stack trace after the line. + Err(_) => { + events.push(HostEvent::Error(format!( + "Failed to validate message acknowledgements from {}", + g.name + ))); + let conn = g.conn; + self.kick( + conn, + translatable("multiplayer.disconnect.chat_validation_failed", &[]), + ); + None + } + } + } + + /// handleChatAck. + pub(super) fn chat_ack(&mut self, i: usize, offset: i32, events: &mut Vec<HostEvent>) { + let g = &mut self.guests[i]; + if g.last_seen.apply_offset(offset).is_err() { + events.push(HostEvent::Error(format!( + "Failed to validate message acknowledgement offset from {}", + g.name + ))); + let conn = g.conn; + self.kick( + conn, + translatable("multiplayer.disconnect.chat_validation_failed", &[]), + ); + } + } + + /// handleMessageDecodeFailure: logged, and the reason in red for the player. + pub fn decode_failure(&mut self, i: usize, e: DecodeError, events: &mut Vec<HostEvent>) { + let reason = red(e.key()); + events.push(HostEvent::Warn(format!( + "Failed to update secure chat state for {}: '{}'", + self.guests[i].name, + self.english(&reason) + ))); + self.guests[i].send(&C::SystemChat(play::SystemChat { + content: reason, + overlay: false, + })); + } + + /// handleChat: the acknowledgements, then the message through the + /// sender's chain, then to everyone. + pub(super) fn handle_chat(&mut self, i: usize, c: play::Chat, events: &mut Vec<HostEvent>) { + let Some(last_seen) = self.apply_last_seen(i, &c.last_seen, events) else { + return; + }; + if !self.try_handle_chat(i, &c.message, false) { + return; + } + let (enforce, now) = (self.enforces_secure_chat(), now_millis()); + let body = Body { + content: &c.message, + timestamp: c.timestamp, + salt: c.salt, + last_seen: &last_seen, + }; + let signature = c.signature.as_deref().and_then(secure_chat::signature); + let g = &mut self.guests[i]; + let (uuid, name) = (g.uuid, g.name.clone()); + let message = match g + .chain + .unpack(uuid, signature.as_ref(), &body, enforce, now) + { + Ok(unpacked) => ChatMessage::new(uuid, unpacked, &body, now), + Err(e) => return self.decode_failure(i, e, events), + }; + if message.signature.is_some() && message.expired(now) { + events.push(HostEvent::Warn(format!( + "Received expired chat: '{}'. Is the client/server system time unsynchronized?", + message.content + ))); + } + let bound = play::ChatTypeBound { + chat_type: self.chat_type, + name: display_name(uuid, &name), + target: None, + }; + let line = format!("<{name}> {}", message.content); + self.broadcast_chat(&message, &bound, line, events); + } + + /// handleChatSessionUpdate: a new profile key, validated against the + /// services' keys, starts a new chain and is announced to everyone. + pub(super) fn chat_session_update( + &mut self, + i: usize, + p: play::ChatSessionUpdate, + events: &mut Vec<HostEvent>, + ) { + let key = ProfileKey { + expires_at: p.expires_at, + key: p.key, + signature: p.key_signature, + }; + let g = &self.guests[i]; + let (conn, uuid) = (g.conn, g.uuid); + let old = g.chain.session().map(|s| s.profile_key.expires_at); + if g.chain.session().is_some_and(|s| s.profile_key == key) { + return; + } + if old.is_some_and(|old| key.expires_at < old) { + self.kick( + conn, + translatable("multiplayer.disconnect.expired_public_key", &[]), + ); + return; + } + let secure = self.lan.shared.config.secure_chat.clone(); + let keys = secure.as_deref().map_or(&[][..], |s| s.keys()); + if keys.is_empty() { + events.push(HostEvent::Warn(format!( + "Ignoring chat session from {} due to missing Services public key", + g.name + ))); + return; + } + match ChatSession::validate(p.session, key, keys, uuid) { + Ok(session) => { + let data = session.data(); + self.guests[i].chain = Chain::keyed(session); + // resetPlayerChatState: INITIALIZE_CHAT to every player. + self.broadcast(&C::PlayerInfoUpdate(play::PlayerInfoUpdate { + actions: 2, + entries: vec![play::PlayerInfo { + uuid, + chat_session: Some(data), + ..Default::default() + }], + })); + } + Err(reason) => { + events.push(HostEvent::Error( + "Failed to validate profile key".to_owned(), + )); + self.kick(conn, translatable(reason, &[])); + } + } + } + + /// PlayerList.broadcastChatMessage: logged (`[Not Secure]` unless signed + /// and fresh), then sent to every player. + pub fn broadcast_chat( + &mut self, + message: &ChatMessage, + bound: &play::ChatTypeBound, + line: String, + events: &mut Vec<HostEvent>, + ) { + let secure = message.trusted(now_millis()); + events.push(HostEvent::Chat { line, secure }); + for k in 0..self.guests.len() { + if self.guests[k].placed { + self.send_chat(k, message, bound); + } + } + } + + /// ServerPlayer.sendChatMessage (players who show chat) and + /// sendPlayerChatMessage: numbered for the connection, the last seen + /// packed against its cache; a signed message joins the cache and the + /// messages the player must acknowledge. + pub fn send_chat(&mut self, k: usize, message: &ChatMessage, bound: &play::ChatTypeBound) { + let g = &mut self.guests[k]; + if g.info.chat_visibility != 0 { + return; + } + let last_seen = message + .last_seen + .iter() + .map(|s| g.signatures.pack(s)) + .collect(); + g.send(&C::PlayerChat(play::PlayerChat { + global_index: g.chat_index, + sender: message.sender, + index: message.index, + signature: message.signature.map(|s| s.to_vec()), + content: message.content.clone(), + timestamp: message.timestamp, + salt: message.salt, + last_seen, + unsigned_content: message.unsigned_content.clone(), + filter: (0, Vec::new()), + chat_type: bound.clone(), + })); + g.chat_index += 1; + if let Some(signature) = &message.signature { + g.signatures.push(&message.last_seen, Some(signature)); + g.last_seen.add_pending(signature); + if g.last_seen.tracked_count() > 4096 { + let conn = g.conn; + self.kick( + conn, + translatable("multiplayer.disconnect.too_many_pending_chats", &[]), + ); + } + } + } + + /// sendChatMessage of a system message: a disguised chat, to players + /// who show chat. + fn send_disguised(&self, k: usize, message: Tag, chat_type: play::ChatTypeBound) { + let g = &self.guests[k]; + if g.info.chat_visibility == 0 { + g.send(&C::DisguisedChat(play::DisguisedChat { + message, + chat_type, + })); + } + } + + /// collectSignedArguments, or collectUnsignedArguments when the client + /// signed none: each of the command's message arguments (`parsed`, by + /// name) through the sender's chain. + pub fn signed_arguments( + &mut self, + i: usize, + p: &play::ChatCommandSigned, + parsed: &[(String, String)], + last_seen: &[Signature], + events: &mut Vec<HostEvent>, + ) -> Result<Vec<(String, ChatMessage)>, DecodeError> { + let (enforce, now) = (self.enforces_secure_chat(), now_millis()); + let g = &mut self.guests[i]; + let uuid = g.uuid; + let mut out = Vec::new(); + if p.arguments.is_empty() { + for (name, value) in parsed { + // SignedMessageBody.unsigned. + let body = Body { + content: value, + timestamp: now, + salt: 0, + last_seen: &[], + }; + let unpacked = g.chain.unpack(uuid, None, &body, enforce, now)?; + out.push((name.clone(), ChatMessage::new(uuid, unpacked, &body, now))); + } + return Ok(out); + } + let mismatch = |events: &mut Vec<HostEvent>| { + let names = |l: &mut dyn Iterator<Item = &String>| { + l.map(String::as_str).collect::<Vec<_>>().join(", ") + }; + events.push(HostEvent::Error(format!( + "Signed command mismatch between server and client ('{}'): got [{}] from client, but expected [{}]", + p.command, + names(&mut p.arguments.iter().map(|a| &a.0)), + names(&mut parsed.iter().map(|a| &a.0)), + ))); + DecodeError::InvalidCommandSignature + }; + for (name, signature) in &p.arguments { + let Some((_, value)) = parsed.iter().find(|(n, _)| n == name) else { + g.chain.set_broken(); + return Err(mismatch(events)); + }; + let body = Body { + content: value, + timestamp: p.timestamp, + salt: p.salt, + last_seen, + }; + let signature = secure_chat::signature(signature); + let unpacked = g + .chain + .unpack(uuid, signature.as_ref(), &body, enforce, now)?; + out.push((name.clone(), ChatMessage::new(uuid, unpacked, &body, now))); + } + if parsed.iter().any(|(n, _)| !out.iter().any(|(o, _)| o == n)) { + return Err(mismatch(events)); + } + Ok(out) + } + + /// A message command's chat (SayCommand, EmoteCommands): to everyone, + /// bound to `chat_type` and the sender's name. + pub fn broadcast_command_chat( + &mut self, + i: usize, + chat_type: &str, + message: Option<ChatMessage>, + text: &str, + events: &mut Vec<HostEvent>, + ) { + let (uuid, name) = (self.guests[i].uuid, self.guests[i].name.clone()); + let bound = play::ChatTypeBound { + chat_type: self.chat_type_id(chat_type), + name: display_name(uuid, &name), + target: None, + }; + let line = match chat_type { + "say_command" => format!("[{name}] {text}"), + _ => format!("* {name} {text}"), + }; + match message { + Some(message) => self.broadcast_chat(&message, &bound, line, events), + // An unsigned command packet (no signing context): + // PlayerChatMessage.system, a disguised chat. + None => { + events.push(HostEvent::Chat { + line, + secure: false, + }); + for k in 0..self.guests.len() { + if self.guests[k].placed { + self.send_disguised(k, literal(text), bound.clone()); + } + } + } + } + } + + /// MsgCommand.sendMessage to each target: the outgoing line for the + /// sender, the incoming one for the target. Nothing is logged. + pub fn whisper( + &mut self, + i: usize, + targets: &[usize], + message: Option<ChatMessage>, + text: &str, + ) { + let (uuid, name) = (self.guests[i].uuid, self.guests[i].name.clone()); + let (outgoing, incoming) = ( + self.chat_type_id("msg_command_outgoing"), + self.chat_type_id("msg_command_incoming"), + ); + for &t in targets { + let target = display_name(self.guests[t].uuid, &self.guests[t].name); + let out = play::ChatTypeBound { + chat_type: outgoing, + name: display_name(uuid, &name), + target: Some(target), + }; + let inc = play::ChatTypeBound { + chat_type: incoming, + name: display_name(uuid, &name), + target: None, + }; + match &message { + Some(m) => { + self.send_chat(i, m, &out); + self.send_chat(t, m, &inc); + } + None => { + self.send_disguised(i, literal(text), out); + self.send_disguised(t, literal(text), inc); + } + } + } + } + + /// A chat type's registry id. + fn chat_type_id(&self, name: &str) -> i32 { + self.sync + .id("minecraft:chat_type", &format!("minecraft:{name}")) + .unwrap_or(self.chat_type) + } +} diff --git a/crates/horizon-client/src/lan/mod.rs b/crates/horizon-client/src/lan/mod.rs index 6af4a1fef..c2f35bc8e 100644 --- a/crates/horizon-client/src/lan/mod.rs +++ b/crates/horizon-client/src/lan/mod.rs @@ -5,6 +5,7 @@ //! ever exchanges decoded packets with these threads through channels, so a //! world that is not open to the network pays nothing. mod advancements; +pub mod attributes; pub mod chunks; mod command_order; pub mod command_suggestions; @@ -20,8 +21,9 @@ pub mod menu; pub mod playerdata; mod recipes; pub mod tracking; +pub mod waypoints; -pub use conn::{Holding, PlayerSender}; +pub use conn::{Holding, PlayerSender, flush_all}; use horizon_protocol::packets::play; use horizon_protocol::{ GAME_VERSION, PROTOCOL_VERSION, crypto::uuid_string, packets::configuration::ClientInformation, @@ -58,6 +60,8 @@ pub struct Joined { pub conn: u32, pub uuid: u128, pub name: String, + /// The profile's properties (skin textures, in online mode). + pub properties: Vec<horizon_protocol::packets::login::Property>, pub info: ClientInformation, pub brand: String, pub sender: PlayerSender, @@ -81,6 +85,27 @@ pub enum Out { pub type LoginGate = Arc<dyn Fn(SocketAddr, u128, &str) -> Option<horizon_world::nbt::Tag> + Send + Sync>; +/// online-mode=true: the key pair the server made at startup +/// (MinecraftServer.initializeKeyPair) and how players are verified. +pub struct OnlineMode { + pub key: horizon_protocol::rsa::RsaPrivateKey, + /// The public key as X.509 (PublicKey.getEncoded), sent in the hello. + pub public_der: Vec<u8>, + /// prevent-proxy-connections: the session service also checks the address. + pub prevent_proxy: bool, +} + +/// The dedicated server's log for the login phase: (thread, level, line) +/// written as vanilla's Log4j would, and the language that turns a +/// disconnect reason into its text (Component.getString). +#[derive(Clone)] +pub struct NetLog { + pub write: Arc<dyn Fn(&str, &str, &str) + Send + Sync>, + pub lang: Arc<horizon_protocol::text::Language>, + /// log-ips: addresses in the log, or "<ip address withheld>". + pub log_ips: bool, +} + /// How the server side of the network behaves: an integrated server's LAN /// uses the defaults, a dedicated server takes them from server.properties. #[derive(Clone)] @@ -98,8 +123,8 @@ pub struct NetConfig { pub hide_online_players: bool, /// server-icon.png (64x64 PNG bytes). pub favicon: Option<Vec<u8>>, - /// MinecraftServer.enforceSecureProfile. - pub enforces_secure_chat: bool, + /// Secure chat (a dedicated server; none on a LAN world). + pub secure_chat: Option<Arc<SecureChat>>, pub accepts_transfers: bool, /// LanServerPinger: announce the world on the local network. pub announce: bool, @@ -109,8 +134,15 @@ pub struct NetConfig { /// Ops bypass a full server (ServerOpList.canBypassPlayerLimit). pub bypasses_limit: Option<Arc<dyn Fn(u128) -> bool + Send + Sync>>, pub gate: Option<LoginGate>, + /// Online mode: players are authenticated (none on a LAN world). + pub online: Option<Arc<OnlineMode>>, + pub log: Option<NetLog>, } impl NetConfig { + /// MinecraftServer.enforceSecureProfile. + pub fn enforces_secure_chat(&self) -> bool { + self.secure_chat.as_ref().is_some_and(|s| s.enforced()) + } /// IntegratedServer.publishServer. pub fn lan(port: u16, motd: String, max_players: usize) -> Self { Self { @@ -122,16 +154,58 @@ impl NetConfig { enable_status: true, hide_online_players: false, favicon: None, - enforces_secure_chat: false, + secure_chat: None, accepts_transfers: false, announce: true, listen: true, bypasses_limit: None, gate: None, + online: None, + log: None, } } } +/// A dedicated server's secure chat (DedicatedServer.enforceSecureProfile, +/// Services.profileKeySignatureValidator): the services' profile-key keys, +/// fetched in the background from startup and waited for by their first +/// use, as authlib's RetryableFetch does. +pub struct SecureChat { + /// enforce-secure-profile, in online mode. + pub enforce: bool, + keys: std::sync::OnceLock<Vec<horizon_protocol::rsa::RsaPublicKey>>, + fetch: Mutex<Option<JoinHandle<Vec<horizon_protocol::rsa::RsaPublicKey>>>>, +} + +impl SecureChat { + pub fn new( + enforce: bool, + fetch: impl FnOnce() -> Vec<horizon_protocol::rsa::RsaPublicKey> + Send + 'static, + ) -> Self { + let handle = std::thread::Builder::new() + .name("Services keys".into()) + .spawn(fetch) + .ok(); + Self { + enforce, + keys: std::sync::OnceLock::new(), + fetch: Mutex::new(handle), + } + } + /// The keys players' chat keys must be signed by (none when the + /// services couldn't be reached). + pub fn keys(&self) -> &[horizon_protocol::rsa::RsaPublicKey] { + self.keys.get_or_init(|| { + let handle = self.fetch.lock().ok().and_then(|mut h| h.take()); + handle.and_then(|h| h.join().ok()).unwrap_or_default() + }) + } + /// enforceSecureProfile: enforced, and able to check keys. + pub fn enforced(&self) -> bool { + self.enforce && !self.keys().is_empty() + } +} + /// State shared with the connection threads. pub struct Shared { pub sync: Arc<data::SyncData>, @@ -272,7 +346,7 @@ impl Shared { Json::String(format!("data:image/png;base64,{}", base64(icon))), )); } - if self.config.enforces_secure_chat { + if self.config.enforces_secure_chat() { status.push(("enforcesSecureChat".to_owned(), Json::Bool(true))); } Json::Object(status).compact() diff --git a/crates/horizon-client/src/lan/playerdata.rs b/crates/horizon-client/src/lan/playerdata.rs index d824683d2..d7dc097dd 100644 --- a/crates/horizon-client/src/lan/playerdata.rs +++ b/crates/horizon-client/src/lan/playerdata.rs @@ -68,8 +68,15 @@ pub fn apply(guest: &mut Guest, level: &Level) { guest.xp_total = level.xp_total; guest.effects = crate::effects::PlayerEffects::from_saved(&level.active_effects); guest.game_mode = level.mode(); + guest.flying = level.flying; guest.selected = (level.selected_slot.max(0) as usize).min(8); + guest.spawn = level.spawn.map(|s| [s[0] + 0.5, s[1], s[2] + 0.5]); guest.saved = level.player_extra.clone(); + guest.recipe_book = level.recipe_book.clone(); + guest.dimension = match level.dimension.as_str() { + d @ ("minecraft:the_nether" | "minecraft:the_end") => d.to_owned(), + _ => "minecraft:overworld".to_owned(), + }; if level.ender_items.len() == 27 { guest.ender_items = level.ender_items.clone(); } @@ -99,7 +106,6 @@ pub fn apply(guest: &mut Guest, level: &Level) { pub fn save(guest: &Guest, world: &Path) { let mut level = Level { player_uuid: Some(uuid_ints(guest.uuid)), - dimension: "minecraft:overworld".into(), ..Level::default() }; store(guest, &mut level); @@ -113,6 +119,7 @@ pub fn save(guest: &Guest, world: &Path) { /// Put a guest's state in `level`'s player fields. pub fn store(guest: &Guest, level: &mut Level) { level.position = Some(guest.position); + level.dimension = guest.dimension.clone(); level.rotation = [guest.yaw, guest.pitch]; level.health = guest.health; level.food = guest.food; @@ -123,9 +130,12 @@ pub fn store(guest: &Guest, level: &mut Level) { level.active_effects = guest.effects.saved(); level.game_mode = guest.game_mode; level.creative = guest.game_mode == 1; + level.flying = guest.flying; level.selected_slot = guest.selected as i32; + level.spawn = guest.spawn.map(|s| [s[0] - 0.5, s[1], s[2] - 0.5]); level.on_ground = guest.on_ground; level.player_extra = guest.saved.clone(); + level.recipe_book = guest.recipe_book.clone(); level.ender_items = guest.ender_items.clone(); level.inventory.clear(); level.inventory_components.clear(); diff --git a/crates/horizon-client/src/lan/waypoints.rs b/crates/horizon-client/src/lan/waypoints.rs new file mode 100644 index 000000000..852cfc4fb --- /dev/null +++ b/crates/horizon-client/src/lan/waypoints.rs @@ -0,0 +1,641 @@ +//! The locator bar for remote players: ServerWaypointManager and +//! WaypointTransmitter's connections, for players (the transmitters vanilla has +//! by default: `waypoint_transmit_range` is 0 for every other entity). +//! +//! Vanilla updates a player's connections each time the player's position +//! changes (`Entity.setPosRaw`, then `ServerChunkCache.move`). The hub sees the +//! position once a tick, after the moves it handled, and updates from there: +//! the same packets, since a client sends one move a tick. Hiding (crouching, +//! invisibility, a carved pumpkin or a mob head worn) changes +//! `waypoint_transmit_range` and tracks or untracks the waypoint, as +//! `LivingEntity.onAttributeUpdated` does; joining or leaving a team remakes the +//! connections with the team's colour (`ServerScoreboard.updatePlayerWaypoint`). +use crate::fast_hash::FxHashMap as HashMap; +use horizon_protocol::packets::play::{ + Clientbound as C, TrackedWaypoint, WaypointId, WaypointTarget, waypoint_operation, +}; + +/// `Player.createAttributes`: both waypoint ranges. +pub const PLAYER_RANGE: f64 = 6.0e7; +/// `WaypointTransmitter.REALLY_FAR_DISTANCE`. +const REALLY_FAR: f32 = 332.0; +/// `EntityAzimuthConnection.update`: half a degree. +const AZIMUTH_STEP: f32 = 0.008_726_646; + +/// Items that hide their wearer (`Waypoint.addHideAttribute` on the head slot). +pub const HIDING_HEADS: [&str; 8] = [ + "minecraft:carved_pumpkin", + "minecraft:skeleton_skull", + "minecraft:wither_skeleton_skull", + "minecraft:player_head", + "minecraft:zombie_head", + "minecraft:creeper_head", + "minecraft:dragon_head", + "minecraft:piglin_head", +]; + +/// A team colour as the locator bar draws it (`Waypoint.Icon.cloneAndAssignStyle`: +/// `TeamColor.rgb()`, black lightened so it shows). +pub fn team_rgb(color: &str) -> Option<i32> { + let rgb = match color { + "black" => return Some(-13_619_152), + "dark_blue" => 0x0000AA, + "dark_green" => 0x00AA00, + "dark_aqua" => 0x00AAAA, + "dark_red" => 0xAA0000, + "dark_purple" => 0xAA00AA, + "gold" => 0xFFAA00, + "gray" => 0xAAAAAA, + "dark_gray" => 0x555555, + "blue" => 0x5555FF, + "green" => 0x55FF55, + "aqua" => 0x55FFFF, + "red" => 0xFF5555, + "light_purple" => 0xFF55FF, + "yellow" => 0xFFFF55, + "white" => 0xFFFFFF, + _ => return None, + }; + Some(rgb) +} + +/// Waypoint.Icon. +#[derive(Clone, Debug, PartialEq)] +pub struct Icon { + pub style: String, + pub color: Option<i32>, +} + +impl Default for Icon { + fn default() -> Self { + Self { + style: "minecraft:default".into(), + color: None, + } + } +} + +/// A player as the waypoint manager sees them this tick. +#[derive(Clone, Debug)] +pub struct Player { + pub uuid: u128, + /// The connection that receives the player's locator bar (None: the host, + /// whose own game draws it). + pub conn: Option<u32>, + pub position: [f64; 3], + pub spectator: bool, + /// `waypoint_transmit_range`: 0 while hidden. + pub transmit_range: f64, + pub receive_range: f64, + /// `Entity.firstTick`: the player has not ticked since joining. + pub first_tick: bool, + /// The chunk view radius (`ChunkMap.getPlayerViewDistance`). + pub view_distance: i32, + /// The player's team and its colour (`TeamColor.rgb`). + pub team: Option<(String, Option<i32>)>, + /// The icon `/waypoint modify` set. + pub icon: Icon, +} + +impl Player { + fn block(&self) -> [i32; 3] { + self.position.map(|v| v.floor() as i32) + } + fn chunk(&self) -> [i32; 2] { + let [x, _, z] = self.block(); + [x >> 4, z >> 4] + } + /// `Entity.distanceTo`. + fn distance_to(&self, other: &Player) -> f32 { + let d2: f64 = (0..3) + .map(|k| (self.position[k] - other.position[k]).powi(2)) + .sum(); + d2.sqrt() as f32 + } +} + +#[derive(Clone, Copy, Debug, PartialEq)] +enum Kind { + Block([i32; 3]), + Chunk([i32; 2]), + Azimuth(f32), +} + +#[derive(Clone, Debug)] +struct Connection { + kind: Kind, + icon: Icon, +} + +/// What the manager remembers of a player between ticks. +struct Memo { + position: [f64; 3], + /// The centre of the player's ChunkTrackingView (`ChunkMap.move`). + view_center: [i32; 2], + transmitting: bool, + team: Option<(String, Option<i32>)>, +} + +/// ServerWaypointManager for one level. +#[derive(Default)] +pub struct Waypoints { + /// The transmitters, in the order they were tracked. + waypoints: Vec<u128>, + /// The receivers, in the order they joined. + players: Vec<u128>, + /// Connections by receiver, then source. + connections: HashMap<u128, Vec<(u128, Connection)>>, + memo: HashMap<u128, Memo>, + /// The `locator_bar` game rule as last seen. + enabled: Option<bool>, +} + +/// One tick's players, and the packets for their connections. +struct Tick<'a> { + players: HashMap<u128, &'a Player>, + view: HashMap<u128, ([i32; 2], i32)>, + /// Players a respawn replaced this tick: a new ServerPlayer, on its first tick. + fresh: &'a [u128], + enabled: bool, + out: Vec<(u32, C)>, +} + +impl<'a> Tick<'a> { + fn send( + &mut self, + receiver: u128, + source: u128, + operation: i32, + icon: &Icon, + target: WaypointTarget, + ) { + let Some(conn) = self.players.get(&receiver).and_then(|p| p.conn) else { + return; + }; + self.out + .push((conn, waypoint(source, operation, icon, target))); + } + fn connect(&mut self, receiver: u128, source: u128, c: &Connection) { + let target = match c.kind { + Kind::Block(p) => WaypointTarget::Block(p), + Kind::Chunk(p) => WaypointTarget::Chunk(p), + Kind::Azimuth(a) => WaypointTarget::Azimuth(a), + }; + self.send(receiver, source, waypoint_operation::TRACK, &c.icon, target); + } + /// `ClientboundTrackedWaypointPacket.removeWaypoint`: an empty waypoint with the null icon. + fn disconnect(&mut self, receiver: u128, source: u128) { + self.send( + receiver, + source, + waypoint_operation::UNTRACK, + &Icon::default(), + WaypointTarget::Empty, + ); + } + /// `ChunkTrackingView.isInViewDistance` of the receiver's view. + fn chunk_visible(&self, [x, z]: [i32; 2], receiver: u128) -> bool { + let Some(&([cx, cz], r)) = self.view.get(&receiver) else { + return false; + }; + let dx = i64::from(((x - cx).abs() - 1).max(0)); + let dz = i64::from(((z - cz).abs() - 1).max(0)); + dx * dx + dz * dz < i64::from(r) * i64::from(r) + } + /// `WaypointTransmitter.doesSourceIgnoreReceiver`. + fn ignores(&self, source: &Player, receiver: &Player) -> bool { + if receiver.spectator { + return false; + } + if source.spectator { + return true; + } + let range = source.transmit_range.min(receiver.receive_range); + f64::from(source.distance_to(receiver)) >= range + } + fn really_far(source: &Player, receiver: &Player) -> bool { + source.distance_to(receiver) > REALLY_FAR + } + /// `EntityAzimuthConnection`: the angle of the receiver seen from the source, + /// `Mth.atan2` of the offset turned clockwise. + fn azimuth(source: &Player, receiver: &Player) -> f32 { + let d = [0, 1, 2].map(|k| receiver.position[k] - source.position[k]); + horizon_core::geometry::trig::atan2(d[0], -d[2]) as f32 + } + /// The source and the receiver, if both are in the level this tick. + fn pair(&self, source: u128, receiver: u128) -> Option<(&'a Player, &'a Player)> { + Some((*self.players.get(&source)?, *self.players.get(&receiver)?)) + } + /// `LivingEntity.makeWaypointConnectionWith`. + fn make(&self, (s, r): (&Player, &Player)) -> Option<Connection> { + if s.first_tick || self.fresh.contains(&s.uuid) || s.uuid == r.uuid || self.ignores(s, r) { + return None; + } + // Waypoint.Icon.cloneAndAssignStyle: the icon's colour, else the team's. + let mut icon = s.icon.clone(); + if icon.color.is_none() { + icon.color = s.team.as_ref().and_then(|t| t.1); + } + let kind = if Self::really_far(s, r) { + Kind::Azimuth(Self::azimuth(s, r)) + } else if !self.chunk_visible(s.chunk(), r.uuid) { + Kind::Chunk(s.chunk()) + } else { + Kind::Block(s.block()) + }; + Some(Connection { kind, icon }) + } + /// `Connection.isBroken` for each kind. + fn broken(&self, c: &Connection, (s, r): (&Player, &Player)) -> bool { + let receiver = r.uuid; + match c.kind { + Kind::Block(last) => { + let now = s.block(); + (0..3).map(|k| (now[k] - last[k]).abs()).sum::<i32>() > 1 || self.ignores(s, r) + } + Kind::Chunk(last) => { + let now = s.chunk(); + let moved = (now[0] - last[0]).abs().max((now[1] - last[1]).abs()); + moved > 1 || self.ignores(s, r) || self.chunk_visible(last, receiver) + } + Kind::Azimuth(_) => { + self.ignores(s, r) + || self.chunk_visible(s.chunk(), receiver) + || !Self::really_far(s, r) + } + } + } + /// `Connection.update`: the packet when the waypoint moved. + fn update(&mut self, c: &mut Connection, (s, r): (&Player, &Player)) { + let target = match &mut c.kind { + Kind::Block(last) => { + let now = s.block(); + if now == *last { + return; + } + *last = now; + WaypointTarget::Block(now) + } + Kind::Chunk(last) => { + let now = s.chunk(); + if now == *last { + return; + } + *last = now; + WaypointTarget::Chunk(now) + } + Kind::Azimuth(last) => { + let now = Self::azimuth(s, r); + if (now - *last).abs() <= AZIMUTH_STEP { + return; + } + *last = now; + WaypointTarget::Azimuth(now) + } + }; + if let Some(conn) = r.conn { + self.out.push(( + conn, + waypoint(s.uuid, waypoint_operation::UPDATE, &c.icon, target), + )); + } + } +} + +fn waypoint(source: u128, operation: i32, icon: &Icon, target: WaypointTarget) -> C { + C::TrackedWaypoint(TrackedWaypoint { + operation, + id: WaypointId::Uuid(source), + style: icon.style.clone(), + color: icon.color, + target, + }) +} + +impl Waypoints { + fn put(&mut self, receiver: u128, source: u128, c: Connection) { + let row = self.connections.entry(receiver).or_default(); + match row.iter_mut().find(|(s, _)| *s == source) { + Some(slot) => slot.1 = c, + None => row.push((source, c)), + } + } + fn take(&mut self, receiver: u128, source: u128) -> Option<Connection> { + let row = self.connections.get_mut(&receiver)?; + let i = row.iter().position(|(s, _)| *s == source)?; + Some(row.remove(i).1) + } + + /// `createConnection`. + fn create_connection(&mut self, t: &mut Tick, receiver: u128, source: u128) { + if receiver == source || !t.enabled { + return; + } + match t.pair(source, receiver).and_then(|pair| t.make(pair)) { + Some(c) => { + t.connect(receiver, source, &c); + self.put(receiver, source, c); + } + None => { + if self.take(receiver, source).is_some() { + t.disconnect(receiver, source); + } + } + } + } + /// The receiver's connection to the source updated in its place + /// (`updateConnection`), or made if it has none (`createConnection`). + fn update_or_create(&mut self, t: &mut Tick, receiver: u128, source: u128) { + if receiver == source || !t.enabled { + return; + } + let Some(row) = self.connections.get_mut(&receiver) else { + return self.create_connection(t, receiver, source); + }; + let Some(i) = row.iter().position(|(s, _)| *s == source) else { + return self.create_connection(t, receiver, source); + }; + let c = &mut row[i].1; + // A connection to a player who isn't in the level is broken. + let pair = t.pair(source, receiver); + if let Some(pair) = pair + && !t.broken(c, pair) + { + t.update(c, pair); + return; + } + match pair.and_then(|pair| t.make(pair)) { + Some(new) => { + t.connect(receiver, source, &new); + *c = new; + } + None => { + t.disconnect(receiver, source); + row.remove(i); + } + } + } + /// `trackWaypoint`. + fn track(&mut self, t: &mut Tick, source: u128) { + if !self.waypoints.contains(&source) { + self.waypoints.push(source); + } + for receiver in self.players.clone() { + self.create_connection(t, receiver, source); + } + } + /// `updateWaypoint`. + fn update_waypoint(&mut self, t: &mut Tick, source: u128) { + if !self.waypoints.contains(&source) { + return; + } + // Connections change; the players don't. + for k in 0..self.players.len() { + self.update_or_create(t, self.players[k], source); + } + } + /// `untrackWaypoint`. + fn untrack(&mut self, t: &mut Tick, source: u128) { + for receiver in self.players.clone() { + if self.take(receiver, source).is_some() { + t.disconnect(receiver, source); + } + } + self.waypoints.retain(|w| *w != source); + } + /// `addPlayer`. + fn add_player(&mut self, t: &mut Tick, player: u128, transmitting: bool) { + if !self.players.contains(&player) { + self.players.push(player); + } + for source in self.waypoints.clone() { + self.create_connection(t, player, source); + } + if transmitting { + self.track(t, player); + } + } + /// `updatePlayer`. + fn update_player(&mut self, t: &mut Tick, player: u128) { + // Connections change; the waypoints don't. + for k in 0..self.waypoints.len() { + self.update_or_create(t, player, self.waypoints[k]); + } + } + /// `removePlayer`. + fn remove_player(&mut self, t: &mut Tick, player: u128) { + for (source, _) in self.connections.remove(&player).unwrap_or_default() { + t.disconnect(player, source); + } + self.untrack(t, player); + self.players.retain(|p| *p != player); + } + /// `breakAllConnections`. + fn break_all(&mut self, t: &mut Tick) { + for receiver in self.players.clone() { + for (source, _) in self.connections.remove(&receiver).unwrap_or_default() { + t.disconnect(receiver, source); + } + } + self.connections.clear(); + } + + /// One tick: players who left, joined, moved, hid or changed team, and the + /// `locator_bar` game rule. `respawned` players are removed and added again, + /// as `PlayerList.respawn` replaces the ServerPlayer. Returns the packets for + /// each connection. + pub fn tick( + &mut self, + players: &[Player], + respawned: &[u128], + locator_bar: bool, + ) -> Vec<(u32, C)> { + let mut t = Tick { + players: players.iter().map(|p| (p.uuid, p)).collect(), + view: HashMap::default(), + fresh: respawned, + enabled: locator_bar, + out: Vec::new(), + }; + // Vanilla reads the rule when it connects; changing it breaks or remakes + // every connection (MinecraftServer.onGameRuleChanged). + let was = self.enabled.replace(locator_bar); + for (uuid, memo) in &self.memo { + if let Some(p) = t.players.get(uuid) { + t.view.insert(*uuid, (memo.view_center, p.view_distance)); + } + } + // Players who left, and the ServerPlayer a respawn replaced. + let gone: Vec<u128> = self + .players + .iter() + .copied() + .filter(|uuid| !t.players.contains_key(uuid) || respawned.contains(uuid)) + .collect(); + for uuid in gone { + self.remove_player(&mut t, uuid); + self.memo.remove(&uuid); + } + if was == Some(true) && !locator_bar { + self.break_all(&mut t); + } + // Players who joined (PlayerList.placeNewPlayer: addPlayer). + for p in players { + if self.memo.contains_key(&p.uuid) { + continue; + } + let center = p.chunk(); + t.view.insert(p.uuid, (center, p.view_distance)); + let transmitting = p.transmit_range > 0.0; + self.memo.insert( + p.uuid, + Memo { + position: p.position, + view_center: center, + transmitting, + team: p.team.clone(), + }, + ); + if p.receive_range > 0.0 { + self.add_player(&mut t, p.uuid, transmitting); + } else if transmitting { + self.track(&mut t, p.uuid); + } + } + if was == Some(false) && locator_bar { + for player in self.players.clone() { + self.update_player(&mut t, player); + } + } + for p in players { + let memo = &self.memo[&p.uuid]; + // Entity.setPosRaw, then ServerChunkCache.move. + if memo.position != p.position { + let moved_view = memo.view_center != p.chunk(); + let transmitting = memo.transmitting; + self.memo.get_mut(&p.uuid).unwrap().position = p.position; + if !p.first_tick { + if transmitting { + self.update_waypoint(&mut t, p.uuid); + } + if p.receive_range > 0.0 { + self.update_player(&mut t, p.uuid); + } + } + if moved_view { + self.memo.get_mut(&p.uuid).unwrap().view_center = p.chunk(); + t.view.insert(p.uuid, (p.chunk(), p.view_distance)); + } + if p.receive_range > 0.0 { + self.update_player(&mut t, p.uuid); + } + } + // LivingEntity.onAttributeUpdated(WAYPOINT_TRANSMIT_RANGE). + let transmitting = p.transmit_range > 0.0; + if transmitting != self.memo[&p.uuid].transmitting { + self.memo.get_mut(&p.uuid).unwrap().transmitting = transmitting; + if transmitting { + self.track(&mut t, p.uuid); + } else { + self.untrack(&mut t, p.uuid); + } + } + // ServerScoreboard: a team joined, left or recoloured remakes the connections. + if p.team != self.memo[&p.uuid].team { + self.memo.get_mut(&p.uuid).unwrap().team = p.team.clone(); + for receiver in self.players.clone() { + self.create_connection(&mut t, receiver, p.uuid); + } + } + } + t.out + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn player(uuid: u128, conn: u32, position: [f64; 3]) -> Player { + Player { + uuid, + conn: Some(conn), + position, + spectator: false, + transmit_range: PLAYER_RANGE, + receive_range: PLAYER_RANGE, + first_tick: false, + view_distance: 4, + team: None, + icon: Icon::default(), + } + } + + /// The packets to `conn`, as (operation, target). + fn to(out: &[(u32, C)], conn: u32) -> Vec<(i32, WaypointTarget)> { + out.iter() + .filter(|(c, _)| *c == conn) + .map(|(_, p)| match p { + C::TrackedWaypoint(w) => (w.operation, w.target), + other => panic!("{other:?}"), + }) + .collect() + } + + #[test] + fn a_new_player_shows_once_it_moves() { + let mut w = Waypoints::default(); + let a = player(1, 10, [0.5, 100.0, 0.5]); + let mut b = player(2, 20, [5.5, 100.0, 0.5]); + b.first_tick = true; + w.tick(std::slice::from_ref(&a), &[], true); + let out = w.tick(&[a.clone(), b.clone()], &[], true); + // B gets A at once; A sees nothing of B while B hasn't ticked. + assert_eq!(to(&out, 20), [(0, WaypointTarget::Block([0, 100, 0]))]); + assert!(to(&out, 10).is_empty()); + b.first_tick = false; + assert!(w.tick(&[a.clone(), b.clone()], &[], true).is_empty()); + b.position[0] += 1.0; + let out = w.tick(&[a.clone(), b.clone()], &[], true); + assert_eq!(to(&out, 10), [(0, WaypointTarget::Block([6, 100, 0]))]); + // One block: an update. Three: a new connection. Out of view: a chunk. + b.position[0] += 1.0; + let out = w.tick(&[a.clone(), b.clone()], &[], true); + assert_eq!(to(&out, 10), [(2, WaypointTarget::Block([7, 100, 0]))]); + b.position[0] += 3.0; + let out = w.tick(&[a.clone(), b.clone()], &[], true); + assert_eq!(to(&out, 10), [(0, WaypointTarget::Block([10, 100, 0]))]); + b.position[0] = 100.5; + let out = w.tick(&[a.clone(), b.clone()], &[], true); + assert_eq!(to(&out, 10), [(0, WaypointTarget::Chunk([6, 0]))]); + // Really far: an angle. + b.position[0] = 1000.5; + let out = w.tick(&[a.clone(), b.clone()], &[], true); + assert!(matches!( + to(&out, 10)[..], + [(0, WaypointTarget::Azimuth(_))] + )); + // Hidden: untracked; the rule off: every connection broken. + b.transmit_range = 0.0; + let out = w.tick(&[a.clone(), b.clone()], &[], true); + assert_eq!(to(&out, 10), [(1, WaypointTarget::Empty)]); + let out = w.tick(&[a.clone(), b.clone()], &[], false); + assert_eq!(to(&out, 20), [(1, WaypointTarget::Empty)]); + assert!(to(&out, 10).is_empty()); + // Leaving untracks what was left. + let out = w.tick(&[a.clone(), b.clone()], &[], true); + assert!(matches!( + to(&out, 20)[..], + [(0, WaypointTarget::Azimuth(_))] + )); + let out = w.tick(&[b.clone()], &[], true); + assert_eq!(to(&out, 20), [(1, WaypointTarget::Empty)]); + } + + #[test] + fn team_colours() { + assert_eq!(team_rgb("red"), Some(0xFF5555)); + assert_eq!(team_rgb("black"), Some(0xFF30_3030_u32 as i32)); + assert_eq!(team_rgb("reset"), None); + } +} diff --git a/crates/horizon-client/src/level_loading.rs b/crates/horizon-client/src/level_loading.rs index a6b3d04d9..686938adf 100644 --- a/crates/horizon-client/src/level_loading.rs +++ b/crates/horizon-client/src/level_loading.rs @@ -115,6 +115,27 @@ pub fn draw( } } +/// The startup loading screen: the stage's line and a progress bar, centred +/// like `LevelLoadingScreen`'s text and bar. +pub fn draw_progress(batch: &mut HudBatch, atlas: &HudAtlas, line: &str, progress: f32) { + let (x_center, y_center) = ((batch.width as i32) / 2, (batch.height as i32) / 2); + let text_top = y_center - 50; + let text = Text::literal(line); + let tw = atlas.font.width_text(&text) as i32; + batch.text_styled( + atlas, + &text, + (x_center - tw / 2) as f32, + text_top as f32, + u32::MAX, + true, + ); + let (left, top) = ((x_center - 100) as f32, (text_top + 9 + 3) as f32); + batch.fill(atlas, left, top, 200.0, 2.0, opaque(0)); + let done = (progress.clamp(0.0, 1.0) * 200.0).round(); + batch.fill(atlas, left, top, done, 2.0, opaque(0x00FF00)); +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index 45e09a6a3..7a69de4aa 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -16,6 +16,7 @@ pub mod banner; pub mod beehive; pub mod benchmark; pub mod biome; +pub mod block_entities; pub mod block_entity_anim; pub mod block_entity_item; pub mod block_entity_nbt; @@ -84,6 +85,7 @@ pub mod hud_layout; pub mod hud_overlays; pub mod hunger; pub mod input; +pub mod interaction_input; pub mod inv_sort; pub mod inventory_effects; pub mod item_data; @@ -100,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; @@ -132,7 +135,9 @@ pub mod particles; pub mod patrol; pub mod perf_overlay; pub mod pick_item; +pub mod player_sync; pub mod poi_sections; +pub mod portal_sections; pub mod pose; pub mod post; pub mod projectiles; @@ -170,6 +175,7 @@ pub mod telemetry; pub mod testgen; pub mod text; pub mod tick_chunk_membership; +pub mod ticket_levels; pub mod title; pub mod title_edition; pub mod toasts; diff --git a/crates/horizon-client/src/light.rs b/crates/horizon-client/src/light.rs index fd271f40f..d7ea7d7fd 100644 --- a/crates/horizon-client/src/light.rs +++ b/crates/horizon-client/src/light.rs @@ -14,6 +14,7 @@ //! most 15 blocks, so the 48-wide region determines the centre exactly. //! `relight_block` applies vanilla's incremental decrease/increase passes //! directly to resident chunks after a block change. +use crate::fast_hash::FxHashMap; use crate::{ blocks::BlockFacts, chunk::{Chunk, HEIGHT, Light, MIN_Y, SECTIONS, index}, @@ -44,11 +45,19 @@ pub struct LightTables { /// `faceShapeOccludes(a, b)` for every pair of distinct face shapes. occludes: Vec<bool>, faces: usize, + /// Number of distinct `(shaped, faces)` configurations. + types: usize, + /// Per state: `emission << 16 | type << 4 | dampening`. The low 16 bits are + /// what `light_chunk` stores per cell, so a cell costs 2 bytes and the + /// flood fill never reads the (large) `cells` table. + info: Vec<u32>, + /// `blocked(type_a, type_b, direction)` as a bit per direction. + block_masks: Vec<u8>, } impl LightTables { /// Dampening and emission only: every occlusion face is empty. pub fn new(facts: &BlockFacts) -> Self { - Self { + let mut tables = Self { cells: facts .states .iter() @@ -61,21 +70,64 @@ impl LightTables { .collect(), occludes: vec![false, true, true, true], faces: 2, + types: 1, + info: Vec::new(), + block_masks: Vec::new(), + }; + tables.rebuild_cache(); + tables + } + /// Build the packed per-cell info and the per-configuration blocking masks + /// used by `light_chunk`'s flood fill. + fn rebuild_cache(&mut self) { + let empty: (bool, [u16; 6]) = (false, [EMPTY; 6]); + let mut by_config: HashMap<(bool, [u16; 6]), u16> = HashMap::new(); + by_config.insert(empty, 0); + let mut configs: Vec<(bool, [u16; 6])> = vec![empty]; + let mut info = vec![0u32; self.cells.len()]; + for (id, cell) in self.cells.iter().enumerate() { + let ty = *by_config + .entry((cell.shaped, cell.faces)) + .or_insert_with(|| { + configs.push((cell.shaped, cell.faces)); + (configs.len() - 1) as u16 + }); + let packed = u32::from(ty) << 4 | u32::from(cell.dampening.min(15)); + info[id] = u32::from(cell.emission.min(15)) << 16 | packed; } + let types = configs.len(); + debug_assert!( + types <= 4096, + "light tables: {types} cell configurations exceed the 12-bit type field" + ); + let mut block_masks = vec![0u8; types * types]; + for a in 0..types { + for b in 0..types { + let (shaped_a, faces_a) = configs[a]; + let (shaped_b, faces_b) = configs[b]; + // `blocked`: an unshaped pair covers no face; otherwise the two + // face shapes are compared through the `occludes` table. + if !shaped_a && !shaped_b { + continue; + } + let mut mask = 0u8; + for d in 0..6 { + if self.occludes[faces_a[d] as usize * self.faces + faces_b[d ^ 1] as usize] { + mask |= 1 << d; + } + } + block_masks[a * types + b] = mask; + } + } + self.types = types; + self.info = info; + self.block_masks = block_masks; } /// Full tables with the vanilla occlusion shapes from `occlusion.nbt`. pub fn load(root: &Path, registry: &BlockRegistry, facts: &BlockFacts) -> crate::Result<Self> { - use horizon_world::{ - collision::CachedCollisionRegistry, - nbt::{self, Limits}, - }; - let data = nbt::read_named( - &std::fs::read(root.join("occlusion.nbt"))?, - Limits::tables(), - )? - .0 - .tag; - let occlusion = CachedCollisionRegistry::from_nbt(&data, registry)?; + use horizon_world::{collision::CachedCollisionRegistry, registry::BlockTableFile}; + let file = BlockTableFile::open(&root.join("occlusion.nbt"), registry)?; + let occlusion = CachedCollisionRegistry::from_table(file.table("occlusion")?)?; let mut tables = Self::new(facts); let block = VoxelShape::block(); let mut shapes: Vec<VoxelShape> = vec![VoxelShape::empty(), block.clone()]; @@ -147,8 +199,23 @@ impl LightTables { } tables.occludes = occludes; tables.faces = n; + tables.rebuild_cache(); Ok(tables) } + /// Assembled tables (tests and benchmarks): derives the packed light cache. + #[allow(dead_code)] + fn from_parts(cells: Vec<Cell>, occludes: Vec<bool>, faces: usize) -> Self { + let mut tables = Self { + cells, + occludes, + faces, + types: 0, + info: Vec::new(), + block_masks: Vec::new(), + }; + tables.rebuild_cache(); + tables + } /// `LightEngine.shapeOccludes(from, to, direction)`. #[inline] fn blocked(&self, from: usize, to: usize, direction: usize) -> bool { @@ -160,19 +227,29 @@ impl LightTables { let fb = b.faces[direction ^ 1] as usize; self.occludes[fa * self.faces + fb] } + /// `blocked` for two packed cells (`type << 4 | dampening`). + #[inline] + fn packed_blocked(&self, a: u16, b: u16, direction: usize) -> bool { + self.block_masks[(a >> 4) as usize * self.types + (b >> 4) as usize] & (1 << direction) != 0 + } /// `ChunkSkyLightSources.isEdgeOccluded(top, bottom)`. #[inline] fn edge_occluded(&self, top: usize, bottom: usize) -> bool { self.cells[bottom].dampening != 0 || self.blocked(top, bottom, Direction::Down as usize) } - pub fn emission(&self, state: u16) -> u8 { + /// `edge_occluded` for two packed cells. + #[inline] + fn packed_edge_occluded(&self, top: u16, bottom: u16) -> bool { + bottom & 0xF != 0 || self.packed_blocked(top, bottom, Direction::Down as usize) + } + pub fn emission(&self, state: u32) -> u8 { self.cells[state as usize].emission } - pub fn dampening(&self, state: u16) -> u8 { + pub fn dampening(&self, state: u32) -> u8 { self.cells[state as usize].dampening } /// Whether a state change can change light (`hasDifferentLightProperties`). - pub fn differs(&self, old: u16, new: u16) -> bool { + pub fn differs(&self, old: u32, new: u32) -> bool { let (a, b) = (&self.cells[old as usize], &self.cells[new as usize]); old != new && (a.dampening != b.dampening || a.emission != b.emission || a.shaped || b.shaped) @@ -211,12 +288,14 @@ pub fn light_chunk( // up to 14 blocks into it (sky light there is always 15). let h = ((top_section + 1) * 16).min(HEIGHT as usize); let at = |x: usize, y: usize, z: usize| (y * W + z) * W + x; + // Packed cells (`type << 4 | dampening`), not state ids: the flood fill + // then never touches the large `LightTables::cells` table. let mut states = vec![0u16; W * W * h]; let mut sky = vec![0u8; W * W * h]; let mut block = vec![0u8; W * W * h]; let mut queue = VecDeque::new(); let mut sky_queue = VecDeque::new(); - let mut ids = Box::new([0u16; 4096]); + let mut ids = Box::new([0u32; 4096]); for (dz, row) in region.iter().enumerate() { for (dx, chunk) in row.iter().enumerate() { let Some(chunk) = chunk else { continue }; @@ -228,10 +307,11 @@ pub fn light_chunk( for (i, &id) in ids.iter().enumerate() { let (lx, lz, ly) = (i & 15, (i >> 4) & 15, i >> 8); let p = at(dx * 16 + lx, si * 16 + ly, dz * 16 + lz); - states[p] = id; - let emission = tables.cells[id as usize].emission; + let info = tables.info[id as usize]; + states[p] = info as u16; + let emission = info >> 16; if emission > 0 { - block[p] = emission; + block[p] = emission as u8; queue.push_back(p as u32); } } @@ -239,20 +319,25 @@ pub fn light_chunk( } } // Columns of missing neighbour chunks neither hold nor pass light. - let present = |x: usize, z: usize| region[z / 16][x / 16].is_some(); + let mut present = vec![false; W * W]; + for z in 0..W { + for x in 0..W { + present[z * W + x] = region[z / 16][x / 16].is_some(); + } + } let mut lowest = vec![h as i32; W * W]; for z in 0..W { for x in 0..W { - if !present(x, z) || !sky_light { + if !present[z * W + x] || !sky_light { continue; } // The top layer (above the occupied range) is always open sky. let mut y = h as i32 - 1; sky[at(x, y as usize, z)] = 15; while y > 0 - && !tables.edge_occluded( - states[at(x, y as usize, z)] as usize, - states[at(x, y as usize - 1, z)] as usize, + && !tables.packed_edge_occluded( + states[at(x, y as usize, z)], + states[at(x, y as usize - 1, z)], ) { y -= 1; @@ -263,7 +348,7 @@ pub fn light_chunk( } for z in 0..W { for x in 0..W { - if !present(x, z) || !sky_light { + if !present[z * W + x] || !sky_light { continue; } let own = lowest[z * W + x]; @@ -274,7 +359,7 @@ pub fn light_chunk( (x, z.wrapping_sub(1)), (x, z + 1), ] { - if nx < W && nz < W && present(nx, nz) { + if nx < W && nz < W && present[nz * W + nx] { deepest = deepest.max(lowest[nz * W + nx]); } } @@ -298,24 +383,40 @@ pub fn light_chunk( let x = p % W; let z = (p / W) % W; let y = p / (W * W); - let from = states[p] as usize; - for (d, (ox, oy, oz)) in OFFSETS.iter().enumerate() { - let (nx, ny, nz) = (x as i32 + ox, y as i32 + oy, z as i32 + oz); - if nx < 0 || nz < 0 || ny < 0 || nx >= W as i32 || nz >= W as i32 || ny >= h as i32 - { - continue; - } - let (nx, ny, nz) = (nx as usize, ny as usize, nz as usize); - if !present(nx, nz) { - continue; - } - let q = at(nx, ny, nz); - let to = states[q] as usize; - let next = level.saturating_sub(tables.cells[to].dampening.max(1)); - if next > light[q] && !tables.blocked(from, to, d) { - light[q] = next; - queue.push_back(q as u32); - } + let from = states[p]; + let col = z * W + x; + // Neighbours by linear offset; `present` only depends on the + // column, and the axis bounds rule out wrapping. + macro_rules! neighbour { + ($d:expr, $q:expr, $ncol:expr) => {{ + if present[$ncol] { + let q = $q; + let to = states[q]; + let next = level.saturating_sub((to & 0xF).max(1) as u8); + if next > light[q] && !tables.packed_blocked(from, to, $d) { + light[q] = next; + queue.push_back(q as u32); + } + } + }}; + } + if y > 0 { + neighbour!(0, p - W * W, col); + } + if y + 1 < h { + neighbour!(1, p + W * W, col); + } + if z > 0 { + neighbour!(2, p - W, col - W); + } + if z + 1 < W { + neighbour!(3, p + W, col + W); + } + if x > 0 { + neighbour!(4, p - 1, col - 1); + } + if x + 1 < W { + neighbour!(5, p + 1, col + 1); } } }; @@ -364,10 +465,8 @@ pub fn apply_lighting(chunk: &mut Chunk, lights: Vec<Light>) { } /// `apply_lighting` for a chunk lit before: writes only the sections whose -/// light changed and returns the block positions that changed. -pub fn apply_relighting(chunk: &mut Chunk, lights: Vec<Light>) -> Vec<(i32, i32, i32)> { - let mut changed = Vec::new(); - let (bx, bz) = (chunk.x * 16, chunk.z * 16); +/// light changed and adds the cells that changed to `changed`. +pub fn apply_relighting(chunk: &mut Chunk, lights: Vec<Light>, changed: &mut LightChanges) { for (sy, (section, light)) in chunk.sections.iter_mut().zip(lights).enumerate() { let old = §ion.light; if let (Light::Uniform(a), Light::Uniform(b)) = (old, &light) @@ -375,29 +474,104 @@ pub fn apply_relighting(chunk: &mut Chunk, lights: Vec<Light>) -> Vec<(i32, i32, { continue; } - let before = changed.len(); + let mut classes = 0; for i in 0..4096 { if old.get(i) != light.get(i) { - let y = MIN_Y + sy as i32 * 16 + (i >> 8) as i32; - changed.push((bx + (i & 15) as i32, y, bz + (i >> 4 & 15) as i32)); + let (x, y, z) = (i as i32 & 15, (i >> 8) as i32, (i >> 4) as i32 & 15); + classes |= 1 << LightChanges::class(x, y, z); } } - if changed.len() > before { + if classes != 0 { + changed.add((chunk.x, sy as i32, chunk.z), classes); Arc::make_mut(section).light = light; } } +} + +/// `DataLayer.get`: the nibble at index `i` (`y << 8 | z << 4 | x`), low first. +#[inline] +fn nibble(layer: &[u8], i: usize) -> u8 { + (layer[i >> 1] >> ((i & 1) * 4)) & 15 +} + +/// A column's light as vanilla stores it, two DataLayers per section (sky, +/// block; `layers[i]` for section `i`, `None` where no layer is stored), into +/// the sections' packed light, which then needs no light job. A missing sky +/// layer repeats the bottom layer of the nearest stored one above it, and +/// `above` (per `z << 4 | x`) above the highest +/// (`SkyLightSectionStorage.getLightValue`); a missing block layer is dark. +pub fn light_from_layers<L: AsRef<[u8]>>( + chunk: &mut Chunk, + layers: &[(Option<L>, Option<L>)], + mut above: [u8; 256], + sky_light: bool, +) { + for (si, (sky, block)) in layers.iter().enumerate().take(SECTIONS).rev() { + let sky = sky.as_ref().filter(|_| sky_light); + let section = Arc::make_mut(&mut chunk.sections[si]); + if sky.is_none() && block.is_none() && above.iter().all(|&v| v == above[0]) { + section.light = Light::Uniform(above[0] << 4); + section.compact(); + continue; + } + let mut data = Box::new([0u8; 4096]); + for (i, packed) in data.iter_mut().enumerate() { + let s = sky.map_or(above[i & 255], |a| nibble(a.as_ref(), i)); + let b = block.as_ref().map_or(0, |a| nibble(a.as_ref(), i)); + *packed = s << 4 | b; + } + for (i, a) in above.iter_mut().enumerate() { + *a = data[i] >> 4; + } + section.light = Light::Full(data); + section.compact(); + } + chunk.lit = true; + chunk.light_loaded = true; +} + +/// A light update's layers (`LevelLightEngine.queueSectionData`): each one +/// given replaces that kind of light in its section, the other kind stays. +/// Returns the sections whose light changed. +pub fn update_light_layers<L: AsRef<[u8]>>( + chunk: &mut Chunk, + layers: &[(Option<L>, Option<L>)], + sky_light: bool, +) -> Vec<usize> { + let mut changed = Vec::new(); + for (si, (sky, block)) in layers.iter().enumerate().take(SECTIONS) { + let sky = sky.as_ref().filter(|_| sky_light); + if sky.is_none() && block.is_none() { + continue; + } + let old = &chunk.sections[si].light; + let mut data = Box::new([0u8; 4096]); + for (i, packed) in data.iter_mut().enumerate() { + let was = old.get(i); + let s = sky.map_or(was >> 4, |a| nibble(a.as_ref(), i)); + let b = block.as_ref().map_or(was & 15, |a| nibble(a.as_ref(), i)); + *packed = s << 4 | b; + } + if (0..4096).all(|i| old.get(i) == data[i]) { + continue; + } + let section = Arc::make_mut(&mut chunk.sections[si]); + section.light = Light::Full(data); + section.compact(); + changed.push(si); + } changed } /// Resident chunks the incremental relighter reads and writes. pub trait LightWorld { - fn state(&self, x: i32, y: i32, z: i32) -> Option<u16>; + fn state(&self, x: i32, y: i32, z: i32) -> Option<u32>; /// Packed light (sky << 4 | block), None outside loaded chunks. fn light(&self, x: i32, y: i32, z: i32) -> Option<u8>; fn set_light(&mut self, x: i32, y: i32, z: i32, packed: u8); } impl<S: std::hash::BuildHasher> LightWorld for HashMap<(i32, i32), Arc<Chunk>, S> { - fn state(&self, x: i32, y: i32, z: i32) -> Option<u16> { + fn state(&self, x: i32, y: i32, z: i32) -> Option<u32> { if !(MIN_Y..MIN_Y + HEIGHT).contains(&y) { return None; } @@ -432,6 +606,90 @@ impl<S: std::hash::BuildHasher> LightWorld for HashMap<(i32, i32), Arc<Chunk>, S } } type Pos = (i32, i32, i32); +/// Changed light by section, as vanilla's light engine reports it to +/// `LevelRenderer` (`setSectionDirty`) instead of cell by cell: for each +/// section (x, section index, z), a bit per class of position the changed +/// cells take in it, `3 * 3 * 3` classes for the low face, the inside and the +/// high face of each axis. Meshes sample light one block across a face, so the +/// class says which neighbouring sections a change dirties too. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct LightChanges { + sections: FxHashMap<Pos, u32>, +} +impl LightChanges { + /// A cell whose stored light changed. + #[inline] + pub fn push(&mut self, (x, y, z): Pos) { + let key = (x >> 4, (y - MIN_Y) >> 4, z >> 4); + *self.sections.entry(key).or_default() |= 1 << Self::class(x, y, z); + } + /// Cells of section `key` (x, section index, z) in the given classes. + fn add(&mut self, key: Pos, classes: u32) { + *self.sections.entry(key).or_default() |= classes; + } + /// The class of a cell in its section: per axis 0 on the low face, 1 + /// inside, 2 on the high face. + fn class(x: i32, y: i32, z: i32) -> u32 { + let axis = |v: i32| match v & 15 { + 0 => 0, + 15 => 2, + _ => 1, + }; + axis(x) + 3 * axis(y) + 9 * axis(z) + } + pub fn is_empty(&self) -> bool { + self.sections.is_empty() + } + /// The sections (x, section index, z) holding changed cells. + pub fn sections(&self) -> impl Iterator<Item = Pos> + '_ { + self.sections.keys().copied() + } + /// Every section (x, section index, z) whose cells lie within one block + /// of a changed cell, section indexes outside `0..SECTIONS` left out (a + /// section next to several changed ones comes once for each). + pub fn dirty_sections(&self, mut out: impl FnMut(Pos)) { + // Per class, the bit set of neighbour offsets (dx, dy, dz in -1..=1, + // bit (dx + 1) + 3 (dy + 1) + 9 (dz + 1)) its cells reach. + const REACH: [u32; 27] = { + let mut reach = [0; 27]; + let mut class = 0; + while class < 27 { + let axis = [class % 3, class / 3 % 3, class / 9]; + let mut offset = 0; + while offset < 27 { + let d = [offset % 3, offset / 3 % 3, offset / 9]; + // Every class reaches its own section (d = 1, offset 0); + // the low face also -1 (d = 0), the high face +1 (d = 2). + if (d[0] == 1 || d[0] == axis[0]) + && (d[1] == 1 || d[1] == axis[1]) + && (d[2] == 1 || d[2] == axis[2]) + { + reach[class] |= 1 << offset; + } + offset += 1; + } + class += 1; + } + reach + }; + for (&(x, sy, z), &classes) in &self.sections { + let mut offsets = 0; + let mut rest = classes; + while rest != 0 { + offsets |= REACH[rest.trailing_zeros() as usize]; + rest &= rest - 1; + } + while offsets != 0 { + let o = offsets.trailing_zeros() as i32; + offsets &= offsets - 1; + let s = sy + o / 3 % 3 - 1; + if (0..SECTIONS as i32).contains(&s) { + out((x + o % 3 - 1, s, z + o / 9 - 1)); + } + } + } + } +} fn level(world: &impl LightWorld, sky: bool, p: Pos) -> Option<u8> { world .light(p.0, p.1, p.2) @@ -470,8 +728,8 @@ pub fn lowest_sky_source(world: &impl LightWorld, tables: &LightTables, x: i32, /// Vanilla's `checkBlock` + `runLightUpdates` for changed positions on /// resident, lit chunks: clear the light that may have come through or from /// the old states (decrease pass), then re-seed sources and propagate back in -/// from the brighter boundary (increase pass). Returns every position whose -/// stored light changed, for remeshing. `force` clears around each position +/// from the brighter boundary (increase pass). Adds every position whose +/// stored light changed to `changed`, for remeshing. `force` clears around each position /// as if it had been fully lit, for light computed from outdated blocks. pub fn relight( world: &mut impl LightWorld, @@ -479,16 +737,9 @@ pub fn relight( sky_light: bool, positions: &[Pos], force: bool, -) -> Vec<Pos> { - let mut changed = Vec::new(); - relight_layer( - world, - tables, - false, - positions.to_vec(), - force, - &mut changed, - ); + changed: &mut LightChanges, +) { + relight_layer(world, tables, false, positions.to_vec(), force, changed); if sky_light { let mut seeds = positions.to_vec(); let mut columns: HashMap<(i32, i32), i32> = HashMap::new(); @@ -511,11 +762,8 @@ pub fn relight( y -= 1; } } - relight_layer(world, tables, true, seeds, force, &mut changed); + relight_layer(world, tables, true, seeds, force, changed); } - changed.sort_unstable(); - changed.dedup(); - changed } fn relight_layer( world: &mut impl LightWorld, @@ -523,7 +771,7 @@ fn relight_layer( sky: bool, seeds: Vec<Pos>, force: bool, - changed: &mut Vec<Pos>, + changed: &mut LightChanges, ) { let mut decrease: VecDeque<(Pos, u8)> = VecDeque::new(); let mut cleared: Vec<Pos> = Vec::new(); @@ -611,10 +859,10 @@ fn relight_layer( mod tests { use super::*; - const AIR: u16 = 0; - const STONE: u16 = 1; - const TORCH: u16 = 2; - const SLAB: u16 = 3; + const AIR: u32 = 0; + const STONE: u32 = 1; + const TORCH: u32 = 2; + const SLAB: u32 = 3; /// Air, stone, torch and a bottom slab (full bottom face, half sides, /// empty top), with a third face shape for the half sides. fn tables() -> LightTables { @@ -624,17 +872,17 @@ mod tests { shaped: faces.iter().any(|f| *f != EMPTY), faces, }; - LightTables { - cells: vec![ + LightTables::from_parts( + vec![ cell(0, 0, [EMPTY; 6]), cell(15, 0, [FULL; 6]), cell(0, 14, [EMPTY; 6]), cell(0, 0, [FULL, EMPTY, 2, 2, 2, 2]), ], // empty, full, half: only pairs with a full face cover the square. - occludes: vec![false, true, false, true, true, true, false, true, false], - faces: 3, - } + vec![false, true, false, true, true, true, false, true, false], + 3, + ) } fn world(edit: impl Fn(&mut Chunk)) -> HashMap<(i32, i32), Arc<Chunk>> { let mut chunks = HashMap::new(); @@ -719,7 +967,14 @@ mod tests { ]; for ((x, y, z), state) in edits { Arc::make_mut(w.get_mut(&(0, 0)).unwrap()).set(x, y, z, state); - relight(&mut w, &tables(), true, &[(x, y, z)], false); + relight( + &mut w, + &tables(), + true, + &[(x, y, z)], + false, + &mut LightChanges::default(), + ); } let region: [[Option<&Chunk>; 3]; 3] = std::array::from_fn(|dz| { std::array::from_fn(|dx| w.get(&(dx as i32 - 1, dz as i32 - 1)).map(|c| &**c)) @@ -765,7 +1020,16 @@ mod tests { assert_eq!(edited_snapshot.sections[0].light(index(8, MIN_Y, 8)), 0xF0); let mut world = HashMap::from([((0, 0), Arc::new(current))]); // Streaming replays edits made after the job snapshot with force=true. - relight(&mut world, &tables(), true, &[(8, MIN_Y, 8)], true); + let mut changed = LightChanges::default(); + relight( + &mut world, + &tables(), + true, + &[(8, MIN_Y, 8)], + true, + &mut changed, + ); + assert!(!changed.is_empty()); let region = [[None; 3], [None, Some(&*world[&(0, 0)]), None], [None; 3]]; let fresh = light_chunk(®ion, &tables(), true); for (section, light) in world[&(0, 0)].sections.iter().zip(fresh) { @@ -788,7 +1052,9 @@ mod tests { let sections: Vec<_> = chunk.sections.iter().map(Arc::as_ptr).collect(); // The same light again: nothing changes and no section is copied. let light = lit(&chunk); - assert!(apply_relighting(&mut chunk, light).is_empty()); + let mut changed = LightChanges::default(); + apply_relighting(&mut chunk, light, &mut changed); + assert!(changed.is_empty()); assert_eq!( sections, chunk.sections.iter().map(Arc::as_ptr).collect::<Vec<_>>() @@ -796,8 +1062,8 @@ mod tests { let before = chunk.clone(); chunk.set(3, MIN_Y + 20, 5, STONE); let light = lit(&chunk); - let changed = apply_relighting(&mut chunk, light); - let mut expected = Vec::new(); + apply_relighting(&mut chunk, light, &mut changed); + let mut expected = LightChanges::default(); for (sy, (old, new)) in before.sections.iter().zip(&chunk.sections).enumerate() { for i in 0..4096 { if old.light(i) != new.light(i) { @@ -817,6 +1083,95 @@ mod tests { assert!(copied <= 3, "{copied} sections copied"); } + #[test] + fn changes_dirty_the_sections_within_a_block_of_each_cell() { + // Cells inside sections, on each face and corner, at the bottom and + // top of the world, on negative coordinates. + let mut cells = Vec::new(); + for x in [-17, -16, -1, 0, 7, 15, 16, 31] { + for y in [ + MIN_Y, + MIN_Y + 15, + MIN_Y + 16, + 70, + 79, + 80, + MIN_Y + HEIGHT - 1, + ] { + for z in [-33, -32, 5, 15] { + cells.push((x, y, z)); + } + } + } + for n in [1, 2, cells.len()] { + for start in 0..cells.len().min(8) { + let some: Vec<Pos> = cells.iter().copied().cycle().skip(start).take(n).collect(); + let mut changes = LightChanges::default(); + some.iter().for_each(|&c| changes.push(c)); + let mut dirty = std::collections::BTreeSet::new(); + changes.dirty_sections(|s| { + dirty.insert(s); + }); + let mut expected = std::collections::BTreeSet::new(); + for &(x, y, z) in &some { + for (dx, dy, dz) in (-1..=1) + .flat_map(|a| (-1..=1).flat_map(move |b| (-1..=1).map(move |c| (a, b, c)))) + { + let s = (y + dy - MIN_Y) >> 4; + if (0..SECTIONS as i32).contains(&s) { + expected.insert(((x + dx) >> 4, s, (z + dz) >> 4)); + } + } + } + assert_eq!(dirty, expected, "{some:?}"); + let sections: std::collections::BTreeSet<_> = changes.sections().collect(); + let own: std::collections::BTreeSet<_> = some + .iter() + .map(|&(x, y, z)| (x >> 4, (y - MIN_Y) >> 4, z >> 4)) + .collect(); + assert_eq!(sections, own); + } + } + } + + #[test] + fn stored_layers_fill_the_column_as_vanilla_reads_them() { + // Section 10's sky layer is 7 on its bottom row and 3 above; no + // layers anywhere else; a block layer of 5 in section 2. + let mut sky = vec![0x33u8; 2048]; + sky[..128].fill(0x77); + let mut layers: Vec<(Option<Vec<u8>>, Option<Vec<u8>>)> = vec![(None, None); SECTIONS]; + layers[10].0 = Some(sky); + layers[2].1 = Some(vec![0x55; 2048]); + let mut chunk = Chunk::empty(0, 0); + light_from_layers(&mut chunk, &layers, [15; 256], true); + let at = |c: &Chunk, si: usize, i: usize| c.sections[si].light(i); + assert!(chunk.lit && chunk.light_loaded); + assert!( + matches!(chunk.sections[11].light, Light::Uniform(0xF0)), + "above: open sky" + ); + assert_eq!((at(&chunk, 10, 0), at(&chunk, 10, 4095)), (0x70, 0x30)); + // Below, each section repeats the bottom row of the stored one above. + assert!(matches!(chunk.sections[9].light, Light::Uniform(0x70))); + assert_eq!(at(&chunk, 2, 100), 0x75, "block light only where stored"); + assert!(matches!(chunk.sections[0].light, Light::Uniform(0x70))); + + // A light update replaces one kind of light in the sections it names. + let mut update: Vec<(Option<Vec<u8>>, Option<Vec<u8>>)> = vec![(None, None); SECTIONS]; + update[2].1 = Some(vec![0; 2048]); + update[3].0 = Some(vec![0x77; 2048]); + let changed = update_light_layers(&mut chunk, &update, true); + assert_eq!(changed, vec![2]); + assert!(matches!(chunk.sections[2].light, Light::Uniform(0x70))); + update[3].0 = Some(vec![0xFF; 2048]); + assert_eq!( + update_light_layers(&mut chunk, &update, false), + Vec::<usize>::new(), + "no sky storage" + ); + } + #[test] fn empty_chunk_missing_neighbours_and_no_sky() { let mut chunk = Chunk::empty(0, 0); @@ -889,7 +1244,7 @@ mod tests { for (si, s) in fixture.sections.iter_mut().enumerate() { let section = Arc::make_mut(s); for i in 0..4096 { - section.set(i, ((i + si) % 16) as u16); + section.set(i, ((i + si) % 16) as u32); } for i in 0..64 { section.biomes.set(i, (i % 4) as u32); diff --git a/crates/horizon-client/src/lod.rs b/crates/horizon-client/src/lod.rs new file mode 100644 index 000000000..c2e69a291 --- /dev/null +++ b/crates/horizon-client/src/lod.rs @@ -0,0 +1,1727 @@ +//! Bounded voxel-column terrain for distances beyond the resident chunk area. +//! Far tiles never enter World, lighting, simulation or the section cave graph. +use crate::{ + blocks::BlockFacts, + chunk::{Chunk, MIN_Y}, + fast_hash::{FxHashMap, FxHashSet}, + mesher::SectionMesh, + streaming::{LodDetail, LodSource, WorkerContext}, +}; +mod mesh; +use horizon_world::registry::BlockStateId; +use std::{ + collections::VecDeque, + sync::{ + Arc, Condvar, Mutex, + atomic::{AtomicBool, Ordering}, + mpsc, + }, + thread, +}; + +const CELLS: usize = 16; +const MAX_SAVED: usize = 1024; +const SAMPLE_JOBS: usize = 4; +/// Top solid face and optional fluid face, in absolute block coordinates. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Surface { + pub height: i32, + pub state: BlockStateId, + pub water: Option<(i32, BlockStateId)>, + pub biome: u16, +} +/// A half-open material run; gaps between runs remain air. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Span { + pub min: i16, + pub max: i16, + pub state: BlockStateId, + pub biome: u16, + pub light: u16, +} +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Column { + pub surface: Option<Surface>, + pub spans: Vec<Span>, +} +impl Column { + /// Reduce an entire horizontal footprint, rather than inflating its corner + /// column (a single leaf used to become a whole distant tree-sized slab). + pub(crate) fn reduced(columns: &[Column; 256], x: usize, z: usize, width: usize) -> Self { + if width == 1 { + return columns[z * 16 + x].clone(); + } + let mut events = Vec::new(); + for dz in z..z + width { + for dx in x..x + width { + for span in &columns[dz * 16 + dx].spans { + let material = (span.state, span.biome, span.light); + events.push((span.min, material, 1)); + events.push((span.max, material, -1)); + } + } + } + events.sort_unstable_by_key(|event| event.0); + let mut result = Self::default(); + let mut votes = Vec::<((BlockStateId, u16), i32)>::new(); + let mut sky = [0i32; 16]; + let mut block = [0i32; 16]; + let mut occupied = 0; + let mut bottom = MIN_Y as i16; + for (y, material, change) in events { + if bottom < y { + if occupied * 2 >= (width * width) as i32 + && let Some(&((state, biome), _)) = + votes.iter().max_by_key(|&(material, count)| { + (*count, std::cmp::Reverse((material.0.0, material.1))) + }) + { + // Lighting must not split votes for the same material. + // Reduction can expose buried faces; retain the brightest + // actual light in this occupied footprint, per channel. + let light = (sky.iter().rposition(|&n| n > 0).unwrap_or(0) << 4) + | (block.iter().rposition(|&n| n > 0).unwrap_or(0) << 12); + result.push(Span { + min: bottom, + max: y, + state, + biome, + light: light as u16, + }); + } + bottom = y; + } + occupied += change; + sky[usize::from((material.2 >> 4) & 15)] += change; + block[usize::from((material.2 >> 12) & 15)] += change; + let material = (material.0, material.1); + if let Some(vote) = votes.iter_mut().find(|vote| vote.0 == material) { + vote.1 += change; + } else { + votes.push((material, change)); + } + } + result + } + pub fn from_surface(surface: Option<Surface>) -> Self { + let mut column = Self { + surface, + spans: Vec::new(), + }; + if let Some(s) = surface { + if s.height > MIN_Y { + column.spans.push(Span { + min: MIN_Y as i16, + max: s.height as i16, + state: s.state, + biome: s.biome, + light: 240, + }); + } + if let Some((height, state)) = s.water { + column.spans.push(Span { + min: s.height as i16, + max: height as i16, + state, + biome: s.biome, + light: 240, + }); + } + } + column + } + pub(crate) fn push(&mut self, span: Span) { + if let Some(last) = self.spans.last_mut() + && last.max == span.min + && last.state == span.state + && last.biome == span.biome + && last.light == span.light + { + last.max = span.max; + } else { + self.spans.push(span); + } + } + fn from_chunk(chunk: &Chunk, facts: &BlockFacts, x: i32, z: i32) -> Self { + let face_light = |y: i32| { + let mut sky = 0; + let mut block = 0; + for (dx, dy, dz) in [ + (0, 0, 0), + (-1, 0, 0), + (1, 0, 0), + (0, -1, 0), + (0, 1, 0), + (0, 0, -1), + (0, 0, 1), + ] { + if !(0..16).contains(&(x + dx)) || !(0..16).contains(&(z + dz)) { + continue; + } + let value = chunk.light(x + dx, y + dy, z + dz); + sky = sky.max(value & 0xf0); + block = block.max(value & 15); + } + sky | block + }; + let mut column = Self::default(); + for (sy, section) in chunk.sections.iter().enumerate() { + let min = MIN_Y + sy as i32 * 16; + let uniform = section.blocks.bits() == 0 + && section.biomes.bits() == 0 + && matches!(section.light, crate::chunk::Light::Uniform(_)); + let uniform = uniform + && face_light(min) == chunk.light(x, min, z) + && face_light(min + 15) == chunk.light(x, min, z); + let step = if uniform { 16 } else { 1 }; + for offset in (0..16).step_by(step) { + let y = min + offset; + let state = BlockStateId(chunk.get(x, y, z)); + let f = facts.get(state); + if facts.is_air(state) + || !(f.solid_render + || f.full_collision + || f.name.ends_with("_leaves") + || matches!(&*f.name, "minecraft:water" | "minecraft:lava")) + { + continue; + } + let light = face_light(y); + column.push(Span { + min: y as i16, + max: (y + step as i32) as i16, + state, + biome: chunk.biome(x, y, z), + light: u16::from(light & 0xf0) + | (u16::from((light & 15).max(f.emission)) << 12), + }); + } + } + let mut water = None; + for span in column.spans.iter().rev() { + if matches!( + &*facts.get(span.state).name, + "minecraft:water" | "minecraft:lava" + ) { + water.get_or_insert((i32::from(span.max), span.state)); + } else { + column.surface = Some(Surface { + height: i32::from(span.max), + state: span.state, + water, + biome: span.biome, + }); + break; + } + } + if column.surface.is_none() + && let Some((height, state)) = water + { + column.surface = Some(Surface { + height: MIN_Y, + state: facts.air, + water: Some((height, state)), + biome: chunk.biome(x, height - 1, z), + }); + } + column + } +} +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct TileKey { + pub x: i32, + pub z: i32, + pub level: u8, +} +impl TileKey { + pub fn size(self) -> i32 { + 16 << self.level + } + pub fn origin(self) -> [i32; 3] { + [self.x * self.size(), 0, self.z * self.size()] + } + fn children(self) -> [Self; 4] { + std::array::from_fn(|i| Self { + x: self.x * 2 + (i & 1) as i32, + z: self.z * 2 + (i >> 1) as i32, + level: self.level - 1, + }) + } + fn distance(self, camera: [i32; 2]) -> i32 { + let [x, _, z] = self.origin(); + (x - camera[0]) + .max(camera[0] - x - self.size()) + .max(z - camera[1]) + .max(camera[1] - z - self.size()) + .max(0) + } + fn in_view(self, camera: [i32; 2], yaw: f32) -> bool { + let [x, _, z] = self.origin(); + let (sin, cos) = yaw.sin_cos(); + let mut forward = f32::NEG_INFINITY; + let mut left = f32::NEG_INFINITY; + let mut right = f32::NEG_INFINITY; + for (dx, dz) in [ + (0, 0), + (self.size(), 0), + (0, self.size()), + (self.size(), self.size()), + ] { + let (x, z) = ((x + dx - camera[0]) as f32, (z + dz - camera[1]) as f32); + let f = -x * sin + z * cos; + let side = x * cos + z * sin; + forward = forward.max(f); + left = left.max(f * 0.866 + side * 0.5); + right = right.max(f * 0.866 - side * 0.5); + } + forward > 0. && left > 0. && right > 0. + } +} +#[derive(Default)] +struct Plan { + roots: Vec<TileKey>, + nodes: FxHashSet<TileKey>, + split: FxHashSet<TileKey>, + elevation: i32, +} +impl Plan { + #[cfg(test)] + fn new(camera: [i32; 2], detail: i32, distance: u32) -> Self { + Self::with_previous(camera, detail, distance, &Self::default(), 0) + } + fn with_previous( + camera: [i32; 2], + detail: i32, + distance: u32, + previous: &Self, + elevation: i32, + ) -> Self { + let range = distance.min(1024) as i32 * 16; + let mut level = 0; + while (16 << level) < range { + level += 1; + } + let size = 16 << level; + let mut result = Self { + elevation, + ..Self::default() + }; + for z in (camera[1] - range).div_euclid(size)..=(camera[1] + range).div_euclid(size) { + for x in (camera[0] - range).div_euclid(size)..=(camera[0] + range).div_euclid(size) { + let key = TileKey { x, z, level }; + result.roots.push(key); + result.visit(key, camera, detail, range, previous); + } + } + result + } + fn visit(&mut self, key: TileKey, camera: [i32; 2], detail: i32, range: i32, previous: &Self) { + if key.distance(camera) > range { + return; + } + self.nodes.insert(key); + // Keep distant cells small on screen; finest tiles retain individual blocks. + let threshold = key.size() * if previous.split.contains(&key) { 10 } else { 8 }; + if key.level > 0 + && (key.distance(camera) < detail * 16 + 16 + || key.distance(camera).max(self.elevation) < threshold) + { + self.split.insert(key); + for child in key.children() { + self.visit(child, camera, detail, range, previous); + } + } + } + fn select( + &self, + key: TileKey, + ready: &FxHashMap<TileKey, Vec<Column>>, + out: &mut Vec<TileKey>, + ) -> bool { + if !self.nodes.contains(&key) { + return true; + } + if self.split.contains(&key) { + // Visit every child: a missing sibling must not hide ready detail. + let complete = key + .children() + .into_iter() + .map(|child| self.select(child, ready, out)) + .fold(true, |complete, child| complete & child); + if complete { + return true; + } + if ready.contains_key(&key) { + // Its mesh is clipped around every selected finer tile. + out.push(key); + return true; + } + false + } else if ready.contains_key(&key) { + out.push(key); + true + } else { + false + } + } +} +struct MeshInput { + key: TileKey, + columns: Vec<Column>, + neighbors: [Vec<mesh::EdgeColumn>; 4], + finer: Vec<TileKey>, +} +struct ReducedColumns { + columns: Arc<[Column; 256]>, + levels: [Vec<Column>; 4], +} +impl ReducedColumns { + fn new(columns: Arc<[Column; 256]>) -> Self { + let levels = std::array::from_fn(|level| { + let width = 2 << level; + (0..16 / width) + .flat_map(|z| { + let columns = &columns; + (0..16 / width) + .map(move |x| Column::reduced(columns, x * width, z * width, width)) + }) + .collect() + }); + Self { columns, levels } + } + fn get(&self, x: usize, z: usize, width: usize) -> &Column { + if width == 1 { + &self.columns[z * 16 + x] + } else { + &self.levels[width.trailing_zeros() as usize - 1] + [(z / width) * (16 / width) + x / width] + } + } +} +enum Job { + Sample(TileKey), + Reduce(LodDetail), + RequestDetail { + chunks: Vec<(i32, i32)>, + center: [i32; 2], + }, + Mesh { + active: FxHashSet<TileKey>, + inputs: Vec<MeshInput>, + covered: FxHashSet<(i32, i32)>, + bounds: Option<[i32; 4]>, + }, +} +enum Done { + Detail((i32, i32), ReducedColumns), + DetailRequested, + Sample { + key: TileKey, + samples: Vec<Column>, + }, + Mesh { + active: FxHashSet<TileKey>, + meshes: Vec<(TileKey, SectionMesh)>, + }, +} +struct Shared { + jobs: Mutex<VecDeque<Job>>, + wake: Condvar, + stop: AtomicBool, +} +/// One background worker and at most two outstanding sample jobs. Only the +/// current view's quadtree and a bounded set of visited summaries are retained. +pub struct Lod { + shared: Arc<Shared>, + thread: Option<thread::JoinHandle<()>>, + results: mpsc::Receiver<Done>, + #[cfg(test)] + context: Arc<WorkerContext>, + meshing: bool, + invalidated: FxHashSet<(i32, i32)>, + area: Option<([i32; 2], i32, u32, i32)>, + plan: Plan, + pending: FxHashSet<TileKey>, + sample_nearby: bool, + detail_masks: FxHashMap<TileKey, [u64; 4]>, + detail_request_pending: bool, + detail_request_at: std::time::Instant, + ready: FxHashMap<TileKey, Vec<Column>>, + active: FxHashSet<TileKey>, + dirty: FxHashSet<TileKey>, + removed: Vec<TileKey>, + covered: FxHashSet<(i32, i32)>, + covered_bounds: Option<[i32; 4]>, + saved: FxHashMap<(i32, i32), Arc<[Column; 256]>>, + reduced: FxHashMap<(i32, i32), ReducedColumns>, + saved_order: VecDeque<(i32, i32)>, +} +impl Lod { + pub fn new(source: Arc<dyn LodSource>, context: Arc<WorkerContext>) -> Self { + let shared = Arc::new(Shared { + jobs: Mutex::new(VecDeque::new()), + wake: Condvar::new(), + stop: AtomicBool::new(false), + }); + let worker = shared.clone(); + let mesh_context = context.clone(); + let (tx, results) = mpsc::channel(); + let handle = thread::Builder::new() + .name("Far terrain".into()) + .spawn(move || { + let mut sampler = source.sampler(); + let count = thread::available_parallelism() + .map_or(1, |n| n.get() / 4) + .clamp(1, 3); + let mut sampling = Vec::new(); + for _ in 0..count { + let Some(mut sample) = sampler.fork() else { + break; + }; + let shared = worker.clone(); + let tx = tx.clone(); + sampling.push( + thread::Builder::new() + .name("Far sampling".into()) + .spawn(move || { + loop { + let key = { + let mut jobs = shared.jobs.lock().unwrap(); + loop { + if shared.stop.load(Ordering::Relaxed) { + return; + } + if let Some(index) = jobs + .iter() + .position(|job| matches!(job, Job::Sample(_))) + { + let Some(Job::Sample(key)) = jobs.remove(index) + else { + unreachable!() + }; + break key; + } + jobs = shared.wake.wait(jobs).unwrap(); + } + }; + let [x, _, z] = key.origin(); + let samples = + sample.grid(x, z, key.size() / CELLS as i32, CELLS); + if shared.stop.load(Ordering::Relaxed) { + return; + } + if tx.send(Done::Sample { key, samples }).is_err() { + return; + } + } + }) + .expect("far sampling worker"), + ); + } + let mut mesher = mesh::Mesher::default(); + 'work: loop { + for (key, columns) in sampler.drain_details() { + if tx + .send(Done::Detail(key, ReducedColumns::new(columns))) + .is_err() + { + break 'work; + } + } + let job = { + let mut jobs = worker.jobs.lock().unwrap(); + if (jobs.is_empty() + || (!sampling.is_empty() + && jobs.iter().all(|job| matches!(job, Job::Sample(_))))) + && !worker.stop.load(Ordering::Relaxed) + { + jobs = worker + .wake + .wait_timeout(jobs, std::time::Duration::from_millis(100)) + .unwrap() + .0; + } + if worker.stop.load(Ordering::Relaxed) { + break; + } + if sampling.is_empty() { + jobs.pop_front() + } else { + jobs.iter() + .position(|job| !matches!(job, Job::Sample(_))) + .and_then(|index| jobs.remove(index)) + } + }; + let Some(job) = job else { continue }; + let done = match job { + Job::Reduce((key, columns)) => { + Done::Detail(key, ReducedColumns::new(columns)) + } + Job::RequestDetail { chunks, center } => { + sampler.request_detail(&chunks, center); + Done::DetailRequested + } + Job::Sample(key) => { + let [x, _, z] = key.origin(); + let samples = sampler.grid(x, z, key.size() / CELLS as i32, CELLS); + Done::Sample { key, samples } + } + Job::Mesh { + active, + inputs, + covered, + bounds, + } => { + let mut meshes = Vec::with_capacity(inputs.len()); + for input in inputs { + if worker.stop.load(Ordering::Relaxed) { + break 'work; + } + let mesh = mesher.mesh( + input.key, + &input.columns, + &covered, + bounds, + &mesh_context, + &input.neighbors, + &input.finer, + ); + meshes.push((input.key, mesh)); + } + Done::Mesh { active, meshes } + } + }; + if tx.send(done).is_err() { + break; + } + } + worker.stop.store(true, Ordering::Relaxed); + worker.wake.notify_all(); + for handle in sampling { + let _ = handle.join(); + } + }) + .expect("far terrain worker"); + Self { + shared, + thread: Some(handle), + results, + #[cfg(test)] + context, + meshing: false, + invalidated: FxHashSet::default(), + area: None, + plan: Plan::default(), + pending: FxHashSet::default(), + sample_nearby: true, + detail_masks: FxHashMap::default(), + detail_request_pending: false, + detail_request_at: std::time::Instant::now(), + ready: FxHashMap::default(), + active: FxHashSet::default(), + dirty: FxHashSet::default(), + removed: Vec::new(), + covered: FxHashSet::default(), + covered_bounds: None, + saved: FxHashMap::default(), + reduced: FxHashMap::default(), + saved_order: VecDeque::new(), + } + } + pub fn stats(&self) -> (usize, usize) { + (self.active.len(), self.ready.len()) + } + pub fn progress(&self) -> (usize, usize) { + let mut desired = 0; + let mut sampled = 0; + for key in &self.plan.nodes { + if !self.plan.split.contains(key) { + desired += 1; + sampled += usize::from(self.ready.contains_key(key)); + } + } + (desired, sampled) + } + pub fn refined_samples(&self) -> usize { + self.active + .iter() + .filter_map(|key| self.detail_masks.get(key)) + .flat_map(|mask| mask.iter()) + .map(|bits| bits.count_ones() as usize) + .sum() + } + pub fn removed(&mut self) -> Vec<TileKey> { + std::mem::take(&mut self.removed) + } + pub fn invalidate_chunk(&mut self, x: i32, z: i32) { + if self.meshing { + self.invalidated.insert((x, z)); + } + for &key in &self.active { + let [ox, _, oz] = key.origin(); + if x * 16 <= ox + key.size() + && (x + 1) * 16 >= ox + && z * 16 <= oz + key.size() + && (z + 1) * 16 >= oz + { + self.dirty.insert(key); + } + } + } + /// Capture actual block columns, including canopy gaps and material layers. + pub fn observe(&mut self, chunk: &Chunk, facts: &BlockFacts) { + let values = Arc::new(std::array::from_fn(|i| { + Column::from_chunk(chunk, facts, (i & 15) as i32, (i >> 4) as i32) + })); + let key = (chunk.x, chunk.z); + if self.saved.get(&key) == Some(&values) { + return; + } + if !self.saved.contains_key(&key) { + while self.saved.len() >= MAX_SAVED { + if let Some(old) = self.saved_order.pop_front() { + self.saved.remove(&old); + self.reduced.remove(&old); + } + } + self.saved_order.push_back(key); + } + self.apply_fine(key, &values); + self.saved.insert(key, values.clone()); + self.reduced.remove(&key); + let mut jobs = self.shared.jobs.lock().unwrap(); + // Coalesce edits and drop jobs whose bounded visited cache was evicted. + jobs.retain(|job| !matches!(job, Job::Reduce((at, _)) if *at == key || !self.saved.contains_key(at))); + jobs.push_back(Job::Reduce((key, values))); + drop(jobs); + self.shared.wake.notify_all(); + self.invalidate_chunk(chunk.x, chunk.z); + } + fn apply_fine(&mut self, (cx, cz): (i32, i32), columns: &[Column; 256]) { + let fine = TileKey { + x: cx, + z: cz, + level: 0, + }; + if self.plan.nodes.contains(&fine) || self.ready.contains_key(&fine) { + self.ready.insert(fine, columns.to_vec()); + self.detail_masks.insert(fine, [u64::MAX; 4]); + } + } + fn apply_detail(&mut self, (cx, cz): (i32, i32), columns: &ReducedColumns) { + self.apply_fine((cx, cz), &columns.columns); + // A single generated chunk cannot describe a larger cell's area + // (strides over 16 blocks, levels over 4); at each level up to that, + // only the tile holding the chunk overlaps it. + for level in 0..=4u8 { + let size = 16 << level; + let key = TileKey { + x: (cx * 16).div_euclid(size), + z: (cz * 16).div_euclid(size), + level, + }; + let Some(samples) = self.ready.get_mut(&key) else { + continue; + }; + let [ox, _, oz] = key.origin(); + let stride = key.size() / 16; + let x0 = ((cx * 16 - ox + stride - 1).div_euclid(stride)).clamp(0, 16); + let x1 = (((cx + 1) * 16 - ox + stride - 1).div_euclid(stride)).clamp(0, 16); + let z0 = ((cz * 16 - oz + stride - 1).div_euclid(stride)).clamp(0, 16); + let z1 = (((cz + 1) * 16 - oz + stride - 1).div_euclid(stride)).clamp(0, 16); + if x0 == x1 || z0 == z1 { + continue; + } + let mask = self.detail_masks.entry(key).or_default(); + for z in z0..z1 { + for x in x0..x1 { + let index = (z * 16 + x) as usize; + samples[index] = columns + .get( + (ox + x * stride).rem_euclid(16) as usize, + (oz + z * stride).rem_euclid(16) as usize, + stride as usize, + ) + .clone(); + mask[index / 64] |= 1 << (index % 64); + } + } + } + } + fn finish_sample(&mut self, key: TileKey, samples: Vec<Column>) { + self.pending.remove(&key); + if self.plan.nodes.contains(&key) && samples.len() == CELLS * CELLS { + let mut samples = samples; + if let (Some(old), Some(mask)) = (self.ready.get(&key), self.detail_masks.get(&key)) { + for i in 0..256 { + if mask[i / 64] & (1 << (i % 64)) != 0 { + samples[i] = old[i].clone(); + } + } + } + self.ready.insert(key, samples); + } + } + fn finish_detail(&mut self, key: (i32, i32), columns: ReducedColumns) { + if let Some(visited) = self.saved.get(&key).cloned() + && !Arc::ptr_eq(&visited, &columns.columns) + { + // Its latest reduction is queued; never restore old edits or seed data. + self.apply_fine(key, &visited); + return; + } + self.apply_detail(key, &columns); + if self.saved.contains_key(&key) { + self.reduced.insert(key, columns); + } + self.invalidate_chunk(key.0, key.1); + } + fn request_detail(&mut self, camera: [i32; 2]) { + if self.detail_request_pending + || self.detail_request_at.elapsed() < std::time::Duration::from_millis(250) + { + return; + } + self.detail_request_at = std::time::Instant::now(); + let mut keys: Vec<_> = self + .active + .iter() + .filter(|key| { + key.level <= 1 + && !self.plan.split.contains(key) + && !(key.level == 0 && self.covered.contains(&(key.x, key.z))) + && self.detail_masks.get(key) != Some(&[u64::MAX; 4]) + }) + .copied() + .collect(); + keys.sort_unstable_by_key(|key| (key.distance(camera), *key)); + let mut chunks = FxHashSet::default(); + for key in keys { + let [ox, _, oz] = key.origin(); + let stride = key.size() / 16; + let mask = self.detail_masks.get(&key).copied().unwrap_or_default(); + for index in 0..256 { + if mask[index / 64] & (1 << (index % 64)) == 0 { + let chunk = ( + (ox + (index as i32 % 16) * stride).div_euclid(16), + (oz + (index as i32 / 16) * stride).div_euclid(16), + ); + if self.covered.contains(&chunk) { + self.detail_masks.entry(key).or_default()[index / 64] |= 1 << (index % 64); + } else { + chunks.insert(chunk); + } + } + } + if chunks.len() >= 32 { + break; + } + } + let mut chunks: Vec<_> = chunks.into_iter().collect(); + chunks.sort_unstable_by_key(|&(x, z)| { + let dx = i64::from(x * 16 + 8 - camera[0]); + let dz = i64::from(z * 16 + 8 - camera[1]); + dx * dx + dz * dz + }); + chunks.truncate(32); + if !chunks.is_empty() { + self.detail_request_pending = true; + self.shared + .jobs + .lock() + .unwrap() + .push_back(Job::RequestDetail { + chunks, + center: camera, + }); + self.shared.wake.notify_all(); + } + } + pub fn update( + &mut self, + camera: [f64; 3], + yaw: f32, + detail: i32, + distance: u32, + covered: FxHashSet<(i32, i32)>, + ) -> Vec<(TileKey, SectionMesh)> { + let elevation = + (((camera[1] - f64::from(MIN_Y + crate::chunk::HEIGHT)).clamp(0., 16384.)) as i32 / 32) + * 32; + let camera = [camera[0].floor() as i32, camera[2].floor() as i32]; + // Fill the view ahead first; a nearest-only ring spends most work out + // of sight while visible forests remain undecorated noise terrain. + let ahead = (detail * 16 + 128).max(elevation * 2).min(4096) as f32; + let yaw = yaw.to_radians(); + let focus = [ + camera[0] - (yaw.sin() * ahead) as i32, + camera[1] + (yaw.cos() * ahead) as i32, + ]; + // Replan at chunk boundaries, not on every camera movement. + let area = ( + [camera[0].div_euclid(16), camera[1].div_euclid(16)], + detail, + distance, + elevation, + ); + if self.area != Some(area) { + self.area = Some(area); + self.plan = Plan::with_previous(camera, detail, distance, &self.plan, elevation); + self.ready.retain(|key, _| self.plan.nodes.contains(key)); + self.detail_masks + .retain(|key, _| self.plan.nodes.contains(key)); + } + let mut meshes = Vec::new(); + while let Ok(done) = self.results.try_recv() { + match done { + Done::Detail(key, columns) => { + crate::spans::time("lod_detail", || self.finish_detail(key, columns)) + } + Done::DetailRequested => self.detail_request_pending = false, + Done::Sample { key, samples } => self.finish_sample(key, samples), + Done::Mesh { + active, + meshes: completed, + } => { + // Retire parents only when the entire replacement is ready. + self.removed + .extend(self.active.difference(&active).copied()); + self.active = active; + self.meshing = false; + for (x, z) in std::mem::take(&mut self.invalidated) { + self.invalidate_chunk(x, z); + } + meshes.extend(completed); + } + } + } + if self.covered != covered { + let changes: Vec<_> = self + .covered + .symmetric_difference(&covered) + .copied() + .collect(); + for (x, z) in changes { + self.invalidate_chunk(x, z); + } + self.covered_bounds = covered.iter().fold(None, |bounds, &(x, z)| { + Some(match bounds { + None => [x * 16, z * 16, (x + 1) * 16, (z + 1) * 16], + Some([x0, z0, x1, z1]) => [ + x0.min(x * 16), + z0.min(z * 16), + x1.max((x + 1) * 16), + z1.max((z + 1) * 16), + ], + }) + }); + self.covered = covered; + } + // Complete resident columns need no duplicate noise work. Their real + // summaries are retained when the full-detail chunks leave the view. + for &key in &self.plan.nodes { + if key.level == 0 && self.covered.contains(&(key.x, key.z)) { + self.ready + .entry(key) + .or_insert_with(|| vec![Column::default(); CELLS * CELLS]); + } + } + if !self.meshing { + let mut selected = Vec::new(); + for &key in &self.plan.roots { + self.plan.select(key, &self.ready, &mut selected); + } + // Their normal meshes already provide coverage. Empty stand-ins + // must not become hundreds of redundant fallback clipping masks. + selected.retain(|key| key.level != 0 || !self.covered.contains(&(key.x, key.z))); + let next: FxHashSet<_> = selected.iter().copied().collect(); + let changed: Vec<_> = self.active.symmetric_difference(&next).copied().collect(); + for &tile in &next { + let [x, _, z] = tile.origin(); + let size = tile.size(); + if changed.iter().any(|other| { + let [ox, _, oz] = other.origin(); + let os = other.size(); + (x < ox + os && ox < x + size && z < oz + os && oz < z + size) + || (((x + size == ox) || (ox + os == x)) && z < oz + os && oz < z + size) + || (((z + size == oz) || (oz + os == z)) && x < ox + os && ox < x + size) + }) { + self.dirty.insert(tile); + } + } + self.dirty.extend(next.difference(&self.active).copied()); + self.dirty.retain(|key| next.contains(key)); + if !self.dirty.is_empty() || next != self.active { + let inputs = self + .dirty + .iter() + .map(|&key| crate::spans::time("lod_input", || self.mesh_input(key, &next))) + .collect(); + self.dirty.clear(); + self.meshing = true; + self.shared.jobs.lock().unwrap().push_front(Job::Mesh { + active: next, + inputs, + covered: self.covered.clone(), + bounds: self.covered_bounds, + }); + self.shared.wake.notify_all(); + } + } + self.request_detail(focus); + // Every sample slot busy: no tile can be requested this frame. + let free = SAMPLE_JOBS.saturating_sub(self.pending.len()); + if free == 0 { + return meshes; + } + // Load desired nearby leaves directly, alongside broad fallback coverage. + // Waiting for every ancestor/sibling made full detail take minutes to appear. + let mut wanted = Vec::new(); + for &root in &self.plan.roots { + if self.plan.nodes.contains(&root) && !self.ready.contains_key(&root) { + wanted.push((0, 0, root)); + } + } + for &key in &self.plan.nodes { + if !self.plan.split.contains(&key) && !self.ready.contains_key(&key) { + wanted.push(( + if key.in_view(camera, yaw) { 1 } else { 3 }, + i64::from(key.distance(focus)) * 4 + i64::from(key.distance(camera)), + key, + )); + } + } + let mut fallback = Vec::new(); + for &parent in &self.active { + if self.plan.split.contains(&parent) { + for child in parent.children() { + if self.plan.nodes.contains(&child) && !self.ready.contains_key(&child) { + // Covered near terrain bounds projected error. Using + // zero distance for a parent touching the camera kept + // refining it while huge visible walls waited elsewhere. + let priority = + i64::from(child.distance(camera).max((detail * 16).max(elevation))) + * 1024 + / i64::from(child.size()); + fallback.push(( + if child.in_view(camera, yaw) { 2 } else { 4 }, + priority, + child, + )); + } + } + } + } + // Only the first `free` keys not already pending are taken. A key is + // listed at most twice (as a root and as a node), so the best + // `2 * (SAMPLE_JOBS + pending)` entries hold them, in order. + let best = 2 * (SAMPLE_JOBS + self.pending.len()); + if wanted.len() > best { + wanted.select_nth_unstable(best); + wanted.truncate(best); + } + wanted.sort_unstable(); + // Alternate one nearby final tile with one broad refinement tile. + if let Some(fallback) = fallback + .into_iter() + .filter(|(_, _, k)| !self.pending.contains(k)) + .min() + { + let index = if self.sample_nearby { + wanted + .iter() + .position(|(_, _, k)| !self.pending.contains(k)) + .map_or(0, |i| i + 1) + } else { + 0 + }; + wanted.insert(index, fallback); + } + let mut unique = FxHashSet::default(); + let wanted: Vec<_> = wanted + .into_iter() + .map(|(_, _, key)| key) + .filter(|key| !self.pending.contains(key) && unique.insert(*key)) + .collect(); + if !wanted.is_empty() { + let mut jobs = self.shared.jobs.lock().unwrap(); + for key in wanted.into_iter().take(free) { + self.pending.insert(key); + self.sample_nearby = !self.sample_nearby; + jobs.push_back(Job::Sample(key)); + } + drop(jobs); + self.shared.wake.notify_all(); + } + meshes + } + fn visited_column(&self, wx: i32, wz: i32, width: i32) -> Option<&Column> { + let key = (wx.div_euclid(16), wz.div_euclid(16)); + if width == 1 { + self.saved + .get(&key) + .map(|columns| &columns[(wz.rem_euclid(16) * 16 + wx.rem_euclid(16)) as usize]) + } else if width <= 16 { + self.reduced.get(&key).map(|columns| { + columns.get( + wx.rem_euclid(16) as usize, + wz.rem_euclid(16) as usize, + width as usize, + ) + }) + } else { + None + } + } + fn neighbor(&self, wx: i32, wz: i32, active: &FxHashSet<TileKey>) -> Option<(Column, i32)> { + for level in 0..=10 { + let size = 16 << level; + let key = TileKey { + x: wx.div_euclid(size), + z: wz.div_euclid(size), + level, + }; + if !active.contains(&key) { + continue; + } + let stride = 1 << level; + let [ox, _, oz] = key.origin(); + let x = (wx - ox) / stride; + let z = (wz - oz) / stride; + let (sx, sz) = (ox + x * stride, oz + z * stride); + if let Some(column) = self.visited_column(sx, sz, stride) { + return Some((column.clone(), stride)); + } + return Some((self.ready[&key][(z * 16 + x) as usize].clone(), stride)); + } + None + } + fn mesh_input(&self, key: TileKey, active: &FxHashSet<TileKey>) -> MeshInput { + let stride = key.size() / CELLS as i32; + let [ox, _, oz] = key.origin(); + let mut columns = self.ready[&key].clone(); + for z in 0..CELLS { + for x in 0..CELLS { + let (wx, wz) = (ox + x as i32 * stride, oz + z as i32 * stride); + if let Some(column) = self.visited_column(wx, wz, stride) { + columns[z * CELLS + x] = column.clone(); + } + } + } + let neighbors = std::array::from_fn(|direction| { + let mut edge = Vec::new(); + let mut offset = 0; + while offset < key.size() { + let (wx, wz) = match direction { + 0 => (ox - 1, oz + offset), + 1 => (ox + key.size(), oz + offset), + 2 => (ox + offset, oz - 1), + _ => (ox + offset, oz + key.size()), + }; + let (column, neighbor_stride) = self + .neighbor(wx, wz, active) + .unwrap_or_else(|| (Column::default(), stride)); + let coordinate = if direction < 2 { wz } else { wx }; + let width = (neighbor_stride - coordinate.rem_euclid(neighbor_stride)) + .min(key.size() - offset); + edge.push(mesh::EdgeColumn { + start: offset, + end: offset + width, + column, + }); + offset += width; + } + edge + }); + MeshInput { + key, + columns, + neighbors, + finer: active + .iter() + .copied() + .filter(|other| { + let [x, _, z] = other.origin(); + other.level < key.level + && x >= ox + && z >= oz + && x + other.size() <= ox + key.size() + && z + other.size() <= oz + key.size() + }) + .collect(), + } + } + #[cfg(test)] + fn mesh(&self, key: TileKey) -> SectionMesh { + let input = self.mesh_input(key, &self.active); + mesh::Mesher::default().mesh( + key, + &input.columns, + &self.covered, + self.covered_bounds, + &self.context, + &input.neighbors, + &input.finer, + ) + } +} +impl Drop for Lod { + fn drop(&mut self) { + self.shared.stop.store(true, Ordering::Relaxed); + self.shared.wake.notify_all(); + if let Some(thread) = self.thread.take() { + let _ = thread.join(); + } + } +} +#[cfg(test)] +mod tests { + use super::*; + use crate::mesher::TerrainVertex; + #[test] + fn far_plan_is_bounded_and_near_tiles_align_with_chunks() { + for camera in [[0, 0], [-1, -17], [1023, -4097]] { + for distance in [64, 256, 1024] { + let plan = Plan::new(camera, 12, distance); + assert!(plan.nodes.len() < 12000, "{} nodes", plan.nodes.len()); + for key in &plan.nodes { + if !plan.split.contains(key) && key.distance(camera) < 12 * 16 { + assert_eq!(key.level, 0); + } + } + let cells = plan + .nodes + .iter() + .filter(|k| !plan.split.contains(k)) + .count(); + assert!(cells < 10000); + } + } + assert_eq!( + TileKey { + x: -1, + z: -2, + level: 1 + } + .origin(), + [-32, 0, -64] + ); + } + #[test] + fn aerial_distance_avoids_unseen_full_detail_outside_the_near_area() { + let ground = Plan::new([0, 0], 12, 1024); + let high = Plan::with_previous([0, 0], 12, 1024, &Plan::default(), 1024); + assert!(high.nodes.len() < ground.nodes.len()); + assert!( + high.nodes.contains(&TileKey { + x: 0, + z: 0, + level: 0 + }), + "near full-detail footprint stays exact" + ); + assert!( + !high.nodes.contains(&TileKey { + x: 20, + z: 0, + level: 0 + }), + "far terrain viewed from high above needs distance-appropriate detail" + ); + } + #[test] + fn late_parent_keeps_ready_detail_and_only_fills_missing_children() { + let parent = TileKey { + x: -1, + z: 0, + level: 1, + }; + let mut plan = Plan::default(); + plan.roots.push(parent); + plan.nodes.insert(parent); + plan.split.insert(parent); + plan.nodes.extend(parent.children()); + let mut ready = FxHashMap::default(); + ready.insert(parent, vec![]); + ready.insert(parent.children()[0], vec![]); + let mut selected = Vec::new(); + ready.remove(&parent); + assert!(!plan.select(parent, &ready, &mut selected)); + assert_eq!(selected, [parent.children()[0]]); + ready.insert(parent, vec![]); + selected.clear(); + assert!(plan.select(parent, &ready, &mut selected)); + assert_eq!(selected, [parent.children()[0], parent]); + for child in parent.children() { + ready.insert(child, vec![]); + } + selected.clear(); + assert!(plan.select(parent, &ready, &mut selected)); + assert_eq!(selected, parent.children()); + } + #[test] + fn detail_survives_movement_across_a_split_threshold() { + let before = Plan::new([0, 0], 2, 256); + let after = Plan::with_previous([16, 0], 2, 256, &before, 0); + let mut retained = 0; + for key in &before.split { + if key.distance([16, 0]) < key.size() * 10 { + assert!(after.split.contains(key)); + retained += 1; + } + } + assert!(retained > 100); + assert!(after.nodes.len() < 16000); + } + #[test] + fn reduced_material_votes_ignore_light_and_retain_exposure() { + let mut columns: [Column; 256] = std::array::from_fn(|_| Column::default()); + for i in 0..16 { + columns[(i / 4) * 16 + i % 4].spans.push(Span { + min: 60, + max: 64, + state: BlockStateId(if i < 10 { 2 } else { 1 }), + biome: 0, + light: if i < 10 { (i as u16 + 6) << 4 } else { 0 }, + }); + } + let coarse = Column::reduced(&columns, 0, 0, 4); + assert_eq!(coarse.spans.len(), 1); + assert_eq!( + coarse.spans[0].state, + BlockStateId(2), + "majority grass must beat dark dirt" + ); + assert_eq!(coarse.spans[0].light, 240, "retain sampled sky exposure"); + for column in &mut columns { + for span in &mut column.spans { + span.light = 0; + } + } + assert_eq!( + Column::reduced(&columns, 0, 0, 4).spans[0].light, + 0, + "unlit caves stay unlit" + ); + columns[0].spans[0].light = 7 << 12; + assert_eq!( + Column::reduced(&columns, 0, 0, 4).spans[0].light, + 7 << 12, + "block light does not invent skylight" + ); + } + #[test] + fn coarse_voxels_reduce_the_area_instead_of_inflating_one_leaf() { + let span = |min, max, state| Span { + min, + max, + state: BlockStateId(state), + biome: 0, + light: 240, + }; + let mut columns = std::array::from_fn(|_| Column { + surface: None, + spans: vec![span(0, 4, 1)], + }); + columns[0].spans.push(span(8, 10, 2)); + assert_eq!(Column::reduced(&columns, 0, 0, 4).spans, [span(0, 4, 1)]); + for z in 0..4 { + for x in 0..4 { + columns[z * 16 + x].spans.push(span(12, 14, 3)); + } + } + assert_eq!( + Column::reduced(&columns, 0, 0, 4).spans, + [span(0, 4, 1), span(12, 14, 3)] + ); + assert_eq!(Column::reduced(&columns, 0, 0, 1), columns[0]); + let reduced = ReducedColumns::new(Arc::new(columns)); + assert_eq!( + reduced.get(0, 0, 1).spans, + [span(0, 4, 1), span(8, 10, 2), span(12, 14, 3)] + ); + assert_eq!(reduced.get(0, 0, 4).spans, [span(0, 4, 1), span(12, 14, 3)]); + assert_eq!(reduced.get(4, 8, 4).spans, [span(0, 4, 1)]); + assert_eq!(reduced.get(0, 0, 16).spans, [span(0, 4, 1)]); + } + #[test] + #[ignore = "requires prepared models/textures in HORIZON_TEST_RESOURCES"] + fn real_mesh_clips_near_chunks_and_preserves_water_and_edited_surfaces() -> crate::Result<()> { + use crate::{ + assets::Assets, biome::Biomes, light::LightTables, mesher::MeshTables, + testgen::TestTerrain, + }; + let root = std::path::PathBuf::from( + std::env::var_os("HORIZON_TEST_RESOURCES").expect("HORIZON_TEST_RESOURCES"), + ); + let assets = Arc::new(Assets::load(&root)?); + let render = Arc::new(assets.reload(&root)?); + let facts = BlockFacts::load(&root, assets.registry)?; + let biomes = Arc::new(Biomes::load(&root)?); + let tables = Arc::new(MeshTables::new( + &assets, + &render, + &facts, + crate::models::load_visibility(&root, assets.registry)?, + )?); + let context = Arc::new(WorkerContext { + light: Arc::new(LightTables::load(&root, assets.registry, &facts)?), + tables, + assets: assets.clone(), + render, + biomes: biomes.clone(), + }); + let source = Arc::new(TestTerrain::new(assets.registry, &biomes)?); + let mut lod = Lod::new(source, context); + let stone = assets.registry.default_state("minecraft:stone").unwrap(); + let water = assets.registry.default_state("minecraft:water").unwrap(); + let key = TileKey { + x: -1, + z: -1, + level: 5, + }; + lod.ready.insert( + key, + vec![ + Column::from_surface(Some(Surface { + height: 70, + state: stone, + water: Some((80, water)), + biome: biomes.id("minecraft:plains"), + })); + CELLS * CELLS + ], + ); + // A chunk covers only one quarter of this parent's 32² cell. Both + // opaque terrain and water must leave precisely that 16² footprint. + lod.covered.insert((-1, -2)); + lod.covered_bounds = Some([-16, -32, 0, -16]); + let mesh = lod.mesh(key); + let top_area = |vertices: &[TerrainVertex], y: f32| { + vertices + .as_chunks::<4>() + .0 + .iter() + .filter(|quad| quad.iter().all(|v| (v.pos[1] - y).abs() < 0.001)) + .map(|quad| { + let a = glam::Vec3::from(quad[0].pos); + let b = glam::Vec3::from(quad[1].pos); + let c = glam::Vec3::from(quad[2].pos); + let normal = (b - a).cross(c - a); + assert!(normal.y > 0.0, "surface must face the camera from above"); + let center = (a + c) * 0.5; + let [ox, _, oz] = key.origin(); + let chunk = ( + ((center.x as i32) + ox).div_euclid(16), + ((center.z as i32) + oz).div_euclid(16), + ); + assert!( + !lod.covered.contains(&chunk), + "LOD overlaps a full-detail chunk" + ); + normal.y + }) + .sum::<f32>() + }; + let expected = (key.size() * key.size() - 16 * 16) as f32; + assert_eq!(top_area(&mesh.solid, 70.0), expected); + assert_eq!(top_area(&mesh.translucent, 80.0 - 1.0 / 9.0), expected); + assert_eq!( + mesh.faces.quads.iter().sum::<u32>() as usize, + mesh.solid.len() / 4 + ); + assert!(mesh.translucent.iter().all(|v| v.light == 240)); + let side = |axis: usize, plane: f32, low: f32, high: f32| { + let other = if axis == 0 { 2 } else { 0 }; + mesh.solid + .as_chunks::<4>() + .0 + .iter() + .filter(|quad| { + quad.iter().all(|v| v.pos[axis] == plane) + && quad.iter().map(|v| v.pos[1]).fold(f32::INFINITY, f32::min) + == MIN_Y as f32 + && quad + .iter() + .map(|v| v.pos[1]) + .fold(f32::NEG_INFINITY, f32::max) + == 70.0 + && quad + .iter() + .map(|v| v.pos[other]) + .fold(f32::INFINITY, f32::min) + == low + && quad + .iter() + .map(|v| v.pos[other]) + .fold(f32::NEG_INFINITY, f32::max) + == high + }) + .map(move |quad| { + let a = glam::Vec3::from(quad[0].pos); + (glam::Vec3::from(quad[1].pos) - a).cross(glam::Vec3::from(quad[2].pos) - a) + [axis] + }) + .collect::<Vec<_>>() + }; + // The two exposed edges face the missing full-detail chunk and seal + // the height mismatch all the way down. Shared subpieces add no walls. + let east_cut = side(0, 496.0, 480.0, 496.0); + assert_eq!(east_cut.len(), 1); + assert!(east_cut[0] > 0.0); + let north_cut = side(2, 496.0, 496.0, 512.0); + assert_eq!(north_cut.len(), 1); + assert!(north_cut[0] < 0.0); + assert!(side(0, 496.0, 496.0, 512.0).is_empty()); + + // Block edits do not refresh Chunk.heights; observation must scan the + // actual blocks, including builds above the old heightmap. + let mut chunk = Chunk::empty(-1, -1); + chunk.set(0, 120, 0, stone.0); + chunk.set(4, 54, 4, stone.0); + for y in 55..64 { + chunk.set(4, y, 4, water.0); + } + lod.observe(&chunk, &facts); + let observed = &lod.saved[&(-1, -1)]; + assert_eq!(observed[0].surface.unwrap().height, 121); + assert_eq!(observed[0].surface.unwrap().state, stone); + assert_eq!(observed[68].surface.unwrap().height, 55); + assert_eq!(observed[68].surface.unwrap().water, Some((64, water))); + + let fine = TileKey { + x: -1, + z: -1, + level: 0, + }; + lod.ready.insert(fine, vec![Column::default(); 256]); + lod.finish_detail( + (-1, -1), + ReducedColumns::new(Arc::new(std::array::from_fn(|_| Column::default()))), + ); + assert_eq!( + lod.ready[&fine][0].surface.unwrap().height, + 121, + "late seed generation cannot overwrite a visited build" + ); + assert_eq!(lod.detail_masks[&fine], [u64::MAX; 4]); + lod.plan.nodes.insert(fine); + lod.pending.insert(fine); + lod.finish_sample(fine, vec![Column::default(); 256]); + assert_eq!( + lod.ready[&fine][0].surface.unwrap().height, + 121, + "a late coarse sampling job cannot erase completed detail" + ); + assert_eq!(lod.detail_masks[&fine], [u64::MAX; 4]); + lod.plan.nodes.remove(&fine); + + // Real worker results populate the reusable pyramid; an older result + // must never put a previous edit back into that cache. + let receive_reduction = |lod: &mut Lod| { + let Done::Detail(key, columns) = lod + .results + .recv_timeout(std::time::Duration::from_secs(5)) + .unwrap() + else { + panic!("expected queued visited reduction"); + }; + lod.finish_detail(key, columns); + }; + receive_reduction(&mut lod); + assert!(Arc::ptr_eq( + &lod.reduced[&(-1, -1)].columns, + &lod.saved[&(-1, -1)] + )); + assert!(lod.visited_column(-16, -16, 2).unwrap().spans.is_empty()); + let old = lod.saved[&(-1, -1)].clone(); + chunk.set(0, 130, 0, stone.0); + lod.observe(&chunk, &facts); + assert!(!lod.reduced.contains_key(&(-1, -1))); + lod.finish_detail((-1, -1), ReducedColumns::new(old)); + assert!(!lod.reduced.contains_key(&(-1, -1))); + assert_eq!(lod.ready[&fine][0].surface.unwrap().height, 131); + receive_reduction(&mut lod); + assert!(Arc::ptr_eq( + &lod.reduced[&(-1, -1)].columns, + &lod.saved[&(-1, -1)] + )); + + // Exercise the actual worker/update seam: a parent stays displayed + // until all four child meshes can replace it in the same update. + let camera = [-256.0, 100.0, -256.0]; + lod.area = Some(([-16, -16], 2, 64, 0)); + lod.plan.roots = vec![key]; + lod.plan.nodes.insert(key); + let await_meshes = |lod: &mut Lod| { + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); + loop { + let meshes = lod.update(camera, 0.0, 2, 64, lod.covered.clone()); + if !meshes.is_empty() { + return meshes; + } + assert!(std::time::Instant::now() < deadline, "mesh worker stalled"); + std::thread::sleep(std::time::Duration::from_millis(1)); + } + }; + let uploaded = await_meshes(&mut lod); + assert_eq!(uploaded.len(), 1); + assert_eq!(uploaded[0].0, key); + assert!(lod.active.contains(&key)); + lod.plan.split.insert(key); + for child in key.children() { + lod.plan.nodes.insert(child); + lod.ready.insert(child, lod.ready[&key].clone()); + } + assert!( + lod.update(camera, 0.0, 2, 64, lod.covered.clone()) + .is_empty() + ); + assert!( + lod.active.contains(&key), + "parent retired before child meshes" + ); + assert!(lod.removed().is_empty()); + let uploaded = await_meshes(&mut lod); + assert_eq!(uploaded.len(), 4); + assert!( + key.children() + .iter() + .all(|child| lod.active.contains(child)) + ); + assert_eq!(lod.removed(), [key]); + + let covered_fine = TileKey { + x: -1, + z: -2, + level: 0, + }; + lod.plan = Plan::default(); + lod.plan.roots.push(covered_fine); + lod.plan.nodes.insert(covered_fine); + lod.ready.insert(covered_fine, vec![Column::default(); 256]); + lod.covered.insert((-1, -2)); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); + loop { + lod.update(camera, 0., 2, 64, lod.covered.clone()); + if !lod.meshing && lod.active.is_empty() { + break; + } + assert!( + std::time::Instant::now() < deadline, + "covered normal chunk remained an active LOD mask" + ); + std::thread::sleep(std::time::Duration::from_millis(1)); + } + assert_eq!(lod.progress(), (1, 1)); + + // Keep one sample outstanding, as on ordinary frames. Completing just + // one job at a time must still let both loading queues make progress. + lod.shared.stop.store(true, Ordering::Relaxed); + lod.shared.wake.notify_all(); + lod.thread.take().unwrap().join().unwrap(); + while lod.results.try_recv().is_ok() {} + lod.shared.jobs.lock().unwrap().clear(); + lod.plan = Plan::new([-256, -256], 2, 64); + let parent = TileKey { + x: -2, + z: -2, + level: 3, + }; + lod.plan.roots = vec![parent]; + lod.ready.clear(); + lod.ready.insert(parent, vec![Column::default(); 256]); + lod.active = [parent].into_iter().collect(); + lod.pending = (0..SAMPLE_JOBS - 1) + .map(|i| TileKey { + x: 999 + i as i32, + z: 999, + level: 0, + }) + .collect(); + lod.sample_nearby = true; + lod.detail_request_pending = true; + lod.meshing = true; + lod.update(camera, 0.0, 2, 64, FxHashSet::default()); + let Some(Job::Sample(nearby)) = lod.shared.jobs.lock().unwrap().pop_front() else { + panic!("nearby detail was not scheduled"); + }; + assert_eq!(nearby.level, 0); + assert!( + nearby.origin()[2] > -256 + 64, + "detail should load ahead of the view" + ); + lod.finish_sample(nearby, vec![Column::default(); 256]); + lod.update(camera, 0.0, 2, 64, FxHashSet::default()); + let Some(Job::Sample(fallback)) = lod.shared.jobs.lock().unwrap().pop_front() else { + panic!("fallback refinement was starved"); + }; + assert!(fallback.level > 0); + lod.pending.remove(&fallback); + lod.sample_nearby = true; + lod.update(camera, 90.0, 2, 64, FxHashSet::default()); + let Some(Job::Sample(turned)) = lod.shared.jobs.lock().unwrap().pop_front() else { + panic!("turned view was not scheduled"); + }; + assert!( + turned.origin()[0] < -256 - 64, + "loading priority should follow a turn" + ); + lod.shared.jobs.lock().unwrap().clear(); + let near = TileKey { + x: -4, + z: -4, + level: 2, + }; + let far = TileKey { + x: 0, + z: 0, + level: 6, + }; + lod.plan = Plan::default(); + lod.plan.roots = vec![near, far]; + lod.ready.clear(); + for parent in [near, far] { + lod.plan.nodes.insert(parent); + lod.plan.split.insert(parent); + lod.plan.nodes.extend(parent.children()); + lod.ready.insert(parent, vec![Column::default(); 256]); + } + lod.active = [near, far].into_iter().collect(); + lod.shared.jobs.lock().unwrap().clear(); + lod.pending = (0..SAMPLE_JOBS - 1) + .map(|i| TileKey { + x: 999 + i as i32, + z: 999, + level: 0, + }) + .collect(); + lod.sample_nearby = false; + lod.update(camera, 0., 2, 64, FxHashSet::default()); + let Some(Job::Sample(broad)) = lod.shared.jobs.lock().unwrap().pop_front() else { + panic!("projected-error refinement missing") + }; + assert_eq!( + broad.level, 5, + "a near tile touching the camera must not starve a huge visible distant tile" + ); + lod.shared.jobs.lock().unwrap().clear(); + lod.active = (0..40).map(|x| TileKey { x, z: 0, level: 0 }).collect(); + lod.plan.split.clear(); + lod.detail_masks.clear(); + lod.covered = [(0, 0)].into_iter().collect(); + lod.detail_request_pending = false; + lod.detail_request_at = std::time::Instant::now() - std::time::Duration::from_secs(1); + lod.request_detail([0, 0]); + let Some(Job::RequestDetail { chunks, .. }) = lod.shared.jobs.lock().unwrap().pop_front() + else { + panic!("detail batch was not requested"); + }; + assert_eq!( + chunks, + (1..=32).map(|x| (x, 0)).collect::<Vec<_>>(), + "one unfinished fine tile must not starve neighbouring requests" + ); + Ok(()) + } +} diff --git a/crates/horizon-client/src/lod/mesh.rs b/crates/horizon-client/src/lod/mesh.rs new file mode 100644 index 000000000..a991a4a8e --- /dev/null +++ b/crates/horizon-client/src/lod/mesh.rs @@ -0,0 +1,1178 @@ +//! Exposed voxel-column faces, merged without changing their block-scale UVs. +use super::{Column, Span, TileKey}; +use crate::{ + fast_hash::{FxHashMap, FxHashSet}, + mesher::{FaceGroups, OPAQUE_LAYER, SectionMesh, TerrainVertex}, + streaming::WorkerContext, +}; +use horizon_assets::model::Direction; +use horizon_core::position::BlockPos; +use horizon_world::registry::BlockStateId; +use std::collections::BTreeMap; + +const CELLS: usize = 16; + +pub(super) struct EdgeColumn { + pub start: i32, + pub end: i32, + pub column: Column, +} + +#[derive(Clone, Copy)] +enum Tint { + None, + Grass, + Foliage, + Dry, + Water, + Constant(u32), +} +#[derive(Clone, Copy)] +struct Style { + layer: u16, + tint: Tint, + translucent: bool, +} +#[derive(Clone, Copy)] +struct Styles { + faces: [Option<Style>; 2], + fluid: u8, + opaque: bool, +} +#[derive(Clone, Copy)] +struct Paint { + layer: u16, + light: u16, + color: u32, + translucent: bool, +} +#[derive(Clone, Copy)] +struct Material { + faces: [Option<Paint>; 2], + fluid: u8, + opaque: bool, +} +#[derive(Default)] +pub(super) struct Mesher { + styles: FxHashMap<(BlockStateId, u8), Styles>, +} +impl Mesher { + pub(super) fn mesh( + &mut self, + key: TileKey, + columns: &[Column], + covered: &FxHashSet<(i32, i32)>, + covered_bounds: Option<[i32; 4]>, + context: &WorkerContext, + neighbors: &[Vec<EdgeColumn>; 4], + finer: &[TileKey], + ) -> SectionMesh { + mesh_columns_with_edges( + key, + columns, + covered, + covered_bounds, + neighbors, + finer, + |span, direction| self.material(span, direction, context), + ) + } + fn material(&mut self, span: &Span, direction: Direction, context: &WorkerContext) -> Material { + let styles = *self + .styles + .entry((span.state, direction as u8)) + .or_insert_with(|| { + let assets = &context.assets; + let name = assets + .registry + .state(span.state) + .map_or("", |s| s.name.as_str()); + let fluid = match name { + "minecraft:water" | "minecraft:bubble_column" => 1, + "minecraft:lava" => 2, + _ => 0, + }; + if fluid != 0 { + let sprite = if fluid == 1 { + "block/water_still" + } else { + "block/lava_still" + }; + let render = &context.render; + let layer = render + .sprite_layers + .get(sprite) + .or_else(|| render.sprite_layers.get(&format!("minecraft:{sprite}"))) + .copied() + .unwrap_or(0) as u16; + return Styles { + faces: [ + Some(Style { + layer, + tint: if fluid == 1 { Tint::Water } else { Tint::None }, + translucent: fluid == 1, + }), + None, + ], + fluid, + opaque: fluid == 2, + }; + } + let leaves = name.ends_with("_leaves"); + let tint = match name.strip_prefix("minecraft:").unwrap_or(name) { + "spruce_leaves" => Tint::Constant(0x61_9961), + "birch_leaves" => Tint::Constant(0x80_A755), + "leaf_litter" => Tint::Dry, + _ if leaves => Tint::Foliage, + _ => Tint::Grass, + }; + let models = context + .render + .model_catalog + .collect(span.state, BlockPos::new(0, 0, 0)) + .ok(); + let opaque = leaves + || models + .iter() + .flatten() + .flat_map(|(_, model)| &model.faces) + .any(|face| !face.translucent); + let mut matching = models + .iter() + .flatten() + .flat_map(|(_, model)| &model.faces) + .filter(|face| face.quad.facing == direction); + let mut faces = [None; 2]; + for slot in &mut faces { + let Some(face) = matching.next() else { break }; + *slot = Some(Style { + layer: face.layer as u16 | if leaves { OPAQUE_LAYER } else { 0 }, + tint: if face.quad.tint_index >= 0 { + tint + } else { + Tint::None + }, + translucent: face.translucent && !leaves, + }); + // Grass's tinted side overlay must follow its untinted base. + if name != "minecraft:grass_block" + || matches!(direction, Direction::Up | Direction::Down) + { + break; + } + } + Styles { + opaque, + faces, + fluid, + } + }); + let biome = context.biomes.get(span.biome); + let shade = match direction { + Direction::Up => 1.0, + Direction::Down => 0.5, + Direction::North | Direction::South => 0.8, + Direction::West | Direction::East => 0.6, + }; + Material { + faces: styles.faces.map(|face| { + face.map(|face| { + let rgb = match face.tint { + Tint::None => 0xffffff, + Tint::Grass => biome.grass, + Tint::Foliage => biome.foliage, + Tint::Dry => biome.dry_foliage, + Tint::Water => biome.water, + Tint::Constant(color) => color, + }; + let channel = |shift: u32| (((rgb >> shift) & 255u32) as f32 * shade) as u32; + Paint { + layer: face.layer, + light: span.light, + color: channel(16) | channel(8) << 8 | channel(0) << 16 | 0xff000000, + translucent: face.translucent, + } + }) + }), + fluid: styles.fluid, + opaque: styles.opaque, + } + } +} + +struct Segment<'a> { + span: &'a Span, + min: f32, + max: f32, + material: Material, + top: bool, + bottom: bool, +} +fn hides(neighbor: &Span, material: Material, current: &Span, current_material: Material) -> bool { + material.opaque + || neighbor.state == current.state + || (material.fluid != 0 && material.fluid == current_material.fluid) +} + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +struct FaceKey { + direction: Direction, + plane: u32, + overlay: u8, + layer: u16, + light: u16, + color: u32, + translucent: bool, +} +#[derive(Clone, Copy, Debug, PartialEq)] +struct Rect { + u0: f32, + v0: f32, + u1: f32, + v1: f32, +} +type Faces = BTreeMap<FaceKey, Vec<Rect>>; +fn add_face(faces: &mut Faces, direction: Direction, plane: f32, rect: Rect, material: Material) { + for (overlay, paint) in material.faces.into_iter().enumerate() { + let Some(paint) = paint else { continue }; + faces + .entry(FaceKey { + direction, + plane: plane.to_bits(), + overlay: overlay as u8, + layer: paint.layer, + light: paint.light, + color: paint.color, + translucent: paint.translucent, + }) + .or_default() + .push(rect); + } +} + +#[cfg(test)] +fn mesh_columns( + key: TileKey, + columns: &[Column], + covered: &FxHashSet<(i32, i32)>, + covered_bounds: Option<[i32; 4]>, + material: impl FnMut(&Span, Direction) -> Material, +) -> SectionMesh { + mesh_columns_with_neighbors(key, columns, covered, covered_bounds, &[], material) +} +#[cfg(test)] +fn mesh_columns_with_neighbors( + key: TileKey, + columns: &[Column], + covered: &FxHashSet<(i32, i32)>, + covered_bounds: Option<[i32; 4]>, + neighbors: &[Column], + material: impl FnMut(&Span, Direction) -> Material, +) -> SectionMesh { + assert!(neighbors.is_empty() || neighbors.len() == 4 * CELLS); + let stride = key.size() / CELLS as i32; + let edges = std::array::from_fn(|side| { + neighbors + .get(side * CELLS..(side + 1) * CELLS) + .unwrap_or(&[]) + .iter() + .enumerate() + .map(|(row, column)| EdgeColumn { + start: row as i32 * stride, + end: (row as i32 + 1) * stride, + column: column.clone(), + }) + .collect() + }); + mesh_columns_with_edges(key, columns, covered, covered_bounds, &edges, &[], material) +} +fn mesh_columns_with_edges( + key: TileKey, + columns: &[Column], + covered: &FxHashSet<(i32, i32)>, + covered_bounds: Option<[i32; 4]>, + neighbors: &[Vec<EdgeColumn>; 4], + finer: &[TileKey], + mut material: impl FnMut(&Span, Direction) -> Material, +) -> SectionMesh { + assert_eq!(columns.len(), CELLS * CELLS); + let mut count = CELLS * CELLS; + let edge_offsets = std::array::from_fn::<_, 4, _>(|side| { + let start = count; + count += neighbors[side].len(); + start + }); + let prepared: Vec<Vec<Segment<'_>>> = columns + .iter() + .chain(neighbors.iter().flatten().map(|edge| &edge.column)) + .map(|column| { + let materials: Vec<_> = column + .spans + .iter() + .map(|s| material(s, Direction::Up)) + .collect(); + column + .spans + .iter() + .enumerate() + .filter(|(_, span)| span.min < span.max) + .map(|(i, span)| { + let current = materials[i]; + let top = !column.spans.get(i + 1).is_some_and(|next| { + next.min == span.max && hides(next, materials[i + 1], span, current) + }); + let bottom = i == 0 + || !(column.spans[i - 1].max == span.min + && hides(&column.spans[i - 1], materials[i - 1], span, current)); + Segment { + span, + min: f32::from(span.min), + max: f32::from(span.max) + - if current.fluid != 0 && top { + 1.0 / 9.0 + } else { + 0.0 + }, + material: current, + top, + bottom, + } + }) + .collect() + }) + .collect(); + let mut faces = Faces::new(); + let stride = key.size() / CELLS as i32; + let [ox, _, oz] = key.origin(); + let finer_keys: FxHashSet<_> = finer.iter().copied().collect(); + let mut finer_levels: Vec<_> = finer.iter().map(|key| key.level).collect(); + finer_levels.sort_unstable(); + finer_levels.dedup(); + let is_covered = |x: i32, z: i32| { + covered.contains(&(x.div_euclid(16), z.div_euclid(16))) + || finer_levels.iter().any(|&level| { + let size = 16 << level; + finer_keys.contains(&TileKey { + x: x.div_euclid(size), + z: z.div_euclid(size), + level, + }) + }) + }; + for z in 0..CELLS { + for x in 0..CELLS { + let (wx, wz) = (ox + x as i32 * stride, oz + z as i32 * stride); + let overlaps = covered_bounds.is_some_and(|[x0, z0, x1, z1]| { + wx < x1 && wx + stride > x0 && wz < z1 && wz + stride > z0 + }); + let mut step = if overlaps { stride.min(16) } else { stride }; + for other in finer { + let [x, _, z] = other.origin(); + if wx < x + other.size() + && x < wx + stride + && wz < z + other.size() + && z < wz + stride + { + step = step.min(other.size()); + } + } + let column = &prepared[z * CELLS + x]; + for dz in (0..stride).step_by(step as usize) { + for dx in (0..stride).step_by(step as usize) { + if is_covered(wx + dx, wz + dz) { + continue; + } + let (x0, z0) = ( + (x as i32 * stride + dx) as f32, + (z as i32 * stride + dz) as f32, + ); + let (x1, z1) = (x0 + step as f32, z0 + step as f32); + for segment in column { + if segment.top { + add_face( + &mut faces, + Direction::Up, + segment.max, + Rect { + u0: x0, + v0: z0, + u1: x1, + v1: z1, + }, + segment.material, + ); + } + if segment.bottom { + add_face( + &mut faces, + Direction::Down, + segment.min, + Rect { + u0: x0, + v0: z0, + u1: x1, + v1: z1, + }, + material(segment.span, Direction::Down), + ); + } + for (direction, nx, nz, edge, nw, plane, u0, u1) in [ + ( + Direction::West, + x as i32 - 1, + z as i32, + dx == 0, + [wx + dx - 1, wz + dz], + x0, + z0, + z1, + ), + ( + Direction::East, + x as i32 + 1, + z as i32, + dx + step == stride, + [wx + dx + step, wz + dz], + x1, + z0, + z1, + ), + ( + Direction::North, + x as i32, + z as i32 - 1, + dz == 0, + [wx + dx, wz + dz - 1], + z0, + x0, + x1, + ), + ( + Direction::South, + x as i32, + z as i32 + 1, + dz + step == stride, + [wx + dx, wz + dz + step], + z1, + x0, + x1, + ), + ] { + let side = material(segment.span, direction); + // Near meshes may have culled against the real neighbor. + // Keep a finite seal where their full-detail area begins. + if is_covered(nw[0], nw[1]) { + add_side(&mut faces, direction, plane, u0, u1, segment, &[], side); + } else if !edge { + add_side( + &mut faces, direction, plane, u0, u1, segment, column, side, + ); + } else if (0..CELLS as i32).contains(&nx) + && (0..CELLS as i32).contains(&nz) + { + add_side( + &mut faces, + direction, + plane, + u0, + u1, + segment, + &prepared[nz as usize * CELLS + nx as usize], + side, + ); + } else { + let edge_side = match direction { + Direction::West => 0, + Direction::East => 1, + Direction::North => 2, + Direction::South => 3, + _ => unreachable!(), + }; + if neighbors[edge_side].is_empty() { + add_side( + &mut faces, + direction, + plane, + u0, + u1, + segment, + &[], + side, + ); + } + // A coarse face can meet several finer columns. + // Clip each interval independently to avoid missing walls. + for (i, edge) in neighbors[edge_side].iter().enumerate() { + let start = u0.max(edge.start as f32); + let end = u1.min(edge.end as f32); + if start < end { + add_side( + &mut faces, + direction, + plane, + start, + end, + segment, + &prepared[edge_offsets[edge_side] + i], + side, + ); + } + } + } + } + } + } + } + } + } + let mut mesh = SectionMesh::default(); + for (key, mut rects) in faces { + merge(&mut rects); + for rect in rects { + emit(&mut mesh, key, rect) + } + } + mesh.faces = FaceGroups::group(&mut mesh.solid); + mesh +} + +#[cfg(test)] +mod refinement_tests { + use super::*; + #[test] + fn fallback_mesh_leaves_exactly_the_fine_tiles_footprints_empty() { + let parent = TileKey { + x: -1, + z: -1, + level: 5, + }; + // Smaller than one coarse cell, away from the parent's outer edges. + let fine = TileKey { + x: -17, + z: -17, + level: 0, + }; + let columns = vec![ + Column { + surface: None, + spans: vec![Span { + min: 0, + max: 8, + state: BlockStateId(1), + biome: 0, + light: 240, + }] + }; + 256 + ]; + let mesh = mesh_columns_with_edges( + parent, + &columns, + &FxHashSet::default(), + None, + &std::array::from_fn(|_| Vec::new()), + &[fine], + |_, _| Material { + faces: [ + Some(Paint { + layer: 1, + light: 240, + color: u32::MAX, + translucent: false, + }), + None, + ], + fluid: 0, + opaque: true, + }, + ); + let [ox, _, oz] = parent.origin(); + let [fx, _, fz] = fine.origin(); + let mut area = 0.0; + for quad in mesh.solid.as_chunks::<4>().0 { + if !quad.iter().all(|v| v.pos[1] == 8.0) { + continue; + } + let a = glam::Vec3::from(quad[0].pos); + let b = glam::Vec3::from(quad[1].pos); + let c = glam::Vec3::from(quad[2].pos); + let normal = (b - a).cross(c - a); + area += normal.y; + let min_x = quad.iter().map(|v| v.pos[0]).fold(f32::INFINITY, f32::min) + ox as f32; + let max_x = quad + .iter() + .map(|v| v.pos[0]) + .fold(f32::NEG_INFINITY, f32::max) + + ox as f32; + let min_z = quad.iter().map(|v| v.pos[2]).fold(f32::INFINITY, f32::min) + oz as f32; + let max_z = quad + .iter() + .map(|v| v.pos[2]) + .fold(f32::NEG_INFINITY, f32::max) + + oz as f32; + assert!( + !(min_x < (fx + 16) as f32 + && max_x > fx as f32 + && min_z < (fz + 16) as f32 + && max_z > fz as f32) + ); + } + assert_eq!(area, (parent.size() * parent.size() - 256) as f32); + } +} + +fn add_side( + faces: &mut Faces, + direction: Direction, + plane: f32, + u0: f32, + u1: f32, + segment: &Segment<'_>, + neighbor: &[Segment<'_>], + side: Material, +) { + let mut floor = segment.min; + for next in neighbor { + if next.max <= floor + || next.min >= segment.max + || !hides(next.span, next.material, segment.span, segment.material) + { + continue; + } + if floor < next.min { + add_face( + faces, + direction, + plane, + Rect { + u0, + v0: floor, + u1, + v1: next.min.min(segment.max), + }, + side, + ); + } + floor = floor.max(next.max); + if floor >= segment.max { + break; + } + } + if floor < segment.max { + add_face( + faces, + direction, + plane, + Rect { + u0, + v0: floor, + u1, + v1: segment.max, + }, + side, + ); + } +} + +fn merge(rects: &mut Vec<Rect>) { + rects.sort_unstable_by(|a, b| { + a.v0.total_cmp(&b.v0) + .then(a.v1.total_cmp(&b.v1)) + .then(a.u0.total_cmp(&b.u0)) + .then(a.u1.total_cmp(&b.u1)) + }); + let mut end = 0; + for i in 0..rects.len() { + let rect = rects[i]; + if end > 0 + && rects[end - 1].v0 == rect.v0 + && rects[end - 1].v1 == rect.v1 + && rects[end - 1].u1 == rect.u0 + { + rects[end - 1].u1 = rect.u1; + } else { + rects[end] = rect; + end += 1 + } + } + rects.truncate(end); + rects.sort_unstable_by(|a, b| { + a.u0.total_cmp(&b.u0) + .then(a.u1.total_cmp(&b.u1)) + .then(a.v0.total_cmp(&b.v0)) + .then(a.v1.total_cmp(&b.v1)) + }); + end = 0; + for i in 0..rects.len() { + let rect = rects[i]; + if end > 0 + && rects[end - 1].u0 == rect.u0 + && rects[end - 1].u1 == rect.u1 + && rects[end - 1].v1 == rect.v0 + { + rects[end - 1].v1 = rect.v1; + } else { + rects[end] = rect; + end += 1 + } + } + rects.truncate(end); +} +/// The widest face `emit` writes, in blocks: its UVs, counted from the face's +/// corner, must stay within `TerrainVertex::tiled`'s range. +const MAX_FACE: f32 = 1024.0; +fn emit(mesh: &mut SectionMesh, key: FaceKey, rect: Rect) { + let mut u0 = rect.u0; + while u0 < rect.u1 { + let u1 = rect.u1.min(u0 + MAX_FACE); + let mut v0 = rect.v0; + while v0 < rect.v1 { + let v1 = rect.v1.min(v0 + MAX_FACE); + emit_quad(mesh, key, Rect { u0, v0, u1, v1 }); + v0 = v1; + } + u0 = u1; + } +} +fn emit_quad(mesh: &mut SectionMesh, key: FaceKey, rect: Rect) { + let Rect { u0, v0, u1, v1 } = rect; + let p = f32::from_bits(key.plane); + let mut points = match key.direction { + Direction::Up | Direction::Down => [[u0, p, v1], [u1, p, v1], [u1, p, v0], [u0, p, v0]], + Direction::North | Direction::South => [[u0, v1, p], [u1, v1, p], [u1, v0, p], [u0, v0, p]], + Direction::West | Direction::East => [[p, v1, u1], [p, v1, u0], [p, v0, u0], [p, v0, u1]], + }; + if matches!( + key.direction, + Direction::Down | Direction::South | Direction::East + ) { + points.reverse() + } + let out = if key.translucent { + &mut mesh.translucent + } else { + &mut mesh.solid + }; + // Tile origins are integer multiples of a block, so local coordinates + // retain the world texture phase without distant-coordinate precision loss; + // a whole number of blocks off each UV keeps it (the sampler repeats) and + // counts it from the face's corner. + let uvs = points.map(|pos| match key.direction { + Direction::Up | Direction::Down => [pos[0], -pos[2]], + Direction::North | Direction::South => [pos[0], -pos[1]], + Direction::West | Direction::East => [pos[2], -pos[1]], + }); + let corner = [0, 1].map(|i| { + uvs.iter() + .map(|uv| uv[i]) + .fold(f32::INFINITY, f32::min) + .floor() + }); + out.extend(points.into_iter().zip(uvs).map(|(pos, uv)| { + TerrainVertex::tiled( + pos, + [uv[0] - corner[0], uv[1] - corner[1]], + key.layer, + key.color, + key.light, + ) + })); +} + +#[cfg(test)] +mod tests { + use super::*; + fn span(min: i16, max: i16, state: u32) -> Span { + Span { + min, + max, + state: BlockStateId(state), + biome: 0, + light: 240, + } + } + fn columns() -> Vec<Column> { + (0..CELLS * CELLS) + .map(|_| Column { + surface: None, + spans: Vec::new(), + }) + .collect() + } + fn material(span: &Span, _: Direction) -> Material { + let water = span.state.0 == 2; + Material { + faces: [ + Some(Paint { + layer: span.state.0 as u16, + light: span.light, + color: u32::MAX, + translucent: water, + }), + None, + ], + fluid: u8::from(water), + opaque: !water, + } + } + fn mesh(columns: &[Column]) -> SectionMesh { + mesh_columns( + TileKey { + x: 0, + z: 0, + level: 0, + }, + columns, + &FxHashSet::default(), + None, + material, + ) + } + fn tiled_uv(v: &TerrainVertex) -> [f32; 2] { + v.uv.map(|u| f32::from(u) / crate::mesher::TILED_UV_STEPS) + } + #[test] + fn wide_faces_split_and_keep_their_texture_phase() { + let mut mesh = SectionMesh::default(); + let key = FaceKey { + direction: Direction::Up, + plane: 64.0f32.to_bits(), + overlay: 0, + layer: 1, + light: 0, + color: 0, + translucent: false, + }; + emit( + &mut mesh, + key, + Rect { + u0: 0.0, + v0: 0.5, + u1: 2500.0, + v1: 16.0, + }, + ); + // 2500 blocks: 1024 + 1024 + 452. + assert_eq!(mesh.solid.len(), 3 * 4); + let mut area = 0.0; + for q in mesh.solid.as_chunks::<4>().0 { + let x = + |f: fn(f32, f32) -> f32, i: usize| q.iter().map(|v| v.pos[i]).reduce(f).unwrap(); + area += (x(f32::max, 0) - x(f32::min, 0)) * (x(f32::max, 2) - x(f32::min, 2)); + for v in q { + // UVs follow the world position modulo a block. + let uv = tiled_uv(v); + assert_eq!((uv[0] - v.pos[0]).rem_euclid(1.0), 0.0); + assert_eq!((uv[1] + v.pos[2]).rem_euclid(1.0), 0.0); + assert!(uv[0] <= MAX_FACE + 1.0 && uv[1] <= MAX_FACE + 1.0); + } + } + assert_eq!(area, 2500.0 * 15.5); + } + fn normal(q: &[TerrainVertex; 4]) -> glam::Vec3 { + let p = |i: usize| glam::Vec3::from(q[i].pos); + (p(1) - p(0)).cross(p(2) - p(0)) + } + #[test] + fn flat_solid_cube_merges_to_six_faces_with_block_scaled_uvs() { + let mut columns = columns(); + for column in &mut columns { + column.spans = vec![span(0, 16, 1)] + } + let mesh = mesh(&columns); + assert_eq!(mesh.solid.len(), 6 * 4); + assert_eq!(mesh.faces.quads.iter().sum::<u32>(), 6); + assert!(mesh.translucent.is_empty()); + for q in mesh.solid.as_chunks::<4>().0.iter() { + let center = q + .iter() + .map(|v| glam::Vec3::from(v.pos)) + .sum::<glam::Vec3>() + / 4.0; + assert!(normal(q).dot(center - glam::Vec3::splat(8.0)) > 0.0); + assert_eq!( + q.iter() + .map(|v| tiled_uv(v)[0]) + .fold(f32::NEG_INFINITY, f32::max) + - q.iter() + .map(|v| tiled_uv(v)[0]) + .fold(f32::INFINITY, f32::min), + 16.0 + ); + assert_eq!( + q.iter() + .map(|v| tiled_uv(v)[1]) + .fold(f32::NEG_INFINITY, f32::max) + - q.iter() + .map(|v| tiled_uv(v)[1]) + .fold(f32::INFINITY, f32::min), + 16.0 + ); + } + } + #[test] + fn gaps_and_real_bottoms_remain_open_without_floor_curtains() { + let mut columns = columns(); + columns[0].spans = vec![span(3, 5, 1), span(8, 9, 1)]; + let mesh = mesh(&columns); + assert_eq!(mesh.solid.len(), 12 * 4); + assert!( + mesh.solid + .iter() + .all(|v| (3.0..=5.0).contains(&v.pos[1]) || (8.0..=9.0).contains(&v.pos[1])) + ); + assert!( + mesh.solid + .as_chunks::<4>() + .0 + .iter() + .any(|q| q.iter().all(|v| v.pos[1] == 8.0) && normal(q).y < 0.0) + ); + } + #[test] + fn neighbor_hides_only_its_occupied_side_intervals() { + let mut columns = columns(); + columns[0].spans = vec![span(0, 10, 1)]; + columns[1].spans = vec![span(0, 3, 1), span(7, 10, 1)]; + let mesh = mesh(&columns); + let opening: Vec<_> = mesh + .solid + .as_chunks::<4>() + .0 + .iter() + .filter(|q| q.iter().all(|v| v.pos[0] == 1.0) && normal(q).x > 0.0) + .collect(); + assert_eq!(opening.len(), 1); + assert_eq!( + opening[0] + .iter() + .map(|v| v.pos[1]) + .fold(f32::INFINITY, f32::min), + 3.0 + ); + assert_eq!( + opening[0] + .iter() + .map(|v| v.pos[1]) + .fold(f32::NEG_INFINITY, f32::max), + 7.0 + ); + } + #[test] + fn greedy_faces_preserve_material_and_light_boundaries() { + let mut columns = columns(); + columns[0].spans = vec![span(0, 1, 1)]; + columns[1].spans = vec![span(0, 1, 3)]; + columns[2].spans = vec![Span { + light: 16, + ..span(0, 1, 3) + }]; + let mesh = mesh(&columns); + let tops: Vec<_> = mesh + .solid + .as_chunks::<4>() + .0 + .iter() + .filter(|q| normal(q).y > 0.0) + .collect(); + assert_eq!(tops.len(), 3); + assert_eq!(tops.iter().filter(|q| q[0].layer == 3).count(), 2); + assert_eq!(tops.iter().filter(|q| q[0].light == 16).count(), 1); + assert!(!mesh.solid.as_chunks::<4>().0.iter().any(|q| { + normal(q).x != 0.0 && q.iter().all(|v| v.pos[0] == 1.0 || v.pos[0] == 2.0) + })); + } + #[test] + fn water_keeps_ground_surface_and_culls_shared_fluid_faces() { + let mut columns = columns(); + columns[0].spans = vec![span(0, 3, 1), span(3, 6, 2)]; + columns[1].spans = columns[0].spans.clone(); + let mesh = mesh(&columns); + assert!( + mesh.solid + .as_chunks::<4>() + .0 + .iter() + .any(|q| q.iter().all(|v| v.pos[1] == 3.0) && normal(q).y > 0.0) + ); + assert!( + mesh.translucent + .as_chunks::<4>() + .0 + .iter() + .all(|q| !(q.iter().all(|v| v.pos[0] == 1.0) && normal(q).x != 0.0)) + ); + assert!(mesh.translucent.as_chunks::<4>().0.iter().any(|q| { + q.iter() + .all(|v| (v.pos[1] - (6.0 - 1.0 / 9.0)).abs() < 0.001) + && normal(q).y > 0.0 + })); + } + #[test] + fn coarse_edge_clips_separate_fine_neighbor_heights() { + let mut columns = columns(); + for column in &mut columns { + column.spans = vec![span(0, 16, 1)]; + } + let key = TileKey { + x: -1, + z: -1, + level: 1, + }; + let mut neighbors: [Vec<EdgeColumn>; 4] = std::array::from_fn(|_| { + vec![EdgeColumn { + start: 0, + end: 32, + column: columns[0].clone(), + }] + }); + // One fine column is lower than both the coarse cell and its sibling. + // Sampling only the first neighbor would incorrectly hide this wall. + neighbors[0] = vec![ + EdgeColumn { + start: 0, + end: 1, + column: columns[0].clone(), + }, + EdgeColumn { + start: 1, + end: 2, + column: Column { + surface: None, + spans: vec![span(0, 8, 1)], + }, + }, + EdgeColumn { + start: 2, + end: 32, + column: columns[0].clone(), + }, + ]; + let mesh = mesh_columns_with_edges( + key, + &columns, + &FxHashSet::default(), + None, + &neighbors, + &[], + material, + ); + let walls: Vec<_> = mesh + .solid + .as_chunks::<4>() + .0 + .iter() + .filter(|quad| normal(quad).x < 0.0) + .collect(); + assert_eq!(walls.len(), 1); + let wall = walls[0]; + assert!(wall.iter().all(|v| v.pos[0] == 0.0 + && (1.0..=2.0).contains(&v.pos[2]) + && (8.0..=16.0).contains(&v.pos[1]))); + assert_eq!(normal(wall).length(), 8.0); + } + #[test] + fn tile_halo_culls_shared_walls_but_keeps_steps_and_gaps() { + let mut columns = columns(); + for column in &mut columns { + column.spans = vec![span(0, 16, 1)]; + } + let mut neighbors = columns[..4 * CELLS].to_vec(); + let key = TileKey { + x: 0, + z: 0, + level: 0, + }; + let covered = FxHashSet::default(); + let mesh = mesh_columns_with_neighbors(key, &columns, &covered, None, &neighbors, material); + assert_eq!( + mesh.solid.len(), + 2 * 4, + "only the top and real bottom remain" + ); + assert!( + mesh.solid + .as_chunks::<4>() + .0 + .iter() + .all(|q| normal(q).y != 0.0) + ); + + for column in &mut neighbors[CELLS..2 * CELLS] { + column.spans = vec![span(0, 8, 1)]; + } + for column in &mut neighbors[..CELLS] { + column.spans = vec![span(0, 3, 1), span(7, 16, 1)]; + } + let mesh = mesh_columns_with_neighbors(key, &columns, &covered, None, &neighbors, material); + assert_eq!(mesh.solid.len(), 4 * 4); + let sides: Vec<_> = mesh + .solid + .as_chunks::<4>() + .0 + .iter() + .filter(|q| normal(q).y == 0.0) + .collect(); + assert_eq!(sides.len(), 2); + for q in sides { + let min = q.iter().map(|v| v.pos[1]).fold(f32::INFINITY, f32::min); + let max = q.iter().map(|v| v.pos[1]).fold(f32::NEG_INFINITY, f32::max); + if normal(q).x > 0.0 { + assert_eq!((min, max), (8.0, 16.0)); + } else { + assert!(normal(q).x < 0.0); + assert_eq!((min, max), (3.0, 7.0)); + } + } + } + #[test] + fn negative_parent_clips_chunks_without_internal_piece_walls() { + let mut columns = columns(); + for column in &mut columns { + column.spans = vec![span(10, 12, 1)] + } + let key = TileKey { + x: -1, + z: -1, + level: 5, + }; + let [ox, _, oz] = key.origin(); + let mut covered = FxHashSet::default(); + covered.insert((ox.div_euclid(16), oz.div_euclid(16))); + let mesh = mesh_columns( + key, + &columns, + &covered, + Some([ox, oz, ox + 16, oz + 16]), + material, + ); + assert!(mesh.solid.as_chunks::<4>().0.iter().all(|q| { + if normal(q).y == 0.0 { + return true; + } + let center = q + .iter() + .map(|v| glam::Vec3::from(v.pos)) + .sum::<glam::Vec3>() + / 4.0; + !(center.x < 16.0 && center.z < 16.0) + })); + let cuts: Vec<_> = mesh + .solid + .as_chunks::<4>() + .0 + .iter() + .filter(|q| { + (q.iter().all(|v| v.pos[0] == 16.0) && normal(q).x < 0.0) + || (q.iter().all(|v| v.pos[2] == 16.0) && normal(q).z < 0.0) + }) + .collect(); + assert_eq!(cuts.len(), 2); + assert!( + cuts.iter() + .all(|q| q.iter().all(|v| (10.0..=12.0).contains(&v.pos[1]))), + "cut faces keep finite voxel bounds" + ); + } +} diff --git a/crates/horizon-client/src/logging.rs b/crates/horizon-client/src/logging.rs index c434c8efb..cb7638f26 100644 --- a/crates/horizon-client/src/logging.rs +++ b/crates/horizon-client/src/logging.rs @@ -330,6 +330,14 @@ pub fn write_only(level: log::Level, message: &str) { } } +/// Writes `message` to the log file only, under the thread vanilla names for it (`Server thread` for +/// a login refused on a connection's own thread), as the console line beside it shows. +pub fn write_as(thread: &str, level: log::Level, message: &str) { + if let Some(logger) = LOGGER.get() { + logger.write(thread, level, message); + } +} + /// The last lines written, oldest first (for crash reports). pub fn recent_lines() -> Vec<String> { LOGGER.get().map_or_else(Vec::new, |l| { diff --git a/crates/horizon-client/src/menu.rs b/crates/horizon-client/src/menu.rs index 5e57edcfc..64286b1fe 100644 --- a/crates/horizon-client/src/menu.rs +++ b/crates/horizon-client/src/menu.rs @@ -1668,6 +1668,18 @@ impl Menu { env: &Env, clicked: usize, moved: &ItemStack, + ) -> Result<(), &'static str> { + self.quick_move_all_each(env, clicked, moved, &mut |_, _| {}) + } + + /// `quick_move_all`, calling `each` with the menu and the slot after each + /// QUICK_MOVE (a server's client sends every one of them). + pub fn quick_move_all_each( + &mut self, + env: &Env, + clicked: usize, + moved: &ItemStack, + each: &mut dyn FnMut(&Self, usize), ) -> Result<(), &'static str> { if clicked >= self.slots.len() || !self.can_take_for_pick_all(clicked) { return Ok(()); @@ -1680,6 +1692,7 @@ impl Menu { && self.quick_replace(t, moved, env) { self.click(env, t as i32, 0, Input::QuickMove)?; + each(self, t); } } Ok(()) diff --git a/crates/horizon-client/src/mesher.rs b/crates/horizon-client/src/mesher.rs index 7f1191126..e6cd27f3e 100644 --- a/crates/horizon-client/src/mesher.rs +++ b/crates/horizon-client/src/mesher.rs @@ -7,7 +7,7 @@ mod facing; mod fluid; mod lighting; use crate::{ - assets::Assets, + assets::{Assets, RenderAssets}, biome::Biomes, blocks::{BlockFacts, RenderShape}, chunk::{Chunk, MIN_Y}, @@ -39,16 +39,22 @@ pub struct Vertex { /// RGBA8_SNORM face normal; w = -127 for two-sided lighting, 0 to keep baked/unlit colour. pub normal: [i8; 4], } -/// A section vertex, 28 bytes like vanilla's `DefaultVertexFormat.BLOCK` -/// (position, colour, UV0, UV2) with the sprite's array layer beside the -/// light. Terrain has no normal: `ModelBlockRenderer` bakes the face shade -/// into the colour and `terrain.vsh` only multiplies it by the lightmap. +/// A section vertex: vanilla's `DefaultVertexFormat.BLOCK` (position, colour, +/// UV0, UV2) with the sprite's array layer beside the light, in 24 bytes +/// rather than vanilla's 28: the UV is fixed point, which holds model UVs +/// (multiples of 1/16 and their halves) exactly. The position stays a float: +/// fixed point would move the edges of offset plants and rotated elements by +/// a fraction of a pixel. Terrain has no normal: `ModelBlockRenderer` bakes +/// the face shade into the colour and `terrain.vsh` only multiplies it by the +/// lightmap. #[repr(C)] #[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)] pub struct TerrainVertex { /// Section-relative position. pub pos: [f32; 3], - pub uv: [f32; 2], + /// UV within the sprite, `UV_STEPS` per unit (`TerrainVertex::new`, + /// `terrain.wgsl`'s `terrain_uv`). + pub(crate) uv: [u16; 2], /// Sprite array layer, with `OPAQUE_LAYER` for opaque leaves. pub layer: u16, /// Smooth light, 16x fixed point: sky in the low byte, block in the high one. @@ -56,6 +62,58 @@ pub struct TerrainVertex { /// RGBA8: tint multiplied by directional shade and ambient occlusion. pub color: u32, } +/// 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 { + pos, + uv: uv.map(|v| (v * UV_STEPS).round().clamp(0.0, f32::from(u16::MAX)) as u16), + layer, + light, + 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) + } +} +#[cfg(test)] +mod vertex_tests { + use super::TerrainVertex; + #[test] + fn model_uvs_pack_exactly() { + for i in 0..=32 { + let u = i as f32 / 32.0; + let v = TerrainVertex::new([0.0; 3], [u, 1.0 - u], 0, 0, 0); + assert_eq!(v.uv(), [u, 1.0 - u]); + } + } + #[test] + fn other_uvs_round_within_a_step() { + // A flowing fluid's rotated UV. + let uv = [0.5 + 0.25 * 0.3f32.sin(), 0.5 - 0.25 * 0.3f32.cos()]; + let packed = TerrainVertex::new([0.0; 3], uv, 0, 0, 0).uv(); + for (a, b) in packed.iter().zip(uv) { + assert!((a - b).abs() <= 0.5 / 32768.0, "{a} {b}"); + } + } +} pub struct SectionMesh { pub solid: Vec<TerrainVertex>, /// `solid`'s quads by the side they face (`facing`). @@ -258,18 +316,19 @@ fn redstone_color(power: u8) -> u32 { impl MeshTables { pub fn new( assets: &Assets, + render: &RenderAssets, facts: &BlockFacts, visibility: FaceVisibility, ) -> crate::Result<Self> { // Terrain vertices keep the sprite layer below `OPAQUE_LAYER`. - let layers = assets.sprite_layers.values().max().map_or(0, |l| l + 1); + let layers = render.sprite_layers.values().max().map_or(0, |l| l + 1); if layers > u32::from(OPAQUE_LAYER) { return Err( format!("{layers} block sprites, terrain vertices index {OPAQUE_LAYER}").into(), ); } let layer = |name: &str| -> crate::Result<u16> { - assets + render .sprite_layers .get(name) .map(|&l| l as u16) @@ -434,17 +493,17 @@ fn pi(x: i32, y: i32, z: i32) -> usize { } /// Blocks and light of one section plus a `B`-block border. struct Padded { - blocks: Vec<u16>, + blocks: Vec<u32>, light: Vec<u8>, } impl Padded { fn new(region: &[[Option<&Chunk>; 3]; 3], sy: i32) -> Self { - let mut blocks = vec![0u16; P * P * P]; + let mut blocks = vec![0u32; P * P * P]; let mut light = vec![0u8; P * P * P]; let base_y = MIN_Y + sy * 16; // The section itself is unpacked in one pass; the border cells of the // neighbouring sections are read one by one. - let mut own = Box::new([0u16; 4096]); + let mut own = Box::new([0u32; 4096]); let center = region[1][1].map(|c| &c.sections[sy as usize]); if let Some(section) = center { section.unpack(&mut own); @@ -594,6 +653,7 @@ pub struct Mesher<'a> { /// CardinalLighting.NETHER: brighter up/down faces. pub nether_shading: bool, pub assets: &'a Assets, + pub render: &'a RenderAssets, pub tables: &'a MeshTables, pub biomes: &'a Biomes, pub visibility: FaceVisibility, @@ -643,10 +703,8 @@ impl Mesher<'_> { } let state = &info[id as usize]; if state.block_entity { - out.block_entities.push(( - [center.x * 16 + x, base_y + y, center.z * 16 + z], - u32::from(id), - )); + out.block_entities + .push(([center.x * 16 + x, base_y + y, center.z * 16 + z], id)); } if state.solid && Direction::ALL.iter().all(|d| { @@ -665,10 +723,7 @@ impl Mesher<'_> { continue; } let world = BlockPos::new(center.x * 16 + x, base_y + y, center.z * 16 + z); - let Ok(models) = self - .assets - .model_catalog - .collect(BlockStateId(id.into()), world) + let Ok(models) = self.render.model_catalog.collect(BlockStateId(id), world) else { continue; }; @@ -700,8 +755,8 @@ impl Mesher<'_> { && self .visibility .should_render_face( - BlockStateId(id.into()), - BlockStateId(neighbor.into()), + BlockStateId(id), + BlockStateId(neighbor), d, true, ) @@ -791,18 +846,18 @@ impl Mesher<'_> { None => [g; 3], }; let p = q.positions[i]; - target.push(TerrainVertex { - pos: [p[0] + origin[0], p[1] + origin[1], p[2] + origin[2]], - uv: q.uv[i], + target.push(TerrainVertex::new( + [p[0] + origin[0], p[1] + origin[1], p[2] + origin[2]], + q.uv[i], // Opaque leaves (forceOpaque) draw in the solid layer: no alpha cutout. - layer: if !self.cutout_leaves && state.leaves { + if !self.cutout_leaves && state.leaves { face.layer as u16 | OPAQUE_LAYER } else { face.layer as u16 }, - color: pack_rgb(rgb), - light: pack_coords(lighting::with_emission(coords[i], q.light_emission)), - }); + pack_rgb(rgb), + pack_coords(lighting::with_emission(coords[i], q.light_emission)), + )); } } } diff --git a/crates/horizon-client/src/mesher/facing.rs b/crates/horizon-client/src/mesher/facing.rs index 2a7f0ba61..7bafe9e82 100644 --- a/crates/horizon-client/src/mesher/facing.rs +++ b/crates/horizon-client/src/mesher/facing.rs @@ -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 { @@ -175,13 +180,7 @@ mod tests { use super::*; fn vertex(pos: [f32; 3], layer: u16) -> TerrainVertex { - TerrainVertex { - pos, - uv: [0.0; 2], - layer, - light: 0, - color: 0, - } + TerrainVertex::new(pos, [0.0; 2], layer, 0, 0) } /// A unit quad in the plane `axis = plane`, wound to face `sign`, tagged /// with `layer` so tests can follow it. diff --git a/crates/horizon-client/src/mesher/fluid.rs b/crates/horizon-client/src/mesher/fluid.rs index 41b1e200e..6e42685ba 100644 --- a/crates/horizon-client/src/mesher/fluid.rs +++ b/crates/horizon-client/src/mesher/fluid.rs @@ -159,28 +159,28 @@ struct FlowView<'a> { info: &'a [Info], } impl FlowView<'_> { - fn id(&self, p: BlockPos) -> u16 { + fn id(&self, p: BlockPos) -> u32 { self.pad.blocks[pi(p.x, p.y, p.z)] } } impl FluidFlowWorld for FlowView<'_> { type Error = Infallible; - type Block = u16; + type Block = u32; fn fluid(&mut self, p: BlockPos) -> Result<FluidState, Infallible> { Ok(self.info[self.id(p) as usize].fluid) } - fn block(&mut self, p: BlockPos) -> Result<u16, Infallible> { + fn block(&mut self, p: BlockPos) -> Result<u32, Infallible> { Ok(self.id(p)) } - fn blocks_fluid_flow(&self, state: u16) -> bool { + fn blocks_fluid_flow(&self, state: u32) -> bool { self.info[state as usize].blocks_flow } - fn is_ice_class(&self, state: u16) -> bool { + fn is_ice_class(&self, state: u32) -> bool { self.info[state as usize].ice } fn is_face_sturdy( &mut self, - state: u16, + state: u32, _: BlockPos, d: Direction, ) -> Result<bool, Infallible> { @@ -306,13 +306,7 @@ pub(super) fn tesselate( }; let mut quad = |v: [([f32; 3], [f32; 2]); 4], layer: u16, color: u32, light: u16, back: bool| { - let vertex = |(pos, uv): ([f32; 3], [f32; 2])| TerrainVertex { - pos, - uv, - layer, - color, - light, - }; + let vertex = |(pos, uv)| TerrainVertex::new(pos, uv, layer, color, light); target.extend(v.map(vertex)); if back { target.extend([v[0], v[3], v[2], v[1]].map(vertex)); diff --git a/crates/horizon-client/src/mesher/leaf_culling_tests.rs b/crates/horizon-client/src/mesher/leaf_culling_tests.rs index 16ea98cef..679b19090 100644 --- a/crates/horizon-client/src/mesher/leaf_culling_tests.rs +++ b/crates/horizon-client/src/mesher/leaf_culling_tests.rs @@ -9,16 +9,17 @@ fn leaf_culling_removes_only_internal_leaf_and_root_faces() { return; } let assets = Assets::load(&root).unwrap(); + let render = assets.reload(&root).unwrap(); let facts = BlockFacts::load(&root, assets.registry).unwrap(); let visibility = crate::models::load_visibility(&root, assets.registry).unwrap(); - let tables = MeshTables::new(&assets, &facts, visibility.clone()).unwrap(); + let tables = MeshTables::new(&assets, &render, &facts, visibility.clone()).unwrap(); let biomes = Biomes::load(&root).unwrap(); - let state = |name| assets.registry.default_state(name).unwrap().0 as u16; + let state = |name| assets.registry.default_state(name).unwrap().0; let oak = state("minecraft:oak_leaves"); let birch = state("minecraft:birch_leaves"); let roots = state("minecraft:mangrove_roots"); let stone = state("minecraft:stone"); - let mesh = |blocks: &[([i32; 3], u16)], cull_leaves, cutout_leaves| { + let mesh = |blocks: &[([i32; 3], u32)], cull_leaves, cutout_leaves| { let mut center = Chunk::empty(0, 0); let mut east = Chunk::empty(1, 0); for &([x, y, z], state) in blocks { @@ -31,6 +32,7 @@ fn leaf_culling_removes_only_internal_leaf_and_root_faces() { Mesher { nether_shading: false, assets: &assets, + render: &render, tables: &tables, biomes: &biomes, visibility: visibility.clone(), @@ -49,9 +51,9 @@ fn leaf_culling_removes_only_internal_leaf_and_root_faces() { // preserving every other quad and its full vertex attributes. let without_face = |at: [i32; 3], id, direction| { let vertices = mesh(&[(at, id)], false, true); - let models = assets + let models = render .model_catalog - .collect(BlockStateId(id.into()), BlockPos::new(at[0], at[1], at[2])) + .collect(BlockStateId(id), BlockPos::new(at[0], at[1], at[2])) .unwrap(); let faces: Vec<_> = models.iter().flat_map(|(_, model)| &model.faces).collect(); assert_eq!(vertices.len(), faces.len() * 4); diff --git a/crates/horizon-client/src/mesher/visibility_tests.rs b/crates/horizon-client/src/mesher/visibility_tests.rs index 2f95c45cf..32eb4800c 100644 --- a/crates/horizon-client/src/mesher/visibility_tests.rs +++ b/crates/horizon-client/src/mesher/visibility_tests.rs @@ -105,7 +105,7 @@ fn fixture(mut opaque: impl FnMut(i32, i32, i32) -> bool) -> Padded { for y in 0..16 { for z in 0..16 { for x in 0..16 { - pad.blocks[pi(x, y, z)] = u16::from(opaque(x, y, z)) + 2 * ((x + y + z) as u16 & 1); + pad.blocks[pi(x, y, z)] = u32::from(opaque(x, y, z)) + 2 * ((x + y + z) as u32 & 1); } } } diff --git a/crates/horizon-client/src/minimap.rs b/crates/horizon-client/src/minimap.rs index 8015e2ab2..8b250e1af 100644 --- a/crates/horizon-client/src/minimap.rs +++ b/crates/horizon-client/src/minimap.rs @@ -75,8 +75,8 @@ impl Palette { } } #[inline] - fn get(&self, id: u16) -> (u8, u8) { - let i = usize::from(id); + fn get(&self, id: u32) -> (u8, u8) { + let i = id as usize; ( self.color.get(i).copied().unwrap_or(0), self.flags.get(i).copied().unwrap_or(AIR), @@ -137,7 +137,7 @@ fn scan_column( (0, MIN_Y as i16, 0) } /// The highest block of a chunk worth scanning from. -fn top_of(chunk: &Chunk, air: u16, layer: Layer) -> i32 { +fn top_of(chunk: &Chunk, air: u32, layer: Layer) -> i32 { let surface = (0..SECTIONS) .rev() .find(|&s| { @@ -152,7 +152,7 @@ fn top_of(chunk: &Chunk, air: u16, layer: Layer) -> i32 { Layer::Below(head) => surface.min(head), } } -fn scan_tile(chunk: &Chunk, palette: &Palette, air: u16, layer: Layer, revision: u64) -> Tile { +fn scan_tile(chunk: &Chunk, palette: &Palette, air: u32, layer: Layer, revision: u64) -> Tile { let top = top_of(chunk, air, layer); let mut tile = Tile { color: [0; 256], @@ -287,7 +287,7 @@ impl Minimap { ) -> usize { let palette = self.palette.get_or_insert_with(|| Palette::new(facts)); let started = Instant::now(); - let air = world.air.0 as u16; + let air = world.air.0; // Tiles of unloaded or distant chunks go. self.tiles.retain(|k, _| { (k.0 - center.0).abs() <= radius + 2 @@ -417,7 +417,7 @@ mod tests { (LAVA, 2), ]) } - fn chunk(columns: &[(i32, i32, &[(i32, u16)])]) -> Chunk { + fn chunk(columns: &[(i32, i32, &[(i32, u32)])]) -> Chunk { let mut c = Chunk::empty(0, 0); for &(x, z, blocks) in columns { for &(y, id) in blocks { @@ -467,7 +467,7 @@ mod tests { fn below_a_height_shows_the_floor_and_cuts_walls() { let p = palette(); // A cave: stone everywhere from 0 to 20, air at 10 and 11 in column 0. - let mut blocks: Vec<(i32, u16)> = (0..=20).map(|y| (y, 2)).collect(); + let mut blocks: Vec<(i32, u32)> = (0..=20).map(|y| (y, 2)).collect(); let solid = blocks.clone(); blocks.retain(|&(y, _)| y != 10 && y != 11); let c = chunk(&[(0, 0, &blocks), (1, 0, &solid)]); diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 8b51593bc..c8155984c 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -686,10 +686,16 @@ pub struct Extra { pub legs: Option<Arc<str>>, pub feet: Option<Arc<str>>, pub using_item: i32, + pub using_off_hand: bool, + pub blocking: bool, pub fall_flying: bool, pub fall_fly_ticks: i32, pub main_hand_left: bool, pub elytra: ElytraAnimation, + pub swim: SwimAnimation, + /// The arm the current swing moves (`currentSwing.hand().asArm`): `Some(true)` + /// for the right arm. + pub swing_right_arm: Option<bool>, pub flag: bool, pub flag2: bool, /// Species scratch values (the wither's head targets and clocks). @@ -807,6 +813,8 @@ pub struct Mob { /// Entity.wasEyeInWater: the eyes were in water before this tick's fluid /// update (`isUnderWater` reads it). pub was_eye_in_water: bool, + /// `Entity.firstTick`: no splash for a mob that appears in water. + pub first_tick: bool, pub no_gravity: bool, /// `Entity.mainSupportingBlockPos`: the block it stood on after its last /// move. @@ -1392,6 +1400,9 @@ pub struct PlayerView { /// The mobs on the player's team (`isAlliedTo`): combat targeting /// leaves the player alone. pub allies: std::sync::Arc<std::collections::HashSet<u32>>, + /// A LAN guest: its own client moves it, and touching mobs push it there + /// (`EntitySelector.pushableBy` on the client). + pub remote: bool, } pub struct MobWorld<'a> { pub world: &'a World, @@ -1401,6 +1412,9 @@ pub struct MobWorld<'a> { pub collision: &'a BlockCollisionRegistry<'a>, pub data: &'a MobData, pub player: Option<PlayerView>, + /// `getNearestPlayer(mob, -1)`: where the nearest non-spectator of every + /// player in the level stands, alive or not (the despawn distance). + pub nearest_player: Option<Vec3>, pub others: &'a [Seen], /// `others` by position, when the caller keeps one (the mob ticks). pub others_grid: Option<&'a seen_grid::SeenGrid>, @@ -1484,6 +1498,7 @@ impl Mob { extra: Extra::default(), fluids: living::Fluids::default(), was_eye_in_water: false, + first_tick: true, no_gravity: false, supporting: None, no_ai: false, @@ -2068,6 +2083,35 @@ impl ElytraAnimation { } } +/// `LivingEntity.swimAmount` and the render state the swimming pose reads +/// (`isVisuallySwimming`, `isInWater`). +#[derive(Clone, Copy, Debug, Default)] +pub struct SwimAnimation { + pub old: f32, + pub amount: f32, + /// `isVisuallySwimming`: in the swimming pose (or still in the gliding + /// pose after a glide ended). + pub visually: bool, + pub in_water: bool, +} +impl SwimAnimation { + /// `LivingEntity.updateSwimAmount`. + pub fn tick(&mut self, visually: bool, in_water: bool) { + self.old = self.amount; + self.amount = if visually { + (self.amount + 0.09).min(1.0) + } else { + (self.amount - 0.09).max(0.0) + }; + self.visually = visually; + self.in_water = in_water; + } + /// `LivingEntity.getSwimAmount`. + pub fn amount(&self, partial: f32) -> f32 { + self.old + partial * (self.amount - self.old) + } +} + #[cfg(test)] mod elytra_tests { use super::*; diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 318b37d5c..34276ddda 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -225,6 +225,22 @@ pub fn register(m: &mut Mob) { Family::Bat => super::bat::register(m), Family::Bee => super::bee::register(m), } + // FloatGoal's constructor lets the navigation float, as do the + // constructors of these brain-driven and flying mobs. A floater rises + // 0.04 a tick in Mob.jumpInLiquid, the others 0.3 (the bounce of #542). + if m.ai.goals.has("Float") + || matches!( + m.species(), + Species::Allay + | Species::Armadillo + | Species::Camel + | Species::Goat + | Species::Parrot + | Species::Wither + ) + { + m.ai.nav.can_float = true; + } } /// Mob.tick additions that run before the entity tick (creeper fuse, puffing). fn pre_tick(m: &mut Mob, events: &mut Vec<MobEvent>) { @@ -413,7 +429,7 @@ pub fn tick( true } /// LivingEntity.updateSwingTime (6-tick swing duration). -fn tick_swing(m: &mut Mob) { +pub fn tick_swing(m: &mut Mob) { if m.ai.swinging { m.ai.swing_time += 1; if m.ai.swing_time >= 6 { @@ -428,10 +444,7 @@ fn tick_swing(m: &mut Mob) { /// Entity.baseTick + LivingEntity.baseTick + Mob.baseTick. fn base_tick(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) { m.was_eye_in_water = m.fluids.eye_in_water; - m.fluids = w.fluids(m.bounds(), m.eye_y()); - if m.fluids.in_water { - m.fall_distance = 0.0; - } + m.update_fluids(w, events); let fire = crate::motion::environment::fire_contact(w.world, w.registry, m.bounds()); if fire > 0.0 { m.ticks_frozen = 0; @@ -474,6 +487,7 @@ fn base_tick(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) { if m.position.y < f64::from(crate::chunk::MIN_Y - 64) { m.damage(4.0, Damage::of(DamageKind::OutOfWorld)); } + m.first_tick = false; if m.alive() { // LivingEntity.baseTick: suffocation inside full blocks. let (ex, ey, ez) = ( @@ -808,10 +822,14 @@ fn push_entities(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) { za *= pow; xa *= f64::from(0.05_f32); za *= f64::from(0.05_f32); - events.push(MobEvent::Push { - target: who, - velocity: [-xa, 0.0, -za], - }); + // A guest's server-side push never reaches its client (ServerPlayer + // motion is synced only when hurt): its client pushes it itself. + if who != Who::Player || w.player.as_ref().is_none_or(|p| !p.remote) { + events.push(MobEvent::Push { + target: who, + velocity: [-xa, 0.0, -za], + }); + } if self_pushable { m.velocity = Vec3::new(m.velocity.x + xa, m.velocity.y, m.velocity.z + za); } @@ -1000,22 +1018,21 @@ pub fn despawn_check(m: &mut Mob, w: &MobWorld) -> bool { /// Mob.checkDespawn, before the tick (players are the only despawn /// reference). Returns false when the mob is discarded. fn check_despawn(m: &mut Mob, w: &MobWorld) -> bool { - let Some(p) = &w.player else { return true }; - let d = m.distance_sqr(p.position); + let Some(p) = w.nearest_player else { + return true; + }; + let d = m.distance_sqr(p); // requiresCustomPersistence: raiders in a raid, a tame nautilus, a sulfur // cube holding a block or let out of a bucket. let persistent = m.persistent || super::nautilus::is_nautilus(m.species()) && m.extra.tame || m.species() == Species::SulfurCube && (super::sulfur_cube::has_body(m) || m.extra.scratch[3] != 0); - // removeWhenFarAway: animals, wardens, allays, patrols and raiders, - // zombie villagers being cured and ridden mobs stay; the wither's - // checkDespawn never removes it for distance. + // removeWhenFarAway: animals, villagers, golems, patrols and raiders, + // zombie villagers being cured and ridden mobs stay. let creature = m.kind.category == "creature"; - let removable = !(matches!( - m.species(), - Species::Warden | Species::Allay | Species::Wither - ) || creature && m.kind.flags.animal && !m.extra.jockey + let removable = !(never_removed_when_far(m.species()) + || creature && m.kind.flags.animal && !m.extra.jockey || super::raider::keeps_alive(m, d) || super::zombie_villager::keeps_alive(m) || super::riders::keeps_alive(m)); @@ -1041,6 +1058,24 @@ fn check_despawn(m: &mut Mob, w: &MobWorld) -> bool { } true } +/// The types whose `removeWhenFarAway` is always false (`Villager`, +/// `WanderingTrader`, `AbstractGolem`: iron, snow and copper golems and +/// shulkers; `Allay`, `Warden`), and the wither, whose `checkDespawn` never +/// removes it for distance. +fn never_removed_when_far(s: Species) -> bool { + matches!( + s, + Species::Villager + | Species::WanderingTrader + | Species::IronGolem + | Species::SnowGolem + | Species::CopperGolem + | Species::Shulker + | Species::Allay + | Species::Warden + | Species::Wither + ) +} /// Movement speed bonus multiplier (baby zombies are 50% faster, etc.). pub fn speed_bonus(m: &Mob) -> f64 { match m.species() { @@ -1561,3 +1596,25 @@ mod bucket_axolotl_tests { assert_eq!(family(Species::Axolotl), Family::Aquatic); } } +#[cfg(test)] +mod despawn_tests { + use super::*; + /// Village villagers are placed without PersistenceRequired (unlike the + /// village's iron golem), so they must never despawn for distance. + #[test] + fn villagers_traders_and_golems_never_despawn_for_distance() { + for s in [ + Species::Villager, + Species::WanderingTrader, + Species::IronGolem, + Species::SnowGolem, + Species::CopperGolem, + Species::Shulker, + ] { + assert!(never_removed_when_far(s), "{s:?}"); + } + for s in [Species::Zombie, Species::ZombieVillager, Species::Pillager] { + assert!(!never_removed_when_far(s), "{s:?}"); + } + } +} diff --git a/crates/horizon-client/src/mobs/anim.rs b/crates/horizon-client/src/mobs/anim.rs index 161db24f9..1410bf858 100644 --- a/crates/horizon-client/src/mobs/anim.rs +++ b/crates/horizon-client/src/mobs/anim.rs @@ -4,7 +4,7 @@ use super::{Mob, Part, keyframes::AnimationState, living::wrap}; use horizon_core::geometry::trig::Mth; use std::collections::HashMap; -use std::f32::consts::PI; +use std::f32::consts::{FRAC_PI_6, PI}; /// The setupAnim implementation a model uses. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -232,9 +232,12 @@ fn bob(p: &mut Poses, n: &str, age: f32, scale: f32) { pub enum ArmPose { Empty, Item, + Block, Bow, CrossbowHold, CrossbowCharge, + /// Raising a trident to throw it (or to launch with Riptide). + ThrowTrident, } /// HumanoidModel.setupAnim for piglins: `PiglinModel` poses the arms itself. pub fn humanoid_item(p: &mut Poses, s: &State, _holding: bool) { @@ -276,6 +279,12 @@ pub struct Hum { pub charge: (f32, f32), /// `PiglinModel.setupAttackAnimation`: swing a held weapon down. pub weapon_swing: bool, + /// `swimAmount` at this frame. + pub swim: f32, + /// `isUsingItem`: the swim stroke leaves the arms alone. + pub using_item: bool, + /// The arm the current swing moves (`Some(true)` for the right one). + pub swing_right_arm: Option<bool>, } impl Hum { pub fn new(right: ArmPose, left: ArmPose) -> Self { @@ -290,6 +299,9 @@ impl Hum { holding: false, charge: (0.0, 0.0), weapon_swing: false, + swim: 0.0, + using_item: false, + swing_right_arm: None, } } } @@ -298,7 +310,7 @@ impl ArmPose { matches!(self, Self::Bow | Self::CrossbowHold | Self::CrossbowCharge) } fn affects_offhand(self) -> bool { - self.two_handed() + self.two_handed() || self == Self::ThrowTrident } } /// `AnimationUtils.animateCrossbowHold`. @@ -344,12 +356,25 @@ fn pose_arm(p: &mut Poses, pose: ArmPose, right: bool, h: &Hum) { p.xrot(arm, x * 0.5 - PI / 10.0); p.yrot(arm, 0.0); } + ArmPose::Block => { + let x = p.get(arm)[3] * 0.5 - 0.942_477_9 + hx.clamp(-1.396_263_4, 0.436_332_32); + p.xrot(arm, x); + p.yrot( + arm, + if right { -FRAC_PI_6 } else { FRAC_PI_6 } + hy.clamp(-FRAC_PI_6, FRAC_PI_6), + ); + } ArmPose::Bow => { p.yrot("right_arm", -0.1 + hy - if right { 0.0 } else { 0.4 }); p.yrot("left_arm", 0.1 + hy + if right { 0.4 } else { 0.0 }); p.xrot("right_arm", -PI / 2.0 + hx); p.xrot("left_arm", -PI / 2.0 + hx); } + ArmPose::ThrowTrident => { + let x = p.get(arm)[3]; + p.xrot(arm, x * 0.5 - PI); + p.yrot(arm, 0.0); + } ArmPose::CrossbowHold => crossbow_hold(p, hx, hy, right), ArmPose::CrossbowCharge => crossbow_charge(p, h.charge.0, h.charge.1, right), } @@ -435,12 +460,17 @@ pub(super) fn humanoid(p: &mut Poses, s: &State, h: &Hum) { let xm = ((1.0 - (1.0 - swing) * (1.0 - swing)) * PI).msin(); let x = drop + ym * 1.2 - xm * 0.4; let y = 0.1 - ym * 0.6; - p.xrot("right_arm", x); - p.yrot("right_arm", if raise { -y } else { y }); - p.zrot("right_arm", 0.0); - p.xrot("left_arm", x); - p.yrot("left_arm", if raise { y } else { -y }); - p.zrot("left_arm", 0.0); + // An arm raising a trident keeps its pose. + if h.right != ArmPose::ThrowTrident { + p.xrot("right_arm", x); + p.yrot("right_arm", if raise { -y } else { y }); + p.zrot("right_arm", 0.0); + } + if h.left != ArmPose::ThrowTrident { + p.xrot("left_arm", x); + p.yrot("left_arm", if raise { y } else { -y }); + p.zrot("left_arm", 0.0); + } } if h.crouching { p.xrot("body", 0.5); @@ -455,6 +485,86 @@ pub(super) fn humanoid(p: &mut Poses, s: &State, h: &Hum) { } bob(p, "right_arm", s.age, 1.0); bob(p, "left_arm", s.age, -1.0); + if h.swim > 0.0 { + swim(p, s.pos, h); + } +} +/// `Mth.rotLerpRad`. +fn rot_lerp_rad(a: f32, from: f32, to: f32) -> f32 { + let mut diff = to - from; + while diff < -PI { + diff += PI * 2.0; + } + while diff >= PI { + diff -= PI * 2.0; + } + from + a * diff +} +/// `HumanoidModel.setupSwimAnimation`: the stroke of both arms over 26 +/// walk-animation units, the head down and a slow flutter kick. The left arm +/// eases the short way round, the right one linearly, as in vanilla. +fn swim(p: &mut Poses, pos: f32, h: &Hum) { + let amount = h.swim; + if !h.fall_flying { + let x = p.get("head")[3]; + p.xrot( + "head", + rot_lerp_rad(amount, x, -std::f32::consts::FRAC_PI_4), + ); + } + let swim_pos = pos % 26.0; + let right_amount = if h.swing_right_arm == Some(true) { + 0.0 + } else { + amount + }; + let left_amount = if h.swing_right_arm == Some(false) { + 0.0 + } else { + amount + }; + let lerp = |a: f32, from: f32, to: f32| from + a * (to - from); + let quadratic = |x: f32| -65.0 * x + x * x; + if !h.using_item { + // Targets (x, y, z) for the left and right arm. + let targets = if swim_pos < 14.0 { + let stroke = 1.870_796_4 * quadratic(swim_pos) / quadratic(14.0); + Some(([0.0, PI, PI + stroke], [0.0, PI, PI - stroke])) + } else if swim_pos < 22.0 { + let t = (swim_pos - 14.0) / 8.0; + let x = PI / 2.0 * t; + Some(( + [x, PI, 5.012_389 - 1.870_796_4 * t], + [x, PI, 1.270_796_3 + 1.870_796_4 * t], + )) + } else if swim_pos < 26.0 { + let t = (swim_pos - 22.0) / 4.0; + let x = PI / 2.0 - PI / 2.0 * t; + Some(([x, PI, PI], [x, PI, PI])) + } else { + None + }; + if let Some((left, right)) = targets { + for (i, target) in left.into_iter().enumerate() { + let angle = &mut p.get("left_arm")[3 + i]; + *angle = rot_lerp_rad(left_amount, *angle, target); + } + for (i, target) in right.into_iter().enumerate() { + let angle = &mut p.get("right_arm")[3 + i]; + *angle = lerp(right_amount, *angle, target); + } + } + } + let x = p.get("left_leg")[3]; + p.xrot( + "left_leg", + lerp(amount, x, 0.3 * (pos * 0.333_333_34 + PI).mcos()), + ); + let x = p.get("right_leg")[3]; + p.xrot( + "right_leg", + lerp(amount, x, 0.3 * (pos * 0.333_333_34).mcos()), + ); } /// Apply the mob's model animation to `p`. pub fn setup( @@ -526,7 +636,21 @@ pub fn setup( | Anim::ZombieVillager | Anim::ZombifiedPiglin | Anim::Humanoid { zombie_arms: true } => { - let mut h = Hum::new(ArmPose::Empty, ArmPose::Empty); + // DrownedRenderer.getArmPose: an aggressive drowned raises the + // trident in its main arm. + let throwing = m.kind.anim == Anim::Drowned + && m.ai.aggressive + && m.extra.main_hand.as_deref() == Some("minecraft:trident"); + let main = if throwing { + ArmPose::ThrowTrident + } else { + ArmPose::Empty + }; + let mut h = if m.extra.main_hand_left { + Hum::new(ArmPose::Empty, main) + } else { + Hum::new(main, ArmPose::Empty) + }; h.zombie = Some(m.ai.aggressive); h.holding = m.extra.main_hand.is_some(); h.crouching = m.extra.crouching; @@ -546,10 +670,13 @@ pub fn setup( } else { ArmPose::Empty }; + let using_main = m.extra.using_item > 0 && !m.extra.using_off_hand; let pose = if player { match m.extra.main_hand.as_deref() { - Some("minecraft:bow") if m.extra.using_item > 0 => ArmPose::Bow, - Some("minecraft:crossbow") if m.extra.using_item > 0 && !m.extra.charged => { + Some(_) if using_main && m.extra.blocking => ArmPose::Block, + Some("minecraft:bow") if using_main => ArmPose::Bow, + Some("minecraft:trident") if using_main => ArmPose::ThrowTrident, + Some("minecraft:crossbow") if using_main && !m.extra.charged => { ArmPose::CrossbowCharge } Some("minecraft:crossbow") if m.extra.charged && s.swing == 0.0 => { @@ -561,8 +688,12 @@ pub fn setup( } else { right }; - let off = if player && m.extra.off_hand.is_some() && !pose.two_handed() { - ArmPose::Item + let off = if player && m.extra.off_hand.is_some() && !pose.affects_offhand() { + if m.extra.using_item > 0 && m.extra.using_off_hand && m.extra.blocking { + ArmPose::Block + } else { + ArmPose::Item + } } else { ArmPose::Empty }; @@ -580,6 +711,9 @@ pub fn setup( } h.crouching = m.extra.crouching; h.passenger = m.extra.passenger; + h.swim = m.extra.swim.amount(s.partial); + h.using_item = m.extra.using_item > 0; + h.swing_right_arm = m.extra.swing_right_arm; humanoid(p, s, &h); if m.ai.aggressive && !bow { // SkeletonModel melee pose. @@ -672,6 +806,42 @@ pub fn setup( #[cfg(test)] mod flight_pose_tests { use super::*; + + #[test] + fn blocking_arm_follows_the_head_with_limits_in_both_hands() { + for right in [false, true] { + for (pitch, yaw, expected_pitch, expected_yaw) in [ + (0.0, 0.0, -0.742_477_9, 0.0), + (2.0, 2.0, -0.306_145_58, FRAC_PI_6), + (-2.0, -2.0, -2.138_741_3, -FRAC_PI_6), + ] { + let mut p = Poses { + map: ["head", "right_arm", "left_arm"] + .into_iter() + .map(|n| (n.to_string(), [0.0; 9])) + .collect(), + dummy: [0.0; 9], + skipped: Default::default(), + hidden: Default::default(), + }; + let arm = if right { "right_arm" } else { "left_arm" }; + p.xrot("head", pitch); + p.yrot("head", yaw); + p.xrot(arm, 0.4); + pose_arm( + &mut p, + ArmPose::Block, + right, + &Hum::new(ArmPose::Empty, ArmPose::Empty), + ); + let pose = *p.get(arm); + assert!((pose[3] - expected_pitch).abs() < 1e-6); + let side = if right { -FRAC_PI_6 } else { FRAC_PI_6 }; + assert!((pose[4] - (side + expected_yaw)).abs() < 1e-6); + assert_eq!(p.get(if right { "left_arm" } else { "right_arm" })[3], 0.0); + } + } + } #[test] fn flight_slows_limbs_and_sets_the_head_pose() { let state = State { @@ -709,5 +879,74 @@ mod flight_pose_tests { humanoid(&mut flight, &state, &h); assert_eq!(flight.get("head")[3], -std::f32::consts::FRAC_PI_4); assert_eq!(flight.get("right_leg")[3], ground.get("right_leg")[3] / 8.0); + // THROW_TRIDENT: the arm goes up over the head, and zombie arms leave it there. + let mut throw = make(); + let mut h = Hum::new(ArmPose::ThrowTrident, ArmPose::Item); + h.zombie = Some(true); + humanoid(&mut throw, &state, &h); + let still = (state.pos * 0.6662 + PI).mcos() * 2.0 * 0.5; + assert_eq!(throw.get("right_arm")[3], still * 0.5 - PI); + assert_eq!(throw.get("right_arm")[4], 0.0); + assert_eq!(throw.get("left_arm")[3], -PI / 1.5); + } + + #[test] + fn swimming_strokes_the_arms_and_kicks() { + let state = State { + pos: 7.0, + speed: 1.0, + age: 0.0, + y_rot: 0.0, + x_rot: 30.0, + swing: 0.0, + baby: false, + partial: 0.0, + }; + let make = || Poses { + map: ["head", "right_arm", "left_arm", "right_leg", "left_leg"] + .into_iter() + .map(|n| (n.to_string(), [0.0; 9])) + .collect(), + dummy: [0.0; 9], + skipped: Default::default(), + hidden: Default::default(), + }; + let same_angle = |a: f32, b: f32| ((a - b + PI).rem_euclid(PI * 2.0) - PI).abs() < 1e-5; + let mut h = Hum::new(ArmPose::Empty, ArmPose::Empty); + h.swim = 1.0; + let mut p = make(); + humanoid(&mut p, &state, &h); + // Mid-stroke: both arms sweep out from overhead by the quadratic. + let stroke = 1.870_796_4 * (-65.0 * 7.0 + 49.0) / (-65.0 * 14.0 + 196.0); + assert_eq!(p.get("right_arm")[5], PI - stroke); + assert!(same_angle(p.get("left_arm")[5], PI + stroke)); + assert_eq!(p.get("right_arm")[4], PI); + assert!(same_angle(p.get("head")[3], -std::f32::consts::FRAC_PI_4)); + assert!(same_angle( + p.get("right_leg")[3], + 0.3 * (7.0f32 * 0.333_333_34).mcos() + )); + // Using an item keeps the arms; a swing frees its own arm only. + let mut using = make(); + h.using_item = true; + humanoid(&mut using, &state, &h); + assert_eq!(using.get("right_arm")[4], 0.0); + h.using_item = false; + h.swing_right_arm = Some(true); + let mut swinging = make(); + humanoid( + &mut swinging, + &State { + swing: 0.5, + ..state + }, + &h, + ); + assert!(swinging.get("right_arm")[4] < 1.0); + assert!(same_angle(swinging.get("left_arm")[4], PI)); + // Out of the pose nothing changes. + let mut dry = make(); + humanoid(&mut dry, &state, &Hum::new(ArmPose::Empty, ArmPose::Empty)); + assert_eq!(dry.get("head")[3], 30.0f32.to_radians()); } } diff --git a/crates/horizon-client/src/mobs/cube.rs b/crates/horizon-client/src/mobs/cube.rs index 3986ffb93..ea8a05392 100644 --- a/crates/horizon-client/src/mobs/cube.rs +++ b/crates/horizon-client/src/mobs/cube.rs @@ -47,14 +47,24 @@ pub fn register(m: &mut Mob) { m.extra.vector[0] = f64::from(180.0 * m.yaw / std::f32::consts::PI); set_size(m, 1); } -/// AbstractCubeMob.setSize: health size^2, speed 0.2 + 0.1 * size, damage size. +/// AbstractCubeMob.setSize(size, true): the max health (size^2), movement +/// speed (0.2 + 0.1 * size, in floats) and attack damage (size) bases, a +/// magma cube's armor (size * 3), then the health back to the max. pub fn set_size(m: &mut Mob, size: i32) { + use super::finalize::{Attr, set_base}; let size = size.clamp(1, 127); m.extra.size = size; - m.attrs.max_health = (size * size) as f32; - m.attrs.speed = 0.2 + 0.1 * f64::from(size); - m.attrs.attack_damage = size as f32; - m.health = m.attrs.max_health; + set_base(m, Attr::MaxHealth, f64::from(size * size)); + set_base( + m, + Attr::MovementSpeed, + f64::from(0.2f32 + 0.1f32 * size as f32), + ); + set_base(m, Attr::AttackDamage, f64::from(size)); + if m.species() == super::Species::MagmaCube { + set_base(m, Attr::Armor, f64::from(size * 3)); + } + m.health = super::finalize::max_health(m); } /// AbstractCubeMob.setSpawnSize: 1, 2 or 4 (normal difficulty special multiplier 0). pub fn finalize(m: &mut Mob) { diff --git a/crates/horizon-client/src/mobs/equine.rs b/crates/horizon-client/src/mobs/equine.rs index 46d33b576..e7d73f0fa 100644 --- a/crates/horizon-client/src/mobs/equine.rs +++ b/crates/horizon-client/src/mobs/equine.rs @@ -83,19 +83,33 @@ pub fn register(m: &mut Mob) { return super::llama::register(m); } let tempt = m.kind.tempt.clone(); - let rears = can_rear(m.species()); + let species = m.species(); + let rears = can_rear(species); + let stand_interval = m.kind.ambient_interval; let g = &mut m.ai.goals; - g.add(0, Float); - g.add(1, MountPanic(Panic::new(1.2))); g.add(1, RunAroundLikeCrazy); g.add(2, Breed::new(1.0)); - g.add(3, Tempt::new(1.25, tempt, false)); g.add(4, FollowParent::new(1.0)); g.add(6, Stroll::avoiding_water(0.7)); g.add(7, LookAtPlayer::new(6.0)); g.add(8, RandomLookAround::default()); if rears { - g.add(9, RandomStand { next: -80 }); + // resetStandInterval: -getAmbientStandInterval (400 for every horse). + g.add( + 9, + RandomStand { + next: -stand_interval, + }, + ); + } + // addBehaviourGoals, after the rest: none for the skeleton horse (it + // sinks), no mount panic for the zombie horse. + if species != Species::SkeletonHorse { + g.add(0, Float); + if species != Species::ZombieHorse { + g.add(1, MountPanic(Panic::new(1.2))); + } + g.add(3, Tempt::new(1.25, tempt, false)); } } /// `generateMaxHealth`, `generateSpeed`, `generateJumpStrength` and the coat. @@ -285,7 +299,7 @@ impl Goal for RandomStand { fn can_use(&mut self, m: &mut Mob, _: &mut Cx) -> bool { self.next += 1; if self.next > 0 && m.random.next_int_bound(1000).unwrap_or(1000) < self.next { - self.next = -80; + self.next = -m.kind.ambient_interval; return m.random.next_int_bound(10).unwrap_or(1) == 0; } false diff --git a/crates/horizon-client/src/mobs/finalize.rs b/crates/horizon-client/src/mobs/finalize.rs index a3db1ee52..ae6a864cc 100644 --- a/crates/horizon-client/src/mobs/finalize.rs +++ b/crates/horizon-client/src/mobs/finalize.rs @@ -483,7 +483,7 @@ pub fn base_value(m: &Mob, a: Attr) -> f64 { .unwrap_or(if a == Attr::BurningTime { 1.0 } else { 0.0 }) } /// The transient modifiers of the mob's state (`SPEED_MODIFIER_BABY`). -fn transient(m: &Mob, a: Attr) -> Option<(f64, Op)> { +pub fn transient(m: &Mob, a: Attr) -> Option<(f64, Op)> { if a != Attr::MovementSpeed || !m.is_baby() { return None; } @@ -1961,19 +1961,7 @@ fn sulfur_size(m: &mut Mob, size: i32) { m.health = max_health(m); } fn cube_size(m: &mut Mob, size: i32) { - let size = size.clamp(1, 127); - m.extra.size = size; - set_base(m, Attr::MaxHealth, f64::from(size * size)); - set_base( - m, - Attr::MovementSpeed, - f64::from(0.2f32 + 0.1f32 * size as f32), - ); - set_base(m, Attr::AttackDamage, f64::from(size)); - if m.species() == Species::MagmaCube { - set_base(m, Attr::Armor, f64::from(size * 3)); - } - m.health = max_health(m); + super::cube::set_size(m, size); } /// The index the game keeps for a variant registry entry. pub fn variant_index(s: Species, name: &str) -> u8 { diff --git a/crates/horizon-client/src/mobs/goals.rs b/crates/horizon-client/src/mobs/goals.rs index 0d13b0e81..9419a23ba 100644 --- a/crates/horizon-client/src/mobs/goals.rs +++ b/crates/horizon-client/src/mobs/goals.rs @@ -94,6 +94,10 @@ impl GoalSelector { .map(|g| g.goal.name()) .collect() } + /// Whether a goal of this name is registered, running or not. + pub fn has(&self, name: &str) -> bool { + self.goals.iter().any(|g| g.goal.name() == name) + } pub fn is_running(&self, name: &str) -> bool { self.goals .iter() diff --git a/crates/horizon-client/src/mobs/living.rs b/crates/horizon-client/src/mobs/living.rs index 6bf246541..ebbfabf6b 100644 --- a/crates/horizon-client/src/mobs/living.rs +++ b/crates/horizon-client/src/mobs/living.rs @@ -398,10 +398,7 @@ impl Mob { /// (touching water resets the fall), then Entity.checkFallDamage. fn check_fall_damage(&mut self, w: &MobWorld, moved: Vec3, events: &mut Vec<MobEvent>) { if !self.fluids.in_water { - self.fluids = w.fluids(self.bounds(), self.eye_y()); - if self.fluids.in_water { - self.fall_distance = 0.0; - } + self.update_fluids(w, events); } if !self.fluids.in_water && moved.y < 0.0 { self.fall_distance -= f64::from(moved.y as f32); @@ -413,6 +410,23 @@ impl Mob { self.fall_distance = 0.0; } } + /// Entity.updateFluidInteraction: the fluid state, then touching water + /// resets the fall and entering it (after the first tick) splashes. + pub(super) fn update_fluids(&mut self, w: &MobWorld, events: &mut Vec<MobEvent>) { + let was_touching_water = self.fluids.in_water; + self.fluids = w.fluids(self.bounds(), self.eye_y()); + if self.fluids.in_water { + self.fall_distance = 0.0; + if !was_touching_water && !self.first_tick { + // The splash is the mob's own: Horizon's mobs carry no + // controlling passenger here. + let _ = horizon_world::water_splash::water_splash( + &mut Splash { mob: self, events }, + 1_000_000, + ); + } + } + } /// Entity.applyMovementEmissionAndPlaySound: walked distance, footsteps /// and swimming sounds. fn movement_emission(&mut self, w: &MobWorld, moved: Vec3, events: &mut Vec<MobEvent>) { @@ -811,7 +825,8 @@ impl Mob { self.velocity.y += if self.species() == super::Species::Bee { 0.01 } else if self.ai.nav.can_float { - 0.04 + // LivingEntity.jumpInLiquid adds 0.04F. + f64::from(0.04_f32) } else { 0.3 }; @@ -915,3 +930,93 @@ fn free_position( let free = allowed_shape.join(&collisions, ShapeOp::ONLY_FIRST).ok()?; free.closest_point(preferred) } + +/// `Entity.doWaterSplashEffect` for a mob: its own random, its splash sound +/// (`getSwimSplashSound`), particles for the client and the SPLASH game event. +struct Splash<'a> { + mob: &'a mut Mob, + events: &'a mut Vec<MobEvent>, +} +impl horizon_world::water_splash::WaterSplashWorld for Splash<'_> { + type Error = std::convert::Infallible; + type Entity = (); + type Sound = &'static str; + fn this_entity(&self) {} + fn controlling_passenger(&mut self) -> Result<Option<()>, Self::Error> { + Ok(None) + } + fn velocity(&mut self, _: ()) -> Result<Vec3, Self::Error> { + Ok(self.mob.velocity) + } + fn splash_sound(&mut self, _high_speed: bool) -> Result<&'static str, Self::Error> { + use super::Species; + // Entity.getSwimSplashSound and getSwimHighSpeedSplashSound: the + // overrides are Monster's, the hoglin's, the dolphin's and the axolotl's. + let species = self.mob.species(); + Ok(match species { + Species::Dolphin => "entity.dolphin.splash", + Species::Axolotl => "entity.axolotl.splash", + _ if species == Species::Hoglin || super::ai::is_monster(species) => { + "entity.hostile.splash" + } + _ => "entity.generic.splash", + }) + } + fn random_float(&mut self) -> Result<f32, Self::Error> { + Ok(self.mob.random.next_float()) + } + fn random_double(&mut self) -> Result<f64, Self::Error> { + Ok(self.mob.random.next_double()) + } + fn play_sound( + &mut self, + sound: &'static str, + volume: f32, + pitch: f32, + ) -> Result<(), Self::Error> { + if !self.mob.silent { + let at = [ + self.mob.position.x, + self.mob.position.y, + self.mob.position.z, + ]; + self.events.push(MobEvent::Sound(sound, at, volume, pitch)); + } + Ok(()) + } + fn position(&self) -> Vec3 { + self.mob.position + } + fn width(&self) -> f32 { + self.mob.width() + } + fn particle( + &mut self, + kind: horizon_world::water_splash::SplashParticle, + position: Vec3, + velocity: Vec3, + ) -> Result<(), Self::Error> { + self.events.push(MobEvent::ParticleAt { + kind: match kind { + horizon_world::water_splash::SplashParticle::Bubble => "bubble", + horizon_world::water_splash::SplashParticle::Splash => "splash", + }, + at: [position.x, position.y, position.z], + velocity: [velocity.x, velocity.y, velocity.z], + }); + Ok(()) + } + fn splash_event(&mut self) -> Result<(), Self::Error> { + self.events.push(MobEvent::GameEvent { + event: "minecraft:splash", + at: [ + self.mob.position.x, + self.mob.position.y, + self.mob.position.z, + ], + source: self.mob.id, + state: horizon_world::registry::BlockStateId(0), + }); + Ok(()) + } +} diff --git a/crates/horizon-client/src/mobs/path.rs b/crates/horizon-client/src/mobs/path.rs index 368dead97..abadc9565 100644 --- a/crates/horizon-client/src/mobs/path.rs +++ b/crates/horizon-client/src/mobs/path.rs @@ -4,8 +4,8 @@ //! `AmphibiousNodeEvaluator`), ported rule for rule. use super::{MobWorld, data}; use crate::blocks::Fluid; +use crate::fast_hash::FxHashMap; use horizon_core::geometry::{Aabb, Vec3}; -use std::collections::HashMap; #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum PathType { @@ -301,10 +301,13 @@ pub struct Finder<'a, 'w> { mob: &'a PathMob, eval: Evaluator, nodes: Vec<Node>, - index: HashMap<(i32, i32, i32), u32>, + index: FxHashMap<(i32, i32, i32), u32>, heap: Vec<u32>, - types: HashMap<(i32, i32, i32), PathType>, - collisions: HashMap<[u64; 6], bool>, + types: FxHashMap<(i32, i32, i32), PathType>, + /// `path_type` of each position asked during this query: it reads up to + /// 26 blocks, and overlapping mob boxes ask for the same positions. + statics: std::cell::RefCell<FxHashMap<(i32, i32, i32), PathType>>, + collisions: FxHashMap<[u64; 6], bool>, entity_width: i32, entity_height: i32, entity_depth: i32, @@ -319,10 +322,11 @@ impl<'a, 'w> Finder<'a, 'w> { mob, eval, nodes: Vec::new(), - index: HashMap::new(), + index: FxHashMap::default(), heap: Vec::new(), - types: HashMap::new(), - collisions: HashMap::new(), + types: FxHashMap::default(), + statics: Default::default(), + collisions: FxHashMap::default(), entity_width: (mob.width + 1.0).floor() as i32, entity_height: (mob.height + 1.0).floor() as i32, entity_depth: (mob.width + 1.0).floor() as i32, @@ -385,7 +389,17 @@ impl<'a, 'w> Finder<'a, 'w> { } // --- path types ----------------------------------------------------- /// WalkNodeEvaluator.getPathTypeStatic (+ the fly/amphibious overrides). + /// The world doesn't change during a query, so each position's type is + /// worked out once. fn path_type(&self, x: i32, y: i32, z: i32) -> PathType { + if let Some(t) = self.statics.borrow().get(&(x, y, z)) { + return *t; + } + let t = self.compute_path_type(x, y, z); + self.statics.borrow_mut().insert((x, y, z), t); + t + } + fn compute_path_type(&self, x: i32, y: i32, z: i32) -> PathType { match self.eval { Evaluator::Fly => self.fly_path_type(x, y, z), Evaluator::Amphibious { .. } => { @@ -991,7 +1005,7 @@ impl<'a, 'w> Finder<'a, 'w> { let malus = |s: &Self, n: Option<u32>| n.is_some_and(|n| s.nodes[n as usize].cost_malus >= 0.0); // Direction.values(): down, up, north, south, west, east. - let mut by_dir: HashMap<(i32, i32, i32), Option<u32>> = HashMap::new(); + let mut by_dir: FxHashMap<(i32, i32, i32), Option<u32>> = FxHashMap::default(); for (dx, dy, dz) in [ (0, -1, 0), (0, 1, 0), diff --git a/crates/horizon-client/src/mobs/render.rs b/crates/horizon-client/src/mobs/render.rs index e8120e92e..a0dce0e08 100644 --- a/crates/horizon-client/src/mobs/render.rs +++ b/crates/horizon-client/src/mobs/render.rs @@ -765,6 +765,17 @@ pub fn base_transform_at(m: &Mob, alpha: f32, origin: glam::Mat4) -> glam::Mat4 .unwrap_or_else(|| glam::Mat4::from_rotation_y((180.0 - body).to_radians())), }; let mut base = origin * rotation; + if m.death_time > 0 && !super::creaking::tearing_down(m) { + let f = ((m.death_time as f32 + alpha - 1.0) / 20.0 * 1.6) + .sqrt() + .min(1.0); + base *= glam::Mat4::from_rotation_z(f * flip_degrees(m).to_radians()); + } else if matches!(m.custom_name.as_deref(), Some("Dinnerbone" | "Grumm")) { + // isEntityUpsideDown + base *= glam::Mat4::from_translation(glam::Vec3::new(0.0, m.height() + 0.1, 0.0)) + * glam::Mat4::from_rotation_z(PI); + } + // AvatarRenderer.setupRotations, after LivingEntityRenderer's. if m.extra.fall_flying { let ticks = m.extra.fall_fly_ticks as f32 + alpha; let scale = (ticks * ticks / 100.0).min(1.0); @@ -785,17 +796,20 @@ pub fn base_transform_at(m: &Mob, alpha: f32, origin: glam::Mat4) -> glam::Mat4 (sign * horizon_core::jmath::acos(dot.abs().min(1.0))) as f32, ); } - } - - if m.death_time > 0 && !super::creaking::tearing_down(m) { - let f = ((m.death_time as f32 + alpha - 1.0) / 20.0 * 1.6) - .sqrt() - .min(1.0); - base *= glam::Mat4::from_rotation_z(f * flip_degrees(m).to_radians()); - } else if matches!(m.custom_name.as_deref(), Some("Dinnerbone" | "Grumm")) { - // isEntityUpsideDown - base *= glam::Mat4::from_translation(glam::Vec3::new(0.0, m.height() + 0.1, 0.0)) - * glam::Mat4::from_rotation_z(PI); + } else if m.extra.swim.amount(alpha) > 0.0 { + // AvatarRenderer.setupRotations: tip forward with the swim amount, + // following the pitch in water, then lie along the swimming box. + let amount = m.extra.swim.amount(alpha); + let pitch = m.previous_head_pitch + (m.head_pitch - m.previous_head_pitch) * alpha; + let target = if m.extra.swim.in_water { + -90.0 - pitch + } else { + -90.0 + }; + base *= glam::Mat4::from_rotation_x((amount * target).to_radians()); + if m.extra.swim.visually { + base *= glam::Mat4::from_translation(glam::Vec3::new(0.0, -1.0, 0.3)); + } } base *= species_rotations(m, alpha); base *= glam::Mat4::from_scale(glam::Vec3::new(-1.0, -1.0, 1.0)); @@ -1027,17 +1041,24 @@ fn render_visible( .unwrap_or_else(|| m.kind.model.clone()); let mut poses = Poses::new(&model); anim::setup(m, &s, &mut poses, &data.animations); - if let Some(uv) = atlas(&texture) { - draw( - &model, - base, - &poses, - uv, - body_color(m, alpha), - light, - body_overlay(m, alpha), - out, - ); + let draw_body = |out: &mut Vec<Vertex>| { + if let Some(uv) = atlas(&texture) { + draw( + &model, + base, + &poses, + uv, + body_color(m, alpha), + light, + body_overlay(m, alpha), + out, + ); + } + }; + // The translucent shell writes depth: draw the opaque core before it. + let shell_last = m.species() == Species::SulfurCube; + if !shell_last { + draw_body(out); } for (part, uv, color) in extra { draw(part, base, &poses, *uv, *color, light, overlay, out); @@ -1095,6 +1116,9 @@ fn render_visible( } } } + if shell_last { + draw_body(out); + } } /// The equipment slot of a `HumanoidArmorLayer` model (`zombie#helmet`). fn armor_slot(model: &str) -> Option<usize> { @@ -1235,3 +1259,93 @@ pub fn held_item_transform( anim::setup(m, &s, &mut poses, &data.animations); hand_base(m, &model, base, &poses, left) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn sulfur_cube_opaque_core_is_submitted_before_its_translucent_shell() { + let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../.local/horizon-client/26.3"); + if !root.join("entity-models.json").exists() { + return; + } + let mut types = super::super::MobTypes::load(&root).unwrap(); + let kind = std::sync::Arc::new(types.types.remove("minecraft:sulfur_cube").unwrap()); + let data = MobData { + states: Vec::new(), + placements: Default::default(), + heightmaps: Default::default(), + animations: Default::default(), + biome_tags: Default::default(), + entity_tags: Default::default(), + villager_hats: Default::default(), + pois: Default::default(), + }; + let atlas = |name: &str| { + Some(if name.contains("_inner") { + [0.0, 0.0, 0.25, 0.25] + } else { + [0.5, 0.5, 0.75, 0.75] + }) + }; + for baby in [false, true] { + let mut mob = Mob::new(1, kind.clone(), horizon_core::geometry::Vec3::ZERO, 0.0); + mob.extra.age = if baby { -24000 } else { 0 }; + super::super::sulfur_cube::finalize(&mut mob); + assert_eq!(mob.is_baby(), baby); + for posed in [false, true] { + let mut vertices = Vec::new(); + if posed { + render_posed( + &mob, + 0.5, + glam::Mat4::IDENTITY, + &atlas, + 0xf0f0, + &data, + &mut vertices, + ); + } else { + render( + &mob, + 0.5, + glam::DVec3::ZERO, + &atlas, + 0xf0f0, + &data, + &mut vertices, + ); + } + assert_eq!( + vertices.len(), + 48, + "both real six-face models must be drawn" + ); + assert!( + vertices[..24].iter().all(|v| v.uv[0] <= 0.25), + "opaque core must precede shell" + ); + assert!(vertices[24..].iter().all(|v| v.uv[0] >= 0.5)); + } + mob.extra.head = Some("minecraft:stone".into()); + let mut vertices = Vec::new(); + render( + &mob, + 0.5, + glam::DVec3::ZERO, + &atlas, + 0xf0f0, + &data, + &mut vertices, + ); + assert_eq!( + vertices.len(), + 24, + "a swallowed block still hides the default core" + ); + assert!(vertices.iter().all(|v| v.uv[0] >= 0.5)); + } + } +} diff --git a/crates/horizon-client/src/mobs/spawn_rules.rs b/crates/horizon-client/src/mobs/spawn_rules.rs index 22f9206af..1cdca41b9 100644 --- a/crates/horizon-client/src/mobs/spawn_rules.rs +++ b/crates/horizon-client/src/mobs/spawn_rules.rs @@ -589,11 +589,7 @@ impl SpawnLevel for super::MobWorld<'_> { self.sea_level } fn world_surface(&self, x: i32, z: i32) -> i32 { - let top = crate::chunk::MIN_Y + crate::chunk::HEIGHT; - (crate::chunk::MIN_Y..top) - .rev() - .find(|&y| !self.facts.is_air(self.world.block(x, y, z))) - .map_or(crate::chunk::MIN_Y, |y| y + 1) + self.world.world_surface(x, z, |id| self.facts.is_air(id)) } fn peaceful(&self) -> bool { self.difficulty.effective == 0.0 diff --git a/crates/horizon-client/src/mobs/species.rs b/crates/horizon-client/src/mobs/species.rs index fcfd61992..28ea729b3 100644 --- a/crates/horizon-client/src/mobs/species.rs +++ b/crates/horizon-client/src/mobs/species.rs @@ -1634,7 +1634,16 @@ impl Def { use Species as S; match self.species { S::Turtle => 200, - S::Horse | S::Donkey | S::Mule | S::Llama | S::TraderLlama => 400, + // AbstractHorse.getAmbientSoundInterval, for every subclass. + S::Horse + | S::Donkey + | S::Mule + | S::ZombieHorse + | S::SkeletonHorse + | S::Llama + | S::TraderLlama + | S::Camel + | S::CamelHusk => 400, S::Ocelot => 900, S::Squid | S::GlowSquid diff --git a/crates/horizon-client/src/mobs/tame.rs b/crates/horizon-client/src/mobs/tame.rs index 4e5e3d290..1e725df80 100644 --- a/crates/horizon-client/src/mobs/tame.rs +++ b/crates/horizon-client/src/mobs/tame.rs @@ -855,8 +855,17 @@ impl Goal for CatSitOnBlock { JUMP | MOVE } fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + if !m.extra.tame || m.extra.sitting { + return false; + } + // MoveToBlockGoal.canUse draws the next delay only once the last one + // has run out. + if self.0.next_start > 0 { + self.0.next_start -= 1; + return false; + } let next = reduced(200 + m.random.next_int_bound(200).unwrap_or(0)); - m.extra.tame && !m.extra.sitting && self.0.can_use(m, cx.w, next, can_sit_on) + self.0.can_use(m, cx.w, next, can_sit_on) } fn can_continue(&mut self, _: &mut Mob, cx: &mut Cx) -> bool { self.0.can_continue(cx.w, can_sit_on) diff --git a/crates/horizon-client/src/mod_content.rs b/crates/horizon-client/src/mod_content.rs index f78808da7..c396c3333 100644 --- a/crates/horizon-client/src/mod_content.rs +++ b/crates/horizon-client/src/mod_content.rs @@ -323,7 +323,7 @@ impl<'a> Plan<'a> { } let list: Vec<String> = by_mod.iter().map(|(m, n)| format!("{m} {n}")).collect(); return Err(format!( - "{next} block states, more than the {MAX_STATES} chunks can hold ({} in the game; \ + "{next} block states, more than the {MAX_STATES} a world can hold ({} in the game; \ the mods: {})", names.len(), list.join(", ") @@ -644,9 +644,11 @@ const NBT_TABLES: [&str; 6] = [ /// In a root built here: the content mods' ids, the namespaces loaders /// read besides `minecraft`. const MARKER: &str = "horizon-mod-content.json"; -/// Block states a world can hold: chunk sections store a state in 16 bits -/// (crate::chunk). Mods past it are refused, not wrapped onto other blocks. -pub const MAX_STATES: u32 = 1 << 16; +/// Block states a world can hold. Chunk sections widen their global palette +/// past vanilla's 16 bits to fit the registry (crate::chunk::set_block_states); +/// this caps it at 20 bits, about 29 times vanilla's 35,723 states. Mods past +/// it are refused, not wrapped onto other blocks. +pub const MAX_STATES: u32 = 1 << 20; /// The content mods' namespaces in a root built by [`build`]; none in any /// other root, so vanilla's loaders read exactly what they did. @@ -672,6 +674,100 @@ pub fn tag_namespaces(root: &Path) -> Vec<String> { /// Bundles of block tag files (tag id → the tag file's text). const TAG_BUNDLES: [&str; 2] = ["block-tags.json", "worldgen/block_tags.json"]; +/// The worldgen loader's bundle of biome tags. +const BIOME_TAGS: &str = "worldgen/biome_tags.json"; +/// Worldgen registries a content mod may add to, and the bundle the +/// worldgen loader reads each from (id → the file's text): what structure +/// mods hold. Biomes, noise and density functions change the terrain and +/// are not read from mods. +const WORLDGEN_BUNDLES: [(&str, &str); 6] = [ + ("structure", "worldgen/structures.json"), + ("structure_set", "worldgen/structure_sets.json"), + ("template_pool", "worldgen/template_pools.json"), + ("processor_list", "worldgen/processor_lists.json"), + ("configured_feature", "worldgen/features.json"), + ("placed_feature", "worldgen/placed_features.json"), +]; + +/// A mod file that adds to a worldgen bundle: (bundle, id). +fn worldgen_entry(file: &str) -> Option<(&'static str, String)> { + let (ns, rest) = file.strip_prefix("data/")?.split_once("/worldgen/")?; + let (kind, path) = rest.split_once('/')?; + let (_, bundle) = WORLDGEN_BUNDLES.iter().find(|(k, _)| *k == kind)?; + Some((bundle, format!("{ns}:{}", path.strip_suffix(".json")?))) +} + +/// The key of a mod's structure set or template pool file that adds its +/// entries to other sets or pools (vanilla's, another mod's) instead of +/// being one of its own. Vanilla data never has it. +const ADD_TO: &str = "horizon:add_to"; +/// The bundles a mod may add to, and the list each entry goes into. +const ADDABLE: [(&str, &str); 2] = [ + ("worldgen/structure_sets.json", "structures"), + ("worldgen/template_pools.json", "elements"), +]; + +/// A mod file's additions (`horizon:add_to`): its entries appended to the +/// list of each set or pool it names, as lithostitched's +/// `add_structure_set_entries` and `add_template_pool_elements` do for +/// NeoForge and Fabric packs. +struct Addition { + targets: Vec<String>, + list: &'static str, + entries: Vec<J>, +} +impl Addition { + /// What a mod file of `bundle` adds, if it is an addition file. + fn read(bundle: &str, text: &str) -> Result<Option<Self>> { + let Some((_, list)) = ADDABLE.iter().find(|(b, _)| *b == bundle) else { + return Ok(None); + }; + if !text.contains(ADD_TO) { + return Ok(None); + } + let doc: J = serde_json::from_str(text)?; + let Some(targets) = doc.get(ADD_TO) else { + return Ok(None); + }; + let targets = match targets { + J::Str(s) => vec![s.clone()], + J::Arr(a) => a + .iter() + .map(|v| match v { + J::Str(s) => Some(s.clone()), + _ => None, + }) + .collect::<Option<_>>() + .ok_or(format!("{ADD_TO}: not a list of ids"))?, + _ => return Err(format!("{ADD_TO}: not an id or a list of ids").into()), + }; + let Some(J::Arr(entries)) = doc.get(list) else { + return Err(format!("{ADD_TO} without a {list} list").into()); + }; + Ok(Some(Self { + targets, + list, + entries: entries.clone(), + })) + } + /// Appends the entries to each target in the bundle `doc` (id → the + /// file's text). A target nobody defines is skipped, as a tag entry + /// naming an unknown id is. + fn apply(&self, doc: &mut J) -> Result<()> { + for target in &self.targets { + let Some(J::Str(text)) = doc.get(target) else { + continue; + }; + let mut v: J = serde_json::from_str(text).map_err(|e| format!("{target}: {e}"))?; + let Some(J::Arr(list)) = v.get_mut(self.list) else { + continue; + }; + list.extend(self.entries.iter().cloned()); + doc.set(target, J::Str(serde_json::to_string(&v)?)); + } + Ok(()) + } +} /// The overlay being written: `out` mirrors `base` through links, and a /// directory becomes real only when something inside it is written. @@ -771,6 +867,9 @@ fn merge_tag(text: Option<&str>, more: &[J], what: &str) -> Result<String> { struct Known { blocks: std::collections::BTreeSet<String>, items: std::collections::BTreeSet<String>, + /// The worldgen loader's biomes and biome tags. + biomes: std::collections::BTreeSet<String>, + biome_tags: std::collections::BTreeSet<String>, /// `data/<ns>/tags/<kind>/<path>.json` files the mods add. tag_files: std::collections::BTreeSet<String>, } @@ -784,14 +883,27 @@ impl Known { items.extend(o.into_iter().map(|(k, _)| k)); } items.extend(plan.items.iter().map(|i| i.rename.to.clone())); + let keys = |name: &str| -> Result<std::collections::BTreeSet<String>> { + Ok(match base.join(name).exists() { + true => match read_json(&base.join(name))? { + J::Obj(o) => o.into_iter().map(|(k, _)| k).collect(), + _ => Default::default(), + }, + false => Default::default(), + }) + }; Ok(Known { blocks, items, + biomes: keys("worldgen/biomes.json")?, + biome_tags: keys(BIOME_TAGS)?, tag_files: tags.keys().cloned().collect(), }) } /// Whether tag `file` keeps entry `v`: a known id or an existing tag, - /// for block and item tags (other registries are kept as they are). + /// for block, item and biome tags (other registries are kept as they + /// are). The worldgen loader refuses a biome tag naming what it lacks + /// (another mod's biomes, a loader's tags), so those entries go. fn keeps(&self, file: &str, v: &J) -> bool { let id = match v { J::Str(s) => s.as_str(), @@ -801,23 +913,32 @@ impl Known { }, _ => return true, }; - let Some(kind) = file + let Some(rest) = file .strip_prefix("data/") .and_then(|r| r.split_once("/tags/")) - .and_then(|(_, rest)| Some(rest.split_once('/')?.0)) + .map(|(_, rest)| rest) else { return true; }; + let kind = match rest.strip_prefix("worldgen/biome/") { + Some(_) => "worldgen/biome", + None => rest.split_once('/').map_or("", |(k, _)| k), + }; let ids = match kind { "block" => &self.blocks, "item" => &self.items, + "worldgen/biome" => &self.biomes, _ => return true, }; match id.strip_prefix('#') { Some(tag) => { let (tns, path) = tag.split_once(':').unwrap_or(("minecraft", tag)); let rel = format!("data/{tns}/tags/{kind}/{path}.json"); - self.tag_files.contains(&rel) || tns == "minecraft" + self.tag_files.contains(&rel) + || match kind { + "worldgen/biome" => self.biome_tags.contains(&format!("{tns}:{path}")), + _ => tns == "minecraft", + } } None => ids.contains(id), } @@ -854,6 +975,8 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { } // Mod files, and every tag addition gathered per tag file. let mut tags: BTreeMap<String, Vec<J>> = BTreeMap::new(); + let mut worldgen: BTreeMap<&str, Vec<(String, String)>> = BTreeMap::new(); + let mut additions: BTreeMap<&str, Vec<Addition>> = BTreeMap::new(); for m in mods { let declarations = format!("data/{}/horizon/", m.id); for file in &m.content.files { @@ -876,6 +999,22 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { .extend(values.iter().cloned()); } else if !file.starts_with(&declarations) { overlay.write(file, &bytes)?; + if let Some((bundle, id)) = worldgen_entry(file) { + let text = String::from_utf8(bytes).map_err(|e| format!("{file}: {e}"))?; + let entries = worldgen.entry(bundle).or_default(); + match Addition::read(bundle, &text).map_err(|e| format!("{file}: {e}"))? { + Some(addition) => additions.entry(bundle).or_default().push(addition), + None => entries.push((id, text)), + } + } else if let Some(template) = file + .strip_prefix("data/") + .and_then(|r| r.split_once("/structure/")) + .filter(|(_, path)| path.ends_with(".nbt")) + { + // The worldgen loader's templates: worldgen/structure/<ns>/<path>.nbt. + let (ns, path) = template; + overlay.write(&format!("worldgen/structure/{ns}/{path}"), &bytes)?; + } } } } @@ -919,6 +1058,44 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { for (name, doc) in &bundles { overlay.write(name, &serde_json::to_vec(doc)?)?; } + // Biome tags, merged into the worldgen loader's bundle as block tags are. + let biome_tags: Vec<(String, &Vec<J>)> = tags + .iter() + .filter_map(|(file, values)| { + let (ns, path) = file + .strip_prefix("data/")? + .split_once("/tags/worldgen/biome/")?; + Some((format!("{ns}:{}", path.strip_suffix(".json")?), values)) + }) + .collect(); + if !biome_tags.is_empty() && base.join(BIOME_TAGS).exists() { + let mut doc = read_json(&base.join(BIOME_TAGS))?; + for (id, values) in biome_tags { + let text = match doc.get(&id) { + Some(J::Str(t)) => Some(t.clone()), + _ => None, + }; + doc.set(&id, J::Str(merge_tag(text.as_deref(), values, BIOME_TAGS)?)); + } + overlay.write(BIOME_TAGS, &serde_json::to_vec(&doc)?)?; + } + // Structures and the pools, sets, processors and features they use, then + // what mods add to sets and pools, in mod and file order. + for (bundle, entries) in &worldgen { + if !base.join(bundle).exists() { + continue; + } + let mut doc = read_json(&base.join(bundle))?; + for (id, text) in entries { + doc.set(id, J::Str(text.clone())); + } + for addition in additions.get(bundle).into_iter().flatten() { + addition + .apply(&mut doc) + .map_err(|e| format!("{bundle}: {e}"))?; + } + overlay.write(bundle, &serde_json::to_vec(&doc)?)?; + } // A block without its own blockstate looks like its template, so every // state still has a model. for m in mods { @@ -941,15 +1118,75 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { /// The root to load: `base` itself with no content, else the mods' overlay /// over it, built in `cache/mod-content`. +/// +/// The root is built once and kept: later starts with the same build, base +/// and mod files reuse it. With ATM10's carried mods a build takes about +/// 25 s and peaks above 3 GB, more than everything the game keeps. pub fn resource_root(base: &Path, mods: &[ContentMod], cache: &Path) -> Result<PathBuf> { if mods.iter().all(|m| m.content.is_empty()) { return Ok(base.to_owned()); } let out = cache.join("mod-content"); + let stamp = stamp(base, mods)?; + if fs::read(out.join(STAMP)).is_ok_and(|s| s == stamp.as_bytes()) { + return Ok(out); + } build(base, mods, &out)?; + fs::write(out.join(STAMP), stamp)?; Ok(out) } +/// In a root built by [`resource_root`]: what it was built from. +const STAMP: &str = "horizon-mod-content.stamp"; + +/// What a content root is built from: this build of the game, the base's +/// manifest and the tables [`build`] rewrites, and every file of every mod +/// in order. Files count by size and modification time, as build tools do. +fn stamp(base: &Path, mods: &[ContentMod]) -> Result<String> { + use sha2::{Digest, Sha256}; + let meta = |path: &Path| -> Result<String> { + let m = fs::metadata(path).map_err(|e| format!("{}: {e}", path.display()))?; + let modified = m + .modified()? + .duration_since(std::time::UNIX_EPOCH) + .unwrap_or_default(); + Ok(format!( + "{} {} {}", + path.display(), + m.len(), + modified.as_nanos() + )) + }; + let mut parts = vec!["horizon mod content 1".to_owned()]; + parts.push(meta(&std::env::current_exe()?)?); + let base = std::path::absolute(base)?; + parts.push( + String::from_utf8_lossy(&fs::read(base.join("horizon-resources.json"))?).into_owned(), + ); + for name in JSON_TABLES.iter().chain(&NBT_TABLES).chain(&TAG_BUNDLES) { + if base.join(name).exists() { + parts.push(meta(&base.join(name))?); + } + } + for m in mods { + parts.push(m.id.clone()); + let source = m.content.source(); + if source.is_dir() { + for file in &m.content.files { + parts.push(meta(&source.join(file))?); + } + } else { + parts.push(meta(source)?); + } + } + let mut hasher = Sha256::new(); + for part in &parts { + hasher.update((part.len() as u64).to_le_bytes()); + hasher.update(part.as_bytes()); + } + Ok(format!("{:x}", hasher.finalize())) +} + #[cfg(test)] #[path = "mod_content_tests.rs"] mod tests; diff --git a/crates/horizon-client/src/mod_content_tests.rs b/crates/horizon-client/src/mod_content_tests.rs index 7067eef40..c80b45c3e 100644 --- a/crates/horizon-client/src/mod_content_tests.rs +++ b/crates/horizon-client/src/mod_content_tests.rs @@ -266,17 +266,17 @@ fn refuses_unknown_templates_and_vanilla_ids() { } #[test] -fn refuses_more_states_than_chunks_hold() { +fn refuses_more_states_than_a_world_holds() { let Some(base) = prepared() else { return; }; let scratch = std::env::temp_dir().join(format!("horizon-mod-states-{}", std::process::id())); let _ = fs::remove_dir_all(&scratch); let mut m = ruby_mod(&scratch.join("ruby")); - // Redstone wire has 1296 states: 40 copies pass any game's 65536. + // Redstone wire has 1296 states: 810 copies pass 2^20 with any game. let mut wire = m.content.blocks[0].clone(); wire.template = "minecraft:redstone_wire".into(); - for i in 0..40 { + for i in 0..810 { let mut b = wire.clone(); b.id = format!("ruby:wire_{i}"); m.content.blocks.push(b); @@ -284,13 +284,33 @@ fn refuses_more_states_than_chunks_hold() { let e = build(&base, std::slice::from_ref(&m), &scratch.join("out")).unwrap_err(); assert!( e.to_string() - .contains("more than the 65536 chunks can hold"), + .contains("more than the 1048576 a world can hold"), "{e}" ); assert!(e.to_string().contains("the mods: ruby "), "{e}"); fs::remove_dir_all(&scratch).unwrap(); } +#[test] +fn builds_past_sixteen_bit_states() { + let Some(base) = prepared() else { + return; + }; + let scratch = std::env::temp_dir().join(format!("horizon-mod-wide-{}", std::process::id())); + let _ = fs::remove_dir_all(&scratch); + let mut m = ruby_mod(&scratch.join("ruby")); + // 40 redstone wires: 51,840 states, past 65,536 with vanilla's 35,723. + let mut wire = m.content.blocks[0].clone(); + wire.template = "minecraft:redstone_wire".into(); + for i in 0..40 { + let mut b = wire.clone(); + b.id = format!("ruby:wire_{i}"); + m.content.blocks.push(b); + } + build(&base, std::slice::from_ref(&m), &scratch.join("out")).unwrap(); + fs::remove_dir_all(&scratch).unwrap(); +} + #[test] fn no_content_keeps_the_base_root() { let base = Path::new("/nonexistent/base"); @@ -482,3 +502,162 @@ fn independent_reloads_read_changed_behaviour_json_and_report_invalid_files() { ); fs::remove_dir_all(&scratch).unwrap(); } + +/// A built root is kept for later starts, and built again when a mod's +/// files change. +#[test] +fn content_root_is_reused_until_a_mod_changes() { + let Some(base) = prepared() else { + return; + }; + let scratch = std::env::temp_dir().join(format!("horizon-mod-reuse-{}", std::process::id())); + let _ = fs::remove_dir_all(&scratch); + let m = ruby_mod(&scratch.join("ruby")); + let cache = scratch.join("cache"); + let root = resource_root(&base, std::slice::from_ref(&m), &cache).unwrap(); + // A rebuild starts from an empty folder: this file tells them apart. + let sentinel = root.join("sentinel"); + fs::write(&sentinel, b"").unwrap(); + assert_eq!( + resource_root(&base, std::slice::from_ref(&m), &cache).unwrap(), + root + ); + assert!(sentinel.exists(), "an unchanged mod rebuilt the root"); + let lang = scratch.join("ruby/assets/ruby/lang/en_us.json"); + fs::write(&lang, r#"{"block.ruby.ruby_block": "Ruby Block"}"#).unwrap(); + let file = fs::File::options().write(true).open(&lang).unwrap(); + file.set_modified(std::time::SystemTime::now() + std::time::Duration::from_secs(5)) + .unwrap(); + resource_root(&base, std::slice::from_ref(&m), &cache).unwrap(); + assert!(!sentinel.exists(), "a changed mod kept the old root"); + assert!( + fs::read_to_string(root.join("assets/ruby/lang/en_us.json")) + .unwrap() + .contains("Ruby Block") + ); + let _ = fs::remove_dir_all(&scratch); +} + +/// A structure mod's worldgen reaches the worldgen loader: its structure, +/// set, pool and templates join the bundles, and its biome tag keeps only +/// what the loader knows. +#[test] +fn content_root_carries_a_structure_mod() { + let Some(base) = prepared() else { + return; + }; + let scratch = std::env::temp_dir().join(format!("horizon-mod-worldgen-{}", std::process::id())); + let _ = fs::remove_dir_all(&scratch); + let dir = scratch.join("huts"); + let files = [ + ( + "horizon-mod.toml", + "id = \"huts\"\nname = \"Huts\"\nversion = \"1.0\"\napi = \"0.2\"\nside = \"both\"\ncapabilities = [\"registries\"]\n", + ), + ( + "data/huts/worldgen/structure/hut.json", + r##"{"type": "minecraft:jigsaw", "biomes": "#huts:has_hut", "step": "surface_structures", + "spawn_overrides": {}, "terrain_adaptation": "beard_thin", "start_pool": "huts:hut/start", + "size": 1, "start_height": {"absolute": 0}, "project_start_to_heightmap": "WORLD_SURFACE_WG", + "max_distance_from_center": 80}"##, + ), + ( + "data/huts/worldgen/template_pool/hut/start.json", + r#"{"fallback": "minecraft:empty", "elements": [{"weight": 1, "element": { + "element_type": "minecraft:single_pool_element", "location": "huts:hut", + "projection": "rigid", "processors": "minecraft:empty"}}]}"#, + ), + ( + "data/huts/worldgen/structure_set/huts.json", + r#"{"structures": [{"structure": "huts:hut", "weight": 1}], + "placement": {"type": "minecraft:random_spread", "salt": 7, "spacing": 4, "separation": 2}}"#, + ), + ( + "data/huts/tags/worldgen/biome/has_hut.json", + r##"{"values": ["#minecraft:is_overworld", "#c:is_plains", "othermod:glade", "minecraft:plains"]}"##, + ), + // Additions to vanilla's set and pool; a target nobody defines is skipped. + ( + "data/huts/worldgen/structure_set/added/villages.json", + r#"{"horizon:add_to": "minecraft:villages", + "structures": [{"structure": "huts:hut", "weight": 3}]}"#, + ), + ( + "data/huts/worldgen/template_pool/added/houses.json", + r#"{"horizon:add_to": ["othermod:houses", "minecraft:village/plains/houses"], + "elements": [{"weight": 2, "element": {"element_type": "minecraft:single_pool_element", + "location": "huts:hut", "projection": "rigid", "processors": "minecraft:empty"}}]}"#, + ), + ]; + for (name, text) in files { + let path = dir.join(name); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, text).unwrap(); + } + // The template: vanilla's smallest plains house, as the mod's own file. + let house = + base.join("worldgen/structure/minecraft/village/plains/houses/plains_small_house_1.nbt"); + fs::create_dir_all(dir.join("data/huts/structure")).unwrap(); + fs::copy(&house, dir.join("data/huts/structure/hut.nbt")).unwrap(); + let package = horizon_mods::Package::read(&dir).unwrap(); + let m = ContentMod { + id: "huts".into(), + content: Content::read(&dir, &package.manifest).unwrap(), + }; + let root = resource_root(&base, &[m], &scratch.join("cache")).unwrap(); + assert!(root.join("worldgen/structure/huts/hut.nbt").exists()); + let tags: BTreeMap<String, String> = + serde_json::from_slice(&fs::read(root.join("worldgen/biome_tags.json")).unwrap()).unwrap(); + let hut: serde_json::Value = serde_json::from_str(&tags["huts:has_hut"]).unwrap(); + assert_eq!( + hut["values"], + serde_json::json!(["#minecraft:is_overworld", "minecraft:plains"]) + ); + // The additions join vanilla's entries at the end and are no set or pool + // of their own. + let bundle = |name: &str, base: &Path| -> BTreeMap<String, String> { + serde_json::from_slice(&fs::read(base.join("worldgen").join(name)).unwrap()).unwrap() + }; + for (name, id, list, added) in [ + ( + "structure_sets.json", + "minecraft:villages", + "structures", + serde_json::json!({"structure": "huts:hut", "weight": 3}), + ), + ( + "template_pools.json", + "minecraft:village/plains/houses", + "elements", + serde_json::json!({"weight": 2, "element": {"element_type": "minecraft:single_pool_element", + "location": "huts:hut", "projection": "rigid", "processors": "minecraft:empty"}}), + ), + ] { + let (ours, vanilla) = (bundle(name, &root), bundle(name, &base)); + let entries = |text: &str| -> Vec<serde_json::Value> { + let v: serde_json::Value = serde_json::from_str(text).unwrap(); + v[list].as_array().unwrap().clone() + }; + let mut expected = entries(&vanilla[id]); + expected.push(added); + assert_eq!(entries(&ours[id]), expected, "{id}"); + assert!(!ours.keys().any(|k| k.starts_with("huts:added/")), "{name}"); + assert!(!ours.contains_key("othermod:houses")); + } + + let assets = Assets::load(&root).unwrap(); + let biomes = crate::biome::Biomes::load(&root).unwrap(); + let terrain = crate::vanillagen::VanillaTerrain::new( + &root.join("worldgen"), + assets.registry, + &biomes, + 1234, + ) + .unwrap(); + let (name, at) = terrain + .locate(&["huts:hut".to_owned()], [0, 64, 0], 16) + .expect("the mod's structure generates"); + assert_eq!(name, "huts:hut"); + assert!(at[0].abs() < 1024 && at[2].abs() < 1024, "{at:?}"); + let _ = fs::remove_dir_all(&scratch); +} diff --git a/crates/horizon-client/src/model_library.rs b/crates/horizon-client/src/model_library.rs index 7f7852ad0..c6a1a9d87 100644 --- a/crates/horizon-client/src/model_library.rs +++ b/crates/horizon-client/src/model_library.rs @@ -42,25 +42,52 @@ impl ParsedModels { /// Blocks require every referenced resource, including parents. Items instead /// skip broken resources and use the resolver's missing-model fallback. - pub(crate) fn block_library( + pub(crate) fn block_library(&self, root: &Path, pending: Vec<String>) -> Result<ModelLibrary> { + self.block_models(root, pending, false) + } + /// [`Self::block_library`], but a model that does not read takes the + /// built-in missing model, logged, as vanilla's model discovery does + /// ("Missing block model"). Mods' content gets this; vanilla's resources + /// always read. + pub(crate) fn block_library_or_missing( + &self, + root: &Path, + pending: Vec<String>, + ) -> Result<ModelLibrary> { + self.block_models(root, pending, true) + } + fn block_models( &self, root: &Path, mut pending: Vec<String>, + or_missing: bool, ) -> Result<ModelLibrary> { let mut models = BTreeMap::new(); + let mut skipped = std::collections::BTreeSet::new(); while let Some(name) = pending.pop() { - if models.contains_key(&name) { + if models.contains_key(&name) || skipped.contains(&name) { continue; } let model = match self.models.get(&name) { Some(model) => Arc::clone(model), None => { - let (namespace, path) = - name.split_once(':').ok_or("invalid model identifier")?; - let text = fs::read_to_string( - root.join(format!("assets/{namespace}/models/{path}.json")), - )?; - Arc::new(CuboidModel::from_json(&text)?) + let read = || -> Result<CuboidModel> { + let (namespace, path) = + name.split_once(':').ok_or("invalid model identifier")?; + let text = fs::read_to_string( + root.join(format!("assets/{namespace}/models/{path}.json")), + )?; + Ok(CuboidModel::from_json(&text)?) + }; + match read() { + Ok(model) => Arc::new(model), + Err(e) if or_missing => { + log::warn!("Horizon: missing block model {name}: {e}"); + skipped.insert(name); + continue; + } + Err(e) => return Err(e), + } } }; if let Some(parent) = &model.parent { @@ -141,6 +168,13 @@ mod tests { .to_string() }; assert_eq!(load(&direct), load(&parsed)); + // Mods' content: vanilla's missing model instead of an error. + let lenient = parsed.block_library_or_missing(&fixture.0, vec![name.into()])?; + assert_eq!( + format!("{:?}", lenient.resolve(name)?.elements()), + format!("{:?}", lenient.resolve(MISSING_MODEL)?.elements()), + "{name}" + ); } let items = parsed.into_item_library()?; let block = shared.resolve("horizon:nested/child")?; diff --git a/crates/horizon-client/src/models.rs b/crates/horizon-client/src/models.rs index 110166f29..5979672e9 100644 --- a/crates/horizon-client/src/models.rs +++ b/crates/horizon-client/src/models.rs @@ -117,13 +117,11 @@ pub fn load_visibility( use horizon_world::{ block_render::{FaceVisibility, RenderDefinition, SkipRendering}, collision::CachedCollisionRegistry, - nbt::{self, Limits}, + registry::BlockTableFile, }; use std::{collections::BTreeMap, fs}; - let data = nbt::read_named(&fs::read(root.join("occlusion.nbt"))?, Limits::tables())? - .0 - .tag; - let occlusion = CachedCollisionRegistry::from_nbt(&data, registry)?; + let file = BlockTableFile::open(&root.join("occlusion.nbt"), registry)?; + let occlusion = CachedCollisionRegistry::from_table(file.table("occlusion")?)?; let raw: BTreeMap<String, Value> = serde_json::from_slice(&fs::read(root.join("render-definitions.json"))?)?; let definitions = raw diff --git a/crates/horizon-client/src/models/catalog.rs b/crates/horizon-client/src/models/catalog.rs index ecedd0f95..be550d8c6 100644 --- a/crates/horizon-client/src/models/catalog.rs +++ b/crates/horizon-client/src/models/catalog.rs @@ -136,11 +136,29 @@ impl StateModels { }) .collect::<Result<_>>()?; let possible: Vec<Properties> = serde_json::from_value(facts["states"].clone())?; - let resource = fs::read_to_string( + // Vanilla's BlockStateModelLoader: a definition that does not + // read or bind is logged and its block gets no model; the other + // blocks still load. + let loaded = fs::read_to_string( root.join(format!("assets/{namespace}/blockstates/{path}.json")), - )?; - let definition = BlockStateDefinition::from_json(&resource)?; - let bound = definition.bind(&schema, &possible)?; + ) + .map_err(|e| e.to_string()) + .and_then(|text| BlockStateDefinition::from_json(&text).map_err(|e| e.to_string())) + .and_then(|definition| { + let bound = definition + .bind(&schema, &possible) + .map_err(|e| e.to_string())?; + Ok((definition, bound)) + }); + let (definition, bound) = match loaded { + Ok(loaded) => loaded, + Err(e) => { + log::warn!( + "Horizon: blockstate definition of {namespace}:{path} not loaded: {e}" + ); + continue; + } + }; let native_order: Vec<_> = by_name .get(name.as_str()) .map(Vec::as_slice) @@ -185,10 +203,14 @@ impl StateModels { } choices.push(CompiledChoices::new(&definition)?); } - let library = models.block_library( - root, - variants.iter().map(|(name, _, _)| name.clone()).collect(), - )?; + let names: Vec<String> = variants.iter().map(|(name, _, _)| name.clone()).collect(); + let library = match models.block_library(root, names.clone()) { + Ok(library) => library, + Err(e) => { + log::warn!("Horizon: block models incomplete ({e}); using the missing model"); + models.block_library_or_missing(root, names)? + } + }; let mut baked = BTreeMap::new(); for (name, turns, lock) in variants { let model = library.resolve(&name)?; @@ -231,14 +253,20 @@ impl StateModels { &self, state: BlockStateId, position: BlockPos, + ) -> Result<Vec<(&Variant, &BakedModel)>> { + // Blocks use their position seed; contained/displayed blocks can supply one. + self.collect_seeded(state, position_seed(position)) + } + pub fn collect_seeded( + &self, + state: BlockStateId, + seed: i64, ) -> Result<Vec<(&Variant, &BakedModel)>> { let (choice, selected) = self .states .get(&state) .ok_or("unbound gallery block state")?; - // These inspection blocks use Block's default position seed. Blocks with - // specialized model seeds (e.g. two-block structures) need their callbacks. - let mut random = LegacyRandom::new(position_seed(position)); + let mut random = LegacyRandom::new(seed); let mut variants = Vec::new(); self.choices[*choice].collect(selected, &mut random, &mut variants)?; variants diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index ae2486351..6fca66c1d 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -31,8 +31,7 @@ use horizon_world::{ movement_restitution::{self, RestitutionInput, RestitutionWorld}, movement_statistics::{self, MovementStat, MovementStatisticsWorld}, movement_support::{self, MovementLocation}, - nbt::{self, Limits}, - registry::BlockStateId, + registry::{BlockStateId, BlockTableFile}, world_movement::{self, MovementRequest}, worldgen::tags::TagDefinitions, }; @@ -57,17 +56,27 @@ pub struct MotionResources { pub fluids: FluidResources, } impl MotionResources { + /// [`Self::load_with_facts`] with the block facts decoded here. pub fn load(root: &Path, assets: &Assets) -> Result<Self> { + Self::load_with_facts( + root, + assets, + &crate::blocks::BlockFacts::load(root, assets.registry)?, + ) + } + /// The motion tables, with the block facts the game already decoded + /// (decoding `gameplay-facts.json` again costs a second or more and, with + /// a big modpack, hundreds of megabytes while it runs). + pub fn load_with_facts( + root: &Path, + assets: &Assets, + facts: &crate::blocks::BlockFacts, + ) -> Result<Self> { let attributes = Arc::new(Catalog::from_json(&serde_json::from_slice(&fs::read( root.join("attributes.json"), )?)?)?); - let data = nbt::read_named( - &fs::read(root.join("movement-properties.nbt"))?, - Limits::tables(), - )? - .0 - .tag; - let properties = MovementProperties::from_nbt(&data, assets.registry)?; + let file = BlockTableFile::open(&root.join("movement-properties.nbt"), assets.registry)?; + let properties = MovementProperties::from_table(file.table("movement")?)?; let bundle: BTreeMap<String, String> = serde_json::from_slice(&fs::read(root.join("block-tags.json"))?)?; let mut definitions = TagDefinitions::default(); @@ -91,7 +100,7 @@ impl MotionResources { Ok(Self { landing: crate::landing::load(root, &properties)?, health: crate::health::HealthResources::load(root)?, - fluids: FluidResources::load(root, assets)?, + fluids: FluidResources::new(assets, facts)?, attributes, blocks: properties.bind(&MovementTags { fences: tag("minecraft:fences")?, @@ -215,6 +224,8 @@ pub struct PlayerMotion { /// Spectator mode: no physics, always flying (`Player.tick`). pub spectator: bool, pub flying: bool, + /// Client-side `Abilities.flyingSpeed`, adjusted by the spectator scroll wheel. + pub flying_speed: f32, /// The jump key last tick (LocalPlayer.aiStep's wasJumping). pub was_jumping: bool, /// `LocalPlayer.autoJumpEnabled` (the Auto-Jump option) and @@ -281,19 +292,37 @@ fn attribute_error(error: impl std::fmt::Debug) -> io::Error { io::Error::other(format!("movement attribute: {error:?}")) } impl PlayerMotion { - pub fn load_environment_once( + /// `Player.isSweepAttack`, using the resident player's known horizontal velocity. + pub fn is_sweep_attack(&self, strength: f32, critical: bool, sword: bool) -> bool { + let movement = self.velocity; + let limit = f64::from(self.speed) * 2.5; + strength > 0.9 + && !critical + && !self.sprinting + && self.ground.on_ground + && movement.x * movement.x + movement.z * movement.z < limit * limit + && sword + } + /// Restore login state once; a later respawn starts with fresh health and food. + pub fn load_saved_state_once( &mut self, pending: &mut bool, - extra: Option<&horizon_world::nbt::Tag>, - ) { + level: &crate::save::Level, + ) -> Result<()> { if std::mem::take(pending) { + let extra = level.player_extra.as_ref(); self.fire_ticks = extra .and_then(|t| crate::save::number(t.get("Fire"))) .unwrap_or(-20) as i32; self.air = extra .and_then(|t| crate::save::number(t.get("Air"))) .unwrap_or(300) as i32; + self.set_health(level.health, false)?; + self.health.food.food = level.food; + self.health.food.saturation = level.saturation; + self.flying = level.flying; } + Ok(()) } pub fn set_remaining_fire_ticks(&mut self, ticks: i32) { self.fire_ticks = if self.invulnerable { @@ -360,6 +389,7 @@ impl PlayerMotion { invulnerable: false, spectator: false, flying: false, + flying_speed: 0.05, was_jumping: false, auto_jump_enabled: false, auto_jump_time: 0, @@ -622,7 +652,8 @@ impl MotionWorld<'_, '_, '_> { entities: self.entities, } } - pub(crate) fn no_player_collision(&mut self, bounds: Aabb) -> Result<bool> { + /// `Level.noCollision(player, bounds)`: blocks and the host's entity boxes. + pub fn no_player_collision(&mut self, bounds: Aabb) -> Result<bool> { let context = self.context(); Ok(world_movement::no_world_collision( &mut self.movement_collisions(), @@ -761,7 +792,7 @@ impl MotionWorld<'_, '_, '_> { // Vertical flight: jump up, shift down, three times the flying speed. let ya = i32::from(jumping) - i32::from(self.player.shift_key_down); if ya != 0 { - self.player.velocity.y += f64::from(ya as f32 * 0.05 * 3.0); + self.player.velocity.y += f64::from(ya as f32 * self.player.flying_speed * 3.0); } } self.dry_tick(TickInput::PreparedLocal { @@ -1835,8 +1866,8 @@ impl LivingAirWorld for MotionWorld<'_, '_, '_> { } fn flying_speed(&mut self) -> Result<f32> { if self.player.flying { - // Abilities.flyingSpeed 0.05, doubled when sprinting. - return Ok(if self.player.sprinting { 0.1 } else { 0.05 }); + // Player.getFlyingSpeed: abilities speed, doubled when sprinting. + return Ok(self.player.flying_speed * if self.player.sprinting { 2.0 } else { 1.0 }); } Ok(if self.player.sprinting { 0.025999999 @@ -1986,6 +2017,40 @@ impl LivingMotionWorld for MotionWorld<'_, '_, '_> { mod tests { use super::*; + #[test] + fn sword_sweep_requires_charge_ground_and_slow_horizontal_movement() { + let root = std::env::var_os("HORIZON_TEST_RESOURCES") + .map(std::path::PathBuf::from) + .unwrap_or_else(|| { + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3") + }); + if !root.join("movement-properties.nbt").exists() { + eprintln!("skipped sword sweep regression: prepared resources unavailable"); + return; + } + let assets = Assets::load(&root).unwrap(); + let resources = MotionResources::load(&root, &assets).unwrap(); + let mut p = PlayerMotion::new(&resources, Vec3::ZERO).unwrap(); + p.ground.on_ground = true; + p.speed = 0.1; + assert!(p.is_sweep_attack(1.0, false, true)); + assert!(!p.is_sweep_attack(0.9, false, true)); + assert!(!p.is_sweep_attack(1.0, true, true)); + assert!(!p.is_sweep_attack(1.0, false, false)); + p.sprinting = true; + assert!(!p.is_sweep_attack(1.0, false, true)); + p.sprinting = false; + p.ground.on_ground = false; + assert!(!p.is_sweep_attack(1.0, false, true)); + p.ground.on_ground = true; + p.velocity = Vec3::new(0.2, 10.0, 0.0); + assert!(p.is_sweep_attack(1.0, false, true)); + p.velocity.z = 0.2; + assert!(!p.is_sweep_attack(1.0, false, true)); + p.velocity = Vec3::new(f64::from(p.speed) * 2.5, 0.0, 0.0); + assert!(!p.is_sweep_attack(1.0, false, true)); + } + #[test] fn player_stands_walks_sneaks_and_jumps_on_ghast() { let root = std::env::var_os("HORIZON_TEST_RESOURCES") @@ -1997,9 +2062,10 @@ mod tests { return; } let assets = Assets::load(&root).unwrap(); + let render = assets.reload(&root).unwrap(); let binding = crate::collisions::load(&root, assets.registry).unwrap(); let resources = MotionResources::load(&root, &assets).unwrap(); - let mut scene = World::test_scene(&assets, 1).unwrap(); + let mut scene = World::test_scene(&assets, &render, 1).unwrap(); let entities = [Aabb::new( Vec3::new(10.0, 80.0, 10.0), Vec3::new(14.0, 84.0, 14.0), @@ -2060,6 +2126,47 @@ mod tests { assert!(player.position.y < 84.0); } + #[test] + fn spectator_scroll_speed_controls_horizontal_and_vertical_flight() { + let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3"); + if !root.join("movement-properties.nbt").exists() { + return; + } + let assets = Assets::load(&root).unwrap(); + let render = assets.reload(&root).unwrap(); + let binding = crate::collisions::load(&root, assets.registry).unwrap(); + let resources = MotionResources::load(&root, &assets).unwrap(); + let run = |steps| { + let mut scene = World::test_scene(&assets, &render, 2).unwrap(); + let start = Vec3::new(20.5, 12.0, 20.5); + let mut player = PlayerMotion::new(&resources, start).unwrap(); + player.spectator = true; + player.flying_speed = crate::mouse::spectator_flying_speed(player.flying_speed, steps); + MotionWorld { + entities: &[], + player: &mut player, + scene: &mut scene, + assets: &assets, + collision_binding: &binding, + resources: &resources, + } + .controlled_simulation_tick(crate::sprint::Keys { + forward: true, + jump: true, + ..Default::default() + }) + .unwrap(); + player.position.subtract(start) + }; + let stopped = run(-100); + assert_eq!(stopped, Vec3::ZERO); + let normal = run(0); + let fast = run(10); + assert!(normal.y > 0.0 && normal.z > 0.0); + assert!((fast.y - normal.y * 2.0).abs() < 1e-6); + assert!((fast.z - normal.z * 2.0).abs() < 1e-6); + } + #[test] fn resident_spectator_flight_ignores_solid_and_climbable_blocks() { let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3"); @@ -2067,10 +2174,11 @@ mod tests { return; } let assets = Assets::load(&root).unwrap(); + let render = assets.reload(&root).unwrap(); let binding = crate::collisions::load(&root, assets.registry).unwrap(); let resources = MotionResources::load(&root, &assets).unwrap(); let run = |name: &str, spectator: bool| { - let mut scene = World::test_scene(&assets, 2).unwrap(); + let mut scene = World::test_scene(&assets, &render, 2).unwrap(); let state = assets.registry.default_state(name).unwrap(); for x in 19..=23 { for y in 10..=17 { @@ -2121,8 +2229,13 @@ mod tests { #[test] fn player_fire_damage_queues_hurt_sound_only_for_fresh_hits() { - let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3"); + let root = std::env::var_os("HORIZON_TEST_RESOURCES") + .map(std::path::PathBuf::from) + .unwrap_or_else(|| { + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3") + }); if !root.join("movement-properties.nbt").exists() { + eprintln!("skipped damage regression: prepared resources unavailable"); return; } let assets = Assets::load(&root).unwrap(); @@ -2150,6 +2263,8 @@ mod tests { .count(), 1 ); + assert_eq!(player.health.hurt_animation_time, 10); + player.health.advance_clock(); player.events.clear(); assert!( !player @@ -2162,9 +2277,14 @@ mod tests { .iter() .any(|e| matches!(e, MotionEvent::HurtSound(_))) ); + assert_eq!( + player.health.hurt_animation_time, 9, + "cooldown hit must not restart animation" + ); for _ in 0..20 { player.health.advance_clock(); } + assert_eq!(player.health.hurt_animation_time, 0); player.invulnerable = true; player.events.clear(); assert!( @@ -2226,16 +2346,62 @@ mod tests { Default::default(), source, 4.0, - Some(shield), + Some(shield.clone()), ) .unwrap(); assert!(!outcome.hurt && outcome.sound && outcome.blocked == 4.0); + assert_eq!( + player.health.hurt.hurt_time, 10, + "preserve server damage bookkeeping" + ); + assert_eq!( + player.health.hurt_animation_time, 0, + "a fully blocked arrow must not animate hurt" + ); assert!( !player .events .iter() .any(|e| matches!(e, MotionEvent::HurtSound(_))) ); + // An unblocked hit after the shield block must still animate normally. + for _ in 0..20 { + player.health.advance_clock(); + } + assert!( + player + .hurt(&resources.health, Default::default(), source, 4.0) + .unwrap() + ); + assert_eq!(player.health.hurt_animation_time, 10); + player.health.advance_clock(); + assert!( + !player + .hurt_blocking( + &resources.health, + Default::default(), + source, + 4.0, + Some(shield) + ) + .unwrap() + .hurt + ); + assert_eq!( + player.health.hurt_animation_time, 9, + "blocking must not erase or restart an existing animation" + ); + let mut starving = PlayerMotion::new(&resources, Vec3::ZERO).unwrap(); + starving.health.food.food = 0; + starving.health.food.timer = 79; + starving + .food_tick(&resources.health, Default::default()) + .unwrap(); + assert_eq!(starving.health.health(), 19.0); + assert_eq!( + starving.health.hurt_animation_time, 10, + "starvation must still animate hurt" + ); } /// Footsteps on the prepared vanilla resources, when they are present: @@ -2248,10 +2414,11 @@ mod tests { return; } let assets = Assets::load(&root).unwrap(); + let render = assets.reload(&root).unwrap(); let binding = crate::collisions::load(&root, assets.registry).unwrap(); let resources = MotionResources::load(&root, &assets).unwrap(); for wet in [false, true] { - let mut scene = World::test_scene(&assets, 2).unwrap(); + let mut scene = World::test_scene(&assets, &render, 2).unwrap(); if wet { let water = assets.registry.default_state("minecraft:water").unwrap(); for x in 19..=21 { @@ -2284,14 +2451,20 @@ mod tests { #[test] fn resident_water_and_lava_are_not_treated_as_air() { - let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3"); + let root = std::env::var_os("HORIZON_TEST_RESOURCES") + .map(std::path::PathBuf::from) + .unwrap_or_else(|| { + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3") + }); if !root.join("movement-properties.nbt").exists() { + eprintln!("skipping resident water/lava test: prepared resources unavailable"); return; } let assets = Assets::load(&root).unwrap(); + let render = assets.reload(&root).unwrap(); let binding = crate::collisions::load(&root, assets.registry).unwrap(); let resources = MotionResources::load(&root, &assets).unwrap(); - let mut scene = World::test_scene(&assets, 3).unwrap(); + let mut scene = World::test_scene(&assets, &render, 3).unwrap(); let mut player = PlayerMotion::new(&resources, Vec3::new(20.5, 4.0, 20.5)).unwrap(); let water = assets.registry.default_state("minecraft:water").unwrap(); for x in 19..=21 { @@ -2491,12 +2664,44 @@ mod tests { ("Fire", horizon_world::nbt::Tag::Short(160)), ("Air", horizon_world::nbt::Tag::Short(8)), ]); + let mut level = crate::save::Level { + health: 13.0, + food: 11, + saturation: 2.25, + player_extra: Some(extra), + flying: true, + ..Default::default() + }; let mut pending = true; - w.player.load_environment_once(&mut pending, Some(&extra)); + w.player + .load_saved_state_once(&mut pending, &level) + .unwrap(); assert_eq!((w.player.fire_ticks, w.player.air), (160, 8)); + assert!(w.player.flying); + assert_eq!( + ( + w.player.health.health(), + w.player.health.food.food, + w.player.health.food.saturation + ), + (13.0, 11, 2.25) + ); let mut respawn = PlayerMotion::new(&resources, Vec3::new(20.5, 4.0, 20.5)).unwrap(); - respawn.load_environment_once(&mut pending, Some(&extra)); + respawn.load_saved_state_once(&mut pending, &level).unwrap(); assert_eq!((respawn.fire_ticks, respawn.air), (-20, 300)); + assert!(!respawn.flying); + assert_eq!( + (respawn.health.health(), respawn.health.food.food), + (20.0, 20) + ); + // Reopening another world restores its state, including a saved death. + pending = true; + level.health = 0.0; + w.player + .load_saved_state_once(&mut pending, &level) + .unwrap(); + assert!(w.player.health.dead_or_dying()); + assert!(!pending); w.player.fluids.was_touching_water = false; assert!( w.player.moving_slowly(), @@ -2511,9 +2716,10 @@ mod tests { return; } let assets = Assets::load(&root).unwrap(); + let render = assets.reload(&root).unwrap(); let binding = crate::collisions::load(&root, assets.registry).unwrap(); let resources = MotionResources::load(&root, &assets).unwrap(); - let mut scene = World::test_scene(&assets, 3).unwrap(); + let mut scene = World::test_scene(&assets, &render, 3).unwrap(); let water = assets.registry.default_state("minecraft:water").unwrap(); for y in 4..=6 { scene.set(20, y, 20, water).unwrap(); @@ -2583,10 +2789,11 @@ mod tests { return; } let assets = Assets::load(&root).unwrap(); + let render = assets.reload(&root).unwrap(); let binding = crate::collisions::load(&root, assets.registry).unwrap(); let resources = MotionResources::load(&root, &assets).unwrap(); for name in ["minecraft:lava", "minecraft:water"] { - let mut scene = World::test_scene(&assets, 3).unwrap(); + let mut scene = World::test_scene(&assets, &render, 3).unwrap(); let fluid = assets.registry.default_state(name).unwrap(); scene.set(20, 4, 20, fluid).unwrap(); let mut player = PlayerMotion::new(&resources, Vec3::new(20.5, 4.0, 20.5)).unwrap(); @@ -2634,9 +2841,10 @@ mod tests { return; } let assets = Assets::load(&root).unwrap(); + let render = assets.reload(&root).unwrap(); let binding = crate::collisions::load(&root, assets.registry).unwrap(); let resources = MotionResources::load(&root, &assets).unwrap(); - let mut scene = World::test_scene(&assets, 3).unwrap(); + let mut scene = World::test_scene(&assets, &render, 3).unwrap(); let water = assets.registry.default_state("minecraft:water").unwrap(); scene.set(20, 4, 20, water).unwrap(); scene.set(20, 5, 20, water).unwrap(); @@ -2703,10 +2911,11 @@ mod tests { return; } let assets = Assets::load(&root).unwrap(); + let render = assets.reload(&root).unwrap(); let binding = crate::collisions::load(&root, assets.registry).unwrap(); let resources = MotionResources::load(&root, &assets).unwrap(); for (water_level, lava_level, ground_jump) in [(0, 6, true), (6, 0, false)] { - let mut scene = World::test_scene(&assets, 3).unwrap(); + let mut scene = World::test_scene(&assets, &render, 3).unwrap(); for (name, level, x) in [ ("minecraft:water", water_level, 20), ("minecraft:lava", lava_level, 21), @@ -2751,7 +2960,8 @@ mod tests { return; } let assets = Assets::load(&root).unwrap(); - let mut scene = World::test_scene(&assets, 3).unwrap(); + let render = assets.reload(&root).unwrap(); + let mut scene = World::test_scene(&assets, &render, 3).unwrap(); let binding = crate::collisions::load(&root, assets.registry).unwrap(); let resources = MotionResources::load(&root, &assets).unwrap(); let mut player = PlayerMotion::new(&resources, Vec3::new(20.5, 4.0, 20.5)).unwrap(); diff --git a/crates/horizon-client/src/motion/fluids.rs b/crates/horizon-client/src/motion/fluids.rs index f85f755a9..89affeb64 100644 --- a/crates/horizon-client/src/motion/fluids.rs +++ b/crates/horizon-client/src/motion/fluids.rs @@ -33,8 +33,7 @@ pub struct FluidResources { pub shapes: Mutex<FluidShapeCache>, } impl FluidResources { - pub fn load(root: &Path, assets: &Assets) -> Result<Self> { - let facts = crate::blocks::BlockFacts::load(root, assets.registry)?; + pub fn new(assets: &Assets, facts: &crate::blocks::BlockFacts) -> Result<Self> { Ok(Self { states: FluidStateTable::new(assets.registry)?, sturdy: facts.states.iter().map(|s| s.sturdy_full).collect(), diff --git a/crates/horizon-client/src/mouse.rs b/crates/horizon-client/src/mouse.rs index d2794d790..1cee2be87 100644 --- a/crates/horizon-client/src/mouse.rs +++ b/crates/horizon-client/src/mouse.rs @@ -80,6 +80,12 @@ impl ScrollWheelHandler { } } +/// `MouseHandler.onScroll` with the spectator menu closed. Only vertical whole +/// wheel steps adjust `Abilities.flyingSpeed`; horizontal scrolling does not. +pub fn spectator_flying_speed(speed: f32, vertical_steps: i32) -> f32 { + (speed + vertical_steps as f32 * 0.005).clamp(0.0, 0.2) +} + /// Entity rotation as `Entity.turn` keeps it. #[derive(Clone, Copy, Debug, PartialEq)] pub struct Rotation { diff --git a/crates/horizon-client/src/multiplayer.rs b/crates/horizon-client/src/multiplayer.rs index 60a5b2277..2228d32a2 100644 --- a/crates/horizon-client/src/multiplayer.rs +++ b/crates/horizon-client/src/multiplayer.rs @@ -204,6 +204,14 @@ fn save_icon(file: &Path, address: &str, icon: Option<String>) { } } +/// `ClientPacketListener.handleServerData`: a valid icon the joined server +/// sends replaces the one its list entries keep. +pub fn save_server_icon(saves: &Path, address: &str, png: &[u8]) { + if icon_pixels(png).is_some() { + save_icon(&servers_file(saves), address, Some(crate::lan::base64(png))); + } +} + /// The address of list row `index`: the saved servers, then the LAN games. fn entry_address(servers: &[ServerEntry], lan: &Scanner, index: usize) -> Option<String> { match servers.get(index) { diff --git a/crates/horizon-client/src/net.rs b/crates/horizon-client/src/net.rs index f9ad5992a..74f1b0b6d 100644 --- a/crates/horizon-client/src/net.rs +++ b/crates/horizon-client/src/net.rs @@ -1,7 +1,8 @@ //! Multiplayer session: a network thread that connects to a server, logs in, //! runs the configuration phase and then forwards decoded play packets to the -//! game thread. Keep-alives and pings are answered on the network thread like -//! vanilla's connection handlers so a busy frame never times the client out. +//! game thread. Keep-alives are answered on the network thread; pings wait for +//! the game thread, like ClientCommonPacketListenerImpl.handlePing, so a pong +//! cannot overtake an earlier world update or teleport acknowledgement. //! Online-mode servers need the Microsoft account the launcher signed in //! (crate::auth): the login joins the server through the session service and //! encrypts the connection (ClientHandshakePacketListenerImpl.handleHello). @@ -382,8 +383,17 @@ fn run( uuid: profile.uuid, name: profile.name, }); + receive_play(&mut reader, shared_writer, lang, send) +} + +fn receive_play( + reader: &mut ConnReader, + shared_writer: &Mutex<Option<ConnWriter>>, + lang: &Language, + send: &Sender<Event>, +) -> Result<String, String> { loop { - let (id, payload) = reader.recv().map_err(e)?; + let (id, payload) = reader.recv().map_err(|e| e.to_string())?; let packet = match play::Clientbound::decode(id, &payload) { Ok(p) => p, Err(err) => { @@ -404,7 +414,6 @@ fn run( }; match packet { play::Clientbound::KeepAlive(k) => reply(play::Serverbound::KeepAlive(k)), - play::Clientbound::Ping(p) => reply(play::Serverbound::Pong(p)), play::Clientbound::BundleDelimiter(_) => {} play::Clientbound::ResourcePackPush(p) => { // Declined: Horizon has no server resource packs. @@ -415,7 +424,7 @@ fn run( } play::Clientbound::StartConfiguration(_) => { reply(play::Serverbound::ConfigurationAcknowledged(Empty)); - let registries = configure(&mut reader, shared_writer, lang, send)?; + let registries = configure(reader, shared_writer, lang, send)?; let _ = send.send(Event::Reconfigured(Arc::new(registries))); } play::Clientbound::Disconnect(d) => return Ok(plain(&d.reason, lang)), @@ -585,6 +594,50 @@ pub fn ping(address: &str, timeout: Duration) -> Result<(serde_json::Value, u128 mod tests { use super::*; + #[test] + fn pings_wait_behind_teleports_but_keepalives_do_not_wait_for_the_game() { + let (client, server) = horizon_protocol::connection::Pipe::pair(); + let (mut reader, writer) = Connection::local(client).split(); + let writer = Arc::new(Mutex::new(Some(writer))); + let (send, events) = channel(); + let thread_writer = writer.clone(); + let thread = std::thread::spawn(move || { + receive_play(&mut reader, &thread_writer, &Language::new(), &send) + }); + let mut server = Connection::local(server); + let teleport = play::Clientbound::PlayerPosition(play::PlayerPosition { + teleport: 42, + position: [10.0, 64.0, 10.0], + delta: [0.0; 3], + yaw: 90.0, + pitch: 0.0, + relatives: 0, + }); + server.send(&teleport.encode()).unwrap(); + server + .send(&play::Clientbound::Ping(cfg::Ping { id: 7 }).encode()) + .unwrap(); + // A reply to this proves the receive loop has passed the ping. The + // old implementation instead replies with Pong before this keepalive. + server + .send(&play::Clientbound::KeepAlive(cfg::KeepAlive { id: 9 }).encode()) + .unwrap(); + let (id, body) = server.recv().unwrap(); + assert_eq!( + play::Serverbound::decode(id, &body).unwrap(), + play::Serverbound::KeepAlive(cfg::KeepAlive { id: 9 }) + ); + let timeout = Duration::from_secs(5); + assert!( + matches!(events.recv_timeout(timeout).unwrap(), Event::Packet(p) if *p == teleport) + ); + assert!( + matches!(events.recv_timeout(timeout).unwrap(), Event::Packet(p) if *p == play::Clientbound::Ping(cfg::Ping { id: 7 })) + ); + writer.lock().unwrap().take().unwrap().shutdown(); + assert!(thread.join().unwrap().is_err()); + } + #[test] fn server_brand_payload_is_decoded_and_invalid_or_unrelated_payloads_are_ignored() { let (send, events) = channel(); diff --git a/crates/horizon-client/src/options_file.rs b/crates/horizon-client/src/options_file.rs index 339bb0fdf..49c94613f 100644 --- a/crates/horizon-client/src/options_file.rs +++ b/crates/horizon-client/src/options_file.rs @@ -159,7 +159,7 @@ static TABLE: LazyLock<Vec<Def>> = LazyLock::new(|| { b("reducedDebugInfo", false), def("renderClouds", O(Clouds), V::Int(2)), i("cloudRange", 2, 128, 128), - i("renderDistance", 2, 32, 12), + i("renderDistance", 2, 64, 12), i("simulationDistance", 5, 32, 12), u("screenEffectScale", 1.0), def("soundDevice", O(AnyStr), V::Str(String::new())), diff --git a/crates/horizon-client/src/options_horizon.rs b/crates/horizon-client/src/options_horizon.rs index 634bb8d75..b0a64c7e5 100644 --- a/crates/horizon-client/src/options_horizon.rs +++ b/crates/horizon-client/src/options_horizon.rs @@ -36,6 +36,8 @@ pub enum HRow { Extra(extras::Item), /// Installed mod `i` (`settings.mods.installed`) and its switch. Mod(usize), + /// Opens the directory where Horizon mods are installed. + OpenModFolder, } /// A sidebar section: name, description, rows. pub struct Section { @@ -192,12 +194,15 @@ pub const SECTIONS: &[Section] = &[ name: "Mods", blurb: "Mods made for Horizon, from the horizon-mods folder. Each runs in a sandbox and stays off until you turn it on; check what it may do first.", rows: |s| { + let mut rows = vec![HRow::OpenModFolder]; if s.mods.installed.is_empty() { - return vec![HRow::Header( + rows.push(HRow::Header( "No mods: put .hmod files in the horizon-mods folder", - )]; + )); + } else { + rows.extend((0..s.mods.installed.len()).map(HRow::Mod)); } - (0..s.mods.installed.len()).map(HRow::Mod).collect() + rows }, }, ]; @@ -207,8 +212,15 @@ const DONE: u64 = id("options.done", 0); const ROW: u64 = id("options.row", 0); const STYLE: u64 = id("options.style", 0); const LEVEL: u64 = id("options.level", 0); +const SEARCH: u64 = id("options.search", 0); const ROW_H: f32 = 12.0 + CONTROL + 12.0; const HEADER_H: f32 = 40.0; +pub(crate) fn searchable_section(section: usize) -> bool { + matches!( + SECTIONS.get(section).map(|s| s.name), + Some("Video" | "Controls") + ) +} /// Whether an option is an on/off switch, and its value. fn switch_value(opt: Opt, s: &Settings) -> Option<bool> { @@ -258,20 +270,51 @@ fn caption_value(opt: Opt, s: &Settings) -> (String, String) { enum Target { Nav(usize), Done, + Search, Row(usize), Slider(Item, f32), Style(usize), Level(usize), } +struct HorizonScrollbar { + x: f32, + top: f32, + bottom: f32, + max: f32, + thumb: f32, +} +impl HorizonScrollbar { + fn contains(&self, x: f32, y: f32) -> bool { + x >= self.x && x < self.x + 6.0 && y >= self.top && y < self.bottom + } +} +struct HorizonLayout { + sliders: Vec<(Item, f32, f32)>, + scrollbar: Option<HorizonScrollbar>, +} +fn horizon_batch(framebuffer: [i32; 2]) -> HudBatch { + HudBatch::new([framebuffer[0].max(1) as u32, framebuffer[1].max(1) as u32]) +} impl OptionsUi { + fn horizon_rows(&self, s: &Settings, section: usize) -> Vec<HRow> { + let rows = (SECTIONS[section].rows)(s); + let query = self.settings_search.value.to_lowercase(); + if query.is_empty() || !searchable_section(section) { + return rows; + } + rows.into_iter() + .filter(|row| match row { + HRow::Opt(opt) => caption_value(*opt, s).0.to_lowercase().contains(&query), + HRow::Graphics(item, ..) => item.caption_value(s).0.to_lowercase().contains(&query), + HRow::Page(label, ..) => label.to_lowercase().contains(&query), + HRow::Level => true, + _ => false, + }) + .collect() + } /// Lay out (and draw, on a real frame) the Horizon options shell. - /// Returns the sliders' tracks (option, x, width) for dragging. - fn horizon_layout( - &mut self, - s: &Settings, - ui: &mut Ui, - backdrop: f32, - ) -> Vec<(Item, f32, f32)> { + /// Returns the sliders' tracks and the scroll area for pointer input. + fn horizon_layout(&mut self, s: &Settings, ui: &mut Ui, backdrop: f32) -> HorizonLayout { let (w, h) = (ui.w, ui.h); if self.in_world { ui.atmosphere(horizon_theme::VEIL, 1.0); @@ -307,8 +350,17 @@ impl OptionsUi { let sec = &SECTIONS[section]; let mut y = 36.0; y += ui.page_header(x, y, sec.name, sec.blurb) + 24.0; + if searchable_section(section) { + ui.text_field( + SEARCH, + [x + cw - 240.0, y, 240.0, CONTROL], + &self.settings_search.value, + self.settings_search.focused, + ); + y += CONTROL + 12.0; + } let top = y; - let rows = (sec.rows)(s); + let rows = self.horizon_rows(s, section); let content: f32 = rows .iter() .map(|r| { @@ -436,6 +488,18 @@ impl OptionsUi { ui.described_row(x, y, &installed.title, "", 0.0, cost, text, cw - 60.0); ui.switch(ROW + i as u64, control_x, y + ROW_H / 2.0, s.mods.is_on(m)); } + HRow::OpenModFolder => { + ui.row(x, y, "Mod folder", "", 0.0); + let label = "Open Folder"; + let bw = ui.button_width(label, false).max(96.0); + ui.button( + ROW + i as u64, + [control_x - bw, y + 12.0, bw, CONTROL], + label, + Button::Secondary, + true, + ); + } HRow::Extra(item) => { let cy = y + ROW_H / 2.0; let id = ROW + i as u64; @@ -499,6 +563,7 @@ impl OptionsUi { .filter(|h| h.rect[1] >= cy0 && h.rect[3] <= cy1) .collect(); ui.hits.extend(kept); + let mut scrollbar = None; if max > 0.0 { // A thin scroll indicator. let track = bottom - top; @@ -512,11 +577,18 @@ impl OptionsUi { 1.5, color::with_alpha(color::MUTED, 0.35), ); + scrollbar = Some(HorizonScrollbar { + x: x + cw + 14.0, + top: top + oy, + bottom: bottom + oy, + max, + thumb: bar, + }); } let _ = ox; ui.opacity = 1.0; ui.offset = (0.0, 0.0); - sliders + HorizonLayout { sliders, scrollbar } } pub(crate) fn draw_horizon( &mut self, @@ -530,38 +602,67 @@ impl OptionsUi { self.horizon_layout(s, &mut ui, backdrop); ui.finish(); } - fn horizon_target(&mut self, s: &Settings, atlas: &HudAtlas, x: f32, y: f32) -> Option<Target> { - let mut batch = t::scratch_batch(); + fn horizon_target( + &mut self, + s: &Settings, + atlas: &HudAtlas, + x: f32, + y: f32, + framebuffer: [i32; 2], + ) -> (Option<Target>, Option<HorizonScrollbar>, (f32, f32)) { + let mut batch = horizon_batch(framebuffer); let mut ui = Ui::dry(atlas, &mut batch, (x, y)); - let sliders = self.horizon_layout(s, &mut ui, 1.0); - let hit = ui.hit((x, y))?; + let layout = self.horizon_layout(s, &mut ui, 1.0); + let scrollbar = layout.scrollbar; + // The UI layer transforms raw GUI-pixel input into its own dp. + let cursor = ui.cursor; + let Some(hit) = ui.hit((x, y)) else { + return (None, scrollbar, cursor); + }; + if hit == SEARCH { + return (Some(Target::Search), scrollbar, cursor); + } let section = self.section.min(SECTIONS.len() - 1); if (NAV..NAV + SECTIONS.len() as u64).contains(&hit) { - return Some(Target::Nav((hit - NAV) as usize)); + return (Some(Target::Nav((hit - NAV) as usize)), scrollbar, cursor); } if hit == DONE { - return Some(Target::Done); + return (Some(Target::Done), scrollbar, cursor); } if hit == STYLE || hit == STYLE + 1 { - return Some(Target::Style((hit - STYLE) as usize)); + return ( + Some(Target::Style((hit - STYLE) as usize)), + scrollbar, + cursor, + ); } if hit == LEVEL || hit == LEVEL + 1 { - return Some(Target::Level((hit - LEVEL) as usize)); + return ( + Some(Target::Level((hit - LEVEL) as usize)), + scrollbar, + cursor, + ); } - let i = hit.checked_sub(ROW)? as usize; - let rows = (SECTIONS[section].rows)(s); + let Some(i) = hit.checked_sub(ROW).map(|i| i as usize) else { + return (None, scrollbar, cursor); + }; + let rows = self.horizon_rows(s, section); let item = match rows.get(i) { Some(HRow::Opt(opt)) => Some(Item::Vanilla(*opt)), Some(HRow::Graphics(item, ..)) => Some(*item), _ => None, }; if let Some(item) = item - && let Some(&(_, sx, sw)) = sliders.iter().find(|(o, ..)| *o == item) + && let Some(&(_, sx, sw)) = layout.sliders.iter().find(|(o, ..)| *o == item) { let v = t::slider_value(&ui, sx, sw, x); - return Some(Target::Slider(item, v)); + return (Some(Target::Slider(item, v)), scrollbar, cursor); } - (i < rows.len()).then_some(Target::Row(i)) + ( + (i < rows.len()).then_some(Target::Row(i)), + scrollbar, + cursor, + ) } /// A click (or a drag with the button held) on the Horizon shell. pub(crate) fn click_horizon( @@ -576,14 +677,37 @@ impl OptionsUi { ) -> Vec<Event> { let mut events = Vec::new(); if drag { - if let Some(item) = self.hdragging { - let mut batch = t::scratch_batch(); - let mut ui = Ui::dry(atlas, &mut batch, (x, y)); - let sliders = self.horizon_layout(s, &mut ui, 1.0); - if let Some(&(_, sx, sw)) = sliders.iter().find(|(o, ..)| *o == item) { - set_item_slider(item, s, t::slider_value(&ui, sx, sw, x)); - events.push(Event::Changed); + let mut batch = horizon_batch(framebuffer); + let mut ui = Ui::dry(atlas, &mut batch, (x, y)); + let layout = self.horizon_layout(s, &mut ui, 1.0); + // Scrollbar geometry and stored drag positions are in Horizon dp. + let pointer_y = ui.cursor.1; + if let Some(previous_y) = self.scrollbar_drag_y { + if let Some(scrollbar) = layout.scrollbar { + // AbstractScrollArea.mouseDragged (inherited by AbstractSelectionList) + // moves by the pointer delta and sends out-of-range drags to an end. + let track = scrollbar.bottom - scrollbar.top; + self.hscroll = if pointer_y < scrollbar.top { + 0.0 + } else if pointer_y > scrollbar.bottom { + scrollbar.max + } else { + self.hscroll + + (pointer_y - previous_y) * scrollbar.max + / (track - scrollbar.thumb).max(1.0) + }; + self.hscroll = self.hscroll.clamp(0.0, scrollbar.max); + self.scrollbar_drag_y = Some(pointer_y); + } else { + self.scrollbar_drag_y = None; } + return events; + } + if let Some(item) = self.hdragging + && let Some(&(_, sx, sw)) = layout.sliders.iter().find(|(o, ..)| *o == item) + { + set_item_slider(item, s, t::slider_value(&ui, sx, sw, x)); + events.push(Event::Changed); } return events; } @@ -598,11 +722,28 @@ impl OptionsUi { }, ); } - match self.horizon_target(s, atlas, x, y) { + let (target, scrollbar, cursor) = self.horizon_target(s, atlas, x, y, framebuffer); + self.settings_search.focused = false; + // AbstractScrollArea.updateScrolling grabs anywhere in the scrollbar column. + if !right + && let Some(scrollbar) = scrollbar + && scrollbar.contains(cursor.0, cursor.1) + { + self.scrollbar_drag_y = Some(cursor.1); + return events; + } + match target { Some(Target::Nav(i)) => { + if searchable_section(self.section) || searchable_section(i) { + self.settings_search.set_value(""); + } self.section = i; self.hscroll = 0.0; } + Some(Target::Search) => { + self.settings_search.focused = true; + self.settings_search.move_cursor_to_end(false); + } Some(Target::Done) => events.extend(self.back(s)), Some(Target::Style(i)) => { t::set_style(if i == 1 { @@ -626,7 +767,7 @@ impl OptionsUi { } Some(Target::Row(i)) => { let section = self.section.min(SECTIONS.len() - 1); - match (SECTIONS[section].rows)(s).get(i) { + match self.horizon_rows(s, section).get(i) { Some(HRow::Opt(opt)) => { opt.cycle(s, right, framebuffer); if *opt == Opt::Preset { @@ -650,6 +791,10 @@ impl OptionsUi { s.mods.toggle(*m); events.push(Event::Changed); } + Some(HRow::OpenModFolder) if !right => { + events.push(Event::ButtonClick); + events.push(Event::OpenModFolder); + } _ => {} } } @@ -670,3 +815,211 @@ fn set_item_slider(item: Item, s: &mut Settings, v: f32) { Item::Horizon(_) => item.set_slider(s, v), } } + +#[cfg(test)] +mod tests { + use super::*; + + fn atlas() -> HudAtlas { + HudAtlas::build(std::path::Path::new("/nonexistent/horizon-test-assets")).unwrap() + } + + fn section_ui(name: &str) -> OptionsUi { + let mut ui = OptionsUi::default(); + ui.section = SECTIONS + .iter() + .position(|section| section.name == name) + .unwrap(); + ui + } + + fn extras_ui() -> OptionsUi { + section_ui("Extras") + } + + fn scrollbar( + ui: &mut OptionsUi, + settings: &Settings, + atlas: &HudAtlas, + framebuffer: [i32; 2], + ) -> HorizonScrollbar { + let mut batch = horizon_batch(framebuffer); + let mut draw = Ui::dry(atlas, &mut batch, (0.0, 0.0)); + ui.horizon_layout(settings, &mut draw, 1.0) + .scrollbar + .unwrap() + } + + fn gui_pointer(atlas: &HudAtlas, framebuffer: [i32; 2], point: (f32, f32)) -> (f32, f32) { + let mut batch = horizon_batch(framebuffer); + let ui = Ui::dry(atlas, &mut batch, (0.0, 0.0)); + let gui_per_dp = ui.density / ui.batch.scale; + (point.0 * gui_per_dp, point.1 * gui_per_dp) + } + + #[test] + fn horizon_scrollbar_track_drag_is_bounded_and_stops_on_release() { + let atlas = atlas(); + let framebuffer = [2560, 1440]; + let mut settings = Settings::default(); + let mut ui = section_ui("Controls"); + let bar = scrollbar(&mut ui, &settings, &atlas, framebuffer); + let max = bar.max; + let track = bar.bottom - bar.top; + assert!(max > 0.0); + + // Start on the track below the thumb, then move horizontally away; + // the held scrollbar keeps following the pointer. + let x = bar.x + 2.0; + let y = bar.top + bar.thumb + (track - bar.thumb) / 2.0; + assert!(y < bar.bottom); + let (click_x, click_y) = gui_pointer(&atlas, framebuffer, (x, y)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + click_x, + click_y, + framebuffer, + false, + false, + ) + .is_empty() + ); + assert_eq!(ui.hscroll, 0.0); + let delta = 5.0; + let (drag_x, drag_y) = gui_pointer(&atlas, framebuffer, (x - 100.0, y + delta)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + drag_x, + drag_y, + framebuffer, + true, + false, + ) + .is_empty() + ); + assert!((ui.hscroll - delta * max / (track - bar.thumb)).abs() < 0.001); + + let (below_x, below_y) = gui_pointer(&atlas, framebuffer, (x, bar.bottom + 100.0)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + below_x, + below_y, + framebuffer, + true, + false, + ) + .is_empty() + ); + assert_eq!(ui.hscroll, max); + let (above_x, above_y) = gui_pointer(&atlas, framebuffer, (x, bar.top - 100.0)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + above_x, + above_y, + framebuffer, + true, + false, + ) + .is_empty() + ); + assert_eq!(ui.hscroll, 0.0); + + assert!(ui.release().is_empty()); + let (below_x, below_y) = gui_pointer(&atlas, framebuffer, (x, bar.bottom + 100.0)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + below_x, + below_y, + framebuffer, + true, + false, + ) + .is_empty() + ); + assert_eq!(ui.hscroll, 0.0); + assert_eq!(settings.keys, Settings::default().keys); + } + + #[test] + fn horizon_key_rebinding_takes_precedence_over_scrollbar_press() { + let atlas = atlas(); + let framebuffer = [2560, 1440]; + let mut settings = Settings::default(); + let mut ui = extras_ui(); + let bar = scrollbar(&mut ui, &settings, &atlas, framebuffer); + let dp_y = bar.top + bar.thumb + (bar.bottom - bar.top - bar.thumb) / 2.0; + ui.selected_key = Some(extras::SORT_KEY); + let (x, y) = gui_pointer(&atlas, framebuffer, (bar.x + 2.0, dp_y)); + + assert_eq!( + ui.click_horizon(&mut settings, &atlas, x, y, framebuffer, false, false,), + vec![Event::Changed] + ); + assert_eq!(settings.extras.sort_key, "key.mouse.left"); + assert_eq!(ui.selected_key, None); + assert_eq!(ui.hscroll, 0.0); + + let (x, drag_y) = gui_pointer(&atlas, framebuffer, (bar.x + 2.0, dp_y + 10.0)); + ui.click_horizon(&mut settings, &atlas, x, drag_y, framebuffer, true, false); + assert_eq!(ui.hscroll, 0.0); + } + + #[test] + fn mods_section_opens_folder_from_its_button() { + let atlas = atlas(); + let framebuffer = [2560, 1440]; + let mut options = section_ui("Mods"); + let mut settings = Settings::default(); + let mut batch = horizon_batch(framebuffer); + let mut ui = Ui::dry(&atlas, &mut batch, (0.0, 0.0)); + options.horizon_layout(&settings, &mut ui, 1.0); + let rect = ui + .hits + .iter() + .find(|hit| hit.id == ROW) + .expect("Mods section has an Open Folder button") + .rect; + let scale = ui.density / ui.batch.scale; + let cursor = ( + (rect[0] + rect[2]) * 0.5 * scale, + (rect[1] + rect[3]) * 0.5 * scale, + ); + drop(ui); + + assert_eq!( + options.click_horizon( + &mut settings, + &atlas, + cursor.0, + cursor.1, + framebuffer, + false, + false + ), + vec![Event::ButtonClick, Event::OpenModFolder] + ); + assert!( + options + .click_horizon( + &mut settings, + &atlas, + cursor.0, + cursor.1, + framebuffer, + false, + true + ) + .is_empty() + ); + } +} diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs index 62a2ac91b..884b0dd6b 100644 --- a/crates/horizon-client/src/options_ui.rs +++ b/crates/horizon-client/src/options_ui.rs @@ -190,7 +190,7 @@ impl Opt { let unit = |v: f32, lo: f32, hi: f32| Kind::Slider(((v - lo) / (hi - lo)).clamp(0.0, 1.0)); match self { Opt::Fov => unit(s.fov, 30.0, 110.0), - Opt::RenderDistance => unit(s.render_distance as f32, 2.0, 32.0), + Opt::RenderDistance => unit(s.render_distance as f32, 2.0, 64.0), Opt::SimulationDistance => unit(s.simulation_distance as f32, 5.0, 32.0), Opt::Sensitivity => Kind::Slider(s.sensitivity), Opt::WheelSensitivity => unit(s.mouse_wheel_sensitivity, 0.01, 10.0), @@ -484,7 +484,7 @@ impl Opt { let step = |lo: f32, hi: f32| (lo + v * (hi - lo)).round(); match self { Opt::Fov => s.fov = step(30.0, 110.0), - Opt::RenderDistance => s.render_distance = step(2.0, 32.0) as i32, + Opt::RenderDistance => s.render_distance = step(2.0, 64.0) as i32, Opt::SimulationDistance => s.simulation_distance = step(5.0, 32.0) as i32, Opt::Sensitivity => s.sensitivity = v, Opt::WheelSensitivity => s.mouse_wheel_sensitivity = 0.01 + v * 9.99, @@ -734,6 +734,7 @@ enum Act { /// Back to Horizon's own menus (Horizon only, see `list_rows`). HorizonMenus, ToggleMod(usize), + OpenModFolder, } enum WKind { Button { @@ -780,6 +781,7 @@ pub enum Event { /// The pack selection changed (reload resources). Packs(Vec<String>), OpenPackFolder, + OpenModFolder, } /// `SliderableValueSet.applyValueImmediately`: these apply 600 ms after /// the last change (`OptionInstanceSliderButton.delayedApplyAt`). @@ -806,7 +808,8 @@ pub struct OptionsUi { /// when it last moved: the handle follows the pointer while the option /// keeps its stepped value, then snaps to it (`resetValue`). slider_value: Option<(Opt, f32, Instant)>, - scrollbar_drag_y: Option<f32>, + /// The pointer's last y while a Classic or Horizon scrollbar is held. + pub(crate) scrollbar_drag_y: Option<f32>, /// Horizon style: the selected sidebar section, the one last drawn and /// the rows' scroll in dp (`options_horizon`). pub section: usize, @@ -823,6 +826,7 @@ pub struct OptionsUi { pub languages: Vec<LanguageInfo>, language_choice: Option<String>, language_search: ui::EditBox, + pub(crate) settings_search: ui::EditBox, /// Packs: the packs found and the selection the screen opens with. pub pack_infos: Vec<crate::pack_screen::PackInfo>, pub pack_selection: Vec<String>, @@ -841,6 +845,8 @@ impl Default for OptionsUi { fn default() -> Self { let mut search = ui::EditBox::new(0.0, 0.0, 200.0, 15.0); search.max_length = 32; + let mut settings_search = ui::EditBox::new(0.0, 0.0, 200.0, 15.0); + settings_search.focused = false; Self { stack: vec![Page::Main], scroll: 0.0, @@ -857,6 +863,7 @@ impl Default for OptionsUi { languages: Vec::new(), language_choice: None, language_search: search, + settings_search, pack_infos: Vec::new(), pack_selection: Vec::new(), pack_screen: None, @@ -918,6 +925,13 @@ fn pairs(opts: &[Opt]) -> Vec<Row> { .collect() } impl OptionsUi { + fn settings_search_active(&self) -> bool { + self.settings_search.focused + && (matches!(self.page(), Page::Video | Page::Controls | Page::KeyBinds) + || (crate::ui_theme::horizon() + && self.page() == Page::Main + && crate::options_horizon::searchable_section(self.section))) + } pub fn page(&self) -> Page { *self.stack.last().unwrap_or(&Page::Main) } @@ -945,6 +959,10 @@ impl OptionsUi { self.stack.push(page); self.scroll = 0.0; self.scrollbar_drag_y = None; + if matches!(page, Page::Video | Page::Controls | Page::KeyBinds) { + self.settings_search.set_value(""); + self.settings_search.focused = false; + } if page == Page::Language { self.language_choice = None; self.language_search.set_value(""); @@ -993,6 +1011,33 @@ impl OptionsUi { _ => 163.0, } } + fn filter_option_rows(&self, rows: Vec<Row>) -> Vec<Row> { + let query = self.settings_search.value.to_lowercase(); + if query.is_empty() { + return rows; + } + rows.into_iter() + .flat_map(|row| match row { + Row::Big(opt) if lang::get(opt.caption()).to_lowercase().contains(&query) => { + vec![Row::Big(opt)] + } + Row::Pair(a, b) => { + let matches = + |opt: Opt| lang::get(opt.caption()).to_lowercase().contains(&query); + pairs( + &[Some(a), b] + .into_iter() + .flatten() + .filter(|opt| matches(*opt)) + .collect::<Vec<_>>(), + ) + } + // Keep navigation available and omit section headers with no visible options. + Row::Buttons(a, b) => vec![Row::Buttons(a, b)], + _ => Vec::new(), + }) + .collect() + } fn list_rows(&self, s: &Settings) -> Vec<Row> { match self.page() { Page::Extras => crate::extras::rows() @@ -1080,7 +1125,7 @@ impl OptionsUi { Opt::AttackIndicator, Opt::ChunkFade, ])); - rows + self.filter_option_rows(rows) } Page::Sound => { let mut rows = vec![Row::Big(Opt::Volume(0))]; @@ -1122,7 +1167,7 @@ impl OptionsUi { ), None, )); - rows + self.filter_option_rows(rows) } Page::Controller => { let mut rows = vec![Row::Buttons( @@ -1227,6 +1272,10 @@ impl OptionsUi { let mut out = Vec::new(); let page = self.page(); let cx = (w / 2.0).floor(); + if matches!(page, Page::Video | Page::Controls | Page::KeyBinds) { + self.settings_search.x = cx - 100.0; + self.settings_search.y = 17.0; + } let done = |y: f32| button(cx - 100.0, y, 200.0, tr("gui.done"), Act::Done); // (list top, list bottom, content height) let (mut top, mut bottom, mut content) = (33.0, h - 33.0, 0.0); @@ -1276,9 +1325,10 @@ impl OptionsUi { bottom = 0.0; } Page::KeyBinds => { + top = 38.0; out.push(Widget { x: 0.0, - y: 12.0, + y: 4.0, w, h: 9.0, kind: WKind::Text { @@ -1290,6 +1340,10 @@ impl OptionsUi { }); let mut mappings: Vec<(&str, &str)> = MAPPINGS.iter().map(|(n, _, c)| (*n, *c)).collect(); + let query = self.settings_search.value.to_lowercase(); + mappings.retain(|(name, _)| { + query.is_empty() || lang::get(name).to_lowercase().contains(&query) + }); mappings.sort_by(|a, b| { let ca = CATEGORIES.iter().position(|c| *c == a.1).unwrap_or(99); let cb = CATEGORIES.iter().position(|c| *c == b.1).unwrap_or(99); @@ -1558,9 +1612,16 @@ impl OptionsUi { // Drawn by `pack_screen`. Page::Packs => {} _ => { + if matches!(page, Page::Video | Page::Controls) { + top = 38.0; + } out.push(Widget { x: 0.0, - y: 12.0, + y: if matches!(page, Page::Video | Page::Controls) { + 4.0 + } else { + 12.0 + }, w, h: 9.0, kind: WKind::Text { @@ -1625,7 +1686,18 @@ impl OptionsUi { } content = y + self.scroll - top + 2.0; out[list_start..].iter_mut().for_each(|w| w.list = true); - out.push(done(h - 27.0)); + if page == Page::Mods { + out.push(button( + cx - 154.0, + h - 27.0, + 150.0, + Text::literal("Open Mod Folder"), + Act::OpenModFolder, + )); + out.push(button(cx + 4.0, h - 27.0, 150.0, tr("gui.done"), Act::Done)); + } else { + out.push(done(h - 27.0)); + } } } // Layout may change the offset (language centering), or the list may @@ -1881,6 +1953,23 @@ impl OptionsUi { } self.language_search = search; } + if matches!(self.page(), Page::Video | Page::Controls | Page::KeyBinds) { + let mut search = self.settings_search.clone(); + search.draw(batch, atlas, None); + if search.value.is_empty() && !search.focused { + batch.text_styled( + atlas, + &Text::literal("Search settings...") + .with_color(text::GRAY) + .italic(), + search.text_x(), + search.text_y(), + u32::MAX, + true, + ); + } + self.settings_search = search; + } if let Some(lines) = tooltip { ui::tooltip(batch, atlas, &lines, cursor); } @@ -1964,6 +2053,16 @@ impl OptionsUi { } let (widgets, top, bottom, content) = self.layout(s, size.0, size.1); let mut events = Vec::new(); + if !drag && matches!(self.page(), Page::Video | Page::Controls | Page::KeyBinds) { + let search = &mut self.settings_search; + search.focused = ui::hovered((x, y), search.x, search.y, search.width, search.height); + if search.focused { + search.click(atlas, x, false, false); + self.scroll = 0.0; + events.push(Event::ButtonClick); + return events; + } + } if drag { if let Some(previous_y) = self.scrollbar_drag_y { if let Some((_, thumb)) = ui::scrollbar_thumb(top, bottom, content, self.scroll) { @@ -2077,6 +2176,7 @@ impl OptionsUi { s.mods.toggle(i); events.push(Event::Changed); } + Act::OpenModFolder => events.push(Event::OpenModFolder), Act::None => {} } } @@ -2238,6 +2338,9 @@ impl OptionsUi { if self.page() == Page::Language { self.language_search.insert_text(text); self.scroll = 0.0; + } else if self.settings_search_active() { + self.settings_search.insert_text(text); + self.scroll = 0.0; } } /// Ctrl+A/C/V/X in the language or pack search box; false when neither @@ -2263,6 +2366,12 @@ impl OptionsUi { } return true; } + if self.settings_search_active() { + if self.settings_search.shortcut(shortcut, clipboard) { + self.scroll = 0.0; + } + return true; + } false } fn bind_key(&mut self, s: &mut Settings, value: String) -> Vec<Event> { @@ -2346,6 +2455,16 @@ impl OptionsUi { } } self.scroll = 0.0; + } else if self.settings_search_active() { + if !self.settings_search.key(key, false, false) + && let Some(t) = text + { + let typed: String = t.chars().filter(|c| !c.is_control()).collect(); + if !typed.is_empty() { + self.settings_search.insert_text(&typed); + } + } + self.scroll = 0.0; } Vec::new() } @@ -2356,6 +2475,25 @@ mod tests { use super::*; use crate::ui_theme::{MenuStyle, set_style, style}; + #[test] + fn render_distance_slider_round_trips_through_extended_options_range() { + let mut s = Settings::default(); + for (position, distance) in [(0.0, 2), (0.5, 33), (1.0, 64)] { + set_slider(Opt::RenderDistance, &mut s, position); + assert_eq!(s.render_distance, distance); + let reloaded = Settings::parse(&s.serialize()); + assert_eq!(reloaded.render_distance, distance); + assert_eq!( + Opt::RenderDistance.slider_position(&reloaded), + Some(position) + ); + } + let mut file = crate::options_file::OptionsFile::default(); + file.set_text("renderDistance", "64").unwrap(); + assert!(file.set_text("renderDistance", "65").is_err()); + assert!(file.set_text("renderDistance", "1").is_err()); + assert_eq!(file.int("renderDistance"), 64); + } fn atlas() -> HudAtlas { HudAtlas::build(std::path::Path::new("/nonexistent/horizon-test-assets")).unwrap() } @@ -2430,6 +2568,31 @@ mod tests { assert!(empty.iter().any(|w| { matches!(&w.kind, WKind::Text { text, .. } if text.plain().contains(".hmod")) })); + let open_folder = empty + .iter() + .find(|w| { + matches!( + &w.kind, + WKind::Button { + act: Act::OpenModFolder, + .. + } + ) + }) + .expect("Mods page has an Open Mod Folder button"); + assert_eq!( + ui.click( + &mut s, + &atlas, + open_folder.x + 1.0, + open_folder.y + 1.0, + size, + [480, 240], + false, + false, + ), + vec![Event::ButtonClick, Event::OpenModFolder] + ); for (id, dev) in [("normal", false), ("dev", true), ("", false)] { s.mods.installed.push(crate::mod_list::Installed { id: id.into(), diff --git a/crates/horizon-client/src/particles.rs b/crates/horizon-client/src/particles.rs index 57d9e60d7..938e8d72d 100644 --- a/crates/horizon-client/src/particles.rs +++ b/crates/horizon-client/src/particles.rs @@ -1264,12 +1264,21 @@ impl ParticleEngine { } self.create(options, p, v); } - /// `LevelEventHandler.singleBlockTeleportationParticles` (event 2015). - pub fn block_teleportation(&mut self, from: [i32; 3], data: i32, particle_level: u8) { + /// `LevelEventHandler.singleBlockTeleportationParticles` towards + /// `BlockUtil.unpackDifferenceInPosition(from, data, radius)`: the dragon + /// egg's event 2015 packs offsets within (16, 8, 16), a consumable's + /// teleport (2017) within (127, 127, 127). + pub fn block_teleportation( + &mut self, + from: [i32; 3], + data: i32, + radius: [i32; 3], + particle_level: u8, + ) { let to = [ - from[0].wrapping_add(((data >> 16) & 255) - 16), - from[1].wrapping_add(((data >> 8) & 255) - 8), - from[2].wrapping_add((data & 255) - 16), + from[0].wrapping_add(((data >> 16) & 255) - radius[0]), + from[1].wrapping_add(((data >> 8) & 255) - radius[1]), + from[2].wrapping_add((data & 255) - radius[2]), ] .map(f64::from); let from = from.map(f64::from); @@ -3798,7 +3807,7 @@ mod tests { let mut engine = ParticleEngine::new(Path::new(""), &HashMap::new()); engine.seed_randoms(0, 0, 0); // Destination offset (-15, 7, 15); the unused top byte is ignored. - engine.block_teleportation([4, 12, -5], 0xff01_0f1fu32 as i32, 0); + engine.block_teleportation([4, 12, -5], 0xff01_0f1fu32 as i32, [16, 8, 16], 0); assert_eq!(engine.pending.len(), 128); assert!(engine.pending.iter().all(|p| p.ty == ParticleType::Portal)); let first = &engine.pending[0]; @@ -3818,6 +3827,25 @@ mod tests { assert!(engine.sounds.is_empty()); } + #[test] + fn consume_teleport_unpacks_offsets_within_127() { + let mut engine = ParticleEngine::new(Path::new(""), &HashMap::new()); + engine.seed_randoms(0, 0, 0); + // TeleportRandomlyConsumeEffect: BlockUtil.clampedPackDifferenceInPosition + // of the move (-7, -3, 9) with radii 127, from (4, 12, -5) to (-3, 9, 4). + let data = (-7 + 127) << 16 | (-3 + 127) << 8 | (9 + 127); + engine.block_teleportation([4, 12, -5], data, [127, 127, 127], 0); + assert_eq!(engine.pending.len(), 128); + for p in &engine.pending { + let [x, y, z] = p.position; + assert!((-3.0..5.0).contains(&x), "{x}"); + assert!((8.5..12.5).contains(&y), "{y}"); + assert!((-5.0..5.0).contains(&z), "{z}"); + } + // The same draws as the egg's: only the unpacking differs. + assert_eq!(engine.level_random.state(), 99_413_408_083_437); + } + #[test] fn egg_teleport_options_roll_before_distance_rejection() { for (option, count, state) in [ @@ -3831,7 +3859,7 @@ mod tests { if distant { engine.camera = [1000.0; 3]; } - engine.block_teleportation([4, 12, -5], 0x010f1f, option); + engine.block_teleportation([4, 12, -5], 0x010f1f, [16, 8, 16], option); assert_eq!(engine.pending.len(), if distant { 0 } else { count }); assert_eq!(engine.level_random.state(), state); } diff --git a/crates/horizon-client/src/player_sync.rs b/crates/horizon-client/src/player_sync.rs new file mode 100644 index 000000000..96b09c708 --- /dev/null +++ b/crates/horizon-client/src/player_sync.rs @@ -0,0 +1,86 @@ +//! LocalPlayer.sendPosition and PositionMoveRotation.calculateAbsolute. +use horizon_core::geometry::Vec3; +use horizon_protocol::packets::play::{self, PlayerPosition, Serverbound as S, relative}; + +/// Only movement updates advance this baseline. A teleport acknowledgement is +/// separate and must not suppress the next tick's corrected position or look. +#[derive(Default)] +pub struct PositionSender { + position: [f64; 3], + rotation: (f32, f32), + on_ground: bool, + collision: bool, + reminder: u32, +} + +impl PositionSender { + pub fn update( + &mut self, + position: [f64; 3], + rotation: (f32, f32), + on_ground: bool, + collision: bool, + ) -> Option<S> { + self.reminder += 1; + let d: [f64; 3] = std::array::from_fn(|i| position[i] - self.position[i]); + let moved = + d[0] * d[0] + d[1] * d[1] + d[2] * d[2] > 2.0e-4 * 2.0e-4 || self.reminder >= 20; + let rotated = rotation.0 - self.rotation.0 != 0.0 || rotation.1 - self.rotation.1 != 0.0; + let flags = u8::from(on_ground) | u8::from(collision) << 1; + let [x, y, z] = position; + let (yaw, pitch) = rotation; + let packet = if moved && rotated { + Some(S::MovePlayerPosRot(play::MovePlayerPosRot { + x, + y, + z, + yaw, + pitch, + flags, + })) + } else if moved { + Some(S::MovePlayerPos(play::MovePlayerPos { x, y, z, flags })) + } else if rotated { + Some(S::MovePlayerRot(play::MovePlayerRot { yaw, pitch, flags })) + } else if self.on_ground != on_ground || self.collision != collision { + Some(S::MovePlayerStatus(play::MovePlayerStatus { flags })) + } else { + None + }; + if moved { + self.position = position; + self.reminder = 0; + } + if rotated { + self.rotation = rotation; + } + self.on_ground = on_ground; + self.collision = collision; + packet + } +} + +/// Rotate the old velocity before adding the relative delta components. +/// Angles deliberately use Java's double toRadians followed by a float cast. +pub fn teleport_velocity( + mut velocity: Vec3, + old_rotation: (f32, f32), + new_rotation: (f32, f32), + packet: &PlayerPosition, +) -> Vec3 { + if packet.relatives & relative::ROTATE_DELTA != 0 { + let pitch = f64::from(old_rotation.1 - new_rotation.1).to_radians() as f32; + let yaw = f64::from(old_rotation.0 - new_rotation.0).to_radians() as f32; + velocity = velocity.rotate_x(pitch).rotate_y(yaw); + } + let old = [velocity.x, velocity.y, velocity.z]; + let flags = [relative::DELTA_X, relative::DELTA_Y, relative::DELTA_Z]; + let result: [f64; 3] = std::array::from_fn(|i| { + if packet.relatives & flags[i] != 0 { + old[i] + packet.delta[i] + } else { + packet.delta[i] + } + }); + Vec3::new(result[0], result[1], result[2]) +} diff --git a/crates/horizon-client/src/poi_sections.rs b/crates/horizon-client/src/poi_sections.rs index 2116362e0..79ff0441d 100644 --- a/crates/horizon-client/src/poi_sections.rs +++ b/crates/horizon-client/src/poi_sections.rs @@ -54,12 +54,12 @@ impl<T: Copy> PoiSections<T> { return cached.pois.clone(); } let pois: SectionPois<T> = if section.blocks.maybe_has(|v| kind(v).is_some()) { - let mut states = [0u16; 4096]; + let mut states = [0u32; 4096]; section.unpack(&mut states); states .iter() .enumerate() - .filter_map(|(i, &state)| kind(u32::from(state)).map(|poi| (i as u16, poi))) + .filter_map(|(i, &state)| kind(state).map(|poi| (i as u16, poi))) .collect() } else { Arc::from([]) diff --git a/crates/horizon-client/src/portal_sections.rs b/crates/horizon-client/src/portal_sections.rs new file mode 100644 index 000000000..f22cd2b83 --- /dev/null +++ b/crates/horizon-client/src/portal_sections.rs @@ -0,0 +1,123 @@ +//! Memoized nether-portal palette eligibility per resident chunk. +//! +//! `Game::find_entity_portals` asks the same sections every tick whether their +//! palette may hold a portal state. The answer only changes when a chunk's +//! blocks change, so it is kept per chunk until the weak handle stops matching. +use crate::{chunk::Chunk, fast_hash::FxHashMap}; +use std::sync::{Arc, Weak}; + +struct Cached { + chunk: Weak<Chunk>, + /// A bit per section, set when that section's palette may hold a portal. + sections: u32, +} + +/// Resident chunks are immutable behind `Arc`; edits use `Arc::make_mut`. +/// A weak reference detects both edits and replacement, without retaining block +/// data or forcing a deep copy on the next edit. It also prevents address reuse. +/// Clear this cache whenever the gameplay facts change (including resource reload). +#[derive(Default)] +pub struct PortalSections { + chunks: FxHashMap<(i32, i32), Cached>, +} + +impl PortalSections { + pub fn clear(&mut self) { + self.chunks.clear(); + } + pub fn remove(&mut self, key: &(i32, i32)) { + self.chunks.remove(key); + } + /// The sections of `chunk` whose palette may hold a portal, as a bitmask in + /// ascending section order (`MIN_Y` at bit 0). `portal` is only called while + /// (re)building an entry, so an unchanged chunk scans its palette once. + pub fn get( + &mut self, + key: (i32, i32), + chunk: &Arc<Chunk>, + mut portal: impl FnMut(u32) -> bool, + ) -> u32 { + const { + assert!(crate::chunk::SECTIONS <= u32::BITS as usize); + } + let cached = self.chunks.entry(key).or_insert_with(|| Cached { + chunk: Weak::new(), + sections: 0, + }); + if cached.chunk.as_ptr() != Arc::as_ptr(chunk) { + let mut mask = 0; + for (i, section) in chunk.sections.iter().enumerate() { + // Preserve maybe_has, including unused entries and global palettes. + if section.blocks.maybe_has(&mut portal) { + mask |= 1 << i; + } + } + cached.sections = mask; + cached.chunk = Arc::downgrade(chunk); + } + cached.sections + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::chunk::{MIN_Y, SECTIONS, Section}; + + #[test] + fn edits_replacements_compaction_and_reload() { + let mut cache = PortalSections::default(); + let mut chunk = Arc::new(Chunk::empty(0, 0)); + assert_eq!(cache.get((0, 0), &chunk, |id| id == 7), 0); + cache.get((0, 0), &chunk, |_| panic!("unchanged chunk rescanned")); + let worker = chunk.clone(); + Arc::make_mut(&mut chunk).set(0, MIN_Y, 0, 7); + assert_eq!(cache.get((0, 0), &chunk, |id| id == 7), 1); + assert_eq!(worker.get(0, MIN_Y, 0), 0); + // Even after the last portal block is removed, its unused palette entry counts. + Arc::make_mut(&mut chunk).set(0, MIN_Y, 0, 0); + assert_eq!(cache.get((0, 0), &chunk, |id| id == 7), 1); + Arc::make_mut(&mut Arc::make_mut(&mut chunk).sections[0]).compact(); + assert_eq!(cache.get((0, 0), &chunk, |id| id == 7), 0); + Arc::make_mut(&mut chunk).sections[SECTIONS - 1] = Arc::new(Section::default()); + cache.clear(); // resource reload, with changed facts for the same state IDs + assert_eq!( + cache.get((0, 0), &chunk, |id| id == 0), + (1 << SECTIONS) - 1, + "a fresh palette is rescanned for every section" + ); + cache.remove(&(0, 0)); + assert!(cache.chunks.is_empty()); + } + + #[test] + fn replacement_at_same_coordinates_and_early_snapshot() { + let mut cache = PortalSections::default(); + let mut chunk = Arc::new(Chunk::empty(-2, 3)); + let before = cache.get((-2, 3), &chunk, |id| id == 1); + Arc::make_mut(&mut chunk).set(0, MIN_Y + 16, 0, 1); + assert_eq!(before, 0); + assert_eq!(cache.get((-2, 3), &chunk, |id| id == 1), 1 << 1); + let weak = Arc::downgrade(&chunk); + drop(chunk); + assert!(weak.upgrade().is_none()); // cache does not retain unloaded block data + let replacement = Arc::new(Chunk::empty(-2, 3)); + assert_eq!(cache.get((-2, 3), &replacement, |id| id == 1), 0); + } + + #[test] + fn global_palette_covers_the_whole_id_range() { + let mut chunk = Chunk::empty(0, 0); + for i in [0, 9, SECTIONS - 1] { + Arc::make_mut(&mut chunk.sections[i]).blocks = + Section::from_blocks(&std::array::from_fn(|j| (j % 300) as u32)); + } + let chunk = Arc::new(chunk); + let mut cache = PortalSections::default(); + // Global maybe_has considers the entire ID range, including absent states. + assert_eq!( + cache.get((0, 0), &chunk, |id| id == 40000), + (1 << 0) | (1 << 9) | (1 << (SECTIONS - 1)) + ); + } +} diff --git a/crates/horizon-client/src/projectiles.rs b/crates/horizon-client/src/projectiles.rs index bfd0e8a97..cc44e7511 100644 --- a/crates/horizon-client/src/projectiles.rs +++ b/crates/horizon-client/src/projectiles.rs @@ -63,8 +63,13 @@ pub enum ProjectileKind { } #[derive(Clone, Debug, PartialEq)] pub struct Projectile { + /// The entity id a server tracks it by (0 until it is tracked). + pub id: u32, pub kind: ProjectileKind, pub owner: Option<Who>, + /// On a server, the guest (by connection) who shot it: `Who::Player` + /// then means them, not the host. + pub guest: Option<u32>, pub position: Vec3, pub previous: Vec3, pub velocity: Vec3, @@ -128,8 +133,10 @@ impl Projectile { pub fn new(kind: ProjectileKind, position: Vec3, owner: Option<Who>) -> Self { let kind_is_arrow = matches!(kind, ProjectileKind::Arrow { .. }); Self { + id: 0, kind, owner, + guest: None, position, previous: position, velocity: Vec3::ZERO, diff --git a/crates/horizon-client/src/random_tick_sections.rs b/crates/horizon-client/src/random_tick_sections.rs index 97a1f02fa..cd06f2ac9 100644 --- a/crates/horizon-client/src/random_tick_sections.rs +++ b/crates/horizon-client/src/random_tick_sections.rs @@ -134,7 +134,7 @@ mod tests { let mut chunk = Chunk::empty(0, 0); for i in [0, 9, SECTIONS - 1] { Arc::make_mut(&mut chunk.sections[i]).blocks = - Section::from_blocks(&std::array::from_fn(|j| (j % 300) as u16)); + Section::from_blocks(&std::array::from_fn(|j| (j % 300) as u32)); } let chunk = Arc::new(chunk); let mut cache = RandomTickSections::default(); @@ -176,12 +176,9 @@ mod tick_parity { } fn set_raw(&mut self, pos: BlockPos, state: BlockStateId) -> Option<BlockStateId> { let chunk = self.chunks.get_mut(&(pos.x >> 4, pos.z >> 4))?; - Some(BlockStateId(u32::from(Arc::make_mut(chunk).set( - pos.x, - pos.y, - pos.z, - state.0 as u16, - )))) + Some(BlockStateId( + Arc::make_mut(chunk).set(pos.x, pos.y, pos.z, state.0), + )) } fn is_loaded(&self, pos: BlockPos) -> bool { self.chunks.contains_key(&(pos.x >> 4, pos.z >> 4)) @@ -233,7 +230,7 @@ mod tick_parity { // Every sampled block can run real behaviour and change to dirt. for i in [4, 7] { Arc::make_mut(&mut c.sections[i]).blocks = - crate::chunk::Section::from_blocks(&[grass.0 as u16; 4096]); + crate::chunk::Section::from_blocks(&[grass.0; 4096]); } Arc::new(c) }; diff --git a/crates/horizon-client/src/recipe_book.rs b/crates/horizon-client/src/recipe_book.rs index 2caa68cac..27051bbc6 100644 --- a/crates/horizon-client/src/recipe_book.rs +++ b/crates/horizon-client/src/recipe_book.rs @@ -756,6 +756,15 @@ impl RecipeIndex { r.first_display..r.first_display + r.displays as i32 }) } + /// The recipe a display id belongs to (`RecipeManager.getRecipeFromDisplay`). + pub fn recipe_of_display(&self, display: i32) -> Option<&str> { + self.recipes + .iter() + .find(|(_, r)| { + (r.first_display..r.first_display + r.displays as i32).contains(&display) + }) + .map(|(id, _)| id.as_str()) + } pub fn first_display(&self, id: &str) -> Option<i32> { self.recipes .get(id) diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index a8553e276..dffaeb46d 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -16,7 +16,7 @@ use crate::{ Result, biome::Biomes, - chunk::{BIOME_STRATEGY, BLOCK_STRATEGY, Chunk, Light, MIN_Y, SECTIONS}, + chunk::{BIOME_STRATEGY, Chunk, Light, MIN_Y, SECTIONS, Section, block_strategy}, streaming::ChunkGenerator, }; use horizon_core::{ @@ -29,9 +29,9 @@ 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, RegionWriter}, + region::{ChunkRecord, RegionFile}, registry::{BlockRegistry, BlockStateId}, worldgen::terrain::TerrainBlockStates, }; @@ -238,7 +238,7 @@ pub struct SaveTables { default_state: Vec<bool>, block_entity: Vec<bool>, heightmaps: crate::assets::Properties, - air: u16, + air: u32, } impl SaveTables { pub fn new(assets: &crate::assets::Assets) -> Self { @@ -266,9 +266,19 @@ impl SaveTables { default_state, block_entity: Vec::new(), heightmaps: assets.properties.clone(), - air: registry - .default_state("minecraft:air") - .map_or(0, |s| s.0 as u16), + air: registry.default_state("minecraft:air").map_or(0, |s| s.0), + } + } + /// Tables with only the heightmaps' state facts, for tests. + #[cfg(test)] + pub(crate) fn for_heightmaps(heightmaps: crate::assets::Properties) -> Self { + Self { + block_names: Vec::new(), + block_ids: HashMap::new(), + default_state: Vec::new(), + block_entity: Vec::new(), + heightmaps, + air: 0, } } /// Which states carry a block entity (`BlockFacts`), to drop entries for @@ -675,8 +685,9 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain Some((pos, entity)) } +/// A column's sections as `Heightmap.primeHeightmaps` reads them. struct HeightmapView<'a> { - chunk: &'a Chunk, + sections: &'a [Arc<Section>], tables: &'a SaveTables, } impl HeightmapChunk for HeightmapView<'_> { @@ -689,7 +700,6 @@ impl HeightmapChunk for HeightmapView<'_> { } fn highest_section_y(&self) -> i32 { let top = self - .chunk .sections .iter() .rposition(|s| !s.is_uniform(self.tables.air)) @@ -697,9 +707,12 @@ impl HeightmapChunk for HeightmapView<'_> { MIN_Y + top as i32 * 16 } fn block_state(&self, x: i32, y: i32, z: i32) -> Self::State { - self.tables - .heightmaps - .heightmap_state(self.chunk.block(x, y, z)) + // Chunk.get: no block outside the sections. + let state = usize::try_from((y - MIN_Y) >> 4) + .ok() + .and_then(|s| self.sections.get(s)) + .map_or(0, |s| s.get(crate::chunk::index(x, y, z))); + self.tables.heightmaps.heightmap_state(BlockStateId(state)) } } /// Heightmaps kept after world generation (`ChunkStatus.FULL.heightmapsAfter`). @@ -714,7 +727,10 @@ fn heightmaps(chunk: &Chunk, tables: &SaveTables) -> Tag { .iter() .filter_map(|k| Heightmap::new(*k, MIN_Y, SECTIONS as i32 * 16).ok()) .collect(); - let view = HeightmapView { chunk, tables }; + let view = HeightmapView { + sections: &chunk.sections, + tables, + }; let mut refs: Vec<&mut Heightmap> = maps.iter_mut().collect(); if Heightmap::prime(&view, &mut refs).is_err() { return Tag::Compound(Compound::new()); @@ -730,6 +746,16 @@ fn heightmaps(chunk: &Chunk, tables: &SaveTables) -> Tag { /// sendToClient: WORLD_SURFACE, MOTION_BLOCKING, MOTION_BLOCKING_NO_LEAVES), /// as (network id, packed longs) in Heightmap.Types order. pub fn client_heightmaps(chunk: &Chunk, tables: &SaveTables) -> Vec<(i32, Vec<i64>)> { + client_heightmaps_in(&chunk.sections, tables, MIN_Y, SECTIONS as i32 * 16) +} +/// The client heightmaps of a column's sections, in a level from `min_y`, +/// `height` blocks high. +pub fn client_heightmaps_in( + sections: &[Arc<Section>], + tables: &SaveTables, + min_y: i32, + height: i32, +) -> Vec<(i32, Vec<i64>)> { let kinds = [ (1, HeightmapType::WorldSurface), (4, HeightmapType::MotionBlocking), @@ -737,13 +763,9 @@ pub fn client_heightmaps(chunk: &Chunk, tables: &SaveTables) -> Vec<(i32, Vec<i6 ]; let mut maps: Vec<(i32, Heightmap)> = kinds .iter() - .filter_map(|(id, k)| { - Heightmap::new(*k, MIN_Y, SECTIONS as i32 * 16) - .ok() - .map(|m| (*id, m)) - }) + .filter_map(|(id, k)| Heightmap::new(*k, min_y, height).ok().map(|m| (*id, m))) .collect(); - let view = HeightmapView { chunk, tables }; + let view = HeightmapView { sections, tables }; let mut refs: Vec<&mut Heightmap> = maps.iter_mut().map(|(_, m)| m).collect(); if Heightmap::prime(&view, &mut refs).is_err() { return Vec::new(); @@ -1030,8 +1052,8 @@ fn decode_column( } } } - let air = registry.default_state("minecraft:air")?.0 as u16; - let mut cache: HashMap<String, u16> = HashMap::new(); + let air = registry.default_state("minecraft:air")?.0; + let mut cache: HashMap<String, u32> = HashMap::new(); let Some(Tag::List(sections)) = tag.get("sections") else { return Some(chunk); }; @@ -1046,7 +1068,7 @@ fn decode_column( if let Some(states) = section.get("block_states") && let Some(Tag::List(palette)) = states.get("palette") { - let ids: Vec<u16> = palette + let palette: Vec<u32> = palette .iter() .map(|p| { let state = BlockState::from_nbt(p).ok(); @@ -1069,7 +1091,7 @@ fn decode_column( registry.id(&full).or(Some(default)) }) }) - .map_or(air, |id| id.0 as u16) + .map_or(air, |id| id.0) }) }) .collect(); @@ -1080,9 +1102,8 @@ fn decode_column( } _ => None, }; - let palette: Vec<u32> = ids.iter().map(|&id| u32::from(id)).collect(); if let Ok(blocks) = - PalettedContainer::from_packed(BLOCK_STRATEGY, palette, data.as_deref()) + PalettedContainer::from_packed(block_strategy(), palette, data.as_deref()) { target.blocks = blocks; } @@ -1276,125 +1297,146 @@ pub fn load_light(tag: &Tag, chunk: &mut Chunk, sky_light: bool) { }; layers[si as usize] = (layer("SkyLight"), layer("BlockLight")); } - let sky_default = if sky_light { 15u8 } else { 0 }; - // Sky nibbles of the section above's bottom layer, going down. - let mut above: [u8; 256] = [sky_default; 256]; - for si in (0..SECTIONS).rev() { - let (sky, block) = layers[si]; - let mut data = Box::new([0u8; 4096]); - let nibble = |a: &[u8], i: usize| (a[i >> 1] >> ((i & 1) * 4)) & 15; - for (i, packed) in data.iter_mut().enumerate() { - let s = match sky { - Some(a) if sky_light => nibble(a, i), - _ => above[i & 255], - }; - let b = block.map_or(0, |a| nibble(a, i)); - *packed = s << 4 | b; - } - for (i, a) in above.iter_mut().enumerate() { - *a = data[i] >> 4; - } - let mut section = (*chunk.sections[si]).clone(); - section.light = Light::Full(data); - section.compact(); - chunk.sections[si] = Arc::new(section); - } - chunk.lit = true; - chunk.light_loaded = true; + let above = [if sky_light { 15 } else { 0 }; 256]; + crate::light::light_from_layers(chunk, &layers, above, sky_light); } type RegionCache = Mutex<HashMap<(i32, i32), Option<Arc<Mutex<RegionFile>>>>>; -/// Rewrite every region file in `folder` touched by `records`, keeping its -/// other chunks. `None` deletes a chunk. +/// The region file at `path` through the cache, which readers and the writer +/// share, so a read never sees a header older than the sectors it points to. +/// `create` makes a missing file (for a write); otherwise a missing file is +/// `None`, until a write creates it. +fn cached_region( + cache: &RegionCache, + key: (i32, i32), + path: &Path, + create: bool, +) -> Result<Option<Arc<Mutex<RegionFile>>>> { + let mut regions = cache.lock().map_err(|_| "region cache poisoned")?; + if let Some(Some(region)) = regions.get(&key) { + return Ok(Some(region.clone())); + } + if !create && regions.contains_key(&key) { + return Ok(None); + } + if create && let Some(parent) = path.parent() { + fs::create_dir_all(parent)?; + } + // A file this process can't write (a read-only world) is still read. + let region = match RegionFile::open_writable(path, create) { + Err(e) if !create && e.kind() == std::io::ErrorKind::PermissionDenied => { + RegionFile::open(path) + } + other => other, + }; + let region = match region { + Ok(r) => Some(Arc::new(Mutex::new(r))), + Err(_) if !create => None, + Err(e) => return Err(e.into()), + }; + regions.insert(key, region.clone()); + Ok(region) +} +/// Write `records` into the region files of `folder` in place, as vanilla's +/// `RegionFile.write` does: each chunk takes free sectors and the header +/// then points to it; the region's other chunks are not touched. `None` +/// clears a chunk. The chunks are encoded and compressed before their region +/// is locked for the writes (`compress_records`), and each region is flushed +/// to the disk once. fn write_region_records( root: &Path, cache: &RegionCache, folder: &str, - records: Vec<((i32, i32), Option<Arc<NamedTag>>)>, + records: &[((i32, i32), &Record)], ) -> Result<()> { - let mut by_region: BTreeMap<(i32, i32), Vec<((i32, i32), Option<Arc<NamedTag>>)>> = - BTreeMap::new(); - for (key, tag) in records { - by_region - .entry((key.0 >> 5, key.1 >> 5)) - .or_default() - .push((key, tag)); - } - if by_region.is_empty() { - return Ok(()); - } - let staging = root.join(folder).join(".staging"); - fs::create_dir_all(&staging)?; let timestamp = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map_or(0, |d| d.as_secs() as u32); + let compressed = compress_records(records, timestamp); + let mut by_region: BTreeMap<(i32, i32), Vec<(ChunkPos, Option<ChunkRecord>)>> = BTreeMap::new(); + for (&((x, z), _), record) in records.iter().zip(compressed) { + let record = record?; + by_region + .entry((x >> 5, z >> 5)) + .or_default() + .push((ChunkPos::new(x, z), record)); + } for ((rx, rz), list) in by_region { let path = region_path(root, folder, rx, rz); - let mut records: BTreeMap<(i32, i32), ChunkRecord> = BTreeMap::new(); - let only_deletes = list.iter().all(|(_, tag)| tag.is_none()); - if let Ok(mut existing) = RegionFile::open(&path) { - let positions: Vec<ChunkPos> = existing.chunks().map(|(p, _)| p).collect(); - if only_deletes - && !positions - .iter() - .any(|p| list.iter().any(|(k, _)| *k == (p.x, p.z))) - { - continue; - } - for p in positions { - if let Ok(Some(r)) = existing.read(p) { - records.insert((p.x, p.z), r); - } - } - } else if only_deletes { + // Clearing chunks of a region that doesn't exist changes nothing. + let create = list.iter().any(|(_, r)| r.is_some()); + let Some(region) = cached_region(cache, (rx, rz), &path, create)? else { continue; - } - // Deleting chunks the region doesn't hold changes nothing: leave - // the file alone (unloading empty entity chunks asks for that). + }; + let mut region = region.lock().map_err(|_| "region file poisoned")?; let mut changed = false; - for (key, tag) in list { - match tag { - Some(tag) => { + for (pos, record) in &list { + match record { + Some(record) => { + region.write(*pos, record)?; changed = true; - records.insert( - key, - ChunkRecord::from_nbt( - &tag, - Compression::Zlib, - timestamp, - Limits::default(), - )?, - ); - } - None => { - changed |= records.remove(&key).is_some(); } + None => changed |= region.clear(*pos, timestamp)?, } } - if !changed { - continue; + if changed { + region.sync()?; } - if records.is_empty() { - let _ = fs::remove_file(&path); - } else { - let temporary = staging.join(format!("r.{rx}.{rz}.mca")); - let _ = fs::remove_file(&temporary); - let mut writer = RegionWriter::create(&temporary)?; - for ((x, z), r) in &records { - writer.write(ChunkPos::new(*x, *z), r)?; - } - writer.finish()?; - fs::rename(&temporary, &path)?; - } - cache - .lock() - .map_err(|_| "region cache poisoned")? - .remove(&(rx, rz)); } - let _ = fs::remove_dir(&staging); Ok(()) } +/// Fewest chunks each helper of `compress_records` takes: below this a thread +/// costs more to start than it saves. +const CHUNKS_PER_HELPER: usize = 4; +/// Each record encoded and compressed (`None` for a deletion), in the order +/// given. Vanilla's IOWorker does this one chunk after another; here a batch +/// is shared out to scoped threads, which take the next chunk as they finish +/// one. Each chunk's bytes depend only on the chunk, and the writes stay in +/// order, so the files are the same as from one thread. At most half the +/// hardware threads help, leaving the rest to the game and its workers. +fn compress_records( + records: &[((i32, i32), &Record)], + timestamp: u32, +) -> Vec<std::io::Result<Option<ChunkRecord>>> { + let compress = |record: &Record| { + record + .tag() + .map(|tag| ChunkRecord::from_nbt(&tag, Compression::Zlib, timestamp, Limits::default())) + .transpose() + }; + let helpers = std::thread::available_parallelism() + .map_or(1, |n| n.get().div_ceil(2)) + .min(records.len() / CHUNKS_PER_HELPER); + if helpers <= 1 { + return records.iter().map(|(_, r)| compress(r)).collect(); + } + let next = std::sync::atomic::AtomicUsize::new(0); + let mut done: Vec<(usize, std::io::Result<Option<ChunkRecord>>)> = + std::thread::scope(|scope| { + let threads: Vec<_> = (0..helpers) + .map(|_| { + scope.spawn(|| { + let mut out = Vec::new(); + loop { + let i = next.fetch_add(1, Ordering::Relaxed); + let Some((_, record)) = records.get(i) else { + return out; + }; + out.push((i, compress(record))); + } + }) + }) + .collect(); + threads + .into_iter() + .flat_map(|t| t.join().unwrap_or_else(|e| std::panic::resume_unwind(e))) + .collect() + }); + done.sort_unstable_by_key(|(i, _)| *i); + done.into_iter().map(|(_, r)| r).collect() +} + fn region_path(root: &Path, folder: &str, rx: i32, rz: i32) -> PathBuf { root.join(format!("{folder}/r.{rx}.{rz}.mca")) } @@ -1434,7 +1476,7 @@ impl Record { type PendingWrites = Mutex<HashMap<(bool, i32, i32), (u64, Record)>>; type WriteBatch = (bool, Vec<((i32, i32), Record, u64)>); -/// The background thread that encodes chunks and rewrites region files, in +/// The background thread that encodes chunks and writes them into the region files, in /// submission order. struct Writer { send: mpsc::Sender<WriteBatch>, @@ -1496,17 +1538,8 @@ impl SaveDir { } else { &self.entity_regions }; - let region = { - let mut regions = cache.lock().ok()?; - regions - .entry(key) - .or_insert_with(|| { - RegionFile::open(self.region_path(folder, key.0, key.1)) - .ok() - .map(|r| Arc::new(Mutex::new(r))) - }) - .clone()? - }; + let path = self.region_path(folder, key.0, key.1); + let region = cached_region(cache, key, &path, false).ok()??; let record = region.lock().ok()?.read(ChunkPos::new(x, z)).ok()??; record.nbt(Limits::default()).ok() } @@ -1612,8 +1645,19 @@ impl SaveDir { } else { ("region", ®ions) }; - let records = batch.iter().map(|(k, r, _)| (*k, r.tag())).collect(); - if let Err(e) = write_region_records(&root, cache, folder, records) { + // IOWorker.pendingWrites: a chunk queued again since is + // written once, by its newest batch. + let newest = |k: &(i32, i32), seq: u64| { + pending.lock().map_or(true, |p| { + p.get(&(entities, k.0, k.1)).is_none_or(|p| p.0 <= seq) + }) + }; + let records: Vec<_> = batch + .iter() + .filter(|(k, _, seq)| newest(k, *seq)) + .map(|(k, r, _)| (*k, r)) + .collect(); + if let Err(e) = write_region_records(&root, cache, folder, &records) { log::warn!("Horizon: failed to write {folder} files: {e}"); } // Readers find the files (and a fresh region cache) from here on. @@ -1648,7 +1692,7 @@ impl SaveDir { }; } } - /// Rewrite every region touched by `chunks`, keeping their other chunks. + /// Write `chunks` into their region files and wait until they are on disk. pub fn save( &self, chunks: &[Arc<Chunk>], @@ -1975,40 +2019,51 @@ impl Default for WorldGen { } /// Writes a new world's `world_gen_settings` (seed, options and dimensions). pub fn write_world_gen_settings(dir: &Path, seed: i64, options: WorldGen) -> Result<()> { + write_world_gen_settings_with(dir, seed, options, None) +} +/// [`write_world_gen_settings`] with a flat overworld's own generator +/// settings (FlatLevelGeneratorSettings, as `flatgen::settings_from_json` +/// reads them) instead of the Superflat preset's classic layers. +pub fn write_world_gen_settings_with( + dir: &Path, + seed: i64, + options: WorldGen, + flat_settings: Option<Tag>, +) -> Result<()> { fs::create_dir_all(dir)?; // worldgen/world_preset/flat.json's overworld: the classic flat layers. let layer = |block: &str, height: i32| { Tag::compound([("block", string(block)), ("height", Tag::Int(height))]) }; + let classic = || { + Tag::compound([ + ("biome", string("minecraft:plains")), + ("features", Tag::Byte(0)), + ("lakes", Tag::Byte(0)), + ( + "layers", + Tag::List(vec![ + layer("minecraft:bedrock", 1), + layer("minecraft:dirt", 2), + layer("minecraft:grass_block", 1), + ]), + ), + ( + "structure_overrides", + Tag::List(vec![ + string("minecraft:strongholds"), + string("minecraft:villages"), + ]), + ), + ]) + }; let flat = Tag::compound([ ("type", string("minecraft:overworld")), ( "generator", Tag::compound([ ("type", string("minecraft:flat")), - ( - "settings", - Tag::compound([ - ("biome", string("minecraft:plains")), - ("features", Tag::Byte(0)), - ("lakes", Tag::Byte(0)), - ( - "layers", - Tag::List(vec![ - layer("minecraft:bedrock", 1), - layer("minecraft:dirt", 2), - layer("minecraft:grass_block", 1), - ]), - ), - ( - "structure_overrides", - Tag::List(vec![ - string("minecraft:strongholds"), - string("minecraft:villages"), - ]), - ), - ]), - ), + ("settings", flat_settings.unwrap_or_else(classic)), ]), ), ]); @@ -2133,11 +2188,25 @@ pub struct Level { pub on_ground: bool, pub food_exhaustion: f32, pub food_tick_timer: i32, + /// Abilities.flying: whether creative/spectator flight is active. + pub flying: bool, /// The player compound as loaded, for fields Horizon does not model /// (ender chest, effects, abilities, recipe book, attributes, ...). pub player_extra: Option<Tag>, } impl Level { + /// Initialize the offline single-player identity before saving or dropping items. + pub fn ensure_player_uuid(&mut self) -> [i32; 4] { + *self.player_uuid.get_or_insert_with(|| { + let u = horizon_protocol::crypto::offline_uuid("Player"); + [ + (u >> 96) as u32 as i32, + (u >> 64) as u32 as i32, + (u >> 32) as u32 as i32, + u as u32 as i32, + ] + }) + } pub fn game_rule(&self, name: &str) -> i32 { self.game_rules .get(name) @@ -2284,6 +2353,10 @@ impl Level { self.creative = mode == 1; self.game_mode = mode as i32; } + self.flying = p + .get("abilities") + .and_then(|a| number(a.get("flying"))) + .is_some_and(|v| v != 0); if let Some(d) = text(p.get("Dimension")) { self.dimension = d; } @@ -2415,7 +2488,9 @@ impl Level { } if spectator { put(&mut abilities, "flying", Tag::Byte(1)); - } else if !creative { + } else if creative { + put(&mut abilities, "flying", Tag::Byte(i8::from(self.flying))); + } else { put(&mut abilities, "flying", Tag::Byte(0)); } if let Tag::Compound(map) = &mut abilities { @@ -2595,17 +2670,7 @@ impl Level { map.remove(&JavaString::from(key)); } } - // A new world's player has the offline profile id of its name - // (UUIDUtil.createOfflinePlayerUUID), as in an offline single-player game. - let uuid = *self.player_uuid.get_or_insert_with(|| { - let u = horizon_protocol::crypto::offline_uuid("Player"); - [ - (u >> 96) as u32 as i32, - (u >> 64) as u32 as i32, - (u >> 32) as u32 as i32, - u as u32 as i32, - ] - }); + let uuid = self.ensure_player_uuid(); // A new world's first spawn point is its world spawn. if self.world_spawn.is_none() { self.world_spawn = self.spawn.map(|s| { @@ -2768,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(§ions, 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 @@ -2784,6 +3200,13 @@ 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); + } fn proto(&self, worker: &mut G::Worker, x: i32, z: i32) -> crate::streaming::Proto { let mut proto = self.inner.proto(worker, x, z); if let Some(saved) = self @@ -2839,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; @@ -2907,6 +3533,43 @@ mod tests { assert_eq!(source.block_entities[0].0, (99, 0, 99)); } + #[test] + fn records_compressed_on_helpers_match_one_thread_in_order() { + use super::{Record, compress_records}; + let records: Vec<((i32, i32), Record)> = (0..53i32) + .map(|i| { + let record = if i % 7 == 3 { + Record::Delete + } else { + let payload: Vec<i64> = (0..i * 97 % 2000).map(|v| i64::from(v * i)).collect(); + Record::Tag(Arc::new(NamedTag { + name: "".into(), + tag: Tag::compound([ + ("xPos", Tag::Int(i)), + ("Data", Tag::LongArray(payload)), + ]), + })) + }; + ((i, -i), record) + }) + .collect(); + let refs: Vec<_> = records.iter().map(|(k, r)| (*k, r)).collect(); + let compressed = compress_records(&refs, 1234); + assert_eq!(compressed.len(), records.len()); + for ((_, record), got) in records.iter().zip(compressed) { + let want = record + .tag() + .map(|t| ChunkRecord::from_nbt(&t, Compression::Zlib, 1234, Limits::default())) + .transpose() + .unwrap(); + let got = got.unwrap(); + assert_eq!( + got.as_ref().map(|r| (&r.compressed, r.timestamp)), + want.as_ref().map(|r| (&r.compressed, r.timestamp)) + ); + } + } + #[test] fn older_worlds_are_upgraded_on_opening() { let dir = std::env::temp_dir().join(format!("horizon-dv-{}", std::process::id())); @@ -3135,6 +3798,7 @@ mod tests { world_spawn: Some([8, 64, 8]), spawn: Some([20.0, 70.0, 20.0]), difficulty_locked: true, + flying: true, ..Level::default() }; level.weather.raining = true; @@ -3146,6 +3810,7 @@ mod tests { assert_eq!(back.seed, -42); assert_eq!((back.time, back.day_time), (1234, 25000)); assert!(back.creative); + assert!(back.flying); assert_eq!(back.position, Some([1.5, 70.0, -3.25])); assert_eq!(back.rotation, [90.0, 10.0]); assert_eq!((back.health, back.food, back.saturation), (17.0, 15, 2.5)); @@ -3199,6 +3864,17 @@ mod tests { third.ender_items[3].as_ref().map(|s| (&*s.item, s.count)), Some(("minecraft:diamond", 2)) ); + // A dead player's empty inventory replaces the previously loaded items. + let mut dead = third; + dead.health = 0.0; + dead.inventory.clear(); + dead.inventory_components.clear(); + dead.write(&dir).unwrap(); + let dead = Level::read(&dir).unwrap(); + assert_eq!(dead.health, 0.0); + assert!(dead.inventory.is_empty()); + assert!(dead.inventory_components.is_empty()); + assert!(dead.player_extra.unwrap().get("equipment").is_none()); assert!(dir.join("data/minecraft/world_gen_settings.dat").exists()); let _ = fs::remove_dir_all(&dir); } @@ -3217,4 +3893,33 @@ mod tests { level.active_effects.clear(); assert!(level.player_tag().get("active_effects").is_none()); } + + #[test] + fn creative_flight_status_persists_in_save_and_resets_for_survival() { + let mut level = Level { + creative: true, + flying: true, + ..Level::default() + }; + let tag = level.player_tag(); + let flying_byte = tag + .get("abilities") + .and_then(|a| number(a.get("flying"))) + .unwrap_or(0); + assert_eq!(flying_byte, 1); + + let mut loaded = Level::default(); + loaded.read_player(&tag); + assert!(loaded.flying); + + // Survival mode resets flying ability to 0 in save. + level.creative = false; + level.game_mode = 0; + let tag_survival = level.player_tag(); + let flying_survival = tag_survival + .get("abilities") + .and_then(|a| number(a.get("flying"))) + .unwrap_or(0); + assert_eq!(flying_survival, 0); + } } diff --git a/crates/horizon-client/src/spans.rs b/crates/horizon-client/src/spans.rs index 9fd91b2df..5c37f2bfc 100644 --- a/crates/horizon-client/src/spans.rs +++ b/crates/horizon-client/src/spans.rs @@ -1,11 +1,12 @@ //! Named timings within a frame, for finding what makes a frame slow: //! `HORIZON_SPIKES=MS` prints the spans of every frame whose CPU time -//! exceeds MS milliseconds. Costs one clock read per span when enabled and -//! a relaxed load otherwise. +//! exceeds MS milliseconds, and of every server tick (dedicated or +//! integrated) longer than MS. Costs one clock read per span when enabled +//! and a relaxed load otherwise. use std::{ cell::RefCell, sync::atomic::{AtomicBool, Ordering}, - time::Instant, + time::{Duration, Instant}, }; static ENABLED: AtomicBool = AtomicBool::new(false); @@ -28,6 +29,18 @@ pub fn time<T>(name: &'static str, f: impl FnOnce() -> T) -> T { SPANS.with(|s| s.borrow_mut().push((name, ms))); out } +/// The CPU time this thread has used, where the platform tells it: a tick +/// with more wall time than CPU time waited (for a lock, or for a core on a +/// busy machine). +pub fn thread_cpu() -> Option<Duration> { + #[cfg(unix)] + { + let t = rustix::time::clock_gettime(rustix::time::ClockId::ThreadCPUTime); + Some(Duration::new(t.tv_sec as u64, t.tv_nsec as u32)) + } + #[cfg(not(unix))] + None +} /// The frame's spans, summed by name, slowest first; clears them. pub fn take() -> Vec<(&'static str, f32)> { let spans = SPANS.with(|s| std::mem::take(&mut *s.borrow_mut())); @@ -41,3 +54,29 @@ pub fn take() -> Vec<(&'static str, f32)> { sums.sort_by(|a, b| b.1.total_cmp(&a.1)); sums } + +#[cfg(test)] +mod tests { + /// The thread's CPU clock moves with work done on it, not with time + /// spent asleep. + #[cfg(unix)] + #[test] + fn thread_cpu_counts_work_not_sleep() { + let start = super::thread_cpu().unwrap(); + std::thread::sleep(std::time::Duration::from_millis(50)); + let slept = super::thread_cpu().unwrap() - start; + // Busy until the clock shows 10 ms of work (a loaded machine gives + // the thread less than its wall time). + let mut x = 0u64; + let busy = std::time::Instant::now(); + let worked = loop { + x = std::hint::black_box(x.wrapping_mul(31).wrapping_add(7)); + let worked = super::thread_cpu().unwrap() - start - slept; + if worked.as_millis() >= 10 || busy.elapsed().as_secs() >= 10 { + break worked; + } + }; + assert!(slept.as_millis() < 25, "{slept:?} while asleep"); + assert!(worked.as_millis() >= 10, "{worked:?} while busy"); + } +} diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 994316324..933219a21 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -5,7 +5,7 @@ use crate::fast_hash::{FxHashMap, FxHashSet}; use crate::{ chunk::{Chunk, SECTIONS}, - light::{LightTables, apply_lighting, apply_relighting, light_chunk}, + light::{LightChanges, LightTables, apply_lighting, apply_relighting, light_chunk}, mesher::{MeshTables, Mesher, Region, SectionMesh, borrow}, world::World, }; @@ -32,10 +32,32 @@ 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 } + /// What a loaded chunk needs from the generator before the game uses it, + /// on the worker that loaded it. + fn prepare_loaded(&self, _worker: &mut Self::Worker, _chunk: &Chunk) {} fn proto(&self, worker: &mut Self::Worker, x: i32, z: i32) -> Proto; fn decorates(&self) -> bool { false @@ -60,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); } @@ -197,8 +269,10 @@ pub struct Streaming { /// Chunks come from a server (`insert_remote`/`forget`): nothing is /// generated and columns are only unloaded when the server says so. pub remote: bool, - /// More areas to keep loaded (centre chunk, radius), e.g. around guests - /// of a world open to the network. They are generated and kept but not meshed. + /// More areas to keep loaded (centre chunk, radius), e.g. around the + /// players of a server. Each is a square, like vanilla's ticket levels: + /// chunks finish out to `radius + 1` (Chebyshev) and generation reaches + /// past that as around the player. They are generated and kept but not meshed. pub extra: Vec<((i32, i32), i32)>, /// Mesh the sections for drawing (false on a server thread). pub mesh: bool, @@ -261,25 +335,34 @@ impl GenerationScan { } { let mut wanted = Vec::new(); - let areas: Vec<((i32, i32), i32)> = - std::iter::once((input.ranges.center, input.ranges.radius)) - .chain(input.ranges.extra.iter().copied()) - .collect(); - for (center, radius) in areas { + // Each area's square of columns, as one run per row. Players' + // areas overlap (a server's players often stand together), so + // the runs of a row are merged and each column is looked at once. + let mut runs: Vec<(i32, i32, i32)> = Vec::new(); + for (center, radius) in std::iter::once((input.ranges.center, input.ranges.radius)) + .chain(input.ranges.extra.iter().copied()) + { let r = radius + margin.max(reach + 1); - for dz in -r..=r { - for dx in -r..=r { - let (x, z) = (center.0 + dx, center.1 + dz); - if !input.ranges.requested(x, z, input.decorates) - || input.world.chunks.contains_key(&(x, z)) - || input.generating.contains(&(x, z)) - || input.protos.contains_key(&(x, z)) - || input.finishing.contains(&(x, z)) - { - continue; - } - wanted.push((input.ranges.distance(x, z), x, z)); + runs.extend((center.1 - r..=center.1 + r).map(|z| (z, center.0 - r, center.0 + r))); + } + runs.sort_unstable(); + let mut runs = runs.into_iter().peekable(); + while let Some((z, x0, mut x1)) = runs.next() { + while let Some((_, _, next_x1)) = + runs.next_if(|&(nz, nx0, _)| nz == z && nx0 <= x1.saturating_add(1)) + { + x1 = x1.max(next_x1); + } + for x in x0..=x1 { + if !input.ranges.requested(x, z, input.decorates) + || input.world.chunks.contains_key(&(x, z)) + || input.generating.contains(&(x, z)) + || input.protos.contains_key(&(x, z)) + || input.finishing.contains(&(x, z)) + { + continue; } + wanted.push((input.ranges.distance(x, z), x, z)); } } // Forced chunks and their generation neighbourhood. @@ -321,6 +404,7 @@ impl GenerationScan { } pub struct WorkerContext { pub assets: Arc<crate::assets::Assets>, + pub render: Arc<crate::assets::RenderAssets>, pub tables: Arc<MeshTables>, pub biomes: Arc<crate::biome::Biomes>, pub light: Arc<LightTables>, @@ -369,7 +453,10 @@ impl Streaming { }; let done = match job { Job::Generate(x, z) => match generator.load(x, z) { - Some(chunk) => Done::Generated(chunk), + Some(chunk) => { + generator.prepare_loaded(&mut worker, &chunk); + Done::Generated(chunk) + } None => { let proto = generator.proto(&mut worker, x, z); let decorated = generator.is_decorated(&proto); @@ -407,6 +494,7 @@ impl Streaming { cutout_leaves: settings.cutout_leaves, cull_leaves: settings.cull_leaves, assets: &context.assets, + render: &context.render, tables: &context.tables, biomes: &context.biomes, visibility, @@ -471,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. @@ -485,16 +577,38 @@ impl Streaming { } } } - /// A column received from a server: store it and relight it and its neighbours. + /// A column received from a server, with the light it sent + /// (`ClientPacketListener.handleLevelChunkWithLight`): nothing is + /// relit, and it and its neighbours remesh (`enableChunkLight`). pub fn insert_remote(&mut self, world: &mut World, chunk: Chunk) { let key = (chunk.x, chunk.z); - self.lit.remove(&key); self.lighting.remove(&key); + self.light_snapshots.remove(&key); world.chunks.insert(key, Arc::new(chunk)); + self.lit.insert(key); self.generated_total += 1; for dz in -1..=1 { for dx in -1..=1 { - world.dirty_light.insert((key.0 + dx, key.1 + dz)); + for sy in 0..SECTIONS as i32 { + world.dirty_sections.insert((key.0 + dx, sy, key.1 + dz)); + } + } + } + } + /// A server's light update for a resident column: the sections whose + /// light changed and their neighbours remesh + /// (`setSectionDirtyWithNeighbors`). + pub fn light_updated(world: &mut World, x: i32, z: i32, sections: &[usize]) { + for &sy in sections { + for dz in -1..=1 { + for dy in -1..=1 { + for dx in -1..=1 { + let s = sy as i32 + dy; + if (0..SECTIONS as i32).contains(&s) { + world.dirty_sections.insert((x + dx, s, z + dz)); + } + } + } } } } @@ -505,7 +619,7 @@ impl Streaming { self.lighting.remove(&(x, z)); self.meshing.retain(|k, _| (k.0, k.2) != (x, z)); self.waiting.retain(|k| (k.0, k.2) != (x, z)); - world.dirty_sections.retain(|k| (k.0, k.2) != (x, z)); + world.dirty_sections.take_column(x, z); world.chunks.remove(&(x, z)) } fn submit(&mut self, priority: i64, job: Job) { @@ -574,7 +688,7 @@ impl Streaming { } world .block_entities - .extend(std::mem::take(&mut chunk.block_entities)); + .load_chunk(std::mem::take(&mut chunk.block_entities)); world .block_names .extend(std::mem::take(&mut chunk.block_names)); @@ -709,12 +823,12 @@ impl Streaming { // only around the light that changed, as vanilla's light // updates dirty only their sections (#270). let first = self.lit.insert((x, z)); - let changed = if first { + let mut changed = LightChanges::default(); + if first { apply_lighting(chunk, sections); - Vec::new() } else { - apply_relighting(chunk, sections) - }; + apply_relighting(chunk, sections, &mut changed); + } chunk.lit = true; if first { for dz in -1..=1 { @@ -770,23 +884,22 @@ impl Streaming { let far: HashSet<(i32, i32)> = far.iter().copied().collect(); self.meshing.retain(|k, _| !far.contains(&(k.0, k.2))); self.waiting.retain(|k| !far.contains(&(k.0, k.2))); - world.dirty_sections.retain(|k| !far.contains(&(k.0, k.2))); + world + .dirty_sections + .retain_columns(|x, z| !far.contains(&(x, z))); } // Drop far proto chunks that are not checked out. - let areas: Vec<((i32, i32), i64)> = - std::iter::once((self.center, i64::from(self.radius + 4))) - .chain(self.extra.iter().map(|&(c, r)| (c, i64::from(r + 4)))) - .collect(); - let forced = &self.forced; + let (center, r4) = (self.center, i64::from(self.radius + 4)); + let ranges = self.ranges(); let far: Vec<(i32, i32)> = self .protos .iter() .filter(|&(&(x, z), p)| { p.is_some() - && !areas.iter().any(|&((cx, cz), r4)| { - i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2) <= r4 * r4 - }) - && !forced + && i64::from(x - center.0).pow(2) + i64::from(z - center.1).pow(2) > r4 * r4 + && !ranges.in_extra(x, z, 4) + && !ranges + .forced .iter() .any(|f| (x - f.0).abs() <= 3 && (z - f.1).abs() <= 3) }) @@ -989,66 +1102,63 @@ impl Streaming { !back }); } - let dirty: Vec<_> = world.dirty_sections.iter().copied().collect(); - let mut columns: FxHashMap<(i32, i32), Option<Box<Region>>> = FxHashMap::default(); - let mut ready_columns: FxHashMap<(i32, i32), bool> = FxHashMap::default(); - for (x, sy, z) in dirty { + // Column by column: the checks below hold for all of a column's + // sections, and columns waiting for light cost one look per frame. + let columns: Vec<_> = world.dirty_sections.columns().collect(); + for ((x, z), sections) in columns { if !world.chunks.contains_key(&(x, z)) { - world.dirty_sections.remove(&(x, sy, z)); + world.dirty_sections.take_column(x, z); continue; } if !self.ranges().in_view(x, z) { - world.dirty_sections.remove(&(x, sy, z)); - self.waiting.insert((x, sy, z)); + world.dirty_sections.take_column(x, z); + for sy in section_indexes(sections) { + self.waiting.insert((x, sy, z)); + } continue; } - // Once per column: the 3x3 check would otherwise repeat for each - // of its sections every frame. A neighbour being lit dirties this - // column again when its light job finishes (new chunks relight - // their eight neighbours), so wait for it rather than mesh once - // per neighbour. Columns still in `dirty_light` here can't be lit - // yet (a neighbour of theirs is missing): waiting for them could - // last until the player comes back. - let (lit, lighting) = (&self.lit, &self.lighting); - let ready = *ready_columns.entry((x, z)).or_insert_with(|| { - (-1..=1).all(|dz| { - (-1..=1).all(|dx| { - let n = (x + dx, z + dz); - lit.contains(&n) && !lighting.contains_key(&n) - }) + // A neighbour being lit dirties this column again when its light + // job finishes (new chunks relight their eight neighbours), so + // wait for it rather than mesh once per neighbour. Columns still + // in `dirty_light` here can't be lit yet (a neighbour of theirs is + // missing): waiting for them could last until the player comes + // back. + let ready = (-1..=1).all(|dz| { + (-1..=1).all(|dx| { + let n = (x + dx, z + dz); + self.lit.contains(&n) && !self.lighting.contains_key(&n) }) }); if !ready { continue; } - let region = columns - .entry((x, z)) - .or_insert_with(|| Self::region(world, x, z)); - let Some(region) = region.clone() else { + let Some(region) = Self::region(world, x, z) else { continue; }; - world.dirty_sections.remove(&(x, sy, z)); - let first = self.meshed.insert((x, z)); - self.sequence += 1; - self.meshing.insert((x, sy, z), self.sequence); - self.submit( - self.distance(x, z) * 4 + if first { 2 } else { 0 }, - Job::Mesh( - x, - sy, - z, - region, - self.sequence, - MeshSettings { - nether: self.nether_shading, - zoom: self.biome_zoom, - blend_radius: self.biome_blend, - smooth_lighting: self.smooth_lighting, - cutout_leaves: self.cutout_leaves, - cull_leaves: self.cull_leaves, - }, - ), - ); + world.dirty_sections.take_column(x, z); + for sy in section_indexes(sections) { + let first = self.meshed.insert((x, z)); + self.sequence += 1; + self.meshing.insert((x, sy, z), self.sequence); + self.submit( + self.distance(x, z) * 4 + if first { 2 } else { 0 }, + Job::Mesh( + x, + sy, + z, + region.clone(), + self.sequence, + MeshSettings { + nether: self.nether_shading, + zoom: self.biome_zoom, + blend_radius: self.biome_blend, + smooth_lighting: self.smooth_lighting, + cutout_leaves: self.cutout_leaves, + cull_leaves: self.cull_leaves, + }, + ), + ); + } } (uploads, removed) } @@ -1089,12 +1199,14 @@ impl Streaming { if positions.is_empty() { return; } - let changed = crate::light::relight( + let mut changed = LightChanges::default(); + crate::light::relight( &mut world.chunks, &self.light_tables, self.sky_light, &positions, false, + &mut changed, ); Self::mark_light_changes(world, &changed); if !self.light_snapshots.is_empty() { @@ -1116,12 +1228,14 @@ impl Streaming { .map(|(_, p)| *p) .collect(); if !positions.is_empty() { - let changed = crate::light::relight( + let mut changed = LightChanges::default(); + crate::light::relight( &mut world.chunks, &self.light_tables, self.sky_light, &positions, true, + &mut changed, ); Self::mark_light_changes(world, &changed); } @@ -1133,26 +1247,15 @@ impl Streaming { } /// Sections whose meshes sample a changed light value (faces and corners /// read light from neighbouring blocks). - fn mark_light_changes(world: &mut World, changed: &[(i32, i32, i32)]) { - let mut last = None; - for &(x, y, z) in changed { - for dz in -1..=1 { - for dy in -1..=1 { - for dx in -1..=1 { - let s = ( - (y + dy - crate::chunk::MIN_Y) >> 4, - (x + dx) >> 4, - (z + dz) >> 4, - ); - if last == Some(s) || !(0..SECTIONS as i32).contains(&s.0) { - continue; - } - last = Some(s); - world.dirty_sections.insert((s.1, s.0, s.2)); - } - } + fn mark_light_changes(world: &mut World, changed: &LightChanges) { + if let Some(log) = &mut world.light_log { + for (x, sy, z) in changed.sections() { + *log.entry((x, z)).or_default() |= 1 << sy; } } + changed.dirty_sections(|section| { + world.dirty_sections.insert(section); + }); } fn ranges(&self) -> Ranges<'_> { Ranges { @@ -1262,21 +1365,21 @@ impl Ranges<'_> { /// circles, so a fixed margin alone misses the corners of those squares /// (render distance 2 never finished the chunks next to the player). fn near_finished(&self, x: i32, z: i32, reach: i32) -> bool { - let near = |c: (i32, i32), radius: i32| { - let gap = |d: i32| i64::from((d.abs() - reach).max(0)); - let (dx, dz) = (gap(x - c.0), gap(z - c.1)); - let r = i64::from(radius + 1); - dx * dx + dz * dz <= r * r - }; - near(self.center, self.radius) || self.extra.iter().any(|&(c, r)| near(c, r)) + let gap = |d: i32| i64::from((d.abs() - reach).max(0)); + let (dx, dz) = (gap(x - self.center.0), gap(z - self.center.1)); + let r = i64::from(self.radius + 1); + // The extra areas are squares: their finished square grown by `reach`. + dx * dx + dz * dz <= r * r || self.in_extra(x, z, 1 + reach) } fn in_range(&self, x: i32, z: i32, margin: i32) -> bool { - self.in_main_range(x, z, margin) - || self.extra.iter().any(|&(c, radius)| { - let r = i64::from(radius + margin); - let (dx, dz) = (i64::from(x - c.0), i64::from(z - c.1)); - dx * dx + dz * dz <= r * r - }) + self.in_main_range(x, z, margin) || self.in_extra(x, z, margin) + } + /// Within `margin` of an extra area's square (Chebyshev distance, as + /// vanilla's ticket levels spread). + fn in_extra(&self, x: i32, z: i32, margin: i32) -> bool { + self.extra + .iter() + .any(|&(c, radius)| (x - c.0).abs().max((z - c.1).abs()) <= radius + margin) } /// The columns meshed for drawing: those vanilla draws around the /// player (`in_view_distance`), and forced ones. @@ -1314,6 +1417,16 @@ impl Drop for Streaming { } } } +/// The section indexes set in a column's bits, lowest first. +fn section_indexes(mut bits: u32) -> impl Iterator<Item = i32> { + std::iter::from_fn(move || { + (bits != 0).then(|| { + let sy = bits.trailing_zeros() as i32; + bits &= bits - 1; + sy + }) + }) +} #[cfg(test)] mod tests { use super::*; @@ -1563,6 +1676,38 @@ mod tests { } } + #[test] + fn generation_scan_looks_at_overlapping_areas_once() { + // A server's players: many areas around one spot, some apart, one + // touching another only at a corner, and part of the land present. + for decorates in [false, true] { + let mut f = GenerationFixture::new(10, decorates); + for i in 0..40 { + f.extra.push(((i % 7 - 3, i / 7 - 3), 19)); + } + f.extra.push(((200, -150), 19)); + f.extra.push(((243, -107), 19)); + f.extra.push(((-60, 5), 4)); + f.forced.insert((90, 90)); + let chunk = Arc::new(Chunk::empty(0, 0)); + for x in -40i32..40 { + for z in -40i32..40 { + match (x * 7 + z * 13).rem_euclid(5) { + 0 => drop(f.world.chunks.insert((x, z), chunk.clone())), + 1 => drop(f.protos.insert((x, z), None)), + 2 if x % 3 == 0 => drop(f.generating.insert((x, z))), + _ => {} + } + } + } + assert!(baseline_candidates(f.input(), usize::MAX).len() > 1000); + for capacity in [usize::MAX, f.generating.len() + 7] { + let mut gate = GenerationScan::default(); + assert!(f.assert_same(&mut gate, capacity)); + } + } + } + #[test] #[ignore = "reproducible actual generation scheduler benchmark; run with --ignored --nocapture"] fn generation_scan_benchmark() { @@ -1731,4 +1876,58 @@ mod tests { } } } + + /// A server player's area (`lan_update_streaming`: the server view + /// distance + 1) finishes vanilla's FULL square: PLAYER_LOADING tickets at + /// level 31 out to the view distance, FULL (33) two chunks past it. Every + /// chunk the player tracks has its 3x3 neighbourhood finished, so it can + /// be lit and sent, and generation reaches vanilla's FEATURES (34) and + /// TERRAIN (35) rings. + #[test] + fn server_player_areas_finish_vanillas_full_square() { + let forced = HashSet::new(); + for view in 2..=32 { + let extra = [((40, -9), view + 1)]; + let r = Ranges { + // The window's own area, far away. + center: (-400, 300), + radius: 2, + extra: &extra, + forced: &forced, + }; + let level = |x: i32, z: i32| 31 + ((x - 40).abs().max((z + 9).abs()) - view).max(0); + for x in 40 - view - 8..=40 + view + 8 { + for z in -9 - view - 8..=-9 + view + 8 { + assert_eq!( + r.in_range(x, z, 1), + level(x, z) <= 33, + "view {view}: ({x}, {z})" + ); + assert_eq!( + r.requested(x, z, true), + level(x, z) <= 35, + "view {view}: ({x}, {z})" + ); + assert_eq!( + r.unloaded(x, z, true), + level(x, z) > 35, + "view {view}: ({x}, {z})" + ); + // ChunkTrackingView.contains, at any view distance the player may ask for. + let tracked = |v: i64| { + let gap = |d: i32| i64::from(((d).abs() - 2).max(0)); + gap(x - 40).pow(2) + gap(z + 9).pow(2) < v * v + }; + if tracked(i64::from(view)) { + for (dx, dz) in (-1..=1).flat_map(|dx| (-1..=1).map(move |dz| (dx, dz))) { + assert!( + r.in_range(x + dx, z + dz, 1), + "view {view}: ({x}, {z}) tracked, not lit" + ); + } + } + } + } + } + } } diff --git a/crates/horizon-client/src/telemetry.rs b/crates/horizon-client/src/telemetry.rs index 9e83655fa..d0140f941 100644 --- a/crates/horizon-client/src/telemetry.rs +++ b/crates/horizon-client/src/telemetry.rs @@ -3,6 +3,7 @@ //! summary, in the outbox. It never sends anything itself: the launcher does, at its next start. //! Without the player's yes for that kind nothing is kept, and crash-reports/ stays local. use std::path::Path; +use std::sync::atomic::{AtomicU32, Ordering}; use std::sync::{Mutex, OnceLock}; use std::time::Instant; @@ -18,6 +19,11 @@ const MIN_SECONDS: f32 = 60.0; const CRASH_DETAILS: [&str; 3] = ["Horizon Version", "Graphics Adapter", "Render Settings"]; static STARTED: OnceLock<Instant> = OnceLock::new(); +/// When the menu bundle became ready, so the time to the title screen measures +/// the menu and not the world bundle loaded behind it. +static UI_READY_MS: AtomicU32 = AtomicU32::new(u32::MAX); +static CORE_READY_MS: AtomicU32 = AtomicU32::new(u32::MAX); +static REST_READY_MS: AtomicU32 = AtomicU32::new(u32::MAX); /// The GPU's name, driver and backend, once the renderer has its adapter. static GPU: Mutex<Option<(String, String, String)>> = Mutex::new(None); @@ -26,6 +32,34 @@ pub fn mark_start() { STARTED.get_or_init(Instant::now); } +/// The menu bundle is ready: the title backdrop can draw. The first writer wins. +pub fn mark_ui_ready() { + if let Some(started) = STARTED.get() { + let ms = started.elapsed().as_millis() as u32; + let _ = UI_READY_MS.compare_exchange(u32::MAX, ms, Ordering::Relaxed, Ordering::Relaxed); + } +} + +pub fn mark_core_ready() { + mark_ready(&CORE_READY_MS); +} + +pub fn mark_rest_ready() { + mark_ready(&REST_READY_MS); +} + +fn mark_ready(mark: &AtomicU32) { + if let Some(started) = STARTED.get() { + let ms = started.elapsed().as_millis().min(u128::from(u32::MAX - 1)) as u32; + let _ = mark.compare_exchange(u32::MAX, ms, Ordering::Relaxed, Ordering::Relaxed); + } +} + +fn readiness_ms(mark: &AtomicU32) -> Option<u32> { + let ms = mark.load(Ordering::Relaxed); + (ms != u32::MAX).then_some(ms) +} + pub fn set_gpu(name: &str, driver: String, backend: String) { *GPU.lock().unwrap_or_else(|e| e.into_inner()) = Some((name.to_owned(), driver, backend)); } @@ -138,7 +172,12 @@ impl Session { pub fn title_shown(&mut self) { if !self.title_measured { self.title_measured = true; - self.to_title_ms = STARTED.get().map(|s| s.elapsed().as_millis() as u32); + let ui_ms = UI_READY_MS.load(Ordering::Relaxed); + self.to_title_ms = if ui_ms != u32::MAX { + Some(ui_ms) + } else { + STARTED.get().map(|s| s.elapsed().as_millis() as u32) + }; } } @@ -147,6 +186,11 @@ impl Session { self.opening.get_or_insert_with(Instant::now); } + /// An entry queued during startup was cancelled before opening a world. + pub fn cancel_world_opening(&mut self) { + self.opening = None; + } + /// A frame drawn while playing (not in menus or loading). pub fn frame(&mut self, ms: f32) { if let Some(opening) = self.opening.take() @@ -175,6 +219,8 @@ impl Session { peak_ram_mb: peak_ram_mb(), to_title_ms: self.to_title_ms, to_world_ms: self.to_world_ms, + to_core_ms: self.to_title_ms.and_then(|_| readiness_ms(&CORE_READY_MS)), + to_rest_ms: self.to_title_ms.and_then(|_| readiness_ms(&REST_READY_MS)), }) } @@ -377,6 +423,20 @@ mod tests { assert_eq!(pending[0].1.kind(), Kind::Crash); } + #[test] + fn cancelled_startup_entry_does_not_time_menu_browsing() { + let mut session = Session::default(); + session.world_opening(); + assert!(session.opening.is_some()); + session.cancel_world_opening(); + assert!(session.opening.is_none()); + session.frame(16.0); + assert!(session.to_world_ms.is_none()); + session.world_opening(); + session.frame(16.0); + assert!(session.to_world_ms.is_some()); + } + #[test] fn a_session_is_kept_only_with_consent_and_once_long_enough() { let home = home("session"); diff --git a/crates/horizon-client/src/terrain.wgsl b/crates/horizon-client/src/terrain.wgsl index 4689e7d2b..be3405b23 100644 --- a/crates/horizon-client/src/terrain.wgsl +++ b/crates/horizon-client/src/terrain.wgsl @@ -31,6 +31,10 @@ struct Globals { @group(0) @binding(5) var sprites_1: texture_2d_array<f32>; @group(0) @binding(6) var sprites_2: texture_2d_array<f32>; @group(0) @binding(7) var sprites_3: texture_2d_array<f32>; +@group(0) @binding(8) var sprites_4: texture_2d_array<f32>; +@group(0) @binding(9) var sprites_5: texture_2d_array<f32>; +@group(0) @binding(10) var sprites_6: texture_2d_array<f32>; +@group(0) @binding(11) var sprites_7: texture_2d_array<f32>; // How many sprite arrays hold layers (a pipeline constant): 1 for vanilla, // which then samples exactly as before. override SPRITE_ARRAYS: u32 = 1u; @@ -47,7 +51,11 @@ fn sprite(uv: vec2<f32>, layer: u32) -> vec4<f32> { case 0u: { return textureSampleGrad(sprites, sprite_sampler, uv, l, dx, dy); } case 1u: { return textureSampleGrad(sprites_1, sprite_sampler, uv, l, dx, dy); } case 2u: { return textureSampleGrad(sprites_2, sprite_sampler, uv, l, dx, dy); } - default: { return textureSampleGrad(sprites_3, sprite_sampler, uv, l, dx, dy); } + case 3u: { return textureSampleGrad(sprites_3, sprite_sampler, uv, l, dx, dy); } + case 4u: { return textureSampleGrad(sprites_4, sprite_sampler, uv, l, dx, dy); } + case 5u: { return textureSampleGrad(sprites_5, sprite_sampler, uv, l, dx, dy); } + case 6u: { return textureSampleGrad(sprites_6, sprite_sampler, uv, l, dx, dy); } + default: { return textureSampleGrad(sprites_7, sprite_sampler, uv, l, dx, dy); } } } @@ -64,11 +72,35 @@ 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 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<f32>, + @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 { - let pos = position + 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: +// the block gives its resident slot, the slot its camera-relative origin. +@group(1) @binding(0) var<storage, read> block_slots: array<u32>; +@group(1) @binding(1) var<storage, read> slot_origins: array<vec4<f32>>; +@vertex fn vs_terrain_flat( + @builtin(vertex_index) vertex: u32, + @location(0) position: vec3<f32>, @location(1) uv: vec2<u32>, + @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, 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 { + let pos = position + origin; var out: TerrainOut; out.clip = globals.view_proj * vec4(pos, 1.0); out.uv = uv; diff --git a/crates/horizon-client/src/testgen.rs b/crates/horizon-client/src/testgen.rs index e0a5d49b3..cded7ae47 100644 --- a/crates/horizon-client/src/testgen.rs +++ b/crates/horizon-client/src/testgen.rs @@ -4,24 +4,24 @@ use crate::{chunk::Chunk, streaming::ChunkGenerator}; use horizon_world::registry::BlockRegistry; pub struct TestTerrain { - stone: u16, - dirt: u16, - grass: u16, - water: u16, - sand: u16, - bedrock: u16, - log: u16, - leaves: u16, - short_grass: u16, + stone: u32, + dirt: u32, + grass: u32, + water: u32, + sand: u32, + bedrock: u32, + log: u32, + leaves: u32, + short_grass: u32, plains: u16, } impl TestTerrain { pub fn new(registry: &BlockRegistry, biomes: &crate::biome::Biomes) -> crate::Result<Self> { - let id = |name: &str| -> crate::Result<u16> { + let id = |name: &str| -> crate::Result<u32> { Ok(registry .default_state(name) .ok_or_else(|| format!("missing {name}"))? - .0 as u16) + .0) }; Ok(Self { stone: id("minecraft:stone")?, @@ -41,7 +41,7 @@ impl TestTerrain { ] .into(), })? - .0 as u16, + .0, short_grass: id("minecraft:short_grass")?, plains: biomes.id("minecraft:plains"), }) @@ -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"); + } +} diff --git a/crates/horizon-client/src/text.rs b/crates/horizon-client/src/text.rs index e9710e442..3874efd17 100644 --- a/crates/horizon-client/src/text.rs +++ b/crates/horizon-client/src/text.rs @@ -233,6 +233,10 @@ impl Text { } /// The `Component` as vanilla's JSON (ComponentSerialization): the one /// built through the constructors, else the spans as literal siblings. + /// The key of a `Component.translatable` built through [`Text::translate`]. + pub fn translation_key(&self) -> Option<&str> { + self.source.as_deref()?.get("translate")?.as_str() + } pub fn component(&self) -> Value { if let Some(c) = &self.source { return (**c).clone(); @@ -263,6 +267,24 @@ impl Text { _ => c, } } + /// Style fields the spans don't model (an entity hover, an insertion), + /// set on the component sent to vanilla only. + pub fn with_component_fields(mut self, fields: Vec<(String, Value)>) -> Self { + let mut c = self.component(); + if let Value::Object(o) = &mut c { + for (k, v) in fields { + o.entry(k).or_insert(v); + } + } + self.source = Some(c.into()); + self + } + /// The same spans, sent to vanilla as `component` (a shape the spans' + /// own JSON would not take, such as a styled root with siblings). + pub fn with_component(mut self, component: Value) -> Self { + self.source = Some(component.into()); + self + } pub fn with_color(self, color: u32) -> Self { self.with_style(&Style::color(color)) } diff --git a/crates/horizon-client/src/tick_chunk_membership.rs b/crates/horizon-client/src/tick_chunk_membership.rs index 42785f289..a7e934443 100644 --- a/crates/horizon-client/src/tick_chunk_membership.rs +++ b/crates/horizon-client/src/tick_chunk_membership.rs @@ -10,6 +10,9 @@ pub struct TickChunkMembership { revision: Option<u64>, added: Vec<Key>, removed: Vec<Key>, + /// Loaded chunks with saved ticks that have not started ticking yet + /// (`LevelChunkTicks.pendingTicks`, unpacked by `startTickingChunk`). + waiting: HashSet<Key>, } impl TickChunkMembership { @@ -20,6 +23,7 @@ impl TickChunkMembership { self.revision = None; self.added.clear(); self.removed.clear(); + self.waiting.clear(); } /// Returns false in O(1) when resident membership is unchanged. On change, @@ -36,6 +40,7 @@ impl TickChunkMembership { true } else { self.removed.push(*key); + self.waiting.remove(key); false } }); @@ -62,6 +67,28 @@ impl TickChunkMembership { pub fn removed(&self) -> &[Key] { &self.removed } + + /// `key` was registered with saved ticks to unpack once it ticks. + pub fn wait(&mut self, key: Key) { + self.waiting.insert(key); + } + + /// The waiting chunks that `ticking` says now tick, no longer waiting. + pub fn start_ticking(&mut self, mut ticking: impl FnMut(Key) -> bool) -> Vec<Key> { + if self.waiting.is_empty() { + return Vec::new(); + } + let started: Vec<Key> = self + .waiting + .iter() + .copied() + .filter(|&k| ticking(k)) + .collect(); + for key in &started { + self.waiting.remove(key); + } + started + } } #[cfg(test)] @@ -124,6 +151,24 @@ mod tests { compare(&mut cache, &mut old, &map); } + #[test] + fn waiting_chunks_start_ticking_once_and_unloaded_ones_stop_waiting() { + let mut map = ChunkMap::default(); + let mut cache = TickChunkMembership::default(); + for key in [(0, 0), (1, 0), (2, 0)] { + map.insert(key, Arc::new(Chunk::empty(key.0, key.1))); + cache.wait(key); + } + cache.synchronize(&map); + assert_eq!(cache.start_ticking(|k| k == (0, 0)), vec![(0, 0)]); + assert!(cache.start_ticking(|k| k == (0, 0)).is_empty()); + map.remove(&(1, 0)); + cache.synchronize(&map); + let mut started = cache.start_ticking(|_| true); + started.sort_unstable(); + assert_eq!(started, vec![(2, 0)]); + } + #[test] fn scheduler_keeps_saved_ticks_on_stable_membership_and_removes_unloaded_chunks() { use horizon_core::{ diff --git a/crates/horizon-client/src/ticket_levels.rs b/crates/horizon-client/src/ticket_levels.rs new file mode 100644 index 000000000..216815de1 --- /dev/null +++ b/crates/horizon-client/src/ticket_levels.rs @@ -0,0 +1,232 @@ +//! Which chunks tick, from the chunk tickets of `DistanceManager`. +//! +//! Every player holds two kinds of ticket: +//! - `PLAYER_LOADING` at level 31 (`ENTITY_TICKING`) on every chunk within +//! the server's view distance (Chebyshev), from `PlayerTicketTracker`; +//! - `PLAYER_SIMULATION` at level `31 - simulationDistance` on its own chunk. +//! +//! A `/forceload`ed chunk holds a `FORCED` ticket at level 31 of both kinds. +//! Levels rise by one per chunk away from a ticket (`ChunkTracker`) and each +//! chunk takes the lowest. The loading level gives the chunk's +//! `FullChunkStatus` (`ChunkLevel.fullStatus`: 31 entity ticking, 32 block +//! ticking, 33 full) and the simulation level gives what +//! `inEntityTickingRange` (31 or less) and `inBlockTickingRange` (32 or less) +//! answer. Vanilla needs both: +//! - entities tick where both levels are entity ticking (`ServerLevel.tick`, +//! and `EntityTickList` holds only the entities of entity-ticking chunks); +//! - random ticks run in the simulation tracker's entity-ticking chunks that +//! have a ticking chunk (`ChunkMap.forEachBlockTickingChunk`); +//! - scheduled ticks, block events and block entities run where both are +//! block ticking (`isPositionTickingWithEntitiesLoaded`, +//! `shouldTickBlocksAt`, `LevelChunk.isTicking`). +//! +//! Whoever the players are (the local player, LAN guests, a dedicated +//! server's players), they count the same. +use crate::fast_hash::FxHashMap; +use std::sync::Arc; + +/// `ChunkLevel`'s levels. +pub const ENTITY_TICKING: u8 = 31; +pub const BLOCK_TICKING: u8 = 32; +/// What a chunk with no ticket near it has (`FULL` and above count as none). +const NONE: (u8, u8) = (BLOCK_TICKING + 1, BLOCK_TICKING + 1); + +/// The tickets of one level: where its players are, its forced chunks, and +/// the server's view and simulation distances. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Sources { + pub players: Vec<(i32, i32)>, + pub forced: Vec<(i32, i32)>, + pub view_distance: i32, + pub simulation_distance: i32, +} + +/// Each chunk's (loading, simulation) level, for the chunks where either is +/// block ticking or lower; `None` ticks everywhere (a game with no server and +/// no player: the headless fixtures). +#[derive(Clone, Default)] +pub struct Levels(Option<Arc<FxHashMap<(i32, i32), (u8, u8)>>>); + +impl Levels { + /// Chunk coordinates. + fn get(&self, x: i32, z: i32) -> Option<(u8, u8)> { + let map = self.0.as_ref()?; + Some(map.get(&(x, z)).copied().unwrap_or(NONE)) + } + /// `DistanceManager.inEntityTickingRange`, in an entity-ticking chunk: + /// where entities tick. + pub fn entity_ticking(&self, x: i32, z: i32) -> bool { + self.get(x, z) + .is_none_or(|(l, s)| l <= ENTITY_TICKING && s <= ENTITY_TICKING) + } + /// `inBlockTickingRange` in a block-ticking chunk: where scheduled ticks, + /// block events and block entities run. + pub fn block_ticking(&self, x: i32, z: i32) -> bool { + self.get(x, z) + .is_none_or(|(l, s)| l <= BLOCK_TICKING && s <= BLOCK_TICKING) + } + /// `inEntityTickingRange` in a block-ticking chunk: where random ticks run. + pub fn random_ticking(&self, x: i32, z: i32) -> bool { + self.get(x, z) + .is_none_or(|(l, s)| l <= BLOCK_TICKING && s <= ENTITY_TICKING) + } + /// `ChunkHolder.getFullStatus` is block ticking or more: the chunk's + /// ticking future can complete (its saved ticks unpack then). + pub fn loaded_ticking(&self, x: i32, z: i32) -> bool { + self.get(x, z).is_none_or(|(l, _)| l <= BLOCK_TICKING) + } + /// The same as [`Self::entity_ticking`] at a block position. + pub fn entity_ticking_at(&self, x: i32, z: i32) -> bool { + self.entity_ticking(x >> 4, z >> 4) + } + /// The same as [`Self::block_ticking`] at a block position. + pub fn block_ticking_at(&self, x: i32, z: i32) -> bool { + self.block_ticking(x >> 4, z >> 4) + } +} + +/// The levels, rebuilt only when their sources change (a player crossing +/// into another chunk, a distance or a forced chunk changing). +#[derive(Default)] +pub struct ChunkTickets { + sources: Option<Sources>, + levels: Levels, +} + +impl ChunkTickets { + /// `None`: tick everywhere. + pub fn update(&mut self, mut sources: Option<Sources>) { + if let Some(s) = sources.as_mut() { + s.players.sort_unstable(); + s.players.dedup(); + s.forced.sort_unstable(); + } + if sources == self.sources { + return; + } + self.levels = Levels(sources.as_ref().map(|s| Arc::new(compute(s)))); + self.sources = sources; + } + pub fn levels(&self) -> Levels { + self.levels.clone() + } +} + +fn compute(s: &Sources) -> FxHashMap<(i32, i32), (u8, u8)> { + let view = s.view_distance.clamp(0, 32); + let simulation = s.simulation_distance.clamp(0, 32); + let mut levels: FxHashMap<(i32, i32), (u8, u8)> = FxHashMap::default(); + let mut lower = |key, loading: i32, sim: i32| { + let e = levels.entry(key).or_insert(NONE); + e.0 = e.0.min(loading.min(i32::from(NONE.0)) as u8); + e.1 = e.1.min(sim.min(i32::from(NONE.1)) as u8); + }; + // PLAYER_LOADING within the view distance, PLAYER_SIMULATION at the + // player's chunk (`getPlayerTicketLevel`: never below 0). + let player_sim = (i32::from(ENTITY_TICKING) - simulation).max(0); + let reach = (view + 1).max(i32::from(BLOCK_TICKING) - player_sim); + for &(px, pz) in &s.players { + for dz in -reach..=reach { + for dx in -reach..=reach { + let d = dx.abs().max(dz.abs()); + lower( + (px + dx, pz + dz), + i32::from(ENTITY_TICKING) + (d - view).max(0), + player_sim + d, + ); + } + } + } + for &(fx, fz) in &s.forced { + for dz in -1i32..=1 { + for dx in -1i32..=1 { + let level = i32::from(ENTITY_TICKING) + dx.abs().max(dz.abs()); + lower((fx + dx, fz + dz), level, level); + } + } + } + levels.retain(|_, l| *l != NONE); + levels +} + +#[cfg(test)] +mod tests { + use super::*; + + fn levels(players: &[(i32, i32)], forced: &[(i32, i32)], view: i32, sim: i32) -> Levels { + let mut t = ChunkTickets::default(); + t.update(Some(Sources { + players: players.to_vec(), + forced: forced.to_vec(), + view_distance: view, + simulation_distance: sim, + })); + t.levels() + } + + #[test] + fn simulation_distance_inside_the_view() { + // view 8, simulation 4: entities and random ticks out to 4 chunks, + // scheduled ticks out to 5. + let l = levels(&[(0, 0)], &[], 8, 4); + assert!(l.entity_ticking(4, -4) && !l.entity_ticking(5, 0)); + assert!(l.random_ticking(-4, 4) && !l.random_ticking(0, 5)); + assert!(l.block_ticking(5, 5) && !l.block_ticking(6, 0)); + assert!(l.loaded_ticking(9, 9) && !l.loaded_ticking(10, 0)); + } + + #[test] + fn view_distance_inside_the_simulation() { + // view 4, simulation 10: what isn't loaded at entity ticking (beyond + // the view distance) runs no entities, and block ticks stop where the + // chunk is only full (beyond the view distance + 1). + let l = levels(&[(0, 0)], &[], 4, 10); + assert!(l.entity_ticking(4, 0) && !l.entity_ticking(5, 0)); + assert!(l.random_ticking(5, 0) && !l.random_ticking(6, 0)); + assert!(l.block_ticking(5, 5) && !l.block_ticking(6, 0)); + } + + #[test] + fn every_player_and_forced_chunk() { + let l = levels(&[(0, 0), (40, 0)], &[(-100, 7)], 4, 4); + assert!(l.entity_ticking(44, 0) && l.entity_ticking(0, -4)); + assert!(!l.entity_ticking(20, 0)); + // A forced chunk entity ticks, its neighbours only block tick. + assert!(l.entity_ticking(-100, 7) && !l.entity_ticking(-101, 7)); + assert!(l.block_ticking(-101, 8) && !l.block_ticking(-102, 7)); + assert!(!l.random_ticking(-101, 7)); + assert!(l.entity_ticking_at(-1600, 112) && !l.block_ticking_at(-1617, 112)); + } + + #[test] + fn no_tickets_tick_nothing_and_no_sources_everything() { + let none = levels(&[], &[], 10, 10); + assert!(!none.block_ticking(0, 0) && !none.entity_ticking(0, 0)); + let everywhere = ChunkTickets::default().levels(); + assert!(everywhere.random_ticking(1000, -1000)); + } + + #[test] + fn rebuilt_only_when_sources_change() { + let mut t = ChunkTickets::default(); + let s = |players: Vec<(i32, i32)>| { + Some(Sources { + players, + forced: Vec::new(), + view_distance: 2, + simulation_distance: 2, + }) + }; + t.update(s(vec![(0, 0), (5, 5)])); + let first = t.levels(); + t.update(s(vec![(5, 5), (0, 0)])); + assert!(Arc::ptr_eq( + first.0.as_ref().unwrap(), + t.levels().0.as_ref().unwrap() + )); + t.update(s(vec![(1, 0), (5, 5)])); + assert!(t.levels().entity_ticking(3, 0) && !first.entity_ticking(3, 0)); + t.update(None); + assert!(t.levels().0.is_none()); + } +} diff --git a/crates/horizon-client/src/ui_theme.rs b/crates/horizon-client/src/ui_theme.rs index 0f4a2d5a7..2c30bf219 100644 --- a/crates/horizon-client/src/ui_theme.rs +++ b/crates/horizon-client/src/ui_theme.rs @@ -1044,6 +1044,49 @@ impl<'a> Ui<'a> { self.opacity = saved; hover } + /// A search field styled like the launcher's rounded text inputs. + pub fn text_field(&mut self, id: u64, rect: [f32; 4], value: &str, focused: bool) { + let [x, y, w, h] = rect; + self.record_label(value, rect, true, crate::widgets::Kind::Button); + let hover = self.interact(id, x, y, w, h, true); + self.rounded( + x, + y, + w, + h, + 8.0, + if focused { color::HOVER } else { color::RAISED }, + ); + self.rounded( + x, + y, + w, + h, + 8.0, + color::with_alpha(color::HOVER, hover * 0.5), + ); + let display = if value.is_empty() { + "Search settings".to_owned() + } else if focused { + format!("{value}|") + } else { + value.to_owned() + }; + let display = self.fit(Face::Regular, BODY, &display, w - 24.0); + self.text_line( + Face::Regular, + BODY, + h, + x + 12.0, + y, + if value.is_empty() { + color::FAINT + } else { + color::TEXT + }, + &display, + ); + } /// The width the launcher's button gives a label (padding 16, or 24 large). pub fn button_width(&self, label: &str, large: bool) -> f32 { let (size, pad) = if large { (LARGE, 24.0) } else { (BODY, 16.0) }; diff --git a/crates/horizon-client/src/vanillagen.rs b/crates/horizon-client/src/vanillagen.rs index fc18e8404..c44e1f542 100644 --- a/crates/horizon-client/src/vanillagen.rs +++ b/crates/horizon-client/src/vanillagen.rs @@ -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, @@ -56,6 +74,19 @@ impl VanillaTerrain { biomes: &Biomes, seed: i64, dimension: &str, + ) -> crate::Result<Self> { + Self::for_world(dir, registry, biomes, seed, dimension, true) + } + /// A dimension of a world whose `generate_structures` is `structures` + /// (`ChunkGenerator.createStructures` and `findNearestMapStructure` do + /// nothing without). + pub fn for_world( + dir: &Path, + registry: &'static BlockRegistry, + biomes: &Biomes, + seed: i64, + dimension: &str, + structures: bool, ) -> crate::Result<Self> { let data: Arc<GeneratorData> = shared_data(dir, registry)?; let biome_map = data @@ -64,7 +95,7 @@ impl VanillaTerrain { .map(|name| biomes.id(name)) .collect(); let generator = - OverworldGenerator::for_dimension(data, seed, "minecraft:normal", dimension)?; + OverworldGenerator::for_world(data, seed, "minecraft:normal", dimension, structures)?; let unsupported = generator.unsupported_features(); if !unsupported.is_empty() { log::warn!( @@ -106,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 @@ -119,7 +152,7 @@ impl VanillaTerrain { chunk.terrain.min_y().max(MIN_Y), (chunk.terrain.min_y() + chunk.terrain.height()).min(MIN_Y + HEIGHT), ); - let mut values = Box::new([0u16; 4096]); + let mut values = Box::new([0u32; 4096]); for si in 0..SECTIONS as i32 { let base = MIN_Y + si * 16; if base + 16 <= lo || base >= hi { @@ -130,7 +163,7 @@ impl VanillaTerrain { for y in base.max(lo)..(base + 16).min(hi) { for z in 0..16 { for x in 0..16 { - let id = chunk.block(bx + x, y, bz + z).0 as u16; + let id = chunk.block(bx + x, y, bz + z).0; values[((y & 15) << 8 | z << 4 | x) as usize] = id; any |= id != 0; } @@ -265,6 +298,30 @@ 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 + /// spawning or a mob asks which structures hold a position (a village + /// start took 70 to 110 ms there, one tick each, after a world loaded). + fn prepare_loaded(&self, worker: &mut GeneratorWorker, chunk: &Chunk) { + let mut starts: Vec<(i32, i32)> = chunk.structure_refs.iter().map(|(_, at)| *at).collect(); + starts.sort_unstable(); + starts.dedup(); + for (x, z) in starts { + worker.structure_starts(ChunkPos::new(x, z)); + } + } fn proto(&self, worker: &mut GeneratorWorker, x: i32, z: i32) -> Proto { match worker.generate_proto(ChunkPos::new(x, z)) { Ok(chunk) => Box::new(VanillaProto::Ready(Box::new(chunk))), diff --git a/crates/horizon-client/src/vanillagen/lod.rs b/crates/horizon-client/src/vanillagen/lod.rs new file mode 100644 index 000000000..7cce03ae4 --- /dev/null +++ b/crates/horizon-client/src/vanillagen/lod.rs @@ -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(¢er)) +} +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(®ion[&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(¢er).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(()) + } +} diff --git a/crates/horizon-client/src/vanillagen/lod/vegetation.rs b/crates/horizon-client/src/vanillagen/lod/vegetation.rs new file mode 100644 index 000000000..a9952cb20 --- /dev/null +++ b/crates/horizon-client/src/vanillagen/lod/vegetation.rs @@ -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 + }); + } + } +} diff --git a/crates/horizon-client/src/window.rs b/crates/horizon-client/src/window.rs index 7c77d85c6..33ff52111 100644 --- a/crates/horizon-client/src/window.rs +++ b/crates/horizon-client/src/window.rs @@ -128,6 +128,20 @@ pub fn vsync_present_mode(supported: &[PresentMode], vsync: bool) -> Option<Pres preferred.iter().copied().find(|m| supported.contains(m)) } +/// The present mode for the VSync option and Horizon's present-mode setting +/// (`None` for Auto). VSync on always means Fifo, in a window and in +/// fullscreen alike, as in vanilla; the setting chooses only while VSync is +/// off, when the surface supports it, and otherwise vanilla's choice applies. +pub fn chosen_present_mode( + supported: &[PresentMode], + vsync: bool, + setting: Option<PresentMode>, +) -> Option<PresentMode> { + setting + .filter(|m| !vsync && supported.contains(m)) + .or_else(|| vsync_present_mode(supported, vsync)) +} + /// `FramerateLimiter.limitDisplayFPS` with an injectable clock so the sleep /// and spin decisions are testable. #[derive(Clone, Debug)] @@ -415,4 +429,23 @@ mod tests { Some(PresentMode::Fifo) ); } + #[test] + fn vsync_on_waits_for_the_display_whatever_the_present_mode() { + use PresentMode::*; + let all = [Mailbox, Fifo, Immediate]; + for setting in [None, Some(Fifo), Some(Mailbox), Some(Immediate)] { + assert_eq!(chosen_present_mode(&all, true, setting), Some(Fifo)); + } + assert_eq!(chosen_present_mode(&all, false, None), Some(Immediate)); + assert_eq!( + chosen_present_mode(&all, false, Some(Mailbox)), + Some(Mailbox) + ); + assert_eq!(chosen_present_mode(&all, false, Some(Fifo)), Some(Fifo)); + // Metal has no Mailbox: vanilla's choice instead. + assert_eq!( + chosen_present_mode(&[Fifo, Immediate], false, Some(Mailbox)), + Some(Immediate) + ); + } } diff --git a/crates/horizon-client/src/world.rs b/crates/horizon-client/src/world.rs index 44e442ec1..af7c1671b 100644 --- a/crates/horizon-client/src/world.rs +++ b/crates/horizon-client/src/world.rs @@ -8,6 +8,46 @@ use std::{ sync::Arc, }; +/// Sections (x, section index, z) waiting to be remeshed, kept by column +/// (a bit per section index), so the mesh scheduler looks at each column once +/// rather than at every section of it. +#[derive(Clone, Debug, Default)] +pub struct DirtySections { + columns: crate::fast_hash::FxHashMap<(i32, i32), u32>, +} +const _: () = assert!(SECTIONS <= 32); +impl DirtySections { + pub fn insert(&mut self, (x, sy, z): (i32, i32, i32)) { + debug_assert!((0..SECTIONS as i32).contains(&sy)); + *self.columns.entry((x, z)).or_default() |= 1 << sy; + } + pub fn contains(&self, &(x, sy, z): &(i32, i32, i32)) -> bool { + (0..SECTIONS as i32).contains(&sy) + && self.columns.get(&(x, z)).is_some_and(|m| m & 1 << sy != 0) + } + /// The dirty section indexes of column (x, z), as bits, removed. + pub fn take_column(&mut self, x: i32, z: i32) -> u32 { + self.columns.remove(&(x, z)).unwrap_or(0) + } + /// Keep the columns `keep` accepts. + pub fn retain_columns(&mut self, mut keep: impl FnMut(i32, i32) -> bool) { + self.columns.retain(|&(x, z), _| keep(x, z)); + } + /// The columns holding dirty sections, with their section bits. + pub fn columns(&self) -> impl Iterator<Item = ((i32, i32), u32)> + '_ { + self.columns.iter().map(|(&c, &m)| (c, m)) + } + pub fn clear(&mut self) { + self.columns.clear(); + } + pub fn is_empty(&self) -> bool { + self.columns.is_empty() + } + /// How many sections are dirty. + pub fn len(&self) -> usize { + self.columns.values().map(|m| m.count_ones() as usize).sum() + } +} pub struct World { pub fast_lava: bool, pub chunks: crate::chunk_map::ChunkMap, @@ -16,17 +56,18 @@ pub struct World { pub health_rules: crate::health::HealthRules, pub air: BlockStateId, /// Sections whose geometry changed and need remeshing. - pub dirty_sections: crate::fast_hash::FxHashSet<(i32, i32, i32)>, + pub dirty_sections: DirtySections, /// Chunks whose light must be recomputed. pub dirty_light: crate::fast_hash::FxHashSet<(i32, i32)>, /// Edits on lit chunks (position, old state, new state), relit /// incrementally by streaming. - pub light_edits: Vec<((i32, i32, i32), u16, u16)>, + pub light_edits: Vec<((i32, i32, i32), u32, u32)>, /// Saved entities of chunks that just loaded, spawned by the game. pub loaded_entities: Vec<((i32, i32), Vec<horizon_world::nbt::Tag>)>, /// Chunks changed since the last save. pub modified: HashSet<(i32, i32)>, - pub block_entities: HashMap<(i32, i32, i32), crate::containers::BlockEntity>, + /// Block entities, and the order they tick in. + pub block_entities: crate::block_entities::BlockEntities, /// Each chest's `ContainerOpenersCounter`: opener count and largest /// interaction range. pub chest_openers: HashMap<(i32, i32, i32), (i32, f64)>, @@ -36,7 +77,7 @@ pub struct World { /// blocks, which need a block entity). pub watched: Vec<bool>, /// Writes of watched states: position, previous and new state. - pub watched_writes: Vec<((i32, i32, i32), u16, u16)>, + pub watched_writes: Vec<((i32, i32, i32), u32, u32)>, /// `ChunkAccess.inhabitedTime` of resident chunks: ticks a player spent /// within spawning range (local difficulty). pub inhabited: HashMap<(i32, i32), i64>, @@ -46,6 +87,10 @@ pub struct World { /// Every block position written since the log was last drained, kept /// only while the world is open to the network (see `lan`). pub block_log: Option<Vec<(i32, i32, i32)>>, + /// The sections whose light changed since the log was last drained, as + /// a bit per section of each column, kept while `block_log` is + /// (`ChunkHolder.sectionLightChanged`). + pub light_log: Option<crate::fast_hash::FxHashMap<(i32, i32), u32>>, /// The villager POIs of each section (`PoiManager`'s sections). pub pois: crate::poi_sections::PoiSections<crate::mobs::villager_brain::Poi>, } @@ -122,7 +167,7 @@ impl World { light_edits: Vec::new(), loaded_entities: Vec::new(), modified: HashSet::new(), - block_entities: HashMap::new(), + block_entities: Default::default(), chest_openers: HashMap::new(), block_names: HashMap::new(), watched: Vec::new(), @@ -132,12 +177,17 @@ impl World { day_time: 1000, weather: Weather::default(), block_log: None, + light_log: None, pois: Default::default(), } } /// A controlled blockstate scene for the native scene probes: material /// layers, a stepped pyramid and the model gallery. Not generated terrain. - pub fn test_scene(assets: &crate::assets::Assets, radius: i32) -> crate::Result<Self> { + pub fn test_scene( + assets: &crate::assets::Assets, + render: &crate::assets::RenderAssets, + radius: i32, + ) -> crate::Result<Self> { let air = assets .registry .default_state("minecraft:air") @@ -182,7 +232,7 @@ impl World { } } } - for (position, state) in &assets.model_placements { + for (position, state) in &render.model_placements { world.set(position.x, position.y, position.z, *state)?; } world.revision = 0; @@ -198,12 +248,13 @@ impl World { if y < MIN_Y || y >= MIN_Y + (SECTIONS as i32) * 16 { return Err("write outside world height".into()); } - let previous = Arc::make_mut(chunk).set(x & 15, y, z & 15, state.0 as u16); - if u32::from(previous) != state.0 { - let watches = |id: u16| self.watched.get(usize::from(id)).copied().unwrap_or(false); - if watches(previous) || watches(state.0 as u16) { - self.watched_writes - .push(((x, y, z), previous, state.0 as u16)); + let previous = Arc::make_mut(chunk).set(x & 15, y, z & 15, state.0); + if previous != state.0 { + self.block_entities + .state_changed((x, y, z), previous, state.0); + let watches = |id: u32| self.watched.get(id as usize).copied().unwrap_or(false); + if watches(previous) || watches(state.0) { + self.watched_writes.push(((x, y, z), previous, state.0)); } self.revision = self.revision.wrapping_add(1); if let Some(log) = &mut self.block_log { @@ -225,7 +276,7 @@ impl World { } let _ = sy; if self.chunks.get(&(x >> 4, z >> 4)).is_some_and(|c| c.lit) { - self.light_edits.push(((x, y, z), previous, state.0 as u16)); + self.light_edits.push(((x, y, z), previous, state.0)); } else { for dz in -1..=1 { for dx in -1..=1 { @@ -236,11 +287,28 @@ impl World { } Ok(()) } + /// `getHeight(Heightmap.Types.WORLD_SURFACE, x, z)`: one above the highest + /// block that isn't `air`, or `MIN_Y` (also where the chunk isn't loaded). + pub fn world_surface(&self, x: i32, z: i32, air: impl Fn(BlockStateId) -> bool) -> i32 { + self.chunks.get(&(x >> 4, z >> 4)).map_or(MIN_Y, |chunk| { + chunk.world_surface(x, z, |v| air(BlockStateId(v))) + }) + } pub fn block(&self, x: i32, y: i32, z: i32) -> BlockStateId { self.chunks .get(&(x >> 4, z >> 4)) .map_or(self.air, |chunk| chunk.block(x & 15, y, z & 15)) } + /// The block entities to tick this tick, in order (`BlockEntities::tickers`). + pub fn block_entity_tickers(&mut self) -> Vec<(crate::block_entities::Pos, u32)> { + let (chunks, air) = (&self.chunks, self.air); + self.block_entities.tickers(|(x, y, z)| { + chunks + .get(&(x >> 4, z >> 4)) + .map_or(air, |chunk| chunk.block(x & 15, y, z & 15)) + .0 + }) + } pub fn light(&self, x: i32, y: i32, z: i32) -> u8 { self.chunks .get(&(x >> 4, z >> 4)) diff --git a/crates/horizon-client/tests/generation_saves.rs b/crates/horizon-client/tests/generation_saves.rs index 26da11efe..ce91a1096 100644 --- a/crates/horizon-client/tests/generation_saves.rs +++ b/crates/horizon-client/tests/generation_saves.rs @@ -170,13 +170,15 @@ fn chunks_left_behind_keep_their_decoration() { return; }; let assets = Arc::new(Assets::load(&root).unwrap()); + let render = Arc::new(assets.reload(&root).unwrap()); let facts = BlockFacts::load(&root, assets.registry).unwrap(); let visibility = models::load_visibility(&root, assets.registry).unwrap(); let context = Arc::new(WorkerContext { - tables: Arc::new(MeshTables::new(&assets, &facts, visibility).unwrap()), + tables: Arc::new(MeshTables::new(&assets, &render, &facts, visibility).unwrap()), light: Arc::new(LightTables::load(&root, assets.registry, &facts).unwrap()), biomes: Arc::new(Biomes::load(&root).unwrap()), assets, + render: render.clone(), }); let generator = Arc::new(Recorder::default()); let mut session = Session::new(generator.clone(), context.clone()); diff --git a/crates/horizon-client/tests/lan_bot.rs b/crates/horizon-client/tests/lan_bot.rs index 36f53444c..18af20343 100644 --- a/crates/horizon-client/tests/lan_bot.rs +++ b/crates/horizon-client/tests/lan_bot.rs @@ -38,7 +38,7 @@ fn resources() -> Option<std::path::PathBuf> { root.join("items.json").exists().then_some(root) } -const STONE: u16 = 1; +const STONE: u32 = 1; fn flat_world() -> World { let mut world = World::new(BlockStateId(0), 0); @@ -163,7 +163,7 @@ struct Server { hub: Hub, world: World, next_entity: u32, - placed: Vec<(u32, [i32; 3], u16, i32)>, + placed: Vec<(u32, [i32; 3], u32, i32)>, } impl Server { fn tick(&mut self, entities: &[EntityState]) { @@ -181,6 +181,8 @@ impl Server { sprinting: false, equipment: Default::default(), health: 20.0, + attributes: Vec::new(), + living_flags: 0, }; let mut ctx = TickCtx { world: &mut self.world, @@ -193,6 +195,7 @@ impl Server { clock_rate: 1.0, tick_rate: 20.0, tick_frozen: false, + collision: None, }; let events = self.hub.tick(&mut ctx); for event in events { @@ -202,7 +205,7 @@ impl Server { if let S::UseItemOn(p) = *packet { let at = [p.pos[0], p.pos[1] + 1, p.pos[2]]; ctx.world - .set(at[0], at[1], at[2], BlockStateId(u32::from(STONE))) + .set(at[0], at[1], at[2], BlockStateId(STONE)) .unwrap(); self.placed.push((conn, at, STONE, p.sequence)); self.hub.ack(conn, p.sequence); @@ -341,14 +344,12 @@ fn status_and_join() { C::PlayerInfoUpdate(u) => { info |= u.entries.iter().any(|e| e.name.as_deref() == Some("Host")); } - C::Unknown(id, _) if id == horizon_protocol::ids::play::clientbound::COMMANDS => { - commands = true - } + C::Commands(_) => commands = true, C::LevelChunkWithLight(l) => { chunks += 1; let sections = l.chunk.sections(24, 16, 7).map_err(|e| e.to_string())?; // The floor is in the lowest section. - assert_eq!(sections[0].states.get(4 * 256), i32::from(STONE)); + assert_eq!(sections[0].states.get(4 * 256), STONE as i32); assert_eq!(sections[0].non_empty, 256); assert_eq!(sections[1].non_empty, 0); assert!(!l.light.sky.is_empty()); @@ -394,7 +395,7 @@ fn status_and_join() { assert!(chunks >= 9, "only {chunks} chunks"); assert_eq!( block_update, - Some(([9, MIN_Y + 5, 9], i32::from(STONE))), + Some(([9, MIN_Y + 5, 9], STONE as i32)), "placed block was not broadcast" ); Ok(bot.name) @@ -423,16 +424,40 @@ fn guests_see_each_other_and_the_world_entities() { data: 0, meta: Vec::new(), equipment: Vec::new(), + attributes: Vec::new(), + precise: false, }; let first = thread::spawn(move || { let mut bot = connect(port, "Alex", false)?; + bot.c + .reader + .set_read_timeout(Some(Duration::from_millis(200))) + .unwrap(); let mut zombies = 0; let mut players = 0; let mut moves = 0; + let mut at: Option<[f64; 3]> = None; + let mut reminder = Instant::now(); let deadline = Instant::now() + Duration::from_secs(20); while Instant::now() < deadline && !(zombies > 0 && players > 0 && moves > 0) { - match bot.recv_play()? { + // A vanilla client sends its position at least every 20 ticks even + // when standing still (LocalPlayer's positionReminder), and the + // server checks which entities it sees again on each one. + if let Some([x, y, z]) = at + && reminder.elapsed() >= Duration::from_secs(1) + { + bot.send(S::MovePlayerPos(play::MovePlayerPos { x, y, z, flags: 1 })); + reminder = Instant::now(); + } + let Ok((id, payload)) = bot.c.recv() else { + continue; + }; + let Ok(packet) = C::decode(id, &payload) else { + continue; + }; + match packet { C::PlayerPosition(p) => { + at = Some(p.position); bot.send(S::AcceptTeleportation(play::AcceptTeleportation { id: p.teleport, x: p.position[0], diff --git a/crates/horizon-client/tests/streaming_meshes.rs b/crates/horizon-client/tests/streaming_meshes.rs index 74edd5049..3aa30a330 100644 --- a/crates/horizon-client/tests/streaming_meshes.rs +++ b/crates/horizon-client/tests/streaming_meshes.rs @@ -55,13 +55,15 @@ fn columns_loaded_past_the_render_distance_mesh_when_they_come_in_range() { return; }; let assets = Arc::new(Assets::load(&root).unwrap()); + let render = Arc::new(assets.reload(&root).unwrap()); let facts = BlockFacts::load(&root, assets.registry).unwrap(); let visibility = models::load_visibility(&root, assets.registry).unwrap(); let context = Arc::new(WorkerContext { - tables: Arc::new(MeshTables::new(&assets, &facts, visibility).unwrap()), + tables: Arc::new(MeshTables::new(&assets, &render, &facts, visibility).unwrap()), light: Arc::new(LightTables::load(&root, assets.registry, &facts).unwrap()), biomes: Arc::new(Biomes::load(&root).unwrap()), assets, + render: render.clone(), }); let mut streaming = Streaming::new(Arc::new(Saved), context, 3); streaming.radius = 4; @@ -107,7 +109,7 @@ fn columns_loaded_past_the_render_distance_mesh_when_they_come_in_range() { /// A one-block cave under the sky: a stone floor at y 62 with a glowstone, /// and a roof at y 64 with holes, so that every light change in the cave is /// sampled by a face. -fn roofed(x: i32, z: i32, stone: u16, glowstone: u16) -> Chunk { +fn roofed(x: i32, z: i32, stone: u32, glowstone: u32) -> Chunk { let mut chunk = Chunk::empty(x, z); for lz in 0..16 { for lx in 0..16 { @@ -135,22 +137,25 @@ fn relit_columns_keep_every_mesh_current() { return; }; let assets = Arc::new(Assets::load(&root).unwrap()); + let render = Arc::new(assets.reload(&root).unwrap()); let facts = BlockFacts::load(&root, assets.registry).unwrap(); let visibility = models::load_visibility(&root, assets.registry).unwrap(); let context = Arc::new(WorkerContext { - tables: Arc::new(MeshTables::new(&assets, &facts, visibility).unwrap()), + tables: Arc::new(MeshTables::new(&assets, &render, &facts, visibility).unwrap()), light: Arc::new(LightTables::load(&root, assets.registry, &facts).unwrap()), biomes: Arc::new(Biomes::load(&root).unwrap()), assets: assets.clone(), + render: render.clone(), }); - let state = |name: &str| assets.registry.default_state(name).unwrap().0 as u16; + let state = |name: &str| assets.registry.default_state(name).unwrap().0; let (stone, glowstone, torch) = ( state("minecraft:stone"), state("minecraft:glowstone"), state("minecraft:torch"), ); - // Columns from a server, one of them never sent: its neighbours stay - // unlit, next to lit columns within the render distance. + // Columns from a server, one of them not sent yet. They keep the light + // the server sent (#824, as vanilla's `enableChunkLight`), so its + // neighbours are lit too. let mut streaming = Streaming::new(Arc::new(Saved), context.clone(), 3); streaming.radius = 4; streaming.remote = true; @@ -176,7 +181,7 @@ fn relit_columns_keep_every_mesh_current() { } }; settle(&mut streaming, &mut world); - assert!(!streaming.is_lit(1, 0) && streaming.is_lit(0, 0)); + assert!(streaming.is_lit(1, 0) && streaming.is_lit(0, 0)); // Open the roof and put torches in the cave on a ring two blocks inside // the unlit (1, 0): its lit neighbours are relit, and their meshes change // only through light (no edit is next to them). @@ -185,9 +190,7 @@ fn relit_columns_keep_every_mesh_current() { let (bx, bz) = (16 + lx, lz); world.set(bx, 64, bz, BlockStateId(0)).unwrap(); if i % 3 == 0 { - world - .set(bx, 63, bz, BlockStateId(u32::from(torch))) - .unwrap(); + world.set(bx, 63, bz, BlockStateId(torch)).unwrap(); } } } @@ -212,6 +215,7 @@ fn relit_columns_keep_every_mesh_current() { cutout_leaves: true, cull_leaves: false, assets: &context.assets, + render: &context.render, tables: &context.tables, biomes: &context.biomes, visibility: context.tables.visibility.clone(), diff --git a/crates/horizon-client/tests/wide_states.rs b/crates/horizon-client/tests/wide_states.rs new file mode 100644 index 000000000..690732ceb --- /dev/null +++ b/crates/horizon-client/tests/wide_states.rs @@ -0,0 +1,40 @@ +//! Block states past vanilla's 16-bit global palette (docs/modpacks.md): +//! content mods can take the registry past 65,536 states. Its own test binary, +//! since the palette width is set once for the whole process. + +use horizon_client::chunk::{Chunk, Section, block_strategy, set_block_states}; + +#[test] +fn sections_hold_states_past_sixteen_bits() { + // Vanilla's width until the registry needs more (one test: the width is + // global to the process). + assert_eq!(block_strategy().global_bits, 16); + set_block_states(35_723); + assert_eq!(block_strategy().global_bits, 16); + set_block_states(100_000); + assert_eq!(block_strategy().global_bits, 17); + + // A few states past 65,536 stay in the section's own palette. + let mut chunk = Chunk::empty(0, 0); + chunk.set(1, 70, 2, 70_000); + chunk.set(3, 70, 4, 99_999); + assert_eq!(chunk.get(1, 70, 2), 70_000); + assert_eq!(chunk.get(3, 70, 4), 99_999); + assert_eq!(chunk.get(0, 70, 0), 0); + + // More than 256 distinct states take the global palette at 17 bits. + let mut values = [0u32; 4096]; + for (i, v) in values.iter_mut().enumerate() { + *v = 65_000 + (i as u32 % 1000) * 30; + } + let mut section = Section { + blocks: Section::from_blocks(&values), + ..Section::default() + }; + let mut out = [0u32; 4096]; + section.unpack(&mut out); + assert_eq!(out, values); + section.set(17, 99_998); + assert_eq!(section.get(17), 99_998); + assert_eq!(section.get(18), values[18]); +} diff --git a/crates/horizon-core/src/biome.rs b/crates/horizon-core/src/biome.rs index 4f8c9ff54..96baeda4b 100644 --- a/crates/horizon-core/src/biome.rs +++ b/crates/horizon-core/src/biome.rs @@ -22,20 +22,86 @@ impl BiomeZoom { } /// Choose the closest of eight jittered quart corners, preserving vanilla's /// corner order, strict tie comparison and signed coordinate wrapping. + /// + /// Each corner re-derives its distance from a fresh six-step mix driven by + /// the quart coordinate. The first two steps depend only on the X and Y + /// offsets, so those shared prefixes are mixed once (two X states, then four + /// XY states) instead of eight times, and the six-step tail is evaluated for + /// all eight corners. Since `next(m, salt + offset) == next(m, salt) + + /// offset`, each coordinate is folded into its step with one addition. pub fn quart_at_block(self, block: BlockPos) -> [i32; 3] { let shifted = [block.x, block.y, block.z].map(|v| v.wrapping_sub(2)); let base = shifted.map(|v| v >> 2); let fractions = shifted.map(|v| f64::from(v & 3) / 4.); + let coordinate = base.map(i64::from); + let seed = self.seed; + const A: i64 = 6364136223846793005; + const B: i64 = 1442695040888963407; + let g = |m: i64| m.wrapping_mul(m.wrapping_mul(A).wrapping_add(B)); + let root = g(seed); + let m0: [i64; 2] = + std::array::from_fn(|x: usize| root.wrapping_add(coordinate[0].wrapping_add(x as i64))); + let m1: [[i64; 2]; 2] = std::array::from_fn(|x: usize| { + std::array::from_fn(|y: usize| { + g(m0[x]).wrapping_add(coordinate[1].wrapping_add(y as i64)) + }) + }); + let m2: [[[i64; 2]; 2]; 2] = std::array::from_fn(|x: usize| { + std::array::from_fn(|y: usize| { + std::array::from_fn(|z: usize| { + g(m1[x][y]).wrapping_add(coordinate[2].wrapping_add(z as i64)) + }) + }) + }); + let m3: [[[i64; 2]; 2]; 2] = std::array::from_fn(|x: usize| { + std::array::from_fn(|y: usize| { + std::array::from_fn(|z: usize| { + g(m2[x][y][z]).wrapping_add(coordinate[0].wrapping_add(x as i64)) + }) + }) + }); + let m4: [[[i64; 2]; 2]; 2] = std::array::from_fn(|x: usize| { + std::array::from_fn(|y: usize| { + std::array::from_fn(|z: usize| { + g(m3[x][y][z]).wrapping_add(coordinate[1].wrapping_add(y as i64)) + }) + }) + }); + let m5: [[[i64; 2]; 2]; 2] = std::array::from_fn(|x: usize| { + std::array::from_fn(|y: usize| { + std::array::from_fn(|z: usize| { + g(m4[x][y][z]).wrapping_add(coordinate[2].wrapping_add(z as i64)) + }) + }) + }); + let m6: [[[i64; 2]; 2]; 2] = std::array::from_fn(|x: usize| { + std::array::from_fn(|y: usize| { + std::array::from_fn(|z: usize| g(m5[x][y][z]).wrapping_add(seed)) + }) + }); + let m7: [[[i64; 2]; 2]; 2] = std::array::from_fn(|x: usize| { + std::array::from_fn(|y: usize| { + std::array::from_fn(|z: usize| g(m6[x][y][z]).wrapping_add(seed)) + }) + }); let mut selected = base; let mut best = f64::INFINITY; - for corner in 0..8 { - let offsets = [(corner >> 2) & 1, (corner >> 1) & 1, corner & 1]; - let quart = std::array::from_fn(|i| base[i].wrapping_add(offsets[i])); - let delta = std::array::from_fn(|i| fractions[i] - f64::from(offsets[i])); - let distance = fiddled_distance(self.seed, quart, delta); - if best > distance { - best = distance; - selected = quart; + for x in 0..2 { + for y in 0..2 { + for z in 0..2 { + let dx = fractions[0] - x as f64 + fiddle(m5[x][y][z]); + let dy = fractions[1] - y as f64 + fiddle(m6[x][y][z]); + let dz = fractions[2] - z as f64 + fiddle(m7[x][y][z]); + let distance = dz * dz + dy * dy + dx * dx; + if best > distance { + best = distance; + selected = [ + base[0].wrapping_add(x as i32), + base[1].wrapping_add(y as i32), + base[2].wrapping_add(z as i32), + ]; + } + } } } selected @@ -50,25 +116,112 @@ pub fn noise_quart_at_block(block: BlockPos) -> [i32; 3] { pub fn noise_quart_at_position(position: [f64; 3]) -> [i32; 3] { position.map(|v| (v.floor() as i32) >> 2) } -fn next(seed: i64, salt: i64) -> i64 { - seed.wrapping_mul( - seed.wrapping_mul(6364136223846793005) - .wrapping_add(1442695040888963407), - ) - .wrapping_add(salt) -} fn fiddle(seed: i64) -> f64 { (((seed >> 24) & 1023) as f64 / 1024. - 0.5) * 0.9 } -fn fiddled_distance(seed: i64, quart: [i32; 3], delta: [f64; 3]) -> f64 { - let mut mixed = seed; - for coordinate in quart.into_iter().cycle().take(6) { - mixed = next(mixed, i64::from(coordinate)); + +#[cfg(test)] +mod tests { + use super::*; + + /// The straightforward per-corner formulation, kept as the parity oracle + /// for the prefix-shared implementation. + fn reference(seed: i64, block: BlockPos) -> [i32; 3] { + let shifted = [block.x, block.y, block.z].map(|v| v.wrapping_sub(2)); + let base = shifted.map(|v| v >> 2); + let fractions = shifted.map(|v| f64::from(v & 3) / 4.); + let mut selected = base; + let mut best = f64::INFINITY; + for corner in 0..8 { + let offsets = [(corner >> 2) & 1, (corner >> 1) & 1, corner & 1]; + let quart: [i32; 3] = std::array::from_fn(|i| base[i].wrapping_add(offsets[i])); + let delta: [f64; 3] = std::array::from_fn(|i| fractions[i] - f64::from(offsets[i])); + let mut mixed = seed; + for coordinate in quart.into_iter().cycle().take(6) { + mixed = mixed + .wrapping_mul( + mixed + .wrapping_mul(6364136223846793005) + .wrapping_add(1442695040888963407), + ) + .wrapping_add(i64::from(coordinate)); + } + let x = delta[0] + fiddle(mixed); + mixed = mixed + .wrapping_mul( + mixed + .wrapping_mul(6364136223846793005) + .wrapping_add(1442695040888963407), + ) + .wrapping_add(seed); + let y = delta[1] + fiddle(mixed); + mixed = mixed + .wrapping_mul( + mixed + .wrapping_mul(6364136223846793005) + .wrapping_add(1442695040888963407), + ) + .wrapping_add(seed); + let z = delta[2] + fiddle(mixed); + let distance = z * z + y * y + x * x; + if best > distance { + best = distance; + selected = quart; + } + } + selected + } + + #[test] + fn quart_at_block_matches_reference() { + let seeds = [ + 0i64, + 1, + -1, + i64::MIN, + i64::MAX, + 0x0123_4567_89ab_cdef, + -0x0123_4567_89ab_cdef, + ]; + let extremes = [ + i32::MIN, + i32::MIN + 1, + -1, + 0, + 1, + 2, + 3, + 4, + -2, + i32::MAX - 1, + i32::MAX, + ]; + for &seed in &seeds { + let zoom = BiomeZoom::new(seed); + for x in -34..34 { + for y in -34..34 { + for z in -34..34 { + let block = BlockPos::new(x, y, z); + assert_eq!( + zoom.quart_at_block(block), + reference(seed, block), + "seed {seed} at {x} {y} {z}" + ); + } + } + } + for &x in &extremes { + for &y in &extremes { + for &z in &extremes { + let block = BlockPos::new(x, y, z); + assert_eq!( + zoom.quart_at_block(block), + reference(seed, block), + "seed {seed} at {x} {y} {z}" + ); + } + } + } + } } - let x = delta[0] + fiddle(mixed); - mixed = next(mixed, seed); - let y = delta[1] + fiddle(mixed); - mixed = next(mixed, seed); - let z = delta[2] + fiddle(mixed); - z * z + y * y + x * x } diff --git a/crates/horizon-core/src/density/arithmetic.rs b/crates/horizon-core/src/density/arithmetic.rs index f82b96ce2..51892ffc7 100644 --- a/crates/horizon-core/src/density/arithmetic.rs +++ b/crates/horizon-core/src/density/arithmetic.rs @@ -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}" + ); + } + } + } + } +} diff --git a/crates/horizon-core/src/density/beardifier.rs b/crates/horizon-core/src/density/beardifier.rs index 73b94a066..5dec64189 100644 --- a/crates/horizon-core/src/density/beardifier.rs +++ b/crates/horizon-core/src/density/beardifier.rs @@ -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()); + } +} diff --git a/crates/horizon-core/src/density/cache.rs b/crates/horizon-core/src/density/cache.rs index 8e5b46f44..eddcbfdd0 100644 --- a/crates/horizon-core/src/density/cache.rs +++ b/crates/horizon-core/src/density/cache.rs @@ -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(); diff --git a/crates/horizon-core/src/density/interpolation.rs b/crates/horizon-core/src/density/interpolation.rs index 25a5e826b..4bcb51a3a 100644 --- a/crates/horizon-core/src/density/interpolation.rs +++ b/crates/horizon-core/src/density/interpolation.rs @@ -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 diff --git a/crates/horizon-core/src/density/mod.rs b/crates/horizon-core/src/density/mod.rs index 6bc84a8d1..450568eb7 100644 --- a/crates/horizon-core/src/density/mod.rs +++ b/crates/horizon-core/src/density/mod.rs @@ -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>( @@ -103,6 +107,13 @@ impl SamplerContext { self.scratch.push(buffer); result } + /// Start an independent evaluation, retaining only temporary scratch buffers. + /// Cached scalar/volume results and world bindings must not cross chunks or + /// graphs, whose cache IDs can refer to different functions. + pub fn reset(&mut self) { + self.caches = self.caches.as_ref().map(|_| Vec::new()); + self.fields = WorldFields::default(); + } /// Release retained storage between worlds or after unusually large work. pub fn clear_scratch(&mut self) { self.scratch.clear(); diff --git a/crates/horizon-core/src/density/tests.rs b/crates/horizon-core/src/density/tests.rs index 77a643097..447e46c40 100644 --- a/crates/horizon-core/src/density/tests.rs +++ b/crates/horizon-core/src/density/tests.rs @@ -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()); } @@ -755,3 +777,185 @@ fn spline_lazy_branch_failure_preserves_completed_output_and_returns_scratch() { assert_eq!(context.scratch.len(), 2); assert_eq!(sampler.sample_value(&mut context, [0; 3]), Ok(7.0)); } + +#[test] +fn reset_retains_scratch_but_discards_graph_caches_and_world_bindings() { + let binding = Arc::new(ConstantSampler(17.0)); + let lifetime = Arc::downgrade(&binding); + let mut context = SamplerContext::cached() + .with_world_fields(WorldFields::default().with_field(WorldField::Beardifier, binding)); + let cached = CachingDensitySampler::new( + 0, + Arc::new(ContextBoundSampler::new(WorldField::Beardifier)), + ) + .unwrap(); + let volume = DensityVolume::new([2; 3], [0; 3], [1; 3]).unwrap(); + let mut output = [0.0; 8]; + cached + .sample_volume(&mut context, &mut output, volume) + .unwrap(); + assert_eq!(output, [17.0; 8]); + assert_eq!(cached.sample_value(&mut context, [9; 3]), Ok(17.0)); + context + .with_buffer(128, |_, buffer| { + buffer.fill(99.0); + Ok(()) + }) + .unwrap(); + let allocation = context.scratch[0].as_ptr(); + let capacity = context.scratch[0].capacity(); + context.reset(); + assert!(lifetime.upgrade().is_none()); + assert!(context.caches.as_ref().unwrap().is_empty()); + assert_eq!(context.scratch[0].as_ptr(), allocation); + assert_eq!(context.scratch[0].capacity(), capacity); + // Same coordinates and cache ID must resolve the new context's fallback. + assert_eq!(cached.sample_value(&mut context, [9; 3]), Ok(0.0)); + cached + .sample_volume(&mut context, &mut output, volume) + .unwrap(); + assert_eq!(output, [0.0; 8]); + // A different graph may reuse IDs without inheriting scalar/volume results. + context.reset(); + let replacement = CachingDensitySampler::new(0, Arc::new(ConstantSampler(3.0))).unwrap(); + assert_eq!(replacement.sample_value(&mut context, [9; 3]), Ok(3.0)); + replacement + .sample_volume(&mut context, &mut output, volume) + .unwrap(); + assert_eq!(output, [3.0; 8]); +} + +#[test] +fn reset_after_sampling_error_drops_blender_and_preserves_uncached_mode() { + let blender = Arc::new(EmptyBlender); + let lifetime = Arc::downgrade(&blender); + let mut context = + SamplerContext::default().with_world_fields(WorldFields::default().with_blender(blender)); + let result: Result<(), SampleError> = context.with_buffer(64, |_, buffer| { + buffer.fill(42.0); + Err(SampleError::RecursiveCache) + }); + assert_eq!(result, Err(SampleError::RecursiveCache)); + let allocation = context.scratch[0].as_ptr(); + context.reset(); + assert!(lifetime.upgrade().is_none()); + assert!(context.caches.is_none()); + context + .with_buffer(32, |_, buffer| { + assert_eq!(buffer.as_ptr(), allocation); + Ok(()) + }) + .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"] + } + ); + } +} diff --git a/crates/horizon-core/src/noise/perlin.rs b/crates/horizon-core/src/noise/perlin.rs index 101ded427..725373eff 100644 --- a/crates/horizon-core/src/noise/perlin.rs +++ b/crates/horizon-core/src/noise/perlin.rs @@ -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); diff --git a/crates/horizon-core/src/paletted.rs b/crates/horizon-core/src/paletted.rs index f162404f5..3d56bb987 100644 --- a/crates/horizon-core/src/paletted.rs +++ b/crates/horizon-core/src/paletted.rs @@ -151,33 +151,85 @@ fn write(words: &mut [u64], bits: u8, i: usize, value: u32) { let (word, shift) = locate(bits, i); words[word] = (words[word] & !(mask(bits) << shift)) | (u64::from(value) << shift); } +/// Values below this are looked up in a table indexed by value once a +/// palette outgrows the scan: every block state and biome id of 26.3. +const DENSE_VALUES: u32 = 1 << 16; +thread_local! { + /// `palette_ids`' table: 1 + the id of each value seen, 0 for the others. + /// Only the entries of the palette being built are set, and they are + /// cleared again before it returns. + static DENSE_IDS: std::cell::RefCell<Vec<u32>> = const { std::cell::RefCell::new(Vec::new()) }; +} /// The distinct `values` in first-occurrence order and each value's index in -/// them. Up to 16 distinct values a scan finds them faster than hashing. +/// them. A run of one value (most of a section) is matched once, a scan finds +/// up to 16 distinct values faster than any lookup, and past that a table +/// indexed by value finds them without hashing. fn palette_ids(values: &[u32]) -> (Vec<u32>, Vec<u32>) { let mut palette: Vec<u32> = Vec::new(); - let mut small = true; - let mut lookup: std::collections::HashMap<u32, u32> = std::collections::HashMap::new(); let mut ids = Vec::with_capacity(values.len()); - for &value in values { - let id = if small { - match palette.iter().position(|&v| v == value) { + let mut last: Option<(u32, u32)> = None; + let mut rest = values; + while let Some((&value, tail)) = rest.split_first() { + let id = match last { + Some((v, id)) if v == value => id, + _ if palette.len() > 16 => break, + _ => match palette.iter().position(|&v| v == value) { Some(id) => id as u32, None => { palette.push(value); - if palette.len() > 16 { - small = false; - lookup.extend(palette.iter().enumerate().map(|(i, &v)| (v, i as u32))); - } palette.len() as u32 - 1 } + }, + }; + last = Some((value, id)); + ids.push(id); + rest = tail; + } + if rest.is_empty() { + return (palette, ids); + } + if palette.iter().chain(rest).all(|&v| v < DENSE_VALUES) { + DENSE_IDS.with(|table| { + let mut table = table.borrow_mut(); + for (id, &v) in palette.iter().enumerate() { + let v = v as usize; + if table.len() <= v { + table.resize(v + 1, 0); + } + table[v] = id as u32 + 1; } - } else { - *lookup.entry(value).or_insert_with(|| { + for &value in rest { + let v = value as usize; + if table.len() <= v { + table.resize(v + 1, 0); + } + let id = match table[v] { + 0 => { + palette.push(value); + table[v] = palette.len() as u32; + palette.len() as u32 - 1 + } + slot => slot - 1, + }; + ids.push(id); + } + for &v in &palette { + table[v as usize] = 0; + } + }); + } else { + let mut lookup: std::collections::HashMap<u32, u32> = palette + .iter() + .enumerate() + .map(|(i, &v)| (v, i as u32)) + .collect(); + for &value in rest { + let id = *lookup.entry(value).or_insert_with(|| { palette.push(value); palette.len() as u32 - 1 - }) - }; - ids.push(id); + }); + ids.push(id); + } } (palette, ids) } @@ -391,17 +443,24 @@ impl PalettedContainer { assert_eq!(out.len(), self.strategy.entries()); match &self.palette { Palette::Single(value) => out.fill(*value), - palette => { + Palette::List(list) => { let bits = usize::from(self.bits); let per = 64 / bits; let m = mask(self.bits); for (chunk, &word) in out.chunks_mut(per).zip(&self.words) { for (slot, cell) in chunk.iter_mut().enumerate() { - let id = ((word >> (slot * bits)) & m) as u32; - *cell = match palette { - Palette::List(list) => list[id as usize], - _ => id, - }; + let id = ((word >> (slot * bits)) & m) as usize; + *cell = list[id]; + } + } + } + Palette::Global => { + let bits = usize::from(self.bits); + let per = 64 / bits; + let m = mask(self.bits); + for (chunk, &word) in out.chunks_mut(per).zip(&self.words) { + for (slot, cell) in chunk.iter_mut().enumerate() { + *cell = ((word >> (slot * bits)) & m) as u32; } } } @@ -412,17 +471,24 @@ impl PalettedContainer { debug_assert_eq!(self.strategy.entries(), 4096); match &self.palette { Palette::Single(value) => out.fill(*value as u16), - palette => { + Palette::List(list) => { let bits = usize::from(self.bits); let per = 64 / bits; let m = mask(self.bits); for (chunk, &word) in out.chunks_mut(per).zip(&self.words) { for (slot, cell) in chunk.iter_mut().enumerate() { let id = ((word >> (slot * bits)) & m) as usize; - *cell = match palette { - Palette::List(list) => list[id] as u16, - _ => id as u16, - }; + *cell = list[id] as u16; + } + } + } + Palette::Global => { + let bits = usize::from(self.bits); + let per = 64 / bits; + let m = mask(self.bits); + for (chunk, &word) in out.chunks_mut(per).zip(&self.words) { + for (slot, cell) in chunk.iter_mut().enumerate() { + *cell = ((word >> (slot * bits)) & m) as u16; } } } @@ -738,4 +804,211 @@ mod tests { } } } + + /// The pre-optimization `unpack`, kept to compare against in the benchmark. + fn baseline_unpack(c: &PalettedContainer, out: &mut [u32]) { + match &c.palette { + Palette::Single(value) => out.fill(*value), + palette => { + let bits = usize::from(c.bits); + let per = 64 / bits; + let m = mask(c.bits); + for (chunk, &word) in out.chunks_mut(per).zip(&c.words) { + for (slot, cell) in chunk.iter_mut().enumerate() { + let id = ((word >> (slot * bits)) & m) as u32; + *cell = match palette { + Palette::List(list) => list[id as usize], + _ => id, + }; + } + } + } + } + } + + /// `unpack` against the pre-optimization implementation, for a list palette + /// and the global palette. The result is checked before timing; 25 measured + /// rounds alternate order and report the median and best microsecond-per-call. + #[test] + #[ignore = "microbenchmark; run with --ignored --nocapture"] + fn benchmark_unpack() { + use std::{hint::black_box, time::Instant}; + let values: Vec<u32> = (0..4096).map(|i| (i % 12) as u32).collect(); + let list = PalettedContainer::from_values(BLOCKS, &values); + let global = PalettedContainer::from_values( + BLOCKS, + &(0..4096).map(|i| (i * 3) as u32).collect::<Vec<_>>(), + ); + for (name, container) in [("list", &list), ("global", &global)] { + let mut expected = vec![0u32; 4096]; + baseline_unpack(container, &mut expected); + let mut out = vec![0u32; 4096]; + container.unpack(&mut out); + assert_eq!(expected, out, "unpack {name} differs"); + let mut times = [Vec::new(), Vec::new()]; + for round in 0..25 { + for optimized in [round % 2 == 0, round % 2 != 0] { + let started = Instant::now(); + for _ in 0..2000 { + if optimized { + container.unpack(black_box(&mut out)); + } else { + baseline_unpack(black_box(container), black_box(&mut out)); + } + } + if round > 0 { + times[usize::from(optimized)] + .push(started.elapsed().as_secs_f64() * 1e6 / 2000.0); + } + } + } + for values in &mut times { + values.sort_by(f64::total_cmp); + } + let median = |v: &[f64]| v[v.len() / 2]; + println!( + "unpack {name} median: baseline {:.3} us optimized {:.3} us speedup {:.3}x", + median(×[0]), + median(×[1]), + median(×[0]) / median(×[1]) + ); + println!( + "unpack {name} best: baseline {:.3} us optimized {:.3} us speedup {:.3}x", + times[0][0], + times[1][0], + times[0][0] / times[1][0] + ); + } + } + + /// The pre-optimization `palette_ids` (scan, then a SipHash map), kept to + /// compare against. + fn baseline_palette_ids(values: &[u32]) -> (Vec<u32>, Vec<u32>) { + let mut palette: Vec<u32> = Vec::new(); + let mut small = true; + let mut lookup: std::collections::HashMap<u32, u32> = std::collections::HashMap::new(); + let mut ids = Vec::with_capacity(values.len()); + for &value in values { + let id = if small { + match palette.iter().position(|&v| v == value) { + Some(id) => id as u32, + None => { + palette.push(value); + if palette.len() > 16 { + small = false; + lookup.extend(palette.iter().enumerate().map(|(i, &v)| (v, i as u32))); + } + palette.len() as u32 - 1 + } + } + } else { + *lookup.entry(value).or_insert_with(|| { + palette.push(value); + palette.len() as u32 - 1 + }) + }; + ids.push(id); + } + (palette, ids) + } + /// Sections like the ones saved: stone, ores and caves under dirt, grass + /// and air; a mixed surface (plants, leaves, logs) in short runs; a + /// global palette; ids past the table; and biomes. + fn palette_cases() -> Vec<(&'static str, Vec<u32>)> { + let mut state = 0x9E37_79B9_7F4A_7C15u64; + let mut terrain = Vec::with_capacity(4096); + for i in 0..4096u32 { + let y = i >> 8; + let r = rng(&mut state); + terrain.push(match y { + 0..=8 if r % 40 == 0 => 2000 + (r >> 8) as u32 % 6, + 0..=8 if r % 13 == 0 => 0, + 0..=8 => 1, + 9..=10 => 10, + 11 => 9, + _ => 0, + }); + } + let mut surface = Vec::with_capacity(4096); + while surface.len() < 4096 { + let r = rng(&mut state); + let run = 1 + (r % 8) as usize; + let v = 100 + ((r >> 8) % 24) as u32 * 37; + surface.extend(std::iter::repeat_n(v, run.min(4096 - surface.len()))); + } + let global: Vec<u32> = (0..4096) + .map(|_| (rng(&mut state) % 600) as u32 * 41) + .collect(); + let large: Vec<u32> = (0..4096) + .map(|i| { + if i % 3 == 0 { + 70_000 + (rng(&mut state) % 40) as u32 + } else { + i as u32 % 20 + } + }) + .collect(); + let biomes: Vec<u32> = (0..64).map(|i| [3, 3, 3, 7, 7, 12][i % 6]).collect(); + vec![ + ("terrain", terrain), + ("surface", surface), + ("global", global), + ("large ids", large), + ("biomes", biomes), + ("single", vec![5; 4096]), + ("seventeen", (0..4096).map(|i| (i % 17) as u32).collect()), + ] + } + #[test] + fn palette_ids_match_first_occurrence_order() { + for (name, values) in palette_cases() { + assert_eq!( + palette_ids(&values), + baseline_palette_ids(&values), + "{name}" + ); + // The table is left clean for the next section. + assert_eq!( + palette_ids(&values), + baseline_palette_ids(&values), + "{name} again" + ); + } + DENSE_IDS.with(|t| assert!(t.borrow().iter().all(|&slot| slot == 0))); + } + /// `palette_ids` against the pre-optimization implementation over the + /// cases above. 25 measured rounds alternate order and report the median + /// microseconds per call. + #[test] + #[ignore = "microbenchmark; run with --ignored --nocapture"] + fn benchmark_palette_ids() { + use std::{hint::black_box, time::Instant}; + for (name, values) in palette_cases() { + let mut times = [Vec::new(), Vec::new()]; + for round in 0..25 { + for optimized in [round % 2 == 0, round % 2 != 0] { + let started = Instant::now(); + for _ in 0..500 { + if optimized { + black_box(palette_ids(black_box(&values))); + } else { + black_box(baseline_palette_ids(black_box(&values))); + } + } + if round > 0 { + times[usize::from(optimized)] + .push(started.elapsed().as_secs_f64() * 1e6 / 500.0); + } + } + } + for t in &mut times { + t.sort_by(f64::total_cmp); + } + let (base, new) = (times[0][times[0].len() / 2], times[1][times[1].len() / 2]); + println!( + "palette_ids {name}: baseline {base:.2} us, new {new:.2} us, {:.2}x", + base / new + ); + } + } } diff --git a/crates/horizon-core/src/storage.rs b/crates/horizon-core/src/storage.rs index 61f8c505f..6b8a8ec38 100644 --- a/crates/horizon-core/src/storage.rs +++ b/crates/horizon-core/src/storage.rs @@ -88,6 +88,28 @@ impl PackedStorage { Ok(((self.words[word] >> shift) & self.mask) as u32) } + /// Reads `index` without a bounds check. Callers use this once they hold a + /// slice length or a section index they have already validated. + /// `index` must be less than `len`. + pub fn read(&self, index: usize) -> u32 { + debug_assert!(index < self.len); + let word = index / self.per_word; + let shift = (index % self.per_word) * usize::from(self.bits); + ((self.words[word] >> shift) & self.mask) as u32 + } + + /// Writes `value` at `index` without revalidating either. `index` must be + /// less than `len` and `value` must fit `bits`, which holds for a section + /// palette index from the same container. Returns the previous value. + pub fn write(&mut self, index: usize, value: u32) -> u32 { + debug_assert!(index < self.len && u64::from(value) <= self.mask); + let word = index / self.per_word; + let shift = (index % self.per_word) * usize::from(self.bits); + let old = ((self.words[word] >> shift) & self.mask) as u32; + self.words[word] = (self.words[word] & !(self.mask << shift)) | (u64::from(value) << shift); + old + } + /// Validation fails before mutation. Writes accept nonnegative Java ints; /// imported 32-bit words may still decode to any u32 bit pattern. pub fn set(&mut self, index: usize, value: u32) -> Result<u32, StorageError> { diff --git a/crates/horizon-mods/src/content.rs b/crates/horizon-mods/src/content.rs index 40b641759..9afe95d0a 100644 --- a/crates/horizon-mods/src/content.rs +++ b/crates/horizon-mods/src/content.rs @@ -192,6 +192,13 @@ impl Content { Ok(content) } + /// Where the files are read from: the mod's folder or its `.hmod`. + pub fn source(&self) -> &Path { + match &self.source { + Source::Dir(path) | Source::Zip(path) => path, + } + } + /// No files: a mod with code only. pub fn is_empty(&self) -> bool { self.files.is_empty() diff --git a/crates/horizon-port-pack/Cargo.toml b/crates/horizon-port-pack/Cargo.toml index 3b903ad03..775a853a8 100644 --- a/crates/horizon-port-pack/Cargo.toml +++ b/crates/horizon-port-pack/Cargo.toml @@ -7,6 +7,8 @@ rust-version = "1.86" description = "Port a Java modpack to Horizon: import, inventory, classification and data carry-over (docs/modpacks.md)" [dependencies] +# Content path rules only (no runtime): what the game's content loader accepts. +horizon-mods = { path = "../horizon-mods", default-features = false } serde = { version = "1", features = ["derive"] } serde_json = "1" sha1 = "0.10" @@ -14,8 +16,5 @@ toml = "1.1.6" ureq = { version = "2", default-features = false, features = ["tls"] } zip = { version = "2", default-features = false, features = ["deflate"] } -[dev-dependencies] -horizon-mods = { path = "../horizon-mods", default-features = false } - [lints] workspace = true diff --git a/crates/horizon-port-pack/src/carry.rs b/crates/horizon-port-pack/src/carry.rs index a637d6246..0cf36b88b 100644 --- a/crates/horizon-port-pack/src/carry.rs +++ b/crates/horizon-port-pack/src/carry.rs @@ -19,8 +19,15 @@ pub struct Report { /// Files outside what a content mod may hold (other namespaces, vanilla /// data other than tags), kept in `outside/` for review. pub outside: Vec<String>, + /// Files of the mod's own content the game would refuse, kept in + /// `outside/` too, with why: a path vanilla does not read (upper case), + /// JSON that does not parse, a vanilla tag the mod replaces. + pub unreadable: BTreeMap<String, String>, pub recipes_upgraded: usize, pub loot_tables_upgraded: usize, + /// Blocks whose jar has no loot table but drop themselves in the oracle: + /// the mod registers that table in code, and carry-over writes it. + pub loot_tables_added: usize, /// Worldgen and structure files (`data/<id>/worldgen/`, `structure/`). pub worldgen: usize, /// Recipes left as they were: custom types, NeoForge conditions. @@ -40,6 +47,11 @@ pub struct Report { pub items_unknown: BTreeMap<String, String>, /// Convention (`c:`) tags the recipes use, filled in from the oracle. pub convention_tags: usize, + /// Structure, placement and pool element types rewritten as vanilla's, + /// and those left as they are. + pub worldgen_types: crate::worldgen::Upgrade, + /// The loader's convention biome tags the structures name, carried. + pub convention_biome_tags: usize, } /// Vanilla blocks by their sorted property names. @@ -193,6 +205,29 @@ pub fn upgrade_recipe(recipe: &mut Value) -> bool { true } +/// Block and item ids vanilla renamed after 1.21.1, as its datafixers +/// rename them in worlds (DataVersion 4541: the chain became the iron chain +/// when copper chains came in). A data file naming the old id fails to load +/// in 26.3: a recipe, a loot entry, a processor's block match. +const RENAMED_IDS: [(&str, &str); 1] = [("minecraft:chain", "minecraft:iron_chain")]; + +/// Every string of a data file that is a renamed id ([`RENAMED_IDS`]) set to +/// the new one, in place; whether any was. +pub fn renamed_ids(v: &mut Value) -> bool { + match v { + Value::String(s) => match RENAMED_IDS.iter().find(|(old, _)| s == old) { + Some((_, new)) => { + *s = (*new).to_owned(); + true + } + None => false, + }, + Value::Array(a) => a.iter_mut().fold(false, |c, x| renamed_ids(x) | c), + Value::Object(o) => o.values_mut().fold(false, |c, x| renamed_ids(x) | c), + _ => false, + } +} + /// A 1.21.1 loot table in 26.3's shape, in place: `conditions` lists become /// one `condition` (`all_of` for several), `functions` become `modifier`, /// and conditions and functions name their kind in `type`. A tool match on @@ -291,15 +326,100 @@ fn place(name: &str, id: &str) -> Option<bool> { Some(ns == id || shared_tags) } +/// Why the game would refuse a file of the mod's own content, if it would. +/// Vanilla skips such files one by one (an invalid resource path, a data +/// file that does not parse); Horizon's content loader refuses the whole +/// set, so they stay out of the carry. +fn unreadable(name: &str, bytes: &mut Vec<u8>) -> Option<String> { + if let Err(e) = horizon_mods::content::check_path(name) { + return Some(e); + } + if !(name.ends_with(".json") || name.ends_with(".mcmeta")) { + return None; + } + // A byte-order mark: Horizon's JSON readers take the file without it. + if bytes.starts_with(b"\xEF\xBB\xBF") { + bytes.drain(..3); + } + let value: Value = match serde_json::from_slice(bytes) { + Ok(v) => v, + // Minecraft reads data files with Gson's lenient reader, which skips + // comments: YUNG's pools have them. The carry is plain JSON. + Err(e) => match serde_json::from_slice::<Value>(&without_comments(bytes)) { + Ok(v) => { + *bytes = serde_json::to_vec_pretty(&v).expect("a parsed value serializes"); + v + } + Err(_) => return Some(format!("does not parse as JSON: {e}")), + }, + }; + let shared_tag = name.starts_with("data/minecraft/tags/") || name.starts_with("data/c/tags/"); + if shared_tag && value.get("replace").and_then(Value::as_bool) == Some(true) { + return Some("replaces a shared tag; a content mod may only add to it".into()); + } + None +} + +/// JSON text without the comments Gson's lenient reader skips: `//` and +/// `#` to the end of the line, and `/* ... */`, outside strings. +fn without_comments(bytes: &[u8]) -> Vec<u8> { + let mut out = Vec::with_capacity(bytes.len()); + let mut i = 0; + let mut string = false; + while i < bytes.len() { + let b = bytes[i]; + if string { + out.push(b); + if b == b'\\' { + out.extend(bytes.get(i + 1)); + i += 1; + } else if b == b'"' { + string = false; + } + i += 1; + continue; + } + match (b, bytes.get(i + 1)) { + (b'"', _) => { + string = true; + out.push(b); + i += 1; + } + (b'/', Some(b'/')) | (b'#', _) => { + while i < bytes.len() && bytes[i] != b'\n' { + i += 1; + } + } + (b'/', Some(b'*')) => { + i += 2; + while i < bytes.len() && !bytes[i..].starts_with(b"*/") { + i += 1; + } + i += 2; + out.push(b' '); + } + _ => { + out.push(b); + i += 1; + } + } + } + out +} + /// Carries `jar` over into `out/carry/`. With the oracle's `facts`, blocks /// and items are the ones the mod registers, with the templates and numbers -/// the facts give; `allowed` says which vanilla blocks the game has. +/// the facts give; `allowed` says which vanilla blocks the game has; +/// `loaded` which mods are ported along, for the conditions of the mod's +/// worldgen modifiers. pub fn carry( jar: &Path, info: &JarInfo, out: &Path, facts: Option<&Facts>, allowed: &dyn Fn(&str) -> bool, + loaded: &dyn Fn(&str) -> bool, + conventions: &BTreeMap<String, Value>, ) -> Result<Report, String> { let id = &info.mod_id; if id.is_empty() @@ -321,6 +441,11 @@ pub fn carry( let mut blockstates: BTreeMap<String, Value> = BTreeMap::new(); let mut item_models = BTreeSet::new(); let mut convention = BTreeSet::new(); + let mut block_loot = BTreeSet::new(); + let mut biome_conventions = BTreeSet::new(); + let set_tags = crate::worldgen::structure_set_tags(&mut zip); + let piece_counts = crate::worldgen::piece_counts(&mut zip); + let processor_data = crate::processors::ProcessorData::read(&mut zip); for i in 0..zip.len() { let mut entry = zip.by_index(i).map_err(|e| e.to_string())?; let name = entry.name().to_owned(); @@ -334,6 +459,15 @@ pub fn carry( entry .read_to_end(&mut bytes) .map_err(|e| format!("{name}: {e}"))?; + let unreadable = if keep { + unreadable(&name, &mut bytes) + } else { + None + }; + let keep = keep && unreadable.is_none(); + if let Some(why) = unreadable { + report.unreadable.insert(name.clone(), why); + } let dest = if keep { report.copied += 1; inside.join(&name) @@ -354,17 +488,103 @@ pub fn carry( { if let Ok(mut recipe) = serde_json::from_slice::<Value>(&bytes) { tags_used(&recipe, &mut convention); - if upgrade_recipe(&mut recipe) { + let renamed = renamed_ids(&mut recipe); + let upgraded = upgrade_recipe(&mut recipe); + if upgraded { report.recipes_upgraded += 1; - bytes = serde_json::to_vec_pretty(&recipe).unwrap_or(bytes); } else { report.recipes_kept.push(file.to_owned()); } + if upgraded || renamed { + bytes = serde_json::to_vec_pretty(&recipe).unwrap_or(bytes); + } + } + } else if let Some(kind) = rel + .strip_prefix("worldgen/") + .and_then(|r| r.split_once('/')) + .map(|(kind, _)| kind) + .filter(|_| name.starts_with("data/") && name.ends_with(".json")) + { + if let Ok(mut v) = serde_json::from_slice::<Value>(&bytes) { + if kind == "structure" { + crate::worldgen::convention_tags(&v["biomes"], &mut biome_conventions); + } + let types = &mut report.worldgen_types; + let carried = crate::processors::carry( + kind, + &mut v, + &processor_data, + &mut types.processors_carried, + ) | crate::processors::rule_tests( + kind, + &mut v, + &mut types.rule_tests_carried, + ); + let mut changed = + crate::worldgen::upgrade(kind, &mut v, types, &set_tags) || carried; + if kind == "structure" { + let path = rel.trim_start_matches("worldgen/structure/"); + let structure = format!("{id}:{}", path.trim_end_matches(".json")); + changed |= crate::worldgen::disables(&structure, &mut v); + changed |= + crate::worldgen::add_piece_counts(&structure, &mut v, &piece_counts); + } + if changed { + bytes = serde_json::to_vec_pretty(&v).unwrap_or(bytes); + } + } + } else if let Some(path) = rel + .strip_prefix("lithostitched/worldgen_modifier/") + .filter(|_| name.starts_with("data/") && name.ends_with(".json")) + { + // Additions to other sets and pools, as files the game merges. + let modifier = serde_json::from_slice::<Value>(&bytes).ok(); + let types = &mut report.worldgen_types; + if let Some((kind, mut added)) = + modifier.and_then(|v| crate::worldgen::modifier(&v, loaded, types)) + { + crate::processors::carry( + kind, + &mut added, + &processor_data, + &mut types.processors_carried, + ); + crate::processors::rule_tests(kind, &mut added, &mut types.rule_tests_carried); + crate::worldgen::upgrade(kind, &mut added, types, &set_tags); + let to = format!( + "data/{id}/worldgen/{kind}/{}/{path}", + crate::worldgen::ADDED + ); + write( + &inside.join(to), + &serde_json::to_vec_pretty(&added).unwrap_or_default(), + )?; + } + } else if rel.starts_with("trial_spawner/") + && name.starts_with("data/") + && name.ends_with(".json") + { + // Trial spawner configs (Moog's arenas): their mobs' NBT. + if let Ok(mut config) = serde_json::from_slice::<Value>(&bytes) + && crate::processors::upgrade_trial_spawner(&mut config) + { + bytes = serde_json::to_vec_pretty(&config).unwrap_or(bytes); + } + } else if rel.starts_with("tags/worldgen/biome/") && name.starts_with("data/") { + if let Ok(v) = serde_json::from_slice::<Value>(&bytes) { + crate::worldgen::convention_tags(&v, &mut biome_conventions); } } else if rel.starts_with("loot_table/") && name.starts_with("data/") { + if let Some(b) = rel + .strip_prefix("loot_table/blocks/") + .and_then(|b| b.strip_suffix(".json")) + { + block_loot.insert(b.to_owned()); + } if let Ok(mut table) = serde_json::from_slice::<Value>(&bytes) { let before = table.clone(); upgrade_loot(&mut table); + renamed_ids(&mut table); if table != before { report.loot_tables_upgraded += 1; bytes = serde_json::to_vec_pretty(&table).unwrap_or(bytes); @@ -386,6 +606,35 @@ pub fn carry( } write(&dest, &bytes)?; } + // Entries the mod adds to other structure sets in Java code, for the + // structures it has. + for (file, set, entries) in crate::worldgen::code_set_entries(id) { + let entries: Vec<Value> = entries + .into_iter() + .filter(|(s, _)| { + s.split_once(':').is_some_and(|(ns, path)| { + inside + .join(format!("data/{ns}/worldgen/structure/{path}.json")) + .exists() + }) + }) + .map(|(structure, weight)| json!({"structure": structure, "weight": weight})) + .collect(); + if entries.is_empty() { + continue; + } + let added = &mut report.worldgen_types.added_to; + *added.entry(set.to_owned()).or_default() += entries.len(); + let to = format!( + "data/{id}/worldgen/structure_set/{}/{file}.json", + crate::worldgen::ADDED + ); + let v = json!({"horizon:add_to": set, "structures": entries}); + write( + &inside.join(to), + &serde_json::to_vec_pretty(&v).unwrap_or_default(), + )?; + } // 1.21.4 moved item models behind `items/<name>.json` definitions. for m in &item_models { let def = inside.join(format!("assets/{id}/items/{m}.json")); @@ -396,6 +645,18 @@ pub fn carry( report.item_definitions_added += 1; } } + // The loader's convention biome tags the structures and their tags name + // (NeoForge's `c:is_plains`...): the game has no loader, so they come along. + for tag in crate::worldgen::with_included(&biome_conventions, conventions) { + let file = inside.join(format!("data/c/tags/worldgen/biome/{tag}.json")); + if !file.exists() { + write( + &file, + &serde_json::to_vec_pretty(&conventions[&tag]).unwrap_or_default(), + )?; + report.convention_biome_tags += 1; + } + } let decl = inside.join(format!("data/{id}/horizon")); let write_decl = |kind: &str, name: &str, v: &Value| { let text = serde_json::to_vec_pretty(v).unwrap_or_default(); @@ -431,6 +692,34 @@ pub fn carry( .push(format!("its item is {}", block.item)); } else { declared.insert(full.clone()); + let drop = (!block_loot.contains(b)) + .then(|| block.drops_itself(full)) + .flatten(); + if let Some(drop) = drop { + let mut entry = json!({"type": "minecraft:item", "name": full}); + if let crate::oracle::SelfDrop::When(property, value) = drop { + entry["condition"] = json!({ + "type": "minecraft:match_block", + "blocks": full, + "state": {property: value} + }); + } + let table = json!({ + "type": "minecraft:block", + "pools": [{ + "condition": {"type": "minecraft:survives_explosion"}, + "entries": [entry], + "rolls": 1 + }], + "random_sequence": format!("{id}:blocks/{b}") + }); + let file = inside.join(format!("data/{id}/loot_table/blocks/{b}.json")); + write( + &file, + &serde_json::to_vec_pretty(&table).unwrap_or_default(), + )?; + report.loot_tables_added += 1; + } } write_decl("block", b, &v)?; report.blocks.insert(b.to_owned(), choice.template); @@ -566,6 +855,36 @@ mod tests { use super::*; use std::io::Write; + /// YUNG's pools carry comments, which Minecraft's lenient reader skips; + /// the carry holds them as plain JSON. A comment marker inside a string + /// stays, and text that is not JSON without its comments is refused. + #[test] + fn reads_json_with_comments_as_minecraft_does() { + let name = "data/m/worldgen/template_pool/bridge.json"; + let mut bytes = br#"{ + // Pillar w/ chance of unlit beacon + "elements": [ /* one */ {"weight": 20, "location": "m:a//b#c"} ], # done + "fallback": "minecraft:empty" +}"# + .to_vec(); + assert_eq!(unreadable(name, &mut bytes), None); + let v: Value = serde_json::from_slice(&bytes).unwrap(); + assert_eq!( + v, + json!({"elements": [{"weight": 20, "location": "m:a//b#c"}], + "fallback": "minecraft:empty"}) + ); + let mut plain = br#"{"a": "b\"//"}"#.to_vec(); + assert_eq!(unreadable(name, &mut plain), None); + assert_eq!(plain, br#"{"a": "b\"//"}"#.to_vec()); + let mut broken = b"{\"a\": 1} trailing".to_vec(); + assert!( + unreadable(name, &mut broken) + .unwrap() + .starts_with("does not parse") + ); + } + #[test] fn upgrades_ingredients() { let mut r = json!({"type": "minecraft:crafting_shaped", "pattern": ["##"], @@ -618,6 +937,27 @@ mod tests { assert_eq!(new, glass); } + #[test] + fn renames_ids_vanilla_renamed() { + let mut recipe = json!({"type": "minecraft:crafting_shaped", "pattern": ["c"], + "key": {"c": {"item": "minecraft:chain"}}, "result": {"id": "m:sign", "count": 1}}); + assert!(renamed_ids(&mut recipe)); + assert_eq!(recipe["key"]["c"]["item"], "minecraft:iron_chain"); + let mut rule = json!({"input_predicate": {"predicate_type": "minecraft:block_match", + "block": "minecraft:chain"}, "output_state": {"id": "minecraft:chain", + "properties": {"axis": "y"}}}); + assert!(renamed_ids(&mut rule)); + assert_eq!(rule["input_predicate"]["block"], "minecraft:iron_chain"); + assert_eq!(rule["output_state"]["id"], "minecraft:iron_chain"); + // Only whole ids: other blocks and mods' own chains stay. + let mut other = json!([ + "minecraft:chain_command_block", + "m:chain", + "minecraft:iron_chain" + ]); + assert!(!renamed_ids(&mut other)); + } + #[test] fn templates_from_blockstate_properties() { let door = @@ -716,6 +1056,25 @@ mod tests { r#"{"values": ["gem:gem_block"]}"#, ), ("data/c/tags/item/gems.json", r#"{"values": ["gem:gem"]}"#), + ( + "data/gem/lithostitched/worldgen_modifier/houses.json", + r#"{"type": "lithostitched:add_template_pool_elements", + "template_pools": "minecraft:village/plains/houses", + "elements": [{"weight": 2, "element": {"element_type": "minecraft:single_pool_element", + "location": "gem:hut", "projection": "rigid", + "processors": {"processors": [{"processor_type": "lithostitched:block_swap"}]}}}]}"#, + ), + // What the game would refuse: vanilla skips these files alone. + ("assets/gem/lang/ry_UA.json", r#"{"item.gem.gem": "Gem"}"#), + ("data/gem/tags/item/empty.json", ""), + ( + "data/minecraft/tags/worldgen/biome/has_structure/village_plains.json", + r#"{"replace": true, "values": []}"#, + ), + ( + "assets/gem/lang/de_de.json", + "\u{feff}{\"item.gem.gem\": \"Juwel\"}", + ), ]; for (name, text) in files { zip.start_file(*name, zip::write::SimpleFileOptions::default()) @@ -731,9 +1090,33 @@ mod tests { ); assert_eq!(info.census.classes, 1); let out = dir.join("work/mods/gem"); - let report = carry(&jar, &info, &out, None, &|_| true).unwrap(); - assert_eq!(report.copied, 12); - assert!(report.outside.is_empty()); + let report = carry( + &jar, + &info, + &out, + None, + &|_| true, + &|_| true, + &BTreeMap::new(), + ) + .unwrap(); + assert_eq!(report.copied, 14); + let unreadable: Vec<&str> = report.unreadable.keys().map(String::as_str).collect(); + assert_eq!( + unreadable, + [ + "assets/gem/lang/ry_UA.json", + "data/gem/tags/item/empty.json", + "data/minecraft/tags/worldgen/biome/has_structure/village_plains.json", + ] + ); + assert_eq!(report.outside.len(), 3); + assert!(!out.join("carry/data/gem/tags/item/empty.json").exists()); + // A byte-order mark is dropped, the file kept. + assert_eq!( + fs::read(out.join("carry/assets/gem/lang/de_de.json")).unwrap()[0], + b'{' + ); assert_eq!((report.recipes_upgraded, report.recipes_kept.len()), (1, 1)); assert_eq!(report.item_definitions_added, 2); assert_eq!(report.blocks["gem_door"], "minecraft:oak_door"); @@ -750,6 +1133,25 @@ mod tests { ) .unwrap(); assert_eq!(recipe["key"]["#"], "gem:gem"); + // A lithostitched pool addition, as a file the game merges, upgraded + // as the mod's own pools are. + let added: Value = serde_json::from_slice( + &fs::read(carried.join("data/gem/worldgen/template_pool/horizon_added/houses.json")) + .unwrap(), + ) + .unwrap(); + assert_eq!( + added["horizon:add_to"], + json!(["minecraft:village/plains/houses"]) + ); + assert_eq!( + added["elements"][0]["element"]["processors"], + json!({"processors": []}) + ); + assert_eq!( + report.worldgen_types.added_to["minecraft:village/plains/houses"], + 1 + ); // Horizon's own reader accepts it: blocks and items declared, files fenced. let package = horizon_mods::Package::read(&carried).unwrap(); diff --git a/crates/horizon-port-pack/src/fetch.rs b/crates/horizon-port-pack/src/fetch.rs new file mode 100644 index 000000000..f8f7b2dc7 --- /dev/null +++ b/crates/horizon-port-pack/src/fetch.rs @@ -0,0 +1,411 @@ +//! Step 1b, the jars: for the mods the player asked to port, their own +//! download of each jar into `<work>/jars/`. Only from where the pack itself +//! points (a Modrinth pack's download URLs) or the same file on Modrinth's +//! public CDN (found by its SHA-1), and only kept when the hash matches; or +//! copied from a folder the player already has (an installed instance, a +//! downloads folder). CurseForge's own downloads need an API key, so a file +//! only CurseForge has is listed as needing one. `<work>/fetch.json` says +//! what happened to each file. +use crate::{ + import::{Pack, PackFile, sha1_hex}, + inventory::Record, + modrinth::{self, Cache}, + pipeline::{Log, read_json, write_json}, +}; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use std::{ + collections::HashMap, + fs, + io::Read, + path::{Path, PathBuf}, +}; + +/// Hosts a Modrinth pack may download from (the `.mrpack` format's list). +const HOSTS: [&str; 4] = [ + "https://cdn.modrinth.com/", + "https://github.com/", + "https://raw.githubusercontent.com/", + "https://gitlab.com/", +]; +/// No mod jar is this large; a bigger answer is not one. +const MAX_JAR: u64 = 512 << 20; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum How { + /// Already in `jars/` (or the imported instance). + Present, + /// Downloaded from the pack's URL or Modrinth's CDN, hash checked. + Downloaded, + /// Copied from the player's folder. + Local, + /// Only on CurseForge, whose downloads need an API key. + NeedsKey, + NotFound, + Failed, +} + +impl How { + pub fn label(self) -> &'static str { + match self { + How::Present => "already here", + How::Downloaded => "downloaded", + How::Local => "copied from your folder", + How::NeedsKey => "needs a CurseForge key", + How::NotFound => "not found", + How::Failed => "failed", + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Outcome { + pub key: String, + pub file: String, + pub how: How, + #[serde(default, skip_serializing_if = "String::is_empty")] + pub detail: String, +} + +#[derive(Debug, Default, Serialize, Deserialize)] +pub struct Fetched { + pub files: Vec<Outcome>, +} + +impl Fetched { + pub fn count(&self, how: How) -> usize { + self.files.iter().filter(|o| o.how == how).count() + } + /// `<work>/fetch.json`, empty when the step has not run. + pub fn load(work: &Path) -> Self { + read_json(&work.join("fetch.json")).unwrap_or_default() + } +} + +/// The jars of the records `want` picks that the work folder lacks. +pub fn fetch( + work: &Path, + records: &[Record], + from: Option<&Path>, + want: &dyn Fn(&Record) -> bool, + log: Log, +) -> Result<Fetched, String> { + let pack: Pack = read_json(&work.join("pack.json"))?; + let cache = Cache::load(&work.join("modrinth.json")); + let files: HashMap<&str, &PackFile> = pack.files.iter().map(|f| (f.path.as_str(), f)).collect(); + let jars = work.join("jars"); + fs::create_dir_all(&jars).map_err(|e| format!("{}: {e}", jars.display()))?; + let local = from.map(Local::index); + let wanted: Vec<&Record> = records.iter().filter(|r| want(r)).collect(); + let mut fetched = Fetched::default(); + for (i, r) in wanted.iter().enumerate() { + let Some(file) = files.get(r.path.as_str()) else { + continue; + }; + let name = file.file_name(); + let outcome = |how, detail: String| Outcome { + key: r.key.clone(), + file: name.to_owned(), + how, + detail, + }; + let dest = jars.join(name); + let sha1 = file.sha1.as_deref(); + let o = if r.jar.is_some() || matches(&dest, sha1) { + outcome(How::Present, String::new()) + } else if let Some(path) = local.as_ref().and_then(|l| l.find(file)) { + match fs::copy(&path, &dest) { + Ok(_) => outcome(How::Local, path.display().to_string()), + Err(e) => outcome(How::Failed, format!("{}: {e}", path.display())), + } + } else if let Some(url) = url_of(file, &cache) { + log(format!("{}/{} {name}", i + 1, wanted.len())); + match download(&url, sha1, &dest) { + Ok(()) => outcome(How::Downloaded, url), + Err(e) => outcome(How::Failed, e), + } + } else if pack.format == "curseforge" || file.curseforge.is_some() { + outcome( + How::NeedsKey, + "on CurseForge only: choose your installed instance, or put the jar in jars/" + .into(), + ) + } else { + outcome(How::NotFound, "no download link in the pack".into()) + }; + if matches!(o.how, How::Failed | How::NeedsKey | How::NotFound) { + log(format!("{}: {} ({})", o.key, o.how.label(), o.detail)); + } + fetched.files.push(o); + } + // The loader's own data (its convention tags), from its own repository. + if let Some((dest, url)) = crate::worldgen::loader_jar(work, &pack.loader, &pack.loader_version) + .filter(|(dest, _)| !dest.exists()) + { + let sha1 = ureq::get(&format!("{url}.sha1")) + .set("User-Agent", modrinth::AGENT) + .call() + .ok() + .and_then(|r| r.into_string().ok()) + .map(|s| s.trim().to_owned()); + let made = dest.parent().map(fs::create_dir_all); + match made + .unwrap_or(Ok(())) + .map_err(|e| e.to_string()) + .and_then(|()| download(&url, sha1.as_deref(), &dest)) + { + Ok(()) => log(format!("loader: {}", dest.display())), + Err(e) => log(format!( + "loader: {e} (structures keep only vanilla biome tags)" + )), + } + } + write_json(&work.join("fetch.json"), &fetched)?; + log(format!( + "jars: {} downloaded, {} copied, {} already here, {} need a CurseForge key, {} not found, {} failed", + fetched.count(How::Downloaded), + fetched.count(How::Local), + fetched.count(How::Present), + fetched.count(How::NeedsKey), + fetched.count(How::NotFound), + fetched.count(How::Failed) + )); + Ok(fetched) +} + +/// Whether `path` holds the file with this SHA-1 (any file when none is known). +fn matches(path: &Path, sha1: Option<&str>) -> bool { + match (fs::read(path), sha1) { + (Ok(bytes), Some(h)) => sha1_hex(&bytes).eq_ignore_ascii_case(h), + (Ok(_), None) => true, + (Err(_), _) => false, + } +} + +/// The pack's own download link, else the same file on Modrinth. +pub fn url_of(file: &PackFile, cache: &Cache) -> Option<String> { + let allowed = |u: &&str| HOSTS.iter().any(|h| u.starts_with(h)); + if let Some(u) = file.downloads.iter().map(String::as_str).find(allowed) { + return Some(u.to_owned()); + } + let sha1 = file.sha1.as_deref()?; + cache.versions.get(sha1)?["files"] + .as_array()? + .iter() + .find(|f| f["hashes"]["sha1"].as_str() == Some(sha1))?["url"] + .as_str() + .filter(allowed) + .map(str::to_owned) +} + +/// Downloads `url` to `dest`, kept only if its SHA-1 is `sha1`. +fn download(url: &str, sha1: Option<&str>, dest: &Path) -> Result<(), String> { + let response = ureq::get(url) + .set("User-Agent", modrinth::AGENT) + .call() + .map_err(|e| format!("{url}: {e}"))?; + let mut bytes = Vec::new(); + response + .into_reader() + .take(MAX_JAR) + .read_to_end(&mut bytes) + .map_err(|e| format!("{url}: {e}"))?; + let got = sha1_hex(&bytes); + if let Some(want) = sha1.filter(|w| !w.eq_ignore_ascii_case(&got)) { + return Err(format!("{url}: SHA-1 {got}, the pack says {want}")); + } + let part = dest.with_extension("part"); + fs::write(&part, &bytes).map_err(|e| format!("{}: {e}", part.display()))?; + fs::rename(&part, dest).map_err(|e| format!("{}: {e}", dest.display())) +} + +/// The jars in a folder of the player's, by SHA-1 and by name, and what a +/// CurseForge app instance (`minecraftinstance.json`) says each +/// project's file is called. +#[derive(Default)] +struct Local { + by_sha1: HashMap<String, PathBuf>, + by_name: HashMap<String, PathBuf>, + curseforge: HashMap<(u64, u64), String>, +} + +impl Local { + fn index(dir: &Path) -> Local { + let mut local = Local::default(); + local.walk(dir, 3); + if let Ok(text) = fs::read_to_string(dir.join("minecraftinstance.json")) { + if let Ok(v) = serde_json::from_str::<Value>(&text) { + local.curseforge = curseforge_names(&v); + } + } + local + } + + fn walk(&mut self, dir: &Path, depth: usize) { + let Ok(entries) = fs::read_dir(dir) else { + return; + }; + for path in entries.flatten().map(|e| e.path()) { + if path.is_dir() { + if depth > 0 { + self.walk(&path, depth - 1); + } + } else if path.extension().is_some_and(|x| x == "jar") { + if let Ok(bytes) = fs::read(&path) { + self.by_sha1.insert(sha1_hex(&bytes), path.clone()); + } + if let Some(n) = path.file_name() { + self.by_name + .insert(n.to_string_lossy().into_owned(), path.clone()); + } + } + } + } + + fn find(&self, file: &PackFile) -> Option<PathBuf> { + if let Some(h) = &file.sha1 { + return self.by_sha1.get(&h.to_ascii_lowercase()).cloned(); + } + let name = file + .curseforge + .and_then(|ids| self.curseforge.get(&ids)) + .map_or(file.file_name(), String::as_str); + self.by_name.get(name).cloned() + } +} + +/// `installedAddons[]`: (project, file) → the jar's file name. +pub fn curseforge_names(instance: &Value) -> HashMap<(u64, u64), String> { + instance["installedAddons"] + .as_array() + .into_iter() + .flatten() + .filter_map(|a| { + let file = &a["installedFile"]; + Some(( + (a["addonID"].as_u64()?, file["id"].as_u64()?), + file["fileName"].as_str()?.to_owned(), + )) + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + fn file(sha1: Option<&str>, downloads: &[&str]) -> PackFile { + PackFile { + path: "mods/a.jar".into(), + sha1: sha1.map(str::to_owned), + downloads: downloads.iter().map(|d| (*d).to_owned()).collect(), + ..PackFile::default() + } + } + + #[test] + fn links_come_from_the_pack_or_modrinth_and_only_allowed_hosts() { + let mut cache = Cache::default(); + cache.versions.insert( + "ab".into(), + json!({"files": [ + {"hashes": {"sha1": "cd"}, "url": "https://cdn.modrinth.com/other.jar"}, + {"hashes": {"sha1": "ab"}, "url": "https://cdn.modrinth.com/a.jar"}]}), + ); + let pack_link = file( + Some("ab"), + &["https://evil.example/a.jar", "https://github.com/a.jar"], + ); + assert_eq!( + url_of(&pack_link, &cache).as_deref(), + Some("https://github.com/a.jar") + ); + let modrinth = file(Some("ab"), &["https://evil.example/a.jar"]); + assert_eq!( + url_of(&modrinth, &cache).as_deref(), + Some("https://cdn.modrinth.com/a.jar") + ); + assert_eq!(url_of(&file(Some("ef"), &[]), &cache), None); + assert_eq!(url_of(&file(None, &[]), &cache), None); + } + + #[test] + fn reads_a_curseforge_instance() { + let instance = json!({"installedAddons": [ + {"addonID": 238222, "installedFile": {"id": 5101366, "fileName": "jei-1.21.1.jar"}}, + {"addonID": 1, "installedFile": {"fileName": "no-id.jar"}}]}); + let names = curseforge_names(&instance); + assert_eq!(names.len(), 1); + assert_eq!(names[&(238222, 5101366)], "jei-1.21.1.jar"); + } + + #[test] + fn copies_the_players_jars_and_lists_what_needs_a_key() { + let root = std::env::temp_dir().join(format!("horizon-fetch-{}", std::process::id())); + let _ = fs::remove_dir_all(&root); + let (work, mine) = (root.join("work"), root.join("instance/mods")); + fs::create_dir_all(&mine).unwrap(); + fs::create_dir_all(&work).unwrap(); + fs::write(mine.join("renamed.jar"), b"jar a").unwrap(); + fs::write(mine.join("cf.jar"), b"jar b").unwrap(); + fs::write( + root.join("instance/minecraftinstance.json"), + json!({"installedAddons": [{"addonID": 7, "installedFile": {"id": 8, "fileName": "cf.jar"}}]}) + .to_string(), + ) + .unwrap(); + let pack = Pack { + format: "curseforge".into(), + files: vec![ + PackFile { + path: "mods/a.jar".into(), + sha1: Some(sha1_hex(b"jar a")), + ..PackFile::default() + }, + PackFile { + path: "mods/curseforge-7-8".into(), + curseforge: Some((7, 8)), + ..PackFile::default() + }, + PackFile { + path: "mods/curseforge-9-9".into(), + curseforge: Some((9, 9)), + ..PackFile::default() + }, + ], + ..Pack::default() + }; + write_json(&work.join("pack.json"), &pack).unwrap(); + let records: Vec<Record> = pack + .files + .iter() + .map(|f| Record { + key: f.file_name().into(), + path: f.path.clone(), + ..Record::default() + }) + .collect(); + let fetched = fetch( + &work, + &records, + Some(&root.join("instance")), + &|_| true, + &|_| {}, + ) + .unwrap(); + let hows: Vec<How> = fetched.files.iter().map(|o| o.how).collect(); + assert_eq!(hows, [How::Local, How::Local, How::NeedsKey]); + assert_eq!(fs::read(work.join("jars/a.jar")).unwrap(), b"jar a"); + assert_eq!( + fs::read(work.join("jars/curseforge-7-8")).unwrap(), + b"jar b" + ); + // A second run finds them in place. + let again = fetch(&work, &records, None, &|_| true, &|_| {}).unwrap(); + assert_eq!(again.count(How::Present), 2); + assert_eq!(Fetched::load(&work).count(How::Present), 2); + fs::remove_dir_all(&root).unwrap(); + } +} diff --git a/crates/horizon-port-pack/src/import.rs b/crates/horizon-port-pack/src/import.rs index dd712acf1..0a3aa8d06 100644 --- a/crates/horizon-port-pack/src/import.rs +++ b/crates/horizon-port-pack/src/import.rs @@ -22,8 +22,20 @@ pub struct Pack { pub files: Vec<PackFile>, /// Override files by top folder (`config`, `kubejs`, ...). pub overrides: BTreeMap<String, usize>, + /// A CurseForge zip's files by project and file id only: how many its + /// manifest lists, when the files came from the pack's own mod list. + #[serde(default, skip_serializing_if = "is_zero")] + pub curseforge_files: usize, } +fn is_zero(n: &usize) -> bool { + *n == 0 +} + +/// Where Crash Assistant keeps its list in an instance (and in the +/// overrides of packs that ship it, such as All the Mods 10). +pub const MODLIST: &str = "config/crash_assistant/modlist.json"; + #[derive(Debug, Default, Serialize, Deserialize)] pub struct PackFile { /// Path in the instance (`mods/create-1.21.1-6.0.4.jar`). @@ -83,7 +95,16 @@ pub fn import(path: &Path) -> Result<Pack, String> { return Ok(mrpack(&index, &names(&zip))); } if let Some(manifest) = read_json(&mut zip, "manifest.json")? { - return Ok(curseforge(&manifest, &names(&zip))); + let mut pack = curseforge(&manifest, &names(&zip)); + // The manifest names files only by CurseForge ids, which need a key + // to resolve; the pack's own mod list names them and gives their + // SHA-1s, so Modrinth can answer for most. + let root = manifest["overrides"].as_str().unwrap_or("overrides"); + if let Some(list) = read_json(&mut zip, &format!("{root}/{MODLIST}"))? { + pack.curseforge_files = pack.files.len(); + pack.files = modlist(&list)?.files; + } + return Ok(pack); } Err(format!( "{}: neither modrinth.index.json nor manifest.json", @@ -103,7 +124,7 @@ fn read_json(zip: &mut zip::ZipArchive<fs::File>, name: &str) -> Result<Option<V entry .read_to_string(&mut text) .map_err(|e| format!("{name}: {e}"))?; - serde_json::from_str(&text) + serde_json::from_str(text.trim_start_matches('\u{feff}')) .map(Some) .map_err(|e| format!("{name}: {e}")) } @@ -161,6 +182,7 @@ pub fn mrpack(index: &Value, names: &[String]) -> Pack { names, &["overrides", "client-overrides", "server-overrides"], ), + ..Pack::default() } } @@ -197,6 +219,7 @@ pub fn curseforge(manifest: &Value, names: &[String]) -> Pack { loader_version: loader_version.to_owned(), files, overrides: overrides(names, &[root]), + ..Pack::default() } } @@ -418,4 +441,36 @@ mod tests { assert_eq!(p.files[0].curseforge, Some((238222, 5101366))); assert_eq!(p.overrides["kubejs"], 1); } + + #[test] + fn a_curseforge_zip_takes_names_from_its_mod_list() { + use std::io::Write; + let dir = std::env::temp_dir().join(format!("horizon-cf-zip-{}", std::process::id())); + fs::create_dir_all(&dir).unwrap(); + let path = dir.join("pack.zip"); + let mut zip = zip::ZipWriter::new(fs::File::create(&path).unwrap()); + let opts = zip::write::SimpleFileOptions::default(); + let manifest = json!({"name": "Pack", "overrides": "overrides", + "minecraft": {"version": "1.21.1", "modLoaders": [{"id": "neoforge-21.1.259"}]}, + "files": [{"projectID": 1, "fileID": 2}, {"projectID": 3, "fileID": 4}]}); + zip.start_file("manifest.json", opts).unwrap(); + zip.write_all(manifest.to_string().as_bytes()).unwrap(); + zip.start_file(format!("overrides/{MODLIST}"), opts) + .unwrap(); + let list = json!({"mcw-doors-1.1.2.jar": {"jarName": "mcw-doors-1.1.2.jar", + "modId": "mcwdoors", "modrinthHash": "0123456789abcdef0123456789abcdef01234567"}}); + // Crash Assistant writes a byte order mark. + zip.write_all(format!("\u{feff}{list}").as_bytes()).unwrap(); + zip.finish().unwrap(); + let pack = import(&path).unwrap(); + assert_eq!( + (pack.format.as_str(), pack.name.as_str()), + ("curseforge", "Pack") + ); + assert_eq!(pack.curseforge_files, 2); + assert_eq!(pack.files.len(), 1); + assert_eq!(pack.files[0].path, "mods/mcw-doors-1.1.2.jar"); + assert_eq!(pack.overrides["config"], 1); + fs::remove_dir_all(&dir).unwrap(); + } } diff --git a/crates/horizon-port-pack/src/lib.rs b/crates/horizon-port-pack/src/lib.rs new file mode 100644 index 000000000..08f9b273f --- /dev/null +++ b/crates/horizon-port-pack/src/lib.rs @@ -0,0 +1,19 @@ +//! Port a Java modpack to Horizon (`docs/modpacks.md`): the steps behind the +//! `horizon-port-pack` command, and the launcher's "Import modpack". +//! +//! Every step reads and writes files in a work folder (`pipeline`), so it can +//! be rerun, resumed and inspected. The work folder belongs to the user: the +//! jars they downloaded and what is carried over from them stay there and +//! are never uploaded or committed. +pub mod carry; +pub mod classify; +pub mod fetch; +pub mod import; +pub mod inventory; +pub mod jar; +pub mod modrinth; +pub mod oracle; +pub mod pipeline; +pub mod processors; +pub mod report; +pub mod worldgen; diff --git a/crates/horizon-port-pack/src/main.rs b/crates/horizon-port-pack/src/main.rs index 3e6cdf4d7..e766a442d 100644 --- a/crates/horizon-port-pack/src/main.rs +++ b/crates/horizon-port-pack/src/main.rs @@ -6,57 +6,77 @@ //! `<work>/modrinth.json`), the jars in the instance or `<work>/jars/`, //! classes, tiers and dependency layers in `<work>/inventory.json` and //! `inventory.csv`. +//! - `fetch <work> [--from <folder>] [<mod id>...]`: the jars of the +//! portable mods (content, data tier; or the ones named) into +//! `<work>/jars/`: copied from the player's folder, or downloaded from the +//! pack's own links or Modrinth's CDN, hash checked. What only CurseForge +//! has needs a key and is listed in `<work>/fetch.json`. //! - `carry <work> [--resources <prepared>] [<mod id>...]`: the content //! mods' data and assets into `<work>/mods/<id>/carry/`, a folder mod for //! local use. With the NeoForge oracle's `<work>/oracle/facts.json` //! (`tools/neoforge_oracle.py`), blocks and items come from its facts; //! `--resources` limits templates to the blocks Horizon's tables have. -//! //! - `report <work>`: the coverage report, per mod ported, partial, not yet //! or not needed with the reason, in `<work>/coverage.json` and //! `coverage.md`. +//! - `port <pack> <work> [--from <folder>] [--resources <prepared>]`: every +//! step for the portable mods, as the launcher's "Import modpack" runs it. //! //! The work folder belongs to the user: jars and carried-over files stay //! there and are never uploaded or committed. -mod carry; -mod classify; -mod import; -mod inventory; -mod jar; -mod modrinth; -mod oracle; -mod report; +use horizon_port_pack::{fetch, inventory::Record, pipeline}; +use std::{ + path::{Path, PathBuf}, + process::ExitCode, +}; -use std::{fs, path::Path, process::ExitCode}; - -const USAGE: &str = "usage: horizon-port-pack import <pack> <work> | inventory <work> [--offline] | carry <work> [--resources <prepared>] [<mod id>...] | report <work>"; +const USAGE: &str = "usage: horizon-port-pack import <pack> <work> | inventory <work> [--offline] | fetch <work> [--from <folder>] [<mod id>...] | carry <work> [--resources <prepared>] [<mod id>...] | report <work> | port <pack> <work> [--from <folder>] [--resources <prepared>]"; fn main() -> ExitCode { - let args: Vec<String> = std::env::args().skip(1).collect(); - let path = |i: usize| args.get(i).map(Path::new).ok_or(USAGE.to_owned()); - let result = match args.first().map(String::as_str) { - Some("import") => path(1) - .and_then(|p| Ok((p, path(2)?))) - .and_then(|(p, w)| import(p, w)), - Some("inventory") => { - path(1).and_then(|w| inventory(w, args.iter().any(|a| a == "--offline"))) + let mut args: Vec<String> = std::env::args().skip(1).collect(); + let result = (|| { + let from = option(&mut args, "--from")?; + let resources = option(&mut args, "--resources")?; + let offline = take_flag(&mut args, "--offline"); + let path = |i: usize| args.get(i).map(Path::new).ok_or(USAGE.to_owned()); + let rest = || args.get(2..).unwrap_or_default().to_vec(); + let log = &|line: String| println!("{line}"); + match args.first().map(String::as_str) { + Some("import") => pipeline::import(path(1)?, path(2)?, log).map(drop), + Some("inventory") => pipeline::inventory(path(1)?, offline, log).map(drop), + Some("fetch") => { + let work = path(1)?; + let records: Vec<Record> = pipeline::read_json(&work.join("inventory.json"))?; + let only = rest(); + let want = |r: &Record| { + if only.is_empty() { + pipeline::portable(r) + } else { + only.contains(&r.key) + } + }; + fetch::fetch(work, &records, from.as_deref(), &want, log).map(drop) + } + Some("carry") => { + pipeline::carry(path(1)?, resources.as_deref(), &rest(), log).map(drop) + } + Some("report") => { + let work = path(1)?; + pipeline::report(work, log)?; + println!("{}", work.join("coverage.md").display()); + Ok(()) + } + Some("port") => { + let work = path(2)?; + pipeline::import(path(1)?, work, log)?; + pipeline::inventory(work, offline, log)?; + pipeline::port(work, resources.as_deref(), from.as_deref(), log)?; + println!("{}", work.join("coverage.md").display()); + Ok(()) + } + _ => Err(USAGE.to_owned()), } - Some("carry") => path(1).and_then(|w| { - let mut rest = args[2..].to_vec(); - let resources = match rest.iter().position(|a| a == "--resources") { - Some(i) if i + 1 < rest.len() => { - let r = rest.remove(i + 1); - rest.remove(i); - Some(std::path::PathBuf::from(r)) - } - Some(_) => return Err(USAGE.to_owned()), - None => None, - }; - carry(w, resources.as_deref(), &rest) - }), - Some("report") => path(1).and_then(report), - _ => Err(USAGE.to_owned()), - }; + })(); match result { Ok(()) => ExitCode::SUCCESS, Err(e) => { @@ -66,158 +86,21 @@ fn main() -> ExitCode { } } -fn write_json(path: &Path, value: &impl serde::Serialize) -> Result<(), String> { - let text = serde_json::to_string_pretty(value).map_err(|e| e.to_string())?; - fs::write(path, text).map_err(|e| format!("{}: {e}", path.display())) -} - -fn read_json<T: serde::de::DeserializeOwned>(path: &Path) -> Result<T, String> { - let text = fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?; - serde_json::from_str(&text).map_err(|e| format!("{}: {e}", path.display())) -} - -fn report(work: &Path) -> Result<(), String> { - let pack: import::Pack = read_json(&work.join("pack.json"))?; - let records: Vec<inventory::Record> = read_json(&work.join("inventory.json"))?; - let rows = report::build(&records, work); - write_json(&work.join("coverage.json"), &rows)?; - let title = format!( - "{} ({} {}, Minecraft {})", - pack.name, pack.loader, pack.loader_version, pack.minecraft - ); - fs::write(work.join("coverage.md"), report::markdown(&title, &rows)) - .map_err(|e| format!("coverage.md: {e}"))?; - println!("{title}: {} files: {:?}", rows.len(), report::counts(&rows)); - println!("{}", work.join("coverage.md").display()); - Ok(()) -} - -fn import(pack: &Path, work: &Path) -> Result<(), String> { - let pack = import::import(pack)?; - fs::create_dir_all(work).map_err(|e| format!("{}: {e}", work.display()))?; - write_json(&work.join("pack.json"), &pack)?; - let mut kinds = std::collections::BTreeMap::<&str, usize>::new(); - for f in &pack.files { - *kinds.entry(f.kind()).or_default() += 1; - } - println!( - "{} ({}): Minecraft {}, {} {}; files {kinds:?}; overrides {:?}", - pack.name, pack.format, pack.minecraft, pack.loader, pack.loader_version, pack.overrides - ); - Ok(()) -} - -fn inventory(work: &Path, offline: bool) -> Result<(), String> { - let pack: import::Pack = read_json(&work.join("pack.json"))?; - let cache_path = work.join("modrinth.json"); - let mut cache = modrinth::Cache::load(&cache_path); - if !offline { - let hashes: Vec<String> = pack.files.iter().filter_map(|f| f.sha1.clone()).collect(); - cache.fetch(&hashes)?; - write_json(&cache_path, &cache)?; - } - let records = inventory::build(&pack, &cache, work); - write_json(&work.join("inventory.json"), &records)?; - fs::write(work.join("inventory.csv"), inventory::csv(&records)) - .map_err(|e| format!("inventory.csv: {e}"))?; - let jars = records.iter().filter(|r| r.jar.is_some()).count(); - println!("{} files, {jars} jars read", records.len()); - for (title, field) in [ - ( - "class", - (|r: &inventory::Record| r.class.as_str()) as fn(&inventory::Record) -> &str, - ), - ("tier", |r| r.tier.as_str()), - ("confidence", |r| r.confidence.as_str()), - ("source", |r| r.source.as_str()), - ] { - println!("{title}: {:?}", inventory::counts(&records, field)); - } - let layers = records.iter().map(|r| r.layer).max().map_or(0, |m| m + 1); - let per_layer: Vec<usize> = (0..layers) - .map(|l| records.iter().filter(|r| r.layer == l).count()) - .collect(); - println!("layers: {per_layer:?}"); - let missing: usize = records.iter().map(|r| r.missing.len()).sum(); - if missing > 0 { - println!("{missing} required dependencies are not in the pack (see inventory.json)"); - } - Ok(()) -} - -/// The vanilla blocks a prepared resource root has (`facts.json`'s defaults). -fn vanilla_blocks(resources: &Path) -> Result<std::collections::BTreeSet<String>, String> { - let facts: serde_json::Value = read_json(&resources.join("facts.json"))?; - let defaults = facts["defaults"] - .as_object() - .ok_or("facts.json: no defaults")?; - Ok(defaults.keys().cloned().collect()) -} - -fn carry(work: &Path, resources: Option<&Path>, only: &[String]) -> Result<(), String> { - let records: Vec<inventory::Record> = read_json(&work.join("inventory.json"))?; - let facts_path = work.join("oracle/facts.json"); - let facts = if facts_path.exists() { - let f = oracle::Facts::read(&facts_path)?; - println!( - "oracle facts: {} blocks, {} items", - f.blocks.len(), - f.items.len() - ); - Some(f) - } else { - None - }; - let vanilla = resources.map(vanilla_blocks).transpose()?; - let allowed = |b: &str| vanilla.as_ref().is_none_or(|v| v.contains(b)); - let mut done = 0; - for r in &records { - let chosen = if only.is_empty() { - r.class == "content" - } else { - only.contains(&r.key) - }; - let (Some(jar), Some(info)) = (&r.jar, &r.info) else { - if chosen && !only.is_empty() { - eprintln!("{}: no jar (put it in {}/jars/)", r.key, work.display()); - } - continue; - }; - if !chosen { - continue; - } - match carry::carry( - jar, - info, - &work.join("mods").join(&r.key), - facts.as_ref(), - &allowed, - ) { - Ok(report) => { - done += 1; - println!( - "{}: {} files, {} recipes upgraded ({} kept), {} loot tables upgraded, {} item definitions added, {} blocks ({} left out, {} with differences), {} items ({} left out), {} convention tags, {} outside", - r.key, - report.copied, - report.recipes_upgraded, - report.recipes_kept.len(), - report.loot_tables_upgraded, - report.item_definitions_added, - report.blocks.len(), - report.blocks_unknown.len(), - report.differences.len(), - report.items, - report.items_unknown.len(), - report.convention_tags, - report.outside.len() - ); - } - Err(e) => eprintln!("{}: {e}", r.key), +/// Removes `--name <value>` from `args`. +fn option(args: &mut Vec<String>, name: &str) -> Result<Option<PathBuf>, String> { + match args.iter().position(|a| a == name) { + Some(i) if i + 1 < args.len() => { + let value = args.remove(i + 1); + args.remove(i); + Ok(Some(PathBuf::from(value))) } + Some(_) => Err(USAGE.to_owned()), + None => Ok(None), } - println!( - "{done} mods carried over into {}", - work.join("mods").display() - ); - Ok(()) +} + +fn take_flag(args: &mut Vec<String>, name: &str) -> bool { + let before = args.len(); + args.retain(|a| a != name); + args.len() != before } diff --git a/crates/horizon-port-pack/src/modrinth.rs b/crates/horizon-port-pack/src/modrinth.rs index 8c34a2004..4e90c871a 100644 --- a/crates/horizon-port-pack/src/modrinth.rs +++ b/crates/horizon-port-pack/src/modrinth.rs @@ -1,13 +1,13 @@ //! Modrinth metadata for the pack's files: the version of each file by -//! SHA-1, and its project (categories, licence, sides). Only metadata: no -//! jar is downloaded. Answers are cached in `<work>/modrinth.json`, so a +//! SHA-1, and its project (categories, licence, sides). Only metadata (the +//! jars are `fetch`'s). Answers are cached in `<work>/modrinth.json`, so a //! rerun works offline. use serde::{Deserialize, Serialize}; use serde_json::Value; use std::{collections::BTreeMap, path::Path}; const API: &str = "https://api.modrinth.com/v2"; -const AGENT: &str = "ludovic111/horizon horizon-port-pack/0.1 (docs/modpacks.md)"; +pub(crate) const AGENT: &str = "ludovic111/horizon horizon-port-pack/0.1 (docs/modpacks.md)"; #[derive(Debug, Default, Serialize, Deserialize)] pub struct Cache { diff --git a/crates/horizon-port-pack/src/oracle.rs b/crates/horizon-port-pack/src/oracle.rs index 1fa9a717c..7e04aa9ef 100644 --- a/crates/horizon-port-pack/src/oracle.rs +++ b/crates/horizon-port-pack/src/oracle.rs @@ -60,6 +60,10 @@ pub struct State { pub sound: Value, pub outline: Value, pub collision: Value, + /// What breaking the state drops, per tool (`hand`, `diamond_pickaxe`, + /// `shears`, `silk_touch_pickaxe`...): item id → count. + #[serde(default)] + pub drops: BTreeMap<String, BTreeMap<String, u32>>, } #[derive(Debug, Deserialize)] @@ -76,6 +80,23 @@ pub struct Item { const VANILLA_PACKAGE: &str = "net.minecraft."; /// A state key with its properties sorted, so keys compare across blocks. +/// Which states of a block drop the block itself. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum SelfDrop { + /// Every state: `dropSelf`. + Always, + /// Only the states where this property has this value. + When(String, String), +} + +/// A state key's `property=value` pairs. +fn pairs(key: &str) -> BTreeSet<(String, String)> { + key.split(',') + .filter_map(|p| p.split_once('=')) + .map(|(k, v)| (k.to_owned(), v.to_owned())) + .collect() +} + pub fn state_key(key: &str) -> String { let mut parts: Vec<&str> = key.split(',').filter(|p| !p.is_empty()).collect(); parts.sort_unstable(); @@ -96,6 +117,45 @@ impl Block { .first() .is_some_and(|c| !c.starts_with(VANILLA_PACKAGE)) } + /// Every state drops one `item` (the block's own) with every tool, except + /// where the state needs a tool and gets none: vanilla's `dropSelf`. Or + /// only the states where one property has one value do, and the others + /// drop nothing with any tool: a door's lower half (`createDoorTable`). + /// Mods often register these tables in code, so their jar has no file. + pub fn drops_itself(&self, item: &str) -> Option<SelfDrop> { + let one = BTreeMap::from([(item.to_owned(), 1)]); + let (mut dropping, mut nothing) = (Vec::new(), Vec::new()); + for (key, state) in &self.states { + if state.drops.is_empty() { + continue; + } + let mut drops = false; + let mut short = false; + for got in state.drops.values() { + if *got == one { + drops = true; + } else if got.is_empty() { + short = true; + } else { + return None; + } + } + match (drops, short) { + (true, true) if !state.requires_tool => return None, + (true, _) => dropping.push(pairs(key)), + (false, _) => nothing.push(pairs(key)), + } + } + let (first, rest) = dropping.split_first()?; + if nothing.is_empty() { + return Some(SelfDrop::Always); + } + first + .iter() + .filter(|pair| rest.iter().all(|s| s.contains(*pair))) + .find(|pair| nothing.iter().all(|s| !s.contains(*pair))) + .map(|(p, v)| SelfDrop::When(p.clone(), v.clone())) + } fn default_state(&self) -> Option<&State> { self.states .get(&self.default) @@ -373,6 +433,65 @@ mod tests { assert_eq!(tile.differences[0], "sounds: no vanilla block has them"); } + #[test] + fn drops_itself_is_vanillas_drop_self() { + let cube = json!([[0, 0, 0, 1, 1, 1]]); + let with = |requires_tool: bool, drops: Value| { + let mut s = state(1.5, "stone", cube.clone()); + s["requires_tool"] = json!(requires_tool); + s["drops"] = drops; + let b: Block = serde_json::from_value(block(&[], json!({}), json!({"": s}))).unwrap(); + b.drops_itself("m:tile") + }; + let me = json!({"m:tile": 1}); + let always = Some(SelfDrop::Always); + assert_eq!(with(false, json!({"hand": me, "shears": me})), always); + // A state that needs a tool drops nothing by hand, as dropSelf does. + assert_eq!( + with(true, json!({"hand": {}, "diamond_pickaxe": me})), + always + ); + assert_eq!( + with(false, json!({"hand": {}, "diamond_pickaxe": me})), + None + ); + // Two of itself (a double slab), or something else, is not dropSelf. + assert_eq!(with(false, json!({"hand": {"m:tile": 2}})), None); + assert_eq!(with(false, json!({"hand": {"m:other": 1}})), None); + assert_eq!(with(false, json!({})), None); + } + + #[test] + fn a_door_drops_from_its_lower_half() { + let cube = json!([[0, 0, 0, 1, 1, 1]]); + let me = json!({"hand": {"m:door": 1}, "shears": {"m:door": 1}}); + let none = json!({"hand": {}, "shears": {}}); + let door = |upper: &Value| { + let mut states = serde_json::Map::new(); + for (key, drops) in [ + ("half=lower,open=false", &me), + ("half=lower,open=true", &me), + ("half=upper,open=false", upper), + ("half=upper,open=true", upper), + ] { + let mut s = state(3.0, "wood", cube.clone()); + s["drops"] = drops.clone(); + states.insert(key.to_owned(), s); + } + let props = json!({"half": ["upper", "lower"], "open": ["true", "false"]}); + let b: Block = + serde_json::from_value(block(&[], props, Value::Object(states))).unwrap(); + b.drops_itself("m:door") + }; + assert_eq!( + door(&none), + Some(SelfDrop::When("half".to_owned(), "lower".to_owned())) + ); + assert_eq!(door(&me), Some(SelfDrop::Always)); + // Something else from the upper half: not a door table. + assert_eq!(door(&json!({"hand": {"m:other": 1}})), None); + } + #[test] fn state_keys_sort() { assert_eq!( diff --git a/crates/horizon-port-pack/src/pipeline.rs b/crates/horizon-port-pack/src/pipeline.rs new file mode 100644 index 000000000..7152dc921 --- /dev/null +++ b/crates/horizon-port-pack/src/pipeline.rs @@ -0,0 +1,264 @@ +//! The steps on a work folder, as the command line and the launcher run +//! them. Each one reads what the earlier steps wrote and says what it did +//! through `log`, one line at a time. +use crate::{carry, fetch, import, inventory, modrinth, oracle, report}; +use std::{ + collections::{BTreeMap, BTreeSet}, + fs, + path::Path, +}; + +/// Where a step's lines go: the terminal, or the launcher's log. +pub type Log<'a> = &'a dyn Fn(String); + +pub fn write_json(path: &Path, value: &impl serde::Serialize) -> Result<(), String> { + let text = serde_json::to_string_pretty(value).map_err(|e| e.to_string())?; + fs::write(path, text).map_err(|e| format!("{}: {e}", path.display())) +} + +pub fn read_json<T: serde::de::DeserializeOwned>(path: &Path) -> Result<T, String> { + let text = fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?; + serde_json::from_str(&text).map_err(|e| format!("{}: {e}", path.display())) +} + +/// Step 1: the pack at `pack` to `<work>/pack.json`. +pub fn import(pack: &Path, work: &Path, log: Log) -> Result<import::Pack, String> { + let pack = import::import(pack)?; + fs::create_dir_all(work).map_err(|e| format!("{}: {e}", work.display()))?; + write_json(&work.join("pack.json"), &pack)?; + let mut kinds = BTreeMap::<&str, usize>::new(); + for f in &pack.files { + *kinds.entry(f.kind()).or_default() += 1; + } + log(format!( + "{} ({}): Minecraft {}, {} {}; files {kinds:?}; overrides {:?}", + pack.name, pack.format, pack.minecraft, pack.loader, pack.loader_version, pack.overrides + )); + Ok(pack) +} + +/// Step 2: `<work>/inventory.json` and `inventory.csv`. Online, Modrinth is +/// asked about the files it has not answered for yet; when it cannot be +/// reached, the cached answers and the reviewed table classify alone. +pub fn inventory(work: &Path, offline: bool, log: Log) -> Result<Vec<inventory::Record>, String> { + let pack: import::Pack = read_json(&work.join("pack.json"))?; + let cache_path = work.join("modrinth.json"); + let mut cache = modrinth::Cache::load(&cache_path); + if !offline { + let hashes: Vec<String> = pack.files.iter().filter_map(|f| f.sha1.clone()).collect(); + match cache.fetch(&hashes) { + Ok(()) => write_json(&cache_path, &cache)?, + Err(e) => log(format!("Modrinth not reached, classifying offline: {e}")), + } + } + let records = inventory::build(&pack, &cache, work); + write_json(&work.join("inventory.json"), &records)?; + fs::write(work.join("inventory.csv"), inventory::csv(&records)) + .map_err(|e| format!("inventory.csv: {e}"))?; + let jars = records.iter().filter(|r| r.jar.is_some()).count(); + log(format!("{} files, {jars} jars read", records.len())); + for (title, field) in [ + ( + "class", + (|r: &inventory::Record| r.class.as_str()) as fn(&inventory::Record) -> &str, + ), + ("tier", |r| r.tier.as_str()), + ("confidence", |r| r.confidence.as_str()), + ("source", |r| r.source.as_str()), + ] { + log(format!("{title}: {:?}", inventory::counts(&records, field))); + } + let layers = records.iter().map(|r| r.layer).max().map_or(0, |m| m + 1); + let per_layer: Vec<usize> = (0..layers) + .map(|l| records.iter().filter(|r| r.layer == l).count()) + .collect(); + log(format!("layers: {per_layer:?}")); + let missing: usize = records.iter().map(|r| r.missing.len()).sum(); + if missing > 0 { + log(format!( + "{missing} required dependencies are not in the pack (see inventory.json)" + )); + } + Ok(records) +} + +/// The vanilla blocks a prepared resource root has (`facts.json`'s defaults). +fn vanilla_blocks(resources: &Path) -> Result<BTreeSet<String>, String> { + let facts: serde_json::Value = read_json(&resources.join("facts.json"))?; + let defaults = facts["defaults"] + .as_object() + .ok_or("facts.json: no defaults")?; + Ok(defaults.keys().cloned().collect()) +} + +/// Step 4: the content mods with a jar (or the ones in `only`) carried over +/// into `<work>/mods/<id>/carry/`. Returns how many were. +pub fn carry( + work: &Path, + resources: Option<&Path>, + only: &[String], + log: Log, +) -> Result<usize, String> { + let records: Vec<inventory::Record> = read_json(&work.join("inventory.json"))?; + let facts_path = work.join("oracle/facts.json"); + let facts = if facts_path.exists() { + let f = oracle::Facts::read(&facts_path)?; + log(format!( + "oracle facts: {} blocks, {} items", + f.blocks.len(), + f.items.len() + )); + Some(f) + } else { + None + }; + // Without the prepared tables, templates are not limited to what the game has. + let vanilla = resources.and_then(|r| { + vanilla_blocks(r) + .inspect_err(|e| log(format!("templates not limited to the game's blocks: {e}"))) + .ok() + }); + let allowed = |b: &str| vanilla.as_ref().is_none_or(|v| v.contains(b)); + let conventions = crate::worldgen::loader_biome_tags(work); + let chosen = |r: &inventory::Record| { + if only.is_empty() { + r.class == "content" + } else { + only.contains(&r.key) + } + }; + // The mods ported along (carried now or before), for the conditions of + // the mods' worldgen modifiers. + let ported: BTreeSet<&str> = records + .iter() + .filter(|r| { + r.jar.is_some() && (chosen(r) || work.join("mods").join(&r.key).join("carry").is_dir()) + }) + .filter_map(|r| Some(r.info.as_ref()?.mod_id.as_str())) + .chain(["minecraft"]) + .collect(); + let loaded = |m: &str| ported.contains(m); + let mut done = 0; + for r in &records { + let chosen = chosen(r); + let (Some(jar), Some(info)) = (&r.jar, &r.info) else { + if chosen && !only.is_empty() { + log(format!( + "{}: no jar (put it in {}/jars/)", + r.key, + work.display() + )); + } + continue; + }; + if !chosen { + continue; + } + // The oracle's facts for the mods it ran; a mod it did not load (a + // library missing) keeps the guesses from its blockstates. + let prefix = format!("{}:", info.mod_id); + let seen = |f: &&oracle::Facts| { + f.blocks + .keys() + .chain(f.items.keys()) + .any(|k| k.starts_with(&prefix)) + }; + match carry::carry( + jar, + info, + &work.join("mods").join(&r.key), + facts.as_ref().filter(seen), + &allowed, + &loaded, + &conventions, + ) { + Ok(report) => { + done += 1; + log(format!( + "{}: {} files, {} recipes upgraded ({} kept), {} loot tables upgraded ({} added), {} item definitions added, {} blocks ({} left out, {} with differences), {} items ({} left out), {} convention tags, {} outside", + r.key, + report.copied, + report.recipes_upgraded, + report.recipes_kept.len(), + report.loot_tables_upgraded, + report.loot_tables_added, + report.item_definitions_added, + report.blocks.len(), + report.blocks_unknown.len(), + report.differences.len(), + report.items, + report.items_unknown.len(), + report.convention_tags, + report.outside.len() + )); + } + Err(e) => log(format!("{}: {e}", r.key)), + } + } + log(format!( + "{done} mods carried over into {}", + work.join("mods").display() + )); + Ok(done) +} + +/// The pack's title as the report heads it. +pub fn title(pack: &import::Pack) -> String { + format!( + "{} ({} {}, Minecraft {})", + pack.name, pack.loader, pack.loader_version, pack.minecraft + ) +} + +/// Step 7: `<work>/coverage.json` and `coverage.md`. +pub fn report(work: &Path, log: Log) -> Result<Vec<report::Row>, String> { + let pack: import::Pack = read_json(&work.join("pack.json"))?; + let records: Vec<inventory::Record> = read_json(&work.join("inventory.json"))?; + let rows = report::build(&records, work); + write_json(&work.join("coverage.json"), &rows)?; + let title = title(&pack); + fs::write(work.join("coverage.md"), report::markdown(&title, &rows)) + .map_err(|e| format!("coverage.md: {e}"))?; + log(format!( + "{title}: {} files: {:?}", + rows.len(), + report::counts(&rows) + )); + Ok(rows) +} + +/// What a player's machine can port today without an agent: content mods +/// of the data tier (carry-over only). +pub fn portable(r: &inventory::Record) -> bool { + r.class == "content" && r.tier == "data" +} + +/// Everything after the import, for the portable mods: their jars (`fetch`, +/// from `from` when the player has them), the inventory again with the jars +/// read, the carry-over and the report. `resources` is the prepared +/// version the ports will play on. +pub fn port( + work: &Path, + resources: Option<&Path>, + from: Option<&Path>, + log: Log, +) -> Result<(fetch::Fetched, Vec<report::Row>), String> { + let records = match read_json::<Vec<inventory::Record>>(&work.join("inventory.json")) { + Ok(r) => r, + Err(_) => inventory(work, false, log)?, + }; + let fetched = fetch::fetch(work, &records, from, &portable, log)?; + // The jars tell classes and tiers better than metadata alone. + let records = inventory(work, true, log)?; + let only: Vec<String> = records + .iter() + .filter(|r| portable(r) && r.jar.is_some()) + .map(|r| r.key.clone()) + .collect(); + if only.is_empty() { + log("no portable mod has its jar: nothing to carry over".into()); + } else { + carry(work, resources, &only, log)?; + } + Ok((fetched, report(work, log)?)) +} diff --git a/crates/horizon-port-pack/src/processors.rs b/crates/horizon-port-pack/src/processors.rs new file mode 100644 index 000000000..5dd9c9730 --- /dev/null +++ b/crates/horizon-port-pack/src/processors.rs @@ -0,0 +1,1309 @@ +//! Structure processors the structure libraries write in Java, carried over +//! as data the game runs: the ones that give spawners their mob. +//! +//! - Moog's Structure Lib's and Repurposed Structures' spawner randomizing +//! processors pick a mob from a weighted list for each spawner. They become +//! `horizon:spawner_mobs` (`horizon_world::worldgen::structure::processor`), +//! with Repurposed's `rs_spawners` list written in. +//! - YUNG's Better Dungeons and Better Desert Temples turn a spawner or a +//! placeholder block into a spawner of a fixed mob. That is vanilla's `rule` +//! processor with an `append_static` block entity. +//! - Moog's vanilla loot swap only acts when its mod's "replace vanilla" +//! preset is on; one that ships off is a no-op and is taken out. +//! - Lithostitched's `reference` runs other processor lists in turn: their +//! processors are written in its place (Choice Theorem's Overhauled +//! Villages builds most of its lists so). Its `apply_random` and +//! `condition` become the game's `horizon:apply_random` and +//! `horizon:condition`, with the lists inside them carried the same way, +//! and its `set_block` (simple or weighted states) `horizon:set_block`. +//! +//! What is not here is taken out of its list later and counted +//! (`worldgen::upgrade`). The entity NBT is written in 26.3's shape (an +//! `equipment` map, not 1.21.1's `HandItems` and `ArmorItems`). +use serde_json::{Value, json}; +use std::{ + collections::{BTreeMap, BTreeSet}, + io::{Read, Seek}, +}; + +/// What the processors read from the mod's own data files. +#[derive(Debug, Default)] +pub struct ProcessorData { + /// Repurposed Structures' `rs_spawners/<path>.json` by id: each mob and + /// its weight, without the weight-0 entries its loader removes. + pub spawners: BTreeMap<String, Vec<(String, f64)>>, + /// Moog's "replace vanilla" presets that are on by default: + /// `(mod id, vanilla_key)`. + pub replace_vanilla: BTreeSet<(String, String)>, + /// The mod's processor lists by id, for Lithostitched's references. + pub lists: BTreeMap<String, Value>, +} + +impl ProcessorData { + pub fn read<R: Read + Seek>(zip: &mut zip::ZipArchive<R>) -> Self { + let mut out = Self::default(); + for i in 0..zip.len() { + let Ok(mut entry) = zip.by_index(i) else { + continue; + }; + let name = entry.name().to_owned(); + let Some((ns, rest)) = name + .strip_prefix("data/") + .and_then(|r| r.strip_suffix(".json")) + .and_then(|r| r.split_once('/')) + else { + continue; + }; + let spawners = rest.strip_prefix("rs_spawners/"); + let list = rest.strip_prefix("worldgen/processor_list/"); + if spawners.is_none() && list.is_none() && rest != "moogs_structures/replace_vanilla" { + continue; + } + let mut text = String::new(); + let Ok(v) = entry + .read_to_string(&mut text) + .map_err(|_| ()) + .and_then(|_| serde_json::from_str::<Value>(&text).map_err(|_| ())) + else { + continue; + }; + if let Some(path) = list { + out.lists.insert(format!("{ns}:{path}"), v); + continue; + } + if let Some(path) = spawners { + let mobs = v["mobs"] + .as_array() + .into_iter() + .flatten() + .filter_map(|m| Some((m["name"].as_str()?.to_owned(), m["weight"].as_f64()?))) + .filter(|(_, w)| *w != 0.) + .collect(); + out.spawners.insert(format!("{ns}:{path}"), mobs); + continue; + } + for preset in v["presets"].as_array().into_iter().flatten() { + if preset["default_enabled"].as_bool() != Some(true) { + continue; + } + for r in preset["replacements"].as_array().into_iter().flatten() { + if let Some(key) = r["vanilla_key"].as_str() { + out.replace_vanilla.insert((ns.to_owned(), key.to_owned())); + } + } + } + } + out + } +} + +/// Calls `f` on each processor list in a worldgen file: a `processor_list` +/// file, and the inline lists of a template pool's elements (list elements +/// included). Whether any call changed something. +pub fn each_list(kind: &str, v: &mut Value, f: &mut dyn FnMut(&mut Value) -> bool) -> bool { + match kind { + "processor_list" => f(v), + "template_pool" => { + let mut changed = false; + for e in v["elements"].as_array_mut().into_iter().flatten() { + let element = &mut e["element"]; + if let Some(list) = element.get_mut("elements").and_then(Value::as_array_mut) { + for inner in list { + if let Some(p) = inner.get_mut("processors") { + changed |= f(p); + } + } + } else if let Some(p) = element.get_mut("processors") { + changed |= f(p); + } + } + changed + } + _ => false, + } +} + +/// The processors of a list: `{"processors": [...]}` or a bare list (an id +/// names another file and is left alone). +pub fn processors(list: &mut Value) -> Option<&mut Vec<Value>> { + match list { + Value::Array(a) => Some(a), + Value::Object(o) => o.get_mut("processors").and_then(Value::as_array_mut), + _ => None, + } +} + +/// Rewrites the processors of a worldgen file that have a data form; each is +/// counted by type in `carried`. Whether anything changed. +pub fn carry( + kind: &str, + v: &mut Value, + data: &ProcessorData, + carried: &mut BTreeMap<String, usize>, +) -> bool { + each_list(kind, v, &mut |list| { + let Some(list) = processors(list) else { + return false; + }; + let (out, changed) = carry_all(std::mem::take(list), data, carried); + *list = out; + changed + }) +} + +/// The processors of one list carried: references written in, then each +/// one with a data form replaced by it. Whether anything changed. +fn carry_all( + list: Vec<Value>, + data: &ProcessorData, + carried: &mut BTreeMap<String, usize>, +) -> (Vec<Value>, bool) { + let (expanded, mut changed) = references(list, data, carried, 0); + let mut out = Vec::with_capacity(expanded.len()); + for p in expanded { + let kind = p["processor_type"].as_str().unwrap_or_default().to_owned(); + let replacement = match kind.as_str() { + "lithostitched:apply_random" => apply_random(&p, data, carried).map(|p| vec![p]), + "lithostitched:condition" => condition(&p, data, carried).map(|p| vec![p]), + _ => as_data(&kind, &p, data), + }; + match replacement { + Some(replacement) => { + *carried.entry(kind).or_default() += 1; + out.extend(replacement); + changed = true; + } + None => out.push(p), + } + } + (out, changed) +} + +/// A processor list inside a carried processor: an id stays (the game reads +/// that file, carried on its own); an inline one is carried here. +fn nested(list: &Value, data: &ProcessorData, carried: &mut BTreeMap<String, usize>) -> Value { + let mut list = list.clone(); + if let Some(ps) = processors(&mut list) { + *ps = carry_all(std::mem::take(ps), data, carried).0; + } + list +} + +/// Lithostitched's `apply_random` as `horizon:apply_random`: its weighted +/// lists, or its set of lists at weight 1. A tag is not carried. +fn apply_random( + p: &Value, + data: &ProcessorData, + carried: &mut BTreeMap<String, usize>, +) -> Option<Value> { + let lists = &p["processor_lists"]; + let weighted = lists.as_array().is_some_and(|a| { + a.iter() + .all(|e| e.get("weight").is_some() && e.get("data").is_some()) + }); + let entries: Vec<(Value, Value)> = if weighted { + lists + .as_array()? + .iter() + .map(|e| (e["weight"].clone(), e["data"].clone())) + .collect() + } else { + match lists { + Value::String(id) if !id.starts_with('#') => vec![(1.into(), lists.clone())], + Value::Array(a) if a.iter().any(|x| x.get("processor_type").is_some()) => { + vec![(1.into(), lists.clone())] + } + Value::Array(a) + if a.iter() + .all(|x| x.as_str().is_some_and(|s| !s.starts_with('#')) || x.is_object()) => + { + a.iter().map(|x| (1.into(), x.clone())).collect() + } + Value::Object(_) => vec![(1.into(), lists.clone())], + _ => return None, + } + }; + let lists: Vec<Value> = entries + .into_iter() + .map(|(weight, list)| json!({"weight": weight, "data": nested(&list, data, carried)})) + .collect(); + let mut out = json!({"processor_type": "horizon:apply_random", "processor_lists": lists}); + if let Some(mode) = p.get("mode") { + out["mode"] = mode.clone(); + } + Some(out) +} + +/// Lithostitched's `condition` as `horizon:condition`, when the game runs +/// every condition in it (true, all_of, any_of, not, random_chance, +/// matching_blocks with plain property values); else it stays Java. +fn condition( + p: &Value, + data: &ProcessorData, + carried: &mut BTreeMap<String, usize>, +) -> Option<Value> { + fn runs(c: &Value) -> bool { + if let Some(all) = c.as_array() { + return all.iter().all(runs); + } + let kind = c["type"].as_str().unwrap_or_default(); + match kind.strip_prefix("lithostitched:").unwrap_or(kind) { + "true" | "random_chance" => true, + "all_of" | "any_of" => c["conditions"] + .as_array() + .is_some_and(|a| a.iter().all(runs)), + "not" => runs(&c["condition"]), + "matching_blocks" => c.get("properties").is_none_or(|p| { + p.as_object() + .is_some_and(|o| o.values().all(Value::is_string)) + }), + _ => false, + } + } + if !runs(&p["if_true"]) { + return None; + } + // One processor or a list of them. + let mut branch = |key: &str| -> Value { + let list = match p.get(key) { + None => Vec::new(), + Some(Value::Array(a)) => a.clone(), + Some(one) => vec![one.clone()], + }; + carry_all(list, data, carried).0.into() + }; + let (then, otherwise) = (branch("then"), branch("else")); + let mut out = json!({"processor_type": "horizon:condition", "if_true": p["if_true"], + "then": then, "else": otherwise}); + if let Some(mode) = p.get("random_mode") { + out["random_mode"] = mode.clone(); + } + Some(out) +} + +/// Repurposed Structures' rule tests and the game's types that run them +/// (`horizon_world::worldgen`'s `rule_test` and `structure::processor`). +const RULE_TESTS: [(&str, &str); 3] = [ + ( + "repurposed_structures:matter_phase_rule_test", + "horizon:matter_phase", + ), + ( + "repurposed_structures:piece_origin_axis_aligned_linear_pos_rule_test", + "horizon:piece_origin_axis_aligned_linear_pos", + ), + ( + "repurposed_structures:y_value_pos_rule_test", + "horizon:y_value", + ), +]; + +/// Renames the rule tests written in Java that the game runs under its own +/// type ([`RULE_TESTS`]) in a worldgen file's processor lists; each is +/// counted by type in `carried`. Their fields are the mod's. Whether +/// anything changed. Without this the game, like vanilla, refuses the list +/// and every pool naming it: Repurposed's mineshafts and Nether monument. +pub fn rule_tests(kind: &str, v: &mut Value, carried: &mut BTreeMap<String, usize>) -> bool { + fn rename(v: &mut Value, carried: &mut BTreeMap<String, usize>) -> bool { + match v { + Value::Object(o) => { + let mut changed = false; + if let Some(Value::String(t)) = o.get_mut("predicate_type") + && let Some((old, new)) = RULE_TESTS.iter().find(|(old, _)| t == old) + { + *carried.entry((*old).to_owned()).or_default() += 1; + *t = (*new).to_owned(); + changed = true; + } + o.values_mut().fold(changed, |c, x| rename(x, carried) | c) + } + Value::Array(a) => a.iter_mut().fold(false, |c, x| rename(x, carried) | c), + _ => false, + } + } + each_list(kind, v, &mut |list| rename(list, carried)) +} + +/// Lithostitched's references in a list, each replaced by the processors of +/// the lists it names, in order (references in them too, to a depth of 8). +/// One naming a tag or a list the mod does not have stays, to be dropped. +/// Whether any was replaced. +fn references( + list: Vec<Value>, + data: &ProcessorData, + carried: &mut BTreeMap<String, usize>, + depth: usize, +) -> (Vec<Value>, bool) { + let mut out = Vec::with_capacity(list.len()); + let mut changed = false; + for p in list { + let named = (p["processor_type"] == "lithostitched:reference" && depth < 8) + .then(|| referenced(&p["processor_lists"], data)) + .flatten(); + match named { + Some(inner) => { + *carried.entry("lithostitched:reference".into()).or_default() += 1; + out.extend(references(inner, data, carried, depth + 1).0); + changed = true; + } + None => out.push(p), + } + } + (out, changed) +} + +/// The processors of the lists a reference names: an id, an inline list, or +/// a list of either. +fn referenced(lists: &Value, data: &ProcessorData) -> Option<Vec<Value>> { + let one = |l: &Value| -> Option<Vec<Value>> { + let mut list = match l { + Value::String(id) if !id.starts_with('#') => { + let id = if id.contains(':') { + id.clone() + } else { + format!("minecraft:{id}") + }; + data.lists.get(&id)?.clone() + } + Value::Object(_) | Value::Array(_) => l.clone(), + _ => return None, + }; + processors(&mut list).map(std::mem::take) + }; + match lists { + // A bare list of processors is one inline list; else a list of lists. + Value::Array(a) if a.iter().any(|x| x.get("processor_type").is_some()) => Some(a.clone()), + Value::Array(a) => Some(a.iter().map(one).collect::<Option<Vec<_>>>()?.concat()), + other => one(other), + } +} + +/// The data form of a Java processor: the processors to put in its place +/// (none for a no-op), or `None` when it has none. +fn as_data(kind: &str, p: &Value, data: &ProcessorData) -> Option<Vec<Value>> { + let (ns, _) = kind.split_once(':')?; + Some(match kind { + "moogs_structures:spawner_randomizing_processor" => { + let mobs = p["weighted_entities"] + .as_array()? + .iter() + .map(|e| { + // The entry's NBT, then its id over it. + let mut entity = match &e["nbt"] { + Value::Object(o) => o.clone(), + _ => serde_json::Map::new(), + }; + entity.insert("id".into(), e["entity"].as_str()?.into()); + Some(json!({"entity": entity, "weight": e["weight"].as_f64()?})) + }) + .collect::<Option<Vec<_>>>()?; + vec![spawner_mobs(p, mobs, None)] + } + "repurposed_structures:spawner_randomizing_processor" => { + // A list the mod does not ship falls back to a vanilla dungeon + // mob in the mod; left out here. + let list = data + .spawners + .get(p["rs_spawner_resourcelocation"].as_str()?)?; + let mobs = list + .iter() + .map(|(id, weight)| json!({"entity": {"id": id}, "weight": weight})) + .collect(); + vec![spawner_mobs(p, mobs, repurposed_light(p)?)] + } + "moogs_structures:vanilla_loot_swap_processor" => { + let key = ( + p["modid"].as_str()?.to_owned(), + p["vanilla_key"].as_str()?.to_owned(), + ); + if data.replace_vanilla.contains(&key) { + return None; + } + Vec::new() + } + "moogs_structures:trial_spawner_randomizing_processor" => { + // The configs by id: 26.3 reads them from `trial_spawner/`, the + // folder Moog's loads them from. + let mut nbt = json!({"normal_config": p["normal_config"].as_str()?}); + if let Some(ominous) = p["ominous_config"].as_str() { + nbt["ominous_config"] = ominous.into(); + } + // The state the template was saved with goes. + let saved = [ + "server_data", + "shared_data", + "spawn_data", + "cooldown_end_at_tick", + "next_mob_spawns_at", + ]; + vec![same_state_rule(&TRIAL_SPAWNER_STATES, &saved, |_| { + nbt.clone() + })] + } + "moogs_structures:vault_randomizing_processor" => { + let normal = p["loot_table"].as_str()?; + let config = |ominous: bool| { + let (table, key) = match ominous { + true => ( + p["ominous_loot_table"].as_str().unwrap_or(normal), + p["ominous_key_item"] + .as_str() + .unwrap_or("minecraft:ominous_trial_key"), + ), + false => ( + normal, + p["key_item"].as_str().unwrap_or("minecraft:trial_key"), + ), + }; + // Merged into the vault's config, as the mod puts both in. + json!({"config": {"loot_table": table, "key_item": {"id": key, "count": 1}}}) + }; + vec![same_state_rule( + &VAULT_STATES, + &["server_data", "shared_data"], + |s| config(s.get("ominous") == Some(&"true")), + )] + } + _ if ns == "betterdeserttemples" + && let Some(processor) = desert_temple(kind) => + { + vec![processor] + } + "lithostitched:block_swap" => { + // The same map; the game's swap keeps the properties both have. + p["blocks"].as_object()?; + vec![json!({"processor_type": "horizon:block_swap", "blocks": p["blocks"]})] + } + "lithostitched:set_block" => { + // A simple or weighted state provider, the block entity left as + // it is. + let provider = p["state_provider"]["type"].as_str()?; + let modifier = p["block_entity_modifier"]["type"].as_str(); + let plain = + modifier.is_none_or(|m| m.trim_start_matches("minecraft:") == "passthrough"); + if !plain + || !matches!( + provider, + "minecraft:simple_state_provider" | "minecraft:weighted_state_provider" + ) + { + return None; + } + let mut out = p.clone(); + out["processor_type"] = "horizon:set_block".into(); + if let Some(o) = out.as_object_mut() { + o.remove("block_entity_modifier"); + } + vec![out] + } + "betterdungeons:small_dungeon_chest_processor" => { + // Its config's defaults: one chest at least, two at most, a + // fifth of the others kept until then; ChestBlock covers both. + vec![json!({"processor_type": "horizon:count_cap", + "blocks": ["minecraft:chest", "minecraft:trapped_chest"], + "min": 1, "max": 2, "chance": 0.2f32 as f64, + "replacement": block("minecraft:cave_air")})] + } + _ if ns == "betterdungeons" || ns == "betterdeserttemples" => vec![yung_spawner(kind, p)?], + _ => return None, + }) +} + +/// A block's states, as (block, [(property, values)]). +type States = ( + &'static str, + &'static [(&'static str, &'static [&'static str])], +); +const TRIAL_SPAWNER_STATES: States = ( + "minecraft:trial_spawner", + &[ + ( + "trial_spawner_state", + &[ + "inactive", + "waiting_for_players", + "active", + "waiting_for_reward_ejection", + "ejecting_reward", + "cooldown", + ], + ), + ("ominous", &["false", "true"]), + ], +); +const VAULT_STATES: States = ( + "minecraft:vault", + &[ + ("facing", &["north", "south", "west", "east"]), + ( + "vault_state", + &["inactive", "active", "unlocking", "ejecting"], + ), + ("ominous", &["false", "true"]), + ], +); + +/// Moog's trial spawner and vault processors keep the block, take keys out +/// of its block entity and set others. As vanilla's `rule`: one rule per +/// state of the block, matching it and writing it back, with the game's +/// `horizon:edit` modifier taking out `remove` and merging `nbt(state)` in. +fn same_state_rule( + states: &States, + remove: &[&str], + nbt: impl Fn(&BTreeMap<&str, &str>) -> Value, +) -> Value { + let (block, properties) = states; + let mut all = vec![BTreeMap::new()]; + for (key, values) in properties.iter() { + all = all + .into_iter() + .flat_map(|s| { + values.iter().map(move |v| { + let mut s = s.clone(); + s.insert(*key, *v); + s + }) + }) + .collect(); + } + let rules: Vec<Value> = all + .iter() + .map(|s| { + let state = json!({"id": block, "properties": s}); + json!({ + "input_predicate": {"predicate_type": "minecraft:blockstate_match", "block_state": state}, + "location_predicate": {"predicate_type": "minecraft:always_true"}, + "output_state": state, + "block_entity_modifier": {"type": "horizon:edit", "remove": remove, "data": nbt(s)}, + }) + }) + .collect(); + json!({"processor_type": "minecraft:rule", "rules": rules}) +} + +/// An entity's NBT in 26.3's shape, in place: 1.21.1's `ArmorItems` (feet +/// to head), `HandItems` (main hand, off hand) and their drop chances become +/// `equipment` and `drop_chances` by slot; empty items and the default drop +/// chance (0.085) are left out, as 26.3 saves them. Whether it changed. +pub fn upgrade_entity(entity: &mut Value) -> bool { + let Some(o) = entity.as_object_mut() else { + return false; + }; + const ARMOR: [&str; 4] = ["feet", "legs", "chest", "head"]; + const HANDS: [&str; 2] = ["mainhand", "offhand"]; + let mut changed = false; + for (items, chances, slots) in [ + ("ArmorItems", "ArmorDropChances", &ARMOR[..]), + ("HandItems", "HandDropChances", &HANDS[..]), + ] { + if let Some(Value::Array(list)) = o.remove(items) { + changed = true; + for (slot, item) in slots.iter().zip(list) { + if item.get("id").is_some() { + let equipment = o.entry("equipment").or_insert_with(|| json!({})); + equipment[*slot] = item; + } + } + } + if let Some(Value::Array(list)) = o.remove(chances) { + changed = true; + for (slot, chance) in slots.iter().zip(list) { + if chance.as_f64().is_some_and(|c| (c - 0.085).abs() > 1e-6) { + let drop = o.entry("drop_chances").or_insert_with(|| json!({})); + drop[*slot] = chance; + } + } + } + } + changed +} + +/// A 1.21.1 trial spawner config (`trial_spawner/`) in 26.3's shape, in +/// place: its mobs' NBT ([`upgrade_entity`]). Whether it changed. +pub fn upgrade_trial_spawner(config: &mut Value) -> bool { + let mut changed = false; + for p in config["spawn_potentials"] + .as_array_mut() + .into_iter() + .flatten() + { + changed |= upgrade_entity(&mut p["data"]["entity"]); + } + changed +} + +/// A block state as 26.3 writes it. +fn block(id: &str) -> Value { + json!({"id": id}) +} + +/// Better Desert Temples' placeholder blocks (glass, concrete and wool in +/// the templates) as the mod's processors turn them: the fixed ones as +/// vanilla's `rule`, the random ones as the game's `horizon:pick`, with the +/// mod's bounds on one `nextFloat` (YUNG's BlockStateRandomizer adds its +/// chances up in floats). The ones that read or change the world around +/// (pillars, Diorite's waterlogging) stay dropped. +fn desert_temple(kind: &str) -> Option<Value> { + let swap = |from: &str, to: &str| { + json!({"processor_type": "minecraft:rule", "rules": [{ + "input_predicate": {"predicate_type": "minecraft:block_match", "block": from}, + "location_predicate": {"predicate_type": "minecraft:always_true"}, + "output_state": block(to)}]}) + }; + let pick = |from: &str, picks: Vec<Value>, default: Value| json!({"processor_type": "horizon:pick", "block": from, "picks": picks, "default": default}); + // YUNG's BlockStateRandomizer: an entry wins below the sum of the + // chances so far, summed as floats. + let randomizer = |entries: &[(f32, &str)]| { + let mut bottom = 0f32; + entries + .iter() + .map(|(chance, id)| { + bottom += chance; + json!({"below": bottom as f64, "state": block(id)}) + }) + .collect::<Vec<_>>() + }; + let at = |below: f32, id: &str| json!({"below": below as f64, "state": block(id)}); + // Sandstone stairs, upside down, facing Plane.HORIZONTAL.getRandomDirection. + let stairs = || { + let states: Vec<Value> = ["north", "east", "south", "west"] + .iter() + .map(|facing| { + json!({"id": "minecraft:sandstone_stairs", "properties": { + "facing": facing, "half": "top", "shape": "straight", "waterlogged": "false"}}) + }) + .collect(); + json!({"states": states}) + }; + Some(match kind.strip_prefix("betterdeserttemples:")? { + "blue_concrete_processor" => swap("minecraft:blue_concrete", "minecraft:blue_terracotta"), + "yellow_concrete_processor" => { + swap("minecraft:yellow_concrete", "minecraft:yellow_terracotta") + } + "red_wool_processor" => swap("minecraft:red_wool", "minecraft:tnt"), + "white_stained_glass_processor" => pick( + "minecraft:white_stained_glass", + randomizer(&[(0.4, "minecraft:cobweb")]), + json!({"state": block("minecraft:air")}), + ), + "polished_diorite_processor" => pick( + "minecraft:polished_diorite", + randomizer(&[(0.6, "minecraft:cut_sandstone")]), + json!({"state": block("minecraft:sandstone")}), + ), + "orange_stained_glass_processor" => { + let mut sus = at(0.01, "minecraft:suspicious_sand"); + sus["nbt"] = json!({"LootTable": "minecraft:archaeology/desert_pyramid"}); + let mut stairs = stairs(); + stairs["below"] = (0.45f32 as f64).into(); + pick( + "minecraft:orange_stained_glass", + vec![ + sus, + at(0.1, "minecraft:sand"), + at(0.2, "minecraft:sandstone"), + stairs, + ], + json!({"state": block("minecraft:cut_sandstone")}), + ) + } + "red_stained_glass_processor" => pick( + "minecraft:red_stained_glass", + vec![at(0.3, "minecraft:sand"), at(0.75, "minecraft:sandstone")], + stairs(), + ), + "pot_processor" => { + // Four sherds, one float each; the pot back to its default state. + let values: Vec<Value> = [ + (0.05f32, "archer"), + (0.1, "miner"), + (0.15, "prize"), + (0.2, "skull"), + ] + .iter() + .map(|(below, sherd)| { + json!({"below": *below as f64, "value": format!("minecraft:{sherd}_pottery_sherd")}) + }) + .collect(); + pick( + "minecraft:decorated_pot", + Vec::new(), + json!({"state": block("minecraft:decorated_pot"), "lists": [{ + "key": "sherds", "count": 4, "values": values, "default": "minecraft:brick"}]}), + ) + } + _ => return None, + }) +} + +/// [`as_data`]'s `horizon:spawner_mobs` for the mods' spawner randomizing +/// processors, whose fields have the same names and defaults. +fn spawner_mobs(p: &Value, mobs: Vec<Value>, rules: Option<Value>) -> Value { + let mut out = json!({"processor_type": "horizon:spawner_mobs", "mobs": mobs}); + for key in [ + "delay", + "min_spawn_delay", + "max_spawn_delay", + "spawn_count", + "max_nearby_entities", + "required_player_range", + "spawn_range", + ] { + if let Some(n) = p.get(key).filter(|n| n.is_i64()) { + out[key] = n.clone(); + } + } + if let Some(r) = rules { + out["custom_spawn_rules"] = r; + } + if let Some(b) = p.get("spawner_replacement_block") { + out["replacement"] = b.clone(); + } + out +} + +/// Repurposed Structures' `custom_spawn_rules` from its light level ranges: +/// `Some(None)` without either, `None` when one does not read. The sky +/// light's upper bound is written as `max_exclusive`, as the mod does. +fn repurposed_light(p: &Value) -> Option<Option<Value>> { + let range = |key: &str| -> Option<Option<(i64, i64)>> { + match p.get(key) { + None => Some(None), + Some(Value::Number(n)) => n.as_i64().map(|n| Some((n, n))), + Some(v) => Some(Some(( + v["min_inclusive"].as_i64()?, + v["max_inclusive"].as_i64()?, + ))), + } + }; + let (block, sky) = ( + range("valid_block_light_level")?, + range("valid_sky_light_level")?, + ); + if block.is_none() && sky.is_none() { + return Some(None); + } + let mut rules = json!({}); + if let Some((min, max)) = block { + rules["block_light_limit"] = json!({"min_inclusive": min, "max_inclusive": max}); + } + if let Some((min, max)) = sky { + rules["sky_light_limit"] = json!({"min_inclusive": min, "max_exclusive": max}); + } + Some(Some(rules)) +} + +/// An item as 26.3 saves it. +fn item(id: &str) -> Value { + json!({"id": format!("minecraft:{id}"), "count": 1}) +} + +/// YUNG's processors that make a spawner of a fixed mob (YUNG's API's +/// `MobSpawnerData`), as vanilla's `rule`: every `input` block becomes a +/// spawner with that NBT. +fn yung_spawner(kind: &str, p: &Value) -> Option<Value> { + const SPAWNER: &str = "minecraft:spawner"; + // (input block, mob, fields other than the defaults, whether the mob is + // also the only potential, its equipment) + let (input, mob, fields, potential, equipment): (_, _, &[(&str, i64)], _, _) = match kind { + "betterdungeons:mob_spawner_processor" => { + (SPAWNER, p["spawner_mob"].as_str()?, &[], true, None) + } + "betterdungeons:zombie_mob_spawner_processor" => ( + SPAWNER, + "minecraft:zombie", + &[("MaxNearbyEntities", 8)], + true, + None, + ), + "betterdungeons:skeleton_mob_spawner_processor" => ( + SPAWNER, + "minecraft:skeleton", + &[ + ("RequiredPlayerRange", 18), + ("MaxNearbyEntities", 8), + ("MaxSpawnDelay", 650), + ], + true, + None, + ), + "betterdungeons:small_nether_dungeon_mob_spawner_processor" => { + let mob = p["spawner_mob"].as_str()?; + // Wither skeletons get netherite armor that never drops and a + // stone sword (with the mod's default config, they and blazes + // keep their loot). + let armor = (mob == "minecraft:wither_skeleton").then(|| { + json!({ + "equipment": { + "feet": item("netherite_boots"), + "legs": item("netherite_leggings"), + "chest": item("netherite_chestplate"), + "head": item("netherite_helmet"), + "mainhand": item("stone_sword"), + }, + "drop_chances": {"feet": 0.0, "legs": 0.0, "chest": 0.0, "head": 0.0}, + }) + }); + (SPAWNER, mob, &[], true, armor) + } + "betterdungeons:zombie_tombstone_spawner_processor" => ( + "minecraft:black_stained_glass", + "minecraft:skeleton", + &[], + false, + Some(json!({"equipment": {"mainhand": item("iron_sword")}})), + ), + "betterdeserttemples:gravel_processor" => ( + "minecraft:gravel", + "minecraft:husk", + &[("MaxNearbyEntities", 8), ("RequiredPlayerRange", 24)], + false, + None, + ), + // An empty hand list in 1.21.1: the skeletons spawn without a bow, + // since a spawn entry with more than its id is not finalized. + "betterdeserttemples:bone_block_processor" => ( + "minecraft:bone_block", + "minecraft:skeleton", + &[("RequiredPlayerRange", 32)], + false, + Some(json!({"equipment": {}})), + ), + "betterdeserttemples:infested_cracked_stone_bricks_processor" => ( + "minecraft:infested_cracked_stone_bricks", + "minecraft:silverfish", + &[("RequiredPlayerRange", 24)], + false, + None, + ), + // 1.21.1 ignores the items' old `Count` and `tag` (the shield's + // pattern, the boots' dye): plain items. + "betterdeserttemples:yellow_wool_processor" => ( + "minecraft:yellow_wool", + "minecraft:husk", + &[("MaxNearbyEntities", 8), ("RequiredPlayerRange", 24)], + false, + Some(json!({"equipment": { + "mainhand": item("golden_sword"), + "offhand": item("shield"), + "feet": item("leather_boots"), + "legs": item("leather_leggings"), + "chest": item("golden_chestplate"), + "head": item("chainmail_helmet"), + }})), + ), + _ => return None, + }; + let mut entity = json!({"id": mob}); + if let Some(Value::Object(extra)) = equipment { + entity.as_object_mut()?.extend(extra); + } + let potentials = match potential { + true => json!([{"data": {"entity": entity.clone()}, "weight": 1}]), + false => json!([]), + }; + let mut nbt = json!({ + "Delay": 20, "MinSpawnDelay": 200, "MaxSpawnDelay": 800, "SpawnCount": 4, + "MaxNearbyEntities": 6, "RequiredPlayerRange": 16, "SpawnRange": 4, + "SpawnData": {"entity": entity}, + "SpawnPotentials": potentials, + }); + for (key, value) in fields { + nbt[*key] = (*value).into(); + } + Some(json!({ + "processor_type": "minecraft:rule", + "rules": [{ + "input_predicate": {"predicate_type": "minecraft:block_match", "block": input}, + "location_predicate": {"predicate_type": "minecraft:always_true"}, + "output_state": {"Name": SPAWNER}, + "block_entity_modifier": {"type": "minecraft:append_static", "data": nbt}, + }], + })) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn data() -> ProcessorData { + let mut d = ProcessorData::default(); + d.spawners.insert( + "repurposed_structures:dungeons/badlands".into(), + vec![ + ("minecraft:husk".into(), 10.), + ("minecraft:cave_spider".into(), 3.), + ], + ); + d.replace_vanilla.insert(("mvs".into(), "village".into())); + d + } + + #[test] + fn spawner_processors_become_data() { + let mut list = json!({"processors": [ + {"processor_type": "moogs_structures:spawner_randomizing_processor", + "weighted_entities": [{"entity": "minecraft:blaze", "weight": 2, "nbt": {"Health": 5}}], + "spawn_count": 2, "spawner_replacement_block": {"Name": "minecraft:stone"}}, + {"processor_type": "repurposed_structures:spawner_randomizing_processor", + "rs_spawner_resourcelocation": "repurposed_structures:dungeons/badlands", + "valid_block_light_level": {"min_inclusive": 0, "max_inclusive": 7}}, + {"processor_type": "repurposed_structures:spawner_randomizing_processor", + "rs_spawner_resourcelocation": "repurposed_structures:missing"}, + {"processor_type": "minecraft:gravity", "heightmap": "WORLD_SURFACE_WG", "offset": 0}, + {"processor_type": "moogs_structures:vanilla_loot_swap_processor", + "modid": "mns", "vanilla_key": "fortress", "loot_table_mapping": {}}, + {"processor_type": "moogs_structures:vanilla_loot_swap_processor", + "modid": "mvs", "vanilla_key": "village", "loot_table_mapping": {}}]}); + let mut carried = BTreeMap::new(); + assert!(carry("processor_list", &mut list, &data(), &mut carried)); + let p = list["processors"].as_array().unwrap(); + assert_eq!(p.len(), 5, "{p:#?}"); + assert_eq!( + p[0], + json!({"processor_type": "horizon:spawner_mobs", + "mobs": [{"entity": {"Health": 5, "id": "minecraft:blaze"}, "weight": 2.0}], + "spawn_count": 2, "replacement": {"Name": "minecraft:stone"}}) + ); + assert_eq!( + p[1], + json!({"processor_type": "horizon:spawner_mobs", + "mobs": [{"entity": {"id": "minecraft:husk"}, "weight": 10.0}, + {"entity": {"id": "minecraft:cave_spider"}, "weight": 3.0}], + "custom_spawn_rules": {"block_light_limit": {"min_inclusive": 0, "max_inclusive": 7}}}) + ); + // Unknown list, vanilla processor, and a swap whose preset is on: kept + // (the Java ones are dropped later); the swap that is off is gone. + assert_eq!( + p[2]["rs_spawner_resourcelocation"], + "repurposed_structures:missing" + ); + assert_eq!(p[3]["processor_type"], "minecraft:gravity"); + assert_eq!(p[4]["modid"], "mvs"); + assert_eq!( + carried, + BTreeMap::from([ + ("moogs_structures:spawner_randomizing_processor".into(), 1), + ("moogs_structures:vanilla_loot_swap_processor".into(), 1), + ( + "repurposed_structures:spawner_randomizing_processor".into(), + 1 + ), + ]) + ); + } + + #[test] + fn yungs_spawners_become_rules() { + let mut pool = json!({"elements": [{"element": {"processors": {"processors": [ + {"processor_type": "betterdungeons:skeleton_mob_spawner_processor"}, + {"processor_type": "betterdeserttemples:bone_block_processor"}, + {"processor_type": "betterdungeons:small_nether_dungeon_mob_spawner_processor", + "spawner_mob": "minecraft:wither_skeleton"}, + {"processor_type": "betterdungeons:candle_processor"}]}}}]}); + let mut carried = BTreeMap::new(); + assert!(carry("template_pool", &mut pool, &data(), &mut carried)); + let p = &pool["elements"][0]["element"]["processors"]["processors"]; + let rule = |i: usize| &p[i]["rules"][0]; + assert_eq!(rule(0)["input_predicate"]["block"], "minecraft:spawner"); + assert_eq!( + rule(0)["output_state"], + json!({"Name": "minecraft:spawner"}) + ); + let nbt = &rule(0)["block_entity_modifier"]["data"]; + assert_eq!( + ( + nbt["RequiredPlayerRange"].clone(), + nbt["MaxNearbyEntities"].clone() + ), + (json!(18), json!(8)) + ); + assert_eq!(nbt["MaxSpawnDelay"], 650); + assert_eq!( + nbt["SpawnPotentials"], + json!([{"data": {"entity": {"id": "minecraft:skeleton"}}, "weight": 1}]) + ); + // Bone blocks become bowless skeleton spawners with no potentials. + let bone = &rule(1)["block_entity_modifier"]["data"]; + assert_eq!(rule(1)["input_predicate"]["block"], "minecraft:bone_block"); + assert_eq!( + bone["SpawnData"], + json!({"entity": {"id": "minecraft:skeleton", "equipment": {}}}) + ); + assert_eq!(bone["SpawnPotentials"], json!([])); + let wither = &rule(2)["block_entity_modifier"]["data"]["SpawnData"]["entity"]; + assert_eq!( + wither["equipment"]["head"]["id"], + "minecraft:netherite_helmet" + ); + assert_eq!(wither["drop_chances"]["feet"], 0.0); + assert_eq!(p[3]["processor_type"], "betterdungeons:candle_processor"); + assert_eq!(carried.values().sum::<usize>(), 3); + } + + #[test] + fn trial_spawners_and_vaults_drop_their_saved_state() { + let mut list = json!({"processors": [ + {"processor_type": "moogs_structures:trial_spawner_randomizing_processor", + "normal_config": "mns:arena/normal", "ominous_config": "mns:arena/ominous"}, + {"processor_type": "moogs_structures:vault_randomizing_processor", + "loot_table": "mns:vaults/main", "ominous_loot_table": "mns:vaults/ominous"}]}); + let mut carried = BTreeMap::new(); + assert!(carry("processor_list", &mut list, &data(), &mut carried)); + let p = list["processors"].as_array().unwrap(); + let rules = |i: usize| p[i]["rules"].as_array().unwrap().clone(); + // Every state of the block: 6 x 2 trial spawners, 4 x 4 x 2 vaults. + assert_eq!((rules(0).len(), rules(1).len()), (12, 32)); + let spawner = &rules(0)[0]; + assert_eq!( + spawner["input_predicate"]["block_state"], + spawner["output_state"] + ); + assert_eq!( + spawner["block_entity_modifier"], + json!({"type": "horizon:edit", + "remove": ["server_data", "shared_data", "spawn_data", + "cooldown_end_at_tick", "next_mob_spawns_at"], + "data": {"normal_config": "mns:arena/normal", + "ominous_config": "mns:arena/ominous"}}) + ); + for rule in rules(1) { + let ominous = rule["output_state"]["properties"]["ominous"] == "true"; + let (table, key) = match ominous { + true => ("mns:vaults/ominous", "minecraft:ominous_trial_key"), + false => ("mns:vaults/main", "minecraft:trial_key"), + }; + assert_eq!( + rule["block_entity_modifier"], + json!({"type": "horizon:edit", "remove": ["server_data", "shared_data"], + "data": {"config": {"loot_table": table, + "key_item": {"id": key, "count": 1}}}}) + ); + } + assert_eq!(carried.len(), 2); + } + + #[test] + fn trial_spawner_mobs_take_26_3s_equipment() { + let mut config = json!({"spawn_potentials": [{"weight": 1, "data": {"entity": { + "id": "minecraft:skeleton", + "ArmorItems": [{}, {}, {}, {"id": "minecraft:iron_helmet", "count": 1}], + "ArmorDropChances": [0.085, 0.085, 0.085, 0.0], + "HandItems": [{"id": "minecraft:bow", "count": 1}, {}], + "HandDropChances": [0.085, 0.085]}}}]}); + assert!(upgrade_trial_spawner(&mut config)); + assert_eq!( + config["spawn_potentials"][0]["data"]["entity"], + json!({"id": "minecraft:skeleton", + "equipment": {"head": {"id": "minecraft:iron_helmet", "count": 1}, + "mainhand": {"id": "minecraft:bow", "count": 1}}, + "drop_chances": {"head": 0.0}}) + ); + assert!(!upgrade_trial_spawner(&mut config)); + } + + #[test] + fn desert_temple_placeholders_become_rules_and_picks() { + let mut list = json!({"processors": [ + {"processor_type": "betterdeserttemples:red_wool_processor"}, + {"processor_type": "betterdeserttemples:white_stained_glass_processor"}, + {"processor_type": "betterdeserttemples:orange_stained_glass_processor"}, + {"processor_type": "betterdeserttemples:pot_processor"}, + {"processor_type": "betterdeserttemples:quartz_pillar_processor"}]}); + let mut carried = BTreeMap::new(); + assert!(carry("processor_list", &mut list, &data(), &mut carried)); + let p = list["processors"].as_array().unwrap(); + assert_eq!( + p[0]["rules"][0]["output_state"], + json!({"id": "minecraft:tnt"}) + ); + assert_eq!(p[1]["processor_type"], "horizon:pick"); + assert_eq!(p[1]["picks"][0]["below"], 0.4f32 as f64); + assert_eq!(p[1]["default"]["state"]["id"], "minecraft:air"); + let glass = &p[2]["picks"]; + assert_eq!(glass[0]["state"]["id"], "minecraft:suspicious_sand"); + assert_eq!( + glass[0]["nbt"]["LootTable"], + "minecraft:archaeology/desert_pyramid" + ); + assert_eq!(glass[3]["states"].as_array().unwrap().len(), 4); + assert_eq!(glass[3]["below"], 0.45f32 as f64); + assert_eq!(p[3]["default"]["lists"][0]["count"], 4); + // The pillar reads the world below: still Java, still dropped later. + assert_eq!( + p[4]["processor_type"], + "betterdeserttemples:quartz_pillar_processor" + ); + assert_eq!(carried.values().sum::<usize>(), 4); + } + + #[test] + fn small_dungeon_chests_are_capped() { + let mut list = json!({"processors": [ + {"processor_type": "betterdungeons:small_dungeon_chest_processor"}]}); + let mut carried = BTreeMap::new(); + assert!(carry("processor_list", &mut list, &data(), &mut carried)); + let cap = &list["processors"][0]; + assert_eq!(cap["processor_type"], "horizon:count_cap"); + assert_eq!( + (cap["min"].as_u64(), cap["max"].as_u64()), + (Some(1), Some(2)) + ); + assert_eq!(cap["replacement"], json!({"id": "minecraft:cave_air"})); + } + + #[test] + fn lithostitched_block_swaps_become_the_games() { + let mut list = json!({"processors": [ + {"processor_type": "lithostitched:block_swap", + "blocks": {"minecraft:cobblestone": "minecraft:mossy_cobblestone"}}, + {"processor_type": "lithostitched:block_swap", "blocks": []}]}); + let mut carried = BTreeMap::new(); + assert!(carry("processor_list", &mut list, &data(), &mut carried)); + let p = list["processors"].as_array().unwrap(); + assert_eq!( + p[0], + json!({"processor_type": "horizon:block_swap", + "blocks": {"minecraft:cobblestone": "minecraft:mossy_cobblestone"}}) + ); + // A map that is not one stays Java (dropped later). + assert_eq!(p[1]["processor_type"], "lithostitched:block_swap"); + } + + #[test] + fn lithostitched_references_are_written_in() { + let mut data = data(); + let gravity = json!({"processor_type": "minecraft:gravity", "heightmap": "WORLD_SURFACE_WG", "offset": 0}); + data.lists + .insert("v:road".into(), json!({"processors": [gravity.clone()]})); + data.lists.insert( + "v:house".into(), + json!({"processors": [ + {"processor_type": "lithostitched:reference", "processor_lists": "v:road"}, + {"processor_type": "lithostitched:block_swap", "blocks": {"minecraft:dirt": "minecraft:grass_block"}}]}), + ); + let mut list = json!({"processors": [ + {"processor_type": "lithostitched:reference", "processor_lists": ["v:house", "v:road"]}, + {"processor_type": "lithostitched:reference", "processor_lists": "#v:roads"}, + {"processor_type": "lithostitched:reference", "processor_lists": ["v:nowhere"]}]}); + let mut carried = BTreeMap::new(); + assert!(carry("processor_list", &mut list, &data, &mut carried)); + let p = list["processors"].as_array().unwrap(); + // House (its own reference to road first, then its swap), then road; + // the swap inside is carried too. + assert_eq!(p[0], gravity); + assert_eq!(p[1]["processor_type"], "horizon:block_swap"); + assert_eq!(p[2], gravity); + // A tag or a list the mod lacks stays, to be dropped. + assert_eq!(p[3]["processor_lists"], "#v:roads"); + assert_eq!(p[4]["processor_lists"], json!(["v:nowhere"])); + assert_eq!(carried["lithostitched:reference"], 2); + } + + #[test] + fn lithostitched_random_and_condition_become_the_games() { + let mut data = data(); + let swap = json!({"processor_type": "lithostitched:block_swap", + "blocks": {"minecraft:dirt": "minecraft:grass_block"}}); + data.lists + .insert("v:green".into(), json!({"processors": [swap.clone()]})); + let mut list = json!({"processors": [ + {"processor_type": "lithostitched:apply_random", "mode": "per_piece", "processor_lists": [ + {"weight": 3, "data": "minecraft:empty"}, + {"weight": 1, "data": {"processors": [ + {"processor_type": "lithostitched:reference", "processor_lists": "v:green"}, + {"processor_type": "v:java"}]}}]}, + {"processor_type": "lithostitched:apply_random", "mode": "per_block", + "processor_lists": ["v:green", "v:red"]}, + {"processor_type": "lithostitched:apply_random", "mode": "per_block", + "processor_lists": "#v:colours"}, + {"processor_type": "lithostitched:condition", + "if_true": [{"type": "lithostitched:matching_blocks", "blocks": "minecraft:cobblestone"}], + "then": swap, "else": {"processor_type": "lithostitched:set_block", "state_provider": + {"type": "minecraft:simple_state_provider", "state": {"Name": "minecraft:stone"}}}}, + {"processor_type": "lithostitched:condition", + "if_true": {"type": "lithostitched:matching_biomes", "biomes": "#c:is_cold"}, + "then": []}]}); + let mut carried = BTreeMap::new(); + assert!(carry("processor_list", &mut list, &data, &mut carried)); + let p = list["processors"].as_array().unwrap(); + let green = json!({"processor_type": "horizon:block_swap", + "blocks": {"minecraft:dirt": "minecraft:grass_block"}}); + // Inline lists are carried; Java left in them is dropped later. + assert_eq!( + p[0], + json!({"processor_type": "horizon:apply_random", "mode": "per_piece", "processor_lists": [ + {"weight": 3, "data": "minecraft:empty"}, + {"weight": 1, "data": {"processors": [green.clone(), {"processor_type": "v:java"}]}}]}) + ); + assert_eq!( + p[1]["processor_lists"], + json!([{"weight": 1, "data": "v:green"}, {"weight": 1, "data": "v:red"}]) + ); + assert_eq!(p[2]["processor_type"], "lithostitched:apply_random"); + assert_eq!( + p[3], + json!({"processor_type": "horizon:condition", + "if_true": [{"type": "lithostitched:matching_blocks", "blocks": "minecraft:cobblestone"}], + "then": [green], "else": [{"processor_type": "horizon:set_block", "state_provider": + {"type": "minecraft:simple_state_provider", "state": {"Name": "minecraft:stone"}}}]}) + ); + assert_eq!(p[4]["processor_type"], "lithostitched:condition"); + assert_eq!(carried["lithostitched:apply_random"], 2); + assert_eq!(carried["lithostitched:condition"], 1); + } + + #[test] + fn repurposed_rule_tests_become_the_games() { + let rule = |input: Value, position: Value| { + json!({"input_predicate": input, "location_predicate": {"predicate_type": + "repurposed_structures:matter_phase_rule_test", "phase_to_test_for": "AIR"}, + "position_predicate": position, "output_state": {"id": "minecraft:lava"}}) + }; + let mut pool = json!({"elements": [{"weight": 1, "element": { + "element_type": "minecraft:single_pool_element", "processors": {"processors": [ + {"processor_type": "minecraft:rule", "rules": [rule( + json!({"predicate_type": "minecraft:always_true"}), + json!({"predicate_type": "repurposed_structures:y_value_pos_rule_test", + "min_y_value": 0, "max_y_value": 31}))]}, + {"processor_type": "minecraft:capped", "limit": 2, "delegate": { + "processor_type": "minecraft:rule", "rules": [rule( + json!({"predicate_type": "minecraft:block_match", "block": "minecraft:rail"}), + json!({"predicate_type": + "repurposed_structures:piece_origin_axis_aligned_linear_pos_rule_test", + "axis": "y", "min_dist": 0, "max_dist": 1}))]}}]}}}]}); + let mut carried = BTreeMap::new(); + assert!(rule_tests("template_pool", &mut pool, &mut carried)); + let list = &pool["elements"][0]["element"]["processors"]["processors"]; + let first = &list[0]["rules"][0]; + assert_eq!( + first["location_predicate"]["predicate_type"], + "horizon:matter_phase" + ); + assert_eq!(first["location_predicate"]["phase_to_test_for"], "AIR"); + assert_eq!( + first["position_predicate"]["predicate_type"], + "horizon:y_value" + ); + assert_eq!(first["position_predicate"]["max_y_value"], 31); + let delegated = &list[1]["delegate"]["rules"][0]; + assert_eq!( + delegated["position_predicate"]["predicate_type"], + "horizon:piece_origin_axis_aligned_linear_pos" + ); + assert_eq!( + delegated["input_predicate"]["predicate_type"], + "minecraft:block_match" + ); + assert_eq!(carried.values().sum::<usize>(), 4); + assert_eq!(carried["repurposed_structures:matter_phase_rule_test"], 2); + // A file holding none is left as it is. + assert!(!rule_tests("template_pool", &mut pool, &mut carried)); + assert!(!rule_tests( + "structure", + &mut json!({"predicate_type": + "repurposed_structures:y_value_pos_rule_test"}), + &mut carried + )); + } +} diff --git a/crates/horizon-port-pack/src/report.rs b/crates/horizon-port-pack/src/report.rs index ffa025c79..a98dd0432 100644 --- a/crates/horizon-port-pack/src/report.rs +++ b/crates/horizon-port-pack/src/report.rs @@ -83,7 +83,7 @@ pub fn license_kind(license: &str) -> &'static str { /// What still blocks a mod of this tier that has not been carried over. fn not_yet(tier: &str) -> &'static str { match tier { - "data" => "data tier: carry-over only, no agent; its jar is not in the work folder", + "data" => "data tier: carry-over only, no agent; its jar is not here yet (fetch gets it)", "logic" => { "needs mod API 0.3: behaviour callbacks (use, place, break, ticks) and server events" } diff --git a/crates/horizon-port-pack/src/worldgen.rs b/crates/horizon-port-pack/src/worldgen.rs new file mode 100644 index 000000000..7b8f36b65 --- /dev/null +++ b/crates/horizon-port-pack/src/worldgen.rs @@ -0,0 +1,1776 @@ +//! Structure mods' worldgen, carried over: block and fluid states in 26.3's +//! shape, the structure, placement and pool element types of the common +//! structure libraries rewritten as vanilla's, and the loader's convention +//! biome tags (`c:`) the structures name. +//! +//! The libraries' types add rules on top of vanilla's. The start and +//! placement rules of Repurposed Structures' and Moog's jigsaws (no start in +//! liquid, biomes and terrain height in a radius, a minimum distance from +//! the world origin, sets to keep away from) are written under `horizon:` +//! keys the game reads (`horizon_world::worldgen::structure::start_checks`), +//! so those structures start where the mod would. Rules that change how the +//! pieces are laid out (piece caps, burying, clipping) are lost and counted +//! by name. Each rewrite is counted in the carry report, and every type left +//! as it is is listed, so the coverage report can say what generates and how +//! closely. +use serde_json::Value; +use std::{ + collections::{BTreeMap, BTreeSet}, + io::Read, + path::{Path, PathBuf}, +}; + +/// Structure types that are vanilla's jigsaw with extra start rules. +const JIGSAWS: [&str; 3] = [ + "lithostitched:jigsaw", + "moogs_structures:moogs_structures_generic_jigsaw_structure", + "repurposed_structures:generic_jigsaw_structure", +]; +/// The Nether variants of the piece-limited jigsaws: laid out the same, +/// then moved onto land. +const NETHER_JIGSAWS: [&str; 2] = [ + "moogs_structures:moogs_structures_generic_nether_jigsaw_structure", + "repurposed_structures:generic_nether_jigsaw_structure", +]; +/// Repurposed Structures' generic jigsaws with a start check of their own: the +/// `repurposed` key of `horizon:start_checks` names it. +const REPURPOSED_CHECKED: [(&str, &str); 3] = [ + ("repurposed_structures:city_nether_structure", "city_nether"), + ( + "repurposed_structures:shipwreck_nether_structure", + "shipwreck_nether", + ), + ("repurposed_structures:stronghold_end", "stronghold_end"), +]; +/// Repurposed Structures' mineshaft: its generic jigsaw laid out under the +/// terrain (`"layout": "mineshaft"` in `horizon:piece_limited`). +const REPURPOSED_MINESHAFT: &str = "repurposed_structures:generic_mineshaft"; +/// Layout rules the piece-limited assembler keeps (`horizon:piece_limited`). +const PIECE_LIMITED_RULES: [&str; 4] = [ + "y_allowance", + "burying_type", + "use_bounding_box_hack", + "pools_that_ignore_boundaries", +]; +/// Placements that are vanilla's random spread with extra rules. +const SPREADS: [&str; 2] = [ + "moogs_structures:advanced_random_spread", + "repurposed_structures:advanced_random_spread", +]; +/// YUNG's placements: vanilla's random spread with an enhanced exclusion +/// zone (several sets), plus, for the temples, no start near an ocean or a +/// river, and for strongholds, starts only in odd rings around the origin. +const YUNG_SPREADS: [&str; 4] = [ + "yungsapi:enhanced_random_spread", + "betterdeserttemples:desert_temple", + "betterjungletemples:jungle_temple", + "betterstrongholds:stronghold", +]; +/// YUNG's structures and the vanilla structure type each one's mod turns +/// off (its DisableVanilla*Mixin, on by default in each mod's config). +const YUNG_REPLACES: [(&str, &str); 7] = [ + ( + "betterdeserttemples:desert_temple", + "minecraft:desert_pyramid", + ), + ( + "betterjungletemples:jungle_temple", + "minecraft:jungle_temple", + ), + ("betterwitchhuts:witch_hut", "minecraft:swamp_hut"), + ( + "betteroceanmonuments:ocean_monument", + "minecraft:ocean_monument", + ), + ("betterstrongholds:stronghold", "minecraft:stronghold"), + ("betterfortresses:fortress", "minecraft:fortress"), + ("bettermineshafts:mineshaft_", "minecraft:mineshaft"), +]; +/// Moog's Structure Lib spreads its structures 1.65 times as far apart as +/// its files say (AdvancedRandomSpread's constructor). +const MOOGS_SPACING: f64 = 1.65; +/// The jigsaws' start rules and the `horizon:start_checks` key each maps to. +const START_RULES: [(&str, &str); 3] = [ + ("valid_biome_radius_check", "biome_radius"), + ("terrain_height_radius_check", "terrain_radius"), + ("allowed_terrain_height_range", "terrain_range"), +]; +/// The jigsaws' rules on laying out pieces, which vanilla's jigsaw has no +/// place for. +const LAYOUT_RULES: [&str; 4] = [ + "y_allowance", + "burying_type", + "use_bounding_box_hack", + "pools_that_ignore_boundaries", +]; +/// 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`. +const DEAD_FIELDS: [&str; 1] = ["vertical_distance_from_start_piece"]; +/// Pool elements that are vanilla's with extra fields: (mod type, vanilla type). +const ELEMENTS: [(&str, &str); 3] = [ + ( + "moogs_structures:versioned_single_pool_element", + "minecraft:single_pool_element", + ), + ( + "repurposed_structures:legacy_ocean_bottom_single_pool_element", + "minecraft:legacy_single_pool_element", + ), + ("empty_pool_element", "minecraft:empty_pool_element"), +]; + +/// What carry-over did to a mod's worldgen files. +#[derive(Debug, Default, serde::Serialize)] +pub struct Upgrade { + /// Files rewritten to vanilla types. + pub mapped: usize, + /// Files whose block and fluid states were written in 26.3's shape. + pub block_states: usize, + /// Types left as they are (Horizon skips what it cannot run), by type. + pub unsupported: BTreeMap<String, usize>, + /// Rules of mapped types the game does not run, by field. + pub rules_lost: BTreeMap<String, usize>, + /// Processors written in Java, taken out of their lists, by type. + pub processors_dropped: BTreeMap<String, usize>, + /// Processors written in Java carried as data the game runs + /// ([`crate::processors`]), by type. + pub processors_carried: BTreeMap<String, usize>, + /// Rule tests written in Java carried as the game's own + /// ([`crate::processors::rule_tests`]), by type. + pub rule_tests_carried: BTreeMap<String, usize>, + /// Entries added to other structure sets and template pools + /// (`horizon:add_to`), by the set or pool they join. + pub added_to: BTreeMap<String, usize>, + /// Lithostitched worldgen modifiers not carried: by type, `conditions` + /// when they need a mod that is not ported along, `target` when they + /// name their sets or pools by a tag. + pub modifiers_left: BTreeMap<String, usize>, +} + +/// Where a mod's additions to other sets and pools go in its carry: +/// `data/<id>/worldgen/<kind>/horizon_added/<name>.json`. +pub const ADDED: &str = "horizon_added"; + +/// A lithostitched worldgen modifier +/// (`data/<ns>/lithostitched/worldgen_modifier/`) that adds to structure +/// sets or template pools, as a file the game merges into them (the key +/// `horizon:add_to`, `horizon_client::mod_content`): (kind, file). The +/// modifier's conditions are checked against `loaded`, the mods ported +/// along: a compat addition for a mod that is not would place that mod's +/// blocks, which the game does not have. Other modifier types are left out. +/// Both are counted in `report`. +pub fn modifier( + v: &Value, + loaded: &dyn Fn(&str) -> bool, + report: &mut Upgrade, +) -> Option<(&'static str, Value)> { + let t = v["type"].as_str().unwrap_or_default(); + let mut left = |why: &str| *report.modifiers_left.entry(why.to_owned()).or_default() += 1; + let (kind, targets, from, to) = match t { + "lithostitched:add_structure_set_entries" => { + ("structure_set", "structure_sets", "entries", "structures") + } + "lithostitched:add_template_pool_elements" => { + ("template_pool", "template_pools", "elements", "elements") + } + _ => { + left(t); + return None; + } + }; + if !conditions_hold(v, loaded) { + left("conditions"); + return None; + } + let targets: Option<Vec<&str>> = match &v[targets] { + Value::String(s) => Some(vec![s.as_str()]), + Value::Array(a) => a.iter().map(Value::as_str).collect(), + _ => None, + }; + let Some(targets) = targets.filter(|t| !t.is_empty() && t.iter().all(|t| !t.starts_with('#'))) + else { + left("target"); + return None; + }; + let entries = v[from].as_array().cloned().unwrap_or_default(); + for target in &targets { + *report.added_to.entry((*target).to_owned()).or_default() += entries.len(); + } + Some(( + kind, + serde_json::json!({"horizon:add_to": targets, to: entries}), + )) +} + +/// Whether a modifier's load conditions hold: lithostitched's `predicate`, +/// and the loader's conditions (NeoForge's, else Fabric's). A condition +/// type not known here does not hold. +fn conditions_hold(v: &Value, loaded: &dyn Fn(&str) -> bool) -> bool { + let all = |list: &Value, test: &dyn Fn(&Value) -> bool| { + list.as_array().is_some_and(|a| a.iter().all(test)) + }; + fn predicate(p: &Value, loaded: &dyn Fn(&str) -> bool) -> bool { + let test = |q: &Value| predicate(q, loaded); + let list = &p["predicates"]; + match p["type"] + .as_str() + .map(|t| t.trim_start_matches("lithostitched:")) + { + Some("true") => true, + Some("mod_loaded") => p["mod_id"].as_str().is_some_and(loaded), + Some("not") => !predicate(&p["predicate"], loaded), + Some("all_of") => list.as_array().is_some_and(|a| a.iter().all(test)), + Some("any_of") => list.as_array().is_some_and(|a| a.iter().any(test)), + _ => false, + } + } + fn neoforge(c: &Value, loaded: &dyn Fn(&str) -> bool) -> bool { + let test = |q: &Value| neoforge(q, loaded); + match c["type"].as_str() { + Some("neoforge:true") => true, + Some("neoforge:mod_loaded") => c["modid"].as_str().is_some_and(loaded), + Some("neoforge:not") => !neoforge(&c["value"], loaded), + Some("neoforge:and") => c["values"].as_array().is_some_and(|a| a.iter().all(test)), + Some("neoforge:or") => c["values"].as_array().is_some_and(|a| a.iter().any(test)), + _ => false, + } + } + fn fabric(c: &Value, loaded: &dyn Fn(&str) -> bool) -> bool { + let test = |q: &Value| fabric(q, loaded); + let mods = || { + c["values"] + .as_array() + .into_iter() + .flatten() + .map(Value::as_str) + }; + match c["condition"].as_str() { + Some("fabric:true") => true, + Some("fabric:all_mods_loaded") => mods().all(|m| m.is_some_and(loaded)), + Some("fabric:any_mods_loaded") => mods().any(|m| m.is_some_and(loaded)), + Some("fabric:not") => !fabric(&c["value"], loaded), + Some("fabric:and") => c["values"].as_array().is_some_and(|a| a.iter().all(test)), + Some("fabric:or") => c["values"].as_array().is_some_and(|a| a.iter().any(test)), + _ => false, + } + } + let litho = v.get("predicate").is_none_or(|p| predicate(p, loaded)); + let loader = match ( + v.get("neoforge:conditions"), + v.get("fabric:load_conditions"), + ) { + (Some(c), _) => all(c, &|c| neoforge(c, loaded)), + // One condition or a list of them. + (None, Some(c)) if c.is_array() => all(c, &|c| fabric(c, loaded)), + (None, Some(c)) => fabric(c, loaded), + (None, None) => true, + }; + litho && loader +} + +/// ChoiceTheorem's Overhauled Village's villages and outposts, which it adds +/// to vanilla's `villages` and `pillager_outposts` sets in Java code +/// (`CTOV.registerstructure`, through lithostitched), with its default +/// config: every village type in three sizes, weighted 10, 4 and 1, and +/// every outpost weighted 1. Vanilla's own entries stay (weight 1 each). +const CTOV_VILLAGES: [&str; 21] = [ + "beach", + "christmas", + "desert", + "desert_oasis", + "dark_forest", + "jungle", + "jungle_tree", + "mesa", + "mesa_fortified", + "mountain", + "mountain_alpine", + "mushroom", + "plains", + "plains_fortified", + "savanna", + "savanna_na", + "snowy_igloo", + "swamp", + "swamp_fortified", + "taiga", + "taiga_fortified", +]; +const CTOV_OUTPOSTS: [&str; 11] = [ + "beach", + "dark_forest", + "desert", + "jungle", + "badlands", + "mountain", + "plains", + "savanna", + "snowy", + "swamp", + "taiga", +]; + +/// The entries a mod adds to other structure sets in Java code, as its +/// default config has them: (file name, set, `[(structure, weight)]`). +/// Lithostitched appends them in the order of a hash map of every modifier +/// in the pack, which carry cannot know; they are written in the order the +/// mod registers them. Each set picks by weight, so how often each starts +/// is the same. +pub fn code_set_entries(mod_id: &str) -> Vec<(&'static str, &'static str, Vec<(String, u32)>)> { + match mod_id { + "ctov" => { + let outposts = CTOV_OUTPOSTS + .iter() + .map(|v| (format!("ctov:pillager_outpost_{v}"), 1)) + .collect(); + let villages = [("small", 10), ("medium", 4), ("large", 1)] + .into_iter() + .flat_map(|(size, weight)| { + CTOV_VILLAGES + .iter() + .map(move |v| (format!("ctov:{size}/village_{v}"), weight)) + }) + .collect(); + vec![ + ("pillager_outposts", "minecraft:pillager_outposts", outposts), + ("villages", "minecraft:villages", villages), + ] + } + _ => Vec::new(), + } +} + +/// Rewrites one `data/<ns>/worldgen/<kind>/...` file in place; whether it +/// changed. +/// `set_tags` are the mod's structure set tags, expanded +/// ([`structure_set_tags`]). +pub fn upgrade( + kind: &str, + v: &mut Value, + report: &mut Upgrade, + set_tags: &BTreeMap<String, Vec<String>>, +) -> bool { + let states = block_states(v); + report.block_states += usize::from(states); + let renamed = crate::carry::renamed_ids(v); + let dropped = crate::processors::each_list(kind, v, &mut |list| { + drop_java_processors(list, &mut report.processors_dropped) + }); + upgrade_types(kind, v, report, set_tags) || states || dropped || renamed +} + +/// Takes the processors written in Java (any type outside `minecraft:` and +/// the game's own `horizon:`) out of a processor list: `{"processors": +/// [...]}`, a bare list, or an id (left as it is). The ones with a data form +/// were rewritten before ([`crate::processors::carry`]). The game, like vanilla, refuses a list holding a type it +/// does not know, and with it every pool that names the list, so without +/// this the structure does not generate at all. Without them it generates +/// undecorated: no pillars under it, spawners left as the template has +/// them, blocks not swapped. Whether anything was taken out. +fn drop_java_processors(list: &mut Value, dropped: &mut BTreeMap<String, usize>) -> bool { + let Some(processors) = crate::processors::processors(list) else { + return false; + }; + let before = processors.len(); + processors.retain(|p| { + let t = p["processor_type"].as_str().unwrap_or_default(); + let java = !t.starts_with("minecraft:") && !t.starts_with("horizon:"); + if java { + *dropped.entry(t.to_owned()).or_default() += 1; + } + !java + }); + let mut changed = processors.len() != before; + // The inline lists inside the game's apply_random and condition. + for p in processors.iter_mut() { + let inner: Vec<&mut Value> = match p["processor_type"].as_str() { + Some("horizon:apply_random") => p["processor_lists"] + .as_array_mut() + .into_iter() + .flatten() + .map(|e| &mut e["data"]) + .collect(), + Some("horizon:condition") => { + let o = p.as_object_mut().into_iter().flat_map(|o| o.iter_mut()); + o.filter(|(k, _)| *k == "then" || *k == "else") + .map(|(_, v)| v) + .collect() + } + _ => Vec::new(), + }; + for list in inner { + changed |= drop_java_processors(list, dropped); + } + } + changed +} + +/// 1.21.1 writes a block or fluid state as `{"Name": ..., "Properties": +/// {...}}`; 26.3's codec reads `{"id": ..., "properties": {...}}` and +/// refuses the old shape, which fails the processor list, feature or pool +/// holding it. Whether anything changed. +fn block_states(v: &mut Value) -> bool { + match v { + Value::Object(o) => { + let state = o.get("Name").is_some_and(Value::is_string) + && o.keys().all(|k| k == "Name" || k == "Properties"); + if state { + if let Some(name) = o.remove("Name") { + o.insert("id".into(), name); + } + if let Some(properties) = o.remove("Properties") { + o.insert("properties".into(), properties); + } + return true; + } + o.values_mut() + .fold(false, |changed, x| block_states(x) | changed) + } + Value::Array(a) => a + .iter_mut() + .fold(false, |changed, x| block_states(x) | changed), + _ => false, + } +} + +/// The library types of one file rewritten as vanilla's; whether it changed. +fn upgrade_types( + kind: &str, + v: &mut Value, + report: &mut Upgrade, + set_tags: &BTreeMap<String, Vec<String>>, +) -> bool { + let lost = &mut report.rules_lost; + let mut unsupported = |t: &str| *report.unsupported.entry(t.to_owned()).or_default() += 1; + let changed = match kind { + "structure" => match v["type"].as_str() { + // Better Dungeons' small Nether dungeon is YUNG's jigsaw behind a + // config switch that ships on. + Some("yungsapi:yung_jigsaw" | "betterdungeons:small_nether_dungeon") => { + yung_structure(v, lost); + true + } + Some(t) + if JIGSAWS.contains(&t) + || NETHER_JIGSAWS.contains(&t) + || t == REPURPOSED_MINESHAFT + || REPURPOSED_CHECKED.iter().any(|(r, _)| *r == t) => + { + let mineshaft = t == REPURPOSED_MINESHAFT; + let check = REPURPOSED_CHECKED.iter().find(|(r, _)| *r == t); + let check = check.map(|(_, c)| *c); + let library = match t.split_once(':').map(|(ns, _)| ns) { + Some("moogs_structures") => Some("moogs"), + Some("repurposed_structures") => Some("repurposed"), + _ => None, + }; + let liquid = if library == Some("moogs") { + "top" + } else { + "lowest" + }; + let mut limited = library.map(|l| piece_limited(v, l)); + if let Some(limited) = limited.as_mut().filter(|_| mineshaft) { + 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()); + } + v["horizon:start_checks"]["repurposed"] = check.into(); + } + if let Some(limited) = limited { + v["horizon:piece_limited"] = limited; + } + v["type"] = "minecraft:jigsaw".into(); + true + } + Some(t) if !t.starts_with("minecraft:") => { + unsupported(t); + false + } + _ => false, + }, + "structure_set" => match v["placement"]["type"].as_str() { + Some(t) if SPREADS.contains(&t) => { + let moogs = t.starts_with("moogs_structures:"); + let p = &mut v["placement"]; + if moogs { + moogs_spacing(p, lost); + } + placement_rules(p, set_tags, lost); + p["type"] = "minecraft:random_spread".into(); + true + } + Some(t) if YUNG_SPREADS.contains(&t) => { + let t = t.to_owned(); + yung_placement(&t, &mut v["placement"], set_tags, lost); + true + } + Some(t) if !t.starts_with("minecraft:") => { + unsupported(t); + false + } + _ => false, + }, + "template_pool" => { + let mut changed = false; + let mut elements: Vec<&mut Value> = Vec::new(); + for e in v["elements"].as_array_mut().into_iter().flatten() { + let element = &mut e["element"]; + // A list element holds elements of its own. + if element.get("elements").is_some_and(Value::is_array) { + elements.extend(element["elements"].as_array_mut().into_iter().flatten()); + } else { + elements.push(element); + } + } + for element in elements { + let Some(t) = element["element_type"].as_str() else { + 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" { + yung_element(element, lost); + changed = true; + } else if !t.starts_with("minecraft:") { + unsupported(t); + } + } + changed + } + _ => false, + }; + report.mapped += usize::from(changed); + changed +} + +/// 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 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; + }; + o.insert("type".into(), "minecraft:jigsaw".into()); + let mut rules = serde_json::Map::new(); + for key in ["min_y", "max_y"] { + if let Some(y) = o.remove(key) { + rules.insert(key.into(), y); + } + } + // 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; + } + } + o.insert("horizon:yungsapi".into(), rules.into()); +} + +/// YUNG's single pool element as vanilla's, its rules under +/// `horizon:yungsapi`: name and `max_count`, depth range, priority, +/// `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; + }; + o.insert( + "element_type".into(), + "minecraft:single_pool_element".into(), + ); + let mut rules = serde_json::Map::new(); + for (from, to) in [ + ("name", "name"), + ("max_count", "max_count"), + ("min_required_depth", "min_depth"), + ("max_possible_depth", "max_depth"), + ("is_priority", "priority"), + ("ignore_bounds", "ignore_bounds"), + ] { + if let Some(x) = o.remove(from) { + rules.insert(to.into(), x); + } + } + if let Some(c) = o.remove("condition") { + rules.insert("condition".into(), yung_condition(&c, lost)); + } + // 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 +/// have does not hold, and is counted. +fn yung_condition(c: &Value, lost: &mut BTreeMap<String, usize>) -> Value { + let t = c["type"].as_str().unwrap_or_default(); + let list = |lost: &mut BTreeMap<String, usize>| -> Value { + let all = c["conditions"].as_array().into_iter().flatten(); + all.map(|x| yung_condition(x, lost)) + .collect::<Vec<_>>() + .into() + }; + let opt = |k: &str| c.get(k).cloned().unwrap_or(Value::Null); + match t.trim_start_matches("yungsapi:") { + "always_true" => serde_json::json!({"type": "true"}), + "not" => { + serde_json::json!({"type": "not", "condition": yung_condition(&c["condition"], lost)}) + } + "all_of" => serde_json::json!({"type": "all_of", "conditions": list(lost)}), + "any_of" => serde_json::json!({"type": "any_of", "conditions": list(lost)}), + "depth" => serde_json::json!({"type": "depth", + "min_depth": opt("min_required_depth"), "max_depth": opt("max_possible_depth")}), + "altitude" => serde_json::json!({"type": "altitude", + "bottom": opt("bottom_cutoff_y"), "top": opt("top_cutoff_y")}), + "random_chance" => serde_json::json!({"type": "random_chance", "chance": opt("chance")}), + "rotation" => serde_json::json!({"type": "rotation", "rotations": opt("rotations")}), + "piece_in_horizontal_direction" => { + // "yungsapi:all" (or no list) matches any piece. + let pieces: Vec<Value> = c["pieces"].as_array().cloned().unwrap_or_default(); + let any = pieces.is_empty() || pieces.iter().any(|p| p == "yungsapi:all"); + serde_json::json!({"type": "piece_in_horizontal_direction", + "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"}), + _ => { + *lost.entry(format!("yungsapi condition {t}")).or_default() += 1; + serde_json::json!({"type": "false"}) + } + } +} + +/// Moves a Repurposed Structures or Moog's jigsaw's start rules under +/// `horizon:start_checks`, and counts the layout rules it loses. `liquid` is +/// the block its no-start-in-liquid rule reads. +fn start_checks(v: &mut Value, liquid: &str, lost: &mut BTreeMap<String, usize>, limited: bool) { + let Some(o) = v.as_object_mut() else { + return; + }; + let mut checks = serde_json::Map::new(); + for (from, to) in START_RULES { + if let Some(n) = o.remove(from) { + checks.insert(to.into(), n); + } + } + if o.remove("cannot_spawn_in_liquid").and_then(|b| b.as_bool()) == Some(true) { + checks.insert("liquid".into(), liquid.into()); + } + // y_allowance bounds the terrain heights checked; it also clips pieces, + // which is lost. + for (from, to) in [("min_y_allowed", "min_y"), ("max_y_allowed", "max_y")] { + if let Some(n) = o.get("y_allowance").and_then(|y| y.get(from)) { + checks.insert(to.into(), n.clone()); + } + } + for field in DEAD_FIELDS { + o.remove(field); + } + for rule in LAYOUT_RULES { + let kept = limited && PIECE_LIMITED_RULES.contains(&rule); + if o.remove(rule).is_some_and(|r| r != Value::Bool(false)) && !kept { + *lost.entry(rule.into()).or_default() += 1; + } + } + if !checks.is_empty() { + o.insert("horizon:start_checks".into(), checks.into()); + } +} + +/// A Repurposed Structures or Moog's jigsaw's layout rules for the game's +/// piece-limited assembler: y_allowance, burying, the pools whose fallbacks +/// ignore the bounds, liquid, and the Nether variants' land search. The +/// bounding box hack is vanilla's expansion hack. The pieces' counts are +/// added from the mod's count files ([`piece_counts`]). +fn piece_limited(v: &mut Value, library: &str) -> Value { + let mut out = serde_json::Map::new(); + out.insert("library".into(), library.into()); + let lower = |x: &Value| x.as_str().map(str::to_ascii_lowercase); + for (from, to) in [("min_y_allowed", "min_y"), ("max_y_allowed", "max_y")] { + if let Some(n) = v.get("y_allowance").and_then(|y| y.get(from)) { + out.insert(to.into(), n.clone()); + } + } + if let Some(b) = v.get("burying_type").and_then(lower) { + out.insert("burying".into(), b.into()); + } + if v["cannot_spawn_in_liquid"].as_bool() == Some(true) { + out.insert("no_liquid".into(), true.into()); + } + if let Some(pools) = v + .get("pools_that_ignore_boundaries") + .filter(|p| p.is_array()) + { + out.insert("ignore_bounds_pools".into(), pools.clone()); + } + if v["use_bounding_box_hack"].as_bool() == Some(true) { + v["use_expansion_hack"] = true.into(); + } + if let Some(o) = v.as_object_mut() { + if let Some(search) = o.remove("land_search_direction") { + out.insert("land_search".into(), lower(&search).into()); + } + if let Some(ledge) = o.remove("ledge_offset_y") { + out.insert("ledge_offset_y".into(), ledge); + } + } + out.into() +} + +/// The pieces' spawn counts of the structures a mod ships, by structure id: +/// Repurposed Structures' `rs_pieces_spawn_counts` (each file names its +/// `target_structure`) and Moog's `msl_pieces_spawn_counts` (the file's id), +/// in the game's `horizon:piece_limited` shape. +pub fn piece_counts<R: Read + std::io::Seek>( + zip: &mut zip::ZipArchive<R>, +) -> BTreeMap<String, Value> { + let mut out = BTreeMap::new(); + for i in 0..zip.len() { + let Ok(mut entry) = zip.by_index(i) else { + continue; + }; + let name = entry.name().to_owned(); + let Some((ns, rest)) = name + .strip_prefix("data/") + .and_then(|r| r.strip_suffix(".json")) + .and_then(|r| r.split_once('/')) + else { + continue; + }; + let (rs, path) = if let Some(p) = rest.strip_prefix("rs_pieces_spawn_counts/") { + (true, p) + } else if let Some(p) = rest.strip_prefix("msl_pieces_spawn_counts/") { + (false, p) + } else { + continue; + }; + let mut text = String::new(); + let Ok(v) = entry + .read_to_string(&mut text) + .map_err(|_| ()) + .and_then(|_| serde_json::from_str::<Value>(&text).map_err(|_| ())) + else { + continue; + }; + let id = match v["target_structure"].as_str() { + Some(target) if rs => target.to_owned(), + _ if rs => continue, + _ => format!("{ns}:{path}"), + }; + let pieces: Vec<Value> = v["pieces_spawn_counts"] + .as_array() + .into_iter() + .flatten() + .filter_map(|p| { + let mut e = serde_json::Map::new(); + e.insert("name".into(), p["nbt_piece_name"].as_str()?.into()); + for (from, to) in [ + ("always_spawn_this_many", "required"), + ("never_spawn_more_than_this_many", "max"), + ("minimum_distance_from_center_piece", "min_depth"), + ] { + if let Some(n) = p.get(from).filter(|n| n.is_number()) { + e.insert(to.into(), n.clone()); + } + } + Some(Value::Object(e)) + }) + .collect(); + out.insert(id, pieces.into()); + } + out +} + +/// Adds a structure's piece counts to its `horizon:piece_limited` key. +pub fn add_piece_counts(id: &str, v: &mut Value, counts: &BTreeMap<String, Value>) -> bool { + match (counts.get(id), v.get_mut("horizon:piece_limited")) { + (Some(pieces), Some(limited)) => { + limited["pieces"] = pieces.clone(); + true + } + _ => false, + } +} + +/// AdvancedRandomSpread's spacing and separation, as Moog's computes them +/// with its default config: 1.65 times the file's, rounded, the separation +/// below the spacing. `when_replacing` (other numbers while a preset +/// replaces a vanilla structure) is lost. +fn moogs_spacing(p: &mut Value, lost: &mut BTreeMap<String, usize>) { + let scaled = |k: &str| (p[k].as_f64().unwrap_or(0.) * MOOGS_SPACING).round() as i64; + let spacing = scaled("spacing").max(1); + let separation = scaled("separation").min(spacing - 1); + p["spacing"] = spacing.into(); + p["separation"] = separation.into(); + if let Some(o) = p.as_object_mut() { + if o.remove("when_replacing").is_some() { + *lost.entry("when_replacing".into()).or_default() += 1; + } + } +} + +/// An advanced random spread's minimum distance from the world origin and +/// its super exclusion zone (sets named by a tag) under `horizon:` keys. +fn placement_rules( + p: &mut Value, + set_tags: &BTreeMap<String, Vec<String>>, + lost: &mut BTreeMap<String, usize>, +) { + let Some(o) = p.as_object_mut() else { + return; + }; + if let Some(d) = o.remove("min_distance_from_world_origin") { + o.insert("horizon:min_distance_from_world_origin".into(), d); + } + let zone = ["super_exclusion_zone", "enhanced_exclusion_zone"] + .into_iter() + .find_map(|k| Some((k, o.remove(k)?))); + if let Some((key, zone)) = zone { + let sets: Vec<String> = match &zone["other_set"] { + Value::String(s) => match s.strip_prefix('#') { + Some(tag) => set_tags.get(tag).cloned().unwrap_or_default(), + None => vec![s.clone()], + }, + Value::Array(a) => a + .iter() + .filter_map(|s| Some(s.as_str()?.to_owned())) + .collect(), + _ => Vec::new(), + }; + if sets.is_empty() { + *lost.entry(key.into()).or_default() += 1; + } else { + o.insert( + "horizon:exclusion_zone".into(), + serde_json::json!({"other_sets": sets, "chunk_count": zone["chunk_count"]}), + ); + } + } +} + +/// One of YUNG's placements as vanilla's random spread with `horizon:` keys: +/// the enhanced exclusion zone as `horizon:exclusion_zone`; the temples' +/// ocean and river search (findBiomeHorizontal around the chunk's middle +/// block at y 120, 48 blocks, every 2 quarts) as `horizon:avoid_biomes`; the +/// strongholds' rings as `horizon:ring_sections`. +fn yung_placement( + t: &str, + p: &mut Value, + set_tags: &BTreeMap<String, Vec<String>>, + lost: &mut BTreeMap<String, usize>, +) { + placement_rules(p, set_tags, lost); + p["type"] = "minecraft:random_spread".into(); + let Some(o) = p.as_object_mut() else { + return; + }; + match t { + "betterdeserttemples:desert_temple" | "betterjungletemples:jungle_temple" => { + o.insert( + "horizon:avoid_biomes".into(), + serde_json::json!({"biomes": ["#minecraft:is_river", "#minecraft:is_ocean"], + "radius": 48, "step": 2, "y": 120}), + ); + } + "betterstrongholds:stronghold" => { + let mut rings = serde_json::Map::new(); + for (from, to) in [ + ("chunk_distance_to_first_ring", "distance_to_first_ring"), + ("ring_chunk_thickness", "thickness"), + ("max_ring_section", "max_section"), + ] { + if let Some(n) = o.remove(from) { + rings.insert(to.into(), n); + } + } + o.insert("horizon:ring_sections".into(), rings.into()); + } + _ => {} + } +} + +/// Marks a YUNG's structure that the game can start (its type is vanilla's +/// after the upgrade) with the vanilla type its mod turns off, under +/// `horizon:disables`. One the game cannot start leaves the vanilla +/// structure in place, so the world keeps one of the two. +pub fn disables(id: &str, v: &mut Value) -> bool { + let Some((_, vanilla)) = YUNG_REPLACES.iter().find(|(s, _)| id.starts_with(s)) else { + return false; + }; + if !v["type"] + .as_str() + .is_some_and(|t| t.starts_with("minecraft:")) + { + return false; + } + v["horizon:disables"] = serde_json::json!([vanilla]); + true +} + +/// The mod's structure set tags (`data/<ns>/tags/worldgen/structure_set/`), +/// by tag id, with the tags they include expanded. Optional entries +/// (`{"id": ..., "required": false}`) count as their id. +pub fn structure_set_tags<R: Read + std::io::Seek>( + zip: &mut zip::ZipArchive<R>, +) -> BTreeMap<String, Vec<String>> { + let mut raw: BTreeMap<String, Vec<String>> = BTreeMap::new(); + for i in 0..zip.len() { + let Ok(mut entry) = zip.by_index(i) else { + continue; + }; + let Some(id) = entry + .name() + .strip_prefix("data/") + .and_then(|r| r.split_once("/tags/worldgen/structure_set/")) + .and_then(|(ns, path)| Some(format!("{ns}:{}", path.strip_suffix(".json")?))) + else { + continue; + }; + let mut text = String::new(); + let Ok(v) = entry.read_to_string(&mut text).map_err(drop).and_then(|_| { + serde_json::from_str::<Value>(text.trim_start_matches('\u{feff}')).map_err(drop) + }) else { + continue; + }; + let values = v["values"].as_array().into_iter().flatten(); + raw.insert( + id, + values + .filter_map(|e| e.as_str().or_else(|| e["id"].as_str()).map(str::to_owned)) + .collect(), + ); + } + let expand = |tag: &str| { + let (mut out, mut seen, mut todo) = (Vec::new(), BTreeSet::new(), vec![tag.to_owned()]); + while let Some(t) = todo.pop() { + if !seen.insert(t.clone()) { + continue; + } + for e in raw.get(&t).into_iter().flatten() { + match e.strip_prefix('#') { + Some(inner) => todo.push(inner.to_owned()), + None if !out.contains(e) => out.push(e.clone()), + None => {} + } + } + } + out + }; + raw.keys().map(|t| (t.clone(), expand(t))).collect() +} + +/// The `#c:` biome tags `v` names. +pub fn convention_tags(v: &Value, out: &mut BTreeSet<String>) { + match v { + Value::String(s) => { + if let Some(tag) = s.strip_prefix("#c:") { + out.insert(tag.to_owned()); + } + } + Value::Array(a) => a.iter().for_each(|x| convention_tags(x, out)), + Value::Object(o) => o.values().for_each(|x| convention_tags(x, out)), + _ => {} + } +} + +/// The loader's own convention biome tags (`data/c/tags/worldgen/biome/`), +/// by path, read from the loader jar `fetch` keeps in `<work>/loader/`. +pub fn loader_biome_tags(work: &Path) -> BTreeMap<String, Value> { + let mut out = BTreeMap::new(); + let Ok(dir) = std::fs::read_dir(work.join("loader")) else { + return out; + }; + for jar in dir.flatten().map(|e| e.path()) { + let Some(mut zip) = std::fs::File::open(&jar) + .ok() + .and_then(|f| zip::ZipArchive::new(f).ok()) + else { + continue; + }; + for i in 0..zip.len() { + let Ok(mut entry) = zip.by_index(i) else { + continue; + }; + let Some(path) = entry + .name() + .strip_prefix("data/c/tags/worldgen/biome/") + .and_then(|p| p.strip_suffix(".json")) + .map(str::to_owned) + else { + continue; + }; + let mut text = String::new(); + if entry.read_to_string(&mut text).is_ok() { + if let Ok(v) = serde_json::from_str(&text) { + out.insert(path, v); + } + } + } + } + out +} + +/// `tags` and the convention tags they include, in `conventions`. +pub fn with_included( + tags: &BTreeSet<String>, + conventions: &BTreeMap<String, Value>, +) -> BTreeSet<String> { + let mut all = BTreeSet::new(); + let mut todo: Vec<String> = tags.iter().cloned().collect(); + while let Some(tag) = todo.pop() { + if !conventions.contains_key(&tag) || !all.insert(tag.clone()) { + continue; + } + let mut more = BTreeSet::new(); + convention_tags(&conventions[&tag], &mut more); + todo.extend(more); + } + all +} + +/// Where `fetch` keeps the pack's loader jar, and where it comes from: +/// NeoForge's own Maven repository. Only NeoForge packs need it today. +pub fn loader_jar(work: &Path, loader: &str, version: &str) -> Option<(PathBuf, String)> { + (loader == "neoforge" && !version.is_empty()).then(|| { + ( + work.join("loader").join(format!("neoforge-{version}-universal.jar")), + format!( + "https://maven.neoforged.net/releases/net/neoforged/neoforge/{version}/neoforge-{version}-universal.jar" + ), + ) + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + const NO_TAGS: BTreeMap<String, Vec<String>> = BTreeMap::new(); + + #[test] + fn maps_library_types_to_vanillas() { + let mut report = Upgrade::default(); + let mut s = json!({"type": "lithostitched:jigsaw", "start_pool": "a:b"}); + assert!(upgrade("structure", &mut s, &mut report, &NO_TAGS)); + assert_eq!(s["type"], "minecraft:jigsaw"); + let mut formation = json!({"type": "formations:simple"}); + assert!(!upgrade("structure", &mut formation, &mut report, &NO_TAGS)); + let mut set = json!({"placement": {"type": "moogs_structures:advanced_random_spread", + "spacing": 10, "separation": 5, "min_distance_from_world_origin": 250}}); + assert!(upgrade("structure_set", &mut set, &mut report, &NO_TAGS)); + assert_eq!(set["placement"]["type"], "minecraft:random_spread"); + // Moog's spreads 1.65 times as far as its file says. + assert_eq!(set["placement"]["spacing"], 17); + assert_eq!(set["placement"]["separation"], 8); + assert_eq!( + set["placement"]["horizon:min_distance_from_world_origin"], + 250 + ); + assert!( + set["placement"] + .get("min_distance_from_world_origin") + .is_none() + ); + let mut pool = json!({"elements": [ + {"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"}} + ]}); + assert!(upgrade("template_pool", &mut pool, &mut report, &NO_TAGS)); + assert_eq!( + 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" + ); + assert_eq!(report.mapped, 3); + assert_eq!(report.block_states, 0); + assert_eq!( + report.unsupported, + BTreeMap::from([ + ("formations:simple".into(), 1), + ("somelib:element".into(), 1), + ]) + ); + } + + #[test] + fn keeps_the_jigsaws_start_rules() { + let mut report = Upgrade::default(); + let mut moogs = json!({"type": "moogs_structures:moogs_structures_generic_jigsaw_structure", + "cannot_spawn_in_liquid": true, "terrain_height_radius_check": 2, + "allowed_terrain_height_range": 12, "valid_biome_radius_check": 1, + "y_allowance": {"min_y_allowed": 60}, "burying_type": "lowest_corner", + "use_bounding_box_hack": false, "size": 7}); + assert!(upgrade("structure", &mut moogs, &mut report, &NO_TAGS)); + assert_eq!( + moogs["horizon:start_checks"], + json!({"liquid": "top", "terrain_radius": 2, "terrain_range": 12, + "biome_radius": 1, "min_y": 60}) + ); + for gone in [ + "cannot_spawn_in_liquid", + "y_allowance", + "burying_type", + "use_bounding_box_hack", + ] { + 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", + "cannot_spawn_in_liquid": true, "vertical_distance_from_start_piece": 35}); + assert!(upgrade("structure", &mut rs, &mut report, &NO_TAGS)); + assert_eq!(rs["horizon:start_checks"], json!({"liquid": "lowest"})); + assert!(rs.get("vertical_distance_from_start_piece").is_none()); + // Repurposed's own checks: the Nether city, Nether shipwrecks, the End stronghold. + for (t, check) in [ + ("city_nether_structure", "city_nether"), + ("shipwreck_nether_structure", "shipwreck_nether"), + ("stronghold_end", "stronghold_end"), + ] { + let mut rs = json!({"type": format!("repurposed_structures:{t}"), + "cannot_spawn_in_liquid": false, "size": 7}); + assert!(upgrade("structure", &mut rs, &mut report, &NO_TAGS)); + assert_eq!(rs["type"], "minecraft:jigsaw"); + assert_eq!(rs["horizon:start_checks"], json!({"repurposed": check})); + assert_eq!(rs["horizon:piece_limited"]["library"], "repurposed"); + } + assert!(report.unsupported.is_empty()); + // Nothing to check: no key. + let mut plain = json!({"type": "lithostitched:jigsaw", "cannot_spawn_in_liquid": false}); + assert!(upgrade("structure", &mut plain, &mut report, &NO_TAGS)); + assert!(plain.get("horizon:start_checks").is_none()); + // The layout rules go to the piece-limited assembler. + assert!(report.rules_lost.is_empty()); + assert_eq!( + moogs["horizon:piece_limited"], + json!({"library": "moogs", "min_y": 60, "burying": "lowest_corner", + "no_liquid": true}) + ); + } + + #[test] + fn super_exclusion_zones_name_their_sets() { + let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let opts = zip::write::SimpleFileOptions::default(); + for (name, text) in [ + ( + "data/rs/tags/worldgen/structure_set/avoid.json", + r##"{"values": ["minecraft:villages", "#rs:more", {"id": "x:y", "required": false}]}"##, + ), + ( + "data/rs/tags/worldgen/structure_set/more.json", + r##"{"values": ["minecraft:villages", "rs:cities", "#rs:avoid"]}"##, + ), + ] { + zip.start_file(name, opts).unwrap(); + std::io::Write::write_all(&mut zip, text.as_bytes()).unwrap(); + } + let mut zip = zip::ZipArchive::new(zip.finish().unwrap()).unwrap(); + let tags = structure_set_tags(&mut zip); + assert_eq!(tags["rs:avoid"], ["minecraft:villages", "x:y", "rs:cities"]); + let mut report = Upgrade::default(); + let mut set = json!({"placement": {"type": "repurposed_structures:advanced_random_spread", + "spacing": 30, "separation": 10, + "super_exclusion_zone": {"chunk_count": 4, "other_set": "#rs:avoid"}}}); + assert!(upgrade("structure_set", &mut set, &mut report, &tags)); + assert_eq!(set["placement"]["spacing"], 30); + assert_eq!( + set["placement"]["horizon:exclusion_zone"], + json!({"other_sets": ["minecraft:villages", "x:y", "rs:cities"], "chunk_count": 4}) + ); + let mut unknown = json!({"placement": {"type": "repurposed_structures:advanced_random_spread", + "spacing": 30, "separation": 10, + "super_exclusion_zone": {"chunk_count": 4, "other_set": "#rs:none"}}}); + assert!(upgrade("structure_set", &mut unknown, &mut report, &tags)); + assert_eq!(report.rules_lost["super_exclusion_zone"], 1); + } + + #[test] + fn yungs_jigsaws_keep_their_rules() { + 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, + "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}"); + } + // Better Dungeons' small Nether dungeon is the same jigsaw. + let mut nether = json!({"type": "betterdungeons:small_nether_dungeon", + "start_pool": "betterdungeons:small_nether_dungeon", "size": 10, + "start_jigsaw_name": "betterdungeons:small_nether_dungeon_anchor", + "max_distance_from_center": 80, "liquid_settings": "ignore_waterlogging"}); + 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" + ); + assert!(report.unsupported.is_empty()); + let mut pool = json!({"elements": [{"weight": 3, "element": { + "element_type": "yungsapi:yung_single_element", "location": "y:room", + "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"}, + {"type": "yungsapi:not", "condition": {"type": "yungsapi:mod_loaded", "modid": "create"}}, + {"type": "yungsapi:biome", "biomes": []}]}}}]}); + assert!(upgrade("template_pool", &mut pool, &mut report, &NO_TAGS)); + let e = &pool["elements"][0]["element"]; + assert_eq!(e["element_type"], "minecraft:single_pool_element"); + assert_eq!(e["location"], "y:room"); + assert_eq!( + e["horizon:yungsapi"], + json!({"name": "room", "max_count": 2, "min_depth": 1, "priority": true, + "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"}]}, + "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_eq!( + report.rules_lost, + BTreeMap::from([ + ("yungsapi condition yungsapi:biome".into(), 1), + ("yungsapi modifier action yungsapi:spin".into(), 1), + ("yungsapi modifier target yungsapi:all".into(), 1), + ]) + ); + assert!(report.unsupported.is_empty()); + } + + #[test] + fn drops_processors_written_in_java() { + let mut report = Upgrade::default(); + let mut list = json!({"processors": [ + {"processor_type": "repurposed_structures:bottom_pillar_processor"}, + {"processor_type": "minecraft:gravity", "heightmap": "WORLD_SURFACE_WG", "offset": -1}, + {"processor_type": "moogs_structures:spawner_randomizing_processor"}]}); + assert!(upgrade("processor_list", &mut list, &mut report, &NO_TAGS)); + assert_eq!( + list["processors"], + json!([{"processor_type": "minecraft:gravity", + "heightmap": "WORLD_SURFACE_WG", "offset": -1}]) + ); + let mut pool = json!({"elements": [ + {"weight": 1, "element": {"element_type": "minecraft:single_pool_element", + "processors": {"processors": [{"processor_type": "lithostitched:block_swap"}]}}}, + {"weight": 1, "element": {"element_type": "minecraft:list_pool_element", "elements": [ + {"element_type": "minecraft:single_pool_element", "processors": "x:named"}]}}]}); + assert!(upgrade("template_pool", &mut pool, &mut report, &NO_TAGS)); + assert_eq!( + pool["elements"][0]["element"]["processors"], + json!({"processors": []}) + ); + assert_eq!( + pool["elements"][1]["element"]["elements"][0]["processors"], + "x:named" + ); + let mut vanilla = json!({"processors": [{"processor_type": "minecraft:nop"}]}); + assert!(!upgrade( + "processor_list", + &mut vanilla, + &mut report, + &NO_TAGS + )); + assert_eq!( + report.processors_dropped, + BTreeMap::from([ + ("lithostitched:block_swap".into(), 1), + ("moogs_structures:spawner_randomizing_processor".into(), 1), + ("repurposed_structures:bottom_pillar_processor".into(), 1), + ]) + ); + } + + #[test] + fn yungs_placements_keep_their_rules() { + let tags = BTreeMap::from([( + "bf:avoid".to_owned(), + vec!["minecraft:nether_complexes".to_owned()], + )]); + let mut report = Upgrade::default(); + let mut fortress = json!({"placement": {"type": "yungsapi:enhanced_random_spread", + "salt": 1, "spacing": 30, "separation": 20, + "enhanced_exclusion_zone": {"other_set": "#bf:avoid", "chunk_count": 10}}}); + assert!(upgrade("structure_set", &mut fortress, &mut report, &tags)); + assert_eq!( + fortress["placement"], + json!({"type": "minecraft:random_spread", "salt": 1, "spacing": 30, "separation": 20, + "horizon:exclusion_zone": {"other_sets": ["minecraft:nether_complexes"], "chunk_count": 10}}) + ); + let mut temple = json!({"placement": {"type": "betterdeserttemples:desert_temple", + "salt": 2, "spacing": 30, "separation": 20, + "exclusion_zone": {"other_set": "minecraft:villages", "chunk_count": 6}}}); + assert!(upgrade("structure_set", &mut temple, &mut report, &tags)); + assert_eq!(temple["placement"]["type"], "minecraft:random_spread"); + assert_eq!(temple["placement"]["exclusion_zone"]["chunk_count"], 6); + assert_eq!( + temple["placement"]["horizon:avoid_biomes"], + json!({"biomes": ["#minecraft:is_river", "#minecraft:is_ocean"], + "radius": 48, "step": 2, "y": 120}) + ); + let mut stronghold = json!({"placement": {"type": "betterstrongholds:stronghold", + "salt": 3, "spacing": 85, "separation": 50, + "chunk_distance_to_first_ring": 80, "ring_chunk_thickness": 96}}); + assert!(upgrade( + "structure_set", + &mut stronghold, + &mut report, + &tags + )); + assert_eq!( + stronghold["placement"], + json!({"type": "minecraft:random_spread", "salt": 3, "spacing": 85, "separation": 50, + "horizon:ring_sections": {"distance_to_first_ring": 80, "thickness": 96}}) + ); + assert_eq!(report.mapped, 3); + assert!(report.unsupported.is_empty()); + // The vanilla counterpart is turned off only for a structure the game starts. + let mut ported = json!({"type": "minecraft:jigsaw"}); + assert!(disables("betterstrongholds:stronghold", &mut ported)); + assert_eq!(ported["horizon:disables"], json!(["minecraft:stronghold"])); + let mut java = json!({"type": "bettermineshafts:mineshaft"}); + assert!(!disables("bettermineshafts:mineshaft_oak", &mut java)); + let mut circle = json!({"type": "minecraft:jigsaw"}); + assert!(!disables("betterwitchhuts:witch_circle", &mut circle)); + } + + #[test] + fn piece_limited_jigsaws_keep_their_layout() { + let mut report = Upgrade::default(); + let mut nether = json!({"type": "repurposed_structures:generic_nether_jigsaw_structure", + "start_pool": "rs:x", "size": 8, "y_allowance": {"min_y_allowed": 40, "max_y_allowed": 90}, + "cannot_spawn_in_liquid": true, "pools_that_ignore_boundaries": ["rs:roof"], + "use_bounding_box_hack": true, "land_search_direction": "HIGHEST_LAND", + "ledge_offset_y": 2}); + assert!(upgrade("structure", &mut nether, &mut report, &NO_TAGS)); + assert_eq!(nether["type"], "minecraft:jigsaw"); + assert_eq!( + nether["horizon:piece_limited"], + json!({"library": "repurposed", "min_y": 40, "max_y": 90, "no_liquid": true, + "ignore_bounds_pools": ["rs:roof"], "land_search": "highest_land", + "ledge_offset_y": 2}) + ); + assert_eq!(nether["use_expansion_hack"], true); + for gone in [ + "y_allowance", + "use_bounding_box_hack", + "land_search_direction", + "ledge_offset_y", + ] { + assert!(nether.get(gone).is_none(), "{gone}"); + } + let mut moogs = json!({"type": "moogs_structures:moogs_structures_generic_jigsaw_structure", + "burying_type": "AVERAGE_LAND"}); + assert!(upgrade("structure", &mut moogs, &mut report, &NO_TAGS)); + assert_eq!( + moogs["horizon:piece_limited"], + json!({"library": "moogs", "burying": "average_land"}) + ); + // Repurposed's mineshaft is laid out under the terrain. + let mut mineshaft = json!({"type": "repurposed_structures:generic_mineshaft", + "start_pool": "rs:mineshafts/birch/start_pool", "size": 9, + "liquid_settings": "ignore_waterlogging"}); + assert!(upgrade("structure", &mut mineshaft, &mut report, &NO_TAGS)); + assert_eq!(mineshaft["type"], "minecraft:jigsaw"); + assert_eq!( + mineshaft["horizon:piece_limited"], + json!({"library": "repurposed", "layout": "mineshaft"}) + ); + assert_eq!(mineshaft["liquid_settings"], "ignore_waterlogging"); + // Lithostitched's jigsaw is vanilla's: no key. + let mut litho = json!({"type": "lithostitched:jigsaw"}); + assert!(upgrade("structure", &mut litho, &mut report, &NO_TAGS)); + assert!(litho.get("horizon:piece_limited").is_none()); + assert!(report.rules_lost.is_empty(), "{:?}", report.rules_lost); + assert!(report.unsupported.is_empty()); + + let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let opts = zip::write::SimpleFileOptions::default(); + for (name, text) in [ + ( + "data/rs/rs_pieces_spawn_counts/city.json", + r#"{"target_structure": "rs:city_nether", "pieces_spawn_counts": [ + {"nbt_piece_name": "rs:city/center", "always_spawn_this_many": 1, + "never_spawn_more_than_this_many": 1, "minimum_distance_from_center_piece": 3}]}"#, + ), + ( + "data/mvs/msl_pieces_spawn_counts/tower.json", + r#"{"pieces_spawn_counts": [{"nbt_piece_name": "mvs:top", "never_spawn_more_than_this_many": 2}]}"#, + ), + ] { + zip.start_file(name, opts).unwrap(); + std::io::Write::write_all(&mut zip, text.as_bytes()).unwrap(); + } + let mut zip = zip::ZipArchive::new(zip.finish().unwrap()).unwrap(); + let counts = piece_counts(&mut zip); + assert_eq!( + counts["rs:city_nether"], + json!([{"name": "rs:city/center", "required": 1, "max": 1, "min_depth": 3}]) + ); + assert_eq!(counts["mvs:tower"], json!([{"name": "mvs:top", "max": 2}])); + assert!(add_piece_counts("rs:city_nether", &mut nether, &counts)); + assert_eq!(nether["horizon:piece_limited"]["pieces"][0]["required"], 1); + assert!(!add_piece_counts("rs:other", &mut nether, &counts)); + } + + #[test] + fn writes_block_states_in_26_3_shape() { + let mut report = Upgrade::default(); + let mut list = json!({"processors": [{"processor_type": "minecraft:rule", "rules": [{ + "input_predicate": {"predicate_type": "minecraft:always_true"}, + "output_state": {"Name": "minecraft:oak_stairs", "Properties": {"facing": "east"}}}]}]}); + assert!(upgrade("processor_list", &mut list, &mut report, &NO_TAGS)); + assert_eq!( + list["processors"][0]["rules"][0]["output_state"], + json!({"id": "minecraft:oak_stairs", "properties": {"facing": "east"}}) + ); + let mut spring = json!({"type": "minecraft:spring_feature", "config": { + "state": {"Name": "minecraft:water"}, "valid_blocks": ["minecraft:stone"]}}); + assert!(upgrade( + "configured_feature", + &mut spring, + &mut report, + &NO_TAGS + )); + assert_eq!(spring["config"]["state"], json!({"id": "minecraft:water"})); + // An object with other keys is not a state; a 26.3 state is left as it is. + let mut other = json!({"Name": "x", "weight": 1, "state": {"id": "minecraft:stone"}}); + assert!(!upgrade( + "configured_feature", + &mut other, + &mut report, + &NO_TAGS + )); + assert_eq!(report.block_states, 2); + assert_eq!(report.mapped, 0); + } + + #[test] + fn modifiers_add_to_sets_and_pools() { + let mut report = Upgrade::default(); + let only_fd = |m: &str| m == "farmersdelight"; + let set = json!({"type": "lithostitched:add_structure_set_entries", + "structure_sets": "minecraft:villages", + "entries": [{"structure": "x:village", "weight": 4}]}); + let (kind, added) = modifier(&set, &only_fd, &mut report).unwrap(); + assert_eq!(kind, "structure_set"); + assert_eq!( + added, + json!({"horizon:add_to": ["minecraft:villages"], + "structures": [{"structure": "x:village", "weight": 4}]}) + ); + // A compat addition holds only when its mod is ported along. + let pool = |modid: &str| { + json!({"type": "lithostitched:add_template_pool_elements", + "neoforge:conditions": [{"type": "neoforge:mod_loaded", "modid": modid}], + "fabric:load_conditions": {"condition": "fabric:all_mods_loaded", "values": ["other"]}, + "template_pools": ["x:houses", "y:houses"], + "elements": [{"weight": 5, "element": {"element_type": "minecraft:single_pool_element", + "location": "x:fd_farm", "projection": "rigid", "processors": "minecraft:empty"}}]}) + }; + let (kind, added) = modifier(&pool("farmersdelight"), &only_fd, &mut report).unwrap(); + assert_eq!(kind, "template_pool"); + assert_eq!(added["horizon:add_to"], json!(["x:houses", "y:houses"])); + assert_eq!(added["elements"][0]["weight"], 5); + assert!(modifier(&pool("create"), &only_fd, &mut report).is_none()); + // Fabric's conditions when there are no NeoForge ones; lithostitched's own. + let fabric = json!({"type": "lithostitched:add_structure_set_entries", + "fabric:load_conditions": [{"condition": "fabric:not", + "value": {"condition": "fabric:any_mods_loaded", "values": ["create"]}}], + "predicate": {"type": "lithostitched:all_of", "predicates": [ + {"type": "lithostitched:mod_loaded", "mod_id": "farmersdelight"}, + {"type": "lithostitched:true"}]}, + "structure_sets": "minecraft:villages", "entries": []}); + assert!(modifier(&fabric, &only_fd, &mut report).is_some()); + let tagged = json!({"type": "lithostitched:add_structure_set_entries", + "structure_sets": "#x:sets", "entries": []}); + assert!(modifier(&tagged, &only_fd, &mut report).is_none()); + let features = json!({"type": "lithostitched:add_features"}); + assert!(modifier(&features, &only_fd, &mut report).is_none()); + assert_eq!( + report.added_to, + BTreeMap::from([ + ("minecraft:villages".into(), 1), + ("x:houses".into(), 1), + ("y:houses".into(), 1), + ]) + ); + assert_eq!( + report.modifiers_left, + BTreeMap::from([ + ("conditions".into(), 1), + ("lithostitched:add_features".into(), 1), + ("target".into(), 1), + ]) + ); + } + + #[test] + fn ctov_joins_vanillas_villages_and_outposts() { + let sets = code_set_entries("ctov"); + let (file, set, outposts) = &sets[0]; + assert_eq!( + (*file, *set), + ("pillager_outposts", "minecraft:pillager_outposts") + ); + assert_eq!(outposts.len(), 11); + assert!(outposts.iter().all(|(_, w)| *w == 1)); + let (_, set, villages) = &sets[1]; + assert_eq!(*set, "minecraft:villages"); + assert_eq!(villages.len(), 63); + assert_eq!(villages[0], ("ctov:small/village_beach".into(), 10)); + assert_eq!(villages[21], ("ctov:medium/village_beach".into(), 4)); + assert_eq!( + villages[62], + ("ctov:large/village_taiga_fortified".into(), 1) + ); + assert!(code_set_entries("mvs").is_empty()); + } + + #[test] + fn convention_tags_include_theirs() { + let conventions = BTreeMap::from([ + ( + "is_hot".to_owned(), + json!({"values": ["#c:is_desert", "#minecraft:is_badlands"]}), + ), + ( + "is_desert".to_owned(), + json!({"values": ["minecraft:desert"]}), + ), + ("is_cold".to_owned(), json!({"values": []})), + ]); + let mut used = BTreeSet::new(); + convention_tags( + &json!({"biomes": ["#c:is_hot", "#c:unknown", "#minecraft:x"]}), + &mut used, + ); + assert_eq!(used, BTreeSet::from(["is_hot".into(), "unknown".into()])); + assert_eq!( + with_included(&used, &conventions), + BTreeSet::from(["is_hot".into(), "is_desert".into()]) + ); + } +} diff --git a/crates/horizon-prepare/src/artifacts.rs b/crates/horizon-prepare/src/artifacts.rs index c9b1947ed..80b3bcf05 100644 --- a/crates/horizon-prepare/src/artifacts.rs +++ b/crates/horizon-prepare/src/artifacts.rs @@ -11,9 +11,10 @@ use std::fs; use std::path::{Component, Path, PathBuf}; const LAUNCHER_HINT: &str = - "Horizon does not download the game: install Minecraft Java Edition with the official \ -launcher, play {version} once, then rerun. If the launcher uses another game directory, \ -pass --minecraft-dir or set HORIZON_MINECRAFT_DIR."; + "Horizon does not download the game: play {version} once in the launcher you play \ +Minecraft Java Edition with (the official launcher, Prism Launcher, MultiMC...) so it downloads \ +the game's files, then rerun. If that launcher keeps them in another folder, pass \ +--minecraft-dir or set HORIZON_MINECRAFT_DIR."; const SERVER_HINT: &str = "Horizon does not download the game: download the {version} server jar yourself (minecraft.net/download/server \ while {version} is the current release, otherwise the downloads.server.url in your launcher's \ versions/{version}/{version}.json), then pass --server-jar PATH or set HORIZON_SERVER_JAR."; @@ -337,8 +338,9 @@ fn native_library_matches(library: &Value, names: &[&str]) -> bool { true } -/// The client libraries the official launcher installs on this OS and architecture. -fn client_library_artifacts(manifest: &Value) -> Result<Vec<&Value>, String> { +/// The client libraries the official launcher installs on this OS and architecture: each +/// library's Maven name and its download. +fn client_library_artifacts(manifest: &Value) -> Result<Vec<(&str, &Value)>, String> { let libraries = manifest .get("libraries") .and_then(Value::as_array) @@ -353,7 +355,8 @@ fn client_library_artifacts(manifest: &Value) -> Result<Vec<&Value>, String> { continue; }; if library_allowed(library)? && native_library_matches(library, &names) { - artifacts.push(artifact); + let name = library.get("name").and_then(Value::as_str).unwrap_or(""); + artifacts.push((name, artifact)); } } Ok(artifacts) @@ -374,6 +377,35 @@ pub(crate) fn safe_relative(path: &str) -> Option<PathBuf> { Some(parts.iter().collect()) } +/// A launcher's copy of the library `name`, whose path in the version JSON is `relative`, under +/// its game folder `minecraft`: at that path, else where Prism Launcher keeps it (#849). Prism's +/// metadata moves a native jar's classifier into its artifact name +/// (`com.mojang:jtracy:1.14.38:natives-windows` is its `com.mojang:jtracy-natives-windows:1.14.38`) +/// and Prism keeps libraries at their name's path, so the same jar, with the same sha1, is in +/// `com/mojang/jtracy-natives-windows/1.14.38/jtracy-natives-windows-1.14.38.jar`. The path the +/// version JSON gives is returned when neither exists, for the error to name. +pub fn launcher_library(minecraft: &Path, name: &str, relative: &Path) -> PathBuf { + let libraries = minecraft.join("libraries"); + let official = libraries.join(relative); + if official.is_file() { + return official; + } + if let [group, artifact, version, classifier] = name.split(':').collect::<Vec<_>>()[..] { + let artifact = format!("{artifact}-{classifier}"); + let path = format!( + "{}/{artifact}/{version}/{artifact}-{version}.jar", + group.replace('.', "/") + ); + if let Some(prism) = safe_relative(&path) + .map(|p| libraries.join(p)) + .filter(|p| p.is_file()) + { + return prism; + } + } + official +} + fn artifact_fields(artifact: &Value) -> Result<(&str, &str), String> { let path = artifact.get("path").and_then(Value::as_str); let sha1 = artifact.get("sha1").and_then(Value::as_str); @@ -502,11 +534,11 @@ pub fn import( let server = import_server(lock, server_jar)?; let (server, server_libraries) = unpack_server(server, &lock.directory, id)?; let mut client_libraries = Vec::new(); - for artifact in client_library_artifacts(&manifest)? { + for (name, artifact) in client_library_artifacts(&manifest)? { let (path, sha1) = artifact_fields(artifact)?; let relative = safe_relative(path).ok_or("Unsafe library path")?; client_libraries.push(import_copy( - &minecraft.join("libraries").join(&relative), + &launcher_library(&minecraft, name, &relative), &lock.directory.join("libraries/client").join(&relative), sha1, "sha1", @@ -744,7 +776,7 @@ pub fn verify(lock: &Lock) -> Result<Artifacts, String> { /// `client_libraries`); the client-side exporters compile against them. pub fn client_libraries(lock: &Lock, manifest: &Value) -> Result<Vec<PathBuf>, String> { let mut libraries = Vec::new(); - for artifact in client_library_artifacts(manifest)? { + for (_, artifact) in client_library_artifacts(manifest)? { let (path, sha1) = artifact_fields(artifact)?; let relative = safe_relative(path).ok_or("Unsafe client library path")?; let library = lock.directory.join("libraries/client").join(&relative); @@ -992,6 +1024,43 @@ mod tests { ); } + /// A native jar is found where the official launcher keeps it, else under the name Prism + /// Launcher's metadata gives it (#849); a plain library only at the version JSON's path. + #[test] + fn libraries_are_found_where_prism_launcher_keeps_them() { + let fixture = Imports::new(); + let minecraft = &fixture.directory; + let official = Path::new("com/mojang/jtracy/1.14.38/jtracy-1.14.38-natives-windows.jar"); + let name = "com.mojang:jtracy:1.14.38:natives-windows"; + let prism = minecraft.join( + "libraries/com/mojang/jtracy-natives-windows/1.14.38/jtracy-natives-windows-1.14.38.jar", + ); + assert_eq!( + launcher_library(minecraft, name, official), + minecraft.join("libraries").join(official) + ); + fs::create_dir_all(prism.parent().unwrap()).unwrap(); + fs::write(&prism, b"natives").unwrap(); + assert_eq!(launcher_library(minecraft, name, official), prism); + let mojang = minecraft.join("libraries").join(official); + fs::create_dir_all(mojang.parent().unwrap()).unwrap(); + fs::write(&mojang, b"natives").unwrap(); + assert_eq!(launcher_library(minecraft, name, official), mojang); + let plain = Path::new("com/mojang/jtracy/1.14.38/jtracy-1.14.38.jar"); + assert_eq!( + launcher_library(minecraft, "com.mojang:jtracy:1.14.38", plain), + minecraft.join("libraries").join(plain) + ); + for unsafe_name in ["a:..:1:x", "a:b\\c:1:x"] { + let path = launcher_library(minecraft, unsafe_name, plain); + assert_eq!( + path, + minecraft.join("libraries").join(plain), + "{unsafe_name}" + ); + } + } + #[test] fn natives_follow_the_architecture() { let names = ["a:b:1:natives-macos", "a:b:1:natives-macos-arm64"]; diff --git a/crates/horizon-prepare/src/sounds.rs b/crates/horizon-prepare/src/sounds.rs index b18ed3426..f6ff0b714 100644 --- a/crates/horizon-prepare/src/sounds.rs +++ b/crates/horizon-prepare/src/sounds.rs @@ -53,7 +53,7 @@ fn launcher_object(assets: &Path, checksum: &str) -> Result<PathBuf, String> { let path = object_path(assets, checksum)?; if !path.is_file() { return Err(format!( - "Missing asset object {}. Play the pinned version once in the official launcher.", + "Missing asset object {}. Play the pinned version once in your launcher.", path.display() )); } diff --git a/crates/horizon-protocol/Cargo.toml b/crates/horizon-protocol/Cargo.toml index fad282080..66a0af162 100644 --- a/crates/horizon-protocol/Cargo.toml +++ b/crates/horizon-protocol/Cargo.toml @@ -8,11 +8,13 @@ description = "Horizon's Minecraft Java Edition 26.3 network protocol (version 7 [dependencies] horizon-world = { path = "../horizon-world" } -flate2 = { version = "1.1.10", default-features = false, features = ["rust_backend"] } +flate2 = { version = "1.1.10", default-features = false, features = ["zlib-rs", "runtime_detection"] } serde_json = "1.0.151" # The shared secret and RSA padding (OS randomness) and the server hash. getrandom = "0.3" sha1 = "0.10" +# Secure chat: SHA256withRSA message signatures. +sha2 = "0.10" [lints] workspace = true diff --git a/crates/horizon-protocol/src/buf.rs b/crates/horizon-protocol/src/buf.rs index d937ff8b9..349ec74cf 100644 --- a/crates/horizon-protocol/src/buf.rs +++ b/crates/horizon-protocol/src/buf.rs @@ -23,6 +23,10 @@ impl<'a> Reader<'a> { pub fn position(&self) -> usize { self.pos } + /// The bytes read since `start` (a `position()`). + pub fn since(&self, start: usize) -> &'a [u8] { + &self.data[start.min(self.pos)..self.pos] + } pub fn rest(&mut self) -> &'a [u8] { let rest = &self.data[self.pos..]; self.pos = self.data.len(); @@ -307,6 +311,16 @@ impl Codec<Vec<u8>> for Bytes { w.byte_array(v) } } +/// ByteBufCodecs.byteArray(N): VarInt-prefixed bytes, at most N. +pub struct BytesN<const N: usize>; +impl<const N: usize> Codec<Vec<u8>> for BytesN<N> { + fn read(r: &mut Reader) -> Result<Vec<u8>> { + r.byte_array(N) + } + fn write(v: &Vec<u8>, w: &mut Writer) { + w.byte_array(v) + } +} /// Everything left in the packet (custom payloads). pub struct Rest; impl Codec<Vec<u8>> for Rest { diff --git a/crates/horizon-protocol/src/chunk.rs b/crates/horizon-protocol/src/chunk.rs index 3343eacf5..165627d90 100644 --- a/crates/horizon-protocol/src/chunk.rs +++ b/crates/horizon-protocol/src/chunk.rs @@ -57,6 +57,19 @@ pub fn storage_len(bits: u8, entries: usize) -> usize { } } +/// SimpleBitStorage's longs for `values` at `bits` each, a long at a time. +fn pack<T>(values: &[T], bits: u8, id: impl Fn(&T) -> u64) -> Vec<i64> { + let per = 64 / bits as usize; + values + .chunks(per) + .map(|word| { + word.iter() + .enumerate() + .fold(0u64, |w, (k, v)| w | id(v) << (k * bits as usize)) as i64 + }) + .collect() +} + /// One PalettedContainer as sent. #[derive(Clone, Debug, PartialEq)] pub struct Paletted { @@ -114,6 +127,66 @@ impl Paletted { /// Pack values the way the server's container would have grown: /// the smallest configuration for the number of distinct values. pub fn from_values(values: &[i32], strategy: Strategy, global_bits: u8) -> Self { + // Palette indices by value, in first-seen order: a table over the + // value range (registry ids are small), kept per thread and cleared + // after use, with the previous value remembered, since neighbouring + // blocks repeat. + thread_local! { + static INDEX: std::cell::RefCell<Vec<u32>> = const { std::cell::RefCell::new(Vec::new()) }; + } + let max = values.iter().copied().max().unwrap_or(0); + if values.iter().any(|&v| v < 0) || max >= 1 << 20 { + return Self::from_values_slow(values, strategy, global_bits); + } + let mut palette: Vec<i32> = Vec::new(); + let mut ids = Vec::with_capacity(values.len()); + INDEX.with_borrow_mut(|index| { + if index.len() <= max as usize { + index.resize(max as usize + 1, u32::MAX); + } + let (mut last, mut last_id) = (-1i32, 0u32); + for &v in values { + if v != last { + let slot = &mut index[v as usize]; + if *slot == u32::MAX { + *slot = palette.len() as u32; + palette.push(v); + } + (last, last_id) = (v, *slot); + } + ids.push(last_id); + } + for &v in &palette { + index[v as usize] = u32::MAX; + } + }); + let needed = if palette.len() <= 1 { + 0 + } else { + (usize::BITS - (palette.len() - 1).leading_zeros()) as u8 + }; + let (has_palette, bits) = strategy.configuration(needed, global_bits); + if bits == 0 { + return Self { + bits: 0, + palette: vec![palette.first().copied().unwrap_or(0)], + data: Vec::new(), + }; + } + let data = if has_palette { + pack(&ids, bits, |&id| u64::from(id)) + } else { + pack(values, bits, |&v| v as u64) + }; + Self { + bits, + palette: if has_palette { palette } else { Vec::new() }, + data, + } + } + + /// `from_values` for any values: linear palette searches. + fn from_values_slow(values: &[i32], strategy: Strategy, global_bits: u8) -> Self { let mut palette: Vec<i32> = Vec::new(); for &v in values { if !palette.contains(&v) { @@ -290,6 +363,29 @@ pub fn nibble(layer: &[u8], i: usize) -> u8 { mod tests { use super::*; + #[test] + fn from_values_matches_the_linear_search() { + let mut seed = 12345u64; + let mut next = |n: u64| { + seed = seed + .wrapping_mul(6364136223846793005) + .wrapping_add(1442695040888963407); + ((seed >> 33) % n) as i32 + }; + for distinct in [1u64, 2, 16, 17, 200, 300, 5000] { + let values: Vec<i32> = (0..BLOCKS).map(|_| next(distinct) * 7).collect(); + assert_eq!( + Paletted::from_values(&values, Strategy::Blocks, 16), + Paletted::from_values_slow(&values, Strategy::Blocks, 16) + ); + let biomes: Vec<i32> = values[..BIOMES].to_vec(); + assert_eq!( + Paletted::from_values(&biomes, Strategy::Biomes, 7), + Paletted::from_values_slow(&biomes, Strategy::Biomes, 7) + ); + } + } + #[test] fn bit_storage_matches_simple_bit_storage() { // 5 bits: 12 values per long, the top 4 bits of each long unused. diff --git a/crates/horizon-protocol/src/connection.rs b/crates/horizon-protocol/src/connection.rs index 4deda020c..8f7ae28cc 100644 --- a/crates/horizon-protocol/src/connection.rs +++ b/crates/horizon-protocol/src/connection.rs @@ -289,7 +289,12 @@ impl ConnReader { impl ConnWriter { /// Send one packet body (VarInt id + payload). pub fn send(&mut self, body: &[u8]) -> Result<()> { - let mut framed = frame::encode(body, self.threshold)?; + self.send_contents(&frame::contents(body, self.threshold)?) + } + /// Send a frame whose contents [`frame::contents`] made with this + /// writer's threshold. + pub fn send_contents(&mut self, contents: &[u8]) -> Result<()> { + let mut framed = frame::prefixed(contents); if let Some(c) = &mut self.encrypt { c.encrypt(&mut framed); } diff --git a/crates/horizon-protocol/src/frame.rs b/crates/horizon-protocol/src/frame.rs index 9a1402e0b..a403148f5 100644 --- a/crates/horizon-protocol/src/frame.rs +++ b/crates/horizon-protocol/src/frame.rs @@ -16,6 +16,12 @@ pub const MAX_UNCOMPRESSED: usize = 8_388_608; /// length prefix, and with compression enabled the uncompressed-length /// field (0 when below the threshold). pub fn encode(body: &[u8], threshold: Option<usize>) -> Result<Vec<u8>> { + Ok(prefixed(&contents(body, threshold)?)) +} + +/// What [`encode`] puts after the length prefix: the same for every +/// connection with the same threshold. +pub fn contents(body: &[u8], threshold: Option<usize>) -> Result<Vec<u8>> { let inner = match threshold { None => body.to_vec(), Some(t) if body.len() < t => { @@ -38,10 +44,15 @@ pub fn encode(body: &[u8], threshold: Option<usize>) -> Result<Vec<u8>> { inner.len() ))); } - let mut out = Vec::with_capacity(inner.len() + 3); - write_varint(&mut out, inner.len() as i32); - out.extend_from_slice(&inner); - Ok(out) + Ok(inner) +} + +/// A frame: the length prefix, then the [`contents`]. +pub fn prefixed(contents: &[u8]) -> Vec<u8> { + let mut out = Vec::with_capacity(contents.len() + 3); + write_varint(&mut out, contents.len() as i32); + out.extend_from_slice(contents); + out } /// Split one complete frame off the front of `buffer`, if available. diff --git a/crates/horizon-protocol/src/item.rs b/crates/horizon-protocol/src/item.rs index 81d429865..90ea5b6cd 100644 --- a/crates/horizon-protocol/src/item.rs +++ b/crates/horizon-protocol/src/item.rs @@ -218,6 +218,42 @@ impl Codec<Option<Stack>> for Slot { } } +/// ItemStackTemplate.STREAM_CODEC: the item, the count, then the patch. +pub struct Template; +impl Codec<Stack> for Template { + fn read(r: &mut Reader) -> Result<Stack> { + let item = r.varint()?; + let count = r.varint()?; + let positive = r.count(65536)?; + let negative = r.count(65536)?; + let mut added = Vec::with_capacity(positive.min(64)); + for _ in 0..positive { + let kind = r.varint()?; + added.push((kind, Component::read(kind, r)?)); + } + let removed = (0..negative).map(|_| r.varint()).collect::<Result<_>>()?; + Ok(Stack { + item, + count, + added, + removed, + }) + } + fn write(s: &Stack, w: &mut Writer) { + w.varint(s.item); + w.varint(s.count); + w.varint(s.added.len() as i32); + w.varint(s.removed.len() as i32); + for (kind, c) in &s.added { + w.varint(*kind); + c.write(*kind, w); + } + for kind in &s.removed { + w.varint(*kind); + } + } +} + /// HashedStack: what the client believes a slot holds. Component values are /// sent as hashes; Horizon sends patches without hashes, so a server whose /// stack carries components resynchronises that slot (as it does for any diff --git a/crates/horizon-protocol/src/lib.rs b/crates/horizon-protocol/src/lib.rs index e49031b77..1d0f081f7 100644 --- a/crates/horizon-protocol/src/lib.rs +++ b/crates/horizon-protocol/src/lib.rs @@ -13,6 +13,7 @@ pub mod item; pub mod packets; pub mod registry; pub mod rsa; +pub mod secure_chat; pub mod servers; pub mod text; diff --git a/crates/horizon-protocol/src/packets/mod.rs b/crates/horizon-protocol/src/packets/mod.rs index 57c4f7f36..4ef8ac21a 100644 --- a/crates/horizon-protocol/src/packets/mod.rs +++ b/crates/horizon-protocol/src/packets/mod.rs @@ -7,6 +7,7 @@ pub mod configuration; pub mod handshake; pub mod login; pub mod play; +pub mod recipes; pub mod status; use crate::buf::{Codec, Reader, Writer}; diff --git a/crates/horizon-protocol/src/packets/play.rs b/crates/horizon-protocol/src/packets/play.rs index 2152d5716..95a68d1ef 100644 --- a/crates/horizon-protocol/src/packets/play.rs +++ b/crates/horizon-protocol/src/packets/play.rs @@ -2,9 +2,9 @@ use crate::{ Error, Result, buf::{ - Bool, Byte, ChunkPos, Codec, Double, Float, Ident, Int, List, Long, LpVec3, Nbt, Opt, - OptVarInt, Pos, Reader, SectionPos, Short, Str, StrN, UByte, Uuid, VarInt, VarLong, Vec3d, - Writer, + Bool, Byte, BytesN, ChunkPos, Codec, Double, Float, Ident, Int, List, Long, LpVec3, Nbt, + Opt, OptVarInt, Pos, Reader, SectionPos, Short, Str, StrN, UByte, Uuid, VarInt, VarLong, + Vec3d, Writer, }, chunk::{ChunkData, LightData}, entity_data::{EntityData, Value}, @@ -244,6 +244,18 @@ packet!(EntityPositionSync { id: i32 => VarInt, path: PositionPath => PositionPa packet!(ForgetLevelChunk { pos: [i32; 2] => ChunkPos }); packet!(GameEvent { event: u8 => UByte, param: f32 => Float }); packet!(HurtAnimation { id: i32 => VarInt, yaw: f32 => Float }); +packet!( + /// ClientboundDamageEventPacket: the hurt entity, the damage type's + /// registry id, the causing and the direct entity (none: -1 on the wire's + /// id + 1) and the source's position. + DamageEvent { + id: i32 => VarInt, + source_type: i32 => VarInt, + cause: Option<i32> => OptVarInt, + direct: Option<i32> => OptVarInt, + position: Option<[f64; 3]> => Opt<Vec3d>, + } +); packet!(LevelChunkWithLight { x: i32 => Int, z: i32 => Int, chunk: ChunkData => ChunkData, light: LightData => LightData }); packet!(LevelEvent { kind: i32 => Int, pos: [i32; 3] => Pos, data: i32 => Int, global: bool => Bool }); packet!(LightUpdate { x: i32 => VarInt, z: i32 => VarInt, light: LightData => LightData }); @@ -567,6 +579,10 @@ packet!(PlayerPosition { pitch: f32 => Float, relatives: i32 => Int, }); +packet!( + /// ClientboundProjectilePowerPacket: an AbstractHurtingProjectile's accelerationPower. + ProjectilePower { id: i32 => VarInt, power: f64 => Double } +); packet!(PlayerRotation { yaw: f32 => Float, relative_yaw: bool => Bool, pitch: f32 => Float, relative_pitch: bool => Bool }); packet!(RemoveEntities { ids: Vec<i32> => List<VarInt> }); packet!(Respawn { spawn: SpawnInfo => SpawnInfo, keep: i8 => Byte }); @@ -577,11 +593,23 @@ packet!( ); packet!(Text { text: Tag => Nbt }); packet!(SetChunkCacheCenter { x: i32 => VarInt, z: i32 => VarInt }); +packet!( + /// ClientboundTickingStatePacket: the tick rate and whether the level is frozen. + TickingState { rate: f32 => Float, frozen: bool => Bool } +); +packet!( + /// ClientboundTickingStepPacket: the ticks a frozen level runs (`/tick step`). + TickingStep { steps: i32 => VarInt } +); packet!(Distance { distance: i32 => VarInt }); packet!(SetCursorItem { item: Option<Stack> => Slot }); packet!(SetDefaultSpawnPosition { pos: GlobalPos => GlobalPos, yaw: f32 => Float, pitch: f32 => Float }); packet!(SetEntityData { id: i32 => VarInt, values: Vec<(u8, i32, Value)> => EntityData }); packet!(SetEntityMotion { id: i32 => VarInt, velocity: [f64; 3] => LpVec3 }); +packet!( + /// ClientboundSetPassengersPacket: a vehicle and every entity riding it. + SetPassengers { vehicle: i32 => VarInt, passengers: Vec<i32> => List<VarInt> } +); /// ClientboundSetEquipmentPacket: (EquipmentSlot ordinal, stack) pairs. pub struct Equipment; impl Codec<Vec<(u8, Option<Stack>)>> for Equipment { @@ -865,6 +893,24 @@ packet!( UpdateMobEffect { id: i32 => VarInt, effect: i32 => VarInt, amplifier: i32 => VarInt, duration: i32 => VarInt, flags: i8 => Byte } ); packet!(RemoveMobEffect { id: i32 => VarInt, effect: i32 => VarInt }); +packet!( + /// AttributeModifier.STREAM_CODEC: id, amount and operation (0 add_value, + /// 1 add_multiplied_base, 2 add_multiplied_total). + AttributeModifier { id: String => Ident, amount: f64 => Double, operation: i32 => VarInt } +); +packet!( + /// ClientboundUpdateAttributesPacket.AttributeSnapshot: the attribute's + /// registry id, its base value and every modifier. + AttributeSnapshot { + attribute: i32 => VarInt, + base: f64 => Double, + modifiers: Vec<AttributeModifier> => List<AttributeModifier>, + } +); +packet!( + /// ClientboundUpdateAttributesPacket: an entity's synced attribute instances. + UpdateAttributes { id: i32 => VarInt, values: Vec<AttributeSnapshot> => List<AttributeSnapshot> } +); packet!( /// ClientboundSwingAnimationPacket: hand (0 main, 1 off) and the swing /// type (0 none, 1 whack, 2 stab) with its duration in ticks. @@ -893,6 +939,177 @@ pub struct CommandNode { /// Custom suggestion provider (`minecraft:ask_server`). pub suggestions: Option<String>, } +/// The `minecraft:command_argument_type` registry of 26.3 in id order +/// (ArgumentTypeInfos.bootstrap). +pub const ARGUMENT_TYPES: &[&str] = &[ + "brigadier:bool", + "brigadier:float", + "brigadier:double", + "brigadier:integer", + "brigadier:long", + "brigadier:string", + "minecraft:entity", + "minecraft:game_profile", + "minecraft:block_pos", + "minecraft:column_pos", + "minecraft:vec3", + "minecraft:vec2", + "minecraft:block_state", + "minecraft:block_predicate", + "minecraft:item_stack", + "minecraft:item_predicate", + "minecraft:team_color", + "minecraft:hex_color", + "minecraft:component", + "minecraft:style", + "minecraft:message", + "minecraft:nbt_compound_tag", + "minecraft:nbt_tag", + "minecraft:nbt_path", + "minecraft:objective", + "minecraft:objective_criteria", + "minecraft:operation", + "minecraft:particle", + "minecraft:angle", + "minecraft:rotation", + "minecraft:scoreboard_slot", + "minecraft:score_holder", + "minecraft:swizzle", + "minecraft:team", + "minecraft:item_slot", + "minecraft:item_slots", + "minecraft:resource_location", + "minecraft:function", + "minecraft:entity_anchor", + "minecraft:int_range", + "minecraft:float_range", + "minecraft:dimension", + "minecraft:gamemode", + "minecraft:time", + "minecraft:resource_or_tag", + "minecraft:resource_or_tag_key", + "minecraft:resource", + "minecraft:resource_key", + "minecraft:resource_selector", + "minecraft:template_mirror", + "minecraft:template_rotation", + "minecraft:heightmap", + "minecraft:loot_table", + "minecraft:loot_predicate", + "minecraft:loot_modifier", + "minecraft:context_float_provider", + "minecraft:context_int_provider", + "minecraft:slot_source", + "minecraft:dialog", + "minecraft:feature", + "minecraft:swing_animation", + "minecraft:uuid", +]; + +/// The properties of an argument parser (`ArgumentTypeInfo.Template`) as +/// one of the 26.3 parsers decoded from the network. +#[derive(Clone, Debug, PartialEq)] +pub enum ArgumentProperties { + None, + Integer { + min: Option<i32>, + max: Option<i32>, + }, + Long { + min: Option<i64>, + max: Option<i64>, + }, + Float { + min: Option<f32>, + max: Option<f32>, + }, + Double { + min: Option<f64>, + max: Option<f64>, + }, + /// StringArgumentType.StringType: 0 word, 1 phrase, 2 greedy. + String(i32), + Entity { + single: bool, + players_only: bool, + }, + ScoreHolder { + multiple: bool, + }, + Time { + min: i32, + }, + /// The resource arguments' registry. + Registry(String), +} + +impl ArgumentProperties { + /// ArgumentTypeInfo.deserializeFromNetwork for parser `id`. + pub fn read(r: &mut Reader, id: i32) -> Result<Self> { + let name = usize::try_from(id) + .ok() + .and_then(|i| ARGUMENT_TYPES.get(i)) + .ok_or_else(|| crate::Error::new(format!("unknown argument type {id}")))?; + let flagged = |r: &mut Reader| -> Result<(bool, bool)> { + let f = r.u8()?; + Ok((f & 1 != 0, f & 2 != 0)) + }; + Ok(match *name { + "brigadier:integer" => { + let (min, max) = flagged(r)?; + let min = if min { Some(r.i32()?) } else { None }; + let max = if max { Some(r.i32()?) } else { None }; + Self::Integer { min, max } + } + "brigadier:long" => { + let (min, max) = flagged(r)?; + let min = if min { Some(r.i64()?) } else { None }; + let max = if max { Some(r.i64()?) } else { None }; + Self::Long { min, max } + } + "brigadier:float" => { + let (min, max) = flagged(r)?; + let min = if min { Some(r.f32()?) } else { None }; + let max = if max { Some(r.f32()?) } else { None }; + Self::Float { min, max } + } + "brigadier:double" => { + let (min, max) = flagged(r)?; + let min = if min { Some(r.f64()?) } else { None }; + let max = if max { Some(r.f64()?) } else { None }; + Self::Double { min, max } + } + "brigadier:string" => Self::String(r.varint()?), + "minecraft:entity" => { + let f = r.u8()?; + Self::Entity { + single: f & 1 != 0, + players_only: f & 2 != 0, + } + } + "minecraft:score_holder" => Self::ScoreHolder { + multiple: r.u8()? & 1 != 0, + }, + "minecraft:time" => Self::Time { min: r.i32()? }, + "minecraft:resource" + | "minecraft:resource_key" + | "minecraft:resource_or_tag" + | "minecraft:resource_or_tag_key" + | "minecraft:resource_selector" => Self::Registry(r.string(32767)?), + _ => Self::None, + }) + } + /// Decodes a node's raw `parser` bytes. + pub fn decode(id: i32, bytes: &[u8]) -> Result<Self> { + Self::read(&mut Reader::new(bytes), id) + } +} + +/// Reads past an argument node's parser properties. +fn read_argument_properties(r: &mut Reader, id: i32) -> Result<()> { + ArgumentProperties::read(r, id).map(|_| ()) +} + /// The command tree: nodes and the index of the root. #[derive(Clone, Debug, PartialEq)] pub struct Commands { @@ -918,11 +1135,14 @@ impl Codec<Commands> for Commands { name = r.string(32767)?; } if kind == 2 { - // Properties are parser specific; only the property-less parsers - // can be skipped without a table, so keep the rest of the packet. - return Err(crate::Error::new("argument nodes are not decoded")); + let id = r.varint()?; + let start = r.position(); + read_argument_properties(r, id)?; + parser = Some((id, r.since(start).to_vec())); + if flags & 16 != 0 { + suggestions = Some(r.string(32767)?); + } } - let _ = (&mut parser, &mut suggestions); nodes.push(CommandNode { kind, executable: flags & 4 != 0, @@ -977,10 +1197,200 @@ impl Codec<Commands> for Commands { } } +/// `DisplayInfo` of an advancement as sent. +#[derive(Clone, Debug, PartialEq)] +pub struct AdvancementDisplay { + pub title: Tag, + pub description: Tag, + pub icon: crate::item::Stack, + /// `AdvancementType` ordinal: task, challenge, goal. + pub frame: i32, + pub show_toast: bool, + pub hidden: bool, + pub background: Option<String>, +} +/// `ClientboundUpdateAdvancementsPacket.PositionedAdvancement`. +#[derive(Clone, Debug, PartialEq)] +pub struct Advancement { + pub id: String, + pub parent: Option<String>, + pub display: Option<AdvancementDisplay>, + pub requirements: Vec<Vec<String>>, + pub telemetry: bool, + pub x: f32, + pub y: f32, +} +/// `ClientboundUpdateAdvancementsPacket`: whether it replaces what the +/// client has, the advancements added and removed, the progress of each +/// changed one (criterion, when obtained in epoch milliseconds), and whether +/// to show toasts. +#[derive(Clone, Debug, PartialEq)] +pub struct UpdateAdvancements { + pub reset: bool, + pub added: Vec<Advancement>, + pub removed: Vec<String>, + pub progress: Vec<(String, Vec<(String, Option<i64>)>)>, + pub show: bool, +} +impl Codec<UpdateAdvancements> for UpdateAdvancements { + fn read(r: &mut Reader) -> Result<Self> { + let reset = r.bool()?; + let n = r.count(r.remaining())?; + let mut added = Vec::with_capacity(n.min(1024)); + for _ in 0..n { + let id = r.string(32767)?; + let parent = if r.bool()? { + Some(r.string(32767)?) + } else { + None + }; + let display = if r.bool()? { + let title = r.nbt()?; + let description = r.nbt()?; + let icon = crate::item::Template::read(r)?; + let frame = r.varint()?; + let flags = r.i32()?; + let background = if flags & 1 != 0 { + Some(r.string(32767)?) + } else { + None + }; + Some(AdvancementDisplay { + title, + description, + icon, + frame, + show_toast: flags & 2 != 0, + hidden: flags & 4 != 0, + background, + }) + } else { + None + }; + let groups = r.count(r.remaining())?; + let mut requirements = Vec::with_capacity(groups.min(256)); + for _ in 0..groups { + let k = r.count(r.remaining())?; + requirements.push((0..k).map(|_| r.string(32767)).collect::<Result<_>>()?); + } + let telemetry = r.bool()?; + let (x, y) = (r.f32()?, r.f32()?); + added.push(Advancement { + id, + parent, + display, + requirements, + telemetry, + x, + y, + }); + } + let n = r.count(r.remaining())?; + let removed = (0..n).map(|_| r.string(32767)).collect::<Result<_>>()?; + let n = r.count(r.remaining())?; + let mut progress = Vec::with_capacity(n.min(1024)); + for _ in 0..n { + let id = r.string(32767)?; + let k = r.count(r.remaining())?; + let mut criteria = Vec::with_capacity(k.min(256)); + for _ in 0..k { + let name = r.string(32767)?; + let at = if r.bool()? { Some(r.i64()?) } else { None }; + criteria.push((name, at)); + } + progress.push((id, criteria)); + } + let show = r.bool()?; + Ok(Self { + reset, + added, + removed, + progress, + show, + }) + } + fn write(v: &Self, w: &mut Writer) { + w.bool(v.reset); + w.varint(v.added.len() as i32); + for a in &v.added { + w.string(&a.id); + w.bool(a.parent.is_some()); + if let Some(p) = &a.parent { + w.string(p); + } + w.bool(a.display.is_some()); + if let Some(d) = &a.display { + w.nbt(&d.title); + w.nbt(&d.description); + crate::item::Template::write(&d.icon, w); + w.varint(d.frame); + w.i32( + i32::from(d.background.is_some()) + | i32::from(d.show_toast) << 1 + | i32::from(d.hidden) << 2, + ); + if let Some(b) = &d.background { + w.string(b); + } + } + w.varint(a.requirements.len() as i32); + for g in &a.requirements { + w.varint(g.len() as i32); + for c in g { + w.string(c); + } + } + w.bool(a.telemetry); + w.f32(a.x); + w.f32(a.y); + } + w.varint(v.removed.len() as i32); + for id in &v.removed { + w.string(id); + } + w.varint(v.progress.len() as i32); + for (id, criteria) in &v.progress { + w.string(id); + w.varint(criteria.len() as i32); + for (name, at) in criteria { + w.string(name); + w.bool(at.is_some()); + if let Some(at) = at { + w.i64(*at); + } + } + } + w.bool(v.show); + } +} +packet!( + /// ClientboundAwardStatsPacket: (stat type, value, count) by network id. + AwardStats { stats: Vec<(i32, i32, i32)> => StatList } +); +/// `Object2IntMap<Stat<?>>`: each stat's type and value ids, then its count. +pub struct StatList; +impl Codec<Vec<(i32, i32, i32)>> for StatList { + fn read(r: &mut Reader) -> Result<Vec<(i32, i32, i32)>> { + let n = r.count(r.remaining())?; + (0..n) + .map(|_| Ok((r.varint()?, r.varint()?, r.varint()?))) + .collect() + } + fn write(v: &Vec<(i32, i32, i32)>, w: &mut Writer) { + w.varint(v.len() as i32); + for &(a, b, c) in v { + w.varint(a); + w.varint(b); + w.varint(c); + } + } +} + packet_enum!(Clientbound, crate::ids::play::clientbound { BundleDelimiter(Empty) = BUNDLE_DELIMITER, AddEntity(AddEntity) = ADD_ENTITY, Animate(Animate) = ANIMATE, + AwardStats(AwardStats) = AWARD_STATS, BlockChangedAck(BlockChangedAck) = BLOCK_CHANGED_ACK, BlockDestruction(BlockDestruction) = BLOCK_DESTRUCTION, BlockEntityData(BlockEntityData) = BLOCK_ENTITY_DATA, @@ -991,6 +1401,10 @@ packet_enum!(Clientbound, crate::ids::play::clientbound { ChunkBatchStart(Empty) = CHUNK_BATCH_START, ClearTitles(Bool1) = CLEAR_TITLES, Commands(Commands) = COMMANDS, + RecipeBookAdd(super::recipes::RecipeBookAdd) = RECIPE_BOOK_ADD, + RecipeBookRemove(super::recipes::RecipeBookRemove) = RECIPE_BOOK_REMOVE, + RecipeBookSettings(super::recipes::RecipeBookSettings) = RECIPE_BOOK_SETTINGS, + PlaceGhostRecipe(super::recipes::PlaceGhostRecipe) = PLACE_GHOST_RECIPE, ContainerClose(ContainerClose) = CONTAINER_CLOSE, ContainerSetContent(ContainerSetContent) = CONTAINER_SET_CONTENT, ContainerSetData(ContainerSetData) = CONTAINER_SET_DATA, @@ -1004,6 +1418,7 @@ packet_enum!(Clientbound, crate::ids::play::clientbound { GameEvent(GameEvent) = GAME_EVENT, GameRuleValues(GameRuleValues) = GAME_RULE_VALUES, HurtAnimation(HurtAnimation) = HURT_ANIMATION, + DamageEvent(DamageEvent) = DAMAGE_EVENT, KeepAlive(KeepAlive) = KEEP_ALIVE, InitializeBorder(InitializeBorder) = INITIALIZE_BORDER, LevelChunkWithLight(LevelChunkWithLight) = LEVEL_CHUNK_WITH_LIGHT, @@ -1050,6 +1465,8 @@ packet_enum!(Clientbound, crate::ids::play::clientbound { SetHealth(SetHealth) = SET_HEALTH, SetHeldSlot(SetHeldSlot) = SET_HELD_SLOT, SetObjective(SetObjective) = SET_OBJECTIVE, + SetPassengers(SetPassengers) = SET_PASSENGERS, + ProjectilePower(ProjectilePower) = PROJECTILE_POWER, SetPlayerInventory(SetPlayerInventory) = SET_PLAYER_INVENTORY, SetPlayerTeam(SetPlayerTeam) = SET_PLAYER_TEAM, SetScore(SetScore) = SET_SCORE, @@ -1066,14 +1483,132 @@ packet_enum!(Clientbound, crate::ids::play::clientbound { TabList(TabList) = TAB_LIST, TakeItemEntity(TakeItemEntity) = TAKE_ITEM_ENTITY, TeleportEntity(TeleportEntity) = TELEPORT_ENTITY, + TickingState(TickingState) = TICKING_STATE, + TickingStep(TickingStep) = TICKING_STEP, + UpdateAdvancements(UpdateAdvancements) = UPDATE_ADVANCEMENTS, + UpdateAttributes(UpdateAttributes) = UPDATE_ATTRIBUTES, UpdateMobEffect(UpdateMobEffect) = UPDATE_MOB_EFFECT, UpdateTags(UpdateTags) = UPDATE_TAGS, + TrackedWaypoint(TrackedWaypoint) = WAYPOINT, }); packet!( /// ClientboundClearTitlesPacket: whether to also reset the timings. Bool1 { value: bool => Bool } ); +/// ClientboundTrackedWaypointPacket.Operation, by ordinal. +pub mod waypoint_operation { + pub const TRACK: i32 = 0; + pub const UNTRACK: i32 = 1; + pub const UPDATE: i32 = 2; +} + +/// TrackedWaypoint's identifier: `Either<UUID, String>`. +#[derive(Clone, Debug, PartialEq)] +pub enum WaypointId { + Uuid(u128), + Name(String), +} + +/// What a TrackedWaypoint points at (its `Type` and contents). +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum WaypointTarget { + /// `TrackedWaypoint.empty`: an untracked waypoint. + Empty, + /// A block position (`Vec3iWaypoint`). + Block([i32; 3]), + /// A chunk (`ChunkWaypoint`). + Chunk([i32; 2]), + /// An angle in radians (`AzimuthWaypoint`). + Azimuth(f32), +} + +/// ClientboundTrackedWaypointPacket: the locator bar's waypoints. +#[derive(Clone, Debug, PartialEq)] +pub struct TrackedWaypoint { + /// `waypoint_operation`. + pub operation: i32, + pub id: WaypointId, + /// Waypoint.Icon: the style (a `waypoint_style_asset` key) and an RGB colour. + pub style: String, + pub color: Option<i32>, + pub target: WaypointTarget, +} + +impl Codec<TrackedWaypoint> for TrackedWaypoint { + fn read(r: &mut Reader) -> Result<TrackedWaypoint> { + let operation = r.varint()?; + if !(0..=2).contains(&operation) { + return Err(Error::new(format!("waypoint operation {operation}"))); + } + let id = if r.bool()? { + WaypointId::Uuid(r.uuid()?) + } else { + WaypointId::Name(r.string(32767)?) + }; + let style = Ident::read(r)?; + // ByteBufCodecs.RGB_COLOR: three bytes, read back as an opaque ARGB colour. + let color = if r.bool()? { + let [red, green, blue] = [r.u8()?, r.u8()?, r.u8()?].map(i32::from); + Some(-16_777_216 | red << 16 | green << 8 | blue) + } else { + None + }; + let target = match r.varint()? { + 0 => WaypointTarget::Empty, + 1 => WaypointTarget::Block([r.varint()?, r.varint()?, r.varint()?]), + 2 => WaypointTarget::Chunk([r.varint()?, r.varint()?]), + 3 => WaypointTarget::Azimuth(r.f32()?), + other => return Err(Error::new(format!("waypoint type {other}"))), + }; + Ok(TrackedWaypoint { + operation, + id, + style, + color, + target, + }) + } + fn write(v: &TrackedWaypoint, w: &mut Writer) { + w.varint(v.operation); + match &v.id { + WaypointId::Uuid(uuid) => { + w.bool(true); + w.uuid(*uuid); + } + WaypointId::Name(name) => { + w.bool(false); + w.string(name); + } + } + w.string(&v.style); + w.bool(v.color.is_some()); + if let Some(color) = v.color { + for shift in [16, 8, 0] { + w.u8((color >> shift) as u8); + } + } + match v.target { + WaypointTarget::Empty => w.varint(0), + WaypointTarget::Block([x, y, z]) => { + w.varint(1); + w.varint(x); + w.varint(y); + w.varint(z); + } + WaypointTarget::Chunk([x, z]) => { + w.varint(2); + w.varint(x); + w.varint(z); + } + WaypointTarget::Azimuth(angle) => { + w.varint(3); + w.f32(angle); + } + } + } +} + // ---- serverbound ------------------------------------------------------- packet!(AcceptTeleportation { id: i32 => VarInt, x: f64 => Double, y: f64 => Double, z: f64 => Double, yaw: f32 => Float, pitch: f32 => Float }); @@ -1101,6 +1636,42 @@ packet!(Chat { last_seen: LastSeenUpdate => LastSeenUpdate, }); packet!(ChatCommand { command: String => Str }); +packet!(ChatAck { offset: i32 => VarInt }); +/// ArgumentSignatures: each signed argument's name (at most 16 chars) and +/// signature, at most 8. +pub struct ArgumentSignatures; +impl Codec<Vec<(String, Vec<u8>)>> for ArgumentSignatures { + fn read(r: &mut Reader) -> Result<Vec<(String, Vec<u8>)>> { + let n = r.count(8)?; + (0..n) + .map(|_| Ok((r.string(16)?, r.bytes(256)?.to_vec()))) + .collect() + } + fn write(v: &Vec<(String, Vec<u8>)>, w: &mut Writer) { + w.varint(v.len() as i32); + for (name, signature) in v { + w.string(name); + w.bytes(signature); + } + } +} +packet!(ChatCommandSigned { + command: String => Str, + timestamp: i64 => Long, + salt: i64 => Long, + arguments: Vec<(String, Vec<u8>)> => ArgumentSignatures, + last_seen: LastSeenUpdate => LastSeenUpdate, +}); +packet!( + /// RemoteChatSession.Data: the session id, then ProfilePublicKey.Data + /// (expiry in epoch milliseconds, X.509 key, the services' signature). + ChatSessionUpdate { + session: u128 => Uuid, + expires_at: i64 => Long, + key: Vec<u8> => BytesN<512>, + key_signature: Vec<u8> => BytesN<4096>, + } +); packet!(ChangeDifficultyRequest { difficulty: i32 => VarInt }); packet!(LockDifficulty { locked: bool => Bool }); packet!(SetGameRule { entries: Vec<GameRuleEntry> => List<GameRuleEntry> }); @@ -1194,7 +1765,11 @@ packet_enum!(Serverbound, crate::ids::play::serverbound { Attack(Attack) = ATTACK, ChangeGameMode(ChangeGameMode) = CHANGE_GAME_MODE, Chat(Chat) = CHAT, + ChatAck(ChatAck) = CHAT_ACK, ChatCommand(ChatCommand) = CHAT_COMMAND, + ChatCommandSigned(ChatCommandSigned) = CHAT_COMMAND_SIGNED, + ChatSessionUpdate(ChatSessionUpdate) = CHAT_SESSION_UPDATE, + PlaceRecipe(super::recipes::PlaceRecipe) = PLACE_RECIPE, ChunkBatchReceived(ChunkBatchReceived) = CHUNK_BATCH_RECEIVED, ClientCommand(ClientCommand) = CLIENT_COMMAND, ClientInformation(ClientInformation) = CLIENT_INFORMATION, @@ -1288,6 +1863,42 @@ mod tests { } } + #[test] + fn tracked_waypoint_bytes() { + // TRACK, a UUID, the default style, red (#FF5555), a block position. + let track = Clientbound::TrackedWaypoint(TrackedWaypoint { + operation: waypoint_operation::TRACK, + id: WaypointId::Uuid(0x0102_0304_0506_0708_090a_0b0c_0d0e_0f10), + style: "minecraft:default".into(), + color: Some(0xFFFF_5555_u32 as i32), + target: WaypointTarget::Block([1, -2, 300]), + }); + let mut expected = vec![141, 1, 0, 1]; + expected.extend(1..=16u8); + expected.push(17); + expected.extend(b"minecraft:default"); + expected.extend([ + 1, 0xFF, 0x55, 0x55, 1, 1, 0xFE, 0xFF, 0xFF, 0xFF, 0x0F, 0xAC, 0x02, + ]); + assert_eq!(track.encode(), expected); + assert_eq!(Clientbound::decode(141, &expected[2..]).unwrap(), track); + for target in [ + WaypointTarget::Empty, + WaypointTarget::Chunk([-3, 4]), + WaypointTarget::Azimuth(-1.25), + ] { + let packet = Clientbound::TrackedWaypoint(TrackedWaypoint { + operation: waypoint_operation::UPDATE, + id: WaypointId::Name("spawn".into()), + style: "minecraft:bowtie".into(), + color: None, + target, + }); + let bytes = packet.encode(); + assert_eq!(Clientbound::decode(141, &bytes[2..]).unwrap(), packet); + } + } + #[test] fn clientbound_round_trips() { let packets = vec![ @@ -1522,10 +2133,34 @@ mod tests { id: 2, velocity: [0.0; 3], }), + Clientbound::SetPassengers(SetPassengers { + vehicle: 2, + passengers: vec![3, 300], + }), + Clientbound::ProjectilePower(ProjectilePower { id: 4, power: 0.1 }), Clientbound::SetEquipment(SetEquipment { id: 2, slots: vec![(0, None), (5, Some(Stack::new(1, 1)))], }), + Clientbound::UpdateAttributes(UpdateAttributes { + id: 2, + values: vec![AttributeSnapshot { + attribute: 26, + base: 0.23, + modifiers: vec![AttributeModifier { + id: "minecraft:baby".into(), + amount: 0.5, + operation: 1, + }], + }], + }), + Clientbound::DamageEvent(DamageEvent { + id: 7, + source_type: 31, + cause: Some(0), + direct: None, + position: Some([1.5, -2.0, 3.25]), + }), Clientbound::SetHealth(SetHealth { health: 17.5, food: 18, @@ -1573,6 +2208,40 @@ mod tests { relatives: 0, on_ground: true, }), + Clientbound::TickingState(TickingState { + rate: 40.0, + frozen: true, + }), + Clientbound::TickingStep(TickingStep { steps: 7 }), + Clientbound::AwardStats(AwardStats { + stats: vec![(8, 3, 120), (0, 1, 2)], + }), + Clientbound::UpdateAdvancements(UpdateAdvancements { + reset: true, + added: vec![Advancement { + id: "minecraft:story/root".into(), + parent: None, + display: Some(AdvancementDisplay { + title: literal("Minecraft"), + description: literal("The heart and story of the game"), + icon: crate::item::Stack::new(42, 1), + frame: 0, + show_toast: false, + hidden: false, + background: Some("minecraft:block/stone".into()), + }), + requirements: vec![vec!["crafting_table".into()]], + telemetry: false, + x: 0.0, + y: 1.5, + }], + removed: vec!["minecraft:story/gone".into()], + progress: vec![( + "minecraft:story/root".into(), + vec![("crafting_table".into(), Some(1_792_216_800_000))], + )], + show: true, + }), ]; for p in packets { round_trip(p, Clientbound::encode, Clientbound::decode); @@ -1605,6 +2274,24 @@ mod tests { Serverbound::ChatCommand(ChatCommand { command: "time set day".into(), }), + Serverbound::ChatAck(ChatAck { offset: 3 }), + Serverbound::ChatCommandSigned(ChatCommandSigned { + command: "say hi".into(), + timestamp: 1_700_000_000_000, + salt: -5, + arguments: vec![("message".into(), vec![9; 256])], + last_seen: LastSeenUpdate { + offset: 1, + acknowledged: [1, 0, 8], + checksum: -3, + }, + }), + Serverbound::ChatSessionUpdate(ChatSessionUpdate { + session: 0x1234, + expires_at: 1_800_000_000_000, + key: vec![0x30; 294], + key_signature: vec![7; 512], + }), Serverbound::ChunkBatchReceived(ChunkBatchReceived { chunks_per_tick: 9.5, }), @@ -1711,4 +2398,31 @@ mod tests { ); assert_eq!(p.encode()[1..], [1, 2, 3]); } + + #[test] + fn update_attributes_encode_like_vanilla() { + let p = Clientbound::UpdateAttributes(UpdateAttributes { + id: 5, + values: vec![AttributeSnapshot { + attribute: 1, + base: 0.0, + modifiers: vec![AttributeModifier { + id: "minecraft:armor.helmet".into(), + amount: 3.0, + operation: 0, + }], + }], + }); + let body = p.encode(); + // The packet id (134) is a two-byte VarInt. + let mut expected = vec![134, 1, 5, 1, 1]; + expected.extend(0.0f64.to_be_bytes()); + expected.extend([1, 22]); + expected.extend(b"minecraft:armor.helmet"); + expected.extend(3.0f64.to_be_bytes()); + expected.push(0); + assert_eq!(body, expected); + assert_eq!(crate::ids::play::clientbound::UPDATE_ATTRIBUTES, 134); + assert_eq!(Clientbound::decode(134, &body[2..]).unwrap(), p); + } } diff --git a/crates/horizon-protocol/src/packets/recipes.rs b/crates/horizon-protocol/src/packets/recipes.rs new file mode 100644 index 000000000..c289b8943 --- /dev/null +++ b/crates/horizon-protocol/src/packets/recipes.rs @@ -0,0 +1,607 @@ +//! The recipe book's packets and the recipe displays they carry +//! (`RecipeDisplay`, `SlotDisplay`, `RecipeDisplayEntry`): the 26.3 +//! `minecraft:recipe_display` and `minecraft:slot_display` types in id order. +use crate::{ + Result, + buf::{Codec, Reader, Writer}, + item::{Stack, Template}, +}; +use horizon_world::nbt::Tag; + +/// `HolderSet<Item>` on the network: a tag by name, or item ids. +#[derive(Clone, Debug, PartialEq)] +pub enum HolderSet { + Tag(String), + Items(Vec<i32>), +} +impl HolderSet { + pub fn read(r: &mut Reader) -> Result<Self> { + let n = r.varint()? - 1; + if n == -1 { + return Ok(Self::Tag(r.string(32767)?)); + } + let n = usize::try_from(n).map_err(|_| crate::Error::new("negative holder set size"))?; + if n > r.remaining() { + return Err(crate::Error::new("holder set larger than the packet")); + } + Ok(Self::Items( + (0..n).map(|_| r.varint()).collect::<Result<_>>()?, + )) + } + pub fn write(&self, w: &mut Writer) { + match self { + Self::Tag(name) => { + w.varint(0); + w.string(name); + } + Self::Items(ids) => { + w.varint(ids.len() as i32 + 1); + ids.iter().for_each(|id| w.varint(*id)); + } + } + } +} + +/// A trim pattern as `ByteBufCodecs.holder` sends it: a registry id, or +/// the pattern itself. +#[derive(Clone, Debug, PartialEq)] +pub enum TrimPattern { + Id(i32), + Direct { + asset: String, + description: Tag, + decal: bool, + }, +} + +/// `SlotDisplay`: what a recipe book slot shows. +#[derive(Clone, Debug, PartialEq)] +pub enum SlotDisplay { + Empty, + AnyFuel, + WithAnyPotion(Box<SlotDisplay>), + /// The source, kept to the stacks with this data component type. + OnlyWithComponent(Box<SlotDisplay>, i32), + Item(i32), + ItemStack(Stack), + Tag(HolderSet), + Dyed { + dye: Box<SlotDisplay>, + target: Box<SlotDisplay>, + }, + SmithingTrim { + base: Box<SlotDisplay>, + material: Box<SlotDisplay>, + pattern: TrimPattern, + }, + WithRemainder { + input: Box<SlotDisplay>, + remainder: Box<SlotDisplay>, + }, + Composite(Vec<SlotDisplay>), +} + +/// How deep slot displays may nest (composites of composites…). +const MAX_DEPTH: u32 = 32; + +impl SlotDisplay { + pub fn read(r: &mut Reader) -> Result<Self> { + Self::read_at(r, 0) + } + fn read_at(r: &mut Reader, depth: u32) -> Result<Self> { + if depth > MAX_DEPTH { + return Err(crate::Error::new("slot displays nest too deep")); + } + let inner = |r: &mut Reader| Self::read_at(r, depth + 1).map(Box::new); + Ok(match r.varint()? { + 0 => Self::Empty, + 1 => Self::AnyFuel, + 2 => Self::WithAnyPotion(inner(r)?), + 3 => { + let source = inner(r)?; + Self::OnlyWithComponent(source, r.varint()?) + } + 4 => Self::Item(r.varint()?), + 5 => Self::ItemStack(Template::read(r)?), + 6 => Self::Tag(HolderSet::read(r)?), + 7 => Self::Dyed { + dye: inner(r)?, + target: inner(r)?, + }, + 8 => { + let base = inner(r)?; + let material = inner(r)?; + let pattern = match r.varint()? { + 0 => TrimPattern::Direct { + asset: r.string(32767)?, + description: r.nbt()?, + decal: r.bool()?, + }, + id => TrimPattern::Id(id - 1), + }; + Self::SmithingTrim { + base, + material, + pattern, + } + } + 9 => Self::WithRemainder { + input: inner(r)?, + remainder: inner(r)?, + }, + 10 => { + let n = r.count(r.remaining())?; + let mut out = Vec::with_capacity(n.min(256)); + for _ in 0..n { + out.push(Self::read_at(r, depth + 1)?); + } + Self::Composite(out) + } + id => return Err(crate::Error::new(format!("unknown slot display {id}"))), + }) + } + pub fn write(&self, w: &mut Writer) { + match self { + Self::Empty => w.varint(0), + Self::AnyFuel => w.varint(1), + Self::WithAnyPotion(d) => { + w.varint(2); + d.write(w); + } + Self::OnlyWithComponent(d, component) => { + w.varint(3); + d.write(w); + w.varint(*component); + } + Self::Item(id) => { + w.varint(4); + w.varint(*id); + } + Self::ItemStack(stack) => { + w.varint(5); + Template::write(stack, w); + } + Self::Tag(set) => { + w.varint(6); + set.write(w); + } + Self::Dyed { dye, target } => { + w.varint(7); + dye.write(w); + target.write(w); + } + Self::SmithingTrim { + base, + material, + pattern, + } => { + w.varint(8); + base.write(w); + material.write(w); + match pattern { + TrimPattern::Id(id) => w.varint(id + 1), + TrimPattern::Direct { + asset, + description, + decal, + } => { + w.varint(0); + w.string(asset); + w.nbt(description); + w.bool(*decal); + } + } + } + Self::WithRemainder { input, remainder } => { + w.varint(9); + input.write(w); + remainder.write(w); + } + Self::Composite(list) => { + w.varint(10); + w.varint(list.len() as i32); + list.iter().for_each(|d| d.write(w)); + } + } + } +} + +fn read_list(r: &mut Reader) -> Result<Vec<SlotDisplay>> { + let n = r.count(r.remaining())?; + let mut out = Vec::with_capacity(n.min(256)); + for _ in 0..n { + out.push(SlotDisplay::read(r)?); + } + Ok(out) +} +fn write_list(list: &[SlotDisplay], w: &mut Writer) { + w.varint(list.len() as i32); + list.iter().for_each(|d| d.write(w)); +} + +/// `RecipeDisplay`: a recipe as the recipe book draws it. +#[derive(Clone, Debug, PartialEq)] +pub enum RecipeDisplay { + Shapeless { + ingredients: Vec<SlotDisplay>, + result: SlotDisplay, + station: SlotDisplay, + }, + Shaped { + width: i32, + height: i32, + ingredients: Vec<SlotDisplay>, + result: SlotDisplay, + station: SlotDisplay, + }, + Furnace { + ingredient: SlotDisplay, + fuel: SlotDisplay, + result: SlotDisplay, + station: SlotDisplay, + duration: i32, + experience: f32, + }, + Stonecutter { + input: SlotDisplay, + result: SlotDisplay, + station: SlotDisplay, + }, + Smithing { + template: SlotDisplay, + base: SlotDisplay, + addition: SlotDisplay, + result: SlotDisplay, + station: SlotDisplay, + }, +} +impl RecipeDisplay { + pub fn result(&self) -> &SlotDisplay { + match self { + Self::Shapeless { result, .. } + | Self::Shaped { result, .. } + | Self::Furnace { result, .. } + | Self::Stonecutter { result, .. } + | Self::Smithing { result, .. } => result, + } + } +} +impl Codec<RecipeDisplay> for RecipeDisplay { + fn read(r: &mut Reader) -> Result<Self> { + let slot = SlotDisplay::read; + Ok(match r.varint()? { + 0 => Self::Shapeless { + ingredients: read_list(r)?, + result: slot(r)?, + station: slot(r)?, + }, + 1 => { + let (width, height) = (r.varint()?, r.varint()?); + let ingredients = read_list(r)?; + if i64::from(width) * i64::from(height) != ingredients.len() as i64 { + return Err(crate::Error::new("shaped display size mismatch")); + } + Self::Shaped { + width, + height, + ingredients, + result: slot(r)?, + station: slot(r)?, + } + } + 2 => Self::Furnace { + ingredient: slot(r)?, + fuel: slot(r)?, + result: slot(r)?, + station: slot(r)?, + duration: r.varint()?, + experience: r.f32()?, + }, + 3 => Self::Stonecutter { + input: slot(r)?, + result: slot(r)?, + station: slot(r)?, + }, + 4 => Self::Smithing { + template: slot(r)?, + base: slot(r)?, + addition: slot(r)?, + result: slot(r)?, + station: slot(r)?, + }, + id => return Err(crate::Error::new(format!("unknown recipe display {id}"))), + }) + } + fn write(v: &Self, w: &mut Writer) { + match v { + Self::Shapeless { + ingredients, + result, + station, + } => { + w.varint(0); + write_list(ingredients, w); + result.write(w); + station.write(w); + } + Self::Shaped { + width, + height, + ingredients, + result, + station, + } => { + w.varint(1); + w.varint(*width); + w.varint(*height); + write_list(ingredients, w); + result.write(w); + station.write(w); + } + Self::Furnace { + ingredient, + fuel, + result, + station, + duration, + experience, + } => { + w.varint(2); + ingredient.write(w); + fuel.write(w); + result.write(w); + station.write(w); + w.varint(*duration); + w.f32(*experience); + } + Self::Stonecutter { + input, + result, + station, + } => { + w.varint(3); + input.write(w); + result.write(w); + station.write(w); + } + Self::Smithing { + template, + base, + addition, + result, + station, + } => { + w.varint(4); + template.write(w); + base.write(w); + addition.write(w); + result.write(w); + station.write(w); + } + } + } +} + +/// `RecipeDisplayEntry` with `ClientboundRecipeBookAddPacket.Entry`'s flags +/// (1 show a notification, 2 highlight as new). +#[derive(Clone, Debug, PartialEq)] +pub struct RecipeBookEntry { + pub id: i32, + pub display: RecipeDisplay, + /// The recipe group (0 none, else the group's index + 1). + pub group: i32, + /// `minecraft:recipe_book_category` id. + pub category: i32, + /// The ingredients the "craftable" filter checks, if the recipe has any. + pub requirements: Option<Vec<HolderSet>>, + pub flags: i8, +} + +/// ClientboundRecipeBookAddPacket: the entries, and whether they replace the +/// client's book. +#[derive(Clone, Debug, PartialEq)] +pub struct RecipeBookAdd { + pub entries: Vec<RecipeBookEntry>, + pub replace: bool, +} +impl RecipeBookEntry { + pub fn read(r: &mut Reader) -> Result<Self> { + let id = r.varint()?; + let display = RecipeDisplay::read(r)?; + let group = r.varint()?; + let category = r.varint()?; + let requirements = if r.bool()? { + let k = r.count(r.remaining())?; + let mut list = Vec::with_capacity(k.min(64)); + for _ in 0..k { + list.push(HolderSet::read(r)?); + } + Some(list) + } else { + None + }; + Ok(Self { + id, + display, + group, + category, + requirements, + flags: r.i8()?, + }) + } +} +impl Codec<RecipeBookAdd> for RecipeBookAdd { + fn read(r: &mut Reader) -> Result<Self> { + let n = r.count(r.remaining())?; + let mut entries = Vec::with_capacity(n.min(4096)); + for _ in 0..n { + entries.push(RecipeBookEntry::read(r)?); + } + Ok(Self { + entries, + replace: r.bool()?, + }) + } + fn write(v: &Self, w: &mut Writer) { + w.varint(v.entries.len() as i32); + for e in &v.entries { + w.varint(e.id); + RecipeDisplay::write(&e.display, w); + w.varint(e.group); + w.varint(e.category); + w.bool(e.requirements.is_some()); + if let Some(list) = &e.requirements { + w.varint(list.len() as i32); + list.iter().for_each(|s| s.write(w)); + } + w.i8(e.flags); + } + w.bool(v.replace); + } +} + +/// ClientboundRecipeBookRemovePacket: the display ids. +#[derive(Clone, Debug, PartialEq)] +pub struct RecipeBookRemove { + pub ids: Vec<i32>, +} +impl Codec<RecipeBookRemove> for RecipeBookRemove { + fn read(r: &mut Reader) -> Result<Self> { + let n = r.count(r.remaining())?; + Ok(Self { + ids: (0..n).map(|_| r.varint()).collect::<Result<_>>()?, + }) + } + fn write(v: &Self, w: &mut Writer) { + w.varint(v.ids.len() as i32); + v.ids.iter().for_each(|id| w.varint(*id)); + } +} + +/// RecipeBookSettings: each book's (open, filtering), in `RecipeBookType` +/// order (crafting, furnace, blast furnace, smoker). +#[derive(Clone, Debug, Default, PartialEq)] +pub struct RecipeBookSettings { + pub books: [(bool, bool); 4], +} +impl Codec<RecipeBookSettings> for RecipeBookSettings { + fn read(r: &mut Reader) -> Result<Self> { + let mut books = [(false, false); 4]; + for b in &mut books { + *b = (r.bool()?, r.bool()?); + } + Ok(Self { books }) + } + fn write(v: &Self, w: &mut Writer) { + for (open, filtering) in v.books { + w.bool(open); + w.bool(filtering); + } + } +} + +/// ClientboundPlaceGhostRecipePacket: the recipe to show faded in a menu +/// whose items were missing. +#[derive(Clone, Debug, PartialEq)] +pub struct PlaceGhostRecipe { + pub container: i32, + pub display: RecipeDisplay, +} +impl Codec<PlaceGhostRecipe> for PlaceGhostRecipe { + fn read(r: &mut Reader) -> Result<Self> { + Ok(Self { + container: r.varint()?, + display: RecipeDisplay::read(r)?, + }) + } + fn write(v: &Self, w: &mut Writer) { + w.varint(v.container); + RecipeDisplay::write(&v.display, w); + } +} + +/// ServerboundPlaceRecipePacket: place the recipe with this display id in +/// the open menu, as many times as possible when `use_max` (shift). +#[derive(Clone, Debug, PartialEq)] +pub struct PlaceRecipe { + pub container: i32, + pub recipe: i32, + pub use_max: bool, +} +impl Codec<PlaceRecipe> for PlaceRecipe { + fn read(r: &mut Reader) -> Result<Self> { + Ok(Self { + container: r.varint()?, + recipe: r.varint()?, + use_max: r.bool()?, + }) + } + fn write(v: &Self, w: &mut Writer) { + w.varint(v.container); + w.varint(v.recipe); + w.bool(v.use_max); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn book_entries_round_trip() { + let add = RecipeBookAdd { + entries: vec![ + RecipeBookEntry { + id: 7, + display: RecipeDisplay::Shaped { + width: 2, + height: 1, + ingredients: vec![ + SlotDisplay::Tag(HolderSet::Tag("minecraft:planks".into())), + SlotDisplay::Empty, + ], + result: SlotDisplay::ItemStack(Stack::new(12, 4)), + station: SlotDisplay::Item(300), + }, + group: 3, + category: 3, + requirements: Some(vec![HolderSet::Items(vec![1, 2])]), + flags: 3, + }, + RecipeBookEntry { + id: 8, + display: RecipeDisplay::Smithing { + template: SlotDisplay::Composite(vec![SlotDisplay::Item(1)]), + base: SlotDisplay::WithRemainder { + input: Box::new(SlotDisplay::AnyFuel), + remainder: Box::new(SlotDisplay::Item(2)), + }, + addition: SlotDisplay::OnlyWithComponent( + Box::new(SlotDisplay::WithAnyPotion(Box::new(SlotDisplay::Item(3)))), + 9, + ), + result: SlotDisplay::SmithingTrim { + base: Box::new(SlotDisplay::Item(4)), + material: Box::new(SlotDisplay::Dyed { + dye: Box::new(SlotDisplay::Item(5)), + target: Box::new(SlotDisplay::Item(6)), + }), + pattern: TrimPattern::Id(2), + }, + station: SlotDisplay::Item(7), + }, + group: 0, + category: 11, + requirements: None, + flags: 0, + }, + ], + replace: true, + }; + let mut w = Writer::new(); + RecipeBookAdd::write(&add, &mut w); + let mut r = Reader::new(&w.data); + assert_eq!(RecipeBookAdd::read(&mut r).unwrap(), add); + assert_eq!(r.remaining(), 0); + } +} diff --git a/crates/horizon-protocol/src/rsa.rs b/crates/horizon-protocol/src/rsa.rs index 57443e4fd..d34489fb4 100644 --- a/crates/horizon-protocol/src/rsa.rs +++ b/crates/horizon-protocol/src/rsa.rs @@ -1,8 +1,12 @@ //! RSA for the online-mode login: the server's X.509 public key //! (Crypt.byteToPublicKey) and PKCS#1 v1.5 encryption of the shared secret //! and challenge (Crypt.encryptUsingKey, Java's "RSA" cipher = -//! RSA/ECB/PKCS1Padding). Modular exponentiation uses Montgomery -//! multiplication on 32-bit limbs; RSA moduli are always odd. +//! RSA/ECB/PKCS1Padding), and on the server's side the key pair it makes at +//! startup (Crypt.generateKeyPair: 1024 bits) and the decryption +//! (Crypt.decryptUsingKey); and the PKCS#1 v1.5 signature checks of secure +//! chat (SHA256withRSA for messages, SHA1withRSA for the services' profile +//! key signatures). Modular exponentiation uses Montgomery multiplication on +//! 32-bit limbs; RSA moduli are always odd. use crate::{Error, Result}; /// A non-negative integer, little-endian 32-bit limbs. @@ -43,6 +47,46 @@ impl Big { } true } + /// self · other, as many limbs as both together. + fn mul(&self, other: &Self) -> Self { + let mut out = vec![0u32; self.0.len() + other.0.len()]; + for (i, &a) in self.0.iter().enumerate() { + let mut carry = 0u64; + for (j, &b) in other.0.iter().enumerate() { + let t = u64::from(out[i + j]) + u64::from(a) * u64::from(b) + carry; + out[i + j] = t as u32; + carry = t >> 32; + } + out[i + other.0.len()] = carry as u32; + } + Self(out) + } + /// self · m + a, one limb longer. + fn mul_small_add(&self, m: u32, a: u32) -> Self { + let mut out = Vec::with_capacity(self.0.len() + 1); + let mut carry = u64::from(a); + for &l in &self.0 { + let t = u64::from(l) * u64::from(m) + carry; + out.push(t as u32); + carry = t >> 32; + } + out.push(carry as u32); + Self(out) + } + /// (self / d, self mod d). + fn div_small(&self, d: u32) -> (Self, u32) { + let mut out = vec![0u32; self.0.len()]; + let mut rem = 0u64; + for i in (0..self.0.len()).rev() { + let t = rem << 32 | u64::from(self.0[i]); + out[i] = (t / u64::from(d)) as u32; + rem = t % u64::from(d); + } + (Self(out), rem as u32) + } + fn is_one(&self) -> bool { + self.0.first() == Some(&1) && self.0[1..].iter().all(|&l| l == 0) + } fn sub_assign(&mut self, other: &Self) { let mut borrow = 0i64; for (a, b) in self.0.iter_mut().zip(&other.0) { @@ -249,6 +293,255 @@ impl RsaPublicKey { } } +/// The digest a PKCS#1 v1.5 signature covers: Java's `SHA1withRSA` and +/// `SHA256withRSA`. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum SignatureHash { + Sha1, + Sha256, +} + +impl SignatureHash { + /// The DER DigestInfo before the digest (RFC 8017 9.2, note 1). + fn prefix(self) -> &'static [u8] { + match self { + Self::Sha1 => &[ + 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, + 0x14, + ], + Self::Sha256 => &[ + 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, + 0x01, 0x05, 0x00, 0x04, 0x20, + ], + } + } + fn digest(self, message: &[u8]) -> Vec<u8> { + use sha1::Digest; + match self { + Self::Sha1 => sha1::Sha1::digest(message).to_vec(), + Self::Sha256 => sha2::Sha256::digest(message).to_vec(), + } + } +} + +impl RsaPublicKey { + /// `Signature.verify` for RSASSA-PKCS1-v1_5 (RFC 8017 8.2.2): the + /// signature is exactly the modulus' length and below it, and opens to + /// `00 01 FF.. 00 DigestInfo`. Anything else is a failed check, as + /// SignatureValidator reports Java's exceptions. + pub fn verify(&self, hash: SignatureHash, message: &[u8], signature: &[u8]) -> bool { + let k = self.size(); + // Equal lengths compare as big-endian numbers. + if signature.len() != k || signature >= self.modulus.as_slice() { + return false; + } + let (prefix, digest) = (hash.prefix(), hash.digest(message)); + let t = prefix.len() + digest.len(); + if k < t + 11 { + return false; + } + let mut expected = vec![0xff; k]; + expected[0] = 0; + expected[1] = 1; + expected[k - t - 1] = 0; + expected[k - t..k - digest.len()].copy_from_slice(prefix); + expected[k - digest.len()..].copy_from_slice(&digest); + self.apply(signature, &self.exponent) == expected + } +} + +/// Small odd primes for sieving prime candidates. +const SMALL_PRIMES: [u32; 53] = [ + 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, + 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, + 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, +]; +/// Miller-Rabin rounds: a composite passes all of them with odds below 2⁻⁸⁰. +const ROUNDS: usize = 40; +const E: u32 = 65537; + +/// A random odd number of `limbs` limbs with its two top bits set, so two of +/// them multiply to exactly twice the bits. +fn candidate(limbs: usize) -> Result<Big> { + let mut bytes = vec![0u8; limbs * 4]; + crate::crypto::random_bytes(&mut bytes)?; + bytes[0] |= 0xC0; + bytes[limbs * 4 - 1] |= 1; + Ok(Big::from_be_bytes(&bytes, limbs)) +} + +/// Miller-Rabin with random bases. +fn probably_prime(p: &Big) -> Result<bool> { + if SMALL_PRIMES.iter().any(|&q| p.div_small(q).1 == 0) { + return Ok(false); + } + let k = p.0.len(); + let mont = Mont::new(p.clone()); + let mut p_minus_one = p.clone(); + p_minus_one.0[0] -= 1; // p is odd + let s = (0..k * 32).find(|&i| p_minus_one.bit(i)).unwrap_or(0); + // d = (p - 1) >> s + let mut d = Big(vec![0; k]); + for i in 0..k * 32 - s { + if p_minus_one.bit(i + s) { + d.0[i / 32] |= 1 << (i % 32); + } + } + let mut two = Big(vec![0; k]); + two.0[0] = 2; + 'rounds: for _ in 0..ROUNDS { + // A random base in [2, p - 2]. + let mut a = candidate(k)?; + a.0[k - 1] %= p.0[k - 1]; + if !a.ge(&two) || a.ge(&p_minus_one) { + continue; + } + let mut x = mont.pow(&a, &d); + if x.is_one() || x == p_minus_one { + continue; + } + for _ in 1..s { + x = mont.pow(&x, &two); + if x == p_minus_one { + continue 'rounds; + } + if x.is_one() { + return Ok(false); + } + } + return Ok(false); + } + Ok(true) +} + +/// A prime of `limbs` limbs whose p - 1 is prime to E. +fn prime(limbs: usize) -> Result<Big> { + loop { + let p = candidate(limbs)?; + if p.div_small(E).1 != 1 && probably_prime(&p)? { + return Ok(p); + } + } +} + +/// DER: a tag and its length, then the contents. +fn der_element(tag: u8, contents: &[u8]) -> Vec<u8> { + let mut out = vec![tag]; + let len = contents.len(); + if len < 0x80 { + out.push(len as u8); + } else { + let bytes: Vec<u8> = len + .to_be_bytes() + .into_iter() + .skip_while(|&b| b == 0) + .collect(); + out.push(0x80 | bytes.len() as u8); + out.extend(bytes); + } + out.extend_from_slice(contents); + out +} + +/// A DER INTEGER from big-endian magnitude bytes (a leading zero keeps it positive). +fn der_integer(magnitude: &[u8]) -> Vec<u8> { + let mut v = strip(magnitude); + if v.first().is_none_or(|b| b & 0x80 != 0) { + v.insert(0, 0); + } + der_element(0x02, &v) +} + +/// The server's key pair (Crypt.generateKeyPair). +pub struct RsaPrivateKey { + pub public: RsaPublicKey, + /// The private exponent, big-endian. + exponent: Vec<u8>, +} + +impl std::fmt::Debug for RsaPrivateKey { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("RsaPrivateKey") + .field("public", &self.public) + .finish_non_exhaustive() + } +} + +impl RsaPrivateKey { + /// A new key pair with a `bits`-bit modulus (a multiple of 64) and + /// public exponent 65537, as Java's RSA KeyPairGenerator makes. + pub fn generate(bits: usize) -> Result<Self> { + let limbs = bits / 64; + let p = prime(limbs)?; + let q = loop { + let q = prime(limbs)?; + if q != p { + break q; + } + }; + let n = p.mul(&q); + // d = (1 + k·φ) / E with k making it whole: k ≡ -φ⁻¹ (mod E). + let (mut p1, mut q1) = (p, q); + p1.0[0] -= 1; + q1.0[0] -= 1; + let phi = p1.mul(&q1); + let r = u64::from(phi.div_small(E).1); + let inverse = (1..u64::from(E)) + .find(|k| k * r % u64::from(E) == 1) + .ok_or_else(|| Error::new("RSA key generation failed"))?; + let k = (u64::from(E) - inverse) as u32; + let (d, rem) = phi.mul_small_add(k, 1).div_small(E); + if rem != 0 { + return Err(Error::new("RSA key generation failed")); + } + let size = bits / 8; + Ok(Self { + public: RsaPublicKey { + modulus: strip(&n.to_be_bytes(size)), + exponent: strip(&E.to_be_bytes()), + }, + exponent: d.to_be_bytes(size), + }) + } + + /// PublicKey.getEncoded: X.509 SubjectPublicKeyInfo. + pub fn public_der(&self) -> Vec<u8> { + let key = der_element( + 0x30, + &[ + der_integer(&self.public.modulus), + der_integer(&self.public.exponent), + ] + .concat(), + ); + let algorithm = der_element( + 0x30, + &[der_element(0x06, RSA_ENCRYPTION), vec![0x05, 0x00]].concat(), + ); + let bits = der_element(0x03, &[&[0u8][..], &key].concat()); + der_element(0x30, &[algorithm, bits].concat()) + } + + /// Crypt.decryptUsingKey: RSAES-PKCS1-v1_5 decryption (RFC 8017 7.2.2). + pub fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>> { + let bad = || Error::new("Decryption error"); + let k = self.public.size(); + // The modulus has no leading zero, so equal lengths compare as numbers. + if ciphertext.len() != k || ciphertext >= self.public.modulus.as_slice() { + return Err(bad()); + } + let block = self.public.apply(ciphertext, &self.exponent); + if block[0] != 0 || block[1] != 2 { + return Err(bad()); + } + let zero = block[2..].iter().position(|&b| b == 0).ok_or_else(bad)? + 2; + if zero < 10 { + return Err(bad()); + } + Ok(block[zero + 1..].to_vec()) + } +} + #[cfg(test)] pub(crate) mod tests { use super::*; @@ -316,6 +609,50 @@ pub(crate) mod tests { assert!(key.encrypt(&[0; 118]).is_err()); } + /// A key made here: Java-style DER that parses back, and what its public + /// half encrypts its private half decrypts. + #[test] + fn generated_keys_round_trip() { + let key = RsaPrivateKey::generate(1024).unwrap(); + assert_eq!(key.public.size(), 128); + assert_eq!(key.public.exponent, vec![1, 0, 1]); + let der = key.public_der(); + assert_eq!( + &der[..3], + &[0x30, 0x81, 0x9f], + "a 1024-bit SubjectPublicKeyInfo" + ); + assert_eq!(RsaPublicKey::from_der(&der).unwrap(), key.public); + for message in [&[0x5au8; 16][..], &[1, 2, 3, 4], &[]] { + let c = key.public.encrypt(message).unwrap(); + assert_eq!(key.decrypt(&c).unwrap(), message); + } + assert!(key.decrypt(&[0u8; 128]).is_err()); + assert!(key.decrypt(&[1u8; 64]).is_err()); + } + + #[test] + fn decrypts_with_a_known_key() { + let key = RsaPrivateKey { + public: RsaPublicKey::from_der(&hex(TEST_SPKI)).unwrap(), + exponent: hex(TEST_D), + }; + let c = key.public.encrypt(b"secret").unwrap(); + assert_eq!(key.decrypt(&c).unwrap(), b"secret"); + } + + #[test] + fn finds_primes() { + let p = Big(vec![0xFFFF_FFFB]); // 2^32 - 5 is prime + assert!(probably_prime(&p).unwrap()); + assert!(!probably_prime(&Big(vec![0xFFFF_FFFD])).unwrap()); // 3 · 1431655765 + // 2^61 - 1 (prime) and a Carmichael-free composite 2^61 + 1. + let m61 = (1u64 << 61) - 1; + assert!(probably_prime(&Big(vec![m61 as u32, (m61 >> 32) as u32])).unwrap()); + let c = (1u64 << 61) + 1; + assert!(!probably_prime(&Big(vec![c as u32, (c >> 32) as u32])).unwrap()); + } + /// An OpenSSL signature checked with the public exponent: the block ends /// with SHA-256("hello"), so the modular arithmetic agrees with OpenSSL's. #[test] @@ -326,5 +663,34 @@ pub(crate) mod tests { assert!(block.ends_with(&hex( "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824" ))); + let signature = hex(TEST_SIG_HELLO); + assert!(key.verify(SignatureHash::Sha256, b"hello", &signature)); + assert!(!key.verify(SignatureHash::Sha256, b"hellO", &signature)); + assert!(!key.verify(SignatureHash::Sha1, b"hello", &signature)); + let mut flipped = signature.clone(); + flipped[40] ^= 1; + assert!(!key.verify(SignatureHash::Sha256, b"hello", &flipped)); + assert!(!key.verify(SignatureHash::Sha256, b"hello", &signature[1..])); + assert!(!key.verify(SignatureHash::Sha256, b"hello", &key.modulus)); + } + + /// Signatures made here with the private exponent (EMSA-PKCS1-v1_5 by + /// hand) check out for both digests. + #[test] + fn verifies_both_digests() { + let key = RsaPublicKey::from_der(&hex(TEST_SPKI)).unwrap(); + for hash in [SignatureHash::Sha1, SignatureHash::Sha256] { + let (prefix, digest) = (hash.prefix(), hash.digest(b"payload")); + let mut block = vec![0xff; 128]; + block[0] = 0; + block[1] = 1; + let t = prefix.len() + digest.len(); + block[127 - t] = 0; + block[128 - t..128 - digest.len()].copy_from_slice(prefix); + block[128 - digest.len()..].copy_from_slice(&digest); + let signature = key.apply(&block, &hex(TEST_D)); + assert!(key.verify(hash, b"payload", &signature)); + assert!(!key.verify(hash, b"payloaD", &signature)); + } } } diff --git a/crates/horizon-protocol/src/secure_chat.rs b/crates/horizon-protocol/src/secure_chat.rs new file mode 100644 index 000000000..534c132f6 --- /dev/null +++ b/crates/horizon-protocol/src/secure_chat.rs @@ -0,0 +1,491 @@ +//! Secure chat as vanilla 26.3's server keeps it: a player's profile key, +//! validated against the services' keys (ProfilePublicKey, +//! RemoteChatSession), the message chain each player signs +//! (SignedMessageChain), what a signature covers (PlayerChatMessage, +//! SignedMessageLink, SignedMessageBody), the window of messages a client +//! acknowledges (LastSeenMessagesValidator) and the cache a connection packs +//! signatures against (MessageSignatureCache). +use crate::rsa::{RsaPublicKey, SignatureHash}; +use std::collections::{HashSet, VecDeque}; + +/// MessageSignature.BYTES. +pub const SIGNATURE_BYTES: usize = 256; +/// A message signature (MessageSignature). +pub type Signature = [u8; SIGNATURE_BYTES]; +/// LastSeenMessages.LAST_SEEN_MESSAGES_MAX_LENGTH: the window a client acknowledges. +pub const LAST_SEEN: usize = 20; +/// PlayerChatMessage.MESSAGE_EXPIRES_AFTER_SERVER, in milliseconds. +pub const EXPIRES_AFTER_SERVER: i64 = 5 * 60 * 1000; + +/// A signature from its packet bytes (always 256 on the wire). +pub fn signature(bytes: &[u8]) -> Option<Signature> { + bytes.try_into().ok() +} + +/// ProfilePublicKey.Data: the player's chat key (X.509 DER), when it +/// expires (epoch milliseconds), and the services' signature over both. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ProfileKey { + pub expires_at: i64, + pub key: Vec<u8>, + pub signature: Vec<u8>, +} + +impl ProfileKey { + /// Data.signedPayload: the profile id, the expiry and the key. + fn signed_payload(&self, profile: u128) -> Vec<u8> { + let mut out = Vec::with_capacity(24 + self.key.len()); + out.extend_from_slice(&profile.to_be_bytes()); + out.extend_from_slice(&self.expires_at.to_be_bytes()); + out.extend_from_slice(&self.key); + out + } + /// Data.validateSignature with the services' profile-key keys + /// (SignatureValidator.from(keySet, PROFILE_KEY): any of them, SHA1withRSA). + pub fn signed_by(&self, services: &[RsaPublicKey], profile: u128) -> bool { + let payload = self.signed_payload(profile); + services + .iter() + .any(|k| k.verify(SignatureHash::Sha1, &payload, &self.signature)) + } + /// Data.hasExpired. + pub fn has_expired(&self, now: i64) -> bool { + self.expires_at < now + } +} + +/// A validated RemoteChatSession: its id and the player's profile key. +#[derive(Clone, Debug)] +pub struct ChatSession { + pub id: u128, + pub profile_key: ProfileKey, + key: RsaPublicKey, +} + +impl ChatSession { + /// RemoteChatSession.Data.validate (ProfilePublicKey.createValidated); + /// the error is the disconnect message's translation key. + pub fn validate( + id: u128, + profile_key: ProfileKey, + services: &[RsaPublicKey], + profile: u128, + ) -> Result<Self, &'static str> { + const INVALID: &str = "multiplayer.disconnect.invalid_public_key_signature"; + let key = RsaPublicKey::from_der(&profile_key.key).map_err(|_| INVALID)?; + if !profile_key.signed_by(services, profile) { + return Err(INVALID); + } + Ok(Self { + id, + profile_key, + key, + }) + } + /// RemoteChatSession.Data, as ClientboundPlayerInfoUpdatePacket's + /// INITIALIZE_CHAT carries it. + pub fn data(&self) -> (u128, i64, Vec<u8>, Vec<u8>) { + let k = &self.profile_key; + (self.id, k.expires_at, k.key.clone(), k.signature.clone()) + } +} + +/// SignedMessageBody: what a player signed besides the chain link. +#[derive(Clone, Copy, Debug)] +pub struct Body<'a> { + pub content: &'a str, + /// Epoch milliseconds. + pub timestamp: i64, + pub salt: i64, + pub last_seen: &'a [Signature], +} + +/// PlayerChatMessage.updateSignature: the version, the link (sender, +/// session, index) and the body (salt, time in seconds, content, last seen). +pub fn signed_payload(sender: u128, session: u128, index: i32, body: &Body) -> Vec<u8> { + let content = body.content.as_bytes(); + let mut out = Vec::with_capacity(64 + content.len() + body.last_seen.len() * SIGNATURE_BYTES); + out.extend_from_slice(&1i32.to_be_bytes()); + out.extend_from_slice(&sender.to_be_bytes()); + out.extend_from_slice(&session.to_be_bytes()); + out.extend_from_slice(&index.to_be_bytes()); + out.extend_from_slice(&body.salt.to_be_bytes()); + out.extend_from_slice(&body.timestamp.div_euclid(1000).to_be_bytes()); + out.extend_from_slice(&(content.len() as i32).to_be_bytes()); + out.extend_from_slice(content); + out.extend_from_slice(&(body.last_seen.len() as i32).to_be_bytes()); + for s in body.last_seen { + out.extend_from_slice(s); + } + out +} + +/// SignedMessageChain.DecodeException's messages, and the signed command +/// mismatch (ServerGamePacketListenerImpl.INVALID_COMMAND_SIGNATURE). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum DecodeError { + MissingProfileKey, + ChainBroken, + ExpiredProfileKey, + InvalidSignature, + OutOfOrderChat, + InvalidCommandSignature, +} + +impl DecodeError { + pub fn key(self) -> &'static str { + match self { + Self::MissingProfileKey => "chat.disabled.missingProfileKey", + Self::ChainBroken => "chat.disabled.chain_broken", + Self::ExpiredProfileKey => "chat.disabled.expiredProfileKey", + Self::InvalidSignature => "chat.disabled.invalid_signature", + Self::OutOfOrderChat => "chat.disabled.out_of_order_chat", + Self::InvalidCommandSignature => "chat.disabled.invalid_command_signature", + } + } +} + +/// A message the chain let through: unsigned (PlayerChatMessage.unsigned) +/// or signed at a link of the sender's chain. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Unpacked { + Unsigned, + Signed { index: i32, signature: Signature }, +} + +/// A player's SignedMessageChain.Decoder: unsigned until the player sends a +/// chat session, then keyed to it. +#[derive(Clone, Debug, Default)] +pub struct Chain { + session: Option<ChatSession>, + /// The next link's index; None once the chain is broken. + next: Option<i32>, + /// The last message's time (lastTimeStamp, from Instant.EPOCH). + last_timestamp: i64, +} + +impl Chain { + /// RemoteChatSession.createMessageDecoder: a new chain from link 0. + pub fn keyed(session: ChatSession) -> Self { + Self { + session: Some(session), + next: Some(0), + last_timestamp: 0, + } + } + pub fn session(&self) -> Option<&ChatSession> { + self.session.as_ref() + } + /// Decoder.unpack for `sender`'s message. Without a session the message + /// is unsigned unless the server enforces secure chat; with one it must + /// be the next link, in order, signed by the player's key. + pub fn unpack( + &mut self, + sender: u128, + signature: Option<&Signature>, + body: &Body, + enforce: bool, + now: i64, + ) -> Result<Unpacked, DecodeError> { + let Some(session) = &self.session else { + return if enforce { + Err(DecodeError::MissingProfileKey) + } else { + Ok(Unpacked::Unsigned) + }; + }; + let signature = signature.ok_or(DecodeError::MissingProfileKey)?; + if session.profile_key.has_expired(now) { + return Err(DecodeError::ExpiredProfileKey); + } + let index = self.next.ok_or(DecodeError::ChainBroken)?; + if body.timestamp < self.last_timestamp { + self.next = None; + return Err(DecodeError::OutOfOrderChat); + } + self.last_timestamp = body.timestamp; + let payload = signed_payload(sender, session.id, index, body); + if !session + .key + .verify(SignatureHash::Sha256, &payload, signature) + { + self.next = None; + return Err(DecodeError::InvalidSignature); + } + self.next = index.checked_add(1); + Ok(Unpacked::Signed { + index, + signature: *signature, + }) + } + /// Decoder.setChainBroken (nothing for the unsigned decoder). + pub fn set_broken(&mut self) { + if self.session.is_some() { + self.next = None; + } + } +} + +/// Arrays.hashCode(byte[]). +fn java_hash(bytes: &[u8]) -> i32 { + bytes.iter().fold(1i32, |h, &b| { + h.wrapping_mul(31).wrapping_add(i32::from(b as i8)) + }) +} + +/// LastSeenMessages.computeChecksum: never 0, which means "not checked". +pub fn checksum(entries: &[Signature]) -> i8 { + let c = entries + .iter() + .fold(1i32, |c, e| c.wrapping_mul(31).wrapping_add(java_hash(e))); + match c as i8 { + 0 => 1, + b => b, + } +} + +/// LastSeenMessagesValidator: the messages sent to a player that its next +/// acknowledgement may name, each pending until it does. +#[derive(Clone, Debug)] +pub struct LastSeenValidator { + tracked: VecDeque<Option<(Signature, bool)>>, + last_pending: Option<Signature>, +} + +impl Default for LastSeenValidator { + fn default() -> Self { + Self { + tracked: std::iter::repeat_n(None, LAST_SEEN).collect(), + last_pending: None, + } + } +} + +impl LastSeenValidator { + pub fn add_pending(&mut self, signature: &Signature) { + if self.last_pending.as_ref() != Some(signature) { + self.tracked.push_back(Some((*signature, true))); + self.last_pending = Some(*signature); + } + } + pub fn tracked_count(&self) -> usize { + self.tracked.len() + } + /// applyOffset: drop the `offset` oldest messages. + pub fn apply_offset(&mut self, offset: i32) -> Result<(), String> { + let max = self.tracked.len() as i64 - LAST_SEEN as i64; + if offset >= 0 && i64::from(offset) <= max { + self.tracked.drain(..offset as usize); + Ok(()) + } else { + Err(format!( + "Advanced last seen window by {offset} messages, but expected at most {max}" + )) + } + } + /// applyUpdate: the offset, then the acknowledged messages of the + /// window (a 20-bit set, little-endian bytes), checked against the + /// client's checksum unless it sent 0. + pub fn apply_update( + &mut self, + offset: i32, + acknowledged: [u8; 3], + checksum: i8, + ) -> Result<Vec<Signature>, String> { + self.apply_offset(offset)?; + let bit = |i: usize| acknowledged[i / 8] >> (i % 8) & 1 != 0; + let length = (0..24).rev().find(|&i| bit(i)).map_or(0, |i| i + 1); + if length > LAST_SEEN { + return Err(format!( + "Last seen update contained {length} messages, but maximum window size is {LAST_SEEN}" + )); + } + let mut seen = Vec::with_capacity(LAST_SEEN); + for i in 0..LAST_SEEN { + let entry = self.tracked[i]; + if bit(i) { + let Some((s, _)) = entry else { + return Err(format!( + "Last seen update acknowledged unknown or previously ignored message at index {i}" + )); + }; + self.tracked[i] = Some((s, false)); + seen.push(s); + } else { + if let Some((_, false)) = entry { + return Err(format!( + "Last seen update ignored previously acknowledged message at index {i}" + )); + } + self.tracked[i] = None; + } + } + if checksum != 0 && checksum != self::checksum(&seen) { + return Err( + "Checksum mismatch on last seen update: the client and server must have desynced" + .to_owned(), + ); + } + Ok(seen) + } +} + +/// MessageSignatureCache: the 128 signatures a connection's client also +/// holds, so a packed signature can name one by index. +#[derive(Clone, Debug)] +pub struct SignatureCache { + entries: Vec<Option<Signature>>, +} + +impl Default for SignatureCache { + fn default() -> Self { + Self { + entries: vec![None; 128], + } + } +} + +impl SignatureCache { + /// MessageSignature.pack: the cache index, or the full signature. + pub fn pack(&self, signature: &Signature) -> (i32, Option<Vec<u8>>) { + match self + .entries + .iter() + .position(|e| e.as_ref() == Some(signature)) + { + Some(i) => (i as i32, None), + None => (-1, Some(signature.to_vec())), + } + } + /// push(body, signature): the message's last seen and its signature go + /// to the front, most recent first; what they displace moves back. + pub fn push(&mut self, last_seen: &[Signature], signature: Option<&Signature>) { + let mut queue: VecDeque<Signature> = last_seen.iter().chain(signature).copied().collect(); + let new: HashSet<Signature> = queue.iter().copied().collect(); + for slot in &mut self.entries { + let Some(next) = queue.pop_back() else { break }; + if let Some(old) = slot.replace(next) + && !new.contains(&old) + { + queue.push_front(old); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn sig(b: u8) -> Signature { + [b; SIGNATURE_BYTES] + } + + #[test] + fn payload_layout_matches_update_signature() { + let body = Body { + content: "hé", + timestamp: 1_700_000_123_456, + salt: -2, + last_seen: &[sig(7)], + }; + let p = signed_payload(1, 2, 3, &body); + assert_eq!(&p[..4], &[0, 0, 0, 1]); + assert_eq!(&p[4..20], &1u128.to_be_bytes()); + assert_eq!(&p[20..36], &2u128.to_be_bytes()); + assert_eq!(&p[36..40], &3i32.to_be_bytes()); + assert_eq!(&p[40..48], &(-2i64).to_be_bytes()); + assert_eq!(&p[48..56], &1_700_000_123i64.to_be_bytes()); + assert_eq!(&p[56..60], &3i32.to_be_bytes()); + assert_eq!(&p[60..63], "hé".as_bytes()); + assert_eq!(&p[63..67], &1i32.to_be_bytes()); + assert_eq!(p.len(), 67 + 256); + // Instant.getEpochSecond floors. + let before = Body { + timestamp: -1, + ..body + }; + assert_eq!( + &signed_payload(1, 2, 3, &before)[48..56], + &(-1i64).to_be_bytes() + ); + } + + #[test] + fn unsigned_chain_follows_enforcement() { + let mut chain = Chain::default(); + let body = Body { + content: "hi", + timestamp: 5, + salt: 0, + last_seen: &[], + }; + assert_eq!( + chain.unpack(1, None, &body, false, 0), + Ok(Unpacked::Unsigned) + ); + assert_eq!( + chain.unpack(1, Some(&sig(1)), &body, false, 0), + Ok(Unpacked::Unsigned) + ); + assert_eq!( + chain.unpack(1, None, &body, true, 0), + Err(DecodeError::MissingProfileKey) + ); + } + + #[test] + fn checksums_follow_java_hash_codes() { + // Arrays.hashCode(new byte[]{-1}) = 31 + (-1) = 30. + assert_eq!(java_hash(&[0xff]), 30); + assert_eq!(checksum(&[]), 1); + let one = [sig(0)]; + let expected = 31i32.wrapping_add(java_hash(&sig(0))) as i8; + assert_eq!(checksum(&one), if expected == 0 { 1 } else { expected }); + } + + #[test] + fn last_seen_window() { + let mut v = LastSeenValidator::default(); + assert_eq!(v.tracked_count(), 20); + v.add_pending(&sig(1)); + v.add_pending(&sig(1)); + v.add_pending(&sig(2)); + assert_eq!(v.tracked_count(), 22); + assert!(v.apply_offset(3).is_err()); + // Slide by two: the two messages are the window's last entries. + let mut ack = [0u8; 3]; + ack[18 / 8] |= 1 << (18 % 8); + ack[19 / 8] |= 1 << (19 % 8); + let seen = v.apply_update(2, ack, checksum(&[sig(1), sig(2)])).unwrap(); + assert_eq!(seen, vec![sig(1), sig(2)]); + // Once acknowledged, a message can't be ignored. + assert!(v.apply_update(0, [0; 3], 0).is_err()); + let mut v = LastSeenValidator::default(); + v.add_pending(&sig(1)); + assert!(v.apply_update(1, [0, 0, 0x10], 0).is_err(), "bit 20"); + let mut v = LastSeenValidator::default(); + assert!(v.apply_update(0, [1, 0, 0], 0).is_err(), "unknown"); + let mut v = LastSeenValidator::default(); + v.add_pending(&sig(1)); + let mut ack = [0u8; 3]; + ack[2] |= 1 << 3; + assert!(v.apply_update(1, ack, 99).is_err(), "checksum"); + } + + #[test] + fn cache_moves_recent_signatures_first() { + let mut c = SignatureCache::default(); + assert_eq!(c.pack(&sig(1)), (-1, Some(sig(1).to_vec()))); + c.push(&[], Some(&sig(1))); + assert_eq!(c.pack(&sig(1)), (0, None)); + c.push(&[sig(1)], Some(&sig(2))); + assert_eq!(c.pack(&sig(2)), (0, None)); + assert_eq!(c.pack(&sig(1)), (1, None)); + c.push(&[], Some(&sig(3))); + assert_eq!( + [c.pack(&sig(3)).0, c.pack(&sig(2)).0, c.pack(&sig(1)).0], + [0, 1, 2] + ); + } +} diff --git a/crates/horizon-server-files/Cargo.toml b/crates/horizon-server-files/Cargo.toml index 52114718d..2643fc726 100644 --- a/crates/horizon-server-files/Cargo.toml +++ b/crates/horizon-server-files/Cargo.toml @@ -4,13 +4,17 @@ version = "0.1.0" edition.workspace = true license.workspace = true rust-version = "1.90" -description = "The dedicated server's vanilla files: server.properties, eula.txt, ops, whitelist and bans" +description = "The dedicated server's vanilla files: server.properties, eula.txt, ops, whitelist and bans, and account lookups" [dependencies] horizon-core = { path = "../horizon-core" } horizon-protocol = { path = "../horizon-protocol" } horizon-world = { path = "../horizon-world" } log = "0.4" +# Online mode: finding accounts by name (the services' profiles endpoint). +# Secure chat: the services' public keys arrive base64-encoded. +base64 = "0.22" +ureq = { version = "2", default-features = false, features = ["tls"] } [lints] workspace = true diff --git a/crates/horizon-server-files/src/json.rs b/crates/horizon-server-files/src/json.rs index 8e1d865b2..47fe6d5d3 100644 --- a/crates/horizon-server-files/src/json.rs +++ b/crates/horizon-server-files/src/json.rs @@ -78,6 +78,43 @@ impl Json { } } + /// `new GsonBuilder().create().toJson(element)`: compact, HTML-safe + /// strings, null members skipped. + pub fn gson(&self) -> String { + let mut out = String::new(); + self.write_gson(&mut out); + out + } + fn write_gson(&self, out: &mut String) { + match self { + Json::String(s) => quote(out, s, true), + Json::Array(items) => { + out.push('['); + for (i, v) in items.iter().enumerate() { + if i > 0 { + out.push(','); + } + v.write_gson(out); + } + out.push(']'); + } + Json::Object(m) => { + out.push('{'); + let members = m.iter().filter(|(_, v)| *v != Json::Null); + for (i, (k, v)) in members.enumerate() { + if i > 0 { + out.push(','); + } + quote(out, k, true); + out.push(':'); + v.write_gson(out); + } + out.push('}'); + } + other => other.write_compact(out), + } + } + /// `new GsonBuilder().setPrettyPrinting().create().toJson(element, writer)`: /// two-space indents, HTML-safe strings, null members skipped. pub fn pretty(&self) -> String { diff --git a/crates/horizon-server-files/src/lib.rs b/crates/horizon-server-files/src/lib.rs index 30c75c869..08c059905 100644 --- a/crates/horizon-server-files/src/lib.rs +++ b/crates/horizon-server-files/src/lib.rs @@ -1,10 +1,13 @@ //! The dedicated server's files and settings, ported from //! net.minecraft.server.dedicated and net.minecraft.server.players: -//! server.properties, eula.txt and the four user lists. The game's dedicated -//! server (`horizon-client --dedicated`) reads them, and `horizon-server` -//! manages them; this crate needs neither a window nor the game. +//! server.properties, eula.txt, the four user lists, usercache.json and +//! looking players up by name. The game's dedicated server +//! (`horizon-client --dedicated`) reads them, and `horizon-server` manages +//! them; this crate needs neither a window nor the game. pub mod eula; pub mod java_properties; pub mod json; +pub mod profiles; pub mod properties; +pub mod user_cache; pub mod user_lists; diff --git a/crates/horizon-server-files/src/profiles.rs b/crates/horizon-server-files/src/profiles.rs new file mode 100644 index 000000000..3e16e2a41 --- /dev/null +++ b/crates/horizon-server-files/src/profiles.rs @@ -0,0 +1,168 @@ +//! Finding a player's account by name, as an online-mode server does for the +//! names given to `op`, `whitelist` and `ban` (CachedUserNameToIdResolver, +//! MinecraftServicesProfileRepository.findProfileByName): the services' +//! `profiles` / `getByName` endpoint, found through the discovery document. +use crate::{ + json::{self, Json}, + user_lists::NameAndId, +}; +use horizon_protocol::rsa::RsaPublicKey; +use std::time::Duration; + +/// Mojang's services discovery document. +pub const DEFAULT_DISCOVERY: &str = "https://discovery.minecraftservices.com/minecraft/client"; + +/// The discovery document's URL: `HORIZON_API_DISCOVERY` overrides it like +/// authlib's `-Dminecraft.api.discovery.host`. +pub fn discovery_url() -> String { + std::env::var("HORIZON_API_DISCOVERY") + .ok() + .filter(|u| !u.is_empty()) + .unwrap_or_else(|| DEFAULT_DISCOVERY.to_owned()) +} + +/// StringUtil.isValidPlayerName: at most 16 UTF-16 units, none a control +/// character, a space or above ASCII. +pub fn valid_player_name(name: &str) -> bool { + name.encode_utf16().count() <= 16 && name.encode_utf16().all(|c| c > 32 && c < 127) +} + +/// How a server resolves a name it was given (CachedUserNameToIdResolver.get, +/// without the usercache.json cache): an offline server makes the offline +/// profile; an online one asks the services (for the name in lower case) and +/// knows nobody they don't. +pub fn resolve(name: &str, online: bool) -> Option<NameAndId> { + if online && valid_player_name(name) { + find_by_name(&discovery_url(), &name.to_ascii_lowercase()) + } else { + (!online).then(|| NameAndId::offline(name)) + } +} + +/// MinecraftServicesProfileRepository.findProfileByName: the account named +/// `name` (any case), or None when there is none or the services don't +/// answer (vanilla logs both as "Couldn't find profile with name"). +pub fn find_by_name(discovery: &str, name: &str) -> Option<NameAndId> { + let agent = ureq::AgentBuilder::new() + .timeout(Duration::from_secs(15)) + .user_agent(concat!("Horizon/", env!("CARGO_PKG_VERSION"))) + .build(); + let doc = json::parse(&agent.get(discovery).call().ok()?.into_string().ok()?, true).ok()?; + let url = endpoint(&doc, "profiles", "getByName")?.replace("{name}", &name.to_lowercase()); + found(&agent.get(&url).call().ok()?.into_string().ok()?, name) +} + +/// MinecraftServicesKeyInfo.get: the services' public keys from the +/// discovery document's `authentication` / `getPublicKeys` endpoint. The +/// profile-key keys (`playerCertificateKeys`, ServicesKeyType.PROFILE_KEY) +/// sign the players' chat keys; secure chat checks them. The error is the +/// line vanilla logs, after which it has no keys. +pub fn fetch_profile_key_keys(discovery: &str) -> Result<Vec<RsaPublicKey>, &'static str> { + const FAILED: &str = "Failed to request Minecraft Services public key"; + let agent = ureq::AgentBuilder::new() + .timeout(Duration::from_secs(15)) + .user_agent(concat!("Horizon/", env!("CARGO_PKG_VERSION"))) + .build(); + let get = |url: &str| agent.get(url).call().ok()?.into_string().ok(); + let doc = get(discovery) + .and_then(|d| json::parse(&d, true).ok()) + .ok_or(FAILED)?; + let url = endpoint(&doc, "authentication", "getPublicKeys").ok_or(FAILED)?; + profile_key_keys(&get(&url).ok_or(FAILED)?) +} + +/// The key set answer: `{"profilePropertyKeys": [...], "playerCertificateKeys": +/// [...]}`, each `{"publicKey": base64 X.509}`. One bad key makes the whole +/// answer malformed (parseList throws). +pub fn profile_key_keys(answer: &str) -> Result<Vec<RsaPublicKey>, &'static str> { + use base64::Engine; + const MALFORMED: &str = "Received malformed Minecraft Services public key data"; + let doc = json::parse(answer, true).map_err(|_| MALFORMED)?; + let list = |name: &str| -> Result<Vec<RsaPublicKey>, &'static str> { + let Some(entries) = doc.get(name) else { + return Ok(Vec::new()); + }; + let Json::Array(entries) = entries else { + return Err(MALFORMED); + }; + entries + .iter() + .map(|e| { + let text = e + .get("publicKey") + .and_then(Json::as_string) + .ok_or(MALFORMED)?; + let der = base64::engine::general_purpose::STANDARD + .decode(text) + .map_err(|_| MALFORMED)?; + RsaPublicKey::from_der(&der).map_err(|_| MALFORMED) + }) + .collect() + }; + list("profilePropertyKeys")?; + list("playerCertificateKeys") +} + +/// MinecraftServicesDiscoveryService.getUrl(service, endpoint). +fn endpoint(doc: &Json, service: &str, name: &str) -> Option<String> { + doc.get("discovery")? + .get(service)? + .get("endpoints")? + .get(name)? + .get("uri")? + .as_string() +} + +/// The answer, authlib's NameAndId: `{"id", "name"}`, the id undashed. +pub fn found(answer: &str, name: &str) -> Option<NameAndId> { + let profile = json::parse(answer, true).ok()?; + let found = profile.get("name")?.as_string()?; + let id = profile.get("id")?.as_string()?.replace('-', ""); + (found.eq_ignore_ascii_case(name) && id.len() == 32).then_some(())?; + Some(NameAndId { + id: u128::from_str_radix(&id, 16).ok()?, + name: found, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn names_and_answers() { + assert!(valid_player_name("Alex_123")); + assert!(!valid_player_name("two words")); + assert!(!valid_player_name("ThisNameIsTooLong")); + assert!(!valid_player_name("caf\u{e9}")); + assert_eq!(resolve("Alex", false), Some(NameAndId::offline("Alex"))); + assert_eq!(resolve("not valid", true), None); + let answer = r#"{"id":"069a79f444e94726a5befca90e38aaf5","name":"Notch"}"#; + assert_eq!( + found(answer, "notch"), + Some(NameAndId { + id: 0x069a79f444e94726a5befca90e38aaf5, + name: "Notch".into() + }) + ); + assert_eq!(found(answer, "Steve"), None); + assert_eq!(found("{}", "Notch"), None); + assert_eq!(found("", "Notch"), None); + } + + #[test] + fn services_key_sets() { + // The 1024-bit test key of horizon-protocol's RSA tests, base64. + let key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrAmB0+SRQqPcm84lbsRF2yo8GXzEAzUFDusT302dO3P+VtBoFJdI2WlScEG7GaFiBs5IXdCD9Mf9cFE/xG6JDEedRzSxrXRgXtq9mhqQ8ldYSQ2EwBeaUW2CTzZUiPOxeiFaCbanS7hsyM2KsHOvqs2WcvEUxmptUDdqBakeY/wIDAQAB"; + let answer = format!( + r#"{{"profilePropertyKeys":[{{"publicKey":"{key}"}}],"playerCertificateKeys":[{{"publicKey":"{key}"}},{{"publicKey":"{key}"}}]}}"# + ); + let keys = profile_key_keys(&answer).unwrap(); + assert_eq!(keys.len(), 2); + assert_eq!(keys[0].size(), 128); + assert_eq!(profile_key_keys("{}").unwrap().len(), 0); + let bad = r#"{"profilePropertyKeys":[{"publicKey":"AAAA"}],"playerCertificateKeys":[]}"#; + assert!(profile_key_keys(bad).is_err()); + assert!(profile_key_keys("not json").is_err()); + } +} diff --git a/crates/horizon-server-files/src/user_cache.rs b/crates/horizon-server-files/src/user_cache.rs new file mode 100644 index 000000000..c8ccd6cf0 --- /dev/null +++ b/crates/horizon-server-files/src/user_cache.rs @@ -0,0 +1,318 @@ +//! usercache.json (CachedUserNameToIdResolver): the players the server has +//! seen join or has looked up by name, so commands find a player by name +//! without asking the profile service again. Each entry expires a month after +//! it was added; the file keeps the 1000 most recently used. +use crate::{ + java_properties::{civil_from_days, days_from_civil, now_millis}, + json::{self, Json}, + profiles::valid_player_name, + user_lists::{NameAndId, format_date, parse_date}, +}; +use std::{collections::HashMap, path::PathBuf}; + +const MRU_LIMIT: usize = 1000; + +#[derive(Clone, Debug)] +struct Info { + user: NameAndId, + expires: i64, + last_access: u64, +} + +/// `Calendar.add(MONTH, 1)` on a UTC time: the same day and time a month +/// later, the day clamped to the end of a shorter month. +pub fn one_month_later(millis: i64) -> i64 { + let days = millis.div_euclid(86_400_000); + let time = millis.rem_euclid(86_400_000); + let (y, m, d) = civil_from_days(days); + let (y, m) = if m == 12 { (y + 1, 1) } else { (y, m + 1) }; + let last = days_from_civil( + if m == 12 { y + 1 } else { y }, + if m == 12 { 1 } else { m + 1 }, + 1, + ) - days_from_civil(y, m, 1); + let d = d.min(last as u32); + days_from_civil(y, m, d) * 86_400_000 + time +} + +pub struct UserCache { + file: PathBuf, + /// Every entry ever added; the maps point into it, as vanilla's two maps + /// share their GameProfileInfo objects. + infos: Vec<Info>, + by_name: HashMap<String, usize>, + by_uuid: HashMap<u128, usize>, + operations: u64, + /// Off on an online-mode server: a name the profile service doesn't know + /// is then no player at all, rather than an offline one. + pub resolve_offline_users: bool, +} + +impl UserCache { + /// The cache in `file`, loaded; also returns the warnings vanilla logs. + pub fn load(file: impl Into<PathBuf>) -> (Self, Vec<String>) { + let mut cache = Self { + file: file.into(), + infos: Vec::new(), + by_name: HashMap::new(), + by_uuid: HashMap::new(), + operations: 0, + resolve_offline_users: true, + }; + let mut warnings = Vec::new(); + let entries = match std::fs::read_to_string(&cache.file) { + Ok(text) => cache.parse(&text, &mut warnings), + Err(_) => Vec::new(), + }; + for info in entries.into_iter().rev() { + cache.safe_add(info); + } + (cache, warnings) + } + + fn parse(&self, text: &str, warnings: &mut Vec<String>) -> Vec<Info> { + let items = match json::parse(text, false) { + Ok(Json::Array(items)) => items, + // An empty file is Gson's null. + Ok(Json::Null) => return Vec::new(), + _ if text.trim().is_empty() => return Vec::new(), + _ => { + warnings.push(format!( + "Failed to load profile cache {}", + self.file.display() + )); + return Vec::new(); + } + }; + let mut out = Vec::new(); + for item in &items { + if !matches!(item, Json::Object(_)) { + continue; + } + let Some(user) = NameAndId::from_json(item) else { + continue; + }; + let Some(date) = item.get("expiresOn").and_then(Json::as_string) else { + continue; + }; + match parse_date(&date) { + Some(expires) => out.push(Info { + user, + expires, + last_access: 0, + }), + None => warnings.push(format!("Failed to parse date {date}")), + } + } + out + } + + fn safe_add(&mut self, mut info: Info) -> usize { + self.operations += 1; + info.last_access = self.operations; + let index = self.infos.len(); + self.by_name.insert(info.user.name.to_lowercase(), index); + self.by_uuid.insert(info.user.id, index); + self.infos.push(info); + index + } + + /// `add`: a player who joined, remembered for a month; saves the file. + pub fn add(&mut self, user: NameAndId) { + self.add_internal(user, now_millis()); + } + + fn add_internal(&mut self, user: NameAndId, now: i64) -> NameAndId { + let index = self.safe_add(Info { + user, + expires: one_month_later(now), + last_access: 0, + }); + self.save(); + self.infos[index].user.clone() + } + + /// `get(String)`: the player with this name (any case). An expired entry is + /// dropped; a name not cached is asked of `lookup` (the profile service, + /// given the lower-case name) and, failing that, made an offline player + /// when offline users resolve. What is found is cached. + pub fn get_by_name( + &mut self, + name: &str, + lookup: impl FnOnce(&str) -> Option<NameAndId>, + ) -> Option<NameAndId> { + self.get_by_name_at(name, now_millis(), lookup) + } + + fn get_by_name_at( + &mut self, + name: &str, + now: i64, + lookup: impl FnOnce(&str) -> Option<NameAndId>, + ) -> Option<NameAndId> { + let name = name.to_lowercase(); + let mut found = self.by_name.get(&name).copied(); + let mut needs_save = false; + if let Some(index) = found + && now >= self.infos[index].expires + { + let user = self.infos[index].user.clone(); + self.by_uuid.remove(&user.id); + self.by_name.remove(&user.name.to_lowercase()); + needs_save = true; + found = None; + } + let result = if let Some(index) = found { + self.operations += 1; + self.infos[index].last_access = self.operations; + Some(self.infos[index].user.clone()) + } else { + let profile = if valid_player_name(&name) { + lookup(&name) + } else { + None + }; + let profile = profile.or_else(|| { + self.resolve_offline_users + .then(|| NameAndId::offline(&name)) + }); + match profile { + Some(profile) => { + needs_save = false; + Some(self.add_internal(profile, now)) + } + None => None, + } + }; + if needs_save { + self.save(); + } + result + } + + /// `get(UUID)`: a cached player by id (expired or not). + pub fn get_by_id(&mut self, id: u128) -> Option<NameAndId> { + let index = *self.by_uuid.get(&id)?; + self.operations += 1; + self.infos[index].last_access = self.operations; + Some(self.infos[index].user.clone()) + } + + /// The file's text: the 1000 most recently used entries, most recent + /// first, as Gson writes them (compact, HTML-safe). + pub fn to_json(&self) -> String { + let mut entries: Vec<&Info> = self.by_uuid.values().map(|&i| &self.infos[i]).collect(); + entries.sort_by_key(|info| std::cmp::Reverse(info.last_access)); + let items = entries + .into_iter() + .take(MRU_LIMIT) + .map(|info| { + let mut o = Json::Object(Vec::new()); + info.user.append_to(&mut o); + o.add("expiresOn", Json::String(format_date(info.expires))); + o + }) + .collect(); + Json::Array(items).gson() + } + + /// `save`: errors are ignored, as vanilla does. + pub fn save(&self) { + let _ = std::fs::write(&self.file, self.to_json()); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const DAY: i64 = 86_400_000; + + fn cache(text: &str) -> (UserCache, Vec<String>) { + let dir = std::env::temp_dir().join(format!( + "horizon-usercache-{}-{}", + std::process::id(), + text.len() + )); + std::fs::create_dir_all(&dir).unwrap(); + let file = dir.join("usercache.json"); + std::fs::write(&file, text).unwrap(); + UserCache::load(file) + } + + #[test] + fn months() { + let jan31 = days_from_civil(2026, 1, 31) * DAY + 3_723_000; + assert_eq!( + format_date(one_month_later(jan31)), + "2026-02-28 01:02:03 +0000" + ); + let dec15 = days_from_civil(2026, 12, 15) * DAY; + assert_eq!( + format_date(one_month_later(dec15)), + "2027-01-15 00:00:00 +0000" + ); + let jan31_leap = days_from_civil(2028, 1, 31) * DAY; + assert_eq!( + format_date(one_month_later(jan31_leap)), + "2028-02-29 00:00:00 +0000" + ); + } + + #[test] + fn keeps_order_and_skips_bad_entries() { + let (c, warnings) = cache( + r#"[{"uuid":"069a79f4-44e9-4726-a5be-fca90e38aaf5","name":"Notch","expiresOn":"2099-01-01 00:00:00 +0000"}, + 1, {"name":"NoId"}, {"uuid":"853c80ef-3c37-49fd-aa49-938b674adae6","name":"NoDate"}, + {"uuid":"853c80ef-3c37-49fd-aa49-938b674adae6","name":"BadDate","expiresOn":"soon"}, + {"uuid":"61699b2e-d327-4a01-9f1e-0ea8c3f06bc6","name":"Alex","expiresOn":"2099-01-01 00:00:00 +0000"}]"#, + ); + assert_eq!(warnings, vec!["Failed to parse date soon".to_owned()]); + assert_eq!( + c.to_json(), + r#"[{"uuid":"069a79f4-44e9-4726-a5be-fca90e38aaf5","name":"Notch","expiresOn":"2099-01-01 00:00:00 +0000"},{"uuid":"61699b2e-d327-4a01-9f1e-0ea8c3f06bc6","name":"Alex","expiresOn":"2099-01-01 00:00:00 +0000"}]"# + ); + } + + #[test] + fn names_resolve_through_the_cache() { + let (mut c, _) = cache( + r#"[{"uuid":"069a79f4-44e9-4726-a5be-fca90e38aaf5","name":"Notch","expiresOn":"2099-01-01 00:00:00 +0000"}, + {"uuid":"61699b2e-d327-4a01-9f1e-0ea8c3f06bc6","name":"Old","expiresOn":"2000-01-01 00:00:00 +0000"}]"#, + ); + let now = days_from_civil(2026, 10, 17) * DAY; + let never = |_: &str| -> Option<NameAndId> { panic!("looked up") }; + // A hit in any case, and it becomes the most recent. + let notch = c.get_by_name_at("NOTCH", now, never).unwrap(); + assert_eq!(notch.name, "Notch"); + // Expired: dropped and looked up again, by its lower-case name. + let mut asked = Vec::new(); + let old = c + .get_by_name_at("Old", now, |n| { + asked.push(n.to_owned()); + None + }) + .unwrap(); + assert_eq!(asked, ["old"]); + assert_eq!(old, NameAndId::offline("old")); + // Not a valid name: never looked up. + let odd = c.get_by_name_at("a b", now, never).unwrap(); + assert_eq!(odd, NameAndId::offline("a b")); + c.resolve_offline_users = false; + assert_eq!(c.get_by_name_at("Nobody", now, |_| None), None); + let found = NameAndId { + id: 7, + name: "Steve".into(), + }; + let steve = c.get_by_name_at("steve", now, |_| Some(found.clone())); + assert_eq!(steve, Some(found)); + let json = c.to_json(); + let names: Vec<&str> = json + .split("\"name\":\"") + .skip(1) + .map(|s| s.split('"').next().unwrap()) + .collect(); + assert_eq!(names, ["Steve", "a b", "old", "Notch"]); + assert!(json.contains(r#""name":"Steve","expiresOn":"2026-11-17 00:00:00 +0000"}"#)); + } +} diff --git a/crates/horizon-server-files/src/user_lists.rs b/crates/horizon-server-files/src/user_lists.rs index 3d35aa62f..77de64e31 100644 --- a/crates/horizon-server-files/src/user_lists.rs +++ b/crates/horizon-server-files/src/user_lists.rs @@ -39,7 +39,7 @@ impl NameAndId { name: name.to_owned(), } } - fn from_json(o: &Json) -> Option<Self> { + pub(crate) fn from_json(o: &Json) -> Option<Self> { let uuid = o.get("uuid")?.as_string()?; let name = o.get("name")?.as_string()?; Some(Self { @@ -47,7 +47,7 @@ impl NameAndId { name, }) } - fn append_to(&self, o: &mut Json) { + pub(crate) fn append_to(&self, o: &mut Json) { o.add("uuid", Json::String(uuid_string(self.id))); o.add("name", Json::String(self.name.clone())); } diff --git a/crates/horizon-server/Cargo.toml b/crates/horizon-server/Cargo.toml index c3800b80c..ec3b9bbc4 100644 --- a/crates/horizon-server/Cargo.toml +++ b/crates/horizon-server/Cargo.toml @@ -10,11 +10,17 @@ description = "Host a Horizon server: server.toml, the CLI and the supervisor th horizon-server-files = { path = "../horizon-server-files" } # The game process's RAM and CPU (the same probes the benchmark reports use). horizon-bench = { path = "../horizon-bench" } +# The log's local times, as the game prints them. +chrono = { version = "0.4.42", default-features = false, features = ["clock", "std"] } serde = { version = "1", features = ["derive"] } 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"] } diff --git a/crates/horizon-server/panel/index.html b/crates/horizon-server/panel/index.html index 051b7283e..5d77cb956 100644 --- a/crates/horizon-server/panel/index.html +++ b/crates/horizon-server/panel/index.html @@ -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> diff --git a/crates/horizon-server/panel/panel.css b/crates/horizon-server/panel/panel.css index 5d635d509..7946b5f8f 100644 --- a/crates/horizon-server/panel/panel.css +++ b/crates/horizon-server/panel/panel.css @@ -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); +} diff --git a/crates/horizon-server/panel/panel.js b/crates/horizon-server/panel/panel.js index f74c3e1c2..5405907ca 100644 --- a/crates/horizon-server/panel/panel.js +++ b/crates/horizon-server/panel/panel.js @@ -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)); diff --git a/crates/horizon-server/src/api.rs b/crates/horizon-server/src/api.rs index 85bc0e2b5..03f7ae079 100644 --- a/crates/horizon-server/src/api.rs +++ b/crates/horizon-server/src/api.rs @@ -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, ¤t) { + 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"), } diff --git a/crates/horizon-server/src/cli.rs b/crates/horizon-server/src/cli.rs index 11d9fb6b6..69a192105 100644 --- a/crates/horizon-server/src/cli.rs +++ b/crates/horizon-server/src/cli.rs @@ -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" + ); } } diff --git a/crates/horizon-server/src/config.rs b/crates/horizon-server/src/config.rs index c62869e27..3c389f858 100644 --- a/crates/horizon-server/src/config.rs +++ b/crates/horizon-server/src/config.rs @@ -23,7 +23,7 @@ motd = "A Minecraft Server" port = 25565 bind = "" # listen on every address, or only on this one (server-ip) max_players = 20 -online_mode = true # not supported by Horizon yet: the server runs offline and says so +online_mode = true # check players' Minecraft accounts (false: anyone joins under any name) whitelist = true # only whitelisted players and operators can join enforce_whitelist = false # kick online players taken off the whitelist view_distance = 10 diff --git a/crates/horizon-server/src/engine.rs b/crates/horizon-server/src/engine.rs index c0043e1d3..051ae77f0 100644 --- a/crates/horizon-server/src/engine.rs +++ b/crates/horizon-server/src/engine.rs @@ -11,15 +11,27 @@ use std::{ time::Duration, }; -/// The game's program names, most specific first: a headless engine once there is one, else the game. +/// The game's program names, most specific first: the server build (the game without sound or +/// controllers, so it needs neither ALSA nor udev on Linux; `horizon-server-linux-x86_64.tar.gz` holds it), +/// else the full game. const PROGRAMS: [&str; 2] = ["horizon-engine", "horizon-client"]; 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 @@ -27,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(); @@ -49,6 +64,55 @@ pub fn program(config: &Config, dir: &Path) -> Result<PathBuf, String> { }) } +/// Checks that the game program starts at all: `--help` loads no game files, so it fails only when the +/// system can't load the program, as on a Linux without the ALSA and udev libraries the full game links +/// (the server build doesn't). The reason, with what to install. +pub fn check_starts(program: &Path) -> Result<(), String> { + let output = Command::new(program) + .arg("--help") + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::piped()) + .output() + .map_err(|e| format!("the game ({}) can't start: {e}", program.display()))?; + if output.status.success() { + return Ok(()); + } + let stderr = String::from_utf8_lossy(&output.stderr); + Err(start_failure(program, output.status.code(), &stderr)) +} + +/// What to tell the owner when the game program exits at once. +fn start_failure(program: &Path, code: Option<i32>, stderr: &str) -> String { + let reason = stderr + .lines() + .find(|l| !l.trim().is_empty()) + .unwrap_or("") + .trim(); + let mut text = format!( + "the game ({}) can't start{}{}", + program.display(), + code.map_or(String::new(), |c| format!(" (exit status {c})")), + if reason.is_empty() { + String::new() + } else { + format!(": {reason}") + } + ); + if reason.contains("error while loading shared libraries") + && (reason.contains("libasound") || reason.contains("libudev")) + { + text.push_str( + "\nThe full game needs ALSA's and udev's libraries (libasound.so.2, libudev.so.1). Install the \ + server build, which needs neither (packaging/server/install.sh picks it on Linux), or the \ + libraries:\n Debian, Ubuntu: sudo apt install libasound2 libudev1 (Ubuntu 24.04 and later: \ + libasound2t64)\n Fedora, RHEL: sudo dnf install alsa-lib systemd-libs\n Arch: sudo pacman -S \ + alsa-lib systemd-libs\n NixOS: enable programs.nix-ld with alsa-lib and udev in its libraries", + ); + } + text +} + /// Where the prepared game files may be, in the order they are tried. pub fn resource_candidates(config: &Config, dir: &Path, program: Option<&Path>) -> Vec<PathBuf> { if !config.engine.resources.is_empty() { @@ -65,12 +129,13 @@ pub fn resource_candidates(config: &Config, dir: &Path, program: Option<&Path>) out } -/// The prepared game files: a folder holding the asset index `horizon-prepare` writes. +/// The prepared game files: a folder holding the manifest every `horizon-prepare` run writes +/// (`asset-index.json` comes only with the optional sounds, which a server doesn't need). pub fn resources(config: &Config, dir: &Path, program: Option<&Path>) -> Result<PathBuf, String> { let candidates = resource_candidates(config, dir, program); candidates .iter() - .find(|d| d.join("asset-index.json").is_file()) + .find(|d| d.join("horizon-resources.json").is_file()) .cloned() .ok_or_else(|| { let tried: Vec<String> = candidates @@ -89,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()) @@ -179,4 +248,105 @@ mod tests { [PathBuf::from("/data/res")] ); } + + #[test] + fn resources_prepared_without_sounds_are_found() { + let root = std::env::temp_dir().join(format!("horizon-server-res-{}", std::process::id())); + let prepared = root.join(".local/horizon-client/26.3"); + std::fs::create_dir_all(&prepared).unwrap(); + let program = root.join("horizon-client"); + let config = Config::default(); + let server = root.join("server"); + assert!(resources(&config, &server, Some(&program)).is_err()); + // What `horizon-prepare` writes on every run; no asset index without `horizon-prepare sounds`. + std::fs::write(prepared.join("horizon-resources.json"), "{}").unwrap(); + assert_eq!(resources(&config, &server, Some(&program)), Ok(prepared)); + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn a_missing_library_says_what_to_install() { + let program = Path::new("/opt/horizon/horizon-client"); + let text = start_failure( + program, + Some(127), + "/opt/horizon/horizon-client: error while loading shared libraries: libasound.so.2: cannot open \ + shared object file: No such file or directory\n", + ); + assert!(text.starts_with( + "the game (/opt/horizon/horizon-client) can't start (exit status 127): /opt/horizon/horizon-client: \ + error while loading shared libraries: libasound.so.2" + )); + assert!(text.contains("sudo apt install libasound2 libudev1")); + // Another library: no ALSA advice (the server build, horizon-engine, links neither). + let engine = start_failure( + Path::new("/opt/horizon/horizon-engine"), + Some(127), + "/opt/horizon/horizon-engine: error while loading shared libraries: libgcc_s.so.1: cannot open \ + shared object file: No such file or directory\n", + ); + assert!(!engine.contains("libasound"), "{engine}"); + let other = start_failure(program, Some(1), "something else\n"); + assert_eq!( + other, + "the game (/opt/horizon/horizon-client) can't start (exit status 1): something else" + ); + } + + #[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() { + use std::os::unix::fs::PermissionsExt; + let root = + std::env::temp_dir().join(format!("horizon-server-check-{}", std::process::id())); + std::fs::create_dir_all(&root).unwrap(); + let script = |name: &str, body: &str| { + let path = root.join(name); + std::fs::write(&path, format!("#!/bin/sh\n{body}\n")).unwrap(); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap(); + path + }; + assert_eq!(check_starts(&script("starts", "exit 0")), Ok(())); + let fails = script("fails", "echo 'no luck' >&2; exit 3"); + assert_eq!( + check_starts(&fails), + Err(format!( + "the game ({}) can't start (exit status 3): no luck", + fails.display() + )) + ); + std::fs::remove_dir_all(&root).unwrap(); + } } diff --git a/crates/horizon-server/src/init.rs b/crates/horizon-server/src/init.rs index 37e43df80..bd595cddb 100644 --- a/crates/horizon-server/src/init.rs +++ b/crates/horizon-server/src/init.rs @@ -6,7 +6,7 @@ use crate::{config, config::Config, engine}; use horizon_server_files::{ eula, java_properties::now_millis, - user_lists::{NameAndId, OpEntry, OpList, WhiteEntry, WhiteList}, + user_lists::{OpEntry, OpList, WhiteEntry, WhiteList}, }; use std::{ io::{BufRead, IsTerminal, Write}, @@ -23,6 +23,8 @@ pub struct Answers { pub whitelist: Option<bool>, pub players: Vec<String>, pub op: Option<String>, + /// online-mode (true unless set): players' Minecraft accounts are checked. + pub online: Option<bool>, /// Take the default for every question not answered by a flag. pub yes: bool, } @@ -30,7 +32,7 @@ pub struct Answers { impl Answers { /// The answers in the environment, for containers: `HORIZON_EULA=true`, `HORIZON_WORLD`, /// `HORIZON_SEED`, `HORIZON_PORT`, `HORIZON_WHITELIST=true|false`, `HORIZON_PLAYERS` (names separated by - /// commas) and `HORIZON_OP`. Flags given on the command line win. + /// commas), `HORIZON_OP` and `HORIZON_ONLINE=true|false`. Flags given on the command line win. pub fn from_env() -> Result<Self, String> { let var = |name: &str| std::env::var(name).ok().filter(|v| !v.is_empty()); let flag = |name: &str| var(name).map(|v| v.eq_ignore_ascii_case("true")); @@ -54,6 +56,7 @@ impl Answers { }) .unwrap_or_default(), op: var("HORIZON_OP"), + online: flag("HORIZON_ONLINE"), yes: false, }) } @@ -172,6 +175,9 @@ pub fn run(dir: &Path, answers: Answers) -> Result<(), String> { } // 4. Who may join. + if let Some(online) = answers.online { + cfg.server.online_mode = online; + } cfg.server.whitelist = answers.whitelist.unwrap_or_else(|| { prompt.yes( "Only let in players you list (whitelist)?", @@ -193,12 +199,15 @@ pub fn run(dir: &Path, answers: Answers) -> Result<(), String> { .yes(&format!("Make {first} an operator?"), true) .then(|| first.clone()) }); - add_players(dir, &players, op.as_deref())?; + add_players(dir, &players, op.as_deref(), cfg.server.online_mode)?; // 5. The game and its files. match engine::program(&cfg, dir) { Ok(program) => { println!("Game: {}", program.display()); + if let Err(e) = engine::check_starts(&program) { + println!("Warning: {e}"); + } match engine::resources(&cfg, dir, Some(&program)) { Ok(resources) => println!("Game files: {}", resources.display()), Err(e) => println!("{e}"), @@ -213,24 +222,29 @@ pub fn run(dir: &Path, answers: Answers) -> Result<(), String> { toml_path.display(), dir.display() ); - if cfg.server.online_mode { + if !cfg.server.online_mode { println!( - "Note: Horizon's server doesn't check Mojang accounts yet (online mode), so it runs offline: \ - keep the whitelist on, or play on a LAN or a tailnet." + "Note: online mode is off, so anyone can join under any name: keep the whitelist on, or play on a \ + LAN or a tailnet." ); } Ok(()) } -/// Adds players to the whitelist (and one as an operator) in vanilla's files, with offline UUIDs as the -/// server runs offline. -fn add_players(dir: &Path, players: &[String], op: Option<&str>) -> Result<(), String> { +/// Adds players to the whitelist (and one as an operator) in vanilla's files, with the profiles the server +/// will see: offline ones, or in online mode their accounts. +fn add_players( + dir: &Path, + players: &[String], + op: Option<&str>, + online: bool, +) -> Result<(), String> { if !players.is_empty() { let mut white = WhiteList::new(dir.join("whitelist.json")); let _ = white.load(); for name in players { white.add(WhiteEntry { - user: NameAndId::offline(name), + user: crate::lists::account(name, online)?, }); } println!("Whitelisted: {}", players.join(", ")); @@ -239,7 +253,7 @@ fn add_players(dir: &Path, players: &[String], op: Option<&str>) -> Result<(), S let mut ops = OpList::new(dir.join("ops.json")); let _ = ops.load(); ops.add(OpEntry { - user: NameAndId::offline(name), + user: crate::lists::account(name, online)?, level: 4, bypasses_player_limit: false, }); @@ -264,6 +278,8 @@ mod tests { whitelist: Some(true), players: vec!["Alex".into(), "Steve".into()], op: Some("Alex".into()), + // Offline: no account lookups in a test. + online: Some(false), yes: true, }; run(&dir, answers).unwrap(); @@ -277,6 +293,7 @@ mod tests { ("survival", "12345", 25599) ); assert!(eula::agreed(&dir.join("eula.txt")).unwrap()); + assert!(!cfg.server.online_mode); let white = std::fs::read_to_string(dir.join("whitelist.json")).unwrap(); assert!( white.contains("\"Alex\"") && white.contains("\"Steve\""), diff --git a/crates/horizon-server/src/lists.rs b/crates/horizon-server/src/lists.rs index 398ccfe10..a6c466666 100644 --- a/crates/horizon-server/src/lists.rs +++ b/crates/horizon-server/src/lists.rs @@ -20,7 +20,7 @@ pub fn run(dir: &Path, command: &str, args: &[String]) -> Result<(), String> { let message = match (command, args.as_slice()) { ("op", [name]) => { let mut ops = load(OpList::new(ops_path))?; - let user = NameAndId::offline(name); + let user = account(name, config.server.online_mode)?; if ops.contains(&user) { "Nothing changed. The player already is an operator".to_owned() } else { @@ -34,7 +34,8 @@ pub fn run(dir: &Path, command: &str, args: &[String]) -> Result<(), String> { } ("deop", [name]) => { let mut ops = load(OpList::new(ops_path))?; - if ops.remove(&NameAndId::offline(name)) { + let user = listed(ops.entries().iter().map(|e| &e.user), name); + if user.is_some_and(|user| ops.remove(&user)) { format!("Made {name} no longer a server operator") } else { "Nothing changed. The player is not an operator".to_owned() @@ -42,7 +43,7 @@ pub fn run(dir: &Path, command: &str, args: &[String]) -> Result<(), String> { } ("whitelist", ["add", name]) => { let mut white = load(WhiteList::new(white_path))?; - let user = NameAndId::offline(name); + let user = account(name, config.server.online_mode)?; if white.contains(&user) { "Player is already whitelisted".to_owned() } else { @@ -52,7 +53,8 @@ pub fn run(dir: &Path, command: &str, args: &[String]) -> Result<(), String> { } ("whitelist", ["remove", name]) => { let mut white = load(WhiteList::new(white_path))?; - if white.remove(&NameAndId::offline(name)) { + let user = listed(white.entries().iter().map(|e| &e.user), name); + if user.is_some_and(|user| white.remove(&user)) { format!("Removed {name} from the whitelist") } else { "Player is not whitelisted".to_owned() @@ -97,6 +99,23 @@ pub fn run(dir: &Path, command: &str, args: &[String]) -> Result<(), String> { Ok(()) } +/// The profile a running server would store for `name`: the offline one, or in online mode the account the +/// services know by that name. +pub fn account(name: &str, online: bool) -> Result<NameAndId, String> { + horizon_server_files::profiles::resolve(name, online).ok_or_else(|| { + if online { + format!("That player does not exist (no Minecraft account is named {name}, or the services didn't answer)") + } else { + "That player does not exist".to_owned() + } + }) +} + +/// The entry already on a list under `name` (any case), so removing needs no lookup. +fn listed<'a>(mut users: impl Iterator<Item = &'a NameAndId>, name: &str) -> Option<NameAndId> { + users.find(|u| u.name.eq_ignore_ascii_case(name)).cloned() +} + /// A list from its file; a missing file is an empty list, a broken one an error (vanilla would replace it). fn load<E: horizon_server_files::user_lists::Entry>( mut list: horizon_server_files::user_lists::UserList<E>, diff --git a/crates/horizon-server/src/main.rs b/crates/horizon-server/src/main.rs index af45015de..8fda74fba 100644 --- a/crates/horizon-server/src/main.rs +++ b/crates/horizon-server/src/main.rs @@ -13,6 +13,7 @@ mod panel; mod service; mod signals; mod supervisor; +mod update; use std::{path::PathBuf, process::ExitCode}; @@ -23,7 +24,7 @@ Usage: horizon-server [--dir DIR] <command> init Set up a server here: the EULA, the world, the port, who may join (asks, or takes flags: --eula --world NAME --seed SEED --port N --whitelist/--no-whitelist --player NAME... --op NAME - --yes) + --offline --yes) run Run the server in the foreground; type console commands, Ctrl-C stops it cleanly. It restarts the game if it crashes. start Run the server in the background (its output goes to logs/supervisor.log) @@ -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" @@ -150,6 +158,7 @@ fn init_answers(args: &[String]) -> Result<init::Answers, String> { "--no-whitelist" => answers.whitelist = Some(false), "--player" => answers.players.push(value(arg)?), "--op" => answers.op = Some(value(arg)?), + "--offline" => answers.online = Some(false), other => return Err(format!("init: unknown option {other}")), } } diff --git a/crates/horizon-server/src/panel.rs b/crates/horizon-server/src/panel.rs index 44ca24e40..76bacc047 100644 --- a/crates/horizon-server/src/panel.rs +++ b/crates/horizon-server/src/panel.rs @@ -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)) diff --git a/crates/horizon-server/src/service.rs b/crates/horizon-server/src/service.rs index 2cafa0e3d..6e6d59f23 100644 --- a/crates/horizon-server/src/service.rs +++ b/crates/horizon-server/src/service.rs @@ -32,7 +32,8 @@ pub fn name(dir: &Path) -> String { } /// The program the service runs: the `horizon-server` on the PATH (the installer's launcher, which keeps -/// running the current version after an update), else this program. +/// running the current version after an update), else this program, through the installer's `current` +/// link when it runs from an installed release. fn program() -> Result<PathBuf, String> { let on_path = std::env::var_os("PATH") .map(|p| std::env::split_paths(&p).collect::<Vec<_>>()) @@ -42,10 +43,30 @@ fn program() -> Result<PathBuf, String> { .find(|p| p.is_file()); match on_path { Some(path) => Ok(path), - None => std::env::current_exe().map_err(|e| e.to_string()), + None => std::env::current_exe() + .map(|exe| through_current(&exe)) + .map_err(|e| e.to_string()), } } +/// `<dir>/releases/<tag>/horizon-server` as `<dir>/current/horizon-server` while `current` points at that +/// release (install.sh and install.ps1's layout), so an update applies at the next restart. Linux reports +/// the running program with links resolved, so the release's own path is what this program sees there. +fn through_current(exe: &Path) -> PathBuf { + let current = (|| { + let release = exe.parent()?; + let releases = release.parent()?; + if releases.file_name()? != "releases" { + return None; + } + let current = releases.parent()?.join("current"); + let name = exe.file_name()?; + let same = std::fs::canonicalize(¤t).ok()? == std::fs::canonicalize(release).ok()?; + same.then(|| current.join(name)) + })(); + current.unwrap_or_else(|| exe.to_path_buf()) +} + /// A systemd unit: a user unit, or with `user` a system unit running as that user. pub fn systemd_unit(program: &Path, dir: &Path, user: Option<&str>) -> String { let (run_as, wanted_by) = match user { @@ -374,4 +395,22 @@ mod tests { assert!(plist.contains("<string>/srv/R&D</string>")); assert!(plist.contains("<string>/srv/R&D/logs/supervisor.log</string>")); } + + #[cfg(unix)] + #[test] + fn an_installed_release_runs_through_current() { + let root = + std::env::temp_dir().join(format!("horizon-service-current-{}", std::process::id())); + let release = root.join("releases/v1"); + std::fs::create_dir_all(&release).unwrap(); + let exe = release.join("horizon-server"); + std::fs::write(&exe, "").unwrap(); + // Not the release in use (or no installer layout): this program as it is. + assert_eq!(through_current(&exe), exe); + std::os::unix::fs::symlink(&release, root.join("current")).unwrap(); + assert_eq!(through_current(&exe), root.join("current/horizon-server")); + let other = root.join("bin/horizon-server"); + assert_eq!(through_current(&other), other); + std::fs::remove_dir_all(&root).unwrap(); + } } diff --git a/crates/horizon-server/src/supervisor.rs b/crates/horizon-server/src/supervisor.rs index ad056a77c..1cd36e83e 100644 --- a/crates/horizon-server/src/supervisor.rs +++ b/crates/horizon-server/src/supervisor.rs @@ -1,14 +1,14 @@ //! `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 horizon_server_files::java_properties::now_millis; use serde_json::json; use std::{ fs::File, @@ -29,15 +29,12 @@ const MAX_BACKOFF: Duration = Duration::from_secs(300); /// How long `stop` may take before the game is killed. 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`, UTC), on -/// stdout and in the log the API serves. -fn note(shared: &Shared, level: &str, message: &str) { - let secs = now_millis().div_euclid(1000).rem_euclid(86_400); +/// 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. +pub fn note(shared: &Shared, level: &str, message: &str) { let line = format!( - "[{:02}:{:02}:{:02}] [Supervisor/{level}]: {message}", - secs / 3600, - secs / 60 % 60, - secs % 60 + "[{}] [Supervisor/{level}]: {message}", + chrono::Local::now().format("%H:%M:%S") ); println!("{line}"); shared.log.lock().unwrap().push(line); @@ -69,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> { @@ -76,6 +87,8 @@ pub fn run(dir: &Path) -> Result<(), String> { let config = Config::load(dir)?; let program = engine::program(&config, dir)?; let resources = engine::resources(&config, dir, Some(&program))?; + // A game the system can't load would only crash again at every restart: say why at once. + engine::check_starts(&program)?; let _lock = lock(dir)?; let shared = Shared::new(dir); let _endpoint = api::start(shared.clone())?; @@ -99,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}"))?; @@ -124,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; @@ -146,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 { @@ -164,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, @@ -208,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, ¤t).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)) @@ -311,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, @@ -360,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); } @@ -370,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() { @@ -421,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), @@ -449,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())); diff --git a/crates/horizon-server/src/update.rs b/crates/horizon-server/src/update.rs new file mode 100644 index 000000000..bebbedc76 --- /dev/null +++ b/crates/horizon-server/src/update.rs @@ -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, ¤t) { + 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, ¤t) { + 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(); + } +} diff --git a/crates/horizon-telemetry/src/payload.rs b/crates/horizon-telemetry/src/payload.rs index 1429a7116..2ed0ba88d 100644 --- a/crates/horizon-telemetry/src/payload.rs +++ b/crates/horizon-telemetry/src/payload.rs @@ -179,6 +179,10 @@ pub struct Perf { /// From start to the title screen. #[serde(skip_serializing_if = "Option::is_none")] pub to_title_ms: Option<u32>, + #[serde(skip_serializing_if = "Option::is_none")] + pub to_core_ms: Option<u32>, + #[serde(skip_serializing_if = "Option::is_none")] + pub to_rest_ms: Option<u32>, /// From choosing a world to playing in it, the first time this session. #[serde(skip_serializing_if = "Option::is_none")] pub to_world_ms: Option<u32>, diff --git a/crates/horizon-threads/Cargo.toml b/crates/horizon-threads/Cargo.toml new file mode 100644 index 000000000..35f7e3222 --- /dev/null +++ b/crates/horizon-threads/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "horizon-threads" +version = "0.1.0" +edition.workspace = true +license.workspace = true +rust-version = "1.85" +description = "Scheduling hints for the game's threads: the OS calls the crates that forbid unsafe code can't make" + +[target.'cfg(target_vendor = "apple")'.dependencies] +libc = "0.2" + +[target.'cfg(windows)'.dependencies] +windows-sys = { version = "0.61", features = ["Win32_System_Threading"] } + +[lints] +workspace = true diff --git a/crates/horizon-threads/src/lib.rs b/crates/horizon-threads/src/lib.rs new file mode 100644 index 000000000..6436a4729 --- /dev/null +++ b/crates/horizon-threads/src/lib.rs @@ -0,0 +1,66 @@ +//! Scheduling hints for the game's threads: the few OS calls that the other +//! crates, which forbid unsafe code, can't make themselves. + +/// Ask the OS to run the calling thread ahead of the process's other +/// threads, as vanilla's `MinecraftServer.spin` does for its server thread +/// (`Thread.setPriority(8)` when there are more than four processors), so +/// world generation and chunk encoding on every other core don't hold a tick +/// back: user-interactive quality of service on macOS, above-normal priority +/// on Windows. Nothing elsewhere: Linux lets an unprivileged thread lower its +/// priority but not raise it (and the JVM leaves it there too). Whether the +/// OS took it. +pub fn favour_current() -> bool { + if std::thread::available_parallelism().map_or(1, |n| n.get()) <= 4 { + return false; + } + favour() +} + +#[cfg(target_vendor = "apple")] +fn favour() -> bool { + // SAFETY: sets the calling thread's own QoS class; no pointers. + unsafe { + libc::pthread_set_qos_class_self_np(libc::qos_class_t::QOS_CLASS_USER_INTERACTIVE, 0) == 0 + } +} + +#[cfg(windows)] +fn favour() -> bool { + use windows_sys::Win32::System::Threading::{ + GetCurrentThread, SetThreadPriority, THREAD_PRIORITY_ABOVE_NORMAL, + }; + // SAFETY: GetCurrentThread's pseudo handle needs no closing. + unsafe { SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL) != 0 } +} + +#[cfg(not(any(target_vendor = "apple", windows)))] +fn favour() -> bool { + false +} + +#[cfg(test)] +mod tests { + /// The hint holds on the platforms that take it. + #[test] + fn favouring_a_thread_takes_where_the_os_allows_it() { + let took = std::thread::spawn(super::favour).join().unwrap(); + assert_eq!(took, cfg!(any(target_vendor = "apple", windows))); + #[cfg(target_vendor = "apple")] + std::thread::spawn(|| { + assert!(super::favour()); + let mut class = libc::qos_class_t::QOS_CLASS_UNSPECIFIED; + let mut relative = 0; + // SAFETY: reads the calling thread's own QoS class into locals. + let read = unsafe { + libc::pthread_get_qos_class_np(libc::pthread_self(), &mut class, &mut relative) + }; + assert_eq!(read, 0); + assert!(matches!( + class, + libc::qos_class_t::QOS_CLASS_USER_INTERACTIVE + )); + }) + .join() + .unwrap(); + } +} diff --git a/crates/horizon-world/Cargo.toml b/crates/horizon-world/Cargo.toml index 078692bff..3a8621243 100644 --- a/crates/horizon-world/Cargo.toml +++ b/crates/horizon-world/Cargo.toml @@ -9,8 +9,11 @@ description = "Horizon vanilla world data, NBT, regions and chunk storage" [dependencies] libm = "=0.2.16" serde_json = { version = "1.0.151", features = ["raw_value"] } +serde = { version = "1", features = ["derive"] } +sha2 = "0.10" +postcard = { version = "=1.1.3", default-features = false, features = ["alloc"] } horizon-core = { path = "../horizon-core" } -flate2 = { version = "1.1.10", default-features = false, features = ["rust_backend"] } +flate2 = { version = "1.1.10", default-features = false, features = ["zlib-rs", "runtime_detection"] } lz4_flex = { version = "0.14", default-features = false, features = ["std", "safe-encode", "safe-decode", "checked-decode"] } xxhash-rust = { version = "0.8.19", features = ["xxh32"] } diff --git a/crates/horizon-world/examples/behaviour_load_profile.rs b/crates/horizon-world/examples/behaviour_load_profile.rs new file mode 100644 index 000000000..d9d7a1147 --- /dev/null +++ b/crates/horizon-world/examples/behaviour_load_profile.rs @@ -0,0 +1,32 @@ +//! Measure registry and behaviour load times using prepared client resources. +//! `behaviour_load_profile RESOURCES [CACHE_DIR]`: with CACHE_DIR, loads use and +//! refresh the snapshot there, as the client does with its `.horizon-cache`. +use horizon_world::level::{Behaviours, data::load_registry}; +use std::{path::Path, time::Instant}; + +fn main() -> Result<(), Box<dyn std::error::Error>> { + let root = std::env::args() + .nth(1) + .expect("resource directory argument"); + let root = Path::new(&root); + if let Some(dir) = std::env::args().nth(2) { + Behaviours::set_cache_dir(dir.into()); + } + let started = Instant::now(); + let registry = load_registry(root)?; + eprintln!( + "registry_ms={:.3}", + started.elapsed().as_secs_f64() * 1000.0 + ); + for iteration in 0..3 { + let started = Instant::now(); + let behaviours = Behaviours::load(®istry, root)?; + eprintln!( + "behaviours_{}_ms={:.3}", + iteration, + started.elapsed().as_secs_f64() * 1000.0 + ); + std::hint::black_box(behaviours); + } + Ok(()) +} diff --git a/crates/horizon-world/src/attributes/entity.rs b/crates/horizon-world/src/attributes/entity.rs index 1bdf94aaf..b7ffd3925 100644 --- a/crates/horizon-world/src/attributes/entity.rs +++ b/crates/horizon-world/src/attributes/entity.rs @@ -11,6 +11,11 @@ impl AttributeId { pub(super) fn index(self) -> usize { self.0 } + /// The attribute's id in the `minecraft:attribute` registry: the catalog + /// export lists `BuiltInRegistries.ATTRIBUTE` in order. + pub fn registry_id(self) -> i32 { + self.0 as i32 + } } #[derive(Debug)] pub struct RegisteredAttribute { @@ -100,6 +105,10 @@ impl Catalog { defaults, }) } + /// The entity type's supplier base of an attribute, if the type has it. + pub fn default_base(&self, entity: &str, id: AttributeId) -> Option<f64> { + self.defaults.get(entity).and_then(|b| b[id.0]) + } pub fn id(&self, name: &str) -> Option<AttributeId> { self.ids.get(name).copied() } diff --git a/crates/horizon-world/src/block_properties.rs b/crates/horizon-world/src/block_properties.rs index 2a463b9e3..98e06f7cb 100644 --- a/crates/horizon-world/src/block_properties.rs +++ b/crates/horizon-world/src/block_properties.rs @@ -2,11 +2,10 @@ //! collision shapes. Replacement with an item/fluid context is separate behavior. use crate::{ DATA_VERSION, - chunk::BlockState, collision::CachedCollisionRegistry, compression::invalid, nbt::Tag, - registry::{BlockRegistry, BlockStateId}, + registry::{BlockRegistry, BlockStateId, BlockTable}, }; use horizon_core::geometry::{Direction, ShapeError, ShapeOp, VoxelShape}; use std::{ @@ -80,15 +79,18 @@ impl BlockProperty { #[derive(Clone, Debug)] pub struct BoundBlockProperty { states: Arc<[u8]>, - property: BlockProperty, + mask: u8, } impl BoundBlockProperty { pub fn contains(&self, id: BlockStateId) -> bool { self.states .get(id.0 as usize) - .is_some_and(|flags| flags & self.property.mask() != 0) + .is_some_and(|flags| flags & self.mask != 0) } } +/// The per-state flag of the block definition's `canOcclude`, past the +/// [`BlockProperty`] bits. +const CAN_OCCLUDE: u8 = 16; /// Immutable FULL face-occlusion results, independent of support/collision. #[derive(Clone, Debug)] @@ -112,21 +114,25 @@ pub struct BlockPropertiesRegistry<'r> { } impl<'r> BlockPropertiesRegistry<'r> { pub fn from_nbt(tag: &Tag, collisions: &CachedCollisionRegistry<'r>) -> io::Result<Self> { + Self::from_table( + BlockTable::check(tag, collisions.registry(), "block property")?, + collisions, + ) + } + pub fn from_table( + table: BlockTable<'_, '_>, + collisions: &CachedCollisionRegistry<'r>, + ) -> io::Result<Self> { let registry = collisions.registry(); + if !std::ptr::eq(table.registry(), registry) { + return Err(invalid( + "block properties and collisions use other registries", + )); + } + let tag = table.tag(); if tag.get("DataVersion") != Some(&Tag::Int(DATA_VERSION)) { return Err(invalid("block properties DataVersion mismatch")); } - let Some(Tag::List(states)) = tag.get("states") else { - return Err(invalid("missing block property state identities")); - }; - if states.len() != registry.len() { - return Err(invalid("block property registry size mismatch")); - } - for (id, state) in states.iter().enumerate() { - if registry.state(BlockStateId(id as u32)) != Some(&BlockState::from_nbt(state)?) { - return Err(invalid("block property state identities differ")); - } - } let Some(Tag::Compound(blocks)) = tag.get("blocks") else { return Err(invalid("missing block definition properties")); }; @@ -150,10 +156,8 @@ impl<'r> BlockPropertiesRegistry<'r> { }, ); } - let occlusion = CachedCollisionRegistry::from_nbt( - tag.get("occlusion") - .ok_or_else(|| invalid("missing block occlusion shapes"))?, - registry, + let occlusion = CachedCollisionRegistry::from_table( + table.nested("occlusion", "block occlusion shapes")?, )?; let mut result = Vec::with_capacity(registry.len()); let mut faces = Vec::with_capacity(registry.len()); @@ -194,7 +198,12 @@ impl<'r> BlockPropertiesRegistry<'r> { .ok_or_else(|| invalid("missing block occlusion shape"))?, ) .map_err(|error| invalid(error.to_string()))?, - ) << 3), + ) << 3) + | if properties.can_occlude { + CAN_OCCLUDE + } else { + 0 + }, ); } Ok(Self { @@ -224,7 +233,14 @@ impl<'r> BlockPropertiesRegistry<'r> { pub fn bind(&self, property: BlockProperty) -> BoundBlockProperty { BoundBlockProperty { states: Arc::clone(&self.states), - property, + mask: property.mask(), + } + } + /// The states whose block definition can occlude (BlockState.canOcclude). + pub fn bind_can_occlude(&self) -> BoundBlockProperty { + BoundBlockProperty { + states: Arc::clone(&self.states), + mask: CAN_OCCLUDE, } } } diff --git a/crates/horizon-world/src/block_support.rs b/crates/horizon-world/src/block_support.rs index 2c7a9aecc..295999d18 100644 --- a/crates/horizon-world/src/block_support.rs +++ b/crates/horizon-world/src/block_support.rs @@ -61,7 +61,7 @@ use crate::{ collision::CachedCollisionRegistry, compression::invalid, nbt::Tag, - registry::{BlockRegistry, BlockStateId}, + registry::{BlockRegistry, BlockStateId, BlockTable}, }; use std::{collections::HashMap, io}; /// Cached branch of BlockState.isFaceSturdy. Dynamic support-shape selection @@ -72,10 +72,14 @@ pub struct CachedBlockSupportRegistry<'r> { } impl<'r> CachedBlockSupportRegistry<'r> { pub fn from_nbt(tag: &Tag, registry: &'r BlockRegistry) -> io::Result<Self> { - if tag.get("Kind") != Some(&Tag::String("block_support".into())) { + Self::from_table(BlockTable::check(tag, registry, "support")?) + } + pub fn from_table(table: BlockTable<'_, 'r>) -> io::Result<Self> { + if table.get("Kind") != Some(&Tag::String("block_support".into())) { return Err(invalid("wrong cached support data kind")); } - let shapes = CachedCollisionRegistry::from_nbt(tag, registry)?; + let registry = table.registry(); + let shapes = CachedCollisionRegistry::from_table(table)?; let mut cache = HashMap::new(); let mut states = Vec::with_capacity(registry.len()); for id in 0..registry.len() { @@ -163,7 +167,11 @@ impl<E: std::fmt::Display> std::fmt::Display for SupportQueryError<E> { impl<E: std::error::Error + 'static> std::error::Error for SupportQueryError<E> {} impl<'r> BlockSupportRegistry<'r> { pub fn from_nbt(tag: &Tag, registry: &'r BlockRegistry) -> io::Result<Self> { - let cached = CachedBlockSupportRegistry::from_nbt(tag, registry)?; + Self::from_table(BlockTable::check(tag, registry, "support")?) + } + pub fn from_table(table: BlockTable<'_, 'r>) -> io::Result<Self> { + let registry = table.registry(); + let cached = CachedBlockSupportRegistry::from_table(table)?; let mut dynamic = HashMap::new(); for id in 0..registry.len() { let id = BlockStateId(id as u32); diff --git a/crates/horizon-world/src/chunk.rs b/crates/horizon-world/src/chunk.rs index f4341c92f..fdbefe259 100644 --- a/crates/horizon-world/src/chunk.rs +++ b/crates/horizon-world/src/chunk.rs @@ -442,54 +442,58 @@ impl<'r> BlockSection<'r> { if index >= 4096 { return Err(invalid("section index out of bounds")); } - let id = self.storage.as_ref().map_or(0, |s| s.get(index).unwrap()); + let id = self.storage.as_ref().map_or(0, |s| s.read(index)); Ok(self.palette[id as usize]) } pub fn set(&mut self, index: usize, state: BlockStateId) -> io::Result<BlockStateId> { - let old = self.get(index)?; + if index >= 4096 { + return Err(invalid("section index out of bounds")); + } if state.0 as usize >= self.registry.len() { return Err(invalid("state ID outside registry")); } + // The slot at `index` is the old state's id, so no palette lookup is + // needed for it; only the new state has to be resolved. + let old_id = self.storage.as_ref().map_or(0, |s| s.read(index)); + let old = self.palette[old_id as usize]; if old == state { return Ok(old); } - let old_id = self.local_ids[&old]; - if self.counts[old_id as usize] == 1 && !self.local_ids.contains_key(&state) { - // Replacing the last occurrence can reuse its slot without repacking. - self.palette[old_id as usize] = state; - self.local_ids.remove(&old); - self.local_ids.insert(state, old_id); - return Ok(old); - } - let id = if let Some(id) = self.local_ids.get(&state) { - *id - } else if let Some(id) = self.free.pop() { - self.palette[id as usize] = state; - self.local_ids.insert(state, id); - id - } else { - let id = self.palette.len() as u32; - let next_bits = bits_for_palette(self.palette.len() + 1, 4); - if next_bits != self.bits() { - let mut storage = - PackedStorage::new(next_bits, 4096).map_err(|e| invalid(e.to_string()))?; - if let Some(old) = &self.storage { - for i in 0..4096 { - storage.set(i, old.get(i).unwrap()).unwrap(); - } - } - self.storage = Some(storage); + let id = match self.local_ids.get(&state) { + Some(&id) => id, + None if self.counts[old_id as usize] == 1 => { + // Replacing the last occurrence can reuse its slot without repacking. + self.palette[old_id as usize] = state; + self.local_ids.remove(&old); + self.local_ids.insert(state, old_id); + return Ok(old); + } + None => { + if let Some(id) = self.free.pop() { + self.palette[id as usize] = state; + self.local_ids.insert(state, id); + id + } else { + let id = self.palette.len() as u32; + let next_bits = bits_for_palette(self.palette.len() + 1, 4); + if next_bits != self.bits() { + let mut storage = PackedStorage::new(next_bits, 4096) + .map_err(|e| invalid(e.to_string()))?; + if let Some(old) = &self.storage { + for i in 0..4096 { + storage.write(i, old.read(i)); + } + } + self.storage = Some(storage); + } + self.palette.push(state); + self.counts.push(0); + self.local_ids.insert(state, id); + id + } } - self.palette.push(state); - self.counts.push(0); - self.local_ids.insert(state, id); - id }; - self.storage - .as_mut() - .unwrap() - .set(index, id) - .map_err(|e| invalid(e.to_string()))?; + self.storage.as_mut().unwrap().write(index, id); self.counts[old_id as usize] -= 1; self.counts[id as usize] += 1; if self.counts[old_id as usize] == 0 { @@ -585,4 +589,271 @@ mod tests { map.insert(key("data"), Tag::LongArray(vec![-1; 256])); assert!(PalettedContainer::from_nbt(&tag, 4096, 4).is_err()); } + + /// The pre-optimization `get`, kept to compare against in the benchmark. + fn baseline_get(section: &BlockSection<'_>, index: usize) -> io::Result<BlockStateId> { + if index >= 4096 { + return Err(invalid("section index out of bounds")); + } + let id = section + .storage + .as_ref() + .map_or(0, |s| s.get(index).unwrap()); + Ok(section.palette[id as usize]) + } + /// The pre-optimization `set`, kept to compare against in the benchmark. + fn baseline_set( + section: &mut BlockSection<'_>, + index: usize, + state: BlockStateId, + ) -> io::Result<BlockStateId> { + let old = baseline_get(section, index)?; + if state.0 as usize >= section.registry.len() { + return Err(invalid("state ID outside registry")); + } + if old == state { + return Ok(old); + } + let old_id = section.local_ids[&old]; + if section.counts[old_id as usize] == 1 && !section.local_ids.contains_key(&state) { + section.palette[old_id as usize] = state; + section.local_ids.remove(&old); + section.local_ids.insert(state, old_id); + return Ok(old); + } + let id = if let Some(id) = section.local_ids.get(&state) { + *id + } else if let Some(id) = section.free.pop() { + section.palette[id as usize] = state; + section.local_ids.insert(state, id); + id + } else { + let id = section.palette.len() as u32; + let next_bits = bits_for_palette(section.palette.len() + 1, 4); + if next_bits != section.bits() { + let mut storage = + PackedStorage::new(next_bits, 4096).map_err(|e| invalid(e.to_string()))?; + if let Some(old) = §ion.storage { + for i in 0..4096 { + storage.set(i, old.get(i).unwrap()).unwrap(); + } + } + section.storage = Some(storage); + } + section.palette.push(state); + section.counts.push(0); + section.local_ids.insert(state, id); + id + }; + section + .storage + .as_mut() + .unwrap() + .set(index, id) + .map_err(|e| invalid(e.to_string()))?; + section.counts[old_id as usize] -= 1; + section.counts[id as usize] += 1; + if section.counts[old_id as usize] == 0 { + section.local_ids.remove(&old); + section.free.push(old_id); + } + if section.counts[id as usize] == 4096 { + section.palette = vec![state]; + section.local_ids = local_ids(state); + section.counts = vec![4096]; + section.free.clear(); + section.storage = None; + } + Ok(old) + } + + fn benchmark_registry(states: usize) -> BlockRegistry { + let list: Vec<Tag> = (0..states) + .map(|i| { + Tag::compound([("id", Tag::String(format!("minecraft:b{i}").as_str().into()))]) + }) + .collect(); + let defaults: BTreeMap<_, _> = (0..states) + .map(|i| { + ( + JavaString::from(format!("minecraft:b{i}").as_str()), + Tag::Int(i as i32), + ) + }) + .collect(); + let tag = Tag::compound([ + ("DataVersion", Tag::Int(crate::DATA_VERSION)), + ("states", Tag::List(list)), + ("defaults", Tag::Compound(defaults)), + ]); + BlockRegistry::from_nbt(&tag).unwrap() + } + + /// The optimized `set`/`get` must agree with the pre-optimization ones on + /// every field, across growth, collapse and reclaimed palette slots. + #[test] + fn set_matches_baseline_under_random_edits() { + let registry = benchmark_registry(64); + for &(distinct, seed) in &[(1u64, 1u64), (2, 2), (16, 3), (17, 4), (24, 5), (64, 6)] { + let mut base = BlockSection::uniform(BlockStateId(0), ®istry).unwrap(); + let mut fast = BlockSection::uniform(BlockStateId(0), ®istry).unwrap(); + let mut rng = seed.wrapping_mul(0x9e37_79b9_7f4a_7c15) | 1; + let mut next = || { + rng ^= rng << 13; + rng ^= rng >> 7; + rng ^= rng << 17; + rng + }; + for step in 0..50_000u32 { + if step % 5_000 == 4_999 { + let value = (next() % distinct) as u32; + for index in 0..4096 { + assert_eq!( + baseline_set(&mut base, index, BlockStateId(value)).unwrap(), + fast.set(index, BlockStateId(value)).unwrap() + ); + } + } else { + let index = (next() % 4096) as usize; + let value = (next() % distinct) as u32; + assert_eq!( + baseline_set(&mut base, index, BlockStateId(value)).unwrap(), + fast.set(index, BlockStateId(value)).unwrap() + ); + } + assert_eq!(base.bits(), fast.bits()); + } + assert_eq!(base.palette, fast.palette); + assert_eq!(base.counts, fast.counts); + assert_eq!(base.free, fast.free); + assert_eq!(base.local_ids, fast.local_ids); + for index in 0..4096 { + assert_eq!( + baseline_get(&base, index).unwrap(), + fast.get(index).unwrap(), + "distinct {distinct} index {index}" + ); + } + assert_eq!(base.to_nbt().unwrap(), fast.to_nbt().unwrap()); + } + } + + /// `set` and `get` against the pre-optimization implementations, on the same + /// edit stream. The result is checked before timing; 51 measured rounds + /// alternate order and report the median and best nanosecond-per-operation. + #[test] + #[ignore = "microbenchmark; run with --ignored --nocapture"] + fn benchmark_section_get_set() { + use std::{hint::black_box, time::Instant}; + const OPS: usize = 50_000; + const ROUNDS: u32 = 51; + let registry = benchmark_registry(32); + let mut rng = 0x1234_5678_9abc_def1u64; + let mut next = || { + rng ^= rng << 13; + rng ^= rng >> 7; + rng ^= rng << 17; + rng + }; + let ops: Vec<(usize, u32)> = (0..OPS) + .map(|_| ((next() % 4096) as usize, (next() % 24) as u32)) + .collect(); + let make = || BlockSection::uniform(BlockStateId(0), ®istry).unwrap(); + + let mut base = make(); + let mut fast = make(); + for &(index, value) in &ops { + assert_eq!( + baseline_set(&mut base, index, BlockStateId(value)).unwrap(), + fast.set(index, BlockStateId(value)).unwrap() + ); + } + assert_eq!(base.bits(), fast.bits()); + assert_eq!(base.palette, fast.palette); + assert_eq!(base.counts, fast.counts); + assert_eq!(base.free, fast.free); + assert_eq!(base.to_nbt().unwrap(), fast.to_nbt().unwrap()); + for i in 0..4096 { + assert_eq!(baseline_get(&base, i).unwrap(), fast.get(i).unwrap()); + } + + let mut times = [Vec::new(), Vec::new()]; + for round in 0..ROUNDS { + for optimized in [round % 2 == 0, round % 2 != 0] { + let mut section = make(); + let started = Instant::now(); + for &(index, value) in &ops { + if optimized { + section + .set(black_box(index), black_box(BlockStateId(value))) + .unwrap(); + } else { + baseline_set( + &mut section, + black_box(index), + black_box(BlockStateId(value)), + ) + .unwrap(); + } + } + black_box(§ion); + if round > 0 { + times[usize::from(optimized)] + .push(started.elapsed().as_secs_f64() * 1e9 / OPS as f64); + } + } + } + for values in &mut times { + values.sort_by(f64::total_cmp); + } + let median = |v: &[f64]| v[v.len() / 2]; + println!( + "section set median: baseline {:.3} ns/op optimized {:.3} ns/op speedup {:.3}x", + median(×[0]), + median(×[1]), + median(×[0]) / median(×[1]) + ); + println!( + "section set best: baseline {:.3} ns/op optimized {:.3} ns/op speedup {:.3}x", + times[0][0], + times[1][0], + times[0][0] / times[1][0] + ); + + let mut get_times = [Vec::new(), Vec::new()]; + for round in 0..ROUNDS { + for optimized in [round % 2 == 0, round % 2 != 0] { + let started = Instant::now(); + let mut acc = 0u32; + for &(index, _) in &ops { + let state = if optimized { + fast.get(black_box(index)).unwrap() + } else { + baseline_get(&base, black_box(index)).unwrap() + }; + acc ^= state.0; + } + black_box(acc); + if round > 0 { + get_times[usize::from(optimized)] + .push(started.elapsed().as_secs_f64() * 1e9 / OPS as f64); + } + } + } + for values in &mut get_times { + values.sort_by(f64::total_cmp); + } + println!( + "section get median: baseline {:.3} ns/op optimized {:.3} ns/op speedup {:.3}x", + median(&get_times[0]), + median(&get_times[1]), + median(&get_times[0]) / median(&get_times[1]) + ); + println!( + "section get best: baseline {:.3} ns/op optimized {:.3} ns/op speedup {:.3}x", + get_times[0][0], + get_times[1][0], + get_times[0][0] / get_times[1][0] + ); + } } diff --git a/crates/horizon-world/src/collision.rs b/crates/horizon-world/src/collision.rs index 694264a5e..3d3311646 100644 --- a/crates/horizon-world/src/collision.rs +++ b/crates/horizon-world/src/collision.rs @@ -5,10 +5,10 @@ //! This module covers the cached branch only; uncached states remain explicit. use crate::{ DATA_VERSION, - chunk::{BlockSection, BlockState}, + chunk::BlockSection, compression::invalid, nbt::Tag, - registry::{BlockRegistry, BlockStateId}, + registry::{BlockRegistry, BlockStateId, BlockTable}, }; use horizon_core::geometry::{Axis, VoxelShape}; use std::{collections::HashMap, io}; @@ -35,20 +35,13 @@ impl<'r> CachedCollisionRegistry<'r> { self.registry } pub fn from_nbt(tag: &Tag, registry: &'r BlockRegistry) -> io::Result<Self> { + Self::from_table(BlockTable::check(tag, registry, "collision")?) + } + pub fn from_table(table: BlockTable<'_, 'r>) -> io::Result<Self> { + let (tag, registry) = (table.tag(), table.registry()); if tag.get("DataVersion") != Some(&Tag::Int(DATA_VERSION)) { return Err(invalid("collision DataVersion mismatch")); } - let Some(Tag::List(states)) = tag.get("states") else { - return Err(invalid("missing collision state identities")); - }; - if states.len() != registry.len() { - return Err(invalid("collision registry size mismatch")); - } - for (id, state) in states.iter().enumerate() { - if registry.state(BlockStateId(id as u32)) != Some(&BlockState::from_nbt(state)?) { - return Err(invalid("collision state identities differ")); - } - } let Some(Tag::List(entries)) = tag.get("shapes") else { return Err(invalid("missing collision shapes")); }; diff --git a/crates/horizon-world/src/level/data.rs b/crates/horizon-world/src/level/data.rs index 42f8f75de..e0ef0dca1 100644 --- a/crates/horizon-world/src/level/data.rs +++ b/crates/horizon-world/src/level/data.rs @@ -8,8 +8,7 @@ use crate::{ collision::CachedCollisionRegistry, compression::invalid, fluid::{FluidBlockBindings, FluidKind, FluidState}, - nbt::{self, Limits}, - registry::{BlockRegistry, BlockStateId}, + registry::{BlockRegistry, BlockStateId, BlockTableFile}, worldgen::tags::TagDefinitions, }; use horizon_core::geometry::{Aabb, Direction, ShapeOp, Vec3, VoxelShape}; @@ -21,6 +20,9 @@ use std::{ path::Path, sync::Arc, }; +#[path = "data/cache.rs"] +mod cache; +pub(super) use cache::set_dir as set_cache_dir; /// Per-state flags read from vanilla accessors over an empty level. pub mod state_flags { @@ -251,7 +253,7 @@ macro_rules! named_blocks { #[derive(Clone, Copy, Debug)] pub struct Named { $(pub $field: BlockId,)* } impl Named { - fn resolve(table: &StateTable) -> io::Result<Self> { + pub(super) fn resolve(table: &StateTable) -> io::Result<Self> { Ok(Self { $($field: table.block_by_name(concat!("minecraft:", $name)).ok_or_else(|| invalid(concat!("missing block ", $name)))?,)* }) } } @@ -393,7 +395,7 @@ macro_rules! tags { #[derive(Clone, Debug, Default)] pub struct Tags { $(pub $field: Arc<BlockSet>,)* } impl Tags { - fn resolve(all: &HashMap<String, Arc<BlockSet>>) -> Self { + pub(super) fn resolve(all: &HashMap<String, Arc<BlockSet>>) -> Self { Self { $($field: all.get(concat!("minecraft:", $name)).cloned().unwrap_or_default(),)* } } } @@ -487,7 +489,7 @@ macro_rules! props { #[derive(Clone, Copy, Debug)] pub struct Props { $(pub $field: PropKey,)* } impl Props { - fn resolve(table: &mut StateTable) -> Self { + pub(super) fn resolve(table: &mut StateTable) -> Self { Self { $($field: table.key($name),)* } } } @@ -611,65 +613,71 @@ fn set_type(value: &Value, cache: &mut HashMap<String, Arc<str>>) -> Option<SetT } /// Build the block-state registry from the resources' `facts.json` (the -/// same source the native client uses). +/// same source the native client uses), streamed. pub fn load_registry(root: &Path) -> io::Result<BlockRegistry> { - use crate::{DATA_VERSION, nbt::Tag}; - fn tag(value: &Value) -> io::Result<Tag> { - Ok(match value { - Value::String(value) => Tag::String(value.as_str().into()), - Value::Number(value) => Tag::Int( - value - .as_i64() - .and_then(|v| i32::try_from(v).ok()) - .ok_or_else(|| fail("invalid registry number"))?, - ), - Value::Array(values) => Tag::List(values.iter().map(tag).collect::<io::Result<_>>()?), - Value::Object(values) => Tag::Compound( - values - .iter() - .map(|(name, value)| Ok((name.as_str().into(), tag(value)?))) - .collect::<io::Result<_>>()?, - ), - _ => return Err(fail("unsupported registry value")), - }) - } - let facts = read_json(&root.join("facts.json"))?; - let states = facts["states"] - .as_array() - .ok_or_else(|| fail("missing registry state facts"))?; - BlockRegistry::from_nbt(&Tag::compound([ - ("DataVersion", Tag::Int(DATA_VERSION)), - ( - "states", - Tag::List( - states - .iter() - .map(|state| tag(&state[4])) - .collect::<io::Result<_>>()?, - ), - ), - ("defaults", tag(&facts["defaults"])?), - ])) + BlockRegistry::from_facts_json(File::open(root.join("facts.json"))?) } pub(super) fn load(registry: &BlockRegistry, root: &Path) -> io::Result<Behaviours> { - let behaviour = read_json(&root.join("block-behaviours.json"))?; - load_with_behaviour_json(registry, root, &behaviour) + cached(registry, root, || load_uncached(registry, root)) } +/// [`load`] with `block-behaviours.json` already parsed by the caller. pub(super) fn load_with_behaviour_json( registry: &BlockRegistry, root: &Path, behaviour: &Value, +) -> io::Result<Behaviours> { + cached(registry, root, || { + build_with_behaviour_json(registry, root, behaviour) + }) +} + +/// [`load`] with the client's typed gameplay projection. The snapshot is +/// still tried first: on a hit the projection is not needed. +pub(super) fn load_with_gameplay<G: GameplayStateSource>( + registry: &BlockRegistry, + root: &Path, + behaviour: &Value, + gameplay_states: &[G], +) -> io::Result<Behaviours> { + cached(registry, root, || { + build_with_gameplay(registry, root, behaviour, gameplay_states) + }) +} + +/// The snapshot when it matches, else `build` and refresh the snapshot. +fn cached( + registry: &BlockRegistry, + root: &Path, + build: impl FnOnce() -> io::Result<Behaviours>, +) -> io::Result<Behaviours> { + if let Some(behaviours) = cache::load(registry, root) { + return Ok(behaviours); + } + let behaviours = build()?; + cache::store(registry, root, &behaviours); + Ok(behaviours) +} + +fn load_uncached(registry: &BlockRegistry, root: &Path) -> io::Result<Behaviours> { + let behaviour = read_json(&root.join("block-behaviours.json"))?; + build_with_behaviour_json(registry, root, &behaviour) +} + +fn build_with_behaviour_json( + registry: &BlockRegistry, + root: &Path, + behaviour: &Value, ) -> io::Result<Behaviours> { let gameplay = read_json(&root.join("gameplay-facts.json"))?; let gameplay_states = gameplay["states"] .as_array() .ok_or_else(|| fail("missing gameplay states"))?; - load_with_gameplay(registry, root, behaviour, gameplay_states) + build_with_gameplay(registry, root, behaviour, gameplay_states) } -pub(super) fn load_with_gameplay<G: GameplayStateSource>( +fn build_with_gameplay<G: GameplayStateSource>( registry: &BlockRegistry, root: &Path, behaviour: &Value, @@ -742,14 +750,8 @@ pub(super) fn load_with_gameplay<G: GameplayStateSource>( }; // Collision shapes. - let collision_tag = nbt::read_named( - &fs::read(root.join("cached-collision.nbt"))?, - Limits::default(), - ) - .map_err(|e| invalid(e.to_string()))? - .0 - .tag; - let cached = CachedCollisionRegistry::from_nbt(&collision_tag, registry)?; + let collision_file = BlockTableFile::open(&root.join("cached-collision.nbt"), registry)?; + let cached = CachedCollisionRegistry::from_table(collision_file.table("collision")?)?; let mut shapes: Vec<VoxelShape> = Vec::new(); let mut shape_ids = Vec::with_capacity(table.state_count()); for id in 0..table.state_count() { diff --git a/crates/horizon-world/src/level/data/cache.rs b/crates/horizon-world/src/level/data/cache.rs new file mode 100644 index 000000000..59bc1f8d8 --- /dev/null +++ b/crates/horizon-world/src/level/data/cache.rs @@ -0,0 +1,867 @@ +//! A disposable, versioned snapshot of resolved behaviour tables. +//! +//! The source resources remain authoritative. Failed reads, unsupported +//! versions, corrupt payloads and write failures always use the normal loader. +//! The snapshot lives in the game's own cache folder ([`set_dir`]), never in the +//! resource folder: that one is shared by worktrees and copied between installed +//! versions by the launcher. Its key includes the running executable, so a build +//! that resolves the tables differently never reads another build's snapshot. +use super::*; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use std::{ + io::Write, + path::PathBuf, + sync::{ + OnceLock, + atomic::{AtomicU64, Ordering}, + }, + time::UNIX_EPOCH, +}; + +const MAGIC: &[u8; 8] = b"HZBEHAV\0"; +const VERSION: u32 = 2; +const FILE_NAME: &str = "behaviours-v2.bin"; +const MAX_FILE_BYTES: usize = 128 * 1024 * 1024; +const MAX_DECODED_BYTES: usize = 256 * 1024 * 1024; +static NEXT_TEMP: AtomicU64 = AtomicU64::new(0); +static DIR: OnceLock<PathBuf> = OnceLock::new(); + +/// Keep snapshots in `dir` (the client passes `<game folder>/.horizon-cache`). +/// Without it (tests, tools, servers), `Behaviours::load` always uses the source loader. +pub(in crate::level) fn set_dir(dir: PathBuf) { + let _ = DIR.set(dir); +} + +#[derive(Serialize, Deserialize)] +struct Snapshot { + tags: HashMap<String, Vec<u64>>, + facts: Vec<Fact>, + shapes: Vec<Shape>, + shape_info: Vec<[u8; 3]>, + fluid_tags: [u8; 6], + blocks: Vec<Block>, + items: HashMap<String, Item>, +} + +#[derive(Serialize, Deserialize)] +struct Fact { + flags: u32, + sturdy: u32, + weak: u32, + strong: u32, + fluid_kind: u8, + fluid_amount: u8, + fluid_falling: bool, + emission: u8, + dampening: u8, + hardness: f32, + push: u8, + instrument: u8, + shape: i32, +} + +#[derive(Serialize, Deserialize)] +struct Shape { + coords: [Vec<f64>; 3], + occupied: Vec<bool>, + cube: bool, + coordinate_cubes: [bool; 3], + min: [i32; 3], + max: [i32; 3], + /// Preserve the shared singleton identities used by vanilla shape shortcuts. + singleton: u8, +} + +#[derive(Serialize, Deserialize)] +struct SetTypeSnapshot { + name: String, + can_open_by_hand: bool, + can_open_by_wind_charge: bool, + arrows: bool, + mobs_only: bool, + door_open: String, + door_close: String, + trapdoor_open: String, + trapdoor_close: String, + plate_on: String, + plate_off: String, + button_on: String, + button_off: String, +} + +#[derive(Serialize, Deserialize)] +struct SoundsSnapshot { + volume: f32, + pitch: f32, + place: String, + break_: String, + fall: String, +} + +#[derive(Serialize, Deserialize)] +struct Block { + chain: Vec<u16>, + interfaces: u16, + set_type: Option<SetTypeSnapshot>, + gate_open: Option<String>, + gate_close: Option<String>, + open_sound: Option<String>, + close_sound: Option<String>, + ticks_to_stay_pressed: i32, + max_weight: i32, + link: Option<u16>, + link2: Option<u16>, + support_tag: Option<Vec<u64>>, + support_tag2: Option<Vec<u64>>, + feature: Option<String>, + grow_probability: f64, + growth_direction: u8, + schedule_fluid_ticks: bool, + weather: u8, + ignite_odds: i32, + burn_odds: i32, + sounds: SoundsSnapshot, + item: String, + dynamic_shape: bool, +} + +#[derive(Serialize, Deserialize)] +struct Item { + block: u16, + wall_block: Option<u16>, + attachment: Option<u8>, + kind: u8, + game_master: bool, +} + +fn fingerprint(registry: &BlockRegistry, root: &Path) -> io::Result<[u8; 32]> { + let mut hasher = Sha256::new(); + hasher.update(b"horizon resolved behaviours 2"); + // The build that resolved the tables: the version constant alone misses + // loader changes that keep the snapshot layout. + let exe = std::env::current_exe()?; + let built = fs::metadata(&exe)?; + hash_str(&mut hasher, &exe.to_string_lossy()); + hasher.update(built.len().to_le_bytes()); + let modified = built + .modified()? + .duration_since(UNIX_EPOCH) + .map_err(|e| invalid(e.to_string()))?; + hasher.update(modified.as_nanos().to_le_bytes()); + for relative in [ + "facts.json", + "block-behaviours.json", + "gameplay-facts.json", + "block-tags.json", + "cached-collision.nbt", + ] { + hash_file(&mut hasher, root, &root.join(relative))?; + } + let mut fluids: Vec<PathBuf> = fs::read_dir(root.join("data/minecraft/tags/fluid"))? + .map(|entry| entry.map(|entry| entry.path())) + .collect::<io::Result<_>>()?; + fluids.sort(); + for path in fluids { + hash_file(&mut hasher, root, &path)?; + } + // The registry argument may be modified independently of facts.json. + hasher.update((registry.len() as u64).to_le_bytes()); + let mut seen = std::collections::HashSet::new(); + for index in 0..registry.len() { + let state = registry + .state(BlockStateId(index as u32)) + .ok_or_else(|| invalid("missing registry state"))?; + hash_str(&mut hasher, &state.name); + let mut props: Vec<_> = state.properties.iter().collect(); + props.sort_by_key(|(key, _)| *key); + hasher.update((props.len() as u32).to_le_bytes()); + for (key, value) in props { + hash_str(&mut hasher, key); + hash_str(&mut hasher, value); + } + if seen.insert(state.name.as_str()) { + let default = registry + .default_state(&state.name) + .ok_or_else(|| invalid("missing default state"))?; + hasher.update(default.0.to_le_bytes()); + } + } + Ok(hasher.finalize().into()) +} + +fn hash_str(hasher: &mut Sha256, text: &str) { + hasher.update((text.len() as u64).to_le_bytes()); + hasher.update(text.as_bytes()); +} + +fn hash_file(hasher: &mut Sha256, root: &Path, path: &Path) -> io::Result<()> { + let relative = path + .strip_prefix(root) + .map_err(|e| invalid(e.to_string()))?; + hash_str(hasher, &relative.to_string_lossy()); + let bytes = fs::read(path)?; + hasher.update((bytes.len() as u64).to_le_bytes()); + hasher.update(bytes); + Ok(()) +} + +fn direction(index: u8) -> io::Result<Direction> { + Direction::ALL + .get(index as usize) + .copied() + .ok_or_else(|| invalid("cached direction out of range")) +} +fn direction_index(direction: Direction) -> u8 { + Direction::ALL.iter().position(|d| *d == direction).unwrap() as u8 +} +fn class(index: u16) -> io::Result<Class> { + Class::ALL + .get(index as usize) + .copied() + .ok_or_else(|| invalid("cached block class out of range")) +} +fn kind(index: u8) -> io::Result<ItemKind> { + Ok(match index { + 0 => ItemKind::Block, + 1 => ItemKind::DoubleHigh, + 2 => ItemKind::StandingAndWall, + 3 => ItemKind::Scaffolding, + 4 => ItemKind::PlaceOnWater, + 5 => ItemKind::HangingSign, + 6 => ItemKind::SolidBucket, + 7 => ItemKind::Other, + _ => return Err(invalid("cached item kind out of range")), + }) +} +fn kind_index(kind: ItemKind) -> u8 { + match kind { + ItemKind::Block => 0, + ItemKind::DoubleHigh => 1, + ItemKind::StandingAndWall => 2, + ItemKind::Scaffolding => 3, + ItemKind::PlaceOnWater => 4, + ItemKind::HangingSign => 5, + ItemKind::SolidBucket => 6, + ItemKind::Other => 7, + } +} +fn push(index: u8) -> io::Result<PushReaction> { + Ok(match index { + 0 => PushReaction::PushPull, + 1 => PushReaction::Push, + 2 => PushReaction::Popped, + 3 => PushReaction::Immoveable, + 4 => PushReaction::IgnoreEntity, + _ => return Err(invalid("cached push reaction out of range")), + }) +} +fn push_index(push: PushReaction) -> u8 { + match push { + PushReaction::PushPull => 0, + PushReaction::Push => 1, + PushReaction::Popped => 2, + PushReaction::Immoveable => 3, + PushReaction::IgnoreEntity => 4, + } +} + +impl From<&SetType> for SetTypeSnapshot { + fn from(value: &SetType) -> Self { + Self { + name: value.name.to_string(), + can_open_by_hand: value.can_open_by_hand, + can_open_by_wind_charge: value.can_open_by_wind_charge, + arrows: value.arrows, + mobs_only: value.mobs_only, + door_open: value.door_open.to_string(), + door_close: value.door_close.to_string(), + trapdoor_open: value.trapdoor_open.to_string(), + trapdoor_close: value.trapdoor_close.to_string(), + plate_on: value.plate_on.to_string(), + plate_off: value.plate_off.to_string(), + button_on: value.button_on.to_string(), + button_off: value.button_off.to_string(), + } + } +} +impl From<SetTypeSnapshot> for SetType { + fn from(value: SetTypeSnapshot) -> Self { + Self { + name: value.name.into(), + can_open_by_hand: value.can_open_by_hand, + can_open_by_wind_charge: value.can_open_by_wind_charge, + arrows: value.arrows, + mobs_only: value.mobs_only, + door_open: Arc::from(value.door_open), + door_close: Arc::from(value.door_close), + trapdoor_open: Arc::from(value.trapdoor_open), + trapdoor_close: Arc::from(value.trapdoor_close), + plate_on: Arc::from(value.plate_on), + plate_off: Arc::from(value.plate_off), + button_on: Arc::from(value.button_on), + button_off: Arc::from(value.button_off), + } + } +} +impl From<&Sounds> for SoundsSnapshot { + fn from(value: &Sounds) -> Self { + Self { + volume: value.volume, + pitch: value.pitch, + place: value.place.to_string(), + break_: value.break_.to_string(), + fall: value.fall.to_string(), + } + } +} +impl From<SoundsSnapshot> for Sounds { + fn from(value: SoundsSnapshot) -> Self { + Self { + volume: value.volume, + pitch: value.pitch, + place: Arc::from(value.place), + break_: Arc::from(value.break_), + fall: Arc::from(value.fall), + } + } +} + +impl Snapshot { + fn capture(behaviours: &Behaviours) -> Self { + let tags = behaviours + .all_tags + .iter() + .map(|(key, value)| (key.clone(), value.0.clone())) + .collect(); + let facts = behaviours + .facts + .iter() + .map(|fact| Fact { + flags: fact.flags, + sturdy: fact.sturdy, + weak: fact.weak, + strong: fact.strong, + fluid_kind: fact.fluid.kind() as u8, + fluid_amount: fact.fluid.amount(), + fluid_falling: fact.fluid.falling(), + emission: fact.emission, + dampening: fact.dampening, + hardness: fact.hardness, + push: push_index(fact.push), + instrument: fact.instrument, + shape: fact.shape, + }) + .collect(); + let shapes = behaviours + .shapes + .iter() + .map(|shape| { + let sizes = shape.sizes(); + let mut occupied = Vec::with_capacity(sizes.iter().product()); + for x in 0..sizes[0] { + for y in 0..sizes[1] { + for z in 0..sizes[2] { + occupied.push(shape.is_full(x as i32, y as i32, z as i32)); + } + } + } + let (min, max) = shape.discrete_bounds(); + Shape { + coords: std::array::from_fn(|axis| { + shape + .coordinates( + [ + horizon_core::geometry::Axis::X, + horizon_core::geometry::Axis::Y, + horizon_core::geometry::Axis::Z, + ][axis], + ) + .to_vec() + }), + occupied, + cube: shape.is_cube(), + coordinate_cubes: shape.cube_coordinate_axes(), + min, + max, + singleton: if shape.same_identity(&VoxelShape::block()) { + 1 + } else if shape.same_identity(&VoxelShape::empty()) { + 2 + } else { + 0 + }, + } + }) + .collect(); + let shape_info = behaviours + .shape_info + .iter() + .map(|info| [info.full_faces, info.nonempty_faces, info.wall_cover]) + .collect(); + let f = behaviours.fluid_tags; + let fluid_tags = [ + f.water, + f.lava, + f.supports_frogspawn, + f.supports_lily_pad, + f.supports_sugar_cane_adjacently, + f.bubble_column_can_occupy, + ]; + let blocks = behaviours + .blocks + .iter() + .map(|block| Block { + chain: block.chain.iter().map(|class| *class as u16).collect(), + interfaces: block.interfaces, + set_type: block + .set_type + .as_ref() + .map(|value| SetTypeSnapshot::from(value.as_ref())), + gate_open: block.gate_open.as_ref().map(|v| v.to_string()), + gate_close: block.gate_close.as_ref().map(|v| v.to_string()), + open_sound: block.open_sound.as_ref().map(|v| v.to_string()), + close_sound: block.close_sound.as_ref().map(|v| v.to_string()), + ticks_to_stay_pressed: block.ticks_to_stay_pressed, + max_weight: block.max_weight, + link: block.link.map(|v| v.0), + link2: block.link2.map(|v| v.0), + support_tag: block.support_tag.as_ref().map(|v| v.0.clone()), + support_tag2: block.support_tag2.as_ref().map(|v| v.0.clone()), + feature: block.feature.as_ref().map(|v| v.to_string()), + grow_probability: block.grow_probability, + growth_direction: direction_index(block.growth_direction), + schedule_fluid_ticks: block.schedule_fluid_ticks, + weather: block.weather, + ignite_odds: block.ignite_odds, + burn_odds: block.burn_odds, + sounds: SoundsSnapshot::from(block.sounds.as_ref()), + item: block.item.to_string(), + dynamic_shape: block.dynamic_shape, + }) + .collect(); + let items = behaviours + .items + .iter() + .map(|(name, item)| { + ( + name.clone(), + Item { + block: item.block.0, + wall_block: item.wall_block.map(|v| v.0), + attachment: item.attachment.map(direction_index), + kind: kind_index(item.kind), + game_master: item.game_master, + }, + ) + }) + .collect(); + Self { + tags, + facts, + shapes, + shape_info, + fluid_tags, + blocks, + items, + } + } + + fn restore(self, registry: &BlockRegistry) -> io::Result<Behaviours> { + let mut table = StateTable::new(registry)?; + let props = Props::resolve(&mut table); + let named = Named::resolve(&table)?; + if self.facts.len() != table.state_count() + || self.blocks.len() != table.block_count() + || self.shape_info.len() != self.shapes.len() + { + return Err(invalid("cached behaviour table size mismatch")); + } + let all_tags: HashMap<String, Arc<BlockSet>> = self + .tags + .into_iter() + .map(|(key, value)| (key, Arc::new(BlockSet(value)))) + .collect(); + let tags = Tags::resolve(&all_tags); + let shapes = self + .shapes + .into_iter() + .map(|wire| { + let shape = match wire.singleton { + 1 => VoxelShape::block(), + 2 => VoxelShape::empty(), + 0 => { + if wire.cube { + let sizes = std::array::from_fn(|axis| { + wire.coords[axis].len().saturating_sub(1) + }); + VoxelShape::from_cube(sizes, &wire.occupied) + .map_err(|e| invalid(e.to_string()))? + } else { + VoxelShape::from_mixed_grid( + wire.coords, + &wire.occupied, + wire.coordinate_cubes, + ) + .map_err(|e| invalid(e.to_string()))? + } + } + _ => return Err(invalid("invalid cached shape identity")), + }; + shape + .with_discrete_bounds(wire.min, wire.max) + .map_err(|e| invalid(e.to_string())) + }) + .collect::<io::Result<Vec<_>>>()?; + let shape_info = self + .shape_info + .into_iter() + .map(|row| ShapeInfo { + full_faces: row[0], + nonempty_faces: row[1], + wall_cover: row[2], + }) + .collect(); + let facts = self + .facts + .into_iter() + .map(|wire| { + if wire.shape < -1 || wire.shape >= shapes.len() as i32 { + return Err(invalid("cached shape index out of range")); + } + let fluid_kind = FluidKind::ALL + .get(wire.fluid_kind as usize) + .copied() + .ok_or_else(|| invalid("cached fluid kind out of range"))?; + Ok(StateFacts { + flags: wire.flags, + sturdy: wire.sturdy, + weak: wire.weak, + strong: wire.strong, + fluid: FluidState::new(fluid_kind, wire.fluid_amount, wire.fluid_falling), + emission: wire.emission, + dampening: wire.dampening, + hardness: wire.hardness, + push: push(wire.push)?, + instrument: wire.instrument, + shape: wire.shape, + }) + }) + .collect::<io::Result<Vec<_>>>()?; + let f = self.fluid_tags; + let fluid_tags = FluidTags { + water: f[0], + lava: f[1], + supports_frogspawn: f[2], + supports_lily_pad: f[3], + supports_sugar_cane_adjacently: f[4], + bubble_column_can_occupy: f[5], + }; + let blocks = self + .blocks + .into_iter() + .map(|wire| { + let chain = wire + .chain + .into_iter() + .map(class) + .collect::<io::Result<Vec<_>>>()?; + let mut classes = ClassSet::default(); + for class in &chain { + classes.insert(*class); + } + let class = chain.first().copied().unwrap_or(Class::Block); + for link in [wire.link, wire.link2].into_iter().flatten() { + if link as usize >= table.block_count() { + return Err(invalid("cached block link out of range")); + } + } + Ok(BlockData { + class, + chain, + classes, + interfaces: wire.interfaces, + set_type: wire.set_type.map(|v| Arc::new(SetType::from(v))), + gate_open: wire.gate_open.map(Arc::from), + gate_close: wire.gate_close.map(Arc::from), + open_sound: wire.open_sound.map(Arc::from), + close_sound: wire.close_sound.map(Arc::from), + ticks_to_stay_pressed: wire.ticks_to_stay_pressed, + max_weight: wire.max_weight, + link: wire.link.map(BlockId), + link2: wire.link2.map(BlockId), + support_tag: wire.support_tag.map(|v| Arc::new(BlockSet(v))), + support_tag2: wire.support_tag2.map(|v| Arc::new(BlockSet(v))), + feature: wire.feature.map(Arc::from), + grow_probability: wire.grow_probability, + growth_direction: direction(wire.growth_direction)?, + schedule_fluid_ticks: wire.schedule_fluid_ticks, + weather: wire.weather, + ignite_odds: wire.ignite_odds, + burn_odds: wire.burn_odds, + sounds: Arc::new(Sounds::from(wire.sounds)), + item: Arc::from(wire.item), + dynamic_shape: wire.dynamic_shape, + }) + }) + .collect::<io::Result<Vec<_>>>()?; + let items = self + .items + .into_iter() + .map(|(name, wire)| { + if wire.block as usize >= table.block_count() + || wire + .wall_block + .is_some_and(|id| id as usize >= table.block_count()) + { + return Err(invalid("cached item block out of range")); + } + Ok(( + name, + ItemBinding { + block: BlockId(wire.block), + wall_block: wire.wall_block.map(BlockId), + attachment: wire.attachment.map(direction).transpose()?, + kind: kind(wire.kind)?, + game_master: wire.game_master, + }, + )) + }) + .collect::<io::Result<HashMap<_, _>>>()?; + let liquid_blocks = FluidBlockBindings::new(registry)?; + let mut dynamic = HashMap::new(); + for (id, fact) in facts.iter().enumerate() { + if fact.shape < 0 { + let state = BlockStateId(id as u32); + if let Some(support) = crate::dynamic_support::DynamicSupport::from_state( + registry.state(state).unwrap(), + )? { + dynamic.insert(state, support); + } + } + } + Ok(Behaviours { + table, + props, + named, + tags, + all_tags, + facts, + shapes, + shape_info, + fluid_tags, + blocks, + items, + liquid_blocks, + dynamic, + occlusion_cache: Default::default(), + growth: Default::default(), + }) + } +} + +pub(super) fn load(registry: &BlockRegistry, root: &Path) -> Option<Behaviours> { + load_from(DIR.get()?, registry, root) +} + +fn load_from(dir: &Path, registry: &BlockRegistry, root: &Path) -> Option<Behaviours> { + let bytes = fs::read(dir.join(FILE_NAME)).ok()?; + let fingerprint = fingerprint(registry, root).ok()?; + // Validate bounds before decoding untrusted cache contents. + if bytes.len() < 8 + 4 + 32 + 32 || bytes.len() > MAX_FILE_BYTES { + return None; + } + if &bytes[..8] != MAGIC + || u32::from_le_bytes(bytes[8..12].try_into().ok()?) != VERSION + || bytes[12..44] != fingerprint + { + return None; + } + let body = &bytes[76..]; + if Sha256::digest(body).as_slice() != &bytes[44..76] { + return None; + } + let expanded_len = u32::from_le_bytes(body.get(..4)?.try_into().ok()?) as usize; + if expanded_len > MAX_DECODED_BYTES { + return None; + } + let expanded = lz4_flex::decompress_size_prepended(body).ok()?; + let snapshot: Snapshot = postcard::from_bytes(&expanded).ok()?; + snapshot.restore(registry).ok() +} + +pub(super) fn store(registry: &BlockRegistry, root: &Path, behaviours: &Behaviours) { + if let Some(dir) = DIR.get() { + let _ = write(dir, registry, root, behaviours); + } +} + +fn write( + dir: &Path, + registry: &BlockRegistry, + root: &Path, + behaviours: &Behaviours, +) -> io::Result<()> { + let fingerprint = fingerprint(registry, root)?; + let snapshot = Snapshot::capture(behaviours); + let serialized = postcard::to_allocvec(&snapshot).map_err(|e| invalid(e.to_string()))?; + let body = lz4_flex::compress_prepend_size(&serialized); + if body.len() + 76 > MAX_FILE_BYTES { + return Err(invalid("behaviour cache too large")); + } + let mut bytes = Vec::with_capacity(76 + body.len()); + bytes.extend_from_slice(MAGIC); + bytes.extend_from_slice(&VERSION.to_le_bytes()); + bytes.extend_from_slice(&fingerprint); + bytes.extend_from_slice(&Sha256::digest(&body)); + bytes.extend_from_slice(&body); + fs::create_dir_all(dir)?; + let path = dir.join(FILE_NAME); + let tmp = dir.join(format!( + "{FILE_NAME}.tmp-{}-{}", + std::process::id(), + NEXT_TEMP.fetch_add(1, Ordering::Relaxed) + )); + let result = (|| { + let mut file = fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(&tmp)?; + file.write_all(&bytes)?; + file.sync_all()?; + fs::rename(&tmp, &path) + })(); + if result.is_err() { + let _ = fs::remove_file(&tmp); + } + result +} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::atomic::AtomicU64; + + static NEXT_FIXTURE: AtomicU64 = AtomicU64::new(0); + + struct FixtureDir(PathBuf); + impl Drop for FixtureDir { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } + } + + fn fixture() -> Option<FixtureDir> { + let source = std::env::var_os("HORIZON_RESOURCES") + .map(PathBuf::from) + .unwrap_or_else(|| { + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3") + }); + if !source.join("block-behaviours.json").exists() { + eprintln!("skipping behaviour cache tests: no client resources"); + return None; + } + let dir = std::env::temp_dir().join(format!( + "horizon-behaviour-cache-test-{}-{}", + std::process::id(), + NEXT_FIXTURE.fetch_add(1, Ordering::Relaxed), + )); + fs::create_dir_all(dir.join("data/minecraft/tags/fluid")).unwrap(); + for file in [ + "facts.json", + "block-behaviours.json", + "gameplay-facts.json", + "block-tags.json", + "cached-collision.nbt", + ] { + fs::copy(source.join(file), dir.join(file)).unwrap(); + } + for entry in fs::read_dir(source.join("data/minecraft/tags/fluid")).unwrap() { + let entry = entry.unwrap(); + if entry.file_type().unwrap().is_file() { + fs::copy( + entry.path(), + dir.join("data/minecraft/tags/fluid") + .join(entry.file_name()), + ) + .unwrap(); + } + } + Some(FixtureDir(dir)) + } + + #[test] + fn resolved_snapshot_roundtrips_all_fields() { + let Some(root) = fixture() else { + return; + }; + let registry = super::super::load_registry(&root.0).unwrap(); + let original = super::super::load_uncached(®istry, &root.0).unwrap(); + let captured = Snapshot::capture(&original); + let restored = captured.restore(®istry).unwrap(); + assert_eq!( + serde_json::to_value(Snapshot::capture(&original)).unwrap(), + serde_json::to_value(Snapshot::capture(&restored)).unwrap(), + "all resolved fields, shape grids and identities should roundtrip", + ); + } + + #[test] + fn cache_invalidates_sources_and_recovers_from_corruption() { + let Some(root) = fixture() else { + return; + }; + // The game's cache folder, apart from the resources. + let dir = root.0.join("game/.horizon-cache"); + let registry = super::super::load_registry(&root.0).unwrap(); + // What `data::load` does once `set_dir` gave it a folder. + let load_or_build = || { + load_from(&dir, ®istry, &root.0).unwrap_or_else(|| { + let built = super::super::load_uncached(®istry, &root.0).unwrap(); + write(&dir, ®istry, &root.0, &built).unwrap(); + built + }) + }; + assert!(load_from(&dir, ®istry, &root.0).is_none()); + let initial = load_or_build(); + assert!(dir.join(FILE_NAME).is_file()); + assert!( + !root.0.join(FILE_NAME).exists(), + "the resource folder stays untouched" + ); + let hit = load_from(&dir, ®istry, &root.0).expect("cache hit"); + assert_eq!(initial.table.state_count(), hit.table.state_count()); + let tags = root.0.join("block-tags.json"); + fs::OpenOptions::new() + .append(true) + .open(&tags) + .unwrap() + .write_all(b"\n") + .unwrap(); + assert!( + load_from(&dir, ®istry, &root.0).is_none(), + "changed resource must invalidate" + ); + load_or_build(); + assert!( + load_from(&dir, ®istry, &root.0).is_some(), + "fallback should replace cache" + ); + let cache_file = dir.join(FILE_NAME); + let mut bytes = fs::read(&cache_file).unwrap(); + bytes[80] ^= 0x80; + fs::write(&cache_file, bytes).unwrap(); + assert!( + load_from(&dir, ®istry, &root.0).is_none(), + "checksum failure must invalidate" + ); + load_or_build(); + assert!(load_from(&dir, ®istry, &root.0).is_some()); + // A snapshot written under another key (another build of the game, + // other resources) is never trusted. + let mut other = fs::read(&cache_file).unwrap(); + other[12] ^= 1; + fs::write(&cache_file, other).unwrap(); + assert!(load_from(&dir, ®istry, &root.0).is_none()); + } +} diff --git a/crates/horizon-world/src/level/mod.rs b/crates/horizon-world/src/level/mod.rs index dbd877fc5..c706ca280 100644 --- a/crates/horizon-world/src/level/mod.rs +++ b/crates/horizon-world/src/level/mod.rs @@ -301,6 +301,12 @@ pub trait LevelAccess { fn max_y(&self) -> i32 { 320 } + /// `getHeight(WORLD_SURFACE, x, z)`: one above the column's highest + /// block that isn't air. Only air lies above it, so scans for the other + /// heightmaps start there. The default, `max_y`, scans the whole column. + fn world_surface(&self, _x: i32, _z: i32) -> i32 { + self.max_y() + } /// Vanilla `level.random` (a `LegacyRandomSource`). fn random(&mut self) -> &mut LegacyRandom; fn game_time(&self) -> i64; @@ -484,6 +490,13 @@ impl Behaviours { ) -> io::Result<Self> { data::load_with_behaviour_json(registry, resources_dir, behaviour) } + + /// Keep a snapshot of the resolved tables in `dir` (a game folder's + /// `.horizon-cache`), so later launches of the same build restore it instead + /// of resolving the resources again. Without it, [`Self::load`] never caches. + pub fn set_cache_dir(dir: std::path::PathBuf) { + data::set_cache_dir(dir); + } pub fn facts(&self, state: BlockStateId) -> &StateFacts { &self.facts[state.0 as usize] } diff --git a/crates/horizon-world/src/level/weather.rs b/crates/horizon-world/src/level/weather.rs index c43d96b15..93027d889 100644 --- a/crates/horizon-world/src/level/weather.rs +++ b/crates/horizon-world/src/level/weather.rs @@ -40,14 +40,16 @@ impl Behaviours { BiomePrecipitation::Rain } } - /// `getHeightmapPos(MOTION_BLOCKING, pos)`. + /// `getHeightmapPos(MOTION_BLOCKING, pos)`, scanning down from the + /// world surface (air neither blocks motion nor holds a fluid). pub fn motion_blocking_top<L: LevelAccess + ?Sized>( &self, level: &L, pos: BlockPos, ) -> BlockPos { let blocks = self.tag("minecraft:blocks_motion_in_heightmap"); - for y in (level.min_y()..level.max_y()).rev() { + let top = level.world_surface(pos.x, pos.z).min(level.max_y()); + for y in (level.min_y()..top).rev() { let state = level.block_state(BlockPos::new(pos.x, y, pos.z)); let fluid = self.fluid(state).kind() != FluidKind::Empty; if fluid || blocks.is_some_and(|t| t.contains(self.block(state))) { diff --git a/crates/horizon-world/src/movement_properties.rs b/crates/horizon-world/src/movement_properties.rs index 84cafae1e..9e1dc6236 100644 --- a/crates/horizon-world/src/movement_properties.rs +++ b/crates/horizon-world/src/movement_properties.rs @@ -1,11 +1,10 @@ //! Registry-bound block movement coefficients, identities and reloadable tags. use crate::{ DATA_VERSION, - chunk::BlockState, compression::invalid, movement_support::MovementBlockWorld, nbt::Tag, - registry::{BlockRegistry, BlockStateId}, + registry::{BlockRegistry, BlockStateId, BlockTable}, }; use horizon_core::position::BlockPos; use std::{ @@ -55,20 +54,13 @@ impl<'r> MovementProperties<'r> { } pub fn from_nbt(tag: &Tag, registry: &'r BlockRegistry) -> io::Result<Self> { + Self::from_table(BlockTable::check(tag, registry, "movement")?) + } + pub fn from_table(table: BlockTable<'_, 'r>) -> io::Result<Self> { + let (tag, registry) = (table.tag(), table.registry()); if tag.get("DataVersion") != Some(&Tag::Int(DATA_VERSION)) { return Err(invalid("movement properties DataVersion mismatch")); } - let Some(Tag::List(states)) = tag.get("states") else { - return Err(invalid("missing movement state identities")); - }; - if states.len() != registry.len() { - return Err(invalid("movement registry size mismatch")); - } - for (id, state) in states.iter().enumerate() { - if registry.state(BlockStateId(id as u32)) != Some(&BlockState::from_nbt(state)?) { - return Err(invalid("movement state identities differ")); - } - } let Some(Tag::Compound(blocks)) = tag.get("blocks") else { return Err(invalid("missing movement definitions")); }; diff --git a/crates/horizon-world/src/nbt.rs b/crates/horizon-world/src/nbt.rs index 7e4d76a41..a8bed543a 100644 --- a/crates/horizon-world/src/nbt.rs +++ b/crates/horizon-world/src/nbt.rs @@ -100,11 +100,26 @@ impl Default for Limits { impl Limits { /// The prepared per-state block tables (`cached-collision.nbt`, /// `block-properties.nbt`, ...): trusted files with a row per block - /// state, which content mods' states grow past the default caps. + /// state, which content mods' states grow past the default caps + /// (vanilla's 35,723 states fill about a tenth of these; ATM10's ported + /// mods, 193,762 states, about half). pub fn tables() -> Self { + Self { + max_bytes: 1 << 30, + max_nodes: 128_000_000, + max_depth: 512, + } + } + + /// Structure templates (`structure/*.nbt`): vanilla reads them with + /// `NbtAccounter.unlimitedHeap()` (TemplateSource.readStructure), and + /// mods ship templates past the default node cap (YUNG's Better Ocean + /// Monuments' start piece, 115x64x126 blocks, holds millions of tags). + /// The byte cap stays as the only guard on the decompressed file. + pub fn templates() -> Self { Self { max_bytes: 1 << 28, - max_nodes: 16_000_000, + max_nodes: usize::MAX, max_depth: 512, } } @@ -122,13 +137,20 @@ impl std::fmt::Display for Error { } impl std::error::Error for Error {} -struct Reader<'a> { +/// Hands a streamed list's elements to the caller one at a time (see +/// [`read_named_streaming`]). +type Visit<'s> = &'s mut dyn FnMut(&mut ListElements<'_, '_, '_>) -> Result<(), Error>; + +struct Reader<'a, 's> { bytes: &'a [u8], offset: usize, limits: Limits, nodes: usize, + /// Lists with this key in the root compound or in a compound directly + /// under it are streamed to the visitor instead of kept. + stream: Option<(&'s str, Visit<'s>)>, } -impl<'a> Reader<'a> { +impl<'a> Reader<'a, '_> { fn error(&self, reason: &'static str) -> Error { Error { offset: self.offset, @@ -264,7 +286,11 @@ impl<'a> Reader<'a> { break; } let name = self.string()?; - let value = self.payload(id, depth + 1)?; + let value = if id == 9 && depth <= 1 && self.streams(&name) { + self.stream_list(depth + 1)? + } else { + self.payload(id, depth + 1)? + }; map.insert(name, value); // Java maps retain the last duplicate key. } Tag::Compound(map) @@ -288,6 +314,82 @@ impl<'a> Reader<'a> { _ => return Err(self.error("unknown tag type")), }) } + fn streams(&self, name: &JavaString) -> bool { + self.stream + .as_ref() + .is_some_and(|(key, _)| name.0.iter().copied().eq(key.encode_utf16())) + } + /// A list's payload handed to the visitor element by element; the tree + /// keeps it empty. Elements the visitor leaves are read and dropped. + fn stream_list(&mut self, depth: usize) -> Result<Tag, Error> { + if self.nodes >= self.limits.max_nodes { + return Err(self.error("node limit exceeded")); + } + self.nodes += 1; + if depth >= self.limits.max_depth { + return Err(self.error("depth limit exceeded")); + } + let element_id = self.byte()?; + let len = self.count(0)?; + if len > 0 && (element_id == 0 || element_id > 12) { + return Err(self.error("invalid list element type")); + } + if len > self.bytes.len().saturating_sub(self.offset) { + return Err(self.error("truncated list")); + } + let (key, visit) = self.stream.take().expect("streams() checked the visitor"); + let mut elements = ListElements { + reader: self, + element_id, + depth: depth + 1, + len, + left: len, + }; + let rest = visit(&mut elements).and_then(|()| { + while elements.next_element()?.is_some() {} + Ok(()) + }); + self.stream = Some((key, visit)); + rest.map(|()| Tag::List(Vec::new())) + } +} + +/// The elements of a list [`read_named_streaming`] streams, read one at a +/// time. +pub struct ListElements<'r, 'a, 's> { + reader: &'r mut Reader<'a, 's>, + element_id: u8, + depth: usize, + len: usize, + left: usize, +} +impl ListElements<'_, '_, '_> { + /// The list's length, as its header gives it. + pub fn len(&self) -> usize { + self.len + } + pub fn is_empty(&self) -> bool { + self.len == 0 + } + /// The next element, unwrapped as [`read_named`] unwraps mixed-list + /// elements; `None` past the last. + pub fn next_element(&mut self) -> Result<Option<Tag>, Error> { + if self.left == 0 { + return Ok(None); + } + self.left -= 1; + let mut value = self.reader.payload(self.element_id, self.depth)?; + if let Tag::Compound(map) = &mut value { + if map.len() == 1 && map.contains_key(&JavaString::default()) { + value = map.remove(&JavaString::default()).unwrap(); + } + } + Ok(Some(value)) + } + /// An error at the current position in the input. + pub fn error(&self, reason: &'static str) -> Error { + self.reader.error(reason) + } } /// Read a file root (type, modified-UTF name, payload), returning bytes consumed. @@ -297,6 +399,35 @@ pub fn read_named(bytes: &[u8], limits: Limits) -> Result<(NamedTag, usize), Err offset: 0, limits, nodes: 0, + stream: None, + }; + let id = reader.byte()?; + let name = if id == 0 { + JavaString::default() + } else { + reader.string()? + }; + let tag = reader.payload(id, 0)?; + Ok((NamedTag { name, tag }, reader.offset)) +} + +/// [`read_named`], except that lists keyed `key` in the root compound or in +/// a compound directly under it are not kept: `visit` reads each one's +/// elements as they come, and the tree holds it as an empty list. Per-state +/// tables check hundreds of thousands of state identities this way without +/// holding them as a tree (crate::registry::BlockTableFile). +pub fn read_named_streaming( + bytes: &[u8], + limits: Limits, + key: &str, + visit: &mut dyn FnMut(&mut ListElements<'_, '_, '_>) -> Result<(), Error>, +) -> Result<(NamedTag, usize), Error> { + let mut reader = Reader { + bytes, + offset: 0, + limits, + nodes: 0, + stream: Some((key, visit)), }; let id = reader.byte()?; let name = if id == 0 { @@ -315,6 +446,7 @@ pub fn read_network(bytes: &[u8], limits: Limits) -> Result<(Tag, usize), Error> offset: 0, limits, nodes: 0, + stream: None, }; let id = reader.byte()?; let tag = reader.payload(id, 0)?; @@ -657,6 +789,50 @@ impl Snbt<'_> { mod tests { use super::*; #[test] + fn streamed_lists_reach_the_visitor_and_stay_empty_in_the_tree() { + let list = |n: i32| Tag::List((0..n).map(Tag::Int).collect()); + let root = NamedTag { + name: "".into(), + tag: Tag::compound([ + ("states", list(3)), + ("inner", Tag::compound([("states", list(2))])), + ( + "deep", + Tag::compound([("inner", Tag::compound([("states", list(1))]))]), + ), + ("other", list(4)), + ]), + }; + let bytes = write_named(&root, Limits::default()).unwrap(); + let mut seen = Vec::new(); + let (read, used) = read_named_streaming(&bytes, Limits::default(), "states", &mut |l| { + let mut values = vec![l.len() as i32]; + // Elements left unread are skipped. + if let Some(Tag::Int(v)) = l.next_element()? { + values.push(v); + } + seen.push(values); + Ok(()) + }) + .unwrap(); + assert_eq!(used, bytes.len()); + // Compounds are written in key order: `inner` before `states`. + assert_eq!(seen, [vec![2, 0], vec![3, 0]]); + assert_eq!(read.tag.get("states"), Some(&Tag::List(Vec::new()))); + assert_eq!( + read.tag.get("inner").unwrap().get("states"), + Some(&Tag::List(Vec::new())) + ); + // Only the root and its direct children stream. + assert_eq!(read.tag.get("deep"), root.tag.get("deep")); + assert_eq!(read.tag.get("other"), root.tag.get("other")); + // A visitor's error stops the read. + let failed = read_named_streaming(&bytes, Limits::default(), "states", &mut |l| { + Err(l.error("refused")) + }); + assert_eq!(failed.unwrap_err().reason, "refused"); + } + #[test] fn wire_node_budget_includes_mixed_list_wrappers() { let tag = Tag::List(vec![Tag::Int(1), Tag::Byte(2)]); let limits = Limits { @@ -673,6 +849,18 @@ mod tests { assert!(read_network(&bytes, limits).is_err()); } #[test] + fn templates_read_past_the_default_node_cap() { + // A big mod template (YUNG's monument start) holds more tags than the + // default cap; vanilla reads templates with an unlimited accounter. + let root = NamedTag { + name: JavaString::default(), + tag: Tag::compound([("blocks", Tag::List(vec![Tag::Byte(1); 1_000_001]))]), + }; + let bytes = write_named(&root, Limits::templates()).unwrap(); + assert!(read_named(&bytes, Limits::default()).is_err()); + assert_eq!(read_named(&bytes, Limits::templates()).unwrap().0, root); + } + #[test] fn depth_boundary_and_every_truncated_prefix() { let mut tag = Tag::Int(1); for _ in 0..512 { diff --git a/crates/horizon-world/src/region.rs b/crates/horizon-world/src/region.rs index 05bdc1a54..1e4ba176c 100644 --- a/crates/horizon-world/src/region.rs +++ b/crates/horizon-world/src/region.rs @@ -1,5 +1,7 @@ -//! Anvil region reading and export to a new region. Existing files are never -//! overwritten. Use a separate directory for offline edits and copied .mcc data. +//! Anvil region reading, export to a new region, and in-place chunk writes +//! as vanilla's `RegionFile` makes them (a file opened with `open_writable`). +//! Exports never overwrite a file: use a separate directory for offline +//! edits and copied .mcc data. use crate::{ compression::{Compression, invalid}, nbt::{self, Limits, NamedTag}, @@ -54,6 +56,9 @@ pub struct RegionFile { position: ChunkPos, offsets: [u32; 1024], timestamps: [u32; 1024], + /// The sectors in use (`RegionBitmap`): the header's two and every + /// chunk's. + used: Vec<bool>, } pub fn position_from_path(path: &Path) -> io::Result<ChunkPos> { @@ -94,6 +99,12 @@ fn position(region: ChunkPos, index: usize) -> ChunkPos { fn external_path(directory: &Path, chunk: ChunkPos) -> PathBuf { directory.join(format!("c.{}.{}.mcc", chunk.x, chunk.z)) } +fn remove_if_exists(path: &Path) -> io::Result<()> { + match fs::remove_file(path) { + Err(e) if e.kind() != io::ErrorKind::NotFound => Err(e), + _ => Ok(()), + } +} impl RegionFile { pub fn position(&self) -> ChunkPos { @@ -101,8 +112,25 @@ impl RegionFile { } pub fn open(path: impl AsRef<Path>) -> io::Result<Self> { let path = path.as_ref().to_owned(); + let file = File::open(&path)?; + Self::from_file(path, file) + } + + /// Open for reading and in-place writes (`write`, `clear`), creating an + /// empty region with `create`. + pub fn open_writable(path: impl AsRef<Path>, create: bool) -> io::Result<Self> { + let path = path.as_ref().to_owned(); + let file = OpenOptions::new() + .read(true) + .write(true) + .create(create) + .truncate(false) + .open(&path)?; + Self::from_file(path, file) + } + + fn from_file(path: PathBuf, mut file: File) -> io::Result<Self> { let position = position_from_path(&path)?; - let mut file = File::open(&path)?; let len = file.metadata()?.len(); // RegionFile's constructor: a short header (an empty or truncated file) // reads as far as it goes, the rest stays zero (no chunks). @@ -134,13 +162,140 @@ impl RegionFile { offsets[i] = 0; } } - Ok(Self { + let mut region = Self { file, path, position, offsets, timestamps, - }) + used: Vec::new(), + }; + region.force(0, 2); + for offset in offsets.into_iter().filter(|&o| o != 0) { + region.force((offset >> 8) as usize, (offset & 255) as usize); + } + Ok(region) + } + + /// `RegionBitmap.force`. + fn force(&mut self, start: usize, count: usize) { + if self.used.len() < start + count { + self.used.resize(start + count, false); + } + self.used[start..start + count].fill(true); + } + /// `RegionBitmap.free`. + fn free(&mut self, start: usize, count: usize) { + let end = (start + count).min(self.used.len()); + if start < end { + self.used[start..end].fill(false); + } + } + /// `RegionBitmap.allocate`: the first run of free sectors long enough + /// (the end of the file is free). + fn allocate(&mut self, count: usize) -> usize { + let mut current = 0; + loop { + let start = (current..self.used.len()) + .find(|&s| !self.used[s]) + .unwrap_or(self.used.len()); + let end = (start..self.used.len()).find(|&s| self.used[s]); + if end.is_none_or(|end| end - start >= count) { + self.force(start, count); + return start; + } + current = end.unwrap_or(start); + } + } + + /// `RegionFile.write`: the chunk goes to the first free run of sectors + /// that holds it (an oversized one, 256 sectors or more, to its external + /// `c.X.Z.mcc` file with a one-sector stub), the header points to it, + /// and only then are its old sectors freed. The other chunks stay where + /// they are. A chunk's last sector is padded with zeros. + pub fn write(&mut self, chunk: ChunkPos, record: &ChunkRecord) -> io::Result<()> { + let i = slot(self.position, chunk)?; + if record.compressed.len() > MAX_COMPRESSED { + return Err(invalid("compressed chunk limit exceeded")); + } + let required = (record.compressed.len() + 5).div_ceil(SECTOR); + let external = required >= 256; + let sectors = if external { 1 } else { required }; + let start = self.allocate(sectors); + if start + sectors > 0x100_0000 { + self.free(start, sectors); + return Err(invalid("region sector address limit exceeded")); + } + let directory = self.path.parent().unwrap_or(Path::new(".")).to_owned(); + let external_file = external_path(&directory, chunk); + let mut data = Vec::with_capacity(sectors * SECTOR); + if external { + let temporary = directory.join(format!(".c.{}.{}.mcc.tmp", chunk.x, chunk.z)); + let written = File::create(&temporary) + .and_then(|mut f| f.write_all(&record.compressed)) + .and_then(|_| fs::rename(&temporary, &external_file)); + if let Err(e) = written { + let _ = fs::remove_file(&temporary); + self.free(start, sectors); + return Err(e); + } + data.extend_from_slice(&1u32.to_be_bytes()); + data.push(record.compression as u8 | 128); + } else { + data.extend_from_slice(&(record.compressed.len() as u32 + 1).to_be_bytes()); + data.push(record.compression as u8); + data.extend_from_slice(&record.compressed); + } + data.resize(sectors * SECTOR, 0); + let written = self + .file + .seek(SeekFrom::Start((start * SECTOR) as u64)) + .and_then(|_| self.file.write_all(&data)); + if let Err(e) = written { + self.free(start, sectors); + return Err(e); + } + let old = self.offsets[i]; + self.offsets[i] = (start as u32) << 8 | sectors as u32; + self.timestamps[i] = record.timestamp; + self.write_header_entry(i)?; + if !external { + remove_if_exists(&external_file)?; + } + if old != 0 { + self.free((old >> 8) as usize, (old & 255) as usize); + } + Ok(()) + } + + /// `RegionFile.clear`: the chunk is gone; `timestamp` marks when. + /// Returns whether the region held it. + pub fn clear(&mut self, chunk: ChunkPos, timestamp: u32) -> io::Result<bool> { + let i = slot(self.position, chunk)?; + let old = self.offsets[i]; + if old == 0 { + return Ok(false); + } + self.offsets[i] = 0; + self.timestamps[i] = timestamp; + self.write_header_entry(i)?; + let directory = self.path.parent().unwrap_or(Path::new(".")); + remove_if_exists(&external_path(directory, chunk))?; + self.free((old >> 8) as usize, (old & 255) as usize); + Ok(true) + } + + /// Flush the writes to the disk. + pub fn sync(&self) -> io::Result<()> { + self.file.sync_data() + } + + /// The header's offset and timestamp of slot `i`. + fn write_header_entry(&mut self, i: usize) -> io::Result<()> { + self.file.seek(SeekFrom::Start((i * 4) as u64))?; + self.file.write_all(&self.offsets[i].to_be_bytes())?; + self.file.seek(SeekFrom::Start((SECTOR + i * 4) as u64))?; + self.file.write_all(&self.timestamps[i].to_be_bytes()) } pub fn chunks(&self) -> impl Iterator<Item = (ChunkPos, u32)> + '_ { diff --git a/crates/horizon-world/src/registry.rs b/crates/horizon-world/src/registry.rs index 30367e251..9ce9a6814 100644 --- a/crates/horizon-world/src/registry.rs +++ b/crates/horizon-world/src/registry.rs @@ -1,11 +1,13 @@ //! Runtime block-state IDs imported from a version-pinned registry data export. //! This supplies identities and default properties, not block behavior. +mod facts; mod json; use crate::{DATA_VERSION, chunk::BlockState, compression::invalid, nbt::Tag}; pub use json::BlockStateJsonCodec; pub(crate) use json::java_integer; use std::{ - collections::{BTreeMap, BTreeSet}, + collections::{BTreeMap, BTreeSet, HashMap}, + hash::{DefaultHasher, Hash, Hasher}, io, }; @@ -14,9 +16,25 @@ pub struct BlockStateId(pub u32); pub struct BlockRegistry { states: Vec<BlockState>, - ids: BTreeMap<BlockState, BlockStateId>, + /// Ids by a hash of their state, checked against `states`: 12 bytes per + /// state instead of a second copy of every name and property map (about + /// 125 MB for a registry of 190,000 states). + ids: HashMap<u64, u32>, + /// States whose hash an earlier state already took (none in vanilla). + collided: BTreeMap<BlockState, BlockStateId>, defaults: BTreeMap<String, BlockStateId>, } +/// The hash [`BlockRegistry`] indexes states by (fixed keys: the same in +/// every process). +fn state_key(state: &BlockState) -> u64 { + let mut hasher = DefaultHasher::new(); + state.name.hash(&mut hasher); + for (key, value) in &state.properties { + key.hash(&mut hasher); + value.hash(&mut hasher); + } + hasher.finish() +} impl BlockRegistry { pub fn from_nbt(tag: &Tag) -> io::Result<Self> { if tag.get("DataVersion") != Some(&Tag::Int(DATA_VERSION)) { @@ -28,39 +46,79 @@ impl BlockRegistry { let Some(Tag::Compound(defaults)) = tag.get("defaults") else { return Err(invalid("missing registry defaults")); }; - if entries.is_empty() || entries.len() > i32::MAX as usize { + let states = entries + .iter() + .map(BlockState::from_nbt) + .collect::<io::Result<_>>()?; + let defaults = defaults + .iter() + .map(|(name, id)| { + let name = name.to_string().map_err(|e| invalid(e.to_string()))?; + let Tag::Int(id) = id else { + return Err(invalid("default state ID is not an int")); + }; + Ok((name, i64::from(*id))) + }) + .collect::<io::Result<_>>()?; + Self::from_states(states, defaults) + } + + /// The registry of a resources `facts.json` (`{"states": [[..., ..., ..., + /// ..., state], ...], "defaults": {name: id}}`), read as it streams: no + /// JSON or NBT tree of the whole file is built. For a pack of 190,000 + /// states the two trees were several hundred megabytes at startup. + pub fn from_facts_json(reader: impl io::Read) -> io::Result<Self> { + Ok(Self::from_facts_json_with_flags(reader)?.0) + } + + /// [`Self::from_facts_json`], with each state's `(air, fluid)` columns. + pub fn from_facts_json_with_flags( + reader: impl io::Read, + ) -> io::Result<(Self, Vec<(bool, bool)>)> { + let facts: facts::Facts = serde_json::from_reader(io::BufReader::new(reader)) + .map_err(|e| invalid(e.to_string()))?; + let flags = facts.states.iter().map(|r| (r.air, r.fluid)).collect(); + let states = facts.states.into_iter().map(|r| r.state).collect(); + Ok((Self::from_states(states, facts.defaults)?, flags)) + } + + fn from_states(states: Vec<BlockState>, defaults: BTreeMap<String, i64>) -> io::Result<Self> { + if states.is_empty() || states.len() > i32::MAX as usize { return Err(invalid("invalid registry size")); } let mut result = Self { - states: Vec::with_capacity(entries.len()), - ids: BTreeMap::new(), + states: Vec::with_capacity(states.len()), + ids: HashMap::with_capacity(states.len()), + collided: BTreeMap::new(), defaults: BTreeMap::new(), }; - for (i, entry) in entries.iter().enumerate() { - let state = BlockState::from_nbt(entry)?; - if result - .ids - .insert(state.clone(), BlockStateId(i as u32)) - .is_some() - { + for (i, state) in states.into_iter().enumerate() { + if result.id(&state).is_some() { return Err(invalid("duplicate block registry state")); } + match result.ids.entry(state_key(&state)) { + std::collections::hash_map::Entry::Vacant(slot) => { + slot.insert(i as u32); + } + std::collections::hash_map::Entry::Occupied(_) => { + result + .collided + .insert(state.clone(), BlockStateId(i as u32)); + } + } result.states.push(state); } for (name, id) in defaults { - let name = name.to_string().map_err(|e| invalid(e.to_string()))?; - let Tag::Int(id) = id else { - return Err(invalid("default state ID is not an int")); - }; - if *id < 0 + if id < 0 + || id > i64::from(i32::MAX) || result .states - .get(*id as usize) + .get(id as usize) .is_none_or(|s| s.name != name) { return Err(invalid("invalid default block state ID")); } - result.defaults.insert(name, BlockStateId(*id as u32)); + result.defaults.insert(name, BlockStateId(id as u32)); } let names: BTreeSet<_> = result.states.iter().map(|s| &s.name).collect(); if names.len() != result.defaults.len() @@ -82,7 +140,10 @@ impl BlockRegistry { self.states.get(id.0 as usize) } pub fn id(&self, state: &BlockState) -> Option<BlockStateId> { - self.ids.get(state).copied() + match self.ids.get(&state_key(state)) { + Some(&id) if self.states[id as usize] == *state => Some(BlockStateId(id)), + _ => self.collided.get(state).copied(), + } } pub fn default_state(&self, name: &str) -> Option<BlockStateId> { self.defaults.get(name).copied() @@ -105,3 +166,120 @@ impl BlockRegistry { .ok_or_else(|| invalid("invalid block property value")) } } + +/// A per-state table (`occlusion.nbt`, `cached-collision.nbt`, ...) whose +/// `states` list, the identity of every state it describes in id order, has +/// been checked against `registry`. +#[derive(Clone, Copy)] +pub struct BlockTable<'t, 'r> { + tag: &'t Tag, + registry: &'r BlockRegistry, + /// How many levels, from this one down, [`BlockTableFile::read`] checked + /// while reading: their `states` lists are kept empty in the tree. + read_checked: u8, +} +impl<'t, 'r> BlockTable<'t, 'r> { + /// Checks that `tag`'s `states` names the registry's states in id order. + pub fn check(tag: &'t Tag, registry: &'r BlockRegistry, what: &str) -> io::Result<Self> { + Self { + tag, + registry, + read_checked: 0, + } + .checked(what) + } + fn checked(self, what: &str) -> io::Result<Self> { + let Some(Tag::List(states)) = self.tag.get("states") else { + return Err(invalid(format!("missing {what} state identities"))); + }; + if self.read_checked == 0 { + if states.len() != self.registry.len() { + return Err(invalid(format!("{what} registry size mismatch"))); + } + for (id, state) in states.iter().enumerate() { + if self.registry.state(BlockStateId(id as u32)) + != Some(&BlockState::from_nbt(state)?) + { + return Err(invalid(format!("{what} state identities differ"))); + } + } + } + Ok(self) + } + /// The table nested under `key` (`block-properties.nbt`'s `occlusion`), + /// with its own `states`, checked the same way. + pub fn nested(&self, key: &str, what: &str) -> io::Result<Self> { + Self { + tag: self + .tag + .get(key) + .ok_or_else(|| invalid(format!("missing {what}")))?, + registry: self.registry, + read_checked: self.read_checked.saturating_sub(1), + } + .checked(what) + } + pub fn tag(&self) -> &'t Tag { + self.tag + } + pub fn get(&self, key: &str) -> Option<&'t Tag> { + self.tag.get(key) + } + pub fn registry(&self) -> &'r BlockRegistry { + self.registry + } +} + +/// A per-state table read from its file with its state identities checked +/// while reading, so they never exist as a tree: for 190,000 states (a big +/// modpack) that tree took 300-600 MB per table. +pub struct BlockTableFile<'r> { + tag: Tag, + registry: &'r BlockRegistry, +} +impl<'r> BlockTableFile<'r> { + /// [`Self::read`] of the file at `path`. + pub fn open(path: &std::path::Path, registry: &'r BlockRegistry) -> io::Result<Self> { + let bytes = std::fs::read(path)?; + Self::read(&bytes, registry).map_err(|e| invalid(format!("{}: {e}", path.display()))) + } + /// Reads `bytes`, checking every `states` list of the root compound and + /// of the compounds directly under it against `registry`. + pub fn read(bytes: &[u8], registry: &'r BlockRegistry) -> io::Result<Self> { + let mut visit = |states: &mut crate::nbt::ListElements<'_, '_, '_>| { + if states.len() != registry.len() { + return Err(states.error("table registry size mismatch")); + } + let mut id = 0u32; + while let Some(tag) = states.next_element()? { + let state = + BlockState::from_nbt(&tag).map_err(|_| states.error("invalid table state"))?; + if registry.state(BlockStateId(id)) != Some(&state) { + return Err(states.error("table state identities differ")); + } + id += 1; + } + Ok(()) + }; + let (named, _) = crate::nbt::read_named_streaming( + bytes, + crate::nbt::Limits::tables(), + "states", + &mut visit, + ) + .map_err(|e| invalid(e.to_string()))?; + Ok(Self { + tag: named.tag, + registry, + }) + } + /// The table, checked (`what` names it in errors). + pub fn table(&self, what: &str) -> io::Result<BlockTable<'_, 'r>> { + BlockTable { + tag: &self.tag, + registry: self.registry, + read_checked: 2, + } + .checked(what) + } +} diff --git a/crates/horizon-world/src/registry/facts.rs b/crates/horizon-world/src/registry/facts.rs new file mode 100644 index 000000000..3371a8283 --- /dev/null +++ b/crates/horizon-world/src/registry/facts.rs @@ -0,0 +1,164 @@ +//! The part of `facts.json` the block registry reads, deserialized as it +//! streams: each row's state (its fifth column) and the defaults. +use crate::chunk::BlockState; +use serde::{ + Deserialize, Deserializer, + de::{self, IgnoredAny, SeqAccess, Visitor}, +}; +use std::{collections::BTreeMap, fmt}; + +#[derive(Deserialize)] +pub(super) struct Facts { + pub states: Vec<Row>, + pub defaults: BTreeMap<String, i64>, +} + +/// One `states` row: `[name, air, fluid, description, state, ...]`, keeping +/// the two flags and the state. +pub(super) struct Row { + pub air: bool, + pub fluid: bool, + pub state: BlockState, +} + +impl<'de> Deserialize<'de> for Row { + fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { + struct RowVisitor; + impl<'de> Visitor<'de> for RowVisitor { + type Value = Row; + fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("a registry state row") + } + fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Row, A::Error> { + let length = |i| de::Error::invalid_length(i, &self); + seq.next_element::<IgnoredAny>()?.ok_or_else(|| length(0))?; + let air = seq.next_element::<bool>()?.ok_or_else(|| length(1))?; + let fluid = seq.next_element::<bool>()?.ok_or_else(|| length(2))?; + seq.next_element::<IgnoredAny>()?.ok_or_else(|| length(3))?; + let state = seq.next_element::<State>()?.ok_or_else(|| length(4))?; + while seq.next_element::<IgnoredAny>()?.is_some() {} + Ok(Row { + air, + fluid, + state: state.0, + }) + } + } + deserializer.deserialize_seq(RowVisitor) + } +} + +/// A state as BlockState::from_nbt reads it: a name, or `{"id", "properties"}`. +struct State(BlockState); + +impl<'de> Deserialize<'de> for State { + fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { + #[derive(Deserialize)] + #[serde(untagged)] + enum Shape { + Name(String), + Full { + id: String, + #[serde(default)] + properties: BTreeMap<String, String>, + }, + } + Ok(State(match Shape::deserialize(deserializer)? { + Shape::Name(name) => BlockState { + name, + properties: BTreeMap::new(), + }, + Shape::Full { id, properties } => BlockState { + name: id, + properties, + }, + })) + } +} + +#[cfg(test)] +mod tests { + use crate::{ + DATA_VERSION, + chunk::BlockState, + nbt::Tag, + registry::{BlockRegistry, BlockStateId}, + }; + use serde_json::Value; + use std::path::Path; + + #[test] + fn reads_states_and_defaults() { + let json = br#"{"defaults": {"minecraft:air": 0, "minecraft:oak_slab": 2}, + "states": [ + ["minecraft:air", true, false, "Block{minecraft:air}", "minecraft:air"], + ["minecraft:oak_slab", false, false, "x", {"id": "minecraft:oak_slab", "properties": {"type": "top"}}, "extra"], + ["minecraft:oak_slab", false, true, "x", {"id": "minecraft:oak_slab", "properties": {"type": "bottom"}}] + ], "other": [1, 2]}"#; + let (registry, flags) = BlockRegistry::from_facts_json_with_flags(&json[..]).unwrap(); + assert_eq!(registry.len(), 3); + assert_eq!(flags, [(true, false), (false, false), (false, true)]); + assert_eq!( + registry.default_state("minecraft:oak_slab"), + Some(BlockStateId(2)) + ); + let top = BlockState { + name: "minecraft:oak_slab".into(), + properties: [("type".into(), "top".into())].into(), + }; + assert_eq!(registry.id(&top), Some(BlockStateId(1))); + // A default naming another block, a short row, a missing default. + for bad in [ + &br#"{"defaults": {"minecraft:air": 1}, "states": [["minecraft:air", true, false, "", "minecraft:air"], ["minecraft:stone", false, false, "", "minecraft:stone"]]}"#[..], + br#"{"defaults": {"minecraft:air": 0}, "states": [["minecraft:air", true, false, ""]]}"#, + br#"{"defaults": {}, "states": [["minecraft:air", true, false, "", "minecraft:air"]]}"#, + ] { + assert!(BlockRegistry::from_facts_json(bad).is_err()); + } + } + + /// The streamed registry is the one the JSON and NBT trees built. + #[test] + fn matches_the_tree_built_registry() { + let path = Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../.local/horizon-client/26.3/facts.json"); + let Ok(bytes) = std::fs::read(&path) else { + eprintln!("skipped: no prepared facts.json"); + return; + }; + fn tag(v: &Value) -> Tag { + match v { + Value::String(s) => Tag::String(s.as_str().into()), + Value::Number(n) => Tag::Int(n.as_i64().unwrap() as i32), + Value::Object(o) => { + Tag::Compound(o.iter().map(|(k, v)| (k.as_str().into(), tag(v))).collect()) + } + _ => unreachable!(), + } + } + let facts: Value = serde_json::from_slice(&bytes).unwrap(); + let rows = facts["states"].as_array().unwrap(); + let tree = BlockRegistry::from_nbt(&Tag::compound([ + ("DataVersion", Tag::Int(DATA_VERSION)), + ( + "states", + Tag::List(rows.iter().map(|r| tag(&r[4])).collect()), + ), + ("defaults", tag(&facts["defaults"])), + ])) + .unwrap(); + let (streamed, flags) = BlockRegistry::from_facts_json_with_flags(&bytes[..]).unwrap(); + assert_eq!(streamed.len(), tree.len()); + for (i, row) in rows.iter().enumerate() { + let id = BlockStateId(i as u32); + assert_eq!(streamed.state(id), tree.state(id)); + assert_eq!( + flags[i], + (row[1].as_bool().unwrap(), row[2].as_bool().unwrap()) + ); + } + for name in facts["defaults"].as_object().unwrap().keys() { + assert_eq!(streamed.default_state(name), tree.default_state(name)); + } + } +} diff --git a/crates/horizon-world/src/worldgen/block_feature.rs b/crates/horizon-world/src/worldgen/block_feature.rs index a901a583d..00f0c64c1 100644 --- a/crates/horizon-world/src/worldgen/block_feature.rs +++ b/crates/horizon-world/src/worldgen/block_feature.rs @@ -23,6 +23,7 @@ pub struct FeatureBlockBindings<'r> { )>, physical_properties: Option<[BoundBlockProperty; 4]>, pub(crate) occlusion_faces: Option<crate::block_properties::BoundOcclusionFaces>, + pub(crate) can_occlude: Option<BoundBlockProperty>, biomes: Option<Arc<super::biome_predicate::BiomePredicateBindings>>, pub(crate) fluids: super::fluid_predicate::FluidPredicateBindings, pub(crate) codec: BlockStateJsonCodec<'r>, @@ -88,6 +89,7 @@ impl<'r> FeatureBlockBindings<'r> { properties.bind(BlockProperty::SolidRender), ]); self.occlusion_faces = Some(properties.bind_occlusion_faces()); + self.can_occlude = Some(properties.bind_can_occlude()); Ok(self) } pub fn with_biomes( @@ -156,6 +158,7 @@ impl<'r> FeatureBlockBindings<'r> { biomes: None, physical_properties: None, occlusion_faces: None, + can_occlude: None, fluids: super::fluid_predicate::FluidPredicateBindings::new(registry)?, blocks: blocks .into_iter() diff --git a/crates/horizon-world/src/worldgen/decoration_order.rs b/crates/horizon-world/src/worldgen/decoration_order.rs index 52649ceed..6b0fb2252 100644 --- a/crates/horizon-world/src/worldgen/decoration_order.rs +++ b/crates/horizon-world/src/worldgen/decoration_order.rs @@ -121,7 +121,7 @@ fn ticket_hash(x: i32, z: i32) -> u64 { /// The chunks decorated around a player at `center`, in vanilla's order. pub fn vanilla_order(center: (i32, i32), radius: i32) -> Vec<(i32, i32)> { use std::cmp::Reverse; - use std::collections::{BinaryHeap, HashSet, VecDeque}; + use std::collections::{BinaryHeap, VecDeque}; let mut tickets: Vec<(i32, i32)> = (-radius..=radius) .flat_map(|dx| (-radius..=radius).map(move |dz| (center.0 + dx, center.1 + dz))) .collect(); @@ -133,35 +133,45 @@ pub fn vanilla_order(center: (i32, i32), radius: i32) -> Vec<(i32, i32)> { ) }); let mut tickets: VecDeque<_> = tickets.into(); + // Every chunk this touches lies within `half` of the centre: tickets + // reach `LIGHT_LEVEL - TICKET_LEVEL` further, and FULL tasks decorate + // one more ring. Dense grids over that square replace hash maps. + let reach = LIGHT_LEVEL - TICKET_LEVEL; + let half = radius.max(0) + reach + 1; + let side = (2 * half + 1) as usize; + let cell = |(x, z): (i32, i32)| { + ((x - center.0 + half) as usize) * side + (z - center.1 + half) as usize + }; let mut active: Vec<(i32, i32)> = Vec::new(); - // Queued task per chunk (level, sequence); stale heap entries are skipped. - let mut queued: HashMap<(i32, i32), (i32, u64)> = HashMap::new(); + // Queued task per chunk (level, sequence), `NONE` when there is none; + // stale heap entries are skipped. + const NONE: (i32, u64) = (i32::MAX, 0); + let mut queued = vec![NONE; side * side]; let mut heap = BinaryHeap::new(); let mut sequence = 0u64; - let mut full = HashSet::new(); - let mut seen = HashSet::new(); + let mut full = vec![false; side * side]; + let mut seen = vec![false; side * side]; let mut order = Vec::new(); // Each ticket lowers the levels around it to 31 + its Chebyshev distance. - let mut levels: HashMap<(i32, i32), i32> = HashMap::new(); + let mut levels = vec![i32::MAX; side * side]; let mut add = |ticket: (i32, i32), active: &mut Vec<(i32, i32)>, - queued: &mut HashMap<(i32, i32), (i32, u64)>, + queued: &mut [(i32, u64)], heap: &mut BinaryHeap<Reverse<(i32, u64, (i32, i32))>>, - full: &HashSet<(i32, i32)>| { + full: &[bool]| { active.push(ticket); - let reach = LIGHT_LEVEL - TICKET_LEVEL; for x in ticket.0 - reach..=ticket.0 + reach { for z in ticket.1 - reach..=ticket.1 + reach { let own = TICKET_LEVEL + (x - ticket.0).abs().max((z - ticket.1).abs()); - let l = levels.entry((x, z)).or_insert(i32::MAX); - *l = (*l).min(own); - let l = *l; - if full.contains(&(x, z)) { + let i = cell((x, z)); + levels[i] = levels[i].min(own); + let l = levels[i]; + if full[i] { continue; } - if queued.get(&(x, z)).is_none_or(|&(old, _)| l < old) { + if l < queued[i].0 { sequence += 1; - queued.insert((x, z), (l, sequence)); + queued[i] = (l, sequence); heap.push(Reverse((l, sequence, (x, z)))); } } @@ -173,24 +183,25 @@ pub fn vanilla_order(center: (i32, i32), radius: i32) -> Vec<(i32, i32)> { } } while let Some(Reverse((l, s, chunk))) = heap.pop() { - if queued.get(&chunk) != Some(&(l, s)) { + let i = cell(chunk); + if queued[i] != (l, s) { continue; } - queued.remove(&chunk); + queued[i] = NONE; let r = i32::from(l <= FULL_LEVEL); for x in chunk.0 - r..=chunk.0 + r { for z in chunk.1 - r..=chunk.1 + r { - if seen.insert((x, z)) { + let seen = &mut seen[cell((x, z))]; + if !*seen { + *seen = true; order.push((x, z)); } } } if l <= FULL_LEVEL { - full.insert(chunk); + full[i] = true; } - active.retain(|t| { - !(-2..=2).all(|dx| (-2..=2).all(|dz| full.contains(&(t.0 + dx, t.1 + dz)))) - }); + active.retain(|t| !(-2..=2).all(|dx| (-2..=2).all(|dz| full[cell((t.0 + dx, t.1 + dz))]))); while active.len() < TICKETS_IN_FLIGHT { let Some(t) = tickets.pop_front() else { break }; add(t, &mut active, &mut queued, &mut heap, &full); @@ -203,6 +214,106 @@ pub fn vanilla_order(center: (i32, i32), radius: i32) -> Vec<(i32, i32)> { mod tests { use super::*; + /// The hash map version this module had before its dense grids. + fn hashed_order(center: (i32, i32), radius: i32) -> Vec<(i32, i32)> { + use std::cmp::Reverse; + use std::collections::{BinaryHeap, HashSet, VecDeque}; + let mut tickets: Vec<(i32, i32)> = (-radius..=radius) + .flat_map(|dx| (-radius..=radius).map(move |dz| (center.0 + dx, center.1 + dz))) + .collect(); + // A fastutil open hash set iterates its slots from the last to the first. + tickets.sort_by_key(|&(x, z)| { + ( + (x - center.0).abs().max((z - center.1).abs()), + Reverse(ticket_hash(x, z) & (TICKET_SET_CAPACITY - 1)), + ) + }); + let mut tickets: VecDeque<_> = tickets.into(); + let mut active: Vec<(i32, i32)> = Vec::new(); + // Queued task per chunk (level, sequence); stale heap entries are skipped. + let mut queued: HashMap<(i32, i32), (i32, u64)> = HashMap::new(); + let mut heap = BinaryHeap::new(); + let mut sequence = 0u64; + let mut full = HashSet::new(); + let mut seen = HashSet::new(); + let mut order = Vec::new(); + // Each ticket lowers the levels around it to 31 + its Chebyshev distance. + let mut levels: HashMap<(i32, i32), i32> = HashMap::new(); + let mut add = |ticket: (i32, i32), + active: &mut Vec<(i32, i32)>, + queued: &mut HashMap<(i32, i32), (i32, u64)>, + heap: &mut BinaryHeap<Reverse<(i32, u64, (i32, i32))>>, + full: &HashSet<(i32, i32)>| { + active.push(ticket); + let reach = LIGHT_LEVEL - TICKET_LEVEL; + for x in ticket.0 - reach..=ticket.0 + reach { + for z in ticket.1 - reach..=ticket.1 + reach { + let own = TICKET_LEVEL + (x - ticket.0).abs().max((z - ticket.1).abs()); + let l = levels.entry((x, z)).or_insert(i32::MAX); + *l = (*l).min(own); + let l = *l; + if full.contains(&(x, z)) { + continue; + } + if queued.get(&(x, z)).is_none_or(|&(old, _)| l < old) { + sequence += 1; + queued.insert((x, z), (l, sequence)); + heap.push(Reverse((l, sequence, (x, z)))); + } + } + } + }; + for _ in 0..TICKETS_IN_FLIGHT { + if let Some(t) = tickets.pop_front() { + add(t, &mut active, &mut queued, &mut heap, &full); + } + } + while let Some(Reverse((l, s, chunk))) = heap.pop() { + if queued.get(&chunk) != Some(&(l, s)) { + continue; + } + queued.remove(&chunk); + let r = i32::from(l <= FULL_LEVEL); + for x in chunk.0 - r..=chunk.0 + r { + for z in chunk.1 - r..=chunk.1 + r { + if seen.insert((x, z)) { + order.push((x, z)); + } + } + } + if l <= FULL_LEVEL { + full.insert(chunk); + } + active.retain(|t| { + !(-2..=2).all(|dx| (-2..=2).all(|dz| full.contains(&(t.0 + dx, t.1 + dz)))) + }); + while active.len() < TICKETS_IN_FLIGHT { + let Some(t) = tickets.pop_front() else { break }; + add(t, &mut active, &mut queued, &mut heap, &full); + } + } + order + } + + #[test] + fn dense_grids_keep_the_hashed_order() { + for (center, radius) in [ + ((0, 0), -1), + ((0, 0), 0), + ((0, 0), 1), + ((12, -15), 8), + ((-30_000, 29_999), 5), + ((7, 3), 12), + ((-61, 1234), 32), + ] { + assert_eq!( + vanilla_order(center, radius), + hashed_order(center, radius), + "{center:?} radius {radius}" + ); + } + } + #[test] fn first_wave_matches_vanilla() { // Seed 1891623539983467248, player teleported into chunk 12,-15 with diff --git a/crates/horizon-world/src/worldgen/feature_catalog.rs b/crates/horizon-world/src/worldgen/feature_catalog.rs index 0b8d56cc4..4db251114 100644 --- a/crates/horizon-world/src/worldgen/feature_catalog.rs +++ b/crates/horizon-world/src/worldgen/feature_catalog.rs @@ -192,7 +192,7 @@ impl FeatureCatalog { if names.is_empty() { return Ok(serde_json::to_string(&format!("#{tag}"))?); } - if names.len() > 8192 { + if names.len() > super::state_provider::MAX_TAG_BLOCKS { return Err(DecodeError::Field("placed-feature tag size")); } names diff --git a/crates/horizon-world/src/worldgen/fluid_predicate.rs b/crates/horizon-world/src/worldgen/fluid_predicate.rs index d8bcb10ab..1394445c9 100644 --- a/crates/horizon-world/src/worldgen/fluid_predicate.rs +++ b/crates/horizon-world/src/worldgen/fluid_predicate.rs @@ -45,6 +45,9 @@ impl FluidPredicateBindings { self.tags = resolved; Ok(self) } + pub(crate) fn table(&self) -> &Arc<FluidStateTable> { + &self.table + } pub fn state(&self, block: BlockStateId) -> Option<crate::fluid::FluidState> { self.table.get(block) } diff --git a/crates/horizon-world/src/worldgen/generator.rs b/crates/horizon-world/src/worldgen/generator.rs index 12dfc10d9..0d1f409e6 100644 --- a/crates/horizon-world/src/worldgen/generator.rs +++ b/crates/horizon-world/src/worldgen/generator.rs @@ -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 } @@ -635,6 +649,7 @@ struct Pipeline<R: RandomSource> { picker: GlobalFluidPicker, zoom: BiomeZoom, biome_cache: BiomeCache, + biome_work: ChunkWork, carver_cache: Mutex<HashMap<(i32, i32), BiomeId>>, pub(super) decoration: Option<super::generator_decoration::Decoration>, pub(super) structures: Option<super::generator_structures::StructureState>, @@ -643,6 +658,30 @@ struct Pipeline<R: RandomSource> { spawn_position: std::sync::OnceLock<[i32; 2]>, } const CACHE_LIMIT: usize = 16384; +/// Coordinates only equal missing neighbour keys. Weak entries retain no +/// computed values across eviction or explicit invalidation. Only one work +/// lock may be held at a time; finish shared prerequisites before acquiring it. +pub(super) struct ChunkWork(Mutex<HashMap<(i32, i32), std::sync::Weak<Mutex<()>>>>); +impl ChunkWork { + pub(super) fn new() -> Self { + Self(Mutex::new(HashMap::new())) + } + pub(super) fn at(&self, key: (i32, i32)) -> Arc<Mutex<()>> { + let mut work = self.0.lock().unwrap(); + if let Some(lock) = work.get(&key).and_then(std::sync::Weak::upgrade) { + return lock; + } + if work.len() >= 4096 { + // Keep every active builder/waiter on its original lock, including + // when the result cache was cleared while it was in flight. + work.retain(|_, lock| lock.strong_count() > 0); + } + let lock = Arc::new(Mutex::new(())); + work.insert(key, Arc::downgrade(&lock)); + lock + } +} + type BiomeCache = Mutex<HashMap<(i32, i32), Arc<BiomeChunk<BiomeId>>>>; fn state(registry: &BlockRegistry, raw: &Value) -> Result<BlockStateId, GeneratorError> { @@ -834,6 +873,7 @@ where extensions, picker, biome_cache: Mutex::new(HashMap::new()), + biome_work: ChunkWork::new(), carver_cache: Mutex::new(HashMap::new()), decoration: None, structures: None, @@ -867,6 +907,11 @@ where worker: &mut WorkerState, pos: ChunkPos, ) -> Result<Arc<BiomeChunk<BiomeId>>, GeneratorError> { + if let Some(chunk) = self.biome_cache.lock().unwrap().get(&(pos.x, pos.z)) { + return Ok(chunk.clone()); + } + let work = self.biome_work.at((pos.x, pos.z)); + let _work = work.lock().unwrap(); if let Some(chunk) = self.biome_cache.lock().unwrap().get(&(pos.x, pos.z)) { return Ok(chunk.clone()); } @@ -906,7 +951,15 @@ where Ok(chunk) } fn carver_biome(&self, worker: &mut WorkerState, origin: ChunkPos) -> Result<BiomeId, String> { - if let Some(id) = self.carver_cache.lock().unwrap().get(&(origin.x, origin.z)) { + let key = (origin.x, origin.z); + if let Some(id) = worker.carver_biomes.get(&key) { + return Ok(*id); + } + if worker.carver_biomes.len() >= 4096 { + worker.carver_biomes.clear(); + } + if let Some(id) = self.carver_cache.lock().unwrap().get(&key) { + worker.carver_biomes.insert(key, *id); return Ok(*id); } let mut resolver = self.source.resolver( @@ -926,13 +979,27 @@ where if cache.len() >= CACHE_LIMIT * 4 { cache.clear(); } - cache.insert((origin.x, origin.z), id); + cache.insert(key, id); + worker.carver_biomes.insert(key, id); Ok(id) } fn generate( &self, worker: &mut WorkerState, pos: ChunkPos, + ) -> Result<ProtoChunk, GeneratorError> { + let mut context = std::mem::take(&mut worker.terrain_context); + let result = self.generate_with_context(worker, pos, &mut context); + // Release chunk-specific bindings and caches even after a sampling error. + context.reset(); + worker.terrain_context = context; + result + } + fn generate_with_context( + &self, + worker: &mut WorkerState, + pos: ChunkPos, + context: &mut SamplerContext, ) -> Result<ProtoChunk, GeneratorError> { let data = &self.data; let (min, height, sea) = ( @@ -985,13 +1052,12 @@ where ), None => Vec::new(), }; - let mut context = SamplerContext::cached(); if let Some(beard) = self .structures .as_ref() .and_then(|s| s.beardifier(&references, pos)) { - context = context.with_world_fields( + *context = std::mem::take(context).with_world_fields( horizon_core::density::WorldFields::default().with_field( horizon_core::density::WorldField::Beardifier, Arc::new(beard), @@ -1016,7 +1082,7 @@ where .fork_positional(), RegistryAquiferBlockStates::new(data.registry).map_err(stage("aquifer"))?, volume, - &mut context, + context, ) .map_err(stage("aquifer"))?, ), @@ -1030,7 +1096,7 @@ where &mut chunk, volume, self.router.get(RouterField::FinalDensity).as_ref(), - &mut context, + context, aquifer.as_mut(), self.settings.default_block, ) @@ -1051,7 +1117,7 @@ where possible: Some(possible), }, preliminary_surface: self.preliminary.clone(), - density_context: &mut context, + density_context: context, write_min_y: min, write_max_y: min + height - 1, }, @@ -1086,7 +1152,7 @@ where possible: None, }, preliminary_surface: self.preliminary.clone(), - density_context: &mut context, + density_context: context, }, aquifer.as_mut(), &data.carving_blocks, @@ -1270,15 +1336,23 @@ where self.pipeline.generation() } fn noise_biome(&mut self, quart: [i32; 3]) -> BiomeId { + self.noise_biomes(&[quart])[0] + } + fn noise_biomes(&mut self, quarts: &[[i32; 3]]) -> Vec<BiomeId> { let mut resolver = self.pipeline.source.resolver( &self.pipeline.climate, self.pipeline.router.get(RouterField::Erosion).as_ref(), ); - resolver - .sample(&mut self.worker.carver_context, quart) - .ok() - .and_then(|name| self.pipeline.data.biome_id(name)) - .unwrap_or(BiomeId(0)) + quarts + .iter() + .map(|&quart| { + resolver + .sample(&mut self.worker.carver_context, quart) + .ok() + .and_then(|name| self.pipeline.data.biome_id(name)) + .unwrap_or(BiomeId(0)) + }) + .collect() } fn set_valid_biomes(&mut self, biomes: &BTreeSet<BiomeId>) { self.valid = biomes.clone(); @@ -1391,8 +1465,12 @@ pub struct StageTimings { /// thread-local caches and never change results. pub(super) struct WorkerState { pub(super) temperatures: Vec<BiomeTemperature>, + terrain_context: SamplerContext, biome_context: SamplerContext, carver_context: SamplerContext, + /// Seed/dimension-bound lookups avoid the shared mutex for overlapping + /// carver neighborhoods. Bounded independently of the shared cache. + carver_biomes: HashMap<(i32, i32), BiomeId>, timings: StageTimings, heights: HashMap<(i32, i32, u8), i32>, /// Decorate with structure pieces only (for structure parity checks). @@ -1412,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, @@ -1594,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, @@ -1764,6 +2157,18 @@ impl OverworldGenerator { ) -> Result<Self, GeneratorError> { Self::build(data, seed, preset, dimension, true) } + /// A dimension of a world preset, with or without structures (the + /// world's `generate_structures`: without, no starts, terrain adaptation, + /// structure pieces or structure locating). + pub fn for_world( + data: Arc<GeneratorData>, + seed: i64, + preset: &str, + dimension: &str, + structures: bool, + ) -> Result<Self, GeneratorError> { + Self::build(data, seed, preset, dimension, structures) + } fn build( data: Arc<GeneratorData>, seed: i64, @@ -1854,8 +2259,10 @@ impl OverworldGenerator { .iter() .map(|s| BiomeTemperature::new(*s)) .collect(), + terrain_context: SamplerContext::cached(), biome_context: SamplerContext::cached(), carver_context: SamplerContext::default(), + carver_biomes: HashMap::new(), timings: StageTimings::default(), structures_only: false, heights: HashMap::new(), @@ -1864,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>, @@ -1877,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, @@ -1886,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) } @@ -1935,11 +2381,10 @@ impl GeneratorWorker { mod tests { use super::*; - /// Generators of one seed and dimension share their ring positions; - /// another seed or generator gets its own, and old entries go. #[test] - fn ring_positions_are_shared_by_seed_and_dimension() { - let root = std::env::var_os("HORIZON_RESOURCES") + 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") @@ -1952,6 +2397,191 @@ mod tests { 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") + .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 = Box::leak(Box::new( + crate::level::data::load_registry(&root).expect("registry"), + )); + let data = GeneratorData::load(&root.join("worldgen"), registry).unwrap(); + let generator = OverworldGenerator::new(data.clone(), 12345).unwrap(); + let mut first = generator.worker(); + let mut second = generator.worker(); + let pos = ChunkPos::new(0, 0); + let before = first.generate_proto(pos).unwrap(); + assert!(!first.state.carver_biomes.is_empty()); + assert!(second.state.carver_biomes.is_empty()); + second.generate_proto(pos).unwrap(); + assert_eq!(first.state.carver_biomes, second.state.carver_biomes); + let expected = first.state.carver_biomes.clone(); + first.state.carver_biomes.clear(); + // Fill unrelated origins to force eviction on the next local miss. + for x in 0..4096 { + first.state.carver_biomes.insert((x, 100_000), BiomeId(0)); + } + let after = first.generate_proto(pos).unwrap(); + assert_eq!(first.state.carver_biomes, expected); + for y in -64..320 { + for z in 0..16 { + for x in 0..16 { + assert_eq!(before.block(x, y, z), after.block(x, y, z)); + assert_eq!(before.biome_id_at(x, y, z), after.biome_id_at(x, y, z)); + } + } + } + assert!( + OverworldGenerator::new(data, -1) + .unwrap() + .worker() + .state + .carver_biomes + .is_empty() + ); + assert_eq!(second.state.carver_biomes, expected); + } + + /// The prepared worldgen data, if this checkout has it. + fn prepared_data() -> Option<Arc<GeneratorData>> { + let root = 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 None; + } + let registry: &'static BlockRegistry = Box::leak(Box::new( + crate::level::data::load_registry(&root).expect("registry"), + )); + Some(GeneratorData::load(&root.join("worldgen"), registry).expect("worldgen data")) + } + + /// Seed 1234's strongholds where vanilla 26.3 puts them + /// (VanillaStructureStartOracle with HORIZON_RINGS). Each search sampled + /// its quarts through fresh resolvers, which broke climate ties their own + /// way: three of the 128 were up to 6 chunks off. + #[test] + fn stronghold_rings_match_vanilla() { + let Some(data) = prepared_data() else { + return; + }; + let generator = OverworldGenerator::new(data, 1234).expect("generator"); + generator.worker().structure_starts(ChunkPos::new(0, 0)); + let rings = generator + .ring_positions("minecraft:strongholds") + .expect("ring positions"); + assert_eq!(rings.len(), 128); + for (i, x, z) in [ + (0, -100, -125), + (1, 131, -12), + (111, 1190, -560), + (113, 1305, -112), + ] { + assert_eq!(rings[i], ChunkPos::new(x, z), "stronghold {i}"); + } + } + + /// Generators of one seed and dimension share their ring positions; + /// another seed or generator gets its own, and old entries go. + #[test] + fn ring_positions_are_shared_by_seed_and_dimension() { + let Some(data) = prepared_data() else { + return; + }; let first = OverworldGenerator::new(data.clone(), 1234).expect("generator"); let again = OverworldGenerator::new(data.clone(), 1234).expect("generator"); { @@ -2001,4 +2631,121 @@ mod tests { .is_err() ); } + #[test] + fn neighbour_work_cleanup_keeps_active_keys_and_releases_idle_keys() { + let work = ChunkWork::new(); + let first = work.at((0, 0)); + let held = first.lock().unwrap(); + let same = work.at((0, 0)); + assert!(Arc::ptr_eq(&first, &same)); + assert!(!Arc::ptr_eq(&first, &work.at((1, 0)))); + // Sweep many expired weak entries while a builder and a prospective + // waiter retain their key. Cleanup must not split their coordination. + for x in 1..8192 { + drop(work.at((x, 1))); + } + assert!(work.0.lock().unwrap().len() <= 4096); + assert!(Arc::ptr_eq(&same, &work.at((0, 0)))); + let weak = Arc::downgrade(&first); + drop(held); + drop((first, same)); + assert!(weak.upgrade().is_none()); + let next = work.at((0, 0)); + assert_eq!(Arc::strong_count(&next), 1); + } + + /// Equal concurrent misses share one biome volume and the same mutable + /// structure pieces. Exercise cold ring startup before work coordination, + /// then cache eviction and explicit start invalidation with old Arcs alive. + #[test] + fn concurrent_neighbours_share_results_and_survive_invalidation() { + let root = 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 = Box::leak(Box::new( + crate::level::data::load_registry(&root).expect("registry"), + )); + let data = GeneratorData::load(&root.join("worldgen"), registry).expect("worldgen data"); + let preset: Value = serde_json::from_str(&data.world_presets["minecraft:normal"]).unwrap(); + let generator_json = &preset["dimensions"]["minecraft:overworld"]["generator"]; + let settings: Value = + serde_json::from_str(&data.settings[generator_json["settings"].as_str().unwrap()]) + .unwrap(); + let pipeline = Arc::new( + Pipeline::new( + data, + 12345, + XoroshiroRandom::new(12345), + generator_json, + &settings, + true, + Some((-64, 384)), + ) + .expect("pipeline"), + ); + let generator = OverworldGenerator { + pipeline: pipeline.clone(), + }; + let barrier = std::sync::Barrier::new(6); + let pos = ChunkPos::new(11, -11); // ancient city for this seed + let results = std::thread::scope(|scope| { + let handles: Vec<_> = (0..6) + .map(|_| { + scope.spawn(|| { + let mut worker = generator.worker(); + barrier.wait(); + let biomes = pipeline.biome_chunk(&mut worker.state, pos).unwrap(); + let starts = pipeline.structure_starts(&mut worker.state, pos); + (biomes, starts) + }) + }) + .collect(); + handles + .into_iter() + .map(|h| h.join().unwrap()) + .collect::<Vec<_>>() + }); + let (biomes, starts) = &results[0]; + assert!(!starts.is_empty(), "fixture must exercise shared pieces"); + for (b, s) in &results[1..] { + assert!(Arc::ptr_eq(biomes, b)); + assert_eq!(starts.len(), s.len()); + for (first, other) in starts.iter().zip(s) { + assert!(Arc::ptr_eq(first, other)); + } + } + // Force the existing whole-cache biome eviction threshold. + { + let mut cache = pipeline.biome_cache.lock().unwrap(); + cache.clear(); + for x in 0..CACHE_LIMIT as i32 { + cache.insert((x, 10000), biomes.clone()); + } + } + let mut worker = generator.worker(); + let rebuilt = pipeline.biome_chunk(&mut worker.state, pos).unwrap(); + assert!(!Arc::ptr_eq(biomes, &rebuilt)); + assert_eq!(pipeline.biome_cache.lock().unwrap().len(), 1); + for section in 0..24 { + assert_eq!(biomes.section(section), rebuilt.section(section)); + } + worker.forget_structure_starts(); + let fresh = worker.structure_starts(pos); + assert_eq!(starts.len(), fresh.len()); + for (old, new) in starts.iter().zip(&fresh) { + assert!(!Arc::ptr_eq(old, new)); + assert_eq!(old.name, new.name); + assert_eq!(old.bounding_box, new.bounding_box); + } + let again = worker.structure_starts(pos); + for (first, hit) in fresh.iter().zip(&again) { + assert!(Arc::ptr_eq(first, hit)); + } + } } diff --git a/crates/horizon-world/src/worldgen/generator_blocks.rs b/crates/horizon-world/src/worldgen/generator_blocks.rs index dc7db7d20..ed1182f36 100644 --- a/crates/horizon-world/src/worldgen/generator_blocks.rs +++ b/crates/horizon-world/src/worldgen/generator_blocks.rs @@ -8,8 +8,7 @@ use crate::{ block_support::BlockSupportRegistry, block_survival::{BlockSurvivalRegistry, SurvivalDefinitions, SurvivalRule}, collision::CachedCollisionRegistry, - nbt::{self, Limits, Tag}, - registry::BlockRegistry, + registry::{BlockRegistry, BlockTableFile}, }; use serde_json::Value; use std::{ @@ -102,24 +101,21 @@ impl BlockBehavior { if !dir.join("block-properties.nbt").exists() { return Ok(None); } - let read = |name: &str| -> Result<Tag, GeneratorError> { - Ok( - nbt::read_named(&fs::read(dir.join(name))?, Limits::tables()) - .map_err(|e| resource(format!("{name}: {e:?}")))? - .0 - .tag, - ) + let read = |name: &str| -> Result<BlockTableFile<'static>, GeneratorError> { + BlockTableFile::open(&dir.join(name), registry) + .map_err(|e| resource(format!("{name}: {e}"))) }; let json = |name: &str| -> Result<Value, GeneratorError> { Ok(serde_json::from_str(&fs::read_to_string(dir.join(name))?)?) }; let collisions = - CachedCollisionRegistry::from_nbt(&read("cached-collision.nbt")?, registry)?; - let properties = - BlockPropertiesRegistry::from_nbt(&read("block-properties.nbt")?, &collisions)?; - let support = Arc::new(BlockSupportRegistry::from_nbt( - &read("cached-support.nbt")?, - registry, + CachedCollisionRegistry::from_table(read("cached-collision.nbt")?.table("collision")?)?; + let properties = BlockPropertiesRegistry::from_table( + read("block-properties.nbt")?.table("block property")?, + &collisions, + )?; + let support = Arc::new(BlockSupportRegistry::from_table( + read("cached-support.nbt")?.table("support")?, )?); let collision = Arc::new(BlockCollisionRegistry::new(Arc::new(collisions))?); let rules = json("survival-rules.json")? diff --git a/crates/horizon-world/src/worldgen/generator_structures.rs b/crates/horizon-world/src/worldgen/generator_structures.rs index f5487dc33..1e4913c95 100644 --- a/crates/horizon-world/src/worldgen/generator_structures.rs +++ b/crates/horizon-world/src/worldgen/generator_structures.rs @@ -2,7 +2,7 @@ use super::{ biome::BiomeId, block_feature::FeatureBlockBindings, - generator::{GeneratorData, GeneratorError}, + generator::{ChunkWork, GeneratorData, GeneratorError}, structure::{ StructureResources, start::{ @@ -17,7 +17,7 @@ use std::collections::BTreeMap; use std::{ collections::{BTreeSet, HashMap}, sync::{ - Arc, Condvar, Mutex, OnceLock, + Arc, Condvar, Mutex, OnceLock, RwLock, atomic::{AtomicUsize, Ordering}, }, }; @@ -30,7 +30,8 @@ pub(super) struct StructureState { possible_sets: Vec<usize>, /// Structure indices of each decoration step, in registry order. pub(super) by_step: Vec<Vec<usize>>, - starts: Mutex<HashMap<(i32, i32), Starts>>, + starts: StartCache, + start_work: ChunkWork, /// Concentric ring positions, computed on first use and shared with /// later generators of the same seed and dimension. rings: Arc<OnceLock<Rings>>, @@ -73,14 +74,14 @@ impl RingWork { fn help<'a>( &self, preferred: impl Fn(usize) -> &'a BTreeSet<BiomeId>, - biome: &mut dyn FnMut([i32; 3]) -> BiomeId, + biomes: &mut dyn FnMut(&[[i32; 3]]) -> Vec<BiomeId>, ) { loop { let i = self.next.fetch_add(1, Ordering::Relaxed); let Some((set, search)) = self.searches.get(i) else { break; }; - let _ = self.found[i].set(search.find(preferred(*set), biome)); + let _ = self.found[i].set(search.find(preferred(*set), biomes)); let mut left = self.left.lock().unwrap(); *left -= 1; if *left == 0 { @@ -108,6 +109,45 @@ impl RingWork { } } const START_CACHE_LIMIT: usize = 65536; +const START_SHARDS: usize = 64; + +/// The built starts by chunk, in shards: a lookup locks one shard for +/// reading. Each generated chunk reads the 289 start chunks around it +/// (`references`) and the server thread reads the starts of the chunks its +/// mobs and players are in; with one lock for all, the server thread waited +/// on the workers. +struct StartCache { + shards: Box<[RwLock<HashMap<(i32, i32), Starts>>]>, +} +impl StartCache { + fn new() -> Self { + Self { + shards: (0..START_SHARDS).map(|_| RwLock::default()).collect(), + } + } + fn shard(&self, key: (i32, i32)) -> &RwLock<HashMap<(i32, i32), Starts>> { + let hash = + (key.0 as u32).wrapping_mul(0x9E37_79B9) ^ (key.1 as u32).wrapping_mul(0x85EB_CA6B); + &self.shards[(hash >> (32 - START_SHARDS.trailing_zeros())) as usize] + } + fn get(&self, key: (i32, i32)) -> Option<Starts> { + self.shard(key).read().unwrap().get(&key).cloned() + } + /// Keeps `starts` unless another thread built the same chunk's first: + /// everyone keeps the first, so placement state on its pieces is shared. + fn insert_first(&self, key: (i32, i32), starts: Starts) -> Starts { + let mut shard = self.shard(key).write().unwrap(); + if shard.len() >= START_CACHE_LIMIT / START_SHARDS { + shard.clear(); + } + shard.entry(key).or_insert(starts).clone() + } + fn clear(&self) { + for shard in &self.shards { + shard.write().unwrap().clear(); + } + } +} fn biome_set(data: &GeneratorData, value: &Value) -> Result<BTreeSet<BiomeId>, super::DecodeError> { let names: Vec<&str> = match value { @@ -156,18 +196,27 @@ impl StructureState { for (i, def) in registry.structures.iter().enumerate() { by_step[def.step].push(i); } - let bindings = FeatureBlockBindings::with_tags( + let mut bindings = FeatureBlockBindings::with_tags( data.registry(), data.integers().clone(), &data.block_tags, )?; + // Processors that test what a block is made of read canOcclude. + if let Some(blocks) = data + .feature_resources + .as_ref() + .and_then(|r| r.blocks.as_ref()) + { + bindings = bindings.with_block_properties(&blocks.properties)?; + } Ok(Some(Self { registry, resources: resources.clone(), bindings, possible_sets, by_step, - starts: Mutex::new(HashMap::new()), + starts: StartCache::new(), + start_work: ChunkWork::new(), rings, ring_work: OnceLock::new(), })) @@ -190,8 +239,8 @@ impl StructureState { ) }); let registry = &self.registry; - work.help(|set| registry.ring_biomes(set), &mut |q| { - context.noise_biome(q) + work.help(|set| registry.ring_biomes(set), &mut |quarts| { + context.noise_biomes(quarts) }); self.rings .get_or_init(|| work.rings(|set| registry.sets[set].name.clone())) @@ -199,7 +248,9 @@ impl StructureState { /// Drop the cached starts, so that later chunks build fresh pieces (the /// pieces keep per-start placement state such as a shipwreck's height). pub(super) fn forget_starts(&self) { - self.starts.lock().unwrap().clear(); + // As before, a build already in flight can publish after this clear. + // Do not wait for work locks or retain initialized entries here. + self.starts.clear(); } /// The (cached) structure starts owned by a chunk. pub(super) fn starts_at( @@ -207,10 +258,17 @@ impl StructureState { context: &mut impl super::structure::start::StartContext, chunk: ChunkPos, ) -> Starts { - if let Some(found) = self.starts.lock().unwrap().get(&(chunk.x, chunk.z)) { - return found.clone(); + if let Some(found) = self.starts.get((chunk.x, chunk.z)) { + return found; } + // Ring searches enlist other workers and wait for them: never hold a + // chunk-work lock while helping, even when this chunk has no starts. let rings = self.rings(context); + let work = self.start_work.at((chunk.x, chunk.z)); + let _work = work.lock().unwrap(); + if let Some(found) = self.starts.get((chunk.x, chunk.z)) { + return found; + } let starts: Starts = Arc::new( create_starts( &self.registry, @@ -225,12 +283,7 @@ impl StructureState { .map(Arc::new) .collect(), ); - let mut cache = self.starts.lock().unwrap(); - if cache.len() >= START_CACHE_LIMIT { - cache.clear(); - } - cache.insert((chunk.x, chunk.z), starts.clone()); - starts + self.starts.insert_first((chunk.x, chunk.z), starts) } /// ChunkGenerator.findNearestMapStructure: the nearest start of one of /// `names` from block `from` (rings: every ring chunk; random spreads: @@ -384,13 +437,51 @@ mod tests { use super::*; use crate::worldgen::structure::stronghold::{ring_positions, ring_searches}; + /// The start cache keeps the first starts built for a chunk, spreads a + /// chunk's 289 reference lookups over the shards, and forgets on clear. + #[test] + fn start_cache_keeps_first_and_spreads_neighbours() { + let cache = StartCache::new(); + let first: Starts = Arc::new(Vec::new()); + let second: Starts = Arc::new(Vec::new()); + assert!(cache.get((3, -4)).is_none()); + assert!(Arc::ptr_eq( + &cache.insert_first((3, -4), first.clone()), + &first + )); + assert!(Arc::ptr_eq(&cache.insert_first((3, -4), second), &first)); + assert!(Arc::ptr_eq(&cache.get((3, -4)).unwrap(), &first)); + for (cx, cz) in [(0, 0), (-1000, 77), (i32::MAX - 8, i32::MIN + 8)] { + let mut used = vec![0; START_SHARDS]; + for x in cx - 8..=cx + 8 { + for z in cz - 8..=cz + 8 { + let shard = cache.shard((x, z)) as *const _; + let i = cache + .shards + .iter() + .position(|s| std::ptr::eq(s, shard)) + .unwrap(); + used[i] += 1; + } + } + assert!(used.iter().all(|&n| n > 0 && n < 20), "{used:?}"); + } + cache.clear(); + assert!(cache.get((3, -4)).is_none()); + } + /// Searches shared between threads give the positions of one thread /// running them in order, set by set. #[test] fn ring_work_shared_between_threads_matches_serial_search() { let preferred: BTreeSet<BiomeId> = (0..20).map(|b| BiomeId(b * 3)).collect(); - let biome = |q: [i32; 3]| { - BiomeId((q[0].wrapping_mul(31) ^ q[2].wrapping_mul(17)).rem_euclid(64) as u32) + let biomes = |quarts: &[[i32; 3]]| -> Vec<BiomeId> { + quarts + .iter() + .map(|q| { + BiomeId((q[0].wrapping_mul(31) ^ q[2].wrapping_mul(17)).rem_euclid(64) as u32) + }) + .collect() }; let none = BTreeSet::new(); for seed in [0, 1234, -77, i64::MAX] { @@ -402,12 +493,12 @@ mod tests { let sets = [&preferred, &none, &preferred]; std::thread::scope(|scope| { for _ in 0..4 { - scope.spawn(|| work.help(|set| sets[set], &mut { biome })); + scope.spawn(|| work.help(|set| sets[set], &mut { biomes })); } }); let rings = work.rings(|set| format!("set{set}")); let serial = |s: i64, distance, spread, count, preferred| { - ring_positions(s, distance, spread, count, preferred, &mut { biome }) + ring_positions(s, distance, spread, count, preferred, &mut { biomes }) }; assert_eq!(rings["set0"], serial(seed, 32, 3, 128, &preferred)); assert_eq!(rings["set1"], Vec::new()); diff --git a/crates/horizon-world/src/worldgen/material_condition.rs b/crates/horizon-world/src/worldgen/material_condition.rs index 30eb26ec8..6433b09a4 100644 --- a/crates/horizon-world/src/worldgen/material_condition.rs +++ b/crates/horizon-world/src/worldgen/material_condition.rs @@ -300,6 +300,14 @@ enum Kind<R: RandomSource> { Not(Box<Compiled<R>>), } impl<R: RandomSource> Compiled<R> { + fn constant_result(&self) -> Option<bool> { + match &self.kind { + Kind::Constant(value) => Some(*value), + Kind::Not(child) => child.constant_result().map(|value| !value), + _ => None, + } + } + fn compile<S: MaterialSurfaceQueries>( condition: MaterialCondition, root: &R::Positional, diff --git a/crates/horizon-world/src/worldgen/material_condition/density.rs b/crates/horizon-world/src/worldgen/material_condition/density.rs index 5c98c75e0..b10ea815a 100644 --- a/crates/horizon-world/src/worldgen/material_condition/density.rs +++ b/crates/horizon-world/src/worldgen/material_condition/density.rs @@ -15,6 +15,8 @@ pub struct MaterialDensitySources<S: MaterialSurfaceQueries> { surface: S, expected: DensityVolume, densities: Vec<BoundDensity>, + /// Eagerly sampled volumes, shared by bindings of the same density root. + buffers: Vec<Vec<f32>>, retained_samples: usize, plane: DensityVolume, sampler: Arc<dyn DensitySampler>, @@ -23,7 +25,7 @@ pub struct MaterialDensitySources<S: MaterialSurfaceQueries> { } struct BoundDensity { sampler: Arc<dyn DensitySampler>, - buffer: Option<Vec<f32>>, + buffer: Option<usize>, } impl<S: MaterialSurfaceQueries> MaterialDensitySources<S> { pub fn sampler_context_mut(&mut self) -> &mut SamplerContext { @@ -50,6 +52,7 @@ impl<S: MaterialSurfaceQueries> MaterialDensitySources<S> { surface, expected, densities: Vec::new(), + buffers: Vec::new(), retained_samples: 0, plane, sampler, @@ -71,13 +74,23 @@ impl<S: MaterialSurfaceQueries> MaterialSurfaceQueries for MaterialDensitySource { return Err(DecodeError::Field("material density storage bound")); } - let buffer = if eager { - let mut buffer = vec![0.0; self.expected.size()]; - sampler.sample_volume(&mut self.context, &mut buffer, self.expected)?; - self.retained_samples += buffer.len(); - Some(buffer) - } else { - None + let shared = self + .densities + .iter() + .find(|bound| bound.buffer.is_some() && Arc::ptr_eq(&bound.sampler, &sampler)) + .and_then(|bound| bound.buffer); + let buffer = match (eager, shared) { + (false, _) => None, + // Sampling a root over the same volume and context is deterministic, + // so a second binding (iron and copper veins' richness) reuses it. + (true, Some(buffer)) => Some(buffer), + (true, None) => { + let mut buffer = vec![0.0; self.expected.size()]; + sampler.sample_volume(&mut self.context, &mut buffer, self.expected)?; + self.retained_samples += buffer.len(); + self.buffers.push(buffer); + Some(self.buffers.len() - 1) + } }; let index = self.densities.len(); self.densities.push(BoundDensity { sampler, buffer }); @@ -89,9 +102,9 @@ impl<S: MaterialSurfaceQueries> MaterialSurfaceQueries for MaterialDensitySource .get(index) .ok_or(MaterialRuleError::MissingDensityProvider)?; let position = [position.x, position.y, position.z]; - if let Some(buffer) = &source.buffer { - if let Some(index) = self.expected.index_of_block(position) { - return Ok(buffer[index]); + if let Some(buffer) = source.buffer { + if let Some(index) = block_index(self.expected, position) { + return Ok(self.buffers[buffer][index]); } } Ok(source.sampler.sample_value(&mut self.context, position)?) @@ -145,3 +158,121 @@ impl<S: MaterialSurfaceQueries> MaterialSurfaceQueries for MaterialDensitySource self.surface.band(position) } } + +/// `DensityVolume::index_of_block` without its per-axis divisions for the +/// unit-step volumes the material stage samples (one or two lookups per block). +#[inline] +fn block_index(volume: DensityVolume, position: [i32; 3]) -> Option<usize> { + if volume.steps() != [1; 3] { + return volume.index_of_block(position); + } + let [mx, my, mz] = volume.minimum(); + let [sx, sy, sz] = volume.sizes(); + let x = position[0].wrapping_sub(mx) as u32 as usize; + let y = position[1].wrapping_sub(my) as u32 as usize; + let z = position[2].wrapping_sub(mz) as u32 as usize; + (x < sx && y < sy && z < sz).then(|| y + (x + z * sx) * sy) +} + +#[cfg(test)] +mod tests { + use super::*; + use horizon_core::density::SampleError; + use std::sync::atomic::{AtomicUsize, Ordering}; + + struct Surface; + impl MaterialSurfaceQueries for Surface { + fn surface_depth(&mut self, _: i32, _: i32) -> i32 { + 0 + } + fn surface_secondary(&mut self, _: i32, _: i32) -> f64 { + 0.0 + } + } + /// Position-dependent values; counts bulk samples. + #[derive(Debug, Default)] + struct Counted(AtomicUsize); + impl DensitySampler for Counted { + fn sample_value(&self, _: &mut SamplerContext, p: [i32; 3]) -> Result<f32, SampleError> { + Ok((p[0] * 10_000 + p[1] * 100 + p[2]) as f32) + } + fn sample_volume( + &self, + context: &mut SamplerContext, + buffer: &mut [f32], + volume: DensityVolume, + ) -> Result<(), SampleError> { + self.0.fetch_add(1, Ordering::Relaxed); + for z in 0..volume.sizes()[2] { + for x in 0..volume.sizes()[0] { + for y in 0..volume.sizes()[1] { + let [mx, my, mz] = volume.minimum(); + let p = [mx + x as i32, my + y as i32, mz + z as i32]; + buffer[volume.index([x, y, z]).unwrap()] = self.sample_value(context, p)?; + } + } + } + Ok(()) + } + } + fn sources() -> MaterialDensitySources<Surface> { + let volume = DensityVolume::new([16, 24, 16], [32, -64, -16], [1; 3]).unwrap(); + MaterialDensitySources::new( + Surface, + volume, + Arc::new(Counted::default()), + SamplerContext::default(), + ) + .unwrap() + } + + #[test] + fn eager_bindings_of_one_root_share_a_single_sample() { + let mut sources = sources(); + let shared = Arc::new(Counted::default()); + let other = Arc::new(Counted::default()); + let a = sources.bind_density(shared.clone(), true).unwrap(); + let lazy = sources.bind_density(shared.clone(), false).unwrap(); + let b = sources.bind_density(shared.clone(), true).unwrap(); + let c = sources.bind_density(other.clone(), true).unwrap(); + assert_eq!(shared.0.load(Ordering::Relaxed), 1); + assert_eq!(other.0.load(Ordering::Relaxed), 1); + assert_eq!([a, lazy, b, c], [0, 1, 2, 3]); + for p in [[32, -64, -16], [47, -41, -1], [40, -50, -9], [48, 0, 0]] { + let position = BlockPos::new(p[0], p[1], p[2]); + let expected = (p[0] * 10_000 + p[1] * 100 + p[2]) as f32; + for index in [a, lazy, b, c] { + assert_eq!(sources.density(index, position).unwrap(), expected); + } + } + assert_eq!(shared.0.load(Ordering::Relaxed), 1); + } + + #[test] + fn block_index_matches_index_of_block() { + let unit = DensityVolume::new([16, 24, 16], [i32::MAX - 7, -64, -16], [1; 3]).unwrap(); + let strided = DensityVolume::new([5, 3, 5], [-8, -64, 0], [4, 8, 4]).unwrap(); + for volume in [unit, strided] { + let [mx, my, mz] = volume.minimum(); + for dx in [-1, 0, 1, 4, 7, 8, 15, 16, 17, 20] { + for dy in [-9, -1, 0, 8, 16, 23, 24] { + for dz in [-1, 0, 3, 15, 16, 20] { + let p = [ + mx.wrapping_add(dx), + my.wrapping_add(dy), + mz.wrapping_add(dz), + ]; + assert_eq!(block_index(volume, p), volume.index_of_block(p), "{p:?}"); + } + } + } + for p in [ + [i32::MIN, 0, 0], + [i32::MAX, i32::MAX, i32::MAX], + [0, i32::MIN, 0], + ] { + assert_eq!(block_index(volume, p), volume.index_of_block(p), "{p:?}"); + } + } + } +} diff --git a/crates/horizon-world/src/worldgen/material_condition/rule.rs b/crates/horizon-world/src/worldgen/material_condition/rule.rs index 9dfc2ff1e..cf4fc8547 100644 --- a/crates/horizon-world/src/worldgen/material_condition/rule.rs +++ b/crates/horizon-world/src/worldgen/material_condition/rule.rs @@ -70,6 +70,7 @@ impl From<horizon_core::density::SampleError> for MaterialRuleError { } pub(super) enum CompiledRule<R: RandomSource> { + Empty, Block(BlockStateId), OreVein { blocks: [BlockStateId; 3], @@ -127,7 +128,12 @@ impl<R: RandomSource> CompiledRule<R> { // whether the predicate will match any subsequent position. let condition = Compiled::compile(if_true, root, context, depth + 1)?; let rule = Box::new(Self::compile(*then_run, root, context, depth + 1, nodes)?); - Self::Condition { condition, rule } + // Compile both sides first: binding and errors must retain their order. + match condition.constant_result() { + Some(false) => Self::Empty, + Some(true) => *rule, + None => Self::Condition { condition, rule }, + } } MaterialRule::Sequence(mut children) => { if children.len() == 1 { @@ -135,9 +141,16 @@ impl<R: RandomSource> CompiledRule<R> { } let mut result = Vec::with_capacity(children.len()); for child in children { - result.push(Self::compile(child, root, context, depth + 1, nodes)?); + let child = Self::compile(child, root, context, depth + 1, nodes)?; + if !matches!(child, Self::Empty) { + result.push(child); + } + } + match result.len() { + 0 => Self::Empty, + 1 => result.pop().unwrap(), + _ => Self::Sequence(result), } - Self::Sequence(result) } }) } @@ -147,6 +160,7 @@ impl<R: RandomSource> CompiledRule<R> { context: &mut Context<S>, ) -> Result<Option<BlockStateId>, MaterialRuleError> { match self { + Self::Empty => Ok(None), Self::Block(block) => Ok(Some(*block)), Self::OreVein { blocks, diff --git a/crates/horizon-world/src/worldgen/rule_test.rs b/crates/horizon-world/src/worldgen/rule_test.rs index d9f836961..5202baa31 100644 --- a/crates/horizon-world/src/worldgen/rule_test.rs +++ b/crates/horizon-world/src/worldgen/rule_test.rs @@ -3,7 +3,11 @@ use super::{ DecodeError, Object, block_feature::FeatureBlockBindings, density::identifier, integer, number_text, required, }; -use crate::registry::BlockStateId; +use crate::{ + block_properties::BoundBlockProperty, + fluid::{FluidKind, FluidStateTable}, + registry::BlockStateId, +}; use horizon_core::random::RandomSource; use serde_json::value::RawValue; use std::{collections::BTreeSet, sync::Arc}; @@ -21,6 +25,85 @@ enum Node { RandomStates(Arc<BTreeSet<BlockStateId>>, f32), RandomState(BlockStateId, f32), Height(i32, i32), + Phase(Box<MatterPhase>), +} +/// `horizon:matter_phase`: Repurposed Structures' MatterPhaseRuleTest, which +/// its mineshafts and Nether monument use to match a block by what it is +/// made of (`phase_to_test_for`), optionally inverted (`invert_condition`). +#[derive(Clone, Debug)] +struct MatterPhase { + phase: Phase, + invert: bool, + air: [BlockStateId; 3], + fluids: Arc<FluidStateTable>, + /// Only for `SOLID`. + can_occlude: Option<BoundBlockProperty>, + /// Blocks.RAIL and Blocks.CHAIN (26.3's iron chain). + rail_or_chain: BTreeSet<BlockStateId>, +} +#[derive(Clone, Copy, Debug)] +enum Phase { + Solid, + Liquid, + Air, + AirRailOrChain, + LiquidRailOrChain, +} +impl MatterPhase { + fn decode(object: &Object, bindings: &FeatureBlockBindings<'_>) -> Result<Self, DecodeError> { + let name: String = serde_json::from_str(required(object, "phase_to_test_for")?.get())?; + let phase = match name.as_str() { + "SOLID" => Phase::Solid, + "LIQUID" => Phase::Liquid, + "AIR" => Phase::Air, + "AIR_RAIL_OR_CHAIN" => Phase::AirRailOrChain, + "LIQUID_RAIL_OR_CHAIN" => Phase::LiquidRailOrChain, + _ => return Err(DecodeError::Field("matter phase")), + }; + let invert = match object.get("invert_condition") { + Some(v) => serde_json::from_str(v.get())?, + None => false, + }; + let can_occlude = match phase { + Phase::Solid => Some( + bindings + .can_occlude + .clone() + .ok_or(DecodeError::Field("matter phase block properties"))?, + ), + _ => None, + }; + let mut rail_or_chain = BTreeSet::new(); + for name in ["minecraft:rail", "minecraft:iron_chain"] { + rail_or_chain.extend(bindings.blocks.get(name).into_iter().flat_map(|s| s.iter())); + } + Ok(Self { + phase, + invert, + air: bindings.air, + fluids: bindings.fluids.table().clone(), + can_occlude, + rail_or_chain, + }) + } + fn test(&self, state: BlockStateId) -> bool { + let air = self.air.contains(&state); + let liquid = self + .fluids + .get(state) + .is_some_and(|f| f.kind() != FluidKind::Empty); + let rail_or_chain = || self.rail_or_chain.contains(&state); + let matched = match self.phase { + Phase::Solid => { + !air && !liquid && self.can_occlude.as_ref().is_some_and(|c| c.contains(state)) + } + Phase::Liquid => liquid, + Phase::Air => air, + Phase::AirRailOrChain => air || rail_or_chain(), + Phase::LiquidRailOrChain => liquid || rail_or_chain(), + }; + matched != self.invert + } } impl RuleTest { pub fn from_json(json: &str, bindings: &FeatureBlockBindings<'_>) -> Result<Self, DecodeError> { @@ -104,6 +187,9 @@ impl RuleTest { Node::Any(values) } } + "horizon:matter_phase" => { + Node::Phase(Box::new(MatterPhase::decode(&object, bindings)?)) + } _ => return Err(DecodeError::Field("replacement rule type")), }; Ok(Self(node)) @@ -128,6 +214,7 @@ impl RuleTest { state == *expected && random.next_float() < *probability } Node::Height(min, max) => *min <= position[1] && position[1] <= *max, + Node::Phase(phase) => phase.test(state), } } } diff --git a/crates/horizon-world/src/worldgen/state_provider.rs b/crates/horizon-world/src/worldgen/state_provider.rs index 37e9c866b..098decdf1 100644 --- a/crates/horizon-world/src/worldgen/state_provider.rs +++ b/crates/horizon-world/src/worldgen/state_provider.rs @@ -93,7 +93,7 @@ impl<'r> StateProviderBindings<'r> { } let mut bound_tags = BTreeMap::new(); for (name, entries) in tags { - if entries.len() > 8192 { + if entries.len() > MAX_TAG_BLOCKS { return Err(DecodeError::Field("provider tag size")); } let values = entries @@ -558,6 +558,11 @@ impl BlockStateProvider { } } +/// Blocks a bound block tag may list (8,192 before content mods, vanilla's +/// tags fit well within it): content mods' blocks join the shared tags +/// (`mineable/pickaxe` takes every stone block of a pack). +pub(crate) const MAX_TAG_BLOCKS: usize = 1 << 20; + /// Bind the pinned registry's full-axis/full-facing/horizontal-facing property /// families. A five-way facing or two-axis property is a different property and /// is intentionally left unchanged. Unrelated properties retain their values. diff --git a/crates/horizon-world/src/worldgen/structure.rs b/crates/horizon-world/src/worldgen/structure.rs index f15c37dc2..61a48f3ea 100644 --- a/crates/horizon-world/src/worldgen/structure.rs +++ b/crates/horizon-world/src/worldgen/structure.rs @@ -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; @@ -10,13 +11,16 @@ pub mod mineshaft; pub mod monument; pub mod nether_fossil; pub mod ocean_ruin; +pub mod piece_limited; pub mod processor; pub mod ruined_portal; pub mod simple; pub mod start; +pub mod start_checks; pub mod stronghold; pub mod template; pub mod temple; +pub mod yung; use crate::{ nbt::{self, Limits}, @@ -341,11 +345,7 @@ impl StructureResources { flate2::read::GzDecoder::new(bytes.as_slice()) .read_to_end(&mut raw) .ok()?; - let limits = Limits { - max_bytes: 1 << 28, - ..Limits::default() - }; - let tag = nbt::read_named(&raw, limits).ok()?.0.tag; + let tag = nbt::read_named(&raw, Limits::templates()).ok()?.0.tag; Some(Arc::new(StructureTemplate::from_nbt( &tag, self.registry, diff --git a/crates/horizon-world/src/worldgen/structure/enhanced_terrain.rs b/crates/horizon-world/src/worldgen/structure/enhanced_terrain.rs new file mode 100644 index 000000000..404b9ecca --- /dev/null +++ b/crates/horizon-world/src/worldgen/structure/enhanced_terrain.rs @@ -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])); + } + } +} diff --git a/crates/horizon-world/src/worldgen/structure/jigsaw.rs b/crates/horizon-world/src/worldgen/structure/jigsaw.rs index a50275941..8e2b96e63 100644 --- a/crates/horizon-world/src/worldgen/structure/jigsaw.rs +++ b/crates/horizon-world/src/worldgen/structure/jigsaw.rs @@ -30,7 +30,7 @@ pub enum Projection { Rigid, TerrainMatching, } -#[derive(Debug)] +#[derive(Clone, Debug)] pub enum PoolElement { Empty, Single { @@ -39,6 +39,12 @@ pub enum PoolElement { legacy: bool, projection: Projection, waterlogging: Option<bool>, + /// 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, @@ -81,11 +87,12 @@ fn opposite(d: Direction) -> Direction { Direction::East => Direction::West, } } -fn step(d: Direction) -> [i32; 3] { +pub(super) fn step(d: Direction) -> [i32; 3] { let n = d.normal(); [n[0], n[1], n[2]] } -fn shuffle<T>(list: &mut [T], random: &mut impl RandomSource) { +/// Util.shuffle. +pub(super) fn shuffle<T>(list: &mut [T], random: &mut impl RandomSource) { for i in (2..=list.len()).rev() { let swap = random.next_int_bound(i as i32).unwrap_or(0) as usize; list.swap(i - 1, swap); @@ -135,6 +142,14 @@ impl PoolElement { Some("ignore_waterlogging") => Some(false), _ => None, }, + yung: value + .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 { @@ -219,10 +234,13 @@ impl PoolElement { .first() .map(|e| e.shuffled_jigsaws(resources, position, rotation, random)) .unwrap_or_default(), - Self::Single { template, .. } => { + Self::Single { template, yung, .. } => { let mut jigsaws = template_jigsaws(resources, template, position, rotation); shuffle(&mut jigsaws, random); - jigsaws.sort_by_key(|j| std::cmp::Reverse(j.selection_priority)); + // YUNG's elements keep the shuffled order. + if yung.is_none() { + jigsaws.sort_by_key(|j| std::cmp::Reverse(j.selection_priority)); + } jigsaws } } @@ -230,6 +248,22 @@ impl PoolElement { pub fn ground_level_delta(&self) -> i32 { 1 } + /// YUNG's API's rules, on a piece of one of its structures. + pub fn yung(&self) -> Option<&super::yung::Rules> { + match self { + Self::Single { yung, .. } => yung.as_deref(), + _ => 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( @@ -274,7 +308,7 @@ fn template_jigsaws( }) .collect() } -fn can_attach(source: &Jigsaw, target: &Jigsaw) -> bool { +pub(super) fn can_attach(source: &Jigsaw, target: &Jigsaw) -> bool { source.front == opposite(target.front) && (source.rollable || source.top == target.top) && target @@ -285,12 +319,15 @@ fn can_attach(source: &Jigsaw, target: &Jigsaw) -> bool { #[derive(Debug)] pub struct Pool { - templates: Vec<Arc<PoolElement>>, - fallback: String, + /// Each element repeated by its weight. + pub(super) templates: Vec<Arc<PoolElement>>, + /// The elements with their weights, in file order (`rawTemplates`). + pub(super) raw: Vec<(Arc<PoolElement>, i32)>, + pub(super) fallback: String, max_size: OnceLock<i32>, } impl Pool { - fn random_template(&self, random: &mut impl RandomSource) -> Arc<PoolElement> { + pub(super) fn random_template(&self, random: &mut impl RandomSource) -> Arc<PoolElement> { if self.templates.is_empty() { return Arc::new(PoolElement::Empty); } @@ -304,7 +341,7 @@ impl Pool { shuffle(&mut copy, random); copy } - fn max_size(&self) -> i32 { + pub(super) fn max_size(&self) -> i32 { *self.max_size.get_or_init(|| { self.templates .iter() @@ -343,7 +380,7 @@ impl Pools { } let pool = self.raw.get(name).and_then(|json| { let value: Value = serde_json::from_str(json).ok()?; - let mut templates = Vec::new(); + let (mut templates, mut raw) = (Vec::new(), Vec::new()); for entry in value["elements"].as_array()? { let element = match PoolElement::decode(&entry["element"], resources, bindings, None) { @@ -355,12 +392,15 @@ impl Pools { return None; } }; - for _ in 0..entry["weight"].as_i64().unwrap_or(1) { + let weight = entry["weight"].as_i64().unwrap_or(1); + for _ in 0..weight { templates.push(element.clone()); } + raw.push((element, weight as i32)); } Some(Arc::new(Pool { templates, + raw, fallback: value["fallback"] .as_str() .unwrap_or("minecraft:empty") @@ -376,7 +416,7 @@ impl Pools { } } -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Junction { pub source_x: i32, pub source_ground_y: i32, @@ -393,7 +433,7 @@ pub struct PoolPiece { pub waterlogging: bool, } impl PoolPiece { - fn moved(&mut self, dy: i32) { + pub(super) fn moved(&mut self, dy: i32) { self.position[1] += dy; self.bounding_box = self.bounding_box.moved([0, dy, 0]); } @@ -466,6 +506,7 @@ fn place_element<W: FeatureWorld + PlacementContext, R: RandomSource>( legacy, projection, waterlogging: override_liquid, + .. } => { let ignore = if *legacy { IGNORE_STRUCTURE_AND_AIR.get_or_init(|| { @@ -547,13 +588,13 @@ pub struct JigsawConfig<'a> { /// Free space for children: an inclusive container box minus placed boxes. #[derive(Debug)] -struct Free { - container: BoundingBox, - removed: Vec<BoundingBox>, +pub(super) struct Free { + pub(super) container: BoundingBox, + pub(super) removed: Vec<BoundingBox>, } impl Free { /// Whether every point of the (0.25-deflated) box lies in free space. - fn fits(&self, b: &BoundingBox) -> bool { + pub(super) fn fits(&self, b: &BoundingBox) -> bool { (0..3).all(|i| b.min[i] >= self.container.min[i] && b.max[i] <= self.container.max[i]) && !self.removed.iter().any(|r| r.intersects(b)) } diff --git a/crates/horizon-world/src/worldgen/structure/piece_limited.rs b/crates/horizon-world/src/worldgen/structure/piece_limited.rs new file mode 100644 index 000000000..687c01cd8 --- /dev/null +++ b/crates/horizon-world/src/worldgen/structure/piece_limited.rs @@ -0,0 +1,1056 @@ +//! The piece-limited jigsaw assembler of Repurposed Structures and Moog's +//! Structure Lib (PieceLimitedJigsawManager, the same code in both), for +//! their structures carried over from a modpack (docs/modpacks.md). Carry +//! maps their jigsaw types to `minecraft:jigsaw` with a +//! `"horizon:piece_limited"` key; vanilla data never has it. +//! +//! ```json +//! "horizon:piece_limited": { +//! "library": "moogs", // or "repurposed": their few differences +//! "min_y": 40, "max_y": 120, // y_allowance: pieces stay inside, unless buried +//! "burying": "lowest_corner", // or "average_land", "lowest_side" +//! "no_liquid": true, // cannot_spawn_in_liquid, as burying reads it +//! "ignore_bounds_pools": ["x:pool"], +//! "land_search": "highest_land", // the Nether variants: or "lowest_land", "fixed_height" +//! "ledge_offset_y": 2, +//! "pieces": [{"name": "x:piece", "required": 1, "min_depth": 3, "max": 2}], +//! "layout": "mineshaft" // Repurposed's mineshafts: moved under the terrain +//! } +//! ``` +//! +//! It differs from vanilla's JigsawPlacement: a fresh random seeded by the +//! chunk, the start piece drawn uniformly and moved to the surface at its +//! center, a bounding volume 120 blocks down and 180 up, children drawn by +//! weight from the pools' weighted entries in order (`nextInt(total) + 1`, +//! counted down), pieces capped by template (`max`) and some required +//! (`required`, from `min_depth`), the whole layout retried up to 40 times +//! until every required piece is in, and after it the pieces centred on the +//! chunk corner, then buried in the terrain or moved onto Nether land. +//! Repurposed's overworld mineshafts are not centred: their top goes 15 +//! blocks under the terrain around the chunk. +use super::{ + BoundingBox, Rotation, StructureResources, StructureTemplate, + jigsaw::{ + Free, JigsawConfig, Junction, PoolElement, PoolPiece, Pools, Projection, can_attach, + shuffle, step, + }, + start::StartContext, +}; +use crate::{ + heightmap::HeightmapType, identifier::Identifier, java_hash_map::JavaHashMap, + worldgen::block_feature::FeatureBlockBindings, +}; +use horizon_core::{ + position::ChunkPos, + random::{LegacyRandom, RandomSource, WorldgenRandom}, +}; +use serde_json::Value; +use std::{ + cell::RefCell, + collections::{HashSet, VecDeque}, + rc::Rc, + sync::Arc, +}; + +/// Which mod's copy of the code: they differ in a few post-layout steps. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Library { + Moogs, + Repurposed, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Burying { + LowestCorner, + AverageLand, + LowestSide, +} + +/// The structure type's postLayoutAdjustments. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Layout { + /// GenericJigsawStructure's: centred, then buried or moved onto land. + Generic, + /// Repurposed Structures' MineshaftStructure. + Mineshaft, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum LandSearch { + Highest, + Lowest, + Fixed, +} + +/// One template's counts (`*_pieces_spawn_counts`). +#[derive(Clone, Debug)] +struct Counted { + required: Option<i32>, + min_depth: i32, + max: Option<i32>, +} + +/// `"horizon:piece_limited"` of a jigsaw structure. +#[derive(Debug)] +pub struct Config { + library: Library, + min_y: Option<i32>, + max_y: Option<i32>, + burying: Option<Burying>, + no_liquid: bool, + ignore_bounds: HashSet<String>, + land_search: Option<LandSearch>, + ledge: i32, + layout: Layout, + /// Counted templates by id, in the order of Java's HashMap of the + /// required pieces (the first one still short is forced in first). + pieces: Vec<(Identifier, Counted)>, +} + +impl Config { + pub fn decode(v: &Value) -> Self { + let int = |k: &str| v[k].as_i64().map(|n| n as i32); + let mut pieces: Vec<(Identifier, Counted)> = Vec::new(); + for p in v["pieces"].as_array().into_iter().flatten() { + let Some(id) = p["name"].as_str().and_then(|n| Identifier::parse(n).ok()) else { + continue; + }; + let counted = Counted { + required: p["required"].as_i64().map(|n| n as i32), + min_depth: p["min_depth"].as_i64().unwrap_or(0) as i32, + max: p["max"].as_i64().map(|n| n as i32), + }; + // A later entry for the same piece replaces it, as HashMap.put. + match pieces.iter_mut().find(|(k, _)| *k == id) { + Some(slot) => slot.1 = counted, + None => pieces.push((id, counted)), + } + } + // getRequirePieces builds a HashMap of the required ones and the + // assembler copies it (new HashMap(map)): iterate as that copy does. + let mut map = JavaHashMap::default(); + for (i, (id, c)) in pieces.iter().enumerate() { + if c.required.is_some() { + map.put(id.clone(), i); + } + } + let order: Vec<usize> = map.copied().entries().map(|(_, &i)| i).collect(); + let mut sorted: Vec<(Identifier, Counted)> = + order.iter().map(|&i| pieces[i].clone()).collect(); + sorted.extend(pieces.iter().filter(|(_, c)| c.required.is_none()).cloned()); + Self { + library: match v["library"].as_str() { + Some("repurposed") => Library::Repurposed, + _ => Library::Moogs, + }, + min_y: int("min_y"), + max_y: int("max_y"), + burying: match v["burying"].as_str() { + Some("lowest_corner") => Some(Burying::LowestCorner), + Some("average_land") => Some(Burying::AverageLand), + Some("lowest_side") => Some(Burying::LowestSide), + _ => None, + }, + no_liquid: v["no_liquid"].as_bool().unwrap_or(false), + ignore_bounds: v["ignore_bounds_pools"] + .as_array() + .into_iter() + .flatten() + .filter_map(|p| Some(p.as_str()?.to_owned())) + .collect(), + land_search: match v["land_search"].as_str() { + Some("highest_land") => Some(LandSearch::Highest), + Some("lowest_land") => Some(LandSearch::Lowest), + Some("fixed_height") => Some(LandSearch::Fixed), + _ => None, + }, + ledge: int("ledge_offset_y").unwrap_or(0), + layout: match v["layout"].as_str() { + Some("mineshaft") => Layout::Mineshaft, + _ => Layout::Generic, + }, + pieces: sorted, + } + } + fn required(&self) -> impl Iterator<Item = &Counted> { + self.pieces + .iter() + .map(|(_, c)| c) + .filter(|c| c.required.is_some()) + } +} + +/// The counted templates, loaded, in [`Config::pieces`] order. +struct Counts(Vec<Arc<StructureTemplate>>); +impl Counts { + fn new(limits: &Config, resources: &StructureResources) -> Self { + Self( + limits + .pieces + .iter() + .map(|(id, _)| { + resources + .template(&id.to_string()) + .unwrap_or_else(|| Arc::new(StructureTemplate::empty())) + }) + .collect(), + ) + } + /// The index of a single element's template among the counted ones. + fn of(&self, element: &PoolElement) -> Option<usize> { + let PoolElement::Single { template, .. } = element else { + return None; + }; + self.0.iter().position(|t| Arc::ptr_eq(t, template)) + } +} + +/// PieceLimitedJigsawManager.assembleJigsawStructure and the structure's +/// postLayoutAdjustments: the start's center and the pieces, or None when +/// nothing starts. `start` is the chunk's corner at the sampled start height. +#[allow(clippy::too_many_arguments)] +pub fn assemble( + config: &JigsawConfig<'_>, + limits: &Config, + context: &mut impl StartContext, + pools: &Pools, + resources: &StructureResources, + bindings: &FeatureBlockBindings<'_>, + chunk: ChunkPos, + start: [i32; 3], +) -> Option<([i32; 3], Vec<PoolPiece>)> { + let mut random = WorldgenRandom::new(LegacyRandom::new(0)); + random.set_large_feature_seed(context.seed(), chunk.x, chunk.z); + let rotation = Rotation::random(&mut random); + let pool = pools.get(config.start_pool, resources, bindings)?; + // An empty start pool crashes the mod's generation; nothing starts. + if pool.templates.is_empty() { + return None; + } + let element = pool.random_template(&mut random); + if matches!(element.as_ref(), PoolElement::Empty) { + return None; + } + let mut first = PoolPiece { + bounding_box: element.bounding_box(start, rotation), + ground_level_delta: element.ground_level_delta(), + element, + position: start, + rotation, + junctions: Vec::new(), + waterlogging: config.waterlogging, + }; + let b = first.bounding_box; + let (cx, cz) = ((b.max[0] + b.min[0]) / 2, (b.max[2] + b.min[2]) / 2); + let top = limits.max_y.unwrap_or(i32::MAX); + let bottom = limits.min_y.unwrap_or(i32::MIN); + let cy = match config.project_to { + Some(kind) => { + let y = start[1] + context.base_height(cx, cz, kind); + if y > top || y < bottom { + return None; + } + y + } + None => start[1], + }; + first.moved(cy - (b.min[1] + first.ground_level_delta)); + let start_box = first.bounding_box; + let (start_position, start_rotation) = (first.position, first.rotation); + let counts = Counts::new(limits, resources); + let run_once = limits.required().next().is_none(); + let mut pieces = vec![first]; + let mut attempts = 0; + // `for (attempts = 0; runOnce || doesNotHaveAllRequiredPieces(...); attempts++)`: + // the first test sees the start piece alone. + while run_once || lacks_required(limits, &counts, &pieces) { + if attempts == 40 { + break; + } + if attempts > 0 { + let element = pool.random_template(&mut random); + pieces = vec![PoolPiece { + bounding_box: element.bounding_box(start_position, start_rotation), + ground_level_delta: element.ground_level_delta(), + element, + position: start_position, + rotation: start_rotation, + junctions: Vec::new(), + waterlogging: config.waterlogging, + }]; + } else { + pieces.truncate(1); + } + if config.max_depth > 0 { + let r = config.max_distance[0]; + let free = Rc::new(RefCell::new(Free { + container: BoundingBox::new([cx - r, cy - 120, cz - r], [cx + r, cy + 180, cz + r]), + removed: vec![start_box], + })); + let buried = limits.burying.is_some(); + let mut assembler = Assembler { + config, + limits, + counted: &counts, + pools, + resources, + bindings, + pieces: std::mem::take(&mut pieces), + states: vec![(free, 0)], + counts: vec![0; limits.pieces.len()], + max_y: if buried { i32::MAX } else { top }, + min_y: if buried { i32::MIN } else { bottom }, + }; + let mut queue = VecDeque::from([0]); + while let Some(piece) = queue.pop_front() { + assembler.children(piece, &mut queue, context, &mut random); + } + pieces = assembler.pieces; + } + if run_once { + break; + } + attempts += 1; + } + adjust(config, limits, context, start, &mut pieces); + if pieces.is_empty() { + return None; + } + let mut whole = pieces[0].bounding_box; + for p in &pieces[1..] { + whole.encapsulate(&p.bounding_box); + } + // The builder is cleared when the layout reaches past the build height. + if whole.max[1] > context.max_y() + 1 { + return None; + } + Some(([cx, cy, cz], pieces)) +} + +/// doesNotHaveAllRequiredPieces. +fn lacks_required(limits: &Config, counts: &Counts, pieces: &[PoolPiece]) -> bool { + let mut left: Vec<i32> = limits + .pieces + .iter() + .map(|(_, c)| c.required.unwrap_or(0)) + .collect(); + for p in pieces { + if let Some(i) = counts.of(&p.element) { + left[i] -= 1; + } + } + left.iter().any(|&n| n > 0) +} + +/// Moves a piece; Repurposed Structures' movePieceProperly also writes its +/// junctions, all three setters into the source X, so only the last sticks. +fn shift(limits: &Config, piece: &mut PoolPiece, d: [i32; 3]) { + piece.position = std::array::from_fn(|i| piece.position[i] + d[i]); + piece.bounding_box = piece.bounding_box.moved(d); + if limits.library == Library::Repurposed { + for j in &mut piece.junctions { + j.source_x = j.source_z + d[2]; + } + } +} +fn shift_all(limits: &Config, pieces: &mut [PoolPiece], d: [i32; 3]) { + for p in pieces { + shift(limits, p, d); + } +} + +/// The structure's postLayoutAdjustments. +fn adjust( + config: &JigsawConfig<'_>, + limits: &Config, + context: &mut impl StartContext, + start: [i32; 3], + pieces: &mut Vec<PoolPiece>, +) { + if pieces.is_empty() { + return; + } + if limits.layout == Layout::Mineshaft { + below_terrain(limits, context, start, pieces); + return; + } + // GeneralUtils.centerAllPieces on the chunk's corner. + let center = pieces[0].bounding_box.center(); + let d = [start[0] - center[0], 0, start[2] - center[2]]; + for p in pieces.iter_mut() { + // Centring uses StructurePiece.move in both mods. + p.position = std::array::from_fn(|i| p.position[i] + d[i]); + p.bounding_box = p.bounding_box.moved(d); + } + let offset_y = start[1]; + let sea = context.sea_level(); + if let Some(search) = limits.land_search { + nether(limits, context, search, offset_y, pieces); + return; + } + let Some(burying) = limits.burying else { + return; + }; + let kind = config.project_to.unwrap_or(HeightmapType::WorldSurfaceWg); + let ocean_floor = matches!( + kind, + HeightmapType::OceanFloorWg | HeightmapType::OceanFloor + ); + let box0 = pieces[0].bounding_box; + let mut land = match burying { + Burying::LowestCorner => [ + (box0.min[0], box0.min[2]), + (box0.min[0], box0.max[2]), + (box0.max[0], box0.min[2]), + (box0.max[0], box0.max[2]), + ] + .into_iter() + .map(|(x, z)| context.base_height(x, z, kind) - 1) + .min() + .unwrap_or(0), + Burying::LowestSide => { + let c = box0.center(); + let mut highest = i32::MAX; + for (x, z) in [ + (box0.min[0], c[2]), + (c[0], box0.max[2]), + (c[0], box0.min[2]), + (box0.max[0], c[2]), + ] { + // getFirstOccupiedHeight (Repurposed's cached free height - 1). + let land = context.base_height(x, z, kind) - 1; + match limits.min_y { + Some(min) if land >= min => highest = land, + Some(_) => {} + None => highest = highest.min(land), + } + } + if let Some(min) = limits.min_y.filter(|_| highest == i32::MAX) { + highest = min; + } + highest + } + Burying::AverageLand => { + average_land(limits, context, kind, ocean_floor, offset_y, pieces); + return; + } + }; + if limits.no_liquid || !ocean_floor { + land = land.max(sea); + } else { + land = land.min(sea - box0.span()[1]); + } + // offsetToNewHeight. + if limits.max_y.is_some() + && let Some(min) = limits.min_y + && box0.max[1] + offset_y < min + { + land = land.max(limits.max_y.unwrap_or(land)); + } + if let Some(min) = limits.min_y + && box0.min[1] + offset_y < min + { + land = land.min(min); + } + let dy = land - box0.min[1] + offset_y; + shift_all(limits, pieces, [0, dy, 0]); +} + +/// MineshaftStructure.postLayoutAdjustments: the layout's top moved to 15 +/// blocks under the lowest ocean floor at the chunk's middle and 16 blocks +/// to each side, within y_allowance; never up. +fn below_terrain( + limits: &Config, + context: &mut impl StartContext, + start: [i32; 3], + pieces: &mut [PoolPiece], +) { + let target = mineshaft_top(start, limits.min_y, |x, z| { + context.base_height(x, z, HeightmapType::OceanFloorWg) + }); + let top = pieces + .iter() + .map(|p| p.bounding_box.max[1]) + .max() + .unwrap_or(i32::MIN); + if target < limits.max_y.unwrap_or(i32::MAX) && target < top { + shift_all(limits, pieces, [0, target - top, 0]); + } +} + +/// Where [`below_terrain`] puts a mineshaft's top: `floor` is the first free +/// height over the ocean floor (getFirstFreeHeight, OCEAN_FLOOR_WG). +fn mineshaft_top( + start: [i32; 3], + min_y: Option<i32>, + mut floor: impl FnMut(i32, i32) -> i32, +) -> i32 { + let (x, z) = (start[0] + 7, start[2] + 7); + let mut terrain = floor(x, z) - 1; + // Plane.HORIZONTAL's order; the mod takes one more off after each. + for (dx, dz) in [(0, -16), (16, 0), (0, 16), (-16, 0)] { + terrain = terrain.min(floor(x + dx, z + dz)) - 1; + } + (terrain - 15).max(min_y.unwrap_or(i32::MIN)) +} + +/// AVERAGE_LAND: the mean land height over a grid on the first piece's +/// footprint; no structure when none of the heights is allowed. +fn average_land( + limits: &Config, + context: &mut impl StartContext, + kind: HeightmapType, + ocean_floor: bool, + offset_y: i32, + pieces: &mut Vec<PoolPiece>, +) { + let b = pieces[0].bounding_box; + let c = b.center(); + let (lx, lz) = (b.max[0] - b.min[0], b.max[2] - b.min[2]); + let radius = (f64::from(lx * lx + lz * lz).sqrt() as i32) / 2; + // The mod's loop never ends with a step of 0; one block apart instead. + let step = (radius / 2).max(1); + let (min, max) = ( + limits.min_y.unwrap_or(i32::MIN), + limits.max_y.unwrap_or(i32::MAX), + ); + let (mut sum, mut n) = (0i64, 0i64); + let mut x = -radius; + while x <= radius { + let mut z = -radius; + while z <= radius { + let h = context.base_height(c[0] + x, c[2] + z, kind) - 1; + if h > min && h < max { + sum += i64::from(h); + n += 1; + } + z += step; + } + x += step; + } + let mut average = (n > 0).then(|| sum as f64 / n as f64); + if average.is_none() { + average = limits.max_y.or(limits.min_y).map(f64::from); + } + let Some(mut average) = average else { + pieces.clear(); + return; + }; + if limits.no_liquid && !ocean_floor { + average = average.max(f64::from(context.sea_level())); + if let Some(max) = limits.max_y { + // Math.max, as the mod has it. + average = average.max(f64::from(max)); + } + } + let dy = average as i32 - b.min[1] + offset_y; + shift_all(limits, pieces, [0, dy, 0]); +} + +/// The Nether variants: onto the highest or lowest land at the layout's +/// center (or a fixed height), a ledge above it, within y_allowance. +fn nether( + limits: &Config, + context: &mut impl StartContext, + search: LandSearch, + offset_y: i32, + pieces: &mut [PoolPiece], +) { + let mut whole = pieces[0].bounding_box; + for p in &pieces[1..] { + whole.encapsulate(&p.bounding_box); + } + let sea = context.sea_level(); + let generation = context.generation(); + let terrain_limit = generation.min_y + generation.height; + let base = pieces[0].bounding_box.min[1]; + let found = |context: &mut dyn ColumnQuery, highest: bool| { + let c = whole.center(); + context.land(c[0], c[2], highest, !limits.no_liquid, sea, terrain_limit) + }; + let mut target = match (limits.library, search) { + (Library::Moogs, LandSearch::Fixed) => offset_y + limits.ledge, + (library, search) => { + let y = found(context, search == LandSearch::Highest); + let at = if y < terrain_limit && y > sea + 1 { + y + } else { + sea + }; + if library == Library::Repurposed { + shift_all(limits, pieces, [0, at + limits.ledge - base, 0]); + return; + } + at + limits.ledge + offset_y + } + }; + let height = whole.max[1] - whole.min[1]; + let below = whole.min[1] - base; + if let Some(top) = limits.max_y { + target = target.min(top - height - below); + } + if let Some(bottom) = limits.min_y { + target = target.max(bottom - below); + } + shift_all(limits, pieces, [0, target - base, 0]); +} + +/// GeneralUtils.getHighestLand and getLowestLand on the noise column. +trait ColumnQuery { + fn land( + &mut self, + x: i32, + z: i32, + highest: bool, + on_liquid: bool, + sea: i32, + terrain_limit: i32, + ) -> i32; +} +impl<C: StartContext> ColumnQuery for C { + fn land( + &mut self, + x: i32, + z: i32, + highest: bool, + on_liquid: bool, + sea: i32, + terrain_limit: i32, + ) -> i32 { + let min = self.generation().min_y; + let mut columns = self.base_columns( + x, + z, + &[HeightmapType::WorldSurfaceWg, HeightmapType::OceanFloorWg], + ); + let solid = columns.pop().unwrap_or_default(); + let filled = columns.pop().unwrap_or_default(); + land_in(&filled, &solid, min, highest, on_liquid, sea, terrain_limit) + } +} + +/// The land search over one noise column: `filled` (not air) and `solid` +/// (canOcclude, for the noise's own blocks) from `min` up; past the +/// column's ends a NoiseColumn reads air. +fn land_in( + filled: &[bool], + solid: &[bool], + min: i32, + highest: bool, + on_liquid: bool, + sea: i32, + terrain_limit: i32, +) -> i32 { + let at = |c: &[bool], y: i32| { + usize::try_from(y - min) + .ok() + .and_then(|i| c.get(i).copied()) + .unwrap_or(false) + }; + let air = |y: i32| !at(filled, y); + let stands = |y: i32| if on_liquid { !air(y) } else { at(solid, y) }; + if highest { + let mut y = terrain_limit - 40; + while y > sea { + if at(solid, y) && air(y + 3) && stands(y) { + return y; + } + y -= 1; + } + y + } else { + let mut y = sea + 1; + while y <= terrain_limit - 40 { + if stands(y) && air(y + 1) && air(y + 5) { + return y + 1; + } + y += 1; + } + sea + } +} + +/// PieceLimitedJigsawManager.Assembler. +struct Assembler<'a> { + config: &'a JigsawConfig<'a>, + limits: &'a Config, + counted: &'a Counts, + pools: &'a Pools, + resources: &'a StructureResources, + bindings: &'a FeatureBlockBindings<'a>, + pieces: Vec<PoolPiece>, + /// Per piece: the free space it was placed in, and its depth. + states: Vec<(Rc<RefCell<Free>>, i32)>, + /// Pieces placed per counted template. + counts: Vec<i32>, + max_y: i32, + min_y: i32, +} + +/// Where a child goes: the parent's jigsaw and what it needs. +struct Slot<'s> { + source: usize, + jigsaw: &'s super::jigsaw::Jigsaw, + target_pos: [i32; 3], + free: Rc<RefCell<Free>>, + depth: i32, +} + +impl Assembler<'_> { + fn pool(&self, name: &str) -> Option<Arc<super::jigsaw::Pool>> { + let name = self.config.aliases.get(name).map_or(name, String::as_str); + self.pools.get(name, self.resources, self.bindings) + } + /// generatePiece: one child per jigsaw where one fits, from the target + /// pool unless the piece is at the last depth, else from its fallback. + fn children( + &mut self, + source: usize, + queue: &mut VecDeque<usize>, + context: &mut impl StartContext, + random: &mut impl RandomSource, + ) { + let (element, position, rotation, source_box) = { + let s = &self.pieces[source]; + (s.element.clone(), s.position, s.rotation, s.bounding_box) + }; + let (context_free, depth) = self.states[source].clone(); + let mut source_free: Option<Rc<RefCell<Free>>> = None; + for jigsaw in element.shuffled_jigsaws(self.resources, position, rotation, random) { + let Some(target) = self.pool(&jigsaw.pool) else { + continue; + }; + if target.templates.is_empty() && jigsaw.pool != "minecraft:empty" { + continue; + } + let s = step(jigsaw.front); + let target_pos: [i32; 3] = std::array::from_fn(|i| jigsaw.pos[i] + s[i]); + let free = if source_box.is_inside(target_pos) { + source_free + .get_or_insert_with(|| { + Rc::new(RefCell::new(Free { + container: source_box, + removed: Vec::new(), + })) + }) + .clone() + } else { + context_free.clone() + }; + let slot = Slot { + source, + jigsaw: &jigsaw, + target_pos, + free, + depth, + }; + if depth != self.config.max_depth + && self.process(&slot, target.raw.clone(), false, queue, context, random) + { + continue; + } + let Some(fallback) = self.pool(&target.fallback) else { + continue; + }; + let ignore = self.limits.ignore_bounds.contains(&target.fallback); + self.process(&slot, fallback.raw.clone(), ignore, queue, context, random); + } + } + /// processList: whether a piece was placed. + fn process( + &mut self, + slot: &Slot<'_>, + mut candidates: Vec<(Arc<PoolElement>, i32)>, + ignore_bounds: bool, + queue: &mut VecDeque<usize>, + context: &mut impl StartContext, + random: &mut impl RandomSource, + ) -> bool { + let jigsaw = slot.jigsaw; + let (source_rigid, source_y, source_ground) = { + let s = &self.pieces[slot.source]; + ( + s.element.projection() == Projection::Rigid, + s.bounding_box.min[1], + s.ground_level_delta, + ) + }; + let local_y = jigsaw.pos[1] - source_y; + let mut surface = None; + let (x, z) = (jigsaw.pos[0], jigsaw.pos[2]); + let mut base = + |context: &mut dyn SurfaceQuery| *surface.get_or_insert_with(|| context.surface(x, z)); + let mut total: i32 = candidates.iter().map(|c| c.1).sum(); + while !candidates.is_empty() { + let mut chosen = None; + // The first required piece still short is forced in once deep enough. + let needed = self + .limits + .pieces + .iter() + .enumerate() + .find(|(i, (_, c))| c.required.is_some_and(|r| self.counts[*i] < r)); + if let Some((need_index, (_, need))) = needed { + let need_template = &self.counted.0[need_index]; + let found = candidates.iter().position(|(e, _)| { + matches!(e.as_ref(), PoolElement::Single { template, .. } + if Arc::ptr_eq(template, need_template)) + }); + if let Some(i) = found { + if slot.depth >= (self.config.max_depth - 1).min(need.min_depth) { + chosen = Some(i); + } else { + total -= candidates[i].1; + candidates.remove(i); + } + } + } + let index = match chosen { + Some(i) => i, + None => { + if total <= 0 || candidates.is_empty() { + return false; + } + let mut weight = random.next_int_bound(total).unwrap_or(0) + 1; + let mut found = candidates.len() - 1; + for (i, (_, w)) in candidates.iter().enumerate() { + weight -= w; + if weight <= 0 { + found = i; + break; + } + } + found + } + }; + let (target, weight) = candidates[index].clone(); + if matches!(target.as_ref(), PoolElement::Empty) { + return false; + } + let counted = self.counted.of(&target); + let full = counted.is_some_and(|i| { + self.limits.pieces[i] + .1 + .max + .is_some_and(|m| self.counts[i] >= m) + }); + if full { + total -= weight; + candidates.remove(index); + continue; + } + let mut rotations = Rotation::ALL; + shuffle(&mut rotations, random); + for rotation in rotations { + let target_jigsaws = + target.shuffled_jigsaws(self.resources, [0; 3], rotation, random); + let hack_box = target.bounding_box([0; 3], rotation); + let expand_to = if self.config.use_expansion_hack && hack_box.span()[1] <= 16 { + target_jigsaws + .iter() + .map(|tj| { + let st = step(tj.front); + if !hack_box.is_inside(std::array::from_fn(|i| tj.pos[i] + st[i])) { + return 0; + } + let child = self.pool(&tj.pool); + let child_size = child.as_ref().map_or(0, |p| p.max_size()); + let fallback_size = child + .and_then(|p| self.pool(&p.fallback)) + .map_or(0, |p| p.max_size()); + child_size.max(fallback_size) + }) + .max() + .unwrap_or(0) + } else { + 0 + }; + for target_jigsaw in &target_jigsaws { + if !can_attach(jigsaw, target_jigsaw) { + continue; + } + let target_local = target_jigsaw.pos; + let raw_position: [i32; 3] = + std::array::from_fn(|i| slot.target_pos[i] - target_local[i]); + let raw_box = target.bounding_box(raw_position, rotation); + let target_rigid = target.projection() == Projection::Rigid; + let delta_y = local_y - target_local[1] + step(jigsaw.front)[1]; + let target_y = if source_rigid && target_rigid { + source_y + delta_y + } else { + base(context) - target_local[1] + }; + let offset = target_y - raw_box.min[1]; + let mut target_box = raw_box.moved([0, offset, 0]); + let target_position = + [raw_position[0], raw_position[1] + offset, raw_position[2]]; + if expand_to > 0 { + let size = (expand_to + 1).max(target_box.max[1] - target_box.min[1]); + target_box.max[1] = target_box.min[1] + size; + } + if target_box.max[1] > self.max_y || target_box.min[1] < self.min_y { + continue; + } + if !ignore_bounds && !slot.free.borrow().fits(&target_box) { + continue; + } + let target_ground = if target_rigid { + source_ground - delta_y + } else { + target.ground_level_delta() + }; + let junction_y = if source_rigid { + source_y + local_y + } else if target_rigid { + target_y + target_local[1] + } else { + base(context) + delta_y / 2 + }; + self.pieces[slot.source].junctions.push(Junction { + source_x: slot.target_pos[0], + source_ground_y: junction_y - local_y + source_ground, + source_z: slot.target_pos[2], + }); + slot.free.borrow_mut().removed.push(target_box); + self.pieces.push(PoolPiece { + element: target.clone(), + position: target_position, + ground_level_delta: target_ground, + rotation, + bounding_box: target_box, + junctions: vec![Junction { + source_x: jigsaw.pos[0], + source_ground_y: junction_y - target_local[1] + target_ground, + source_z: jigsaw.pos[2], + }], + waterlogging: self.config.waterlogging, + }); + self.states.push((slot.free.clone(), slot.depth + 1)); + if slot.depth < self.config.max_depth { + queue.push_back(self.pieces.len() - 1); + } + if let Some(i) = counted { + self.counts[i] += 1; + } + return true; + } + } + total -= weight; + candidates.remove(index); + } + false + } +} + +/// The surface height query, object-safe for the per-call cache. +trait SurfaceQuery { + fn surface(&mut self, x: i32, z: i32) -> i32; +} +impl<C: StartContext> SurfaceQuery for C { + fn surface(&mut self, x: i32, z: i32) -> i32 { + self.base_height(x, z, HeightmapType::WorldSurfaceWg) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn required_pieces_follow_javas_hash_map() { + let limits = Config::decode(&json!({"library": "repurposed", "pieces": [ + {"name": "rs:city/a", "required": 1}, + {"name": "rs:city/x", "max": 2}, + {"name": "rs:city/b", "required": 1, "min_depth": 3}, + {"name": "rs:city/c", "required": 2}, + {"name": "rs:city/a", "required": 4}]})); + let names: Vec<String> = limits.pieces.iter().map(|(id, _)| id.to_string()).collect(); + // Buckets 3, 2, 1 of the copied map's 4: c, b, a; then the capped one. + assert_eq!(names, ["rs:city/c", "rs:city/b", "rs:city/a", "rs:city/x"]); + assert_eq!( + limits.pieces[2].1.required, + Some(4), + "a later entry replaces" + ); + assert_eq!(limits.pieces[1].1.min_depth, 3); + assert_eq!(limits.library, Library::Repurposed); + } + + #[test] + fn mineshafts_go_under_the_lowest_floor_around() { + // Floor 80 at the middle (7, 7), 70 north, 90 east, 60 south, 85 west. + let floor = |x, z| match (x, z) { + (7, 7) => 80, + (7, -9) => 70, + (23, 7) => 90, + (7, 23) => 60, + (-9, 7) => 85, + other => panic!("{other:?}"), + }; + // 79, min(79, 70) - 1 = 69, 68, min(68, 60) - 1 = 59, 58; 15 under it. + assert_eq!(mineshaft_top([0, 50, 0], None, floor), 43); + assert_eq!(mineshaft_top([0, 50, 0], Some(48), floor), 48); + } + + #[test] + fn nether_land_is_found_in_the_column() { + // A column from y 0: netherrack to 40, lava 41..=45, air, a ledge 70..=72. + let (mut filled, mut solid) = (vec![false; 128], vec![false; 128]); + for y in 0..=45 { + filled[y] = true; + solid[y] = y <= 40; + } + for y in 70..=72 { + filled[y] = true; + solid[y] = true; + } + // Highest: from 88 down, a solid block with air 3 above. + assert_eq!(land_in(&filled, &solid, 0, true, true, 32, 128), 72); + // Lowest: from 33 up, a block to stand on with air 1 and 5 above; one up. + assert_eq!(land_in(&filled, &solid, 0, false, true, 32, 128), 46); + assert_eq!(land_in(&filled, &solid, 0, false, false, 32, 128), 73); + // Nothing found: the highest search stops at the sea level. + let empty = vec![false; 128]; + assert_eq!(land_in(&empty, &empty, 0, true, true, 32, 128), 32); + assert_eq!(land_in(&empty, &empty, 0, false, true, 32, 128), 32); + } + + #[test] + fn repurposed_moves_junctions_as_its_code_does() { + let template = Arc::new(StructureTemplate::empty()); + let element = Arc::new(PoolElement::Single { + template, + processors: Default::default(), + legacy: false, + projection: Projection::Rigid, + waterlogging: None, + yung: None, + enhanced_terrain: None, + }); + let piece = || PoolPiece { + bounding_box: BoundingBox::new([0; 3], [4; 3]), + ground_level_delta: 1, + element: element.clone(), + position: [0; 3], + rotation: Rotation::None, + junctions: vec![Junction { + source_x: 1, + source_ground_y: 2, + source_z: 3, + }], + waterlogging: true, + }; + let moogs = Config::decode(&json!({})); + let rs = Config::decode(&json!({"library": "repurposed"})); + let (mut a, mut b) = (piece(), piece()); + shift(&moogs, &mut a, [10, 20, 30]); + shift(&rs, &mut b, [10, 20, 30]); + assert_eq!(a.bounding_box, BoundingBox::new([10, 20, 30], [14, 24, 34])); + assert_eq!(a.position, [10, 20, 30]); + assert_eq!(a.junctions[0].source_x, 1); + // movePieceProperly sets the source X three times; Z + dz is left. + assert_eq!(b.junctions[0].source_x, 33); + assert_eq!(b.junctions[0].source_ground_y, 2); + } +} diff --git a/crates/horizon-world/src/worldgen/structure/processor.rs b/crates/horizon-world/src/worldgen/structure/processor.rs index b5bf2e22f..dd65ccd56 100644 --- a/crates/horizon-world/src/worldgen/structure/processor.rs +++ b/crates/horizon-world/src/worldgen/structure/processor.rs @@ -21,6 +21,8 @@ use horizon_core::{ use serde_json::Value; use std::{collections::BTreeSet, sync::Arc}; +mod lithostitched; + /// The draws processors make from StructurePlaceSettings' random. pub trait ProcessorRandom { fn float(&mut self) -> f32; @@ -49,6 +51,22 @@ enum PosRule { max_dist: i32, axis: Option<usize>, }, + /// `horizon:piece_origin_axis_aligned_linear_pos`: Repurposed Structures' + /// PieceOriginAxisAlignedLinearPosRuleTest, vanilla's axis-aligned chance + /// over the block's distance from the template's origin along one axis. + PieceOrigin { + min_chance: f32, + max_chance: f32, + min_dist: i32, + max_dist: i32, + axis: usize, + }, + /// `horizon:y_value`: Repurposed Structures' YValuePosRuleTest, the + /// block's world height in `min_y_value..=max_y_value`. + YValue { + min: i32, + max: i32, + }, } #[derive(Clone, Debug)] enum Modifier { @@ -56,6 +74,188 @@ enum Modifier { Clear, AppendLoot(String), AppendStatic(Compound), + /// `horizon:edit`: `remove`'s keys taken out of the block entity, then + /// `data` merged in as append_static does. The data form of the mods' + /// processors that copy a block entity and change parts of it (Moog's + /// trial spawners and vaults drop their saved state). + Edit { + remove: Vec<String>, + data: Compound, + }, +} +impl PosRule { + /// A rule's `position_predicate` (always true when missing). Types are + /// identifiers: `"always_true"` is `minecraft:always_true`, as vanilla + /// reads it. + fn decode(value: Option<&Value>) -> Result<Self, DecodeError> { + let Some(p) = value else { + return Ok(Self::Always); + }; + let kind = p["predicate_type"] + .as_str() + .unwrap_or("minecraft:always_true"); + Ok(match super::super::tags::identifier(kind)?.as_str() { + "minecraft:always_true" => Self::Always, + kind @ ("minecraft:linear_pos" | "minecraft:axis_aligned_linear_pos") => Self::Linear { + min_chance: p.get("min_chance").and_then(Value::as_f64).unwrap_or(0.) as f32, + max_chance: p.get("max_chance").and_then(Value::as_f64).unwrap_or(0.) as f32, + min_dist: p.get("min_dist").and_then(Value::as_i64).unwrap_or(0) as i32, + max_dist: p.get("max_dist").and_then(Value::as_i64).unwrap_or(0) as i32, + axis: (kind == "minecraft:axis_aligned_linear_pos").then(|| { + match p.get("axis").and_then(Value::as_str) { + Some("x") => 0, + Some("z") => 2, + _ => 1, + } + }), + }, + "horizon:piece_origin_axis_aligned_linear_pos" => { + let int = |key| p.get(key).and_then(Value::as_i64).unwrap_or(0) as i32; + let (min_dist, max_dist) = (int("min_dist"), int("max_dist")); + if min_dist >= max_dist { + return Err(DecodeError::Field("position predicate distance range")); + } + Self::PieceOrigin { + min_chance: p.get("min_chance").and_then(Value::as_f64).unwrap_or(0.) as f32, + max_chance: p.get("max_chance").and_then(Value::as_f64).unwrap_or(0.) as f32, + min_dist, + max_dist, + axis: match p.get("axis").map(|a| a.as_str()) { + Some(Some("x")) => 0, + None | Some(Some("y")) => 1, + Some(Some("z")) => 2, + _ => return Err(DecodeError::Field("position predicate axis")), + }, + } + } + "horizon:y_value" => { + let int = |key| { + p.get(key) + .and_then(Value::as_i64) + .map(|v| v as i32) + .ok_or(DecodeError::Field("position predicate y value")) + }; + let (min, max) = (int("min_y_value")?, int("max_y_value")?); + if min > max { + return Err(DecodeError::Field("position predicate y range")); + } + Self::YValue { min, max } + } + _ => return Err(DecodeError::Field("position predicate")), + }) + } + /// PosRuleTest.test for a block at `world`, `relative` in its template, + /// in a piece placed at `reference`. + fn test( + &self, + relative: [i32; 3], + world: [i32; 3], + reference: [i32; 3], + random: &mut impl RandomSource, + ) -> bool { + match *self { + Self::Always => true, + Self::Linear { + min_chance, + max_chance, + min_dist, + max_dist, + axis, + } => { + let dist = match axis { + None => (0..3).map(|i| (world[i] - reference[i]).abs()).sum::<i32>(), + Some(a) => ((world[a] - reference[a]) as f32).abs() as i32, + }; + random.next_float() + <= clamped_chance(min_chance, max_chance, min_dist, max_dist, dist) + } + Self::PieceOrigin { + min_chance, + max_chance, + min_dist, + max_dist, + axis, + } => { + let dist = (relative[axis] as f32).abs() as i32; + random.next_float() + <= clamped_chance(min_chance, max_chance, min_dist, max_dist, dist) + } + Self::YValue { min, max } => (min..=max).contains(&world[1]), + } + } +} +/// Mth.clampedLerp(min_chance, max_chance, Mth.inverseLerp(dist, min_dist, max_dist)). +fn clamped_chance( + min_chance: f32, + max_chance: f32, + min_dist: i32, + max_dist: i32, + dist: i32, +) -> f32 { + let t = (dist - min_dist) as f32 / (max_dist - min_dist) as f32; + if t < 0. { + min_chance + } else if t > 1. { + max_chance + } else { + min_chance + t * (max_chance - min_chance) + } +} +impl Modifier { + /// A rule's `block_entity_modifier` (passthrough when missing). + fn decode(value: Option<&Value>) -> Result<Self, DecodeError> { + let Some(m) = value else { + return Ok(Self::Passthrough); + }; + Ok( + match super::super::tags::identifier(m["type"].as_str().unwrap_or(""))?.as_str() { + "minecraft:passthrough" => Self::Passthrough, + "minecraft:clear" => Self::Clear, + "minecraft:append_loot" => { + Self::AppendLoot(m["loot_table"].as_str().unwrap_or_default().to_owned()) + } + "minecraft:append_static" => match json_to_nbt(&m["data"]) { + Tag::Compound(c) => Self::AppendStatic(c), + _ => Self::AppendStatic(Compound::new()), + }, + "horizon:edit" => Self::Edit { + remove: m["remove"] + .as_array() + .into_iter() + .flatten() + .map(|k| k.as_str().map(str::to_owned)) + .collect::<Option<_>>() + .ok_or(DecodeError::Field("block entity modifier keys"))?, + data: match json_to_nbt(&m["data"]) { + Tag::Compound(c) => c, + _ => Compound::new(), + }, + }, + _ => return Err(DecodeError::Field("block entity modifier")), + }, + ) + } +} +/// A block entity with `remove`'s keys taken out and `data` merged in +/// (append_static is the one with nothing to remove). +fn edited(nbt: Option<&Compound>, remove: &[String], data: &Compound) -> Compound { + let mut nbt = nbt.cloned().unwrap_or_default(); + for key in remove { + nbt.remove(&JavaString::from(key.as_str())); + } + merge(&mut nbt, data); + nbt +} +/// CompoundTag.merge: compounds in both are merged, anything else replaced. +fn merge(into: &mut Compound, other: &Compound) { + for (key, value) in other { + match (into.get_mut(key), value) { + (Some(Tag::Compound(mine)), Tag::Compound(theirs)) => merge(mine, theirs), + _ => { + into.insert(key.clone(), value.clone()); + } + } + } } #[derive(Clone, Debug)] pub struct Rule { @@ -87,10 +287,322 @@ pub enum Processor { BlockAge(f32), LavaSubmerged, BlackstoneReplace, + SpawnerMobs(Box<SpawnerMobs>), + Pick(Box<Pick>), + CountCap(Box<CountCap>), + /// `horizon:block_swap` (Lithostitched's `block_swap`): blocks by name + /// become others, keeping the properties both have. + BlockSwap(std::collections::BTreeMap<String, String>), + /// `horizon:apply_random` (Lithostitched's `apply_random`): a processor + /// list drawn by weight runs on the block. + ApplyRandom(Box<lithostitched::ApplyRandom>), + /// `horizon:condition` (Lithostitched's `condition`): `then` or `else` + /// runs on the block, by a test of it. + Condition(Box<lithostitched::Conditional>), + /// `horizon:set_block` (Lithostitched's `set_block`). + SetBlock(Box<lithostitched::SetBlock>), } #[derive(Clone, Debug, Default)] pub struct ProcessorList(pub Vec<Processor>); +/// The spawner fields `horizon:spawner_mobs` writes: its JSON key, the NBT +/// key and the default (BaseSpawner's). +const SPAWNER_FIELDS: [(&str, &str, i16); 7] = [ + ("delay", "Delay", 20), + ("min_spawn_delay", "MinSpawnDelay", 200), + ("max_spawn_delay", "MaxSpawnDelay", 800), + ("spawn_count", "SpawnCount", 4), + ("max_nearby_entities", "MaxNearbyEntities", 6), + ("required_player_range", "RequiredPlayerRange", 16), + ("spawn_range", "SpawnRange", 4), +]; + +/// `horizon:pick`, which `horizon-port-pack` writes for the mods' processors +/// that turn a placeholder block into one of several (YUNG's, written in +/// Java): a block of `block` takes the first of `picks` whose `below` is past +/// one `settings.getRandom(pos).nextFloat()`, else `default` (no float is +/// drawn when there are no picks; with no default the block stays). +#[derive(Clone, Debug)] +pub struct Pick { + block: String, + picks: Vec<(f32, Outcome)>, + default: Option<Outcome>, +} +#[derive(Clone, Debug)] +struct Outcome { + /// The state, or one of them by `nextInt(len)` (Util.getRandom), drawn + /// after the pick. + states: Vec<BlockStateId>, + /// The new block entity; the block's own stays when missing. + nbt: Option<Compound>, + /// Keys set to lists of `count` strings, each the first of `values` + /// whose bound is past a new `nextFloat`, else `default` (pot sherds). + lists: Vec<PickedList>, +} +#[derive(Clone, Debug)] +struct PickedList { + key: String, + count: usize, + values: Vec<(f32, String)>, + default: String, +} +fn below(v: &Value) -> Result<f32, DecodeError> { + v["below"] + .as_f64() + .map(|b| b as f32) + .ok_or(DecodeError::Field("pick bound")) +} +impl Pick { + fn decode( + value: &Value, + state: impl Fn(&Value) -> Result<BlockStateId, DecodeError>, + ) -> Result<Self, DecodeError> { + let outcome = |v: &Value| -> Result<Outcome, DecodeError> { + let states = match v.get("states") { + Some(list) => list + .as_array() + .ok_or(DecodeError::Field("pick states"))? + .iter() + .map(&state) + .collect::<Result<Vec<_>, _>>()?, + None => vec![state(&v["state"])?], + }; + if states.is_empty() { + return Err(DecodeError::Field("pick states")); + } + let nbt = match v.get("nbt").map(json_to_nbt) { + Some(Tag::Compound(c)) => Some(c), + Some(_) => return Err(DecodeError::Field("pick nbt")), + None => None, + }; + let text = |v: &Value| { + v.as_str() + .map(str::to_owned) + .ok_or(DecodeError::Field("pick list")) + }; + let lists = v["lists"] + .as_array() + .into_iter() + .flatten() + .map(|l| { + Ok(PickedList { + key: text(&l["key"])?, + count: l["count"].as_u64().ok_or(DecodeError::Field("pick list"))? as usize, + values: l["values"] + .as_array() + .into_iter() + .flatten() + .map(|e| Ok((below(e)?, text(&e["value"])?))) + .collect::<Result<_, DecodeError>>()?, + default: text(&l["default"])?, + }) + }) + .collect::<Result<_, DecodeError>>()?; + Ok(Outcome { states, nbt, lists }) + }; + Ok(Self { + block: value["block"] + .as_str() + .map(|b| super::super::tags::identifier(b).map(|i| i.to_string())) + .ok_or(DecodeError::Field("pick block"))??, + picks: value["picks"] + .as_array() + .into_iter() + .flatten() + .map(|p| Ok((below(p)?, outcome(p)?))) + .collect::<Result<_, DecodeError>>()?, + default: value.get("default").map(outcome).transpose()?, + }) + } + fn apply(&self, info: BlockInfo, random: &mut dyn ProcessorRandom) -> BlockInfo { + let outcome = match self.picks.is_empty() { + true => self.default.as_ref(), + false => { + let f = random.float(); + self.picks + .iter() + .find(|(b, _)| f < *b) + .map(|(_, o)| o) + .or(self.default.as_ref()) + } + }; + let Some(outcome) = outcome else { + return info; + }; + let state = match outcome.states.as_slice() { + [one] => *one, + many => many[random.int(many.len() as i32) as usize], + }; + let mut nbt = outcome.nbt.clone().or(info.nbt); + for list in &outcome.lists { + let values = (0..list.count) + .map(|_| { + let f = random.float(); + let v = list + .values + .iter() + .find(|(b, _)| f < *b) + .map_or(&list.default, |(_, v)| v); + Tag::String(v.as_str().into()) + }) + .collect(); + nbt.get_or_insert_with(Compound::new) + .insert(list.key.as_str().into(), Tag::List(values)); + } + BlockInfo { + pos: info.pos, + state, + nbt, + } + } +} + +/// `horizon:count_cap`, which `horizon-port-pack` writes for Better +/// Dungeons' chest processor (Java in the mod): of the `blocks` one +/// placement meets, the first `min` stay, the rest each stay when +/// `settings.getRandom(pos).nextFloat() <= chance` until `max` have, and the +/// others become `replacement`. The count is the placement's (the mod's +/// DungeonContext, set up per StructureTemplate.placeInWorld: one chunk of +/// the piece), shared by every such processor in it. +#[derive(Clone, Debug)] +pub struct CountCap { + blocks: BTreeSet<String>, + min: u32, + max: u32, + chance: f32, + replacement: BlockStateId, +} +impl CountCap { + fn decode( + value: &Value, + state: impl Fn(&Value) -> Result<BlockStateId, DecodeError>, + ) -> Result<Self, DecodeError> { + let count = |key: &'static str| { + value[key] + .as_u64() + .and_then(|n| u32::try_from(n).ok()) + .ok_or(DecodeError::Field(key)) + }; + Ok(Self { + blocks: value["blocks"] + .as_array() + .ok_or(DecodeError::Field("blocks"))? + .iter() + .map(|b| { + let b = b.as_str().ok_or(DecodeError::Field("blocks"))?; + Ok(super::super::tags::identifier(b)?.to_string()) + }) + .collect::<Result<_, DecodeError>>()?, + min: count("min")?, + max: count("max")?, + chance: float(value, "chance")?, + replacement: state(&value["replacement"])?, + }) + } + /// Whether a counted block stays, counting it if so. + fn keeps(&self, counted: &mut u32, roll: impl FnOnce() -> f32) -> bool { + let keep = *counted < self.min || (*counted < self.max && roll() <= self.chance); + *counted += u32::from(keep); + keep + } +} + +/// `horizon:spawner_mobs`, which `horizon-port-pack` writes for the spawner +/// randomizing processors of Moog's Structure Lib and Repurposed Structures +/// (Java code in the mods): a spawner gets one mob of a weighted list, +/// picked with `settings.getRandom(pos).nextFloat()` as theirs do, and the +/// spawner NBT they write. With no mob to pick it becomes `replacement`. +#[derive(Clone, Debug)] +pub struct SpawnerMobs { + /// Each mob's entity compound (its `id` and any NBT) and weight. + mobs: Vec<(Compound, f64)>, + /// [`SPAWNER_FIELDS`]' values. + fields: [i16; 7], + /// `custom_spawn_rules`, in `SpawnData` and the potential alike. + rules: Option<Compound>, + replacement: BlockStateId, +} +impl SpawnerMobs { + fn decode( + value: &Value, + replacement: impl FnOnce(Option<&Value>) -> Result<BlockStateId, DecodeError>, + ) -> Result<Self, DecodeError> { + let mobs = value["mobs"] + .as_array() + .ok_or(DecodeError::Field("spawner_mobs mobs"))? + .iter() + .map( + |m| match (json_to_nbt(&m["entity"]), m["weight"].as_f64()) { + (Tag::Compound(entity), Some(weight)) + if entity.contains_key(&JavaString::from("id")) => + { + Ok((entity, weight)) + } + _ => Err(DecodeError::Field("spawner_mobs mob")), + }, + ) + .collect::<Result<_, _>>()?; + let mut fields = [0; 7]; + for (field, (key, _, default)) in fields.iter_mut().zip(SPAWNER_FIELDS) { + // The mods' codecs read ints; the spawner saves them as shorts. + *field = value + .get(key) + .map_or(Some(default), |v| v.as_i64().map(|v| v as i16)) + .ok_or(DecodeError::Field("spawner_mobs field"))?; + } + let rules = match value.get("custom_spawn_rules").map(json_to_nbt) { + None => None, + Some(Tag::Compound(c)) => Some(c), + Some(_) => return Err(DecodeError::Field("spawner_mobs custom_spawn_rules")), + }; + Ok(Self { + mobs, + fields, + rules, + replacement: replacement(value.get("replacement"))?, + }) + } + /// The mob for a spawner, drawing from `random` only when there is one + /// to pick (pickWeightedRandom; Repurposed Structures' getSpawnerMob, + /// whose float product can round differently only on a boundary). + fn pick(&self, random: impl FnOnce() -> f32) -> Option<&Compound> { + let total: f64 = self.mobs.iter().map(|m| m.1).sum(); + if self.mobs.is_empty() || total <= 0. { + return None; + } + let mut pick = f64::from(random()) * total; + for (entity, weight) in &self.mobs { + pick -= weight; + if pick <= 0. { + return Some(entity); + } + } + self.mobs.last().map(|m| &m.0) + } + /// The spawner's NBT with `entity` as its only potential and next spawn. + fn nbt(&self, entity: &Compound) -> Compound { + let mut data = Compound::new(); + data.insert("entity".into(), Tag::Compound(entity.clone())); + if let Some(rules) = &self.rules { + data.insert("custom_spawn_rules".into(), Tag::Compound(rules.clone())); + } + let mut potential = Compound::new(); + potential.insert("data".into(), Tag::Compound(data.clone())); + potential.insert("weight".into(), Tag::Int(1)); + let mut nbt: Compound = SPAWNER_FIELDS + .iter() + .zip(self.fields) + .map(|((_, key, _), v)| (JavaString::from(*key), Tag::Short(v))) + .collect(); + nbt.insert( + "SpawnPotentials".into(), + Tag::List(vec![Tag::Compound(potential)]), + ); + nbt.insert("SpawnData".into(), Tag::Compound(data)); + nbt + } +} + fn holder_set( value: &Value, bindings: &FeatureBlockBindings<'_>, @@ -208,55 +720,8 @@ impl Processor { .iter() .map(|rule| { let test = |key| RuleTest::from_json(&rule[key].to_string(), bindings); - let position = match rule.get("position_predicate") { - Some(p) => match p["predicate_type"] - .as_str() - .unwrap_or("minecraft:always_true") - { - "minecraft:always_true" => PosRule::Always, - kind @ ("minecraft:linear_pos" - | "minecraft:axis_aligned_linear_pos") => PosRule::Linear { - min_chance: p - .get("min_chance") - .and_then(Value::as_f64) - .unwrap_or(0.) - as f32, - max_chance: p - .get("max_chance") - .and_then(Value::as_f64) - .unwrap_or(0.) - as f32, - min_dist: p.get("min_dist").and_then(Value::as_i64).unwrap_or(0) - as i32, - max_dist: p.get("max_dist").and_then(Value::as_i64).unwrap_or(0) - as i32, - axis: (kind == "minecraft:axis_aligned_linear_pos").then( - || match p.get("axis").and_then(Value::as_str) { - Some("x") => 0, - Some("z") => 2, - _ => 1, - }, - ), - }, - _ => return Err(DecodeError::Field("position predicate")), - }, - None => PosRule::Always, - }; - let modifier = match rule.get("block_entity_modifier") { - None => Modifier::Passthrough, - Some(m) => match m["type"].as_str().unwrap_or("") { - "minecraft:passthrough" => Modifier::Passthrough, - "minecraft:clear" => Modifier::Clear, - "minecraft:append_loot" => Modifier::AppendLoot( - m["loot_table"].as_str().unwrap_or_default().to_owned(), - ), - "minecraft:append_static" => match json_to_nbt(&m["data"]) { - Tag::Compound(c) => Modifier::AppendStatic(c), - _ => Modifier::AppendStatic(Compound::new()), - }, - _ => return Err(DecodeError::Field("block entity modifier")), - }, - }; + let position = PosRule::decode(rule.get("position_predicate"))?; + let modifier = Modifier::decode(rule.get("block_entity_modifier"))?; Ok(Rule { input: test("input_predicate")?, location: test("location_predicate")?, @@ -267,6 +732,37 @@ impl Processor { }) .collect::<Result<_, DecodeError>>()?, ), + "horizon:spawner_mobs" => { + Self::SpawnerMobs(Box::new(SpawnerMobs::decode(value, |v| match v { + Some(v) => state(v), + None => Ok(resources.registry.default_state("minecraft:air").unwrap()), + })?)) + } + "horizon:pick" => Self::Pick(Box::new(Pick::decode(value, state)?)), + "horizon:block_swap" => Self::BlockSwap( + value["blocks"] + .as_object() + .ok_or(DecodeError::Field("blocks"))? + .iter() + .map(|(from, to)| { + let to = to.as_str().ok_or(DecodeError::Field("blocks"))?; + Ok(( + super::super::tags::identifier(from)?.to_string(), + super::super::tags::identifier(to)?.to_string(), + )) + }) + .collect::<Result<_, DecodeError>>()?, + ), + "horizon:count_cap" => Self::CountCap(Box::new(CountCap::decode(value, state)?)), + "horizon:apply_random" => Self::ApplyRandom(Box::new( + lithostitched::ApplyRandom::decode(value, resources, bindings)?, + )), + "horizon:set_block" => { + Self::SetBlock(Box::new(lithostitched::SetBlock::decode(value, state)?)) + } + "horizon:condition" => Self::Condition(Box::new(lithostitched::Conditional::decode( + value, resources, bindings, + )?)), _ => return Err(DecodeError::Field("structure processor type")), }) } @@ -320,6 +816,8 @@ pub struct PlaceSettings<'a> { /// StructurePlaceSettings.ignoreEntities / finalizeEntities. pub ignore_entities: bool, pub finalize_entities: bool, + /// The blocks `horizon:count_cap` kept in this placement. + counted: u32, } impl<'a> PlaceSettings<'a> { pub fn new(rotation: Rotation) -> Self { @@ -334,6 +832,7 @@ impl<'a> PlaceSettings<'a> { known_shape: false, ignore_entities: false, finalize_entities: false, + counted: 0, } } pub fn with_processors(mut self, list: &'a ProcessorList) -> Self { @@ -355,6 +854,30 @@ impl<'a> PlaceSettings<'a> { } } +/// Block.withPropertiesOf: `block`'s default state with each property of +/// `from` it has too (by name, when the value is one of its own). None for +/// a block the registry does not know (the mod keeps the block then). +fn with_properties_of( + registry: &crate::registry::BlockRegistry, + block: &str, + from: BlockStateId, +) -> Option<BlockStateId> { + let default = registry.default_state(block)?; + let mut state = registry.state(default)?.clone(); + for (key, value) in ®istry.state(from)?.properties { + if let Some(own) = state.properties.get(key) + && own != value + { + let mut changed = state.clone(); + changed.properties.insert(key.clone(), value.clone()); + if registry.id(&changed).is_some() { + state = changed; + } + } + } + registry.id(&state) +} + /// BlackstoneReplaceProcessor's block map. fn blackstone(name: &str) -> Option<&'static str> { Some(match name.strip_prefix("minecraft:")? { @@ -376,11 +899,37 @@ fn blackstone(name: &str) -> Option<&'static str> { }) } type ProcessResult = Result<Option<BlockInfo>, PlacementQueryError>; +/// The processors of a list in turn, until one removes the block. +#[allow(clippy::too_many_arguments)] +fn process_list<W: PlacementContext>( + list: &ProcessorList, + world: &mut W, + resources: &StructureResources, + position: [i32; 3], + reference: [i32; 3], + relative: [i32; 3], + info: BlockInfo, + settings: &mut PlaceSettings<'_>, +) -> ProcessResult { + let mut current = info; + for processor in &list.0 { + match process_block( + processor, world, resources, position, reference, relative, current, settings, + )? { + Some(next) => current = next, + None => return Ok(None), + } + } + Ok(Some(current)) +} +/// StructureProcessor.processBlock: `position` is the piece's, `reference` +/// the structure's pivot and `relative` the block's in the template. #[allow(clippy::too_many_arguments)] fn process_block<W: PlacementContext>( processor: &Processor, world: &mut W, resources: &StructureResources, + position: [i32; 3], reference: [i32; 3], relative: [i32; 3], info: BlockInfo, @@ -469,42 +1018,9 @@ fn process_block<W: PlacementContext>( if !rule.location.test(located, info.pos, &mut random) { continue; } - let position_ok = match &rule.position { - PosRule::Always => true, - PosRule::Linear { - min_chance, - max_chance, - min_dist, - max_dist, - axis, - } => { - let dist = match axis { - None => (0..3) - .map(|i| (info.pos[i] - reference[i]).abs()) - .sum::<i32>(), - Some(a) => { - let d: [f32; 3] = std::array::from_fn(|i| { - if i == *a { - ((info.pos[i] - reference[i]) as f32).abs() - } else { - 0. - } - }); - (d[0] + d[1] + d[2]) as i32 - } - }; - let roll = random.next_float(); - let t = (dist - min_dist) as f32 / (max_dist - min_dist) as f32; - let chance = if t < 0. { - *min_chance - } else if t > 1. { - *max_chance - } else { - min_chance + t * (max_chance - min_chance) - }; - roll <= chance - } - }; + let position_ok = rule + .position + .test(relative, info.pos, reference, &mut random); if !position_ok { continue; } @@ -517,10 +1033,9 @@ fn process_block<W: PlacementContext>( nbt.insert("LootTableSeed".into(), Tag::Long(random.next_long())); Some(nbt) } - Modifier::AppendStatic(data) => { - let mut nbt = info.nbt.clone().unwrap_or_default(); - nbt.extend(data.clone()); - Some(nbt) + Modifier::AppendStatic(data) => Some(edited(info.nbt.as_ref(), &[], data)), + Modifier::Edit { remove, data } => { + Some(edited(info.nbt.as_ref(), remove, data)) } }; return Ok(Some(BlockInfo { @@ -532,6 +1047,90 @@ fn process_block<W: PlacementContext>( Some(info) } Processor::Capped { .. } => Some(info), + Processor::BlockSwap(blocks) => { + let swapped = blocks + .get(name(info.state)) + .and_then(|to| with_properties_of(registry, to, info.state)); + Some(match swapped { + Some(state) => BlockInfo { state, ..info }, + None => info, + }) + } + Processor::CountCap(cap) => match cap.blocks.contains(name(info.state)) { + true => { + let pos = info.pos; + let mut counted = settings.counted; + let keep = cap.keeps(&mut counted, || settings.with_random(pos, |r| r.float())); + settings.counted = counted; + Some(match keep { + true => info, + false => BlockInfo { + pos, + state: cap.replacement, + nbt: None, + }, + }) + } + false => Some(info), + }, + Processor::ApplyRandom(apply) => { + let seed = world.world_seed()?; + let mut random = apply.random.create(seed, position, reference, info.pos); + match apply.pick(&mut random) { + Some(list) => { + return process_list( + list, world, resources, position, reference, relative, info, settings, + ); + } + None => Some(info), + } + } + Processor::SetBlock(set) => { + let seed = world.world_seed()?; + let state = set.state(seed, position, reference, info.pos); + let state = match set.preserves() { + true => registry + .state(state) + .and_then(|s| with_properties_of(registry, &s.name, info.state)) + .unwrap_or(state), + false => state, + }; + Some(BlockInfo { state, ..info }) + } + Processor::Condition(c) => { + let seed = world.world_seed()?; + let mut random = c.random.create(seed, position, reference, info.pos); + let location = world.block_state(info.pos)?; + let passed = c + .condition + .test(info.state, location, registry, &mut random); + let list = if passed { &c.then } else { &c.otherwise }; + return process_list( + list, world, resources, position, reference, relative, info, settings, + ); + } + Processor::Pick(pick) => match name(info.state) == pick.block { + true => Some(settings.with_random(info.pos, |r| pick.apply(info, r))), + false => Some(info), + }, + Processor::SpawnerMobs(spawner) => { + if name(info.state) != "minecraft:spawner" { + return Ok(Some(info)); + } + let pos = info.pos; + let entity = spawner.pick(|| settings.with_random(pos, |r| r.float())); + Some(match entity { + Some(entity) => BlockInfo { + nbt: Some(spawner.nbt(entity)), + ..info + }, + None => BlockInfo { + pos, + state: spawner.replacement, + nbt: None, + }, + }) + } }) } fn block_age( @@ -631,6 +1230,7 @@ fn process_infos<W: PlacementContext>( reference: [i32; 3], settings: &mut PlaceSettings<'_>, ) -> Result<Vec<BlockInfo>, PlacementQueryError> { + settings.counted = 0; let whole = settings .processors .iter() @@ -657,7 +1257,7 @@ fn process_infos<W: PlacementContext>( for processor in processors { let Some(value) = current.take() else { break }; current = process_block( - processor, world, resources, reference, info.pos, value, settings, + processor, world, resources, position, reference, info.pos, value, settings, )?; } if let Some(value) = current { @@ -701,6 +1301,7 @@ fn process_infos<W: PlacementContext>( delegate, world, resources, + position, reference, original[index].pos, before.clone(), @@ -1021,3 +1622,365 @@ fn update_shape_at_edge<W: PlacementContext>( } Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + fn spawner(value: Value) -> SpawnerMobs { + SpawnerMobs::decode(&value, |v| { + assert_eq!(v, Some(&json!({"id": "minecraft:cobblestone"}))); + Ok(BlockStateId(7)) + }) + .unwrap() + } + fn id(entity: Option<&Compound>) -> Option<String> { + match entity?.get(&JavaString::from("id")) { + Some(Tag::String(s)) => Some(s.to_string_lossy()), + _ => None, + } + } + + #[test] + fn spawner_mobs_pick_by_weight() { + let s = spawner(json!({ + "mobs": [ + {"entity": {"id": "minecraft:zombie"}, "weight": 1}, + {"entity": {"id": "minecraft:husk"}, "weight": 3}], + "replacement": {"id": "minecraft:cobblestone"}})); + // pick = nextFloat * 4, less each weight in turn, until <= 0. + for (r, mob) in [ + (0., "zombie"), + (0.25, "zombie"), + (0.2501, "husk"), + (0.999, "husk"), + ] { + assert_eq!(id(s.pick(|| r)), Some(format!("minecraft:{mob}")), "{r}"); + } + let empty = spawner(json!({"mobs": [], "replacement": {"id": "minecraft:cobblestone"}})); + assert_eq!(empty.pick(|| unreachable!()), None); + assert_eq!(empty.replacement, BlockStateId(7)); + } + + #[test] + fn spawner_mobs_write_the_mods_nbt() { + let s = spawner(json!({ + "mobs": [{"entity": {"id": "minecraft:skeleton", "Health": 30}, "weight": 2}], + "delay": 10, "spawn_count": 2, + "custom_spawn_rules": {"block_light_limit": {"min_inclusive": 0, "max_inclusive": 7}}, + "replacement": {"id": "minecraft:cobblestone"}})); + let nbt = s.nbt(s.pick(|| 0.5).unwrap()); + let short = |k: &str| nbt.get(&JavaString::from(k)).cloned(); + for (k, v) in [ + ("Delay", 10), + ("MinSpawnDelay", 200), + ("MaxSpawnDelay", 800), + ("SpawnCount", 2), + ("MaxNearbyEntities", 6), + ("RequiredPlayerRange", 16), + ("SpawnRange", 4), + ] { + assert_eq!(short(k), Some(Tag::Short(v)), "{k}"); + } + let Some(Tag::Compound(data)) = short("SpawnData") else { + panic!("SpawnData") + }; + assert!(data.contains_key(&JavaString::from("custom_spawn_rules"))); + let Some(Tag::Compound(entity)) = data.get(&JavaString::from("entity")) else { + panic!("entity") + }; + assert_eq!(entity.get(&JavaString::from("Health")), Some(&Tag::Int(30))); + let Some(Tag::List(potentials)) = short("SpawnPotentials") else { + panic!("SpawnPotentials") + }; + let Tag::Compound(potential) = &potentials[0] else { + panic!("potential") + }; + assert_eq!( + potential.get(&JavaString::from("weight")), + Some(&Tag::Int(1)) + ); + assert_eq!( + potential.get(&JavaString::from("data")), + Some(&Tag::Compound(data.clone())) + ); + } + + #[test] + fn spawner_mobs_refuse_a_mob_without_an_id() { + let bad = json!({"mobs": [{"entity": {}, "weight": 1}]}); + assert!(SpawnerMobs::decode(&bad, |_| Ok(BlockStateId(0))).is_err()); + } + + #[test] + fn repurposed_position_rules() { + let decode = |v: Value| PosRule::decode(Some(&v)); + // Over 3..4 blocks from the template's origin along y, 0 to 1. + let rule = decode( + json!({"predicate_type": "horizon:piece_origin_axis_aligned_linear_pos", + "axis": "y", "min_chance": 0, "max_chance": 1, "min_dist": 3, "max_dist": 4}), + ) + .unwrap(); + let mut random = LegacyRandom::new(1); + let world = [100, 64, -100]; + // At or under min_dist the chance is 0, a roll of 0 aside; past max_dist it is 1. + assert!(!rule.test([9, 2, 9], world, world, &mut random)); + assert!(rule.test([0, -5, 0], world, world, &mut random)); + // The world position and the piece's do not count. + assert!(rule.test([0, 4, 0], [0, 0, 0], [50, 50, 50], &mut random)); + // Repurposed refuses an empty distance range and an unknown axis. + for bad in [ + json!({"predicate_type": "horizon:piece_origin_axis_aligned_linear_pos"}), + json!({"predicate_type": "horizon:piece_origin_axis_aligned_linear_pos", + "min_dist": 0, "max_dist": 1, "axis": "w"}), + json!({"predicate_type": "horizon:y_value", "min_y_value": 5, "max_y_value": 4}), + json!({"predicate_type": "horizon:y_value", "min_y_value": 5}), + ] { + assert!(decode(bad.clone()).is_err(), "{bad}"); + } + let y = decode( + json!({"predicate_type": "horizon:y_value", "min_y_value": 0, "max_y_value": 31}), + ) + .unwrap(); + assert!(y.test([0; 3], [7, 31, 7], [0; 3], &mut random)); + assert!(y.test([0, 99, 0], [7, 0, 7], [0; 3], &mut random)); + assert!(!y.test([0; 3], [7, 32, 7], [0; 3], &mut random)); + assert!(!y.test([0; 3], [7, -1, 7], [0; 3], &mut random)); + } + + #[test] + fn rule_parts_read_ids_without_a_namespace() { + // CTOV writes "always_true"; vanilla reads it as minecraft:always_true. + for kind in ["always_true", "minecraft:always_true"] { + let p = json!({"predicate_type": kind}); + assert!( + matches!(PosRule::decode(Some(&p)), Ok(PosRule::Always)), + "{kind}" + ); + } + let linear = json!({"predicate_type": "axis_aligned_linear_pos", "axis": "z", + "min_chance": 0.1, "max_dist": 8}); + assert!(matches!( + PosRule::decode(Some(&linear)), + Ok(PosRule::Linear { + axis: Some(2), + max_dist: 8, + .. + }) + )); + assert!(PosRule::decode(Some(&json!({"predicate_type": "x:unknown"}))).is_err()); + assert!(matches!(PosRule::decode(None), Ok(PosRule::Always))); + let loot = json!({"type": "append_loot", "loot_table": "minecraft:chests/x"}); + assert!( + matches!(Modifier::decode(Some(&loot)), Ok(Modifier::AppendLoot(t)) if t == "minecraft:chests/x") + ); + assert!(matches!( + Modifier::decode(Some(&json!({"type": "clear"}))), + Ok(Modifier::Clear) + )); + assert!(Modifier::decode(Some(&json!({"type": ""}))).is_err()); + } + + #[test] + fn block_entity_edits_merge_deep_and_drop_keys() { + let nbt = |v: Value| match json_to_nbt(&v) { + Tag::Compound(c) => c, + _ => unreachable!(), + }; + let vault = nbt(json!({"id": "minecraft:vault", + "config": {"loot_table": "x:old", "key_item": {"id": "minecraft:trial_key"}}, + "server_data": {"state_updating_resumes_at": 533162741}})); + // append_static merges compounds the way CompoundTag.merge does. + let Ok(Modifier::AppendStatic(data)) = Modifier::decode(Some(&json!({ + "type": "append_static", "data": {"config": {"loot_table": "x:new"}}}))) + else { + panic!("append_static") + }; + let merged = edited(Some(&vault), &[], &data); + assert_eq!( + merged, + nbt(json!({"id": "minecraft:vault", + "config": {"loot_table": "x:new", "key_item": {"id": "minecraft:trial_key"}}, + "server_data": {"state_updating_resumes_at": 533162741}})) + ); + let Ok(Modifier::Edit { remove, data }) = Modifier::decode(Some(&json!({ + "type": "horizon:edit", "remove": ["server_data"], + "data": {"config": {"loot_table": "x:new"}}}))) + else { + panic!("horizon:edit") + }; + assert_eq!( + edited(Some(&vault), &remove, &data), + nbt(json!({"id": "minecraft:vault", + "config": {"loot_table": "x:new", "key_item": {"id": "minecraft:trial_key"}}})) + ); + assert_eq!( + edited(None, &remove, &data), + nbt(json!({"config": {"loot_table": "x:new"}})) + ); + let bad = json!({"type": "horizon:edit", "remove": [1], "data": {}}); + assert!(Modifier::decode(Some(&bad)).is_err()); + } + + /// Scripted draws: floats in turn, then ints. + struct Draws(Vec<f32>, Vec<i32>); + impl ProcessorRandom for Draws { + fn float(&mut self) -> f32 { + self.0.remove(0) + } + fn int(&mut self, bound: i32) -> i32 { + let i = self.1.remove(0); + assert!(i < bound); + i + } + fn long(&mut self) -> i64 { + unreachable!() + } + } + + #[test] + fn picks_follow_yungs_cascade() { + // States by name: sand 1, sandstone 2, the four stairs 10..13. + let id = |v: &Value| { + Ok(BlockStateId(match v["Name"].as_str() { + Some("minecraft:sand") => 1, + Some("minecraft:sandstone") => 2, + Some("minecraft:suspicious_sand") => 3, + _ => { + 10 + ["north", "east", "south", "west"] + .iter() + .position(|f| v["Properties"]["facing"] == *f) + .unwrap_or(9) as u32 + } + })) + }; + let stairs: Vec<Value> = ["north", "east", "south", "west"] + .iter() + .map(|f| json!({"Name": "minecraft:sandstone_stairs", "Properties": {"facing": f}})) + .collect(); + let pick = Pick::decode( + &json!({"block": "red_stained_glass", "picks": [ + {"below": 0.01, "state": {"Name": "minecraft:suspicious_sand"}, + "nbt": {"LootTable": "minecraft:archaeology/desert_pyramid"}}, + {"below": 0.3, "state": {"Name": "minecraft:sand"}}, + {"below": 0.75, "state": {"Name": "minecraft:sandstone"}}], + "default": {"states": stairs}}), + id, + ) + .unwrap(); + assert_eq!(pick.block, "minecraft:red_stained_glass"); + let glass = || BlockInfo { + pos: [1, 2, 3], + state: BlockStateId(0), + nbt: Some(Compound::new()), + }; + let run = |f: f32, i: Vec<i32>| pick.apply(glass(), &mut Draws(vec![f], i)); + let sus = run(0.005, vec![]); + assert_eq!(sus.state, BlockStateId(3)); + assert!( + sus.nbt + .unwrap() + .contains_key(&JavaString::from("LootTable")) + ); + assert_eq!(run(0.01, vec![]).state, BlockStateId(1)); + assert_eq!(run(0.2999, vec![]).nbt, Some(Compound::new())); + assert_eq!(run(0.3, vec![]).state, BlockStateId(2)); + // The stairs' facing: Plane.HORIZONTAL[nextInt(4)] after the pick. + assert_eq!(run(0.75, vec![2]).state, BlockStateId(12)); + assert_eq!(run(0.99, vec![1]).state, BlockStateId(11)); + + let pot = Pick::decode( + &json!({"block": "minecraft:decorated_pot", "default": { + "state": {"Name": "minecraft:sand"}, + "lists": [{"key": "sherds", "count": 4, "default": "minecraft:brick", + "values": [{"below": 0.05, "value": "minecraft:archer_pottery_sherd"}, + {"below": 0.1, "value": "minecraft:miner_pottery_sherd"}]}]}}), + id, + ) + .unwrap(); + // No picks: no float for them; one float per sherd. + let out = pot.apply( + BlockInfo { + nbt: None, + ..glass() + }, + &mut Draws(vec![0.5, 0.01, 0.07, 0.1], vec![]), + ); + let Some(Tag::List(sherds)) = out.nbt.unwrap().get(&JavaString::from("sherds")).cloned() + else { + panic!("sherds") + }; + let names: Vec<String> = sherds + .iter() + .map(|t| match t { + Tag::String(s) => s.to_string_lossy(), + _ => String::new(), + }) + .collect(); + assert_eq!( + names, + [ + "minecraft:brick", + "minecraft:archer_pottery_sherd", + "minecraft:miner_pottery_sherd", + "minecraft:brick" + ] + ); + assert!(Pick::decode(&json!({"block": "a", "default": {"states": []}}), id).is_err()); + } + + #[test] + fn count_caps_keep_the_first_then_some() { + let cap = CountCap::decode( + &json!({"blocks": ["chest", "minecraft:trapped_chest"], "min": 1, "max": 2, + "chance": 0.2, "replacement": {"id": "minecraft:cave_air"}}), + |_| Ok(BlockStateId(5)), + ) + .unwrap(); + assert!(cap.blocks.contains("minecraft:chest")); + let mut counted = 0; + // The first stays without a roll. + assert!(cap.keeps(&mut counted, || unreachable!())); + // Then each stays on nextFloat <= 0.2, until two have. + assert!(!cap.keeps(&mut counted, || 0.21)); + assert!(cap.keeps(&mut counted, || 0.2)); + assert!(!cap.keeps(&mut counted, || unreachable!())); + assert_eq!(counted, 2); + assert!( + CountCap::decode(&json!({"blocks": [], "min": -1}), |_| Ok(BlockStateId(5))).is_err() + ); + } + + #[test] + fn block_swaps_keep_the_properties_both_blocks_have() { + let row = |name: &str, props: Value| json!([name, false, false, "", {"id": name, "properties": props}]); + let facts = json!({"defaults": {"a:log": 1, "a:stairs": 4, "a:stone": 6}, + "states": [ + row("a:log", json!({"axis": "x"})), row("a:log", json!({"axis": "y"})), + row("a:stairs", json!({"facing": "east", "half": "top"})), + row("a:stairs", json!({"facing": "east", "half": "bottom"})), + row("a:stairs", json!({"facing": "north", "half": "bottom"})), + row("a:stairs", json!({"facing": "north", "half": "top"})), + ["a:stone", false, false, "", "a:stone"]]}); + let registry = + crate::registry::BlockRegistry::from_facts_json(facts.to_string().as_bytes()).unwrap(); + // facing and half carried over; the default where `from` has none. + assert_eq!( + with_properties_of(®istry, "a:stairs", BlockStateId(2)), + Some(BlockStateId(2)) + ); + assert_eq!( + with_properties_of(®istry, "a:stairs", BlockStateId(0)), + Some(BlockStateId(4)) + ); + assert_eq!( + with_properties_of(®istry, "a:stone", BlockStateId(5)), + Some(BlockStateId(6)) + ); + assert_eq!( + with_properties_of(®istry, "a:missing", BlockStateId(0)), + None + ); + } +} diff --git a/crates/horizon-world/src/worldgen/structure/processor/lithostitched.rs b/crates/horizon-world/src/worldgen/structure/processor/lithostitched.rs new file mode 100644 index 000000000..f0bb1c6ba --- /dev/null +++ b/crates/horizon-world/src/worldgen/structure/processor/lithostitched.rs @@ -0,0 +1,434 @@ +//! Lithostitched's processors that pick processors, carried as data +//! (`horizon:apply_random` and `horizon:condition`): one runs a processor +//! list drawn by weight, the other one list or another by a test of the +//! block. Both draw from Lithostitched's RandomSettings: a random seeded from +//! the world seed and a name, at the block, the piece or the structure. +//! `horizon:set_block` is its `set_block`, which the conditions mostly run. +use super::{Processor, ProcessorList}; +use crate::{ + registry::{BlockRegistry, BlockStateId}, + worldgen::{DecodeError, block_feature::FeatureBlockBindings, structure::StructureResources}, +}; +use horizon_core::{ + position::BlockPos, + random::{LegacyRandom, PositionalRandom, RandomSource}, +}; +use serde_json::Value; +use std::{collections::BTreeSet, sync::Arc}; + +/// RandomSettings: where the random is taken, and the name salting it. +#[derive(Clone, Debug, PartialEq)] +pub struct RandomSettings { + mode: Mode, + /// ResourceLocation.hashCode of the name (`lithostitched:default`). + salt: i32, +} +#[derive(Clone, Copy, Debug, PartialEq)] +enum Mode { + Block, + Piece, + Structure, +} +impl RandomSettings { + /// A mode alone, or `{"mode": ..., "name": ...}`; per block when unset. + fn decode(v: Option<&Value>) -> Result<Self, DecodeError> { + let mode = Mode::decode; + let (mode, name) = match v { + None => (Mode::Block, "lithostitched:default"), + Some(Value::String(m)) => (mode(Some(m))?, "lithostitched:default"), + Some(o) => ( + mode(o["mode"].as_str())?, + o["name"] + .as_str() + .ok_or(DecodeError::Field("random name"))?, + ), + }; + let (namespace, path) = name.split_once(':').unwrap_or(("minecraft", name)); + Ok(Self { + mode, + salt: java_hash(namespace) + .wrapping_mul(31) + .wrapping_add(java_hash(path)), + }) + } + /// RandomSource.create(seed + hash).forkPositional().at(the chosen + /// position): the piece's position, the structure's pivot or the block. + pub(super) fn create( + &self, + seed: i64, + piece: [i32; 3], + pivot: [i32; 3], + block: [i32; 3], + ) -> LegacyRandom { + let [x, y, z] = self.mode.select(piece, pivot, block); + LegacyRandom::new(seed.wrapping_add(i64::from(self.salt))) + .fork_positional() + .at(BlockPos::new(x, y, z)) + } +} +impl Mode { + fn decode(m: Option<&str>) -> Result<Self, DecodeError> { + match m { + None | Some("per_block") => Ok(Self::Block), + Some("per_piece") => Ok(Self::Piece), + Some("per_structure") => Ok(Self::Structure), + _ => Err(DecodeError::Field("random mode")), + } + } + fn select(self, piece: [i32; 3], pivot: [i32; 3], block: [i32; 3]) -> [i32; 3] { + match self { + Self::Block => block, + Self::Piece => piece, + Self::Structure => pivot, + } + } +} + +/// SetBlockProcessor with a simple or weighted state provider and no block +/// entity change: the block becomes the state, keeping the properties it +/// shares with the old one unless `preserve_state` is off. +#[derive(Clone, Debug)] +pub struct SetBlock { + /// The states and their weights; one state alone is a simple provider, + /// which draws nothing. + states: Vec<(BlockStateId, i32)>, + weighted: bool, + preserve: bool, + mode: Mode, +} +impl SetBlock { + pub(super) fn decode( + v: &Value, + state: impl Fn(&Value) -> Result<BlockStateId, DecodeError>, + ) -> Result<Self, DecodeError> { + let provider = &v["state_provider"]; + let (states, weighted) = match provider["type"].as_str() { + Some("minecraft:simple_state_provider") => { + (vec![(state(&provider["state"])?, 1)], false) + } + Some("minecraft:weighted_state_provider") => ( + provider["entries"] + .as_array() + .ok_or(DecodeError::Field("set_block entries"))? + .iter() + .map(|e| { + let weight = e["weight"] + .as_i64() + .ok_or(DecodeError::Field("set_block weight"))?; + Ok((state(&e["data"])?, weight as i32)) + }) + .collect::<Result<_, DecodeError>>()?, + true, + ), + _ => return Err(DecodeError::Field("set_block state_provider")), + }; + if states.is_empty() { + return Err(DecodeError::Field("set_block entries")); + } + Ok(Self { + states, + weighted, + preserve: v["preserve_state"].as_bool().unwrap_or(true), + mode: Mode::decode(v["random_mode"].as_str())?, + }) + } + /// The state to set, before `preserve`: drawn with + /// RandomSource.create(seed).forkPositional().at(the chosen position). + pub(super) fn state( + &self, + seed: i64, + piece: [i32; 3], + pivot: [i32; 3], + block: [i32; 3], + ) -> BlockStateId { + if !self.weighted { + return self.states[0].0; + } + let [x, y, z] = self.mode.select(piece, pivot, block); + let mut random = LegacyRandom::new(seed) + .fork_positional() + .at(BlockPos::new(x, y, z)); + let total: i32 = self.states.iter().map(|s| s.1).sum(); + let mut value = random.next_int_bound(total.max(1)).unwrap_or(0); + for (state, weight) in &self.states { + if value < *weight { + return *state; + } + value -= weight; + } + self.states[0].0 + } + pub(super) fn preserves(&self) -> bool { + self.preserve + } +} + +/// String.hashCode over UTF-16 units. +fn java_hash(s: &str) -> i32 { + s.encode_utf16() + .fold(0i32, |h, c| h.wrapping_mul(31).wrapping_add(i32::from(c))) +} + +fn list( + v: &Value, + resources: &StructureResources, + bindings: &FeatureBlockBindings<'_>, +) -> Result<ProcessorList, DecodeError> { + ProcessorList::decode(&v.to_string(), resources, bindings) +} + +/// ApplyRandomStructureProcessor: a weighted list of processor lists, one +/// drawn per block with `nextInt(total)`. +#[derive(Clone, Debug)] +pub struct ApplyRandom { + pub(super) random: RandomSettings, + lists: Vec<(ProcessorList, i32)>, + total: i32, +} +impl ApplyRandom { + pub(super) fn decode( + v: &Value, + resources: &StructureResources, + bindings: &FeatureBlockBindings<'_>, + ) -> Result<Self, DecodeError> { + let lists = v["processor_lists"] + .as_array() + .ok_or(DecodeError::Field("apply_random processor_lists"))? + .iter() + .map(|e| { + let weight = e["weight"] + .as_i64() + .ok_or(DecodeError::Field("apply_random weight"))?; + Ok((list(&e["data"], resources, bindings)?, weight as i32)) + }) + .collect::<Result<Vec<_>, DecodeError>>()?; + Ok(Self { + random: RandomSettings::decode(v.get("mode"))?, + total: lists.iter().map(|l| l.1).sum(), + lists, + }) + } + /// WeightedList.getRandom: none when the weights add up to 0. + pub(super) fn pick(&self, random: &mut impl RandomSource) -> Option<&ProcessorList> { + if self.total <= 0 { + return None; + } + let mut value = random.next_int_bound(self.total).unwrap_or(0); + for (list, weight) in &self.lists { + if value < *weight { + return Some(list); + } + value -= weight; + } + None + } +} + +/// ConditionProcessor: `then` when the condition holds for the block, else +/// `else`. +#[derive(Clone, Debug)] +pub struct Conditional { + pub(super) random: RandomSettings, + pub(super) condition: Condition, + pub(super) then: ProcessorList, + pub(super) otherwise: ProcessorList, +} +impl Conditional { + pub(super) fn decode( + v: &Value, + resources: &StructureResources, + bindings: &FeatureBlockBindings<'_>, + ) -> Result<Self, DecodeError> { + // A processor alone or a list of them. + let processors = |key: &str| -> Result<ProcessorList, DecodeError> { + match v.get(key) { + None => Ok(ProcessorList::default()), + Some(Value::Object(p)) if p.contains_key("processor_type") => Ok(ProcessorList( + vec![Processor::decode(&v[key], resources, bindings)?], + )), + Some(other) => list(other, resources, bindings), + } + }; + Ok(Self { + random: RandomSettings::decode(v.get("random_mode"))?, + condition: Condition::decode(&v["if_true"], bindings)?, + then: processors("then")?, + otherwise: processors("else")?, + }) + } +} + +/// Lithostitched's processor conditions the game runs. +#[derive(Clone, Debug)] +pub(super) enum Condition { + True, + AllOf(Vec<Condition>), + AnyOf(Vec<Condition>), + Not(Box<Condition>), + RandomChance(f32), + /// The block placed (`input`) or the one in the world (`location`) is + /// one of these, with these property values. + MatchingBlocks { + blocks: Arc<BTreeSet<BlockStateId>>, + properties: Vec<(String, String)>, + location: bool, + }, +} +impl Condition { + fn decode(v: &Value, bindings: &FeatureBlockBindings<'_>) -> Result<Self, DecodeError> { + let all = |a: &Value| -> Result<Vec<Condition>, DecodeError> { + a.as_array() + .ok_or(DecodeError::Field("processor conditions"))? + .iter() + .map(|c| Self::decode(c, bindings)) + .collect() + }; + if v.is_array() { + return Ok(Self::AllOf(all(v)?)); + } + let kind = v["type"].as_str().unwrap_or_default(); + Ok(match kind.strip_prefix("lithostitched:").unwrap_or(kind) { + "true" => Self::True, + "all_of" => Self::AllOf(all(&v["conditions"])?), + "any_of" => Self::AnyOf(all(&v["conditions"])?), + "not" => Self::Not(Box::new(Self::decode(&v["condition"], bindings)?)), + "random_chance" => Self::RandomChance(super::float(v, "chance")?), + "matching_blocks" => Self::MatchingBlocks { + blocks: super::holder_set(&v["blocks"], bindings)?, + properties: match v.get("properties") { + None => Vec::new(), + Some(p) => p + .as_object() + .ok_or(DecodeError::Field("matching_blocks properties"))? + .iter() + .map(|(k, x)| { + let x = x + .as_str() + .ok_or(DecodeError::Field("matching_blocks property"))?; + Ok((k.clone(), x.to_owned())) + }) + .collect::<Result<_, DecodeError>>()?, + }, + location: match v["match_type"].as_str() { + None | Some("input") => false, + Some("location") => true, + _ => return Err(DecodeError::Field("matching_blocks match_type")), + }, + }, + _ => return Err(DecodeError::Field("processor condition type")), + }) + } + /// ProcessorCondition.test with the block placed and the one there. + pub(super) fn test( + &self, + input: BlockStateId, + location: BlockStateId, + registry: &BlockRegistry, + random: &mut impl RandomSource, + ) -> bool { + match self { + Self::True => true, + Self::AllOf(all) => all + .iter() + .all(|c| c.test(input, location, registry, random)), + Self::AnyOf(any) => any + .iter() + .any(|c| c.test(input, location, registry, random)), + Self::Not(c) => !c.test(input, location, registry, random), + Self::RandomChance(chance) => random.next_float() < *chance, + Self::MatchingBlocks { + blocks, + properties, + location: at_location, + } => { + let state = if *at_location { location } else { input }; + blocks.contains(&state) + && properties.iter().all(|(k, x)| { + registry + .state(state) + .and_then(|s| s.properties.get(k)) + .is_some_and(|v| v == x) + }) + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn random_settings_salt_and_position() { + // ResourceLocation.hashCode: 31 * namespace + path. + let default = RandomSettings::decode(Some(&"per_piece".into())).unwrap(); + assert_eq!( + default.salt, + java_hash("lithostitched") + .wrapping_mul(31) + .wrapping_add(java_hash("default")) + ); + assert_eq!(java_hash("default"), 1544803905); + let (seed, piece, pivot, block) = (12345, [16, 64, -32], [8, 70, 8], [17, 65, -30]); + let mut ours = default.create(seed, piece, pivot, block); + let mut theirs = LegacyRandom::new(seed + i64::from(default.salt)) + .fork_positional() + .at(BlockPos::new(16, 64, -32)); + assert_eq!(ours.next_long(), theirs.next_long()); + let named: Value = serde_json::json!({"mode": "per_structure", "name": "v:roof"}); + let named = RandomSettings::decode(Some(&named)).unwrap(); + assert_eq!(named.mode, Mode::Structure); + assert_eq!( + named.salt, + java_hash("v") + .wrapping_mul(31) + .wrapping_add(java_hash("roof")) + ); + } + + #[test] + fn apply_random_draws_by_weight() { + let apply = ApplyRandom { + random: RandomSettings { + mode: Mode::Block, + salt: 0, + }, + lists: vec![ + (ProcessorList(vec![Processor::Nop]), 3), + (ProcessorList::default(), 1), + ], + total: 4, + }; + let mut ours = LegacyRandom::new(9); + let mut theirs = LegacyRandom::new(9); + for _ in 0..32 { + let draw = theirs.next_int_bound(4).unwrap(); + let picked = apply.pick(&mut ours).unwrap(); + assert_eq!(picked.0.len(), usize::from(draw < 3)); + } + } + + #[test] + fn set_block_draws_from_the_unsalted_seed() { + let (a, b) = (BlockStateId(1), BlockStateId(2)); + let set = SetBlock::decode( + &serde_json::json!({"state_provider": {"type": "minecraft:weighted_state_provider", + "entries": [{"weight": 1, "data": 1}, {"weight": 2, "data": 2}]}, + "random_mode": "per_piece"}), + |v| Ok(BlockStateId(v.as_u64().unwrap() as u32)), + ) + .unwrap(); + assert!(set.preserves()); + for x in 0..16 { + let mut java = LegacyRandom::new(77) + .fork_positional() + .at(BlockPos::new(x, 5, 3)); + let want = if java.next_int_bound(3).unwrap() < 1 { + a + } else { + b + }; + assert_eq!(set.state(77, [x, 5, 3], [0; 3], [9, 9, 9]), want); + } + } +} diff --git a/crates/horizon-world/src/worldgen/structure/start.rs b/crates/horizon-world/src/worldgen/structure/start.rs index 1b443eba5..ef360372e 100644 --- a/crates/horizon-world/src/worldgen/structure/start.rs +++ b/crates/horizon-world/src/worldgen/structure/start.rs @@ -55,6 +55,13 @@ pub struct JigsawDef { aliases: Vec<Value>, padding: [i32; 2], waterlogging: bool, + /// A carried mod's start rules (`"horizon:start_checks"`). + checks: super::start_checks::StartChecks, + /// Laid out by YUNG's API's assembler (`"horizon:yungsapi"`). + yung: Option<super::yung::Config>, + /// Laid out by Repurposed Structures' and Moog's piece-limited + /// assembler (`"horizon:piece_limited"`). + piece_limited: Option<super::piece_limited::Config>, } #[derive(Debug)] pub enum StructureKind { @@ -95,6 +102,12 @@ 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, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum Reduction { @@ -129,6 +142,113 @@ pub struct StructureSet { frequency: f32, reduction: Reduction, exclusion: Option<(String, i32)>, + /// A carried mod's placement rules: no start nearer the world origin + /// than this many blocks (`"horizon:min_distance_from_world_origin"`), + /// and none within `chunk_count` chunks of any of several sets + /// (`"horizon:exclusion_zone"`: `other_sets`, `chunk_count`). + min_distance: Option<i64>, + avoid: Option<(Vec<String>, i32)>, + /// `"horizon:ring_sections"`: starts only in odd rings around the origin. + rings: Option<RingSections>, + /// `"horizon:avoid_biomes"`: no start with one of these biomes near. + avoid_biomes: Option<AvoidBiomes>, +} + +/// YUNG's Better Strongholds' placement (BetterStrongholdsPlacement +/// .isPlacementChunk): the world is cut into rings `thickness` chunks wide, +/// the first one starting `distance_to_first_ring` chunks out; a start is +/// placed only in the odd ones, and in none past `max_section`. +/// +/// ```json +/// "horizon:ring_sections": {"distance_to_first_ring": 80, "thickness": 96, "max_section": 5} +/// ``` +#[derive(Debug, Clone, PartialEq, Eq)] +struct RingSections { + first: i32, + thickness: i32, + max: Option<i32>, +} +impl RingSections { + fn decode(v: &Value) -> Self { + let int = |k: &str| v[k].as_i64().map(|n| n as i32); + Self { + first: int("distance_to_first_ring").unwrap_or(0), + thickness: int("thickness").unwrap_or(1).max(1), + max: int("max_section"), + } + } + fn allows(&self, x: i32, z: i32) -> bool { + // Java int arithmetic: the squares wrap past 46,340 chunks, and the + // square root of a negative int is NaN, which casts to 0. + let squared = x.wrapping_mul(x).wrapping_add(z.wrapping_mul(z)); + let distance = f64::from(squared).sqrt() as i32; + let section = distance.wrapping_add(self.thickness - self.first) / self.thickness; + !self.max.is_some_and(|max| section > max) && section % 2 == 1 + } +} + +/// YUNG's Better Desert and Jungle Temples' placement: no start when a +/// biome of the set is found around the chunk's middle block at height `y` +/// (BiomeSource.findBiomeHorizontal with `radius` blocks and `step` quarts +/// between samples, closest first). +/// +/// ```json +/// "horizon:avoid_biomes": {"biomes": ["#minecraft:is_river", "#minecraft:is_ocean"], +/// "radius": 48, "step": 2, "y": 120} +/// ``` +#[derive(Debug)] +struct AvoidBiomes { + biomes: BTreeSet<BiomeId>, + radius: i32, + step: i32, + y: i32, +} +impl AvoidBiomes { + fn decode( + v: &Value, + biome_set: &dyn Fn(&Value) -> Result<BTreeSet<BiomeId>, DecodeError>, + ) -> Result<Self, DecodeError> { + let mut biomes = BTreeSet::new(); + match &v["biomes"] { + Value::Array(list) => { + for entry in list { + biomes.extend(biome_set(entry)?); + } + } + other => biomes = biome_set(other)?, + } + let int = |k: &str, default: i32| v[k].as_i64().map_or(default, |n| n as i32); + Ok(Self { + biomes, + radius: int("radius", 0), + step: int("step", 1).max(1), + y: int("y", 0), + }) + } + fn near(&self, chunk: ChunkPos, mut biome: impl FnMut([i32; 3]) -> BiomeId) -> bool { + // ChunkPos.getMiddleBlockPosition, then QuartPos.fromBlock. + let (cx, cz) = ((chunk.x * 16 + 8) >> 2, (chunk.z * 16 + 8) >> 2); + let (radius, y) = (self.radius >> 2, self.y >> 2); + let mut r = 0; + while r <= radius { + let mut dz = -r; + while dz <= r { + let z_edge = dz.abs() == r; + let mut dx = -r; + while dx <= r { + if (z_edge || dx.abs() == r) + && self.biomes.contains(&biome([cx + dx, y, cz + dz])) + { + return true; + } + dx += self.step; + } + dz += self.step; + } + r += self.step; + } + false + } } fn heightmap(name: &str) -> Option<HeightmapType> { @@ -174,8 +294,19 @@ impl StructureRegistry { biome_set: &dyn Fn(&Value) -> Result<BTreeSet<BiomeId>, DecodeError>, ) -> Result<Self, DecodeError> { let mut defs = Vec::new(); + let (mut types, mut disables) = (Vec::new(), BTreeSet::new()); for (name, json) in structures { let v: Value = serde_json::from_str(json)?; + types.push(v["type"].as_str().unwrap_or_default().to_owned()); + // YUNG's mods turn their vanilla counterparts off (the + // DisableVanilla*Mixin on ChunkGenerator.tryGenerateStructure). + disables.extend( + v["horizon:disables"] + .as_array() + .into_iter() + .flatten() + .filter_map(|t| Some(t.as_str()?.to_owned())), + ); let kind = match v["type"].as_str().unwrap_or_default() { "minecraft:jigsaw" => StructureKind::Jigsaw(JigsawDef { start_pool: v["start_pool"].as_str().unwrap_or_default().to_owned(), @@ -193,6 +324,11 @@ impl StructureRegistry { aliases: v["pool_aliases"].as_array().cloned().unwrap_or_default(), padding: pair(v.get("dimension_padding"), "bottom", "top", 0), waterlogging: v["liquid_settings"].as_str() != Some("ignore_waterlogging"), + checks: super::start_checks::StartChecks::decode(v.get("horizon:start_checks")), + yung: v.get("horizon:yungsapi").map(super::yung::Config::decode), + piece_limited: v + .get("horizon:piece_limited") + .map(super::piece_limited::Config::decode), }), "minecraft:buried_treasure" => StructureKind::BuriedTreasure, "minecraft:igloo" => StructureKind::Igloo, @@ -242,8 +378,16 @@ 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, }); } + for (def, kind) in defs.iter_mut().zip(&types) { + def.disabled = disables.contains(kind); + } let index = |name: &str| defs.iter().position(|d| d.name == name); let mut out = Vec::new(); for (name, json) in sets { @@ -291,6 +435,25 @@ impl StructureRegistry { z["chunk_count"].as_i64().unwrap_or(0) as i32, ) }), + min_distance: p + .get("horizon:min_distance_from_world_origin") + .and_then(Value::as_i64), + avoid: p.get("horizon:exclusion_zone").map(|z| { + ( + z["other_sets"] + .as_array() + .into_iter() + .flatten() + .filter_map(|s| Some(s.as_str()?.to_owned())) + .collect(), + z["chunk_count"].as_i64().unwrap_or(0) as i32, + ) + }), + rings: p.get("horizon:ring_sections").map(RingSections::decode), + avoid_biomes: p + .get("horizon:avoid_biomes") + .map(|a| AvoidBiomes::decode(a, biome_set)) + .transpose()?, }); } Ok(Self { @@ -397,26 +560,50 @@ impl StructureRegistry { if !placed { return false; } + if set.rings.as_ref().is_some_and(|r| !r.allows(x, z)) { + return false; + } + // AdvancedRandomSpread.isPlacementChunk, in blocks from the chunk's corner. + if let Some(d) = set.min_distance { + let (bx, bz) = (i64::from(x) * 16, i64::from(z) * 16); + if bx * bx + bz * bz < d * d { + return false; + } + } + if let Some((others, range)) = &set.avoid { + if others + .iter() + .any(|other| self.has_start_in_range(other, seed, x, z, *range, rings)) + { + return false; + } + } if set.frequency < 1. && !reduce(set.reduction, seed, set.salt, x, z, set.frequency) { return false; } match &set.exclusion { - Some((other, range)) => { - let Some(other) = self.sets.iter().find(|s| s.name == *other) else { - return true; - }; - for tx in x - range..=x + range { - for tz in z - range..=z + range { - if self.is_structure_chunk(other, seed, tx, tz, rings) { - return false; - } - } - } - true - } + Some((other, range)) => !self.has_start_in_range(other, seed, x, z, *range, rings), None => true, } } + /// ChunkGeneratorStructureState.hasStructureChunkInRange: whether set + /// `other` places a structure within `range` chunks of (`x`, `z`). + fn has_start_in_range( + &self, + other: &str, + seed: i64, + x: i32, + z: i32, + range: i32, + rings: &Rings, + ) -> bool { + let Some(other) = self.sets.iter().find(|s| s.name == other) else { + return false; + }; + (x - range..=x + range).any(|tx| { + (z - range..=z + range).any(|tz| self.is_structure_chunk(other, seed, tx, tz, rings)) + }) + } } impl StructureSet { /// The random spread's spacing in chunks (`None` for rings). @@ -527,6 +714,10 @@ pub trait StartContext: JigsawContext { fn generation(&self) -> WorldGenerationContext; /// Noise biome at a quart position (caching resolver, no zoom). fn noise_biome(&mut self, quart: [i32; 3]) -> BiomeId; + /// Noise biomes of `quarts` in order through one resolver, as + /// BiomeSource.findBiomeHorizontal samples: each climate lookup starts + /// from the previous result, which a tie keeps. + fn noise_biomes(&mut self, quarts: &[[i32; 3]]) -> Vec<BiomeId>; fn set_valid_biomes(&mut self, biomes: &BTreeSet<BiomeId>); } @@ -547,6 +738,16 @@ pub fn create_starts( if !registry.is_structure_chunk(set, seed, chunk.x, chunk.z, rings) { continue; } + // Part of the set's placement in YUNG's mods; checked here, where the + // biome source is at hand, so another set's exclusion zone naming + // this one does not see it. + if set + .avoid_biomes + .as_ref() + .is_some_and(|a| a.near(chunk, |q| context.noise_biome(q))) + { + continue; + } if set.structures.len() == 1 { if let Some(start) = try_generate( registry, @@ -596,6 +797,9 @@ fn try_generate( chunk: ChunkPos, ) -> Option<StructureStart> { let def = ®istry.structures[structure]; + if def.disabled { + return None; + } context.set_valid_biomes(&def.biomes); let seed = context.seed(); let mut random = WorldgenRandom::new(LegacyRandom::new(0)); @@ -606,6 +810,12 @@ fn try_generate( .start_height .sample(&mut random, context.generation()) .ok()?; + if !jigsaw + .checks + .pass(context, &def.biomes, chunk, height, jigsaw.project_to) + { + return None; + } let start = [chunk.x * 16, height, chunk.z * 16]; let aliases = resolve_aliases(&jigsaw.aliases, start, seed); let config = JigsawConfig { @@ -619,16 +829,40 @@ fn try_generate( padding: jigsaw.padding, waterlogging: jigsaw.waterlogging, }; - let (position, mut pieces) = assemble( - &config, - context, - ®istry.pools, - resources, - bindings, - &mut random, - start, - )?; - if jigsaw.max_depth <= 0 { + let (position, mut pieces) = match (&jigsaw.yung, &jigsaw.piece_limited) { + (_, Some(limits)) => super::piece_limited::assemble( + &config, + limits, + context, + ®istry.pools, + resources, + bindings, + chunk, + start, + )?, + (Some(yung), None) => super::yung::assemble( + &config, + yung, + context, + ®istry.pools, + resources, + bindings, + &mut random, + start, + )?, + (None, None) => assemble( + &config, + context, + ®istry.pools, + resources, + bindings, + &mut random, + start, + )?, + }; + // Vanilla's JigsawPlacement keeps no piece at size 0; the + // piece-limited assembler keeps the start piece. + if jigsaw.max_depth <= 0 && jigsaw.piece_limited.is_none() { pieces.clear(); } ( @@ -878,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(), @@ -950,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 = ®istry.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; } @@ -998,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. @@ -1064,3 +1319,106 @@ pub fn place_in_chunk<W: FeatureWorld + PlacementContext, R: RandomSource + 'sta } Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + fn registry(structures: &[(&str, Value)], sets: &[(&str, Value)]) -> StructureRegistry { + let text = |l: &[(&str, Value)]| { + l.iter() + .map(|(k, v)| ((*k).to_owned(), v.to_string())) + .collect::<BTreeMap<_, _>>() + }; + // Biome tags: `#t:one` is biome 1, `#t:two` biome 2. + StructureRegistry::new(&text(structures), &text(sets), BTreeMap::new(), &|v| { + Ok(match v.as_str() { + Some("#t:one") => BTreeSet::from([BiomeId(1)]), + Some("#t:two") => BTreeSet::from([BiomeId(2)]), + _ => BTreeSet::new(), + }) + }) + .unwrap() + } + + #[test] + fn a_structure_turns_its_vanilla_counterpart_off() { + let r = registry( + &[ + ( + "minecraft:pyramid", + json!({"type": "minecraft:desert_pyramid"}), + ), + ("minecraft:igloo", json!({"type": "minecraft:igloo"})), + ( + "yung:temple", + json!({"type": "minecraft:swamp_hut", "horizon:disables": ["minecraft:desert_pyramid"]}), + ), + ], + &[], + ); + let disabled: Vec<_> = r + .structures + .iter() + .map(|d| (d.name.as_str(), d.disabled)) + .collect(); + assert_eq!( + disabled, + [ + ("minecraft:igloo", false), + ("minecraft:pyramid", true), + ("yung:temple", false) + ] + ); + } + + #[test] + fn ring_sections_keep_the_odd_rings() { + let rings = RingSections::decode( + &json!({"distance_to_first_ring": 80, "thickness": 96, "max_section": 3}), + ); + // (d + 16) / 96: 0 inside 80 chunks, 1 to 175, 2 to 271, 3 to 367. + assert!(!rings.allows(79, 0)); + assert!(rings.allows(80, 0)); + assert!(rings.allows(0, -175)); + assert!(!rings.allows(176, 0)); + assert!(rings.allows(272, 0)); + assert!(!rings.allows(464, 0), "section 5 is past max_section 3"); + // Java's int square wraps: 50,000² + 0 is negative, its root NaN, 0. + assert!(!rings.allows(50_000, 0)); + let open = RingSections::decode(&json!({"distance_to_first_ring": 80, "thickness": 96})); + assert!(open.allows(464, 0)); + } + + #[test] + fn avoid_biomes_samples_around_the_middle_block() { + let r = registry( + &[], + &[( + "yung:temples", + json!({"structures": [], "placement": {"type": "minecraft:random_spread", + "spacing": 24, "separation": 8, "salt": 1, + "horizon:avoid_biomes": {"biomes": ["#t:one", "#t:two"], + "radius": 48, "step": 2, "y": 120}}}), + )], + ); + let avoid = r.sets[0].avoid_biomes.as_ref().unwrap(); + assert_eq!(avoid.biomes, BTreeSet::from([BiomeId(1), BiomeId(2)])); + // Chunk (1, -1): middle block (24, 120, -8), quart (6, 30, -2). + let mut asked = Vec::new(); + assert!(!avoid.near(ChunkPos::new(1, -1), |q| { + asked.push(q); + BiomeId(0) + })); + assert_eq!(asked[0], [6, 30, -2]); + // Every even offset within 12 quarts, once: 13 x 13. + assert_eq!(asked.len(), 169); + assert!(asked.contains(&[6 - 12, 30, -2 + 12])); + assert!(!asked.contains(&[7, 30, -2])); + // An ocean on the edge of the square is found. + assert!(avoid.near(ChunkPos::new(1, -1), |q| { + BiomeId(if q == [18, 30, 10] { 2 } else { 0 }) + })); + } +} diff --git a/crates/horizon-world/src/worldgen/structure/start_checks.rs b/crates/horizon-world/src/worldgen/structure/start_checks.rs new file mode 100644 index 000000000..f182706a8 --- /dev/null +++ b/crates/horizon-world/src/worldgen/structure/start_checks.rs @@ -0,0 +1,462 @@ +//! Start rules a carried structure mod adds on top of vanilla's jigsaw. +//! +//! Repurposed Structures and Moog's Structure Lib give their jigsaw +//! structures checks that run before the pieces are laid out: every biome +//! in a radius must be one the structure allows, the start column may not +//! end in liquid, and the terrain in a radius must lie within a height range. +//! Carry (`horizon-port-pack`) maps those structures to `minecraft:jigsaw` +//! and writes the checks under `"horizon:start_checks"`; vanilla data never +//! has the key, so vanilla structures are unchanged. +//! +//! ```json +//! "horizon:start_checks": { +//! "biome_radius": 2, // chunks; every noise biome must be allowed +//! "liquid": "top", // "top": the column's highest block; "lowest": the block above min_y +//! "terrain_radius": 1, // chunks, with terrain_range and/or min_y, max_y +//! "terrain_range": 15, +//! "min_y": 60, +//! "max_y": 200, +//! "repurposed": "city_nether" // or "shipwreck_nether", "stronghold_end": below +//! } +//! ``` +//! +//! `repurposed` is the extra check of one of Repurposed Structures' jigsaw +//! types, after the others: its Nether city needs open air above the lava sea +//! in the chunks around, its Nether shipwreck open air or lava around the +//! start, and its End stronghold land around the chunk. +use crate::{heightmap::HeightmapType, worldgen::biome::BiomeId}; +use horizon_core::position::ChunkPos; +use serde_json::Value; +use std::collections::BTreeSet; + +/// Which block of the start column must not be a liquid. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum LiquidCheck { + /// The highest non-air block of the noise column (Moog's). + Top, + /// The block above the column's bottom (Repurposed Structures: its loop + /// keeps the last block it reads, not the first solid one). + Lowest, +} + +/// The extra start check of a Repurposed Structures jigsaw type. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Repurposed { + /// CityNetherStructure: air from sea level + 20 to + 45 (or the top of + /// the noise) at the corner of each of the 8 chunks around. + CityNether, + /// ShipwreckNetherStructure: no solid block at 5×5 columns 8 apart + /// around the chunk corner, from the start height up 30 in steps of 5. + ShipwreckNether, + /// StrongholdEndStructure: land at least 45 above the bottom 35 and 70 + /// blocks away in each direction and at the chunk corner. + StrongholdEnd, +} + +/// The checks of one structure; all empty for vanilla's. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct StartChecks { + biome_radius: Option<i32>, + liquid: Option<LiquidCheck>, + terrain_radius: Option<i32>, + terrain_range: Option<i32>, + min_y: Option<i32>, + max_y: Option<i32>, + repurposed: Option<Repurposed>, +} + +/// The column and biome queries the checks need. +pub trait CheckQueries { + /// ChunkGenerator.getFirstFreeHeight. + fn first_free_height(&mut self, x: i32, z: i32, kind: HeightmapType) -> i32; + /// The noise column at (`x`, `z`) as (not air, blocks motion) per block + /// from the noise's bottom up: in a noise column a block that is not air + /// and does not block motion is a liquid. + fn liquid_column(&mut self, x: i32, z: i32) -> (Vec<bool>, Vec<bool>); + fn noise_biome(&mut self, quart: [i32; 3]) -> BiomeId; + fn sea_level(&self) -> i32; + /// The noise's bottom, where the columns start. + fn bottom(&self) -> i32; +} + +impl<T: super::start::StartContext> CheckQueries for T { + fn first_free_height(&mut self, x: i32, z: i32, kind: HeightmapType) -> i32 { + self.base_height(x, z, kind) + } + fn liquid_column(&mut self, x: i32, z: i32) -> (Vec<bool>, Vec<bool>) { + let mut columns = self.base_columns( + x, + z, + &[HeightmapType::WorldSurfaceWg, HeightmapType::OceanFloorWg], + ); + let solid = columns.pop().unwrap_or_default(); + (columns.pop().unwrap_or_default(), solid) + } + fn noise_biome(&mut self, quart: [i32; 3]) -> BiomeId { + super::start::StartContext::noise_biome(self, quart) + } + fn sea_level(&self) -> i32 { + super::start::StartContext::sea_level(self) + } + fn bottom(&self) -> i32 { + self.min_y() + } +} + +impl StartChecks { + /// `"horizon:start_checks"` of a structure, or none. + pub fn decode(value: Option<&Value>) -> Self { + let Some(v) = value else { + return Self::default(); + }; + let int = |k: &str| v.get(k).and_then(Value::as_i64).map(|n| n as i32); + Self { + biome_radius: int("biome_radius"), + liquid: match v.get("liquid").and_then(Value::as_str) { + Some("top") => Some(LiquidCheck::Top), + Some("lowest") => Some(LiquidCheck::Lowest), + _ => None, + }, + terrain_radius: int("terrain_radius"), + terrain_range: int("terrain_range"), + min_y: int("min_y"), + max_y: int("max_y"), + repurposed: match v.get("repurposed").and_then(Value::as_str) { + Some("city_nether") => Some(Repurposed::CityNether), + Some("shipwreck_nether") => Some(Repurposed::ShipwreckNether), + Some("stronghold_end") => Some(Repurposed::StrongholdEnd), + _ => None, + }, + } + } + + /// GenericJigsawStructure.extraSpawningChecks: whether the structure may + /// start in `chunk` at `y` (the sampled start height). + pub fn pass( + &self, + queries: &mut impl CheckQueries, + biomes: &BTreeSet<BiomeId>, + chunk: ChunkPos, + y: i32, + project_to: Option<HeightmapType>, + ) -> bool { + let (x0, z0) = (chunk.x * 16, chunk.z * 16); + if let Some(radius) = self.biome_radius { + let mut section_y = y; + if let Some(kind) = project_to { + section_y += queries.first_free_height(x0 + 7, z0 + 7, kind) - 1; + } + for cx in chunk.x - radius..=chunk.x + radius { + for cz in chunk.z - radius..=chunk.z + radius { + if !biomes.contains(&queries.noise_biome([cx << 2, section_y >> 2, cz << 2])) { + return false; + } + } + } + } + if let Some(check) = self.liquid { + let (filled, solid) = queries.liquid_column(x0 + 7, z0 + 7); + // From the top down to the block above the bottom, as the mods loop. + let index = match check { + LiquidCheck::Top => (1..filled.len()).rev().find(|&i| filled[i]), + LiquidCheck::Lowest => Some(1).filter(|&i| i < filled.len()), + }; + if index.is_some_and(|i| filled[i] && !solid[i]) { + return false; + } + } + if let Some(radius) = self + .terrain_radius + .filter(|_| self.terrain_range.is_some() || self.min_y.is_some()) + { + let kind = project_to.unwrap_or(HeightmapType::WorldSurfaceWg); + let (mut highest, mut lowest) = (i32::MIN, i32::MAX); + for cx in chunk.x - radius..=chunk.x + radius { + for cz in chunk.z - radius..=chunk.z + radius { + let height = queries.first_free_height((cx << 4) + 7, (cz << 4) + 7, kind); + highest = highest.max(height); + lowest = lowest.min(height); + // Both bounds test the lowest height so far, as the mods do. + if self.min_y.is_some_and(|m| lowest < m) + || self.max_y.is_some_and(|m| lowest > m) + { + return false; + } + } + } + if self + .terrain_range + .is_some_and(|range| highest - lowest > range) + { + return false; + } + } + match self.repurposed { + Some(check) => repurposed(check, queries, chunk, y), + None => true, + } + } +} + +/// [`Repurposed`]'s checks. A noise column reads air past its ends; its +/// length is the noise's depth (GeneralUtils.getMaxTerrainLimit). +fn repurposed(check: Repurposed, queries: &mut impl CheckQueries, chunk: ChunkPos, y: i32) -> bool { + let (x0, z0) = (chunk.x * 16, chunk.z * 16); + let bottom = queries.bottom(); + let at = |column: &[bool], y: i32| { + usize::try_from(y - bottom) + .ok() + .and_then(|i| column.get(i).copied()) + .unwrap_or(false) + }; + match check { + Repurposed::CityNether => { + let sea = queries.sea_level(); + for cx in chunk.x - 1..=chunk.x + 1 { + for cz in chunk.z - 1..=chunk.z + 1 { + if (cx, cz) == (chunk.x, chunk.z) { + continue; + } + let (filled, _) = queries.liquid_column(cx << 4, cz << 4); + let top = (bottom + filled.len() as i32).min(sea + 45); + if (sea + 20..top).any(|y| at(&filled, y)) { + return false; + } + } + } + true + } + Repurposed::ShipwreckNether => { + for dx in (-16..=16).step_by(8) { + for dz in (-16..=16).step_by(8) { + let (filled, solid) = queries.liquid_column(x0 + dx, z0 + dz); + // Not air and no fluid: in a noise column, a solid block. + if (0..=30) + .step_by(5) + .any(|dy| at(&filled, y + dy) && at(&solid, y + dy)) + { + return false; + } + } + } + true + } + Repurposed::StrongholdEnd => { + let (filled, _) = queries.liquid_column(x0, z0); + let min_land = (bottom + filled.len() as i32).min(bottom + 45); + // getHeightAt: the lowest height so far, less one at every sample. + let mut land = i32::MAX; + let mut sample = |x: i32, z: i32, land: &mut i32| { + let h = queries.first_free_height(x, z, HeightmapType::WorldSurfaceWg); + *land = (*land).min(h) - 1; + *land >= min_land + }; + for distance in [70, 35] { + // Direction.Plane.HORIZONTAL: north, east, south, west. + for (dx, dz) in [(0, -1), (1, 0), (0, 1), (-1, 0)] { + if !sample(x0 + dx * distance, z0 + dz * distance, &mut land) { + return false; + } + } + } + sample(x0, z0, &mut land) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + use std::collections::BTreeMap; + + /// Heights per (x, z), a liquid column, one biome per quart column. + #[derive(Default)] + struct Fake { + heights: BTreeMap<(i32, i32), i32>, + column: Vec<char>, + biomes: BTreeMap<(i32, i32), u32>, + asked: Vec<[i32; 3]>, + /// Columns by (x, z), instead of `column`, from y 0. + columns: BTreeMap<(i32, i32), Vec<char>>, + } + impl CheckQueries for Fake { + fn first_free_height(&mut self, x: i32, z: i32, _: HeightmapType) -> i32 { + self.heights.get(&(x, z)).copied().unwrap_or(64) + } + fn liquid_column(&mut self, x: i32, z: i32) -> (Vec<bool>, Vec<bool>) { + // '.' air, '#' stone, '~' water, from the bottom up. + let column = self.columns.get(&(x, z)).unwrap_or(&self.column); + ( + column.iter().map(|&c| c != '.').collect(), + column.iter().map(|&c| c == '#').collect(), + ) + } + fn sea_level(&self) -> i32 { + 32 + } + fn bottom(&self) -> i32 { + 0 + } + fn noise_biome(&mut self, quart: [i32; 3]) -> BiomeId { + self.asked.push(quart); + BiomeId(self.biomes.get(&(quart[0], quart[2])).copied().unwrap_or(1)) + } + } + fn checks(v: Value) -> StartChecks { + StartChecks::decode(Some(&v)) + } + const CHUNK: ChunkPos = ChunkPos { x: 2, z: -1 }; + + #[test] + fn vanilla_structures_pass_everything() { + let mut fake = Fake { + column: "#~~.".chars().collect(), + ..Fake::default() + }; + let none = StartChecks::decode(None); + assert_eq!(none, StartChecks::default()); + assert!(none.pass(&mut fake, &BTreeSet::new(), CHUNK, 0, None)); + assert!(fake.asked.is_empty()); + } + + #[test] + fn every_biome_in_the_radius_must_be_allowed() { + let allowed = BTreeSet::from([BiomeId(1)]); + let mut fake = Fake::default(); + fake.heights.insert((39, -9), 70); + let c = checks(json!({"biome_radius": 1})); + assert!(c.pass( + &mut fake, + &allowed, + CHUNK, + 10, + Some(HeightmapType::WorldSurfaceWg) + )); + // Section y: start height plus the projected height minus one, in quarts. + assert_eq!(fake.asked.len(), 9); + assert!(fake.asked.iter().all(|q| q[1] == (10 + 70 - 1) >> 2)); + assert_eq!(fake.asked[0], [4, 19, -8]); + fake.biomes.insert((12, 0), 2); // chunk (3, 0), the radius' corner + assert!(!c.pass(&mut fake, &allowed, CHUNK, 10, None)); + } + + #[test] + fn liquid_checks_read_the_mods_block() { + let top = checks(json!({"liquid": "top"})); + let lowest = checks(json!({"liquid": "lowest"})); + let none = BTreeSet::new(); + let mut ocean = Fake { + column: "##~~~..".chars().collect(), + ..Fake::default() + }; + assert!(!top.pass(&mut ocean, &none, CHUNK, 0, None)); + assert!(lowest.pass(&mut ocean, &none, CHUNK, 0, None)); + let mut land = Fake { + column: "#~###..".chars().collect(), + ..Fake::default() + }; + assert!(top.pass(&mut land, &none, CHUNK, 0, None)); + assert!(!lowest.pass(&mut land, &none, CHUNK, 0, None)); + // The bottom block itself is never read; an empty column passes. + let mut empty = Fake { + column: "~....".chars().collect(), + ..Fake::default() + }; + assert!(top.pass(&mut empty, &none, CHUNK, 0, None)); + } + + #[test] + fn terrain_must_lie_within_its_range() { + let none = BTreeSet::new(); + let mut fake = Fake::default(); + // Chunk centres around (2, -1): x 23, 39, 55; z -25, -9, 7. + fake.heights.insert((23, -25), 60); + fake.heights.insert((55, 7), 75); + let range = |r: i32| checks(json!({"terrain_radius": 1, "terrain_range": r})); + assert!(range(15).pass(&mut fake, &none, CHUNK, 0, None)); + assert!(!range(14).pass(&mut fake, &none, CHUNK, 0, None)); + // The radius alone, or with only max_y, checks nothing. + assert!( + checks(json!({"terrain_radius": 1, "max_y": 0})).pass(&mut fake, &none, CHUNK, 0, None) + ); + let bounds = + |min: i32, max: i32| checks(json!({"terrain_radius": 1, "min_y": min, "max_y": max})); + assert!(bounds(60, 64).pass(&mut fake, &none, CHUNK, 0, None)); + assert!(!bounds(61, 100).pass(&mut fake, &none, CHUNK, 0, None)); + // max_y is compared with the lowest height so far: 75 never fails it. + assert!(bounds(0, 64).pass(&mut fake, &none, CHUNK, 0, None)); + assert!(!bounds(0, 59).pass(&mut fake, &none, CHUNK, 0, None)); + } + + /// A column of `n` blocks: `fill` up to `top` (exclusive), air above. + fn column(n: usize, top: usize, fill: char) -> Vec<char> { + (0..n).map(|y| if y < top { fill } else { '.' }).collect() + } + + #[test] + fn repurposed_city_needs_air_above_the_lava_sea_around() { + let none = BTreeSet::new(); + let city = checks(json!({"repurposed": "city_nether"})); + // Sea 32: air needed from 52 to 76 (128 deep), in the 8 chunks around. + let mut fake = Fake { + column: column(128, 52, '#'), + ..Fake::default() + }; + // The chunk itself is not read. + fake.columns.insert((32, -16), column(128, 100, '#')); + assert!(city.pass(&mut fake, &none, CHUNK, 33, None)); + fake.columns.insert((48, 0), column(128, 53, '#')); + assert!(!city.pass(&mut fake, &none, CHUNK, 33, None)); + // Above sea + 45 nothing is read. + fake.columns.insert((48, 0), { + let mut c = column(128, 52, '#'); + c[77] = '#'; + c + }); + assert!(city.pass(&mut fake, &none, CHUNK, 33, None)); + } + + #[test] + fn repurposed_shipwreck_needs_no_solid_block_around() { + let none = BTreeSet::new(); + let wreck = checks(json!({"repurposed": "shipwreck_nether"})); + // Lava up to 40, air above: the samples at 28..=58 meet only lava and air. + let mut fake = Fake { + column: column(128, 40, '~'), + ..Fake::default() + }; + assert!(wreck.pass(&mut fake, &none, CHUNK, 28, None)); + // Netherrack at y 43, between samples (28 + 15, + 20 are 43, 48). + let mut rock = column(128, 40, '~'); + rock[43] = '#'; + fake.columns.insert((32 - 16, -16 + 16), rock.clone()); + assert!(!wreck.pass(&mut fake, &none, CHUNK, 28, None)); + rock[43] = '.'; + rock[44] = '#'; + fake.columns.insert((16, 0), rock); + assert!(wreck.pass(&mut fake, &none, CHUNK, 28, None)); + } + + #[test] + fn repurposed_end_stronghold_needs_land_around() { + let none = BTreeSet::new(); + let end = checks(json!({"repurposed": "stronghold_end"})); + let mut fake = Fake { + column: column(256, 0, '#'), + ..Fake::default() + }; + // Every height 64 by default: 63, 62, ... 55 after the 9 samples. + assert!(end.pass(&mut fake, &none, CHUNK, 0, None)); + // Land at least 45 above the bottom, after the sample's minus one: + // 46 at the corner passes, 45 fails. + fake.heights.insert((32, -16), 46); + assert!(end.pass(&mut fake, &none, CHUNK, 0, None)); + fake.heights.insert((32, -16), 45); + assert!(!end.pass(&mut fake, &none, CHUNK, 0, None)); + // Void 70 blocks north fails at once. + fake.heights.clear(); + fake.heights.insert((32, -86), 0); + assert!(!end.pass(&mut fake, &none, CHUNK, 0, None)); + } +} diff --git a/crates/horizon-world/src/worldgen/structure/stronghold.rs b/crates/horizon-world/src/worldgen/structure/stronghold.rs index 270b412af..e1a10808f 100644 --- a/crates/horizon-world/src/worldgen/structure/stronghold.rs +++ b/crates/horizon-world/src/worldgen/structure/stronghold.rs @@ -14,17 +14,18 @@ use std::{collections::BTreeSet, f64::consts::PI, sync::Mutex}; /// ChunkGeneratorStructureState.generateRingPositions with /// BiomeSource.findBiomeHorizontal (radius 112, step 1, not closest). +/// `biomes` samples a search's quarts in order, as one caching resolver. pub fn ring_positions( seed: i64, distance: i32, spread: i32, count: i32, preferred: &BTreeSet<BiomeId>, - biome: &mut dyn FnMut([i32; 3]) -> BiomeId, + biomes: &mut dyn FnMut(&[[i32; 3]]) -> Vec<BiomeId>, ) -> Vec<ChunkPos> { ring_searches(seed, distance, spread, count) .iter() - .map(|search| search.find(preferred, biome)) + .map(|search| search.find(preferred, biomes)) .collect() } @@ -39,23 +40,30 @@ pub struct RingSearch { impl RingSearch { /// BiomeSource.findBiomeHorizontal around the ring chunk's centre: a /// random preferred biome within 112 blocks, else the ring chunk itself. + /// + /// Vanilla samples the square in order on one thread, and its climate + /// index (Climate.RTree) starts each lookup from the previous result and + /// keeps it on a tie; so `biomes` gets the whole square at once, in + /// vanilla's order. Fresh lookups moved seed 1234's first stronghold 6 + /// chunks: one quart on a lush caves / ocean tie went to the ocean. pub fn find( &self, preferred: &BTreeSet<BiomeId>, - biome: &mut dyn FnMut([i32; 3]) -> BiomeId, + biomes: &mut dyn FnMut(&[[i32; 3]]) -> Vec<BiomeId>, ) -> ChunkPos { let mut search = self.random.clone(); let (cx, cz) = ((self.x * 16 + 8) >> 2, (self.z * 16 + 8) >> 2); + let quarts: Vec<[i32; 3]> = (-28..=28) + .flat_map(|dz| (-28..=28).map(move |dx| [cx + dx, 0, cz + dz])) + .collect(); let mut found = 0; let mut result = None; - for dz in -28..=28 { - for dx in -28..=28 { - if preferred.contains(&biome([cx + dx, 0, cz + dz])) { - if result.is_none() || search.next_int_bound(found + 1).unwrap_or(1) == 0 { - result = Some(((cx + dx) << 2, (cz + dz) << 2)); - } - found += 1; + for (quart, biome) in quarts.iter().zip(biomes(&quarts)) { + if preferred.contains(&biome) { + if result.is_none() || search.next_int_bound(found + 1).unwrap_or(1) == 0 { + result = Some((quart[0] << 2, quart[2] << 2)); } + found += 1; } } match result { diff --git a/crates/horizon-world/src/worldgen/structure/yung.rs b/crates/horizon-world/src/worldgen/structure/yung.rs new file mode 100644 index 000000000..861266253 --- /dev/null +++ b/crates/horizon-world/src/worldgen/structure/yung.rs @@ -0,0 +1,1121 @@ +//! YUNG's API's jigsaw assembler, for the structures of the "YUNG's Better" +//! mods carried over from a modpack (docs/modpacks.md). Carry maps +//! `yungsapi:yung_jigsaw` to `minecraft:jigsaw` with a `"horizon:yungsapi"` +//! key, and its pool elements to vanilla's single element with the piece's +//! rules under the same key; vanilla data never has it. +//! +//! The assembler differs from vanilla's JigsawPlacement: it picks the start +//! piece and every child by weight from the pool's weighted entries (a +//! priority piece first), lays pieces out breadth first, caps pieces by name +//! (`max_count`), limits them by depth, lets some ignore the bounds, checks +//! each piece's condition, and keeps every piece between `min_y` and +//! `max_y`. Pieces fit the same free space as vanilla's. A piece that ends +//! up with no child can be swapped for one of a dead-end pool, and once the +//! layout is done each piece's modifiers can turn it into another template +//! or place it last. +use super::{ + BoundingBox, Rotation, StructureResources, StructureTemplate, + jigsaw::{ + Free, JigsawConfig, JigsawContext, Junction, PoolElement, PoolPiece, Pools, Projection, + can_attach, shuffle, step, + }, +}; +use crate::{ + heightmap::HeightmapType, + worldgen::{DecodeError, block_feature::FeatureBlockBindings}, +}; +use horizon_core::random::{LegacyRandom, RandomSource, WorldgenRandom}; +use serde_json::Value; +use std::{ + cell::RefCell, + collections::{HashMap, VecDeque}, + rc::Rc, + sync::Arc, +}; + +/// A YUNG structure's own settings (`"horizon:yungsapi"` on the structure): +/// the heights every piece must stay within. +#[derive(Debug, Default)] +pub struct Config { + pub min_y: Option<i32>, + pub max_y: Option<i32>, +} +impl Config { + pub fn decode(v: &Value) -> Self { + let y = |k: &str| v[k].as_i64().map(|n| n as i32); + Self { + min_y: y("min_y"), + max_y: y("max_y"), + } + } +} + +/// A piece's rules (`"horizon:yungsapi"` on a pool element). +#[derive(Clone, Debug, Default)] +pub struct Rules { + /// Pieces sharing a name share the cap `max_count`. + name: Option<String>, + max_count: Option<i32>, + min_depth: Option<i32>, + max_depth: Option<i32>, + /// Chosen before any other piece of its pool. + priority: bool, + /// Placed even where it overlaps other pieces or leaves the bounds. + ignore_bounds: bool, + condition: Condition, + /// The pool a piece that got no child is swapped for. + deadend_pool: Option<String>, + /// Applied to the piece once the layout is done. + modifiers: Vec<Modifier>, +} +impl Rules { + pub fn decode(v: &Value, resources: &StructureResources) -> Result<Self, DecodeError> { + let int = |k: &str| v[k].as_i64().map(|n| n as i32); + Ok(Self { + name: v["name"].as_str().map(str::to_owned), + max_count: int("max_count"), + min_depth: int("min_depth"), + max_depth: int("max_depth"), + priority: v["priority"].as_bool().unwrap_or(false), + ignore_bounds: v["ignore_bounds"].as_bool().unwrap_or(false), + condition: match v.get("condition") { + Some(c) => Condition::decode(c, resources)?, + None => Condition::True, + }, + deadend_pool: v["deadend_pool"].as_str().map(str::to_owned), + modifiers: v["modifiers"] + .as_array() + .into_iter() + .flatten() + .map(|m| Modifier::decode(m, resources)) + .collect::<Result<_, _>>()?, + }) + } + /// YungJigsawPoolElement.isAtValidDepth. + fn at_valid_depth(&self, depth: i32) -> bool { + self.min_depth.is_none_or(|d| d <= depth) && self.max_depth.is_none_or(|d| d >= depth) + } + /// The name a capped piece counts under: `max_count` without a name is + /// ignored, as YUNG's does. + fn capped(&self) -> Option<(&str, i32)> { + Some((self.name.as_deref()?, self.max_count?)) + } +} + +/// A StructureModifier: when its condition holds for the piece, its actions +/// apply to the piece (the `self` target, the only one YUNG's API has). +#[derive(Clone, Debug)] +struct Modifier { + condition: Condition, + actions: Vec<Action>, +} +impl Modifier { + fn decode(v: &Value, resources: &StructureResources) -> Result<Self, DecodeError> { + Ok(Self { + condition: match v.get("condition") { + Some(c) => Condition::decode(c, resources)?, + None => Condition::True, + }, + actions: v["actions"] + .as_array() + .ok_or(DecodeError::Field("modifier actions"))? + .iter() + .map(|a| Action::decode(a, resources)) + .collect::<Result<_, _>>()?, + }) + } +} + +#[derive(Clone, Debug)] +enum Action { + /// TransformAction: the piece becomes one of these templates, moved by + /// `offset` turned with the piece. + Transform { + output: Vec<Arc<StructureTemplate>>, + offset: [i32; 3], + }, + /// DelayGenerationAction: the piece is placed after every other. + DelayGeneration, +} +impl Action { + fn decode(v: &Value, resources: &StructureResources) -> Result<Self, DecodeError> { + Ok(match v["type"].as_str().unwrap_or_default() { + "transform" => { + let output = v["output"] + .as_array() + .into_iter() + .flatten() + .map(|t| { + let id = t.as_str().ok_or(DecodeError::Field("transform output"))?; + resources + .template(id) + .ok_or_else(|| DecodeError::Registry(format!("missing template {id}"))) + }) + .collect::<Result<Vec<_>, _>>()?; + if output.is_empty() { + return Err(DecodeError::Field("transform output")); + } + let offset = v["offset"].as_array(); + let at = |i: usize| { + offset + .and_then(|o| o.get(i)) + .and_then(Value::as_i64) + .unwrap_or(0) as i32 + }; + Self::Transform { + output, + offset: [at(0), at(1), at(2)], + } + } + "delay_generation" => Self::DelayGeneration, + _ => return Err(DecodeError::Field("YUNG modifier action type")), + }) + } +} + +/// YUNG's structure conditions, as carry writes them. +#[derive(Clone, Debug, Default)] +enum Condition { + #[default] + True, + False, + Not(Box<Condition>), + AllOf(Vec<Condition>), + AnyOf(Vec<Condition>), + Depth { + min: Option<i32>, + max: Option<i32>, + }, + Altitude { + bottom: Option<f64>, + top: Option<f64>, + }, + RandomChance(f32), + Rotation(Vec<Rotation>), + /// Another piece (of these templates, or any) within `range` blocks in + /// the direction `rotation` turns the piece's own north. + PieceInDirection { + templates: Vec<Arc<StructureTemplate>>, + range: i32, + rotation: Rotation, + }, + /// Another piece (of these templates, or any when None) within the + /// piece's box grown by these ranges, not overlapping the piece. + PieceInRange { + templates: Option<Vec<Arc<StructureTemplate>>>, + horizontal: i32, + above: i32, + below: i32, + }, +} +impl Condition { + fn decode(v: &Value, resources: &StructureResources) -> Result<Self, DecodeError> { + let list = |k: &'static str| -> Result<Vec<Condition>, DecodeError> { + v[k].as_array() + .ok_or(DecodeError::Field(k))? + .iter() + .map(|c| Self::decode(c, resources)) + .collect() + }; + let int = |k: &str| v[k].as_i64().map(|n| n as i32); + let rotation = |r: &Value| { + r.as_str() + .and_then(Rotation::from_name) + .ok_or(DecodeError::Field("condition rotation")) + }; + Ok(match v["type"].as_str().unwrap_or_default() { + "true" => Self::True, + "false" => Self::False, + "not" => Self::Not(Box::new(Self::decode(&v["condition"], resources)?)), + "all_of" => Self::AllOf(list("conditions")?), + "any_of" => Self::AnyOf(list("conditions")?), + "depth" => Self::Depth { + min: int("min_depth"), + max: int("max_depth"), + }, + "altitude" => Self::Altitude { + bottom: v["bottom"].as_f64(), + top: v["top"].as_f64(), + }, + "random_chance" => Self::RandomChance(v["chance"].as_f64().unwrap_or(0.) as f32), + "rotation" => Self::Rotation( + v["rotations"] + .as_array() + .into_iter() + .flatten() + .map(rotation) + .collect::<Result<_, _>>()?, + ), + "piece_in_horizontal_direction" => Self::PieceInDirection { + templates: v["pieces"] + .as_array() + .into_iter() + .flatten() + .filter_map(|p| resources.template(p.as_str()?)) + .collect(), + range: int("range").unwrap_or(0), + rotation: rotation(&v["rotation"])?, + }, + "piece_in_range" => { + let pieces = v["pieces"] + .as_array() + .map(Vec::as_slice) + .unwrap_or_default(); + let any = pieces.is_empty() || pieces.iter().any(|p| p == "yungsapi:all"); + Self::PieceInRange { + // An unknown template matches nothing, as YUNG's API's + // empty stand-in does. + templates: (!any).then(|| { + pieces + .iter() + .filter_map(|p| resources.template(p.as_str()?)) + .collect() + }), + horizontal: int("horizontal_range").unwrap_or(0), + above: int("above_range").unwrap_or(0), + below: int("below_range").unwrap_or(0), + } + } + _ => return Err(DecodeError::Field("YUNG condition type")), + }) + } + /// StructureCondition.passes. `at` is the piece being placed: its box, + /// rotation and depth; the start piece has no box yet. + fn passes(&self, at: &At<'_>, random: &mut impl RandomSource) -> bool { + match self { + Self::True => true, + Self::False => false, + Self::Not(c) => !c.passes(at, random), + Self::AllOf(all) => all.iter().all(|c| c.passes(at, random)), + Self::AnyOf(any) => any.iter().any(|c| c.passes(at, random)), + Self::Depth { min, max } => { + min.is_none_or(|d| d <= at.depth) && max.is_none_or(|d| d >= at.depth) + } + Self::Altitude { bottom, top } => { + let [low, high] = at.span; + bottom.is_none_or(|y| f64::from(low) >= y) + && top.is_none_or(|y| f64::from(high) <= y) + } + Self::RandomChance(chance) => random.next_float() < *chance, + Self::Rotation(valid) => valid.contains(&at.rotation), + Self::PieceInDirection { + templates, + range, + rotation, + } => { + let Some(piece) = at.piece else { + return false; + }; + let mut search = piece; + match at.rotation.get_rotated(*rotation) { + Rotation::None => search.min[2] -= range, + Rotation::Clockwise90 => search.max[0] += range, + Rotation::Clockwise180 => search.max[2] += range, + Rotation::Counterclockwise90 => search.min[0] -= range, + } + at.near(piece, &search, |template| { + templates.is_empty() || templates.iter().any(|t| Arc::ptr_eq(t, template)) + }) + } + Self::PieceInRange { + templates, + horizontal, + above, + below, + } => { + let Some(piece) = at.piece else { + return false; + }; + let search = BoundingBox::new( + [ + piece.min[0] - horizontal, + piece.min[1] - below, + piece.min[2] - horizontal, + ], + [ + piece.max[0] + horizontal, + piece.max[1] + above, + piece.max[2] + horizontal, + ], + ); + at.near(piece, &search, |template| { + templates + .as_ref() + .is_none_or(|ts| ts.iter().any(|t| Arc::ptr_eq(t, template))) + }) + } + } + } +} + +/// The surface height query, object-safe for the per-call cache. +trait JigsawContextDyn { + fn surface(&mut self, x: i32, z: i32) -> i32; +} +impl<C: JigsawContext> JigsawContextDyn for C { + fn surface(&mut self, x: i32, z: i32) -> i32 { + self.base_height(x, z, HeightmapType::WorldSurfaceWg) + } +} + +/// What a condition sees of the piece being placed (or modified). +struct At<'a> { + depth: i32, + rotation: Rotation, + piece: Option<BoundingBox>, + /// The piece's lowest and highest Y when the condition was set up. + span: [i32; 2], + pieces: &'a [PoolPiece], + /// Pieces taken out by a dead end, by index; the rest are kept. + gone: &'a [bool], +} +impl At<'_> { + /// Whether another single piece whose template matches lies in `search` + /// without overlapping `piece` (nor being exactly it). + fn near( + &self, + piece: BoundingBox, + search: &BoundingBox, + matches: impl Fn(&Arc<StructureTemplate>) -> bool, + ) -> bool { + self.pieces.iter().enumerate().any(|(i, other)| { + let PoolElement::Single { template, .. } = other.element.as_ref() else { + return false; + }; + let b = other.bounding_box; + !self.gone.get(i).copied().unwrap_or(false) + && b != piece + && matches(template) + && b.intersects(search) + && !b.intersects(&piece) + }) + } +} + +/// The index of the entry to try: the first priority piece, else one drawn +/// by weight (`nextInt(total) + 1`, counted down). +fn pick( + candidates: &[(Arc<PoolElement>, i32)], + total: i32, + random: &mut impl RandomSource, +) -> usize { + if let Some(i) = candidates + .iter() + .position(|(e, _)| e.yung().is_some_and(|r| r.priority)) + { + return i; + } + let mut chosen = random.next_int_bound(total).unwrap_or(0) + 1; + for (i, (_, weight)) in candidates.iter().enumerate() { + chosen -= weight; + if chosen <= 0 { + return i; + } + } + candidates.len() - 1 +} + +/// JigsawManager.assembleJigsawStructure: the start position and the +/// pieces, or None when no start piece fits. +#[allow(clippy::too_many_arguments)] +pub fn assemble( + config: &JigsawConfig<'_>, + yung: &Config, + context: &mut impl JigsawContext, + pools: &Pools, + resources: &StructureResources, + bindings: &FeatureBlockBindings<'_>, + random: &mut impl RandomSource, + position: [i32; 3], +) -> Option<([i32; 3], Vec<PoolPiece>)> { + let pool = pools.get(config.start_pool, resources, bindings)?; + let mut candidates = pool.raw.clone(); + shuffle(&mut candidates, random); + let rotation = Rotation::random(random); + let mut total: i32 = candidates.iter().map(|c| c.1).sum(); + let (element, adjusted, local) = loop { + if candidates.is_empty() || total <= 0 { + return None; + } + let index = pick(&candidates, total, random); + let (element, weight) = candidates[index].clone(); + if matches!(element.as_ref(), PoolElement::Empty) { + return None; + } + let anchored = match config.start_jigsaw { + Some(name) => { + element + .shuffled_jigsaws(resources, position, rotation, random) + .into_iter() + .find(|j| j.name.as_deref() == Some(name))? + .pos + } + None => position, + }; + let local: [i32; 3] = std::array::from_fn(|i| anchored[i] - position[i]); + let adjusted: [i32; 3] = std::array::from_fn(|i| position[i] - local[i]); + let at = At { + depth: 0, + rotation, + piece: None, + span: [0, 0], + pieces: &[], + gone: &[], + }; + if element + .yung() + .is_some_and(|r| !r.condition.passes(&at, random)) + { + total -= weight; + candidates.remove(index); + continue; + } + break (element, adjusted, local); + }; + let mut start = PoolPiece { + bounding_box: element.bounding_box(adjusted, rotation), + ground_level_delta: element.ground_level_delta(), + element, + position: adjusted, + rotation, + junctions: Vec::new(), + waterlogging: config.waterlogging, + }; + let b = start.bounding_box; + let (cx, cz) = ((b.max[0] + b.min[0]) / 2, (b.max[2] + b.min[2]) / 2); + let bottom = match config.project_to { + Some(kind) => position[1] + context.base_height(cx, cz, kind), + None => adjusted[1], + }; + start.moved(bottom - (b.min[1] + start.ground_level_delta)); + let cy = bottom + local[1]; + let d = config.max_distance[0]; + let free = Rc::new(RefCell::new(Free { + container: BoundingBox::new([cx - d, cy - d, cz - d], [cx + d, cy + d, cz + d]), + removed: vec![start.bounding_box], + })); + let mut assembler = Assembler { + config, + yung, + pools, + resources, + bindings, + pieces: vec![start], + states: vec![(free, 0)], + origins: vec![None], + gone: vec![false], + counts: HashMap::new(), + }; + if config.max_depth > 0 { + let mut queue = VecDeque::from([0]); + while let Some(piece) = queue.pop_front() { + assembler.children(piece, &mut queue, context, random); + } + } + Some(([cx, cy, cz], assembler.modified(random))) +} + +/// JigsawStructureAssembler. +struct Assembler<'a> { + config: &'a JigsawConfig<'a>, + yung: &'a Config, + pools: &'a Pools, + resources: &'a StructureResources, + bindings: &'a FeatureBlockBindings<'a>, + pieces: Vec<PoolPiece>, + /// Per piece: the free space it was placed in, and its depth. + states: Vec<(Rc<RefCell<Free>>, i32)>, + /// Per piece: where it was placed and the junction it left on its + /// parent (none for the start piece), for a dead end to undo. + origins: Vec<Option<(Slot, Junction)>>, + /// Per piece: taken out by a dead end. + gone: Vec<bool>, + /// Pieces placed per capped name. + counts: HashMap<String, i32>, +} + +/// Where a child goes: the parent's jigsaw and what it needs. +#[derive(Clone)] +struct Slot { + source: usize, + jigsaw: super::jigsaw::Jigsaw, + target_pos: [i32; 3], + free: Rc<RefCell<Free>>, + depth: i32, +} + +impl Assembler<'_> { + fn pool(&self, name: &str) -> Option<Arc<super::jigsaw::Pool>> { + let name = self.config.aliases.get(name).map_or(name, String::as_str); + self.pools.get(name, self.resources, self.bindings) + } + /// JigsawStructureAssembler.Settings.isInBounds. + fn in_bounds(&self, y: i32, context: &impl JigsawContext) -> bool { + self.yung.max_y.is_none_or(|m| y <= m) + && self.yung.min_y.is_none_or(|m| y >= m) + && y >= context.min_y() + self.config.padding[0] + && y <= context.max_y() + 1 - self.config.padding[1] + } + /// addChildrenForPiece: one child per jigsaw where one fits, from the + /// target pool unless the piece is at the last depth, else its fallback. + fn children( + &mut self, + source: usize, + queue: &mut VecDeque<usize>, + context: &mut impl JigsawContext, + random: &mut impl RandomSource, + ) { + let (element, position, rotation, source_box) = { + let s = &self.pieces[source]; + (s.element.clone(), s.position, s.rotation, s.bounding_box) + }; + let (context_free, depth) = self.states[source].clone(); + let mut source_free: Option<Rc<RefCell<Free>>> = None; + let jigsaws = element.shuffled_jigsaws(self.resources, position, rotation, random); + let count = jigsaws.len(); + let mut any = false; + for jigsaw in jigsaws { + let Some(target) = self.pool(&jigsaw.pool) else { + continue; + }; + if target.templates.is_empty() && jigsaw.pool != "minecraft:empty" { + continue; + } + let Some(fallback) = self.pool(&target.fallback) else { + continue; + }; + if fallback.templates.is_empty() && target.fallback != "minecraft:empty" { + continue; + } + let s = step(jigsaw.front); + let target_pos: [i32; 3] = std::array::from_fn(|i| jigsaw.pos[i] + s[i]); + let free = if source_box.is_inside(target_pos) { + source_free + .get_or_insert_with(|| { + Rc::new(RefCell::new(Free { + container: source_box, + removed: Vec::new(), + })) + }) + .clone() + } else { + context_free.clone() + }; + let slot = Slot { + source, + jigsaw, + target_pos, + free, + depth, + }; + let placed = depth != self.config.max_depth + && self.choose(&slot, target.raw.clone(), queue, context, random); + any |= placed || self.choose(&slot, fallback.raw.clone(), queue, context, random); + } + if let Some(pool) = element.yung().and_then(|r| r.deadend_pool.as_deref()) + && !any + && count > 1 + { + self.dead_end(source, pool, queue, context, random); + } + } + /// A piece with a dead-end pool that got no child though it has other + /// jigsaws is taken out, its parent's junction and its room freed and its + /// count given back, and a piece of that pool is chosen in its place for + /// the same parent jigsaw. The start piece stays. + fn dead_end( + &mut self, + piece: usize, + pool: &str, + queue: &mut VecDeque<usize>, + context: &mut impl JigsawContext, + random: &mut impl RandomSource, + ) { + let Some(pool) = self.pool(pool) else { + return; + }; + let Some((slot, junction)) = self.origins[piece].clone() else { + return; + }; + let parent = &mut self.pieces[slot.source].junctions; + if let Some(i) = parent.iter().position(|j| *j == junction) { + parent.remove(i); + } + let bounds = self.pieces[piece].bounding_box; + { + let mut free = slot.free.borrow_mut(); + if let Some(i) = free.removed.iter().position(|b| *b == bounds) { + free.removed.remove(i); + } + } + self.gone[piece] = true; + let element = self.pieces[piece].element.clone(); + if let Some((name, _)) = element.yung().and_then(Rules::capped) + && let Some(count) = self.counts.get_mut(name) + { + *count -= 1; + } + self.choose(&slot, pool.raw.clone(), queue, context, random); + } + /// applyModifications: each piece's modifiers, in piece order, then the + /// delayed pieces moved last. Returns the pieces kept. + fn modified(mut self, random: &mut impl RandomSource) -> Vec<PoolPiece> { + let mut delayed = vec![false; self.pieces.len()]; + for (i, delay) in delayed.iter_mut().enumerate() { + let element = self.pieces[i].element.clone(); + let Some(rules) = element + .yung() + .filter(|r| !self.gone[i] && !r.modifiers.is_empty()) + else { + continue; + }; + let b = self.pieces[i].bounding_box; + let (depth, rotation, span) = ( + self.states[i].1, + self.pieces[i].rotation, + [b.min[1], b.max[1]], + ); + for modifier in &rules.modifiers { + let at = At { + depth, + rotation, + piece: Some(self.pieces[i].bounding_box), + span, + pieces: &self.pieces, + gone: &self.gone, + }; + if !modifier.condition.passes(&at, random) { + continue; + } + for action in &modifier.actions { + match action { + Action::Transform { output, offset } => { + transform(&mut self.pieces[i], output, *offset); + } + Action::DelayGeneration => *delay = true, + } + } + } + } + let (mut kept, mut last) = (Vec::new(), Vec::new()); + for ((piece, gone), delay) in self.pieces.into_iter().zip(self.gone).zip(delayed) { + if !gone { + if delay { &mut last } else { &mut kept }.push(piece); + } + } + kept.extend(last); + kept + } + /// chooseCandidateFromPool: whether a piece was placed. + fn choose( + &mut self, + slot: &Slot, + mut candidates: Vec<(Arc<PoolElement>, i32)>, + queue: &mut VecDeque<usize>, + context: &mut impl JigsawContext, + random: &mut impl RandomSource, + ) -> bool { + let jigsaw = &slot.jigsaw; + let (source_rigid, source_y, source_ground) = { + let s = &self.pieces[slot.source]; + ( + s.element.projection() == Projection::Rigid, + s.bounding_box.min[1], + s.ground_level_delta, + ) + }; + let local_y = jigsaw.pos[1] - source_y; + // The surface at the jigsaw, read once per call. + let mut surface = None; + let (x, z) = (jigsaw.pos[0], jigsaw.pos[2]); + let mut base = |context: &mut dyn JigsawContextDyn| { + *surface.get_or_insert_with(|| context.surface(x, z)) + }; + shuffle(&mut candidates, random); + let mut total: i32 = candidates.iter().map(|c| c.1).sum(); + while !candidates.is_empty() && total > 0 { + let index = pick(&candidates, total, random); + let (target, weight) = candidates[index].clone(); + if matches!(target.as_ref(), PoolElement::Empty) { + return false; + } + let rules = target.yung(); + let capped = rules.and_then(Rules::capped); + let full = capped + .is_some_and(|(name, max)| self.counts.get(name).copied().unwrap_or(0) >= max); + if full || rules.is_some_and(|r| !r.at_valid_depth(slot.depth)) { + total -= weight; + candidates.remove(index); + continue; + } + let mut rotations = Rotation::ALL; + shuffle(&mut rotations, random); + for rotation in rotations { + let target_jigsaws = + target.shuffled_jigsaws(self.resources, [0; 3], rotation, random); + let hack_box = target.bounding_box([0; 3], rotation); + let expand_to = if self.config.use_expansion_hack && hack_box.span()[1] <= 16 { + target_jigsaws + .iter() + .map(|tj| { + let st = step(tj.front); + if !hack_box.is_inside(std::array::from_fn(|i| tj.pos[i] + st[i])) { + return 0; + } + let child = self.pool(&tj.pool); + let child_size = child.as_ref().map_or(0, |p| p.max_size()); + let fallback_size = child + .and_then(|p| self.pool(&p.fallback)) + .map_or(0, |p| p.max_size()); + child_size.max(fallback_size) + }) + .max() + .unwrap_or(0) + } else { + 0 + }; + for target_jigsaw in &target_jigsaws { + if !can_attach(jigsaw, target_jigsaw) { + continue; + } + let target_local = target_jigsaw.pos; + let raw_position: [i32; 3] = + std::array::from_fn(|i| slot.target_pos[i] - target_local[i]); + let raw_box = target.bounding_box(raw_position, rotation); + let target_rigid = target.projection() == Projection::Rigid; + let delta_y = local_y - target_local[1] + step(jigsaw.front)[1]; + let target_y = if source_rigid && target_rigid { + source_y + delta_y + } else { + base(context) - target_local[1] + }; + let offset = target_y - raw_box.min[1]; + let mut target_box = raw_box.moved([0, offset, 0]); + let target_position = + [raw_position[0], raw_position[1] + offset, raw_position[2]]; + if expand_to > 0 { + let size = (expand_to + 1).max(target_box.max[1] - target_box.min[1]); + target_box.max[1] = target_box.min[1] + size; + } + if !self.in_bounds(target_box.max[1], context) + || !self.in_bounds(target_box.min[1], context) + { + continue; + } + if !rules.is_some_and(|r| r.ignore_bounds) + && !slot.free.borrow().fits(&target_box) + { + continue; + } + let target_ground = if target_rigid { + source_ground - delta_y + } else { + target.ground_level_delta() + }; + let junction_y = if source_rigid { + source_y + local_y + } else if target_rigid { + target_y + target_local[1] + } else { + base(context) + delta_y / 2 + }; + let at = At { + depth: slot.depth + 1, + rotation, + piece: Some(target_box), + span: [target_box.min[1], target_box.max[1]], + pieces: &self.pieces, + gone: &self.gone, + }; + if rules.is_some_and(|r| !r.condition.passes(&at, random)) { + continue; + } + let on_parent = Junction { + source_x: slot.target_pos[0], + source_ground_y: junction_y - local_y + source_ground, + source_z: slot.target_pos[2], + }; + self.pieces[slot.source].junctions.push(on_parent); + slot.free.borrow_mut().removed.push(target_box); + self.pieces.push(PoolPiece { + element: target.clone(), + position: target_position, + ground_level_delta: target_ground, + rotation, + bounding_box: target_box, + junctions: vec![Junction { + source_x: jigsaw.pos[0], + source_ground_y: junction_y - target_local[1] + target_ground, + source_z: jigsaw.pos[2], + }], + waterlogging: self.config.waterlogging, + }); + self.states.push((slot.free.clone(), slot.depth + 1)); + self.origins.push(Some((slot.clone(), on_parent))); + self.gone.push(false); + if slot.depth < self.config.max_depth { + queue.push_back(self.pieces.len() - 1); + } + if let Some((name, _)) = capped { + *self.counts.entry(name.to_owned()).or_default() += 1; + } + return true; + } + } + total -= weight; + candidates.remove(index); + } + false + } +} + +/// TransformAction: the piece takes one of `output`'s templates, picked by a +/// random seeded with setFeatureSeed(x, y, x) of its position, and moves by +/// `offset` turned with it. Like YUNG's API's new piece, it loses its +/// junctions and takes its own liquid setting (else waterlogging). +fn transform(piece: &mut PoolPiece, output: &[Arc<StructureTemplate>], offset: [i32; 3]) { + let [x, y, _] = piece.position; + let mut random = WorldgenRandom::new(LegacyRandom::new(0)); + random.set_feature_seed(i64::from(x), y, x); + let chosen = random.next_int_bound(output.len() as i32).unwrap_or(0) as usize; + let mut element = (*piece.element).clone(); + let PoolElement::Single { + template, + waterlogging, + .. + } = &mut element + else { + return; + }; + *template = output[chosen].clone(); + piece.waterlogging = waterlogging.unwrap_or(true); + let [dx, dy, dz] = offset; + let [dx, dz] = match piece.rotation { + Rotation::None => [dx, dz], + Rotation::Clockwise90 => [-dz, dx], + Rotation::Clockwise180 => [-dx, -dz], + Rotation::Counterclockwise90 => [dz, -dx], + }; + piece.position = [x + dx, y + dy, piece.position[2] + dz]; + piece.bounding_box = element.bounding_box(piece.position, piece.rotation); + piece.element = Arc::new(element); + piece.junctions.clear(); +} + +#[cfg(test)] +mod tests { + use super::*; + use horizon_core::random::LegacyRandom; + + fn single(template: &Arc<StructureTemplate>, rules: Option<Rules>) -> Arc<PoolElement> { + Arc::new(PoolElement::Single { + template: template.clone(), + processors: Default::default(), + legacy: false, + projection: Projection::Rigid, + waterlogging: None, + yung: rules.map(Box::new), + enhanced_terrain: None, + }) + } + + #[test] + fn picks_priority_pieces_then_by_weight() { + let t = Arc::new(StructureTemplate::empty()); + let plain = single(&t, None); + let first = single( + &t, + Some(Rules { + priority: true, + ..Rules::default() + }), + ); + let mut random = LegacyRandom::new(7); + let list = vec![(plain.clone(), 3), (first, 1)]; + assert_eq!(pick(&list, 4, &mut random), 1); + // By weight: nextInt(total) + 1, counted down; the first entry + // takes draws 1..=3 of 4. + let list = vec![(plain.clone(), 3), (plain, 1)]; + let mut ours = LegacyRandom::new(42); + let mut theirs = LegacyRandom::new(42); + for _ in 0..32 { + let draw = theirs.next_int_bound(4).unwrap() + 1; + assert_eq!(pick(&list, 4, &mut ours), usize::from(draw > 3)); + } + } + + #[test] + fn rules_cap_by_name_and_depth() { + let rules = Rules { + name: Some("room".into()), + max_count: Some(2), + min_depth: Some(1), + max_depth: Some(3), + ..Rules::default() + }; + assert_eq!(rules.capped(), Some(("room", 2))); + assert!(!rules.at_valid_depth(0) && rules.at_valid_depth(1) && !rules.at_valid_depth(4)); + // max_count without a name is ignored, as YUNG's does. + let unnamed = Rules { + max_count: Some(1), + ..Rules::default() + }; + assert_eq!(unnamed.capped(), None); + } + + #[test] + fn conditions() { + let t = Arc::new(StructureTemplate::empty()); + let other = Arc::new(StructureTemplate::empty()); + let neighbour = PoolPiece { + element: single(&t, None), + position: [0; 3], + ground_level_delta: 1, + rotation: Rotation::None, + bounding_box: BoundingBox::new([0, 0, -6], [4, 4, -1]), + junctions: Vec::new(), + waterlogging: true, + }; + let pieces = [neighbour]; + let at = |rotation, depth| At { + depth, + rotation, + piece: Some(BoundingBox::new([0, 10, 0], [4, 14, 4])), + span: [10, 14], + pieces: &pieces, + gone: &[], + }; + let mut random = LegacyRandom::new(1); + let depth = Condition::Depth { + min: Some(2), + max: None, + }; + assert!(!depth.passes(&at(Rotation::None, 1), &mut random)); + assert!(depth.passes(&at(Rotation::None, 2), &mut random)); + let altitude = |bottom, top| Condition::Altitude { bottom, top }; + assert!(altitude(Some(10.), Some(14.)).passes(&at(Rotation::None, 0), &mut random)); + assert!(!altitude(Some(10.5), None).passes(&at(Rotation::None, 0), &mut random)); + assert!(!altitude(None, Some(13.9)).passes(&at(Rotation::None, 0), &mut random)); + // North (-Z) of an unrotated piece, within 5 blocks; the neighbour + // stays below it in Y, but only X and Z are searched, and it must + // not overlap the piece itself. + let north = |templates, rotation| Condition::PieceInDirection { + templates, + range: 5, + rotation, + }; + assert!(!north(Vec::new(), Rotation::None).passes(&at(Rotation::None, 1), &mut random)); + let low = At { + piece: Some(BoundingBox::new([0, 0, 0], [4, 4, 4])), + span: [0, 4], + ..at(Rotation::None, 1) + }; + assert!(north(Vec::new(), Rotation::None).passes(&low, &mut random)); + assert!(north(vec![t.clone()], Rotation::None).passes(&low, &mut random)); + assert!(!north(vec![other], Rotation::None).passes(&low, &mut random)); + // The piece turned 180°: its north is south. + let turned = At { + rotation: Rotation::Clockwise180, + ..low + }; + assert!(!north(Vec::new(), Rotation::None).passes(&turned, &mut random)); + assert!(north(Vec::new(), Rotation::Clockwise180).passes(&turned, &mut random)); + let all = Condition::AllOf(vec![ + Condition::True, + Condition::Not(Box::new(Condition::False)), + Condition::Rotation(vec![Rotation::Clockwise180]), + ]); + assert!(all.passes(&turned, &mut random)); + assert!(!Condition::AnyOf(vec![Condition::False]).passes(&turned, &mut random)); + } + + #[test] + fn piece_in_range_searches_a_grown_box() { + let t = Arc::new(StructureTemplate::empty()); + let other = Arc::new(StructureTemplate::empty()); + let piece = BoundingBox::new([0, 0, 0], [4, 4, 4]); + let above = |gone: &'static [bool]| { + let pieces = vec![PoolPiece { + element: single(&t, None), + position: [0, 30, 0], + ground_level_delta: 1, + rotation: Rotation::None, + bounding_box: BoundingBox::new([0, 30, 0], [4, 34, 4]), + junctions: Vec::new(), + waterlogging: true, + }]; + (pieces, gone) + }; + let range = |templates, above| Condition::PieceInRange { + templates, + horizontal: 0, + above, + below: 0, + }; + let mut random = LegacyRandom::new(1); + let (pieces, gone) = above(&[]); + let at = At { + depth: 1, + rotation: Rotation::None, + piece: Some(piece), + span: [0, 4], + pieces: &pieces, + gone, + }; + // 26 blocks above the piece's top: in a 40-block search, not 20. + assert!(range(None, 40).passes(&at, &mut random)); + assert!(!range(None, 20).passes(&at, &mut random)); + assert!(range(Some(vec![t.clone()]), 40).passes(&at, &mut random)); + assert!(!range(Some(vec![other]), 40).passes(&at, &mut random)); + // An unknown template matches nothing; a piece taken out by a dead + // end is not seen. + assert!(!range(Some(Vec::new()), 40).passes(&at, &mut random)); + let (pieces, gone) = above(&[true]); + let at = At { + pieces: &pieces, + gone, + ..at + }; + assert!(!range(None, 40).passes(&at, &mut random)); + } + + #[test] + fn transform_picks_by_position_and_turns_its_offset() { + let (a, b) = ( + Arc::new(StructureTemplate::empty()), + Arc::new(StructureTemplate::empty()), + ); + let rules = Rules { + name: Some("pillar".into()), + ..Rules::default() + }; + let mut piece = PoolPiece { + element: single(&a, Some(rules)), + position: [100, 40, -7], + ground_level_delta: 1, + rotation: Rotation::Clockwise90, + bounding_box: BoundingBox::new([100, 40, -7], [104, 44, -3]), + junctions: vec![Junction { + source_x: 0, + source_ground_y: 0, + source_z: 0, + }], + waterlogging: false, + }; + transform(&mut piece, &[a.clone(), b.clone()], [-1, 2, 0]); + // setFeatureSeed(x, y, x): seed x + y + 10000 * x, then nextInt(2). + let mut java = LegacyRandom::new(100 + 40 + 10000 * 100); + let chosen = [&a, &b][java.next_int_bound(2).unwrap() as usize]; + let PoolElement::Single { template, yung, .. } = piece.element.as_ref() else { + panic!("not a single element"); + }; + assert!(Arc::ptr_eq(template, chosen)); + assert_eq!( + yung.as_ref().and_then(|r| r.name.as_deref()), + Some("pillar") + ); + // (-1, 2, 0) turned 90° clockwise is (0, 2, -1). + assert_eq!(piece.position, [100, 42, -8]); + assert!(piece.junctions.is_empty() && piece.waterlogging); + } +} diff --git a/crates/horizon-world/tests/collision_data.rs b/crates/horizon-world/tests/collision_data.rs index 3738edf1a..85b41d98e 100644 --- a/crates/horizon-world/tests/collision_data.rs +++ b/crates/horizon-world/tests/collision_data.rs @@ -267,3 +267,75 @@ fn registry_scan_binding_requires_complete_canonical_metadata() { ); } } + +/// A table read from its bytes checks its state identities while reading and +/// keeps none of them, yet loads the same shapes as the tree; a nested table +/// (`block-properties.nbt`'s `occlusion`) is checked the same way. +#[test] +fn streamed_tables_check_states_while_reading() { + use horizon_world::{ + nbt::{self, Limits, NamedTag}, + registry::{BlockTable, BlockTableFile}, + }; + let (registry_tag, collision_tag) = fixture(); + let registry = BlockRegistry::from_nbt(®istry_tag).unwrap(); + let bytes = |tag: &Tag| { + let root = NamedTag { + name: "".into(), + tag: tag.clone(), + }; + nbt::write_named(&root, Limits::tables()).unwrap() + }; + let file = BlockTableFile::read(&bytes(&collision_tag), ®istry).unwrap(); + let table = file.table("collision").unwrap(); + assert_eq!(table.get("states"), Some(&Tag::List(Vec::new()))); + let streamed = CachedCollisionRegistry::from_table(table).unwrap(); + let tree = CachedCollisionRegistry::from_nbt(&collision_tag, ®istry).unwrap(); + for id in 0..3 { + let id = BlockStateId(id); + assert_eq!( + streamed.cached_shape(id).unwrap().map(|s| s.boxes()), + tree.cached_shape(id).unwrap().map(|s| s.boxes()) + ); + } + + let mut outer = Tag::compound([("occlusion", collision_tag.clone())]); + let Tag::Compound(map) = &mut outer else { + unreachable!() + }; + map.insert( + "states".into(), + collision_tag.get("states").unwrap().clone(), + ); + let file = BlockTableFile::read(&bytes(&outer), ®istry).unwrap(); + let nested = file + .table("outer") + .unwrap() + .nested("occlusion", "occlusion") + .unwrap(); + assert_eq!(nested.get("states"), Some(&Tag::List(Vec::new()))); + CachedCollisionRegistry::from_table(nested).unwrap(); + // Checked only as a tree, an emptied list is refused. + assert!(BlockTable::check(nested.tag(), ®istry, "occlusion").is_err()); + + // Other states, a different count or no states at all are refused. + let with_states = |edit: &dyn Fn(&mut Vec<Tag>)| { + let mut tag = collision_tag.clone(); + let Some(Tag::List(states)) = tag.get_mut("states") else { + unreachable!() + }; + edit(states); + tag + }; + let swapped = with_states(&|s| s.swap(1, 2)); + assert!(BlockTableFile::read(&bytes(&swapped), ®istry).is_err()); + let short = with_states(&|s| drop(s.pop())); + assert!(BlockTableFile::read(&bytes(&short), ®istry).is_err()); + let mut swapped = swapped; + let Tag::Compound(map) = &mut swapped else { + unreachable!() + }; + map.remove(&"states".into()); + let file = BlockTableFile::read(&bytes(&swapped), ®istry).unwrap(); + assert!(file.table("collision").is_err()); +} diff --git a/crates/horizon-world/tests/material_rule_specialization.rs b/crates/horizon-world/tests/material_rule_specialization.rs new file mode 100644 index 000000000..e953e908e --- /dev/null +++ b/crates/horizon-world/tests/material_rule_specialization.rs @@ -0,0 +1,172 @@ +use horizon_core::{ + density::{ConstantSampler, DensitySampler}, + position::BlockPos, + random::{RandomSource, XoroshiroRandom}, +}; +use horizon_world::{ + registry::BlockStateId, + worldgen::{ + DecodeError, + anchor::WorldGenerationContext, + material_condition::{ + MaterialCondition, MaterialConditions, MaterialRule, MaterialRuleError, + MaterialSurfaceQueries, + }, + }, +}; +use std::{collections::BTreeSet, sync::Arc}; + +#[derive(Default)] +struct Queries { + bindings: Vec<bool>, + temperatures: usize, + biomes: usize, + fail_binding: bool, + fail_temperature: bool, +} +impl MaterialSurfaceQueries for Queries { + fn possible_biomes(&self) -> Option<BTreeSet<String>> { + Some(BTreeSet::from(["minecraft:plains".into()])) + } + fn surface_depth(&mut self, _: i32, _: i32) -> i32 { + 1 + } + fn surface_secondary(&mut self, _: i32, _: i32) -> f64 { + 0. + } + fn bind_density( + &mut self, + _: Arc<dyn DensitySampler>, + eager: bool, + ) -> Result<usize, DecodeError> { + self.bindings.push(eager); + if self.fail_binding { + return Err(DecodeError::Field("test binding failure")); + } + Ok(self.bindings.len() - 1) + } + fn biome(&mut self, _: BlockPos) -> Result<Option<Arc<str>>, MaterialRuleError> { + self.biomes += 1; + Ok(Some(Arc::from("minecraft:plains"))) + } + fn cold_enough_to_snow( + &mut self, + _: &str, + _: BlockPos, + _: i32, + ) -> Result<bool, MaterialRuleError> { + self.temperatures += 1; + if self.fail_temperature { + Err(MaterialRuleError::BiomeQueryFailed) + } else { + Ok(true) + } + } +} +fn worker(surface: Queries) -> MaterialConditions<XoroshiroRandom, Queries> { + MaterialConditions::new( + WorldGenerationContext::new(-64, 384, 63), + XoroshiroRandom::new(7).fork_positional(), + surface, + ) +} +fn condition(if_true: MaterialCondition, then_run: MaterialRule) -> MaterialRule { + MaterialRule::Condition { + if_true, + then_run: Box::new(then_run), + } +} +fn biome(name: &str) -> MaterialCondition { + MaterialCondition::Biome(vec![name.into()]) +} +fn ore() -> MaterialRule { + MaterialRule::OreVein { + ore: BlockStateId(1), + raw_ore: BlockStateId(2), + filler: BlockStateId(3), + raw_ore_chance: 0.02, + density: Arc::new(ConstantSampler(1.)), + richness: Arc::new(ConstantSampler(1.)), + filler_gap: Arc::new(ConstantSampler(-1.)), + } +} +#[test] +fn constant_branches_keep_binding_order_and_short_circuit_queries() { + let mut w = worker(Queries::default()); + let rule = w + .add_rule(MaterialRule::Sequence(vec![ + condition(biome("minecraft:desert"), ore()), + condition( + MaterialCondition::Not(Box::new(biome("minecraft:desert"))), + MaterialRule::Block(BlockStateId(9)), + ), + ore(), + ])) + .unwrap(); + w.update_xz(4, 5, 0, 0); + w.update_y(1, 1, i32::MIN, 64); + for _ in 0..2 { + assert_eq!( + w.try_apply(rule, BlockPos::new(4, 64, 5)), + Ok(Some(BlockStateId(9))) + ); + } + let q = w.into_surface(); + assert_eq!(q.bindings, [true, true, false, true, true, false]); + assert_eq!(q.biomes, 0); + assert_eq!(q.temperatures, 0); +} +#[test] +fn unreachable_children_still_fail_compilation_without_adding_a_rule() { + for preceding_block in [false, true] { + let mut w = worker(Queries { + fail_binding: true, + ..Queries::default() + }); + let dead = condition(biome("minecraft:desert"), ore()); + let rule = if preceding_block { + MaterialRule::Sequence(vec![MaterialRule::Block(BlockStateId(9)), dead]) + } else { + dead + }; + assert!(w.add_rule(rule).is_err()); + assert_eq!( + w.try_apply(0, BlockPos::new(0, 0, 0)), + Err(MaterialRuleError::UnknownRule) + ); + assert_eq!(w.into_surface().bindings, [true]); + } +} +#[test] +fn dynamic_predicate_on_empty_child_retains_each_temperature_call_and_error() { + for fail in [false, true] { + let mut w = worker(Queries { + fail_temperature: fail, + ..Queries::default() + }); + let rule = w + .add_rule(condition( + MaterialCondition::Temperature, + condition( + biome("minecraft:desert"), + MaterialRule::Block(BlockStateId(9)), + ), + )) + .unwrap(); + w.update_xz(0, 0, 0, 0); + w.update_y(1, 1, i32::MIN, 64); + for _ in 0..2 { + assert_eq!( + w.try_apply(rule, BlockPos::new(0, 64, 0)), + if fail { + Err(MaterialRuleError::BiomeQueryFailed) + } else { + Ok(None) + } + ); + } + let q = w.into_surface(); + assert_eq!(q.temperatures, 2); + assert_eq!(q.biomes, 1); + } +} diff --git a/crates/horizon-world/tests/rule_test_bounds.rs b/crates/horizon-world/tests/rule_test_bounds.rs index de4862f54..e3f614590 100644 --- a/crates/horizon-world/tests/rule_test_bounds.rs +++ b/crates/horizon-world/tests/rule_test_bounds.rs @@ -67,3 +67,80 @@ fn binding_rejects_unknown_tag_blocks_and_duplicate_canonical_tags() { ]); assert!(FeatureBlockBindings::with_tags(®istry, BTreeSet::new(), &tags).is_err()); } +#[test] +fn matter_phase_matches_what_a_block_is_made_of() { + // air, cave_air, void_air, stone, then water (level 0), rail, iron_chain. + let mut states: Vec<Tag> = [ + "minecraft:air", + "minecraft:cave_air", + "minecraft:void_air", + "minecraft:stone", + ] + .iter() + .map(|name| Tag::compound([("id", Tag::String((*name).into()))])) + .collect(); + states.push(Tag::compound([ + ("id", Tag::String("minecraft:water".into())), + ( + "properties", + Tag::compound([("level", Tag::String("0".into()))]), + ), + ])); + for name in ["minecraft:rail", "minecraft:iron_chain"] { + states.push(Tag::compound([("id", Tag::String(name.into()))])); + } + let names = [ + "minecraft:air", + "minecraft:cave_air", + "minecraft:void_air", + "minecraft:stone", + "minecraft:water", + "minecraft:rail", + "minecraft:iron_chain", + ]; + let registry = BlockRegistry::from_nbt(&Tag::compound([ + ("DataVersion", Tag::Int(DATA_VERSION)), + ("states", Tag::List(states)), + ( + "defaults", + Tag::Compound( + names + .iter() + .enumerate() + .map(|(id, name)| ((*name).into(), Tag::Int(id as i32))) + .collect(), + ), + ), + ])) + .unwrap(); + let bindings = FeatureBlockBindings::new(®istry, BTreeSet::new()).unwrap(); + let mut random = horizon_core::random::LegacyRandom::new(0); + let matches = |phase: &str, invert: bool, random: &mut horizon_core::random::LegacyRandom| { + let json = format!( + r#"{{"predicate_type":"horizon:matter_phase","phase_to_test_for":"{phase}","invert_condition":{invert}}}"# + ); + let test = RuleTest::from_json(&json, &bindings).unwrap(); + (0..names.len() as u32) + .filter(|&id| test.test(horizon_world::registry::BlockStateId(id), [0; 3], random)) + .collect::<Vec<_>>() + }; + assert_eq!(matches("AIR", false, &mut random), [0, 1, 2]); + assert_eq!(matches("LIQUID", false, &mut random), [4]); + assert_eq!( + matches("AIR_RAIL_OR_CHAIN", false, &mut random), + [0, 1, 2, 5, 6] + ); + assert_eq!( + matches("LIQUID_RAIL_OR_CHAIN", false, &mut random), + [4, 5, 6] + ); + assert_eq!(matches("AIR", true, &mut random), [3, 4, 5, 6]); + // The phase is the enum's name as written; SOLID needs canOcclude. + for json in [ + r#"{"predicate_type":"horizon:matter_phase","phase_to_test_for":"air"}"#, + r#"{"predicate_type":"horizon:matter_phase","phase_to_test_for":"SOLID"}"#, + r#"{"predicate_type":"horizon:matter_phase"}"#, + ] { + assert!(RuleTest::from_json(json, &bindings).is_err(), "{json}"); + } +} diff --git a/crates/horizon-world/tests/state_provider_bounds.rs b/crates/horizon-world/tests/state_provider_bounds.rs index 033d4675c..23553e6b7 100644 --- a/crates/horizon-world/tests/state_provider_bounds.rs +++ b/crates/horizon-world/tests/state_provider_bounds.rs @@ -41,7 +41,13 @@ fn bound_recursive_providers_and_registry_lists_before_sampling() { let branching = serde_json::json!({"if_true":{"type":"all_of","predicates":vec![serde_json::json!({"type":"true"});7]},"then":{"id":"stone"}}); let oversized = serde_json::json!({"type":"rule_based","rules":vec![branching;8192]}); assert!(BlockStateProvider::from_json(&oversized.to_string(), &bindings).is_err()); - let tags = BTreeMap::from([("horizon:oversized".into(), vec!["stone".into(); 8193])]); + // Content mods' blocks join shared tags: the cap is 2^20 blocks. + let tags = BTreeMap::from([("horizon:large".into(), vec!["stone".into(); 8193])]); + assert!(StateProviderBindings::new(®istry, BTreeSet::new(), &tags).is_ok()); + let tags = BTreeMap::from([( + "horizon:oversized".into(), + vec!["stone".into(); (1 << 20) + 1], + )]); assert!(StateProviderBindings::new(®istry, BTreeSet::new(), &tags).is_err()); let tags = BTreeMap::from([("tag".into(), vec![]), ("minecraft:tag".into(), vec![])]); assert!(StateProviderBindings::new(®istry, BTreeSet::new(), &tags).is_err()); diff --git a/crates/horizon-world/tests/world_io.rs b/crates/horizon-world/tests/world_io.rs index 65b5006a8..3dd897794 100644 --- a/crates/horizon-world/tests/world_io.rs +++ b/crates/horizon-world/tests/world_io.rs @@ -74,6 +74,70 @@ fn external_threshold_negative_coordinates_and_timestamps_roundtrip() { assert!(reader.read(ChunkPos::new(0, 0)).is_err()); } +#[test] +fn in_place_writes_reuse_freed_sectors_and_keep_other_chunks() { + let dir = Directory::new(); + let (a, b, c) = ( + ChunkPos::new(-32, 0), + ChunkPos::new(-31, 0), + ChunkPos::new(-1, 31), + ); + let mut region = RegionFile::open_writable(dir.region(), true).unwrap(); + // RegionBitmap: a at sectors 2..4, b at 4. + region.write(a, &record(2 * 4096 - 5)).unwrap(); + region.write(b, &record(100)).unwrap(); + let sectors = |path: &Path| fs::metadata(path).unwrap().len() / 4096; + assert_eq!(sectors(&dir.region()), 5); + // a grows: it moves past b, and its two old sectors are free again. + region.write(a, &record(3 * 4096 - 5)).unwrap(); + assert_eq!(sectors(&dir.region()), 8); + // c fits in a's old sectors; then an oversized c goes to its .mcc file + // (a one-sector stub, at the end: sector 8) and back inline (sector 2). + region.write(c, &record(2 * 4096 - 5)).unwrap(); + assert_eq!(sectors(&dir.region()), 8); + region.write(c, &record(256 * 4096)).unwrap(); + assert!(dir.0.join("c.-1.31.mcc").exists()); + region.write(c, &record(10)).unwrap(); + assert!(!dir.0.join("c.-1.31.mcc").exists()); + assert!(region.clear(b, 7).unwrap()); + assert!(!region.clear(b, 7).unwrap()); + region.sync().unwrap(); + drop(region); + let mut reader = RegionFile::open(dir.region()).unwrap(); + assert_eq!(reader.chunks().map(|(p, _)| p).collect::<Vec<_>>(), [a, c]); + assert_eq!( + reader.read(a).unwrap().unwrap().compressed, + record(3 * 4096 - 5).compressed + ); + assert_eq!( + reader.read(c).unwrap().unwrap().compressed, + record(10).compressed + ); + assert!(reader.read(b).unwrap().is_none()); + // Reopened, the bitmap holds the chunks' sectors: new chunks take the + // first free runs (sectors 3 and 4), not theirs. + let mut region = RegionFile::open_writable(dir.region(), false).unwrap(); + region.write(b, &record(4096 - 5)).unwrap(); + region + .write(ChunkPos::new(-30, 0), &record(4096 - 5)) + .unwrap(); + drop(region); + let mut reader = RegionFile::open(dir.region()).unwrap(); + assert_eq!( + reader.read(a).unwrap().unwrap().compressed, + record(3 * 4096 - 5).compressed + ); + assert_eq!( + reader.read(c).unwrap().unwrap().compressed, + record(10).compressed + ); + assert_eq!( + reader.read(b).unwrap().unwrap().compressed, + record(4096 - 5).compressed + ); + assert_eq!(sectors(&dir.region()), 9); +} + #[test] fn failed_writer_cannot_commit_and_removes_its_own_external_files() { let dir = Directory::new(); @@ -206,3 +270,57 @@ fn chunk_storage_edit_preserves_unknown_fields_and_other_blocks() { let record = ChunkRecord::from_nbt(&root, Compression::Lz4, 17, Limits::default()).unwrap(); assert_eq!(record.nbt(Limits::default()).unwrap(), root); } + +/// Saving one chunk into a full region: the whole file rewritten (every +/// chunk read and written again, then the new file published), against +/// vanilla's in-place write. Run with --ignored --nocapture. +#[test] +#[ignore = "microbenchmark; run with --ignored --nocapture"] +fn benchmark_saving_into_a_full_region() { + let dir = Directory::new(); + let chunks: Vec<ChunkPos> = (0..1024) + .map(|i| ChunkPos::new(-32 + i % 32, i / 32)) + .collect(); + // About what a compressed overworld chunk weighs: two sectors. + let saved = record(6000); + let mut region = RegionFile::open_writable(dir.region(), true).unwrap(); + for &c in &chunks { + region.write(c, &saved).unwrap(); + } + region.sync().unwrap(); + drop(region); + let rounds = 100; + let start = std::time::Instant::now(); + for round in 0..rounds { + let changed = chunks[round * 7 % 1024]; + let mut existing = RegionFile::open(dir.region()).unwrap(); + let mut records = Vec::new(); + for (p, _) in existing.chunks().collect::<Vec<_>>() { + let r = if p == changed { + saved.clone() + } else { + existing.read(p).unwrap().unwrap() + }; + records.push((p, r)); + } + let staging = dir.0.join("staging"); + fs::create_dir_all(&staging).unwrap(); + let temporary = staging.join("r.-1.0.mca"); + let _ = fs::remove_file(&temporary); + let mut writer = RegionWriter::create(&temporary).unwrap(); + for (p, r) in &records { + writer.write(*p, r).unwrap(); + } + writer.finish().unwrap(); + fs::rename(&temporary, dir.region()).unwrap(); + } + let rewrite = start.elapsed() / rounds as u32; + let mut region = RegionFile::open_writable(dir.region(), false).unwrap(); + let start = std::time::Instant::now(); + for round in 0..rounds { + region.write(chunks[round * 7 % 1024], &saved).unwrap(); + region.sync().unwrap(); + } + let in_place = start.elapsed() / rounds as u32; + println!("one chunk into a full region: rewrite {rewrite:?}, in place {in_place:?}"); +} diff --git a/docs/README.md b/docs/README.md index 7cf322042..ffe8e3654 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,7 +6,8 @@ To play, use [the launcher](../launcher/README.md); the project site is [playhor - [Build from source](../README.md#build-from-source): your own copy of the game, resource preparation, `cargo run`. - [Native client](native-client.md): options, controls, the dedicated server, `horizon-ctl` scripting, packaging, architecture. -- [Hosting a server](server.md): `horizon-server`, the design and the plan: install, `server.toml`, the CLI, the web panel, backups, services, Docker, and the bot benchmark against vanilla. +- [Host a server](hosting.md): install `horizon-server`, prepare the game files, first run, `server.toml`, the commands, the web panel, backups, services and Docker; what works today and what doesn't yet. +- [Hosting a server: design](server.md): `horizon-server`, the design and the plan: install, `server.toml`, the CLI, the web panel, backups, services, Docker, and the bot benchmark against vanilla. - [Agent control](agent-control.md): what AI agents can drive in the launcher and the game, and the design of the control socket, `horizon-ctl` and MCP. - [Graphics](graphics.md): the Simple and Advanced graphics settings, the graphics benchmark and what it measured. - [Benchmark](benchmark.md): Horizon against vanilla 26.3 and vanilla with performance mods, on the same machine, world, path and settings: what is measured, how it is kept fair, how to reproduce it. @@ -45,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. diff --git a/docs/collectors.md b/docs/collectors.md index 6be57f8be..98b497328 100644 --- a/docs/collectors.md +++ b/docs/collectors.md @@ -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. diff --git a/docs/graphics.md b/docs/graphics.md index a55313bea..0d3de5b1e 100644 --- a/docs/graphics.md +++ b/docs/graphics.md @@ -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` | @@ -25,13 +25,31 @@ Every row has a one-line description and its cost in frame time (none, low, medi | Performance | battery mode, worker threads, upload budget per frame | `horizon-graphics.txt` | | Overlay | performance overlay: FPS, frame/CPU/GPU times, frame-time graph | `horizon-graphics.txt` | -`options.txt` keeps exactly vanilla's keys and values, so the options harness still compares it with vanilla; Horizon's settings live in `horizon-graphics.txt` beside it. Their defaults render as vanilla does: cave culling on, no upload budgets, sky, fog and weather on, 100% resolution, no FXAA, the present mode following VSync. Frame pacing defaults to Efficient: the limiter sleeps through the last half millisecond where vanilla spins, which changes timing, not pixels. +`options.txt` keeps exactly vanilla's keys and values, so the options harness still compares it with vanilla; Horizon's settings live in `horizon-graphics.txt` beside it. Their defaults render as vanilla does: cave culling on, no upload budgets, sky, fog and weather on, 100% resolution, no FXAA, the present mode following VSync. VSync on always waits for the display (Fifo), in a window and in fullscreen, as in vanilla; the present mode setting chooses how frames are shown only while VSync is off. Frame pacing defaults to Efficient: the limiter sleeps through the last half millisecond where vanilla spins, which changes timing, not pixels. **Battery mode** (Performance, off by default) is for laptops and handhelds: at most 60 frames per second in a world, 30 in menus without a world or while another window has focus, and half the automatic worker threads (chunks load slower). It changes only when frames are drawn: what they show and the game's 20 ticks per second stay as they are. 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 diff --git a/docs/hosting.md b/docs/hosting.md new file mode 100644 index 000000000..255b473a2 --- /dev/null +++ b/docs/hosting.md @@ -0,0 +1,350 @@ +# Host a server + +`horizon-server` hosts a Minecraft Java Edition 26.3 server with Horizon's own game as the engine: one native +program, no Java, one settings file, a command line, and a web panel. Vanilla 26.3 clients and Horizon clients +can join it. This page covers what works on `main` today and says plainly what doesn't yet. The design and the +plan are in [docs/server.md](https://github.com/ludovic111/horizon/blob/main/docs/server.md). + +## Before you start + +- **Online mode checks accounts** (release v2026.10.17.07 and newer). With `online_mode = true`, the + default, players who sign in with the official Minecraft launcher join with their own accounts, as on a + vanilla server. +- **Names you add are accounts** (release v2026.10.17.14 and newer). `init --player`, `horizon-server + whitelist add` and `op`, and the console's `whitelist add`, `op` and `ban` look a name up the way vanilla + does: players already seen (kept in `usercache.json`), then Mojang's profile service. In online mode a name + with no account is refused. So you can whitelist friends before they first join. +- **Horizon's own client can't sign in to a Microsoft account yet.** That needs an Azure app registration and + Mojang's approval, which the project doesn't have + ([docs/microsoft-account.md](https://github.com/ludovic111/horizon/blob/main/docs/microsoft-account.md)). + Until then Horizon players can join offline servers only (`online_mode = false`). An offline server never + checks accounts: anyone who reaches it can join under any name. Keep it on your LAN or a tailnet + (Tailscale, ZeroTier), or share it only with friends you trust. +- **Secure chat** works as on vanilla's server: in online mode, vanilla clients sign their chat, and with + `enforce-secure-profile=true` (the default) unsigned chat is refused. On an offline server, or for a player + without a chat session (Horizon's own client today), chat stays unsigned and shows as not secure. +- **Nether and End:** each player travels on their own through portals, and operators with `execute in + <dimension> run tp`. The players in the level they leave stop seeing them, and the ones in the level they + arrive in start to, as on vanilla (checked against vanilla with two players, by command and through an End + portal: `tools/test_server_dimension_players_parity.py`). The console can't send another player to a + dimension yet (`execute in ... run tp NAME`). +- **Movement checks and chunk pacing** (release v2026.10.17.16 and newer). The server pulls back players who + move too fast ("moved too quickly"), as vanilla does, and sends chunks as fast as each client acknowledges + them. +- **No Docker image is published.** You build it yourself from the repository (below). Publishing an image + needs the project owner. +- Nothing of Mojang's is downloaded or shipped. The server reads the game files you prepare from your own copy + of Minecraft 26.3. + +## 1. Install + +Linux (x86-64) and macOS (Apple Silicon): + +```sh +curl -fsSL https://raw.githubusercontent.com/ludovic111/horizon/main/packaging/server/install.sh | sh +``` + +Windows (x64), in PowerShell: + +```powershell +irm https://raw.githubusercontent.com/ludovic111/horizon/main/packaging/server/install.ps1 | iex +``` + +The script downloads the latest Horizon release for your computer from GitHub, checks it against the SHA-256 +GitHub publishes, and never asks for sudo or administrator rights. It installs: + +- the release in `~/.local/share/horizon/releases/<version>` (Windows: `%LOCALAPPDATA%\Horizon`), with + `current` pointing at the one in use; +- `horizon-server` and `horizon-prepare` in `~/.local/bin` (Windows: added to your PATH); +- a `game-files` folder for the files you prepare, kept across updates. + +Options: `--version vX.Y.Z`, `--dir FOLDER`, `--bin FOLDER` (Windows: `-Version`, `-Dir`). Other systems, +such as Linux on ARM, build from source: `cargo build --release -p horizon-client --bin horizon-client -p +horizon-server` and keep both programs in one folder. + +The Horizon launcher also installs releases. Every release folder holds `horizon-server`, so you can run it +from there too. + +## 2. Prepare the game files + +Horizon reads Minecraft's data from your own copy of the game. You need Minecraft Java Edition 26.3, played +once from the official launcher on this computer, Java 25 (a JDK), and the 26.3 `server.jar`: it's the +`downloads.server.url` in your launcher's `versions/26.3/26.3.json`. Then: + +```sh +horizon-prepare --prepare --server-jar /path/to/server.jar +``` + +Add `--minecraft-dir PATH` if your launcher keeps the game elsewhere. The prepared files go to +`game-files`, which updates keep. + +**On a headless machine** (a VPS, a home server), prepare on your own computer instead, with the launcher or +`horizon-prepare`, and copy the prepared `26.3` folder to the server: into +`~/.local/share/horizon/game-files/horizon-client/26.3`, or into `resources/` in the server folder, or anywhere +`[engine] resources` in `server.toml` names. A folder copied this way needs release v2026.10.17.07 or +newer: older ones don't keep `registries.json` and `commands.json` with the prepared files, and the +server then stops with "registries.json not found next to the resources". Prepare again with a newer release. + +## 3. First run + +```sh +mkdir my-server && cd my-server +horizon-server init +``` + +`init` asks five things and writes `server.toml`: + +1. whether you agree to the [Minecraft EULA](https://aka.ms/MinecraftEULA): only a yes writes `eula=true`; +2. the world's name and an optional seed (an existing world or vanilla server folder is kept and its + `server.properties` imported); +3. the port, 25565 by default; +4. whether to use a whitelist (yes by default, as in vanilla) and who to add; +5. whether to make the first player an operator. + +It then tells you where it found the game and its files. Every question has a flag, so it also runs from a +script: + +```sh +horizon-server init --eula --world survival --seed 12345 --port 25565 --whitelist --player Alex --op Alex --yes +``` + +`--no-whitelist` turns the whitelist off, and `--offline` turns online mode off (see "Before you start"). +The players you add are looked up as Minecraft accounts in online mode, and get offline UUIDs on an offline +server, as vanilla does. + +Start the server in the foreground, with the console on your terminal (Ctrl-C stops it cleanly): + +```sh +horizon-server run +``` + +or in the background: + +```sh +horizon-server start +horizon-server status +``` + +```text +Running, up 29 s +TPS 20.0, MSPT 1.0 ms average, 3.1 ms P95 +0 of 20 players, 377 chunks loaded +RAM 1.42 GB, CPU 2 % of a core +``` + +Players join at your computer's address and the port (`192.168.1.20:25565`, or a tailnet name). Every +command takes `--dir FOLDER` to work on a server folder from elsewhere. + +## 4. Settings: `server.toml` + +You edit one file. The server writes vanilla's `server.properties` from it before every start, so the game +reads exactly what a vanilla server reads, and a vanilla server folder works as it is. + +```toml +[server] +motd = "A Minecraft Server" +port = 25565 +bind = "" # listen on every address, or only on this one (server-ip) +max_players = 20 +online_mode = true # check accounts with Mojang (see "Before you start") +whitelist = true +enforce_whitelist = false +view_distance = 10 +simulation_distance = 10 + +[world] +name = "world" +seed = "" # empty: a random seed on the first start +game_mode = "survival" +difficulty = "easy" +hardcore = false + +# Any other server.properties key, spelled as vanilla spells it: +[properties] +# spawn-protection = 16 + +[engine] +path = "" # the game program; empty: the one beside horizon-server +resources = "" # the prepared game files; empty: resources/ here, else the usual places +restart_on_crash = true + +[backups] +every = "6h" # 30m, 6h, 1d...; "0" turns automatic backups off +keep = 12 # automatic backups kept; backups made by hand are never deleted + +[panel] +bind = "127.0.0.1:8130" # this computer only, or a tailnet address (100.x); "" turns it off +allow_public = false +``` + +The defaults are vanilla 26.3's. Unknown keys and values vanilla would refuse are errors, so a typo stops the +start instead of being ignored. When the game changes a setting itself (`/whitelist on`), the change is copied +back into `server.toml` when it stops, and your comments are kept. Changes to the file apply at the next +restart: there is no live reload yet. + +Already have a vanilla server? Run `horizon-server init` in its folder, or `horizon-server import +server.properties` to write `server.toml` from it. The world, `ops.json`, `whitelist.json` and the ban lists +keep vanilla's names and formats, so going back to vanilla works too. + +## 5. Commands + +```text +horizon-server init first run +horizon-server run in the foreground, with the console; Ctrl-C stops it +horizon-server start | stop | restart in the background (its output goes to logs/supervisor.log) +horizon-server status [--json] up or not, TPS, MSPT, players, chunks, RAM, CPU +horizon-server cmd COMMAND... one console command, and what it answers +horizon-server logs [-f] [-n N] the log; -f follows it +horizon-server op|deop NAME +horizon-server whitelist on|off|list +horizon-server whitelist add|remove NAME +horizon-server backup [NAME] back up now +horizon-server backups list the backups +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 +console commands do. While it's stopped, they edit vanilla's files directly. Any console command works through +`cmd`: `horizon-server cmd ban Griefer`, `horizon-server cmd say Restarting in 5 minutes`. + +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. + +**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. 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 +``` + +prints a login link that works once, for 10 minutes. The panel listens on `127.0.0.1:8130`, this computer +only. To reach it from your other devices: + +- put your tailnet address in `[panel] bind` (for example `bind = "100.101.102.103:8130"`), or +- forward the port over SSH: `ssh -L 8130:127.0.0.1:8130 your-server`, then open the link on your computer. + +Any other address, such as `0.0.0.0` or a public IP, is refused unless you set `allow_public = true`, and +even then the panel warns at every start. It has no passwords to leak: whoever can read the server folder can +make a login link, and no one else can. Never put it on the open internet. + +## 7. Backups + +```sh +horizon-server backup before-the-update +horizon-server backups +horizon-server restore before-the-update +``` + +A backup zips the world, vanilla's files and `server.toml` into `backups/`. A running server saves first and +stays up: players don't notice. Automatic backups run on the `[backups] every` schedule while the server runs, +and the oldest automatic ones beyond `keep` are deleted. Backups you make by hand are never deleted. + +`restore` takes a backup's file name, a unique start of it, or the name you gave it. It moves the current +world aside to `backups/replaced-<time>/` (never deleted), puts the backup in place, and restarts the server if +it was running. The panel does the same with one click. + +## 8. Start it with the computer + +```sh +horizon-server service install +``` + +- **Linux:** a systemd user unit (`--system` for a system unit, with sudo). To keep a user unit running while + you're logged out: `loginctl enable-linger $USER`. +- **macOS:** a launchd agent in `~/Library/LaunchAgents`. +- **Windows:** a task that starts at logon (a real Windows service comes later). + +The service runs `horizon-server run` through the installed `current` release, so an update applies at the +next restart. `service print` shows what it would install, `service uninstall` removes it and leaves the server +folder alone. + +## 9. Docker (build it yourself) + +No image is published: build one from the repository. The image holds none of Minecraft's files; you mount the +folder you prepared (step 2) read-only. + +```sh +git clone https://github.com/ludovic111/horizon && cd horizon +docker build -f packaging/server/Dockerfile -t horizon-server . + +docker run -d --name horizon -p 25565:25565 --stop-timeout 180 \ + -v horizon-data:/data \ + -v /path/to/26.3:/opt/horizon/.local/horizon-client/26.3:ro \ + -e HORIZON_EULA=true -e HORIZON_WHITELIST=true -e HORIZON_PLAYERS=Alex,Steve -e HORIZON_OP=Alex \ + horizon-server +``` + +- `HORIZON_EULA=true` accepts the [Minecraft EULA](https://aka.ms/MinecraftEULA) for this server. Without it + the container stops and says why. +- The first start writes `/data/server.toml` from `HORIZON_EULA`, `HORIZON_WORLD`, `HORIZON_SEED`, + `HORIZON_PORT`, `HORIZON_WHITELIST`, `HORIZON_PLAYERS`, `HORIZON_OP` and `HORIZON_ONLINE`. Later starts use + the file. +- The mounted folder must come from release v2026.10.17.07 or newer (see step 2). +- Manage it with `docker exec horizon horizon-server status` (and `cmd`, `backup`, `logs`, ...). + `docker stop` saves and stops cleanly; `--stop-timeout 180` gives it the time. +- The panel stays on 127.0.0.1 inside the container. +- The image still carries ALSA and udev libraries because the game links them, even though a server never plays + a sound. A smaller engine without them is planned. + +## 10. How fast is it + +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.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. + +For more than 50 active players, give it more cores. + +## When something goes wrong + +- **"no prepared game files"**: `init` and `run` list the folders they looked in. Prepare (step 2), or set + `[engine] resources`. +- **"registries.json not found next to the resources"**: the prepared folder is from an older release. Prepare + again with a newer one. +- **Nobody can join**: the whitelist is on by default (`horizon-server whitelist add NAME`), and the port must be + open in your firewall. On a tailnet, use the server's tailnet address. +- **It keeps restarting**: `horizon-server status` shows the last crash and `horizon-server logs -n 200` the log + before it. Crash reports are in `crash-reports/`. +- Report problems at <https://github.com/ludovic111/horizon/issues>, with the end of the log. diff --git a/docs/java-packs.md b/docs/java-packs.md index d747069f5..66ee402d3 100644 --- a/docs/java-packs.md +++ b/docs/java-packs.md @@ -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: diff --git a/docs/media/menu-options.png b/docs/media/menu-options.png index c12f5441d..c674f6845 100644 Binary files a/docs/media/menu-options.png and b/docs/media/menu-options.png differ diff --git a/docs/media/menu-pause.png b/docs/media/menu-pause.png index 025582f24..9c25724d2 100644 Binary files a/docs/media/menu-pause.png and b/docs/media/menu-pause.png differ diff --git a/docs/media/menu-title.png b/docs/media/menu-title.png index 49423d340..133f393bc 100644 Binary files a/docs/media/menu-title.png and b/docs/media/menu-title.png differ diff --git a/docs/media/menu-title.webp b/docs/media/menu-title.webp index db18a324d..cf41ccf67 100644 Binary files a/docs/media/menu-title.webp and b/docs/media/menu-title.webp differ diff --git a/docs/media/menu-worlds.png b/docs/media/menu-worlds.png index 74b064981..16f41104b 100644 Binary files a/docs/media/menu-worlds.png and b/docs/media/menu-worlds.png differ diff --git a/docs/media/menu-worlds.webp b/docs/media/menu-worlds.webp index ad887f225..b70168080 100644 Binary files a/docs/media/menu-worlds.webp and b/docs/media/menu-worlds.webp differ diff --git a/docs/media/nether.jpg b/docs/media/nether.jpg new file mode 100644 index 000000000..7450f1072 Binary files /dev/null and b/docs/media/nether.jpg differ diff --git a/docs/media/nether.webp b/docs/media/nether.webp index 06faf4b32..2f9cdc5c1 100644 Binary files a/docs/media/nether.webp and b/docs/media/nether.webp differ diff --git a/docs/media/night.jpg b/docs/media/night.jpg new file mode 100644 index 000000000..3484e6c83 Binary files /dev/null and b/docs/media/night.jpg differ diff --git a/docs/media/night.webp b/docs/media/night.webp index b0c245a02..fac8958da 100644 Binary files a/docs/media/night.webp and b/docs/media/night.webp differ diff --git a/docs/media/settings-simple.png b/docs/media/settings-simple.png index c12f5441d..c674f6845 100644 Binary files a/docs/media/settings-simple.png and b/docs/media/settings-simple.png differ diff --git a/docs/media/settings.webp b/docs/media/settings.webp index cba632760..9ad5252c9 100644 Binary files a/docs/media/settings.webp and b/docs/media/settings.webp differ diff --git a/docs/media/sunset.jpg b/docs/media/sunset.jpg new file mode 100644 index 000000000..ff67883c2 Binary files /dev/null and b/docs/media/sunset.jpg differ diff --git a/docs/media/sunset.webp b/docs/media/sunset.webp index 0245a534c..0a8b82762 100644 Binary files a/docs/media/sunset.webp and b/docs/media/sunset.webp differ diff --git a/docs/media/survival.jpg b/docs/media/survival.jpg new file mode 100644 index 000000000..b01017b6c Binary files /dev/null and b/docs/media/survival.jpg differ diff --git a/docs/media/survival.webp b/docs/media/survival.webp index fc16cdc9b..a53713eb0 100644 Binary files a/docs/media/survival.webp and b/docs/media/survival.webp differ diff --git a/docs/media/underwater.jpg b/docs/media/underwater.jpg new file mode 100644 index 000000000..9c8ee88f5 Binary files /dev/null and b/docs/media/underwater.jpg differ diff --git a/docs/media/underwater.webp b/docs/media/underwater.webp index 77ef97735..55aa643c8 100644 Binary files a/docs/media/underwater.webp and b/docs/media/underwater.webp differ diff --git a/docs/media/village.jpg b/docs/media/village.jpg new file mode 100644 index 000000000..63df8c04d Binary files /dev/null and b/docs/media/village.jpg differ diff --git a/docs/media/village.webp b/docs/media/village.webp index 800354dd2..40864e507 100644 Binary files a/docs/media/village.webp and b/docs/media/village.webp differ diff --git a/docs/modpacks.md b/docs/modpacks.md index 1d8609cef..814950597 100644 --- a/docs/modpacks.md +++ b/docs/modpacks.md @@ -236,12 +236,28 @@ How the game loads them, without touching vanilla's path: so the same mod set gives the same ids. Saves store block names, so ids never reach the disk. - Loaders that read only `data/minecraft/` (recipes, loot, tags) read every namespace, as vanilla does. With no mods there is only `minecraft`, so nothing changes. -- The overlay is cached like the resource-pack overlay and rebuilt when a mod file changes. -- **Limits.** Chunk sections store a block state in 16 bits: vanilla 26.3 has 35,723 states, - so mods may add 29,813. Past that the content root is refused with the states per mod - (Macaw's Doors, Fences, Paths and Trapdoors alone add 36,154: not all four fit). Block - sprites fill texture arrays of 2048 layers, four at most (8,192; vanilla takes 1,250, - identical textures share a layer). Wider state ids are the next step for big packs. +- The overlay is built on the first start with a set of mods and kept for later starts. A stamp + (`horizon-mod-content.stamp`) hashes the game executable, the base tables and every mod file's + size and mtime. Adding, removing or editing a mod, or a new build of Horizon, rebuilds it. For + ATM10's 40 data-tier mods the build takes about a minute of the first start on a busy machine. +- **Worldgen.** Mods' structures, structure sets, template pools, processor lists, configured and + placed features join the `worldgen/*.json` bundles. Their structure templates go under + `worldgen/structure/<ns>/`. Their biome tags are merged into `worldgen/biome_tags.json`, minus + the biomes and tags Horizon does not have. Mod biomes, noise and density functions are not + carried yet. +- **Limits.** Chunk sections size their global palette to the registry: 16 bits for vanilla's + 35,723 states (the same chunks, saves and packets as before), one more bit each time content + mods double it, up to 2^20 states (1,048,576). Past that the content root is refused with the + states per mod. ATM10's 42 carried mods take the registry to 193,762 states (18 bits). On the + network the width is the registry's: unchanged for vanilla clients as long as no content mod + is on, wider only between Horizons that share the same mods; a Horizon with content mods still + reads a vanilla server's 16-bit chunks. Block sprites fill texture arrays of 2048 layers, eight + at most (16,384; vanilla takes 1,250, identical textures share a layer; ATM10's 42 take 9,790). +- **Broken mod files load as vanilla loads them.** A blockstate definition that does not bind + (a multipart value the block does not have), a block model that is missing or does not parse, + and a recipe that does not parse are logged and skipped: the block gets no model or the + missing model, the recipe is absent, everything else loads. Vanilla's own files never hit + these paths. - Not in 0.2: multiplayer registry sync (single player and the integrated server first), worlds opened without a mod they used (vanilla turns unknown blocks into air; we will ask first), vanilla ids redefined by a mod (refused). @@ -267,16 +283,32 @@ inspected: └── coverage.json, coverage.md ``` -**Status.** `import`, `inventory`, `carry` and `report` run today: +**Status.** `import`, `inventory`, `fetch`, `carry` and `report` run today, and `port` runs +them all for what a player's machine can port (content mods of the data tier), as the +launcher's "Import modpack" does: ``` cargo run -p horizon-port-pack -- import ~/Downloads/pack.mrpack ~/.horizon/ports/pack cargo run -p horizon-port-pack -- inventory ~/.horizon/ports/pack # --offline reuses modrinth.json +cargo run -p horizon-port-pack -- fetch ~/.horizon/ports/pack # the portable mods' jars, or name them +# --from <folder>: copy the player's own jars (an installed instance, a downloads folder) cargo run -p horizon-port-pack -- carry ~/.horizon/ports/pack # content mods with a jar, or name them # --resources <prepared root>: only templates Horizon's tables have cargo run -p horizon-port-pack -- report ~/.horizon/ports/pack # coverage.json and coverage.md +cargo run -p horizon-port-pack -- port ~/Downloads/pack.zip ~/.horizon/ports/pack [--from <folder>] [--resources <root>] ``` +`fetch` gets each jar the player's way, never from us: copied from a folder they have (matched +by SHA-1, or through a CurseForge app instance's `minecraftinstance.json`), else downloaded from +the link the pack itself gives (a `.mrpack`'s, on the format's hosts only) or, for a CurseForge +pack, the same file on Modrinth's public CDN, found by its SHA-1. A jar is kept only when its +hash matches. A file only CurseForge has is listed as needing a CurseForge API key (Ludovic's +decision; open question below), with the player's installed instance as the way round it. +`<work>/fetch.json` says what happened to each file. On All the Mods 10 8.3's CurseForge zip +(2026-10-17): of the 54 data-tier content mods, 42 come from Modrinth's CDN (84 MB) and 12 need +the key or the instance; `port` then carries the 42 over (0 ported, 42 partial: the harness is a +dev step). + `inventory` reads metadata only: Modrinth answers by file hash, and the jars only if the user's instance has them (an imported instance folder, or jars put in `<work>/jars/`). Without jars, classes come from the reviewed table, Modrinth categories and sides; with jars, from their @@ -286,8 +318,12 @@ only): content 54, library 33, client-ui 26, performance 25, other 14, unknown 2 `carry` copies a jar's `assets/<id>/`, `data/<id>/`, `data/minecraft/tags/` and `data/c/tags/` into `mods/<id>/carry/`, a folder content mod (API 0.2) the game loads with `--dev-mod`, and -keeps what a content mod may not hold (other namespaces, vanilla data) in `outside/`. No class -file is copied. It upgrades from 1.21.1 to 26.3: vanilla-type recipes (ingredients), loot tables +keeps what a content mod may not hold (other namespaces, vanilla data) in `outside/`. Files of +the mod's own content that the game's content loader would refuse whole also go to `outside/`, +listed with the reason in the report's `unreadable`: paths vanilla does not read (upper case), +JSON that does not parse, and shared tags with `"replace": true`. Vanilla skips them one by one. +A byte-order mark is dropped. For ATM10's 42 mods that is 19 files in 7 mods, and the 42 load as +carried. No class file is copied. It upgrades from 1.21.1 to 26.3: vanilla-type recipes (ingredients), loot tables (`conditions` lists become one `condition`, `functions` become `modifier`, kinds move to `type`, tool matches on silk touch or shears become `minecraft:tool/can_silk_touch` and `minecraft:tool/can_shear`), and the `items/` definitions 1.21.4 introduced. @@ -310,7 +346,7 @@ block's name breaking ties (`_pane`, `_gate`, `_trapdoor`). | Format | Read | Jars come from | |---|---|---| | Modrinth `.mrpack` | `modrinth.index.json`: `dependencies` (minecraft, neoforge / fabric-loader / forge / quilt-loader), `files[]` with path, sha1/sha512, `env` (client/server), download URLs; `overrides/`, `client-overrides/`, `server-overrides/` | downloaded from the listed Modrinth URLs, hash-checked | -| CurseForge zip | `manifest.json`: `minecraft.version`, `modLoaders[]`, `files[]` (`projectID`, `fileID`, `required`), `overrides` | the user's installed instance (CurseForge app `minecraftinstance.json` + `mods/`). CurseForge's API needs a key, so we take what the user already downloaded | +| CurseForge zip | `manifest.json`: `minecraft.version`, `modLoaders[]`, `files[]` (`projectID`, `fileID`, `required`), `overrides`. When the overrides hold Crash Assistant's `config/crash_assistant/modlist.json` (All the Mods 10 does), its jar names, mod ids and SHA-1s replace the bare ids | the user's installed instance (CurseForge app `minecraftinstance.json` + `mods/`), or the same file on Modrinth by SHA-1. CurseForge's API needs a key, so we take what the user already downloaded | | Prism / MultiMC instance | `mmc-pack.json` components (`net.minecraft`, `net.neoforged`, `net.fabricmc.fabric-loader`), `instance.cfg`, `.minecraft/mods/*.jar`, packwiz `.pw.toml` files if present | the instance folder | | packwiz | `pack.toml`, `index.toml`, `mods/*.pw.toml` | as listed | | Crash Assistant `modlist.json` | jar name → mod id, name, version, Modrinth SHA-1; the loader entry (NeoForge `21.1.x` is Minecraft 1.21.1). Packs such as ATM10 publish it in their repository | metadata only; jars put in `<work>/jars/` | @@ -370,6 +406,177 @@ Mechanical, the same for every mod, and no agent: carries those over. 5. Write `<id>.hmod`. For a data-tier mod, it is now ported. +Two cases the oracle fills in: + +- **Loot registered in code.** Chipped ships 7 loot tables for about 7,000 blocks; the rest + drop themselves through Java code. When a declared block has no table in the jar and the + oracle shows vanilla's drop-self, carry writes that table in 26.3's shape. When only the + states with one property value drop it (a door's lower half), the entry gets vanilla's + `match_block` condition. Chipped: 6,843 tables added. +- **Worldgen carry-over.** Structure libraries give vanilla's types other names. Carry maps + these to vanilla's: + - `lithostitched:jigsaw`, MoogsStructureLib's and Repurposed Structures' generic jigsaws + become `minecraft:jigsaw`; + - their `advanced_random_spread` becomes `minecraft:random_spread`; + - their versioned, legacy and empty pool elements become vanilla's. + + Three more rewrites make the mapped structures decode and start where their mod would: + - **Block states.** 1.21.1 writes a block or fluid state as `{"Name", "Properties"}`, 26.3 + as `{"id", "properties"}` and refuses the old shape, which failed the whole processor + list, feature or pool. Carry rewrites them (#816; 1,005 files in 17 of ATM10's mods). + - **Start and placement rules** (#820 in the game, #821 in carry). Repurposed Structures' + and Moog's jigsaws refuse to start in liquid and check biomes and terrain height in a + radius; their spreads keep a minimum distance from the world origin and away from other + sets (named by a structure set tag). Carry writes these under `horizon:start_checks`, + `horizon:min_distance_from_world_origin` and `horizon:exclusion_zone`; the game runs them + (`horizon-world`'s `structure/start_checks.rs`). Vanilla data has none of the keys. Moog's + also spreads its structures 1.65 times as far apart as its files say, which carry now + applies. MVS went from 829 starts to 90 in a 128×128-chunk square (seed 1234): 294 from the + spacing, the rest from the start checks. + - **Processors written in Java** are taken out of their lists (#822). The game, like + vanilla, refuses a list holding a type it does not know, and with it every pool naming the + list. The structures now generate undecorated (no pillars under them, spawners as the + template has them); 1,427 processors of 146 types in ATM10's mods, the most common + `lithostitched:block_swap`, Formations' `biome_replacements` and Repurposed Structures' + pillar, fluid and void processors. + - **Spawner processors** carried as data (`horizon-port-pack/src/processors.rs`). Most + chests in the mods' templates already name their loot table; what the Java processors add + is the spawners' mobs. Moog's and Repurposed Structures' spawner randomizing processors + become `horizon:spawner_mobs` (a weighted mob per spawner, picked with the placement's + random as theirs do, and the spawner NBT they write; Repurposed's `rs_spawners` lists are + written in). YUNG's Better Dungeons and Better Desert Temples turn a spawner or a + placeholder block (gravel, bone block, yellow wool...) into a spawner of a fixed mob: that + is vanilla's `rule` processor with an `append_static` block entity. Moog's vanilla loot + swap acts only when a "replace vanilla" preset is on, and they ship off. Moog's trial + spawner and vault processors keep the block and change its block entity: one `rule` per + block state, with the game's `horizon:edit` modifier, which takes out the state the + template was saved with (a vault saved with `state_updating_resumes_at` far in the future + never opens; a spawner's saved `spawn_data` blocks its own mobs) and merges in the + spawner's normal and ominous configs (by id: the carry upgrades `trial_spawner/` files' + mobs to 26.3's `equipment`) or the vault's loot table and key. Better Desert Temples' + placeholder blocks (orange, red and white glass, blue and yellow concrete, red wool, + polished diorite, blank pots) become what its processors make of them: the fixed swaps as + vanilla's `rule`, the random ones (suspicious sand, sand and sandstone, upside-down stairs, + cobwebs, sherds on the pots) as the game's `horizon:pick`, on the same `nextFloat` as the + mod. Better Dungeons' small dungeons keep one chest and each other one with a fifth's + chance, two at most (the game's `horizon:count_cap`, counted per placement as the mod's + DungeonContext is): 12 chests in five small dungeons at seed 1234 become 6. + Lithostitched's `block_swap` (137 lists, all in ChoiceTheorem's Overhauled Village) is the + game's `horizon:block_swap`: the same map, with Block.withPropertiesOf's properties kept; + a target block the game does not have (most of them are Nature's Spirit's and Croptopia's) + leaves the block as it is, as the mod does. Lithostitched's `reference` (CTOV builds most + of its lists from others) has the referenced lists' processors written in its place; its + `apply_random`, `condition` and `set_block` are the game's `horizon:apply_random`, + `horizon:condition` and `horizon:set_block`, with Lithostitched's random (the world seed + plus the name's hash, at the block, piece or structure). In a CTOV plains village at seed + 1234 the farms now grow carrots, potatoes and beetroot besides wheat, and 1,655 + cobblestone blocks become paths, grass, mossy cobblestone and planks. No Lithostitched processor is + dropped now (130 before), only CTOV's compat hooks for other mods inside them (142 copies). + Still taken out: Formations' biome replacements, Repurposed's pillars and fluid fixes, + blocks aged or propped up. + In the 128×128-chunk square (seed 1234), every spawner of a mod structure now has its mob: + in the Overworld 83 Better Dungeons spawners held the template's placeholder pig and 17 of + Moog's were empty (763 spawners with a mob, up from 628); in the Nether the 52 empty ones + in Moog's Nether Structures are filled (162, up from 110). Chests are unchanged: 2,777 + and 1,405 with a loot table. + - **Rule tests** written in Java. Repurposed Structures' mineshafts and Nether monument test + blocks with three of the mod's own rule tests inside vanilla `rule` processors: what a + block is made of (solid, liquid, air, with rails and chains), a chance over the distance + from the template's origin along one axis, and a world height range. The game runs them + as `horizon:matter_phase`, `horizon:piece_origin_axis_aligned_linear_pos` and + `horizon:y_value` with the mod's fields, and carry renames them (213 in 75 lists). + - **Renamed ids.** 26.3 calls 1.21.1's chain the iron chain (its datafixers rename it in + worlds). Carry renames it in recipes, loot tables and worldgen files (112 files in 5 mods: + Repurposed's mineshafts, Better Dungeons' processors, Regions Unexplored's hanging sign + recipes...), which otherwise fail to load in 26.3. With both, the 5 Nether mineshaft pools load: 47 of + Repurposed's Nether mineshafts in the 128×128-chunk square (seed 1234), under the lava sea. + - **Big templates** (#838). Vanilla reads structure templates with no limit on their tag + count; Horizon now does too (`nbt::Limits::templates`). Seven of ATM10's 8,962 carried + templates are past the old 1,000,000-tag cap: YUNG's monument start, four When Dungeons + Arise main pieces and two Nullscape pieces. They used to load as empty templates, so their + structures never started. + - **Additions to other sets and pools** (#832 in the game, #833 in carry). A set or pool file + holding `horizon:add_to` appends its entries to the sets or pools it names (docs/mods.md). + Carry writes these from lithostitched's `add_structure_set_entries` and + `add_template_pool_elements` modifiers whose load conditions hold for the mods ported + along, and from CTOV's Java registration: its 21 village types and 11 outposts join + vanilla's `villages` and `pillager_outposts` with the mod's default weights. + - **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, 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 + structure turns off the vanilla type its mod's mixin turns off. It is written only when + the game can start the replacement, so vanilla mineshafts stay while Better Mineshafts (a + Java type) can't. + - **Repurposed Structures' and Moog's own assembler** (#850 in the game, #851 in carry). Both + lay out their jigsaws with `PieceLimitedJigsawManager`, not vanilla's. Their structures, the + Nether variants too, get `horizon:piece_limited`: `y_allowance`, burying, the pools whose + fallbacks ignore the bounds, liquid, the Nether land search and ledge, and each + 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, + and the types left as they are. NeoForge's convention biome tags (`#c:is_forest`, ...) come + from the loader jar (`<work>/loader/neoforge-<version>-universal.jar`, fetched dev-only and + SHA-1 checked) and are written into the carry. The game skips the structures it cannot + decode. + + On ATM10's 42 data-tier mods with a jar (seed 1234, chunks -64..63, `chunkgen_bench + --starts` over the carried worldgen): 286 starts with 15,097 pieces, 150 of them vanilla's + (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 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; + - 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 + overworld starts with 22,026 pieces. CTOV has 7, Better Dungeons 72 and Better Ocean + Monuments 3, and vanilla's monuments are gone as YUNG's mod has them. In the Nether there + are 512 starts, up from 322: Moog's Nether Structures 147, Repurposed's Nether structures + 40, Better Fortresses 7, with no vanilla fortress. Over chunks -256..255: Better Strongholds 10 + (vanilla 0), Better Jungle Temples 5, Better Witch Huts 11. Vanilla's own worlds are + unchanged: the structure-start oracle still matches 35 of 35. + + Repurposed's Nether city, Nether shipwrecks and End stronghold are its generic jigsaw with + one more start check each, carried as `minecraft:jigsaw` with the check named in + `horizon:start_checks.repurposed`: open air from sea level + 20 to + 45 around the city's + chunk, no solid block in a 33×30×33 grid over a shipwreck, and land 45 blocks up at nine + points 35 and 70 blocks apart for the stronghold. Over chunks -320..319 in the Nether there + are 2 cities and 45 shipwrecks (30 and 865 without the checks); the End stronghold keeps + 8,000 blocks from the centre, and chunks 500..1139 × -320..319 hold 2 (43 without). + + In a world with the 40 mods, seed 1234, `/locate structure mvs:azelea_house` finds one 27 + blocks from spawn and it stands there as the template builds it; When Dungeons Arise's + bandit towers and Structory's chapel locate too. + ### 5. Code: one agent per mod For tiers logic, machines and deep, in dependency order, as many agents at once as the AI @@ -445,12 +652,15 @@ user's jars are, and its facts and results stay in the work folder. The first re | Glassential-renewed 3.4.7 | 105/114 | 5177/5177 | 5177/5177 | 4727/5177 | 6447/6447 | 210/210 | 105/105 | 143/143 | 106/115 | | Factory Blocks 1.4.0 | 42/49 | 42/42 | 0/42 | 42/42 | 294/294 | 84/84 | 42/42 | 1/1 | 42/49 | | Cloud Glass 1.1.0 | 16/16 | 16/16 | 16/16 | 0/16 | 112/112 | 32/32 | – | 31/31 | 16/16 | +| Chipped 4.0.2 | 6857/6981 | 46212/46443 | 46443/46443 | 46396/46443 | 149093/149093 | 13548/13714 | 11984/12378 | 6/13 | 6849/6973 | What fails is what API 0.2 cannot express yet: shapes of their own (Japanese doors, hedges, paths a sixteenth high, glass without collision), sound events of the mod's own (`factory_blocks:metal_sound`), and blocks whose vanilla class has no vanilla block with the same states (garage doors, double gates, engraved and facing paths, fans). Every drop, recipe -and tag of the declared blocks matches. +and tag of the declared blocks matches. Chipped's 124 missing blocks are its own classes (workbenches and +`DirectionBlock`), and its workbench recipes need those blocks. Its drops match since carry writes +the self-drop tables it registers in code (step 4). ### 7. Coverage report @@ -492,9 +702,9 @@ files; `inventory`; `carry` and the harness on the eight mods we downloaded; `re By tier, the 367 not yet: data 57 (carry-over and the harness, no agent: their jars were not downloaded), logic 66 (API 0.3), machines 97 (0.4), deep 92 (0.5), client 35, unclassified 20. Ported: Macaw's Trapdoors. Partial: Macaw's Doors, Fences and Walls, Paths and Pavings, -Glassential, Factory Blocks, Cloud Glass (see the harness table) and MVS, whose 784 worldgen -files are carried but not yet read by the game. Of the 8, 7 are MIT and one LGPL; nothing is -published. +Glassential, Factory Blocks, Cloud Glass, Chipped (see the harness table) and MVS, whose +structures now generate (see step 4). Of the 9, 7 are MIT, one LGPL and Chipped is under the +Terrarium License; nothing is published. ## Measuring "10x" @@ -554,10 +764,86 @@ vanilla's 35,723; the per-state tables are the next thing to measure), while the inside its 2 GB heap. Caveats, so none of this is a "10x" claim yet: other sessions and CI runners kept the machine's load average between 30 and 56 on 14 threads, so the confidence intervals are as wide as the means (`perf_bench.py --table`); Horizon's frames are paced near -the display's 144 Hz at 8 chunks; Horizon's title time includes building the content overlay on -a first start (about 5 s, cached afterwards); and the pack runs 1.21.1 against Horizon's 26.3. +the display's 144 Hz at 8 chunks; Horizon's title time includes building the content overlay +(about 5 s for six mods; it was rebuilt on every start then, and is kept for later starts now); +and the pack runs 1.21.1 against Horizon's 26.3. A quiet rerun with more games is the next measurement. +**Where the RAM goes (2026-10-17).** RSS overstates it on macOS. `ps` counts pages the allocator +freed and marked reusable, and the content tables free a lot at load. `perf_bench.py` now also +samples the physical footprint (`footprint`, what the OS charges the process). The machine was +shared and very busy (load average 90-138 on 14 threads). RD 8, the same six mods, one or two +games per row: + +| game | RSS steady MB | footprint steady MB | +|---|---:|---:| +| Horizon | 1,598-1,636 | 1,277-1,295 | +| Horizon + 6 mods | 2,996-3,052 | 1,736-1,869 | + +So the six mods cost about 0.5 GB of footprint, not 1.1-1.4 GB. ATM10's 42 mods (193,762 +states) take the game to 7.7 GB RSS but 3.9 GB footprint. The rest is startup garbage the +allocator keeps. + +`crates/horizon-client/examples/resource_memory.rs` breaks it down per loader: the live heap +(a counting allocator), each step's peak, RSS and the footprint. Live heap after every loader, +vanilla against ATM10's 42 mods: + +| loader | vanilla MB | ATM10 42 mods MB | +|---|---:|---:| +| block registry | 24 | 125 | +| assets (registry, models, sprites: 153 MB of pixels for 9,790 layers) | 94 | 657 | +| block facts and behaviours | +29 | +75 | +| items, recipes | +14 | +82 | +| light, mesh, collision, motion | +24 | +53 | +| worldgen | +79 | +182 | +| **live** / **footprint** | **240 / 551** | **1,049 / 2,724** | + +The block registry kept a second copy of every state as the key of its id map; it now +indexes ids by a hash checked against the states: -22 MB live for vanilla, -124 MB for +ATM10's, -30 MB and -140 MB of footprint. The startup peaks are what is left. Each step parses +its per-state tables whole: as JSON trees, or as NBT trees of 300-600 MB each for ATM10 +(`block-properties.nbt`, the collision, support, occlusion and movement tables). The peak +reaches 3.9 GB during worldgen loading, and the footprint stays near the peak. + +The NBT tables now stream: the readers check each state's identity as it is read, and no +`states` tree is built (#753). That tree was about 240 MB for each table. With the carried +ATM10 mods (153,511 states), the peak of each step falls by about 210 MB: + +| step | before MB | after MB | +|---|---:|---:| +| light | 911 | 699 | +| collision | 945 | 733 | +| motion | 1,427 | 1,215 | +| worldgen, uncached | 3,000 | 2,788 | + +The fluid tables no longer decode the 211 MB `gameplay-facts.json` a second time (#754). The +content root is kept between starts (#756). What is left at startup: + +- `facts.json` went through a JSON tree, then an NBT tree, in both the registry and the assets + step. It now streams (#825): at ATM10's size (214,338 states) each step's peak fell from + 816 MB to 153 MB (registry) and 235 MB (assets), and the RSS after the assets step from + 1,344 MB to 442 MB; +- on a first start, the content-root build and `Behaviours`' decode of `gameplay-facts.json` + (cached afterwards in the behaviour snapshot). + +**Against NeoForge, same 40 mods (2026-10-17).** The 40 data-tier mods carried from ATM10 (the +42 without Regions Unexplored and MO Structures, whose libraries are only on CurseForge), +against their jars on NeoForge 21.1.259 with `-Xmx2G`. `perf_bench.py --port-work`, RD 8, one +game each, load average 80-125 on 14 threads; `--warm` measures a later start: + +| game | title / world s | RSS steady / peak MB | footprint steady / peak MB | +|---|---|---|---| +| Horizon before #756 (every start) | 81.1 / 102.8 | 4,008 / 4,164 | 2,368 / 2,554 | +| Horizon, first start | 105.2 / 128.8 | 3,795 / 3,993 | 2,306 / 2,560 | +| **Horizon, later starts** | **21.6 / 33.7** | **2,629 / 2,935** | **2,445 / 2,731** | +| NeoForge + 40 mods | 53.2 / 119.9 | 3,099 / 3,203 | 3,304 / 3,381 | + +On later starts, Horizon uses 15 % less RSS and 26 % less footprint than NeoForge with the +same mods. It reaches the title 2.5x sooner and the world 3.6x sooner, and its flights ran at +93-233 FPS against NeoForge's 27-62. The first start is slower than NeoForge's to the title, +because it builds the content root once. One game per row on a loaded machine is not a +benchmark; a quiet rerun with repeats is still to do. + ## The working version for 2026-10-23 In scope (each is a PR or a small stack): @@ -583,11 +869,9 @@ In scope (each is a PR or a small stack): 6. **Measured:** load time, RAM and FPS of that pack on NeoForge 1.21.1 against Horizon, with the full ATM10 on NeoForge as the reference row. -Out of scope for that date, and next in order: worldgen carry-over (the 30 or so structure and biome mods; -the first one looked at, Moog's Voyager Structures, declares all 130 structures and 116 structure -sets with MoogsStructureLib's own `generic_jigsaw_structure` and `advanced_random_spread` types and -NeoForge's `c:` biome tags, so its carry needs a mapping to vanilla's jigsaw and random spread -types plus the oracle's biome tags, not only bundle merging), +Out of scope for that date, and next in order: the rest of worldgen carry-over (mods' +structures, sets, pools and features generate now, see step 4; mod biomes, noise and density +functions, and the structure types written in Java such as YUNG's and Formations', do not yet), behaviour callbacks and server events (0.3), block entities, menus, capabilities and net (0.4), the first agent-ported logic-tier mod as the pilot of step 5, then machines. Porting all of ATM10 is a campaign. Each step publishes its coverage number; we make no claim beyond what diff --git a/docs/mods.md b/docs/mods.md index d1556abd7..97abb36aa 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -287,6 +287,39 @@ its row. The game reads this content into its block and item tables nobody registers (a block a port left out, another mod's item) is dropped, so the rest of the tag still loads; vanilla would drop the whole tag. +A mod adds to structure sets and template pools that are not its own +(vanilla's villages, another mod's houses) with a set or pool file in its own +namespace holding `horizon:add_to`, a key vanilla data never has. Its entries +are appended to each set or pool named, after every mod's own files, in mod +and file order; the file is no set or pool itself. A name nobody defines is +skipped. + +```json +{ "horizon:add_to": "minecraft:villages", + "structures": [ { "structure": "mymod:village_beach", "weight": 10 } ] } +``` + +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 diff --git a/docs/native-client.md b/docs/native-client.md index 4911c8197..fcf28246a 100644 --- a/docs/native-client.md +++ b/docs/native-client.md @@ -109,7 +109,7 @@ game accepts, test hooks included. - **Observe:** `state` (position, rotation, velocity, health, food, saturation, XP, effects, game mode, dimension, time, weather, the screen and what the crosshair points at), `looking-at`, `entities [RADIUS] [TYPE]` (mobs with - ids and health, dropped items, orbs), `block X Y Z`, `blocks X1 Y1 Z1 X2 Y2 Z2`, + ids and health, dropped items, orbs), `block X Y Z`, `blocks X1 Y1 Z1 X2 Y2 Z2`, `light X1 Y1 Z1 X2 Y2 Z2`, `find-block BLOCK [RADIUS]`, `inventory` (stacks with their components as SNBT), `screen` (the open screen or title menu, its buttons and slots) and `messages` (chat, title, subtitle and action bar). diff --git a/docs/noise-aquifer-benchmark.md b/docs/noise-aquifer-benchmark.md new file mode 100644 index 000000000..a68f18f2d --- /dev/null +++ b/docs/noise-aquifer-benchmark.md @@ -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. diff --git a/docs/performance/paletted-container-access-results.md b/docs/performance/paletted-container-access-results.md new file mode 100644 index 000000000..08783d290 --- /dev/null +++ b/docs/performance/paletted-container-access-results.md @@ -0,0 +1,73 @@ +# Paletted container access + +The `unpack`/`get` cell reader and `BlockSection::get`/`set` are hot in the +chunk workers: light and mesh workers unpack a section's 4,096 cells before +propagating or meshing, and world-generation workers write one block at a time +through `BlockSection::set`. + +The write path resolved the old cell and probed the palette three times; the +bulk readers dispatched the palette per cell; and the storage layer revalidated +indices and values a section had already checked. All three are addressed. + +## Changes + +`BlockSection::set` (`crates/horizon-world/src/chunk.rs`) previously resolved the +old cell through `get` and then probed the section's palette map three times for +one non-trivial write: once to key the old state by id (`local_ids[&old]`), once +to test the incoming state (`contains_key`), and once to find it (`get`). The +slot at the written index already *is* the old state's id, so the old state is +now read straight from storage, and the incoming state is resolved with a single +map probe. The palette-reuse, free-slot and growth paths are unchanged, so the +encoded palette, counts, free list and NBT output stay identical. + +`BlockSection::get` and `set` validate the section index and the state id once, +then read and write through `PackedStorage::read`/`write` +(`crates/horizon-core/src/storage.rs`). These skip the storage-level bounds and +value checks and the `Result` that `PackedStorage::get`/`set` keep for external +callers: a section index is below 4,096 and a palette id always fits the width +`bits_for_palette` chose. Both new methods `debug_assert` their contract and are +used for the hot read, the hot write and the palette-growth re-encode. + +`PalettedContainer::unpack` and `unpack_u16` +(`crates/horizon-core/src/paletted.rs`) matched on the palette variant inside the +per-cell loop. `Single`, `List` and `Global` are now dispatched once before the +loop, leaving each arm a branch-free indexed loop. + +No allocation, layout or wire format changes. + +## Reproduce + +```sh +cargo test -p horizon-world --lib --profile fast benchmark_section_get_set -- --ignored --nocapture +cargo test -p horizon-core --lib --profile fast benchmark_unpack -- --ignored --nocapture +``` + +The ignored tests compare the new code against copies of the pre-optimization +implementations, kept in the test modules (`baseline_get`/`baseline_set`, +`baseline_unpack`). They check every resulting palette, count, free list, cell +and NBT before timing. Rounds alternate baseline/optimized order with `black_box` +and report the median and best nanosecond- or microsecond-per-operation; the best +round is the estimate of uninterrupted CPU time on a contended machine. + +`benchmark_section_get_set` uses a 32-state registry and 50,000 random writes and +reads per round over a section whose palette grows through the 4- and 5-bit +widths (24 distinct states); 51 rounds. `benchmark_unpack` fills a 4,096-entry +list palette (12 states) and the global palette, 2,000 calls per round, 25 +rounds. + +AMD Ryzen 5 5600X, Linux x86_64, rustc 1.99.0, optimized `fast` profile, +nonincremental, no LTO: + +| Operation | Original main | New | Speedup | +| --- | ---: | ---: | ---: | +| `BlockSection::set` | 13.15 ns/write | 9.12 ns/write | 1.44x | +| `BlockSection::get` | 2.33 ns/read | 1.45 ns/read | 1.61x | +| `unpack`, list palette | 1.910 us/call | 1.834 us/call | 1.04x | +| `unpack`, global palette | 2.270 us/call | 2.179 us/call | 1.04x | + +The distinct-state differential test +`chunk::tests::set_matches_baseline_under_random_edits` drives the optimized +`set` and `get` and the originals over the same 50,000-edit streams for 1, 2, 16, +17, 24 and 64 distinct states, including periodic whole-section collapses that +clear the storage and reclaim palette slots, and asserts equal results, bits, +palette, counts, free list, `local_ids` and NBT throughout. diff --git a/docs/performance/startup-stage-3.md b/docs/performance/startup-stage-3.md new file mode 100644 index 000000000..28013e87c --- /dev/null +++ b/docs/performance/startup-stage-3.md @@ -0,0 +1,39 @@ +# Progressive world resources (#712) + +Stage 3a changes shared resource storage and accessors. Pack resources keep their existing per-game ownership for pack reloads. Stage 3b publishes core before the simulation tail, builds the real renderer using the previous device after releasing its surface, and gates backdrop streaming on that renderer. Stage 3c queues an early Play or Connect action and draws the existing level-loading progress screen while polling the worker each event-loop turn. Close and resize events continue to work. After the worker completes, the queued local/server action resumes; integrated servers receive the resource handle only after that join. The synchronous harness and dedicated path returns only fully loaded resources. + +Startup logs distinguish `core-ready`, `world renderer ready`, `starting title backdrop`, and `rest-ready`. Opt-in performance summaries carry `to_core_ms` and `to_rest_ms` separately from the existing UI-driven `to_title_ms`. Loader failures and panics propagate to the client; a tail taking more than 120 seconds fails rather than waiting indefinitely. + +## Stage 3d: interactive menu before core + +An ordinary launch now owns a `StartupMenu` inside `Loading`. The first menu atlas and panorama message makes the existing title screens interactive; assets, facts, biomes, pack tables, collision and motion continue on the resource worker. Pack-overlay and mod-content preparation also run on that worker. The live landscape still starts only after the core renderer exists. + +World browsing, world creation, server address entry and options use the existing title/options widgets. Play and Connect queue a `PendingWorldEntry`; the progress screen covers both the core and tail waits. Escape cancels the queued entry in either phase. Close and resize continue to work. The handoff transfers the title object, options screen, settings, cursor, clipboard selection, timing session and pending action instead of rebuilding the user's screen. + +Pack changes during core loading request another loader generation. The old core and tail finish before replacement begins so cache writes and tail registry initialization cannot overlap. Each generation has its own message channel and progress counter. Language/GUI metadata publication stays on the event-loop thread, using the current language selection rather than the worker's stale settings snapshot. Pack changes after the core handoff also wait for the tail before touching mob metadata. + +The menu still needs its own artwork and renderer before interaction. The core handoff still builds the full renderer and starts audio on the main thread; menu click sounds and controller navigation start with `Game`. This stage does not change synchronous harness/dedicated startup or introduce a render-facts export. + +Regression coverage publishes menu artwork while a fake core worker is blocked, edits a world draft, replaces fallback artwork without losing it, queues entry and draws progress, and checks that loader failure reaches the app. Additional checks cover server identity and cancelled-entry timing. + +## Render facts assessment + +`MeshTables::new` reads names, registry properties, class membership, collision shape flags, fluid and render properties from `BlockFacts`. `LightTables::new/load` reads emission, dampening, occlusion and shape-occlusion flags; geometry comes from `occlusion.nbt`. Neither table needs recipes, item data, AI or the other tail tables. + +A render subset is possible, but the current facts decoder also validates registry order and builds the gameplay projection from the same document. Meshing additionally depends on class chains from `block-behaviours.json`. Avoiding the 54 MB read requires an exported render-facts artifact (and its prepare/cache/version checks), or another decoder plus a second full read at world entry. This is beyond a cheap startup ownership change. Stage 4 can add that artifact and compare the resulting mesh/light tables against the full loader before moving facts out of core. + +## Measurement + +Measurements on this machine are warm runs with `HORIZON_NO_AUDIO=1`. Other compilation and parity jobs were running, so elapsed times are diagnostic rather than a controlled benchmark. + +Before phasing (3a, `42d65465`), `--offscreen --frames 3 --smoke-report` completed successfully with peak RSS 762,536 KiB. The report contained 3 frames, no chunks, no generated or meshed sections, no player, and no error. This is the synchronous harness path; the progressive path is measured separately after 3c. + +After phasing, the same warm three-frame synchronous smoke completed successfully with peak RSS 774,080 KiB, up 11,544 KiB (1.5%) from the 3a measurement. Both reports had 3 frames, no chunks, no generated or meshed sections, no player, and no error. Elapsed time is intentionally excluded from the comparison because the machine was running concurrent builds. + +The progressive checks used an overlay of the prepared resources with `mob-data.json` replaced by a FIFO. This holds WorldRest without a production delay hook. Core and the real renderer became ready, Options → Video opened, and the backdrop logged `title backdrop ready; world rest ready: false` before the FIFO was released. In an unblocked Horizon launch, core was ready at 4,776 ms, the renderer at 5,260 ms, and rest at 6,282 ms under compilation load. Backdrop generation and fade-in still take their own time; these marks measure bundle readiness separately. + +A direct singleplayer launch while the FIFO was held displayed `Loading items` and handled a window resize. Releasing the FIFO resumed entry; the run exited cleanly with a player, 85 chunks, 529 meshed sections and no error. That delayed-entry run peaked at 1,142,560 KiB, including the loaded world. Integrated-server entry also joined after release and rendered a world. Dedicated startup reached `Done`, handled `list` and `stop`, saved and exited with status 0 without constructing a renderer. + +The full parity selection against `origin/main` also includes the unmerged Stage 2 ancestors (313 harnesses). The local run was bounded at 30 minutes; its log recorded passes for `active_effects`, `active_effect_tick` and `anchor`. The remainder needs CI or a longer dedicated run. The client Rust tests, focused startup probes and memory measurements are separate from that incomplete parity run. + +Progressive-title RSS was measured separately with audio disabled, render distance 4 and a 30 FPS cap, using separate temporary game directories. The first 30-second pair measured 1,055,132 KiB before and 1,721,772 KiB after. A warm 60-second repeat in reverse order measured 1,031,948 KiB before and 1,145,776 KiB after: an increase of 113,828 KiB (about 111 MiB, 11%). These launches ran during compilation and did not advance through startup/streaming identically. They establish a memory cost to investigate, not a claim of RSS parity; a quiet-machine cold/warm comparison remains needed before merging the stack. diff --git a/docs/portal-scan-benchmark.md b/docs/portal-scan-benchmark.md new file mode 100644 index 000000000..b8104ef26 --- /dev/null +++ b/docs/portal-scan-benchmark.md @@ -0,0 +1,81 @@ +# Nether-portal section-scan benchmark + +Run with the pinned prepared client resources: + +```sh +CARGO_BUILD_JOBS=6 cargo run --profile fast -p horizon-client --example portal_scan_bench -- \ + .local/horizon-client/26.3 +``` + +The example keeps the original `Game::may_hold_portal` alongside the resident +cache (`portal_sections`). Both run in the same optimized binary, alternating +measurement order. Each workload has one discarded warmup pair and seven pairs +of 300 ticks. It reports resource loading and fixture construction separately. +This measures the section scan only, not the whole `entity_portals` span, the +sweep that follows a positive answer, or the rest of the tick. + +The fixture is a 17x17 chunk world (289 chunks), each with 24 sections, and 500 +entity reaches spread over it (an item or mob box 0.25 wide and the small move +that produces the swept box). A reach touches 1-4 sections. Four palettes: +sparse (one non-portal state), dense (128 distinct), global (300 distinct, a +global palette whose `maybe_has` walks the whole state range) and dense_portal +(the dense palette with a portal in every fifth section). These are controlled +fixtures, not a claim about typical generated terrain. + +Steady includes the initial cache fill amortized over 300 ticks. Cold recreates +the cache each tick, including allocation and destruction; reload clears it each +tick while retaining capacity. Edit workloads alternate a block value in three +or all 289 chunks before every scan. Timing includes the `Arc::make_mut` move of +the 176-byte chunk wrapper caused by weak cache handles; the baseline holds no +such handles. Both paths keep the original outer reach list. + +The scan is parity-checked in the same run: the cached and per-tick answers must +agree for every reach over every sample, or the example panics. + +Measured on an AMD Ryzen 5 5600X (12 logical CPUs), Linux x86-64, rustc 1.99.0 +(b940084d7), using `fast` (release optimization, LTO off, 16 codegen units). +Resource loading took 705 ms; fixture construction took 66 ms sparse, 360 ms +dense, 392 ms global and 385 ms dense_portal, shared by both implementations. + +Median microseconds per tick (seven paired samples): + +| Palette | Workload | Original | Cached | Speedup | +| --- | --- | ---: | ---: | ---: | +| sparse | steady | 40.058 | 16.836 | 2.38x | +| sparse | cold | 40.203 | 48.619 | 0.83x | +| sparse | reload | 40.828 | 44.552 | 0.92x | +| sparse | edit_1pct | 41.282 | 52.200 | 0.79x | +| sparse | edit_all | 50.119 | 4192.396 | 0.01x | +| dense | steady | 244.610 | 21.869 | 11.19x | +| dense | cold | 292.533 | 1505.691 | 0.19x | +| dense | reload | 250.397 | 1301.608 | 0.19x | +| dense | edit_1pct | 256.522 | 69.132 | 3.71x | +| dense | edit_all | 263.829 | 5904.581 | 0.04x | +| global | steady | 6179.070 | 360.082 | 17.16x | +| global | edit_1pct | 6251.530 | 1565.249 | 3.99x | +| dense_portal | steady | 256.854 | 21.078 | 12.19x | +| dense_portal | cold | 240.498 | 1526.251 | 0.16x | +| dense_portal | reload | 347.879 | 2037.123 | 0.17x | +| dense_portal | edit_1pct | 368.684 | 118.108 | 3.12x | +| dense_portal | edit_all | 282.876 | 7194.112 | 0.04x | + +The global palette's `maybe_has` walks the whole ID range, so rescanning every +chunk each tick (cold, reload, edit_all) is intractable; those rows are not +measured for it. + +Keep: the steady scan improves for every palette (2.4x to 17x), which is the +case that repeats each tick in real play. Under constant edits the cache also +improves the dense and global palettes (edit_1pct 3-4x) because the original +rescans through the per-tick map. Rescanning every chunk every tick (cold, +reload, edit_all) is slower than the original because a miss scans all 24 +sections of a chunk, where the original only touched the sections a reach +overlaps. No whole-game speedup is inferred from this microbenchmark; real +terrain's palette sizes and block-write frequency determine the gain. Lighting +and other chunk changes conservatively invalidate a chunk's mask. + +The cache adds a weak handle and mask per visited resident chunk, retains no +unloaded block data, and is pruned when a chunk unloads (`sync_tick_chunks`) and +cleared on resource reload, like `random_tick_sections`. Editing a cached chunk +can move its 176-byte wrapper (plus Arc bookkeeping) on the first edit after it +is cached; it does not force a deep copy of the block data. The original outer +reach-list allocation remains. diff --git a/docs/release-flow.md b/docs/release-flow.md index f1015a31f..b72d896b8 100644 --- a/docs/release-flow.md +++ b/docs/release-flow.md @@ -131,6 +131,7 @@ the notes. | Archive | Runs on | Checks | | --- | --- | --- | | `horizon-linux-x86_64.tar.gz` | GitHub `ubuntu-22.04` (an older glibc, so it runs on any recent distribution) | the standard loader and libraries only (`tools/check_imports.py`) | +| `horizon-server-linux-x86_64.tar.gz` | the same Linux job, after the client archive: `horizon-server`, `horizon-prepare` and the game built with `--no-default-features` as `horizon-engine` (docs/server.md) | as above, and neither `libasound.so.2` nor `libudev.so.1` (`tools/check_imports.py --server`) | | `horizon-windows-x64.zip` | GitHub `windows-latest`, C runtime linked in | system DLLs only (`tools/check_imports.py`) | | `horizon-macos-arm64.zip` | The Mac mini's runner (`vars.MAC_RUNNER=macos-latest` sends it to GitHub) | system libraries only (`otool -L`); a 1500-frame offscreen run of a new world (about 12 s; 300 frames end before the world has loaded) with resources prepared from the runner's own Minecraft (`--offscreen --world smoke --frames 1500 --smoke-report`: frames drawn, chunks generated and meshed, a player, no error) | diff --git a/docs/server-quickstart.txt b/docs/server-quickstart.txt deleted file mode 100644 index 84c325884..000000000 --- a/docs/server-quickstart.txt +++ /dev/null @@ -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. diff --git a/docs/server.md b/docs/server.md index 6e165f534..272148d49 100644 --- a/docs/server.md +++ b/docs/server.md @@ -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 @@ -33,6 +34,12 @@ The dedicated server is the game itself, run headless: `whitelist on|off|list|reload|add|remove`, `ban`, `ban-ip`, `pardon`, `pardon-ip`, `banlist`, `kick` and `say` behave as in `DedicatedServer`. Every other command goes to the game's own dispatcher as the server (permission level 4). The log follows vanilla's Log4j pattern, in `logs/latest.log` and on stdout. +- **Player names.** `usercache.json` in the universe folder works as vanilla's `CachedUserNameToIdResolver`. + Players are added when they join and kept for a month, and the file keeps the 1000 most recently used, + newest first. The console finds a player by name in it, in any case. A name it doesn't hold is asked of the + profile service (`getByName`). If the service doesn't know the name, an offline server uses the offline + player of the lower-case name; an online server answers "That player does not exist". + `tools/test_user_cache_parity.py` checks this against vanilla, with a mock profile service. ### Measured (2026-10-16, Mac mini M4 Pro, main at bd8dff20, release build) @@ -49,9 +56,10 @@ distance 10. The vanilla server was the pinned 26.3 `server.jar` on Java 25 with | What has to be installed | one 46.8 MB binary + 589 MB of prepared resources | Java 25 + the 62 MB jar | | Crates linked | 384 (wgpu, winit, cpal, gilrs, arboard, naga…) | n/a | -On Linux the binary loads `libasound.so.2` (cpal) and `libudev.so.1` (gilrs) at startup, even on a server. -wgpu, winit and the Wayland/X11 libraries are loaded only when a window opens. That makes a minimal -container image need ALSA and udev for a program that never plays a sound or reads a controller. +On Linux the full game loads `libasound.so.2` (cpal) and `libudev.so.1` (gilrs) at startup. wgpu, winit +and the Wayland/X11 libraries are loaded only when a window opens. The server build (`horizon-engine`, +section 3) is the same program built without its `audio` and `controllers` features, so it needs neither +library, and a minimal server image runs it as it is. The two clear losses are RAM and idle CPU. The dedicated server loads everything the client needs to draw (the HUD atlas, item renderer, block-entity models, panorama, fog and music tables, entity models), and it @@ -68,23 +76,28 @@ not tick cost, so it is in scope here (see section 9). headless client built from vanilla's own codecs, pings and joins Horizon's server and vanilla's from the same world copy. It compares every login, configuration and play packet up to the first chunk batch, the chunk bytes, chat, `/list`, failing commands, the console lines and the files left behind. Result on main: - see "Harnesses on main" below. The real vanilla GUI client (`tools/vanilla_client.py`) could not be run - here: the tool only knows Linux natives, and the Mac mini's session was locked, which stalls the client's - rendering. It runs on the desktop under vscreen. + see "Harnesses on main" below. +- **The real vanilla 26.3 client** (`tools/vanilla_client.py`, from the owner's own files, dev-only) joins + too. On 2026-10-18 it joined the server build of the game (`horizon-engine`, section 3) on the Mac mini: + `python3 tools/vanilla_client.py --name Vanilla --join localhost:PORT`. It logged in, drew the terrain + around spawn and stayed connected for four minutes, with no warning or error on the server. The tool + runs on Linux under vscreen and on macOS from the logged-in screen session, even a locked one, with vsync + off. ### What fails or is missing | Area | Status | |---|---| -| `online-mode=true` | **Not supported.** The server logs a warning and runs offline: no session `hasJoined` check, no encryption, no secure chat. (The client side of online mode works and is tested by `test_online_mode_parity.py` against vanilla's server.) A public server must not run like this. This is the top engine item. | +| `online-mode=true` | **Works** (ServerLoginPacketListenerImpl): a 1024-bit RSA key pair at start ("Generating keypair"), the encryption request, the shared secret and challenge, AES/CFB8, and the session service's `hasJoined` check (with the address when `prevent-proxy-connections` is on), with vanilla's log lines and messages. `test_online_mode_parity.py` runs the same logins against vanilla's server and Horizon's with a mock session service. | +| Secure chat | **Works** (`SignedMessageChain`, `LastSeenMessagesValidator`, `MessageSignatureCache`): the chat session update and its profile key, checked against the services' `playerCertificateKeys`; signed chat, `say`, `me` and `msg`, relayed with their signatures and last-seen lists; `enforce-secure-profile` (with `online-mode`) refuses unsigned chat; and vanilla's log lines (`[Not Secure]`, the decoder's warnings, the kicks). Offline servers and players without a chat session chat unsigned, as on vanilla. `test_secure_chat_parity.py` runs the same signed and unsigned players against vanilla's server and Horizon's, with test keys and a mock services server. | | Nether and End | **Players can't change dimension.** The engine keeps one dimension loaded (`Game::enter_dimension` moves only the local single-player player), and the hub serves one `dimension` to everyone. | -| Refused logins | Nothing is logged. Vanilla logs `Disconnecting <name>: <reason>` (`ServerLoginPacketListenerImpl.disconnect`) and `<name> lost connection: …`. | -| Gameplay for remote players | The LAN guest leftovers apply: an item used on a block is used twice, the recipe book doesn't work, status effects don't apply to guests, arrows don't record their owner, team friendly fire is ignored (#484). Block desync and short freezes are reported on a same-machine server (#500). | +| Refused logins | Logged as vanilla does: `Disconnecting <name> (/address): <reason>`, then `<name> (/address) lost connection: <reason>`, and the session errors (`Username '<name>' tried to join with an invalid session`, `Couldn't verify username because servers are unavailable`). | +| Gameplay for remote players | Checked against vanilla's server with a vanilla-codec client (#484, `tools/test_dedicated_server_parity.py`): a right click with an item on a block uses it once, a dead player's commands run, players on a team without friendly fire don't hurt each other, and a player's projectiles are theirs (their ender pearl takes them). The recipe book, status effects and block sync while digging (#500) work. Left: a player's projectiles don't hit other players (no bow PvP between guests), and a mob a guest kills counts in the host's statistics and advancements, not the guest's (`mob_died` runs as the host). | | Defaults that surprise owners | `white-list=true` is vanilla 26.3's default, so a fresh server refuses everyone until they are added. That matches vanilla, so the first-run flow (section 3) asks about the whitelist up front. | | Management | Only stdin. There is no service, no backups, no status beyond `list`, no restart after a crash, and a server can't be managed while it runs in the background. | | Packaging | The server is the full game client. Resources come from `horizon-prepare`, which needs a JDK and the official launcher's client install, both awkward on a headless VPS. | | Tick times | The dedicated loop never recorded its tick times in the game clock (only the windowed loop did), so `/tick query` on a dedicated server reported 0 ms. Fixed in PR 2b, which the benchmark needs (section 9). | -| Scale | Not measured with more than one player. The tick is single-threaded, and each remote player costs the hub one tracking and chunk-sending pass. | +| Scale | Measured against vanilla on equal work (section 9, `benchmarks/server-equal-work-2026-10-17/`): 20 TPS up to 50 bots, 9 to 11 TPS at 100, where vanilla's watchdog stops it. Horizon is ahead with few players and when exploring; with 25 to 50 players near spawn vanilla's tick is shorter. | ### Harnesses on main @@ -138,16 +151,22 @@ game built in `Game::new`, the integrated server on its own thread. On that base 1. The dedicated path skips everything only a window uses: the HUD atlas, item renderer, block-entity and entity models, panorama, fog, music, and mesh tables (perf-ticks-2 already stops server-side meshing). This is the RAM and start-time item. -2. The game moves into a library crate, with the window, audio and controller parts behind a `client` - feature. A `horizon-engine` binary built without that feature links no wgpu, winit, cpal or gilrs, and - needs neither ALSA nor udev. The supervisor finds it beside itself and falls back to `horizon-client`. +2. The game moves into a library crate, with the window parts behind a `client` feature, so + `horizon-engine` links no wgpu or winit either. Sound and controllers are already optional (the + `audio` and `controllers` features, on by default): `horizon-engine` today is the game built without + them (section 3). ## 3. Packaging -**Binaries.** The release archives already hold `horizon-client`, `horizon-ctl` and `horizon-prepare`. -`horizon-server` joins them (PR 3: `xtask package` and `release.yml` build it and check it starts). Once -`horizon-engine` exists (section 2), a separate small archive per platform -(`horizon-server-<os>-<arch>.tar.gz` or `.zip`) holds just the server's binaries. +**Binaries.** The release archives hold `horizon-client`, `horizon-ctl`, `horizon-prepare` and +`horizon-server`. On Linux the release also has `horizon-server-linux-x86_64.tar.gz`: `horizon-server`, +`horizon-prepare` and `horizon-engine`, the game built with `--no-default-features` (no sound, no +controllers), which links neither `libasound.so.2` nor `libudev.so.1`. `release.yml` builds it after the +client archive (`xtask package-server`) and fails if `tools/check_imports.py --server` finds either library; +CI lints the same build. `horizon-server` runs `horizon-engine` when it is beside it or on the PATH, else +`horizon-client`. Build it yourself with `cargo build --release -p horizon-client --no-default-features` +and copy `target/release/horizon-client` as `horizon-engine`. macOS and Windows have no such libraries to +avoid, so they keep the one archive. **Game files: the owner's own copy, as with `prepare`.** Nothing of Mojang's is redistributed and nothing is downloaded on the owner's behalf. `horizon-server` looks for prepared resources in this order: @@ -174,10 +193,16 @@ comes out of the headless-engine work. - Linux and macOS: `curl -fsSL https://raw.githubusercontent.com/ludovic111/horizon/main/packaging/server/install.sh | sh` - The script picks the release archive for the OS and architecture (Linux x86_64, macOS arm64), downloads - it from GitHub Releases, and checks it against the SHA-256 GitHub publishes for each asset. It never asks - for sudo. Options: `--version vX.Y.Z`, `--dir` (default `~/.local/share/horizon`), `--bin` (default - `~/.local/bin`). + The script picks the release archive for the OS and architecture (on Linux x86_64 the server archive, + on macOS arm64 the game's), downloads it from GitHub Releases, and checks it against the SHA-256 GitHub + publishes for each asset. It never asks for sudo. Options: `--version vX.Y.Z`, `--dir` (default + `~/.local/share/horizon`), `--bin` (default `~/.local/bin`), and `--archive FILE` to install an archive + already on the machine (a build of your own; no download and no checksum). + A Linux release from before the server archive installs the full game, which links ALSA's and udev's + libraries (`libasound.so.2`, `libudev.so.1`); minimal server images often lack them (Debian and Ubuntu: + `sudo apt install libasound2 libudev1`, `libasound2t64` on Ubuntu 24.04 and later). Then the script lists + any that are missing, `init` warns, and `run` stops at once with what to install, rather than restarting + a game that can't load. - Windows: `irm https://raw.githubusercontent.com/ludovic111/horizon/main/packaging/server/install.ps1 | iex` The script does the same into `%LOCALAPPDATA%\Horizon` (`-Version`, `-Dir`), with junctions instead of @@ -189,8 +214,8 @@ comes out of the headless-engine work. (`horizon-prepare`, `horizon-server init`). **Docker** (`packaging/server/Dockerfile`, built the same way as the relay's image): a multi-stage build that -ends on `debian:bookworm-slim` as user `horizon`. It adds `libasound2` and `libudev1` until the headless engine -lands. It uses volume `/data` for the server folder, takes the owner's prepared game files as a read-only +ends on `debian:bookworm-slim` as user `horizon`, with the server build as `horizon-engine`, so the image needs +no ALSA or udev. It uses volume `/data` for the server folder, takes the owner's prepared game files as a read-only mount on `/opt/horizon/.local/horizon-client/26.3`, exposes port `25565/tcp`, and keeps the control API (and later the panel) on 127.0.0.1 inside the container: `docker exec <name> horizon-server status|cmd|backup`. On the first start, `HORIZON_EULA=true` and `HORIZON_WORLD`, `HORIZON_SEED`, `HORIZON_PORT`, @@ -239,7 +264,7 @@ motd = "A Minecraft Server" port = 25565 bind = "" # listen on every address, or only on this one (server-ip) max_players = 20 -online_mode = true # not supported by Horizon yet: the server runs offline and says so +online_mode = true # check players' Minecraft accounts (false: anyone joins under any name) whitelist = true # only whitelisted players and operators can join enforce_whitelist = false # kick online players taken off the whitelist view_distance = 10 @@ -301,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) ``` @@ -326,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 @@ -369,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 @@ -387,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 @@ -457,8 +501,60 @@ reproducible by anyone, and show the same gameplay on both sides. - Vanilla's chunk sending yields under load. - Horizon sent each nearby `walk` bot 1.5–4 times as many chunks. - Horizon sent 2–4 times the chunks per second in every multi-bot run. The next run equalises the work - (slower `explore`, a `walk`-only pass) on a quiet machine before anything is published. + Horizon sent 2–4 times the chunks per second in every multi-bot run. +- **Equal-work run (2026-10-17,** `benchmarks/server-equal-work-2026-10-17/`, on the ThinkPad with the bots + on the Mac mini). Horizon now checks moves and paces chunks as vanilla does (#586), the bots wait for the + chunk they stand in, an `--explore-speed` pass keeps both servers at the same rate, and vanilla runs with + `gamerule locator_bar false` (Horizon has no locator bar yet). Results: + - Memory: Horizon's RSS is 1.9 to 2.6 times smaller in every run (vanilla's heap is fixed at 4 GB). + - Walking near spawn: Horizon's tick is shorter with 1 and 10 bots (5.5 against 18.3 ms, 11.5 against + 16.8), and longer with 25 and 50 (20.7 against 16.0, 31.4 against 20.5). Both keep 20 TPS up to 50. + - Exploring at the same rate: Horizon's tick is about half of vanilla's (13.5 against 29.2 ms at 10 bots, + 20.2 against 37.0 at 25). + - At 100 bots: vanilla's watchdog stops it in every pass; Horizon runs at 9 to 11 TPS. + + Two server costs were found and fixed on the way: every chunk section was encoded once per player (#623), + and every packet woke its connection's writer thread (#649). Together they took the 50-bot walking tick + from 281 ms to 31 ms. At 25 walking bots most of what remains is the engine's streaming and block entity + ticks, both of which grow with players (the README has the profile). +- **Mixed bots, as installed (2026-10-18,** `benchmarks/server-mixed-2026-10-18/`, the first published + run for the working version). It used the v2026.10.18.15 release as `install.sh` installs it, against + vanilla 26.3, with 1, 10, 25 and 50 `mixed` bots and vanilla's default game rules on both (Horizon has + the locator bar now). The servers ran on the ThinkPad and the bots on the Mac mini. Results: + - Both kept 20 TPS in every run. + - Horizon's RSS was 3.7 to 5.2 times smaller (1.0 GB against 4.0 GB at 50). + - Horizon's average tick was shorter with 1, 10 and 25 bots (2.4 against 17.9 ms, 12.4 against 34.5, + 24.0 against 31.6) and longer with 50 (35.2 against 29.7). Its P99 was lower at every count. + - Horizon also did more work: it kept up with the exploring bots, so it generated 1.9 to 2.9 times + the new terrain a second. +- **Mixed bots, equal work (2026-10-18,** `benchmarks/server-equal-mixed-2026-10-18/`). This is the same + run with the load made equal. The explore bots fly at 0.8 blocks a second, so every bot follows the same + path on both servers and none waits for terrain. A 300-second warm-up lets both finish the bots' first + views before the measurement. `server_bench.py` now prints a work table that compares each count's chunks + a second across servers and flags a run that isn't equal. Horizon ran as `horizon-engine`, the server + build, installed from its archive. Results, on the ThinkPad again (the desktop didn't wake): + - The work was equal at 1 and 10 bots. At 25 the chunks sent a second were equal (158.6 against 157.6), + and vanilla still sent 2.5 more first-time chunks a second while it caught up. + - Tick: 1.8 against 11.3 ms at 1 bot, 4.7 against 34.3 at 10, 11.7 against 50.5 at 25 (vanilla 19.8 TPS). + At 50 Horizon ran at 23.8 ms. Vanilla's watchdog stopped its server 30 s after the last bot joined (a + 60-second tick). This is one pass, to repeat on a bigger machine. + - 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. @@ -477,7 +573,8 @@ vanilla. archive in the release. 3. The web panel MVP: status, live TPS/MSPT/RAM/CPU, players, console, backups and login links. 4. `horizon-bots` and `tools/server_bench.py`, with a first run against vanilla (1/10/25/50 bots, `mixed`) - published in `benchmarks/`. + published in `benchmarks/`: done, `benchmarks/server-mixed-2026-10-18/`, and with equal work + `benchmarks/server-equal-mixed-2026-10-18/` (section 9). 5. Small engine fixes found on the way: logging refused logins (vanilla's lines). **Where it stands (2026-10-16):** items 1 to 4 are built and in review (section 11). What's left for the 23rd: @@ -487,18 +584,13 @@ machine, then published in `benchmarks/`; and item 5. **Out (next):** -- online mode on the server side (session `hasJoined`, encryption, secure chat); - 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). -Until online mode works, the docs say to run Horizon's server only on a LAN, a tailnet, or behind a -whitelist. +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. ## 11. Pull requests diff --git a/docs/shader-packs.md b/docs/shader-packs.md index 812b540f0..5f8e88c20 100644 --- a/docs/shader-packs.md +++ b/docs/shader-packs.md @@ -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 diff --git a/docs/surface-generation-benchmark.md b/docs/surface-generation-benchmark.md new file mode 100644 index 000000000..e40849c19 --- /dev/null +++ b/docs/surface-generation-benchmark.md @@ -0,0 +1,15 @@ +# Constant surface-rule specialization benchmark + +The upstream PR contains only the surface change and tests, reapplied to upstream main. The measurements below were collected with #796 present; they are historical incremental measurements, not new timings for the rebased upstream head. + +This surface-only change is based on the bulk unary density optimization in [upstream PR #796](https://github.com/ludovic111/horizon/pull/796). Measurements compare against `eb3789038e3184c98a1459fe638947200217d360`, which already includes that optimization; its savings are not counted again. + +With one worker, primary initialized Overworld terrain time improved from 10.664 to 10.298 ms/input chunk (3.43%). Terrain plus decoration improved from 12.645 to 12.242 (3.19%). With four workers, terrain improved 2.48% and combined time 2.07%. Nether terrain improved 4.16% / 3.93% with one/four workers; combined improved 3.76% / 3.63%. The second Overworld site improved terrain 2.53% / 2.81%. End improved 2.95% with one worker but regressed 2.93% with four, with substantial paired noise. This meets the revised 3% median gate on selected representative cases, not the original 5% gate or every workload. + +Cases were declared before editing. Each used one discarded pair followed by five alternating baseline/candidate pairs, with identical work and builds. The primary case used seed 12345, center (0,0), 256 terrain chunks and 196 decorated interior chunks, at one/four workers across all three dimensions. The second Overworld case used seed -1 at (32,-32). An offset 9×9 generation pass initialized the same seeded generator without pre-generating the measured region. Wall time is elapsed/input count; combined time includes the complete decoration phase and required borders. Cold startup was measured separately. + +Machine: AMD Ryzen 5 5600X, 32 GiB RAM, Linux, Rust 1.99.0. Optimized `fast` profile: release optimization, LTO off, 16 codegen units. Runs and builds were sequential; background desktop activity was not isolated. Memory use was effectively unchanged. The benchmark used the existing chunkgen benchmark with identical optional initialization and exact timing instrumentation on both revisions. `crates/horizon-world/examples/chunkgen_bench.rs` is the standard workload entry point; its thread-multiplied human-readable ms/chunk display differs from actual elapsed/count. + +Validation passed: 102 targeted Rust tests; 162 world-crate tests; 24 exact complete-field baseline/candidate terrain and decorated comparisons across three dimensions, two sites/seeds, and one/four workers; and six fresh pinned Java 26.3 client/server suites covering material stages, rule library, conditions, temperature, extensions, and surface generation. Differential snapshots included block states/palettes, quart biomes, binary entities/block entities, scheduled ticks, all six heightmaps, ordered postprocessing, non-air counts, and chunk metadata. Compilation/binding errors, dynamic queries, and short-circuit behavior have regression coverage. The final production executable matched the measured candidate byte-for-byte. + +The repository quick PR check also passed public-tree validation, Python tests, formatting, clippy, and world tests. Validation is scoped to the declared corpora and does not establish complete gameplay parity. Generated timing files, diagnostic runners, patches, and validation logs are retained locally outside the repository and are not included in this PR. diff --git a/launcher/Cargo.lock b/launcher/Cargo.lock index 4a281729d..bde1037b3 100644 --- a/launcher/Cargo.lock +++ b/launcher/Cargo.lock @@ -2664,6 +2664,7 @@ dependencies = [ "gpui", "horizon-bench", "horizon-friends", + "horizon-port-pack", "horizon-prepare", "horizon-telemetry", "horizon-theme", @@ -2680,6 +2681,30 @@ dependencies = [ "zip", ] +[[package]] +name = "horizon-mods" +version = "0.1.0" +dependencies = [ + "log", + "serde", + "serde_json", + "toml 1.1.8+spec-1.1.0", + "zip", +] + +[[package]] +name = "horizon-port-pack" +version = "0.1.0" +dependencies = [ + "horizon-mods", + "serde", + "serde_json", + "sha1", + "toml 1.1.8+spec-1.1.0", + "ureq", + "zip", +] + [[package]] name = "horizon-prepare" version = "0.1.0" diff --git a/launcher/Cargo.toml b/launcher/Cargo.toml index 8f5873f87..34af6e2a1 100644 --- a/launcher/Cargo.toml +++ b/launcher/Cargo.toml @@ -26,6 +26,8 @@ horizon-prepare = { path = "../crates/horizon-prepare" } # Join a friend: invite codes (the game does the joining). horizon-friends = { path = "../crates/horizon-friends" } horizon-bench = { path = "../crates/horizon-bench" } +# Import modpack: the porting pipeline, run on the player's own files (docs/modpacks.md). +horizon-port-pack = { path = "../crates/horizon-port-pack" } # Opt-in data collection: the consent, the outbox and its upload (docs/telemetry.md). horizon-telemetry = { path = "../crates/horizon-telemetry" } # The session file's random name. diff --git a/launcher/README.md b/launcher/README.md index e5edba182..b111113f2 100644 --- a/launcher/README.md +++ b/launcher/README.md @@ -25,6 +25,16 @@ The opening, then Play ([clip](docs/opening.webm)): preparation reuses checksum-verified game jars and libraries from the current or another installed Horizon version, so the original Minecraft install is only required when no valid imported copy exists. +- **Modpacks**: import a Java modpack (a Modrinth `.mrpack`, a CurseForge zip, or a Prism, + MultiMC or CurseForge instance folder) and see its coverage report: per mod, ported, partial, + not yet or not needed, with the reason ([docs/modpacks.md](../docs/modpacks.md)). **Port** + gets the jars of what this computer can port today (content mods with data and assets only) + the player's own way: copied from a folder they have, or downloaded from the pack's own links + or the same file on Modrinth's CDN, hash checked; files only CurseForge has need its API key, so + they come from your installed instance. It carries them over for the selected version, and + **Play** starts the game as the pack's own profile (`ports/<pack>/home/` in the game's home: + its own saves and options) with those mods. Nothing is uploaded or shared: mod jars and ports + stay on this computer. - **Patch notes**: each release's notes, newest first, with a link to GitHub Sponsors. Quiet "Discord" and "Website" links sit in the footer. Data lives in the platform's data directory (`~/.local/share/horizon-launcher` on Linux, @@ -33,7 +43,13 @@ Windows): `versions/<tag>/`, `server-jars/`, `server/` and `settings.json`. - **Settings**: install new Horizon versions automatically (on unless you turn it off; the selected version only moves to the new one if you were on the newest), the opening animation, - the game's menu style, and the launcher's own version and update state. + the game's menu style, the game's extra command-line arguments, and the launcher's own version + and update state. +- **Game arguments**: extra arguments added to the game's command line (for example + `--server-thread`), one line where quotes group a word with spaces. They are appended after the + launcher's own arguments, kept as `game_args` in `settings.json`, and apply the next time the + game starts. `horizon-launcher game-args "<line>"` sets them too, and `play --arg <ARG>` passes + one for a single run. - **Menu style**: Horizon's own menus or Classic (vanilla's screens). Classic has no way back inside the game, so the launcher is where you return to Horizon's. It is the `menuStyle:` line of `horizon.txt` in the game's home folder (`HORIZON_HOME`, else `horizon` in the data @@ -71,11 +87,12 @@ The same binary is a command-line tool; without a subcommand it opens the window horizon-launcher install <tag|latest> horizon-launcher remove <tag> horizon-launcher prepare [--minecraft <dir>] [--version <tag>] - horizon-launcher play [--version <tag>] + horizon-launcher play [--version <tag>] [--arg <ARG>]... start the client; --arg passes an extra argument through (repeatable) horizon-launcher server [--dir <dir>] [--version <tag>] [--accept-eula] horizon-launcher notes [<tag>] patch notes as plain text horizon-launcher links the Discord, sponsor and website links horizon-launcher menu-style [horizon|classic] show or set the game's menu style + horizon-launcher game-args [<line>] show or set the extra arguments the game starts with horizon-launcher update [--check] Horizon and the launcher horizon-launcher logs [--lines N] [--crash] the end of the game's log, or its newest crash report horizon-launcher account [list] the Microsoft accounts signed in (* = plays) @@ -83,6 +100,11 @@ The same binary is a command-line tool; without a subcommand it opens the window horizon-launcher account sign-out <name> horizon-launcher account select <name> play as this account horizon-launcher account offline play without an account + horizon-launcher modpack [list] the imported packs and their coverage + horizon-launcher modpack import <pack> a .mrpack, CurseForge zip or instance folder + horizon-launcher modpack port <pack> [--from <dir>] [--version <tag>] get the jars and carry them over + horizon-launcher modpack play <pack> [--version <tag>] play it as its own profile + horizon-launcher modpack remove <pack> horizon-launcher benchmark [--repeats N] measure the launcher (see Performance log) horizon-launcher data [status] what data collection is allowed, the install ID, what waits horizon-launcher data allow|deny <kinds|all> performance, crashes, bugs, benchmarks @@ -92,7 +114,7 @@ The same binary is a command-line tool; without a subcommand it opens the window horizon-launcher data benchmark <report.json> upload a benchmark report, printed first `--json` prints JSON where there is something to report (`versions`, `install`, `notes`, -`next`, `links`, `menu-style`, `update --check`, `logs`, `account`, `benchmark`, `data`). Errors go to stderr with exit code 1. `server` runs in the terminal, +`next`, `links`, `menu-style`, `game-args`, `update --check`, `logs`, `account`, `benchmark`, `data`, `modpack`). Errors go to stderr with exit code 1. `server` runs in the terminal, so lines you type reach its console. `HORIZON_LAUNCHER_HOME` replaces the data folder (for scripts and tests). @@ -192,6 +214,7 @@ bundled so the type is the same on every platform. ## Screenshots ![Server](docs/server.png) +![Modpacks](docs/modpacks.png) ![Versions](docs/versions.png) ![Patch notes](docs/patch-notes.png) ![Settings](docs/settings.png) diff --git a/launcher/docs/modpacks.png b/launcher/docs/modpacks.png new file mode 100644 index 000000000..9abe41c64 Binary files /dev/null and b/launcher/docs/modpacks.png differ diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index 4f5d00ca0..8d1036d22 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -792,7 +792,7 @@ pub fn preparer( match sources::resolve(minecraft, &pins, &known, &linked, &fetch, log)? { sources::Resolved::Folder(dir) => dir, sources::Resolved::NotFound(checked) => { - let not_found = sources::not_found(&version, &checked); + let not_found = sources::not_found(&version, &checked, minecraft); download_for_owner(&pins, &own, &fetch, log).map_err(|e| not_found + &e)?; own } @@ -1008,6 +1008,63 @@ pub fn crash_summary(report: &str) -> Option<String> { (!error.is_empty()).then(|| error.join(" ")) } +/// The line the game prints when it has drawn its first frame. +pub const GAME_FIRST_FRAME: &str = "Horizon: first frame"; + +/// Whether a game's output ends in a crash while its Vulkan renderer started: an adapter line +/// (`Horizon renderer: <GPU> (Vulkan, <type>)`) and no first frame. A native crash in a graphics +/// driver leaves no crash report, only those lines (#746). +pub fn vulkan_start_crashed<S: AsRef<str>>(lines: impl IntoIterator<Item = S>) -> bool { + let mut vulkan = false; + for line in lines { + let line = line.as_ref(); + if line.starts_with(GAME_FIRST_FRAME) { + return false; + } + if let Some(adapter) = line.strip_prefix("Horizon renderer: ") { + // The adapter line, not one of the start's steps: "<GPU> (<backend>, <type>)". + if adapter.ends_with(')') && adapter.contains(" (") { + vulkan = adapter.contains(" (Vulkan, "); + } + } + } + vulkan +} + +/// The game's graphics settings file in its home (the game's `graphics.rs`). +const GRAPHICS_FILE: &str = "horizon-graphics.txt"; + +/// Sets `graphicsApi:dx12` in `home`'s graphics settings when it is unset or `auto`, keeping +/// every other line. True when it changed; a player's own `vulkan` or `dx12` stays. +pub fn switch_to_dx12(home: &Path) -> Result<bool, String> { + let path = home.join(GRAPHICS_FILE); + let text = match fs::read_to_string(&path) { + Ok(text) => text, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => String::new(), + Err(e) => return Err(format!("{}: {e}", path.display())), + }; + let mut lines: Vec<String> = Vec::new(); + let mut found = false; + for line in text.lines() { + match line.split_once(':') { + Some((key, value)) if key.trim() == "graphicsApi" => { + if value.trim() != "auto" { + return Ok(false); + } + found = true; + lines.push("graphicsApi:dx12".to_owned()); + } + _ => lines.push(line.to_owned()), + } + } + if !found { + lines.push("graphicsApi:dx12".to_owned()); + } + fs::create_dir_all(home).map_err(|e| format!("{}: {e}", home.display()))?; + fs::write(&path, lines.join("\n") + "\n").map_err(|e| format!("{}: {e}", path.display()))?; + Ok(true) +} + /// The last `lines` lines of `file`, or why it cannot be read. fn tail_of(file: &Path, lines: usize) -> Result<String, String> { let bytes = fs::read(file).map_err(|e| format!("{}: {e}", file.display()))?; @@ -1089,6 +1146,74 @@ pub fn set_menu_style(style: MenuStyle) -> Result<(), String> { fs::write(file, text).map_err(|e| e.to_string()) } +/// Splits the player's extra game arguments (Settings' one line) into words, the way a shell +/// would: spaces separate them, single or double quotes group a word with spaces in it, and a +/// backslash escapes the next character (outside quotes, or inside double quotes). +pub fn split_args(line: &str) -> Vec<String> { + let mut words = Vec::new(); + let mut word = String::new(); + let mut started = false; + let mut quote: Option<char> = None; + let mut chars = line.chars(); + while let Some(c) = chars.next() { + match quote { + Some(q) => { + if c == q { + quote = None; + } else if c == '\\' && q == '"' { + // Inside double quotes only `"` and `\` are escaped, as a shell does. + match chars.next() { + Some(n @ ('"' | '\\')) => word.push(n), + Some(n) => { + word.push('\\'); + word.push(n); + } + None => word.push('\\'), + } + } else { + word.push(c); + } + } + None => match c { + '\'' | '"' => { + quote = Some(c); + started = true; + } + '\\' => { + if let Some(n) = chars.next() { + word.push(n); + started = true; + } + } + c if c.is_whitespace() => { + if started { + words.push(std::mem::take(&mut word)); + started = false; + } + } + _ => { + word.push(c); + started = true; + } + }, + } + } + if started { + words.push(word); + } + words +} + +/// The extra game arguments the player set in Settings, split into words. +pub fn game_args() -> Vec<String> { + split_args(&Settings::load().game_args) +} + +/// Appends the player's extra game arguments, so they add to the launcher's own or override them. +pub fn append_game_args(cmd: &mut Command) { + cmd.args(game_args()); +} + pub fn client_command(tag: &str) -> Command { let home = game_home(); let _ = fs::create_dir_all(&home); @@ -1195,6 +1320,7 @@ pub fn client(tag: &str) -> Result<Command, String> { if let Err(e) = crate::account::attach_session(&mut cmd, &game_home()) { eprintln!("Playing offline: {e}"); } + append_game_args(&mut cmd); Ok(cmd) } @@ -1709,6 +1835,10 @@ pub enum Msg { /// The selected account's session file for the game about to start (none = offline), or /// why it could not be renewed. Session(Result<Option<PathBuf>, String>), + /// A modpack step's line (`modpacks`). + PackLine(String), + /// A modpack step ended: the pack as it now is. + PackDone(Result<Box<crate::modpacks::Imported>, String>), } /// A running process; its output arrives as `Msg::Line`, its end as `Msg::Exit`. @@ -1792,6 +1922,10 @@ pub struct Settings { /// means yes; removing the entry stores `false`, so it doesn't come back on the next start. #[serde(default, skip_serializing_if = "Option::is_none")] pub desktop_entry: Option<bool>, + /// Extra arguments for the game's command line, as one line (see [`split_args`]). Empty by + /// default. Appended after the launcher's own arguments, so they can add to or override them. + #[serde(default, skip_serializing_if = "String::is_empty")] + pub game_args: String, } impl Settings { @@ -1842,6 +1976,95 @@ mod tests { .contains("application.name = \"Horizon\""))); } + #[test] + fn a_crash_after_the_vulkan_adapter_line_and_before_the_first_frame_is_a_start_crash() { + let start = [ + "Horizon: resources loaded in 11.3s; 35723 block states, 1658 items, 67 biomes", + "Horizon renderer: Intel(R) HD Graphics 530 (Vulkan, IntegratedGpu)", + ]; + assert!(vulkan_start_crashed(start)); + // A step line after the adapter keeps it a start crash. + let step = [start[1], "Horizon renderer: creating the pipelines"]; + assert!(vulkan_start_crashed(step)); + // A crash after the first frame is the game's, not the renderer start's. + let later = [start[1], "Horizon: first frame after 812 ms"]; + assert!(!vulkan_start_crashed(later)); + // Already on DirectX 12, or no renderer yet: nothing to switch. + let dx12 = ["Horizon renderer: Intel(R) HD Graphics 530 (Dx12, IntegratedGpu)"]; + assert!(!vulkan_start_crashed(dx12)); + assert!(!vulkan_start_crashed(&start[..1])); + // Vulkan failed to open and DirectX 12 took over: its line is the last adapter. + assert!(!vulkan_start_crashed([start[1], dx12[0]])); + } + + #[test] + fn a_renderer_start_crash_switches_auto_to_dx12_once() { + let home = std::env::temp_dir().join(format!("horizon-dx12-switch-{}", std::process::id())); + let _ = fs::remove_dir_all(&home); + // No settings file yet: one with the API alone. + assert_eq!(switch_to_dx12(&home), Ok(true)); + assert_eq!( + fs::read_to_string(home.join(GRAPHICS_FILE)).unwrap(), + "graphicsApi:dx12\n" + ); + // Already DirectX 12, or the player's own Vulkan: left alone. + assert_eq!(switch_to_dx12(&home), Ok(false)); + fs::write(home.join(GRAPHICS_FILE), "sky:false\ngraphicsApi:vulkan\n").unwrap(); + assert_eq!(switch_to_dx12(&home), Ok(false)); + // Auto becomes DirectX 12 and the other settings stay. + fs::write( + home.join(GRAPHICS_FILE), + "sky:false\ngraphicsApi:auto\nfog:true\n", + ) + .unwrap(); + assert_eq!(switch_to_dx12(&home), Ok(true)); + assert_eq!( + fs::read_to_string(home.join(GRAPHICS_FILE)).unwrap(), + "sky:false\ngraphicsApi:dx12\nfog:true\n" + ); + let _ = fs::remove_dir_all(&home); + } + + #[test] + fn extra_game_arguments_split_like_a_shell() { + assert!(split_args("").is_empty()); + assert!(split_args(" ").is_empty()); + assert_eq!(split_args("--server-thread"), ["--server-thread"]); + assert_eq!( + split_args("--render-distance 32 --server-thread"), + ["--render-distance", "32", "--server-thread"] + ); + // Quotes keep a word with spaces together; either kind works. + assert_eq!( + split_args("--name 'Two Words' --world \"My World\""), + ["--name", "Two Words", "--world", "My World"] + ); + // An empty quoted word is still a word. + assert_eq!(split_args("--name \"\""), ["--name", ""]); + // A backslash escapes a space outside quotes, and " and \ inside double quotes. + assert_eq!(split_args("--name My\\ World"), ["--name", "My World"]); + assert_eq!(split_args("a\\tb c"), ["atb", "c"]); + assert_eq!(split_args("\"a\\\"b\" 'c\\d'"), ["a\"b", "c\\d"]); + } + + #[test] + fn the_game_arguments_setting_round_trips_and_reaches_the_command() { + let settings = Settings::parse(r#"{"game_args":"--server-thread"}"#); + assert_eq!(settings.game_args, "--server-thread"); + // Unset in an older settings.json: no arguments, and nothing is written for them. + let old = Settings::parse(r#"{"installed":true}"#); + assert_eq!(old.game_args, ""); + assert!(!serde_json::to_string(&old).unwrap().contains("game_args")); + + let mut cmd = Command::new("true"); + cmd.args(split_args("--server-thread --render-distance 40")); + let args: Vec<_> = cmd + .get_args() + .map(|a| a.to_string_lossy().into_owned()) + .collect(); + assert_eq!(args, ["--server-thread", "--render-distance", "40"]); + } + #[test] fn tags_never_mix() { assert!(is_game_tag("v2026.10.13")); diff --git a/launcher/src/cli.rs b/launcher/src/cli.rs index df9cfc6b1..d18a60dac 100644 --- a/launcher/src/cli.rs +++ b/launcher/src/cli.rs @@ -76,6 +76,33 @@ enum DataCmd { }, } +#[derive(Subcommand)] +enum ModpackCmd { + /// The imported packs and their coverage (the default) + List, + /// Import a .mrpack, a CurseForge zip or an instance folder, and write its coverage report + Import { path: PathBuf }, + /// Get the jars of what this computer can port and carry them over + Port { + /// The pack's folder name, as list shows it + pack: String, + /// A folder with your own jars (an installed instance, a downloads folder) + #[arg(long)] + from: Option<PathBuf>, + /// The installed version the ports are for (default: the selected one) + #[arg(long)] + version: Option<String>, + }, + /// Play a ported pack as its own profile + Play { + pack: String, + #[arg(long)] + version: Option<String>, + }, + /// Remove an imported pack: its jars, ports and the profile's saves + Remove { pack: String }, +} + #[derive(Subcommand)] enum Cmd { /// List Horizon releases, or the installed versions @@ -105,6 +132,10 @@ enum Cmd { /// Join a friend's world with its invite code #[arg(long)] join: Option<String>, + /// An extra argument for the game, passed through as-is (repeatable), for example + /// --server-thread + #[arg(long = "arg", value_name = "ARG", allow_hyphen_values = true)] + arg: Vec<String>, }, /// Run a dedicated server here; type commands into its console Server { @@ -132,6 +163,13 @@ enum Cmd { #[arg(value_parser = ["horizon", "classic"])] style: Option<String>, }, + /// Show or set the extra arguments the game starts with (for example --server-thread); + /// quotes group a word with spaces in it + GameArgs { + /// Omit to show the current arguments + #[arg(allow_hyphen_values = true)] + args: Option<String>, + }, /// Print the end of the game's log (logs/latest.log in the game folder), or its newest crash /// report Logs { @@ -161,6 +199,12 @@ enum Cmd { #[command(subcommand)] action: Option<DataCmd>, }, + /// Modpacks: import a Java pack, see its coverage report, port what this computer can and + /// play it as its own profile (docs/modpacks.md) + Modpack { + #[command(subcommand)] + action: Option<ModpackCmd>, + }, /// Serve the Model Context Protocol on stdin and stdout, for AI agents Mcp, /// Measure the launcher: starts (the first is the cold one), page switching, idle CPU and @@ -311,11 +355,12 @@ fn execute(command: Cmd, as_json: bool) -> Result<(), String> { } println!("{tag} is prepared."); } - Cmd::Play { version, join } => { + Cmd::Play { version, join, arg } => { let mut cmd = backend::client(&backend::resolve_version(version.as_deref())?)?; if let Some(code) = join { backend::join_friend(&mut cmd, &code)?; } + cmd.args(arg); attached(cmd)? } Cmd::Server { @@ -378,6 +423,24 @@ fn execute(command: Cmd, as_json: bool) -> Result<(), String> { println!("Menu style: {style}"); } } + Cmd::GameArgs { args } => { + let mut settings = backend::Settings::load(); + if let Some(line) = args { + settings.game_args = line; + settings.save(); + } + let line = settings.game_args; + if as_json { + json(serde_json::json!({ + "game_args": line, + "parsed": backend::split_args(&line), + })); + } else if line.is_empty() { + println!("No extra game arguments."); + } else { + println!("Game arguments: {line}"); + } + } Cmd::Logs { lines, crash } => { let (path, text) = backend::read_logs(lines, crash)?; if as_json { @@ -400,6 +463,7 @@ fn execute(command: Cmd, as_json: bool) -> Result<(), String> { println!("Horizon is in the applications menu."); } } + Cmd::Modpack { action } => modpack(action.unwrap_or(ModpackCmd::List), as_json)?, Cmd::Mcp => unreachable!(), } Ok(()) @@ -591,3 +655,115 @@ fn data(action: DataCmd, as_json: bool) -> Result<(), String> { } Ok(()) } + +fn modpack(action: ModpackCmd, as_json: bool) -> Result<(), String> { + use horizon_launcher::modpacks::{self, Imported}; + use horizon_port_pack::report::Status; + let log = &|line: String| eprintln!("{line}"); + let work = |pack: &str| { + let work = modpacks::ports_dir().join(pack); + modpacks::load(&work).map(|_| work) + }; + let show = |p: &Imported| { + let counts: Vec<String> = Status::ALL + .iter() + .map(|s| format!("{} {}", p.count(*s), s.label())) + .collect(); + if as_json { + json(serde_json::json!({ + "pack": p.slug, "name": p.title(), "summary": p.summary(), + "counts": Status::ALL.iter().map(|s| (s.label(), p.count(*s))).collect::<std::collections::BTreeMap<_, _>>(), + "portable": p.portable, "ported_mods": p.mods.len(), "jars": p.jars(), + "report": p.work.join("coverage.md"), + })); + } else { + println!( + "{} ({})\n {}\n {}", + p.title(), + p.slug, + p.summary(), + counts.join(", ") + ); + println!(" {} portable, {} carried over", p.portable, p.mods.len()); + if let Some(jars) = p.jars() { + println!(" {jars}"); + } + println!(" {}", p.work.join("coverage.md").display()); + } + }; + match action { + ModpackCmd::List => { + let packs = modpacks::list(); + if packs.is_empty() && !as_json { + println!("No modpack imported yet: horizon-launcher modpack import <pack>"); + } + packs.iter().for_each(show); + } + ModpackCmd::Import { path } => show(&modpacks::import(&path, log)?), + ModpackCmd::Port { + pack, + from, + version, + } => { + let tag = backend::resolve_version(version.as_deref()).ok(); + let resources = tag + .filter(|t| backend::is_prepared(t)) + .map(|t| backend::resources(&t)); + show(&modpacks::port( + &work(&pack)?, + resources.as_deref(), + from.as_deref(), + log, + )?) + } + ModpackCmd::Play { pack, version } => { + let mut cmd = backend::client(&backend::resolve_version(version.as_deref())?)?; + modpacks::apply_profile(&mut cmd, &work(&pack)?)?; + attached(cmd)? + } + ModpackCmd::Remove { pack } => modpacks::remove(&work(&pack)?)?, + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use clap::Parser; + + #[test] + fn play_takes_extra_arguments_that_start_with_hyphens() { + let cli = Cli::try_parse_from([ + "horizon-launcher", + "play", + "--arg", + "--server-thread", + "--arg", + "40", + ]) + .unwrap(); + match cli.command { + Some(Cmd::Play { arg, .. }) => assert_eq!(arg, ["--server-thread", "40"]), + _ => panic!("expected play"), + } + } + + #[test] + fn game_args_takes_one_line_that_starts_with_hyphens() { + let cli = Cli::try_parse_from([ + "horizon-launcher", + "game-args", + "--server-thread --render-distance 40", + ]) + .unwrap(); + match cli.command { + Some(Cmd::GameArgs { args }) => { + assert_eq!( + args.as_deref(), + Some("--server-thread --render-distance 40") + ); + } + _ => panic!("expected game-args"), + } + } +} diff --git a/launcher/src/lib.rs b/launcher/src/lib.rs index 5df6a7cbb..11023a46d 100644 --- a/launcher/src/lib.rs +++ b/launcher/src/lib.rs @@ -2,6 +2,7 @@ pub mod account; pub mod backend; pub mod desktop; +pub mod modpacks; pub mod mojang; pub mod sources; pub mod telemetry; diff --git a/launcher/src/main.rs b/launcher/src/main.rs index cc66aa08e..ca86b5b08 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -6,6 +6,7 @@ mod collection; mod game_tools; mod markdown; mod mcp; +mod modpacks_page; mod perf; mod theme; @@ -29,14 +30,16 @@ use theme::Theme; enum Page { Play, Versions, + Modpacks, Notes, Settings, } impl Page { - const ALL: [(Page, &'static str); 4] = [ + const ALL: [(Page, &'static str); 5] = [ (Page::Play, "Play"), (Page::Versions, "Versions"), + (Page::Modpacks, "Modpacks"), (Page::Notes, "Patch notes"), (Page::Settings, "Settings"), ]; @@ -128,6 +131,8 @@ struct Launcher { client_error: Option<String>, /// When the running client started, to tell its crash report from older ones. client_started: Option<std::time::SystemTime>, + /// The folder the running game keeps its settings in: the game home, or a modpack's. + client_home: Option<std::path::PathBuf>, crash: Option<Crash>, server: Option<Running>, /// Microsoft accounts (accounts.json), the sign-in under way and what it last showed. @@ -148,6 +153,10 @@ struct Launcher { friend_focused: bool, friend_focus: FocusHandle, joining: Option<String>, + /// Settings' Game arguments field: the line, whether it has focus, and its focus handle. + args_input: String, + args_focused: bool, + args_focus: FocusHandle, console_scroll: ScrollHandle, /// Opt-in data collection: the player's answer and Settings' section (`collection`). collection: collection::Collection, @@ -161,6 +170,17 @@ struct Launcher { play_pressed: Option<Instant>, /// `horizon-launcher benchmark` started this window to measure it. bench: Option<perf::Bench>, + /// The Modpacks page: the imported packs (read when the page first opens), the one shown, + /// the report's filter, and the pipeline step running now with its log. + packs: Vec<horizon_launcher::modpacks::Imported>, + packs_loaded: Option<Instant>, + pack_open: Option<String>, + pack_filter: Option<horizon_port_pack::report::Status>, + pack_busy: bool, + pack_log: VecDeque<String>, + pack_error: Option<String>, + /// The game starting now plays this pack's profile (its work folder). + starting_pack: Option<std::path::PathBuf>, } impl Launcher { @@ -231,6 +251,9 @@ impl Launcher { install_error: None, confirm_remove: None, picker_open: false, + args_input: settings.game_args.clone(), + args_focused: false, + args_focus: cx.focus_handle(), settings, links: backend::Links::default(), menu_style: backend::menu_style(), @@ -240,6 +263,7 @@ impl Launcher { client: None, client_error: None, client_started: None, + client_home: None, crash: None, server: None, accounts: Accounts::load(), @@ -266,6 +290,14 @@ impl Launcher { prepare_started: None, play_pressed: None, bench: perf::Bench::from_env(), + packs: Vec::new(), + packs_loaded: None, + pack_open: None, + pack_filter: None, + pack_busy: false, + pack_log: VecDeque::new(), + pack_error: None, + starting_pack: None, }; if this.bench.is_some() { this.perf.keep_local_log(false); @@ -406,11 +438,21 @@ impl Launcher { Err(e) => self.client_error = Some(format!("Playing offline: {e}")), } self.accounts = Accounts::load(); + if let Some(work) = self.starting_pack.take() { + if let Err(e) = + horizon_launcher::modpacks::apply_profile(&mut cmd, &work) + { + self.client_error = Some(e); + continue; + } + } if let Some(code) = self.joining.take() { if let Err(e) = backend::join_friend(&mut cmd, &code) { self.client_error = Some(e); } } + backend::append_game_args(&mut cmd); + self.client_home = cmd.get_current_dir().map(|d| d.to_path_buf()); match backend::spawn(Job::Client, cmd, self.tx.clone()) { Ok(r) => self.client = Some(r), Err(e) => self.client_error = Some(e), @@ -473,6 +515,13 @@ impl Launcher { Err(e) => SelfUpdate::Failed(e), }; } + Msg::PackLine(line) => { + self.pack_log.push_back(line); + if self.pack_log.len() > LOG_LINES { + self.pack_log.pop_front(); + } + } + Msg::PackDone(result) => self.pack_done(result), Msg::Line(job, line) => { if job == Job::Client && line.starts_with(perf::GAME_FIRST_FRAME) { if let Some(pressed) = self.play_pressed.take() { @@ -519,7 +568,15 @@ impl Launcher { .client_started .take() .and_then(backend::crash_report_since); - if error.is_some() || report.is_some() { + if error.is_some() && report.is_none() && self.switched_to_dx12() { + self.client_error = Some( + "Horizon's Vulkan renderer crashed while starting on this \ + graphics card, so the game now uses DirectX 12. Press Play \ + to start it again. Video Settings > Graphics API switches \ + back." + .into(), + ); + } else if error.is_some() || report.is_some() { let summary = report .as_ref() .and_then(|p| std::fs::read_to_string(p).ok()) @@ -582,6 +639,27 @@ impl Launcher { } } + /// After a crash with no report: when the game's output ends while its Vulkan renderer + /// started, a native crash in the graphics driver (#746), switches the game to DirectX 12 + /// (Windows only; only where the player left the graphics API on Auto). True when it did. + fn switched_to_dx12(&self) -> bool { + if !cfg!(windows) || !backend::vulkan_start_crashed(&self.prepare_log) { + return false; + } + // The GPU is the same for every profile: the game home's setting and the crashed run's. + let home = backend::game_home(); + let mut homes = vec![home.clone()]; + homes.extend(self.client_home.clone().filter(|h| *h != home)); + let mut switched = false; + for home in homes { + match backend::switch_to_dx12(&home) { + Ok(changed) => switched |= changed, + Err(e) => eprintln!("Horizon launcher: could not switch to DirectX 12: {e}"), + } + } + switched + } + fn selected(&self) -> Option<&String> { self.settings .selected @@ -671,37 +749,8 @@ impl Launcher { if self.client.is_some() || self.starting_client.is_some() { return; } - self.client_error = None; - self.crash = None; - self.prepare_log.clear(); - // A little earlier than the spawn: file times can be coarse. - self.client_started = - Some(std::time::SystemTime::now() - std::time::Duration::from_secs(2)); - // The session (a network round trip when the token needs renewing) comes first; - // `drain` starts the game when it arrives. - self.starting_client = Some(tag); - self.play_pressed = Some(Instant::now()); - let tx = self.tx.clone(); - std::thread::spawn(move || { - let session = (|| { - let mut accounts = Accounts::load(); - let Some((a, secrets)) = account::session_for_launch( - &account::Endpoints::live(), - account::store(), - &mut accounts, - )? - else { - return Ok(None); - }; - account::write_session_file( - &backend::game_home().join("sessions"), - &a, - &secrets, - ) - .map(Some) - })(); - let _ = tx.send(Msg::Session(session)); - }); + self.starting_pack = None; + self.start_client(tag); } Mode::Server => { if let Some(server) = &mut self.server { @@ -729,6 +778,38 @@ impl Launcher { } } + /// Starts the game: the session first (a network round trip when the token needs + /// renewing), then `drain` starts the process when it arrives. + fn start_client(&mut self, tag: String) { + self.client_error = None; + self.crash = None; + self.prepare_log.clear(); + // A little earlier than the spawn: file times can be coarse. + self.client_started = + Some(std::time::SystemTime::now() - std::time::Duration::from_secs(2)); + // The session (a network round trip when the token needs renewing) comes first; + // `drain` starts the game when it arrives. + self.starting_client = Some(tag); + self.play_pressed = Some(Instant::now()); + let tx = self.tx.clone(); + std::thread::spawn(move || { + let session = (|| { + let mut accounts = Accounts::load(); + let Some((a, secrets)) = account::session_for_launch( + &account::Endpoints::live(), + account::store(), + &mut accounts, + )? + else { + return Ok(None); + }; + account::write_session_file(&backend::game_home().join("sessions"), &a, &secrets) + .map(Some) + })(); + let _ = tx.send(Msg::Session(session)); + }); + } + fn choose_folder(&mut self, server: bool, cx: &mut Context<Self>) { let paths = cx.prompt_for_paths(PathPromptOptions { files: false, @@ -1986,23 +2067,24 @@ impl Launcher { Some(menus.into_any_element()), &t, )) - .child(index_row(n + 4, "Horizon", Some(plain(&horizon)), horizon_action, &t)) + .child(self.game_args_row(n + 4, cx)) + .child(index_row(n + 5, "Horizon", Some(plain(&horizon)), horizon_action, &t)) .child(index_row( - n + 5, + n + 6, "Launcher", Some(plain(&launcher)), launcher_action, &t, )) .child(index_row( - n + 6, + n + 7, "Data folder", Some(mono(short(&backend::data_dir()))), None, &t, )) .child(index_row( - n + 7, + n + 8, "Logs", Some(mono(format!( "{} · crash reports beside it", @@ -2021,7 +2103,7 @@ impl Launcher { )) .when(cfg!(target_os = "linux"), |d| { d.child(index_row( - n + 8, + n + 9, "Applications menu", Some(plain(if self.settings.desktop_entry() { "Horizon and its icon are in your desktop's menu." @@ -2037,6 +2119,80 @@ impl Launcher { .child(agents) } + /// Settings' Game arguments row: the extra arguments the game starts with, kept in + /// `settings.json` and appended to the launcher's own (quotes group a word with spaces). + fn game_args_row(&self, n: usize, cx: &mut Context<Self>) -> Div { + let t = self.theme; + let focused = self.args_focused; + let field = div() + .id("game-args") + .track_focus(&self.args_focus) + .flex() + .items_center() + .w(px(300.)) + .h(px(30.)) + .px(px(12.)) + .overflow_hidden() + .rounded(px(6.)) + .border_1() + .border_color(if focused { t.accent } else { t.rule }) + .bg(t.surface) + .font_family(theme::MONO) + .text_size(px(12.)) + .cursor_text() + .child(if self.args_input.is_empty() && !focused { + div() + .text_color(t.muted) + .child("For example --server-thread") + } else { + // The end of a long line stays in view while typing. + let chars: Vec<char> = self.args_input.chars().collect(); + let shown: String = chars[chars.len().saturating_sub(44)..].iter().collect(); + div().text_color(t.ink).child(shown) + }) + .when(focused, |d| d.child(div().w(px(1.)).h(px(14.)).bg(t.ink))) + .on_click(cx.listener(|this, _: &ClickEvent, window, cx| { + window.focus(&this.args_focus); + cx.notify(); + })) + .on_key_down(cx.listener(|this, ev: &KeyDownEvent, _, cx| { + let k = &ev.keystroke; + let paste = k.key == "v" && (k.modifiers.control || k.modifiers.platform); + match k.key.as_str() { + "backspace" => { + this.args_input.pop(); + } + _ if paste => { + if let Some(text) = cx.read_from_clipboard().and_then(|c| c.text()) { + this.args_input.push_str(text.trim()); + } + } + _ if !k.modifiers.control && !k.modifiers.platform => { + if let Some(ch) = &k.key_char { + this.args_input.push_str(ch); + } + } + _ => return, + } + this.args_input.truncate(1024); + this.settings.game_args = this.args_input.clone(); + this.settings.save(); + cx.stop_propagation(); + cx.notify(); + })); + index_row( + n, + "Game arguments", + Some( + div() + .child("Extra arguments for the game, passed through as-is (quotes group a word with spaces). Applies the next time the game starts.") + .into_any_element(), + ), + Some(field.into_any_element()), + &t, + ) + } + /// Settings' Microsoft accounts: who is signed in and plays, the sign-in with its code. The /// rows are numbered from 01; returns them and how many there are. fn account_rows(&mut self, cx: &mut Context<Self>) -> (Div, usize) { @@ -2771,9 +2927,11 @@ impl Render for Launcher { self.perf.frame(); self.input_focused = self.focus.is_focused(window); self.friend_focused = self.friend_focus.is_focused(window); + self.args_focused = self.args_focus.is_focused(window); let content = match self.page { Page::Play => self.play_page(cx), Page::Versions => self.versions_page(cx), + Page::Modpacks => self.modpacks_page(cx), Page::Notes => self.notes_page(cx), Page::Settings => self.settings_page(cx), }; diff --git a/launcher/src/mcp.rs b/launcher/src/mcp.rs index 82304923f..2dd2aa5cb 100644 --- a/launcher/src/mcp.rs +++ b/launcher/src/mcp.rs @@ -37,7 +37,8 @@ fn tools() -> Value { "inputSchema": { "type": "object", "properties": { "version": version, "world": { "type": "string", "description": "Open or create this world (a folder under saves) at once." }, "creative": { "type": "boolean", "description": "Creative mode for a new world." }, - "offscreen": { "type": "boolean", "description": "Render without showing a window (game_screenshot still works)." } } } }, + "offscreen": { "type": "boolean", "description": "Render without showing a window (game_screenshot still works)." }, + "args": { "type": "array", "items": { "type": "string" }, "description": "Extra game arguments, passed through as-is (for example \"--server-thread\")." } } } }, { "name": "start_server", "description": "Start a Horizon dedicated server. It runs until stop_server or the end of this MCP session. Requires agreeing to the Minecraft EULA.", "inputSchema": { "type": "object", "properties": { "version": version, "dir": { "type": "string", "description": "The server's folder. Default: the launcher's server folder." }, "accept_eula": { "type": "boolean", "description": "Agree to the Minecraft EULA (https://aka.ms/MinecraftEULA) on the player's behalf. Only with their consent." } } } }, { "name": "server_command", "description": "Send a console command to the running server (for example \"list\" or \"say hello\"), and return the lines it printed in reply.", @@ -166,6 +167,9 @@ impl Session { cmd.arg(format!("--{flag}")); } } + if let Some(extra) = args.get("args").and_then(Value::as_array) { + cmd.args(extra.iter().filter_map(Value::as_str)); + } let client = Logged::start(Job::Client, cmd)?; // Started once its control socket answers, as horizon-ctl launch waits. let deadline = std::time::Instant::now() + Duration::from_secs(120); diff --git a/launcher/src/modpacks.rs b/launcher/src/modpacks.rs new file mode 100644 index 000000000..b69ff0976 --- /dev/null +++ b/launcher/src/modpacks.rs @@ -0,0 +1,256 @@ +//! Modpacks: import a Java modpack (a Modrinth `.mrpack`, a CurseForge zip, an instance folder), +//! show what Horizon runs of it, port that on this computer from the player's own downloads, and +//! play it as a profile of its own (docs/modpacks.md). +//! +//! Each pack lives in `ports/<slug>/` in the game's home: the work folder `horizon-port-pack` +//! writes (pack.json, inventory, the jars, the carried-over mods, the coverage report) and +//! `home/`, the profile's own saves and options. Nothing is uploaded or shared: mod jars are +//! often All Rights Reserved, so the ports stay on the player's machine. +use crate::backend; +use horizon_port_pack::{ + fetch::{Fetched, How}, + import::Pack, + pipeline::{self, Log}, + report::{Row, Status}, +}; +use std::fs; +use std::path::{Path, PathBuf}; +use std::process::Command; + +/// Where imported packs live. +pub fn ports_dir() -> PathBuf { + backend::game_home().join("ports") +} + +/// A pack's folder name: its name in lowercase words joined by dashes. +pub fn slug(name: &str) -> String { + let words: Vec<String> = name + .split(|c: char| !c.is_ascii_alphanumeric()) + .filter(|w| !w.is_empty()) + .map(str::to_ascii_lowercase) + .collect(); + if words.is_empty() { + "pack".into() + } else { + words.join("-") + } +} + +/// An imported pack, as its work folder has it. +pub struct Imported { + pub slug: String, + pub work: PathBuf, + pub pack: Pack, + pub rows: Vec<Row>, + pub fetched: Fetched, + /// The carried-over mods the profile loads. + pub mods: Vec<PathBuf>, + /// The mods the port step gets and carries over (content, data tier). + pub portable: usize, +} + +impl Imported { + pub fn title(&self) -> &str { + if self.pack.name.is_empty() { + &self.slug + } else { + &self.pack.name + } + } + pub fn count(&self, status: Status) -> usize { + self.rows.iter().filter(|r| r.status == status).count() + } + /// "Minecraft 1.21.1 · neoforge 21.1.259 · 496 mods". + pub fn summary(&self) -> String { + let mut parts = Vec::new(); + if !self.pack.minecraft.is_empty() { + parts.push(format!("Minecraft {}", self.pack.minecraft)); + } + if !self.pack.loader.is_empty() { + parts.push(format!("{} {}", self.pack.loader, self.pack.loader_version)); + } + parts.push(format!("{} files", self.pack.files.len())); + parts.join(" · ") + } + /// What getting the jars did, in one line, or None before it ran. + pub fn jars(&self) -> Option<String> { + if self.fetched.files.is_empty() { + return None; + } + let mut parts = Vec::new(); + for (how, what) in [ + (How::Downloaded, "downloaded"), + (How::Local, "copied from your folder"), + (How::Present, "already here"), + ( + How::NeedsKey, + "only on CurseForge (needs its API key: choose your installed instance instead)", + ), + (How::NotFound, "not found"), + (How::Failed, "failed"), + ] { + let n = self.fetched.count(how); + if n > 0 { + parts.push(format!("{n} {what}")); + } + } + Some(format!("Jars: {}.", parts.join(", "))) + } +} + +/// Reads the pack in `work`. +pub fn load(work: &Path) -> Result<Imported, String> { + let pack: Pack = pipeline::read_json(&work.join("pack.json"))?; + let rows: Vec<Row> = pipeline::read_json(&work.join("coverage.json")).unwrap_or_default(); + let records: Vec<horizon_port_pack::inventory::Record> = + pipeline::read_json(&work.join("inventory.json")).unwrap_or_default(); + Ok(Imported { + slug: work + .file_name() + .map(|n| n.to_string_lossy().into_owned()) + .unwrap_or_default(), + work: work.to_path_buf(), + pack, + rows, + fetched: Fetched::load(work), + mods: carried(work), + portable: records.iter().filter(|r| pipeline::portable(r)).count(), + }) +} + +/// Every imported pack, by name. +pub fn list() -> Vec<Imported> { + let mut packs: Vec<Imported> = fs::read_dir(ports_dir()) + .into_iter() + .flatten() + .flatten() + .filter_map(|e| load(&e.path()).ok()) + .collect(); + packs.sort_by_key(|p| p.title().to_ascii_lowercase()); + packs +} + +/// The carried-over mods in a work folder (`mods/<id>/carry/` with its manifest). +pub fn carried(work: &Path) -> Vec<PathBuf> { + let mut mods: Vec<PathBuf> = fs::read_dir(work.join("mods")) + .into_iter() + .flatten() + .flatten() + .map(|e| e.path().join("carry")) + .filter(|c| c.join("horizon-mod.toml").is_file()) + .collect(); + mods.sort(); + mods +} + +/// Imports the pack at `path` into `ports/<slug>/` and writes its coverage report from +/// metadata (Modrinth answers by file hash; nothing is downloaded yet). Importing the same pack +/// again keeps the jars and ports it already has. +pub fn import(path: &Path, log: Log) -> Result<Imported, String> { + let pack = horizon_port_pack::import::import(path)?; + let name = if pack.name.is_empty() || pack.name == "modlist" { + path.file_stem() + .map(|s| s.to_string_lossy().into_owned()) + .unwrap_or_default() + } else { + pack.name.clone() + }; + let work = ports_dir().join(slug(&name)); + log(format!("Importing {name} into {}", work.display())); + pipeline::import(path, &work, log)?; + pipeline::inventory(&work, false, log)?; + pipeline::report(&work, log)?; + load(&work) +} + +/// Ports what this computer can: the portable mods' jars (from `from` when the player has them, +/// else the pack's links or Modrinth), carried over for `resources`, then the report again. +pub fn port( + work: &Path, + resources: Option<&Path>, + from: Option<&Path>, + log: Log, +) -> Result<Imported, String> { + pipeline::port(work, resources, from, log)?; + load(work) +} + +/// Removes an imported pack: its jars, ports and the profile's saves. +pub fn remove(work: &Path) -> Result<(), String> { + if work.parent() != Some(ports_dir().as_path()) { + return Err(format!("{} is not an imported pack", work.display())); + } + fs::remove_dir_all(work).map_err(|e| format!("{}: {e}", work.display())) +} + +/// The profile's own game home: saves, options.txt, logs. +pub fn profile_home(work: &Path) -> PathBuf { + work.join("home") +} + +/// Turns the game's command into this pack's profile: its own home (the player's options.txt +/// copied in the first time, so keys and video settings carry over) and its carried-over mods. +pub fn apply_profile(cmd: &mut Command, work: &Path) -> Result<(), String> { + let mods = carried(work); + if mods.is_empty() { + return Err("Nothing is ported yet: port the pack first.".into()); + } + let home = profile_home(work); + fs::create_dir_all(&home).map_err(|e| format!("{}: {e}", home.display()))?; + let options = home.join("options.txt"); + let own = backend::game_home().join("options.txt"); + if !options.exists() && own.is_file() { + let _ = fs::copy(own, &options); + } + cmd.current_dir(&home).env("HORIZON_HOME", &home); + for m in mods { + cmd.arg("--dev-mod").arg(m); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn slugs() { + assert_eq!(slug("All the Mods 10"), "all-the-mods-10"); + assert_eq!(slug("Create: Above & Beyond!"), "create-above-beyond"); + assert_eq!(slug(" "), "pack"); + } + + #[test] + fn a_profile_has_its_own_home_and_mods() { + let root = + std::env::temp_dir().join(format!("horizon-launcher-pack-{}", std::process::id())); + let _ = fs::remove_dir_all(&root); + let work = root.join("ports/small"); + let mut cmd = Command::new("true"); + assert!(apply_profile(&mut cmd, &work).is_err()); + for id in ["a", "b"] { + let carry = work.join("mods").join(id).join("carry"); + fs::create_dir_all(&carry).unwrap(); + fs::write(carry.join("horizon-mod.toml"), "").unwrap(); + } + // Left out: no manifest. + fs::create_dir_all(work.join("mods/c/carry")).unwrap(); + apply_profile(&mut cmd, &work).unwrap(); + let args: Vec<String> = cmd + .get_args() + .map(|a| a.to_string_lossy().into_owned()) + .collect(); + assert_eq!(args.len(), 4); + assert_eq!( + Path::new(&args[1]), + work.join("mods").join("a").join("carry") + ); + assert_eq!( + Path::new(&args[3]), + work.join("mods").join("b").join("carry") + ); + assert_eq!(cmd.get_current_dir(), Some(profile_home(&work).as_path())); + assert!(profile_home(&work).is_dir()); + fs::remove_dir_all(&root).unwrap(); + } +} diff --git a/launcher/src/modpacks_page.rs b/launcher/src/modpacks_page.rs new file mode 100644 index 000000000..2398421c6 --- /dev/null +++ b/launcher/src/modpacks_page.rs @@ -0,0 +1,412 @@ +//! The Modpacks page: import a pack, read its coverage report, port what this computer can and +//! play it as its own profile (`horizon_launcher::modpacks`). +use super::*; +use horizon_launcher::modpacks::{self, Imported}; +use horizon_port_pack::report::Status; +use std::path::PathBuf; + +/// The report's filter: every row, or one status. +pub(super) const FILTERS: [(Option<Status>, &str); 5] = [ + (None, "All"), + (Some(Status::Ported), "Ported"), + (Some(Status::Partial), "Partial"), + (Some(Status::NotYet), "Not yet"), + (Some(Status::NotNeeded), "Not needed"), +]; + +/// Lines of the pipeline's log the page shows while it works. +const LOG_SHOWN: usize = 6; + +impl Launcher { + fn open_pack(&self) -> Option<&Imported> { + let slug = self.pack_open.as_ref()?; + self.packs.iter().find(|p| &p.slug == slug) + } + + /// Runs a pipeline step on a thread; its lines and its end arrive as messages. + fn pack_job( + &mut self, + job: impl FnOnce(&dyn Fn(String)) -> Result<Imported, String> + Send + 'static, + ) { + if self.pack_busy { + return; + } + self.pack_busy = true; + self.pack_log.clear(); + self.pack_error = None; + let tx = self.tx.clone(); + std::thread::spawn(move || { + let lines = tx.clone(); + let log = move |line: String| { + let _ = lines.send(Msg::PackLine(line)); + }; + let result = job(&log); + let _ = tx.send(Msg::PackDone(result.map(Box::new))); + }); + } + + /// A finished step: the pack's new state, opened. + pub(super) fn pack_done(&mut self, result: Result<Box<Imported>, String>) { + self.pack_busy = false; + match result { + Ok(pack) => { + self.pack_open = Some(pack.slug.clone()); + self.packs.retain(|p| p.slug != pack.slug); + self.packs.push(*pack); + self.packs.sort_by_key(|p| p.title().to_ascii_lowercase()); + } + Err(e) => self.pack_error = Some(e), + } + } + + fn choose_pack(&mut self, folder: bool, cx: &mut Context<Self>) { + let paths = cx.prompt_for_paths(PathPromptOptions { + files: !folder, + directories: folder, + multiple: false, + prompt: Some("Import".into()), + }); + cx.spawn(async move |this, cx| { + if let Ok(Ok(Some(mut paths))) = paths.await { + if let Some(path) = paths.pop() { + let _ = this.update(cx, |this, cx| { + this.pack_job(move |log| modpacks::import(&path, log)); + cx.notify(); + }); + } + } + }) + .detach(); + } + + /// Ports the open pack; `from` is a folder of the player's jars, chosen when `ask`. + fn port_pack(&mut self, ask: bool, cx: &mut Context<Self>) { + let Some(work) = self.open_pack().map(|p| p.work.clone()) else { + return; + }; + let resources = self + .selected() + .filter(|tag| backend::is_prepared(tag)) + .map(|tag| backend::resources(tag)); + if !ask { + self.pack_job(move |log| modpacks::port(&work, resources.as_deref(), None, log)); + return; + } + let paths = cx.prompt_for_paths(PathPromptOptions { + files: false, + directories: true, + multiple: false, + prompt: Some("Use these jars".into()), + }); + cx.spawn(async move |this, cx| { + if let Ok(Ok(Some(mut paths))) = paths.await { + if let Some(from) = paths.pop() { + let _ = this.update(cx, |this, cx| { + this.pack_job(move |log| { + modpacks::port(&work, resources.as_deref(), Some(&from), log) + }); + cx.notify(); + }); + } + } + }) + .detach(); + } + + fn play_pack(&mut self, work: PathBuf) { + let Some(tag) = self.selected().cloned() else { + return; + }; + if self.client.is_some() || self.starting_client.is_some() { + return; + } + self.starting_pack = Some(work); + self.start_client(tag); + } + + pub(super) fn modpacks_page(&mut self, cx: &mut Context<Self>) -> Div { + let t = self.theme; + if self.packs_loaded.is_none() { + self.packs = modpacks::list(); + if self.pack_open.is_none() { + self.pack_open = self.packs.first().map(|p| p.slug.clone()); + } + self.packs_loaded = Some(Instant::now()); + } + let header = page_header( + "Modpacks", + "Bring a Java modpack: see what Horizon runs of it, port that on this computer from \ + your own downloads, and play it as its own profile. Nothing is uploaded or shared.", + &t, + ); + let busy = self.pack_busy; + let pick = |id: &'static str, label: &'static str, folder: bool| { + let b = button(id, label, Kind::Secondary, &t); + if busy { + disabled(b) + } else { + b.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + this.choose_pack(folder, cx); + cx.notify(); + })) + } + }; + let actions = div() + .flex() + .flex_wrap() + .items_center() + .gap(px(8.)) + .child(pick("import-file", "Import a pack file…", false)) + .child(pick("import-folder", "Import an instance folder…", true)) + .child(text(theme::CAPTION).text_color(t.muted).child( + ".mrpack (Modrinth), .zip (CurseForge), or a Prism, MultiMC or CurseForge instance", + )); + let mut page = div() + .flex() + .flex_col() + .gap(px(32.)) + .child(header) + .child(actions); + if busy || !self.pack_log.is_empty() { + page = page.child(self.pack_log_plate(&t)); + } + if let Some(e) = &self.pack_error { + page = page.child(notice(e.clone(), &t)); + } + if self.packs.is_empty() { + return page.child(centered( + "No modpack yet. Import one to see what Horizon can run of it.", + &t, + )); + } + if self.packs.len() > 1 { + let mut list = div().flex().flex_col().border_t_1().border_color(t.rule); + for (i, p) in self.packs.iter().enumerate() { + let open = self.pack_open.as_ref() == Some(&p.slug); + let slug = p.slug.clone(); + let right = if open { + caps("Open").text_color(t.accent).into_any_element() + } else { + button(format!("open-{slug}"), "Open", Kind::Ghost, &t) + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + this.pack_open = Some(slug.clone()); + cx.notify(); + })) + .into_any_element() + }; + list = list.child(index_row( + i + 1, + p.title().to_string(), + Some(p.summary().into_any_element()), + Some(right), + &t, + )); + } + page = page.child(list); + } + match self.pack_detail(cx) { + Some(detail) => page.child(detail), + None => page, + } + } + + fn pack_log_plate(&self, t: &Theme) -> Div { + let start = self.pack_log.len().saturating_sub(LOG_SHOWN); + plate(t) + .flex() + .flex_col() + .gap(px(4.)) + .p(px(16.)) + .child( + caps(if self.pack_busy { "Working" } else { "Done" }) + .text_color(if self.pack_busy { t.accent } else { t.muted }), + ) + .children(self.pack_log.iter().skip(start).map(|line| { + text(theme::CAPTION) + .font_family(theme::MONO) + .text_color(t.ink_soft) + .truncate() + .child(line.clone()) + })) + } + + fn pack_detail(&self, cx: &mut Context<Self>) -> Option<Div> { + let t = self.theme; + let p = self.open_pack()?; + let mut counts = div().flex().gap(px(12.)); + for (status, label) in FILTERS.iter().skip(1) { + let status = status.expect("the first filter is All"); + let color = match status { + Status::Ported => t.success, + Status::Partial => t.accent, + _ => t.ink, + }; + counts = counts.child( + plate(&t) + .flex() + .flex_col() + .flex_1() + .gap(px(4.)) + .p(px(16.)) + .child( + text(theme::DISPLAY) + .text_color(color) + .child(p.count(status).to_string()), + ) + .child(caps(*label).text_color(t.muted)), + ); + } + let can_play = self.selected().is_some_and(|tag| backend::is_prepared(tag)); + let running = self.client.is_some() || self.starting_client.is_some(); + let mut buttons = div().flex().flex_wrap().items_center().gap(px(8.)); + let play_label = if p.mods.is_empty() { + "Play".to_string() + } else { + format!("Play with {} mods", p.mods.len()) + }; + let mut play = button("pack-play", play_label, Kind::Primary, &t); + if p.mods.is_empty() || !can_play || running || self.pack_busy { + play = disabled(play); + } else { + let work = p.work.clone(); + play = play.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + this.play_pack(work.clone()); + cx.notify(); + })); + } + let port_label = format!("Port {} mods", p.portable); + let mut port = button("pack-port", port_label, Kind::Secondary, &t); + let mut from = button("pack-from", "Use jars from my instance…", Kind::Ghost, &t); + if self.pack_busy || p.portable == 0 { + port = disabled(port); + from = disabled(from); + } else { + port = port.on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.port_pack(false, cx); + cx.notify(); + })); + from = from.on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.port_pack(true, cx); + cx.notify(); + })); + } + let confirming = self.confirm_remove.as_deref() == Some(p.slug.as_str()); + let slug = p.slug.clone(); + let work = p.work.clone(); + let remove = button( + "pack-remove", + if confirming { + "Remove for good" + } else { + "Remove" + }, + if confirming { + Kind::Secondary + } else { + Kind::Ghost + }, + &t, + ) + .when(confirming, |b| b.text_color(t.danger)) + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + if this.confirm_remove.as_ref() == Some(&slug) { + match modpacks::remove(&work) { + Ok(()) => { + this.packs.retain(|p| p.slug != slug); + this.pack_open = this.packs.first().map(|p| p.slug.clone()); + } + Err(e) => this.pack_error = Some(e), + } + this.confirm_remove = None; + } else { + this.confirm_remove = Some(slug.clone()); + } + cx.notify(); + })); + buttons = buttons.child(play).child(port).child(from).child(remove); + let mut notes = vec![format!( + "Port gets the jars of the {} mods this computer can port today (content with data \ + and assets only) from the pack's own links, Modrinth, or your folder, and carries \ + them over. The ports stay here, in {}.", + p.portable, + short(&p.work) + )]; + if let Some(jars) = p.jars() { + notes.push(jars); + } + if !can_play { + notes.push("Playing needs a prepared Horizon version (Play page).".into()); + } + let mut rows: Vec<&horizon_port_pack::report::Row> = p + .rows + .iter() + .filter(|r| self.pack_filter.is_none_or(|s| r.status == s)) + .collect(); + rows.sort_by(|a, b| (a.status, &a.key).cmp(&(b.status, &b.key))); + let filter = segments("pack-filter", &FILTERS, self.pack_filter, &t, { + let this = cx.entity().downgrade(); + move |value, _, cx| { + let _ = this.update(cx, |this, cx| { + this.pack_filter = value; + cx.notify(); + }); + } + }); + let mut list = div().flex().flex_col().border_t_1().border_color(t.rule); + for (i, r) in rows.iter().enumerate() { + let color = match r.status { + Status::Ported => t.success, + Status::Partial => t.accent, + Status::NotYet => t.ink_soft, + Status::NotNeeded => t.muted, + }; + let name = if r.name.is_empty() || r.name == r.key { + r.key.clone() + } else { + format!("{} ({})", r.name, r.key) + }; + let detail = format!("{} · {} · {}: {}", r.class, r.tier, r.license, r.reason); + list = list.child(index_row( + i + 1, + name, + Some(detail.into_any_element()), + Some( + caps(r.status.label()) + .flex_none() + .text_color(color) + .into_any_element(), + ), + &t, + )); + } + Some( + div() + .flex() + .flex_col() + .gap(px(24.)) + .child( + div() + .flex() + .flex_col() + .gap(px(4.)) + .child( + text(theme::TITLE) + .text_color(t.ink) + .child(p.title().to_string()), + ) + .child(text(theme::UI).text_color(t.muted).child(p.summary())), + ) + .child(counts) + .child(buttons) + .children(self.client_error.clone().map(|e| notice(e, &t))) + .children(notes.into_iter().map(|n| { + text(theme::CAPTION) + .max_w(px(720.)) + .text_color(t.muted) + .child(n) + })) + .child(div().flex().child(filter)) + .child(list), + ) + } +} diff --git a/launcher/src/perf.rs b/launcher/src/perf.rs index 18b5d5d33..864f6cfa0 100644 --- a/launcher/src/perf.rs +++ b/launcher/src/perf.rs @@ -22,8 +22,7 @@ pub fn process_started() -> Instant { /// Frames further apart than this are the UI resting, not UI frame times. const IDLE_GAP: Duration = Duration::from_millis(250); -/// The line the game prints when it has drawn its first frame. -pub const GAME_FIRST_FRAME: &str = "Horizon: first frame"; +pub use horizon_launcher::backend::GAME_FIRST_FRAME; pub struct Perf { report: Report, diff --git a/launcher/src/sources.rs b/launcher/src/sources.rs index aafdb51e4..864e6bc73 100644 --- a/launcher/src/sources.rs +++ b/launcher/src/sources.rs @@ -207,6 +207,33 @@ pub(crate) fn sha1_file(path: &Path) -> Option<String> { Some(hash.finalize().iter().map(|b| format!("{b:02x}")).collect()) } +/// The Prism or MultiMC launcher root above `dir` (#693). Their instances live in +/// `instances/<name>/` (Prism's game folder is `minecraft`, MultiMC's `.minecraft`) and the +/// shared `libraries` and assets sit in the launcher root above `instances`, two or three +/// folders above what players naturally pick. +fn instance_root(dir: &Path) -> Option<PathBuf> { + dir.ancestors() + .find(|a| a.file_name() == Some(std::ffi::OsStr::new("instances"))) + .and_then(Path::parent) + .map(Path::to_path_buf) +} + +/// The launcher whose tree `path` is inside, when its folder name is one Horizon knows, so the +/// not-found message can name it (#693). +fn launcher_of(path: &Path) -> Option<&'static str> { + path.ancestors() + .filter_map(Path::file_name) + .filter_map(|name| name.to_str()) + .find_map(|name| match name { + "PrismLauncher" => Some("Prism Launcher"), + "multimc" | "MultiMC" => Some("MultiMC"), + "ModrinthApp" | "com.modrinth.theseus" => Some("the Modrinth App"), + "atlauncher" | "ATLauncher" => Some("ATLauncher"), + "gdlauncher_carbon" | "gdlauncher_next" => Some("GDLauncher"), + _ => None, + }) +} + /// The pinned version in one launcher folder, or what the folder lacks. The folder holding /// `libraries` is the folder itself or its `meta` (Modrinth App), `data` (GDLauncher) or /// `datastore` (GDLauncher Next). @@ -248,19 +275,29 @@ pub fn find(dir: &Path, pins: &Pins) -> Result<Found, String> { } /// The error when no folder has the pinned version: every folder looked in with what it -/// lacks, and what the player can do about it (#419). -pub fn not_found(version: &str, checked: &[(PathBuf, String)]) -> String { +/// lacks, and what the player can do about it (#419, #693). When the chosen folder is inside a +/// launcher Horizon knows, the advice names it: a Prism or MultiMC instance downloads the game +/// only when it is launched once. +pub fn not_found(version: &str, checked: &[(PathBuf, String)], chosen: &Path) -> String { let mut text = format!("Minecraft {version} was not found. Horizon looked in:"); for (dir, missing) in checked { text += &format!("\n {} ({missing})", dir.display()); } - text += &format!( - "\nTo fix it:\n \ - • Play {version} once in the official Minecraft launcher, or in Prism Launcher, MultiMC, \ - the Modrinth App, ATLauncher or GDLauncher, then prepare again.\n \ - • Or choose the folder where your launcher keeps Minecraft {version} (Minecraft folder › \ - Choose… in the launcher, --minecraft on the command line)." - ); + text += "\nTo fix it:"; + text += &match launcher_of(chosen) { + Some(launcher) => format!( + "\n • Launch the {version} instance once in {launcher} so it downloads the game, \ + then prepare again.\n \ + • Or choose the folder {launcher} keeps Minecraft {version} in (Minecraft folder › \ + Choose… in the launcher, --minecraft on the command line)." + ), + None => format!( + "\n • Play {version} once in the official Minecraft launcher, or in Prism Launcher, \ + MultiMC, the Modrinth App, ATLauncher or GDLauncher, then prepare again.\n \ + • Or choose the folder where your launcher keeps Minecraft {version} (Minecraft \ + folder › Choose… in the launcher, --minecraft on the command line)." + ), + }; text } @@ -277,9 +314,9 @@ pub enum Resolved { } /// A Minecraft folder in the official launcher's layout holding the pinned version: the -/// chosen folder when it is one, else the files found in the chosen folder (or a folder above -/// it: a launcher's instance folder may be chosen) or any launcher's default folder, linked into -/// `linked`. +/// chosen folder when it is one, else the files found in the chosen folder, a folder above it +/// (a launcher's instance folder may be chosen, and the launcher root above a Prism or MultiMC +/// `instances` folder is looked in too) or any launcher's default folder, linked into `linked`. pub fn resolve( chosen: &Path, pins: &Pins, @@ -290,13 +327,22 @@ pub fn resolve( ) -> Result<Resolved, String> { let mut checked = Vec::new(); let mut found = None; - for dir in chosen.ancestors().take(4) { - match find(dir, pins) { + // The chosen folder, the folders above it (an instance's own folder may be chosen), and, + // when it is inside a Prism or MultiMC instance tree, the launcher root above `instances` + // that holds their shared `libraries` (#693). + let mut dirs: Vec<PathBuf> = chosen.ancestors().take(4).map(Path::to_path_buf).collect(); + if let Some(root) = instance_root(chosen) { + if !dirs.contains(&root) { + dirs.push(root); + } + } + for dir in dirs { + match find(&dir, pins) { Ok(f) => { found = Some(("the chosen folder", f)); break; } - Err(missing) if dir == chosen => checked.push((dir.to_path_buf(), missing)), + Err(missing) if dir.as_path() == chosen => checked.push((dir, missing)), Err(_) => {} } } @@ -426,16 +472,26 @@ fn make_link(target: &Path, link: &Path) -> std::io::Result<()> { std::os::unix::fs::symlink(target, link) } -/// Symlinks need developer mode on Windows: a junction for a folder, a hard link for a file. +/// Symlinks need developer mode on Windows: a junction for a folder, a hard link for a file (a +/// copy on another volume). #[cfg(windows)] fn make_link(target: &Path, link: &Path) -> std::io::Result<()> { + use std::os::windows::process::CommandExt; if !target.is_dir() { return fs::hard_link(target, link).or_else(|_| fs::copy(target, link).map(|_| ())); } + // Always quoted: cmd reads `&`, `^` or `(` in an unquoted path (a user folder may have + // them) as its own syntax. A Windows path never holds a quote. + let quoted = |path: &Path| { + let mut arg = std::ffi::OsString::from("\""); + arg.push(path); + arg.push("\""); + arg + }; let status = std::process::Command::new("cmd") .args(["/C", "mklink", "/J"]) - .arg(link) - .arg(target) + .raw_arg(quoted(link)) + .raw_arg(quoted(target)) .stdout(std::process::Stdio::null()) .status()?; match status.success() { diff --git a/launcher/src/sources_tests.rs b/launcher/src/sources_tests.rs index f603aaf0a..6d882f032 100644 --- a/launcher/src/sources_tests.rs +++ b/launcher/src/sources_tests.rs @@ -90,19 +90,29 @@ fn resolve_with( &|_| {}, )? { Resolved::Folder(dir) => Ok(dir), - Resolved::NotFound(checked) => Err(not_found("26.3", &checked)), + Resolved::NotFound(checked) => Err(not_found("26.3", &checked, chosen)), } } +/// Whether `a` and `b` are one file, as a symlink (Unix) or a hard link (Windows) makes them: +/// a byte written through `a` shows in `b`. Both are left as they were. +fn same_file(a: &Path, b: &Path) -> bool { + use std::io::Write; + let (before_a, before_b) = (fs::read(a).unwrap(), fs::read(b).unwrap()); + let mut file = fs::OpenOptions::new().write(true).open(a).unwrap(); + file.write_all(&[!before_a[0]]).unwrap(); + drop(file); + let same = fs::read(b).unwrap() != before_b; + fs::write(a, &before_a).unwrap(); + same +} + /// The linked folder is the official layout, pointing at the launcher's files. fn assert_linked(linked: &Path, base: &Path, jar: &str) { let v = linked.join("versions/26.3"); assert_eq!(fs::read(v.join("26.3.jar")).unwrap(), CLIENT); assert_eq!(fs::read(v.join("26.3.json")).unwrap(), MANIFEST); - assert_eq!( - fs::canonicalize(v.join("26.3.jar")).unwrap(), - fs::canonicalize(base.join(jar)).unwrap() - ); + assert!(same_file(&v.join("26.3.jar"), &base.join(jar))); assert_eq!( fs::read(linked.join("libraries/org/lib/1/lib-1.jar")).unwrap(), b"lib" @@ -110,6 +120,19 @@ fn assert_linked(linked: &Path, base: &Path, jar: &str) { assert!(linked.join("assets/indexes/30.json").is_file()); } +/// Folders whose names cmd would read as its own syntax unquoted are still linked (Windows makes +/// a junction through `mklink /J`; a name without spaces isn't quoted by itself). +#[test] +fn folders_with_ampersands_are_linked() { + let s = Scratch::new("a&b(c)"); + let jar = "libraries/com/mojang/minecraft/26.3/minecraft-26.3-client.jar"; + let dir = s.launcher("Prism&Co", "", jar); + let linked = s.0.join("linked&here"); + let got = resolve_with(&dir, &[], &linked, &Mojang::new(MANIFEST)).unwrap(); + assert_eq!(got, linked); + assert_linked(&linked, &dir, jar); +} + #[test] fn maven_names_become_repository_paths() { assert_eq!( @@ -180,6 +203,35 @@ fn prism_launcher_and_multimc_are_read_through_their_metadata() { assert_eq!(mojang.urls.lock().unwrap().len(), 1); } +/// famescare's Prism Launcher 26.3 install on Windows (#849): Prism keeps a native jar under +/// the name its metadata gives it, not at the version JSON's path, and preparing found it. +#[test] +fn prism_native_jars_are_found_through_the_linked_folder() { + let s = Scratch::new("prism-natives"); + let jar = "libraries/com/mojang/minecraft/26.3/minecraft-26.3-client.jar"; + let dir = s.launcher("PrismLauncher", "", jar); + s.write( + "PrismLauncher/meta/net.minecraft/26.3.json", + br#"{"mainJar": {"name": "com.mojang:minecraft:26.3:client"}}"#, + ); + let prism = + "libraries/com/mojang/jtracy-natives-windows/1.14.38/jtracy-natives-windows-1.14.38.jar"; + s.write(&format!("PrismLauncher/{prism}"), b"jtracy natives"); + let linked = s.0.join("linked"); + assert_eq!( + resolve_with(&dir, &[], &linked, &Mojang::new(MANIFEST)).unwrap(), + linked + ); + let official = Path::new("com/mojang/jtracy/1.14.38/jtracy-1.14.38-natives-windows.jar"); + assert!(!linked.join("libraries").join(official).exists()); + let found = horizon_prepare::artifacts::launcher_library( + &linked, + "com.mojang:jtracy:1.14.38:natives-windows", + official, + ); + assert!(same_file(&found, &dir.join(prism))); +} + #[test] fn a_main_jar_elsewhere_is_found_from_the_metadata() { let s = Scratch::new("meta-jar"); @@ -250,6 +302,75 @@ fn an_instance_folder_may_be_chosen() { assert_linked(&linked, &dir, jar); } +/// Every folder a Prism or MultiMC player naturally picks: the instance list, one instance, an +/// instance's game folder and a folder below it. The shared `libraries` are in the launcher +/// root, which the chosen folder's ancestors do not always reach (#693). +#[test] +fn a_prism_instances_folder_finds_the_launcher_root() { + for (name, chosen) in [ + ("list", "instances"), + ("instance", "instances/26.3"), + ("game", "instances/26.3/minecraft"), + // Deeper than the four folders above it: only the `instances` root helps here. + ("deeper", "instances/26.3/minecraft/saves"), + ] { + let s = Scratch::new(&format!("prism-tree-{name}")); + let jar = "libraries/com/mojang/minecraft/26.3/minecraft-26.3-client.jar"; + let dir = s.launcher("PrismLauncher", "", jar); + s.write( + "PrismLauncher/meta/net.minecraft/26.3.json", + br#"{"mainJar": {"name": "com.mojang:minecraft:26.3:client"}}"#, + ); + fs::create_dir_all(dir.join(chosen)).unwrap(); + let linked = s.0.join("linked"); + let got = resolve_with(&dir.join(chosen), &[], &linked, &Mojang::new(MANIFEST)).unwrap(); + assert_eq!(got, linked, "{chosen}"); + assert_linked(&linked, &dir, jar); + } +} + +#[test] +fn a_multimc_instances_folder_finds_the_launcher_root() { + let s = Scratch::new("multimc-tree"); + let jar = "libraries/com/mojang/minecraft/26.3/minecraft-26.3-client.jar"; + let dir = s.launcher("multimc", "", jar); + s.write( + "multimc/meta/net.minecraft/26.3.json", + br#"{"mainJar": {"name": "com.mojang:minecraft:26.3:client"}}"#, + ); + let chosen = dir.join("instances/26.3/.minecraft"); + fs::create_dir_all(&chosen).unwrap(); + let linked = s.0.join("linked"); + let got = resolve_with(&chosen, &[], &linked, &Mojang::new(MANIFEST)).unwrap(); + assert_eq!(got, linked); + assert_linked(&linked, &dir, jar); +} + +/// When the chosen folder is inside a launcher tree, the error names it and says to launch the +/// instance once, instead of listing every launcher (#693). +#[test] +fn the_not_found_message_names_the_launcher_that_was_chosen() { + let s = Scratch::new("prism-hint"); + let dir = s.0.join("PrismLauncher"); + let chosen = dir.join("instances/26.3/minecraft"); + fs::create_dir_all(&chosen).unwrap(); + let error = + resolve_with(&chosen, &[], &s.0.join("linked"), &Mojang::new(MANIFEST)).unwrap_err(); + assert!( + error.contains("Launch the 26.3 instance once in Prism Launcher"), + "{error}" + ); + assert!(!error.contains("or in Prism Launcher, MultiMC"), "{error}"); + // An unknown folder keeps the advice that names every launcher. + let other = s.0.join("elsewhere"); + fs::create_dir_all(&other).unwrap(); + let error = resolve_with(&other, &[], &s.0.join("linked"), &Mojang::new(MANIFEST)).unwrap_err(); + assert!( + error.contains("Prism Launcher, MultiMC, the Modrinth App, ATLauncher or GDLauncher"), + "{error}" + ); +} + #[test] fn the_known_folders_are_searched_after_the_chosen_one() { let s = Scratch::new("known"); @@ -295,10 +416,12 @@ fn relinking_follows_the_launcher_the_files_now_come_from() { let modrinth = s.0.join("ModrinthApp"); s.write("ModrinthApp/meta/versions/26.3/26.3.jar", CLIENT); resolve_with(&modrinth, &[], &linked, &mojang).unwrap(); - assert_eq!( - fs::canonicalize(linked.join("versions/26.3/26.3.jar")).unwrap(), - fs::canonicalize(modrinth.join("meta/versions/26.3/26.3.jar")).unwrap() - ); + let jar = linked.join("versions/26.3/26.3.jar"); + assert!(same_file( + &jar, + &modrinth.join("meta/versions/26.3/26.3.jar") + )); + assert!(!same_file(&jar, &prism.join(prism_jar))); // The Modrinth App folder has no libraries or assets: the old links are gone, and the // folders they pointed at are untouched. assert!(fs::symlink_metadata(linked.join("libraries")).is_err()); diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/EntityTranscript.java b/mods/horizon/src/test/java/dev/horizonlab/parity/EntityTranscript.java new file mode 100644 index 000000000..09c343f16 --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/EntityTranscript.java @@ -0,0 +1,181 @@ +package dev.horizonlab.parity; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.network.protocol.Packet; +import net.minecraft.network.protocol.game.ClientboundAddEntityPacket; +import net.minecraft.network.protocol.game.ClientboundEntityPositionSyncPacket; +import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket; +import net.minecraft.network.protocol.game.ClientboundProjectilePowerPacket; +import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket; +import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket; +import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket; +import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket; +import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket; +import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket; +import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket; +import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket; +import net.minecraft.network.protocol.game.VecDeltaCodec; +import net.minecraft.world.entity.EntityTypes; +import net.minecraft.world.phys.Vec3; + +/** + * What a vanilla client knows about the entities a server tracks to it, as + * ClientPacketListener would apply the packets: each entity's spawn packet, + * the entity data it was paired with and all it was sent since, its + * equipment, attributes and passengers, and where the position syncs leave + * it (VecDeltaCodec, as handleMoveEntity and handleEntityPositionSync). + * Entity ids differ between servers: an entity is named by its type and the + * block it was added in, passengers by those names. Players are left out, but + * for the attributes the player itself is sent from its login on. + */ +final class EntityTranscript { + private static final class Tracked { + final String type; + final String add; + final VecDeltaCodec codec = new VecDeltaCodec(); + String name; + String pairing; + final Map<Integer, String> data = new TreeMap<>(); + final Map<String, String> equipment = new TreeMap<>(); + final Map<String, String> attributes = new TreeMap<>(); + int[] passengers = new int[0]; + float yRot; + float xRot; + float headRot; + boolean onGround; + String motion; + String power = ""; + boolean removed; + + Tracked(ClientboundAddEntityPacket p) { + type = BuiltInRegistries.ENTITY_TYPE.getKey(p.getType()).toString(); + Vec3 at = new Vec3(p.getX(), p.getY(), p.getZ()); + codec.setBase(at); + name = type + " " + (int) Math.floor(at.x) + "," + (int) Math.floor(at.y) + "," + (int) Math.floor(at.z); + add = "at " + at + " rot " + p.getXRot() + "," + p.getYRot() + " head " + p.getYHeadRot() + " data " + p.getData() + + " motion " + p.getMovement(); + yRot = p.getYRot(); + xRot = p.getXRot(); + headRot = p.getYHeadRot(); + motion = String.valueOf(p.getMovement()); + } + } + + private final Map<Integer, Tracked> byId = new HashMap<>(); + private final List<Tracked> all = new ArrayList<>(); + private int self = Integer.MIN_VALUE; + private final Map<String, String> selfAttributes = new TreeMap<>(); + + /** The player's own id (login) and attribute instances; false for any other packet. */ + boolean acceptSelf(Packet<?> packet) { + if (packet instanceof net.minecraft.network.protocol.game.ClientboundLoginPacket p) { + self = p.playerId(); + return true; + } + if (packet instanceof ClientboundUpdateAttributesPacket p && p.getEntityId() == self) { + p.getValues().forEach(a -> selfAttributes.put(a.attribute().getRegisteredName(), a.base() + " " + a.modifiers())); + return true; + } + return false; + } + private int unknown; + + /** Applies one packet; false when it isn't about a tracked entity. */ + boolean accept(Packet<?> packet) { + if (packet instanceof ClientboundAddEntityPacket p) { + if (p.getType() == EntityTypes.PLAYER) return true; + Tracked t = new Tracked(p); + byId.put(p.getId(), t); + all.add(t); + return true; + } + if (packet instanceof ClientboundRemoveEntitiesPacket p) { + p.entityIds().forEach(id -> { + Tracked t = byId.remove(id); + if (t != null) t.removed = true; + }); + return true; + } + Tracked t = switch (packet) { + case ClientboundSetEntityDataPacket p -> byId.get(p.id()); + case ClientboundSetEquipmentPacket p -> byId.get(p.getEntity()); + case ClientboundUpdateAttributesPacket p -> byId.get(p.getEntityId()); + case ClientboundSetPassengersPacket p -> byId.get(p.getVehicle()); + case ClientboundEntityPositionSyncPacket p -> byId.get(p.id()); + case ClientboundSetEntityMotionPacket p -> byId.get(p.id()); + case ClientboundTeleportEntityPacket p -> byId.get(p.id()); + case ClientboundProjectilePowerPacket p -> byId.get(p.getId()); + case ClientboundMoveEntityPacket p -> byId.get(VanillaProtocolClient.moveEntityId(p)); + case ClientboundRotateHeadPacket p -> byId.get(VanillaProtocolClient.rotateHeadId(p)); + default -> null; + }; + if (t == null) { + return false; + } + switch (packet) { + case ClientboundSetEntityDataPacket p -> { + for (var v : p.packedItems()) t.data.put(v.id(), net.minecraft.network.syncher.EntityDataSerializers.getSerializedId(v.serializer()) + " " + v.value()); + if (t.pairing == null) { + StringBuilder b = new StringBuilder(); + for (var v : p.packedItems()) b.append(v.id()).append('=').append(v.value()).append(' '); + t.pairing = b.toString().trim(); + } + } + case ClientboundSetEquipmentPacket p -> p.getSlots().forEach(s -> t.equipment.put(s.getFirst().getName(), String.valueOf(s.getSecond()))); + case ClientboundUpdateAttributesPacket p -> p.getValues().forEach(a -> t.attributes.put(a.attribute().getRegisteredName(), + a.base() + " " + a.modifiers())); + case ClientboundSetPassengersPacket p -> t.passengers = p.getPassengers(); + case ClientboundEntityPositionSyncPacket p -> { + t.codec.setBase(p.position().endPosition()); + t.yRot = p.yRot(); + t.xRot = p.xRot(); + t.onGround = p.onGround(); + } + case ClientboundMoveEntityPacket p -> { + if (p.hasPosition()) t.codec.setBase(p.getPositionDelta().decode(t.codec).endPosition()); + if (p.hasRotation()) { + t.yRot = p.getYRot(); + t.xRot = p.getXRot(); + } + t.onGround = p.isOnGround(); + } + case ClientboundSetEntityMotionPacket p -> t.motion = String.valueOf(p.movement()); + case ClientboundTeleportEntityPacket p -> { + t.codec.setBase(p.change().position()); + t.onGround = p.onGround(); + } + case ClientboundRotateHeadPacket p -> t.headRot = p.getYHeadRot(); + case ClientboundProjectilePowerPacket p -> t.power = " power " + p.getAccelerationPower(); + default -> { + } + } + return true; + } + + /** One line per entity, sorted, each field named. */ + List<String> lines() { + Map<String, Integer> seen = new HashMap<>(); + for (Tracked t : all) { + int n = seen.merge(t.name, 1, Integer::sum); + if (n > 1) t.name += " #" + n; + } + Map<Integer, String> names = new HashMap<>(); + byId.forEach((id, t) -> names.put(id, t.name)); + List<String> out = new ArrayList<>(); + for (Tracked t : all) { + List<String> riders = new ArrayList<>(); + for (int id : t.passengers) riders.add(names.getOrDefault(id, "player or unknown")); + out.add("entity " + t.name + " | add " + t.add + " | pairing " + t.pairing + " | data " + t.data + " | equipment " + t.equipment + + " | attributes " + t.attributes + " | passengers " + riders + " | now " + t.codec.getBase() + " rot " + t.xRot + "," + + t.yRot + " head " + t.headRot + " ground " + t.onGround + " motion " + t.motion + t.power + (t.removed ? " | removed" : "")); + } + out.add("self attributes " + selfAttributes); + out.sort(null); + return out; + } +} diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java b/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java new file mode 100644 index 000000000..3eb241d5e --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java @@ -0,0 +1,460 @@ +package dev.horizonlab.parity; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.zip.Deflater; +import java.util.zip.Inflater; +import net.minecraft.SharedConstants; +import net.minecraft.core.UUIDUtil; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.ProtocolInfo; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.protocol.Packet; +import net.minecraft.network.protocol.common.ClientboundDisconnectPacket; +import net.minecraft.network.protocol.common.ClientboundKeepAlivePacket; +import net.minecraft.network.protocol.common.ClientboundPingPacket; +import net.minecraft.network.protocol.common.ServerboundClientInformationPacket; +import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket; +import net.minecraft.network.protocol.common.ServerboundKeepAlivePacket; +import net.minecraft.network.protocol.common.ServerboundPongPacket; +import net.minecraft.network.protocol.common.custom.BrandPayload; +import net.minecraft.network.protocol.configuration.ClientboundFinishConfigurationPacket; +import net.minecraft.network.protocol.configuration.ClientboundSelectKnownPacks; +import net.minecraft.network.protocol.configuration.ConfigurationProtocols; +import net.minecraft.network.protocol.configuration.ServerboundFinishConfigurationPacket; +import net.minecraft.network.protocol.configuration.ServerboundSelectKnownPacks; +import net.minecraft.network.protocol.game.ClientboundChunkBatchFinishedPacket; +import net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket; +import net.minecraft.network.protocol.game.ClientboundSystemChatPacket; +import net.minecraft.network.protocol.game.ClientboundTrackedWaypointPacket; +import net.minecraft.network.protocol.game.GameProtocols; +import net.minecraft.network.protocol.game.ServerboundAcceptTeleportationPacket; +import net.minecraft.network.protocol.game.ServerboundChatCommandPacket; +import net.minecraft.network.protocol.game.ServerboundChunkBatchReceivedPacket; +import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket; +import net.minecraft.network.protocol.game.ServerboundPlayerInputPacket; +import net.minecraft.network.protocol.game.ServerboundPlayerLoadedPacket; +import net.minecraft.network.protocol.handshake.ClientIntent; +import net.minecraft.network.protocol.handshake.ClientIntentionPacket; +import net.minecraft.network.protocol.handshake.HandshakeProtocols; +import net.minecraft.network.protocol.login.ClientboundLoginCompressionPacket; +import net.minecraft.network.protocol.login.ClientboundLoginDisconnectPacket; +import net.minecraft.network.protocol.login.ClientboundLoginFinishedPacket; +import net.minecraft.network.protocol.login.LoginProtocols; +import net.minecraft.network.protocol.login.ServerboundHelloPacket; +import net.minecraft.network.protocol.login.ServerboundLoginAcknowledgedPacket; +import net.minecraft.server.level.ClientInformation; +import net.minecraft.world.entity.PositionMoveRotation; +import net.minecraft.world.entity.player.Input; +import net.minecraft.world.phys.Vec3; + +/** + * Players made of vanilla's own protocol code, for the locator bar + * (tools/test_locator_bar_parity.py). Each joins in offline mode, answers + * keep-alives, pings, teleports and chunk batches, and records every + * ClientboundTrackedWaypointPacket it receives, decoded by vanilla's codec + * (a byte vanilla would not read is an error) and summarised from its bytes. + * + * <p>{@code LocatorBarClient HOST PORT OUT} takes its script on stdin, one + * line at a time: {@code step LABEL} (the label the next packets are recorded + * under), {@code join NAME}, {@code cmd NAME COMMAND...}, {@code shift NAME + * on|off} (the shift key, as ServerboundPlayerInputPacket), {@code fly NAME + * on|off} (ServerboundPlayerAbilitiesPacket), {@code still NAME} (the + * position reminder LocalPlayer.sendPosition sends every 20 ticks standing + * still), {@code quit NAME}, {@code + * players on|off} (whether each player also records the other players added + * to and removed from its level, and its own respawns into a dimension), + * {@code chat on|off} (whether the system chat each player receives is + * recorded too, as component JSON), {@code state on|off} (whether each player + * also records what the server changes about it: its game mode, teleports, + * inventory slots, effects and death) and {@code end}, which writes each + * player's lines to OUT. + */ +public final class LocatorBarClient { + private static volatile String step = "start"; + private static volatile boolean players; + private static volatile boolean chat; + private static volatile boolean state; + private static final Map<UUID, String> NAMES = new LinkedHashMap<>(); + + public static void main(String[] args) throws Exception { + ParityData.init(); + String host = args[0]; + int port = Integer.parseInt(args[1]); + Path output = Path.of(args[2]); + Map<String, Player> players = new LinkedHashMap<>(); + BufferedReader script = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)); + String line; + while ((line = script.readLine()) != null) { + String[] w = line.trim().split(" ", 3); + try { + switch (w[0]) { + case "step" -> step = w[1]; + case "join" -> { + Player p = new Player(w[1]); + players.put(w[1], p); + NAMES.put(UUIDUtil.createOfflinePlayerUUID(w[1]), w[1]); + p.join(host, port); + } + case "cmd" -> players.get(w[1]).command(w[2]); + case "shift" -> players.get(w[1]).send(new ServerboundPlayerInputPacket( + new Input(false, false, false, false, false, w[2].equals("on"), false))); + case "fly" -> { + net.minecraft.world.entity.player.Abilities abilities = new net.minecraft.world.entity.player.Abilities(); + abilities.flying = w[2].equals("on"); + players.get(w[1]).send(new net.minecraft.network.protocol.game.ServerboundPlayerAbilitiesPacket(abilities)); + } + case "still" -> players.get(w[1]).still(); + case "quit" -> players.get(w[1]).close(); + case "players" -> LocatorBarClient.players = w[1].equals("on"); + case "chat" -> chat = w[1].equals("on"); + case "state" -> state = w[1].equals("on"); + case "end" -> { + try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(output, StandardCharsets.UTF_8))) { + for (Player p : players.values()) { + synchronized (p.lines) { + for (String l : p.lines) out.println(p.name + " " + l); + } + } + } + for (Player p : players.values()) p.close(); + System.exit(0); + } + default -> throw new IllegalArgumentException("unknown script line " + line); + } + } catch (Exception e) { + Player p = w.length > 1 ? players.get(w[1]) : null; + if (p != null) p.record("error " + w[0] + " " + e); + else System.err.println("error " + line + ": " + e); + } + } + System.exit(1); + } + + /** The waypoint packet's fields, read back from its bytes (the operation and type are private enums). */ + static String waypoint(byte[] payload) { + FriendlyByteBuf b = new FriendlyByteBuf(Unpooled.wrappedBuffer(payload)); + b.readVarInt(); // the packet id + String operation = switch (b.readVarInt()) { + case 0 -> "track"; + case 1 -> "untrack"; + case 2 -> "update"; + default -> "operation?"; + }; + String id = b.readBoolean() ? NAMES.getOrDefault(b.readUUID(), "uuid?") : "name:" + b.readUtf(); + String style = b.readUtf(); + String color = b.readBoolean() ? String.format("#%02x%02x%02x", b.readUnsignedByte(), b.readUnsignedByte(), b.readUnsignedByte()) : "-"; + String target = switch (b.readVarInt()) { + case 0 -> "empty"; + case 1 -> "block " + b.readVarInt() + " " + b.readVarInt() + " " + b.readVarInt(); + case 2 -> "chunk " + b.readVarInt() + " " + b.readVarInt(); + case 3 -> "azimuth " + b.readFloat(); + default -> "type?"; + }; + return operation + " " + id + " " + style + " " + color + " " + target; + } + + /** One connection: login and configuration in the caller's thread, then play on its own. */ + private static final class Player { + final String name; + final List<String> lines = new ArrayList<>(); + private Socket socket; + private DataInputStream in; + private OutputStream out; + private int threshold = -1; + private ProtocolInfo<?> inbound; + private ProtocolInfo<?> outbound; + private volatile PositionMoveRotation position = new PositionMoveRotation(Vec3.ZERO, Vec3.ZERO, 0, 0); + private boolean loaded; + /** This player's own entity id (ClientboundLoginPacket). */ + private volatile int id = -1; + /** The other players this one was sent, by entity id. */ + private final Map<Integer, String> seen = new java.util.HashMap<>(); + private final Object replyLock = new Object(); + private int replies; + + Player(String name) { + this.name = name; + } + + /** Runs a command and waits for its feedback, so a slow server still takes each step in its step. */ + void command(String command) throws Exception { + synchronized (replyLock) { + int before = replies; + send(new ServerboundChatCommandPacket(command)); + long until = System.nanoTime() + 30_000_000_000L; + while (replies == before) { + long left = (until - System.nanoTime()) / 1_000_000L; + if (left <= 0) { + record("error no reply to /" + command); + return; + } + replyLock.wait(left); + } + } + } + + void record(String line) { + synchronized (lines) { + lines.add("step=" + step + " " + line); + } + } + + void still() throws Exception { + PositionMoveRotation at = position; + // A tick of its own: the server takes one position per client tick (receivedPositionThisTick). + send(net.minecraft.network.protocol.game.ServerboundClientTickEndPacket.INSTANCE); + send(new ServerboundMovePlayerPacket.PosRot(at.position(), at.yRot(), at.xRot(), false, false)); + send(net.minecraft.network.protocol.game.ServerboundClientTickEndPacket.INSTANCE); + } + + void close() throws Exception { + if (socket != null) socket.close(); + } + + void join(String host, int port) throws Exception { + socket = new Socket(); + socket.connect(new InetSocketAddress(host, port), 10_000); + socket.setTcpNoDelay(true); + in = new DataInputStream(socket.getInputStream()); + out = socket.getOutputStream(); + outbound = HandshakeProtocols.SERVERBOUND; + send(new ClientIntentionPacket(SharedConstants.getProtocolVersion(), host, port, ClientIntent.LOGIN)); + outbound = LoginProtocols.SERVERBOUND; + inbound = LoginProtocols.CLIENTBOUND; + send(new ServerboundHelloPacket(name, UUIDUtil.createOfflinePlayerUUID(name))); + while (true) { + Packet<?> p = read().packet; + if (p instanceof ClientboundLoginCompressionPacket c) { + threshold = c.getCompressionThreshold(); + } else if (p instanceof ClientboundLoginFinishedPacket) { + send(ServerboundLoginAcknowledgedPacket.INSTANCE); + break; + } else if (p instanceof ClientboundLoginDisconnectPacket d) { + throw new IllegalStateException("login refused: " + d.reason().getString()); + } + } + outbound = ConfigurationProtocols.SERVERBOUND; + inbound = ConfigurationProtocols.CLIENTBOUND; + send(new ServerboundCustomPayloadPacket(new BrandPayload("vanilla"))); + send(new ServerboundClientInformationPacket(ClientInformation.createDefault())); + while (true) { + Packet<?> p = read().packet; + if (p instanceof ClientboundSelectKnownPacks k) { + send(new ServerboundSelectKnownPacks(k.knownPacks())); + } else if (p instanceof ClientboundKeepAlivePacket k) { + send(new ServerboundKeepAlivePacket(k.getId())); + } else if (p instanceof ClientboundPingPacket k) { + send(new ServerboundPongPacket(k.getId())); + } else if (p instanceof ClientboundFinishConfigurationPacket) { + send(ServerboundFinishConfigurationPacket.INSTANCE); + break; + } else if (p instanceof ClientboundDisconnectPacket d) { + throw new IllegalStateException("disconnected: " + d.reason().getString()); + } + } + GameProtocols.Context context = new GameProtocols.Context() { + @Override + public boolean hasInfiniteMaterials() { + return true; + } + + @Override + public boolean canUseCommandBlocks() { + return true; + } + }; + outbound = GameProtocols.SERVERBOUND_TEMPLATE.bind(RegistryFriendlyByteBuf.decorator(ParityData.access), context); + inbound = GameProtocols.CLIENTBOUND_TEMPLATE.bind(RegistryFriendlyByteBuf.decorator(ParityData.access)); + Thread reader = new Thread(this::play, name); + reader.setDaemon(true); + reader.start(); + } + + private void play() { + try { + while (true) { + Received r = read(); + Packet<?> p = r.packet; + if (p instanceof net.minecraft.network.protocol.game.ClientboundLoginPacket l) { + id = l.playerId(); + } + if (state) recordState(p); + if (p instanceof net.minecraft.network.protocol.game.ClientboundAddEntityPacket a + && a.getType() == net.minecraft.world.entity.EntityTypes.PLAYER) { + String who = NAMES.getOrDefault(a.getUUID(), "uuid?"); + seen.put(a.getId(), who); + if (players) record("add " + who + " at " + a.getX() + "," + a.getY() + "," + a.getZ()); + } else if (p instanceof net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket rm) { + rm.entityIds().forEach(id -> { + String who = seen.remove(id); + if (who != null && players) record("remove " + who); + }); + } else if (p instanceof net.minecraft.network.protocol.game.ClientboundRespawnPacket rs) { + if (players) record("respawn " + rs.commonPlayerSpawnInfo().dimension().identifier()); + seen.clear(); + } else if (p instanceof ClientboundTrackedWaypointPacket) { + record(waypoint(r.payload)); + } else if (p instanceof ClientboundSystemChatPacket c) { + if (chat) { + record("chat " + (c.overlay() ? "overlay " : "") + net.minecraft.network.chat.ComponentSerialization.CODEC + .encodeStart(ParityData.access.createSerializationContext(com.mojang.serialization.JsonOps.INSTANCE), c.content()) + .getOrThrow()); + } + synchronized (replyLock) { + replies++; + replyLock.notifyAll(); + } + } else if (p instanceof ClientboundKeepAlivePacket k) { + send(new ServerboundKeepAlivePacket(k.getId())); + } else if (p instanceof ClientboundPingPacket k) { + send(new ServerboundPongPacket(k.getId())); + } else if (p instanceof ClientboundPlayerPositionPacket t) { + position = PositionMoveRotation.calculateAbsolute(position, t.change(), t.relatives()); + Vec3 at = position.position(); + if (state) { + record(String.format(java.util.Locale.ROOT, "teleport %.3f %.3f %.3f %.1f %.1f", + at.x, at.y, at.z, position.yRot(), position.xRot())); + } + send(new ServerboundAcceptTeleportationPacket(t.id(), at.x, at.y, at.z, position.yRot(), position.xRot())); + // Only the spawn teleport is answered with a position too (VanillaProtocolClient). + if (!loaded) { + send(new ServerboundMovePlayerPacket.PosRot(at, position.yRot(), position.xRot(), false, false)); + send(new ServerboundPlayerLoadedPacket()); + loaded = true; + } + } else if (p instanceof ClientboundChunkBatchFinishedPacket) { + send(new ServerboundChunkBatchReceivedPacket(64.0F)); + } else if (p instanceof ClientboundDisconnectPacket d) { + record("disconnected " + d.reason().getString()); + return; + } + } + } catch (Exception e) { + if (!socket.isClosed()) record("error play " + e); + } + } + + /** What the server changed about this player ({@code state on}). */ + private void recordState(Packet<?> p) { + if (p instanceof net.minecraft.network.protocol.game.ClientboundGameEventPacket g + && g.getEvent() == net.minecraft.network.protocol.game.ClientboundGameEventPacket.CHANGE_GAME_MODE) { + record("gamemode " + (int) g.getParam()); + } else if (p instanceof net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket c) { + record("slot " + c.getContainerId() + " " + c.getSlot() + " " + item(c.getItem())); + } else if (p instanceof net.minecraft.network.protocol.game.ClientboundSetPlayerInventoryPacket i) { + record("inventory " + i.slot() + " " + item(i.contents())); + } else if (p instanceof net.minecraft.network.protocol.game.ClientboundUpdateMobEffectPacket e && e.getEntityId() == id) { + record("effect " + e.getEffect().getRegisteredName() + " " + e.getEffectAmplifier() + " " + e.getEffectDurationTicks()); + } else if (p instanceof net.minecraft.network.protocol.game.ClientboundRemoveMobEffectPacket e && e.entityId() == id) { + record("effect-removed " + e.effect().getRegisteredName()); + } else if (p instanceof net.minecraft.network.protocol.game.ClientboundPlayerCombatKillPacket k) { + record("killed " + net.minecraft.network.chat.ComponentSerialization.CODEC + .encodeStart(ParityData.access.createSerializationContext(com.mojang.serialization.JsonOps.INSTANCE), k.message()) + .getOrThrow()); + } + } + + private static String item(net.minecraft.world.item.ItemStack stack) { + return stack.isEmpty() ? "empty" + : net.minecraft.core.registries.BuiltInRegistries.ITEM.getKey(stack.getItem()) + " x" + stack.getCount(); + } + + private record Received(Packet<?> packet, byte[] payload) {} + + private Received read() throws Exception { + byte[] payload = readFrame(); + ByteBuf buf = Unpooled.wrappedBuffer(payload); + Packet<?> packet = inbound.codec().decode(buf); + if (buf.readableBytes() > 0) { + throw new IllegalStateException(packet.type().id() + " was larger than expected: " + buf.readableBytes() + " bytes left"); + } + return new Received(packet, payload); + } + + synchronized void send(Packet<?> packet) throws Exception { + ByteBuf body = Unpooled.buffer(); + ((ProtocolInfo) outbound).codec().encode(body, packet); + byte[] raw = new byte[body.readableBytes()]; + body.readBytes(raw); + ByteBuf frame = Unpooled.buffer(); + if (threshold >= 0) { + ByteBuf inner = Unpooled.buffer(); + if (raw.length >= threshold) { + writeVarInt(inner, raw.length); + Deflater d = new Deflater(); + d.setInput(raw); + d.finish(); + byte[] tmp = new byte[raw.length + 64]; + while (!d.finished()) inner.writeBytes(tmp, 0, d.deflate(tmp)); + d.end(); + } else { + writeVarInt(inner, 0); + inner.writeBytes(raw); + } + writeVarInt(frame, inner.readableBytes()); + frame.writeBytes(inner); + } else { + writeVarInt(frame, raw.length); + frame.writeBytes(raw); + } + byte[] bytes = new byte[frame.readableBytes()]; + frame.readBytes(bytes); + out.write(bytes); + out.flush(); + } + + private byte[] readFrame() throws Exception { + byte[] frame = new byte[readVarInt(in)]; + in.readFully(frame); + if (threshold < 0) return frame; + ByteArrayInputStream header = new ByteArrayInputStream(frame); + int dataLength = readVarInt(header); + int skip = frame.length - header.available(); + if (dataLength == 0) return java.util.Arrays.copyOfRange(frame, skip, frame.length); + Inflater inflater = new Inflater(); + inflater.setInput(frame, skip, frame.length - skip); + byte[] raw = new byte[dataLength]; + int n = inflater.inflate(raw); + inflater.end(); + if (n != dataLength) throw new IllegalStateException("decompressed " + n + " of " + dataLength + " bytes"); + return raw; + } + } + + private static int readVarInt(InputStream in) throws Exception { + int value = 0; + for (int shift = 0; shift < 35; shift += 7) { + int b = in.read(); + if (b < 0) throw new java.io.EOFException(); + value |= (b & 0x7F) << shift; + if ((b & 0x80) == 0) return value; + } + throw new IllegalStateException("VarInt too big"); + } + + private static void writeVarInt(ByteBuf buf, int value) { + while ((value & ~0x7F) != 0) { + buf.writeByte((value & 0x7F) | 0x80); + value >>>= 7; + } + buf.writeByte(value); + } +} diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/SecureChatClient.java b/mods/horizon/src/test/java/dev/horizonlab/parity/SecureChatClient.java new file mode 100644 index 000000000..4d08dafd2 --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/SecureChatClient.java @@ -0,0 +1,625 @@ +package dev.horizonlab.parity; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.mojang.serialization.JsonOps; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.EOFException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.math.BigInteger; +import java.net.HttpURLConnection; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.URI; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.KeyFactory; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.PrivateKey; +import java.security.spec.PKCS8EncodedKeySpec; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Base64; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.zip.Deflater; +import java.util.zip.Inflater; +import javax.crypto.Cipher; +import javax.crypto.CipherInputStream; +import javax.crypto.CipherOutputStream; +import javax.crypto.SecretKey; +import net.minecraft.SharedConstants; +import net.minecraft.commands.arguments.ArgumentSignatures; +import net.minecraft.core.UUIDUtil; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.ProtocolInfo; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.ComponentSerialization; +import net.minecraft.network.chat.LastSeenMessages; +import net.minecraft.network.chat.LastSeenMessagesTracker; +import net.minecraft.network.chat.LocalChatSession; +import net.minecraft.network.chat.MessageSignature; +import net.minecraft.network.chat.MessageSignatureCache; +import net.minecraft.network.chat.PlayerChatMessage; +import net.minecraft.network.chat.RemoteChatSession; +import net.minecraft.network.chat.SignedMessageBody; +import net.minecraft.network.chat.SignedMessageChain; +import net.minecraft.network.chat.SignedMessageLink; +import net.minecraft.network.chat.SignedMessageValidator; +import net.minecraft.network.protocol.Packet; +import net.minecraft.network.protocol.common.ClientboundDisconnectPacket; +import net.minecraft.network.protocol.common.ClientboundKeepAlivePacket; +import net.minecraft.network.protocol.common.ClientboundPingPacket; +import net.minecraft.network.protocol.common.ServerboundClientInformationPacket; +import net.minecraft.network.protocol.common.ServerboundKeepAlivePacket; +import net.minecraft.network.protocol.common.ServerboundPongPacket; +import net.minecraft.network.protocol.configuration.ClientboundFinishConfigurationPacket; +import net.minecraft.network.protocol.configuration.ClientboundSelectKnownPacks; +import net.minecraft.network.protocol.configuration.ConfigurationProtocols; +import net.minecraft.network.protocol.configuration.ServerboundFinishConfigurationPacket; +import net.minecraft.network.protocol.configuration.ServerboundSelectKnownPacks; +import net.minecraft.network.protocol.game.ClientboundChunkBatchFinishedPacket; +import net.minecraft.network.protocol.game.ClientboundDisguisedChatPacket; +import net.minecraft.network.protocol.game.ClientboundLoginPacket; +import net.minecraft.network.protocol.game.ClientboundPlayerChatPacket; +import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket; +import net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket; +import net.minecraft.network.protocol.game.ClientboundSystemChatPacket; +import net.minecraft.network.protocol.game.GameProtocols; +import net.minecraft.network.protocol.game.ServerboundAcceptTeleportationPacket; +import net.minecraft.network.protocol.game.ServerboundChatAckPacket; +import net.minecraft.network.protocol.game.ServerboundChatCommandPacket; +import net.minecraft.network.protocol.game.ServerboundChatCommandSignedPacket; +import net.minecraft.network.protocol.game.ServerboundChatPacket; +import net.minecraft.network.protocol.game.ServerboundChatSessionUpdatePacket; +import net.minecraft.network.protocol.game.ServerboundChunkBatchReceivedPacket; +import net.minecraft.network.protocol.game.ServerboundPlayerLoadedPacket; +import net.minecraft.network.protocol.handshake.ClientIntent; +import net.minecraft.network.protocol.handshake.ClientIntentionPacket; +import net.minecraft.network.protocol.handshake.HandshakeProtocols; +import net.minecraft.network.protocol.login.ClientboundHelloPacket; +import net.minecraft.network.protocol.login.ClientboundLoginCompressionPacket; +import net.minecraft.network.protocol.login.ClientboundLoginDisconnectPacket; +import net.minecraft.network.protocol.login.ClientboundLoginFinishedPacket; +import net.minecraft.network.protocol.login.LoginProtocols; +import net.minecraft.network.protocol.login.ServerboundHelloPacket; +import net.minecraft.network.protocol.login.ServerboundKeyPacket; +import net.minecraft.network.protocol.login.ServerboundLoginAcknowledgedPacket; +import net.minecraft.server.level.ClientInformation; +import net.minecraft.util.Crypt; +import net.minecraft.util.Signer; +import net.minecraft.world.entity.player.ProfileKeyPair; +import net.minecraft.world.entity.player.ProfilePublicKey; + +/** + * Secure chat seen by vanilla-codec clients: several players on one server, + * signing their chat with profile keys that a mock of the Minecraft services + * signed (the harness serves its public key as the services' key set). + * + * <p>{@code keys <dir>} writes the mock services' key pair: the private key + * (PKCS#8) and {@code publickeys.json} as the services answer it. With a + * host, a port, the private key, an output file and a scenario, it plays the + * scenario. Each client signs and acknowledges exactly as + * ClientPacketListener does (LocalChatSession, SignedMessageChain's encoder, + * LastSeenMessagesTracker, MessageSignatureCache), and checks every player + * chat it receives with SignedMessageValidator against the sender's session + * from the player list. The transcript holds what each player received and + * whether its signature checked out; keys, salts, times and signatures + * differ between runs and are left out. + */ +public final class SecureChatClient { + private static final List<String> LINES = new ArrayList<>(); + private static PrivateKey services; + private static String sessionUrl; + + public static void main(String[] args) throws Exception { + ParityData.init(); + if (args[0].equals("keys")) { + keys(Path.of(args[1])); + return; + } + String host = args[0]; + int port = Integer.parseInt(args[1]); + services = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(Files.readAllBytes(Path.of(args[2])))); + Path output = Path.of(args[3]); + String scenario = args[4]; + sessionUrl = args.length > 5 ? args[5] : null; + try { + if (scenario.equals("offline")) offline(host, port); + else online(host, port); + } catch (Exception e) { + StringBuilder line = new StringBuilder("error " + e); + for (Throwable c = e.getCause(); c != null; c = c.getCause()) line.append(" / caused by ").append(c); + LINES.add(line.toString()); + } + try (PrintWriter w = new PrintWriter(Files.newBufferedWriter(output, StandardCharsets.UTF_8))) { + for (String line : LINES) w.println(line); + } + System.exit(0); + } + + private static void keys(Path dir) throws Exception { + KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); + generator.initialize(2048); + KeyPair pair = generator.generateKeyPair(); + Files.write(dir.resolve("services-private.der"), pair.getPrivate().getEncoded()); + JsonObject key = new JsonObject(); + key.addProperty("publicKey", Base64.getEncoder().encodeToString(pair.getPublic().getEncoded())); + JsonArray list = new JsonArray(); + list.add(key); + JsonObject set = new JsonObject(); + set.add("profilePropertyKeys", list); + set.add("playerCertificateKeys", list); + Files.writeString(dir.resolve("publickeys.json"), set.toString()); + } + + // ---- scenarios ---------------------------------------------------- + + /** Offline mode: the server checks chat sessions but enforces nothing. */ + private static void offline(String host, int port) throws Exception { + Client alice = Client.join(host, port, "Alice", true, null); + Client bob = Client.join(host, port, "Bob", true, null); + Client carol = Client.join(host, port, "Carol", false, null); + List<Client> all = List.of(alice, bob, carol); + settle(all, "joined"); + alice.chat("hello, signed"); + settle(all, "alice chats"); + bob.chat("seen it"); + settle(all, "bob chats with alice's message seen"); + carol.chat("no key here"); + settle(all, "carol chats unsigned"); + alice.signedCommand("say signed announcement", "message", "signed announcement"); + settle(all, "alice says"); + alice.signedCommand("me waves", "action", "waves"); + settle(all, "alice emotes"); + alice.signedCommand("msg Bob just between us", "message", "just between us"); + settle(all, "alice whispers to bob"); + carol.signedCommand("me shrugs", null, null); + settle(all, "carol emotes, unsigned arguments"); + carol.command("say plain announcement"); + settle(all, "carol says through the unsigned packet"); + bob.signedCommand("say mismatch", "nothing", "mismatch"); + settle(all, "bob signs the wrong argument"); + bob.chat("after the mismatch"); + settle(all, "bob's chain after the mismatch"); + alice.tamperedChat("forged"); + settle(all, "alice's signature fails"); + alice.chat("after the forgery"); + settle(all, "alice's chain is broken"); + bob.send(new ServerboundChatAckPacket(50)); + settle(all, "bob acknowledges too much"); + for (Client c : all) c.close(); + } + + /** Online mode with enforce-secure-profile: unsigned chat is refused. */ + private static void online(String host, int port) throws Exception { + Client alice = Client.join(host, port, "Alice", true, null); + Client carol = Client.join(host, port, "Carol", false, null); + List<Client> all = List.of(alice, carol); + settle(all, "joined"); + alice.chat("signed and enforced"); + settle(all, "alice chats"); + carol.chat("unsigned under enforcement"); + settle(all, "carol chats without a key"); + carol.command("me tries the unsigned packet"); + settle(all, "carol's unsigned command"); + carol.signedCommand("me tries without signatures", null, null); + settle(all, "carol's command without signatures"); + // A key the services signed for another player. + Client mallory = Client.join(host, port, "Mallory", true, UUID.nameUUIDFromBytes("someone else".getBytes())); + settle(List.of(alice, carol, mallory), "mallory's borrowed key"); + for (Client c : all) c.close(); + mallory.close(); + } + + /** Lets the server answer, then records what each player received, in player order. */ + private static void settle(List<Client> clients, String step) throws Exception { + Thread.sleep(1500); + LINES.add("step " + step); + for (Client c : clients) c.drain(); + } + + private static String json(Component c) { + return ComponentSerialization.CODEC.encodeStart(ParityData.access.createSerializationContext(JsonOps.INSTANCE), c).getOrThrow().toString(); + } + + /** A player: one connection, its chat session and what its client tracks. */ + private static final class Client { + final String name; + UUID id; + Socket socket; + DataInputStream in; + OutputStream out; + int threshold = -1; + ProtocolInfo<?> inbound; + ProtocolInfo<?> outbound; + final BlockingQueue<Packet<?>> queue = new LinkedBlockingQueue<>(); + volatile boolean closed; + LocalChatSession session; + SignedMessageChain.Encoder encoder = SignedMessageChain.Encoder.UNSIGNED; + final LastSeenMessagesTracker lastSeen = new LastSeenMessagesTracker(20); + final MessageSignatureCache cache = MessageSignatureCache.createDefault(); + int nextChatIndex; + final Map<UUID, String> names = new LinkedHashMap<>(); + final Map<UUID, SignedMessageValidator> validators = new HashMap<>(); + boolean enforced; + boolean loaded; + net.minecraft.world.entity.PositionMoveRotation position = new net.minecraft.world.entity.PositionMoveRotation( + net.minecraft.world.phys.Vec3.ZERO, net.minecraft.world.phys.Vec3.ZERO, 0, 0); + + Client(String name) { + this.name = name; + } + + void record(String line) { + LINES.add(name + " " + line); + } + + static Client join(String host, int port, String name, boolean signs, UUID keyOwner) throws Exception { + Client c = new Client(name); + c.connect(host, port, signs, keyOwner); + return c; + } + + void connect(String host, int port, boolean signs, UUID keyOwner) throws Exception { + socket = new Socket(); + socket.connect(new InetSocketAddress(host, port), 10_000); + socket.setSoTimeout(30_000); + socket.setTcpNoDelay(true); + in = new DataInputStream(socket.getInputStream()); + out = socket.getOutputStream(); + outbound = HandshakeProtocols.SERVERBOUND; + send(new ClientIntentionPacket(SharedConstants.getProtocolVersion(), host, port, ClientIntent.LOGIN)); + outbound = LoginProtocols.SERVERBOUND; + inbound = LoginProtocols.CLIENTBOUND; + send(new ServerboundHelloPacket(name, UUIDUtil.createOfflinePlayerUUID(name))); + while (true) { + Packet<?> p = read(); + if (p instanceof ClientboundHelloPacket hello) { + // ClientHandshakePacketListenerImpl.handleHello: join through the session service. + SecretKey secret = Crypt.generateSecretKey(); + String hash = new BigInteger(Crypt.digestData(hello.getServerId(), hello.getPublicKey(), secret)).toString(16); + joinSession(hash); + send(new ServerboundKeyPacket(secret, hello.getPublicKey(), hello.getChallenge())); + in = new DataInputStream(new CipherInputStream(socket.getInputStream(), Crypt.getCipher(Cipher.DECRYPT_MODE, secret))); + out = new CipherOutputStream(socket.getOutputStream(), Crypt.getCipher(Cipher.ENCRYPT_MODE, secret)); + } else if (p instanceof ClientboundLoginCompressionPacket compression) { + threshold = compression.getCompressionThreshold(); + } else if (p instanceof ClientboundLoginFinishedPacket finished) { + id = finished.gameProfile().id(); + record("logged in"); + send(ServerboundLoginAcknowledgedPacket.INSTANCE); + break; + } else if (p instanceof ClientboundLoginDisconnectPacket d) { + record("refused " + json(d.reason())); + closed = true; + return; + } + } + outbound = ConfigurationProtocols.SERVERBOUND; + inbound = ConfigurationProtocols.CLIENTBOUND; + send(new ServerboundClientInformationPacket(ClientInformation.createDefault())); + while (true) { + Packet<?> p = read(); + if (p instanceof ClientboundSelectKnownPacks packs) { + send(new ServerboundSelectKnownPacks(packs.knownPacks())); + } else if (p instanceof ClientboundKeepAlivePacket k) { + send(new ServerboundKeepAlivePacket(k.getId())); + } else if (p instanceof ClientboundPingPacket ping) { + send(new ServerboundPongPacket(ping.getId())); + } else if (p instanceof ClientboundFinishConfigurationPacket) { + send(ServerboundFinishConfigurationPacket.INSTANCE); + break; + } else if (p instanceof ClientboundDisconnectPacket d) { + record("disconnected " + json(d.reason())); + closed = true; + return; + } + } + GameProtocols.Context context = new GameProtocols.Context() { + @Override + public boolean hasInfiniteMaterials() { + return false; + } + + @Override + public boolean canUseCommandBlocks() { + return false; + } + }; + outbound = GameProtocols.SERVERBOUND_TEMPLATE.bind(RegistryFriendlyByteBuf.decorator(ParityData.access), context); + inbound = GameProtocols.CLIENTBOUND_TEMPLATE.bind(RegistryFriendlyByteBuf.decorator(ParityData.access)); + // ClientPacketListener.handleLogin: the session goes out once the + // login packet is in (prepareKeyPair, setKeyPair). + while (true) { + Packet<?> p = read(); + if (p instanceof ClientboundLoginPacket login) { + enforced = login.enforcesSecureChat(); + record("login online " + login.onlineMode() + " enforces " + login.enforcesSecureChat()); + if (signs) startSession(keyOwner == null ? id : keyOwner); + break; + } + } + Thread reader = new Thread(() -> { + try { + while (!closed) { + Packet<?> p = read(); + queue.put(p); + // Nothing follows a disconnect but the closed socket. + if (p instanceof ClientboundDisconnectPacket) return; + } + } catch (Exception e) { + if (!closed) queue.add(new ClientboundDisconnectPacket(Component.literal("connection lost: " + e))); + } + }, name + " reader"); + reader.setDaemon(true); + reader.start(); + } + + /** The session service's join, as the launcher's account would. */ + void joinSession(String serverHash) throws Exception { + HttpURLConnection http = (HttpURLConnection) URI.create(sessionUrl + "/session/minecraft/join").toURL().openConnection(); + http.setRequestMethod("POST"); + http.setDoOutput(true); + http.setRequestProperty("Content-Type", "application/json"); + JsonObject body = new JsonObject(); + body.addProperty("accessToken", "mock-token"); + body.addProperty("selectedProfile", name); + body.addProperty("serverId", serverHash); + http.getOutputStream().write(body.toString().getBytes(StandardCharsets.UTF_8)); + http.getResponseCode(); + http.disconnect(); + } + + /** A profile key pair the mock services signed for {@code owner}, and its session. */ + void startSession(UUID owner) throws Exception { + KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); + generator.initialize(2048); + KeyPair pair = generator.generateKeyPair(); + Instant expires = Instant.now().plusSeconds(3600).truncatedTo(java.time.temporal.ChronoUnit.MILLIS); + byte[] key = pair.getPublic().getEncoded(); + byte[] payload = ByteBuffer.allocate(24 + key.length).putLong(owner.getMostSignificantBits()) + .putLong(owner.getLeastSignificantBits()).putLong(expires.toEpochMilli()).put(key).array(); + byte[] signature = Signer.from(services, "SHA1withRSA").sign(payload); + ProfilePublicKey publicKey = new ProfilePublicKey(new ProfilePublicKey.Data(expires, pair.getPublic(), signature)); + session = LocalChatSession.create(new ProfileKeyPair(pair.getPrivate(), publicKey, expires)); + encoder = session.createMessageEncoder(id); + send(new ServerboundChatSessionUpdatePacket(session.asRemote().asData())); + } + + /** ClientPacketListener.sendChat. */ + void chat(String content) throws Exception { + Instant now = Instant.now(); + long salt = Crypt.SaltSupplier.getLong(); + LastSeenMessagesTracker.Update update = lastSeen.generateAndApplyUpdate(); + MessageSignature signature = encoder.pack(new SignedMessageBody(content, now, salt, update.lastSeen())); + send(new ServerboundChatPacket(content, now, salt, Optional.ofNullable(signature), update.update())); + } + + /** A chat whose signature doesn't match its text. */ + void tamperedChat(String content) throws Exception { + Instant now = Instant.now(); + long salt = Crypt.SaltSupplier.getLong(); + LastSeenMessagesTracker.Update update = lastSeen.generateAndApplyUpdate(); + MessageSignature signature = encoder.pack(new SignedMessageBody(content + "!", now, salt, update.lastSeen())); + send(new ServerboundChatPacket(content, now, salt, Optional.ofNullable(signature), update.update())); + } + + /** ClientPacketListener.sendCommand for a command with one message argument (none: no signatures). */ + void signedCommand(String command, String argument, String value) throws Exception { + Instant now = Instant.now(); + long salt = Crypt.SaltSupplier.getLong(); + LastSeenMessagesTracker.Update update = lastSeen.generateAndApplyUpdate(); + List<ArgumentSignatures.Entry> entries = new ArrayList<>(); + if (argument != null) { + MessageSignature signature = encoder.pack(new SignedMessageBody(value, now, salt, update.lastSeen())); + if (signature != null) entries.add(new ArgumentSignatures.Entry(argument, signature)); + } + send(new ServerboundChatCommandSignedPacket(command, now, salt, new ArgumentSignatures(entries), update.update())); + } + + void command(String command) throws Exception { + send(new ServerboundChatCommandPacket(command)); + } + + /** Handles and records what arrived. */ + void drain() throws Exception { + Packet<?> p; + while ((p = queue.poll(50, TimeUnit.MILLISECONDS)) != null) handle(p); + } + + void handle(Packet<?> packet) throws Exception { + if (packet instanceof ClientboundKeepAlivePacket k) { + send(new ServerboundKeepAlivePacket(k.getId())); + } else if (packet instanceof ClientboundPingPacket ping) { + send(new ServerboundPongPacket(ping.getId())); + } else if (packet instanceof ClientboundPlayerPositionPacket pos) { + position = net.minecraft.world.entity.PositionMoveRotation.calculateAbsolute(position, pos.change(), pos.relatives()); + send(new ServerboundAcceptTeleportationPacket(pos.id(), position.position().x, position.position().y, + position.position().z, position.yRot(), position.xRot())); + if (!loaded) { + send(new ServerboundPlayerLoadedPacket()); + loaded = true; + } + } else if (packet instanceof ClientboundChunkBatchFinishedPacket) { + send(new ServerboundChunkBatchReceivedPacket(64.0F)); + } else if (packet instanceof ClientboundPlayerInfoUpdatePacket info) { + for (ClientboundPlayerInfoUpdatePacket.Entry e : info.entries()) { + if (info.actions().contains(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER)) { + names.put(e.profileId(), e.profile().name()); + } + if (info.actions().contains(ClientboundPlayerInfoUpdatePacket.Action.INITIALIZE_CHAT)) { + RemoteChatSession.Data data = e.chatSession(); + String who = names.getOrDefault(e.profileId(), "?"); + if (data != null) { + RemoteChatSession remote = new RemoteChatSession(data.sessionId(), new ProfilePublicKey(data.profilePublicKey())); + validators.put(e.profileId(), remote.createMessageValidator(ProfilePublicKey.EXPIRY_GRACE_PERIOD)); + sessions.put(e.profileId(), data.sessionId()); + record("session of " + who); + } else if (!info.actions().contains(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER)) { + record("session cleared for " + who); + } + } + } + } else if (packet instanceof ClientboundPlayerChatPacket chat) { + playerChat(chat); + } else if (packet instanceof ClientboundDisguisedChatPacket d) { + record("disguised " + d.chatType().chatType().getRegisteredName() + " " + json(d.chatType().name()) + " " + + d.chatType().targetName().map(SecureChatClient::json) + " " + json(d.message())); + } else if (packet instanceof ClientboundSystemChatPacket s) { + record("system " + json(s.content())); + } else if (packet instanceof ClientboundDisconnectPacket d) { + record("disconnected " + json(d.reason())); + closed = true; + } + } + + /** ClientPacketListener.handlePlayerChat, then markMessageAsProcessed. */ + void playerChat(ClientboundPlayerChatPacket p) { + int expected = nextChatIndex++; + String sender = names.getOrDefault(p.sender(), p.sender().toString()); + StringBuilder lastSeenPacked = new StringBuilder(); + for (MessageSignature.Packed packed : p.body().lastSeen().entries()) { + lastSeenPacked.append(packed.fullSignature() != null ? "full" : "cached:" + packed.id()).append(','); + } + Optional<SignedMessageBody> body = p.body().unpack(cache); + if (p.globalIndex() != expected || body.isEmpty()) { + record("chat out of order or unknown signature: index " + p.globalIndex() + " expected " + expected); + return; + } + MessageSignature signature = p.signature().orElse(null); + cache.push(body.get(), signature); + SignedMessageValidator validator = validators.get(p.sender()); + String verdict; + if (validator != null) { + PlayerChatMessage message = new PlayerChatMessage( + new SignedMessageLink(p.index(), p.sender(), sessionId(p.sender())), signature, body.get(), + p.unsignedContent().orElse(null), p.filterMask()); + verdict = validator.updateAndValidate(message) != null ? "verified" : "rejected"; + } else { + verdict = signature == null ? (enforced ? "unsigned-rejected" : "unsigned") : "no-session"; + } + record("chat #" + p.globalIndex() + " from " + sender + " link " + p.index() + " " + verdict + " '" + body.get().content() + + "' seen [" + lastSeenPacked + "] " + body.get().lastSeen().entries().size() + " unsigned " + + p.unsignedContent().map(SecureChatClient::json) + " " + p.chatType().chatType().getRegisteredName() + " " + + json(p.chatType().name()) + " " + p.chatType().targetName().map(SecureChatClient::json)); + if (signature != null) lastSeen.addPending(signature, true); + } + + private final Map<UUID, UUID> sessions = new HashMap<>(); + + UUID sessionId(UUID sender) { + return sessions.getOrDefault(sender, new UUID(0, 0)); + } + + // ---- framing ------------------------------------------------------ + + synchronized void send(Packet<?> packet) throws Exception { + ByteBuf body = Unpooled.buffer(); + encode(body, packet); + byte[] raw = new byte[body.readableBytes()]; + body.readBytes(raw); + ByteBuf frame = Unpooled.buffer(); + if (threshold >= 0) { + ByteBuf inner = Unpooled.buffer(); + if (raw.length >= threshold) { + writeVarInt(inner, raw.length); + Deflater d = new Deflater(); + d.setInput(raw); + d.finish(); + byte[] tmp = new byte[raw.length + 64]; + while (!d.finished()) inner.writeBytes(tmp, 0, d.deflate(tmp)); + d.end(); + } else { + writeVarInt(inner, 0); + inner.writeBytes(raw); + } + writeVarInt(frame, inner.readableBytes()); + frame.writeBytes(inner); + } else { + writeVarInt(frame, raw.length); + frame.writeBytes(raw); + } + byte[] bytes = new byte[frame.readableBytes()]; + frame.readBytes(bytes); + // A server that disconnected the player may close before this + // write: what it sent last is the disconnect, which the reader + // records, not the broken pipe. + if (broken) return; + try { + out.write(bytes); + out.flush(); + } catch (java.io.IOException e) { + broken = true; + } + } + + private boolean broken; + + @SuppressWarnings({"unchecked", "rawtypes"}) + private void encode(ByteBuf body, Packet<?> packet) { + ((ProtocolInfo) outbound).codec().encode(body, packet); + } + + Packet<?> read() throws Exception { + int length = readVarInt(in); + byte[] frame = new byte[length]; + in.readFully(frame); + byte[] raw = frame; + if (threshold >= 0) { + DataInputStream f = new DataInputStream(new ByteArrayInputStream(frame)); + int dataLength = readVarInt(f); + byte[] rest = f.readAllBytes(); + if (dataLength == 0) { + raw = rest; + } else { + Inflater inflater = new Inflater(); + inflater.setInput(rest); + raw = new byte[dataLength]; + inflater.inflate(raw); + inflater.end(); + } + } + return inbound.codec().decode(Unpooled.wrappedBuffer(raw)); + } + + void close() throws Exception { + closed = true; + socket.close(); + } + } + + private static int readVarInt(InputStream in) throws Exception { + int value = 0; + for (int i = 0; i < 5; i++) { + int b = in.read(); + if (b < 0) throw new EOFException(); + value |= (b & 0x7F) << (7 * i); + if ((b & 0x80) == 0) return value; + } + throw new IllegalStateException("VarInt too big"); + } + + private static void writeVarInt(ByteBuf buf, int value) { + while ((value & ~0x7F) != 0) { + buf.writeByte((value & 0x7F) | 0x80); + value >>>= 7; + } + buf.writeByte(value); + } +} diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockEntityTickerOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockEntityTickerOracle.java new file mode 100644 index 000000000..75330c4fe --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaBlockEntityTickerOracle.java @@ -0,0 +1,217 @@ +package dev.horizonlab.parity; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import net.minecraft.core.BlockPos; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.resources.Identifier; +import net.minecraft.util.debug.LevelDebugSynchronizers; +import net.minecraft.world.TickRateManager; +import net.minecraft.world.level.ChunkPos; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.EntityBlock; +import net.minecraft.world.level.block.JukeboxBlock; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.entity.TickingBlockEntity; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.chunk.ChunkAccess; +import net.minecraft.world.level.chunk.LevelChunk; + +/** + * The order of vanilla's `Level.blockEntityTickers`: real `LevelChunk`s of a + * server level register, rebind and drop the tickers of real block entities + * (`addAndRegisterBlockEntity`, `setBlockEntity`, `removeBlockEntity`, + * `updateBlockEntityTicker` after a state change, `registerAllBlockEntitiesAfterLevelLoad` + * after a chunk load, `clearAllBlockEntities` when it unloads), and + * `Level.tickBlockEntities` runs with the tick rate frozen, so it only + * promotes pending tickers and drops removed ones. After each `tick` the + * positions of the live tickers are printed in list order. + * + * Input lines: `reset`, `load CX CZ BLOCK X Y Z ...` (a chunk's block entities in + * its save order), `place BLOCK X Y Z`, `replace X Y Z`, `remove X Y Z`, + * `record X Y Z 0|1` (a jukebox's has_record), `unload CX CZ`, `tick`. + */ +public final class VanillaBlockEntityTickerOracle { + static Object unsafe; + static Method allocate; + + static <T> T alloc(Class<T> c) throws Exception { + return c.cast(allocate.invoke(unsafe, c)); + } + + static void set(Class<?> owner, Object target, String name, Object value) throws Exception { + Field f = owner.getDeclaredField(name); + f.setAccessible(true); + f.set(target, value); + } + + static Object get(Class<?> owner, Object target, String name) throws Exception { + Field f = owner.getDeclaredField(name); + f.setAccessible(true); + return f.get(target); + } + + static Map<BlockPos, BlockState> cells = new HashMap<>(); + static Map<Long, Chunk> chunks = new HashMap<>(); + static World world; + + static final class World extends net.minecraft.server.level.ServerLevel { + TickRateManager rate; + LevelDebugSynchronizers debug; + World() { + super(null, null, null, null, null, null, false, 0L, List.of(), false); + } + @Override public boolean isClientSide() { return false; } + @Override public TickRateManager tickRateManager() { return rate; } + @Override public LevelDebugSynchronizers debugSynchronizers() { return debug; } + @Override public void onBlockEntityAdded(BlockEntity blockEntity) { } + @Override public void levelEvent(net.minecraft.world.entity.Entity source, int type, BlockPos at, int data) { } + @Override public void gameEvent(net.minecraft.core.Holder<net.minecraft.world.level.gameevent.GameEvent> event, + net.minecraft.world.phys.Vec3 at, net.minecraft.world.level.gameevent.GameEvent.Context context) { } + @Override public BlockState getBlockState(BlockPos p) { return cells.getOrDefault(p, Blocks.AIR.defaultBlockState()); } + @Override public void playSeededSound(net.minecraft.world.entity.Entity except, double x, double y, double z, + net.minecraft.core.Holder<net.minecraft.sounds.SoundEvent> sound, net.minecraft.sounds.SoundSource source, + float volume, float pitch, long seed) { } + @Override public int getMinY() { return -64; } + @Override public int getHeight() { return 384; } + } + + static final class Chunk extends LevelChunk { + Chunk() { + super(null, null); + } + @Override public BlockState getBlockState(BlockPos p) { return cells.getOrDefault(p, Blocks.AIR.defaultBlockState()); } + @Override public void markUnsaved() { } + } + + static void reset() throws Exception { + cells = new HashMap<>(); + chunks = new HashMap<>(); + world = alloc(World.class); + set(Level.class, world, "blockEntityTickers", new ArrayList<TickingBlockEntity>()); + set(Level.class, world, "pendingBlockEntityTickers", new ArrayList<TickingBlockEntity>()); + set(Level.class, world, "soundSeedGenerator", net.minecraft.util.RandomSource.create(0)); + TickRateManager rate = new TickRateManager(); + set(TickRateManager.class, rate, "runGameElements", false); + world.rate = rate; + LevelDebugSynchronizers debug = alloc(LevelDebugSynchronizers.class); + set(LevelDebugSynchronizers.class, debug, "sleeping", true); + world.debug = debug; + } + + static Chunk chunk(int cx, int cz) throws Exception { + Chunk c = chunks.get(ChunkPos.pack(cx, cz)); + if (c != null) return c; + c = alloc(Chunk.class); + set(ChunkAccess.class, c, "blockEntities", new it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap<BlockPos, BlockEntity>()); + set(ChunkAccess.class, c, "pendingBlockEntities", new HashMap<BlockPos, net.minecraft.nbt.CompoundTag>()); + set(ChunkAccess.class, c, "chunkPos", new ChunkPos(cx, cz)); + set(LevelChunk.class, c, "level", world); + set(LevelChunk.class, c, "tickersInLevel", new HashMap<>()); + chunks.put(ChunkPos.pack(cx, cz), c); + return c; + } + + /** The chunk at `p`, in the level (a chunk first seen here was already loaded). */ + static Chunk at(BlockPos p) throws Exception { + Chunk c = chunk(p.getX() >> 4, p.getZ() >> 4); + c.setLoaded(true); + return c; + } + + /** The block's state: a jukebox's has_record from the name (`jukebox_record`). */ + static BlockState state(String name) { + boolean record = name.equals("jukebox_record"); + Block block = BuiltInRegistries.BLOCK.getValue(Identifier.withDefaultNamespace(record ? "jukebox" : name)); + BlockState s = block.defaultBlockState(); + return block instanceof JukeboxBlock ? s.setValue(JukeboxBlock.HAS_RECORD, record) : s; + } + + static BlockEntity make(BlockPos p) { + BlockState s = cells.get(p); + return ((EntityBlock) s.getBlock()).newBlockEntity(p, s); + } + + static BlockPos pos(String[] w, int i) { + return new BlockPos(Integer.parseInt(w[i]), Integer.parseInt(w[i + 1]), Integer.parseInt(w[i + 2])); + } + + public static void main(String[] args) throws Exception { + // Registries with item components bound (vaults hold item stacks). + ParityData.init(); + var allocator = Class.forName("sun.misc.Unsafe"); + var field = allocator.getDeclaredField("theUnsafe"); + field.setAccessible(true); + unsafe = field.get(null); + allocate = allocator.getMethod("allocateInstance", Class.class); + Method update = LevelChunk.class.getDeclaredMethod("updateBlockEntityTicker", BlockEntity.class); + update.setAccessible(true); + StringBuilder out = new StringBuilder(); + for (String line : Files.readAllLines(Path.of(args[0]))) { + String[] w = line.trim().split(" "); + switch (w[0]) { + case "reset" -> reset(); + case "load" -> { + // SerializableChunkData.postLoadChunk, then ChunkStatusTasks.full. + Chunk c = chunk(Integer.parseInt(w[1]), Integer.parseInt(w[2])); + for (int i = 3; i + 3 < w.length; i += 4) { + BlockPos p = pos(w, i + 1); + cells.put(p, state(w[i])); + c.setBlockEntity(make(p)); + } + c.setLoaded(true); + c.registerAllBlockEntitiesAfterLevelLoad(); + } + case "place" -> { + // LevelChunk.setBlockState: a new block entity block. + BlockPos p = pos(w, 2); + cells.put(p, state(w[1])); + at(p).addAndRegisterBlockEntity(make(p)); + } + case "replace" -> { + BlockPos p = pos(w, 1); + at(p).addAndRegisterBlockEntity(make(p)); + } + case "remove" -> { + BlockPos p = pos(w, 1); + at(p).removeBlockEntity(p); + cells.remove(p); + } + case "record" -> { + // LevelChunk.setBlockState: the same block, another state. + BlockPos p = pos(w, 1); + BlockState s = state(w[4].equals("1") ? "jukebox_record" : "jukebox"); + cells.put(p, s); + BlockEntity be = at(p).getBlockEntity(p); + be.setBlockState(s); + update.invoke(at(p), be); + } + case "unload" -> { + Chunk c = chunks.remove(ChunkPos.pack(Integer.parseInt(w[1]), Integer.parseInt(w[2]))); + if (c != null) c.clearAllBlockEntities(); + } + case "tick" -> { + world.tickBlockEntities(); + @SuppressWarnings("unchecked") + List<TickingBlockEntity> list = (List<TickingBlockEntity>) get(Level.class, world, "blockEntityTickers"); + out.append("tick"); + for (TickingBlockEntity t : list) { + BlockPos p = t.getPos(); + out.append(' ').append(p.getX()).append(',').append(p.getY()).append(',').append(p.getZ()); + } + out.append('\n'); + } + default -> throw new IllegalArgumentException(line); + } + } + Files.writeString(Path.of(args[1]), out); + } +} diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCommandExecOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCommandExecOracle.java index 4b61558d8..8c6225401 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCommandExecOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCommandExecOracle.java @@ -222,6 +222,7 @@ public final class VanillaCommandExecOracle { } } case "settle" -> settle(); + case "portal" -> portal(); default -> throw new IllegalArgumentException(line); } } @@ -278,6 +279,23 @@ public final class VanillaCommandExecOracle { } /** Force-load the chunks around the player (Horizon waits for its streamed terrain). */ + /** + * Entity.handlePortal once the player's portal process finished in the + * nether portal it stands in (the oracle does not tick the player). + */ + void portal() { + ServerLevel level = player.level(); + net.minecraft.core.BlockPos entry = player.blockPosition(); + if (!level.getBlockState(entry).is(net.minecraft.world.level.block.Blocks.NETHER_PORTAL)) { + throw new IllegalStateException("portal: the player is not in a nether portal"); + } + var block = (net.minecraft.world.level.block.NetherPortalBlock) net.minecraft.world.level.block.Blocks.NETHER_PORTAL; + var transition = block.getPortalDestination(level, player, entry); + if (transition != null) { + player.teleportToPortalDestination(level, transition); + } + } + void settle() { ServerLevel level = player.level(); int cx = player.chunkPosition().x(), cz = player.chunkPosition().z(); diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java index 19779739b..2002285e3 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaDispenserOracle.java @@ -170,14 +170,82 @@ public final class VanillaDispenserOracle { throw new IllegalStateException("no slots in " + be); } - /** The removal a "remove" case runs on the block at its origin. */ - static void remove(VanillaBlockUpdateOracle.World w, BlockPos origin, JsonObject op) { + /** The removal a "remove" case runs on the block at its origin; a player's mining prints its statistics and held stack. */ + static List<String> remove(VanillaBlockUpdateOracle.World w, BlockPos origin, JsonObject op) throws Exception { switch (op.get("op").getAsString()) { case "set" -> w.setBlock(origin, Block.stateById(op.get("state").getAsInt()), op.get("flags").getAsInt()); case "destroy" -> w.destroyBlock(origin, op.get("drop").getAsBoolean()); case "remove" -> w.removeBlock(origin, false); + case "mine" -> { + return mine(w, origin, op); + } default -> throw new IllegalStateException(op.toString()); } + return new ArrayList<>(); + } + + /** + * A survival player three blocks south of `pos` mines it holding the case's + * item: the real `ServerPlayerGameMode.destroyBlock` (playerWillDestroy, + * removeBlock with the block entity's side effects, mineBlock, playerDestroy's + * statistic, exhaustion and drops). The statistics and the held stack follow. + */ + static List<String> mine(VanillaBlockUpdateOracle.World w, BlockPos pos, JsonObject op) throws Exception { + Miner player = (Miner) VanillaBlockUpdateOracle.allocate.invoke(VanillaBlockUpdateOracle.unsafe, Miner.class); + player.world = w; + player.hand = stack(op.get("item").getAsString()); + player.offhand = ItemStack.EMPTY; + player.stats = new ArrayList<>(); + VanillaBlockUpdateOracle.set(net.minecraft.world.entity.player.Player.class, player, "abilities", new net.minecraft.world.entity.player.Abilities()); + VanillaBlockUpdateOracle.set(net.minecraft.world.entity.player.Player.class, player, "foodData", new net.minecraft.world.food.FoodData()); + var advancements = (net.minecraft.server.PlayerAdvancements) VanillaBlockUpdateOracle.allocate.invoke(VanillaBlockUpdateOracle.unsafe, net.minecraft.server.PlayerAdvancements.class); + VanillaBlockUpdateOracle.set(net.minecraft.server.PlayerAdvancements.class, advancements, "activeTriggers", new HashMap<>()); + VanillaBlockUpdateOracle.set(net.minecraft.server.level.ServerPlayer.class, player, "advancements", advancements); + var at = net.minecraft.world.phys.Vec3.atBottomCenterOf(pos).add(0, 1, 3); + VanillaBlockUpdateOracle.set(Entity.class, player, "position", at); + VanillaBlockUpdateOracle.set(Entity.class, player, "blockPosition", BlockPos.containing(at)); + // PiglinAi.angerNearbyPiglins looks around the player's box (gold, chests, barrels...). + player.setBoundingBox(EntityTypes.PLAYER.getDimensions().makeBoundingBox(at)); + var gameMode = new net.minecraft.server.level.ServerPlayerGameMode(player); + VanillaBlockUpdateOracle.set(net.minecraft.server.level.ServerPlayerGameMode.class, gameMode, "gameModeForPlayer", + net.minecraft.world.level.GameType.SURVIVAL); + gameMode.destroyBlock(pos); + List<String> out = new ArrayList<>(player.stats); + out.add("mine held " + ParityData.canon(player.hand)); + return out; + } + + /** The player of a "mine" case: a survival `Clicker` whose selected inventory slot is its main hand. */ + static final class Miner extends net.minecraft.server.level.ServerPlayer { + VanillaBlockUpdateOracle.World world; + ItemStack hand, offhand; + List<String> stats; + Miner() { + super(null, null, null, null); + } + @Override public ServerLevel level() { return world; } + @Override public boolean hasInfiniteMaterials() { return false; } + @Override public boolean isCreative() { return false; } + @Override public boolean isSpectator() { return false; } + @Override public EntityType<?> getType() { return EntityTypes.PLAYER; } + @Override public net.minecraft.core.Holder<EntityType<?>> typeHolder() { return EntityTypes.PLAYER.builtInRegistryHolder(); } + @Override public ItemStack getItemBySlot(EquipmentSlot slot) { + return slot == EquipmentSlot.MAINHAND ? hand : slot == EquipmentSlot.OFFHAND ? offhand : ItemStack.EMPTY; + } + @Override public void setItemSlot(EquipmentSlot slot, ItemStack stack) { + if (slot == EquipmentSlot.MAINHAND) hand = stack; else if (slot == EquipmentSlot.OFFHAND) offhand = stack; + } + @Override public ItemStack getItemInHand(net.minecraft.world.InteractionHand h) { + return h == net.minecraft.world.InteractionHand.MAIN_HAND ? hand : offhand; + } + // Player.hasCorrectToolForDrops asks the inventory's selected slot: the main hand. + @Override public boolean hasCorrectToolForDrops(BlockState state) { + return !state.requiresCorrectToolForDrops() || hand.isCorrectToolForDrops(state); + } + @Override public void awardStat(net.minecraft.stats.Stat<?> stat, int count) { + stats.add("stat " + User.key(stat) + " " + BuiltInRegistries.STAT_TYPE.getKey(stat.getType()) + " " + count); + } + @Override public net.minecraft.server.players.NameAndId nameAndId() { return new net.minecraft.server.players.NameAndId(new UUID(0L, 0L), "Player"); } } static boolean hasList(Object be) { @@ -324,7 +392,7 @@ public final class VanillaDispenserOracle { BlockState state = w.getBlockState(origin); List<String> used = new ArrayList<>(); if (f.has("remove")) { - remove(w, origin, f.getAsJsonObject("remove")); + used = remove(w, origin, f.getAsJsonObject("remove")); } else if (f.has("use")) { used = use(w, origin, f.getAsJsonObject("use")); } else if (f.has("use_item")) { diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaGameLoopOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaGameLoopOracle.java index 0be89888a..294ceacac 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaGameLoopOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaGameLoopOracle.java @@ -118,13 +118,14 @@ public final class VanillaGameLoopOracle { static final class StubPlayer extends LocalPlayer { boolean scoping; + boolean spectator; StubPlayer() { super(null, null, null, null, null, Input.EMPTY, false, null, null); } @Override public boolean isScoping() { return scoping; } - @Override public boolean isSpectator() { return false; } + @Override public boolean isSpectator() { return spectator; } } static Minecraft shell; @@ -327,6 +328,12 @@ public final class VanillaGameLoopOracle { StubPlayer player = make(StubPlayer.class); Inventory inventory = make(Inventory.class); inventory.setSelectedSlot(c.get("slot").getAsInt()); + player.spectator = c.has("spectator") && c.get("spectator").getAsBoolean(); + field(net.minecraft.world.entity.player.Player.class, "abilities").set(player, new net.minecraft.world.entity.player.Abilities()); + var hud = make(net.minecraft.client.gui.Hud.class); + field(net.minecraft.client.gui.Hud.class, "spectatorGui").set(hud, + make(net.minecraft.client.gui.components.spectator.SpectatorGui.class)); + field(Gui.class, "hud").set(mc.gui, hud); field(net.minecraft.world.entity.player.Player.class, "inventory").set(player, inventory); mc.player = player; options.discreteMouseScroll().set(c.get("discrete").getAsBoolean()); @@ -336,7 +343,7 @@ public final class VanillaGameLoopOracle { for (var e : c.getAsJsonArray("ops")) { var op = e.getAsJsonArray(); h.onScroll(1L, d(op.get(0)), d(op.get(1))); - out.println(inventory.getSelectedSlot()); + out.println(inventory.getSelectedSlot() + (player.spectator ? " " + fb(player.getAbilities().getFlyingSpeed()) : "")); } } diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaInteractionInputOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaInteractionInputOracle.java new file mode 100644 index 000000000..884e5042a --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaInteractionInputOracle.java @@ -0,0 +1,161 @@ +package dev.horizonlab.parity; + +import com.google.gson.*; +import java.io.PrintWriter; +import java.lang.reflect.*; +import java.nio.file.*; +import java.util.*; +import net.minecraft.client.*; +import net.minecraft.client.gui.Gui; +import net.minecraft.client.multiplayer.*; +import net.minecraft.client.player.LocalPlayer; +import net.minecraft.core.*; +import net.minecraft.network.protocol.Packet; +import net.minecraft.world.*; +import net.minecraft.world.entity.*; +import net.minecraft.world.entity.player.*; +import net.minecraft.world.flag.*; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.component.SwingAnimation; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.border.WorldBorder; +import net.minecraft.world.phys.*; + +/** Runs the shipped handleKeybinds; only game-mode effects and UI are shells. */ +public final class VanillaInteractionInputOracle { + static Object unsafe; + static Method allocate; + static List<String> events = new ArrayList<>(); + static Field field(Class<?> type, String name) throws Exception { + var f = type.getDeclaredField(name); f.setAccessible(true); return f; + } + static <T> T make(Class<T> type) throws Exception { return type.cast(allocate.invoke(unsafe, type)); } + public static class MC extends Minecraft { + public MC() { super(null); } + @Override public boolean hasControlDown() { return false; } + } + public static class UI extends Gui { + public UI() { super(null, null, null); } + @Override public void handleKeybinds() {} + } + public static class Mouse extends MouseHandler { + public Mouse() { super(null); } + @Override public boolean isMouseGrabbed() { return true; } + } + public static class Level extends ClientLevel { + public Level() { super(null, null, null, null, 0, 0, null, false, 0L, 0); } + @Override public BlockState getBlockState(BlockPos pos) { return Blocks.STONE.defaultBlockState(); } + @Override public FeatureFlagSet enabledFeatures() { return FeatureFlags.REGISTRY.allFlags(); } + } + public static class Connection extends ClientPacketListener { + public Connection() { super(null, null, null); } + @Override public void send(Packet<?> packet) {} + } + public static class Human extends LocalPlayer { + boolean using; + ItemStack held = ItemStack.EMPTY; + public Human() { super(null, null, null, null, null, null, false, null, null); } + @Override public boolean isUsingItem() { return using; } + @Override public boolean isSpectator() { return false; } + @Override public boolean isCreative() { return false; } + @Override public boolean isHandsBusy() { return false; } + @Override public ItemStack getItemInHand(InteractionHand hand) { + return hand == InteractionHand.MAIN_HAND ? held : ItemStack.EMPTY; + } + @Override public void itemUsed(InteractionHand hand) {} + @Override public boolean cannotAttackWithItem(ItemStack item, int slot) { return false; } + @Override public boolean isWithinEntityInteractionRange(Entity e, double buffer) { return true; } + @Override public boolean swing(InteractionHand hand, SwingAnimation animation, boolean send) { return false; } + } + public static class Mode extends MultiPlayerGameMode { + boolean destroying, digOnAttack, beginUsing, passing; + Human player; + public Mode() { super(null, null); } + @Override public boolean isDestroying() { return destroying; } + @Override public boolean isSpectator() { return false; } + @Override public boolean startDestroyBlock(BlockPos pos, Direction face) { + events.add("attack"); destroying = digOnAttack; return true; + } + @Override public boolean continueDestroyBlock(BlockPos pos, Direction face) { + events.add("continue true"); return true; + } + @Override public void stopDestroyBlock() { events.add("continue false"); destroying = false; } + @Override public void attack(Player p, Entity e) { events.add("attack"); } + @Override public void releaseUsingItem(Player p) { events.add("release"); player.using = false; } + InteractionResult use() { events.add("use"); player.using = beginUsing; return InteractionResult.SUCCESS_SERVER; } + @Override public InteractionResult useItemOn(LocalPlayer p, InteractionHand h, BlockHitResult hit) { + if (passing) { events.add("block"); return InteractionResult.PASS; } + return use(); + } + @Override public InteractionResult interact(Player p, Entity e, EntityHitResult hit, InteractionHand h) { + if (passing) { events.add("entity"); return InteractionResult.PASS; } + return use(); + } + @Override public InteractionResult useItem(Player p, InteractionHand h) { + events.add("item"); player.using = beginUsing; return InteractionResult.SUCCESS_SERVER; + } + @Override public void handlePickItemFromBlock(BlockPos pos, boolean data) { events.add("pick"); } + @Override public void handlePickItemFromEntity(Entity entity, boolean data) { events.add("pick"); } + } + static boolean flag(JsonObject row, String name) { return row.get(name).getAsBoolean(); } + public static void main(String[] args) throws Exception { + ParityData.init(); + var u = Class.forName("sun.misc.Unsafe"); unsafe = field(u, "theUnsafe").get(null); + allocate = u.getMethod("allocateInstance", Class.class); + var handle = Minecraft.class.getDeclaredMethod("handleKeybinds"); handle.setAccessible(true); + try (var out = new PrintWriter(Files.newBufferedWriter(Path.of(args[1]))); + var lines = Files.lines(Path.of(args[0]))) { + int i = 0; + for (String line : (Iterable<String>) lines::iterator) { + var row = JsonParser.parseString(line).getAsJsonObject(); + var mc = make(MC.class); var options = make(Options.class); + for (var f : Options.class.getDeclaredFields()) { + if (Modifier.isStatic(f.getModifiers())) continue; + f.setAccessible(true); + if (f.getType() == KeyMapping.class) f.set(options, make(KeyMapping.class)); + if (f.getType() == KeyMapping[].class) { + var keys = new KeyMapping[9]; + for (int n = 0; n < keys.length; n++) keys[n] = make(KeyMapping.class); + f.set(options, keys); + } + } + var player = make(Human.class); player.using = flag(row, "using"); + boolean passing = row.has("passing") && flag(row, "passing"); + player.held = passing ? new ItemStack(Blocks.STONE.asItem()) : ItemStack.EMPTY; + var level = make(Level.class); + field(ClientLevel.class, "worldBorder").set(level, new WorldBorder()); + field(Player.class, "abilities").set(player, new Abilities()); + field(LocalPlayer.class, "connection").set(player, make(Connection.class)); + var mode = make(Mode.class); mode.player = player; + mode.destroying = flag(row, "destroying"); mode.digOnAttack = flag(row, "dig_on_attack"); + mode.beginUsing = flag(row, "begin_using"); + mode.passing = passing; + field(Minecraft.class, "options").set(mc, options); + field(Minecraft.class, "gui").set(mc, make(UI.class)); + field(Minecraft.class, "mouseHandler").set(mc, make(Mouse.class)); + field(Minecraft.class, "player").set(mc, player); + field(Minecraft.class, "level").set(mc, level); + field(Minecraft.class, "gameMode").set(mc, mode); + var entity = make(net.minecraft.world.entity.item.ItemEntity.class); + field(Entity.class, "blockPosition").set(entity, BlockPos.ZERO); + field(Minecraft.class, "hitResult").set(mc, flag(row, "entity") + ? new EntityHitResult(entity, Vec3.ZERO) + : new BlockHitResult(Vec3.ZERO, Direction.UP, BlockPos.ZERO, false)); + String[] names = {"keyAttack", "keyUse", "keyPickItem"}; + for (var click : row.getAsJsonArray("clicks")) { + var key = (KeyMapping)field(Options.class, names[click.getAsInt()]).get(options); + var count = field(KeyMapping.class, "clickCount"); + count.setInt(key, count.getInt(key) + 1); + } + field(KeyMapping.class, "isDown").setBoolean(options.keyAttack, flag(row, "attack_down")); + field(KeyMapping.class, "isDown").setBoolean(options.keyUse, flag(row, "use_down")); + field(Minecraft.class, "rightClickDelay").setInt(mc, row.get("delay").getAsInt()); + events.clear(); handle.invoke(mc); + out.println("== " + i++); + for (String event : events) out.println(event); + out.println("state " + player.using + " " + mode.destroying + " " + field(Minecraft.class, "rightClickDelay").getInt(mc)); + } + } + } +} diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaPlayerSyncOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaPlayerSyncOracle.java new file mode 100644 index 000000000..056240bb8 --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaPlayerSyncOracle.java @@ -0,0 +1,109 @@ +package dev.horizonlab.parity; + +import com.google.gson.*; +import io.netty.buffer.Unpooled; +import java.io.PrintWriter; +import java.lang.reflect.*; +import java.nio.file.*; +import java.util.HexFormat; +import net.minecraft.client.*; +import net.minecraft.client.multiplayer.ClientPacketListener; +import net.minecraft.client.player.LocalPlayer; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.Packet; +import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket; +import net.minecraft.world.entity.*; +import net.minecraft.world.phys.Vec3; + +/** Runs shipped methods; only the world lifecycle and packet sink are fixtures. */ +public final class VanillaPlayerSyncOracle { + static Object unsafe; + static Method allocate; + static PrintWriter out; + static Field field(Class<?> type, String name) throws Exception { + var f = type.getDeclaredField(name); f.setAccessible(true); return f; + } + static <T> T make(Class<T> type) throws Exception { + return type.cast(allocate.invoke(unsafe, type)); + } + public static final class Player extends LocalPlayer { + boolean ground; + public Player() { super(null, null, null, null, null, null, false, null, null); } + @Override protected boolean isControlledCamera() { return true; } + @Override public boolean isSprinting() { return false; } + @Override public boolean onGround() { return ground; } + } + public static final class Sink extends ClientPacketListener { + boolean sent; + public Sink() { super(null, null, null); } + @Override public void send(Packet<?> packet) { + sent = true; + var buf = new FriendlyByteBuf(Unpooled.buffer()); + String name; + try { + if (packet instanceof ServerboundMovePlayerPacket.PosRot p) { + name = "posrot"; ServerboundMovePlayerPacket.PosRot.STREAM_CODEC.encode(buf, p); + } else if (packet instanceof ServerboundMovePlayerPacket.Pos p) { + name = "pos"; ServerboundMovePlayerPacket.Pos.STREAM_CODEC.encode(buf, p); + } else if (packet instanceof ServerboundMovePlayerPacket.Rot p) { + name = "rot"; ServerboundMovePlayerPacket.Rot.STREAM_CODEC.encode(buf, p); + } else if (packet instanceof ServerboundMovePlayerPacket.StatusOnly p) { + name = "status"; ServerboundMovePlayerPacket.StatusOnly.STREAM_CODEC.encode(buf, p); + } else { throw new AssertionError(packet); } + byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); + out.println(name + " " + HexFormat.of().formatHex(bytes)); + } finally { buf.release(); } + } + } + static double number(JsonElement value) { + return Double.longBitsToDouble(Long.parseUnsignedLong(value.getAsString(), 16)); + } + static Vec3 vector(JsonObject row, String key) { + var a = row.getAsJsonArray(key); + return new Vec3(number(a.get(0)), number(a.get(1)), number(a.get(2))); + } + public static void main(String[] args) throws Exception { + net.minecraft.SharedConstants.tryDetectVersion(); + net.minecraft.server.Bootstrap.bootStrap(); + var u = Class.forName("sun.misc.Unsafe"); + unsafe = field(u, "theUnsafe").get(null); + allocate = u.getMethod("allocateInstance", Class.class); + var sendPosition = LocalPlayer.class.getDeclaredMethod("sendPosition"); + sendPosition.setAccessible(true); + var minecraft = make(Minecraft.class); + var options = make(Options.class); + field(Options.class, "autoJump").set(options, OptionInstance.createBoolean("test", false)); + field(Minecraft.class, "options").set(minecraft, options); + try (var output = new PrintWriter(Files.newBufferedWriter(Path.of(args[1]))); + var lines = Files.lines(Path.of(args[0]))) { + out = output; + for (String line : (Iterable<String>) lines::iterator) { + var row = JsonParser.parseString(line).getAsJsonObject(); + if (row.get("kind").getAsString().equals("movement")) { + var p = make(Player.class); + var sink = make(Sink.class); + field(LocalPlayer.class, "connection").set(p, sink); + field(LocalPlayer.class, "minecraft").set(p, minecraft); + for (var value : row.getAsJsonArray("ops")) { + var op = value.getAsJsonArray(); + field(Entity.class, "position").set(p, new Vec3(number(op.get(0)), number(op.get(1)), number(op.get(2)))); + field(Entity.class, "yRot").setFloat(p, (float)number(op.get(3))); + field(Entity.class, "xRot").setFloat(p, (float)number(op.get(4))); + p.ground = op.get(5).getAsBoolean(); + p.horizontalCollision = op.get(6).getAsBoolean(); + sink.sent = false; + sendPosition.invoke(p); + if (!sink.sent) out.println("-"); + } + } else { + var old = row.getAsJsonArray("old"); + var next = row.getAsJsonArray("new"); + var base = new PositionMoveRotation(Vec3.ZERO, vector(row, "velocity"), (float)number(old.get(0)), (float)number(old.get(1))); + var change = new PositionMoveRotation(Vec3.ZERO, vector(row, "delta"), (float)number(next.get(0)), (float)number(next.get(1))); + var v = PositionMoveRotation.calculateAbsolute(base, change, Relative.unpack(row.get("flags").getAsInt())).deltaMovement(); + out.printf("%016x %016x %016x%n", Double.doubleToRawLongBits(v.x), Double.doubleToRawLongBits(v.y), Double.doubleToRawLongBits(v.z)); + } + } + } + } +} diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java index bb438b735..e1e7a24c4 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java @@ -113,6 +113,8 @@ public final class VanillaProtocolClient { private ProtocolInfo<?> inbound; private ProtocolInfo<?> outbound; private String state; + /** With a fifth argument {@code entities}: the entity transcript mode (EntityTranscript). */ + private EntityTranscript entities; public static void main(String[] args) throws Exception { ParityData.init(); @@ -121,6 +123,7 @@ public final class VanillaProtocolClient { String name = args[2]; Path output = Path.of(args[3]); VanillaProtocolClient client = new VanillaProtocolClient(); + if (args.length > 4 && args[4].equals("entities")) client.entities = new EntityTranscript(); try { client.status(host, port); client.join(host, port, name); @@ -405,6 +408,18 @@ public final class VanillaProtocolClient { play(); } + /** The entity mode's step, and how long it records the entities for (ms). */ + private static final int ENTITIES = 1000; + private static final long ENTITY_WAIT = 20_000; + + static int moveEntityId(net.minecraft.network.protocol.game.ClientboundMoveEntityPacket p) { + return (int) field(p, net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.class, "entityId"); + } + + static int rotateHeadId(net.minecraft.network.protocol.game.ClientboundRotateHeadPacket p) { + return (int) field(p, p.getClass(), "entityId"); + } + private static final String[] REPLIES = {"random value 7..7", "random value x", "trigger nope"}; private void play() throws Exception { @@ -447,9 +462,19 @@ public final class VanillaProtocolClient { net.minecraft.core.BlockPos furnace = null; int furnaceWindow = -1; int furnaceState = 0; + // Snowballs the server spawned since the last right click. + int snowballs = 0; + // The player's own id, and what the ender pearl it throws does to it. + int playerId = -1; + boolean pearl = false; + int pearlTeleports = 0; + float pearlHealth = -1; // The villager the server's console summoned beside the spawn. int villager = -1; net.minecraft.world.phys.Vec3 villagerAt = null; + // When the dead player sent a command (ServerGamePacketListenerImpl.handleChatCommand runs it + // whether the player is alive or not); it respawns once the reply came, or 3 s later. + long deadCommandAt = 0; while (System.currentTimeMillis() < deadline) { Received r; try { @@ -461,6 +486,7 @@ public final class VanillaProtocolClient { if (r != null) { Packet<?> packet = r.packet; counts.merge(r.type, 1, Integer::sum); + if (entities != null && !entities.acceptSelf(packet) && step >= ENTITIES) entities.accept(packet); if (r.type.equals("minecraft:chunk_batch_start")) ordered = false; if (respawning && !respawned) { String line = respawnLine(r); @@ -478,13 +504,20 @@ public final class VanillaProtocolClient { loadStart = true; } } + if (packet instanceof net.minecraft.network.protocol.game.ClientboundLoginPacket p) playerId = p.playerId(); + // The first health after the pearl: the hurt (regeneration follows each server's ticks). + if (pearl && pearlHealth < 0 && packet instanceof net.minecraft.network.protocol.game.ClientboundSetHealthPacket p) { + pearlHealth = p.getHealth(); + } if (packet instanceof ClientboundKeepAlivePacket p) { send(new ServerboundKeepAlivePacket(p.getId())); } else if (packet instanceof ClientboundPingPacket p) { send(new ServerboundPongPacket(p.getId())); } else if (packet instanceof ClientboundPlayerPositionPacket p) { position = PositionMoveRotation.calculateAbsolute(position, p.change(), p.relatives()); - record("play-move", text(position.position()) + " " + position.yRot() + " " + position.xRot()); + // Where a pearl lands follows its random inaccuracy: only counted. + if (pearl) pearlTeleports++; + else record("play-move", text(position.position()) + " " + position.yRot() + " " + position.xRot()); if (respawning) respawnAt = position; send(new ServerboundAcceptTeleportationPacket(p.id(), position.position().x, position.position().y, position.position().z, position.yRot(), position.xRot())); @@ -527,9 +560,12 @@ public final class VanillaProtocolClient { } else if (packet instanceof net.minecraft.network.protocol.game.ClientboundPlayerCombatKillPacket p && step > 0) { // The death message is left out: guests' death messages aren't Horizon's yet. record("play-death", "death screen"); - send(new net.minecraft.network.protocol.game.ServerboundClientCommandPacket( - net.minecraft.network.protocol.game.ServerboundClientCommandPacket.Action.PERFORM_RESPAWN)); - respawning = true; + send(new ServerboundChatCommandPacket("trigger nope")); + deadCommandAt = now; + } else if (packet instanceof ClientboundSystemChatPacket p && deadCommandAt > 0 && !respawning + && json(p.content()).contains("nope")) { + record("play-death", "command reply " + json(p.content())); + respawning = respawn(); } else if (packet instanceof ClientboundSystemChatPacket p && step > 0 && step < 5 + REPLIES.length) { record("play-chat", "system " + p.overlay() + " " + json(p.content())); } else if (packet instanceof ClientboundPlayerChatPacket p && step > 0 && step < 5 + REPLIES.length) { @@ -539,6 +575,11 @@ public final class VanillaProtocolClient { + p.chatType().targetName().map(VanillaProtocolClient::json)); } else if (packet instanceof ClientboundDisguisedChatPacket p && step > 0 && step < 5 + REPLIES.length) { record("play-chat", "disguised " + json(p.message()) + " " + p.chatType().chatType().getRegisteredName()); + } else if (packet instanceof net.minecraft.network.protocol.game.ClientboundAddEntityPacket p + && p.getType() == net.minecraft.world.entity.EntityTypes.SNOWBALL) { + snowballs++; + // Projectile.getAddEntityPacket: its owner's id. + record("play-use", "snowball owner is the player: " + (p.getData() == playerId)); } else if (packet instanceof net.minecraft.network.protocol.game.ClientboundAddEntityPacket p && p.getType() == net.minecraft.world.entity.EntityTypes.VILLAGER) { villager = p.getId(); @@ -581,8 +622,20 @@ public final class VanillaProtocolClient { commandTree(p); } } + if (entities != null && loaded && lastChunk > 0 && now - lastChunk > 3000 && step == 0) { + // Entity mode: the harness's console summons the scene once it reads this + // line; everything tracked from then on is recorded until the client leaves. + record("play-chunks", String.valueOf(chunks.size())); + send(new ServerboundChatPacket("summon the entities", Instant.now(), 0L, Optional.empty(), + new LastSeenMessages.Update(0, new BitSet(), (byte) 0))); + step = ENTITIES; + stepAt = now; + } else if (step == ENTITIES && now - stepAt > ENTITY_WAIT) { + for (String line : entities.lines()) record("play-entities", line); + return; + } // Once the chunks stop coming: chat, then /list, then leave. - if (loaded && lastChunk > 0 && now - lastChunk > 3000 && step == 0) { + if (entities == null && loaded && lastChunk > 0 && now - lastChunk > 3000 && step == 0) { record("play-chunks", chunks.size() + " " + chunks); BLOCK_ENTITIES.forEach((k, v) -> record("play-block-entities", k + " " + v)); // Which light layers exist depends on the neighbouring @@ -632,6 +685,9 @@ public final class VanillaProtocolClient { new LastSeenMessages.Update(0, new BitSet(), (byte) 0))); step++; stepAt = now; + } else if (deadCommandAt > 0 && !respawning && now - deadCommandAt > 3000) { + record("play-death", "no command reply"); + respawning = respawn(); } else if (step == 6 + REPLIES.length && loadStart && respawnChunk) { // LevelLoadTracker is done: the vanilla client leaves the loading screen. if (runType != null) record("play-respawn", runLine(runType, run)); @@ -643,6 +699,11 @@ public final class VanillaProtocolClient { + " minecraft:furnace")); send(new ServerboundChatCommandPacket("give @s minecraft:raw_iron")); send(new ServerboundChatCommandPacket("give @s minecraft:coal")); + // A score on @s is the player's own (its feedback reaches the console: broadcast); + // a query's reply doesn't. + send(new ServerboundChatCommandPacket("scoreboard objectives add parity dummy")); + send(new ServerboundChatCommandPacket("scoreboard players set @s parity 3")); + send(new ServerboundChatCommandPacket("scoreboard players get @s parity")); step++; stepAt = now; } else if (step == 7 + REPLIES.length && now - stepAt > 1500) { @@ -667,6 +728,52 @@ public final class VanillaProtocolClient { } else if (step == 11 + REPLIES.length && now - stepAt > 1500) { send(new net.minecraft.network.protocol.game.ServerboundContainerClosePacket(furnaceWindow)); record("play-furnace", "closed"); + step++; + stepAt = now; + } else if (step == 12 + REPLIES.length && now - stepAt > 1500) { + // Once the furnace is closed on the server too: the slot change isn't the furnace menu's. + send(new ServerboundChatCommandPacket("item replace entity @s weapon.mainhand with minecraft:snowball 16")); + step++; + stepAt = now; + } else if (step == 13 + REPLIES.length && now - stepAt > 1500) { + // A right click on the ground with a snowball, as the 26.3 client sends it + // (Minecraft.startUseItem): the block's part, which passes, then the item's. + useOn(respawnAt, net.minecraft.world.InteractionHand.MAIN_HAND, 2); + use(position, net.minecraft.world.InteractionHand.MAIN_HAND, 3); + snowballs = 0; + step++; + stepAt = now; + } else if (step == 14 + REPLIES.length && now - stepAt > 2000) { + record("play-use", "main hand on a block: " + snowballs + " snowball(s)"); + send(new ServerboundChatCommandPacket("item replace entity @s weapon.mainhand with minecraft:air")); + send(new ServerboundChatCommandPacket("item replace entity @s weapon.offhand with minecraft:snowball 16")); + step++; + stepAt = now; + } else if (step == 15 + REPLIES.length && now - stepAt > 1500) { + // An empty main hand: its block part only (no item to use), then the off hand's two. + useOn(respawnAt, net.minecraft.world.InteractionHand.MAIN_HAND, 4); + useOn(respawnAt, net.minecraft.world.InteractionHand.OFF_HAND, 5); + use(position, net.minecraft.world.InteractionHand.OFF_HAND, 6); + snowballs = 0; + step++; + stepAt = now; + } else if (step == 16 + REPLIES.length && now - stepAt > 2000) { + record("play-use", "off hand on a block: " + snowballs + " snowball(s)"); + // An ender pearl thrown at its feet (no endermite: ThrownEnderpearl spawns one 5% of the time). + send(new ServerboundChatCommandPacket("gamerule spawn_monsters false")); + send(new ServerboundChatCommandPacket("item replace entity @s weapon.offhand with minecraft:air")); + send(new ServerboundChatCommandPacket("item replace entity @s weapon.mainhand with minecraft:ender_pearl")); + step++; + stepAt = now; + } else if (step == 17 + REPLIES.length && now - stepAt > 1500) { + pearl = true; + send(new net.minecraft.network.protocol.game.ServerboundUseItemPacket(net.minecraft.world.InteractionHand.MAIN_HAND, 7, + position.yRot(), 90.0F)); + step++; + stepAt = now; + } else if (step == 18 + REPLIES.length && now - stepAt > 2500) { + // ThrownEnderpearl.onHit: the thrower is teleported and takes 5 damage. + record("play-pearl", "teleported " + pearlTeleports + " time(s), health " + pearlHealth); return; } } @@ -677,6 +784,13 @@ public final class VanillaProtocolClient { record("error", "timed out in play, step " + step + ", chunks " + chunks.size()); } + /** The death screen's Respawn button: true, the respawn being under way. */ + private boolean respawn() throws Exception { + send(new net.minecraft.network.protocol.game.ServerboundClientCommandPacket( + net.minecraft.network.protocol.game.ServerboundClientCommandPacket.Action.PERFORM_RESPAWN)); + return true; + } + /** * A run of the same packet: counted, but for the entities removed with the dead player * (which mobs are tracked near the spawn at that moment varies). @@ -685,6 +799,19 @@ public final class VanillaProtocolClient { return type + (run > 1 && !type.equals("minecraft:remove_entities") ? " x" + run : ""); } + /** ServerboundUseItemOnPacket on the top of the block under the player. */ + private void useOn(PositionMoveRotation at, net.minecraft.world.InteractionHand hand, int sequence) throws Exception { + net.minecraft.core.BlockPos ground = net.minecraft.core.BlockPos.containing(at.position()).below(); + send(new net.minecraft.network.protocol.game.ServerboundUseItemOnPacket(hand, + new net.minecraft.world.phys.BlockHitResult(net.minecraft.world.phys.Vec3.atCenterOf(ground).add(0.0, 0.5, 0.0), + net.minecraft.core.Direction.UP, ground, false), sequence)); + } + + /** ServerboundUseItemPacket, looking where the player looks. */ + private void use(PositionMoveRotation at, net.minecraft.world.InteractionHand hand, int sequence) throws Exception { + send(new net.minecraft.network.protocol.game.ServerboundUseItemPacket(hand, sequence, at.yRot(), at.xRot())); + } + /** * A packet after the respawn request, as compared: entity ids and periodic or streamed * packets (time, keep-alive, chunk data and light, whose batching follows each server's @@ -905,8 +1032,12 @@ public final class VanillaProtocolClient { } private static Object field(Object owner, String name) { + return field(owner, owner.getClass(), name); + } + + private static Object field(Object owner, Class<?> declaring, String name) { try { - var f = owner.getClass().getDeclaredField(name); + var f = declaring.getDeclaredField(name); f.setAccessible(true); return f.get(owner); } catch (ReflectiveOperationException e) { diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaTooltipDisplayOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaTooltipDisplayOracle.java new file mode 100644 index 000000000..c31dce98c --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaTooltipDisplayOracle.java @@ -0,0 +1,36 @@ +package dev.horizonlab.parity; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.LinkedHashSet; +import net.minecraft.core.component.DataComponentType; +import net.minecraft.core.component.DataComponents; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraft.world.item.TooltipFlag; +import net.minecraft.world.item.component.TooltipDisplay; + +/** The name and attribute lines returned for a diamond sword's tooltip. */ +public final class VanillaTooltipDisplayOracle { + public static void main(String[] args) throws Exception { + ParityData.init(); + var context = Item.TooltipContext.of(ParityData.lookup); + var output = new ArrayList<String>(); + for (String line : Files.readAllLines(Path.of(args[0]))) { + String[] fields = line.split("\t"); + boolean hideTooltip = Boolean.parseBoolean(fields[0]); + boolean creative = Boolean.parseBoolean(fields[1]); + boolean hideAttributes = Boolean.parseBoolean(fields[2]); + var hidden = new LinkedHashSet<DataComponentType<?>>(); + if (hideAttributes) hidden.add(DataComponents.ATTRIBUTE_MODIFIERS); + var stack = new ItemStack(Items.DIAMOND_SWORD, 1); + stack.set(DataComponents.TOOLTIP_DISPLAY, new TooltipDisplay(hideTooltip, hidden)); + TooltipFlag flag = creative ? TooltipFlag.Default.NORMAL.asCreative() : TooltipFlag.Default.NORMAL; + var tooltip = stack.getTooltipLines(context, null, flag); + output.add((!tooltip.isEmpty()) + "\t" + (tooltip.size() > 1)); + } + Files.write(Path.of(args[1]), output); + } +} diff --git a/packaging/server/Dockerfile b/packaging/server/Dockerfile index bbb74ba26..5ae395205 100644 --- a/packaging/server/Dockerfile +++ b/packaging/server/Dockerfile @@ -13,20 +13,15 @@ # start writes /data/server.toml from HORIZON_* (EULA, WORLD, SEED, PORT, WHITELIST, PLAYERS, OP); later # starts use the file. Manage it with `docker exec horizon horizon-server status|cmd|backup|logs ...`. FROM rust:1-slim-bookworm AS build -RUN apt-get update \ - && apt-get install -y --no-install-recommends pkg-config libasound2-dev libudev-dev \ - && rm -rf /var/lib/apt/lists/* WORKDIR /src COPY . . -RUN cargo build --release --locked -p horizon-client --bin horizon-client -p horizon-server --bin horizon-server \ - && mkdir /out && cp target/release/horizon-client target/release/horizon-server /out/ +# The server build of the game (no sound, no controllers: it needs neither ALSA nor udev) as horizon-engine. +RUN cargo build --release --locked -p horizon-client --no-default-features --bin horizon-client \ + && cargo build --release --locked -p horizon-server --bin horizon-server \ + && mkdir /out && cp target/release/horizon-client /out/horizon-engine && cp target/release/horizon-server /out/ FROM debian:bookworm-slim -# The game links ALSA and udev even when it runs headless with no sound. -RUN apt-get update \ - && apt-get install -y --no-install-recommends libasound2 libudev1 \ - && rm -rf /var/lib/apt/lists/* \ - && useradd --system --home-dir /data --shell /usr/sbin/nologin horizon \ +RUN useradd --system --home-dir /data --shell /usr/sbin/nologin horizon \ && mkdir -p /data /opt/horizon/.local/horizon-client \ && chown horizon:horizon /data COPY --from=build /out/ /opt/horizon/ diff --git a/packaging/server/install.sh b/packaging/server/install.sh index 221fcf0a8..eed312c74 100755 --- a/packaging/server/install.sh +++ b/packaging/server/install.sh @@ -1,9 +1,11 @@ #!/bin/sh # Installs horizon-server (docs/server.md) on Linux or macOS: the latest Horizon release for this computer, -# downloaded from GitHub Releases and checked against the SHA-256 GitHub publishes for it. +# downloaded from GitHub Releases and checked against the SHA-256 GitHub publishes for it. On Linux that is +# the server build (horizon-server-linux-x86_64.tar.gz), which needs neither ALSA nor udev. # # curl -fsSL https://raw.githubusercontent.com/ludovic111/horizon/main/packaging/server/install.sh | sh # curl -fsSL .../install.sh | sh -s -- --version v0.5.0 --dir /opt/horizon --bin /usr/local/bin +# sh install.sh --archive horizon-server-linux-x86_64.tar.gz --version mybuild (an archive you have) # # It needs curl, tar or unzip, and sha256sum or shasum; it never asks for sudo (pick folders you can write). # Releases go to <dir>/releases/<version>, <dir>/current points at the one in use, and <bin>/horizon-server @@ -15,6 +17,7 @@ REPO=ludovic111/horizon VERSION=latest DIR="${XDG_DATA_HOME:-$HOME/.local/share}/horizon" BIN="$HOME/.local/bin" +ARCHIVE= die() { echo "horizon install: $*" >&2; exit 1; } @@ -23,45 +26,58 @@ while [ $# -gt 0 ]; do --version) [ $# -ge 2 ] || die "--version needs a tag"; VERSION=$2; shift 2 ;; --dir) [ $# -ge 2 ] || die "--dir needs a folder"; DIR=$2; shift 2 ;; --bin) [ $# -ge 2 ] || die "--bin needs a folder"; BIN=$2; shift 2 ;; - -h|--help) sed -n '2,12p' "$0" 2>/dev/null || true; exit 0 ;; + --archive) [ $# -ge 2 ] || die "--archive needs a file"; ARCHIVE=$2; shift 2 ;; + -h|--help) sed -n '2,14p' "$0" 2>/dev/null || true; exit 0 ;; *) die "unknown option $1" ;; esac done +# The archives to look for, in order: on Linux the server build, else (releases before it) the full game. case "$(uname -s)-$(uname -m)" in - Linux-x86_64) ASSET=horizon-linux-x86_64.tar.gz ;; - Darwin-arm64) ASSET=horizon-macos-arm64.zip ;; + Linux-x86_64) ASSETS="horizon-server-linux-x86_64.tar.gz horizon-linux-x86_64.tar.gz" ;; + Darwin-arm64) ASSETS=horizon-macos-arm64.zip ;; *) die "no Horizon release for $(uname -s) $(uname -m) yet: build it from source (docs/server.md)" ;; esac -command -v curl >/dev/null || die "curl is needed" -if command -v sha256sum >/dev/null; then SHA="sha256sum" -elif command -v shasum >/dev/null; then SHA="shasum -a 256" -else die "sha256sum or shasum is needed"; fi - -if [ "$VERSION" = latest ]; then API="https://api.github.com/repos/$REPO/releases/latest" -else API="https://api.github.com/repos/$REPO/releases/tags/$VERSION"; fi -JSON=$(curl -fsSL -H "Accept: application/vnd.github+json" "$API") || die "no release $VERSION on GitHub" - -TAG=$(printf '%s\n' "$JSON" | sed -n 's/^ "tag_name": "\([^"]*\)".*/\1/p' | head -n 1) -[ -n "$TAG" ] || die "could not read the release's tag" -# The asset's download address and digest, from its block in the release's JSON. -FIELDS=$(printf '%s\n' "$JSON" | awk -v asset="$ASSET" ' - /"name": / { line = $0; sub(/.*"name": "/, "", line); sub(/".*/, "", line); current = line } - current == asset && /"digest": / { line = $0; sub(/.*"digest": "sha256:/, "", line); sub(/".*/, "", line); digest = line } - current == asset && /"browser_download_url": / { line = $0; sub(/.*"browser_download_url": "/, "", line); sub(/".*/, "", line); url = line } - END { print url; print digest }') -URL=$(printf '%s\n' "$FIELDS" | sed -n 1p) -DIGEST=$(printf '%s\n' "$FIELDS" | sed -n 2p) -[ -n "$URL" ] || die "release $TAG has no $ASSET" -[ -n "$DIGEST" ] || die "release $TAG publishes no SHA-256 for $ASSET" - TMP=$(mktemp -d) trap 'rm -rf "$TMP"' EXIT -echo "Downloading Horizon $TAG ($ASSET)..." -curl -fL --progress-bar -o "$TMP/$ASSET" "$URL" -GOT=$($SHA "$TMP/$ASSET" | cut -d' ' -f1) -[ "$GOT" = "$DIGEST" ] || die "$ASSET doesn't match its SHA-256 (got $GOT, expected $DIGEST): not installed" +if [ -n "$ARCHIVE" ]; then + # An archive already here (a build of your own, or one fetched by hand): checking it is up to you. + [ -f "$ARCHIVE" ] || die "no archive $ARCHIVE" + if [ "$VERSION" = latest ]; then TAG=local; else TAG=$VERSION; fi + ASSET=$(basename "$ARCHIVE") + cp "$ARCHIVE" "$TMP/$ASSET" +else + command -v curl >/dev/null || die "curl is needed" + if command -v sha256sum >/dev/null; then SHA="sha256sum" + elif command -v shasum >/dev/null; then SHA="shasum -a 256" + else die "sha256sum or shasum is needed"; fi + + if [ "$VERSION" = latest ]; then API="https://api.github.com/repos/$REPO/releases/latest" + else API="https://api.github.com/repos/$REPO/releases/tags/$VERSION"; fi + JSON=$(curl -fsSL -H "Accept: application/vnd.github+json" "$API") || die "no release $VERSION on GitHub" + + TAG=$(printf '%s\n' "$JSON" | sed -n 's/^ "tag_name": "\([^"]*\)".*/\1/p' | head -n 1) + [ -n "$TAG" ] || die "could not read the release's tag" + # The first asset the release has: its download address and digest, from its block in the release's JSON. + for ASSET in $ASSETS; do + FIELDS=$(printf '%s\n' "$JSON" | awk -v asset="$ASSET" ' + /"name": / { line = $0; sub(/.*"name": "/, "", line); sub(/".*/, "", line); current = line } + current == asset && /"digest": / { line = $0; sub(/.*"digest": "sha256:/, "", line); sub(/".*/, "", line); digest = line } + current == asset && /"browser_download_url": / { line = $0; sub(/.*"browser_download_url": "/, "", line); sub(/".*/, "", line); url = line } + END { print url; print digest }') + URL=$(printf '%s\n' "$FIELDS" | sed -n 1p) + DIGEST=$(printf '%s\n' "$FIELDS" | sed -n 2p) + [ -z "$URL" ] || break + done + [ -n "$URL" ] || die "release $TAG has none of $ASSETS" + [ -n "$DIGEST" ] || die "release $TAG publishes no SHA-256 for $ASSET" + + echo "Downloading Horizon $TAG ($ASSET)..." + curl -fL --progress-bar -o "$TMP/$ASSET" "$URL" + GOT=$($SHA "$TMP/$ASSET" | cut -d' ' -f1) + [ "$GOT" = "$DIGEST" ] || die "$ASSET doesn't match its SHA-256 (got $GOT, expected $DIGEST): not installed" +fi mkdir -p "$TMP/x" case "$ASSET" in @@ -86,6 +102,18 @@ for tool in horizon-server horizon-prepare; do done echo "Installed Horizon $TAG in $RELEASE; horizon-server and horizon-prepare are in $BIN." +# The full game (releases before the server build) links ALSA and udev; minimal Linux images often lack them. +if [ "$(uname -s)" = Linux ] && [ ! -e "$RELEASE/horizon-engine" ] && command -v ldd >/dev/null; then + MISSING=$(ldd "$RELEASE/horizon-client" 2>/dev/null | awk '/not found/ { printf "%s ", $1 }') + if [ -n "$MISSING" ]; then + echo "The game needs libraries this system doesn't have: $MISSING" + echo "Install them before starting a server:" + echo " Debian, Ubuntu: sudo apt install libasound2 libudev1 (Ubuntu 24.04 and later: libasound2t64)" + echo " Fedora, RHEL: sudo dnf install alsa-lib systemd-libs" + echo " Arch: sudo pacman -S alsa-lib systemd-libs" + echo " NixOS: enable programs.nix-ld with alsa-lib and udev in its libraries" + fi +fi case ":$PATH:" in *":$BIN:"*) ;; *) echo "Add $BIN to your PATH, for example: echo 'export PATH=\"$BIN:\$PATH\"' >> ~/.profile" ;; diff --git a/tests/test_parity_runner.py b/tests/test_parity_runner.py index 78716787a..ffec8d3d5 100644 --- a/tests/test_parity_runner.py +++ b/tests/test_parity_runner.py @@ -1,4 +1,5 @@ from pathlib import Path +import os import tempfile import unittest from unittest import mock @@ -151,6 +152,7 @@ class ShardTests(unittest.TestCase): self.assertEqual(parity.base_name("x#2"), "x") +@unittest.skipIf(os.name == "nt", "oracle PATH shimming uses Unix executable symlink semantics") class OracleReuseTests(unittest.TestCase): """tools/parity_shim.py replays a run only when its inputs and outputs are as recorded.""" diff --git a/tests/test_server_bench.py b/tests/test_server_bench.py new file mode 100644 index 000000000..64fe8960d --- /dev/null +++ b/tests/test_server_bench.py @@ -0,0 +1,49 @@ +import unittest + +import re + +from tools.server_bench import DONE, TICK_AVERAGE, TICK_PERCENTILES, work + + +def run(server, bots, sent, new, waited=0.0, behaviour="mixed"): + return {"server": server, "behaviour": behaviour, "bots": bots, "chunks_per_second": sent, + "unique_per_second": new, "waited_ticks_per_second": waited} + + +class ServerBenchWorkTests(unittest.TestCase): + def test_shares_of_the_busiest_server(self): + [row] = work([run("horizon", 10, 98.2, 36.5), run("vanilla", 10, 24.3, 19.1, 12.0)]) + self.assertFalse(row["equal"]) + self.assertEqual(row["servers"]["horizon"]["chunks_per_second"], {"value": 98.2, "share": 1.0}) + self.assertEqual(row["servers"]["vanilla"]["chunks_per_second"]["share"], 0.247) + self.assertEqual(row["servers"]["vanilla"]["waited_ticks_per_second"], 12.0) + + def test_equal_within_ten_percent_or_a_chunk(self): + [row] = work([run("horizon", 25, 236, 28.9), run("vanilla", 25, 252, 28.9)]) + self.assertTrue(row["equal"]) + # One bot: 0.3 against 1.1 new chunks a second is within a chunk. + [row] = work([run("horizon", 1, 9.9, 0.3), run("vanilla", 1, 9.9, 1.1)]) + self.assertTrue(row["equal"]) + + def test_only_counts_run_on_several_servers(self): + failed = dict(run("vanilla", 50, 0, 0), error="watchdog") + self.assertEqual(work([run("horizon", 50, 450, 10), failed, run("horizon", 1, 9, 0.3)]), []) + + +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")) diff --git a/tests/test_world_input_capture.py b/tests/test_world_input_capture.py index c7fc4629f..5b3c71f45 100644 --- a/tests/test_world_input_capture.py +++ b/tests/test_world_input_capture.py @@ -47,7 +47,7 @@ class WorldInputCaptureTests(unittest.TestCase): fixture = r''' use std::cell::Cell; #[derive(Clone, Copy, PartialEq)] -enum Screen { Playing, Paused, Inventory } +enum Screen { Playing, Paused, Inventory, Chat } enum CursorGrabMode { Locked, Confined, None } #[derive(Default)] struct Window { grabs: Cell<u32>, visible: Cell<bool>, fail: bool } @@ -63,17 +63,17 @@ impl Window { #[derive(Default)] struct Mouse { ignore_first: bool } impl Mouse { fn set_ignore_first_move(&mut self) { self.ignore_first = true; } } -struct Remote { chat_input: Option<()> } +struct Remote; struct Game { focused: bool, agent_pointer: bool, window: Option<Window>, mouse: Mouse, captured: bool, title: Option<()>, screen: Screen, remote: Option<Remote>, - mining: bool, + mining: bool, lan_acting: Option<u32>, } impl Default for Game { fn default() -> Self { Self { focused: true, agent_pointer: false, window: Some(Window::default()), mouse: Mouse::default(), captured: false, title: None, - screen: Screen::Playing, remote: None, mining: false } + screen: Screen::Playing, remote: None, mining: false, lan_acting: None } } } impl Game { @@ -84,7 +84,7 @@ fn main() { // New and existing worlds, returning to title, and reopening a world. for remote in [false, true] { let mut game = Game::default(); - if remote { game.remote = Some(Remote { chat_input: None }); } + if remote { game.remote = Some(Remote); } for _ in 0..3 { game.title = Some(()); game.capture(false); @@ -128,11 +128,14 @@ fn main() { assert!(game.captured); } let mut game = Game::default(); - game.remote = Some(Remote { chat_input: Some(()) }); + // On a server the chat is ChatScreen (#708): loading never grabs over it. + game.remote = Some(Remote); + game.screen = Screen::Chat; game.capture_loaded_world(); assert!(!game.captured); assert_eq!(game.window.as_ref().unwrap().grabs.get(), 0); game.remote = None; + game.screen = Screen::Playing; game.window.as_mut().unwrap().fail = true; game.capture_loaded_world(); assert!(!game.captured); diff --git a/third_party/wgpu-hal/Cargo.toml b/third_party/wgpu-hal/Cargo.toml new file mode 100644 index 000000000..fac5bca54 --- /dev/null +++ b/third_party/wgpu-hal/Cargo.toml @@ -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)", +] diff --git a/third_party/wgpu-hal/LICENSE.APACHE b/third_party/wgpu-hal/LICENSE.APACHE new file mode 100644 index 000000000..d9a10c0d8 --- /dev/null +++ b/third_party/wgpu-hal/LICENSE.APACHE @@ -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 diff --git a/third_party/wgpu-hal/LICENSE.MIT b/third_party/wgpu-hal/LICENSE.MIT new file mode 100644 index 000000000..8d02e4dbd --- /dev/null +++ b/third_party/wgpu-hal/LICENSE.MIT @@ -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. diff --git a/third_party/wgpu-hal/README.md b/third_party/wgpu-hal/README.md new file mode 100644 index 000000000..1ea2f44a3 --- /dev/null +++ b/third_party/wgpu-hal/README.md @@ -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 diff --git a/third_party/wgpu-hal/build.rs b/third_party/wgpu-hal/build.rs new file mode 100644 index 000000000..7430ccba2 --- /dev/null +++ b/third_party/wgpu-hal/build.rs @@ -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" } + } +} diff --git a/third_party/wgpu-hal/src/auxil/dxgi/conv.rs b/third_party/wgpu-hal/src/auxil/dxgi/conv.rs new file mode 100644 index 000000000..c9e4dbc2c --- /dev/null +++ b/third_party/wgpu-hal/src/auxil/dxgi/conv.rs @@ -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 + } + } +} diff --git a/third_party/wgpu-hal/src/auxil/dxgi/exception.rs b/third_party/wgpu-hal/src/auxil/dxgi/exception.rs new file mode 100644 index 000000000..ac7d55d8f --- /dev/null +++ b/third_party/wgpu-hal/src/auxil/dxgi/exception.rs @@ -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 +} diff --git a/third_party/wgpu-hal/src/auxil/dxgi/factory.rs b/third_party/wgpu-hal/src/auxil/dxgi/factory.rs new file mode 100644 index 000000000..ac050bcd3 --- /dev/null +++ b/third_party/wgpu-hal/src/auxil/dxgi/factory.rs @@ -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))) +} diff --git a/third_party/wgpu-hal/src/auxil/dxgi/hdr.rs b/third_party/wgpu-hal/src/auxil/dxgi/hdr.rs new file mode 100644 index 000000000..3d810ee63 --- /dev/null +++ b/third_party/wgpu-hal/src/auxil/dxgi/hdr.rs @@ -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 +} diff --git a/third_party/wgpu-hal/src/auxil/dxgi/mod.rs b/third_party/wgpu-hal/src/auxil/dxgi/mod.rs new file mode 100644 index 000000000..7a4bcc759 --- /dev/null +++ b/third_party/wgpu-hal/src/auxil/dxgi/mod.rs @@ -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; diff --git a/third_party/wgpu-hal/src/auxil/dxgi/name.rs b/third_party/wgpu-hal/src/auxil/dxgi/name.rs new file mode 100644 index 000000000..d120e4ed1 --- /dev/null +++ b/third_party/wgpu-hal/src/auxil/dxgi/name.rs @@ -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") } + } +} diff --git a/third_party/wgpu-hal/src/auxil/dxgi/result.rs b/third_party/wgpu-hal/src/auxil/dxgi/result.rs new file mode 100644 index 000000000..f3d77e27b --- /dev/null +++ b/third_party/wgpu-hal/src/auxil/dxgi/result.rs @@ -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 + } + } + }) + } +} diff --git a/third_party/wgpu-hal/src/auxil/dxgi/time.rs b/third_party/wgpu-hal/src/auxil/dxgi/time.rs new file mode 100644 index 000000000..b74061246 --- /dev/null +++ b/third_party/wgpu-hal/src/auxil/dxgi/time.rs @@ -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 + } + } + } +} diff --git a/third_party/wgpu-hal/src/auxil/mod.rs b/third_party/wgpu-hal/src/auxil/mod.rs new file mode 100644 index 000000000..599b7d0fa --- /dev/null +++ b/third_party/wgpu-hal/src/auxil/mod.rs @@ -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); + } + } +} diff --git a/third_party/wgpu-hal/src/auxil/renderdoc.rs b/third_party/wgpu-hal/src/auxil/renderdoc.rs new file mode 100644 index 000000000..52fa2b0bd --- /dev/null +++ b/third_party/wgpu-hal/src/auxil/renderdoc.rs @@ -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}") + } + }; + } +} diff --git a/third_party/wgpu-hal/src/dx12/adapter.rs b/third_party/wgpu-hal/src/dx12/adapter.rs new file mode 100644 index 000000000..621eea4ca --- /dev/null +++ b/third_party/wgpu-hal/src/dx12/adapter.rs @@ -0,0 +1,1546 @@ +use alloc::{string::String, sync::Arc, vec::Vec}; +use core::{ptr, sync::atomic::AtomicU64}; +use std::thread; + +use parking_lot::Mutex; +use windows::{ + core::Interface as _, + Win32::{ + Devices::DeviceAndDriverInstallation::{ + SetupDiDestroyDeviceInfoList, SetupDiEnumDeviceInfo, SetupDiGetClassDevsW, + SetupDiGetDeviceRegistryPropertyW, DIGCF_PRESENT, GUID_DEVCLASS_DISPLAY, HDEVINFO, + SPDRP_ADDRESS, SPDRP_BUSNUMBER, SPDRP_HARDWAREID, SP_DEVINFO_DATA, + }, + Foundation::{GetLastError, ERROR_NO_MORE_ITEMS}, + Graphics::{Direct3D, Direct3D12, Dxgi}, + UI::WindowsAndMessaging, + }, +}; + +use super::D3D12Lib; +use crate::{ + auxil::{ + self, + dxgi::{factory::DxgiAdapter, result::HResult}, + }, + dx12::{ + dcomp::DCompLib, device_creation::DeviceFactory, shader_compilation, FeatureLevel, + ShaderModel, SurfaceTarget, + }, +}; + +impl Drop for super::Adapter { + fn drop(&mut self) { + // Debug tracking alive objects + if !thread::panicking() + && self + .private_caps + .instance_flags + .contains(wgt::InstanceFlags::VALIDATION) + { + unsafe { + self.report_live_objects(); + } + } + } +} + +impl super::Adapter { + pub unsafe fn report_live_objects(&self) { + if let Ok(debug_device) = self.raw.cast::<Direct3D12::ID3D12DebugDevice>() { + unsafe { + debug_device.ReportLiveDeviceObjects( + Direct3D12::D3D12_RLDO_SUMMARY | Direct3D12::D3D12_RLDO_IGNORE_INTERNAL, + ) + } + .unwrap() + } + } + + pub fn raw_adapter(&self) -> &DxgiAdapter { + &self.raw + } + + #[allow(clippy::too_many_arguments)] + pub(super) fn expose( + adapter: DxgiAdapter, + library: &Arc<D3D12Lib>, + device_factory: &Arc<DeviceFactory>, + dcomp_lib: &Arc<DCompLib>, + instance_flags: wgt::InstanceFlags, + memory_budget_thresholds: wgt::MemoryBudgetThresholds, + compiler_container: Arc<shader_compilation::CompilerContainer>, + backend_options: wgt::Dx12BackendOptions, + telemetry: Option<crate::Telemetry>, + ) -> Option<crate::ExposedAdapter<super::Api>> { + let desc = unsafe { adapter.GetDesc2() }.unwrap(); + let driver_version = unsafe { adapter.CheckInterfaceSupport(&Dxgi::IDXGIDevice::IID) }; + let driver_version = driver_version + .map(|driver_version| { + let driver_version = driver_version as u64; + [ + (driver_version >> 48) as u16, + (driver_version >> 32) as u16, + (driver_version >> 16) as u16, + driver_version as u16, + ] + }) + .map_err(|e| e.code()); + + // Create the device so that we can get the capabilities. + let res = { + profiling::scope!("ID3D12Device::create_device"); + device_factory.create_device(library, &adapter, Direct3D::D3D_FEATURE_LEVEL_11_0) + }; + if let Some(telemetry) = telemetry { + if let Err(err) = res { + (telemetry.d3d12_expose_adapter)( + &desc, + driver_version, + crate::D3D12ExposeAdapterResult::CreateDeviceError(err), + ); + } + } + let device = res.ok()?; + + profiling::scope!("feature queries"); + + // Detect the highest supported feature level. + let d3d_feature_level = [ + Direct3D::D3D_FEATURE_LEVEL_12_2, + Direct3D::D3D_FEATURE_LEVEL_12_1, + Direct3D::D3D_FEATURE_LEVEL_12_0, + Direct3D::D3D_FEATURE_LEVEL_11_1, + Direct3D::D3D_FEATURE_LEVEL_11_0, + ]; + let mut device_levels = Direct3D12::D3D12_FEATURE_DATA_FEATURE_LEVELS { + NumFeatureLevels: d3d_feature_level.len() as u32, + pFeatureLevelsRequested: d3d_feature_level.as_ptr().cast(), + MaxSupportedFeatureLevel: Default::default(), + }; + unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_FEATURE_LEVELS, + <*mut _>::cast(&mut device_levels), + size_of_val(&device_levels) as u32, + ) + } + .unwrap(); + let max_feature_level = match device_levels.MaxSupportedFeatureLevel { + Direct3D::D3D_FEATURE_LEVEL_11_0 => FeatureLevel::V11_0, + Direct3D::D3D_FEATURE_LEVEL_11_1 => FeatureLevel::V11_1, + Direct3D::D3D_FEATURE_LEVEL_12_0 => FeatureLevel::V12_0, + Direct3D::D3D_FEATURE_LEVEL_12_1 => FeatureLevel::V12_1, + Direct3D::D3D_FEATURE_LEVEL_12_2 => FeatureLevel::V12_2, + fl => { + if let Some(telemetry) = telemetry { + (telemetry.d3d12_expose_adapter)( + &desc, + driver_version, + crate::D3D12ExposeAdapterResult::UnknownFeatureLevel(fl.0), + ); + } + return None; + } + }; + + let device_name = auxil::dxgi::conv::map_adapter_name(desc.Description); + + let mut features_architecture = Direct3D12::D3D12_FEATURE_DATA_ARCHITECTURE::default(); + + unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_ARCHITECTURE, + <*mut _>::cast(&mut features_architecture), + size_of_val(&features_architecture) as u32, + ) + } + .unwrap(); + + let mut features1 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS1::default(); + let hr = unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_D3D12_OPTIONS1, + <*mut _>::cast(&mut features1), + size_of_val(&features1) as u32, + ) + }; + + let mut workarounds = super::Workarounds::default(); + + let is_warp = device_name.contains("Microsoft Basic Render Driver"); + + // WARP uses two different versioning schemes. Versions that ship with windows + // use a version that starts with 10.x.x.x. Versions that ship from Nuget use 1.0.x.x. + // + // As far as we know, this is only an issue on the Nuget versions. + if let Ok(driver_version) = driver_version { + if is_warp && driver_version >= [1, 0, 13, 0] && driver_version[0] < 10 { + workarounds.avoid_shader_debug_info = true; + } + } + + let driver_version_string = { + let driver_version = driver_version.unwrap_or([0, 0, 0, 0]); + format!( + "{}.{}.{}.{}", + driver_version[0], driver_version[1], driver_version[2], driver_version[3] + ) + }; + + let info = wgt::AdapterInfo { + backend: wgt::Backend::Dx12, + name: device_name, + vendor: desc.VendorId, + device: desc.DeviceId, + device_type: if Dxgi::DXGI_ADAPTER_FLAG(desc.Flags as i32) + .contains(Dxgi::DXGI_ADAPTER_FLAG_SOFTWARE) + { + wgt::DeviceType::Cpu + } else if features_architecture.UMA.as_bool() { + wgt::DeviceType::IntegratedGpu + } else { + wgt::DeviceType::DiscreteGpu + }, + device_pci_bus_id: get_adapter_pci_info(desc.VendorId, desc.DeviceId), + driver: driver_version_string, + driver_info: String::new(), + subgroup_min_size: features1.WaveLaneCountMin, + subgroup_max_size: features1.WaveLaneCountMax, + transient_saves_memory: Some(false), + limit_bucket: None, + }; + + let mut options = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS::default(); + unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_D3D12_OPTIONS, + <*mut _>::cast(&mut options), + size_of_val(&options) as u32, + ) + } + .unwrap(); + + /// Resource Binding Tiers: https://learn.microsoft.com/en-us/windows/win32/direct3d12/hardware-support#limits-dependant-on-hardware + #[derive(PartialEq, Eq, PartialOrd, Ord)] + enum ResourceBindingTier { + T1, + T2, + T3, + } + let rbt = match options.ResourceBindingTier { + Direct3D12::D3D12_RESOURCE_BINDING_TIER_1 => ResourceBindingTier::T1, + Direct3D12::D3D12_RESOURCE_BINDING_TIER_2 => ResourceBindingTier::T2, + tier if tier.0 >= Direct3D12::D3D12_RESOURCE_BINDING_TIER_3.0 => { + ResourceBindingTier::T3 + } + other => { + log::debug!("Got zero or negative value for resource binding tier {other:?}"); + ResourceBindingTier::T1 + } + }; + + if rbt == ResourceBindingTier::T1 { + if let Some(telemetry) = telemetry { + (telemetry.d3d12_expose_adapter)( + &desc, + driver_version, + crate::D3D12ExposeAdapterResult::ResourceBindingTier2Requirement, + ); + } + // We require Tier 2 or higher for the ability to make samplers bindless in all cases. + return None; + } + + let _depth_bounds_test_supported = { + let mut features2 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS2::default(); + unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_D3D12_OPTIONS2, + <*mut _>::cast(&mut features2), + size_of_val(&features2) as u32, + ) + } + .is_ok() + && features2.DepthBoundsTestSupported.as_bool() + }; + + let (casting_fully_typed_format_supported, view_instancing) = { + let mut features3 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS3::default(); + if unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_D3D12_OPTIONS3, + <*mut _>::cast(&mut features3), + size_of_val(&features3) as u32, + ) + } + .is_ok() + { + ( + features3.CastingFullyTypedFormatSupported.as_bool(), + features3.ViewInstancingTier.0 >= Direct3D12::D3D12_VIEW_INSTANCING_TIER_1.0, + ) + } else { + (false, false) + } + }; + + let heap_create_not_zeroed = { + // For D3D12_HEAP_FLAG_CREATE_NOT_ZEROED we just need to + // make sure that options7 can be queried. See also: + // https://devblogs.microsoft.com/directx/coming-to-directx-12-more-control-over-memory-allocation/ + let mut features7 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS7::default(); + unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_D3D12_OPTIONS7, + <*mut _>::cast(&mut features7), + size_of_val(&features7) as u32, + ) + } + .is_ok() + }; + + let unrestricted_buffer_texture_copy_pitch_supported = { + let mut features13 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS13::default(); + unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_D3D12_OPTIONS13, + <*mut _>::cast(&mut features13), + size_of_val(&features13) as u32, + ) + } + .is_ok() + && features13 + .UnrestrictedBufferTextureCopyPitchSupported + .as_bool() + }; + + let mut max_sampler_descriptor_heap_size = + Direct3D12::D3D12_MAX_SHADER_VISIBLE_SAMPLER_HEAP_SIZE; + { + let mut features19 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS19::default(); + let res = unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_D3D12_OPTIONS19, + <*mut _>::cast(&mut features19), + size_of_val(&features19) as u32, + ) + }; + + // Sometimes on Windows 11 23H2, the function returns success, even though the runtime + // does not know about `Options19`. This can cause this number to be 0 as the structure isn't written to. + // This value is nonsense and creating zero-sized sampler heaps can cause drivers to explode. + // As as we're guaranteed 2048 anyway, we make sure this value is not under 2048. + // + // https://github.com/gfx-rs/wgpu/issues/7053 + let is_ok = res.is_ok(); + let is_above_minimum = features19.MaxSamplerDescriptorHeapSize + > Direct3D12::D3D12_MAX_SHADER_VISIBLE_SAMPLER_HEAP_SIZE; + if is_ok && is_above_minimum { + max_sampler_descriptor_heap_size = features19.MaxSamplerDescriptorHeapSize; + } + }; + + let mut shader_models_after_5_1 = [ + Direct3D12::D3D_SHADER_MODEL_6_9, + Direct3D12::D3D_SHADER_MODEL_6_8, + Direct3D12::D3D_SHADER_MODEL_6_7, + Direct3D12::D3D_SHADER_MODEL_6_6, + Direct3D12::D3D_SHADER_MODEL_6_5, + Direct3D12::D3D_SHADER_MODEL_6_4, + Direct3D12::D3D_SHADER_MODEL_6_3, + Direct3D12::D3D_SHADER_MODEL_6_2, + Direct3D12::D3D_SHADER_MODEL_6_1, + Direct3D12::D3D_SHADER_MODEL_6_0, + ] + .iter(); + let max_device_shader_model = loop { + if let Some(&sm) = shader_models_after_5_1.next() { + let mut sm = Direct3D12::D3D12_FEATURE_DATA_SHADER_MODEL { + HighestShaderModel: sm, + }; + if unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_SHADER_MODEL, + <*mut _>::cast(&mut sm), + size_of_val(&sm) as u32, + ) + } + .is_ok() + { + break match sm.HighestShaderModel { + Direct3D12::D3D_SHADER_MODEL_5_1 => ShaderModel::V5_1, + Direct3D12::D3D_SHADER_MODEL_6_0 => ShaderModel::V6_0, + Direct3D12::D3D_SHADER_MODEL_6_1 => ShaderModel::V6_1, + Direct3D12::D3D_SHADER_MODEL_6_2 => ShaderModel::V6_2, + Direct3D12::D3D_SHADER_MODEL_6_3 => ShaderModel::V6_3, + Direct3D12::D3D_SHADER_MODEL_6_4 => ShaderModel::V6_4, + Direct3D12::D3D_SHADER_MODEL_6_5 => ShaderModel::V6_5, + Direct3D12::D3D_SHADER_MODEL_6_6 => ShaderModel::V6_6, + Direct3D12::D3D_SHADER_MODEL_6_7 => ShaderModel::V6_7, + Direct3D12::D3D_SHADER_MODEL_6_8 => ShaderModel::V6_8, + Direct3D12::D3D_SHADER_MODEL_6_9 => ShaderModel::V6_9, + _ => unreachable!(), + }; + } + } else { + break ShaderModel::V5_1; + } + }; + + let wgt_shader_model = backend_options + .force_shader_model + .get() + .or(compiler_container.max_shader_model()); + + let shader_model = if let Some(max_shader_model) = wgt_shader_model { + let max_dxc_shader_model = match max_shader_model { + wgt::DxcShaderModel::V6_0 => ShaderModel::V6_0, + wgt::DxcShaderModel::V6_1 => ShaderModel::V6_1, + wgt::DxcShaderModel::V6_2 => ShaderModel::V6_2, + wgt::DxcShaderModel::V6_3 => ShaderModel::V6_3, + wgt::DxcShaderModel::V6_4 => ShaderModel::V6_4, + wgt::DxcShaderModel::V6_5 => ShaderModel::V6_5, + wgt::DxcShaderModel::V6_6 => ShaderModel::V6_6, + wgt::DxcShaderModel::V6_7 => ShaderModel::V6_7, + wgt::DxcShaderModel::V6_8 => ShaderModel::V6_8, + wgt::DxcShaderModel::V6_9 => ShaderModel::V6_9, + }; + + let shader_model = max_device_shader_model.min(max_dxc_shader_model); + + match shader_model { + ShaderModel::V5_1 => { + if let Some(telemetry) = telemetry { + (telemetry.d3d12_expose_adapter)( + &desc, + driver_version, + crate::D3D12ExposeAdapterResult::ShaderModel6Requirement, + ); + } + // don't expose this adapter if it doesn't support DXIL + return None; + } + ShaderModel::V6_0 => naga::back::hlsl::ShaderModel::V6_0, + ShaderModel::V6_1 => naga::back::hlsl::ShaderModel::V6_1, + ShaderModel::V6_2 => naga::back::hlsl::ShaderModel::V6_2, + ShaderModel::V6_3 => naga::back::hlsl::ShaderModel::V6_3, + ShaderModel::V6_4 => naga::back::hlsl::ShaderModel::V6_4, + ShaderModel::V6_5 => naga::back::hlsl::ShaderModel::V6_5, + ShaderModel::V6_6 => naga::back::hlsl::ShaderModel::V6_6, + ShaderModel::V6_7 => naga::back::hlsl::ShaderModel::V6_7, + ShaderModel::V6_8 => naga::back::hlsl::ShaderModel::V6_8, + ShaderModel::V6_9 => naga::back::hlsl::ShaderModel::V6_9, + } + } else { + naga::back::hlsl::ShaderModel::V5_1 + }; + let private_caps = super::PrivateCapabilities { + instance_flags, + workarounds, + heterogeneous_resource_heaps: options.ResourceHeapTier + != Direct3D12::D3D12_RESOURCE_HEAP_TIER_1, + memory_architecture: if features_architecture.UMA.as_bool() { + super::MemoryArchitecture::Unified { + cache_coherent: features_architecture.CacheCoherentUMA.as_bool(), + } + } else { + super::MemoryArchitecture::NonUnified + }, + heap_create_not_zeroed, + casting_fully_typed_format_supported, + // See https://github.com/gfx-rs/wgpu/issues/3552 + suballocation_supported: !info.name.contains("Iris(R) Xe"), + shader_model, + max_sampler_descriptor_heap_size, + unrestricted_buffer_texture_copy_pitch_supported, + }; + + // these should always be available on d3d12 + let mut features = wgt::Features::empty() + | wgt::Features::DEPTH_CLIP_CONTROL + | wgt::Features::DEPTH32FLOAT_STENCIL8 + | wgt::Features::INDIRECT_FIRST_INSTANCE + | wgt::Features::MAPPABLE_PRIMARY_BUFFERS + | wgt::Features::MULTI_DRAW_INDIRECT_COUNT + | wgt::Features::ADDRESS_MODE_CLAMP_TO_BORDER + | wgt::Features::ADDRESS_MODE_CLAMP_TO_ZERO + | wgt::Features::POLYGON_MODE_LINE + | wgt::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES + | wgt::Features::TIMESTAMP_QUERY + | wgt::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS + | wgt::Features::TIMESTAMP_QUERY_INSIDE_PASSES + | wgt::Features::TEXTURE_COMPRESSION_BC + | wgt::Features::TEXTURE_COMPRESSION_BC_SLICED_3D + | wgt::Features::CLEAR_TEXTURE + | wgt::Features::TEXTURE_FORMAT_16BIT_NORM + | wgt::Features::IMMEDIATES + | wgt::Features::PRIMITIVE_INDEX + | wgt::Features::RG11B10UFLOAT_RENDERABLE + | wgt::Features::DUAL_SOURCE_BLENDING + | wgt::Features::TEXTURE_FORMAT_NV12 + | wgt::Features::FLOAT32_FILTERABLE + | wgt::Features::FLOAT32_BLENDABLE + | wgt::Features::TEXTURE_ATOMIC + | wgt::Features::PASSTHROUGH_SHADERS + | wgt::Features::EXTERNAL_TEXTURE + | wgt::Features::MEMORY_DECORATION_COHERENT; + + //TODO: in order to expose this, we need to run a compute shader + // that extract the necessary statistics out of the D3D12 result. + // Alternatively, we could allocate a buffer for the query set, + // write the results there, and issue a bunch of copy commands. + //| wgt::Features::PIPELINE_STATISTICS_QUERY + + if max_feature_level >= FeatureLevel::V11_1 { + features |= wgt::Features::VERTEX_WRITABLE_STORAGE; + } + + features.set( + wgt::Features::CONSERVATIVE_RASTERIZATION, + options.ConservativeRasterizationTier + != Direct3D12::D3D12_CONSERVATIVE_RASTERIZATION_TIER_NOT_SUPPORTED, + ); + + features.set( + wgt::Features::TEXTURE_BINDING_ARRAY + | wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY + | wgt::Features::STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING + | wgt::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING + // See note below the table https://learn.microsoft.com/en-us/windows/win32/direct3d12/hardware-support + | wgt::Features::PARTIALLY_BOUND_BINDING_ARRAY, + shader_model >= naga::back::hlsl::ShaderModel::V5_1 && rbt >= ResourceBindingTier::T3, + ); + + let bgra8unorm_storage_supported = { + let mut bgra8unorm_info = Direct3D12::D3D12_FEATURE_DATA_FORMAT_SUPPORT { + Format: Dxgi::Common::DXGI_FORMAT_B8G8R8A8_UNORM, + ..Default::default() + }; + let hr = unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_FORMAT_SUPPORT, + <*mut _>::cast(&mut bgra8unorm_info), + size_of_val(&bgra8unorm_info) as u32, + ) + }; + hr.is_ok() + && bgra8unorm_info + .Support2 + .contains(Direct3D12::D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE) + }; + features.set( + wgt::Features::BGRA8UNORM_STORAGE, + bgra8unorm_storage_supported, + ); + + let p010_format_supported = { + let mut p010_info = Direct3D12::D3D12_FEATURE_DATA_FORMAT_SUPPORT { + Format: Dxgi::Common::DXGI_FORMAT_P010, + ..Default::default() + }; + let hr = unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_FORMAT_SUPPORT, + <*mut _>::cast(&mut p010_info), + size_of_val(&p010_info) as u32, + ) + }; + if hr.is_ok() { + let supports_texture2d = p010_info + .Support1 + .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_TEXTURE2D); + let supports_shader_load = p010_info + .Support1 + .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_SHADER_LOAD); + let supports_shader_sample = p010_info + .Support1 + .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE); + supports_texture2d && supports_shader_load && supports_shader_sample + } else { + false + } + }; + features.set(wgt::Features::TEXTURE_FORMAT_P010, p010_format_supported); + + features.set( + wgt::Features::SHADER_INT64, + shader_model >= naga::back::hlsl::ShaderModel::V6_0 + && hr.is_ok() + && features1.Int64ShaderOps.as_bool(), + ); + + let float16_supported = { + let mut features4 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS4::default(); + let hr = unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_D3D12_OPTIONS4, // https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_feature#syntax + ptr::from_mut(&mut features4).cast(), + size_of::<Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS4>() as _, + ) + }; + hr.is_ok() && features4.Native16BitShaderOpsSupported.as_bool() + }; + + features.set( + wgt::Features::SHADER_F16, + shader_model >= naga::back::hlsl::ShaderModel::V6_2 && float16_supported, + ); + features.set( + wgt::Features::SHADER_I16, + shader_model >= naga::back::hlsl::ShaderModel::V6_2 && float16_supported, + ); + + features.set( + wgt::Features::SUBGROUP, + shader_model >= naga::back::hlsl::ShaderModel::V6_0 + && hr.is_ok() + && features1.WaveOps.as_bool(), + ); + let mut features5 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS5::default(); + let has_features5 = unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_D3D12_OPTIONS5, + <*mut _>::cast(&mut features5), + size_of_val(&features5) as u32, + ) + } + .is_ok(); + + // Once ray tracing pipelines are supported they also will go here + let supports_ray_tracing = features5.RaytracingTier.0 + >= Direct3D12::D3D12_RAYTRACING_TIER_1_1.0 + && shader_model >= naga::back::hlsl::ShaderModel::V6_5 + && has_features5; + + features.set( + wgt::Features::EXPERIMENTAL_RAY_QUERY + | wgt::Features::EXTENDED_ACCELERATION_STRUCTURE_VERTEX_FORMATS, + supports_ray_tracing, + ); + + // Binding arrays of TLAS are supported on D3D12 when ray tracing is supported. + // + // This flag is used for shader-side `binding_array<acceleration_structure>` as well as + // allowing `BindGroupLayoutEntry::count = Some(...)` for `BindingType::AccelerationStructure`. + features.set( + wgt::Features::ACCELERATION_STRUCTURE_BINDING_ARRAY, + supports_ray_tracing, + ); + + // Check for Int64 atomic support on buffers. This is very convoluted, but is based on a conservative reading + // of https://microsoft.github.io/DirectX-Specs/d3d/HLSL_SM_6_6_Int64_and_Float_Atomics.html#integer-64-bit-capabilities. + let atomic_int64_buffers; + let atomic_int64_textures; + { + let mut features9 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS9::default(); + let hr9 = unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_D3D12_OPTIONS9, + <*mut _>::cast(&mut features9), + size_of_val(&features9) as u32, + ) + } + .is_ok(); + + let mut features11 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS11::default(); + let hr11 = unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_D3D12_OPTIONS11, + <*mut _>::cast(&mut features11), + size_of_val(&features11) as u32, + ) + } + .is_ok(); + + atomic_int64_buffers = hr9 && hr11 && hr.is_ok() + // Int64 atomics show up in SM6.6. + && shader_model >= naga::back::hlsl::ShaderModel::V6_6 + // They require Int64 to be available in the shader at all. + && features1.Int64ShaderOps.as_bool() + // As our RWByteAddressBuffers can exist on both descriptor heaps and + // as root descriptors, we need to ensure that both cases are supported. + // base SM6.6 only guarantees Int64 atomics on resources in root descriptors. + && features11.AtomicInt64OnDescriptorHeapResourceSupported.as_bool() + // Our Int64 atomic caps currently require groupshared. This + // prevents Intel or Qcomm from using Int64 currently. + // https://github.com/gfx-rs/wgpu/issues/8666 + && features9.AtomicInt64OnGroupSharedSupported.as_bool(); + + atomic_int64_textures = hr9 && hr11 && hr.is_ok() + // Int64 atomics show up in SM6.6. + && shader_model >= naga::back::hlsl::ShaderModel::V6_6 + // They require Int64 to be available in the shader at all. + && features1.Int64ShaderOps.as_bool() + // Textures are typed resources, so we need this flag. + && features9.AtomicInt64OnTypedResourceSupported.as_bool() + // As textures can only exist in descriptor heaps, we require this. + // However, all architectures that support atomics on typed resources + // support this as well, so this is somewhat redundant. + && features11.AtomicInt64OnDescriptorHeapResourceSupported.as_bool(); + }; + features.set( + wgt::Features::SHADER_INT64_ATOMIC_ALL_OPS | wgt::Features::SHADER_INT64_ATOMIC_MIN_MAX, + atomic_int64_buffers, + ); + features.set(wgt::Features::TEXTURE_INT64_ATOMIC, atomic_int64_textures); + let mesh_shader_supported = { + let mut features7 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS7::default(); + unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_D3D12_OPTIONS7, + <*mut _>::cast(&mut features7), + size_of_val(&features7) as u32, + ) + } + .is_ok() + && features7.MeshShaderTier != Direct3D12::D3D12_MESH_SHADER_TIER_NOT_SUPPORTED + && shader_model >= naga::back::hlsl::ShaderModel::V6_5 + }; + features.set( + wgt::Features::EXPERIMENTAL_MESH_SHADER, + mesh_shader_supported, + ); + let shader_barycentrics_supported = { + let mut features3 = Direct3D12::D3D12_FEATURE_DATA_D3D12_OPTIONS3::default(); + unsafe { + device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_D3D12_OPTIONS3, + <*mut _>::cast(&mut features3), + size_of_val(&features3) as u32, + ) + } + .is_ok() + && features3.BarycentricsSupported.as_bool() + && shader_model >= naga::back::hlsl::ShaderModel::V6_1 + }; + features.set( + wgt::Features::SHADER_BARYCENTRICS | wgt::Features::SHADER_PER_VERTEX, + shader_barycentrics_supported, + ); + + features.set( + wgt::Features::MULTIVIEW, + view_instancing && shader_model >= naga::back::hlsl::ShaderModel::V6_1, + ); + features.set( + wgt::Features::SELECTIVE_MULTIVIEW, + view_instancing && shader_model >= naga::back::hlsl::ShaderModel::V6_1, + ); + + features.set( + wgt::Features::EXPERIMENTAL_MESH_SHADER_MULTIVIEW, + mesh_shader_supported + && view_instancing + && shader_model >= naga::back::hlsl::ShaderModel::V6_1, + ); + + // TODO: Determine if IPresentationManager is supported + let presentation_timer = auxil::dxgi::time::PresentationTimer::new_dxgi(); + + let downlevel = wgt::DownlevelCapabilities::default(); + + // Limits that must share D3D12's root signature size of + // D3D12_MAX_ROOT_COST 64 DWORDS (256 bytes). + // + // Root constants and root tables use 1 DWORD. + // Root descriptors use 2 DWORDs. + // Source: https://learn.microsoft.com/en-us/windows/win32/direct3d12/root-signature-limits#memory-limits-and-costs + // + // Per pipeline layout: + // - RootElement::Immediates, 32 root constants + // (bounded by maxImmediateSize) = 32 x 4 bytes = 128 bytes + // - RootElement::SamplerHeapDescriptorTable, a descriptor table = 4 bytes + // - RootElement::SpecialConstantBuffer, 3 root constants = 3 x 4 bytes = 12 bytes + // - RootElement::DynamicStorageBufferOffsets, a root constant per dynamic storage buffer + // (bounded by maxDynamicStorageBuffersPerPipelineLayout) = 4 x 4 bytes = 16 bytes + // - RootElement::DynamicUniformBuffer, a root descriptor per dynamic uniform buffer + // (bounded by maxDynamicUniformBuffersPerPipelineLayout) = 8 x 8 bytes = 64 bytes + // Per bind group: + // - RootElement::DescriptorTable, a descriptor table + // (bounded by maxBindGroups) = 8 x 4 bytes = 32 bytes + // + // Source: logic in `create_pipeline_layout` + // + // Total: 128 + 4 + 12 + 16 + 64 + 32 = 256 bytes + // + let max_immediate_size = super::MAX_IMMEDIATE_SIZE; + let max_bind_groups = 8; + let max_dynamic_uniform_buffers_per_pipeline_layout = 8; + let max_dynamic_storage_buffers_per_pipeline_layout = 4; + + // "Maximum number of descriptors in a Constant Buffer View (CBV), Shader Resource View (SRV), or Unordered Access View(UAV) heap used for rendering" + let full_heap_count = match rbt { + ResourceBindingTier::T1 | ResourceBindingTier::T2 => 1_000_000, + // 1_000_000+ + ResourceBindingTier::T3 => { + // Theoretically vram limited, but in practice 2^20 is the limit + 1 << 20 + } + }; + + // "Maximum number of Constant Buffer Views in all descriptor tables per shader stage" + let max_uniform_buffers_per_shader_stage = match rbt { + ResourceBindingTier::T1 | ResourceBindingTier::T2 => 14, + _ => full_heap_count, + }; + + // "Maximum number of Shader Resource Views in all descriptor tables per shader stage" + let mut max_srv_per_shader_stage = match rbt { + ResourceBindingTier::T1 => 128, + _ => full_heap_count, + }; + + // We use an extra SRV for all samplers in a bind group. + // See comment in `create_pipeline_layout`. + max_srv_per_shader_stage -= max_bind_groups; + + // If we also support acceleration structures these are shared so we must halve it. + // It's unlikely that this affects anything because most devices that support ray tracing + // probably have a higher binding tier than one. + let mut max_sampled_textures_per_shader_stage = if supports_ray_tracing { + max_srv_per_shader_stage / 2 + } else { + max_srv_per_shader_stage + }; + let mut max_acceleration_structures_per_shader_stage = if supports_ray_tracing { + max_srv_per_shader_stage / 2 + } else { + 0 + }; + + // "Maximum number of Unordered Access Views in all descriptor tables across all stages" + let max_uav_across_all_stages = match rbt { + ResourceBindingTier::T1 => match max_feature_level { + FeatureLevel::V11_0 => 8, + _ => 64, + }, + ResourceBindingTier::T2 => 64, + ResourceBindingTier::T3 => full_heap_count, + }; + const MAX_SHADER_STAGES_PER_PIPELINE: u32 = 2; + // We must share the UAV limit across both storage resource limits. + let max_uav_per_shader_stage = max_uav_across_all_stages / MAX_SHADER_STAGES_PER_PIPELINE; + let max_storage_textures_per_shader_stage = max_uav_per_shader_stage / 2; + let mut max_storage_buffers_per_shader_stage = max_uav_per_shader_stage / 2; + + // WebGPU storage buffers count as 1 SRV if they are read-only + // or as 1 UAV if they are read-write. See comment in + // `create_pipeline_layout`. Make sure we don't exceed + // the maximum number of SRVs for the relevant limits. + auxil::cap_limits_to_be_under_the_sum_limit( + [ + &mut max_sampled_textures_per_shader_stage, + &mut max_acceleration_structures_per_shader_stage, + &mut max_storage_buffers_per_shader_stage, + ], + max_srv_per_shader_stage, + ); + + // "Maximum number of Samplers in all descriptor tables per shader stage" + let max_samplers_per_shader_stage = match rbt { + ResourceBindingTier::T1 => 16, + _ => 2048, + }; + + // See https://microsoft.github.io/DirectX-Specs/d3d/ViewInstancing.html#maximum-viewinstancecount + let max_multiview_view_count = if view_instancing { 4 } else { 0 }; + + if let Some(telemetry) = telemetry { + (telemetry.d3d12_expose_adapter)( + &desc, + driver_version, + crate::D3D12ExposeAdapterResult::Success( + max_feature_level, + max_device_shader_model, + ), + ); + } + + // Source: https://microsoft.github.io/DirectX-Specs/d3d/MeshShader.html#dispatchmesh-api + let max_task_mesh_workgroup_total_count = if mesh_shader_supported { + 2u32.pow(22) + } else { + 0 + }; + // Technically it says "64k" but I highly doubt they want 65536 for compute and exactly 64,000 for task workgroups + let max_task_mesh_workgroups_per_dimension = if mesh_shader_supported { + Direct3D12::D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION + } else { + 0 + }; + + Some(crate::ExposedAdapter { + adapter: super::Adapter { + raw: adapter, + device, + library: Arc::clone(library), + dcomp_lib: Arc::clone(dcomp_lib), + private_caps, + presentation_timer, + memory_budget_thresholds, + compiler_container, + options: backend_options, + }, + info, + features, + capabilities: crate::Capabilities { + limits: auxil::adjust_raw_limits(wgt::Limits { + // + // WebGPU LIMITS: + // Based on https://gpuweb.github.io/gpuweb/correspondence/#limits + // + // 16384 + max_texture_dimension_1d: Direct3D12::D3D12_REQ_TEXTURE1D_U_DIMENSION, + // 16384 + max_texture_dimension_2d: Direct3D12::D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION + .min(Direct3D12::D3D12_REQ_TEXTURECUBE_DIMENSION), + // 2048 + max_texture_dimension_3d: Direct3D12::D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION, + // 2048 + max_texture_array_layers: Direct3D12::D3D12_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION, + // No limit. + max_bind_groups_plus_vertex_buffers: u32::MAX, + // No limit. + max_bindings_per_bind_group: u32::MAX, + max_sampled_textures_per_shader_stage, + max_samplers_per_shader_stage, + max_storage_textures_per_shader_stage, + max_storage_buffers_per_shader_stage, + max_uniform_buffers_per_shader_stage, + // See `InputSlot` param docs: https://learn.microsoft.com/en-ca/windows/win32/api/d3d12/ns-d3d12-d3d12_input_element_desc + max_vertex_buffers: 16, + // Dx12 does not expose a maximum buffer size in the API. + // This limit is chosen to avoid potential issues with drivers should they internally + // store buffer sizes using 32 bit ints (a situation we have already encountered with vulkan). + max_buffer_size: i32::MAX as u64, + max_storage_buffer_binding_size: auxil::MAX_I32_BINDING_SIZE as u64, + // 65536 + max_uniform_buffer_binding_size: + Direct3D12::D3D12_REQ_CONSTANT_BUFFER_ELEMENT_COUNT as u64 * 16, + // 256 + min_uniform_buffer_offset_alignment: + Direct3D12::D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT, + // 16 + min_storage_buffer_offset_alignment: + Direct3D12::D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT, + // 30 + max_vertex_attributes: Direct3D12::D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT + - 2, // -2 for `SV_VertexID` and `SV_InstanceID` + // 2048 + max_vertex_buffer_array_stride: Direct3D12::D3D12_SO_BUFFER_MAX_STRIDE_IN_BYTES, + // 31 + max_inter_stage_shader_variables: Direct3D12::D3D12_VS_OUTPUT_REGISTER_COUNT + .min(Direct3D12::D3D12_PS_INPUT_REGISTER_COUNT) + - 1, // - 1 for position + max_immediate_size, + max_bind_groups, + max_dynamic_uniform_buffers_per_pipeline_layout, + max_dynamic_storage_buffers_per_pipeline_layout, + // 8 + max_color_attachments: Direct3D12::D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT, + // 128 (No documented limit) + max_color_attachment_bytes_per_sample: + Direct3D12::D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT + * wgt::TextureFormat::MAX_TARGET_PIXEL_BYTE_COST, + // From: https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm#18.6.6%20Inter-Thread%20Data%20Sharing + max_compute_workgroup_storage_size: 32768, + // 1024 + max_compute_invocations_per_workgroup: + Direct3D12::D3D12_CS_THREAD_GROUP_MAX_THREADS_PER_GROUP, + // 1024 + max_compute_workgroup_size_x: Direct3D12::D3D12_CS_THREAD_GROUP_MAX_X, + // 1024 + max_compute_workgroup_size_y: Direct3D12::D3D12_CS_THREAD_GROUP_MAX_Y, + // 64 + max_compute_workgroup_size_z: Direct3D12::D3D12_CS_THREAD_GROUP_MAX_Z, + // 65535 + max_compute_workgroups_per_dimension: + Direct3D12::D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, + // + // NATIVE (Non-WebGPU) LIMITS: + // + max_non_sampler_bindings: 1_000_000, + max_binding_array_elements_per_shader_stage: full_heap_count, + max_binding_array_sampler_elements_per_shader_stage: + Direct3D12::D3D12_MAX_SHADER_VISIBLE_SAMPLER_HEAP_SIZE, + + max_task_workgroup_total_count: max_task_mesh_workgroup_total_count, + max_task_workgroups_per_dimension: max_task_mesh_workgroups_per_dimension, + max_mesh_workgroup_total_count: max_task_mesh_workgroup_total_count, + max_mesh_workgroups_per_dimension: max_task_mesh_workgroups_per_dimension, + + // Source: https://microsoft.github.io/DirectX-Specs/d3d/MeshShader.html#dispatchmesh-api + // Assume this inherits from compute shaders + max_task_invocations_per_workgroup: if mesh_shader_supported { + Direct3D12::D3D12_CS_4_X_THREAD_GROUP_MAX_THREADS_PER_GROUP + } else { + 0 + }, + max_task_invocations_per_dimension: if mesh_shader_supported { + Direct3D12::D3D12_CS_THREAD_GROUP_MAX_Z + } else { + 0 + }, + // Source: https://microsoft.github.io/DirectX-Specs/d3d/MeshShader.html#amplification-shader-and-mesh-shader + max_mesh_invocations_per_workgroup: if mesh_shader_supported { 128 } else { 0 }, + max_mesh_invocations_per_dimension: if mesh_shader_supported { 128 } else { 0 }, + + max_task_payload_size: if mesh_shader_supported { 16384 } else { 0 }, + max_mesh_output_vertices: if mesh_shader_supported { 256 } else { 0 }, + max_mesh_output_primitives: if mesh_shader_supported { 256 } else { 0 }, + // Source: https://microsoft.github.io/DirectX-Specs/d3d/MeshShader.html#sv_rendertargetarrayindex-limitations-based-on-queryable-capability + max_mesh_output_layers: if mesh_shader_supported { 8 } else { 0 }, + max_mesh_multiview_view_count: if mesh_shader_supported { + max_multiview_view_count + } else { + 0 + }, + + max_blas_primitive_count: if supports_ray_tracing { + 1 << 29 // 2^29 + } else { + 0 + }, + max_blas_geometry_count: if supports_ray_tracing { + 1 << 24 // 2^24 + } else { + 0 + }, + max_tlas_instance_count: if supports_ray_tracing { + 1 << 24 // 2^24 + } else { + 0 + }, + max_acceleration_structures_per_shader_stage, + max_buffers_and_acceleration_structures_per_shader_stage: u32::MAX, + max_binding_array_acceleration_structure_elements_per_shader_stage: + max_acceleration_structures_per_shader_stage, + max_multiview_view_count, + + // not yet implemented + max_ray_dispatch_count: 0, + max_ray_recursion_depth: 0, + }), + alignments: crate::Alignments { + buffer_copy_offset: wgt::BufferSize::new( + Direct3D12::D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT as u64, + ) + .unwrap(), + buffer_copy_pitch: wgt::BufferSize::new( + Direct3D12::D3D12_TEXTURE_DATA_PITCH_ALIGNMENT as u64, + ) + .unwrap(), + // Direct3D correctly bounds-checks all array accesses: + // https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm#18.6.8.2%20Device%20Memory%20Reads + uniform_bounds_check_alignment: wgt::BufferSize::new(1).unwrap(), + raw_tlas_instance_size: u32::try_from(size_of::< + Direct3D12::D3D12_RAYTRACING_INSTANCE_DESC, + >()) + .unwrap(), + ray_tracing_scratch_buffer_alignment: + Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BYTE_ALIGNMENT, + // Not yet implemented + ray_tracing_pipeline_group_data_size: 0, + ray_tracing_pipeline_group_data_alignment: 0, + ray_tracing_pipeline_data_offset_alignment: 0, + }, + downlevel, + cooperative_matrix_properties: Vec::new(), + }, + }) + } +} + +impl crate::Adapter for super::Adapter { + type A = super::Api; + + unsafe fn open( + &self, + features: wgt::Features, + limits: &wgt::Limits, + memory_hints: &wgt::MemoryHints, + ) -> Result<crate::OpenDevice<super::Api>, crate::DeviceError> { + let queue: Direct3D12::ID3D12CommandQueue = { + profiling::scope!("ID3D12Device::CreateCommandQueue"); + unsafe { + self.device + .CreateCommandQueue(&Direct3D12::D3D12_COMMAND_QUEUE_DESC { + Type: Direct3D12::D3D12_COMMAND_LIST_TYPE_DIRECT, + Priority: Direct3D12::D3D12_COMMAND_QUEUE_PRIORITY_NORMAL.0, + Flags: Direct3D12::D3D12_COMMAND_QUEUE_FLAG_NONE, + NodeMask: 0, + }) + } + .into_device_result("Queue creation")? + }; + + let device = super::Device::new( + self.raw.clone(), + self.device.clone(), + queue.clone(), + features, + limits, + memory_hints, + self.private_caps, + &self.library, + &self.dcomp_lib, + self.memory_budget_thresholds, + self.compiler_container.clone(), + self.options.clone(), + )?; + let idle_fence: Direct3D12::ID3D12Fence = unsafe { + self.device + .CreateFence(0, Direct3D12::D3D12_FENCE_FLAG_NONE) + } + .into_device_result("Queue idle fence creation")?; + let idle_event = super::Event::create(false, false)?; + + Ok(crate::OpenDevice { + device, + queue: super::Queue { + raw: queue, + temp_lists: Mutex::new(Vec::new()), + idle_fence, + idle_event, + idle_fence_value: AtomicU64::new(0), + pending_waits: Mutex::new(Vec::new()), + pending_signals: Mutex::new(Vec::new()), + }, + }) + } + + unsafe fn texture_format_capabilities( + &self, + format: wgt::TextureFormat, + ) -> crate::TextureFormatCapabilities { + use crate::TextureFormatCapabilities as Tfc; + + let raw_format = match auxil::dxgi::conv::map_texture_format_failable(format) { + Some(f) => f, + None => return Tfc::empty(), + }; + let srv_uav_format = if format.is_combined_depth_stencil_format() { + auxil::dxgi::conv::map_texture_format_for_srv_uav( + format, + // use the depth aspect here as opposed to stencil since it has more capabilities + crate::FormatAspects::DEPTH, + ) + } else { + auxil::dxgi::conv::map_texture_format_for_srv_uav( + format, + crate::FormatAspects::from(format), + ) + } + .unwrap(); + + let mut data = Direct3D12::D3D12_FEATURE_DATA_FORMAT_SUPPORT { + Format: raw_format, + ..Default::default() + }; + unsafe { + self.device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_FORMAT_SUPPORT, + <*mut _>::cast(&mut data), + size_of_val(&data) as u32, + ) + } + .unwrap(); + + // Because we use a different format for SRV and UAV views of depth textures, we need to check + // the features that use SRV/UAVs using the no-depth format. + let mut data_srv_uav = Direct3D12::D3D12_FEATURE_DATA_FORMAT_SUPPORT { + Format: srv_uav_format, + Support1: Direct3D12::D3D12_FORMAT_SUPPORT1_NONE, + Support2: Direct3D12::D3D12_FORMAT_SUPPORT2_NONE, + }; + if raw_format != srv_uav_format { + // Only-recheck if we're using a different format + unsafe { + self.device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_FORMAT_SUPPORT, + ptr::addr_of_mut!(data_srv_uav).cast(), + size_of::<Direct3D12::D3D12_FEATURE_DATA_FORMAT_SUPPORT>() as u32, + ) + } + .unwrap(); + } else { + // Same format, just copy over. + data_srv_uav = data; + } + + let mut caps = Tfc::COPY_SRC | Tfc::COPY_DST; + // Cannot use the contains() helper, and windows-rs doesn't provide a .intersect() helper + let is_texture = (data.Support1 + & (Direct3D12::D3D12_FORMAT_SUPPORT1_TEXTURE1D + | Direct3D12::D3D12_FORMAT_SUPPORT1_TEXTURE2D + | Direct3D12::D3D12_FORMAT_SUPPORT1_TEXTURE3D + | Direct3D12::D3D12_FORMAT_SUPPORT1_TEXTURECUBE)) + .0 + != 0; + // SRVs use srv_uav_format + caps.set( + Tfc::SAMPLED, + is_texture + && data_srv_uav + .Support1 + .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_SHADER_LOAD), + ); + caps.set( + Tfc::SAMPLED_LINEAR, + data_srv_uav + .Support1 + .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE), + ); + caps.set( + Tfc::COLOR_ATTACHMENT, + data.Support1 + .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_RENDER_TARGET), + ); + caps.set( + Tfc::COLOR_ATTACHMENT_BLEND, + data.Support1 + .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_BLENDABLE), + ); + caps.set( + Tfc::DEPTH_STENCIL_ATTACHMENT, + data.Support1 + .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_DEPTH_STENCIL), + ); + // UAVs use srv_uav_format + caps.set( + Tfc::STORAGE_READ_ONLY, + data_srv_uav + .Support2 + .contains(Direct3D12::D3D12_FORMAT_SUPPORT2_UAV_TYPED_LOAD), + ); + caps.set( + Tfc::STORAGE_ATOMIC, + data_srv_uav + .Support2 + .contains(Direct3D12::D3D12_FORMAT_SUPPORT2_UAV_ATOMIC_UNSIGNED_MIN_OR_MAX), + ); + caps.set( + Tfc::STORAGE_WRITE_ONLY, + data_srv_uav + .Support2 + .contains(Direct3D12::D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE), + ); + caps.set( + Tfc::STORAGE_READ_WRITE, + caps.contains(Tfc::STORAGE_READ_ONLY | Tfc::STORAGE_WRITE_ONLY), + ); + + // We load via UAV/SRV so use srv_uav_format + let no_msaa_load = caps.contains(Tfc::SAMPLED) + && !data_srv_uav + .Support1 + .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_MULTISAMPLE_LOAD); + + let no_msaa_target = (data.Support1 + & (Direct3D12::D3D12_FORMAT_SUPPORT1_RENDER_TARGET + | Direct3D12::D3D12_FORMAT_SUPPORT1_DEPTH_STENCIL)) + .0 + != 0 + && !data + .Support1 + .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_MULTISAMPLE_RENDERTARGET); + + caps.set( + Tfc::MULTISAMPLE_RESOLVE, + data.Support1 + .contains(Direct3D12::D3D12_FORMAT_SUPPORT1_MULTISAMPLE_RESOLVE), + ); + + let mut ms_levels = Direct3D12::D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS { + Format: raw_format, + SampleCount: 0, + Flags: Direct3D12::D3D12_MULTISAMPLE_QUALITY_LEVELS_FLAG_NONE, + NumQualityLevels: 0, + }; + + let mut set_sample_count = |sc: u32, tfc: Tfc| { + ms_levels.SampleCount = sc; + + if unsafe { + self.device.CheckFeatureSupport( + Direct3D12::D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, + <*mut _>::cast(&mut ms_levels), + size_of_val(&ms_levels) as u32, + ) + } + .is_ok() + && ms_levels.NumQualityLevels != 0 + { + caps.set(tfc, !no_msaa_load && !no_msaa_target); + } + }; + + set_sample_count(2, Tfc::MULTISAMPLE_X2); + set_sample_count(4, Tfc::MULTISAMPLE_X4); + set_sample_count(8, Tfc::MULTISAMPLE_X8); + set_sample_count(16, Tfc::MULTISAMPLE_X16); + + caps + } + + unsafe fn surface_capabilities( + &self, + surface: &super::Surface, + ) -> Option<crate::SurfaceCapabilities> { + let current_extent = { + match surface.target { + SurfaceTarget::WndHandle(wnd_handle) + | SurfaceTarget::VisualFromWndHandle { + handle: wnd_handle, .. + } => { + let mut rect = Default::default(); + if unsafe { WindowsAndMessaging::GetClientRect(wnd_handle, &mut rect) }.is_ok() + { + Some(wgt::Extent3d { + width: (rect.right - rect.left) as u32, + height: (rect.bottom - rect.top) as u32, + depth_or_array_layers: 1, + }) + } else { + log::warn!("Unable to get the window client rect"); + None + } + } + SurfaceTarget::Visual(_) + | SurfaceTarget::SurfaceHandle(_) + | SurfaceTarget::SwapChainPanel(_) => None, + } + }; + + let mut present_modes = vec![wgt::PresentMode::Mailbox, wgt::PresentMode::Fifo]; + if surface.supports_allow_tearing { + present_modes.push(wgt::PresentMode::Immediate); + } + + Some(crate::SurfaceCapabilities { + // `Surface::configure` applies the requested color space with + // `IDXGISwapChain3::SetColorSpace1`. fp16 buffers keep DXGI's + // scRGB interpretation (`DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709`) + // and `Rgb10a2Unorm` additionally supports BT.2100 PQ (HDR10). + // + // These color spaces are advertised unconditionally, not gated on + // whether the output is currently in HDR mode: Windows always + // composites in scRGB and tone-maps PQ down to an SDR output, so the + // color space is configurable regardless, and `CheckColorSpaceSupport` + // returning false does not mean it won't present. Whether HDR is + // actually *visible* is a separate, live question (the upcoming + // display-HDR query, #9739), not a configuration gate. Display-P3 and + // HLG are never reported: DXGI has no RGB HLG swapchain color space, + // and P3 isn't a DXGI swapchain color space. + formats: [ + wgt::TextureFormat::Bgra8UnormSrgb, + wgt::TextureFormat::Bgra8Unorm, + wgt::TextureFormat::Rgba8UnormSrgb, + wgt::TextureFormat::Rgba8Unorm, + wgt::TextureFormat::Rgb10a2Unorm, + wgt::TextureFormat::Rgba16Float, + ] + .map(|format| wgt::SurfaceFormatCapabilities { + format, + color_spaces: match format { + wgt::TextureFormat::Rgba16Float => { + wgt::SurfaceColorSpaces::EXTENDED_SRGB_LINEAR + } + wgt::TextureFormat::Rgb10a2Unorm => { + wgt::SurfaceColorSpaces::SRGB | wgt::SurfaceColorSpaces::BT2100_PQ + } + _ => wgt::SurfaceColorSpaces::SRGB, + }, + }) + .to_vec(), + // See https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgidevice1-setmaximumframelatency + maximum_frame_latency: 1..=16, + current_extent, + usage: wgt::TextureUses::COLOR_TARGET + | wgt::TextureUses::COPY_SRC + | wgt::TextureUses::COPY_DST, + present_modes, + composite_alpha_modes: match surface.target { + SurfaceTarget::WndHandle(_) => vec![wgt::CompositeAlphaMode::Opaque], + SurfaceTarget::Visual(_) + | SurfaceTarget::VisualFromWndHandle { .. } + | SurfaceTarget::SurfaceHandle(_) + | SurfaceTarget::SwapChainPanel(_) => vec![ + wgt::CompositeAlphaMode::Auto, + wgt::CompositeAlphaMode::Inherit, + wgt::CompositeAlphaMode::Opaque, + wgt::CompositeAlphaMode::PostMultiplied, + wgt::CompositeAlphaMode::PreMultiplied, + ], + }, + }) + } + + unsafe fn surface_display_hdr_info( + &self, + surface: &super::Surface, + ) -> Option<wgt::DisplayHdrInfo> { + surface.hdr_source.as_ref()?.display_hdr_info() + } + + unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp { + wgt::PresentationTimestamp(self.presentation_timer.get_timestamp_ns()) + } + + fn get_ordered_buffer_usages(&self) -> wgt::BufferUses { + wgt::BufferUses::INCLUSIVE | wgt::BufferUses::MAP_WRITE + } + + // Don't put barriers between inclusive uses + // DX12 implicitly orders renderpasses on the same resources. + fn get_ordered_texture_usages(&self) -> wgt::TextureUses { + wgt::TextureUses::INCLUSIVE + | wgt::TextureUses::COLOR_TARGET + | wgt::TextureUses::DEPTH_STENCIL_WRITE + } +} + +fn get_adapter_pci_info(vendor_id: u32, device_id: u32) -> String { + // SAFETY: SetupDiGetClassDevsW is called with valid parameters + let device_info_set = unsafe { + match SetupDiGetClassDevsW(Some(&GUID_DEVCLASS_DISPLAY), None, None, DIGCF_PRESENT) { + Ok(set) => set, + Err(_) => return String::new(), + } + }; + + struct DeviceInfoSetGuard(HDEVINFO); + impl Drop for DeviceInfoSetGuard { + fn drop(&mut self) { + // SAFETY: device_info_set is a valid HDEVINFO and is only dropped once via this guard + unsafe { + let _ = SetupDiDestroyDeviceInfoList(self.0); + } + } + } + let _guard = DeviceInfoSetGuard(device_info_set); + + let mut device_index = 0u32; + loop { + let mut device_info_data = SP_DEVINFO_DATA { + cbSize: size_of::<SP_DEVINFO_DATA>() as u32, + ..Default::default() + }; + + // SAFETY: device_info_set is a valid HDEVINFO, device_index starts at 0 and + // device_info_data is properly initialized above + unsafe { + if SetupDiEnumDeviceInfo(device_info_set, device_index, &mut device_info_data).is_err() + { + if GetLastError() == ERROR_NO_MORE_ITEMS { + break; + } + device_index += 1; + continue; + } + } + + let mut hardware_id_size = 0u32; + // SAFETY: device_info_set and device_info_data are valid + unsafe { + let _ = SetupDiGetDeviceRegistryPropertyW( + device_info_set, + &device_info_data, + SPDRP_HARDWAREID, + None, + None, + Some(&mut hardware_id_size), + ); + } + + if hardware_id_size == 0 { + device_index += 1; + continue; + } + + let mut hardware_id_buffer = vec![0u8; hardware_id_size as usize]; + // SAFETY: device_info_set and device_info_data are valid + unsafe { + if SetupDiGetDeviceRegistryPropertyW( + device_info_set, + &device_info_data, + SPDRP_HARDWAREID, + None, + Some(&mut hardware_id_buffer), + Some(&mut hardware_id_size), + ) + .is_err() + { + device_index += 1; + continue; + } + } + + let hardware_id_u16: Vec<u16> = hardware_id_buffer + .chunks_exact(2) + .map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]])) + .collect(); + let hardware_ids: Vec<String> = hardware_id_u16 + .split(|&c| c == 0) + .filter(|s| !s.is_empty()) + .map(|s| String::from_utf16_lossy(s).to_uppercase()) + .collect(); + + // https://learn.microsoft.com/en-us/windows-hardware/drivers/install/identifiers-for-pci-devices + let expected_id = format!("PCI\\VEN_{vendor_id:04X}&DEV_{device_id:04X}"); + if !hardware_ids.iter().any(|id| id.contains(&expected_id)) { + device_index += 1; + continue; + } + + let mut bus_buffer = [0u8; 4]; + let mut data_size = bus_buffer.len() as u32; + // SAFETY: device_info_set and device_info_data are valid + let bus_number = unsafe { + if SetupDiGetDeviceRegistryPropertyW( + device_info_set, + &device_info_data, + SPDRP_BUSNUMBER, + None, + Some(&mut bus_buffer), + Some(&mut data_size), + ) + .is_err() + { + device_index += 1; + continue; + } + u32::from_le_bytes(bus_buffer) + }; + + let mut addr_buffer = [0u8; 4]; + let mut addr_size = addr_buffer.len() as u32; + // SAFETY: device_info_set and device_info_data are valid + unsafe { + if SetupDiGetDeviceRegistryPropertyW( + device_info_set, + &device_info_data, + SPDRP_ADDRESS, + None, + Some(&mut addr_buffer), + Some(&mut addr_size), + ) + .is_err() + { + device_index += 1; + continue; + } + } + let address = u32::from_le_bytes(addr_buffer); + + // https://learn.microsoft.com/en-us/windows-hardware/drivers/kernel/obtaining-device-configuration-information-at-irql---dispatch-level + let device = (address >> 16) & 0x0000FFFF; + let function = address & 0x0000FFFF; + + // domain:bus:device.function + return format!("{:04x}:{:02x}:{:02x}.{:x}", 0, bus_number, device, function); + } + + String::new() +} diff --git a/third_party/wgpu-hal/src/dx12/command.rs b/third_party/wgpu-hal/src/dx12/command.rs new file mode 100644 index 000000000..9e90e2669 --- /dev/null +++ b/third_party/wgpu-hal/src/dx12/command.rs @@ -0,0 +1,1868 @@ +use alloc::vec::Vec; +use core::{mem, ops::Range}; + +use windows::{ + core::Interface as _, + Win32::{ + Foundation, + Graphics::{Direct3D12, Dxgi}, + }, +}; + +use super::conv; +use crate::{ + auxil::{ + self, + dxgi::{name::ObjectExt as _, result::HResult as _}, + }, + dx12::borrow_interface_temporarily, + AccelerationStructureEntries, CommandEncoder as _, +}; + +fn make_box(origin: &wgt::Origin3d, size: &crate::CopyExtent) -> Direct3D12::D3D12_BOX { + Direct3D12::D3D12_BOX { + left: origin.x, + top: origin.y, + right: origin.x + size.width, + bottom: origin.y + size.height, + front: origin.z, + back: origin.z + size.depth, + } +} + +impl crate::BufferTextureCopy { + fn to_subresource_footprint( + &self, + format: wgt::TextureFormat, + ) -> Direct3D12::D3D12_PLACED_SUBRESOURCE_FOOTPRINT { + let (block_width, _) = format.block_dimensions(); + Direct3D12::D3D12_PLACED_SUBRESOURCE_FOOTPRINT { + Offset: self.buffer_layout.offset, + Footprint: Direct3D12::D3D12_SUBRESOURCE_FOOTPRINT { + Format: auxil::dxgi::conv::map_texture_format_for_copy( + format, + self.texture_base.aspect, + ) + .unwrap(), + Width: self.size.width, + Height: self.size.height, + Depth: self.size.depth, + RowPitch: { + let actual = self.buffer_layout.bytes_per_row.unwrap_or_else(|| { + // this may happen for single-line updates + let block_size = format + .block_copy_size(Some(self.texture_base.aspect.map())) + .unwrap(); + (self.size.width / block_width) * block_size + }); + wgt::math::align_to(actual, Direct3D12::D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) + }, + }, + } + } +} + +impl super::Temp { + fn prepare_marker(&mut self, marker: &str) -> (&[u16], u32) { + self.marker.clear(); + self.marker.extend(marker.encode_utf16()); + self.marker.push(0); + (&self.marker, self.marker.len() as u32 * 2) + } +} + +impl Drop for super::CommandEncoder { + fn drop(&mut self) { + use crate::CommandEncoder; + unsafe { self.discard_encoding() } + + let mut rtv_pool = self.rtv_pool.lock(); + for handle in self.temp_rtv_handles.drain(..) { + rtv_pool.free_handle(handle); + } + drop(rtv_pool); + + self.counters.command_encoders.sub(1); + } +} + +impl super::CommandEncoder { + unsafe fn begin_pass(&mut self, kind: super::PassKind, label: crate::Label) { + let list = self.list.as_ref().unwrap(); + self.pass.kind = kind; + if let Some(label) = label { + let (wide_label, size) = self.temp.prepare_marker(label); + unsafe { list.BeginEvent(0, Some(wide_label.as_ptr().cast()), size) }; + self.pass.has_label = true; + } + self.pass.dirty_root_elements = 0; + self.pass.dirty_vertex_buffers = 0; + unsafe { + list.SetDescriptorHeaps(&[ + Some(self.shared.heap_views.raw.clone()), + Some(self.shared.sampler_heap.heap().clone()), + ]) + }; + } + + unsafe fn end_pass(&mut self) { + let list = self.list.as_ref().unwrap(); + unsafe { list.SetDescriptorHeaps(&[]) }; + if self.pass.has_label { + unsafe { list.EndEvent() }; + } + self.pass.clear(); + } + + unsafe fn prepare_vertex_buffers(&mut self) { + while self.pass.dirty_vertex_buffers != 0 { + let list = self.list.as_ref().unwrap(); + let index = self.pass.dirty_vertex_buffers.trailing_zeros(); + self.pass.dirty_vertex_buffers ^= 1 << index; + unsafe { + list.IASetVertexBuffers( + index, + Some(&self.pass.vertex_buffers[index as usize..][..1]), + ); + } + } + } + + unsafe fn prepare_draw(&mut self, first_vertex: i32, first_instance: u32) { + unsafe { + self.prepare_vertex_buffers(); + } + if let Some(root_index) = self + .pass + .layout + .special_constants + .as_ref() + .map(|sc| sc.root_index) + { + let special_constants = super::SpecialConstants::from_indirect_draw_call_params( + first_vertex, + first_instance, + ); + let needs_update = match self.pass.root_elements[root_index as usize] { + super::RootElement::SpecialConstants(old_special_constants) => { + old_special_constants != special_constants + } + _ => true, + }; + if needs_update { + self.pass.dirty_root_elements |= 1 << root_index; + self.pass.root_elements[root_index as usize] = + super::RootElement::SpecialConstants(special_constants); + } + } + self.update_root_elements(); + } + + fn prepare_dispatch(&mut self, workgroup_count: [u32; 3]) { + if let Some(root_index) = self + .pass + .layout + .special_constants + .as_ref() + .map(|sc| sc.root_index) + { + let special_constants = + super::SpecialConstants::from_compute_dispatch_params(workgroup_count); + let needs_update = match self.pass.root_elements[root_index as usize] { + super::RootElement::SpecialConstants(old_special_constants) => { + old_special_constants != special_constants + } + _ => true, + }; + if needs_update { + self.pass.dirty_root_elements |= 1 << root_index; + self.pass.root_elements[root_index as usize] = + super::RootElement::SpecialConstants(special_constants); + } + } + self.update_root_elements(); + } + + // Note: we have to call this lazily before draw calls. Otherwise, D3D complains + // about the root parameters being incompatible with root signature. + fn update_root_elements(&mut self) { + use super::PassKind as Pk; + + while self.pass.dirty_root_elements != 0 { + let list = self.list.as_ref().unwrap(); + let index = self.pass.dirty_root_elements.trailing_zeros(); + self.pass.dirty_root_elements ^= 1 << index; + + match self.pass.root_elements[index as usize] { + super::RootElement::Empty => unreachable!( + "Empty root element at index {index} should not have been marked as dirty" + ), + super::RootElement::Immediates => { + let info = self.pass.layout.immediates_info.as_ref().unwrap(); + + for offset in 0..info.size { + let val = self.pass.immediates[offset as usize]; + match self.pass.kind { + Pk::Render => unsafe { + list.SetGraphicsRoot32BitConstant(index, val, offset) + }, + Pk::Compute => unsafe { + list.SetComputeRoot32BitConstant(index, val, offset) + }, + Pk::Transfer => (), + } + } + } + super::RootElement::SpecialConstants(super::SpecialConstants { + first_vertex_or_x, + first_instance_or_y, + unused_or_z, + }) => match self.pass.kind { + Pk::Render => { + unsafe { + list.SetGraphicsRoot32BitConstant(index, first_vertex_or_x as u32, 0) + }; + unsafe { list.SetGraphicsRoot32BitConstant(index, first_instance_or_y, 1) }; + } + Pk::Compute => { + unsafe { + list.SetComputeRoot32BitConstant(index, first_vertex_or_x as u32, 0) + }; + unsafe { list.SetComputeRoot32BitConstant(index, first_instance_or_y, 1) }; + unsafe { list.SetComputeRoot32BitConstant(index, unused_or_z, 2) }; + } + Pk::Transfer => (), + }, + super::RootElement::DescriptorTable(descriptor) => match self.pass.kind { + Pk::Render => unsafe { list.SetGraphicsRootDescriptorTable(index, descriptor) }, + Pk::Compute => unsafe { list.SetComputeRootDescriptorTable(index, descriptor) }, + Pk::Transfer => (), + }, + super::RootElement::DynamicUniformBuffer { address } => { + let address = address.ptr; + match self.pass.kind { + Pk::Render => unsafe { + list.SetGraphicsRootConstantBufferView(index, address) + }, + Pk::Compute => unsafe { + list.SetComputeRootConstantBufferView(index, address) + }, + Pk::Transfer => (), + } + } + super::RootElement::DynamicStorageBufferOffsets { start, end } => { + let values = &self.pass.dynamic_storage_buffer_offsets[start..end]; + + for (offset, &value) in values.iter().enumerate() { + match self.pass.kind { + Pk::Render => unsafe { + list.SetGraphicsRoot32BitConstant(index, value, offset as u32) + }, + Pk::Compute => unsafe { + list.SetComputeRoot32BitConstant(index, value, offset as u32) + }, + Pk::Transfer => (), + } + } + } + super::RootElement::SamplerHeapDescriptorTable => match self.pass.kind { + Pk::Render => unsafe { + list.SetGraphicsRootDescriptorTable( + index, + self.shared.sampler_heap.gpu_descriptor_table(), + ) + }, + Pk::Compute => unsafe { + list.SetComputeRootDescriptorTable( + index, + self.shared.sampler_heap.gpu_descriptor_table(), + ) + }, + Pk::Transfer => (), + }, + } + } + } + + fn reset_signature(&mut self, layout: &super::PipelineLayoutShared) { + if let Some(root_index) = layout.special_constants.as_ref().map(|sc| sc.root_index) { + self.pass.root_elements[root_index as usize] = + super::RootElement::SpecialConstants(super::SpecialConstants::default()); + } + if let Some(root_index) = layout.sampler_heap_root_index { + self.pass.root_elements[root_index as usize] = + super::RootElement::SamplerHeapDescriptorTable; + } + self.pass.layout = layout.clone(); + self.pass.dirty_root_elements = (1 << layout.total_root_elements) - 1; + } + + fn write_pass_end_timestamp_if_requested(&mut self) { + if let Some((query_set_raw, index)) = self.end_of_pass_timer_query.take() { + use crate::CommandEncoder as _; + unsafe { + self.write_timestamp( + &crate::dx12::QuerySet { + raw: query_set_raw, + raw_ty: Direct3D12::D3D12_QUERY_TYPE_TIMESTAMP, + }, + index, + ); + } + } + } + + unsafe fn buf_tex_intermediate<T>( + &mut self, + region: crate::BufferTextureCopy, + tex_fmt: wgt::TextureFormat, + copy_op: impl FnOnce(&mut Self, &super::Buffer, wgt::BufferSize, crate::BufferTextureCopy) -> T, + ) -> (T, super::Buffer) { + let size = { + let copy_info = region.buffer_layout.get_buffer_texture_copy_info( + tex_fmt, + region.texture_base.aspect.map(), + ®ion.size.into(), + ); + copy_info.unwrap().bytes_in_copy + }; + + let size = wgt::BufferSize::new(size).unwrap(); + + let buffer = { + let (resource, allocation) = + super::suballocation::DeviceAllocationContext::from(&*self) + .create_buffer(&crate::BufferDescriptor { + label: None, + size: size.get(), + usage: wgt::BufferUses::COPY_SRC | wgt::BufferUses::COPY_DST, + memory_flags: crate::MemoryFlags::empty(), + }) + .expect(concat!( + "internal error: ", + "failed to allocate intermediate buffer ", + "for offset alignment" + )); + super::Buffer { + resource, + size: size.get(), + allocation, + } + }; + + let mut region = region; + region.buffer_layout.offset = 0; + + unsafe { + self.transition_buffers( + [crate::BufferBarrier { + buffer: &buffer, + usage: crate::StateTransition { + from: wgt::BufferUses::empty(), + to: wgt::BufferUses::COPY_DST, + }, + }] + .into_iter(), + ) + }; + + let t = copy_op(self, &buffer, size, region); + + unsafe { + self.transition_buffers( + [crate::BufferBarrier { + buffer: &buffer, + usage: crate::StateTransition { + from: wgt::BufferUses::COPY_DST, + to: wgt::BufferUses::COPY_SRC, + }, + }] + .into_iter(), + ) + }; + + (t, buffer) + } +} + +impl crate::CommandEncoder for super::CommandEncoder { + type A = super::Api; + + unsafe fn begin_encoding(&mut self, label: crate::Label) -> Result<(), crate::DeviceError> { + let list = loop { + if let Some(list) = self.free_lists.pop() { + // TODO: Is an error expected here and should we print it? + let reset_result = unsafe { list.Reset(&self.allocator, None) }; + if reset_result.is_ok() { + break Some(list); + } + } else { + break None; + } + }; + + let list = if let Some(list) = list { + list + } else { + unsafe { + self.device.CreateCommandList( + 0, + Direct3D12::D3D12_COMMAND_LIST_TYPE_DIRECT, + &self.allocator, + None, + ) + } + .into_device_result("Create command list")? + }; + + if let Some(label) = label { + list.set_name(label)?; + } + + // Ensure clean state even if the last encoding did not complete normally. + self.temp.clear(); + self.pass.clear(); + self.end_of_pass_timer_query = None; + + self.list = Some(list); + Ok(()) + } + unsafe fn discard_encoding(&mut self) { + if let Some(list) = self.list.take() { + if unsafe { list.Close() }.is_ok() { + self.free_lists.push(list); + } + } + } + unsafe fn end_encoding(&mut self) -> Result<super::CommandBuffer, crate::DeviceError> { + let raw = self.list.take().unwrap(); + unsafe { raw.Close() }.into_device_result("GraphicsCommandList::close")?; + Ok(super::CommandBuffer { raw }) + } + unsafe fn reset_all<I: Iterator<Item = super::CommandBuffer>>(&mut self, command_buffers: I) { + self.intermediate_copy_bufs.clear(); + for cmd_buf in command_buffers { + self.free_lists.push(cmd_buf.raw); + } + if let Err(e) = unsafe { self.allocator.Reset() } { + log::error!("ID3D12CommandAllocator::Reset() failed with {e}"); + } + } + + unsafe fn transition_buffers<'a, T>(&mut self, barriers: T) + where + T: Iterator<Item = crate::BufferBarrier<'a, super::Buffer>>, + { + self.temp.barriers.clear(); + + for barrier in barriers { + let s0 = conv::map_buffer_usage_to_state(barrier.usage.from); + let s1 = conv::map_buffer_usage_to_state(barrier.usage.to); + if s0 != s1 { + let raw = Direct3D12::D3D12_RESOURCE_BARRIER { + Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, + Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE, + Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 { + Transition: mem::ManuallyDrop::new( + Direct3D12::D3D12_RESOURCE_TRANSITION_BARRIER { + pResource: unsafe { + borrow_interface_temporarily(&barrier.buffer.resource) + }, + Subresource: Direct3D12::D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, + StateBefore: s0, + StateAfter: s1, + }, + ), + }, + }; + self.temp.barriers.push(raw); + } else if barrier.usage.from == wgt::BufferUses::STORAGE_READ_WRITE + || barrier.usage.from == wgt::BufferUses::ACCELERATION_STRUCTURE_QUERY + { + let raw = Direct3D12::D3D12_RESOURCE_BARRIER { + Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_UAV, + Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE, + Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 { + UAV: mem::ManuallyDrop::new(Direct3D12::D3D12_RESOURCE_UAV_BARRIER { + pResource: unsafe { + borrow_interface_temporarily(&barrier.buffer.resource) + }, + }), + }, + }; + self.temp.barriers.push(raw); + } + } + + if !self.temp.barriers.is_empty() { + unsafe { + self.list + .as_ref() + .unwrap() + .ResourceBarrier(&self.temp.barriers) + }; + } + } + + unsafe fn transition_textures<'a, T>(&mut self, barriers: T) + where + T: Iterator<Item = crate::TextureBarrier<'a, super::Texture>>, + { + self.temp.barriers.clear(); + + for barrier in barriers { + let s0 = conv::map_texture_usage_to_state(barrier.usage.from); + let s1 = conv::map_texture_usage_to_state(barrier.usage.to); + if s0 != s1 { + let mut raw = Direct3D12::D3D12_RESOURCE_BARRIER { + Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, + Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE, + Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 { + Transition: mem::ManuallyDrop::new( + Direct3D12::D3D12_RESOURCE_TRANSITION_BARRIER { + pResource: unsafe { + borrow_interface_temporarily(&barrier.texture.resource) + }, + Subresource: Direct3D12::D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, + StateBefore: s0, + StateAfter: s1, + }, + ), + }, + }; + + let tex_mip_level_count = barrier.texture.mip_level_count; + let tex_array_layer_count = barrier.texture.array_layer_count(); + + if barrier.range.is_full_resource( + barrier.texture.format, + tex_mip_level_count, + tex_array_layer_count, + ) { + // Only one barrier if it affects the whole image. + self.temp.barriers.push(raw); + } else { + // Selected texture aspect is relevant if the texture format has both depth _and_ stencil aspects. + let planes = if barrier.texture.format.is_combined_depth_stencil_format() { + match barrier.range.aspect { + wgt::TextureAspect::All => 0..2, + wgt::TextureAspect::DepthOnly => 0..1, + wgt::TextureAspect::StencilOnly => 1..2, + _ => unreachable!(), + } + } else if let Some(planes) = barrier.texture.format.planes() { + match barrier.range.aspect { + wgt::TextureAspect::All => 0..planes, + wgt::TextureAspect::Plane0 => 0..1, + wgt::TextureAspect::Plane1 => 1..2, + wgt::TextureAspect::Plane2 => 2..3, + _ => unreachable!(), + } + } else { + match barrier.texture.format { + wgt::TextureFormat::Stencil8 => 1..2, + wgt::TextureFormat::Depth24Plus => 0..2, // TODO: investigate why tests fail if we set this to 0..1 + _ => 0..1, + } + }; + + for mip_level in barrier.range.mip_range(tex_mip_level_count) { + for array_layer in barrier.range.layer_range(tex_array_layer_count) { + for plane in planes.clone() { + unsafe { &mut *raw.Anonymous.Transition }.Subresource = barrier + .texture + .calc_subresource(mip_level, array_layer, plane); + self.temp.barriers.push(raw.clone()); + } + } + } + } + } else if barrier.usage.from == wgt::TextureUses::STORAGE_READ_WRITE { + let raw = Direct3D12::D3D12_RESOURCE_BARRIER { + Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_UAV, + Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE, + Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 { + UAV: mem::ManuallyDrop::new(Direct3D12::D3D12_RESOURCE_UAV_BARRIER { + pResource: unsafe { + borrow_interface_temporarily(&barrier.texture.resource) + }, + }), + }, + }; + self.temp.barriers.push(raw); + } + } + + if !self.temp.barriers.is_empty() { + unsafe { + self.list + .as_ref() + .unwrap() + .ResourceBarrier(&self.temp.barriers) + }; + } + } + + unsafe fn clear_buffer(&mut self, buffer: &super::Buffer, range: crate::MemoryRange) { + let list = self.list.as_ref().unwrap(); + let mut offset = range.start; + while offset < range.end { + let size = super::ZERO_BUFFER_SIZE.min(range.end - offset); + unsafe { + list.CopyBufferRegion(&buffer.resource, offset, &self.shared.zero_buffer, 0, size) + }; + offset += size; + } + } + + unsafe fn copy_buffer_to_buffer<T>( + &mut self, + src: &super::Buffer, + dst: &super::Buffer, + regions: T, + ) where + T: Iterator<Item = crate::BufferCopy>, + { + let list = self.list.as_ref().unwrap(); + for r in regions { + unsafe { + list.CopyBufferRegion( + &dst.resource, + r.dst_offset, + &src.resource, + r.src_offset, + r.size.get(), + ) + }; + } + } + + unsafe fn copy_texture_to_texture<T>( + &mut self, + src: &super::Texture, + _src_usage: wgt::TextureUses, + dst: &super::Texture, + regions: T, + ) where + T: Iterator<Item = crate::TextureCopy>, + { + let list = self.list.as_ref().unwrap(); + + for r in regions { + let src_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION { + pResource: unsafe { borrow_interface_temporarily(&src.resource) }, + Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, + Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 { + SubresourceIndex: src.calc_subresource_for_copy(&r.src_base), + }, + }; + let dst_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION { + pResource: unsafe { borrow_interface_temporarily(&dst.resource) }, + Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, + Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 { + SubresourceIndex: dst.calc_subresource_for_copy(&r.dst_base), + }, + }; + + let src_box = make_box(&r.src_base.origin, &r.size); + + unsafe { + list.CopyTextureRegion( + &dst_location, + r.dst_base.origin.x, + r.dst_base.origin.y, + r.dst_base.origin.z, + &src_location, + Some(&src_box), + ) + }; + } + } + + unsafe fn copy_buffer_to_texture<T>( + &mut self, + src: &super::Buffer, + dst: &super::Texture, + regions: T, + ) where + T: Iterator<Item = crate::BufferTextureCopy>, + { + let offset_alignment = self.shared.private_caps.texture_data_placement_alignment(); + + for naive_copy_region in regions { + let is_offset_aligned = naive_copy_region.buffer_layout.offset % offset_alignment == 0; + let (final_copy_region, src) = if is_offset_aligned { + (naive_copy_region, src) + } else { + let (intermediate_to_dst_region, intermediate_buf) = unsafe { + let src_offset = naive_copy_region.buffer_layout.offset; + self.buf_tex_intermediate( + naive_copy_region, + dst.format, + |this, buf, size, intermediate_to_dst_region| { + let layout = crate::BufferCopy { + src_offset, + dst_offset: 0, + size, + }; + this.copy_buffer_to_buffer(src, buf, [layout].into_iter()); + intermediate_to_dst_region + }, + ) + }; + self.intermediate_copy_bufs.push(intermediate_buf); + let intermediate_buf = self.intermediate_copy_bufs.last().unwrap(); + (intermediate_to_dst_region, intermediate_buf) + }; + + let list = self.list.as_ref().unwrap(); + + let src_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION { + pResource: unsafe { borrow_interface_temporarily(&src.resource) }, + Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, + Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 { + PlacedFootprint: final_copy_region.to_subresource_footprint(dst.format), + }, + }; + let dst_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION { + pResource: unsafe { borrow_interface_temporarily(&dst.resource) }, + Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, + Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 { + SubresourceIndex: dst + .calc_subresource_for_copy(&final_copy_region.texture_base), + }, + }; + + let src_box = make_box(&wgt::Origin3d::ZERO, &final_copy_region.size); + unsafe { + list.CopyTextureRegion( + &dst_location, + final_copy_region.texture_base.origin.x, + final_copy_region.texture_base.origin.y, + final_copy_region.texture_base.origin.z, + &src_location, + Some(&src_box), + ) + }; + } + } + + unsafe fn copy_texture_to_buffer<T>( + &mut self, + src: &super::Texture, + _src_usage: wgt::TextureUses, + dst: &super::Buffer, + regions: T, + ) where + T: Iterator<Item = crate::BufferTextureCopy>, + { + let copy_aligned = |this: &mut Self, + src: &super::Texture, + dst: &super::Buffer, + r: crate::BufferTextureCopy| { + let list = this.list.as_ref().unwrap(); + + let src_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION { + pResource: unsafe { borrow_interface_temporarily(&src.resource) }, + Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, + Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 { + SubresourceIndex: src.calc_subresource_for_copy(&r.texture_base), + }, + }; + let dst_location = Direct3D12::D3D12_TEXTURE_COPY_LOCATION { + pResource: unsafe { borrow_interface_temporarily(&dst.resource) }, + Type: Direct3D12::D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, + Anonymous: Direct3D12::D3D12_TEXTURE_COPY_LOCATION_0 { + PlacedFootprint: r.to_subresource_footprint(src.format), + }, + }; + + let src_box = make_box(&r.texture_base.origin, &r.size); + unsafe { + list.CopyTextureRegion(&dst_location, 0, 0, 0, &src_location, Some(&src_box)) + }; + }; + + let offset_alignment = self.shared.private_caps.texture_data_placement_alignment(); + + for r in regions { + let is_offset_aligned = r.buffer_layout.offset % offset_alignment == 0; + if is_offset_aligned { + copy_aligned(self, src, dst, r) + } else { + let orig_offset = r.buffer_layout.offset; + let (intermediate_to_dst_region, src) = unsafe { + self.buf_tex_intermediate( + r, + src.format, + |this, buf, size, intermediate_region| { + copy_aligned(this, src, buf, intermediate_region); + crate::BufferCopy { + src_offset: 0, + dst_offset: orig_offset, + size, + } + }, + ) + }; + + unsafe { + self.copy_buffer_to_buffer(&src, dst, [intermediate_to_dst_region].into_iter()); + } + + self.intermediate_copy_bufs.push(src); + }; + } + } + + unsafe fn begin_query(&mut self, set: &super::QuerySet, index: u32) { + unsafe { + self.list + .as_ref() + .unwrap() + .BeginQuery(&set.raw, set.raw_ty, index) + }; + } + unsafe fn end_query(&mut self, set: &super::QuerySet, index: u32) { + unsafe { + self.list + .as_ref() + .unwrap() + .EndQuery(&set.raw, set.raw_ty, index) + }; + } + unsafe fn write_timestamp(&mut self, set: &super::QuerySet, index: u32) { + unsafe { + self.list.as_ref().unwrap().EndQuery( + &set.raw, + Direct3D12::D3D12_QUERY_TYPE_TIMESTAMP, + index, + ) + }; + } + unsafe fn read_acceleration_structure_compact_size( + &mut self, + acceleration_structure: &super::AccelerationStructure, + buf: &super::Buffer, + ) { + let list = self + .list + .as_ref() + .unwrap() + .cast::<Direct3D12::ID3D12GraphicsCommandList4>() + .unwrap(); + unsafe { + list.EmitRaytracingAccelerationStructurePostbuildInfo( + &Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_DESC { + DestBuffer: buf.resource.GetGPUVirtualAddress(), + InfoType: Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_COMPACTED_SIZE, + }, + &[ + acceleration_structure.resource.GetGPUVirtualAddress() + ], + ) + } + } + unsafe fn reset_queries(&mut self, _set: &super::QuerySet, _range: Range<u32>) { + // nothing to do here + } + unsafe fn copy_query_results( + &mut self, + set: &super::QuerySet, + range: Range<u32>, + buffer: &super::Buffer, + offset: wgt::BufferAddress, + _stride: wgt::BufferSize, + ) { + unsafe { + self.list.as_ref().unwrap().ResolveQueryData( + &set.raw, + set.raw_ty, + range.start, + range.end - range.start, + &buffer.resource, + offset, + ) + }; + } + + // render + + unsafe fn begin_render_pass( + &mut self, + desc: &crate::RenderPassDescriptor<super::QuerySet, super::TextureView>, + ) -> Result<(), crate::DeviceError> { + unsafe { self.begin_pass(super::PassKind::Render, desc.label) }; + + // Start timestamp if any (before all other commands but after debug marker) + if let Some(timestamp_writes) = desc.timestamp_writes.as_ref() { + if let Some(index) = timestamp_writes.beginning_of_pass_write_index { + unsafe { + self.write_timestamp(timestamp_writes.query_set, index); + } + } + self.end_of_pass_timer_query = timestamp_writes + .end_of_pass_write_index + .map(|index| (timestamp_writes.query_set.raw.clone(), index)); + } + + let mut color_views = + [Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE { ptr: 0 }; crate::MAX_COLOR_ATTACHMENTS]; + let mut rtv_pool = self.rtv_pool.lock(); + for (rtv, cat) in color_views.iter_mut().zip(desc.color_attachments.iter()) { + if let Some(cat) = cat.as_ref() { + if cat.target.view.dimension == wgt::TextureViewDimension::D3 { + let desc = Direct3D12::D3D12_RENDER_TARGET_VIEW_DESC { + Format: cat.target.view.raw_format, + ViewDimension: Direct3D12::D3D12_RTV_DIMENSION_TEXTURE3D, + Anonymous: Direct3D12::D3D12_RENDER_TARGET_VIEW_DESC_0 { + Texture3D: Direct3D12::D3D12_TEX3D_RTV { + MipSlice: cat.target.view.mip_slice, + FirstWSlice: cat.depth_slice.unwrap(), + WSize: 1, + }, + }, + }; + let handle = rtv_pool.alloc_handle()?; + unsafe { + self.device.CreateRenderTargetView( + &cat.target.view.texture, + Some(&desc), + handle.raw, + ) + }; + *rtv = handle.raw; + self.temp_rtv_handles.push(handle); + } else { + *rtv = cat.target.view.handle_rtv.unwrap().raw; + } + } else { + *rtv = self.null_rtv_handle.raw; + } + } + drop(rtv_pool); + + let ds_view = desc.depth_stencil_attachment.as_ref().map(|ds| { + if ds.target.usage == wgt::TextureUses::DEPTH_STENCIL_WRITE { + ds.target.view.handle_dsv_rw.as_ref().unwrap().raw + } else { + ds.target.view.handle_dsv_ro.as_ref().unwrap().raw + } + }); + + let list = self.list.as_ref().unwrap(); + unsafe { + list.OMSetRenderTargets( + desc.color_attachments.len() as u32, + Some(color_views.as_ptr()), + false, + ds_view.as_ref().map(core::ptr::from_ref), + ) + }; + + self.pass.resolves.clear(); + for (rtv, cat) in color_views.iter().zip(desc.color_attachments.iter()) { + if let Some(cat) = cat.as_ref() { + if cat.ops.contains(crate::AttachmentOps::LOAD_CLEAR) { + let value = [ + cat.clear_value.r as f32, + cat.clear_value.g as f32, + cat.clear_value.b as f32, + cat.clear_value.a as f32, + ]; + unsafe { list.ClearRenderTargetView(*rtv, &value, None) }; + } + if let Some(ref target) = cat.resolve_target { + self.pass.resolves.push(super::PassResolve { + src: ( + cat.target.view.texture.clone(), + cat.target.view.subresource_index, + ), + dst: (target.view.texture.clone(), target.view.subresource_index), + format: target.view.raw_format, + }); + } + } + } + + if let Some(ref ds) = desc.depth_stencil_attachment { + let mut flags = Direct3D12::D3D12_CLEAR_FLAGS::default(); + let aspects = ds.target.view.aspects; + if ds.depth_ops.contains(crate::AttachmentOps::LOAD_CLEAR) + && aspects.contains(crate::FormatAspects::DEPTH) + { + flags |= Direct3D12::D3D12_CLEAR_FLAG_DEPTH; + } + if ds.stencil_ops.contains(crate::AttachmentOps::LOAD_CLEAR) + && aspects.contains(crate::FormatAspects::STENCIL) + { + flags |= Direct3D12::D3D12_CLEAR_FLAG_STENCIL; + } + + if let Some(ds_view) = ds_view { + if flags != Direct3D12::D3D12_CLEAR_FLAGS::default() { + unsafe { + list.ClearDepthStencilView( + ds_view, + flags, + ds.clear_value.0, + ds.clear_value.1 as u8, + None, + ) + } + } + } + } + + if let Some(multiview_mask) = desc.multiview_mask { + unsafe { + list.cast::<Direct3D12::ID3D12GraphicsCommandList2>() + .unwrap() + .SetViewInstanceMask(multiview_mask.get()); + } + } + + let raw_vp = Direct3D12::D3D12_VIEWPORT { + TopLeftX: 0.0, + TopLeftY: 0.0, + Width: desc.extent.width as f32, + Height: desc.extent.height as f32, + MinDepth: 0.0, + MaxDepth: 1.0, + }; + let raw_rect = Foundation::RECT { + left: 0, + top: 0, + right: desc.extent.width as i32, + bottom: desc.extent.height as i32, + }; + unsafe { list.RSSetViewports(core::slice::from_ref(&raw_vp)) }; + unsafe { list.RSSetScissorRects(core::slice::from_ref(&raw_rect)) }; + + Ok(()) + } + + unsafe fn end_render_pass(&mut self) { + if !self.pass.resolves.is_empty() { + let list = self.list.as_ref().unwrap(); + self.temp.barriers.clear(); + + // All the targets are expected to be in `COLOR_TARGET` state, + // but D3D12 has special source/destination states for the resolves. + for resolve in self.pass.resolves.iter() { + let barrier = Direct3D12::D3D12_RESOURCE_BARRIER { + Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, + Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE, + Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 { + // Note: this assumes `D3D12_RESOURCE_STATE_RENDER_TARGET`. + // If it's not the case, we can include the `TextureUses` in `PassResolve`. + Transition: mem::ManuallyDrop::new( + Direct3D12::D3D12_RESOURCE_TRANSITION_BARRIER { + pResource: unsafe { borrow_interface_temporarily(&resolve.src.0) }, + Subresource: resolve.src.1, + StateBefore: Direct3D12::D3D12_RESOURCE_STATE_RENDER_TARGET, + StateAfter: Direct3D12::D3D12_RESOURCE_STATE_RESOLVE_SOURCE, + }, + ), + }, + }; + self.temp.barriers.push(barrier); + let barrier = Direct3D12::D3D12_RESOURCE_BARRIER { + Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, + Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE, + Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 { + // Note: this assumes `D3D12_RESOURCE_STATE_RENDER_TARGET`. + // If it's not the case, we can include the `TextureUses` in `PassResolve`. + Transition: mem::ManuallyDrop::new( + Direct3D12::D3D12_RESOURCE_TRANSITION_BARRIER { + pResource: unsafe { borrow_interface_temporarily(&resolve.dst.0) }, + Subresource: resolve.dst.1, + StateBefore: Direct3D12::D3D12_RESOURCE_STATE_RENDER_TARGET, + StateAfter: Direct3D12::D3D12_RESOURCE_STATE_RESOLVE_DEST, + }, + ), + }, + }; + self.temp.barriers.push(barrier); + } + + if !self.temp.barriers.is_empty() { + profiling::scope!("ID3D12GraphicsCommandList::ResourceBarrier"); + unsafe { list.ResourceBarrier(&self.temp.barriers) }; + } + + for resolve in self.pass.resolves.iter() { + profiling::scope!("ID3D12GraphicsCommandList::ResolveSubresource"); + unsafe { + list.ResolveSubresource( + &resolve.dst.0, + resolve.dst.1, + &resolve.src.0, + resolve.src.1, + resolve.format, + ) + }; + } + + // Flip all the barriers to reverse, back into `COLOR_TARGET`. + for barrier in self.temp.barriers.iter_mut() { + let transition = unsafe { &mut *barrier.Anonymous.Transition }; + mem::swap(&mut transition.StateBefore, &mut transition.StateAfter); + } + if !self.temp.barriers.is_empty() { + profiling::scope!("ID3D12GraphicsCommandList::ResourceBarrier"); + unsafe { list.ResourceBarrier(&self.temp.barriers) }; + } + } + + self.write_pass_end_timestamp_if_requested(); + + unsafe { self.end_pass() }; + } + + unsafe fn set_bind_group( + &mut self, + layout: &super::PipelineLayout, + index: u32, + group: &super::BindGroup, + dynamic_offsets: &[wgt::DynamicOffset], + ) { + let info = layout.bind_group_infos[index as usize].as_ref().unwrap(); + let mut root_index = info.base_root_index as usize; + + // Bind CBV/SRC/UAV descriptor tables + if info.tables.contains(super::TableTypes::SRV_CBV_UAV) { + self.pass.root_elements[root_index] = + super::RootElement::DescriptorTable(group.handle_views.unwrap().gpu); + root_index += 1; + } + + let mut offsets_index = 0; + if let Some(dynamic_storage_buffer_offsets) = info.dynamic_storage_buffer_offsets.as_ref() { + let root_index = dynamic_storage_buffer_offsets.root_index; + let range = &dynamic_storage_buffer_offsets.range; + + if range.end > self.pass.dynamic_storage_buffer_offsets.len() { + self.pass + .dynamic_storage_buffer_offsets + .resize(range.end, 0); + } + + offsets_index += range.start; + + self.pass.root_elements[root_index as usize] = + super::RootElement::DynamicStorageBufferOffsets { + start: range.start, + end: range.end, + }; + + if self.pass.layout.signature == layout.shared.signature { + self.pass.dirty_root_elements |= 1 << root_index; + } else { + // D3D12 requires full reset on signature change + // but we don't reset it here since it will be reset below + }; + } + + // Bind root descriptors for dynamic uniform buffers + // or set root constants for offsets of dynamic storage buffers + for (&dynamic_buffer, &offset) in group.dynamic_buffers.iter().zip(dynamic_offsets) { + match dynamic_buffer { + super::DynamicBuffer::Uniform(gpu_base) => { + self.pass.root_elements[root_index] = + super::RootElement::DynamicUniformBuffer { + address: Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE { + ptr: gpu_base.ptr + offset as u64, + }, + }; + root_index += 1; + } + super::DynamicBuffer::Storage => { + self.pass.dynamic_storage_buffer_offsets[offsets_index] = offset; + offsets_index += 1; + } + } + } + + if self.pass.layout.signature == layout.shared.signature { + self.pass.dirty_root_elements |= (1 << root_index) - (1 << info.base_root_index); + } else { + // D3D12 requires full reset on signature change + self.reset_signature(&layout.shared); + }; + } + unsafe fn set_immediates( + &mut self, + layout: &super::PipelineLayout, + offset_bytes: u32, + data: &[u32], + ) { + let offset_words = offset_bytes as usize / 4; + + let info = layout.shared.immediates_info.as_ref().unwrap(); + + self.pass.root_elements[info.root_index as usize] = super::RootElement::Immediates; + + self.pass.immediates[offset_words..(offset_words + data.len())].copy_from_slice(data); + + if self.pass.layout.signature == layout.shared.signature { + self.pass.dirty_root_elements |= 1 << info.root_index; + } else { + // D3D12 requires full reset on signature change + self.reset_signature(&layout.shared); + }; + } + + unsafe fn insert_debug_marker(&mut self, label: &str) { + let (wide_label, size) = self.temp.prepare_marker(label); + unsafe { + self.list + .as_ref() + .unwrap() + .SetMarker(0, Some(wide_label.as_ptr().cast()), size) + }; + } + unsafe fn begin_debug_marker(&mut self, group_label: &str) { + let (wide_label, size) = self.temp.prepare_marker(group_label); + unsafe { + self.list + .as_ref() + .unwrap() + .BeginEvent(0, Some(wide_label.as_ptr().cast()), size) + }; + } + unsafe fn end_debug_marker(&mut self) { + unsafe { self.list.as_ref().unwrap().EndEvent() } + } + + unsafe fn set_render_pipeline(&mut self, pipeline: &super::RenderPipeline) { + let list = self.list.clone().unwrap(); + + if self.pass.layout.signature != pipeline.layout.signature { + // D3D12 requires full reset on signature change + unsafe { list.SetGraphicsRootSignature(pipeline.layout.signature.as_ref()) }; + self.reset_signature(&pipeline.layout); + }; + + unsafe { list.SetPipelineState(&pipeline.raw) }; + unsafe { list.IASetPrimitiveTopology(pipeline.topology) }; + + for (index, (vb, &stride)) in self + .pass + .vertex_buffers + .iter_mut() + .zip(pipeline.vertex_strides.iter()) + .enumerate() + { + if let Some(stride) = stride { + if vb.StrideInBytes != stride { + vb.StrideInBytes = stride; + self.pass.dirty_vertex_buffers |= 1 << index; + } + } + } + } + + unsafe fn set_index_buffer<'a>( + &mut self, + binding: crate::BufferBinding<'a, super::Buffer>, + format: wgt::IndexFormat, + ) { + let ibv = Direct3D12::D3D12_INDEX_BUFFER_VIEW { + BufferLocation: binding.resolve_address(), + SizeInBytes: binding.resolve_size().try_into().unwrap(), + Format: auxil::dxgi::conv::map_index_format(format), + }; + + unsafe { self.list.as_ref().unwrap().IASetIndexBuffer(Some(&ibv)) } + } + unsafe fn set_vertex_buffer<'a>( + &mut self, + index: u32, + binding: crate::BufferBinding<'a, super::Buffer>, + ) { + let vb = &mut self.pass.vertex_buffers[index as usize]; + vb.BufferLocation = binding.resolve_address(); + vb.SizeInBytes = binding.resolve_size().try_into().unwrap(); + self.pass.dirty_vertex_buffers |= 1 << index; + } + + unsafe fn set_viewport(&mut self, rect: &crate::Rect<f32>, depth_range: Range<f32>) { + let raw_vp = Direct3D12::D3D12_VIEWPORT { + TopLeftX: rect.x, + TopLeftY: rect.y, + Width: rect.w, + Height: rect.h, + MinDepth: depth_range.start, + MaxDepth: depth_range.end, + }; + unsafe { + self.list + .as_ref() + .unwrap() + .RSSetViewports(core::slice::from_ref(&raw_vp)) + } + } + unsafe fn set_scissor_rect(&mut self, rect: &crate::Rect<u32>) { + let raw_rect = Foundation::RECT { + left: rect.x as i32, + top: rect.y as i32, + right: (rect.x + rect.w) as i32, + bottom: (rect.y + rect.h) as i32, + }; + unsafe { + self.list + .as_ref() + .unwrap() + .RSSetScissorRects(core::slice::from_ref(&raw_rect)) + } + } + unsafe fn set_stencil_reference(&mut self, value: u32) { + unsafe { self.list.as_ref().unwrap().OMSetStencilRef(value) } + } + unsafe fn set_blend_constants(&mut self, color: &[f32; 4]) { + unsafe { self.list.as_ref().unwrap().OMSetBlendFactor(Some(color)) } + } + + unsafe fn draw( + &mut self, + first_vertex: u32, + vertex_count: u32, + first_instance: u32, + instance_count: u32, + ) { + unsafe { self.prepare_draw(first_vertex as i32, first_instance) }; + unsafe { + self.list.as_ref().unwrap().DrawInstanced( + vertex_count, + instance_count, + first_vertex, + first_instance, + ) + } + } + unsafe fn draw_indexed( + &mut self, + first_index: u32, + index_count: u32, + base_vertex: i32, + first_instance: u32, + instance_count: u32, + ) { + unsafe { self.prepare_draw(base_vertex, first_instance) }; + unsafe { + self.list.as_ref().unwrap().DrawIndexedInstanced( + index_count, + instance_count, + first_index, + base_vertex, + first_instance, + ) + } + } + unsafe fn draw_mesh_tasks( + &mut self, + group_count_x: u32, + group_count_y: u32, + group_count_z: u32, + ) { + self.prepare_dispatch([group_count_x, group_count_y, group_count_z]); + let cmd_list6: Direct3D12::ID3D12GraphicsCommandList6 = + self.list.as_ref().unwrap().cast().unwrap(); + unsafe { + cmd_list6.DispatchMesh(group_count_x, group_count_y, group_count_z); + } + } + unsafe fn draw_indirect( + &mut self, + buffer: &super::Buffer, + offset: wgt::BufferAddress, + draw_count: u32, + ) { + if self + .pass + .layout + .special_constants + .as_ref() + .and_then(|sc| sc.indirect_cmd_signatures.as_ref()) + .is_some() + { + unsafe { self.prepare_vertex_buffers() }; + self.update_root_elements(); + } else { + unsafe { self.prepare_draw(0, 0) }; + } + + let cmd_signature = &self + .pass + .layout + .special_constants + .as_ref() + .and_then(|sc| sc.indirect_cmd_signatures.as_ref()) + .unwrap_or_else(|| &self.shared.cmd_signatures) + .draw; + unsafe { + self.list.as_ref().unwrap().ExecuteIndirect( + cmd_signature, + draw_count, + &buffer.resource, + offset, + None, + 0, + ) + } + } + unsafe fn draw_indexed_indirect( + &mut self, + buffer: &super::Buffer, + offset: wgt::BufferAddress, + draw_count: u32, + ) { + if self + .pass + .layout + .special_constants + .as_ref() + .and_then(|sc| sc.indirect_cmd_signatures.as_ref()) + .is_some() + { + unsafe { self.prepare_vertex_buffers() }; + self.update_root_elements(); + } else { + unsafe { self.prepare_draw(0, 0) }; + } + + let cmd_signature = &self + .pass + .layout + .special_constants + .as_ref() + .and_then(|sc| sc.indirect_cmd_signatures.as_ref()) + .unwrap_or_else(|| &self.shared.cmd_signatures) + .draw_indexed; + unsafe { + self.list.as_ref().unwrap().ExecuteIndirect( + cmd_signature, + draw_count, + &buffer.resource, + offset, + None, + 0, + ) + } + } + unsafe fn draw_mesh_tasks_indirect( + &mut self, + buffer: &<Self::A as crate::Api>::Buffer, + offset: wgt::BufferAddress, + draw_count: u32, + ) { + self.prepare_dispatch([0; 3]); + let cmd_list6: Direct3D12::ID3D12GraphicsCommandList6 = + self.list.as_ref().unwrap().cast().unwrap(); + let Some(cmd_signature) = &self.shared.cmd_signatures.draw_mesh else { + panic!("Feature `MESH_SHADING` not enabled"); + }; + unsafe { + cmd_list6.ExecuteIndirect(cmd_signature, draw_count, &buffer.resource, offset, None, 0); + } + } + unsafe fn draw_indirect_count( + &mut self, + buffer: &super::Buffer, + offset: wgt::BufferAddress, + count_buffer: &super::Buffer, + count_offset: wgt::BufferAddress, + max_count: u32, + ) { + unsafe { self.prepare_draw(0, 0) }; + unsafe { + self.list.as_ref().unwrap().ExecuteIndirect( + &self.shared.cmd_signatures.draw, + max_count, + &buffer.resource, + offset, + &count_buffer.resource, + count_offset, + ) + } + } + unsafe fn draw_indexed_indirect_count( + &mut self, + buffer: &super::Buffer, + offset: wgt::BufferAddress, + count_buffer: &super::Buffer, + count_offset: wgt::BufferAddress, + max_count: u32, + ) { + unsafe { self.prepare_draw(0, 0) }; + unsafe { + self.list.as_ref().unwrap().ExecuteIndirect( + &self.shared.cmd_signatures.draw_indexed, + max_count, + &buffer.resource, + offset, + &count_buffer.resource, + count_offset, + ) + } + } + unsafe fn draw_mesh_tasks_indirect_count( + &mut self, + buffer: &<Self::A as crate::Api>::Buffer, + offset: wgt::BufferAddress, + count_buffer: &<Self::A as crate::Api>::Buffer, + count_offset: wgt::BufferAddress, + max_count: u32, + ) { + self.prepare_dispatch([0; 3]); + let cmd_list6: Direct3D12::ID3D12GraphicsCommandList6 = + self.list.as_ref().unwrap().cast().unwrap(); + let Some(ref command_signature) = self.shared.cmd_signatures.draw_mesh else { + panic!("Feature `MESH_SHADING` not enabled"); + }; + unsafe { + cmd_list6.ExecuteIndirect( + command_signature, + max_count, + &buffer.resource, + offset, + &count_buffer.resource, + count_offset, + ); + } + } + + // compute + + unsafe fn begin_compute_pass<'a>( + &mut self, + desc: &crate::ComputePassDescriptor<'a, super::QuerySet>, + ) { + unsafe { self.begin_pass(super::PassKind::Compute, desc.label) }; + + if let Some(timestamp_writes) = desc.timestamp_writes.as_ref() { + if let Some(index) = timestamp_writes.beginning_of_pass_write_index { + unsafe { + self.write_timestamp(timestamp_writes.query_set, index); + } + } + self.end_of_pass_timer_query = timestamp_writes + .end_of_pass_write_index + .map(|index| (timestamp_writes.query_set.raw.clone(), index)); + } + } + unsafe fn end_compute_pass(&mut self) { + self.write_pass_end_timestamp_if_requested(); + unsafe { self.end_pass() }; + } + + unsafe fn set_compute_pipeline(&mut self, pipeline: &super::ComputePipeline) { + let list = self.list.clone().unwrap(); + + if self.pass.layout.signature != pipeline.layout.signature { + // D3D12 requires full reset on signature change + unsafe { list.SetComputeRootSignature(pipeline.layout.signature.as_ref()) }; + self.reset_signature(&pipeline.layout); + }; + + unsafe { list.SetPipelineState(&pipeline.raw) } + } + + unsafe fn dispatch_workgroups(&mut self, count @ [x, y, z]: [u32; 3]) { + self.prepare_dispatch(count); + unsafe { self.list.as_ref().unwrap().Dispatch(x, y, z) } + } + + unsafe fn dispatch_workgroups_indirect( + &mut self, + buffer: &super::Buffer, + offset: wgt::BufferAddress, + ) { + if self + .pass + .layout + .special_constants + .as_ref() + .and_then(|sc| sc.indirect_cmd_signatures.as_ref()) + .is_some() + { + self.update_root_elements(); + } else { + self.prepare_dispatch([0; 3]); + } + + let cmd_signature = &self + .pass + .layout + .special_constants + .as_ref() + .and_then(|sc| sc.indirect_cmd_signatures.as_ref()) + .unwrap_or_else(|| &self.shared.cmd_signatures) + .dispatch; + unsafe { + self.list.as_ref().unwrap().ExecuteIndirect( + cmd_signature, + 1, + &buffer.resource, + offset, + None, + 0, + ) + } + } + + unsafe fn build_acceleration_structures<'a, T>( + &mut self, + _descriptor_count: u32, + descriptors: T, + ) where + super::Api: 'a, + T: IntoIterator< + Item = crate::BuildAccelerationStructureDescriptor< + 'a, + super::Buffer, + super::AccelerationStructure, + >, + >, + { + // Implement using `BuildRaytracingAccelerationStructure`: + // https://microsoft.github.io/DirectX-Specs/d3d/Raytracing.html#buildraytracingaccelerationstructure + let list = self + .list + .as_ref() + .unwrap() + .cast::<Direct3D12::ID3D12GraphicsCommandList4>() + .unwrap(); + for descriptor in descriptors { + // TODO: This is the same as getting build sizes apart from requiring buffers, should this be de-duped? + let mut geometry_desc; + let ty; + let inputs0; + let num_desc; + match descriptor.entries { + AccelerationStructureEntries::Instances(instances) => { + let desc_address = unsafe { + instances + .buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + } + instances.offset as u64; + ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL; + inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + InstanceDescs: desc_address, + }; + num_desc = instances.count; + } + AccelerationStructureEntries::Triangles(triangles) => { + geometry_desc = Vec::with_capacity(triangles.len()); + for triangle in triangles { + let transform_address = + triangle.transform.as_ref().map_or(0, |transform| unsafe { + transform.buffer.resource.GetGPUVirtualAddress() + + transform.offset as u64 + }); + let index_format = triangle + .indices + .as_ref() + .map_or(Dxgi::Common::DXGI_FORMAT_UNKNOWN, |indices| { + auxil::dxgi::conv::map_index_format(indices.format) + }); + let vertex_format = + auxil::dxgi::conv::map_vertex_format(triangle.vertex_format); + let index_count = + triangle.indices.as_ref().map_or(0, |indices| indices.count); + let index_address = triangle.indices.as_ref().map_or(0, |indices| unsafe { + indices + .buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + + indices.offset as u64 + }); + let vertex_address = unsafe { + triangle + .vertex_buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + + (triangle.first_vertex as u64 * triangle.vertex_stride) + }; + + let triangle_desc = Direct3D12::D3D12_RAYTRACING_GEOMETRY_TRIANGLES_DESC { + Transform3x4: transform_address, + IndexFormat: index_format, + VertexFormat: vertex_format, + IndexCount: index_count, + VertexCount: triangle.vertex_count, + IndexBuffer: index_address, + VertexBuffer: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { + StartAddress: vertex_address, + StrideInBytes: triangle.vertex_stride, + }, + }; + + geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { + Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES, + Flags: conv::map_acceleration_structure_geometry_flags(triangle.flags), + Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { + Triangles: triangle_desc, + }, + }) + } + ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL; + inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + pGeometryDescs: geometry_desc.as_ptr(), + }; + num_desc = geometry_desc.len() as u32; + } + AccelerationStructureEntries::AABBs(aabbs) => { + geometry_desc = Vec::with_capacity(aabbs.len()); + for aabb in aabbs { + let aabb_address = unsafe { + aabb.buffer + .expect("needs buffer to build") + .resource + .GetGPUVirtualAddress() + + (aabb.offset as u64 * aabb.stride) + }; + + let aabb_desc = Direct3D12::D3D12_RAYTRACING_GEOMETRY_AABBS_DESC { + AABBCount: aabb.count as u64, + AABBs: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { + StartAddress: aabb_address, + StrideInBytes: aabb.stride, + }, + }; + + geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { + Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_PROCEDURAL_PRIMITIVE_AABBS, + Flags: conv::map_acceleration_structure_geometry_flags(aabb.flags), + Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { + AABBs: aabb_desc, + }, + }) + } + ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL; + inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + pGeometryDescs: geometry_desc.as_ptr(), + }; + num_desc = geometry_desc.len() as u32; + } + }; + let acceleration_structure_inputs = + Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS { + Type: ty, + Flags: conv::map_acceleration_structure_build_flags( + descriptor.flags, + Some(descriptor.mode), + ), + NumDescs: num_desc, + DescsLayout: Direct3D12::D3D12_ELEMENTS_LAYOUT_ARRAY, + Anonymous: inputs0, + }; + + let dst_acceleration_structure_address = unsafe { + descriptor + .destination_acceleration_structure + .resource + .GetGPUVirtualAddress() + }; + let src_acceleration_structure_address = descriptor + .source_acceleration_structure + .as_ref() + .map_or(0, |source| unsafe { + source.resource.GetGPUVirtualAddress() + }); + let scratch_address = unsafe { + descriptor.scratch_buffer.resource.GetGPUVirtualAddress() + + descriptor.scratch_buffer_offset + }; + + let desc = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC { + DestAccelerationStructureData: dst_acceleration_structure_address, + Inputs: acceleration_structure_inputs, + SourceAccelerationStructureData: src_acceleration_structure_address, + ScratchAccelerationStructureData: scratch_address, + }; + unsafe { list.BuildRaytracingAccelerationStructure(&desc, None) }; + } + } + + unsafe fn place_acceleration_structure_barrier( + &mut self, + _barriers: crate::AccelerationStructureBarrier, + ) { + // TODO: This is not very optimal, we should be using [enhanced barriers](https://microsoft.github.io/DirectX-Specs/d3d/D3D12EnhancedBarriers.html) if possible + let list = self + .list + .as_ref() + .unwrap() + .cast::<Direct3D12::ID3D12GraphicsCommandList4>() + .unwrap(); + unsafe { + list.ResourceBarrier(&[Direct3D12::D3D12_RESOURCE_BARRIER { + Type: Direct3D12::D3D12_RESOURCE_BARRIER_TYPE_UAV, + Flags: Direct3D12::D3D12_RESOURCE_BARRIER_FLAG_NONE, + Anonymous: Direct3D12::D3D12_RESOURCE_BARRIER_0 { + UAV: mem::ManuallyDrop::new(Direct3D12::D3D12_RESOURCE_UAV_BARRIER { + pResource: Default::default(), + }), + }, + }]) + } + } + + unsafe fn copy_acceleration_structure_to_acceleration_structure( + &mut self, + src: &super::AccelerationStructure, + dst: &super::AccelerationStructure, + copy: wgt::AccelerationStructureCopy, + ) { + let list = self + .list + .as_ref() + .unwrap() + .cast::<Direct3D12::ID3D12GraphicsCommandList4>() + .unwrap(); + unsafe { + list.CopyRaytracingAccelerationStructure( + dst.resource.GetGPUVirtualAddress(), + src.resource.GetGPUVirtualAddress(), + conv::map_acceleration_structure_copy_mode(copy), + ) + } + } + + unsafe fn set_acceleration_structure_dependencies( + _command_buffers: &[&super::CommandBuffer], + _dependencies: &[&super::AccelerationStructure], + ) { + } + + unsafe fn begin_ray_tracing_pass(&mut self, _desc: &crate::RayTracingPassDescriptor) { + unreachable!("Ray tracing pipelines not supported") + } + + unsafe fn end_ray_tracing_pass(&mut self) { + unreachable!("Ray tracing pipelines not supported") + } + + unsafe fn set_ray_tracing_pipeline( + &mut self, + _pipeline: &<Self::A as crate::Api>::RayTracingPipeline, + ) { + unreachable!("Ray tracing pipelines not supported") + } + + unsafe fn trace_rays( + &mut self, + _count: [u32; 3], + _ray_generation_group_data: crate::PipelineGroupData<super::Buffer>, + _miss_group_data: crate::PipelineGroupData<super::Buffer>, + _intersection_group_data: crate::PipelineGroupData<super::Buffer>, + ) { + unreachable!("Ray tracing pipelines not supported") + } +} diff --git a/third_party/wgpu-hal/src/dx12/conv.rs b/third_party/wgpu-hal/src/dx12/conv.rs new file mode 100644 index 000000000..6cc2bec28 --- /dev/null +++ b/third_party/wgpu-hal/src/dx12/conv.rs @@ -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, + ) +} diff --git a/third_party/wgpu-hal/src/dx12/dcomp.rs b/third_party/wgpu-hal/src/dx12/dcomp.rs new file mode 100644 index 000000000..46ef3da4d --- /dev/null +++ b/third_party/wgpu-hal/src/dx12/dcomp.rs @@ -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, + }) + } +} diff --git a/third_party/wgpu-hal/src/dx12/descriptor.rs b/third_party/wgpu-hal/src/dx12/descriptor.rs new file mode 100644 index 000000000..f57ba46e1 --- /dev/null +++ b/third_party/wgpu-hal/src/dx12/descriptor.rs @@ -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)) +} diff --git a/third_party/wgpu-hal/src/dx12/device.rs b/third_party/wgpu-hal/src/dx12/device.rs new file mode 100644 index 000000000..63fb31395 --- /dev/null +++ b/third_party/wgpu-hal/src/dx12/device.rs @@ -0,0 +1,2691 @@ +use alloc::borrow::ToOwned; +use alloc::{ + borrow::Cow, + string::{String, ToString as _}, + sync::Arc, + vec::Vec, +}; +use arrayvec::ArrayVec; +use core::{ffi, num::NonZeroU32, ptr, time::Duration}; +use std::time::Instant; + +use bytemuck::TransparentWrapper; +use parking_lot::Mutex; +use windows::{ + core::Interface as _, + Win32::{ + Foundation, + Graphics::{Direct3D12, Dxgi}, + System::Threading, + }, +}; + +use super::{conv, descriptor, D3D12Lib}; +use crate::{ + auxil::{ + self, + dxgi::{name::ObjectExt as _, result::HResult as _}, + }, + dx12::{ + borrow_optional_interface_temporarily, pipeline_desc::RenderPipelineStateStreamDesc, + shader_compilation, suballocation, DCompLib, DynamicStorageBufferOffsets, Event, + ShaderCacheKey, ShaderCacheValue, + }, + AccelerationStructureEntries, TlasInstance, +}; + +// this has to match Naga's HLSL backend, and also needs to be null-terminated +const NAGA_LOCATION_SEMANTIC: &[u8] = c"LOC".to_bytes(); + +impl super::Device { + #[allow(clippy::too_many_arguments)] + pub(super) fn new( + adapter: auxil::dxgi::factory::DxgiAdapter, + raw: Direct3D12::ID3D12Device, + present_queue: Direct3D12::ID3D12CommandQueue, + features: wgt::Features, + limits: &wgt::Limits, + memory_hints: &wgt::MemoryHints, + private_caps: super::PrivateCapabilities, + library: &Arc<D3D12Lib>, + dcomp_lib: &Arc<DCompLib>, + memory_budget_thresholds: wgt::MemoryBudgetThresholds, + compiler_container: Arc<shader_compilation::CompilerContainer>, + backend_options: wgt::Dx12BackendOptions, + ) -> Result<Self, crate::DeviceError> { + if private_caps + .instance_flags + .contains(wgt::InstanceFlags::VALIDATION) + { + auxil::dxgi::exception::register_exception_handler(); + } + + let mem_allocator = + suballocation::Allocator::new(&raw, memory_hints, memory_budget_thresholds)?; + + let idle_fence: Direct3D12::ID3D12Fence = unsafe { + profiling::scope!("ID3D12Device::CreateFence"); + raw.CreateFence(0, Direct3D12::D3D12_FENCE_FLAG_NONE) + } + .into_device_result("Idle fence creation")?; + + let raw_desc = Direct3D12::D3D12_RESOURCE_DESC { + Dimension: Direct3D12::D3D12_RESOURCE_DIMENSION_BUFFER, + Alignment: 0, + Width: super::ZERO_BUFFER_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: Direct3D12::D3D12_RESOURCE_FLAG_NONE, + }; + + let heap_properties = Direct3D12::D3D12_HEAP_PROPERTIES { + Type: Direct3D12::D3D12_HEAP_TYPE_CUSTOM, + CPUPageProperty: Direct3D12::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE, + MemoryPoolPreference: match private_caps.memory_architecture { + super::MemoryArchitecture::Unified { .. } => Direct3D12::D3D12_MEMORY_POOL_L0, + super::MemoryArchitecture::NonUnified => Direct3D12::D3D12_MEMORY_POOL_L1, + }, + CreationNodeMask: 0, + VisibleNodeMask: 0, + }; + + profiling::scope!("Zero Buffer Allocation"); + let mut zero_buffer = None::<Direct3D12::ID3D12Resource>; + unsafe { + raw.CreateCommittedResource( + &heap_properties, + Direct3D12::D3D12_HEAP_FLAG_NONE, + &raw_desc, + Direct3D12::D3D12_RESOURCE_STATE_COMMON, + None, + &mut zero_buffer, + ) + } + .into_device_result("Zero buffer creation")?; + + let zero_buffer = zero_buffer.ok_or(crate::DeviceError::Unexpected)?; + + // Note: without `D3D12_HEAP_FLAG_CREATE_NOT_ZEROED` + // this resource is zeroed by default. + + // maximum number of CBV/SRV/UAV descriptors in heap for Tier 1 + let capacity_views = limits.max_non_sampler_bindings as u64; + + let draw_mesh = if features + .features_wgpu + .contains(wgt::FeaturesWGPU::EXPERIMENTAL_MESH_SHADER) + { + Some(Self::create_command_signature( + &raw, + None, + size_of::<wgt::DispatchIndirectArgs>(), + &[Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC { + Type: Direct3D12::D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH_MESH, + ..Default::default() + }], + 0, + )?) + } else { + None + }; + + let shared = super::DeviceShared { + adapter, + zero_buffer, + cmd_signatures: super::CommandSignatures { + draw: Self::create_command_signature( + &raw, + None, + size_of::<wgt::DrawIndirectArgs>(), + &[Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC { + Type: Direct3D12::D3D12_INDIRECT_ARGUMENT_TYPE_DRAW, + ..Default::default() + }], + 0, + )?, + draw_indexed: Self::create_command_signature( + &raw, + None, + size_of::<wgt::DrawIndexedIndirectArgs>(), + &[Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC { + Type: Direct3D12::D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED, + ..Default::default() + }], + 0, + )?, + draw_mesh, + dispatch: Self::create_command_signature( + &raw, + None, + size_of::<wgt::DispatchIndirectArgs>(), + &[Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC { + Type: Direct3D12::D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH, + ..Default::default() + }], + 0, + )?, + }, + heap_views: descriptor::GeneralHeap::new( + &raw, + Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, + capacity_views, + )?, + sampler_heap: super::sampler::SamplerHeap::new(&raw, &private_caps)?, + private_caps, + }; + + let mut rtv_pool = + descriptor::CpuPool::new(raw.clone(), Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE_RTV); + let null_rtv_handle = rtv_pool.alloc_handle()?; + // A null pResource is used to initialize a null descriptor, + // which guarantees D3D11-like null binding behavior (reading 0s, writes are discarded) + unsafe { + raw.CreateRenderTargetView( + None, + Some(&Direct3D12::D3D12_RENDER_TARGET_VIEW_DESC { + Format: Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM, + ViewDimension: Direct3D12::D3D12_RTV_DIMENSION_TEXTURE2D, + Anonymous: Direct3D12::D3D12_RENDER_TARGET_VIEW_DESC_0 { + Texture2D: Direct3D12::D3D12_TEX2D_RTV { + MipSlice: 0, + PlaneSlice: 0, + }, + }, + }), + null_rtv_handle.raw, + ) + }; + + Ok(super::Device { + raw: raw.clone(), + present_queue, + idler: super::Idler { fence: idle_fence }, + features, + shared: Arc::new(shared), + rtv_pool: Arc::new(Mutex::new(rtv_pool)), + dsv_pool: Mutex::new(descriptor::CpuPool::new( + raw.clone(), + Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE_DSV, + )), + srv_uav_pool: Mutex::new(descriptor::CpuPool::new( + raw.clone(), + Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, + )), + options: backend_options, + library: Arc::clone(library), + dcomp_lib: Arc::clone(dcomp_lib), + #[cfg(feature = "renderdoc")] + render_doc: Default::default(), + null_rtv_handle, + mem_allocator, + compiler_container, + shader_cache: Default::default(), + counters: Default::default(), + limits: limits.clone(), + }) + } + + fn create_command_signature( + raw: &Direct3D12::ID3D12Device, + root_signature: Option<&Direct3D12::ID3D12RootSignature>, + byte_stride: usize, + arguments: &[Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC], + node_mask: u32, + ) -> Result<Direct3D12::ID3D12CommandSignature, crate::DeviceError> { + let mut signature = None; + unsafe { + raw.CreateCommandSignature( + &Direct3D12::D3D12_COMMAND_SIGNATURE_DESC { + ByteStride: byte_stride as u32, + NumArgumentDescs: arguments.len() as u32, + pArgumentDescs: arguments.as_ptr(), + NodeMask: node_mask, + }, + root_signature, + &mut signature, + ) + } + .into_device_result("Command signature creation")?; + signature.ok_or(crate::DeviceError::Unexpected) + } + + // Blocks until the dedicated present queue is finished with all of its work. + // + // Once this method completes, the surface is able to be resized or deleted. + pub(super) unsafe fn wait_for_present_queue_idle(&self) -> Result<(), crate::DeviceError> { + let cur_value = unsafe { self.idler.fence.GetCompletedValue() }; + if cur_value == !0 { + return Err(crate::DeviceError::Lost); + } + + let event = Event::create(false, false)?; + + let value = cur_value + 1; + unsafe { self.present_queue.Signal(&self.idler.fence, value) } + .into_device_result("Signal")?; + let hr = unsafe { self.idler.fence.SetEventOnCompletion(value, event.0) }; + hr.into_device_result("Set event")?; + unsafe { Threading::WaitForSingleObject(event.0, Threading::INFINITE) }; + Ok(()) + } + + /// When generating the vertex shader, the fragment stage must be passed if it exists! + /// Otherwise, the generated HLSL may be incorrect since the fragment shader inputs are + /// allowed to be a subset of the vertex outputs. + fn load_shader( + &self, + stage: &crate::ProgrammableStage<super::ShaderModule>, + layout: &super::PipelineLayout, + naga_stage: naga::ShaderStage, + fragment_stage: Option<&crate::ProgrammableStage<super::ShaderModule>>, + ) -> Result<super::CompiledShader, crate::PipelineError> { + let stage_bit = auxil::map_naga_stage(naga_stage); + + let needs_temp_options = stage.zero_initialize_workgroup_memory + != layout.naga_options.zero_initialize_workgroup_memory + || stage.module.runtime_checks.bounds_checks != layout.naga_options.restrict_indexing + || !stage.module.runtime_checks.task_shader_dispatch_tracking + || !stage + .module + .runtime_checks + .mesh_shader_primitive_indices_clamp + || stage.module.runtime_checks.force_loop_bounding + != layout.naga_options.force_loop_bounding + || stage + .module + .runtime_checks + .ray_query_initialization_tracking + != layout.naga_options.ray_query_initialization_tracking; + let mut temp_options; + let naga_options = if needs_temp_options { + temp_options = layout.naga_options.clone(); + temp_options.zero_initialize_workgroup_memory = stage.zero_initialize_workgroup_memory; + temp_options.restrict_indexing = stage.module.runtime_checks.bounds_checks; + temp_options.force_loop_bounding = stage.module.runtime_checks.force_loop_bounding; + if !stage.module.runtime_checks.task_shader_dispatch_tracking { + temp_options.task_dispatch_limits = None; + } + temp_options.mesh_shader_primitive_indices_clamp = stage + .module + .runtime_checks + .mesh_shader_primitive_indices_clamp; + temp_options.ray_query_initialization_tracking = stage + .module + .runtime_checks + .ray_query_initialization_tracking; + &temp_options + } else { + &layout.naga_options + }; + + let key = match &stage.module.source { + super::ShaderModuleSource::Naga(naga_shader) => { + use naga::back::hlsl; + + let frag_ep = match fragment_stage { + Some(crate::ProgrammableStage { + module: + super::ShaderModule { + source: super::ShaderModuleSource::Naga(naga_shader), + .. + }, + entry_point, + .. + }) => Some( + hlsl::FragmentEntryPoint::new(&naga_shader.module, entry_point).ok_or( + crate::PipelineError::EntryPoint(naga::ShaderStage::Fragment), + ), + ), + _ => None, + } + .transpose()?; + let (module, info) = naga::back::pipeline_constants::process_overrides( + &naga_shader.module, + &naga_shader.info, + Some((naga_stage, stage.entry_point)), + stage.constants, + ) + .map_err(|e| { + crate::PipelineError::PipelineConstants(stage_bit, format!("HLSL: {e:?}")) + })?; + + let pipeline_options = hlsl::PipelineOptions { + entry_point: Some((naga_stage, stage.entry_point.to_string())), + }; + + //TODO: reuse the writer + let (source, entry_point) = { + let mut source = String::new(); + let mut writer = + hlsl::Writer::new(&mut source, naga_options, &pipeline_options); + + profiling::scope!("naga::back::hlsl::write"); + let mut reflection_info = writer + .write(&module, &info, frag_ep.as_ref()) + .map_err(|e| { + crate::PipelineError::Linkage(stage_bit, format!("HLSL: {e:?}")) + })?; + + assert_eq!(reflection_info.entry_point_names.len(), 1); + + let entry_point = reflection_info + .entry_point_names + .pop() + .unwrap() + .map_err(|e| crate::PipelineError::Linkage(stage_bit, format!("{e}")))?; + + (source, entry_point) + }; + log::debug!( + "Naga generated shader for {entry_point:?} at {naga_stage:?}:\n{source}" + ); + + ShaderCacheKey { + source, + entry_point, + stage: naga_stage, + shader_model: naga_options.shader_model, + } + } + super::ShaderModuleSource::HlslPassthrough(passthrough) => ShaderCacheKey { + source: passthrough.shader.clone(), + entry_point: stage.entry_point.to_string(), + stage: naga_stage, + shader_model: naga_options.shader_model, + }, + super::ShaderModuleSource::DxilPassthrough(passthrough) => { + return Ok(super::CompiledShader::Precompiled( + passthrough.shader.clone(), + )) + } + }; + + { + let mut shader_cache = self.shader_cache.lock(); + let nr_of_shaders_compiled = shader_cache.nr_of_shaders_compiled; + if let Some(value) = shader_cache.entries.get_mut(&key) { + value.last_used = nr_of_shaders_compiled; + return Ok(value.shader.clone()); + } + } + + let source_name = stage.module.raw_name.as_deref(); + + let full_stage = format!( + "{}_{}", + naga::back::hlsl::shader_stage_to_hlsl_str(naga_stage), + key.shader_model.to_str() + ); + + let compiled_shader = self.compiler_container.compile( + self, + &key.source, + source_name, + &key.entry_point, + stage_bit, + &full_stage, + )?; + + { + let mut shader_cache = self.shader_cache.lock(); + shader_cache.nr_of_shaders_compiled += 1; + let nr_of_shaders_compiled = shader_cache.nr_of_shaders_compiled; + let value = ShaderCacheValue { + last_used: nr_of_shaders_compiled, + shader: compiled_shader.clone(), + }; + shader_cache.entries.insert(key, value); + + // Retain all entries that have been used since we compiled the last 100 shaders. + if shader_cache.entries.len() > 200 { + shader_cache + .entries + .retain(|_, v| v.last_used >= nr_of_shaders_compiled - 100); + } + } + + Ok(compiled_shader) + } + + pub fn raw_device(&self) -> &Direct3D12::ID3D12Device { + &self.raw + } + + pub fn raw_queue(&self) -> &Direct3D12::ID3D12CommandQueue { + &self.present_queue + } + + pub unsafe fn texture_from_raw( + resource: Direct3D12::ID3D12Resource, + format: wgt::TextureFormat, + dimension: wgt::TextureDimension, + size: wgt::Extent3d, + mip_level_count: u32, + sample_count: u32, + ) -> super::Texture { + super::Texture { + resource, + format, + dimension, + size, + mip_level_count, + sample_count, + allocation: suballocation::Allocation::none( + suballocation::AllocationType::Texture, + format.theoretical_memory_footprint(size), + ), + plane_slice_override: None, + } + } + + pub unsafe fn buffer_from_raw( + resource: Direct3D12::ID3D12Resource, + size: wgt::BufferAddress, + ) -> super::Buffer { + super::Buffer { + resource, + size, + allocation: suballocation::Allocation::none( + suballocation::AllocationType::Buffer, + size, + ), + } + } +} + +impl crate::Device for super::Device { + type A = super::Api; + + unsafe fn create_buffer( + &self, + desc: &crate::BufferDescriptor, + ) -> Result<super::Buffer, crate::DeviceError> { + let mut desc = desc.clone(); + + if desc.usage.contains(wgt::BufferUses::UNIFORM) { + desc.size = desc + .size + .next_multiple_of(Direct3D12::D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT.into()) + } + + let (resource, allocation) = + suballocation::DeviceAllocationContext::from(self).create_buffer(&desc)?; + + self.counters.buffers.add(1); + + Ok(super::Buffer { + resource, + size: desc.size, + allocation, + }) + } + + unsafe fn destroy_buffer(&self, buffer: super::Buffer) { + suballocation::DeviceAllocationContext::from(self) + .free_resource(buffer.resource, buffer.allocation); + + self.counters.buffers.sub(1); + } + + unsafe fn add_raw_buffer(&self, _buffer: &super::Buffer) { + self.counters.buffers.add(1); + } + + unsafe fn map_buffer( + &self, + buffer: &super::Buffer, + range: crate::MemoryRange, + ) -> Result<crate::BufferMapping, crate::DeviceError> { + let mut ptr = ptr::null_mut(); + // TODO: 0 for subresource should be fine here until map and unmap buffer is subresource aware? + unsafe { buffer.resource.Map(0, None, Some(&mut ptr)) }.into_device_result("Map buffer")?; + + Ok(crate::BufferMapping { + ptr: ptr::NonNull::new(unsafe { ptr.offset(range.start as isize).cast::<u8>() }) + .unwrap(), + //TODO: double-check this. Documentation is a bit misleading - + // it implies that Map/Unmap is needed to invalidate/flush memory. + is_coherent: true, + }) + } + + unsafe fn unmap_buffer(&self, buffer: &super::Buffer) { + unsafe { buffer.resource.Unmap(0, None) }; + } + + unsafe fn flush_mapped_ranges<I>(&self, _buffer: &super::Buffer, _ranges: I) {} + unsafe fn invalidate_mapped_ranges<I>(&self, _buffer: &super::Buffer, _ranges: I) {} + + unsafe fn create_texture( + &self, + desc: &crate::TextureDescriptor, + ) -> Result<super::Texture, crate::DeviceError> { + let raw_desc = Direct3D12::D3D12_RESOURCE_DESC { + Dimension: conv::map_texture_dimension(desc.dimension), + Alignment: 0, + Width: desc.size.width as u64, + Height: desc.size.height, + DepthOrArraySize: desc.size.depth_or_array_layers as u16, + MipLevels: desc.mip_level_count as u16, + Format: auxil::dxgi::conv::map_texture_format_for_resource( + desc.format, + desc.usage, + !desc.view_formats.is_empty(), + self.shared + .private_caps + .casting_fully_typed_format_supported, + ), + SampleDesc: Dxgi::Common::DXGI_SAMPLE_DESC { + Count: desc.sample_count, + Quality: 0, + }, + Layout: Direct3D12::D3D12_TEXTURE_LAYOUT_UNKNOWN, + Flags: conv::map_texture_usage_to_resource_flags(desc.usage), + }; + + let (resource, allocation) = + suballocation::DeviceAllocationContext::from(self).create_texture(desc, raw_desc)?; + + self.counters.textures.add(1); + + Ok(super::Texture { + resource, + format: desc.format, + dimension: desc.dimension, + size: desc.size, + mip_level_count: desc.mip_level_count, + sample_count: desc.sample_count, + allocation, + plane_slice_override: None, + }) + } + + unsafe fn destroy_texture(&self, texture: super::Texture) { + suballocation::DeviceAllocationContext::from(self) + .free_resource(texture.resource, texture.allocation); + + self.counters.textures.sub(1); + } + + unsafe fn add_raw_texture(&self, _texture: &super::Texture) { + self.counters.textures.add(1); + } + + unsafe fn create_texture_view( + &self, + texture: &super::Texture, + desc: &crate::TextureViewDescriptor, + ) -> Result<super::TextureView, crate::DeviceError> { + let view_desc = desc.to_internal(texture); + + self.counters.texture_views.add(1); + + Ok(super::TextureView { + raw_format: view_desc.rtv_dsv_format, + aspects: view_desc.aspects, + dimension: desc.dimension, + texture: texture.resource.clone(), + subresource_index: texture.calc_subresource( + desc.range.base_mip_level, + desc.range.base_array_layer, + view_desc.plane_slice(), + ), + mip_slice: desc.range.base_mip_level, + handle_srv: if desc.usage.intersects(wgt::TextureUses::RESOURCE) { + match unsafe { view_desc.to_srv() } { + Some(raw_desc) => { + let handle = self.srv_uav_pool.lock().alloc_handle()?; + unsafe { + self.raw.CreateShaderResourceView( + &texture.resource, + Some(&raw_desc), + handle.raw, + ) + }; + Some(handle) + } + None => None, + } + } else { + None + }, + handle_uav: if desc.usage.intersects( + wgt::TextureUses::STORAGE_READ_ONLY + | wgt::TextureUses::STORAGE_WRITE_ONLY + | wgt::TextureUses::STORAGE_READ_WRITE, + ) { + match unsafe { view_desc.to_uav() } { + Some(raw_desc) => { + let handle = self.srv_uav_pool.lock().alloc_handle()?; + unsafe { + self.raw.CreateUnorderedAccessView( + &texture.resource, + None, + Some(&raw_desc), + handle.raw, + ); + } + Some(handle) + } + None => None, + } + } else { + None + }, + handle_rtv: if desc.usage.intersects(wgt::TextureUses::COLOR_TARGET) + && desc.dimension != wgt::TextureViewDimension::D3 + // 3D RTVs must be created in the render pass + { + let raw_desc = unsafe { view_desc.to_rtv() }; + let handle = self.rtv_pool.lock().alloc_handle()?; + unsafe { + self.raw + .CreateRenderTargetView(&texture.resource, Some(&raw_desc), handle.raw) + }; + Some(handle) + } else { + None + }, + handle_dsv_ro: if desc.usage.intersects(wgt::TextureUses::DEPTH_STENCIL_READ) { + let raw_desc = unsafe { view_desc.to_dsv(true) }; + let handle = self.dsv_pool.lock().alloc_handle()?; + unsafe { + self.raw + .CreateDepthStencilView(&texture.resource, Some(&raw_desc), handle.raw) + }; + Some(handle) + } else { + None + }, + handle_dsv_rw: if desc.usage.intersects(wgt::TextureUses::DEPTH_STENCIL_WRITE) { + let raw_desc = unsafe { view_desc.to_dsv(false) }; + let handle = self.dsv_pool.lock().alloc_handle()?; + unsafe { + self.raw + .CreateDepthStencilView(&texture.resource, Some(&raw_desc), handle.raw) + }; + Some(handle) + } else { + None + }, + }) + } + + unsafe fn destroy_texture_view(&self, view: super::TextureView) { + if view.handle_srv.is_some() || view.handle_uav.is_some() { + let mut pool = self.srv_uav_pool.lock(); + if let Some(handle) = view.handle_srv { + pool.free_handle(handle); + } + if let Some(handle) = view.handle_uav { + pool.free_handle(handle); + } + } + if let Some(handle) = view.handle_rtv { + self.rtv_pool.lock().free_handle(handle); + } + if view.handle_dsv_ro.is_some() || view.handle_dsv_rw.is_some() { + let mut pool = self.dsv_pool.lock(); + if let Some(handle) = view.handle_dsv_ro { + pool.free_handle(handle); + } + if let Some(handle) = view.handle_dsv_rw { + pool.free_handle(handle); + } + } + + self.counters.texture_views.sub(1); + } + + unsafe fn create_sampler( + &self, + desc: &crate::SamplerDescriptor, + ) -> Result<super::Sampler, crate::DeviceError> { + let reduction = match desc.compare { + Some(_) => Direct3D12::D3D12_FILTER_REDUCTION_TYPE_COMPARISON, + None => Direct3D12::D3D12_FILTER_REDUCTION_TYPE_STANDARD, + }; + let mut filter = Direct3D12::D3D12_FILTER( + (conv::map_filter_mode(desc.min_filter).0 << Direct3D12::D3D12_MIN_FILTER_SHIFT) + | (conv::map_filter_mode(desc.mag_filter).0 << Direct3D12::D3D12_MAG_FILTER_SHIFT) + | (conv::map_mipmap_filter_mode(desc.mipmap_filter).0 + << Direct3D12::D3D12_MIP_FILTER_SHIFT) + | (reduction.0 << Direct3D12::D3D12_FILTER_REDUCTION_TYPE_SHIFT), + ); + + if desc.anisotropy_clamp != 1 { + filter.0 |= Direct3D12::D3D12_FILTER_ANISOTROPIC.0; + }; + + let border_color = conv::map_border_color(desc.border_color); + + let raw_desc = Direct3D12::D3D12_SAMPLER_DESC { + Filter: filter, + AddressU: conv::map_address_mode(desc.address_modes[0]), + AddressV: conv::map_address_mode(desc.address_modes[1]), + AddressW: conv::map_address_mode(desc.address_modes[2]), + MipLODBias: 0f32, + MaxAnisotropy: desc.anisotropy_clamp as u32, + + ComparisonFunc: conv::map_comparison(desc.compare.unwrap_or_default()), + BorderColor: border_color, + MinLOD: desc.lod_clamp.start, + MaxLOD: desc.lod_clamp.end, + }; + + let index = self + .shared + .sampler_heap + .create_sampler(&self.raw, raw_desc)?; + + self.counters.samplers.add(1); + + Ok(super::Sampler { + index, + desc: raw_desc, + }) + } + + unsafe fn destroy_sampler(&self, sampler: super::Sampler) { + self.shared + .sampler_heap + .destroy_sampler(sampler.desc, sampler.index); + self.counters.samplers.sub(1); + } + + unsafe fn create_command_encoder( + &self, + desc: &crate::CommandEncoderDescriptor<super::Queue>, + ) -> Result<super::CommandEncoder, crate::DeviceError> { + let allocator: Direct3D12::ID3D12CommandAllocator = unsafe { + self.raw + .CreateCommandAllocator(Direct3D12::D3D12_COMMAND_LIST_TYPE_DIRECT) + } + .into_device_result("Command allocator creation")?; + + if let Some(label) = desc.label { + allocator.set_name(label)?; + } + + self.counters.command_encoders.add(1); + + Ok(super::CommandEncoder { + allocator, + device: self.raw.clone(), + shared: Arc::clone(&self.shared), + mem_allocator: self.mem_allocator.clone(), + rtv_pool: Arc::clone(&self.rtv_pool), + temp_rtv_handles: Vec::new(), + intermediate_copy_bufs: Vec::new(), + null_rtv_handle: self.null_rtv_handle, + list: None, + free_lists: Vec::new(), + pass: super::PassState::new(), + temp: super::Temp::default(), + end_of_pass_timer_query: None, + counters: Arc::clone(&self.counters), + }) + } + + unsafe fn create_bind_group_layout( + &self, + desc: &crate::BindGroupLayoutDescriptor, + ) -> Result<super::BindGroupLayout, crate::DeviceError> { + let mut num_views = 0; + let mut has_sampler_in_group = false; + for entry in desc.entries.iter() { + let count = entry.count.map_or(1, NonZeroU32::get); + match entry.ty { + wgt::BindingType::Buffer { + ty: wgt::BufferBindingType::Uniform, + has_dynamic_offset: true, + .. + } => {} + wgt::BindingType::Buffer { .. } + | wgt::BindingType::Texture { .. } + | wgt::BindingType::StorageTexture { .. } + | wgt::BindingType::AccelerationStructure { .. } => num_views += count, + wgt::BindingType::Sampler { .. } => has_sampler_in_group = true, + // Three texture planes and one params buffer + wgt::BindingType::ExternalTexture => num_views += 4 * count, + } + } + + if has_sampler_in_group { + num_views += 1; + } + + self.counters.bind_group_layouts.add(1); + + Ok(super::BindGroupLayout { + entries: desc.entries.to_vec(), + cpu_heap_views: if num_views != 0 { + let heap = descriptor::CpuHeap::new( + &self.raw, + Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, + num_views, + )?; + Some(heap) + } else { + None + }, + copy_counts: vec![1; num_views as usize], + }) + } + + unsafe fn destroy_bind_group_layout(&self, _bg_layout: super::BindGroupLayout) { + self.counters.bind_group_layouts.sub(1); + } + + unsafe fn create_pipeline_layout( + &self, + desc: &crate::PipelineLayoutDescriptor<super::BindGroupLayout>, + ) -> Result<super::PipelineLayout, crate::DeviceError> { + use naga::back::hlsl; + // Pipeline layouts are implemented as RootSignature for D3D12. + // + // Immediates are implemented as root constants. + // + // Each bind group layout might use one SRV/CBV/UAV descriptor table. + // With resources in the bind group layout using: + // - 1 CBV per non-dynamic uniform buffer + // - 1 SRV per acceleration structure + // - 1 SRV for all samplers in a bind group + // - 1 SRV per texture + // - 1 SRV per read-only storage buffer + // - 1 UAV per storage texture + // - 1 UAV per read-write storage buffer + // - 3 SRVs & 1 CBV per external texture + // + // Each dynamic uniform buffer takes up a CBV root descriptor. + // This is easier than trying to patch up the offset on the shader side. + // + // Each dynamic storage buffer is an SRV or UAV in the descriptor table + // and its dynamic offsets are passed via root constants. + // + // All samplers go into a single sampler descriptor table. + // + // 3 additional root constants are used to populate built-in (shader) inputs. + // + // Root signature layout: + // Root Constants: Parameter=0, Space=0 + // ... + // (bind group [0]) - Space=0 + // View descriptor table, if any + // Sampler buffer descriptor table, if any + // Root descriptors (for dynamic offset buffers) + // (bind group [1]) - Space=0 + // ... + // (bind group [2]) - Space=0 + // Special constant buffer: Space=0 + // Sampler descriptor tables: Space=0 + // SamplerState Array: Space=0, Register=0-2047 + // SamplerComparisonState Array: Space=0, Register=2048-4095 + + //TODO: put lower bind group indices further down the root signature. See: + // https://microsoft.github.io/DirectX-Specs/d3d/ResourceBinding.html#binding-model + // Currently impossible because wgpu-core only re-binds the descriptor sets based + // on Vulkan-like layout compatibility rules. + + let mut binding_map = hlsl::BindingMap::default(); + let mut sampler_buffer_binding_map = hlsl::SamplerIndexBufferBindingMap::default(); + let mut external_texture_binding_map = hlsl::ExternalTextureBindingMap::default(); + let mut bind_cbv = hlsl::BindTarget::default(); + let mut bind_srv = hlsl::BindTarget::default(); + let mut bind_uav = hlsl::BindTarget::default(); + let mut parameters = Vec::new(); + let mut immediates_target = None; + let mut immediates_info = None; + + if desc.immediate_size != 0 { + let parameter_index = parameters.len(); + let size = desc.immediate_size / 4; + parameters.push(Direct3D12::D3D12_ROOT_PARAMETER { + ParameterType: Direct3D12::D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS, + Anonymous: Direct3D12::D3D12_ROOT_PARAMETER_0 { + Constants: Direct3D12::D3D12_ROOT_CONSTANTS { + ShaderRegister: bind_cbv.register, + RegisterSpace: bind_cbv.space as u32, + Num32BitValues: size, + }, + }, + ShaderVisibility: Direct3D12::D3D12_SHADER_VISIBILITY_ALL, + }); + let binding = bind_cbv; + bind_cbv.register += 1; + immediates_info = Some(super::ImmediatesInfo { + root_index: parameter_index as u32, + size, + }); + immediates_target = Some(binding); + + bind_cbv.space += 1; + } + + let mut dynamic_storage_buffer_offsets_targets = alloc::collections::BTreeMap::new(); + let mut total_dynamic_storage_buffers = 0; + + // Collect the whole number of bindings we will create upfront. + // It allows us to preallocate enough storage to avoid reallocation, + // which could cause invalid pointers. + let mut total_non_dynamic_entries = 0_usize; + let mut sampler_in_any_bind_group = false; + for bgl in desc.bind_group_layouts { + let Some(bgl) = bgl else { + continue; + }; + + let mut sampler_in_bind_group = false; + + for entry in &bgl.entries { + match entry.ty { + wgt::BindingType::Buffer { + ty: wgt::BufferBindingType::Uniform, + has_dynamic_offset: true, + .. + } => {} + wgt::BindingType::Sampler(_) => sampler_in_bind_group = true, + // Three texture planes and one params buffer + wgt::BindingType::ExternalTexture => total_non_dynamic_entries += 4, + _ => total_non_dynamic_entries += 1, + } + } + + if sampler_in_bind_group { + // One for the sampler buffer + total_non_dynamic_entries += 1; + sampler_in_any_bind_group = true; + } + } + + if sampler_in_any_bind_group { + // Two for the sampler arrays themselves + total_non_dynamic_entries += 2; + } + + let mut ranges = Vec::with_capacity(total_non_dynamic_entries); + + let mut bind_group_infos = [const { None }; crate::MAX_BIND_GROUPS]; + for (index, bgl) in desc.bind_group_layouts.iter().enumerate() { + let Some(bgl) = bgl else { + continue; + }; + + let mut info = super::BindGroupInfo { + tables: super::TableTypes::empty(), + base_root_index: parameters.len() as u32, + dynamic_storage_buffer_offsets: None, + }; + + let mut visibility_view_static = wgt::ShaderStages::empty(); + let mut visibility_view_dynamic_uniform = wgt::ShaderStages::empty(); + let mut visibility_view_dynamic_storage = wgt::ShaderStages::empty(); + for entry in bgl.entries.iter() { + match entry.ty { + wgt::BindingType::Sampler { .. } => { + visibility_view_static |= wgt::ShaderStages::all() + } + wgt::BindingType::Buffer { + ty: wgt::BufferBindingType::Uniform, + has_dynamic_offset: true, + .. + } => visibility_view_dynamic_uniform |= entry.visibility, + wgt::BindingType::Buffer { + ty: wgt::BufferBindingType::Storage { .. }, + has_dynamic_offset: true, + .. + } => visibility_view_dynamic_storage |= entry.visibility, + _ => visibility_view_static |= entry.visibility, + } + } + + let mut dynamic_storage_buffers = 0; + + // SRV/CBV/UAV descriptor tables + let range_base = ranges.len(); + for entry in bgl.entries.iter() { + let count = entry.count.map_or(1, NonZeroU32::get); + if let wgt::BindingType::ExternalTexture = entry.ty { + // External textures need 3 SRVs (a texture for each plane) + // and 1 CBV for the parameters buffer. + let bind_target = hlsl::ExternalTextureBindTarget { + planes: core::array::from_fn(|_| hlsl::BindTarget { + register: { + let register = bind_srv.register; + bind_srv.register += count; + register + }, + ..bind_srv + }), + params: hlsl::BindTarget { + register: { + let register = bind_cbv.register; + bind_cbv.register += count; + register + }, + ..bind_cbv + }, + }; + external_texture_binding_map.insert( + naga::ResourceBinding { + group: index as u32, + binding: entry.binding, + }, + bind_target, + ); + for bt in bind_target.planes { + ranges.push(Direct3D12::D3D12_DESCRIPTOR_RANGE { + RangeType: Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SRV, + NumDescriptors: count, + BaseShaderRegister: bt.register, + RegisterSpace: bt.space as u32, + OffsetInDescriptorsFromTableStart: + Direct3D12::D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND, + }); + } + ranges.push(Direct3D12::D3D12_DESCRIPTOR_RANGE { + RangeType: Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_CBV, + NumDescriptors: count, + BaseShaderRegister: bind_target.params.register, + RegisterSpace: bind_target.params.space as u32, + OffsetInDescriptorsFromTableStart: + Direct3D12::D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND, + }); + } else { + let (range_ty, has_dynamic_offset) = match entry.ty { + wgt::BindingType::Buffer { + ty, + has_dynamic_offset: true, + .. + } => match ty { + wgt::BufferBindingType::Uniform => continue, + wgt::BufferBindingType::Storage { .. } => { + (conv::map_binding_type(&entry.ty), true) + } + }, + ref other => (conv::map_binding_type(other), false), + }; + let bt = match range_ty { + Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_CBV => &mut bind_cbv, + Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SRV => &mut bind_srv, + Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_UAV => &mut bind_uav, + Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER => continue, + _ => todo!(), + }; + + let binding_array_size = entry.count.map(NonZeroU32::get); + + let dynamic_storage_buffer_offsets_index = if has_dynamic_offset { + debug_assert!( + binding_array_size.is_none(), + "binding arrays and dynamic buffers are mutually exclusive" + ); + let ret = Some(dynamic_storage_buffers); + dynamic_storage_buffers += 1; + ret + } else { + None + }; + + binding_map.insert( + naga::ResourceBinding { + group: index as u32, + binding: entry.binding, + }, + hlsl::BindTarget { + binding_array_size, + dynamic_storage_buffer_offsets_index, + ..*bt + }, + ); + ranges.push(Direct3D12::D3D12_DESCRIPTOR_RANGE { + RangeType: range_ty, + NumDescriptors: count, + BaseShaderRegister: bt.register, + RegisterSpace: bt.space as u32, + OffsetInDescriptorsFromTableStart: + Direct3D12::D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND, + }); + bt.register += count; + } + } + + let mut sampler_index_within_bind_group = 0; + for entry in bgl.entries.iter() { + if let wgt::BindingType::Sampler(_) = entry.ty { + binding_map.insert( + naga::ResourceBinding { + group: index as u32, + binding: entry.binding, + }, + hlsl::BindTarget { + // Naga does not use the space field for samplers + space: 255, + register: sampler_index_within_bind_group, + binding_array_size: None, + dynamic_storage_buffer_offsets_index: None, + restrict_indexing: false, + }, + ); + sampler_index_within_bind_group += 1; + } + } + + if sampler_index_within_bind_group != 0 { + sampler_buffer_binding_map.insert( + hlsl::SamplerIndexBufferKey { + group: index as u32, + }, + bind_srv, + ); + ranges.push(Direct3D12::D3D12_DESCRIPTOR_RANGE { + RangeType: Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SRV, + NumDescriptors: 1, + BaseShaderRegister: bind_srv.register, + RegisterSpace: bind_srv.space as u32, + OffsetInDescriptorsFromTableStart: + Direct3D12::D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND, + }); + bind_srv.register += 1; + } + + if ranges.len() > range_base { + let range = &ranges[range_base..]; + parameters.push(Direct3D12::D3D12_ROOT_PARAMETER { + ParameterType: Direct3D12::D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE, + Anonymous: Direct3D12::D3D12_ROOT_PARAMETER_0 { + DescriptorTable: Direct3D12::D3D12_ROOT_DESCRIPTOR_TABLE { + NumDescriptorRanges: range.len() as u32, + pDescriptorRanges: range.as_ptr(), + }, + }, + ShaderVisibility: conv::map_visibility(visibility_view_static), + }); + info.tables |= super::TableTypes::SRV_CBV_UAV; + } + + // Root descriptors for dynamic uniform buffers + let dynamic_buffers_visibility = conv::map_visibility(visibility_view_dynamic_uniform); + for entry in bgl.entries.iter() { + match entry.ty { + wgt::BindingType::Buffer { + ty: wgt::BufferBindingType::Uniform, + has_dynamic_offset: true, + .. + } => {} + _ => continue, + }; + + binding_map.insert( + naga::ResourceBinding { + group: index as u32, + binding: entry.binding, + }, + hlsl::BindTarget { + binding_array_size: entry.count.map(NonZeroU32::get), + restrict_indexing: true, + ..bind_cbv + }, + ); + + parameters.push(Direct3D12::D3D12_ROOT_PARAMETER { + ParameterType: Direct3D12::D3D12_ROOT_PARAMETER_TYPE_CBV, + Anonymous: Direct3D12::D3D12_ROOT_PARAMETER_0 { + Descriptor: Direct3D12::D3D12_ROOT_DESCRIPTOR { + ShaderRegister: bind_cbv.register, + RegisterSpace: bind_cbv.space as u32, + }, + }, + ShaderVisibility: dynamic_buffers_visibility, + }); + + bind_cbv.register += entry.count.map_or(1, NonZeroU32::get); + } + + // Root constants for (offsets of) dynamic storage buffers + if dynamic_storage_buffers > 0 { + let parameter_index = parameters.len(); + + parameters.push(Direct3D12::D3D12_ROOT_PARAMETER { + ParameterType: Direct3D12::D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS, + Anonymous: Direct3D12::D3D12_ROOT_PARAMETER_0 { + Constants: Direct3D12::D3D12_ROOT_CONSTANTS { + ShaderRegister: bind_cbv.register, + RegisterSpace: bind_cbv.space as u32, + Num32BitValues: dynamic_storage_buffers, + }, + }, + ShaderVisibility: conv::map_visibility(visibility_view_dynamic_storage), + }); + + let binding = hlsl::OffsetsBindTarget { + space: bind_cbv.space, + register: bind_cbv.register, + size: dynamic_storage_buffers, + }; + + bind_cbv.register += 1; + + dynamic_storage_buffer_offsets_targets.insert(index as u32, binding); + info.dynamic_storage_buffer_offsets = Some(DynamicStorageBufferOffsets { + root_index: parameter_index as u32, + range: total_dynamic_storage_buffers as usize + ..total_dynamic_storage_buffers as usize + dynamic_storage_buffers as usize, + }); + total_dynamic_storage_buffers += dynamic_storage_buffers; + } + + bind_group_infos[index] = Some(info); + } + + let sampler_heap_target = hlsl::SamplerHeapBindTargets { + standard_samplers: hlsl::BindTarget { + space: 0, + register: 0, + binding_array_size: None, + dynamic_storage_buffer_offsets_index: None, + restrict_indexing: false, + }, + comparison_samplers: hlsl::BindTarget { + space: 0, + register: 2048, + binding_array_size: None, + dynamic_storage_buffer_offsets_index: None, + restrict_indexing: false, + }, + }; + + let mut sampler_heap_root_index = None; + if sampler_in_any_bind_group { + // Sampler descriptor tables + // + // We bind two sampler ranges pointing to the same descriptor heap, using two different register ranges. + // + // We bind them as normal samplers in registers 0-2047 and comparison samplers in registers 2048-4095. + // Tier 2 hardware guarantees that the type of sampler only needs to match if the sampler is actually + // accessed in the shader. As such, we can bind the same array of samplers to both registers. + // + // We do this because HLSL does not allow you to alias registers at all. + let range_base = ranges.len(); + // Standard samplers, registers 0-2047 + ranges.push(Direct3D12::D3D12_DESCRIPTOR_RANGE { + RangeType: Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, + NumDescriptors: 2048, + BaseShaderRegister: 0, + RegisterSpace: 0, + OffsetInDescriptorsFromTableStart: 0, + }); + // Comparison samplers, registers 2048-4095 + ranges.push(Direct3D12::D3D12_DESCRIPTOR_RANGE { + RangeType: Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, + NumDescriptors: 2048, + BaseShaderRegister: 2048, + RegisterSpace: 0, + OffsetInDescriptorsFromTableStart: 0, + }); + + let range = &ranges[range_base..]; + sampler_heap_root_index = Some(parameters.len() as super::RootIndex); + parameters.push(Direct3D12::D3D12_ROOT_PARAMETER { + ParameterType: Direct3D12::D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE, + Anonymous: Direct3D12::D3D12_ROOT_PARAMETER_0 { + DescriptorTable: Direct3D12::D3D12_ROOT_DESCRIPTOR_TABLE { + NumDescriptorRanges: range.len() as u32, + pDescriptorRanges: range.as_ptr(), + }, + }, + ShaderVisibility: Direct3D12::D3D12_SHADER_VISIBILITY_ALL, + }); + } + + // Ensure that we didn't reallocate! + debug_assert_eq!(ranges.len(), total_non_dynamic_entries); + + let (special_constants_root_index, special_constants_binding) = if desc.flags.intersects( + crate::PipelineLayoutFlags::FIRST_VERTEX_INSTANCE + | crate::PipelineLayoutFlags::NUM_WORK_GROUPS, + ) { + let parameter_index = parameters.len(); + parameters.push(Direct3D12::D3D12_ROOT_PARAMETER { + ParameterType: Direct3D12::D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS, + Anonymous: Direct3D12::D3D12_ROOT_PARAMETER_0 { + Constants: Direct3D12::D3D12_ROOT_CONSTANTS { + ShaderRegister: bind_cbv.register, + RegisterSpace: bind_cbv.space as u32, + Num32BitValues: 3, // 0 = first_vertex, 1 = first_instance, 2 = other + }, + }, + ShaderVisibility: Direct3D12::D3D12_SHADER_VISIBILITY_ALL, // really needed for VS and CS only, + }); + let binding = bind_cbv; + // This is the last time we use this, but lets increment + // it so if we add more later, the value behaves correctly. + + // This is an allow as it doesn't trigger on 1.90, hal's MSRV. + #[allow(unused_assignments)] + { + bind_cbv.register += 1; + } + (Some(parameter_index as u32), Some(binding)) + } else { + (None, None) + }; + + let blob = self.library.serialize_root_signature( + Direct3D12::D3D_ROOT_SIGNATURE_VERSION_1_0, + ¶meters, + &[], + Direct3D12::D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT, + )?; + + let raw = unsafe { + self.raw + .CreateRootSignature::<Direct3D12::ID3D12RootSignature>(0, blob.as_slice()) + } + .into_device_result("Root signature creation")?; + + let special_constants = if let Some(root_index) = special_constants_root_index { + let cmd_signatures = if desc + .flags + .contains(crate::PipelineLayoutFlags::INDIRECT_BUILTIN_UPDATE) + { + let constant_indirect_argument_desc = Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC { + Type: Direct3D12::D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT, + Anonymous: Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC_0 { + Constant: Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC_0_1 { + RootParameterIndex: root_index, + DestOffsetIn32BitValues: 0, + Num32BitValuesToSet: 3, + }, + }, + }; + let special_constant_buffer_args_len = size_of::<super::SpecialConstants>(); + + Some(super::CommandSignatures { + draw: Self::create_command_signature( + &self.raw, + Some(&raw), + special_constant_buffer_args_len + size_of::<wgt::DrawIndirectArgs>(), + &[ + constant_indirect_argument_desc, + Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC { + Type: Direct3D12::D3D12_INDIRECT_ARGUMENT_TYPE_DRAW, + ..Default::default() + }, + ], + 0, + )?, + draw_indexed: Self::create_command_signature( + &self.raw, + Some(&raw), + special_constant_buffer_args_len + + size_of::<wgt::DrawIndexedIndirectArgs>(), + &[ + constant_indirect_argument_desc, + Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC { + Type: Direct3D12::D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED, + ..Default::default() + }, + ], + 0, + )?, + // Mesh pipelines don't support updating special constants. + draw_mesh: None, + dispatch: Self::create_command_signature( + &self.raw, + Some(&raw), + special_constant_buffer_args_len + size_of::<wgt::DispatchIndirectArgs>(), + &[ + constant_indirect_argument_desc, + Direct3D12::D3D12_INDIRECT_ARGUMENT_DESC { + Type: Direct3D12::D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH, + ..Default::default() + }, + ], + 0, + )?, + }) + } else { + None + }; + Some(super::PipelineLayoutSpecialConstants { + root_index, + indirect_cmd_signatures: cmd_signatures, + }) + } else { + None + }; + + if let Some(label) = desc.label { + raw.set_name(label)?; + } + + self.counters.pipeline_layouts.add(1); + + Ok(super::PipelineLayout { + shared: super::PipelineLayoutShared { + signature: Some(raw), + total_root_elements: parameters.len() as super::RootIndex, + special_constants, + immediates_info, + sampler_heap_root_index, + }, + bind_group_infos, + naga_options: hlsl::Options { + shader_model: self.shared.private_caps.shader_model, + binding_map, + fake_missing_bindings: false, + special_constants_binding, + immediates_target, + dynamic_storage_buffer_offsets_targets, + zero_initialize_workgroup_memory: true, + restrict_indexing: true, + sampler_heap_target, + sampler_buffer_binding_map, + external_texture_binding_map, + force_loop_bounding: true, + task_dispatch_limits: Some(naga::back::TaskDispatchLimits { + max_mesh_workgroups_per_dim: self.limits.max_mesh_workgroups_per_dimension, + max_mesh_workgroups_total: self.limits.max_mesh_workgroup_total_count, + }), + mesh_shader_primitive_indices_clamp: true, + ray_query_initialization_tracking: true, + }, + }) + } + + unsafe fn destroy_pipeline_layout(&self, _pipeline_layout: super::PipelineLayout) { + self.counters.pipeline_layouts.sub(1); + } + + unsafe fn create_bind_group( + &self, + desc: &crate::BindGroupDescriptor< + super::BindGroupLayout, + super::Buffer, + super::Sampler, + super::TextureView, + super::AccelerationStructure, + >, + ) -> Result<super::BindGroup, crate::DeviceError> { + let mut cpu_views = desc + .layout + .cpu_heap_views + .as_ref() + .map(|cpu_heap| cpu_heap.inner.lock()); + if let Some(ref mut inner) = cpu_views { + inner.stage.clear(); + } + let mut dynamic_buffers = Vec::new(); + + let layout_and_entry_iter = desc.entries.iter().map(|entry| { + let layout = desc + .layout + .entries + .iter() + .find(|layout_entry| layout_entry.binding == entry.binding) + .expect("internal error: no layout entry found with binding slot"); + (layout, entry) + }); + let mut sampler_indexes: Vec<super::sampler::SamplerIndex> = Vec::new(); + + for (layout, entry) in layout_and_entry_iter { + match layout.ty { + wgt::BindingType::Buffer { + ty, + has_dynamic_offset, + .. + } => { + let start = entry.resource_index as usize; + let end = start + entry.count as usize; + for data in &desc.buffers[start..end] { + let gpu_address = data.resolve_address(); + let mut size = data.resolve_size().try_into().unwrap(); + + if has_dynamic_offset { + match ty { + wgt::BufferBindingType::Uniform => { + dynamic_buffers.push(super::DynamicBuffer::Uniform( + Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE { + ptr: data.resolve_address(), + }, + )); + continue; + } + wgt::BufferBindingType::Storage { .. } => { + size = (data.buffer.size - data.offset) as u32; + dynamic_buffers.push(super::DynamicBuffer::Storage); + } + } + } + + let inner = cpu_views.as_mut().unwrap(); + let cpu_index = inner.stage.len() as u32; + let handle = desc.layout.cpu_heap_views.as_ref().unwrap().at(cpu_index); + match ty { + wgt::BufferBindingType::Uniform => { + let size_mask = + Direct3D12::D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT - 1; + let raw_desc = Direct3D12::D3D12_CONSTANT_BUFFER_VIEW_DESC { + BufferLocation: gpu_address, + SizeInBytes: ((size - 1) | size_mask) + 1, + }; + unsafe { + self.raw.CreateConstantBufferView(Some(&raw_desc), handle) + }; + } + wgt::BufferBindingType::Storage { read_only: true } => { + let raw_desc = Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC { + Format: Dxgi::Common::DXGI_FORMAT_R32_TYPELESS, + Shader4ComponentMapping: + Direct3D12::D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, + ViewDimension: Direct3D12::D3D12_SRV_DIMENSION_BUFFER, + Anonymous: Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC_0 { + Buffer: Direct3D12::D3D12_BUFFER_SRV { + FirstElement: data.offset / 4, + NumElements: size / 4, + StructureByteStride: 0, + Flags: Direct3D12::D3D12_BUFFER_SRV_FLAG_RAW, + }, + }, + }; + unsafe { + self.raw.CreateShaderResourceView( + &data.buffer.resource, + Some(&raw_desc), + handle, + ) + }; + } + wgt::BufferBindingType::Storage { read_only: false } => { + let raw_desc = Direct3D12::D3D12_UNORDERED_ACCESS_VIEW_DESC { + Format: Dxgi::Common::DXGI_FORMAT_R32_TYPELESS, + ViewDimension: Direct3D12::D3D12_UAV_DIMENSION_BUFFER, + Anonymous: Direct3D12::D3D12_UNORDERED_ACCESS_VIEW_DESC_0 { + Buffer: Direct3D12::D3D12_BUFFER_UAV { + FirstElement: data.offset / 4, + NumElements: size / 4, + StructureByteStride: 0, + CounterOffsetInBytes: 0, + Flags: Direct3D12::D3D12_BUFFER_UAV_FLAG_RAW, + }, + }, + }; + unsafe { + self.raw.CreateUnorderedAccessView( + &data.buffer.resource, + None, + Some(&raw_desc), + handle, + ) + }; + } + } + inner.stage.push(handle); + } + } + wgt::BindingType::Texture { .. } => { + let start = entry.resource_index as usize; + let end = start + entry.count as usize; + for data in &desc.textures[start..end] { + let handle = data.view.handle_srv.unwrap(); + cpu_views.as_mut().unwrap().stage.push(handle.raw); + } + // The table range reserves the full `layout.count`, so pad a + // partially-bound array with null SRVs to keep later bindings aligned. + let array_size = layout.count.map_or(1, NonZeroU32::get); + for _ in entry.count..array_size { + let inner = cpu_views.as_mut().unwrap(); + let cpu_index = inner.stage.len() as u32; + let handle = desc.layout.cpu_heap_views.as_ref().unwrap().at(cpu_index); + let raw_desc = Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC { + Format: Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM, + ViewDimension: Direct3D12::D3D12_SRV_DIMENSION_TEXTURE2D, + Shader4ComponentMapping: + Direct3D12::D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, + Anonymous: Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC_0 { + Texture2D: Direct3D12::D3D12_TEX2D_SRV { + MostDetailedMip: 0, + MipLevels: 1, + PlaneSlice: 0, + ResourceMinLODClamp: 0.0, + }, + }, + }; + unsafe { + self.raw + .CreateShaderResourceView(None, Some(&raw_desc), handle) + }; + inner.stage.push(handle); + } + } + wgt::BindingType::StorageTexture { .. } => { + let start = entry.resource_index as usize; + let end = start + entry.count as usize; + for data in &desc.textures[start..end] { + let handle = data.view.handle_uav.unwrap(); + cpu_views.as_mut().unwrap().stage.push(handle.raw); + } + // Same partial-binding-array padding as the `Texture` branch above, but + // with null UAVs so that bindings following the array keep their offsets. + let array_size = layout.count.map_or(1, NonZeroU32::get); + for _ in entry.count..array_size { + let inner = cpu_views.as_mut().unwrap(); + let cpu_index = inner.stage.len() as u32; + let handle = desc.layout.cpu_heap_views.as_ref().unwrap().at(cpu_index); + let raw_desc = Direct3D12::D3D12_UNORDERED_ACCESS_VIEW_DESC { + Format: Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM, + ViewDimension: Direct3D12::D3D12_UAV_DIMENSION_TEXTURE2D, + Anonymous: Direct3D12::D3D12_UNORDERED_ACCESS_VIEW_DESC_0 { + Texture2D: Direct3D12::D3D12_TEX2D_UAV { + MipSlice: 0, + PlaneSlice: 0, + }, + }, + }; + unsafe { + self.raw + .CreateUnorderedAccessView(None, None, Some(&raw_desc), handle) + }; + inner.stage.push(handle); + } + } + wgt::BindingType::Sampler { .. } => { + let start = entry.resource_index as usize; + let end = start + entry.count as usize; + for &data in &desc.samplers[start..end] { + sampler_indexes.push(data.index); + } + } + wgt::BindingType::AccelerationStructure { .. } => { + let start = entry.resource_index as usize; + let end = start + entry.count as usize; + for data in &desc.acceleration_structures[start..end] { + let inner = cpu_views.as_mut().unwrap(); + let cpu_index = inner.stage.len() as u32; + let handle = desc.layout.cpu_heap_views.as_ref().unwrap().at(cpu_index); + let raw_desc = Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC { + Format: Dxgi::Common::DXGI_FORMAT_UNKNOWN, + Shader4ComponentMapping: + Direct3D12::D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, + ViewDimension: + Direct3D12::D3D12_SRV_DIMENSION_RAYTRACING_ACCELERATION_STRUCTURE, + Anonymous: Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC_0 { + RaytracingAccelerationStructure: + Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_SRV { + Location: unsafe { data.resource.GetGPUVirtualAddress() }, + }, + }, + }; + unsafe { + self.raw + .CreateShaderResourceView(None, Some(&raw_desc), handle) + }; + inner.stage.push(handle); + } + } + wgt::BindingType::ExternalTexture => { + // We don't yet support binding arrays of external textures. + // https://github.com/gfx-rs/wgpu/issues/8027 + assert_eq!(entry.count, 1); + let external_texture = &desc.external_textures[entry.resource_index as usize]; + for plane in &external_texture.planes { + let plane_handle = plane.view.handle_srv.unwrap(); + cpu_views.as_mut().unwrap().stage.push(plane_handle.raw); + } + let gpu_address = external_texture.params.resolve_address(); + let size = external_texture.params.resolve_size() as u32; + let inner = cpu_views.as_mut().unwrap(); + let cpu_index = inner.stage.len() as u32; + let params_handle = desc.layout.cpu_heap_views.as_ref().unwrap().at(cpu_index); + let size_mask = Direct3D12::D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT - 1; + let raw_desc = Direct3D12::D3D12_CONSTANT_BUFFER_VIEW_DESC { + BufferLocation: gpu_address, + SizeInBytes: ((size - 1) | size_mask) + 1, + }; + unsafe { + self.raw + .CreateConstantBufferView(Some(&raw_desc), params_handle) + }; + inner.stage.push(params_handle); + } + } + } + + let sampler_index_buffer = if !sampler_indexes.is_empty() { + let buffer_size = (sampler_indexes.len() * size_of::<u32>()) as u64; + + let label = if let Some(label) = desc.label { + Cow::Owned(format!("{label} (Internal Sampler Index Buffer)")) + } else { + Cow::Borrowed("Internal Sampler Index Buffer") + }; + + let buffer_desc = crate::BufferDescriptor { + label: Some(&label), + size: buffer_size, + usage: wgt::BufferUses::STORAGE_READ_ONLY | wgt::BufferUses::MAP_WRITE, + // D3D12 backend doesn't care about the memory flags + memory_flags: crate::MemoryFlags::empty(), + }; + + let (buffer, allocation) = + suballocation::DeviceAllocationContext::from(self).create_buffer(&buffer_desc)?; + + let mut mapping = ptr::null_mut::<ffi::c_void>(); + unsafe { buffer.Map(0, None, Some(&mut mapping)) }.into_device_result("Map")?; + + assert!(!mapping.is_null()); + assert_eq!(mapping as usize % 4, 0); + + unsafe { + ptr::copy_nonoverlapping( + sampler_indexes.as_ptr(), + mapping.cast(), + sampler_indexes.len(), + ) + }; + + // The unmapping is not needed, as all memory is coherent in d3d12, but lets be nice to our address space. + unsafe { buffer.Unmap(0, None) }; + + let srv_desc = Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC { + Format: Dxgi::Common::DXGI_FORMAT_UNKNOWN, + ViewDimension: Direct3D12::D3D12_SRV_DIMENSION_BUFFER, + Anonymous: Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC_0 { + Buffer: Direct3D12::D3D12_BUFFER_SRV { + FirstElement: 0, + NumElements: sampler_indexes.len() as u32, + StructureByteStride: 4, + Flags: Direct3D12::D3D12_BUFFER_SRV_FLAG_NONE, + }, + }, + Shader4ComponentMapping: Direct3D12::D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, + }; + + let inner = cpu_views.as_mut().unwrap(); + let cpu_index = inner.stage.len() as u32; + let srv = desc.layout.cpu_heap_views.as_ref().unwrap().at(cpu_index); + + unsafe { + self.raw + .CreateShaderResourceView(&buffer, Some(&srv_desc), srv) + }; + + cpu_views.as_mut().unwrap().stage.push(srv); + + Some(super::SamplerIndexBuffer { buffer, allocation }) + } else { + None + }; + + let handle_views = match cpu_views { + Some(inner) => { + let dual = unsafe { + descriptor::upload( + &self.raw, + &inner, + &self.shared.heap_views, + &desc.layout.copy_counts, + ) + }?; + Some(dual) + } + None => None, + }; + + self.counters.bind_groups.add(1); + + Ok(super::BindGroup { + handle_views, + sampler_index_buffer, + dynamic_buffers, + }) + } + + unsafe fn destroy_bind_group(&self, group: super::BindGroup) { + if let Some(dual) = group.handle_views { + self.shared.heap_views.free_slice(dual); + } + + if let Some(sampler_buffer) = group.sampler_index_buffer { + suballocation::DeviceAllocationContext::from(self) + .free_resource(sampler_buffer.buffer, sampler_buffer.allocation); + } + + self.counters.bind_groups.sub(1); + } + + unsafe fn create_shader_module( + &self, + desc: &crate::ShaderModuleDescriptor, + shader: crate::ShaderInput, + ) -> Result<super::ShaderModule, crate::ShaderError> { + self.counters.shader_modules.add(1); + + let raw_name = desc + .label + .and_then(|label| alloc::ffi::CString::new(label).ok()); + match shader { + crate::ShaderInput::Naga(naga) => Ok(super::ShaderModule { + source: super::ShaderModuleSource::Naga(naga), + raw_name, + runtime_checks: desc.runtime_checks, + }), + crate::ShaderInput::Dxil { shader } => Ok(super::ShaderModule { + source: super::ShaderModuleSource::DxilPassthrough(super::DxilPassthroughShader { + shader: shader.to_vec(), + }), + raw_name, + runtime_checks: desc.runtime_checks, + }), + crate::ShaderInput::Hlsl { shader } => Ok(super::ShaderModule { + source: super::ShaderModuleSource::HlslPassthrough(super::HlslPassthroughShader { + shader: shader.to_owned(), + }), + raw_name, + runtime_checks: desc.runtime_checks, + }), + crate::ShaderInput::SpirV(_) + | crate::ShaderInput::MetalLib { .. } + | crate::ShaderInput::Msl { .. } + | crate::ShaderInput::Glsl { .. } => { + unreachable!() + } + } + } + unsafe fn destroy_shader_module(&self, _module: super::ShaderModule) { + self.counters.shader_modules.sub(1); + // just drop + } + + unsafe fn create_render_pipeline( + &self, + desc: &crate::RenderPipelineDescriptor< + super::PipelineLayout, + super::ShaderModule, + super::PipelineCache, + >, + ) -> Result<super::RenderPipeline, crate::PipelineError> { + let mut shader_stages = wgt::ShaderStages::empty(); + let (topology_class, topology) = conv::map_topology(desc.primitive.topology); + let mut rtv_formats = [Dxgi::Common::DXGI_FORMAT_UNKNOWN; + Direct3D12::D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT as usize]; + for (rtv_format, ct) in rtv_formats.iter_mut().zip(desc.color_targets) { + if let Some(ct) = ct.as_ref() { + *rtv_format = auxil::dxgi::conv::map_texture_format(ct.format); + } + } + + let bias = desc + .depth_stencil + .as_ref() + .map(|ds| ds.bias) + .unwrap_or_default(); + + let rasterizer_state = Direct3D12::D3D12_RASTERIZER_DESC { + FillMode: conv::map_polygon_mode(desc.primitive.polygon_mode), + CullMode: match desc.primitive.cull_mode { + None => Direct3D12::D3D12_CULL_MODE_NONE, + Some(wgt::Face::Front) => Direct3D12::D3D12_CULL_MODE_FRONT, + Some(wgt::Face::Back) => Direct3D12::D3D12_CULL_MODE_BACK, + }, + FrontCounterClockwise: match desc.primitive.front_face { + wgt::FrontFace::Cw => Foundation::FALSE, + wgt::FrontFace::Ccw => Foundation::TRUE, + }, + DepthBias: bias.constant, + DepthBiasClamp: bias.clamp, + SlopeScaledDepthBias: bias.slope_scale, + DepthClipEnable: windows_core::BOOL::from(!desc.primitive.unclipped_depth), + MultisampleEnable: windows_core::BOOL::from(desc.multisample.count > 1), + ForcedSampleCount: 0, + AntialiasedLineEnable: false.into(), + ConservativeRaster: if desc.primitive.conservative { + Direct3D12::D3D12_CONSERVATIVE_RASTERIZATION_MODE_ON + } else { + Direct3D12::D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF + }, + }; + + let blob_fs = match desc.fragment_stage { + Some(ref stage) => { + shader_stages |= wgt::ShaderStages::FRAGMENT; + Some(self.load_shader(stage, desc.layout, naga::ShaderStage::Fragment, None)?) + } + None => None, + }; + let pixel_shader = match &blob_fs { + Some(shader) => shader.create_native_shader(), + None => Direct3D12::D3D12_SHADER_BYTECODE::default(), + }; + let stream_output = Direct3D12::D3D12_STREAM_OUTPUT_DESC { + pSODeclaration: ptr::null(), + NumEntries: 0, + pBufferStrides: ptr::null(), + NumStrides: 0, + RasterizedStream: 0, + }; + let blend_state = Direct3D12::D3D12_BLEND_DESC { + AlphaToCoverageEnable: windows_core::BOOL::from( + desc.multisample.alpha_to_coverage_enabled, + ), + IndependentBlendEnable: true.into(), + RenderTarget: conv::map_render_targets(desc.color_targets), + }; + let depth_stencil_state = match desc.depth_stencil { + Some(ref ds) => conv::map_depth_stencil(ds), + None => Default::default(), + }; + let dsv_format = desc + .depth_stencil + .as_ref() + .map_or(Dxgi::Common::DXGI_FORMAT_UNKNOWN, |ds| { + auxil::dxgi::conv::map_texture_format(ds.format) + }); + let sample_desc = Dxgi::Common::DXGI_SAMPLE_DESC { + Count: desc.multisample.count, + Quality: 0, + }; + let cached_pso = Direct3D12::D3D12_CACHED_PIPELINE_STATE { + pCachedBlob: ptr::null(), + CachedBlobSizeInBytes: 0, + }; + let flags = Direct3D12::D3D12_PIPELINE_STATE_FLAG_NONE; + + let mut view_instancing = ArrayVec::<Direct3D12::D3D12_VIEW_INSTANCE_LOCATION, 32>::new(); + if let Some(mask) = desc.multiview_mask { + let mask = mask.get(); + // This array is just what _could_ be rendered to. We actually apply the mask at + // renderpass creation time. The `view_index` passed to the shader depends on the + // view's index in this array, so if we include every view in this array, `view_index` + // actually the texture array layer, like in vulkan. + for i in 0..32 - mask.leading_zeros() { + view_instancing.push(Direct3D12::D3D12_VIEW_INSTANCE_LOCATION { + ViewportArrayIndex: 0, + RenderTargetArrayIndex: i, + }); + } + } + + // Borrow view instancing slice, so we can be sure that it won't be moved while we have pointers into this buffer. + let view_instancing_slice = view_instancing.as_slice(); + + let mut stream_desc = RenderPipelineStateStreamDesc { + // Shared by vertex and mesh pipelines + root_signature: desc.layout.shared.signature.as_ref(), + pixel_shader, + blend_state, + sample_mask: desc.multisample.mask as u32, + rasterizer_state, + depth_stencil_state, + primitive_topology_type: topology_class, + rtv_formats: Direct3D12::D3D12_RT_FORMAT_ARRAY { + RTFormats: rtv_formats, + NumRenderTargets: desc.color_targets.len() as u32, + }, + dsv_format, + sample_desc, + node_mask: 0, + cached_pso, + flags, + view_instancing: if !view_instancing_slice.is_empty() { + Some(Direct3D12::D3D12_VIEW_INSTANCING_DESC { + ViewInstanceCount: view_instancing_slice.len() as u32, + pViewInstanceLocations: view_instancing_slice.as_ptr(), + // This lets us hide/mask certain values later, at renderpass creation time. + Flags: Direct3D12::D3D12_VIEW_INSTANCING_FLAG_ENABLE_VIEW_INSTANCE_MASKING, + }) + } else { + None + }, + + // Optional data that depends on the pipeline type (vertex vs mesh). + vertex_shader: Default::default(), + input_layout: Default::default(), + index_buffer_strip_cut_value: Default::default(), + stream_output, + task_shader: Default::default(), + mesh_shader: Default::default(), + }; + let mut input_element_descs = Vec::new(); + let blob_vs; + let blob_ts; + let blob_ms; + let mut vertex_strides = [None; crate::MAX_VERTEX_BUFFERS]; + match &desc.vertex_processor { + &crate::VertexProcessor::Standard { + vertex_buffers, + ref vertex_stage, + } => { + shader_stages |= wgt::ShaderStages::VERTEX; + blob_vs = Some(self.load_shader( + vertex_stage, + desc.layout, + naga::ShaderStage::Vertex, + desc.fragment_stage.as_ref(), + )?); + + for (i, (stride, vbuf)) in vertex_strides.iter_mut().zip(vertex_buffers).enumerate() + { + let Some(vbuf) = vbuf else { + continue; + }; + *stride = Some(vbuf.array_stride as u32); + let (slot_class, step_rate) = match vbuf.step_mode { + wgt::VertexStepMode::Vertex => { + (Direct3D12::D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0) + } + wgt::VertexStepMode::Instance => { + (Direct3D12::D3D12_INPUT_CLASSIFICATION_PER_INSTANCE_DATA, 1) + } + }; + for attribute in vbuf.attributes { + input_element_descs.push(Direct3D12::D3D12_INPUT_ELEMENT_DESC { + SemanticName: windows::core::PCSTR(NAGA_LOCATION_SEMANTIC.as_ptr()), + SemanticIndex: attribute.shader_location, + Format: auxil::dxgi::conv::map_vertex_format(attribute.format), + InputSlot: i as u32, + AlignedByteOffset: attribute.offset as u32, + InputSlotClass: slot_class, + InstanceDataStepRate: step_rate, + }); + } + } + stream_desc.vertex_shader = blob_vs.as_ref().unwrap().create_native_shader(); + stream_desc.input_layout = Direct3D12::D3D12_INPUT_LAYOUT_DESC { + pInputElementDescs: if input_element_descs.is_empty() { + ptr::null() + } else { + input_element_descs.as_ptr() + }, + NumElements: input_element_descs.len() as u32, + }; + stream_desc.index_buffer_strip_cut_value = match desc.primitive.strip_index_format { + Some(wgt::IndexFormat::Uint16) => { + Direct3D12::D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFF + } + Some(wgt::IndexFormat::Uint32) => { + Direct3D12::D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFFFFFF + } + None => Direct3D12::D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_DISABLED, + }; + stream_desc.stream_output = Direct3D12::D3D12_STREAM_OUTPUT_DESC { + pSODeclaration: ptr::null(), + NumEntries: 0, + pBufferStrides: ptr::null(), + NumStrides: 0, + RasterizedStream: 0, + }; + } + crate::VertexProcessor::Mesh { + task_stage, + mesh_stage, + } => { + blob_ts = if let Some(ts) = task_stage { + shader_stages |= wgt::ShaderStages::TASK; + Some(self.load_shader( + ts, + desc.layout, + naga::ShaderStage::Task, + desc.fragment_stage.as_ref(), + )?) + } else { + None + }; + let task_shader = if let Some(ts) = &blob_ts { + ts.create_native_shader() + } else { + Default::default() + }; + shader_stages |= wgt::ShaderStages::MESH; + blob_ms = Some(self.load_shader( + mesh_stage, + desc.layout, + naga::ShaderStage::Mesh, + desc.fragment_stage.as_ref(), + )?); + stream_desc.task_shader = task_shader; + stream_desc.mesh_shader = blob_ms.as_ref().unwrap().create_native_shader(); + } + }; + let raw: Direct3D12::ID3D12PipelineState = + // If stream descriptors are available, use them as they are more flexible. + if let Ok(device) = self.raw.cast::<Direct3D12::ID3D12Device2>() { + // Prefer stream descs where possible + let mut stream = stream_desc.to_stream(); + unsafe { + profiling::scope!("ID3D12Device2::CreatePipelineState"); + stream.create_pipeline_state(&device).map_err(|err| { + crate::PipelineError::Linkage(shader_stages, err.to_string()) + })? + } + } else { + unsafe { + // Safety: `stream_desc` entirely outlives the `desc`. + let desc = stream_desc.to_graphics_pipeline_descriptor(); + self.raw.CreateGraphicsPipelineState(&desc).map_err(|err| { + crate::PipelineError::Linkage(shader_stages, err.to_string()) + })? + } + }; + + if let Some(label) = desc.label { + raw.set_name(label)?; + } + + self.counters.render_pipelines.add(1); + + Ok(super::RenderPipeline { + raw, + layout: desc.layout.shared.clone(), + topology, + vertex_strides, + }) + } + + unsafe fn destroy_render_pipeline(&self, _pipeline: super::RenderPipeline) { + self.counters.render_pipelines.sub(1); + } + + unsafe fn create_compute_pipeline( + &self, + desc: &crate::ComputePipelineDescriptor< + super::PipelineLayout, + super::ShaderModule, + super::PipelineCache, + >, + ) -> Result<super::ComputePipeline, crate::PipelineError> { + let blob_cs = + self.load_shader(&desc.stage, desc.layout, naga::ShaderStage::Compute, None)?; + + let pair = { + profiling::scope!("ID3D12Device::CreateComputePipelineState"); + unsafe { + self.raw.CreateComputePipelineState( + &Direct3D12::D3D12_COMPUTE_PIPELINE_STATE_DESC { + pRootSignature: borrow_optional_interface_temporarily( + &desc.layout.shared.signature, + ), + CS: blob_cs.create_native_shader(), + NodeMask: 0, + CachedPSO: Direct3D12::D3D12_CACHED_PIPELINE_STATE::default(), + Flags: Direct3D12::D3D12_PIPELINE_STATE_FLAG_NONE, + }, + ) + } + }; + + let raw: Direct3D12::ID3D12PipelineState = pair.map_err(|err| { + crate::PipelineError::Linkage(wgt::ShaderStages::COMPUTE, err.to_string()) + })?; + + if let Some(label) = desc.label { + raw.set_name(label)?; + } + + self.counters.compute_pipelines.add(1); + + Ok(super::ComputePipeline { + raw, + layout: desc.layout.shared.clone(), + }) + } + + unsafe fn destroy_compute_pipeline(&self, _pipeline: super::ComputePipeline) { + self.counters.compute_pipelines.sub(1); + } + + unsafe fn create_ray_tracing_pipeline( + &self, + _desc: &crate::RayTracingPipelineDescriptor< + super::PipelineLayout, + super::ShaderModule, + super::PipelineCache, + >, + ) -> Result<<Self::A as crate::Api>::RayTracingPipeline, crate::PipelineError> { + unreachable!("ray tracing pipelines not yet implemented") + } + + unsafe fn destroy_ray_tracing_pipeline(&self, _pipeline: super::RayTracingPipeline) { + unreachable!("ray tracing pipelines not yet implemented") + } + + unsafe fn get_raytracing_pipeline_group_data( + &self, + _pipeline: &super::RayTracingPipeline, + _groups: core::ops::Range<u32>, + ) -> Result<Vec<u8>, crate::DeviceError> { + unimplemented!("ray tracing pipelines not yet implemented") + } + + unsafe fn create_pipeline_cache( + &self, + _desc: &crate::PipelineCacheDescriptor<'_>, + ) -> Result<super::PipelineCache, crate::PipelineCacheError> { + Ok(super::PipelineCache) + } + unsafe fn destroy_pipeline_cache(&self, _: super::PipelineCache) {} + + unsafe fn create_query_set( + &self, + desc: &wgt::QuerySetDescriptor<crate::Label>, + ) -> Result<super::QuerySet, crate::DeviceError> { + let (heap_ty, raw_ty) = match desc.ty { + wgt::QueryType::Occlusion => ( + Direct3D12::D3D12_QUERY_HEAP_TYPE_OCCLUSION, + Direct3D12::D3D12_QUERY_TYPE_BINARY_OCCLUSION, + ), + wgt::QueryType::PipelineStatistics(_) => ( + Direct3D12::D3D12_QUERY_HEAP_TYPE_PIPELINE_STATISTICS, + Direct3D12::D3D12_QUERY_TYPE_PIPELINE_STATISTICS, + ), + wgt::QueryType::Timestamp => ( + Direct3D12::D3D12_QUERY_HEAP_TYPE_TIMESTAMP, + Direct3D12::D3D12_QUERY_TYPE_TIMESTAMP, + ), + }; + + if let Some(threshold) = self + .mem_allocator + .memory_budget_thresholds + .for_resource_creation + { + let info = self + .shared + .adapter + .query_video_memory_info(Dxgi::DXGI_MEMORY_SEGMENT_GROUP_LOCAL)?; + + // Assume each query is 256 bytes. + // On an AMD W6800 with driver version 32.0.12030.9, occlusion and pipeline statistics are 256, timestamp is 8. + + if info.CurrentUsage + desc.count as u64 * 256 >= info.Budget / 100 * threshold as u64 { + return Err(crate::DeviceError::OutOfMemory); + } + } + + let mut raw = None::<Direct3D12::ID3D12QueryHeap>; + unsafe { + self.raw.CreateQueryHeap( + &Direct3D12::D3D12_QUERY_HEAP_DESC { + Type: heap_ty, + Count: desc.count, + NodeMask: 0, + }, + &mut raw, + ) + } + .into_device_result("Query heap creation")?; + + let raw = raw.ok_or(crate::DeviceError::Unexpected)?; + + if let Some(label) = desc.label { + raw.set_name(label)?; + } + + self.counters.query_sets.add(1); + + Ok(super::QuerySet { raw, raw_ty }) + } + + unsafe fn destroy_query_set(&self, _set: super::QuerySet) { + self.counters.query_sets.sub(1); + } + + unsafe fn create_fence(&self) -> Result<super::Fence, crate::DeviceError> { + let raw: Direct3D12::ID3D12Fence = + unsafe { self.raw.CreateFence(0, Direct3D12::D3D12_FENCE_FLAG_SHARED) } + .into_device_result("Fence creation")?; + + self.counters.fences.add(1); + + Ok(super::Fence { raw }) + } + unsafe fn destroy_fence(&self, _fence: super::Fence) { + self.counters.fences.sub(1); + } + + unsafe fn get_fence_value( + &self, + fence: &super::Fence, + ) -> Result<crate::FenceValue, crate::DeviceError> { + Ok(unsafe { fence.raw.GetCompletedValue() }) + } + unsafe fn wait( + &self, + fence: &super::Fence, + value: crate::FenceValue, + timeout: Option<Duration>, + ) -> Result<bool, crate::DeviceError> { + let timeout = timeout.unwrap_or(Duration::MAX); + + // We first check if the fence has already reached the value we're waiting for. + let mut fence_value = unsafe { fence.raw.GetCompletedValue() }; + if fence_value >= value { + return Ok(true); + } + + let event = Event::create(false, false)?; + + unsafe { fence.raw.SetEventOnCompletion(value, event.0) } + .into_device_result("Set event")?; + + let start_time = Instant::now(); + + // We need to loop to get correct behavior when timeouts are involved. + // + // wait(0): + // - We set the event from the fence value 0. + // - WaitForSingleObject times out, we return false. + // + // wait(1): + // - We set the event from the fence value 1. + // - WaitForSingleObject returns. However we do not know if the fence value is 0 or 1, + // just that _something_ triggered the event. We check the fence value, and if it is + // 1, we return true. Otherwise, we loop and wait again. + loop { + let elapsed = start_time.elapsed(); + + // We need to explicitly use checked_sub. Overflow with duration panics, and if the + // timing works out just right, we can get a negative remaining wait duration. + // + // This happens when a previous iteration WaitForSingleObject succeeded with a previous fence value, + // right before the timeout would have been hit. + let remaining_wait_duration = match timeout.checked_sub(elapsed) { + Some(remaining) => remaining, + None => { + log::trace!("Timeout elapsed in between waits!"); + break Ok(false); + } + }; + + log::trace!("Waiting for fence value {value} for {remaining_wait_duration:?}"); + + match unsafe { + Threading::WaitForSingleObject( + event.0, + remaining_wait_duration.as_millis().min(u32::MAX as u128) as u32, + ) + } { + Foundation::WAIT_OBJECT_0 => {} + Foundation::WAIT_ABANDONED | Foundation::WAIT_FAILED => { + log::error!("Wait failed!"); + break Err(crate::DeviceError::Lost); + } + Foundation::WAIT_TIMEOUT => { + log::trace!("Wait timed out!"); + break Ok(false); + } + other => { + log::error!("Unexpected wait status: 0x{other:?}"); + break Err(crate::DeviceError::Lost); + } + }; + + fence_value = unsafe { fence.raw.GetCompletedValue() }; + log::trace!("Wait complete! Fence actual value: {fence_value}"); + + if fence_value >= value { + break Ok(true); + } + } + } + + unsafe fn start_graphics_debugger_capture(&self) -> bool { + #[cfg(feature = "renderdoc")] + { + unsafe { + self.render_doc + .start_frame_capture(self.raw.as_raw(), ptr::null_mut()) + } + } + #[cfg(not(feature = "renderdoc"))] + false + } + + unsafe fn stop_graphics_debugger_capture(&self) { + #[cfg(feature = "renderdoc")] + unsafe { + self.render_doc + .end_frame_capture(self.raw.as_raw(), ptr::null_mut()) + } + } + + unsafe fn get_acceleration_structure_build_sizes<'a>( + &self, + desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, super::Buffer>, + ) -> crate::AccelerationStructureBuildSizes { + let mut geometry_desc; + let device5 = self.raw.cast::<Direct3D12::ID3D12Device5>().unwrap(); + let ty; + let inputs0; + let num_desc; + match desc.entries { + AccelerationStructureEntries::Instances(instances) => { + ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL; + inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + InstanceDescs: 0, + }; + num_desc = instances.count; + } + AccelerationStructureEntries::Triangles(triangles) => { + geometry_desc = Vec::with_capacity(triangles.len()); + for triangle in triangles { + let index_format = triangle + .indices + .as_ref() + .map_or(Dxgi::Common::DXGI_FORMAT_UNKNOWN, |indices| { + auxil::dxgi::conv::map_index_format(indices.format) + }); + let index_count = triangle.indices.as_ref().map_or(0, |indices| indices.count); + + let triangle_desc = Direct3D12::D3D12_RAYTRACING_GEOMETRY_TRIANGLES_DESC { + // https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12device5-getraytracingaccelerationstructureprebuildinfo + // It may not inspect/dereference any GPU virtual addresses, other than + // to check to see if a pointer is NULL or not, such as the optional + // transform in D3D12_RAYTRACING_GEOMETRY_TRIANGLES_DESC, without + // dereferencing it. + // + // This suggests we could pass a non-zero invalid address here if fetching the + // real address has significant overhead, but we pass the real one to be on the + // safe side for now. + Transform3x4: if desc + .flags + .contains(wgt::AccelerationStructureFlags::USE_TRANSFORM) + { + unsafe { + triangle + .transform + .as_ref() + .unwrap() + .buffer + .resource + .GetGPUVirtualAddress() + } + } else { + 0 + }, + IndexFormat: index_format, + VertexFormat: auxil::dxgi::conv::map_vertex_format(triangle.vertex_format), + IndexCount: index_count, + VertexCount: triangle.vertex_count, + IndexBuffer: 0, + VertexBuffer: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { + StartAddress: 0, + StrideInBytes: triangle.vertex_stride, + }, + }; + + geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { + Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES, + Flags: conv::map_acceleration_structure_geometry_flags(triangle.flags), + Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { + Triangles: triangle_desc, + }, + }) + } + ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL; + inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + pGeometryDescs: geometry_desc.as_ptr(), + }; + num_desc = geometry_desc.len() as u32; + } + AccelerationStructureEntries::AABBs(aabbs) => { + geometry_desc = Vec::with_capacity(aabbs.len()); + for aabb in aabbs { + let aabb_desc = Direct3D12::D3D12_RAYTRACING_GEOMETRY_AABBS_DESC { + AABBCount: aabb.count as u64, + AABBs: Direct3D12::D3D12_GPU_VIRTUAL_ADDRESS_AND_STRIDE { + StartAddress: 0, + StrideInBytes: aabb.stride, + }, + }; + geometry_desc.push(Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC { + Type: Direct3D12::D3D12_RAYTRACING_GEOMETRY_TYPE_PROCEDURAL_PRIMITIVE_AABBS, + Flags: conv::map_acceleration_structure_geometry_flags(aabb.flags), + Anonymous: Direct3D12::D3D12_RAYTRACING_GEOMETRY_DESC_0 { + AABBs: aabb_desc, + }, + }) + } + ty = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL; + inputs0 = Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS_0 { + pGeometryDescs: geometry_desc.as_ptr(), + }; + num_desc = geometry_desc.len() as u32; + } + }; + let acceleration_structure_inputs = + Direct3D12::D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS { + Type: ty, + Flags: conv::map_acceleration_structure_build_flags(desc.flags, None), + NumDescs: num_desc, + DescsLayout: Direct3D12::D3D12_ELEMENTS_LAYOUT_ARRAY, + Anonymous: inputs0, + }; + let mut info = Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO::default(); + unsafe { + device5.GetRaytracingAccelerationStructurePrebuildInfo( + &acceleration_structure_inputs, + &mut info, + ) + }; + crate::AccelerationStructureBuildSizes { + acceleration_structure_size: info.ResultDataMaxSizeInBytes, + update_scratch_size: info.UpdateScratchDataSizeInBytes, + build_scratch_size: info.ScratchDataSizeInBytes, + } + } + + unsafe fn get_acceleration_structure_device_address( + &self, + acceleration_structure: &super::AccelerationStructure, + ) -> wgt::BufferAddress { + unsafe { acceleration_structure.resource.GetGPUVirtualAddress() } + } + + unsafe fn create_acceleration_structure( + &self, + desc: &crate::AccelerationStructureDescriptor, + ) -> Result<super::AccelerationStructure, crate::DeviceError> { + // Create a D3D12 resource as per-usual. + let size = desc.size; + + let raw_desc = Direct3D12::D3D12_RESOURCE_DESC { + Dimension: Direct3D12::D3D12_RESOURCE_DIMENSION_BUFFER, + Alignment: 0, + Width: 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, + // TODO: when moving to enhanced barriers use Direct3D12::D3D12_RESOURCE_FLAG_RAYTRACING_ACCELERATION_STRUCTURE + Flags: Direct3D12::D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, + }; + + let (resource, allocation) = suballocation::DeviceAllocationContext::from(self) + .create_acceleration_structure(desc, raw_desc)?; + + // for some reason there is no counter for acceleration structures + + Ok(super::AccelerationStructure { + resource, + allocation, + }) + } + + unsafe fn destroy_acceleration_structure( + &self, + acceleration_structure: super::AccelerationStructure, + ) { + suballocation::DeviceAllocationContext::from(self).free_resource( + acceleration_structure.resource, + acceleration_structure.allocation, + ); + } + + fn get_internal_counters(&self) -> wgt::HalCounters { + self.counters.as_ref().clone() + } + + fn generate_allocator_report(&self) -> Option<wgt::AllocatorReport> { + Some(self.mem_allocator.generate_report()) + } + + fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec<u8> { + const MAX_U24: u32 = (1u32 << 24u32) - 1u32; + let temp = Direct3D12::D3D12_RAYTRACING_INSTANCE_DESC { + Transform: instance.transform, + _bitfield1: (instance.custom_data & MAX_U24) | (u32::from(instance.mask) << 24), + _bitfield2: (instance.pipeline_intersection_data_offset & MAX_U24), + AccelerationStructure: instance.blas_address, + }; + + wgt::bytemuck_wrapper!(unsafe struct Desc(Direct3D12::D3D12_RAYTRACING_INSTANCE_DESC)); + + bytemuck::bytes_of(&Desc::wrap(temp)).to_vec() + } + + fn check_if_oom(&self) -> Result<(), crate::DeviceError> { + let Some(threshold) = self.mem_allocator.memory_budget_thresholds.for_device_loss else { + return Ok(()); + }; + + let info = self + .shared + .adapter + .query_video_memory_info(Dxgi::DXGI_MEMORY_SEGMENT_GROUP_LOCAL)?; + + if info.CurrentUsage >= info.Budget / 100 * threshold as u64 { + return Err(crate::DeviceError::OutOfMemory); + } + + if matches!( + self.shared.private_caps.memory_architecture, + super::MemoryArchitecture::NonUnified + ) { + let info = self + .shared + .adapter + .query_video_memory_info(Dxgi::DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL)?; + + if info.CurrentUsage >= info.Budget / 100 * threshold as u64 { + return Err(crate::DeviceError::OutOfMemory); + } + } + + Ok(()) + } +} diff --git a/third_party/wgpu-hal/src/dx12/device_creation.rs b/third_party/wgpu-hal/src/dx12/device_creation.rs new file mode 100644 index 000000000..dc5cdf9e2 --- /dev/null +++ b/third_party/wgpu-hal/src/dx12/device_creation.rs @@ -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), +} diff --git a/third_party/wgpu-hal/src/dx12/instance.rs b/third_party/wgpu-hal/src/dx12/instance.rs new file mode 100644 index 000000000..fdb63bcb6 --- /dev/null +++ b/third_party/wgpu-hal/src/dx12/instance.rs @@ -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() + } +} diff --git a/third_party/wgpu-hal/src/dx12/mod.rs b/third_party/wgpu-hal/src/dx12/mod.rs new file mode 100644 index 000000000..285d63566 --- /dev/null +++ b/third_party/wgpu-hal/src/dx12/mod.rs @@ -0,0 +1,1908 @@ +/*! +# DirectX12 API internals. + +Generally the mapping is straightforward. + +## Resource transitions + +D3D12 API matches WebGPU internal states very well. The only +caveat here is issuing a special UAV barrier whenever both source +and destination states match, and they are for storage sync. + +## Memory + +For now, all resources are created with "committed" memory. + +## Sampler Descriptor Management + +At most one descriptor heap of each type can be bound at once. This +means that the descriptors from all bind groups need to be present +in the same heap, and they need to be contiguous within that heap. +This is not a problem for the SRV/CBV/UAV heap as it can be sized into +the millions of entries. However the sampler heap is limited to 2048 entries. + +In order to work around this limitation, we refer to samplers indirectly by index. +The entire sampler heap is bound at once and a buffer containing all sampler indexes +for that bind group is bound. The shader then uses the index to look up the sampler +in the heap. To help visualize this, the generated HLSL looks like this: + +```wgsl +@group(0) @binding(2) var myLinearSampler: sampler; +@group(1) @binding(1) var myAnisoSampler: sampler; +@group(1) @binding(4) var myCompSampler: sampler; +``` + +```cpp +// These bindings alias the same descriptors. Depending on the type, the shader will use the correct one. +SamplerState nagaSamplerHeap[2048]: register(s0, space0); +SamplerComparisonState nagaComparisonSamplerHeap[2048]: register(s2048, space1); + +StructuredBuffer<uint> nagaGroup0SamplerIndexArray : register(t0, space0); +StructuredBuffer<uint> nagaGroup1SamplerIndexArray : register(t1, space0); + +// Indexes into group 0 index array +static const SamplerState myLinearSampler = nagaSamplerHeap[nagaGroup0SamplerIndexArray[0]]; + +// Indexes into group 1 index array +static const SamplerState myAnisoSampler = nagaSamplerHeap[nagaGroup1SamplerIndexArray[0]]; +static const SamplerComparisonState myCompSampler = nagaComparisonSamplerHeap[nagaGroup1SamplerIndexArray[1]]; +``` + +Without this transform we would need separate set of sampler descriptors for each unique combination of samplers +in a bind group. This results in a lot of duplication and makes it easy to hit the 2048 limit. With the transform +the limit is merely 2048 unique samplers in existence, which is much more reasonable. + +## Resource binding + +See [`crate::Device::create_pipeline_layout`] documentation for the structure +of the root signature corresponding to WebGPU pipeline layout. + +Binding groups is mostly straightforward, with one big caveat: +all bindings have to be reset whenever the root signature changes. +This is the rule of D3D12, and we can do nothing to help it. + +We detect this change at both [`crate::CommandEncoder::set_bind_group`] +and [`crate::CommandEncoder::set_render_pipeline`] with +[`crate::CommandEncoder::set_compute_pipeline`]. + +For this reason, in order avoid repeating the binding code, +we are binding everything in `CommandEncoder::update_root_elements`. +When the pipeline layout is changed, we reset all bindings. +Otherwise, we pass a range corresponding only to the current bind group. + +!*/ + +#![expect( + missing_debug_implementations, + reason = "TODO: someone developing on Windows add Debug impls where possible" +)] + +mod adapter; +mod command; +mod conv; +mod dcomp; +mod descriptor; +mod device; +mod device_creation; +mod instance; +mod pipeline_desc; +mod sampler; +mod shader_compilation; +mod suballocation; +mod types; +mod view; + +use alloc::{borrow::ToOwned as _, string::String, sync::Arc, vec::Vec}; +use core::{ffi, fmt, mem, ops::Deref, sync::atomic::AtomicU64}; + +use arrayvec::ArrayVec; +use hashbrown::HashMap; +use parking_lot::{Mutex, RwLock}; +use suballocation::Allocator; +use windows::{ + core::{Free as _, Interface}, + Win32::{ + Foundation, + Graphics::{ + Direct3D, + Direct3D12::{self, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT}, + DirectComposition, Dxgi, + }, + System::Threading, + }, +}; + +use self::dcomp::DCompLib; +use crate::auxil::{ + self, + dxgi::{ + factory::{DxgiAdapter, DxgiFactory}, + result::HResult, + }, +}; + +#[derive(Debug)] +struct DynLib { + inner: libloading::Library, +} + +impl DynLib { + unsafe fn new<P>(filename: P) -> Result<Self, libloading::Error> + where + P: AsRef<std::ffi::OsStr>, + { + unsafe { libloading::Library::new(filename) }.map(|inner| Self { inner }) + } + + unsafe fn get<T>( + &self, + symbol: &[u8], + ) -> Result<libloading::Symbol<'_, T>, crate::DeviceError> { + unsafe { self.inner.get(symbol) }.map_err(|e| match e { + libloading::Error::GetProcAddress { .. } | libloading::Error::GetProcAddressUnknown => { + crate::DeviceError::Unexpected + } + libloading::Error::IncompatibleSize + | libloading::Error::CreateCString { .. } + | libloading::Error::CreateCStringWithTrailing { .. } => crate::hal_internal_error(e), + _ => crate::DeviceError::Unexpected, // could be unreachable!() but we prefer to be more robust + }) + } +} + +#[derive(Debug)] +struct D3D12Lib { + lib: DynLib, +} + +#[derive(Clone, Copy, Debug)] +pub enum CreateDeviceError { + GetProcAddress, + D3D12CreateDevice(windows_core::HRESULT), + RetDeviceIsNull, +} + +impl D3D12Lib { + fn new() -> Result<Self, libloading::Error> { + unsafe { DynLib::new("d3d12.dll").map(|lib| Self { lib }) } + } + + fn create_device( + &self, + adapter: &DxgiAdapter, + feature_level: Direct3D::D3D_FEATURE_LEVEL, + ) -> Result<Direct3D12::ID3D12Device, CreateDeviceError> { + // Calls windows::Win32::Graphics::Direct3D12::D3D12CreateDevice on d3d12.dll + type Fun = extern "system" fn( + padapter: *mut ffi::c_void, + minimumfeaturelevel: Direct3D::D3D_FEATURE_LEVEL, + riid: *const windows_core::GUID, + ppdevice: *mut *mut ffi::c_void, + ) -> windows_core::HRESULT; + let func: libloading::Symbol<Fun> = + unsafe { self.lib.get(c"D3D12CreateDevice".to_bytes()) } + .map_err(|_| CreateDeviceError::GetProcAddress)?; + + let mut result__: Option<Direct3D12::ID3D12Device> = None; + + let res = (func)( + adapter.as_raw(), + feature_level, + // TODO: Generic? + &Direct3D12::ID3D12Device::IID, + <*mut _>::cast(&mut result__), + ); + + if res.is_err() { + return Err(CreateDeviceError::D3D12CreateDevice(res)); + } + + result__.ok_or(CreateDeviceError::RetDeviceIsNull) + } + + fn serialize_root_signature( + &self, + version: Direct3D12::D3D_ROOT_SIGNATURE_VERSION, + parameters: &[Direct3D12::D3D12_ROOT_PARAMETER], + static_samplers: &[Direct3D12::D3D12_STATIC_SAMPLER_DESC], + flags: Direct3D12::D3D12_ROOT_SIGNATURE_FLAGS, + ) -> Result<D3DBlob, crate::DeviceError> { + // Calls windows::Win32::Graphics::Direct3D12::D3D12SerializeRootSignature on d3d12.dll + type Fun = extern "system" fn( + prootsignature: *const Direct3D12::D3D12_ROOT_SIGNATURE_DESC, + version: Direct3D12::D3D_ROOT_SIGNATURE_VERSION, + ppblob: *mut *mut ffi::c_void, + pperrorblob: *mut *mut ffi::c_void, + ) -> windows_core::HRESULT; + let func: libloading::Symbol<Fun> = + unsafe { self.lib.get(c"D3D12SerializeRootSignature".to_bytes()) }?; + + let desc = Direct3D12::D3D12_ROOT_SIGNATURE_DESC { + NumParameters: parameters.len() as _, + pParameters: parameters.as_ptr(), + NumStaticSamplers: static_samplers.len() as _, + pStaticSamplers: static_samplers.as_ptr(), + Flags: flags, + }; + + let mut blob = None; + let mut error = None::<Direct3D::ID3DBlob>; + (func)( + &desc, + version, + <*mut _>::cast(&mut blob), + <*mut _>::cast(&mut error), + ) + .ok() + .into_device_result("Root signature serialization")?; + + if let Some(error) = error { + let error = D3DBlob(error); + log::error!( + "Root signature serialization error: {:?}", + unsafe { error.as_c_str() }.unwrap().to_str().unwrap() + ); + return Err(crate::DeviceError::Unexpected); // could be hal_usage_error or hal_internal_error + } + + blob.ok_or(crate::DeviceError::Unexpected) + } + + fn debug_interface(&self) -> Result<Option<Direct3D12::ID3D12Debug>, crate::DeviceError> { + // Calls windows::Win32::Graphics::Direct3D12::D3D12GetDebugInterface on d3d12.dll + type Fun = extern "system" fn( + riid: *const windows_core::GUID, + ppvdebug: *mut *mut ffi::c_void, + ) -> windows_core::HRESULT; + let func: libloading::Symbol<Fun> = + unsafe { self.lib.get(c"D3D12GetDebugInterface".to_bytes()) }?; + + let mut result__ = None; + + let res = (func)(&Direct3D12::ID3D12Debug::IID, <*mut _>::cast(&mut result__)).ok(); + + if let Err(ref err) = res { + match err.code() { + Dxgi::DXGI_ERROR_SDK_COMPONENT_MISSING => return Ok(None), + _ => {} + } + } + + res.into_device_result("GetDebugInterface")?; + + result__.ok_or(crate::DeviceError::Unexpected).map(Some) + } + + /// Calls D3D12GetInterface to obtain a COM interface by CLSID and IID. + /// + /// This is used by the Independent Devices API to obtain `ID3D12SDKConfiguration1`. + fn get_interface<T: Interface>( + &self, + clsid: &windows_core::GUID, + ) -> Result<T, GetInterfaceError> { + // Calls windows::Win32::Graphics::Direct3D12::D3D12GetInterface on d3d12.dll + type Fun = extern "system" fn( + rclsid: *const windows_core::GUID, + riid: *const windows_core::GUID, + ppvdebug: *mut *mut ffi::c_void, + ) -> windows_core::HRESULT; + let func: libloading::Symbol<Fun> = + unsafe { self.lib.get(c"D3D12GetInterface".to_bytes()) } + .map_err(|_| GetInterfaceError::GetProcAddress)?; + + let mut result__: Option<T> = None; + + let res = (func)(clsid, &T::IID, <*mut _>::cast(&mut result__)); + + if res.is_err() { + return Err(GetInterfaceError::D3D12GetInterface(res)); + } + + result__.ok_or(GetInterfaceError::RetIsNull) + } +} + +#[derive(Clone, Copy, Debug)] +pub(super) enum GetInterfaceError { + GetProcAddress, + D3D12GetInterface(windows_core::HRESULT), + RetIsNull, +} + +impl fmt::Display for GetInterfaceError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::GetProcAddress => write!(f, "D3D12GetInterface not found in d3d12.dll"), + Self::D3D12GetInterface(hr) => write!(f, "D3D12GetInterface failed: {hr}"), + Self::RetIsNull => write!(f, "D3D12GetInterface returned null"), + } + } +} + +impl core::error::Error for GetInterfaceError {} + +#[derive(Debug)] +pub(super) struct DxgiLib { + lib: DynLib, +} + +impl DxgiLib { + pub fn new() -> Result<Self, libloading::Error> { + unsafe { DynLib::new("dxgi.dll").map(|lib| Self { lib }) } + } + + /// Will error with crate::DeviceError::Unexpected if DXGI 1.3 is not available. + pub fn debug_interface1(&self) -> Result<Option<Dxgi::IDXGIInfoQueue>, crate::DeviceError> { + // Calls windows::Win32::Graphics::Dxgi::DXGIGetDebugInterface1 on dxgi.dll + type Fun = extern "system" fn( + flags: u32, + riid: *const windows_core::GUID, + pdebug: *mut *mut ffi::c_void, + ) -> windows_core::HRESULT; + let func: libloading::Symbol<Fun> = + unsafe { self.lib.get(c"DXGIGetDebugInterface1".to_bytes()) }?; + + let mut result__ = None; + + let res = (func)(0, &Dxgi::IDXGIInfoQueue::IID, <*mut _>::cast(&mut result__)).ok(); + + if let Err(ref err) = res { + match err.code() { + Dxgi::DXGI_ERROR_SDK_COMPONENT_MISSING => return Ok(None), + _ => {} + } + } + + res.into_device_result("debug_interface1")?; + + result__.ok_or(crate::DeviceError::Unexpected).map(Some) + } + + /// Will error with crate::DeviceError::Unexpected if DXGI 1.4 is not available. + pub fn create_factory4( + &self, + factory_flags: Dxgi::DXGI_CREATE_FACTORY_FLAGS, + ) -> Result<Dxgi::IDXGIFactory4, crate::DeviceError> { + // Calls windows::Win32::Graphics::Dxgi::CreateDXGIFactory2 on dxgi.dll + type Fun = extern "system" fn( + flags: Dxgi::DXGI_CREATE_FACTORY_FLAGS, + riid: *const windows_core::GUID, + ppfactory: *mut *mut ffi::c_void, + ) -> windows_core::HRESULT; + let func: libloading::Symbol<Fun> = + unsafe { self.lib.get(c"CreateDXGIFactory2".to_bytes()) }?; + + let mut result__ = None; + + (func)( + factory_flags, + &Dxgi::IDXGIFactory4::IID, + <*mut _>::cast(&mut result__), + ) + .ok() + .into_device_result("create_factory4")?; + + result__.ok_or(crate::DeviceError::Unexpected) + } + + /// Will error with crate::DeviceError::Unexpected if DXGI 1.3 is not available. + pub fn create_factory_media(&self) -> Result<Dxgi::IDXGIFactoryMedia, crate::DeviceError> { + // Calls windows::Win32::Graphics::Dxgi::CreateDXGIFactory1 on dxgi.dll + type Fun = extern "system" fn( + riid: *const windows_core::GUID, + ppfactory: *mut *mut ffi::c_void, + ) -> windows_core::HRESULT; + let func: libloading::Symbol<Fun> = + unsafe { self.lib.get(c"CreateDXGIFactory1".to_bytes()) }?; + + let mut result__ = None; + + // https://learn.microsoft.com/en-us/windows/win32/api/dxgi1_3/nn-dxgi1_3-idxgifactorymedia + (func)(&Dxgi::IDXGIFactoryMedia::IID, <*mut _>::cast(&mut result__)) + .ok() + .into_device_result("create_factory_media")?; + + result__.ok_or(crate::DeviceError::Unexpected) + } +} + +/// Create a temporary "owned" copy inside a [`mem::ManuallyDrop`] without increasing the refcount or +/// moving away the source variable. +/// +/// This is a common pattern when needing to pass interface pointers ("borrows") into Windows +/// structs. Moving/cloning ownership is impossible/inconvenient because: +/// +/// - The caller does _not_ assume ownership (and decrement the refcount at a later time); +/// - Unnecessarily increasing and decrementing the refcount; +/// - [`Drop`] destructors cannot run inside `union` structures (when the created structure is +/// implicitly dropped after a call). +/// +/// See also <https://github.com/microsoft/windows-rs/pull/2361#discussion_r1150799401> and +/// <https://github.com/microsoft/windows-rs/issues/2386>. +/// +/// # Safety +/// Performs a [`mem::transmute_copy()`] on a refcounted [`Interface`] type. The returned +/// [`mem::ManuallyDrop`] should _not_ be dropped. +pub unsafe fn borrow_interface_temporarily<I: Interface>(src: &I) -> mem::ManuallyDrop<Option<I>> { + unsafe { mem::transmute_copy(src) } +} + +/// See [`borrow_interface_temporarily()`] +pub unsafe fn borrow_optional_interface_temporarily<I: Interface>( + src: &Option<I>, +) -> mem::ManuallyDrop<Option<I>> { + unsafe { mem::transmute_copy(src) } +} + +struct D3DBlob(Direct3D::ID3DBlob); + +impl Deref for D3DBlob { + type Target = Direct3D::ID3DBlob; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl D3DBlob { + unsafe fn as_slice(&self) -> &[u8] { + unsafe { core::slice::from_raw_parts(self.GetBufferPointer().cast(), self.GetBufferSize()) } + } + + unsafe fn as_c_str(&self) -> Result<&ffi::CStr, ffi::FromBytesUntilNulError> { + ffi::CStr::from_bytes_until_nul(unsafe { self.as_slice() }) + } +} + +#[derive(Clone, Debug)] +pub struct Api; + +impl crate::Api for Api { + const VARIANT: wgt::Backend = wgt::Backend::Dx12; + + type Instance = Instance; + type Surface = Surface; + type Adapter = Adapter; + type Device = Device; + + type Queue = Queue; + type CommandEncoder = CommandEncoder; + type CommandBuffer = CommandBuffer; + + type Buffer = Buffer; + type Texture = Texture; + type SurfaceTexture = Texture; + type TextureView = TextureView; + type Sampler = Sampler; + type QuerySet = QuerySet; + type Fence = Fence; + + type BindGroupLayout = BindGroupLayout; + type BindGroup = BindGroup; + type PipelineLayout = PipelineLayout; + type ShaderModule = ShaderModule; + type RenderPipeline = RenderPipeline; + type ComputePipeline = ComputePipeline; + type RayTracingPipeline = RayTracingPipeline; + type PipelineCache = PipelineCache; + + type AccelerationStructure = AccelerationStructure; +} + +crate::impl_dyn_resource!( + Adapter, + AccelerationStructure, + BindGroup, + BindGroupLayout, + Buffer, + CommandBuffer, + CommandEncoder, + ComputePipeline, + Device, + Fence, + Instance, + PipelineCache, + PipelineLayout, + QuerySet, + Queue, + RayTracingPipeline, + RenderPipeline, + Sampler, + ShaderModule, + Surface, + Texture, + TextureView +); + +// Limited by D3D12's root signature size of 64. Each element takes 1 or 2 entries. +const MAX_ROOT_ELEMENTS: usize = 64; +/// See comment in [`Adapter::expose`]. +/// You must change the math in the comment before you update this value. +const MAX_IMMEDIATE_SIZE: u32 = 128; +const MAX_IMMEDIATES: usize = MAX_IMMEDIATE_SIZE as usize / 4; +const ZERO_BUFFER_SIZE: wgt::BufferAddress = 256 << 10; + +pub struct Instance { + factory: DxgiFactory, + factory_media: Option<Dxgi::IDXGIFactoryMedia>, + // `device_factory` must be dropped before `library` because the COM + // object's Release call goes through the d3d12.dll vtable. If + // `library` (which unloads d3d12.dll) is dropped first the Release + // segfaults. + device_factory: Arc<device_creation::DeviceFactory>, + library: Arc<D3D12Lib>, + dcomp_lib: Arc<DCompLib>, + supports_allow_tearing: bool, + presentation_system: wgt::Dx12SwapchainKind, + _lib_dxgi: DxgiLib, + flags: wgt::InstanceFlags, + memory_budget_thresholds: wgt::MemoryBudgetThresholds, + compiler_container: Arc<shader_compilation::CompilerContainer>, + options: wgt::Dx12BackendOptions, + telemetry: Option<crate::Telemetry>, +} + +impl Instance { + /// Get the raw DXGI factory associated with this instance. + pub unsafe fn raw_factory4(&self) -> &Dxgi::IDXGIFactory4 { + self.factory.deref() + } + + pub unsafe fn create_surface_from_visual(&self, visual: *mut ffi::c_void) -> Surface { + let visual = unsafe { DirectComposition::IDCompositionVisual::from_raw_borrowed(&visual) } + .expect("COM pointer should not be NULL"); + Surface { + factory: self.factory.clone(), + factory_media: self.factory_media.clone(), + target: SurfaceTarget::Visual(visual.to_owned()), + supports_allow_tearing: self.supports_allow_tearing, + swap_chain: RwLock::new(None), + options: self.options.clone(), + hdr_source: None, + } + } + + pub unsafe fn create_surface_from_surface_handle( + &self, + surface_handle: *mut ffi::c_void, + ) -> Surface { + // TODO: We're not given ownership, so we shouldn't call HANDLE::free(). This puts an extra burden on the caller to keep it alive. + // https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-duplicatehandle could help us, even though DirectComposition is not in the list? + // Or we make all these types owned, require an ownership transition, and replace SurfaceTargetUnsafe with SurfaceTarget. + let surface_handle = Foundation::HANDLE(surface_handle); + Surface { + factory: self.factory.clone(), + factory_media: self.factory_media.clone(), + target: SurfaceTarget::SurfaceHandle(surface_handle), + supports_allow_tearing: self.supports_allow_tearing, + swap_chain: RwLock::new(None), + options: self.options.clone(), + hdr_source: None, + } + } + + pub unsafe fn create_surface_from_swap_chain_panel( + &self, + swap_chain_panel: *mut ffi::c_void, + ) -> Surface { + let swap_chain_panel = + unsafe { types::ISwapChainPanelNative::from_raw_borrowed(&swap_chain_panel) } + .expect("COM pointer should not be NULL"); + Surface { + factory: self.factory.clone(), + factory_media: self.factory_media.clone(), + target: SurfaceTarget::SwapChainPanel(swap_chain_panel.to_owned()), + supports_allow_tearing: self.supports_allow_tearing, + swap_chain: RwLock::new(None), + options: self.options.clone(), + hdr_source: None, + } + } +} + +unsafe impl Send for Instance {} +unsafe impl Sync for Instance {} + +struct SwapChain { + // TODO: Drop order frees the SWC before the raw image pointers...? + raw: Dxgi::IDXGISwapChain3, + // need to associate raw image pointers with the swapchain so they can be properly released + // when the swapchain is destroyed + resources: Vec<Direct3D12::ID3D12Resource>, + /// Handle is freed in [`Self::release_resources()`] + waitable: Option<Foundation::HANDLE>, + acquired_count: usize, + present_mode: wgt::PresentMode, + format: wgt::TextureFormat, + size: wgt::Extent3d, +} + +enum SurfaceTarget { + /// Borrowed, lifetime externally managed + WndHandle(Foundation::HWND), + /// `handle` is borrowed, lifetime externally managed + VisualFromWndHandle { + handle: Foundation::HWND, + dcomp_state: Mutex<dcomp::DCompState>, + }, + Visual(DirectComposition::IDCompositionVisual), + /// Borrowed, lifetime externally managed + SurfaceHandle(Foundation::HANDLE), + SwapChainPanel(types::ISwapChainPanelNative), +} + +pub struct Surface { + factory: DxgiFactory, + factory_media: Option<Dxgi::IDXGIFactoryMedia>, + target: SurfaceTarget, + supports_allow_tearing: bool, + swap_chain: RwLock<Option<SwapChain>>, + options: wgt::Dx12BackendOptions, + /// HDR-info source for HWND-backed targets; `None` for composition / + /// SwapChainPanel / surface-handle targets, which have no monitor identity. + hdr_source: Option<auxil::dxgi::hdr::DxgiHdrSource>, +} + +unsafe impl Send for Surface {} +unsafe impl Sync for Surface {} + +impl Surface { + pub fn swap_chain(&self) -> Option<Dxgi::IDXGISwapChain3> { + Some(self.swap_chain.read().as_ref()?.raw.clone()) + } + + /// Returns the waitable handle associated with this swap chain, if any. + /// Handle is only valid while the swap chain is alive. + pub unsafe fn waitable_handle(&self) -> Option<Foundation::HANDLE> { + self.swap_chain.read().as_ref()?.waitable + } +} + +#[derive(Debug, Clone, Copy)] +enum MemoryArchitecture { + Unified { + #[allow(unused)] + cache_coherent: bool, + }, + NonUnified, +} + +#[derive(Debug, Clone, Copy)] +struct PrivateCapabilities { + instance_flags: wgt::InstanceFlags, + workarounds: Workarounds, + #[allow(unused)] + heterogeneous_resource_heaps: bool, + memory_architecture: MemoryArchitecture, + heap_create_not_zeroed: bool, + casting_fully_typed_format_supported: bool, + suballocation_supported: bool, + shader_model: naga::back::hlsl::ShaderModel, + max_sampler_descriptor_heap_size: u32, + unrestricted_buffer_texture_copy_pitch_supported: bool, +} + +impl PrivateCapabilities { + fn texture_data_placement_alignment(&self) -> u64 { + if self.unrestricted_buffer_texture_copy_pitch_supported { + 4 + } else { + D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT.into() + } + } +} + +#[derive(Default, Debug, Copy, Clone)] +struct Workarounds { + // On WARP 1.0.13+, debug information in shaders in certain situations causes the device + // to hang. https://github.com/gfx-rs/wgpu/issues/8368 + avoid_shader_debug_info: bool, +} + +pub struct Adapter { + raw: DxgiAdapter, + device: Direct3D12::ID3D12Device, + library: Arc<D3D12Lib>, + dcomp_lib: Arc<DCompLib>, + private_caps: PrivateCapabilities, + presentation_timer: auxil::dxgi::time::PresentationTimer, + memory_budget_thresholds: wgt::MemoryBudgetThresholds, + compiler_container: Arc<shader_compilation::CompilerContainer>, + options: wgt::Dx12BackendOptions, +} + +unsafe impl Send for Adapter {} +unsafe impl Sync for Adapter {} + +impl Adapter { + pub fn as_raw(&self) -> &Dxgi::IDXGIAdapter3 { + &self.raw + } +} + +struct Event(pub Foundation::HANDLE); +impl Event { + pub fn create(manual_reset: bool, initial_state: bool) -> Result<Self, crate::DeviceError> { + Ok(Self( + unsafe { Threading::CreateEventA(None, manual_reset, initial_state, None) } + .into_device_result("CreateEventA")?, + )) + } +} + +impl Drop for Event { + fn drop(&mut self) { + unsafe { Foundation::HANDLE::free(&mut self.0) } + } +} + +/// Helper structure for waiting for GPU. +struct Idler { + fence: Direct3D12::ID3D12Fence, +} + +#[derive(Debug, Clone)] +struct CommandSignatures { + draw: Direct3D12::ID3D12CommandSignature, + draw_indexed: Direct3D12::ID3D12CommandSignature, + draw_mesh: Option<Direct3D12::ID3D12CommandSignature>, + dispatch: Direct3D12::ID3D12CommandSignature, +} + +struct DeviceShared { + adapter: DxgiAdapter, + zero_buffer: Direct3D12::ID3D12Resource, + cmd_signatures: CommandSignatures, + heap_views: descriptor::GeneralHeap, + sampler_heap: sampler::SamplerHeap, + private_caps: PrivateCapabilities, +} + +unsafe impl Send for DeviceShared {} +unsafe impl Sync for DeviceShared {} + +pub struct Device { + raw: Direct3D12::ID3D12Device, + present_queue: Direct3D12::ID3D12CommandQueue, + idler: Idler, + features: wgt::Features, + shared: Arc<DeviceShared>, + options: wgt::Dx12BackendOptions, + // CPU only pools + rtv_pool: Arc<Mutex<descriptor::CpuPool>>, + dsv_pool: Mutex<descriptor::CpuPool>, + srv_uav_pool: Mutex<descriptor::CpuPool>, + // library + library: Arc<D3D12Lib>, + dcomp_lib: Arc<DCompLib>, + #[cfg(feature = "renderdoc")] + render_doc: auxil::renderdoc::RenderDoc, + null_rtv_handle: descriptor::Handle, + mem_allocator: Allocator, + compiler_container: Arc<shader_compilation::CompilerContainer>, + shader_cache: Mutex<ShaderCache>, + counters: Arc<wgt::HalCounters>, + limits: wgt::Limits, +} + +impl Drop for Device { + fn drop(&mut self) { + self.rtv_pool.lock().free_handle(self.null_rtv_handle); + if self + .shared + .private_caps + .instance_flags + .contains(wgt::InstanceFlags::VALIDATION) + { + auxil::dxgi::exception::unregister_exception_handler(); + } + } +} + +unsafe impl Send for Device {} +unsafe impl Sync for Device {} + +pub struct Queue { + raw: Direct3D12::ID3D12CommandQueue, + temp_lists: Mutex<Vec<Option<Direct3D12::ID3D12CommandList>>>, + idle_fence: Direct3D12::ID3D12Fence, + idle_event: Event, + idle_fence_value: AtomicU64, + pending_waits: Mutex<Vec<(Direct3D12::ID3D12Fence, u64)>>, + pending_signals: Mutex<Vec<(Direct3D12::ID3D12Fence, u64)>>, +} + +impl Queue { + pub fn as_raw(&self) -> &Direct3D12::ID3D12CommandQueue { + &self.raw + } + + /// Stage a `ID3D12CommandQueue::Wait(fence, value)` for the next + /// [`crate::Queue::submit`]. The wait is enqueued before the + /// submit's command lists, so subsequent GPU work observes the + /// foreign signal at `value`. + pub fn add_wait_fence(&self, fence: Direct3D12::ID3D12Fence, value: u64) { + self.pending_waits.lock().push((fence, value)); + } + + /// Remove `fence` from the pending wait list if it is still present. + /// Returns `true` if it was found and removed. + pub fn remove_wait_fence(&self, fence: &Direct3D12::ID3D12Fence) -> bool { + let target = fence.as_raw(); + let mut waits = self.pending_waits.lock(); + let before = waits.len(); + waits.retain(|(f, _)| f.as_raw() != target); + waits.len() != before + } + + /// Stage a `ID3D12CommandQueue::Signal(fence, value)` for the next + /// [`crate::Queue::submit`]. The signal is enqueued after the + /// submit's command lists complete, so a foreign API waiting on + /// `(fence, value)` observes the wgpu work as done. + pub fn add_signal_fence(&self, fence: Direct3D12::ID3D12Fence, value: u64) { + self.pending_signals.lock().push((fence, value)); + } + + /// Remove `fence` from the pending signal list if it is still present. + /// Returns `true` if it was found and removed. + pub fn remove_signal_fence(&self, fence: &Direct3D12::ID3D12Fence) -> bool { + let target = fence.as_raw(); + let mut signals = self.pending_signals.lock(); + let before = signals.len(); + signals.retain(|(f, _)| f.as_raw() != target); + signals.len() != before + } +} + +unsafe impl Send for Queue {} +unsafe impl Sync for Queue {} + +#[derive(Default)] +struct Temp { + marker: Vec<u16>, + barriers: Vec<Direct3D12::D3D12_RESOURCE_BARRIER>, +} + +impl Temp { + fn clear(&mut self) { + self.marker.clear(); + self.barriers.clear(); + } +} + +struct PassResolve { + src: (Direct3D12::ID3D12Resource, u32), + dst: (Direct3D12::ID3D12Resource, u32), + format: Dxgi::Common::DXGI_FORMAT, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq)] +struct SpecialConstants { + /// The first vertex in an indirect draw call, _or_ the `x` of a compute dispatch. + first_vertex_or_x: i32, + /// The first instance in an indirect draw call, _or_ the `y` of a compute dispatch. + first_instance_or_y: u32, + /// Unused in an indirect draw call, _or_ the `z` of a compute dispatch. + unused_or_z: u32, +} + +impl SpecialConstants { + fn from_indirect_draw_call_params(first_vertex: i32, first_instance: u32) -> Self { + Self { + first_vertex_or_x: first_vertex, + first_instance_or_y: first_instance, + unused_or_z: 0, + } + } + + fn from_compute_dispatch_params(workgroup_count: [u32; 3]) -> Self { + Self { + first_vertex_or_x: workgroup_count[0] as i32, + first_instance_or_y: workgroup_count[1], + unused_or_z: workgroup_count[2], + } + } +} + +#[derive(Clone, Copy, Debug)] +enum RootElement { + Empty, + Immediates, + SpecialConstants(SpecialConstants), + DescriptorTable(Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE), + /// Descriptor table referring to the entire sampler heap. + SamplerHeapDescriptorTable, + /// Root descriptor for a uniform buffer binding that has a dynamic offset. + DynamicUniformBuffer { + address: Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE, + }, + /// Root constants for storage buffer bindings with dynamic offsets. + /// + /// start..end is the range of values in [`PassState::dynamic_storage_buffer_offsets`] + /// that will be used to update the root constants. + DynamicStorageBufferOffsets { + start: usize, + end: usize, + }, +} + +#[derive(Clone, Copy)] +enum PassKind { + Render, + Compute, + Transfer, +} + +struct PassState { + has_label: bool, + resolves: ArrayVec<PassResolve, { crate::MAX_COLOR_ATTACHMENTS }>, + layout: PipelineLayoutShared, + root_elements: [RootElement; MAX_ROOT_ELEMENTS], + immediates: [u32; MAX_IMMEDIATES], + dynamic_storage_buffer_offsets: Vec<u32>, + dirty_root_elements: u64, + vertex_buffers: [Direct3D12::D3D12_VERTEX_BUFFER_VIEW; crate::MAX_VERTEX_BUFFERS], + dirty_vertex_buffers: usize, + kind: PassKind, +} + +// `root_elements` size must match `dirty_root_elements` bit size +const _: () = assert!(MAX_ROOT_ELEMENTS == u64::BITS as usize); + +impl PassState { + fn new() -> Self { + PassState { + has_label: false, + resolves: ArrayVec::new(), + layout: PipelineLayoutShared { + signature: None, + total_root_elements: 0, + special_constants: None, + immediates_info: None, + sampler_heap_root_index: None, + }, + root_elements: [RootElement::Empty; MAX_ROOT_ELEMENTS], + immediates: [0; MAX_IMMEDIATES], + dynamic_storage_buffer_offsets: Vec::new(), + dirty_root_elements: 0, + vertex_buffers: [Default::default(); crate::MAX_VERTEX_BUFFERS], + dirty_vertex_buffers: 0, + kind: PassKind::Transfer, + } + } + + fn clear(&mut self) { + // careful about heap allocations! + *self = Self::new(); + } +} + +// Any state in this struct that may be dirty after an abandoned encoding must +// be reset for reused encoders in `begin_encoding`. +pub struct CommandEncoder { + allocator: Direct3D12::ID3D12CommandAllocator, + device: Direct3D12::ID3D12Device, + shared: Arc<DeviceShared>, + mem_allocator: Allocator, + + rtv_pool: Arc<Mutex<descriptor::CpuPool>>, + temp_rtv_handles: Vec<descriptor::Handle>, + + intermediate_copy_bufs: Vec<Buffer>, + + null_rtv_handle: descriptor::Handle, + list: Option<Direct3D12::ID3D12GraphicsCommandList>, + free_lists: Vec<Direct3D12::ID3D12GraphicsCommandList>, + pass: PassState, + temp: Temp, + + /// If set, the end of the next render/compute pass will write a timestamp at + /// the given pool & location. + end_of_pass_timer_query: Option<(Direct3D12::ID3D12QueryHeap, u32)>, + + counters: Arc<wgt::HalCounters>, +} + +unsafe impl Send for CommandEncoder {} +unsafe impl Sync for CommandEncoder {} + +impl fmt::Debug for CommandEncoder { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("CommandEncoder") + .field("allocator", &self.allocator) + .field("device", &self.allocator) + .finish() + } +} + +#[derive(Debug)] +pub struct CommandBuffer { + raw: Direct3D12::ID3D12GraphicsCommandList, +} + +impl crate::DynCommandBuffer for CommandBuffer {} + +unsafe impl Send for CommandBuffer {} +unsafe impl Sync for CommandBuffer {} + +#[derive(Debug)] +pub struct Buffer { + resource: Direct3D12::ID3D12Resource, + // While the allocation also has _a_ size, it may not + // be the same as the original size of the buffer, + // as the allocation size varies for assorted reasons. + size: wgt::BufferAddress, + allocation: suballocation::Allocation, +} + +impl Buffer { + pub unsafe fn raw_resource(&self) -> &Direct3D12::ID3D12Resource { + &self.resource + } +} + +unsafe impl Send for Buffer {} +unsafe impl Sync for Buffer {} + +impl crate::DynBuffer for Buffer {} + +impl crate::BufferBinding<'_, Buffer> { + fn resolve_size(&self) -> wgt::BufferAddress { + match self.size { + Some(size) => size.get(), + None => self.buffer.size - self.offset, + } + } + + // TODO: Return GPU handle directly? + fn resolve_address(&self) -> wgt::BufferAddress { + (unsafe { self.buffer.resource.GetGPUVirtualAddress() }) + self.offset + } +} + +#[derive(Debug)] +pub struct Texture { + resource: Direct3D12::ID3D12Resource, + format: wgt::TextureFormat, + dimension: wgt::TextureDimension, + size: wgt::Extent3d, + mip_level_count: u32, + sample_count: u32, + allocation: suballocation::Allocation, + /// Pins `PlaneSlice` for every view and copy derived from this texture, + /// overriding the default aspect-based derivation. Used by cross-API + /// importers wrapping one plane of a multi-plane DXGI resource as a + /// single-plane wgpu texture. + plane_slice_override: Option<u32>, +} + +impl Texture { + pub unsafe fn raw_resource(&self) -> &Direct3D12::ID3D12Resource { + &self.resource + } + + /// Pin the D3D12 plane slice for every view and copy derived from + /// this texture. Pass `0` for the luma plane, `1` for chroma. The + /// declared `TextureFormat` must be single-plane (e.g. `R8Unorm`, + /// `Rg8Unorm`) and match the plane's texel layout. + /// + /// # Safety / aliasing + /// + /// The intended use is to wrap one plane of a multi-plane DXGI + /// resource (e.g. NV12) as a single-plane wgpu texture, typically + /// by calling [`Device::texture_from_raw`](Device::texture_from_raw) + /// twice with two clones of the same `ID3D12Resource` and a + /// different `plane_slice` on each. wgpu's state tracker treats + /// these wrappers as independent textures: per-subresource state, + /// hazard tracking, and resource-state barriers all assume + /// non-aliased ownership and have no way to know the two wrappers + /// alias the same underlying resource. + /// + /// The caller is responsible for ensuring that the underlying + /// resource is not concurrently used by another wgpu texture + /// wrapping a different plane in a way that would race or + /// invalidate state tracking — in particular, do not submit work + /// touching both wrappers in the same submission unless every + /// access goes through `COPY_SRC` / `COPY_DST` on the wrapper that + /// owns that plane's subresource, and do not destroy the wrappers + /// concurrently with in-flight work that uses either of them. + pub fn with_plane_slice(mut self, plane_slice: u32) -> Self { + debug_assert!( + !self.format.is_multi_planar_format(), + "`with_plane_slice` expects a single-plane format wrapping a \ + multi-plane DXGI resource; got planar format `{:?}`", + self.format, + ); + self.plane_slice_override = Some(plane_slice); + self + } + + pub(super) fn plane_slice_override(&self) -> Option<u32> { + self.plane_slice_override + } +} + +impl crate::DynTexture for Texture {} +impl crate::DynSurfaceTexture for Texture {} + +impl core::borrow::Borrow<dyn crate::DynTexture> for Texture { + fn borrow(&self) -> &dyn crate::DynTexture { + self + } +} + +unsafe impl Send for Texture {} +unsafe impl Sync for Texture {} + +impl Texture { + fn array_layer_count(&self) -> u32 { + match self.dimension { + wgt::TextureDimension::D1 | wgt::TextureDimension::D3 => 1, + wgt::TextureDimension::D2 => self.size.depth_or_array_layers, + } + } + + /// see <https://learn.microsoft.com/en-us/windows/win32/direct3d12/subresources#plane-slice> + fn calc_subresource(&self, mip_level: u32, array_layer: u32, plane: u32) -> u32 { + mip_level + (array_layer + plane * self.array_layer_count()) * self.mip_level_count + } + + fn calc_subresource_for_copy(&self, base: &crate::TextureCopyBase) -> u32 { + // Single-plane wrappers around a multi-plane resource report + // `COLOR` aspect, which would otherwise resolve to plane 0. + let plane = self + .plane_slice_override + .unwrap_or_else(|| match base.aspect { + crate::FormatAspects::COLOR + | crate::FormatAspects::DEPTH + | crate::FormatAspects::PLANE_0 => 0, + crate::FormatAspects::STENCIL | crate::FormatAspects::PLANE_1 => 1, + crate::FormatAspects::PLANE_2 => 2, + _ => unreachable!(), + }); + self.calc_subresource(base.mip_level, base.array_layer, plane) + } +} + +#[derive(Debug)] +pub struct TextureView { + raw_format: Dxgi::Common::DXGI_FORMAT, + aspects: crate::FormatAspects, + dimension: wgt::TextureViewDimension, + texture: Direct3D12::ID3D12Resource, + subresource_index: u32, + mip_slice: u32, + handle_srv: Option<descriptor::Handle>, + handle_uav: Option<descriptor::Handle>, + handle_rtv: Option<descriptor::Handle>, + handle_dsv_ro: Option<descriptor::Handle>, + handle_dsv_rw: Option<descriptor::Handle>, +} + +impl crate::DynTextureView for TextureView {} + +unsafe impl Send for TextureView {} +unsafe impl Sync for TextureView {} + +#[derive(Debug)] +pub struct Sampler { + index: sampler::SamplerIndex, + desc: Direct3D12::D3D12_SAMPLER_DESC, +} + +impl crate::DynSampler for Sampler {} + +#[cfg(send_sync)] +static_assertions::assert_impl_all!(Sampler: Send, Sync); + +#[derive(Debug)] +pub struct QuerySet { + raw: Direct3D12::ID3D12QueryHeap, + raw_ty: Direct3D12::D3D12_QUERY_TYPE, +} + +impl crate::DynQuerySet for QuerySet {} + +unsafe impl Send for QuerySet {} +unsafe impl Sync for QuerySet {} + +#[derive(Debug)] +pub struct Fence { + raw: Direct3D12::ID3D12Fence, +} + +impl crate::DynFence for Fence {} + +unsafe impl Send for Fence {} +unsafe impl Sync for Fence {} + +impl Fence { + pub fn raw_fence(&self) -> &Direct3D12::ID3D12Fence { + &self.raw + } +} + +#[derive(Debug)] +pub struct BindGroupLayout { + /// Sorted list of entries. + entries: Vec<wgt::BindGroupLayoutEntry>, + cpu_heap_views: Option<descriptor::CpuHeap>, + copy_counts: Vec<u32>, // all 1's +} + +impl crate::DynBindGroupLayout for BindGroupLayout {} + +#[derive(Debug, Clone, Copy)] +enum DynamicBuffer { + Uniform(Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE), + Storage, +} + +#[derive(Debug)] +struct SamplerIndexBuffer { + buffer: Direct3D12::ID3D12Resource, + allocation: suballocation::Allocation, +} + +#[derive(Debug)] +pub struct BindGroup { + handle_views: Option<descriptor::DualHandle>, + sampler_index_buffer: Option<SamplerIndexBuffer>, + dynamic_buffers: Vec<DynamicBuffer>, +} + +impl crate::DynBindGroup for BindGroup {} + +bitflags::bitflags! { + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] + struct TableTypes: u8 { + const SRV_CBV_UAV = 1 << 0; + const SAMPLERS = 1 << 1; + } +} + +// Element (also known as parameter) index into the root signature. +type RootIndex = u32; + +#[derive(Debug)] +struct BindGroupInfo { + base_root_index: RootIndex, + tables: TableTypes, + dynamic_storage_buffer_offsets: Option<DynamicStorageBufferOffsets>, +} + +#[derive(Debug, Clone)] +struct ImmediatesInfo { + root_index: RootIndex, + size: u32, +} + +#[derive(Debug, Clone)] +struct DynamicStorageBufferOffsets { + root_index: RootIndex, + range: core::ops::Range<usize>, +} + +#[derive(Debug, Clone)] +struct PipelineLayoutShared { + signature: Option<Direct3D12::ID3D12RootSignature>, + total_root_elements: RootIndex, + special_constants: Option<PipelineLayoutSpecialConstants>, + immediates_info: Option<ImmediatesInfo>, + sampler_heap_root_index: Option<RootIndex>, +} + +unsafe impl Send for PipelineLayoutShared {} +unsafe impl Sync for PipelineLayoutShared {} + +#[derive(Debug, Clone)] +struct PipelineLayoutSpecialConstants { + root_index: RootIndex, + indirect_cmd_signatures: Option<CommandSignatures>, +} + +unsafe impl Send for PipelineLayoutSpecialConstants {} +unsafe impl Sync for PipelineLayoutSpecialConstants {} + +#[derive(Debug)] +pub struct PipelineLayout { + shared: PipelineLayoutShared, + // Storing for each associated bind group, which tables we created + // in the root signature. This is required for binding descriptor sets. + bind_group_infos: [Option<BindGroupInfo>; crate::MAX_BIND_GROUPS], + naga_options: naga::back::hlsl::Options, +} + +impl crate::DynPipelineLayout for PipelineLayout {} + +#[derive(Debug)] +pub struct ShaderModule { + source: ShaderModuleSource, + raw_name: Option<alloc::ffi::CString>, + runtime_checks: wgt::ShaderRuntimeChecks, +} + +impl crate::DynShaderModule for ShaderModule {} + +#[derive(Default)] +pub struct ShaderCache { + nr_of_shaders_compiled: u32, + entries: HashMap<ShaderCacheKey, ShaderCacheValue>, +} + +#[derive(PartialEq, Eq, Hash)] +pub(super) struct ShaderCacheKey { + source: String, + entry_point: String, + stage: naga::ShaderStage, + shader_model: naga::back::hlsl::ShaderModel, +} + +pub(super) struct ShaderCacheValue { + /// This is the value of [`ShaderCache::nr_of_shaders_compiled`] + /// at the time the cache entry was last used. + last_used: u32, + shader: CompiledShader, +} + +#[derive(Clone)] +pub(super) enum CompiledShader { + Dxc(Direct3D::Dxc::IDxcBlob), + Fxc(Direct3D::ID3DBlob), + Precompiled(Vec<u8>), +} + +impl CompiledShader { + fn create_native_shader(&self) -> Direct3D12::D3D12_SHADER_BYTECODE { + match self { + CompiledShader::Dxc(shader) => Direct3D12::D3D12_SHADER_BYTECODE { + pShaderBytecode: unsafe { shader.GetBufferPointer() }, + BytecodeLength: unsafe { shader.GetBufferSize() }, + }, + CompiledShader::Fxc(shader) => Direct3D12::D3D12_SHADER_BYTECODE { + pShaderBytecode: unsafe { shader.GetBufferPointer() }, + BytecodeLength: unsafe { shader.GetBufferSize() }, + }, + CompiledShader::Precompiled(shader) => Direct3D12::D3D12_SHADER_BYTECODE { + pShaderBytecode: shader.as_ptr().cast(), + BytecodeLength: shader.len(), + }, + } + } +} + +#[derive(Debug)] +pub struct RenderPipeline { + raw: Direct3D12::ID3D12PipelineState, + layout: PipelineLayoutShared, + topology: Direct3D::D3D_PRIMITIVE_TOPOLOGY, + vertex_strides: [Option<u32>; crate::MAX_VERTEX_BUFFERS], +} + +impl crate::DynRenderPipeline for RenderPipeline {} + +unsafe impl Send for RenderPipeline {} +unsafe impl Sync for RenderPipeline {} + +#[derive(Debug)] +pub struct ComputePipeline { + raw: Direct3D12::ID3D12PipelineState, + layout: PipelineLayoutShared, +} + +impl crate::DynComputePipeline for ComputePipeline {} + +unsafe impl Send for ComputePipeline {} +unsafe impl Sync for ComputePipeline {} + +#[derive(Debug)] +pub struct RayTracingPipeline {} + +impl crate::DynRayTracingPipeline for RayTracingPipeline {} + +#[derive(Debug)] +pub struct PipelineCache; + +impl crate::DynPipelineCache for PipelineCache {} + +#[derive(Debug)] +pub struct AccelerationStructure { + resource: Direct3D12::ID3D12Resource, + allocation: suballocation::Allocation, +} + +impl crate::DynAccelerationStructure for AccelerationStructure {} + +impl SwapChain { + unsafe fn release_resources(mut self) -> Dxgi::IDXGISwapChain3 { + if let Some(mut waitable) = self.waitable.take() { + unsafe { Foundation::HANDLE::free(&mut waitable) }; + } + self.raw + } + + unsafe fn wait( + &mut self, + timeout: Option<core::time::Duration>, + ) -> Result<bool, crate::SurfaceError> { + let timeout_ms = match timeout { + Some(duration) => duration.as_millis() as u32, + None => Threading::INFINITE, + }; + + if let Some(waitable) = self.waitable { + match unsafe { Threading::WaitForSingleObject(waitable, timeout_ms) } { + Foundation::WAIT_ABANDONED | Foundation::WAIT_FAILED => { + Err(crate::SurfaceError::Lost) + } + Foundation::WAIT_OBJECT_0 => Ok(true), + Foundation::WAIT_TIMEOUT => Ok(false), + other => { + log::error!("Unexpected wait status: 0x{other:x?}"); + Err(crate::SurfaceError::Lost) + } + } + } else { + Ok(true) + } + } +} + +fn map_surface_color_space( + color_space: wgt::SurfaceColorSpace, +) -> Dxgi::Common::DXGI_COLOR_SPACE_TYPE { + use wgt::SurfaceColorSpace as Scs; + match color_space { + Scs::Srgb => Dxgi::Common::DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709, + Scs::ExtendedSrgbLinear => Dxgi::Common::DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709, + Scs::Bt2100Pq => Dxgi::Common::DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020, + Scs::Auto + | Scs::DisplayP3 + | Scs::Bt2100Hlg + | Scs::ExtendedSrgb + | Scs::ExtendedDisplayP3 => { + unreachable!("`{color_space:?}` is never reported in the DX12 surface capabilities") + } + } +} + +impl crate::Surface for Surface { + type A = Api; + + unsafe fn configure( + &self, + device: &Device, + config: &crate::SurfaceConfiguration, + ) -> Result<(), crate::SurfaceError> { + let mut flags = Dxgi::DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT; + // We always set ALLOW_TEARING on the swapchain no matter + // what kind of swapchain we want because ResizeBuffers + // cannot change the swapchain's ALLOW_TEARING flag. + // + // This does not change the behavior of the swapchain, just + // allow present calls to use tearing. + if self.supports_allow_tearing { + flags |= Dxgi::DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING; + } + + // While `configure`s contract ensures that no work on the GPU's main queues + // are in flight, we still need to wait for the present queue to be idle. + unsafe { device.wait_for_present_queue_idle() }?; + + let non_srgb_format = auxil::dxgi::conv::map_texture_format_nosrgb(config.format); + + // The range for `SetMaximumFrameLatency` is 1-16 so the maximum latency requested should be 15 because we add 1. + // https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgidevice1-setmaximumframelatency + debug_assert!(config.maximum_frame_latency <= 15); + + // Nvidia recommends to use 1-2 more buffers than the maximum latency + // https://developer.nvidia.com/blog/advanced-api-performance-swap-chains/ + // For high latency extra buffers seems excessive, so go with a minimum of 3 and beyond that add 1. + let swap_chain_buffer = (config.maximum_frame_latency + 1).min(16); + + let swap_chain = match self.swap_chain.write().take() { + //Note: this path doesn't properly re-initialize all of the things + Some(sc) => { + let raw = unsafe { sc.release_resources() }; + let result = unsafe { + raw.ResizeBuffers( + swap_chain_buffer, + config.extent.width, + config.extent.height, + non_srgb_format, + flags, + ) + }; + if let Err(err) = result { + log::error!("ResizeBuffers failed: {err}"); + return Err(crate::SurfaceError::Other("window is in use")); + } + raw + } + None => { + let desc = Dxgi::DXGI_SWAP_CHAIN_DESC1 { + AlphaMode: auxil::dxgi::conv::map_acomposite_alpha_mode( + config.composite_alpha_mode, + ), + Width: config.extent.width, + Height: config.extent.height, + Format: non_srgb_format, + Stereo: false.into(), + SampleDesc: Dxgi::Common::DXGI_SAMPLE_DESC { + Count: 1, + Quality: 0, + }, + BufferUsage: Dxgi::DXGI_USAGE_RENDER_TARGET_OUTPUT, + BufferCount: swap_chain_buffer, + Scaling: Dxgi::DXGI_SCALING_STRETCH, + SwapEffect: Dxgi::DXGI_SWAP_EFFECT_FLIP_DISCARD, + Flags: flags.0 as u32, + }; + let swap_chain1 = match self.target { + SurfaceTarget::Visual(_) + | SurfaceTarget::VisualFromWndHandle { .. } + | SurfaceTarget::SwapChainPanel(_) => { + profiling::scope!("IDXGIFactory2::CreateSwapChainForComposition"); + unsafe { + self.factory.CreateSwapChainForComposition( + &device.present_queue, + &desc, + None, + ) + } + } + SurfaceTarget::SurfaceHandle(handle) => { + profiling::scope!( + "IDXGIFactoryMedia::CreateSwapChainForCompositionSurfaceHandle" + ); + unsafe { + self.factory_media + .as_ref() + .ok_or(crate::SurfaceError::Other("IDXGIFactoryMedia not found"))? + .CreateSwapChainForCompositionSurfaceHandle( + &device.present_queue, + Some(handle), + &desc, + None, + ) + } + } + SurfaceTarget::WndHandle(hwnd) => { + profiling::scope!("IDXGIFactory2::CreateSwapChainForHwnd"); + unsafe { + self.factory.CreateSwapChainForHwnd( + &device.present_queue, + hwnd, + &desc, + None, + None, + ) + } + } + }; + + let swap_chain1 = swap_chain1.map_err(|err| { + log::error!("SwapChain creation error: {err}"); + crate::SurfaceError::Other("swapchain creation") + })?; + + match &self.target { + SurfaceTarget::WndHandle(_) | SurfaceTarget::SurfaceHandle(_) => {} + SurfaceTarget::VisualFromWndHandle { + handle, + dcomp_state, + } => { + let mut dcomp_state = dcomp_state.lock(); + let dcomp_state = + unsafe { dcomp_state.get_or_init(&device.dcomp_lib, handle) }?; + // Set the new swap chain as the content for the backing visual + // and commit the changes to the composition visual tree. + { + profiling::scope!("IDCompositionVisual::SetContent"); + unsafe { dcomp_state.visual.SetContent(&swap_chain1) }.map_err( + |err| { + log::error!("IDCompositionVisual::SetContent failed: {err}"); + crate::SurfaceError::Other("IDCompositionVisual::SetContent") + }, + )?; + } + + // Commit the changes to the composition device. + { + profiling::scope!("IDCompositionDevice::Commit"); + unsafe { dcomp_state.device.Commit() }.map_err(|err| { + log::error!("IDCompositionDevice::Commit failed: {err}"); + crate::SurfaceError::Other("IDCompositionDevice::Commit") + })?; + } + } + SurfaceTarget::Visual(visual) => { + if let Err(err) = unsafe { visual.SetContent(&swap_chain1) } { + log::error!("Unable to SetContent: {err}"); + return Err(crate::SurfaceError::Other( + "IDCompositionVisual::SetContent", + )); + } + } + SurfaceTarget::SwapChainPanel(swap_chain_panel) => { + if let Err(err) = unsafe { swap_chain_panel.SetSwapChain(&swap_chain1) } { + log::error!("Unable to SetSwapChain: {err}"); + return Err(crate::SurfaceError::Other( + "ISwapChainPanelNative::SetSwapChain", + )); + } + } + } + + swap_chain1.cast::<Dxgi::IDXGISwapChain3>().map_err(|err| { + log::error!("Unable to cast swapchain: {err}"); + crate::SurfaceError::Other("swapchain cast to version 3") + })? + } + }; + + // Apply the color space unconditionally (even for sRGB) so that + // reconfiguring away from HDR10 resets the swapchain's state. + // + // We deliberately skip `CheckColorSpaceSupport`: a color space reporting as + // unsupported is not a failure. Windows composites in scRGB and tone-maps + // the color space down to the output, so `SetColorSpace1` still presents + // correctly even when the check returns false (as the MS docs note). This + // lets an app configure e.g. BT.2100 PQ on an SDR output and let the + // compositor map it. + let color_space = map_surface_color_space(config.color_space); + // SAFETY: `swap_chain` is a live `IDXGISwapChain3`; `color_space` is a + // valid `DXGI_COLOR_SPACE_TYPE`. + unsafe { swap_chain.SetColorSpace1(color_space) }.map_err(|err| { + log::error!("SetColorSpace1 failed: {err}"); + crate::SurfaceError::Other("IDXGISwapChain3::SetColorSpace1") + })?; + + match self.target { + SurfaceTarget::WndHandle(wnd_handle) => { + // Disable automatic Alt+Enter handling by DXGI. + unsafe { + self.factory.MakeWindowAssociation( + wnd_handle, + Dxgi::DXGI_MWA_NO_WINDOW_CHANGES | Dxgi::DXGI_MWA_NO_ALT_ENTER, + ) + } + .into_device_result("MakeWindowAssociation")?; + } + SurfaceTarget::Visual(_) + | SurfaceTarget::VisualFromWndHandle { .. } + | SurfaceTarget::SurfaceHandle(_) + | SurfaceTarget::SwapChainPanel(_) => {} + } + + unsafe { swap_chain.SetMaximumFrameLatency(config.maximum_frame_latency) } + .into_device_result("SetMaximumFrameLatency")?; + + let waitable = match device.options.latency_waitable_object { + wgt::Dx12UseFrameLatencyWaitableObject::None => None, + wgt::Dx12UseFrameLatencyWaitableObject::Wait + | wgt::Dx12UseFrameLatencyWaitableObject::DontWait => { + Some(unsafe { swap_chain.GetFrameLatencyWaitableObject() }) + } + }; + + let mut resources = Vec::with_capacity(swap_chain_buffer as usize); + for i in 0..swap_chain_buffer { + let resource = unsafe { swap_chain.GetBuffer(i) } + .into_device_result("Failed to get swapchain buffer")?; + resources.push(resource); + } + + let mut swapchain = self.swap_chain.write(); + *swapchain = Some(SwapChain { + raw: swap_chain, + resources, + waitable, + acquired_count: 0, + present_mode: config.present_mode, + format: config.format, + size: config.extent, + }); + + Ok(()) + } + + unsafe fn unconfigure(&self, device: &Device) { + if let Some(sc) = self.swap_chain.write().take() { + unsafe { + // While `unconfigure`s contract ensures that no work on the GPU's main queues + // are in flight, we still need to wait for the present queue to be idle. + + // The major failure mode of this function is device loss, + // which if we have lost the device, we should just continue + // cleaning up, without error. + let _ = device.wait_for_present_queue_idle(); + + let _raw = sc.release_resources(); + } + } + } + + unsafe fn acquire_texture( + &self, + timeout: Option<core::time::Duration>, + _fence: &Fence, + ) -> Result<crate::AcquiredSurfaceTexture<Api>, crate::SurfaceError> { + let mut swapchain = self.swap_chain.write(); + let sc = swapchain.as_mut().unwrap(); + + match self.options.latency_waitable_object { + wgt::Dx12UseFrameLatencyWaitableObject::None + | wgt::Dx12UseFrameLatencyWaitableObject::DontWait => {} + wgt::Dx12UseFrameLatencyWaitableObject::Wait => { + unsafe { sc.wait(timeout) }?; + } + } + + let base_index = unsafe { sc.raw.GetCurrentBackBufferIndex() } as usize; + let index = (base_index + sc.acquired_count) % sc.resources.len(); + sc.acquired_count += 1; + + let texture = Texture { + resource: sc.resources[index].clone(), + format: sc.format, + dimension: wgt::TextureDimension::D2, + size: sc.size, + mip_level_count: 1, + sample_count: 1, + allocation: suballocation::Allocation::none( + suballocation::AllocationType::Texture, + sc.format.theoretical_memory_footprint(sc.size), + ), + plane_slice_override: None, + }; + Ok(crate::AcquiredSurfaceTexture { + texture, + suboptimal: false, + }) + } + unsafe fn discard_texture(&self, _texture: Texture) { + let mut swapchain = self.swap_chain.write(); + let sc = swapchain.as_mut().unwrap(); + sc.acquired_count -= 1; + } +} + +impl crate::Queue for Queue { + type A = Api; + + unsafe fn submit( + &self, + command_buffers: &[&CommandBuffer], + _surface_textures: &[&Texture], + (signal_fence, signal_value): (&Fence, crate::FenceValue), + ) -> Result<(), crate::DeviceError> { + let mut temp_lists = self.temp_lists.lock(); + temp_lists.clear(); + for cmd_buf in command_buffers { + temp_lists.push(Some(cmd_buf.raw.clone().into())); + } + + // Drain caller-staged waits before ExecuteCommandLists so the + // GPU queue blocks on each foreign signal before running our + // command lists. D3D12 queue commands are FIFO - Wait calls + // here gate everything submitted after them. + { + let mut waits = self.pending_waits.lock(); + for (fence, value) in waits.drain(..) { + unsafe { self.raw.Wait(&fence, value) }.into_device_result("Wait pending fence")?; + } + } + + { + profiling::scope!("ID3D12CommandQueue::ExecuteCommandLists"); + unsafe { self.raw.ExecuteCommandLists(&temp_lists) } + } + + unsafe { self.raw.Signal(&signal_fence.raw, signal_value) } + .into_device_result("Signal fence")?; + + // Drain caller-staged signals after our own Signal so each + // additional fence value publishes once the submit completes. + { + let mut signals = self.pending_signals.lock(); + for (fence, value) in signals.drain(..) { + unsafe { self.raw.Signal(&fence, value) } + .into_device_result("Signal pending fence")?; + } + } + + // Note the lack of synchronization here between the main Direct queue + // and the dedicated presentation queue. This is automatically handled + // by the D3D runtime by detecting uses of resources derived from the + // swapchain. This automatic detection is why you cannot use a swapchain + // as an UAV in D3D12. + + Ok(()) + } + unsafe fn present( + &self, + surface: &Surface, + _texture: Texture, + ) -> Result<(), crate::SurfaceError> { + let mut swapchain = surface.swap_chain.write(); + let sc = swapchain.as_mut().unwrap(); + sc.acquired_count -= 1; + + let (interval, flags) = match sc.present_mode { + // We only allow immediate if ALLOW_TEARING is valid. + wgt::PresentMode::Immediate => (0, Dxgi::DXGI_PRESENT_ALLOW_TEARING), + wgt::PresentMode::Mailbox => (0, Dxgi::DXGI_PRESENT::default()), + wgt::PresentMode::Fifo => (1, Dxgi::DXGI_PRESENT::default()), + m => unreachable!("Cannot make surface with present mode {m:?}"), + }; + + profiling::scope!("IDXGISwapchain3::Present"); + unsafe { sc.raw.Present(interval, flags) } + .ok() + .into_device_result("Present")?; + + Ok(()) + } + + unsafe fn get_timestamp_period(&self) -> f32 { + let frequency = unsafe { self.raw.GetTimestampFrequency() }.expect("GetTimestampFrequency"); + (1_000_000_000.0 / frequency as f64) as f32 + } + + unsafe fn wait_for_idle(&self) -> Result<(), crate::DeviceError> { + let value = self + .idle_fence_value + .fetch_add(1, core::sync::atomic::Ordering::Relaxed) + + 1; + unsafe { self.raw.Signal(&self.idle_fence, value) } + .into_device_result("Signal idle fence")?; + unsafe { + self.idle_fence + .SetEventOnCompletion(value, self.idle_event.0) + } + .into_device_result("SetEventOnCompletion")?; + unsafe { Threading::WaitForSingleObject(self.idle_event.0, Threading::INFINITE) }; + Ok(()) + } +} +#[derive(Debug)] +pub struct DxilPassthroughShader { + pub shader: Vec<u8>, +} + +#[derive(Debug)] +pub struct HlslPassthroughShader { + pub shader: String, +} + +#[derive(Debug)] +pub enum ShaderModuleSource { + Naga(crate::NagaShader), + DxilPassthrough(DxilPassthroughShader), + HlslPassthrough(HlslPassthroughShader), +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub enum FeatureLevel { + V11_0, + V11_1, + V12_0, + V12_1, + V12_2, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub enum ShaderModel { + V5_1, + V6_0, + V6_1, + V6_2, + V6_3, + V6_4, + V6_5, + V6_6, + V6_7, + V6_8, + V6_9, +} diff --git a/third_party/wgpu-hal/src/dx12/pipeline_desc.rs b/third_party/wgpu-hal/src/dx12/pipeline_desc.rs new file mode 100644 index 000000000..9fcf5661e --- /dev/null +++ b/third_party/wgpu-hal/src/dx12/pipeline_desc.rs @@ -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()); + } +} diff --git a/third_party/wgpu-hal/src/dx12/sampler.rs b/third_party/wgpu-hal/src/dx12/sampler.rs new file mode 100644 index 000000000..4b58a0b42 --- /dev/null +++ b/third_party/wgpu-hal/src/dx12/sampler.rs @@ -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(); + } + } +} diff --git a/third_party/wgpu-hal/src/dx12/shader_compilation.rs b/third_party/wgpu-hal/src/dx12/shader_compilation.rs new file mode 100644 index 000000000..ff41e503a --- /dev/null +++ b/third_party/wgpu-hal/src/dx12/shader_compilation.rs @@ -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)) +} diff --git a/third_party/wgpu-hal/src/dx12/suballocation.rs b/third_party/wgpu-hal/src/dx12/suballocation.rs new file mode 100644 index 000000000..89c7dc479 --- /dev/null +++ b/third_party/wgpu-hal/src/dx12/suballocation.rs @@ -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) + } +} diff --git a/third_party/wgpu-hal/src/dx12/types.rs b/third_party/wgpu-hal/src/dx12/types.rs new file mode 100644 index 000000000..5270c6ca8 --- /dev/null +++ b/third_party/wgpu-hal/src/dx12/types.rs @@ -0,0 +1,39 @@ +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] + +use windows::Win32::Graphics::Dxgi; + +windows_core::imp::define_interface!( + ISwapChainPanelNative, + ISwapChainPanelNative_Vtbl, + 0x63aad0b8_7c24_40ff_85a8_640d944cc325 +); +impl core::ops::Deref for ISwapChainPanelNative { + type Target = windows_core::IUnknown; + fn deref(&self) -> &Self::Target { + unsafe { core::mem::transmute(self) } + } +} +windows_core::imp::interface_hierarchy!(ISwapChainPanelNative, windows_core::IUnknown); +impl ISwapChainPanelNative { + pub unsafe fn SetSwapChain<P0>(&self, swap_chain: P0) -> windows_core::Result<()> + where + P0: windows_core::Param<Dxgi::IDXGISwapChain1>, + { + unsafe { + (windows_core::Interface::vtable(self).SetSwapChain)( + windows_core::Interface::as_raw(self), + swap_chain.param().abi(), + ) + } + .ok() + } +} +#[repr(C)] +pub struct ISwapChainPanelNative_Vtbl { + pub base__: windows_core::IUnknown_Vtbl, + pub SetSwapChain: unsafe extern "system" fn( + swap_chain_panel_native: *mut core::ffi::c_void, + swap_chain: *mut core::ffi::c_void, + ) -> windows_core::HRESULT, +} diff --git a/third_party/wgpu-hal/src/dx12/view.rs b/third_party/wgpu-hal/src/dx12/view.rs new file mode 100644 index 000000000..ba715833f --- /dev/null +++ b/third_party/wgpu-hal/src/dx12/view.rs @@ -0,0 +1,370 @@ +use windows::Win32::Graphics::{Direct3D12, Dxgi}; + +use crate::auxil; + +pub(super) struct ViewDescriptor { + dimension: wgt::TextureViewDimension, + pub aspects: crate::FormatAspects, + pub rtv_dsv_format: Dxgi::Common::DXGI_FORMAT, + srv_uav_format: Option<Dxgi::Common::DXGI_FORMAT>, + multisampled: bool, + array_layer_base: u32, + array_layer_count: u32, + mip_level_base: u32, + mip_level_count: u32, + plane_slice_override: Option<u32>, +} + +impl crate::TextureViewDescriptor<'_> { + pub(super) fn to_internal(&self, texture: &super::Texture) -> ViewDescriptor { + let aspects = crate::FormatAspects::new(texture.format, self.range.aspect); + + ViewDescriptor { + dimension: self.dimension, + aspects, + rtv_dsv_format: auxil::dxgi::conv::map_texture_format(self.format), + srv_uav_format: auxil::dxgi::conv::map_texture_format_for_srv_uav(self.format, aspects), + multisampled: texture.sample_count > 1, + mip_level_base: self.range.base_mip_level, + mip_level_count: self.range.mip_level_count.unwrap_or(!0), + array_layer_base: self.range.base_array_layer, + array_layer_count: self.range.array_layer_count.unwrap_or(!0), + plane_slice_override: texture.plane_slice_override(), + } + } +} + +fn aspects_to_plane(aspects: crate::FormatAspects) -> u32 { + match aspects { + crate::FormatAspects::STENCIL => 1, + crate::FormatAspects::PLANE_1 => 1, + crate::FormatAspects::PLANE_2 => 2, + _ => 0, + } +} + +impl ViewDescriptor { + pub(super) fn plane_slice(&self) -> u32 { + self.plane_slice_override + .unwrap_or_else(|| aspects_to_plane(self.aspects)) + } +} + +/// Shader component mapping for stencil views +/// +/// Stencil views use `DXGI_FORMAT_X24_TYPELESS_G8_UINT` or +/// `DXGI_FORMAT_X32_TYPELESS_G8X24_UINT`, which have the stencil value in +/// the green component. WebGPU specifies that the stencil value be in the +/// red component. It also specifies that the remaining components _should_ +/// be (0, 0, 1), but may have an unspecified value. +const STENCIL_COMPONENT_MAPPING: Direct3D12::D3D12_SHADER_COMPONENT_MAPPING = + super::conv::make_shader_component_mapping( + Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_FROM_MEMORY_COMPONENT_1, + Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0, + Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0, + Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_1, + ); + +impl ViewDescriptor { + pub(crate) unsafe fn to_srv(&self) -> Option<Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC> { + let swizzle = if self.aspects == crate::FormatAspects::STENCIL { + STENCIL_COMPONENT_MAPPING.0 as u32 + } else { + Direct3D12::D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING + }; + + let mut desc = Direct3D12::D3D12_SHADER_RESOURCE_VIEW_DESC { + Format: self.srv_uav_format?, + ViewDimension: Direct3D12::D3D12_SRV_DIMENSION_UNKNOWN, + Shader4ComponentMapping: swizzle, + Anonymous: Default::default(), + }; + + match self.dimension { + wgt::TextureViewDimension::D1 => { + desc.ViewDimension = Direct3D12::D3D12_SRV_DIMENSION_TEXTURE1D; + desc.Anonymous.Texture1D = Direct3D12::D3D12_TEX1D_SRV { + MostDetailedMip: self.mip_level_base, + MipLevels: self.mip_level_count, + ResourceMinLODClamp: 0.0, + } + } + /* + wgt::TextureViewDimension::D1Array => { + desc.ViewDimension = Direct3D12::D3D12_SRV_DIMENSION_TEXTURE1DARRAY; + desc.Anonymous.Texture1DArray = Direct3D12::D3D12_TEX1D_ARRAY_SRV { + MostDetailedMip: self.mip_level_base, + MipLevels: self.mip_level_count, + FirstArraySlice: self.array_layer_base, + ArraySize: self.array_layer_count, + ResourceMinLODClamp: 0.0, + } + } + */ + wgt::TextureViewDimension::D2 if self.multisampled && self.array_layer_base == 0 => { + desc.ViewDimension = Direct3D12::D3D12_SRV_DIMENSION_TEXTURE2DMS; + desc.Anonymous.Texture2DMS = Direct3D12::D3D12_TEX2DMS_SRV { + UnusedField_NothingToDefine: 0, + } + } + wgt::TextureViewDimension::D2 if self.array_layer_base == 0 => { + desc.ViewDimension = Direct3D12::D3D12_SRV_DIMENSION_TEXTURE2D; + desc.Anonymous.Texture2D = Direct3D12::D3D12_TEX2D_SRV { + MostDetailedMip: self.mip_level_base, + MipLevels: self.mip_level_count, + PlaneSlice: self.plane_slice(), + ResourceMinLODClamp: 0.0, + } + } + wgt::TextureViewDimension::D2 | wgt::TextureViewDimension::D2Array + if self.multisampled => + { + desc.ViewDimension = Direct3D12::D3D12_SRV_DIMENSION_TEXTURE2DMSARRAY; + desc.Anonymous.Texture2DMSArray = Direct3D12::D3D12_TEX2DMS_ARRAY_SRV { + FirstArraySlice: self.array_layer_base, + ArraySize: self.array_layer_count, + } + } + wgt::TextureViewDimension::D2 | wgt::TextureViewDimension::D2Array => { + desc.ViewDimension = Direct3D12::D3D12_SRV_DIMENSION_TEXTURE2DARRAY; + desc.Anonymous.Texture2DArray = Direct3D12::D3D12_TEX2D_ARRAY_SRV { + MostDetailedMip: self.mip_level_base, + MipLevels: self.mip_level_count, + FirstArraySlice: self.array_layer_base, + ArraySize: self.array_layer_count, + PlaneSlice: self.plane_slice(), + ResourceMinLODClamp: 0.0, + } + } + wgt::TextureViewDimension::D3 => { + desc.ViewDimension = Direct3D12::D3D12_SRV_DIMENSION_TEXTURE3D; + desc.Anonymous.Texture3D = Direct3D12::D3D12_TEX3D_SRV { + MostDetailedMip: self.mip_level_base, + MipLevels: self.mip_level_count, + ResourceMinLODClamp: 0.0, + } + } + wgt::TextureViewDimension::Cube if self.array_layer_base == 0 => { + desc.ViewDimension = Direct3D12::D3D12_SRV_DIMENSION_TEXTURECUBE; + desc.Anonymous.TextureCube = Direct3D12::D3D12_TEXCUBE_SRV { + MostDetailedMip: self.mip_level_base, + MipLevels: self.mip_level_count, + ResourceMinLODClamp: 0.0, + } + } + wgt::TextureViewDimension::Cube | wgt::TextureViewDimension::CubeArray => { + desc.ViewDimension = Direct3D12::D3D12_SRV_DIMENSION_TEXTURECUBEARRAY; + desc.Anonymous.TextureCubeArray = Direct3D12::D3D12_TEXCUBE_ARRAY_SRV { + MostDetailedMip: self.mip_level_base, + MipLevels: self.mip_level_count, + First2DArrayFace: self.array_layer_base, + NumCubes: if self.array_layer_count == !0 { + !0 + } else { + self.array_layer_count / 6 + }, + ResourceMinLODClamp: 0.0, + } + } + } + + Some(desc) + } + + pub(crate) unsafe fn to_uav(&self) -> Option<Direct3D12::D3D12_UNORDERED_ACCESS_VIEW_DESC> { + let mut desc = Direct3D12::D3D12_UNORDERED_ACCESS_VIEW_DESC { + Format: self.srv_uav_format?, + ViewDimension: Direct3D12::D3D12_UAV_DIMENSION_UNKNOWN, + Anonymous: Default::default(), + }; + + match self.dimension { + wgt::TextureViewDimension::D1 => { + desc.ViewDimension = Direct3D12::D3D12_UAV_DIMENSION_TEXTURE1D; + desc.Anonymous.Texture1D = Direct3D12::D3D12_TEX1D_UAV { + MipSlice: self.mip_level_base, + } + } + /* + wgt::TextureViewDimension::D1Array => { + desc.ViewDimension = Direct3D12::D3D12_UAV_DIMENSION_TEXTURE1DARRAY; + desc.Anonymous.Texture1DArray = Direct3D12::D3D12_TEX1D_ARRAY_UAV { + MipSlice: self.mip_level_base, + FirstArraySlice: self.array_layer_base, + ArraySize, + } + }*/ + wgt::TextureViewDimension::D2 if self.array_layer_base == 0 => { + desc.ViewDimension = Direct3D12::D3D12_UAV_DIMENSION_TEXTURE2D; + desc.Anonymous.Texture2D = Direct3D12::D3D12_TEX2D_UAV { + MipSlice: self.mip_level_base, + PlaneSlice: self.plane_slice(), + } + } + wgt::TextureViewDimension::D2 | wgt::TextureViewDimension::D2Array => { + desc.ViewDimension = Direct3D12::D3D12_UAV_DIMENSION_TEXTURE2DARRAY; + desc.Anonymous.Texture2DArray = Direct3D12::D3D12_TEX2D_ARRAY_UAV { + MipSlice: self.mip_level_base, + FirstArraySlice: self.array_layer_base, + ArraySize: self.array_layer_count, + PlaneSlice: self.plane_slice(), + } + } + wgt::TextureViewDimension::D3 => { + desc.ViewDimension = Direct3D12::D3D12_UAV_DIMENSION_TEXTURE3D; + desc.Anonymous.Texture3D = Direct3D12::D3D12_TEX3D_UAV { + MipSlice: self.mip_level_base, + FirstWSlice: 0, + WSize: u32::MAX, + } + } + wgt::TextureViewDimension::Cube | wgt::TextureViewDimension::CubeArray => { + panic!("Unable to view texture as cube UAV") + } + } + + Some(desc) + } + + pub(crate) unsafe fn to_rtv(&self) -> Direct3D12::D3D12_RENDER_TARGET_VIEW_DESC { + let mut desc = Direct3D12::D3D12_RENDER_TARGET_VIEW_DESC { + Format: self.rtv_dsv_format, + ViewDimension: Direct3D12::D3D12_RTV_DIMENSION_UNKNOWN, + Anonymous: Default::default(), + }; + + match self.dimension { + wgt::TextureViewDimension::D1 => { + desc.ViewDimension = Direct3D12::D3D12_RTV_DIMENSION_TEXTURE1D; + desc.Anonymous.Texture1D = Direct3D12::D3D12_TEX1D_RTV { + MipSlice: self.mip_level_base, + } + } + /* + wgt::TextureViewDimension::D1Array => { + desc.ViewDimension = Direct3D12::D3D12_RTV_DIMENSION_TEXTURE1DARRAY; + desc.Anonymous.Texture1DArray = Direct3D12::D3D12_TEX1D_ARRAY_RTV { + MipSlice: self.mip_level_base, + FirstArraySlice: self.array_layer_base, + ArraySize, + } + }*/ + wgt::TextureViewDimension::D2 if self.multisampled && self.array_layer_base == 0 => { + desc.ViewDimension = Direct3D12::D3D12_RTV_DIMENSION_TEXTURE2DMS; + desc.Anonymous.Texture2DMS = Direct3D12::D3D12_TEX2DMS_RTV { + UnusedField_NothingToDefine: 0, + } + } + wgt::TextureViewDimension::D2 if self.array_layer_base == 0 => { + desc.ViewDimension = Direct3D12::D3D12_RTV_DIMENSION_TEXTURE2D; + desc.Anonymous.Texture2D = Direct3D12::D3D12_TEX2D_RTV { + MipSlice: self.mip_level_base, + PlaneSlice: self.plane_slice(), + } + } + wgt::TextureViewDimension::D2 | wgt::TextureViewDimension::D2Array + if self.multisampled => + { + desc.ViewDimension = Direct3D12::D3D12_RTV_DIMENSION_TEXTURE2DMSARRAY; + desc.Anonymous.Texture2DMSArray = Direct3D12::D3D12_TEX2DMS_ARRAY_RTV { + FirstArraySlice: self.array_layer_base, + ArraySize: self.array_layer_count, + } + } + wgt::TextureViewDimension::D2 | wgt::TextureViewDimension::D2Array => { + desc.ViewDimension = Direct3D12::D3D12_RTV_DIMENSION_TEXTURE2DARRAY; + desc.Anonymous.Texture2DArray = Direct3D12::D3D12_TEX2D_ARRAY_RTV { + MipSlice: self.mip_level_base, + FirstArraySlice: self.array_layer_base, + ArraySize: self.array_layer_count, + PlaneSlice: self.plane_slice(), + } + } + wgt::TextureViewDimension::D3 + | wgt::TextureViewDimension::Cube + | wgt::TextureViewDimension::CubeArray => { + panic!("Unable to view texture as cube or 3D RTV") + } + } + + desc + } + + pub(crate) unsafe fn to_dsv( + &self, + read_only: bool, + ) -> Direct3D12::D3D12_DEPTH_STENCIL_VIEW_DESC { + let mut desc = Direct3D12::D3D12_DEPTH_STENCIL_VIEW_DESC { + Format: self.rtv_dsv_format, + ViewDimension: Direct3D12::D3D12_DSV_DIMENSION_UNKNOWN, + Flags: { + let mut flags = Direct3D12::D3D12_DSV_FLAG_NONE; + if read_only { + if self.aspects.contains(crate::FormatAspects::DEPTH) { + flags |= Direct3D12::D3D12_DSV_FLAG_READ_ONLY_DEPTH; + } + if self.aspects.contains(crate::FormatAspects::STENCIL) { + flags |= Direct3D12::D3D12_DSV_FLAG_READ_ONLY_STENCIL; + } + } + flags + }, + Anonymous: Default::default(), + }; + + match self.dimension { + wgt::TextureViewDimension::D1 => { + desc.ViewDimension = Direct3D12::D3D12_DSV_DIMENSION_TEXTURE1D; + desc.Anonymous.Texture1D = Direct3D12::D3D12_TEX1D_DSV { + MipSlice: self.mip_level_base, + } + } + /* + wgt::TextureViewDimension::D1Array => { + desc.ViewDimension = Direct3D12::D3D12_DSV_DIMENSION_TEXTURE1DARRAY; + desc.Anonymous.Texture1DArray = Direct3D12::D3D12_TEX1D_ARRAY_DSV { + MipSlice: self.mip_level_base, + FirstArraySlice: self.array_layer_base, + ArraySize, + } + }*/ + wgt::TextureViewDimension::D2 if self.multisampled && self.array_layer_base == 0 => { + desc.ViewDimension = Direct3D12::D3D12_DSV_DIMENSION_TEXTURE2DMS; + desc.Anonymous.Texture2DMS = Direct3D12::D3D12_TEX2DMS_DSV { + UnusedField_NothingToDefine: 0, + } + } + wgt::TextureViewDimension::D2 if self.array_layer_base == 0 => { + desc.ViewDimension = Direct3D12::D3D12_DSV_DIMENSION_TEXTURE2D; + + desc.Anonymous.Texture2D = Direct3D12::D3D12_TEX2D_DSV { + MipSlice: self.mip_level_base, + } + } + wgt::TextureViewDimension::D2 | wgt::TextureViewDimension::D2Array + if self.multisampled => + { + desc.ViewDimension = Direct3D12::D3D12_DSV_DIMENSION_TEXTURE2DMSARRAY; + desc.Anonymous.Texture2DMSArray = Direct3D12::D3D12_TEX2DMS_ARRAY_DSV { + FirstArraySlice: self.array_layer_base, + ArraySize: self.array_layer_count, + } + } + wgt::TextureViewDimension::D2 | wgt::TextureViewDimension::D2Array => { + desc.ViewDimension = Direct3D12::D3D12_DSV_DIMENSION_TEXTURE2DARRAY; + desc.Anonymous.Texture2DArray = Direct3D12::D3D12_TEX2D_ARRAY_DSV { + MipSlice: self.mip_level_base, + FirstArraySlice: self.array_layer_base, + ArraySize: self.array_layer_count, + } + } + wgt::TextureViewDimension::D3 + | wgt::TextureViewDimension::Cube + | wgt::TextureViewDimension::CubeArray => { + panic!("Unable to view texture as cube or 3D DSV") + } + } + + desc + } +} diff --git a/third_party/wgpu-hal/src/dynamic/adapter.rs b/third_party/wgpu-hal/src/dynamic/adapter.rs new file mode 100644 index 000000000..4921d7163 --- /dev/null +++ b/third_party/wgpu-hal/src/dynamic/adapter.rs @@ -0,0 +1,95 @@ +use alloc::boxed::Box; + +use crate::{ + Adapter, Api, DeviceError, OpenDevice, SurfaceCapabilities, TextureFormatCapabilities, +}; + +use super::{DynDevice, DynQueue, DynResource, DynResourceExt, DynSurface}; + +#[expect(missing_debug_implementations, reason = "dyn")] +pub struct DynOpenDevice { + pub device: Box<dyn DynDevice>, + pub queue: Box<dyn DynQueue>, +} + +impl<A: Api> From<OpenDevice<A>> for DynOpenDevice { + fn from(open_device: OpenDevice<A>) -> Self { + Self { + device: Box::new(open_device.device), + queue: Box::new(open_device.queue), + } + } +} + +pub trait DynAdapter: DynResource { + unsafe fn open( + &self, + features: wgt::Features, + limits: &wgt::Limits, + memory_hints: &wgt::MemoryHints, + ) -> Result<DynOpenDevice, DeviceError>; + + unsafe fn texture_format_capabilities( + &self, + format: wgt::TextureFormat, + ) -> TextureFormatCapabilities; + + unsafe fn surface_capabilities(&self, surface: &dyn DynSurface) -> Option<SurfaceCapabilities>; + + unsafe fn surface_display_hdr_info( + &self, + surface: &dyn DynSurface, + ) -> Option<wgt::DisplayHdrInfo>; + + unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp; + + fn get_ordered_buffer_usages(&self) -> wgt::BufferUses; + + fn get_ordered_texture_usages(&self) -> wgt::TextureUses; +} + +impl<A: Adapter + DynResource> DynAdapter for A { + unsafe fn open( + &self, + features: wgt::Features, + limits: &wgt::Limits, + memory_hints: &wgt::MemoryHints, + ) -> Result<DynOpenDevice, DeviceError> { + unsafe { A::open(self, features, limits, memory_hints) }.map(|open_device| DynOpenDevice { + device: Box::new(open_device.device), + queue: Box::new(open_device.queue), + }) + } + + unsafe fn texture_format_capabilities( + &self, + format: wgt::TextureFormat, + ) -> TextureFormatCapabilities { + unsafe { A::texture_format_capabilities(self, format) } + } + + unsafe fn surface_capabilities(&self, surface: &dyn DynSurface) -> Option<SurfaceCapabilities> { + let surface = surface.expect_downcast_ref(); + unsafe { A::surface_capabilities(self, surface) } + } + + unsafe fn surface_display_hdr_info( + &self, + surface: &dyn DynSurface, + ) -> Option<wgt::DisplayHdrInfo> { + let surface = surface.expect_downcast_ref(); + unsafe { A::surface_display_hdr_info(self, surface) } + } + + unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp { + unsafe { A::get_presentation_timestamp(self) } + } + + fn get_ordered_buffer_usages(&self) -> wgt::BufferUses { + A::get_ordered_buffer_usages(self) + } + + fn get_ordered_texture_usages(&self) -> wgt::TextureUses { + A::get_ordered_texture_usages(self) + } +} diff --git a/third_party/wgpu-hal/src/dynamic/command.rs b/third_party/wgpu-hal/src/dynamic/command.rs new file mode 100644 index 000000000..aca5bbcb3 --- /dev/null +++ b/third_party/wgpu-hal/src/dynamic/command.rs @@ -0,0 +1,823 @@ +use alloc::{boxed::Box, vec::Vec}; +use core::ops::Range; + +use crate::{ + AccelerationStructureBarrier, Api, Attachment, BufferBarrier, BufferBinding, BufferCopy, + BufferTextureCopy, BuildAccelerationStructureDescriptor, ColorAttachment, CommandEncoder, + ComputePassDescriptor, DepthStencilAttachment, DeviceError, DynRayTracingPipeline, Label, + MemoryRange, PassTimestampWrites, RayTracingPassDescriptor, Rect, RenderPassDescriptor, + TextureBarrier, TextureCopy, +}; + +use super::{ + DynAccelerationStructure, DynBindGroup, DynBuffer, DynCommandBuffer, DynComputePipeline, + DynPipelineLayout, DynQuerySet, DynRenderPipeline, DynResource, DynResourceExt as _, + DynTexture, DynTextureView, +}; + +pub trait DynCommandEncoder: DynResource + core::fmt::Debug { + unsafe fn begin_encoding(&mut self, label: Label) -> Result<(), DeviceError>; + + unsafe fn discard_encoding(&mut self); + + unsafe fn end_encoding(&mut self) -> Result<Box<dyn DynCommandBuffer>, DeviceError>; + + unsafe fn reset_all(&mut self, command_buffers: Vec<Box<dyn DynCommandBuffer>>); + + unsafe fn transition_buffers(&mut self, barriers: &[BufferBarrier<'_, dyn DynBuffer>]); + unsafe fn transition_textures(&mut self, barriers: &[TextureBarrier<'_, dyn DynTexture>]); + + unsafe fn clear_buffer(&mut self, buffer: &dyn DynBuffer, range: MemoryRange); + + unsafe fn copy_buffer_to_buffer( + &mut self, + src: &dyn DynBuffer, + dst: &dyn DynBuffer, + regions: &[BufferCopy], + ); + + unsafe fn copy_texture_to_texture( + &mut self, + src: &dyn DynTexture, + src_usage: wgt::TextureUses, + dst: &dyn DynTexture, + regions: &[TextureCopy], + ); + + unsafe fn copy_buffer_to_texture( + &mut self, + src: &dyn DynBuffer, + dst: &dyn DynTexture, + regions: &[BufferTextureCopy], + ); + + unsafe fn copy_texture_to_buffer( + &mut self, + src: &dyn DynTexture, + src_usage: wgt::TextureUses, + dst: &dyn DynBuffer, + regions: &[BufferTextureCopy], + ); + + unsafe fn set_bind_group( + &mut self, + layout: &dyn DynPipelineLayout, + index: u32, + group: &dyn DynBindGroup, + dynamic_offsets: &[wgt::DynamicOffset], + ); + + unsafe fn set_immediates( + &mut self, + layout: &dyn DynPipelineLayout, + offset_bytes: u32, + data: &[u32], + ); + + unsafe fn insert_debug_marker(&mut self, label: &str); + unsafe fn begin_debug_marker(&mut self, group_label: &str); + unsafe fn end_debug_marker(&mut self); + + unsafe fn begin_query(&mut self, set: &dyn DynQuerySet, index: u32); + unsafe fn end_query(&mut self, set: &dyn DynQuerySet, index: u32); + unsafe fn write_timestamp(&mut self, set: &dyn DynQuerySet, index: u32); + unsafe fn reset_queries(&mut self, set: &dyn DynQuerySet, range: Range<u32>); + unsafe fn copy_query_results( + &mut self, + set: &dyn DynQuerySet, + range: Range<u32>, + buffer: &dyn DynBuffer, + offset: wgt::BufferAddress, + stride: wgt::BufferSize, + ); + + unsafe fn begin_render_pass( + &mut self, + desc: &RenderPassDescriptor<dyn DynQuerySet, dyn DynTextureView>, + ) -> Result<(), DeviceError>; + unsafe fn end_render_pass(&mut self); + + unsafe fn set_render_pipeline(&mut self, pipeline: &dyn DynRenderPipeline); + + unsafe fn set_index_buffer<'a>( + &mut self, + binding: BufferBinding<'a, dyn DynBuffer>, + format: wgt::IndexFormat, + ); + + unsafe fn set_vertex_buffer<'a>( + &mut self, + index: u32, + binding: BufferBinding<'a, dyn DynBuffer>, + ); + unsafe fn set_viewport(&mut self, rect: &Rect<f32>, depth_range: Range<f32>); + unsafe fn set_scissor_rect(&mut self, rect: &Rect<u32>); + unsafe fn set_stencil_reference(&mut self, value: u32); + unsafe fn set_blend_constants(&mut self, color: &[f32; 4]); + + unsafe fn draw( + &mut self, + first_vertex: u32, + vertex_count: u32, + first_instance: u32, + instance_count: u32, + ); + unsafe fn draw_indexed( + &mut self, + first_index: u32, + index_count: u32, + base_vertex: i32, + first_instance: u32, + instance_count: u32, + ); + unsafe fn draw_mesh_tasks( + &mut self, + group_count_x: u32, + group_count_y: u32, + group_count_z: u32, + ); + unsafe fn draw_indirect( + &mut self, + buffer: &dyn DynBuffer, + offset: wgt::BufferAddress, + draw_count: u32, + ); + unsafe fn draw_indexed_indirect( + &mut self, + buffer: &dyn DynBuffer, + offset: wgt::BufferAddress, + draw_count: u32, + ); + unsafe fn draw_mesh_tasks_indirect( + &mut self, + buffer: &dyn DynBuffer, + offset: wgt::BufferAddress, + draw_count: u32, + ); + unsafe fn draw_indirect_count( + &mut self, + buffer: &dyn DynBuffer, + offset: wgt::BufferAddress, + count_buffer: &dyn DynBuffer, + count_offset: wgt::BufferAddress, + max_count: u32, + ); + unsafe fn draw_indexed_indirect_count( + &mut self, + buffer: &dyn DynBuffer, + offset: wgt::BufferAddress, + count_buffer: &dyn DynBuffer, + count_offset: wgt::BufferAddress, + max_count: u32, + ); + unsafe fn draw_mesh_tasks_indirect_count( + &mut self, + buffer: &dyn DynBuffer, + offset: wgt::BufferAddress, + count_buffer: &dyn DynBuffer, + count_offset: wgt::BufferAddress, + max_count: u32, + ); + + unsafe fn begin_compute_pass(&mut self, desc: &ComputePassDescriptor<dyn DynQuerySet>); + unsafe fn end_compute_pass(&mut self); + + unsafe fn set_compute_pipeline(&mut self, pipeline: &dyn DynComputePipeline); + + unsafe fn dispatch_workgroups(&mut self, count: [u32; 3]); + unsafe fn dispatch_workgroups_indirect( + &mut self, + buffer: &dyn DynBuffer, + offset: wgt::BufferAddress, + ); + + unsafe fn begin_ray_tracing_pass(&mut self, desc: &RayTracingPassDescriptor); + unsafe fn end_ray_tracing_pass(&mut self); + + unsafe fn trace_rays( + &mut self, + count: [u32; 3], + ray_generation_group_data: crate::PipelineGroupData<dyn DynBuffer>, + miss_group_data: crate::PipelineGroupData<dyn DynBuffer>, + intersection_group_data: crate::PipelineGroupData<dyn DynBuffer>, + ); + + unsafe fn set_ray_tracing_pipeline(&mut self, pipeline: &dyn DynRayTracingPipeline); + + unsafe fn build_acceleration_structures<'a>( + &mut self, + descriptors: &'a [BuildAccelerationStructureDescriptor< + 'a, + dyn DynBuffer, + dyn DynAccelerationStructure, + >], + ); + unsafe fn place_acceleration_structure_barrier( + &mut self, + barrier: AccelerationStructureBarrier, + ); + unsafe fn copy_acceleration_structure_to_acceleration_structure( + &mut self, + src: &dyn DynAccelerationStructure, + dst: &dyn DynAccelerationStructure, + copy: wgt::AccelerationStructureCopy, + ); + unsafe fn read_acceleration_structure_compact_size( + &mut self, + acceleration_structure: &dyn DynAccelerationStructure, + buf: &dyn DynBuffer, + ); + unsafe fn set_acceleration_structure_dependencies( + &self, + command_buffers: &[Box<dyn DynCommandBuffer>], + dependencies: &[&dyn DynAccelerationStructure], + ); +} + +impl<C: CommandEncoder + DynResource> DynCommandEncoder for C { + unsafe fn begin_encoding(&mut self, label: Label) -> Result<(), DeviceError> { + unsafe { C::begin_encoding(self, label) } + } + + unsafe fn discard_encoding(&mut self) { + unsafe { C::discard_encoding(self) } + } + + unsafe fn end_encoding(&mut self) -> Result<Box<dyn DynCommandBuffer>, DeviceError> { + unsafe { C::end_encoding(self) }.map(|cb| { + let boxed_command_buffer: Box<<C::A as Api>::CommandBuffer> = Box::new(cb); + let boxed_command_buffer: Box<dyn DynCommandBuffer> = boxed_command_buffer; + boxed_command_buffer + }) + } + + unsafe fn reset_all(&mut self, command_buffers: Vec<Box<dyn DynCommandBuffer>>) { + unsafe { C::reset_all(self, command_buffers.into_iter().map(|cb| cb.unbox())) } + } + + unsafe fn transition_buffers(&mut self, barriers: &[BufferBarrier<'_, dyn DynBuffer>]) { + let barriers = barriers.iter().map(|barrier| BufferBarrier { + buffer: barrier.buffer.expect_downcast_ref(), + usage: barrier.usage.clone(), + }); + unsafe { self.transition_buffers(barriers) }; + } + + unsafe fn transition_textures(&mut self, barriers: &[TextureBarrier<'_, dyn DynTexture>]) { + let barriers = barriers.iter().map(|barrier| TextureBarrier { + texture: barrier.texture.expect_downcast_ref(), + usage: barrier.usage.clone(), + range: barrier.range, + }); + unsafe { self.transition_textures(barriers) }; + } + + unsafe fn clear_buffer(&mut self, buffer: &dyn DynBuffer, range: MemoryRange) { + let buffer = buffer.expect_downcast_ref(); + unsafe { C::clear_buffer(self, buffer, range) }; + } + + unsafe fn copy_buffer_to_buffer( + &mut self, + src: &dyn DynBuffer, + dst: &dyn DynBuffer, + regions: &[BufferCopy], + ) { + let src = src.expect_downcast_ref(); + let dst = dst.expect_downcast_ref(); + unsafe { + C::copy_buffer_to_buffer(self, src, dst, regions.iter().copied()); + } + } + + unsafe fn copy_texture_to_texture( + &mut self, + src: &dyn DynTexture, + src_usage: wgt::TextureUses, + dst: &dyn DynTexture, + regions: &[TextureCopy], + ) { + let src = src.expect_downcast_ref(); + let dst = dst.expect_downcast_ref(); + unsafe { + C::copy_texture_to_texture(self, src, src_usage, dst, regions.iter().cloned()); + } + } + + unsafe fn copy_buffer_to_texture( + &mut self, + src: &dyn DynBuffer, + dst: &dyn DynTexture, + regions: &[BufferTextureCopy], + ) { + let src = src.expect_downcast_ref(); + let dst = dst.expect_downcast_ref(); + unsafe { + C::copy_buffer_to_texture(self, src, dst, regions.iter().cloned()); + } + } + + unsafe fn copy_texture_to_buffer( + &mut self, + src: &dyn DynTexture, + src_usage: wgt::TextureUses, + dst: &dyn DynBuffer, + regions: &[BufferTextureCopy], + ) { + let src = src.expect_downcast_ref(); + let dst = dst.expect_downcast_ref(); + unsafe { + C::copy_texture_to_buffer(self, src, src_usage, dst, regions.iter().cloned()); + } + } + + unsafe fn set_bind_group( + &mut self, + layout: &dyn DynPipelineLayout, + index: u32, + group: &dyn DynBindGroup, + dynamic_offsets: &[wgt::DynamicOffset], + ) { + let layout = layout.expect_downcast_ref(); + let group = group.expect_downcast_ref(); + unsafe { C::set_bind_group(self, layout, index, group, dynamic_offsets) }; + } + + unsafe fn set_immediates( + &mut self, + layout: &dyn DynPipelineLayout, + offset_bytes: u32, + data: &[u32], + ) { + let layout = layout.expect_downcast_ref(); + unsafe { C::set_immediates(self, layout, offset_bytes, data) }; + } + + unsafe fn insert_debug_marker(&mut self, label: &str) { + unsafe { + C::insert_debug_marker(self, label); + } + } + + unsafe fn begin_debug_marker(&mut self, group_label: &str) { + unsafe { + C::begin_debug_marker(self, group_label); + } + } + + unsafe fn end_debug_marker(&mut self) { + unsafe { + C::end_debug_marker(self); + } + } + + unsafe fn begin_query(&mut self, set: &dyn DynQuerySet, index: u32) { + let set = set.expect_downcast_ref(); + unsafe { C::begin_query(self, set, index) }; + } + + unsafe fn end_query(&mut self, set: &dyn DynQuerySet, index: u32) { + let set = set.expect_downcast_ref(); + unsafe { C::end_query(self, set, index) }; + } + + unsafe fn write_timestamp(&mut self, set: &dyn DynQuerySet, index: u32) { + let set = set.expect_downcast_ref(); + unsafe { C::write_timestamp(self, set, index) }; + } + + unsafe fn reset_queries(&mut self, set: &dyn DynQuerySet, range: Range<u32>) { + let set = set.expect_downcast_ref(); + unsafe { C::reset_queries(self, set, range) }; + } + + unsafe fn copy_query_results( + &mut self, + set: &dyn DynQuerySet, + range: Range<u32>, + buffer: &dyn DynBuffer, + offset: wgt::BufferAddress, + stride: wgt::BufferSize, + ) { + let set = set.expect_downcast_ref(); + let buffer = buffer.expect_downcast_ref(); + unsafe { C::copy_query_results(self, set, range, buffer, offset, stride) }; + } + + unsafe fn begin_render_pass( + &mut self, + desc: &RenderPassDescriptor<dyn DynQuerySet, dyn DynTextureView>, + ) -> Result<(), DeviceError> { + let color_attachments = desc + .color_attachments + .iter() + .map(|attachment| { + attachment + .as_ref() + .map(|attachment| attachment.expect_downcast()) + }) + .collect::<Vec<_>>(); + + let desc: RenderPassDescriptor<<C::A as Api>::QuerySet, <C::A as Api>::TextureView> = + RenderPassDescriptor { + label: desc.label, + extent: desc.extent, + sample_count: desc.sample_count, + color_attachments: &color_attachments, + depth_stencil_attachment: desc + .depth_stencil_attachment + .as_ref() + .map(|ds| ds.expect_downcast()), + multiview_mask: desc.multiview_mask, + timestamp_writes: desc + .timestamp_writes + .as_ref() + .map(|writes| writes.expect_downcast()), + occlusion_query_set: desc + .occlusion_query_set + .map(|set| set.expect_downcast_ref()), + }; + unsafe { C::begin_render_pass(self, &desc) } + } + + unsafe fn end_render_pass(&mut self) { + unsafe { + C::end_render_pass(self); + } + } + + unsafe fn set_viewport(&mut self, rect: &Rect<f32>, depth_range: Range<f32>) { + unsafe { + C::set_viewport(self, rect, depth_range); + } + } + + unsafe fn set_scissor_rect(&mut self, rect: &Rect<u32>) { + unsafe { + C::set_scissor_rect(self, rect); + } + } + + unsafe fn set_stencil_reference(&mut self, value: u32) { + unsafe { + C::set_stencil_reference(self, value); + } + } + + unsafe fn set_blend_constants(&mut self, color: &[f32; 4]) { + unsafe { C::set_blend_constants(self, color) }; + } + + unsafe fn draw( + &mut self, + first_vertex: u32, + vertex_count: u32, + first_instance: u32, + instance_count: u32, + ) { + unsafe { + C::draw( + self, + first_vertex, + vertex_count, + first_instance, + instance_count, + ) + }; + } + + unsafe fn draw_indexed( + &mut self, + first_index: u32, + index_count: u32, + base_vertex: i32, + first_instance: u32, + instance_count: u32, + ) { + unsafe { + C::draw_indexed( + self, + first_index, + index_count, + base_vertex, + first_instance, + instance_count, + ) + }; + } + + unsafe fn draw_mesh_tasks( + &mut self, + group_count_x: u32, + group_count_y: u32, + group_count_z: u32, + ) { + unsafe { C::draw_mesh_tasks(self, group_count_x, group_count_y, group_count_z) }; + } + + unsafe fn draw_indirect( + &mut self, + buffer: &dyn DynBuffer, + offset: wgt::BufferAddress, + draw_count: u32, + ) { + let buffer = buffer.expect_downcast_ref(); + unsafe { C::draw_indirect(self, buffer, offset, draw_count) }; + } + + unsafe fn draw_indexed_indirect( + &mut self, + buffer: &dyn DynBuffer, + offset: wgt::BufferAddress, + draw_count: u32, + ) { + let buffer = buffer.expect_downcast_ref(); + unsafe { C::draw_indexed_indirect(self, buffer, offset, draw_count) }; + } + + unsafe fn draw_mesh_tasks_indirect( + &mut self, + buffer: &dyn DynBuffer, + offset: wgt::BufferAddress, + draw_count: u32, + ) { + let buffer = buffer.expect_downcast_ref(); + unsafe { C::draw_mesh_tasks_indirect(self, buffer, offset, draw_count) }; + } + + unsafe fn draw_indirect_count( + &mut self, + buffer: &dyn DynBuffer, + offset: wgt::BufferAddress, + count_buffer: &dyn DynBuffer, + count_offset: wgt::BufferAddress, + max_count: u32, + ) { + let buffer = buffer.expect_downcast_ref(); + let count_buffer = count_buffer.expect_downcast_ref(); + unsafe { + C::draw_indirect_count(self, buffer, offset, count_buffer, count_offset, max_count) + }; + } + + unsafe fn draw_indexed_indirect_count( + &mut self, + buffer: &dyn DynBuffer, + offset: wgt::BufferAddress, + count_buffer: &dyn DynBuffer, + count_offset: wgt::BufferAddress, + max_count: u32, + ) { + let buffer = buffer.expect_downcast_ref(); + let count_buffer = count_buffer.expect_downcast_ref(); + unsafe { + C::draw_indexed_indirect_count( + self, + buffer, + offset, + count_buffer, + count_offset, + max_count, + ) + }; + } + + unsafe fn draw_mesh_tasks_indirect_count( + &mut self, + buffer: &dyn DynBuffer, + offset: wgt::BufferAddress, + count_buffer: &dyn DynBuffer, + count_offset: wgt::BufferAddress, + max_count: u32, + ) { + let buffer = buffer.expect_downcast_ref(); + let count_buffer = count_buffer.expect_downcast_ref(); + unsafe { + C::draw_mesh_tasks_indirect_count( + self, + buffer, + offset, + count_buffer, + count_offset, + max_count, + ) + }; + } + + unsafe fn begin_compute_pass(&mut self, desc: &ComputePassDescriptor<dyn DynQuerySet>) { + let desc = ComputePassDescriptor { + label: desc.label, + timestamp_writes: desc + .timestamp_writes + .as_ref() + .map(|writes| writes.expect_downcast()), + }; + unsafe { C::begin_compute_pass(self, &desc) }; + } + + unsafe fn end_compute_pass(&mut self) { + unsafe { C::end_compute_pass(self) }; + } + + unsafe fn set_compute_pipeline(&mut self, pipeline: &dyn DynComputePipeline) { + let pipeline = pipeline.expect_downcast_ref(); + unsafe { C::set_compute_pipeline(self, pipeline) }; + } + + unsafe fn dispatch_workgroups(&mut self, count: [u32; 3]) { + unsafe { C::dispatch_workgroups(self, count) }; + } + + unsafe fn dispatch_workgroups_indirect( + &mut self, + buffer: &dyn DynBuffer, + offset: wgt::BufferAddress, + ) { + let buffer = buffer.expect_downcast_ref(); + unsafe { C::dispatch_workgroups_indirect(self, buffer, offset) }; + } + + unsafe fn set_render_pipeline(&mut self, pipeline: &dyn DynRenderPipeline) { + let pipeline = pipeline.expect_downcast_ref(); + unsafe { C::set_render_pipeline(self, pipeline) }; + } + + unsafe fn set_index_buffer<'a>( + &mut self, + binding: BufferBinding<'a, dyn DynBuffer>, + format: wgt::IndexFormat, + ) { + let binding = binding.expect_downcast(); + unsafe { self.set_index_buffer(binding, format) }; + } + + unsafe fn set_vertex_buffer<'a>( + &mut self, + index: u32, + binding: BufferBinding<'a, dyn DynBuffer>, + ) { + let binding = binding.expect_downcast(); + unsafe { self.set_vertex_buffer(index, binding) }; + } + + unsafe fn begin_ray_tracing_pass(&mut self, desc: &RayTracingPassDescriptor) { + let desc = RayTracingPassDescriptor { label: desc.label }; + unsafe { C::begin_ray_tracing_pass(self, &desc) }; + } + + unsafe fn end_ray_tracing_pass(&mut self) { + unsafe { C::end_ray_tracing_pass(self) }; + } + + unsafe fn set_ray_tracing_pipeline(&mut self, pipeline: &dyn DynRayTracingPipeline) { + let pipeline = pipeline.expect_downcast_ref(); + unsafe { C::set_ray_tracing_pipeline(self, pipeline) }; + } + + unsafe fn trace_rays<'a>( + &mut self, + count: [u32; 3], + ray_generation_group_data: crate::PipelineGroupData<'a, dyn DynBuffer>, + miss_group_data: crate::PipelineGroupData<'a, dyn DynBuffer>, + intersection_group_data: crate::PipelineGroupData<'a, dyn DynBuffer>, + ) { + let downcast_group_data = + |data: crate::PipelineGroupData<'a, dyn DynBuffer>| crate::PipelineGroupData { + buffer: data.buffer.expect_downcast_ref(), + offset: data.offset, + stride: data.stride, + count: data.count, + }; + + unsafe { + C::trace_rays( + self, + count, + downcast_group_data(ray_generation_group_data), + downcast_group_data(miss_group_data), + downcast_group_data(intersection_group_data), + ); + } + } + + unsafe fn build_acceleration_structures<'a>( + &mut self, + descriptors: &'a [BuildAccelerationStructureDescriptor< + 'a, + dyn DynBuffer, + dyn DynAccelerationStructure, + >], + ) { + // Need to collect entries here so we can reference them in the descriptor. + // TODO: API should be redesigned to avoid this and other descriptor copies that happen due to the dyn api. + let descriptor_entries = descriptors + .iter() + .map(|d| d.entries.expect_downcast()) + .collect::<Vec<_>>(); + let descriptors = descriptors + .iter() + .zip(descriptor_entries.iter()) + .map(|(d, entries)| BuildAccelerationStructureDescriptor::< + <C::A as Api>::Buffer, + <C::A as Api>::AccelerationStructure, + > { + entries, + mode: d.mode, + flags: d.flags, + source_acceleration_structure: d + .source_acceleration_structure + .map(|a| a.expect_downcast_ref()), + destination_acceleration_structure: d + .destination_acceleration_structure + .expect_downcast_ref(), + scratch_buffer: d.scratch_buffer.expect_downcast_ref(), + scratch_buffer_offset: d.scratch_buffer_offset, + }); + unsafe { C::build_acceleration_structures(self, descriptors.len() as _, descriptors) }; + } + + unsafe fn place_acceleration_structure_barrier( + &mut self, + barrier: AccelerationStructureBarrier, + ) { + unsafe { C::place_acceleration_structure_barrier(self, barrier) }; + } + + unsafe fn copy_acceleration_structure_to_acceleration_structure( + &mut self, + src: &dyn DynAccelerationStructure, + dst: &dyn DynAccelerationStructure, + copy: wgt::AccelerationStructureCopy, + ) { + let src = src.expect_downcast_ref(); + let dst = dst.expect_downcast_ref(); + unsafe { C::copy_acceleration_structure_to_acceleration_structure(self, src, dst, copy) }; + } + unsafe fn read_acceleration_structure_compact_size( + &mut self, + acceleration_structure: &dyn DynAccelerationStructure, + buf: &dyn DynBuffer, + ) { + let acceleration_structure = acceleration_structure.expect_downcast_ref(); + let buf = buf.expect_downcast_ref(); + unsafe { C::read_acceleration_structure_compact_size(self, acceleration_structure, buf) } + } + + unsafe fn set_acceleration_structure_dependencies( + &self, + command_buffers: &[Box<dyn DynCommandBuffer>], + dependencies: &[&dyn DynAccelerationStructure], + ) { + let command_buffers: Vec<&<C::A as Api>::CommandBuffer> = command_buffers + .iter() + .map(|command_buffer| command_buffer.expect_downcast_ref()) + .collect(); + let dependencies: Vec<&<C::A as Api>::AccelerationStructure> = dependencies + .iter() + .map(|dependency| dependency.expect_downcast_ref()) + .collect(); + unsafe { C::set_acceleration_structure_dependencies(&command_buffers, &dependencies) } + } +} + +impl<'a> PassTimestampWrites<'a, dyn DynQuerySet> { + pub fn expect_downcast<B: DynQuerySet>(&self) -> PassTimestampWrites<'a, B> { + PassTimestampWrites { + query_set: self.query_set.expect_downcast_ref(), + beginning_of_pass_write_index: self.beginning_of_pass_write_index, + end_of_pass_write_index: self.end_of_pass_write_index, + } + } +} + +impl<'a> Attachment<'a, dyn DynTextureView> { + pub fn expect_downcast<B: DynTextureView>(&self) -> Attachment<'a, B> { + Attachment { + view: self.view.expect_downcast_ref(), + usage: self.usage, + } + } +} + +impl<'a> ColorAttachment<'a, dyn DynTextureView> { + pub fn expect_downcast<B: DynTextureView>(&self) -> ColorAttachment<'a, B> { + ColorAttachment { + target: self.target.expect_downcast(), + depth_slice: self.depth_slice, + resolve_target: self.resolve_target.as_ref().map(|rt| rt.expect_downcast()), + ops: self.ops, + clear_value: self.clear_value, + } + } +} + +impl<'a> DepthStencilAttachment<'a, dyn DynTextureView> { + pub fn expect_downcast<B: DynTextureView>(&self) -> DepthStencilAttachment<'a, B> { + DepthStencilAttachment { + target: self.target.expect_downcast(), + depth_ops: self.depth_ops, + stencil_ops: self.stencil_ops, + clear_value: self.clear_value, + } + } +} diff --git a/third_party/wgpu-hal/src/dynamic/device.rs b/third_party/wgpu-hal/src/dynamic/device.rs new file mode 100644 index 000000000..f9b9fe682 --- /dev/null +++ b/third_party/wgpu-hal/src/dynamic/device.rs @@ -0,0 +1,622 @@ +use alloc::{borrow::ToOwned as _, boxed::Box, vec::Vec}; + +use crate::{ + AccelerationStructureBuildSizes, AccelerationStructureDescriptor, Api, BindGroupDescriptor, + BindGroupLayoutDescriptor, BufferDescriptor, BufferMapping, CommandEncoderDescriptor, + ComputePipelineDescriptor, Device, DeviceError, FenceValue, + GetAccelerationStructureBuildSizesDescriptor, Label, MemoryRange, PipelineCacheDescriptor, + PipelineCacheError, PipelineError, PipelineLayoutDescriptor, RayObjectIntersectionState, + RayTracingPipelineDescriptor, RenderPipelineDescriptor, SamplerDescriptor, ShaderError, + ShaderInput, ShaderModuleDescriptor, TextureDescriptor, TextureViewDescriptor, TlasInstance, +}; + +use super::{ + DynAccelerationStructure, DynBindGroup, DynBindGroupLayout, DynBuffer, DynCommandEncoder, + DynComputePipeline, DynFence, DynPipelineCache, DynPipelineLayout, DynQuerySet, DynQueue, + DynRayTracingPipeline, DynRenderPipeline, DynResource, DynResourceExt as _, DynSampler, + DynShaderModule, DynTexture, DynTextureView, +}; + +pub trait DynDevice: DynResource { + unsafe fn create_buffer( + &self, + desc: &BufferDescriptor, + ) -> Result<Box<dyn DynBuffer>, DeviceError>; + + unsafe fn destroy_buffer(&self, buffer: Box<dyn DynBuffer>); + unsafe fn add_raw_buffer(&self, buffer: &dyn DynBuffer); + + unsafe fn map_buffer( + &self, + buffer: &dyn DynBuffer, + range: MemoryRange, + ) -> Result<BufferMapping, DeviceError>; + + unsafe fn unmap_buffer(&self, buffer: &dyn DynBuffer); + + unsafe fn flush_mapped_ranges(&self, buffer: &dyn DynBuffer, ranges: &[MemoryRange]); + unsafe fn invalidate_mapped_ranges(&self, buffer: &dyn DynBuffer, ranges: &[MemoryRange]); + + unsafe fn create_texture( + &self, + desc: &TextureDescriptor, + ) -> Result<Box<dyn DynTexture>, DeviceError>; + unsafe fn destroy_texture(&self, texture: Box<dyn DynTexture>); + unsafe fn add_raw_texture(&self, texture: &dyn DynTexture); + + unsafe fn create_texture_view( + &self, + texture: &dyn DynTexture, + desc: &TextureViewDescriptor, + ) -> Result<Box<dyn DynTextureView>, DeviceError>; + unsafe fn destroy_texture_view(&self, view: Box<dyn DynTextureView>); + unsafe fn create_sampler( + &self, + desc: &SamplerDescriptor, + ) -> Result<Box<dyn DynSampler>, DeviceError>; + unsafe fn destroy_sampler(&self, sampler: Box<dyn DynSampler>); + + unsafe fn create_command_encoder( + &self, + desc: &CommandEncoderDescriptor<dyn DynQueue>, + ) -> Result<Box<dyn DynCommandEncoder>, DeviceError>; + + unsafe fn create_bind_group_layout( + &self, + desc: &BindGroupLayoutDescriptor, + ) -> Result<Box<dyn DynBindGroupLayout>, DeviceError>; + unsafe fn destroy_bind_group_layout(&self, bg_layout: Box<dyn DynBindGroupLayout>); + + unsafe fn create_pipeline_layout( + &self, + desc: &PipelineLayoutDescriptor<dyn DynBindGroupLayout>, + ) -> Result<Box<dyn DynPipelineLayout>, DeviceError>; + unsafe fn destroy_pipeline_layout(&self, pipeline_layout: Box<dyn DynPipelineLayout>); + + unsafe fn create_bind_group( + &self, + desc: &BindGroupDescriptor< + dyn DynBindGroupLayout, + dyn DynBuffer, + dyn DynSampler, + dyn DynTextureView, + dyn DynAccelerationStructure, + >, + ) -> Result<Box<dyn DynBindGroup>, DeviceError>; + unsafe fn destroy_bind_group(&self, group: Box<dyn DynBindGroup>); + + unsafe fn create_shader_module( + &self, + desc: &ShaderModuleDescriptor, + shader: ShaderInput, + ) -> Result<Box<dyn DynShaderModule>, ShaderError>; + unsafe fn destroy_shader_module(&self, module: Box<dyn DynShaderModule>); + + unsafe fn create_render_pipeline( + &self, + desc: &RenderPipelineDescriptor< + dyn DynPipelineLayout, + dyn DynShaderModule, + dyn DynPipelineCache, + >, + ) -> Result<Box<dyn DynRenderPipeline>, PipelineError>; + unsafe fn destroy_render_pipeline(&self, pipeline: Box<dyn DynRenderPipeline>); + + unsafe fn create_compute_pipeline( + &self, + desc: &ComputePipelineDescriptor< + dyn DynPipelineLayout, + dyn DynShaderModule, + dyn DynPipelineCache, + >, + ) -> Result<Box<dyn DynComputePipeline>, PipelineError>; + unsafe fn destroy_compute_pipeline(&self, pipeline: Box<dyn DynComputePipeline>); + + unsafe fn create_ray_tracing_pipeline( + &self, + desc: &RayTracingPipelineDescriptor< + dyn DynPipelineLayout, + dyn DynShaderModule, + dyn DynPipelineCache, + >, + ) -> Result<Box<dyn DynRayTracingPipeline>, PipelineError>; + unsafe fn destroy_ray_tracing_pipeline(&self, pipeline: Box<dyn DynRayTracingPipeline>); + unsafe fn get_raytracing_pipeline_group_data( + &self, + pipeline: &dyn DynRayTracingPipeline, + groups: core::ops::Range<u32>, + ) -> Result<Vec<u8>, DeviceError>; + + unsafe fn create_pipeline_cache( + &self, + desc: &PipelineCacheDescriptor<'_>, + ) -> Result<Box<dyn DynPipelineCache>, PipelineCacheError>; + fn pipeline_cache_validation_key(&self) -> Option<[u8; 16]> { + None + } + unsafe fn destroy_pipeline_cache(&self, cache: Box<dyn DynPipelineCache>); + + unsafe fn create_query_set( + &self, + desc: &wgt::QuerySetDescriptor<Label>, + ) -> Result<Box<dyn DynQuerySet>, DeviceError>; + unsafe fn destroy_query_set(&self, set: Box<dyn DynQuerySet>); + + unsafe fn create_fence(&self) -> Result<Box<dyn DynFence>, DeviceError>; + unsafe fn destroy_fence(&self, fence: Box<dyn DynFence>); + unsafe fn get_fence_value(&self, fence: &dyn DynFence) -> Result<FenceValue, DeviceError>; + + unsafe fn wait( + &self, + fence: &dyn DynFence, + value: FenceValue, + timeout: Option<core::time::Duration>, + ) -> Result<bool, DeviceError>; + + unsafe fn start_graphics_debugger_capture(&self) -> bool; + unsafe fn stop_graphics_debugger_capture(&self); + + unsafe fn pipeline_cache_get_data(&self, cache: &dyn DynPipelineCache) -> Option<Vec<u8>>; + + unsafe fn create_acceleration_structure( + &self, + desc: &AccelerationStructureDescriptor, + ) -> Result<Box<dyn DynAccelerationStructure>, DeviceError>; + unsafe fn get_acceleration_structure_build_sizes( + &self, + desc: &GetAccelerationStructureBuildSizesDescriptor<dyn DynBuffer>, + ) -> AccelerationStructureBuildSizes; + unsafe fn get_acceleration_structure_device_address( + &self, + acceleration_structure: &dyn DynAccelerationStructure, + ) -> wgt::BufferAddress; + unsafe fn destroy_acceleration_structure( + &self, + acceleration_structure: Box<dyn DynAccelerationStructure>, + ); + fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec<u8>; + + fn get_internal_counters(&self) -> wgt::HalCounters; + fn generate_allocator_report(&self) -> Option<wgt::AllocatorReport>; + + fn check_if_oom(&self) -> Result<(), DeviceError>; +} + +impl<D: Device + DynResource> DynDevice for D { + unsafe fn create_buffer( + &self, + desc: &BufferDescriptor, + ) -> Result<Box<dyn DynBuffer>, DeviceError> { + unsafe { D::create_buffer(self, desc) }.map(|b| -> Box<dyn DynBuffer> { Box::new(b) }) + } + + unsafe fn destroy_buffer(&self, buffer: Box<dyn DynBuffer>) { + unsafe { D::destroy_buffer(self, buffer.unbox()) }; + } + unsafe fn add_raw_buffer(&self, buffer: &dyn DynBuffer) { + let buffer = buffer.expect_downcast_ref(); + unsafe { D::add_raw_buffer(self, buffer) }; + } + + unsafe fn map_buffer( + &self, + buffer: &dyn DynBuffer, + range: MemoryRange, + ) -> Result<BufferMapping, DeviceError> { + let buffer = buffer.expect_downcast_ref(); + unsafe { D::map_buffer(self, buffer, range) } + } + + unsafe fn unmap_buffer(&self, buffer: &dyn DynBuffer) { + let buffer = buffer.expect_downcast_ref(); + unsafe { D::unmap_buffer(self, buffer) } + } + + unsafe fn flush_mapped_ranges(&self, buffer: &dyn DynBuffer, ranges: &[MemoryRange]) { + let buffer = buffer.expect_downcast_ref(); + unsafe { D::flush_mapped_ranges(self, buffer, ranges.iter().cloned()) } + } + + unsafe fn invalidate_mapped_ranges(&self, buffer: &dyn DynBuffer, ranges: &[MemoryRange]) { + let buffer = buffer.expect_downcast_ref(); + unsafe { D::invalidate_mapped_ranges(self, buffer, ranges.iter().cloned()) } + } + + unsafe fn create_texture( + &self, + desc: &TextureDescriptor, + ) -> Result<Box<dyn DynTexture>, DeviceError> { + unsafe { D::create_texture(self, desc) }.map(|b| { + let boxed_texture: Box<<D::A as Api>::Texture> = Box::new(b); + let boxed_texture: Box<dyn DynTexture> = boxed_texture; + boxed_texture + }) + } + + unsafe fn destroy_texture(&self, texture: Box<dyn DynTexture>) { + unsafe { D::destroy_texture(self, texture.unbox()) }; + } + + unsafe fn add_raw_texture(&self, texture: &dyn DynTexture) { + let texture = texture.expect_downcast_ref(); + unsafe { D::add_raw_texture(self, texture) }; + } + + unsafe fn create_texture_view( + &self, + texture: &dyn DynTexture, + desc: &TextureViewDescriptor, + ) -> Result<Box<dyn DynTextureView>, DeviceError> { + let texture = texture.expect_downcast_ref(); + unsafe { D::create_texture_view(self, texture, desc) }.map(|b| { + let boxed_texture_view: Box<<D::A as Api>::TextureView> = Box::new(b); + let boxed_texture_view: Box<dyn DynTextureView> = boxed_texture_view; + boxed_texture_view + }) + } + + unsafe fn destroy_texture_view(&self, view: Box<dyn DynTextureView>) { + unsafe { D::destroy_texture_view(self, view.unbox()) }; + } + + unsafe fn create_sampler( + &self, + desc: &SamplerDescriptor, + ) -> Result<Box<dyn DynSampler>, DeviceError> { + unsafe { D::create_sampler(self, desc) }.map(|b| { + let boxed_sampler: Box<<D::A as Api>::Sampler> = Box::new(b); + let boxed_sampler: Box<dyn DynSampler> = boxed_sampler; + boxed_sampler + }) + } + + unsafe fn destroy_sampler(&self, sampler: Box<dyn DynSampler>) { + unsafe { D::destroy_sampler(self, sampler.unbox()) }; + } + + unsafe fn create_command_encoder( + &self, + desc: &CommandEncoderDescriptor<'_, dyn DynQueue>, + ) -> Result<Box<dyn DynCommandEncoder>, DeviceError> { + let desc = CommandEncoderDescriptor { + label: desc.label, + queue: desc.queue.expect_downcast_ref(), + }; + unsafe { D::create_command_encoder(self, &desc) } + .map(|b| -> Box<dyn DynCommandEncoder> { Box::new(b) }) + } + + unsafe fn create_bind_group_layout( + &self, + desc: &BindGroupLayoutDescriptor, + ) -> Result<Box<dyn DynBindGroupLayout>, DeviceError> { + unsafe { D::create_bind_group_layout(self, desc) } + .map(|b| -> Box<dyn DynBindGroupLayout> { Box::new(b) }) + } + + unsafe fn destroy_bind_group_layout(&self, bg_layout: Box<dyn DynBindGroupLayout>) { + unsafe { D::destroy_bind_group_layout(self, bg_layout.unbox()) }; + } + + unsafe fn create_pipeline_layout( + &self, + desc: &PipelineLayoutDescriptor<dyn DynBindGroupLayout>, + ) -> Result<Box<dyn DynPipelineLayout>, DeviceError> { + let bind_group_layouts: Vec<_> = desc + .bind_group_layouts + .iter() + .map(|bgl| bgl.map(|bgl| bgl.expect_downcast_ref())) + .collect(); + let desc = PipelineLayoutDescriptor { + label: desc.label, + bind_group_layouts: &bind_group_layouts, + immediate_size: desc.immediate_size, + flags: desc.flags, + }; + + unsafe { D::create_pipeline_layout(self, &desc) } + .map(|b| -> Box<dyn DynPipelineLayout> { Box::new(b) }) + } + + unsafe fn destroy_pipeline_layout(&self, pipeline_layout: Box<dyn DynPipelineLayout>) { + unsafe { D::destroy_pipeline_layout(self, pipeline_layout.unbox()) }; + } + + unsafe fn create_bind_group( + &self, + desc: &BindGroupDescriptor< + dyn DynBindGroupLayout, + dyn DynBuffer, + dyn DynSampler, + dyn DynTextureView, + dyn DynAccelerationStructure, + >, + ) -> Result<Box<dyn DynBindGroup>, DeviceError> { + let buffers: Vec<_> = desc + .buffers + .iter() + .map(|b| b.clone().expect_downcast()) + .collect(); + let samplers: Vec<_> = desc + .samplers + .iter() + .map(|s| s.expect_downcast_ref()) + .collect(); + let textures: Vec<_> = desc + .textures + .iter() + .map(|t| t.clone().expect_downcast()) + .collect(); + let acceleration_structures: Vec<_> = desc + .acceleration_structures + .iter() + .map(|a| a.expect_downcast_ref()) + .collect(); + let external_textures: Vec<_> = desc + .external_textures + .iter() + .map(|et| et.clone().expect_downcast()) + .collect(); + + let desc = BindGroupDescriptor { + label: desc.label.to_owned(), + layout: desc.layout.expect_downcast_ref(), + buffers: &buffers, + samplers: &samplers, + textures: &textures, + entries: desc.entries, + acceleration_structures: &acceleration_structures, + external_textures: &external_textures, + }; + + unsafe { D::create_bind_group(self, &desc) } + .map(|b| -> Box<dyn DynBindGroup> { Box::new(b) }) + } + + unsafe fn destroy_bind_group(&self, group: Box<dyn DynBindGroup>) { + unsafe { D::destroy_bind_group(self, group.unbox()) }; + } + + unsafe fn create_shader_module( + &self, + desc: &ShaderModuleDescriptor, + shader: ShaderInput, + ) -> Result<Box<dyn DynShaderModule>, ShaderError> { + unsafe { D::create_shader_module(self, desc, shader) } + .map(|b| -> Box<dyn DynShaderModule> { Box::new(b) }) + } + + unsafe fn destroy_shader_module(&self, module: Box<dyn DynShaderModule>) { + unsafe { D::destroy_shader_module(self, module.unbox()) }; + } + + unsafe fn create_render_pipeline( + &self, + desc: &RenderPipelineDescriptor< + dyn DynPipelineLayout, + dyn DynShaderModule, + dyn DynPipelineCache, + >, + ) -> Result<Box<dyn DynRenderPipeline>, PipelineError> { + let desc = RenderPipelineDescriptor { + label: desc.label, + layout: desc.layout.expect_downcast_ref(), + vertex_processor: match &desc.vertex_processor { + crate::VertexProcessor::Standard { + vertex_buffers, + vertex_stage, + } => crate::VertexProcessor::Standard { + vertex_buffers, + vertex_stage: vertex_stage.clone().expect_downcast(), + }, + crate::VertexProcessor::Mesh { + task_stage: task, + mesh_stage: mesh, + } => crate::VertexProcessor::Mesh { + task_stage: task.as_ref().map(|a| a.clone().expect_downcast()), + mesh_stage: mesh.clone().expect_downcast(), + }, + }, + primitive: desc.primitive, + depth_stencil: desc.depth_stencil.clone(), + multisample: desc.multisample, + fragment_stage: desc.fragment_stage.clone().map(|f| f.expect_downcast()), + color_targets: desc.color_targets, + multiview_mask: desc.multiview_mask, + cache: desc.cache.map(|c| c.expect_downcast_ref()), + }; + + unsafe { D::create_render_pipeline(self, &desc) } + .map(|b| -> Box<dyn DynRenderPipeline> { Box::new(b) }) + } + + unsafe fn destroy_render_pipeline(&self, pipeline: Box<dyn DynRenderPipeline>) { + unsafe { D::destroy_render_pipeline(self, pipeline.unbox()) }; + } + + unsafe fn create_compute_pipeline( + &self, + desc: &ComputePipelineDescriptor< + dyn DynPipelineLayout, + dyn DynShaderModule, + dyn DynPipelineCache, + >, + ) -> Result<Box<dyn DynComputePipeline>, PipelineError> { + let desc = ComputePipelineDescriptor { + label: desc.label, + layout: desc.layout.expect_downcast_ref(), + stage: desc.stage.clone().expect_downcast(), + cache: desc.cache.as_ref().map(|c| c.expect_downcast_ref()), + }; + + unsafe { D::create_compute_pipeline(self, &desc) } + .map(|b| -> Box<dyn DynComputePipeline> { Box::new(b) }) + } + + unsafe fn destroy_compute_pipeline(&self, pipeline: Box<dyn DynComputePipeline>) { + unsafe { D::destroy_compute_pipeline(self, pipeline.unbox()) }; + } + + unsafe fn create_ray_tracing_pipeline( + &self, + desc: &RayTracingPipelineDescriptor< + dyn DynPipelineLayout, + dyn DynShaderModule, + dyn DynPipelineCache, + >, + ) -> Result<Box<dyn DynRayTracingPipeline>, PipelineError> { + let ray_intersection: Vec<_> = desc + .intersection + .iter() + .map(|stage| RayObjectIntersectionState { + closest_hit: stage.closest_hit.clone().expect_downcast(), + any_hit: stage + .any_hit + .as_ref() + .map(|stage| stage.clone().expect_downcast()), + }) + .collect(); + + let desc = RayTracingPipelineDescriptor { + label: desc.label, + layout: desc.layout.expect_downcast_ref(), + ray_generation: desc.ray_generation.clone().expect_downcast(), + miss: desc.miss.clone().expect_downcast(), + intersection: &ray_intersection, + max_recursion_depth: desc.max_recursion_depth, + cache: desc.cache.as_ref().map(|c| c.expect_downcast_ref()), + }; + + unsafe { D::create_ray_tracing_pipeline(self, &desc) } + .map(|b| -> Box<dyn DynRayTracingPipeline> { Box::new(b) }) + } + + unsafe fn destroy_ray_tracing_pipeline(&self, pipeline: Box<dyn DynRayTracingPipeline>) { + unsafe { + D::destroy_ray_tracing_pipeline(self, pipeline.unbox()); + }; + } + unsafe fn get_raytracing_pipeline_group_data( + &self, + pipeline: &dyn DynRayTracingPipeline, + groups: core::ops::Range<u32>, + ) -> Result<Vec<u8>, DeviceError> { + unsafe { + D::get_raytracing_pipeline_group_data(self, pipeline.expect_downcast_ref(), groups) + } + } + + unsafe fn create_pipeline_cache( + &self, + desc: &PipelineCacheDescriptor<'_>, + ) -> Result<Box<dyn DynPipelineCache>, PipelineCacheError> { + unsafe { D::create_pipeline_cache(self, desc) } + .map(|b| -> Box<dyn DynPipelineCache> { Box::new(b) }) + } + + fn pipeline_cache_validation_key(&self) -> Option<[u8; 16]> { + D::pipeline_cache_validation_key(self) + } + + unsafe fn destroy_pipeline_cache(&self, pipeline_cache: Box<dyn DynPipelineCache>) { + unsafe { D::destroy_pipeline_cache(self, pipeline_cache.unbox()) }; + } + + unsafe fn create_query_set( + &self, + desc: &wgt::QuerySetDescriptor<Label>, + ) -> Result<Box<dyn DynQuerySet>, DeviceError> { + unsafe { D::create_query_set(self, desc) }.map(|b| -> Box<dyn DynQuerySet> { Box::new(b) }) + } + + unsafe fn destroy_query_set(&self, query_set: Box<dyn DynQuerySet>) { + unsafe { D::destroy_query_set(self, query_set.unbox()) }; + } + + unsafe fn create_fence(&self) -> Result<Box<dyn DynFence>, DeviceError> { + unsafe { D::create_fence(self) }.map(|b| -> Box<dyn DynFence> { Box::new(b) }) + } + + unsafe fn destroy_fence(&self, fence: Box<dyn DynFence>) { + unsafe { D::destroy_fence(self, fence.unbox()) }; + } + + unsafe fn get_fence_value(&self, fence: &dyn DynFence) -> Result<FenceValue, DeviceError> { + let fence = fence.expect_downcast_ref(); + unsafe { D::get_fence_value(self, fence) } + } + + unsafe fn wait( + &self, + fence: &dyn DynFence, + value: FenceValue, + timeout: Option<core::time::Duration>, + ) -> Result<bool, DeviceError> { + let fence = fence.expect_downcast_ref(); + unsafe { D::wait(self, fence, value, timeout) } + } + + unsafe fn start_graphics_debugger_capture(&self) -> bool { + unsafe { D::start_graphics_debugger_capture(self) } + } + + unsafe fn stop_graphics_debugger_capture(&self) { + unsafe { D::stop_graphics_debugger_capture(self) } + } + + unsafe fn pipeline_cache_get_data(&self, cache: &dyn DynPipelineCache) -> Option<Vec<u8>> { + let cache = cache.expect_downcast_ref(); + unsafe { D::pipeline_cache_get_data(self, cache) } + } + + unsafe fn create_acceleration_structure( + &self, + desc: &AccelerationStructureDescriptor, + ) -> Result<Box<dyn DynAccelerationStructure>, DeviceError> { + unsafe { D::create_acceleration_structure(self, desc) } + .map(|b| -> Box<dyn DynAccelerationStructure> { Box::new(b) }) + } + + unsafe fn get_acceleration_structure_build_sizes( + &self, + desc: &GetAccelerationStructureBuildSizesDescriptor<dyn DynBuffer>, + ) -> AccelerationStructureBuildSizes { + let entries = desc.entries.expect_downcast(); + let desc = GetAccelerationStructureBuildSizesDescriptor { + entries: &entries, + flags: desc.flags, + }; + unsafe { D::get_acceleration_structure_build_sizes(self, &desc) } + } + + unsafe fn get_acceleration_structure_device_address( + &self, + acceleration_structure: &dyn DynAccelerationStructure, + ) -> wgt::BufferAddress { + let acceleration_structure = acceleration_structure.expect_downcast_ref(); + unsafe { D::get_acceleration_structure_device_address(self, acceleration_structure) } + } + + unsafe fn destroy_acceleration_structure( + &self, + acceleration_structure: Box<dyn DynAccelerationStructure>, + ) { + unsafe { D::destroy_acceleration_structure(self, acceleration_structure.unbox()) } + } + + fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec<u8> { + D::tlas_instance_to_bytes(self, instance) + } + + fn get_internal_counters(&self) -> wgt::HalCounters { + D::get_internal_counters(self) + } + + fn generate_allocator_report(&self) -> Option<wgt::AllocatorReport> { + D::generate_allocator_report(self) + } + + fn check_if_oom(&self) -> Result<(), DeviceError> { + D::check_if_oom(self) + } +} diff --git a/third_party/wgpu-hal/src/dynamic/instance.rs b/third_party/wgpu-hal/src/dynamic/instance.rs new file mode 100644 index 000000000..cab14cdf1 --- /dev/null +++ b/third_party/wgpu-hal/src/dynamic/instance.rs @@ -0,0 +1,71 @@ +use alloc::{boxed::Box, vec::Vec}; + +use crate::{Api, Capabilities, ExposedAdapter, Instance, InstanceError}; + +use super::{DynAdapter, DynResource, DynResourceExt as _, DynSurface}; + +#[expect(missing_debug_implementations, reason = "dyn")] +pub struct DynExposedAdapter { + pub adapter: Box<dyn DynAdapter>, + pub info: wgt::AdapterInfo, + pub features: wgt::Features, + pub capabilities: Capabilities, +} + +impl DynExposedAdapter { + /// Returns the backend this adapter is using. + pub fn backend(&self) -> wgt::Backend { + self.info.backend + } +} + +impl<A: Api> From<ExposedAdapter<A>> for DynExposedAdapter { + fn from(exposed_adapter: ExposedAdapter<A>) -> Self { + Self { + adapter: Box::new(exposed_adapter.adapter), + info: exposed_adapter.info, + features: exposed_adapter.features, + capabilities: exposed_adapter.capabilities, + } + } +} + +pub trait DynInstance: DynResource { + unsafe fn create_surface( + &self, + display_handle: raw_window_handle::RawDisplayHandle, + window_handle: raw_window_handle::RawWindowHandle, + ) -> Result<Box<dyn DynSurface>, InstanceError>; + + unsafe fn enumerate_adapters( + &self, + surface_hint: Option<&dyn DynSurface>, + ) -> Vec<DynExposedAdapter>; +} + +impl<I: Instance + DynResource> DynInstance for I { + unsafe fn create_surface( + &self, + display_handle: raw_window_handle::RawDisplayHandle, + window_handle: raw_window_handle::RawWindowHandle, + ) -> Result<Box<dyn DynSurface>, InstanceError> { + unsafe { I::create_surface(self, display_handle, window_handle) } + .map(|surface| -> Box<dyn DynSurface> { Box::new(surface) }) + } + + unsafe fn enumerate_adapters( + &self, + surface_hint: Option<&dyn DynSurface>, + ) -> Vec<DynExposedAdapter> { + let surface_hint = surface_hint.map(|s| s.expect_downcast_ref()); + unsafe { I::enumerate_adapters(self, surface_hint) } + .into_iter() + .map(|exposed| DynExposedAdapter { + adapter: Box::new(exposed.adapter), + info: exposed.info, + features: exposed.features, + capabilities: exposed.capabilities, + }) + .collect() + } +} diff --git a/third_party/wgpu-hal/src/dynamic/mod.rs b/third_party/wgpu-hal/src/dynamic/mod.rs new file mode 100644 index 000000000..cce4d3b66 --- /dev/null +++ b/third_party/wgpu-hal/src/dynamic/mod.rs @@ -0,0 +1,212 @@ +mod adapter; +mod command; +mod device; +mod instance; +mod queue; +mod surface; + +pub use adapter::{DynAdapter, DynOpenDevice}; +pub use command::DynCommandEncoder; +pub use device::DynDevice; +pub use instance::{DynExposedAdapter, DynInstance}; +pub use queue::DynQueue; +pub use surface::{DynAcquiredSurfaceTexture, DynSurface}; + +use alloc::boxed::Box; +use core::{ + any::{Any, TypeId}, + fmt, +}; + +use wgt::WasmNotSendSync; + +use crate::{ + AccelerationStructureAABBs, AccelerationStructureEntries, AccelerationStructureInstances, + AccelerationStructureTriangleIndices, AccelerationStructureTriangleTransform, + AccelerationStructureTriangles, BufferBinding, ExternalTextureBinding, ProgrammableStage, + TextureBinding, +}; + +/// Base trait for all resources, allows downcasting via [`Any`]. +pub trait DynResource: Any + WasmNotSendSync + 'static { + fn as_any(&self) -> &dyn Any; + fn as_any_mut(&mut self) -> &mut dyn Any; +} + +/// Utility macro for implementing `DynResource` for a list of types. +macro_rules! impl_dyn_resource { + ($($type:ty),*) => { + $( + impl crate::DynResource for $type { + fn as_any(&self) -> &dyn ::core::any::Any { + self + } + + fn as_any_mut(&mut self) -> &mut dyn ::core::any::Any { + self + } + } + )* + }; +} +pub(crate) use impl_dyn_resource; + +/// Extension trait for `DynResource` used by implementations of various dynamic resource traits. +trait DynResourceExt { + /// # Panics + /// + /// - Panics if `self` is not downcastable to `T`. + fn expect_downcast_ref<T: DynResource>(&self) -> &T; + + /// Unboxes a `Box<dyn DynResource>` to a concrete type. + /// + /// # Safety + /// + /// - `self` must be the correct concrete type. + unsafe fn unbox<T: DynResource + 'static>(self: Box<Self>) -> T; +} + +impl<R: DynResource + ?Sized> DynResourceExt for R { + fn expect_downcast_ref<'a, T: DynResource>(&'a self) -> &'a T { + self.as_any() + .downcast_ref() + .expect("Resource doesn't have the expected backend type.") + } + + unsafe fn unbox<T: DynResource + 'static>(self: Box<Self>) -> T { + debug_assert!( + <Self as Any>::type_id(self.as_ref()) == TypeId::of::<T>(), + "Resource doesn't have the expected type, expected {:?}, got {:?}", + TypeId::of::<T>(), + <Self as Any>::type_id(self.as_ref()) + ); + + let casted_ptr = Box::into_raw(self).cast::<T>(); + // SAFETY: This is adheres to the safety contract of `Box::from_raw` because: + // + // - We are casting the value of a previously `Box`ed value, which guarantees: + // - `casted_ptr` is not null. + // - `casted_ptr` is valid for reads and writes, though by itself this does not mean + // valid reads and writes for `T` (read on for that). + // - We don't change the allocator. + // - The contract of `Box::from_raw` requires that an initialized and aligned `T` is stored + // within `casted_ptr`. + *unsafe { Box::from_raw(casted_ptr) } + } +} + +pub trait DynAccelerationStructure: DynResource + fmt::Debug {} +pub trait DynBindGroup: DynResource + fmt::Debug {} +pub trait DynBindGroupLayout: DynResource + fmt::Debug {} +pub trait DynBuffer: DynResource + fmt::Debug {} +pub trait DynCommandBuffer: DynResource + fmt::Debug {} +pub trait DynComputePipeline: DynResource + fmt::Debug {} +pub trait DynFence: DynResource + fmt::Debug {} +pub trait DynPipelineCache: DynResource + fmt::Debug {} +pub trait DynPipelineLayout: DynResource + fmt::Debug {} +pub trait DynQuerySet: DynResource + fmt::Debug {} +pub trait DynRenderPipeline: DynResource + fmt::Debug {} +pub trait DynRayTracingPipeline: DynResource + fmt::Debug {} +pub trait DynSampler: DynResource + fmt::Debug {} +pub trait DynShaderModule: DynResource + fmt::Debug {} +pub trait DynSurfaceTexture: + DynResource + core::borrow::Borrow<dyn DynTexture> + fmt::Debug +{ +} +pub trait DynTexture: DynResource + fmt::Debug {} +pub trait DynTextureView: DynResource + fmt::Debug {} + +impl<'a> BufferBinding<'a, dyn DynBuffer> { + pub fn expect_downcast<B: DynBuffer>(self) -> BufferBinding<'a, B> { + BufferBinding { + buffer: self.buffer.expect_downcast_ref(), + offset: self.offset, + size: self.size, + } + } +} + +impl<'a> TextureBinding<'a, dyn DynTextureView> { + pub fn expect_downcast<T: DynTextureView>(self) -> TextureBinding<'a, T> { + TextureBinding { + view: self.view.expect_downcast_ref(), + usage: self.usage, + } + } +} + +impl<'a> ExternalTextureBinding<'a, dyn DynBuffer, dyn DynTextureView> { + pub fn expect_downcast<B: DynBuffer, T: DynTextureView>( + self, + ) -> ExternalTextureBinding<'a, B, T> { + let planes = self.planes.map(|plane| plane.expect_downcast()); + let params = self.params.expect_downcast(); + ExternalTextureBinding { planes, params } + } +} + +impl<'a> ProgrammableStage<'a, dyn DynShaderModule> { + fn expect_downcast<T: DynShaderModule>(self) -> ProgrammableStage<'a, T> { + ProgrammableStage { + module: self.module.expect_downcast_ref(), + entry_point: self.entry_point, + constants: self.constants, + zero_initialize_workgroup_memory: self.zero_initialize_workgroup_memory, + } + } +} + +impl<'a> AccelerationStructureEntries<'a, dyn DynBuffer> { + fn expect_downcast<B: DynBuffer>(&self) -> AccelerationStructureEntries<'a, B> { + match self { + AccelerationStructureEntries::Instances(instances) => { + AccelerationStructureEntries::Instances(AccelerationStructureInstances { + buffer: instances.buffer.map(|b| b.expect_downcast_ref()), + offset: instances.offset, + count: instances.count, + }) + } + AccelerationStructureEntries::Triangles(triangles) => { + AccelerationStructureEntries::Triangles( + triangles + .iter() + .map(|t| AccelerationStructureTriangles { + vertex_buffer: t.vertex_buffer.map(|b| b.expect_downcast_ref()), + vertex_format: t.vertex_format, + first_vertex: t.first_vertex, + vertex_count: t.vertex_count, + vertex_stride: t.vertex_stride, + indices: t.indices.as_ref().map(|i| { + AccelerationStructureTriangleIndices { + buffer: i.buffer.map(|b| b.expect_downcast_ref()), + format: i.format, + offset: i.offset, + count: i.count, + } + }), + transform: t.transform.as_ref().map(|t| { + AccelerationStructureTriangleTransform { + buffer: t.buffer.expect_downcast_ref(), + offset: t.offset, + } + }), + flags: t.flags, + }) + .collect(), + ) + } + AccelerationStructureEntries::AABBs(entries) => AccelerationStructureEntries::AABBs( + entries + .iter() + .map(|e| AccelerationStructureAABBs { + buffer: e.buffer.map(|b| b.expect_downcast_ref()), + offset: e.offset, + count: e.count, + stride: e.stride, + flags: e.flags, + }) + .collect(), + ), + } + } +} diff --git a/third_party/wgpu-hal/src/dynamic/queue.rs b/third_party/wgpu-hal/src/dynamic/queue.rs new file mode 100644 index 000000000..5425b2d12 --- /dev/null +++ b/third_party/wgpu-hal/src/dynamic/queue.rs @@ -0,0 +1,61 @@ +use alloc::{boxed::Box, vec::Vec}; + +use crate::{ + DeviceError, DynCommandBuffer, DynFence, DynResource, DynSurface, DynSurfaceTexture, + FenceValue, Queue, SurfaceError, +}; + +use super::DynResourceExt as _; + +pub trait DynQueue: DynResource { + unsafe fn submit( + &self, + command_buffers: &[&dyn DynCommandBuffer], + surface_textures: &[&dyn DynSurfaceTexture], + signal_fence: (&dyn DynFence, FenceValue), + ) -> Result<(), DeviceError>; + unsafe fn present( + &self, + surface: &dyn DynSurface, + texture: Box<dyn DynSurfaceTexture>, + ) -> Result<(), SurfaceError>; + unsafe fn get_timestamp_period(&self) -> f32; + unsafe fn wait_for_idle(&self) -> Result<(), DeviceError>; +} + +impl<Q: Queue + DynResource> DynQueue for Q { + unsafe fn submit( + &self, + command_buffers: &[&dyn DynCommandBuffer], + surface_textures: &[&dyn DynSurfaceTexture], + signal_fence: (&dyn DynFence, FenceValue), + ) -> Result<(), DeviceError> { + let command_buffers = command_buffers + .iter() + .map(|cb| (*cb).expect_downcast_ref()) + .collect::<Vec<_>>(); + let surface_textures = surface_textures + .iter() + .map(|surface| (*surface).expect_downcast_ref()) + .collect::<Vec<_>>(); + let signal_fence = (signal_fence.0.expect_downcast_ref(), signal_fence.1); + unsafe { Q::submit(self, &command_buffers, &surface_textures, signal_fence) } + } + + unsafe fn present( + &self, + surface: &dyn DynSurface, + texture: Box<dyn DynSurfaceTexture>, + ) -> Result<(), SurfaceError> { + let surface = surface.expect_downcast_ref(); + unsafe { Q::present(self, surface, texture.unbox()) } + } + + unsafe fn get_timestamp_period(&self) -> f32 { + unsafe { Q::get_timestamp_period(self) } + } + + unsafe fn wait_for_idle(&self) -> Result<(), DeviceError> { + unsafe { Q::wait_for_idle(self) } + } +} diff --git a/third_party/wgpu-hal/src/dynamic/surface.rs b/third_party/wgpu-hal/src/dynamic/surface.rs new file mode 100644 index 000000000..17fa8ee89 --- /dev/null +++ b/third_party/wgpu-hal/src/dynamic/surface.rs @@ -0,0 +1,72 @@ +use alloc::boxed::Box; +use core::time::Duration; + +use crate::{ + DynDevice, DynFence, DynResource, DynSurfaceTexture, Surface, SurfaceConfiguration, + SurfaceError, +}; + +use super::DynResourceExt as _; + +#[derive(Debug)] +pub struct DynAcquiredSurfaceTexture { + pub texture: Box<dyn DynSurfaceTexture>, + /// The presentation configuration no longer matches + /// the surface properties exactly, but can still be used to present + /// to the surface successfully. + pub suboptimal: bool, +} + +pub trait DynSurface: DynResource { + unsafe fn configure( + &self, + device: &dyn DynDevice, + config: &SurfaceConfiguration, + ) -> Result<(), SurfaceError>; + + unsafe fn unconfigure(&self, device: &dyn DynDevice); + + unsafe fn acquire_texture( + &self, + timeout: Option<Duration>, + fence: &dyn DynFence, + ) -> Result<DynAcquiredSurfaceTexture, SurfaceError>; + + unsafe fn discard_texture(&self, texture: Box<dyn DynSurfaceTexture>); +} + +impl<S: Surface + DynResource> DynSurface for S { + unsafe fn configure( + &self, + device: &dyn DynDevice, + config: &SurfaceConfiguration, + ) -> Result<(), SurfaceError> { + let device = device.expect_downcast_ref(); + unsafe { S::configure(self, device, config) } + } + + unsafe fn unconfigure(&self, device: &dyn DynDevice) { + let device = device.expect_downcast_ref(); + unsafe { S::unconfigure(self, device) } + } + + unsafe fn acquire_texture( + &self, + timeout: Option<Duration>, + fence: &dyn DynFence, + ) -> Result<DynAcquiredSurfaceTexture, SurfaceError> { + let fence = fence.expect_downcast_ref(); + unsafe { S::acquire_texture(self, timeout, fence) }.map(|ast| { + let texture = Box::new(ast.texture); + let suboptimal = ast.suboptimal; + DynAcquiredSurfaceTexture { + texture, + suboptimal, + } + }) + } + + unsafe fn discard_texture(&self, texture: Box<dyn DynSurfaceTexture>) { + unsafe { S::discard_texture(self, texture.unbox()) } + } +} diff --git a/third_party/wgpu-hal/src/gles/adapter.rs b/third_party/wgpu-hal/src/gles/adapter.rs new file mode 100644 index 000000000..47b8115a8 --- /dev/null +++ b/third_party/wgpu-hal/src/gles/adapter.rs @@ -0,0 +1,1470 @@ +use alloc::{borrow::ToOwned as _, format, string::String, sync::Arc, vec, vec::Vec}; +use core::sync::atomic::AtomicU8; + +use glow::HasContext; +use parking_lot::Mutex; +use wgt::AstcChannel; + +use crate::auxil::db; +use crate::gles::ShaderClearProgram; + +// https://webgl2fundamentals.org/webgl/lessons/webgl-data-textures.html + +const GL_UNMASKED_VENDOR_WEBGL: u32 = 0x9245; +const GL_UNMASKED_RENDERER_WEBGL: u32 = 0x9246; + +impl super::Adapter { + pub fn get_glsl_version(&self) -> naga::back::glsl::Version { + self.shared.shading_language_version + } + + /// Note that this function is intentionally lenient in regards to parsing, + /// and will try to recover at least the first two version numbers without + /// resulting in an `Err`. + /// # Notes + /// `WebGL 2` version returned as `OpenGL ES 3.0` + fn parse_version(mut src: &str) -> Result<(u8, u8), crate::InstanceError> { + let webgl_sig = "WebGL "; + // According to the WebGL specification + // VERSION WebGL<space>1.0<space><vendor-specific information> + // SHADING_LANGUAGE_VERSION WebGL<space>GLSL<space>ES<space>1.0<space><vendor-specific information> + let is_webgl = src.starts_with(webgl_sig); + if is_webgl { + let pos = src.rfind(webgl_sig).unwrap_or(0); + src = &src[pos + webgl_sig.len()..]; + } else { + let es_sig = " ES "; + match src.rfind(es_sig) { + Some(pos) => { + src = &src[pos + es_sig.len()..]; + } + None => { + return Err(crate::InstanceError::new(format!( + "OpenGL version {src:?} does not contain 'ES'" + ))); + } + } + }; + + let glsl_es_sig = "GLSL ES "; + let is_glsl = match src.find(glsl_es_sig) { + Some(pos) => { + src = &src[pos + glsl_es_sig.len()..]; + true + } + None => false, + }; + + Self::parse_full_version(src).map(|(major, minor)| { + ( + // Return WebGL 2.0 version as OpenGL ES 3.0 + if is_webgl && !is_glsl { + major + 1 + } else { + major + }, + minor, + ) + }) + } + + /// According to the OpenGL specification, the version information is + /// expected to follow the following syntax: + /// + /// ~~~bnf + /// <major> ::= <number> + /// <minor> ::= <number> + /// <revision> ::= <number> + /// <vendor-info> ::= <string> + /// <release> ::= <major> "." <minor> ["." <release>] + /// <version> ::= <release> [" " <vendor-info>] + /// ~~~ + /// + /// Note that this function is intentionally lenient in regards to parsing, + /// and will try to recover at least the first two version numbers without + /// resulting in an `Err`. + pub(super) fn parse_full_version(src: &str) -> Result<(u8, u8), crate::InstanceError> { + let (version, _vendor_info) = match src.find(' ') { + Some(i) => (&src[..i], src[i + 1..].to_owned()), + None => (src, String::new()), + }; + + // TODO: make this even more lenient so that we can also accept + // `<major> "." <minor> [<???>]` + let mut it = version.split('.'); + let major = it.next().and_then(|s| s.parse().ok()); + let minor = it.next().and_then(|s| { + let trimmed = if s.starts_with('0') { + "0" + } else { + s.trim_end_matches('0') + }; + trimmed.parse().ok() + }); + + match (major, minor) { + (Some(major), Some(minor)) => Ok((major, minor)), + _ => Err(crate::InstanceError::new(format!( + "unable to extract OpenGL version from {version:?}" + ))), + } + } + + fn make_info(vendor_orig: String, renderer_orig: String, version: String) -> wgt::AdapterInfo { + let vendor = vendor_orig.to_lowercase(); + let renderer = renderer_orig.to_lowercase(); + + // opengl has no way to discern device_type, so we can try to infer it from the renderer string + let strings_that_imply_integrated = [ + " xpress", // space here is on purpose so we don't match express + "amd renoir", + "radeon hd 4200", + "radeon hd 4250", + "radeon hd 4290", + "radeon hd 4270", + "radeon hd 4225", + "radeon hd 3100", + "radeon hd 3200", + "radeon hd 3000", + "radeon hd 3300", + "radeon(tm) r4 graphics", + "radeon(tm) r5 graphics", + "radeon(tm) r6 graphics", + "radeon(tm) r7 graphics", + "radeon r7 graphics", + "nforce", // all nvidia nforce are integrated + "tegra", // all nvidia tegra are integrated + "shield", // all nvidia shield are integrated + "igp", + "mali", + "intel", + "v3d", + "apple m", // all apple m are integrated + ]; + let strings_that_imply_cpu = ["mesa offscreen", "swiftshader", "llvmpipe"]; + + //TODO: handle Intel Iris XE as discreet + let inferred_device_type = if vendor.contains("qualcomm") + || vendor.contains("intel") + || strings_that_imply_integrated + .iter() + .any(|&s| renderer.contains(s)) + { + wgt::DeviceType::IntegratedGpu + } else if strings_that_imply_cpu.iter().any(|&s| renderer.contains(s)) { + wgt::DeviceType::Cpu + } else { + // At this point the Device type is Unknown. + // It's most likely DiscreteGpu, but we do not know for sure. + // Use "Other" to avoid possibly making incorrect assumptions. + // Note that if this same device is available under some other API (ex: Vulkan), + // It will mostly likely get a different device type (probably DiscreteGpu). + wgt::DeviceType::Other + }; + + // source: Sascha Willems at Vulkan + let vendor_id = if vendor.contains("amd") { + db::amd::VENDOR + } else if vendor.contains("imgtec") { + db::imgtec::VENDOR + } else if vendor.contains("nvidia") { + db::nvidia::VENDOR + } else if vendor.contains("arm") { + db::arm::VENDOR + } else if vendor.contains("qualcomm") { + db::qualcomm::VENDOR + } else if vendor.contains("intel") { + db::intel::VENDOR + } else if vendor.contains("broadcom") { + db::broadcom::VENDOR + } else if vendor.contains("mesa") { + db::mesa::VENDOR + } else if vendor.contains("apple") { + db::apple::VENDOR + } else { + 0 + }; + + wgt::AdapterInfo { + name: renderer_orig, + vendor: vendor_id, + driver_info: version, + ..wgt::AdapterInfo::new(inferred_device_type, wgt::Backend::Gl) + } + } + + pub(super) unsafe fn expose( + context: super::AdapterContext, + backend_options: wgt::GlBackendOptions, + ) -> Option<crate::ExposedAdapter<super::Api>> { + let gl = context.lock(); + let extensions = gl.supported_extensions(); + + let (vendor_const, renderer_const) = if extensions.contains("WEBGL_debug_renderer_info") { + // emscripten doesn't enable "WEBGL_debug_renderer_info" extension by default. so, we do it manually. + // See https://github.com/gfx-rs/wgpu/issues/3245 for context + #[cfg(Emscripten)] + if unsafe { + super::emscripten::enable_extension(c"WEBGL_debug_renderer_info".to_str().unwrap()) + } { + (GL_UNMASKED_VENDOR_WEBGL, GL_UNMASKED_RENDERER_WEBGL) + } else { + (glow::VENDOR, glow::RENDERER) + } + // glow already enables WEBGL_debug_renderer_info on wasm32-unknown-unknown target by default. + #[cfg(not(Emscripten))] + (GL_UNMASKED_VENDOR_WEBGL, GL_UNMASKED_RENDERER_WEBGL) + } else { + (glow::VENDOR, glow::RENDERER) + }; + + let vendor = unsafe { gl.get_parameter_string(vendor_const) }; + let renderer = unsafe { gl.get_parameter_string(renderer_const) }; + let version = unsafe { gl.get_parameter_string(glow::VERSION) }; + log::debug!("Vendor: {vendor}"); + log::debug!("Renderer: {renderer}"); + log::debug!("Version: {version}"); + + let full_ver = Self::parse_full_version(&version).ok(); + let es_ver = full_ver.map_or_else(|| Self::parse_version(&version).ok(), |_| None); + + if let Some(full_ver) = full_ver { + let core_profile = (full_ver >= (3, 2)).then(|| unsafe { + gl.get_parameter_i32(glow::CONTEXT_PROFILE_MASK) + & glow::CONTEXT_CORE_PROFILE_BIT as i32 + != 0 + }); + log::trace!( + "Profile: {}", + core_profile + .map(|core_profile| if core_profile { + "Core" + } else { + "Compatibility" + }) + .unwrap_or("Legacy") + ); + } + + if es_ver.is_none() && full_ver.is_none() { + log::warn!("Unable to parse OpenGL version"); + return None; + } + + if let Some(es_ver) = es_ver { + if es_ver < (3, 0) { + log::warn!( + "Returned GLES context is {}.{}, when 3.0+ was requested", + es_ver.0, + es_ver.1 + ); + return None; + } + } + + if let Some(full_ver) = full_ver { + if full_ver < (3, 3) { + log::warn!( + "Returned GL context is {}.{}, when 3.3+ is needed", + full_ver.0, + full_ver.1 + ); + return None; + } + } + + let shading_language_version = { + let sl_version = unsafe { gl.get_parameter_string(glow::SHADING_LANGUAGE_VERSION) }; + log::debug!("SL version: {}", &sl_version); + if full_ver.is_some() { + let (sl_major, sl_minor) = Self::parse_full_version(&sl_version).ok()?; + let mut value = sl_major as u16 * 100 + sl_minor as u16 * 10; + // Naga doesn't think it supports GL 460+, so we cap it at 450 + if value > 450 { + value = 450; + } + naga::back::glsl::Version::Desktop(value) + } else { + let (sl_major, sl_minor) = Self::parse_version(&sl_version).ok()?; + let value = sl_major as u16 * 100 + sl_minor as u16 * 10; + naga::back::glsl::Version::Embedded { + version: value, + is_webgl: cfg!(any(webgl, Emscripten)), + } + } + }; + + log::debug!("Supported GL Extensions: {extensions:#?}"); + + let supported = |(req_es_major, req_es_minor), (req_full_major, req_full_minor)| { + let es_supported = es_ver + .map(|es_ver| es_ver >= (req_es_major, req_es_minor)) + .unwrap_or_default(); + + let full_supported = full_ver + .map(|full_ver| full_ver >= (req_full_major, req_full_minor)) + .unwrap_or_default(); + + es_supported || full_supported + }; + + let supports_storage = + supported((3, 1), (4, 3)) || extensions.contains("GL_ARB_shader_storage_buffer_object"); + let supports_compute = + supported((3, 1), (4, 3)) || extensions.contains("GL_ARB_compute_shader"); + let supports_work_group_params = supports_compute; + + // ANGLE provides renderer strings like: "ANGLE (Apple, Apple M1 Pro, OpenGL 4.1)" + let is_angle = renderer.contains("ANGLE"); + + let vertex_shader_storage_blocks = if supports_storage { + let value = + (unsafe { gl.get_parameter_i32(glow::MAX_VERTEX_SHADER_STORAGE_BLOCKS) } as u32); + + if value == 0 && extensions.contains("GL_ARB_shader_storage_buffer_object") { + // The driver for AMD Radeon HD 5870 returns zero here, so assume the value matches the compute shader storage block count. + // Windows doesn't recognize `GL_MAX_VERTEX_ATTRIB_STRIDE`. + let new = (unsafe { gl.get_parameter_i32(glow::MAX_COMPUTE_SHADER_STORAGE_BLOCKS) } + as u32); + log::debug!("Max vertex shader storage blocks is zero, but GL_ARB_shader_storage_buffer_object is specified. Assuming the compute value {new}"); + new + } else { + value + } + } else { + 0 + }; + let fragment_shader_storage_blocks = if supports_storage { + (unsafe { gl.get_parameter_i32(glow::MAX_FRAGMENT_SHADER_STORAGE_BLOCKS) } as u32) + } else { + 0 + }; + let vertex_shader_storage_textures = if supports_storage { + (unsafe { gl.get_parameter_i32(glow::MAX_VERTEX_IMAGE_UNIFORMS) } as u32) + } else { + 0 + }; + let fragment_shader_storage_textures = if supports_storage { + (unsafe { gl.get_parameter_i32(glow::MAX_FRAGMENT_IMAGE_UNIFORMS) } as u32) + } else { + 0 + }; + let max_storage_block_size = if supports_storage { + (unsafe { gl.get_parameter_i32(glow::MAX_SHADER_STORAGE_BLOCK_SIZE) } as u32) + } else { + 0 + }; + let max_element_index = unsafe { gl.get_parameter_i32(glow::MAX_ELEMENT_INDEX) } as u32; + + // WORKAROUND: In order to work around an issue with GL on RPI4 and similar, we ignore a + // zero vertex ssbo count if there are vertex sstos. (more info: + // https://github.com/gfx-rs/wgpu/pull/1607#issuecomment-874938961) The hardware does not + // want us to write to these SSBOs, but GLES cannot express that. We detect this case and + // disable writing to SSBOs. + let vertex_ssbo_false_zero = + vertex_shader_storage_blocks == 0 && vertex_shader_storage_textures != 0; + if vertex_ssbo_false_zero { + // We only care about fragment here as the 0 is a lie. + log::debug!("Max vertex shader SSBO == 0 and SSTO != 0. Interpreting as false zero."); + } + + let max_storage_buffers_per_shader_stage = if vertex_shader_storage_blocks == 0 { + fragment_shader_storage_blocks + } else { + vertex_shader_storage_blocks.min(fragment_shader_storage_blocks) + }; + let max_storage_textures_per_shader_stage = if vertex_shader_storage_textures == 0 { + fragment_shader_storage_textures + } else { + vertex_shader_storage_textures.min(fragment_shader_storage_textures) + }; + // NOTE: GL_ARB_compute_shader adds support for indirect dispatch + let indirect_execution = supported((3, 1), (4, 3)) + || (extensions.contains("GL_ARB_draw_indirect") && supports_compute); + let supports_cube_array = supported((3, 2), (4, 0)) + || (supported((3, 1), (4, 0)) && extensions.contains("GL_EXT_texture_cube_map_array")); + + let mut downlevel_flags = wgt::DownlevelFlags::empty() + | wgt::DownlevelFlags::NON_POWER_OF_TWO_MIPMAPPED_TEXTURES + | wgt::DownlevelFlags::COMPARISON_SAMPLERS + | wgt::DownlevelFlags::SHADER_F16_IN_F32 + | wgt::DownlevelFlags::MSL2_1; + downlevel_flags.set( + wgt::DownlevelFlags::CUBE_ARRAY_TEXTURES, + supports_cube_array, + ); + downlevel_flags.set(wgt::DownlevelFlags::COMPUTE_SHADERS, supports_compute); + downlevel_flags.set( + wgt::DownlevelFlags::FRAGMENT_WRITABLE_STORAGE, + max_storage_block_size != 0, + ); + downlevel_flags.set(wgt::DownlevelFlags::INDIRECT_EXECUTION, indirect_execution); + downlevel_flags.set(wgt::DownlevelFlags::BASE_VERTEX, supported((3, 2), (3, 2))); + downlevel_flags.set( + wgt::DownlevelFlags::INDEPENDENT_BLEND, + supported((3, 2), (4, 0)) || extensions.contains("GL_EXT_draw_buffers_indexed"), + ); + downlevel_flags.set( + wgt::DownlevelFlags::VERTEX_STORAGE, + max_storage_block_size != 0 + && max_storage_buffers_per_shader_stage != 0 + && (vertex_shader_storage_blocks != 0 || vertex_ssbo_false_zero), + ); + downlevel_flags.set(wgt::DownlevelFlags::FRAGMENT_STORAGE, supports_storage); + if extensions.contains("EXT_texture_filter_anisotropic") + || extensions.contains("GL_EXT_texture_filter_anisotropic") + { + let max_aniso = + unsafe { gl.get_parameter_i32(glow::MAX_TEXTURE_MAX_ANISOTROPY_EXT) } as u32; + downlevel_flags.set(wgt::DownlevelFlags::ANISOTROPIC_FILTERING, max_aniso >= 16); + } + downlevel_flags.set( + wgt::DownlevelFlags::BUFFER_BINDINGS_NOT_16_BYTE_ALIGNED, + !(cfg!(any(webgl, Emscripten)) || is_angle), + ); + // see https://registry.khronos.org/webgl/specs/latest/2.0/#BUFFER_OBJECT_BINDING + downlevel_flags.set( + wgt::DownlevelFlags::UNRESTRICTED_INDEX_BUFFER, + !cfg!(any(webgl, Emscripten)), + ); + downlevel_flags.set( + wgt::DownlevelFlags::UNRESTRICTED_EXTERNAL_TEXTURE_COPIES, + !cfg!(any(webgl, Emscripten)), + ); + downlevel_flags.set( + wgt::DownlevelFlags::FULL_DRAW_INDEX_UINT32, + max_element_index == u32::MAX, + ); + downlevel_flags.set( + wgt::DownlevelFlags::MULTISAMPLED_SHADING, + supported((3, 2), (4, 0)) || extensions.contains("OES_sample_variables"), + ); + let query_buffers = extensions.contains("GL_ARB_query_buffer_object") + || extensions.contains("GL_AMD_query_buffer_object"); + if query_buffers { + downlevel_flags.set(wgt::DownlevelFlags::NONBLOCKING_QUERY_RESOLVE, true); + } + + // Desktop GL: norm16 is core since GL 3.0/3.1; we minimum-version + // to GL 3.3, so always on. GLES/WebGL2: needs `EXT_texture_norm16`. + let supports_16bit_norm = if es_ver.is_some() { + extensions.contains("GL_EXT_texture_norm16") + || extensions.contains("EXT_texture_norm16") + } else { + true + }; + // SNORM color-rendering is not spec-guaranteed on either path + // (GLES Table 8.13 marks it not-renderable; desktop GL exposes + // it as only "optionally renderable"), so gate it on + // `EXT_render_snorm` for both - matching how `COLOR_BUFFER_FLOAT` + // is probed above. + let supports_16bit_snorm_renderable = supports_16bit_norm + && (extensions.contains("GL_EXT_render_snorm") + || extensions.contains("EXT_render_snorm")); + // Storage on norm16. ARB_shader_image_load_store / GL 4.2 Table X.2 and + // NV_image_formats Table 8.27 both list `r16/rg16/rgba16` and the SNORM + // variants as image-unit formats, so one gate covers UNORM and SNORM. + // Paths: + // * Desktop: core in GL 4.2+; on 3.3..=4.1 via `GL_ARB_shader_image_load_store`. + // * GLES/WebGL2: only via `GL_NV_image_formats` - the ES 3.1/3.2 core + // Table 8.27 omits these formats, and `GL_EXT_texture_norm16` does not + // extend Table 8.27 (NV_image_formats itself depends on EXT_texture_norm16 + // for the norm16 entries, so an ES driver exposing NV_image_formats + // without EXT_texture_norm16 still wouldn't accept them - the + // `supports_16bit_norm` prerequisite below catches that). + let supports_16bit_norm_storage = supports_16bit_norm + && if es_ver.is_some() { + extensions.contains("GL_NV_image_formats") + } else { + full_ver.is_some_and(|v| v >= (4, 2)) + || extensions.contains("GL_ARB_shader_image_load_store") + }; + + let mut features = wgt::Features::empty() + | wgt::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES + | wgt::Features::CLEAR_TEXTURE + | wgt::Features::IMMEDIATES + | wgt::Features::DEPTH32FLOAT_STENCIL8 + | wgt::Features::PASSTHROUGH_SHADERS; + features.set( + wgt::Features::TEXTURE_FORMAT_16BIT_NORM, + supports_16bit_norm, + ); + features.set( + wgt::Features::ADDRESS_MODE_CLAMP_TO_BORDER | wgt::Features::ADDRESS_MODE_CLAMP_TO_ZERO, + extensions.contains("GL_EXT_texture_border_clamp") + || extensions.contains("GL_ARB_texture_border_clamp"), + ); + features.set( + wgt::Features::DEPTH_CLIP_CONTROL, + extensions.contains("GL_EXT_depth_clamp") || extensions.contains("GL_ARB_depth_clamp"), + ); + features.set( + wgt::Features::VERTEX_WRITABLE_STORAGE, + downlevel_flags.contains(wgt::DownlevelFlags::VERTEX_STORAGE) + && vertex_shader_storage_textures != 0, + ); + features.set( + wgt::Features::MULTIVIEW, + extensions.contains("OVR_multiview2") || extensions.contains("GL_OVR_multiview2"), + ); + features.set( + wgt::Features::DUAL_SOURCE_BLENDING, + extensions.contains("GL_EXT_blend_func_extended") + || extensions.contains("GL_ARB_blend_func_extended"), + ); + features.set( + wgt::Features::CLIP_DISTANCES, + full_ver.is_some() || extensions.contains("GL_EXT_clip_cull_distance"), + ); + features.set( + wgt::Features::PRIMITIVE_INDEX, + supported((3, 2), (3, 2)) + || extensions.contains("OES_geometry_shader") + || extensions.contains("GL_ARB_geometry_shader4"), + ); + features.set( + wgt::Features::SHADER_EARLY_DEPTH_TEST, + supported((3, 1), (4, 2)) || extensions.contains("GL_ARB_shader_image_load_store"), + ); + if extensions.contains("GL_ARB_timer_query") { + features.set(wgt::Features::TIMESTAMP_QUERY, true); + features.set(wgt::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS, true); + features.set(wgt::Features::TIMESTAMP_QUERY_INSIDE_PASSES, true); + } + let gl_bcn_exts = [ + "GL_EXT_texture_compression_s3tc", + "GL_EXT_texture_compression_rgtc", + "GL_ARB_texture_compression_bptc", + ]; + let gles_bcn_exts = [ + "GL_EXT_texture_compression_s3tc_srgb", + "GL_EXT_texture_compression_rgtc", + "GL_EXT_texture_compression_bptc", + ]; + let webgl_bcn_exts = [ + "WEBGL_compressed_texture_s3tc", + "WEBGL_compressed_texture_s3tc_srgb", + "EXT_texture_compression_rgtc", + "EXT_texture_compression_bptc", + ]; + let bcn_exts = if cfg!(any(webgl, Emscripten)) { + &webgl_bcn_exts[..] + } else if es_ver.is_some() { + &gles_bcn_exts[..] + } else { + &gl_bcn_exts[..] + }; + features.set( + wgt::Features::TEXTURE_COMPRESSION_BC, + bcn_exts.iter().all(|&ext| extensions.contains(ext)), + ); + features.set( + wgt::Features::TEXTURE_COMPRESSION_BC_SLICED_3D, + bcn_exts.iter().all(|&ext| extensions.contains(ext)), // BC guaranteed Sliced 3D + ); + let has_etc = if cfg!(any(webgl, Emscripten)) { + extensions.contains("WEBGL_compressed_texture_etc") + } else { + es_ver.is_some() || extensions.contains("GL_ARB_ES3_compatibility") + }; + features.set(wgt::Features::TEXTURE_COMPRESSION_ETC2, has_etc); + + // `OES_texture_compression_astc` provides 2D + 3D, LDR + HDR support + if extensions.contains("WEBGL_compressed_texture_astc") + || extensions.contains("GL_OES_texture_compression_astc") + { + #[cfg(webgl)] + { + if context + .glow_context + .compressed_texture_astc_supports_ldr_profile() + { + features.insert(wgt::Features::TEXTURE_COMPRESSION_ASTC); + features.insert(wgt::Features::TEXTURE_COMPRESSION_ASTC_SLICED_3D); + } + if context + .glow_context + .compressed_texture_astc_supports_hdr_profile() + { + features.insert(wgt::Features::TEXTURE_COMPRESSION_ASTC_HDR); + } + } + + #[cfg(any(native, Emscripten))] + { + features.insert(wgt::Features::TEXTURE_COMPRESSION_ASTC); + features.insert(wgt::Features::TEXTURE_COMPRESSION_ASTC_SLICED_3D); + features.insert(wgt::Features::TEXTURE_COMPRESSION_ASTC_HDR); + } + } else { + features.set( + wgt::Features::TEXTURE_COMPRESSION_ASTC, + extensions.contains("GL_KHR_texture_compression_astc_ldr"), + ); + features.set( + wgt::Features::TEXTURE_COMPRESSION_ASTC_SLICED_3D, + extensions.contains("GL_KHR_texture_compression_astc_ldr") + && extensions.contains("GL_KHR_texture_compression_astc_sliced_3d"), + ); + features.set( + wgt::Features::TEXTURE_COMPRESSION_ASTC_HDR, + extensions.contains("GL_KHR_texture_compression_astc_hdr"), + ); + } + + downlevel_flags.set( + wgt::DownlevelFlags::TEXTURE_COMPRESSION, + features.contains(wgt::Features::TEXTURE_COMPRESSION_BC) + || features.contains( + wgt::Features::TEXTURE_COMPRESSION_ETC2 + | wgt::Features::TEXTURE_COMPRESSION_ASTC, + ), + ); + + features.set( + wgt::Features::FLOAT32_FILTERABLE, + extensions.contains("GL_ARB_color_buffer_float") + || extensions.contains("GL_EXT_color_buffer_float") + || extensions.contains("OES_texture_float_linear"), + ); + + if es_ver.is_none() { + features |= wgt::Features::POLYGON_MODE_LINE | wgt::Features::POLYGON_MODE_POINT; + } + + // We *might* be able to emulate bgra8unorm-storage but currently don't attempt to. + + let mut private_caps = super::PrivateCapabilities::empty(); + private_caps.set( + super::PrivateCapabilities::BUFFER_ALLOCATION, + extensions.contains("GL_EXT_buffer_storage") + || extensions.contains("GL_ARB_buffer_storage"), + ); + private_caps.set( + super::PrivateCapabilities::SHADER_BINDING_LAYOUT, + supports_compute, + ); + private_caps.set( + super::PrivateCapabilities::SHADER_TEXTURE_SHADOW_LOD, + extensions.contains("GL_EXT_texture_shadow_lod"), + ); + private_caps.set( + super::PrivateCapabilities::MEMORY_BARRIERS, + supported((3, 1), (4, 2)), + ); + private_caps.set( + super::PrivateCapabilities::VERTEX_BUFFER_LAYOUT, + supported((3, 1), (4, 3)) || extensions.contains("GL_ARB_vertex_attrib_binding"), + ); + private_caps.set( + super::PrivateCapabilities::INDEX_BUFFER_ROLE_CHANGE, + !cfg!(any(webgl, Emscripten)), + ); + private_caps.set( + super::PrivateCapabilities::GET_BUFFER_SUB_DATA, + cfg!(any(webgl, Emscripten)) || full_ver.is_some(), + ); + let color_buffer_float = extensions.contains("GL_EXT_color_buffer_float") + || extensions.contains("GL_ARB_color_buffer_float") + || extensions.contains("EXT_color_buffer_float"); + let color_buffer_half_float = extensions.contains("GL_EXT_color_buffer_half_float") + || extensions.contains("GL_ARB_half_float_pixel"); + private_caps.set( + super::PrivateCapabilities::COLOR_BUFFER_HALF_FLOAT, + color_buffer_half_float || color_buffer_float, + ); + private_caps.set( + super::PrivateCapabilities::COLOR_BUFFER_FLOAT, + color_buffer_float, + ); + private_caps.set(super::PrivateCapabilities::QUERY_BUFFERS, query_buffers); + private_caps.set(super::PrivateCapabilities::QUERY_64BIT, full_ver.is_some()); + private_caps.set( + super::PrivateCapabilities::TEXTURE_STORAGE, + supported((3, 0), (4, 2)), + ); + let is_mali = renderer.to_lowercase().contains("mali"); + let debug_fns_enabled = match backend_options.debug_fns { + wgt::GlDebugFns::Auto => gl.supports_debug() && !is_mali, + wgt::GlDebugFns::ForceEnabled => gl.supports_debug(), + wgt::GlDebugFns::Disabled => false, + }; + private_caps.set(super::PrivateCapabilities::DEBUG_FNS, debug_fns_enabled); + private_caps.set( + super::PrivateCapabilities::INVALIDATE_FRAMEBUFFER, + supported((3, 0), (4, 3)), + ); + if let Some(full_ver) = full_ver { + let supported = + full_ver >= (4, 2) && extensions.contains("GL_ARB_shader_draw_parameters"); + private_caps.set( + super::PrivateCapabilities::FULLY_FEATURED_INSTANCING, + supported, + ); + // Desktop 4.2 and greater specify the first instance parameter. + // + // For all other versions, the behavior is undefined. + // + // We only support indirect first instance when we also have ARB_shader_draw_parameters as + // that's the only way to get gl_InstanceID to work correctly. + features.set(wgt::Features::INDIRECT_FIRST_INSTANCE, supported); + } + private_caps.set( + super::PrivateCapabilities::MULTISAMPLED_RENDER_TO_TEXTURE, + extensions.contains("GL_EXT_multisampled_render_to_texture"), + ); + private_caps.set( + super::PrivateCapabilities::TEXTURE_FORMAT_NORM16, + supports_16bit_norm, + ); + private_caps.set( + super::PrivateCapabilities::TEXTURE_FORMAT_SNORM16_RENDERABLE, + supports_16bit_snorm_renderable, + ); + private_caps.set( + super::PrivateCapabilities::TEXTURE_FORMAT_NORM16_STORAGE, + supports_16bit_norm_storage, + ); + + // GLSL ES 3.10+ / GLSL 4.30+ natively support coherent/volatile qualifiers + // on storage buffers. These were introduced alongside storage buffer support. + if supports_storage { + features |= wgt::Features::MEMORY_DECORATION_COHERENT + | wgt::Features::MEMORY_DECORATION_VOLATILE; + } + + let max_texture_size = unsafe { gl.get_parameter_i32(glow::MAX_TEXTURE_SIZE) } as u32; + let max_texture_3d_size = unsafe { gl.get_parameter_i32(glow::MAX_3D_TEXTURE_SIZE) } as u32; + + let min_uniform_buffer_offset_alignment = + (unsafe { gl.get_parameter_i32(glow::UNIFORM_BUFFER_OFFSET_ALIGNMENT) } as u32); + let min_storage_buffer_offset_alignment = if supports_storage { + (unsafe { gl.get_parameter_i32(glow::SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT) } as u32) + } else { + 256 + }; + let max_uniform_buffers_per_shader_stage = + unsafe { gl.get_parameter_i32(glow::MAX_VERTEX_UNIFORM_BLOCKS) } + .min(unsafe { gl.get_parameter_i32(glow::MAX_FRAGMENT_UNIFORM_BLOCKS) }) + as u32; + + let max_compute_workgroups_per_dimension = if supports_work_group_params { + unsafe { gl.get_parameter_indexed_i32(glow::MAX_COMPUTE_WORK_GROUP_COUNT, 0) } + .min(unsafe { gl.get_parameter_indexed_i32(glow::MAX_COMPUTE_WORK_GROUP_COUNT, 1) }) + .min(unsafe { gl.get_parameter_indexed_i32(glow::MAX_COMPUTE_WORK_GROUP_COUNT, 2) }) + as u32 + } else { + 0 + }; + + let max_color_attachments = unsafe { + gl.get_parameter_i32(glow::MAX_COLOR_ATTACHMENTS) + .min(gl.get_parameter_i32(glow::MAX_DRAW_BUFFERS)) as u32 + }; + + // 16 bytes per sample is the maximum size of a color attachment. + let max_color_attachment_bytes_per_sample = + max_color_attachments * wgt::TextureFormat::MAX_TARGET_PIXEL_BYTE_COST; + + let limits = crate::auxil::adjust_raw_limits(wgt::Limits { + max_texture_dimension_1d: max_texture_size, + max_texture_dimension_2d: max_texture_size, + max_texture_dimension_3d: max_texture_3d_size, + max_texture_array_layers: unsafe { + gl.get_parameter_i32(glow::MAX_ARRAY_TEXTURE_LAYERS) + } as u32, + max_bind_groups: u32::MAX, + // No limit. + max_bind_groups_plus_vertex_buffers: u32::MAX, + // No limit. + max_bindings_per_bind_group: u32::MAX, + max_dynamic_uniform_buffers_per_pipeline_layout: max_uniform_buffers_per_shader_stage, + max_dynamic_storage_buffers_per_pipeline_layout: max_storage_buffers_per_shader_stage, + max_sampled_textures_per_shader_stage: super::MAX_TEXTURE_SLOTS as u32, + max_samplers_per_shader_stage: super::MAX_SAMPLERS as u32, + max_storage_buffers_per_shader_stage, + max_storage_textures_per_shader_stage, + max_uniform_buffers_per_shader_stage, + max_binding_array_elements_per_shader_stage: 0, + max_binding_array_sampler_elements_per_shader_stage: 0, + max_binding_array_acceleration_structure_elements_per_shader_stage: 0, + max_uniform_buffer_binding_size: unsafe { + gl.get_parameter_i32(glow::MAX_UNIFORM_BLOCK_SIZE) + } as u64, + max_storage_buffer_binding_size: if supports_storage { + unsafe { gl.get_parameter_i32(glow::MAX_SHADER_STORAGE_BLOCK_SIZE) } + } else { + 0 + } as u64, + max_vertex_buffers: if private_caps + .contains(super::PrivateCapabilities::VERTEX_BUFFER_LAYOUT) + { + (unsafe { gl.get_parameter_i32(glow::MAX_VERTEX_ATTRIB_BINDINGS) } as u32) + } else { + 16 // should this be different? + }, + max_vertex_attributes: (unsafe { gl.get_parameter_i32(glow::MAX_VERTEX_ATTRIBS) } + as u32) + .min(super::MAX_VERTEX_ATTRIBUTES as u32), + max_vertex_buffer_array_stride: if private_caps + .contains(super::PrivateCapabilities::VERTEX_BUFFER_LAYOUT) + { + if let Some(full_ver) = full_ver { + if full_ver >= (4, 4) { + // We can query `GL_MAX_VERTEX_ATTRIB_STRIDE` in OpenGL 4.4+ + let value = + (unsafe { gl.get_parameter_i32(glow::MAX_VERTEX_ATTRIB_STRIDE) }) + as u32; + + if value == 0 { + // This should be at least 2048, but the driver for AMD Radeon HD 5870 on + // Windows doesn't recognize `GL_MAX_VERTEX_ATTRIB_STRIDE`. + + log::debug!("Max vertex attribute stride is 0. Assuming it is the OpenGL minimum spec 2048"); + 2048 + } else { + value + } + } else { + log::debug!("Max vertex attribute stride unknown. Assuming it is the OpenGL minimum spec 2048"); + 2048 + } + } else { + (unsafe { gl.get_parameter_i32(glow::MAX_VERTEX_ATTRIB_STRIDE) }) as u32 + } + } else { + !0 + }, + max_immediate_size: super::MAX_IMMEDIATES as u32 * 4, + min_uniform_buffer_offset_alignment, + min_storage_buffer_offset_alignment, + max_inter_stage_shader_variables: { + // MAX_VARYING_COMPONENTS may return 0, because it is deprecated since OpenGL 3.2 core, + // and an OpenGL Context with the core profile and with forward-compatibility=true, + // will make deprecated constants unavailable. + let max_varying_components = + unsafe { gl.get_parameter_i32(glow::MAX_VARYING_COMPONENTS) } as u32; + if max_varying_components == 0 { + // default value for max_inter_stage_shader_variables + 15 + } else { + max_varying_components / 4 + } + }, + max_color_attachments, + max_color_attachment_bytes_per_sample, + max_compute_workgroup_storage_size: if supports_work_group_params { + (unsafe { gl.get_parameter_i32(glow::MAX_COMPUTE_SHARED_MEMORY_SIZE) } as u32) + } else { + 0 + }, + max_compute_invocations_per_workgroup: if supports_work_group_params { + (unsafe { gl.get_parameter_i32(glow::MAX_COMPUTE_WORK_GROUP_INVOCATIONS) } as u32) + } else { + 0 + }, + max_compute_workgroup_size_x: if supports_work_group_params { + (unsafe { gl.get_parameter_indexed_i32(glow::MAX_COMPUTE_WORK_GROUP_SIZE, 0) } + as u32) + } else { + 0 + }, + max_compute_workgroup_size_y: if supports_work_group_params { + (unsafe { gl.get_parameter_indexed_i32(glow::MAX_COMPUTE_WORK_GROUP_SIZE, 1) } + as u32) + } else { + 0 + }, + max_compute_workgroup_size_z: if supports_work_group_params { + (unsafe { gl.get_parameter_indexed_i32(glow::MAX_COMPUTE_WORK_GROUP_SIZE, 2) } + as u32) + } else { + 0 + }, + max_compute_workgroups_per_dimension, + max_buffer_size: i32::MAX as u64, + max_non_sampler_bindings: u32::MAX, + + max_task_workgroup_total_count: 0, + max_task_workgroups_per_dimension: 0, + max_mesh_workgroup_total_count: 0, + max_mesh_workgroups_per_dimension: 0, + max_task_invocations_per_workgroup: 0, + max_task_invocations_per_dimension: 0, + max_mesh_invocations_per_workgroup: 0, + max_mesh_invocations_per_dimension: 0, + max_task_payload_size: 0, + max_mesh_output_vertices: 0, + max_mesh_output_primitives: 0, + max_mesh_output_layers: 0, + max_mesh_multiview_view_count: 0, + + max_blas_primitive_count: 0, + max_blas_geometry_count: 0, + max_tlas_instance_count: 0, + max_acceleration_structures_per_shader_stage: 0, + max_buffers_and_acceleration_structures_per_shader_stage: u32::MAX, + + max_multiview_view_count: 0, + + max_ray_dispatch_count: 0, + max_ray_recursion_depth: 0, + }); + + let mut workarounds = super::Workarounds::empty(); + + workarounds.set( + super::Workarounds::EMULATE_BUFFER_MAP, + cfg!(any(webgl, Emscripten)), + ); + + let r = renderer.to_lowercase(); + // Check for Mesa sRGB clear bug. See + // [`super::PrivateCapabilities::MESA_I915_SRGB_SHADER_CLEAR`]. + if context.is_owned() + && r.contains("mesa") + && r.contains("intel") + && r.split(&[' ', '(', ')'][..]) + .any(|substr| substr.len() == 3 && substr.chars().nth(2) == Some('l')) + { + log::debug!( + "Detected skylake derivative running on mesa i915. Clears to srgb textures will \ + use manual shader clears." + ); + workarounds.set(super::Workarounds::MESA_I915_SRGB_SHADER_CLEAR, true); + } + + let downlevel_defaults = wgt::DownlevelLimits {}; + let max_samples = unsafe { gl.get_parameter_i32(glow::MAX_SAMPLES) }; + + // Drop the GL guard so we can move the context into AdapterShared + // ( on Wasm the gl handle is just a ref so we tell clippy to allow + // dropping the ref ) + #[cfg_attr(target_arch = "wasm32", allow(dropping_references))] + drop(gl); + + Some(crate::ExposedAdapter { + adapter: super::Adapter { + shared: Arc::new(super::AdapterShared { + context, + private_caps, + workarounds, + features, + limits: limits.clone(), + options: backend_options, + shading_language_version, + next_shader_id: Default::default(), + program_cache: Default::default(), + es: es_ver.is_some(), + max_msaa_samples: max_samples, + }), + }, + info: Self::make_info(vendor, renderer, version), + features, + capabilities: crate::Capabilities { + limits, + downlevel: wgt::DownlevelCapabilities { + flags: downlevel_flags, + limits: downlevel_defaults, + shader_model: wgt::ShaderModel::Sm5, + }, + alignments: crate::Alignments { + buffer_copy_offset: wgt::BufferSize::new(4).unwrap(), + buffer_copy_pitch: wgt::BufferSize::new(4).unwrap(), + // #6151: `wgpu_hal::gles` doesn't ask Naga to inject bounds + // checks in GLSL, and it doesn't request extensions like + // `KHR_robust_buffer_access_behavior` that would provide + // them, so we can't really implement the checks promised by + // [`crate::BufferBinding`]. + // + // Since this is a pre-existing condition, for the time + // being, provide 1 as the value here, to cause as little + // trouble as possible. + uniform_bounds_check_alignment: wgt::BufferSize::new(1).unwrap(), + raw_tlas_instance_size: 0, + ray_tracing_scratch_buffer_alignment: 0, + ray_tracing_pipeline_group_data_size: 0, + ray_tracing_pipeline_group_data_alignment: 0, + ray_tracing_pipeline_data_offset_alignment: 0, + }, + cooperative_matrix_properties: Vec::new(), + }, + }) + } + + unsafe fn compile_shader( + source: &str, + gl: &glow::Context, + shader_type: u32, + es: bool, + ) -> Option<glow::Shader> { + let source = if es { + format!("#version 300 es\nprecision lowp float;\n{source}") + } else { + let version = gl.version(); + if version.major == 3 && version.minor == 0 { + // OpenGL 3.0 only supports this format + format!("#version 130\n{source}") + } else { + // OpenGL 3.1+ support this format + format!("#version 140\n{source}") + } + }; + let shader = unsafe { gl.create_shader(shader_type) }.expect("Could not create shader"); + unsafe { gl.shader_source(shader, &source) }; + unsafe { gl.compile_shader(shader) }; + + if !unsafe { gl.get_shader_compile_status(shader) } { + let msg = unsafe { gl.get_shader_info_log(shader) }; + if !msg.is_empty() { + log::error!("\tShader compile error: {msg}"); + } + unsafe { gl.delete_shader(shader) }; + None + } else { + Some(shader) + } + } + + unsafe fn create_shader_clear_program( + gl: &glow::Context, + es: bool, + ) -> Option<ShaderClearProgram> { + let program = unsafe { gl.create_program() }.expect("Could not create shader program"); + let vertex = unsafe { + Self::compile_shader( + include_str!("./shaders/clear.vert"), + gl, + glow::VERTEX_SHADER, + es, + )? + }; + let fragment = unsafe { + Self::compile_shader( + include_str!("./shaders/clear.frag"), + gl, + glow::FRAGMENT_SHADER, + es, + )? + }; + unsafe { gl.attach_shader(program, vertex) }; + unsafe { gl.attach_shader(program, fragment) }; + unsafe { gl.link_program(program) }; + + let linked_ok = unsafe { gl.get_program_link_status(program) }; + let msg = unsafe { gl.get_program_info_log(program) }; + if !msg.is_empty() { + log::error!("Shader link error: {msg}"); + } + if !linked_ok { + return None; + } + + let color_uniform_location = unsafe { gl.get_uniform_location(program, "color") } + .expect("Could not find color uniform in shader clear shader"); + unsafe { gl.delete_shader(vertex) }; + unsafe { gl.delete_shader(fragment) }; + + Some(ShaderClearProgram { + program, + color_uniform_location, + }) + } +} + +impl crate::Adapter for super::Adapter { + type A = super::Api; + + unsafe fn open( + &self, + features: wgt::Features, + _limits: &wgt::Limits, + _memory_hints: &wgt::MemoryHints, + ) -> Result<crate::OpenDevice<super::Api>, crate::DeviceError> { + let gl = &self.shared.context.lock(); + unsafe { gl.pixel_store_i32(glow::UNPACK_ALIGNMENT, 1) }; + unsafe { gl.pixel_store_i32(glow::PACK_ALIGNMENT, 1) }; + let main_vao = + unsafe { gl.create_vertex_array() }.map_err(|_| crate::DeviceError::OutOfMemory)?; + unsafe { gl.bind_vertex_array(Some(main_vao)) }; + + let zero_buffer = + unsafe { gl.create_buffer() }.map_err(|_| crate::DeviceError::OutOfMemory)?; + unsafe { gl.bind_buffer(glow::COPY_READ_BUFFER, Some(zero_buffer)) }; + let zeroes = vec![0u8; super::ZERO_BUFFER_SIZE]; + unsafe { gl.buffer_data_u8_slice(glow::COPY_READ_BUFFER, &zeroes, glow::STATIC_DRAW) }; + + // Compile the shader program we use for doing manual clears to work around Mesa fastclear + // bug. + + let shader_clear_program = if self + .shared + .workarounds + .contains(super::Workarounds::MESA_I915_SRGB_SHADER_CLEAR) + { + Some(unsafe { + Self::create_shader_clear_program(gl, self.shared.es) + .ok_or(crate::DeviceError::Lost)? + }) + } else { + // If we don't need the workaround, don't waste time and resources compiling the clear program + None + }; + + Ok(crate::OpenDevice { + device: super::Device { + shared: Arc::clone(&self.shared), + main_vao, + #[cfg(all(native, feature = "renderdoc"))] + render_doc: Default::default(), + counters: Default::default(), + }, + queue: super::Queue { + shared: Arc::clone(&self.shared), + features, + draw_fbo: unsafe { gl.create_framebuffer() } + .map_err(|_| crate::DeviceError::OutOfMemory)?, + copy_fbo: unsafe { gl.create_framebuffer() } + .map_err(|_| crate::DeviceError::OutOfMemory)?, + shader_clear_program, + zero_buffer, + temp_query_results: Mutex::new(Vec::new()), + draw_buffer_count: AtomicU8::new(1), + current_index_buffer: Mutex::new(None), + }, + }) + } + + unsafe fn texture_format_capabilities( + &self, + format: wgt::TextureFormat, + ) -> crate::TextureFormatCapabilities { + use crate::TextureFormatCapabilities as Tfc; + use wgt::TextureFormat as Tf; + + let sample_count = { + let max_samples = self.shared.max_msaa_samples; + if max_samples >= 16 { + Tfc::MULTISAMPLE_X2 + | Tfc::MULTISAMPLE_X4 + | Tfc::MULTISAMPLE_X8 + | Tfc::MULTISAMPLE_X16 + } else if max_samples >= 8 { + Tfc::MULTISAMPLE_X2 | Tfc::MULTISAMPLE_X4 | Tfc::MULTISAMPLE_X8 + } else { + // The lowest supported level in GLE3.0/WebGL2 is 4X + // (see GL_MAX_SAMPLES in https://registry.khronos.org/OpenGL-Refpages/es3.0/html/glGet.xhtml). + // On some platforms, like iOS Safari, `get_parameter_i32(MAX_SAMPLES)` returns 0, + // so we always fall back to supporting 4x here. + Tfc::MULTISAMPLE_X2 | Tfc::MULTISAMPLE_X4 + } + }; + + // Base types are pulled from the table in the OpenGLES 3.0 spec in section 3.8. + // + // The storage types are based on table 8.26, in section + // "TEXTURE IMAGE LOADS AND STORES" of OpenGLES-3.2 spec. + let empty = Tfc::empty(); + let base = Tfc::COPY_SRC | Tfc::COPY_DST; + let unfilterable = base | Tfc::SAMPLED; + let depth = base | Tfc::SAMPLED | sample_count | Tfc::DEPTH_STENCIL_ATTACHMENT; + let filterable = unfilterable | Tfc::SAMPLED_LINEAR; + let renderable = + unfilterable | Tfc::COLOR_ATTACHMENT | sample_count | Tfc::MULTISAMPLE_RESOLVE; + let filterable_renderable = filterable | renderable | Tfc::COLOR_ATTACHMENT_BLEND; + let storage = + base | Tfc::STORAGE_READ_WRITE | Tfc::STORAGE_READ_ONLY | Tfc::STORAGE_WRITE_ONLY; + + let feature_fn = |f, caps| { + if self.shared.features.contains(f) { + caps + } else { + empty + } + }; + + let bcn_features = feature_fn(wgt::Features::TEXTURE_COMPRESSION_BC, filterable); + let etc2_features = feature_fn(wgt::Features::TEXTURE_COMPRESSION_ETC2, filterable); + let astc_features = feature_fn(wgt::Features::TEXTURE_COMPRESSION_ASTC, filterable); + let astc_hdr_features = feature_fn(wgt::Features::TEXTURE_COMPRESSION_ASTC_HDR, filterable); + + let private_caps_fn = |f, caps| { + if self.shared.private_caps.contains(f) { + caps + } else { + empty + } + }; + + let half_float_renderable = private_caps_fn( + super::PrivateCapabilities::COLOR_BUFFER_HALF_FLOAT, + Tfc::COLOR_ATTACHMENT + | Tfc::COLOR_ATTACHMENT_BLEND + | sample_count + | Tfc::MULTISAMPLE_RESOLVE, + ); + + let float_renderable = private_caps_fn( + super::PrivateCapabilities::COLOR_BUFFER_FLOAT, + Tfc::COLOR_ATTACHMENT + | Tfc::COLOR_ATTACHMENT_BLEND + | sample_count + | Tfc::MULTISAMPLE_RESOLVE, + ); + + let texture_float_linear = feature_fn(wgt::Features::FLOAT32_FILTERABLE, filterable); + + let image_atomic = feature_fn(wgt::Features::TEXTURE_ATOMIC, Tfc::STORAGE_ATOMIC); + let image_64_atomic = feature_fn(wgt::Features::TEXTURE_INT64_ATOMIC, Tfc::STORAGE_ATOMIC); + + // UNORM gets full filterable+renderable; SNORM splits because + // `EXT_texture_norm16` marks only UNORM as color-renderable. + // Storage rides on a separate cap (desktop GL >= 4.2 core / pre-4.2 + // `GL_ARB_shader_image_load_store`, GLES `GL_NV_image_formats`). + let norm16_unorm = private_caps_fn( + super::PrivateCapabilities::TEXTURE_FORMAT_NORM16, + filterable_renderable, + ); + let norm16_snorm = if self + .shared + .private_caps + .contains(super::PrivateCapabilities::TEXTURE_FORMAT_SNORM16_RENDERABLE) + { + norm16_unorm + } else { + private_caps_fn( + super::PrivateCapabilities::TEXTURE_FORMAT_NORM16, + filterable, + ) + }; + let norm16_storage = private_caps_fn( + super::PrivateCapabilities::TEXTURE_FORMAT_NORM16_STORAGE, + storage, + ); + + match format { + Tf::R8Unorm => filterable_renderable, + Tf::R8Snorm => filterable, + Tf::R8Uint => renderable, + Tf::R8Sint => renderable, + Tf::R16Uint => renderable, + Tf::R16Sint => renderable, + Tf::R16Unorm => norm16_unorm | norm16_storage, + Tf::R16Snorm => norm16_snorm | norm16_storage, + Tf::R16Float => filterable | half_float_renderable, + Tf::Rg8Unorm => filterable_renderable, + Tf::Rg8Snorm => filterable, + Tf::Rg8Uint => renderable, + Tf::Rg8Sint => renderable, + Tf::R32Uint => renderable | storage | image_atomic, + Tf::R32Sint => renderable | storage | image_atomic, + Tf::R32Float => unfilterable | storage | float_renderable | texture_float_linear, + Tf::Rg16Uint => renderable, + Tf::Rg16Sint => renderable, + Tf::Rg16Unorm => norm16_unorm | norm16_storage, + Tf::Rg16Snorm => norm16_snorm | norm16_storage, + Tf::Rg16Float => filterable | half_float_renderable, + Tf::Rgba8Unorm => filterable_renderable | storage, + Tf::Rgba8UnormSrgb => filterable_renderable, + Tf::Bgra8Unorm | Tf::Bgra8UnormSrgb => filterable_renderable, + Tf::Rgba8Snorm => filterable | storage, + Tf::Rgba8Uint => renderable | storage, + Tf::Rgba8Sint => renderable | storage, + Tf::Rgb10a2Uint => renderable, + Tf::Rgb10a2Unorm => filterable_renderable, + Tf::Rg11b10Ufloat => filterable | float_renderable, + Tf::R64Uint => image_64_atomic, + Tf::Rg32Uint => renderable, + Tf::Rg32Sint => renderable, + Tf::Rg32Float => unfilterable | float_renderable | texture_float_linear, + Tf::Rgba16Uint => renderable | storage, + Tf::Rgba16Sint => renderable | storage, + Tf::Rgba16Unorm => norm16_unorm | norm16_storage, + Tf::Rgba16Snorm => norm16_snorm | norm16_storage, + Tf::Rgba16Float => filterable | storage | half_float_renderable, + Tf::Rgba32Uint => renderable | storage, + Tf::Rgba32Sint => renderable | storage, + Tf::Rgba32Float => unfilterable | storage | float_renderable | texture_float_linear, + Tf::Stencil8 + | Tf::Depth16Unorm + | Tf::Depth32Float + | Tf::Depth32FloatStencil8 + | Tf::Depth24Plus + | Tf::Depth24PlusStencil8 => depth, + Tf::NV12 => empty, + Tf::P010 => empty, + Tf::Rgb9e5Ufloat => filterable, + Tf::Bc1RgbaUnorm + | Tf::Bc1RgbaUnormSrgb + | Tf::Bc2RgbaUnorm + | Tf::Bc2RgbaUnormSrgb + | Tf::Bc3RgbaUnorm + | Tf::Bc3RgbaUnormSrgb + | Tf::Bc4RUnorm + | Tf::Bc4RSnorm + | Tf::Bc5RgUnorm + | Tf::Bc5RgSnorm + | Tf::Bc6hRgbFloat + | Tf::Bc6hRgbUfloat + | Tf::Bc7RgbaUnorm + | Tf::Bc7RgbaUnormSrgb => bcn_features, + Tf::Etc2Rgb8Unorm + | Tf::Etc2Rgb8UnormSrgb + | Tf::Etc2Rgb8A1Unorm + | Tf::Etc2Rgb8A1UnormSrgb + | Tf::Etc2Rgba8Unorm + | Tf::Etc2Rgba8UnormSrgb + | Tf::EacR11Unorm + | Tf::EacR11Snorm + | Tf::EacRg11Unorm + | Tf::EacRg11Snorm => etc2_features, + Tf::Astc { + block: _, + channel: AstcChannel::Unorm | AstcChannel::UnormSrgb, + } => astc_features, + Tf::Astc { + block: _, + channel: AstcChannel::Hdr, + } => astc_hdr_features, + } + } + + unsafe fn surface_capabilities( + &self, + surface: &super::Surface, + ) -> Option<crate::SurfaceCapabilities> { + #[cfg(webgl)] + if self.shared.context.webgl2_context != surface.webgl2_context { + return None; + } + + if surface.presentable { + // There is no extended-range or wide-gamut path in the GLES + // backend; everything is presented as sRGB. + let format_caps = |format: wgt::TextureFormat| wgt::SurfaceFormatCapabilities { + format, + color_spaces: wgt::SurfaceColorSpaces::SRGB, + }; + let mut formats = vec![ + format_caps(wgt::TextureFormat::Rgba8Unorm), + #[cfg(native)] + format_caps(wgt::TextureFormat::Bgra8Unorm), + ]; + if surface.supports_srgb() { + formats.extend([ + format_caps(wgt::TextureFormat::Rgba8UnormSrgb), + #[cfg(native)] + format_caps(wgt::TextureFormat::Bgra8UnormSrgb), + ]) + } + if self + .shared + .private_caps + .contains(super::PrivateCapabilities::COLOR_BUFFER_HALF_FLOAT) + { + formats.push(format_caps(wgt::TextureFormat::Rgba16Float)) + } + + Some(crate::SurfaceCapabilities { + formats, + present_modes: if cfg!(windows) { + vec![wgt::PresentMode::Fifo, wgt::PresentMode::Immediate] + } else { + vec![wgt::PresentMode::Fifo] //TODO + }, + composite_alpha_modes: vec![wgt::CompositeAlphaMode::Opaque], //TODO + maximum_frame_latency: 2..=2, //TODO, unused currently + current_extent: None, + usage: wgt::TextureUses::COLOR_TARGET, + }) + } else { + None + } + } + + unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp { + wgt::PresentationTimestamp::INVALID_TIMESTAMP + } + + fn get_ordered_buffer_usages(&self) -> wgt::BufferUses { + wgt::BufferUses::INCLUSIVE | wgt::BufferUses::MAP_WRITE + } + + // Don't put barriers between inclusive uses + fn get_ordered_texture_usages(&self) -> wgt::TextureUses { + wgt::TextureUses::INCLUSIVE + | wgt::TextureUses::COLOR_TARGET + | wgt::TextureUses::DEPTH_STENCIL_WRITE + } +} + +impl super::AdapterShared { + pub(super) unsafe fn get_buffer_sub_data( + &self, + gl: &glow::Context, + target: u32, + offset: i32, + dst_data: &mut [u8], + ) { + if self + .private_caps + .contains(super::PrivateCapabilities::GET_BUFFER_SUB_DATA) + { + unsafe { gl.get_buffer_sub_data(target, offset, dst_data) }; + } else { + log::error!("Fake map"); + let length = dst_data.len(); + // glMapBufferRange throws an error if length is 0. + if length != 0 { + let buffer_mapping = + unsafe { gl.map_buffer_range(target, offset, length as _, glow::MAP_READ_BIT) }; + + unsafe { + core::ptr::copy_nonoverlapping(buffer_mapping, dst_data.as_mut_ptr(), length) + }; + + unsafe { gl.unmap_buffer(target) }; + } + } + } +} + +#[cfg(send_sync)] +unsafe impl Sync for super::Adapter {} +#[cfg(send_sync)] +unsafe impl Send for super::Adapter {} + +#[cfg(test)] +mod tests { + use super::super::Adapter; + + #[test] + fn test_version_parse() { + Adapter::parse_version("1").unwrap_err(); + Adapter::parse_version("1.").unwrap_err(); + Adapter::parse_version("1 h3l1o. W0rld").unwrap_err(); + Adapter::parse_version("1. h3l1o. W0rld").unwrap_err(); + Adapter::parse_version("1.2.3").unwrap_err(); + + assert_eq!(Adapter::parse_version("OpenGL ES 3.1").unwrap(), (3, 1)); + assert_eq!( + Adapter::parse_version("OpenGL ES 2.0 Google Nexus").unwrap(), + (2, 0) + ); + assert_eq!(Adapter::parse_version("GLSL ES 1.1").unwrap(), (1, 1)); + assert_eq!( + Adapter::parse_version("OpenGL ES GLSL ES 3.20").unwrap(), + (3, 2) + ); + assert_eq!( + // WebGL 2.0 should parse as OpenGL ES 3.0 + Adapter::parse_version("WebGL 2.0 (OpenGL ES 3.0 Chromium)").unwrap(), + (3, 0) + ); + assert_eq!( + Adapter::parse_version("WebGL GLSL ES 3.00 (OpenGL ES GLSL ES 3.0 Chromium)").unwrap(), + (3, 0) + ); + } +} diff --git a/third_party/wgpu-hal/src/gles/command.rs b/third_party/wgpu-hal/src/gles/command.rs new file mode 100644 index 000000000..427551cec --- /dev/null +++ b/third_party/wgpu-hal/src/gles/command.rs @@ -0,0 +1,1345 @@ +use alloc::string::String; +use core::{mem, ops::Range}; + +use arrayvec::ArrayVec; + +use super::{conv, Command as C}; + +#[derive(Clone, Copy, Debug, Default)] +struct TextureSlotDesc { + tex_target: super::BindTarget, + sampler_index: Option<u8>, +} + +pub(super) struct State { + topology: u32, + primitive: super::PrimitiveState, + index_format: wgt::IndexFormat, + index_offset: wgt::BufferAddress, + vertex_buffers: [( + Option<super::VertexBufferDesc>, + Option<super::BufferBinding>, + ); crate::MAX_VERTEX_BUFFERS], + vertex_attributes: ArrayVec<super::AttributeDesc, { super::MAX_VERTEX_ATTRIBUTES }>, + color_targets: ArrayVec<super::ColorTargetDesc, { crate::MAX_COLOR_ATTACHMENTS }>, + stencil: super::StencilState, + depth_bias: wgt::DepthBiasState, + alpha_to_coverage_enabled: bool, + samplers: [Option<glow::Sampler>; super::MAX_SAMPLERS], + texture_slots: [TextureSlotDesc; super::MAX_TEXTURE_SLOTS], + render_size: wgt::Extent3d, + resolve_attachments: ArrayVec<(u32, super::TextureView), { crate::MAX_COLOR_ATTACHMENTS }>, + invalidate_attachments: ArrayVec<u32, { crate::MAX_COLOR_ATTACHMENTS + 2 }>, + has_pass_label: bool, + instance_vbuf_mask: usize, + dirty_vbuf_mask: usize, + active_first_instance: u32, + first_instance_location: Option<glow::UniformLocation>, + immediates_descs: ArrayVec<super::ImmediateDesc, { super::MAX_IMMEDIATES_COMMANDS }>, + // The current state of the immediate data block. + current_immediates_data: [u32; super::MAX_IMMEDIATES], + end_of_pass_timestamp: Option<glow::Query>, + clip_distance_count: u32, +} + +impl Default for State { + fn default() -> Self { + Self { + topology: Default::default(), + primitive: Default::default(), + index_format: Default::default(), + index_offset: Default::default(), + vertex_buffers: Default::default(), + vertex_attributes: Default::default(), + color_targets: Default::default(), + stencil: Default::default(), + depth_bias: Default::default(), + alpha_to_coverage_enabled: Default::default(), + samplers: Default::default(), + texture_slots: Default::default(), + render_size: Default::default(), + resolve_attachments: Default::default(), + invalidate_attachments: Default::default(), + has_pass_label: Default::default(), + instance_vbuf_mask: Default::default(), + dirty_vbuf_mask: Default::default(), + active_first_instance: Default::default(), + first_instance_location: Default::default(), + immediates_descs: Default::default(), + current_immediates_data: [0; super::MAX_IMMEDIATES], + end_of_pass_timestamp: Default::default(), + clip_distance_count: Default::default(), + } + } +} + +impl super::CommandBuffer { + fn clear(&mut self) { + self.label = None; + self.commands.clear(); + self.data_bytes.clear(); + self.queries.clear(); + } + + fn add_marker(&mut self, marker: &str) -> Range<u32> { + let start = self.data_bytes.len() as u32; + self.data_bytes.extend(marker.as_bytes()); + start..self.data_bytes.len() as u32 + } + + fn add_immediates_data(&mut self, data: &[u32]) -> Range<u32> { + let data_raw = bytemuck::cast_slice(data); + let start = self.data_bytes.len(); + assert!(start < u32::MAX as usize); + self.data_bytes.extend_from_slice(data_raw); + let end = self.data_bytes.len(); + assert!(end < u32::MAX as usize); + (start as u32)..(end as u32) + } +} + +impl Drop for super::CommandEncoder { + fn drop(&mut self) { + use crate::CommandEncoder; + unsafe { self.discard_encoding() } + self.counters.command_encoders.sub(1); + } +} + +impl super::CommandEncoder { + fn rebind_stencil_func(&mut self) { + fn make(s: &super::StencilSide, face: u32) -> C { + C::SetStencilFunc { + face, + function: s.function, + reference: s.reference, + read_mask: s.mask_read, + } + } + + let s = &self.state.stencil; + if s.front.function == s.back.function + && s.front.mask_read == s.back.mask_read + && s.front.reference == s.back.reference + { + self.cmd_buffer + .commands + .push(make(&s.front, glow::FRONT_AND_BACK)); + } else { + self.cmd_buffer.commands.push(make(&s.front, glow::FRONT)); + self.cmd_buffer.commands.push(make(&s.back, glow::BACK)); + } + } + + fn rebind_vertex_data(&mut self, first_instance: u32) { + if self + .private_caps + .contains(super::PrivateCapabilities::VERTEX_BUFFER_LAYOUT) + { + for (index, pair) in self.state.vertex_buffers.iter().enumerate() { + if self.state.dirty_vbuf_mask & (1 << index) == 0 { + continue; + } + let (buffer_desc, vb) = match *pair { + (Some(ref vb_desc), Some(ref vb)) => (vb_desc.clone(), vb), + // Not all dirty bindings are necessarily filled. Some may be unused. + (_, _) => continue, + }; + let instance_offset = match buffer_desc.step { + wgt::VertexStepMode::Vertex => 0, + wgt::VertexStepMode::Instance => first_instance * buffer_desc.stride, + }; + + self.cmd_buffer.commands.push(C::SetVertexBuffer { + index: index as u32, + buffer: super::BufferBinding { + raw: vb.raw, + offset: vb.offset + instance_offset as wgt::BufferAddress, + }, + buffer_desc, + }); + self.state.dirty_vbuf_mask ^= 1 << index; + } + } else { + let mut vbuf_mask = 0; + for attribute in self.state.vertex_attributes.iter() { + if self.state.dirty_vbuf_mask & (1 << attribute.buffer_index) == 0 { + continue; + } + let (buffer_desc, vb) = + match self.state.vertex_buffers[attribute.buffer_index as usize] { + (Some(ref vb_desc), Some(ref vb)) => (vb_desc.clone(), vb), + // Not all dirty bindings are necessarily filled. Some may be unused. + (_, _) => continue, + }; + + let mut attribute_desc = attribute.clone(); + attribute_desc.offset += vb.offset as u32; + if buffer_desc.step == wgt::VertexStepMode::Instance { + attribute_desc.offset += buffer_desc.stride * first_instance; + } + + self.cmd_buffer.commands.push(C::SetVertexAttribute { + buffer: Some(vb.raw), + buffer_desc, + attribute_desc, + }); + vbuf_mask |= 1 << attribute.buffer_index; + } + self.state.dirty_vbuf_mask ^= vbuf_mask; + } + } + + fn rebind_sampler_states(&mut self, dirty_textures: u32, dirty_samplers: u32) { + for (texture_index, slot) in self.state.texture_slots.iter().enumerate() { + if dirty_textures & (1 << texture_index) != 0 + || slot + .sampler_index + .is_some_and(|si| dirty_samplers & (1 << si) != 0) + { + let sampler = slot + .sampler_index + .and_then(|si| self.state.samplers[si as usize]); + self.cmd_buffer + .commands + .push(C::BindSampler(texture_index as u32, sampler)); + } + } + } + + fn prepare_draw(&mut self, first_instance: u32) { + // If we support fully featured instancing, we want to bind everything as normal + // and let the draw call sort it out. + let emulated_first_instance_value = if self + .private_caps + .contains(super::PrivateCapabilities::FULLY_FEATURED_INSTANCING) + { + 0 + } else { + first_instance + }; + + if emulated_first_instance_value != self.state.active_first_instance { + // rebind all per-instance buffers on first-instance change + self.state.dirty_vbuf_mask |= self.state.instance_vbuf_mask; + self.state.active_first_instance = emulated_first_instance_value; + } + if self.state.dirty_vbuf_mask != 0 { + self.rebind_vertex_data(emulated_first_instance_value); + } + } + + fn set_pipeline_inner(&mut self, inner: &super::PipelineInner) { + self.cmd_buffer.commands.push(C::SetProgram(inner.program)); + + self.state + .first_instance_location + .clone_from(&inner.first_instance_location); + self.state + .immediates_descs + .clone_from(&inner.immediates_descs); + + // rebind textures, if needed + let mut dirty_textures = 0u32; + for (texture_index, (slot, &sampler_index)) in self + .state + .texture_slots + .iter_mut() + .zip(inner.sampler_map.iter()) + .enumerate() + { + if slot.sampler_index != sampler_index { + slot.sampler_index = sampler_index; + dirty_textures |= 1 << texture_index; + } + } + if dirty_textures != 0 { + self.rebind_sampler_states(dirty_textures, 0); + } + } +} + +impl crate::CommandEncoder for super::CommandEncoder { + type A = super::Api; + + unsafe fn begin_encoding(&mut self, label: crate::Label) -> Result<(), crate::DeviceError> { + self.state = State::default(); + self.cmd_buffer.label = label.map(String::from); + Ok(()) + } + unsafe fn discard_encoding(&mut self) { + self.cmd_buffer.clear(); + } + unsafe fn end_encoding(&mut self) -> Result<super::CommandBuffer, crate::DeviceError> { + Ok(mem::take(&mut self.cmd_buffer)) + } + unsafe fn reset_all<I>(&mut self, _command_buffers: I) { + //TODO: could re-use the allocations in all these command buffers + } + + unsafe fn transition_buffers<'a, T>(&mut self, barriers: T) + where + T: Iterator<Item = crate::BufferBarrier<'a, super::Buffer>>, + { + if !self + .private_caps + .contains(super::PrivateCapabilities::MEMORY_BARRIERS) + { + return; + } + for bar in barriers { + // GLES only synchronizes storage -> anything explicitly + if !bar.usage.from.contains(wgt::BufferUses::STORAGE_READ_WRITE) { + continue; + } + self.cmd_buffer + .commands + .push(C::BufferBarrier(bar.buffer.raw.unwrap(), bar.usage.to)); + } + } + + unsafe fn transition_textures<'a, T>(&mut self, barriers: T) + where + T: Iterator<Item = crate::TextureBarrier<'a, super::Texture>>, + { + if !self + .private_caps + .contains(super::PrivateCapabilities::MEMORY_BARRIERS) + { + return; + } + + let mut combined_usage = wgt::TextureUses::empty(); + for bar in barriers { + // GLES only synchronizes storage -> anything explicitly + // if shader writes to a texture then barriers should be placed + if !bar.usage.from.intersects( + wgt::TextureUses::STORAGE_READ_WRITE | wgt::TextureUses::STORAGE_WRITE_ONLY, + ) { + continue; + } + // unlike buffers, there is no need for a concrete texture + // object to be bound anywhere for a barrier + combined_usage |= bar.usage.to; + } + + if !combined_usage.is_empty() { + self.cmd_buffer + .commands + .push(C::TextureBarrier(combined_usage)); + } + } + + unsafe fn clear_buffer(&mut self, buffer: &super::Buffer, range: crate::MemoryRange) { + self.cmd_buffer.commands.push(C::ClearBuffer { + dst: buffer.clone(), + dst_target: buffer.target, + range, + }); + } + + unsafe fn copy_buffer_to_buffer<T>( + &mut self, + src: &super::Buffer, + dst: &super::Buffer, + regions: T, + ) where + T: Iterator<Item = crate::BufferCopy>, + { + let (src_target, dst_target) = if src.target == dst.target { + (glow::COPY_READ_BUFFER, glow::COPY_WRITE_BUFFER) + } else { + (src.target, dst.target) + }; + for copy in regions { + self.cmd_buffer.commands.push(C::CopyBufferToBuffer { + src: src.clone(), + src_target, + dst: dst.clone(), + dst_target, + copy, + }) + } + } + + #[cfg(webgl)] + unsafe fn copy_external_image_to_texture<T>( + &mut self, + src: &wgt::CopyExternalImageSourceInfo, + dst: &super::Texture, + dst_premultiplication: bool, + regions: T, + ) where + T: Iterator<Item = crate::TextureCopy>, + { + let (dst_raw, dst_target) = dst.inner.as_native(); + for copy in regions { + self.cmd_buffer + .commands + .push(C::CopyExternalImageToTexture { + src: src.clone(), + dst: dst_raw, + dst_target, + dst_format: dst.format, + dst_premultiplication, + copy, + }) + } + } + + unsafe fn copy_texture_to_texture<T>( + &mut self, + src: &super::Texture, + _src_usage: wgt::TextureUses, + dst: &super::Texture, + regions: T, + ) where + T: Iterator<Item = crate::TextureCopy>, + { + let (src_raw, src_target) = src.inner.as_native(); + let (dst_raw, dst_target) = dst.inner.as_native(); + for mut copy in regions { + copy.clamp_size_to_virtual(&src.copy_size, &dst.copy_size); + self.cmd_buffer.commands.push(C::CopyTextureToTexture { + src: src_raw, + src_target, + dst: dst_raw, + dst_target, + copy, + }) + } + } + + unsafe fn copy_buffer_to_texture<T>( + &mut self, + src: &super::Buffer, + dst: &super::Texture, + regions: T, + ) where + T: Iterator<Item = crate::BufferTextureCopy>, + { + let (dst_raw, dst_target) = dst.inner.as_native(); + + for mut copy in regions { + copy.clamp_size_to_virtual(&dst.copy_size); + self.cmd_buffer.commands.push(C::CopyBufferToTexture { + src: src.clone(), + src_target: src.target, + dst: dst_raw, + dst_target, + dst_format: dst.format, + copy, + }) + } + } + + unsafe fn copy_texture_to_buffer<T>( + &mut self, + src: &super::Texture, + _src_usage: wgt::TextureUses, + dst: &super::Buffer, + regions: T, + ) where + T: Iterator<Item = crate::BufferTextureCopy>, + { + let (src_raw, src_target) = src.inner.as_native(); + for mut copy in regions { + copy.clamp_size_to_virtual(&src.copy_size); + self.cmd_buffer.commands.push(C::CopyTextureToBuffer { + src: src_raw, + src_target, + src_format: src.format, + dst: dst.clone(), + dst_target: dst.target, + copy, + }) + } + } + + unsafe fn begin_query(&mut self, set: &super::QuerySet, index: u32) { + let query = set.queries[index as usize]; + self.cmd_buffer + .commands + .push(C::BeginQuery(query, set.target)); + } + unsafe fn end_query(&mut self, set: &super::QuerySet, _index: u32) { + self.cmd_buffer.commands.push(C::EndQuery(set.target)); + } + unsafe fn write_timestamp(&mut self, set: &super::QuerySet, index: u32) { + let query = set.queries[index as usize]; + self.cmd_buffer.commands.push(C::TimestampQuery(query)); + } + unsafe fn reset_queries(&mut self, _set: &super::QuerySet, _range: Range<u32>) { + //TODO: what do we do here? + } + unsafe fn copy_query_results( + &mut self, + set: &super::QuerySet, + range: Range<u32>, + buffer: &super::Buffer, + offset: wgt::BufferAddress, + _stride: wgt::BufferSize, + ) { + let start = self.cmd_buffer.queries.len(); + self.cmd_buffer + .queries + .extend_from_slice(&set.queries[range.start as usize..range.end as usize]); + let query_range = start as u32..self.cmd_buffer.queries.len() as u32; + self.cmd_buffer.commands.push(C::CopyQueryResults { + query_range, + dst: buffer.clone(), + dst_target: buffer.target, + dst_offset: offset, + }); + } + + // render + + unsafe fn begin_render_pass( + &mut self, + desc: &crate::RenderPassDescriptor<super::QuerySet, super::TextureView>, + ) -> Result<(), crate::DeviceError> { + debug_assert!(self.state.end_of_pass_timestamp.is_none()); + if let Some(ref t) = desc.timestamp_writes { + if let Some(index) = t.beginning_of_pass_write_index { + unsafe { self.write_timestamp(t.query_set, index) } + } + self.state.end_of_pass_timestamp = t + .end_of_pass_write_index + .map(|index| t.query_set.queries[index as usize]); + } + + self.state.render_size = desc.extent; + self.state.resolve_attachments.clear(); + self.state.invalidate_attachments.clear(); + if let Some(label) = desc.label { + let range = self.cmd_buffer.add_marker(label); + self.cmd_buffer.commands.push(C::PushDebugGroup(range)); + self.state.has_pass_label = true; + } + + let rendering_to_external_framebuffer = desc + .color_attachments + .iter() + .filter_map(|at| at.as_ref()) + .any(|at| match at.target.view.inner { + #[cfg(webgl)] + super::TextureInner::ExternalFramebuffer { .. } => true, + #[cfg(native)] + super::TextureInner::ExternalNativeFramebuffer { .. } => true, + _ => false, + }); + + if rendering_to_external_framebuffer && desc.color_attachments.len() != 1 { + panic!("Multiple render attachments with external framebuffers are not supported."); + } + + // `COLOR_ATTACHMENT0` to `COLOR_ATTACHMENT31` gives 32 possible color attachments. + assert!(desc.color_attachments.len() <= 32); + + match desc + .color_attachments + .first() + .filter(|at| at.is_some()) + .and_then(|at| at.as_ref().map(|at| &at.target.view.inner)) + { + // default framebuffer (provided externally) + Some(&super::TextureInner::DefaultRenderbuffer) => { + self.cmd_buffer + .commands + .push(C::ResetFramebuffer { is_default: true }); + } + _ => { + // set the framebuffer + self.cmd_buffer + .commands + .push(C::ResetFramebuffer { is_default: false }); + + for (i, cat) in desc.color_attachments.iter().enumerate() { + if let Some(cat) = cat.as_ref() { + let attachment = glow::COLOR_ATTACHMENT0 + i as u32; + // Try to use the multisampled render-to-texture extension to avoid resolving + if let Some(ref rat) = cat.resolve_target { + if matches!(rat.view.inner, super::TextureInner::Texture { .. }) + && self.private_caps.contains( + super::PrivateCapabilities::MULTISAMPLED_RENDER_TO_TEXTURE, + ) + && !cat.ops.contains(crate::AttachmentOps::STORE) + // Extension specifies that only COLOR_ATTACHMENT0 is valid + && i == 0 + { + self.cmd_buffer.commands.push(C::BindAttachment { + attachment, + view: rat.view.clone(), + depth_slice: None, + sample_count: desc.sample_count, + }); + continue; + } + } + self.cmd_buffer.commands.push(C::BindAttachment { + attachment, + view: cat.target.view.clone(), + depth_slice: cat.depth_slice, + sample_count: 1, + }); + if let Some(ref rat) = cat.resolve_target { + self.state + .resolve_attachments + .push((attachment, rat.view.clone())); + } + if cat.ops.contains(crate::AttachmentOps::STORE_DISCARD) { + self.state.invalidate_attachments.push(attachment); + } + } + } + if let Some(ref dsat) = desc.depth_stencil_attachment { + let aspects = dsat.target.view.aspects; + let attachment = match aspects { + crate::FormatAspects::DEPTH => glow::DEPTH_ATTACHMENT, + crate::FormatAspects::STENCIL => glow::STENCIL_ATTACHMENT, + _ => glow::DEPTH_STENCIL_ATTACHMENT, + }; + self.cmd_buffer.commands.push(C::BindAttachment { + attachment, + view: dsat.target.view.clone(), + depth_slice: None, + sample_count: 1, + }); + if aspects.contains(crate::FormatAspects::DEPTH) + && dsat.depth_ops.contains(crate::AttachmentOps::STORE_DISCARD) + { + self.state + .invalidate_attachments + .push(glow::DEPTH_ATTACHMENT); + } + if aspects.contains(crate::FormatAspects::STENCIL) + && dsat + .stencil_ops + .contains(crate::AttachmentOps::STORE_DISCARD) + { + self.state + .invalidate_attachments + .push(glow::STENCIL_ATTACHMENT); + } + } + } + } + + let rect = crate::Rect { + x: 0, + y: 0, + w: desc.extent.width as i32, + h: desc.extent.height as i32, + }; + self.cmd_buffer.commands.push(C::SetScissor(rect.clone())); + self.cmd_buffer.commands.push(C::SetViewport { + rect, + depth: 0.0..1.0, + }); + + if !rendering_to_external_framebuffer { + // set the draw buffers and states + self.cmd_buffer + .commands + .push(C::SetDrawColorBuffers(desc.color_attachments.len() as u8)); + } + + // issue the clears + for (i, cat) in desc + .color_attachments + .iter() + .filter_map(|at| at.as_ref()) + .enumerate() + { + if cat.ops.contains(crate::AttachmentOps::LOAD_CLEAR) { + let c = &cat.clear_value; + self.cmd_buffer.commands.push( + match cat.target.view.format.sample_type(None, None).unwrap() { + wgt::TextureSampleType::Float { .. } => C::ClearColorF { + draw_buffer: i as u32, + color: [c.r as f32, c.g as f32, c.b as f32, c.a as f32], + is_srgb: cat.target.view.format.is_srgb(), + }, + wgt::TextureSampleType::Uint => C::ClearColorU( + i as u32, + [c.r as u32, c.g as u32, c.b as u32, c.a as u32], + ), + wgt::TextureSampleType::Sint => C::ClearColorI( + i as u32, + [c.r as i32, c.g as i32, c.b as i32, c.a as i32], + ), + wgt::TextureSampleType::Depth => unreachable!(), + }, + ); + } + } + + if let Some(ref dsat) = desc.depth_stencil_attachment { + let clear_depth = dsat.depth_ops.contains(crate::AttachmentOps::LOAD_CLEAR); + let clear_stencil = dsat.stencil_ops.contains(crate::AttachmentOps::LOAD_CLEAR); + + if clear_depth && clear_stencil { + self.cmd_buffer.commands.push(C::ClearDepthAndStencil( + dsat.clear_value.0, + dsat.clear_value.1, + )); + } else if clear_depth { + self.cmd_buffer + .commands + .push(C::ClearDepth(dsat.clear_value.0)); + } else if clear_stencil { + self.cmd_buffer + .commands + .push(C::ClearStencil(dsat.clear_value.1)); + } + } + Ok(()) + } + unsafe fn end_render_pass(&mut self) { + for (attachment, dst) in self.state.resolve_attachments.drain(..) { + self.cmd_buffer.commands.push(C::ResolveAttachment { + attachment, + dst, + size: self.state.render_size, + }); + } + if !self.state.invalidate_attachments.is_empty() { + self.cmd_buffer.commands.push(C::InvalidateAttachments( + self.state.invalidate_attachments.clone(), + )); + self.state.invalidate_attachments.clear(); + } + if self.state.has_pass_label { + self.cmd_buffer.commands.push(C::PopDebugGroup); + self.state.has_pass_label = false; + } + self.state.instance_vbuf_mask = 0; + self.state.dirty_vbuf_mask = 0; + self.state.active_first_instance = 0; + self.state.color_targets.clear(); + for vat in &self.state.vertex_attributes { + self.cmd_buffer + .commands + .push(C::UnsetVertexAttribute(vat.location)); + } + self.state.vertex_attributes.clear(); + self.state.primitive = super::PrimitiveState::default(); + + if let Some(query) = self.state.end_of_pass_timestamp.take() { + self.cmd_buffer.commands.push(C::TimestampQuery(query)); + } + } + + unsafe fn set_bind_group( + &mut self, + layout: &super::PipelineLayout, + index: u32, + group: &super::BindGroup, + dynamic_offsets: &[wgt::DynamicOffset], + ) { + let mut do_index = 0; + let mut dirty_textures = 0u32; + let mut dirty_samplers = 0u32; + let group_info = layout.group_infos[index as usize].as_ref().unwrap(); + + for (binding_layout, raw_binding) in group_info.entries.iter().zip(group.contents.iter()) { + let slot = group_info.binding_to_slot[binding_layout.binding as usize] as u32; + match *raw_binding { + super::RawBinding::Buffer { + raw, + offset: base_offset, + size, + } => { + let mut offset = base_offset; + let target = match binding_layout.ty { + wgt::BindingType::Buffer { + ty, + has_dynamic_offset, + min_binding_size: _, + } => { + if has_dynamic_offset { + offset += dynamic_offsets[do_index] as i32; + do_index += 1; + } + match ty { + wgt::BufferBindingType::Uniform => glow::UNIFORM_BUFFER, + wgt::BufferBindingType::Storage { .. } => { + glow::SHADER_STORAGE_BUFFER + } + } + } + _ => unreachable!(), + }; + self.cmd_buffer.commands.push(C::BindBuffer { + target, + slot, + buffer: raw, + offset, + size, + }); + } + super::RawBinding::Sampler(sampler) => { + dirty_samplers |= 1 << slot; + self.state.samplers[slot as usize] = Some(sampler); + } + super::RawBinding::Texture { + raw, + target, + aspects, + ref mip_levels, + } => { + dirty_textures |= 1 << slot; + self.state.texture_slots[slot as usize].tex_target = target; + self.cmd_buffer.commands.push(C::BindTexture { + slot, + texture: raw, + target, + aspects, + mip_levels: mip_levels.clone(), + }); + } + super::RawBinding::Image(ref binding) => { + self.cmd_buffer.commands.push(C::BindImage { + slot, + binding: binding.clone(), + }); + } + } + } + + self.rebind_sampler_states(dirty_textures, dirty_samplers); + } + + unsafe fn set_immediates( + &mut self, + _layout: &super::PipelineLayout, + offset_bytes: u32, + data: &[u32], + ) { + // There is nothing preventing the user from trying to update a single value within + // a vector or matrix in the set_immediates call, as to the user, all of this is + // just memory. However OpenGL does not allow partial uniform updates. + // + // As such, we locally keep a copy of the current state of the immediate data memory + // block. If the user tries to update a single value, we have the data to update the entirety + // of the uniform. + let start_words = offset_bytes / 4; + let end_words = start_words + data.len() as u32; + self.state.current_immediates_data[start_words as usize..end_words as usize] + .copy_from_slice(data); + + // We iterate over the uniform list as there may be multiple uniforms that need + // updating from the same immediate data memory (one for each shader stage). + // + // Additionally, any statically unused uniform descs will have been removed from this list + // by OpenGL, so the uniform list is not contiguous. + for uniform in self.state.immediates_descs.iter().cloned() { + let uniform_size_words = uniform.size_bytes / 4; + let uniform_start_words = uniform.offset / 4; + let uniform_end_words = uniform_start_words + uniform_size_words; + + // Is true if any word within the uniform binding was updated + let needs_updating = + start_words < uniform_end_words || uniform_start_words <= end_words; + + if needs_updating { + let uniform_data = &self.state.current_immediates_data + [uniform_start_words as usize..uniform_end_words as usize]; + + let range = self.cmd_buffer.add_immediates_data(uniform_data); + + self.cmd_buffer.commands.push(C::SetImmediates { + uniform, + offset: range.start, + }); + } + } + } + + unsafe fn insert_debug_marker(&mut self, label: &str) { + let range = self.cmd_buffer.add_marker(label); + self.cmd_buffer.commands.push(C::InsertDebugMarker(range)); + } + unsafe fn begin_debug_marker(&mut self, group_label: &str) { + let range = self.cmd_buffer.add_marker(group_label); + self.cmd_buffer.commands.push(C::PushDebugGroup(range)); + } + unsafe fn end_debug_marker(&mut self) { + self.cmd_buffer.commands.push(C::PopDebugGroup); + } + + unsafe fn set_render_pipeline(&mut self, pipeline: &super::RenderPipeline) { + self.state.topology = conv::map_primitive_topology(pipeline.primitive.topology); + + if self + .private_caps + .contains(super::PrivateCapabilities::VERTEX_BUFFER_LAYOUT) + { + for vat in pipeline.vertex_attributes.iter() { + let vb = pipeline.vertex_buffers[vat.buffer_index as usize] + .as_ref() + .unwrap(); + // set the layout + self.cmd_buffer.commands.push(C::SetVertexAttribute { + buffer: None, + buffer_desc: vb.clone(), + attribute_desc: vat.clone(), + }); + } + } else { + for vat in &self.state.vertex_attributes { + self.cmd_buffer + .commands + .push(C::UnsetVertexAttribute(vat.location)); + } + self.state.vertex_attributes.clear(); + + self.state.dirty_vbuf_mask = 0; + // copy vertex attributes + for vat in pipeline.vertex_attributes.iter() { + //Note: we can invalidate more carefully here. + self.state.dirty_vbuf_mask |= 1 << vat.buffer_index; + self.state.vertex_attributes.push(vat.clone()); + } + } + + self.state.instance_vbuf_mask = 0; + // copy vertex state + for (index, (&mut (ref mut state_desc, _), pipe_desc)) in self + .state + .vertex_buffers + .iter_mut() + .zip(pipeline.vertex_buffers.iter()) + .enumerate() + { + let Some(pipe_desc) = pipe_desc else { + continue; + }; + if pipe_desc.step == wgt::VertexStepMode::Instance { + self.state.instance_vbuf_mask |= 1 << index; + } + if state_desc.as_ref() != Some(pipe_desc) { + self.state.dirty_vbuf_mask |= 1 << index; + *state_desc = Some(pipe_desc.clone()); + } + } + + self.set_pipeline_inner(&pipeline.inner); + + // set primitive state + let prim_state = conv::map_primitive_state(&pipeline.primitive); + if prim_state != self.state.primitive { + self.cmd_buffer + .commands + .push(C::SetPrimitive(prim_state.clone())); + self.state.primitive = prim_state; + } + + // set depth/stencil states + let mut aspects = crate::FormatAspects::empty(); + if pipeline.depth_bias != self.state.depth_bias { + self.state.depth_bias = pipeline.depth_bias; + self.cmd_buffer + .commands + .push(C::SetDepthBias(pipeline.depth_bias)); + } + if let Some(ref depth) = pipeline.depth { + aspects |= crate::FormatAspects::DEPTH; + self.cmd_buffer.commands.push(C::SetDepth(depth.clone())); + } + if let Some(ref stencil) = pipeline.stencil { + aspects |= crate::FormatAspects::STENCIL; + self.state.stencil = stencil.clone(); + self.rebind_stencil_func(); + if stencil.front.ops == stencil.back.ops + && stencil.front.mask_write == stencil.back.mask_write + { + self.cmd_buffer.commands.push(C::SetStencilOps { + face: glow::FRONT_AND_BACK, + write_mask: stencil.front.mask_write, + ops: stencil.front.ops.clone(), + }); + } else { + self.cmd_buffer.commands.push(C::SetStencilOps { + face: glow::FRONT, + write_mask: stencil.front.mask_write, + ops: stencil.front.ops.clone(), + }); + self.cmd_buffer.commands.push(C::SetStencilOps { + face: glow::BACK, + write_mask: stencil.back.mask_write, + ops: stencil.back.ops.clone(), + }); + } + } + self.cmd_buffer + .commands + .push(C::ConfigureDepthStencil(aspects)); + + // set multisampling state + if pipeline.alpha_to_coverage_enabled != self.state.alpha_to_coverage_enabled { + self.state.alpha_to_coverage_enabled = pipeline.alpha_to_coverage_enabled; + self.cmd_buffer + .commands + .push(C::SetAlphaToCoverage(pipeline.alpha_to_coverage_enabled)); + } + + // set blend states + if self.state.color_targets[..] != pipeline.color_targets[..] { + if pipeline + .color_targets + .iter() + .skip(1) + .any(|ct| *ct != pipeline.color_targets[0]) + { + for (index, ct) in pipeline.color_targets.iter().enumerate() { + self.cmd_buffer.commands.push(C::SetColorTarget { + draw_buffer_index: Some(index as u32), + desc: ct.clone(), + }); + } + } else { + self.cmd_buffer.commands.push(C::SetColorTarget { + draw_buffer_index: None, + desc: pipeline.color_targets.first().cloned().unwrap_or_default(), + }); + } + } + self.state.color_targets.clear(); + for ct in pipeline.color_targets.iter() { + self.state.color_targets.push(ct.clone()); + } + + // set clip plane count + if pipeline.inner.clip_distance_count != self.state.clip_distance_count { + self.cmd_buffer.commands.push(C::SetClipDistances { + old_count: self.state.clip_distance_count, + new_count: pipeline.inner.clip_distance_count, + }); + self.state.clip_distance_count = pipeline.inner.clip_distance_count; + } + } + + unsafe fn set_index_buffer<'a>( + &mut self, + binding: crate::BufferBinding<'a, super::Buffer>, + format: wgt::IndexFormat, + ) { + self.state.index_offset = binding.offset; + self.state.index_format = format; + self.cmd_buffer + .commands + .push(C::SetIndexBuffer(binding.buffer.raw.unwrap())); + } + unsafe fn set_vertex_buffer<'a>( + &mut self, + index: u32, + binding: crate::BufferBinding<'a, super::Buffer>, + ) { + self.state.dirty_vbuf_mask |= 1 << index; + let (_, ref mut vb) = self.state.vertex_buffers[index as usize]; + *vb = Some(super::BufferBinding { + raw: binding.buffer.raw.unwrap(), + offset: binding.offset, + }); + } + unsafe fn set_viewport(&mut self, rect: &crate::Rect<f32>, depth: Range<f32>) { + self.cmd_buffer.commands.push(C::SetViewport { + rect: crate::Rect { + x: rect.x as i32, + y: rect.y as i32, + w: rect.w as i32, + h: rect.h as i32, + }, + depth, + }); + } + unsafe fn set_scissor_rect(&mut self, rect: &crate::Rect<u32>) { + self.cmd_buffer.commands.push(C::SetScissor(crate::Rect { + x: rect.x as i32, + y: rect.y as i32, + w: rect.w as i32, + h: rect.h as i32, + })); + } + unsafe fn set_stencil_reference(&mut self, value: u32) { + self.state.stencil.front.reference = value; + self.state.stencil.back.reference = value; + self.rebind_stencil_func(); + } + unsafe fn set_blend_constants(&mut self, color: &[f32; 4]) { + self.cmd_buffer.commands.push(C::SetBlendConstant(*color)); + } + + unsafe fn draw( + &mut self, + first_vertex: u32, + vertex_count: u32, + first_instance: u32, + instance_count: u32, + ) { + self.prepare_draw(first_instance); + #[allow( + clippy::clone_on_copy, + reason = "False positive when cloning glow::UniformLocation" + )] + self.cmd_buffer.commands.push(C::Draw { + topology: self.state.topology, + first_vertex, + vertex_count, + first_instance, + instance_count, + first_instance_location: self.state.first_instance_location.clone(), + }); + } + unsafe fn draw_indexed( + &mut self, + first_index: u32, + index_count: u32, + base_vertex: i32, + first_instance: u32, + instance_count: u32, + ) { + self.prepare_draw(first_instance); + let (index_size, index_type) = match self.state.index_format { + wgt::IndexFormat::Uint16 => (2, glow::UNSIGNED_SHORT), + wgt::IndexFormat::Uint32 => (4, glow::UNSIGNED_INT), + }; + let index_offset = self.state.index_offset + index_size * first_index as wgt::BufferAddress; + #[allow( + clippy::clone_on_copy, + reason = "False positive when cloning glow::UniformLocation" + )] + self.cmd_buffer.commands.push(C::DrawIndexed { + topology: self.state.topology, + index_type, + index_offset, + index_count, + base_vertex, + first_instance, + instance_count, + first_instance_location: self.state.first_instance_location.clone(), + }); + } + unsafe fn draw_mesh_tasks( + &mut self, + _group_count_x: u32, + _group_count_y: u32, + _group_count_z: u32, + ) { + unreachable!() + } + unsafe fn draw_indirect( + &mut self, + buffer: &super::Buffer, + offset: wgt::BufferAddress, + draw_count: u32, + ) { + self.prepare_draw(0); + for draw in 0..draw_count as wgt::BufferAddress { + let indirect_offset = + offset + draw * size_of::<wgt::DrawIndirectArgs>() as wgt::BufferAddress; + #[allow( + clippy::clone_on_copy, + reason = "False positive when cloning glow::UniformLocation" + )] + self.cmd_buffer.commands.push(C::DrawIndirect { + topology: self.state.topology, + indirect_buf: buffer.raw.unwrap(), + indirect_offset, + first_instance_location: self.state.first_instance_location.clone(), + }); + } + } + unsafe fn draw_indexed_indirect( + &mut self, + buffer: &super::Buffer, + offset: wgt::BufferAddress, + draw_count: u32, + ) { + self.prepare_draw(0); + let index_type = match self.state.index_format { + wgt::IndexFormat::Uint16 => glow::UNSIGNED_SHORT, + wgt::IndexFormat::Uint32 => glow::UNSIGNED_INT, + }; + for draw in 0..draw_count as wgt::BufferAddress { + let indirect_offset = + offset + draw * size_of::<wgt::DrawIndexedIndirectArgs>() as wgt::BufferAddress; + #[allow( + clippy::clone_on_copy, + reason = "False positive when cloning glow::UniformLocation" + )] + self.cmd_buffer.commands.push(C::DrawIndexedIndirect { + topology: self.state.topology, + index_type, + indirect_buf: buffer.raw.unwrap(), + indirect_offset, + first_instance_location: self.state.first_instance_location.clone(), + }); + } + } + unsafe fn draw_mesh_tasks_indirect( + &mut self, + _buffer: &<Self::A as crate::Api>::Buffer, + _offset: wgt::BufferAddress, + _draw_count: u32, + ) { + unreachable!() + } + unsafe fn draw_indirect_count( + &mut self, + _buffer: &super::Buffer, + _offset: wgt::BufferAddress, + _count_buffer: &super::Buffer, + _count_offset: wgt::BufferAddress, + _max_count: u32, + ) { + unreachable!() + } + unsafe fn draw_indexed_indirect_count( + &mut self, + _buffer: &super::Buffer, + _offset: wgt::BufferAddress, + _count_buffer: &super::Buffer, + _count_offset: wgt::BufferAddress, + _max_count: u32, + ) { + unreachable!() + } + unsafe fn draw_mesh_tasks_indirect_count( + &mut self, + _buffer: &<Self::A as crate::Api>::Buffer, + _offset: wgt::BufferAddress, + _count_buffer: &<Self::A as crate::Api>::Buffer, + _count_offset: wgt::BufferAddress, + _max_count: u32, + ) { + unreachable!() + } + + // compute + + unsafe fn begin_compute_pass(&mut self, desc: &crate::ComputePassDescriptor<super::QuerySet>) { + debug_assert!(self.state.end_of_pass_timestamp.is_none()); + if let Some(ref t) = desc.timestamp_writes { + if let Some(index) = t.beginning_of_pass_write_index { + unsafe { self.write_timestamp(t.query_set, index) } + } + self.state.end_of_pass_timestamp = t + .end_of_pass_write_index + .map(|index| t.query_set.queries[index as usize]); + } + + if let Some(label) = desc.label { + let range = self.cmd_buffer.add_marker(label); + self.cmd_buffer.commands.push(C::PushDebugGroup(range)); + self.state.has_pass_label = true; + } + } + unsafe fn end_compute_pass(&mut self) { + if self.state.has_pass_label { + self.cmd_buffer.commands.push(C::PopDebugGroup); + self.state.has_pass_label = false; + } + + if let Some(query) = self.state.end_of_pass_timestamp.take() { + self.cmd_buffer.commands.push(C::TimestampQuery(query)); + } + } + + unsafe fn set_compute_pipeline(&mut self, pipeline: &super::ComputePipeline) { + self.set_pipeline_inner(&pipeline.inner); + } + + unsafe fn dispatch_workgroups(&mut self, count: [u32; 3]) { + // Empty dispatches are invalid in OpenGL, but valid in WebGPU. + if count.contains(&0) { + return; + } + self.cmd_buffer.commands.push(C::Dispatch(count)); + } + unsafe fn dispatch_workgroups_indirect( + &mut self, + buffer: &super::Buffer, + offset: wgt::BufferAddress, + ) { + self.cmd_buffer.commands.push(C::DispatchIndirect { + indirect_buf: buffer.raw.unwrap(), + indirect_offset: offset, + }); + } + + unsafe fn build_acceleration_structures<'a, T>( + &mut self, + _descriptor_count: u32, + _descriptors: T, + ) where + super::Api: 'a, + T: IntoIterator< + Item = crate::BuildAccelerationStructureDescriptor< + 'a, + super::Buffer, + super::AccelerationStructure, + >, + >, + { + unimplemented!() + } + + unsafe fn place_acceleration_structure_barrier( + &mut self, + _barriers: crate::AccelerationStructureBarrier, + ) { + unimplemented!() + } + + unsafe fn copy_acceleration_structure_to_acceleration_structure( + &mut self, + _src: &super::AccelerationStructure, + _dst: &super::AccelerationStructure, + _copy: wgt::AccelerationStructureCopy, + ) { + unimplemented!() + } + + unsafe fn read_acceleration_structure_compact_size( + &mut self, + _acceleration_structure: &super::AccelerationStructure, + _buf: &super::Buffer, + ) { + unimplemented!() + } + + unsafe fn set_acceleration_structure_dependencies( + _command_buffers: &[&super::CommandBuffer], + _dependencies: &[&super::AccelerationStructure], + ) { + unimplemented!() + } + + unsafe fn begin_ray_tracing_pass(&mut self, _desc: &crate::RayTracingPassDescriptor) { + unimplemented!() + } + + unsafe fn end_ray_tracing_pass(&mut self) { + unimplemented!() + } + + unsafe fn set_ray_tracing_pipeline( + &mut self, + _pipeline: &<Self::A as crate::Api>::RayTracingPipeline, + ) { + unimplemented!() + } + + unsafe fn trace_rays( + &mut self, + _count: [u32; 3], + _ray_generation_group_data: crate::PipelineGroupData<super::Buffer>, + _miss_group_data: crate::PipelineGroupData<super::Buffer>, + _intersection_group_data: crate::PipelineGroupData<super::Buffer>, + ) { + unimplemented!() + } +} diff --git a/third_party/wgpu-hal/src/gles/conv.rs b/third_party/wgpu-hal/src/gles/conv.rs new file mode 100644 index 000000000..5b54f8f1e --- /dev/null +++ b/third_party/wgpu-hal/src/gles/conv.rs @@ -0,0 +1,430 @@ +impl super::AdapterShared { + pub(super) fn describe_texture_format( + &self, + texture_format: wgt::TextureFormat, + ) -> super::TextureFormatDesc { + use wgt::TextureFormat as Tf; + use wgt::{AstcBlock, AstcChannel}; + + let (internal, external, data_type) = match texture_format { + Tf::R8Unorm => (glow::R8, glow::RED, glow::UNSIGNED_BYTE), + Tf::R8Snorm => (glow::R8_SNORM, glow::RED, glow::BYTE), + Tf::R8Uint => (glow::R8UI, glow::RED_INTEGER, glow::UNSIGNED_BYTE), + Tf::R8Sint => (glow::R8I, glow::RED_INTEGER, glow::BYTE), + Tf::R16Uint => (glow::R16UI, glow::RED_INTEGER, glow::UNSIGNED_SHORT), + Tf::R16Sint => (glow::R16I, glow::RED_INTEGER, glow::SHORT), + Tf::R16Unorm => (glow::R16, glow::RED, glow::UNSIGNED_SHORT), + Tf::R16Snorm => (glow::R16_SNORM, glow::RED, glow::SHORT), + Tf::R16Float => (glow::R16F, glow::RED, glow::HALF_FLOAT), + Tf::Rg8Unorm => (glow::RG8, glow::RG, glow::UNSIGNED_BYTE), + Tf::Rg8Snorm => (glow::RG8_SNORM, glow::RG, glow::BYTE), + Tf::Rg8Uint => (glow::RG8UI, glow::RG_INTEGER, glow::UNSIGNED_BYTE), + Tf::Rg8Sint => (glow::RG8I, glow::RG_INTEGER, glow::BYTE), + Tf::R32Uint => (glow::R32UI, glow::RED_INTEGER, glow::UNSIGNED_INT), + Tf::R32Sint => (glow::R32I, glow::RED_INTEGER, glow::INT), + Tf::R32Float => (glow::R32F, glow::RED, glow::FLOAT), + Tf::Rg16Uint => (glow::RG16UI, glow::RG_INTEGER, glow::UNSIGNED_SHORT), + Tf::Rg16Sint => (glow::RG16I, glow::RG_INTEGER, glow::SHORT), + Tf::Rg16Unorm => (glow::RG16, glow::RG, glow::UNSIGNED_SHORT), + Tf::Rg16Snorm => (glow::RG16_SNORM, glow::RG, glow::SHORT), + Tf::Rg16Float => (glow::RG16F, glow::RG, glow::HALF_FLOAT), + Tf::Rgba8Unorm => (glow::RGBA8, glow::RGBA, glow::UNSIGNED_BYTE), + Tf::Rgba8UnormSrgb => (glow::SRGB8_ALPHA8, glow::RGBA, glow::UNSIGNED_BYTE), + Tf::Bgra8UnormSrgb => (glow::SRGB8_ALPHA8, glow::BGRA, glow::UNSIGNED_BYTE), //TODO? + Tf::Rgba8Snorm => (glow::RGBA8_SNORM, glow::RGBA, glow::BYTE), + Tf::Bgra8Unorm => (glow::RGBA8, glow::BGRA, glow::UNSIGNED_BYTE), //TODO? + Tf::Rgba8Uint => (glow::RGBA8UI, glow::RGBA_INTEGER, glow::UNSIGNED_BYTE), + Tf::Rgba8Sint => (glow::RGBA8I, glow::RGBA_INTEGER, glow::BYTE), + Tf::Rgb10a2Uint => ( + glow::RGB10_A2UI, + glow::RGBA_INTEGER, + glow::UNSIGNED_INT_2_10_10_10_REV, + ), + Tf::Rgb10a2Unorm => ( + glow::RGB10_A2, + glow::RGBA, + glow::UNSIGNED_INT_2_10_10_10_REV, + ), + Tf::Rg11b10Ufloat => ( + glow::R11F_G11F_B10F, + glow::RGB, + glow::UNSIGNED_INT_10F_11F_11F_REV, + ), + Tf::R64Uint => (glow::RG32UI, glow::RED_INTEGER, glow::UNSIGNED_INT), + Tf::Rg32Uint => (glow::RG32UI, glow::RG_INTEGER, glow::UNSIGNED_INT), + Tf::Rg32Sint => (glow::RG32I, glow::RG_INTEGER, glow::INT), + Tf::Rg32Float => (glow::RG32F, glow::RG, glow::FLOAT), + Tf::Rgba16Uint => (glow::RGBA16UI, glow::RGBA_INTEGER, glow::UNSIGNED_SHORT), + Tf::Rgba16Sint => (glow::RGBA16I, glow::RGBA_INTEGER, glow::SHORT), + Tf::Rgba16Unorm => (glow::RGBA16, glow::RGBA, glow::UNSIGNED_SHORT), + Tf::Rgba16Snorm => (glow::RGBA16_SNORM, glow::RGBA, glow::SHORT), + Tf::Rgba16Float => (glow::RGBA16F, glow::RGBA, glow::HALF_FLOAT), + Tf::Rgba32Uint => (glow::RGBA32UI, glow::RGBA_INTEGER, glow::UNSIGNED_INT), + Tf::Rgba32Sint => (glow::RGBA32I, glow::RGBA_INTEGER, glow::INT), + Tf::Rgba32Float => (glow::RGBA32F, glow::RGBA, glow::FLOAT), + Tf::Stencil8 => ( + glow::STENCIL_INDEX8, + glow::STENCIL_INDEX, + glow::UNSIGNED_BYTE, + ), + Tf::Depth16Unorm => ( + glow::DEPTH_COMPONENT16, + glow::DEPTH_COMPONENT, + glow::UNSIGNED_SHORT, + ), + Tf::Depth32Float => (glow::DEPTH_COMPONENT32F, glow::DEPTH_COMPONENT, glow::FLOAT), + Tf::Depth32FloatStencil8 => ( + glow::DEPTH32F_STENCIL8, + glow::DEPTH_STENCIL, + glow::FLOAT_32_UNSIGNED_INT_24_8_REV, + ), + Tf::Depth24Plus => ( + glow::DEPTH_COMPONENT24, + glow::DEPTH_COMPONENT, + glow::UNSIGNED_INT, + ), + Tf::Depth24PlusStencil8 => ( + glow::DEPTH24_STENCIL8, + glow::DEPTH_STENCIL, + glow::UNSIGNED_INT_24_8, + ), + Tf::NV12 => unreachable!(), + Tf::P010 => unreachable!(), + Tf::Rgb9e5Ufloat => (glow::RGB9_E5, glow::RGB, glow::UNSIGNED_INT_5_9_9_9_REV), + Tf::Bc1RgbaUnorm => (glow::COMPRESSED_RGBA_S3TC_DXT1_EXT, glow::RGBA, 0), + Tf::Bc1RgbaUnormSrgb => (glow::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, glow::RGBA, 0), + Tf::Bc2RgbaUnorm => (glow::COMPRESSED_RGBA_S3TC_DXT3_EXT, glow::RGBA, 0), + Tf::Bc2RgbaUnormSrgb => (glow::COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, glow::RGBA, 0), + Tf::Bc3RgbaUnorm => (glow::COMPRESSED_RGBA_S3TC_DXT5_EXT, glow::RGBA, 0), + Tf::Bc3RgbaUnormSrgb => (glow::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, glow::RGBA, 0), + Tf::Bc4RUnorm => (glow::COMPRESSED_RED_RGTC1, glow::RED, 0), + Tf::Bc4RSnorm => (glow::COMPRESSED_SIGNED_RED_RGTC1, glow::RED, 0), + Tf::Bc5RgUnorm => (glow::COMPRESSED_RG_RGTC2, glow::RG, 0), + Tf::Bc5RgSnorm => (glow::COMPRESSED_SIGNED_RG_RGTC2, glow::RG, 0), + Tf::Bc6hRgbUfloat => (glow::COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT, glow::RGB, 0), + Tf::Bc6hRgbFloat => (glow::COMPRESSED_RGB_BPTC_SIGNED_FLOAT, glow::RGB, 0), + Tf::Bc7RgbaUnorm => (glow::COMPRESSED_RGBA_BPTC_UNORM, glow::RGBA, 0), + Tf::Bc7RgbaUnormSrgb => (glow::COMPRESSED_SRGB_ALPHA_BPTC_UNORM, glow::RGBA, 0), + Tf::Etc2Rgb8Unorm => (glow::COMPRESSED_RGB8_ETC2, glow::RGB, 0), + Tf::Etc2Rgb8UnormSrgb => (glow::COMPRESSED_SRGB8_ETC2, glow::RGB, 0), + Tf::Etc2Rgb8A1Unorm => ( + glow::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, + glow::RGBA, + 0, + ), + Tf::Etc2Rgb8A1UnormSrgb => ( + glow::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, + glow::RGBA, + 0, + ), + Tf::Etc2Rgba8Unorm => (glow::COMPRESSED_RGBA8_ETC2_EAC, glow::RGBA, 0), + Tf::Etc2Rgba8UnormSrgb => (glow::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC, glow::RGBA, 0), + Tf::EacR11Unorm => (glow::COMPRESSED_R11_EAC, glow::RED, 0), + Tf::EacR11Snorm => (glow::COMPRESSED_SIGNED_R11_EAC, glow::RED, 0), + Tf::EacRg11Unorm => (glow::COMPRESSED_RG11_EAC, glow::RG, 0), + Tf::EacRg11Snorm => (glow::COMPRESSED_SIGNED_RG11_EAC, glow::RG, 0), + Tf::Astc { block, channel } => match channel { + AstcChannel::Unorm | AstcChannel::Hdr => match block { + AstcBlock::B4x4 => (glow::COMPRESSED_RGBA_ASTC_4x4_KHR, glow::RGBA, 0), + AstcBlock::B5x4 => (glow::COMPRESSED_RGBA_ASTC_5x4_KHR, glow::RGBA, 0), + AstcBlock::B5x5 => (glow::COMPRESSED_RGBA_ASTC_5x5_KHR, glow::RGBA, 0), + AstcBlock::B6x5 => (glow::COMPRESSED_RGBA_ASTC_6x5_KHR, glow::RGBA, 0), + AstcBlock::B6x6 => (glow::COMPRESSED_RGBA_ASTC_6x6_KHR, glow::RGBA, 0), + AstcBlock::B8x5 => (glow::COMPRESSED_RGBA_ASTC_8x5_KHR, glow::RGBA, 0), + AstcBlock::B8x6 => (glow::COMPRESSED_RGBA_ASTC_8x6_KHR, glow::RGBA, 0), + AstcBlock::B8x8 => (glow::COMPRESSED_RGBA_ASTC_8x8_KHR, glow::RGBA, 0), + AstcBlock::B10x5 => (glow::COMPRESSED_RGBA_ASTC_10x5_KHR, glow::RGBA, 0), + AstcBlock::B10x6 => (glow::COMPRESSED_RGBA_ASTC_10x6_KHR, glow::RGBA, 0), + AstcBlock::B10x8 => (glow::COMPRESSED_RGBA_ASTC_10x8_KHR, glow::RGBA, 0), + AstcBlock::B10x10 => (glow::COMPRESSED_RGBA_ASTC_10x10_KHR, glow::RGBA, 0), + AstcBlock::B12x10 => (glow::COMPRESSED_RGBA_ASTC_12x10_KHR, glow::RGBA, 0), + AstcBlock::B12x12 => (glow::COMPRESSED_RGBA_ASTC_12x12_KHR, glow::RGBA, 0), + }, + AstcChannel::UnormSrgb => match block { + AstcBlock::B4x4 => (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR, glow::RGBA, 0), + AstcBlock::B5x4 => (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR, glow::RGBA, 0), + AstcBlock::B5x5 => (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR, glow::RGBA, 0), + AstcBlock::B6x5 => (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR, glow::RGBA, 0), + AstcBlock::B6x6 => (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR, glow::RGBA, 0), + AstcBlock::B8x5 => (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR, glow::RGBA, 0), + AstcBlock::B8x6 => (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR, glow::RGBA, 0), + AstcBlock::B8x8 => (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR, glow::RGBA, 0), + AstcBlock::B10x5 => { + (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR, glow::RGBA, 0) + } + AstcBlock::B10x6 => { + (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR, glow::RGBA, 0) + } + AstcBlock::B10x8 => { + (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR, glow::RGBA, 0) + } + AstcBlock::B10x10 => { + (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR, glow::RGBA, 0) + } + AstcBlock::B12x10 => { + (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR, glow::RGBA, 0) + } + AstcBlock::B12x12 => { + (glow::COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR, glow::RGBA, 0) + } + }, + }, + }; + + super::TextureFormatDesc { + internal, + external, + data_type, + } + } +} + +pub(super) fn describe_vertex_format(vertex_format: wgt::VertexFormat) -> super::VertexFormatDesc { + use super::VertexAttribKind as Vak; + use wgt::VertexFormat as Vf; + + let (element_count, element_format, attrib_kind) = match vertex_format { + Vf::Unorm8 => (1, glow::UNSIGNED_BYTE, Vak::Float), + Vf::Snorm8 => (1, glow::BYTE, Vak::Float), + Vf::Uint8 => (1, glow::UNSIGNED_BYTE, Vak::Integer), + Vf::Sint8 => (1, glow::BYTE, Vak::Integer), + Vf::Unorm8x2 => (2, glow::UNSIGNED_BYTE, Vak::Float), + Vf::Snorm8x2 => (2, glow::BYTE, Vak::Float), + Vf::Uint8x2 => (2, glow::UNSIGNED_BYTE, Vak::Integer), + Vf::Sint8x2 => (2, glow::BYTE, Vak::Integer), + Vf::Unorm8x4 => (4, glow::UNSIGNED_BYTE, Vak::Float), + Vf::Snorm8x4 => (4, glow::BYTE, Vak::Float), + Vf::Uint8x4 => (4, glow::UNSIGNED_BYTE, Vak::Integer), + Vf::Sint8x4 => (4, glow::BYTE, Vak::Integer), + Vf::Unorm16 => (1, glow::UNSIGNED_SHORT, Vak::Float), + Vf::Snorm16 => (1, glow::SHORT, Vak::Float), + Vf::Uint16 => (1, glow::UNSIGNED_SHORT, Vak::Integer), + Vf::Sint16 => (1, glow::SHORT, Vak::Integer), + Vf::Float16 => (1, glow::HALF_FLOAT, Vak::Float), + Vf::Unorm16x2 => (2, glow::UNSIGNED_SHORT, Vak::Float), + Vf::Snorm16x2 => (2, glow::SHORT, Vak::Float), + Vf::Uint16x2 => (2, glow::UNSIGNED_SHORT, Vak::Integer), + Vf::Sint16x2 => (2, glow::SHORT, Vak::Integer), + Vf::Float16x2 => (2, glow::HALF_FLOAT, Vak::Float), + Vf::Unorm16x4 => (4, glow::UNSIGNED_SHORT, Vak::Float), + Vf::Snorm16x4 => (4, glow::SHORT, Vak::Float), + Vf::Uint16x4 => (4, glow::UNSIGNED_SHORT, Vak::Integer), + Vf::Sint16x4 => (4, glow::SHORT, Vak::Integer), + Vf::Float16x4 => (4, glow::HALF_FLOAT, Vak::Float), + Vf::Uint32 => (1, glow::UNSIGNED_INT, Vak::Integer), + Vf::Sint32 => (1, glow::INT, Vak::Integer), + Vf::Float32 => (1, glow::FLOAT, Vak::Float), + Vf::Uint32x2 => (2, glow::UNSIGNED_INT, Vak::Integer), + Vf::Sint32x2 => (2, glow::INT, Vak::Integer), + Vf::Float32x2 => (2, glow::FLOAT, Vak::Float), + Vf::Uint32x3 => (3, glow::UNSIGNED_INT, Vak::Integer), + Vf::Sint32x3 => (3, glow::INT, Vak::Integer), + Vf::Float32x3 => (3, glow::FLOAT, Vak::Float), + Vf::Uint32x4 => (4, glow::UNSIGNED_INT, Vak::Integer), + Vf::Sint32x4 => (4, glow::INT, Vak::Integer), + Vf::Float32x4 => (4, glow::FLOAT, Vak::Float), + Vf::Unorm10_10_10_2 => (4, glow::UNSIGNED_INT_2_10_10_10_REV, Vak::Float), + Vf::Unorm8x4Bgra => (glow::BGRA as i32, glow::UNSIGNED_BYTE, Vak::Float), + Vf::Float64 | Vf::Float64x2 | Vf::Float64x3 | Vf::Float64x4 => unimplemented!(), + }; + + super::VertexFormatDesc { + element_count, + element_format, + attrib_kind, + } +} + +pub fn map_filter_modes( + min: wgt::FilterMode, + mag: wgt::FilterMode, + mip: wgt::MipmapFilterMode, +) -> (u32, u32) { + use wgt::FilterMode as Fm; + use wgt::MipmapFilterMode as Mfm; + + let mag_filter = match mag { + Fm::Nearest => glow::NEAREST, + Fm::Linear => glow::LINEAR, + }; + + let min_filter = match (min, mip) { + (Fm::Nearest, Mfm::Nearest) => glow::NEAREST_MIPMAP_NEAREST, + (Fm::Nearest, Mfm::Linear) => glow::NEAREST_MIPMAP_LINEAR, + (Fm::Linear, Mfm::Nearest) => glow::LINEAR_MIPMAP_NEAREST, + (Fm::Linear, Mfm::Linear) => glow::LINEAR_MIPMAP_LINEAR, + }; + + (min_filter, mag_filter) +} + +pub fn map_address_mode(mode: wgt::AddressMode) -> u32 { + match mode { + wgt::AddressMode::Repeat => glow::REPEAT, + wgt::AddressMode::MirrorRepeat => glow::MIRRORED_REPEAT, + wgt::AddressMode::ClampToEdge => glow::CLAMP_TO_EDGE, + wgt::AddressMode::ClampToBorder => glow::CLAMP_TO_BORDER, + //wgt::AddressMode::MirrorClamp => glow::MIRROR_CLAMP_TO_EDGE, + } +} + +pub fn map_compare_func(fun: wgt::CompareFunction) -> u32 { + use wgt::CompareFunction as Cf; + match fun { + Cf::Never => glow::NEVER, + Cf::Less => glow::LESS, + Cf::LessEqual => glow::LEQUAL, + Cf::Equal => glow::EQUAL, + Cf::GreaterEqual => glow::GEQUAL, + Cf::Greater => glow::GREATER, + Cf::NotEqual => glow::NOTEQUAL, + Cf::Always => glow::ALWAYS, + } +} + +pub fn map_primitive_topology(topology: wgt::PrimitiveTopology) -> u32 { + use wgt::PrimitiveTopology as Pt; + match topology { + Pt::PointList => glow::POINTS, + Pt::LineList => glow::LINES, + Pt::LineStrip => glow::LINE_STRIP, + Pt::TriangleList => glow::TRIANGLES, + Pt::TriangleStrip => glow::TRIANGLE_STRIP, + } +} + +pub(super) fn map_primitive_state(state: &wgt::PrimitiveState) -> super::PrimitiveState { + super::PrimitiveState { + //Note: we are flipping the front face, so that + // the Y-flip in the generated GLSL keeps the same visibility. + // See `naga::back::glsl::WriterFlags::ADJUST_COORDINATE_SPACE`. + front_face: match state.front_face { + wgt::FrontFace::Cw => glow::CCW, + wgt::FrontFace::Ccw => glow::CW, + }, + cull_face: match state.cull_mode { + Some(wgt::Face::Front) => glow::FRONT, + Some(wgt::Face::Back) => glow::BACK, + None => 0, + }, + unclipped_depth: state.unclipped_depth, + polygon_mode: match state.polygon_mode { + wgt::PolygonMode::Fill => glow::FILL, + wgt::PolygonMode::Line => glow::LINE, + wgt::PolygonMode::Point => glow::POINT, + }, + } +} + +pub fn _map_view_dimension(dim: wgt::TextureViewDimension) -> u32 { + use wgt::TextureViewDimension as Tvd; + match dim { + Tvd::D1 | Tvd::D2 => glow::TEXTURE_2D, + Tvd::D2Array => glow::TEXTURE_2D_ARRAY, + Tvd::Cube => glow::TEXTURE_CUBE_MAP, + Tvd::CubeArray => glow::TEXTURE_CUBE_MAP_ARRAY, + Tvd::D3 => glow::TEXTURE_3D, + } +} + +fn map_stencil_op(operation: wgt::StencilOperation) -> u32 { + use wgt::StencilOperation as So; + match operation { + So::Keep => glow::KEEP, + So::Zero => glow::ZERO, + So::Replace => glow::REPLACE, + So::Invert => glow::INVERT, + So::IncrementClamp => glow::INCR, + So::DecrementClamp => glow::DECR, + So::IncrementWrap => glow::INCR_WRAP, + So::DecrementWrap => glow::DECR_WRAP, + } +} + +fn map_stencil_ops(face: &wgt::StencilFaceState) -> super::StencilOps { + super::StencilOps { + pass: map_stencil_op(face.pass_op), + fail: map_stencil_op(face.fail_op), + depth_fail: map_stencil_op(face.depth_fail_op), + } +} + +pub(super) fn map_stencil(state: &wgt::StencilState) -> super::StencilState { + super::StencilState { + front: super::StencilSide { + function: map_compare_func(state.front.compare), + mask_read: state.read_mask, + mask_write: state.write_mask, + reference: 0, + ops: map_stencil_ops(&state.front), + }, + back: super::StencilSide { + function: map_compare_func(state.back.compare), + mask_read: state.read_mask, + mask_write: state.write_mask, + reference: 0, + ops: map_stencil_ops(&state.back), + }, + } +} + +fn map_blend_factor(factor: wgt::BlendFactor) -> u32 { + use wgt::BlendFactor as Bf; + match factor { + Bf::Zero => glow::ZERO, + Bf::One => glow::ONE, + Bf::Src => glow::SRC_COLOR, + Bf::OneMinusSrc => glow::ONE_MINUS_SRC_COLOR, + Bf::Dst => glow::DST_COLOR, + Bf::OneMinusDst => glow::ONE_MINUS_DST_COLOR, + Bf::SrcAlpha => glow::SRC_ALPHA, + Bf::OneMinusSrcAlpha => glow::ONE_MINUS_SRC_ALPHA, + Bf::DstAlpha => glow::DST_ALPHA, + Bf::OneMinusDstAlpha => glow::ONE_MINUS_DST_ALPHA, + Bf::Constant => glow::CONSTANT_COLOR, + Bf::OneMinusConstant => glow::ONE_MINUS_CONSTANT_COLOR, + Bf::SrcAlphaSaturated => glow::SRC_ALPHA_SATURATE, + Bf::Src1 => glow::SRC1_COLOR, + Bf::OneMinusSrc1 => glow::ONE_MINUS_SRC1_COLOR, + Bf::Src1Alpha => glow::SRC1_ALPHA, + Bf::OneMinusSrc1Alpha => glow::ONE_MINUS_SRC1_ALPHA, + } +} + +fn map_blend_component(component: &wgt::BlendComponent) -> super::BlendComponent { + super::BlendComponent { + src: map_blend_factor(component.src_factor), + dst: map_blend_factor(component.dst_factor), + equation: match component.operation { + wgt::BlendOperation::Add => glow::FUNC_ADD, + wgt::BlendOperation::Subtract => glow::FUNC_SUBTRACT, + wgt::BlendOperation::ReverseSubtract => glow::FUNC_REVERSE_SUBTRACT, + wgt::BlendOperation::Min => glow::MIN, + wgt::BlendOperation::Max => glow::MAX, + }, + } +} + +pub(super) fn map_blend(blend: &wgt::BlendState) -> super::BlendDesc { + super::BlendDesc { + color: map_blend_component(&blend.color), + alpha: map_blend_component(&blend.alpha), + } +} + +pub(super) fn map_storage_access(access: wgt::StorageTextureAccess) -> u32 { + match access { + wgt::StorageTextureAccess::ReadOnly => glow::READ_ONLY, + wgt::StorageTextureAccess::WriteOnly => glow::WRITE_ONLY, + wgt::StorageTextureAccess::ReadWrite => glow::READ_WRITE, + wgt::StorageTextureAccess::Atomic => glow::READ_WRITE, + } +} + +pub(super) fn is_layered_target(target: u32) -> bool { + match target { + glow::TEXTURE_2D | glow::TEXTURE_CUBE_MAP => false, + glow::TEXTURE_2D_ARRAY | glow::TEXTURE_CUBE_MAP_ARRAY | glow::TEXTURE_3D => true, + _ => unreachable!(), + } +} diff --git a/third_party/wgpu-hal/src/gles/device.rs b/third_party/wgpu-hal/src/gles/device.rs new file mode 100644 index 000000000..1c61cf3b7 --- /dev/null +++ b/third_party/wgpu-hal/src/gles/device.rs @@ -0,0 +1,1823 @@ +use alloc::{ + borrow::{Cow, ToOwned}, + format, + string::String, + string::ToString as _, + sync::Arc, + vec, + vec::Vec, +}; +use core::{cmp::max, convert::TryInto, num::NonZeroU32, ptr, sync::atomic::Ordering}; + +use arrayvec::ArrayVec; +use glow::HasContext; +use naga::FastHashMap; + +use super::{conv, lock, MaybeMutex, PrivateCapabilities}; +use crate::auxil::map_naga_stage; +use crate::TlasInstance; + +type ShaderStage<'a> = ( + naga::ShaderStage, + &'a crate::ProgrammableStage<'a, super::ShaderModule>, +); +type NameBindingMap = FastHashMap<String, (super::BindingRegister, u8)>; + +struct CompilationContext<'a> { + layout: &'a super::PipelineLayout, + sampler_map: &'a mut super::SamplerBindMap, + name_binding_map: &'a mut NameBindingMap, + immediates_items: &'a mut Vec<naga::back::glsl::ImmediateItem>, + multiview_mask: Option<NonZeroU32>, + clip_distance_count: &'a mut u32, +} + +impl CompilationContext<'_> { + fn consume_reflection( + self, + gl: &glow::Context, + module: &naga::Module, + ep_info: &naga::valid::FunctionInfo, + reflection_info: naga::back::glsl::ReflectionInfo, + naga_stage: naga::ShaderStage, + program: glow::Program, + ) { + for (handle, var) in module.global_variables.iter() { + if ep_info[handle].is_empty() { + continue; + } + let register = match var.space { + naga::AddressSpace::Uniform => super::BindingRegister::UniformBuffers, + naga::AddressSpace::Storage { .. } => super::BindingRegister::StorageBuffers, + _ => continue, + }; + + let br = var.binding.as_ref().unwrap(); + let slot = self.layout.get_slot(br); + + let name = match reflection_info.uniforms.get(&handle) { + Some(name) => name.clone(), + None => continue, + }; + log::trace!( + "Rebind buffer: {:?} -> {}, register={:?}, slot={}", + var.name.as_ref(), + &name, + register, + slot + ); + self.name_binding_map.insert(name, (register, slot)); + } + + for (name, mapping) in reflection_info.texture_mapping { + let var = &module.global_variables[mapping.texture]; + let register = match module.types[var.ty].inner { + naga::TypeInner::Image { + class: naga::ImageClass::Storage { .. }, + .. + } => super::BindingRegister::Images, + _ => super::BindingRegister::Textures, + }; + + let tex_br = var.binding.as_ref().unwrap(); + let texture_linear_index = self.layout.get_slot(tex_br); + + self.name_binding_map + .insert(name, (register, texture_linear_index)); + if let Some(sampler_handle) = mapping.sampler { + let sam_br = module.global_variables[sampler_handle] + .binding + .as_ref() + .unwrap(); + let sampler_linear_index = self.layout.get_slot(sam_br); + self.sampler_map[texture_linear_index as usize] = Some(sampler_linear_index); + } + } + + for (name, location) in reflection_info.varying { + match naga_stage { + naga::ShaderStage::Vertex => { + assert_eq!(location.index, 0); + unsafe { gl.bind_attrib_location(program, location.location, &name) } + } + naga::ShaderStage::Fragment => { + assert_eq!(location.index, 0); + unsafe { gl.bind_frag_data_location(program, location.location, &name) } + } + naga::ShaderStage::Compute => {} + naga::ShaderStage::Task + | naga::ShaderStage::Mesh + | naga::ShaderStage::RayGeneration + | naga::ShaderStage::AnyHit + | naga::ShaderStage::ClosestHit + | naga::ShaderStage::Miss => unreachable!(), + } + } + + *self.immediates_items = reflection_info.immediates_items; + + if naga_stage == naga::ShaderStage::Vertex { + *self.clip_distance_count = reflection_info.clip_distance_count; + } + } +} + +impl super::Device { + /// # Safety + /// + /// - `name` must be created respecting `desc` + /// - `name` must be a texture + /// - If `drop_callback` is [`None`], wgpu-hal will take ownership of the texture. If + /// `drop_callback` is [`Some`], the texture must be valid until the callback is called. + #[cfg(any(native, Emscripten))] + pub unsafe fn texture_from_raw( + &self, + name: NonZeroU32, + desc: &crate::TextureDescriptor, + drop_callback: Option<crate::DropCallback>, + ) -> super::Texture { + super::Texture { + inner: super::TextureInner::Texture { + raw: glow::NativeTexture(name), + target: super::Texture::get_info_from_desc(desc), + }, + drop_guard: crate::DropGuard::from_option(drop_callback), + mip_level_count: desc.mip_level_count, + array_layer_count: desc.array_layer_count(), + format: desc.format, + format_desc: self.shared.describe_texture_format(desc.format), + copy_size: desc.copy_extent(), + } + } + + /// # Safety + /// + /// - `name` must be created respecting `desc` + /// - `name` must be a renderbuffer + /// - If `drop_callback` is [`None`], wgpu-hal will take ownership of the renderbuffer. If + /// `drop_callback` is [`Some`], the renderbuffer must be valid until the callback is called. + #[cfg(any(native, Emscripten))] + pub unsafe fn texture_from_raw_renderbuffer( + &self, + name: NonZeroU32, + desc: &crate::TextureDescriptor, + drop_callback: Option<crate::DropCallback>, + ) -> super::Texture { + super::Texture { + inner: super::TextureInner::Renderbuffer { + raw: glow::NativeRenderbuffer(name), + }, + drop_guard: crate::DropGuard::from_option(drop_callback), + mip_level_count: desc.mip_level_count, + array_layer_count: desc.array_layer_count(), + format: desc.format, + format_desc: self.shared.describe_texture_format(desc.format), + copy_size: desc.copy_extent(), + } + } + + /// # Safety + /// + /// - `name` must be a non-zero GL buffer name created respecting `desc`. + /// - The buffer's storage size must be at least `desc.size`. + /// - If `desc.usage` includes [`BufferUses::MAP_READ`](wgt::BufferUses::MAP_READ) or + /// [`BufferUses::MAP_WRITE`](wgt::BufferUses::MAP_WRITE), the GL buffer must have + /// been allocated with `glBufferStorage` (or equivalent) using flags compatible + /// with persistent mapping (`GL_MAP_PERSISTENT_BIT` plus matching read/write/coherent + /// bits). Buffers created with the legacy `glBufferData` family cannot be mapped + /// through this path. + /// - If `drop_callback` is [`None`], wgpu-hal will take ownership of the buffer and + /// call `glDeleteBuffers` on it. If `drop_callback` is [`Some`], the buffer must + /// remain valid until the callback is invoked. + #[cfg(any(native, Emscripten))] + pub unsafe fn buffer_from_raw( + &self, + name: NonZeroU32, + desc: &crate::BufferDescriptor, + drop_callback: Option<crate::DropCallback>, + ) -> super::Buffer { + let target = if desc.usage.contains(wgt::BufferUses::INDEX) { + glow::ELEMENT_ARRAY_BUFFER + } else { + glow::ARRAY_BUFFER + }; + + let is_host_visible = desc + .usage + .intersects(wgt::BufferUses::MAP_READ | wgt::BufferUses::MAP_WRITE); + let is_coherent = desc + .memory_flags + .contains(crate::MemoryFlags::PREFER_COHERENT); + + let mut map_flags = 0; + if desc.usage.contains(wgt::BufferUses::MAP_READ) { + map_flags |= glow::MAP_READ_BIT; + } + if desc.usage.contains(wgt::BufferUses::MAP_WRITE) { + map_flags |= glow::MAP_WRITE_BIT; + } + if is_host_visible { + map_flags |= glow::MAP_PERSISTENT_BIT; + if is_coherent { + map_flags |= glow::MAP_COHERENT_BIT; + } + } + if !is_coherent && desc.usage.contains(wgt::BufferUses::MAP_WRITE) { + map_flags |= glow::MAP_FLUSH_EXPLICIT_BIT; + } + + self.counters.buffers.add(1); + + super::Buffer { + raw: Some(glow::NativeBuffer(name)), + target, + size: desc.size, + map_flags, + map_state: Arc::new(MaybeMutex::new(super::BufferMapState { + mapped: false, + data: None, + offset_of_current_mapping: 0, + })), + drop_guard: crate::DropGuard::from_option(drop_callback).map(Arc::new), + } + } + + unsafe fn compile_shader( + gl: &glow::Context, + shader: &str, + naga_stage: naga::ShaderStage, + #[cfg_attr(target_arch = "wasm32", allow(unused))] label: Option<&str>, + ) -> Result<glow::Shader, crate::PipelineError> { + let target = match naga_stage { + naga::ShaderStage::Vertex => glow::VERTEX_SHADER, + naga::ShaderStage::Fragment => glow::FRAGMENT_SHADER, + naga::ShaderStage::Compute => glow::COMPUTE_SHADER, + naga::ShaderStage::Task + | naga::ShaderStage::Mesh + | naga::ShaderStage::RayGeneration + | naga::ShaderStage::AnyHit + | naga::ShaderStage::ClosestHit + | naga::ShaderStage::Miss => unreachable!(), + }; + + let raw = unsafe { gl.create_shader(target) }.unwrap(); + #[cfg(native)] + if gl.supports_debug() { + let name = raw.0.get(); + unsafe { gl.object_label(glow::SHADER, name, label) }; + } + + unsafe { gl.shader_source(raw, shader) }; + unsafe { gl.compile_shader(raw) }; + + log::debug!("\tCompiled shader {raw:?}"); + + let compiled_ok = unsafe { gl.get_shader_compile_status(raw) }; + let msg = unsafe { gl.get_shader_info_log(raw) }; + if compiled_ok { + if !msg.is_empty() { + log::debug!("\tCompile message: {msg}"); + } + Ok(raw) + } else { + log::error!("\tShader compilation failed: {msg}"); + unsafe { gl.delete_shader(raw) }; + Err(crate::PipelineError::Linkage( + map_naga_stage(naga_stage), + msg, + )) + } + } + + fn create_shader( + gl: &glow::Context, + naga_stage: naga::ShaderStage, + stage: &crate::ProgrammableStage<super::ShaderModule>, + context: CompilationContext, + program: glow::Program, + ) -> Result<glow::Shader, crate::PipelineError> { + let source = 'outer: { + use naga::back::glsl; + let pipeline_options = glsl::PipelineOptions { + shader_stage: naga_stage, + entry_point: stage.entry_point.to_owned(), + multiview: context + .multiview_mask + .map(|a| NonZeroU32::new(a.get().count_ones()).unwrap()), + }; + + let naga = match stage.module.source { + super::ShaderModuleSource::Naga(ref naga) => naga, + super::ShaderModuleSource::Passthrough { ref source } => { + break 'outer Cow::Borrowed(source); + } + }; + + let (module, info) = naga::back::pipeline_constants::process_overrides( + &naga.module, + &naga.info, + Some((naga_stage, stage.entry_point)), + stage.constants, + ) + .map_err(|e| { + let msg = format!("{e}"); + crate::PipelineError::PipelineConstants(map_naga_stage(naga_stage), msg) + })?; + + let entry_point_index = module + .entry_points + .iter() + .position(|ep| ep.name.as_str() == stage.entry_point) + .ok_or(crate::PipelineError::EntryPoint(naga_stage))?; + + use naga::proc::BoundsCheckPolicy; + // The image bounds checks require the TEXTURE_LEVELS feature available in GL core 4.3+. + let version = gl.version(); + let image_check = if !version.is_embedded && (version.major, version.minor) >= (4, 3) { + BoundsCheckPolicy::ReadZeroSkipWrite + } else { + BoundsCheckPolicy::Unchecked + }; + + // Other bounds check are either provided by glsl or not implemented yet. + let policies = naga::proc::BoundsCheckPolicies { + index: BoundsCheckPolicy::Unchecked, + buffer: BoundsCheckPolicy::Unchecked, + image_load: image_check, + binding_array: BoundsCheckPolicy::Unchecked, + }; + + let mut output = String::new(); + let needs_temp_options = stage.zero_initialize_workgroup_memory + != context.layout.naga_options.zero_initialize_workgroup_memory; + let mut temp_options; + let naga_options = if needs_temp_options { + // We use a conditional here, as cloning the naga_options could be expensive + // That is, we want to avoid doing that unless we cannot avoid it + temp_options = context.layout.naga_options.clone(); + temp_options.zero_initialize_workgroup_memory = + stage.zero_initialize_workgroup_memory; + &temp_options + } else { + &context.layout.naga_options + }; + let mut writer = glsl::Writer::new( + &mut output, + &module, + &info, + naga_options, + &pipeline_options, + policies, + ) + .map_err(|e| { + let msg = format!("{e}"); + crate::PipelineError::Linkage(map_naga_stage(naga_stage), msg) + })?; + + let reflection_info = writer.write().map_err(|e| { + let msg = format!("{e}"); + crate::PipelineError::Linkage(map_naga_stage(naga_stage), msg) + })?; + + log::debug!("Naga generated shader:\n{output}"); + + context.consume_reflection( + gl, + &module, + info.get_entry_point(entry_point_index), + reflection_info, + naga_stage, + program, + ); + Cow::Owned(output) + }; + + unsafe { Self::compile_shader(gl, &source, naga_stage, stage.module.label.as_deref()) } + } + + unsafe fn create_pipeline<'a>( + &self, + gl: &glow::Context, + shaders: ArrayVec<ShaderStage<'a>, { crate::MAX_CONCURRENT_SHADER_STAGES }>, + layout: &super::PipelineLayout, + #[cfg_attr(target_arch = "wasm32", allow(unused))] label: Option<&str>, + multiview_mask: Option<NonZeroU32>, + ) -> Result<Arc<super::PipelineInner>, crate::PipelineError> { + let mut program_stages = ArrayVec::new(); + let group_to_binding_to_slot = layout + .group_infos + .iter() + .map(|group| group.as_ref().map(|group| group.binding_to_slot.clone())) + .collect::<Vec<_>>(); + for &(naga_stage, stage) in &shaders { + program_stages.push(super::ProgramStage { + naga_stage: naga_stage.to_owned(), + shader_id: stage.module.id, + entry_point: stage.entry_point.to_owned(), + zero_initialize_workgroup_memory: stage.zero_initialize_workgroup_memory, + constant_hash: Self::create_constant_hash(stage), + }); + } + let mut guard = self + .shared + .program_cache + .try_lock() + .expect("Couldn't acquire program_cache lock"); + // This guard ensures that we can't accidentally destroy a program whilst we're about to reuse it + // The only place that destroys a pipeline is also locking on `program_cache` + let program = guard + .entry(super::ProgramCacheKey { + stages: program_stages, + group_to_binding_to_slot: group_to_binding_to_slot.into_boxed_slice(), + }) + .or_insert_with(|| unsafe { + Self::create_program( + gl, + shaders, + layout, + label, + multiview_mask, + self.shared.shading_language_version, + self.shared.private_caps, + ) + }) + .to_owned()?; + drop(guard); + + Ok(program) + } + + fn create_constant_hash(stage: &crate::ProgrammableStage<super::ShaderModule>) -> Vec<u8> { + let mut buf: Vec<u8> = Vec::new(); + + for (key, value) in stage.constants.iter() { + buf.extend_from_slice(key.as_bytes()); + buf.extend_from_slice(&value.to_ne_bytes()); + } + + buf + } + + unsafe fn create_program<'a>( + gl: &glow::Context, + shaders: ArrayVec<ShaderStage<'a>, { crate::MAX_CONCURRENT_SHADER_STAGES }>, + layout: &super::PipelineLayout, + #[cfg_attr(target_arch = "wasm32", allow(unused))] label: Option<&str>, + multiview_mask: Option<NonZeroU32>, + glsl_version: naga::back::glsl::Version, + private_caps: PrivateCapabilities, + ) -> Result<Arc<super::PipelineInner>, crate::PipelineError> { + let glsl_version = match glsl_version { + naga::back::glsl::Version::Embedded { version, .. } => format!("{version} es"), + naga::back::glsl::Version::Desktop(version) => format!("{version}"), + }; + let program = unsafe { gl.create_program() }.unwrap(); + #[cfg(native)] + if let Some(label) = label { + if private_caps.contains(PrivateCapabilities::DEBUG_FNS) { + let name = program.0.get(); + unsafe { gl.object_label(glow::PROGRAM, name, Some(label)) }; + } + } + + let mut name_binding_map = NameBindingMap::default(); + let mut immediates_items = ArrayVec::<_, { crate::MAX_CONCURRENT_SHADER_STAGES }>::new(); + let mut sampler_map = [None; super::MAX_TEXTURE_SLOTS]; + let mut has_stages = wgt::ShaderStages::empty(); + let mut shaders_to_delete = ArrayVec::<_, { crate::MAX_CONCURRENT_SHADER_STAGES }>::new(); + let mut clip_distance_count = 0; + + for &(naga_stage, stage) in &shaders { + has_stages |= map_naga_stage(naga_stage); + let pc_item = { + immediates_items.push(Vec::new()); + immediates_items.last_mut().unwrap() + }; + let context = CompilationContext { + layout, + sampler_map: &mut sampler_map, + name_binding_map: &mut name_binding_map, + immediates_items: pc_item, + multiview_mask, + clip_distance_count: &mut clip_distance_count, + }; + + let shader = Self::create_shader(gl, naga_stage, stage, context, program)?; + shaders_to_delete.push(shader); + } + + // Create empty fragment shader if only vertex shader is present + if has_stages == wgt::ShaderStages::VERTEX { + let shader_src = format!("#version {glsl_version}\n void main(void) {{}}",); + log::debug!("Only vertex shader is present. Creating an empty fragment shader",); + let shader = unsafe { + Self::compile_shader( + gl, + &shader_src, + naga::ShaderStage::Fragment, + Some("(wgpu internal) dummy fragment shader"), + ) + }?; + shaders_to_delete.push(shader); + } + + for &shader in shaders_to_delete.iter() { + unsafe { gl.attach_shader(program, shader) }; + } + unsafe { gl.link_program(program) }; + + for shader in shaders_to_delete { + unsafe { gl.delete_shader(shader) }; + } + + log::debug!("\tLinked program {program:?}"); + + let linked_ok = unsafe { gl.get_program_link_status(program) }; + let msg = unsafe { gl.get_program_info_log(program) }; + if !linked_ok { + return Err(crate::PipelineError::Linkage(has_stages, msg)); + } + if !msg.is_empty() { + log::debug!("\tLink message: {msg}"); + } + + if !private_caps.contains(PrivateCapabilities::SHADER_BINDING_LAYOUT) { + // This remapping is only needed if we aren't able to put the binding layout + // in the shader. We can't remap storage buffers this way. + unsafe { gl.use_program(Some(program)) }; + for (ref name, (register, slot)) in name_binding_map { + log::trace!("Get binding {name:?} from program {program:?}"); + match register { + super::BindingRegister::UniformBuffers => { + let index = unsafe { gl.get_uniform_block_index(program, name) }.unwrap(); + log::trace!("\tBinding slot {slot} to block index {index}"); + unsafe { gl.uniform_block_binding(program, index, slot as _) }; + } + super::BindingRegister::StorageBuffers => { + let index = + unsafe { gl.get_shader_storage_block_index(program, name) }.unwrap(); + log::error!("Unable to re-map shader storage block {name} to {index}"); + return Err(crate::DeviceError::Lost.into()); + } + super::BindingRegister::Textures | super::BindingRegister::Images => { + let location = unsafe { gl.get_uniform_location(program, name) }; + unsafe { gl.uniform_1_i32(location.as_ref(), slot as _) }; + } + } + } + } + + let mut uniforms = ArrayVec::new(); + + for stage_items in immediates_items { + for item in stage_items { + let location = unsafe { gl.get_uniform_location(program, &item.access_path) }; + + log::trace!( + "immediate data item: name={}, ty={:?}, offset={}, location={:?}", + item.access_path, + item.ty, + item.offset, + location, + ); + + if let Some(location) = location { + uniforms.push(super::ImmediateDesc { + location, + offset: item.offset, + size_bytes: item.size_bytes, + ty: item.ty, + }); + } + } + } + + let first_instance_location = if has_stages.contains(wgt::ShaderStages::VERTEX) { + // If this returns none (the uniform isn't active), that's fine, we just won't set it. + unsafe { gl.get_uniform_location(program, naga::back::glsl::FIRST_INSTANCE_BINDING) } + } else { + None + }; + + Ok(Arc::new(super::PipelineInner { + program, + sampler_map, + first_instance_location, + immediates_descs: uniforms, + clip_distance_count, + })) + } +} + +impl crate::Device for super::Device { + type A = super::Api; + + unsafe fn create_buffer( + &self, + desc: &crate::BufferDescriptor, + ) -> Result<super::Buffer, crate::DeviceError> { + let target = if desc.usage.contains(wgt::BufferUses::INDEX) { + glow::ELEMENT_ARRAY_BUFFER + } else { + glow::ARRAY_BUFFER + }; + + let emulate_map = self + .shared + .workarounds + .contains(super::Workarounds::EMULATE_BUFFER_MAP) + || !self + .shared + .private_caps + .contains(PrivateCapabilities::BUFFER_ALLOCATION); + + if emulate_map && desc.usage.intersects(wgt::BufferUses::MAP_WRITE) { + return Ok(super::Buffer { + raw: None, + target, + size: desc.size, + map_flags: 0, + map_state: Arc::new(MaybeMutex::new(super::BufferMapState { + mapped: false, + data: Some(vec![0; desc.size as usize]), + offset_of_current_mapping: 0, + })), + drop_guard: None, + }); + } + + let gl = &self.shared.context.lock(); + + let target = if desc.usage.contains(wgt::BufferUses::INDEX) { + glow::ELEMENT_ARRAY_BUFFER + } else { + glow::ARRAY_BUFFER + }; + + let is_host_visible = desc + .usage + .intersects(wgt::BufferUses::MAP_READ | wgt::BufferUses::MAP_WRITE); + let is_coherent = desc + .memory_flags + .contains(crate::MemoryFlags::PREFER_COHERENT); + + let mut map_flags = 0; + if desc.usage.contains(wgt::BufferUses::MAP_READ) { + map_flags |= glow::MAP_READ_BIT; + } + if desc.usage.contains(wgt::BufferUses::MAP_WRITE) { + map_flags |= glow::MAP_WRITE_BIT; + } + + let raw = Some(unsafe { gl.create_buffer() }.map_err(|_| crate::DeviceError::OutOfMemory)?); + unsafe { gl.bind_buffer(target, raw) }; + let raw_size = desc + .size + .try_into() + .map_err(|_| crate::DeviceError::OutOfMemory)?; + + if self + .shared + .private_caps + .contains(PrivateCapabilities::BUFFER_ALLOCATION) + { + if is_host_visible { + map_flags |= glow::MAP_PERSISTENT_BIT; + if is_coherent { + map_flags |= glow::MAP_COHERENT_BIT; + } + } + // TODO: may also be required for other calls involving `buffer_sub_data_u8_slice` (e.g. copy buffer to buffer and clear buffer) + if desc.usage.intersects(wgt::BufferUses::QUERY_RESOLVE) { + map_flags |= glow::DYNAMIC_STORAGE_BIT; + } + unsafe { gl.buffer_storage(target, raw_size, None, map_flags) }; + } else { + assert!(!is_coherent); + let usage = if is_host_visible { + if desc.usage.contains(wgt::BufferUses::MAP_READ) { + glow::STREAM_READ + } else { + glow::DYNAMIC_DRAW + } + } else { + // Even if the usage doesn't contain SRC_READ, we update it internally at least once + // Some vendors take usage very literally and STATIC_DRAW will freeze us with an empty buffer + // https://github.com/gfx-rs/wgpu/issues/3371 + glow::DYNAMIC_DRAW + }; + unsafe { gl.buffer_data_size(target, raw_size, usage) }; + } + + unsafe { gl.bind_buffer(target, None) }; + + if !is_coherent && desc.usage.contains(wgt::BufferUses::MAP_WRITE) { + map_flags |= glow::MAP_FLUSH_EXPLICIT_BIT; + } + //TODO: do we need `glow::MAP_UNSYNCHRONIZED_BIT`? + + #[cfg(native)] + if let Some(label) = desc.label { + if self + .shared + .private_caps + .contains(PrivateCapabilities::DEBUG_FNS) + { + let name = raw.map_or(0, |buf| buf.0.get()); + unsafe { gl.object_label(glow::BUFFER, name, Some(label)) }; + } + } + + let data = if emulate_map && desc.usage.contains(wgt::BufferUses::MAP_READ) { + Some(vec![0; desc.size as usize]) + } else { + None + }; + + self.counters.buffers.add(1); + + Ok(super::Buffer { + raw, + target, + size: desc.size, + map_flags, + map_state: Arc::new(MaybeMutex::new(super::BufferMapState { + mapped: false, + data, + offset_of_current_mapping: 0, + })), + drop_guard: None, + }) + } + + unsafe fn destroy_buffer(&self, buffer: super::Buffer) { + if buffer.drop_guard.is_none() { + if let Some(raw) = buffer.raw { + let gl = &self.shared.context.lock(); + unsafe { gl.delete_buffer(raw) }; + } + } + + // For clarity, we explicitly drop the drop guard. Although this has no real semantic effect as the + // end of the scope will drop the drop guard since this function takes ownership of the buffer. + drop(buffer.drop_guard); + + self.counters.buffers.sub(1); + } + + unsafe fn add_raw_buffer(&self, _buffer: &super::Buffer) { + self.counters.buffers.add(1); + } + + unsafe fn map_buffer( + &self, + buffer: &super::Buffer, + range: crate::MemoryRange, + ) -> Result<crate::BufferMapping, crate::DeviceError> { + let is_coherent = buffer.map_flags & glow::MAP_COHERENT_BIT != 0; + let ptr = match buffer.raw { + None => { + let mut map_state = lock(&buffer.map_state); + let vec = map_state.data.as_mut().unwrap(); + let slice = &mut vec.as_mut_slice()[range.start as usize..range.end as usize]; + slice.as_mut_ptr() + } + Some(raw) => { + let gl = &self.shared.context.lock(); + unsafe { gl.bind_buffer(buffer.target, Some(raw)) }; + let mut map_state = lock(&buffer.map_state); + let ptr = if let Some(map_read_allocation) = map_state.data.as_mut() { + let slice = map_read_allocation.as_mut_slice(); + unsafe { self.shared.get_buffer_sub_data(gl, buffer.target, 0, slice) }; + slice.as_mut_ptr() + } else { + map_state.offset_of_current_mapping = range.start; + // glMapBufferRange throws an error if length is 0. + // We want to allow mapping 0-sized buffer slices, so perform a workaround + // if the range length is 0. The resulting pointer must never be dereferenced. + let range_start: i32 = range + .start + .try_into() + .expect("Buffer range invalid for GLES"); + let range_length: i32 = (range.end - range.start) + .try_into() + .expect("Buffer range invalid for GLES"); + if range_length != 0 { + map_state.mapped = true; + unsafe { + gl.map_buffer_range( + buffer.target, + range_start, + range_length, + buffer.map_flags, + ) + } + } else { + ptr::dangling_mut() + } + }; + unsafe { gl.bind_buffer(buffer.target, None) }; + ptr + } + }; + Ok(crate::BufferMapping { + ptr: ptr::NonNull::new(ptr).ok_or(crate::DeviceError::Lost)?, + is_coherent, + }) + } + unsafe fn unmap_buffer(&self, buffer: &super::Buffer) { + let gl = &self.shared.context.lock(); + let mut map_state = lock(&buffer.map_state); + if core::mem::replace(&mut map_state.mapped, false) { + if let Some(raw) = buffer.raw { + if map_state.data.is_none() { + unsafe { gl.bind_buffer(buffer.target, Some(raw)) }; + unsafe { gl.unmap_buffer(buffer.target) }; + unsafe { gl.bind_buffer(buffer.target, None) }; + map_state.offset_of_current_mapping = 0; + } + } + } + } + unsafe fn flush_mapped_ranges<I>(&self, buffer: &super::Buffer, ranges: I) + where + I: Iterator<Item = crate::MemoryRange>, + { + let gl = &self.shared.context.lock(); + let map_state = lock(&buffer.map_state); + if map_state.mapped { + if let Some(raw) = buffer.raw { + if map_state.data.is_none() { + unsafe { gl.bind_buffer(buffer.target, Some(raw)) }; + for range in ranges { + let offset_of_current_mapping = map_state.offset_of_current_mapping; + unsafe { + gl.flush_mapped_buffer_range( + buffer.target, + (range.start - offset_of_current_mapping) as i32, + (range.end - range.start) as i32, + ) + }; + } + } + } + } + } + unsafe fn invalidate_mapped_ranges<I>(&self, _buffer: &super::Buffer, _ranges: I) { + //TODO: do we need to do anything? + } + + unsafe fn create_texture( + &self, + desc: &crate::TextureDescriptor, + ) -> Result<super::Texture, crate::DeviceError> { + let gl = &self.shared.context.lock(); + + let render_usage = wgt::TextureUses::COLOR_TARGET + | wgt::TextureUses::DEPTH_STENCIL_WRITE + | wgt::TextureUses::DEPTH_STENCIL_READ + | wgt::TextureUses::TRANSIENT; + let format_desc = self.shared.describe_texture_format(desc.format); + + let inner = if render_usage.contains(desc.usage) + && desc.dimension == wgt::TextureDimension::D2 + && desc.size.depth_or_array_layers == 1 + { + let raw = unsafe { gl.create_renderbuffer().unwrap() }; + unsafe { gl.bind_renderbuffer(glow::RENDERBUFFER, Some(raw)) }; + if desc.sample_count > 1 { + unsafe { + gl.renderbuffer_storage_multisample( + glow::RENDERBUFFER, + desc.sample_count as i32, + format_desc.internal, + desc.size.width as i32, + desc.size.height as i32, + ) + }; + } else { + unsafe { + gl.renderbuffer_storage( + glow::RENDERBUFFER, + format_desc.internal, + desc.size.width as i32, + desc.size.height as i32, + ) + }; + } + + #[cfg(native)] + if let Some(label) = desc.label { + if self + .shared + .private_caps + .contains(PrivateCapabilities::DEBUG_FNS) + { + let name = raw.0.get(); + unsafe { gl.object_label(glow::RENDERBUFFER, name, Some(label)) }; + } + } + + unsafe { gl.bind_renderbuffer(glow::RENDERBUFFER, None) }; + super::TextureInner::Renderbuffer { raw } + } else { + let raw = unsafe { gl.create_texture().unwrap() }; + let target = super::Texture::get_info_from_desc(desc); + + unsafe { gl.bind_texture(target, Some(raw)) }; + //Note: this has to be done before defining the storage! + match desc.format.sample_type(None, Some(self.shared.features)) { + Some( + wgt::TextureSampleType::Float { filterable: false } + | wgt::TextureSampleType::Uint + | wgt::TextureSampleType::Sint, + ) => { + // reset default filtering mode + unsafe { + gl.tex_parameter_i32(target, glow::TEXTURE_MIN_FILTER, glow::NEAREST as i32) + }; + unsafe { + gl.tex_parameter_i32(target, glow::TEXTURE_MAG_FILTER, glow::NEAREST as i32) + }; + } + _ => {} + } + + if conv::is_layered_target(target) { + unsafe { + if self + .shared + .private_caps + .contains(PrivateCapabilities::TEXTURE_STORAGE) + { + gl.tex_storage_3d( + target, + desc.mip_level_count as i32, + format_desc.internal, + desc.size.width as i32, + desc.size.height as i32, + desc.size.depth_or_array_layers as i32, + ) + } else if target == glow::TEXTURE_3D { + let mut width = desc.size.width; + let mut height = desc.size.height; + let mut depth = desc.size.depth_or_array_layers; + for i in 0..desc.mip_level_count { + gl.tex_image_3d( + target, + i as i32, + format_desc.internal as i32, + width as i32, + height as i32, + depth as i32, + 0, + format_desc.external, + format_desc.data_type, + glow::PixelUnpackData::Slice(None), + ); + width = max(1, width / 2); + height = max(1, height / 2); + depth = max(1, depth / 2); + } + } else { + let mut width = desc.size.width; + let mut height = desc.size.height; + for i in 0..desc.mip_level_count { + gl.tex_image_3d( + target, + i as i32, + format_desc.internal as i32, + width as i32, + height as i32, + desc.size.depth_or_array_layers as i32, + 0, + format_desc.external, + format_desc.data_type, + glow::PixelUnpackData::Slice(None), + ); + width = max(1, width / 2); + height = max(1, height / 2); + } + } + }; + } else if desc.sample_count > 1 { + unsafe { + gl.tex_storage_2d_multisample( + target, + desc.sample_count as i32, + format_desc.internal, + desc.size.width as i32, + desc.size.height as i32, + true, + ) + }; + } else { + unsafe { + if self + .shared + .private_caps + .contains(PrivateCapabilities::TEXTURE_STORAGE) + { + gl.tex_storage_2d( + target, + desc.mip_level_count as i32, + format_desc.internal, + desc.size.width as i32, + desc.size.height as i32, + ) + } else if target == glow::TEXTURE_CUBE_MAP { + let mut width = desc.size.width; + let mut height = desc.size.height; + for i in 0..desc.mip_level_count { + for face in [ + glow::TEXTURE_CUBE_MAP_POSITIVE_X, + glow::TEXTURE_CUBE_MAP_NEGATIVE_X, + glow::TEXTURE_CUBE_MAP_POSITIVE_Y, + glow::TEXTURE_CUBE_MAP_NEGATIVE_Y, + glow::TEXTURE_CUBE_MAP_POSITIVE_Z, + glow::TEXTURE_CUBE_MAP_NEGATIVE_Z, + ] { + gl.tex_image_2d( + face, + i as i32, + format_desc.internal as i32, + width as i32, + height as i32, + 0, + format_desc.external, + format_desc.data_type, + glow::PixelUnpackData::Slice(None), + ); + } + width = max(1, width / 2); + height = max(1, height / 2); + } + } else { + let mut width = desc.size.width; + let mut height = desc.size.height; + for i in 0..desc.mip_level_count { + gl.tex_image_2d( + target, + i as i32, + format_desc.internal as i32, + width as i32, + height as i32, + 0, + format_desc.external, + format_desc.data_type, + glow::PixelUnpackData::Slice(None), + ); + width = max(1, width / 2); + height = max(1, height / 2); + } + } + }; + } + + #[cfg(native)] + if let Some(label) = desc.label { + if self + .shared + .private_caps + .contains(PrivateCapabilities::DEBUG_FNS) + { + let name = raw.0.get(); + unsafe { gl.object_label(glow::TEXTURE, name, Some(label)) }; + } + } + + unsafe { gl.bind_texture(target, None) }; + super::TextureInner::Texture { raw, target } + }; + + self.counters.textures.add(1); + + Ok(super::Texture { + inner, + drop_guard: None, + mip_level_count: desc.mip_level_count, + array_layer_count: desc.array_layer_count(), + format: desc.format, + format_desc, + copy_size: desc.copy_extent(), + }) + } + + unsafe fn destroy_texture(&self, texture: super::Texture) { + if texture.drop_guard.is_none() { + let gl = &self.shared.context.lock(); + match texture.inner { + super::TextureInner::Renderbuffer { raw, .. } => { + unsafe { gl.delete_renderbuffer(raw) }; + } + super::TextureInner::DefaultRenderbuffer => {} + super::TextureInner::Texture { raw, .. } => { + unsafe { gl.delete_texture(raw) }; + } + #[cfg(webgl)] + super::TextureInner::ExternalFramebuffer { .. } => {} + #[cfg(native)] + super::TextureInner::ExternalNativeFramebuffer { .. } => {} + } + } + + // For clarity, we explicitly drop the drop guard. Although this has no real semantic effect as the + // end of the scope will drop the drop guard since this function takes ownership of the texture. + drop(texture.drop_guard); + + self.counters.textures.sub(1); + } + + unsafe fn add_raw_texture(&self, _texture: &super::Texture) { + self.counters.textures.add(1); + } + + unsafe fn create_texture_view( + &self, + texture: &super::Texture, + desc: &crate::TextureViewDescriptor, + ) -> Result<super::TextureView, crate::DeviceError> { + self.counters.texture_views.add(1); + Ok(super::TextureView { + //TODO: use `conv::map_view_dimension(desc.dimension)`? + inner: texture.inner.clone(), + aspects: crate::FormatAspects::new(texture.format, desc.range.aspect), + mip_levels: desc.range.mip_range(texture.mip_level_count), + array_layers: desc.range.layer_range(texture.array_layer_count), + format: texture.format, + }) + } + + unsafe fn destroy_texture_view(&self, _view: super::TextureView) { + self.counters.texture_views.sub(1); + } + + unsafe fn create_sampler( + &self, + desc: &crate::SamplerDescriptor, + ) -> Result<super::Sampler, crate::DeviceError> { + let gl = &self.shared.context.lock(); + + let raw = unsafe { gl.create_sampler().unwrap() }; + + let (min, mag) = + conv::map_filter_modes(desc.min_filter, desc.mag_filter, desc.mipmap_filter); + + unsafe { gl.sampler_parameter_i32(raw, glow::TEXTURE_MIN_FILTER, min as i32) }; + unsafe { gl.sampler_parameter_i32(raw, glow::TEXTURE_MAG_FILTER, mag as i32) }; + + unsafe { + gl.sampler_parameter_i32( + raw, + glow::TEXTURE_WRAP_S, + conv::map_address_mode(desc.address_modes[0]) as i32, + ) + }; + unsafe { + gl.sampler_parameter_i32( + raw, + glow::TEXTURE_WRAP_T, + conv::map_address_mode(desc.address_modes[1]) as i32, + ) + }; + unsafe { + gl.sampler_parameter_i32( + raw, + glow::TEXTURE_WRAP_R, + conv::map_address_mode(desc.address_modes[2]) as i32, + ) + }; + + if let Some(border_color) = desc.border_color { + let border = match border_color { + wgt::SamplerBorderColor::TransparentBlack | wgt::SamplerBorderColor::Zero => { + [0.0; 4] + } + wgt::SamplerBorderColor::OpaqueBlack => [0.0, 0.0, 0.0, 1.0], + wgt::SamplerBorderColor::OpaqueWhite => [1.0; 4], + }; + unsafe { gl.sampler_parameter_f32_slice(raw, glow::TEXTURE_BORDER_COLOR, &border) }; + } + + unsafe { gl.sampler_parameter_f32(raw, glow::TEXTURE_MIN_LOD, desc.lod_clamp.start) }; + unsafe { gl.sampler_parameter_f32(raw, glow::TEXTURE_MAX_LOD, desc.lod_clamp.end) }; + + // If clamp is not 1, we know anisotropy is supported up to 16x + if desc.anisotropy_clamp != 1 { + unsafe { + gl.sampler_parameter_i32( + raw, + glow::TEXTURE_MAX_ANISOTROPY, + desc.anisotropy_clamp as i32, + ) + }; + } + + //set_param_float(glow::TEXTURE_LOD_BIAS, info.lod_bias.0); + + if let Some(compare) = desc.compare { + unsafe { + gl.sampler_parameter_i32( + raw, + glow::TEXTURE_COMPARE_MODE, + glow::COMPARE_REF_TO_TEXTURE as i32, + ) + }; + unsafe { + gl.sampler_parameter_i32( + raw, + glow::TEXTURE_COMPARE_FUNC, + conv::map_compare_func(compare) as i32, + ) + }; + } + + #[cfg(native)] + if let Some(label) = desc.label { + if self + .shared + .private_caps + .contains(PrivateCapabilities::DEBUG_FNS) + { + let name = raw.0.get(); + unsafe { gl.object_label(glow::SAMPLER, name, Some(label)) }; + } + } + + self.counters.samplers.add(1); + + Ok(super::Sampler { raw }) + } + + unsafe fn destroy_sampler(&self, sampler: super::Sampler) { + let gl = &self.shared.context.lock(); + unsafe { gl.delete_sampler(sampler.raw) }; + self.counters.samplers.sub(1); + } + + unsafe fn create_command_encoder( + &self, + _desc: &crate::CommandEncoderDescriptor<super::Queue>, + ) -> Result<super::CommandEncoder, crate::DeviceError> { + self.counters.command_encoders.add(1); + + Ok(super::CommandEncoder { + cmd_buffer: super::CommandBuffer::default(), + state: Default::default(), + private_caps: self.shared.private_caps, + counters: Arc::clone(&self.counters), + }) + } + + unsafe fn create_bind_group_layout( + &self, + desc: &crate::BindGroupLayoutDescriptor, + ) -> Result<super::BindGroupLayout, crate::DeviceError> { + self.counters.bind_group_layouts.add(1); + Ok(super::BindGroupLayout { + entries: Arc::from(desc.entries), + }) + } + + unsafe fn destroy_bind_group_layout(&self, _bg_layout: super::BindGroupLayout) { + self.counters.bind_group_layouts.sub(1); + } + + unsafe fn create_pipeline_layout( + &self, + desc: &crate::PipelineLayoutDescriptor<super::BindGroupLayout>, + ) -> Result<super::PipelineLayout, crate::DeviceError> { + use naga::back::glsl; + + let mut group_infos = Vec::with_capacity(desc.bind_group_layouts.len()); + let mut num_samplers = 0u8; + let mut num_textures = 0u8; + let mut num_images = 0u8; + let mut num_uniform_buffers = 0u8; + let mut num_storage_buffers = 0u8; + + let mut writer_flags = glsl::WriterFlags::ADJUST_COORDINATE_SPACE; + writer_flags.set( + glsl::WriterFlags::TEXTURE_SHADOW_LOD, + self.shared + .private_caps + .contains(PrivateCapabilities::SHADER_TEXTURE_SHADOW_LOD), + ); + writer_flags.set( + glsl::WriterFlags::DRAW_PARAMETERS, + self.shared + .private_caps + .contains(PrivateCapabilities::FULLY_FEATURED_INSTANCING), + ); + // We always force point size to be written and it will be ignored by the driver if it's not a point list primitive. + // https://github.com/gfx-rs/wgpu/pull/3440/files#r1095726950 + writer_flags.set(glsl::WriterFlags::FORCE_POINT_SIZE, true); + let mut binding_map = glsl::BindingMap::default(); + + for (group_index, bg_layout) in desc.bind_group_layouts.iter().enumerate() { + let Some(bg_layout) = bg_layout else { + group_infos.push(None); + continue; + }; + + // create a vector with the size enough to hold all the bindings, filled with `!0` + let mut binding_to_slot = vec![ + !0; + bg_layout + .entries + .iter() + .map(|b| b.binding) + .max() + .map_or(0, |idx| idx as usize + 1) + ] + .into_boxed_slice(); + + for entry in bg_layout.entries.iter() { + let counter = match entry.ty { + wgt::BindingType::Sampler { .. } => &mut num_samplers, + wgt::BindingType::Texture { .. } => &mut num_textures, + wgt::BindingType::StorageTexture { .. } => &mut num_images, + wgt::BindingType::Buffer { + ty: wgt::BufferBindingType::Uniform, + .. + } => &mut num_uniform_buffers, + wgt::BindingType::Buffer { + ty: wgt::BufferBindingType::Storage { .. }, + .. + } => &mut num_storage_buffers, + wgt::BindingType::AccelerationStructure { .. } => unimplemented!(), + wgt::BindingType::ExternalTexture => unimplemented!(), + }; + + binding_to_slot[entry.binding as usize] = *counter; + let br = naga::ResourceBinding { + group: group_index as u32, + binding: entry.binding, + }; + binding_map.insert(br, *counter); + *counter += entry.count.map_or(1, |c| c.get() as u8); + } + + group_infos.push(Some(super::BindGroupLayoutInfo { + entries: Arc::clone(&bg_layout.entries), + binding_to_slot, + })); + } + + self.counters.pipeline_layouts.add(1); + + Ok(super::PipelineLayout { + group_infos: group_infos.into_boxed_slice(), + naga_options: glsl::Options { + version: self.shared.shading_language_version, + writer_flags, + binding_map, + zero_initialize_workgroup_memory: true, + }, + }) + } + + unsafe fn destroy_pipeline_layout(&self, _pipeline_layout: super::PipelineLayout) { + self.counters.pipeline_layouts.sub(1); + } + + unsafe fn create_bind_group( + &self, + desc: &crate::BindGroupDescriptor< + super::BindGroupLayout, + super::Buffer, + super::Sampler, + super::TextureView, + super::AccelerationStructure, + >, + ) -> Result<super::BindGroup, crate::DeviceError> { + let mut contents = Vec::new(); + + let layout_and_entry_iter = desc.entries.iter().map(|entry| { + let layout = desc + .layout + .entries + .iter() + .find(|layout_entry| layout_entry.binding == entry.binding) + .expect("internal error: no layout entry found with binding slot"); + (entry, layout) + }); + for (entry, layout) in layout_and_entry_iter { + let binding = match layout.ty { + wgt::BindingType::Buffer { .. } => { + let bb = &desc.buffers[entry.resource_index as usize]; + super::RawBinding::Buffer { + raw: bb.buffer.raw.unwrap(), + offset: bb.offset as i32, + size: match bb.size { + Some(s) => s.get() as i32, + None => (bb.buffer.size - bb.offset) as i32, + }, + } + } + wgt::BindingType::Sampler { .. } => { + let sampler = desc.samplers[entry.resource_index as usize]; + super::RawBinding::Sampler(sampler.raw) + } + wgt::BindingType::Texture { view_dimension, .. } => { + let view = desc.textures[entry.resource_index as usize].view; + if view.array_layers.start != 0 { + log::error!("Unable to create a sampled texture binding for non-zero array layer.\n{}", + "This is an implementation problem of wgpu-hal/gles backend.") + } + let (raw, target) = view.inner.as_native(); + + super::Texture::log_failing_target_heuristics(view_dimension, target); + + super::RawBinding::Texture { + raw, + target, + aspects: view.aspects, + mip_levels: view.mip_levels.clone(), + } + } + wgt::BindingType::StorageTexture { + access, + format, + view_dimension, + } => { + let view = desc.textures[entry.resource_index as usize].view; + let format_desc = self.shared.describe_texture_format(format); + let (raw, _target) = view.inner.as_native(); + super::RawBinding::Image(super::ImageBinding { + raw, + mip_level: view.mip_levels.start, + array_layer: match view_dimension { + wgt::TextureViewDimension::D2Array + | wgt::TextureViewDimension::CubeArray => None, + _ => Some(view.array_layers.start), + }, + access: conv::map_storage_access(access), + format: format_desc.internal, + }) + } + wgt::BindingType::AccelerationStructure { .. } => unimplemented!(), + wgt::BindingType::ExternalTexture => unimplemented!(), + }; + contents.push(binding); + } + + self.counters.bind_groups.add(1); + + Ok(super::BindGroup { + contents: contents.into_boxed_slice(), + }) + } + + unsafe fn destroy_bind_group(&self, _group: super::BindGroup) { + self.counters.bind_groups.sub(1); + } + + unsafe fn create_shader_module( + &self, + desc: &crate::ShaderModuleDescriptor, + shader: crate::ShaderInput, + ) -> Result<super::ShaderModule, crate::ShaderError> { + self.counters.shader_modules.add(1); + + Ok(super::ShaderModule { + source: match shader { + crate::ShaderInput::Naga(naga) => super::ShaderModuleSource::Naga(naga), + // The backend doesn't yet expose this feature so it should be fine + crate::ShaderInput::Glsl { shader, .. } => super::ShaderModuleSource::Passthrough { + source: shader.to_owned(), + }, + crate::ShaderInput::SpirV(_) + | crate::ShaderInput::MetalLib { .. } + | crate::ShaderInput::Msl { .. } + | crate::ShaderInput::Dxil { .. } + | crate::ShaderInput::Hlsl { .. } => { + unreachable!() + } + }, + label: desc.label.map(|str| str.to_string()), + id: self.shared.next_shader_id.fetch_add(1, Ordering::Relaxed), + }) + } + + unsafe fn destroy_shader_module(&self, _module: super::ShaderModule) { + self.counters.shader_modules.sub(1); + } + + unsafe fn create_render_pipeline( + &self, + desc: &crate::RenderPipelineDescriptor< + super::PipelineLayout, + super::ShaderModule, + super::PipelineCache, + >, + ) -> Result<super::RenderPipeline, crate::PipelineError> { + let (vertex_stage, vertex_buffers) = match &desc.vertex_processor { + crate::VertexProcessor::Standard { + vertex_buffers, + ref vertex_stage, + } => (vertex_stage, vertex_buffers), + crate::VertexProcessor::Mesh { .. } => unreachable!(), + }; + let gl = &self.shared.context.lock(); + let mut shaders = ArrayVec::new(); + shaders.push((naga::ShaderStage::Vertex, vertex_stage)); + if let Some(ref fs) = desc.fragment_stage { + shaders.push((naga::ShaderStage::Fragment, fs)); + } + let inner = unsafe { + self.create_pipeline(gl, shaders, desc.layout, desc.label, desc.multiview_mask) + }?; + + let (vertex_buffers, vertex_attributes) = { + let mut buffers = Vec::new(); + let mut attributes = Vec::new(); + for (index, vb_layout) in vertex_buffers.iter().enumerate() { + let vb_desc = if let Some(vb_layout) = vb_layout { + for vat in vb_layout.attributes.iter() { + let format_desc = conv::describe_vertex_format(vat.format); + attributes.push(super::AttributeDesc { + location: vat.shader_location, + offset: vat.offset as u32, + buffer_index: index as u32, + format_desc, + }); + } + Some(super::VertexBufferDesc { + step: vb_layout.step_mode, + stride: vb_layout.array_stride as u32, + }) + } else { + None + }; + buffers.push(vb_desc); + } + (buffers.into_boxed_slice(), attributes.into_boxed_slice()) + }; + + let color_targets = { + let mut targets = Vec::new(); + for ct in desc.color_targets.iter().filter_map(|at| at.as_ref()) { + targets.push(super::ColorTargetDesc { + mask: ct.write_mask, + blend: ct.blend.as_ref().map(conv::map_blend), + }); + } + //Note: if any of the states are different, and `INDEPENDENT_BLEND` flag + // is not exposed, then this pipeline will not bind correctly. + targets.into_boxed_slice() + }; + + self.counters.render_pipelines.add(1); + + Ok(super::RenderPipeline { + inner, + primitive: desc.primitive, + vertex_buffers, + vertex_attributes, + color_targets, + depth: desc.depth_stencil.as_ref().map(|ds| super::DepthState { + function: conv::map_compare_func(ds.depth_compare.unwrap_or_default()), + mask: ds.depth_write_enabled.unwrap_or_default(), + }), + depth_bias: desc + .depth_stencil + .as_ref() + .map(|ds| ds.bias) + .unwrap_or_default(), + stencil: desc + .depth_stencil + .as_ref() + .map(|ds| conv::map_stencil(&ds.stencil)), + alpha_to_coverage_enabled: desc.multisample.alpha_to_coverage_enabled, + }) + } + + unsafe fn destroy_render_pipeline(&self, pipeline: super::RenderPipeline) { + // If the pipeline only has 2 strong references remaining, they're `pipeline` and `program_cache` + // This is safe to assume as long as: + // - `RenderPipeline` can't be cloned + // - The only place that we can get a new reference is during `program_cache.lock()` + if Arc::strong_count(&pipeline.inner) == 2 { + let gl = &self.shared.context.lock(); + let mut program_cache = self.shared.program_cache.lock(); + program_cache.retain(|_, v| match *v { + Ok(ref p) => p.program != pipeline.inner.program, + Err(_) => false, + }); + unsafe { gl.delete_program(pipeline.inner.program) }; + } + + self.counters.render_pipelines.sub(1); + } + + unsafe fn create_compute_pipeline( + &self, + desc: &crate::ComputePipelineDescriptor< + super::PipelineLayout, + super::ShaderModule, + super::PipelineCache, + >, + ) -> Result<super::ComputePipeline, crate::PipelineError> { + let gl = &self.shared.context.lock(); + let mut shaders = ArrayVec::new(); + shaders.push((naga::ShaderStage::Compute, &desc.stage)); + let inner = unsafe { self.create_pipeline(gl, shaders, desc.layout, desc.label, None) }?; + + self.counters.compute_pipelines.add(1); + + Ok(super::ComputePipeline { inner }) + } + + unsafe fn destroy_compute_pipeline(&self, pipeline: super::ComputePipeline) { + // If the pipeline only has 2 strong references remaining, they're `pipeline` and `program_cache`` + // This is safe to assume as long as: + // - `ComputePipeline` can't be cloned + // - The only place that we can get a new reference is during `program_cache.lock()` + if Arc::strong_count(&pipeline.inner) == 2 { + let gl = &self.shared.context.lock(); + let mut program_cache = self.shared.program_cache.lock(); + program_cache.retain(|_, v| match *v { + Ok(ref p) => p.program != pipeline.inner.program, + Err(_) => false, + }); + unsafe { gl.delete_program(pipeline.inner.program) }; + } + + self.counters.compute_pipelines.sub(1); + } + + unsafe fn create_ray_tracing_pipeline( + &self, + _desc: &crate::RayTracingPipelineDescriptor< + super::PipelineLayout, + super::ShaderModule, + super::PipelineCache, + >, + ) -> Result<super::RayTracingPipeline, crate::PipelineError> { + unimplemented!("Ray tracing is unsupported on GL") + } + + unsafe fn destroy_ray_tracing_pipeline(&self, _pipeline: super::RayTracingPipeline) { + unimplemented!("Ray tracing is unsupported on GL") + } + + unsafe fn get_raytracing_pipeline_group_data( + &self, + _pipeline: &super::RayTracingPipeline, + _groups: core::ops::Range<u32>, + ) -> Result<Vec<u8>, crate::DeviceError> { + unimplemented!("Ray tracing is unsupported on GL") + } + + unsafe fn create_pipeline_cache( + &self, + _: &crate::PipelineCacheDescriptor<'_>, + ) -> Result<super::PipelineCache, crate::PipelineCacheError> { + // Even though the cache doesn't do anything, we still return something here + // as the least bad option + Ok(super::PipelineCache) + } + unsafe fn destroy_pipeline_cache(&self, _: super::PipelineCache) {} + + #[cfg_attr(target_arch = "wasm32", allow(unused))] + unsafe fn create_query_set( + &self, + desc: &wgt::QuerySetDescriptor<crate::Label>, + ) -> Result<super::QuerySet, crate::DeviceError> { + let gl = &self.shared.context.lock(); + + let mut queries = Vec::with_capacity(desc.count as usize); + for _ in 0..desc.count { + let query = + unsafe { gl.create_query() }.map_err(|_| crate::DeviceError::OutOfMemory)?; + + // We aren't really able to, in general, label queries. + // + // We could take a timestamp here to "initialize" the query, + // but that's a bit of a hack, and we don't want to insert + // random timestamps into the command stream of we don't have to. + + queries.push(query); + } + + self.counters.query_sets.add(1); + + Ok(super::QuerySet { + queries: queries.into_boxed_slice(), + target: match desc.ty { + wgt::QueryType::Occlusion => glow::ANY_SAMPLES_PASSED_CONSERVATIVE, + wgt::QueryType::Timestamp => glow::TIMESTAMP, + _ => unimplemented!(), + }, + }) + } + + unsafe fn destroy_query_set(&self, set: super::QuerySet) { + let gl = &self.shared.context.lock(); + for &query in set.queries.iter() { + unsafe { gl.delete_query(query) }; + } + self.counters.query_sets.sub(1); + } + + unsafe fn create_fence(&self) -> Result<super::Fence, crate::DeviceError> { + self.counters.fences.add(1); + Ok(super::Fence::new(&self.shared.options)) + } + + unsafe fn destroy_fence(&self, fence: super::Fence) { + let gl = &self.shared.context.lock(); + fence.destroy(gl); + self.counters.fences.sub(1); + } + + unsafe fn get_fence_value( + &self, + fence: &super::Fence, + ) -> Result<crate::FenceValue, crate::DeviceError> { + #[cfg_attr(target_arch = "wasm32", allow(clippy::needless_borrow))] + Ok(fence.get_latest(&self.shared.context.lock())) + } + unsafe fn wait( + &self, + fence: &super::Fence, + wait_value: crate::FenceValue, + timeout: Option<core::time::Duration>, + ) -> Result<bool, crate::DeviceError> { + if fence.satisfied(wait_value) { + return Ok(true); + } + + let gl = &self.shared.context.lock(); + // MAX_CLIENT_WAIT_TIMEOUT_WEBGL is: + // - 1s in Gecko https://searchfox.org/mozilla-central/rev/754074e05178e017ef6c3d8e30428ffa8f1b794d/dom/canvas/WebGLTypes.h#1386 + // - 0 in WebKit https://github.com/WebKit/WebKit/blob/4ef90d4672ca50267c0971b85db403d9684508ea/Source/WebCore/html/canvas/WebGL2RenderingContext.cpp#L110 + // - 0 in Chromium https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/modules/webgl/webgl2_rendering_context_base.cc;l=112;drc=a3cb0ac4c71ec04abfeaed199e5d63230eca2551 + let timeout_ns = if cfg!(any(webgl, Emscripten)) { + 0 + } else { + timeout + .map(|t| t.as_nanos().min(u32::MAX as u128) as u32) + .unwrap_or(u32::MAX) + }; + fence.wait(gl, wait_value, timeout_ns) + } + + unsafe fn start_graphics_debugger_capture(&self) -> bool { + #[cfg(all(native, feature = "renderdoc"))] + return unsafe { + self.render_doc + .start_frame_capture(self.shared.context.raw_context(), ptr::null_mut()) + }; + #[allow(unreachable_code)] + false + } + unsafe fn stop_graphics_debugger_capture(&self) { + #[cfg(all(native, feature = "renderdoc"))] + unsafe { + self.render_doc + .end_frame_capture(ptr::null_mut(), ptr::null_mut()) + } + } + unsafe fn create_acceleration_structure( + &self, + _desc: &crate::AccelerationStructureDescriptor, + ) -> Result<super::AccelerationStructure, crate::DeviceError> { + unimplemented!() + } + unsafe fn get_acceleration_structure_build_sizes<'a>( + &self, + _desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, super::Buffer>, + ) -> crate::AccelerationStructureBuildSizes { + unimplemented!() + } + unsafe fn get_acceleration_structure_device_address( + &self, + _acceleration_structure: &super::AccelerationStructure, + ) -> wgt::BufferAddress { + unimplemented!() + } + unsafe fn destroy_acceleration_structure( + &self, + _acceleration_structure: super::AccelerationStructure, + ) { + } + + fn tlas_instance_to_bytes(&self, _instance: TlasInstance) -> Vec<u8> { + unimplemented!() + } + + fn get_internal_counters(&self) -> wgt::HalCounters { + self.counters.as_ref().clone() + } + + fn check_if_oom(&self) -> Result<(), crate::DeviceError> { + Ok(()) + } +} + +#[cfg(send_sync)] +unsafe impl Sync for super::Device {} +#[cfg(send_sync)] +unsafe impl Send for super::Device {} diff --git a/third_party/wgpu-hal/src/gles/egl.rs b/third_party/wgpu-hal/src/gles/egl.rs new file mode 100644 index 000000000..eeb73694f --- /dev/null +++ b/third_party/wgpu-hal/src/gles/egl.rs @@ -0,0 +1,1470 @@ +use alloc::{string::String, sync::Arc, vec::Vec}; +use core::{ffi, mem::ManuallyDrop, ptr, time::Duration}; +use std::sync::LazyLock; + +use glow::HasContext; +use hashbrown::HashMap; +use parking_lot::{MappedMutexGuard, Mutex, MutexGuard, RwLock}; + +/// The amount of time to wait while trying to obtain a lock to the adapter context +const CONTEXT_LOCK_TIMEOUT_SECS: u64 = 6; + +const EGL_CONTEXT_FLAGS_KHR: i32 = 0x30FC; +const EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR: i32 = 0x0001; +const EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT: i32 = 0x30BF; +const EGL_PLATFORM_WAYLAND_KHR: u32 = 0x31D8; +const EGL_PLATFORM_X11_KHR: u32 = 0x31D5; +const EGL_PLATFORM_XCB_EXT: u32 = 0x31DC; +const EGL_PLATFORM_XCB_SCREEN_EXT: u32 = 0x31DE; +const EGL_PLATFORM_ANGLE_ANGLE: u32 = 0x3202; +const EGL_PLATFORM_ANGLE_NATIVE_PLATFORM_TYPE_ANGLE: u32 = 0x348F; +const EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED: u32 = 0x3451; +const EGL_PLATFORM_SURFACELESS_MESA: u32 = 0x31DD; +const EGL_GL_COLORSPACE_KHR: u32 = 0x309D; +const EGL_GL_COLORSPACE_SRGB_KHR: u32 = 0x3089; + +#[cfg(not(Emscripten))] +type EglInstance = khronos_egl::DynamicInstance<khronos_egl::EGL1_4>; + +#[cfg(Emscripten)] +type EglInstance = khronos_egl::Instance<khronos_egl::Static>; + +type EglLabel = *const ffi::c_void; + +#[allow(clippy::upper_case_acronyms)] +type EGLDEBUGPROCKHR = Option< + unsafe extern "system" fn( + error: khronos_egl::Enum, + command: *const ffi::c_char, + message_type: u32, + thread_label: EglLabel, + object_label: EglLabel, + message: *const ffi::c_char, + ), +>; + +const EGL_DEBUG_MSG_CRITICAL_KHR: u32 = 0x33B9; +const EGL_DEBUG_MSG_ERROR_KHR: u32 = 0x33BA; +const EGL_DEBUG_MSG_WARN_KHR: u32 = 0x33BB; +const EGL_DEBUG_MSG_INFO_KHR: u32 = 0x33BC; + +type EglDebugMessageControlFun = unsafe extern "system" fn( + proc: EGLDEBUGPROCKHR, + attrib_list: *const khronos_egl::Attrib, +) -> ffi::c_int; + +unsafe extern "system" fn egl_debug_proc( + error: khronos_egl::Enum, + command_raw: *const ffi::c_char, + message_type: u32, + _thread_label: EglLabel, + _object_label: EglLabel, + message_raw: *const ffi::c_char, +) { + let log_severity = match message_type { + EGL_DEBUG_MSG_CRITICAL_KHR | EGL_DEBUG_MSG_ERROR_KHR => log::Level::Error, + EGL_DEBUG_MSG_WARN_KHR => log::Level::Warn, + // We intentionally suppress info messages down to debug + // so that users are not inundated with info messages from + // the runtime. + EGL_DEBUG_MSG_INFO_KHR => log::Level::Debug, + _ => log::Level::Trace, + }; + let command = unsafe { ffi::CStr::from_ptr(command_raw) }.to_string_lossy(); + let message = if message_raw.is_null() { + "".into() + } else { + unsafe { ffi::CStr::from_ptr(message_raw) }.to_string_lossy() + }; + + log::log!(log_severity, "EGL '{command}' code 0x{error:x}: {message}",); +} + +#[derive(Clone, Copy, Debug)] +enum SrgbFrameBufferKind { + /// No support for SRGB surface + None, + /// Using EGL 1.5's support for colorspaces + Core, + /// Using EGL_KHR_gl_colorspace + Khr, +} + +/// Choose GLES framebuffer configuration. +fn choose_config( + egl: &EglInstance, + display: khronos_egl::Display, + srgb_kind: SrgbFrameBufferKind, +) -> Result<(khronos_egl::Config, bool), crate::InstanceError> { + //TODO: EGL_SLOW_CONFIG + let tiers = [ + ( + "off-screen", + &[ + khronos_egl::SURFACE_TYPE, + khronos_egl::PBUFFER_BIT, + khronos_egl::RENDERABLE_TYPE, + khronos_egl::OPENGL_ES2_BIT, + ][..], + ), + ( + "presentation", + &[khronos_egl::SURFACE_TYPE, khronos_egl::WINDOW_BIT][..], + ), + #[cfg(not(target_os = "android"))] + ( + "native-render", + &[khronos_egl::NATIVE_RENDERABLE, khronos_egl::TRUE as _][..], + ), + ]; + + let mut attributes = Vec::with_capacity(9); + for tier_max in (0..tiers.len()).rev() { + let name = tiers[tier_max].0; + log::debug!("\tTrying {name}"); + + attributes.clear(); + for &(_, tier_attr) in tiers[..=tier_max].iter() { + attributes.extend_from_slice(tier_attr); + } + // make sure the Alpha is enough to support sRGB + match srgb_kind { + SrgbFrameBufferKind::None => {} + _ => { + attributes.push(khronos_egl::ALPHA_SIZE); + attributes.push(8); + } + } + attributes.push(khronos_egl::NONE); + + match egl.choose_first_config(display, &attributes) { + Ok(Some(config)) => { + if tier_max == 1 { + //Note: this has been confirmed to malfunction on Intel+NV laptops, + // but also on Angle. + log::info!("EGL says it can present to the window but not natively",); + } + // Android emulator can't natively present either. + let tier_threshold = + if cfg!(target_os = "android") || cfg!(windows) || cfg!(target_env = "ohos") { + 1 + } else { + 2 + }; + return Ok((config, tier_max >= tier_threshold)); + } + Ok(None) => { + log::debug!("No config found!"); + } + Err(e) => { + log::error!("error in choose_first_config: {e:?}"); + } + } + } + + // TODO: include diagnostic details that are currently logged + Err(crate::InstanceError::new(String::from( + "unable to find an acceptable EGL framebuffer configuration", + ))) +} + +#[derive(Clone, Debug)] +struct EglContext { + instance: Arc<EglInstance>, + version: (i32, i32), + display: khronos_egl::Display, + raw: khronos_egl::Context, + pbuffer: Option<khronos_egl::Surface>, +} + +impl EglContext { + fn make_current(&self) { + self.instance + .make_current(self.display, self.pbuffer, self.pbuffer, Some(self.raw)) + .unwrap(); + } + + fn unmake_current(&self) { + self.instance + .make_current(self.display, None, None, None) + .unwrap(); + } +} + +/// A wrapper around a [`glow::Context`] and the required EGL context that uses locking to guarantee +/// exclusive access when shared with multiple threads. +#[derive(Debug)] +pub struct AdapterContext { + glow: Mutex<ManuallyDrop<glow::Context>>, + egl: Option<EglContext>, +} + +unsafe impl Sync for AdapterContext {} +unsafe impl Send for AdapterContext {} + +impl AdapterContext { + pub fn is_owned(&self) -> bool { + self.egl.is_some() + } + + /// Returns the EGL instance. + /// + /// This provides access to EGL functions and the ability to load GL and EGL extension functions. + pub fn egl_instance(&self) -> Option<&EglInstance> { + self.egl.as_ref().map(|egl| &*egl.instance) + } + + /// Returns the EGLDisplay corresponding to the adapter context. + /// + /// Returns [`None`] if the adapter was externally created. + pub fn raw_display(&self) -> Option<&khronos_egl::Display> { + self.egl.as_ref().map(|egl| &egl.display) + } + + /// Returns the EGL version the adapter context was created with. + /// + /// Returns [`None`] if the adapter was externally created. + pub fn egl_version(&self) -> Option<(i32, i32)> { + self.egl.as_ref().map(|egl| egl.version) + } + + pub fn raw_context(&self) -> *mut ffi::c_void { + match self.egl { + Some(ref egl) => egl.raw.as_ptr(), + None => ptr::null_mut(), + } + } +} + +impl Drop for AdapterContext { + fn drop(&mut self) { + struct CurrentGuard<'a>(&'a EglContext); + impl Drop for CurrentGuard<'_> { + fn drop(&mut self) { + self.0.unmake_current(); + } + } + + // Context must be current when dropped. See safety docs on + // `glow::HasContext`. + // + // NOTE: This is only set to `None` by `Adapter::new_external` which + // requires the context to be current when anything that may be holding + // the `Arc<AdapterShared>` is dropped. + let _guard = self.egl.as_ref().map(|egl| { + egl.make_current(); + CurrentGuard(egl) + }); + let glow = self.glow.get_mut(); + // SAFETY: Field not used after this. + unsafe { ManuallyDrop::drop(glow) }; + } +} + +struct EglContextLock<'a> { + instance: &'a Arc<EglInstance>, + display: khronos_egl::Display, +} + +/// A guard containing a lock to an [`AdapterContext`], while the GL context is kept current. +#[expect(missing_debug_implementations)] +pub struct AdapterContextLock<'a> { + glow: MutexGuard<'a, ManuallyDrop<glow::Context>>, + egl: Option<EglContextLock<'a>>, +} + +impl<'a> core::ops::Deref for AdapterContextLock<'a> { + type Target = glow::Context; + + fn deref(&self) -> &Self::Target { + &self.glow + } +} + +impl<'a> Drop for AdapterContextLock<'a> { + fn drop(&mut self) { + if let Some(egl) = self.egl.take() { + if let Err(err) = egl.instance.make_current(egl.display, None, None, None) { + log::error!("Failed to make EGL context current: {err:?}"); + } + } + } +} + +impl AdapterContext { + /// Get's the [`glow::Context`] without waiting for a lock + /// + /// # Safety + /// + /// This should only be called when you have manually made sure that the current thread has made + /// the EGL context current and that no other thread also has the EGL context current. + /// Additionally, you must manually make the EGL context **not** current after you are done with + /// it, so that future calls to `lock()` will not fail. + /// + /// > **Note:** Calling this function **will** still lock the [`glow::Context`] which adds an + /// > extra safe-guard against accidental concurrent access to the context. + pub unsafe fn get_without_egl_lock(&self) -> MappedMutexGuard<'_, glow::Context> { + let guard = self + .glow + .try_lock_for(Duration::from_secs(CONTEXT_LOCK_TIMEOUT_SECS)) + .expect("Could not lock adapter context. This is most-likely a deadlock."); + MutexGuard::map(guard, |glow| &mut **glow) + } + + /// Obtain a lock to the EGL context and get handle to the [`glow::Context`] that can be used to + /// do rendering. + #[track_caller] + pub fn lock<'a>(&'a self) -> AdapterContextLock<'a> { + let glow = self + .glow + // Don't lock forever. If it takes longer than 1 second to get the lock we've got a + // deadlock and should panic to show where we got stuck + .try_lock_for(Duration::from_secs(CONTEXT_LOCK_TIMEOUT_SECS)) + .expect("Could not lock adapter context. This is most-likely a deadlock."); + + let egl = self.egl.as_ref().map(|egl| { + egl.make_current(); + EglContextLock { + instance: &egl.instance, + display: egl.display, + } + }); + + AdapterContextLock { glow, egl } + } +} + +#[derive(Debug)] +struct Inner { + /// Note: the context contains a dummy pbuffer (1x1). + /// Required for `eglMakeCurrent` on platforms that doesn't supports `EGL_KHR_surfaceless_context`. + egl: EglContext, + version: (i32, i32), + supports_native_window: bool, + config: khronos_egl::Config, + /// Method by which the framebuffer should support srgb + srgb_kind: SrgbFrameBufferKind, +} + +#[cfg(send_sync)] +unsafe impl Send for Inner {} +#[cfg(send_sync)] +unsafe impl Sync for Inner {} + +// Different calls to `eglGetPlatformDisplay` may return the same `Display`, making it a global +// state of all our `EglContext`s. This forces us to track the number of such context to prevent +// terminating the display if it's currently used by another `EglContext`. +static DISPLAYS_REFERENCE_COUNT: LazyLock<Mutex<HashMap<usize, usize>>> = + LazyLock::new(Default::default); + +fn initialize_display( + egl: &EglInstance, + display: khronos_egl::Display, +) -> Result<(i32, i32), khronos_egl::Error> { + let mut guard = DISPLAYS_REFERENCE_COUNT.lock(); + *guard.entry(display.as_ptr() as usize).or_default() += 1; + + // We don't need to check the reference count here since according to the `eglInitialize` + // documentation, initializing an already initialized EGL display connection has no effect + // besides returning the version numbers. + egl.initialize(display) +} + +fn terminate_display( + egl: &EglInstance, + display: khronos_egl::Display, +) -> Result<(), khronos_egl::Error> { + let key = &(display.as_ptr() as usize); + let mut guard = DISPLAYS_REFERENCE_COUNT.lock(); + let count_ref = guard + .get_mut(key) + .expect("Attempted to decref a display before incref was called"); + + if *count_ref > 1 { + *count_ref -= 1; + + Ok(()) + } else { + guard.remove(key); + + egl.terminate(display) + } +} + +fn instance_err<E: core::error::Error + Send + Sync + 'static>( + message: impl Into<String>, +) -> impl FnOnce(E) -> crate::InstanceError { + move |e| crate::InstanceError::with_source(message.into(), e) +} + +impl Inner { + fn create( + flags: wgt::InstanceFlags, + egl: Arc<EglInstance>, + display: khronos_egl::Display, + force_gles_minor_version: wgt::Gles3MinorVersion, + ) -> Result<Self, crate::InstanceError> { + let version = initialize_display(&egl, display) + .map_err(instance_err("failed to initialize EGL display connection"))?; + let vendor = egl + .query_string(Some(display), khronos_egl::VENDOR) + .map_err(instance_err("failed to query EGL vendor"))?; + let display_extensions = egl + .query_string(Some(display), khronos_egl::EXTENSIONS) + .map_err(instance_err("failed to query EGL display extensions"))? + .to_string_lossy(); + log::debug!("Display vendor {vendor:?}, version {version:?}",); + log::debug!( + "Display extensions: {:#?}", + display_extensions.split_whitespace().collect::<Vec<_>>() + ); + + let srgb_kind = if version >= (1, 5) { + log::debug!("\tEGL surface: +srgb"); + SrgbFrameBufferKind::Core + } else if display_extensions.contains("EGL_KHR_gl_colorspace") { + log::debug!("\tEGL surface: +srgb khr"); + SrgbFrameBufferKind::Khr + } else { + log::debug!("\tEGL surface: -srgb"); + SrgbFrameBufferKind::None + }; + + if log::max_level() >= log::LevelFilter::Trace { + log::trace!("Configurations:"); + let config_count = egl + .get_config_count(display) + .map_err(instance_err("failed to get config count"))?; + let mut configurations = Vec::with_capacity(config_count); + egl.get_configs(display, &mut configurations) + .map_err(instance_err("failed to get configs"))?; + for &config in configurations.iter() { + log::trace!("\tCONFORMANT=0x{:X?}, RENDERABLE=0x{:X?}, NATIVE_RENDERABLE=0x{:X?}, SURFACE_TYPE=0x{:X?}, ALPHA_SIZE={:?}", + egl.get_config_attrib(display, config, khronos_egl::CONFORMANT), + egl.get_config_attrib(display, config, khronos_egl::RENDERABLE_TYPE), + egl.get_config_attrib(display, config, khronos_egl::NATIVE_RENDERABLE), + egl.get_config_attrib(display, config, khronos_egl::SURFACE_TYPE), + egl.get_config_attrib(display, config, khronos_egl::ALPHA_SIZE), + ); + } + } + + let (config, supports_native_window) = choose_config(&egl, display, srgb_kind)?; + + let supports_opengl = if version >= (1, 4) { + let client_apis = egl + .query_string(Some(display), khronos_egl::CLIENT_APIS) + .map_err(instance_err("failed to query EGL client APIs string"))? + .to_string_lossy(); + client_apis + .split(' ') + .any(|client_api| client_api == "OpenGL") + } else { + false + }; + + let mut khr_context_flags = 0; + let supports_khr_context = display_extensions.contains("EGL_KHR_create_context"); + + let mut context_attributes = vec![]; + let mut gl_context_attributes = vec![]; + let mut gles_context_attributes = vec![]; + gl_context_attributes.push(khronos_egl::CONTEXT_MAJOR_VERSION); + gl_context_attributes.push(3); + gl_context_attributes.push(khronos_egl::CONTEXT_MINOR_VERSION); + gl_context_attributes.push(3); + if supports_opengl && force_gles_minor_version != wgt::Gles3MinorVersion::Automatic { + log::warn!("Ignoring specified GLES minor version as OpenGL is used"); + } + gles_context_attributes.push(khronos_egl::CONTEXT_MAJOR_VERSION); + gles_context_attributes.push(3); // Request GLES 3.0 or higher + if force_gles_minor_version != wgt::Gles3MinorVersion::Automatic { + gles_context_attributes.push(khronos_egl::CONTEXT_MINOR_VERSION); + gles_context_attributes.push(match force_gles_minor_version { + wgt::Gles3MinorVersion::Automatic => unreachable!(), + wgt::Gles3MinorVersion::Version0 => 0, + wgt::Gles3MinorVersion::Version1 => 1, + wgt::Gles3MinorVersion::Version2 => 2, + }); + } + if flags.contains(wgt::InstanceFlags::DEBUG) { + if version >= (1, 5) { + log::debug!("\tEGL context: +debug"); + context_attributes.push(khronos_egl::CONTEXT_OPENGL_DEBUG); + context_attributes.push(khronos_egl::TRUE as _); + } else if supports_khr_context { + log::debug!("\tEGL context: +debug KHR"); + khr_context_flags |= EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR; + } else { + log::debug!("\tEGL context: -debug"); + } + } + + if khr_context_flags != 0 { + context_attributes.push(EGL_CONTEXT_FLAGS_KHR); + context_attributes.push(khr_context_flags); + } + + gl_context_attributes.extend(&context_attributes); + gles_context_attributes.extend(&context_attributes); + + let context = { + #[derive(Copy, Clone)] + enum Robustness { + Core, + Ext, + } + + let robustness = if version >= (1, 5) { + Some(Robustness::Core) + } else if display_extensions.contains("EGL_EXT_create_context_robustness") { + Some(Robustness::Ext) + } else { + None + }; + + let create_context = |api, base_attributes: &[khronos_egl::Int]| { + egl.bind_api(api)?; + + let mut robustness = robustness; + loop { + let robustness_attributes = match robustness { + Some(Robustness::Core) => { + vec![ + khronos_egl::CONTEXT_OPENGL_ROBUST_ACCESS, + khronos_egl::TRUE as _, + khronos_egl::NONE, + ] + } + Some(Robustness::Ext) => { + vec![ + EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT, + khronos_egl::TRUE as _, + khronos_egl::NONE, + ] + } + None => vec![khronos_egl::NONE], + }; + + let mut context_attributes = base_attributes.to_vec(); + context_attributes.extend(&robustness_attributes); + + match egl.create_context(display, config, None, &context_attributes) { + Ok(context) => { + match robustness { + Some(Robustness::Core) => { + log::debug!("\tEGL context: +robust access"); + } + Some(Robustness::Ext) => { + log::debug!("\tEGL context: +robust access EXT"); + } + None => { + log::debug!("\tEGL context: -robust access"); + } + } + return Ok(context); + } + + // Robust access context creation can fail with different error codes + // depending on the EGL path. Retry with a lower robustness level. + Err( + khronos_egl::Error::BadAttribute + | khronos_egl::Error::BadMatch + | khronos_egl::Error::BadConfig, + ) if robustness.is_some() => { + robustness = match robustness { + Some(Robustness::Core) + if display_extensions + .contains("EGL_EXT_create_context_robustness") => + { + Some(Robustness::Ext) + } + _ => None, + }; + continue; + } + + Err(e) => return Err(e), + } + } + }; + + let result = if supports_opengl { + create_context(khronos_egl::OPENGL_API, &gl_context_attributes).or_else( + |gl_error| { + log::debug!("Failed to create desktop OpenGL context: {gl_error}, falling back to OpenGL ES"); + create_context(khronos_egl::OPENGL_ES_API, &gles_context_attributes) + }, + ) + } else { + create_context(khronos_egl::OPENGL_ES_API, &gles_context_attributes) + }; + + result.map_err(|e| { + crate::InstanceError::with_source( + String::from("unable to create OpenGL or GLES 3.x context"), + e, + ) + }) + }?; + + // Testing if context can be binded without surface + // and creating dummy pbuffer surface if not. + let pbuffer = if version >= (1, 5) + || display_extensions.contains("EGL_KHR_surfaceless_context") + || cfg!(Emscripten) + { + log::debug!("\tEGL context: +surfaceless"); + None + } else { + let attributes = [ + khronos_egl::WIDTH, + 1, + khronos_egl::HEIGHT, + 1, + khronos_egl::NONE, + ]; + egl.create_pbuffer_surface(display, config, &attributes) + .map(Some) + .map_err(|e| { + crate::InstanceError::with_source( + String::from("error in create_pbuffer_surface"), + e, + ) + })? + }; + + Ok(Self { + egl: EglContext { + instance: egl, + display, + raw: context, + pbuffer, + version, + }, + version, + supports_native_window, + config, + srgb_kind, + }) + } +} + +impl Drop for Inner { + fn drop(&mut self) { + // ERROR: Since EglContext is erroneously Clone, these handles could be copied and + // accidentally used elsewhere outside of Inner, despite us assuming ownership and + // destroying the handles here. + if let Err(e) = self + .egl + .instance + .destroy_context(self.egl.display, self.egl.raw) + { + log::warn!("Error in destroy_context: {e:?}"); + } + + if let Err(e) = terminate_display(&self.egl.instance, self.egl.display) { + log::warn!("Error in terminate: {e:?}"); + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq)] +enum WindowKind { + Wayland, + X11, + AngleX11, + Unknown, +} + +#[derive(Clone, Debug)] +struct WindowSystemInterface { + kind: WindowKind, +} + +#[derive(Debug)] +pub struct Instance { + wsi: WindowSystemInterface, + flags: wgt::InstanceFlags, + options: wgt::GlBackendOptions, + inner: Mutex<Inner>, +} + +impl Instance { + pub fn raw_display(&self) -> khronos_egl::Display { + self.inner + .try_lock() + .expect("Could not lock instance. This is most-likely a deadlock.") + .egl + .display + } + + /// Returns the version of the EGL display. + pub fn egl_version(&self) -> (i32, i32) { + self.inner + .try_lock() + .expect("Could not lock instance. This is most-likely a deadlock.") + .version + } + + pub fn egl_config(&self) -> khronos_egl::Config { + self.inner + .try_lock() + .expect("Could not lock instance. This is most-likely a deadlock.") + .config + } +} + +#[cfg(send_sync)] +static_assertions::assert_impl_all!(Instance: Send, Sync); + +impl crate::Instance for Instance { + type A = super::Api; + + unsafe fn init(desc: &crate::InstanceDescriptor<'_>) -> Result<Self, crate::InstanceError> { + use raw_window_handle::RawDisplayHandle as Rdh; + + profiling::scope!("Init OpenGL (EGL) Backend"); + #[cfg(Emscripten)] + let egl_result: Result<EglInstance, khronos_egl::Error> = + Ok(khronos_egl::Instance::new(khronos_egl::Static)); + + #[cfg(not(Emscripten))] + let egl_result = if cfg!(windows) { + unsafe { + khronos_egl::DynamicInstance::<khronos_egl::EGL1_4>::load_required_from_filename( + "libEGL.dll", + ) + } + } else if cfg!(target_vendor = "apple") { + unsafe { + khronos_egl::DynamicInstance::<khronos_egl::EGL1_4>::load_required_from_filename( + "libEGL.dylib", + ) + } + } else { + unsafe { khronos_egl::DynamicInstance::<khronos_egl::EGL1_4>::load_required() } + }; + let egl = egl_result + .map(Arc::new) + .map_err(instance_err("unable to open libEGL"))?; + + let client_extensions = egl.query_string(None, khronos_egl::EXTENSIONS); + + let client_ext_str = match client_extensions { + Ok(ext) => ext.to_string_lossy().into_owned(), + Err(_) => String::new(), + }; + log::debug!( + "Client extensions: {:#?}", + client_ext_str.split_whitespace().collect::<Vec<_>>() + ); + + #[cfg(not(Emscripten))] + let egl1_5 = egl.upcast::<khronos_egl::EGL1_5>(); + + #[cfg(Emscripten)] + let egl1_5: Option<&Arc<EglInstance>> = Some(&egl); + + let (display, wsi_kind) = match (desc.display.map(|d| d.as_raw()), egl1_5) { + (Some(Rdh::Wayland(wayland_display_handle)), Some(egl)) + if client_ext_str.contains("EGL_EXT_platform_wayland") => + { + log::debug!("Using Wayland platform"); + let display_attributes = [khronos_egl::ATTRIB_NONE]; + let display = unsafe { + egl.get_platform_display( + EGL_PLATFORM_WAYLAND_KHR, + wayland_display_handle.display.as_ptr(), + &display_attributes, + ) + } + .map_err(instance_err("failed to get Wayland display"))?; + (display, WindowKind::Wayland) + } + (Some(Rdh::Xlib(xlib_display_handle)), Some(egl)) + if client_ext_str.contains("EGL_EXT_platform_x11") => + { + log::debug!("Using X11 platform"); + let display_attributes = [khronos_egl::ATTRIB_NONE]; + let display = unsafe { + egl.get_platform_display( + EGL_PLATFORM_X11_KHR, + xlib_display_handle + .display + .map_or(khronos_egl::DEFAULT_DISPLAY, ptr::NonNull::as_ptr), + &display_attributes, + ) + } + .map_err(instance_err("failed to get X11 display"))?; + (display, WindowKind::X11) + } + (Some(Rdh::Xlib(xlib_display_handle)), Some(egl)) + if client_ext_str.contains("EGL_ANGLE_platform_angle") => + { + log::debug!("Using Angle platform with X11"); + let display_attributes = [ + EGL_PLATFORM_ANGLE_NATIVE_PLATFORM_TYPE_ANGLE as khronos_egl::Attrib, + EGL_PLATFORM_X11_KHR as khronos_egl::Attrib, + EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED as khronos_egl::Attrib, + usize::from(desc.flags.contains(wgt::InstanceFlags::VALIDATION)), + khronos_egl::ATTRIB_NONE, + ]; + let display = unsafe { + egl.get_platform_display( + EGL_PLATFORM_ANGLE_ANGLE, + xlib_display_handle + .display + .map_or(khronos_egl::DEFAULT_DISPLAY, ptr::NonNull::as_ptr), + &display_attributes, + ) + } + .map_err(instance_err("failed to get Angle display"))?; + (display, WindowKind::AngleX11) + } + (Some(Rdh::Xcb(xcb_display_handle)), Some(egl)) + if client_ext_str.contains("EGL_EXT_platform_xcb") => + { + log::debug!("Using XCB platform"); + let display_attributes = [ + EGL_PLATFORM_XCB_SCREEN_EXT as khronos_egl::Attrib, + xcb_display_handle.screen as khronos_egl::Attrib, + khronos_egl::ATTRIB_NONE, + ]; + let display = unsafe { + egl.get_platform_display( + EGL_PLATFORM_XCB_EXT, + xcb_display_handle + .connection + .map_or(khronos_egl::DEFAULT_DISPLAY, ptr::NonNull::as_ptr), + &display_attributes, + ) + } + .map_err(instance_err("failed to get XCB display"))?; + (display, WindowKind::X11) + } + x if client_ext_str.contains("EGL_MESA_platform_surfaceless") => { + log::debug!( + "No (or unknown) windowing system ({x:?}) present. Using surfaceless platform" + ); + #[allow( + clippy::unnecessary_literal_unwrap, + reason = "this is only a literal on Emscripten" + )] + // TODO: This extension is also supported on EGL 1.4 with EGL_EXT_platform_base: https://registry.khronos.org/EGL/extensions/MESA/EGL_MESA_platform_surfaceless.txt + let egl = egl1_5.expect("Failed to get EGL 1.5 for surfaceless"); + let display = unsafe { + egl.get_platform_display( + EGL_PLATFORM_SURFACELESS_MESA, + khronos_egl::DEFAULT_DISPLAY, + &[khronos_egl::ATTRIB_NONE], + ) + } + .map_err(instance_err("failed to get MESA surfaceless display"))?; + (display, WindowKind::Unknown) + } + x => { + log::debug!( + "No (or unknown) windowing system {x:?} and EGL_MESA_platform_surfaceless not available. Using default platform" + ); + let display = + unsafe { egl.get_display(khronos_egl::DEFAULT_DISPLAY) }.ok_or_else(|| { + crate::InstanceError::new("Failed to get default display".into()) + })?; + (display, WindowKind::Unknown) + } + }; + + if desc.flags.contains(wgt::InstanceFlags::VALIDATION) + && client_ext_str.contains("EGL_KHR_debug") + { + log::debug!("Enabling EGL debug output"); + let function: EglDebugMessageControlFun = { + let addr = egl + .get_proc_address("eglDebugMessageControlKHR") + .ok_or_else(|| { + crate::InstanceError::new( + "failed to get `eglDebugMessageControlKHR` proc address".into(), + ) + })?; + unsafe { core::mem::transmute(addr) } + }; + let attributes = [ + EGL_DEBUG_MSG_CRITICAL_KHR as khronos_egl::Attrib, + 1, + EGL_DEBUG_MSG_ERROR_KHR as khronos_egl::Attrib, + 1, + EGL_DEBUG_MSG_WARN_KHR as khronos_egl::Attrib, + 1, + EGL_DEBUG_MSG_INFO_KHR as khronos_egl::Attrib, + 1, + khronos_egl::ATTRIB_NONE, + ]; + unsafe { (function)(Some(egl_debug_proc), attributes.as_ptr()) }; + } + + let inner = Inner::create( + desc.flags, + egl, + display, + desc.backend_options.gl.gles_minor_version, + )?; + + Ok(Instance { + wsi: WindowSystemInterface { kind: wsi_kind }, + flags: desc.flags, + options: desc.backend_options.gl.clone(), + inner: Mutex::new(inner), + }) + } + + unsafe fn create_surface( + &self, + display_handle: raw_window_handle::RawDisplayHandle, + window_handle: raw_window_handle::RawWindowHandle, + ) -> Result<Surface, crate::InstanceError> { + use raw_window_handle::RawWindowHandle as Rwh; + + let inner = self.inner.lock(); + + match (window_handle, display_handle) { + (Rwh::Xlib(_), _) => {} + (Rwh::Xcb(_), _) => {} + (Rwh::Win32(_), _) => {} + (Rwh::AppKit(_), _) => {} + (Rwh::OhosNdk(_), _) => {} + #[cfg(target_os = "android")] + (Rwh::AndroidNdk(handle), _) => { + let format = inner + .egl + .instance + .get_config_attrib( + inner.egl.display, + inner.config, + khronos_egl::NATIVE_VISUAL_ID, + ) + .map_err(instance_err("failed to get config NATIVE_VISUAL_ID"))?; + + let ret = unsafe { + ndk_sys::ANativeWindow_setBuffersGeometry( + handle + .a_native_window + .as_ptr() + .cast::<ndk_sys::ANativeWindow>(), + 0, + 0, + format, + ) + }; + + if ret != 0 { + return Err(crate::InstanceError::new(format!( + "error {ret} returned from ANativeWindow_setBuffersGeometry", + ))); + } + } + (Rwh::Wayland(_), _) => {} + #[cfg(Emscripten)] + (Rwh::Web(_), _) => {} + other => { + return Err(crate::InstanceError::new(format!( + "unsupported window: {other:?}" + ))); + } + }; + + inner.egl.unmake_current(); + + Ok(Surface { + egl: inner.egl.clone(), + wsi: self.wsi.clone(), + config: inner.config, + presentable: inner.supports_native_window, + raw_window_handle: window_handle, + swapchain: RwLock::new(None), + srgb_kind: inner.srgb_kind, + }) + } + + unsafe fn enumerate_adapters( + &self, + _surface_hint: Option<&Surface>, + ) -> Vec<crate::ExposedAdapter<super::Api>> { + let inner = self.inner.lock(); + inner.egl.make_current(); + + let mut gl = unsafe { + glow::Context::from_loader_function(|name| { + inner + .egl + .instance + .get_proc_address(name) + .map_or(ptr::null(), |p| p as *const _) + }) + }; + + // In contrast to OpenGL ES, OpenGL requires explicitly enabling sRGB conversions, + // as otherwise the user has to do the sRGB conversion. + if !matches!(inner.srgb_kind, SrgbFrameBufferKind::None) { + unsafe { gl.enable(glow::FRAMEBUFFER_SRGB) }; + } + + if self.flags.contains(wgt::InstanceFlags::DEBUG) && gl.supports_debug() { + log::debug!("Max label length: {}", unsafe { + gl.get_parameter_i32(glow::MAX_LABEL_LENGTH) + }); + } + + if self.flags.contains(wgt::InstanceFlags::VALIDATION) && gl.supports_debug() { + log::debug!("Enabling GLES debug output"); + unsafe { gl.enable(glow::DEBUG_OUTPUT) }; + unsafe { gl.debug_message_callback(super::gl_debug_message_callback) }; + } + + // Wrap in ManuallyDrop to make it easier to "current" the GL context before dropping this + // GLOW context, which could also happen if a panic occurs after we uncurrent the context + // below but before AdapterContext is constructed. + let gl = ManuallyDrop::new(gl); + inner.egl.unmake_current(); + + unsafe { + super::Adapter::expose( + AdapterContext { + glow: Mutex::new(gl), + // ERROR: Copying owned reference handles here, be careful to not drop them! + egl: Some(inner.egl.clone()), + }, + self.options.clone(), + ) + } + .into_iter() + .collect() + } +} + +impl super::Adapter { + /// Creates a new external adapter using the specified loader function. + /// + /// # Safety + /// + /// - The underlying OpenGL ES context must be current. + /// - The underlying OpenGL ES context must be current when interfacing with any objects returned by + /// wgpu-hal from this adapter. + /// - The underlying OpenGL ES context must be current when dropping this adapter and when + /// dropping any objects returned from this adapter. + pub unsafe fn new_external( + fun: impl FnMut(&str) -> *const ffi::c_void, + options: wgt::GlBackendOptions, + ) -> Option<crate::ExposedAdapter<super::Api>> { + let context = unsafe { glow::Context::from_loader_function(fun) }; + unsafe { + Self::expose( + AdapterContext { + glow: Mutex::new(ManuallyDrop::new(context)), + egl: None, + }, + options, + ) + } + } + + pub fn adapter_context(&self) -> &AdapterContext { + &self.shared.context + } +} + +impl super::Device { + /// Returns the underlying EGL context. + pub fn context(&self) -> &AdapterContext { + &self.shared.context + } +} + +#[derive(Debug)] +pub struct Swapchain { + surface: khronos_egl::Surface, + wl_window: Option<*mut wayland_sys::egl::wl_egl_window>, + framebuffer: glow::Framebuffer, + renderbuffer: glow::Renderbuffer, + /// Extent because the window lies + extent: wgt::Extent3d, + format: wgt::TextureFormat, + format_desc: super::TextureFormatDesc, + #[allow(unused)] + sample_type: wgt::TextureSampleType, +} + +#[derive(Debug)] +pub struct Surface { + egl: EglContext, + wsi: WindowSystemInterface, + config: khronos_egl::Config, + pub(super) presentable: bool, + raw_window_handle: raw_window_handle::RawWindowHandle, + swapchain: RwLock<Option<Swapchain>>, + srgb_kind: SrgbFrameBufferKind, +} + +unsafe impl Send for Surface {} +unsafe impl Sync for Surface {} + +impl Surface { + pub(super) unsafe fn present( + &self, + _suf_texture: super::Texture, + context: &AdapterContext, + ) -> Result<(), crate::SurfaceError> { + let gl = unsafe { context.get_without_egl_lock() }; + let swapchain = self.swapchain.read(); + let sc = swapchain.as_ref().ok_or(crate::SurfaceError::Other( + "Surface has no swap-chain configured", + ))?; + + self.egl + .instance + .make_current( + self.egl.display, + Some(sc.surface), + Some(sc.surface), + Some(self.egl.raw), + ) + .map_err(|e| { + log::error!("make_current(surface) failed: {e}"); + crate::SurfaceError::Lost + })?; + + unsafe { gl.disable(glow::SCISSOR_TEST) }; + unsafe { gl.color_mask(true, true, true, true) }; + + unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, None) }; + unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(sc.framebuffer)) }; + + if !matches!(self.srgb_kind, SrgbFrameBufferKind::None) { + // Disable sRGB conversions for `glBlitFramebuffer` as behavior does diverge between + // drivers and formats otherwise and we want to ensure no sRGB conversions happen. + unsafe { gl.disable(glow::FRAMEBUFFER_SRGB) }; + } + + // Note the Y-flipping here. GL's presentation is not flipped, + // but main rendering is. Therefore, we Y-flip the output positions + // in the shader, and also this blit. + unsafe { + gl.blit_framebuffer( + 0, + sc.extent.height as i32, + sc.extent.width as i32, + 0, + 0, + 0, + sc.extent.width as i32, + sc.extent.height as i32, + glow::COLOR_BUFFER_BIT, + glow::NEAREST, + ) + }; + + if !matches!(self.srgb_kind, SrgbFrameBufferKind::None) { + unsafe { gl.enable(glow::FRAMEBUFFER_SRGB) }; + } + + unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, None) }; + + self.egl + .instance + .swap_buffers(self.egl.display, sc.surface) + .map_err(|e| { + log::error!("swap_buffers failed: {e}"); + crate::SurfaceError::Lost + // TODO: should we unset the current context here? + })?; + self.egl + .instance + .make_current(self.egl.display, None, None, None) + .map_err(|e| { + log::error!("make_current(null) failed: {e}"); + crate::SurfaceError::Lost + })?; + + Ok(()) + } + + unsafe fn unconfigure_impl( + &self, + device: &super::Device, + ) -> Option<( + khronos_egl::Surface, + Option<*mut wayland_sys::egl::wl_egl_window>, + )> { + let gl = &device.shared.context.lock(); + match self.swapchain.write().take() { + Some(sc) => { + unsafe { gl.delete_renderbuffer(sc.renderbuffer) }; + unsafe { gl.delete_framebuffer(sc.framebuffer) }; + Some((sc.surface, sc.wl_window)) + } + None => None, + } + } + + pub fn supports_srgb(&self) -> bool { + match self.srgb_kind { + SrgbFrameBufferKind::None => false, + _ => true, + } + } +} + +impl crate::Surface for Surface { + type A = super::Api; + + unsafe fn configure( + &self, + device: &super::Device, + config: &crate::SurfaceConfiguration, + ) -> Result<(), crate::SurfaceError> { + use raw_window_handle::RawWindowHandle as Rwh; + + let (surface, wl_window) = match unsafe { self.unconfigure_impl(device) } { + Some((sc, wl_window)) => { + if let Some(window) = wl_window { + wayland_sys::ffi_dispatch!( + wayland_sys::egl::wayland_egl_handle(), + wl_egl_window_resize, + window, + config.extent.width as i32, + config.extent.height as i32, + 0, + 0, + ); + } + + (sc, wl_window) + } + None => { + let mut wl_window = None; + let (mut temp_xlib_handle, mut temp_xcb_handle); + let native_window_ptr = match (self.wsi.kind, self.raw_window_handle) { + (WindowKind::Unknown | WindowKind::X11, Rwh::Xlib(handle)) => { + temp_xlib_handle = handle.window; + ptr::from_mut(&mut temp_xlib_handle).cast::<ffi::c_void>() + } + (WindowKind::AngleX11, Rwh::Xlib(handle)) => handle.window as *mut ffi::c_void, + (WindowKind::Unknown | WindowKind::X11, Rwh::Xcb(handle)) => { + temp_xcb_handle = handle.window; + ptr::from_mut(&mut temp_xcb_handle).cast::<ffi::c_void>() + } + (WindowKind::AngleX11, Rwh::Xcb(handle)) => { + handle.window.get() as *mut ffi::c_void + } + (WindowKind::Unknown, Rwh::AndroidNdk(handle)) => { + handle.a_native_window.as_ptr() + } + (WindowKind::Unknown, Rwh::OhosNdk(handle)) => handle.native_window.as_ptr(), + #[cfg(unix)] + (WindowKind::Wayland, Rwh::Wayland(handle)) => { + let window = wayland_sys::ffi_dispatch!( + wayland_sys::egl::wayland_egl_handle(), + wl_egl_window_create, + handle.surface.as_ptr().cast(), + config.extent.width as i32, + config.extent.height as i32, + ); + wl_window = Some(window); + window.cast() + } + #[cfg(Emscripten)] + (WindowKind::Unknown, Rwh::Web(handle)) => handle.id as *mut ffi::c_void, + (WindowKind::Unknown, Rwh::Win32(handle)) => { + handle.hwnd.get() as *mut ffi::c_void + } + (WindowKind::Unknown, Rwh::AppKit(handle)) => { + #[cfg(not(target_os = "macos"))] + let window_ptr = handle.ns_view.as_ptr(); + #[cfg(target_os = "macos")] + let window_ptr = { + use objc2::msg_send; + use objc2::runtime::AnyObject; + // ns_view always have a layer and don't need to verify that it exists. + let layer: *mut AnyObject = + msg_send![handle.ns_view.as_ptr().cast::<AnyObject>(), layer]; + layer.cast::<ffi::c_void>() + }; + window_ptr + } + _ => { + log::warn!( + "Initialized platform {:?} doesn't work with window {:?}", + self.wsi.kind, + self.raw_window_handle + ); + return Err(crate::SurfaceError::Other("incompatible window kind")); + } + }; + + let mut attributes = vec![ + khronos_egl::RENDER_BUFFER, + // We don't want any of the buffering done by the driver, because we + // manage a swapchain on our side. + // Some drivers just fail on surface creation seeing `EGL_SINGLE_BUFFER`. + if cfg!(any( + target_os = "android", + target_os = "macos", + target_env = "ohos" + )) || cfg!(windows) + || self.wsi.kind == WindowKind::AngleX11 + { + khronos_egl::BACK_BUFFER + } else { + khronos_egl::SINGLE_BUFFER + }, + ]; + if config.format.is_srgb() { + match self.srgb_kind { + SrgbFrameBufferKind::None => {} + SrgbFrameBufferKind::Core => { + attributes.push(khronos_egl::GL_COLORSPACE); + attributes.push(khronos_egl::GL_COLORSPACE_SRGB); + } + SrgbFrameBufferKind::Khr => { + attributes.push(EGL_GL_COLORSPACE_KHR as i32); + attributes.push(EGL_GL_COLORSPACE_SRGB_KHR as i32); + } + } + } + attributes.push(khronos_egl::ATTRIB_NONE as i32); + + #[cfg(not(Emscripten))] + let egl1_5 = self.egl.instance.upcast::<khronos_egl::EGL1_5>(); + + #[cfg(Emscripten)] + let egl1_5: Option<&Arc<EglInstance>> = Some(&self.egl.instance); + + // Careful, we can still be in 1.4 version even if `upcast` succeeds + let raw_result = match egl1_5 { + Some(egl) if self.wsi.kind != WindowKind::Unknown => { + let attributes_usize = attributes + .into_iter() + .map(|v| v as usize) + .collect::<Vec<_>>(); + unsafe { + egl.create_platform_window_surface( + self.egl.display, + self.config, + native_window_ptr, + &attributes_usize, + ) + } + } + _ => unsafe { + self.egl.instance.create_window_surface( + self.egl.display, + self.config, + native_window_ptr, + Some(&attributes), + ) + }, + }; + + match raw_result { + Ok(raw) => (raw, wl_window), + Err(e) => { + log::warn!("Error in create_window_surface: {e:?}"); + return Err(crate::SurfaceError::Lost); + } + } + } + }; + + let format_desc = device.shared.describe_texture_format(config.format); + let gl = &device.shared.context.lock(); + let renderbuffer = unsafe { gl.create_renderbuffer() }.map_err(|error| { + log::error!("Internal swapchain renderbuffer creation failed: {error}"); + crate::DeviceError::OutOfMemory + })?; + unsafe { gl.bind_renderbuffer(glow::RENDERBUFFER, Some(renderbuffer)) }; + unsafe { + gl.renderbuffer_storage( + glow::RENDERBUFFER, + format_desc.internal, + config.extent.width as _, + config.extent.height as _, + ) + }; + let framebuffer = unsafe { gl.create_framebuffer() }.map_err(|error| { + log::error!("Internal swapchain framebuffer creation failed: {error}"); + crate::DeviceError::OutOfMemory + })?; + unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(framebuffer)) }; + unsafe { + gl.framebuffer_renderbuffer( + glow::READ_FRAMEBUFFER, + glow::COLOR_ATTACHMENT0, + glow::RENDERBUFFER, + Some(renderbuffer), + ) + }; + unsafe { gl.bind_renderbuffer(glow::RENDERBUFFER, None) }; + unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, None) }; + + let mut swapchain = self.swapchain.write(); + *swapchain = Some(Swapchain { + surface, + wl_window, + renderbuffer, + framebuffer, + extent: config.extent, + format: config.format, + format_desc, + sample_type: wgt::TextureSampleType::Float { filterable: false }, + }); + + Ok(()) + } + + unsafe fn unconfigure(&self, device: &super::Device) { + if let Some((surface, wl_window)) = unsafe { self.unconfigure_impl(device) } { + self.egl + .instance + .destroy_surface(self.egl.display, surface) + .unwrap(); + if let Some(window) = wl_window { + wayland_sys::ffi_dispatch!( + wayland_sys::egl::wayland_egl_handle(), + wl_egl_window_destroy, + window, + ); + } + } + } + + unsafe fn acquire_texture( + &self, + _timeout_ms: Option<Duration>, //TODO + _fence: &super::Fence, + ) -> Result<crate::AcquiredSurfaceTexture<super::Api>, crate::SurfaceError> { + let swapchain = self.swapchain.read(); + let sc = swapchain.as_ref().ok_or(crate::SurfaceError::Other( + "Surface has no swap-chain configured", + ))?; + let texture = super::Texture { + inner: super::TextureInner::Renderbuffer { + raw: sc.renderbuffer, + }, + drop_guard: None, + array_layer_count: 1, + mip_level_count: 1, + format: sc.format, + format_desc: sc.format_desc.clone(), + copy_size: crate::CopyExtent { + width: sc.extent.width, + height: sc.extent.height, + depth: 1, + }, + }; + Ok(crate::AcquiredSurfaceTexture { + texture, + suboptimal: false, + }) + } + unsafe fn discard_texture(&self, _texture: super::Texture) {} +} diff --git a/third_party/wgpu-hal/src/gles/emscripten.rs b/third_party/wgpu-hal/src/gles/emscripten.rs new file mode 100644 index 000000000..88cc0becc --- /dev/null +++ b/third_party/wgpu-hal/src/gles/emscripten.rs @@ -0,0 +1,26 @@ +extern "C" { + /// returns 1 if success. 0 if failure. extension name must be null terminated + fn emscripten_webgl_enable_extension( + context: core::ffi::c_int, + extension: *const core::ffi::c_char, + ) -> core::ffi::c_int; + fn emscripten_webgl_get_current_context() -> core::ffi::c_int; +} +/// Webgl requires extensions to be enabled before using them. +/// This function can be used to enable webgl extension on emscripten target. +/// +/// returns true on success +/// +/// # Safety +/// +/// - opengl context MUST BE current +/// - extension_name_null_terminated argument must be a valid string with null terminator. +/// - extension must be present. check `glow_context.supported_extensions()` +pub unsafe fn enable_extension(extension_name_null_terminated: &str) -> bool { + unsafe { + emscripten_webgl_enable_extension( + emscripten_webgl_get_current_context(), + extension_name_null_terminated.as_ptr().cast(), + ) == 1 + } +} diff --git a/third_party/wgpu-hal/src/gles/fence.rs b/third_party/wgpu-hal/src/gles/fence.rs new file mode 100644 index 000000000..0470e5ba1 --- /dev/null +++ b/third_party/wgpu-hal/src/gles/fence.rs @@ -0,0 +1,198 @@ +use alloc::{sync::Arc, vec::Vec}; +use core::sync::atomic::Ordering; +use parking_lot::RwLock; + +use glow::HasContext; + +use crate::AtomicFenceValue; + +#[derive(Debug)] +struct GLFence { + // Since a fence can be `Copy`ed, there can exist some + // cases where a fence could be destroyed while something + // else is still using it. Therefore, while a function is + // using this fence (and doesn't keep pending read locked), + // it should clone the `Arc` to show it needs this to + // stay alive. + // + // The arc should not be kept after a function has finished + sync: Arc<glow::Fence>, + value: crate::FenceValue, +} + +#[derive(Debug)] +pub struct Fence { + last_completed: AtomicFenceValue, + pending: RwLock<Vec<GLFence>>, + fence_behavior: wgt::GlFenceBehavior, +} + +impl crate::DynFence for Fence {} + +#[cfg(send_sync)] +unsafe impl Send for Fence {} +#[cfg(send_sync)] +unsafe impl Sync for Fence {} + +impl Fence { + pub fn new(options: &wgt::GlBackendOptions) -> Self { + Self { + last_completed: AtomicFenceValue::new(0), + pending: RwLock::new(Vec::new()), + fence_behavior: options.fence_behavior, + } + } + + pub fn signal( + &self, + gl: &glow::Context, + value: crate::FenceValue, + ) -> Result<(), crate::DeviceError> { + if self.fence_behavior.is_auto_finish() { + self.last_completed.store(value, Ordering::Release); + return Ok(()); + } + + let sync = unsafe { gl.fence_sync(glow::SYNC_GPU_COMMANDS_COMPLETE, 0) } + .map_err(|_| crate::DeviceError::OutOfMemory)?; + self.pending.write().push(GLFence { + sync: Arc::new(sync), + value, + }); + + Ok(()) + } + + pub fn satisfied(&self, value: crate::FenceValue) -> bool { + self.last_completed.load(Ordering::Acquire) >= value + } + + pub fn get_latest(&self, gl: &glow::Context) -> crate::FenceValue { + let mut max_value = self.last_completed.load(Ordering::Acquire); + + if self.fence_behavior.is_auto_finish() { + return max_value; + } + + let pending = self.pending.read(); + + for gl_fence in pending.iter() { + if gl_fence.value <= max_value { + // We already know this was good, no need to check again + continue; + } + // We have pending `read` locked, so we shouldn't have to clone it. + let status = unsafe { gl.get_sync_status(*gl_fence.sync) }; + if status == glow::SIGNALED { + max_value = gl_fence.value; + } else { + // Anything after the first unsignalled is guaranteed to also be unsignalled + break; + } + } + + // Track the latest value, to save ourselves some querying later + self.last_completed.fetch_max(max_value, Ordering::AcqRel); + + max_value + } + + pub fn maintain(&self, gl: &glow::Context) { + if self.fence_behavior.is_auto_finish() { + return; + } + + let latest = self.get_latest(gl); + let mut pending = self.pending.write(); + pending.retain_mut(|gl_fence| { + if gl_fence.value > latest { + true + } else if let Some(fence) = Arc::get_mut(&mut gl_fence.sync) { + unsafe { + gl.delete_sync(*fence); + } + false + } else { + // Another function is currently using this value. In general, these should finish + // very quickly (for wait because the fence should already be signaled, an all + // others are just fast), but submit should be very fast, so we shouldn't block on + // this. + true + } + }); + } + + pub fn wait( + &self, + gl: &glow::Context, + wait_value: crate::FenceValue, + timeout_ns: u32, + ) -> Result<bool, crate::DeviceError> { + let last_completed = self.last_completed.load(Ordering::Acquire); + + if self.fence_behavior.is_auto_finish() { + return Ok(last_completed >= wait_value); + } + + // We already know this fence has been signalled to that value. Return signalled. + if last_completed >= wait_value { + return Ok(true); + } + + let pending = self.pending.read(); + + // Find a matching fence + let gl_fence = pending.iter().find(|gl_fence| gl_fence.value >= wait_value); + + let Some(gl_fence) = gl_fence else { + log::warn!("Tried to wait for {wait_value} but that value has not been signalled yet"); + return Ok(false); + }; + + // clone to show we're using the fence + let sync = gl_fence.sync.clone(); + let fence_value = gl_fence.value; + + drop(pending); + + let status = unsafe { + gl.client_wait_sync( + *sync, + glow::SYNC_FLUSH_COMMANDS_BIT, + timeout_ns.min(i32::MAX as u32) as i32, + ) + }; + + drop(sync); + + let signalled = match status { + glow::ALREADY_SIGNALED | glow::CONDITION_SATISFIED => true, + glow::TIMEOUT_EXPIRED | glow::WAIT_FAILED => false, + _ => { + log::warn!("Unexpected result from client_wait_sync: {status}"); + false + } + }; + + if signalled { + self.last_completed.fetch_max(fence_value, Ordering::AcqRel); + } + + Ok(signalled) + } + + pub fn destroy(self, gl: &glow::Context) { + if self.fence_behavior.is_auto_finish() { + return; + } + + for gl_fence in self.pending.into_inner() { + unsafe { + gl.delete_sync( + Arc::into_inner(gl_fence.sync) + .expect("A function has failed to drop all its references to this"), + ); + } + } + } +} diff --git a/third_party/wgpu-hal/src/gles/mod.rs b/third_party/wgpu-hal/src/gles/mod.rs new file mode 100644 index 000000000..24e26c67e --- /dev/null +++ b/third_party/wgpu-hal/src/gles/mod.rs @@ -0,0 +1,1215 @@ +/*! +# OpenGL ES3 API (aka GLES3). + +Designed to work on Linux and Android, with context provided by EGL. + +## Texture views + +GLES3 doesn't really have separate texture view objects. We have to remember the +original texture and the sub-range into it. Problem is, however, that there is +no way to expose a subset of array layers or mip levels of a sampled texture. + +## Binding model + +Binding model is very different from WebGPU, especially with regards to samplers. +GLES3 has sampler objects, but they aren't separately bindable to the shaders. +Each sampled texture is exposed to the shader as a combined texture-sampler binding. + +When building the pipeline layout, we linearize binding entries based on the groups +(uniform/storage buffers, uniform/storage textures), and record the mapping into +`BindGroupLayoutInfo`. +When a pipeline gets created, and we track all the texture-sampler associations +from the static use in the shader. +We only support at most one sampler used with each texture so far. The linear index +of this sampler is stored per texture slot in `SamplerBindMap` array. + +The texture-sampler pairs get potentially invalidated in 2 places: + - when a new pipeline is set, we update the linear indices of associated samplers + - when a new bind group is set, we update both the textures and the samplers + +We expect that the changes to sampler states between any 2 pipelines of the same layout +will be minimal, if any. + +## Vertex data + +Generally, vertex buffers are marked as dirty and lazily bound on draw. + +GLES3 doesn't support `first_instance` semantics. However, it's easy to support, +since we are forced to do late binding anyway. We just adjust the offsets +into the vertex data. + +### Old path + +In GLES-3.0 and WebGL2, vertex buffer layout is provided +together with the actual buffer binding. +We invalidate the attributes on the vertex buffer change, and re-bind them. + +### New path + +In GLES-3.1 and higher, the vertex buffer layout can be declared separately +from the vertex data itself. This mostly matches WebGPU, however there is a catch: +`stride` needs to be specified with the data, not as a part of the layout. + +To address this, we invalidate the vertex buffers based on: + - whether or not `first_instance` is used + - stride has changed + +## Handling of `base_vertex`, `first_instance`, and `first_vertex` + +Between indirect, the lack of `first_instance` semantics, and the availability of `gl_BaseInstance` +in shaders, getting buffers and builtins to work correctly is a bit tricky. + +We never emulate `base_vertex` and gl_VertexID behaves as `@builtin(vertex_index)` does, so we +never need to do anything about that. + +### GL 4.2+ with ARB_shader_draw_parameters + +- `@builtin(instance_index)` translates to `gl_InstanceID + gl_BaseInstance` +- We bind instance buffers without any offset emulation. +- We advertise support for the `INDIRECT_FIRST_INSTANCE` feature. + +While we can theoretically have a card with 4.2+ support but without ARB_shader_draw_parameters, +we don't bother with that combination. + +### GLES & GL 4.1 + +- `@builtin(instance_index)` translates to `gl_InstanceID + naga_vs_first_instance` +- We bind instance buffers with offset emulation. +- We _do not_ advertise support for `INDIRECT_FIRST_INSTANCE` and cpu-side pretend the `first_instance` is 0 on indirect calls. + +*/ + +///cbindgen:ignore +#[cfg(not(any(windows, webgl)))] +mod egl; +#[cfg(Emscripten)] +mod emscripten; +#[cfg(webgl)] +mod web; +#[cfg(windows)] +mod wgl; + +mod adapter; +mod command; +mod conv; +mod device; +mod fence; +mod queue; + +pub use fence::Fence; + +#[cfg(not(any(windows, webgl)))] +pub use self::egl::{AdapterContext, AdapterContextLock}; +#[cfg(not(any(windows, webgl)))] +pub use self::egl::{Instance, Surface}; + +#[cfg(webgl)] +pub use self::web::AdapterContext; +#[cfg(webgl)] +pub use self::web::{Instance, Surface}; + +#[cfg(windows)] +use self::wgl::AdapterContext; +#[cfg(windows)] +pub use self::wgl::{Instance, Surface}; + +use alloc::{boxed::Box, string::String, string::ToString as _, sync::Arc, vec::Vec}; +use core::{ + fmt, + ops::Range, + sync::atomic::{AtomicU32, AtomicU8}, +}; +use parking_lot::Mutex; + +use arrayvec::ArrayVec; +use glow::HasContext; +use naga::FastHashMap; + +use crate::{CopyExtent, TextureDescriptor}; + +#[derive(Clone, Debug)] +pub struct Api; + +//Note: we can support more samplers if not every one of them is used at a time, +// but it probably doesn't worth it. +const MAX_TEXTURE_SLOTS: usize = 16; +const MAX_SAMPLERS: usize = 16; +const MAX_VERTEX_ATTRIBUTES: usize = 16; +const ZERO_BUFFER_SIZE: usize = 256 << 10; +const MAX_IMMEDIATES: usize = 64; +// We have to account for each immediate data may need to be set for every shader. +const MAX_IMMEDIATES_COMMANDS: usize = MAX_IMMEDIATES * crate::MAX_CONCURRENT_SHADER_STAGES; + +impl crate::Api for Api { + const VARIANT: wgt::Backend = wgt::Backend::Gl; + + type Instance = Instance; + type Surface = Surface; + type Adapter = Adapter; + type Device = Device; + + type Queue = Queue; + type CommandEncoder = CommandEncoder; + type CommandBuffer = CommandBuffer; + + type Buffer = Buffer; + type Texture = Texture; + type SurfaceTexture = Texture; + type TextureView = TextureView; + type Sampler = Sampler; + type QuerySet = QuerySet; + type Fence = Fence; + type AccelerationStructure = AccelerationStructure; + type PipelineCache = PipelineCache; + + type BindGroupLayout = BindGroupLayout; + type BindGroup = BindGroup; + type PipelineLayout = PipelineLayout; + type ShaderModule = ShaderModule; + type RenderPipeline = RenderPipeline; + type ComputePipeline = ComputePipeline; + type RayTracingPipeline = RayTracingPipeline; +} + +crate::impl_dyn_resource!( + Adapter, + AccelerationStructure, + BindGroup, + BindGroupLayout, + Buffer, + CommandBuffer, + CommandEncoder, + ComputePipeline, + Device, + Fence, + Instance, + PipelineCache, + PipelineLayout, + QuerySet, + Queue, + RenderPipeline, + RayTracingPipeline, + Sampler, + ShaderModule, + Surface, + Texture, + TextureView +); + +bitflags::bitflags! { + /// Flags that affect internal code paths but do not + /// change the exposed feature set. + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] + struct PrivateCapabilities: u32 { + /// Indicates support for `glBufferStorage` allocation. + const BUFFER_ALLOCATION = 1 << 0; + /// Support explicit layouts in shader. + const SHADER_BINDING_LAYOUT = 1 << 1; + /// Support extended shadow sampling instructions. + const SHADER_TEXTURE_SHADOW_LOD = 1 << 2; + /// Support memory barriers. + const MEMORY_BARRIERS = 1 << 3; + /// Vertex buffer layouts separate from the data. + const VERTEX_BUFFER_LAYOUT = 1 << 4; + /// Indicates that buffers used as `GL_ELEMENT_ARRAY_BUFFER` may be created / initialized / used + /// as other targets, if not present they must not be mixed with other targets. + const INDEX_BUFFER_ROLE_CHANGE = 1 << 5; + /// Supports `glGetBufferSubData` + const GET_BUFFER_SUB_DATA = 1 << 7; + /// Supports `f16` color buffers + const COLOR_BUFFER_HALF_FLOAT = 1 << 8; + /// Supports `f11/f10` and `f32` color buffers + const COLOR_BUFFER_FLOAT = 1 << 9; + /// Supports query buffer objects. + const QUERY_BUFFERS = 1 << 11; + /// Supports 64 bit queries via `glGetQueryObjectui64v` + const QUERY_64BIT = 1 << 12; + /// Supports `glTexStorage2D`, etc. + const TEXTURE_STORAGE = 1 << 13; + /// Supports `push_debug_group`, `pop_debug_group` and `debug_message_insert`. + const DEBUG_FNS = 1 << 14; + /// Supports framebuffer invalidation. + const INVALIDATE_FRAMEBUFFER = 1 << 15; + /// Indicates support for `glDrawElementsInstancedBaseVertexBaseInstance` and `ARB_shader_draw_parameters` + /// + /// When this is true, instance offset emulation via vertex buffer rebinding and a shader uniform will be disabled. + const FULLY_FEATURED_INSTANCING = 1 << 16; + /// Supports direct multisampled rendering to a texture without needing a resolve texture. + const MULTISAMPLED_RENDER_TO_TEXTURE = 1 << 17; + /// Supports norm16 sized internal formats as filterable sampled + /// textures, with UNORM variants also color-renderable. SNORM + /// renderability is gated on `TEXTURE_FORMAT_SNORM16_RENDERABLE`. + const TEXTURE_FORMAT_NORM16 = 1 << 18; + /// Supports SNORM 16-bit formats as color attachments. Requires + /// `GL_EXT_render_snorm` (in addition to `GL_EXT_texture_norm16` + /// on GLES) - desktop GL alone only "optionally" renders SNORM 16. + const TEXTURE_FORMAT_SNORM16_RENDERABLE = 1 << 19; + /// Supports norm16 sized internal formats as image-load/store targets. + /// Desktop GL >= 4.2 (core image-format list) or pre-4.2 with + /// `GL_ARB_shader_image_load_store`; GLES needs `GL_NV_image_formats` + /// (which itself depends on `GL_EXT_texture_norm16`). + const TEXTURE_FORMAT_NORM16_STORAGE = 1 << 20; + } +} + +bitflags::bitflags! { + /// Flags that indicate necessary workarounds for specific devices or driver bugs + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] + struct Workarounds: u32 { + // Needs workaround for Intel Mesa bug: + // https://gitlab.freedesktop.org/mesa/mesa/-/issues/2565. + // + // This comment + // (https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4972/diffs?diff_id=75888#22f5d1004713c9bbf857988c7efb81631ab88f99_323_327) + // seems to indicate all skylake models are effected. + const MESA_I915_SRGB_SHADER_CLEAR = 1 << 0; + /// Buffer map must emulated because it is not supported natively + const EMULATE_BUFFER_MAP = 1 << 1; + } +} + +type BindTarget = u32; + +#[derive(Debug, Default, Clone, Copy)] +enum VertexAttribKind { + #[default] + Float, // glVertexAttribPointer + Integer, // glVertexAttribIPointer + //Double, // glVertexAttribLPointer +} + +#[derive(Clone, Debug)] +pub struct TextureFormatDesc { + pub internal: u32, + pub external: u32, + pub data_type: u32, +} + +struct AdapterShared { + context: AdapterContext, + private_caps: PrivateCapabilities, + features: wgt::Features, + limits: wgt::Limits, + workarounds: Workarounds, + options: wgt::GlBackendOptions, + shading_language_version: naga::back::glsl::Version, + next_shader_id: AtomicU32, + program_cache: Mutex<ProgramCache>, + es: bool, + + /// Result of `gl.get_parameter_i32(glow::MAX_SAMPLES)`. + /// Cached here so it doesn't need to be queried every time texture format capabilities are requested. + /// (this has been shown to be a significant enough overhead) + max_msaa_samples: i32, +} + +impl fmt::Debug for AdapterShared { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let Self { + context: _, // may or may not implement Debug depending on platform + private_caps, + features, + limits, + workarounds, + options, + shading_language_version, + next_shader_id, + program_cache: _, + es, + max_msaa_samples, + } = self; + f.debug_struct("AdapterShared") + .field("private_caps", private_caps) + .field("features", features) + .field("limits", limits) + .field("workarounds", workarounds) + .field("options", options) + .field("shading_language_version", shading_language_version) + .field("next_shader_id", next_shader_id) + .field("es", es) + .field("max_msaa_samples", max_msaa_samples) + .finish_non_exhaustive() + } +} + +#[derive(Debug)] +pub struct Adapter { + shared: Arc<AdapterShared>, +} + +#[derive(Debug)] +pub struct Device { + shared: Arc<AdapterShared>, + main_vao: glow::VertexArray, + #[cfg(all(native, feature = "renderdoc"))] + render_doc: crate::auxil::renderdoc::RenderDoc, + counters: Arc<wgt::HalCounters>, +} + +impl Drop for Device { + fn drop(&mut self) { + let gl = &self.shared.context.lock(); + unsafe { gl.delete_vertex_array(self.main_vao) }; + } +} + +#[derive(Debug)] +pub struct ShaderClearProgram { + pub program: glow::Program, + pub color_uniform_location: glow::UniformLocation, +} + +#[derive(Debug)] +pub struct Queue { + shared: Arc<AdapterShared>, + features: wgt::Features, + draw_fbo: glow::Framebuffer, + copy_fbo: glow::Framebuffer, + /// Shader program used to clear the screen for [`Workarounds::MESA_I915_SRGB_SHADER_CLEAR`] + /// devices. + shader_clear_program: Option<ShaderClearProgram>, + /// Keep a reasonably large buffer filled with zeroes, so that we can implement `ClearBuffer` of + /// zeroes by copying from it. + zero_buffer: glow::Buffer, + temp_query_results: Mutex<Vec<u64>>, + draw_buffer_count: AtomicU8, + current_index_buffer: Mutex<Option<glow::Buffer>>, +} + +impl Drop for Queue { + fn drop(&mut self) { + let gl = &self.shared.context.lock(); + unsafe { gl.delete_framebuffer(self.draw_fbo) }; + unsafe { gl.delete_framebuffer(self.copy_fbo) }; + unsafe { gl.delete_buffer(self.zero_buffer) }; + } +} + +#[derive(Clone, Debug)] +pub struct Buffer { + raw: Option<glow::Buffer>, + target: BindTarget, + size: wgt::BufferAddress, + /// Flags to use within calls to [`Device::map_buffer`](crate::Device::map_buffer). + map_flags: u32, + /// Buffer mapping state. + /// + /// If locked concurrently with the GL context, the GL context should be locked first. + map_state: Arc<MaybeMutex<BufferMapState>>, + /// Set when the buffer wraps an externally-owned GL name created via + /// [`Device::buffer_from_raw`](crate::gles::Device::buffer_from_raw). + /// + /// `Buffer` is `Clone`, so the guard is shared via `Arc` + /// and only fires its callback once every clone is dropped. + drop_guard: Option<Arc<crate::DropGuard>>, +} + +#[derive(Clone, Debug)] +struct BufferMapState { + /// True if the GL buffer is actually mapped, i.e. not "fake-mapped" with + /// an empty slice + mapped: bool, + data: Option<Vec<u8>>, + offset_of_current_mapping: wgt::BufferAddress, +} + +#[cfg(send_sync)] +static_assertions::assert_impl_all!(Buffer: Send, Sync); + +impl crate::DynBuffer for Buffer {} + +#[derive(Clone, Debug)] +pub enum TextureInner { + Renderbuffer { + raw: glow::Renderbuffer, + }, + DefaultRenderbuffer, + Texture { + raw: glow::Texture, + target: BindTarget, + }, + #[cfg(webgl)] + /// Render to a `WebGLFramebuffer` + /// + /// This is a web feature + ExternalFramebuffer { + inner: web_sys::WebGlFramebuffer, + }, + #[cfg(native)] + /// Render to a `glow::NativeFramebuffer` + /// Useful when the framebuffer to draw to + /// has a non-zero framebuffer ID + /// + /// This is a native feature + ExternalNativeFramebuffer { + inner: glow::NativeFramebuffer, + }, +} + +#[cfg(send_sync)] +unsafe impl Sync for TextureInner {} +#[cfg(send_sync)] +unsafe impl Send for TextureInner {} + +impl TextureInner { + fn as_native(&self) -> (glow::Texture, BindTarget) { + match *self { + Self::Renderbuffer { .. } | Self::DefaultRenderbuffer => { + panic!("Unexpected renderbuffer"); + } + Self::Texture { raw, target } => (raw, target), + #[cfg(webgl)] + Self::ExternalFramebuffer { .. } => panic!("Unexpected external framebuffer"), + #[cfg(native)] + Self::ExternalNativeFramebuffer { .. } => panic!("unexpected external framebuffer"), + } + } +} + +#[derive(Debug)] +pub struct Texture { + pub inner: TextureInner, + pub mip_level_count: u32, + pub array_layer_count: u32, + pub format: wgt::TextureFormat, + pub format_desc: TextureFormatDesc, + pub copy_size: CopyExtent, + + // The `drop_guard` field must be the last field of this struct so it is dropped last. + // Do not add new fields after it. + pub drop_guard: Option<crate::DropGuard>, +} + +impl crate::DynTexture for Texture {} +impl crate::DynSurfaceTexture for Texture {} + +impl core::borrow::Borrow<dyn crate::DynTexture> for Texture { + fn borrow(&self) -> &dyn crate::DynTexture { + self + } +} + +impl Texture { + pub fn default_framebuffer(format: wgt::TextureFormat) -> Self { + Self { + inner: TextureInner::DefaultRenderbuffer, + drop_guard: None, + mip_level_count: 1, + array_layer_count: 1, + format, + format_desc: TextureFormatDesc { + internal: 0, + external: 0, + data_type: 0, + }, + copy_size: CopyExtent { + width: 0, + height: 0, + depth: 0, + }, + } + } + + /// Returns the `target`, whether the image is 3d and whether the image is a cubemap. + fn get_info_from_desc(desc: &TextureDescriptor) -> u32 { + match desc.dimension { + // WebGL (1 and 2) as well as some GLES versions do not have 1D textures, so we are + // doing `TEXTURE_2D` instead + wgt::TextureDimension::D1 => glow::TEXTURE_2D, + wgt::TextureDimension::D2 => { + // HACK: detect a cube map; forces cube compatible textures to be cube textures + match (desc.is_cube_compatible(), desc.size.depth_or_array_layers) { + (false, 1) => glow::TEXTURE_2D, + (false, _) => glow::TEXTURE_2D_ARRAY, + (true, 6) => glow::TEXTURE_CUBE_MAP, + (true, _) => glow::TEXTURE_CUBE_MAP_ARRAY, + } + } + wgt::TextureDimension::D3 => glow::TEXTURE_3D, + } + } + + /// More information can be found in issues #1614 and #1574 + fn log_failing_target_heuristics(view_dimension: wgt::TextureViewDimension, target: u32) { + let expected_target = match view_dimension { + wgt::TextureViewDimension::D1 => glow::TEXTURE_2D, + wgt::TextureViewDimension::D2 => glow::TEXTURE_2D, + wgt::TextureViewDimension::D2Array => glow::TEXTURE_2D_ARRAY, + wgt::TextureViewDimension::Cube => glow::TEXTURE_CUBE_MAP, + wgt::TextureViewDimension::CubeArray => glow::TEXTURE_CUBE_MAP_ARRAY, + wgt::TextureViewDimension::D3 => glow::TEXTURE_3D, + }; + + if expected_target == target { + return; + } + + let buffer; + let got = match target { + glow::TEXTURE_2D => "D2", + glow::TEXTURE_2D_ARRAY => "D2Array", + glow::TEXTURE_CUBE_MAP => "Cube", + glow::TEXTURE_CUBE_MAP_ARRAY => "CubeArray", + glow::TEXTURE_3D => "D3", + target => { + buffer = target.to_string(); + &buffer + } + }; + + log::error!( + concat!( + "wgpu-hal heuristics assumed that ", + "the view dimension will be equal to `{}` rather than `{:?}`.\n", + "`D2` textures with ", + "`depth_or_array_layers == 1` ", + "are assumed to have view dimension `D2`\n", + "`D2` textures with ", + "`depth_or_array_layers > 1` ", + "are assumed to have view dimension `D2Array`\n", + "`D2` textures with ", + "`depth_or_array_layers == 6` ", + "are assumed to have view dimension `Cube`\n", + "`D2` textures with ", + "`depth_or_array_layers > 6 && depth_or_array_layers % 6 == 0` ", + "are assumed to have view dimension `CubeArray`\n", + ), + got, + view_dimension, + ); + } +} + +#[derive(Clone, Debug)] +pub struct TextureView { + inner: TextureInner, + aspects: crate::FormatAspects, + mip_levels: Range<u32>, + array_layers: Range<u32>, + format: wgt::TextureFormat, +} + +impl crate::DynTextureView for TextureView {} + +#[derive(Debug)] +pub struct Sampler { + raw: glow::Sampler, +} + +impl crate::DynSampler for Sampler {} + +#[derive(Debug)] +pub struct BindGroupLayout { + entries: Arc<[wgt::BindGroupLayoutEntry]>, +} + +impl crate::DynBindGroupLayout for BindGroupLayout {} + +#[derive(Debug)] +struct BindGroupLayoutInfo { + entries: Arc<[wgt::BindGroupLayoutEntry]>, + /// Mapping of resources, indexed by `binding`, into the whole layout space. + /// For texture resources, the value is the texture slot index. + /// For sampler resources, the value is the index of the sampler in the whole layout. + /// For buffers, the value is the uniform or storage slot index. + /// For unused bindings, the value is `!0` + binding_to_slot: Box<[u8]>, +} + +#[derive(Debug)] +pub struct PipelineLayout { + group_infos: Box<[Option<BindGroupLayoutInfo>]>, + naga_options: naga::back::glsl::Options, +} + +impl crate::DynPipelineLayout for PipelineLayout {} + +impl PipelineLayout { + /// # Panics + /// If the pipeline layout does not contain a bind group layout used by + /// the resource binding. + fn get_slot(&self, br: &naga::ResourceBinding) -> u8 { + let group_info = self.group_infos[br.group as usize].as_ref().unwrap(); + group_info.binding_to_slot[br.binding as usize] + } +} + +#[derive(Debug)] +enum BindingRegister { + UniformBuffers, + StorageBuffers, + Textures, + Images, +} + +#[derive(Debug)] +enum RawBinding { + Buffer { + raw: glow::Buffer, + offset: i32, + size: i32, + }, + Texture { + raw: glow::Texture, + target: BindTarget, + aspects: crate::FormatAspects, + mip_levels: Range<u32>, + //TODO: array layers + }, + Image(ImageBinding), + Sampler(glow::Sampler), +} + +#[derive(Debug)] +pub struct BindGroup { + contents: Box<[RawBinding]>, +} + +impl crate::DynBindGroup for BindGroup {} + +type ShaderId = u32; + +#[derive(Debug)] +pub enum ShaderModuleSource { + Naga(crate::NagaShader), + Passthrough { source: String }, +} + +#[derive(Debug)] +pub struct ShaderModule { + source: ShaderModuleSource, + label: Option<String>, + id: ShaderId, +} + +impl crate::DynShaderModule for ShaderModule {} + +#[derive(Clone, Debug, Default)] +struct VertexFormatDesc { + element_count: i32, + element_format: u32, + attrib_kind: VertexAttribKind, +} + +#[derive(Clone, Debug, Default)] +struct AttributeDesc { + location: u32, + offset: u32, + buffer_index: u32, + format_desc: VertexFormatDesc, +} + +#[derive(Clone, Debug)] +struct BufferBinding { + raw: glow::Buffer, + offset: wgt::BufferAddress, +} + +#[derive(Clone, Debug)] +struct ImageBinding { + raw: glow::Texture, + mip_level: u32, + array_layer: Option<u32>, + access: u32, + format: u32, +} + +#[derive(Clone, Debug, Default, PartialEq)] +struct VertexBufferDesc { + step: wgt::VertexStepMode, + stride: u32, +} + +#[derive(Clone, Debug)] +struct ImmediateDesc { + location: glow::UniformLocation, + ty: nt::glsl::GlslUniformType, + offset: u32, + size_bytes: u32, +} + +#[cfg(send_sync)] +unsafe impl Sync for ImmediateDesc {} +#[cfg(send_sync)] +unsafe impl Send for ImmediateDesc {} + +/// For each texture in the pipeline layout, store the index of the only +/// sampler (in this layout) that the texture is used with. +type SamplerBindMap = [Option<u8>; MAX_TEXTURE_SLOTS]; + +#[derive(Debug)] +struct PipelineInner { + program: glow::Program, + sampler_map: SamplerBindMap, + first_instance_location: Option<glow::UniformLocation>, + immediates_descs: ArrayVec<ImmediateDesc, MAX_IMMEDIATES_COMMANDS>, + clip_distance_count: u32, +} + +#[cfg(send_sync)] +unsafe impl Sync for PipelineInner {} +#[cfg(send_sync)] +unsafe impl Send for PipelineInner {} + +#[derive(Clone, Debug)] +struct DepthState { + function: u32, + mask: bool, +} + +#[derive(Clone, Debug, PartialEq)] +struct BlendComponent { + src: u32, + dst: u32, + equation: u32, +} + +#[derive(Clone, Debug, PartialEq)] +struct BlendDesc { + alpha: BlendComponent, + color: BlendComponent, +} + +#[derive(Clone, Debug, Default, PartialEq)] +struct ColorTargetDesc { + mask: wgt::ColorWrites, + blend: Option<BlendDesc>, +} + +#[derive(Debug, PartialEq, Eq, Hash)] +struct ProgramStage { + naga_stage: naga::ShaderStage, + shader_id: ShaderId, + entry_point: String, + zero_initialize_workgroup_memory: bool, + constant_hash: Vec<u8>, +} + +#[derive(Debug, PartialEq, Eq, Hash)] +struct ProgramCacheKey { + stages: ArrayVec<ProgramStage, 3>, + group_to_binding_to_slot: Box<[Option<Box<[u8]>>]>, +} + +type ProgramCache = FastHashMap<ProgramCacheKey, Result<Arc<PipelineInner>, crate::PipelineError>>; + +#[derive(Debug)] +pub struct RenderPipeline { + inner: Arc<PipelineInner>, + primitive: wgt::PrimitiveState, + vertex_buffers: Box<[Option<VertexBufferDesc>]>, + vertex_attributes: Box<[AttributeDesc]>, + color_targets: Box<[ColorTargetDesc]>, + depth: Option<DepthState>, + depth_bias: wgt::DepthBiasState, + stencil: Option<StencilState>, + alpha_to_coverage_enabled: bool, +} + +impl crate::DynRenderPipeline for RenderPipeline {} + +#[cfg(send_sync)] +static_assertions::assert_impl_all!(RenderPipeline: Send, Sync); + +#[derive(Debug)] +pub struct ComputePipeline { + inner: Arc<PipelineInner>, +} + +impl crate::DynComputePipeline for ComputePipeline {} + +#[derive(Debug)] +pub struct RayTracingPipeline {} + +impl crate::DynRayTracingPipeline for RayTracingPipeline {} + +#[cfg(send_sync)] +static_assertions::assert_impl_all!(ComputePipeline: Send, Sync); + +#[derive(Debug)] +pub struct QuerySet { + queries: Box<[glow::Query]>, + target: BindTarget, +} + +impl crate::DynQuerySet for QuerySet {} + +#[derive(Debug)] +pub struct AccelerationStructure; + +impl crate::DynAccelerationStructure for AccelerationStructure {} + +#[derive(Debug)] +pub struct PipelineCache; + +impl crate::DynPipelineCache for PipelineCache {} + +#[derive(Clone, Debug, PartialEq)] +struct StencilOps { + pass: u32, + fail: u32, + depth_fail: u32, +} + +impl Default for StencilOps { + fn default() -> Self { + Self { + pass: glow::KEEP, + fail: glow::KEEP, + depth_fail: glow::KEEP, + } + } +} + +#[derive(Clone, Debug, PartialEq)] +struct StencilSide { + function: u32, + mask_read: u32, + mask_write: u32, + reference: u32, + ops: StencilOps, +} + +impl Default for StencilSide { + fn default() -> Self { + Self { + function: glow::ALWAYS, + mask_read: 0xFF, + mask_write: 0xFF, + reference: 0, + ops: StencilOps::default(), + } + } +} + +#[derive(Debug, Clone, Default)] +struct StencilState { + front: StencilSide, + back: StencilSide, +} + +#[derive(Clone, Debug, Default, PartialEq)] +struct PrimitiveState { + front_face: u32, + cull_face: u32, + unclipped_depth: bool, + polygon_mode: u32, +} + +type InvalidatedAttachments = ArrayVec<u32, { crate::MAX_COLOR_ATTACHMENTS + 2 }>; + +#[derive(Debug)] +enum Command { + Draw { + topology: u32, + first_vertex: u32, + vertex_count: u32, + first_instance: u32, + instance_count: u32, + first_instance_location: Option<glow::UniformLocation>, + }, + DrawIndexed { + topology: u32, + index_type: u32, + index_count: u32, + index_offset: wgt::BufferAddress, + base_vertex: i32, + first_instance: u32, + instance_count: u32, + first_instance_location: Option<glow::UniformLocation>, + }, + DrawIndirect { + topology: u32, + indirect_buf: glow::Buffer, + indirect_offset: wgt::BufferAddress, + first_instance_location: Option<glow::UniformLocation>, + }, + DrawIndexedIndirect { + topology: u32, + index_type: u32, + indirect_buf: glow::Buffer, + indirect_offset: wgt::BufferAddress, + first_instance_location: Option<glow::UniformLocation>, + }, + Dispatch([u32; 3]), + DispatchIndirect { + indirect_buf: glow::Buffer, + indirect_offset: wgt::BufferAddress, + }, + ClearBuffer { + dst: Buffer, + dst_target: BindTarget, + range: crate::MemoryRange, + }, + CopyBufferToBuffer { + src: Buffer, + src_target: BindTarget, + dst: Buffer, + dst_target: BindTarget, + copy: crate::BufferCopy, + }, + #[cfg(webgl)] + CopyExternalImageToTexture { + src: wgt::CopyExternalImageSourceInfo, + dst: glow::Texture, + dst_target: BindTarget, + dst_format: wgt::TextureFormat, + dst_premultiplication: bool, + copy: crate::TextureCopy, + }, + CopyTextureToTexture { + src: glow::Texture, + src_target: BindTarget, + dst: glow::Texture, + dst_target: BindTarget, + copy: crate::TextureCopy, + }, + CopyBufferToTexture { + src: Buffer, + #[allow(unused)] + src_target: BindTarget, + dst: glow::Texture, + dst_target: BindTarget, + dst_format: wgt::TextureFormat, + copy: crate::BufferTextureCopy, + }, + CopyTextureToBuffer { + src: glow::Texture, + src_target: BindTarget, + src_format: wgt::TextureFormat, + dst: Buffer, + #[allow(unused)] + dst_target: BindTarget, + copy: crate::BufferTextureCopy, + }, + SetIndexBuffer(glow::Buffer), + BeginQuery(glow::Query, BindTarget), + EndQuery(BindTarget), + TimestampQuery(glow::Query), + CopyQueryResults { + query_range: Range<u32>, + dst: Buffer, + dst_target: BindTarget, + dst_offset: wgt::BufferAddress, + }, + ResetFramebuffer { + is_default: bool, + }, + BindAttachment { + attachment: u32, + view: TextureView, + depth_slice: Option<u32>, + sample_count: u32, + }, + ResolveAttachment { + attachment: u32, + dst: TextureView, + size: wgt::Extent3d, + }, + InvalidateAttachments(InvalidatedAttachments), + SetDrawColorBuffers(u8), + ClearColorF { + draw_buffer: u32, + color: [f32; 4], + is_srgb: bool, + }, + ClearColorU(u32, [u32; 4]), + ClearColorI(u32, [i32; 4]), + ClearDepth(f32), + ClearStencil(u32), + // Clearing both the depth and stencil buffer individually appears to + // result in the stencil buffer failing to clear, atleast in WebGL. + // It is also more efficient to emit a single command instead of two for + // this. + ClearDepthAndStencil(f32, u32), + BufferBarrier(glow::Buffer, wgt::BufferUses), + TextureBarrier(wgt::TextureUses), + SetViewport { + rect: crate::Rect<i32>, + depth: Range<f32>, + }, + SetScissor(crate::Rect<i32>), + SetStencilFunc { + face: u32, + function: u32, + reference: u32, + read_mask: u32, + }, + SetStencilOps { + face: u32, + write_mask: u32, + ops: StencilOps, + }, + SetDepth(DepthState), + SetDepthBias(wgt::DepthBiasState), + ConfigureDepthStencil(crate::FormatAspects), + SetAlphaToCoverage(bool), + SetVertexAttribute { + buffer: Option<glow::Buffer>, + buffer_desc: VertexBufferDesc, + attribute_desc: AttributeDesc, + }, + UnsetVertexAttribute(u32), + SetVertexBuffer { + index: u32, + buffer: BufferBinding, + buffer_desc: VertexBufferDesc, + }, + SetProgram(glow::Program), + SetPrimitive(PrimitiveState), + SetBlendConstant([f32; 4]), + SetColorTarget { + draw_buffer_index: Option<u32>, + desc: ColorTargetDesc, + }, + BindBuffer { + target: BindTarget, + slot: u32, + buffer: glow::Buffer, + offset: i32, + size: i32, + }, + BindSampler(u32, Option<glow::Sampler>), + BindTexture { + slot: u32, + texture: glow::Texture, + target: BindTarget, + aspects: crate::FormatAspects, + mip_levels: Range<u32>, + }, + BindImage { + slot: u32, + binding: ImageBinding, + }, + InsertDebugMarker(Range<u32>), + PushDebugGroup(Range<u32>), + PopDebugGroup, + SetImmediates { + uniform: ImmediateDesc, + /// Offset from the start of the `data_bytes` + offset: u32, + }, + SetClipDistances { + old_count: u32, + new_count: u32, + }, +} + +#[derive(Default)] +pub struct CommandBuffer { + label: Option<String>, + commands: Vec<Command>, + data_bytes: Vec<u8>, + queries: Vec<glow::Query>, +} + +impl crate::DynCommandBuffer for CommandBuffer {} + +impl fmt::Debug for CommandBuffer { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut builder = f.debug_struct("CommandBuffer"); + if let Some(ref label) = self.label { + builder.field("label", label); + } + builder.finish() + } +} + +#[cfg(send_sync)] +unsafe impl Sync for CommandBuffer {} +#[cfg(send_sync)] +unsafe impl Send for CommandBuffer {} + +//TODO: we would have something like `Arc<typed_arena::Arena>` +// here and in the command buffers. So that everything grows +// inside the encoder and stays there until `reset_all`. + +pub struct CommandEncoder { + cmd_buffer: CommandBuffer, + state: command::State, + private_caps: PrivateCapabilities, + counters: Arc<wgt::HalCounters>, +} + +impl fmt::Debug for CommandEncoder { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("CommandEncoder") + .field("cmd_buffer", &self.cmd_buffer) + .finish() + } +} + +#[cfg(send_sync)] +unsafe impl Sync for CommandEncoder {} +#[cfg(send_sync)] +unsafe impl Send for CommandEncoder {} + +#[cfg(not(webgl))] +fn gl_debug_message_callback(source: u32, gltype: u32, id: u32, severity: u32, message: &str) { + let source_str = match source { + glow::DEBUG_SOURCE_API => "API", + glow::DEBUG_SOURCE_WINDOW_SYSTEM => "Window System", + glow::DEBUG_SOURCE_SHADER_COMPILER => "ShaderCompiler", + glow::DEBUG_SOURCE_THIRD_PARTY => "Third Party", + glow::DEBUG_SOURCE_APPLICATION => "Application", + glow::DEBUG_SOURCE_OTHER => "Other", + _ => unreachable!(), + }; + + let log_severity = match severity { + glow::DEBUG_SEVERITY_HIGH => log::Level::Error, + glow::DEBUG_SEVERITY_MEDIUM => log::Level::Warn, + glow::DEBUG_SEVERITY_LOW => log::Level::Debug, + glow::DEBUG_SEVERITY_NOTIFICATION => log::Level::Trace, + _ => unreachable!(), + }; + + let type_str = match gltype { + glow::DEBUG_TYPE_DEPRECATED_BEHAVIOR => "Deprecated Behavior", + glow::DEBUG_TYPE_ERROR => "Error", + glow::DEBUG_TYPE_MARKER => "Marker", + glow::DEBUG_TYPE_OTHER => "Other", + glow::DEBUG_TYPE_PERFORMANCE => "Performance", + glow::DEBUG_TYPE_POP_GROUP => "Pop Group", + glow::DEBUG_TYPE_PORTABILITY => "Portability", + glow::DEBUG_TYPE_PUSH_GROUP => "Push Group", + glow::DEBUG_TYPE_UNDEFINED_BEHAVIOR => "Undefined Behavior", + _ => unreachable!(), + }; + + let _ = std::panic::catch_unwind(|| { + log::log!( + log_severity, + "GLES: [{source_str}/{type_str}] ID {id} : {message}" + ); + }); + + #[cfg(feature = "validation_canary")] + if cfg!(debug_assertions) && log_severity == log::Level::Error { + // Set canary and continue + crate::VALIDATION_CANARY.add(message.to_string()); + } +} + +// If we are using `std`, then use `Mutex` to provide `Send` and `Sync` +cfg_if::cfg_if! { + if #[cfg(gles_with_std)] { + type MaybeMutex<T> = std::sync::Mutex<T>; + + fn lock<T>(mutex: &MaybeMutex<T>) -> std::sync::MutexGuard<'_, T> { + mutex.lock().unwrap() + } + } else { + // It should be impossible for any build configuration to trigger this error + // It is intended only as a guard against changes elsewhere causing the use of + // `RefCell` here to become unsound. + #[cfg(all(send_sync, not(feature = "fragile-send-sync-non-atomic-wasm")))] + compile_error!("cannot provide non-fragile Send+Sync without std"); + + type MaybeMutex<T> = core::cell::RefCell<T>; + + fn lock<T>(mutex: &MaybeMutex<T>) -> core::cell::RefMut<'_, T> { + mutex.borrow_mut() + } + } +} diff --git a/third_party/wgpu-hal/src/gles/queue.rs b/third_party/wgpu-hal/src/gles/queue.rs new file mode 100644 index 000000000..9984cd555 --- /dev/null +++ b/third_party/wgpu-hal/src/gles/queue.rs @@ -0,0 +1,1980 @@ +use alloc::sync::Arc; +use alloc::vec; +use core::sync::atomic::Ordering; + +use arrayvec::ArrayVec; +use glow::HasContext; + +use super::{conv::is_layered_target, lock, Command as C, PrivateCapabilities}; + +const DEBUG_ID: u32 = 0; + +fn extract_marker<'a>(data: &'a [u8], range: &core::ops::Range<u32>) -> &'a str { + core::str::from_utf8(&data[range.start as usize..range.end as usize]).unwrap() +} + +fn to_debug_str(s: &str) -> &str { + // The spec mentions that if the length given to debug functions is negative, + // the implementation will access the ptr and look for a null that terminates + // the string but some implementations will try to access the ptr even if the + // length is 0. + if s.is_empty() { + "<empty>" + } else { + s + } +} + +fn get_2d_target(target: u32, array_layer: u32) -> u32 { + const CUBEMAP_FACES: [u32; 6] = [ + glow::TEXTURE_CUBE_MAP_POSITIVE_X, + glow::TEXTURE_CUBE_MAP_NEGATIVE_X, + glow::TEXTURE_CUBE_MAP_POSITIVE_Y, + glow::TEXTURE_CUBE_MAP_NEGATIVE_Y, + glow::TEXTURE_CUBE_MAP_POSITIVE_Z, + glow::TEXTURE_CUBE_MAP_NEGATIVE_Z, + ]; + + match target { + glow::TEXTURE_2D => target, + glow::TEXTURE_CUBE_MAP => CUBEMAP_FACES[array_layer as usize], + _ => unreachable!(), + } +} + +fn get_z_offset(target: u32, base: &crate::TextureCopyBase) -> u32 { + match target { + glow::TEXTURE_2D_ARRAY | glow::TEXTURE_CUBE_MAP_ARRAY => base.array_layer, + glow::TEXTURE_3D => base.origin.z, + _ => unreachable!(), + } +} + +impl super::Queue { + /// Performs a manual shader clear, used as a workaround for a clearing bug on mesa + unsafe fn perform_shader_clear(&self, gl: &glow::Context, draw_buffer: u32, color: [f32; 4]) { + let shader_clear = self + .shader_clear_program + .as_ref() + .expect("shader_clear_program should always be set if the workaround is enabled"); + unsafe { gl.use_program(Some(shader_clear.program)) }; + unsafe { + gl.uniform_4_f32( + Some(&shader_clear.color_uniform_location), + color[0], + color[1], + color[2], + color[3], + ) + }; + unsafe { gl.disable(glow::DEPTH_TEST) }; + unsafe { gl.disable(glow::STENCIL_TEST) }; + unsafe { gl.disable(glow::SCISSOR_TEST) }; + unsafe { gl.disable(glow::BLEND) }; + unsafe { gl.disable(glow::CULL_FACE) }; + unsafe { gl.draw_buffers(&[glow::COLOR_ATTACHMENT0 + draw_buffer]) }; + unsafe { gl.draw_arrays(glow::TRIANGLES, 0, 3) }; + + let draw_buffer_count = self.draw_buffer_count.load(Ordering::Relaxed); + if draw_buffer_count != 0 { + // Reset the draw buffers to what they were before the clear + let indices = (0..draw_buffer_count as u32) + .map(|i| glow::COLOR_ATTACHMENT0 + i) + .collect::<ArrayVec<_, { crate::MAX_COLOR_ATTACHMENTS }>>(); + unsafe { gl.draw_buffers(&indices) }; + } + } + + unsafe fn reset_state(&self, gl: &glow::Context) { + unsafe { gl.use_program(None) }; + unsafe { gl.bind_framebuffer(glow::FRAMEBUFFER, None) }; + unsafe { gl.disable(glow::DEPTH_TEST) }; + unsafe { gl.disable(glow::STENCIL_TEST) }; + unsafe { gl.disable(glow::SCISSOR_TEST) }; + unsafe { gl.disable(glow::BLEND) }; + unsafe { gl.disable(glow::CULL_FACE) }; + unsafe { gl.disable(glow::POLYGON_OFFSET_FILL) }; + unsafe { gl.disable(glow::SAMPLE_ALPHA_TO_COVERAGE) }; + if self.features.contains(wgt::Features::DEPTH_CLIP_CONTROL) { + unsafe { gl.disable(glow::DEPTH_CLAMP) }; + } + + unsafe { gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, None) }; + let mut current_index_buffer = self.current_index_buffer.lock(); + *current_index_buffer = None; + } + + unsafe fn set_attachment( + &self, + gl: &glow::Context, + fbo_target: u32, + attachment: u32, + view: &super::TextureView, + depth_slice: Option<u32>, + sample_count: u32, + ) { + match view.inner { + super::TextureInner::Renderbuffer { raw } => { + unsafe { + gl.framebuffer_renderbuffer( + fbo_target, + attachment, + glow::RENDERBUFFER, + Some(raw), + ) + }; + } + super::TextureInner::DefaultRenderbuffer => panic!("Unexpected default RBO"), + super::TextureInner::Texture { raw, target } => { + let num_layers = view.array_layers.end - view.array_layers.start; + if num_layers > 1 { + #[cfg(webgl)] + unsafe { + gl.framebuffer_texture_multiview_ovr( + fbo_target, + attachment, + Some(raw), + view.mip_levels.start as i32, + view.array_layers.start as i32, + num_layers as i32, + ) + }; + } else if is_layered_target(target) { + let layer = if target == glow::TEXTURE_3D { + depth_slice.unwrap() as i32 + } else { + view.array_layers.start as i32 + }; + unsafe { + gl.framebuffer_texture_layer( + fbo_target, + attachment, + Some(raw), + view.mip_levels.start as i32, + layer, + ) + }; + } else { + unsafe { + assert_eq!(view.mip_levels.len(), 1); + if sample_count != 1 { + gl.framebuffer_texture_2d_multisample( + fbo_target, + attachment, + get_2d_target(target, view.array_layers.start), + Some(raw), + view.mip_levels.start as i32, + sample_count as i32, + ) + } else { + gl.framebuffer_texture_2d( + fbo_target, + attachment, + get_2d_target(target, view.array_layers.start), + Some(raw), + view.mip_levels.start as i32, + ) + } + }; + } + } + #[cfg(webgl)] + super::TextureInner::ExternalFramebuffer { ref inner } => unsafe { + gl.bind_external_framebuffer(glow::FRAMEBUFFER, inner); + }, + #[cfg(native)] + super::TextureInner::ExternalNativeFramebuffer { ref inner } => unsafe { + gl.bind_framebuffer(glow::FRAMEBUFFER, Some(*inner)); + }, + } + } + + unsafe fn process( + &self, + gl: &glow::Context, + command: &C, + #[cfg_attr(target_arch = "wasm32", allow(unused))] data_bytes: &[u8], + queries: &[glow::Query], + ) { + match *command { + C::Draw { + topology, + first_vertex, + vertex_count, + instance_count, + first_instance, + ref first_instance_location, + } => { + let supports_full_instancing = self + .shared + .private_caps + .contains(PrivateCapabilities::FULLY_FEATURED_INSTANCING); + + if supports_full_instancing { + unsafe { + gl.draw_arrays_instanced_base_instance( + topology, + first_vertex as i32, + vertex_count as i32, + instance_count as i32, + first_instance, + ) + } + } else { + unsafe { + gl.uniform_1_u32(first_instance_location.as_ref(), first_instance); + } + + // Don't use `gl.draw_arrays` for `instance_count == 1`. + // Angle has a bug where it doesn't consider the instance divisor when `DYNAMIC_DRAW` is used in `draw_arrays`. + // See https://github.com/gfx-rs/wgpu/issues/3578 + unsafe { + gl.draw_arrays_instanced( + topology, + first_vertex as i32, + vertex_count as i32, + instance_count as i32, + ) + } + }; + } + C::DrawIndexed { + topology, + index_type, + index_count, + index_offset, + base_vertex, + first_instance, + instance_count, + ref first_instance_location, + } => { + let supports_full_instancing = self + .shared + .private_caps + .contains(PrivateCapabilities::FULLY_FEATURED_INSTANCING); + + if supports_full_instancing { + unsafe { + gl.draw_elements_instanced_base_vertex_base_instance( + topology, + index_count as i32, + index_type, + index_offset as i32, + instance_count as i32, + base_vertex, + first_instance, + ) + } + } else { + unsafe { gl.uniform_1_u32(first_instance_location.as_ref(), first_instance) }; + + if base_vertex == 0 { + unsafe { + // Don't use `gl.draw_elements`/`gl.draw_elements_base_vertex` for `instance_count == 1`. + // Angle has a bug where it doesn't consider the instance divisor when `DYNAMIC_DRAW` is used in `gl.draw_elements`/`gl.draw_elements_base_vertex`. + // See https://github.com/gfx-rs/wgpu/issues/3578 + gl.draw_elements_instanced( + topology, + index_count as i32, + index_type, + index_offset as i32, + instance_count as i32, + ) + } + } else { + // If we've gotten here, wgpu-core has already validated that this function exists via the DownlevelFlags::BASE_VERTEX feature. + unsafe { + gl.draw_elements_instanced_base_vertex( + topology, + index_count as _, + index_type, + index_offset as i32, + instance_count as i32, + base_vertex, + ) + } + } + } + } + C::DrawIndirect { + topology, + indirect_buf, + indirect_offset, + ref first_instance_location, + } => { + unsafe { gl.uniform_1_u32(first_instance_location.as_ref(), 0) }; + + unsafe { gl.bind_buffer(glow::DRAW_INDIRECT_BUFFER, Some(indirect_buf)) }; + unsafe { gl.draw_arrays_indirect_offset(topology, indirect_offset as i32) }; + } + C::DrawIndexedIndirect { + topology, + index_type, + indirect_buf, + indirect_offset, + ref first_instance_location, + } => { + unsafe { gl.uniform_1_u32(first_instance_location.as_ref(), 0) }; + + unsafe { gl.bind_buffer(glow::DRAW_INDIRECT_BUFFER, Some(indirect_buf)) }; + unsafe { + gl.draw_elements_indirect_offset(topology, index_type, indirect_offset as i32) + }; + } + C::Dispatch(group_counts) => { + unsafe { gl.dispatch_compute(group_counts[0], group_counts[1], group_counts[2]) }; + } + C::DispatchIndirect { + indirect_buf, + indirect_offset, + } => { + unsafe { gl.bind_buffer(glow::DISPATCH_INDIRECT_BUFFER, Some(indirect_buf)) }; + unsafe { gl.dispatch_compute_indirect(indirect_offset as i32) }; + } + C::ClearBuffer { + ref dst, + dst_target, + ref range, + } => match dst.raw { + Some(buffer) => { + // When `INDEX_BUFFER_ROLE_CHANGE` isn't available, we can't copy into the + // index buffer from the zero buffer. This would fail in Chrome with the + // following message: + // + // > Cannot copy into an element buffer destination from a non-element buffer + // > source + // + // Instead, we'll upload zeroes into the buffer. + let can_use_zero_buffer = self + .shared + .private_caps + .contains(PrivateCapabilities::INDEX_BUFFER_ROLE_CHANGE) + || dst_target != glow::ELEMENT_ARRAY_BUFFER; + + if can_use_zero_buffer { + unsafe { gl.bind_buffer(glow::COPY_READ_BUFFER, Some(self.zero_buffer)) }; + unsafe { gl.bind_buffer(dst_target, Some(buffer)) }; + let mut dst_offset = range.start; + while dst_offset < range.end { + let size = (range.end - dst_offset).min(super::ZERO_BUFFER_SIZE as u64); + unsafe { + gl.copy_buffer_sub_data( + glow::COPY_READ_BUFFER, + dst_target, + 0, + dst_offset as i32, + size as i32, + ) + }; + dst_offset += size; + } + } else { + unsafe { gl.bind_buffer(dst_target, Some(buffer)) }; + let zeroes = vec![0u8; (range.end - range.start) as usize]; + unsafe { + gl.buffer_sub_data_u8_slice(dst_target, range.start as i32, &zeroes) + }; + } + } + None => { + let mut map_state = lock(&dst.map_state); + map_state.data.as_mut().unwrap().as_mut_slice() + [range.start as usize..range.end as usize] + .fill(0); + } + }, + C::CopyBufferToBuffer { + ref src, + src_target, + ref dst, + dst_target, + copy, + } => { + let copy_src_target = glow::COPY_READ_BUFFER; + let is_index_buffer_only_element_dst = !self + .shared + .private_caps + .contains(PrivateCapabilities::INDEX_BUFFER_ROLE_CHANGE) + && dst_target == glow::ELEMENT_ARRAY_BUFFER + || src_target == glow::ELEMENT_ARRAY_BUFFER; + + // WebGL not allowed to copy data from other targets to element buffer and can't copy element data to other buffers + let copy_dst_target = if is_index_buffer_only_element_dst { + glow::ELEMENT_ARRAY_BUFFER + } else { + glow::COPY_WRITE_BUFFER + }; + let size = copy.size.get() as usize; + match (src.raw, dst.raw) { + (Some(ref src), Some(ref dst)) => { + unsafe { gl.bind_buffer(copy_src_target, Some(*src)) }; + unsafe { gl.bind_buffer(copy_dst_target, Some(*dst)) }; + unsafe { + gl.copy_buffer_sub_data( + copy_src_target, + copy_dst_target, + copy.src_offset as _, + copy.dst_offset as _, + copy.size.get() as _, + ) + }; + } + (Some(src), None) => { + let mut map_state = lock(&dst.map_state); + let dst_data = &mut map_state.data.as_mut().unwrap().as_mut_slice() + [copy.dst_offset as usize..copy.dst_offset as usize + size]; + + unsafe { gl.bind_buffer(copy_src_target, Some(src)) }; + unsafe { + self.shared.get_buffer_sub_data( + gl, + copy_src_target, + copy.src_offset as i32, + dst_data, + ) + }; + } + (None, Some(dst)) => { + let map_state = lock(&src.map_state); + let src_data = &map_state.data.as_ref().unwrap().as_slice() + [copy.src_offset as usize..copy.src_offset as usize + size]; + unsafe { gl.bind_buffer(copy_dst_target, Some(dst)) }; + unsafe { + gl.buffer_sub_data_u8_slice( + copy_dst_target, + copy.dst_offset as i32, + src_data, + ) + }; + } + (None, None) => { + todo!() + } + } + unsafe { gl.bind_buffer(copy_src_target, None) }; + if is_index_buffer_only_element_dst { + unsafe { + gl.bind_buffer( + glow::ELEMENT_ARRAY_BUFFER, + *self.current_index_buffer.lock(), + ) + }; + } else { + unsafe { gl.bind_buffer(copy_dst_target, None) }; + } + } + #[cfg(webgl)] + C::CopyExternalImageToTexture { + ref src, + dst, + dst_target, + dst_format, + dst_premultiplication, + ref copy, + } => { + const UNPACK_FLIP_Y_WEBGL: u32 = + web_sys::WebGl2RenderingContext::UNPACK_FLIP_Y_WEBGL; + const UNPACK_PREMULTIPLY_ALPHA_WEBGL: u32 = + web_sys::WebGl2RenderingContext::UNPACK_PREMULTIPLY_ALPHA_WEBGL; + + unsafe { + if src.flip_y { + gl.pixel_store_bool(UNPACK_FLIP_Y_WEBGL, true); + } + if dst_premultiplication { + gl.pixel_store_bool(UNPACK_PREMULTIPLY_ALPHA_WEBGL, true); + } + } + + unsafe { gl.bind_texture(dst_target, Some(dst)) }; + let format_desc = self.shared.describe_texture_format(dst_format); + if is_layered_target(dst_target) { + let z_offset = get_z_offset(dst_target, ©.dst_base); + + match src.source { + wgt::ExternalImageSource::ImageBitmap(ref b) => unsafe { + gl.tex_sub_image_3d_with_image_bitmap( + dst_target, + copy.dst_base.mip_level as i32, + copy.dst_base.origin.x as i32, + copy.dst_base.origin.y as i32, + z_offset as i32, + copy.size.width as i32, + copy.size.height as i32, + copy.size.depth as i32, + format_desc.external, + format_desc.data_type, + b, + ); + }, + wgt::ExternalImageSource::HTMLImageElement(ref i) => unsafe { + gl.tex_sub_image_3d_with_html_image_element( + dst_target, + copy.dst_base.mip_level as i32, + copy.dst_base.origin.x as i32, + copy.dst_base.origin.y as i32, + z_offset as i32, + copy.size.width as i32, + copy.size.height as i32, + copy.size.depth as i32, + format_desc.external, + format_desc.data_type, + i, + ); + }, + wgt::ExternalImageSource::HTMLVideoElement(ref v) => unsafe { + gl.tex_sub_image_3d_with_html_video_element( + dst_target, + copy.dst_base.mip_level as i32, + copy.dst_base.origin.x as i32, + copy.dst_base.origin.y as i32, + z_offset as i32, + copy.size.width as i32, + copy.size.height as i32, + copy.size.depth as i32, + format_desc.external, + format_desc.data_type, + v, + ); + }, + #[cfg(not(web_sys_unstable_apis))] + wgt::ExternalImageSource::VideoFrame(_) => { + unimplemented!("web_sys_unstable_apis is needed for glow") + } + #[cfg(web_sys_unstable_apis)] + wgt::ExternalImageSource::VideoFrame(ref v) => unsafe { + gl.tex_sub_image_3d_with_video_frame( + dst_target, + copy.dst_base.mip_level as i32, + copy.dst_base.origin.x as i32, + copy.dst_base.origin.y as i32, + z_offset as i32, + copy.size.width as i32, + copy.size.height as i32, + copy.size.depth as i32, + format_desc.external, + format_desc.data_type, + v, + ) + }, + wgt::ExternalImageSource::ImageData(ref i) => unsafe { + gl.tex_sub_image_3d_with_image_data( + dst_target, + copy.dst_base.mip_level as i32, + copy.dst_base.origin.x as i32, + copy.dst_base.origin.y as i32, + z_offset as i32, + copy.size.width as i32, + copy.size.height as i32, + copy.size.depth as i32, + format_desc.external, + format_desc.data_type, + i, + ); + }, + wgt::ExternalImageSource::HTMLCanvasElement(ref c) => unsafe { + gl.tex_sub_image_3d_with_html_canvas_element( + dst_target, + copy.dst_base.mip_level as i32, + copy.dst_base.origin.x as i32, + copy.dst_base.origin.y as i32, + z_offset as i32, + copy.size.width as i32, + copy.size.height as i32, + copy.size.depth as i32, + format_desc.external, + format_desc.data_type, + c, + ); + }, + wgt::ExternalImageSource::OffscreenCanvas(_) => unreachable!(), + } + } else { + let dst_target = get_2d_target(dst_target, copy.dst_base.array_layer); + + match src.source { + wgt::ExternalImageSource::ImageBitmap(ref b) => unsafe { + gl.tex_sub_image_2d_with_image_bitmap_and_width_and_height( + dst_target, + copy.dst_base.mip_level as i32, + copy.dst_base.origin.x as i32, + copy.dst_base.origin.y as i32, + copy.size.width as i32, + copy.size.height as i32, + format_desc.external, + format_desc.data_type, + b, + ); + }, + wgt::ExternalImageSource::HTMLImageElement(ref i) => unsafe { + gl.tex_sub_image_2d_with_html_image_and_width_and_height( + dst_target, + copy.dst_base.mip_level as i32, + copy.dst_base.origin.x as i32, + copy.dst_base.origin.y as i32, + copy.size.width as i32, + copy.size.height as i32, + format_desc.external, + format_desc.data_type, + i, + ) + }, + wgt::ExternalImageSource::HTMLVideoElement(ref v) => unsafe { + gl.tex_sub_image_2d_with_html_video_and_width_and_height( + dst_target, + copy.dst_base.mip_level as i32, + copy.dst_base.origin.x as i32, + copy.dst_base.origin.y as i32, + copy.size.width as i32, + copy.size.height as i32, + format_desc.external, + format_desc.data_type, + v, + ) + }, + #[cfg(not(web_sys_unstable_apis))] + wgt::ExternalImageSource::VideoFrame(_) => { + unimplemented!("web_sys_unstable_apis is needed for glow") + } + #[cfg(web_sys_unstable_apis)] + wgt::ExternalImageSource::VideoFrame(ref v) => unsafe { + gl.tex_sub_image_2d_with_video_frame_and_width_and_height( + dst_target, + copy.dst_base.mip_level as i32, + copy.dst_base.origin.x as i32, + copy.dst_base.origin.y as i32, + copy.size.width as i32, + copy.size.height as i32, + format_desc.external, + format_desc.data_type, + v, + ) + }, + wgt::ExternalImageSource::ImageData(ref i) => unsafe { + gl.tex_sub_image_2d_with_image_data_and_width_and_height( + dst_target, + copy.dst_base.mip_level as i32, + copy.dst_base.origin.x as i32, + copy.dst_base.origin.y as i32, + copy.size.width as i32, + copy.size.height as i32, + format_desc.external, + format_desc.data_type, + i, + ); + }, + wgt::ExternalImageSource::HTMLCanvasElement(ref c) => unsafe { + gl.tex_sub_image_2d_with_html_canvas_and_width_and_height( + dst_target, + copy.dst_base.mip_level as i32, + copy.dst_base.origin.x as i32, + copy.dst_base.origin.y as i32, + copy.size.width as i32, + copy.size.height as i32, + format_desc.external, + format_desc.data_type, + c, + ) + }, + wgt::ExternalImageSource::OffscreenCanvas(_) => unreachable!(), + } + } + + unsafe { + if src.flip_y { + gl.pixel_store_bool(UNPACK_FLIP_Y_WEBGL, false); + } + if dst_premultiplication { + gl.pixel_store_bool(UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); + } + } + } + C::CopyTextureToTexture { + src, + src_target, + dst, + dst_target, + ref copy, + } => { + //TODO: handle 3D copies + unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(self.copy_fbo)) }; + if is_layered_target(src_target) { + //TODO: handle GLES without framebuffer_texture_3d + unsafe { + gl.framebuffer_texture_layer( + glow::READ_FRAMEBUFFER, + glow::COLOR_ATTACHMENT0, + Some(src), + copy.src_base.mip_level as i32, + copy.src_base.array_layer as i32, + ) + }; + } else { + unsafe { + gl.framebuffer_texture_2d( + glow::READ_FRAMEBUFFER, + glow::COLOR_ATTACHMENT0, + src_target, + Some(src), + copy.src_base.mip_level as i32, + ) + }; + } + + unsafe { gl.bind_texture(dst_target, Some(dst)) }; + if is_layered_target(dst_target) { + unsafe { + gl.copy_tex_sub_image_3d( + dst_target, + copy.dst_base.mip_level as i32, + copy.dst_base.origin.x as i32, + copy.dst_base.origin.y as i32, + get_z_offset(dst_target, ©.dst_base) as i32, + copy.src_base.origin.x as i32, + copy.src_base.origin.y as i32, + copy.size.width as i32, + copy.size.height as i32, + ) + }; + } else { + unsafe { + gl.copy_tex_sub_image_2d( + get_2d_target(dst_target, copy.dst_base.array_layer), + copy.dst_base.mip_level as i32, + copy.dst_base.origin.x as i32, + copy.dst_base.origin.y as i32, + copy.src_base.origin.x as i32, + copy.src_base.origin.y as i32, + copy.size.width as i32, + copy.size.height as i32, + ) + }; + } + } + C::CopyBufferToTexture { + ref src, + src_target: _, + dst, + dst_target, + dst_format, + ref copy, + } => { + let (block_width, block_height) = dst_format.block_dimensions(); + let block_size = dst_format.block_copy_size(None).unwrap(); + let format_desc = self.shared.describe_texture_format(dst_format); + let row_texels = copy + .buffer_layout + .bytes_per_row + .map_or(0, |bpr| block_width * bpr / block_size); + let column_texels = copy + .buffer_layout + .rows_per_image + .map_or(0, |rpi| block_height * rpi); + + unsafe { gl.bind_texture(dst_target, Some(dst)) }; + unsafe { gl.pixel_store_i32(glow::UNPACK_ROW_LENGTH, row_texels as i32) }; + unsafe { gl.pixel_store_i32(glow::UNPACK_IMAGE_HEIGHT, column_texels as i32) }; + let mut unbind_unpack_buffer = false; + if !dst_format.is_compressed() { + let map_state; + let unpack_data = match src.raw { + Some(buffer) => { + unsafe { gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, Some(buffer)) }; + unbind_unpack_buffer = true; + glow::PixelUnpackData::BufferOffset(copy.buffer_layout.offset as u32) + } + None => { + map_state = lock(&src.map_state); + let src_data = &map_state.data.as_ref().unwrap().as_slice() + [copy.buffer_layout.offset as usize..]; + glow::PixelUnpackData::Slice(Some(src_data)) + } + }; + if is_layered_target(dst_target) { + unsafe { + gl.tex_sub_image_3d( + dst_target, + copy.texture_base.mip_level as i32, + copy.texture_base.origin.x as i32, + copy.texture_base.origin.y as i32, + get_z_offset(dst_target, ©.texture_base) as i32, + copy.size.width as i32, + copy.size.height as i32, + copy.size.depth as i32, + format_desc.external, + format_desc.data_type, + unpack_data, + ) + }; + } else { + unsafe { + gl.tex_sub_image_2d( + get_2d_target(dst_target, copy.texture_base.array_layer), + copy.texture_base.mip_level as i32, + copy.texture_base.origin.x as i32, + copy.texture_base.origin.y as i32, + copy.size.width as i32, + copy.size.height as i32, + format_desc.external, + format_desc.data_type, + unpack_data, + ) + }; + } + } else { + let bytes_per_row = copy + .buffer_layout + .bytes_per_row + .unwrap_or(copy.size.width * block_size); + let minimum_rows_per_image = copy.size.height.div_ceil(block_height); + let rows_per_image = copy + .buffer_layout + .rows_per_image + .unwrap_or(minimum_rows_per_image); + + let bytes_per_image = bytes_per_row * rows_per_image; + let minimum_bytes_per_image = bytes_per_row * minimum_rows_per_image; + let bytes_in_upload = + (bytes_per_image * (copy.size.depth - 1)) + minimum_bytes_per_image; + let offset = copy.buffer_layout.offset as u32; + + let map_state; + let unpack_data = match src.raw { + Some(buffer) => { + unsafe { gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, Some(buffer)) }; + unbind_unpack_buffer = true; + glow::CompressedPixelUnpackData::BufferRange( + offset..offset + bytes_in_upload, + ) + } + None => { + map_state = lock(&src.map_state); + let src_data = &map_state.data.as_ref().unwrap().as_slice() + [(offset as usize)..(offset + bytes_in_upload) as usize]; + glow::CompressedPixelUnpackData::Slice(src_data) + } + }; + + if is_layered_target(dst_target) { + unsafe { + gl.compressed_tex_sub_image_3d( + dst_target, + copy.texture_base.mip_level as i32, + copy.texture_base.origin.x as i32, + copy.texture_base.origin.y as i32, + get_z_offset(dst_target, ©.texture_base) as i32, + copy.size.width as i32, + copy.size.height as i32, + copy.size.depth as i32, + format_desc.internal, + unpack_data, + ) + }; + } else { + unsafe { + gl.compressed_tex_sub_image_2d( + get_2d_target(dst_target, copy.texture_base.array_layer), + copy.texture_base.mip_level as i32, + copy.texture_base.origin.x as i32, + copy.texture_base.origin.y as i32, + copy.size.width as i32, + copy.size.height as i32, + format_desc.internal, + unpack_data, + ) + }; + } + } + if unbind_unpack_buffer { + unsafe { gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, None) }; + } + } + C::CopyTextureToBuffer { + src, + src_target, + src_format, + ref dst, + dst_target: _, + ref copy, + } => { + let block_size = src_format.block_copy_size(None).unwrap(); + if src_format.is_compressed() { + log::error!("Not implemented yet: compressed texture copy to buffer"); + return; + } + if src_target == glow::TEXTURE_CUBE_MAP + || src_target == glow::TEXTURE_CUBE_MAP_ARRAY + { + log::error!("Not implemented yet: cubemap texture copy to buffer"); + return; + } + let format_desc = self.shared.describe_texture_format(src_format); + let row_texels = copy + .buffer_layout + .bytes_per_row + .map_or(copy.size.width, |bpr| bpr / block_size); + let column_texels = copy + .buffer_layout + .rows_per_image + .unwrap_or(copy.size.height); + + unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(self.copy_fbo)) }; + + let read_pixels = |offset| { + let mut map_state; + let unpack_data = match dst.raw { + Some(buffer) => { + unsafe { gl.pixel_store_i32(glow::PACK_ROW_LENGTH, row_texels as i32) }; + unsafe { gl.bind_buffer(glow::PIXEL_PACK_BUFFER, Some(buffer)) }; + glow::PixelPackData::BufferOffset(offset as u32) + } + None => { + map_state = lock(&dst.map_state); + let dst_data = &mut map_state.data.as_mut().unwrap().as_mut_slice() + [offset as usize..]; + glow::PixelPackData::Slice(Some(dst_data)) + } + }; + unsafe { + gl.read_pixels( + copy.texture_base.origin.x as i32, + copy.texture_base.origin.y as i32, + copy.size.width as i32, + copy.size.height as i32, + format_desc.external, + format_desc.data_type, + unpack_data, + ) + }; + }; + + match src_target { + glow::TEXTURE_2D => { + unsafe { + gl.framebuffer_texture_2d( + glow::READ_FRAMEBUFFER, + glow::COLOR_ATTACHMENT0, + src_target, + Some(src), + copy.texture_base.mip_level as i32, + ) + }; + read_pixels(copy.buffer_layout.offset); + } + glow::TEXTURE_2D_ARRAY => { + unsafe { + gl.framebuffer_texture_layer( + glow::READ_FRAMEBUFFER, + glow::COLOR_ATTACHMENT0, + Some(src), + copy.texture_base.mip_level as i32, + copy.texture_base.array_layer as i32, + ) + }; + read_pixels(copy.buffer_layout.offset); + } + glow::TEXTURE_3D => { + for z in copy.texture_base.origin.z..copy.size.depth { + unsafe { + gl.framebuffer_texture_layer( + glow::READ_FRAMEBUFFER, + glow::COLOR_ATTACHMENT0, + Some(src), + copy.texture_base.mip_level as i32, + z as i32, + ) + }; + let offset = copy.buffer_layout.offset + + (z * block_size * row_texels * column_texels) as u64; + read_pixels(offset); + } + } + glow::TEXTURE_CUBE_MAP | glow::TEXTURE_CUBE_MAP_ARRAY => unimplemented!(), + _ => unreachable!(), + } + } + C::SetIndexBuffer(buffer) => { + unsafe { gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(buffer)) }; + let mut current_index_buffer = self.current_index_buffer.lock(); + *current_index_buffer = Some(buffer); + } + C::BeginQuery(query, target) => { + unsafe { gl.begin_query(target, query) }; + } + C::EndQuery(target) => { + unsafe { gl.end_query(target) }; + } + C::TimestampQuery(query) => { + unsafe { gl.query_counter(query, glow::TIMESTAMP) }; + } + C::CopyQueryResults { + ref query_range, + ref dst, + dst_target, + dst_offset, + } => { + if self + .shared + .private_caps + .contains(PrivateCapabilities::QUERY_BUFFERS) + && dst.raw.is_some() + { + unsafe { + // We're assuming that the only relevant queries are 8 byte timestamps or + // occlusion tests. + let query_size = 8; + + let query_range_size = query_size * query_range.len(); + + let buffer = gl.create_buffer().ok(); + gl.bind_buffer(glow::QUERY_BUFFER, buffer); + gl.buffer_data_size( + glow::QUERY_BUFFER, + query_range_size as _, + glow::STREAM_COPY, + ); + + for (i, &query) in queries + [query_range.start as usize..query_range.end as usize] + .iter() + .enumerate() + { + gl.get_query_parameter_u64_with_offset( + query, + glow::QUERY_RESULT, + query_size * i, + ) + } + gl.bind_buffer(dst_target, dst.raw); + gl.copy_buffer_sub_data( + glow::QUERY_BUFFER, + dst_target, + 0, + dst_offset as _, + query_range_size as _, + ); + if let Some(buffer) = buffer { + gl.delete_buffer(buffer) + } + } + } else { + let mut temp_query_results = self.temp_query_results.lock(); + temp_query_results.clear(); + for &query in + queries[query_range.start as usize..query_range.end as usize].iter() + { + let mut result: u64 = 0; + unsafe { + if self + .shared + .private_caps + .contains(PrivateCapabilities::QUERY_64BIT) + { + let result: *mut u64 = &mut result; + gl.get_query_parameter_u64_with_offset( + query, + glow::QUERY_RESULT, + result as usize, + ) + } else { + result = + gl.get_query_parameter_u32(query, glow::QUERY_RESULT) as u64; + } + }; + temp_query_results.push(result); + } + let query_data = bytemuck::cast_slice(&temp_query_results); + match dst.raw { + Some(buffer) => { + unsafe { gl.bind_buffer(dst_target, Some(buffer)) }; + unsafe { + gl.buffer_sub_data_u8_slice( + dst_target, + dst_offset as i32, + query_data, + ) + }; + } + None => { + let mut map_state = lock(&dst.map_state); + let data = map_state.data.as_mut().unwrap(); + let len = query_data.len().min(data.len()); + data[..len].copy_from_slice(&query_data[..len]); + } + } + } + } + C::ResetFramebuffer { is_default } => { + if is_default { + unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, None) }; + } else { + unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, Some(self.draw_fbo)) }; + unsafe { + gl.framebuffer_texture_2d( + glow::DRAW_FRAMEBUFFER, + glow::DEPTH_STENCIL_ATTACHMENT, + glow::TEXTURE_2D, + None, + 0, + ) + }; + for i in 0..self.shared.limits.max_color_attachments { + let target = glow::COLOR_ATTACHMENT0 + i; + unsafe { + gl.framebuffer_texture_2d( + glow::DRAW_FRAMEBUFFER, + target, + glow::TEXTURE_2D, + None, + 0, + ) + }; + } + } + unsafe { gl.color_mask(true, true, true, true) }; + unsafe { gl.depth_mask(true) }; + unsafe { gl.stencil_mask(!0) }; + unsafe { gl.disable(glow::DEPTH_TEST) }; + unsafe { gl.disable(glow::STENCIL_TEST) }; + unsafe { gl.disable(glow::SCISSOR_TEST) }; + } + C::BindAttachment { + attachment, + ref view, + depth_slice, + sample_count, + } => { + unsafe { + self.set_attachment( + gl, + glow::DRAW_FRAMEBUFFER, + attachment, + view, + depth_slice, + sample_count, + ) + }; + } + C::ResolveAttachment { + attachment, + ref dst, + ref size, + } => { + unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(self.draw_fbo)) }; + unsafe { gl.read_buffer(attachment) }; + unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, Some(self.copy_fbo)) }; + unsafe { + self.set_attachment( + gl, + glow::DRAW_FRAMEBUFFER, + glow::COLOR_ATTACHMENT0, + dst, + None, + 1, + ) + }; + unsafe { + gl.blit_framebuffer( + 0, + 0, + size.width as i32, + size.height as i32, + 0, + 0, + size.width as i32, + size.height as i32, + glow::COLOR_BUFFER_BIT, + glow::NEAREST, + ) + }; + unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, None) }; + unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, Some(self.draw_fbo)) }; + } + C::InvalidateAttachments(ref list) => { + if self + .shared + .private_caps + .contains(PrivateCapabilities::INVALIDATE_FRAMEBUFFER) + { + unsafe { gl.invalidate_framebuffer(glow::DRAW_FRAMEBUFFER, list) }; + } + } + C::SetDrawColorBuffers(count) => { + self.draw_buffer_count.store(count, Ordering::Relaxed); + let indices = (0..count as u32) + .map(|i| glow::COLOR_ATTACHMENT0 + i) + .collect::<ArrayVec<_, { crate::MAX_COLOR_ATTACHMENTS }>>(); + unsafe { gl.draw_buffers(&indices) }; + } + C::ClearColorF { + draw_buffer, + ref color, + is_srgb, + } => { + if self + .shared + .workarounds + .contains(super::Workarounds::MESA_I915_SRGB_SHADER_CLEAR) + && is_srgb + { + unsafe { self.perform_shader_clear(gl, draw_buffer, *color) }; + } else { + unsafe { gl.clear_buffer_f32_slice(glow::COLOR, draw_buffer, color) }; + } + } + C::ClearColorU(draw_buffer, ref color) => { + unsafe { gl.clear_buffer_u32_slice(glow::COLOR, draw_buffer, color) }; + } + C::ClearColorI(draw_buffer, ref color) => { + unsafe { gl.clear_buffer_i32_slice(glow::COLOR, draw_buffer, color) }; + } + C::ClearDepth(depth) => { + // Prefer `clear` as `clear_buffer` functions have issues on Sandy Bridge + // on Windows. + unsafe { + gl.clear_depth_f32(depth); + gl.clear(glow::DEPTH_BUFFER_BIT); + } + } + C::ClearStencil(value) => { + // Prefer `clear` as `clear_buffer` functions have issues on Sandy Bridge + // on Windows. + unsafe { + gl.clear_stencil(value as i32); + gl.clear(glow::STENCIL_BUFFER_BIT); + } + } + C::ClearDepthAndStencil(depth, stencil_value) => { + // Prefer `clear` as `clear_buffer` functions have issues on Sandy Bridge + // on Windows. + unsafe { + gl.clear_depth_f32(depth); + gl.clear_stencil(stencil_value as i32); + gl.clear(glow::DEPTH_BUFFER_BIT | glow::STENCIL_BUFFER_BIT); + } + } + C::BufferBarrier(raw, usage) => { + let mut flags = 0; + if usage.contains(wgt::BufferUses::VERTEX) { + flags |= glow::VERTEX_ATTRIB_ARRAY_BARRIER_BIT; + unsafe { gl.bind_buffer(glow::ARRAY_BUFFER, Some(raw)) }; + unsafe { gl.vertex_attrib_pointer_f32(0, 1, glow::BYTE, true, 0, 0) }; + } + if usage.contains(wgt::BufferUses::INDEX) { + flags |= glow::ELEMENT_ARRAY_BARRIER_BIT; + unsafe { gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(raw)) }; + } + if usage.contains(wgt::BufferUses::UNIFORM) { + flags |= glow::UNIFORM_BARRIER_BIT; + } + if usage.contains(wgt::BufferUses::INDIRECT) { + flags |= glow::COMMAND_BARRIER_BIT; + unsafe { gl.bind_buffer(glow::DRAW_INDIRECT_BUFFER, Some(raw)) }; + } + if usage.contains(wgt::BufferUses::COPY_SRC) { + flags |= glow::PIXEL_BUFFER_BARRIER_BIT; + unsafe { gl.bind_buffer(glow::PIXEL_UNPACK_BUFFER, Some(raw)) }; + } + if usage.contains(wgt::BufferUses::COPY_DST) { + flags |= glow::PIXEL_BUFFER_BARRIER_BIT; + unsafe { gl.bind_buffer(glow::PIXEL_PACK_BUFFER, Some(raw)) }; + } + if usage.intersects(wgt::BufferUses::MAP_READ | wgt::BufferUses::MAP_WRITE) { + flags |= glow::BUFFER_UPDATE_BARRIER_BIT; + } + if usage.intersects( + wgt::BufferUses::STORAGE_READ_ONLY | wgt::BufferUses::STORAGE_READ_WRITE, + ) { + flags |= glow::SHADER_STORAGE_BARRIER_BIT; + } + unsafe { gl.memory_barrier(flags) }; + } + // because `STORAGE_WRITE_ONLY` and `STORAGE_READ_WRITE` are only states + // we can transit from due OpenGL memory barriers are used to make _subsequent_ + // operations see changes from the _shader_ side. We filter out usage changes that are + // does not comes from the shader side in `transition_textures` + C::TextureBarrier(usage) => { + let mut flags = 0; + if usage.contains(wgt::TextureUses::RESOURCE) { + flags |= glow::TEXTURE_FETCH_BARRIER_BIT; + } + if usage.intersects( + wgt::TextureUses::STORAGE_READ_ONLY + | wgt::TextureUses::STORAGE_WRITE_ONLY + | wgt::TextureUses::STORAGE_READ_WRITE, + ) { + flags |= glow::SHADER_IMAGE_ACCESS_BARRIER_BIT; + } + if usage.intersects(wgt::TextureUses::COPY_SRC) { + flags |= glow::PIXEL_BUFFER_BARRIER_BIT; + } + if usage.contains(wgt::TextureUses::COPY_DST) { + flags |= glow::TEXTURE_UPDATE_BARRIER_BIT; + } + if usage.intersects( + wgt::TextureUses::COLOR_TARGET + | wgt::TextureUses::DEPTH_STENCIL_READ + | wgt::TextureUses::DEPTH_STENCIL_WRITE, + ) { + flags |= glow::FRAMEBUFFER_BARRIER_BIT; + } + unsafe { gl.memory_barrier(flags) }; + } + C::SetViewport { + ref rect, + ref depth, + } => { + unsafe { gl.viewport(rect.x, rect.y, rect.w, rect.h) }; + unsafe { gl.depth_range_f32(depth.start, depth.end) }; + } + C::SetScissor(ref rect) => { + unsafe { gl.scissor(rect.x, rect.y, rect.w, rect.h) }; + unsafe { gl.enable(glow::SCISSOR_TEST) }; + } + C::SetStencilFunc { + face, + function, + reference, + read_mask, + } => { + unsafe { gl.stencil_func_separate(face, function, reference as i32, read_mask) }; + } + C::SetStencilOps { + face, + write_mask, + ref ops, + } => { + unsafe { gl.stencil_mask_separate(face, write_mask) }; + unsafe { gl.stencil_op_separate(face, ops.fail, ops.depth_fail, ops.pass) }; + } + C::SetVertexAttribute { + buffer, + ref buffer_desc, + attribute_desc: ref vat, + } => { + unsafe { gl.bind_buffer(glow::ARRAY_BUFFER, buffer) }; + unsafe { gl.enable_vertex_attrib_array(vat.location) }; + + if buffer.is_none() { + match vat.format_desc.attrib_kind { + super::VertexAttribKind::Float => unsafe { + gl.vertex_attrib_format_f32( + vat.location, + vat.format_desc.element_count, + vat.format_desc.element_format, + true, // always normalized + vat.offset, + ) + }, + super::VertexAttribKind::Integer => unsafe { + gl.vertex_attrib_format_i32( + vat.location, + vat.format_desc.element_count, + vat.format_desc.element_format, + vat.offset, + ) + }, + } + + //Note: there is apparently a bug on AMD 3500U: + // this call is ignored if the current array is disabled. + unsafe { gl.vertex_attrib_binding(vat.location, vat.buffer_index) }; + } else { + match vat.format_desc.attrib_kind { + super::VertexAttribKind::Float => unsafe { + gl.vertex_attrib_pointer_f32( + vat.location, + vat.format_desc.element_count, + vat.format_desc.element_format, + true, // always normalized + buffer_desc.stride as i32, + vat.offset as i32, + ) + }, + super::VertexAttribKind::Integer => unsafe { + gl.vertex_attrib_pointer_i32( + vat.location, + vat.format_desc.element_count, + vat.format_desc.element_format, + buffer_desc.stride as i32, + vat.offset as i32, + ) + }, + } + unsafe { gl.vertex_attrib_divisor(vat.location, buffer_desc.step as u32) }; + } + } + C::UnsetVertexAttribute(location) => { + unsafe { gl.disable_vertex_attrib_array(location) }; + } + C::SetVertexBuffer { + index, + ref buffer, + ref buffer_desc, + } => { + unsafe { gl.vertex_binding_divisor(index, buffer_desc.step as u32) }; + unsafe { + gl.bind_vertex_buffer( + index, + Some(buffer.raw), + buffer.offset as i32, + buffer_desc.stride as i32, + ) + }; + } + C::SetDepth(ref depth) => { + unsafe { gl.depth_func(depth.function) }; + unsafe { gl.depth_mask(depth.mask) }; + } + C::SetDepthBias(bias) => { + if bias.is_enabled() { + unsafe { gl.enable(glow::POLYGON_OFFSET_FILL) }; + unsafe { gl.polygon_offset(bias.slope_scale, bias.constant as f32) }; + } else { + unsafe { gl.disable(glow::POLYGON_OFFSET_FILL) }; + } + } + C::ConfigureDepthStencil(aspects) => { + if aspects.contains(crate::FormatAspects::DEPTH) { + unsafe { gl.enable(glow::DEPTH_TEST) }; + } else { + unsafe { gl.disable(glow::DEPTH_TEST) }; + } + if aspects.contains(crate::FormatAspects::STENCIL) { + unsafe { gl.enable(glow::STENCIL_TEST) }; + } else { + unsafe { gl.disable(glow::STENCIL_TEST) }; + } + } + C::SetAlphaToCoverage(enabled) => { + if enabled { + unsafe { gl.enable(glow::SAMPLE_ALPHA_TO_COVERAGE) }; + } else { + unsafe { gl.disable(glow::SAMPLE_ALPHA_TO_COVERAGE) }; + } + } + C::SetProgram(program) => { + unsafe { gl.use_program(Some(program)) }; + } + C::SetPrimitive(ref state) => { + unsafe { gl.front_face(state.front_face) }; + if state.cull_face != 0 { + unsafe { gl.enable(glow::CULL_FACE) }; + unsafe { gl.cull_face(state.cull_face) }; + } else { + unsafe { gl.disable(glow::CULL_FACE) }; + } + if self.features.contains(wgt::Features::DEPTH_CLIP_CONTROL) { + //Note: this is a bit tricky, since we are controlling the clip, not the clamp. + if state.unclipped_depth { + unsafe { gl.enable(glow::DEPTH_CLAMP) }; + } else { + unsafe { gl.disable(glow::DEPTH_CLAMP) }; + } + } + // POLYGON_MODE_LINE also implies POLYGON_MODE_POINT + if self.features.contains(wgt::Features::POLYGON_MODE_LINE) { + unsafe { gl.polygon_mode(glow::FRONT_AND_BACK, state.polygon_mode) }; + } + } + C::SetBlendConstant(c) => { + unsafe { gl.blend_color(c[0], c[1], c[2], c[3]) }; + } + C::SetColorTarget { + draw_buffer_index, + desc: super::ColorTargetDesc { mask, ref blend }, + } => { + use wgt::ColorWrites as Cw; + if let Some(index) = draw_buffer_index { + unsafe { + gl.color_mask_draw_buffer( + index, + mask.contains(Cw::RED), + mask.contains(Cw::GREEN), + mask.contains(Cw::BLUE), + mask.contains(Cw::ALPHA), + ) + }; + if let Some(ref blend) = *blend { + unsafe { gl.enable_draw_buffer(glow::BLEND, index) }; + if blend.color != blend.alpha { + unsafe { + gl.blend_equation_separate_draw_buffer( + index, + blend.color.equation, + blend.alpha.equation, + ) + }; + unsafe { + gl.blend_func_separate_draw_buffer( + index, + blend.color.src, + blend.color.dst, + blend.alpha.src, + blend.alpha.dst, + ) + }; + } else { + unsafe { gl.blend_equation_draw_buffer(index, blend.color.equation) }; + unsafe { + gl.blend_func_draw_buffer(index, blend.color.src, blend.color.dst) + }; + } + } else { + unsafe { gl.disable_draw_buffer(glow::BLEND, index) }; + } + } else { + unsafe { + gl.color_mask( + mask.contains(Cw::RED), + mask.contains(Cw::GREEN), + mask.contains(Cw::BLUE), + mask.contains(Cw::ALPHA), + ) + }; + if let Some(ref blend) = *blend { + unsafe { gl.enable(glow::BLEND) }; + if blend.color != blend.alpha { + unsafe { + gl.blend_equation_separate( + blend.color.equation, + blend.alpha.equation, + ) + }; + unsafe { + gl.blend_func_separate( + blend.color.src, + blend.color.dst, + blend.alpha.src, + blend.alpha.dst, + ) + }; + } else { + unsafe { gl.blend_equation(blend.color.equation) }; + unsafe { gl.blend_func(blend.color.src, blend.color.dst) }; + } + } else { + unsafe { gl.disable(glow::BLEND) }; + } + } + } + C::BindBuffer { + target, + slot, + buffer, + offset, + size, + } => { + unsafe { gl.bind_buffer_range(target, slot, Some(buffer), offset, size) }; + } + C::BindSampler(texture_index, sampler) => { + unsafe { gl.bind_sampler(texture_index, sampler) }; + } + C::BindTexture { + slot, + texture, + target, + aspects, + ref mip_levels, + } => { + unsafe { gl.active_texture(glow::TEXTURE0 + slot) }; + unsafe { gl.bind_texture(target, Some(texture)) }; + + unsafe { + gl.tex_parameter_i32(target, glow::TEXTURE_BASE_LEVEL, mip_levels.start as i32) + }; + unsafe { + gl.tex_parameter_i32( + target, + glow::TEXTURE_MAX_LEVEL, + (mip_levels.end - 1) as i32, + ) + }; + + let version = gl.version(); + let is_min_es_3_1 = version.is_embedded && (version.major, version.minor) >= (3, 1); + let is_min_4_3 = !version.is_embedded && (version.major, version.minor) >= (4, 3); + if is_min_es_3_1 || is_min_4_3 { + let mode = match aspects { + crate::FormatAspects::DEPTH => Some(glow::DEPTH_COMPONENT), + crate::FormatAspects::STENCIL => Some(glow::STENCIL_INDEX), + _ => None, + }; + if let Some(mode) = mode { + unsafe { + gl.tex_parameter_i32( + target, + glow::DEPTH_STENCIL_TEXTURE_MODE, + mode as _, + ) + }; + } + } + } + C::BindImage { slot, ref binding } => { + unsafe { + gl.bind_image_texture( + slot, + Some(binding.raw), + binding.mip_level as i32, + binding.array_layer.is_none(), + binding.array_layer.unwrap_or_default() as i32, + binding.access, + binding.format, + ) + }; + } + C::InsertDebugMarker(ref range) => { + let marker = extract_marker(data_bytes, range); + unsafe { + if self + .shared + .private_caps + .contains(PrivateCapabilities::DEBUG_FNS) + { + gl.debug_message_insert( + glow::DEBUG_SOURCE_APPLICATION, + glow::DEBUG_TYPE_MARKER, + DEBUG_ID, + glow::DEBUG_SEVERITY_NOTIFICATION, + to_debug_str(marker), + ) + } + }; + } + C::PushDebugGroup(ref range) => { + let marker = extract_marker(data_bytes, range); + unsafe { + if self + .shared + .private_caps + .contains(PrivateCapabilities::DEBUG_FNS) + { + gl.push_debug_group( + glow::DEBUG_SOURCE_APPLICATION, + DEBUG_ID, + to_debug_str(marker), + ) + } + }; + } + C::PopDebugGroup => { + unsafe { + if self + .shared + .private_caps + .contains(PrivateCapabilities::DEBUG_FNS) + { + gl.pop_debug_group() + } + }; + } + C::SetImmediates { + ref uniform, + offset, + } => { + fn get_data<T, const COUNT: usize>(data: &[u8], offset: u32) -> [T; COUNT] + where + [T; COUNT]: bytemuck::AnyBitPattern, + { + let data_required = size_of::<T>() * COUNT; + let raw = &data[(offset as usize)..][..data_required]; + bytemuck::pod_read_unaligned(raw) + } + + let location = Some(&uniform.location); + use nt::glsl::{GlslScalar, GlslUniformType, GlslVectorSize}; + match uniform.ty { + // + // --- Float 1-4 Component --- + // + GlslUniformType::Scalar(GlslScalar::F32) => { + let data = get_data::<f32, 1>(data_bytes, offset)[0]; + unsafe { gl.uniform_1_f32(location, data) }; + } + GlslUniformType::Vector { + size: GlslVectorSize::Bi, + scalar: GlslScalar::F32, + } => { + let data = &get_data::<f32, 2>(data_bytes, offset); + unsafe { gl.uniform_2_f32_slice(location, data) }; + } + GlslUniformType::Vector { + size: GlslVectorSize::Tri, + scalar: GlslScalar::F32, + } => { + let data = &get_data::<f32, 3>(data_bytes, offset); + unsafe { gl.uniform_3_f32_slice(location, data) }; + } + GlslUniformType::Vector { + size: GlslVectorSize::Quad, + scalar: GlslScalar::F32, + } => { + let data = &get_data::<f32, 4>(data_bytes, offset); + unsafe { gl.uniform_4_f32_slice(location, data) }; + } + + // + // --- Int 1-4 Component --- + // + GlslUniformType::Scalar(GlslScalar::I32) => { + let data = get_data::<i32, 1>(data_bytes, offset)[0]; + unsafe { gl.uniform_1_i32(location, data) }; + } + GlslUniformType::Vector { + size: GlslVectorSize::Bi, + scalar: GlslScalar::I32, + } => { + let data = &get_data::<i32, 2>(data_bytes, offset); + unsafe { gl.uniform_2_i32_slice(location, data) }; + } + GlslUniformType::Vector { + size: GlslVectorSize::Tri, + scalar: GlslScalar::I32, + } => { + let data = &get_data::<i32, 3>(data_bytes, offset); + unsafe { gl.uniform_3_i32_slice(location, data) }; + } + GlslUniformType::Vector { + size: GlslVectorSize::Quad, + scalar: GlslScalar::I32, + } => { + let data = &get_data::<i32, 4>(data_bytes, offset); + unsafe { gl.uniform_4_i32_slice(location, data) }; + } + + // + // --- Uint 1-4 Component --- + // + GlslUniformType::Scalar(GlslScalar::U32) => { + let data = get_data::<u32, 1>(data_bytes, offset)[0]; + unsafe { gl.uniform_1_u32(location, data) }; + } + GlslUniformType::Vector { + size: GlslVectorSize::Bi, + scalar: GlslScalar::U32, + } => { + let data = &get_data::<u32, 2>(data_bytes, offset); + unsafe { gl.uniform_2_u32_slice(location, data) }; + } + GlslUniformType::Vector { + size: GlslVectorSize::Tri, + scalar: GlslScalar::U32, + } => { + let data = &get_data::<u32, 3>(data_bytes, offset); + unsafe { gl.uniform_3_u32_slice(location, data) }; + } + GlslUniformType::Vector { + size: GlslVectorSize::Quad, + scalar: GlslScalar::U32, + } => { + let data = &get_data::<u32, 4>(data_bytes, offset); + unsafe { gl.uniform_4_u32_slice(location, data) }; + } + + // + // --- Matrix 2xR --- + // + GlslUniformType::Matrix { + columns: GlslVectorSize::Bi, + rows: GlslVectorSize::Bi, + scalar: GlslScalar::F32, + } => { + let data = &get_data::<f32, 4>(data_bytes, offset); + unsafe { gl.uniform_matrix_2_f32_slice(location, false, data) }; + } + GlslUniformType::Matrix { + columns: GlslVectorSize::Bi, + rows: GlslVectorSize::Tri, + scalar: GlslScalar::F32, + } => { + // repack 2 vec3s into 6 values. + let unpacked_data = &get_data::<f32, 8>(data_bytes, offset); + #[rustfmt::skip] + let packed_data = [ + unpacked_data[0], unpacked_data[1], unpacked_data[2], + unpacked_data[4], unpacked_data[5], unpacked_data[6], + ]; + unsafe { gl.uniform_matrix_2x3_f32_slice(location, false, &packed_data) }; + } + GlslUniformType::Matrix { + columns: GlslVectorSize::Bi, + rows: GlslVectorSize::Quad, + scalar: GlslScalar::F32, + } => { + let data = &get_data::<f32, 8>(data_bytes, offset); + unsafe { gl.uniform_matrix_2x4_f32_slice(location, false, data) }; + } + + // + // --- Matrix 3xR --- + // + GlslUniformType::Matrix { + columns: GlslVectorSize::Tri, + rows: GlslVectorSize::Bi, + scalar: GlslScalar::F32, + } => { + let data = &get_data::<f32, 6>(data_bytes, offset); + unsafe { gl.uniform_matrix_3x2_f32_slice(location, false, data) }; + } + GlslUniformType::Matrix { + columns: GlslVectorSize::Tri, + rows: GlslVectorSize::Tri, + scalar: GlslScalar::F32, + } => { + // repack 3 vec3s into 9 values. + let unpacked_data = &get_data::<f32, 12>(data_bytes, offset); + #[rustfmt::skip] + let packed_data = [ + unpacked_data[0], unpacked_data[1], unpacked_data[2], + unpacked_data[4], unpacked_data[5], unpacked_data[6], + unpacked_data[8], unpacked_data[9], unpacked_data[10], + ]; + unsafe { gl.uniform_matrix_3_f32_slice(location, false, &packed_data) }; + } + GlslUniformType::Matrix { + columns: GlslVectorSize::Tri, + rows: GlslVectorSize::Quad, + scalar: GlslScalar::F32, + } => { + let data = &get_data::<f32, 12>(data_bytes, offset); + unsafe { gl.uniform_matrix_3x4_f32_slice(location, false, data) }; + } + + // + // --- Matrix 4xR --- + // + GlslUniformType::Matrix { + columns: GlslVectorSize::Quad, + rows: GlslVectorSize::Bi, + scalar: GlslScalar::F32, + } => { + let data = &get_data::<f32, 8>(data_bytes, offset); + unsafe { gl.uniform_matrix_4x2_f32_slice(location, false, data) }; + } + GlslUniformType::Matrix { + columns: GlslVectorSize::Quad, + rows: GlslVectorSize::Tri, + scalar: GlslScalar::F32, + } => { + // repack 4 vec3s into 12 values. + let unpacked_data = &get_data::<f32, 16>(data_bytes, offset); + #[rustfmt::skip] + let packed_data = [ + unpacked_data[0], unpacked_data[1], unpacked_data[2], + unpacked_data[4], unpacked_data[5], unpacked_data[6], + unpacked_data[8], unpacked_data[9], unpacked_data[10], + unpacked_data[12], unpacked_data[13], unpacked_data[14], + ]; + unsafe { gl.uniform_matrix_4x3_f32_slice(location, false, &packed_data) }; + } + GlslUniformType::Matrix { + columns: GlslVectorSize::Quad, + rows: GlslVectorSize::Quad, + scalar: GlslScalar::F32, + } => { + let data = &get_data::<f32, 16>(data_bytes, offset); + unsafe { gl.uniform_matrix_4_f32_slice(location, false, data) }; + } + _ => panic!("Unsupported uniform datatype: {:?}!", uniform.ty), + } + } + C::SetClipDistances { + old_count, + new_count, + } => { + // Disable clip planes that are no longer active + for i in new_count..old_count { + unsafe { gl.disable(glow::CLIP_DISTANCE0 + i) }; + } + + // Enable clip planes that are now active + for i in old_count..new_count { + unsafe { gl.enable(glow::CLIP_DISTANCE0 + i) }; + } + } + } + } +} + +impl crate::Queue for super::Queue { + type A = super::Api; + + unsafe fn submit( + &self, + command_buffers: &[&super::CommandBuffer], + _surface_textures: &[&super::Texture], + (signal_fence, signal_value): (&super::Fence, crate::FenceValue), + ) -> Result<(), crate::DeviceError> { + let shared = Arc::clone(&self.shared); + let gl = &shared.context.lock(); + for cmd_buf in command_buffers.iter() { + // The command encoder assumes a default state when encoding the command buffer. + // Always reset the state between command_buffers to reflect this assumption. Do + // this at the beginning of the loop in case something outside of wgpu modified + // this state prior to commit. + unsafe { self.reset_state(gl) }; + if let Some(ref label) = cmd_buf.label { + if self + .shared + .private_caps + .contains(PrivateCapabilities::DEBUG_FNS) + { + unsafe { + gl.push_debug_group( + glow::DEBUG_SOURCE_APPLICATION, + DEBUG_ID, + to_debug_str(label), + ) + }; + } + } + + for command in cmd_buf.commands.iter() { + unsafe { self.process(gl, command, &cmd_buf.data_bytes, &cmd_buf.queries) }; + } + + if cmd_buf.label.is_some() + && self + .shared + .private_caps + .contains(PrivateCapabilities::DEBUG_FNS) + { + unsafe { gl.pop_debug_group() }; + } + } + + signal_fence.maintain(gl); + signal_fence.signal(gl, signal_value)?; + + // This is extremely important. If we don't flush, the above fences may never + // be signaled, particularly in headless contexts. Headed contexts will + // often flush every so often, but headless contexts may not. + unsafe { gl.flush() }; + + Ok(()) + } + + unsafe fn present( + &self, + surface: &super::Surface, + texture: super::Texture, + ) -> Result<(), crate::SurfaceError> { + unsafe { surface.present(texture, &self.shared.context) } + } + + unsafe fn get_timestamp_period(&self) -> f32 { + 1.0 + } + + unsafe fn wait_for_idle(&self) -> Result<(), crate::DeviceError> { + let gl = &self.shared.context.lock(); + unsafe { gl.finish() }; + Ok(()) + } +} + +#[cfg(send_sync)] +unsafe impl Sync for super::Queue {} +#[cfg(send_sync)] +unsafe impl Send for super::Queue {} diff --git a/third_party/wgpu-hal/src/gles/shaders/clear.frag b/third_party/wgpu-hal/src/gles/shaders/clear.frag new file mode 100644 index 000000000..1d0e414b2 --- /dev/null +++ b/third_party/wgpu-hal/src/gles/shaders/clear.frag @@ -0,0 +1,7 @@ +uniform vec4 color; +//Hack: Some WebGL implementations don't find "color" otherwise. +uniform vec4 color_workaround; +out vec4 frag; +void main() { + frag = color + color_workaround; +} diff --git a/third_party/wgpu-hal/src/gles/shaders/clear.vert b/third_party/wgpu-hal/src/gles/shaders/clear.vert new file mode 100644 index 000000000..341b4e5f0 --- /dev/null +++ b/third_party/wgpu-hal/src/gles/shaders/clear.vert @@ -0,0 +1,9 @@ +// A triangle that fills the whole screen +vec2[3] TRIANGLE_POS = vec2[]( + vec2( 0.0, -3.0), + vec2(-3.0, 1.0), + vec2( 3.0, 1.0) +); +void main() { + gl_Position = vec4(TRIANGLE_POS[gl_VertexID], 0.0, 1.0); +} \ No newline at end of file diff --git a/third_party/wgpu-hal/src/gles/shaders/srgb_present.frag b/third_party/wgpu-hal/src/gles/shaders/srgb_present.frag new file mode 100644 index 000000000..853f82a6a --- /dev/null +++ b/third_party/wgpu-hal/src/gles/shaders/srgb_present.frag @@ -0,0 +1,16 @@ +#version 300 es +precision mediump float; +in vec2 uv; +uniform sampler2D present_texture; +out vec4 frag; +vec4 linear_to_srgb(vec4 linear) { + vec3 color_linear = linear.rgb; + vec3 selector = ceil(color_linear - 0.0031308); // 0 if under value, 1 if over + vec3 under = 12.92 * color_linear; + vec3 over = 1.055 * pow(color_linear, vec3(0.41666)) - 0.055; + vec3 result = mix(under, over, selector); + return vec4(result, linear.a); +} +void main() { + frag = linear_to_srgb(texture(present_texture, uv)); +} \ No newline at end of file diff --git a/third_party/wgpu-hal/src/gles/shaders/srgb_present.vert b/third_party/wgpu-hal/src/gles/shaders/srgb_present.vert new file mode 100644 index 000000000..922f2a184 --- /dev/null +++ b/third_party/wgpu-hal/src/gles/shaders/srgb_present.vert @@ -0,0 +1,18 @@ +#version 300 es +precision mediump float; +// A triangle that fills the whole screen +const vec2[3] TRIANGLE_POS = vec2[]( + vec2( 0.0, -3.0), + vec2(-3.0, 1.0), + vec2( 3.0, 1.0) +); +const vec2[3] TRIANGLE_UV = vec2[]( + vec2( 0.5, 1.), + vec2( -1.0, -1.0), + vec2( 2.0, -1.0) +); +out vec2 uv; +void main() { + uv = TRIANGLE_UV[gl_VertexID]; + gl_Position = vec4(TRIANGLE_POS[gl_VertexID], 0.0, 1.0); +} \ No newline at end of file diff --git a/third_party/wgpu-hal/src/gles/web.rs b/third_party/wgpu-hal/src/gles/web.rs new file mode 100644 index 000000000..326b4f35e --- /dev/null +++ b/third_party/wgpu-hal/src/gles/web.rs @@ -0,0 +1,490 @@ +use alloc::{format, string::String, vec::Vec}; + +use glow::HasContext; +use parking_lot::{Mutex, RwLock}; +use wasm_bindgen::{JsCast, JsValue}; + +use super::TextureFormatDesc; + +/// A wrapper around a [`glow::Context`] to provide a fake `lock()` api that makes it compatible +/// with the `AdapterContext` API from the EGL implementation. +#[derive(Debug)] +pub struct AdapterContext { + pub glow_context: glow::Context, + pub webgl2_context: web_sys::WebGl2RenderingContext, +} + +impl AdapterContext { + pub fn is_owned(&self) -> bool { + false + } + + /// Obtain a lock to the EGL context and get handle to the [`glow::Context`] that can be used to + /// do rendering. + #[track_caller] + pub fn lock(&self) -> &glow::Context { + &self.glow_context + } +} + +#[derive(Debug)] +pub struct Instance { + options: wgt::GlBackendOptions, +} + +impl Instance { + pub fn create_surface_from_canvas( + &self, + canvas: web_sys::HtmlCanvasElement, + ) -> Result<Surface, crate::InstanceError> { + let result = + canvas.get_context_with_context_options("webgl2", &Self::create_context_options()); + self.create_surface_from_context(Canvas::Canvas(canvas), result) + } + + pub fn create_surface_from_offscreen_canvas( + &self, + canvas: web_sys::OffscreenCanvas, + ) -> Result<Surface, crate::InstanceError> { + let result = + canvas.get_context_with_context_options("webgl2", &Self::create_context_options()); + self.create_surface_from_context(Canvas::Offscreen(canvas), result) + } + + /// Common portion of public `create_surface_from_*` functions. + /// + /// Note: Analogous code also exists in the WebGPU backend at + /// `wgpu::backend::web::Context`. + fn create_surface_from_context( + &self, + canvas: Canvas, + context_result: Result<Option<js_sys::Object>, JsValue>, + ) -> Result<Surface, crate::InstanceError> { + let context_object: js_sys::Object = match context_result { + Ok(Some(context)) => context, + Ok(None) => { + // <https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-getcontext-dev> + // A getContext() call “returns null if contextId is not supported, or if the + // canvas has already been initialized with another context type”. Additionally, + // “not supported” could include “insufficient GPU resources” or “the GPU process + // previously crashed”. So, we must return it as an `Err` since it could occur + // for circumstances outside the application author's control. + return Err(crate::InstanceError::new(String::from(concat!( + "canvas.getContext() returned null; ", + "webgl2 not available or canvas already in use" + )))); + } + Err(js_error) => { + // <https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-getcontext> + // A thrown exception indicates misuse of the canvas state. + return Err(crate::InstanceError::new(format!( + "canvas.getContext() threw exception {js_error:?}", + ))); + } + }; + + // Not returning this error because it is a type error that shouldn't happen unless + // the browser, JS builtin objects, or wasm bindings are misbehaving somehow. + let webgl2_context: web_sys::WebGl2RenderingContext = context_object + .dyn_into() + .expect("canvas context is not a WebGl2RenderingContext"); + + Ok(Surface { + canvas, + webgl2_context, + srgb_present_program: Mutex::new(None), + swapchain: RwLock::new(None), + texture: Mutex::new(None), + presentable: true, + }) + } + + fn create_context_options() -> js_sys::Object { + let context_options = js_sys::Object::new(); + js_sys::Reflect::set(&context_options, &"antialias".into(), &JsValue::FALSE) + .expect("Cannot create context options"); + context_options + } +} + +#[cfg(send_sync)] +static_assertions::assert_impl_all!(Instance: Send, Sync); + +impl crate::Instance for Instance { + type A = super::Api; + + unsafe fn init(desc: &crate::InstanceDescriptor<'_>) -> Result<Self, crate::InstanceError> { + profiling::scope!("Init OpenGL (WebGL) Backend"); + Ok(Instance { + options: desc.backend_options.gl.clone(), + }) + } + + unsafe fn enumerate_adapters( + &self, + surface_hint: Option<&Surface>, + ) -> Vec<crate::ExposedAdapter<super::Api>> { + if let Some(surface_hint) = surface_hint { + let gl = glow::Context::from_webgl2_context(surface_hint.webgl2_context.clone()); + + unsafe { + super::Adapter::expose( + AdapterContext { + glow_context: gl, + webgl2_context: surface_hint.webgl2_context.clone(), + }, + self.options.clone(), + ) + } + .into_iter() + .collect() + } else { + Vec::new() + } + } + + unsafe fn create_surface( + &self, + _display_handle: raw_window_handle::RawDisplayHandle, + window_handle: raw_window_handle::RawWindowHandle, + ) -> Result<Surface, crate::InstanceError> { + let canvas: web_sys::HtmlCanvasElement = match window_handle { + raw_window_handle::RawWindowHandle::Web(handle) => web_sys::window() + .and_then(|win| win.document()) + .expect("Cannot get document") + .query_selector(&format!("canvas[data-raw-handle=\"{}\"]", handle.id)) + .expect("Cannot query for canvas") + .expect("Canvas is not found") + .dyn_into() + .expect("Failed to downcast to canvas type"), + raw_window_handle::RawWindowHandle::WebCanvas(handle) => { + let value: &JsValue = unsafe { handle.obj.cast().as_ref() }; + value.clone().unchecked_into() + } + raw_window_handle::RawWindowHandle::WebOffscreenCanvas(handle) => { + let value: &JsValue = unsafe { handle.obj.cast().as_ref() }; + let canvas: web_sys::OffscreenCanvas = value.clone().unchecked_into(); + + return self.create_surface_from_offscreen_canvas(canvas); + } + _ => { + return Err(crate::InstanceError::new(format!( + "window handle {window_handle:?} is not a web handle" + ))) + } + }; + + self.create_surface_from_canvas(canvas) + } +} + +impl super::Adapter { + /// Creates a new external adapter from an existing WebGL2 rendering context. + /// + /// # Safety + /// + /// - The underlying WebGL2 context must be valid (not lost). + /// - The underlying WebGL2 context must be valid when interfacing with any objects returned by + /// wgpu-hal from this adapter. + /// - The underlying WebGL2 context must be valid when dropping this adapter and when + /// dropping any objects returned from this adapter. + pub unsafe fn new_external( + webgl2_context: web_sys::WebGl2RenderingContext, + options: wgt::GlBackendOptions, + ) -> Option<crate::ExposedAdapter<super::Api>> { + let glow_context = glow::Context::from_webgl2_context(webgl2_context.clone()); + unsafe { + Self::expose( + AdapterContext { + glow_context, + webgl2_context, + }, + options, + ) + } + } + + pub fn adapter_context(&self) -> &AdapterContext { + &self.shared.context + } +} + +impl super::Device { + /// Returns the underlying WebGL2 `AdapterContext`. + pub fn context(&self) -> &AdapterContext { + &self.shared.context + } +} + +#[derive(Debug)] +pub struct Surface { + canvas: Canvas, + pub(super) webgl2_context: web_sys::WebGl2RenderingContext, + pub(super) swapchain: RwLock<Option<Swapchain>>, + texture: Mutex<Option<glow::Texture>>, + pub(super) presentable: bool, + srgb_present_program: Mutex<Option<glow::Program>>, +} + +impl Clone for Surface { + fn clone(&self) -> Self { + Self { + canvas: self.canvas.clone(), + webgl2_context: self.webgl2_context.clone(), + swapchain: RwLock::new(self.swapchain.read().clone()), + texture: Mutex::new(*self.texture.lock()), + presentable: self.presentable, + srgb_present_program: Mutex::new(*self.srgb_present_program.lock()), + } + } +} + +#[cfg(send_sync)] +unsafe impl Sync for Surface {} +#[cfg(send_sync)] +unsafe impl Send for Surface {} + +#[derive(Clone, Debug)] +enum Canvas { + Canvas(web_sys::HtmlCanvasElement), + Offscreen(web_sys::OffscreenCanvas), +} + +#[derive(Clone, Debug)] +pub struct Swapchain { + pub(crate) extent: wgt::Extent3d, + // pub(crate) channel: f::ChannelType, + pub(super) format: wgt::TextureFormat, + pub(super) framebuffer: glow::Framebuffer, + pub(super) format_desc: TextureFormatDesc, +} + +impl Surface { + pub(super) unsafe fn present( + &self, + _suf_texture: super::Texture, + context: &AdapterContext, + ) -> Result<(), crate::SurfaceError> { + let gl = &context.glow_context; + let swapchain = self.swapchain.read(); + let swapchain = swapchain.as_ref().ok_or(crate::SurfaceError::Other( + "need to configure surface before presenting", + ))?; + + if swapchain.format.is_srgb() { + // Important to set the viewport since we don't know in what state the user left it. + unsafe { + gl.viewport( + 0, + 0, + swapchain.extent.width as _, + swapchain.extent.height as _, + ) + }; + unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, None) }; + unsafe { gl.bind_sampler(0, None) }; + unsafe { gl.active_texture(glow::TEXTURE0) }; + unsafe { gl.bind_texture(glow::TEXTURE_2D, *self.texture.lock()) }; + unsafe { gl.use_program(*self.srgb_present_program.lock()) }; + unsafe { gl.disable(glow::DEPTH_TEST) }; + unsafe { gl.disable(glow::STENCIL_TEST) }; + unsafe { gl.disable(glow::SCISSOR_TEST) }; + unsafe { gl.disable(glow::BLEND) }; + unsafe { gl.disable(glow::CULL_FACE) }; + unsafe { gl.draw_buffers(&[glow::BACK]) }; + unsafe { gl.draw_arrays(glow::TRIANGLES, 0, 3) }; + } else { + unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(swapchain.framebuffer)) }; + unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, None) }; + // Note the Y-flipping here. GL's presentation is not flipped, + // but main rendering is. Therefore, we Y-flip the output positions + // in the shader, and also this blit. + unsafe { + gl.blit_framebuffer( + 0, + swapchain.extent.height as i32, + swapchain.extent.width as i32, + 0, + 0, + 0, + swapchain.extent.width as i32, + swapchain.extent.height as i32, + glow::COLOR_BUFFER_BIT, + glow::NEAREST, + ) + }; + } + + Ok(()) + } + + unsafe fn create_srgb_present_program(gl: &glow::Context) -> glow::Program { + let program = unsafe { gl.create_program() }.expect("Could not create shader program"); + let vertex = + unsafe { gl.create_shader(glow::VERTEX_SHADER) }.expect("Could not create shader"); + unsafe { gl.shader_source(vertex, include_str!("./shaders/srgb_present.vert")) }; + unsafe { gl.compile_shader(vertex) }; + let fragment = + unsafe { gl.create_shader(glow::FRAGMENT_SHADER) }.expect("Could not create shader"); + unsafe { gl.shader_source(fragment, include_str!("./shaders/srgb_present.frag")) }; + unsafe { gl.compile_shader(fragment) }; + unsafe { gl.attach_shader(program, vertex) }; + unsafe { gl.attach_shader(program, fragment) }; + unsafe { gl.link_program(program) }; + unsafe { gl.delete_shader(vertex) }; + unsafe { gl.delete_shader(fragment) }; + unsafe { gl.bind_texture(glow::TEXTURE_2D, None) }; + + program + } + + pub fn supports_srgb(&self) -> bool { + // present.frag takes care of handling srgb conversion + true + } +} + +impl crate::Surface for Surface { + type A = super::Api; + + unsafe fn configure( + &self, + device: &super::Device, + config: &crate::SurfaceConfiguration, + ) -> Result<(), crate::SurfaceError> { + match self.canvas { + Canvas::Canvas(ref canvas) => { + canvas.set_width(config.extent.width); + canvas.set_height(config.extent.height); + } + Canvas::Offscreen(ref canvas) => { + canvas.set_width(config.extent.width); + canvas.set_height(config.extent.height); + } + } + + let gl = &device.shared.context.lock(); + + { + let mut swapchain = self.swapchain.write(); + if let Some(swapchain) = swapchain.take() { + // delete all frame buffers already allocated + unsafe { gl.delete_framebuffer(swapchain.framebuffer) }; + } + } + { + let mut srgb_present_program = self.srgb_present_program.lock(); + if srgb_present_program.is_none() && config.format.is_srgb() { + *srgb_present_program = Some(unsafe { Self::create_srgb_present_program(gl) }); + } + } + { + let mut texture = self.texture.lock(); + if let Some(texture) = texture.take() { + unsafe { gl.delete_texture(texture) }; + } + + *texture = Some(unsafe { gl.create_texture() }.map_err(|error| { + log::error!("Internal swapchain texture creation failed: {error}"); + crate::DeviceError::OutOfMemory + })?); + + let desc = device.shared.describe_texture_format(config.format); + unsafe { gl.bind_texture(glow::TEXTURE_2D, *texture) }; + unsafe { + gl.tex_parameter_i32( + glow::TEXTURE_2D, + glow::TEXTURE_MIN_FILTER, + glow::NEAREST as _, + ) + }; + unsafe { + gl.tex_parameter_i32( + glow::TEXTURE_2D, + glow::TEXTURE_MAG_FILTER, + glow::NEAREST as _, + ) + }; + unsafe { + gl.tex_storage_2d( + glow::TEXTURE_2D, + 1, + desc.internal, + config.extent.width as i32, + config.extent.height as i32, + ) + }; + + let framebuffer = unsafe { gl.create_framebuffer() }.map_err(|error| { + log::error!("Internal swapchain framebuffer creation failed: {error}"); + crate::DeviceError::OutOfMemory + })?; + unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(framebuffer)) }; + unsafe { + gl.framebuffer_texture_2d( + glow::READ_FRAMEBUFFER, + glow::COLOR_ATTACHMENT0, + glow::TEXTURE_2D, + *texture, + 0, + ) + }; + unsafe { gl.bind_texture(glow::TEXTURE_2D, None) }; + + let mut swapchain = self.swapchain.write(); + *swapchain = Some(Swapchain { + extent: config.extent, + // channel: config.format.base_format().1, + format: config.format, + format_desc: desc, + framebuffer, + }); + } + + Ok(()) + } + + unsafe fn unconfigure(&self, device: &super::Device) { + let gl = device.shared.context.lock(); + { + let mut swapchain = self.swapchain.write(); + if let Some(swapchain) = swapchain.take() { + unsafe { gl.delete_framebuffer(swapchain.framebuffer) }; + } + } + if let Some(renderbuffer) = self.texture.lock().take() { + unsafe { gl.delete_texture(renderbuffer) }; + } + } + + unsafe fn acquire_texture( + &self, + _timeout_ms: Option<core::time::Duration>, //TODO + _fence: &super::Fence, + ) -> Result<crate::AcquiredSurfaceTexture<super::Api>, crate::SurfaceError> { + let swapchain = self.swapchain.read(); + let sc = swapchain.as_ref().unwrap(); + let texture = super::Texture { + inner: super::TextureInner::Texture { + raw: self.texture.lock().unwrap(), + target: glow::TEXTURE_2D, + }, + drop_guard: None, + array_layer_count: 1, + mip_level_count: 1, + format: sc.format, + format_desc: sc.format_desc.clone(), + copy_size: crate::CopyExtent { + width: sc.extent.width, + height: sc.extent.height, + depth: 1, + }, + }; + Ok(crate::AcquiredSurfaceTexture { + texture, + suboptimal: false, + }) + } + + unsafe fn discard_texture(&self, _texture: super::Texture) {} +} diff --git a/third_party/wgpu-hal/src/gles/wgl.rs b/third_party/wgpu-hal/src/gles/wgl.rs new file mode 100644 index 000000000..09ee3e959 --- /dev/null +++ b/third_party/wgpu-hal/src/gles/wgl.rs @@ -0,0 +1,905 @@ +#![expect( + missing_debug_implementations, + reason = "TODO: someone developing on Windows add Debug impls where possible" +)] + +use alloc::{borrow::ToOwned as _, ffi::CString, string::String, sync::Arc, vec::Vec}; +use core::{ + ffi::{c_int, c_void, CStr}, + mem::{self, ManuallyDrop}, + ptr, + time::Duration, +}; +use std::{ + sync::{ + mpsc::{sync_channel, SyncSender}, + LazyLock, + }, + thread, +}; + +use glow::HasContext; +use glutin_wgl_sys::wgl_extra::{ + Wgl, CONTEXT_CORE_PROFILE_BIT_ARB, CONTEXT_DEBUG_BIT_ARB, CONTEXT_FLAGS_ARB, + CONTEXT_PROFILE_MASK_ARB, +}; +use hashbrown::HashSet; +use parking_lot::{Mutex, MutexGuard, RwLock}; +use raw_window_handle::{RawDisplayHandle, RawWindowHandle}; +use wgt::InstanceFlags; +use windows::{ + core::{Error, PCSTR}, + Win32::{ + Foundation, + Graphics::{Gdi, OpenGL}, + System::LibraryLoader, + UI::WindowsAndMessaging, + }, +}; + +/// The amount of time to wait while trying to obtain a lock to the adapter context +const CONTEXT_LOCK_TIMEOUT_SECS: u64 = 1; + +/// A wrapper around a `[`glow::Context`]` and the required WGL context that uses locking to +/// guarantee exclusive access when shared with multiple threads. +pub struct AdapterContext { + inner: Arc<Mutex<Inner>>, +} + +#[cfg(send_sync)] +static_assertions::assert_impl_all!(AdapterContext: Send, Sync); + +impl AdapterContext { + pub fn is_owned(&self) -> bool { + true + } + + pub fn raw_context(&self) -> *mut c_void { + match self.inner.lock().context { + Some(ref wgl) => wgl.context.0, + None => ptr::null_mut(), + } + } + + /// Obtain a lock to the WGL context and get handle to the [`glow::Context`] that can be used to + /// do rendering. + #[track_caller] + pub fn lock(&self) -> AdapterContextLock<'_> { + let inner = self + .inner + // Don't lock forever. If it takes longer than 1 second to get the lock we've got a + // deadlock and should panic to show where we got stuck + .try_lock_for(Duration::from_secs(CONTEXT_LOCK_TIMEOUT_SECS)) + .expect("Could not lock adapter context. This is most-likely a deadlock."); + + if let Some(wgl) = &inner.context { + wgl.make_current(inner.device.dc).unwrap() + }; + + AdapterContextLock { inner } + } + + /// Obtain a lock to the WGL context and get handle to the [`glow::Context`] that can be used to + /// do rendering. + /// + /// Unlike [`lock`](Self::lock), this accepts a device to pass to `make_current` and exposes the error + /// when `make_current` fails. + #[track_caller] + fn lock_with_dc(&self, device: Gdi::HDC) -> windows::core::Result<AdapterContextLock<'_>> { + let inner = self + .inner + .try_lock_for(Duration::from_secs(CONTEXT_LOCK_TIMEOUT_SECS)) + .expect("Could not lock adapter context. This is most-likely a deadlock."); + + if let Some(wgl) = &inner.context { + wgl.make_current(device)?; + } + + Ok(AdapterContextLock { inner }) + } +} + +/// A guard containing a lock to an [`AdapterContext`], while the GL context is kept current. +pub struct AdapterContextLock<'a> { + inner: MutexGuard<'a, Inner>, +} + +impl<'a> core::ops::Deref for AdapterContextLock<'a> { + type Target = glow::Context; + + fn deref(&self) -> &Self::Target { + &self.inner.gl + } +} + +impl<'a> Drop for AdapterContextLock<'a> { + fn drop(&mut self) { + if let Some(wgl) = &self.inner.context { + wgl.unmake_current().unwrap() + } + } +} + +struct WglContext { + context: OpenGL::HGLRC, +} + +impl WglContext { + fn make_current(&self, device: Gdi::HDC) -> windows::core::Result<()> { + unsafe { OpenGL::wglMakeCurrent(device, self.context) } + } + + fn unmake_current(&self) -> windows::core::Result<()> { + if unsafe { OpenGL::wglGetCurrentContext() }.is_invalid() { + return Ok(()); + } + unsafe { OpenGL::wglMakeCurrent(Default::default(), Default::default()) } + } +} + +impl Drop for WglContext { + fn drop(&mut self) { + if let Err(e) = unsafe { OpenGL::wglDeleteContext(self.context) } { + log::error!("failed to delete WGL context: {e}"); + } + } +} + +unsafe impl Send for WglContext {} +unsafe impl Sync for WglContext {} + +struct Inner { + gl: ManuallyDrop<glow::Context>, + device: InstanceDevice, + context: Option<WglContext>, +} + +impl Drop for Inner { + fn drop(&mut self) { + struct CurrentGuard<'a>(&'a WglContext); + impl Drop for CurrentGuard<'_> { + fn drop(&mut self) { + self.0.unmake_current().unwrap(); + } + } + + // Context must be current when dropped. See safety docs on + // `glow::HasContext`. + // + // NOTE: This is only set to `None` by `Adapter::new_external` which + // requires the context to be current when anything that may be holding + // the `Arc<AdapterShared>` is dropped. + let _guard = self.context.as_ref().map(|wgl| { + wgl.make_current(self.device.dc).unwrap(); + CurrentGuard(wgl) + }); + // SAFETY: Field not used after this. + unsafe { ManuallyDrop::drop(&mut self.gl) }; + } +} + +unsafe impl Send for Inner {} +unsafe impl Sync for Inner {} + +pub struct Instance { + srgb_capable: bool, + options: wgt::GlBackendOptions, + inner: Arc<Mutex<Inner>>, +} + +#[cfg(send_sync)] +static_assertions::assert_impl_all!(Instance: Send, Sync); + +fn load_gl_func(name: &str, module: Option<Foundation::HMODULE>) -> *const c_void { + let addr = CString::new(name.as_bytes()).unwrap(); + let mut ptr = unsafe { OpenGL::wglGetProcAddress(PCSTR(addr.as_ptr().cast())) }; + if ptr.is_none() { + if let Some(module) = module { + ptr = unsafe { LibraryLoader::GetProcAddress(module, PCSTR(addr.as_ptr().cast())) }; + } + } + ptr.map_or_else(ptr::null_mut, |p| p as *mut c_void) +} + +fn get_extensions(extra: &Wgl, dc: Gdi::HDC) -> HashSet<String> { + if extra.GetExtensionsStringARB.is_loaded() { + unsafe { CStr::from_ptr(extra.GetExtensionsStringARB(dc.0)) } + .to_str() + .unwrap_or("") + } else { + "" + } + .split(' ') + .map(|s| s.to_owned()) + .collect() +} + +unsafe fn setup_pixel_format(dc: Gdi::HDC) -> Result<(), crate::InstanceError> { + { + let format = OpenGL::PIXELFORMATDESCRIPTOR { + nVersion: 1, + nSize: size_of::<OpenGL::PIXELFORMATDESCRIPTOR>() as u16, + dwFlags: OpenGL::PFD_DRAW_TO_WINDOW + | OpenGL::PFD_SUPPORT_OPENGL + | OpenGL::PFD_DOUBLEBUFFER, + iPixelType: OpenGL::PFD_TYPE_RGBA, + cColorBits: 8, + ..unsafe { mem::zeroed() } + }; + + let index = unsafe { OpenGL::ChoosePixelFormat(dc, &format) }; + if index == 0 { + return Err(crate::InstanceError::with_source( + String::from("unable to choose pixel format"), + Error::from_thread(), + )); + } + + let current = unsafe { OpenGL::GetPixelFormat(dc) }; + + if index != current { + unsafe { OpenGL::SetPixelFormat(dc, index, &format) }.map_err(|e| { + crate::InstanceError::with_source(String::from("unable to set pixel format"), e) + })?; + } + } + + { + let index = unsafe { OpenGL::GetPixelFormat(dc) }; + if index == 0 { + return Err(crate::InstanceError::with_source( + String::from("unable to get pixel format index"), + Error::from_thread(), + )); + } + let mut format = Default::default(); + if unsafe { + OpenGL::DescribePixelFormat(dc, index, size_of_val(&format) as u32, Some(&mut format)) + } == 0 + { + return Err(crate::InstanceError::with_source( + String::from("unable to read pixel format"), + Error::from_thread(), + )); + } + + if !format.dwFlags.contains(OpenGL::PFD_SUPPORT_OPENGL) + || format.iPixelType != OpenGL::PFD_TYPE_RGBA + { + return Err(crate::InstanceError::new(String::from( + "unsuitable pixel format", + ))); + } + } + Ok(()) +} + +fn create_global_window_class() -> Result<CString, crate::InstanceError> { + let instance = unsafe { LibraryLoader::GetModuleHandleA(None) }.map_err(|e| { + crate::InstanceError::with_source(String::from("unable to get executable instance"), e) + })?; + + // Use the address of `UNIQUE` as part of the window class name to ensure different + // `wgpu` versions use different names. + static UNIQUE: Mutex<u8> = Mutex::new(0); + let class_addr: *const _ = &UNIQUE; + let name = format!("wgpu Device Class {:x}\0", class_addr as usize); + let name = CString::from_vec_with_nul(name.into_bytes()).unwrap(); + + // The window class may already be registered if we are a dynamic library that got + // unloaded & loaded back into the same process. If so, just skip creation. + let already_exists = unsafe { + let mut wc = mem::zeroed::<WindowsAndMessaging::WNDCLASSEXA>(); + WindowsAndMessaging::GetClassInfoExA( + Some(instance.into()), + PCSTR(name.as_ptr().cast()), + &mut wc, + ) + .is_ok() + }; + if already_exists { + return Ok(name); + } + + // Use a wrapper function for compatibility with `windows-rs`. + unsafe extern "system" fn wnd_proc( + window: Foundation::HWND, + msg: u32, + wparam: Foundation::WPARAM, + lparam: Foundation::LPARAM, + ) -> Foundation::LRESULT { + unsafe { WindowsAndMessaging::DefWindowProcA(window, msg, wparam, lparam) } + } + + let window_class = WindowsAndMessaging::WNDCLASSEXA { + cbSize: size_of::<WindowsAndMessaging::WNDCLASSEXA>() as u32, + style: WindowsAndMessaging::CS_OWNDC, + lpfnWndProc: Some(wnd_proc), + cbClsExtra: 0, + cbWndExtra: 0, + hInstance: instance.into(), + hIcon: WindowsAndMessaging::HICON::default(), + hCursor: WindowsAndMessaging::HCURSOR::default(), + hbrBackground: Gdi::HBRUSH::default(), + lpszMenuName: PCSTR::null(), + lpszClassName: PCSTR(name.as_ptr().cast()), + hIconSm: WindowsAndMessaging::HICON::default(), + }; + + let atom = unsafe { WindowsAndMessaging::RegisterClassExA(&window_class) }; + + if atom == 0 { + return Err(crate::InstanceError::with_source( + String::from("unable to register window class"), + Error::from_thread(), + )); + } + + // We intentionally leak the window class as we only need one per process. + + Ok(name) +} + +fn get_global_window_class() -> Result<CString, crate::InstanceError> { + static GLOBAL: LazyLock<Result<CString, crate::InstanceError>> = + LazyLock::new(create_global_window_class); + GLOBAL.clone() +} + +struct InstanceDevice { + dc: Gdi::HDC, + + /// This is used to keep the thread owning `dc` alive until this struct is dropped. + _tx: SyncSender<()>, +} + +fn create_instance_device() -> Result<InstanceDevice, crate::InstanceError> { + #[derive(Clone, Copy)] + // TODO: We can get these SendSync definitions in the upstream metadata if this is the case + struct SendDc(Gdi::HDC); + unsafe impl Sync for SendDc {} + unsafe impl Send for SendDc {} + + struct Window { + window: Foundation::HWND, + } + impl Drop for Window { + fn drop(&mut self) { + if let Err(e) = unsafe { WindowsAndMessaging::DestroyWindow(self.window) } { + log::error!("failed to destroy window: {e}"); + } + } + } + + let window_class = get_global_window_class()?; + + let (drop_tx, drop_rx) = sync_channel(0); + let (setup_tx, setup_rx) = sync_channel(0); + + // We spawn a thread which owns the hidden window for this instance. + thread::Builder::new() + .stack_size(256 * 1024) + .name("wgpu-hal WGL Instance Thread".to_owned()) + .spawn(move || { + let setup = (|| { + let instance = unsafe { LibraryLoader::GetModuleHandleA(None) }.map_err(|e| { + crate::InstanceError::with_source( + String::from("unable to get executable instance"), + e, + ) + })?; + + // Create a hidden window since we don't pass `WS_VISIBLE`. + let window = unsafe { + WindowsAndMessaging::CreateWindowExA( + WindowsAndMessaging::WINDOW_EX_STYLE::default(), + PCSTR(window_class.as_ptr().cast()), + PCSTR(window_class.as_ptr().cast()), + WindowsAndMessaging::WINDOW_STYLE::default(), + 0, + 0, + 1, + 1, + None, + None, + Some(instance.into()), + None, + ) + } + .map_err(|e| { + crate::InstanceError::with_source( + String::from("unable to create hidden instance window"), + e, + ) + })?; + let window = Window { window }; + + let dc = unsafe { Gdi::GetDC(Some(window.window)) }; + if dc.is_invalid() { + return Err(crate::InstanceError::with_source( + String::from("unable to create memory device"), + Error::from_thread(), + )); + } + let dc = DeviceContextHandle { + device: dc, + window: window.window, + }; + unsafe { setup_pixel_format(dc.device)? }; + + Ok((window, dc)) + })(); + + match setup { + Ok((_window, dc)) => { + setup_tx.send(Ok(SendDc(dc.device))).unwrap(); + // Wait for the shutdown event to free the window and device context handle. + drop_rx.recv().ok(); + } + Err(err) => { + setup_tx.send(Err(err)).unwrap(); + } + } + }) + .map_err(|e| { + crate::InstanceError::with_source(String::from("unable to create instance thread"), e) + })?; + + let dc = setup_rx.recv().unwrap()?.0; + + Ok(InstanceDevice { dc, _tx: drop_tx }) +} + +impl crate::Instance for Instance { + type A = super::Api; + + unsafe fn init(desc: &crate::InstanceDescriptor<'_>) -> Result<Self, crate::InstanceError> { + profiling::scope!("Init OpenGL (WGL) Backend"); + let opengl_module = + unsafe { LibraryLoader::LoadLibraryA(PCSTR(c"opengl32.dll".as_ptr().cast())) } + .map_err(|e| { + crate::InstanceError::with_source( + String::from("unable to load the OpenGL library"), + e, + ) + })?; + + let device = create_instance_device()?; + let dc = device.dc; + + let context = unsafe { OpenGL::wglCreateContext(dc) }.map_err(|e| { + crate::InstanceError::with_source( + String::from("unable to create initial OpenGL context"), + e, + ) + })?; + let context = WglContext { context }; + context.make_current(dc).map_err(|e| { + crate::InstanceError::with_source( + String::from("unable to set initial OpenGL context as current"), + e, + ) + })?; + + let extra = Wgl::load_with(|name| load_gl_func(name, None)); + let extensions = get_extensions(&extra, dc); + + let can_use_profile = extensions.contains("WGL_ARB_create_context_profile") + && extra.CreateContextAttribsARB.is_loaded(); + + let context = if can_use_profile { + let attributes = [ + CONTEXT_PROFILE_MASK_ARB as c_int, + CONTEXT_CORE_PROFILE_BIT_ARB as c_int, + CONTEXT_FLAGS_ARB as c_int, + if desc.flags.contains(InstanceFlags::DEBUG) { + CONTEXT_DEBUG_BIT_ARB as c_int + } else { + 0 + }, + 0, // End of list + ]; + let context = + unsafe { extra.CreateContextAttribsARB(dc.0, ptr::null(), attributes.as_ptr()) }; + if context.is_null() { + return Err(crate::InstanceError::with_source( + String::from("unable to create OpenGL context"), + Error::from_thread(), + )); + } + WglContext { + context: OpenGL::HGLRC(context.cast_mut()), + } + } else { + context + }; + + context.make_current(dc).map_err(|e| { + crate::InstanceError::with_source( + String::from("unable to set OpenGL context as current"), + e, + ) + })?; + + let mut gl = unsafe { + glow::Context::from_loader_function(|name| load_gl_func(name, Some(opengl_module))) + }; + + let extra = Wgl::load_with(|name| load_gl_func(name, None)); + let extensions = get_extensions(&extra, dc); + + let srgb_capable = extensions.contains("WGL_EXT_framebuffer_sRGB") + || extensions.contains("WGL_ARB_framebuffer_sRGB") + || gl + .supported_extensions() + .contains("GL_ARB_framebuffer_sRGB"); + + // In contrast to OpenGL ES, OpenGL requires explicitly enabling sRGB conversions, + // as otherwise the user has to do the sRGB conversion. + if srgb_capable { + unsafe { gl.enable(glow::FRAMEBUFFER_SRGB) }; + } + + if desc.flags.contains(InstanceFlags::VALIDATION) && gl.supports_debug() { + log::debug!("Enabling GL debug output"); + unsafe { gl.enable(glow::DEBUG_OUTPUT) }; + unsafe { gl.debug_message_callback(super::gl_debug_message_callback) }; + } + + // Wrap in ManuallyDrop to make it easier to "current" the GL context before dropping this + // GLOW context, which could also happen if a panic occurs after we uncurrent the context + // below but before Inner is constructed. + let gl = ManuallyDrop::new(gl); + context.unmake_current().map_err(|e| { + crate::InstanceError::with_source( + String::from("unable to unset the current WGL context"), + e, + ) + })?; + + Ok(Instance { + inner: Arc::new(Mutex::new(Inner { + device, + gl, + context: Some(context), + })), + options: desc.backend_options.gl.clone(), + srgb_capable, + }) + } + + unsafe fn create_surface( + &self, + display_handle: RawDisplayHandle, + window_handle: RawWindowHandle, + ) -> Result<Surface, crate::InstanceError> { + assert!(matches!(display_handle, RawDisplayHandle::Windows(_))); + let window = if let RawWindowHandle::Win32(handle) = window_handle { + handle + } else { + return Err(crate::InstanceError::new(format!( + "unsupported window: {window_handle:?}" + ))); + }; + Ok(Surface { + // This cast exists because of https://github.com/rust-windowing/raw-window-handle/issues/171 + window: Foundation::HWND(window.hwnd.get() as *mut _), + presentable: true, + swapchain: RwLock::new(None), + srgb_capable: self.srgb_capable, + }) + } + + unsafe fn enumerate_adapters( + &self, + _surface_hint: Option<&Surface>, + ) -> Vec<crate::ExposedAdapter<super::Api>> { + unsafe { + super::Adapter::expose( + AdapterContext { + inner: self.inner.clone(), + }, + self.options.clone(), + ) + } + .into_iter() + .collect() + } +} + +impl super::Adapter { + /// Creates a new external adapter using the specified loader function. + /// + /// # Safety + /// + /// - The underlying OpenGL ES context must be current. + /// - The underlying OpenGL ES context must be current when interfacing with any objects returned by + /// wgpu-hal from this adapter. + /// - The underlying OpenGL ES context must be current when dropping this adapter and when + /// dropping any objects returned from this adapter. + pub unsafe fn new_external( + fun: impl FnMut(&str) -> *const c_void, + options: wgt::GlBackendOptions, + ) -> Option<crate::ExposedAdapter<super::Api>> { + let context = unsafe { glow::Context::from_loader_function(fun) }; + unsafe { + Self::expose( + AdapterContext { + inner: Arc::new(Mutex::new(Inner { + gl: ManuallyDrop::new(context), + device: create_instance_device().ok()?, + context: None, + })), + }, + options, + ) + } + } + + pub fn adapter_context(&self) -> &AdapterContext { + &self.shared.context + } +} + +impl super::Device { + /// Returns the underlying WGL context. + pub fn context(&self) -> &AdapterContext { + &self.shared.context + } +} + +struct DeviceContextHandle { + device: Gdi::HDC, + window: Foundation::HWND, +} + +impl Drop for DeviceContextHandle { + fn drop(&mut self) { + unsafe { + Gdi::ReleaseDC(Some(self.window), self.device); + }; + } +} + +pub struct Swapchain { + framebuffer: glow::Framebuffer, + renderbuffer: glow::Renderbuffer, + + /// Extent because the window lies + extent: wgt::Extent3d, + + format: wgt::TextureFormat, + format_desc: super::TextureFormatDesc, + #[allow(unused)] + sample_type: wgt::TextureSampleType, +} + +pub struct Surface { + window: Foundation::HWND, + pub(super) presentable: bool, + swapchain: RwLock<Option<Swapchain>>, + srgb_capable: bool, +} + +unsafe impl Send for Surface {} +unsafe impl Sync for Surface {} + +impl Surface { + pub(super) unsafe fn present( + &self, + _suf_texture: super::Texture, + context: &AdapterContext, + ) -> Result<(), crate::SurfaceError> { + let swapchain = self.swapchain.read(); + let sc = swapchain.as_ref().unwrap(); + let dc = unsafe { Gdi::GetDC(Some(self.window)) }; + if dc.is_invalid() { + log::error!( + "unable to get the device context from window: {}", + Error::from_thread() + ); + return Err(crate::SurfaceError::Other( + "unable to get the device context from window", + )); + } + let dc = DeviceContextHandle { + device: dc, + window: self.window, + }; + + let gl = context.lock_with_dc(dc.device).map_err(|e| { + log::error!("unable to make the OpenGL context current for surface: {e}",); + crate::SurfaceError::Other("unable to make the OpenGL context current for surface") + })?; + + unsafe { gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, None) }; + unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(sc.framebuffer)) }; + + if self.srgb_capable { + // Disable sRGB conversions for `glBlitFramebuffer` as behavior does diverge between + // drivers and formats otherwise and we want to ensure no sRGB conversions happen. + unsafe { gl.disable(glow::FRAMEBUFFER_SRGB) }; + } + + // Note the Y-flipping here. GL's presentation is not flipped, + // but main rendering is. Therefore, we Y-flip the output positions + // in the shader, and also this blit. + unsafe { + gl.blit_framebuffer( + 0, + sc.extent.height as i32, + sc.extent.width as i32, + 0, + 0, + 0, + sc.extent.width as i32, + sc.extent.height as i32, + glow::COLOR_BUFFER_BIT, + glow::NEAREST, + ) + }; + + if self.srgb_capable { + unsafe { gl.enable(glow::FRAMEBUFFER_SRGB) }; + } + + unsafe { gl.bind_renderbuffer(glow::RENDERBUFFER, None) }; + unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, None) }; + + if let Err(e) = unsafe { OpenGL::SwapBuffers(dc.device) } { + log::error!("unable to swap buffers: {e}"); + return Err(crate::SurfaceError::Other("unable to swap buffers")); + } + + Ok(()) + } + + pub fn supports_srgb(&self) -> bool { + self.srgb_capable + } +} + +impl crate::Surface for Surface { + type A = super::Api; + + unsafe fn configure( + &self, + device: &super::Device, + config: &crate::SurfaceConfiguration, + ) -> Result<(), crate::SurfaceError> { + // Remove the old configuration. + unsafe { self.unconfigure(device) }; + + let dc = unsafe { Gdi::GetDC(Some(self.window)) }; + if dc.is_invalid() { + log::error!( + "unable to get the device context from window: {}", + Error::from_thread() + ); + return Err(crate::SurfaceError::Other( + "unable to get the device context from window", + )); + } + let dc = DeviceContextHandle { + device: dc, + window: self.window, + }; + + if let Err(e) = unsafe { setup_pixel_format(dc.device) } { + log::error!("unable to setup surface pixel format: {e}",); + return Err(crate::SurfaceError::Other( + "unable to setup surface pixel format", + )); + } + + let format_desc = device.shared.describe_texture_format(config.format); + let gl = &device.shared.context.lock_with_dc(dc.device).map_err(|e| { + log::error!("unable to make the OpenGL context current for surface: {e}",); + crate::SurfaceError::Other("unable to make the OpenGL context current for surface") + })?; + + let renderbuffer = unsafe { gl.create_renderbuffer() }.map_err(|error| { + log::error!("Internal swapchain renderbuffer creation failed: {error}"); + crate::DeviceError::OutOfMemory + })?; + unsafe { gl.bind_renderbuffer(glow::RENDERBUFFER, Some(renderbuffer)) }; + unsafe { + gl.renderbuffer_storage( + glow::RENDERBUFFER, + format_desc.internal, + config.extent.width as _, + config.extent.height as _, + ) + }; + + let framebuffer = unsafe { gl.create_framebuffer() }.map_err(|error| { + log::error!("Internal swapchain framebuffer creation failed: {error}"); + crate::DeviceError::OutOfMemory + })?; + unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(framebuffer)) }; + unsafe { + gl.framebuffer_renderbuffer( + glow::READ_FRAMEBUFFER, + glow::COLOR_ATTACHMENT0, + glow::RENDERBUFFER, + Some(renderbuffer), + ) + }; + unsafe { gl.bind_renderbuffer(glow::RENDERBUFFER, None) }; + unsafe { gl.bind_framebuffer(glow::READ_FRAMEBUFFER, None) }; + + // Setup presentation mode + let extra = Wgl::load_with(|name| load_gl_func(name, None)); + let extensions = get_extensions(&extra, dc.device); + if !(extensions.contains("WGL_EXT_swap_control") && extra.SwapIntervalEXT.is_loaded()) { + log::error!("WGL_EXT_swap_control is unsupported"); + return Err(crate::SurfaceError::Other( + "WGL_EXT_swap_control is unsupported", + )); + } + + let vsync = match config.present_mode { + wgt::PresentMode::Immediate => false, + wgt::PresentMode::Fifo => true, + _ => { + log::error!("unsupported present mode: {:?}", config.present_mode); + return Err(crate::SurfaceError::Other("unsupported present mode")); + } + }; + + if unsafe { extra.SwapIntervalEXT(if vsync { 1 } else { 0 }) } == Foundation::FALSE.0 { + log::error!("unable to set swap interval: {}", Error::from_thread()); + return Err(crate::SurfaceError::Other("unable to set swap interval")); + } + + self.swapchain.write().replace(Swapchain { + renderbuffer, + framebuffer, + extent: config.extent, + format: config.format, + format_desc, + sample_type: wgt::TextureSampleType::Float { filterable: false }, + }); + + Ok(()) + } + + unsafe fn unconfigure(&self, device: &super::Device) { + let gl = &device.shared.context.lock(); + if let Some(sc) = self.swapchain.write().take() { + unsafe { + gl.delete_renderbuffer(sc.renderbuffer); + gl.delete_framebuffer(sc.framebuffer) + }; + } + } + + unsafe fn acquire_texture( + &self, + _timeout_ms: Option<Duration>, + _fence: &super::Fence, + ) -> Result<crate::AcquiredSurfaceTexture<super::Api>, crate::SurfaceError> { + let swapchain = self.swapchain.read(); + let sc = swapchain.as_ref().unwrap(); + let texture = super::Texture { + inner: super::TextureInner::Renderbuffer { + raw: sc.renderbuffer, + }, + drop_guard: None, + array_layer_count: 1, + mip_level_count: 1, + format: sc.format, + format_desc: sc.format_desc.clone(), + copy_size: crate::CopyExtent { + width: sc.extent.width, + height: sc.extent.height, + depth: 1, + }, + }; + Ok(crate::AcquiredSurfaceTexture { + texture, + suboptimal: false, + }) + } + unsafe fn discard_texture(&self, _texture: super::Texture) {} +} diff --git a/third_party/wgpu-hal/src/lib.rs b/third_party/wgpu-hal/src/lib.rs new file mode 100644 index 000000000..ca626f275 --- /dev/null +++ b/third_party/wgpu-hal/src/lib.rs @@ -0,0 +1,3086 @@ +//! 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 [`Device::map_buffer`] indicates that this is necessary. +//! +//! - 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 groups. +//! 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 +//! +//! ## Traits +//! +//! The `wgpu-hal` crate defines a handful of traits that together +//! represent a cross-platform abstraction for modern GPU APIs. +//! +//! - The [`Api`] trait represents a `wgpu-hal` backend. It has no methods of its +//! own, only a collection of associated types. +//! +//! - [`Api::Instance`] implements the [`Instance`] trait. [`Instance::init`] +//! creates an instance value, which you can use to enumerate the adapters +//! available on the system. For example, [`vulkan::Api::Instance::init`][Ii] +//! returns an instance that can enumerate the Vulkan physical devices on your +//! system. +//! +//! - [`Api::Adapter`] implements the [`Adapter`] trait, representing a +//! particular device from a particular backend. For example, a Vulkan instance +//! might have a Lavapipe software adapter and a GPU-based adapter. +//! +//! - [`Api::Device`] implements the [`Device`] trait, representing an active +//! link to a device. You get a device value by calling [`Adapter::open`], and +//! then use it to create buffers, textures, shader modules, and so on. +//! +//! - [`Api::Queue`] implements the [`Queue`] trait, which you use to submit +//! command buffers to a given device. +//! +//! - [`Api::CommandEncoder`] implements the [`CommandEncoder`] trait, which you +//! use to build buffers of commands to submit to a queue. This has all the +//! methods for drawing and running compute shaders, which is presumably what +//! you're here for. +//! +//! - [`Api::Surface`] implements the [`Surface`] trait, which represents a +//! swapchain for presenting images on the screen, via interaction with the +//! system's window manager. +//! +//! The [`Api`] trait has various other associated types like [`Api::Buffer`] and +//! [`Api::Texture`] that represent resources the rest of the interface can +//! operate on, but these generally do not have their own traits. +//! +//! [Ii]: Instance::init +//! +//! ## Validation is the calling code's responsibility, not `wgpu-hal`'s +//! +//! As much as possible, `wgpu-hal` traits place the burden of validation, +//! resource tracking, and state tracking on the caller, not on the trait +//! implementations themselves. Anything which can reasonably be handled in +//! backend-independent code should be. A `wgpu_hal` backend's sole obligation is +//! to provide portable behavior, and report conditions that the calling code +//! can't reasonably anticipate, like device loss or running out of memory. +//! +//! The `wgpu` crate collection is intended for use in security-sensitive +//! applications, like web browsers, where the API is available to untrusted +//! code. This means that `wgpu-core`'s validation is not simply a service to +//! developers, to be provided opportunistically when the performance costs are +//! acceptable and the necessary data is ready at hand. Rather, `wgpu-core`'s +//! validation must be exhaustive, to ensure that even malicious content cannot +//! provoke and exploit undefined behavior in the platform's graphics API. +//! +//! Because graphics APIs' requirements are complex, the only practical way for +//! `wgpu` to provide exhaustive validation is to comprehensively track the +//! lifetime and state of all the resources in the system. Implementing this +//! separately for each backend is infeasible; effort would be better spent +//! making the cross-platform validation in `wgpu-core` legible and trustworthy. +//! Fortunately, the requirements are largely similar across the various +//! platforms, so cross-platform validation is practical. +//! +//! Some backends have specific requirements that aren't practical to foist off +//! on the `wgpu-hal` user. For example, properly managing macOS Objective-C or +//! Microsoft COM reference counts is best handled by using appropriate pointer +//! types within the backend. +//! +//! A desire for "defense in depth" may suggest performing additional validation +//! in `wgpu-hal` when the opportunity arises, but this must be done with +//! caution. Even experienced contributors infer the expectations their changes +//! must meet by considering not just requirements made explicit in types, tests, +//! assertions, and comments, but also those implicit in the surrounding code. +//! When one sees validation or state-tracking code in `wgpu-hal`, it is tempting +//! to conclude, "Oh, `wgpu-hal` checks for this, so `wgpu-core` needn't worry +//! about it - that would be redundant!" The responsibility for exhaustive +//! validation always rests with `wgpu-core`, regardless of what may or may not +//! be checked in `wgpu-hal`. +//! +//! To this end, any "defense in depth" validation that does appear in `wgpu-hal` +//! for requirements that `wgpu-core` should have enforced should report failure +//! via the `unreachable!` macro, because problems detected at this stage always +//! indicate a bug in `wgpu-core`. +//! +//! ## 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 + +#![no_std] +#![cfg_attr(docsrs, feature(doc_cfg))] +#![allow( + // this happens on the GL backend, where it is both thread safe and non-thread safe in the same code. + clippy::arc_with_non_send_sync, + // We don't use syntax sugar where it's not necessary. + clippy::match_like_matches_macro, + // Redundant matching is more explicit. + clippy::redundant_pattern_matching, + // Explicit lifetimes are often easier to reason about. + clippy::needless_lifetimes, + // No need for defaults in the internal types. + clippy::new_without_default, + // Matches are good and extendable, no need to make an exception here. + clippy::single_match, + // Push commands are more regular than macros. + clippy::vec_init_then_push, + // TODO! + clippy::missing_safety_doc, + // It gets in the way a lot and does not prevent bugs in practice. + clippy::pattern_type_mismatch, + // We should investigate these. + clippy::large_enum_variant +)] +#![warn( + clippy::alloc_instead_of_core, + clippy::ptr_as_ptr, + clippy::std_instead_of_alloc, + clippy::std_instead_of_core, + trivial_casts, + trivial_numeric_casts, + unsafe_op_in_unsafe_fn, + unused_extern_crates, + unused_qualifications +)] + +extern crate alloc; +#[allow(unused_extern_crates)] +extern crate naga_types as nt; +extern crate wgpu_types as wgt; +// Each of these backends needs `std` in some fashion; usually `std::thread` functions. +#[cfg(any(dx12, gles_with_std, metal, vulkan))] +#[macro_use] +extern crate std; + +/// DirectX12 API internals. +#[cfg(dx12)] +pub mod dx12; +/// GLES API internals. +#[cfg(gles)] +pub mod gles; +/// Metal API internals. +#[cfg(metal)] +pub mod metal; +/// A dummy API implementation. +// TODO(https://github.com/gfx-rs/wgpu/issues/7120): this should have a cfg +pub mod noop; +/// Vulkan API internals. +#[cfg(vulkan)] +pub mod vulkan; + +pub mod auxil; +pub mod api { + #[cfg(dx12)] + pub use super::dx12::Api as Dx12; + #[cfg(gles)] + pub use super::gles::Api as Gles; + #[cfg(metal)] + pub use super::metal::Api as Metal; + pub use super::noop::Api as Noop; + #[cfg(vulkan)] + pub use super::vulkan::Api as Vulkan; +} + +mod dynamic; +#[cfg(feature = "validation_canary")] +mod validation_canary; + +#[cfg(feature = "validation_canary")] +pub use validation_canary::{ValidationCanary, VALIDATION_CANARY}; + +pub(crate) use dynamic::impl_dyn_resource; +pub use dynamic::{ + DynAccelerationStructure, DynAcquiredSurfaceTexture, DynAdapter, DynBindGroup, + DynBindGroupLayout, DynBuffer, DynCommandBuffer, DynCommandEncoder, DynComputePipeline, + DynDevice, DynExposedAdapter, DynFence, DynInstance, DynOpenDevice, DynPipelineCache, + DynPipelineLayout, DynQuerySet, DynQueue, DynRayTracingPipeline, DynRenderPipeline, + DynResource, DynSampler, DynShaderModule, DynSurface, DynSurfaceTexture, DynTexture, + DynTextureView, +}; + +#[allow(unused)] +use alloc::boxed::Box; +use alloc::{borrow::Cow, string::String, vec::Vec}; +use core::{ + borrow::Borrow, + error::Error, + fmt, + num::{NonZeroU32, NonZeroU64}, + ops::{Range, RangeInclusive}, + ptr::NonNull, +}; + +use bitflags::bitflags; +use raw_window_handle::DisplayHandle; +use thiserror::Error; +use wgt::WasmNotSendSync; + +cfg_if::cfg_if! { + if #[cfg(supports_ptr_atomics)] { + use alloc::sync::Arc; + } else if #[cfg(feature = "portable-atomic")] { + use portable_atomic_util::Arc; + } +} + +// - Vertex + Fragment +// - Compute +// Task + Mesh + Fragment +pub const MAX_CONCURRENT_SHADER_STAGES: usize = 3; +pub const MAX_ANISOTROPY: u8 = 16; +pub const MAX_BIND_GROUPS: usize = 8; +pub const MAX_VERTEX_BUFFERS: usize = 16; +pub const MAX_COLOR_ATTACHMENTS: usize = 8; +pub const MAX_MIP_LEVELS: u32 = 16; +/// Size of a single occlusion/timestamp query, when copied into a buffer, in bytes. +/// cbindgen:ignore +pub const QUERY_SIZE: wgt::BufferAddress = 8; + +pub type Label<'a> = Option<&'a str>; +pub type MemoryRange = Range<wgt::BufferAddress>; +pub type FenceValue = u64; +#[cfg(supports_64bit_atomics)] +pub type AtomicFenceValue = core::sync::atomic::AtomicU64; +#[cfg(not(supports_64bit_atomics))] +pub type AtomicFenceValue = portable_atomic::AtomicU64; + +/// A callback to signal that wgpu is no longer using a resource. +#[cfg(any(gles, vulkan, metal))] +pub type DropCallback = Box<dyn FnOnce() + Send + Sync + 'static>; + +#[cfg(any(gles, vulkan, metal))] +pub struct DropGuard { + callback: Option<DropCallback>, +} + +#[cfg(all(any(gles, vulkan, metal), any(native, Emscripten)))] +impl DropGuard { + fn from_option(callback: Option<DropCallback>) -> Option<Self> { + callback.map(Self::new) + } + + fn new(callback: DropCallback) -> Self { + Self { + callback: Some(callback), + } + } +} + +#[cfg(any(gles, vulkan, metal))] +impl Drop for DropGuard { + fn drop(&mut self) { + if let Some(cb) = self.callback.take() { + (cb)(); + } + } +} + +#[cfg(any(gles, vulkan, metal))] +impl fmt::Debug for DropGuard { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("DropGuard").finish() + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum DeviceError { + #[error("Out of memory")] + OutOfMemory, + #[error("Device is lost")] + Lost, + #[error("Unexpected error variant (driver implementation is at fault)")] + Unexpected, +} + +#[cfg(any(dx12, vulkan))] +impl From<gpu_allocator::AllocationError> for DeviceError { + fn from(result: gpu_allocator::AllocationError) -> Self { + match result { + gpu_allocator::AllocationError::OutOfMemory => Self::OutOfMemory, + gpu_allocator::AllocationError::FailedToMap(e) => { + log::error!("gpu-allocator: Failed to map: {e}"); + Self::Lost + } + gpu_allocator::AllocationError::NoCompatibleMemoryTypeFound => { + log::error!("gpu-allocator: No Compatible Memory Type Found"); + Self::Lost + } + gpu_allocator::AllocationError::InvalidAllocationCreateDesc => { + log::error!("gpu-allocator: Invalid Allocation Creation Description"); + Self::Lost + } + gpu_allocator::AllocationError::InvalidAllocatorCreateDesc(e) => { + log::error!("gpu-allocator: Invalid Allocator Creation Description: {e}"); + Self::Lost + } + + gpu_allocator::AllocationError::Internal(e) => { + log::error!("gpu-allocator: Internal Error: {e}"); + Self::Lost + } + gpu_allocator::AllocationError::BarrierLayoutNeedsDevice10 + | gpu_allocator::AllocationError::CastableFormatsRequiresEnhancedBarriers + | gpu_allocator::AllocationError::CastableFormatsRequiresAtLeastDevice12 => { + unreachable!() + } + } + } +} + +// A copy of gpu_allocator::AllocationSizes, allowing to read the configured value for +// the dx12 backend, we should instead add getters to gpu_allocator::AllocationSizes +// and remove this type. +// https://github.com/Traverse-Research/gpu-allocator/issues/295 +#[cfg_attr(not(any(dx12, vulkan)), expect(dead_code))] +pub(crate) struct AllocationSizes { + pub(crate) min_device_memblock_size: u64, + pub(crate) max_device_memblock_size: u64, + pub(crate) min_host_memblock_size: u64, + pub(crate) max_host_memblock_size: u64, +} + +impl AllocationSizes { + #[allow(dead_code, reason = "may be unused on some platforms")] + pub(crate) fn from_memory_hints(memory_hints: &wgt::MemoryHints) -> Self { + // TODO: the allocator's configuration should take hardware capability into + // account. + const MB: u64 = 1024 * 1024; + + match memory_hints { + wgt::MemoryHints::Performance => Self { + min_device_memblock_size: 128 * MB, + max_device_memblock_size: 256 * MB, + min_host_memblock_size: 64 * MB, + max_host_memblock_size: 128 * MB, + }, + wgt::MemoryHints::MemoryUsage => Self { + min_device_memblock_size: 8 * MB, + max_device_memblock_size: 64 * MB, + min_host_memblock_size: 4 * MB, + max_host_memblock_size: 32 * MB, + }, + wgt::MemoryHints::Manual { + suballocated_device_memory_block_size, + } => { + // TODO: https://github.com/gfx-rs/wgpu/issues/8625 + // Would it be useful to expose the host size in memory hints + // instead of always using half of the device size? + let device_size = suballocated_device_memory_block_size; + let host_size = device_size.start / 2..device_size.end / 2; + + // gpu_allocator clamps the sizes between 4MiB and 256MiB, but we clamp them ourselves since we use + // the sizes when detecting high memory pressure and there is no way to query the values otherwise. + Self { + min_device_memblock_size: device_size.start.clamp(4 * MB, 256 * MB), + max_device_memblock_size: device_size.end.clamp(4 * MB, 256 * MB), + min_host_memblock_size: host_size.start.clamp(4 * MB, 256 * MB), + max_host_memblock_size: host_size.end.clamp(4 * MB, 256 * MB), + } + } + } + } +} + +#[cfg(any(dx12, vulkan))] +impl From<AllocationSizes> for gpu_allocator::AllocationSizes { + fn from(value: AllocationSizes) -> gpu_allocator::AllocationSizes { + gpu_allocator::AllocationSizes::new( + value.min_device_memblock_size, + value.min_host_memblock_size, + ) + .with_max_device_memblock_size(value.max_device_memblock_size) + .with_max_host_memblock_size(value.max_host_memblock_size) + } +} + +#[allow(dead_code, reason = "may be unused on some platforms")] +#[cold] +fn hal_usage_error<T: fmt::Display>(txt: T) -> ! { + panic!("wgpu-hal invariant was violated (usage error): {txt}") +} + +#[allow(dead_code, reason = "may be unused on some platforms")] +#[cold] +fn hal_internal_error<T: fmt::Display>(txt: T) -> ! { + panic!("wgpu-hal ran into a preventable internal error: {txt}") +} + +#[derive(Clone, Debug, Eq, PartialEq, Error)] +pub enum ShaderError { + #[error("Compilation failed: {0:?}")] + Compilation(String), + #[error(transparent)] + Device(#[from] DeviceError), +} + +#[derive(Clone, Debug, Eq, PartialEq, Error)] +pub enum PipelineError { + #[error("Linkage failed for stage {0:?}: {1}")] + Linkage(wgt::ShaderStages, String), + #[error("Entry point for stage {0:?} is invalid")] + EntryPoint(naga::ShaderStage), + #[error(transparent)] + Device(#[from] DeviceError), + #[error("Pipeline constant error for stage {0:?}: {1}")] + PipelineConstants(wgt::ShaderStages, String), +} + +#[derive(Clone, Debug, Eq, PartialEq, Error)] +pub enum PipelineCacheError { + #[error(transparent)] + Device(#[from] DeviceError), +} + +#[derive(Clone, Debug, Eq, PartialEq, Error)] +pub enum SurfaceError { + #[error("Surface is lost")] + Lost, + #[error("Surface is outdated, needs to be re-created")] + Outdated, + #[error("Timed out waiting for a surface texture")] + Timeout, + #[error("The window is occluded (e.g. minimized or behind another window). Try again once the window is no longer occluded.")] + Occluded, + #[error(transparent)] + Device(#[from] DeviceError), + #[error("Other reason: {0}")] + Other(&'static str), +} + +/// Error occurring while trying to create an instance, or create a surface from an instance; +/// typically relating to the state of the underlying graphics API or hardware. +#[derive(Clone, Debug, Error)] +#[error("{message}")] +pub struct InstanceError { + /// These errors are very platform specific, so do not attempt to encode them as an enum. + /// + /// This message should describe the problem in sufficient detail to be useful for a + /// user-to-developer “why won't this work on my machine” bug report, and otherwise follow + /// <https://rust-lang.github.io/api-guidelines/interoperability.html#error-types-are-meaningful-and-well-behaved-c-good-err>. + message: String, + + /// Underlying error value, if any is available. + #[source] + source: Option<Arc<dyn Error + Send + Sync + 'static>>, +} + +impl InstanceError { + #[allow(dead_code, reason = "may be unused on some platforms")] + pub(crate) fn new(message: String) -> Self { + Self { + message, + source: None, + } + } + #[allow(dead_code, reason = "may be unused on some platforms")] + pub(crate) fn with_source(message: String, source: impl Error + Send + Sync + 'static) -> Self { + cfg_if::cfg_if! { + if #[cfg(supports_ptr_atomics)] { + let source = Arc::new(source); + } else { + // TODO(https://github.com/rust-lang/rust/issues/18598): avoid indirection via Box once arbitrary types support unsized coercion + let source: Box<dyn Error + Send + Sync + 'static> = Box::new(source); + let source = Arc::from(source); + } + } + Self { + message, + source: Some(source), + } + } +} + +/// All the types and methods that make up a implementation on top of a backend. +/// +/// Only the types that have non-dyn trait bounds have methods on them. Most methods +/// are either on [`CommandEncoder`] or [`Device`]. +/// +/// The api can either be used through generics (through use of this trait and associated +/// types) or dynamically through using the `Dyn*` traits. +pub trait Api: Clone + fmt::Debug + Sized + WasmNotSendSync + 'static { + const VARIANT: wgt::Backend; + + type Instance: DynInstance + Instance<A = Self>; + type Surface: DynSurface + Surface<A = Self>; + type Adapter: DynAdapter + Adapter<A = Self>; + type Device: DynDevice + Device<A = Self>; + + type Queue: DynQueue + Queue<A = Self>; + type CommandEncoder: DynCommandEncoder + CommandEncoder<A = Self>; + + /// This API's command buffer type. + /// + /// The only thing you can do with `CommandBuffer`s is build them + /// with a [`CommandEncoder`] and then pass them to + /// [`Queue::submit`] for execution, or destroy them by passing + /// them to [`CommandEncoder::reset_all`]. + /// + /// [`CommandEncoder`]: Api::CommandEncoder + type CommandBuffer: DynCommandBuffer; + + type Buffer: DynBuffer; + type Texture: DynTexture; + type SurfaceTexture: DynSurfaceTexture + Borrow<Self::Texture>; + type TextureView: DynTextureView; + type Sampler: DynSampler; + type QuerySet: DynQuerySet; + + /// A value you can block on to wait for something to finish. + /// + /// A `Fence` holds a monotonically increasing [`FenceValue`]. You can call + /// [`Device::wait`] to block until a fence reaches or passes a value you + /// choose. [`Queue::submit`] can take a `Fence` and a [`FenceValue`] to + /// store in it when the submitted work is complete. + /// + /// Attempting to set a fence to a value less than its current value has no + /// effect. + /// + /// Waiting on a fence returns as soon as the fence reaches *or passes* the + /// requested value. This implies that, in order to reliably determine when + /// an operation has completed, operations must finish in order of + /// increasing fence values: if a higher-valued operation were to finish + /// before a lower-valued operation, then waiting for the fence to reach the + /// lower value could return before the lower-valued operation has actually + /// finished. + /// + /// Fences are internally synchronised by the hal, and so should not need to be + /// contained in external synchronisation primitives. + type Fence: DynFence; + + type BindGroupLayout: DynBindGroupLayout; + type BindGroup: DynBindGroup; + type PipelineLayout: DynPipelineLayout; + type ShaderModule: DynShaderModule; + type RenderPipeline: DynRenderPipeline; + type ComputePipeline: DynComputePipeline; + type RayTracingPipeline: DynRayTracingPipeline; + type PipelineCache: DynPipelineCache; + + type AccelerationStructure: DynAccelerationStructure + 'static; +} + +pub trait Instance: Sized + WasmNotSendSync { + type A: Api; + + unsafe fn init(desc: &InstanceDescriptor<'_>) -> Result<Self, InstanceError>; + unsafe fn create_surface( + &self, + display_handle: raw_window_handle::RawDisplayHandle, + window_handle: raw_window_handle::RawWindowHandle, + ) -> Result<<Self::A as Api>::Surface, InstanceError>; + /// `surface_hint` is only used by the GLES backend targeting WebGL2 + unsafe fn enumerate_adapters( + &self, + surface_hint: Option<&<Self::A as Api>::Surface>, + ) -> Vec<ExposedAdapter<Self::A>>; +} + +pub trait Surface: WasmNotSendSync { + type A: Api; + + /// Configure `self` to use `device`. + /// + /// # Safety + /// + /// - All GPU work using `self` must have been completed. + /// - All [`AcquiredSurfaceTexture`]s must have been destroyed. + /// - All [`Api::TextureView`]s derived from the [`AcquiredSurfaceTexture`]s must have been destroyed. + /// - The surface `self` must not currently be configured to use any other [`Device`]. + unsafe fn configure( + &self, + device: &<Self::A as Api>::Device, + config: &SurfaceConfiguration, + ) -> Result<(), SurfaceError>; + + /// Unconfigure `self` on `device`. + /// + /// # Safety + /// + /// - All GPU work that uses `surface` must have been completed. + /// - All [`AcquiredSurfaceTexture`]s must have been destroyed. + /// - All [`Api::TextureView`]s derived from the [`AcquiredSurfaceTexture`]s must have been destroyed. + /// - The surface `self` must have been configured on `device`. + unsafe fn unconfigure(&self, device: &<Self::A as Api>::Device); + + /// Return the next texture to be presented by `self`, for the caller to draw on. + /// + /// On success, return an [`AcquiredSurfaceTexture`] representing the + /// texture into which the caller should draw the image to be displayed on + /// `self`. + /// + /// If `timeout` elapses before `self` has a texture ready to be acquired, + /// return `Err(SurfaceError::Timeout)`. If `timeout` is `None`, wait + /// indefinitely, with no timeout. + /// + /// # Using an [`AcquiredSurfaceTexture`] + /// + /// On success, this function returns an [`AcquiredSurfaceTexture`] whose + /// [`texture`] field is a [`SurfaceTexture`] from which the caller can + /// [`borrow`] a [`Texture`] to draw on. The [`AcquiredSurfaceTexture`] also + /// carries some metadata about that [`SurfaceTexture`]. + /// + /// All calls to [`Queue::submit`] that draw on that [`Texture`] must also + /// include the [`SurfaceTexture`] in the `surface_textures` argument. + /// + /// When you are done drawing on the texture, you can display it on `self` + /// by passing the [`SurfaceTexture`] and `self` to [`Queue::present`]. + /// + /// If you do not wish to display the texture, you must pass the + /// [`SurfaceTexture`] to [`self.discard_texture`], so that it can be reused + /// by future acquisitions. + /// + /// The fence is internally synchronised by the hal. + /// + /// # Portability + /// + /// Some backends can't support a timeout when acquiring a texture. On these + /// backends, `timeout` is ignored. + /// + /// On macOS, this returns `Err(SurfaceError::Timeout)` when the window is + /// not visible (minimized, fully occluded, or on another virtual desktop) + /// to avoid blocking in `CAMetalLayer.nextDrawable()`. + /// + /// # Safety + /// + /// - The surface `self` must currently be configured on some [`Device`]. + /// + /// - The `fence` argument must be the same [`Fence`] passed to all calls to + /// [`Queue::submit`] that used [`Texture`]s acquired from this surface. + /// + /// - You may only have one texture acquired from `self` at a time. When + /// `acquire_texture` returns `Ok(ast)`, you must pass the returned + /// [`SurfaceTexture`] `ast.texture` to either [`Queue::present`] or + /// [`Surface::discard_texture`] before calling `acquire_texture` again. + /// + /// [`texture`]: AcquiredSurfaceTexture::texture + /// [`SurfaceTexture`]: Api::SurfaceTexture + /// [`borrow`]: alloc::borrow::Borrow::borrow + /// [`Texture`]: Api::Texture + /// [`Fence`]: Api::Fence + /// [`self.discard_texture`]: Surface::discard_texture + unsafe fn acquire_texture( + &self, + timeout: Option<core::time::Duration>, + fence: &<Self::A as Api>::Fence, + ) -> Result<AcquiredSurfaceTexture<Self::A>, SurfaceError>; + + /// Relinquish an acquired texture without presenting it. + /// + /// After this call, the texture underlying [`SurfaceTexture`] may be + /// returned by subsequent calls to [`self.acquire_texture`]. + /// + /// # Safety + /// + /// - The surface `self` must currently be configured on some [`Device`]. + /// + /// - `texture` must be a [`SurfaceTexture`] returned by a call to + /// [`self.acquire_texture`] that has not yet been passed to + /// [`Queue::present`]. + /// + /// [`SurfaceTexture`]: Api::SurfaceTexture + /// [`self.acquire_texture`]: Surface::acquire_texture + unsafe fn discard_texture(&self, texture: <Self::A as Api>::SurfaceTexture); +} + +pub trait Adapter: WasmNotSendSync { + type A: Api; + + unsafe fn open( + &self, + features: wgt::Features, + limits: &wgt::Limits, + memory_hints: &wgt::MemoryHints, + ) -> Result<OpenDevice<Self::A>, DeviceError>; + + /// Return the set of supported capabilities for a texture format. + unsafe fn texture_format_capabilities( + &self, + format: wgt::TextureFormat, + ) -> TextureFormatCapabilities; + + /// Returns the capabilities of working with a specified surface. + /// + /// `None` means presentation is not supported for it. + unsafe fn surface_capabilities( + &self, + surface: &<Self::A as Api>::Surface, + ) -> Option<SurfaceCapabilities>; + + /// Returns the HDR / luminance characteristics of the display backing + /// `surface`, queried from the OS on each call. + /// + /// `None` means no information is available; wgpu-core maps it to + /// [`wgt::DisplayHdrInfo::default`]. Implementors must not panic; degrade any + /// OS-query failure to `None`. The default implementation returns `None`. + /// + /// Implemented by Metal (macOS only, and only from the main thread), DX12, and + /// Vulkan (Win32 `HWND` surfaces only); GLES and noop keep the default `None`. + unsafe fn surface_display_hdr_info( + &self, + surface: &<Self::A as Api>::Surface, + ) -> Option<wgt::DisplayHdrInfo> { + let _ = surface; + None + } + + /// Creates a [`PresentationTimestamp`] using the adapter's WSI. + /// + /// [`PresentationTimestamp`]: wgt::PresentationTimestamp + unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp; + + /// The combination of all usages that the are guaranteed to be be ordered by the hardware. + /// If a usage is ordered, then if the buffer state doesn't change between draw calls, + /// there are no barriers needed for synchronization. + fn get_ordered_buffer_usages(&self) -> wgt::BufferUses; + + /// The combination of all usages that the are guaranteed to be be ordered by the hardware. + /// If a usage is ordered, then if the buffer state doesn't change between draw calls, + /// there are no barriers needed for synchronization. + fn get_ordered_texture_usages(&self) -> wgt::TextureUses; +} + +/// A connection to a GPU and a pool of resources to use with it. +/// +/// A `wgpu-hal` `Device` represents an open connection to a specific graphics +/// processor, controlled via the backend [`Device::A`]. A `Device` is mostly +/// used for creating resources. Each `Device` has an associated [`Queue`] used +/// for command submission. +/// +/// On Vulkan a `Device` corresponds to a logical device ([`VkDevice`]). Other +/// backends don't have an exact analog: for example, [`ID3D12Device`]s and +/// [`MTLDevice`]s are owned by the backends' [`wgpu_hal::Adapter`] +/// implementations, and shared by all [`wgpu_hal::Device`]s created from that +/// `Adapter`. +/// +/// A `Device`'s life cycle is generally: +/// +/// 1) Obtain a `Device` and its associated [`Queue`] by calling +/// [`Adapter::open`]. +/// +/// Alternatively, the backend-specific types that implement [`Adapter`] often +/// have methods for creating a `wgpu-hal` `Device` from a platform-specific +/// handle. For example, [`vulkan::Adapter::device_from_raw`] can create a +/// [`vulkan::Device`] from an [`ash::Device`]. +/// +/// 1) Create resources to use on the device by calling methods like +/// [`Device::create_texture`] or [`Device::create_shader_module`]. +/// +/// 1) Call [`Device::create_command_encoder`] to obtain a [`CommandEncoder`], +/// which you can use to build [`CommandBuffer`]s holding commands to be +/// executed on the GPU. +/// +/// 1) Call [`Queue::submit`] on the `Device`'s associated [`Queue`] to submit +/// [`CommandBuffer`]s for execution on the GPU. If needed, call +/// [`Device::wait`] to wait for them to finish execution. +/// +/// 1) Free resources with methods like [`Device::destroy_texture`] or +/// [`Device::destroy_shader_module`]. +/// +/// 1) Drop the device. +/// +/// [`vkDevice`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#VkDevice +/// [`ID3D12Device`]: https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nn-d3d12-id3d12device +/// [`MTLDevice`]: https://developer.apple.com/documentation/metal/mtldevice +/// [`wgpu_hal::Adapter`]: Adapter +/// [`wgpu_hal::Device`]: Device +/// [`vulkan::Adapter::device_from_raw`]: vulkan/struct.Adapter.html#method.device_from_raw +/// [`vulkan::Device`]: vulkan/struct.Device.html +/// [`ash::Device`]: https://docs.rs/ash/latest/ash/struct.Device.html +/// [`CommandBuffer`]: Api::CommandBuffer +/// +/// # Safety +/// +/// As with other `wgpu-hal` APIs, [validation] is the caller's +/// responsibility. Here are the general requirements for all `Device` +/// methods: +/// +/// - Any resource passed to a `Device` method must have been created by that +/// `Device`. For example, a [`Texture`] passed to [`Device::destroy_texture`] must +/// have been created with the `Device` passed as `self`. +/// +/// - Resources may not be destroyed if they are used by any submitted command +/// buffers that have not yet finished execution. +/// +/// [validation]: index.html#validation-is-the-calling-codes-responsibility-not-wgpu-hals +/// [`Texture`]: Api::Texture +pub trait Device: WasmNotSendSync { + type A: Api; + + /// Creates a new buffer. + /// + /// The initial usage is `wgt::BufferUses::empty()`. + unsafe fn create_buffer( + &self, + desc: &BufferDescriptor, + ) -> Result<<Self::A as Api>::Buffer, DeviceError>; + + /// Free `buffer` and any GPU resources it owns. + /// + /// Note that backends are allowed to allocate GPU memory for buffers from + /// allocation pools, and this call is permitted to simply return `buffer`'s + /// storage to that pool, without making it available to other applications. + /// + /// # Safety + /// + /// - The given `buffer` must not currently be mapped. + unsafe fn destroy_buffer(&self, buffer: <Self::A as Api>::Buffer); + + /// A hook for when a wgpu-core buffer is created from a raw wgpu-hal buffer. + unsafe fn add_raw_buffer(&self, buffer: &<Self::A as Api>::Buffer); + + /// Return a pointer to CPU memory mapping the contents of `buffer`. + /// + /// Buffer mappings are persistent: the buffer may remain mapped on the CPU + /// while the GPU reads or writes to it. (Note that `wgpu_core` does not use + /// this feature: when a `wgpu_core::Buffer` is unmapped, the underlying + /// `wgpu_hal` buffer is also unmapped.) + /// + /// If this function returns `Ok(mapping)`, then: + /// + /// - `mapping.ptr` is the CPU address of the start of the mapped memory. + /// + /// - If `mapping.is_coherent` is `true`, then CPU writes to the mapped + /// memory are immediately visible on the GPU, and vice versa. + /// + /// # Safety + /// + /// - The given `buffer` must have been created with the [`MAP_READ`] or + /// [`MAP_WRITE`] flags set in [`BufferDescriptor::usage`]. + /// + /// - The given `range` must fall within the size of `buffer`. + /// + /// - The caller must avoid data races between the CPU and the GPU. A data + /// race is any pair of accesses to a particular byte, one of which is a + /// write, that are not ordered with respect to each other by some sort of + /// synchronization operation. + /// + /// - If this function returns `Ok(mapping)` and `mapping.is_coherent` is + /// `false`, then: + /// + /// - Every CPU write to a mapped byte followed by a GPU read of that byte + /// must have at least one call to [`Device::flush_mapped_ranges`] + /// covering that byte that occurs between those two accesses. + /// + /// - Every GPU write to a mapped byte followed by a CPU read of that byte + /// must have at least one call to [`Device::invalidate_mapped_ranges`] + /// covering that byte that occurs between those two accesses. + /// + /// Note that the data race rule above requires that all such access pairs + /// be ordered, so it is meaningful to talk about what must occur + /// "between" them. + /// + /// - Zero-sized mappings are not allowed. + /// + /// - The returned [`BufferMapping::ptr`] must not be used after a call to + /// [`Device::unmap_buffer`]. + /// + /// [`MAP_READ`]: wgt::BufferUses::MAP_READ + /// [`MAP_WRITE`]: wgt::BufferUses::MAP_WRITE + unsafe fn map_buffer( + &self, + buffer: &<Self::A as Api>::Buffer, + range: MemoryRange, + ) -> Result<BufferMapping, DeviceError>; + + /// Remove the mapping established by the last call to [`Device::map_buffer`]. + /// + /// # Safety + /// + /// - The given `buffer` must be currently mapped. + unsafe fn unmap_buffer(&self, buffer: &<Self::A as Api>::Buffer); + + /// Indicate that CPU writes to mapped buffer memory should be made visible to the GPU. + /// + /// # Safety + /// + /// - The given `buffer` must be currently mapped. + /// + /// - All ranges produced by `ranges` must fall within `buffer`'s size. + unsafe fn flush_mapped_ranges<I>(&self, buffer: &<Self::A as Api>::Buffer, ranges: I) + where + I: Iterator<Item = MemoryRange>; + + /// Indicate that GPU writes to mapped buffer memory should be made visible to the CPU. + /// + /// # Safety + /// + /// - The given `buffer` must be currently mapped. + /// + /// - All ranges produced by `ranges` must fall within `buffer`'s size. + unsafe fn invalidate_mapped_ranges<I>(&self, buffer: &<Self::A as Api>::Buffer, ranges: I) + where + I: Iterator<Item = MemoryRange>; + + /// Creates a new texture. + /// + /// The initial usage for all subresources is `wgt::TextureUses::UNINITIALIZED`. + unsafe fn create_texture( + &self, + desc: &TextureDescriptor, + ) -> Result<<Self::A as Api>::Texture, DeviceError>; + unsafe fn destroy_texture(&self, texture: <Self::A as Api>::Texture); + + /// A hook for when a wgpu-core texture is created from a raw wgpu-hal texture. + unsafe fn add_raw_texture(&self, texture: &<Self::A as Api>::Texture); + + unsafe fn create_texture_view( + &self, + texture: &<Self::A as Api>::Texture, + desc: &TextureViewDescriptor, + ) -> Result<<Self::A as Api>::TextureView, DeviceError>; + unsafe fn destroy_texture_view(&self, view: <Self::A as Api>::TextureView); + unsafe fn create_sampler( + &self, + desc: &SamplerDescriptor, + ) -> Result<<Self::A as Api>::Sampler, DeviceError>; + unsafe fn destroy_sampler(&self, sampler: <Self::A as Api>::Sampler); + + /// Create a fresh [`CommandEncoder`]. + /// + /// The new `CommandEncoder` is in the "closed" state. + unsafe fn create_command_encoder( + &self, + desc: &CommandEncoderDescriptor<<Self::A as Api>::Queue>, + ) -> Result<<Self::A as Api>::CommandEncoder, DeviceError>; + + /// Creates a bind group layout. + unsafe fn create_bind_group_layout( + &self, + desc: &BindGroupLayoutDescriptor, + ) -> Result<<Self::A as Api>::BindGroupLayout, DeviceError>; + unsafe fn destroy_bind_group_layout(&self, bg_layout: <Self::A as Api>::BindGroupLayout); + unsafe fn create_pipeline_layout( + &self, + desc: &PipelineLayoutDescriptor<<Self::A as Api>::BindGroupLayout>, + ) -> Result<<Self::A as Api>::PipelineLayout, DeviceError>; + unsafe fn destroy_pipeline_layout(&self, pipeline_layout: <Self::A as Api>::PipelineLayout); + + #[allow(clippy::type_complexity)] + unsafe fn create_bind_group( + &self, + desc: &BindGroupDescriptor< + <Self::A as Api>::BindGroupLayout, + <Self::A as Api>::Buffer, + <Self::A as Api>::Sampler, + <Self::A as Api>::TextureView, + <Self::A as Api>::AccelerationStructure, + >, + ) -> Result<<Self::A as Api>::BindGroup, DeviceError>; + unsafe fn destroy_bind_group(&self, group: <Self::A as Api>::BindGroup); + + unsafe fn create_shader_module( + &self, + desc: &ShaderModuleDescriptor, + shader: ShaderInput, + ) -> Result<<Self::A as Api>::ShaderModule, ShaderError>; + unsafe fn destroy_shader_module(&self, module: <Self::A as Api>::ShaderModule); + + #[allow(clippy::type_complexity)] + unsafe fn create_render_pipeline( + &self, + desc: &RenderPipelineDescriptor< + <Self::A as Api>::PipelineLayout, + <Self::A as Api>::ShaderModule, + <Self::A as Api>::PipelineCache, + >, + ) -> Result<<Self::A as Api>::RenderPipeline, PipelineError>; + unsafe fn destroy_render_pipeline(&self, pipeline: <Self::A as Api>::RenderPipeline); + + #[allow(clippy::type_complexity)] + unsafe fn create_compute_pipeline( + &self, + desc: &ComputePipelineDescriptor< + <Self::A as Api>::PipelineLayout, + <Self::A as Api>::ShaderModule, + <Self::A as Api>::PipelineCache, + >, + ) -> Result<<Self::A as Api>::ComputePipeline, PipelineError>; + unsafe fn destroy_compute_pipeline(&self, pipeline: <Self::A as Api>::ComputePipeline); + + #[allow(clippy::type_complexity)] + unsafe fn create_ray_tracing_pipeline( + &self, + desc: &RayTracingPipelineDescriptor< + <Self::A as Api>::PipelineLayout, + <Self::A as Api>::ShaderModule, + <Self::A as Api>::PipelineCache, + >, + ) -> Result<<Self::A as Api>::RayTracingPipeline, PipelineError>; + unsafe fn destroy_ray_tracing_pipeline(&self, pipeline: <Self::A as Api>::RayTracingPipeline); + /// Obtain the opaque data from each group, behaves as if group 0 is the ray generation, group 1 + /// is the miss shader, and group 2.. are the intersection groups. + unsafe fn get_raytracing_pipeline_group_data( + &self, + pipeline: &<Self::A as Api>::RayTracingPipeline, + groups: Range<u32>, + ) -> Result<Vec<u8>, DeviceError>; + + unsafe fn create_pipeline_cache( + &self, + desc: &PipelineCacheDescriptor<'_>, + ) -> Result<<Self::A as Api>::PipelineCache, PipelineCacheError>; + fn pipeline_cache_validation_key(&self) -> Option<[u8; 16]> { + None + } + unsafe fn destroy_pipeline_cache(&self, cache: <Self::A as Api>::PipelineCache); + + unsafe fn create_query_set( + &self, + desc: &wgt::QuerySetDescriptor<Label>, + ) -> Result<<Self::A as Api>::QuerySet, DeviceError>; + unsafe fn destroy_query_set(&self, set: <Self::A as Api>::QuerySet); + unsafe fn create_fence(&self) -> Result<<Self::A as Api>::Fence, DeviceError>; + unsafe fn destroy_fence(&self, fence: <Self::A as Api>::Fence); + unsafe fn get_fence_value( + &self, + fence: &<Self::A as Api>::Fence, + ) -> Result<FenceValue, DeviceError>; + + /// Wait for `fence` to reach `value`. + /// + /// Operations like [`Queue::submit`] can accept a [`Fence`] and a + /// [`FenceValue`] to store in it, so you can use this `wait` function + /// to wait for a given queue submission to finish execution. + /// + /// The `value` argument must not exceed the highest value that an actual + /// operation you have already presented to the device is going to store in + /// `fence`. You cannot wait for values yet to be submitted. (This + /// restriction accommodates implementations like the `vulkan` backend's + /// [`FencePool`] that must allocate a distinct synchronization object for + /// each fence value one is able to wait for.) + /// + /// Calling `wait` with a lower [`FenceValue`] than `fence`'s current value + /// returns immediately. + /// + /// If `timeout` is not provided, the function will block indefinitely or until + /// an error is encountered. + /// + /// Returns `Ok(true)` on success and `Ok(false)` on timeout. + /// + /// [`Fence`]: Api::Fence + /// [`FencePool`]: vulkan/enum.Fence.html#variant.FencePool + unsafe fn wait( + &self, + fence: &<Self::A as Api>::Fence, + value: FenceValue, + timeout: Option<core::time::Duration>, + ) -> Result<bool, DeviceError>; + + /// Start a graphics debugger capture. + /// + /// # Safety + /// + /// See [`wgpu::Device::start_graphics_debugger_capture`][api] for more details. + /// + /// [api]: ../wgpu/struct.Device.html#method.start_graphics_debugger_capture + unsafe fn start_graphics_debugger_capture(&self) -> bool; + + /// Stop a graphics debugger capture. + /// + /// # Safety + /// + /// See [`wgpu::Device::stop_graphics_debugger_capture`][api] for more details. + /// + /// [api]: ../wgpu/struct.Device.html#method.stop_graphics_debugger_capture + unsafe fn stop_graphics_debugger_capture(&self); + + #[allow(unused_variables)] + unsafe fn pipeline_cache_get_data( + &self, + cache: &<Self::A as Api>::PipelineCache, + ) -> Option<Vec<u8>> { + None + } + + unsafe fn create_acceleration_structure( + &self, + desc: &AccelerationStructureDescriptor, + ) -> Result<<Self::A as Api>::AccelerationStructure, DeviceError>; + unsafe fn get_acceleration_structure_build_sizes( + &self, + desc: &GetAccelerationStructureBuildSizesDescriptor<<Self::A as Api>::Buffer>, + ) -> AccelerationStructureBuildSizes; + unsafe fn get_acceleration_structure_device_address( + &self, + acceleration_structure: &<Self::A as Api>::AccelerationStructure, + ) -> wgt::BufferAddress; + unsafe fn destroy_acceleration_structure( + &self, + acceleration_structure: <Self::A as Api>::AccelerationStructure, + ); + fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec<u8>; + + fn get_internal_counters(&self) -> wgt::HalCounters; + + fn generate_allocator_report(&self) -> Option<wgt::AllocatorReport> { + None + } + + fn check_if_oom(&self) -> Result<(), DeviceError>; +} + +pub trait Queue: WasmNotSendSync { + type A: Api; + + /// Submit `command_buffers` for execution on GPU. + /// + /// Update `fence` to `value` when the operation is complete. See + /// [`Fence`] for details. + /// + /// All command buffers submitted to a `wgpu_hal` queue are executed in the + /// order they're submitted, with each buffer able to observe the effects of + /// previous buffers' execution. Specifically: + /// + /// - If two calls to `submit` on a single `Queue` occur in a particular + /// order (that is, they happen on the same thread, or on two threads that + /// have synchronized to establish an ordering), then the first + /// submission's commands all complete execution before any of the second + /// submission's commands begin. All results produced by one submission + /// are visible to the next. + /// + /// - Within a submission, command buffers execute in the order in which they + /// appear in `command_buffers`. All results produced by one buffer are + /// visible to the next. + /// + /// If two calls to `submit` on a single `Queue` from different threads are + /// not synchronized to occur in a particular order, they must pass distinct + /// [`Fence`]s. As explained in the [`Fence`] documentation, waiting for + /// operations to complete is only trustworthy when operations finish in + /// order of increasing fence value, but submissions from different threads + /// cannot determine how to order the fence values if the submissions + /// themselves are unordered. If each thread uses a separate [`Fence`], this + /// problem does not arise. + /// + /// # Safety + /// + /// - Each [`CommandBuffer`][cb] in `command_buffers` must have been created + /// from a [`CommandEncoder`][ce] that was constructed from the + /// [`Device`][d] associated with this [`Queue`]. + /// + /// - Each [`CommandBuffer`][cb] must remain alive until the submitted + /// commands have finished execution. Since command buffers must not + /// outlive their encoders, this implies that the encoders must remain + /// alive as well. + /// + /// - All resources used by a submitted [`CommandBuffer`][cb] + /// ([`Texture`][t]s, [`BindGroup`][bg]s, [`RenderPipeline`][rp]s, and so + /// on) must remain alive until the command buffer finishes execution. + /// + /// - Every [`SurfaceTexture`][st] that any command in `command_buffers` + /// writes to must appear in the `surface_textures` argument. + /// + /// - No [`SurfaceTexture`][st] may appear in the `surface_textures` + /// argument more than once. + /// + /// - Each [`SurfaceTexture`][st] in `surface_textures` must be configured + /// for use with the [`Device`][d] associated with this [`Queue`], + /// typically by calling [`Surface::configure`]. + /// + /// - All calls to this function that include a given [`SurfaceTexture`][st] + /// in `surface_textures` must use the same [`Fence`]. + /// + /// - The [`Fence`] passed as `signal_fence.0` must remain alive until + /// all submissions that will signal it have completed. + /// + /// [`Fence`]: Api::Fence + /// [cb]: Api::CommandBuffer + /// [ce]: Api::CommandEncoder + /// [d]: Api::Device + /// [t]: Api::Texture + /// [bg]: Api::BindGroup + /// [rp]: Api::RenderPipeline + /// [st]: Api::SurfaceTexture + unsafe fn submit( + &self, + command_buffers: &[&<Self::A as Api>::CommandBuffer], + surface_textures: &[&<Self::A as Api>::SurfaceTexture], + signal_fence: (&<Self::A as Api>::Fence, FenceValue), + ) -> Result<(), DeviceError>; + /// Present a surface texture to the screen. + /// + /// This consumes the surface texture, returning it to the swapchain. + /// + /// # Safety + /// + /// - `texture` must have been acquired from `surface` via + /// [`Surface::acquire_texture`] and not yet presented or discarded. + /// - `surface` must be configured for use with the [`Device`][d] associated + /// with this [`Queue`]. + /// - `texture` must be in the "present" state. Either: + /// - It was passed in [`submit`][s]'s `surface_textures` argument + /// (which transitions it to the present state), or + /// - The caller has otherwise transitioned it (e.g. via a clear + + /// barrier to `PRESENT` for textures that were never rendered to). + /// - Any command buffers that write to `texture` must have been submitted + /// via [`submit`][s] before this call. The submissions do not need to + /// have completed on the GPU; platform-level synchronization handles the + /// ordering between rendering and display. + /// - Must be externally synchronized with all other queue operations + /// ([`submit`][s], [`present`][Queue::present], + /// [`wait_for_idle`][Queue::wait_for_idle]) on the same queue. + /// + /// [d]: Api::Device + /// [s]: Queue::submit + unsafe fn present( + &self, + surface: &<Self::A as Api>::Surface, + texture: <Self::A as Api>::SurfaceTexture, + ) -> Result<(), SurfaceError>; + /// Block until all previously submitted work on this queue has completed, + /// including any pending presentations. + /// + /// # Safety + /// + /// - Must be externally synchronized with all other queue operations + /// ([`submit`][Queue::submit], [`present`][Queue::present], + /// [`wait_for_idle`][Queue::wait_for_idle]) on the same queue. + unsafe fn wait_for_idle(&self) -> Result<(), DeviceError>; + unsafe fn get_timestamp_period(&self) -> f32; +} + +/// Encoder and allocation pool for `CommandBuffer`s. +/// +/// A `CommandEncoder` not only constructs `CommandBuffer`s but also +/// acts as the allocation pool that owns the buffers' underlying +/// storage. Thus, `CommandBuffer`s must not outlive the +/// `CommandEncoder` that created them. +/// +/// The life cycle of a `CommandBuffer` is as follows: +/// +/// - Call [`Device::create_command_encoder`] to create a new +/// `CommandEncoder`, in the "closed" state. +/// +/// - Call `begin_encoding` on a closed `CommandEncoder` to begin +/// recording commands. This puts the `CommandEncoder` in the +/// "recording" state. +/// +/// - Call methods like `copy_buffer_to_buffer`, `begin_render_pass`, +/// etc. on a "recording" `CommandEncoder` to add commands to the +/// list. (If an error occurs, you must call `discard_encoding`; see +/// below.) +/// +/// - Call `end_encoding` on a recording `CommandEncoder` to close the +/// encoder and construct a fresh `CommandBuffer` consisting of the +/// list of commands recorded up to that point. +/// +/// - Call `discard_encoding` on a recording `CommandEncoder` to drop +/// the commands recorded thus far and close the encoder. This is +/// the only safe thing to do on a `CommandEncoder` if an error has +/// occurred while recording commands. +/// +/// - Call `reset_all` on a closed `CommandEncoder`, passing all the +/// live `CommandBuffers` built from it. All the `CommandBuffer`s +/// are destroyed, and their resources are freed. +/// +/// # Safety +/// +/// - The `CommandEncoder` must be in the states described above to +/// make the given calls. +/// +/// - A `CommandBuffer` that has been submitted for execution on the +/// GPU must live until its execution is complete. +/// +/// - A `CommandBuffer` must not outlive the `CommandEncoder` that +/// built it. +/// +/// It is the user's responsibility to meet this requirements. This +/// allows `CommandEncoder` implementations to keep their state +/// tracking to a minimum. +pub trait CommandEncoder: WasmNotSendSync + fmt::Debug { + type A: Api; + + /// Begin encoding a new command buffer. + /// + /// This puts this `CommandEncoder` in the "recording" state. + /// + /// # Safety + /// + /// This `CommandEncoder` must be in the "closed" state. + unsafe fn begin_encoding(&mut self, label: Label) -> Result<(), DeviceError>; + + /// Discard the command list under construction. + /// + /// If an error has occurred while recording commands, this + /// is the only safe thing to do with the encoder. + /// + /// This puts this `CommandEncoder` in the "closed" state. + /// + /// # Safety + /// + /// This `CommandEncoder` must be in the "recording" state. + /// + /// Callers must not assume that implementations of this + /// function are idempotent, and thus should not call it + /// multiple times in a row. + unsafe fn discard_encoding(&mut self); + + /// Return a fresh [`CommandBuffer`] holding the recorded commands. + /// + /// The returned [`CommandBuffer`] holds all the commands recorded + /// on this `CommandEncoder` since the last call to + /// [`begin_encoding`]. + /// + /// This puts this `CommandEncoder` in the "closed" state. + /// + /// # Safety + /// + /// This `CommandEncoder` must be in the "recording" state. + /// + /// The returned [`CommandBuffer`] must not outlive this + /// `CommandEncoder`. Implementations are allowed to build + /// `CommandBuffer`s that depend on storage owned by this + /// `CommandEncoder`. + /// + /// [`CommandBuffer`]: Api::CommandBuffer + /// [`begin_encoding`]: CommandEncoder::begin_encoding + unsafe fn end_encoding(&mut self) -> Result<<Self::A as Api>::CommandBuffer, DeviceError>; + + /// Reclaim all resources belonging to this `CommandEncoder`. + /// + /// # Safety + /// + /// This `CommandEncoder` must be in the "closed" state. + /// + /// The `command_buffers` iterator must produce all the live + /// [`CommandBuffer`]s built using this `CommandEncoder` --- that + /// is, every extant `CommandBuffer` returned from `end_encoding`. + /// + /// [`CommandBuffer`]: Api::CommandBuffer + unsafe fn reset_all<I>(&mut self, command_buffers: I) + where + I: Iterator<Item = <Self::A as Api>::CommandBuffer>; + + unsafe fn transition_buffers<'a, T>(&mut self, barriers: T) + where + T: Iterator<Item = BufferBarrier<'a, <Self::A as Api>::Buffer>>; + + unsafe fn transition_textures<'a, T>(&mut self, barriers: T) + where + T: Iterator<Item = TextureBarrier<'a, <Self::A as Api>::Texture>>; + + // copy operations + + unsafe fn clear_buffer(&mut self, buffer: &<Self::A as Api>::Buffer, range: MemoryRange); + + unsafe fn copy_buffer_to_buffer<T>( + &mut self, + src: &<Self::A as Api>::Buffer, + dst: &<Self::A as Api>::Buffer, + regions: T, + ) where + T: Iterator<Item = BufferCopy>; + + /// Copy from an external image to an internal texture. + /// Works with a single array layer. + /// Note: `dst` current usage has to be `wgt::TextureUses::COPY_DST`. + /// Note: the copy extent is in physical size (rounded to the block size) + #[cfg(webgl)] + unsafe fn copy_external_image_to_texture<T>( + &mut self, + src: &wgt::CopyExternalImageSourceInfo, + dst: &<Self::A as Api>::Texture, + dst_premultiplication: bool, + regions: T, + ) where + T: Iterator<Item = TextureCopy>; + + /// Copy from one texture to another. + /// Works with a single array layer. + /// Note: `dst` current usage has to be `wgt::TextureUses::COPY_DST`. + /// Note: the copy extent is in physical size (rounded to the block size) + unsafe fn copy_texture_to_texture<T>( + &mut self, + src: &<Self::A as Api>::Texture, + src_usage: wgt::TextureUses, + dst: &<Self::A as Api>::Texture, + regions: T, + ) where + T: Iterator<Item = TextureCopy>; + + /// Copy from buffer to texture. + /// Works with a single array layer. + /// Note: `dst` current usage has to be `wgt::TextureUses::COPY_DST`. + /// Note: the copy extent is in physical size (rounded to the block size) + unsafe fn copy_buffer_to_texture<T>( + &mut self, + src: &<Self::A as Api>::Buffer, + dst: &<Self::A as Api>::Texture, + regions: T, + ) where + T: Iterator<Item = BufferTextureCopy>; + + /// Copy from texture to buffer. + /// Works with a single array layer. + /// Note: the copy extent is in physical size (rounded to the block size) + unsafe fn copy_texture_to_buffer<T>( + &mut self, + src: &<Self::A as Api>::Texture, + src_usage: wgt::TextureUses, + dst: &<Self::A as Api>::Buffer, + regions: T, + ) where + T: Iterator<Item = BufferTextureCopy>; + + unsafe fn copy_acceleration_structure_to_acceleration_structure( + &mut self, + src: &<Self::A as Api>::AccelerationStructure, + dst: &<Self::A as Api>::AccelerationStructure, + copy: wgt::AccelerationStructureCopy, + ); + // pass common + + /// Sets the bind group at `index` to `group`. + /// + /// If this is not the first call to `set_bind_group` within the current + /// render or compute pass: + /// + /// - If `layout` contains `n` bind group layouts, then any previously set + /// bind groups at indices `n` or higher are cleared. + /// + /// - If the first `m` bind group layouts of `layout` are equal to those of + /// the previously passed layout, but no more, then any previously set + /// bind groups at indices `m` or higher are cleared. + /// + /// It follows from the above that passing the same layout as before doesn't + /// clear any bind groups. + /// + /// # Safety + /// + /// - This [`CommandEncoder`] must be within a render or compute pass. + /// + /// - `index` must be the valid index of some bind group layout in `layout`. + /// Call this the "relevant bind group layout". + /// + /// - The layout of `group` must be equal to the relevant bind group layout. + /// + /// - The length of `dynamic_offsets` must match the number of buffer + /// bindings [with dynamic offsets][hdo] in the relevant bind group + /// layout. + /// + /// - If those buffer bindings are ordered by increasing [`binding` number] + /// and paired with elements from `dynamic_offsets`, then each offset must + /// be a valid offset for the binding's corresponding buffer in `group`. + /// + /// [hdo]: wgt::BindingType::Buffer::has_dynamic_offset + /// [`binding` number]: wgt::BindGroupLayoutEntry::binding + unsafe fn set_bind_group( + &mut self, + layout: &<Self::A as Api>::PipelineLayout, + index: u32, + group: &<Self::A as Api>::BindGroup, + dynamic_offsets: &[wgt::DynamicOffset], + ); + + /// Sets a range in immediate data. + /// + /// IMPORTANT: while the data is passed as words, the offset is in bytes! + /// + /// # Safety + /// + /// - `offset_bytes` must be a multiple of 4. + /// - The range of immediates written must be valid for the pipeline layout at draw time. + unsafe fn set_immediates( + &mut self, + layout: &<Self::A as Api>::PipelineLayout, + offset_bytes: u32, + data: &[u32], + ); + + unsafe fn insert_debug_marker(&mut self, label: &str); + unsafe fn begin_debug_marker(&mut self, group_label: &str); + unsafe fn end_debug_marker(&mut self); + + // queries + + /// # Safety: + /// + /// - If `set` is an occlusion query set, it must be the same one as used in the [`RenderPassDescriptor::occlusion_query_set`] parameter. + unsafe fn begin_query(&mut self, set: &<Self::A as Api>::QuerySet, index: u32); + /// # Safety: + /// + /// - If `set` is an occlusion query set, it must be the same one as used in the [`RenderPassDescriptor::occlusion_query_set`] parameter. + unsafe fn end_query(&mut self, set: &<Self::A as Api>::QuerySet, index: u32); + unsafe fn write_timestamp(&mut self, set: &<Self::A as Api>::QuerySet, index: u32); + unsafe fn reset_queries(&mut self, set: &<Self::A as Api>::QuerySet, range: Range<u32>); + unsafe fn copy_query_results( + &mut self, + set: &<Self::A as Api>::QuerySet, + range: Range<u32>, + buffer: &<Self::A as Api>::Buffer, + offset: wgt::BufferAddress, + stride: wgt::BufferSize, + ); + + // render passes + + /// Begin a new render pass, clearing all active bindings. + /// + /// This clears any bindings established by the following calls: + /// + /// - [`set_bind_group`](CommandEncoder::set_bind_group) + /// - [`set_immediates`](CommandEncoder::set_immediates) + /// - [`begin_query`](CommandEncoder::begin_query) + /// - [`set_render_pipeline`](CommandEncoder::set_render_pipeline) + /// - [`set_index_buffer`](CommandEncoder::set_index_buffer) + /// - [`set_vertex_buffer`](CommandEncoder::set_vertex_buffer) + /// + /// # Safety + /// + /// - All prior calls to [`begin_render_pass`] on this [`CommandEncoder`] must have been followed + /// by a call to [`end_render_pass`]. + /// + /// - All prior calls to [`begin_compute_pass`] on this [`CommandEncoder`] must have been followed + /// by a call to [`end_compute_pass`]. + /// + /// - All prior calls to [`begin_ray_tracing_pass`] on this [`CommandEncoder`] must have been followed + /// by a call to [`end_ray_tracing_pass`]. + /// + /// [`begin_render_pass`]: CommandEncoder::begin_render_pass + /// [`begin_compute_pass`]: CommandEncoder::begin_compute_pass + /// [`begin_ray_tracing_pass`]: CommandEncoder::begin_ray_tracing_pass + /// [`end_render_pass`]: CommandEncoder::end_render_pass + /// [`end_compute_pass`]: CommandEncoder::end_compute_pass + /// [`end_ray_tracing_pass`]: CommandEncoder::end_ray_tracing_pass + unsafe fn begin_render_pass( + &mut self, + desc: &RenderPassDescriptor<<Self::A as Api>::QuerySet, <Self::A as Api>::TextureView>, + ) -> Result<(), DeviceError>; + + /// End the current render pass. + /// + /// # Safety + /// + /// - There must have been a prior call to [`begin_render_pass`] on this [`CommandEncoder`] + /// that has not been followed by a call to [`end_render_pass`]. + /// + /// [`begin_render_pass`]: CommandEncoder::begin_render_pass + /// [`end_render_pass`]: CommandEncoder::end_render_pass + unsafe fn end_render_pass(&mut self); + + unsafe fn set_render_pipeline(&mut self, pipeline: &<Self::A as Api>::RenderPipeline); + + unsafe fn set_index_buffer<'a>( + &mut self, + binding: BufferBinding<'a, <Self::A as Api>::Buffer>, + format: wgt::IndexFormat, + ); + unsafe fn set_vertex_buffer<'a>( + &mut self, + index: u32, + binding: BufferBinding<'a, <Self::A as Api>::Buffer>, + ); + unsafe fn set_viewport(&mut self, rect: &Rect<f32>, depth_range: Range<f32>); + unsafe fn set_scissor_rect(&mut self, rect: &Rect<u32>); + unsafe fn set_stencil_reference(&mut self, value: u32); + unsafe fn set_blend_constants(&mut self, color: &[f32; 4]); + + unsafe fn draw( + &mut self, + first_vertex: u32, + vertex_count: u32, + first_instance: u32, + instance_count: u32, + ); + unsafe fn draw_indexed( + &mut self, + first_index: u32, + index_count: u32, + base_vertex: i32, + first_instance: u32, + instance_count: u32, + ); + unsafe fn draw_indirect( + &mut self, + buffer: &<Self::A as Api>::Buffer, + offset: wgt::BufferAddress, + draw_count: u32, + ); + unsafe fn draw_indexed_indirect( + &mut self, + buffer: &<Self::A as Api>::Buffer, + offset: wgt::BufferAddress, + draw_count: u32, + ); + unsafe fn draw_indirect_count( + &mut self, + buffer: &<Self::A as Api>::Buffer, + offset: wgt::BufferAddress, + count_buffer: &<Self::A as Api>::Buffer, + count_offset: wgt::BufferAddress, + max_count: u32, + ); + unsafe fn draw_indexed_indirect_count( + &mut self, + buffer: &<Self::A as Api>::Buffer, + offset: wgt::BufferAddress, + count_buffer: &<Self::A as Api>::Buffer, + count_offset: wgt::BufferAddress, + max_count: u32, + ); + unsafe fn draw_mesh_tasks( + &mut self, + group_count_x: u32, + group_count_y: u32, + group_count_z: u32, + ); + unsafe fn draw_mesh_tasks_indirect( + &mut self, + buffer: &<Self::A as Api>::Buffer, + offset: wgt::BufferAddress, + draw_count: u32, + ); + unsafe fn draw_mesh_tasks_indirect_count( + &mut self, + buffer: &<Self::A as Api>::Buffer, + offset: wgt::BufferAddress, + count_buffer: &<Self::A as Api>::Buffer, + count_offset: wgt::BufferAddress, + max_count: u32, + ); + + // compute passes + + /// Begin a new compute pass, clearing all active bindings. + /// + /// This clears any bindings established by the following calls: + /// + /// - [`set_bind_group`](CommandEncoder::set_bind_group) + /// - [`set_immediates`](CommandEncoder::set_immediates) + /// - [`begin_query`](CommandEncoder::begin_query) + /// - [`set_compute_pipeline`](CommandEncoder::set_compute_pipeline) + /// + /// # Safety + /// + /// - All prior calls to [`begin_render_pass`] on this [`CommandEncoder`] must have been followed + /// by a call to [`end_render_pass`]. + /// + /// - All prior calls to [`begin_compute_pass`] on this [`CommandEncoder`] must have been followed + /// by a call to [`end_compute_pass`]. + /// + /// - All prior calls to [`begin_ray_tracing_pass`] on this [`CommandEncoder`] must have been followed + /// by a call to [`end_ray_tracing_pass`]. + /// + /// [`begin_render_pass`]: CommandEncoder::begin_render_pass + /// [`begin_compute_pass`]: CommandEncoder::begin_compute_pass + /// [`begin_ray_tracing_pass`]: CommandEncoder::begin_ray_tracing_pass + /// [`end_render_pass`]: CommandEncoder::end_render_pass + /// [`end_compute_pass`]: CommandEncoder::end_compute_pass + /// [`end_ray_tracing_pass`]: CommandEncoder::end_ray_tracing_pass + unsafe fn begin_compute_pass( + &mut self, + desc: &ComputePassDescriptor<<Self::A as Api>::QuerySet>, + ); + + /// End the current compute pass. + /// + /// # Safety + /// + /// - There must have been a prior call to [`begin_compute_pass`] on this [`CommandEncoder`] + /// that has not been followed by a call to [`end_compute_pass`]. + /// + /// [`begin_compute_pass`]: CommandEncoder::begin_compute_pass + /// [`end_compute_pass`]: CommandEncoder::end_compute_pass + unsafe fn end_compute_pass(&mut self); + + unsafe fn set_compute_pipeline(&mut self, pipeline: &<Self::A as Api>::ComputePipeline); + + unsafe fn dispatch_workgroups(&mut self, count: [u32; 3]); + unsafe fn dispatch_workgroups_indirect( + &mut self, + buffer: &<Self::A as Api>::Buffer, + offset: wgt::BufferAddress, + ); + + /// Begin a new ray tracing pass, clearing all active bindings. + /// + /// This clears any bindings established by the following calls: + /// + /// - [`set_bind_group`](CommandEncoder::set_bind_group) + /// - [`set_immediates`](CommandEncoder::set_immediates) + /// - [`begin_query`](CommandEncoder::begin_query) + /// - [`set_ray_tracing_pipeline`](CommandEncoder::set_compute_pipeline) + /// + /// # Safety + /// + /// - All prior calls to [`begin_render_pass`] on this [`CommandEncoder`] must have been followed + /// by a call to [`end_render_pass`]. + /// + /// - All prior calls to [`begin_compute_pass`] on this [`CommandEncoder`] must have been followed + /// by a call to [`end_compute_pass`]. + /// + /// - All prior calls to [`begin_ray_tracing_pass`] on this [`CommandEncoder`] must have been followed + /// by a call to [`end_ray_tracing_pass`]. + /// + /// [`begin_render_pass`]: CommandEncoder::begin_render_pass + /// [`begin_compute_pass`]: CommandEncoder::begin_compute_pass + /// [`begin_ray_tracing_pass`]: CommandEncoder::begin_ray_tracing_pass + /// [`end_render_pass`]: CommandEncoder::end_render_pass + /// [`end_compute_pass`]: CommandEncoder::end_compute_pass + /// [`end_ray_tracing_pass`]: CommandEncoder::end_ray_tracing_pass + unsafe fn begin_ray_tracing_pass(&mut self, desc: &RayTracingPassDescriptor); + + /// End the current compute pass. + /// + /// # Safety + /// + /// - There must have been a prior call to [`begin_ray_tracing_pass`] on this [`CommandEncoder`] + /// that has not been followed by a call to [`end_ray_tracing_pass`]. + /// + /// [`begin_ray_tracing_pass`]: CommandEncoder::begin_ray_tracing_pass + /// [`end_ray_tracing_pass`]: CommandEncoder::end_ray_tracing_pass + unsafe fn end_ray_tracing_pass(&mut self); + + /// # Safety + /// + /// - Pipeline must not be destroyed + unsafe fn set_ray_tracing_pipeline(&mut self, pipeline: &<Self::A as Api>::RayTracingPipeline); + + unsafe fn trace_rays<'a>( + &mut self, + count: [u32; 3], + ray_generation_group_data: PipelineGroupData<'a, <Self::A as Api>::Buffer>, + miss_group_data: PipelineGroupData<'a, <Self::A as Api>::Buffer>, + intersection_group_data: PipelineGroupData<'a, <Self::A as Api>::Buffer>, + ); + + /// To get the required sizes for the buffer allocations use `get_acceleration_structure_build_sizes` per descriptor + /// All buffers must be synchronized externally + /// All buffer regions, which are written to may only be passed once per function call, + /// with the exception of updates in the same descriptor. + /// Consequences of this limitation: + /// - scratch buffers need to be unique + /// - a tlas can't be build in the same call with a blas it contains + unsafe fn build_acceleration_structures<'a, T>( + &mut self, + descriptor_count: u32, + descriptors: T, + ) where + Self::A: 'a, + T: IntoIterator< + Item = BuildAccelerationStructureDescriptor< + 'a, + <Self::A as Api>::Buffer, + <Self::A as Api>::AccelerationStructure, + >, + >; + unsafe fn place_acceleration_structure_barrier( + &mut self, + barrier: AccelerationStructureBarrier, + ); + // modeled off dx12, because this is able to be polyfilled in vulkan as opposed to the other way round + unsafe fn read_acceleration_structure_compact_size( + &mut self, + acceleration_structure: &<Self::A as Api>::AccelerationStructure, + buf: &<Self::A as Api>::Buffer, + ); + unsafe fn set_acceleration_structure_dependencies( + command_buffers: &[&<Self::A as Api>::CommandBuffer], + dependencies: &[&<Self::A as Api>::AccelerationStructure], + ); +} + +bitflags!( + /// Pipeline layout creation flags. + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] + pub struct PipelineLayoutFlags: u32 { + /// D3D12: Add support for `first_vertex` and `first_instance` builtins + /// via immediates for direct execution. + const FIRST_VERTEX_INSTANCE = 1 << 0; + /// D3D12: Add support for `num_workgroups` builtins via immediates + /// for direct execution. + const NUM_WORK_GROUPS = 1 << 1; + /// D3D12: Add support for the builtins that the other flags enable for + /// indirect execution. + const INDIRECT_BUILTIN_UPDATE = 1 << 2; + } +); + +bitflags!( + /// Pipeline layout creation flags. + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] + pub struct BindGroupLayoutFlags: u32 { + /// Allows for bind group binding arrays to be shorter than the array in the BGL. + const PARTIALLY_BOUND = 1 << 0; + } +); + +bitflags!( + /// Texture format capability flags. + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] + pub struct TextureFormatCapabilities: u32 { + /// Format can be sampled. + const SAMPLED = 1 << 0; + /// Format can be sampled with a linear sampler. + const SAMPLED_LINEAR = 1 << 1; + /// Format can be sampled with a min/max reduction sampler. + const SAMPLED_MINMAX = 1 << 2; + + /// Format can be used as storage with read-only access. + const STORAGE_READ_ONLY = 1 << 3; + /// Format can be used as storage with write-only access. + const STORAGE_WRITE_ONLY = 1 << 4; + /// Format can be used as storage with both read and write access. + const STORAGE_READ_WRITE = 1 << 5; + /// Format can be used as storage with atomics. + const STORAGE_ATOMIC = 1 << 6; + + /// Format can be used as color and input attachment. + const COLOR_ATTACHMENT = 1 << 7; + /// Format can be used as color (with blending) and input attachment. + const COLOR_ATTACHMENT_BLEND = 1 << 8; + /// Format can be used as depth-stencil and input attachment. + const DEPTH_STENCIL_ATTACHMENT = 1 << 9; + + /// Format can be multisampled by x2. + const MULTISAMPLE_X2 = 1 << 10; + /// Format can be multisampled by x4. + const MULTISAMPLE_X4 = 1 << 11; + /// Format can be multisampled by x8. + const MULTISAMPLE_X8 = 1 << 12; + /// Format can be multisampled by x16. + const MULTISAMPLE_X16 = 1 << 13; + + /// Format can be used for render pass resolve targets. + const MULTISAMPLE_RESOLVE = 1 << 14; + + /// Format can be copied from. + const COPY_SRC = 1 << 15; + /// Format can be copied to. + const COPY_DST = 1 << 16; + } +); + +bitflags!( + /// Texture format capability flags. + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] + pub struct FormatAspects: u8 { + const COLOR = 1 << 0; + const DEPTH = 1 << 1; + const STENCIL = 1 << 2; + const PLANE_0 = 1 << 3; + const PLANE_1 = 1 << 4; + const PLANE_2 = 1 << 5; + + const DEPTH_STENCIL = Self::DEPTH.bits() | Self::STENCIL.bits(); + } +); + +impl FormatAspects { + pub fn new(format: wgt::TextureFormat, aspect: wgt::TextureAspect) -> Self { + let aspect_mask = match aspect { + wgt::TextureAspect::All => Self::all(), + wgt::TextureAspect::DepthOnly => Self::DEPTH, + wgt::TextureAspect::StencilOnly => Self::STENCIL, + wgt::TextureAspect::Plane0 => Self::PLANE_0, + wgt::TextureAspect::Plane1 => Self::PLANE_1, + wgt::TextureAspect::Plane2 => Self::PLANE_2, + }; + Self::from(format) & aspect_mask + } + + /// Returns `true` if only one flag is set + pub fn is_one(&self) -> bool { + self.bits().is_power_of_two() + } + + pub fn map(&self) -> wgt::TextureAspect { + match *self { + Self::COLOR => wgt::TextureAspect::All, + Self::DEPTH => wgt::TextureAspect::DepthOnly, + Self::STENCIL => wgt::TextureAspect::StencilOnly, + Self::PLANE_0 => wgt::TextureAspect::Plane0, + Self::PLANE_1 => wgt::TextureAspect::Plane1, + Self::PLANE_2 => wgt::TextureAspect::Plane2, + _ => unreachable!(), + } + } +} + +impl From<wgt::TextureFormat> for FormatAspects { + fn from(format: wgt::TextureFormat) -> Self { + match format { + wgt::TextureFormat::Stencil8 => Self::STENCIL, + wgt::TextureFormat::Depth16Unorm + | wgt::TextureFormat::Depth32Float + | wgt::TextureFormat::Depth24Plus => Self::DEPTH, + wgt::TextureFormat::Depth32FloatStencil8 | wgt::TextureFormat::Depth24PlusStencil8 => { + Self::DEPTH_STENCIL + } + wgt::TextureFormat::NV12 | wgt::TextureFormat::P010 => Self::PLANE_0 | Self::PLANE_1, + _ => Self::COLOR, + } + } +} + +bitflags!( + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] + pub struct MemoryFlags: u32 { + const TRANSIENT = 1 << 0; + const PREFER_COHERENT = 1 << 1; + } +); + +bitflags!( + /// Attachment load and store operations. + /// + /// There must be at least one flag from the LOAD group and one from the STORE group set. + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] + pub struct AttachmentOps: u8 { + /// Load the existing contents of the attachment. + const LOAD = 1 << 0; + /// Clear the attachment to a specified value. + const LOAD_CLEAR = 1 << 1; + /// The contents of the attachment are undefined. + const LOAD_DONT_CARE = 1 << 2; + /// Store the contents of the attachment. + const STORE = 1 << 3; + /// The contents of the attachment are undefined after the pass. + const STORE_DISCARD = 1 << 4; + } +); + +#[derive(Debug)] +pub struct InstanceDescriptor<'a> { + pub name: &'a str, + pub flags: wgt::InstanceFlags, + pub memory_budget_thresholds: wgt::MemoryBudgetThresholds, + pub backend_options: wgt::BackendOptions, + pub telemetry: Option<Telemetry>, + /// This is a borrow because the surrounding `core::Instance` keeps the owned display handle + /// alive already. + pub display: Option<DisplayHandle<'a>>, +} + +#[derive(Clone, Debug)] +pub struct Alignments { + /// The alignment of the start of the buffer used as a GPU copy source. + pub buffer_copy_offset: wgt::BufferSize, + + /// The alignment of the row pitch of the texture data stored in a buffer that is + /// used in a GPU copy operation. + pub buffer_copy_pitch: wgt::BufferSize, + + /// The finest alignment of bound range checking for uniform buffers. + /// + /// When `wgpu_hal` restricts shader references to the [accessible + /// region][ar] of a [`Uniform`] buffer, the size of the accessible region + /// is the bind group binding's stated [size], rounded up to the next + /// multiple of this value. + /// + /// We don't need an analogous field for storage buffer bindings, because + /// all our backends promise to enforce the size at least to a four-byte + /// alignment, and `wgpu_hal` requires bound range lengths to be a multiple + /// of four anyway. + /// + /// [ar]: struct.BufferBinding.html#accessible-region + /// [`Uniform`]: wgt::BufferBindingType::Uniform + /// [size]: BufferBinding::size + pub uniform_bounds_check_alignment: wgt::BufferSize, + + /// The size of the raw TLAS instance + pub raw_tlas_instance_size: u32, + + /// What the scratch buffer for building an acceleration structure must be aligned to + pub ray_tracing_scratch_buffer_alignment: u32, + + /// How large a single piece of group data is. That is, how large the vector returned + /// from `device.get_raytracing_pipeline_group_data(&pipeline, n..(n+1))` is. + /// + /// If ray tracing pipelines are implemented, this must be non zero. + pub ray_tracing_pipeline_group_data_size: u32, + + /// If ray tracing pipelines are implemented, this must be a power of two (and non zero). + pub ray_tracing_pipeline_group_data_alignment: u32, + + /// If ray tracing pipelines are implemented, this must be a power of two (and non zero). + /// + /// The offset within `PipelineGroupData` must be a multiple of this + pub ray_tracing_pipeline_data_offset_alignment: u32, +} + +#[derive(Clone, Debug)] +pub struct Capabilities { + pub limits: wgt::Limits, + pub alignments: Alignments, + pub downlevel: wgt::DownlevelCapabilities, + /// Supported cooperative matrix configurations. + /// + /// Empty if cooperative matrices are not supported. + pub cooperative_matrix_properties: Vec<wgt::CooperativeMatrixProperties>, +} + +/// An adapter with all the information needed to reason about its capabilities. +/// +/// These are either made by [`Instance::enumerate_adapters`] or by backend specific +/// methods on the backend [`Instance`] or [`Adapter`]. +#[derive(Debug)] +pub struct ExposedAdapter<A: Api> { + pub adapter: A::Adapter, + pub info: wgt::AdapterInfo, + pub features: wgt::Features, + pub capabilities: Capabilities, +} + +/// Describes information about what a `Surface`'s presentation capabilities are. +/// Fetch this with [Adapter::surface_capabilities]. +#[derive(Debug, Clone)] +pub struct SurfaceCapabilities { + /// List of supported texture formats together with the color spaces + /// supported for each format. + /// + /// Must be at least one. At most one entry per format, each with a + /// non-empty set of color spaces. + pub formats: Vec<wgt::SurfaceFormatCapabilities>, + + /// Range for the number of queued frames. + /// + /// This adjusts either the swapchain frame count to value + 1 - or sets SetMaximumFrameLatency to the value given, + /// or uses a wait-for-present in the acquire method to limit rendering such that it acts like it's a value + 1 swapchain frame set. + /// + /// - `maximum_frame_latency.start` must be at least 1. + /// - `maximum_frame_latency.end` must be larger or equal to `maximum_frame_latency.start`. + pub maximum_frame_latency: RangeInclusive<u32>, + + /// Current extent of the surface, if known. + pub current_extent: Option<wgt::Extent3d>, + + /// Supported texture usage flags. + /// + /// Must have at least `wgt::TextureUses::COLOR_TARGET` + pub usage: wgt::TextureUses, + + /// List of supported V-sync modes. + /// + /// Must be at least one. + pub present_modes: Vec<wgt::PresentMode>, + + /// List of supported alpha composition modes. + /// + /// Must be at least one. + pub composite_alpha_modes: Vec<wgt::CompositeAlphaMode>, +} + +impl SurfaceCapabilities { + /// Returns the supported texture formats, dropping the per-format color-space + /// information carried in [`Self::formats`]. + pub fn texture_formats(&self) -> impl Iterator<Item = wgt::TextureFormat> + '_ { + self.formats.iter().map(|fc| fc.format) + } +} + +#[derive(Debug)] +pub struct AcquiredSurfaceTexture<A: Api> { + pub texture: A::SurfaceTexture, + /// The presentation configuration no longer matches + /// the surface properties exactly, but can still be used to present + /// to the surface successfully. + pub suboptimal: bool, +} + +/// An open connection to a device and a queue. +/// +/// This can be created from [`Adapter::open`] or backend +/// specific methods on the backend's [`Instance`] or [`Adapter`]. +#[derive(Debug)] +pub struct OpenDevice<A: Api> { + pub device: A::Device, + pub queue: A::Queue, +} + +#[derive(Clone, Debug)] +pub struct BufferMapping { + pub ptr: NonNull<u8>, + pub is_coherent: bool, +} + +#[derive(Clone, Debug)] +pub struct BufferDescriptor<'a> { + pub label: Label<'a>, + pub size: wgt::BufferAddress, + pub usage: wgt::BufferUses, + pub memory_flags: MemoryFlags, +} + +#[derive(Clone, Debug)] +pub struct TextureDescriptor<'a> { + pub label: Label<'a>, + pub size: wgt::Extent3d, + pub mip_level_count: u32, + pub sample_count: u32, + pub dimension: wgt::TextureDimension, + pub format: wgt::TextureFormat, + pub usage: wgt::TextureUses, + pub memory_flags: MemoryFlags, + /// Allows views of this texture to have a different format + /// than the texture does. + pub view_formats: Vec<wgt::TextureFormat>, +} + +impl TextureDescriptor<'_> { + pub fn copy_extent(&self) -> CopyExtent { + CopyExtent::map_extent_to_copy_size(&self.size, self.dimension) + } + + pub fn is_cube_compatible(&self) -> bool { + self.dimension == wgt::TextureDimension::D2 + && self.size.depth_or_array_layers.is_multiple_of(6) + && self.sample_count == 1 + && self.size.width == self.size.height + } + + pub fn array_layer_count(&self) -> u32 { + match self.dimension { + wgt::TextureDimension::D1 | wgt::TextureDimension::D3 => 1, + wgt::TextureDimension::D2 => self.size.depth_or_array_layers, + } + } +} + +/// TextureView descriptor. +/// +/// Valid usage: +///. - `format` has to be the same as `TextureDescriptor::format` +///. - `dimension` has to be compatible with `TextureDescriptor::dimension` +///. - `usage` has to be a subset of `TextureDescriptor::usage` +///. - `range` has to be a subset of parent texture +#[derive(Clone, Debug)] +pub struct TextureViewDescriptor<'a> { + pub label: Label<'a>, + pub format: wgt::TextureFormat, + pub dimension: wgt::TextureViewDimension, + pub usage: wgt::TextureUses, + pub range: wgt::ImageSubresourceRange, +} + +#[derive(Clone, Debug)] +pub struct SamplerDescriptor<'a> { + pub label: Label<'a>, + pub address_modes: [wgt::AddressMode; 3], + pub mag_filter: wgt::FilterMode, + pub min_filter: wgt::FilterMode, + pub mipmap_filter: wgt::MipmapFilterMode, + pub lod_clamp: Range<f32>, + pub compare: Option<wgt::CompareFunction>, + // Must in the range [1, 16]. + // + // Anisotropic filtering must be supported if this is not 1. + pub anisotropy_clamp: u16, + pub border_color: Option<wgt::SamplerBorderColor>, +} + +/// BindGroupLayout descriptor. +/// +/// Valid usage: +/// - `entries` are sorted by ascending `wgt::BindGroupLayoutEntry::binding` +#[derive(Clone, Debug)] +pub struct BindGroupLayoutDescriptor<'a> { + pub label: Label<'a>, + pub flags: BindGroupLayoutFlags, + pub entries: &'a [wgt::BindGroupLayoutEntry], +} + +#[derive(Clone, Debug)] +pub struct PipelineLayoutDescriptor<'a, B: DynBindGroupLayout + ?Sized> { + pub label: Label<'a>, + pub flags: PipelineLayoutFlags, + pub bind_group_layouts: &'a [Option<&'a B>], + pub immediate_size: u32, +} + +/// A region of a buffer made visible to shaders via a [`BindGroup`]. +/// +/// [`BindGroup`]: Api::BindGroup +/// +/// ## Construction +/// +/// The recommended way to construct a `BufferBinding` is using the `binding` +/// method on a wgpu-core `Buffer`, which will validate the binding size +/// against the buffer size. A `new_unchecked` constructor is also provided for +/// cases where direct construction is necessary. +/// +/// ## Accessible region +/// +/// `wgpu_hal` guarantees that shaders compiled with +/// [`ShaderModuleDescriptor::runtime_checks`] set to `true` cannot read or +/// write data via this binding outside the *accessible region* of a buffer: +/// +/// - The accessible region starts at [`offset`]. +/// +/// - For [`Storage`] bindings, the size of the accessible region is [`size`], +/// which must be a multiple of 4. +/// +/// - For [`Uniform`] bindings, the size of the accessible region is [`size`] +/// rounded up to the next multiple of +/// [`Alignments::uniform_bounds_check_alignment`]. +/// +/// Note that this guarantee is stricter than WGSL's requirements for +/// [out-of-bounds accesses][woob], as WGSL allows them to return values from +/// elsewhere in the buffer. But this guarantee is necessary anyway, to permit +/// `wgpu-core` to avoid clearing uninitialized regions of buffers that will +/// never be read by the application before they are overwritten. This +/// optimization consults bind group buffer binding regions to determine which +/// parts of which buffers shaders might observe. This optimization is only +/// sound if shader access is bounds-checked. +/// +/// ## Zero-length bindings +/// +/// Some back ends cannot tolerate zero-length regions; for example, see +/// [VUID-VkDescriptorBufferInfo-offset-00340][340] and +/// [VUID-VkDescriptorBufferInfo-range-00341][341], or the +/// documentation for GLES's [glBindBufferRange][bbr]. This documentation +/// previously stated that a `BufferBinding` must have `offset` strictly less +/// than the size of the buffer, but this restriction was not honored elsewhere +/// in the code, so has been removed. However, it remains the case that +/// some backends do not support zero-length bindings, so additional +/// logic is needed somewhere to handle this properly. See +/// [#3170](https://github.com/gfx-rs/wgpu/issues/3170). +/// +/// [`offset`]: BufferBinding::offset +/// [`size`]: BufferBinding::size +/// [`Storage`]: wgt::BufferBindingType::Storage +/// [`Uniform`]: wgt::BufferBindingType::Uniform +/// [340]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#VUID-VkDescriptorBufferInfo-offset-00340 +/// [341]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#VUID-VkDescriptorBufferInfo-range-00341 +/// [bbr]: https://registry.khronos.org/OpenGL-Refpages/es3.0/html/glBindBufferRange.xhtml +/// [woob]: https://gpuweb.github.io/gpuweb/wgsl/#out-of-bounds-access-sec +#[derive(Debug)] +pub struct BufferBinding<'a, B: DynBuffer + ?Sized> { + /// The buffer being bound. + /// + /// This is not fully `pub` to prevent direct construction of + /// `BufferBinding`s, while still allowing public read access to the `offset` + /// and `size` properties. + pub(crate) buffer: &'a B, + + /// The offset at which the bound region starts. + /// + /// This must be less or equal to the size of the buffer. + pub offset: wgt::BufferAddress, + + /// The size of the region bound, in bytes. + /// + /// If `None`, the region extends from `offset` to the end of the + /// buffer. Given the restrictions on `offset`, this means that + /// the size is always greater than zero. + pub size: Option<wgt::BufferSize>, +} + +// We must implement this manually because `B` is not necessarily `Clone`. +impl<B: DynBuffer + ?Sized> Clone for BufferBinding<'_, B> { + fn clone(&self) -> Self { + BufferBinding { + buffer: self.buffer, + offset: self.offset, + size: self.size, + } + } +} + +/// Temporary convenience trait to let us call `.get()` on `u64`s in code that +/// really wants to be using `NonZeroU64`. +/// TODO(<https://github.com/gfx-rs/wgpu/issues/3170>): remove this +pub trait ShouldBeNonZeroExt { + fn get(&self) -> u64; +} + +impl ShouldBeNonZeroExt for NonZeroU64 { + fn get(&self) -> u64 { + NonZeroU64::get(*self) + } +} + +impl ShouldBeNonZeroExt for u64 { + fn get(&self) -> u64 { + *self + } +} + +impl ShouldBeNonZeroExt for Option<NonZeroU64> { + fn get(&self) -> u64 { + match *self { + Some(non_zero) => non_zero.get(), + None => 0, + } + } +} + +impl<'a, B: DynBuffer + ?Sized> BufferBinding<'a, B> { + /// Construct a `BufferBinding` with the given contents. + /// + /// When possible, use the `binding` method on a wgpu-core `Buffer` instead + /// of this method. `Buffer::binding` validates the size of the binding + /// against the size of the buffer. + /// + /// It is more difficult to provide a validating constructor here, due to + /// not having direct access to the size of a `DynBuffer`. + /// + /// SAFETY: The caller is responsible for ensuring that a binding of `size` + /// bytes starting at `offset` is contained within the buffer. + /// + /// The `S` type parameter is a temporary convenience to allow callers to + /// pass a zero size. When the zero-size binding issue is resolved, the + /// argument should just match the type of the member. + /// TODO(<https://github.com/gfx-rs/wgpu/issues/3170>): remove the parameter + pub fn new_unchecked<S: Into<Option<NonZeroU64>>>( + buffer: &'a B, + offset: wgt::BufferAddress, + size: S, + ) -> Self { + Self { + buffer, + offset, + size: size.into(), + } + } +} + +#[derive(Debug)] +pub struct TextureBinding<'a, T: DynTextureView + ?Sized> { + pub view: &'a T, + pub usage: wgt::TextureUses, +} + +impl<'a, T: DynTextureView + ?Sized> Clone for TextureBinding<'a, T> { + fn clone(&self) -> Self { + TextureBinding { + view: self.view, + usage: self.usage, + } + } +} + +#[derive(Debug)] +pub struct ExternalTextureBinding<'a, B: DynBuffer + ?Sized, T: DynTextureView + ?Sized> { + pub planes: [TextureBinding<'a, T>; 3], + pub params: BufferBinding<'a, B>, +} + +impl<'a, B: DynBuffer + ?Sized, T: DynTextureView + ?Sized> Clone + for ExternalTextureBinding<'a, B, T> +{ + fn clone(&self) -> Self { + ExternalTextureBinding { + planes: self.planes.clone(), + params: self.params.clone(), + } + } +} + +/// cbindgen:ignore +#[derive(Clone, Debug)] +pub struct BindGroupEntry { + pub binding: u32, + pub resource_index: u32, + pub count: u32, +} + +/// BindGroup descriptor. +/// +/// Valid usage: +///. - `entries` has to be sorted by ascending `BindGroupEntry::binding` +///. - `entries` has to have the same set of `BindGroupEntry::binding` as `layout` +///. - each entry has to be compatible with the `layout` +///. - each entry's `BindGroupEntry::resource_index` is within range +/// of the corresponding resource array, selected by the relevant +/// `BindGroupLayoutEntry`. +#[derive(Clone, Debug)] +pub struct BindGroupDescriptor< + 'a, + Bgl: DynBindGroupLayout + ?Sized, + B: DynBuffer + ?Sized, + S: DynSampler + ?Sized, + T: DynTextureView + ?Sized, + A: DynAccelerationStructure + ?Sized, +> { + pub label: Label<'a>, + pub layout: &'a Bgl, + pub buffers: &'a [BufferBinding<'a, B>], + pub samplers: &'a [&'a S], + pub textures: &'a [TextureBinding<'a, T>], + pub entries: &'a [BindGroupEntry], + pub acceleration_structures: &'a [&'a A], + pub external_textures: &'a [ExternalTextureBinding<'a, B, T>], +} + +#[derive(Clone, Debug)] +pub struct CommandEncoderDescriptor<'a, Q: DynQueue + ?Sized> { + pub label: Label<'a>, + pub queue: &'a Q, +} + +/// Naga shader module. +#[derive(Default)] +pub struct NagaShader { + /// Shader module IR. + pub module: Cow<'static, naga::Module>, + /// Analysis information of the module. + pub info: naga::valid::ModuleInfo, + /// Source codes for debug + pub debug_source: Option<DebugSource>, +} + +// Custom implementation avoids the need to generate Debug impl code +// for the whole Naga module and info. +impl fmt::Debug for NagaShader { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "Naga shader") + } +} + +/// Shader input. +pub enum ShaderInput<'a> { + Naga(NagaShader), + MetalLib { + file: &'a [u8], + num_workgroups: hashbrown::HashMap<String, (u32, u32, u32)>, + }, + Msl { + shader: &'a str, + num_workgroups: hashbrown::HashMap<String, (u32, u32, u32)>, + }, + SpirV(&'a [u32]), + Dxil { + shader: &'a [u8], + }, + Hlsl { + shader: &'a str, + }, + Glsl { + shader: &'a str, + }, +} + +impl fmt::Debug for ShaderInput<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + // Don't include the entire shader source, especially for binary formats, because it + // would be spammy. + Self::Naga { .. } => f.debug_tuple("Naga").finish_non_exhaustive(), + Self::MetalLib { .. } => f.debug_tuple("MetalLib").finish_non_exhaustive(), + Self::Msl { .. } => f.debug_tuple("Msl").finish_non_exhaustive(), + Self::SpirV { .. } => f.debug_tuple("SpirV").finish_non_exhaustive(), + Self::Dxil { .. } => f.debug_tuple("Dxil").finish_non_exhaustive(), + Self::Hlsl { .. } => f.debug_tuple("Hlsl").finish_non_exhaustive(), + Self::Glsl { .. } => f.debug_tuple("Glsl").finish_non_exhaustive(), + } + } +} + +#[derive(Debug)] +pub struct ShaderModuleDescriptor<'a> { + pub label: Label<'a>, + + /// # Safety + /// + /// See the documentation for each flag in [`ShaderRuntimeChecks`][src]. + /// + /// [src]: wgt::ShaderRuntimeChecks + pub runtime_checks: wgt::ShaderRuntimeChecks, +} + +#[derive(Debug, Clone)] +pub struct DebugSource { + pub file_name: Cow<'static, str>, + pub source_code: Cow<'static, str>, +} + +/// Describes a programmable pipeline stage. +#[derive(Debug)] +pub struct ProgrammableStage<'a, M: DynShaderModule + ?Sized> { + /// The compiled shader module for this stage. + pub module: &'a M, + /// The name of the entry point in the compiled shader. There must be a function with this name + /// in the shader. + pub entry_point: &'a str, + /// Pipeline constants + pub constants: &'a naga::back::PipelineConstants, + /// Whether workgroup scoped memory will be initialized with zero values for this stage. + /// + /// This is required by the WebGPU spec, but may have overhead which can be avoided + /// for cross-platform applications + pub zero_initialize_workgroup_memory: bool, +} + +impl<M: DynShaderModule + ?Sized> Clone for ProgrammableStage<'_, M> { + fn clone(&self) -> Self { + Self { + module: self.module, + entry_point: self.entry_point, + constants: self.constants, + zero_initialize_workgroup_memory: self.zero_initialize_workgroup_memory, + } + } +} + +/// Describes a compute pipeline. +#[derive(Clone, Debug)] +pub struct ComputePipelineDescriptor< + 'a, + Pl: DynPipelineLayout + ?Sized, + M: DynShaderModule + ?Sized, + Pc: DynPipelineCache + ?Sized, +> { + pub label: Label<'a>, + /// The layout of bind groups for this pipeline. + pub layout: &'a Pl, + /// The compiled compute stage and its entry point. + pub stage: ProgrammableStage<'a, M>, + /// The cache which will be used and filled when compiling this pipeline + pub cache: Option<&'a Pc>, +} + +#[derive(Debug)] +pub struct PipelineCacheDescriptor<'a> { + pub label: Label<'a>, + pub data: Option<&'a [u8]>, +} + +/// Describes how the vertex buffer is interpreted. +#[derive(Clone, Debug)] +pub struct VertexBufferLayout<'a> { + /// The stride, in bytes, between elements of this buffer. + pub array_stride: wgt::BufferAddress, + /// How often this vertex buffer is "stepped" forward. + pub step_mode: wgt::VertexStepMode, + /// The list of attributes which comprise a single vertex. + pub attributes: &'a [wgt::VertexAttribute], +} + +#[derive(Clone, Debug)] +pub enum VertexProcessor<'a, M: DynShaderModule + ?Sized> { + Standard { + /// The format of any vertex buffers used with this pipeline. + vertex_buffers: &'a [Option<VertexBufferLayout<'a>>], + /// The vertex stage for this pipeline. + vertex_stage: ProgrammableStage<'a, M>, + }, + Mesh { + task_stage: Option<ProgrammableStage<'a, M>>, + mesh_stage: ProgrammableStage<'a, M>, + }, +} + +/// Describes a render (graphics) pipeline. +#[derive(Clone, Debug)] +pub struct RenderPipelineDescriptor< + 'a, + Pl: DynPipelineLayout + ?Sized, + M: DynShaderModule + ?Sized, + Pc: DynPipelineCache + ?Sized, +> { + pub label: Label<'a>, + /// The layout of bind groups for this pipeline. + pub layout: &'a Pl, + /// The vertex processing state(vertex shader + buffers or task + mesh shaders) + pub vertex_processor: VertexProcessor<'a, M>, + /// The properties of the pipeline at the primitive assembly and rasterization level. + pub primitive: wgt::PrimitiveState, + /// The effect of draw calls on the depth and stencil aspects of the output target, if any. + pub depth_stencil: Option<wgt::DepthStencilState>, + /// The multi-sampling properties of the pipeline. + pub multisample: wgt::MultisampleState, + /// The fragment stage for this pipeline. + pub fragment_stage: Option<ProgrammableStage<'a, M>>, + /// The effect of draw calls on the color aspect of the output target. + pub color_targets: &'a [Option<wgt::ColorTargetState>], + /// If the pipeline will be used with a multiview render pass, this indicates how many array + /// layers the attachments will have. + pub multiview_mask: Option<NonZeroU32>, + /// The cache which will be used and filled when compiling this pipeline + pub cache: Option<&'a Pc>, +} + +#[derive(Clone, Debug)] +pub struct RayObjectIntersectionState<'a, M: DynShaderModule + ?Sized> { + pub closest_hit: ProgrammableStage<'a, M>, + pub any_hit: Option<ProgrammableStage<'a, M>>, +} + +/// Describes a ray tracing pipeline. +#[derive(Clone, Debug)] +pub struct RayTracingPipelineDescriptor< + 'a, + Pl: DynPipelineLayout + ?Sized, + M: DynShaderModule + ?Sized, + Pc: DynPipelineCache + ?Sized, +> { + pub label: Label<'a>, + /// The layout of bind groups for this pipeline. + pub layout: &'a Pl, + /// The ray generation stage. + pub ray_generation: ProgrammableStage<'a, M>, + /// The miss stage. + pub miss: ProgrammableStage<'a, M>, + /// All the object intersection stages. + pub intersection: &'a [RayObjectIntersectionState<'a, M>], + /// The maximum recursion depth allowed for the ray tracing (ray_generation shader counts as depth 0). + pub max_recursion_depth: u32, + /// The cache which will be used and filled when compiling this pipeline + pub cache: Option<&'a Pc>, +} + +#[derive(Debug, Clone)] +pub struct SurfaceConfiguration { + /// Maximum number of queued frames. Must be in + /// `SurfaceCapabilities::maximum_frame_latency` range. + pub maximum_frame_latency: u32, + /// Vertical synchronization mode. + pub present_mode: wgt::PresentMode, + /// Alpha composition mode. + pub composite_alpha_mode: wgt::CompositeAlphaMode, + /// Format of the surface textures. + pub format: wgt::TextureFormat, + /// Color space in which the presentation engine interprets the surface + /// textures. Never [`wgt::SurfaceColorSpace::Auto`]; `wgpu-core` resolves + /// `Auto` to a concrete color space before configuring the surface, and + /// the (format, color space) pair must be listed in + /// `SurfaceCapabilities::formats`. + pub color_space: wgt::SurfaceColorSpace, + /// Requested texture extent. Must be in + /// `SurfaceCapabilities::extents` range. + pub extent: wgt::Extent3d, + /// Allowed usage of surface textures, + pub usage: wgt::TextureUses, + /// Allows views of swapchain texture to have a different format + /// than the texture does. + pub view_formats: Vec<wgt::TextureFormat>, +} + +#[derive(Debug, Clone)] +pub struct Rect<T> { + pub x: T, + pub y: T, + pub w: T, + pub h: T, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct StateTransition<T> { + pub from: T, + pub to: T, +} + +#[derive(Debug, Clone)] +pub struct BufferBarrier<'a, B: DynBuffer + ?Sized> { + pub buffer: &'a B, + pub usage: StateTransition<wgt::BufferUses>, +} + +#[derive(Debug, Clone)] +pub struct TextureBarrier<'a, T: DynTexture + ?Sized> { + pub texture: &'a T, + pub range: wgt::ImageSubresourceRange, + pub usage: StateTransition<wgt::TextureUses>, +} + +#[derive(Clone, Copy, Debug)] +pub struct BufferCopy { + pub src_offset: wgt::BufferAddress, + pub dst_offset: wgt::BufferAddress, + pub size: wgt::BufferSize, +} + +#[derive(Clone, Debug)] +pub struct TextureCopyBase { + pub mip_level: u32, + pub array_layer: u32, + /// Origin within a texture. + /// Note: for 1D and 2D textures, Z must be 0. + pub origin: wgt::Origin3d, + pub aspect: FormatAspects, +} + +#[derive(Clone, Copy, Debug)] +pub struct CopyExtent { + pub width: u32, + pub height: u32, + pub depth: u32, +} + +impl From<wgt::Extent3d> for CopyExtent { + fn from(value: wgt::Extent3d) -> Self { + let wgt::Extent3d { + width, + height, + depth_or_array_layers, + } = value; + Self { + width, + height, + depth: depth_or_array_layers, + } + } +} + +impl From<CopyExtent> for wgt::Extent3d { + fn from(value: CopyExtent) -> Self { + let CopyExtent { + width, + height, + depth, + } = value; + Self { + width, + height, + depth_or_array_layers: depth, + } + } +} + +#[derive(Clone, Debug)] +pub struct TextureCopy { + pub src_base: TextureCopyBase, + pub dst_base: TextureCopyBase, + pub size: CopyExtent, +} + +#[derive(Clone, Debug)] +pub struct BufferTextureCopy { + pub buffer_layout: wgt::TexelCopyBufferLayout, + pub texture_base: TextureCopyBase, + pub size: CopyExtent, +} + +#[derive(Clone, Debug)] +pub struct Attachment<'a, T: DynTextureView + ?Sized> { + pub view: &'a T, + /// Contains either a single mutating usage as a target, + /// or a valid combination of read-only usages. + pub usage: wgt::TextureUses, +} + +#[derive(Clone, Debug)] +pub struct ColorAttachment<'a, T: DynTextureView + ?Sized> { + pub target: Attachment<'a, T>, + pub depth_slice: Option<u32>, + pub resolve_target: Option<Attachment<'a, T>>, + pub ops: AttachmentOps, + pub clear_value: wgt::Color, +} + +#[derive(Clone, Debug)] +pub struct DepthStencilAttachment<'a, T: DynTextureView + ?Sized> { + pub target: Attachment<'a, T>, + pub depth_ops: AttachmentOps, + pub stencil_ops: AttachmentOps, + pub clear_value: (f32, u32), +} + +#[derive(Clone, Debug)] +pub struct PassTimestampWrites<'a, Q: DynQuerySet + ?Sized> { + pub query_set: &'a Q, + pub beginning_of_pass_write_index: Option<u32>, + pub end_of_pass_write_index: Option<u32>, +} + +#[derive(Clone, Debug)] +pub struct RenderPassDescriptor<'a, Q: DynQuerySet + ?Sized, T: DynTextureView + ?Sized> { + pub label: Label<'a>, + pub extent: wgt::Extent3d, + pub sample_count: u32, + pub color_attachments: &'a [Option<ColorAttachment<'a, T>>], + pub depth_stencil_attachment: Option<DepthStencilAttachment<'a, T>>, + pub multiview_mask: Option<NonZeroU32>, + pub timestamp_writes: Option<PassTimestampWrites<'a, Q>>, + pub occlusion_query_set: Option<&'a Q>, +} + +#[derive(Clone, Debug)] +pub struct ComputePassDescriptor<'a, Q: DynQuerySet + ?Sized> { + pub label: Label<'a>, + pub timestamp_writes: Option<PassTimestampWrites<'a, Q>>, +} + +#[derive(Clone, Debug)] +pub struct RayTracingPassDescriptor<'a> { + pub label: Label<'a>, +} + +#[test] +fn test_default_limits() { + let limits = wgt::Limits::default(); + assert!(limits.max_bind_groups <= MAX_BIND_GROUPS as u32); +} + +#[derive(Clone, Debug)] +pub struct AccelerationStructureDescriptor<'a> { + pub label: Label<'a>, + pub size: wgt::BufferAddress, + pub format: AccelerationStructureFormat, + pub allow_compaction: bool, +} + +#[derive(Debug, Clone, Copy, Eq, PartialEq)] +pub enum AccelerationStructureFormat { + TopLevel, + BottomLevel, +} + +#[derive(Debug, Clone, Copy, Eq, PartialEq)] +pub enum AccelerationStructureBuildMode { + Build, + Update, +} + +/// Information of the required size for a corresponding entries struct (+ flags) +#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)] +pub struct AccelerationStructureBuildSizes { + pub acceleration_structure_size: wgt::BufferAddress, + pub update_scratch_size: wgt::BufferAddress, + pub build_scratch_size: wgt::BufferAddress, +} + +/// Updates use source_acceleration_structure if present, else the update will be performed in place. +/// For updates, only the data is allowed to change (not the meta data or sizes). +#[derive(Clone, Debug)] +pub struct BuildAccelerationStructureDescriptor< + 'a, + B: DynBuffer + ?Sized, + A: DynAccelerationStructure + ?Sized, +> { + pub entries: &'a AccelerationStructureEntries<'a, B>, + pub mode: AccelerationStructureBuildMode, + pub flags: AccelerationStructureBuildFlags, + pub source_acceleration_structure: Option<&'a A>, + pub destination_acceleration_structure: &'a A, + pub scratch_buffer: &'a B, + pub scratch_buffer_offset: wgt::BufferAddress, +} + +/// - All buffers, buffer addresses and offsets will be ignored. +/// - The build mode will be ignored. +/// - Reducing the amount of Instances, Triangle groups or AABB groups (or the number of Triangles/AABBs in corresponding groups), +/// may result in reduced size requirements. +/// - Any other change may result in a bigger or smaller size requirement. +#[derive(Clone, Debug)] +pub struct GetAccelerationStructureBuildSizesDescriptor<'a, B: DynBuffer + ?Sized> { + pub entries: &'a AccelerationStructureEntries<'a, B>, + pub flags: AccelerationStructureBuildFlags, +} + +/// Entries for a single descriptor +/// * `Instances` - Multiple instances for a top level acceleration structure +/// * `Triangles` - Multiple triangle meshes for a bottom level acceleration structure +/// * `AABBs` - List of list of axis aligned bounding boxes for a bottom level acceleration structure +#[derive(Debug)] +pub enum AccelerationStructureEntries<'a, B: DynBuffer + ?Sized> { + Instances(AccelerationStructureInstances<'a, B>), + Triangles(Vec<AccelerationStructureTriangles<'a, B>>), + AABBs(Vec<AccelerationStructureAABBs<'a, B>>), +} + +/// * `first_vertex` - offset in the vertex buffer (as number of vertices) +/// * `indices` - optional index buffer with attributes +/// * `transform` - optional transform +#[derive(Clone, Debug)] +pub struct AccelerationStructureTriangles<'a, B: DynBuffer + ?Sized> { + pub vertex_buffer: Option<&'a B>, + pub vertex_format: wgt::VertexFormat, + pub first_vertex: u32, + pub vertex_count: u32, + pub vertex_stride: wgt::BufferAddress, + pub indices: Option<AccelerationStructureTriangleIndices<'a, B>>, + pub transform: Option<AccelerationStructureTriangleTransform<'a, B>>, + pub flags: AccelerationStructureGeometryFlags, +} + +/// * `offset` - offset in bytes +#[derive(Clone, Debug)] +pub struct AccelerationStructureAABBs<'a, B: DynBuffer + ?Sized> { + pub buffer: Option<&'a B>, + pub offset: u32, + pub count: u32, + pub stride: wgt::BufferAddress, + pub flags: AccelerationStructureGeometryFlags, +} + +#[derive(Clone, Debug)] +pub struct AccelerationStructureCopy { + pub copy_flags: wgt::AccelerationStructureCopy, + pub type_flags: wgt::AccelerationStructureType, +} + +/// * `offset` - offset in bytes +#[derive(Clone, Debug)] +pub struct AccelerationStructureInstances<'a, B: DynBuffer + ?Sized> { + pub buffer: Option<&'a B>, + pub offset: u32, + pub count: u32, +} + +/// * `offset` - offset in bytes +#[derive(Clone, Debug)] +pub struct AccelerationStructureTriangleIndices<'a, B: DynBuffer + ?Sized> { + pub format: wgt::IndexFormat, + pub buffer: Option<&'a B>, + pub offset: u32, + pub count: u32, +} + +/// * `offset` - offset in bytes +#[derive(Clone, Debug)] +pub struct AccelerationStructureTriangleTransform<'a, B: DynBuffer + ?Sized> { + pub buffer: &'a B, + pub offset: u32, +} + +pub use wgt::AccelerationStructureFlags as AccelerationStructureBuildFlags; +pub use wgt::AccelerationStructureGeometryFlags; + +bitflags::bitflags! { + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] + pub struct AccelerationStructureUses: u8 { + // For blas used as input for tlas + const BUILD_INPUT = 1 << 0; + // Target for acceleration structure build + const BUILD_OUTPUT = 1 << 1; + // Tlas used in a shader + const SHADER_INPUT = 1 << 2; + // Blas used to query compacted size + const QUERY_INPUT = 1 << 3; + // BLAS used as a src for a copy operation + const COPY_SRC = 1 << 4; + // BLAS used as a dst for a copy operation + const COPY_DST = 1 << 5; + } +} + +#[derive(Debug, Clone)] +pub struct AccelerationStructureBarrier { + pub usage: StateTransition<AccelerationStructureUses>, +} + +#[derive(Debug, Copy, Clone)] +pub struct TlasInstance { + pub transform: [f32; 12], + pub custom_data: u32, + pub mask: u8, + pub blas_address: u64, + /// The offset for the index into the intersection hit + /// group calculation. Number is in hit groups. + pub pipeline_intersection_data_offset: u32, +} + +#[cfg(dx12)] +#[derive(Debug)] +pub enum D3D12ExposeAdapterResult { + CreateDeviceError(dx12::CreateDeviceError), + UnknownFeatureLevel(i32), + ResourceBindingTier2Requirement, + ShaderModel6Requirement, + Success(dx12::FeatureLevel, dx12::ShaderModel), +} + +/// Pluggable telemetry, mainly to be used by Firefox. +#[derive(Debug, Clone, Copy)] +pub struct Telemetry { + #[cfg(dx12)] + pub d3d12_expose_adapter: fn( + desc: &windows::Win32::Graphics::Dxgi::DXGI_ADAPTER_DESC2, + driver_version: Result<[u16; 4], windows_core::HRESULT>, + result: D3D12ExposeAdapterResult, + ), +} + +#[derive(Debug)] +pub struct PipelineGroupData<'a, B: DynBuffer + ?Sized> { + pub buffer: &'a B, + pub offset: wgt::BufferAddress, + pub stride: u64, + pub count: u64, +} diff --git a/third_party/wgpu-hal/src/metal/adapter.rs b/third_party/wgpu-hal/src/metal/adapter.rs new file mode 100644 index 000000000..c9c48c359 --- /dev/null +++ b/third_party/wgpu-hal/src/metal/adapter.rs @@ -0,0 +1,1808 @@ +use objc2::rc::autoreleasepool; +use objc2::runtime::{AnyObject, ProtocolObject, Sel}; +use objc2::{available, sel}; +use objc2_foundation::{NSOperatingSystemVersion, NSProcessInfo}; +use objc2_metal::{ + MTLArgumentBuffersTier, MTLCounterSamplingPoint, MTLDevice, MTLFeatureSet, MTLGPUFamily, + MTLIndirectAccelerationStructureInstanceDescriptor, MTLLanguageVersion, MTLPixelFormat, + MTLReadWriteTextureTier, +}; +use wgt::{AstcBlock, AstcChannel}; + +use alloc::{string::ToString as _, sync::Arc, vec::Vec}; +use core::sync::atomic; +use parking_lot::Mutex; +use std::sync::OnceLock; + +use crate::metal::QueueShared; + +use super::{OsFeatures, TimestampQuerySupport}; + +/// Check if a device's class has a given method in its method table. +/// +/// This mirrors the check that `objc2` performs internally (in debug builds) +/// before sending a message. We use it to skip method calls that would panic +/// on proxy objects like Apple's `CaptureMTLDevice`, which forwards messages +/// at runtime but doesn't declare the methods in its class. +fn device_class_responds_to(device: &ProtocolObject<dyn MTLDevice>, sel: Sel) -> bool { + AnyObject::class(device.as_ref()).responds_to(sel) +} + +/// Maximum number of command buffers for `MTLCommandQueue`s that we create. +/// +/// If a [new command buffer] is requested when Metal has run out of command +/// buffers, it waits indefinitely for one to become available. If the +/// outstanding command buffers are actively executing on the GPU, this will +/// happen relatively quickly. But if the outstanding command buffers will only +/// be recovered upon GC, and attempting to get a new command buffer prevents +/// forward progress towards that GC, there is a deadlock. +/// +/// This is mostly a problem for the CTS, which frequently creates command +/// buffers that it does not submit. It is unclear how likely command buffer +/// exhaustion is in real applications. +/// +/// This limit was increased from a previous value of 2048 for +/// <https://bugzilla.mozilla.org/show_bug.cgi?id=1971452>. +/// +/// [new command buffer]: https://developer.apple.com/documentation/metal/mtlcommandqueue/makecommandbuffer()?language=objc +pub(super) const MAX_COMMAND_BUFFERS: usize = 4096; + +/// "Maximum number of entries in the buffer argument table, per graphics or kernel function" +/// +/// Vertex buffers are placed at the end of the table (highest indices), +/// counting down from MAX_BUFFERS - 1. +pub const MAX_BUFFERS: u32 = 31; + +impl super::Adapter { + pub(super) fn new(shared: Arc<super::AdapterShared>) -> Self { + Self { shared } + } + pub fn raw_device(&self) -> &ProtocolObject<dyn MTLDevice> { + &self.shared.device + } +} + +impl crate::Adapter for super::Adapter { + type A = super::Api; + + unsafe fn open( + &self, + features: wgt::Features, + limits: &wgt::Limits, + _memory_hints: &wgt::MemoryHints, + ) -> Result<crate::OpenDevice<super::Api>, crate::DeviceError> { + autoreleasepool(|_| { + let queue = self + .shared + .device + .newCommandQueueWithMaxCommandBufferCount(MAX_COMMAND_BUFFERS) + .unwrap(); + + // Acquiring the meaning of timestamp ticks is hard with Metal! + // The only thing there is a method correlating cpu & gpu timestamps (`device.sample_timestamps`). + // Users are supposed to call this method twice and calculate the difference, + // see "Converting GPU Timestamps into CPU Time": + // https://developer.apple.com/documentation/metal/gpu_counters_and_counter_sample_buffers/converting_gpu_timestamps_into_cpu_time + // Not only does this mean we get an approximate value, this is as also *very slow*! + // Chromium opted to solve this using a linear regression that they stop at some point + // https://source.chromium.org/chromium/chromium/src/+/refs/heads/main:third_party/dawn/src/dawn/native/metal/DeviceMTL.mm;drc=76be2f9f117654f3fe4faa477b0445114fccedda;bpv=0;bpt=1;l=46 + // Generally, the assumption is that timestamp values aren't changing over time, after all all other APIs provide stable values. + // + // We should do as Chromium does for the general case, but this requires quite some state tracking + // and doesn't even provide perfectly accurate values, especially at the start of the application when + // we didn't have the chance to sample a lot of values just yet. + // + // So instead, we're doing the dangerous but easy thing and use our "knowledge" of timestamps + // conversions on different devices, after all Metal isn't supported on that many ;) + // Based on: + // * https://github.com/gfx-rs/wgpu/pull/2528 + // * https://github.com/gpuweb/gpuweb/issues/1325#issuecomment-761041326 + let timestamp_period = if self.shared.device.name().to_string().starts_with("Intel") { + 83.333 + } else { + // Known for Apple Silicon (at least M1 & M2, iPad Pro 2018) and AMD GPUs. + 1.0 + }; + + Ok(crate::OpenDevice { + device: super::Device { + shared: Arc::clone(&self.shared), + features, + counters: Default::default(), + limits: limits.clone(), + }, + queue: super::Queue { + shared: Arc::new(QueueShared { + raw: queue, + command_buffer_created_not_submitted: atomic::AtomicUsize::new(0), + pending_waits: Mutex::new(Vec::new()), + pending_signals: Mutex::new(Vec::new()), + relay: OnceLock::new(), + }), + timestamp_period, + }, + }) + }) + } + + unsafe fn texture_format_capabilities( + &self, + format: wgt::TextureFormat, + ) -> crate::TextureFormatCapabilities { + use crate::TextureFormatCapabilities as Tfc; + use wgt::TextureFormat as Tf; + + let msl_version = self.shared.private_caps.msl_version; + let pc = &self.shared.private_texture_format_caps; + // Affected formats documented at: + // https://developer.apple.com/documentation/metal/mtlreadwritetexturetier/mtlreadwritetexturetier1?language=objc + // https://developer.apple.com/documentation/metal/mtlreadwritetexturetier/mtlreadwritetexturetier2?language=objc + let (read_write_tier1_if, read_write_tier2_if) = match pc.read_write_texture_tier { + MTLReadWriteTextureTier::TierNone => (Tfc::empty(), Tfc::empty()), + MTLReadWriteTextureTier::Tier1 => (Tfc::STORAGE_READ_WRITE, Tfc::empty()), + MTLReadWriteTextureTier::Tier2 => (Tfc::STORAGE_READ_WRITE, Tfc::STORAGE_READ_WRITE), + // Unknown levels of support are likely higher than Tier 2. + _ => (Tfc::STORAGE_READ_WRITE, Tfc::STORAGE_READ_WRITE), + }; + let msaa_count = pc.sample_count_mask; + + let msaa_resolve_desktop_if = if pc.msaa_desktop { + Tfc::MULTISAMPLE_RESOLVE + } else { + Tfc::empty() + }; + let msaa_resolve_apple3x_if = if pc.msaa_desktop | pc.msaa_apple3 { + Tfc::MULTISAMPLE_RESOLVE + } else { + Tfc::empty() + }; + let is_not_apple1x = super::CapabilitiesQuery::supports_any( + self.shared.device.as_ref(), + &[ + MTLFeatureSet::iOS_GPUFamily2_v1, + MTLFeatureSet::macOS_GPUFamily1_v1, + MTLFeatureSet::tvOS_GPUFamily1_v1, + ], + ); + + let image_atomic_if = if msl_version >= MTLLanguageVersion::Version3_1 { + Tfc::STORAGE_ATOMIC + } else { + Tfc::empty() + }; + + let image_64_atomic_if = if pc.int64_atomics { + Tfc::STORAGE_ATOMIC + } else { + Tfc::empty() + }; + + // Metal defined pixel format capabilities + let all_caps = Tfc::SAMPLED_LINEAR + | Tfc::STORAGE_WRITE_ONLY + | Tfc::COLOR_ATTACHMENT + | Tfc::COLOR_ATTACHMENT_BLEND + | msaa_count + | Tfc::MULTISAMPLE_RESOLVE; + + let extra = match format { + Tf::R8Unorm | Tf::R16Float | Tf::Rgba8Unorm | Tf::Rgba16Float => { + read_write_tier2_if | all_caps + } + Tf::R8Snorm | Tf::Rg8Snorm | Tf::Rgba8Snorm => { + let mut flags = all_caps; + flags.set(Tfc::MULTISAMPLE_RESOLVE, is_not_apple1x); + flags + } + Tf::R8Uint + | Tf::R8Sint + | Tf::R16Uint + | Tf::R16Sint + | Tf::Rgba8Uint + | Tf::Rgba8Sint + | Tf::Rgba16Uint + | Tf::Rgba16Sint => { + read_write_tier2_if | Tfc::STORAGE_WRITE_ONLY | Tfc::COLOR_ATTACHMENT | msaa_count + } + Tf::R16Unorm + | Tf::R16Snorm + | Tf::Rg16Unorm + | Tf::Rg16Snorm + | Tf::Rgba16Unorm + | Tf::Rgba16Snorm => { + Tfc::SAMPLED_LINEAR + | Tfc::STORAGE_WRITE_ONLY + | Tfc::COLOR_ATTACHMENT + | Tfc::COLOR_ATTACHMENT_BLEND + | msaa_count + | msaa_resolve_desktop_if + } + Tf::Rg8Unorm | Tf::Rg16Float | Tf::Bgra8Unorm => all_caps, + Tf::Rg8Uint | Tf::Rg8Sint => { + Tfc::STORAGE_WRITE_ONLY | Tfc::COLOR_ATTACHMENT | msaa_count + } + Tf::R32Uint | Tf::R32Sint => { + read_write_tier1_if + | Tfc::STORAGE_WRITE_ONLY + | Tfc::COLOR_ATTACHMENT + | msaa_count + | image_atomic_if + } + Tf::R32Float => { + let flags = if pc.format_r32float_all { + all_caps + } else { + Tfc::STORAGE_WRITE_ONLY + | Tfc::COLOR_ATTACHMENT + | Tfc::COLOR_ATTACHMENT_BLEND + | msaa_count + }; + read_write_tier1_if | flags + } + Tf::Rg16Uint | Tf::Rg16Sint => { + Tfc::STORAGE_WRITE_ONLY | Tfc::COLOR_ATTACHMENT | msaa_count + } + Tf::Rgba8UnormSrgb | Tf::Bgra8UnormSrgb => { + let mut flags = all_caps; + flags.set(Tfc::STORAGE_WRITE_ONLY, pc.format_rgba8_srgb_all); + flags + } + Tf::Rgb10a2Uint => { + let mut flags = Tfc::COLOR_ATTACHMENT | msaa_count; + flags.set(Tfc::STORAGE_WRITE_ONLY, pc.format_rgb10a2_uint_write); + flags + } + Tf::Rgb10a2Unorm => { + let mut flags = all_caps; + flags.set(Tfc::STORAGE_WRITE_ONLY, pc.format_rgb10a2_unorm_all); + flags + } + Tf::Rg11b10Ufloat => { + let mut flags = all_caps; + flags.set(Tfc::STORAGE_WRITE_ONLY, pc.format_rg11b10_all); + flags + } + Tf::R64Uint => { + Tfc::COLOR_ATTACHMENT + | Tfc::STORAGE_WRITE_ONLY + | image_64_atomic_if + | read_write_tier1_if + } + Tf::Rg32Uint | Tf::Rg32Sint => { + Tfc::COLOR_ATTACHMENT | Tfc::STORAGE_WRITE_ONLY | msaa_count + } + Tf::Rg32Float => { + if pc.format_rg32float_all { + all_caps + } else { + Tfc::STORAGE_WRITE_ONLY + | Tfc::COLOR_ATTACHMENT + | Tfc::COLOR_ATTACHMENT_BLEND + | msaa_count + } + } + Tf::Rgba32Uint | Tf::Rgba32Sint => { + read_write_tier2_if | Tfc::STORAGE_WRITE_ONLY | Tfc::COLOR_ATTACHMENT | msaa_count + } + Tf::Rgba32Float => { + let mut flags = + read_write_tier2_if | Tfc::STORAGE_WRITE_ONLY | Tfc::COLOR_ATTACHMENT; + if pc.format_rgba32float_all { + flags |= all_caps + } else if pc.msaa_apple7 { + flags |= msaa_count + }; + flags + } + Tf::Stencil8 => { + all_caps | Tfc::DEPTH_STENCIL_ATTACHMENT | msaa_count | msaa_resolve_apple3x_if + } + Tf::Depth16Unorm => { + if pc.format_depth16unorm { + let mut flags = + Tfc::DEPTH_STENCIL_ATTACHMENT | msaa_count | msaa_resolve_apple3x_if; + if pc.format_depth16unorm_filter { + flags |= Tfc::SAMPLED_LINEAR; + } + flags + } else { + return Tfc::empty(); + } + } + Tf::Depth32Float | Tf::Depth32FloatStencil8 => { + let mut flags = + Tfc::DEPTH_STENCIL_ATTACHMENT | msaa_count | msaa_resolve_apple3x_if; + if pc.format_depth32float_filter { + flags |= Tfc::SAMPLED_LINEAR + } + flags + } + Tf::Depth24Plus | Tf::Depth24PlusStencil8 => { + let mut flags = Tfc::DEPTH_STENCIL_ATTACHMENT | msaa_count; + if pc.format_depth24_stencil8 { + flags |= Tfc::SAMPLED_LINEAR | Tfc::MULTISAMPLE_RESOLVE + } else { + flags |= msaa_resolve_apple3x_if; + if pc.format_depth32float_filter { + flags |= Tfc::SAMPLED_LINEAR + } + } + flags + } + Tf::NV12 => return Tfc::empty(), + Tf::P010 => return Tfc::empty(), + Tf::Rgb9e5Ufloat => { + if pc.msaa_apple3 { + all_caps + } else if pc.msaa_desktop { + Tfc::SAMPLED_LINEAR + } else { + Tfc::SAMPLED_LINEAR + | Tfc::COLOR_ATTACHMENT + | Tfc::COLOR_ATTACHMENT_BLEND + | msaa_count + | Tfc::MULTISAMPLE_RESOLVE + } + } + Tf::Bc1RgbaUnorm + | Tf::Bc1RgbaUnormSrgb + | Tf::Bc2RgbaUnorm + | Tf::Bc2RgbaUnormSrgb + | Tf::Bc3RgbaUnorm + | Tf::Bc3RgbaUnormSrgb + | Tf::Bc4RUnorm + | Tf::Bc4RSnorm + | Tf::Bc5RgUnorm + | Tf::Bc5RgSnorm + | Tf::Bc6hRgbUfloat + | Tf::Bc6hRgbFloat + | Tf::Bc7RgbaUnorm + | Tf::Bc7RgbaUnormSrgb => { + if pc.format_bc { + Tfc::SAMPLED_LINEAR + } else { + Tfc::empty() + } + } + Tf::Etc2Rgb8Unorm + | Tf::Etc2Rgb8UnormSrgb + | Tf::Etc2Rgb8A1Unorm + | Tf::Etc2Rgb8A1UnormSrgb + | Tf::Etc2Rgba8Unorm + | Tf::Etc2Rgba8UnormSrgb + | Tf::EacR11Unorm + | Tf::EacR11Snorm + | Tf::EacRg11Unorm + | Tf::EacRg11Snorm => { + if pc.format_eac_etc { + Tfc::SAMPLED_LINEAR + } else { + Tfc::empty() + } + } + Tf::Astc { + block: _, + channel: _, + } => { + if pc.format_astc || pc.format_astc_hdr { + Tfc::SAMPLED_LINEAR + } else { + Tfc::empty() + } + } + }; + + Tfc::COPY_SRC | Tfc::COPY_DST | Tfc::SAMPLED | Tfc::STORAGE_READ_ONLY | extra + } + + unsafe fn surface_capabilities( + &self, + surface: &super::Surface, + ) -> Option<crate::SurfaceCapabilities> { + // `CAMetalLayer` color-matches layer contents to whatever display the + // window is on, with the compositor tone-mapping where needed, so + // Display-P3 and HDR (PQ/HLG) color spaces work regardless of the + // physical display's gamut and are not gated on it here. + let format_caps = |format: wgt::TextureFormat| { + let mut color_spaces = + wgt::SurfaceColorSpaces::SRGB | wgt::SurfaceColorSpaces::DISPLAY_P3; + if format == wgt::TextureFormat::Rgba16Float { + // `Rgba16Float` enables Metal's extended dynamic range, in both + // linear (scRGB) and encoded (extended nonlinear sRGB) form, + // for the BT.709 and Display-P3 gamuts. + color_spaces |= wgt::SurfaceColorSpaces::EXTENDED_SRGB_LINEAR + | wgt::SurfaceColorSpaces::EXTENDED_SRGB + | wgt::SurfaceColorSpaces::EXTENDED_DISPLAY_P3; + } + // PQ/HLG only on the >=10-bit formats: 8-bit PQ would result in unusable + // banding. The ITUR_2100 color space constants require + // macOS 11.0/iOS 14.0. + if matches!( + format, + wgt::TextureFormat::Rgba16Float | wgt::TextureFormat::Rgb10a2Unorm + ) && available!(macos = 11.0, ios = 14.0, tvos = 14.0, visionos = 1.0) + { + color_spaces |= + wgt::SurfaceColorSpaces::BT2100_PQ | wgt::SurfaceColorSpaces::BT2100_HLG; + } + wgt::SurfaceFormatCapabilities { + format, + color_spaces, + } + }; + let mut formats = vec![ + format_caps(wgt::TextureFormat::Bgra8Unorm), + format_caps(wgt::TextureFormat::Bgra8UnormSrgb), + format_caps(wgt::TextureFormat::Rgba16Float), + ]; + if self + .shared + .private_texture_format_caps + .format_rgb10a2_unorm_all + { + formats.push(format_caps(wgt::TextureFormat::Rgb10a2Unorm)); + } + + Some(crate::SurfaceCapabilities { + formats, + // We use this here to govern the maximum number of drawables + 1. + // See https://developer.apple.com/documentation/quartzcore/cametallayer/maximumdrawablecount + maximum_frame_latency: if available!( + macos = 10.13.2, + ios = 11.2, + tvos = 11.2, + visionos = 1.0 + ) { + 1..=2 + } else { + // 3 is the default value for maximum drawables in `CAMetalLayer` documentation + // iOS 10.3 was tested to use 3 on iphone5s + 2..=2 + }, + // We enable Immediate mode using `-[CAMetalLayer setDisplaySyncEnabled: false]`. + present_modes: if OsFeatures::display_sync() { + vec![wgt::PresentMode::Fifo, wgt::PresentMode::Immediate] + } else { + vec![wgt::PresentMode::Fifo] + }, + composite_alpha_modes: vec![ + wgt::CompositeAlphaMode::Opaque, + wgt::CompositeAlphaMode::PostMultiplied, + ], + + current_extent: Some(surface.dimensions()), + usage: wgt::TextureUses::COLOR_TARGET + | wgt::TextureUses::COPY_SRC + | wgt::TextureUses::COPY_DST + | wgt::TextureUses::STORAGE_READ_ONLY + | wgt::TextureUses::STORAGE_WRITE_ONLY + | wgt::TextureUses::STORAGE_READ_WRITE, + }) + } + + unsafe fn surface_display_hdr_info( + &self, + surface: &super::Surface, + ) -> Option<wgt::DisplayHdrInfo> { + surface.display_hdr_info() + } + + unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp { + let timestamp = self.shared.presentation_timer.get_timestamp_ns(); + + wgt::PresentationTimestamp(timestamp) + } + + fn get_ordered_buffer_usages(&self) -> wgt::BufferUses { + wgt::BufferUses::INCLUSIVE | wgt::BufferUses::MAP_WRITE + } + + // Don't put barriers between inclusive uses + fn get_ordered_texture_usages(&self) -> wgt::TextureUses { + wgt::TextureUses::INCLUSIVE + | wgt::TextureUses::COLOR_TARGET + | wgt::TextureUses::DEPTH_STENCIL_WRITE + } +} + +const RESOURCE_HEAP_SUPPORT: &[MTLFeatureSet] = &[ + MTLFeatureSet::iOS_GPUFamily1_v3, + MTLFeatureSet::tvOS_GPUFamily1_v2, + MTLFeatureSet::macOS_GPUFamily1_v3, +]; + +const MUTABLE_COMPARISON_SAMPLER_SUPPORT: &[MTLFeatureSet] = &[ + MTLFeatureSet::iOS_GPUFamily3_v1, + MTLFeatureSet::macOS_GPUFamily1_v1, +]; + +const SAMPLER_CLAMP_TO_BORDER_SUPPORT: &[MTLFeatureSet] = &[MTLFeatureSet::macOS_GPUFamily1_v2]; + +const ASTC_PIXEL_FORMAT_FEATURES: &[MTLFeatureSet] = &[ + MTLFeatureSet::iOS_GPUFamily2_v1, + MTLFeatureSet::tvOS_GPUFamily1_v1, +]; + +const ANY8_UNORM_SRGB_ALL: &[MTLFeatureSet] = &[ + MTLFeatureSet::iOS_GPUFamily2_v3, + MTLFeatureSet::tvOS_GPUFamily1_v2, +]; + +const ANY8_SNORM_RESOLVE: &[MTLFeatureSet] = &[ + MTLFeatureSet::iOS_GPUFamily2_v1, + MTLFeatureSet::tvOS_GPUFamily1_v1, + MTLFeatureSet::macOS_GPUFamily1_v1, +]; + +const RGBA8_SRGB: &[MTLFeatureSet] = &[ + MTLFeatureSet::iOS_GPUFamily2_v3, + MTLFeatureSet::tvOS_GPUFamily1_v2, +]; + +const RGB10A2UNORM_ALL: &[MTLFeatureSet] = &[ + MTLFeatureSet::iOS_GPUFamily3_v1, + MTLFeatureSet::tvOS_GPUFamily2_v1, + MTLFeatureSet::macOS_GPUFamily1_v1, +]; + +const RGB10A2UINT_WRITE: &[MTLFeatureSet] = &[ + MTLFeatureSet::iOS_GPUFamily3_v1, + MTLFeatureSet::tvOS_GPUFamily2_v1, + MTLFeatureSet::macOS_GPUFamily1_v1, +]; + +const RG11B10FLOAT_ALL: &[MTLFeatureSet] = &[ + MTLFeatureSet::iOS_GPUFamily3_v1, + MTLFeatureSet::tvOS_GPUFamily2_v1, + MTLFeatureSet::macOS_GPUFamily1_v1, +]; + +const RGB9E5FLOAT_ALL: &[MTLFeatureSet] = &[ + MTLFeatureSet::iOS_GPUFamily3_v1, + MTLFeatureSet::tvOS_GPUFamily2_v1, +]; + +const BGR10A2_ALL: &[MTLFeatureSet] = &[ + MTLFeatureSet::iOS_GPUFamily1_v4, + MTLFeatureSet::tvOS_GPUFamily1_v3, + MTLFeatureSet::macOS_GPUFamily2_v1, +]; + +/// "Indirect draw & dispatch arguments" in the Metal feature set tables +const INDIRECT_DRAW_DISPATCH_SUPPORT: &[MTLFeatureSet] = &[ + MTLFeatureSet::iOS_GPUFamily3_v1, + MTLFeatureSet::tvOS_GPUFamily2_v1, + MTLFeatureSet::macOS_GPUFamily1_v1, +]; + +/// "Base vertex/instance drawing" in the Metal feature set tables +/// +/// in our terms, `base_vertex` and `first_instance` must be 0 +const BASE_VERTEX_FIRST_INSTANCE_SUPPORT: &[MTLFeatureSet] = INDIRECT_DRAW_DISPATCH_SUPPORT; + +const TEXTURE_CUBE_ARRAY_SUPPORT: &[MTLFeatureSet] = &[ + MTLFeatureSet::iOS_GPUFamily4_v1, + MTLFeatureSet::tvOS_GPUFamily1_v2, + MTLFeatureSet::macOS_GPUFamily1_v1, +]; + +const DUAL_SOURCE_BLEND_SUPPORT: &[MTLFeatureSet] = &[ + MTLFeatureSet::iOS_GPUFamily1_v4, + MTLFeatureSet::tvOS_GPUFamily1_v3, + MTLFeatureSet::macOS_GPUFamily1_v2, +]; + +const LAYERED_RENDERING_SUPPORT: &[MTLFeatureSet] = &[ + MTLFeatureSet::iOS_GPUFamily5_v1, + MTLFeatureSet::macOS_GPUFamily1_v1, + MTLFeatureSet::macOS_GPUFamily2_v1, +]; + +const FUNCTION_SPECIALIZATION_SUPPORT: &[MTLFeatureSet] = &[ + MTLFeatureSet::iOS_GPUFamily1_v3, + MTLFeatureSet::tvOS_GPUFamily1_v2, + MTLFeatureSet::macOS_GPUFamily1_v2, +]; + +const DEPTH_CLIP_MODE: &[MTLFeatureSet] = &[ + MTLFeatureSet::iOS_GPUFamily4_v1, + MTLFeatureSet::tvOS_GPUFamily1_v3, + MTLFeatureSet::macOS_GPUFamily1_v1, +]; + +impl super::CapabilitiesQuery { + fn supports_any(raw: &ProtocolObject<dyn MTLDevice>, features_sets: &[MTLFeatureSet]) -> bool { + features_sets + .iter() + .cloned() + .any(|x| raw.supportsFeatureSet(x)) + } + + /// Query the capabilities of the device. + pub fn new(device: &ProtocolObject<dyn MTLDevice>) -> Self { + // There are four different OSes we can target: macOS, iOS, tvOS and + // visionOS. This can be detected using `cfg!(target_os = "ios")`, or + // more conveniently using the `available!(...)` macro, which also + // checks that the OS version that the binary is currently running on + // is higher than or equal to the specified version. + // + // Along with the different OSes, there is also two other modes that + // applications can run in: the Simulator, and Mac Catalyst. This can + // be detected using `cfg!(target_env = "sim")` or + // `cfg!(target_env = "macabi")`. + // + // Finally, iOS applications can be run on macOS and visionOS directly + // using the "Designed for iPad" mode. This cannot be detected at + // compile-time. + // + // All of this means that it only makes sense to use `cfg!(...)` and + // `available!(...)` in here to check which Metal APIs are available; + // we cannot rely on it for knowing properties of the device. For + // that, we'll want to use `supportsFeatureSet` or `supportsFamily`. + // + // See the following link for further details: + // https://developer.apple.com/documentation/metal/developing-metal-apps-that-run-in-simulator + + let version = NSProcessInfo::processInfo().operatingSystemVersion(); + let os_type = super::OsType::new(version, device); + + let family_check = available!(macos = 10.15, ios = 13.0, tvos = 13.0, visionos = 1.0); + let metal3 = family_check && device.supportsFamily(MTLGPUFamily::Metal3); + let metal4 = family_check && device.supportsFamily(MTLGPUFamily::Metal4); + let mut sample_count_mask = crate::TextureFormatCapabilities::MULTISAMPLE_X4; // 1 and 4 samples are supported on all devices + if device.supportsTextureSampleCount(2) { + sample_count_mask |= crate::TextureFormatCapabilities::MULTISAMPLE_X2; + } + if device.supportsTextureSampleCount(8) { + sample_count_mask |= crate::TextureFormatCapabilities::MULTISAMPLE_X8; + } + if device.supportsTextureSampleCount(16) { + sample_count_mask |= crate::TextureFormatCapabilities::MULTISAMPLE_X16; + } + + let rw_texture_tier = if available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0) + { + device.readWriteTextureSupport() + } else if available!(macos = 10.12) { + if Self::supports_any(device, &[MTLFeatureSet::macOS_ReadWriteTextureTier2]) { + MTLReadWriteTextureTier::Tier2 + } else { + MTLReadWriteTextureTier::Tier1 + } + } else { + MTLReadWriteTextureTier::TierNone + }; + + let mut timestamp_query_support = TimestampQuerySupport::empty(); + if available!(macos = 11.0, ios = 14.0, tvos = 14.0, visionos = 1.0) + && device.supportsCounterSampling(MTLCounterSamplingPoint::AtStageBoundary) + { + // If we don't support at stage boundary, don't support anything else. + timestamp_query_support.insert(TimestampQuerySupport::STAGE_BOUNDARIES); + + if device.supportsCounterSampling(MTLCounterSamplingPoint::AtDrawBoundary) { + timestamp_query_support.insert(TimestampQuerySupport::ON_RENDER_ENCODER); + } + if device.supportsCounterSampling(MTLCounterSamplingPoint::AtDispatchBoundary) { + timestamp_query_support.insert(TimestampQuerySupport::ON_COMPUTE_ENCODER); + } + if device.supportsCounterSampling(MTLCounterSamplingPoint::AtBlitBoundary) { + timestamp_query_support.insert(TimestampQuerySupport::ON_BLIT_ENCODER); + } + // `TimestampQuerySupport::INSIDE_WGPU_PASSES` emerges from the other flags. + } + + let argument_buffers = available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0) + .then(|| device.argumentBuffersSupport()); + + let is_virtual = device.name().to_string().to_lowercase().contains("virtual"); + + let mesh_shaders = family_check + && (device.supportsFamily(MTLGPUFamily::Metal3) + || device.supportsFamily(MTLGPUFamily::Apple7) + || device.supportsFamily(MTLGPUFamily::Mac2)) + // Mesh shaders don't work on virtual devices even if they should be supported. CI thing + && !is_virtual; + + let msl_version = if available!(macos = 26.0, ios = 26.0, tvos = 26.0, visionos = 26.0) { + MTLLanguageVersion::Version4_0 + } else if available!(macos = 15.0, ios = 18.0, tvos = 18.0, visionos = 2.0) { + MTLLanguageVersion::Version3_2 + } else if available!(macos = 14.0, ios = 17.0, tvos = 17.0, visionos = 1.0) { + MTLLanguageVersion::Version3_1 + } else if available!(macos = 13.0, ios = 16.0, tvos = 16.0, visionos = 1.0) { + MTLLanguageVersion::Version3_0 + } else if available!(macos = 12.0, ios = 15.0, tvos = 15.0, visionos = 1.0) { + MTLLanguageVersion::Version2_4 + } else if available!(macos = 11.0, ios = 14.0, tvos = 14.0, visionos = 1.0) { + MTLLanguageVersion::Version2_3 + } else if available!(macos = 10.15, ios = 13.0, tvos = 13.0, visionos = 1.0) { + MTLLanguageVersion::Version2_2 + } else if available!(macos = 10.14, ios = 12.0, tvos = 12.0, visionos = 1.0) { + MTLLanguageVersion::Version2_1 + } else if available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0) { + MTLLanguageVersion::Version2_0 + } else if available!(macos = 10.12, ios = 10.0, tvos = 10.0, visionos = 1.0) { + MTLLanguageVersion::Version1_2 + } else if available!(macos = 10.11, ios = 9.0, tvos = 9.0, visionos = 1.0) { + MTLLanguageVersion::Version1_1 + } else { + MTLLanguageVersion::Version1_0 + }; + + Self { + msl_version, + // macOS 10.11 doesn't support read-write resources + fragment_rw_storage: available!(macos = 10.12, ios = 8.0, tvos = 8.0, visionos = 1.0), + read_write_texture_tier: rw_texture_tier, + msaa_desktop: os_type == super::OsType::Macos, + msaa_apple3: (family_check && device.supportsFamily(MTLGPUFamily::Apple3)) + || device.supportsFeatureSet(MTLFeatureSet::iOS_GPUFamily3_v4), + msaa_apple7: family_check && device.supportsFamily(MTLGPUFamily::Apple7), + resource_heaps: Self::supports_any(device, RESOURCE_HEAP_SUPPORT), + argument_buffers, + mutable_comparison_samplers: Self::supports_any( + device, + MUTABLE_COMPARISON_SAMPLER_SUPPORT, + ), + sampler_clamp_to_border: Self::supports_any(device, SAMPLER_CLAMP_TO_BORDER_SUPPORT), + indirect_draw_dispatch: Self::supports_any(device, INDIRECT_DRAW_DISPATCH_SUPPORT), + base_vertex_first_instance_drawing: Self::supports_any( + device, + BASE_VERTEX_FIRST_INSTANCE_SUPPORT, + ), + dual_source_blending: Self::supports_any(device, DUAL_SOURCE_BLEND_SUPPORT), + low_power: os_type != super::OsType::Macos || device.isLowPower(), + headless: os_type == super::OsType::Macos && device.isHeadless(), + layered_rendering: Self::supports_any(device, LAYERED_RENDERING_SUPPORT), + function_specialization: Self::supports_any(device, FUNCTION_SPECIALIZATION_SUPPORT), + depth_clip_mode: Self::supports_any(device, DEPTH_CLIP_MODE), + texture_cube_array: Self::supports_any(device, TEXTURE_CUBE_ARRAY_SUPPORT), + supports_float_filtering: os_type == super::OsType::Macos + || (available!(macos = 11.0, ios = 14.0, tvos = 16.0, visionos = 1.0) + && device_class_responds_to(device, sel!(supports32BitFloatFiltering)) + && device.supports32BitFloatFiltering()), + format_depth24_stencil8: os_type == super::OsType::Macos + && device.isDepth24Stencil8PixelFormatSupported(), + format_depth32_stencil8_filter: os_type == super::OsType::Macos, + format_depth32_stencil8_none: os_type != super::OsType::Macos, + format_min_srgb_channels: if os_type == super::OsType::Macos { + 4 + } else { + 1 + }, + format_b5: os_type != super::OsType::Macos, + format_bc: os_type == super::OsType::Macos + || (available!(macos = 11.0, ios = 16.4, tvos = 16.4, visionos = 1.0) + && device_class_responds_to(device, sel!(supportsBCTextureCompression)) + && device.supportsBCTextureCompression()), + format_eac_etc: os_type != super::OsType::Macos + // M1 in macOS supports EAC/ETC2 + || (family_check && device.supportsFamily(MTLGPUFamily::Apple7)), + // A8(Apple2) and later always support ASTC pixel formats + format_astc: (family_check && device.supportsFamily(MTLGPUFamily::Apple2)) + || Self::supports_any(device, ASTC_PIXEL_FORMAT_FEATURES), + // A13(Apple6) M1(Apple7) and later always support HDR ASTC pixel formats + format_astc_hdr: family_check && device.supportsFamily(MTLGPUFamily::Apple6), + // Apple3 and later supports compressed volume texture formats including ASTC Sliced 3D + format_astc_3d: family_check && device.supportsFamily(MTLGPUFamily::Apple3), + format_any8_unorm_srgb_all: Self::supports_any(device, ANY8_UNORM_SRGB_ALL), + format_any8_unorm_srgb_no_write: !Self::supports_any(device, ANY8_UNORM_SRGB_ALL) + && os_type != super::OsType::Macos, + format_any8_snorm_all: Self::supports_any(device, ANY8_SNORM_RESOLVE), + format_r16_norm_all: os_type == super::OsType::Macos, + // No devices support r32's all capabilities + format_r32_all: false, + // All devices support r32's write capability + format_r32_no_write: false, + // iOS support r32float's write capability, macOS support r32float's all capabilities + format_r32float_no_write_no_filter: false, + // Only iOS doesn't support r32float's filter capability + format_r32float_no_filter: os_type != super::OsType::Macos, + format_r32float_all: os_type == super::OsType::Macos, + format_rgba8_srgb_all: Self::supports_any(device, RGBA8_SRGB), + format_rgba8_srgb_no_write: !Self::supports_any(device, RGBA8_SRGB), + format_rgb10a2_unorm_all: Self::supports_any(device, RGB10A2UNORM_ALL), + format_rgb10a2_unorm_no_write: !Self::supports_any(device, RGB10A2UNORM_ALL), + format_rgb10a2_uint_write: Self::supports_any(device, RGB10A2UINT_WRITE), + format_rg11b10_all: Self::supports_any(device, RG11B10FLOAT_ALL), + format_rg11b10_no_write: !Self::supports_any(device, RG11B10FLOAT_ALL), + format_rgb9e5_all: Self::supports_any(device, RGB9E5FLOAT_ALL), + format_rgb9e5_no_write: !Self::supports_any(device, RGB9E5FLOAT_ALL) + && os_type != super::OsType::Macos, + format_rgb9e5_filter_only: os_type == super::OsType::Macos, + format_rg32_color: true, + format_rg32_color_write: true, + // Only macOS support rg32float's all capabilities + format_rg32float_all: os_type == super::OsType::Macos, + // All devices support rg32float's color + blend capabilities + format_rg32float_color_blend: true, + // Only iOS doesn't support rg32float's filter + format_rg32float_no_filter: os_type != super::OsType::Macos, + format_rgba32int_color: true, + // All devices support rgba32uint and rgba32sint's color + write capabilities + format_rgba32int_color_write: true, + format_rgba32float_color: true, + // All devices support rgba32float's color + write capabilities + format_rgba32float_color_write: true, + // Only macOS support rgba32float's all capabilities + format_rgba32float_all: os_type == super::OsType::Macos, + // https://developer.apple.com/documentation/metal/mtlpixelformat/depth16unorm + format_depth16unorm: available!(macos = 10.12, ios = 13.0, tvos = 13.0, visionos = 1.0), + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=12 + format_depth16unorm_filter: family_check + && (metal3 + || device.supportsFamily(MTLGPUFamily::Apple3) + || device.supportsFamily(MTLGPUFamily::Mac2)), + format_depth32float_filter: os_type == super::OsType::Macos, + format_depth32float_none: os_type != super::OsType::Macos, + format_bgr10a2_all: Self::supports_any(device, BGR10A2_ALL), + format_bgr10a2_no_write: !Self::supports_any(device, BGR10A2_ALL), + // "Maximum number of entries in the texture argument table, per graphics or kernel function" + // The tuple is (sampled, storage). + // The default limit split in WebGPU is 80%-20%. + // - The default maxSampledTexturesPerShaderStage in WebGPU is 16. + // - The default maxStorageTexturesPerShaderStage in WebGPU is 4. + // Use a split of 75%-25% which can exactly split 128 and 96. + max_textures_per_stage: if os_type == super::OsType::Macos + || (family_check && device.supportsFamily(MTLGPUFamily::Apple6)) + { + (96, 32) // 128 + } else if family_check && device.supportsFamily(MTLGPUFamily::Apple4) { + (72, 24) // 96 + } else { + (23, 8) // 31 + }, + max_binding_array_elements: if argument_buffers == Some(MTLArgumentBuffersTier::Tier2) { + 1_000_000 + } else if family_check && device.supportsFamily(MTLGPUFamily::Apple4) { + 96 + } else { + 31 + }, + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=7 + max_sampler_binding_array_elements: if family_check + && device.supportsFamily(MTLGPUFamily::Apple9) + { + 500_000 + } else if family_check + && (device.supportsFamily(MTLGPUFamily::Apple7) + || device.supportsFamily(MTLGPUFamily::Mac2)) + { + 1024 + } else if family_check && device.supportsFamily(MTLGPUFamily::Apple6) { + 128 + } else { + 16 + }, + // "Buffer alignment for copying an existing texture to a buffer" + buffer_alignment: if matches!(os_type, super::OsType::Macos | super::OsType::VisionOs) { + 256 + } else if family_check && device.supportsFamily(MTLGPUFamily::Apple3) { + 16 + } else { + 64 + }, + // "Minimum constant buffer offset alignment" + constant_buffer_offset_alignment: if matches!( + os_type, + super::OsType::Macos | super::OsType::VisionOs + ) { + 256 + } else if device.supportsFeatureSet(MTLFeatureSet::macOS_GPUFamily2_v1) { + 32 + } else { + 4 + }, + max_buffer_size: if available!(macos = 10.14, ios = 12.0, tvos = 12.0, visionos = 1.0) { + device.maxBufferLength() as u64 + } else if os_type == super::OsType::Macos { + 1 << 30 // 1GB on macOS 10.11 and up + } else { + 1 << 28 // 256MB on iOS 8.0+ + }, + // "Maximum 1D texture width" & + // "Maximum 2D texture width and height" & + // "Maximum cube map texture width and height" + max_texture_size: if family_check && device.supportsFamily(MTLGPUFamily::Apple10) { + 32768 + } else if Self::supports_any( + device, + &[ + MTLFeatureSet::iOS_GPUFamily3_v1, + MTLFeatureSet::tvOS_GPUFamily2_v1, + MTLFeatureSet::macOS_GPUFamily1_v1, + ], + ) { + 16384 + } else { + 8192 + }, + // "Maximum 3D texture width, height, and depth" + max_texture_3d_size: 2048, + // "Maximum number of layers per 1D texture array, 2D texture array, or 3D texture" + max_texture_layers: 2048, + max_fragment_input_components: if os_type == super::OsType::Macos + || device.supportsFeatureSet(MTLFeatureSet::iOS_GPUFamily4_v1) + { + 124 + } else { + 60 + }, + // "Maximum number of color render targets per render pass descriptor" + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=7 + max_color_render_targets: if Self::supports_any( + device, + &[ + MTLFeatureSet::iOS_GPUFamily2_v1, + MTLFeatureSet::tvOS_GPUFamily1_v1, + MTLFeatureSet::macOS_GPUFamily1_v1, + ], + ) { + 8 + } else { + 4 + }, + // "Maximum total render target size, per pixel, when using multiple color render targets" + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=7 + max_color_attachment_bytes_per_sample: if family_check + && device.supportsFamily(MTLGPUFamily::Apple4) + { + 64 // 512 bits + } else if family_check && device.supportsFamily(MTLGPUFamily::Apple2) { + 32 // 256 bits + } else if device.supportsFeatureSet(MTLFeatureSet::macOS_GPUFamily1_v1) { + // No Limit, use max_color_render_targets * MAX_TARGET_PIXEL_BYTE_COST + 8 * wgt::TextureFormat::MAX_TARGET_PIXEL_BYTE_COST as u8 // 1024 bits + } else { + 16 // 128 bits + }, + // This limit is the minimum of: + // - "Maximum scalar or vector inputs to a fragment function" + // - "Maximum number of input components to a fragment function" / 4 + // + // On non-Apple GPUs only, the Metal validation layers will error with: + // + // "number of shader varying components (125) exceeds limit (124). + // Note that on macOS the following attributes count towards the + // limit: [[position]], [[clip_distance]], [[point_size]], + // [[point_coord]], and, when read in the fragment shader, + // [[viewport_array_index]] & [[render_target_array_index]]." + // + // if the limit in the feature tables is crossed while also using + // one of the built-ins mentioned above. + // + // Note that the error says "on macOS" but it actually only applies + // to non-Apple GPUs. + // + // We only need to account for the position built-in since the others + // are either not used or they already count towards the limit + // calculation as specified by WebGPU. + max_inter_stage_shader_variables: if family_check + && device.supportsFamily(MTLGPUFamily::Apple4) + { + 31 // min(124, 124 / 4) + } else if device.supportsFeatureSet(MTLFeatureSet::macOS_GPUFamily1_v1) { + 30 // min(32, 124 / 4) - 1 + } else { + 15 // min(60, 60 / 4) + }, + // "Maximum threads per threadgroup" + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=6 + // These are older checks but still hold true; no entry in this table supports + // more than 1024 threads. + // **Note:** this is used to emit the `[[max_total_threads_per_threadgroup]]` + // attribute. This is how we guarantee that compute dispatches will always run + // and not silently fail due to hardware register pressure or occupancy limits. + max_threads_per_group: if Self::supports_any( + device, + &[ + MTLFeatureSet::iOS_GPUFamily4_v1, + MTLFeatureSet::macOS_GPUFamily1_v1, + ], + ) { + 1024 + } else { + 512 + }, + // "Maximum total threadgroup memory allocation" + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=6 + // These are older checks but still hold true; no entry in this table supports + // more than 32kb. + max_total_threadgroup_memory: if Self::supports_any( + device, + &[ + MTLFeatureSet::iOS_GPUFamily4_v1, + MTLFeatureSet::macOS_GPUFamily1_v1, + ], + ) { + 32 << 10 + } else { + 16 << 10 + }, + sample_count_mask, + supports_debug_markers: Self::supports_any( + device, + &[ + MTLFeatureSet::macOS_GPUFamily1_v2, + MTLFeatureSet::iOS_GPUFamily1_v3, + MTLFeatureSet::tvOS_GPUFamily1_v2, + ], + ), + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=4 + supports_binary_archives: family_check + && (metal3 + || device.supportsFamily(MTLGPUFamily::Apple3) + || device.supportsFamily(MTLGPUFamily::Mac2)), + // This is just trusted blindly since docs referencing supports_any have been removed + // but we don't want to remove feature support. + supports_arrays_of_textures: Self::supports_any( + device, + &[ + MTLFeatureSet::iOS_GPUFamily3_v2, + MTLFeatureSet::tvOS_GPUFamily2_v1, + MTLFeatureSet::macOS_GPUFamily1_v3, + ], + ), + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=3 + supports_arrays_of_textures_write: family_check + && (metal3 + || device.supportsFamily(MTLGPUFamily::Apple6) + || device.supportsFamily(MTLGPUFamily::Mac2)), + // Depth clipping is supported on all macOS GPU families and iOS family 4 and later + supports_depth_clip_control: os_type == super::OsType::Macos + || device.supportsFeatureSet(MTLFeatureSet::iOS_GPUFamily4_v1), + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=4 + supports_shader_primitive_index: family_check + && (metal3 + || device.supportsFamily(MTLGPUFamily::Apple7) + || device.supportsFamily(MTLGPUFamily::Mac2)), + // https://developer.apple.com/documentation/metal/mtldevice/hasunifiedmemory + has_unified_memory: if available!( + macos = 10.15, + ios = 13.0, + tvos = 13.0, + visionos = 1.0 + ) && device_class_responds_to(device, sel!(hasUnifiedMemory)) + { + Some(device.hasUnifiedMemory()) + } else { + None + }, + timestamp_query_support, + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=4 + supports_simd_scoped_operations: family_check + && (metal3 + || device.supportsFamily(MTLGPUFamily::Mac2) + || device.supportsFamily(MTLGPUFamily::Apple7)), + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=4 + int64: family_check && (metal3 || device.supportsFamily(MTLGPUFamily::Apple3)), + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=4 + // There is also a footnote that says + // "Some GPU devices in the Apple8 family support 64-bit atomic minimum and maximum..." + int64_atomics_min_max: family_check + && (device.supportsFamily(MTLGPUFamily::Apple9) + || (device.supportsFamily(MTLGPUFamily::Apple8) + && device.supportsFamily(MTLGPUFamily::Mac2))), + int64_atomics: family_check && device.supportsFamily(MTLGPUFamily::Apple9), + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=4 + float_atomics: family_check + && (metal3 + || device.supportsFamily(MTLGPUFamily::Apple7) + || device.supportsFamily(MTLGPUFamily::Mac2)), + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=5 (footnote) + // Supported on some Metal4, Apple7, Mac2, and some other platforms can be queried with device.supportsShaderBarycentricCoordinates(). + shader_barycentrics: metal4 + || (family_check + && (device.supportsFamily(MTLGPUFamily::Apple7) + || device.supportsFamily(MTLGPUFamily::Mac2))) + || (available!(macos = 10.15, ios = 14.0, tvos = 16.0, visionos = 1.0) + && device_class_responds_to( + device, + sel!(supportsShaderBarycentricCoordinates), + ) + && device.supportsShaderBarycentricCoordinates()), + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=3 + // See https://github.com/gfx-rs/wgpu/pull/8725 for more details + supports_memoryless_storage: metal4 + || if family_check { + // Apple A7 (MTLGPUFamily::Apple1) has been tested to have support. + device.supportsFamily(MTLGPUFamily::Apple1) + } else { + // macOS: Always rely on family check + // iOS/tvOS: API added in 10.0 + // visionOS: Always rely on family check + available!(ios = 10.0, tvos = 10.0) + }, + supported_vertex_amplification_factor: { + let mut factor = 1; + // https://developer.apple.com/documentation/metal/mtldevice/supportsvertexamplificationcount(_:) + if available!(macos = 10.15.4, ios = 13.0, tvos = 16.0, visionos = 1.0) { + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=8 + // The table specifies either none, 2, 8, or unsupported, implying it is a relatively small power of 2 + // The bitmask only uses 32 bits, so it can't be higher even if the device for some reason claims to support that. + while factor < 32 && device.supportsVertexAmplificationCount(factor * 2) { + factor *= 2 + } + } + factor as u32 + }, + // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=4 + mesh_shaders, + max_task_workgroup_count: if mesh_shaders + && (metal4 || device.supportsFamily(MTLGPUFamily::Apple2)) + { + u32::MAX + } else if mesh_shaders { + 1024 + } else { + 0 + }, + max_mesh_workgroup_count: if mesh_shaders + && device.supportsFamily(MTLGPUFamily::Apple10) + { + 2u32.pow(22) + } else if mesh_shaders && device.supportsFamily(MTLGPUFamily::Apple9) { + 2u32.pow(20) + } else if mesh_shaders { + 1024 + } else { + 0 + }, + max_task_payload_size: if mesh_shaders { 16384 - 32 } else { 0 }, + supports_cooperative_matrix: family_check + && (device.supportsFamily(MTLGPUFamily::Apple7) + || device.supportsFamily(MTLGPUFamily::Mac2)), + // https://developer.apple.com/documentation/metal/mtlresidencyset + supports_raytracing: if available!( + macos = 15.0, + ios = 18.0, + tvos = 18.0, + visionos = 2.0, + ) { + device_class_responds_to(device, sel!(supportsRaytracing)) + && device.supportsRaytracing() + && device_class_responds_to(device, sel!(supportsRaytracingFromRender)) + && device.supportsRaytracingFromRender() + } else { + false + }, + shader_per_vertex: family_check && device.supportsFamily(MTLGPUFamily::Apple10), + + //https://developer.apple.com/documentation/metal/mtltexturetype/type2dmultisamplearray + supports_multisample_array: available!( + macos = 10.14, + ios = 14.0, + tvos = 16.0, + visionos = 1.0 + ), + } + } + + pub fn features(&self) -> wgt::Features { + use wgt::Features as F; + + let mut features = F::empty() + | F::MAPPABLE_PRIMARY_BUFFERS + | F::VERTEX_WRITABLE_STORAGE + | F::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES + | F::IMMEDIATES + | F::POLYGON_MODE_LINE + | F::CLEAR_TEXTURE + | F::TEXTURE_FORMAT_16BIT_NORM + | F::SHADER_F16 + | F::SHADER_I16 + | F::DEPTH32FLOAT_STENCIL8 + | F::BGRA8UNORM_STORAGE + | F::PASSTHROUGH_SHADERS + | F::EXTERNAL_TEXTURE; + + features.set(F::FLOAT32_FILTERABLE, self.supports_float_filtering); + features.set(F::FLOAT32_BLENDABLE, true); + features.set(F::INDIRECT_FIRST_INSTANCE, self.indirect_draw_dispatch); + features.set( + F::TIMESTAMP_QUERY, + self.timestamp_query_support + .contains(TimestampQuerySupport::STAGE_BOUNDARIES), + ); + features.set( + F::TIMESTAMP_QUERY_INSIDE_ENCODERS, + self.timestamp_query_support + .contains(TimestampQuerySupport::ON_BLIT_ENCODER), + ); + features.set( + F::TIMESTAMP_QUERY_INSIDE_PASSES, + self.timestamp_query_support + .contains(TimestampQuerySupport::INSIDE_WGPU_PASSES), + ); + features.set(F::CLIP_DISTANCES, true); + features.set( + F::DUAL_SOURCE_BLENDING, + self.msl_version >= MTLLanguageVersion::Version1_2 && self.dual_source_blending, + ); + features.set(F::TEXTURE_COMPRESSION_ASTC, self.format_astc); + features.set(F::TEXTURE_COMPRESSION_ASTC_HDR, self.format_astc_hdr); + features.set(F::TEXTURE_COMPRESSION_ASTC_SLICED_3D, self.format_astc_3d); + features.set(F::TEXTURE_COMPRESSION_BC, self.format_bc); + features.set(F::TEXTURE_COMPRESSION_BC_SLICED_3D, self.format_bc); // BC guarantees Sliced 3D + features.set(F::TEXTURE_COMPRESSION_ETC2, self.format_eac_etc); + + features.set(F::DEPTH_CLIP_CONTROL, self.supports_depth_clip_control); + features.set(F::PRIMITIVE_INDEX, self.supports_shader_primitive_index); + + features.set( + F::TEXTURE_BINDING_ARRAY + | F::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING + | F::STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING + | F::PARTIALLY_BOUND_BINDING_ARRAY, + self.msl_version >= MTLLanguageVersion::Version3_0 + && self.supports_arrays_of_textures + && self + .argument_buffers + .unwrap_or(MTLArgumentBuffersTier::Tier1) + >= MTLArgumentBuffersTier::Tier2, + ); + features.set( + F::BUFFER_BINDING_ARRAY, + self.msl_version >= MTLLanguageVersion::Version2_0 + && self + .argument_buffers + .unwrap_or(MTLArgumentBuffersTier::Tier1) + >= MTLArgumentBuffersTier::Tier2, + ); + features.set( + F::STORAGE_RESOURCE_BINDING_ARRAY, + self.msl_version >= MTLLanguageVersion::Version3_0 + && self.supports_arrays_of_textures + && self.supports_arrays_of_textures_write + && self + .argument_buffers + .unwrap_or(MTLArgumentBuffersTier::Tier1) + >= MTLArgumentBuffersTier::Tier2, + ); + features.set( + F::SHADER_INT64, + self.int64 && self.msl_version >= MTLLanguageVersion::Version2_3, + ); + features.set( + F::SHADER_INT64_ATOMIC_MIN_MAX, + self.int64_atomics_min_max && self.msl_version >= MTLLanguageVersion::Version2_4, + ); + features.set( + F::TEXTURE_INT64_ATOMIC, + self.int64_atomics && self.msl_version >= MTLLanguageVersion::Version3_1, + ); + features.set( + F::TEXTURE_ATOMIC, + self.msl_version >= MTLLanguageVersion::Version3_1, + ); + features.set( + F::SHADER_FLOAT32_ATOMIC, + self.float_atomics && self.msl_version >= MTLLanguageVersion::Version3_0, + ); + features.set( + F::MEMORY_DECORATION_COHERENT, + self.msl_version >= MTLLanguageVersion::Version3_2, + ); + + features.set( + F::ADDRESS_MODE_CLAMP_TO_BORDER, + self.sampler_clamp_to_border, + ); + features.set(F::ADDRESS_MODE_CLAMP_TO_ZERO, true); + + features.set(F::RG11B10UFLOAT_RENDERABLE, self.format_rg11b10_all); + + features.set( + F::SHADER_BARYCENTRICS, + self.shader_barycentrics && self.msl_version >= MTLLanguageVersion::Version2_2, + ); + + features.set(F::SHADER_PER_VERTEX, self.shader_per_vertex); + + if self.supports_simd_scoped_operations { + features.insert(F::SUBGROUP | F::SUBGROUP_BARRIER); + } + + features.set( + F::EXPERIMENTAL_MESH_SHADER | F::EXPERIMENTAL_MESH_SHADER_POINTS, + self.mesh_shaders, + ); + features.set( + F::EXPERIMENTAL_MESH_SHADER_MULTIVIEW, + self.supported_vertex_amplification_factor > 1 && self.mesh_shaders, + ); + + // Cooperative matrix (simdgroup matrix) requires MSL 2.3+ + features.set( + F::EXPERIMENTAL_COOPERATIVE_MATRIX, + self.supports_cooperative_matrix && self.msl_version >= MTLLanguageVersion::Version2_3, + ); + + if self.supported_vertex_amplification_factor > 1 { + features.insert(F::MULTIVIEW); + } + + features.set(F::EXPERIMENTAL_RAY_QUERY, self.supports_raytracing); + + features.set(F::MULTISAMPLE_ARRAY, self.supports_multisample_array); + + features + } + + pub fn capabilities(&self, instance_flags: wgt::InstanceFlags) -> crate::Capabilities { + let mut downlevel = wgt::DownlevelCapabilities::default(); + downlevel.flags.set( + wgt::DownlevelFlags::FRAGMENT_WRITABLE_STORAGE, + self.fragment_rw_storage, + ); + downlevel.flags.set( + wgt::DownlevelFlags::CUBE_ARRAY_TEXTURES, + self.texture_cube_array, + ); + // TODO: separate the mutable comparisons from immutable ones + downlevel.flags.set( + wgt::DownlevelFlags::COMPARISON_SAMPLERS, + self.mutable_comparison_samplers, + ); + downlevel.flags.set( + wgt::DownlevelFlags::INDIRECT_EXECUTION, + self.indirect_draw_dispatch, + ); + // TODO: add another flag for `first_instance` + downlevel.flags.set( + wgt::DownlevelFlags::BASE_VERTEX, + self.base_vertex_first_instance_drawing, + ); + downlevel + .flags + .set(wgt::DownlevelFlags::ANISOTROPIC_FILTERING, true); + + downlevel.flags.set( + wgt::DownlevelFlags::MSL2_1, + self.msl_version >= MTLLanguageVersion::Version2_1, + ); + downlevel.flags.set( + wgt::DownlevelFlags::TEXTURE_COMPRESSION, + self.format_bc || (self.format_eac_etc && self.format_astc), + ); + + let max_storage_buffers_per_shader_stage; + let max_uniform_buffers_per_shader_stage; + let max_vertex_buffers; + let max_acceleration_structures_per_shader_stage; + let max_buffers_and_acceleration_structures_per_shader_stage; + + // Metal has a single buffer limit that we must split across 3 WebGPU limits: + // - maxStorageBuffersPerShaderStage; must be at least 8 + // - maxUniformBuffersPerShaderStage; must be at least 12 + // - maxVertexBuffers; must be at least 8 + // We also have to reserve 2 additional internal buffers: + // - one for immediate data + // - one for sizes of other buffers + if instance_flags.contains(wgt::InstanceFlags::STRICT_WEBGPU_COMPLIANCE) { + max_storage_buffers_per_shader_stage = 9; + max_uniform_buffers_per_shader_stage = 12; + max_vertex_buffers = 8; + max_acceleration_structures_per_shader_stage = 0; + max_buffers_and_acceleration_structures_per_shader_stage = u32::MAX; + } else { + const MAX_USABLE_BUFFERS: u32 = MAX_BUFFERS - 2; + max_storage_buffers_per_shader_stage = MAX_USABLE_BUFFERS; + max_uniform_buffers_per_shader_stage = MAX_USABLE_BUFFERS; + max_vertex_buffers = MAX_USABLE_BUFFERS; + max_acceleration_structures_per_shader_stage = MAX_USABLE_BUFFERS; + max_buffers_and_acceleration_structures_per_shader_stage = MAX_USABLE_BUFFERS; + } + + let limits = crate::auxil::adjust_raw_limits(wgt::Limits { + // + // WebGPU LIMITS: + // Based on https://gpuweb.github.io/gpuweb/correspondence/#limits + // + max_texture_dimension_1d: self.max_texture_size as u32, + max_texture_dimension_2d: self.max_texture_size as u32, + max_texture_dimension_3d: self.max_texture_3d_size as u32, + max_texture_array_layers: self.max_texture_layers as u32, + // No limit. + max_bind_groups: u32::MAX, + // No limit. Once we start using argument buffers we should set this appropriately. + max_bind_groups_plus_vertex_buffers: u32::MAX, + // No limit. + max_bindings_per_bind_group: u32::MAX, + // No limit, use maxUniformBuffersPerShaderStage. + max_dynamic_uniform_buffers_per_pipeline_layout: max_uniform_buffers_per_shader_stage, + // No limit, use maxStorageBuffersPerShaderStage. + max_dynamic_storage_buffers_per_pipeline_layout: max_storage_buffers_per_shader_stage, + // "Maximum number of entries in the sampler state argument table, per graphics or kernel function" + max_samplers_per_shader_stage: 16, + max_sampled_textures_per_shader_stage: self.max_textures_per_stage.0, + max_storage_textures_per_shader_stage: self.max_textures_per_stage.1, + max_storage_buffers_per_shader_stage, + max_uniform_buffers_per_shader_stage, + max_vertex_buffers, + max_buffer_size: self.max_buffer_size, + // No limit, use maxBufferSize. + max_uniform_buffer_binding_size: self.max_buffer_size, + // No limit, use maxBufferSize. + max_storage_buffer_binding_size: self.max_buffer_size, + min_uniform_buffer_offset_alignment: self.constant_buffer_offset_alignment, + // No documented limit. Use 32, which is the lowest allowed value. + min_storage_buffer_offset_alignment: 32, + // "Maximum number of vertex attributes, per vertex descriptor" + max_vertex_attributes: 31, + // No documented limit, matches Vulkan's minimum limit and D3D12's static limit. + max_vertex_buffer_array_stride: 2048, + max_inter_stage_shader_variables: self.max_inter_stage_shader_variables, + max_color_attachments: self.max_color_render_targets as u32, + max_color_attachment_bytes_per_sample: self.max_color_attachment_bytes_per_sample + as u32, + max_compute_workgroup_storage_size: self.max_total_threadgroup_memory, + max_compute_invocations_per_workgroup: self.max_threads_per_group, + max_compute_workgroup_size_x: self.max_threads_per_group, + max_compute_workgroup_size_y: self.max_threads_per_group, + max_compute_workgroup_size_z: self.max_threads_per_group, + // No documented limit, matches Vulkan's minimum limit and D3D12's static limit. + max_compute_workgroups_per_dimension: 0xFFFF, + max_immediate_size: 0x1000, + // + // NATIVE (Non-WebGPU) LIMITS: + // + max_non_sampler_bindings: u32::MAX, + + max_binding_array_elements_per_shader_stage: self.max_binding_array_elements, + max_binding_array_sampler_elements_per_shader_stage: self + .max_sampler_binding_array_elements, + max_binding_array_acceleration_structure_elements_per_shader_stage: 0, + + // from https://developer.apple.com/documentation/metal/mtlaccelerationstructureusage/extendedlimits + max_blas_primitive_count: 1 << 28, + max_blas_geometry_count: 1 << 24, + max_tlas_instance_count: 1 << 24, + // From 2.17.7 in https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf + // > [Acceleration structures] are opaque objects that can be bound directly using + // buffer binding points or via argument buffers + max_acceleration_structures_per_shader_stage, + max_buffers_and_acceleration_structures_per_shader_stage, + + max_multiview_view_count: if self.supported_vertex_amplification_factor > 1 { + self.supported_vertex_amplification_factor + } else { + 0 + }, + + // Should be not too large + max_task_workgroup_total_count: self.max_task_workgroup_count, + max_task_workgroups_per_dimension: self.max_task_workgroup_count, + max_mesh_workgroup_total_count: self.max_mesh_workgroup_count, + max_mesh_workgroups_per_dimension: self.max_mesh_workgroup_count, + max_task_invocations_per_workgroup: if self.mesh_shaders { 1024 } else { 0 }, + max_task_invocations_per_dimension: if self.mesh_shaders { 1024 } else { 0 }, + max_mesh_invocations_per_workgroup: if self.mesh_shaders { 1024 } else { 0 }, + max_mesh_invocations_per_dimension: if self.mesh_shaders { 1024 } else { 0 }, + // Using certain variables or debuggers can reduce the size by 32 bytes + max_task_payload_size: self.max_task_payload_size, + max_mesh_output_vertices: 256, + max_mesh_output_primitives: 256, + max_mesh_output_layers: self.max_texture_layers as u32, + max_mesh_multiview_view_count: 0, + // unimplemented + max_ray_dispatch_count: 0, + max_ray_recursion_depth: 0, + }); + + crate::Capabilities { + limits, + alignments: crate::Alignments { + buffer_copy_offset: wgt::BufferSize::new(self.buffer_alignment).unwrap(), + buffer_copy_pitch: wgt::BufferSize::new(4).unwrap(), + // This backend has Naga incorporate bounds checks into the + // Metal Shading Language it generates, so from `wgpu_hal`'s + // users' point of view, references are tightly checked. + uniform_bounds_check_alignment: wgt::BufferSize::new(1).unwrap(), + raw_tlas_instance_size: u32::try_from(size_of::< + MTLIndirectAccelerationStructureInstanceDescriptor, + >()) + .unwrap(), + ray_tracing_scratch_buffer_alignment: 1, + // Not yet supported + ray_tracing_pipeline_group_data_size: 0, + ray_tracing_pipeline_group_data_alignment: 0, + ray_tracing_pipeline_data_offset_alignment: 0, + }, + downlevel, + cooperative_matrix_properties: self.cooperative_matrix_properties(), + } + } + + /// Returns the supported cooperative matrix configurations for Metal. + /// + /// Metal's simdgroup_matrix supports 8x8 tiles with f16 and f32 element types. + fn cooperative_matrix_properties(&self) -> Vec<wgt::CooperativeMatrixProperties> { + if !self.supports_cooperative_matrix || self.msl_version < MTLLanguageVersion::Version2_3 { + return Vec::new(); + } + + vec![ + // 8x8 f32 configuration + wgt::CooperativeMatrixProperties { + m_size: 8, + n_size: 8, + k_size: 8, + ab_type: wgt::CooperativeScalarType::F32, + cr_type: wgt::CooperativeScalarType::F32, + saturating_accumulation: false, + }, + // 8x8 f16 configuration + wgt::CooperativeMatrixProperties { + m_size: 8, + n_size: 8, + k_size: 8, + ab_type: wgt::CooperativeScalarType::F16, + cr_type: wgt::CooperativeScalarType::F16, + saturating_accumulation: false, + }, + // Mixed precision: f16 inputs, f32 accumulator + wgt::CooperativeMatrixProperties { + m_size: 8, + n_size: 8, + k_size: 8, + ab_type: wgt::CooperativeScalarType::F16, + cr_type: wgt::CooperativeScalarType::F32, + saturating_accumulation: false, + }, + ] + } + + pub fn private_capabilities(&self) -> super::PrivateCapabilities { + super::PrivateCapabilities { + msl_version: self.msl_version, + low_power: self.low_power, + headless: self.headless, + has_unified_memory: self.has_unified_memory, + timestamp_query_support: self.timestamp_query_support, + supports_memoryless_storage: self.supports_memoryless_storage, + mesh_shaders: self.mesh_shaders, + } + } + + pub fn private_texture_format_capabilities(&self) -> super::PrivateTextureFormatCapabilities { + super::PrivateTextureFormatCapabilities { + read_write_texture_tier: self.read_write_texture_tier, + sample_count_mask: self.sample_count_mask, + int64_atomics: self.int64_atomics, + msaa_desktop: self.msaa_desktop, + msaa_apple3: self.msaa_apple3, + msaa_apple7: self.msaa_apple7, + format_r32float_all: self.format_r32float_all, + format_rgba8_srgb_all: self.format_rgba8_srgb_all, + format_rgb10a2_uint_write: self.format_rgb10a2_uint_write, + format_rgb10a2_unorm_all: self.format_rgb10a2_unorm_all, + format_rg11b10_all: self.format_rg11b10_all, + format_rg32float_all: self.format_rg32float_all, + format_rgba32float_all: self.format_rgba32float_all, + format_depth16unorm: self.format_depth16unorm, + format_depth16unorm_filter: self.format_depth16unorm_filter, + format_depth32float_filter: self.format_depth32float_filter, + format_depth24_stencil8: self.format_depth24_stencil8, + format_bc: self.format_bc, + format_eac_etc: self.format_eac_etc, + format_astc: self.format_astc, + format_astc_hdr: self.format_astc_hdr, + } + } +} + +impl super::PrivateCapabilities { + pub fn device_type(&self) -> wgt::DeviceType { + if self.has_unified_memory.unwrap_or(self.low_power) { + wgt::DeviceType::IntegratedGpu + } else { + wgt::DeviceType::DiscreteGpu + } + } +} + +impl super::PrivateTextureFormatCapabilities { + pub fn map_format(&self, format: wgt::TextureFormat) -> MTLPixelFormat { + use wgt::TextureFormat as Tf; + use MTLPixelFormat as MTL; + match format { + Tf::R8Unorm => MTL::R8Unorm, + Tf::R8Snorm => MTL::R8Snorm, + Tf::R8Uint => MTL::R8Uint, + Tf::R8Sint => MTL::R8Sint, + Tf::R16Uint => MTL::R16Uint, + Tf::R16Sint => MTL::R16Sint, + Tf::R16Unorm => MTL::R16Unorm, + Tf::R16Snorm => MTL::R16Snorm, + Tf::R16Float => MTL::R16Float, + Tf::Rg8Unorm => MTL::RG8Unorm, + Tf::Rg8Snorm => MTL::RG8Snorm, + Tf::Rg8Uint => MTL::RG8Uint, + Tf::Rg8Sint => MTL::RG8Sint, + Tf::Rg16Unorm => MTL::RG16Unorm, + Tf::Rg16Snorm => MTL::RG16Snorm, + Tf::R32Uint => MTL::R32Uint, + Tf::R32Sint => MTL::R32Sint, + Tf::R32Float => MTL::R32Float, + Tf::Rg16Uint => MTL::RG16Uint, + Tf::Rg16Sint => MTL::RG16Sint, + Tf::Rg16Float => MTL::RG16Float, + Tf::Rgba8Unorm => MTL::RGBA8Unorm, + Tf::Rgba8UnormSrgb => MTL::RGBA8Unorm_sRGB, + Tf::Bgra8UnormSrgb => MTL::BGRA8Unorm_sRGB, + Tf::Rgba8Snorm => MTL::RGBA8Snorm, + Tf::Bgra8Unorm => MTL::BGRA8Unorm, + Tf::Rgba8Uint => MTL::RGBA8Uint, + Tf::Rgba8Sint => MTL::RGBA8Sint, + Tf::Rgb10a2Uint => MTL::RGB10A2Uint, + Tf::Rgb10a2Unorm => MTL::RGB10A2Unorm, + Tf::Rg11b10Ufloat => MTL::RG11B10Float, + // Ruint64 textures are emulated on metal + Tf::R64Uint => MTL::RG32Uint, + Tf::Rg32Uint => MTL::RG32Uint, + Tf::Rg32Sint => MTL::RG32Sint, + Tf::Rg32Float => MTL::RG32Float, + Tf::Rgba16Uint => MTL::RGBA16Uint, + Tf::Rgba16Sint => MTL::RGBA16Sint, + Tf::Rgba16Unorm => MTL::RGBA16Unorm, + Tf::Rgba16Snorm => MTL::RGBA16Snorm, + Tf::Rgba16Float => MTL::RGBA16Float, + Tf::Rgba32Uint => MTL::RGBA32Uint, + Tf::Rgba32Sint => MTL::RGBA32Sint, + Tf::Rgba32Float => MTL::RGBA32Float, + Tf::Stencil8 => MTL::Stencil8, + Tf::Depth16Unorm => MTL::Depth16Unorm, + Tf::Depth32Float => MTL::Depth32Float, + Tf::Depth32FloatStencil8 => MTL::Depth32Float_Stencil8, + Tf::Depth24Plus => { + if self.format_depth24_stencil8 { + MTL::Depth24Unorm_Stencil8 + } else { + MTL::Depth32Float + } + } + Tf::Depth24PlusStencil8 => { + if self.format_depth24_stencil8 { + MTL::Depth24Unorm_Stencil8 + } else { + MTL::Depth32Float_Stencil8 + } + } + Tf::NV12 => unreachable!(), + Tf::P010 => unreachable!(), + Tf::Rgb9e5Ufloat => MTL::RGB9E5Float, + Tf::Bc1RgbaUnorm => MTL::BC1_RGBA, + Tf::Bc1RgbaUnormSrgb => MTL::BC1_RGBA_sRGB, + Tf::Bc2RgbaUnorm => MTL::BC2_RGBA, + Tf::Bc2RgbaUnormSrgb => MTL::BC2_RGBA_sRGB, + Tf::Bc3RgbaUnorm => MTL::BC3_RGBA, + Tf::Bc3RgbaUnormSrgb => MTL::BC3_RGBA_sRGB, + Tf::Bc4RUnorm => MTL::BC4_RUnorm, + Tf::Bc4RSnorm => MTL::BC4_RSnorm, + Tf::Bc5RgUnorm => MTL::BC5_RGUnorm, + Tf::Bc5RgSnorm => MTL::BC5_RGSnorm, + Tf::Bc6hRgbFloat => MTL::BC6H_RGBFloat, + Tf::Bc6hRgbUfloat => MTL::BC6H_RGBUfloat, + Tf::Bc7RgbaUnorm => MTL::BC7_RGBAUnorm, + Tf::Bc7RgbaUnormSrgb => MTL::BC7_RGBAUnorm_sRGB, + Tf::Etc2Rgb8Unorm => MTL::ETC2_RGB8, + Tf::Etc2Rgb8UnormSrgb => MTL::ETC2_RGB8_sRGB, + Tf::Etc2Rgb8A1Unorm => MTL::ETC2_RGB8A1, + Tf::Etc2Rgb8A1UnormSrgb => MTL::ETC2_RGB8A1_sRGB, + Tf::Etc2Rgba8Unorm => MTL::EAC_RGBA8, + Tf::Etc2Rgba8UnormSrgb => MTL::EAC_RGBA8_sRGB, + Tf::EacR11Unorm => MTL::EAC_R11Unorm, + Tf::EacR11Snorm => MTL::EAC_R11Snorm, + Tf::EacRg11Unorm => MTL::EAC_RG11Unorm, + Tf::EacRg11Snorm => MTL::EAC_RG11Snorm, + Tf::Astc { block, channel } => match channel { + AstcChannel::Unorm => match block { + AstcBlock::B4x4 => MTL::ASTC_4x4_LDR, + AstcBlock::B5x4 => MTL::ASTC_5x4_LDR, + AstcBlock::B5x5 => MTL::ASTC_5x5_LDR, + AstcBlock::B6x5 => MTL::ASTC_6x5_LDR, + AstcBlock::B6x6 => MTL::ASTC_6x6_LDR, + AstcBlock::B8x5 => MTL::ASTC_8x5_LDR, + AstcBlock::B8x6 => MTL::ASTC_8x6_LDR, + AstcBlock::B8x8 => MTL::ASTC_8x8_LDR, + AstcBlock::B10x5 => MTL::ASTC_10x5_LDR, + AstcBlock::B10x6 => MTL::ASTC_10x6_LDR, + AstcBlock::B10x8 => MTL::ASTC_10x8_LDR, + AstcBlock::B10x10 => MTL::ASTC_10x10_LDR, + AstcBlock::B12x10 => MTL::ASTC_12x10_LDR, + AstcBlock::B12x12 => MTL::ASTC_12x12_LDR, + }, + AstcChannel::UnormSrgb => match block { + AstcBlock::B4x4 => MTL::ASTC_4x4_sRGB, + AstcBlock::B5x4 => MTL::ASTC_5x4_sRGB, + AstcBlock::B5x5 => MTL::ASTC_5x5_sRGB, + AstcBlock::B6x5 => MTL::ASTC_6x5_sRGB, + AstcBlock::B6x6 => MTL::ASTC_6x6_sRGB, + AstcBlock::B8x5 => MTL::ASTC_8x5_sRGB, + AstcBlock::B8x6 => MTL::ASTC_8x6_sRGB, + AstcBlock::B8x8 => MTL::ASTC_8x8_sRGB, + AstcBlock::B10x5 => MTL::ASTC_10x5_sRGB, + AstcBlock::B10x6 => MTL::ASTC_10x6_sRGB, + AstcBlock::B10x8 => MTL::ASTC_10x8_sRGB, + AstcBlock::B10x10 => MTL::ASTC_10x10_sRGB, + AstcBlock::B12x10 => MTL::ASTC_12x10_sRGB, + AstcBlock::B12x12 => MTL::ASTC_12x12_sRGB, + }, + AstcChannel::Hdr => match block { + AstcBlock::B4x4 => MTL::ASTC_4x4_HDR, + AstcBlock::B5x4 => MTL::ASTC_5x4_HDR, + AstcBlock::B5x5 => MTL::ASTC_5x5_HDR, + AstcBlock::B6x5 => MTL::ASTC_6x5_HDR, + AstcBlock::B6x6 => MTL::ASTC_6x6_HDR, + AstcBlock::B8x5 => MTL::ASTC_8x5_HDR, + AstcBlock::B8x6 => MTL::ASTC_8x6_HDR, + AstcBlock::B8x8 => MTL::ASTC_8x8_HDR, + AstcBlock::B10x5 => MTL::ASTC_10x5_HDR, + AstcBlock::B10x6 => MTL::ASTC_10x6_HDR, + AstcBlock::B10x8 => MTL::ASTC_10x8_HDR, + AstcBlock::B10x10 => MTL::ASTC_10x10_HDR, + AstcBlock::B12x10 => MTL::ASTC_12x10_HDR, + AstcBlock::B12x12 => MTL::ASTC_12x12_HDR, + }, + }, + } + } + + pub fn map_view_format( + &self, + format: wgt::TextureFormat, + aspects: crate::FormatAspects, + ) -> MTLPixelFormat { + use crate::FormatAspects as Fa; + use wgt::TextureFormat as Tf; + use MTLPixelFormat as MTL; + match (format, aspects) { + // map combined depth-stencil format to their stencil-only format + // see https://developer.apple.com/library/archive/documentation/Miscellaneous/Conceptual/MetalProgrammingGuide/WhatsNewiniOS10tvOS10andOSX1012/WhatsNewiniOS10tvOS10andOSX1012.html#//apple_ref/doc/uid/TP40014221-CH14-DontLinkElementID_77 + (Tf::Depth24PlusStencil8, Fa::STENCIL) => { + if self.format_depth24_stencil8 { + MTL::X24_Stencil8 + } else { + MTL::X32_Stencil8 + } + } + (Tf::Depth32FloatStencil8, Fa::STENCIL) => MTL::X32_Stencil8, + + _ => self.map_format(format), + } + } +} + +impl super::PrivateDisabilities { + pub fn new(device: &ProtocolObject<dyn MTLDevice>) -> Self { + let is_intel = device.name().to_string().starts_with("Intel"); + Self { + broken_viewport_near_depth: is_intel + && !device.supportsFeatureSet(MTLFeatureSet::macOS_GPUFamily1_v4), + broken_layered_clear_image: is_intel, + } + } +} + +impl super::OsType { + fn new(version: NSOperatingSystemVersion, device: &ProtocolObject<dyn MTLDevice>) -> Self { + // Metal was first introduced in OS X 10.11 and iOS 8. The current version number of visionOS is 1.0.0. Additionally, + // on the Simulator, Apple only provides the Apple2 GPU capability, and the Apple2+ GPU capability covers the capabilities of Apple2. + // Therefore, the following conditions can be used to determine if it is visionOS. + // https://developer.apple.com/documentation/metal/developing_metal_apps_that_run_in_simulator + let os_is_vision = version.majorVersion < 8 && device.supportsFamily(MTLGPUFamily::Apple2); + let os_is_mac = device.supportsFeatureSet(MTLFeatureSet::macOS_GPUFamily1_v1); + let os_is_tvos = device.supportsFeatureSet(MTLFeatureSet::tvOS_GPUFamily1_v1); + if os_is_vision { + Self::VisionOs + } else if os_is_mac { + Self::Macos + } else if os_is_tvos { + Self::Tvos + } else { + Self::Ios + } + } +} diff --git a/third_party/wgpu-hal/src/metal/command.rs b/third_party/wgpu-hal/src/metal/command.rs new file mode 100644 index 000000000..ba41a6312 --- /dev/null +++ b/third_party/wgpu-hal/src/metal/command.rs @@ -0,0 +1,1979 @@ +use objc2::{ + rc::{autoreleasepool, Retained}, + runtime::ProtocolObject, +}; +use objc2_foundation::{NSRange, NSString, NSUInteger}; +use objc2_metal::{ + MTLAccelerationStructure, MTLAccelerationStructureCommandEncoder, MTLBlitCommandEncoder, + MTLBlitPassDescriptor, MTLBuffer, MTLCommandBuffer, MTLCommandBufferStatus, MTLCommandEncoder, + MTLCommandQueue, MTLComputeCommandEncoder, MTLComputePassDescriptor, MTLCounterDontSample, + MTLDevice, MTLLoadAction, MTLPrimitiveType, MTLRenderCommandEncoder, MTLRenderPassDescriptor, + MTLResidencySet, MTLResidencySetDescriptor, MTLSamplerState, MTLScissorRect, MTLSize, + MTLStoreAction, MTLTexture, MTLVertexAmplificationViewMapping, MTLViewport, + MTLVisibilityResultMode, +}; + +use super::{ + adapter::{self, MAX_BUFFERS}, + conv, TimestampQuerySupport, +}; +use crate::CommandEncoder as _; +use alloc::{ + borrow::{Cow, ToOwned as _}, + sync::Arc, + vec::Vec, +}; +use core::{ops::Range, ptr::NonNull, sync::atomic}; +use smallvec::SmallVec; + +// has to match `Temp::binding_sizes` +const WORD_SIZE: usize = 4; + +impl Default for super::CommandState { + fn default() -> Self { + Self { + blit: None, + acceleration_structure_builder: None, + render: None, + compute: None, + raw_primitive_type: MTLPrimitiveType::Point, + index: None, + stage_infos: Default::default(), + storage_buffer_length_map: Default::default(), + vertex_buffer_size_map: Default::default(), + immediates: Vec::new(), + pending_timer_queries: Vec::new(), + } + } +} + +/// Helper for passing encoders to `update_bind_group_state`. +/// +/// Combines [`naga::ShaderStage`] and an encoder of the appropriate type for +/// that stage. +enum Encoder<'e> { + Vertex(&'e ProtocolObject<dyn MTLRenderCommandEncoder>), + Fragment(&'e ProtocolObject<dyn MTLRenderCommandEncoder>), + Task(&'e ProtocolObject<dyn MTLRenderCommandEncoder>), + Mesh(&'e ProtocolObject<dyn MTLRenderCommandEncoder>), + Compute(&'e ProtocolObject<dyn MTLComputeCommandEncoder>), +} + +impl Encoder<'_> { + fn stage(&self) -> naga::ShaderStage { + match self { + Self::Vertex(_) => naga::ShaderStage::Vertex, + Self::Fragment(_) => naga::ShaderStage::Fragment, + Self::Task(_) => naga::ShaderStage::Task, + Self::Mesh(_) => naga::ShaderStage::Mesh, + Self::Compute(_) => naga::ShaderStage::Compute, + } + } + + fn set_buffer( + &self, + buffer: Option<&ProtocolObject<dyn MTLBuffer>>, + offset: NSUInteger, + index: NSUInteger, + ) { + unsafe { + match *self { + Self::Vertex(enc) => enc.setVertexBuffer_offset_atIndex(buffer, offset, index), + Self::Fragment(enc) => enc.setFragmentBuffer_offset_atIndex(buffer, offset, index), + Self::Task(enc) => enc.setObjectBuffer_offset_atIndex(buffer, offset, index), + Self::Mesh(enc) => enc.setMeshBuffer_offset_atIndex(buffer, offset, index), + Self::Compute(enc) => enc.setBuffer_offset_atIndex(buffer, offset, index), + } + } + } + + fn set_acceleration_structure( + &self, + buffer: Option<&ProtocolObject<dyn MTLAccelerationStructure>>, + index: NSUInteger, + ) { + unsafe { + match *self { + Self::Vertex(enc) => { + enc.setVertexAccelerationStructure_atBufferIndex(buffer, index) + } + Self::Fragment(enc) => { + enc.setFragmentAccelerationStructure_atBufferIndex(buffer, index) + } + Self::Task(_) => { + unreachable!("Acceleration structures are not allowed in task shaders") + } + Self::Mesh(_) => { + unreachable!("Acceleration structures are not allowed in mesh shaders") + } + Self::Compute(enc) => enc.setAccelerationStructure_atBufferIndex(buffer, index), + } + } + } + + fn set_bytes(&self, bytes: NonNull<core::ffi::c_void>, length: NSUInteger, index: NSUInteger) { + unsafe { + match *self { + Self::Vertex(enc) => enc.setVertexBytes_length_atIndex(bytes, length, index), + Self::Fragment(enc) => enc.setFragmentBytes_length_atIndex(bytes, length, index), + Self::Task(enc) => enc.setObjectBytes_length_atIndex(bytes, length, index), + Self::Mesh(enc) => enc.setMeshBytes_length_atIndex(bytes, length, index), + Self::Compute(enc) => enc.setBytes_length_atIndex(bytes, length, index), + } + } + } + + fn set_sampler_state( + &self, + state: Option<&ProtocolObject<dyn MTLSamplerState>>, + index: NSUInteger, + ) { + unsafe { + match *self { + Self::Vertex(enc) => enc.setVertexSamplerState_atIndex(state, index), + Self::Fragment(enc) => enc.setFragmentSamplerState_atIndex(state, index), + Self::Task(enc) => enc.setObjectSamplerState_atIndex(state, index), + Self::Mesh(enc) => enc.setMeshSamplerState_atIndex(state, index), + Self::Compute(enc) => enc.setSamplerState_atIndex(state, index), + } + } + } + + fn set_texture(&self, texture: Option<&ProtocolObject<dyn MTLTexture>>, index: NSUInteger) { + unsafe { + match *self { + Self::Vertex(enc) => enc.setVertexTexture_atIndex(texture, index), + Self::Fragment(enc) => enc.setFragmentTexture_atIndex(texture, index), + Self::Task(enc) => enc.setObjectTexture_atIndex(texture, index), + Self::Mesh(enc) => enc.setMeshTexture_atIndex(texture, index), + Self::Compute(enc) => enc.setTexture_atIndex(texture, index), + } + } + } +} + +impl super::CommandEncoder { + pub fn raw_command_buffer(&self) -> Option<&ProtocolObject<dyn MTLCommandBuffer>> { + self.raw_cmd_buf.as_deref() + } + + fn enter_blit(&mut self) -> Retained<ProtocolObject<dyn MTLBlitCommandEncoder>> { + if self.state.blit.is_none() { + self.leave_acceleration_structure_builder(); + debug_assert!(self.state.render.is_none() && self.state.compute.is_none()); + let cmd_buf = self.raw_cmd_buf.as_ref().unwrap(); + + // This code path is currently unused since the written timestamp is always 0. + // We should see if we can get it working again, if not remove the code. + // + // Take care of pending timer queries. + // If we can't use `sample_counters_in_buffer` we have to create a dummy blit encoder! + // + // There is a known bug in Metal where blit encoders won't write timestamps if they don't have a blit operation. + // See https://github.com/gpuweb/gpuweb/issues/2046#issuecomment-1205793680 & https://source.chromium.org/chromium/chromium/src/+/006c4eb70c96229834bbaf271290f40418144cd3:third_party/dawn/src/dawn/native/metal/BackendMTL.mm;l=350 + // + // To make things worse: + // * what counts as a blit operation is a bit unclear, experimenting seemed to indicate that resolve_counters doesn't count. + // * in some cases (when?) using `set_start_of_encoder_sample_index` doesn't work, so we have to use `set_end_of_encoder_sample_index` instead + // + // All this means that pretty much the only *reliable* thing as of writing is to: + // * create a dummy blit encoder using set_end_of_encoder_sample_index + // * do a dummy write that is known to be not optimized out. + // * close the encoder since we used set_end_of_encoder_sample_index and don't want to get any extra stuff in there. + // * create another encoder for whatever we actually had in mind. + let supports_sample_counters_in_buffer = self + .shared + .private_caps + .timestamp_query_support + .contains(TimestampQuerySupport::ON_BLIT_ENCODER); + + if !self.state.pending_timer_queries.is_empty() && !supports_sample_counters_in_buffer { + autoreleasepool(|_| { + let descriptor = MTLBlitPassDescriptor::new(); + let mut last_query = None; + for (i, (set, index)) in self.state.pending_timer_queries.drain(..).enumerate() + { + let sba_descriptor = unsafe { + descriptor + .sampleBufferAttachments() + .objectAtIndexedSubscript(i) + }; + sba_descriptor + .setSampleBuffer(Some(set.counter_sample_buffer.as_ref().unwrap())); + + // Here be dragons: + // As mentioned above, for some reasons using the start of the encoder won't yield any results sometimes! + unsafe { + sba_descriptor.setStartOfEncoderSampleIndex(MTLCounterDontSample) + }; + unsafe { sba_descriptor.setEndOfEncoderSampleIndex(index as _) }; + + last_query = Some((set, index)); + } + let encoder = cmd_buf + .blitCommandEncoderWithDescriptor(&descriptor) + .unwrap(); + + // As explained above, we need to do some write: + // Conveniently, we have a buffer with every query set, that we can use for this for a dummy write, + // since we know that it is going to be overwritten again on timer resolve and HAL doesn't define its state before that. + let raw_range = NSRange { + location: last_query.as_ref().unwrap().1 as usize + * crate::QUERY_SIZE as usize, + length: 1, + }; + encoder.fillBuffer_range_value( + &last_query.as_ref().unwrap().0.raw_buffer, + raw_range, + 255, // Don't write 0, so it's easier to identify if something went wrong. + ); + + encoder.endEncoding(); + }); + } + + autoreleasepool(|_| { + self.state.blit = Some(cmd_buf.blitCommandEncoder().unwrap()); + }); + + #[allow(clippy::panicking_unwrap, reason = "false positive (fixed by 1.93.1)")] + let encoder = self.state.blit.as_ref().unwrap(); + + // UNTESTED: + // If the above described issue with empty blit encoder applies to `sample_counters_in_buffer` as well, we should use the same workaround instead! + for (set, index) in self.state.pending_timer_queries.drain(..) { + debug_assert!(supports_sample_counters_in_buffer); + unsafe { + encoder.sampleCountersInBuffer_atSampleIndex_withBarrier( + set.counter_sample_buffer.as_ref().unwrap(), + index as _, + true, + ) + }; + } + } + self.state.blit.as_ref().unwrap().clone() + } + + pub(super) fn leave_blit(&mut self) { + if let Some(encoder) = self.state.blit.take() { + encoder.endEncoding(); + } + } + + fn enter_acceleration_structure_builder( + &mut self, + ) -> Retained<ProtocolObject<dyn MTLAccelerationStructureCommandEncoder>> { + if self.state.acceleration_structure_builder.is_none() { + self.leave_blit(); + debug_assert!( + self.state.render.is_none() + && self.state.compute.is_none() + && self.state.blit.is_none() + ); + let cmd_buf = self.raw_cmd_buf.as_ref().unwrap(); + autoreleasepool(|_| { + self.state.acceleration_structure_builder = + cmd_buf.accelerationStructureCommandEncoder().to_owned(); + }); + } + self.state.acceleration_structure_builder.clone().unwrap() + } + + pub(super) fn leave_acceleration_structure_builder(&mut self) { + if let Some(encoder) = self.state.acceleration_structure_builder.take() { + encoder.endEncoding(); + } + } + + fn active_encoder(&mut self) -> Option<&ProtocolObject<dyn MTLCommandEncoder>> { + if let Some(ref encoder) = self.state.render { + Some(ProtocolObject::from_ref(&**encoder)) + } else if let Some(ref encoder) = self.state.acceleration_structure_builder { + Some(ProtocolObject::from_ref(&**encoder)) + } else if let Some(ref encoder) = self.state.compute { + Some(ProtocolObject::from_ref(&**encoder)) + } else if let Some(ref encoder) = self.state.blit { + Some(ProtocolObject::from_ref(&**encoder)) + } else { + None + } + } + + fn begin_pass(&mut self) { + self.state.reset(); + self.leave_blit(); + self.leave_acceleration_structure_builder(); + } + + /// Updates the bindings for a single shader stage, called in `set_bind_group`. + fn update_bind_group_state( + &mut self, + encoder: Encoder<'_>, + index_base: super::ResourceData<u32>, + bg_info: &super::BindGroupLayoutInfo, + dynamic_offsets: &[wgt::DynamicOffset], + group_index: u32, + group: &super::BindGroup, + ) { + use naga::ShaderStage as S; + let resource_indices = match encoder.stage() { + S::Vertex => &bg_info.base_resource_indices.vs, + S::Fragment => &bg_info.base_resource_indices.fs, + S::Task => &bg_info.base_resource_indices.ts, + S::Mesh => &bg_info.base_resource_indices.ms, + S::Compute => &bg_info.base_resource_indices.cs, + S::RayGeneration | S::AnyHit | S::ClosestHit | S::Miss => unimplemented!(), + }; + let buffers = match encoder.stage() { + S::Vertex => group.counters.vs.buffers, + S::Fragment => group.counters.fs.buffers, + S::Task => group.counters.ts.buffers, + S::Mesh => group.counters.ms.buffers, + S::Compute => group.counters.cs.buffers, + S::RayGeneration | S::AnyHit | S::ClosestHit | S::Miss => unimplemented!(), + }; + let mut changes_sizes_buffer = false; + for index in 0..buffers { + let res = &group.buffers[(index_base.buffers + index) as usize]; + match res { + super::BufferLikeResource::Buffer { + ptr, + mut offset, + dynamic_index, + binding_size, + binding_location, + } => { + let buffer = Some(unsafe { ptr.as_ref() }); + if let Some(dyn_index) = dynamic_index { + offset += dynamic_offsets[*dyn_index as usize] as wgt::BufferAddress; + } + let index = (resource_indices.buffers + index) as usize; + encoder.set_buffer(buffer, offset as usize, index); + let br = naga::ResourceBinding { + group: group_index, + binding: *binding_location, + }; + if let Some(size) = binding_size { + self.state.storage_buffer_length_map.insert((br, 0), *size); + changes_sizes_buffer = true; + } + } + super::BufferLikeResource::StorageBindingArray { + ptr, + array_element_sizes, + binding_location, + } => { + let buffer = Some(unsafe { ptr.as_ref() }); + let index = (resource_indices.buffers + index) as usize; + encoder.set_buffer(buffer, 0, index); + let br = naga::ResourceBinding { + group: group_index, + binding: *binding_location, + }; + for &(array_idx, size) in array_element_sizes { + self.state + .storage_buffer_length_map + .insert((br, array_idx), size); + changes_sizes_buffer = true; + } + } + super::BufferLikeResource::AccelerationStructure(ptr) => { + let buffer = Some(unsafe { ptr.as_ref() }); + let index = (resource_indices.buffers + index) as usize; + encoder.set_acceleration_structure(buffer, index); + } + } + } + if changes_sizes_buffer { + if let Some((index, sizes)) = self + .state + .make_sizes_buffer_update(encoder.stage(), &mut self.temp.binding_sizes) + { + let bytes_ptr = NonNull::new(sizes.as_ptr().cast_mut().cast()).unwrap(); + let length = sizes.len() * WORD_SIZE; + let index = index as _; + encoder.set_bytes(bytes_ptr, length, index); + } + } + let samplers = match encoder.stage() { + S::Vertex => group.counters.vs.samplers, + S::Fragment => group.counters.fs.samplers, + S::Task => group.counters.ts.samplers, + S::Mesh => group.counters.ms.samplers, + S::Compute => group.counters.cs.samplers, + S::RayGeneration | S::AnyHit | S::ClosestHit | S::Miss => unimplemented!(), + }; + for index in 0..samplers { + let res = group.samplers[(index_base.samplers + index) as usize]; + let index = (resource_indices.samplers + index) as usize; + let state = Some(unsafe { res.as_ref() }); + encoder.set_sampler_state(state, index); + } + + let textures = match encoder.stage() { + S::Vertex => group.counters.vs.textures, + S::Fragment => group.counters.fs.textures, + S::Task => group.counters.ts.textures, + S::Mesh => group.counters.ms.textures, + S::Compute => group.counters.cs.textures, + S::RayGeneration | S::AnyHit | S::ClosestHit | S::Miss => unimplemented!(), + }; + for index in 0..textures { + let res = group.textures[(index_base.textures + index) as usize]; + let index = (resource_indices.textures + index) as usize; + let texture = Some(unsafe { res.as_ref() }); + encoder.set_texture(texture, index); + } + } +} + +impl super::CommandState { + fn reset(&mut self) { + self.storage_buffer_length_map.clear(); + self.vertex_buffer_size_map.clear(); + self.stage_infos.vs.clear(); + self.stage_infos.fs.clear(); + self.stage_infos.cs.clear(); + self.stage_infos.ts.clear(); + self.stage_infos.ms.clear(); + self.immediates.clear(); + } + + fn make_sizes_buffer_update<'a>( + &self, + stage: naga::ShaderStage, + result_sizes: &'a mut Vec<u32>, + ) -> Option<(u32, &'a [u32])> { + let stage_info = &self.stage_infos[stage]; + let slot = stage_info.sizes_slot?; + + result_sizes.clear(); + result_sizes.extend(stage_info.sized_bindings.iter().map(|(br, array_idx)| { + self.storage_buffer_length_map + .get(&(*br, *array_idx)) + .map(|size| u32::try_from(size.get()).unwrap_or(u32::MAX)) + .unwrap_or_default() + })); + + // Extend with the sizes of the mapped vertex buffers, in the order + // they were added to the map. + result_sizes.extend(stage_info.vertex_buffer_mappings.iter().map(|vbm| { + self.vertex_buffer_size_map + .get(&vbm.id) + .map(|size| u32::try_from(size.get()).unwrap_or(u32::MAX)) + .unwrap_or_default() + })); + + if !result_sizes.is_empty() { + Some((slot as _, result_sizes)) + } else { + None + } + } +} + +impl crate::CommandEncoder for super::CommandEncoder { + type A = super::Api; + + unsafe fn begin_encoding(&mut self, label: crate::Label) -> Result<(), crate::DeviceError> { + let queue = &self.queue_shared.raw; + let retain_references = self.shared.settings.retain_command_buffer_references; + let relay = self.queue_shared.relay.get(); + + // Guard against exhausting Metal's command buffer budget. Use the hard + // limit (`MAX_COMMAND_BUFFERS`) so we fail before Metal can hang inside + // `new_command_buffer`. + let previous = self + .queue_shared + .command_buffer_created_not_submitted + .fetch_add(1, atomic::Ordering::AcqRel); + if previous >= adapter::MAX_COMMAND_BUFFERS { + let current = previous + 1; + log::warn!( + "metal: refusing to create new command buffer; {current} outstanding command \ + buffers exceeds the limit of {}. Treating this as device lost. \ + Ensure command encoders are submitted or dropped rather than kept alive \ + to avoid exhausting Metal's command buffer budget.", + adapter::MAX_COMMAND_BUFFERS + ); + return Err(crate::DeviceError::Lost); + } + + let raw = autoreleasepool(move |_| { + let cmd_buf_ref = if retain_references { + queue.commandBuffer() + } else { + queue.commandBufferWithUnretainedReferences() + } + .unwrap(); + if let Some(label) = label { + cmd_buf_ref.setLabel(Some(&NSString::from_str(label))); + } + // If strict event sync is enabled on this queue, gate the + // CB on the relay event at the value the next submit will + // signal. The CB pauses at the start until the relay fires. + if let Some(relay) = relay { + let expected = relay.next_release_value.load(atomic::Ordering::Acquire); + cmd_buf_ref.encodeWaitForEvent_value(relay.event.as_ref(), expected); + } + cmd_buf_ref.to_owned() + }); + + // Queries should either be closed out, or cleared in `discard_encoding`. + // This assertion is here mainly to facilitate comparison with the other + // backends, which clear in `begin_encoding` rather than `discard_encoding`. + debug_assert!(self.state.pending_timer_queries.is_empty()); + + self.raw_cmd_buf = Some(raw); + + Ok(()) + } + + unsafe fn discard_encoding(&mut self) { + self.leave_blit(); + self.leave_acceleration_structure_builder(); + // when discarding, we don't have a guarantee that + // everything is in a good state, so check carefully + if let Some(encoder) = self.state.render.take() { + encoder.endEncoding(); + } + if let Some(encoder) = self.state.compute.take() { + encoder.endEncoding(); + } + self.state.pending_timer_queries.clear(); + let had_command_buffer = self.raw_cmd_buf.is_some(); + // Clear the Option first so the underlying `metal::CommandBuffer` is + // dropped before we update the counter. + self.raw_cmd_buf = None; + if had_command_buffer { + self.queue_shared + .command_buffer_created_not_submitted + .fetch_sub(1, atomic::Ordering::AcqRel); + } + } + + unsafe fn end_encoding(&mut self) -> Result<super::CommandBuffer, crate::DeviceError> { + // Handle pending timer query if any. + if !self.state.pending_timer_queries.is_empty() { + self.leave_blit(); + self.enter_blit(); + } + + self.leave_blit(); + self.leave_acceleration_structure_builder(); + debug_assert!(self.state.render.is_none()); + debug_assert!(self.state.compute.is_none()); + debug_assert!(self.state.pending_timer_queries.is_empty()); + + Ok(super::CommandBuffer { + raw: self.raw_cmd_buf.take().unwrap(), + queue_shared: Arc::clone(&self.queue_shared), + }) + } + + unsafe fn reset_all<I>(&mut self, _cmd_bufs: I) + where + I: Iterator<Item = super::CommandBuffer>, + { + //do nothing + } + + unsafe fn transition_buffers<'a, T>(&mut self, _barriers: T) + where + T: Iterator<Item = crate::BufferBarrier<'a, super::Buffer>>, + { + } + + unsafe fn transition_textures<'a, T>(&mut self, _barriers: T) + where + T: Iterator<Item = crate::TextureBarrier<'a, super::Texture>>, + { + } + + unsafe fn clear_buffer(&mut self, buffer: &super::Buffer, range: crate::MemoryRange) { + let encoder = self.enter_blit(); + encoder.fillBuffer_range_value(&buffer.raw, conv::map_range(&range), 0); + } + + unsafe fn copy_buffer_to_buffer<T>( + &mut self, + src: &super::Buffer, + dst: &super::Buffer, + regions: T, + ) where + T: Iterator<Item = crate::BufferCopy>, + { + let encoder = self.enter_blit(); + for copy in regions { + unsafe { + encoder.copyFromBuffer_sourceOffset_toBuffer_destinationOffset_size( + &src.raw, + copy.src_offset as usize, + &dst.raw, + copy.dst_offset as usize, + copy.size.get() as usize, + ) + }; + } + } + + unsafe fn copy_texture_to_texture<T>( + &mut self, + src: &super::Texture, + _src_usage: wgt::TextureUses, + dst: &super::Texture, + regions: T, + ) where + T: Iterator<Item = crate::TextureCopy>, + { + let dst_texture = if src.format != dst.format { + let raw_format = self + .shared + .private_texture_format_caps + .map_format(src.format); + Cow::Owned(autoreleasepool(|_| { + dst.raw.newTextureViewWithPixelFormat(raw_format).unwrap() + })) + } else { + Cow::Borrowed(&dst.raw) + }; + let encoder = self.enter_blit(); + for copy in regions { + let src_origin = conv::map_origin(©.src_base.origin); + let dst_origin = conv::map_origin(©.dst_base.origin); + // no clamping is done: Metal expects physical sizes here + let extent = conv::map_copy_extent(©.size); + unsafe { + encoder.copyFromTexture_sourceSlice_sourceLevel_sourceOrigin_sourceSize_toTexture_destinationSlice_destinationLevel_destinationOrigin( + &src.raw, + copy.src_base.array_layer as usize, + copy.src_base.mip_level as usize, + src_origin, + extent, + &dst_texture, + copy.dst_base.array_layer as usize, + copy.dst_base.mip_level as usize, + dst_origin, + ) + }; + } + } + + unsafe fn copy_buffer_to_texture<T>( + &mut self, + src: &super::Buffer, + dst: &super::Texture, + regions: T, + ) where + T: Iterator<Item = crate::BufferTextureCopy>, + { + let encoder = self.enter_blit(); + for copy in regions { + let dst_origin = conv::map_origin(©.texture_base.origin); + // Metal expects buffer-texture copies in virtual sizes + let extent = copy + .texture_base + .max_copy_size(&dst.copy_size) + .min(©.size); + let bytes_per_row = copy.buffer_layout.bytes_per_row.unwrap_or(0) as u64; + let image_byte_stride = if extent.depth > 1 { + copy.buffer_layout + .rows_per_image + .map_or(0, |v| v as u64 * bytes_per_row) + } else { + // Don't pass a stride when updating a single layer, otherwise metal validation + // fails when updating a subset of the image due to the stride being larger than + // the amount of data to copy. + 0 + }; + unsafe { + encoder.copyFromBuffer_sourceOffset_sourceBytesPerRow_sourceBytesPerImage_sourceSize_toTexture_destinationSlice_destinationLevel_destinationOrigin_options( + &src.raw, + copy.buffer_layout.offset as usize, + bytes_per_row as usize, + image_byte_stride as usize, + conv::map_copy_extent(&extent), + &dst.raw, + copy.texture_base.array_layer as usize, + copy.texture_base.mip_level as usize, + dst_origin, + conv::get_blit_option(dst.format, copy.texture_base.aspect), + ) + }; + } + } + + unsafe fn copy_texture_to_buffer<T>( + &mut self, + src: &super::Texture, + _src_usage: wgt::TextureUses, + dst: &super::Buffer, + regions: T, + ) where + T: Iterator<Item = crate::BufferTextureCopy>, + { + let encoder = self.enter_blit(); + for copy in regions { + let src_origin = conv::map_origin(©.texture_base.origin); + // Metal expects texture-buffer copies in virtual sizes + let extent = copy + .texture_base + .max_copy_size(&src.copy_size) + .min(©.size); + let bytes_per_row = copy.buffer_layout.bytes_per_row.unwrap_or(0) as u64; + let bytes_per_image = copy + .buffer_layout + .rows_per_image + .map_or(0, |v| v as u64 * bytes_per_row); + unsafe { + encoder.copyFromTexture_sourceSlice_sourceLevel_sourceOrigin_sourceSize_toBuffer_destinationOffset_destinationBytesPerRow_destinationBytesPerImage_options( + &src.raw, + copy.texture_base.array_layer as usize, + copy.texture_base.mip_level as usize, + src_origin, + conv::map_copy_extent(&extent), + &dst.raw, + copy.buffer_layout.offset as usize, + bytes_per_row as usize, + bytes_per_image as usize, + conv::get_blit_option(src.format, copy.texture_base.aspect), + ) + }; + } + } + + unsafe fn copy_acceleration_structure_to_acceleration_structure( + &mut self, + src: &super::AccelerationStructure, + dst: &super::AccelerationStructure, + copy: wgt::AccelerationStructureCopy, + ) { + let command_encoder = self.enter_acceleration_structure_builder(); + match copy { + wgt::AccelerationStructureCopy::Clone => unsafe { + command_encoder + .copyAccelerationStructure_toAccelerationStructure(&src.raw, &dst.raw); + }, + wgt::AccelerationStructureCopy::Compact => { + command_encoder.copyAndCompactAccelerationStructure_toAccelerationStructure( + &src.raw, &dst.raw, + ); + } + }; + } + + unsafe fn begin_query(&mut self, set: &super::QuerySet, index: u32) { + match set.ty { + wgt::QueryType::Occlusion => { + self.state + .render + .as_ref() + .unwrap() + .setVisibilityResultMode_offset( + MTLVisibilityResultMode::Boolean, + index as usize * crate::QUERY_SIZE as usize, + ); + } + _ => {} + } + } + unsafe fn end_query(&mut self, set: &super::QuerySet, index: u32) { + match set.ty { + wgt::QueryType::Occlusion => { + self.state + .render + .as_ref() + .unwrap() + .setVisibilityResultMode_offset( + MTLVisibilityResultMode::Disabled, + index as usize * crate::QUERY_SIZE as usize, + ); + } + _ => {} + } + } + unsafe fn write_timestamp(&mut self, set: &super::QuerySet, index: u32) { + let support = self.shared.private_caps.timestamp_query_support; + debug_assert!( + support.contains(TimestampQuerySupport::STAGE_BOUNDARIES), + "Timestamp queries are not supported" + ); + let sample_buffer = set.counter_sample_buffer.as_ref().unwrap(); + let with_barrier = true; + + // Try to use an existing encoder for timestamp query if possible. + // This works only if it's supported for the active encoder. + if let (true, Some(encoder)) = ( + support.contains(TimestampQuerySupport::ON_BLIT_ENCODER), + self.state.blit.as_ref(), + ) { + unsafe { + encoder.sampleCountersInBuffer_atSampleIndex_withBarrier( + sample_buffer, + index as _, + with_barrier, + ) + }; + } else if let (true, Some(encoder)) = ( + support.contains(TimestampQuerySupport::ON_RENDER_ENCODER), + self.state.render.as_ref(), + ) { + unsafe { + encoder.sampleCountersInBuffer_atSampleIndex_withBarrier( + sample_buffer, + index as _, + with_barrier, + ) + }; + } else if let (true, Some(encoder)) = ( + support.contains(TimestampQuerySupport::ON_COMPUTE_ENCODER), + self.state.compute.as_ref(), + ) { + unsafe { + encoder.sampleCountersInBuffer_atSampleIndex_withBarrier( + sample_buffer, + index as _, + with_barrier, + ) + }; + } else { + // If we're here it means we either have no encoder open, or it's not supported to sample within them. + // If this happens with render/compute open, this is an invalid usage! + debug_assert!(self.state.render.is_none() && self.state.compute.is_none()); + + // But otherwise it means we'll put defer this to the next created encoder. + self.state.pending_timer_queries.push((set.clone(), index)); + + // Ensure we didn't already have a blit open. + self.leave_blit(); + }; + } + + unsafe fn reset_queries(&mut self, _set: &super::QuerySet, _range: Range<u32>) {} + + unsafe fn copy_query_results( + &mut self, + set: &super::QuerySet, + range: Range<u32>, + buffer: &super::Buffer, + offset: wgt::BufferAddress, + _: wgt::BufferSize, // Metal doesn't support queries that are bigger than a single element are not supported + ) { + let encoder = self.enter_blit(); + match set.ty { + wgt::QueryType::Occlusion => { + let size = (range.end - range.start) as u64 * crate::QUERY_SIZE; + unsafe { + encoder.copyFromBuffer_sourceOffset_toBuffer_destinationOffset_size( + &set.raw_buffer, + range.start as usize * crate::QUERY_SIZE as usize, + &buffer.raw, + offset as usize, + size as usize, + ) + }; + } + wgt::QueryType::Timestamp => { + unsafe { + encoder.resolveCounters_inRange_destinationBuffer_destinationOffset( + set.counter_sample_buffer.as_ref().unwrap(), + NSRange::new(range.start as usize, (range.end - range.start) as usize), + &buffer.raw, + offset as usize, + ) + }; + } + wgt::QueryType::PipelineStatistics(_) => todo!(), + } + } + + // render + + unsafe fn begin_render_pass( + &mut self, + desc: &crate::RenderPassDescriptor<super::QuerySet, super::TextureView>, + ) -> Result<(), crate::DeviceError> { + self.begin_pass(); + self.state.index = None; + + assert!(self.state.blit.is_none()); + assert!(self.state.compute.is_none()); + assert!(self.state.render.is_none()); + + autoreleasepool(|_| { + let descriptor = MTLRenderPassDescriptor::new(); + + for (i, at) in desc.color_attachments.iter().enumerate() { + if let Some(at) = at.as_ref() { + let at_descriptor = + unsafe { descriptor.colorAttachments().objectAtIndexedSubscript(i) }; + at_descriptor.setTexture(Some(&at.target.view.raw)); + if let Some(depth_slice) = at.depth_slice { + at_descriptor.setDepthPlane(depth_slice as usize); + } + if let Some(ref resolve) = at.resolve_target { + //Note: the selection of levels and slices is already handled by `TextureView` + at_descriptor.setResolveTexture(Some(&resolve.view.raw)); + } + let load_action = if at.ops.contains(crate::AttachmentOps::LOAD) { + MTLLoadAction::Load + } else if at.ops.contains(crate::AttachmentOps::LOAD_DONT_CARE) { + MTLLoadAction::DontCare + } else if at.ops.contains(crate::AttachmentOps::LOAD_CLEAR) { + at_descriptor.setClearColor(conv::map_clear_color(&at.clear_value)); + MTLLoadAction::Clear + } else { + unreachable!() + }; + let store_action = conv::map_store_action( + at.ops.contains(crate::AttachmentOps::STORE), + at.resolve_target.is_some(), + ); + at_descriptor.setLoadAction(load_action); + at_descriptor.setStoreAction(store_action); + } + } + + if let Some(ref at) = desc.depth_stencil_attachment { + if at.target.view.aspects.contains(crate::FormatAspects::DEPTH) { + let at_descriptor = descriptor.depthAttachment(); + at_descriptor.setTexture(Some(&at.target.view.raw)); + + let load_action = if at.depth_ops.contains(crate::AttachmentOps::LOAD) { + MTLLoadAction::Load + } else if at.depth_ops.contains(crate::AttachmentOps::LOAD_DONT_CARE) { + MTLLoadAction::DontCare + } else if at.depth_ops.contains(crate::AttachmentOps::LOAD_CLEAR) { + at_descriptor.setClearDepth(at.clear_value.0 as f64); + MTLLoadAction::Clear + } else { + unreachable!(); + }; + let store_action = if at.depth_ops.contains(crate::AttachmentOps::STORE) { + MTLStoreAction::Store + } else { + MTLStoreAction::DontCare + }; + at_descriptor.setLoadAction(load_action); + at_descriptor.setStoreAction(store_action); + } + if at + .target + .view + .aspects + .contains(crate::FormatAspects::STENCIL) + { + let at_descriptor = descriptor.stencilAttachment(); + at_descriptor.setTexture(Some(&at.target.view.raw)); + + let load_action = if at.stencil_ops.contains(crate::AttachmentOps::LOAD) { + MTLLoadAction::Load + } else if at + .stencil_ops + .contains(crate::AttachmentOps::LOAD_DONT_CARE) + { + MTLLoadAction::DontCare + } else if at.stencil_ops.contains(crate::AttachmentOps::LOAD_CLEAR) { + at_descriptor.setClearStencil(at.clear_value.1); + MTLLoadAction::Clear + } else { + unreachable!() + }; + let store_action = if at.stencil_ops.contains(crate::AttachmentOps::STORE) { + MTLStoreAction::Store + } else { + MTLStoreAction::DontCare + }; + at_descriptor.setLoadAction(load_action); + at_descriptor.setStoreAction(store_action); + } + } + + let mut sba_index = 0; + let mut next_sba_descriptor = || { + let sba_descriptor = unsafe { + descriptor + .sampleBufferAttachments() + .objectAtIndexedSubscript(sba_index) + }; + + unsafe { sba_descriptor.setEndOfVertexSampleIndex(MTLCounterDontSample) }; + unsafe { sba_descriptor.setStartOfFragmentSampleIndex(MTLCounterDontSample) }; + + sba_index += 1; + sba_descriptor + }; + + for (set, index) in self.state.pending_timer_queries.drain(..) { + let sba_descriptor = next_sba_descriptor(); + sba_descriptor.setSampleBuffer(Some(set.counter_sample_buffer.as_ref().unwrap())); + unsafe { sba_descriptor.setStartOfVertexSampleIndex(index as _) }; + unsafe { sba_descriptor.setEndOfFragmentSampleIndex(MTLCounterDontSample) }; + } + + if let Some(ref timestamp_writes) = desc.timestamp_writes { + let sba_descriptor = next_sba_descriptor(); + sba_descriptor.setSampleBuffer(Some( + timestamp_writes + .query_set + .counter_sample_buffer + .as_ref() + .unwrap(), + )); + + unsafe { + sba_descriptor.setStartOfVertexSampleIndex( + timestamp_writes + .beginning_of_pass_write_index + .map_or(MTLCounterDontSample, |i| i as _), + ) + }; + unsafe { + sba_descriptor.setEndOfFragmentSampleIndex( + timestamp_writes + .end_of_pass_write_index + .map_or(MTLCounterDontSample, |i| i as _), + ) + }; + } + + if let Some(occlusion_query_set) = desc.occlusion_query_set { + descriptor.setVisibilityResultBuffer(Some(occlusion_query_set.raw_buffer.as_ref())) + } + // This strangely isn't mentioned in https://developer.apple.com/documentation/metal/improving-rendering-performance-with-vertex-amplification. + // The docs for [`renderTargetArrayLength`](https://developer.apple.com/documentation/metal/mtlrenderpassdescriptor/rendertargetarraylength) + // also say "The number of active layers that all attachments must have for layered rendering," implying it is only for layered rendering. + // However, when I don't set this, I get undefined behavior in nonzero layers, and all non-apple examples of vertex amplification set it. + // So this is just one of those undocumented requirements. + if let Some(mv) = desc.multiview_mask { + descriptor.setRenderTargetArrayLength(32 - mv.leading_zeros() as usize); + } + let raw = self.raw_cmd_buf.as_ref().unwrap(); + let encoder = raw.renderCommandEncoderWithDescriptor(&descriptor).unwrap(); + if let Some(mv) = desc.multiview_mask { + // Most likely the API just wasn't thought about enough. It's not like they ever allow you + // to use enough views to overflow a 32-bit bitmask. + let mv = mv.get(); + let msb = 32 - mv.leading_zeros(); + let mut maps: SmallVec<[MTLVertexAmplificationViewMapping; 32]> = SmallVec::new(); + for i in 0..msb { + if (mv & (1 << i)) != 0 { + maps.push(MTLVertexAmplificationViewMapping { + renderTargetArrayIndexOffset: i, + viewportArrayIndexOffset: i, + }); + } + } + unsafe { + encoder.setVertexAmplificationCount_viewMappings( + mv.count_ones() as usize, + maps.as_ptr(), + ) + }; + } + if let Some(label) = desc.label { + encoder.setLabel(Some(&NSString::from_str(label))); + } + self.state.render = Some(encoder); + }); + + Ok(()) + } + + unsafe fn end_render_pass(&mut self) { + self.state.render.take().unwrap().endEncoding(); + } + + unsafe fn set_bind_group( + &mut self, + layout: &super::PipelineLayout, + group_index: u32, + group: &super::BindGroup, + dynamic_offsets: &[wgt::DynamicOffset], + ) { + let bg_info = layout.bind_group_infos[group_index as usize] + .as_ref() + .unwrap(); + let render_encoder = self.state.render.clone(); + let compute_encoder = self.state.compute.clone(); + if let Some(encoder) = render_encoder { + self.update_bind_group_state( + Encoder::Vertex(&encoder), + // All zeros, as vs comes first + super::ResourceData::default(), + bg_info, + dynamic_offsets, + group_index, + group, + ); + self.update_bind_group_state( + Encoder::Task(&encoder), + // All zeros, as ts comes first + super::ResourceData::default(), + bg_info, + dynamic_offsets, + group_index, + group, + ); + self.update_bind_group_state( + Encoder::Mesh(&encoder), + group.counters.ts.clone(), + bg_info, + dynamic_offsets, + group_index, + group, + ); + self.update_bind_group_state( + Encoder::Fragment(&encoder), + super::ResourceData { + buffers: group.counters.vs.buffers + + group.counters.ts.buffers + + group.counters.ms.buffers, + textures: group.counters.vs.textures + + group.counters.ts.textures + + group.counters.ms.textures, + samplers: group.counters.vs.samplers + + group.counters.ts.samplers + + group.counters.ms.samplers, + }, + bg_info, + dynamic_offsets, + group_index, + group, + ); + // Call useResource on all textures and buffers used indirectly so they are alive + for (resource, use_info) in group.resources_to_use.iter() { + encoder.useResource_usage_stages( + unsafe { resource.as_ref() }, + use_info.uses, + use_info.stages, + ); + } + } + if let Some(encoder) = compute_encoder { + self.update_bind_group_state( + Encoder::Compute(&encoder), + super::ResourceData { + buffers: group.counters.vs.buffers + + group.counters.ts.buffers + + group.counters.ms.buffers + + group.counters.fs.buffers, + textures: group.counters.vs.textures + + group.counters.ts.textures + + group.counters.ms.textures + + group.counters.fs.textures, + samplers: group.counters.vs.samplers + + group.counters.ts.samplers + + group.counters.ms.samplers + + group.counters.fs.samplers, + }, + bg_info, + dynamic_offsets, + group_index, + group, + ); + // Call useResource on all textures and buffers used indirectly so they are alive + for (resource, use_info) in group.resources_to_use.iter() { + if !use_info.visible_in_compute { + continue; + } + encoder.useResource_usage(unsafe { resource.as_ref() }, use_info.uses); + } + } + } + + unsafe fn set_immediates( + &mut self, + layout: &super::PipelineLayout, + offset_bytes: u32, + data: &[u32], + ) { + let state_pc = &mut self.state.immediates; + if state_pc.len() < layout.total_immediates as usize { + state_pc.resize(layout.total_immediates as usize, 0); + } + debug_assert_eq!(offset_bytes as usize % WORD_SIZE, 0); + + let offset_words = offset_bytes as usize / WORD_SIZE; + state_pc[offset_words..offset_words + data.len()].copy_from_slice(data); + + let bytes = NonNull::new(state_pc.as_ptr().cast_mut().cast()).unwrap(); + if let Some(ref compute) = self.state.compute { + unsafe { + compute.setBytes_length_atIndex( + bytes, + layout.total_immediates as usize * WORD_SIZE, + layout.immediates_infos.cs.unwrap().buffer_index as usize, + ) + }; + } + if let Some(ref render) = self.state.render { + if let Some(vs) = layout.immediates_infos.vs { + unsafe { + render.setVertexBytes_length_atIndex( + bytes, + layout.total_immediates as usize * WORD_SIZE, + vs.buffer_index as _, + ) + } + } + if let Some(fs) = layout.immediates_infos.fs { + unsafe { + render.setFragmentBytes_length_atIndex( + bytes, + layout.total_immediates as usize * WORD_SIZE, + fs.buffer_index as _, + ) + } + } + if let Some(ts) = layout.immediates_infos.ts { + if self.shared.private_caps.mesh_shaders { + unsafe { + render.setObjectBytes_length_atIndex( + bytes, + layout.total_immediates as usize * WORD_SIZE, + ts.buffer_index as _, + ) + } + } + } + if let Some(ms) = layout.immediates_infos.ms { + if self.shared.private_caps.mesh_shaders { + unsafe { + render.setMeshBytes_length_atIndex( + bytes, + layout.total_immediates as usize * WORD_SIZE, + ms.buffer_index as _, + ) + } + } + } + } + } + + unsafe fn insert_debug_marker(&mut self, label: &str) { + if let Some(encoder) = self.active_encoder() { + encoder.insertDebugSignpost(&NSString::from_str(label)); + } + } + unsafe fn begin_debug_marker(&mut self, group_label: &str) { + if let Some(encoder) = self.active_encoder() { + encoder.pushDebugGroup(&NSString::from_str(group_label)); + } else if let Some(ref buf) = self.raw_cmd_buf { + buf.pushDebugGroup(&NSString::from_str(group_label)); + } + } + unsafe fn end_debug_marker(&mut self) { + if let Some(encoder) = self.active_encoder() { + encoder.popDebugGroup(); + } else if let Some(ref buf) = self.raw_cmd_buf { + buf.popDebugGroup(); + } + } + + unsafe fn set_render_pipeline(&mut self, pipeline: &super::RenderPipeline) { + self.state.raw_primitive_type = pipeline.raw_primitive_type; + match pipeline.vs_info { + Some(ref info) => self.state.stage_infos.vs.assign_from(info), + None => self.state.stage_infos.vs.clear(), + } + match pipeline.fs_info { + Some(ref info) => self.state.stage_infos.fs.assign_from(info), + None => self.state.stage_infos.fs.clear(), + } + match pipeline.ts_info { + Some(ref info) => self.state.stage_infos.ts.assign_from(info), + None => self.state.stage_infos.ts.clear(), + } + match pipeline.ms_info { + Some(ref info) => self.state.stage_infos.ms.assign_from(info), + None => self.state.stage_infos.ms.clear(), + } + + let encoder = self.state.render.as_ref().unwrap(); + encoder.setRenderPipelineState(&pipeline.raw); + encoder.setFrontFacingWinding(pipeline.raw_front_winding); + encoder.setCullMode(pipeline.raw_cull_mode); + encoder.setTriangleFillMode(pipeline.raw_triangle_fill_mode); + if let Some(depth_clip) = pipeline.raw_depth_clip_mode { + encoder.setDepthClipMode(depth_clip); + } + if let Some((ref state, bias)) = pipeline.depth_stencil { + encoder.setDepthStencilState(Some(state)); + encoder.setDepthBias_slopeScale_clamp( + bias.constant as f32, + bias.slope_scale, + bias.clamp, + ); + } + + if pipeline.vs_info.is_some() { + if let Some((index, sizes)) = self + .state + .make_sizes_buffer_update(naga::ShaderStage::Vertex, &mut self.temp.binding_sizes) + { + unsafe { + encoder.setVertexBytes_length_atIndex( + NonNull::new(sizes.as_ptr().cast_mut().cast()).unwrap(), + sizes.len() * WORD_SIZE, + index as _, + ) + }; + } + } + if pipeline.fs_info.is_some() { + if let Some((index, sizes)) = self + .state + .make_sizes_buffer_update(naga::ShaderStage::Fragment, &mut self.temp.binding_sizes) + { + unsafe { + encoder.setFragmentBytes_length_atIndex( + NonNull::new(sizes.as_ptr().cast_mut().cast()).unwrap(), + sizes.len() * WORD_SIZE, + index as _, + ) + }; + } + } + if let Some(ts_info) = &pipeline.ts_info { + // update the threadgroup memory sizes + while self.state.stage_infos.ms.work_group_memory_sizes.len() + < ts_info.work_group_memory_sizes.len() + { + self.state.stage_infos.ms.work_group_memory_sizes.push(0); + } + for (index, (cur_size, pipeline_size)) in self + .state + .stage_infos + .ms + .work_group_memory_sizes + .iter_mut() + .zip(ts_info.work_group_memory_sizes.iter()) + .enumerate() + { + let size = pipeline_size.next_multiple_of(16); + if *cur_size != size { + *cur_size = size; + unsafe { encoder.setObjectThreadgroupMemoryLength_atIndex(size as _, index) }; + } + } + if let Some((index, sizes)) = self + .state + .make_sizes_buffer_update(naga::ShaderStage::Task, &mut self.temp.binding_sizes) + { + unsafe { + encoder.setObjectBytes_length_atIndex( + NonNull::new(sizes.as_ptr().cast_mut().cast()).unwrap(), + sizes.len() * WORD_SIZE, + index as _, + ) + }; + } + } + if let Some(_ms_info) = &pipeline.ms_info { + // So there isn't an equivalent to + // https://developer.apple.com/documentation/metal/mtlrendercommandencoder/setthreadgroupmemorylength(_:offset:index:) + // for mesh shaders. This is probably because the CPU has less control over the dispatch sizes and such. Interestingly + // it also affects mesh shaders without task/object shaders, even though none of compute, task or fragment shaders + // behave this way. + if let Some((index, sizes)) = self + .state + .make_sizes_buffer_update(naga::ShaderStage::Mesh, &mut self.temp.binding_sizes) + { + unsafe { + encoder.setMeshBytes_length_atIndex( + NonNull::new(sizes.as_ptr().cast_mut().cast()).unwrap(), + sizes.len() * WORD_SIZE, + index as _, + ) + }; + } + } + } + + unsafe fn set_index_buffer<'a>( + &mut self, + binding: crate::BufferBinding<'a, super::Buffer>, + format: wgt::IndexFormat, + ) { + let (stride, raw_type) = conv::map_index_format(format); + self.state.index = Some(super::IndexState { + buffer_ptr: NonNull::from(&*binding.buffer.raw), + offset: binding.offset, + stride, + raw_type, + }); + } + + unsafe fn set_vertex_buffer<'a>( + &mut self, + index: u32, + binding: crate::BufferBinding<'a, super::Buffer>, + ) { + let buffer_index = MAX_BUFFERS - 1 - index; + let encoder = self.state.render.as_ref().unwrap(); + unsafe { + encoder.setVertexBuffer_offset_atIndex( + Some(&binding.buffer.raw), + binding.offset as usize, + buffer_index as usize, + ) + }; + + let buffer_size = binding.resolve_size(); + if buffer_size > 0 { + self.state.vertex_buffer_size_map.insert( + buffer_index, + core::num::NonZeroU64::new(buffer_size).unwrap(), + ); + } else { + self.state.vertex_buffer_size_map.remove(&buffer_index); + } + + if let Some((index, sizes)) = self + .state + .make_sizes_buffer_update(naga::ShaderStage::Vertex, &mut self.temp.binding_sizes) + { + unsafe { + encoder.setVertexBytes_length_atIndex( + NonNull::new(sizes.as_ptr().cast_mut().cast()).unwrap(), + sizes.len() * WORD_SIZE, + index as _, + ) + }; + } + } + + unsafe fn set_viewport(&mut self, rect: &crate::Rect<f32>, depth_range: Range<f32>) { + let zfar = if self.shared.disabilities.broken_viewport_near_depth { + depth_range.end - depth_range.start + } else { + depth_range.end + }; + let encoder = self.state.render.as_ref().unwrap(); + encoder.setViewport(MTLViewport { + originX: rect.x as _, + originY: rect.y as _, + width: rect.w as _, + height: rect.h as _, + znear: depth_range.start as _, + zfar: zfar as _, + }); + } + unsafe fn set_scissor_rect(&mut self, rect: &crate::Rect<u32>) { + //TODO: support empty scissors by modifying the viewport + let scissor = MTLScissorRect { + x: rect.x as _, + y: rect.y as _, + width: rect.w as _, + height: rect.h as _, + }; + let encoder = self.state.render.as_ref().unwrap(); + encoder.setScissorRect(scissor); + } + unsafe fn set_stencil_reference(&mut self, value: u32) { + let encoder = self.state.render.as_ref().unwrap(); + encoder.setStencilFrontReferenceValue_backReferenceValue(value, value); + } + unsafe fn set_blend_constants(&mut self, color: &[f32; 4]) { + let encoder = self.state.render.as_ref().unwrap(); + encoder.setBlendColorRed_green_blue_alpha(color[0], color[1], color[2], color[3]); + } + + unsafe fn draw( + &mut self, + first_vertex: u32, + vertex_count: u32, + first_instance: u32, + instance_count: u32, + ) { + let encoder = self.state.render.as_ref().unwrap(); + if first_instance != 0 { + unsafe { + encoder.drawPrimitives_vertexStart_vertexCount_instanceCount_baseInstance( + self.state.raw_primitive_type, + first_vertex as _, + vertex_count as _, + instance_count as _, + first_instance as _, + ) + }; + } else if instance_count != 1 { + unsafe { + encoder.drawPrimitives_vertexStart_vertexCount_instanceCount( + self.state.raw_primitive_type, + first_vertex as _, + vertex_count as _, + instance_count as _, + ) + }; + } else { + unsafe { + encoder.drawPrimitives_vertexStart_vertexCount( + self.state.raw_primitive_type, + first_vertex as _, + vertex_count as _, + ) + }; + } + } + + unsafe fn draw_indexed( + &mut self, + first_index: u32, + index_count: u32, + base_vertex: i32, + first_instance: u32, + instance_count: u32, + ) { + let encoder = self.state.render.as_ref().unwrap(); + let index = self.state.index.as_ref().unwrap(); + let offset = (index.offset + index.stride * first_index as wgt::BufferAddress) as usize; + if base_vertex != 0 || first_instance != 0 { + unsafe { + encoder.drawIndexedPrimitives_indexCount_indexType_indexBuffer_indexBufferOffset_instanceCount_baseVertex_baseInstance( + self.state.raw_primitive_type, + index_count as _, + index.raw_type, + index.buffer_ptr.as_ref(), + offset, + instance_count as _, + base_vertex as _, + first_instance as _, + ) + }; + } else if instance_count != 1 { + unsafe { + encoder.drawIndexedPrimitives_indexCount_indexType_indexBuffer_indexBufferOffset_instanceCount( + self.state.raw_primitive_type, + index_count as _, + index.raw_type, + index.buffer_ptr.as_ref(), + offset, + instance_count as _, + ) + }; + } else { + unsafe { + encoder.drawIndexedPrimitives_indexCount_indexType_indexBuffer_indexBufferOffset( + self.state.raw_primitive_type, + index_count as _, + index.raw_type, + index.buffer_ptr.as_ref(), + offset, + ) + }; + } + } + + unsafe fn draw_mesh_tasks( + &mut self, + group_count_x: u32, + group_count_y: u32, + group_count_z: u32, + ) { + let encoder = self.state.render.as_ref().unwrap(); + let size = MTLSize { + width: group_count_x as usize, + height: group_count_y as usize, + depth: group_count_z as usize, + }; + encoder.drawMeshThreadgroups_threadsPerObjectThreadgroup_threadsPerMeshThreadgroup( + size, + self.state.stage_infos.ts.raw_wg_size, + self.state.stage_infos.ms.raw_wg_size, + ); + } + + unsafe fn draw_indirect( + &mut self, + buffer: &super::Buffer, + mut offset: wgt::BufferAddress, + draw_count: u32, + ) { + let encoder = self.state.render.as_ref().unwrap(); + for _ in 0..draw_count { + unsafe { + encoder.drawPrimitives_indirectBuffer_indirectBufferOffset( + self.state.raw_primitive_type, + &buffer.raw, + offset as usize, + ) + }; + offset += size_of::<wgt::DrawIndirectArgs>() as wgt::BufferAddress; + } + } + + unsafe fn draw_indexed_indirect( + &mut self, + buffer: &super::Buffer, + mut offset: wgt::BufferAddress, + draw_count: u32, + ) { + let encoder = self.state.render.as_ref().unwrap(); + let index = self.state.index.as_ref().unwrap(); + for _ in 0..draw_count { + unsafe { + encoder.drawIndexedPrimitives_indexType_indexBuffer_indexBufferOffset_indirectBuffer_indirectBufferOffset( + self.state.raw_primitive_type, + index.raw_type, + index.buffer_ptr.as_ref(), + index.offset as usize, + &buffer.raw, + offset as usize, + ) + }; + offset += size_of::<wgt::DrawIndexedIndirectArgs>() as wgt::BufferAddress; + } + } + + unsafe fn draw_mesh_tasks_indirect( + &mut self, + buffer: &<Self::A as crate::Api>::Buffer, + mut offset: wgt::BufferAddress, + draw_count: u32, + ) { + let encoder = self.state.render.as_ref().unwrap(); + for _ in 0..draw_count { + unsafe { + encoder.drawMeshThreadgroupsWithIndirectBuffer_indirectBufferOffset_threadsPerObjectThreadgroup_threadsPerMeshThreadgroup( + &buffer.raw, + offset as usize, + self.state.stage_infos.ts.raw_wg_size, + self.state.stage_infos.ms.raw_wg_size, + ) + }; + offset += size_of::<wgt::DispatchIndirectArgs>() as wgt::BufferAddress; + } + } + + unsafe fn draw_indirect_count( + &mut self, + _buffer: &super::Buffer, + _offset: wgt::BufferAddress, + _count_buffer: &super::Buffer, + _count_offset: wgt::BufferAddress, + _max_count: u32, + ) { + //TODO + } + unsafe fn draw_indexed_indirect_count( + &mut self, + _buffer: &super::Buffer, + _offset: wgt::BufferAddress, + _count_buffer: &super::Buffer, + _count_offset: wgt::BufferAddress, + _max_count: u32, + ) { + //TODO + } + + unsafe fn draw_mesh_tasks_indirect_count( + &mut self, + _buffer: &<Self::A as crate::Api>::Buffer, + _offset: wgt::BufferAddress, + _count_buffer: &<Self::A as crate::Api>::Buffer, + _count_offset: wgt::BufferAddress, + _max_count: u32, + ) { + unreachable!() + } + + // compute + + unsafe fn begin_compute_pass(&mut self, desc: &crate::ComputePassDescriptor<super::QuerySet>) { + self.begin_pass(); + + debug_assert!(self.state.blit.is_none()); + debug_assert!(self.state.compute.is_none()); + debug_assert!(self.state.render.is_none()); + + let raw = self.raw_cmd_buf.as_ref().unwrap(); + + autoreleasepool(|_| { + // TimeStamp Queries and ComputePassDescriptor were both introduced in Metal 2.3 (macOS 11, iOS 14) + // and we currently only need ComputePassDescriptor for timestamp queries + let encoder = if self.shared.private_caps.timestamp_query_support.is_empty() { + raw.computeCommandEncoder().unwrap() + } else { + let descriptor = MTLComputePassDescriptor::new(); + + let mut sba_index = 0; + let mut next_sba_descriptor = || { + let sba_descriptor = unsafe { + descriptor + .sampleBufferAttachments() + .objectAtIndexedSubscript(sba_index) + }; + sba_index += 1; + sba_descriptor + }; + + for (set, index) in self.state.pending_timer_queries.drain(..) { + let sba_descriptor = next_sba_descriptor(); + sba_descriptor + .setSampleBuffer(Some(set.counter_sample_buffer.as_ref().unwrap())); + unsafe { sba_descriptor.setStartOfEncoderSampleIndex(index as _) }; + unsafe { sba_descriptor.setEndOfEncoderSampleIndex(MTLCounterDontSample) }; + } + + if let Some(timestamp_writes) = desc.timestamp_writes.as_ref() { + let sba_descriptor = next_sba_descriptor(); + sba_descriptor.setSampleBuffer(Some( + timestamp_writes + .query_set + .counter_sample_buffer + .as_ref() + .unwrap(), + )); + + unsafe { + sba_descriptor.setStartOfEncoderSampleIndex( + timestamp_writes + .beginning_of_pass_write_index + .map_or(MTLCounterDontSample, |i| i as _), + ) + }; + unsafe { + sba_descriptor.setEndOfEncoderSampleIndex( + timestamp_writes + .end_of_pass_write_index + .map_or(MTLCounterDontSample, |i| i as _), + ) + }; + } + + raw.computeCommandEncoderWithDescriptor(&descriptor) + .unwrap() + }; + + if let Some(label) = desc.label { + encoder.setLabel(Some(&NSString::from_str(label))); + } + + self.state.compute = Some(encoder.to_owned()); + }); + } + unsafe fn end_compute_pass(&mut self) { + self.state.compute.take().unwrap().endEncoding(); + } + + unsafe fn set_compute_pipeline(&mut self, pipeline: &super::ComputePipeline) { + let previous_sizes = + core::mem::take(&mut self.state.stage_infos.cs.work_group_memory_sizes); + self.state.stage_infos.cs.assign_from(&pipeline.cs_info); + + let encoder = self.state.compute.as_ref().unwrap(); + encoder.setComputePipelineState(&pipeline.raw); + + if let Some((index, sizes)) = self + .state + .make_sizes_buffer_update(naga::ShaderStage::Compute, &mut self.temp.binding_sizes) + { + unsafe { + encoder.setBytes_length_atIndex( + NonNull::new(sizes.as_ptr().cast_mut().cast()).unwrap(), + sizes.len() * WORD_SIZE, + index as _, + ) + }; + } + + // update the threadgroup memory sizes + for (i, current_size) in self + .state + .stage_infos + .cs + .work_group_memory_sizes + .iter_mut() + .enumerate() + { + let prev_size = if i < previous_sizes.len() { + previous_sizes[i] + } else { + u32::MAX + }; + let size: u32 = current_size.next_multiple_of(16); + *current_size = size; + if size != prev_size { + unsafe { encoder.setThreadgroupMemoryLength_atIndex(size as _, i) }; + } + } + } + + unsafe fn dispatch_workgroups(&mut self, count: [u32; 3]) { + if count[0] > 0 && count[1] > 0 && count[2] > 0 { + let encoder = self.state.compute.as_ref().unwrap(); + let raw_count = MTLSize { + width: count[0] as usize, + height: count[1] as usize, + depth: count[2] as usize, + }; + encoder.dispatchThreadgroups_threadsPerThreadgroup( + raw_count, + self.state.stage_infos.cs.raw_wg_size, + ); + } + } + + unsafe fn dispatch_workgroups_indirect( + &mut self, + buffer: &super::Buffer, + offset: wgt::BufferAddress, + ) { + let encoder = self.state.compute.as_ref().unwrap(); + unsafe { + encoder + .dispatchThreadgroupsWithIndirectBuffer_indirectBufferOffset_threadsPerThreadgroup( + &buffer.raw, + offset as usize, + self.state.stage_infos.cs.raw_wg_size, + ) + }; + } + + unsafe fn build_acceleration_structures<'a, T>( + &mut self, + _descriptor_count: u32, + descriptors: T, + ) where + super::Api: 'a, + T: IntoIterator< + Item = crate::BuildAccelerationStructureDescriptor< + 'a, + super::Buffer, + super::AccelerationStructure, + >, + >, + { + let command_encoder = self.enter_acceleration_structure_builder(); + for descriptor in descriptors { + let acceleration_structure_descriptor = + conv::map_acceleration_structure_descriptor(descriptor.entries, descriptor.flags); + match descriptor.mode { + crate::AccelerationStructureBuildMode::Build => { + command_encoder + .buildAccelerationStructure_descriptor_scratchBuffer_scratchBufferOffset( + &descriptor.destination_acceleration_structure.raw, + &acceleration_structure_descriptor, + &descriptor.scratch_buffer.raw, + descriptor.scratch_buffer_offset as usize, + ); + } + crate::AccelerationStructureBuildMode::Update => unsafe { + command_encoder.refitAccelerationStructure_descriptor_destination_scratchBuffer_scratchBufferOffset( + &descriptor.source_acceleration_structure.unwrap().raw, + &acceleration_structure_descriptor, + Some(&descriptor.destination_acceleration_structure.raw), + Some(&descriptor.scratch_buffer.raw), + descriptor.scratch_buffer_offset as usize, + ); + }, + } + } + } + + unsafe fn place_acceleration_structure_barrier( + &mut self, + _barriers: crate::AccelerationStructureBarrier, + ) { + } + + unsafe fn read_acceleration_structure_compact_size( + &mut self, + acceleration_structure: &super::AccelerationStructure, + buffer: &super::Buffer, + ) { + let command_encoder = self.enter_acceleration_structure_builder(); + command_encoder.writeCompactedAccelerationStructureSize_toBuffer_offset( + &acceleration_structure.raw, + &buffer.raw, + 0, + ); + } + + unsafe fn set_acceleration_structure_dependencies( + command_buffers: &[&super::CommandBuffer], + dependencies: &[&super::AccelerationStructure], + ) { + let Some(first_command_buffer) = command_buffers.first() else { + return; + }; + let desc = MTLResidencySetDescriptor::new(); + desc.setLabel(first_command_buffer.raw.label().as_deref()); + let residency_set = first_command_buffer + .raw + .device() + .newResidencySetWithDescriptor_error(&desc) + .unwrap(); + for command_buffer in command_buffers { + command_buffer.raw.useResidencySet(&residency_set); + } + for dependency in dependencies { + residency_set.addAllocation(ProtocolObject::from_ref(&*dependency.raw)); + } + residency_set.commit(); + } + + unsafe fn begin_ray_tracing_pass(&mut self, _desc: &crate::RayTracingPassDescriptor) { + unreachable!("Ray tracing pipelines not supported") + } + + unsafe fn end_ray_tracing_pass(&mut self) { + unreachable!("Ray tracing pipelines not supported") + } + + unsafe fn set_ray_tracing_pipeline( + &mut self, + _pipeline: &<Self::A as crate::Api>::RayTracingPipeline, + ) { + unreachable!("Ray tracing pipelines not supported") + } + + unsafe fn trace_rays( + &mut self, + _count: [u32; 3], + _ray_generation_group_data: crate::PipelineGroupData<super::Buffer>, + _miss_group_data: crate::PipelineGroupData<super::Buffer>, + _intersection_group_data: crate::PipelineGroupData<super::Buffer>, + ) { + unreachable!("Ray tracing pipelines not supported") + } +} + +impl Drop for super::CommandEncoder { + fn drop(&mut self) { + // Metal raises an assert when a MTLCommandEncoder is deallocated without a call + // to endEncoding. This isn't documented in the general case at + // https://developer.apple.com/documentation/metal/mtlcommandencoder, but for the + // more-specific MTLComputeCommandEncoder it is stated as a requirement at + // https://developer.apple.com/documentation/metal/mtlcomputecommandencoder. It + // appears to be a requirement for all MTLCommandEncoder objects. Failing to call + // endEncoding causes a crash with the message 'Command encoder released without + // endEncoding'. To prevent this, we explicitiy call discard_encoding, which + // calls endEncoding on any still-held MTLCommandEncoders. + unsafe { + self.discard_encoding(); + } + self.counters.command_encoders.sub(1); + } +} + +impl Drop for super::CommandBuffer { + fn drop(&mut self) { + // `command_buffer_created_not_submitted` is usually decremented when the command + // buffer is submitted. But if we're dropping a command buffer that was never + // submitted, we need to decrement the count here. + let status = self.raw.status(); + if status == MTLCommandBufferStatus::NotEnqueued + || status == MTLCommandBufferStatus::Enqueued + { + let previous = self + .queue_shared + .command_buffer_created_not_submitted + .fetch_sub(1, atomic::Ordering::AcqRel); + debug_assert!(previous > 0); + } + } +} diff --git a/third_party/wgpu-hal/src/metal/conv.rs b/third_party/wgpu-hal/src/metal/conv.rs new file mode 100644 index 000000000..0b7616427 --- /dev/null +++ b/third_party/wgpu-hal/src/metal/conv.rs @@ -0,0 +1,494 @@ +use objc2::rc::Retained; +use objc2_foundation::{NSArray, NSRange}; +use objc2_metal::{ + MTLAccelerationStructureBoundingBoxGeometryDescriptor, MTLAccelerationStructureDescriptor, + MTLAccelerationStructureGeometryDescriptor, MTLAccelerationStructureInstanceDescriptorType, + MTLAccelerationStructureTriangleGeometryDescriptor, MTLAccelerationStructureUsage, + MTLAttributeFormat, MTLBlendFactor, MTLBlendOperation, MTLBlitOption, MTLClearColor, + MTLColorWriteMask, MTLCompareFunction, MTLCullMode, MTLIndexType, + MTLInstanceAccelerationStructureDescriptor, MTLOrigin, + MTLPrimitiveAccelerationStructureDescriptor, MTLPrimitiveTopologyClass, MTLPrimitiveType, + MTLRenderStages, MTLResourceUsage, MTLSamplerAddressMode, MTLSamplerBorderColor, + MTLSamplerMinMagFilter, MTLSize, MTLStencilOperation, MTLStoreAction, MTLTextureType, + MTLTextureUsage, MTLVertexFormat, MTLVertexStepFunction, MTLWinding, +}; + +pub fn map_texture_usage(format: wgt::TextureFormat, usage: wgt::TextureUses) -> MTLTextureUsage { + use wgt::TextureUses as Tu; + + let mut mtl_usage = MTLTextureUsage::Unknown; + + mtl_usage.set( + MTLTextureUsage::RenderTarget, + usage.intersects(Tu::COLOR_TARGET | Tu::DEPTH_STENCIL_READ | Tu::DEPTH_STENCIL_WRITE), + ); + mtl_usage.set( + MTLTextureUsage::ShaderRead, + usage.intersects( + Tu::RESOURCE | Tu::DEPTH_STENCIL_READ | Tu::STORAGE_READ_ONLY | Tu::STORAGE_READ_WRITE, + ), + ); + mtl_usage.set( + MTLTextureUsage::ShaderWrite, + usage.intersects(Tu::STORAGE_WRITE_ONLY | Tu::STORAGE_READ_WRITE), + ); + // needed for combined depth/stencil formats since we might + // create a stencil-only view from them + mtl_usage.set( + MTLTextureUsage::PixelFormatView, + format.is_combined_depth_stencil_format(), + ); + + mtl_usage.set( + MTLTextureUsage::ShaderAtomic, + usage.intersects(Tu::STORAGE_ATOMIC), + ); + + mtl_usage +} + +pub fn map_texture_view_dimension(dim: wgt::TextureViewDimension) -> MTLTextureType { + use wgt::TextureViewDimension as Tvd; + use MTLTextureType as MTL; + match dim { + Tvd::D1 => MTL::Type1D, + Tvd::D2 => MTL::Type2D, + Tvd::D2Array => MTL::Type2DArray, + Tvd::D3 => MTL::Type3D, + Tvd::Cube => MTL::TypeCube, + Tvd::CubeArray => MTL::TypeCubeArray, + } +} + +pub fn map_compare_function(fun: wgt::CompareFunction) -> MTLCompareFunction { + use wgt::CompareFunction as Cf; + use MTLCompareFunction as MTL; + match fun { + Cf::Never => MTL::Never, + Cf::Less => MTL::Less, + Cf::LessEqual => MTL::LessEqual, + Cf::Equal => MTL::Equal, + Cf::GreaterEqual => MTL::GreaterEqual, + Cf::Greater => MTL::Greater, + Cf::NotEqual => MTL::NotEqual, + Cf::Always => MTL::Always, + } +} + +pub fn map_filter_mode(filter: wgt::FilterMode) -> MTLSamplerMinMagFilter { + use MTLSamplerMinMagFilter as MTL; + match filter { + wgt::FilterMode::Nearest => MTL::Nearest, + wgt::FilterMode::Linear => MTL::Linear, + } +} + +pub fn map_address_mode(address: wgt::AddressMode) -> MTLSamplerAddressMode { + use wgt::AddressMode as Fm; + use MTLSamplerAddressMode as MTL; + match address { + Fm::Repeat => MTL::Repeat, + Fm::MirrorRepeat => MTL::MirrorRepeat, + Fm::ClampToEdge => MTL::ClampToEdge, + Fm::ClampToBorder => MTL::ClampToBorderColor, + //Fm::MirrorClamp => MTL::MirrorClampToEdge, + } +} + +pub fn map_border_color(border_color: wgt::SamplerBorderColor) -> MTLSamplerBorderColor { + use MTLSamplerBorderColor as MTL; + match border_color { + wgt::SamplerBorderColor::TransparentBlack => MTL::TransparentBlack, + wgt::SamplerBorderColor::OpaqueBlack => MTL::OpaqueBlack, + wgt::SamplerBorderColor::OpaqueWhite => MTL::OpaqueWhite, + wgt::SamplerBorderColor::Zero => unreachable!(), + } +} + +pub fn map_primitive_topology( + topology: wgt::PrimitiveTopology, +) -> (MTLPrimitiveTopologyClass, MTLPrimitiveType) { + use wgt::PrimitiveTopology as Pt; + match topology { + Pt::PointList => (MTLPrimitiveTopologyClass::Point, MTLPrimitiveType::Point), + Pt::LineList => (MTLPrimitiveTopologyClass::Line, MTLPrimitiveType::Line), + Pt::LineStrip => (MTLPrimitiveTopologyClass::Line, MTLPrimitiveType::LineStrip), + Pt::TriangleList => ( + MTLPrimitiveTopologyClass::Triangle, + MTLPrimitiveType::Triangle, + ), + Pt::TriangleStrip => ( + MTLPrimitiveTopologyClass::Triangle, + MTLPrimitiveType::TriangleStrip, + ), + } +} + +pub fn map_color_write(mask: wgt::ColorWrites) -> MTLColorWriteMask { + let mut raw_mask = MTLColorWriteMask::empty(); + + if mask.contains(wgt::ColorWrites::RED) { + raw_mask |= MTLColorWriteMask::Red; + } + if mask.contains(wgt::ColorWrites::GREEN) { + raw_mask |= MTLColorWriteMask::Green; + } + if mask.contains(wgt::ColorWrites::BLUE) { + raw_mask |= MTLColorWriteMask::Blue; + } + if mask.contains(wgt::ColorWrites::ALPHA) { + raw_mask |= MTLColorWriteMask::Alpha; + } + + raw_mask +} + +pub fn map_blend_factor(factor: wgt::BlendFactor) -> MTLBlendFactor { + use wgt::BlendFactor as Bf; + use MTLBlendFactor as MTL; + + match factor { + Bf::Zero => MTL::Zero, + Bf::One => MTL::One, + Bf::Src => MTL::SourceColor, + Bf::OneMinusSrc => MTL::OneMinusSourceColor, + Bf::Dst => MTL::DestinationColor, + Bf::OneMinusDst => MTL::OneMinusDestinationColor, + Bf::SrcAlpha => MTL::SourceAlpha, + Bf::OneMinusSrcAlpha => MTL::OneMinusSourceAlpha, + Bf::DstAlpha => MTL::DestinationAlpha, + Bf::OneMinusDstAlpha => MTL::OneMinusDestinationAlpha, + Bf::Constant => MTL::BlendColor, + Bf::OneMinusConstant => MTL::OneMinusBlendColor, + Bf::SrcAlphaSaturated => MTL::SourceAlphaSaturated, + Bf::Src1 => MTL::Source1Color, + Bf::OneMinusSrc1 => MTL::OneMinusSource1Color, + Bf::Src1Alpha => MTL::Source1Alpha, + Bf::OneMinusSrc1Alpha => MTL::OneMinusSource1Alpha, + } +} + +pub fn map_blend_op(operation: wgt::BlendOperation) -> MTLBlendOperation { + use wgt::BlendOperation as Bo; + use MTLBlendOperation as MTL; + + match operation { + Bo::Add => MTL::Add, + Bo::Subtract => MTL::Subtract, + Bo::ReverseSubtract => MTL::ReverseSubtract, + Bo::Min => MTL::Min, + Bo::Max => MTL::Max, + } +} + +pub fn map_blend_component( + component: &wgt::BlendComponent, +) -> (MTLBlendOperation, MTLBlendFactor, MTLBlendFactor) { + ( + map_blend_op(component.operation), + map_blend_factor(component.src_factor), + map_blend_factor(component.dst_factor), + ) +} + +pub fn map_vertex_format(format: wgt::VertexFormat) -> MTLVertexFormat { + use wgt::VertexFormat as Vf; + use MTLVertexFormat as MTL; + + match format { + Vf::Unorm8 => MTL::UCharNormalized, + Vf::Snorm8 => MTL::CharNormalized, + Vf::Uint8 => MTL::UChar, + Vf::Sint8 => MTL::Char, + Vf::Unorm8x2 => MTL::UChar2Normalized, + Vf::Snorm8x2 => MTL::Char2Normalized, + Vf::Uint8x2 => MTL::UChar2, + Vf::Sint8x2 => MTL::Char2, + Vf::Unorm8x4 => MTL::UChar4Normalized, + Vf::Snorm8x4 => MTL::Char4Normalized, + Vf::Uint8x4 => MTL::UChar4, + Vf::Sint8x4 => MTL::Char4, + Vf::Unorm16 => MTL::UShortNormalized, + Vf::Snorm16 => MTL::ShortNormalized, + Vf::Uint16 => MTL::UShort, + Vf::Sint16 => MTL::Short, + Vf::Float16 => MTL::Half, + Vf::Unorm16x2 => MTL::UShort2Normalized, + Vf::Snorm16x2 => MTL::Short2Normalized, + Vf::Uint16x2 => MTL::UShort2, + Vf::Sint16x2 => MTL::Short2, + Vf::Float16x2 => MTL::Half2, + Vf::Unorm16x4 => MTL::UShort4Normalized, + Vf::Snorm16x4 => MTL::Short4Normalized, + Vf::Uint16x4 => MTL::UShort4, + Vf::Sint16x4 => MTL::Short4, + Vf::Float16x4 => MTL::Half4, + Vf::Uint32 => MTL::UInt, + Vf::Sint32 => MTL::Int, + Vf::Float32 => MTL::Float, + Vf::Uint32x2 => MTL::UInt2, + Vf::Sint32x2 => MTL::Int2, + Vf::Float32x2 => MTL::Float2, + Vf::Uint32x3 => MTL::UInt3, + Vf::Sint32x3 => MTL::Int3, + Vf::Float32x3 => MTL::Float3, + Vf::Uint32x4 => MTL::UInt4, + Vf::Sint32x4 => MTL::Int4, + Vf::Float32x4 => MTL::Float4, + Vf::Unorm10_10_10_2 => MTL::UInt1010102Normalized, + Vf::Unorm8x4Bgra => MTL::UChar4Normalized_BGRA, + Vf::Float64 | Vf::Float64x2 | Vf::Float64x3 | Vf::Float64x4 => unimplemented!(), + } +} + +pub fn map_index_format(format: wgt::IndexFormat) -> (u64, MTLIndexType) { + match format { + wgt::IndexFormat::Uint16 => (2, MTLIndexType::UInt16), + wgt::IndexFormat::Uint32 => (4, MTLIndexType::UInt32), + } +} + +pub fn map_step_mode(mode: wgt::VertexStepMode) -> MTLVertexStepFunction { + match mode { + wgt::VertexStepMode::Vertex => MTLVertexStepFunction::PerVertex, + wgt::VertexStepMode::Instance => MTLVertexStepFunction::PerInstance, + } +} + +pub fn map_stencil_op(op: wgt::StencilOperation) -> MTLStencilOperation { + use wgt::StencilOperation as So; + use MTLStencilOperation as MTL; + + match op { + So::Keep => MTL::Keep, + So::Zero => MTL::Zero, + So::Replace => MTL::Replace, + So::IncrementClamp => MTL::IncrementClamp, + So::IncrementWrap => MTL::IncrementWrap, + So::DecrementClamp => MTL::DecrementClamp, + So::DecrementWrap => MTL::DecrementWrap, + So::Invert => MTL::Invert, + } +} + +pub fn map_winding(winding: wgt::FrontFace) -> MTLWinding { + match winding { + wgt::FrontFace::Cw => MTLWinding::Clockwise, + wgt::FrontFace::Ccw => MTLWinding::CounterClockwise, + } +} + +pub fn map_cull_mode(face: Option<wgt::Face>) -> MTLCullMode { + match face { + None => MTLCullMode::None, + Some(wgt::Face::Front) => MTLCullMode::Front, + Some(wgt::Face::Back) => MTLCullMode::Back, + } +} + +pub fn map_range(range: &crate::MemoryRange) -> NSRange { + NSRange { + location: range.start as usize, + length: (range.end - range.start) as usize, + } +} + +pub fn map_copy_extent(extent: &crate::CopyExtent) -> MTLSize { + MTLSize { + width: extent.width as usize, + height: extent.height as usize, + depth: extent.depth as usize, + } +} + +pub fn map_origin(origin: &wgt::Origin3d) -> MTLOrigin { + MTLOrigin { + x: origin.x as usize, + y: origin.y as usize, + z: origin.z as usize, + } +} + +pub fn map_store_action(store: bool, resolve: bool) -> MTLStoreAction { + use MTLStoreAction as MTL; + match (store, resolve) { + (true, true) => MTL::StoreAndMultisampleResolve, + (false, true) => MTL::MultisampleResolve, + (true, false) => MTL::Store, + (false, false) => MTL::DontCare, + } +} + +pub fn map_clear_color(color: &wgt::Color) -> MTLClearColor { + MTLClearColor { + red: color.r, + green: color.g, + blue: color.b, + alpha: color.a, + } +} + +pub fn get_blit_option(format: wgt::TextureFormat, aspect: crate::FormatAspects) -> MTLBlitOption { + if format.is_combined_depth_stencil_format() { + match aspect { + crate::FormatAspects::DEPTH => MTLBlitOption::DepthFromDepthStencil, + crate::FormatAspects::STENCIL => MTLBlitOption::StencilFromDepthStencil, + _ => unreachable!(), + } + } else { + MTLBlitOption::None + } +} + +pub fn map_render_stages(stage: wgt::ShaderStages) -> MTLRenderStages { + let mut raw_stages = MTLRenderStages::empty(); + + if stage.contains(wgt::ShaderStages::VERTEX) { + raw_stages |= MTLRenderStages::Vertex; + } + if stage.contains(wgt::ShaderStages::FRAGMENT) { + raw_stages |= MTLRenderStages::Fragment; + } + + raw_stages +} + +pub fn map_resource_usage(ty: &wgt::BindingType) -> MTLResourceUsage { + match ty { + #[allow(deprecated)] + wgt::BindingType::Texture { .. } => MTLResourceUsage::Sample, + wgt::BindingType::StorageTexture { access, .. } => match access { + wgt::StorageTextureAccess::WriteOnly => MTLResourceUsage::Write, + wgt::StorageTextureAccess::ReadOnly => MTLResourceUsage::Read, + wgt::StorageTextureAccess::Atomic | wgt::StorageTextureAccess::ReadWrite => { + MTLResourceUsage::Read | MTLResourceUsage::Write + } + }, + wgt::BindingType::Sampler(..) => MTLResourceUsage::empty(), + wgt::BindingType::Buffer { ty, .. } => match ty { + wgt::BufferBindingType::Uniform + | wgt::BufferBindingType::Storage { read_only: true } => MTLResourceUsage::Read, + wgt::BufferBindingType::Storage { read_only: false } => { + MTLResourceUsage::Read | MTLResourceUsage::Write + } + }, + _ => unreachable!(), + } +} + +pub fn map_acceleration_structure_descriptor<'a>( + entries: &'a crate::AccelerationStructureEntries<'a, super::Buffer>, + flags: crate::AccelerationStructureBuildFlags, +) -> Retained<MTLAccelerationStructureDescriptor> { + let descriptor = match entries { + crate::AccelerationStructureEntries::Instances(instances) => { + let descriptor = MTLInstanceAccelerationStructureDescriptor::new(); + descriptor.setInstanceDescriptorType( + MTLAccelerationStructureInstanceDescriptorType::Indirect, + ); + descriptor.setInstanceCount(instances.count as usize); + descriptor.setInstanceDescriptorBuffer(Some(&instances.buffer.unwrap().raw)); + unsafe { + descriptor.setInstanceDescriptorBufferOffset(instances.offset as usize); + } + descriptor.into_super() + } + crate::AccelerationStructureEntries::Triangles(entries) => { + let geometry_descriptors = entries + .iter() + .map(|triangles| { + let descriptor = MTLAccelerationStructureTriangleGeometryDescriptor::new(); + if let Some(indices) = triangles.indices.as_ref() { + descriptor.setIndexBuffer(Some(&indices.buffer.unwrap().raw)); + unsafe { + descriptor.setIndexBufferOffset(indices.offset as usize); + } + descriptor.setIndexType(map_index_format(indices.format).1); + descriptor.setTriangleCount(indices.count as usize / 3); + } else { + descriptor.setTriangleCount(triangles.vertex_count as usize / 3); + } + descriptor.setVertexBuffer(Some(&triangles.vertex_buffer.unwrap().raw)); + unsafe { + descriptor.setVertexBufferOffset( + triangles.first_vertex as usize * triangles.vertex_stride as usize, + ); + } + descriptor.setVertexStride(triangles.vertex_stride as usize); + // Safety: MTLVertexFormat and MTLAttributeFormat are identical. + // https://docs.rs/objc2-metal/latest/objc2_metal/struct.MTLAttributeFormat.html + // https://docs.rs/objc2-metal/latest/objc2_metal/struct.MTLVertexFormat.html + descriptor.setVertexFormat(unsafe { + core::mem::transmute::<MTLVertexFormat, MTLAttributeFormat>( + map_vertex_format(triangles.vertex_format), + ) + }); + if let Some(transform) = triangles.transform.as_ref() { + unsafe { + descriptor.setTransformationMatrixBuffer(Some(&transform.buffer.raw)); + descriptor + .setTransformationMatrixBufferOffset(transform.offset as usize); + } + } + descriptor.setOpaque( + triangles + .flags + .contains(wgt::AccelerationStructureGeometryFlags::OPAQUE), + ); + if !triangles.flags.contains( + wgt::AccelerationStructureGeometryFlags::NO_DUPLICATE_ANY_HIT_INVOCATION, + ) { + descriptor.allowDuplicateIntersectionFunctionInvocation(); + } + // descriptor.setIntersectionFunctionTableOffset(offset); + descriptor.into_super() + }) + .collect::<alloc::vec::Vec<Retained<MTLAccelerationStructureGeometryDescriptor>>>(); + let descriptor = MTLPrimitiveAccelerationStructureDescriptor::new(); + descriptor.setGeometryDescriptors(Some(&NSArray::from_retained_slice( + geometry_descriptors.as_slice(), + ))); + descriptor.into_super() + } + crate::AccelerationStructureEntries::AABBs(entries) => { + let geometry_descriptors = entries + .iter() + .map(|aabbs| { + let descriptor = MTLAccelerationStructureBoundingBoxGeometryDescriptor::new(); + descriptor.setBoundingBoxBuffer(Some(&aabbs.buffer.unwrap().raw)); + descriptor.setBoundingBoxCount(aabbs.count as usize); + unsafe { + descriptor.setBoundingBoxStride(aabbs.stride as usize); + descriptor.setBoundingBoxBufferOffset(aabbs.offset as usize); + } + descriptor.setOpaque( + aabbs + .flags + .contains(wgt::AccelerationStructureGeometryFlags::OPAQUE), + ); + if !aabbs.flags.contains( + wgt::AccelerationStructureGeometryFlags::NO_DUPLICATE_ANY_HIT_INVOCATION, + ) { + descriptor.allowDuplicateIntersectionFunctionInvocation(); + } + // descriptor.setIntersectionFunctionTableOffset(offset); + descriptor.into_super() + }) + .collect::<alloc::vec::Vec<Retained<MTLAccelerationStructureGeometryDescriptor>>>(); + let descriptor = MTLPrimitiveAccelerationStructureDescriptor::new(); + descriptor.setGeometryDescriptors(Some(&NSArray::from_retained_slice( + geometry_descriptors.as_slice(), + ))); + descriptor.into_super() + } + }; + let mut usage = MTLAccelerationStructureUsage::None; + if flags.contains(wgt::AccelerationStructureFlags::ALLOW_UPDATE) { + usage |= MTLAccelerationStructureUsage::Refit; + } + if flags.contains(wgt::AccelerationStructureFlags::PREFER_FAST_BUILD) { + usage |= MTLAccelerationStructureUsage::PreferFastBuild; + } + descriptor.setUsage(usage); + descriptor +} diff --git a/third_party/wgpu-hal/src/metal/device.rs b/third_party/wgpu-hal/src/metal/device.rs new file mode 100644 index 000000000..d7287b977 --- /dev/null +++ b/third_party/wgpu-hal/src/metal/device.rs @@ -0,0 +1,2170 @@ +use alloc::{borrow::ToOwned as _, sync::Arc, vec::Vec}; +use core::{mem::align_of, ptr::NonNull}; + +use bytemuck::TransparentWrapper; +use objc2::{ + available, + rc::{autoreleasepool, Retained}, + runtime::ProtocolObject, +}; +use objc2_foundation::{ns_string, NSError, NSRange, NSString, NSUInteger}; +use objc2_metal::{ + MTLAccelerationStructure, MTLAccelerationStructureInstanceOptions, MTLBuffer, + MTLCaptureManager, MTLCaptureScope, MTLCompileOptions, MTLComputePipelineDescriptor, + MTLComputePipelineState, MTLCounterSampleBufferDescriptor, MTLCounterSet, MTLDepthClipMode, + MTLDepthStencilDescriptor, MTLDevice, MTLFunction, + MTLIndirectAccelerationStructureInstanceDescriptor, MTLLanguageVersion, MTLLibrary, + MTLMeshRenderPipelineDescriptor, MTLMutability, MTLPackedFloat3, MTLPackedFloat4x3, + MTLPipelineBufferDescriptorArray, MTLPipelineOption, MTLPixelFormat, MTLPrimitiveTopologyClass, + MTLRenderPipelineColorAttachmentDescriptorArray, MTLRenderPipelineDescriptor, MTLResource, + MTLResourceID, MTLResourceOptions, MTLSamplerAddressMode, MTLSamplerDescriptor, + MTLSamplerMipFilter, MTLSamplerState, MTLSize, MTLStencilDescriptor, MTLStorageMode, + MTLTexture, MTLTextureDescriptor, MTLTextureType, MTLTriangleFillMode, MTLVertexDescriptor, + MTLVertexStepFunction, +}; +use parking_lot::{Condvar, Mutex, RwLock}; + +use super::{adapter::MAX_BUFFERS, conv, PassthroughShader, ShaderModuleSource}; +use crate::{auxil::map_naga_stage, DropCallback, DropGuard, TlasInstance}; + +type DeviceResult<T> = Result<T, crate::DeviceError>; + +/// True on arm64_32 (watchOS ILP32) targets. +/// +/// There are no Apple OSes that support both 32-bit applications and Metal, +/// so `target_pointer_width = "32"` is a reliable proxy for ILP32 watchOS +/// devices (Apple Watch S4–S9, SE, Ultra). Several AGXMetalS4 driver bugs +/// require workarounds gated on this flag. +const IS_WATCHOS_ILP32: bool = cfg!(target_pointer_width = "32"); + +struct CompiledShader { + library: Retained<ProtocolObject<dyn MTLLibrary>>, + function: Retained<ProtocolObject<dyn MTLFunction>>, + wg_size: MTLSize, + wg_memory_sizes: Vec<u32>, + + /// Bindings of WGSL `storage` globals that contain variable-sized arrays, + /// paired with a binding-array element index when applicable. + /// + /// In order to implement bounds checks and the `arrayLength` function for + /// WGSL runtime-sized arrays, we pass the entry point a struct with a + /// `u32` member for each such binding. For ordinary storage buffers the + /// element index is zero; for storage binding arrays there is one entry per + /// layout element. Each member holds that element's total size in bytes--- + /// the size of the `Buffer` supplying its contents for the draw or dispatch. + sized_bindings: Vec<(naga::ResourceBinding, u32)>, + + immutable_buffer_mask: usize, +} + +fn create_stencil_desc( + face: &wgt::StencilFaceState, + read_mask: u32, + write_mask: u32, +) -> Retained<MTLStencilDescriptor> { + let desc = MTLStencilDescriptor::new(); + desc.setStencilCompareFunction(conv::map_compare_function(face.compare)); + desc.setReadMask(read_mask); + desc.setWriteMask(write_mask); + desc.setStencilFailureOperation(conv::map_stencil_op(face.fail_op)); + desc.setDepthFailureOperation(conv::map_stencil_op(face.depth_fail_op)); + desc.setDepthStencilPassOperation(conv::map_stencil_op(face.pass_op)); + desc +} + +fn create_depth_stencil_desc( + state: &wgt::DepthStencilState, +) -> Retained<MTLDepthStencilDescriptor> { + let desc = MTLDepthStencilDescriptor::new(); + desc.setDepthCompareFunction(conv::map_compare_function( + state.depth_compare.unwrap_or_default(), + )); + desc.setDepthWriteEnabled(state.depth_write_enabled.unwrap_or_default()); + let s = &state.stencil; + if s.is_enabled() { + let front_desc = create_stencil_desc(&s.front, s.read_mask, s.write_mask); + desc.setFrontFaceStencil(Some(&front_desc)); + let back_desc = create_stencil_desc(&s.back, s.read_mask, s.write_mask); + desc.setBackFaceStencil(Some(&back_desc)); + } + desc +} + +#[allow( + clippy::mut_from_ref, + reason = "MTLBuffer shared contents are writable through `&MTLBuffer`" +)] +fn bindless_id_table_mut( + buffer: &ProtocolObject<dyn MTLBuffer>, + count: u32, +) -> &mut [MTLResourceID] { + let ptr = buffer.contents().cast::<u8>().as_ptr(); + // SAFETY: The buffer is aligned to the size of `MTLResourceID`. + assert_eq!(ptr as usize % align_of::<MTLResourceID>(), 0); + let ptr = ptr.cast::<MTLResourceID>(); + unsafe { core::slice::from_raw_parts_mut(ptr, count as usize) } +} + +const fn convert_vertex_format_to_naga(format: wgt::VertexFormat) -> nt::VertexFormat { + match format { + wgt::VertexFormat::Uint8 => nt::VertexFormat::Uint8, + wgt::VertexFormat::Uint8x2 => nt::VertexFormat::Uint8x2, + wgt::VertexFormat::Uint8x4 => nt::VertexFormat::Uint8x4, + wgt::VertexFormat::Sint8 => nt::VertexFormat::Sint8, + wgt::VertexFormat::Sint8x2 => nt::VertexFormat::Sint8x2, + wgt::VertexFormat::Sint8x4 => nt::VertexFormat::Sint8x4, + wgt::VertexFormat::Unorm8 => nt::VertexFormat::Unorm8, + wgt::VertexFormat::Unorm8x2 => nt::VertexFormat::Unorm8x2, + wgt::VertexFormat::Unorm8x4 => nt::VertexFormat::Unorm8x4, + wgt::VertexFormat::Snorm8 => nt::VertexFormat::Snorm8, + wgt::VertexFormat::Snorm8x2 => nt::VertexFormat::Snorm8x2, + wgt::VertexFormat::Snorm8x4 => nt::VertexFormat::Snorm8x4, + wgt::VertexFormat::Uint16 => nt::VertexFormat::Uint16, + wgt::VertexFormat::Uint16x2 => nt::VertexFormat::Uint16x2, + wgt::VertexFormat::Uint16x4 => nt::VertexFormat::Uint16x4, + wgt::VertexFormat::Sint16 => nt::VertexFormat::Sint16, + wgt::VertexFormat::Sint16x2 => nt::VertexFormat::Sint16x2, + wgt::VertexFormat::Sint16x4 => nt::VertexFormat::Sint16x4, + wgt::VertexFormat::Unorm16 => nt::VertexFormat::Unorm16, + wgt::VertexFormat::Unorm16x2 => nt::VertexFormat::Unorm16x2, + wgt::VertexFormat::Unorm16x4 => nt::VertexFormat::Unorm16x4, + wgt::VertexFormat::Snorm16 => nt::VertexFormat::Snorm16, + wgt::VertexFormat::Snorm16x2 => nt::VertexFormat::Snorm16x2, + wgt::VertexFormat::Snorm16x4 => nt::VertexFormat::Snorm16x4, + wgt::VertexFormat::Float16 => nt::VertexFormat::Float16, + wgt::VertexFormat::Float16x2 => nt::VertexFormat::Float16x2, + wgt::VertexFormat::Float16x4 => nt::VertexFormat::Float16x4, + wgt::VertexFormat::Float32 => nt::VertexFormat::Float32, + wgt::VertexFormat::Float32x2 => nt::VertexFormat::Float32x2, + wgt::VertexFormat::Float32x3 => nt::VertexFormat::Float32x3, + wgt::VertexFormat::Float32x4 => nt::VertexFormat::Float32x4, + wgt::VertexFormat::Uint32 => nt::VertexFormat::Uint32, + wgt::VertexFormat::Uint32x2 => nt::VertexFormat::Uint32x2, + wgt::VertexFormat::Uint32x3 => nt::VertexFormat::Uint32x3, + wgt::VertexFormat::Uint32x4 => nt::VertexFormat::Uint32x4, + wgt::VertexFormat::Sint32 => nt::VertexFormat::Sint32, + wgt::VertexFormat::Sint32x2 => nt::VertexFormat::Sint32x2, + wgt::VertexFormat::Sint32x3 => nt::VertexFormat::Sint32x3, + wgt::VertexFormat::Sint32x4 => nt::VertexFormat::Sint32x4, + wgt::VertexFormat::Unorm10_10_10_2 => nt::VertexFormat::Unorm10_10_10_2, + wgt::VertexFormat::Unorm8x4Bgra => nt::VertexFormat::Unorm8x4Bgra, + + wgt::VertexFormat::Float64 + | wgt::VertexFormat::Float64x2 + | wgt::VertexFormat::Float64x3 + | wgt::VertexFormat::Float64x4 => { + unimplemented!() + } + } +} + +impl super::Device { + fn load_shader( + &self, + stage: &crate::ProgrammableStage<super::ShaderModule>, + vertex_buffer_mappings: &[naga::back::msl::VertexBufferMapping], + layout: &super::PipelineLayout, + primitive_class: MTLPrimitiveTopologyClass, + naga_stage: naga::ShaderStage, + ) -> Result<CompiledShader, crate::PipelineError> { + match stage.module.source { + ShaderModuleSource::Naga(ref naga_shader) => { + let stage_bit = map_naga_stage(naga_stage); + let (module, module_info) = naga::back::pipeline_constants::process_overrides( + &naga_shader.module, + &naga_shader.info, + Some((naga_stage, stage.entry_point)), + stage.constants, + ) + .map_err(|e| { + crate::PipelineError::PipelineConstants(stage_bit, format!("MSL: {e:?}")) + })?; + + let ep_resources = &layout.per_stage_map[naga_stage]; + + let bounds_check_policy = if stage.module.runtime_checks.bounds_checks { + naga::proc::BoundsCheckPolicy::Restrict + } else { + naga::proc::BoundsCheckPolicy::Unchecked + }; + + let options = naga::back::msl::Options { + lang_version: match self.shared.private_caps.msl_version { + #[allow(deprecated)] + MTLLanguageVersion::Version1_0 => (1, 0), + MTLLanguageVersion::Version1_1 => (1, 1), + MTLLanguageVersion::Version1_2 => (1, 2), + MTLLanguageVersion::Version2_0 => (2, 0), + MTLLanguageVersion::Version2_1 => (2, 1), + MTLLanguageVersion::Version2_2 => (2, 2), + MTLLanguageVersion::Version2_3 => (2, 3), + MTLLanguageVersion::Version2_4 => (2, 4), + MTLLanguageVersion::Version3_0 => (3, 0), + MTLLanguageVersion::Version3_1 => (3, 1), + MTLLanguageVersion::Version3_2 => (3, 2), + MTLLanguageVersion::Version4_0 => (4, 0), + // Newer version, fall back to 3.2 + _ => (4, 0), + }, + inline_samplers: Default::default(), + spirv_cross_compatibility: false, + fake_missing_bindings: false, + per_entry_point_map: naga::back::msl::EntryPointResourceMap::from([( + stage.entry_point.to_owned(), + ep_resources.clone(), + )]), + bounds_check_policies: naga::proc::BoundsCheckPolicies { + index: bounds_check_policy, + buffer: bounds_check_policy, + image_load: bounds_check_policy, + // TODO: support bounds checks on binding arrays + binding_array: naga::proc::BoundsCheckPolicy::Unchecked, + }, + zero_initialize_workgroup_memory: stage.zero_initialize_workgroup_memory, + force_loop_bounding: stage.module.runtime_checks.force_loop_bounding, + task_dispatch_limits: stage + .module + .runtime_checks + .task_shader_dispatch_tracking + .then_some(naga::back::TaskDispatchLimits { + max_mesh_workgroups_per_dim: self + .limits + .max_mesh_workgroups_per_dimension, + max_mesh_workgroups_total: self.limits.max_mesh_workgroup_total_count, + }), + mesh_shader_primitive_indices_clamp: stage + .module + .runtime_checks + .mesh_shader_primitive_indices_clamp, + emit_int_div_checks: stage.module.runtime_checks.int_div_checks, + ray_query_initialization_tracking: stage + .module + .runtime_checks + .ray_query_initialization_tracking, + }; + + let pipeline_options = naga::back::msl::PipelineOptions { + entry_point: Some((naga_stage, stage.entry_point.to_owned())), + allow_and_force_point_size: match primitive_class { + MTLPrimitiveTopologyClass::Point => true, + _ => false, + }, + vertex_pulling_transform: true, + vertex_buffer_mappings: vertex_buffer_mappings.to_vec(), + binding_array_length_map: layout.binding_array_length_map.clone(), + }; + + let (source, info) = naga::back::msl::write_string( + &module, + &module_info, + &options, + &pipeline_options, + ) + .map_err(|e| crate::PipelineError::Linkage(stage_bit, format!("MSL: {e:?}")))?; + + log::debug!( + "Naga generated shader for entry point '{}' and stage {:?}\n{}", + stage.entry_point, + naga_stage, + &source + ); + + let options = MTLCompileOptions::new(); + options.setLanguageVersion(self.shared.private_caps.msl_version); + + // https://developer.apple.com/documentation/metal/mtlcompileoptions/preserveinvariance + if available!(macos = 11.0, ios = 13.0, tvos = 14.0, visionos = 1.0) { + options.setPreserveInvariance(true); + } + + let library = self + .shared + .device + .newLibraryWithSource_options_error( + &NSString::from_str(&source), + Some(&options), + ) + .map_err(|err| { + log::debug!("Naga generated shader:\n{source}"); + crate::PipelineError::Linkage(stage_bit, format!("Metal: {err}")) + })?; + + let ep_index = module + .entry_points + .iter() + .position(|ep| ep.stage == naga_stage && ep.name == stage.entry_point) + .ok_or(crate::PipelineError::EntryPoint(naga_stage))?; + let ep = &module.entry_points[ep_index]; + let translated_ep_name = info.entry_point_names[0] + .as_ref() + .map_err(|e| crate::PipelineError::Linkage(stage_bit, format!("{e}")))?; + + let wg_size = MTLSize { + width: ep.workgroup_size[0] as _, + height: ep.workgroup_size[1] as _, + depth: ep.workgroup_size[2] as _, + }; + + let function = library + .newFunctionWithName(&NSString::from_str(translated_ep_name)) + .ok_or_else(|| { + log::error!("Function '{translated_ep_name}' does not exist"); + crate::PipelineError::EntryPoint(naga_stage) + })?; + + // collect sizes indices, immutable buffers, and work group memory sizes + let ep_info = &module_info.get_entry_point(ep_index); + let mut wg_memory_sizes = Vec::new(); + let mut sized_bindings = Vec::new(); + let mut immutable_buffer_mask = 0; + for (var_handle, var) in module.global_variables.iter() { + match var.space { + naga::AddressSpace::WorkGroup => { + if !ep_info[var_handle].is_empty() { + let size = module.types[var.ty].inner.size(module.to_ctx()); + wg_memory_sizes.push(size); + } + } + naga::AddressSpace::Uniform | naga::AddressSpace::Storage { .. } => { + let br = match var.binding { + Some(br) => br, + None => continue, + }; + let storage_access_store = match var.space { + naga::AddressSpace::Storage { access } => { + access.contains(naga::StorageAccess::STORE) + } + _ => false, + }; + + // check for an immutable buffer + if !ep_info[var_handle].is_empty() && !storage_access_store { + let slot = ep_resources.resources[&br].buffer.unwrap(); + immutable_buffer_mask |= 1 << slot; + } + + if module.types[var.ty] + .inner + .needs_host_buffer_byte_size(&module.types) + { + let n = match module.types[var.ty].inner { + naga::TypeInner::BindingArray { size, .. } => { + let from_shader = match size { + naga::ArraySize::Constant(n) => n.get(), + naga::ArraySize::Pending(_) + | naga::ArraySize::Dynamic => 0, + }; + let from_layout = layout + .binding_array_length_map + .get(&br) + .copied() + .unwrap_or(0); + from_shader.max(from_layout).max(1) + } + _ => 1, + }; + sized_bindings.extend((0..n).map(|i| (br, i))); + } + } + _ => {} + } + } + + Ok(CompiledShader { + library, + function, + wg_size, + wg_memory_sizes, + sized_bindings, + immutable_buffer_mask, + }) + } + ShaderModuleSource::Passthrough(ref shader) => { + let size = shader.num_workgroups[stage.entry_point]; + Ok(CompiledShader { + library: shader.library.clone(), + function: shader + .library + .newFunctionWithName(&NSString::from_str(stage.entry_point)) + .ok_or(crate::PipelineError::EntryPoint(naga_stage))?, + wg_size: MTLSize { + width: size.0 as usize, + height: size.1 as usize, + depth: size.2 as usize, + }, + wg_memory_sizes: vec![], + sized_bindings: vec![], + immutable_buffer_mask: 0, + }) + } + } + } + + fn set_buffers_mutability( + buffers: &MTLPipelineBufferDescriptorArray, + mut immutable_mask: usize, + ) { + while immutable_mask != 0 { + let slot = immutable_mask.trailing_zeros(); + immutable_mask ^= 1 << slot; + unsafe { buffers.objectAtIndexedSubscript(slot as usize) } + .setMutability(MTLMutability::Immutable); + } + } + + pub unsafe fn texture_from_raw( + raw: Retained<ProtocolObject<dyn MTLTexture>>, + format: wgt::TextureFormat, + raw_type: MTLTextureType, + array_layers: u32, + mip_levels: u32, + copy_size: crate::CopyExtent, + drop_callback: Option<DropCallback>, + ) -> super::Texture { + super::Texture { + raw, + format, + raw_type, + array_layers, + mip_levels, + copy_size, + _drop_guard: DropGuard::from_option(drop_callback), + } + } + + pub unsafe fn device_from_raw( + raw: Retained<ProtocolObject<dyn MTLDevice>>, + features: wgt::Features, + limits: &wgt::Limits, + ) -> super::Device { + let capabilities_query = super::CapabilitiesQuery::new(&raw); + let shared = super::AdapterShared::new(raw, &capabilities_query); + super::Device { + shared: Arc::new(shared), + features, + counters: Default::default(), + limits: limits.clone(), + } + } + + pub unsafe fn buffer_from_raw( + raw: Retained<ProtocolObject<dyn MTLBuffer>>, + size: wgt::BufferAddress, + ) -> super::Buffer { + super::Buffer { raw, size } + } + + pub fn raw_device(&self) -> &Retained<ProtocolObject<dyn MTLDevice>> { + &self.shared.device + } +} + +impl crate::Device for super::Device { + type A = super::Api; + + unsafe fn create_buffer(&self, desc: &crate::BufferDescriptor) -> DeviceResult<super::Buffer> { + let map_read = desc.usage.contains(wgt::BufferUses::MAP_READ); + let map_write = desc.usage.contains(wgt::BufferUses::MAP_WRITE); + + let mut options = MTLResourceOptions::empty(); + options |= if map_read || map_write { + // `crate::MemoryFlags::PREFER_COHERENT` is ignored here + MTLResourceOptions::StorageModeShared + } else { + MTLResourceOptions::StorageModePrivate + }; + options.set(MTLResourceOptions::CPUCacheModeWriteCombined, map_write); + + //TODO: HazardTrackingModeUntracked + + autoreleasepool(|_| { + let raw = self + .shared + .device + .newBufferWithLength_options(desc.size as usize, options) + .ok_or(crate::DeviceError::OutOfMemory)?; + if let Some(label) = desc.label { + raw.setLabel(Some(&NSString::from_str(label))); + } + self.counters.buffers.add(1); + Ok(super::Buffer { + raw, + size: desc.size, + }) + }) + } + unsafe fn destroy_buffer(&self, _buffer: super::Buffer) { + self.counters.buffers.sub(1); + } + + unsafe fn add_raw_buffer(&self, _buffer: &super::Buffer) { + self.counters.buffers.add(1); + } + + unsafe fn map_buffer( + &self, + buffer: &super::Buffer, + range: crate::MemoryRange, + ) -> DeviceResult<crate::BufferMapping> { + let ptr = buffer.raw.contents().cast::<u8>(); + Ok(crate::BufferMapping { + ptr: NonNull::new(unsafe { ptr.as_ptr().offset(range.start as isize) }).unwrap(), + is_coherent: true, + }) + } + + unsafe fn unmap_buffer(&self, _buffer: &super::Buffer) {} + unsafe fn flush_mapped_ranges<I>(&self, _buffer: &super::Buffer, _ranges: I) {} + unsafe fn invalidate_mapped_ranges<I>(&self, _buffer: &super::Buffer, _ranges: I) {} + + unsafe fn create_texture( + &self, + desc: &crate::TextureDescriptor, + ) -> DeviceResult<super::Texture> { + let mtl_format = self + .shared + .private_texture_format_caps + .map_format(desc.format); + + autoreleasepool(|_| { + let descriptor = MTLTextureDescriptor::new(); + + let mtl_type = match desc.dimension { + wgt::TextureDimension::D1 => MTLTextureType::Type1D, + wgt::TextureDimension::D2 => { + if desc.sample_count > 1 { + unsafe { descriptor.setSampleCount(desc.sample_count as usize) }; + + if desc.size.depth_or_array_layers > 1 { + unsafe { + descriptor.setArrayLength(desc.size.depth_or_array_layers as usize) + }; + MTLTextureType::Type2DMultisampleArray + } else { + MTLTextureType::Type2DMultisample + } + } else if desc.size.depth_or_array_layers > 1 { + unsafe { + descriptor.setArrayLength(desc.size.depth_or_array_layers as usize) + }; + MTLTextureType::Type2DArray + } else { + MTLTextureType::Type2D + } + } + wgt::TextureDimension::D3 => { + unsafe { descriptor.setDepth(desc.size.depth_or_array_layers as usize) }; + MTLTextureType::Type3D + } + }; + + let mtl_storage_mode = if desc.usage.contains(wgt::TextureUses::TRANSIENT) + && self.shared.private_caps.supports_memoryless_storage + { + MTLStorageMode::Memoryless + } else if IS_WATCHOS_ILP32 { + // The AGXMetalS4 driver (A13/S6 GPU) crashes in + // copyFromTexture:toBuffer: on Private textures — null deref at + // offset 0x50 in the driver's internal texture state. Use Shared + // storage which works correctly on Apple's unified memory + // architecture and matches what native Swift Metal code uses on + // these devices. + MTLStorageMode::Shared + } else { + MTLStorageMode::Private + }; + + descriptor.setTextureType(mtl_type); + unsafe { descriptor.setWidth(desc.size.width as usize) }; + unsafe { descriptor.setHeight(desc.size.height as usize) }; + unsafe { descriptor.setMipmapLevelCount(desc.mip_level_count as usize) }; + descriptor.setPixelFormat(mtl_format); + descriptor.setUsage(conv::map_texture_usage(desc.format, desc.usage)); + descriptor.setStorageMode(mtl_storage_mode); + + let raw = self + .shared + .device + .newTextureWithDescriptor(&descriptor) + .ok_or(crate::DeviceError::OutOfMemory)?; + if let Some(label) = desc.label { + raw.setLabel(Some(&NSString::from_str(label))); + } + + self.counters.textures.add(1); + + Ok(super::Texture { + raw, + format: desc.format, + raw_type: mtl_type, + mip_levels: desc.mip_level_count, + array_layers: desc.array_layer_count(), + copy_size: desc.copy_extent(), + _drop_guard: None, + }) + }) + } + + unsafe fn destroy_texture(&self, _texture: super::Texture) { + self.counters.textures.sub(1); + } + + unsafe fn add_raw_texture(&self, _texture: &super::Texture) { + self.counters.textures.add(1); + } + + unsafe fn create_texture_view( + &self, + texture: &super::Texture, + desc: &crate::TextureViewDescriptor, + ) -> DeviceResult<super::TextureView> { + let raw_type = if texture.raw_type == MTLTextureType::Type2DMultisample + || texture.raw_type == MTLTextureType::Type2DMultisampleArray + { + texture.raw_type + } else { + conv::map_texture_view_dimension(desc.dimension) + }; + + let aspects = crate::FormatAspects::new(texture.format, desc.range.aspect); + + let raw_format = self + .shared + .private_texture_format_caps + .map_view_format(desc.format, aspects); + + let format_equal = raw_format + == self + .shared + .private_texture_format_caps + .map_format(texture.format); + let type_equal = raw_type == texture.raw_type; + let range_full_resource = + desc.range + .is_full_resource(desc.format, texture.mip_levels, texture.array_layers); + + let raw = if format_equal && type_equal && range_full_resource { + // Some images are marked as framebuffer-only, and we can't create aliases of them. + // Also helps working around Metal bugs with aliased array textures. + texture.raw.to_owned() + } else { + let mip_level_count = desc + .range + .mip_level_count + .unwrap_or(texture.mip_levels - desc.range.base_mip_level); + let array_layer_count = desc + .range + .array_layer_count + .unwrap_or(texture.array_layers - desc.range.base_array_layer); + + autoreleasepool(|_| { + let level_range = NSRange { + location: desc.range.base_mip_level as _, + length: mip_level_count as _, + }; + let slice_range = NSRange { + location: desc.range.base_array_layer as _, + length: array_layer_count as _, + }; + let raw = unsafe { + texture + .raw + .newTextureViewWithPixelFormat_textureType_levels_slices( + raw_format, + raw_type, + level_range, + slice_range, + ) + .unwrap() + }; + if let Some(label) = desc.label { + raw.setLabel(Some(&NSString::from_str(label))); + } + raw + }) + }; + + self.counters.texture_views.add(1); + + Ok(super::TextureView { raw, aspects }) + } + + unsafe fn destroy_texture_view(&self, _view: super::TextureView) { + self.counters.texture_views.sub(1); + } + + unsafe fn create_sampler( + &self, + desc: &crate::SamplerDescriptor, + ) -> DeviceResult<super::Sampler> { + autoreleasepool(|_| { + let descriptor = MTLSamplerDescriptor::new(); + + descriptor.setMinFilter(conv::map_filter_mode(desc.min_filter)); + descriptor.setMagFilter(conv::map_filter_mode(desc.mag_filter)); + descriptor.setMipFilter(match desc.mipmap_filter { + wgt::MipmapFilterMode::Nearest if desc.lod_clamp == (0.0..0.0) => { + MTLSamplerMipFilter::NotMipmapped + } + wgt::MipmapFilterMode::Nearest => MTLSamplerMipFilter::Nearest, + wgt::MipmapFilterMode::Linear => MTLSamplerMipFilter::Linear, + }); + + let [s, t, r] = desc.address_modes; + descriptor.setSAddressMode(conv::map_address_mode(s)); + descriptor.setTAddressMode(conv::map_address_mode(t)); + descriptor.setRAddressMode(conv::map_address_mode(r)); + + // Anisotropy is always supported on mac up to 16x + descriptor.setMaxAnisotropy(desc.anisotropy_clamp as _); + + descriptor.setLodMinClamp(desc.lod_clamp.start); + descriptor.setLodMaxClamp(desc.lod_clamp.end); + + if let Some(fun) = desc.compare { + descriptor.setCompareFunction(conv::map_compare_function(fun)); + } + + if let Some(border_color) = desc.border_color { + if let wgt::SamplerBorderColor::Zero = border_color { + if s == wgt::AddressMode::ClampToBorder { + descriptor.setSAddressMode(MTLSamplerAddressMode::ClampToZero); + } + + if t == wgt::AddressMode::ClampToBorder { + descriptor.setTAddressMode(MTLSamplerAddressMode::ClampToZero); + } + + if r == wgt::AddressMode::ClampToBorder { + descriptor.setRAddressMode(MTLSamplerAddressMode::ClampToZero); + } + } else { + descriptor.setBorderColor(conv::map_border_color(border_color)); + } + } + + if let Some(label) = desc.label { + descriptor.setLabel(Some(&NSString::from_str(label))); + } + if self.features.contains(wgt::Features::TEXTURE_BINDING_ARRAY) { + descriptor.setSupportArgumentBuffers(true); + } + let raw = self + .shared + .device + .newSamplerStateWithDescriptor(&descriptor) + .unwrap(); + + self.counters.samplers.add(1); + + Ok(super::Sampler { raw }) + }) + } + unsafe fn destroy_sampler(&self, _sampler: super::Sampler) { + self.counters.samplers.sub(1); + } + + unsafe fn create_command_encoder( + &self, + desc: &crate::CommandEncoderDescriptor<super::Queue>, + ) -> Result<super::CommandEncoder, crate::DeviceError> { + self.counters.command_encoders.add(1); + Ok(super::CommandEncoder { + shared: Arc::clone(&self.shared), + queue_shared: Arc::clone(&desc.queue.shared), + raw_cmd_buf: None, + state: super::CommandState::default(), + temp: super::Temp::default(), + counters: Arc::clone(&self.counters), + }) + } + + unsafe fn create_bind_group_layout( + &self, + desc: &crate::BindGroupLayoutDescriptor, + ) -> DeviceResult<super::BindGroupLayout> { + self.counters.bind_group_layouts.add(1); + + Ok(super::BindGroupLayout { + entries: Arc::from(desc.entries), + }) + } + + unsafe fn destroy_bind_group_layout(&self, _bg_layout: super::BindGroupLayout) { + self.counters.bind_group_layouts.sub(1); + } + + unsafe fn create_pipeline_layout( + &self, + desc: &crate::PipelineLayoutDescriptor<super::BindGroupLayout>, + ) -> DeviceResult<super::PipelineLayout> { + #[derive(Debug)] + struct StageInfo { + stage: naga::ShaderStage, + counters: super::ResourceData<super::ResourceIndex>, + pc_buffer: Option<super::ResourceIndex>, + pc_limit: u32, + sizes_buffer: Option<super::ResourceIndex>, + need_sizes_buffer: bool, + resources: naga::back::msl::BindingMap, + } + + let mut stage_data = super::NAGA_STAGES.map(|stage| StageInfo { + stage, + counters: super::ResourceData::default(), + pc_buffer: None, + pc_limit: 0, + sizes_buffer: None, + need_sizes_buffer: false, + resources: Default::default(), + }); + let mut bind_group_infos = [const { None }; crate::MAX_BIND_GROUPS]; + let mut binding_array_length_map = naga::FastHashMap::default(); + + // First, place the immediates + for info in stage_data.iter_mut() { + info.pc_limit = desc.immediate_size; + + // handle the immediate data buffer assignment and shader overrides + if info.pc_limit != 0 { + info.pc_buffer = Some(info.counters.buffers); + info.counters.buffers += 1; + } + } + + // Second, place the described resources + for (group_index, bgl) in desc.bind_group_layouts.iter().enumerate() { + let Some(bgl) = bgl else { + continue; + }; + + // remember where the resources for this set start at each shader stage + let base_resource_indices = stage_data.map_ref(|info| info.counters.clone()); + + for entry in bgl.entries.iter() { + let br = naga::ResourceBinding { + group: group_index as u32, + binding: entry.binding, + }; + if let Some(count) = entry.count { + if matches!( + entry.ty, + wgt::BindingType::Buffer { + ty: wgt::BufferBindingType::Storage { .. }, + .. + } + ) { + binding_array_length_map.insert(br, count.get()); + } + } + + if let wgt::BindingType::Buffer { + ty: wgt::BufferBindingType::Storage { .. }, + .. + } = entry.ty + { + for info in stage_data.iter_mut() { + if entry.visibility.contains(map_naga_stage(info.stage)) { + info.need_sizes_buffer = true; + } + } + } + + for info in stage_data.iter_mut() { + if !entry.visibility.contains(map_naga_stage(info.stage)) { + continue; + } + + let mut target = naga::back::msl::BindTarget::default(); + // Bindless path + if let Some(_) = entry.count { + target.buffer = Some(info.counters.buffers as _); + info.counters.buffers += 1; + } else { + match entry.ty { + wgt::BindingType::Buffer { ty, .. } => { + target.buffer = Some(info.counters.buffers as _); + info.counters.buffers += 1; + if let wgt::BufferBindingType::Storage { read_only } = ty { + target.mutable = !read_only; + } + } + wgt::BindingType::Sampler { .. } => { + target.sampler = + Some(naga::back::msl::BindSamplerTarget::Resource( + info.counters.samplers as _, + )); + info.counters.samplers += 1; + } + wgt::BindingType::Texture { .. } => { + target.texture = Some(info.counters.textures as _); + info.counters.textures += 1; + } + wgt::BindingType::StorageTexture { access, .. } => { + target.texture = Some(info.counters.textures as _); + info.counters.textures += 1; + target.mutable = match access { + wgt::StorageTextureAccess::ReadOnly => false, + wgt::StorageTextureAccess::WriteOnly => true, + wgt::StorageTextureAccess::ReadWrite => true, + wgt::StorageTextureAccess::Atomic => true, + }; + } + wgt::BindingType::AccelerationStructure { .. } => { + target.buffer = Some(info.counters.buffers as _); + info.counters.buffers += 1; + } + wgt::BindingType::ExternalTexture => { + target.external_texture = + Some(naga::back::msl::BindExternalTextureTarget { + planes: [ + info.counters.textures as _, + (info.counters.textures + 1) as _, + (info.counters.textures + 2) as _, + ], + params: info.counters.buffers as _, + }); + info.counters.textures += 3; + info.counters.buffers += 1; + } + } + } + + info.resources.insert(br, target); + } + } + + bind_group_infos[group_index] = Some(super::BindGroupLayoutInfo { + base_resource_indices, + }); + } + + // Finally, make sure we fit the limits + for info in stage_data.iter_mut() { + if info.need_sizes_buffer || info.stage == naga::ShaderStage::Vertex { + // Set aside space for the sizes_buffer, which is required + // for variable-length buffers, or to support vertex pulling. + info.sizes_buffer = Some(info.counters.buffers); + info.counters.buffers += 1; + } + } + + let immediates_infos = stage_data.map_ref(|info| { + info.pc_buffer.map(|buffer_index| super::ImmediateDataInfo { + count: info.pc_limit, + buffer_index, + }) + }); + + let per_stage_map = stage_data.map(|info| naga::back::msl::EntryPointResources { + immediates_buffer: info + .pc_buffer + .map(|buffer_index| buffer_index as naga::back::msl::Slot), + sizes_buffer: info + .sizes_buffer + .map(|buffer_index| buffer_index as naga::back::msl::Slot), + resources: info.resources, + }); + + self.counters.pipeline_layouts.add(1); + + Ok(super::PipelineLayout { + bind_group_infos, + immediates_infos, + total_immediates: desc.immediate_size, + per_stage_map, + binding_array_length_map, + }) + } + + unsafe fn destroy_pipeline_layout(&self, _pipeline_layout: super::PipelineLayout) { + self.counters.pipeline_layouts.sub(1); + } + + unsafe fn create_bind_group( + &self, + desc: &crate::BindGroupDescriptor< + super::BindGroupLayout, + super::Buffer, + super::Sampler, + super::TextureView, + super::AccelerationStructure, + >, + ) -> DeviceResult<super::BindGroup> { + autoreleasepool(|_| { + let mut bg = super::BindGroup::default(); + for (&stage, counter) in super::NAGA_STAGES.iter().zip(bg.counters.iter_mut()) { + let stage_bit = map_naga_stage(stage); + let mut dynamic_offsets_count = 0u32; + let layout_and_entry_iter = desc.entries.iter().map(|entry| { + let layout = desc + .layout + .entries + .iter() + .find(|layout_entry| layout_entry.binding == entry.binding) + .expect("internal error: no layout entry found with binding slot"); + (entry, layout) + }); + for (entry, layout) in layout_and_entry_iter { + // Bindless path + if layout.count.is_some() { + if !layout.visibility.contains(stage_bit) { + continue; + } + + let count = entry.count; + + let stages = conv::map_render_stages(layout.visibility); + let uses = conv::map_resource_usage(&layout.ty); + + // Create argument buffer for this array + let argument_buffer = self + .shared + .device + .newBufferWithLength_options( + 8 * count as usize, + MTLResourceOptions::HazardTrackingModeUntracked + | MTLResourceOptions::StorageModeShared, + ) + .unwrap(); + + let mut array_element_sizes = Vec::new(); + match layout.ty { + wgt::BindingType::Texture { .. } + | wgt::BindingType::StorageTexture { .. } => { + let resource_ids = + bindless_id_table_mut(argument_buffer.as_ref(), count); + let start = entry.resource_index as usize; + let end = start + count as usize; + let textures = &desc.textures[start..end]; + + for (idx, tex) in textures.iter().enumerate() { + resource_ids[idx] = tex.view.raw.gpuResourceID(); + + let use_info = bg + .resources_to_use + .entry(tex.view.as_raw().cast()) + .or_default(); + use_info.stages |= stages; + use_info.uses |= uses; + use_info.visible_in_compute |= + layout.visibility.contains(wgt::ShaderStages::COMPUTE); + } + } + wgt::BindingType::Sampler { .. } => { + let resource_ids = + bindless_id_table_mut(argument_buffer.as_ref(), count); + let start = entry.resource_index as usize; + let end = start + count as usize; + let samplers = &desc.samplers[start..end]; + + for (idx, &sampler) in samplers.iter().enumerate() { + resource_ids[idx] = sampler.raw.gpuResourceID(); + // Samplers aren't resources like buffers and textures, so don't + // need to be passed to useResource + } + } + wgt::BindingType::Buffer { ty, .. } => { + let start = entry.resource_index as usize; + let end = start + count as usize; + let buffers = &desc.buffers[start..end]; + let pointers = argument_buffer.contents().cast::<u64>(); + for (idx, source) in buffers.iter().enumerate() { + let addr = source.buffer.raw.gpuAddress() + source.offset; + unsafe { + pointers.add(idx).write(addr); + } + + if let wgt::BufferBindingType::Storage { .. } = ty { + let remaining_size = wgt::BufferSize::new( + source.buffer.size - source.offset, + ); + if let Some(binding_size) = source.size.or(remaining_size) { + array_element_sizes.push((idx as u32, binding_size)); + } + } + + let use_info = bg + .resources_to_use + .entry(source.buffer.as_raw().cast()) + .or_default(); + use_info.stages |= stages; + use_info.uses |= uses; + use_info.visible_in_compute |= + layout.visibility.contains(wgt::ShaderStages::COMPUTE); + } + } + wgt::BindingType::AccelerationStructure { .. } => { + let resource_ids = + bindless_id_table_mut(argument_buffer.as_ref(), count); + let start = entry.resource_index as usize; + let end = start + count as usize; + let acceleration_structures = + &desc.acceleration_structures[start..end]; + + for (idx, &acceleration_structure) in + acceleration_structures.iter().enumerate() + { + resource_ids[idx] = acceleration_structure.raw.gpuResourceID(); + + let use_info = bg + .resources_to_use + .entry(acceleration_structure.as_raw().cast()) + .or_default(); + use_info.stages |= stages; + use_info.uses |= uses; + use_info.visible_in_compute |= + layout.visibility.contains(wgt::ShaderStages::COMPUTE); + } + } + _ => { + unimplemented!(); + } + } + + if matches!( + layout.ty, + wgt::BindingType::Buffer { + ty: wgt::BufferBindingType::Storage { .. }, + .. + } + ) { + bg.buffers + .push(super::BufferLikeResource::StorageBindingArray { + ptr: NonNull::from(&*argument_buffer), + array_element_sizes, + binding_location: layout.binding, + }); + } else { + bg.buffers.push(super::BufferLikeResource::Buffer { + ptr: NonNull::from(&*argument_buffer), + offset: 0, + dynamic_index: None, + binding_size: None, + binding_location: layout.binding, + }); + } + counter.buffers += 1; + + bg.argument_buffers.push(argument_buffer) + } + // Bindfull path + else { + if let wgt::BindingType::Buffer { + has_dynamic_offset: true, + .. + } = layout.ty + { + dynamic_offsets_count += 1; + } + if !layout.visibility.contains(stage_bit) { + continue; + } + match layout.ty { + wgt::BindingType::Buffer { + ty, + has_dynamic_offset, + .. + } => { + let start = entry.resource_index as usize; + let end = start + 1; + bg.buffers + .extend(desc.buffers[start..end].iter().map(|source| { + // Given the restrictions on `BufferBinding::offset`, + // this should never be `None`. + let remaining_size = wgt::BufferSize::new( + source.buffer.size - source.offset, + ); + let binding_size = match ty { + wgt::BufferBindingType::Storage { .. } => { + source.size.or(remaining_size) + } + _ => None, + }; + super::BufferLikeResource::Buffer { + ptr: source.buffer.as_raw(), + offset: source.offset, + dynamic_index: if has_dynamic_offset { + Some(dynamic_offsets_count - 1) + } else { + None + }, + binding_size, + binding_location: layout.binding, + } + })); + counter.buffers += 1; + } + wgt::BindingType::Sampler { .. } => { + let start = entry.resource_index as usize; + let end = start + 1; + bg.samplers.extend( + desc.samplers[start..end].iter().map(|samp| samp.as_raw()), + ); + counter.samplers += 1; + } + wgt::BindingType::Texture { .. } + | wgt::BindingType::StorageTexture { .. } => { + let start = entry.resource_index as usize; + let end = start + 1; + bg.textures.extend( + desc.textures[start..end] + .iter() + .map(|tex| tex.view.as_raw()), + ); + counter.textures += 1; + } + wgt::BindingType::AccelerationStructure { .. } => { + let start = entry.resource_index as usize; + let end = start + 1; + bg.buffers.extend( + desc.acceleration_structures[start..end].iter().map( + |acceleration_structure| { + super::BufferLikeResource::AccelerationStructure( + acceleration_structure.as_raw(), + ) + }, + ), + ); + counter.buffers += 1; + } + wgt::BindingType::ExternalTexture => { + // We don't yet support binding arrays of external textures. + // https://github.com/gfx-rs/wgpu/issues/8027 + assert_eq!(entry.count, 1); + let external_texture = + &desc.external_textures[entry.resource_index as usize]; + bg.textures.extend( + external_texture + .planes + .iter() + .map(|plane| plane.view.as_raw()), + ); + bg.buffers.push(super::BufferLikeResource::Buffer { + ptr: external_texture.params.buffer.as_raw(), + offset: external_texture.params.offset, + dynamic_index: None, + binding_size: None, + binding_location: layout.binding, + }); + counter.textures += 3; + counter.buffers += 1; + } + } + } + } + } + + self.counters.bind_groups.add(1); + + Ok(bg) + }) + } + + unsafe fn destroy_bind_group(&self, _group: super::BindGroup) { + self.counters.bind_groups.sub(1); + } + + unsafe fn create_shader_module( + &self, + desc: &crate::ShaderModuleDescriptor, + shader: crate::ShaderInput, + ) -> Result<super::ShaderModule, crate::ShaderError> { + self.counters.shader_modules.add(1); + + match shader { + crate::ShaderInput::Naga(naga) => Ok(super::ShaderModule { + source: ShaderModuleSource::Naga(naga), + runtime_checks: desc.runtime_checks, + }), + crate::ShaderInput::MetalLib { + file, + num_workgroups, + } => { + // SAFETY: this creates a reference to `file` that is dropped before `file` is dropped. + let library = super::library_from_metallib::new_library_from_metallib_bytes( + &self.shared.device, + file, + ) + .map_err(|e| crate::ShaderError::Compilation(format!("Metallib: {e:?}")))?; + Ok(super::ShaderModule { + source: ShaderModuleSource::Passthrough(PassthroughShader { + library, + num_workgroups, + }), + // This goes unused for passthrough shaders + runtime_checks: wgt::ShaderRuntimeChecks::unchecked(), + }) + } + crate::ShaderInput::Msl { + shader: source, + num_workgroups, + } => { + let options = MTLCompileOptions::new(); + // Obtain the device from shared + let device = &self.shared.device; + let library = device + .newLibraryWithSource_options_error(&NSString::from_str(source), Some(&options)) + .map_err(|e| crate::ShaderError::Compilation(format!("MSL: {e:?}")))?; + + Ok(super::ShaderModule { + source: ShaderModuleSource::Passthrough(PassthroughShader { + library, + num_workgroups, + }), + runtime_checks: desc.runtime_checks, + }) + } + crate::ShaderInput::SpirV(_) + | crate::ShaderInput::Dxil { .. } + | crate::ShaderInput::Hlsl { .. } + | crate::ShaderInput::Glsl { .. } => unreachable!(), + } + } + + unsafe fn destroy_shader_module(&self, _module: super::ShaderModule) { + self.counters.shader_modules.sub(1); + } + + unsafe fn create_render_pipeline( + &self, + desc: &crate::RenderPipelineDescriptor< + super::PipelineLayout, + super::ShaderModule, + super::PipelineCache, + >, + ) -> Result<super::RenderPipeline, crate::PipelineError> { + autoreleasepool(|_| { + enum MetalGenericRenderPipelineDescriptor { + Standard(Retained<MTLRenderPipelineDescriptor>), + Mesh(Retained<MTLMeshRenderPipelineDescriptor>), + } + macro_rules! descriptor_fn { + ($descriptor:ident . $method:ident $( ( $($args:expr),* ) )? ) => { + match $descriptor { + MetalGenericRenderPipelineDescriptor::Standard(ref inner) => inner.$method$(($($args),*))?, + MetalGenericRenderPipelineDescriptor::Mesh(ref inner) => inner.$method$(($($args),*))?, + } + }; + } + #[allow(non_snake_case)] + impl MetalGenericRenderPipelineDescriptor { + unsafe fn setFragmentFunction( + &self, + function: Option<&ProtocolObject<dyn MTLFunction>>, + ) { + unsafe { descriptor_fn!(self.setFragmentFunction(function)) }; + } + fn fragmentBuffers(&self) -> Retained<MTLPipelineBufferDescriptorArray> { + descriptor_fn!(self.fragmentBuffers()) + } + fn setDepthAttachmentPixelFormat(&self, pixel_format: MTLPixelFormat) { + descriptor_fn!(self.setDepthAttachmentPixelFormat(pixel_format)); + } + fn colorAttachments( + &self, + ) -> Retained<MTLRenderPipelineColorAttachmentDescriptorArray> { + descriptor_fn!(self.colorAttachments()) + } + fn setStencilAttachmentPixelFormat(&self, pixel_format: MTLPixelFormat) { + descriptor_fn!(self.setStencilAttachmentPixelFormat(pixel_format)); + } + fn setAlphaToCoverageEnabled(&self, enabled: bool) { + descriptor_fn!(self.setAlphaToCoverageEnabled(enabled)); + } + fn setLabel(&self, label: Option<&NSString>) { + descriptor_fn!(self.setLabel(label)); + } + unsafe fn setMaxVertexAmplificationCount(&self, count: NSUInteger) { + unsafe { descriptor_fn!(self.setMaxVertexAmplificationCount(count)) } + } + } + + // https://developer.apple.com/documentation/metal/mtlpipelinebufferdescriptor/mutability + // Disabled on watchOS ILP32: the AGXMetalS4 driver exhibits instability + // when mutability hints are combined with Shared storage mode textures. + // Conservative disable until broader device coverage. + let supports_mutability = !IS_WATCHOS_ILP32 + && available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0); + + let (primitive_class, raw_primitive_type) = + conv::map_primitive_topology(desc.primitive.topology); + + let vs_info; + let ts_info; + let ms_info; + + // Create the pipeline descriptor and do vertex/mesh pipeline specific setup + let descriptor = match desc.vertex_processor { + crate::VertexProcessor::Standard { + vertex_buffers, + ref vertex_stage, + } => { + // Vertex pipeline specific setup + + let descriptor = MTLRenderPipelineDescriptor::new(); + ts_info = None; + ms_info = None; + + // Collect vertex buffer mappings + let mut vertex_buffer_mappings = + Vec::<naga::back::msl::VertexBufferMapping>::new(); + for (i, vbl) in vertex_buffers.iter().enumerate() { + let Some(vbl) = vbl else { + continue; + }; + let mut attributes = Vec::<naga::back::msl::AttributeMapping>::new(); + for attribute in vbl.attributes.iter() { + attributes.push(naga::back::msl::AttributeMapping { + shader_location: attribute.shader_location, + offset: attribute.offset as u32, + format: convert_vertex_format_to_naga(attribute.format), + }); + } + + let mapping = naga::back::msl::VertexBufferMapping { + id: MAX_BUFFERS - 1 - i as u32, + stride: if vbl.array_stride > 0 { + vbl.array_stride.try_into().unwrap() + } else { + vbl.attributes + .iter() + .map(|attribute| attribute.offset + attribute.format.size()) + .max() + .unwrap_or(0) + .try_into() + .unwrap() + }, + step_mode: match (vbl.array_stride == 0, vbl.step_mode) { + (true, _) => naga::back::msl::VertexBufferStepMode::Constant, + (false, wgt::VertexStepMode::Vertex) => { + naga::back::msl::VertexBufferStepMode::ByVertex + } + (false, wgt::VertexStepMode::Instance) => { + naga::back::msl::VertexBufferStepMode::ByInstance + } + }, + attributes, + }; + vertex_buffer_mappings.push(mapping); + } + + // Setup vertex shader + { + let vs = self.load_shader( + vertex_stage, + &vertex_buffer_mappings, + desc.layout, + primitive_class, + naga::ShaderStage::Vertex, + )?; + + descriptor.setVertexFunction(Some(&vs.function)); + + if supports_mutability { + Self::set_buffers_mutability( + &descriptor.vertexBuffers(), + vs.immutable_buffer_mask, + ); + } + + vs_info = Some(super::PipelineStageInfo { + immediates: desc.layout.immediates_infos.vs, + sizes_slot: desc.layout.per_stage_map.vs.sizes_buffer, + sized_bindings: vs.sized_bindings, + vertex_buffer_mappings, + library: Some(vs.library), + raw_wg_size: MTLSize { + width: 0, + height: 0, + depth: 0, + }, + work_group_memory_sizes: vec![], + }); + } + + // Set the pipeline vertex buffer info + if !vertex_buffers.is_empty() { + let vertex_descriptor = MTLVertexDescriptor::new(); + for (i, vb) in vertex_buffers.iter().enumerate() { + let Some(vb) = vb else { + continue; + }; + + let buffer_index = MAX_BUFFERS as usize - 1 - i; + let buffer_desc = unsafe { + vertex_descriptor + .layouts() + .objectAtIndexedSubscript(buffer_index) + }; + + // Metal expects the stride to be the actual size of the attributes. + // The semantics of array_stride == 0 can be achieved by setting + // the step function to constant and rate to 0. + if vb.array_stride == 0 { + let stride = vb + .attributes + .iter() + .map(|attribute| attribute.offset + attribute.format.size()) + .max() + .unwrap_or(0); + unsafe { + buffer_desc.setStride(wgt::math::align_to( + NSUInteger::try_from(stride).unwrap(), + 4, + )) + }; + buffer_desc.setStepFunction(MTLVertexStepFunction::Constant); + unsafe { buffer_desc.setStepRate(0) }; + } else { + unsafe { buffer_desc.setStride(vb.array_stride as _) }; + buffer_desc.setStepFunction(conv::map_step_mode(vb.step_mode)); + } + + for at in vb.attributes { + let attribute_desc = unsafe { + vertex_descriptor + .attributes() + .objectAtIndexedSubscript(at.shader_location as _) + }; + attribute_desc.setFormat(conv::map_vertex_format(at.format)); + unsafe { attribute_desc.setBufferIndex(buffer_index) }; + unsafe { attribute_desc.setOffset(at.offset as _) }; + } + } + descriptor.setVertexDescriptor(Some(&vertex_descriptor)); + } + + MetalGenericRenderPipelineDescriptor::Standard(descriptor) + } + crate::VertexProcessor::Mesh { + ref task_stage, + ref mesh_stage, + } => { + // Mesh pipeline specific setup + + vs_info = None; + let descriptor = MTLMeshRenderPipelineDescriptor::new(); + + // Setup task stage + if let Some(ref task_stage) = task_stage { + let ts = self.load_shader( + task_stage, + &[], + desc.layout, + primitive_class, + naga::ShaderStage::Task, + )?; + unsafe { descriptor.setObjectFunction(Some(&ts.function)) }; + if supports_mutability { + Self::set_buffers_mutability( + &descriptor.meshBuffers(), + ts.immutable_buffer_mask, + ); + } + ts_info = Some(super::PipelineStageInfo { + immediates: desc.layout.immediates_infos.ts, + sizes_slot: desc.layout.per_stage_map.ts.sizes_buffer, + sized_bindings: ts.sized_bindings, + vertex_buffer_mappings: vec![], + library: Some(ts.library), + raw_wg_size: ts.wg_size, + work_group_memory_sizes: ts.wg_memory_sizes, + }); + } else { + ts_info = None; + } + + // Setup mesh stage + { + let ms = self.load_shader( + mesh_stage, + &[], + desc.layout, + primitive_class, + naga::ShaderStage::Mesh, + )?; + unsafe { descriptor.setMeshFunction(Some(&ms.function)) }; + if supports_mutability { + Self::set_buffers_mutability( + &descriptor.meshBuffers(), + ms.immutable_buffer_mask, + ); + } + ms_info = Some(super::PipelineStageInfo { + immediates: desc.layout.immediates_infos.ms, + sizes_slot: desc.layout.per_stage_map.ms.sizes_buffer, + sized_bindings: ms.sized_bindings, + vertex_buffer_mappings: vec![], + library: Some(ms.library), + raw_wg_size: ms.wg_size, + work_group_memory_sizes: ms.wg_memory_sizes, + }); + } + + MetalGenericRenderPipelineDescriptor::Mesh(descriptor) + } + }; + + let raw_triangle_fill_mode = match desc.primitive.polygon_mode { + wgt::PolygonMode::Fill => MTLTriangleFillMode::Fill, + wgt::PolygonMode::Line => MTLTriangleFillMode::Lines, + wgt::PolygonMode::Point => panic!( + "{:?} is not enabled for this backend", + wgt::Features::POLYGON_MODE_POINT + ), + }; + + // Fragment shader + let fs_info = match desc.fragment_stage { + Some(ref stage) => { + let fs = self.load_shader( + stage, + &[], + desc.layout, + primitive_class, + naga::ShaderStage::Fragment, + )?; + + unsafe { descriptor.setFragmentFunction(Some(&fs.function)) }; + if supports_mutability { + Self::set_buffers_mutability( + &descriptor.fragmentBuffers(), + fs.immutable_buffer_mask, + ); + } + + Some(super::PipelineStageInfo { + immediates: desc.layout.immediates_infos.fs, + sizes_slot: desc.layout.per_stage_map.fs.sizes_buffer, + sized_bindings: fs.sized_bindings, + vertex_buffer_mappings: vec![], + library: Some(fs.library), + raw_wg_size: MTLSize { + width: 0, + height: 0, + depth: 0, + }, + work_group_memory_sizes: vec![], + }) + } + None => { + // TODO: This is a workaround for what appears to be a Metal validation bug + // A pixel format is required even though no attachments are provided + if desc.color_targets.is_empty() && desc.depth_stencil.is_none() { + descriptor.setDepthAttachmentPixelFormat(MTLPixelFormat::Depth32Float); + } + None + } + }; + + // Setup pipeline color attachments + for (i, ct) in desc.color_targets.iter().enumerate() { + let at_descriptor = + unsafe { descriptor.colorAttachments().objectAtIndexedSubscript(i) }; + let ct = if let Some(color_target) = ct.as_ref() { + color_target + } else { + at_descriptor.setPixelFormat(MTLPixelFormat::Invalid); + continue; + }; + + let raw_format = self + .shared + .private_texture_format_caps + .map_format(ct.format); + at_descriptor.setPixelFormat(raw_format); + at_descriptor.setWriteMask(conv::map_color_write(ct.write_mask)); + + if let Some(ref blend) = ct.blend { + at_descriptor.setBlendingEnabled(true); + let (color_op, color_src, color_dst) = conv::map_blend_component(&blend.color); + let (alpha_op, alpha_src, alpha_dst) = conv::map_blend_component(&blend.alpha); + + at_descriptor.setRgbBlendOperation(color_op); + at_descriptor.setSourceRGBBlendFactor(color_src); + at_descriptor.setDestinationRGBBlendFactor(color_dst); + + at_descriptor.setAlphaBlendOperation(alpha_op); + at_descriptor.setSourceAlphaBlendFactor(alpha_src); + at_descriptor.setDestinationAlphaBlendFactor(alpha_dst); + } + } + + // Setup depth stencil state + let depth_stencil = match desc.depth_stencil { + Some(ref ds) => { + let raw_format = self + .shared + .private_texture_format_caps + .map_format(ds.format); + let aspects = crate::FormatAspects::from(ds.format); + if aspects.contains(crate::FormatAspects::DEPTH) { + descriptor.setDepthAttachmentPixelFormat(raw_format); + } + if aspects.contains(crate::FormatAspects::STENCIL) { + descriptor.setStencilAttachmentPixelFormat(raw_format); + } + + let ds_descriptor = create_depth_stencil_desc(ds); + let raw = self + .shared + .device + .newDepthStencilStateWithDescriptor(&ds_descriptor) + .unwrap(); + Some((raw, ds.bias)) + } + None => None, + }; + + // Setup multisample state + if desc.multisample.count != 1 { + //TODO: handle sample mask + match descriptor { + MetalGenericRenderPipelineDescriptor::Standard(ref inner) => { + #[allow(deprecated)] + inner.setSampleCount(desc.multisample.count as _); + } + MetalGenericRenderPipelineDescriptor::Mesh(ref inner) => { + unsafe { inner.setRasterSampleCount(desc.multisample.count as _) }; + } + } + descriptor.setAlphaToCoverageEnabled(desc.multisample.alpha_to_coverage_enabled); + //descriptor.set_alpha_to_one_enabled(desc.multisample.alpha_to_one_enabled); + } + + // Set debug label + if let Some(name) = desc.label { + descriptor.setLabel(Some(&NSString::from_str(name))); + } + if let Some(mv) = desc.multiview_mask { + unsafe { + descriptor.setMaxVertexAmplificationCount(mv.get().count_ones() as usize) + }; + } + + // Create the pipeline from descriptor + let raw = match descriptor { + MetalGenericRenderPipelineDescriptor::Standard(d) => self + .shared + .device + .newRenderPipelineStateWithDescriptor_error(&d), + MetalGenericRenderPipelineDescriptor::Mesh(d) => self + .shared + .device + .newRenderPipelineStateWithMeshDescriptor_options_reflection_error( + &d, + MTLPipelineOption::empty(), + None, + ), + } + .map_err(|e| { + crate::PipelineError::Linkage( + wgt::ShaderStages::VERTEX | wgt::ShaderStages::FRAGMENT, + format!("new_render_pipeline_state: {e:?}"), + ) + })?; + + self.counters.render_pipelines.add(1); + + Ok(super::RenderPipeline { + raw, + vs_info, + fs_info, + ts_info, + ms_info, + raw_primitive_type, + raw_triangle_fill_mode, + raw_front_winding: conv::map_winding(desc.primitive.front_face), + raw_cull_mode: conv::map_cull_mode(desc.primitive.cull_mode), + raw_depth_clip_mode: if self.features.contains(wgt::Features::DEPTH_CLIP_CONTROL) { + Some(if desc.primitive.unclipped_depth { + MTLDepthClipMode::Clamp + } else { + MTLDepthClipMode::Clip + }) + } else { + None + }, + depth_stencil, + }) + }) + } + + unsafe fn destroy_render_pipeline(&self, _pipeline: super::RenderPipeline) { + self.counters.render_pipelines.sub(1); + } + + unsafe fn create_compute_pipeline( + &self, + desc: &crate::ComputePipelineDescriptor< + super::PipelineLayout, + super::ShaderModule, + super::PipelineCache, + >, + ) -> Result<super::ComputePipeline, crate::PipelineError> { + autoreleasepool(|_| { + let descriptor = MTLComputePipelineDescriptor::new(); + + let module = desc.stage.module; + let cs = if let ShaderModuleSource::Passthrough(passthrough_desc) = &module.source { + let size = passthrough_desc.num_workgroups[desc.stage.entry_point]; + CompiledShader { + library: passthrough_desc.library.clone(), + function: passthrough_desc + .library + .newFunctionWithName(&NSString::from_str(desc.stage.entry_point)) + .ok_or(crate::PipelineError::EntryPoint(naga::ShaderStage::Compute))?, + wg_size: MTLSize { + width: size.0 as usize, + height: size.1 as usize, + depth: size.2 as usize, + }, + wg_memory_sizes: vec![], + sized_bindings: vec![], + immutable_buffer_mask: 0, + } + } else { + self.load_shader( + &desc.stage, + &[], + desc.layout, + MTLPrimitiveTopologyClass::Unspecified, + naga::ShaderStage::Compute, + )? + }; + + descriptor.setComputeFunction(Some(&cs.function)); + + // https://developer.apple.com/documentation/metal/mtlpipelinebufferdescriptor/mutability + if available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0) { + Self::set_buffers_mutability(&descriptor.buffers(), cs.immutable_buffer_mask); + } + + let cs_info = super::PipelineStageInfo { + library: Some(cs.library), + immediates: desc.layout.immediates_infos.cs, + sizes_slot: desc.layout.per_stage_map.cs.sizes_buffer, + sized_bindings: cs.sized_bindings, + vertex_buffer_mappings: vec![], + raw_wg_size: cs.wg_size, + work_group_memory_sizes: cs.wg_memory_sizes, + }; + + if let Some(name) = desc.label { + descriptor.setLabel(Some(&NSString::from_str(name))); + } + + let raw = self + .shared + .device + .newComputePipelineStateWithDescriptor_options_reflection_error( + &descriptor, + MTLPipelineOption::empty(), + None, + ); + + let raw: Retained<ProtocolObject<dyn MTLComputePipelineState>> = + raw.map_err(|e: Retained<NSError>| { + crate::PipelineError::Linkage( + wgt::ShaderStages::COMPUTE, + format!("new_compute_pipeline_state: {e:?}"), + ) + })?; + + self.counters.compute_pipelines.add(1); + + Ok(super::ComputePipeline { raw, cs_info }) + }) + } + + unsafe fn destroy_compute_pipeline(&self, _pipeline: super::ComputePipeline) { + self.counters.compute_pipelines.sub(1); + } + + unsafe fn create_ray_tracing_pipeline( + &self, + _desc: &crate::RayTracingPipelineDescriptor< + super::PipelineLayout, + super::ShaderModule, + super::PipelineCache, + >, + ) -> Result<super::RayTracingPipeline, crate::PipelineError> { + unimplemented!("Ray tracing pipelines are unsupported on Metal") + } + + unsafe fn destroy_ray_tracing_pipeline(&self, _pipeline: super::RayTracingPipeline) { + unimplemented!("Ray tracing pipelines are unsupported on Metal") + } + + unsafe fn get_raytracing_pipeline_group_data( + &self, + _pipeline: &super::RayTracingPipeline, + _groups: core::ops::Range<u32>, + ) -> Result<Vec<u8>, crate::DeviceError> { + unimplemented!("Ray tracing pipelines are unsupported on Metal") + } + + unsafe fn create_pipeline_cache( + &self, + _desc: &crate::PipelineCacheDescriptor<'_>, + ) -> Result<super::PipelineCache, crate::PipelineCacheError> { + Ok(super::PipelineCache) + } + unsafe fn destroy_pipeline_cache(&self, _: super::PipelineCache) {} + + unsafe fn create_query_set( + &self, + desc: &wgt::QuerySetDescriptor<crate::Label>, + ) -> DeviceResult<super::QuerySet> { + autoreleasepool(|_| { + match desc.ty { + wgt::QueryType::Occlusion => { + let size = desc.count as u64 * crate::QUERY_SIZE; + let options = MTLResourceOptions::empty(); + //TODO: HazardTrackingModeUntracked + let raw_buffer = self + .shared + .device + .newBufferWithLength_options(size as usize, options) + .unwrap(); + if let Some(label) = desc.label { + raw_buffer.setLabel(Some(&NSString::from_str(label))); + } + Ok(super::QuerySet { + raw_buffer, + counter_sample_buffer: None, + ty: desc.ty, + }) + } + wgt::QueryType::Timestamp => { + let size = desc.count as u64 * crate::QUERY_SIZE; + let device = &self.shared.device; + let destination_buffer = device + .newBufferWithLength_options(size as usize, MTLResourceOptions::empty()) + .unwrap(); + + let csb_desc = MTLCounterSampleBufferDescriptor::new(); + csb_desc.setStorageMode(MTLStorageMode::Shared); + unsafe { csb_desc.setSampleCount(desc.count as _) }; + if let Some(label) = desc.label { + csb_desc.setLabel(&NSString::from_str(label)); + } + + let counter_sets = device.counterSets().unwrap(); + let timestamp_counter = match counter_sets + .iter() + .find(|cs| &*cs.name() == ns_string!("timestamp")) + { + Some(counter) => counter, + None => { + log::error!("Failed to obtain timestamp counter set."); + return Err(crate::DeviceError::Unexpected); + } + }; + csb_desc.setCounterSet(Some(×tamp_counter)); + + let counter_sample_buffer = + match device.newCounterSampleBufferWithDescriptor_error(&csb_desc) { + Ok(buffer) => buffer, + Err(err) => { + log::error!("Failed to create counter sample buffer: {err:?}"); + return Err(crate::DeviceError::Unexpected); + } + }; + + self.counters.query_sets.add(1); + + Ok(super::QuerySet { + raw_buffer: destination_buffer, + counter_sample_buffer: Some(counter_sample_buffer), + ty: desc.ty, + }) + } + _ => { + todo!() + } + } + }) + } + + unsafe fn destroy_query_set(&self, _set: super::QuerySet) { + self.counters.query_sets.sub(1); + } + + unsafe fn create_fence(&self) -> DeviceResult<super::Fence> { + self.counters.fences.add(1); + // https://developer.apple.com/documentation/metal/mtlsharedevent + let shared_event = if available!(macos = 10.14, ios = 12.0, tvos = 12.0, visionos = 1.0) { + self.shared.device.newSharedEvent() // This should be supported on said devices, but some sandbox environments may still restrict it, making it return `None`. + } else { + None + }; + Ok(super::Fence { + sync: Arc::new((Mutex::new(0), Condvar::new())), + pending_command_buffers: RwLock::new(Vec::new()), + shared_event, + }) + } + + unsafe fn destroy_fence(&self, _fence: super::Fence) { + self.counters.fences.sub(1); + } + + unsafe fn get_fence_value(&self, fence: &super::Fence) -> DeviceResult<crate::FenceValue> { + Ok(fence.get_latest()) + } + unsafe fn wait( + &self, + fence: &super::Fence, + wait_value: crate::FenceValue, + timeout: Option<core::time::Duration>, + ) -> DeviceResult<bool> { + let (ref mutex, ref condvar) = *fence.sync; + let mut lock = mutex.lock(); + + if wait_value <= *lock { + return Ok(true); + } + + { + let pending_command_buffers = fence.pending_command_buffers.read(); + if !pending_command_buffers + .iter() + .any(|&(value, _)| value >= wait_value) + { + log::error!("No active command buffers for fence value {wait_value}"); + return Err(crate::DeviceError::Lost); + } + } + + if let Some(timeout) = timeout { + let result = condvar.wait_while_for(&mut lock, |value| *value < wait_value, timeout); + if result.timed_out() { + return Ok(*lock >= wait_value); + } + } else { + condvar.wait_while(&mut lock, |value| *value < wait_value); + } + + Ok(true) + } + + unsafe fn start_graphics_debugger_capture(&self) -> bool { + // https://developer.apple.com/documentation/metal/mtlcapturemanager + if !available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0) { + return false; + } + let device = &self.shared.device; + let shared_capture_manager = unsafe { MTLCaptureManager::sharedCaptureManager() }; + let default_capture_scope = shared_capture_manager.newCaptureScopeWithDevice(device); + shared_capture_manager.setDefaultCaptureScope(Some(&default_capture_scope)); + #[allow(deprecated)] + shared_capture_manager.startCaptureWithScope(&default_capture_scope); + default_capture_scope.beginScope(); + true + } + + unsafe fn stop_graphics_debugger_capture(&self) { + let shared_capture_manager = unsafe { MTLCaptureManager::sharedCaptureManager() }; + if let Some(default_capture_scope) = shared_capture_manager.defaultCaptureScope() { + default_capture_scope.endScope(); + } + shared_capture_manager.stopCapture(); + } + + unsafe fn get_acceleration_structure_build_sizes( + &self, + descriptor: &crate::GetAccelerationStructureBuildSizesDescriptor<super::Buffer>, + ) -> crate::AccelerationStructureBuildSizes { + let acceleration_structure_descriptor = + conv::map_acceleration_structure_descriptor(descriptor.entries, descriptor.flags); + let info = self + .shared + .device + .accelerationStructureSizesWithDescriptor(&acceleration_structure_descriptor); + crate::AccelerationStructureBuildSizes { + acceleration_structure_size: info.accelerationStructureSize as u64, + update_scratch_size: info.refitScratchBufferSize as u64, + build_scratch_size: info.buildScratchBufferSize as u64, + } + } + + unsafe fn get_acceleration_structure_device_address( + &self, + acceleration_structure: &super::AccelerationStructure, + ) -> wgt::BufferAddress { + acceleration_structure.raw.gpuResourceID().to_raw() + } + + unsafe fn create_acceleration_structure( + &self, + descriptor: &crate::AccelerationStructureDescriptor, + ) -> Result<super::AccelerationStructure, crate::DeviceError> { + // self.counters.acceleration_structures.add(1); + autoreleasepool(|_| { + Ok(super::AccelerationStructure { + raw: self + .shared + .device + .newAccelerationStructureWithSize(descriptor.size as usize) + .ok_or(crate::DeviceError::OutOfMemory)?, + }) + }) + } + + unsafe fn destroy_acceleration_structure( + &self, + _acceleration_structure: super::AccelerationStructure, + ) { + // self.counters.acceleration_structures.sub(1); + } + + fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec<u8> { + let temp = MTLIndirectAccelerationStructureInstanceDescriptor { + transformationMatrix: MTLPackedFloat4x3 { + columns: [ + MTLPackedFloat3 { + x: instance.transform[0], + y: instance.transform[4], + z: instance.transform[8], + }, + MTLPackedFloat3 { + x: instance.transform[1], + y: instance.transform[5], + z: instance.transform[9], + }, + MTLPackedFloat3 { + x: instance.transform[2], + y: instance.transform[6], + z: instance.transform[10], + }, + MTLPackedFloat3 { + x: instance.transform[3], + y: instance.transform[7], + z: instance.transform[11], + }, + ], + }, + options: MTLAccelerationStructureInstanceOptions::None, + mask: instance.mask as u32, + intersectionFunctionTableOffset: instance.pipeline_intersection_data_offset, + userID: instance.custom_data, + accelerationStructureID: unsafe { MTLResourceID::from_raw(instance.blas_address) }, + }; + + wgt::bytemuck_wrapper!(unsafe struct Desc(MTLIndirectAccelerationStructureInstanceDescriptor)); + + bytemuck::bytes_of(&Desc::wrap(temp)).to_vec() + } + + fn get_internal_counters(&self) -> wgt::HalCounters { + self.counters.as_ref().clone() + } + + fn check_if_oom(&self) -> Result<(), crate::DeviceError> { + // TODO: see https://github.com/gfx-rs/wgpu/issues/7460 + + Ok(()) + } +} diff --git a/third_party/wgpu-hal/src/metal/library_from_metallib.rs b/third_party/wgpu-hal/src/metal/library_from_metallib.rs new file mode 100644 index 000000000..23898474d --- /dev/null +++ b/third_party/wgpu-hal/src/metal/library_from_metallib.rs @@ -0,0 +1,48 @@ +use core::{ffi::c_void, ptr::NonNull}; + +use objc2::{ + msg_send, + rc::Retained, + runtime::{AnyObject, ProtocolObject}, +}; +use objc2_foundation::NSError; +use objc2_metal::{MTLDevice, MTLLibrary}; + +#[link(name = "System")] // libdispatch lives inside libSystem +extern "C" { + fn dispatch_data_create( + buffer: NonNull<c_void>, + size: usize, + queue: Option<NonNull<c_void>>, // dispatch_queue_t _Nullable + destructor: *mut c_void, // dispatch_block_t _Nullable + ) -> *mut AnyObject; // dispatch_data_t + + fn dispatch_release(object: *mut AnyObject); // dispatch_data_t +} + +const DISPATCH_DATA_DESTRUCTOR_DEFAULT: *mut c_void = core::ptr::null_mut(); + +/// See <https://github.com/madsmtm/objc2/issues/819>. +/// +/// newLibraryWithData requires DispatchData, part of the Dispatch library. +/// Not only is this a big additional dependency for a glorified `Vec<u8>` +/// in this case, but the code in the dispatch2 rust crate is a WIP and +/// Mozilla has decided not to vendor it yet. +/// +/// This function allows us to deal with DispatchData without dealing +/// with these problems. +pub(crate) fn new_library_from_metallib_bytes( + device: &ProtocolObject<dyn MTLDevice>, + data: &[u8], +) -> Result<Retained<ProtocolObject<dyn MTLLibrary>>, Retained<NSError>> { + let buffer = NonNull::new(data.as_ptr().cast_mut()).unwrap().cast(); + let data = + unsafe { dispatch_data_create(buffer, data.len(), None, DISPATCH_DATA_DESTRUCTOR_DEFAULT) }; + assert!(!data.is_null()); + + let res: Result<Retained<ProtocolObject<dyn MTLLibrary>>, Retained<NSError>> = + unsafe { msg_send![device, newLibraryWithData: data, error: _] }; + + unsafe { dispatch_release(data) }; + res +} diff --git a/third_party/wgpu-hal/src/metal/mod.rs b/third_party/wgpu-hal/src/metal/mod.rs new file mode 100644 index 000000000..e227590e5 --- /dev/null +++ b/third_party/wgpu-hal/src/metal/mod.rs @@ -0,0 +1,1410 @@ +/*! +# Metal API internals. + +## Pipeline Layout + +In Metal, immediates, vertex buffers, and resources in the bind groups +are all placed together in the native resource bindings, which work similarly to D3D11: +there are tables of textures, buffers, and samplers. + +We put immediates first (if any) in the table, followed by bind group 0 +resources, followed by other bind groups. The vertex buffers are bound at the very +end of the VS buffer table. + +!*/ + +#[allow( + deprecated, + reason = "MTLFeatureSet` is superseded by `MTLGpuFamily`. + However, `MTLGpuFamily` is only supported starting MacOS 10.15, whereas our minimum target is MacOS 10.13, + See https://github.com/gpuweb/gpuweb/issues/1069 for minimum spec. + TODO: Eventually all deprecated features should be abstracted and use new api when available." +)] +mod adapter; +mod command; +mod conv; +mod device; +mod library_from_metallib; +mod surface; +mod time; + +use alloc::{ + string::{String, ToString as _}, + sync::Arc, + vec::Vec, +}; +use core::{fmt, iter, ops, ptr::NonNull, sync::atomic}; +use std::sync::OnceLock; + +use bitflags::bitflags; +use hashbrown::HashMap; +use naga::FastHashMap; +use objc2::{ + available, + rc::{autoreleasepool, Retained}, + runtime::ProtocolObject, +}; +use objc2_foundation::ns_string; +use objc2_metal::{ + MTLAccelerationStructure, MTLAccelerationStructureCommandEncoder, MTLArgumentBuffersTier, + MTLBlitCommandEncoder, MTLBuffer, MTLCommandBuffer, MTLCommandBufferStatus, MTLCommandQueue, + MTLComputeCommandEncoder, MTLComputePipelineState, MTLCounterSampleBuffer, MTLCullMode, + MTLDepthClipMode, MTLDepthStencilState, MTLDevice, MTLDrawable, MTLIndexType, + MTLLanguageVersion, MTLLibrary, MTLPrimitiveType, MTLReadWriteTextureTier, + MTLRenderCommandEncoder, MTLRenderPipelineState, MTLRenderStages, MTLResource, + MTLResourceUsage, MTLSamplerState, MTLSharedEvent, MTLSize, MTLTexture, MTLTextureType, + MTLTriangleFillMode, MTLWinding, +}; +use objc2_quartz_core::CAMetalLayer; +use parking_lot::{Condvar, Mutex, RwLock}; + +#[derive(Clone, Debug)] +pub struct Api; + +type ResourceIndex = u32; + +impl crate::Api for Api { + const VARIANT: wgt::Backend = wgt::Backend::Metal; + + type Instance = Instance; + type Surface = Surface; + type Adapter = Adapter; + type Device = Device; + + type Queue = Queue; + type CommandEncoder = CommandEncoder; + type CommandBuffer = CommandBuffer; + + type Buffer = Buffer; + type Texture = Texture; + type SurfaceTexture = SurfaceTexture; + type TextureView = TextureView; + type Sampler = Sampler; + type QuerySet = QuerySet; + type Fence = Fence; + + type BindGroupLayout = BindGroupLayout; + type BindGroup = BindGroup; + type PipelineLayout = PipelineLayout; + type ShaderModule = ShaderModule; + type RenderPipeline = RenderPipeline; + type RayTracingPipeline = RayTracingPipeline; + type ComputePipeline = ComputePipeline; + type PipelineCache = PipelineCache; + + type AccelerationStructure = AccelerationStructure; +} + +crate::impl_dyn_resource!( + Adapter, + AccelerationStructure, + BindGroup, + BindGroupLayout, + Buffer, + CommandBuffer, + CommandEncoder, + ComputePipeline, + Device, + Fence, + Instance, + PipelineCache, + PipelineLayout, + QuerySet, + Queue, + RenderPipeline, + RayTracingPipeline, + Sampler, + ShaderModule, + Surface, + SurfaceTexture, + Texture, + TextureView +); + +/// Provides availability information about Mac APIs. +/// +/// This may include Metal features that depend only on software support. +/// Features with varying hardware support are in [`CapabilitiesQuery`] +/// +/// When feature detection is only needed once, it may also be done inline. +struct OsFeatures; + +impl OsFeatures { + fn display_sync() -> bool { + // https://developer.apple.com/documentation/quartzcore/cametallayer/displaysyncenabled + available!(macos = 10.13) || cfg!(target_abi = "macabi") + } +} + +#[derive(Debug)] +pub struct Instance { + flags: wgt::InstanceFlags, +} + +impl Instance { + pub fn create_surface_from_layer(&self, layer: &CAMetalLayer) -> Surface { + Surface::from_layer(layer) + } +} + +impl crate::Instance for Instance { + type A = Api; + + unsafe fn init(desc: &crate::InstanceDescriptor<'_>) -> Result<Self, crate::InstanceError> { + profiling::scope!("Init Metal Backend"); + // We do not enable metal validation based on the validation flags as it affects the entire + // process. Instead, we enable the validation inside the test harness itself in tests/src/native.rs. + Ok(Instance { flags: desc.flags }) + } + + unsafe fn create_surface( + &self, + display_handle: raw_window_handle::RawDisplayHandle, + window_handle: raw_window_handle::RawWindowHandle, + ) -> Result<Surface, crate::InstanceError> { + let layer = match (display_handle, window_handle) { + ( + raw_window_handle::RawDisplayHandle::AppKit(_), + raw_window_handle::RawWindowHandle::AppKit(handle), + ) => unsafe { raw_window_metal::Layer::from_ns_view(handle.ns_view) }, + ( + raw_window_handle::RawDisplayHandle::UiKit(_), + raw_window_handle::RawWindowHandle::UiKit(handle), + ) => unsafe { raw_window_metal::Layer::from_ui_view(handle.ui_view) }, + _ => { + return Err(crate::InstanceError::new(format!( + "window handle {window_handle:?} is not a Metal-compatible handle" + ))) + } + }; + + // SAFETY: The layer is an initialized instance of `CAMetalLayer`, and + // we transfer the retain count to `Retained` using `into_raw`. + let layer = unsafe { + Retained::from_raw(layer.into_raw().cast::<CAMetalLayer>().as_ptr()).unwrap() + }; + + Ok(Surface::new(layer)) + } + + unsafe fn enumerate_adapters( + &self, + _surface_hint: Option<&Surface>, + ) -> Vec<crate::ExposedAdapter<Api>> { + let devices = objc2_metal::MTLCopyAllDevices(); + let instance_flags = self.flags; + let mut adapters: Vec<crate::ExposedAdapter<Api>> = devices + .into_iter() + .map(|d| AdapterShared::expose(d, instance_flags)) + .collect(); + adapters.sort_by_key(|ad| { + ( + ad.adapter.shared.private_caps.low_power, + ad.adapter.shared.private_caps.headless, + ) + }); + adapters + } +} + +bitflags!( + /// Similar to `MTLCounterSamplingPoint`, but a bit higher abstracted for our purposes. + #[derive(Debug, Copy, Clone)] + pub struct TimestampQuerySupport: u32 { + /// On creating Metal encoders. + const STAGE_BOUNDARIES = 1 << 1; + /// Within existing draw encoders. + const ON_RENDER_ENCODER = Self::STAGE_BOUNDARIES.bits() | (1 << 2); + /// Within existing dispatch encoders. + const ON_COMPUTE_ENCODER = Self::STAGE_BOUNDARIES.bits() | (1 << 3); + /// Within existing blit encoders. + const ON_BLIT_ENCODER = Self::STAGE_BOUNDARIES.bits() | (1 << 4); + + /// Within any wgpu render/compute pass. + const INSIDE_WGPU_PASSES = Self::ON_RENDER_ENCODER.bits() | Self::ON_COMPUTE_ENCODER.bits(); + } +); + +#[allow(dead_code)] +struct CapabilitiesQuery { + msl_version: MTLLanguageVersion, + fragment_rw_storage: bool, + read_write_texture_tier: MTLReadWriteTextureTier, + msaa_desktop: bool, + msaa_apple3: bool, + msaa_apple7: bool, + resource_heaps: bool, + argument_buffers: Option<MTLArgumentBuffersTier>, + mutable_comparison_samplers: bool, + sampler_clamp_to_border: bool, + indirect_draw_dispatch: bool, + base_vertex_first_instance_drawing: bool, + dual_source_blending: bool, + low_power: bool, + headless: bool, + layered_rendering: bool, + function_specialization: bool, + depth_clip_mode: bool, + texture_cube_array: bool, + supports_float_filtering: bool, + format_depth24_stencil8: bool, + format_depth32_stencil8_filter: bool, + format_depth32_stencil8_none: bool, + format_min_srgb_channels: u8, + format_b5: bool, + format_bc: bool, + format_eac_etc: bool, + format_astc: bool, + format_astc_hdr: bool, + format_astc_3d: bool, + format_any8_unorm_srgb_all: bool, + format_any8_unorm_srgb_no_write: bool, + format_any8_snorm_all: bool, + format_r16_norm_all: bool, + format_r32_all: bool, + format_r32_no_write: bool, + format_r32float_no_write_no_filter: bool, + format_r32float_no_filter: bool, + format_r32float_all: bool, + format_rgba8_srgb_all: bool, + format_rgba8_srgb_no_write: bool, + format_rgb10a2_unorm_all: bool, + format_rgb10a2_unorm_no_write: bool, + format_rgb10a2_uint_write: bool, + format_rg11b10_all: bool, + format_rg11b10_no_write: bool, + format_rgb9e5_all: bool, + format_rgb9e5_no_write: bool, + format_rgb9e5_filter_only: bool, + format_rg32_color: bool, + format_rg32_color_write: bool, + format_rg32float_all: bool, + format_rg32float_color_blend: bool, + format_rg32float_no_filter: bool, + format_rgba32int_color: bool, + format_rgba32int_color_write: bool, + format_rgba32float_color: bool, + format_rgba32float_color_write: bool, + format_rgba32float_all: bool, + format_depth16unorm: bool, + format_depth16unorm_filter: bool, + format_depth32float_filter: bool, + format_depth32float_none: bool, + format_bgr10a2_all: bool, + format_bgr10a2_no_write: bool, + max_textures_per_stage: (ResourceIndex, ResourceIndex), + max_binding_array_elements: ResourceIndex, + max_sampler_binding_array_elements: ResourceIndex, + buffer_alignment: u64, + constant_buffer_offset_alignment: u32, + max_buffer_size: u64, + max_texture_size: u64, + max_texture_3d_size: u64, + max_texture_layers: u64, + max_fragment_input_components: u64, + max_color_render_targets: u8, + max_color_attachment_bytes_per_sample: u8, + max_inter_stage_shader_variables: u32, + max_threads_per_group: u32, + max_total_threadgroup_memory: u32, + sample_count_mask: crate::TextureFormatCapabilities, + supports_debug_markers: bool, + supports_binary_archives: bool, + supports_arrays_of_textures: bool, + supports_arrays_of_textures_write: bool, + supports_depth_clip_control: bool, + supports_shader_primitive_index: bool, + has_unified_memory: Option<bool>, + timestamp_query_support: TimestampQuerySupport, + supports_simd_scoped_operations: bool, + supports_cooperative_matrix: bool, + int64: bool, + int64_atomics_min_max: bool, + int64_atomics: bool, + float_atomics: bool, + mesh_shaders: bool, + max_task_workgroup_count: u32, + max_mesh_workgroup_count: u32, + max_task_payload_size: u32, + supported_vertex_amplification_factor: u32, + shader_barycentrics: bool, + supports_memoryless_storage: bool, + supports_raytracing: bool, + shader_per_vertex: bool, + supports_multisample_array: bool, +} + +#[derive(Debug)] +struct PrivateCapabilities { + msl_version: MTLLanguageVersion, + low_power: bool, + headless: bool, + has_unified_memory: Option<bool>, + timestamp_query_support: TimestampQuerySupport, + supports_memoryless_storage: bool, + mesh_shaders: bool, +} + +#[derive(Debug)] +struct PrivateTextureFormatCapabilities { + read_write_texture_tier: MTLReadWriteTextureTier, + sample_count_mask: crate::TextureFormatCapabilities, + int64_atomics: bool, + msaa_desktop: bool, + msaa_apple3: bool, + msaa_apple7: bool, + format_r32float_all: bool, + format_rgba8_srgb_all: bool, + format_rgb10a2_uint_write: bool, + format_rgb10a2_unorm_all: bool, + format_rg11b10_all: bool, + format_rg32float_all: bool, + format_rgba32float_all: bool, + format_depth16unorm: bool, + format_depth16unorm_filter: bool, + format_depth32float_filter: bool, + format_depth24_stencil8: bool, + format_bc: bool, + format_eac_etc: bool, + format_astc: bool, + format_astc_hdr: bool, +} + +#[derive(Clone, Debug)] +struct PrivateDisabilities { + /// Near depth is not respected properly on some Intel GPUs. + broken_viewport_near_depth: bool, + /// Multi-target clears don't appear to work properly on Intel GPUs. + #[allow(dead_code)] + broken_layered_clear_image: bool, +} + +#[derive(Debug)] +struct Settings { + retain_command_buffer_references: bool, +} + +impl Default for Settings { + fn default() -> Self { + Self { + retain_command_buffer_references: true, + } + } +} + +#[derive(Debug)] +struct AdapterShared { + device: Retained<ProtocolObject<dyn MTLDevice>>, + disabilities: PrivateDisabilities, + private_caps: PrivateCapabilities, + private_texture_format_caps: PrivateTextureFormatCapabilities, + settings: Settings, + presentation_timer: time::PresentationTimer, +} + +#[cfg(send_sync)] +static_assertions::assert_impl_all!(AdapterShared: Send, Sync); + +impl AdapterShared { + fn new( + device: Retained<ProtocolObject<dyn MTLDevice>>, + capabilities_query: &CapabilitiesQuery, + ) -> Self { + let private_caps = capabilities_query.private_capabilities(); + let private_texture_format_caps = capabilities_query.private_texture_format_capabilities(); + log::debug!("{private_caps:#?}"); + log::debug!("{private_texture_format_caps:#?}"); + + Self { + disabilities: PrivateDisabilities::new(&device), + private_caps, + private_texture_format_caps, + device, + settings: Settings::default(), + presentation_timer: time::PresentationTimer::new(), + } + } + + fn expose( + device: Retained<ProtocolObject<dyn MTLDevice>>, + instance_flags: wgt::InstanceFlags, + ) -> crate::ExposedAdapter<Api> { + autoreleasepool(|_| { + let name = device.name().to_string(); + let capabilities_query = CapabilitiesQuery::new(&device); + let shared = AdapterShared::new(device, &capabilities_query); + let features = capabilities_query.features(); + let capabilities = capabilities_query.capabilities(instance_flags); + crate::ExposedAdapter { + info: wgt::AdapterInfo { + name, + // These are hardcoded based on typical values for Metal devices + // + // See <https://github.com/gpuweb/gpuweb/blob/main/proposals/subgroups.md#adapter-info> + // for more information. + subgroup_min_size: 4, + subgroup_max_size: 64, + transient_saves_memory: Some(shared.private_caps.supports_memoryless_storage), + ..wgt::AdapterInfo::new(shared.private_caps.device_type(), wgt::Backend::Metal) + }, + features, + capabilities, + adapter: Adapter::new(Arc::new(shared)), + } + }) + } +} + +#[derive(Debug)] +pub struct Adapter { + shared: Arc<AdapterShared>, +} + +#[cfg(send_sync)] +static_assertions::assert_impl_all!(Adapter: Send, Sync); + +#[derive(Debug)] +pub struct Queue { + shared: Arc<QueueShared>, + timestamp_period: f32, +} + +#[cfg(send_sync)] +static_assertions::assert_impl_all!(Queue: Send, Sync); + +impl Queue { + pub unsafe fn queue_from_raw( + raw: Retained<ProtocolObject<dyn MTLCommandQueue>>, + timestamp_period: f32, + ) -> Self { + Self { + shared: Arc::new(QueueShared { + raw, + command_buffer_created_not_submitted: atomic::AtomicUsize::new(0), + pending_waits: Mutex::new(Vec::new()), + pending_signals: Mutex::new(Vec::new()), + relay: OnceLock::new(), + }), + timestamp_period, + } + } + + pub fn as_raw(&self) -> &ProtocolObject<dyn MTLCommandQueue> { + &self.shared.raw + } + + /// Enable strict GPU-side ordering for [`Self::add_wait_event`]. + /// + /// By default, `add_wait_event` encodes the wait on a separate + /// internal command buffer. Metal allows independent command + /// buffers in a queue to overlap on the GPU, so a wait CB does + /// not strictly gate subsequent user command buffers when those + /// CBs share no Metal-tracked resources with it. Single-stream + /// pipelines often serialize anyway because the GPU has no other + /// concurrent work to fill the slot, but mixed workloads (decode + /// + compute + render) can race. + /// + /// When enabled, every [`crate::CommandEncoder::begin_encoding`] + /// pre-encodes a wait on an internal `MTLSharedEvent` at the start + /// of the new command buffer; every [`crate::Queue::submit`] then + /// signals that event after draining the staged external waits. + /// All command buffers since the previous submit are released in + /// lockstep once the foreign signals arrive, regardless of GPU + /// concurrency. + /// + /// Costs one extra `encodeWaitForEvent` per command buffer plus + /// one extra internal command buffer per submit on this queue. + /// Other queues are unaffected. + /// + /// Idempotent. Cannot be disabled - once enabled, the queue stays + /// in strict mode for its lifetime, since command buffers already + /// encoded would be stranded if the relay stopped firing. + pub fn enable_strict_event_sync(&self) -> Result<(), crate::DeviceError> { + if self.shared.relay.get().is_some() { + return Ok(()); + } + let event = self + .shared + .raw + .device() + .newSharedEvent() + .ok_or(crate::DeviceError::OutOfMemory)?; + let _ = self.shared.relay.set(Relay { + event, + next_release_value: atomic::AtomicU64::new(1), + commit_lock: Mutex::new(()), + }); + Ok(()) + } + + /// Stage an `MTLCommandBuffer::encodeWaitForEvent(event, value)` for + /// the next [`crate::Queue::submit`]. Lets external producers be waited + /// on without a CPU block. + /// + /// By default the wait is encoded onto a dedicated internal command + /// buffer committed before the submit's user CBs - best-effort under + /// cross-CB GPU concurrency, see [`Self::enable_strict_event_sync`] + /// for strict gating. With strict mode enabled, the wait is chained + /// through an internal relay event that gates every user command + /// buffer encoded since the previous submit. + /// + /// Staging is queue-wide, not per-thread or per-submit: any + /// `add_wait_event` call is consumed by whichever + /// [`crate::Queue::submit`] runs next on this queue. If you stage + /// events from multiple threads, coordinate the staging and the + /// submit yourself, or another thread's submit may drain your + /// pending waits. + pub fn add_wait_event(&self, event: Retained<ProtocolObject<dyn MTLSharedEvent>>, value: u64) { + self.shared.pending_waits.lock().push((event, value)); + } + + /// Remove `event` from the pending wait list if it is still present. + /// Returns `true` if it was found and removed. + pub fn remove_wait_event(&self, event: &ProtocolObject<dyn MTLSharedEvent>) -> bool { + let target: *const ProtocolObject<dyn MTLSharedEvent> = event; + let mut waits = self.shared.pending_waits.lock(); + let before = waits.len(); + waits.retain(|(e, _)| Retained::as_ptr(e) != target); + waits.len() != before + } + + /// Stage an `MTLCommandBuffer::encodeSignalEvent(event, value)` for + /// the next [`crate::Queue::submit`]. The signal is encoded after + /// the submit's own completion signal, so a foreign API waiting on + /// `(event, value)` observes the wgpu work as done. + /// + /// Staging is queue-wide, not per-thread or per-submit: see + /// [`Self::add_wait_event`] for the threading caveat. + pub fn add_signal_event( + &self, + event: Retained<ProtocolObject<dyn MTLSharedEvent>>, + value: u64, + ) { + self.shared.pending_signals.lock().push((event, value)); + } + + /// Remove `event` from the pending signal list if it is still present. + /// Returns `true` if it was found and removed. + pub fn remove_signal_event(&self, event: &ProtocolObject<dyn MTLSharedEvent>) -> bool { + let target: *const ProtocolObject<dyn MTLSharedEvent> = event; + let mut signals = self.shared.pending_signals.lock(); + let before = signals.len(); + signals.retain(|(e, _)| Retained::as_ptr(e) != target); + signals.len() != before + } +} + +type PendingEvents = Mutex<Vec<(Retained<ProtocolObject<dyn MTLSharedEvent>>, u64)>>; + +/// Internal relay used by [`Queue::enable_strict_event_sync`] to chain +/// staged waits across all CBs in a submit. +/// +/// `begin_encoding` reads `next_release_value` and pre-encodes +/// `encodeWaitForEvent(event, expected)` at the start of each CB. +/// `submit` claims the value via `fetch_add`, encodes the foreign +/// waits + `encodeSignalEvent(event, claimed)` on a wait CB, and +/// commits it. `commit_lock` serializes the claim+commit pair so +/// concurrent submits land their signals in monotonic *commit* order +/// on the CPU. GPU-side execution of the resulting wait CBs may still +/// reorder under concurrency; see the comment in `submit` for why +/// that's harmless. +#[derive(Debug)] +struct Relay { + event: Retained<ProtocolObject<dyn MTLSharedEvent>>, + next_release_value: atomic::AtomicU64, + commit_lock: Mutex<()>, +} + +#[derive(Debug)] +pub struct QueueShared { + raw: Retained<ProtocolObject<dyn MTLCommandQueue>>, + // Tracks command buffers created via `CommandEncoder::begin_encoding` that + // have not yet been submitted or discarded. Used to proactively fail + // before hitting Metal's `maxCommandBufferCount`. + // + // (In a few places we call `.commandBuffer{,WithUnretainedReferences}` directly + // to create command buffers for internal purposes. In those cases we always + // commit the buffer immediately, so we don't adjust the counter for them.) + command_buffer_created_not_submitted: atomic::AtomicUsize, + pending_waits: PendingEvents, + pending_signals: PendingEvents, + relay: OnceLock<Relay>, +} + +#[derive(Debug)] +pub struct Device { + shared: Arc<AdapterShared>, + features: wgt::Features, + counters: Arc<wgt::HalCounters>, + limits: wgt::Limits, +} + +#[derive(Debug)] +pub struct Surface { + render_layer: Mutex<Retained<CAMetalLayer>>, + swapchain_format: RwLock<Option<wgt::TextureFormat>>, + extent: RwLock<wgt::Extent3d>, +} + +unsafe impl Send for Surface {} +unsafe impl Sync for Surface {} + +#[derive(Debug)] +pub struct SurfaceTexture { + texture: Texture, + // Useful for UI-intensive applications that are sensitive to + // window resizing. + drawable: Retained<ProtocolObject<dyn MTLDrawable>>, + present_with_transaction: bool, +} + +impl crate::DynSurfaceTexture for SurfaceTexture {} + +impl core::borrow::Borrow<Texture> for SurfaceTexture { + fn borrow(&self) -> &Texture { + &self.texture + } +} + +impl core::borrow::Borrow<dyn crate::DynTexture> for SurfaceTexture { + fn borrow(&self) -> &dyn crate::DynTexture { + &self.texture + } +} + +unsafe impl Send for SurfaceTexture {} +unsafe impl Sync for SurfaceTexture {} + +impl crate::Queue for Queue { + type A = Api; + + unsafe fn submit( + &self, + command_buffers: &[&CommandBuffer], + _surface_textures: &[&SurfaceTexture], + (signal_fence, signal_value): (&Fence, crate::FenceValue), + ) -> Result<(), crate::DeviceError> { + autoreleasepool(|_| { + // Drain caller-staged waits onto a dedicated command buffer + // committed before the user CBs. + // + // When strict event sync is enabled, this CB also signals + // the relay event to release every user CB encoded since + // the previous submit (see `Queue::enable_strict_event_sync`). + // The `commit_lock` is held across `fetch_add` + `commit` so + // concurrent submits land their relay signals in monotonic + // *commit* order on the CPU side; otherwise a later-claimed + // signal could commit first and the subsequent backward + // signal would temporarily regress the relay's signaledValue. + // + // GPU-side ordering across independent wait CBs remains + // best-effort: Metal may run them in parallel, so a wait CB + // with foreign waits can fire its signal after a later + // submit's wait-free signal. CBs already released stay + // released (`MTLSharedEvent` waits are `>=`), and future + // submits' signals catch the value back up, so the regression + // is harmless - but users wanting strict GPU-side ordering + // across concurrent submits must serialize submits themselves. + // + // Without strict mode, we only emit a wait CB when there are + // pending waits - keeps the common-case submit overhead-free. + { + let relay = self.shared.relay.get(); + let mut waits = self.shared.pending_waits.lock(); + if relay.is_some() || !waits.is_empty() { + let _commit_guard = relay.map(|r| r.commit_lock.lock()); + // We do not bother adjusting `command_buffer_created_not_submitted` + // because we immediately commit this buffer. + let wait_cb = self + .shared + .raw + .commandBufferWithUnretainedReferences() + .ok_or(crate::DeviceError::Lost)?; + wait_cb.setLabel(Some(ns_string!("(wgpu internal) Wait"))); + for (event, value) in waits.drain(..) { + wait_cb.encodeWaitForEvent_value(event.as_ref(), value); + } + if let Some(relay) = relay { + let release = relay + .next_release_value + .fetch_add(1, atomic::Ordering::AcqRel); + wait_cb.encodeSignalEvent_value(relay.event.as_ref(), release); + } + wait_cb.commit(); + } + } + + let extra_command_buffer = { + let fence_sync = Arc::clone(&signal_fence.sync); + let block = block2::RcBlock::new(move |_cmd_buf| { + *fence_sync.0.lock() = signal_value; + fence_sync.1.notify_all(); + }); + + let raw = match command_buffers.last() { + Some(&cmd_buf) => cmd_buf.raw.clone(), + None => { + // We do not bother adjusting `command_buffer_created_not_submitted` + // because we immediately commit this buffer. + self.shared + .raw + .commandBufferWithUnretainedReferences() + .ok_or(crate::DeviceError::Lost)? + } + }; + raw.setLabel(Some(ns_string!("(wgpu internal) Signal"))); + unsafe { raw.addCompletedHandler(block2::RcBlock::as_ptr(&block)) }; + + signal_fence.maintain(); + signal_fence + .pending_command_buffers + .write() + .push((signal_value, raw.clone())); + + if let Some(shared_event) = &signal_fence.shared_event { + raw.encodeSignalEvent_value(shared_event.as_ref(), signal_value); + } + + // Drain caller-staged signals after our own signal so each + // additional event value publishes once the submit completes. + { + let mut signals = self.shared.pending_signals.lock(); + for (event, value) in signals.drain(..) { + raw.encodeSignalEvent_value(event.as_ref(), value); + } + } + + // only return an extra one if it's extra + match command_buffers.last() { + Some(_) => None, + None => Some(raw), + } + }; + + for cmd_buffer in command_buffers { + cmd_buffer.raw.commit(); + // One command buffer per `end_encoding` call moves from the + // "created but not yet submitted" bucket into the submitted + // set, so update the counter. + let previous = self + .shared + .command_buffer_created_not_submitted + .fetch_sub(1, atomic::Ordering::AcqRel); + debug_assert!(previous > 0); + } + + if let Some(raw) = extra_command_buffer { + raw.commit(); + } + Ok(()) + }) + } + unsafe fn present( + &self, + _surface: &Surface, + texture: SurfaceTexture, + ) -> Result<(), crate::SurfaceError> { + autoreleasepool(|_| { + // We do not bother adjusting `command_buffer_created_not_submitted` + // because we immediately commit this buffer. + let command_buffer = self.shared.raw.commandBuffer().unwrap(); + command_buffer.setLabel(Some(ns_string!("(wgpu internal) Present"))); + + // https://developer.apple.com/documentation/quartzcore/cametallayer/1478157-presentswithtransaction?language=objc + if !texture.present_with_transaction { + command_buffer.presentDrawable(&texture.drawable); + } + + command_buffer.commit(); + + if texture.present_with_transaction { + command_buffer.waitUntilScheduled(); + texture.drawable.present(); + } + }); + Ok(()) + } + + unsafe fn get_timestamp_period(&self) -> f32 { + self.timestamp_period + } + + unsafe fn wait_for_idle(&self) -> Result<(), crate::DeviceError> { + autoreleasepool(|_| { + let command_buffer = self.shared.raw.commandBuffer().unwrap(); + command_buffer.setLabel(Some(ns_string!("(wgpu internal) wait_for_idle"))); + command_buffer.commit(); + command_buffer.waitUntilCompleted(); + }); + Ok(()) + } +} + +#[derive(Debug)] +pub struct Buffer { + raw: Retained<ProtocolObject<dyn MTLBuffer>>, + size: wgt::BufferAddress, +} + +unsafe impl Send for Buffer {} +unsafe impl Sync for Buffer {} + +impl crate::DynBuffer for Buffer {} + +impl Buffer { + fn as_raw(&self) -> NonNull<ProtocolObject<dyn MTLBuffer>> { + unsafe { NonNull::new_unchecked(Retained::as_ptr(&self.raw) as *mut _) } + } +} + +impl crate::BufferBinding<'_, Buffer> { + fn resolve_size(&self) -> wgt::BufferAddress { + match self.size { + Some(size) => size.get(), + None => self.buffer.size - self.offset, + } + } +} + +#[derive(Debug)] +pub struct Texture { + raw: Retained<ProtocolObject<dyn MTLTexture>>, + format: wgt::TextureFormat, + raw_type: MTLTextureType, + array_layers: u32, + mip_levels: u32, + copy_size: crate::CopyExtent, + + // The `drop_guard` field must be the last field of this struct so it is dropped last. + // Do not add new fields after it. + _drop_guard: Option<crate::DropGuard>, +} + +impl Texture { + pub fn raw_handle(&self) -> &ProtocolObject<dyn MTLTexture> { + &self.raw + } +} + +impl crate::DynTexture for Texture {} + +unsafe impl Send for Texture {} +unsafe impl Sync for Texture {} + +#[derive(Debug)] +pub struct TextureView { + raw: Retained<ProtocolObject<dyn MTLTexture>>, + aspects: crate::FormatAspects, +} + +impl crate::DynTextureView for TextureView {} + +unsafe impl Send for TextureView {} +unsafe impl Sync for TextureView {} + +impl TextureView { + fn as_raw(&self) -> NonNull<ProtocolObject<dyn MTLTexture>> { + unsafe { NonNull::new_unchecked(Retained::as_ptr(&self.raw) as *mut _) } + } +} + +#[derive(Debug)] +pub struct Sampler { + raw: Retained<ProtocolObject<dyn MTLSamplerState>>, +} + +impl crate::DynSampler for Sampler {} + +#[cfg(send_sync)] +static_assertions::assert_impl_all!(Sampler: Send, Sync); + +impl Sampler { + fn as_raw(&self) -> NonNull<ProtocolObject<dyn MTLSamplerState>> { + unsafe { NonNull::new_unchecked(Retained::as_ptr(&self.raw) as *mut _) } + } +} + +#[derive(Debug)] +pub struct BindGroupLayout { + /// Sorted list of BGL entries. + entries: Arc<[wgt::BindGroupLayoutEntry]>, +} + +impl crate::DynBindGroupLayout for BindGroupLayout {} + +#[derive(Clone, Debug, Default)] +struct ResourceData<T> { + buffers: T, + textures: T, + samplers: T, +} + +#[derive(Clone, Debug, Default)] +struct MultiStageData<T> { + vs: T, + fs: T, + cs: T, + ts: T, + ms: T, +} + +const NAGA_STAGES: MultiStageData<naga::ShaderStage> = MultiStageData { + vs: naga::ShaderStage::Vertex, + fs: naga::ShaderStage::Fragment, + cs: naga::ShaderStage::Compute, + ts: naga::ShaderStage::Task, + ms: naga::ShaderStage::Mesh, +}; + +impl<T> ops::Index<naga::ShaderStage> for MultiStageData<T> { + type Output = T; + fn index(&self, stage: naga::ShaderStage) -> &T { + match stage { + naga::ShaderStage::Vertex => &self.vs, + naga::ShaderStage::Fragment => &self.fs, + naga::ShaderStage::Compute => &self.cs, + naga::ShaderStage::Task => &self.ts, + naga::ShaderStage::Mesh => &self.ms, + naga::ShaderStage::RayGeneration + | naga::ShaderStage::AnyHit + | naga::ShaderStage::ClosestHit + | naga::ShaderStage::Miss => unimplemented!(), + } + } +} + +impl<T> MultiStageData<T> { + fn map_ref<Y>(&self, fun: impl Fn(&T) -> Y) -> MultiStageData<Y> { + MultiStageData { + vs: fun(&self.vs), + fs: fun(&self.fs), + cs: fun(&self.cs), + ts: fun(&self.ts), + ms: fun(&self.ms), + } + } + fn map<Y>(self, fun: impl Fn(T) -> Y) -> MultiStageData<Y> { + MultiStageData { + vs: fun(self.vs), + fs: fun(self.fs), + cs: fun(self.cs), + ts: fun(self.ts), + ms: fun(self.ms), + } + } + fn iter<'a>(&'a self) -> impl Iterator<Item = &'a T> { + iter::once(&self.vs) + .chain(iter::once(&self.fs)) + .chain(iter::once(&self.cs)) + .chain(iter::once(&self.ts)) + .chain(iter::once(&self.ms)) + } + fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut T> { + iter::once(&mut self.vs) + .chain(iter::once(&mut self.fs)) + .chain(iter::once(&mut self.cs)) + .chain(iter::once(&mut self.ts)) + .chain(iter::once(&mut self.ms)) + } +} + +type MultiStageResourceCounters = MultiStageData<ResourceData<ResourceIndex>>; +type MultiStageResources = MultiStageData<naga::back::msl::EntryPointResources>; + +#[derive(Debug)] +struct BindGroupLayoutInfo { + base_resource_indices: MultiStageResourceCounters, +} + +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +struct ImmediateDataInfo { + count: u32, + buffer_index: ResourceIndex, +} + +#[derive(Debug)] +pub struct PipelineLayout { + bind_group_infos: [Option<BindGroupLayoutInfo>; crate::MAX_BIND_GROUPS], + immediates_infos: MultiStageData<Option<ImmediateDataInfo>>, + total_immediates: u32, + per_stage_map: MultiStageResources, + binding_array_length_map: FastHashMap<naga::ResourceBinding, u32>, +} + +impl crate::DynPipelineLayout for PipelineLayout {} + +#[derive(Debug)] +enum BufferLikeResource { + Buffer { + ptr: NonNull<ProtocolObject<dyn MTLBuffer>>, + offset: wgt::BufferAddress, + dynamic_index: Option<u32>, + + /// The buffer's size, if it is a [`Storage`] binding. Otherwise `None`. + /// + /// Buffers with the [`wgt::BufferBindingType::Storage`] binding type can + /// hold WGSL runtime-sized arrays. When one does, we must pass its size to + /// shader entry points to implement bounds checks and WGSL's `arrayLength` + /// function. See `device::CompiledShader::sized_bindings` for details. + /// + /// [`Storage`]: wgt::BufferBindingType::Storage + binding_size: Option<wgt::BufferSize>, + + binding_location: u32, + }, + + /// Bindless storage `binding_array`: one argument [`MTLBuffer`] (pointer table) plus element + /// byte sizes `(array index, size)` for `_buffer_sizes` / runtime-sized arrays. + /// + /// [`MTLBuffer`]: objc2_metal::MTLBuffer + StorageBindingArray { + ptr: NonNull<ProtocolObject<dyn MTLBuffer>>, + array_element_sizes: Vec<(u32, wgt::BufferSize)>, + binding_location: u32, + }, + AccelerationStructure(NonNull<ProtocolObject<dyn MTLAccelerationStructure>>), +} + +#[derive(Debug)] +struct UseResourceInfo { + uses: MTLResourceUsage, + stages: MTLRenderStages, + visible_in_compute: bool, +} + +impl Default for UseResourceInfo { + fn default() -> Self { + Self { + uses: MTLResourceUsage::empty(), + stages: MTLRenderStages::empty(), + visible_in_compute: false, + } + } +} + +#[derive(Debug, Default)] +pub struct BindGroup { + counters: MultiStageResourceCounters, + buffers: Vec<BufferLikeResource>, + samplers: Vec<NonNull<ProtocolObject<dyn MTLSamplerState>>>, + textures: Vec<NonNull<ProtocolObject<dyn MTLTexture>>>, + + argument_buffers: Vec<Retained<ProtocolObject<dyn MTLBuffer>>>, + resources_to_use: HashMap<NonNull<ProtocolObject<dyn MTLResource>>, UseResourceInfo>, +} + +impl crate::DynBindGroup for BindGroup {} + +unsafe impl Send for BindGroup {} +unsafe impl Sync for BindGroup {} + +#[derive(Debug)] +pub enum ShaderModuleSource { + Naga(crate::NagaShader), + Passthrough(PassthroughShader), +} + +#[derive(Debug)] +pub struct PassthroughShader { + pub library: Retained<ProtocolObject<dyn MTLLibrary>>, + pub num_workgroups: HashMap<String, (u32, u32, u32)>, +} + +#[cfg(send_sync)] +static_assertions::assert_impl_all!(PassthroughShader: Send, Sync); + +#[derive(Debug)] +pub struct ShaderModule { + source: ShaderModuleSource, + runtime_checks: wgt::ShaderRuntimeChecks, +} + +impl crate::DynShaderModule for ShaderModule {} + +#[derive(Debug)] +struct PipelineStageInfo { + #[allow(dead_code)] + library: Option<Retained<ProtocolObject<dyn MTLLibrary>>>, + immediates: Option<ImmediateDataInfo>, + + /// The buffer argument table index at which we pass runtime-sized arrays' buffer sizes. + /// + /// See `device::CompiledShader::sized_bindings` for more details. + sizes_slot: Option<naga::back::msl::Slot>, + + /// Bindings of all WGSL `storage` globals that contain runtime-sized arrays. + /// + /// See `device::CompiledShader::sized_bindings` for more details. + sized_bindings: Vec<(naga::ResourceBinding, u32)>, + + /// Info on all bound vertex buffers. + vertex_buffer_mappings: Vec<naga::back::msl::VertexBufferMapping>, + + /// The workgroup size for compute, task or mesh stages + raw_wg_size: MTLSize, + + /// The workgroup memory sizes for compute task or mesh stages + work_group_memory_sizes: Vec<u32>, +} + +// TODO(madsmtm): Derive this when a release with +// https://github.com/madsmtm/objc2/issues/804 is available (likely 0.4). +impl Default for PipelineStageInfo { + fn default() -> Self { + Self { + library: Default::default(), + immediates: Default::default(), + sizes_slot: Default::default(), + sized_bindings: Default::default(), + vertex_buffer_mappings: Default::default(), + raw_wg_size: MTLSize { + width: 0, + height: 0, + depth: 0, + }, + work_group_memory_sizes: Default::default(), + } + } +} + +impl PipelineStageInfo { + fn clear(&mut self) { + self.immediates = None; + self.sizes_slot = None; + self.sized_bindings.clear(); + self.vertex_buffer_mappings.clear(); + self.library = None; + self.work_group_memory_sizes.clear(); + self.raw_wg_size = MTLSize { + width: 0, + height: 0, + depth: 0, + }; + } + + fn assign_from(&mut self, other: &Self) { + self.immediates = other.immediates; + self.sizes_slot = other.sizes_slot; + self.sized_bindings.clear(); + self.sized_bindings.extend_from_slice(&other.sized_bindings); + self.vertex_buffer_mappings.clear(); + self.vertex_buffer_mappings + .extend_from_slice(&other.vertex_buffer_mappings); + self.library = Some(other.library.as_ref().unwrap().clone()); + self.raw_wg_size = other.raw_wg_size; + self.work_group_memory_sizes.clear(); + self.work_group_memory_sizes + .extend_from_slice(&other.work_group_memory_sizes); + } +} + +#[derive(Debug)] +pub struct RenderPipeline { + raw: Retained<ProtocolObject<dyn MTLRenderPipelineState>>, + vs_info: Option<PipelineStageInfo>, + fs_info: Option<PipelineStageInfo>, + ts_info: Option<PipelineStageInfo>, + ms_info: Option<PipelineStageInfo>, + raw_primitive_type: MTLPrimitiveType, + raw_triangle_fill_mode: MTLTriangleFillMode, + raw_front_winding: MTLWinding, + raw_cull_mode: MTLCullMode, + raw_depth_clip_mode: Option<MTLDepthClipMode>, + depth_stencil: Option<( + Retained<ProtocolObject<dyn MTLDepthStencilState>>, + wgt::DepthBiasState, + )>, +} + +#[cfg(send_sync)] +static_assertions::assert_impl_all!(RenderPipeline: Send, Sync); + +impl crate::DynRenderPipeline for RenderPipeline {} + +#[derive(Debug)] +pub struct ComputePipeline { + raw: Retained<ProtocolObject<dyn MTLComputePipelineState>>, + cs_info: PipelineStageInfo, +} + +#[cfg(send_sync)] +static_assertions::assert_impl_all!(ComputePipeline: Send, Sync); + +impl crate::DynComputePipeline for ComputePipeline {} + +#[derive(Debug)] +pub struct RayTracingPipeline {} + +impl crate::DynRayTracingPipeline for RayTracingPipeline {} + +#[derive(Debug, Clone)] +pub struct QuerySet { + raw_buffer: Retained<ProtocolObject<dyn MTLBuffer>>, + //Metal has a custom buffer for counters. + counter_sample_buffer: Option<Retained<ProtocolObject<dyn MTLCounterSampleBuffer>>>, + ty: wgt::QueryType, +} + +impl crate::DynQuerySet for QuerySet {} + +unsafe impl Send for QuerySet {} +unsafe impl Sync for QuerySet {} + +#[derive(Debug)] +pub struct Fence { + sync: Arc<(Mutex<crate::FenceValue>, Condvar)>, + /// The pending fence values have to be ascending. + pending_command_buffers: RwLock<Vec<PendingCommandBuffer>>, + shared_event: Option<Retained<ProtocolObject<dyn MTLSharedEvent>>>, +} + +type PendingCommandBuffer = ( + crate::FenceValue, + Retained<ProtocolObject<dyn MTLCommandBuffer>>, +); + +impl crate::DynFence for Fence {} + +unsafe impl Send for Fence {} +unsafe impl Sync for Fence {} + +impl Fence { + fn get_latest(&self) -> crate::FenceValue { + let mut max_value = *self.sync.0.lock(); + let pending_command_buffers = self.pending_command_buffers.read(); + for &(value, ref cmd_buf) in pending_command_buffers.iter() { + match cmd_buf.status() { + MTLCommandBufferStatus::Completed | MTLCommandBufferStatus::Error => { + max_value = value; + } + _ => {} + } + } + max_value + } + + fn maintain(&self) { + let latest = self.get_latest(); + self.pending_command_buffers + .write() + .retain(|&(value, _)| value > latest); + } + + pub fn raw_shared_event(&self) -> Option<&ProtocolObject<dyn MTLSharedEvent>> { + self.shared_event.as_deref() + } +} + +struct IndexState { + buffer_ptr: NonNull<ProtocolObject<dyn MTLBuffer>>, + offset: wgt::BufferAddress, + stride: wgt::BufferAddress, + raw_type: MTLIndexType, +} + +#[derive(Default)] +struct Temp { + binding_sizes: Vec<u32>, +} + +// Any state in this struct that may be dirty after an abandoned encoding must +// be reset in `discard_encoding` for possible encoder reuse. +struct CommandState { + blit: Option<Retained<ProtocolObject<dyn MTLBlitCommandEncoder>>>, + acceleration_structure_builder: + Option<Retained<ProtocolObject<dyn MTLAccelerationStructureCommandEncoder>>>, + render: Option<Retained<ProtocolObject<dyn MTLRenderCommandEncoder>>>, + compute: Option<Retained<ProtocolObject<dyn MTLComputeCommandEncoder>>>, + raw_primitive_type: MTLPrimitiveType, + index: Option<IndexState>, + stage_infos: MultiStageData<PipelineStageInfo>, + + /// Sizes of currently bound [`wgt::BufferBindingType::Storage`] buffers. + /// + /// Specifically: + /// + /// - The keys are [`ResourceBinding`] values (that is, the WGSL `@group` + /// and `@binding` attributes) for `var<storage>` global variables in the + /// current module that contain runtime-sized arrays. + /// + /// - The values are the actual sizes of the buffers currently bound to + /// provide those globals' contents, which are needed to implement bounds + /// checks and the WGSL `arrayLength` function. + /// + /// For each stage `S` in `stage_infos`, we consult this to find the sizes + /// of the buffers listed in `stage_infos.S.sized_bindings`, which we must + /// pass to the entry point. + /// + /// See `device::CompiledShader::sized_bindings` for more details. + /// + /// [`ResourceBinding`]: naga::ResourceBinding + storage_buffer_length_map: FastHashMap<(naga::ResourceBinding, u32), wgt::BufferSize>, + + vertex_buffer_size_map: FastHashMap<u32, wgt::BufferSize>, + + immediates: Vec<u32>, + + /// Timer query that should be executed when the next pass starts. + pending_timer_queries: Vec<(QuerySet, u32)>, +} + +// Any state in this struct that may be dirty after an abandoned encoding must +// be reset in `discard_encoding` for possible encoder reuse. +pub struct CommandEncoder { + shared: Arc<AdapterShared>, + queue_shared: Arc<QueueShared>, + raw_cmd_buf: Option<Retained<ProtocolObject<dyn MTLCommandBuffer>>>, + state: CommandState, + temp: Temp, + counters: Arc<wgt::HalCounters>, +} + +impl fmt::Debug for CommandEncoder { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("CommandEncoder") + .field("raw_cmd_buf", &self.raw_cmd_buf) + .finish() + } +} + +unsafe impl Send for CommandEncoder {} +unsafe impl Sync for CommandEncoder {} + +#[derive(Debug)] +pub struct CommandBuffer { + raw: Retained<ProtocolObject<dyn MTLCommandBuffer>>, + queue_shared: Arc<QueueShared>, +} + +impl crate::DynCommandBuffer for CommandBuffer {} + +unsafe impl Send for CommandBuffer {} +unsafe impl Sync for CommandBuffer {} + +#[derive(Debug)] +pub struct PipelineCache; + +impl crate::DynPipelineCache for PipelineCache {} + +#[derive(Debug)] +pub struct AccelerationStructure { + raw: Retained<ProtocolObject<dyn MTLAccelerationStructure>>, +} + +impl AccelerationStructure { + fn as_raw(&self) -> NonNull<ProtocolObject<dyn MTLAccelerationStructure>> { + unsafe { NonNull::new_unchecked(Retained::as_ptr(&self.raw) as *mut _) } + } +} + +impl crate::DynAccelerationStructure for AccelerationStructure {} +unsafe impl Send for AccelerationStructure {} +unsafe impl Sync for AccelerationStructure {} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum OsType { + Macos, + Ios, + Tvos, + VisionOs, +} diff --git a/third_party/wgpu-hal/src/metal/surface.rs b/third_party/wgpu-hal/src/metal/surface.rs new file mode 100644 index 000000000..f8028a4f3 --- /dev/null +++ b/third_party/wgpu-hal/src/metal/surface.rs @@ -0,0 +1,409 @@ +use std::sync::LazyLock; + +use alloc::borrow::ToOwned as _; + +use objc2::{ + available, + rc::{autoreleasepool, Retained}, + runtime::ProtocolObject, + ClassType, Message, +}; +use objc2_core_foundation::{CFString, CGSize}; +use objc2_core_graphics::CGColorSpace; +use objc2_foundation::NSObjectProtocol; +use objc2_metal::MTLTextureType; +use objc2_quartz_core::{CAMetalDrawable, CAMetalLayer}; +use parking_lot::{Mutex, RwLock}; + +use super::OsFeatures; + +/// Walks up from `start` to the `NSWindow` hosting this layer: the first ancestor +/// layer with a delegate is the backing `NSView`, and we return its `window`. +/// `None` if no ancestor has a delegate. +#[cfg(target_os = "macos")] +fn hosting_window( + start: Retained<objc2_quartz_core::CALayer>, +) -> Option<Retained<objc2::runtime::NSObject>> { + let mut current = Some(start); + while let Some(layer) = current { + if let Some(delegate) = layer.delegate() { + return unsafe { objc2::msg_send![&*delegate, window] }; + } + current = layer.superlayer(); + } + None +} + +impl super::Surface { + pub fn new(layer: Retained<CAMetalLayer>) -> Self { + Self { + render_layer: Mutex::new(layer), + swapchain_format: RwLock::new(None), + extent: RwLock::new(wgt::Extent3d::default()), + } + } + + pub fn from_layer(layer: &CAMetalLayer) -> Self { + assert!(layer.isKindOfClass(CAMetalLayer::class())); + Self::new(layer.retain()) + } + + pub fn render_layer(&self) -> &Mutex<Retained<CAMetalLayer>> { + &self.render_layer + } + + /// Returns the EDR headroom of the screen hosting this surface, as a + /// [`wgt::DisplayHdrInfo`]. + /// + /// macOS only. Returns `None` on other Apple platforms (iOS, tvOS, visionOS), + /// when the hosting screen can't be resolved (e.g. an off-screen window), or + /// when called off the main thread: `NSScreen` and `NSWindow` are + /// main-thread-only, and off-thread access is undefined behavior. + /// + /// Only `headroom` and the coarse `high_dynamic_range` bit are filled: Apple + /// exposes a relative EDR multiplier (`1.0` == SDR white), no absolute nits, + /// and no HDR-mode flag. `high_dynamic_range` tracks the live `current` + /// multiplier (`> 1.0`), so it reports HDR active, not merely capable. + pub(super) fn display_hdr_info(&self) -> Option<wgt::DisplayHdrInfo> { + #[cfg(target_os = "macos")] + { + use objc2::rc::Retained; + use objc2::runtime::NSObject; + + // Bail before any message send if we are not on the main thread. + // `MainThreadMarker::new()` is `None` off the main thread and does no + // Objective-C work, so it is a cheap, safe gate. + if objc2::MainThreadMarker::new().is_none() { + // Of the paths that yield `None`, this is the only one a caller + // can fix (the others — off-screen window, non-macOS — are + // environmental), so leave a breadcrumb instead of failing + // silently. Warn once per process so a caller that polls this + // per frame is not spammed. + static WARN_ONCE: std::sync::Once = std::sync::Once::new(); + WARN_ONCE.call_once(|| { + log::warn!( + "Surface::display_hdr_info() was called from thread {:?} \ + and will return None. On the Metal backend, it must be \ + called from the main thread to succeed.", + std::thread::current().id() + ); + }); + return None; + } + + // Take an owned reference to the layer and drop the lock before + // accessing the window and screen, so a main-thread AppKit callback + // can never deadlock against this lock. + let render_layer = { + let guard = self.render_layer.lock(); + guard.clone() + }; + + // Resolve the hosting `NSScreen` from the hosting window. `NSWindow.screen` + // is nil when the window is off-screen, so this can legitimately yield `None`. + let screen: Retained<NSObject> = autoreleasepool(|_| { + hosting_window(Retained::into_super(render_layer)) + .and_then(|window| unsafe { objc2::msg_send![&*window, screen] }) + })?; + + // AppKit documents these EDR properties as finite multipliers + // (`1.0` == SDR white), but guard against a non-finite read anyway so + // the advisory values stay finite and the coarse `high_dynamic_range` + // bit reports unknown (`None`) rather than a false `Some(false)`, + // mirroring the `is_finite` discipline in + // [`wgt::DisplayHdrInfo::tone_map_headroom`]. The EDR properties return + // `CGFloat` (`f64` on 64-bit macOS). + let finite = |v: f64| v.is_finite().then_some(v as f32); + + // `maximumExtendedDynamicRangeColorComponentValue` is macOS 10.11+, so + // it is safe at our 10.13 minimum. + let current: f64 = unsafe { + objc2::msg_send![&*screen, maximumExtendedDynamicRangeColorComponentValue] + }; + + // Apple exposes no discrete HDR-mode flag, so derive the coarse + // dynamic-range bit from the live `current` EDR multiplier (`> 1.0`), + // not `potential` (which is `> 1.0` on nearly every Apple display and + // would conflate "capable" with "active"). + let high_dynamic_range = current.is_finite().then_some(current > 1.0); + + // `maximumPotential...` and `maximumReference...` are macOS 10.15+, + // below which sending them would raise an unrecognized-selector + // exception, so only message them where available; otherwise leave + // them `None`. + let (potential, reference) = if available!(macos = 10.15) { + let potential: f64 = unsafe { + objc2::msg_send![ + &*screen, + maximumPotentialExtendedDynamicRangeColorComponentValue + ] + }; + let reference: f64 = unsafe { + objc2::msg_send![ + &*screen, + maximumReferenceExtendedDynamicRangeColorComponentValue + ] + }; + // AppKit reports `0.0` when there is no reference value; treat that + // (and any non-finite read) as "unknown" rather than a real `0.0`. + (finite(potential), finite(reference).filter(|&v| v > 0.0)) + } else { + (None, None) + }; + let headroom = wgt::DisplayHeadroom { + current: finite(current), + potential, + reference, + }; + + // `NSScreen.colorSpace` returns generic names on HDR panels + // post-Monterey, so deriving a gamut bucket from it would lie; leave + // `gamut` as `None` on macOS. + let coarse = wgt::DisplayCoarseRange { + high_dynamic_range, + gamut: None, + }; + + // Apple exposes no absolute nits, CIE-xy primaries, or panel bit + // depth, so `luminance` / `chromaticity` / `bits_per_color` stay `None`. + let info = wgt::DisplayHdrInfo { + luminance: None, + headroom: Some(headroom), + chromaticity: None, + coarse: Some(coarse), + bits_per_color: None, + }; + Some(info) + } + #[cfg(not(target_os = "macos"))] + { + // TODO: iOS/tvOS/visionOS could report EDR headroom via + // `UIScreen.currentEDRHeadroom`. + None + } + } + + /// Gets the current dimensions of the `Surface`. + /// + /// This function is safe to call off of the main thread. However, note that + /// `bounds` and `contentsScale` may be modified by the main thread while + /// this function is running, possibly resulting in the two values being out + /// of sync. This is sound, as these properties are accessed atomically. + /// See: <https://github.com/gfx-rs/wgpu/pull/7692> + pub(super) fn dimensions(&self) -> wgt::Extent3d { + let (size, scale) = { + let render_layer = self.render_layer.lock(); + let bounds = render_layer.bounds(); + let contents_scale = render_layer.contentsScale(); + (bounds.size, contents_scale) + }; + + wgt::Extent3d { + width: (size.width * scale) as u32, + height: (size.height * scale) as u32, + depth_or_array_layers: 1, + } + } +} + +// objc2 strings are not, in general, thread-safe, but these should be constants. +unsafe impl Send for ColorSpaces {} +unsafe impl Sync for ColorSpaces {} + +struct ColorSpaces { + extended_display_p3: &'static CFString, + itur_bt2100_pq: &'static CFString, + itur_bt2100_hlg: &'static CFString, +} + +static COLOR_SPACES: LazyLock<Result<ColorSpaces, crate::SurfaceError>> = LazyLock::new(|| { + // Stable Rust doesn't support weak linkage, so objc2 doesn't + // offer it. To avoid link errors on old OS versions, resolve + // these dynamically. + let lib = unsafe { + libloading::Library::new("/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics") + .map_err(|_| crate::SurfaceError::Other("error loading CoreGraphics"))? + }; + fn lookup( + lib: &libloading::Library, + name: &[u8], + ) -> Result<&'static CFString, crate::SurfaceError> { + let sym = unsafe { lib.get(name) } + .map_err(|_| crate::SurfaceError::Other("error resolving symbol in CoreGraphics"))?; + Ok(*sym) + } + let extended_display_p3 = lookup(&lib, b"kCGColorSpaceExtendedDisplayP3\0")?; + let itur_bt2100_pq = lookup(&lib, b"kCGColorSpaceITUR_2100_PQ\0")?; + let itur_bt2100_hlg = lookup(&lib, b"kCGColorSpaceITUR_2100_HLG\0")?; + Ok(ColorSpaces { + extended_display_p3, + itur_bt2100_pq, + itur_bt2100_hlg, + }) +}); + +impl crate::Surface for super::Surface { + type A = super::Api; + + unsafe fn configure( + &self, + device: &super::Device, + config: &crate::SurfaceConfiguration, + ) -> Result<(), crate::SurfaceError> { + log::debug!("build swapchain {config:?}"); + + let caps = &device.shared.private_texture_format_caps; + *self.swapchain_format.write() = Some(config.format); + *self.extent.write() = config.extent; + + let render_layer = self.render_layer.lock(); + let framebuffer_only = config.usage == wgt::TextureUses::COLOR_TARGET; + let display_sync = match config.present_mode { + wgt::PresentMode::Fifo => true, + wgt::PresentMode::Immediate => false, + m => unreachable!("Unsupported present mode: {m:?}"), + }; + // CGFloat is f64 on 64-bit, f32 on 32-bit (arm64_32/ILP32) + let drawable_size = CGSize::new(config.extent.width as _, config.extent.height as _); + + match config.composite_alpha_mode { + wgt::CompositeAlphaMode::Opaque => render_layer.setOpaque(true), + wgt::CompositeAlphaMode::PostMultiplied => render_layer.setOpaque(false), + _ => (), + } + + let device_raw = &device.shared.device; + render_layer.setDevice(Some(device_raw)); + render_layer.setPixelFormat(caps.map_format(config.format)); + render_layer.setFramebufferOnly(framebuffer_only); + // Opt into Metal EDR for the HDR color spaces (more display power, memory, + // and bandwidth). The HDR spaces are exactly those `is_hdr()` classifies. + let wants_edr = config.color_space.is_hdr(); + if wants_edr != render_layer.wantsExtendedDynamicRangeContent() { + render_layer.setWantsExtendedDynamicRangeContent(wants_edr); + } + + let colorspace_name: Option<&'static CFString> = match config.color_space { + wgt::SurfaceColorSpace::Auto => { + unreachable!("wgpu-core resolves `Auto` before configuring the surface") + } + // Reset to the layer's default, which treats contents as sRGB. + wgt::SurfaceColorSpace::Srgb => None, + wgt::SurfaceColorSpace::ExtendedSrgbLinear => { + Some(unsafe { objc2_core_graphics::kCGColorSpaceExtendedLinearSRGB }) + } + wgt::SurfaceColorSpace::ExtendedSrgb => { + Some(unsafe { objc2_core_graphics::kCGColorSpaceExtendedSRGB }) + } + wgt::SurfaceColorSpace::ExtendedDisplayP3 => { + // TODO: This needs protection in `surface_capabilities` like the BT.2100 cases. + Some( + COLOR_SPACES + .as_ref() + .map_err(|e| e.clone())? + .extended_display_p3, + ) + } + wgt::SurfaceColorSpace::DisplayP3 => { + Some(unsafe { objc2_core_graphics::kCGColorSpaceDisplayP3 }) + } + wgt::SurfaceColorSpace::Bt2100Pq | wgt::SurfaceColorSpace::Bt2100Hlg => { + // The ITUR_2100 color space constants require macOS 11.0/iOS 14.0; + // `surface_capabilities` only reports BT.2100 PQ/HLG on those OS versions. + if !available!(macos = 11.0, ios = 14.0, tvos = 14.0, visionos = 1.0) { + unreachable!("BT.2100 PQ/HLG color spaces are only reported on macOS 11.0+/iOS 14.0+/tvOS 14.0+"); + } + Some(if config.color_space == wgt::SurfaceColorSpace::Bt2100Pq { + COLOR_SPACES.as_ref().map_err(|e| e.clone())?.itur_bt2100_pq + } else { + COLOR_SPACES + .as_ref() + .map_err(|e| e.clone())? + .itur_bt2100_hlg + }) + } + }; + let colorspace = colorspace_name.and_then(|name| CGColorSpace::with_name(Some(name))); + render_layer.setColorspace(colorspace.as_deref()); + + // this gets ignored on iOS for certain OS/device combinations (iphone5s iOS 10.3) + render_layer.setMaximumDrawableCount(config.maximum_frame_latency as usize + 1); + render_layer.setDrawableSize(drawable_size); + // https://developer.apple.com/documentation/quartzcore/cametallayer/allowsnextdrawabletimeout + if available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0) { + render_layer.setAllowsNextDrawableTimeout(false); + } + if OsFeatures::display_sync() { + render_layer.setDisplaySyncEnabled(display_sync); + } + + Ok(()) + } + + unsafe fn unconfigure(&self, _device: &super::Device) { + *self.swapchain_format.write() = None; + } + + unsafe fn acquire_texture( + &self, + _timeout: Option<core::time::Duration>, // TODO + _fence: &super::Fence, + ) -> Result<crate::AcquiredSurfaceTexture<super::Api>, crate::SurfaceError> { + let render_layer = self.render_layer.lock(); + + #[cfg(target_os = "macos")] + { + // Workaround for https://github.com/gfx-rs/wgpu/issues/8309 + // When the window is occluded on macOS, presented drawables get stuck waiting + // for vsync. Check the window's occlusion state and skip acquisition if + // the window is not visible - this avoids a 1-second hang in nextDrawable(). + use objc2::rc::Retained; + + // The CAMetalLayer is typically a sublayer; find the hosting window + // and skip acquisition while it is occluded. + if let Some(window) = hosting_window(Retained::into_super(render_layer.clone())) { + const NS_WINDOW_OCCLUSION_STATE_VISIBLE: usize = 1 << 1; + let occlusion_state: usize = unsafe { objc2::msg_send![&*window, occlusionState] }; + if occlusion_state & NS_WINDOW_OCCLUSION_STATE_VISIBLE == 0 { + return Err(crate::SurfaceError::Occluded); + } + } + } + + let (drawable, texture) = match autoreleasepool(|_| { + render_layer + .nextDrawable() + .map(|drawable| (drawable.to_owned(), drawable.texture().to_owned())) + }) { + Some(pair) => pair, + None => return Err(crate::SurfaceError::Timeout), + }; + + let swapchain_format = self.swapchain_format.read().unwrap(); + let extent = self.extent.read(); + let suf_texture = super::SurfaceTexture { + texture: super::Texture { + raw: texture, + format: swapchain_format, + raw_type: MTLTextureType::Type2D, + array_layers: 1, + mip_levels: 1, + copy_size: crate::CopyExtent { + width: extent.width, + height: extent.height, + depth: 1, + }, + _drop_guard: None, + }, + drawable: ProtocolObject::from_retained(drawable), + present_with_transaction: render_layer.presentsWithTransaction(), + }; + + Ok(crate::AcquiredSurfaceTexture { + texture: suf_texture, + suboptimal: false, + }) + } + + unsafe fn discard_texture(&self, _texture: super::SurfaceTexture) {} +} diff --git a/third_party/wgpu-hal/src/metal/time.rs b/third_party/wgpu-hal/src/metal/time.rs new file mode 100644 index 000000000..5c6bec10c --- /dev/null +++ b/third_party/wgpu-hal/src/metal/time.rs @@ -0,0 +1,38 @@ +//! Handling of global timestamps. + +#[repr(C)] +#[derive(Debug)] +struct MachTimebaseInfo { + numerator: u32, + denominator: u32, +} +extern "C" { + fn mach_timebase_info(out: *mut MachTimebaseInfo) -> u32; + fn mach_absolute_time() -> u64; +} + +/// A timer which uses mach_absolute_time to get its time. This is what the metal callbacks use. +#[derive(Debug)] +pub struct PresentationTimer { + scale: MachTimebaseInfo, +} +impl PresentationTimer { + /// Generates a new timer. + pub fn new() -> Self { + // Default to 1 / 1 in case the call to timebase_info fails. + let mut scale = MachTimebaseInfo { + numerator: 1, + denominator: 1, + }; + unsafe { mach_timebase_info(&mut scale) }; + + Self { scale } + } + + /// Gets the current time in nanoseconds. + pub fn get_timestamp_ns(&self) -> u128 { + let time = unsafe { mach_absolute_time() }; + + (time as u128 * self.scale.numerator as u128) / self.scale.denominator as u128 + } +} diff --git a/third_party/wgpu-hal/src/noop/buffer.rs b/third_party/wgpu-hal/src/noop/buffer.rs new file mode 100644 index 000000000..9b8ab17a1 --- /dev/null +++ b/third_party/wgpu-hal/src/noop/buffer.rs @@ -0,0 +1,88 @@ +use alloc::vec::Vec; +use core::{cell::UnsafeCell, ops::Range, ptr}; + +cfg_if::cfg_if! { + if #[cfg(supports_ptr_atomics)] { + use alloc::sync::Arc; + } else if #[cfg(feature = "portable-atomic")] { + use portable_atomic_util::Arc; + } +} + +#[derive(Clone, Debug)] +pub struct Buffer { + /// This data is potentially accessed mutably in arbitrary non-overlapping slices, + /// so we must store it in `UnsafeCell` to avoid making any too-strong no-aliasing claims. + storage: Arc<UnsafeCell<[u8]>>, + + /// Size of the allocation. + /// + /// This is redundant with `storage.get().len()`, but that method is not + /// available until our MSRV is 1.79 or greater. + size: usize, +} + +/// SAFETY: +/// This shared mutable data will not be accessed in a way which causes data races; +/// the obligation to do so is on the caller of the HAL API. +/// For safe code, `wgpu-core` validation manages appropriate access. +unsafe impl Send for Buffer {} +unsafe impl Sync for Buffer {} + +impl Buffer { + pub(super) fn new(desc: &crate::BufferDescriptor) -> Result<Self, crate::DeviceError> { + let &crate::BufferDescriptor { + label: _, + size, + usage: _, + memory_flags: _, + } = desc; + + let size = usize::try_from(size).map_err(|_| crate::DeviceError::OutOfMemory)?; + + let mut vector: Vec<u8> = Vec::new(); + vector + .try_reserve_exact(size) + .map_err(|_| crate::DeviceError::OutOfMemory)?; + vector.resize(size, 0); + let storage: Arc<[u8]> = Arc::from(vector); + debug_assert_eq!(storage.len(), size); + + // SAFETY: `UnsafeCell<[u8]>` and `[u8]` have the same layout. + // This is just adding a wrapper type without changing any layout, + // because there is not currently a safe language/`std` way to accomplish this. + let storage: Arc<UnsafeCell<[u8]>> = + unsafe { Arc::from_raw(Arc::into_raw(storage) as *mut UnsafeCell<[u8]>) }; + + Ok(Buffer { storage, size }) + } + + /// Returns a pointer to the memory owned by this buffer within the given `range`. + /// + /// This may be used to create any number of simultaneous pointers; + /// aliasing is only a concern when actually reading, writing, or converting the pointer + /// to a reference. + pub(super) fn get_slice_ptr(&self, range: crate::MemoryRange) -> *mut [u8] { + let base_ptr = self.storage.get(); + let range = range_to_usize(range, self.size); + + // We must obtain a slice pointer without ever creating a slice reference + // that could alias with another slice. + ptr::slice_from_raw_parts_mut( + // SAFETY: `range_to_usize` bounds checks this addition. + unsafe { base_ptr.cast::<u8>().add(range.start) }, + range.len(), + ) + } +} + +/// Convert a [`crate::MemoryRange`] to `Range<usize>` and bounds check it. +fn range_to_usize(range: crate::MemoryRange, upper_bound: usize) -> Range<usize> { + // Note: these assertions should be impossible to trigger from safe code. + // We're doing them anyway since this entire backend is for testing + // (except for when it is an unused placeholder) + let start = usize::try_from(range.start).expect("range too large"); + let end = usize::try_from(range.end).expect("range too large"); + assert!(start <= end && end <= upper_bound, "range out of bounds"); + start..end +} diff --git a/third_party/wgpu-hal/src/noop/command.rs b/third_party/wgpu-hal/src/noop/command.rs new file mode 100644 index 000000000..16cca58d4 --- /dev/null +++ b/third_party/wgpu-hal/src/noop/command.rs @@ -0,0 +1,356 @@ +use alloc::vec::Vec; +use core::mem; +use core::ops::Range; + +use super::{Api, Buffer, DeviceResult, Resource}; + +/// Command buffer type, which performs double duty as the command encoder type too. +#[derive(Debug)] +pub struct CommandBuffer { + commands: Vec<Command>, +} + +#[derive(Debug)] +enum Command { + ClearBuffer { + buffer: Buffer, + range: crate::MemoryRange, + }, + CopyBufferToBuffer { + src: Buffer, + dst: Buffer, + regions: Vec<crate::BufferCopy>, + }, +} + +impl CommandBuffer { + /// # Safety + /// + /// Must be called with appropriate synchronization for the resources affected by the command, + /// such as ensuring that buffers are not accessed by a command while aliasing references exist. + pub(crate) unsafe fn execute(&self) { + for command in &self.commands { + unsafe { command.execute() }; + } + } + + pub(crate) fn new() -> Self { + Self { + commands: Vec::new(), + } + } +} + +impl crate::CommandEncoder for CommandBuffer { + type A = Api; + + unsafe fn begin_encoding(&mut self, label: crate::Label) -> DeviceResult<()> { + assert!(self.commands.is_empty()); + Ok(()) + } + unsafe fn discard_encoding(&mut self) { + self.commands.clear(); + } + unsafe fn end_encoding(&mut self) -> DeviceResult<CommandBuffer> { + Ok(CommandBuffer { + commands: mem::take(&mut self.commands), + }) + } + unsafe fn reset_all<I>(&mut self, command_buffers: I) {} + + unsafe fn transition_buffers<'a, T>(&mut self, barriers: T) + where + T: Iterator<Item = crate::BufferBarrier<'a, Buffer>>, + { + } + + unsafe fn transition_textures<'a, T>(&mut self, barriers: T) + where + T: Iterator<Item = crate::TextureBarrier<'a, Resource>>, + { + } + + unsafe fn clear_buffer(&mut self, buffer: &Buffer, range: crate::MemoryRange) { + self.commands.push(Command::ClearBuffer { + buffer: buffer.clone(), + range, + }) + } + + unsafe fn copy_buffer_to_buffer<T>(&mut self, src: &Buffer, dst: &Buffer, regions: T) + where + T: Iterator<Item = crate::BufferCopy>, + { + self.commands.push(Command::CopyBufferToBuffer { + src: src.clone(), + dst: dst.clone(), + regions: regions.collect(), + }); + } + + #[cfg(webgl)] + unsafe fn copy_external_image_to_texture<T>( + &mut self, + src: &wgt::CopyExternalImageSourceInfo, + dst: &Resource, + dst_premultiplication: bool, + regions: T, + ) where + T: Iterator<Item = crate::TextureCopy>, + { + } + + unsafe fn copy_texture_to_texture<T>( + &mut self, + src: &Resource, + src_usage: wgt::TextureUses, + dst: &Resource, + regions: T, + ) { + // TODO: consider implementing this and other texture manipulation + } + + unsafe fn copy_buffer_to_texture<T>(&mut self, src: &Buffer, dst: &Resource, regions: T) { + // TODO: consider implementing this and other texture manipulation + } + + unsafe fn copy_texture_to_buffer<T>( + &mut self, + src: &Resource, + src_usage: wgt::TextureUses, + dst: &Buffer, + regions: T, + ) { + // TODO: consider implementing this and other texture manipulation + } + + unsafe fn begin_query(&mut self, set: &Resource, index: u32) {} + unsafe fn end_query(&mut self, set: &Resource, index: u32) {} + unsafe fn write_timestamp(&mut self, set: &Resource, index: u32) {} + unsafe fn read_acceleration_structure_compact_size( + &mut self, + acceleration_structure: &Resource, + buf: &Buffer, + ) { + } + unsafe fn reset_queries(&mut self, set: &Resource, range: Range<u32>) {} + unsafe fn copy_query_results( + &mut self, + set: &Resource, + range: Range<u32>, + buffer: &Buffer, + offset: wgt::BufferAddress, + stride: wgt::BufferSize, + ) { + } + + // render + + unsafe fn begin_render_pass( + &mut self, + desc: &crate::RenderPassDescriptor<Resource, Resource>, + ) -> DeviceResult<()> { + Ok(()) + } + unsafe fn end_render_pass(&mut self) {} + + unsafe fn set_bind_group( + &mut self, + layout: &Resource, + index: u32, + group: &Resource, + dynamic_offsets: &[wgt::DynamicOffset], + ) { + } + unsafe fn set_immediates(&mut self, layout: &Resource, offset_bytes: u32, data: &[u32]) {} + + unsafe fn insert_debug_marker(&mut self, label: &str) {} + unsafe fn begin_debug_marker(&mut self, group_label: &str) {} + unsafe fn end_debug_marker(&mut self) {} + + unsafe fn set_render_pipeline(&mut self, pipeline: &Resource) {} + + unsafe fn set_index_buffer<'a>( + &mut self, + binding: crate::BufferBinding<'a, Buffer>, + format: wgt::IndexFormat, + ) { + } + unsafe fn set_vertex_buffer<'a>( + &mut self, + index: u32, + binding: crate::BufferBinding<'a, Buffer>, + ) { + } + unsafe fn set_viewport(&mut self, rect: &crate::Rect<f32>, depth_range: Range<f32>) {} + unsafe fn set_scissor_rect(&mut self, rect: &crate::Rect<u32>) {} + unsafe fn set_stencil_reference(&mut self, value: u32) {} + unsafe fn set_blend_constants(&mut self, color: &[f32; 4]) {} + + unsafe fn draw( + &mut self, + first_vertex: u32, + vertex_count: u32, + first_instance: u32, + instance_count: u32, + ) { + } + unsafe fn draw_indexed( + &mut self, + first_index: u32, + index_count: u32, + base_vertex: i32, + first_instance: u32, + instance_count: u32, + ) { + } + unsafe fn draw_mesh_tasks( + &mut self, + group_count_x: u32, + group_count_y: u32, + group_count_z: u32, + ) { + } + unsafe fn draw_indirect( + &mut self, + buffer: &Buffer, + offset: wgt::BufferAddress, + draw_count: u32, + ) { + } + unsafe fn draw_indexed_indirect( + &mut self, + buffer: &Buffer, + offset: wgt::BufferAddress, + draw_count: u32, + ) { + } + unsafe fn draw_mesh_tasks_indirect( + &mut self, + buffer: &<Self::A as crate::Api>::Buffer, + offset: wgt::BufferAddress, + draw_count: u32, + ) { + } + unsafe fn draw_indirect_count( + &mut self, + buffer: &Buffer, + offset: wgt::BufferAddress, + count_buffer: &Buffer, + count_offset: wgt::BufferAddress, + max_count: u32, + ) { + } + unsafe fn draw_indexed_indirect_count( + &mut self, + buffer: &Buffer, + offset: wgt::BufferAddress, + count_buffer: &Buffer, + count_offset: wgt::BufferAddress, + max_count: u32, + ) { + } + unsafe fn draw_mesh_tasks_indirect_count( + &mut self, + buffer: &<Self::A as crate::Api>::Buffer, + offset: wgt::BufferAddress, + count_buffer: &<Self::A as crate::Api>::Buffer, + count_offset: wgt::BufferAddress, + max_count: u32, + ) { + } + + // compute + + unsafe fn begin_compute_pass(&mut self, desc: &crate::ComputePassDescriptor<Resource>) {} + unsafe fn end_compute_pass(&mut self) {} + + unsafe fn set_compute_pipeline(&mut self, pipeline: &Resource) {} + + unsafe fn dispatch_workgroups(&mut self, count: [u32; 3]) {} + unsafe fn dispatch_workgroups_indirect(&mut self, buffer: &Buffer, offset: wgt::BufferAddress) { + } + + unsafe fn build_acceleration_structures<'a, T>( + &mut self, + _descriptor_count: u32, + descriptors: T, + ) where + Api: 'a, + T: IntoIterator<Item = crate::BuildAccelerationStructureDescriptor<'a, Buffer, Resource>>, + { + } + + unsafe fn place_acceleration_structure_barrier( + &mut self, + _barriers: crate::AccelerationStructureBarrier, + ) { + } + + unsafe fn copy_acceleration_structure_to_acceleration_structure( + &mut self, + src: &Resource, + dst: &Resource, + copy: wgt::AccelerationStructureCopy, + ) { + } + + unsafe fn set_acceleration_structure_dependencies( + command_buffers: &[&CommandBuffer], + dependencies: &[&Resource], + ) { + } + + unsafe fn begin_ray_tracing_pass(&mut self, _desc: &crate::RayTracingPassDescriptor) {} + + unsafe fn end_ray_tracing_pass(&mut self) {} + + unsafe fn set_ray_tracing_pipeline( + &mut self, + _pipeline: &<Self::A as crate::Api>::RayTracingPipeline, + ) { + } + + unsafe fn trace_rays( + &mut self, + _count: [u32; 3], + _ray_generation_group_data: crate::PipelineGroupData<Buffer>, + _miss_group_data: crate::PipelineGroupData<Buffer>, + _intersection_group_data: crate::PipelineGroupData<Buffer>, + ) { + } +} + +impl Command { + /// # Safety + /// + /// Must be called with appropriate synchronization for the resources affected by the command, + /// such as ensuring that buffers are not accessed by a command while aliasing references exist. + unsafe fn execute(&self) { + match self { + Command::ClearBuffer { ref buffer, range } => { + // SAFETY: + // Caller is responsible for ensuring this does not alias. + let buffer_slice: &mut [u8] = unsafe { &mut *buffer.get_slice_ptr(range.clone()) }; + buffer_slice.fill(0); + } + + Command::CopyBufferToBuffer { src, dst, regions } => { + for &crate::BufferCopy { + src_offset, + dst_offset, + size, + } in regions + { + // SAFETY: + // Caller is responsible for ensuring this does not alias. + let src_region: &[u8] = + unsafe { &*src.get_slice_ptr(src_offset..src_offset + size.get()) }; + let dst_region: &mut [u8] = + unsafe { &mut *dst.get_slice_ptr(dst_offset..dst_offset + size.get()) }; + dst_region.copy_from_slice(src_region); + } + } + } + } +} diff --git a/third_party/wgpu-hal/src/noop/mod.rs b/third_party/wgpu-hal/src/noop/mod.rs new file mode 100644 index 000000000..b2e397d39 --- /dev/null +++ b/third_party/wgpu-hal/src/noop/mod.rs @@ -0,0 +1,487 @@ +#![allow(unused_variables)] + +use alloc::{string::String, sync::Arc, vec, vec::Vec}; +use core::{ptr, sync::atomic::Ordering, time::Duration}; + +#[cfg(supports_64bit_atomics)] +use core::sync::atomic::AtomicU64; +#[cfg(not(supports_64bit_atomics))] +use portable_atomic::AtomicU64; + +use crate::TlasInstance; + +mod buffer; +pub use buffer::Buffer; +mod command; +pub use command::CommandBuffer; + +#[derive(Clone, Debug)] +pub struct Api; + +#[derive(Debug)] +pub struct Context { + options: Arc<wgt::NoopBackendOptions>, +} +#[derive(Debug)] +pub struct Encoder; +#[derive(Debug)] +pub struct Resource; + +#[derive(Debug)] +pub struct Fence { + value: AtomicU64, +} + +type DeviceResult<T> = Result<T, crate::DeviceError>; + +impl crate::Api for Api { + const VARIANT: wgt::Backend = wgt::Backend::Noop; + + type Instance = Context; + type Surface = Context; + type Adapter = Context; + type Device = Context; + + type Queue = Context; + type CommandEncoder = CommandBuffer; + type CommandBuffer = CommandBuffer; + + type Buffer = Buffer; + type Texture = Resource; + type SurfaceTexture = Resource; + type TextureView = Resource; + type Sampler = Resource; + type QuerySet = Resource; + type Fence = Fence; + type AccelerationStructure = Resource; + type PipelineCache = Resource; + + type BindGroupLayout = Resource; + type BindGroup = Resource; + type PipelineLayout = Resource; + type ShaderModule = Resource; + type RenderPipeline = Resource; + type RayTracingPipeline = Resource; + type ComputePipeline = Resource; +} + +crate::impl_dyn_resource!(Buffer, CommandBuffer, Context, Fence, Resource); + +impl crate::DynAccelerationStructure for Resource {} +impl crate::DynBindGroup for Resource {} +impl crate::DynBindGroupLayout for Resource {} +impl crate::DynBuffer for Buffer {} +impl crate::DynCommandBuffer for CommandBuffer {} +impl crate::DynComputePipeline for Resource {} +impl crate::DynFence for Fence {} +impl crate::DynPipelineCache for Resource {} +impl crate::DynPipelineLayout for Resource {} +impl crate::DynQuerySet for Resource {} +impl crate::DynRenderPipeline for Resource {} +impl crate::DynRayTracingPipeline for Resource {} +impl crate::DynSampler for Resource {} +impl crate::DynShaderModule for Resource {} +impl crate::DynSurfaceTexture for Resource {} +impl crate::DynTexture for Resource {} +impl crate::DynTextureView for Resource {} + +impl core::borrow::Borrow<dyn crate::DynTexture> for Resource { + fn borrow(&self) -> &dyn crate::DynTexture { + self + } +} + +impl crate::Instance for Context { + type A = Api; + + unsafe fn init(desc: &crate::InstanceDescriptor<'_>) -> Result<Self, crate::InstanceError> { + let options = Arc::new(desc.backend_options.noop.clone()); + if options.enable { + Ok(Context { options }) + } else { + Err(crate::InstanceError::new(String::from( + "noop backend disabled because NoopBackendOptions::enable is false", + ))) + } + } + unsafe fn create_surface( + &self, + _display_handle: raw_window_handle::RawDisplayHandle, + _window_handle: raw_window_handle::RawWindowHandle, + ) -> Result<Context, crate::InstanceError> { + Ok(Context { + options: Arc::clone(&self.options), + }) + } + unsafe fn enumerate_adapters( + &self, + _surface_hint: Option<&Context>, + ) -> Vec<crate::ExposedAdapter<Api>> { + let device_type = self.options.device_type.unwrap_or(wgt::DeviceType::Other); + let subgroup_min_size = self + .options + .subgroup_min_size + .unwrap_or(wgt::MINIMUM_SUBGROUP_MIN_SIZE); + let subgroup_max_size = self + .options + .subgroup_max_size + .unwrap_or(wgt::MAXIMUM_SUBGROUP_MAX_SIZE); + let features = self.options.features.unwrap_or(wgt::Features::all()); + let limits = self + .options + .limits + .clone() + .unwrap_or(CAPABILITIES.limits.clone()); + + let info = wgt::AdapterInfo { + subgroup_min_size, + subgroup_max_size, + ..adapter_info() + }; + + let capabilities = crate::Capabilities { + limits, + ..CAPABILITIES + }; + + vec![crate::ExposedAdapter { + adapter: Context { + options: Arc::clone(&self.options), + }, + info, + features, + capabilities, + }] + } +} + +/// Returns the adapter info for the noop backend. +/// +/// This is used in the test harness to construct info about +/// the noop backend adapter without actually initializing wgpu. +pub fn adapter_info() -> wgt::AdapterInfo { + wgt::AdapterInfo { + name: String::from("noop wgpu backend"), + driver: String::from("wgpu"), + ..wgt::AdapterInfo::new(wgt::DeviceType::Cpu, wgt::Backend::Noop) + } +} + +/// The capabilities of the noop backend. +/// +/// This is used in the test harness to construct capabilities +/// of the noop backend without actually initializing wgpu. +pub const CAPABILITIES: crate::Capabilities = { + crate::Capabilities { + limits: wgt::Limits::unlimited(), + alignments: crate::Alignments { + // All maximally permissive + buffer_copy_offset: wgt::BufferSize::MIN, + buffer_copy_pitch: wgt::BufferSize::MIN, + uniform_bounds_check_alignment: wgt::BufferSize::MIN, + raw_tlas_instance_size: 0, + ray_tracing_scratch_buffer_alignment: 1, + ray_tracing_pipeline_group_data_size: 1, + ray_tracing_pipeline_group_data_alignment: 1, + ray_tracing_pipeline_data_offset_alignment: 1, + }, + downlevel: wgt::DownlevelCapabilities { + flags: wgt::DownlevelFlags::all(), + limits: wgt::DownlevelLimits {}, + shader_model: wgt::ShaderModel::Sm5, + }, + cooperative_matrix_properties: Vec::new(), + } +}; + +impl crate::Surface for Context { + type A = Api; + + unsafe fn configure( + &self, + device: &Context, + config: &crate::SurfaceConfiguration, + ) -> Result<(), crate::SurfaceError> { + Ok(()) + } + + unsafe fn unconfigure(&self, device: &Context) {} + + unsafe fn acquire_texture( + &self, + _timeout: Option<Duration>, + _fence: &Fence, + ) -> Result<crate::AcquiredSurfaceTexture<Api>, crate::SurfaceError> { + Err(crate::SurfaceError::Timeout) + } + unsafe fn discard_texture(&self, texture: Resource) {} +} + +impl crate::Adapter for Context { + type A = Api; + + unsafe fn open( + &self, + features: wgt::Features, + _limits: &wgt::Limits, + _memory_hints: &wgt::MemoryHints, + ) -> DeviceResult<crate::OpenDevice<Api>> { + Ok(crate::OpenDevice { + device: Context { + options: Arc::clone(&self.options), + }, + queue: Context { + options: Arc::clone(&self.options), + }, + }) + } + unsafe fn texture_format_capabilities( + &self, + format: wgt::TextureFormat, + ) -> crate::TextureFormatCapabilities { + crate::TextureFormatCapabilities::empty() + } + + unsafe fn surface_capabilities(&self, surface: &Context) -> Option<crate::SurfaceCapabilities> { + None + } + + unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp { + wgt::PresentationTimestamp::INVALID_TIMESTAMP + } + + fn get_ordered_buffer_usages(&self) -> wgt::BufferUses { + wgt::BufferUses::INCLUSIVE | wgt::BufferUses::MAP_WRITE + } + + fn get_ordered_texture_usages(&self) -> wgt::TextureUses { + wgt::TextureUses::INCLUSIVE + | wgt::TextureUses::COLOR_TARGET + | wgt::TextureUses::DEPTH_STENCIL_WRITE + } +} + +impl crate::Queue for Context { + type A = Api; + + unsafe fn submit( + &self, + command_buffers: &[&CommandBuffer], + surface_textures: &[&Resource], + (fence, fence_value): (&Fence, crate::FenceValue), + ) -> DeviceResult<()> { + // All commands are executed synchronously. + for cb in command_buffers { + // SAFETY: Caller is responsible for ensuring synchronization between commands and + // other mutations. + unsafe { + cb.execute(); + } + } + fence.value.store(fence_value, Ordering::Release); + Ok(()) + } + unsafe fn present( + &self, + surface: &Context, + texture: Resource, + ) -> Result<(), crate::SurfaceError> { + Ok(()) + } + + unsafe fn get_timestamp_period(&self) -> f32 { + 1.0 + } + + unsafe fn wait_for_idle(&self) -> Result<(), crate::DeviceError> { + Ok(()) + } +} + +impl crate::Device for Context { + type A = Api; + + unsafe fn create_buffer(&self, desc: &crate::BufferDescriptor) -> DeviceResult<Buffer> { + Buffer::new(desc) + } + + unsafe fn destroy_buffer(&self, buffer: Buffer) {} + unsafe fn add_raw_buffer(&self, _buffer: &Buffer) {} + + unsafe fn map_buffer( + &self, + buffer: &Buffer, + range: crate::MemoryRange, + ) -> DeviceResult<crate::BufferMapping> { + // Safety: the `wgpu-core` validation layer will prevent any user-accessible aliasing + // mappings from being created, so we don’t need to perform any checks here, except for + // bounds checks on the range which are built into `get_slice_ptr()`. + Ok(crate::BufferMapping { + ptr: ptr::NonNull::new(buffer.get_slice_ptr(range).cast::<u8>()).unwrap(), + is_coherent: true, + }) + } + unsafe fn unmap_buffer(&self, buffer: &Buffer) {} + unsafe fn flush_mapped_ranges<I>(&self, buffer: &Buffer, ranges: I) {} + unsafe fn invalidate_mapped_ranges<I>(&self, buffer: &Buffer, ranges: I) {} + + unsafe fn create_texture(&self, desc: &crate::TextureDescriptor) -> DeviceResult<Resource> { + Ok(Resource) + } + unsafe fn destroy_texture(&self, texture: Resource) {} + unsafe fn add_raw_texture(&self, _texture: &Resource) {} + + unsafe fn create_texture_view( + &self, + texture: &Resource, + desc: &crate::TextureViewDescriptor, + ) -> DeviceResult<Resource> { + Ok(Resource) + } + unsafe fn destroy_texture_view(&self, view: Resource) {} + unsafe fn create_sampler(&self, desc: &crate::SamplerDescriptor) -> DeviceResult<Resource> { + Ok(Resource) + } + unsafe fn destroy_sampler(&self, sampler: Resource) {} + + unsafe fn create_command_encoder( + &self, + desc: &crate::CommandEncoderDescriptor<Context>, + ) -> DeviceResult<CommandBuffer> { + Ok(CommandBuffer::new()) + } + + unsafe fn create_bind_group_layout( + &self, + desc: &crate::BindGroupLayoutDescriptor, + ) -> DeviceResult<Resource> { + Ok(Resource) + } + unsafe fn destroy_bind_group_layout(&self, bg_layout: Resource) {} + unsafe fn create_pipeline_layout( + &self, + desc: &crate::PipelineLayoutDescriptor<Resource>, + ) -> DeviceResult<Resource> { + Ok(Resource) + } + unsafe fn destroy_pipeline_layout(&self, pipeline_layout: Resource) {} + unsafe fn create_bind_group( + &self, + desc: &crate::BindGroupDescriptor<Resource, Buffer, Resource, Resource, Resource>, + ) -> DeviceResult<Resource> { + Ok(Resource) + } + unsafe fn destroy_bind_group(&self, group: Resource) {} + + unsafe fn create_shader_module( + &self, + desc: &crate::ShaderModuleDescriptor, + shader: crate::ShaderInput, + ) -> Result<Resource, crate::ShaderError> { + Ok(Resource) + } + unsafe fn destroy_shader_module(&self, module: Resource) {} + unsafe fn create_render_pipeline( + &self, + desc: &crate::RenderPipelineDescriptor<Resource, Resource, Resource>, + ) -> Result<Resource, crate::PipelineError> { + Ok(Resource) + } + unsafe fn destroy_render_pipeline(&self, pipeline: Resource) {} + unsafe fn create_compute_pipeline( + &self, + desc: &crate::ComputePipelineDescriptor<Resource, Resource, Resource>, + ) -> Result<Resource, crate::PipelineError> { + Ok(Resource) + } + unsafe fn destroy_compute_pipeline(&self, pipeline: Resource) {} + unsafe fn create_ray_tracing_pipeline( + &self, + desc: &crate::RayTracingPipelineDescriptor<Resource, Resource, Resource>, + ) -> Result<Resource, crate::PipelineError> { + Ok(Resource) + } + unsafe fn destroy_ray_tracing_pipeline(&self, pipeline: Resource) {} + unsafe fn get_raytracing_pipeline_group_data( + &self, + pipeline: &Resource, + groups: core::ops::Range<u32>, + ) -> Result<Vec<u8>, crate::DeviceError> { + Ok(vec![0; groups.count()]) + } + unsafe fn create_pipeline_cache( + &self, + desc: &crate::PipelineCacheDescriptor<'_>, + ) -> Result<Resource, crate::PipelineCacheError> { + Ok(Resource) + } + unsafe fn destroy_pipeline_cache(&self, cache: Resource) {} + + unsafe fn create_query_set( + &self, + desc: &wgt::QuerySetDescriptor<crate::Label>, + ) -> DeviceResult<Resource> { + Ok(Resource) + } + unsafe fn destroy_query_set(&self, set: Resource) {} + unsafe fn create_fence(&self) -> DeviceResult<Fence> { + Ok(Fence { + value: AtomicU64::new(0), + }) + } + unsafe fn destroy_fence(&self, fence: Fence) {} + unsafe fn get_fence_value(&self, fence: &Fence) -> DeviceResult<crate::FenceValue> { + Ok(fence.value.load(Ordering::Acquire)) + } + unsafe fn wait( + &self, + fence: &Fence, + value: crate::FenceValue, + timeout: Option<Duration>, + ) -> DeviceResult<bool> { + // The relevant commands must have already been submitted, and noop-backend commands are + // executed synchronously, so there is no waiting — either it is already done, + // or this method was called incorrectly. + assert!( + fence.value.load(Ordering::Acquire) >= value, + "submission must have already been done" + ); + Ok(true) + } + + unsafe fn start_graphics_debugger_capture(&self) -> bool { + false + } + unsafe fn stop_graphics_debugger_capture(&self) {} + unsafe fn create_acceleration_structure( + &self, + desc: &crate::AccelerationStructureDescriptor, + ) -> DeviceResult<Resource> { + Ok(Resource) + } + unsafe fn get_acceleration_structure_build_sizes<'a>( + &self, + _desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, Buffer>, + ) -> crate::AccelerationStructureBuildSizes { + Default::default() + } + unsafe fn get_acceleration_structure_device_address( + &self, + _acceleration_structure: &Resource, + ) -> wgt::BufferAddress { + Default::default() + } + unsafe fn destroy_acceleration_structure(&self, _acceleration_structure: Resource) {} + + fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec<u8> { + vec![] + } + + fn get_internal_counters(&self) -> wgt::HalCounters { + Default::default() + } + + fn check_if_oom(&self) -> DeviceResult<()> { + Ok(()) + } +} diff --git a/third_party/wgpu-hal/src/validation_canary.rs b/third_party/wgpu-hal/src/validation_canary.rs new file mode 100644 index 000000000..bd23db332 --- /dev/null +++ b/third_party/wgpu-hal/src/validation_canary.rs @@ -0,0 +1,40 @@ +use alloc::{string::String, vec::Vec}; + +use parking_lot::Mutex; + +/// Stores the text of any validation errors that have occurred since +/// the last call to `get_and_reset`. +/// +/// Each value is a validation error and a message associated with it, +/// or `None` if the error has no message from the api. +/// +/// This is used for internal wgpu testing only and _must not_ be used +/// as a way to check for errors. +/// +/// This works as a static because `cargo nextest` runs all of our +/// tests in separate processes, so each test gets its own canary. +/// +/// This prevents the issue of one validation error terminating the +/// entire process. +pub static VALIDATION_CANARY: ValidationCanary = ValidationCanary { + inner: Mutex::new(Vec::new()), +}; + +/// Flag for internal testing. +#[derive(Debug)] +pub struct ValidationCanary { + inner: Mutex<Vec<String>>, +} + +impl ValidationCanary { + #[allow(dead_code, reason = "in some configurations this function is dead")] + pub(crate) fn add(&self, msg: String) { + self.inner.lock().push(msg); + } + + /// Returns any API validation errors that have occurred in this process + /// since the last call to this function. + pub fn get_and_reset(&self) -> Vec<String> { + self.inner.lock().drain(..).collect() + } +} diff --git a/third_party/wgpu-hal/src/vulkan/adapter.rs b/third_party/wgpu-hal/src/vulkan/adapter.rs new file mode 100644 index 000000000..196daf4c5 --- /dev/null +++ b/third_party/wgpu-hal/src/vulkan/adapter.rs @@ -0,0 +1,3551 @@ +use alloc::{borrow::ToOwned as _, boxed::Box, collections::BTreeMap, sync::Arc, vec::Vec}; +use core::{ffi::CStr, marker::PhantomData}; + +use ash::{ext, google, khr, vk}; +use parking_lot::Mutex; + +use crate::{vulkan::semaphore_list::SemaphoreList, AllocationSizes}; + +use super::semaphore_list::SemaphoreListMode; + +fn depth_stencil_required_flags() -> vk::FormatFeatureFlags { + vk::FormatFeatureFlags::SAMPLED_IMAGE | vk::FormatFeatureFlags::DEPTH_STENCIL_ATTACHMENT +} + +const INDEXING_FEATURES: wgt::Features = wgt::Features::TEXTURE_BINDING_ARRAY + .union(wgt::Features::BUFFER_BINDING_ARRAY) + .union(wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY) + .union(wgt::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING) + .union(wgt::Features::STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING) + .union(wgt::Features::UNIFORM_BUFFER_BINDING_ARRAYS) + .union(wgt::Features::PARTIALLY_BOUND_BINDING_ARRAY); +#[expect(rustdoc::private_intra_doc_links)] +/// Features supported by a [`vk::PhysicalDevice`] and its extensions. +/// +/// This is used in two phases: +/// +/// - When enumerating adapters, this represents the features offered by the +/// adapter. [`Instance::expose_adapter`] calls `vkGetPhysicalDeviceFeatures2` +/// (or `vkGetPhysicalDeviceFeatures` if that is not available) to collect +/// this information about the `VkPhysicalDevice` represented by the +/// `wgpu_hal::ExposedAdapter`. +/// +/// - When opening a device, this represents the features we would like to +/// enable. At `wgpu_hal::Device` construction time, +/// [`PhysicalDeviceFeatures::from_extensions_and_requested_features`] +/// constructs an value of this type indicating which Vulkan features to +/// enable, based on the `wgpu_types::Features` requested. +/// +/// [`Instance::expose_adapter`]: super::Instance::expose_adapter +#[derive(Debug, Default)] +pub struct PhysicalDeviceFeatures { + /// Basic Vulkan 1.0 features. + core: vk::PhysicalDeviceFeatures, + + /// Features provided by `VK_EXT_descriptor_indexing`, promoted to Vulkan 1.2. + pub(super) descriptor_indexing: + Option<vk::PhysicalDeviceDescriptorIndexingFeaturesEXT<'static>>, + + /// Features provided by `VK_KHR_timeline_semaphore`, promoted to Vulkan 1.2 + timeline_semaphore: Option<vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR<'static>>, + + /// Features provided by `VK_EXT_image_robustness`, promoted to Vulkan 1.3 + image_robustness: Option<vk::PhysicalDeviceImageRobustnessFeaturesEXT<'static>>, + + /// Features provided by `VK_EXT_robustness2`. + robustness2: Option<vk::PhysicalDeviceRobustness2FeaturesEXT<'static>>, + + /// Features provided by `VK_KHR_multiview`, promoted to Vulkan 1.1. + multiview: Option<vk::PhysicalDeviceMultiviewFeaturesKHR<'static>>, + + /// Features provided by `VK_KHR_sampler_ycbcr_conversion`, promoted to Vulkan 1.1. + sampler_ycbcr_conversion: Option<vk::PhysicalDeviceSamplerYcbcrConversionFeatures<'static>>, + + /// Features provided by `VK_EXT_texture_compression_astc_hdr`, promoted to Vulkan 1.3. + astc_hdr: Option<vk::PhysicalDeviceTextureCompressionASTCHDRFeaturesEXT<'static>>, + + /// Features provided by `VK_KHR_shader_float16_int8`, promoted to Vulkan 1.2 + shader_float16_int8: Option<vk::PhysicalDeviceShaderFloat16Int8Features<'static>>, + + /// Features provided by `VK_KHR_16bit_storage`, promoted to Vulkan 1.1 + _16bit_storage: Option<vk::PhysicalDevice16BitStorageFeatures<'static>>, + + /// Features provided by `VK_KHR_acceleration_structure`. + acceleration_structure: Option<vk::PhysicalDeviceAccelerationStructureFeaturesKHR<'static>>, + + /// Features provided by `VK_KHR_buffer_device_address`, promoted to Vulkan 1.2. + /// + /// We only use this feature for + /// [`Features::EXPERIMENTAL_RAY_QUERY`], which requires + /// `VK_KHR_acceleration_structure`, which depends on + /// `VK_KHR_buffer_device_address`, so [`Instance::expose_adapter`] only + /// bothers to check if `VK_KHR_acceleration_structure` is available, + /// leaving this `None`. + /// + /// However, we do populate this when creating a device if + /// [`Features::EXPERIMENTAL_RAY_QUERY`] is requested. + /// + /// [`Instance::expose_adapter`]: super::Instance::expose_adapter + /// [`Features::EXPERIMENTAL_RAY_QUERY`]: wgt::Features::EXPERIMENTAL_RAY_QUERY + buffer_device_address: Option<vk::PhysicalDeviceBufferDeviceAddressFeaturesKHR<'static>>, + + /// Features provided by `VK_KHR_ray_query`, + /// + /// Vulkan requires that the feature be present if the `VK_KHR_ray_query` + /// extension is present, so [`Instance::expose_adapter`] doesn't bother retrieving + /// this from `vkGetPhysicalDeviceFeatures2`. + /// + /// However, we do populate this when creating a device if ray tracing is requested. + /// + /// [`Instance::expose_adapter`]: super::Instance::expose_adapter + ray_query: Option<vk::PhysicalDeviceRayQueryFeaturesKHR<'static>>, + + /// Features provided by `VK_KHR_ray_tracing_pipeline`. + ray_tracing_pipeline: Option<vk::PhysicalDeviceRayTracingPipelineFeaturesKHR<'static>>, + + /// Features provided by `VK_KHR_zero_initialize_workgroup_memory`, promoted + /// to Vulkan 1.3. + zero_initialize_workgroup_memory: + Option<vk::PhysicalDeviceZeroInitializeWorkgroupMemoryFeatures<'static>>, + position_fetch: Option<vk::PhysicalDeviceRayTracingPositionFetchFeaturesKHR<'static>>, + + /// Features provided by `VK_KHR_shader_atomic_int64`, promoted to Vulkan 1.2. + shader_atomic_int64: Option<vk::PhysicalDeviceShaderAtomicInt64Features<'static>>, + + /// Features provided by `VK_EXT_shader_image_atomic_int64` + shader_image_atomic_int64: Option<vk::PhysicalDeviceShaderImageAtomicInt64FeaturesEXT<'static>>, + + /// Features provided by `VK_EXT_shader_atomic_float`. + shader_atomic_float: Option<vk::PhysicalDeviceShaderAtomicFloatFeaturesEXT<'static>>, + + /// Features provided by `VK_EXT_subgroup_size_control`, promoted to Vulkan 1.3. + subgroup_size_control: Option<vk::PhysicalDeviceSubgroupSizeControlFeatures<'static>>, + + /// Features provided by `VK_KHR_maintenance4`, promoted to Vulkan 1.3. + maintenance4: Option<vk::PhysicalDeviceMaintenance4FeaturesKHR<'static>>, + + /// Features proved by `VK_EXT_mesh_shader` + mesh_shader: Option<vk::PhysicalDeviceMeshShaderFeaturesEXT<'static>>, + + /// Features provided by `VK_KHR_shader_integer_dot_product`, promoted to Vulkan 1.3. + shader_integer_dot_product: + Option<vk::PhysicalDeviceShaderIntegerDotProductFeaturesKHR<'static>>, + + /// Features provided by `VK_KHR_fragment_shader_barycentric` + shader_barycentrics: Option<vk::PhysicalDeviceFragmentShaderBarycentricFeaturesKHR<'static>>, + + /// Features provided by `VK_KHR_portability_subset`. + /// + /// Strictly speaking this tells us what features we *don't* have compared to core. + portability_subset: Option<vk::PhysicalDevicePortabilitySubsetFeaturesKHR<'static>>, + + /// Features provided by `VK_KHR_cooperative_matrix` + cooperative_matrix: Option<vk::PhysicalDeviceCooperativeMatrixFeaturesKHR<'static>>, + + /// Features provided by `VK_KHR_vulkan_memory_model`, promoted to Vulkan 1.2 + vulkan_memory_model: Option<vk::PhysicalDeviceVulkanMemoryModelFeaturesKHR<'static>>, + + shader_draw_parameters: Option<vk::PhysicalDeviceShaderDrawParametersFeatures<'static>>, +} + +impl PhysicalDeviceFeatures { + pub fn get_core(&self) -> vk::PhysicalDeviceFeatures { + self.core + } + + /// Add the members of `self` into `info.enabled_features` and its `p_next` chain. + pub fn add_to_device_create<'a>( + &'a mut self, + mut info: vk::DeviceCreateInfo<'a>, + ) -> vk::DeviceCreateInfo<'a> { + info = info.enabled_features(&self.core); + if let Some(ref mut feature) = self.descriptor_indexing { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.timeline_semaphore { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.image_robustness { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.robustness2 { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.multiview { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.astc_hdr { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.shader_float16_int8 { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self._16bit_storage { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.zero_initialize_workgroup_memory { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.acceleration_structure { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.buffer_device_address { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.ray_query { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.ray_tracing_pipeline { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.shader_atomic_int64 { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.position_fetch { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.shader_image_atomic_int64 { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.shader_atomic_float { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.subgroup_size_control { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.maintenance4 { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.mesh_shader { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.shader_integer_dot_product { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.shader_barycentrics { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.portability_subset { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.cooperative_matrix { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.vulkan_memory_model { + info = info.push_next(feature); + } + if let Some(ref mut feature) = self.shader_draw_parameters { + info = info.push_next(feature); + } + info + } + + fn supports_storage_input_output_16(&self) -> bool { + self._16bit_storage + .as_ref() + .map(|features| features.storage_input_output16 != 0) + .unwrap_or(false) + } + + /// Create a `PhysicalDeviceFeatures` that can be used to create a logical + /// device. + /// + /// Return a `PhysicalDeviceFeatures` value capturing all the Vulkan + /// features needed for the given [`Features`], [`DownlevelFlags`], and + /// [`PrivateCapabilities`]. You can use the returned value's + /// [`add_to_device_create`] method to configure a + /// [`vk::DeviceCreateInfo`] to build a logical device providing those + /// features. + /// + /// To ensure that the returned value is able to select all the Vulkan + /// features needed to express `requested_features`, `downlevel_flags`, and + /// `private_caps`: + /// + /// - The given `enabled_extensions` set must include all the extensions + /// selected by [`Adapter::required_device_extensions`] when passed + /// `features`. + /// + /// - The given `device_api_version` must be the Vulkan API version of the + /// physical device we will use to create the logical device. + /// + /// [`Features`]: wgt::Features + /// [`DownlevelFlags`]: wgt::DownlevelFlags + /// [`PrivateCapabilities`]: super::PrivateCapabilities + /// [`add_to_device_create`]: PhysicalDeviceFeatures::add_to_device_create + /// [`Adapter::required_device_extensions`]: super::Adapter::required_device_extensions + fn from_extensions_and_requested_features( + phd_capabilities: &PhysicalDeviceProperties, + phd_features: &PhysicalDeviceFeatures, + enabled_extensions: &[&'static CStr], + requested_features: wgt::Features, + downlevel_flags: wgt::DownlevelFlags, + private_caps: &super::PrivateCapabilities, + ) -> Self { + let device_api_version = phd_capabilities.device_api_version; + let needs_bindless = requested_features.intersects( + wgt::Features::TEXTURE_BINDING_ARRAY + | wgt::Features::BUFFER_BINDING_ARRAY + | wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY + | wgt::Features::STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING + | wgt::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING, + ); + let needs_partially_bound = + requested_features.intersects(wgt::Features::PARTIALLY_BOUND_BINDING_ARRAY); + + Self { + // vk::PhysicalDeviceFeatures is a struct composed of Bool32's while + // Features is a bitfield so we need to map everything manually + core: vk::PhysicalDeviceFeatures::default() + .robust_buffer_access(private_caps.robust_buffer_access) + .independent_blend(downlevel_flags.contains(wgt::DownlevelFlags::INDEPENDENT_BLEND)) + .sample_rate_shading( + downlevel_flags.contains(wgt::DownlevelFlags::MULTISAMPLED_SHADING), + ) + .image_cube_array( + downlevel_flags.contains(wgt::DownlevelFlags::CUBE_ARRAY_TEXTURES), + ) + .draw_indirect_first_instance( + requested_features.contains(wgt::Features::INDIRECT_FIRST_INSTANCE), + ) + //.dual_src_blend(requested_features.contains(wgt::Features::DUAL_SRC_BLENDING)) + .multi_draw_indirect(phd_features.core.multi_draw_indirect != 0) + .fill_mode_non_solid(requested_features.intersects( + wgt::Features::POLYGON_MODE_LINE | wgt::Features::POLYGON_MODE_POINT, + )) + //.depth_bounds(requested_features.contains(wgt::Features::DEPTH_BOUNDS)) + //.alpha_to_one(requested_features.contains(wgt::Features::ALPHA_TO_ONE)) + //.multi_viewport(requested_features.contains(wgt::Features::MULTI_VIEWPORTS)) + .sampler_anisotropy( + downlevel_flags.contains(wgt::DownlevelFlags::ANISOTROPIC_FILTERING), + ) + .texture_compression_etc2( + requested_features.contains(wgt::Features::TEXTURE_COMPRESSION_ETC2), + ) + .texture_compression_astc_ldr( + requested_features.contains(wgt::Features::TEXTURE_COMPRESSION_ASTC), + ) + .texture_compression_bc( + requested_features.contains(wgt::Features::TEXTURE_COMPRESSION_BC), + // BC provides formats for Sliced 3D + ) + //.occlusion_query_precise(requested_features.contains(wgt::Features::PRECISE_OCCLUSION_QUERY)) + .pipeline_statistics_query( + requested_features.contains(wgt::Features::PIPELINE_STATISTICS_QUERY), + ) + .vertex_pipeline_stores_and_atomics( + requested_features.contains(wgt::Features::VERTEX_WRITABLE_STORAGE), + ) + .fragment_stores_and_atomics( + downlevel_flags.contains(wgt::DownlevelFlags::FRAGMENT_WRITABLE_STORAGE), + ) + //.shader_image_gather_extended( + //.shader_storage_image_extended_formats( + .shader_uniform_buffer_array_dynamic_indexing( + requested_features.contains(wgt::Features::BUFFER_BINDING_ARRAY), + ) + .shader_storage_buffer_array_dynamic_indexing(requested_features.contains( + wgt::Features::BUFFER_BINDING_ARRAY + | wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY, + )) + .shader_sampled_image_array_dynamic_indexing( + requested_features.contains(wgt::Features::TEXTURE_BINDING_ARRAY), + ) + .shader_storage_buffer_array_dynamic_indexing(requested_features.contains( + wgt::Features::TEXTURE_BINDING_ARRAY + | wgt::Features::STORAGE_RESOURCE_BINDING_ARRAY, + )) + //.shader_storage_image_array_dynamic_indexing( + .shader_clip_distance(requested_features.contains(wgt::Features::CLIP_DISTANCES)) + //.shader_cull_distance(requested_features.contains(wgt::Features::SHADER_CULL_DISTANCE)) + .shader_float64(requested_features.contains(wgt::Features::SHADER_F64)) + .shader_int64(requested_features.contains(wgt::Features::SHADER_INT64)) + .shader_int16(requested_features.contains(wgt::Features::SHADER_I16)) + //.shader_resource_residency(requested_features.contains(wgt::Features::SHADER_RESOURCE_RESIDENCY)) + .geometry_shader(requested_features.contains(wgt::Features::PRIMITIVE_INDEX)) + .depth_clamp(requested_features.contains(wgt::Features::DEPTH_CLIP_CONTROL)) + .dual_src_blend(requested_features.contains(wgt::Features::DUAL_SOURCE_BLENDING)), + descriptor_indexing: if requested_features.intersects(INDEXING_FEATURES) { + Some( + vk::PhysicalDeviceDescriptorIndexingFeaturesEXT::default() + .shader_sampled_image_array_non_uniform_indexing(needs_bindless) + .shader_storage_image_array_non_uniform_indexing(needs_bindless) + .shader_storage_buffer_array_non_uniform_indexing(needs_bindless) + .descriptor_binding_sampled_image_update_after_bind(needs_bindless) + .descriptor_binding_storage_image_update_after_bind(needs_bindless) + .descriptor_binding_storage_buffer_update_after_bind(needs_bindless) + .descriptor_binding_partially_bound(needs_partially_bound), + ) + } else { + None + }, + timeline_semaphore: if device_api_version >= vk::API_VERSION_1_2 + || enabled_extensions.contains(&khr::timeline_semaphore::NAME) + { + Some( + vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR::default() + .timeline_semaphore(private_caps.timeline_semaphores), + ) + } else { + None + }, + image_robustness: if device_api_version >= vk::API_VERSION_1_3 + || enabled_extensions.contains(&ext::image_robustness::NAME) + { + Some( + vk::PhysicalDeviceImageRobustnessFeaturesEXT::default() + .robust_image_access(private_caps.robust_image_access), + ) + } else { + None + }, + robustness2: if enabled_extensions.contains(&ext::robustness2::NAME) { + Some( + vk::PhysicalDeviceRobustness2FeaturesEXT::default() + .robust_buffer_access2(private_caps.robust_buffer_access2) + .robust_image_access2(private_caps.robust_image_access2), + ) + } else { + None + }, + multiview: if device_api_version >= vk::API_VERSION_1_1 + || enabled_extensions.contains(&khr::multiview::NAME) + { + Some( + vk::PhysicalDeviceMultiviewFeatures::default() + .multiview(requested_features.contains(wgt::Features::MULTIVIEW)), + ) + } else { + None + }, + sampler_ycbcr_conversion: if device_api_version >= vk::API_VERSION_1_1 + || enabled_extensions.contains(&khr::sampler_ycbcr_conversion::NAME) + { + Some( + vk::PhysicalDeviceSamplerYcbcrConversionFeatures::default(), // .sampler_ycbcr_conversion(requested_features.contains(wgt::Features::TEXTURE_FORMAT_NV12)) + ) + } else { + None + }, + astc_hdr: if enabled_extensions.contains(&ext::texture_compression_astc_hdr::NAME) { + Some( + vk::PhysicalDeviceTextureCompressionASTCHDRFeaturesEXT::default() + .texture_compression_astc_hdr(true), + ) + } else { + None + }, + shader_float16_int8: match requested_features.contains(wgt::Features::SHADER_F16) { + shader_float16 if shader_float16 || private_caps.shader_int8 => Some( + vk::PhysicalDeviceShaderFloat16Int8Features::default() + .shader_float16(shader_float16) + .shader_int8(private_caps.shader_int8), + ), + _ => None, + }, + _16bit_storage: if requested_features + .intersects(wgt::Features::SHADER_F16 | wgt::Features::SHADER_I16) + { + Some( + vk::PhysicalDevice16BitStorageFeatures::default() + .storage_buffer16_bit_access(true) + .storage_input_output16(phd_features.supports_storage_input_output_16()) + .uniform_and_storage_buffer16_bit_access(true), + ) + } else { + None + }, + acceleration_structure: if enabled_extensions + .contains(&khr::acceleration_structure::NAME) + { + Some( + vk::PhysicalDeviceAccelerationStructureFeaturesKHR::default() + .acceleration_structure(true) + .descriptor_binding_acceleration_structure_update_after_bind( + requested_features + .contains(wgt::Features::ACCELERATION_STRUCTURE_BINDING_ARRAY), + ), + ) + } else { + None + }, + buffer_device_address: if enabled_extensions.contains(&khr::buffer_device_address::NAME) + { + Some( + vk::PhysicalDeviceBufferDeviceAddressFeaturesKHR::default() + .buffer_device_address(true), + ) + } else { + None + }, + ray_query: if enabled_extensions.contains(&khr::ray_query::NAME) { + Some(vk::PhysicalDeviceRayQueryFeaturesKHR::default().ray_query(true)) + } else { + None + }, + ray_tracing_pipeline: if enabled_extensions.contains(&khr::ray_tracing_pipeline::NAME) { + Some( + vk::PhysicalDeviceRayTracingPipelineFeaturesKHR::default() + .ray_tracing_pipeline(true), + ) + } else { + None + }, + zero_initialize_workgroup_memory: if device_api_version >= vk::API_VERSION_1_3 + || enabled_extensions.contains(&khr::zero_initialize_workgroup_memory::NAME) + { + Some( + vk::PhysicalDeviceZeroInitializeWorkgroupMemoryFeatures::default() + .shader_zero_initialize_workgroup_memory( + private_caps.zero_initialize_workgroup_memory, + ), + ) + } else { + None + }, + shader_atomic_int64: if device_api_version >= vk::API_VERSION_1_2 + || enabled_extensions.contains(&khr::shader_atomic_int64::NAME) + { + let needed = requested_features.intersects( + wgt::Features::SHADER_INT64_ATOMIC_ALL_OPS + | wgt::Features::SHADER_INT64_ATOMIC_MIN_MAX, + ); + Some( + vk::PhysicalDeviceShaderAtomicInt64Features::default() + .shader_buffer_int64_atomics(needed) + .shader_shared_int64_atomics(needed), + ) + } else { + None + }, + shader_image_atomic_int64: if enabled_extensions + .contains(&ext::shader_image_atomic_int64::NAME) + { + let needed = requested_features.intersects(wgt::Features::TEXTURE_INT64_ATOMIC); + Some( + vk::PhysicalDeviceShaderImageAtomicInt64FeaturesEXT::default() + .shader_image_int64_atomics(needed), + ) + } else { + None + }, + shader_atomic_float: if enabled_extensions.contains(&ext::shader_atomic_float::NAME) { + let needed = requested_features.contains(wgt::Features::SHADER_FLOAT32_ATOMIC); + Some( + vk::PhysicalDeviceShaderAtomicFloatFeaturesEXT::default() + .shader_buffer_float32_atomics(needed) + .shader_buffer_float32_atomic_add(needed), + ) + } else { + None + }, + subgroup_size_control: if device_api_version >= vk::API_VERSION_1_3 + || enabled_extensions.contains(&ext::subgroup_size_control::NAME) + { + Some( + vk::PhysicalDeviceSubgroupSizeControlFeatures::default() + .subgroup_size_control(true), + ) + } else { + None + }, + position_fetch: if enabled_extensions.contains(&khr::ray_tracing_position_fetch::NAME) { + Some( + vk::PhysicalDeviceRayTracingPositionFetchFeaturesKHR::default() + .ray_tracing_position_fetch(true), + ) + } else { + None + }, + mesh_shader: if enabled_extensions.contains(&ext::mesh_shader::NAME) { + let needed = requested_features.contains(wgt::Features::EXPERIMENTAL_MESH_SHADER); + let multiview_needed = + requested_features.contains(wgt::Features::EXPERIMENTAL_MESH_SHADER_MULTIVIEW); + Some( + vk::PhysicalDeviceMeshShaderFeaturesEXT::default() + .mesh_shader(needed) + .task_shader(needed) + .multiview_mesh_shader(multiview_needed), + ) + } else { + None + }, + maintenance4: if device_api_version >= vk::API_VERSION_1_3 + || enabled_extensions.contains(&khr::maintenance4::NAME) + { + let needed = requested_features.contains(wgt::Features::EXPERIMENTAL_MESH_SHADER); + Some(vk::PhysicalDeviceMaintenance4Features::default().maintenance4(needed)) + } else { + None + }, + shader_integer_dot_product: if device_api_version >= vk::API_VERSION_1_3 + || enabled_extensions.contains(&khr::shader_integer_dot_product::NAME) + { + Some( + vk::PhysicalDeviceShaderIntegerDotProductFeaturesKHR::default() + .shader_integer_dot_product(private_caps.shader_integer_dot_product), + ) + } else { + None + }, + shader_barycentrics: if enabled_extensions + .contains(&khr::fragment_shader_barycentric::NAME) + { + let needed = requested_features.intersects( + wgt::Features::SHADER_BARYCENTRICS | wgt::Features::SHADER_PER_VERTEX, + ); + Some( + vk::PhysicalDeviceFragmentShaderBarycentricFeaturesKHR::default() + .fragment_shader_barycentric(needed), + ) + } else { + None + }, + portability_subset: if enabled_extensions.contains(&khr::portability_subset::NAME) { + let multisample_array_needed = + requested_features.intersects(wgt::Features::MULTISAMPLE_ARRAY); + + Some( + vk::PhysicalDevicePortabilitySubsetFeaturesKHR::default() + .multisample_array_image(multisample_array_needed), + ) + } else { + None + }, + cooperative_matrix: if enabled_extensions.contains(&khr::cooperative_matrix::NAME) { + let needed = + requested_features.contains(wgt::Features::EXPERIMENTAL_COOPERATIVE_MATRIX); + Some( + vk::PhysicalDeviceCooperativeMatrixFeaturesKHR::default() + .cooperative_matrix(needed), + ) + } else { + None + }, + vulkan_memory_model: if device_api_version >= vk::API_VERSION_1_2 + || enabled_extensions.contains(&khr::vulkan_memory_model::NAME) + { + let needed = + requested_features.contains(wgt::Features::EXPERIMENTAL_COOPERATIVE_MATRIX); + Some( + vk::PhysicalDeviceVulkanMemoryModelFeaturesKHR::default() + .vulkan_memory_model(needed) + // The SPIR-V backend emits storage atomics with `Device` + // memory scope, so whenever the Vulkan memory model is + // enabled we must also enable device scope, otherwise the + // validation layers report + // `VUID-RuntimeSpirv-vulkanMemoryModel-06265`. + .vulkan_memory_model_device_scope(needed), + ) + } else { + None + }, + shader_draw_parameters: if device_api_version >= vk::API_VERSION_1_1 { + let needed = requested_features.contains(wgt::Features::SHADER_DRAW_INDEX); + Some( + vk::PhysicalDeviceShaderDrawParametersFeatures::default() + .shader_draw_parameters(needed), + ) + } else { + None + }, + } + } + + /// Compute the wgpu [`Features`] and [`DownlevelFlags`] supported by a physical device. + /// + /// Given `self`, together with the instance and physical device it was + /// built from, and a `caps` also built from those, determine which wgpu + /// features and downlevel flags the device can support. + /// + /// [`Features`]: wgt::Features + /// [`DownlevelFlags`]: wgt::DownlevelFlags + fn to_wgpu( + &self, + instance: &ash::Instance, + phd: vk::PhysicalDevice, + caps: &PhysicalDeviceProperties, + queue_props: &vk::QueueFamilyProperties, + ) -> (wgt::Features, wgt::DownlevelFlags) { + use wgt::{DownlevelFlags as Df, Features as F}; + let mut features = F::empty() + | F::MAPPABLE_PRIMARY_BUFFERS + | F::IMMEDIATES + | F::ADDRESS_MODE_CLAMP_TO_BORDER + | F::ADDRESS_MODE_CLAMP_TO_ZERO + | F::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES + | F::CLEAR_TEXTURE + | F::PIPELINE_CACHE + | F::SHADER_EARLY_DEPTH_TEST + | F::TEXTURE_ATOMIC + | F::PASSTHROUGH_SHADERS + | F::MEMORY_DECORATION_COHERENT + | F::MEMORY_DECORATION_VOLATILE; + + let mut dl_flags = Df::COMPUTE_SHADERS + | Df::BASE_VERTEX + | Df::READ_ONLY_DEPTH_STENCIL + | Df::NON_POWER_OF_TWO_MIPMAPPED_TEXTURES + | Df::COMPARISON_SAMPLERS + | Df::VERTEX_STORAGE + | Df::FRAGMENT_STORAGE + | Df::DEPTH_TEXTURE_AND_BUFFER_COPIES + | Df::BUFFER_BINDINGS_NOT_16_BYTE_ALIGNED + | Df::UNRESTRICTED_INDEX_BUFFER + | Df::INDIRECT_EXECUTION + | Df::VIEW_FORMATS + | Df::UNRESTRICTED_EXTERNAL_TEXTURE_COPIES + | Df::NONBLOCKING_QUERY_RESOLVE + | Df::SHADER_F16_IN_F32 + | Df::MSL2_1; + + dl_flags.set( + Df::SURFACE_VIEW_FORMATS, + caps.supports_extension(khr::swapchain_mutable_format::NAME) + || !caps.supports_extension(khr::swapchain::NAME), + ); + dl_flags.set(Df::CUBE_ARRAY_TEXTURES, self.core.image_cube_array != 0); + dl_flags.set(Df::ANISOTROPIC_FILTERING, self.core.sampler_anisotropy != 0); + dl_flags.set( + Df::FRAGMENT_WRITABLE_STORAGE, + self.core.fragment_stores_and_atomics != 0, + ); + dl_flags.set(Df::MULTISAMPLED_SHADING, self.core.sample_rate_shading != 0); + dl_flags.set(Df::INDEPENDENT_BLEND, self.core.independent_blend != 0); + dl_flags.set( + Df::FULL_DRAW_INDEX_UINT32, + self.core.full_draw_index_uint32 != 0, + ); + dl_flags.set(Df::DEPTH_BIAS_CLAMP, self.core.depth_bias_clamp != 0); + + features.set( + F::TIMESTAMP_QUERY + | F::TIMESTAMP_QUERY_INSIDE_ENCODERS + | F::TIMESTAMP_QUERY_INSIDE_PASSES, + // Vulkan strictly defines this as either 36-64, or zero. + queue_props.timestamp_valid_bits >= 36, + ); + features.set( + F::INDIRECT_FIRST_INSTANCE, + self.core.draw_indirect_first_instance != 0, + ); + //if self.core.dual_src_blend != 0 + features.set(F::POLYGON_MODE_LINE, self.core.fill_mode_non_solid != 0); + features.set(F::POLYGON_MODE_POINT, self.core.fill_mode_non_solid != 0); + //if self.core.depth_bounds != 0 { + //if self.core.alpha_to_one != 0 { + //if self.core.multi_viewport != 0 { + features.set( + F::TEXTURE_COMPRESSION_ETC2, + self.core.texture_compression_etc2 != 0, + ); + features.set( + F::TEXTURE_COMPRESSION_ASTC, + self.core.texture_compression_astc_ldr != 0, + ); + features.set( + F::TEXTURE_COMPRESSION_BC, + self.core.texture_compression_bc != 0, + ); + features.set( + F::TEXTURE_COMPRESSION_BC_SLICED_3D, + self.core.texture_compression_bc != 0, // BC guarantees Sliced 3D + ); + features.set( + F::PIPELINE_STATISTICS_QUERY, + self.core.pipeline_statistics_query != 0, + ); + features.set( + F::VERTEX_WRITABLE_STORAGE, + self.core.vertex_pipeline_stores_and_atomics != 0, + ); + + features.set(F::SHADER_F64, self.core.shader_float64 != 0); + features.set(F::SHADER_INT64, self.core.shader_int64 != 0); + if let Some(ref bit16) = self._16bit_storage { + features.set( + F::SHADER_I16, + self.core.shader_int16 != 0 + && bit16.storage_buffer16_bit_access != 0 + && bit16.uniform_and_storage_buffer16_bit_access != 0, + ); + } + + features.set(F::PRIMITIVE_INDEX, self.core.geometry_shader != 0); + + if let Some(ref shader_atomic_int64) = self.shader_atomic_int64 { + features.set( + F::SHADER_INT64_ATOMIC_ALL_OPS | F::SHADER_INT64_ATOMIC_MIN_MAX, + shader_atomic_int64.shader_buffer_int64_atomics != 0 + && shader_atomic_int64.shader_shared_int64_atomics != 0, + ); + } + + if let Some(ref shader_image_atomic_int64) = self.shader_image_atomic_int64 { + features.set( + F::TEXTURE_INT64_ATOMIC, + shader_image_atomic_int64 + .shader_image_int64_atomics(true) + .shader_image_int64_atomics + != 0, + ); + } + + if let Some(ref shader_atomic_float) = self.shader_atomic_float { + features.set( + F::SHADER_FLOAT32_ATOMIC, + shader_atomic_float.shader_buffer_float32_atomics != 0 + && shader_atomic_float.shader_buffer_float32_atomic_add != 0, + ); + } + + if let Some(ref shader_barycentrics) = self.shader_barycentrics { + features.set( + F::SHADER_BARYCENTRICS | F::SHADER_PER_VERTEX, + shader_barycentrics.fragment_shader_barycentric != 0, + ); + } + + //if caps.supports_extension(khr::sampler_mirror_clamp_to_edge::NAME) { + //if caps.supports_extension(ext::sampler_filter_minmax::NAME) { + features.set( + F::MULTI_DRAW_INDIRECT_COUNT, + caps.supports_extension(khr::draw_indirect_count::NAME), + ); + features.set( + F::CONSERVATIVE_RASTERIZATION, + caps.supports_extension(ext::conservative_rasterization::NAME), + ); + features.set( + F::EXPERIMENTAL_RAY_HIT_VERTEX_RETURN, + caps.supports_extension(khr::ray_tracing_position_fetch::NAME), + ); + + if let Some(ref descriptor_indexing) = self.descriptor_indexing { + // We use update-after-bind descriptors for all bind groups containing binding arrays. + // + // In those bind groups, we allow all binding types except uniform buffers to be present. + // + // As we can only switch between update-after-bind and not on a per bind group basis, + // all supported binding types need to be able to be marked update after bind. + // + // As such, we enable all features as a whole, rather individually. + let supports_descriptor_indexing = + // Sampled Images + descriptor_indexing.shader_sampled_image_array_non_uniform_indexing != 0 + && descriptor_indexing.descriptor_binding_sampled_image_update_after_bind != 0 + // Storage Images + && descriptor_indexing.shader_storage_image_array_non_uniform_indexing != 0 + && descriptor_indexing.descriptor_binding_storage_image_update_after_bind != 0 + // Storage Buffers + && descriptor_indexing.shader_storage_buffer_array_non_uniform_indexing != 0 + && descriptor_indexing.descriptor_binding_storage_buffer_update_after_bind != 0; + + let descriptor_indexing_features = F::BUFFER_BINDING_ARRAY + | F::TEXTURE_BINDING_ARRAY + | F::STORAGE_RESOURCE_BINDING_ARRAY + | F::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING + | F::STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING; + + features.set(descriptor_indexing_features, supports_descriptor_indexing); + + let supports_partially_bound = + descriptor_indexing.descriptor_binding_partially_bound != 0; + + features.set(F::PARTIALLY_BOUND_BINDING_ARRAY, supports_partially_bound); + } + + features.set(F::DEPTH_CLIP_CONTROL, self.core.depth_clamp != 0); + features.set(F::DUAL_SOURCE_BLENDING, self.core.dual_src_blend != 0); + features.set(F::CLIP_DISTANCES, self.core.shader_clip_distance != 0); + + if let Some(ref multiview) = self.multiview { + features.set(F::MULTIVIEW, multiview.multiview != 0); + features.set(F::SELECTIVE_MULTIVIEW, multiview.multiview != 0); + } + + features.set( + F::TEXTURE_FORMAT_16BIT_NORM, + is_format_16bit_norm_supported(instance, phd), + ); + + if let Some(ref astc_hdr) = self.astc_hdr { + features.set( + F::TEXTURE_COMPRESSION_ASTC_HDR, + astc_hdr.texture_compression_astc_hdr != 0, + ); + } + + if self.core.texture_compression_astc_ldr != 0 { + features.set( + F::TEXTURE_COMPRESSION_ASTC_SLICED_3D, + supports_astc_3d(instance, phd), + ); + } + + if let (Some(ref f16_i8), Some(ref bit16)) = (self.shader_float16_int8, self._16bit_storage) + { + // Note `storage_input_output16` is not required, we polyfill `f16` I/O using `f32` + // types when this capability is not available + features.set( + F::SHADER_F16, + f16_i8.shader_float16 != 0 + && bit16.storage_buffer16_bit_access != 0 + && bit16.uniform_and_storage_buffer16_bit_access != 0, + ); + } + + if let Some(ref subgroup) = caps.subgroup { + if (caps.device_api_version >= vk::API_VERSION_1_3 + || caps.supports_extension(ext::subgroup_size_control::NAME)) + && subgroup.supported_operations.contains( + vk::SubgroupFeatureFlags::BASIC + | vk::SubgroupFeatureFlags::VOTE + | vk::SubgroupFeatureFlags::ARITHMETIC + | vk::SubgroupFeatureFlags::BALLOT + | vk::SubgroupFeatureFlags::SHUFFLE + | vk::SubgroupFeatureFlags::SHUFFLE_RELATIVE + | vk::SubgroupFeatureFlags::QUAD, + ) + { + features.set( + F::SUBGROUP, + subgroup + .supported_stages + .contains(vk::ShaderStageFlags::COMPUTE | vk::ShaderStageFlags::FRAGMENT), + ); + features.set( + F::SUBGROUP_VERTEX, + subgroup + .supported_stages + .contains(vk::ShaderStageFlags::VERTEX), + ); + features.insert(F::SUBGROUP_BARRIER); + } + } + + let supports_depth_format = |format| { + supports_format( + instance, + phd, + format, + vk::ImageTiling::OPTIMAL, + depth_stencil_required_flags(), + ) + }; + + let texture_s8 = supports_depth_format(vk::Format::S8_UINT); + let texture_d32 = supports_depth_format(vk::Format::D32_SFLOAT); + let texture_d24_s8 = supports_depth_format(vk::Format::D24_UNORM_S8_UINT); + let texture_d32_s8 = supports_depth_format(vk::Format::D32_SFLOAT_S8_UINT); + + let stencil8 = texture_s8 || texture_d24_s8; + let depth24_plus_stencil8 = texture_d24_s8 || texture_d32_s8; + + dl_flags.set( + Df::WEBGPU_TEXTURE_FORMAT_SUPPORT, + stencil8 && depth24_plus_stencil8 && texture_d32, + ); + + features.set(F::DEPTH32FLOAT_STENCIL8, texture_d32_s8); + + let supports_acceleration_structures = caps + .supports_extension(khr::deferred_host_operations::NAME) + && caps.supports_extension(khr::acceleration_structure::NAME) + && caps.supports_extension(khr::buffer_device_address::NAME); + + let supports_ray_query = + supports_acceleration_structures && caps.supports_extension(khr::ray_query::NAME); + let supports_acceleration_structure_binding_array = supports_ray_query + && self + .acceleration_structure + .as_ref() + .is_some_and(|features| { + features.descriptor_binding_acceleration_structure_update_after_bind != 0 + }); + + features.set( + F::EXPERIMENTAL_RAY_QUERY + // Although this doesn't really require ray queries, it does not make sense to be enabled if acceleration structures + // aren't enabled. + | F::EXTENDED_ACCELERATION_STRUCTURE_VERTEX_FORMATS, + supports_ray_query, + ); + + // Binding arrays of TLAS are supported on Vulkan when ray queries are supported. + // + // Note: this flag is used for shader-side `binding_array<acceleration_structure>` as well as + // allowing `BindGroupLayoutEntry::count = Some(...)` for `BindingType::AccelerationStructure`. + features.set( + F::ACCELERATION_STRUCTURE_BINDING_ARRAY, + supports_acceleration_structure_binding_array, + ); + + if supports_acceleration_structures + && caps.supports_extension(khr::ray_tracing_pipeline::NAME) + { + features.insert( + F::EXPERIMENTAL_RAY_TRACING_PIPELINES + // Same reason as for ray queries. + | F::EXTENDED_ACCELERATION_STRUCTURE_VERTEX_FORMATS, + ); + } + + let rg11b10ufloat_renderable = supports_format( + instance, + phd, + vk::Format::B10G11R11_UFLOAT_PACK32, + vk::ImageTiling::OPTIMAL, + vk::FormatFeatureFlags::COLOR_ATTACHMENT + | vk::FormatFeatureFlags::COLOR_ATTACHMENT_BLEND, + ); + features.set(F::RG11B10UFLOAT_RENDERABLE, rg11b10ufloat_renderable); + + features.set( + F::BGRA8UNORM_STORAGE, + supports_bgra8unorm_storage(instance, phd, caps.device_api_version), + ); + + features.set( + F::FLOAT32_FILTERABLE, + is_float32_filterable_supported(instance, phd), + ); + + features.set( + F::FLOAT32_BLENDABLE, + is_float32_blendable_supported(instance, phd), + ); + + if let Some(ref _sampler_ycbcr_conversion) = self.sampler_ycbcr_conversion { + features.set( + F::TEXTURE_FORMAT_NV12, + supports_format( + instance, + phd, + vk::Format::G8_B8R8_2PLANE_420_UNORM, + vk::ImageTiling::OPTIMAL, + vk::FormatFeatureFlags::SAMPLED_IMAGE + | vk::FormatFeatureFlags::TRANSFER_SRC + | vk::FormatFeatureFlags::TRANSFER_DST, + ) && !caps.is_driver(vk::DriverId::MOLTENVK), + ); + } + + if let Some(ref _sampler_ycbcr_conversion) = self.sampler_ycbcr_conversion { + features.set( + F::TEXTURE_FORMAT_P010, + supports_format( + instance, + phd, + vk::Format::G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16, + vk::ImageTiling::OPTIMAL, + vk::FormatFeatureFlags::SAMPLED_IMAGE + | vk::FormatFeatureFlags::TRANSFER_SRC + | vk::FormatFeatureFlags::TRANSFER_DST, + ) && !caps.is_driver(vk::DriverId::MOLTENVK), + ); + } + + features.set( + F::VULKAN_GOOGLE_DISPLAY_TIMING, + caps.supports_extension(google::display_timing::NAME), + ); + + features.set( + F::VULKAN_EXTERNAL_MEMORY_WIN32, + caps.supports_extension(khr::external_memory_win32::NAME), + ); + features.set( + F::VULKAN_EXTERNAL_MEMORY_FD, + caps.supports_extension(khr::external_memory_fd::NAME), + ); + features.set( + F::VULKAN_EXTERNAL_MEMORY_DMA_BUF, + caps.supports_extension(khr::external_memory_fd::NAME) + && caps.supports_extension(ext::external_memory_dma_buf::NAME) + && caps.supports_extension(ext::image_drm_format_modifier::NAME), + ); + features.set( + F::EXPERIMENTAL_MESH_SHADER, + caps.supports_extension(ext::mesh_shader::NAME), + ); + features.set( + F::EXPERIMENTAL_MESH_SHADER_POINTS, + caps.supports_extension(ext::mesh_shader::NAME), + ); + if let Some(ref mesh_shader) = self.mesh_shader { + features.set( + F::EXPERIMENTAL_MESH_SHADER_MULTIVIEW, + mesh_shader.multiview_mesh_shader != 0, + ); + } + + // Not supported by default by `VK_KHR_portability_subset`, which we use on apple platforms. + features.set( + F::MULTISAMPLE_ARRAY, + self.portability_subset + .map(|p| p.multisample_array_image == vk::TRUE) + .unwrap_or(true), + ); + // Enable cooperative matrix if any configuration is supported. The SPIR-V + // we emit for it uses `Device`-scope atomics under the Vulkan memory model, + // so the device must also support `vulkanMemoryModelDeviceScope`, otherwise + // those shaders trip `VUID-RuntimeSpirv-vulkanMemoryModel-06265`. + features.set( + F::EXPERIMENTAL_COOPERATIVE_MATRIX, + !caps.cooperative_matrix_properties.is_empty() + && self.vulkan_memory_model.is_some_and(|m| { + m.vulkan_memory_model == vk::TRUE + && m.vulkan_memory_model_device_scope == vk::TRUE + }), + ); + + features.set( + F::SHADER_DRAW_INDEX, + self.shader_draw_parameters + .is_some_and(|a| a.shader_draw_parameters != 0) + || caps.supports_extension(c"VK_KHR_shader_draw_parameters"), + ); + + (features, dl_flags) + } +} + +/// Vulkan "properties" structures gathered about a physical device. +/// +/// This structure holds the properties of a [`vk::PhysicalDevice`]: +/// - the standard Vulkan device properties +/// - the `VkExtensionProperties` structs for all available extensions, and +/// - the per-extension properties structures for the available extensions that +/// `wgpu` cares about. +/// +/// Generally, if you get it from any of these functions, it's stored +/// here: +/// - `vkEnumerateDeviceExtensionProperties` +/// - `vkGetPhysicalDeviceProperties` +/// - `vkGetPhysicalDeviceProperties2` +/// +/// This also includes a copy of the device API version, since we can +/// use that as a shortcut for searching for an extension, if the +/// extension has been promoted to core in the current version. +/// +/// This does not include device features; for those, see +/// [`PhysicalDeviceFeatures`]. +#[derive(Default, Debug)] +pub struct PhysicalDeviceProperties { + /// Extensions supported by the `vk::PhysicalDevice`, + /// as returned by `vkEnumerateDeviceExtensionProperties`. + supported_extensions: Vec<vk::ExtensionProperties>, + + /// Properties of the `vk::PhysicalDevice`, as returned by + /// `vkGetPhysicalDeviceProperties`. + properties: vk::PhysicalDeviceProperties, + + /// Additional `vk::PhysicalDevice` properties from the + /// `VK_KHR_maintenance3` extension, promoted to Vulkan 1.1. + maintenance_3: Option<vk::PhysicalDeviceMaintenance3Properties<'static>>, + + /// Additional `vk::PhysicalDevice` properties from the + /// `VK_KHR_maintenance4` extension, promoted to Vulkan 1.3. + maintenance_4: Option<vk::PhysicalDeviceMaintenance4Properties<'static>>, + + /// Additional `vk::PhysicalDevice` properties from the + /// `VK_EXT_descriptor_indexing` extension, promoted to Vulkan 1.2. + descriptor_indexing: Option<vk::PhysicalDeviceDescriptorIndexingPropertiesEXT<'static>>, + + /// Additional `vk::PhysicalDevice` properties from the + /// `VK_KHR_acceleration_structure` extension. + acceleration_structure: Option<vk::PhysicalDeviceAccelerationStructurePropertiesKHR<'static>>, + + /// Additional `vk::PhysicalDevice` properties from the + /// `VK_KHR_ray_tracing_pipeline` extension. + ray_tracing_pipeline: Option<vk::PhysicalDeviceRayTracingPipelinePropertiesKHR<'static>>, + + /// Additional `vk::PhysicalDevice` properties from the + /// `VK_KHR_driver_properties` extension, promoted to Vulkan 1.2. + driver: Option<vk::PhysicalDeviceDriverPropertiesKHR<'static>>, + + /// Additional `vk::PhysicalDevice` properties from Vulkan 1.1. + subgroup: Option<vk::PhysicalDeviceSubgroupProperties<'static>>, + + /// Additional `vk::PhysicalDevice` properties from the + /// `VK_EXT_subgroup_size_control` extension, promoted to Vulkan 1.3. + subgroup_size_control: Option<vk::PhysicalDeviceSubgroupSizeControlProperties<'static>>, + + /// Additional `vk::PhysicalDevice` properties from the + /// `VK_EXT_robustness2` extension. + robustness2: Option<vk::PhysicalDeviceRobustness2PropertiesEXT<'static>>, + + /// Additional `vk::PhysicalDevice` properties from the + /// `VK_EXT_mesh_shader` extension. + mesh_shader: Option<vk::PhysicalDeviceMeshShaderPropertiesEXT<'static>>, + + /// Additional `vk::PhysicalDevice` properties from the + /// `VK_KHR_multiview` extension. + multiview: Option<vk::PhysicalDeviceMultiviewPropertiesKHR<'static>>, + + /// `VK_EXT_pci_bus_info` extension. + pci_bus_info: Option<vk::PhysicalDevicePCIBusInfoPropertiesEXT<'static>>, + + /// The device API version. + /// + /// Which is the version of Vulkan supported for device-level functionality. + /// + /// It is associated with a `VkPhysicalDevice` and its children. + device_api_version: u32, + + /// Supported cooperative matrix configurations. + /// + /// This is determined by querying `vkGetPhysicalDeviceCooperativeMatrixPropertiesKHR`. + cooperative_matrix_properties: Vec<wgt::CooperativeMatrixProperties>, +} + +impl PhysicalDeviceProperties { + pub fn properties(&self) -> vk::PhysicalDeviceProperties { + self.properties + } + + pub fn supports_extension(&self, extension: &CStr) -> bool { + self.supported_extensions + .iter() + .any(|ep| ep.extension_name_as_c_str() == Ok(extension)) + } + + pub fn is_driver(&self, id: vk::DriverId) -> bool { + self.driver.is_some_and(|driver| driver.driver_id == id) + } + + /// Map `requested_features` to the list of Vulkan extension strings required to create the logical device. + fn get_required_extensions(&self, requested_features: wgt::Features) -> Vec<&'static CStr> { + let mut extensions = Vec::new(); + + // Note that quite a few extensions depend on the `VK_KHR_get_physical_device_properties2` instance extension. + // We enable `VK_KHR_get_physical_device_properties2` unconditionally (if available). + + // Require `VK_KHR_swapchain` + extensions.push(khr::swapchain::NAME); + + if self.device_api_version < vk::API_VERSION_1_1 { + // Require `VK_KHR_maintenance1` + extensions.push(khr::maintenance1::NAME); + + // Optional `VK_KHR_maintenance2` + if self.supports_extension(khr::maintenance2::NAME) { + extensions.push(khr::maintenance2::NAME); + } + + // Optional `VK_KHR_maintenance3` + if self.supports_extension(khr::maintenance3::NAME) { + extensions.push(khr::maintenance3::NAME); + } + + // Require `VK_KHR_storage_buffer_storage_class` + extensions.push(khr::storage_buffer_storage_class::NAME); + + // Require `VK_KHR_multiview` if the associated feature was requested + if requested_features.contains(wgt::Features::MULTIVIEW) { + extensions.push(khr::multiview::NAME); + } + + // Require `VK_KHR_sampler_ycbcr_conversion` if the associated feature was requested + if requested_features.contains(wgt::Features::TEXTURE_FORMAT_NV12) { + extensions.push(khr::sampler_ycbcr_conversion::NAME); + } + + // Require `VK_KHR_16bit_storage` if `SHADER_F16` or `SHADER_I16` was requested + if requested_features.intersects(wgt::Features::SHADER_F16 | wgt::Features::SHADER_I16) + { + // - Feature `SHADER_F16` also requires `VK_KHR_shader_float16_int8`, but we always + // require that anyway (if it is available) below. + // - `VK_KHR_16bit_storage` requires `VK_KHR_storage_buffer_storage_class`, however + // we require that one already. + extensions.push(khr::_16bit_storage::NAME); + } + + if requested_features.contains(wgt::Features::SHADER_DRAW_INDEX) { + extensions.push(khr::shader_draw_parameters::NAME); + } + } + + if self.device_api_version < vk::API_VERSION_1_2 { + // Optional `VK_KHR_image_format_list` + if self.supports_extension(khr::image_format_list::NAME) { + extensions.push(khr::image_format_list::NAME); + } + + // Optional `VK_KHR_driver_properties` + if self.supports_extension(khr::driver_properties::NAME) { + extensions.push(khr::driver_properties::NAME); + } + + // Optional `VK_KHR_timeline_semaphore` + if self.supports_extension(khr::timeline_semaphore::NAME) { + extensions.push(khr::timeline_semaphore::NAME); + } + + // Require `VK_EXT_descriptor_indexing` if one of the associated features was requested + if requested_features.intersects(INDEXING_FEATURES) { + extensions.push(ext::descriptor_indexing::NAME); + } + + // Always require `VK_KHR_shader_float16_int8` if available as it enables + // Int8 optimizations. Also require it even if it's not available but + // requested so that we get a corresponding error message. + if requested_features.contains(wgt::Features::SHADER_F16) + || self.supports_extension(khr::shader_float16_int8::NAME) + { + extensions.push(khr::shader_float16_int8::NAME); + } + + if requested_features.intersects(wgt::Features::EXPERIMENTAL_MESH_SHADER) { + extensions.push(khr::spirv_1_4::NAME); + } + + //extensions.push(khr::sampler_mirror_clamp_to_edge::NAME); + //extensions.push(ext::sampler_filter_minmax::NAME); + } + + if self.device_api_version < vk::API_VERSION_1_3 { + // Optional `VK_KHR_maintenance4` + if self.supports_extension(khr::maintenance4::NAME) { + extensions.push(khr::maintenance4::NAME); + } + + // Optional `VK_EXT_image_robustness` + if self.supports_extension(ext::image_robustness::NAME) { + extensions.push(ext::image_robustness::NAME); + } + + // Require `VK_EXT_subgroup_size_control` if the associated feature was requested + if requested_features.contains(wgt::Features::SUBGROUP) { + extensions.push(ext::subgroup_size_control::NAME); + } + + // Optional `VK_KHR_shader_integer_dot_product` + if self.supports_extension(khr::shader_integer_dot_product::NAME) { + extensions.push(khr::shader_integer_dot_product::NAME); + } + } + + // Optional `VK_KHR_swapchain_mutable_format` + if self.supports_extension(khr::swapchain_mutable_format::NAME) { + extensions.push(khr::swapchain_mutable_format::NAME); + } + + // Optional `VK_EXT_robustness2` + if self.supports_extension(ext::robustness2::NAME) { + extensions.push(ext::robustness2::NAME); + } + + // Optional `VK_KHR_external_memory_win32` + if self.supports_extension(khr::external_memory_win32::NAME) { + extensions.push(khr::external_memory_win32::NAME); + } + + // Optional `VK_KHR_external_memory_fd` + if self.supports_extension(khr::external_memory_fd::NAME) { + extensions.push(khr::external_memory_fd::NAME); + } + + // Optional `VK_EXT_external_memory_dma` + if self.supports_extension(ext::external_memory_dma_buf::NAME) { + extensions.push(ext::external_memory_dma_buf::NAME); + } + + // Optional `VK_EXT_image_drm_format_modifier` + if self.supports_extension(ext::image_drm_format_modifier::NAME) { + extensions.push(ext::image_drm_format_modifier::NAME); + } + + // Optional `VK_EXT_memory_budget` + if self.supports_extension(ext::memory_budget::NAME) { + extensions.push(ext::memory_budget::NAME); + } else { + log::debug!("VK_EXT_memory_budget is not available.") + } + + // Require `VK_KHR_draw_indirect_count` if the associated feature was requested + // Even though Vulkan 1.2 has promoted the extension to core, we must require the extension to avoid + // large amounts of spaghetti involved with using PhysicalDeviceVulkan12Features. + if requested_features.contains(wgt::Features::MULTI_DRAW_INDIRECT_COUNT) { + extensions.push(khr::draw_indirect_count::NAME); + } + + // Require `VK_KHR_deferred_host_operations`, `VK_KHR_acceleration_structure` `VK_KHR_buffer_device_address` (for acceleration structures) if either `EXPERIMENTAL_RAY_QUERY` or `EXPERIMENTAL_RAY_TRACING_PIPELINES` were requested. + if requested_features.intersects( + wgt::Features::EXPERIMENTAL_RAY_QUERY + | wgt::Features::EXPERIMENTAL_RAY_TRACING_PIPELINES, + ) { + extensions.push(khr::deferred_host_operations::NAME); + extensions.push(khr::acceleration_structure::NAME); + extensions.push(khr::buffer_device_address::NAME); + } + + // Require `VK_KHR_ray_query` if `EXPERIMENTAL_RAY_QUERY` was requested + if requested_features.contains(wgt::Features::EXPERIMENTAL_RAY_QUERY) { + extensions.push(khr::ray_query::NAME); + } + + // Require `VK_KHR_ray_tracing_pipeline` if `EXPERIMENTAL_RAY_TRACING_PIPELINES` was requested + if requested_features.contains(wgt::Features::EXPERIMENTAL_RAY_TRACING_PIPELINES) { + extensions.push(khr::ray_tracing_pipeline::NAME); + } + + if requested_features.contains(wgt::Features::EXPERIMENTAL_RAY_HIT_VERTEX_RETURN) { + extensions.push(khr::ray_tracing_position_fetch::NAME) + } + + // Require `VK_EXT_conservative_rasterization` if the associated feature was requested + if requested_features.contains(wgt::Features::CONSERVATIVE_RASTERIZATION) { + extensions.push(ext::conservative_rasterization::NAME); + } + + // Require `VK_KHR_portability_subset` on macOS/iOS + #[cfg(target_vendor = "apple")] + extensions.push(khr::portability_subset::NAME); + + // Require `VK_EXT_texture_compression_astc_hdr` if the associated feature was requested + if requested_features.contains(wgt::Features::TEXTURE_COMPRESSION_ASTC_HDR) { + extensions.push(ext::texture_compression_astc_hdr::NAME); + } + + // Require `VK_KHR_shader_atomic_int64` if the associated feature was requested + if requested_features.intersects( + wgt::Features::SHADER_INT64_ATOMIC_ALL_OPS | wgt::Features::SHADER_INT64_ATOMIC_MIN_MAX, + ) { + extensions.push(khr::shader_atomic_int64::NAME); + } + + // Require `VK_EXT_shader_image_atomic_int64` if the associated feature was requested + if requested_features.intersects(wgt::Features::TEXTURE_INT64_ATOMIC) { + extensions.push(ext::shader_image_atomic_int64::NAME); + } + + // Require `VK_EXT_shader_atomic_float` if the associated feature was requested + if requested_features.contains(wgt::Features::SHADER_FLOAT32_ATOMIC) { + extensions.push(ext::shader_atomic_float::NAME); + } + + // Require VK_GOOGLE_display_timing if the associated feature was requested + if requested_features.contains(wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING) { + extensions.push(google::display_timing::NAME); + } + + if requested_features.contains(wgt::Features::EXPERIMENTAL_MESH_SHADER) { + extensions.push(ext::mesh_shader::NAME); + } + + // Require `VK_KHR_fragment_shader_barycentric` if an associated feature was requested + // Vulkan bundles both barycentrics and per-vertex attributes under the same feature. + if requested_features + .intersects(wgt::Features::SHADER_BARYCENTRICS | wgt::Features::SHADER_PER_VERTEX) + { + extensions.push(khr::fragment_shader_barycentric::NAME); + } + + // Require `VK_KHR_cooperative_matrix` if the associated feature was requested + if requested_features.contains(wgt::Features::EXPERIMENTAL_COOPERATIVE_MATRIX) { + extensions.push(khr::cooperative_matrix::NAME); + } + + extensions + } + + fn to_wgpu_limits(&self) -> wgt::Limits { + let limits = &self.properties.limits; + + // Default is only implemented for tuples up to a certain size. + let ( + mut max_task_workgroup_total_count, + mut max_task_workgroups_per_dimension, + mut max_mesh_workgroup_total_count, + mut max_mesh_workgroups_per_dimension, + ) = Default::default(); + let ( + mut max_task_invocations_per_workgroup, + mut max_task_invocations_per_dimension, + mut max_mesh_invocations_per_workgroup, + mut max_mesh_invocations_per_dimension, + mut max_task_payload_size, + mut max_mesh_output_vertices, + mut max_mesh_output_primitives, + mut max_mesh_output_layers, + mut max_mesh_multiview_view_count, + ) = Default::default(); + if let Some(m) = self.mesh_shader { + max_task_workgroup_total_count = m.max_task_work_group_total_count; + max_task_workgroups_per_dimension = + m.max_task_work_group_count.into_iter().min().unwrap(); + max_mesh_workgroup_total_count = m.max_mesh_work_group_total_count; + max_mesh_workgroups_per_dimension = + m.max_mesh_work_group_count.into_iter().min().unwrap(); + max_task_invocations_per_workgroup = m.max_task_work_group_invocations; + max_task_invocations_per_dimension = + m.max_task_work_group_size.into_iter().min().unwrap(); + max_mesh_invocations_per_workgroup = m.max_mesh_work_group_invocations; + max_mesh_invocations_per_dimension = + m.max_mesh_work_group_size.into_iter().min().unwrap(); + max_task_payload_size = m.max_task_payload_size; + max_mesh_output_vertices = m.max_mesh_output_vertices; + max_mesh_output_primitives = m.max_mesh_output_primitives; + max_mesh_output_layers = m.max_mesh_output_layers; + max_mesh_multiview_view_count = m.max_mesh_multiview_view_count; + } + + let max_memory_allocation_size = self + .maintenance_3 + .map(|maintenance_3| maintenance_3.max_memory_allocation_size) + .unwrap_or(u64::MAX); + let max_buffer_size = self + .maintenance_4 + .map(|maintenance_4| maintenance_4.max_buffer_size) + .unwrap_or(u64::MAX); + let max_buffer_size = max_buffer_size.min(max_memory_allocation_size); + + // Prevent very large buffers on mesa and most android devices, and in all cases + // don't risk confusing JS by exceeding the range of a double. + let is_nvidia = self.properties.vendor_id == crate::auxil::db::nvidia::VENDOR; + let max_buffer_size_cap = + if (cfg!(target_os = "linux") || cfg!(target_os = "android")) && !is_nvidia { + i32::MAX as u64 + } else { + 1u64 << 52 + }; + + let max_buffer_size = max_buffer_size.min(max_buffer_size_cap); + + let mut max_binding_array_elements = 0; + let mut max_sampler_binding_array_elements = 0; + if let Some(ref descriptor_indexing) = self.descriptor_indexing { + max_binding_array_elements = descriptor_indexing + .max_descriptor_set_update_after_bind_sampled_images + .min(descriptor_indexing.max_descriptor_set_update_after_bind_storage_images) + .min(descriptor_indexing.max_descriptor_set_update_after_bind_storage_buffers) + .min(descriptor_indexing.max_per_stage_descriptor_update_after_bind_sampled_images) + .min(descriptor_indexing.max_per_stage_descriptor_update_after_bind_storage_images) + .min( + descriptor_indexing.max_per_stage_descriptor_update_after_bind_storage_buffers, + ); + + max_sampler_binding_array_elements = descriptor_indexing + .max_descriptor_set_update_after_bind_samplers + .min(descriptor_indexing.max_per_stage_descriptor_update_after_bind_samplers); + } + + const MAX_SHADER_STAGES_PER_PIPELINE: u32 = 2; + + // When summed, the 3 limits below must be under Vulkan's maxFragmentCombinedOutputResources. + // https://gpuweb.github.io/gpuweb/correspondence/#vulkan-maxFragmentCombinedOutputResources + // + // - maxStorageTexturesPerShaderStage, WebGPU default: 4 + // - maxStorageBuffersPerShaderStage, WebGPU default: 8 + // - maxColorAttachments, WebGPU default: 8 + // + // However, maxFragmentCombinedOutputResources should be ignored on + // intel/nvidia/amd/imgtec since it's not reported correctly. + // + // https://github.com/gpuweb/gpuweb/issues/3631#issuecomment-1498747606 + // https://github.com/gpuweb/gpuweb/issues/4018 + let mut max_storage_textures_per_shader_stage = limits + .max_per_stage_descriptor_storage_images + .min(limits.max_descriptor_set_storage_images / MAX_SHADER_STAGES_PER_PIPELINE); + let mut max_storage_buffers_per_shader_stage = limits + .max_per_stage_descriptor_storage_buffers + .min(limits.max_descriptor_set_storage_buffers / MAX_SHADER_STAGES_PER_PIPELINE); + let mut max_color_attachments = limits + .max_color_attachments + .min(limits.max_fragment_output_attachments); + + let ignore_max_fragment_combined_output_resources_by_device = [ + crate::auxil::db::intel::VENDOR, + crate::auxil::db::nvidia::VENDOR, + crate::auxil::db::amd::VENDOR, + crate::auxil::db::imgtec::VENDOR, + ] + .contains(&self.properties.vendor_id); + let ignore_max_fragment_combined_output_resources_by_driver = + self.is_driver(vk::DriverId::MESA_AGXV); + let ignore_max_fragment_combined_output_resources = + ignore_max_fragment_combined_output_resources_by_device + || ignore_max_fragment_combined_output_resources_by_driver; + + if !ignore_max_fragment_combined_output_resources { + crate::auxil::cap_limits_to_be_under_the_sum_limit( + [ + &mut max_storage_textures_per_shader_stage, + &mut max_storage_buffers_per_shader_stage, + &mut max_color_attachments, + ], + limits.max_fragment_combined_output_resources, + ); + } + + // When summed, the 5 limits below must be under Vulkan's maxPerStageResources. + // + // - maxUniformBuffersPerShaderStage, WebGPU default: 12 + // - maxSampledTexturesPerShaderStage, WebGPU default: 16 + // - maxStorageTexturesPerShaderStage, WebGPU default: 4 + // - maxStorageBuffersPerShaderStage, WebGPU default: 8 + // - maxColorAttachments, WebGPU default: 8 + // + // Note: Vulkan's texel buffers and input attachments also count towards + // maxPerStageResources but we don't make use of them. + let mut max_sampled_textures_per_shader_stage = limits + .max_per_stage_descriptor_sampled_images + .min(limits.max_descriptor_set_sampled_images / MAX_SHADER_STAGES_PER_PIPELINE); + let mut max_uniform_buffers_per_shader_stage = limits + .max_per_stage_descriptor_uniform_buffers + .min(limits.max_descriptor_set_uniform_buffers / MAX_SHADER_STAGES_PER_PIPELINE); + + crate::auxil::cap_limits_to_be_under_the_sum_limit( + [ + &mut max_sampled_textures_per_shader_stage, + &mut max_uniform_buffers_per_shader_stage, + &mut max_storage_textures_per_shader_stage, + &mut max_storage_buffers_per_shader_stage, + &mut max_color_attachments, + ], + limits.max_per_stage_resources, + ); + + // Acceleration structure limits + let mut max_blas_geometry_count = 0; + let mut max_blas_primitive_count = 0; + let mut max_tlas_instance_count = 0; + let mut max_acceleration_structures_per_shader_stage = 0; + if let Some(properties) = self.acceleration_structure { + max_blas_geometry_count = properties.max_geometry_count as u32; + max_blas_primitive_count = properties.max_primitive_count as u32; + max_tlas_instance_count = properties.max_instance_count as u32; + max_acceleration_structures_per_shader_stage = properties + .max_per_stage_descriptor_acceleration_structures + .min( + properties.max_descriptor_set_acceleration_structures + / MAX_SHADER_STAGES_PER_PIPELINE, + ); + } + + // When summed, the 6 limits below must be under Vulkan's + // maxPerSetDescriptors / MAX_SHADER_STAGES_PER_PIPELINE. + // + // - maxUniformBuffersPerShaderStage, WebGPU default: 12 + // - maxSampledTexturesPerShaderStage, WebGPU default: 16 + // - maxStorageTexturesPerShaderStage, WebGPU default: 4 + // - maxStorageBuffersPerShaderStage, WebGPU default: 8 + // - maxSamplersPerShaderStage, WebGPU default: 16 + // - maxAccelerationStructuresPerShaderStage, Native only + // + // Note: All Vulkan's descriptor types count towards maxPerSetDescriptors but + // we don't use all of them. + // See https://registry.khronos.org/vulkan/specs/latest/html/vkspec.html#interfaces-resources-limits + let max_per_set_descriptors = self + .maintenance_3 + .map(|maintenance_3| maintenance_3.max_per_set_descriptors) + // The lowest value seen in reports is 312, use 256 as a safe default. + // https://vulkan.gpuinfo.org/displayextensionproperty.php?extensionname=VK_KHR_maintenance3&extensionproperty=maxPerSetDescriptors&platform=all + // https://vulkan.gpuinfo.org/displaycoreproperty.php?core=1.1&name=maxPerSetDescriptors&platform=all + .unwrap_or(256); + + let mut max_samplers_per_shader_stage = limits + .max_per_stage_descriptor_samplers + .min(limits.max_descriptor_set_samplers / MAX_SHADER_STAGES_PER_PIPELINE); + + crate::auxil::cap_limits_to_be_under_the_sum_limit( + [ + &mut max_sampled_textures_per_shader_stage, + &mut max_uniform_buffers_per_shader_stage, + &mut max_storage_textures_per_shader_stage, + &mut max_storage_buffers_per_shader_stage, + &mut max_samplers_per_shader_stage, + &mut max_acceleration_structures_per_shader_stage, + ], + max_per_set_descriptors / MAX_SHADER_STAGES_PER_PIPELINE, + ); + + // Use max(default, maxPerSetDescriptors) since the spec requires this + // limit to be at least 1000. This is ok because we already lowered + // all the other relevant per stage limits so their sum is lower + // than maxPerSetDescriptors. + let max_bindings_per_bind_group = 1000.max(max_per_set_descriptors); + + // TODO: programmatically determine this, if possible. It's unclear whether we can + // as of https://github.com/gpuweb/gpuweb/issues/2965#issuecomment-1361315447. + // + // In theory some tilers may not support this much. We can't tell however, and + // the driver will throw a DEVICE_REMOVED if it goes too high in usage. This is fine. + let max_color_attachment_bytes_per_sample = + max_color_attachments * wgt::TextureFormat::MAX_TARGET_PIXEL_BYTE_COST; + + let mut max_ray_dispatch_count = 0; + let mut max_ray_recursion_depth = 0; + + if let Some(properties) = self.ray_tracing_pipeline { + max_ray_dispatch_count = properties.max_ray_dispatch_invocation_count; + max_ray_recursion_depth = properties.max_ray_recursion_depth; + } + + let max_multiview_view_count = self + .multiview + .map(|a| a.max_multiview_view_count.min(32)) + .unwrap_or(0); + + crate::auxil::adjust_raw_limits(wgt::Limits { + // + // WebGPU LIMITS: + // Based on https://gpuweb.github.io/gpuweb/correspondence/#limits + // + max_texture_dimension_1d: limits.max_image_dimension1_d, + max_texture_dimension_2d: limits + .max_image_dimension2_d + .min(limits.max_image_dimension_cube) + .min(limits.max_framebuffer_width) + .min(limits.max_framebuffer_height), + max_texture_dimension_3d: limits.max_image_dimension3_d, + max_texture_array_layers: limits.max_image_array_layers, + max_bind_groups: limits.max_bound_descriptor_sets, + // No limit. + max_bind_groups_plus_vertex_buffers: u32::MAX, + max_bindings_per_bind_group, + max_dynamic_uniform_buffers_per_pipeline_layout: limits + .max_descriptor_set_uniform_buffers_dynamic, + max_dynamic_storage_buffers_per_pipeline_layout: limits + .max_descriptor_set_storage_buffers_dynamic, + max_samplers_per_shader_stage, + max_sampled_textures_per_shader_stage, + max_storage_textures_per_shader_stage, + max_storage_buffers_per_shader_stage, + max_uniform_buffers_per_shader_stage, + max_vertex_buffers: limits.max_vertex_input_bindings, + max_buffer_size, + max_uniform_buffer_binding_size: limits + .max_uniform_buffer_range + .min(crate::auxil::MAX_I32_BINDING_SIZE) + .into(), + max_storage_buffer_binding_size: limits + .max_storage_buffer_range + .min(crate::auxil::MAX_I32_BINDING_SIZE) + .into(), + min_uniform_buffer_offset_alignment: limits.min_uniform_buffer_offset_alignment as u32, + min_storage_buffer_offset_alignment: limits.min_storage_buffer_offset_alignment as u32, + max_vertex_attributes: limits.max_vertex_input_attributes, + max_vertex_buffer_array_stride: limits.max_vertex_input_binding_stride, + max_inter_stage_shader_variables: limits + .max_vertex_output_components + .min(limits.max_fragment_input_components) + / 4 + - 1, // -1 for position + max_color_attachments, + max_color_attachment_bytes_per_sample, + max_compute_workgroup_storage_size: limits.max_compute_shared_memory_size, + max_compute_invocations_per_workgroup: limits.max_compute_work_group_invocations, + max_compute_workgroup_size_x: limits.max_compute_work_group_size[0], + max_compute_workgroup_size_y: limits.max_compute_work_group_size[1], + max_compute_workgroup_size_z: limits.max_compute_work_group_size[2], + max_compute_workgroups_per_dimension: limits.max_compute_work_group_count[0] + .min(limits.max_compute_work_group_count[1]) + .min(limits.max_compute_work_group_count[2]), + max_immediate_size: limits.max_push_constants_size, + // + // NATIVE (Non-WebGPU) LIMITS: + // + max_non_sampler_bindings: u32::MAX, + + max_binding_array_elements_per_shader_stage: max_binding_array_elements, + max_binding_array_sampler_elements_per_shader_stage: max_sampler_binding_array_elements, + max_binding_array_acceleration_structure_elements_per_shader_stage: if self + .descriptor_indexing + .is_some() + { + max_acceleration_structures_per_shader_stage + } else { + 0 + }, + + max_task_workgroup_total_count, + max_task_workgroups_per_dimension, + max_mesh_workgroup_total_count, + max_mesh_workgroups_per_dimension, + + max_task_invocations_per_workgroup, + max_task_invocations_per_dimension, + + max_mesh_invocations_per_workgroup, + max_mesh_invocations_per_dimension, + + max_task_payload_size, + max_mesh_output_vertices, + max_mesh_output_primitives, + max_mesh_output_layers, + max_mesh_multiview_view_count, + + max_blas_primitive_count, + max_blas_geometry_count, + max_tlas_instance_count, + max_acceleration_structures_per_shader_stage, + max_buffers_and_acceleration_structures_per_shader_stage: u32::MAX, + + max_multiview_view_count, + + max_ray_dispatch_count, + max_ray_recursion_depth, + }) + } + + /// Return a `wgpu_hal::Alignments` structure describing this adapter. + /// + /// The `using_robustness2` argument says how this adapter will implement + /// `wgpu_hal`'s guarantee that shaders can only read the [accessible + /// region][ar] of bindgroup's buffer bindings: + /// + /// - If this adapter will depend on `VK_EXT_robustness2`'s + /// `robustBufferAccess2` feature to apply bounds checks to shader buffer + /// access, `using_robustness2` must be `true`. + /// + /// - Otherwise, this adapter must use Naga to inject bounds checks on + /// buffer accesses, and `using_robustness2` must be `false`. + /// + /// [ar]: ../../struct.BufferBinding.html#accessible-region + fn to_hal_alignments(&self, using_robustness2: bool) -> crate::Alignments { + let limits = &self.properties.limits; + crate::Alignments { + buffer_copy_offset: wgt::BufferSize::new(limits.optimal_buffer_copy_offset_alignment) + .unwrap(), + buffer_copy_pitch: wgt::BufferSize::new(limits.optimal_buffer_copy_row_pitch_alignment) + .unwrap(), + uniform_bounds_check_alignment: { + let alignment = if using_robustness2 { + self.robustness2 + .unwrap() // if we're using it, we should have its properties + .robust_uniform_buffer_access_size_alignment + } else { + // If the `robustness2` properties are unavailable, then `robustness2` is not available either Naga-injected bounds checks are precise. + 1 + }; + wgt::BufferSize::new(alignment).unwrap() + }, + raw_tlas_instance_size: 64, + ray_tracing_scratch_buffer_alignment: self.acceleration_structure.map_or( + 0, + |acceleration_structure| { + acceleration_structure.min_acceleration_structure_scratch_offset_alignment + }, + ), + ray_tracing_pipeline_group_data_size: self + .ray_tracing_pipeline + .map_or(0, |ray_tracing_pipeline| { + ray_tracing_pipeline.shader_group_handle_size + }), + ray_tracing_pipeline_group_data_alignment: self + .ray_tracing_pipeline + .map_or(0, |ray_tracing_pipeline| { + ray_tracing_pipeline.shader_group_handle_alignment + }), + ray_tracing_pipeline_data_offset_alignment: self + .ray_tracing_pipeline + .map_or(0, |ray_tracing_pipeline| { + ray_tracing_pipeline.shader_group_base_alignment + }), + } + } +} + +impl super::InstanceShared { + fn inspect( + &self, + phd: vk::PhysicalDevice, + ) -> (PhysicalDeviceProperties, PhysicalDeviceFeatures) { + let capabilities = { + let mut capabilities = PhysicalDeviceProperties::default(); + capabilities.supported_extensions = + unsafe { self.raw.enumerate_device_extension_properties(phd).unwrap() }; + capabilities.properties = unsafe { self.raw.get_physical_device_properties(phd) }; + capabilities.device_api_version = capabilities.properties.api_version; + + let supports_multiview = capabilities.device_api_version >= vk::API_VERSION_1_1 + || capabilities.supports_extension(khr::multiview::NAME); + + if let Some(ref get_device_properties) = self.get_physical_device_properties { + // Get these now to avoid borrowing conflicts later + let supports_maintenance3 = capabilities.device_api_version >= vk::API_VERSION_1_1 + || capabilities.supports_extension(khr::maintenance3::NAME); + let supports_maintenance4 = capabilities.device_api_version >= vk::API_VERSION_1_3 + || capabilities.supports_extension(khr::maintenance4::NAME); + let supports_descriptor_indexing = capabilities.device_api_version + >= vk::API_VERSION_1_2 + || capabilities.supports_extension(ext::descriptor_indexing::NAME); + let supports_driver_properties = capabilities.device_api_version + >= vk::API_VERSION_1_2 + || capabilities.supports_extension(khr::driver_properties::NAME); + let supports_subgroup_size_control = capabilities.device_api_version + >= vk::API_VERSION_1_3 + || capabilities.supports_extension(ext::subgroup_size_control::NAME); + let supports_robustness2 = capabilities.supports_extension(ext::robustness2::NAME); + let supports_pci_bus_info = + capabilities.supports_extension(ext::pci_bus_info::NAME); + + let supports_acceleration_structure = + capabilities.supports_extension(khr::acceleration_structure::NAME); + + let supports_ray_tracing_pipeline = + capabilities.supports_extension(khr::ray_tracing_pipeline::NAME); + + let supports_mesh_shader = capabilities.supports_extension(ext::mesh_shader::NAME); + + let mut properties2 = vk::PhysicalDeviceProperties2KHR::default(); + if supports_maintenance3 { + let next = capabilities + .maintenance_3 + .insert(vk::PhysicalDeviceMaintenance3Properties::default()); + properties2 = properties2.push_next(next); + } + + if supports_maintenance4 { + let next = capabilities + .maintenance_4 + .insert(vk::PhysicalDeviceMaintenance4Properties::default()); + properties2 = properties2.push_next(next); + } + + if supports_descriptor_indexing { + let next = capabilities + .descriptor_indexing + .insert(vk::PhysicalDeviceDescriptorIndexingPropertiesEXT::default()); + properties2 = properties2.push_next(next); + } + + if supports_acceleration_structure { + let next = capabilities + .acceleration_structure + .insert(vk::PhysicalDeviceAccelerationStructurePropertiesKHR::default()); + properties2 = properties2.push_next(next); + } + + if supports_ray_tracing_pipeline { + let next = capabilities + .ray_tracing_pipeline + .insert(vk::PhysicalDeviceRayTracingPipelinePropertiesKHR::default()); + properties2 = properties2.push_next(next); + } + + if supports_driver_properties { + let next = capabilities + .driver + .insert(vk::PhysicalDeviceDriverPropertiesKHR::default()); + properties2 = properties2.push_next(next); + } + + if capabilities.device_api_version >= vk::API_VERSION_1_1 { + let next = capabilities + .subgroup + .insert(vk::PhysicalDeviceSubgroupProperties::default()); + properties2 = properties2.push_next(next); + } + + if supports_subgroup_size_control { + let next = capabilities + .subgroup_size_control + .insert(vk::PhysicalDeviceSubgroupSizeControlProperties::default()); + properties2 = properties2.push_next(next); + } + + if supports_robustness2 { + let next = capabilities + .robustness2 + .insert(vk::PhysicalDeviceRobustness2PropertiesEXT::default()); + properties2 = properties2.push_next(next); + } + + if supports_pci_bus_info { + let next = capabilities + .pci_bus_info + .insert(vk::PhysicalDevicePCIBusInfoPropertiesEXT::default()); + properties2 = properties2.push_next(next); + } + + if supports_mesh_shader { + let next = capabilities + .mesh_shader + .insert(vk::PhysicalDeviceMeshShaderPropertiesEXT::default()); + properties2 = properties2.push_next(next); + } + + if supports_multiview { + let next = capabilities + .multiview + .insert(vk::PhysicalDeviceMultiviewProperties::default()); + properties2 = properties2.push_next(next); + } + + unsafe { + get_device_properties.get_physical_device_properties2(phd, &mut properties2) + }; + + // Query cooperative matrix properties + if capabilities.supports_extension(khr::cooperative_matrix::NAME) { + let coop_matrix = + khr::cooperative_matrix::Instance::new(&self.entry, &self.raw); + capabilities.cooperative_matrix_properties = + query_cooperative_matrix_properties(&coop_matrix, phd); + } + + // Suppress some capabilities to avoid known problems + if is_intel_igpu_outdated_for_robustness2(&capabilities) { + capabilities + .supported_extensions + .retain(|&x| x.extension_name_as_c_str() != Ok(ext::robustness2::NAME)); + capabilities.robustness2 = None; + } + + // Due to https://gitlab.freedesktop.org/mesa/mesa/-/work_items/15725 + // TODO(https://github.com/gfx-rs/wgpu/issues/9742): enable on + // fixed driver versions, when available + if capabilities.is_driver(vk::DriverId::MESA_RADV) { + capabilities + .supported_extensions + .retain(|&x| x.extension_name_as_c_str() != Ok(ext::memory_budget::NAME)); + } + }; + capabilities + }; + + let mut features = PhysicalDeviceFeatures::default(); + features.core = if let Some(ref get_device_properties) = self.get_physical_device_properties + { + let core = vk::PhysicalDeviceFeatures::default(); + let mut features2 = vk::PhysicalDeviceFeatures2KHR::default().features(core); + + // `VK_KHR_multiview` is promoted to 1.1 + if capabilities.device_api_version >= vk::API_VERSION_1_1 + || capabilities.supports_extension(khr::multiview::NAME) + { + let next = features + .multiview + .insert(vk::PhysicalDeviceMultiviewFeatures::default()); + features2 = features2.push_next(next); + } + + // `VK_KHR_sampler_ycbcr_conversion` is promoted to 1.1 + if capabilities.device_api_version >= vk::API_VERSION_1_1 + || capabilities.supports_extension(khr::sampler_ycbcr_conversion::NAME) + { + let next = features + .sampler_ycbcr_conversion + .insert(vk::PhysicalDeviceSamplerYcbcrConversionFeatures::default()); + features2 = features2.push_next(next); + } + + if capabilities.supports_extension(ext::descriptor_indexing::NAME) { + let next = features + .descriptor_indexing + .insert(vk::PhysicalDeviceDescriptorIndexingFeaturesEXT::default()); + features2 = features2.push_next(next); + } + + // `VK_KHR_timeline_semaphore` is promoted to 1.2, but has no + // changes, so we can keep using the extension unconditionally. + if capabilities.supports_extension(khr::timeline_semaphore::NAME) { + let next = features + .timeline_semaphore + .insert(vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR::default()); + features2 = features2.push_next(next); + } + + // `VK_KHR_shader_atomic_int64` is promoted to 1.2, but has no + // changes, so we can keep using the extension unconditionally. + if capabilities.device_api_version >= vk::API_VERSION_1_2 + || capabilities.supports_extension(khr::shader_atomic_int64::NAME) + { + let next = features + .shader_atomic_int64 + .insert(vk::PhysicalDeviceShaderAtomicInt64Features::default()); + features2 = features2.push_next(next); + } + + if capabilities.supports_extension(ext::shader_image_atomic_int64::NAME) { + let next = features + .shader_image_atomic_int64 + .insert(vk::PhysicalDeviceShaderImageAtomicInt64FeaturesEXT::default()); + features2 = features2.push_next(next); + } + if capabilities.supports_extension(ext::shader_atomic_float::NAME) { + let next = features + .shader_atomic_float + .insert(vk::PhysicalDeviceShaderAtomicFloatFeaturesEXT::default()); + features2 = features2.push_next(next); + } + if capabilities.supports_extension(ext::image_robustness::NAME) { + let next = features + .image_robustness + .insert(vk::PhysicalDeviceImageRobustnessFeaturesEXT::default()); + features2 = features2.push_next(next); + } + if capabilities.supports_extension(ext::robustness2::NAME) { + let next = features + .robustness2 + .insert(vk::PhysicalDeviceRobustness2FeaturesEXT::default()); + features2 = features2.push_next(next); + } + if capabilities.supports_extension(ext::texture_compression_astc_hdr::NAME) { + let next = features + .astc_hdr + .insert(vk::PhysicalDeviceTextureCompressionASTCHDRFeaturesEXT::default()); + features2 = features2.push_next(next); + } + + // `VK_KHR_shader_float16_int8` is promoted to 1.2 + if capabilities.device_api_version >= vk::API_VERSION_1_2 + || capabilities.supports_extension(khr::shader_float16_int8::NAME) + { + let next = features + .shader_float16_int8 + .insert(vk::PhysicalDeviceShaderFloat16Int8FeaturesKHR::default()); + features2 = features2.push_next(next); + } + + if capabilities.supports_extension(khr::_16bit_storage::NAME) { + let next = features + ._16bit_storage + .insert(vk::PhysicalDevice16BitStorageFeaturesKHR::default()); + features2 = features2.push_next(next); + } + if capabilities.supports_extension(khr::acceleration_structure::NAME) { + let next = features + .acceleration_structure + .insert(vk::PhysicalDeviceAccelerationStructureFeaturesKHR::default()); + features2 = features2.push_next(next); + } + + if capabilities.supports_extension(khr::ray_tracing_position_fetch::NAME) { + let next = features + .position_fetch + .insert(vk::PhysicalDeviceRayTracingPositionFetchFeaturesKHR::default()); + features2 = features2.push_next(next); + } + + // `VK_KHR_maintenance4` is promoted to 1.3 + if capabilities.device_api_version >= vk::API_VERSION_1_3 + || capabilities.supports_extension(khr::maintenance4::NAME) + { + let next = features + .maintenance4 + .insert(vk::PhysicalDeviceMaintenance4Features::default()); + features2 = features2.push_next(next); + } + + // `VK_KHR_zero_initialize_workgroup_memory` is promoted to 1.3 + if capabilities.device_api_version >= vk::API_VERSION_1_3 + || capabilities.supports_extension(khr::zero_initialize_workgroup_memory::NAME) + { + let next = features + .zero_initialize_workgroup_memory + .insert(vk::PhysicalDeviceZeroInitializeWorkgroupMemoryFeatures::default()); + features2 = features2.push_next(next); + } + + // `VK_EXT_subgroup_size_control` is promoted to 1.3 + if capabilities.device_api_version >= vk::API_VERSION_1_3 + || capabilities.supports_extension(ext::subgroup_size_control::NAME) + { + let next = features + .subgroup_size_control + .insert(vk::PhysicalDeviceSubgroupSizeControlFeatures::default()); + features2 = features2.push_next(next); + } + + if capabilities.supports_extension(ext::mesh_shader::NAME) { + let next = features + .mesh_shader + .insert(vk::PhysicalDeviceMeshShaderFeaturesEXT::default()); + features2 = features2.push_next(next); + } + + // `VK_KHR_shader_integer_dot_product` is promoted to 1.3 + if capabilities.device_api_version >= vk::API_VERSION_1_3 + || capabilities.supports_extension(khr::shader_integer_dot_product::NAME) + { + let next = features + .shader_integer_dot_product + .insert(vk::PhysicalDeviceShaderIntegerDotProductFeatures::default()); + features2 = features2.push_next(next); + } + + if capabilities.supports_extension(khr::fragment_shader_barycentric::NAME) { + let next = features + .shader_barycentrics + .insert(vk::PhysicalDeviceFragmentShaderBarycentricFeaturesKHR::default()); + features2 = features2.push_next(next); + } + + if capabilities.supports_extension(khr::portability_subset::NAME) { + let next = features + .portability_subset + .insert(vk::PhysicalDevicePortabilitySubsetFeaturesKHR::default()); + features2 = features2.push_next(next); + } + + if capabilities.supports_extension(khr::cooperative_matrix::NAME) { + let next = features + .cooperative_matrix + .insert(vk::PhysicalDeviceCooperativeMatrixFeaturesKHR::default()); + features2 = features2.push_next(next); + } + + if capabilities.device_api_version >= vk::API_VERSION_1_2 + || capabilities.supports_extension(khr::vulkan_memory_model::NAME) + { + let next = features + .vulkan_memory_model + .insert(vk::PhysicalDeviceVulkanMemoryModelFeaturesKHR::default()); + features2 = features2.push_next(next); + } + + if capabilities.device_api_version >= vk::API_VERSION_1_1 { + let next = features + .shader_draw_parameters + .insert(vk::PhysicalDeviceShaderDrawParametersFeatures::default()); + features2 = features2.push_next(next); + } + + unsafe { get_device_properties.get_physical_device_features2(phd, &mut features2) }; + features2.features + } else { + unsafe { self.raw.get_physical_device_features(phd) } + }; + + (capabilities, features) + } +} + +impl super::Instance { + pub fn expose_adapter( + &self, + phd: vk::PhysicalDevice, + ) -> Option<crate::ExposedAdapter<super::Api>> { + use crate::auxil::db; + + let (phd_capabilities, phd_features) = self.shared.inspect(phd); + + let mem_properties = { + profiling::scope!("vkGetPhysicalDeviceMemoryProperties"); + unsafe { self.shared.raw.get_physical_device_memory_properties(phd) } + }; + let memory_types = &mem_properties.memory_types_as_slice(); + let supports_lazily_allocated = memory_types.iter().any(|mem| { + mem.property_flags + .contains(vk::MemoryPropertyFlags::LAZILY_ALLOCATED) + }); + + let device_type = match phd_capabilities.properties.device_type { + vk::PhysicalDeviceType::OTHER => wgt::DeviceType::Other, + vk::PhysicalDeviceType::INTEGRATED_GPU => wgt::DeviceType::IntegratedGpu, + vk::PhysicalDeviceType::DISCRETE_GPU => wgt::DeviceType::DiscreteGpu, + vk::PhysicalDeviceType::VIRTUAL_GPU => wgt::DeviceType::VirtualGpu, + vk::PhysicalDeviceType::CPU => wgt::DeviceType::Cpu, + _ => wgt::DeviceType::Other, + }; + let info = wgt::AdapterInfo { + name: { + phd_capabilities + .properties + .device_name_as_c_str() + .ok() + .and_then(|name| name.to_str().ok()) + .unwrap_or("?") + .to_owned() + }, + vendor: phd_capabilities.properties.vendor_id, + device: phd_capabilities.properties.device_id, + device_pci_bus_id: phd_capabilities + .pci_bus_info + .filter(|info| info.pci_bus != 0 || info.pci_device != 0) + .map(|info| { + format!( + "{:04x}:{:02x}:{:02x}.{}", + info.pci_domain, info.pci_bus, info.pci_device, info.pci_function + ) + }) + .unwrap_or_default(), + driver: { + phd_capabilities + .driver + .as_ref() + .and_then(|driver| driver.driver_name_as_c_str().ok()) + .and_then(|name| name.to_str().ok()) + .unwrap_or("?") + .to_owned() + }, + driver_info: { + phd_capabilities + .driver + .as_ref() + .and_then(|driver| driver.driver_info_as_c_str().ok()) + .and_then(|name| name.to_str().ok()) + .unwrap_or("?") + .to_owned() + }, + subgroup_min_size: phd_capabilities + .subgroup_size_control + .map(|subgroup_size| subgroup_size.min_subgroup_size) + .unwrap_or(wgt::MINIMUM_SUBGROUP_MIN_SIZE), + subgroup_max_size: phd_capabilities + .subgroup_size_control + .map(|subgroup_size| subgroup_size.max_subgroup_size) + .unwrap_or(wgt::MAXIMUM_SUBGROUP_MAX_SIZE), + transient_saves_memory: Some(supports_lazily_allocated), + ..wgt::AdapterInfo::new(device_type, wgt::Backend::Vulkan) + }; + let mut workarounds = super::Workarounds::empty(); + { + // TODO: only enable for particular devices + workarounds |= super::Workarounds::SEPARATE_ENTRY_POINTS; + workarounds.set( + super::Workarounds::EMPTY_RESOLVE_ATTACHMENT_LISTS, + phd_capabilities.properties.vendor_id == db::qualcomm::VENDOR, + ); + workarounds.set( + super::Workarounds::FORCE_FILL_BUFFER_WITH_SIZE_GREATER_4096_ALIGNED_OFFSET_16, + phd_capabilities.properties.vendor_id == db::nvidia::VENDOR, + ); + }; + + if let Some(driver) = phd_capabilities.driver { + if driver.conformance_version.major == 0 { + if driver.driver_id == vk::DriverId::MOLTENVK { + log::debug!("Adapter is not Vulkan compliant, but is MoltenVK, continuing"); + } else if self + .shared + .flags + .contains(wgt::InstanceFlags::ALLOW_UNDERLYING_NONCOMPLIANT_ADAPTER) + { + log::debug!("Adapter is not Vulkan compliant: {}", info.name); + } else { + log::debug!( + "Adapter is not Vulkan compliant, hiding adapter: {}", + info.name + ); + return None; + } + } + } + if phd_capabilities.device_api_version == vk::API_VERSION_1_0 + && !phd_capabilities.supports_extension(khr::storage_buffer_storage_class::NAME) + { + log::debug!( + "SPIR-V storage buffer class is not supported, hiding adapter: {}", + info.name + ); + return None; + } + if !phd_capabilities.supports_extension(khr::maintenance1::NAME) + && phd_capabilities.device_api_version < vk::API_VERSION_1_1 + { + log::debug!( + "VK_KHR_maintenance1 is not supported, hiding adapter: {}", + info.name + ); + return None; + } + + let queue_families = unsafe { + self.shared + .raw + .get_physical_device_queue_family_properties(phd) + }; + let queue_family_properties = queue_families.first()?; + let queue_flags = queue_family_properties.queue_flags; + if !queue_flags.contains(vk::QueueFlags::GRAPHICS) { + log::debug!("The first queue only exposes {queue_flags:?}"); + return None; + } + + let (available_features, mut downlevel_flags) = phd_features.to_wgpu( + &self.shared.raw, + phd, + &phd_capabilities, + queue_family_properties, + ); + + if phd_capabilities.is_driver(vk::DriverId::MESA_LLVMPIPE) { + // The `F16_IN_F32` instructions do not normally require native `F16` support, but on + // llvmpipe, they do. + downlevel_flags.set( + wgt::DownlevelFlags::SHADER_F16_IN_F32, + available_features.contains(wgt::Features::SHADER_F16), + ); + } + + downlevel_flags.set( + wgt::DownlevelFlags::TEXTURE_COMPRESSION, + available_features.contains(wgt::Features::TEXTURE_COMPRESSION_BC) + || available_features.contains( + wgt::Features::TEXTURE_COMPRESSION_ETC2 + | wgt::Features::TEXTURE_COMPRESSION_ASTC, + ), + ); + + let has_robust_buffer_access2 = phd_features + .robustness2 + .as_ref() + .map(|r| r.robust_buffer_access2 == 1) + .unwrap_or_default(); + + let alignments = phd_capabilities.to_hal_alignments(has_robust_buffer_access2); + + let private_caps = super::PrivateCapabilities { + image_view_usage: phd_capabilities.device_api_version >= vk::API_VERSION_1_1 + || phd_capabilities.supports_extension(khr::maintenance2::NAME), + timeline_semaphores: match phd_features.timeline_semaphore { + Some(features) => features.timeline_semaphore == vk::TRUE, + None => phd_features + .timeline_semaphore + .is_some_and(|ext| ext.timeline_semaphore != 0), + }, + texture_d24: supports_format( + &self.shared.raw, + phd, + vk::Format::X8_D24_UNORM_PACK32, + vk::ImageTiling::OPTIMAL, + depth_stencil_required_flags(), + ), + texture_d24_s8: supports_format( + &self.shared.raw, + phd, + vk::Format::D24_UNORM_S8_UINT, + vk::ImageTiling::OPTIMAL, + depth_stencil_required_flags(), + ), + texture_s8: supports_format( + &self.shared.raw, + phd, + vk::Format::S8_UINT, + vk::ImageTiling::OPTIMAL, + depth_stencil_required_flags(), + ), + multi_draw_indirect: phd_features.core.multi_draw_indirect != 0, + max_draw_indirect_count: phd_capabilities.properties.limits.max_draw_indirect_count, + non_coherent_map_mask: phd_capabilities.properties.limits.non_coherent_atom_size - 1, + can_present: true, + //TODO: make configurable + robust_buffer_access: phd_features.core.robust_buffer_access != 0, + robust_image_access: match phd_features.robustness2 { + Some(ref f) => f.robust_image_access2 != 0, + None => phd_features + .image_robustness + .is_some_and(|ext| ext.robust_image_access != 0), + }, + robust_buffer_access2: has_robust_buffer_access2, + robust_image_access2: phd_features + .robustness2 + .as_ref() + .map(|r| r.robust_image_access2 == 1) + .unwrap_or_default(), + zero_initialize_workgroup_memory: phd_features + .zero_initialize_workgroup_memory + .is_some_and(|ext| ext.shader_zero_initialize_workgroup_memory == vk::TRUE), + image_format_list: phd_capabilities.device_api_version >= vk::API_VERSION_1_2 + || phd_capabilities.supports_extension(khr::image_format_list::NAME), + maximum_samplers: phd_capabilities + .properties + .limits + .max_sampler_allocation_count, + shader_integer_dot_product: phd_features + .shader_integer_dot_product + .is_some_and(|ext| ext.shader_integer_dot_product != 0), + shader_int8: phd_features + .shader_float16_int8 + .is_some_and(|features| features.shader_int8 != 0), + multiview_instance_index_limit: phd_capabilities + .multiview + .map(|a| a.max_multiview_instance_index) + .unwrap_or(0), + scratch_buffer_alignment: alignments.ray_tracing_scratch_buffer_alignment, + ray_tracing_pipeline_group_data_size: alignments.ray_tracing_pipeline_group_data_size, + }; + let capabilities = crate::Capabilities { + limits: phd_capabilities.to_wgpu_limits(), + alignments, + downlevel: wgt::DownlevelCapabilities { + flags: downlevel_flags, + limits: wgt::DownlevelLimits {}, + shader_model: wgt::ShaderModel::Sm5, //TODO? + }, + cooperative_matrix_properties: phd_capabilities.cooperative_matrix_properties.clone(), + }; + + let adapter = super::Adapter { + raw: phd, + instance: Arc::clone(&self.shared), + //queue_families, + known_memory_flags: vk::MemoryPropertyFlags::DEVICE_LOCAL + | vk::MemoryPropertyFlags::HOST_VISIBLE + | vk::MemoryPropertyFlags::HOST_COHERENT + | vk::MemoryPropertyFlags::HOST_CACHED + | vk::MemoryPropertyFlags::LAZILY_ALLOCATED, + phd_capabilities, + phd_features, + downlevel_flags, + private_caps, + workarounds, + }; + + Some(crate::ExposedAdapter { + adapter, + info, + features: available_features, + capabilities, + }) + } +} + +impl super::Adapter { + pub fn raw_physical_device(&self) -> vk::PhysicalDevice { + self.raw + } + + pub fn get_physical_device_features(&self) -> &PhysicalDeviceFeatures { + &self.phd_features + } + + pub fn physical_device_capabilities(&self) -> &PhysicalDeviceProperties { + &self.phd_capabilities + } + + pub fn shared_instance(&self) -> &super::InstanceShared { + &self.instance + } + + pub fn required_device_extensions(&self, features: wgt::Features) -> Vec<&'static CStr> { + let (supported_extensions, unsupported_extensions) = self + .phd_capabilities + .get_required_extensions(features) + .iter() + .partition::<Vec<&CStr>, _>(|&&extension| { + self.phd_capabilities.supports_extension(extension) + }); + + if !unsupported_extensions.is_empty() { + log::debug!("Missing extensions: {unsupported_extensions:?}"); + } + + log::debug!("Supported extensions: {supported_extensions:?}"); + supported_extensions + } + + /// Create a `PhysicalDeviceFeatures` for opening a logical device with + /// `features` from this adapter. + /// + /// The given `enabled_extensions` set must include all the extensions + /// selected by [`required_device_extensions`] when passed `features`. + /// Otherwise, the `PhysicalDeviceFeatures` value may not be able to select + /// all the Vulkan features needed to represent `features` and this + /// adapter's characteristics. + /// + /// Typically, you'd simply call `required_device_extensions`, and then pass + /// its return value and the feature set you gave it directly to this + /// function. But it's fine to add more extensions to the list. + /// + /// [`required_device_extensions`]: Self::required_device_extensions + pub fn physical_device_features( + &self, + enabled_extensions: &[&'static CStr], + features: wgt::Features, + ) -> PhysicalDeviceFeatures { + PhysicalDeviceFeatures::from_extensions_and_requested_features( + &self.phd_capabilities, + &self.phd_features, + enabled_extensions, + features, + self.downlevel_flags, + &self.private_caps, + ) + } + + /// # Safety + /// + /// - `raw_device` must be created from this adapter. + /// - `raw_device` must be created using `family_index`, `enabled_extensions` and `physical_device_features()` + /// - `enabled_extensions` must be a superset of `required_device_extensions()`. + /// - If `drop_callback` is [`None`], wgpu-hal will take ownership of `raw_device`. If + /// `drop_callback` is [`Some`], `raw_device` must be valid until the callback is called. + #[allow(clippy::too_many_arguments)] + pub unsafe fn device_from_raw( + &self, + raw_device: ash::Device, + drop_callback: Option<crate::DropCallback>, + enabled_extensions: &[&'static CStr], + features: wgt::Features, + limits: &wgt::Limits, + memory_hints: &wgt::MemoryHints, + family_index: u32, + queue_index: u32, + ) -> Result<crate::OpenDevice<super::Api>, crate::DeviceError> { + let mem_properties = { + profiling::scope!("vkGetPhysicalDeviceMemoryProperties"); + unsafe { + self.instance + .raw + .get_physical_device_memory_properties(self.raw) + } + }; + let memory_types = &mem_properties.memory_types_as_slice(); + let valid_ash_memory_types = memory_types.iter().enumerate().fold(0, |u, (i, mem)| { + if self.known_memory_flags.contains(mem.property_flags) { + u | (1 << i) + } else { + u + } + }); + + // Note that VK_EXT_debug_utils is an instance extension (enabled at the instance + // level) but contains a few functions that can be loaded directly on the Device for a + // dispatch-table-less pointer. + let debug_utils_fn = if self.instance.extensions.contains(&ext::debug_utils::NAME) { + Some(ext::debug_utils::Device::new( + &self.instance.raw, + &raw_device, + )) + } else { + None + }; + let indirect_count_fn = if enabled_extensions.contains(&khr::draw_indirect_count::NAME) { + Some(khr::draw_indirect_count::Device::new( + &self.instance.raw, + &raw_device, + )) + } else { + None + }; + let timeline_semaphore_fn = if enabled_extensions.contains(&khr::timeline_semaphore::NAME) { + Some(super::ExtensionFn::Extension( + khr::timeline_semaphore::Device::new(&self.instance.raw, &raw_device), + )) + } else if self.phd_capabilities.device_api_version >= vk::API_VERSION_1_2 { + Some(super::ExtensionFn::Promoted) + } else { + None + }; + let ray_tracing_fns = if enabled_extensions.contains(&khr::acceleration_structure::NAME) + && enabled_extensions.contains(&khr::buffer_device_address::NAME) + { + Some(super::RayTracingDeviceExtensionFunctions { + acceleration_structure: khr::acceleration_structure::Device::new( + &self.instance.raw, + &raw_device, + ), + buffer_device_address: khr::buffer_device_address::Device::new( + &self.instance.raw, + &raw_device, + ), + }) + } else { + None + }; + let ray_tracing_pipeline_fns = + if enabled_extensions.contains(&khr::ray_tracing_pipeline::NAME) { + Some(khr::ray_tracing_pipeline::Device::new( + &self.instance.raw, + &raw_device, + )) + } else { + None + }; + let mesh_shading_fns = if enabled_extensions.contains(&ext::mesh_shader::NAME) { + Some(ext::mesh_shader::Device::new( + &self.instance.raw, + &raw_device, + )) + } else { + None + }; + let external_memory_fd_fn = if enabled_extensions.contains(&khr::external_memory_fd::NAME) { + Some(khr::external_memory_fd::Device::new( + &self.instance.raw, + &raw_device, + )) + } else { + None + }; + + let naga_options = { + use naga::back::spv; + + // The following capabilities are always available + // see https://registry.khronos.org/vulkan/specs/1.3-extensions/html/chap52.html#spirvenv-capabilities + let mut capabilities = vec![ + spv::Capability::Shader, + spv::Capability::Matrix, + spv::Capability::Sampled1D, + spv::Capability::Image1D, + spv::Capability::ImageQuery, + spv::Capability::DerivativeControl, + spv::Capability::StorageImageExtendedFormats, + ]; + + if self + .downlevel_flags + .contains(wgt::DownlevelFlags::CUBE_ARRAY_TEXTURES) + { + capabilities.push(spv::Capability::SampledCubeArray); + } + + if self + .downlevel_flags + .contains(wgt::DownlevelFlags::MULTISAMPLED_SHADING) + { + capabilities.push(spv::Capability::SampleRateShading); + } + + if features.contains(wgt::Features::MULTIVIEW) { + capabilities.push(spv::Capability::MultiView); + } + + if features.contains(wgt::Features::PRIMITIVE_INDEX) { + capabilities.push(spv::Capability::Geometry); + } + + if features.intersects(wgt::Features::SUBGROUP | wgt::Features::SUBGROUP_VERTEX) { + capabilities.push(spv::Capability::GroupNonUniform); + capabilities.push(spv::Capability::GroupNonUniformVote); + capabilities.push(spv::Capability::GroupNonUniformArithmetic); + capabilities.push(spv::Capability::GroupNonUniformBallot); + capabilities.push(spv::Capability::GroupNonUniformShuffle); + capabilities.push(spv::Capability::GroupNonUniformShuffleRelative); + capabilities.push(spv::Capability::GroupNonUniformQuad); + } + + if features.intersects( + wgt::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING + | wgt::Features::STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING + | wgt::Features::UNIFORM_BUFFER_BINDING_ARRAYS, + ) { + capabilities.push(spv::Capability::ShaderNonUniform); + } + if features.contains(wgt::Features::BGRA8UNORM_STORAGE) { + capabilities.push(spv::Capability::StorageImageWriteWithoutFormat); + } + + if features.contains(wgt::Features::EXPERIMENTAL_RAY_QUERY) { + capabilities.push(spv::Capability::RayQueryKHR); + } + + if features.contains(wgt::Features::SHADER_INT64) { + capabilities.push(spv::Capability::Int64); + } + + if features.contains(wgt::Features::SHADER_F16) { + capabilities.push(spv::Capability::Float16); + } + + if features.contains(wgt::Features::SHADER_I16) { + capabilities.push(spv::Capability::Int16); + } + + if features.intersects( + wgt::Features::SHADER_INT64_ATOMIC_ALL_OPS + | wgt::Features::SHADER_INT64_ATOMIC_MIN_MAX + | wgt::Features::TEXTURE_INT64_ATOMIC, + ) { + capabilities.push(spv::Capability::Int64Atomics); + } + + if features.intersects(wgt::Features::TEXTURE_INT64_ATOMIC) { + capabilities.push(spv::Capability::Int64ImageEXT); + } + + if features.contains(wgt::Features::SHADER_FLOAT32_ATOMIC) { + capabilities.push(spv::Capability::AtomicFloat32AddEXT); + } + + if features.contains(wgt::Features::CLIP_DISTANCES) { + capabilities.push(spv::Capability::ClipDistance); + } + + // Vulkan bundles both barycentrics and per-vertex attributes under the same feature. + if features + .intersects(wgt::Features::SHADER_BARYCENTRICS | wgt::Features::SHADER_PER_VERTEX) + { + capabilities.push(spv::Capability::FragmentBarycentricKHR); + } + + if features.contains(wgt::Features::SHADER_DRAW_INDEX) { + capabilities.push(spv::Capability::DrawParameters); + } + + let mut flags = spv::WriterFlags::empty(); + flags.set( + spv::WriterFlags::DEBUG, + self.instance.flags.contains(wgt::InstanceFlags::DEBUG), + ); + flags.set( + spv::WriterFlags::LABEL_VARYINGS, + self.phd_capabilities.properties.vendor_id != crate::auxil::db::qualcomm::VENDOR, + ); + flags.set( + spv::WriterFlags::FORCE_POINT_SIZE, + //Note: we could technically disable this when we are compiling separate entry points, + // and we know exactly that the primitive topology is not `PointList`. + // But this requires cloning the `spv::Options` struct, which has heap allocations. + true, // could check `super::Workarounds::SEPARATE_ENTRY_POINTS` + ); + flags.set( + spv::WriterFlags::PRINT_ON_RAY_QUERY_INITIALIZATION_FAIL + | spv::WriterFlags::PRINT_ON_TRACE_RAYS_FAIL, + self.instance.flags.contains(wgt::InstanceFlags::DEBUG) + && (self.instance.instance_api_version >= vk::API_VERSION_1_3 + || enabled_extensions.contains(&khr::shader_non_semantic_info::NAME)), + ); + if features.contains(wgt::Features::EXPERIMENTAL_RAY_QUERY) { + capabilities.push(spv::Capability::RayQueryKHR); + } + if features.contains(wgt::Features::EXPERIMENTAL_RAY_TRACING_PIPELINES) { + capabilities.push(spv::Capability::RayTracingKHR); + } + if features.contains(wgt::Features::EXPERIMENTAL_RAY_HIT_VERTEX_RETURN) { + capabilities.push(spv::Capability::RayQueryPositionFetchKHR) + } + if features.contains(wgt::Features::EXPERIMENTAL_MESH_SHADER) { + capabilities.push(spv::Capability::MeshShadingEXT); + } + if features.contains(wgt::Features::EXPERIMENTAL_COOPERATIVE_MATRIX) { + capabilities.push(spv::Capability::CooperativeMatrixKHR); + // TODO: expose this more generally + capabilities.push(spv::Capability::VulkanMemoryModel); + } + if self.private_caps.shader_integer_dot_product { + // See <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_shader_integer_dot_product.html#_new_spir_v_capabilities>. + capabilities.extend(&[ + spv::Capability::DotProductInputAllKHR, + spv::Capability::DotProductInput4x8BitKHR, + spv::Capability::DotProductInput4x8BitPackedKHR, + spv::Capability::DotProductKHR, + ]); + } + if self.private_caps.shader_int8 { + // See <https://registry.khronos.org/vulkan/specs/latest/man/html/VkPhysicalDeviceShaderFloat16Int8Features.html#extension-features-shaderInt8>. + capabilities.extend(&[spv::Capability::Int8]); + } + spv::Options { + lang_version: match self.phd_capabilities.device_api_version { + // Use maximum supported SPIR-V version according to + // <https://github.com/KhronosGroup/Vulkan-Docs/blob/19b7651/appendices/spirvenv.adoc?plain=1#L21-L40>. + vk::API_VERSION_1_0..vk::API_VERSION_1_1 => (1, 0), + vk::API_VERSION_1_1..vk::API_VERSION_1_2 => (1, 3), + vk::API_VERSION_1_2..vk::API_VERSION_1_3 => (1, 5), + vk::API_VERSION_1_3.. => (1, 6), + _ => unreachable!(), + }, + flags, + capabilities: Some(capabilities.iter().cloned().collect()), + bounds_check_policies: naga::proc::BoundsCheckPolicies { + index: naga::proc::BoundsCheckPolicy::Restrict, + buffer: if self.private_caps.robust_buffer_access2 { + naga::proc::BoundsCheckPolicy::Unchecked + } else { + naga::proc::BoundsCheckPolicy::Restrict + }, + image_load: if self.private_caps.robust_image_access { + naga::proc::BoundsCheckPolicy::Unchecked + } else { + naga::proc::BoundsCheckPolicy::Restrict + }, + // TODO: support bounds checks on binding arrays + binding_array: naga::proc::BoundsCheckPolicy::Unchecked, + }, + zero_initialize_workgroup_memory: if self + .private_caps + .zero_initialize_workgroup_memory + { + spv::ZeroInitializeWorkgroupMemoryMode::Native + } else { + spv::ZeroInitializeWorkgroupMemoryMode::Polyfill + }, + force_loop_bounding: true, + ray_query_initialization_tracking: true, + use_storage_input_output_16: features.contains(wgt::Features::SHADER_F16) + && self.phd_features.supports_storage_input_output_16(), + fake_missing_bindings: false, + // We need to build this separately for each invocation, so just default it out here + binding_map: BTreeMap::default(), + debug_info: None, + task_dispatch_limits: Some(naga::back::TaskDispatchLimits { + max_mesh_workgroups_per_dim: limits.max_mesh_workgroups_per_dimension, + max_mesh_workgroups_total: limits.max_mesh_workgroup_total_count, + }), + mesh_shader_primitive_indices_clamp: true, + trace_ray_argument_validation: true, + emit_int_div_checks: true, + } + }; + + let raw_queue = { + profiling::scope!("vkGetDeviceQueue"); + unsafe { raw_device.get_device_queue(family_index, queue_index) } + }; + + let driver_version = self + .phd_capabilities + .properties + .driver_version + .to_be_bytes(); + #[rustfmt::skip] + let pipeline_cache_validation_key = [ + driver_version[0], driver_version[1], driver_version[2], driver_version[3], + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + ]; + + let drop_guard = crate::DropGuard::from_option(drop_callback); + + let empty_descriptor_set_layout = unsafe { + raw_device + .create_descriptor_set_layout(&vk::DescriptorSetLayoutCreateInfo::default(), None) + .map_err(super::map_host_device_oom_err)? + }; + + let shared = Arc::new(super::DeviceShared { + raw: raw_device, + family_index, + queue_index, + raw_queue, + drop_guard, + instance: Arc::clone(&self.instance), + physical_device: self.raw, + enabled_extensions: enabled_extensions.into(), + extension_fns: super::DeviceExtensionFunctions { + debug_utils: debug_utils_fn, + draw_indirect_count: indirect_count_fn, + timeline_semaphore: timeline_semaphore_fn, + ray_tracing: ray_tracing_fns, + ray_tracing_pipelines: ray_tracing_pipeline_fns, + mesh_shading: mesh_shading_fns, + external_memory_fd: external_memory_fd_fn, + }, + pipeline_cache_validation_key, + vendor_id: self.phd_capabilities.properties.vendor_id, + timestamp_period: self.phd_capabilities.properties.limits.timestamp_period, + private_caps: self.private_caps.clone(), + features, + workarounds: self.workarounds, + render_passes: Mutex::new(Default::default()), + sampler_cache: Mutex::new(super::sampler::SamplerCache::new( + self.private_caps.maximum_samplers, + )), + memory_allocations_counter: Default::default(), + + texture_identity_factory: super::ResourceIdentityFactory::new(), + texture_view_identity_factory: super::ResourceIdentityFactory::new(), + empty_descriptor_set_layout, + }); + + let relay_semaphores = super::RelaySemaphores::new(&shared)?; + + let queue = super::Queue { + raw: raw_queue, + device: Arc::clone(&shared), + family_index, + relay_semaphores: Mutex::new(relay_semaphores), + signal_semaphores: Mutex::new(SemaphoreList::new(SemaphoreListMode::Signal)), + wait_semaphores: Mutex::new(SemaphoreList::new(SemaphoreListMode::Wait)), + }; + + let allocation_sizes = AllocationSizes::from_memory_hints(memory_hints).into(); + + let buffer_device_address = enabled_extensions.contains(&khr::buffer_device_address::NAME); + + let mem_allocator = + gpu_allocator::vulkan::Allocator::new(&gpu_allocator::vulkan::AllocatorCreateDesc { + instance: self.instance.raw.clone(), + device: shared.raw.clone(), + physical_device: self.raw, + debug_settings: Default::default(), + buffer_device_address, + allocation_sizes, + })?; + + let desc_allocator = super::descriptor::DescriptorAllocator::new( + if let Some(di) = self.phd_capabilities.descriptor_indexing { + di.max_update_after_bind_descriptors_in_all_pools + } else { + 0 + }, + ); + + let device = super::Device { + shared, + mem_allocator: Mutex::new(mem_allocator), + desc_allocator: Mutex::new(desc_allocator), + valid_ash_memory_types, + naga_options, + #[cfg(feature = "renderdoc")] + render_doc: Default::default(), + counters: Default::default(), + }; + + Ok(crate::OpenDevice { device, queue }) + } + + pub fn texture_format_as_raw(&self, texture_format: wgt::TextureFormat) -> vk::Format { + self.private_caps.map_texture_format(texture_format) + } + + /// # Safety: + /// - Same as `open` plus + /// - The callback may not change anything that the device does not support. + /// - The callback may not remove features. + pub unsafe fn open_with_callback<'a>( + &self, + features: wgt::Features, + limits: &wgt::Limits, + memory_hints: &wgt::MemoryHints, + callback: Option<Box<super::CreateDeviceCallback<'a>>>, + ) -> Result<crate::OpenDevice<super::Api>, crate::DeviceError> { + let mut enabled_extensions = self.required_device_extensions(features); + let mut enabled_phd_features = self.physical_device_features(&enabled_extensions, features); + + let family_index = 0; //TODO + let family_info = vk::DeviceQueueCreateInfo::default() + .queue_family_index(family_index) + .queue_priorities(&[1.0]); + let mut family_infos = Vec::from([family_info]); + + let mut pre_info = vk::DeviceCreateInfo::default(); + + if let Some(callback) = callback { + callback(super::CreateDeviceCallbackArgs { + extensions: &mut enabled_extensions, + device_features: &mut enabled_phd_features, + queue_create_infos: &mut family_infos, + create_info: &mut pre_info, + _phantom: PhantomData, + }) + } + + let str_pointers = enabled_extensions + .iter() + .map(|&s| { + // Safe because `enabled_extensions` entries have static lifetime. + s.as_ptr() + }) + .collect::<Vec<_>>(); + + let pre_info = pre_info + .queue_create_infos(&family_infos) + .enabled_extension_names(&str_pointers); + let info = enabled_phd_features.add_to_device_create(pre_info); + let raw_device = { + profiling::scope!("vkCreateDevice"); + unsafe { + self.instance + .raw + .create_device(self.raw, &info, None) + .map_err(map_err)? + } + }; + fn map_err(err: vk::Result) -> crate::DeviceError { + match err { + vk::Result::ERROR_TOO_MANY_OBJECTS => crate::DeviceError::OutOfMemory, + vk::Result::ERROR_INITIALIZATION_FAILED => crate::DeviceError::Lost, + vk::Result::ERROR_EXTENSION_NOT_PRESENT | vk::Result::ERROR_FEATURE_NOT_PRESENT => { + crate::hal_usage_error(err) + } + other => super::map_host_device_oom_and_lost_err(other), + } + } + + unsafe { + self.device_from_raw( + raw_device, + None, + &enabled_extensions, + features, + limits, + memory_hints, + family_info.queue_family_index, + 0, + ) + } + } +} + +impl crate::Adapter for super::Adapter { + type A = super::Api; + + unsafe fn open( + &self, + features: wgt::Features, + limits: &wgt::Limits, + memory_hints: &wgt::MemoryHints, + ) -> Result<crate::OpenDevice<super::Api>, crate::DeviceError> { + unsafe { self.open_with_callback(features, limits, memory_hints, None) } + } + + unsafe fn texture_format_capabilities( + &self, + format: wgt::TextureFormat, + ) -> crate::TextureFormatCapabilities { + use crate::TextureFormatCapabilities as Tfc; + + let vk_format = self.private_caps.map_texture_format(format); + let properties = unsafe { + self.instance + .raw + .get_physical_device_format_properties(self.raw, vk_format) + }; + let features = properties.optimal_tiling_features; + + let mut flags = Tfc::empty(); + flags.set( + Tfc::SAMPLED, + features.contains(vk::FormatFeatureFlags::SAMPLED_IMAGE), + ); + flags.set( + Tfc::SAMPLED_LINEAR, + features.contains(vk::FormatFeatureFlags::SAMPLED_IMAGE_FILTER_LINEAR), + ); + // flags.set( + // Tfc::SAMPLED_MINMAX, + // features.contains(vk::FormatFeatureFlags::SAMPLED_IMAGE_FILTER_MINMAX), + // ); + flags.set( + Tfc::STORAGE_READ_WRITE + | Tfc::STORAGE_WRITE_ONLY + | Tfc::STORAGE_READ_ONLY + | Tfc::STORAGE_ATOMIC, + features.contains(vk::FormatFeatureFlags::STORAGE_IMAGE), + ); + flags.set( + Tfc::STORAGE_ATOMIC, + features.contains(vk::FormatFeatureFlags::STORAGE_IMAGE_ATOMIC), + ); + flags.set( + Tfc::COLOR_ATTACHMENT, + features.contains(vk::FormatFeatureFlags::COLOR_ATTACHMENT), + ); + flags.set( + Tfc::COLOR_ATTACHMENT_BLEND, + features.contains(vk::FormatFeatureFlags::COLOR_ATTACHMENT_BLEND), + ); + flags.set( + Tfc::DEPTH_STENCIL_ATTACHMENT, + features.contains(vk::FormatFeatureFlags::DEPTH_STENCIL_ATTACHMENT), + ); + flags.set( + Tfc::COPY_SRC, + features.intersects(vk::FormatFeatureFlags::TRANSFER_SRC), + ); + flags.set( + Tfc::COPY_DST, + features.intersects(vk::FormatFeatureFlags::TRANSFER_DST), + ); + flags.set( + Tfc::STORAGE_ATOMIC, + features.intersects(vk::FormatFeatureFlags::STORAGE_IMAGE_ATOMIC), + ); + // Vulkan is very permissive about MSAA + flags.set(Tfc::MULTISAMPLE_RESOLVE, !format.is_compressed()); + + // get the supported sample counts + let format_aspect = crate::FormatAspects::from(format); + let limits = self.phd_capabilities.properties.limits; + + let sample_flags = if format_aspect.contains(crate::FormatAspects::DEPTH) { + limits + .framebuffer_depth_sample_counts + .min(limits.sampled_image_depth_sample_counts) + } else if format_aspect.contains(crate::FormatAspects::STENCIL) { + limits + .framebuffer_stencil_sample_counts + .min(limits.sampled_image_stencil_sample_counts) + } else { + let first_aspect = format_aspect + .iter() + .next() + .expect("All texture should at least one aspect") + .map(); + + // We should never get depth or stencil out of this, due to the above. + assert_ne!(first_aspect, wgt::TextureAspect::DepthOnly); + assert_ne!(first_aspect, wgt::TextureAspect::StencilOnly); + + match format.sample_type(Some(first_aspect), None).unwrap() { + wgt::TextureSampleType::Float { .. } => limits + .framebuffer_color_sample_counts + .min(limits.sampled_image_color_sample_counts), + wgt::TextureSampleType::Sint | wgt::TextureSampleType::Uint => { + limits.sampled_image_integer_sample_counts + } + _ => unreachable!(), + } + }; + + flags.set( + Tfc::MULTISAMPLE_X2, + sample_flags.contains(vk::SampleCountFlags::TYPE_2), + ); + flags.set( + Tfc::MULTISAMPLE_X4, + sample_flags.contains(vk::SampleCountFlags::TYPE_4), + ); + flags.set( + Tfc::MULTISAMPLE_X8, + sample_flags.contains(vk::SampleCountFlags::TYPE_8), + ); + flags.set( + Tfc::MULTISAMPLE_X16, + sample_flags.contains(vk::SampleCountFlags::TYPE_16), + ); + + flags + } + + unsafe fn surface_capabilities( + &self, + surface: &super::Surface, + ) -> Option<crate::SurfaceCapabilities> { + surface.inner.surface_capabilities(self) + } + + unsafe fn surface_display_hdr_info( + &self, + surface: &super::Surface, + ) -> Option<wgt::DisplayHdrInfo> { + // Vulkan has no portable luminance query; the Win32 surface reads it + // through DXGI (see `dxgi::hdr`). Every other surface reports `None`. + surface.inner.display_hdr_info() + } + + unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp { + // VK_GOOGLE_display_timing is the only way to get presentation + // timestamps on vulkan right now and it is only ever available + // on android and linux. This includes mac, but there's no alternative + // on mac, so this is fine. + #[cfg(unix)] + { + let mut timespec = libc::timespec::default(); + unsafe { + libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut timespec); + } + + wgt::PresentationTimestamp( + timespec.tv_sec as u128 * 1_000_000_000 + timespec.tv_nsec as u128, + ) + } + #[cfg(not(unix))] + { + wgt::PresentationTimestamp::INVALID_TIMESTAMP + } + } + + fn get_ordered_buffer_usages(&self) -> wgt::BufferUses { + wgt::BufferUses::INCLUSIVE | wgt::BufferUses::MAP_WRITE + } + + // Vulkan makes very few execution ordering guarantees + // see https://registry.khronos.org/vulkan/specs/latest/html/vkspec.html#synchronization-implicit + // We just don't want to insert barriers between inclusive uses + // See https://github.com/gfx-rs/wgpu/issues/8853 + fn get_ordered_texture_usages(&self) -> wgt::TextureUses { + wgt::TextureUses::INCLUSIVE + } +} + +fn is_format_16bit_norm_supported(instance: &ash::Instance, phd: vk::PhysicalDevice) -> bool { + [ + vk::Format::R16_UNORM, + vk::Format::R16_SNORM, + vk::Format::R16G16_UNORM, + vk::Format::R16G16_SNORM, + vk::Format::R16G16B16A16_UNORM, + vk::Format::R16G16B16A16_SNORM, + ] + .into_iter() + .all(|format| { + supports_format( + instance, + phd, + format, + vk::ImageTiling::OPTIMAL, + vk::FormatFeatureFlags::SAMPLED_IMAGE + | vk::FormatFeatureFlags::STORAGE_IMAGE + | vk::FormatFeatureFlags::TRANSFER_SRC + | vk::FormatFeatureFlags::TRANSFER_DST, + ) + }) +} + +fn is_float32_filterable_supported(instance: &ash::Instance, phd: vk::PhysicalDevice) -> bool { + [ + vk::Format::R32_SFLOAT, + vk::Format::R32G32_SFLOAT, + vk::Format::R32G32B32A32_SFLOAT, + ] + .into_iter() + .all(|format| { + supports_format( + instance, + phd, + format, + vk::ImageTiling::OPTIMAL, + vk::FormatFeatureFlags::SAMPLED_IMAGE_FILTER_LINEAR, + ) + }) +} + +fn is_float32_blendable_supported(instance: &ash::Instance, phd: vk::PhysicalDevice) -> bool { + [ + vk::Format::R32_SFLOAT, + vk::Format::R32G32_SFLOAT, + vk::Format::R32G32B32A32_SFLOAT, + ] + .into_iter() + .all(|format| { + supports_format( + instance, + phd, + format, + vk::ImageTiling::OPTIMAL, + vk::FormatFeatureFlags::COLOR_ATTACHMENT_BLEND, + ) + }) +} + +fn supports_format( + instance: &ash::Instance, + phd: vk::PhysicalDevice, + format: vk::Format, + tiling: vk::ImageTiling, + features: vk::FormatFeatureFlags, +) -> bool { + let properties = unsafe { instance.get_physical_device_format_properties(phd, format) }; + match tiling { + vk::ImageTiling::LINEAR => properties.linear_tiling_features.contains(features), + vk::ImageTiling::OPTIMAL => properties.optimal_tiling_features.contains(features), + _ => false, + } +} + +fn supports_astc_3d(instance: &ash::Instance, phd: vk::PhysicalDevice) -> bool { + [ + vk::Format::ASTC_4X4_UNORM_BLOCK, + vk::Format::ASTC_4X4_SRGB_BLOCK, + vk::Format::ASTC_5X4_UNORM_BLOCK, + vk::Format::ASTC_5X4_SRGB_BLOCK, + vk::Format::ASTC_5X5_UNORM_BLOCK, + vk::Format::ASTC_5X5_SRGB_BLOCK, + vk::Format::ASTC_6X5_UNORM_BLOCK, + vk::Format::ASTC_6X5_SRGB_BLOCK, + vk::Format::ASTC_6X6_UNORM_BLOCK, + vk::Format::ASTC_6X6_SRGB_BLOCK, + vk::Format::ASTC_8X5_UNORM_BLOCK, + vk::Format::ASTC_8X5_SRGB_BLOCK, + vk::Format::ASTC_8X6_UNORM_BLOCK, + vk::Format::ASTC_8X6_SRGB_BLOCK, + vk::Format::ASTC_8X8_UNORM_BLOCK, + vk::Format::ASTC_8X8_SRGB_BLOCK, + vk::Format::ASTC_10X5_UNORM_BLOCK, + vk::Format::ASTC_10X5_SRGB_BLOCK, + vk::Format::ASTC_10X6_UNORM_BLOCK, + vk::Format::ASTC_10X6_SRGB_BLOCK, + vk::Format::ASTC_10X8_UNORM_BLOCK, + vk::Format::ASTC_10X8_SRGB_BLOCK, + vk::Format::ASTC_10X10_UNORM_BLOCK, + vk::Format::ASTC_10X10_SRGB_BLOCK, + vk::Format::ASTC_12X10_UNORM_BLOCK, + vk::Format::ASTC_12X10_SRGB_BLOCK, + vk::Format::ASTC_12X12_UNORM_BLOCK, + vk::Format::ASTC_12X12_SRGB_BLOCK, + ] + .into_iter() + .all(|format| { + unsafe { + instance.get_physical_device_image_format_properties( + phd, + format, + vk::ImageType::TYPE_3D, + vk::ImageTiling::OPTIMAL, + vk::ImageUsageFlags::SAMPLED, + vk::ImageCreateFlags::empty(), + ) + } + .is_ok() + }) +} + +fn supports_bgra8unorm_storage( + instance: &ash::Instance, + phd: vk::PhysicalDevice, + device_api_version: u32, +) -> bool { + // See https://github.com/KhronosGroup/Vulkan-Docs/issues/2027#issuecomment-1380608011 + + // This check gates the function call and structures used below. + // TODO: check for (`VK_KHR_get_physical_device_properties2` or VK1.1) and (`VK_KHR_format_feature_flags2` or VK1.3). + // Right now we only check for VK1.3. + if device_api_version < vk::API_VERSION_1_3 { + return false; + } + + unsafe { + let mut properties3 = vk::FormatProperties3::default(); + let mut properties2 = vk::FormatProperties2::default().push_next(&mut properties3); + + instance.get_physical_device_format_properties2( + phd, + vk::Format::B8G8R8A8_UNORM, + &mut properties2, + ); + + let features2 = properties2.format_properties.optimal_tiling_features; + let features3 = properties3.optimal_tiling_features; + + features2.contains(vk::FormatFeatureFlags::STORAGE_IMAGE) + && features3.contains(vk::FormatFeatureFlags2::STORAGE_WRITE_WITHOUT_FORMAT) + } +} + +// For https://github.com/gfx-rs/wgpu/issues/4599 +// Intel iGPUs with outdated drivers can break rendering if `VK_EXT_robustness2` is used. +// Driver version 31.0.101.2115 works, but there's probably an earlier functional version. +fn is_intel_igpu_outdated_for_robustness2(capabilities: &PhysicalDeviceProperties) -> bool { + const DRIVER_VERSION_WORKING: u32 = (101 << 14) | 2115; // X.X.101.2115 + + let props = &capabilities.properties; + + let is_outdated = props.vendor_id == crate::auxil::db::intel::VENDOR + && props.device_type == vk::PhysicalDeviceType::INTEGRATED_GPU + && props.driver_version < DRIVER_VERSION_WORKING + && capabilities.is_driver(vk::DriverId::INTEL_PROPRIETARY_WINDOWS); + + if is_outdated { + log::debug!( + "Disabling robustBufferAccess2 and robustImageAccess2: IntegratedGpu Intel Driver is outdated. Found with version 0x{:X}, less than the known good version 0x{:X} (31.0.101.2115)", + props.driver_version, + DRIVER_VERSION_WORKING + ); + } + is_outdated +} + +/// Convert Vulkan component type to wgt::CooperativeScalarType. +fn map_vk_component_type(ty: vk::ComponentTypeKHR) -> Option<wgt::CooperativeScalarType> { + match ty { + vk::ComponentTypeKHR::FLOAT16 => Some(wgt::CooperativeScalarType::F16), + vk::ComponentTypeKHR::FLOAT32 => Some(wgt::CooperativeScalarType::F32), + vk::ComponentTypeKHR::SINT32 => Some(wgt::CooperativeScalarType::I32), + vk::ComponentTypeKHR::UINT32 => Some(wgt::CooperativeScalarType::U32), + _ => None, + } +} + +/// Convert Vulkan matrix size. +fn map_vk_cooperative_size(size: u32) -> Option<u32> { + match size { + 8 | 16 => Some(size), + _ => None, + } +} + +/// Query all supported cooperative matrix configurations from Vulkan. +fn query_cooperative_matrix_properties( + coop_matrix: &khr::cooperative_matrix::Instance, + phd: vk::PhysicalDevice, +) -> Vec<wgt::CooperativeMatrixProperties> { + let vk_properties = + match unsafe { coop_matrix.get_physical_device_cooperative_matrix_properties(phd) } { + Ok(props) => props, + Err(e) => { + log::warn!("Failed to query cooperative matrix properties: {e:?}"); + return Vec::new(); + } + }; + + log::debug!( + "Vulkan reports {} cooperative matrix configurations", + vk_properties.len() + ); + + let mut result = Vec::new(); + for prop in &vk_properties { + log::debug!( + " Vulkan coop matrix: M={} N={} K={} A={:?} B={:?} C={:?} Result={:?} scope={:?} saturating={}", + prop.m_size, + prop.n_size, + prop.k_size, + prop.a_type, + prop.b_type, + prop.c_type, + prop.result_type, + prop.scope, + prop.saturating_accumulation + ); + + // Only include subgroup-scoped operations (the only scope we support) + if prop.scope != vk::ScopeKHR::SUBGROUP { + log::debug!(" Skipped: scope is not SUBGROUP"); + continue; + } + + // Map sizes - skip configurations with sizes we don't support + let m_size = match map_vk_cooperative_size(prop.m_size) { + Some(s) => s, + None => { + log::debug!(" Skipped: M size {} not supported", prop.m_size); + continue; + } + }; + let n_size = match map_vk_cooperative_size(prop.n_size) { + Some(s) => s, + None => { + log::debug!(" Skipped: N size {} not supported", prop.n_size); + continue; + } + }; + let k_size = match map_vk_cooperative_size(prop.k_size) { + Some(s) => s, + None => { + log::debug!(" Skipped: K size {} not supported", prop.k_size); + continue; + } + }; + + // Map the component types - A and B must match, C and Result must match + let ab_type = match map_vk_component_type(prop.a_type) { + Some(t) if Some(t) == map_vk_component_type(prop.b_type) => t, + _ => { + log::debug!( + " Skipped: A/B types {:?}/{:?} not supported or don't match", + prop.a_type, + prop.b_type + ); + continue; + } + }; + let cr_type = match map_vk_component_type(prop.c_type) { + Some(t) if Some(t) == map_vk_component_type(prop.result_type) => t, + _ => { + log::debug!( + " Skipped: C/Result types {:?}/{:?} not supported or don't match", + prop.c_type, + prop.result_type + ); + continue; + } + }; + + log::debug!(" Accepted!"); + result.push(wgt::CooperativeMatrixProperties { + m_size, + n_size, + k_size, + ab_type, + cr_type, + saturating_accumulation: prop.saturating_accumulation != 0, + }); + } + + log::info!( + "Found {} cooperative matrix configurations supported by wgpu", + result.len() + ); + result +} diff --git a/third_party/wgpu-hal/src/vulkan/command.rs b/third_party/wgpu-hal/src/vulkan/command.rs new file mode 100644 index 000000000..3ea853e67 --- /dev/null +++ b/third_party/wgpu-hal/src/vulkan/command.rs @@ -0,0 +1,1495 @@ +use super::conv; +use arrayvec::ArrayVec; +use ash::vk; +use core::{mem, ops::Range}; +use hashbrown::hash_map::Entry; + +const ALLOCATION_GRANULARITY: u32 = 16; +const DST_IMAGE_LAYOUT: vk::ImageLayout = vk::ImageLayout::TRANSFER_DST_OPTIMAL; + +impl super::Texture { + fn map_buffer_copies<T>(&self, regions: T) -> impl Iterator<Item = vk::BufferImageCopy> + where + T: Iterator<Item = crate::BufferTextureCopy>, + { + let (block_width, block_height) = self.format.block_dimensions(); + let format = self.format; + let copy_size = self.copy_size; + regions.map(move |r| { + let extent = r.texture_base.max_copy_size(©_size).min(&r.size); + let (image_subresource, image_offset) = conv::map_subresource_layers(&r.texture_base); + vk::BufferImageCopy { + buffer_offset: r.buffer_layout.offset, + buffer_row_length: r.buffer_layout.bytes_per_row.map_or(0, |bpr| { + let block_size = format + .block_copy_size(Some(r.texture_base.aspect.map())) + .unwrap(); + block_width * (bpr / block_size) + }), + buffer_image_height: r + .buffer_layout + .rows_per_image + .map_or(0, |rpi| rpi * block_height), + image_subresource, + image_offset, + image_extent: conv::map_copy_extent(&extent), + } + }) + } +} + +impl super::CommandEncoder { + fn write_pass_end_timestamp_if_requested(&mut self) { + if let Some((query_set, index)) = self.end_of_pass_timer_query.take() { + unsafe { + self.device.raw.cmd_write_timestamp( + self.active, + vk::PipelineStageFlags::BOTTOM_OF_PIPE, + query_set, + index, + ); + } + } + } + + fn make_framebuffer( + &mut self, + key: super::FramebufferKey, + ) -> Result<vk::Framebuffer, crate::DeviceError> { + Ok(match self.framebuffers.entry(key) { + Entry::Occupied(e) => *e.get(), + Entry::Vacant(e) => { + let super::FramebufferKey { + raw_pass, + ref attachment_views, + attachment_identities: _, + extent, + } = *e.key(); + + let vk_info = vk::FramebufferCreateInfo::default() + .render_pass(raw_pass) + .width(extent.width) + .height(extent.height) + .layers(extent.depth_or_array_layers) + .attachments(attachment_views); + + let raw = unsafe { self.device.raw.create_framebuffer(&vk_info, None).unwrap() }; + *e.insert(raw) + } + }) + } + + fn make_temp_texture_view( + &mut self, + key: super::TempTextureViewKey, + ) -> Result<super::IdentifiedTextureView, crate::DeviceError> { + Ok(match self.temp_texture_views.entry(key) { + Entry::Occupied(e) => *e.get(), + Entry::Vacant(e) => { + let super::TempTextureViewKey { + texture, + texture_identity: _, + format, + mip_level, + depth_slice, + } = *e.key(); + + let vk_info = vk::ImageViewCreateInfo::default() + .image(texture) + .view_type(vk::ImageViewType::TYPE_2D) + .format(format) + .subresource_range(vk::ImageSubresourceRange { + aspect_mask: vk::ImageAspectFlags::COLOR, + base_mip_level: mip_level, + level_count: 1, + base_array_layer: depth_slice, + layer_count: 1, + }); + let raw = unsafe { self.device.raw.create_image_view(&vk_info, None) } + .map_err(super::map_host_device_oom_and_ioca_err)?; + + let identity = self.device.texture_view_identity_factory.next(); + + *e.insert(super::IdentifiedTextureView { raw, identity }) + } + }) + } +} + +impl crate::CommandEncoder for super::CommandEncoder { + type A = super::Api; + + unsafe fn begin_encoding(&mut self, label: crate::Label) -> Result<(), crate::DeviceError> { + if self.free.is_empty() { + let vk_info = vk::CommandBufferAllocateInfo::default() + .command_pool(self.raw) + .command_buffer_count(ALLOCATION_GRANULARITY); + let cmd_buf_vec = unsafe { + self.device + .raw + .allocate_command_buffers(&vk_info) + .map_err(super::map_host_device_oom_err)? + }; + self.free.extend(cmd_buf_vec); + } + let raw = self.free.pop().unwrap(); + + // Set the name unconditionally, since there might be a + // previous name assigned to this. + unsafe { self.device.set_object_name(raw, label.unwrap_or_default()) }; + + // Reset some state in case the last renderpass was never ended. + self.rpass_debug_marker_active = false; + self.end_of_pass_timer_query = None; + + let vk_info = vk::CommandBufferBeginInfo::default() + .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT); + unsafe { self.device.raw.begin_command_buffer(raw, &vk_info) } + .map_err(super::map_host_device_oom_err)?; + self.active = raw; + + Ok(()) + } + + unsafe fn end_encoding(&mut self) -> Result<super::CommandBuffer, crate::DeviceError> { + let raw = self.active; + self.active = vk::CommandBuffer::null(); + unsafe { self.device.raw.end_command_buffer(raw) }.map_err(map_err)?; + fn map_err(err: vk::Result) -> crate::DeviceError { + // We don't use VK_KHR_video_encode_queue + // VK_ERROR_INVALID_VIDEO_STD_PARAMETERS_KHR + super::map_host_device_oom_err(err) + } + Ok(super::CommandBuffer { raw }) + } + + unsafe fn discard_encoding(&mut self) { + // Safe use requires this is not called in the "closed" state, so the buffer + // shouldn't be null. Assert this to make sure we're not pushing null + // buffers to the discard pile. + assert_ne!(self.active, vk::CommandBuffer::null()); + + self.discarded.push(self.active); + self.active = vk::CommandBuffer::null(); + } + + unsafe fn reset_all<I>(&mut self, cmd_bufs: I) + where + I: Iterator<Item = super::CommandBuffer>, + { + self.temp.clear(); + self.free + .extend(cmd_bufs.into_iter().map(|cmd_buf| cmd_buf.raw)); + self.free.append(&mut self.discarded); + // Delete framebuffers from the framebuffer cache + for (_, framebuffer) in self.framebuffers.drain() { + unsafe { self.device.raw.destroy_framebuffer(framebuffer, None) }; + } + let _ = unsafe { + self.device + .raw + .reset_command_pool(self.raw, vk::CommandPoolResetFlags::default()) + }; + } + + unsafe fn transition_buffers<'a, T>(&mut self, barriers: T) + where + T: Iterator<Item = crate::BufferBarrier<'a, super::Buffer>>, + { + //Note: this is done so that we never end up with empty stage flags + let mut src_stages = vk::PipelineStageFlags::TOP_OF_PIPE; + let mut dst_stages = vk::PipelineStageFlags::BOTTOM_OF_PIPE; + let vk_barriers = &mut self.temp.buffer_barriers; + vk_barriers.clear(); + + for bar in barriers { + let (src_stage, src_access) = conv::map_buffer_usage_to_barrier(bar.usage.from); + src_stages |= src_stage; + let (dst_stage, dst_access) = conv::map_buffer_usage_to_barrier(bar.usage.to); + dst_stages |= dst_stage; + + vk_barriers.push( + vk::BufferMemoryBarrier::default() + .buffer(bar.buffer.raw) + .size(vk::WHOLE_SIZE) + .src_access_mask(src_access) + .dst_access_mask(dst_access), + ) + } + + if !vk_barriers.is_empty() { + unsafe { + self.device.raw.cmd_pipeline_barrier( + self.active, + src_stages, + dst_stages, + vk::DependencyFlags::empty(), + &[], + vk_barriers, + &[], + ) + }; + } + } + + unsafe fn transition_textures<'a, T>(&mut self, barriers: T) + where + T: Iterator<Item = crate::TextureBarrier<'a, super::Texture>>, + { + let mut src_stages = vk::PipelineStageFlags::empty(); + let mut dst_stages = vk::PipelineStageFlags::empty(); + let vk_barriers = &mut self.temp.image_barriers; + vk_barriers.clear(); + + for bar in barriers { + let range = conv::map_subresource_range_combined_aspect( + &bar.range, + bar.texture.format, + &self.device.private_caps, + ); + let (src_stage, src_access) = conv::map_texture_usage_to_barrier(bar.usage.from); + let src_layout = conv::derive_image_layout(bar.usage.from, bar.texture.format); + src_stages |= src_stage; + let (dst_stage, dst_access) = conv::map_texture_usage_to_barrier(bar.usage.to); + let dst_layout = conv::derive_image_layout(bar.usage.to, bar.texture.format); + dst_stages |= dst_stage; + + vk_barriers.push( + vk::ImageMemoryBarrier::default() + .image(bar.texture.raw) + .subresource_range(range) + .src_access_mask(src_access) + .dst_access_mask(dst_access) + .old_layout(src_layout) + .new_layout(dst_layout), + ); + } + + if !vk_barriers.is_empty() { + unsafe { + self.device.raw.cmd_pipeline_barrier( + self.active, + src_stages, + dst_stages, + vk::DependencyFlags::empty(), + &[], + &[], + vk_barriers, + ) + }; + } + } + + unsafe fn clear_buffer(&mut self, buffer: &super::Buffer, range: crate::MemoryRange) { + let range_size = range.end - range.start; + if self.device.workarounds.contains( + super::Workarounds::FORCE_FILL_BUFFER_WITH_SIZE_GREATER_4096_ALIGNED_OFFSET_16, + ) && range_size >= 4096 + && !range.start.is_multiple_of(16) + { + let rounded_start = wgt::math::align_to(range.start, 16); + let prefix_size = rounded_start - range.start; + + unsafe { + self.device.raw.cmd_fill_buffer( + self.active, + buffer.raw, + range.start, + prefix_size, + 0, + ) + }; + + // This will never be zero, as rounding can only add up to 12 bytes, and the total size is 4096. + let suffix_size = range.end - rounded_start; + + unsafe { + self.device.raw.cmd_fill_buffer( + self.active, + buffer.raw, + rounded_start, + suffix_size, + 0, + ) + }; + } else { + unsafe { + self.device + .raw + .cmd_fill_buffer(self.active, buffer.raw, range.start, range_size, 0) + }; + } + } + + unsafe fn copy_buffer_to_buffer<T>( + &mut self, + src: &super::Buffer, + dst: &super::Buffer, + regions: T, + ) where + T: Iterator<Item = crate::BufferCopy>, + { + let vk_regions_iter = regions.map(|r| vk::BufferCopy { + src_offset: r.src_offset, + dst_offset: r.dst_offset, + size: r.size.get(), + }); + + unsafe { + self.device.raw.cmd_copy_buffer( + self.active, + src.raw, + dst.raw, + &smallvec::SmallVec::<[vk::BufferCopy; 32]>::from_iter(vk_regions_iter), + ) + }; + } + + unsafe fn copy_texture_to_texture<T>( + &mut self, + src: &super::Texture, + src_usage: wgt::TextureUses, + dst: &super::Texture, + regions: T, + ) where + T: Iterator<Item = crate::TextureCopy>, + { + let src_layout = conv::derive_image_layout(src_usage, src.format); + + let vk_regions_iter = regions.map(|r| { + let (src_subresource, src_offset) = conv::map_subresource_layers(&r.src_base); + let (dst_subresource, dst_offset) = conv::map_subresource_layers(&r.dst_base); + let extent = r + .size + .min(&r.src_base.max_copy_size(&src.copy_size)) + .min(&r.dst_base.max_copy_size(&dst.copy_size)); + vk::ImageCopy { + src_subresource, + src_offset, + dst_subresource, + dst_offset, + extent: conv::map_copy_extent(&extent), + } + }); + + unsafe { + self.device.raw.cmd_copy_image( + self.active, + src.raw, + src_layout, + dst.raw, + DST_IMAGE_LAYOUT, + &smallvec::SmallVec::<[vk::ImageCopy; 32]>::from_iter(vk_regions_iter), + ) + }; + } + + unsafe fn copy_buffer_to_texture<T>( + &mut self, + src: &super::Buffer, + dst: &super::Texture, + regions: T, + ) where + T: Iterator<Item = crate::BufferTextureCopy>, + { + let vk_regions_iter = dst.map_buffer_copies(regions); + + unsafe { + self.device.raw.cmd_copy_buffer_to_image( + self.active, + src.raw, + dst.raw, + DST_IMAGE_LAYOUT, + &smallvec::SmallVec::<[vk::BufferImageCopy; 32]>::from_iter(vk_regions_iter), + ) + }; + } + + unsafe fn copy_texture_to_buffer<T>( + &mut self, + src: &super::Texture, + src_usage: wgt::TextureUses, + dst: &super::Buffer, + regions: T, + ) where + T: Iterator<Item = crate::BufferTextureCopy>, + { + let src_layout = conv::derive_image_layout(src_usage, src.format); + let vk_regions_iter = src.map_buffer_copies(regions); + + unsafe { + self.device.raw.cmd_copy_image_to_buffer( + self.active, + src.raw, + src_layout, + dst.raw, + &smallvec::SmallVec::<[vk::BufferImageCopy; 32]>::from_iter(vk_regions_iter), + ) + }; + } + + unsafe fn begin_query(&mut self, set: &super::QuerySet, index: u32) { + unsafe { + self.device.raw.cmd_begin_query( + self.active, + set.raw, + index, + vk::QueryControlFlags::empty(), + ) + }; + } + unsafe fn end_query(&mut self, set: &super::QuerySet, index: u32) { + unsafe { self.device.raw.cmd_end_query(self.active, set.raw, index) }; + } + unsafe fn write_timestamp(&mut self, set: &super::QuerySet, index: u32) { + unsafe { + self.device.raw.cmd_write_timestamp( + self.active, + vk::PipelineStageFlags::BOTTOM_OF_PIPE, + set.raw, + index, + ) + }; + } + unsafe fn read_acceleration_structure_compact_size( + &mut self, + acceleration_structure: &super::AccelerationStructure, + buffer: &super::Buffer, + ) { + let ray_tracing_functions = self + .device + .extension_fns + .ray_tracing + .as_ref() + .expect("Feature `RAY_TRACING` not enabled"); + let query_pool = acceleration_structure + .compacted_size_query + .as_ref() + .unwrap(); + unsafe { + self.device + .raw + .cmd_reset_query_pool(self.active, *query_pool, 0, 1); + ray_tracing_functions + .acceleration_structure + .cmd_write_acceleration_structures_properties( + self.active, + &[acceleration_structure.raw], + vk::QueryType::ACCELERATION_STRUCTURE_COMPACTED_SIZE_KHR, + *query_pool, + 0, + ); + self.device.raw.cmd_copy_query_pool_results( + self.active, + *query_pool, + 0, + 1, + buffer.raw, + 0, + wgt::QUERY_SIZE as vk::DeviceSize, + vk::QueryResultFlags::TYPE_64 | vk::QueryResultFlags::WAIT, + ) + }; + } + unsafe fn reset_queries(&mut self, set: &super::QuerySet, range: Range<u32>) { + unsafe { + self.device.raw.cmd_reset_query_pool( + self.active, + set.raw, + range.start, + range.end - range.start, + ) + }; + } + unsafe fn copy_query_results( + &mut self, + set: &super::QuerySet, + range: Range<u32>, + buffer: &super::Buffer, + offset: wgt::BufferAddress, + stride: wgt::BufferSize, + ) { + unsafe { + self.device.raw.cmd_copy_query_pool_results( + self.active, + set.raw, + range.start, + range.end - range.start, + buffer.raw, + offset, + stride.get(), + vk::QueryResultFlags::TYPE_64 | vk::QueryResultFlags::WAIT, + ) + }; + } + + unsafe fn build_acceleration_structures<'a, T>(&mut self, descriptor_count: u32, descriptors: T) + where + super::Api: 'a, + T: IntoIterator< + Item = crate::BuildAccelerationStructureDescriptor< + 'a, + super::Buffer, + super::AccelerationStructure, + >, + >, + { + const CAPACITY_OUTER: usize = 8; + const CAPACITY_INNER: usize = 1; + let descriptor_count = descriptor_count as usize; + + let ray_tracing_functions = self + .device + .extension_fns + .ray_tracing + .as_ref() + .expect("Feature `RAY_TRACING` not enabled"); + + let get_device_address = |buffer: Option<&super::Buffer>| unsafe { + match buffer { + Some(buffer) => ray_tracing_functions + .buffer_device_address + .get_buffer_device_address( + &vk::BufferDeviceAddressInfo::default().buffer(buffer.raw), + ), + None => panic!("Buffers are required to build acceleration structures"), + } + }; + + // storage to all the data required for cmd_build_acceleration_structures + let mut ranges_storage = smallvec::SmallVec::< + [smallvec::SmallVec<[vk::AccelerationStructureBuildRangeInfoKHR; CAPACITY_INNER]>; + CAPACITY_OUTER], + >::with_capacity(descriptor_count); + let mut geometries_storage = smallvec::SmallVec::< + [smallvec::SmallVec<[vk::AccelerationStructureGeometryKHR; CAPACITY_INNER]>; + CAPACITY_OUTER], + >::with_capacity(descriptor_count); + + // pointers to all the data required for cmd_build_acceleration_structures + let mut geometry_infos = smallvec::SmallVec::< + [vk::AccelerationStructureBuildGeometryInfoKHR; CAPACITY_OUTER], + >::with_capacity(descriptor_count); + let mut ranges_ptrs = smallvec::SmallVec::< + [&[vk::AccelerationStructureBuildRangeInfoKHR]; CAPACITY_OUTER], + >::with_capacity(descriptor_count); + + for desc in descriptors { + let (geometries, ranges) = match *desc.entries { + crate::AccelerationStructureEntries::Instances(ref instances) => { + let instance_data = vk::AccelerationStructureGeometryInstancesDataKHR::default( + // TODO: Code is so large that rustfmt refuses to treat this... :( + ) + .data(vk::DeviceOrHostAddressConstKHR { + device_address: get_device_address(instances.buffer), + }); + + let geometry = vk::AccelerationStructureGeometryKHR::default() + .geometry_type(vk::GeometryTypeKHR::INSTANCES) + .geometry(vk::AccelerationStructureGeometryDataKHR { + instances: instance_data, + }); + + let range = vk::AccelerationStructureBuildRangeInfoKHR::default() + .primitive_count(instances.count) + .primitive_offset(instances.offset); + + (smallvec::smallvec![geometry], smallvec::smallvec![range]) + } + crate::AccelerationStructureEntries::Triangles(ref in_geometries) => { + let mut ranges = smallvec::SmallVec::< + [vk::AccelerationStructureBuildRangeInfoKHR; CAPACITY_INNER], + >::with_capacity(in_geometries.len()); + let mut geometries = smallvec::SmallVec::< + [vk::AccelerationStructureGeometryKHR; CAPACITY_INNER], + >::with_capacity(in_geometries.len()); + for triangles in in_geometries { + let mut triangle_data = + vk::AccelerationStructureGeometryTrianglesDataKHR::default() + // IndexType::NONE_KHR is not set by default (due to being provided by VK_KHR_acceleration_structure) but unless there is an + // index buffer we need to have IndexType::NONE_KHR as our index type. + .index_type(vk::IndexType::NONE_KHR) + .vertex_data(vk::DeviceOrHostAddressConstKHR { + device_address: get_device_address(triangles.vertex_buffer) + + (triangles.first_vertex as u64 * triangles.vertex_stride), + }) + .vertex_format(conv::map_vertex_format(triangles.vertex_format)) + .max_vertex(triangles.vertex_count) + .vertex_stride(triangles.vertex_stride); + + let mut range = vk::AccelerationStructureBuildRangeInfoKHR::default(); + + if let Some(ref indices) = triangles.indices { + triangle_data = triangle_data + .index_data(vk::DeviceOrHostAddressConstKHR { + device_address: get_device_address(indices.buffer), + }) + .index_type(conv::map_index_format(indices.format)); + + range = range + .primitive_count(indices.count / 3) + .primitive_offset(indices.offset); + } else { + range = range.primitive_count(triangles.vertex_count / 3); + } + + if let Some(ref transform) = triangles.transform { + let transform_device_address = unsafe { + ray_tracing_functions + .buffer_device_address + .get_buffer_device_address( + &vk::BufferDeviceAddressInfo::default() + .buffer(transform.buffer.raw), + ) + }; + triangle_data = + triangle_data.transform_data(vk::DeviceOrHostAddressConstKHR { + device_address: transform_device_address, + }); + + range = range.transform_offset(transform.offset); + } + + let geometry = vk::AccelerationStructureGeometryKHR::default() + .geometry_type(vk::GeometryTypeKHR::TRIANGLES) + .geometry(vk::AccelerationStructureGeometryDataKHR { + triangles: triangle_data, + }) + .flags(conv::map_acceleration_structure_geometry_flags( + triangles.flags, + )); + + geometries.push(geometry); + ranges.push(range); + } + (geometries, ranges) + } + crate::AccelerationStructureEntries::AABBs(ref in_geometries) => { + let mut ranges = smallvec::SmallVec::< + [vk::AccelerationStructureBuildRangeInfoKHR; CAPACITY_INNER], + >::with_capacity(in_geometries.len()); + let mut geometries = smallvec::SmallVec::< + [vk::AccelerationStructureGeometryKHR; CAPACITY_INNER], + >::with_capacity(in_geometries.len()); + for aabb in in_geometries { + let aabbs_data = vk::AccelerationStructureGeometryAabbsDataKHR::default() + .data(vk::DeviceOrHostAddressConstKHR { + device_address: get_device_address(aabb.buffer), + }) + .stride(aabb.stride); + + let range = vk::AccelerationStructureBuildRangeInfoKHR::default() + .primitive_count(aabb.count) + .primitive_offset(aabb.offset); + + let geometry = vk::AccelerationStructureGeometryKHR::default() + .geometry_type(vk::GeometryTypeKHR::AABBS) + .geometry(vk::AccelerationStructureGeometryDataKHR { + aabbs: aabbs_data, + }) + .flags(conv::map_acceleration_structure_geometry_flags(aabb.flags)); + + geometries.push(geometry); + ranges.push(range); + } + (geometries, ranges) + } + }; + + ranges_storage.push(ranges); + geometries_storage.push(geometries); + + let scratch_device_address = unsafe { + ray_tracing_functions + .buffer_device_address + .get_buffer_device_address( + &vk::BufferDeviceAddressInfo::default().buffer(desc.scratch_buffer.raw), + ) + }; + let ty = match *desc.entries { + crate::AccelerationStructureEntries::Instances(_) => { + vk::AccelerationStructureTypeKHR::TOP_LEVEL + } + _ => vk::AccelerationStructureTypeKHR::BOTTOM_LEVEL, + }; + let mut geometry_info = vk::AccelerationStructureBuildGeometryInfoKHR::default() + .ty(ty) + .mode(conv::map_acceleration_structure_build_mode(desc.mode)) + .flags(conv::map_acceleration_structure_flags(desc.flags)) + .dst_acceleration_structure(desc.destination_acceleration_structure.raw) + .scratch_data(vk::DeviceOrHostAddressKHR { + device_address: scratch_device_address + desc.scratch_buffer_offset, + }); + + if desc.mode == crate::AccelerationStructureBuildMode::Update { + geometry_info.src_acceleration_structure = desc + .source_acceleration_structure + .unwrap_or(desc.destination_acceleration_structure) + .raw; + } + + geometry_infos.push(geometry_info); + } + + for (i, geometry_info) in geometry_infos.iter_mut().enumerate() { + geometry_info.geometry_count = geometries_storage[i].len() as u32; + geometry_info.p_geometries = geometries_storage[i].as_ptr(); + ranges_ptrs.push(&ranges_storage[i]); + } + + unsafe { + ray_tracing_functions + .acceleration_structure + .cmd_build_acceleration_structures(self.active, &geometry_infos, &ranges_ptrs); + } + } + + unsafe fn place_acceleration_structure_barrier( + &mut self, + barrier: crate::AccelerationStructureBarrier, + ) { + let (src_stage, src_access) = conv::map_acceleration_structure_usage_to_barrier( + barrier.usage.from, + self.device.features, + ); + let (dst_stage, dst_access) = conv::map_acceleration_structure_usage_to_barrier( + barrier.usage.to, + self.device.features, + ); + + unsafe { + self.device.raw.cmd_pipeline_barrier( + self.active, + src_stage | vk::PipelineStageFlags::TOP_OF_PIPE, + dst_stage | vk::PipelineStageFlags::BOTTOM_OF_PIPE, + vk::DependencyFlags::empty(), + &[vk::MemoryBarrier::default() + .src_access_mask(src_access) + .dst_access_mask(dst_access)], + &[], + &[], + ) + }; + } + + unsafe fn set_acceleration_structure_dependencies( + _command_buffers: &[&super::CommandBuffer], + _dependencies: &[&super::AccelerationStructure], + ) { + } + // render + + unsafe fn begin_render_pass( + &mut self, + desc: &crate::RenderPassDescriptor<super::QuerySet, super::TextureView>, + ) -> Result<(), crate::DeviceError> { + let mut vk_clear_values = + ArrayVec::<vk::ClearValue, { super::MAX_TOTAL_ATTACHMENTS }>::new(); + let mut rp_key = super::RenderPassKey { + colors: ArrayVec::default(), + depth_stencil: None, + sample_count: desc.sample_count, + multiview_mask: desc.multiview_mask, + }; + let mut fb_key = super::FramebufferKey { + raw_pass: vk::RenderPass::null(), + attachment_views: ArrayVec::default(), + attachment_identities: ArrayVec::default(), + extent: desc.extent, + }; + + for cat in desc.color_attachments { + if let Some(cat) = cat.as_ref() { + let color_view = if cat.target.view.dimension == wgt::TextureViewDimension::D3 { + let key = super::TempTextureViewKey { + texture: cat.target.view.raw_texture, + texture_identity: cat.target.view.texture_identity, + format: cat.target.view.raw_format, + mip_level: cat.target.view.base_mip_level, + depth_slice: cat.depth_slice.unwrap(), + }; + self.make_temp_texture_view(key)? + } else { + cat.target.view.identified_raw_view() + }; + + vk_clear_values.push(vk::ClearValue { + color: unsafe { cat.make_vk_clear_color() }, + }); + let color = super::ColorAttachmentKey { + base: cat.target.make_attachment_key(cat.ops), + resolve: cat.resolve_target.as_ref().map(|target| { + target.make_attachment_key( + crate::AttachmentOps::LOAD_CLEAR | crate::AttachmentOps::STORE, + ) + }), + }; + + rp_key.colors.push(Some(color)); + fb_key.push_view(color_view); + if let Some(ref at) = cat.resolve_target { + vk_clear_values.push(unsafe { mem::zeroed() }); + fb_key.push_view(at.view.identified_raw_view()); + } + } else { + rp_key.colors.push(None); + } + } + if let Some(ref ds) = desc.depth_stencil_attachment { + vk_clear_values.push(vk::ClearValue { + depth_stencil: vk::ClearDepthStencilValue { + depth: ds.clear_value.0, + stencil: ds.clear_value.1, + }, + }); + rp_key.depth_stencil = Some(super::DepthStencilAttachmentKey { + base: ds.target.make_attachment_key(ds.depth_ops), + stencil_ops: ds.stencil_ops, + }); + fb_key.push_view(ds.target.view.identified_raw_view()); + } + + let render_area = vk::Rect2D { + offset: vk::Offset2D { x: 0, y: 0 }, + extent: vk::Extent2D { + width: desc.extent.width, + height: desc.extent.height, + }, + }; + let vk_viewports = [vk::Viewport { + x: 0.0, + y: desc.extent.height as f32, + width: desc.extent.width as f32, + height: -(desc.extent.height as f32), + min_depth: 0.0, + max_depth: 1.0, + }]; + + let raw_pass = self.device.make_render_pass(rp_key).unwrap(); + fb_key.raw_pass = raw_pass; + let raw_framebuffer = self.make_framebuffer(fb_key).unwrap(); + + let vk_info = vk::RenderPassBeginInfo::default() + .render_pass(raw_pass) + .render_area(render_area) + .clear_values(&vk_clear_values) + .framebuffer(raw_framebuffer); + + if let Some(label) = desc.label { + unsafe { self.begin_debug_marker(label) }; + self.rpass_debug_marker_active = true; + } + + // Start timestamp if any (before all other commands but after debug marker) + if let Some(timestamp_writes) = desc.timestamp_writes.as_ref() { + if let Some(index) = timestamp_writes.beginning_of_pass_write_index { + unsafe { + self.write_timestamp(timestamp_writes.query_set, index); + } + } + self.end_of_pass_timer_query = timestamp_writes + .end_of_pass_write_index + .map(|index| (timestamp_writes.query_set.raw, index)); + } + + unsafe { + self.device + .raw + .cmd_set_viewport(self.active, 0, &vk_viewports); + self.device + .raw + .cmd_set_scissor(self.active, 0, &[render_area]); + self.device.raw.cmd_begin_render_pass( + self.active, + &vk_info, + vk::SubpassContents::INLINE, + ); + }; + + self.bind_point = vk::PipelineBindPoint::GRAPHICS; + + Ok(()) + } + unsafe fn end_render_pass(&mut self) { + unsafe { + self.device.raw.cmd_end_render_pass(self.active); + } + + // After all other commands but before debug marker, so this is still seen as part of this pass. + self.write_pass_end_timestamp_if_requested(); + + if self.rpass_debug_marker_active { + unsafe { + self.end_debug_marker(); + } + self.rpass_debug_marker_active = false; + } + } + + unsafe fn set_bind_group( + &mut self, + layout: &super::PipelineLayout, + index: u32, + group: &super::BindGroup, + dynamic_offsets: &[wgt::DynamicOffset], + ) { + let sets = [group.set.raw()]; + unsafe { + self.device.raw.cmd_bind_descriptor_sets( + self.active, + self.bind_point, + layout.raw, + index, + &sets, + dynamic_offsets, + ) + }; + } + unsafe fn set_immediates( + &mut self, + layout: &super::PipelineLayout, + offset_bytes: u32, + data: &[u32], + ) { + unsafe { + self.device.raw.cmd_push_constants( + self.active, + layout.raw, + vk::ShaderStageFlags::ALL, + offset_bytes, + bytemuck::cast_slice(data), + ) + }; + } + + unsafe fn insert_debug_marker(&mut self, label: &str) { + if let Some(ext) = self.device.extension_fns.debug_utils.as_ref() { + let cstr = self.temp.make_c_str(label); + let vk_label = vk::DebugUtilsLabelEXT::default().label_name(cstr); + unsafe { ext.cmd_insert_debug_utils_label(self.active, &vk_label) }; + } + } + unsafe fn begin_debug_marker(&mut self, group_label: &str) { + if let Some(ext) = self.device.extension_fns.debug_utils.as_ref() { + let cstr = self.temp.make_c_str(group_label); + let vk_label = vk::DebugUtilsLabelEXT::default().label_name(cstr); + unsafe { ext.cmd_begin_debug_utils_label(self.active, &vk_label) }; + } + } + unsafe fn end_debug_marker(&mut self) { + if let Some(ext) = self.device.extension_fns.debug_utils.as_ref() { + unsafe { ext.cmd_end_debug_utils_label(self.active) }; + } + } + + unsafe fn set_render_pipeline(&mut self, pipeline: &super::RenderPipeline) { + unsafe { + self.current_pipeline_is_multiview = pipeline.is_multiview; + self.device.raw.cmd_bind_pipeline( + self.active, + vk::PipelineBindPoint::GRAPHICS, + pipeline.raw, + ) + }; + } + + unsafe fn set_index_buffer<'a>( + &mut self, + binding: crate::BufferBinding<'a, super::Buffer>, + format: wgt::IndexFormat, + ) { + unsafe { + self.device.raw.cmd_bind_index_buffer( + self.active, + binding.buffer.raw, + binding.offset, + conv::map_index_format(format), + ) + }; + } + unsafe fn set_vertex_buffer<'a>( + &mut self, + index: u32, + binding: crate::BufferBinding<'a, super::Buffer>, + ) { + let vk_buffers = [binding.buffer.raw]; + let vk_offsets = [binding.offset]; + unsafe { + self.device + .raw + .cmd_bind_vertex_buffers(self.active, index, &vk_buffers, &vk_offsets) + }; + } + unsafe fn set_viewport(&mut self, rect: &crate::Rect<f32>, depth_range: Range<f32>) { + let vk_viewports = [vk::Viewport { + x: rect.x, + y: rect.y + rect.h, + width: rect.w, + height: -rect.h, // flip Y + min_depth: depth_range.start, + max_depth: depth_range.end, + }]; + unsafe { + self.device + .raw + .cmd_set_viewport(self.active, 0, &vk_viewports) + }; + } + unsafe fn set_scissor_rect(&mut self, rect: &crate::Rect<u32>) { + let vk_scissors = [vk::Rect2D { + offset: vk::Offset2D { + x: rect.x as i32, + y: rect.y as i32, + }, + extent: vk::Extent2D { + width: rect.w, + height: rect.h, + }, + }]; + unsafe { + self.device + .raw + .cmd_set_scissor(self.active, 0, &vk_scissors) + }; + } + unsafe fn set_stencil_reference(&mut self, value: u32) { + unsafe { + self.device.raw.cmd_set_stencil_reference( + self.active, + vk::StencilFaceFlags::FRONT_AND_BACK, + value, + ) + }; + } + unsafe fn set_blend_constants(&mut self, color: &[f32; 4]) { + unsafe { self.device.raw.cmd_set_blend_constants(self.active, color) }; + } + + unsafe fn draw( + &mut self, + first_vertex: u32, + vertex_count: u32, + first_instance: u32, + instance_count: u32, + ) { + if self.current_pipeline_is_multiview + && (first_instance as u64 + instance_count as u64 - 1) + > self.device.private_caps.multiview_instance_index_limit as u64 + { + panic!("This vulkan device is affected by [#8333](https://github.com/gfx-rs/wgpu/issues/8333)"); + } + unsafe { + self.device.raw.cmd_draw( + self.active, + vertex_count, + instance_count, + first_vertex, + first_instance, + ) + }; + } + unsafe fn draw_indexed( + &mut self, + first_index: u32, + index_count: u32, + base_vertex: i32, + first_instance: u32, + instance_count: u32, + ) { + if self.current_pipeline_is_multiview + && (first_instance as u64 + instance_count as u64 - 1) + > self.device.private_caps.multiview_instance_index_limit as u64 + { + panic!("This vulkan device is affected by [#8333](https://github.com/gfx-rs/wgpu/issues/8333)"); + } + unsafe { + self.device.raw.cmd_draw_indexed( + self.active, + index_count, + instance_count, + first_index, + base_vertex, + first_instance, + ) + }; + } + unsafe fn draw_mesh_tasks( + &mut self, + group_count_x: u32, + group_count_y: u32, + group_count_z: u32, + ) { + if let Some(ref t) = self.device.extension_fns.mesh_shading { + unsafe { + t.cmd_draw_mesh_tasks(self.active, group_count_x, group_count_y, group_count_z); + }; + } else { + panic!("Feature `MESH_SHADING` not enabled"); + } + } + unsafe fn draw_indirect( + &mut self, + buffer: &super::Buffer, + offset: wgt::BufferAddress, + draw_count: u32, + ) { + if draw_count >= 1 + && self.device.private_caps.multi_draw_indirect + && draw_count <= self.device.private_caps.max_draw_indirect_count + { + unsafe { + self.device.raw.cmd_draw_indirect( + self.active, + buffer.raw, + offset, + draw_count, + size_of::<wgt::DrawIndirectArgs>() as u32, + ) + }; + } else { + for i in 0..draw_count { + let indirect_offset = offset + + i as wgt::BufferAddress + * size_of::<wgt::DrawIndirectArgs>() as wgt::BufferAddress; + unsafe { + self.device.raw.cmd_draw_indirect( + self.active, + buffer.raw, + indirect_offset, + 1, + size_of::<wgt::DrawIndirectArgs>() as u32, + ) + }; + } + } + } + unsafe fn draw_indexed_indirect( + &mut self, + buffer: &super::Buffer, + offset: wgt::BufferAddress, + draw_count: u32, + ) { + if draw_count >= 1 + && self.device.private_caps.multi_draw_indirect + && draw_count <= self.device.private_caps.max_draw_indirect_count + { + unsafe { + self.device.raw.cmd_draw_indexed_indirect( + self.active, + buffer.raw, + offset, + draw_count, + size_of::<wgt::DrawIndexedIndirectArgs>() as u32, + ) + }; + } else { + for i in 0..draw_count { + let indirect_offset = offset + + i as wgt::BufferAddress + * size_of::<wgt::DrawIndexedIndirectArgs>() as wgt::BufferAddress; + unsafe { + self.device.raw.cmd_draw_indexed_indirect( + self.active, + buffer.raw, + indirect_offset, + 1, + size_of::<wgt::DrawIndexedIndirectArgs>() as u32, + ) + }; + } + } + } + unsafe fn draw_mesh_tasks_indirect( + &mut self, + buffer: &<Self::A as crate::Api>::Buffer, + offset: wgt::BufferAddress, + draw_count: u32, + ) { + if let Some(ref t) = self.device.extension_fns.mesh_shading { + unsafe { + t.cmd_draw_mesh_tasks_indirect( + self.active, + buffer.raw, + offset, + draw_count, + size_of::<wgt::DispatchIndirectArgs>() as u32, + ); + }; + } else { + panic!("Feature `MESH_SHADING` not enabled"); + } + } + unsafe fn draw_indirect_count( + &mut self, + buffer: &super::Buffer, + offset: wgt::BufferAddress, + count_buffer: &super::Buffer, + count_offset: wgt::BufferAddress, + max_count: u32, + ) { + let stride = size_of::<wgt::DrawIndirectArgs>() as u32; + match self.device.extension_fns.draw_indirect_count { + Some(ref t) => { + unsafe { + t.cmd_draw_indirect_count( + self.active, + buffer.raw, + offset, + count_buffer.raw, + count_offset, + max_count, + stride, + ) + }; + } + None => panic!("Feature `DRAW_INDIRECT_COUNT` not enabled"), + } + } + unsafe fn draw_indexed_indirect_count( + &mut self, + buffer: &super::Buffer, + offset: wgt::BufferAddress, + count_buffer: &super::Buffer, + count_offset: wgt::BufferAddress, + max_count: u32, + ) { + let stride = size_of::<wgt::DrawIndexedIndirectArgs>() as u32; + match self.device.extension_fns.draw_indirect_count { + Some(ref t) => { + unsafe { + t.cmd_draw_indexed_indirect_count( + self.active, + buffer.raw, + offset, + count_buffer.raw, + count_offset, + max_count, + stride, + ) + }; + } + None => panic!("Feature `DRAW_INDIRECT_COUNT` not enabled"), + } + } + unsafe fn draw_mesh_tasks_indirect_count( + &mut self, + buffer: &<Self::A as crate::Api>::Buffer, + offset: wgt::BufferAddress, + count_buffer: &super::Buffer, + count_offset: wgt::BufferAddress, + max_count: u32, + ) { + if self.device.extension_fns.draw_indirect_count.is_none() { + panic!("Feature `DRAW_INDIRECT_COUNT` not enabled"); + } + if let Some(ref t) = self.device.extension_fns.mesh_shading { + unsafe { + t.cmd_draw_mesh_tasks_indirect_count( + self.active, + buffer.raw, + offset, + count_buffer.raw, + count_offset, + max_count, + size_of::<wgt::DispatchIndirectArgs>() as u32, + ); + }; + } else { + panic!("Feature `MESH_SHADING` not enabled"); + } + } + + // compute + + unsafe fn begin_compute_pass( + &mut self, + desc: &crate::ComputePassDescriptor<'_, super::QuerySet>, + ) { + self.bind_point = vk::PipelineBindPoint::COMPUTE; + if let Some(label) = desc.label { + unsafe { self.begin_debug_marker(label) }; + self.rpass_debug_marker_active = true; + } + + if let Some(timestamp_writes) = desc.timestamp_writes.as_ref() { + if let Some(index) = timestamp_writes.beginning_of_pass_write_index { + unsafe { + self.write_timestamp(timestamp_writes.query_set, index); + } + } + self.end_of_pass_timer_query = timestamp_writes + .end_of_pass_write_index + .map(|index| (timestamp_writes.query_set.raw, index)); + } + } + unsafe fn end_compute_pass(&mut self) { + self.write_pass_end_timestamp_if_requested(); + + if self.rpass_debug_marker_active { + unsafe { self.end_debug_marker() }; + self.rpass_debug_marker_active = false + } + } + + unsafe fn set_compute_pipeline(&mut self, pipeline: &super::ComputePipeline) { + unsafe { + self.device.raw.cmd_bind_pipeline( + self.active, + vk::PipelineBindPoint::COMPUTE, + pipeline.raw, + ) + }; + } + + unsafe fn dispatch_workgroups(&mut self, count: [u32; 3]) { + unsafe { + self.device + .raw + .cmd_dispatch(self.active, count[0], count[1], count[2]) + }; + } + unsafe fn dispatch_workgroups_indirect( + &mut self, + buffer: &super::Buffer, + offset: wgt::BufferAddress, + ) { + unsafe { + self.device + .raw + .cmd_dispatch_indirect(self.active, buffer.raw, offset) + } + } + + // ray tracing + + unsafe fn begin_ray_tracing_pass(&mut self, desc: &crate::RayTracingPassDescriptor<'_>) { + self.bind_point = vk::PipelineBindPoint::RAY_TRACING_KHR; + if let Some(label) = desc.label { + unsafe { self.begin_debug_marker(label) }; + self.rpass_debug_marker_active = true; + } + } + unsafe fn end_ray_tracing_pass(&mut self) { + if self.rpass_debug_marker_active { + unsafe { self.end_debug_marker() }; + self.rpass_debug_marker_active = false + } + } + + unsafe fn trace_rays( + &mut self, + count: [u32; 3], + ray_generation_group_data: crate::PipelineGroupData<super::Buffer>, + miss_group_data: crate::PipelineGroupData<super::Buffer>, + intersection_group_data: crate::PipelineGroupData<super::Buffer>, + ) { + let ray_tracing_functions = self + .device + .extension_fns + .ray_tracing + .as_ref() + .expect("Feature `EXPERIMENTAL_RAY_TRACING` not enabled"); + + let ray_tracing_pipeline_functions = self + .device + .extension_fns + .ray_tracing_pipelines + .as_ref() + .expect("Feature `EXPERIMENTAL_RAY_TRACING_PIPELINES` not enabled"); + + let get_device_address = |buffer: &super::Buffer| unsafe { + ray_tracing_functions + .buffer_device_address + .get_buffer_device_address( + &vk::BufferDeviceAddressInfo::default().buffer(buffer.raw), + ) + }; + + unsafe { + ray_tracing_pipeline_functions.cmd_trace_rays( + self.raw_handle(), + &vk::StridedDeviceAddressRegionKHR { + device_address: get_device_address(ray_generation_group_data.buffer) + + ray_generation_group_data.offset, + stride: ray_generation_group_data.stride, + size: ray_generation_group_data.stride /* no need for multiplying by count, vulkan requires the ray gen sbt to be just one group */, + }, + &vk::StridedDeviceAddressRegionKHR { + device_address: get_device_address(miss_group_data.buffer) + + miss_group_data.offset, + stride: miss_group_data.stride, + size: miss_group_data.stride * miss_group_data.count, + }, + &vk::StridedDeviceAddressRegionKHR { + device_address: get_device_address(intersection_group_data.buffer) + + intersection_group_data.offset, + stride: intersection_group_data.stride, + size: intersection_group_data.stride * intersection_group_data.count, + }, + &vk::StridedDeviceAddressRegionKHR { + device_address: 0, + stride: 0, + size: 0, + }, + count[0], + count[1], + count[2], + ) + }; + } + + unsafe fn set_ray_tracing_pipeline(&mut self, pipeline: &super::RayTracingPipeline) { + unsafe { + self.device.raw.cmd_bind_pipeline( + self.active, + vk::PipelineBindPoint::RAY_TRACING_KHR, + pipeline.raw, + ) + }; + } + + unsafe fn copy_acceleration_structure_to_acceleration_structure( + &mut self, + src: &super::AccelerationStructure, + dst: &super::AccelerationStructure, + copy: wgt::AccelerationStructureCopy, + ) { + let ray_tracing_functions = self + .device + .extension_fns + .ray_tracing + .as_ref() + .expect("Feature `RAY_TRACING` not enabled"); + + let mode = match copy { + wgt::AccelerationStructureCopy::Clone => vk::CopyAccelerationStructureModeKHR::CLONE, + wgt::AccelerationStructureCopy::Compact => { + vk::CopyAccelerationStructureModeKHR::COMPACT + } + }; + + unsafe { + ray_tracing_functions + .acceleration_structure + .cmd_copy_acceleration_structure( + self.active, + &vk::CopyAccelerationStructureInfoKHR { + s_type: vk::StructureType::COPY_ACCELERATION_STRUCTURE_INFO_KHR, + p_next: core::ptr::null(), + src: src.raw, + dst: dst.raw, + mode, + _marker: Default::default(), + }, + ); + } + } +} + +#[test] +fn check_dst_image_layout() { + assert_eq!( + conv::derive_image_layout(wgt::TextureUses::COPY_DST, wgt::TextureFormat::Rgba8Unorm), + DST_IMAGE_LAYOUT + ); +} diff --git a/third_party/wgpu-hal/src/vulkan/conv.rs b/third_party/wgpu-hal/src/vulkan/conv.rs new file mode 100644 index 000000000..47bdf4954 --- /dev/null +++ b/third_party/wgpu-hal/src/vulkan/conv.rs @@ -0,0 +1,1136 @@ +use alloc::vec::Vec; + +use ash::vk; + +impl super::PrivateCapabilities { + pub fn map_texture_format(&self, format: wgt::TextureFormat) -> vk::Format { + use ash::vk::Format as F; + use wgt::TextureFormat as Tf; + use wgt::{AstcBlock, AstcChannel}; + match format { + Tf::R8Unorm => F::R8_UNORM, + Tf::R8Snorm => F::R8_SNORM, + Tf::R8Uint => F::R8_UINT, + Tf::R8Sint => F::R8_SINT, + Tf::R16Uint => F::R16_UINT, + Tf::R16Sint => F::R16_SINT, + Tf::R16Unorm => F::R16_UNORM, + Tf::R16Snorm => F::R16_SNORM, + Tf::R16Float => F::R16_SFLOAT, + Tf::Rg8Unorm => F::R8G8_UNORM, + Tf::Rg8Snorm => F::R8G8_SNORM, + Tf::Rg8Uint => F::R8G8_UINT, + Tf::Rg8Sint => F::R8G8_SINT, + Tf::Rg16Unorm => F::R16G16_UNORM, + Tf::Rg16Snorm => F::R16G16_SNORM, + Tf::R32Uint => F::R32_UINT, + Tf::R32Sint => F::R32_SINT, + Tf::R32Float => F::R32_SFLOAT, + Tf::Rg16Uint => F::R16G16_UINT, + Tf::Rg16Sint => F::R16G16_SINT, + Tf::Rg16Float => F::R16G16_SFLOAT, + Tf::Rgba8Unorm => F::R8G8B8A8_UNORM, + Tf::Rgba8UnormSrgb => F::R8G8B8A8_SRGB, + Tf::Bgra8UnormSrgb => F::B8G8R8A8_SRGB, + Tf::Rgba8Snorm => F::R8G8B8A8_SNORM, + Tf::Bgra8Unorm => F::B8G8R8A8_UNORM, + Tf::Rgba8Uint => F::R8G8B8A8_UINT, + Tf::Rgba8Sint => F::R8G8B8A8_SINT, + Tf::Rgb10a2Uint => F::A2B10G10R10_UINT_PACK32, + Tf::Rgb10a2Unorm => F::A2B10G10R10_UNORM_PACK32, + Tf::Rg11b10Ufloat => F::B10G11R11_UFLOAT_PACK32, + Tf::R64Uint => F::R64_UINT, + Tf::Rg32Uint => F::R32G32_UINT, + Tf::Rg32Sint => F::R32G32_SINT, + Tf::Rg32Float => F::R32G32_SFLOAT, + Tf::Rgba16Uint => F::R16G16B16A16_UINT, + Tf::Rgba16Sint => F::R16G16B16A16_SINT, + Tf::Rgba16Unorm => F::R16G16B16A16_UNORM, + Tf::Rgba16Snorm => F::R16G16B16A16_SNORM, + Tf::Rgba16Float => F::R16G16B16A16_SFLOAT, + Tf::Rgba32Uint => F::R32G32B32A32_UINT, + Tf::Rgba32Sint => F::R32G32B32A32_SINT, + Tf::Rgba32Float => F::R32G32B32A32_SFLOAT, + Tf::Depth32Float => F::D32_SFLOAT, + Tf::Depth32FloatStencil8 => F::D32_SFLOAT_S8_UINT, + Tf::Depth24Plus => { + if self.texture_d24 { + F::X8_D24_UNORM_PACK32 + } else { + F::D32_SFLOAT + } + } + Tf::Depth24PlusStencil8 => { + if self.texture_d24_s8 { + F::D24_UNORM_S8_UINT + } else { + F::D32_SFLOAT_S8_UINT + } + } + Tf::Stencil8 => { + if self.texture_s8 { + F::S8_UINT + } else if self.texture_d24_s8 { + F::D24_UNORM_S8_UINT + } else { + F::D32_SFLOAT_S8_UINT + } + } + Tf::Depth16Unorm => F::D16_UNORM, + Tf::NV12 => F::G8_B8R8_2PLANE_420_UNORM, + Tf::P010 => F::G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16, + Tf::Rgb9e5Ufloat => F::E5B9G9R9_UFLOAT_PACK32, + Tf::Bc1RgbaUnorm => F::BC1_RGBA_UNORM_BLOCK, + Tf::Bc1RgbaUnormSrgb => F::BC1_RGBA_SRGB_BLOCK, + Tf::Bc2RgbaUnorm => F::BC2_UNORM_BLOCK, + Tf::Bc2RgbaUnormSrgb => F::BC2_SRGB_BLOCK, + Tf::Bc3RgbaUnorm => F::BC3_UNORM_BLOCK, + Tf::Bc3RgbaUnormSrgb => F::BC3_SRGB_BLOCK, + Tf::Bc4RUnorm => F::BC4_UNORM_BLOCK, + Tf::Bc4RSnorm => F::BC4_SNORM_BLOCK, + Tf::Bc5RgUnorm => F::BC5_UNORM_BLOCK, + Tf::Bc5RgSnorm => F::BC5_SNORM_BLOCK, + Tf::Bc6hRgbUfloat => F::BC6H_UFLOAT_BLOCK, + Tf::Bc6hRgbFloat => F::BC6H_SFLOAT_BLOCK, + Tf::Bc7RgbaUnorm => F::BC7_UNORM_BLOCK, + Tf::Bc7RgbaUnormSrgb => F::BC7_SRGB_BLOCK, + Tf::Etc2Rgb8Unorm => F::ETC2_R8G8B8_UNORM_BLOCK, + Tf::Etc2Rgb8UnormSrgb => F::ETC2_R8G8B8_SRGB_BLOCK, + Tf::Etc2Rgb8A1Unorm => F::ETC2_R8G8B8A1_UNORM_BLOCK, + Tf::Etc2Rgb8A1UnormSrgb => F::ETC2_R8G8B8A1_SRGB_BLOCK, + Tf::Etc2Rgba8Unorm => F::ETC2_R8G8B8A8_UNORM_BLOCK, + Tf::Etc2Rgba8UnormSrgb => F::ETC2_R8G8B8A8_SRGB_BLOCK, + Tf::EacR11Unorm => F::EAC_R11_UNORM_BLOCK, + Tf::EacR11Snorm => F::EAC_R11_SNORM_BLOCK, + Tf::EacRg11Unorm => F::EAC_R11G11_UNORM_BLOCK, + Tf::EacRg11Snorm => F::EAC_R11G11_SNORM_BLOCK, + Tf::Astc { block, channel } => match channel { + AstcChannel::Unorm => match block { + AstcBlock::B4x4 => F::ASTC_4X4_UNORM_BLOCK, + AstcBlock::B5x4 => F::ASTC_5X4_UNORM_BLOCK, + AstcBlock::B5x5 => F::ASTC_5X5_UNORM_BLOCK, + AstcBlock::B6x5 => F::ASTC_6X5_UNORM_BLOCK, + AstcBlock::B6x6 => F::ASTC_6X6_UNORM_BLOCK, + AstcBlock::B8x5 => F::ASTC_8X5_UNORM_BLOCK, + AstcBlock::B8x6 => F::ASTC_8X6_UNORM_BLOCK, + AstcBlock::B8x8 => F::ASTC_8X8_UNORM_BLOCK, + AstcBlock::B10x5 => F::ASTC_10X5_UNORM_BLOCK, + AstcBlock::B10x6 => F::ASTC_10X6_UNORM_BLOCK, + AstcBlock::B10x8 => F::ASTC_10X8_UNORM_BLOCK, + AstcBlock::B10x10 => F::ASTC_10X10_UNORM_BLOCK, + AstcBlock::B12x10 => F::ASTC_12X10_UNORM_BLOCK, + AstcBlock::B12x12 => F::ASTC_12X12_UNORM_BLOCK, + }, + AstcChannel::UnormSrgb => match block { + AstcBlock::B4x4 => F::ASTC_4X4_SRGB_BLOCK, + AstcBlock::B5x4 => F::ASTC_5X4_SRGB_BLOCK, + AstcBlock::B5x5 => F::ASTC_5X5_SRGB_BLOCK, + AstcBlock::B6x5 => F::ASTC_6X5_SRGB_BLOCK, + AstcBlock::B6x6 => F::ASTC_6X6_SRGB_BLOCK, + AstcBlock::B8x5 => F::ASTC_8X5_SRGB_BLOCK, + AstcBlock::B8x6 => F::ASTC_8X6_SRGB_BLOCK, + AstcBlock::B8x8 => F::ASTC_8X8_SRGB_BLOCK, + AstcBlock::B10x5 => F::ASTC_10X5_SRGB_BLOCK, + AstcBlock::B10x6 => F::ASTC_10X6_SRGB_BLOCK, + AstcBlock::B10x8 => F::ASTC_10X8_SRGB_BLOCK, + AstcBlock::B10x10 => F::ASTC_10X10_SRGB_BLOCK, + AstcBlock::B12x10 => F::ASTC_12X10_SRGB_BLOCK, + AstcBlock::B12x12 => F::ASTC_12X12_SRGB_BLOCK, + }, + AstcChannel::Hdr => match block { + AstcBlock::B4x4 => F::ASTC_4X4_SFLOAT_BLOCK_EXT, + AstcBlock::B5x4 => F::ASTC_5X4_SFLOAT_BLOCK_EXT, + AstcBlock::B5x5 => F::ASTC_5X5_SFLOAT_BLOCK_EXT, + AstcBlock::B6x5 => F::ASTC_6X5_SFLOAT_BLOCK_EXT, + AstcBlock::B6x6 => F::ASTC_6X6_SFLOAT_BLOCK_EXT, + AstcBlock::B8x5 => F::ASTC_8X5_SFLOAT_BLOCK_EXT, + AstcBlock::B8x6 => F::ASTC_8X6_SFLOAT_BLOCK_EXT, + AstcBlock::B8x8 => F::ASTC_8X8_SFLOAT_BLOCK_EXT, + AstcBlock::B10x5 => F::ASTC_10X5_SFLOAT_BLOCK_EXT, + AstcBlock::B10x6 => F::ASTC_10X6_SFLOAT_BLOCK_EXT, + AstcBlock::B10x8 => F::ASTC_10X8_SFLOAT_BLOCK_EXT, + AstcBlock::B10x10 => F::ASTC_10X10_SFLOAT_BLOCK_EXT, + AstcBlock::B12x10 => F::ASTC_12X10_SFLOAT_BLOCK_EXT, + AstcBlock::B12x12 => F::ASTC_12X12_SFLOAT_BLOCK_EXT, + }, + }, + } + } +} + +pub fn map_vk_surface_formats( + sf: vk::SurfaceFormatKHR, +) -> Option<(wgt::TextureFormat, wgt::SurfaceColorSpace)> { + use ash::vk::Format as F; + use wgt::TextureFormat as Tf; + // Format list we care about pulled from https://vulkan.gpuinfo.org/listsurfaceformats.php. + let format = match sf.format { + F::B8G8R8A8_UNORM => Tf::Bgra8Unorm, + F::B8G8R8A8_SRGB => Tf::Bgra8UnormSrgb, + F::R8G8B8A8_SNORM => Tf::Rgba8Snorm, + F::R8G8B8A8_UNORM => Tf::Rgba8Unorm, + F::R8G8B8A8_SRGB => Tf::Rgba8UnormSrgb, + F::R16G16B16A16_SNORM => Tf::Rgba16Snorm, + F::R16G16B16A16_UNORM => Tf::Rgba16Unorm, + F::R16G16B16A16_SFLOAT => Tf::Rgba16Float, + F::A2B10G10R10_UNORM_PACK32 => Tf::Rgb10a2Unorm, + _ => return None, + }; + let color_space = map_vk_color_space(sf.color_space)?; + Some((format, color_space)) +} + +pub fn map_vk_color_space(color_space: vk::ColorSpaceKHR) -> Option<wgt::SurfaceColorSpace> { + use wgt::SurfaceColorSpace as Scs; + Some(match color_space { + vk::ColorSpaceKHR::SRGB_NONLINEAR => Scs::Srgb, + vk::ColorSpaceKHR::EXTENDED_SRGB_LINEAR_EXT => Scs::ExtendedSrgbLinear, + vk::ColorSpaceKHR::EXTENDED_SRGB_NONLINEAR_EXT => Scs::ExtendedSrgb, + vk::ColorSpaceKHR::DISPLAY_P3_NONLINEAR_EXT => Scs::DisplayP3, + vk::ColorSpaceKHR::HDR10_ST2084_EXT => Scs::Bt2100Pq, + vk::ColorSpaceKHR::HDR10_HLG_EXT => Scs::Bt2100Hlg, + _ => return None, + }) +} + +pub fn map_surface_color_space(color_space: wgt::SurfaceColorSpace) -> vk::ColorSpaceKHR { + use wgt::SurfaceColorSpace as Scs; + match color_space { + Scs::Auto => unreachable!("wgpu-core resolves `Auto` before configuring the surface"), + Scs::Srgb => vk::ColorSpaceKHR::SRGB_NONLINEAR, + Scs::ExtendedSrgbLinear => vk::ColorSpaceKHR::EXTENDED_SRGB_LINEAR_EXT, + Scs::ExtendedSrgb => vk::ColorSpaceKHR::EXTENDED_SRGB_NONLINEAR_EXT, + Scs::DisplayP3 => vk::ColorSpaceKHR::DISPLAY_P3_NONLINEAR_EXT, + Scs::Bt2100Pq => vk::ColorSpaceKHR::HDR10_ST2084_EXT, + Scs::Bt2100Hlg => vk::ColorSpaceKHR::HDR10_HLG_EXT, + Scs::ExtendedDisplayP3 => { + unreachable!("`ExtendedDisplayP3` is never reported in the Vulkan surface capabilities") + } + } +} + +impl crate::Attachment<'_, super::TextureView> { + pub(super) fn make_attachment_key(&self, ops: crate::AttachmentOps) -> super::AttachmentKey { + super::AttachmentKey { + format: self.view.raw_format, + layout: derive_image_layout(self.usage, self.view.format), + ops, + } + } +} + +impl crate::ColorAttachment<'_, super::TextureView> { + pub(super) unsafe fn make_vk_clear_color(&self) -> vk::ClearColorValue { + let cv = &self.clear_value; + match self.target.view.format.sample_type(None, None).unwrap() { + wgt::TextureSampleType::Float { .. } => vk::ClearColorValue { + float32: [cv.r as f32, cv.g as f32, cv.b as f32, cv.a as f32], + }, + wgt::TextureSampleType::Sint => vk::ClearColorValue { + int32: [cv.r as i32, cv.g as i32, cv.b as i32, cv.a as i32], + }, + wgt::TextureSampleType::Uint => vk::ClearColorValue { + uint32: [cv.r as u32, cv.g as u32, cv.b as u32, cv.a as u32], + }, + wgt::TextureSampleType::Depth => unreachable!(), + } + } +} + +pub fn derive_image_layout(usage: wgt::TextureUses, format: wgt::TextureFormat) -> vk::ImageLayout { + // Note: depth textures are always sampled with RODS layout + let is_color = !format.is_depth_stencil_format(); + match usage { + wgt::TextureUses::UNINITIALIZED => vk::ImageLayout::UNDEFINED, + wgt::TextureUses::COPY_SRC => vk::ImageLayout::TRANSFER_SRC_OPTIMAL, + wgt::TextureUses::COPY_DST => vk::ImageLayout::TRANSFER_DST_OPTIMAL, + wgt::TextureUses::RESOURCE if is_color => vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL, + wgt::TextureUses::COLOR_TARGET => vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL, + wgt::TextureUses::DEPTH_STENCIL_WRITE => vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL, + _ => { + if usage == wgt::TextureUses::PRESENT { + vk::ImageLayout::PRESENT_SRC_KHR + } else if is_color { + vk::ImageLayout::GENERAL + } else { + vk::ImageLayout::DEPTH_STENCIL_READ_ONLY_OPTIMAL + } + } + } +} + +pub fn map_texture_usage(usage: wgt::TextureUses) -> vk::ImageUsageFlags { + let mut flags = vk::ImageUsageFlags::empty(); + if usage.contains(wgt::TextureUses::COPY_SRC) { + flags |= vk::ImageUsageFlags::TRANSFER_SRC; + } + if usage.contains(wgt::TextureUses::COPY_DST) { + flags |= vk::ImageUsageFlags::TRANSFER_DST; + } + if usage.contains(wgt::TextureUses::RESOURCE) { + flags |= vk::ImageUsageFlags::SAMPLED; + } + if usage.contains(wgt::TextureUses::COLOR_TARGET) { + flags |= vk::ImageUsageFlags::COLOR_ATTACHMENT; + } + if usage + .intersects(wgt::TextureUses::DEPTH_STENCIL_READ | wgt::TextureUses::DEPTH_STENCIL_WRITE) + { + flags |= vk::ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT; + } + if usage.intersects( + wgt::TextureUses::STORAGE_READ_ONLY + | wgt::TextureUses::STORAGE_WRITE_ONLY + | wgt::TextureUses::STORAGE_READ_WRITE + | wgt::TextureUses::STORAGE_ATOMIC, + ) { + flags |= vk::ImageUsageFlags::STORAGE; + } + if usage.contains(wgt::TextureUses::TRANSIENT) { + flags |= vk::ImageUsageFlags::TRANSIENT_ATTACHMENT; + } + flags +} + +pub fn map_texture_usage_to_barrier( + usage: wgt::TextureUses, +) -> (vk::PipelineStageFlags, vk::AccessFlags) { + let mut stages = vk::PipelineStageFlags::empty(); + let mut access = vk::AccessFlags::empty(); + let shader_stages = vk::PipelineStageFlags::VERTEX_SHADER + | vk::PipelineStageFlags::FRAGMENT_SHADER + | vk::PipelineStageFlags::COMPUTE_SHADER; + + if usage.contains(wgt::TextureUses::COPY_SRC) { + stages |= vk::PipelineStageFlags::TRANSFER; + access |= vk::AccessFlags::TRANSFER_READ; + } + if usage.contains(wgt::TextureUses::COPY_DST) { + stages |= vk::PipelineStageFlags::TRANSFER; + access |= vk::AccessFlags::TRANSFER_WRITE; + } + if usage.contains(wgt::TextureUses::RESOURCE) { + stages |= shader_stages; + access |= vk::AccessFlags::SHADER_READ; + } + if usage.contains(wgt::TextureUses::COLOR_TARGET) { + stages |= vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT; + access |= vk::AccessFlags::COLOR_ATTACHMENT_READ | vk::AccessFlags::COLOR_ATTACHMENT_WRITE; + } + if usage.intersects(wgt::TextureUses::DEPTH_STENCIL_READ) { + stages |= vk::PipelineStageFlags::EARLY_FRAGMENT_TESTS + | vk::PipelineStageFlags::LATE_FRAGMENT_TESTS; + access |= vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_READ; + } + if usage.intersects(wgt::TextureUses::DEPTH_STENCIL_WRITE) { + stages |= vk::PipelineStageFlags::EARLY_FRAGMENT_TESTS + | vk::PipelineStageFlags::LATE_FRAGMENT_TESTS; + access |= vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_READ + | vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_WRITE; + } + if usage.intersects( + wgt::TextureUses::STORAGE_READ_ONLY + | wgt::TextureUses::STORAGE_READ_WRITE + | wgt::TextureUses::STORAGE_ATOMIC, + ) { + stages |= shader_stages; + access |= vk::AccessFlags::SHADER_READ; + } + if usage.intersects( + wgt::TextureUses::STORAGE_WRITE_ONLY + | wgt::TextureUses::STORAGE_READ_WRITE + | wgt::TextureUses::STORAGE_ATOMIC, + ) { + stages |= shader_stages; + access |= vk::AccessFlags::SHADER_WRITE; + } + + if usage == wgt::TextureUses::UNINITIALIZED || usage == wgt::TextureUses::PRESENT { + ( + vk::PipelineStageFlags::TOP_OF_PIPE, + vk::AccessFlags::empty(), + ) + } else { + (stages, access) + } +} + +pub fn map_vk_image_usage(usage: vk::ImageUsageFlags) -> wgt::TextureUses { + let mut bits = wgt::TextureUses::empty(); + if usage.contains(vk::ImageUsageFlags::TRANSFER_SRC) { + bits |= wgt::TextureUses::COPY_SRC; + } + if usage.contains(vk::ImageUsageFlags::TRANSFER_DST) { + bits |= wgt::TextureUses::COPY_DST; + } + if usage.contains(vk::ImageUsageFlags::SAMPLED) { + bits |= wgt::TextureUses::RESOURCE; + } + if usage.contains(vk::ImageUsageFlags::COLOR_ATTACHMENT) { + bits |= wgt::TextureUses::COLOR_TARGET; + } + if usage.contains(vk::ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT) { + bits |= wgt::TextureUses::DEPTH_STENCIL_READ | wgt::TextureUses::DEPTH_STENCIL_WRITE; + } + if usage.contains(vk::ImageUsageFlags::STORAGE) { + bits |= wgt::TextureUses::STORAGE_READ_ONLY + | wgt::TextureUses::STORAGE_WRITE_ONLY + | wgt::TextureUses::STORAGE_READ_WRITE + | wgt::TextureUses::STORAGE_ATOMIC; + } + if usage.contains(vk::ImageUsageFlags::TRANSIENT_ATTACHMENT) { + bits |= wgt::TextureUses::TRANSIENT; + } + bits +} + +pub fn map_texture_dimension(dim: wgt::TextureDimension) -> vk::ImageType { + match dim { + wgt::TextureDimension::D1 => vk::ImageType::TYPE_1D, + wgt::TextureDimension::D2 => vk::ImageType::TYPE_2D, + wgt::TextureDimension::D3 => vk::ImageType::TYPE_3D, + } +} + +pub fn map_index_format(index_format: wgt::IndexFormat) -> vk::IndexType { + match index_format { + wgt::IndexFormat::Uint16 => vk::IndexType::UINT16, + wgt::IndexFormat::Uint32 => vk::IndexType::UINT32, + } +} + +pub fn map_vertex_format(vertex_format: wgt::VertexFormat) -> vk::Format { + use wgt::VertexFormat as Vf; + match vertex_format { + Vf::Uint8 => vk::Format::R8_UINT, + Vf::Uint8x2 => vk::Format::R8G8_UINT, + Vf::Uint8x4 => vk::Format::R8G8B8A8_UINT, + Vf::Sint8 => vk::Format::R8_SINT, + Vf::Sint8x2 => vk::Format::R8G8_SINT, + Vf::Sint8x4 => vk::Format::R8G8B8A8_SINT, + Vf::Unorm8 => vk::Format::R8_UNORM, + Vf::Unorm8x2 => vk::Format::R8G8_UNORM, + Vf::Unorm8x4 => vk::Format::R8G8B8A8_UNORM, + Vf::Snorm8 => vk::Format::R8_SNORM, + Vf::Snorm8x2 => vk::Format::R8G8_SNORM, + Vf::Snorm8x4 => vk::Format::R8G8B8A8_SNORM, + Vf::Uint16 => vk::Format::R16_UINT, + Vf::Uint16x2 => vk::Format::R16G16_UINT, + Vf::Uint16x4 => vk::Format::R16G16B16A16_UINT, + Vf::Sint16 => vk::Format::R16_SINT, + Vf::Sint16x2 => vk::Format::R16G16_SINT, + Vf::Sint16x4 => vk::Format::R16G16B16A16_SINT, + Vf::Unorm16 => vk::Format::R16_UNORM, + Vf::Unorm16x2 => vk::Format::R16G16_UNORM, + Vf::Unorm16x4 => vk::Format::R16G16B16A16_UNORM, + Vf::Snorm16 => vk::Format::R16_SNORM, + Vf::Snorm16x2 => vk::Format::R16G16_SNORM, + Vf::Snorm16x4 => vk::Format::R16G16B16A16_SNORM, + Vf::Float16 => vk::Format::R16_SFLOAT, + Vf::Float16x2 => vk::Format::R16G16_SFLOAT, + Vf::Float16x4 => vk::Format::R16G16B16A16_SFLOAT, + Vf::Float32 => vk::Format::R32_SFLOAT, + Vf::Float32x2 => vk::Format::R32G32_SFLOAT, + Vf::Float32x3 => vk::Format::R32G32B32_SFLOAT, + Vf::Float32x4 => vk::Format::R32G32B32A32_SFLOAT, + Vf::Uint32 => vk::Format::R32_UINT, + Vf::Uint32x2 => vk::Format::R32G32_UINT, + Vf::Uint32x3 => vk::Format::R32G32B32_UINT, + Vf::Uint32x4 => vk::Format::R32G32B32A32_UINT, + Vf::Sint32 => vk::Format::R32_SINT, + Vf::Sint32x2 => vk::Format::R32G32_SINT, + Vf::Sint32x3 => vk::Format::R32G32B32_SINT, + Vf::Sint32x4 => vk::Format::R32G32B32A32_SINT, + Vf::Float64 => vk::Format::R64_SFLOAT, + Vf::Float64x2 => vk::Format::R64G64_SFLOAT, + Vf::Float64x3 => vk::Format::R64G64B64_SFLOAT, + Vf::Float64x4 => vk::Format::R64G64B64A64_SFLOAT, + Vf::Unorm10_10_10_2 => vk::Format::A2B10G10R10_UNORM_PACK32, + Vf::Unorm8x4Bgra => vk::Format::B8G8R8A8_UNORM, + } +} + +pub fn map_aspects(aspects: crate::FormatAspects) -> vk::ImageAspectFlags { + let mut flags = vk::ImageAspectFlags::empty(); + if aspects.contains(crate::FormatAspects::COLOR) { + flags |= vk::ImageAspectFlags::COLOR; + } + if aspects.contains(crate::FormatAspects::DEPTH) { + flags |= vk::ImageAspectFlags::DEPTH; + } + if aspects.contains(crate::FormatAspects::STENCIL) { + flags |= vk::ImageAspectFlags::STENCIL; + } + if aspects.contains(crate::FormatAspects::PLANE_0) { + flags |= vk::ImageAspectFlags::PLANE_0; + } + if aspects.contains(crate::FormatAspects::PLANE_1) { + flags |= vk::ImageAspectFlags::PLANE_1; + } + if aspects.contains(crate::FormatAspects::PLANE_2) { + flags |= vk::ImageAspectFlags::PLANE_2; + } + flags +} + +pub fn map_attachment_ops( + op: crate::AttachmentOps, +) -> (vk::AttachmentLoadOp, vk::AttachmentStoreOp) { + let load_op = if op.contains(crate::AttachmentOps::LOAD) { + vk::AttachmentLoadOp::LOAD + } else if op.contains(crate::AttachmentOps::LOAD_DONT_CARE) { + vk::AttachmentLoadOp::DONT_CARE + } else if op.contains(crate::AttachmentOps::LOAD_CLEAR) { + vk::AttachmentLoadOp::CLEAR + } else { + unreachable!() + }; + let store_op = if op.contains(crate::AttachmentOps::STORE) { + vk::AttachmentStoreOp::STORE + } else if op.contains(crate::AttachmentOps::STORE_DISCARD) { + vk::AttachmentStoreOp::DONT_CARE + } else { + unreachable!() + }; + (load_op, store_op) +} + +pub fn map_present_mode(mode: wgt::PresentMode) -> vk::PresentModeKHR { + match mode { + wgt::PresentMode::Immediate => vk::PresentModeKHR::IMMEDIATE, + wgt::PresentMode::Mailbox => vk::PresentModeKHR::MAILBOX, + wgt::PresentMode::Fifo => vk::PresentModeKHR::FIFO, + wgt::PresentMode::FifoRelaxed => vk::PresentModeKHR::FIFO_RELAXED, + wgt::PresentMode::AutoNoVsync | wgt::PresentMode::AutoVsync => { + unreachable!("Cannot create swapchain with Auto PresentationMode") + } + } +} + +pub fn map_vk_present_mode(mode: vk::PresentModeKHR) -> Option<wgt::PresentMode> { + // Not exposed in Ash yet. + const FIFO_LATEST_READY: vk::PresentModeKHR = vk::PresentModeKHR::from_raw(1_000_361_000); + + // See https://registry.khronos.org/vulkan/specs/latest/man/html/VkPresentModeKHR.html + match mode { + vk::PresentModeKHR::IMMEDIATE => Some(wgt::PresentMode::Immediate), + vk::PresentModeKHR::MAILBOX => Some(wgt::PresentMode::Mailbox), + vk::PresentModeKHR::FIFO => Some(wgt::PresentMode::Fifo), + vk::PresentModeKHR::FIFO_RELAXED => Some(wgt::PresentMode::FifoRelaxed), + + // Modes that aren't exposed yet. + vk::PresentModeKHR::SHARED_DEMAND_REFRESH => None, + vk::PresentModeKHR::SHARED_CONTINUOUS_REFRESH => None, + FIFO_LATEST_READY => None, + + _ => { + log::debug!("Unrecognized present mode {mode:?}"); + None + } + } +} + +pub fn map_composite_alpha_mode(mode: wgt::CompositeAlphaMode) -> vk::CompositeAlphaFlagsKHR { + match mode { + wgt::CompositeAlphaMode::Opaque => vk::CompositeAlphaFlagsKHR::OPAQUE, + wgt::CompositeAlphaMode::PreMultiplied => vk::CompositeAlphaFlagsKHR::PRE_MULTIPLIED, + wgt::CompositeAlphaMode::PostMultiplied => vk::CompositeAlphaFlagsKHR::POST_MULTIPLIED, + wgt::CompositeAlphaMode::Inherit => vk::CompositeAlphaFlagsKHR::INHERIT, + wgt::CompositeAlphaMode::Auto => unreachable!(), + } +} + +pub fn map_vk_composite_alpha(flags: vk::CompositeAlphaFlagsKHR) -> Vec<wgt::CompositeAlphaMode> { + let mut modes = Vec::new(); + if flags.contains(vk::CompositeAlphaFlagsKHR::OPAQUE) { + modes.push(wgt::CompositeAlphaMode::Opaque); + } + if flags.contains(vk::CompositeAlphaFlagsKHR::PRE_MULTIPLIED) { + modes.push(wgt::CompositeAlphaMode::PreMultiplied); + } + if flags.contains(vk::CompositeAlphaFlagsKHR::POST_MULTIPLIED) { + modes.push(wgt::CompositeAlphaMode::PostMultiplied); + } + if flags.contains(vk::CompositeAlphaFlagsKHR::INHERIT) { + modes.push(wgt::CompositeAlphaMode::Inherit); + } + modes +} + +pub fn map_buffer_usage(usage: wgt::BufferUses) -> vk::BufferUsageFlags { + let mut flags = vk::BufferUsageFlags::empty(); + if usage.contains(wgt::BufferUses::COPY_SRC) { + flags |= vk::BufferUsageFlags::TRANSFER_SRC; + } + if usage.contains(wgt::BufferUses::COPY_DST) { + flags |= vk::BufferUsageFlags::TRANSFER_DST; + } + if usage.contains(wgt::BufferUses::UNIFORM) { + flags |= vk::BufferUsageFlags::UNIFORM_BUFFER; + } + if usage.intersects(wgt::BufferUses::STORAGE_READ_ONLY | wgt::BufferUses::STORAGE_READ_WRITE) { + flags |= vk::BufferUsageFlags::STORAGE_BUFFER; + } + if usage.contains(wgt::BufferUses::INDEX) { + flags |= vk::BufferUsageFlags::INDEX_BUFFER; + } + if usage.contains(wgt::BufferUses::VERTEX) { + flags |= vk::BufferUsageFlags::VERTEX_BUFFER; + } + if usage.contains(wgt::BufferUses::INDIRECT) { + flags |= vk::BufferUsageFlags::INDIRECT_BUFFER; + } + if usage.contains(wgt::BufferUses::ACCELERATION_STRUCTURE_SCRATCH) { + flags |= vk::BufferUsageFlags::STORAGE_BUFFER | vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS; + } + if usage.intersects( + wgt::BufferUses::BOTTOM_LEVEL_ACCELERATION_STRUCTURE_INPUT + | wgt::BufferUses::TOP_LEVEL_ACCELERATION_STRUCTURE_INPUT, + ) { + flags |= vk::BufferUsageFlags::ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_KHR + | vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS; + } + if usage.intersects(wgt::BufferUses::ACCELERATION_STRUCTURE_QUERY) { + flags |= vk::BufferUsageFlags::TRANSFER_DST; + } + if usage.intersects(wgt::BufferUses::RAY_TRACING_PIPELINE_SHADER_DATA) { + flags |= vk::BufferUsageFlags::SHADER_BINDING_TABLE_KHR + | vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS; + } + flags +} + +pub fn map_buffer_usage_to_barrier( + usage: wgt::BufferUses, +) -> (vk::PipelineStageFlags, vk::AccessFlags) { + let mut stages = vk::PipelineStageFlags::empty(); + let mut access = vk::AccessFlags::empty(); + let shader_stages = vk::PipelineStageFlags::VERTEX_SHADER + | vk::PipelineStageFlags::FRAGMENT_SHADER + | vk::PipelineStageFlags::COMPUTE_SHADER; + + if usage.contains(wgt::BufferUses::MAP_READ) { + stages |= vk::PipelineStageFlags::HOST; + access |= vk::AccessFlags::HOST_READ; + } + if usage.contains(wgt::BufferUses::MAP_WRITE) { + stages |= vk::PipelineStageFlags::HOST; + access |= vk::AccessFlags::HOST_WRITE; + } + if usage.contains(wgt::BufferUses::COPY_SRC) { + stages |= vk::PipelineStageFlags::TRANSFER; + access |= vk::AccessFlags::TRANSFER_READ; + } + if usage.contains(wgt::BufferUses::COPY_DST) { + stages |= vk::PipelineStageFlags::TRANSFER; + access |= vk::AccessFlags::TRANSFER_WRITE; + } + if usage.contains(wgt::BufferUses::UNIFORM) { + stages |= shader_stages; + access |= vk::AccessFlags::UNIFORM_READ; + } + if usage.intersects(wgt::BufferUses::STORAGE_READ_ONLY) { + stages |= shader_stages; + access |= vk::AccessFlags::SHADER_READ; + } + if usage.intersects(wgt::BufferUses::STORAGE_READ_WRITE) { + stages |= shader_stages; + access |= vk::AccessFlags::SHADER_READ | vk::AccessFlags::SHADER_WRITE; + } + if usage.contains(wgt::BufferUses::INDEX) { + stages |= vk::PipelineStageFlags::VERTEX_INPUT; + access |= vk::AccessFlags::INDEX_READ; + } + if usage.contains(wgt::BufferUses::VERTEX) { + stages |= vk::PipelineStageFlags::VERTEX_INPUT; + access |= vk::AccessFlags::VERTEX_ATTRIBUTE_READ; + } + if usage.contains(wgt::BufferUses::INDIRECT) { + stages |= vk::PipelineStageFlags::DRAW_INDIRECT; + access |= vk::AccessFlags::INDIRECT_COMMAND_READ; + } + if usage.intersects( + wgt::BufferUses::BOTTOM_LEVEL_ACCELERATION_STRUCTURE_INPUT + | wgt::BufferUses::TOP_LEVEL_ACCELERATION_STRUCTURE_INPUT + | wgt::BufferUses::ACCELERATION_STRUCTURE_SCRATCH, + ) { + stages |= vk::PipelineStageFlags::ACCELERATION_STRUCTURE_BUILD_KHR; + access |= vk::AccessFlags::ACCELERATION_STRUCTURE_READ_KHR + | vk::AccessFlags::ACCELERATION_STRUCTURE_WRITE_KHR + | vk::AccessFlags::SHADER_READ; + } + if usage.contains(wgt::BufferUses::ACCELERATION_STRUCTURE_QUERY) { + stages |= vk::PipelineStageFlags::TRANSFER; + access |= vk::AccessFlags::TRANSFER_WRITE; + } + + (stages, access) +} + +pub fn map_view_dimension(dim: wgt::TextureViewDimension) -> vk::ImageViewType { + match dim { + wgt::TextureViewDimension::D1 => vk::ImageViewType::TYPE_1D, + wgt::TextureViewDimension::D2 => vk::ImageViewType::TYPE_2D, + wgt::TextureViewDimension::D2Array => vk::ImageViewType::TYPE_2D_ARRAY, + wgt::TextureViewDimension::Cube => vk::ImageViewType::CUBE, + wgt::TextureViewDimension::CubeArray => vk::ImageViewType::CUBE_ARRAY, + wgt::TextureViewDimension::D3 => vk::ImageViewType::TYPE_3D, + } +} + +pub fn map_copy_extent(extent: &crate::CopyExtent) -> vk::Extent3D { + vk::Extent3D { + width: extent.width, + height: extent.height, + depth: extent.depth, + } +} + +pub fn map_subresource_range( + range: &wgt::ImageSubresourceRange, + format: wgt::TextureFormat, +) -> vk::ImageSubresourceRange { + vk::ImageSubresourceRange { + aspect_mask: map_aspects(crate::FormatAspects::new(format, range.aspect)), + base_mip_level: range.base_mip_level, + level_count: range.mip_level_count.unwrap_or(vk::REMAINING_MIP_LEVELS), + base_array_layer: range.base_array_layer, + layer_count: range + .array_layer_count + .unwrap_or(vk::REMAINING_ARRAY_LAYERS), + } +} + +// Special subresource range mapping for dealing with barriers +// so that we account for the "hidden" depth aspect in emulated Stencil8. +pub(super) fn map_subresource_range_combined_aspect( + range: &wgt::ImageSubresourceRange, + format: wgt::TextureFormat, + private_caps: &super::PrivateCapabilities, +) -> vk::ImageSubresourceRange { + let mut range = map_subresource_range(range, format); + if !private_caps.texture_s8 && format == wgt::TextureFormat::Stencil8 { + range.aspect_mask |= vk::ImageAspectFlags::DEPTH; + } + range +} + +pub fn map_subresource_layers( + base: &crate::TextureCopyBase, +) -> (vk::ImageSubresourceLayers, vk::Offset3D) { + let offset = vk::Offset3D { + x: base.origin.x as i32, + y: base.origin.y as i32, + z: base.origin.z as i32, + }; + let subresource = vk::ImageSubresourceLayers { + aspect_mask: map_aspects(base.aspect), + mip_level: base.mip_level, + base_array_layer: base.array_layer, + layer_count: 1, + }; + (subresource, offset) +} + +pub fn map_filter_mode(mode: wgt::FilterMode) -> vk::Filter { + match mode { + wgt::FilterMode::Nearest => vk::Filter::NEAREST, + wgt::FilterMode::Linear => vk::Filter::LINEAR, + } +} + +pub fn map_mip_filter_mode(mode: wgt::MipmapFilterMode) -> vk::SamplerMipmapMode { + match mode { + wgt::MipmapFilterMode::Nearest => vk::SamplerMipmapMode::NEAREST, + wgt::MipmapFilterMode::Linear => vk::SamplerMipmapMode::LINEAR, + } +} + +pub fn map_address_mode(mode: wgt::AddressMode) -> vk::SamplerAddressMode { + match mode { + wgt::AddressMode::ClampToEdge => vk::SamplerAddressMode::CLAMP_TO_EDGE, + wgt::AddressMode::Repeat => vk::SamplerAddressMode::REPEAT, + wgt::AddressMode::MirrorRepeat => vk::SamplerAddressMode::MIRRORED_REPEAT, + wgt::AddressMode::ClampToBorder => vk::SamplerAddressMode::CLAMP_TO_BORDER, + // wgt::AddressMode::MirrorClamp => vk::SamplerAddressMode::MIRROR_CLAMP_TO_EDGE, + } +} + +pub fn map_border_color(border_color: wgt::SamplerBorderColor) -> vk::BorderColor { + match border_color { + wgt::SamplerBorderColor::TransparentBlack | wgt::SamplerBorderColor::Zero => { + vk::BorderColor::FLOAT_TRANSPARENT_BLACK + } + wgt::SamplerBorderColor::OpaqueBlack => vk::BorderColor::FLOAT_OPAQUE_BLACK, + wgt::SamplerBorderColor::OpaqueWhite => vk::BorderColor::FLOAT_OPAQUE_WHITE, + } +} + +pub fn map_comparison(fun: wgt::CompareFunction) -> vk::CompareOp { + use wgt::CompareFunction as Cf; + match fun { + Cf::Never => vk::CompareOp::NEVER, + Cf::Less => vk::CompareOp::LESS, + Cf::LessEqual => vk::CompareOp::LESS_OR_EQUAL, + Cf::Equal => vk::CompareOp::EQUAL, + Cf::GreaterEqual => vk::CompareOp::GREATER_OR_EQUAL, + Cf::Greater => vk::CompareOp::GREATER, + Cf::NotEqual => vk::CompareOp::NOT_EQUAL, + Cf::Always => vk::CompareOp::ALWAYS, + } +} + +pub fn map_shader_stage(stage: wgt::ShaderStages) -> vk::ShaderStageFlags { + let mut flags = vk::ShaderStageFlags::empty(); + if stage.contains(wgt::ShaderStages::VERTEX) { + flags |= vk::ShaderStageFlags::VERTEX; + } + if stage.contains(wgt::ShaderStages::FRAGMENT) { + flags |= vk::ShaderStageFlags::FRAGMENT; + } + if stage.contains(wgt::ShaderStages::COMPUTE) { + flags |= vk::ShaderStageFlags::COMPUTE; + } + if stage.contains(wgt::ShaderStages::TASK) { + flags |= vk::ShaderStageFlags::TASK_EXT; + } + if stage.contains(wgt::ShaderStages::MESH) { + flags |= vk::ShaderStageFlags::MESH_EXT; + } + if stage.contains(wgt::ShaderStages::RAY_GENERATION) { + flags |= vk::ShaderStageFlags::RAYGEN_KHR; + } + if stage.contains(wgt::ShaderStages::MISS) { + flags |= vk::ShaderStageFlags::MISS_KHR; + } + if stage.contains(wgt::ShaderStages::ANY_HIT) { + flags |= vk::ShaderStageFlags::ANY_HIT_KHR; + } + if stage.contains(wgt::ShaderStages::CLOSEST_HIT) { + flags |= vk::ShaderStageFlags::CLOSEST_HIT_KHR; + } + flags +} + +pub fn map_binding_type(ty: wgt::BindingType) -> vk::DescriptorType { + match ty { + wgt::BindingType::Buffer { + ty, + has_dynamic_offset, + .. + } => match ty { + wgt::BufferBindingType::Storage { .. } => match has_dynamic_offset { + true => vk::DescriptorType::STORAGE_BUFFER_DYNAMIC, + false => vk::DescriptorType::STORAGE_BUFFER, + }, + wgt::BufferBindingType::Uniform => match has_dynamic_offset { + true => vk::DescriptorType::UNIFORM_BUFFER_DYNAMIC, + false => vk::DescriptorType::UNIFORM_BUFFER, + }, + }, + wgt::BindingType::Sampler { .. } => vk::DescriptorType::SAMPLER, + wgt::BindingType::Texture { .. } => vk::DescriptorType::SAMPLED_IMAGE, + wgt::BindingType::StorageTexture { .. } => vk::DescriptorType::STORAGE_IMAGE, + wgt::BindingType::AccelerationStructure { .. } => { + vk::DescriptorType::ACCELERATION_STRUCTURE_KHR + } + wgt::BindingType::ExternalTexture => unimplemented!(), + } +} + +pub fn map_topology(topology: wgt::PrimitiveTopology) -> vk::PrimitiveTopology { + use wgt::PrimitiveTopology as Pt; + match topology { + Pt::PointList => vk::PrimitiveTopology::POINT_LIST, + Pt::LineList => vk::PrimitiveTopology::LINE_LIST, + Pt::LineStrip => vk::PrimitiveTopology::LINE_STRIP, + Pt::TriangleList => vk::PrimitiveTopology::TRIANGLE_LIST, + Pt::TriangleStrip => vk::PrimitiveTopology::TRIANGLE_STRIP, + } +} + +pub fn map_polygon_mode(mode: wgt::PolygonMode) -> vk::PolygonMode { + match mode { + wgt::PolygonMode::Fill => vk::PolygonMode::FILL, + wgt::PolygonMode::Line => vk::PolygonMode::LINE, + wgt::PolygonMode::Point => vk::PolygonMode::POINT, + } +} + +pub fn map_front_face(front_face: wgt::FrontFace) -> vk::FrontFace { + match front_face { + wgt::FrontFace::Cw => vk::FrontFace::CLOCKWISE, + wgt::FrontFace::Ccw => vk::FrontFace::COUNTER_CLOCKWISE, + } +} + +pub fn map_cull_face(face: wgt::Face) -> vk::CullModeFlags { + match face { + wgt::Face::Front => vk::CullModeFlags::FRONT, + wgt::Face::Back => vk::CullModeFlags::BACK, + } +} + +pub fn map_stencil_op(op: wgt::StencilOperation) -> vk::StencilOp { + use wgt::StencilOperation as So; + match op { + So::Keep => vk::StencilOp::KEEP, + So::Zero => vk::StencilOp::ZERO, + So::Replace => vk::StencilOp::REPLACE, + So::Invert => vk::StencilOp::INVERT, + So::IncrementClamp => vk::StencilOp::INCREMENT_AND_CLAMP, + So::IncrementWrap => vk::StencilOp::INCREMENT_AND_WRAP, + So::DecrementClamp => vk::StencilOp::DECREMENT_AND_CLAMP, + So::DecrementWrap => vk::StencilOp::DECREMENT_AND_WRAP, + } +} + +pub fn map_stencil_face( + face: &wgt::StencilFaceState, + compare_mask: u32, + write_mask: u32, +) -> vk::StencilOpState { + vk::StencilOpState { + fail_op: map_stencil_op(face.fail_op), + pass_op: map_stencil_op(face.pass_op), + depth_fail_op: map_stencil_op(face.depth_fail_op), + compare_op: map_comparison(face.compare), + compare_mask, + write_mask, + reference: 0, + } +} + +fn map_blend_factor(factor: wgt::BlendFactor) -> vk::BlendFactor { + use wgt::BlendFactor as Bf; + match factor { + Bf::Zero => vk::BlendFactor::ZERO, + Bf::One => vk::BlendFactor::ONE, + Bf::Src => vk::BlendFactor::SRC_COLOR, + Bf::OneMinusSrc => vk::BlendFactor::ONE_MINUS_SRC_COLOR, + Bf::SrcAlpha => vk::BlendFactor::SRC_ALPHA, + Bf::OneMinusSrcAlpha => vk::BlendFactor::ONE_MINUS_SRC_ALPHA, + Bf::Dst => vk::BlendFactor::DST_COLOR, + Bf::OneMinusDst => vk::BlendFactor::ONE_MINUS_DST_COLOR, + Bf::DstAlpha => vk::BlendFactor::DST_ALPHA, + Bf::OneMinusDstAlpha => vk::BlendFactor::ONE_MINUS_DST_ALPHA, + Bf::SrcAlphaSaturated => vk::BlendFactor::SRC_ALPHA_SATURATE, + Bf::Constant => vk::BlendFactor::CONSTANT_COLOR, + Bf::OneMinusConstant => vk::BlendFactor::ONE_MINUS_CONSTANT_COLOR, + Bf::Src1 => vk::BlendFactor::SRC1_COLOR, + Bf::OneMinusSrc1 => vk::BlendFactor::ONE_MINUS_SRC1_COLOR, + Bf::Src1Alpha => vk::BlendFactor::SRC1_ALPHA, + Bf::OneMinusSrc1Alpha => vk::BlendFactor::ONE_MINUS_SRC1_ALPHA, + } +} + +fn map_blend_op(operation: wgt::BlendOperation) -> vk::BlendOp { + use wgt::BlendOperation as Bo; + match operation { + Bo::Add => vk::BlendOp::ADD, + Bo::Subtract => vk::BlendOp::SUBTRACT, + Bo::ReverseSubtract => vk::BlendOp::REVERSE_SUBTRACT, + Bo::Min => vk::BlendOp::MIN, + Bo::Max => vk::BlendOp::MAX, + } +} + +pub fn map_blend_component( + component: &wgt::BlendComponent, +) -> (vk::BlendOp, vk::BlendFactor, vk::BlendFactor) { + let op = map_blend_op(component.operation); + let src = map_blend_factor(component.src_factor); + let dst = map_blend_factor(component.dst_factor); + (op, src, dst) +} + +pub fn map_pipeline_statistics( + types: wgt::PipelineStatisticsTypes, +) -> vk::QueryPipelineStatisticFlags { + use wgt::PipelineStatisticsTypes as Pst; + let mut flags = vk::QueryPipelineStatisticFlags::empty(); + if types.contains(Pst::VERTEX_SHADER_INVOCATIONS) { + flags |= vk::QueryPipelineStatisticFlags::VERTEX_SHADER_INVOCATIONS; + } + if types.contains(Pst::CLIPPER_INVOCATIONS) { + flags |= vk::QueryPipelineStatisticFlags::CLIPPING_INVOCATIONS; + } + if types.contains(Pst::CLIPPER_PRIMITIVES_OUT) { + flags |= vk::QueryPipelineStatisticFlags::CLIPPING_PRIMITIVES; + } + if types.contains(Pst::FRAGMENT_SHADER_INVOCATIONS) { + flags |= vk::QueryPipelineStatisticFlags::FRAGMENT_SHADER_INVOCATIONS; + } + if types.contains(Pst::COMPUTE_SHADER_INVOCATIONS) { + flags |= vk::QueryPipelineStatisticFlags::COMPUTE_SHADER_INVOCATIONS; + } + flags +} + +pub fn map_acceleration_structure_format( + format: crate::AccelerationStructureFormat, +) -> vk::AccelerationStructureTypeKHR { + match format { + crate::AccelerationStructureFormat::TopLevel => vk::AccelerationStructureTypeKHR::TOP_LEVEL, + crate::AccelerationStructureFormat::BottomLevel => { + vk::AccelerationStructureTypeKHR::BOTTOM_LEVEL + } + } +} + +pub fn map_acceleration_structure_build_mode( + format: crate::AccelerationStructureBuildMode, +) -> vk::BuildAccelerationStructureModeKHR { + match format { + crate::AccelerationStructureBuildMode::Build => { + vk::BuildAccelerationStructureModeKHR::BUILD + } + crate::AccelerationStructureBuildMode::Update => { + vk::BuildAccelerationStructureModeKHR::UPDATE + } + } +} + +pub fn map_acceleration_structure_flags( + flags: crate::AccelerationStructureBuildFlags, +) -> vk::BuildAccelerationStructureFlagsKHR { + let mut vk_flags = vk::BuildAccelerationStructureFlagsKHR::empty(); + + if flags.contains(crate::AccelerationStructureBuildFlags::PREFER_FAST_TRACE) { + vk_flags |= vk::BuildAccelerationStructureFlagsKHR::PREFER_FAST_TRACE; + } + + if flags.contains(crate::AccelerationStructureBuildFlags::PREFER_FAST_BUILD) { + vk_flags |= vk::BuildAccelerationStructureFlagsKHR::PREFER_FAST_BUILD; + } + + if flags.contains(crate::AccelerationStructureBuildFlags::ALLOW_UPDATE) { + vk_flags |= vk::BuildAccelerationStructureFlagsKHR::ALLOW_UPDATE; + } + + if flags.contains(crate::AccelerationStructureBuildFlags::LOW_MEMORY) { + vk_flags |= vk::BuildAccelerationStructureFlagsKHR::LOW_MEMORY; + } + + if flags.contains(crate::AccelerationStructureBuildFlags::ALLOW_COMPACTION) { + vk_flags |= vk::BuildAccelerationStructureFlagsKHR::ALLOW_COMPACTION + } + + if flags.contains(crate::AccelerationStructureBuildFlags::ALLOW_RAY_HIT_VERTEX_RETURN) { + vk_flags |= vk::BuildAccelerationStructureFlagsKHR::ALLOW_DATA_ACCESS + } + + vk_flags +} + +pub fn map_acceleration_structure_geometry_flags( + flags: crate::AccelerationStructureGeometryFlags, +) -> vk::GeometryFlagsKHR { + let mut vk_flags = vk::GeometryFlagsKHR::empty(); + + if flags.contains(crate::AccelerationStructureGeometryFlags::OPAQUE) { + vk_flags |= vk::GeometryFlagsKHR::OPAQUE; + } + + if flags.contains(crate::AccelerationStructureGeometryFlags::NO_DUPLICATE_ANY_HIT_INVOCATION) { + vk_flags |= vk::GeometryFlagsKHR::NO_DUPLICATE_ANY_HIT_INVOCATION; + } + + vk_flags +} + +pub fn map_acceleration_structure_usage_to_barrier( + usage: crate::AccelerationStructureUses, + features: wgt::Features, +) -> (vk::PipelineStageFlags, vk::AccessFlags) { + let mut stages = vk::PipelineStageFlags::empty(); + let mut access = vk::AccessFlags::empty(); + + if usage.contains(crate::AccelerationStructureUses::BUILD_INPUT) { + stages |= vk::PipelineStageFlags::ACCELERATION_STRUCTURE_BUILD_KHR; + access |= vk::AccessFlags::ACCELERATION_STRUCTURE_READ_KHR; + } + if usage.contains(crate::AccelerationStructureUses::QUERY_INPUT) { + stages |= vk::PipelineStageFlags::ACCELERATION_STRUCTURE_BUILD_KHR; + access |= vk::AccessFlags::ACCELERATION_STRUCTURE_READ_KHR; + } + if usage.contains(crate::AccelerationStructureUses::BUILD_OUTPUT) { + stages |= vk::PipelineStageFlags::ACCELERATION_STRUCTURE_BUILD_KHR; + access |= vk::AccessFlags::ACCELERATION_STRUCTURE_WRITE_KHR; + } + if usage.contains(crate::AccelerationStructureUses::SHADER_INPUT) + && features.contains(wgt::Features::EXPERIMENTAL_RAY_QUERY) + { + stages |= vk::PipelineStageFlags::VERTEX_SHADER + | vk::PipelineStageFlags::FRAGMENT_SHADER + | vk::PipelineStageFlags::COMPUTE_SHADER; + access |= vk::AccessFlags::ACCELERATION_STRUCTURE_READ_KHR; + } + if usage.contains(crate::AccelerationStructureUses::SHADER_INPUT) + && features.contains(wgt::Features::EXPERIMENTAL_RAY_TRACING_PIPELINES) + { + stages |= vk::PipelineStageFlags::RAY_TRACING_SHADER_KHR; + access |= vk::AccessFlags::ACCELERATION_STRUCTURE_READ_KHR; + } + if usage.contains(crate::AccelerationStructureUses::COPY_SRC) { + stages |= vk::PipelineStageFlags::ACCELERATION_STRUCTURE_BUILD_KHR; + access |= vk::AccessFlags::ACCELERATION_STRUCTURE_READ_KHR; + } + if usage.contains(crate::AccelerationStructureUses::COPY_DST) { + stages |= vk::PipelineStageFlags::ACCELERATION_STRUCTURE_BUILD_KHR; + access |= vk::AccessFlags::ACCELERATION_STRUCTURE_WRITE_KHR; + } + + (stages, access) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// `map_vk_color_space` and `map_surface_color_space` must stay mutually + /// inverse so that a color space reported in the surface capabilities is + /// exactly what the swapchain is created with. + #[test] + fn color_space_round_trip() { + for vk_color_space in [ + vk::ColorSpaceKHR::SRGB_NONLINEAR, + vk::ColorSpaceKHR::EXTENDED_SRGB_LINEAR_EXT, + vk::ColorSpaceKHR::EXTENDED_SRGB_NONLINEAR_EXT, + vk::ColorSpaceKHR::DISPLAY_P3_NONLINEAR_EXT, + vk::ColorSpaceKHR::HDR10_ST2084_EXT, + vk::ColorSpaceKHR::HDR10_HLG_EXT, + ] { + let mapped = map_vk_color_space(vk_color_space).unwrap(); + assert_eq!(map_surface_color_space(mapped), vk_color_space); + } + } + + #[test] + fn unknown_color_spaces_are_dropped() { + for vk_color_space in [ + vk::ColorSpaceKHR::BT2020_LINEAR_EXT, + vk::ColorSpaceKHR::DOLBYVISION_EXT, + vk::ColorSpaceKHR::PASS_THROUGH_EXT, + vk::ColorSpaceKHR::ADOBERGB_NONLINEAR_EXT, + ] { + assert!(map_vk_color_space(vk_color_space).is_none()); + assert!(map_vk_surface_formats(vk::SurfaceFormatKHR { + format: vk::Format::B8G8R8A8_UNORM, + color_space: vk_color_space, + }) + .is_none()); + } + } + + #[test] + fn hdr10_surface_format_maps() { + let (format, color_space) = map_vk_surface_formats(vk::SurfaceFormatKHR { + format: vk::Format::A2B10G10R10_UNORM_PACK32, + color_space: vk::ColorSpaceKHR::HDR10_ST2084_EXT, + }) + .unwrap(); + assert_eq!(format, wgt::TextureFormat::Rgb10a2Unorm); + assert_eq!(color_space, wgt::SurfaceColorSpace::Bt2100Pq); + } +} diff --git a/third_party/wgpu-hal/src/vulkan/descriptor.rs b/third_party/wgpu-hal/src/vulkan/descriptor.rs new file mode 100644 index 000000000..99fd5c69b --- /dev/null +++ b/third_party/wgpu-hal/src/vulkan/descriptor.rs @@ -0,0 +1,350 @@ +use alloc::vec::Vec; +use arrayvec::ArrayVec; +use ash::vk; +use hashbrown::{HashMap, HashSet}; + +const POOL_MIN_SETS: u32 = 64; +const POOL_MAX_SETS: u32 = 512; + +/// The dynamic counts are tracked separately since dynamic textures/buffers +/// have distinct descriptor types compared to their non-dynamic counterparts. +#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)] +pub struct DescriptorCounts { + pub sampler: u32, + pub sampled_image: u32, + pub storage_image: u32, + pub uniform_buffer: u32, + pub uniform_buffer_dynamic: u32, + pub storage_buffer: u32, + pub storage_buffer_dynamic: u32, + pub acceleration_structure: u32, +} + +impl DescriptorCounts { + fn total(&self) -> u32 { + self.sampler + + self.sampled_image + + self.storage_image + + self.uniform_buffer + + self.uniform_buffer_dynamic + + self.storage_buffer + + self.storage_buffer_dynamic + + self.acceleration_structure + } +} + +#[derive(Debug)] +pub struct DescriptorSet { + raw: vk::DescriptorSet, + bucket_key: BucketKey, + pool_index: usize, +} + +impl DescriptorSet { + pub fn raw(&self) -> vk::DescriptorSet { + self.raw + } +} + +#[derive(Debug, PartialEq, Eq, Hash)] +struct BucketKey { + counts: DescriptorCounts, + update_after_bind: bool, +} + +struct Pool { + raw: vk::DescriptorPool, + capacity: u32, + available: u32, +} + +/// Keeps track of all pools created with this bucket's [`BucketKey`]. +#[derive(Default)] +struct Bucket { + /// We keep track of all descriptor set layouts that might use this bucket. + /// If the set becomes empty, the bucket is destroyed. + layouts: HashSet<vk::DescriptorSetLayout>, + pools: Vec<Pool>, + available_sets: u32, + allocated_sets: u32, +} + +impl Bucket { + fn create_pool( + &mut self, + device: &ash::Device, + key: &BucketKey, + capacity_hint: u32, + ) -> Result<(usize, &mut Pool), crate::DeviceError> { + let index = self.pools.len(); + let pool = create_descriptor_pool(device, key, capacity_hint)?; + self.available_sets += pool.capacity; + self.pools.push(pool); + Ok((index, self.pools.last_mut().unwrap())) + } +} + +pub struct DescriptorAllocator { + buckets: HashMap<BucketKey, Bucket>, + max_update_after_bind_descriptors_in_all_pools: u32, + update_after_bind_descriptors_in_all_pools: u32, +} + +impl super::BindGroupLayout { + fn bucket_key(&self) -> BucketKey { + let update_after_bind = self.contains_binding_arrays; + let counts = self.desc_count.clone(); + BucketKey { + counts, + update_after_bind, + } + } +} + +impl DescriptorAllocator { + pub fn new(max_update_after_bind_descriptors_in_all_pools: u32) -> Self { + DescriptorAllocator { + buckets: HashMap::default(), + max_update_after_bind_descriptors_in_all_pools, + update_after_bind_descriptors_in_all_pools: 0, + } + } + + pub fn register_layout( + &mut self, + device: &ash::Device, + layout: &super::BindGroupLayout, + ) -> Result<(), crate::DeviceError> { + let key = layout.bucket_key(); + let bucket = match self.buckets.entry(key) { + hashbrown::hash_map::Entry::Occupied(occupied_entry) => occupied_entry.into_mut(), + hashbrown::hash_map::Entry::Vacant(vacant_entry) => { + let mut bucket = Bucket::default(); + // Create at least 1 pool upfront instead of doing this when + // creating the first bind group. + bucket.create_pool(device, vacant_entry.key(), POOL_MIN_SETS)?; + vacant_entry.insert(bucket) + } + }; + + assert!(bucket.layouts.insert(layout.raw)); + + Ok(()) + } + + pub fn unregister_layout(&mut self, device: &ash::Device, layout: &super::BindGroupLayout) { + let key = layout.bucket_key(); + let bucket = self.buckets.get_mut(&key).unwrap(); + + assert!(bucket.layouts.remove(&layout.raw)); + + if bucket.layouts.is_empty() { + // Remove the bucket and destroy any remaining pools. + let bucket = self.buckets.remove(&key).unwrap(); + for pool in bucket.pools { + assert_eq!( + pool.available, pool.capacity, + "pool is not empty, at least one DescriptorSet has not been freed" + ); + unsafe { device.destroy_descriptor_pool(pool.raw, None) }; + } + } + } + + pub unsafe fn alloc( + &mut self, + device: &ash::Device, + layout: &super::BindGroupLayout, + ) -> Result<DescriptorSet, crate::DeviceError> { + let update_after_bind = layout.contains_binding_arrays; + let total_descriptors = layout.desc_count.total(); + + if update_after_bind + && self.max_update_after_bind_descriptors_in_all_pools + - self.update_after_bind_descriptors_in_all_pools + < total_descriptors + { + return Err(crate::DeviceError::OutOfMemory); + } + + let key = layout.bucket_key(); + let bucket = self.buckets.get_mut(&key).unwrap(); + + // Prefer smaller/older/fuller pools for new allocations to prevent + // fragmentation and possibly fragmentation of hardware resources + // (VK_ERROR_FRAGMENTATION) + let pool = bucket + .pools + .iter_mut() + .enumerate() + .find(|(_, pool)| pool.available != 0); + + let (pool_index, pool) = if let Some(pool) = pool { + pool + } else { + let capacity_hint = bucket.allocated_sets; + bucket.create_pool(device, &key, capacity_hint)? + }; + + let vk_info = vk::DescriptorSetAllocateInfo::default() + .descriptor_pool(pool.raw) + .set_layouts(core::slice::from_ref(&layout.raw)); + + let raw = match unsafe { device.allocate_descriptor_sets(&vk_info) } { + Ok(sets) => Ok(sets[0]), + // We make sure not to exceed the size of the pool. + Err(vk::Result::ERROR_OUT_OF_POOL_MEMORY) => unreachable!(), + // We only allocate from a pool if the nr and type of descriptors + // used by a layout is the same as those specified at pool + // creation time. + // + // > Additionally, if all sets allocated from the pool since it was + // created or most recently reset use the same number of + // descriptors (of each type) and the requested allocation also + // uses that same number of descriptors (of each type), + // then fragmentation must not cause an allocation failure. + // + // from https://docs.vulkan.org/refpages/latest/refpages/source/VkDescriptorPoolCreateInfo.html#_description + Err(vk::Result::ERROR_FRAGMENTED_POOL) => unreachable!(), + Err(err) => Err(super::map_host_device_oom_err(err)), + }?; + + pool.available -= 1; + bucket.available_sets -= 1; + bucket.allocated_sets += 1; + if update_after_bind { + self.update_after_bind_descriptors_in_all_pools += total_descriptors; + } + + Ok(DescriptorSet { + raw, + bucket_key: key, + pool_index, + }) + } + + pub unsafe fn free(&mut self, device: &ash::Device, set: DescriptorSet) { + let bucket = self.buckets.get_mut(&set.bucket_key).unwrap(); + let pool = bucket.pools.get_mut(set.pool_index).unwrap(); + + let result = + unsafe { device.free_descriptor_sets(pool.raw, core::slice::from_ref(&set.raw())) }; + if let Err(err) = result { + // vkFreeDescriptorSets is documented to return: + // - VK_ERROR_UNKNOWN + // - VK_ERROR_VALIDATION_FAILED (we shouldn't encounter this one) + // wgpu-hal doesn't currently report errors in destroy functions. + // Panic here for now. It might be ok to ignore the error but the + // proper solution would probably be to lose the device. + panic!("vkFreeDescriptorSets error: {err}, please report this error"); + } + + pool.available += 1; + bucket.available_sets += 1; + bucket.allocated_sets -= 1; + if set.bucket_key.update_after_bind { + self.update_after_bind_descriptors_in_all_pools -= set.bucket_key.counts.total(); + } + + // Do not immediately destroy empty pools since they might have + // been recently created. + // Destroy a pool only if it's empty and we have 1/4th its capacity + // in other pools. + // Note that this logic will never destroy the last pool. + let pool = bucket.pools.last().unwrap(); + if pool.available == pool.capacity + && bucket.available_sets - pool.capacity > pool.capacity / 4 + { + let pool = bucket.pools.pop().unwrap(); + unsafe { device.destroy_descriptor_pool(pool.raw, None) }; + bucket.available_sets -= pool.capacity; + } + } +} + +impl Drop for DescriptorAllocator { + fn drop(&mut self) { + if !std::thread::panicking() { + assert!( + self.buckets.is_empty(), + "buckets are not empty, at least one BGL has not been unregistered" + ) + } + } +} + +fn create_descriptor_pool( + device: &ash::Device, + key: &BucketKey, + capacity_hint: u32, +) -> Result<Pool, crate::DeviceError> { + let counts = &key.counts; + + const NR_OF_DESCRIPTOR_TYPES: usize = 8; + + use vk::DescriptorType as Dt; + let counts: [_; NR_OF_DESCRIPTOR_TYPES] = [ + (Dt::SAMPLER, counts.sampler), + (Dt::SAMPLED_IMAGE, counts.sampled_image), + (Dt::STORAGE_IMAGE, counts.storage_image), + (Dt::UNIFORM_BUFFER, counts.uniform_buffer), + (Dt::UNIFORM_BUFFER_DYNAMIC, counts.uniform_buffer_dynamic), + (Dt::STORAGE_BUFFER, counts.storage_buffer), + (Dt::STORAGE_BUFFER_DYNAMIC, counts.storage_buffer_dynamic), + ( + Dt::ACCELERATION_STRUCTURE_KHR, + counts.acceleration_structure, + ), + ]; + + // bounded doubling growth strategy + let mut capacity = capacity_hint + .clamp(POOL_MIN_SETS, POOL_MAX_SETS) + .next_power_of_two(); + + // Lower capacity to avoid overflowing `count * capacity` calculations + // below. The resulting `capacity` might not be a power of 2 but that's + // ok. + for (_, count) in counts { + capacity = (u32::MAX / count.max(1)).min(capacity); + } + + let pool_sizes = counts + .into_iter() + .filter(|&(_, count)| count != 0) + .map(|(ty, count)| vk::DescriptorPoolSize { + ty, + descriptor_count: count * capacity, + }) + .collect::<ArrayVec<_, NR_OF_DESCRIPTOR_TYPES>>(); + + // Prior to Vulkan 1.3.215 `poolSizeCount` was required to be non-zero, so + // add a single small sampler entry if `pool_sizes` would otherwise be + // empty. + let mut pool_sizes = pool_sizes; + if pool_sizes.is_empty() { + pool_sizes.push(vk::DescriptorPoolSize { + ty: Dt::SAMPLER, + descriptor_count: 1, + }); + } + + let mut flags = vk::DescriptorPoolCreateFlags::FREE_DESCRIPTOR_SET; + if key.update_after_bind { + flags |= vk::DescriptorPoolCreateFlags::UPDATE_AFTER_BIND; + }; + + let vk_info = vk::DescriptorPoolCreateInfo::default() + .flags(flags) + .max_sets(capacity) + .pool_sizes(&pool_sizes); + + let raw = unsafe { device.create_descriptor_pool(&vk_info, None) } + .map_err(super::map_host_device_oom_and_fragmentation_err)?; + + Ok(Pool { + raw, + capacity, + available: capacity, + }) +} diff --git a/third_party/wgpu-hal/src/vulkan/device.rs b/third_party/wgpu-hal/src/vulkan/device.rs new file mode 100644 index 000000000..948f77a58 --- /dev/null +++ b/third_party/wgpu-hal/src/vulkan/device.rs @@ -0,0 +1,3116 @@ +use alloc::{borrow::ToOwned as _, collections::BTreeMap, ffi::CString, sync::Arc, vec::Vec}; +use core::{ + ffi::CStr, + mem::{self, MaybeUninit}, + num::NonZeroU32, + ptr, + time::Duration, +}; + +use arrayvec::ArrayVec; +use ash::{ext, vk}; +use hashbrown::hash_map::Entry; +use parking_lot::{Mutex, RwLock}; + +use super::{conv, descriptor::DescriptorCounts, RawTlasInstance}; +use crate::TlasInstance; + +impl super::DeviceShared { + /// Set the name of `object` to `name`. + /// + /// If `name` contains an interior null byte, then the name set will be truncated to that byte. + /// + /// # Safety + /// + /// This method inherits the safety contract from [`vkSetDebugUtilsObjectName`]. In particular: + /// + /// - `object` must be a valid handle for one of the following: + /// - An instance-level object from the same instance as this device. + /// - A physical-device-level object that descends from the same physical device as this + /// device. + /// - A device-level object that descends from this device. + /// - `object` must be externally synchronized—only the calling thread should access it during + /// this call. + /// + /// [`vkSetDebugUtilsObjectName`]: https://registry.khronos.org/vulkan/specs/latest/man/html/vkSetDebugUtilsObjectNameEXT.html + pub(super) unsafe fn set_object_name(&self, object: impl vk::Handle, name: &str) { + let Some(extension) = self.extension_fns.debug_utils.as_ref() else { + return; + }; + + // Keep variables outside the if-else block to ensure they do not + // go out of scope while we hold a pointer to them + let mut buffer: [u8; 64] = [0u8; 64]; + let buffer_vec: Vec<u8>; + + // Append a null terminator to the string + let name_bytes = if name.len() < buffer.len() { + // Common case, string is very small. Allocate a copy on the stack. + buffer[..name.len()].copy_from_slice(name.as_bytes()); + // Add null terminator + buffer[name.len()] = 0; + &buffer[..name.len() + 1] + } else { + // Less common case, the string is large. + // This requires a heap allocation. + buffer_vec = name + .as_bytes() + .iter() + .cloned() + .chain(core::iter::once(0)) + .collect(); + &buffer_vec + }; + + let name = CStr::from_bytes_until_nul(name_bytes).expect("We have added a null byte"); + + let _result = unsafe { + extension.set_debug_utils_object_name( + &vk::DebugUtilsObjectNameInfoEXT::default() + .object_handle(object) + .object_name(name), + ) + }; + } + + pub fn make_render_pass( + &self, + key: super::RenderPassKey, + ) -> Result<vk::RenderPass, crate::DeviceError> { + Ok(match self.render_passes.lock().entry(key) { + Entry::Occupied(e) => *e.get(), + Entry::Vacant(e) => { + let super::RenderPassKey { + ref colors, + ref depth_stencil, + sample_count, + multiview_mask, + } = *e.key(); + + let mut vk_attachments = Vec::new(); + let mut color_refs = Vec::with_capacity(colors.len()); + let mut resolve_refs = Vec::with_capacity(color_refs.capacity()); + let mut ds_ref = None; + let samples = vk::SampleCountFlags::from_raw(sample_count); + let unused = vk::AttachmentReference { + attachment: vk::ATTACHMENT_UNUSED, + layout: vk::ImageLayout::UNDEFINED, + }; + for cat in colors.iter() { + let (color_ref, resolve_ref) = + if let Some(super::ColorAttachmentKey { base, resolve }) = cat { + let super::AttachmentKey { + format, + layout, + ops, + } = *base; + + let color_ref = vk::AttachmentReference { + attachment: vk_attachments.len() as u32, + layout, + }; + vk_attachments.push({ + let (load_op, store_op) = conv::map_attachment_ops(ops); + vk::AttachmentDescription::default() + .format(format) + .samples(samples) + .load_op(load_op) + .store_op(store_op) + .initial_layout(layout) + .final_layout(layout) + }); + let resolve_ref = if let Some(rat) = resolve { + let super::AttachmentKey { + format, + layout, + ops, + } = *rat; + + let (load_op, store_op) = conv::map_attachment_ops(ops); + let vk_attachment = vk::AttachmentDescription::default() + .format(format) + .samples(vk::SampleCountFlags::TYPE_1) + .load_op(load_op) + .store_op(store_op) + .initial_layout(layout) + .final_layout(layout); + vk_attachments.push(vk_attachment); + + vk::AttachmentReference { + attachment: vk_attachments.len() as u32 - 1, + layout, + } + } else { + unused + }; + + (color_ref, resolve_ref) + } else { + (unused, unused) + }; + + color_refs.push(color_ref); + resolve_refs.push(resolve_ref); + } + + if let Some(ds) = depth_stencil { + let super::DepthStencilAttachmentKey { + ref base, + stencil_ops, + } = *ds; + + let super::AttachmentKey { + format, + layout, + ops, + } = *base; + + ds_ref = Some(vk::AttachmentReference { + attachment: vk_attachments.len() as u32, + layout, + }); + let (load_op, store_op) = conv::map_attachment_ops(ops); + let (stencil_load_op, stencil_store_op) = conv::map_attachment_ops(stencil_ops); + let vk_attachment = vk::AttachmentDescription::default() + .format(format) + .samples(samples) + .load_op(load_op) + .store_op(store_op) + .stencil_load_op(stencil_load_op) + .stencil_store_op(stencil_store_op) + .initial_layout(layout) + .final_layout(layout); + vk_attachments.push(vk_attachment); + } + + let vk_subpasses = [{ + let mut vk_subpass = vk::SubpassDescription::default() + .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS) + .color_attachments(&color_refs) + .resolve_attachments(&resolve_refs); + + if self + .workarounds + .contains(super::Workarounds::EMPTY_RESOLVE_ATTACHMENT_LISTS) + && resolve_refs.is_empty() + { + vk_subpass.p_resolve_attachments = ptr::null(); + } + + if let Some(ref reference) = ds_ref { + vk_subpass = vk_subpass.depth_stencil_attachment(reference) + } + vk_subpass + }]; + + let mut vk_info = vk::RenderPassCreateInfo::default() + .attachments(&vk_attachments) + .subpasses(&vk_subpasses); + + let mut multiview_info; + let mask; + if let Some(multiview_mask) = multiview_mask { + mask = [multiview_mask.get()]; + + // On Vulkan 1.1 or later, this is an alias for core functionality + multiview_info = vk::RenderPassMultiviewCreateInfoKHR::default() + .view_masks(&mask) + .correlation_masks(&mask); + vk_info = vk_info.push_next(&mut multiview_info); + } + + let raw = unsafe { + self.raw + .create_render_pass(&vk_info, None) + .map_err(super::map_host_device_oom_err)? + }; + + *e.insert(raw) + } + }) + } + + fn make_memory_ranges<'a, I: 'a + Iterator<Item = crate::MemoryRange>>( + &self, + buffer: &'a super::Buffer, + ranges: I, + ) -> Option<impl 'a + Iterator<Item = vk::MappedMemoryRange<'a>>> { + let super::BufferOwnership::Managed(ref allocation) = buffer.ownership else { + return None; + }; + let allocation = allocation.lock(); + let mask = self.private_caps.non_coherent_map_mask; + Some(ranges.map(move |range| { + vk::MappedMemoryRange::default() + .memory(allocation.memory()) + .offset((allocation.offset() + range.start) & !mask) + .size((range.end - range.start + mask) & !mask) + })) + } +} + +struct CompiledStage { + create_info: vk::PipelineShaderStageCreateInfo<'static>, + _entry_point: CString, + temp_raw_module: Option<vk::ShaderModule>, +} + +struct MemoryProperties { + base: vk::PhysicalDeviceMemoryProperties, + heap_budget: ArrayVec<vk::DeviceSize, { vk::MAX_MEMORY_HEAPS }>, + heap_usage: ArrayVec<vk::DeviceSize, { vk::MAX_MEMORY_HEAPS }>, +} + +impl MemoryProperties { + fn types(&self) -> &[vk::MemoryType] { + let count = self.base.memory_type_count as usize; + &self.base.memory_types[0..count] + } + + fn heaps(&self) -> &[vk::MemoryHeap] { + let count = self.base.memory_heap_count as usize; + &self.base.memory_heaps[0..count] + } + + fn heap_budget(&self) -> &[vk::DeviceSize] { + let count = self.base.memory_heap_count as usize; + &self.heap_budget[0..count] + } + + fn heap_usage(&self) -> &[vk::DeviceSize] { + let count = self.base.memory_heap_count as usize; + &self.heap_usage[0..count] + } +} + +impl super::Device { + /// # Safety + /// + /// - `vk_image` must be created respecting `desc` + /// - If `drop_callback` is [`None`], wgpu-hal will take ownership of `vk_image`. If + /// `drop_callback` is [`Some`], `vk_image` must be valid until the callback is called. + /// - If the `ImageCreateFlags` does not contain `MUTABLE_FORMAT`, the `view_formats` of `desc` must be empty. + /// - If `memory` is not [`super::TextureMemory::External`], wgpu-hal will take ownership of the + /// memory (which is presumed to back `vk_image`). Otherwise, the memory must remain valid until + /// `drop_callback` is called. + pub unsafe fn texture_from_raw( + &self, + vk_image: vk::Image, + desc: &crate::TextureDescriptor, + drop_callback: Option<crate::DropCallback>, + memory: super::TextureMemory, + ) -> super::Texture { + let identity = self.shared.texture_identity_factory.next(); + let drop_guard = crate::DropGuard::from_option(drop_callback); + + if let Some(label) = desc.label { + unsafe { self.shared.set_object_name(vk_image, label) }; + } + + super::Texture { + raw: vk_image, + drop_guard, + memory, + format: desc.format, + copy_size: desc.copy_extent(), + identity, + } + } + + fn find_memory_type_index( + &self, + type_bits_req: u32, + flags_req: vk::MemoryPropertyFlags, + ) -> Option<usize> { + let mem_properties = unsafe { + self.shared + .instance + .raw + .get_physical_device_memory_properties(self.shared.physical_device) + }; + + // https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkPhysicalDeviceMemoryProperties.html + for (i, mem_ty) in mem_properties.memory_types_as_slice().iter().enumerate() { + let types_bits = 1 << i; + let is_required_memory_type = type_bits_req & types_bits != 0; + let has_required_properties = mem_ty.property_flags & flags_req == flags_req; + if is_required_memory_type && has_required_properties { + return Some(i); + } + } + + None + } + + fn create_image_without_memory( + &self, + desc: &crate::TextureDescriptor, + external_memory_image_create_info: Option<&mut vk::ExternalMemoryImageCreateInfo>, + ) -> Result<ImageWithoutMemory, crate::DeviceError> { + self.create_image_without_memory_with_tiling( + desc, + vk::ImageTiling::OPTIMAL, + external_memory_image_create_info, + None, + ) + } + + fn create_image_without_memory_with_tiling( + &self, + desc: &crate::TextureDescriptor, + tiling: vk::ImageTiling, + external_memory_image_create_info: Option<&mut vk::ExternalMemoryImageCreateInfo>, + drm_modifier_info: Option<&mut vk::ImageDrmFormatModifierExplicitCreateInfoEXT>, + ) -> Result<ImageWithoutMemory, crate::DeviceError> { + let copy_size = desc.copy_extent(); + + let mut raw_flags = vk::ImageCreateFlags::empty(); + if desc.dimension == wgt::TextureDimension::D3 + && desc.usage.contains(wgt::TextureUses::COLOR_TARGET) + { + raw_flags |= vk::ImageCreateFlags::TYPE_2D_ARRAY_COMPATIBLE; + } + if desc.is_cube_compatible() { + raw_flags |= vk::ImageCreateFlags::CUBE_COMPATIBLE; + } + + let original_format = self.shared.private_caps.map_texture_format(desc.format); + let mut vk_view_formats = vec![]; + if !desc.view_formats.is_empty() { + raw_flags |= vk::ImageCreateFlags::MUTABLE_FORMAT; + + if self.shared.private_caps.image_format_list { + vk_view_formats = desc + .view_formats + .iter() + .map(|f| self.shared.private_caps.map_texture_format(*f)) + .collect(); + vk_view_formats.push(original_format) + } + } + if desc.format.is_multi_planar_format() { + raw_flags |= + vk::ImageCreateFlags::MUTABLE_FORMAT | vk::ImageCreateFlags::EXTENDED_USAGE; + } + + let mut vk_info = vk::ImageCreateInfo::default() + .flags(raw_flags) + .image_type(conv::map_texture_dimension(desc.dimension)) + .format(original_format) + .extent(conv::map_copy_extent(©_size)) + .mip_levels(desc.mip_level_count) + .array_layers(desc.array_layer_count()) + .samples(vk::SampleCountFlags::from_raw(desc.sample_count)) + .tiling(tiling) + .usage(conv::map_texture_usage(desc.usage)) + .sharing_mode(vk::SharingMode::EXCLUSIVE) + .initial_layout(vk::ImageLayout::UNDEFINED); + + let mut format_list_info = vk::ImageFormatListCreateInfo::default(); + if !vk_view_formats.is_empty() { + format_list_info = format_list_info.view_formats(&vk_view_formats); + vk_info = vk_info.push_next(&mut format_list_info); + } + + if let Some(ext_info) = external_memory_image_create_info { + vk_info = vk_info.push_next(ext_info); + } + + if let Some(drm_info) = drm_modifier_info { + vk_info = vk_info.push_next(drm_info); + } + + let raw = unsafe { self.shared.raw.create_image(&vk_info, None) }.map_err(map_err)?; + fn map_err(err: vk::Result) -> crate::DeviceError { + // We don't use VK_EXT_image_compression_control + // VK_ERROR_COMPRESSION_EXHAUSTED_EXT + super::map_host_device_oom_and_ioca_err(err) + } + let mut req = unsafe { self.shared.raw.get_image_memory_requirements(raw) }; + + if desc.usage.contains(wgt::TextureUses::TRANSIENT) { + let mem_type_index = self.find_memory_type_index( + req.memory_type_bits, + vk::MemoryPropertyFlags::LAZILY_ALLOCATED, + ); + if let Some(mem_type_index) = mem_type_index { + req.memory_type_bits = 1 << mem_type_index; + } + } + + Ok(ImageWithoutMemory { + raw, + requirements: req, + }) + } + + /// # Safety + /// + /// - Vulkan (with VK_KHR_external_memory_win32) + /// - The `d3d11_shared_handle` must be valid and respecting `desc` + /// - `VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT` flag is used because we need to hold a reference to the handle + #[cfg(windows)] + pub unsafe fn texture_from_d3d11_shared_handle( + &self, + d3d11_shared_handle: windows::Win32::Foundation::HANDLE, + desc: &crate::TextureDescriptor, + ) -> Result<super::Texture, crate::DeviceError> { + if !self + .shared + .features + .contains(wgt::Features::VULKAN_EXTERNAL_MEMORY_WIN32) + { + log::error!("Vulkan driver does not support VK_KHR_external_memory_win32"); + return Err(crate::DeviceError::Unexpected); + } + + let mut external_memory_image_info = vk::ExternalMemoryImageCreateInfo::default() + .handle_types(vk::ExternalMemoryHandleTypeFlags::D3D11_TEXTURE); + + let image = + self.create_image_without_memory(desc, Some(&mut external_memory_image_info))?; + + // Some external memory types require dedicated allocation + // https://docs.vulkan.org/guide/latest/extensions/external.html#_importing_memory + let mut dedicated_allocate_info = + vk::MemoryDedicatedAllocateInfo::default().image(image.raw); + + let mut import_memory_info = vk::ImportMemoryWin32HandleInfoKHR::default() + .handle_type(vk::ExternalMemoryHandleTypeFlags::D3D11_TEXTURE) + .handle(d3d11_shared_handle.0 as _); + // TODO: We should use `push_next` instead, but currently ash does not provide this method for the `ImportMemoryWin32HandleInfoKHR` type. + #[allow(clippy::unnecessary_mut_passed)] + { + import_memory_info.p_next = <*const _>::cast(&mut dedicated_allocate_info); + } + + let mem_type_index = self + .find_memory_type_index( + image.requirements.memory_type_bits, + vk::MemoryPropertyFlags::DEVICE_LOCAL, + ) + .ok_or(crate::DeviceError::Unexpected)?; + + let memory_allocate_info = vk::MemoryAllocateInfo::default() + .allocation_size(image.requirements.size) + .memory_type_index(mem_type_index as _) + .push_next(&mut import_memory_info); + let memory = unsafe { self.shared.raw.allocate_memory(&memory_allocate_info, None) } + .map_err(super::map_host_device_oom_err)?; + + unsafe { self.shared.raw.bind_image_memory(image.raw, memory, 0) } + .map_err(super::map_host_device_oom_err)?; + + Ok(unsafe { + self.texture_from_raw( + image.raw, + desc, + None, + super::TextureMemory::Dedicated(memory), + ) + }) + } + + /// Import a DMA-buf as a texture. Currently only supports single-plane DMA-bufs. + /// + /// # Safety + /// + /// - Requires `VULKAN_EXTERNAL_MEMORY_DMA_BUF` feature (implies VK_EXT_external_memory_dma_buf + /// and VK_EXT_image_drm_format_modifier) + /// - The `fd` must be a valid DMA-buf file descriptor matching `desc` + /// - On success, Vulkan takes ownership of the file descriptor. On failure, + /// the file descriptor is closed. + /// - The `drm_modifier`, `stride`, and `offset` must match the DMA-buf layout + #[cfg(unix)] + pub unsafe fn texture_from_dmabuf_fd( + &self, + fd: std::os::unix::io::OwnedFd, + desc: &crate::TextureDescriptor, + drm_modifier: u64, + stride: u64, + offset: u64, + ) -> Result<super::Texture, crate::DeviceError> { + use std::os::unix::io::IntoRawFd; + + if !self + .shared + .features + .contains(wgt::Features::VULKAN_EXTERNAL_MEMORY_DMA_BUF) + { + log::error!( + "Vulkan driver does not support VK_EXT_external_memory_dma_buf \ + or VK_EXT_image_drm_format_modifier" + ); + return Err(crate::DeviceError::Unexpected); + } + + let external_memory_fd_fn = self + .shared + .extension_fns + .external_memory_fd + .as_ref() + .ok_or_else(|| { + log::error!("VK_KHR_external_memory_fd extension not loaded"); + crate::DeviceError::Unexpected + })?; + + let mut external_memory_image_info = vk::ExternalMemoryImageCreateInfo::default() + .handle_types(vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT); + + let plane_layout = vk::SubresourceLayout { + offset, + row_pitch: stride, + size: 0, + array_pitch: 0, + depth_pitch: 0, + }; + let mut drm_modifier_info = vk::ImageDrmFormatModifierExplicitCreateInfoEXT::default() + .drm_format_modifier(drm_modifier) + .plane_layouts(core::slice::from_ref(&plane_layout)); + + let image = self.create_image_without_memory_with_tiling( + desc, + vk::ImageTiling::DRM_FORMAT_MODIFIER_EXT, + Some(&mut external_memory_image_info), + Some(&mut drm_modifier_info), + )?; + + // Convert to raw fd. We must close it ourselves if any operation below + // fails, since Vulkan only takes ownership on successful vkAllocateMemory. + let fd_raw = fd.into_raw_fd(); + + let result = self.import_dmabuf_memory( + external_memory_fd_fn, + fd_raw, + image.raw, + &image.requirements, + ); + + match result { + Ok(memory) => Ok(unsafe { + self.texture_from_raw( + image.raw, + desc, + None, + super::TextureMemory::Dedicated(memory), + ) + }), + Err(e) => { + // Clean up the VkImage on failure. + unsafe { self.shared.raw.destroy_image(image.raw, None) }; + Err(e) + } + } + } + + /// Import DMA-buf memory and bind it to the image. + /// + /// On failure, the raw fd is closed (if not yet consumed by Vulkan) and the + /// caller is responsible for destroying the VkImage. + #[cfg(unix)] + fn import_dmabuf_memory( + &self, + external_memory_fd_fn: &ash::khr::external_memory_fd::Device, + fd_raw: i32, + image: vk::Image, + requirements: &vk::MemoryRequirements, + ) -> Result<vk::DeviceMemory, crate::DeviceError> { + let mut fd_props = vk::MemoryFdPropertiesKHR::default(); + unsafe { + external_memory_fd_fn.get_memory_fd_properties( + vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT, + fd_raw, + &mut fd_props, + ) + } + .map_err(|e| { + unsafe { libc::close(fd_raw) }; + super::map_host_device_oom_err(e) + })?; + + let mem_type_index = self + .find_memory_type_index( + requirements.memory_type_bits & fd_props.memory_type_bits, + vk::MemoryPropertyFlags::empty(), + ) + .ok_or_else(|| { + unsafe { libc::close(fd_raw) }; + crate::DeviceError::Unexpected + })?; + + let mut dedicated_allocate_info = vk::MemoryDedicatedAllocateInfo::default().image(image); + + let mut import_memory_info = vk::ImportMemoryFdInfoKHR::default() + .handle_type(vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT) + .fd(fd_raw); + + let memory_allocate_info = vk::MemoryAllocateInfo::default() + .allocation_size(requirements.size) + .memory_type_index(mem_type_index as _) + .push_next(&mut import_memory_info) + .push_next(&mut dedicated_allocate_info); + + // vkAllocateMemory takes ownership of the fd on success. + // On failure, the fd is NOT consumed and we must close it. + let memory = unsafe { self.shared.raw.allocate_memory(&memory_allocate_info, None) } + .map_err(|e| { + unsafe { libc::close(fd_raw) }; + super::map_host_device_oom_err(e) + })?; + + // From this point, the fd is consumed. Only VkDeviceMemory needs cleanup on error. + unsafe { self.shared.raw.bind_image_memory(image, memory, 0) }.map_err(|e| { + unsafe { self.shared.raw.free_memory(memory, None) }; + super::map_host_device_oom_err(e) + })?; + + Ok(memory) + } + + fn create_shader_module_impl( + &self, + spv: &[u32], + label: &crate::Label<'_>, + ) -> Result<vk::ShaderModule, crate::DeviceError> { + let vk_info = vk::ShaderModuleCreateInfo::default() + .flags(vk::ShaderModuleCreateFlags::empty()) + .code(spv); + + let raw = unsafe { + profiling::scope!("vkCreateShaderModule"); + self.shared + .raw + .create_shader_module(&vk_info, None) + .map_err(map_err)? + }; + fn map_err(err: vk::Result) -> crate::DeviceError { + // We don't use VK_NV_glsl_shader + // VK_ERROR_INVALID_SHADER_NV + super::map_host_device_oom_err(err) + } + + if let Some(label) = label { + unsafe { self.shared.set_object_name(raw, label) }; + } + + Ok(raw) + } + + fn compile_stage( + &self, + stage: &crate::ProgrammableStage<super::ShaderModule>, + naga_stage: naga::ShaderStage, + binding_map: &naga::back::spv::BindingMap, + ) -> Result<CompiledStage, crate::PipelineError> { + let stage_flags = crate::auxil::map_naga_stage(naga_stage); + let vk_module = match *stage.module { + super::ShaderModule::Raw(raw) => raw, + super::ShaderModule::Intermediate { + ref naga_shader, + runtime_checks, + } => { + let pipeline_options = naga::back::spv::PipelineOptions { + entry_point: stage.entry_point.to_owned(), + shader_stage: naga_stage, + }; + let needs_temp_options = !runtime_checks.bounds_checks + || !runtime_checks.force_loop_bounding + || !runtime_checks.ray_query_initialization_tracking + || !binding_map.is_empty() + || naga_shader.debug_source.is_some() + || !stage.zero_initialize_workgroup_memory + || !runtime_checks.task_shader_dispatch_tracking + || !runtime_checks.mesh_shader_primitive_indices_clamp + || !runtime_checks.int_div_checks; + + let mut temp_options; + let options = if needs_temp_options { + temp_options = self.naga_options.clone(); + if !runtime_checks.bounds_checks { + temp_options.bounds_check_policies = naga::proc::BoundsCheckPolicies { + index: naga::proc::BoundsCheckPolicy::Unchecked, + buffer: naga::proc::BoundsCheckPolicy::Unchecked, + image_load: naga::proc::BoundsCheckPolicy::Unchecked, + binding_array: naga::proc::BoundsCheckPolicy::Unchecked, + }; + } + if !runtime_checks.force_loop_bounding { + temp_options.force_loop_bounding = false; + } + if !runtime_checks.ray_query_initialization_tracking { + temp_options.ray_query_initialization_tracking = false; + } + if !binding_map.is_empty() { + temp_options.binding_map = binding_map.clone(); + } + + if let Some(ref debug) = naga_shader.debug_source { + temp_options.debug_info = Some(naga::back::spv::DebugInfo { + source_code: &debug.source_code, + file_name: debug.file_name.as_ref(), + language: naga::back::spv::SourceLanguage::WGSL, + }) + } + if !stage.zero_initialize_workgroup_memory { + temp_options.zero_initialize_workgroup_memory = + naga::back::spv::ZeroInitializeWorkgroupMemoryMode::None; + } + if !runtime_checks.task_shader_dispatch_tracking { + temp_options.task_dispatch_limits = None; + } + temp_options.mesh_shader_primitive_indices_clamp = + runtime_checks.mesh_shader_primitive_indices_clamp; + temp_options.emit_int_div_checks = runtime_checks.int_div_checks; + + &temp_options + } else { + &self.naga_options + }; + + let (module, info) = naga::back::pipeline_constants::process_overrides( + &naga_shader.module, + &naga_shader.info, + Some((naga_stage, stage.entry_point)), + stage.constants, + ) + .map_err(|e| { + crate::PipelineError::PipelineConstants(stage_flags, format!("{e}")) + })?; + + let spv = { + profiling::scope!("naga::spv::write_vec"); + naga::back::spv::write_vec(&module, &info, options, Some(&pipeline_options)) + } + .map_err(|e| crate::PipelineError::Linkage(stage_flags, format!("{e}")))?; + self.create_shader_module_impl(&spv, &None)? + } + }; + + let mut flags = vk::PipelineShaderStageCreateFlags::empty(); + if self.shared.features.contains(wgt::Features::SUBGROUP) { + flags |= vk::PipelineShaderStageCreateFlags::ALLOW_VARYING_SUBGROUP_SIZE + } + + let entry_point = CString::new(stage.entry_point).unwrap(); + let mut create_info = vk::PipelineShaderStageCreateInfo::default() + .flags(flags) + .stage(conv::map_shader_stage(stage_flags)) + .module(vk_module); + + // Circumvent struct lifetime check because of a self-reference inside CompiledStage + create_info.p_name = entry_point.as_ptr(); + + Ok(CompiledStage { + create_info, + _entry_point: entry_point, + temp_raw_module: match *stage.module { + super::ShaderModule::Raw(_) => None, + super::ShaderModule::Intermediate { .. } => Some(vk_module), + }, + }) + } + + /// Returns the queue family index of the device's internal queue. + /// + /// This is useful for constructing memory barriers needed for queue family ownership transfer when + /// external memory is involved (from/to `VK_QUEUE_FAMILY_EXTERNAL_KHR` and `VK_QUEUE_FAMILY_FOREIGN_EXT` + /// for example). + pub fn queue_family_index(&self) -> u32 { + self.shared.family_index + } + + pub fn queue_index(&self) -> u32 { + self.shared.queue_index + } + + pub fn raw_device(&self) -> &ash::Device { + &self.shared.raw + } + + pub fn raw_physical_device(&self) -> vk::PhysicalDevice { + self.shared.physical_device + } + + pub fn raw_queue(&self) -> vk::Queue { + self.shared.raw_queue + } + + pub fn enabled_device_extensions(&self) -> &[&'static CStr] { + &self.shared.enabled_extensions + } + + pub fn shared_instance(&self) -> &super::InstanceShared { + &self.shared.instance + } + + fn get_memory_properties(&self) -> Option<MemoryProperties> { + if !self + .shared + .enabled_extensions + .contains(&ext::memory_budget::NAME) + { + return None; + } + + let get_physical_device_properties = self + .shared + .instance + .get_physical_device_properties + .as_ref() + .unwrap(); + + let mut budget = vk::PhysicalDeviceMemoryBudgetPropertiesEXT::default(); + let mut props = vk::PhysicalDeviceMemoryProperties2::default().push_next(&mut budget); + + unsafe { + get_physical_device_properties + .get_physical_device_memory_properties2(self.shared.physical_device, &mut props); + } + + let vk::PhysicalDeviceMemoryProperties2 { + memory_properties, .. + } = props; + + let mut heap_budget = ArrayVec::from(budget.heap_budget); + let mut heap_usage = ArrayVec::from(budget.heap_usage); + heap_budget.truncate(memory_properties.memory_heap_count as usize); + heap_usage.truncate(memory_properties.memory_heap_count as usize); + + Some(MemoryProperties { + base: memory_properties, + heap_budget, + heap_usage, + }) + } + + /// Predict whether a proposed allocation will result in an OOM condition. + /// + /// If so, returns `Err(crate::DeviceError::OutOfMemory)`. If not, returns + /// `Ok(())`. + /// + /// The prediction quality depends on accurately selecting the heap that + /// [`gpu_allocator`] will use for the allocation, and is subject to + /// deteriorate if the logic in [`gpu_allocator`] changes. + fn error_if_would_oom_on_resource_allocation( + &self, + location: gpu_allocator::MemoryLocation, + requirements: &vk::MemoryRequirements, + ) -> Result<(), crate::DeviceError> { + use gpu_allocator::MemoryLocation; + + let Some(threshold) = self + .shared + .instance + .memory_budget_thresholds + .for_resource_creation + else { + return Ok(()); + }; + + let Some(memory_properties) = self.get_memory_properties() else { + return Ok(()); + }; + + // memory types not used by gpu-allocator + let invalid_flags = + vk::MemoryPropertyFlags::LAZILY_ALLOCATED | vk::MemoryPropertyFlags::PROTECTED; + + let preferred_flags = match location { + MemoryLocation::GpuOnly => vk::MemoryPropertyFlags::DEVICE_LOCAL, + MemoryLocation::CpuToGpu => { + vk::MemoryPropertyFlags::HOST_VISIBLE + | vk::MemoryPropertyFlags::HOST_COHERENT + | vk::MemoryPropertyFlags::DEVICE_LOCAL + } + MemoryLocation::GpuToCpu => { + vk::MemoryPropertyFlags::HOST_VISIBLE + | vk::MemoryPropertyFlags::HOST_COHERENT + | vk::MemoryPropertyFlags::HOST_CACHED + } + MemoryLocation::Unknown => vk::MemoryPropertyFlags::empty(), + }; + + let mut selected_heap = memory_properties + .types() + .iter() + .enumerate() + .find(|(i, ty)| { + (1 << i) & requirements.memory_type_bits != 0 + && ty.property_flags.contains(preferred_flags) + && !ty.property_flags.intersects(invalid_flags) + }); + + if selected_heap.is_none() { + let required_flags = match location { + MemoryLocation::GpuOnly => vk::MemoryPropertyFlags::DEVICE_LOCAL, + MemoryLocation::CpuToGpu | MemoryLocation::GpuToCpu => { + vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT + } + MemoryLocation::Unknown => vk::MemoryPropertyFlags::empty(), + }; + selected_heap = memory_properties + .types() + .iter() + .enumerate() + .find(|(i, ty)| { + (1 << i) & requirements.memory_type_bits != 0 + && ty.property_flags.contains(required_flags) + && !ty.property_flags.intersects(invalid_flags) + }); + } + + if let Some((_, ty)) = selected_heap { + let i = ty.heap_index as usize; + let heap_usage = memory_properties.heap_usage()[i]; + let heap_budget = memory_properties.heap_budget()[i]; + if heap_usage + requirements.size < heap_budget / 100 * threshold as u64 { + Ok(()) + } else { + log::warn!( + "Allocation would result in an OOM condition\n\ + Request: {requirements:?}\n\ + Heap {index} had {heap_usage}B used of {heap_budget}B total before this request.", + index = ty.heap_index, + ); + Err(crate::DeviceError::OutOfMemory) + } + } else { + log::warn!("Failed to find a suitable heap for {requirements:?}"); + Err(crate::DeviceError::OutOfMemory) + } + } +} + +impl crate::Device for super::Device { + type A = super::Api; + + unsafe fn create_buffer( + &self, + desc: &crate::BufferDescriptor, + ) -> Result<super::Buffer, crate::DeviceError> { + let vk_info = vk::BufferCreateInfo::default() + .size(desc.size) + .usage(conv::map_buffer_usage(desc.usage)) + .sharing_mode(vk::SharingMode::EXCLUSIVE); + + let raw = unsafe { + self.shared + .raw + .create_buffer(&vk_info, None) + .map_err(super::map_host_device_oom_and_ioca_err)? + }; + + let mut requirements = unsafe { self.shared.raw.get_buffer_memory_requirements(raw) }; + + 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) => gpu_allocator::MemoryLocation::CpuToGpu, + (true, false) => gpu_allocator::MemoryLocation::GpuToCpu, + (false, true) => gpu_allocator::MemoryLocation::CpuToGpu, + (false, false) => gpu_allocator::MemoryLocation::GpuOnly, + }; + + self.error_if_would_oom_on_resource_allocation(location, &requirements) + .inspect_err(|_| { + unsafe { self.shared.raw.destroy_buffer(raw, None) }; + })?; + + let name = desc.label.unwrap_or("Unlabeled buffer"); + + if desc + .usage + .contains(wgt::BufferUses::ACCELERATION_STRUCTURE_SCRATCH) + { + // There is no way to specify this usage to Vulkan so we must make sure the alignment requirement is large enough. + requirements.alignment = requirements + .alignment + .max(self.shared.private_caps.scratch_buffer_alignment as u64); + } + + let allocation = self + .mem_allocator + .lock() + .allocate(&gpu_allocator::vulkan::AllocationCreateDesc { + name, + requirements: vk::MemoryRequirements { + memory_type_bits: requirements.memory_type_bits & self.valid_ash_memory_types, + ..requirements + }, + location, + linear: true, // Buffers are always linear + allocation_scheme: gpu_allocator::vulkan::AllocationScheme::GpuAllocatorManaged, + }) + .inspect_err(|_| { + unsafe { self.shared.raw.destroy_buffer(raw, None) }; + })?; + + unsafe { + self.shared + .raw + .bind_buffer_memory(raw, allocation.memory(), allocation.offset()) + } + .map_err(super::map_host_device_oom_and_ioca_err) + .inspect_err(|_| { + unsafe { self.shared.raw.destroy_buffer(raw, None) }; + })?; + + if let Some(label) = desc.label { + unsafe { self.shared.set_object_name(raw, label) }; + } + + self.counters.buffer_memory.add(allocation.size() as isize); + self.counters.buffers.add(1); + + Ok(super::Buffer { + raw, + ownership: super::BufferOwnership::Managed(Mutex::new( + super::BufferMemoryBacking::Managed(allocation), + )), + }) + } + unsafe fn destroy_buffer(&self, buffer: super::Buffer) { + match buffer.ownership { + super::BufferOwnership::Managed(allocation) => { + unsafe { self.shared.raw.destroy_buffer(buffer.raw, None) }; + let allocation = allocation.into_inner(); + self.counters.buffer_memory.sub(allocation.size() as isize); + match allocation { + super::BufferMemoryBacking::Managed(allocation) => { + let result = self.mem_allocator.lock().free(allocation); + if let Err(err) = result { + log::warn!("Failed to free buffer allocation: {err}"); + } + } + super::BufferMemoryBacking::VulkanMemory { memory, .. } => unsafe { + self.shared.raw.free_memory(memory, None); + }, + } + } + super::BufferOwnership::RawHandle => { + unsafe { self.shared.raw.destroy_buffer(buffer.raw, None) }; + } + super::BufferOwnership::External(_drop_guard) => { + // The caller owns the `vk::Buffer` and its memory. Dropping + // `_drop_guard` at the end of this arm runs the cleanup callback. + } + } + + self.counters.buffers.sub(1); + } + + unsafe fn add_raw_buffer(&self, _buffer: &super::Buffer) { + self.counters.buffers.add(1); + } + + unsafe fn map_buffer( + &self, + buffer: &super::Buffer, + range: crate::MemoryRange, + ) -> Result<crate::BufferMapping, crate::DeviceError> { + let super::BufferOwnership::Managed(ref allocation) = buffer.ownership else { + crate::hal_usage_error("tried to map external buffer") + }; + let mut allocation = allocation.lock(); + let super::BufferMemoryBacking::Managed(ref mut allocation) = *allocation else { + crate::hal_usage_error("tried to map externally created buffer") + }; + let is_coherent = allocation + .memory_properties() + .contains(vk::MemoryPropertyFlags::HOST_COHERENT); + Ok(crate::BufferMapping { + ptr: unsafe { + allocation + .mapped_ptr() + .unwrap() + .cast() + .offset(range.start as isize) + }, + is_coherent, + }) + } + + unsafe fn unmap_buffer(&self, buffer: &super::Buffer) { + match buffer.ownership { + super::BufferOwnership::Managed(_) => { + // gpu-allocator maps the buffer when allocated and unmaps it when free'd + } + super::BufferOwnership::RawHandle | super::BufferOwnership::External(_) => { + crate::hal_usage_error("tried to unmap external buffer") + } + } + } + + unsafe fn flush_mapped_ranges<I>(&self, buffer: &super::Buffer, ranges: I) + where + I: Iterator<Item = crate::MemoryRange>, + { + if let Some(vk_ranges) = self.shared.make_memory_ranges(buffer, ranges) { + unsafe { + self.shared + .raw + .flush_mapped_memory_ranges( + &smallvec::SmallVec::<[vk::MappedMemoryRange; 32]>::from_iter(vk_ranges), + ) + } + .unwrap(); + } + } + unsafe fn invalidate_mapped_ranges<I>(&self, buffer: &super::Buffer, ranges: I) + where + I: Iterator<Item = crate::MemoryRange>, + { + if let Some(vk_ranges) = self.shared.make_memory_ranges(buffer, ranges) { + unsafe { + self.shared + .raw + .invalidate_mapped_memory_ranges(&smallvec::SmallVec::< + [vk::MappedMemoryRange; 32], + >::from_iter(vk_ranges)) + } + .unwrap(); + } + } + + unsafe fn create_texture( + &self, + desc: &crate::TextureDescriptor, + ) -> Result<super::Texture, crate::DeviceError> { + let image = self.create_image_without_memory(desc, None)?; + + self.error_if_would_oom_on_resource_allocation( + gpu_allocator::MemoryLocation::GpuOnly, + &image.requirements, + ) + .inspect_err(|_| { + unsafe { self.shared.raw.destroy_image(image.raw, None) }; + })?; + + let name = desc.label.unwrap_or("Unlabeled texture"); + + let allocation = self + .mem_allocator + .lock() + .allocate(&gpu_allocator::vulkan::AllocationCreateDesc { + name, + requirements: vk::MemoryRequirements { + memory_type_bits: image.requirements.memory_type_bits + & self.valid_ash_memory_types, + ..image.requirements + }, + location: gpu_allocator::MemoryLocation::GpuOnly, + linear: false, + allocation_scheme: gpu_allocator::vulkan::AllocationScheme::GpuAllocatorManaged, + }) + .inspect_err(|_| { + unsafe { self.shared.raw.destroy_image(image.raw, None) }; + })?; + + self.counters.texture_memory.add(allocation.size() as isize); + + unsafe { + self.shared + .raw + .bind_image_memory(image.raw, allocation.memory(), allocation.offset()) + } + .map_err(super::map_host_device_oom_err) + .inspect_err(|_| { + unsafe { self.shared.raw.destroy_image(image.raw, None) }; + })?; + + Ok(unsafe { + self.texture_from_raw( + image.raw, + desc, + None, + super::TextureMemory::Allocation(allocation), + ) + }) + } + + unsafe fn destroy_texture(&self, texture: super::Texture) { + if texture.drop_guard.is_none() { + unsafe { self.shared.raw.destroy_image(texture.raw, None) }; + } + + match texture.memory { + super::TextureMemory::Allocation(allocation) => { + self.counters.texture_memory.sub(allocation.size() as isize); + let result = self.mem_allocator.lock().free(allocation); + if let Err(err) = result { + log::warn!("Failed to free texture allocation: {err}"); + } + } + super::TextureMemory::Dedicated(memory) => unsafe { + self.shared.raw.free_memory(memory, None); + }, + super::TextureMemory::External => {} + } + + self.counters.textures.sub(1); + } + + unsafe fn add_raw_texture(&self, _texture: &super::Texture) { + self.counters.textures.add(1); + } + + unsafe fn create_texture_view( + &self, + texture: &super::Texture, + desc: &crate::TextureViewDescriptor, + ) -> Result<super::TextureView, crate::DeviceError> { + let subresource_range = conv::map_subresource_range(&desc.range, texture.format); + let raw_format = self.shared.private_caps.map_texture_format(desc.format); + let mut vk_info = vk::ImageViewCreateInfo::default() + .flags(vk::ImageViewCreateFlags::empty()) + .image(texture.raw) + .view_type(conv::map_view_dimension(desc.dimension)) + .format(raw_format) + .subresource_range(subresource_range); + let layers = + NonZeroU32::new(subresource_range.layer_count).expect("Unexpected zero layer count"); + + let mut image_view_info; + if self.shared.private_caps.image_view_usage && !desc.usage.is_empty() { + image_view_info = + vk::ImageViewUsageCreateInfo::default().usage(conv::map_texture_usage(desc.usage)); + vk_info = vk_info.push_next(&mut image_view_info); + } + + let raw = unsafe { self.shared.raw.create_image_view(&vk_info, None) } + .map_err(super::map_host_device_oom_and_ioca_err)?; + + if let Some(label) = desc.label { + unsafe { self.shared.set_object_name(raw, label) }; + } + + let identity = self.shared.texture_view_identity_factory.next(); + + self.counters.texture_views.add(1); + + Ok(super::TextureView { + raw_texture: texture.raw, + raw, + _layers: layers, + format: desc.format, + raw_format, + base_mip_level: desc.range.base_mip_level, + dimension: desc.dimension, + texture_identity: texture.identity, + view_identity: identity, + }) + } + unsafe fn destroy_texture_view(&self, view: super::TextureView) { + unsafe { self.shared.raw.destroy_image_view(view.raw, None) }; + + self.counters.texture_views.sub(1); + } + + unsafe fn create_sampler( + &self, + desc: &crate::SamplerDescriptor, + ) -> Result<super::Sampler, crate::DeviceError> { + let mut create_info = vk::SamplerCreateInfo::default() + .flags(vk::SamplerCreateFlags::empty()) + .mag_filter(conv::map_filter_mode(desc.mag_filter)) + .min_filter(conv::map_filter_mode(desc.min_filter)) + .mipmap_mode(conv::map_mip_filter_mode(desc.mipmap_filter)) + .address_mode_u(conv::map_address_mode(desc.address_modes[0])) + .address_mode_v(conv::map_address_mode(desc.address_modes[1])) + .address_mode_w(conv::map_address_mode(desc.address_modes[2])) + .min_lod(desc.lod_clamp.start) + .max_lod(desc.lod_clamp.end); + + if let Some(fun) = desc.compare { + create_info = create_info + .compare_enable(true) + .compare_op(conv::map_comparison(fun)); + } + + if desc.anisotropy_clamp != 1 { + // We only enable anisotropy if it is supported, and wgpu-hal interface guarantees + // the clamp is in the range [1, 16] which is always supported if anisotropy is. + create_info = create_info + .anisotropy_enable(true) + .max_anisotropy(desc.anisotropy_clamp as f32); + } + + if let Some(color) = desc.border_color { + create_info = create_info.border_color(conv::map_border_color(color)); + } + + let mut sampler_cache_guard = self.shared.sampler_cache.lock(); + + let raw = sampler_cache_guard.create_sampler(&self.shared.raw, create_info)?; + + // Note: Cached samplers will just continually overwrite the label + // + // https://github.com/gfx-rs/wgpu/issues/6867 + if let Some(label) = desc.label { + // SAFETY: we are holding a lock on the sampler cache, + // so we can only be setting the name from one thread. + unsafe { self.shared.set_object_name(raw, label) }; + } + + drop(sampler_cache_guard); + + self.counters.samplers.add(1); + + Ok(super::Sampler { raw, create_info }) + } + unsafe fn destroy_sampler(&self, sampler: super::Sampler) { + self.shared.sampler_cache.lock().destroy_sampler( + &self.shared.raw, + sampler.create_info, + sampler.raw, + ); + + self.counters.samplers.sub(1); + } + + unsafe fn create_command_encoder( + &self, + desc: &crate::CommandEncoderDescriptor<super::Queue>, + ) -> Result<super::CommandEncoder, crate::DeviceError> { + let vk_info = vk::CommandPoolCreateInfo::default() + .queue_family_index(desc.queue.family_index) + .flags(vk::CommandPoolCreateFlags::TRANSIENT); + + let raw = unsafe { + self.shared + .raw + .create_command_pool(&vk_info, None) + .map_err(super::map_host_device_oom_err)? + }; + + self.counters.command_encoders.add(1); + + Ok(super::CommandEncoder { + raw, + device: Arc::clone(&self.shared), + active: vk::CommandBuffer::null(), + bind_point: vk::PipelineBindPoint::default(), + temp: super::Temp::default(), + free: Vec::new(), + discarded: Vec::new(), + rpass_debug_marker_active: false, + end_of_pass_timer_query: None, + framebuffers: Default::default(), + temp_texture_views: Default::default(), + counters: Arc::clone(&self.counters), + current_pipeline_is_multiview: false, + }) + } + + unsafe fn create_bind_group_layout( + &self, + desc: &crate::BindGroupLayoutDescriptor, + ) -> Result<super::BindGroupLayout, crate::DeviceError> { + // Iterate through the entries and accumulate our Vulkan + // DescriptorSetLayoutBindings and DescriptorBindingFlags, as well as + // our binding map and our descriptor counts. + // Note: not bothering with on stack arrays here as it's low frequency + let mut vk_bindings = Vec::new(); + let mut binding_flags = Vec::new(); + let mut binding_map = Vec::new(); + let mut next_binding = 0; + let mut contains_binding_arrays = false; + let mut desc_count = DescriptorCounts::default(); + for entry in desc.entries { + if entry.count.is_some() { + contains_binding_arrays = true; + } + + let partially_bound = desc + .flags + .contains(crate::BindGroupLayoutFlags::PARTIALLY_BOUND); + let mut flags = vk::DescriptorBindingFlags::empty(); + if partially_bound && entry.count.is_some() { + flags |= vk::DescriptorBindingFlags::PARTIALLY_BOUND; + } + if entry.count.is_some() { + flags |= vk::DescriptorBindingFlags::UPDATE_AFTER_BIND; + } + + let count = entry.count.map_or(1, |c| c.get()); + match entry.ty { + wgt::BindingType::ExternalTexture => unimplemented!(), + _ => { + vk_bindings.push(vk::DescriptorSetLayoutBinding { + binding: next_binding, + descriptor_type: conv::map_binding_type(entry.ty), + descriptor_count: count, + stage_flags: conv::map_shader_stage(entry.visibility), + p_immutable_samplers: ptr::null(), + _marker: Default::default(), + }); + binding_flags.push(flags); + binding_map.push(( + entry.binding, + super::BindingInfo { + binding: next_binding, + binding_array_size: entry.count, + }, + )); + next_binding += 1; + } + } + + match entry.ty { + wgt::BindingType::Buffer { + ty, + has_dynamic_offset, + .. + } => match ty { + wgt::BufferBindingType::Uniform => { + if has_dynamic_offset { + desc_count.uniform_buffer_dynamic += count; + } else { + desc_count.uniform_buffer += count; + } + } + wgt::BufferBindingType::Storage { .. } => { + if has_dynamic_offset { + desc_count.storage_buffer_dynamic += count; + } else { + desc_count.storage_buffer += count; + } + } + }, + wgt::BindingType::Sampler { .. } => { + desc_count.sampler += count; + } + wgt::BindingType::Texture { .. } => { + desc_count.sampled_image += count; + } + wgt::BindingType::StorageTexture { .. } => { + desc_count.storage_image += count; + } + wgt::BindingType::AccelerationStructure { .. } => { + desc_count.acceleration_structure += count; + } + wgt::BindingType::ExternalTexture => unimplemented!(), + } + } + + let vk_info = vk::DescriptorSetLayoutCreateInfo::default() + .bindings(&vk_bindings) + .flags(if contains_binding_arrays { + vk::DescriptorSetLayoutCreateFlags::UPDATE_AFTER_BIND_POOL + } else { + vk::DescriptorSetLayoutCreateFlags::empty() + }); + + let mut binding_flag_info = + vk::DescriptorSetLayoutBindingFlagsCreateInfo::default().binding_flags(&binding_flags); + + let vk_info = vk_info.push_next(&mut binding_flag_info); + + let raw = unsafe { + self.shared + .raw + .create_descriptor_set_layout(&vk_info, None) + .map_err(super::map_host_device_oom_err)? + }; + + if let Some(label) = desc.label { + unsafe { self.shared.set_object_name(raw, label) }; + } + + let layout = super::BindGroupLayout { + raw, + desc_count, + entries: desc.entries.into(), + binding_map, + contains_binding_arrays, + }; + + let result = self + .desc_allocator + .lock() + .register_layout(&self.shared.raw, &layout); + if let Err(err) = result { + unsafe { + self.shared + .raw + .destroy_descriptor_set_layout(layout.raw, None) + }; + return Err(err); + } + + self.counters.bind_group_layouts.add(1); + + Ok(layout) + } + unsafe fn destroy_bind_group_layout(&self, bg_layout: super::BindGroupLayout) { + self.desc_allocator + .lock() + .unregister_layout(&self.shared.raw, &bg_layout); + + unsafe { + self.shared + .raw + .destroy_descriptor_set_layout(bg_layout.raw, None) + }; + + self.counters.bind_group_layouts.sub(1); + } + + unsafe fn create_pipeline_layout( + &self, + desc: &crate::PipelineLayoutDescriptor<super::BindGroupLayout>, + ) -> Result<super::PipelineLayout, crate::DeviceError> { + //Note: not bothering with on stack array here as it's low frequency + let vk_set_layouts = desc + .bind_group_layouts + .iter() + .map(|bgl| match bgl { + Some(bgl) => bgl.raw, + None => { + // `VUID-VkPipelineLayoutCreateInfo-pSetLayouts-parameter` + // says `VK_NULL_HANDLE` is allowed but + // `VUID-VkPipelineLayoutCreateInfo-graphicsPipelineLibrary-06753` + // says it's not, unless the `graphicsPipelineLibrary` + // feature is enabled. + // + // We use an empty descriptor set layout to work around this. + self.shared.empty_descriptor_set_layout + } + }) + .collect::<Vec<_>>(); + let vk_immediates_ranges: Option<vk::PushConstantRange> = if desc.immediate_size != 0 { + Some(vk::PushConstantRange { + stage_flags: vk::ShaderStageFlags::ALL, + offset: 0, + size: desc.immediate_size, + }) + } else { + None + }; + + let vk_info = vk::PipelineLayoutCreateInfo::default() + .flags(vk::PipelineLayoutCreateFlags::empty()) + .set_layouts(&vk_set_layouts) + .push_constant_ranges(vk_immediates_ranges.as_slice()); + + let raw = { + profiling::scope!("vkCreatePipelineLayout"); + unsafe { + self.shared + .raw + .create_pipeline_layout(&vk_info, None) + .map_err(super::map_host_device_oom_err)? + } + }; + + if let Some(label) = desc.label { + unsafe { self.shared.set_object_name(raw, label) }; + } + + let mut binding_map = BTreeMap::new(); + for (group, layout) in desc.bind_group_layouts.iter().enumerate() { + let Some(layout) = layout else { + continue; + }; + + for &(binding, binding_info) in &layout.binding_map { + binding_map.insert( + naga::ResourceBinding { + group: group as u32, + binding, + }, + naga::back::spv::BindingInfo { + descriptor_set: group as u32, + binding: binding_info.binding, + binding_array_size: binding_info.binding_array_size.map(NonZeroU32::get), + }, + ); + } + } + + self.counters.pipeline_layouts.add(1); + Ok(super::PipelineLayout { raw, binding_map }) + } + unsafe fn destroy_pipeline_layout(&self, pipeline_layout: super::PipelineLayout) { + unsafe { + self.shared + .raw + .destroy_pipeline_layout(pipeline_layout.raw, None) + }; + + self.counters.pipeline_layouts.sub(1); + } + + unsafe fn create_bind_group( + &self, + desc: &crate::BindGroupDescriptor< + super::BindGroupLayout, + super::Buffer, + super::Sampler, + super::TextureView, + super::AccelerationStructure, + >, + ) -> Result<super::BindGroup, crate::DeviceError> { + let set = unsafe { + self.desc_allocator + .lock() + .alloc(&self.shared.raw, desc.layout)? + }; + + if let Some(label) = desc.label { + unsafe { self.shared.set_object_name(set.raw(), label) }; + } + + /// Helper for splitting off and initializing a given number of elements on a pre-allocated + /// stack, based on items returned from an [`ExactSizeIterator`]. Typically created from a + /// [`MaybeUninit`] slice (see [`Vec::spare_capacity_mut()`]). + /// The updated [`ExtensionStack`] of remaining uninitialized elements is returned, safely + /// representing that the initialized and remaining elements are two independent mutable + /// borrows. + struct ExtendStack<'a, T> { + remainder: &'a mut [MaybeUninit<T>], + } + + impl<'a, T> ExtendStack<'a, T> { + fn from_vec_capacity(vec: &'a mut Vec<T>) -> Self { + Self { + remainder: vec.spare_capacity_mut(), + } + } + + fn extend_one(self, value: T) -> (Self, &'a mut T) { + let (to_init, remainder) = self.remainder.split_first_mut().unwrap(); + let init = to_init.write(value); + (Self { remainder }, init) + } + + fn extend( + self, + iter: impl IntoIterator<Item = T> + ExactSizeIterator, + ) -> (Self, &'a mut [T]) { + let (to_init, remainder) = self.remainder.split_at_mut(iter.len()); + + for (value, to_init) in iter.into_iter().zip(to_init.iter_mut()) { + to_init.write(value); + } + + // we can't use the safe (yet unstable) MaybeUninit::write_slice() here because of having an iterator to write + + let init = { + // SAFETY: The loop above has initialized exactly as many items as to_init is + // long, so it is safe to cast away the MaybeUninit<T> wrapper into T. + + // Additional safety docs from unstable slice_assume_init_mut + // SAFETY: similar to safety notes for `slice_get_ref`, but we have a + // mutable reference which is also guaranteed to be valid for writes. + unsafe { mem::transmute::<&mut [MaybeUninit<T>], &mut [T]>(to_init) } + }; + (Self { remainder }, init) + } + } + + let mut writes = Vec::with_capacity(desc.entries.len()); + let mut buffer_infos = Vec::with_capacity(desc.buffers.len()); + let mut buffer_infos = ExtendStack::from_vec_capacity(&mut buffer_infos); + let mut image_infos = Vec::with_capacity(desc.samplers.len() + desc.textures.len()); + let mut image_infos = ExtendStack::from_vec_capacity(&mut image_infos); + // TODO: This length could be reduced to just the number of top-level acceleration + // structure bindings, where multiple consecutive TLAS bindings that are set via + // one `WriteDescriptorSet` count towards one "info" struct, not the total number of + // acceleration structure bindings to write: + let mut acceleration_structure_infos = + Vec::with_capacity(desc.acceleration_structures.len()); + let mut acceleration_structure_infos = + ExtendStack::from_vec_capacity(&mut acceleration_structure_infos); + let mut raw_acceleration_structures = + Vec::with_capacity(desc.acceleration_structures.len()); + let mut raw_acceleration_structures = + ExtendStack::from_vec_capacity(&mut raw_acceleration_structures); + + let layout_and_entry_iter = desc.entries.iter().map(|entry| { + let layout = desc + .layout + .entries + .iter() + .find(|layout_entry| layout_entry.binding == entry.binding) + .expect("internal error: no layout entry found with binding slot"); + (layout, entry) + }); + let mut next_binding = 0; + for (layout, entry) in layout_and_entry_iter { + let write = vk::WriteDescriptorSet::default().dst_set(set.raw()); + + match layout.ty { + wgt::BindingType::Sampler(_) => { + let start = entry.resource_index; + let end = start + entry.count; + let local_image_infos; + (image_infos, local_image_infos) = + image_infos.extend(desc.samplers[start as usize..end as usize].iter().map( + |sampler| vk::DescriptorImageInfo::default().sampler(sampler.raw), + )); + writes.push( + write + .dst_binding(next_binding) + .descriptor_type(conv::map_binding_type(layout.ty)) + .image_info(local_image_infos), + ); + next_binding += 1; + } + wgt::BindingType::Texture { .. } | wgt::BindingType::StorageTexture { .. } => { + let start = entry.resource_index; + let end = start + entry.count; + let local_image_infos; + (image_infos, local_image_infos) = + image_infos.extend(desc.textures[start as usize..end as usize].iter().map( + |binding| { + let layout = + conv::derive_image_layout(binding.usage, binding.view.format); + vk::DescriptorImageInfo::default() + .image_view(binding.view.raw) + .image_layout(layout) + }, + )); + writes.push( + write + .dst_binding(next_binding) + .descriptor_type(conv::map_binding_type(layout.ty)) + .image_info(local_image_infos), + ); + next_binding += 1; + } + wgt::BindingType::Buffer { .. } => { + let start = entry.resource_index; + let end = start + entry.count; + let local_buffer_infos; + (buffer_infos, local_buffer_infos) = + buffer_infos.extend(desc.buffers[start as usize..end as usize].iter().map( + |binding| { + vk::DescriptorBufferInfo::default() + .buffer(binding.buffer.raw) + .offset(binding.offset) + .range( + binding.size.map_or(vk::WHOLE_SIZE, wgt::BufferSize::get), + ) + }, + )); + writes.push( + write + .dst_binding(next_binding) + .descriptor_type(conv::map_binding_type(layout.ty)) + .buffer_info(local_buffer_infos), + ); + next_binding += 1; + } + wgt::BindingType::AccelerationStructure { .. } => { + let start = entry.resource_index; + let end = start + entry.count; + + let local_raw_acceleration_structures; + ( + raw_acceleration_structures, + local_raw_acceleration_structures, + ) = raw_acceleration_structures.extend( + desc.acceleration_structures[start as usize..end as usize] + .iter() + .map(|acceleration_structure| acceleration_structure.raw), + ); + + let local_acceleration_structure_infos; + ( + acceleration_structure_infos, + local_acceleration_structure_infos, + ) = acceleration_structure_infos.extend_one( + vk::WriteDescriptorSetAccelerationStructureKHR::default() + .acceleration_structures(local_raw_acceleration_structures), + ); + + writes.push( + write + .dst_binding(next_binding) + .descriptor_type(conv::map_binding_type(layout.ty)) + .descriptor_count(entry.count) + .push_next(local_acceleration_structure_infos), + ); + next_binding += 1; + } + wgt::BindingType::ExternalTexture => unimplemented!(), + } + } + + unsafe { self.shared.raw.update_descriptor_sets(&writes, &[]) }; + + self.counters.bind_groups.add(1); + + Ok(super::BindGroup { set }) + } + + unsafe fn destroy_bind_group(&self, group: super::BindGroup) { + unsafe { self.desc_allocator.lock().free(&self.shared.raw, group.set) }; + + self.counters.bind_groups.sub(1); + } + + unsafe fn create_shader_module( + &self, + desc: &crate::ShaderModuleDescriptor, + shader: crate::ShaderInput, + ) -> Result<super::ShaderModule, crate::ShaderError> { + let shader_module = match shader { + crate::ShaderInput::Naga(naga_shader) + if self + .shared + .workarounds + .contains(super::Workarounds::SEPARATE_ENTRY_POINTS) + || !naga_shader.module.overrides.is_empty() => + { + super::ShaderModule::Intermediate { + naga_shader, + runtime_checks: desc.runtime_checks, + } + } + crate::ShaderInput::Naga(naga_shader) => { + let mut naga_options = self.naga_options.clone(); + naga_options.debug_info = + naga_shader + .debug_source + .as_ref() + .map(|d| naga::back::spv::DebugInfo { + source_code: d.source_code.as_ref(), + file_name: d.file_name.as_ref(), + language: naga::back::spv::SourceLanguage::WGSL, + }); + if !desc.runtime_checks.bounds_checks { + naga_options.bounds_check_policies = naga::proc::BoundsCheckPolicies { + index: naga::proc::BoundsCheckPolicy::Unchecked, + buffer: naga::proc::BoundsCheckPolicy::Unchecked, + image_load: naga::proc::BoundsCheckPolicy::Unchecked, + binding_array: naga::proc::BoundsCheckPolicy::Unchecked, + }; + } + let spv = naga::back::spv::write_vec( + &naga_shader.module, + &naga_shader.info, + &naga_options, + None, + ) + .map_err(|e| crate::ShaderError::Compilation(format!("{e}")))?; + super::ShaderModule::Raw(self.create_shader_module_impl(&spv, &desc.label)?) + } + crate::ShaderInput::SpirV(data) => { + super::ShaderModule::Raw(self.create_shader_module_impl(data, &desc.label)?) + } + crate::ShaderInput::MetalLib { .. } + | crate::ShaderInput::Msl { .. } + | crate::ShaderInput::Dxil { .. } + | crate::ShaderInput::Hlsl { .. } + | crate::ShaderInput::Glsl { .. } => unreachable!(), + }; + + self.counters.shader_modules.add(1); + + Ok(shader_module) + } + + unsafe fn destroy_shader_module(&self, module: super::ShaderModule) { + match module { + super::ShaderModule::Raw(raw) => { + unsafe { self.shared.raw.destroy_shader_module(raw, None) }; + } + super::ShaderModule::Intermediate { .. } => {} + } + + self.counters.shader_modules.sub(1); + } + + unsafe fn create_render_pipeline( + &self, + desc: &crate::RenderPipelineDescriptor< + super::PipelineLayout, + super::ShaderModule, + super::PipelineCache, + >, + ) -> Result<super::RenderPipeline, crate::PipelineError> { + let dynamic_states = [ + vk::DynamicState::VIEWPORT, + vk::DynamicState::SCISSOR, + vk::DynamicState::BLEND_CONSTANTS, + vk::DynamicState::STENCIL_REFERENCE, + ]; + let mut compatible_rp_key = super::RenderPassKey { + sample_count: desc.multisample.count, + multiview_mask: desc.multiview_mask, + ..Default::default() + }; + let mut stages = ArrayVec::<_, { crate::MAX_CONCURRENT_SHADER_STAGES }>::new(); + let mut vertex_buffers = Vec::new(); + let mut vertex_attributes = Vec::new(); + + if let crate::VertexProcessor::Standard { + vertex_buffers: desc_vertex_buffers, + vertex_stage: _, + } = &desc.vertex_processor + { + vertex_buffers = Vec::with_capacity(desc_vertex_buffers.len()); + for (i, vb) in desc_vertex_buffers.iter().enumerate() { + let Some(vb) = vb else { + continue; + }; + vertex_buffers.push(vk::VertexInputBindingDescription { + binding: i as u32, + stride: vb.array_stride as u32, + input_rate: match vb.step_mode { + wgt::VertexStepMode::Vertex => vk::VertexInputRate::VERTEX, + wgt::VertexStepMode::Instance => vk::VertexInputRate::INSTANCE, + }, + }); + for at in vb.attributes { + vertex_attributes.push(vk::VertexInputAttributeDescription { + location: at.shader_location, + binding: i as u32, + format: conv::map_vertex_format(at.format), + offset: at.offset as u32, + }); + } + } + } + + let vk_vertex_input = vk::PipelineVertexInputStateCreateInfo::default() + .vertex_binding_descriptions(&vertex_buffers) + .vertex_attribute_descriptions(&vertex_attributes); + + let vk_input_assembly = vk::PipelineInputAssemblyStateCreateInfo::default() + .topology(conv::map_topology(desc.primitive.topology)) + .primitive_restart_enable(desc.primitive.strip_index_format.is_some()); + + let mut compiled_vs = None; + let mut compiled_ms = None; + let mut compiled_ts = None; + match &desc.vertex_processor { + crate::VertexProcessor::Standard { + vertex_buffers: _, + vertex_stage, + } => { + compiled_vs = Some(self.compile_stage( + vertex_stage, + naga::ShaderStage::Vertex, + &desc.layout.binding_map, + )?); + stages.push(compiled_vs.as_ref().unwrap().create_info); + } + crate::VertexProcessor::Mesh { + task_stage, + mesh_stage, + } => { + if let Some(t) = task_stage.as_ref() { + compiled_ts = Some(self.compile_stage( + t, + naga::ShaderStage::Task, + &desc.layout.binding_map, + )?); + stages.push(compiled_ts.as_ref().unwrap().create_info); + } + compiled_ms = Some(self.compile_stage( + mesh_stage, + naga::ShaderStage::Mesh, + &desc.layout.binding_map, + )?); + stages.push(compiled_ms.as_ref().unwrap().create_info); + } + } + let compiled_fs = match desc.fragment_stage { + Some(ref stage) => { + let compiled = self.compile_stage( + stage, + naga::ShaderStage::Fragment, + &desc.layout.binding_map, + )?; + stages.push(compiled.create_info); + Some(compiled) + } + None => None, + }; + + let mut vk_rasterization = vk::PipelineRasterizationStateCreateInfo::default() + .polygon_mode(conv::map_polygon_mode(desc.primitive.polygon_mode)) + .front_face(conv::map_front_face(desc.primitive.front_face)) + .line_width(1.0) + .depth_clamp_enable(desc.primitive.unclipped_depth); + if let Some(face) = desc.primitive.cull_mode { + vk_rasterization = vk_rasterization.cull_mode(conv::map_cull_face(face)) + } + let mut vk_rasterization_conservative_state = + vk::PipelineRasterizationConservativeStateCreateInfoEXT::default() + .conservative_rasterization_mode( + vk::ConservativeRasterizationModeEXT::OVERESTIMATE, + ); + if desc.primitive.conservative { + vk_rasterization = vk_rasterization.push_next(&mut vk_rasterization_conservative_state); + } + + let mut vk_depth_stencil = vk::PipelineDepthStencilStateCreateInfo::default(); + if let Some(ref ds) = desc.depth_stencil { + let vk_format = self.shared.private_caps.map_texture_format(ds.format); + let vk_layout = if ds.is_read_only(desc.primitive.cull_mode) { + vk::ImageLayout::DEPTH_STENCIL_READ_ONLY_OPTIMAL + } else { + vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL + }; + compatible_rp_key.depth_stencil = Some(super::DepthStencilAttachmentKey { + base: super::AttachmentKey::compatible(vk_format, vk_layout), + stencil_ops: crate::AttachmentOps::all(), + }); + + if ds.is_depth_enabled() { + vk_depth_stencil = vk_depth_stencil + .depth_test_enable(true) + .depth_write_enable(ds.depth_write_enabled.unwrap_or_default()) + .depth_compare_op(conv::map_comparison(ds.depth_compare.unwrap_or_default())); + } + if ds.stencil.is_enabled() { + let s = &ds.stencil; + let front = conv::map_stencil_face(&s.front, s.read_mask, s.write_mask); + let back = conv::map_stencil_face(&s.back, s.read_mask, s.write_mask); + vk_depth_stencil = vk_depth_stencil + .stencil_test_enable(true) + .front(front) + .back(back); + } + + if ds.bias.is_enabled() { + vk_rasterization = vk_rasterization + .depth_bias_enable(true) + .depth_bias_constant_factor(ds.bias.constant as f32) + .depth_bias_clamp(ds.bias.clamp) + .depth_bias_slope_factor(ds.bias.slope_scale); + } + } + + let vk_viewport = vk::PipelineViewportStateCreateInfo::default() + .flags(vk::PipelineViewportStateCreateFlags::empty()) + .scissor_count(1) + .viewport_count(1); + + let vk_sample_mask = [ + desc.multisample.mask as u32, + (desc.multisample.mask >> 32) as u32, + ]; + let vk_multisample = vk::PipelineMultisampleStateCreateInfo::default() + .rasterization_samples(vk::SampleCountFlags::from_raw(desc.multisample.count)) + .alpha_to_coverage_enable(desc.multisample.alpha_to_coverage_enabled) + .sample_mask(&vk_sample_mask); + + let mut vk_attachments = Vec::with_capacity(desc.color_targets.len()); + for cat in desc.color_targets { + let (key, attarchment) = if let Some(cat) = cat.as_ref() { + let mut vk_attachment = vk::PipelineColorBlendAttachmentState::default() + .color_write_mask(vk::ColorComponentFlags::from_raw(cat.write_mask.bits())); + if let Some(ref blend) = cat.blend { + let (color_op, color_src, color_dst) = conv::map_blend_component(&blend.color); + let (alpha_op, alpha_src, alpha_dst) = conv::map_blend_component(&blend.alpha); + vk_attachment = vk_attachment + .blend_enable(true) + .color_blend_op(color_op) + .src_color_blend_factor(color_src) + .dst_color_blend_factor(color_dst) + .alpha_blend_op(alpha_op) + .src_alpha_blend_factor(alpha_src) + .dst_alpha_blend_factor(alpha_dst); + } + + let vk_format = self.shared.private_caps.map_texture_format(cat.format); + ( + Some(super::ColorAttachmentKey { + base: super::AttachmentKey::compatible( + vk_format, + vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL, + ), + resolve: None, + }), + vk_attachment, + ) + } else { + (None, vk::PipelineColorBlendAttachmentState::default()) + }; + + compatible_rp_key.colors.push(key); + vk_attachments.push(attarchment); + } + + let vk_color_blend = + vk::PipelineColorBlendStateCreateInfo::default().attachments(&vk_attachments); + + let vk_dynamic_state = + vk::PipelineDynamicStateCreateInfo::default().dynamic_states(&dynamic_states); + + let raw_pass = self.shared.make_render_pass(compatible_rp_key)?; + + let vk_infos = [{ + vk::GraphicsPipelineCreateInfo::default() + .layout(desc.layout.raw) + .stages(&stages) + .vertex_input_state(&vk_vertex_input) + .input_assembly_state(&vk_input_assembly) + .rasterization_state(&vk_rasterization) + .viewport_state(&vk_viewport) + .multisample_state(&vk_multisample) + .depth_stencil_state(&vk_depth_stencil) + .color_blend_state(&vk_color_blend) + .dynamic_state(&vk_dynamic_state) + .render_pass(raw_pass) + }]; + + let pipeline_cache = desc + .cache + .map(|it| it.raw) + .unwrap_or(vk::PipelineCache::null()); + + let mut raw_vec = { + profiling::scope!("vkCreateGraphicsPipelines"); + unsafe { + self.shared + .raw + .create_graphics_pipelines(pipeline_cache, &vk_infos, None) + .map_err(|(_, e)| super::map_pipeline_err(e)) + }? + }; + + let raw = raw_vec.pop().unwrap(); + if let Some(label) = desc.label { + unsafe { self.shared.set_object_name(raw, label) }; + } + + if let Some(CompiledStage { + temp_raw_module: Some(raw_module), + .. + }) = compiled_vs + { + unsafe { self.shared.raw.destroy_shader_module(raw_module, None) }; + } + if let Some(CompiledStage { + temp_raw_module: Some(raw_module), + .. + }) = compiled_ts + { + unsafe { self.shared.raw.destroy_shader_module(raw_module, None) }; + } + if let Some(CompiledStage { + temp_raw_module: Some(raw_module), + .. + }) = compiled_ms + { + unsafe { self.shared.raw.destroy_shader_module(raw_module, None) }; + } + if let Some(CompiledStage { + temp_raw_module: Some(raw_module), + .. + }) = compiled_fs + { + unsafe { self.shared.raw.destroy_shader_module(raw_module, None) }; + } + + self.counters.render_pipelines.add(1); + + Ok(super::RenderPipeline { + raw, + is_multiview: desc.multiview_mask.is_some(), + }) + } + + unsafe fn destroy_render_pipeline(&self, pipeline: super::RenderPipeline) { + unsafe { self.shared.raw.destroy_pipeline(pipeline.raw, None) }; + + self.counters.render_pipelines.sub(1); + } + + unsafe fn create_compute_pipeline( + &self, + desc: &crate::ComputePipelineDescriptor< + super::PipelineLayout, + super::ShaderModule, + super::PipelineCache, + >, + ) -> Result<super::ComputePipeline, crate::PipelineError> { + let compiled = self.compile_stage( + &desc.stage, + naga::ShaderStage::Compute, + &desc.layout.binding_map, + )?; + + let vk_infos = [{ + vk::ComputePipelineCreateInfo::default() + .layout(desc.layout.raw) + .stage(compiled.create_info) + }]; + + let pipeline_cache = desc + .cache + .map(|it| it.raw) + .unwrap_or(vk::PipelineCache::null()); + + let mut raw_vec = { + profiling::scope!("vkCreateComputePipelines"); + unsafe { + self.shared + .raw + .create_compute_pipelines(pipeline_cache, &vk_infos, None) + .map_err(|(_, e)| super::map_pipeline_err(e)) + }? + }; + + let raw = raw_vec.pop().unwrap(); + if let Some(label) = desc.label { + unsafe { self.shared.set_object_name(raw, label) }; + } + + if let Some(raw_module) = compiled.temp_raw_module { + unsafe { self.shared.raw.destroy_shader_module(raw_module, None) }; + } + + self.counters.compute_pipelines.add(1); + + Ok(super::ComputePipeline { raw }) + } + + unsafe fn destroy_compute_pipeline(&self, pipeline: super::ComputePipeline) { + unsafe { self.shared.raw.destroy_pipeline(pipeline.raw, None) }; + + self.counters.compute_pipelines.sub(1); + } + + unsafe fn create_ray_tracing_pipeline( + &self, + desc: &crate::RayTracingPipelineDescriptor< + super::PipelineLayout, + super::ShaderModule, + super::PipelineCache, + >, + ) -> Result<super::RayTracingPipeline, crate::PipelineError> { + let mut stages = Vec::new(); + let mut groups = Vec::new(); + + let compiled_ray_gen = self.compile_stage( + &desc.ray_generation, + naga::ShaderStage::RayGeneration, + &desc.layout.binding_map, + )?; + + groups.push( + vk::RayTracingShaderGroupCreateInfoKHR::default() + .closest_hit_shader(vk::SHADER_UNUSED_KHR) + .any_hit_shader(vk::SHADER_UNUSED_KHR) + .intersection_shader(vk::SHADER_UNUSED_KHR) + .general_shader(0) // stages is empty so next index is 0. + .ty(vk::RayTracingShaderGroupTypeKHR::GENERAL), + ); + + stages.push(compiled_ray_gen.create_info); + + let compiled_miss = self.compile_stage( + &desc.miss, + naga::ShaderStage::Miss, + &desc.layout.binding_map, + )?; + + groups.push( + vk::RayTracingShaderGroupCreateInfoKHR::default() + .closest_hit_shader(vk::SHADER_UNUSED_KHR) + .any_hit_shader(vk::SHADER_UNUSED_KHR) + .intersection_shader(vk::SHADER_UNUSED_KHR) + .general_shader(1) // stages always has one element so next index is 1. + .ty(vk::RayTracingShaderGroupTypeKHR::GENERAL), + ); + + stages.push(compiled_miss.create_info); + + // This is to keep alive the CStrings, as the ones in the loop would be deallocated + // causing UB otherwise. + let mut compiled_stages = Vec::new(); + + for group in desc.intersection { + let compiled_closest_hits = self.compile_stage( + &group.closest_hit, + naga::ShaderStage::ClosestHit, + &desc.layout.binding_map, + )?; + + let closest_idx = stages.len(); + + stages.push(compiled_closest_hits.create_info); + + compiled_stages.push(compiled_closest_hits); + + let mut raw_hit: vk::RayTracingShaderGroupCreateInfoKHR<'_> = + vk::RayTracingShaderGroupCreateInfoKHR::default() + .closest_hit_shader(closest_idx as _) + .any_hit_shader(vk::SHADER_UNUSED_KHR) + .intersection_shader(vk::SHADER_UNUSED_KHR) + .general_shader(vk::SHADER_UNUSED_KHR) + .ty(vk::RayTracingShaderGroupTypeKHR::TRIANGLES_HIT_GROUP); + + if let Some(any_hit) = &group.any_hit { + let compiled_any_hit = self.compile_stage( + any_hit, + naga::ShaderStage::AnyHit, + &desc.layout.binding_map, + )?; + + let any_idx = stages.len(); + + stages.push(compiled_any_hit.create_info); + + compiled_stages.push(compiled_any_hit); + + raw_hit = raw_hit.any_hit_shader(any_idx as _); + } + + groups.push(raw_hit); + } + + let create_infos = [{ + vk::RayTracingPipelineCreateInfoKHR::default() + .layout(desc.layout.raw) + .max_pipeline_ray_recursion_depth(desc.max_recursion_depth) + .stages(&stages) + .groups(&groups) + }]; + + let pipeline_cache = desc + .cache + .map(|it| it.raw) + .unwrap_or(vk::PipelineCache::null()); + + let fns = self + .shared + .extension_fns + .ray_tracing_pipelines + .as_ref() + .unwrap(); + let pipelines = unsafe { + fns.create_ray_tracing_pipelines( + vk::DeferredOperationKHR::null(), + pipeline_cache, + &create_infos, + None, + ) + .map_err(|(_, e)| super::map_pipeline_err(e)) + }?; + + if let Some(raw_module) = compiled_ray_gen.temp_raw_module { + unsafe { self.shared.raw.destroy_shader_module(raw_module, None) }; + } + + if let Some(raw_module) = compiled_miss.temp_raw_module { + unsafe { self.shared.raw.destroy_shader_module(raw_module, None) }; + } + + for raw_module in compiled_stages + .into_iter() + .flat_map(|stage| stage.temp_raw_module) + { + unsafe { self.shared.raw.destroy_shader_module(raw_module, None) }; + } + + self.counters.ray_tracing_pipelines.add(1); + + Ok(super::RayTracingPipeline { raw: pipelines[0] }) + } + + unsafe fn destroy_ray_tracing_pipeline(&self, pipeline: super::RayTracingPipeline) { + unsafe { self.shared.raw.destroy_pipeline(pipeline.raw, None) }; + + self.counters.ray_tracing_pipelines.sub(1); + } + + unsafe fn get_raytracing_pipeline_group_data( + &self, + pipeline: &super::RayTracingPipeline, + groups: core::ops::Range<u32>, + ) -> Result<Vec<u8>, crate::DeviceError> { + let fns = self + .shared + .extension_fns + .ray_tracing_pipelines + .as_ref() + .unwrap(); + + let num = groups.end - groups.start; + + unsafe { + fns.get_ray_tracing_shader_group_handles( + pipeline.raw, + groups.start, + num, + (num * self + .shared + .private_caps + .ray_tracing_pipeline_group_data_size) as usize, + ) + } + .map_err(super::map_host_device_oom_err) + } + + unsafe fn create_pipeline_cache( + &self, + desc: &crate::PipelineCacheDescriptor<'_>, + ) -> Result<super::PipelineCache, crate::PipelineCacheError> { + let mut info = vk::PipelineCacheCreateInfo::default(); + if let Some(data) = desc.data { + info = info.initial_data(data) + } + profiling::scope!("vkCreatePipelineCache"); + let raw = unsafe { self.shared.raw.create_pipeline_cache(&info, None) } + .map_err(super::map_host_device_oom_err)?; + + Ok(super::PipelineCache { raw }) + } + fn pipeline_cache_validation_key(&self) -> Option<[u8; 16]> { + Some(self.shared.pipeline_cache_validation_key) + } + unsafe fn destroy_pipeline_cache(&self, cache: super::PipelineCache) { + unsafe { self.shared.raw.destroy_pipeline_cache(cache.raw, None) } + } + unsafe fn create_query_set( + &self, + desc: &wgt::QuerySetDescriptor<crate::Label>, + ) -> Result<super::QuerySet, crate::DeviceError> { + // Assume each query is 256 bytes. This is the case for occlusion + // queries on an AMD W6800 with driver version 32.0.12030.9. The + // size and allocation policy may vary; this is an approximate + // check only. + self.error_if_would_oom_on_resource_allocation( + gpu_allocator::MemoryLocation::GpuToCpu, + &vk::MemoryRequirements { + size: desc.count as u64 * 256, + alignment: 256, + memory_type_bits: self.valid_ash_memory_types, + }, + )?; + + let (vk_type, pipeline_statistics) = match desc.ty { + wgt::QueryType::Occlusion => ( + vk::QueryType::OCCLUSION, + vk::QueryPipelineStatisticFlags::empty(), + ), + wgt::QueryType::PipelineStatistics(statistics) => ( + vk::QueryType::PIPELINE_STATISTICS, + conv::map_pipeline_statistics(statistics), + ), + wgt::QueryType::Timestamp => ( + vk::QueryType::TIMESTAMP, + vk::QueryPipelineStatisticFlags::empty(), + ), + }; + + let vk_info = vk::QueryPoolCreateInfo::default() + .query_type(vk_type) + .query_count(desc.count) + .pipeline_statistics(pipeline_statistics); + + let raw = unsafe { self.shared.raw.create_query_pool(&vk_info, None) } + .map_err(super::map_host_device_oom_err)?; + if let Some(label) = desc.label { + unsafe { self.shared.set_object_name(raw, label) }; + } + + self.counters.query_sets.add(1); + + Ok(super::QuerySet { raw }) + } + + unsafe fn destroy_query_set(&self, set: super::QuerySet) { + unsafe { self.shared.raw.destroy_query_pool(set.raw, None) }; + + self.counters.query_sets.sub(1); + } + + unsafe fn create_fence(&self) -> Result<super::Fence, crate::DeviceError> { + self.counters.fences.add(1); + + Ok(if self.shared.private_caps.timeline_semaphores { + let mut sem_type_info = + vk::SemaphoreTypeCreateInfo::default().semaphore_type(vk::SemaphoreType::TIMELINE); + let vk_info = vk::SemaphoreCreateInfo::default().push_next(&mut sem_type_info); + let raw = unsafe { self.shared.raw.create_semaphore(&vk_info, None) } + .map_err(super::map_host_device_oom_err)?; + + super::Fence::TimelineSemaphore(raw) + } else { + super::Fence::FencePool(RwLock::new(super::FencePool { + last_completed: 0, + active: Vec::new(), + free: Vec::new(), + })) + }) + } + unsafe fn destroy_fence(&self, fence: super::Fence) { + match fence { + super::Fence::TimelineSemaphore(raw) => { + unsafe { self.shared.raw.destroy_semaphore(raw, None) }; + } + super::Fence::FencePool(pool) => { + let super::FencePool { + active, + free, + last_completed: _, + } = pool.into_inner(); + + for (_, raw) in active { + unsafe { + self.shared.raw.destroy_fence(Arc::into_inner(raw).expect("Fence should have its reference count be one by the end of each function"), None) + }; + } + for raw in free { + unsafe { self.shared.raw.destroy_fence(raw, None) }; + } + } + } + + self.counters.fences.sub(1); + } + unsafe fn get_fence_value( + &self, + fence: &super::Fence, + ) -> Result<crate::FenceValue, crate::DeviceError> { + fence.get_latest( + &self.shared.raw, + self.shared.extension_fns.timeline_semaphore.as_ref(), + ) + } + unsafe fn wait( + &self, + fence: &super::Fence, + wait_value: crate::FenceValue, + timeout: Option<Duration>, + ) -> Result<bool, crate::DeviceError> { + let timeout_ns = timeout + .unwrap_or(Duration::MAX) + .as_nanos() + .min(u64::MAX as _) as u64; + self.shared.wait_for_fence(fence, wait_value, timeout_ns) + } + + unsafe fn start_graphics_debugger_capture(&self) -> bool { + #[cfg(feature = "renderdoc")] + { + // Renderdoc requires us to give us the pointer that vkInstance _points to_. + let raw_vk_instance = + vk::Handle::as_raw(self.shared.instance.raw.handle()) as *mut *mut _; + let raw_vk_instance_dispatch_table = unsafe { *raw_vk_instance }; + unsafe { + self.render_doc + .start_frame_capture(raw_vk_instance_dispatch_table, ptr::null_mut()) + } + } + #[cfg(not(feature = "renderdoc"))] + false + } + unsafe fn stop_graphics_debugger_capture(&self) { + #[cfg(feature = "renderdoc")] + { + // Renderdoc requires us to give us the pointer that vkInstance _points to_. + let raw_vk_instance = + vk::Handle::as_raw(self.shared.instance.raw.handle()) as *mut *mut _; + let raw_vk_instance_dispatch_table = unsafe { *raw_vk_instance }; + + unsafe { + self.render_doc + .end_frame_capture(raw_vk_instance_dispatch_table, ptr::null_mut()) + } + } + } + + unsafe fn pipeline_cache_get_data(&self, cache: &super::PipelineCache) -> Option<Vec<u8>> { + let data = unsafe { self.raw_device().get_pipeline_cache_data(cache.raw) }; + data.ok() + } + + unsafe fn get_acceleration_structure_build_sizes<'a>( + &self, + desc: &crate::GetAccelerationStructureBuildSizesDescriptor<'a, super::Buffer>, + ) -> crate::AccelerationStructureBuildSizes { + const CAPACITY: usize = 8; + + let ray_tracing_functions = self + .shared + .extension_fns + .ray_tracing + .as_ref() + .expect("Feature `RAY_TRACING` not enabled"); + + let (geometries, primitive_counts) = match *desc.entries { + crate::AccelerationStructureEntries::Instances(ref instances) => { + let instance_data = vk::AccelerationStructureGeometryInstancesDataKHR::default(); + + let geometry = vk::AccelerationStructureGeometryKHR::default() + .geometry_type(vk::GeometryTypeKHR::INSTANCES) + .geometry(vk::AccelerationStructureGeometryDataKHR { + instances: instance_data, + }); + + ( + smallvec::smallvec![geometry], + smallvec::smallvec![instances.count], + ) + } + crate::AccelerationStructureEntries::Triangles(ref in_geometries) => { + let mut primitive_counts = + smallvec::SmallVec::<[u32; CAPACITY]>::with_capacity(in_geometries.len()); + let mut geometries = smallvec::SmallVec::< + [vk::AccelerationStructureGeometryKHR; CAPACITY], + >::with_capacity(in_geometries.len()); + + for triangles in in_geometries { + let mut triangle_data = + vk::AccelerationStructureGeometryTrianglesDataKHR::default() + .index_type(vk::IndexType::NONE_KHR) + .vertex_format(conv::map_vertex_format(triangles.vertex_format)) + .max_vertex(triangles.vertex_count) + .vertex_stride(triangles.vertex_stride) + // The vulkan spec suggests we could pass a non-zero invalid address here if fetching + // the real address has significant overhead, but we pass the real one to be on the + // safe side for now. + // from https://registry.khronos.org/vulkan/specs/latest/man/html/vkGetAccelerationStructureBuildSizesKHR.html + // > The srcAccelerationStructure, dstAccelerationStructure, and mode members + // > of pBuildInfo are ignored. Any VkDeviceOrHostAddressKHR or VkDeviceOrHostAddressConstKHR + // > members of pBuildInfo are ignored by this command, except that the hostAddress + // > member of VkAccelerationStructureGeometryTrianglesDataKHR::transformData will + // > be examined to check if it is NULL. + .transform_data(vk::DeviceOrHostAddressConstKHR { + device_address: if desc + .flags + .contains(wgt::AccelerationStructureFlags::USE_TRANSFORM) + { + unsafe { + ray_tracing_functions + .buffer_device_address + .get_buffer_device_address( + &vk::BufferDeviceAddressInfo::default().buffer( + triangles + .transform + .as_ref() + .unwrap() + .buffer + .raw, + ), + ) + } + } else { + 0 + }, + }); + + let pritive_count = if let Some(ref indices) = triangles.indices { + triangle_data = + triangle_data.index_type(conv::map_index_format(indices.format)); + indices.count / 3 + } else { + triangles.vertex_count / 3 + }; + + let geometry = vk::AccelerationStructureGeometryKHR::default() + .geometry_type(vk::GeometryTypeKHR::TRIANGLES) + .geometry(vk::AccelerationStructureGeometryDataKHR { + triangles: triangle_data, + }) + .flags(conv::map_acceleration_structure_geometry_flags( + triangles.flags, + )); + + geometries.push(geometry); + primitive_counts.push(pritive_count); + } + (geometries, primitive_counts) + } + crate::AccelerationStructureEntries::AABBs(ref in_geometries) => { + let mut primitive_counts = + smallvec::SmallVec::<[u32; CAPACITY]>::with_capacity(in_geometries.len()); + let mut geometries = smallvec::SmallVec::< + [vk::AccelerationStructureGeometryKHR; CAPACITY], + >::with_capacity(in_geometries.len()); + for aabb in in_geometries { + let aabbs_data = vk::AccelerationStructureGeometryAabbsDataKHR::default() + .stride(aabb.stride); + + let geometry = vk::AccelerationStructureGeometryKHR::default() + .geometry_type(vk::GeometryTypeKHR::AABBS) + .geometry(vk::AccelerationStructureGeometryDataKHR { aabbs: aabbs_data }) + .flags(conv::map_acceleration_structure_geometry_flags(aabb.flags)); + + geometries.push(geometry); + primitive_counts.push(aabb.count); + } + (geometries, primitive_counts) + } + }; + + let ty = match *desc.entries { + crate::AccelerationStructureEntries::Instances(_) => { + vk::AccelerationStructureTypeKHR::TOP_LEVEL + } + _ => vk::AccelerationStructureTypeKHR::BOTTOM_LEVEL, + }; + + let geometry_info = vk::AccelerationStructureBuildGeometryInfoKHR::default() + .ty(ty) + .flags(conv::map_acceleration_structure_flags(desc.flags)) + .geometries(&geometries); + + let mut raw = Default::default(); + unsafe { + ray_tracing_functions + .acceleration_structure + .get_acceleration_structure_build_sizes( + vk::AccelerationStructureBuildTypeKHR::DEVICE, + &geometry_info, + &primitive_counts, + &mut raw, + ) + } + + crate::AccelerationStructureBuildSizes { + acceleration_structure_size: raw.acceleration_structure_size, + update_scratch_size: raw.update_scratch_size, + build_scratch_size: raw.build_scratch_size, + } + } + + unsafe fn get_acceleration_structure_device_address( + &self, + acceleration_structure: &super::AccelerationStructure, + ) -> wgt::BufferAddress { + let ray_tracing_functions = self + .shared + .extension_fns + .ray_tracing + .as_ref() + .expect("Feature `RAY_TRACING` not enabled"); + + unsafe { + ray_tracing_functions + .acceleration_structure + .get_acceleration_structure_device_address( + &vk::AccelerationStructureDeviceAddressInfoKHR::default() + .acceleration_structure(acceleration_structure.raw), + ) + } + } + + unsafe fn create_acceleration_structure( + &self, + desc: &crate::AccelerationStructureDescriptor, + ) -> Result<super::AccelerationStructure, crate::DeviceError> { + let ray_tracing_functions = self + .shared + .extension_fns + .ray_tracing + .as_ref() + .expect("Feature `RAY_TRACING` not enabled"); + + let vk_buffer_info = vk::BufferCreateInfo::default() + .size(desc.size) + .usage( + vk::BufferUsageFlags::ACCELERATION_STRUCTURE_STORAGE_KHR + | vk::BufferUsageFlags::SHADER_DEVICE_ADDRESS, + ) + .sharing_mode(vk::SharingMode::EXCLUSIVE); + + unsafe { + let raw_buffer = self + .shared + .raw + .create_buffer(&vk_buffer_info, None) + .map_err(super::map_host_device_oom_and_ioca_err)?; + + let requirements = self.shared.raw.get_buffer_memory_requirements(raw_buffer); + + self.error_if_would_oom_on_resource_allocation( + gpu_allocator::MemoryLocation::GpuOnly, + &requirements, + ) + .inspect_err(|_| { + self.shared.raw.destroy_buffer(raw_buffer, None); + })?; + + let name = desc + .label + .unwrap_or("Unlabeled acceleration structure buffer"); + + let allocation = self + .mem_allocator + .lock() + .allocate(&gpu_allocator::vulkan::AllocationCreateDesc { + name, + requirements, + location: gpu_allocator::MemoryLocation::GpuOnly, + linear: true, // Buffers are always linear + allocation_scheme: gpu_allocator::vulkan::AllocationScheme::GpuAllocatorManaged, + }) + .inspect_err(|_| { + self.shared.raw.destroy_buffer(raw_buffer, None); + })?; + + self.shared + .raw + .bind_buffer_memory(raw_buffer, allocation.memory(), allocation.offset()) + .map_err(super::map_host_device_oom_and_ioca_err) + .inspect_err(|_| { + self.shared.raw.destroy_buffer(raw_buffer, None); + })?; + + if let Some(label) = desc.label { + self.shared.set_object_name(raw_buffer, label); + } + + let vk_info = vk::AccelerationStructureCreateInfoKHR::default() + .buffer(raw_buffer) + .offset(0) + .size(desc.size) + .ty(conv::map_acceleration_structure_format(desc.format)); + + let raw_acceleration_structure = ray_tracing_functions + .acceleration_structure + .create_acceleration_structure(&vk_info, None) + .map_err(super::map_host_oom_and_ioca_err) + .inspect_err(|_| { + self.shared.raw.destroy_buffer(raw_buffer, None); + })?; + + if let Some(label) = desc.label { + self.shared + .set_object_name(raw_acceleration_structure, label); + } + + let pool = if desc.allow_compaction { + let vk_info = vk::QueryPoolCreateInfo::default() + .query_type(vk::QueryType::ACCELERATION_STRUCTURE_COMPACTED_SIZE_KHR) + .query_count(1); + + let raw = self + .shared + .raw + .create_query_pool(&vk_info, None) + .map_err(super::map_host_device_oom_err) + .inspect_err(|_| { + ray_tracing_functions + .acceleration_structure + .destroy_acceleration_structure(raw_acceleration_structure, None); + self.shared.raw.destroy_buffer(raw_buffer, None); + })?; + Some(raw) + } else { + None + }; + + Ok(super::AccelerationStructure { + raw: raw_acceleration_structure, + buffer: raw_buffer, + allocation, + compacted_size_query: pool, + }) + } + } + + unsafe fn destroy_acceleration_structure( + &self, + acceleration_structure: super::AccelerationStructure, + ) { + let ray_tracing_functions = self + .shared + .extension_fns + .ray_tracing + .as_ref() + .expect("Feature `RAY_TRACING` not enabled"); + + unsafe { + ray_tracing_functions + .acceleration_structure + .destroy_acceleration_structure(acceleration_structure.raw, None); + self.shared + .raw + .destroy_buffer(acceleration_structure.buffer, None); + let result = self + .mem_allocator + .lock() + .free(acceleration_structure.allocation); + if let Err(err) = result { + log::warn!("Failed to free buffer acceleration structure: {err}"); + } + if let Some(query) = acceleration_structure.compacted_size_query { + self.shared.raw.destroy_query_pool(query, None) + } + } + } + + fn get_internal_counters(&self) -> wgt::HalCounters { + self.counters + .memory_allocations + .set(self.shared.memory_allocations_counter.read()); + + self.counters.as_ref().clone() + } + + fn generate_allocator_report(&self) -> Option<wgt::AllocatorReport> { + let gpu_allocator::AllocatorReport { + allocations, + blocks, + total_allocated_bytes, + total_capacity_bytes, + } = self.mem_allocator.lock().generate_report(); + + let allocations = allocations + .into_iter() + .map(|alloc| wgt::AllocationReport { + name: alloc.name, + offset: alloc.offset, + size: alloc.size, + }) + .collect(); + + let blocks = blocks + .into_iter() + .map(|block| wgt::MemoryBlockReport { + size: block.size, + allocations: block.allocations.clone(), + }) + .collect(); + + Some(wgt::AllocatorReport { + allocations, + blocks, + total_allocated_bytes, + total_reserved_bytes: total_capacity_bytes, + }) + } + + fn tlas_instance_to_bytes(&self, instance: TlasInstance) -> Vec<u8> { + const MAX_U24: u32 = (1u32 << 24u32) - 1u32; + let temp = RawTlasInstance { + transform: instance.transform, + custom_data_and_mask: (instance.custom_data & MAX_U24) + | (u32::from(instance.mask) << 24), + shader_binding_table_record_offset_and_flags: (instance + .pipeline_intersection_data_offset + & MAX_U24), + acceleration_structure_reference: instance.blas_address, + }; + bytemuck::bytes_of(&temp).to_vec() + } + + fn check_if_oom(&self) -> Result<(), crate::DeviceError> { + let Some(threshold) = self + .shared + .instance + .memory_budget_thresholds + .for_device_loss + else { + return Ok(()); + }; + + let Some(memory_properties) = self.get_memory_properties() else { + return Ok(()); + }; + + for i in 0..memory_properties.heaps().len() { + let heap_usage = memory_properties.heap_usage()[i]; + let heap_budget = memory_properties.heap_budget()[i]; + + if heap_usage >= heap_budget / 100 * threshold as u64 { + return Err(crate::DeviceError::OutOfMemory); + } + } + + Ok(()) + } +} + +impl super::DeviceShared { + pub(super) fn new_binary_semaphore( + &self, + name: &str, + ) -> Result<vk::Semaphore, crate::DeviceError> { + unsafe { + let semaphore = self + .raw + .create_semaphore(&vk::SemaphoreCreateInfo::default(), None) + .map_err(super::map_host_device_oom_err)?; + + self.set_object_name(semaphore, name); + + Ok(semaphore) + } + } + + pub(super) fn wait_for_fence( + &self, + fence: &super::Fence, + wait_value: crate::FenceValue, + timeout_ns: u64, + ) -> Result<bool, crate::DeviceError> { + profiling::scope!("Device::wait"); + match *fence { + super::Fence::TimelineSemaphore(raw) => { + let semaphores = [raw]; + let values = [wait_value]; + let vk_info = vk::SemaphoreWaitInfo::default() + .semaphores(&semaphores) + .values(&values); + let result = match self.extension_fns.timeline_semaphore { + Some(super::ExtensionFn::Extension(ref ext)) => unsafe { + ext.wait_semaphores(&vk_info, timeout_ns) + }, + Some(super::ExtensionFn::Promoted) => unsafe { + self.raw.wait_semaphores(&vk_info, timeout_ns) + }, + None => unreachable!(), + }; + match result { + Ok(()) => Ok(true), + Err(vk::Result::TIMEOUT) => Ok(false), + Err(other) => Err(super::map_host_device_oom_and_lost_err(other)), + } + } + super::Fence::FencePool(ref pool) => { + let pool = pool.read(); + let super::FencePool { + last_completed, + ref active, + free: _, + } = *pool; + if wait_value <= last_completed { + Ok(true) + } else { + match active.iter().find(|&&(value, _)| value >= wait_value) { + Some((_, fence)) => { + // clone to show we are using this fence while the pool is unlocked. + let fence = fence.clone(); + drop(pool); + match unsafe { + self.raw.wait_for_fences( + core::slice::from_ref(&fence), + true, + timeout_ns, + ) + } { + Ok(()) => Ok(true), + Err(vk::Result::TIMEOUT) => Ok(false), + Err(other) => Err(super::map_host_device_oom_and_lost_err(other)), + } + } + None => { + crate::hal_usage_error(format!( + "no signals reached value {wait_value}" + )); + } + } + } + } + } + } +} + +struct ImageWithoutMemory { + raw: vk::Image, + requirements: vk::MemoryRequirements, +} diff --git a/third_party/wgpu-hal/src/vulkan/drm.rs b/third_party/wgpu-hal/src/vulkan/drm.rs new file mode 100644 index 000000000..9c03b0415 --- /dev/null +++ b/third_party/wgpu-hal/src/vulkan/drm.rs @@ -0,0 +1,252 @@ +#![cfg(drm)] + +use alloc::{string::ToString, vec::Vec}; +use core::{mem::MaybeUninit, num::NonZeroU32}; +use std::os::fd::{AsFd, BorrowedFd}; + +use ash::{ext, khr, vk}; + +use drm::{ + self, + control::{self, connector, Device as _}, +}; + +struct Card(i32); +impl AsFd for Card { + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.0) } + } +} +impl control::Device for Card {} +impl drm::Device for Card {} + +macro_rules! to_u64 { + ($expr:expr) => {{ + #[allow(trivial_numeric_casts)] + let expr = $expr as u64; + assert!(size_of_val(&expr) <= size_of::<u64>()); + expr + }}; +} + +impl super::Instance { + /// Creates a new surface from the given drm fd and plane, deriving the connector and mode. + /// + /// # Safety + /// + /// - All parameters must point to valid DRM values. + pub fn create_surface_from_drm_plane( + &self, + fd: i32, + plane: u32, + ) -> Result<super::Surface, crate::InstanceError> { + let card = Card(fd); + let plane_info = card + .get_plane( + NonZeroU32::new(plane) + .ok_or(crate::InstanceError::new("Invalid drm plane".to_string()))? + .into(), + ) + .map_err(|e| crate::InstanceError::with_source("drm plane not found".to_string(), e))?; + let crtc_handle = plane_info.crtc().ok_or(crate::InstanceError::new( + "No CRTC for drm plane".to_string(), + ))?; + let crtc_info = card + .get_crtc(crtc_handle) + .map_err(|e| crate::InstanceError::with_source("drm CRTC not found".to_string(), e))?; + let mode = crtc_info.mode().ok_or(crate::InstanceError::new( + "No mode for drm CRTC".to_string(), + ))?; + + let mut connector_and_mode = None; + let resources = card.resource_handles().map_err(|e| { + crate::InstanceError::with_source("No drm resource handles found".to_string(), e) + })?; + for connector_handle in resources.connectors() { + let Ok(connector_info) = card.get_connector(*connector_handle, false) else { + continue; + }; + if connector_info.state() != connector::State::Connected { + continue; + } + + if let Some(encoder_handle) = connector_info.current_encoder() { + if let Ok(encoder_info) = card.get_encoder(encoder_handle) { + if encoder_info.crtc() == Some(crtc_handle) { + connector_and_mode = Some((*connector_handle, mode)); + break; + } + } + } + } + + let (connector_handle, mode) = connector_and_mode.ok_or(crate::InstanceError::new( + "Failed to derive drm connector and mode for plane".to_string(), + ))?; + let (width, height) = mode.size(); + // Rate in millihertz + let refresh_rate = (((mode.clock() as f64 * 1000.0) + / (mode.hsync().2 as f64 * mode.vsync().2 as f64)) + * 1000.0) + .round() as u32; + unsafe { + self.create_surface_from_drm( + fd, + plane, + connector_handle.into(), + width as u32, + height as u32, + refresh_rate, + ) + } + } + + /// Creates a new surface from the given drm configuration. + /// + /// # Safety + /// + /// - All parameters must point to valid DRM values. + pub unsafe fn create_surface_from_drm( + &self, + fd: i32, + plane: u32, + connector_id: u32, + width: u32, + height: u32, + refresh_rate: u32, + ) -> Result<super::Surface, crate::InstanceError> { + if !self + .shared + .extensions + .contains(&ext::acquire_drm_display::NAME) + { + return Err(crate::InstanceError::new( + "Vulkan driver does not support VK_EXT_acquire_drm_display".to_string(), + )); + } + + let drm_stat = { + let mut stat = MaybeUninit::<libc::stat>::uninit(); + + if unsafe { libc::fstat(fd, stat.as_mut_ptr()) } != 0 { + return Err(crate::InstanceError::new( + "Unable to fstat drm device".to_string(), + )); + } + + unsafe { stat.assume_init() } + }; + + let raw_devices = match unsafe { self.shared.raw.enumerate_physical_devices() } { + Ok(devices) => devices, + Err(err) => { + log::error!("enumerate_adapters: {err}"); + Vec::new() + } + }; + + let mut physical_device = None; + + for device in raw_devices { + let properties2 = vk::PhysicalDeviceProperties2KHR::default(); + + let mut drm_props = vk::PhysicalDeviceDrmPropertiesEXT::default(); + let mut properties2 = properties2.push_next(&mut drm_props); + + unsafe { + self.shared + .raw + .get_physical_device_properties2(device, &mut properties2) + }; + + /* + The makedev call is just bit manipulation to combine major and minor device numbers into a Unix device ID. + It doesn't perform any filesystem operations, only bitshifting. + See: https://github.com/rust-lang/libc/blob/268e1b3810ac07ed637d9005bc1a54e49218c958/src/unix/linux_like/linux/mod.rs#L6049 + We use the resulting device IDs to check if the Vulkan raw device from enumerate_physical_devices + matches the DRM device referred to by our file descriptor. + */ + + let primary_devid = + libc::makedev(drm_props.primary_major as _, drm_props.primary_minor as _); + let render_devid = + libc::makedev(drm_props.render_major as _, drm_props.render_minor as _); + + // On most platforms, both `*_devid`s and `st_rdev` are `dev_t`s (which is generally + // observed to be an unsigned integral type no greater than 64 bits). However, on some + // platforms, there divergences from this pattern: + // + // - `armv7-linux-androideabi`: `dev_t` is `c_ulong`, and `*_devid`s are `dev_t`, but + // `st_rdev` is `c_ulonglong`. So, we can't just do a `==` comparison. + // - OpenBSD has `dev_t` on both sides, but is `i32` (N.B., unsigned). Therefore, we + // can't just use `u64::from`. + #[allow(clippy::useless_conversion)] + if [primary_devid, render_devid] + .map(|devid| to_u64!(devid)) + .contains(&to_u64!(drm_stat.st_rdev)) + { + physical_device = Some(device) + } + } + + let physical_device = physical_device.ok_or(crate::InstanceError::new( + "Failed to find suitable drm device".to_string(), + ))?; + + let acquire_drm_display_instance = + ext::acquire_drm_display::Instance::new(&self.shared.entry, &self.shared.raw); + + let display = unsafe { + acquire_drm_display_instance + .get_drm_display(physical_device, fd, connector_id) + .expect("Failed to get drm display") + }; + + unsafe { + acquire_drm_display_instance + .acquire_drm_display(physical_device, fd, display) + .expect("Failed to acquire drm display") + } + + let display_instance = khr::display::Instance::new(&self.shared.entry, &self.shared.raw); + + let modes = unsafe { + display_instance + .get_display_mode_properties(physical_device, display) + .expect("Failed to get display modes") + }; + + let mut mode = None; + + for current_mode in modes { + log::trace!( + "Comparing mode {}x{}@{} with {width}x{height}@{refresh_rate}", + current_mode.parameters.visible_region.width, + current_mode.parameters.visible_region.height, + current_mode.parameters.refresh_rate + ); + if current_mode.parameters.refresh_rate == refresh_rate + && current_mode.parameters.visible_region.width == width + && current_mode.parameters.visible_region.height == height + { + mode = Some(current_mode) + } + } + + let mode = mode.ok_or(crate::InstanceError::new( + "Failed to find suitable display mode".to_string(), + ))?; + + let create_info = vk::DisplaySurfaceCreateInfoKHR::default() + .display_mode(mode.display_mode) + .image_extent(mode.parameters.visible_region) + .transform(vk::SurfaceTransformFlagsKHR::IDENTITY) + .alpha_mode(vk::DisplayPlaneAlphaFlagsKHR::OPAQUE) + .plane_index(plane); + + let surface = unsafe { display_instance.create_display_plane_surface(&create_info, None) } + .expect("Failed to create DRM surface"); + + Ok(self.create_surface_from_vk_surface_khr(surface, None)) + } +} diff --git a/third_party/wgpu-hal/src/vulkan/instance.rs b/third_party/wgpu-hal/src/vulkan/instance.rs new file mode 100644 index 000000000..177587582 --- /dev/null +++ b/third_party/wgpu-hal/src/vulkan/instance.rs @@ -0,0 +1,1051 @@ +use alloc::{borrow::ToOwned as _, boxed::Box, ffi::CString, string::String, sync::Arc, vec::Vec}; +use core::{ + ffi::{c_void, CStr}, + marker::PhantomData, + slice, + str::FromStr, +}; +use std::thread; + +use arrayvec::ArrayVec; +use ash::{ext, khr, vk}; +use parking_lot::RwLock; + +unsafe extern "system" fn debug_utils_messenger_callback( + message_severity: vk::DebugUtilsMessageSeverityFlagsEXT, + message_type: vk::DebugUtilsMessageTypeFlagsEXT, + callback_data_ptr: *const vk::DebugUtilsMessengerCallbackDataEXT, + user_data: *mut c_void, +) -> vk::Bool32 { + use alloc::borrow::Cow; + + if thread::panicking() { + return vk::FALSE; + } + + let cd = unsafe { &*callback_data_ptr }; + let user_data = unsafe { &*user_data.cast::<super::DebugUtilsMessengerUserData>() }; + + const VUID_VKCMDENDDEBUGUTILSLABELEXT_COMMANDBUFFER_01912: i32 = 0x56146426; + if cd.message_id_number == VUID_VKCMDENDDEBUGUTILSLABELEXT_COMMANDBUFFER_01912 { + // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/5671 + // Versions 1.3.240 through 1.3.250 return a spurious error here if + // the debug range start and end appear in different command buffers. + if let Some(layer_properties) = user_data.validation_layer_properties.as_ref() { + if layer_properties.layer_description.as_ref() == c"Khronos Validation Layer" + && layer_properties.layer_spec_version >= vk::make_api_version(0, 1, 3, 240) + && layer_properties.layer_spec_version <= vk::make_api_version(0, 1, 3, 250) + { + return vk::FALSE; + } + } + } + + // Silence Vulkan Validation error "VUID-VkSwapchainCreateInfoKHR-pNext-07781" + // This happens when a surface is configured with a size outside the allowed extent. + // It's a false positive due to the inherent racy-ness of surface resizing. + const VUID_VKSWAPCHAINCREATEINFOKHR_PNEXT_07781: i32 = 0x4c8929c1; + if cd.message_id_number == VUID_VKSWAPCHAINCREATEINFOKHR_PNEXT_07781 { + return vk::FALSE; + } + + // Silence Vulkan Validation error "VUID-VkRenderPassBeginInfo-framebuffer-04627" + // if the OBS layer is enabled. This is a bug in the OBS layer. As the OBS layer + // does not have a version number they increment, there is no way to qualify the + // suppression of the error to a specific version of the OBS layer. + // + // See https://github.com/obsproject/obs-studio/issues/9353 + const VUID_VKRENDERPASSBEGININFO_FRAMEBUFFER_04627: i32 = 0x45125641; + if cd.message_id_number == VUID_VKRENDERPASSBEGININFO_FRAMEBUFFER_04627 + && user_data.has_obs_layer + { + return vk::FALSE; + } + + // Silence Vulkan Validation error "VUID-vkCmdCopyImageToBuffer-pRegions-00184". + // While we aren't sure yet, we suspect this is probably a VVL issue. + // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/9276 + const VUID_VKCMDCOPYIMAGETOBUFFER_PREGIONS_00184: i32 = 0x45ef177c; + if cd.message_id_number == VUID_VKCMDCOPYIMAGETOBUFFER_PREGIONS_00184 { + return vk::FALSE; + } + + // Silence Vulkan Validation error "VUID-StandaloneSpirv-None-10684". + // + // This is a bug. To prevent massive noise in the tests, lets suppress it for now. + // https://github.com/gfx-rs/wgpu/issues/7696 + const VUID_STANDALONESPIRV_NONE_10684: i32 = 0xb210f7c2_u32 as i32; + if cd.message_id_number == VUID_STANDALONESPIRV_NONE_10684 { + return vk::FALSE; + } + + let level = match message_severity { + // We intentionally suppress info messages down to debug + // so that users are not innundated with info messages from the runtime. + vk::DebugUtilsMessageSeverityFlagsEXT::VERBOSE => log::Level::Trace, + vk::DebugUtilsMessageSeverityFlagsEXT::INFO => log::Level::Debug, + vk::DebugUtilsMessageSeverityFlagsEXT::WARNING => log::Level::Warn, + vk::DebugUtilsMessageSeverityFlagsEXT::ERROR => log::Level::Error, + _ => log::Level::Warn, + }; + + let message_id_name = + unsafe { cd.message_id_name_as_c_str() }.map_or(Cow::Borrowed(""), CStr::to_string_lossy); + let message = unsafe { cd.message_as_c_str() }.map_or(Cow::Borrowed(""), CStr::to_string_lossy); + + let _ = std::panic::catch_unwind(|| { + log::log!( + level, + "{:?} [{} (0x{:x})]\n\t{}", + message_type, + message_id_name, + cd.message_id_number, + message, + ); + }); + + if cd.queue_label_count != 0 { + let labels = + unsafe { slice::from_raw_parts(cd.p_queue_labels, cd.queue_label_count as usize) }; + let names = labels + .iter() + .flat_map(|dul_obj| unsafe { dul_obj.label_name_as_c_str() }.map(CStr::to_string_lossy)) + .collect::<Vec<_>>(); + + let _ = std::panic::catch_unwind(|| { + log::log!(level, "\tqueues: {}", names.join(", ")); + }); + } + + if cd.cmd_buf_label_count != 0 { + let labels = + unsafe { slice::from_raw_parts(cd.p_cmd_buf_labels, cd.cmd_buf_label_count as usize) }; + let names = labels + .iter() + .flat_map(|dul_obj| unsafe { dul_obj.label_name_as_c_str() }.map(CStr::to_string_lossy)) + .collect::<Vec<_>>(); + + let _ = std::panic::catch_unwind(|| { + log::log!(level, "\tcommand buffers: {}", names.join(", ")); + }); + } + + if cd.object_count != 0 { + let labels = unsafe { slice::from_raw_parts(cd.p_objects, cd.object_count as usize) }; + //TODO: use color fields of `vk::DebugUtilsLabelExt`? + let names = labels + .iter() + .map(|obj_info| { + let name = unsafe { obj_info.object_name_as_c_str() } + .map_or(Cow::Borrowed("?"), CStr::to_string_lossy); + + format!( + "(type: {:?}, hndl: 0x{:x}, name: {})", + obj_info.object_type, obj_info.object_handle, name + ) + }) + .collect::<Vec<_>>(); + let _ = std::panic::catch_unwind(|| { + log::log!(level, "\tobjects: {}", names.join(", ")); + }); + } + + #[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()); + } + + vk::FALSE +} + +impl super::DebugUtilsCreateInfo { + fn to_vk_create_info(&self) -> vk::DebugUtilsMessengerCreateInfoEXT<'_> { + let user_data_ptr: *const super::DebugUtilsMessengerUserData = &*self.callback_data; + vk::DebugUtilsMessengerCreateInfoEXT::default() + .message_severity(self.severity) + .message_type(self.message_type) + .user_data(user_data_ptr as *mut _) + .pfn_user_callback(Some(debug_utils_messenger_callback)) + } +} + +impl super::InstanceShared { + pub fn entry(&self) -> &ash::Entry { + &self.entry + } + + pub fn raw_instance(&self) -> &ash::Instance { + &self.raw + } + + pub fn instance_api_version(&self) -> u32 { + self.instance_api_version + } + + pub fn extensions(&self) -> &[&'static CStr] { + &self.extensions[..] + } +} + +impl super::Instance { + pub fn shared_instance(&self) -> &super::InstanceShared { + &self.shared + } + + fn enumerate_instance_extension_properties( + entry: &ash::Entry, + layer_name: Option<&CStr>, + ) -> Result<Vec<vk::ExtensionProperties>, crate::InstanceError> { + let instance_extensions = { + profiling::scope!("vkEnumerateInstanceExtensionProperties"); + unsafe { entry.enumerate_instance_extension_properties(layer_name) } + }; + instance_extensions.map_err(|e| { + crate::InstanceError::with_source( + String::from("enumerate_instance_extension_properties() failed"), + e, + ) + }) + } + + /// Return the instance extension names wgpu would like to enable. + /// + /// Return a vector of the names of instance extensions actually available + /// on `entry` that wgpu would like to enable. + /// + /// The `instance_api_version` argument should be the instance's Vulkan API + /// version, as obtained from `vkEnumerateInstanceVersion`. This is the same + /// space of values as the `VK_API_VERSION` constants. + /// + /// Note that wgpu can function without many of these extensions (for + /// example, `VK_KHR_wayland_surface` is certainly not going to be available + /// everywhere), but if one of these extensions is available at all, wgpu + /// assumes that it has been enabled. + pub fn desired_extensions( + entry: &ash::Entry, + _instance_api_version: u32, + flags: wgt::InstanceFlags, + ) -> Result<Vec<&'static CStr>, crate::InstanceError> { + let instance_extensions = Self::enumerate_instance_extension_properties(entry, None)?; + + // Check our extensions against the available extensions + let mut extensions: Vec<&'static CStr> = Vec::new(); + + // VK_KHR_surface + extensions.push(khr::surface::NAME); + + // Platform-specific WSI extensions + if cfg!(all( + unix, + not(target_os = "android"), + not(target_os = "macos") + )) { + // VK_KHR_xlib_surface + extensions.push(khr::xlib_surface::NAME); + // VK_KHR_xcb_surface + extensions.push(khr::xcb_surface::NAME); + // VK_KHR_wayland_surface + extensions.push(khr::wayland_surface::NAME); + } + if cfg!(target_os = "android") { + // VK_KHR_android_surface + extensions.push(khr::android_surface::NAME); + } + if cfg!(target_os = "windows") { + // VK_KHR_win32_surface + extensions.push(khr::win32_surface::NAME); + } + if cfg!(target_os = "macos") { + // VK_EXT_metal_surface + extensions.push(ext::metal_surface::NAME); + extensions.push(khr::portability_enumeration::NAME); + } + if cfg!(drm) { + // VK_EXT_acquire_drm_display -> VK_EXT_direct_mode_display -> VK_KHR_display + extensions.push(ext::acquire_drm_display::NAME); + extensions.push(ext::direct_mode_display::NAME); + extensions.push(khr::display::NAME); + extensions.push(khr::get_physical_device_properties2::NAME); + extensions.push(khr::get_display_properties2::NAME); + } + + if flags.contains(wgt::InstanceFlags::DEBUG) { + // VK_EXT_debug_utils + extensions.push(ext::debug_utils::NAME); + } + + // VK_EXT_swapchain_colorspace + // Provides wide color gamut + extensions.push(ext::swapchain_colorspace::NAME); + + // VK_KHR_get_physical_device_properties2 + // Even though the extension was promoted to Vulkan 1.1, we still require the extension + // so that we don't have to conditionally use the functions provided by the 1.1 instance + extensions.push(khr::get_physical_device_properties2::NAME); + + // Only keep available extensions. + extensions.retain(|&ext| { + if instance_extensions + .iter() + .any(|inst_ext| inst_ext.extension_name_as_c_str() == Ok(ext)) + { + true + } else { + log::debug!("Unable to find extension: {}", ext.to_string_lossy()); + false + } + }); + Ok(extensions) + } + + /// # Safety + /// + /// - `raw_instance` must be created from `entry` + /// - `raw_instance` must be created respecting `instance_api_version`, `extensions` and `flags` + /// - `extensions` must be a superset of `desired_extensions()` and must be created from the + /// same entry, `instance_api_version`` and flags. + /// - `android_sdk_version` is ignored and can be `0` for all platforms besides Android + /// - If `drop_callback` is [`None`], wgpu-hal will take ownership of `raw_instance`. If + /// `drop_callback` is [`Some`], `raw_instance` must be valid until the callback is called. + /// + /// If `debug_utils_user_data` is `Some`, then the validation layer is + /// available, so create a [`vk::DebugUtilsMessengerEXT`]. + #[allow(clippy::too_many_arguments)] + pub unsafe fn from_raw( + entry: ash::Entry, + raw_instance: ash::Instance, + instance_api_version: u32, + android_sdk_version: u32, + debug_utils_create_info: Option<super::DebugUtilsCreateInfo>, + extensions: Vec<&'static CStr>, + flags: wgt::InstanceFlags, + memory_budget_thresholds: wgt::MemoryBudgetThresholds, + has_nv_optimus: bool, + drop_callback: Option<crate::DropCallback>, + ) -> Result<Self, crate::InstanceError> { + log::debug!("Instance version: 0x{instance_api_version:x}"); + + let debug_utils = if let Some(debug_utils_create_info) = debug_utils_create_info { + if extensions.contains(&ext::debug_utils::NAME) { + log::debug!("Enabling debug utils"); + + let extension = ext::debug_utils::Instance::new(&entry, &raw_instance); + let vk_info = debug_utils_create_info.to_vk_create_info(); + let messenger = + unsafe { extension.create_debug_utils_messenger(&vk_info, None) }.unwrap(); + + Some(super::DebugUtils { + extension, + messenger, + callback_data: debug_utils_create_info.callback_data, + }) + } else { + log::debug!("Debug utils not enabled: extension not listed"); + None + } + } else { + log::debug!( + "Debug utils not enabled: \ + debug_utils_user_data not passed to Instance::from_raw" + ); + None + }; + + let get_physical_device_properties = + if extensions.contains(&khr::get_physical_device_properties2::NAME) { + log::debug!("Enabling device properties2"); + Some(khr::get_physical_device_properties2::Instance::new( + &entry, + &raw_instance, + )) + } else { + None + }; + + let drop_guard = crate::DropGuard::from_option(drop_callback); + + Ok(Self { + shared: Arc::new(super::InstanceShared { + raw: raw_instance, + extensions, + drop_guard, + flags, + memory_budget_thresholds, + debug_utils, + get_physical_device_properties, + entry, + has_nv_optimus, + instance_api_version, + android_sdk_version, + }), + }) + } + + fn create_surface_from_xlib( + &self, + dpy: *mut vk::Display, + window: vk::Window, + ) -> Result<super::Surface, crate::InstanceError> { + if !self.shared.extensions.contains(&khr::xlib_surface::NAME) { + return Err(crate::InstanceError::new(String::from( + "Vulkan driver does not support VK_KHR_xlib_surface", + ))); + } + + let surface = { + let xlib_loader = + khr::xlib_surface::Instance::new(&self.shared.entry, &self.shared.raw); + let info = vk::XlibSurfaceCreateInfoKHR::default() + .flags(vk::XlibSurfaceCreateFlagsKHR::empty()) + .window(window) + .dpy(dpy); + + unsafe { xlib_loader.create_xlib_surface(&info, None) } + .expect("XlibSurface::create_xlib_surface() failed") + }; + + Ok(self.create_surface_from_vk_surface_khr(surface, None)) + } + + fn create_surface_from_xcb( + &self, + connection: *mut vk::xcb_connection_t, + window: vk::xcb_window_t, + ) -> Result<super::Surface, crate::InstanceError> { + if !self.shared.extensions.contains(&khr::xcb_surface::NAME) { + return Err(crate::InstanceError::new(String::from( + "Vulkan driver does not support VK_KHR_xcb_surface", + ))); + } + + let surface = { + let xcb_loader = khr::xcb_surface::Instance::new(&self.shared.entry, &self.shared.raw); + let info = vk::XcbSurfaceCreateInfoKHR::default() + .flags(vk::XcbSurfaceCreateFlagsKHR::empty()) + .window(window) + .connection(connection); + + unsafe { xcb_loader.create_xcb_surface(&info, None) } + .expect("XcbSurface::create_xcb_surface() failed") + }; + + Ok(self.create_surface_from_vk_surface_khr(surface, None)) + } + + fn create_surface_from_wayland( + &self, + display: *mut vk::wl_display, + surface: *mut vk::wl_surface, + ) -> Result<super::Surface, crate::InstanceError> { + if !self.shared.extensions.contains(&khr::wayland_surface::NAME) { + return Err(crate::InstanceError::new(String::from( + "Vulkan driver does not support VK_KHR_wayland_surface", + ))); + } + + let surface = { + let w_loader = + khr::wayland_surface::Instance::new(&self.shared.entry, &self.shared.raw); + let info = vk::WaylandSurfaceCreateInfoKHR::default() + .flags(vk::WaylandSurfaceCreateFlagsKHR::empty()) + .display(display) + .surface(surface); + + unsafe { w_loader.create_wayland_surface(&info, None) }.expect("WaylandSurface failed") + }; + + Ok(self.create_surface_from_vk_surface_khr(surface, None)) + } + + fn create_surface_android( + &self, + window: *mut vk::ANativeWindow, + ) -> Result<super::Surface, crate::InstanceError> { + if !self.shared.extensions.contains(&khr::android_surface::NAME) { + return Err(crate::InstanceError::new(String::from( + "Vulkan driver does not support VK_KHR_android_surface", + ))); + } + + let surface = { + let a_loader = + khr::android_surface::Instance::new(&self.shared.entry, &self.shared.raw); + let info = vk::AndroidSurfaceCreateInfoKHR::default() + .flags(vk::AndroidSurfaceCreateFlagsKHR::empty()) + .window(window); + + unsafe { a_loader.create_android_surface(&info, None) }.expect("AndroidSurface failed") + }; + + Ok(self.create_surface_from_vk_surface_khr(surface, None)) + } + + fn create_surface_from_hwnd( + &self, + hinstance: vk::HINSTANCE, + hwnd: vk::HWND, + ) -> Result<super::Surface, crate::InstanceError> { + if !self.shared.extensions.contains(&khr::win32_surface::NAME) { + return Err(crate::InstanceError::new(String::from( + "Vulkan driver does not support VK_KHR_win32_surface", + ))); + } + + let surface = { + let info = vk::Win32SurfaceCreateInfoKHR::default() + .flags(vk::Win32SurfaceCreateFlagsKHR::empty()) + .hinstance(hinstance) + .hwnd(hwnd); + let win32_loader = + khr::win32_surface::Instance::new(&self.shared.entry, &self.shared.raw); + unsafe { + win32_loader + .create_win32_surface(&info, None) + .expect("Unable to create Win32 surface") + } + }; + + // Wrap ash's `isize` `HWND` in `WindowHandle`; on Windows the + // `NativeSurface` builds its DXGI HDR source from it. + #[cfg(windows)] + let window_handle = Some(crate::vulkan::swapchain::WindowHandle( + windows::Win32::Foundation::HWND(hwnd as *mut c_void), + )); + #[cfg(not(windows))] + let window_handle: Option<crate::vulkan::swapchain::WindowHandle> = None; + Ok(self.create_surface_from_vk_surface_khr(surface, window_handle)) + } + + #[cfg(target_vendor = "apple")] + fn create_surface_from_layer( + &self, + layer: raw_window_metal::Layer, + ) -> Result<super::Surface, crate::InstanceError> { + if !self.shared.extensions.contains(&ext::metal_surface::NAME) { + return Err(crate::InstanceError::new(String::from( + "Vulkan driver does not support VK_EXT_metal_surface", + ))); + } + + // NOTE: The layer is retained by Vulkan's `vkCreateMetalSurfaceEXT`, + // so no need to retain it beyond the scope of this function. + let surface = { + let metal_loader = + ext::metal_surface::Instance::new(&self.shared.entry, &self.shared.raw); + let vk_info = vk::MetalSurfaceCreateInfoEXT::default() + .flags(vk::MetalSurfaceCreateFlagsEXT::empty()) + .layer(layer.as_ptr().as_ptr()); + + unsafe { metal_loader.create_metal_surface(&vk_info, None).unwrap() } + }; + + Ok(self.create_surface_from_vk_surface_khr(surface, None)) + } + + pub(super) fn create_surface_from_vk_surface_khr( + &self, + surface: vk::SurfaceKHR, + hwnd: Option<crate::vulkan::swapchain::WindowHandle>, + ) -> super::Surface { + let native_surface = + crate::vulkan::swapchain::NativeSurface::from_vk_surface_khr(self, surface, hwnd); + + super::Surface { + swapchain: RwLock::new(None), + inner: Box::new(native_surface), + } + } + + /// `Instance::init` but with a callback. + /// If you want to add extensions, add the to the `Vec<'static CStr>` not the create info, otherwise + /// it will be overwritten + /// + /// # Safety: + /// Same as `init` but additionally + /// - Callback must not remove features. + /// - Callback must not change anything to what the instance does not support. + pub unsafe fn init_with_callback( + desc: &crate::InstanceDescriptor<'_>, + callback: Option<Box<super::CreateInstanceCallback>>, + ) -> Result<Self, crate::InstanceError> { + profiling::scope!("Init Vulkan Backend"); + + let entry = unsafe { + profiling::scope!("Load vk library"); + // ohos support is already fixed on ash main, but it's unclear when + // a new release can happen. + #[cfg(target_env = "ohos")] + let loaded = ash::Entry::load_from("libvulkan.so"); + #[cfg(not(target_env = "ohos"))] + let loaded = ash::Entry::load(); + loaded + } + .map_err(|err| { + crate::InstanceError::with_source(String::from("missing Vulkan entry points"), err) + })?; + let version = { + profiling::scope!("vkEnumerateInstanceVersion"); + unsafe { entry.try_enumerate_instance_version() } + }; + let instance_api_version = match version { + // Vulkan 1.1+ + Ok(Some(version)) => version, + Ok(None) => vk::API_VERSION_1_0, + Err(err) => { + return Err(crate::InstanceError::with_source( + String::from("try_enumerate_instance_version() failed"), + err, + )); + } + }; + + let app_name = CString::new(desc.name).unwrap(); + let app_info = vk::ApplicationInfo::default() + .application_name(app_name.as_c_str()) + .application_version(1) + .engine_name(c"wgpu-hal") + .engine_version(2) + .api_version( + // Vulkan 1.0 doesn't like anything but 1.0 passed in here... + if instance_api_version < vk::API_VERSION_1_1 { + vk::API_VERSION_1_0 + } else { + // This is the max Vulkan API version supported by `wgpu-hal`. + // + // If we want to increment this, there are some things that must be done first: + // - Audit the behavioral differences between the previous and new API versions. + // - Audit all extensions used by this backend: + // - If any were promoted in the new API version and the behavior has changed, we must handle the new behavior in addition to the old behavior. + // - If any were obsoleted in the new API version, we must implement a fallback for the new API version + // - If any are non-KHR-vendored, we must ensure the new behavior is still correct (since backwards-compatibility is not guaranteed). + vk::API_VERSION_1_3 + }, + ); + + let mut extensions = Self::desired_extensions(&entry, instance_api_version, desc.flags)?; + let mut create_info = vk::InstanceCreateInfo::default(); + + if let Some(callback) = callback { + callback(super::CreateInstanceCallbackArgs { + extensions: &mut extensions, + create_info: &mut create_info, + entry: &entry, + _phantom: PhantomData, + }); + } + + let instance_layers = { + profiling::scope!("vkEnumerateInstanceLayerProperties"); + unsafe { entry.enumerate_instance_layer_properties() } + }; + let instance_layers = instance_layers.map_err(|e| { + log::debug!("enumerate_instance_layer_properties: {e:?}"); + crate::InstanceError::with_source( + String::from("enumerate_instance_layer_properties() failed"), + e, + ) + })?; + + fn find_layer<'layers>( + instance_layers: &'layers [vk::LayerProperties], + name: &CStr, + ) -> Option<&'layers vk::LayerProperties> { + instance_layers + .iter() + .find(|inst_layer| inst_layer.layer_name_as_c_str() == Ok(name)) + } + + let validation_layer_name = c"VK_LAYER_KHRONOS_validation"; + let validation_layer_properties = find_layer(&instance_layers, validation_layer_name); + + // Determine if VK_EXT_validation_features is available, so we can enable + // GPU assisted validation and synchronization validation. + let validation_features_are_enabled = if validation_layer_properties.is_some() { + // Get the all the instance extension properties. + let exts = + Self::enumerate_instance_extension_properties(&entry, Some(validation_layer_name))?; + // Convert all the names of the extensions into an iterator of CStrs. + let mut ext_names = exts + .iter() + .filter_map(|ext| ext.extension_name_as_c_str().ok()); + // Find the validation features extension. + ext_names.any(|ext_name| ext_name == ext::validation_features::NAME) + } else { + false + }; + + let should_enable_gpu_based_validation = desc + .flags + .intersects(wgt::InstanceFlags::GPU_BASED_VALIDATION) + && validation_features_are_enabled; + + let has_nv_optimus = find_layer(&instance_layers, c"VK_LAYER_NV_optimus").is_some(); + + let has_obs_layer = find_layer(&instance_layers, c"VK_LAYER_OBS_HOOK").is_some(); + + let mut layers: Vec<&'static CStr> = Vec::new(); + + let has_debug_extension = extensions.contains(&ext::debug_utils::NAME); + let mut debug_user_data = has_debug_extension.then(|| { + // Put the callback data on the heap, to ensure it will never be + // moved. + Box::new(super::DebugUtilsMessengerUserData { + validation_layer_properties: None, + has_obs_layer, + }) + }); + + // Request validation layer if asked. + if desc.flags.intersects(wgt::InstanceFlags::VALIDATION) + || should_enable_gpu_based_validation + { + if let Some(layer_properties) = validation_layer_properties { + layers.push(validation_layer_name); + + if let Some(debug_user_data) = debug_user_data.as_mut() { + debug_user_data.validation_layer_properties = + Some(super::ValidationLayerProperties { + layer_description: layer_properties + .description_as_c_str() + .unwrap() + .to_owned(), + layer_spec_version: layer_properties.spec_version, + }); + } + } else { + log::debug!( + "InstanceFlags::VALIDATION requested, but unable to find layer: {}", + validation_layer_name.to_string_lossy() + ); + } + } + let mut debug_utils = if let Some(callback_data) = debug_user_data { + // having ERROR unconditionally because Vk doesn't like empty flags + let mut severity = vk::DebugUtilsMessageSeverityFlagsEXT::ERROR; + if log::max_level() >= log::LevelFilter::Debug { + severity |= vk::DebugUtilsMessageSeverityFlagsEXT::VERBOSE; + } + if log::max_level() >= log::LevelFilter::Info { + severity |= vk::DebugUtilsMessageSeverityFlagsEXT::INFO; + } + if log::max_level() >= log::LevelFilter::Warn { + severity |= vk::DebugUtilsMessageSeverityFlagsEXT::WARNING; + } + + let message_type = vk::DebugUtilsMessageTypeFlagsEXT::GENERAL + | vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION + | vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE; + + let create_info = super::DebugUtilsCreateInfo { + severity, + message_type, + callback_data, + }; + + Some(create_info) + } else { + None + }; + + #[cfg(target_os = "android")] + let android_sdk_version = { + let properties = android_system_properties::AndroidSystemProperties::new(); + // See: https://developer.android.com/reference/android/os/Build.VERSION_CODES + if let Some(val) = properties.get("ro.build.version.sdk") { + match val.parse::<u32>() { + Ok(sdk_ver) => sdk_ver, + Err(err) => { + log::error!( + concat!( + "Couldn't parse Android's ", + "ro.build.version.sdk system property ({}): {}", + ), + val, + err, + ); + 0 + } + } + } else { + log::error!("Couldn't read Android's ro.build.version.sdk system property"); + 0 + } + }; + #[cfg(not(target_os = "android"))] + let android_sdk_version = 0; + + let mut flags = vk::InstanceCreateFlags::empty(); + + // Avoid VUID-VkInstanceCreateInfo-flags-06559: Only ask the instance to + // enumerate incomplete Vulkan implementations (which we need on Mac) if + // we managed to find the extension that provides the flag. + if extensions.contains(&khr::portability_enumeration::NAME) { + flags |= vk::InstanceCreateFlags::ENUMERATE_PORTABILITY_KHR; + } + let vk_instance = { + let str_pointers = layers + .iter() + .chain(extensions.iter()) + .map(|&s: &&'static _| { + // Safe because `layers` and `extensions` entries have static lifetime. + s.as_ptr() + }) + .collect::<Vec<_>>(); + + create_info = create_info + .flags(flags) + .application_info(&app_info) + .enabled_layer_names(&str_pointers[..layers.len()]) + .enabled_extension_names(&str_pointers[layers.len()..]); + + let mut debug_utils_create_info = debug_utils + .as_mut() + .map(|create_info| create_info.to_vk_create_info()); + if let Some(debug_utils_create_info) = debug_utils_create_info.as_mut() { + create_info = create_info.push_next(debug_utils_create_info); + } + + // Enable explicit validation features if available + let mut validation_features; + let mut validation_feature_list: ArrayVec<_, 3>; + if validation_features_are_enabled { + validation_feature_list = ArrayVec::new(); + + // Always enable synchronization validation + validation_feature_list + .push(vk::ValidationFeatureEnableEXT::SYNCHRONIZATION_VALIDATION); + + // Only enable GPU assisted validation if requested. + if should_enable_gpu_based_validation { + validation_feature_list.push(vk::ValidationFeatureEnableEXT::GPU_ASSISTED); + validation_feature_list + .push(vk::ValidationFeatureEnableEXT::GPU_ASSISTED_RESERVE_BINDING_SLOT); + } + + validation_features = vk::ValidationFeaturesEXT::default() + .enabled_validation_features(&validation_feature_list); + create_info = create_info.push_next(&mut validation_features); + } + + unsafe { + profiling::scope!("vkCreateInstance"); + entry.create_instance(&create_info, None) + } + .map_err(|e| { + crate::InstanceError::with_source( + String::from("Entry::create_instance() failed"), + e, + ) + })? + }; + + unsafe { + Self::from_raw( + entry, + vk_instance, + instance_api_version, + android_sdk_version, + debug_utils, + extensions, + desc.flags, + desc.memory_budget_thresholds, + has_nv_optimus, + None, + ) + } + } +} + +impl Drop for super::InstanceShared { + fn drop(&mut self) { + unsafe { + // Keep du alive since destroy_instance may also log + let _du = self.debug_utils.take().inspect(|du| { + du.extension + .destroy_debug_utils_messenger(du.messenger, None); + }); + if self.drop_guard.is_none() { + self.raw.destroy_instance(None); + } + } + } +} + +impl crate::Instance for super::Instance { + type A = super::Api; + + unsafe fn init(desc: &crate::InstanceDescriptor<'_>) -> Result<Self, crate::InstanceError> { + unsafe { Self::init_with_callback(desc, None) } + } + + unsafe fn create_surface( + &self, + display_handle: raw_window_handle::RawDisplayHandle, + window_handle: raw_window_handle::RawWindowHandle, + ) -> Result<super::Surface, crate::InstanceError> { + use raw_window_handle::{RawDisplayHandle as Rdh, RawWindowHandle as Rwh}; + + // TODO: Replace with ash-window, which also lazy-loads the extension based on handle type + + match (window_handle, display_handle) { + (Rwh::Wayland(handle), Rdh::Wayland(display)) => { + self.create_surface_from_wayland(display.display.as_ptr(), handle.surface.as_ptr()) + } + (Rwh::Xlib(handle), Rdh::Xlib(display)) => { + let display = display.display.expect("Display pointer is not set."); + self.create_surface_from_xlib(display.as_ptr(), handle.window) + } + (Rwh::Xcb(handle), Rdh::Xcb(display)) => { + let connection = display.connection.expect("Pointer to X-Server is not set."); + self.create_surface_from_xcb(connection.as_ptr(), handle.window.get()) + } + #[cfg(drm)] + (Rwh::Drm(handle), Rdh::Drm(display)) => { + self.create_surface_from_drm_plane(display.fd, handle.plane) + } + (Rwh::AndroidNdk(handle), _) => { + self.create_surface_android(handle.a_native_window.as_ptr()) + } + (Rwh::Win32(handle), _) => { + let hinstance = handle.hinstance.ok_or_else(|| { + crate::InstanceError::new(String::from( + "Vulkan requires raw-window-handle's Win32::hinstance to be set", + )) + })?; + self.create_surface_from_hwnd(hinstance.get(), handle.hwnd.get()) + } + #[cfg(target_vendor = "apple")] + (Rwh::AppKit(handle), _) + if self.shared.extensions.contains(&ext::metal_surface::NAME) => + { + let layer = unsafe { raw_window_metal::Layer::from_ns_view(handle.ns_view) }; + self.create_surface_from_layer(layer) + } + #[cfg(target_vendor = "apple")] + (Rwh::UiKit(handle), _) + if self.shared.extensions.contains(&ext::metal_surface::NAME) => + { + let layer = unsafe { raw_window_metal::Layer::from_ui_view(handle.ui_view) }; + self.create_surface_from_layer(layer) + } + (_, _) => Err(crate::InstanceError::new(format!( + "window handle {window_handle:?} is not a Vulkan-compatible handle" + ))), + } + } + + unsafe fn enumerate_adapters( + &self, + _surface_hint: Option<&super::Surface>, + ) -> Vec<crate::ExposedAdapter<super::Api>> { + use crate::auxil::db; + + let raw_devices = match unsafe { self.shared.raw.enumerate_physical_devices() } { + Ok(devices) => devices, + Err(err) => { + log::error!("enumerate_adapters: {err}"); + Vec::new() + } + }; + + let mut exposed_adapters = raw_devices + .into_iter() + .flat_map(|device| self.expose_adapter(device)) + .collect::<Vec<_>>(); + + // Detect if it's an Intel + NVidia configuration with Optimus + let has_nvidia_dgpu = exposed_adapters.iter().any(|exposed| { + exposed.info.device_type == wgt::DeviceType::DiscreteGpu + && exposed.info.vendor == db::nvidia::VENDOR + }); + if cfg!(target_os = "linux") && has_nvidia_dgpu && self.shared.has_nv_optimus { + for exposed in exposed_adapters.iter_mut() { + if exposed.info.device_type == wgt::DeviceType::IntegratedGpu + && exposed.info.vendor == db::intel::VENDOR + { + // Check if mesa driver and version less than 21.2 + if let Some(version) = exposed.info.driver_info.split_once("Mesa ").map(|s| { + let mut components = s.1.split('.'); + let major = components.next().and_then(|s| u8::from_str(s).ok()); + let minor = components.next().and_then(|s| u8::from_str(s).ok()); + if let (Some(major), Some(minor)) = (major, minor) { + (major, minor) + } else { + (0, 0) + } + }) { + if version < (21, 2) { + // See https://gitlab.freedesktop.org/mesa/mesa/-/issues/4688 + log::debug!( + concat!( + "Disabling presentation on '{}' (id {:?}) ", + "due to NV Optimus and Intel Mesa < v21.2" + ), + exposed.info.name, + exposed.adapter.raw + ); + exposed.adapter.private_caps.can_present = false; + } + } + } + } + } + + exposed_adapters + } +} + +impl crate::Surface for super::Surface { + type A = super::Api; + + unsafe fn configure( + &self, + device: &super::Device, + config: &crate::SurfaceConfiguration, + ) -> Result<(), crate::SurfaceError> { + // SAFETY: `configure`'s contract guarantees there are no resources derived from the swapchain in use. + let mut swap_chain = self.swapchain.write(); + + let mut old = swap_chain.take(); + if let Some(ref mut old) = old { + unsafe { old.release_resources(device) }; + } + + let swapchain = unsafe { self.inner.create_swapchain(device, config, old)? }; + *swap_chain = Some(swapchain); + + Ok(()) + } + + unsafe fn unconfigure(&self, device: &super::Device) { + if let Some(mut sc) = self.swapchain.write().take() { + // SAFETY: `unconfigure`'s contract guarantees there are no resources derived from the swapchain in use. + unsafe { sc.release_resources(device) }; + } + } + + unsafe fn acquire_texture( + &self, + timeout: Option<core::time::Duration>, + fence: &super::Fence, + ) -> Result<crate::AcquiredSurfaceTexture<super::Api>, crate::SurfaceError> { + let mut swapchain = self.swapchain.write(); + let swapchain = swapchain.as_mut().unwrap(); + + unsafe { swapchain.acquire(timeout, fence) } + } + + unsafe fn discard_texture(&self, texture: super::SurfaceTexture) { + unsafe { + self.swapchain + .write() + .as_mut() + .unwrap() + .discard_texture(texture) + .unwrap() + }; + } +} diff --git a/third_party/wgpu-hal/src/vulkan/mod.rs b/third_party/wgpu-hal/src/vulkan/mod.rs new file mode 100644 index 000000000..51e7414b6 --- /dev/null +++ b/third_party/wgpu-hal/src/vulkan/mod.rs @@ -0,0 +1,1763 @@ +/*! +# Vulkan API internals. + +## Stack memory + +Ash expects slices, which we don't generally have available. +We cope with this requirement by the combination of the following ways: + - temporarily allocating `Vec` on heap, where overhead is permitted + - growing temporary local storage + +## Framebuffers and Render passes + +Render passes are cached on the device and kept forever. + +Framebuffers are also cached on the device, but they are removed when +any of the image views (they have) gets removed. +If Vulkan supports image-less framebuffers, +then the actual views are excluded from the framebuffer key. + +## Fences + +If timeline semaphores are available, they are used 1:1 with wgpu-hal fences. +Otherwise, we manage a pool of `VkFence` objects behind each `hal::Fence`. + +!*/ + +mod adapter; +mod command; +pub mod conv; +mod descriptor; +mod device; +mod drm; +mod instance; +mod sampler; +mod semaphore_list; +mod swapchain; + +pub use adapter::PhysicalDeviceFeatures; + +use alloc::{boxed::Box, ffi::CString, sync::Arc, vec::Vec}; +use core::{borrow::Borrow, ffi::CStr, fmt, marker::PhantomData, mem, num::NonZeroU32}; + +use arrayvec::ArrayVec; +use ash::{ext, khr, vk}; +use bytemuck::{Pod, Zeroable}; +use hashbrown::HashSet; +use parking_lot::{Mutex, RwLock}; + +use naga::FastHashMap; +use wgt::InternalCounter; + +use semaphore_list::SemaphoreList; + +use crate::vulkan::semaphore_list::{SemaphoreListMode, SemaphoreType}; + +const MAX_TOTAL_ATTACHMENTS: usize = crate::MAX_COLOR_ATTACHMENTS * 2 + 1; + +#[derive(Clone, Debug)] +pub struct Api; + +impl crate::Api for Api { + const VARIANT: wgt::Backend = wgt::Backend::Vulkan; + + type Instance = Instance; + type Surface = Surface; + type Adapter = Adapter; + type Device = Device; + + type Queue = Queue; + type CommandEncoder = CommandEncoder; + type CommandBuffer = CommandBuffer; + + type Buffer = Buffer; + type Texture = Texture; + type SurfaceTexture = SurfaceTexture; + type TextureView = TextureView; + type Sampler = Sampler; + type QuerySet = QuerySet; + type Fence = Fence; + type AccelerationStructure = AccelerationStructure; + type PipelineCache = PipelineCache; + + type BindGroupLayout = BindGroupLayout; + type BindGroup = BindGroup; + type PipelineLayout = PipelineLayout; + type ShaderModule = ShaderModule; + type RenderPipeline = RenderPipeline; + type ComputePipeline = ComputePipeline; + type RayTracingPipeline = RayTracingPipeline; +} + +crate::impl_dyn_resource!( + Adapter, + AccelerationStructure, + BindGroup, + BindGroupLayout, + Buffer, + CommandBuffer, + CommandEncoder, + ComputePipeline, + Device, + Fence, + Instance, + PipelineCache, + PipelineLayout, + QuerySet, + Queue, + RenderPipeline, + RayTracingPipeline, + Sampler, + ShaderModule, + Surface, + SurfaceTexture, + Texture, + TextureView +); + +struct DebugUtils { + extension: ext::debug_utils::Instance, + messenger: vk::DebugUtilsMessengerEXT, + + /// Owning pointer to the debug messenger callback user data. + /// + /// `InstanceShared::drop` destroys the debug messenger before + /// dropping this, so the callback should never receive a dangling + /// user data pointer. + #[allow(dead_code)] + callback_data: Box<DebugUtilsMessengerUserData>, +} + +#[derive(Debug)] +pub struct DebugUtilsCreateInfo { + severity: vk::DebugUtilsMessageSeverityFlagsEXT, + message_type: vk::DebugUtilsMessageTypeFlagsEXT, + callback_data: Box<DebugUtilsMessengerUserData>, +} + +#[derive(Debug)] +/// The properties related to the validation layer needed for the +/// DebugUtilsMessenger for their workarounds +struct ValidationLayerProperties { + /// Validation layer description, from `vk::LayerProperties`. + layer_description: CString, + + /// Validation layer specification version, from `vk::LayerProperties`. + layer_spec_version: u32, +} + +/// User data needed by `instance::debug_utils_messenger_callback`. +/// +/// When we create the [`vk::DebugUtilsMessengerEXT`], the `pUserData` +/// pointer refers to one of these values. +#[derive(Debug)] +pub struct DebugUtilsMessengerUserData { + /// The properties related to the validation layer, if present + validation_layer_properties: Option<ValidationLayerProperties>, + + /// If the OBS layer is present. OBS never increments the version of their layer, + /// so there's no reason to have the version. + has_obs_layer: bool, +} + +pub struct InstanceShared { + raw: ash::Instance, + extensions: Vec<&'static CStr>, + flags: wgt::InstanceFlags, + memory_budget_thresholds: wgt::MemoryBudgetThresholds, + debug_utils: Option<DebugUtils>, + get_physical_device_properties: Option<khr::get_physical_device_properties2::Instance>, + entry: ash::Entry, + has_nv_optimus: bool, + android_sdk_version: u32, + /// The instance API version. + /// + /// Which is the version of Vulkan supported for instance-level functionality. + /// + /// It is associated with a `VkInstance` and its children, + /// except for a `VkPhysicalDevice` and its children. + instance_api_version: u32, + + // The `drop_guard` field must be the last field of this struct so it is dropped last. + // Do not add new fields after it. + drop_guard: Option<crate::DropGuard>, +} + +impl fmt::Debug for InstanceShared { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let Self { + raw: _, + extensions, + flags, + memory_budget_thresholds, + debug_utils: _, + get_physical_device_properties: _, + entry: _, + has_nv_optimus, + android_sdk_version, + instance_api_version, + drop_guard: _, + } = self; + f.debug_struct("InstanceShared") + .field("extensions", extensions) + .field("flags", flags) + .field("memory_budget_thresholds", memory_budget_thresholds) + .field("has_nv_optimus", has_nv_optimus) + .field("android_sdk_version", android_sdk_version) + .field("instance_api_version", instance_api_version) + .finish_non_exhaustive() + } +} + +#[derive(Debug)] +pub struct Instance { + shared: Arc<InstanceShared>, +} + +#[expect(missing_debug_implementations, reason = "TODO?")] +pub struct Surface { + swapchain: RwLock<Option<Box<dyn swapchain::Swapchain>>>, + inner: Box<dyn swapchain::Surface>, +} + +impl Surface { + /// Returns the raw Vulkan surface handle. + /// + /// Returns `None` if the surface is a DXGI surface. + pub unsafe fn raw_native_handle(&self) -> Option<vk::SurfaceKHR> { + Some( + self.inner + .as_any() + .downcast_ref::<swapchain::NativeSurface>()? + .as_raw(), + ) + } + + /// Get the raw Vulkan swapchain associated with this surface. + /// + /// Returns [`None`] if the surface is not configured or if the swapchain + /// is a DXGI swapchain. + pub fn raw_native_swapchain(&self) -> Option<vk::SwapchainKHR> { + let read = self.swapchain.read(); + Some( + read.as_ref()? + .as_any() + .downcast_ref::<swapchain::NativeSwapchain>()? + .as_raw(), + ) + } + + /// Set the present timing information which will be used for the next [presentation](crate::Queue::present()) of this surface, + /// using [VK_GOOGLE_display_timing]. + /// + /// This can be used to give an id to presentations, for future use of [`vk::PastPresentationTimingGOOGLE`]. + /// Note that `wgpu-hal` does *not* provide a way to use that API - you should manually access this through [`ash`]. + /// + /// This can also be used to add a "not before" timestamp to the presentation. + /// + /// The exact semantics of the fields are also documented in the [specification](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkPresentTimeGOOGLE.html) for the extension. + /// + /// # Panics + /// + /// - If the surface hasn't been configured. + /// - If the surface has been configured for a DXGI swapchain. + /// - If the device doesn't [support present timing](wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING). + /// + /// [VK_GOOGLE_display_timing]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_GOOGLE_display_timing.html + #[track_caller] + pub fn set_next_present_time(&self, present_timing: vk::PresentTimeGOOGLE) { + let mut swapchain = self.swapchain.write(); + swapchain + .as_mut() + .expect("Surface should have been configured") + .as_any_mut() + .downcast_mut::<swapchain::NativeSwapchain>() + .expect("Surface should have a native Vulkan swapchain") + .set_next_present_time(present_timing); + } +} + +#[derive(Debug)] +pub struct SurfaceTexture { + index: u32, + texture: Texture, + metadata: Box<dyn swapchain::SurfaceTextureMetadata>, +} + +impl crate::DynSurfaceTexture for SurfaceTexture {} + +impl Borrow<Texture> for SurfaceTexture { + fn borrow(&self) -> &Texture { + &self.texture + } +} + +impl Borrow<dyn crate::DynTexture> for SurfaceTexture { + fn borrow(&self) -> &dyn crate::DynTexture { + &self.texture + } +} + +#[derive(Debug)] +pub struct Adapter { + raw: vk::PhysicalDevice, + instance: Arc<InstanceShared>, + //queue_families: Vec<vk::QueueFamilyProperties>, + known_memory_flags: vk::MemoryPropertyFlags, + phd_capabilities: adapter::PhysicalDeviceProperties, + phd_features: PhysicalDeviceFeatures, + downlevel_flags: wgt::DownlevelFlags, + private_caps: PrivateCapabilities, + workarounds: Workarounds, +} + +// TODO there's no reason why this can't be unified--the function pointers should all be the same--it's not clear how to do this with `ash`. +enum ExtensionFn<T> { + /// The loaded function pointer struct for an extension. + Extension(T), + /// The extension was promoted to a core version of Vulkan and the functions on `ash`'s `DeviceV1_x` traits should be used. + Promoted, +} + +struct DeviceExtensionFunctions { + debug_utils: Option<ext::debug_utils::Device>, + draw_indirect_count: Option<khr::draw_indirect_count::Device>, + timeline_semaphore: Option<ExtensionFn<khr::timeline_semaphore::Device>>, + ray_tracing: Option<RayTracingDeviceExtensionFunctions>, + ray_tracing_pipelines: Option<khr::ray_tracing_pipeline::Device>, + mesh_shading: Option<ext::mesh_shader::Device>, + #[cfg_attr(not(unix), allow(dead_code))] + external_memory_fd: Option<khr::external_memory_fd::Device>, +} + +struct RayTracingDeviceExtensionFunctions { + acceleration_structure: khr::acceleration_structure::Device, + buffer_device_address: khr::buffer_device_address::Device, +} + +/// Set of internal capabilities, which don't show up in the exposed +/// device geometry, but affect the code paths taken internally. +#[derive(Clone, Debug)] +struct PrivateCapabilities { + image_view_usage: bool, + timeline_semaphores: bool, + texture_d24: bool, + texture_d24_s8: bool, + texture_s8: bool, + /// Ability to present contents to any screen. Only needed to work around broken platform configurations. + can_present: bool, + non_coherent_map_mask: wgt::BufferAddress, + multi_draw_indirect: bool, + max_draw_indirect_count: u32, + + /// True if this adapter advertises the [`robustBufferAccess`][vrba] feature. + /// + /// Note that Vulkan's `robustBufferAccess` is not sufficient to implement + /// `wgpu_hal`'s guarantee that shaders will not access buffer contents via + /// a given bindgroup binding outside that binding's [accessible + /// region][ar]. Enabling `robustBufferAccess` does ensure that + /// out-of-bounds reads and writes are not undefined behavior (that's good), + /// but still permits out-of-bounds reads to return data from anywhere + /// within the buffer, not just the accessible region. + /// + /// [ar]: ../struct.BufferBinding.html#accessible-region + /// [vrba]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#features-robustBufferAccess + robust_buffer_access: bool, + + robust_image_access: bool, + + /// True if this adapter supports the [`VK_EXT_robustness2`] extension's + /// [`robustBufferAccess2`] feature. + /// + /// This is sufficient to implement `wgpu_hal`'s [required bounds-checking][ar] of + /// shader accesses to buffer contents. If this feature is not available, + /// this backend must have Naga inject bounds checks in the generated + /// SPIR-V. + /// + /// [`VK_EXT_robustness2`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_EXT_robustness2.html + /// [`robustBufferAccess2`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkPhysicalDeviceRobustness2FeaturesEXT.html#features-robustBufferAccess2 + /// [ar]: ../struct.BufferBinding.html#accessible-region + robust_buffer_access2: bool, + + robust_image_access2: bool, + zero_initialize_workgroup_memory: bool, + image_format_list: bool, + maximum_samplers: u32, + + /// True if this adapter supports the [`VK_KHR_shader_integer_dot_product`] extension + /// (promoted to Vulkan 1.3). + /// + /// This is used to generate optimized code for WGSL's `dot4{I, U}8Packed`. + /// + /// [`VK_KHR_shader_integer_dot_product`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_shader_integer_dot_product.html + shader_integer_dot_product: bool, + + /// True if this adapter supports 8-bit integers provided by the + /// [`VK_KHR_shader_float16_int8`] extension (promoted to Vulkan 1.2). + /// + /// Allows shaders to declare the "Int8" capability. Note, however, that this + /// feature alone allows the use of 8-bit integers "only in the `Private`, + /// `Workgroup` (for non-Block variables), and `Function` storage classes" + /// ([see spec]). To use 8-bit integers in the interface storage classes (e.g., + /// `StorageBuffer`), you also need to enable the corresponding feature in + /// `VkPhysicalDevice8BitStorageFeatures` and declare the corresponding SPIR-V + /// capability (e.g., `StorageBuffer8BitAccess`). + /// + /// [`VK_KHR_shader_float16_int8`]: https://registry.khronos.org/vulkan/specs/latest/man/html/VK_KHR_shader_float16_int8.html + /// [see spec]: https://registry.khronos.org/vulkan/specs/latest/man/html/VkPhysicalDeviceShaderFloat16Int8Features.html#extension-features-shaderInt8 + shader_int8: bool, + + /// This is done to panic before undefined behavior, and is imperfect. + /// Basically, to allow implementations to emulate mv using instancing, if you + /// want to draw `n` instances to VR, you must draw `2n` instances, but you + /// can never draw more than `u32::MAX` instances. Therefore, when drawing + /// multiview on some vulkan implementations, it might restrict the instance + /// count, which isn't usually a thing in webgpu. We don't expose this limit + /// because its strange, i.e. only occurs on certain vulkan implementations + /// if you are drawing more than 128 million instances. We still want to avoid + /// undefined behavior in this situation, so we panic if the limit is violated. + multiview_instance_index_limit: u32, + + /// BufferUsages::ACCELERATION_STRUCTURE_SCRATCH allows usage as a scratch buffer. + /// Vulkan has no way to specify this as a usage, and it maps to other usages, but + /// these usages do not have as high of an alignment requirement using the buffer as + /// a scratch buffer when building acceleration structures. + scratch_buffer_alignment: u32, + + /// `get_raytracing_pipeline_group_data` requires both a group count and a data size. + /// The data size parameter is just this * the group count, so we store this to not + /// require an unnecessary parameter. + ray_tracing_pipeline_group_data_size: u32, +} + +bitflags::bitflags!( + /// Workaround flags. + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] + pub struct Workarounds: u32 { + /// Only generate SPIR-V for one entry point at a time. + const SEPARATE_ENTRY_POINTS = 0x1; + /// Qualcomm OOMs when there are zero color attachments but a non-null pointer + /// to a subpass resolve attachment array. This nulls out that pointer in that case. + const EMPTY_RESOLVE_ATTACHMENT_LISTS = 0x2; + /// If the following code returns false, then nvidia will end up filling the wrong range. + /// + /// ```skip + /// fn nvidia_succeeds() -> bool { + /// # let (copy_length, start_offset) = (0, 0); + /// if copy_length >= 4096 { + /// if start_offset % 16 != 0 { + /// if copy_length == 4096 { + /// return true; + /// } + /// if copy_length % 16 == 0 { + /// return false; + /// } + /// } + /// } + /// true + /// } + /// ``` + /// + /// As such, we need to make sure all calls to vkCmdFillBuffer are aligned to 16 bytes + /// if they cover a range of 4096 bytes or more. + const FORCE_FILL_BUFFER_WITH_SIZE_GREATER_4096_ALIGNED_OFFSET_16 = 0x4; + } +); + +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +struct AttachmentKey { + format: vk::Format, + layout: vk::ImageLayout, + ops: crate::AttachmentOps, +} + +impl AttachmentKey { + /// Returns an attachment key for a compatible attachment. + fn compatible(format: vk::Format, layout: vk::ImageLayout) -> Self { + Self { + format, + layout, + ops: crate::AttachmentOps::all(), + } + } +} + +#[derive(Clone, Eq, Hash, PartialEq)] +struct ColorAttachmentKey { + base: AttachmentKey, + resolve: Option<AttachmentKey>, +} + +#[derive(Clone, Eq, Hash, PartialEq)] +struct DepthStencilAttachmentKey { + base: AttachmentKey, + stencil_ops: crate::AttachmentOps, +} + +#[derive(Clone, Eq, Default, Hash, PartialEq)] +struct RenderPassKey { + colors: ArrayVec<Option<ColorAttachmentKey>, { crate::MAX_COLOR_ATTACHMENTS }>, + depth_stencil: Option<DepthStencilAttachmentKey>, + sample_count: u32, + multiview_mask: Option<NonZeroU32>, +} + +struct DeviceShared { + raw: ash::Device, + family_index: u32, + queue_index: u32, + raw_queue: vk::Queue, + instance: Arc<InstanceShared>, + physical_device: vk::PhysicalDevice, + enabled_extensions: Vec<&'static CStr>, + extension_fns: DeviceExtensionFunctions, + vendor_id: u32, + pipeline_cache_validation_key: [u8; 16], + timestamp_period: f32, + private_caps: PrivateCapabilities, + workarounds: Workarounds, + features: wgt::Features, + render_passes: Mutex<FastHashMap<RenderPassKey, vk::RenderPass>>, + sampler_cache: Mutex<sampler::SamplerCache>, + memory_allocations_counter: InternalCounter, + + /// Because we have cached framebuffers which are not deleted from until + /// the device is destroyed, if the implementation of vulkan re-uses handles + /// we need some way to differentiate between the old handle and the new handle. + /// This factory allows us to have a dedicated identity value for each texture. + texture_identity_factory: ResourceIdentityFactory<vk::Image>, + /// As above, for texture views. + texture_view_identity_factory: ResourceIdentityFactory<vk::ImageView>, + + empty_descriptor_set_layout: vk::DescriptorSetLayout, + + // The `drop_guard` field must be the last field of this struct so it is dropped last. + // Do not add new fields after it. + drop_guard: Option<crate::DropGuard>, +} + +impl Drop for DeviceShared { + fn drop(&mut self) { + for &raw in self.render_passes.lock().values() { + unsafe { self.raw.destroy_render_pass(raw, None) }; + } + unsafe { + self.raw + .destroy_descriptor_set_layout(self.empty_descriptor_set_layout, None) + }; + if self.drop_guard.is_none() { + unsafe { self.raw.destroy_device(None) }; + } + } +} + +#[expect( + missing_debug_implementations, + reason = "needs work to not be disastrously verbose" +)] +pub struct Device { + mem_allocator: Mutex<gpu_allocator::vulkan::Allocator>, + desc_allocator: Mutex<descriptor::DescriptorAllocator>, + valid_ash_memory_types: u32, + naga_options: naga::back::spv::Options<'static>, + #[cfg(feature = "renderdoc")] + render_doc: crate::auxil::renderdoc::RenderDoc, + counters: Arc<wgt::HalCounters>, + // Struct members are dropped from first to last, put the Device last to ensure that + // all resources that depends on it are destroyed before it like the mem_allocator + shared: Arc<DeviceShared>, +} + +impl Drop for Device { + fn drop(&mut self) {} +} + +/// Semaphores for forcing queue submissions to run in order. +/// +/// The [`wgpu_hal::Queue`] trait promises that if two calls to [`submit`] are +/// ordered, then the first submission will finish on the GPU before the second +/// submission begins. To get this behavior on Vulkan we need to pass semaphores +/// to [`vkQueueSubmit`] for the commands to wait on before beginning execution, +/// and to signal when their execution is done. +/// +/// Normally this can be done with a single semaphore, waited on and then +/// signalled for each submission. At any given time there's exactly one +/// submission that would signal the semaphore, and exactly one waiting on it, +/// as Vulkan requires. +/// +/// However, as of Oct 2021, bug [#5508] in the Mesa ANV drivers caused them to +/// hang if we use a single semaphore. The workaround is to alternate between +/// two semaphores. The bug has been fixed in Mesa, but we should probably keep +/// the workaround until, say, Oct 2026. +/// +/// [`wgpu_hal::Queue`]: crate::Queue +/// [`submit`]: crate::Queue::submit +/// [`vkQueueSubmit`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkQueueSubmit +/// [#5508]: https://gitlab.freedesktop.org/mesa/mesa/-/issues/5508 +#[derive(Clone)] +struct RelaySemaphores { + /// The semaphore the next submission should wait on before beginning + /// execution on the GPU. This is `None` for the first submission, which + /// should not wait on anything at all. + wait: Option<vk::Semaphore>, + + /// The semaphore the next submission should signal when it has finished + /// execution on the GPU. + signal: vk::Semaphore, +} + +impl RelaySemaphores { + fn new(device: &DeviceShared) -> Result<Self, crate::DeviceError> { + Ok(Self { + wait: None, + signal: device.new_binary_semaphore("RelaySemaphores: 1")?, + }) + } + + /// Advances the semaphores, returning the semaphores that should be used for a submission. + fn advance(&mut self, device: &DeviceShared) -> Result<Self, crate::DeviceError> { + let old = self.clone(); + + // Build the state for the next submission. + match self.wait { + None => { + // The `old` values describe the first submission to this queue. + // The second submission should wait on `old.signal`, and then + // signal a new semaphore which we'll create now. + self.wait = Some(old.signal); + self.signal = device.new_binary_semaphore("RelaySemaphores: 2")?; + } + Some(ref mut wait) => { + // What this submission signals, the next should wait. + mem::swap(wait, &mut self.signal); + } + }; + + Ok(old) + } + + /// Destroys the semaphores. + unsafe fn destroy(&self, device: &ash::Device) { + unsafe { + if let Some(wait) = self.wait { + device.destroy_semaphore(wait, None); + } + device.destroy_semaphore(self.signal, None); + } + } +} + +pub struct Queue { + raw: vk::Queue, + device: Arc<DeviceShared>, + family_index: u32, + relay_semaphores: Mutex<RelaySemaphores>, + signal_semaphores: Mutex<SemaphoreList>, + wait_semaphores: Mutex<SemaphoreList>, +} + +impl fmt::Debug for Queue { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let Self { + raw: _, + device: _, + family_index, + relay_semaphores: _, + signal_semaphores: _, + wait_semaphores: _, + } = self; + f.debug_struct("Queue") + .field("family_index", family_index) + .finish_non_exhaustive() + } +} + +impl Queue { + pub fn as_raw(&self) -> vk::Queue { + self.raw + } +} + +impl Drop for Queue { + fn drop(&mut self) { + unsafe { self.relay_semaphores.lock().destroy(&self.device.raw) }; + } +} +#[derive(Debug)] +enum BufferMemoryBacking { + Managed(gpu_allocator::vulkan::Allocation), + VulkanMemory { + memory: vk::DeviceMemory, + offset: u64, + size: u64, + }, +} +impl BufferMemoryBacking { + fn memory(&self) -> vk::DeviceMemory { + match self { + Self::Managed(m) => unsafe { m.memory() }, + Self::VulkanMemory { memory, .. } => *memory, + } + } + fn offset(&self) -> u64 { + match self { + Self::Managed(m) => m.offset(), + Self::VulkanMemory { offset, .. } => *offset, + } + } + fn size(&self) -> u64 { + match self { + Self::Managed(m) => m.size(), + Self::VulkanMemory { size, .. } => *size, + } + } +} +/// Describes who owns a [`Buffer`]'s `vk::Buffer` handle and its backing memory, +/// and therefore what cleanup is required when the buffer is destroyed. +#[derive(Debug)] +enum BufferOwnership { + /// wgpu-hal owns the `vk::Buffer` and its backing memory. On cleanup the buffer + /// handle is destroyed and the memory is released. + Managed(Mutex<BufferMemoryBacking>), + /// wgpu-hal owns the `vk::Buffer` handle but the backing memory is kept alive + /// by the caller. On cleanup only the buffer handle is destroyed. + RawHandle, + /// Caller owns the `vk::Buffer` and its backing memory. On cleanup the + /// [`crate::DropGuard`] runs the caller's cleanup callback and wgpu-hal touches + /// neither the handle nor the memory. + External(crate::DropGuard), +} + +#[derive(Debug)] +pub struct Buffer { + raw: vk::Buffer, + + // This field must be last, because it may contain a `DropGuard` which needs to be dropped after all other fields. + ownership: BufferOwnership, +} +impl Buffer { + /// # Safety + /// + /// - `vk_buffer`'s memory must be managed by the caller + /// - Externally imported buffers can't be mapped by `wgpu` + pub unsafe fn from_raw(vk_buffer: vk::Buffer) -> Self { + Self { + raw: vk_buffer, + ownership: BufferOwnership::RawHandle, + } + } + + /// # Safety + /// - `vk_buffer` must outlive the returned `Buffer`. + /// - wgpu-hal will NOT call `vkDestroyBuffer`; the caller remains responsible for the buffer handle's destruction. + /// The `drop_callback` runs when the `Buffer` drops and may be used to release caller-side bookkeeping. + /// - Externally imported buffers can't be mapped by `wgpu`. + pub unsafe fn from_raw_externally_owned( + vk_buffer: vk::Buffer, + drop_callback: crate::DropCallback, + ) -> Self { + Self { + raw: vk_buffer, + ownership: BufferOwnership::External(crate::DropGuard::new(drop_callback)), + } + } + + /// # Safety + /// - We will use this buffer and the buffer's backing memory range as if we have exclusive ownership over it, until the wgpu resource is dropped and the wgpu-hal object is cleaned up + /// - Externally imported buffers can't be mapped by `wgpu` + /// - `offset` and `size` must be valid with the allocation of `memory` + pub unsafe fn from_raw_managed( + vk_buffer: vk::Buffer, + memory: vk::DeviceMemory, + offset: u64, + size: u64, + ) -> Self { + Self { + raw: vk_buffer, + ownership: BufferOwnership::Managed(Mutex::new(BufferMemoryBacking::VulkanMemory { + memory, + offset, + size, + })), + } + } + + /// # Safety + /// - The buffer handle must not be manually destroyed + pub unsafe fn raw_handle(&self) -> vk::Buffer { + self.raw + } +} + +impl crate::DynBuffer for Buffer {} + +#[derive(Debug)] +pub struct AccelerationStructure { + raw: vk::AccelerationStructureKHR, + buffer: vk::Buffer, + allocation: gpu_allocator::vulkan::Allocation, + compacted_size_query: Option<vk::QueryPool>, +} + +impl crate::DynAccelerationStructure for AccelerationStructure {} + +#[derive(Debug)] +pub enum TextureMemory { + // shared memory in GPU allocator (owned by wgpu-hal) + Allocation(gpu_allocator::vulkan::Allocation), + + // dedicated memory (owned by wgpu-hal) + Dedicated(vk::DeviceMemory), + + // memory not owned by wgpu + External, +} + +#[derive(Debug)] +pub struct Texture { + raw: vk::Image, + memory: TextureMemory, + format: wgt::TextureFormat, + copy_size: crate::CopyExtent, + identity: ResourceIdentity<vk::Image>, + + // The `drop_guard` field must be the last field of this struct so it is dropped last. + // Do not add new fields after it. + drop_guard: Option<crate::DropGuard>, +} + +impl crate::DynTexture for Texture {} + +impl Texture { + /// # Safety + /// + /// - The image handle must not be manually destroyed + pub unsafe fn raw_handle(&self) -> vk::Image { + self.raw + } + + /// # Safety + /// + /// - The caller must not free the `vk::DeviceMemory` or + /// `gpu_alloc::MemoryBlock` in the returned `TextureMemory`. + pub unsafe fn memory(&self) -> &TextureMemory { + &self.memory + } +} + +#[derive(Debug)] +pub struct TextureView { + raw_texture: vk::Image, + raw: vk::ImageView, + _layers: NonZeroU32, + format: wgt::TextureFormat, + raw_format: vk::Format, + base_mip_level: u32, + dimension: wgt::TextureViewDimension, + texture_identity: ResourceIdentity<vk::Image>, + view_identity: ResourceIdentity<vk::ImageView>, +} + +impl crate::DynTextureView for TextureView {} + +impl TextureView { + /// # Safety + /// + /// - The image view handle must not be manually destroyed + pub unsafe fn raw_handle(&self) -> vk::ImageView { + self.raw + } + + /// Returns the raw texture view, along with its identity. + fn identified_raw_view(&self) -> IdentifiedTextureView { + IdentifiedTextureView { + raw: self.raw, + identity: self.view_identity, + } + } +} + +#[derive(Debug)] +pub struct Sampler { + raw: vk::Sampler, + create_info: vk::SamplerCreateInfo<'static>, +} + +impl crate::DynSampler for Sampler {} + +/// Information about a binding within a specific BindGroupLayout / BindGroup. +/// This will be used to construct a [`naga::back::spv::BindingInfo`], where +/// the descriptor set value will be taken from the index of the group. +#[derive(Copy, Clone, Debug)] +struct BindingInfo { + binding: u32, + binding_array_size: Option<NonZeroU32>, +} + +#[derive(Debug)] +pub struct BindGroupLayout { + raw: vk::DescriptorSetLayout, + desc_count: descriptor::DescriptorCounts, + /// Sorted list of entries. + entries: Box<[wgt::BindGroupLayoutEntry]>, + /// Map of original binding index to remapped binding index and optional + /// array size. + binding_map: Vec<(u32, BindingInfo)>, + contains_binding_arrays: bool, +} + +impl crate::DynBindGroupLayout for BindGroupLayout {} + +#[derive(Debug)] +pub struct PipelineLayout { + raw: vk::PipelineLayout, + binding_map: naga::back::spv::BindingMap, +} + +impl crate::DynPipelineLayout for PipelineLayout {} + +#[derive(Debug)] +pub struct BindGroup { + set: descriptor::DescriptorSet, +} + +impl crate::DynBindGroup for BindGroup {} + +/// Miscellaneous allocation recycling pool for `CommandAllocator`. +#[derive(Default)] +struct Temp { + marker: Vec<u8>, + buffer_barriers: Vec<vk::BufferMemoryBarrier<'static>>, + image_barriers: Vec<vk::ImageMemoryBarrier<'static>>, +} + +impl Temp { + fn clear(&mut self) { + self.marker.clear(); + self.buffer_barriers.clear(); + self.image_barriers.clear(); + } + + fn make_c_str(&mut self, name: &str) -> &CStr { + self.marker.clear(); + self.marker.extend_from_slice(name.as_bytes()); + self.marker.push(0); + unsafe { CStr::from_bytes_with_nul_unchecked(&self.marker) } + } +} + +/// Generates unique IDs for each resource of type `T`. +/// +/// Because vk handles are not permanently unique, this +/// provides a way to generate unique IDs for each resource. +struct ResourceIdentityFactory<T> { + #[cfg(not(target_has_atomic = "64"))] + next_id: Mutex<u64>, + #[cfg(target_has_atomic = "64")] + next_id: core::sync::atomic::AtomicU64, + _phantom: PhantomData<T>, +} + +impl<T> ResourceIdentityFactory<T> { + fn new() -> Self { + Self { + #[cfg(not(target_has_atomic = "64"))] + next_id: Mutex::new(0), + #[cfg(target_has_atomic = "64")] + next_id: core::sync::atomic::AtomicU64::new(0), + _phantom: PhantomData, + } + } + + /// Returns a new unique ID for a resource of type `T`. + fn next(&self) -> ResourceIdentity<T> { + #[cfg(not(target_has_atomic = "64"))] + { + let mut next_id = self.next_id.lock(); + let id = *next_id; + *next_id += 1; + ResourceIdentity { + id, + _phantom: PhantomData, + } + } + + #[cfg(target_has_atomic = "64")] + ResourceIdentity { + id: self + .next_id + .fetch_add(1, core::sync::atomic::Ordering::Relaxed), + _phantom: PhantomData, + } + } +} + +/// A unique identifier for a resource of type `T`. +/// +/// This is used as a hashable key for resources, which +/// is permanently unique through the lifetime of the program. +#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)] +struct ResourceIdentity<T> { + id: u64, + _phantom: PhantomData<T>, +} + +#[derive(Clone, Eq, Hash, PartialEq)] +struct FramebufferKey { + raw_pass: vk::RenderPass, + /// Because this is used as a key in a hash map, we need to include the identity + /// so that this hashes differently, even if the ImageView handles are the same + /// between different views. + attachment_identities: ArrayVec<ResourceIdentity<vk::ImageView>, { MAX_TOTAL_ATTACHMENTS }>, + /// While this is redundant for calculating the hash, we need access to an array + /// of all the raw ImageViews when we are creating the actual framebuffer, + /// so we store this here. + attachment_views: ArrayVec<vk::ImageView, { MAX_TOTAL_ATTACHMENTS }>, + extent: wgt::Extent3d, +} + +impl FramebufferKey { + fn push_view(&mut self, view: IdentifiedTextureView) { + self.attachment_identities.push(view.identity); + self.attachment_views.push(view.raw); + } +} + +/// A texture view paired with its identity. +#[derive(Copy, Clone)] +struct IdentifiedTextureView { + raw: vk::ImageView, + identity: ResourceIdentity<vk::ImageView>, +} + +#[derive(Clone, Eq, Hash, PartialEq)] +struct TempTextureViewKey { + texture: vk::Image, + /// As this is used in a hashmap, we need to + /// include the identity so that this hashes differently, + /// even if the Image handles are the same between different images. + texture_identity: ResourceIdentity<vk::Image>, + format: vk::Format, + mip_level: u32, + depth_slice: u32, +} + +// Any state in this struct that may be dirty after an abandoned encoding must +// be reset for reused encoders in `begin_encoding`. +pub struct CommandEncoder { + raw: vk::CommandPool, + device: Arc<DeviceShared>, + + /// The current command buffer, if `self` is in the ["recording"] + /// state. + /// + /// ["recording"]: crate::CommandEncoder + /// + /// If non-`null`, the buffer is in the Vulkan "recording" state. + active: vk::CommandBuffer, + + /// What kind of pass we are currently within: compute or render. + bind_point: vk::PipelineBindPoint, + + /// Allocation recycling pool for this encoder. + temp: Temp, + + /// A pool of available command buffers. + /// + /// These are all in the Vulkan "initial" state. + free: Vec<vk::CommandBuffer>, + + /// A pool of discarded command buffers. + /// + /// These could be in any Vulkan state except "pending". + discarded: Vec<vk::CommandBuffer>, + + /// If this is true, the active renderpass enabled a debug span, + /// and needs to be disabled on renderpass close. + rpass_debug_marker_active: bool, + + /// If set, the end of the next render/compute pass will write a timestamp at + /// the given pool & location. + end_of_pass_timer_query: Option<(vk::QueryPool, u32)>, + + framebuffers: FastHashMap<FramebufferKey, vk::Framebuffer>, + temp_texture_views: FastHashMap<TempTextureViewKey, IdentifiedTextureView>, + + counters: Arc<wgt::HalCounters>, + + current_pipeline_is_multiview: bool, +} + +impl Drop for CommandEncoder { + fn drop(&mut self) { + // SAFETY: + // + // VUID-vkDestroyCommandPool-commandPool-00041: wgpu_hal requires that a + // `CommandBuffer` must live until its execution is complete, and that a + // `CommandBuffer` must not outlive the `CommandEncoder` that built it. + // Thus, we know that none of our `CommandBuffers` are in the "pending" + // state. + // + // The other VUIDs are pretty obvious. + unsafe { + // `vkDestroyCommandPool` also frees any command buffers allocated + // from that pool, so there's no need to explicitly call + // `vkFreeCommandBuffers` on `cmd_encoder`'s `free` and `discarded` + // fields. + self.device.raw.destroy_command_pool(self.raw, None); + } + + for (_, fb) in self.framebuffers.drain() { + unsafe { self.device.raw.destroy_framebuffer(fb, None) }; + } + + for (_, view) in self.temp_texture_views.drain() { + unsafe { self.device.raw.destroy_image_view(view.raw, None) }; + } + + self.counters.command_encoders.sub(1); + } +} + +impl CommandEncoder { + /// # Safety + /// + /// - The command buffer handle must not be manually destroyed + pub unsafe fn raw_handle(&self) -> vk::CommandBuffer { + self.active + } +} + +impl fmt::Debug for CommandEncoder { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("CommandEncoder") + .field("raw", &self.raw) + .finish() + } +} + +#[derive(Debug)] +pub struct CommandBuffer { + raw: vk::CommandBuffer, +} + +impl crate::DynCommandBuffer for CommandBuffer {} + +#[derive(Debug)] +pub enum ShaderModule { + Raw(vk::ShaderModule), + Intermediate { + naga_shader: crate::NagaShader, + runtime_checks: wgt::ShaderRuntimeChecks, + }, +} + +impl crate::DynShaderModule for ShaderModule {} + +#[derive(Debug)] +pub struct RenderPipeline { + raw: vk::Pipeline, + is_multiview: bool, +} + +impl crate::DynRenderPipeline for RenderPipeline {} + +#[derive(Debug)] +pub struct ComputePipeline { + raw: vk::Pipeline, +} + +impl crate::DynComputePipeline for ComputePipeline {} + +#[derive(Debug)] +pub struct RayTracingPipeline { + raw: vk::Pipeline, +} + +impl crate::DynRayTracingPipeline for RayTracingPipeline {} + +#[derive(Debug)] +pub struct PipelineCache { + raw: vk::PipelineCache, +} + +impl crate::DynPipelineCache for PipelineCache {} + +#[derive(Debug)] +pub struct QuerySet { + raw: vk::QueryPool, +} + +impl crate::DynQuerySet for QuerySet {} + +/// The [`Api::Fence`] type for [`vulkan::Api`]. +/// +/// This is an `enum` because there are two possible implementations of +/// `wgpu-hal` fences on Vulkan: Vulkan fences, which work on any version of +/// Vulkan, and Vulkan timeline semaphores, which are easier and cheaper but +/// require non-1.0 features. +/// +/// [`Device::create_fence`] returns a [`TimelineSemaphore`] if +/// [`VK_KHR_timeline_semaphore`] is available and enabled, and a [`FencePool`] +/// otherwise. +/// +/// [`Api::Fence`]: crate::Api::Fence +/// [`vulkan::Api`]: Api +/// [`Device::create_fence`]: crate::Device::create_fence +/// [`TimelineSemaphore`]: Fence::TimelineSemaphore +/// [`VK_KHR_timeline_semaphore`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#VK_KHR_timeline_semaphore +/// [`FencePool`]: Fence::FencePool +#[derive(Debug)] +pub enum Fence { + /// A Vulkan [timeline semaphore]. + /// + /// These are simpler to use than Vulkan fences, since timeline semaphores + /// work exactly the way [`wpgu_hal::Api::Fence`] is specified to work. + /// + /// [timeline semaphore]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#synchronization-semaphores + /// [`wpgu_hal::Api::Fence`]: crate::Api::Fence + TimelineSemaphore(vk::Semaphore), + + /// A collection of Vulkan [fence]s, each associated with a [`FenceValue`]. + /// + /// The effective [`FenceValue`] of this variant is the greater of + /// `last_completed` and the maximum value associated with a signalled fence + /// in `active`. + /// + /// Fences are available in all versions of Vulkan, but since they only have + /// two states, "signaled" and "unsignaled", we need to use a separate fence + /// for each queue submission we might want to wait for, and remember which + /// [`FenceValue`] each one represents. + /// + /// One should keep the fence pool read while there are any references to the + /// fences inside of them. This ensures there are no race conditions when + /// resetting the fences + /// + /// [fence]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#synchronization-fences + /// [`FenceValue`]: crate::FenceValue + FencePool(RwLock<FencePool>), +} + +/// A shared fence type. The arc is expect to have a ref-count of one once a function has finished being called +/// +/// A fence should have access synchronised as fence resetting might happen at any point. Resetting checks the ref-count +/// of the fence, so instead of copying the fence, it should have its `Arc` container cloned which shows not to reset +/// this fence as it is being used. +pub(super) type SynchronizedFence = Arc<vk::Fence>; + +#[derive(Debug)] +pub struct FencePool { + last_completed: crate::FenceValue, + /// The pending fence values have to be ascending. + active: Vec<(crate::FenceValue, SynchronizedFence)>, + // Don't need extra synchronisation around the fences here, if they are used they should be put into active. + free: Vec<vk::Fence>, +} + +impl crate::DynFence for Fence {} + +impl Fence { + /// Return the highest [`FenceValue`] among the signalled fences in `active`. + /// + /// As an optimization, assume that we already know that the fence has + /// reached `last_completed`, and don't bother checking fences whose values + /// are less than that: those fences remain in the `active` array only + /// because we haven't called `maintain` yet to clean them up. + /// + /// [`FenceValue`]: crate::FenceValue + fn check_active( + device: &ash::Device, + mut last_completed: crate::FenceValue, + active: &[(crate::FenceValue, SynchronizedFence)], + ) -> Result<crate::FenceValue, crate::DeviceError> { + for &(value, ref raw) in active.iter() { + unsafe { + if value > last_completed + && device + // Don't need to clone as active should be from a read or + // write lock which means this is already synchronised. + .get_fence_status(**raw) + .map_err(map_host_device_oom_and_lost_err)? + { + last_completed = value; + } + } + } + Ok(last_completed) + } + + /// Return the highest signalled [`FenceValue`] for `self`. + /// + /// [`FenceValue`]: crate::FenceValue + fn get_latest( + &self, + device: &ash::Device, + extension: Option<&ExtensionFn<khr::timeline_semaphore::Device>>, + ) -> Result<crate::FenceValue, crate::DeviceError> { + match *self { + Self::TimelineSemaphore(raw) => unsafe { + Ok(match *extension.unwrap() { + ExtensionFn::Extension(ref ext) => ext + .get_semaphore_counter_value(raw) + .map_err(map_host_device_oom_and_lost_err)?, + ExtensionFn::Promoted => device + .get_semaphore_counter_value(raw) + .map_err(map_host_device_oom_and_lost_err)?, + }) + }, + Self::FencePool(ref pool) => { + let FencePool { + last_completed, + ref active, + free: _, + } = *pool.read(); + Self::check_active(device, last_completed, active) + } + } + } + + /// Trim the internal state of this [`Fence`]. + /// + /// This function has no externally visible effect, but you should call it + /// periodically to keep this fence's resource consumption under control. + /// + /// For fences using the [`FencePool`] implementation, this function + /// recycles fences that have been signaled. If you don't call this, + /// [`Queue::submit`] will just keep allocating a new Vulkan fence every + /// time it's called. + /// + /// [`FencePool`]: Fence::FencePool + /// [`Queue::submit`]: crate::Queue::submit + fn maintain(&self, device: &ash::Device) -> Result<(), crate::DeviceError> { + match *self { + Self::TimelineSemaphore(_) => {} + Self::FencePool(ref pool) => { + let FencePool { + ref mut last_completed, + ref mut active, + ref mut free, + } = *pool.write(); + + let base_free = free.len(); + let latest = Self::check_active(device, *last_completed, active)?; + + active.retain_mut(|&mut (value, ref mut fence)| { + if value > latest { + true + } else if let Some(fence) = Arc::get_mut(fence) { + // No other references to these, so we have exclusive access. Add them to free and reset them later, + // but drop them from active immediately + free.push(*fence); + false + } else { + // some other function is using it. Although this shouldn't be to long, + // maintain shouldn't block, and it should be cleared up by the next time it happens + true + } + }); + + if free.len() != base_free { + unsafe { device.reset_fences(&free[base_free..]) } + .map_err(map_device_oom_err)? + } + *last_completed = latest; + } + } + Ok(()) + } +} + +impl crate::Queue for Queue { + type A = Api; + + unsafe fn submit( + &self, + command_buffers: &[&CommandBuffer], + surface_textures: &[&SurfaceTexture], + (signal_fence, signal_value): (&Fence, crate::FenceValue), + ) -> Result<(), crate::DeviceError> { + let mut fence_raw = vk::Fence::null(); + + let mut wait_semaphores = SemaphoreList::new(SemaphoreListMode::Wait); + let mut signal_semaphores = SemaphoreList::new(SemaphoreListMode::Signal); + + // Double check that the same swapchain image isn't being given to us multiple times, + // as that will deadlock when we try to lock them all. + debug_assert!( + { + let mut check = HashSet::with_capacity(surface_textures.len()); + // We compare the Box by pointer, as Eq isn't well defined for SurfaceSemaphores. + for st in surface_textures { + let ptr: *const () = <*const _>::cast(&*st.metadata); + check.insert(ptr as usize); + } + check.len() == surface_textures.len() + }, + "More than one surface texture is being used from the same swapchain. This will cause a deadlock in release." + ); + + let locked_swapchain_semaphores = surface_textures + .iter() + .map(|st| st.metadata.get_semaphore_guard()) + .collect::<Vec<_>>(); + + for mut semaphores in locked_swapchain_semaphores { + semaphores.set_used_fence_value(signal_value); + + // If we're the first submission to operate on this image, wait on + // its acquire semaphore, to make sure the presentation engine is + // done with it. + if let Some(sem) = semaphores.get_acquire_wait_semaphore() { + wait_semaphores.push_wait(sem, vk::PipelineStageFlags::TOP_OF_PIPE); + } + + // Get a semaphore to signal when we're done writing to this surface + // image. Presentation of this image will wait for this. + let signal_semaphore = semaphores.get_submit_signal_semaphore(&self.device)?; + signal_semaphores.push_signal(signal_semaphore); + } + + let mut guard = self.signal_semaphores.lock(); + if !guard.is_empty() { + signal_semaphores.append(&mut guard); + } + + let mut wait_guard = self.wait_semaphores.lock(); + if !wait_guard.is_empty() { + wait_semaphores.append(&mut wait_guard); + } + + // In order for submissions to be strictly ordered, we encode a dependency between each submission + // using a pair of semaphores. This adds a wait if it is needed, and signals the next semaphore. + let semaphore_state = self.relay_semaphores.lock().advance(&self.device)?; + + if let Some(sem) = semaphore_state.wait { + wait_semaphores.push_wait( + SemaphoreType::Binary(sem), + vk::PipelineStageFlags::TOP_OF_PIPE, + ); + } + + signal_semaphores.push_signal(SemaphoreType::Binary(semaphore_state.signal)); + + // We need to signal our wgpu::Fence if we have one, this adds it to the signal list. + signal_fence.maintain(&self.device.raw)?; + // Keeping the Arc around is probably unneeded - the fence should never be signaled as it was reset, + // and newer submits should not happen until this submit is done. Therefore, it should be too high + // to be reset. + let shared_fence; + match *signal_fence { + Fence::TimelineSemaphore(raw) => { + signal_semaphores.push_signal(SemaphoreType::Timeline(raw, signal_value)); + } + Fence::FencePool(ref pool) => { + let FencePool { + ref mut active, + ref mut free, + .. + } = *pool.write(); + shared_fence = match free.pop() { + Some(raw) => Arc::new(raw), + None => unsafe { + let fence = self + .device + .raw + .create_fence(&vk::FenceCreateInfo::default(), None) + .map_err(map_host_device_oom_err)?; + Arc::new(fence) + }, + }; + fence_raw = *shared_fence; + active.push((signal_value, shared_fence.clone())); + } + } + + let vk_cmd_buffers = command_buffers + .iter() + .map(|cmd| cmd.raw) + .collect::<Vec<_>>(); + + let mut vk_info = vk::SubmitInfo::default().command_buffers(&vk_cmd_buffers); + let mut vk_timeline_info = mem::MaybeUninit::uninit(); + vk_info = SemaphoreList::add_to_submit( + &mut wait_semaphores, + &mut signal_semaphores, + vk_info, + &mut vk_timeline_info, + ); + + profiling::scope!("vkQueueSubmit"); + unsafe { + self.device + .raw + .queue_submit(self.raw, &[vk_info], fence_raw) + .map_err(map_host_device_oom_and_lost_err)? + }; + Ok(()) + } + + unsafe fn present( + &self, + surface: &Surface, + texture: SurfaceTexture, + ) -> Result<(), crate::SurfaceError> { + let mut swapchain = surface.swapchain.write(); + + unsafe { swapchain.as_mut().unwrap().present(self, texture) } + } + + unsafe fn get_timestamp_period(&self) -> f32 { + self.device.timestamp_period + } + + unsafe fn wait_for_idle(&self) -> Result<(), crate::DeviceError> { + unsafe { self.device.raw.queue_wait_idle(self.raw) } + .map_err(map_host_device_oom_and_lost_err) + } +} + +impl Queue { + pub fn raw_device(&self) -> &ash::Device { + &self.device.raw + } + + pub fn add_signal_semaphore(&self, semaphore: vk::Semaphore, semaphore_value: Option<u64>) { + let mut guard = self.signal_semaphores.lock(); + if let Some(value) = semaphore_value { + guard.push_signal(SemaphoreType::Timeline(semaphore, value)); + } else { + guard.push_signal(SemaphoreType::Binary(semaphore)); + } + } + + /// Remove `semaphore` from the pending signal list if it is still present. + /// + /// Returns `true` if the semaphore was found and removed. If the submit + /// already consumed it, this is a harmless no-op that returns `false`. + pub fn remove_signal_semaphore(&self, semaphore: vk::Semaphore) -> bool { + self.signal_semaphores.lock().remove(semaphore) + } + + /// Stage a semaphore wait on the next [`crate::Queue::submit`] call. + /// + /// `semaphore_value` selects the kind of payload the wait targets: + /// + /// - `Some(value)` - wait until `semaphore` (a timeline semaphore) has been signalled to at least `value`. + /// - `None` - wait on a binary semaphore signal. + /// + /// `stage` is the pipeline stage at which the wait blocks downstream + /// work (e.g. `vk::PipelineStageFlags::TOP_OF_PIPE` to gate the + /// entire submission, or a more specific stage when only that stage + /// reads the synchronised resource). + pub fn add_wait_semaphore( + &self, + semaphore: vk::Semaphore, + semaphore_value: Option<u64>, + stage: vk::PipelineStageFlags, + ) { + let mut guard = self.wait_semaphores.lock(); + if let Some(value) = semaphore_value { + guard.push_wait(SemaphoreType::Timeline(semaphore, value), stage); + } else { + guard.push_wait(SemaphoreType::Binary(semaphore), stage); + } + } + + /// Remove `semaphore` from the pending wait list if it is still present. + /// + /// Returns `true` if the semaphore was found and removed. If the submit + /// already consumed it, this is a no-op that returns `false`. + pub fn remove_wait_semaphore(&self, semaphore: vk::Semaphore) -> bool { + self.wait_semaphores.lock().remove(semaphore) + } +} + +/// Maps +/// +/// - VK_ERROR_OUT_OF_HOST_MEMORY +/// - VK_ERROR_OUT_OF_DEVICE_MEMORY +fn map_host_device_oom_err(err: vk::Result) -> crate::DeviceError { + match err { + vk::Result::ERROR_OUT_OF_HOST_MEMORY | vk::Result::ERROR_OUT_OF_DEVICE_MEMORY => { + get_oom_err(err) + } + e => get_unexpected_err(e), + } +} + +/// Maps +/// +/// - VK_ERROR_OUT_OF_HOST_MEMORY +/// - VK_ERROR_OUT_OF_DEVICE_MEMORY +/// - VK_ERROR_DEVICE_LOST +fn map_host_device_oom_and_lost_err(err: vk::Result) -> crate::DeviceError { + match err { + vk::Result::ERROR_DEVICE_LOST => get_lost_err(), + other => map_host_device_oom_err(other), + } +} + +/// Maps +/// +/// - VK_ERROR_OUT_OF_HOST_MEMORY +/// - VK_ERROR_OUT_OF_DEVICE_MEMORY +/// - VK_ERROR_FRAGMENTATION +fn map_host_device_oom_and_fragmentation_err(err: vk::Result) -> crate::DeviceError { + match err { + vk::Result::ERROR_FRAGMENTATION => get_oom_err(err), + other => map_host_device_oom_err(other), + } +} + +/// Maps +/// +/// - VK_ERROR_OUT_OF_HOST_MEMORY +/// - VK_ERROR_OUT_OF_DEVICE_MEMORY +/// - VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR +fn map_host_device_oom_and_ioca_err(err: vk::Result) -> crate::DeviceError { + // We don't use VK_KHR_buffer_device_address + // VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR + map_host_device_oom_err(err) +} + +/// Maps +/// +/// - VK_ERROR_OUT_OF_HOST_MEMORY +fn map_host_oom_err(err: vk::Result) -> crate::DeviceError { + match err { + vk::Result::ERROR_OUT_OF_HOST_MEMORY => get_oom_err(err), + e => get_unexpected_err(e), + } +} + +/// Maps +/// +/// - VK_ERROR_OUT_OF_DEVICE_MEMORY +fn map_device_oom_err(err: vk::Result) -> crate::DeviceError { + match err { + vk::Result::ERROR_OUT_OF_DEVICE_MEMORY => get_oom_err(err), + e => get_unexpected_err(e), + } +} + +/// Maps +/// +/// - VK_ERROR_OUT_OF_HOST_MEMORY +/// - VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR +fn map_host_oom_and_ioca_err(err: vk::Result) -> crate::DeviceError { + // We don't use VK_KHR_buffer_device_address + // VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS_KHR + map_host_oom_err(err) +} + +/// Maps +/// +/// - VK_ERROR_OUT_OF_HOST_MEMORY +/// - VK_ERROR_OUT_OF_DEVICE_MEMORY +/// - VK_PIPELINE_COMPILE_REQUIRED_EXT +/// - VK_ERROR_INVALID_SHADER_NV +fn map_pipeline_err(err: vk::Result) -> crate::DeviceError { + // We don't use VK_EXT_pipeline_creation_cache_control + // VK_PIPELINE_COMPILE_REQUIRED_EXT + // We don't use VK_NV_glsl_shader + // VK_ERROR_INVALID_SHADER_NV + map_host_device_oom_err(err) +} + +/// Returns [`crate::DeviceError::Unexpected`] or panics if the `internal_error_panic` +/// feature flag is enabled. +fn get_unexpected_err(_err: vk::Result) -> crate::DeviceError { + #[cfg(feature = "internal_error_panic")] + panic!("Unexpected Vulkan error: {_err:?}"); + + #[allow(unreachable_code)] + crate::DeviceError::Unexpected +} + +/// Returns [`crate::DeviceError::OutOfMemory`]. +fn get_oom_err(_err: vk::Result) -> crate::DeviceError { + crate::DeviceError::OutOfMemory +} + +/// Returns [`crate::DeviceError::Lost`] or panics if the `device_lost_panic` +/// feature flag is enabled. +fn get_lost_err() -> crate::DeviceError { + #[cfg(feature = "device_lost_panic")] + panic!("Device lost"); + + #[allow(unreachable_code)] + crate::DeviceError::Lost +} + +#[derive(Clone, Copy, Pod, Zeroable)] +#[repr(C)] +struct RawTlasInstance { + transform: [f32; 12], + custom_data_and_mask: u32, + shader_binding_table_record_offset_and_flags: u32, + acceleration_structure_reference: u64, +} + +/// Arguments to the [`CreateDeviceCallback`]. +#[derive(Debug)] +pub struct CreateDeviceCallbackArgs<'arg, 'pnext, 'this> +where + 'this: 'pnext, +{ + /// The extensions to enable for the device. You must not remove anything from this list, + /// but you may add to it. + pub extensions: &'arg mut Vec<&'static CStr>, + /// The physical device features to enable. You may enable features, but must not disable any. + pub device_features: &'arg mut PhysicalDeviceFeatures, + /// The queue create infos for the device. You may add or modify queue create infos as needed. + pub queue_create_infos: &'arg mut Vec<vk::DeviceQueueCreateInfo<'pnext>>, + /// The create info for the device. You may add or modify things in the pnext chain, but + /// do not turn features off. Additionally, do not add things to the list of extensions, + /// or to the feature set, as all changes to that member will be overwritten. + pub create_info: &'arg mut vk::DeviceCreateInfo<'pnext>, + /// We need to have `'this` in the struct, so we can declare that all lifetimes coming from + /// captures in the closure will live longer (and hence satisfy) `'pnext`. However, we + /// don't actually directly use `'this` + _phantom: PhantomData<&'this ()>, +} + +/// Callback to allow changing the vulkan device creation parameters. +/// +/// # Safety: +/// - If you want to add extensions, add the to the `Vec<'static CStr>` not the create info, +/// as the create info value will be overwritten. +/// - Callback must not remove features. +/// - Callback must not change anything to what the instance does not support. +pub type CreateDeviceCallback<'this> = + dyn for<'arg, 'pnext> FnOnce(CreateDeviceCallbackArgs<'arg, 'pnext, 'this>) + 'this; + +/// Arguments to the [`CreateInstanceCallback`]. +#[expect(missing_debug_implementations, reason = "TODO?")] +pub struct CreateInstanceCallbackArgs<'arg, 'pnext, 'this> +where + 'this: 'pnext, +{ + /// The extensions to enable for the instance. You must not remove anything from this list, + /// but you may add to it. + pub extensions: &'arg mut Vec<&'static CStr>, + /// The create info for the instance. You may add or modify things in the pnext chain, but + /// do not turn features off. Additionally, do not add things to the list of extensions, + /// all changes to that member will be overwritten. + pub create_info: &'arg mut vk::InstanceCreateInfo<'pnext>, + /// Vulkan entry point. + pub entry: &'arg ash::Entry, + /// We need to have `'this` in the struct, so we can declare that all lifetimes coming from + /// captures in the closure will live longer (and hence satisfy) `'pnext`. However, we + /// don't actually directly use `'this` + _phantom: PhantomData<&'this ()>, +} + +/// Callback to allow changing the vulkan instance creation parameters. +/// +/// # Safety: +/// - If you want to add extensions, add the to the `Vec<'static CStr>` not the create info, +/// as the create info value will be overwritten. +/// - Callback must not remove features. +/// - Callback must not change anything to what the instance does not support. +pub type CreateInstanceCallback<'this> = + dyn for<'arg, 'pnext> FnOnce(CreateInstanceCallbackArgs<'arg, 'pnext, 'this>) + 'this; diff --git a/third_party/wgpu-hal/src/vulkan/sampler.rs b/third_party/wgpu-hal/src/vulkan/sampler.rs new file mode 100644 index 000000000..b99341d21 --- /dev/null +++ b/third_party/wgpu-hal/src/vulkan/sampler.rs @@ -0,0 +1,177 @@ +//! Sampler cache for Vulkan backend. +//! +//! Nearly identical to the DX12 sampler cache, without descriptor heap management. + +use ash::vk; +use hashbrown::{hash_map::Entry, HashMap}; +use ordered_float::OrderedFloat; + +/// If the allowed sampler count is above this value, the sampler cache is disabled. +const ENABLE_SAMPLER_CACHE_CUTOFF: u32 = 1 << 20; + +/// [`vk::SamplerCreateInfo`] 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(Copy, Clone)] +struct HashableSamplerCreateInfo(vk::SamplerCreateInfo<'static>); + +impl PartialEq for HashableSamplerCreateInfo { + fn eq(&self, other: &Self) -> bool { + self.0.flags == other.0.flags + && self.0.mag_filter == other.0.mag_filter + && self.0.min_filter == other.0.min_filter + && self.0.mipmap_mode == other.0.mipmap_mode + && self.0.address_mode_u == other.0.address_mode_u + && self.0.address_mode_v == other.0.address_mode_v + && self.0.address_mode_w == other.0.address_mode_w + && OrderedFloat(self.0.mip_lod_bias) == OrderedFloat(other.0.mip_lod_bias) + && self.0.anisotropy_enable == other.0.anisotropy_enable + && OrderedFloat(self.0.max_anisotropy) == OrderedFloat(other.0.max_anisotropy) + && self.0.compare_enable == other.0.compare_enable + && self.0.compare_op == other.0.compare_op + && OrderedFloat(self.0.min_lod) == OrderedFloat(other.0.min_lod) + && OrderedFloat(self.0.max_lod) == OrderedFloat(other.0.max_lod) + && self.0.border_color == other.0.border_color + && self.0.unnormalized_coordinates == other.0.unnormalized_coordinates + } +} + +impl Eq for HashableSamplerCreateInfo {} + +impl core::hash::Hash for HashableSamplerCreateInfo { + fn hash<H: core::hash::Hasher>(&self, state: &mut H) { + self.0.flags.hash(state); + self.0.mag_filter.hash(state); + self.0.min_filter.hash(state); + self.0.mipmap_mode.hash(state); + self.0.address_mode_u.hash(state); + self.0.address_mode_v.hash(state); + self.0.address_mode_w.hash(state); + OrderedFloat(self.0.mip_lod_bias).hash(state); + self.0.anisotropy_enable.hash(state); + OrderedFloat(self.0.max_anisotropy).hash(state); + self.0.compare_enable.hash(state); + self.0.compare_op.hash(state); + OrderedFloat(self.0.min_lod).hash(state); + OrderedFloat(self.0.max_lod).hash(state); + self.0.border_color.hash(state); + self.0.unnormalized_coordinates.hash(state); + } +} + +/// Entry in the sampler cache. +struct CacheEntry { + sampler: vk::Sampler, + ref_count: u32, +} + +/// Global sampler cache. +/// +/// As some devices have a low limit (4000) on the number of unique samplers that can be created, +/// we need to cache samplers to avoid running out if people eagerly create duplicate samplers. +pub(crate) struct SamplerCache { + /// Mapping from the sampler description to sampler and reference count. + samplers: HashMap<HashableSamplerCreateInfo, CacheEntry>, + /// Maximum number of unique samplers that can be created. + total_capacity: u32, + /// If true, the sampler cache is disabled and all samplers are created on demand. + passthrough: bool, +} + +impl SamplerCache { + pub fn new(total_capacity: u32) -> Self { + let passthrough = total_capacity >= ENABLE_SAMPLER_CACHE_CUTOFF; + Self { + samplers: HashMap::new(), + total_capacity, + passthrough, + } + } + + /// Create a sampler, or return an existing one if it already exists. + /// + /// If the sampler already exists, the reference count is incremented. + /// + /// If the sampler does not exist, a new sampler is created and inserted into the cache. + /// + /// If the cache is full, an error is returned. + pub fn create_sampler( + &mut self, + device: &ash::Device, + create_info: vk::SamplerCreateInfo<'static>, + ) -> Result<vk::Sampler, crate::DeviceError> { + if self.passthrough { + return unsafe { device.create_sampler(&create_info, None) } + .map_err(super::map_host_device_oom_and_ioca_err); + }; + + // Get the number of used samplers. Needs to be done before to appease the borrow checker. + let used_samplers = self.samplers.len(); + + match self.samplers.entry(HashableSamplerCreateInfo(create_info)) { + Entry::Occupied(occupied_entry) => { + // We have found a match, so increment the refcount and return the index. + let value = occupied_entry.into_mut(); + value.ref_count += 1; + Ok(value.sampler) + } + Entry::Vacant(vacant_entry) => { + // We need to create a new sampler. + + // We need to check if we can create more samplers. + if used_samplers >= self.total_capacity as usize { + log::error!("There is no more room in the global sampler heap for more unique samplers. Your device supports a maximum of {} unique samplers.", self.samplers.len()); + return Err(crate::DeviceError::OutOfMemory); + } + + // Create the sampler. + let sampler = unsafe { device.create_sampler(&create_info, None) } + .map_err(super::map_host_device_oom_and_ioca_err)?; + + // Insert the new sampler into the mapping. + vacant_entry.insert(CacheEntry { + sampler, + ref_count: 1, + }); + + Ok(sampler) + } + } + } + + /// Decrease the reference count of a sampler and destroy it if the reference count reaches 0. + /// + /// The provided sampler is checked against the sampler in the cache to ensure there is no clerical error. + pub fn destroy_sampler( + &mut self, + device: &ash::Device, + create_info: vk::SamplerCreateInfo<'static>, + provided_sampler: vk::Sampler, + ) { + if self.passthrough { + unsafe { device.destroy_sampler(provided_sampler, None) }; + return; + }; + + let Entry::Occupied(mut hash_map_entry) = + self.samplers.entry(HashableSamplerCreateInfo(create_info)) + else { + log::error!("Trying to destroy a sampler that does not exist."); + return; + }; + let cache_entry = hash_map_entry.get_mut(); + + assert_eq!( + cache_entry.sampler, provided_sampler, + "Provided sampler does not match the sampler in the cache." + ); + + cache_entry.ref_count -= 1; + + if cache_entry.ref_count == 0 { + unsafe { device.destroy_sampler(cache_entry.sampler, None) }; + hash_map_entry.remove(); + } + } +} diff --git a/third_party/wgpu-hal/src/vulkan/semaphore_list.rs b/third_party/wgpu-hal/src/vulkan/semaphore_list.rs new file mode 100644 index 000000000..045bfe845 --- /dev/null +++ b/third_party/wgpu-hal/src/vulkan/semaphore_list.rs @@ -0,0 +1,218 @@ +//! Definition of the [`SemaphoreList`] type. + +use alloc::vec::Vec; +use ash::vk; +use core::mem::MaybeUninit; + +#[derive(Debug, PartialEq)] +pub enum SemaphoreListMode { + Wait, + Signal, +} + +/// A list of Vulkan semaphores to wait for or signal. +/// +/// This represents a list of binary or timeline semaphores, together +/// with values for the timeline semaphores, and stage masks, if these +/// are used for waiting. +/// +/// This type ensures that the array of semaphores to be signaled +/// stays aligned with the array of values for timeline semaphores +/// appearing in that list. The [`add_to_submit`] method prepares the +/// `vkQueueSubmit` arguments appropriately for whatever semaphores we +/// actually have. +/// +/// [`add_to_submit`]: SemaphoreList::add_to_submit +#[derive(Debug)] +pub struct SemaphoreList { + /// Mode of the semaphore list. Used for validation. + mode: SemaphoreListMode, + + /// Semaphores to use. + /// + /// This can be a mix of binary and timeline semaphores. + semaphores: Vec<vk::Semaphore>, + + /// Values for the timeline semaphores. + /// + /// If no timeline semaphores are present in [`semaphores`], this + /// is empty. If any timeline semaphores are present, then this + /// has the same length as [`semaphores`], with dummy !0 values + /// in the elements corresponding to binary semaphores, since + /// Vulkan ignores these. + /// + /// [`semaphores`]: Self::semaphores + values: Vec<u64>, + + /// Stage masks for wait semaphores. + /// + /// This is only used if `mode` is `Wait`. + pub stage_masks: Vec<vk::PipelineStageFlags>, +} + +impl SemaphoreList { + pub fn new(mode: SemaphoreListMode) -> Self { + Self { + mode, + semaphores: Vec::new(), + values: Vec::new(), + stage_masks: Vec::new(), + } + } + + pub fn is_empty(&self) -> bool { + self.semaphores.is_empty() + } + + /// Add this list to the semaphores to be signalled by a `vkQueueSubmit` call. + /// + /// - Set `submit_info`'s `pSignalSemaphores` list to this list's + /// semaphores. + /// + /// - If this list contains any timeline semaphores, then initialize + /// `timeline_info`, set its `pSignalSemaphoreValues` to this + /// list's values, and add it to `submit_info`s extension chain. + /// + /// Return the revised `submit_info` value. + pub fn add_to_submit<'info, 'semaphores: 'info>( + wait_semaphores: &'semaphores mut Self, + signal_semaphores: &'semaphores mut Self, + submit_info: vk::SubmitInfo<'info>, + timeline_info: &'info mut MaybeUninit<vk::TimelineSemaphoreSubmitInfo<'info>>, + ) -> vk::SubmitInfo<'info> { + wait_semaphores.check(); + signal_semaphores.check(); + + assert!(matches!(wait_semaphores.mode, SemaphoreListMode::Wait)); + assert!(matches!(signal_semaphores.mode, SemaphoreListMode::Signal)); + + let timeline_info = timeline_info.write(vk::TimelineSemaphoreSubmitInfo::default()); + + let mut uses_timeline = false; + + if !wait_semaphores.values.is_empty() { + *timeline_info = timeline_info.wait_semaphore_values(&wait_semaphores.values); + uses_timeline = true; + } + + if !signal_semaphores.values.is_empty() { + *timeline_info = timeline_info.signal_semaphore_values(&signal_semaphores.values); + uses_timeline = true; + } + + let mut submit_info = submit_info + .wait_semaphores(&wait_semaphores.semaphores) + .wait_dst_stage_mask(&wait_semaphores.stage_masks) + .signal_semaphores(&signal_semaphores.semaphores); + + if uses_timeline { + submit_info = submit_info.push_next(timeline_info); + } + + submit_info + } + + /// Add a semaphore to be signaled. Panics if this is a list of semaphores to wait. + pub fn push_signal(&mut self, semaphore: SemaphoreType) { + assert!(matches!(self.mode, SemaphoreListMode::Signal)); + self.push_inner(semaphore); + } + + /// Add a semaphore to be waited for. Panics if this is a list of semaphores to signal. + pub fn push_wait(&mut self, semaphore: SemaphoreType, stage: vk::PipelineStageFlags) { + assert!(matches!(self.mode, SemaphoreListMode::Wait)); + + self.stage_masks.push(stage); + self.push_inner(semaphore); + } + + fn push_inner(&mut self, semaphore: SemaphoreType) { + match semaphore { + SemaphoreType::Binary(semaphore) => { + self.semaphores.push(semaphore); + // Push a dummy value if necessary. + if !self.values.is_empty() { + self.values.push(!0); + } + } + SemaphoreType::Timeline(semaphore, value) => { + // We may be the first timeline semaphore, ensure that the values + // array is filled with dummy values for existing binary semaphores. + self.pad_values(); + self.semaphores.push(semaphore); + self.values.push(value); + } + } + + self.check(); + } + + /// Remove all entries matching `semaphore` from the list. Returns `true` + /// if anything was removed. + pub fn remove(&mut self, semaphore: vk::Semaphore) -> bool { + let mut removed = false; + let mut i = 0; + while i < self.semaphores.len() { + if self.semaphores[i] == semaphore { + self.semaphores.swap_remove(i); + if !self.values.is_empty() { + self.values.swap_remove(i); + } + if !self.stage_masks.is_empty() { + self.stage_masks.swap_remove(i); + } + removed = true; + } else { + i += 1; + } + } + self.check(); + removed + } + + /// Append `other` to `self`, leaving `other` empty. + pub fn append(&mut self, other: &mut Self) { + assert_eq!(self.mode, other.mode); + + // If we're about to receive values, ensure we're aligned first. + if !other.values.is_empty() { + self.pad_values(); + } + self.semaphores.append(&mut other.semaphores); + self.values.append(&mut other.values); + // If we had values, but `other` did not, re-align. + if !self.values.is_empty() { + self.pad_values(); + } + self.stage_masks.append(&mut other.stage_masks); + self.check(); + } + + /// Pad `self.values` with dummy values for binary semaphores, + /// in preparation for adding a timeline semaphore value. + /// + /// This is a no-op if we already have values. + fn pad_values(&mut self) { + self.values.resize(self.semaphores.len(), !0); + } + + #[track_caller] + fn check(&self) { + debug_assert!(self.values.is_empty() || self.values.len() == self.semaphores.len()); + match self.mode { + SemaphoreListMode::Wait => { + debug_assert!( + self.stage_masks.is_empty() || self.stage_masks.len() == self.semaphores.len() + ); + } + SemaphoreListMode::Signal => { + debug_assert!(self.stage_masks.is_empty()); + } + } + } +} + +pub enum SemaphoreType { + Binary(vk::Semaphore), + Timeline(vk::Semaphore, u64), +} diff --git a/third_party/wgpu-hal/src/vulkan/swapchain/mod.rs b/third_party/wgpu-hal/src/vulkan/swapchain/mod.rs new file mode 100644 index 000000000..9be6002e3 --- /dev/null +++ b/third_party/wgpu-hal/src/vulkan/swapchain/mod.rs @@ -0,0 +1,119 @@ +use alloc::boxed::Box; +use core::{any::Any, fmt::Debug, time::Duration}; + +use crate::vulkan::{semaphore_list::SemaphoreType, DeviceShared}; + +pub(super) use native::*; + +mod native; + +/// Win32 `HWND`, handed to a Win32 [`NativeSurface`] at construction so it can +/// build its [`DxgiHdrSource`](crate::auxil::dxgi::hdr::DxgiHdrSource) for the +/// display-HDR query. Borrowed from the app's window. +/// +/// A newtype rather than a bare `isize` so the cfg lives in one place: off Windows +/// it is an uninhabited enum, so the non-Windows surface paths can't construct one +/// and the constructor signatures stay cfg-free. +#[cfg(windows)] +#[derive(Clone, Copy)] +pub(crate) struct WindowHandle(pub(crate) windows::Win32::Foundation::HWND); + +#[cfg(not(windows))] +#[derive(Clone, Copy)] +pub(crate) enum WindowHandle {} + +pub(super) trait Surface: Send + Sync + 'static { + /// Returns the surface capabilities for the given adapter. + /// + /// Returns `None` if the surface is not compatible with the adapter. + fn surface_capabilities(&self, adapter: &super::Adapter) -> Option<crate::SurfaceCapabilities>; + + /// Creates a swapchain for the surface with the given configuration. + /// + /// If this is not the first swapchain created for the surface, the old swapchain + /// must be provided. [`Swapchain::release_resources`] must be called on the old swapchain + /// before calling this method. + unsafe fn create_swapchain( + &self, + device: &super::Device, + config: &crate::SurfaceConfiguration, + provided_old_swapchain: Option<Box<dyn Swapchain>>, + ) -> Result<Box<dyn Swapchain>, crate::SurfaceError>; + + /// This surface's current display HDR info, if it can report it. + /// + /// `Some` only for Win32 surfaces (read through DXGI); `None` otherwise + /// (Wayland / X11 / Android / Metal), which is the default. + fn display_hdr_info(&self) -> Option<wgt::DisplayHdrInfo> { + None + } + + /// Allows downcasting to the concrete type. + fn as_any(&self) -> &dyn Any; +} + +pub(super) trait Swapchain: Send + Sync + 'static { + /// Releases all resources associated with the swapchain, without + /// destroying the swapchain itself. Must be called before calling + /// either [`Surface::create_swapchain`] or dropping the swapchain. + /// + /// The swapchain must not be in use when this is called. + unsafe fn release_resources(&mut self, device: &super::Device); + + /// Acquires the next available surface texture for rendering. + /// + /// `timeout` specifies the maximum time to wait for an image to become available. + /// If `None` is specified, this function will wait indefinitely. + /// + /// Returns `Err(SurfaceError::Timeout)` if the timeout elapsed before an image became available. + unsafe fn acquire( + &mut self, + timeout: Option<Duration>, + fence: &super::Fence, + ) -> Result<crate::AcquiredSurfaceTexture<crate::api::Vulkan>, crate::SurfaceError>; + + /// Tries to discard the acquired texture without presenting it. + /// + /// In practice, this doesn't really work in the current implementations. + unsafe fn discard_texture( + &mut self, + texture: super::SurfaceTexture, + ) -> Result<(), crate::SurfaceError>; + + /// Presents the given surface texture using the queue. + unsafe fn present( + &mut self, + queue: &super::Queue, + texture: crate::vulkan::SurfaceTexture, + ) -> Result<(), crate::SurfaceError>; + + /// Allows downcasting to the concrete type. + fn as_any(&self) -> &dyn Any; + + /// Allows downcasting to the concrete type mutably. + fn as_any_mut(&mut self) -> &mut dyn Any; +} + +/// Swapchain specific metadata associated with a surface texture. +pub(super) trait SurfaceTextureMetadata: Debug + Send + Sync + 'static { + /// Returns a guard which can yield the semaphores needed for submission using this swapchain texture. + fn get_semaphore_guard(&self) -> Box<dyn SwapchainSubmissionSemaphoreGuard + '_>; + + /// Allows downcasting to the concrete type. + fn as_any(&self) -> &dyn Any; +} + +/// Guard type for managing swapchain submission semaphores. +pub(super) trait SwapchainSubmissionSemaphoreGuard { + /// Sets the Fence value for this submission. + fn set_used_fence_value(&mut self, value: u64); + + /// Gets semaphores to wait on before doing GPU work for this swapchain texture. + fn get_acquire_wait_semaphore(&mut self) -> Option<SemaphoreType>; + + /// Gets the semaphore to signal when GPU work for this swapchain texture is complete. + fn get_submit_signal_semaphore( + &mut self, + device: &DeviceShared, + ) -> Result<SemaphoreType, crate::DeviceError>; +} diff --git a/third_party/wgpu-hal/src/vulkan/swapchain/native.rs b/third_party/wgpu-hal/src/vulkan/swapchain/native.rs new file mode 100644 index 000000000..218b9ef16 --- /dev/null +++ b/third_party/wgpu-hal/src/vulkan/swapchain/native.rs @@ -0,0 +1,937 @@ +//! Vulkan Surface and Swapchain implementation using native Vulkan surfaces. + +use alloc::{boxed::Box, sync::Arc, vec::Vec}; +use core::any::Any; + +use ash::{khr, vk}; +use parking_lot::{Mutex, MutexGuard}; + +use crate::vulkan::{ + conv, map_host_device_oom_and_lost_err, + semaphore_list::SemaphoreType, + swapchain::{ + Surface, SurfaceTextureMetadata, Swapchain, SwapchainSubmissionSemaphoreGuard, WindowHandle, + }, + DeviceShared, InstanceShared, +}; + +pub(crate) struct NativeSurface { + raw: vk::SurfaceKHR, + functor: khr::surface::Instance, + instance: Arc<InstanceShared>, + /// Built from the window's `HWND` (Windows only) to answer the display-HDR + /// query; `None` for non-Win32 surfaces. + #[cfg(windows)] + hdr_source: Option<crate::auxil::dxgi::hdr::DxgiHdrSource>, +} + +impl NativeSurface { + pub fn from_vk_surface_khr( + instance: &crate::vulkan::Instance, + raw: vk::SurfaceKHR, + hwnd: Option<WindowHandle>, + ) -> Self { + #[cfg(not(windows))] + let _ = hwnd; + let functor = khr::surface::Instance::new(&instance.shared.entry, &instance.shared.raw); + Self { + raw, + functor, + instance: Arc::clone(&instance.shared), + #[cfg(windows)] + hdr_source: hwnd.map(|wh| crate::auxil::dxgi::hdr::DxgiHdrSource::new(wh.0)), + } + } + + pub fn as_raw(&self) -> vk::SurfaceKHR { + self.raw + } +} + +impl Drop for NativeSurface { + fn drop(&mut self) { + unsafe { + self.functor.destroy_surface(self.raw, None); + } + } +} + +impl Surface for NativeSurface { + fn surface_capabilities( + &self, + adapter: &crate::vulkan::Adapter, + ) -> Option<crate::SurfaceCapabilities> { + if !adapter.private_caps.can_present { + return None; + } + let queue_family_index = 0; //TODO + { + profiling::scope!("vkGetPhysicalDeviceSurfaceSupportKHR"); + match unsafe { + self.functor.get_physical_device_surface_support( + adapter.raw, + queue_family_index, + self.raw, + ) + } { + Ok(true) => (), + Ok(false) => return None, + Err(e) => { + log::error!("get_physical_device_surface_support: {e}"); + return None; + } + } + } + + let caps = { + profiling::scope!("vkGetPhysicalDeviceSurfaceCapabilitiesKHR"); + match unsafe { + self.functor + .get_physical_device_surface_capabilities(adapter.raw, self.raw) + } { + Ok(caps) => caps, + Err(e) => { + log::error!("get_physical_device_surface_capabilities: {e}"); + return None; + } + } + }; + + // If image count is 0, the support number of images is unlimited. + let max_image_count = if caps.max_image_count == 0 { + !0 + } else { + caps.max_image_count + }; + + // `0xFFFFFFFF` indicates that the extent depends on the created swapchain. + let current_extent = if caps.current_extent.width != !0 && caps.current_extent.height != !0 + { + Some(wgt::Extent3d { + width: caps.current_extent.width, + height: caps.current_extent.height, + depth_or_array_layers: 1, + }) + } else { + None + }; + + let raw_present_modes = { + profiling::scope!("vkGetPhysicalDeviceSurfacePresentModesKHR"); + match unsafe { + self.functor + .get_physical_device_surface_present_modes(adapter.raw, self.raw) + } { + Ok(present_modes) => present_modes, + Err(e) => { + log::error!("get_physical_device_surface_present_modes: {e}"); + // Per definition of `SurfaceCapabilities`, there must be at least one present mode. + return None; + } + } + }; + + let raw_surface_formats = { + profiling::scope!("vkGetPhysicalDeviceSurfaceFormatsKHR"); + match unsafe { + self.functor + .get_physical_device_surface_formats(adapter.raw, self.raw) + } { + Ok(formats) => formats, + Err(e) => { + log::error!("get_physical_device_surface_formats: {e}"); + // Per definition of `SurfaceCapabilities`, there must be at least one present format. + return None; + } + } + }; + + // Group the driver's (format, color space) pairs into one entry per + // format, preserving the driver's format order. + let mut formats: Vec<wgt::SurfaceFormatCapabilities> = Vec::new(); + for (format, color_space) in raw_surface_formats + .into_iter() + .filter_map(conv::map_vk_surface_formats) + { + let color_spaces = color_space.to_color_spaces().unwrap(); + match formats.iter_mut().find(|fc| fc.format == format) { + Some(fc) => fc.color_spaces |= color_spaces, + None => formats.push(wgt::SurfaceFormatCapabilities { + format, + color_spaces, + }), + } + } + Some(crate::SurfaceCapabilities { + formats, + // TODO: Right now we're always truncating the swap chain + // (presumably - we're actually setting the min image count which isn't necessarily the swap chain size) + // Instead, we should use extensions when available to wait in present. + // See https://github.com/gfx-rs/wgpu/issues/2869 + maximum_frame_latency: (caps.min_image_count - 1)..=(max_image_count - 1), // Note this can't underflow since both `min_image_count` is at least one and we already patched `max_image_count`. + current_extent, + usage: conv::map_vk_image_usage(caps.supported_usage_flags), + present_modes: raw_present_modes + .into_iter() + .flat_map(conv::map_vk_present_mode) + .collect(), + composite_alpha_modes: conv::map_vk_composite_alpha(caps.supported_composite_alpha), + }) + } + + unsafe fn create_swapchain( + &self, + device: &crate::vulkan::Device, + config: &crate::SurfaceConfiguration, + provided_old_swapchain: Option<Box<dyn Swapchain>>, + ) -> Result<Box<dyn Swapchain>, crate::SurfaceError> { + profiling::scope!("Device::create_swapchain"); + let functor = khr::swapchain::Device::new(&self.instance.raw, &device.shared.raw); + + let old_swapchain = provided_old_swapchain + .as_ref() + .map(|osc| osc.as_any().downcast_ref::<NativeSwapchain>().unwrap().raw) + .unwrap_or(vk::SwapchainKHR::null()); + + let color_space = conv::map_surface_color_space(config.color_space); + + let original_format = device.shared.private_caps.map_texture_format(config.format); + let mut raw_flags = vk::SwapchainCreateFlagsKHR::empty(); + let mut raw_view_formats: Vec<vk::Format> = vec![]; + if !config.view_formats.is_empty() { + raw_flags |= vk::SwapchainCreateFlagsKHR::MUTABLE_FORMAT; + raw_view_formats = config + .view_formats + .iter() + .map(|f| device.shared.private_caps.map_texture_format(*f)) + .collect(); + raw_view_formats.push(original_format); + } + + let mut info = vk::SwapchainCreateInfoKHR::default() + .flags(raw_flags) + .surface(self.raw) + .min_image_count(config.maximum_frame_latency + 1) // TODO: https://github.com/gfx-rs/wgpu/issues/2869 + .image_format(original_format) + .image_color_space(color_space) + .image_extent(vk::Extent2D { + width: config.extent.width, + height: config.extent.height, + }) + .image_array_layers(config.extent.depth_or_array_layers) + .image_usage(conv::map_texture_usage(config.usage)) + .image_sharing_mode(vk::SharingMode::EXCLUSIVE) + .pre_transform(vk::SurfaceTransformFlagsKHR::IDENTITY) + .composite_alpha(conv::map_composite_alpha_mode(config.composite_alpha_mode)) + .present_mode(conv::map_present_mode(config.present_mode)) + .clipped(true) + .old_swapchain(old_swapchain); + + let mut format_list_info = vk::ImageFormatListCreateInfo::default(); + if !raw_view_formats.is_empty() { + format_list_info = format_list_info.view_formats(&raw_view_formats); + info = info.push_next(&mut format_list_info); + } + + let result = { + profiling::scope!("vkCreateSwapchainKHR"); + unsafe { functor.create_swapchain(&info, None) } + }; + + let raw = match result { + Ok(swapchain) => swapchain, + Err(error) => { + return Err(match error { + vk::Result::ERROR_SURFACE_LOST_KHR + | vk::Result::ERROR_INITIALIZATION_FAILED => crate::SurfaceError::Lost, + vk::Result::ERROR_NATIVE_WINDOW_IN_USE_KHR => { + crate::SurfaceError::Other("Native window is in use") + } + // We don't use VK_EXT_image_compression_control + // VK_ERROR_COMPRESSION_EXHAUSTED_EXT + other => map_host_device_oom_and_lost_err(other).into(), + }); + } + }; + + let images = unsafe { functor.get_swapchain_images(raw) } + .map_err(crate::vulkan::map_host_device_oom_err)?; + + // This fence is only used to throttle acquisition on Windows. It is very important to + // avoid bad frame pacing when the Vulkan driver is using a DXGI swapchain. See + // https://github.com/gfx-rs/wgpu/issues/8310 and + // https://github.com/gfx-rs/wgpu/issues/8354 for more details. + let fence = if cfg!(target_os = "windows") { + let raw = unsafe { + device + .shared + .raw + .create_fence(&vk::FenceCreateInfo::default(), None) + .map_err(crate::vulkan::map_host_device_oom_err)? + }; + Some(raw) + } else { + None + }; + + // NOTE: It's important that we define the same number of acquire/present semaphores + // as we will need to index into them with the image index. + let acquire_semaphores = (0..images.len()) + .map(|i| { + SwapchainAcquireSemaphore::new(&device.shared, i) + .map(Mutex::new) + .map(Arc::new) + }) + .collect::<Result<Vec<_>, _>>()?; + + let present_semaphores = (0..images.len()) + .map(|i| Arc::new(Mutex::new(SwapchainPresentSemaphores::new(i)))) + .collect::<Vec<_>>(); + + Ok(Box::new(NativeSwapchain { + raw, + functor, + device: Arc::clone(&device.shared), + images, + fence, + config: config.clone(), + acquire_semaphores, + next_acquire_index: 0, + present_semaphores, + next_present_time: None, + })) + } + + #[cfg(windows)] + fn display_hdr_info(&self) -> Option<wgt::DisplayHdrInfo> { + self.hdr_source.as_ref()?.display_hdr_info() + } + + fn as_any(&self) -> &dyn Any { + self + } +} + +pub(crate) struct NativeSwapchain { + raw: vk::SwapchainKHR, + functor: khr::swapchain::Device, + device: Arc<DeviceShared>, + images: Vec<vk::Image>, + /// Fence used to wait on the acquired image. + fence: Option<vk::Fence>, + config: crate::SurfaceConfiguration, + + /// Semaphores used between image acquisition and the first submission + /// that uses that image. This is indexed using [`next_acquire_index`]. + /// + /// Because we need to provide this to [`vkAcquireNextImageKHR`], we haven't + /// received the swapchain image index for the frame yet, so we cannot use + /// that to index it. + /// + /// Before we pass this to [`vkAcquireNextImageKHR`], we ensure that we wait on + /// the submission indicated by [`previously_used_submission_index`]. This ensures + /// the semaphore is no longer in use before we use it. + /// + /// [`next_acquire_index`]: NativeSwapchain::next_acquire_index + /// [`vkAcquireNextImageKHR`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkAcquireNextImageKHR + /// [`previously_used_submission_index`]: SwapchainAcquireSemaphore::previously_used_submission_index + acquire_semaphores: Vec<Arc<Mutex<SwapchainAcquireSemaphore>>>, + /// The index of the next acquire semaphore to use. + /// + /// This is incremented each time we acquire a new image, and wraps around + /// to 0 when it reaches the end of [`acquire_semaphores`]. + /// + /// [`acquire_semaphores`]: NativeSwapchain::acquire_semaphores + next_acquire_index: usize, + + /// Semaphore sets used between all submissions that write to an image and + /// the presentation of that image. + /// + /// This is indexed by the swapchain image index returned by + /// [`vkAcquireNextImageKHR`]. + /// + /// We know it is safe to use these semaphores because use them + /// _after_ the acquire semaphore. Because the acquire semaphore + /// has been signaled, the previous presentation using that image + /// is known-finished, so this semaphore is no longer in use. + /// + /// [`vkAcquireNextImageKHR`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkAcquireNextImageKHR + present_semaphores: Vec<Arc<Mutex<SwapchainPresentSemaphores>>>, + + /// The present timing information which will be set in the next call to [`present()`](crate::Queue::present()). + /// + /// # Safety + /// + /// This must only be set if [`wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING`] is enabled, and + /// so the VK_GOOGLE_display_timing extension is present. + next_present_time: Option<vk::PresentTimeGOOGLE>, +} + +impl Drop for NativeSwapchain { + fn drop(&mut self) { + unsafe { + self.functor.destroy_swapchain(self.raw, None); + } + } +} + +impl Swapchain for NativeSwapchain { + unsafe fn release_resources(&mut self, device: &crate::vulkan::Device) { + profiling::scope!("Swapchain::release_resources"); + { + profiling::scope!("vkDeviceWaitIdle"); + // We need to also wait until all presentation work is done. Because there is no way to portably wait until + // the presentation work is done, we are forced to wait until the device is idle. + let _ = unsafe { + device + .shared + .raw + .device_wait_idle() + .map_err(map_host_device_oom_and_lost_err) + }; + }; + + if let Some(fence) = self.fence { + unsafe { device.shared.raw.destroy_fence(fence, None) } + } + + // We cannot take this by value, as the function returns `self`. + for semaphore in self.acquire_semaphores.drain(..) { + let arc_removed = Arc::into_inner(semaphore).expect( + "Trying to destroy a SwapchainAcquireSemaphore that is still in use by a SurfaceTexture", + ); + let mutex_removed = arc_removed.into_inner(); + + unsafe { mutex_removed.destroy(&device.shared.raw) }; + } + + for semaphore in self.present_semaphores.drain(..) { + let arc_removed = Arc::into_inner(semaphore).expect( + "Trying to destroy a SwapchainPresentSemaphores that is still in use by a SurfaceTexture", + ); + let mutex_removed = arc_removed.into_inner(); + + unsafe { mutex_removed.destroy(&device.shared.raw) }; + } + } + + unsafe fn acquire( + &mut self, + timeout: Option<core::time::Duration>, + fence: &crate::vulkan::Fence, + ) -> Result<crate::AcquiredSurfaceTexture<crate::api::Vulkan>, crate::SurfaceError> { + let mut timeout_ns = match timeout { + Some(duration) => duration.as_nanos() as u64, + None => u64::MAX, + }; + + // AcquireNextImageKHR on Android (prior to Android 11) doesn't support timeouts + // and will also log verbose warnings if tying to use a timeout. + // + // Android 10 implementation for reference: + // https://android.googlesource.com/platform/frameworks/native/+/refs/tags/android-mainline-10.0.0_r13/vulkan/libvulkan/swapchain.cpp#1426 + // Android 11 implementation for reference: + // https://android.googlesource.com/platform/frameworks/native/+/refs/tags/android-mainline-11.0.0_r45/vulkan/libvulkan/swapchain.cpp#1438 + // + // Android 11 corresponds to an SDK_INT/ro.build.version.sdk of 30 + if cfg!(target_os = "android") && self.device.instance.android_sdk_version < 30 { + timeout_ns = u64::MAX; + } + + let acquire_semaphore_arc = self.get_acquire_semaphore(); + // Nothing should be using this, so we don't block, but panic if we fail to lock. + let acquire_semaphore_guard = acquire_semaphore_arc + .try_lock() + .expect("Failed to lock a SwapchainSemaphores."); + + // Wait for all commands writing to the previously acquired image to + // complete. + // + // Almost all the steps in the usual acquire-draw-present flow are + // asynchronous: they get something started on the presentation engine + // or the GPU, but on the CPU, control returns immediately. Without some + // sort of intervention, the CPU could crank out frames much faster than + // the presentation engine can display them. + // + // This is the intervention: if any submissions drew on this image, and + // thus waited for `locked_swapchain_semaphores.acquire`, wait for all + // of them to finish, thus ensuring that it's okay to pass `acquire` to + // `vkAcquireNextImageKHR` again. + let completed = self.device.wait_for_fence( + fence, + acquire_semaphore_guard.previously_used_submission_index, + timeout_ns, + )?; + if !completed { + return Err(crate::SurfaceError::Timeout); + } + + let acquire_fence = self.fence.unwrap_or_else(vk::Fence::null); + + // will block if no image is available + let (index, suboptimal) = match unsafe { + profiling::scope!("vkAcquireNextImageKHR"); + self.functor.acquire_next_image( + self.raw, + timeout_ns, + acquire_semaphore_guard.acquire, + acquire_fence, + ) + } { + // We treat `VK_SUBOPTIMAL_KHR` as `VK_SUCCESS` on Android. + // See the comment in `Queue::present`. + #[cfg(target_os = "android")] + Ok((index, _)) => (index, false), + #[cfg(not(target_os = "android"))] + Ok(pair) => pair, + Err(error) => { + return match error { + vk::Result::TIMEOUT => Err(crate::SurfaceError::Timeout), + vk::Result::NOT_READY | vk::Result::ERROR_OUT_OF_DATE_KHR => { + Err(crate::SurfaceError::Outdated) + } + vk::Result::ERROR_SURFACE_LOST_KHR => Err(crate::SurfaceError::Lost), + // We don't use VK_EXT_full_screen_exclusive + // VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT + other => Err(map_host_device_oom_and_lost_err(other).into()), + }; + } + }; + + if let Some(fence) = self.fence { + unsafe { + // The `wait_all` argument must be `true` to avoid crash on some Android devices. See https://github.com/gfx-rs/wgpu/pull/8769 + self.device + .raw + .wait_for_fences(&[fence], true, timeout_ns) + .map_err(map_host_device_oom_and_lost_err)?; + + self.device + .raw + .reset_fences(&[fence]) + .map_err(map_host_device_oom_and_lost_err)?; + } + } + + drop(acquire_semaphore_guard); + // We only advance the surface semaphores if we successfully acquired an image, otherwise + // we should try to re-acquire using the same semaphores. + self.advance_acquire_semaphore(); + + let present_semaphore_arc = self.get_present_semaphores(index); + + // special case for Intel Vulkan returning bizarre values (ugh) + if self.device.vendor_id == crate::auxil::db::intel::VENDOR && index > 0x100 { + return Err(crate::SurfaceError::Outdated); + } + + let identity = self.device.texture_identity_factory.next(); + + let texture = crate::vulkan::SurfaceTexture { + index, + texture: crate::vulkan::Texture { + raw: self.images[index as usize], + drop_guard: None, + memory: crate::vulkan::TextureMemory::External, + format: self.config.format, + copy_size: crate::CopyExtent { + width: self.config.extent.width, + height: self.config.extent.height, + depth: 1, + }, + identity, + }, + metadata: Box::new(NativeSurfaceTextureMetadata { + acquire_semaphores: acquire_semaphore_arc, + present_semaphores: present_semaphore_arc, + }), + }; + Ok(crate::AcquiredSurfaceTexture { + texture, + suboptimal, + }) + } + + unsafe fn discard_texture( + &mut self, + _texture: crate::vulkan::SurfaceTexture, + ) -> Result<(), crate::SurfaceError> { + // TODO: Current implementation no-ops + Ok(()) + } + + unsafe fn present( + &mut self, + queue: &crate::vulkan::Queue, + texture: crate::vulkan::SurfaceTexture, + ) -> Result<(), crate::SurfaceError> { + let metadata = texture + .metadata + .as_any() + .downcast_ref::<NativeSurfaceTextureMetadata>() + .unwrap(); + let mut acquire_semaphore = metadata.acquire_semaphores.lock(); + let mut present_semaphores = metadata.present_semaphores.lock(); + + let wait_semaphores = present_semaphores.get_present_wait_semaphores(); + + // Reset the acquire and present semaphores internal state + // to be ready for the next frame. + // + // We do this before the actual call to present to ensure that + // even if this method errors and early outs, we have reset + // the state for next frame. + acquire_semaphore.end_semaphore_usage(); + present_semaphores.end_semaphore_usage(); + + drop(acquire_semaphore); + + let swapchains = [self.raw]; + let image_indices = [texture.index]; + let vk_info = vk::PresentInfoKHR::default() + .swapchains(&swapchains) + .image_indices(&image_indices) + .wait_semaphores(&wait_semaphores); + + let mut display_timing; + let present_times; + let vk_info = if let Some(present_time) = self.next_present_time.take() { + debug_assert!( + self.device + .features + .contains(wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING), + "`next_present_time` should only be set if `VULKAN_GOOGLE_DISPLAY_TIMING` is enabled" + ); + present_times = [present_time]; + display_timing = vk::PresentTimesInfoGOOGLE::default().times(&present_times); + // SAFETY: We know that VK_GOOGLE_display_timing is present because of the safety contract on `next_present_time`. + vk_info.push_next(&mut display_timing) + } else { + vk_info + }; + + let suboptimal = { + profiling::scope!("vkQueuePresentKHR"); + unsafe { self.functor.queue_present(queue.raw, &vk_info) }.map_err(|error| { + match error { + vk::Result::ERROR_OUT_OF_DATE_KHR => crate::SurfaceError::Outdated, + vk::Result::ERROR_SURFACE_LOST_KHR => crate::SurfaceError::Lost, + // We don't use VK_EXT_full_screen_exclusive + // VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT + _ => map_host_device_oom_and_lost_err(error).into(), + } + })? + }; + if suboptimal { + // We treat `VK_SUBOPTIMAL_KHR` as `VK_SUCCESS` on Android. + // On Android 10+, libvulkan's `vkQueuePresentKHR` implementation returns `VK_SUBOPTIMAL_KHR` if not doing pre-rotation + // (i.e `VkSwapchainCreateInfoKHR::preTransform` not being equal to the current device orientation). + // This is always the case when the device orientation is anything other than the identity one, as we unconditionally use `VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR`. + #[cfg(not(target_os = "android"))] + log::debug!("Suboptimal present of frame {}", texture.index); + } + Ok(()) + } + + fn as_any(&self) -> &dyn Any { + self + } + + fn as_any_mut(&mut self) -> &mut dyn Any { + self + } +} + +impl NativeSwapchain { + pub(crate) fn as_raw(&self) -> vk::SwapchainKHR { + self.raw + } + + pub fn set_next_present_time(&mut self, present_timing: vk::PresentTimeGOOGLE) { + let features = wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING; + if self.device.features.contains(features) { + self.next_present_time = Some(present_timing); + } else { + // Ideally we'd use something like `device.required_features` here, but that's in `wgpu-core`, which we are a dependency of + panic!( + concat!( + "Tried to set display timing properties ", + "without the corresponding feature ({:?}) enabled." + ), + features + ); + } + } + + /// Mark the current frame finished, advancing to the next acquire semaphore. + fn advance_acquire_semaphore(&mut self) { + let semaphore_count = self.acquire_semaphores.len(); + self.next_acquire_index = (self.next_acquire_index + 1) % semaphore_count; + } + + /// Get the next acquire semaphore that should be used with this swapchain. + fn get_acquire_semaphore(&self) -> Arc<Mutex<SwapchainAcquireSemaphore>> { + self.acquire_semaphores[self.next_acquire_index].clone() + } + + /// Get the set of present semaphores that should be used with the given image index. + fn get_present_semaphores(&self, index: u32) -> Arc<Mutex<SwapchainPresentSemaphores>> { + self.present_semaphores[index as usize].clone() + } +} + +/// Semaphore used to acquire a swapchain image. +#[derive(Debug)] +struct SwapchainAcquireSemaphore { + /// A semaphore that is signaled when this image is safe for us to modify. + /// + /// When [`vkAcquireNextImageKHR`] returns the index of the next swapchain + /// image that we should use, that image may actually still be in use by the + /// presentation engine, and is not yet safe to modify. However, that + /// function does accept a semaphore that it will signal when the image is + /// indeed safe to begin messing with. + /// + /// This semaphore is: + /// + /// - waited for by the first queue submission to operate on this image + /// since it was acquired, and + /// + /// - signaled by [`vkAcquireNextImageKHR`] when the acquired image is ready + /// for us to use. + /// + /// [`vkAcquireNextImageKHR`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkAcquireNextImageKHR + acquire: vk::Semaphore, + + /// True if the next command submission operating on this image should wait + /// for [`acquire`]. + /// + /// We must wait for `acquire` before drawing to this swapchain image, but + /// because `wgpu-hal` queue submissions are always strongly ordered, only + /// the first submission that works with a swapchain image actually needs to + /// wait. We set this flag when this image is acquired, and clear it the + /// first time it's passed to [`Queue::submit`] as a surface texture. + /// + /// Additionally, semaphores can only be waited on once, so we need to ensure + /// that we only actually pass this semaphore to the first submission that + /// uses that image. + /// + /// [`acquire`]: SwapchainAcquireSemaphore::acquire + /// [`Queue::submit`]: crate::Queue::submit + should_wait_for_acquire: bool, + + /// The fence value of the last command submission that wrote to this image. + /// + /// The next time we try to acquire this image, we'll block until + /// this submission finishes, proving that [`acquire`] is ready to + /// pass to `vkAcquireNextImageKHR` again. + /// + /// [`acquire`]: SwapchainAcquireSemaphore::acquire + previously_used_submission_index: crate::FenceValue, +} + +impl SwapchainAcquireSemaphore { + fn new(device: &DeviceShared, index: usize) -> Result<Self, crate::DeviceError> { + Ok(Self { + acquire: device + .new_binary_semaphore(&format!("SwapchainImageSemaphore: Index {index} acquire"))?, + should_wait_for_acquire: true, + previously_used_submission_index: 0, + }) + } + + /// Sets the fence value which the next acquire will wait for. This prevents + /// the semaphore from being used while the previous submission is still in flight. + fn set_used_fence_value(&mut self, value: crate::FenceValue) { + self.previously_used_submission_index = value; + } + + /// Return the semaphore that commands drawing to this image should wait for, if any. + /// + /// This only returns `Some` once per acquisition; see + /// [`SwapchainAcquireSemaphore::should_wait_for_acquire`] for details. + fn get_acquire_wait_semaphore(&mut self) -> Option<vk::Semaphore> { + if self.should_wait_for_acquire { + self.should_wait_for_acquire = false; + Some(self.acquire) + } else { + None + } + } + + /// Indicates the cpu-side usage of this semaphore has finished for the frame, + /// so reset internal state to be ready for the next frame. + fn end_semaphore_usage(&mut self) { + // Reset the acquire semaphore, so that the next time we acquire this + // image, we can wait for it again. + self.should_wait_for_acquire = true; + } + + unsafe fn destroy(&self, device: &ash::Device) { + unsafe { + device.destroy_semaphore(self.acquire, None); + } + } +} + +#[derive(Debug)] +struct SwapchainPresentSemaphores { + /// A pool of semaphores for ordering presentation after drawing. + /// + /// The first [`present_index`] semaphores in this vector are: + /// + /// - all waited on by the call to [`vkQueuePresentKHR`] that presents this + /// image, and + /// + /// - each signaled by some [`vkQueueSubmit`] queue submission that draws to + /// this image, when the submission finishes execution. + /// + /// This vector accumulates one semaphore per submission that writes to this + /// image. This is awkward, but hard to avoid: [`vkQueuePresentKHR`] + /// requires a semaphore to order it with respect to drawing commands, and + /// we can't attach new completion semaphores to a command submission after + /// it's been submitted. This means that, at submission time, we must create + /// the semaphore we might need if the caller's next action is to enqueue a + /// presentation of this image. + /// + /// An alternative strategy would be for presentation to enqueue an empty + /// submit, ordered relative to other submits in the usual way, and + /// signaling a single presentation semaphore. But we suspect that submits + /// are usually expensive enough, and semaphores usually cheap enough, that + /// performance-sensitive users will avoid making many submits, so that the + /// cost of accumulated semaphores will usually be less than the cost of an + /// additional submit. + /// + /// Only the first [`present_index`] semaphores in the vector are actually + /// going to be signalled by submitted commands, and need to be waited for + /// by the next present call. Any semaphores beyond that index were created + /// for prior presents and are simply being retained for recycling. + /// + /// [`present_index`]: SwapchainPresentSemaphores::present_index + /// [`vkQueuePresentKHR`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkQueuePresentKHR + /// [`vkQueueSubmit`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkQueueSubmit + present: Vec<vk::Semaphore>, + + /// The number of semaphores in [`present`] to be signalled for this submission. + /// + /// [`present`]: SwapchainPresentSemaphores::present + present_index: usize, + + /// Which image this semaphore set is used for. + frame_index: usize, +} + +impl SwapchainPresentSemaphores { + pub fn new(frame_index: usize) -> Self { + Self { + present: Vec::new(), + present_index: 0, + frame_index, + } + } + + /// Return the semaphore that the next submission that writes to this image should + /// signal when it's done. + /// + /// See [`SwapchainPresentSemaphores::present`] for details. + fn get_submit_signal_semaphore( + &mut self, + device: &DeviceShared, + ) -> Result<vk::Semaphore, crate::DeviceError> { + // Try to recycle a semaphore we created for a previous presentation. + let sem = match self.present.get(self.present_index) { + Some(sem) => *sem, + None => { + let sem = device.new_binary_semaphore(&format!( + "SwapchainImageSemaphore: Image {} present semaphore {}", + self.frame_index, self.present_index + ))?; + self.present.push(sem); + sem + } + }; + + self.present_index += 1; + + Ok(sem) + } + + /// Indicates the cpu-side usage of this semaphore has finished for the frame, + /// so reset internal state to be ready for the next frame. + fn end_semaphore_usage(&mut self) { + // Reset the index to 0, so that the next time we get a semaphore, we + // start from the beginning of the list. + self.present_index = 0; + } + + /// Return the semaphores that a presentation of this image should wait on. + /// + /// Return a slice of semaphores that the call to [`vkQueueSubmit`] that + /// ends this image's acquisition should wait for. See + /// [`SwapchainPresentSemaphores::present`] for details. + /// + /// Reset `self` to be ready for the next acquisition cycle. + /// + /// [`vkQueueSubmit`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkQueueSubmit + fn get_present_wait_semaphores(&mut self) -> Vec<vk::Semaphore> { + self.present[0..self.present_index].to_vec() + } + + unsafe fn destroy(&self, device: &ash::Device) { + unsafe { + for sem in &self.present { + device.destroy_semaphore(*sem, None); + } + } + } +} + +#[derive(Debug)] +struct NativeSurfaceTextureMetadata { + acquire_semaphores: Arc<Mutex<SwapchainAcquireSemaphore>>, + present_semaphores: Arc<Mutex<SwapchainPresentSemaphores>>, +} + +impl SurfaceTextureMetadata for NativeSurfaceTextureMetadata { + fn get_semaphore_guard(&self) -> Box<dyn SwapchainSubmissionSemaphoreGuard + '_> { + Box::new(NativeSwapchainSubmissionSemaphoreGuard { + acquire_semaphore_guard: self + .acquire_semaphores + .try_lock() + .expect("Failed to lock surface acquire semaphore"), + present_semaphores_guard: self + .present_semaphores + .try_lock() + .expect("Failed to lock surface present semaphores"), + }) + } + + fn as_any(&self) -> &dyn Any { + self + } +} + +struct NativeSwapchainSubmissionSemaphoreGuard<'a> { + acquire_semaphore_guard: MutexGuard<'a, SwapchainAcquireSemaphore>, + present_semaphores_guard: MutexGuard<'a, SwapchainPresentSemaphores>, +} + +impl<'a> SwapchainSubmissionSemaphoreGuard for NativeSwapchainSubmissionSemaphoreGuard<'a> { + fn set_used_fence_value(&mut self, value: u64) { + self.acquire_semaphore_guard.set_used_fence_value(value); + } + + fn get_acquire_wait_semaphore(&mut self) -> Option<SemaphoreType> { + self.acquire_semaphore_guard + .get_acquire_wait_semaphore() + .map(SemaphoreType::Binary) + } + + fn get_submit_signal_semaphore( + &mut self, + device: &DeviceShared, + ) -> Result<SemaphoreType, crate::DeviceError> { + self.present_semaphores_guard + .get_submit_signal_semaphore(device) + .map(SemaphoreType::Binary) + } +} diff --git a/tools/agent_control_e2e.py b/tools/agent_control_e2e.py index ab408b2b2..53296669a 100644 --- a/tools/agent_control_e2e.py +++ b/tools/agent_control_e2e.py @@ -7,7 +7,8 @@ runs through the CLI (one horizon-ctl process per command) and through `horizon- small JSON-RPC client, and saves a screenshot of each run. The ui scenario drives the menus by widget id instead: the title screen, the options (a slider set by id) and the inventory's slots; the options scenario reads and sets options.txt values by key; the events scenario waits for -states (wait-for) and reads the event log. +states (wait-for) and reads the event log; the consume scenario eats and drinks consumables (a +golden apple, milk, honey, chorus fruit) and checks their effects, remainders and teleport. python3 tools/agent_control_e2e.py # both modes python3 tools/agent_control_e2e.py --mode mcp # one of them @@ -378,7 +379,52 @@ def events_scenario(d, out): return {'events': len(events)} -SCENARIOS = {'survival': scenario, 'ui': ui_scenario, 'options': options_scenario, 'events': events_scenario} +def consume_scenario(d, out): + # Consumables finish as vanilla's Consumable.onConsume: always-edible food at full hunger with its + # effects, milk and honey (no food or drink sounds) with their effects and use remainders, and + # chorus fruit's random teleport. + d('click', id='singleplayer') + if 'game' not in widgets(d): + d('click', id='create-new-world') + d('type', field='name', replace=True, text='Consume') + d('click', id='create-new-world') + d('wait_for', condition='loaded', timeout=400) + + def consume(item, seconds): + d('command', command='clear @s') + d('give', item=item, count=1) + d('use_item', hold=seconds) + d('wait', ticks=5) + state = d('state') + return state, {e['id'] for e in state['player']['effects']} + + def held(state): + return state['held'] and state['held']['item'] + + state, effects = consume('golden_apple', 2.2) + assert state['player']['food'] == 20 and {'regeneration', 'absorption'} <= effects, effects + assert held(state) is None, state['held'] + d('command', command='effect give @s minecraft:speed 60') + state, effects = consume('milk_bucket', 2.2) + assert effects == set(), effects + assert held(state) == 'minecraft:bucket', state['held'] + d('command', command='effect give @s minecraft:poison 60') + d('command', command='effect give @s minecraft:speed 60') + state, effects = consume('honey_bottle', 2.6) + assert effects == {'speed'}, effects + assert held(state) == 'minecraft:glass_bottle', state['held'] + before = d('state')['player']['position'] + state, _ = consume('chorus_fruit', 2.2) + after = state['player']['position'] + moved = [b - a for a, b in zip(before, after)] + assert held(state) is None, state['held'] + assert moved != [0, 0, 0] and abs(moved[0]) <= 8 and abs(moved[2]) <= 8, moved + d('screenshot', path=str(out / f'consume-{d.name}.png')) + return {'teleported': [round(m, 2) for m in moved]} + + +SCENARIOS = {'survival': scenario, 'ui': ui_scenario, 'options': options_scenario, 'events': events_scenario, + 'consume': consume_scenario} def run(mode, ctl, out, resources, which='survival'): diff --git a/tools/build_release.py b/tools/build_release.py index 00ded705b..46f952131 100644 --- a/tools/build_release.py +++ b/tools/build_release.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 -"""Create downloadable client/server starters and hashes from original project artifacts.""" +"""Create the downloadable client starter and hashes from original project artifacts. (The Java/Fabric server +starter is gone: horizon-server replaces it, docs/hosting.md.)""" import hashlib from pathlib import Path @@ -27,15 +28,6 @@ def main(): client["Compare-Benchmarks.html"] = viewer (ROOT / "dist/Compare-Benchmarks.html").write_bytes(viewer) build_pack.write_zip(ROOT / "dist" / f"horizon-mac-starter-{version}.zip", client) - server = {"LICENSE": client["LICENSE"], "dist/" + mod.name: mod.read_bytes(), - "Compare-Benchmarks.html": viewer, - "START-HERE.txt": (ROOT / "docs/server-quickstart.txt").read_text().replace("@PACK_VERSION@", version).encode(), - "research-lock.json": (ROOT / "research-lock.json").read_bytes()} - for name in ["lab.py", "runtime.py", "server.py", "compare_benchmarks.py"]: - server["tools/" + name] = (ROOT / "tools" / name).read_bytes() - for name in ["mods.lock.json", "fabric.lock.json"]: - server["packs/" + name] = (ROOT / "packs" / name).read_bytes() - build_pack.write_zip(ROOT / "dist" / f"horizon-server-starter-{version}.zip", server) artifacts = sorted(path for path in (ROOT / "dist").glob("*") if version in path.name) artifacts.append(ROOT / "dist/Compare-Benchmarks.html") checksums = [] @@ -43,7 +35,7 @@ def main(): if path.suffix in [".jar", ".mrpack", ".zip", ".html"]: checksums.append(hashlib.sha256(path.read_bytes()).hexdigest() + " " + path.name) (ROOT / "dist/SHA256SUMS.txt").write_text("\n".join(checksums) + "\n") - print("Built client/server starters and SHA256SUMS.txt") + print("Built the client starter and SHA256SUMS.txt") if __name__ == "__main__": diff --git a/tools/check_imports.py b/tools/check_imports.py index 8cdabe45b..85afb541d 100644 --- a/tools/check_imports.py +++ b/tools/check_imports.py @@ -5,7 +5,8 @@ Windows .exe: every DLL in the import and delay-import tables must ship with Windows (no VCRUNTIME140.dll, vulkan-1.dll, ...): Vulkan and the like are loaded at run time, so a missing driver is an error message, not a dialog. Linux ELF: the loader must be the standard one (not a /nix/store path) and the -needed libraries the ones every desktop distribution has. +needed libraries the ones every desktop distribution has. `--server`: a server +build, which may not need ALSA or udev either (minimal server images lack them). """ import argparse @@ -30,6 +31,8 @@ LINUX_SYSTEM = { "libc.so.6", "libm.so.6", "libgcc_s.so.1", "libdl.so.2", "libpthread.so.0", "librt.so.1", "ld-linux-x86-64.so.2", "libasound.so.2", "libudev.so.1", } +# The game's sound (cpal) and controllers (gilrs): a desktop has them, a server image may not. +LINUX_DESKTOP_ONLY = {"libasound.so.2", "libudev.so.1"} def windows_allowed(name): @@ -111,7 +114,7 @@ def elf_needs(data): return interpreter, needed, runpath -def check(path): +def check(path, server=False): data = Path(path).read_bytes() problems = [] if data[:2] == b"MZ": @@ -123,6 +126,8 @@ def check(path): if interpreter not in (None, LINUX_LOADER): problems.append(f"loader is {interpreter}, not {LINUX_LOADER}") problems += [f"needs {n}, which not every distribution has" for n in needed if n not in LINUX_SYSTEM] + if server: + problems += [f"needs {n}, which a server may not have" for n in needed if n in LINUX_DESKTOP_ONLY] problems += [f"has a run path to {p}" for p in runpath if "/nix/store" in p] summary = f"{interpreter}; " + ", ".join(needed) print(f"{path}: {summary}") @@ -134,8 +139,10 @@ def check(path): def main(): parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("binaries", nargs="+") + parser.add_argument("--server", action="store_true", + help="a server build: not even ALSA or udev (the game's sound and controllers)") args = parser.parse_args() - ok = all([check(path) for path in args.binaries]) + ok = all([check(path, args.server) for path in args.binaries]) sys.exit(0 if ok else 1) diff --git a/tools/command_exec_corpus.py b/tools/command_exec_corpus.py index eb99e1f1a..166ecaf70 100644 --- a/tools/command_exec_corpus.py +++ b/tools/command_exec_corpus.py @@ -2,7 +2,8 @@ Each group is a list of operations: `cmd` runs a command as the player and records its messages and results (`record: false` runs setup silently), -`tick` advances the game and `settle` loads the terrain around the player. +`tick` advances the game, `settle` loads the terrain and entities around the +player and `portal` sends the player through the nether portal it stands in. Everything happens high above the terrain (y 200 and up, where both worlds are air) after the world's entities are removed, so the results do not depend on world generation. @@ -391,6 +392,49 @@ def execute(): *setup('/kill @e[type=minecraft:armor_stand]', '/setblock 4 210 4 minecraft:air', '/setblock 5 210 5 minecraft:air', '/scoreboard objectives remove exec', '/tp @s 8.5 230 8.5 0 0'), {'op': 'tick', 'n': 25}, + *setup('/gamemode spectator'), + cmd('/execute in minecraft:the_nether if dimension minecraft:the_nether run say source dimension'), + cmd('/execute in minecraft:the_nether at @s if dimension minecraft:overworld run say entity dimension'), + cmd('/execute in minecraft:the_nether positioned as @s if dimension minecraft:the_nether run say position only'), + cmd('/execute in minecraft:the_nether if loaded 0 100 0'), + cmd('/execute in minecraft:the_nether run tp @s 0 100 0'), + {'op': 'settle'}, + cmd('/execute if dimension minecraft:the_nether run say dimension switched'), + cmd('/data get entity @s Pos'), + cmd('/execute in minecraft:the_nether positioned 80 100 -40 in minecraft:overworld run tp @s ~ ~ ~'), + {'op': 'settle'}, + cmd('/execute if dimension minecraft:overworld run say relative dimension'), + cmd('/data get entity @s Pos'), + cmd('/execute in minecraft:overworld positioned 80 100 -40 in minecraft:the_nether run tp @s ~ ~ ~ 45 30'), + {'op': 'settle'}, + cmd('/data get entity @s Pos'), + cmd('/data get entity @s Rotation'), + cmd('/execute in minecraft:overworld run tp @s 8.5 230 8.5'), + {'op': 'settle'}, + cmd('/execute if dimension minecraft:overworld run say dimension restored'), + cmd('/data get entity @s Pos'), + *setup('/gamemode creative'), + ] + + +def levels(): + """A nether portal round trip: on the way back the Overworld's entities (the + pig left by `entities`) are found and a summon names what it summoned.""" + return [ + *setup('/fill 2 231 12 5 236 12 minecraft:obsidian', '/fill 3 232 12 4 235 12 minecraft:nether_portal[axis=x]', + '/tp @s 3.5 232 12.5 0 0'), + {'op': 'portal'}, + {'op': 'settle'}, + cmd('/execute if dimension minecraft:the_nether run say through the portal'), + {'op': 'portal'}, + {'op': 'settle'}, + cmd('/execute if dimension minecraft:overworld run say back through the portal'), + cmd('/data get entity @s Pos'), + cmd('/data get entity @s Rotation'), + cmd('/execute if entity @e[type=minecraft:pig]'), + cmd('/summon minecraft:armor_stand 4.5 210 4.5 {NoGravity:1b,Tags:["lv"],CustomName:"Back"}'), + *setup('/kill @e[tag=lv]', '/fill 2 231 12 5 236 12 minecraft:air', '/tp @s 8.5 230 8.5 0 0'), + {'op': 'settle'}, ] @@ -918,6 +962,7 @@ def groups(): ('entities', entities()), ('scores, teams and bossbars', scores()), ('execute', execute()), + ('levels', levels()), ('items and loot', items_and_loot()), ('world', world_misc()), ('progression', progression()), diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index 667fdda2d..723ef342c 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -10,16 +10,71 @@ address. The script then checks, from both sides, what a guest's game used to ge - armour: each side draws the other player in that player's own armour; - nametags: each side draws the other player's name above them; - chest: a chest the guest opens opens on the guest's screen, its lid opens on both sides and - closes on both when the guest closes it; -- crafting: the guest crafts planks in the inventory's 2x2 grid and opens a crafting table; + closes on both when the guest closes it; the host keeps playing with its mouse look (#687); +- crafting: the guest crafts planks in the inventory's 2x2 grid and opens a crafting table, which + leaves the host's screen and mouse look alone; +- sign: a sign the guest places doesn't open a sign editor on the host; - damage: a guest in survival takes fall damage; - mobs: with the host in creative, a zombie and a skeleton go for the guest and hurt them; - hazards: a guest in survival gets hungry sprint-jumping and is hurt by lava, a block at the eyes, drowning and the void (then respawns); -- pvp: the guest and the host hit each other with a sword; with the pvp game rule off, nothing; +- pvp: the guest and the host hit each other with a sword; with the pvp game rule off, or on one + team without friendly fire, nothing; +- respawn: a player killed is drawn dead by the other side, and alive again once it respawns (the + respawned player is a new entity to the others); the host dies, and on the server thread + the guest too; +- hurt: a pig the guest hits, and one the host hits, flashes red on the other side too (the + server's damage event) (#848); - containers: the guest's ender chest holds the guest's items, not the host's, and a double chest opens as 9x6 with the right half first; -- feedback: the guest's command feedback names the guest and shows the numbers. +- machines: a hopper, a dispenser and a dropper open on the guest's screen with their 5 and 9 + slots, and keep what the guest shift-clicks into them (#848); +- brewing: a brewing stand opens on the guest's screen; blaze powder, nether wart and a bottle the + guest shift-clicks go to the fuel, ingredient and bottle slots, and the stand's fuel shows + (#848); +- feedback: the guest's command feedback names the guest and shows the numbers; +- chat: a line the host sends reaches the guest's event log (`wait-for chat`), and on the + server thread the host window's too; +- effects: a beacon's power reaches every player in range, the host and the guest; +- distance: (--server-thread) the server follows the host's render distance, past vanilla's 32 too + (IntegratedServer.tickServer), and the guest's chunks follow the server's (#804) +- portals: (--server-thread) the server holds a level per dimension: the guest goes to the Nether + through a portal while the host stays, then the host goes to the End while the guest is in + the Nether, and both come back. +- commands: the guest's chat completes commands and their arguments over the server's command + tree (Tab), and runs what it completed; +- recipes: the guest's recipe book lists the planks recipe the logs unlock, and clicking it + places a log in the inventory's crafting grid (the server places it). +- feedback: the guest's command feedback names the guest and shows the numbers, and the host (an + operator) is told of it as `[Guest: ...]`. +- clicks: with real mouse presses (not click-slot, which skips the screen's press and drag + handling), the guest puts a stack into a chest and a furnace, drags planks across + its crafting grid and gathers a stack with a double click; the server has each. +- sleep: at night the guest lies down in its own bed (not the host); alone it stays night, it + gets up with Escape, and once the host sleeps too the night ends and both wake. +- push: a cow standing in the guest pushes it just clear of the cow (the guest's own client + pushes it, as in vanilla), not ever faster across the ground. +- eat: holding right click with bread, the guest's own screen plays the eating (the item in use + counts up) and the server eats one loaf and feeds the guest. +- eating: each side sees the other player eat: the item in use (LivingEntity's flags) on the + player it draws, and no longer once the bread is eaten (#803); +- xp: experience orbs 4 blocks away fly to the guest, show on the guest's screen and give the + guest their points (not the host). +- objects: boats, minecarts, armor stands, item frames and paintings the host summons reach the + guest as the host has them (kind, place, rotation, what they hold or wear, the variant). +- projectiles: primed TNT (one falling), an end crystal and an arrow that lands reach the guest where + the host has them; a falling block lands on both sides. +- plates: a stone pressure plate the guest steps on is pressed on both sides, and comes up once + the guest steps off (#848). +- swings: each side sees the other's arm swing while it mines a block, and the guest sees its own + swing when its hoe tills dirt (the server's use) (#848). +- time: the guest's clock is the host's: it stops with `/time pause`, and catches up with the + server's game time after the guest's game stalls for a few seconds (#848). +- growth: far from the host, the chunks around the guest tick (#890): water placed there flows + and wheat on farmland grows (random ticks), however the host runs its world. +- light: the guest's light matches the host's: around a torch placed near it, under a roof and + once a block of the roof is removed under the sky, and in the last chunk it tracks next to + glowstone in a chunk it doesn't (only the server's light updates bring that one). Each check is reported on its own (a failing one doesn't stop the others) and the screenshots of both sides go to --out (host-*.png, guest-*.png). @@ -27,6 +82,7 @@ both sides go to --out (host-*.png, guest-*.png). python3 tools/lan_e2e.py # build with the fast profile, run every check python3 tools/lan_e2e.py --bin-dir target/fast # prebuilt binaries python3 tools/lan_e2e.py --check chest --check tab + python3 tools/lan_e2e.py --server-thread --check portals # the host's world on its server thread Needs the prepared resources in .local/horizon-client and, on Linux, vscreen (the desktop); on macOS both games render offscreen. Games started here have no audio (HORIZON_NO_AUDIO=1). @@ -36,9 +92,11 @@ import json import os import re import shutil +import signal import subprocess import sys import tempfile +import threading import time import zlib from pathlib import Path @@ -48,7 +106,9 @@ from agent_control_e2e import Cli, Failed, game_env # noqa: E402 ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'trials'] +# The host plays single player on its server thread (--server-thread). +SERVER_THREAD = False +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'hurt', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'trials', 'clicks', 'sleep', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'light', 'time', 'swings', 'plates', 'effects', 'portals', 'distance', 'growth'] class Game: @@ -161,7 +221,7 @@ def check_armour(host, guest, out): face_each_other(host, guest) host.shot(out, 'armour') guest.shot(out, 'armour') - seen = host.raw('lan', 'status')['guests'][0].get('model_armor') + seen = lan_guest(host).get('model_armor') assert seen == [None, 'minecraft:diamond_chestplate', 'minecraft:diamond_leggings', None], \ f'the host draws the guest in {seen}' host_gear = [e for e in remote(guest).get('player_equipment', [])] @@ -180,6 +240,19 @@ def check_nametags(host, guest, out): return {'host': h, 'guest': g} +def grab(game): + """Mouse look on, as a player's grabbed pointer: an agent's mouse-move stands in for it.""" + game.raw('mouse-move', 1, 0) + game.raw('mouse-move', -1, 0) + + +def host_unaffected(host, what): + """What a guest opens is the guest's: the host keeps its world on screen and its mouse look (#687).""" + state = host('state') + assert state.get('screen') == 'playing', f'{what} opened the {state.get("screen")} screen on the host' + assert state.get('pointer_grabbed'), f"{what} released the host's pointer" + + def lid(game, pos): return game.raw('block', *pos).get('lid', 0.0) @@ -189,9 +262,11 @@ def check_chest(host, guest, out): chest = (x, y, z + 2) guest('command', command=f'setblock {chest[0]} {chest[1]} {chest[2]} minecraft:chest') guest.wait(10) + grab(host) guest.raw('interact', *chest) screen = guest.raw('wait-for', 'screen', 'container', '--timeout', 10) guest.wait(20) + host_unaffected(host, "the guest's chest") opened = {'guest': lid(guest, chest), 'host': lid(host, chest)} host.raw('look-at', chest[0] + 0.5, chest[1] + 0.5, chest[2] + 0.5) host.wait(2) @@ -231,8 +306,10 @@ def check_crafting(host, guest, out): table = (x, y, z - 2) guest('command', command=f'setblock {table[0]} {table[1]} {table[2]} minecraft:crafting_table') guest.wait(10) + grab(host) guest.raw('interact', *table) guest.raw('wait-for', 'screen', 'inventory', '--timeout', 10) + host_unaffected(host, "the guest's crafting table") size = guest('screen').get('craft_size') assert size == 3, f'the crafting table opened a {size}x{size} grid' # Two planks one above the other in the 3x3 grid: four sticks. @@ -252,6 +329,131 @@ def check_crafting(host, guest, out): return {'planks': planks, 'table': counts} +def check_sign(host, guest, out): + x, y, z = (int(v // 1) for v in pos_of(guest)) + spot = (x + 2, y, z) + guest('command', command=f'setblock {spot[0]} {spot[1] - 1} {spot[2]} minecraft:stone') + guest('command', command=f'setblock {spot[0]} {spot[1]} {spot[2]} minecraft:air') + guest('command', command='give @s minecraft:oak_sign 1') + guest.wait(10) + grab(host) + guest.raw('place', *spot, '--item', 'minecraft:oak_sign') + guest.wait(20) + placed = host.raw('block', *spot).get('block') + host.shot(out, 'sign') + assert placed and 'sign' in placed, f'the guest placed {placed}' + host_unaffected(host, "the guest's sign") + return placed + + +def effect_ids(game): + return [e.get('id', '').removeprefix('minecraft:') for e in game('state')['player'].get('effects', [])] + + +def check_effects(host, guest, out): + x, y, z = (int(v // 1) for v in pos_of(host)) + bx, by, bz = x - 3, y, z - 3 + host('command', command=f'fill {bx - 1} {by - 1} {bz - 1} {bx + 1} {by - 1} {bz + 1} minecraft:iron_block') + host('command', command=f'setblock {bx} {by} {bz} minecraft:beacon{{primary_effect:"minecraft:speed"}}') + # BeaconBlockEntity.tick applies its powers every 80 ticks, once the beam has reached the sky. + seen = {} + for _ in range(15): + host.wait(20) + seen = {'host': effect_ids(host), 'guest': effect_ids(guest)} + if all('speed' in v for v in seen.values()): + break + host.shot(out, 'effects') + guest.shot(out, 'effects') + assert 'speed' in seen['host'] and 'speed' in seen['guest'], f'beacon effects: {seen}' + return seen + + +def wait_dimension(game, dimension, ticks): + """Wait up to `ticks` for the game to be in `dimension` with its world loaded around it.""" + for _ in range(ticks // 20): + state = game('state') + if state.get('dimension') == dimension and state.get('player'): + return state + game.wait(20) + return game('state') + + +def nether_portal(game, x, y, z): + """A lit portal along x, two blocks wide and three tall, its inside starting at x y z.""" + game('command', command=f'fill {x - 1} {y - 1} {z} {x + 2} {y + 3} {z} minecraft:obsidian') + game('command', command=f'fill {x} {y} {z} {x + 1} {y + 2} {z} minecraft:nether_portal[axis=x]') + + +def lan_guest(host): + """The guest's entry in the host's `lan status` (on the server thread the owner is listed too).""" + guests = host.raw('lan', 'status')['guests'] + return next(g for g in guests if g['name'] == GUEST_NAME) + +def check_distance(host, guest, out): + """IntegratedServer.tickServer: the server takes the host's render distance as its view distance + (past 32, which Horizon's options allow) and sends it to every player (SetChunkCacheRadius).""" + if not SERVER_THREAD: + return 'skipped (needs --server-thread)' + seen = {} + try: + for distance in (48, 6): + host.raw('render-distance', distance) + # The pause screen doesn't hold the server here: it follows on its next tick. + host.wait(20) + guest.wait(20) + seen[distance] = (host('status').get('render_distance'), guest('status').get('render_distance')) + finally: + host.raw('render-distance', 12) + # A client streams out to min(its render distance, the server's view distance + 1). + assert seen[48][0] == 48, f'the host at 48 streams {seen[48][0]} chunks out (all: {seen})' + assert seen[6][0] == 6, f'the host at 6 streams {seen[6][0]} chunks out (all: {seen})' + assert seen[6][1] == 7, f'the guest streams {seen[6][1]} with the server at 6 (all: {seen})' + return seen + + +def check_portals(host, guest, out): + if not SERVER_THREAD: + return 'skipped (needs --server-thread)' + x, y, z = (int(v // 1) for v in pos_of(guest)) + nether_portal(guest, x, y, z + 3) + guest.wait(5) + guest('command', command=f'tp @s {x + 0.5} {y} {z + 3.5}') + # players_nether_portal_default_delay: 80 ticks in survival. + there = wait_dimension(guest, 'minecraft:the_nether', 600) + guest.wait(40) + guest.shot(out, 'portals-nether') + host_dim = host('state').get('dimension') + assert there.get('dimension') == 'minecraft:the_nether', f'the guest is in {there.get("dimension")}' + assert host_dim == 'minecraft:overworld', f'the host went to {host_dim} with the guest' + arrived = [int(v // 1) for v in pos_of(guest)] + below = guest.raw('block', arrived[0], arrived[1] - 1, arrived[2]) + assert below.get('loaded') and below.get('block') != 'minecraft:air', f'under the guest in the Nether: {below}' + # The host takes an End portal while the guest is in the Nether: three levels at once. + hx, hy, hz = (int(v // 1) for v in pos_of(host)) + host('command', command=f'setblock {hx} {hy} {hz} minecraft:end_portal') + end = wait_dimension(host, 'minecraft:the_end', 600) + host.wait(40) + host.shot(out, 'portals-end') + guest_dim = guest('state').get('dimension') + assert end.get('dimension') == 'minecraft:the_end', f'the host is in {end.get("dimension")}' + assert guest_dim == 'minecraft:the_nether', f'the guest left the Nether for {guest_dim}' + # Both come back: the host through an exit portal, the guest out of its portal and in again. + ex, ey, ez = (int(v // 1) for v in pos_of(host)) + host('command', command=f'setblock {ex + 2} {ey} {ez} minecraft:end_portal') + host('command', command=f'tp @s {ex + 2.5} {ey} {ez + 0.5}') + guest('command', command=f'tp @s {arrived[0] + 0.5} {arrived[1]} {arrived[2] - 2.5}') + guest.wait(30) + guest('command', command=f'tp @s {arrived[0] + 0.5} {arrived[1]} {arrived[2] + 0.5}') + home = wait_dimension(host, 'minecraft:overworld', 600) + back = wait_dimension(guest, 'minecraft:overworld', 600) + guest.wait(40) + host.shot(out, 'portals-back') + guest.shot(out, 'portals-back') + assert home.get('dimension') == 'minecraft:overworld', f'the host stayed in {home.get("dimension")}' + assert back.get('dimension') == 'minecraft:overworld', f'the guest stayed in {back.get("dimension")}' + return {'guest_nether': arrived, 'host_end': [ex, ey, ez], 'back': pos_of(guest)} + + def check_damage(host, guest, out): guest('command', command='gamemode survival') guest.wait(10) @@ -313,6 +515,9 @@ def check_hazards(host, guest, out): # A fresh life: full health, so no regeneration eats food while the guest runs. guest('command', command='kill @s') guest.wait(20) + # ServerPlayer.doTick sends the dead player's health of 0 too. + died = {'screen': guest('state').get('screen'), 'health': remote(guest)['health']} + assert died == {'screen': 'dead', 'health': 0.0}, f'the guest was killed: {died}' guest.raw('respawn') guest.wait(40) # Sprint-jumping down a stone lane: ServerPlayer.jumpFromGround and checkMovementStatistics. @@ -321,14 +526,14 @@ def check_hazards(host, guest, out): guest.wait(10) x, y, z = pos_of(guest) guest.raw('look-at', f'{x + 60:.2f}', f'{y + 1.6:.2f}', f'{z:.2f}') - food = host.raw('lan', 'status')['guests'][0]['food'] + food = lan_guest(host)['food'] for k in ('w', 'ctrl', 'space'): guest.raw('key', k, 'down') guest.wait(160) for k in ('space', 'ctrl', 'w'): guest.raw('key', k, 'up') guest.wait(10) - fed = host.raw('lan', 'status')['guests'][0]['food'] + fed = lan_guest(host)['food'] hunger = {'before': food, 'after': fed} hurt = {} hx, hy, hz = (int(v // 1) for v in pos_of(host)) @@ -350,11 +555,13 @@ def check_hazards(host, guest, out): guest('command', command=f'tp @s {x + 0.5} -140 {z + 0.5}') hurt[name] = (before, first_hurt(guest, before, ticks)) guest.shot(out, f'hazards-{name}') + # The host tidies up: the void can kill the guest before this, the server drops a dead + # guest's packets (lan_guest_packet), and the box left standing suffocated it in pvp. # Water puts the lava's fire out. - guest('command', command=f'fill {x} {y} {z} {x} {y + 1} {z} minecraft:water') + host('command', command=f'fill {x} {y} {z} {x} {y + 1} {z} minecraft:water') guest.wait(5) - guest('command', command=f'fill {x - 1} {y - 1} {z - 1} {x + 1} {y + 2} {z + 1} minecraft:air') - guest('command', command=f'fill {x - 1} {y - 1} {z - 1} {x + 1} {y - 1} {z + 1} minecraft:stone') + host('command', command=f'fill {x - 1} {y - 1} {z - 1} {x + 1} {y + 2} {z + 1} minecraft:air') + host('command', command=f'fill {x - 1} {y - 1} {z - 1} {x + 1} {y - 1} {z + 1} minecraft:stone') guest.wait(20) if remote(guest)['health'] <= 0: guest.raw('respawn') @@ -365,7 +572,7 @@ def check_hazards(host, guest, out): assert spent > 1.0, f'no hunger from moving: {hunger} ({spent:.2f} spent)' for name, (before, after) in hurt.items(): assert after < before, f'{name} never hurt the guest: health {before} -> {after} (all: {hurt})' - return {'hunger': hunger, **hurt} + return {'died': died, 'hunger': hunger, **hurt} def check_pvp(host, guest, out): @@ -376,8 +583,14 @@ def check_pvp(host, guest, out): hits = {} try: for name, rule, attacker in (('guest hits host', 'true', guest), ('host hits guest', 'true', host), - ('pvp off', 'false', guest)): + ('pvp off', 'false', guest), ('guest hits teammate', 'true', guest), + ('host hits teammate', 'true', host)): host('command', command=f'gamerule pvp {rule}') + if name == 'guest hits teammate': + # Player.canHarmPlayer: the victim's team without friendly fire spares its allies. + host('command', command='team add parity_allies') + host('command', command='team modify parity_allies friendlyFire false') + host('command', command='team join parity_allies @a') face_each_other(host, guest) attacker.wait(30) # a full attack charge victim = (lambda: host('state')['player']['health']) if attacker is guest else (lambda: remote(guest)['health']) @@ -388,12 +601,81 @@ def check_pvp(host, guest, out): attacker.shot(out, 'pvp-' + name.replace(' ', '-')) finally: host('command', command='gamerule pvp true') + host('command', command='team remove parity_allies') assert hits['guest hits host'][1] < hits['guest hits host'][0], f'the guest did not hurt the host: {hits}' assert hits['host hits guest'][1] < hits['host hits guest'][0], f'the host did not hurt the guest: {hits}' assert hits['pvp off'][1] == hits['pvp off'][0], f'pvp off still hurts: {hits}' + for name in ('guest hits teammate', 'host hits teammate'): + assert hits[name][1] == hits[name][0], f'friendly fire off still hurts ({name}): {hits}' return hits +def dead_players(game, want, ticks=60): + """The other players `game`'s client draws dead, once that is `want` (empty or not) or after `ticks`.""" + for _ in range(ticks // 5): + dead = remote(game)['dead_players'] + if bool(dead) == want: + return dead + game.wait(5) + return dead + + +def check_respawn(host, guest, out): + # ServerLevel.removePlayerImmediately and addRespawnedPlayer: a respawned player is removed + # from the other players and paired again as a new entity, so their clients drop the body. + # The host window only has a client's view of the guest on the server thread. + for g in (host, guest): + back_to_game(g) + results = {} + sides = [('host', host, guest)] + ([('guest', guest, host)] if SERVER_THREAD else []) + for name, dying, watching in sides: + face_each_other(host, guest) + dying('command', command='kill @s') + dying.wait(30) + dead = dead_players(watching, True) + watching.shot(out, f'respawn-{name}-dead') + dying.raw('respawn') + dying.wait(40) + back_to_game(dying) + # Back within tracking range of the other player, wherever the respawn point is. + x, y, z = pos_of(dying) + watching('command', command=f'tp @s {x + 3:.2f} {y:.2f} {z:.2f}') + watching.wait(40) + alive = dead_players(watching, False) + watching.shot(out, f'respawn-{name}-alive') + results[name] = {'dead': dead, 'after_respawn': alive} + assert dead, f'the {name} was not drawn dead: {results}' + assert not alive, f'the {name} still lies dead after respawning: {results}' + return results + + +def check_hurt(host, guest, out): + for g in (host, guest): + g('command', command='gamemode survival') + g('command', command='clear @s') + face_each_other(host, guest) + hx, hy, hz = pos_of(host) + # Between the two, within both players' reach; bare hands leave it alive. + host('command', command=f'summon minecraft:pig {hx + 1.5:.2f} {hy:.2f} {hz + 0.8:.2f} {{NoAI:1b}}') + host.wait(30) + + def pig(game): + return game.raw('entities', 6, 'pig')['entities'][0] + seen = {} + for name, attacker, watcher in (('guest hits', guest, host), ('host hits', host, guest)): + attacker.wait(30) # a full attack charge + attacker.raw('attack', pig(attacker)['id']) + seen[name] = {'attacker': pig(attacker)['hurt_time'], 'watcher': pig(watcher)['hurt_time']} + attacker.shot(out, 'hurt-' + name.replace(' ', '-')) + watcher.shot(out, 'hurt-' + name.replace(' ', '-') + '-seen') + attacker.wait(20) + host('command', command='kill @e[type=minecraft:pig]') + # LivingEntity.hurtTime: 10 when hurt, one less each tick; the reads come a few ticks later. + for name, sides in seen.items(): + assert all(t > 0 for t in sides.values()), f'{name}: the pig\'s hurt flash {seen}' + return seen + + def container_items(game): """The open container's stacks by slot (Horizon's container slots are 300 and up) and its size.""" slots = [s for s in game('screen').get('slots', []) if s.get('slot', 0) >= 300] @@ -456,12 +738,162 @@ def check_containers(host, guest, out): return {'ender': ender, 'double': double, 'taken': taken} +def check_machines(host, guest, out): + # After `recipes`: the stacks given here unlock recipes, which would move the book's first one. + guest('command', command='gamemode survival') + x, y, z = (int(v // 1) for v in pos_of(guest)) + at = (x, y, z + 2) + seen = {} + for block in ('hopper', 'dispenser', 'dropper'): + guest('command', command='clear @s') + guest('command', command='give @s minecraft:cobblestone 4') + guest('command', command=f'setblock {at[0]} {at[1]} {at[2]} minecraft:{block}') + guest.wait(10) + guest.raw('interact', *at) + guest.raw('wait-for', 'screen', 'container', '--timeout', 10) + guest.wait(10) + size = container_items(guest)[1] + guest.shot(out, f'machines-{block}') + guest.raw('click-slot', hotbar_slot(guest, 'minecraft:cobblestone'), 'shift') + guest.wait(10) + guest.raw('key', 'esc', 'tap') + guest.wait(10) + # Opened again, the menu holds what the server has. + seen[block] = (size, open_container(guest, at, out, f'machines-{block}-after')[0]) + guest('command', command=f'setblock {at[0]} {at[1]} {at[2]} minecraft:air') + want = {'hopper': (5, {0: 'minecraft:cobblestone'}), 'dispenser': (9, {0: 'minecraft:cobblestone'}), + 'dropper': (9, {0: 'minecraft:cobblestone'})} + assert seen == want, f'the machines on the guest: {seen}' + return seen + + +def check_brewing(host, guest, out): + guest('command', command='gamemode survival') + guest('command', command='clear @s') + loads = ('blaze_powder', 'nether_wart', 'glass_bottle') + for item in loads: + guest('command', command=f'give @s minecraft:{item}') + x, y, z = (int(v // 1) for v in pos_of(guest)) + at = (x, y, z + 2) + guest('command', command=f'setblock {at[0]} {at[1]} {at[2]} minecraft:brewing_stand') + guest.wait(10) + guest.raw('interact', *at) + guest.raw('wait-for', 'screen', 'container', '--timeout', 10) + guest.wait(10) + size = container_items(guest)[1] + # BrewingStandMenu.quickMoveStack: blaze powder to the fuel slot, an ingredient to its slot, + # a bottle to the bottle slots. + for item in loads: + guest.raw('click-slot', hotbar_slot(guest, f'minecraft:{item}'), 'shift') + guest.wait(5) + # BrewingStandBlockEntity.serverTick burns the blaze powder into 20 uses; the guest sees them + # through the menu's data slots. + guest.wait(10) + info = guest.raw('menu')['info'] + guest.shot(out, 'brewing') + guest.raw('key', 'esc', 'tap') + guest.wait(10) + items = open_container(guest, at, out, 'brewing-after')[0] + guest('command', command=f'setblock {at[0]} {at[1]} {at[2]} minecraft:air') + seen = {'size': size, 'items': items, 'fuel': info and info.get('fuel')} + want = {'size': 5, 'items': {0: 'minecraft:glass_bottle', 3: 'minecraft:nether_wart'}, 'fuel': 20} + assert seen == want, f'the brewing stand on the guest: {seen}' + return seen + + +def slot_point(game, slot): + """The GUI point in the middle of slot `slot` of the open screen (`ui`'s slot-N).""" + for w in game.raw('ui')['widgets']: + if w.get('id') == f'slot-{slot}': + x, y, width, height = w['bounds'] + return x + width / 2, y + height / 2 + raise Failed(f'no slot {slot} on the screen') + + +def mouse(game, *slots, right=False): + """Press over the first slot, move over the others and release: one click, or a drag.""" + points = [f'{v:.1f}' for s in slots for v in slot_point(game, s)] + game.raw('drag-at', *(['right'] if right else []), *points) + game.wait(4) + + +def hotbar_slot(game, item): + return next(s['slot'] for s in game('inventory')['hotbar'] if s and s.get('item') == item) + + +def check_clicks(host, guest, out): + guest('command', command='gamemode survival') + guest('command', command='clear @s') + guest('command', command='give @s minecraft:cobblestone 10') + guest('command', command='give @s minecraft:coal 3') + guest('command', command='give @s minecraft:raw_iron 2') + guest('command', command='give @s minecraft:oak_planks 2') + guest.wait(10) + x, y, z = (int(v // 1) for v in pos_of(guest)) + chest, furnace = (x, y, z + 2), (x, y, z - 2) + guest('command', command=f'setblock {chest[0]} {chest[1]} {chest[2]} minecraft:chest') + guest('command', command=f'setblock {furnace[0]} {furnace[1]} {furnace[2]} minecraft:furnace') + guest.wait(10) + # Pick the cobblestone up and put it down in the chest's first slot. + guest.raw('interact', *chest) + guest.raw('wait-for', 'screen', 'container', '--timeout', 10) + guest.wait(10) + mouse(guest, hotbar_slot(guest, 'minecraft:cobblestone')) + mouse(guest, 300) + guest.wait(10) + guest.shot(out, 'clicks-chest') + guest.raw('key', 'esc', 'tap') + guest.wait(10) + in_chest, _ = open_container(guest, chest, out, 'clicks-chest-after') + # Coal into the fuel slot, raw iron into the input. + guest.raw('interact', *furnace) + guest.raw('wait-for', 'screen', 'container', '--timeout', 10) + guest.wait(10) + mouse(guest, hotbar_slot(guest, 'minecraft:coal')) + mouse(guest, 301) + mouse(guest, hotbar_slot(guest, 'minecraft:raw_iron')) + mouse(guest, 300) + guest.wait(10) + guest.shot(out, 'clicks-furnace') + guest.raw('key', 'esc', 'tap') + guest.wait(10) + # Opening the furnace again resends the whole menu: the server's own view. + in_furnace, _ = open_container(guest, furnace, out, 'clicks-furnace-after') + # Two planks dragged over the 2x2 grid's left column: one each, four sticks. + guest.raw('key', 'e', 'tap') + guest.raw('wait-for', 'screen', 'inventory') + guest.wait(5) + mouse(guest, hotbar_slot(guest, 'minecraft:oak_planks')) + mouse(guest, 100, 102) + guest.wait(10) + grid = {s['slot']: (s.get('item'), s.get('count')) for s in guest('screen').get('slots', []) + if s.get('slot') in (100, 102, 200) and s.get('item')} + guest.shot(out, 'clicks-drag') + guest.raw('click-slot', 200, 'shift') + guest.wait(10) + guest.raw('key', 'esc', 'tap') + guest.wait(10) + # The chest's menu brings the whole inventory as the server has it. + open_container(guest, chest, out, 'clicks-inventory') + counts = guest('inventory')['counts'] + for at in (chest, furnace): + guest('command', command=f'setblock {at[0]} {at[1]} {at[2]} minecraft:air') + assert in_chest == {0: 'minecraft:cobblestone'}, f'the chest holds {in_chest} after the guest put cobblestone in' + assert in_furnace == {0: 'minecraft:raw_iron', 1: 'minecraft:coal'}, f'the furnace holds {in_furnace}' + assert grid.get(100) == ('minecraft:oak_planks', 1) and grid.get(102) == ('minecraft:oak_planks', 1), \ + f'the drag left the grid as {grid}' + assert counts.get('minecraft:stick') == 4 and 'minecraft:cobblestone' not in counts, f'inventory after: {counts}' + return {'chest': in_chest, 'furnace': in_furnace, 'grid': grid, 'inventory': counts} + + def check_feedback(host, guest, out): x, y, z = (round(v, 1) for v in pos_of(guest)) guest('command', command='item replace entity @s weapon.mainhand with minecraft:stick') guest('command', command=f'tp @s {x:.1f} {y:.1f} {z:.1f}') guest.wait(10) chat = guest.raw('messages', 5)['chat'] + # The host is an operator: broadcastToAdmins tells it, as `[Guest: ...]`. + host_chat = host.raw('messages', 5)['chat'] guest.raw('key', 't', 'tap') guest.wait(5) guest.shot(out, 'feedback') @@ -472,7 +904,128 @@ def check_feedback(host, guest, out): assert any(replaced in line for line in chat), f'no "{replaced}" in {chat}' assert any(line.startswith(f'Teleported {GUEST_NAME} to {x:.6f}') for line in chat), f'teleport feedback: {chat}' assert screen in (None, 'playing'), f'the chat did not close: {screen}' - return chat + told = f'[{GUEST_NAME}: Teleported {GUEST_NAME} to {x:.6f}' + assert any(line.startswith(told) for line in host_chat), f'no "{told}" for the host: {host_chat}' + return chat + host_chat + + +def chat_input(game): + return game('screen').get('chat_input') + + +def complete(guest, text): + """Opens the chat on `/`, types `text` and presses Tab: the input after it.""" + guest.raw('key', 'slash', 'tap') + guest.wait(3) + guest.raw('type', text) + guest.wait(2) + guest.raw('key', 'tab', 'tap') + guest.wait(2) + return chat_input(guest) + + +def check_commands(host, guest, out): + literal = complete(guest, 'gamem') + # The first Escape hides the suggestions, the next closes the chat. + for _ in range(3): + if guest('screen').get('screen') != 'chat': + break + guest.raw('key', 'esc', 'tap') + guest.wait(3) + argument = complete(guest, 'gamemode cr') + guest.shot(out, 'commands') + guest.raw('key', 'enter', 'tap') + guest.wait(20) + mode = guest('state').get('gamemode') + screen = guest('state').get('screen') + guest('command', command='gamemode survival') + guest.wait(10) + # A node the tree marks ask_server (a bossbar id): the guest asks the + # server, which completes it from its own bossbars. + host('command', command='bossbar add pt6:check "Check"') + host.wait(5) + guest.raw('key', 'slash', 'tap') + guest.wait(3) + guest.raw('type', 'bossbar get pt') + guest.wait(10) + guest.raw('key', 'tab', 'tap') + guest.wait(2) + asked = chat_input(guest) + for _ in range(3): + if guest('screen').get('screen') != 'chat': + break + guest.raw('key', 'esc', 'tap') + guest.wait(3) + host('command', command='bossbar remove pt6:check') + assert literal == '/gamemode', f'Tab after /gamem: {literal!r}' + assert argument == '/gamemode creative', f'Tab after /gamemode cr: {argument!r}' + assert mode == 'creative', f'the completed command did not run: {mode}' + assert screen in (None, 'playing'), f'the chat did not close: {screen}' + assert asked == '/bossbar get pt6:check', f'Tab after /bossbar get pt: {asked!r}' + return {'literal': literal, 'argument': argument, 'mode': mode, 'ask_server': asked} + + +def centre(rect): + x, y, w, h = rect + return x + w / 2, y + h / 2 + + +def grid_items(game): + slots = game('screen').get('slots') or [] + return {s['slot']: s.get('item') for s in slots if 100 <= s['slot'] < 109 and s.get('item')} + + +def check_recipes(host, guest, out): + guest('command', command='clear @s') + guest('command', command='give @s minecraft:oak_log 4') + guest.wait(40) + guest.raw('key', 'e', 'tap') + guest.wait(5) + try: + book = guest('screen').get('recipe_book') + assert book, 'the guest\'s inventory has no recipe book' + if len(book['targets']) == 1: + guest.raw('click-at', *centre(book['targets'][0])) + guest.wait(5) + book = guest('screen').get('recipe_book') + known = book['known'] + assert known > 0, f'the guest\'s book is empty: {book}' + guest.raw('click-at', *centre(book['targets'][1])) + guest.wait(10) + grid = grid_items(guest) + guest.shot(out, 'recipes') + finally: + guest.raw('key', 'esc', 'tap') + guest.wait(5) + assert any('oak_log' in str(item) for item in grid.values()), f'nothing placed in the grid: {grid}' + return {'known': known, 'grid': grid} + + +def check_titles(host, guest, out): + # TitleCommand sends the guest its title packets; its HUD shows and clears them. + for command in ('title @s times 10 400 10', 'title @s subtitle "Under"', 'title @s title "Over"', + 'title @s actionbar "Bar"'): + guest('command', command=command) + guest.wait(5) + shown = {k: guest.raw('messages', 1)[k] for k in ('title', 'subtitle', 'actionbar')} + guest.shot(out, 'titles') + guest('command', command='title @s clear') + guest.wait(5) + cleared = {k: guest.raw('messages', 1)[k] for k in ('title', 'subtitle')} + assert shown == {'title': 'Over', 'subtitle': 'Under', 'actionbar': 'Bar'}, f'titles shown: {shown}' + assert cleared == {'title': None, 'subtitle': None}, f'titles cleared: {cleared}' + return {'shown': shown, 'cleared': cleared} + + +def check_chat(host, guest, out): + # The agent's event log follows a remote client's chat: `wait-for chat` sees the line another + # player sends (and on the server thread the host window's own line, back from the server). + line = f'chat check {int(time.time())}' + host.raw('chat', line) + seen = {g.name: g.raw('wait-for', 'chat', line, '--timeout', 10, '--since', 0).get('ok') + for g in ((guest, host) if SERVER_THREAD else (guest,))} + guest.shot(out, 'chat') + return seen def png_rows(path): @@ -519,7 +1072,7 @@ def changed_pixels(a, b): def sneaking_on_host(host, want, ticks=40): """Whether the host sees the guest crouching, once it matches `want` or after `ticks`.""" for _ in range(ticks // 5): - sneaking = host.raw('lan', 'status')['guests'][0]['sneaking'] + sneaking = lan_guest(host)['sneaking'] if sneaking == want: return sneaking host.wait(5) @@ -580,6 +1133,27 @@ def wait_state(game, pos, key, want, ticks): return state_of(game, pos, key) +def check_plates(host, guest, out): + guest('command', command='gamemode survival') + x, y, z = (int(v // 1) for v in pos_of(guest)) + plate = (x + 2, y, z) + guest('command', command=f'setblock {plate[0]} {plate[1]} {plate[2]} minecraft:stone_pressure_plate') + guest.wait(10) + guest('command', command=f'tp @s {plate[0] + 0.5} {plate[1]} {plate[2] + 0.5}') + pressed = {'host': wait_state(host, plate, 'powered', 'true', 40), + 'guest': wait_state(guest, plate, 'powered', 'true', 40)} + guest.shot(out, 'plate-pressed') + guest('command', command=f'tp @s {x + 0.5} {y} {z + 0.5}') + # PressurePlateBlock.getPressedTime: a stone plate checks again every 20 ticks. + released = {'host': wait_state(host, plate, 'powered', 'false', 60), + 'guest': wait_state(guest, plate, 'powered', 'false', 60)} + guest('command', command=f'setblock {plate[0]} {plate[1]} {plate[2]} minecraft:air') + detail = {'pressed': pressed, 'released': released} + assert pressed == {'host': 'true', 'guest': 'true'}, f'the plate under the guest: {detail}' + assert released == {'host': 'false', 'guest': 'false'}, f'the plate after the guest left: {detail}' + return detail + + def check_trials(host, guest, out): # The host watches as a spectator, whom neither block detects: only the guest can wake them. hx, hy, hz = pos_of(host) @@ -622,15 +1196,464 @@ def check_trials(host, guest, out): return {'spawner': trial, 'vault': active, 'rewarded': uuid(rewarded), 'host': uuid(host_uuid)} +def bed_at(game, foot): + """A bed whose foot is at `foot`, its head one block east.""" + x, y, z = foot + game('command', command=f'setblock {x} {y} {z} minecraft:red_bed[facing=east,part=foot]') + game('command', command=f'setblock {x + 1} {y} {z} minecraft:red_bed[facing=east,part=head]') + return [x + 1, y, z] + + +def check_sleep(host, guest, out): + for g in (host, guest): + g('command', command='gamemode survival') + host('command', command='time set midnight') + gx, gy, gz = (int(v // 1) for v in pos_of(guest)) + hx, hy, hz = (int(v // 1) for v in pos_of(host)) + guest_bed = bed_at(guest, (gx, gy, gz + 2)) + host_bed = bed_at(host, (hx, hy, hz + 2)) + guest.wait(10) + guest.raw('interact', *guest_bed) + guest.wait(10) + asleep = {'guest': guest('state').get('sleeping'), 'host': host('state').get('sleeping'), + 'server': lan_guest(host).get('sleeping')} + guest.shot(out, 'sleep-guest') + # Alone in bed: still night 120 ticks later (everyone must sleep). + guest.wait(120) + night = host('state')['time']['time_of_day'] + # Escape gets the guest up (InBedChatScreen), then back to bed. + guest.raw('key', 'esc', 'tap') + guest.wait(10) + up = guest('state').get('sleeping') + guest.raw('interact', *guest_bed) + host.raw('interact', *host_bed) + host.wait(130) + after = {'guest': guest('state').get('sleeping'), 'host': host('state').get('sleeping'), + 'time': host('state')['time']['time_of_day']} + guest.shot(out, 'sleep-morning') + for at in (guest_bed, host_bed): + guest('command', command=f'fill {at[0] - 1} {at[1]} {at[2]} {at[0]} {at[1]} {at[2]} minecraft:air') + host('command', command='time set day') + detail = {'asleep': asleep, 'night': night, 'up': up, 'after': after} + assert asleep['guest'] == guest_bed and asleep['server'] == guest_bed, f'the guest is not in its bed: {detail}' + assert asleep['host'] is None, f'the guest\'s bed put the host to sleep: {detail}' + assert 13000 < night < 23000, f'one of two players asleep ended the night: {detail}' + assert up is None, f'Escape did not get the guest up: {detail}' + assert after['guest'] is None and after['host'] is None and after['time'] < 1000, f'no morning: {detail}' + return detail +def check_push(host, guest, out): + guest('command', command='gamemode survival') + x, y, z = (int(v // 1) + 0.5 for v in pos_of(guest)) + guest('command', command=f'tp @s {x} {y - 0.5} {z}') + guest.wait(20) + x, y, z = pos_of(guest) + # A cow that doesn't walk, 0.3 east of the guest: the two boxes overlap until their centres are + # 0.75 apart, so the push stops a fraction of a block away. + guest('command', command=f'summon minecraft:cow {x + 0.3:.2f} {y:.2f} {z:.2f} {{NoAI:1b}}') + guest.wait(60) + nx, _, nz = pos_of(guest) + guest.shot(out, 'push') + guest('command', command='kill @e[type=minecraft:cow]') + moved = ((nx - x) ** 2 + (nz - z) ** 2) ** 0.5 + detail = {'from': [round(x, 3), round(z, 3)], 'to': [round(nx, 3), round(nz, 3)], 'moved': round(moved, 3)} + assert nx < x, f'the cow did not push the guest west: {detail}' + assert moved < 1.5, f'the cow pushed the guest too far: {detail}' + return detail +def check_eat(host, guest, out): + guest('command', command='gamemode survival') + guest('command', command='clear @s') + # Hungry: the eating needs food below 20 (Player.canEat). + guest('command', command='effect give @s minecraft:hunger 3 255') + guest.wait(70) + guest('command', command='effect clear @s') + guest('command', command='give @s minecraft:bread 3') + guest.wait(5) + guest.raw('select', 'minecraft:bread') + hungry = guest('state')['player']['food'] + # use-item --hold returns once released: watch the guest's screen meanwhile. + hold = threading.Thread(target=guest.raw, args=('use-item', '--hold', 2.2)) + hold.start() + using = [] + for _ in range(4): + time.sleep(0.2) + using.append(guest('state').get('using')) + guest.shot(out, 'eat') + hold.join() + guest.wait(10) + fed = guest('state')['player']['food'] + left = guest('inventory')['counts'].get('minecraft:bread', 0) + detail = {'hungry': hungry, 'fed': fed, 'bread': left, 'using': using, 'after': guest('state').get('using')} + assert hungry < 20, f'the guest is not hungry: {detail}' + ticks = [u['ticks'] for u in using if u] + assert len(ticks) >= 3 and ticks == sorted(ticks) and ticks[-1] > ticks[0], f'no eating on the guest\'s screen: {detail}' + assert left == 2 and fed > hungry, f'the server did not eat one loaf: {detail}' + assert detail['after'] is None, f'still eating after the release: {detail}' + return detail + + +def watch_mining(miner, pos, seen): + """Mine the block at `pos` (by hand, for seconds) and sample `seen()` meanwhile.""" + dig = threading.Thread(target=miner.raw, args=('break', *pos)) + dig.start() + samples = [] + for _ in range(8): + time.sleep(0.25) + samples.append(seen()) + dig.join() + return samples + + +def check_swings(host, guest, out): + for g in (host, guest): + g('command', command='gamemode survival') + g('command', command='clear @s') + face_each_other(host, guest) + hx, hy, hz = (int(v // 1) for v in pos_of(host)) + gx, gy, gz = (int(v // 1) for v in pos_of(guest)) + # Stone by hand takes 7.5 seconds: ServerboundPunchPacket every tick keeps the arm swinging. + guest('command', command=f'setblock {gx} {gy} {gz + 1} minecraft:stone') + host('command', command=f'setblock {hx} {hy} {hz + 1} minecraft:stone') + host.wait(10) + def swinging(game): + # On its server thread the host's window is a client of its server too. + if game is host and not SERVER_THREAD: + return lan_guest(host).get('model_swinging') + return any(e.get('swinging') for e in remote(game).get('player_equipment', [])) + # Most samples: a swing restarts only past its first half (SwingState.startIfAble), so a sample can + # fall between two. + host_sees = watch_mining(guest, (gx, gy, gz + 1), lambda: swinging(host)) + guest_sees = watch_mining(host, (hx, hy, hz + 1), lambda: swinging(guest)) + # A hoe on dirt: the server tills it and swings the guest's hand, on the guest's screen too. + # Where the guest stands now (an earlier check can leave it on a ledge it then drops from). + gx, gy, gz = (int(v // 1) for v in pos_of(guest)) + soil = (gx, gy - 1, gz + 1) + guest('command', command=f'setblock {soil[0]} {soil[1]} {soil[2]} minecraft:dirt') + # HoeItem.useOn tills only under air. + guest('command', command=f'setblock {soil[0]} {soil[1] + 1} {soil[2]} minecraft:air') + guest('command', command='give @s minecraft:wooden_hoe') + guest.wait(10) + guest.raw('select', 'minecraft:wooden_hoe') + before = guest('state')['swings'] + guest.raw('interact', *soil) + guest.wait(10) + hoe = {'swings': guest('state')['swings'] - before, 'soil': host.raw('block', *soil)['block']} + guest('command', command=f'setblock {soil[0]} {soil[1]} {soil[2]} minecraft:stone') + detail = {'host_sees_guest': host_sees, 'guest_sees_host': guest_sees, 'hoe': hoe} + assert sum(map(bool, host_sees)) >= 4, f'the host does not see the guest mine: {detail}' + assert sum(map(bool, guest_sees)) >= 4, f'the guest does not see the host mine: {detail}' + assert hoe == {'swings': 1, 'soil': 'minecraft:farmland'}, f'the guest\'s hoe: {detail}' + return detail + + +def check_eating(host, guest, out): + for g in (host, guest): + g('command', command='gamemode survival') + g('command', command='clear @s') + # Hungry: Player.canEat. + g('command', command='effect give @s minecraft:hunger 3 255') + host.wait(70) + for g in (host, guest): + g('command', command='effect clear @s') + g('command', command='give @s minecraft:bread 2') + host.wait(5) + face_each_other(host, guest) + + def using(game): + # On its server thread the host's window is a client of its server too. + if game is host and not SERVER_THREAD: + return lan_guest(host).get('model_using') or 0 + return max((e.get('using') or 0 for e in remote(game).get('player_equipment', [])), default=0) + seen = {} + for name, eater, watcher in (('host eats', host, guest), ('guest eats', guest, host)): + eater.raw('select', 'minecraft:bread') + hold = threading.Thread(target=eater.raw, args=('use-item', '--hold', 2.2)) + hold.start() + samples = [] + for _ in range(4): + time.sleep(0.3) + samples.append(using(watcher)) + watcher.shot(out, 'eating-' + name.replace(' ', '-')) + hold.join() + eater.wait(10) + seen[name] = {'during': samples, 'after': using(watcher)} + for name, s in seen.items(): + assert sum(1 for u in s['during'] if u > 0) >= 3 and s['after'] == 0, f'{name}: {seen}' + return seen + + +def check_xp(host, guest, out): + for g in (host, guest): + g('command', command='gamemode survival') + g('command', command='xp set @s 0 levels') + g('command', command='xp set @s 0 points') + # Out of the orbs' way wherever an earlier check left the host (ExperienceOrb follows the + # nearest player who isn't a spectator). + host('command', command='gamemode spectator') + guest.wait(10) + # `xp set` leaves Player.totalExperience (the score): experience from earlier checks stays in it. + total = guest('state')['player']['xp']['total'] + host_total = host('state')['player']['xp']['total'] + x, y, z = pos_of(guest) + # Three orbs of 7 points 4 blocks east of the guest (out of reach, within the 8 blocks orbs + # follow from): they fly to the guest. + for _ in range(3): + guest('command', command=f'summon minecraft:experience_orb {x + 4:.2f} {y + 0.5:.2f} {z:.2f} {{Value:7s}}') + guest.wait(2) + seen = remote(guest).get('entities', {}).get('minecraft:experience_orb', 0) + guest.shot(out, 'xp-orbs') + guest.wait(60) + xp = guest('state')['player']['xp'] + host_xp = host('state')['player']['xp'] + left = remote(guest).get('entities', {}).get('minecraft:experience_orb', 0) + detail = {'seen': seen, 'guest': xp, 'host': host_xp, 'left': left} + # ExperienceOrb.scanForMerges: orbs of one value side by side can merge into one. + assert 1 <= seen <= 3, f'the guest saw {seen} orbs: {detail}' + assert xp['total'] - total == 21 and xp['level'] == 2, f'the guest\'s experience after three 7-point orbs: {detail}' + assert host_xp['total'] == host_total, f'the host took the orbs: {detail}' + assert left == 0, f'orbs left on the guest\'s screen: {detail}' + return {'seen': seen, 'guest': xp, 'host': host_xp} + + +def check_time(host, guest, out): + def times(): + return host('state')['time']['day_time'], guest('state')['time']['day_time'] + host('command', command='time set 1000') + host('command', command='time pause') + host.wait(60) + paused = times() + host('command', command='time resume') + guest.wait(20) + # A stalled game runs at most 10 missed ticks when it wakes (DeltaTracker); the server's + # game time, sent every second, moves the guest's clock on (ClientClockManager.tick). + os.kill(guest.pid, signal.SIGSTOP) + time.sleep(4) + os.kill(guest.pid, signal.SIGCONT) + guest.wait(60) + stalled = times() + detail = {'paused': paused, 'after_stall': stalled} + assert paused[0] == paused[1] and 1000 <= paused[0] < 1010, f'the clock while paused: {detail}' + # The two reads are a tick or two apart. + assert abs(stalled[0] - stalled[1]) <= 3, f'the guest\'s clock after its game stalled: {detail}' + return detail + + +def objects_near(game, centre, key='objects'): + """`status`'s objects within 8 blocks of centre, without their ids (each side numbers its own).""" + out = [] + for o in game('status')[key]: + if all(abs(o['position'][i] - centre[i]) <= 8 for i in (0, 2)): + o = dict(o) + o.pop('id') + o['position'] = [round(v, 2) for v in o['position']] + if 'yaw' in o: # as the packets carry it, in 256ths of a turn + o['yaw'] = round(o['yaw'] * 256 / 360) % 256 + out.append(o) + return sorted(out, key=json.dumps) + + +def check_objects(host, guest, out): + x, y, z = (int(v // 1) for v in pos_of(host)) + commands = [ + f'fill {x - 6} {y} {z - 6} {x + 6} {y + 3} {z - 6} minecraft:stone', + f'summon minecraft:minecart {x - 4.5} {y} {z + 3.5}', + f'summon minecraft:chest_minecart {x - 2.5} {y} {z + 3.5}', + f'summon minecraft:furnace_minecart {x + 1.5} {y} {z + 3.5}', + f'summon minecraft:tnt_minecart {x + 4.5} {y} {z + 3.5}', + f'summon minecraft:oak_boat {x - 4.5} {y} {z + 6.5} {{Rotation:[45f,0f]}}', + f'summon minecraft:birch_chest_boat {x - 1.5} {y} {z + 6.5}', + f'summon minecraft:bamboo_raft {x + 2.5} {y} {z + 6.5}', + f'summon minecraft:armor_stand {x - 2.5} {y} {z - 2.5} {{Rotation:[90f,0f]}}', + f'summon minecraft:armor_stand {x - 4.5} {y} {z - 2.5} {{ShowArms:1b,Small:1b,NoBasePlate:1b,' + 'equipment:{head:{id:"minecraft:diamond_helmet",count:1}}}', + f'summon minecraft:item_frame {x - 3.5} {y + 1} {z - 4.97} {{block_pos:[I;{x - 4},{y + 1},{z - 5}],Facing:3b,' + 'Item:{id:"minecraft:diamond",count:1},ItemRotation:3b}', + f'summon minecraft:glow_item_frame {x - 1.5} {y + 1} {z - 4.97} {{block_pos:[I;{x - 2},{y + 1},{z - 5}],Facing:3b}}', + f'summon minecraft:painting {x + 1.5} {y + 1} {z - 4.97} {{block_pos:[I;{x + 1},{y + 1},{z - 5}],facing:0b,' + 'variant:"minecraft:kebab"}', + f'summon minecraft:painting {x + 4} {y + 1} {z - 4.97} {{block_pos:[I;{x + 4},{y + 1},{z - 5}],facing:0b,' + 'variant:"minecraft:courbet"}', + ] + try: + for command in commands: + host('command', command=command) + host.wait(60) + guest.raw('look-at', x, y, z - 5) + guest.wait(10) + guest.shot(out, 'objects') + host.shot(out, 'objects') + mine, theirs = objects_near(host, (x, y, z)), objects_near(guest, (x, y, z)) + finally: + host('command', command=f'kill @e[type=!minecraft:player,x={x - 8},y={y - 2},z={z - 8},dx=16,dy=8,dz=16]') + host('command', command=f'fill {x - 6} {y} {z - 6} {x + 6} {y + 3} {z - 6} minecraft:air') + assert len(mine) == len(commands) - 1, f'the host has {len(mine)} objects: {mine}' + assert theirs == mine, f'the guest sees {theirs}\nthe host has {mine}' + return f'{len(theirs)} objects identical' + + +def check_projectiles(host, guest, out): + x, y, z = (int(v // 1) for v in pos_of(host)) + commands = [ + f'summon minecraft:tnt {x + 4.5} {y} {z + 4.5} {{fuse:2000s}}', + f'summon minecraft:end_crystal {x - 4.5} {y} {z + 4.5} {{ShowBottom:0b}}', + f'summon minecraft:tnt {x + 2.5} {y + 5} {z - 3.5} {{fuse:2000s}}', + f'summon minecraft:arrow {x - 2.5} {y + 4} {z - 2.5} {{Motion:[0.2d,0d,0d]}}', + f'summon minecraft:falling_block {x - 0.5} {y + 6} {z - 4.5} {{BlockState:{{Name:"minecraft:sand"}}}}', + ] + try: + for command in commands: + host('command', command=command) + host.wait(80) + guest.raw('look-at', x, y, z) + guest.wait(5) + guest.shot(out, 'projectiles') + mine = objects_near(host, (x, y, z), 'projectiles') + theirs = objects_near(guest, (x, y, z), 'projectiles') + finally: + host('command', command=f'kill @e[type=!minecraft:player,x={x - 8},y={y - 2},z={z - 8},dx=16,dy=10,dz=16]') + host('command', command=f'setblock {x - 1} {y} {z - 5} minecraft:air') + assert len(mine) == len(commands) - 1, f'the host has {len(mine)}: {mine}' + # The fuse counts down every tick: the guest's, read a moment later, may be a tick or two behind. + fuses = [(m.pop('fuse', 0), t.pop('fuse', 0)) for m, t in zip(mine, theirs)] + assert theirs == mine, f'the guest sees {theirs}\nthe host has {mine}' + assert all(abs(a - b) <= 2 for a, b in fuses), f'fuses (host, guest): {fuses}' + return f'{len(theirs)} identical, fuses (host, guest) {fuses}' + + +def light_differences(host, guest, lo, hi): + """Positions of the box loaded on both sides, and those whose sky or block light differ.""" + h, g = host.raw('light', *lo, *hi), guest.raw('light', *lo, *hi) + both = [i for i in range(len(h['sky'])) if h['sky'][i] != '.' and g['sky'][i] != '.'] + differ = [i for i in both if (h['sky'][i], h['block'][i]) != (g['sky'][i], g['block'][i])] + return h, g, both, differ + + +def same_light(host, guest, lo, hi, what, ticks=100): + """Wait until the guest's light over the box is the host's; the number of positions compared.""" + for _ in range(ticks // 10): + h, g, both, differ = light_differences(host, guest, lo, hi) + if not differ: + assert both, f'{what}: no position of the box is loaded on both sides' + return len(both) + host.wait(10) + i = differ[0] + nx, nz = hi[0] - lo[0] + 1, hi[2] - lo[2] + 1 + pos = (lo[0] + i % nx, lo[1] + i // (nx * nz), lo[2] + i // nx % nz) + raise AssertionError(f'{what}: {len(differ)} of {len(both)} positions differ, first {pos}: host sky ' + f'{h["sky"][i]} block {h["block"][i]}, guest sky {g["sky"][i]} block {g["block"][i]}') + + +def check_growth(host, guest, out): + """Block ticks near a guest 1000 blocks from the host: only the guest's tickets tick them.""" + x0, y0, z0 = pos_of(guest) + guest('command', command='gamemode creative') + guest('command', command=f'tp @s {x0 + 1000:.2f} 200 {z0:.2f}') + guest.wait(100) + x, y, z = (int(v // 1) for v in pos_of(guest)) + # A platform in the air over wherever the guest landed: a water source two blocks east of the + # guest, wheat on farmland two blocks west. Vanilla's water spreads only towards the nearest + # drop (FlowingFluid.getSpread), so any of the source's four neighbours may take it. Wheat + # grows only in daylight (raw brightness 9 or more). + top = max(y + 12, 120) + water = (x + 2, top + 1, z) + beside = [(water[0] + dx, water[1], water[2] + dz) for dx, dz in ((1, 0), (-1, 0), (0, 1), (0, -1))] + wheat = (x - 2, top + 1, z) + flowed = [] + try: + host('command', command='time set noon') + guest('command', command=f'fill {x - 4} {top} {z - 2} {x + 4} {top} {z + 2} minecraft:stone') + guest('command', command=f'setblock {wheat[0]} {top} {wheat[2]} minecraft:farmland[moisture=7]') + guest('command', command=f'setblock {wheat[0]} {wheat[1]} {wheat[2]} minecraft:wheat[age=0]') + guest('command', command='gamerule random_tick_speed 1000') + guest('command', command=f'setblock {water[0]} {water[1]} {water[2]} minecraft:water') + for _ in range(40): + guest.wait(10) + flowed = [p for p in beside if guest.raw('block', *p).get('block') == 'minecraft:water'] + if flowed: + break + age = wait_state(guest, wheat, 'age', '7', 2400) + guest.shot(out, 'growth') + finally: + guest('command', command='gamerule random_tick_speed 3') + host('command', command='time set day') + guest('command', command=f'tp @s {x0:.2f} {y0:.2f} {z0:.2f}') + guest('command', command='gamemode survival') + guest.wait(40) + detail = {'flowed': flowed, 'wheat age': age} + assert flowed, f'the water beside the guest did not flow: {detail}' + assert age == '7', f'the wheat beside the guest did not grow: {detail}' + return detail + + +def check_light(host, guest, out): + gx, gy, gz = (int(v // 1) for v in pos_of(guest)) + results = {} + # A torch near the guest: the guest lights it itself, as vanilla's client does. + torch = (gx + 3, gy, gz + 3) + host('command', command=f'setblock {torch[0]} {torch[1]} {torch[2]} minecraft:torch') + try: + results['torch'] = same_light(host, guest, (gx - 12, gy - 1, gz - 12), (gx + 16, gy + 3, gz + 16), 'torch') + finally: + host('command', command=f'setblock {torch[0]} {torch[1]} {torch[2]} minecraft:air') + # A roof over the guest, then a hole in it: sky light falls through. + roof = f'{gx - 3} {gy + 4} {gz - 3} {gx + 3} {gy + 4} {gz + 3}' + box = (gx - 6, gy - 1, gz - 6), (gx + 6, gy + 5, gz + 6) + host('command', command=f'fill {roof} minecraft:stone') + try: + results['roof'] = same_light(host, guest, *box, 'roof') + host('command', command=f'setblock {gx} {gy + 4} {gz} minecraft:air') + results['hole'] = same_light(host, guest, *box, 'hole in the roof') + finally: + host('command', command=f'fill {roof} minecraft:air') + # The last chunk the guest tracks eastwards, and glowstone high up just past it in a chunk it + # doesn't track: the guest never sees that block, only the light updates of its border chunk. + y = 200 + row = guest.raw('light', gx, y, gz, gx + 16 * 24, y, gz)['sky'] + assert '.' in row, 'the guest has every chunk 24 chunks east' + untracked = (gx + row.index('.')) >> 4 + near = untracked * 16 + 1 + # On its server thread the host's own game is a client too, sent the same light updates. + if not SERVER_THREAD: + assert host.raw('light', near, y, gz, near, y, gz)['sky'] != '.', 'the host lacks the chunk past the guest' + host('command', command=f'setblock {near} {y} {gz} minecraft:glowstone') + try: + lo, hi = (untracked * 16 - 16, y - 3, gz - 8), (untracked * 16 - 1, y + 3, gz + 8) + # On the server thread the host's game is a client too, as dark as the guest until the light + # update arrives: wait for it before comparing them. + for _ in range(10): + lit = guest.raw('light', near - 2, y, gz, near - 2, y, gz)['block'] + if lit == 'd': + break + host.wait(10) + assert lit == 'd', f'the border chunk by the glowstone has block light {lit}, not 13, after 100 ticks' + results['border'] = same_light(host, guest, lo, hi, 'border chunk') + finally: + host('command', command=f'setblock {near} {y} {gz} minecraft:air') + results['border_cleared'] = same_light(host, guest, lo, hi, 'border chunk without the glowstone') + return results + + CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'nametags': check_nametags, - 'chest': check_chest, 'crafting': check_crafting, 'damage': check_damage, + 'chest': check_chest, 'crafting': check_crafting, 'sign': check_sign, 'damage': check_damage, 'mobs': check_mobs, 'hazards': check_hazards, 'pvp': check_pvp, - 'containers': check_containers, 'feedback': check_feedback, 'trials': check_trials, - 'walls': check_walls} + 'respawn': check_respawn, + 'containers': check_containers, 'feedback': check_feedback, 'titles': check_titles, 'chat': check_chat, + 'machines': check_machines, + 'brewing': check_brewing, + 'hurt': check_hurt, + 'commands': check_commands, + 'recipes': check_recipes, 'trials': check_trials, 'walls': check_walls, + 'effects': check_effects, 'portals': check_portals, 'distance': check_distance, 'clicks': check_clicks, + 'sleep': check_sleep, 'push': check_push, 'eat': check_eat, 'eating': check_eating, + 'xp': check_xp, 'objects': check_objects, 'projectiles': check_projectiles, + 'light': check_light, 'plates': check_plates, + 'swings': check_swings, + 'time': check_time, 'growth': check_growth} def run(ctl, out, resources, checks): host, guest = Game('host', ctl, resources), Game('guest', ctl, resources) + if SERVER_THREAD: + host.d.env['HORIZON_SERVER_THREAD'] = '1' results = {} try: host('launch', wait=False, offscreen=sys.platform == 'darwin') @@ -647,7 +1670,7 @@ def run(ctl, out, resources, checks): host('command', command='gamerule spawn_mobs false') port = host.raw('lan', 'open', 0, 'commands')['port'] host.player = host.raw('lan', 'status').get('host', 'Player') # the host's profile name - guest('launch', wait=False, offscreen=sys.platform == 'darwin') + guest.pid = guest('launch', wait=False, offscreen=sys.platform == 'darwin')['pid'] guest.raw('connect', f'127.0.0.1:{port}', GUEST_NAME) guest.raw('wait-for', 'loaded', '--timeout', 300) guest.wait(40) @@ -676,7 +1699,10 @@ def main(): parser.add_argument('--bin-dir', help='prebuilt horizon-client and horizon-ctl (default: build with the fast profile)') parser.add_argument('--check', action='append', choices=CHECKS, help='run only these checks (repeatable)') parser.add_argument('--out', default=str(ROOT / '.local/lan-e2e'), help='screenshots and game logs go here') + parser.add_argument('--server-thread', action='store_true', help="the host's world runs on its server thread") args = parser.parse_args() + global SERVER_THREAD + SERVER_THREAD = args.server_thread if args.bin_dir: bins = Path(args.bin_dir).resolve() else: diff --git a/tools/parity.py b/tools/parity.py index 5809f913e..6ba4c0d90 100644 --- a/tools/parity.py +++ b/tools/parity.py @@ -36,8 +36,9 @@ import argparse import hashlib import json import os -from pathlib import Path +from pathlib import Path, PurePosixPath import re +import socket import subprocess import sys import time @@ -61,14 +62,14 @@ SHIM = STATE_DIR / "shim" # so a harness whose inputs match one that passed elsewhere doesn't run again. SHARED = Path(os.environ.get("PARITY_SHARED") or (LOCAL / "minecraft").resolve().parent / "parity-shared") MACHINE_JOBS = int(os.environ.get("PARITY_MACHINE_JOBS") or max(1, (os.cpu_count() or 4) // 2)) -HOST = os.uname().nodename.split(".")[0] +HOST = socket.gethostname().split(".")[0] DEFAULT_MEMORY = 3 << 30 # assumed peak for a harness that never ran here def harness_settings(path): """PARITY_SHARDS and PARITY_RUST_SCOPE, read from a harness's source without running it.""" import ast - text = Path(path).read_text() + text = Path(path).read_text(encoding="utf-8") settings = {} if "PARITY_" in text: for node in ast.parse(text).body: @@ -102,7 +103,7 @@ def base_name(name): def load_state(): - return json.loads(STATE.read_text()) if STATE.exists() else {"harnesses": {}, "hashes": {}} + return json.loads(STATE.read_text(encoding="utf-8")) if STATE.exists() else {"harnesses": {}, "hashes": {}} def save_state(state): @@ -129,7 +130,7 @@ class Hasher: if cached and cached[0] == stamp: return cached[1] if keep: - digest = hashlib.sha256(crate_root_without_modules(Path(real).read_text(), set(keep.split(","))).encode()).hexdigest() + digest = hashlib.sha256(crate_root_without_modules(Path(real).read_text(encoding="utf-8"), set(keep.split(","))).encode()).hexdigest() else: with open(real, "rb") as stream: digest = hashlib.file_digest(stream, "sha256").hexdigest() @@ -168,7 +169,7 @@ def environment_key(hasher): return (result.stdout + result.stderr).strip() except FileNotFoundError: return "missing" - lock = json.loads((ROOT / "research-lock.json").read_text()) + lock = json.loads((ROOT / "research-lock.json").read_text(encoding="utf-8")) version_dir = LOCAL / "minecraft" / lock["minecraft"]["version"] parts = [version(["rustc", "-V"]), version(["java", "-version"]), version(["javac", "-version"]), json.dumps(PROFILE, sort_keys=True), hasher(ROOT / "research-lock.json"), hasher(ROOT / "Cargo.lock"), @@ -197,7 +198,7 @@ def dep_info(binary): if not info.exists(): return [] sources = [] - for line in info.read_text().splitlines(): + for line in info.read_text(encoding="utf-8").splitlines(): _, separator, rest = line.partition(": ") if separator: sources += [part.replace("\\ ", " ") for part in re.split(r"(?<!\\) ", rest.strip()) if part] @@ -371,17 +372,17 @@ def narrow_rust_sources(sources, scope): for candidate in (crate_dir / "src" / Path(*parts[:count - 1]) / f"{parts[count - 1]}.rs", crate_dir / "src" / Path(*parts[:count]) / "mod.rs"): if candidate.is_file(): - return str(candidate.relative_to(ROOT)), count + return candidate.relative_to(ROOT).as_posix(), count return None, 0 def module_path(path): - parts = list(Path(*Path(path).parts[3:]).with_suffix("").parts) # crates/<crate>/src/... + parts = list(PurePosixPath(path).parts[3:-1] + (PurePosixPath(path).with_suffix("").name,)) return parts[:-1] if parts[-1] in ("mod", "lib") else parts defined_types, used_macros, kept_modules = set(), set(), {} for path in inner + entry: - text = rust_code((ROOT / path).read_text(errors="replace")) - crate_name = Path(path).parts[1].replace("-", "_") + text = rust_code((ROOT / path).read_text(encoding="utf-8", errors="replace")) + crate_name = PurePosixPath(path).parts[1].replace("-", "_") nesting = inline_modules(text) if re.search(r"\binclude!\s*\(", text): return sources, f"{path} includes source" @@ -401,7 +402,7 @@ def narrow_rust_sources(sources, scope): if head in crates: # a workspace crate base, crate_name = [], head elif head.startswith(("crate", "self", "super")) and path in inner: - own, crate_name = module_path(path) + nesting(match.start()), Path(path).parts[1].replace("-", "_") + own, crate_name = module_path(path) + nesting(match.start()), PurePosixPath(path).parts[1].replace("-", "_") ups = head.count("super") if head != "crate" and ups <= len(nesting(match.start())): continue # within this file @@ -415,14 +416,14 @@ def narrow_rust_sources(sources, scope): item = (base + parts)[0] defined_in_root = root.is_file() and re.search( rf"^\s*(?:pub(?:\([^)]*\))?\s+)?(?:const|static|fn|struct|enum|type|trait|union)\s+{item}\b", - root.read_text(), re.MULTILINE) + root.read_text(encoding="utf-8"), re.MULTILINE) if not defined_in_root: return sources, f"{path} names {head}::{rest}, which the crate root doesn't define itself" continue if not scoped(target): return sources, f"{path} names {head}::{rest} ({target})" for path in outside: - text = rust_code((ROOT / path).read_text(errors="replace")) + text = rust_code((ROOT / path).read_text(encoding="utf-8", errors="replace")) defined = set(RUST_MACRO_DEFINITION.findall(text)) & used_macros if defined: return sources, f"the scope uses macro {sorted(defined)[0]}! defined in {path}" @@ -433,7 +434,7 @@ def narrow_rust_sources(sources, scope): kept = [path for path in sources if path not in outside and not path.endswith("/src/lib.rs")] for path in sources: if path.endswith("/src/lib.rs"): - modules = kept_modules.get(Path(path).parts[1].replace("-", "_")) or {"-"} # "-": no module of it is reachable + modules = kept_modules.get(PurePosixPath(path).parts[1].replace("-", "_")) or {"-"} # "-": no module of it is reachable kept.append(f"{path}{KEEP}{','.join(sorted(modules))}") return sorted(kept), None @@ -549,7 +550,7 @@ def clear_large_outputs(name, trace): everything else large under the harness's .local directories is rebuilt on its next run.""" local = LOCAL.resolve() - keep = {os.path.realpath(line) for line in outputs_listing(name).read_text().splitlines() if line} + keep = {os.path.realpath(line) for line in outputs_listing(name).read_text(encoding="utf-8").splitlines() if line} directories = set() for command in trace["commands"]: for argument in command[1:]: @@ -600,7 +601,7 @@ def memory_available(): """Bytes the system can give harnesses now: MemAvailable on Linux, free and reclaimable pages on macOS.""" meminfo = Path("/proc/meminfo") if meminfo.exists(): - for line in meminfo.read_text().splitlines(): + for line in meminfo.read_text(encoding="utf-8").splitlines(): if line.startswith("MemAvailable:"): return int(line.split()[1]) * 1024 try: @@ -634,7 +635,7 @@ def outcome(name, script, passed, elapsed, trace_path, hasher=None): record = {"status": "pass" if passed else "fail", "seconds": round(elapsed, 1), "finished": time.strftime("%Y-%m-%dT%H:%M:%S"), "host": HOST} try: - trace = json.loads(Path(trace_path).read_text()) + trace = json.loads(Path(trace_path).read_text(encoding="utf-8")) except (OSError, ValueError): trace = None # killed before writing it, or the disk was full: the pass can't be keyed if trace: @@ -726,7 +727,7 @@ class LocalExecutor: def host_config(): """tools/parity_hosts.json: the machines harnesses can run on, and the harnesses that must stay local.""" path = ROOT / "tools" / "parity_hosts.json" - return json.loads(path.read_text()) if path.exists() else {"default": [], "hosts": {}, "local_only": {}} + return json.loads(path.read_text(encoding="utf-8")) if path.exists() else {"default": [], "hosts": {}, "local_only": {}} class RemoteExecutor: @@ -1017,11 +1018,11 @@ def stale(name, state, hasher, environment): candidates = [record] if record and record.get("dependencies") else [] template = SHARED / "dependencies" / f"{name}.json" if template.exists(): - candidates.append(json.loads(template.read_text())) + candidates.append(json.loads(template.read_text(encoding="utf-8"))) for candidate in candidates: shared = SHARED / "passes" / name / f"{key(candidate, hasher, environment)}.json" if shared.exists(): - state["harnesses"][name] = dict(json.loads(shared.read_text()), shared=True) + state["harnesses"][name] = dict(json.loads(shared.read_text(encoding="utf-8")), shared=True) return False return True @@ -1088,7 +1089,7 @@ def changed_harnesses(found, records, ref): for name, script in found.items(): specific = [path for path in records.get(name, {}).get("dependencies", []) if path.startswith("mods/horizon/src/test/") or "/examples/" in path or "/src/bin/" in path] - if str(Path(script).relative_to(ROOT)) in changed or any(path in changed for path in specific): + if Path(script).relative_to(ROOT).as_posix() in changed or any(path in changed for path in specific): found_changed.add(name) return found_changed @@ -1103,8 +1104,8 @@ def affected_harnesses(found, records, changed): dependencies = records.get(name, {}).get("dependencies") template = SHARED / "dependencies" / f"{name}.json" if dependencies is None and template.exists(): - dependencies = json.loads(template.read_text()).get("dependencies") - if dependencies is None or str(Path(script).relative_to(ROOT)) in changed or \ + dependencies = json.loads(template.read_text(encoding="utf-8")).get("dependencies") + if dependencies is None or Path(script).relative_to(ROOT).as_posix() in changed or \ any(path.split(KEEP)[0] in changed for path in dependencies): affected.add(name) return affected @@ -1180,7 +1181,7 @@ def main(): chosen = [name for name in found if records.get(name, {}).get("status") == "fail"] elif args.changed or args.changed_files: if args.changed_files: - changed, since = set(Path(args.changed_files).read_text().split("\n")) - {""}, "" + changed, since = set(Path(args.changed_files).read_text(encoding="utf-8").split("\n")) - {""}, "" else: changed, since = changed_files(args.changed), f" since the merge base with {args.changed}" if changed is None: diff --git a/tools/perf_bench.py b/tools/perf_bench.py index 013df41d6..6d0c5571d 100644 --- a/tools/perf_bench.py +++ b/tools/perf_bench.py @@ -44,6 +44,7 @@ import json import math import os import platform +import re import shutil import socket import statistics @@ -185,19 +186,35 @@ def summary(values): return {"n": n, "mean": rnd(mean), "stdev": sd, "min": rnd(min(values)), "max": rnd(max(values)), "ci95": ci} +def phys_footprint(pid): + """macOS: the bytes the kernel charges the process (`footprint`), without + the freed pages the allocator marked reusable, which `ps` still counts as + resident until memory runs short. None elsewhere.""" + if sys.platform != "darwin": + return None + out = subprocess.run(["footprint", "-f", "bytes", str(pid)], capture_output=True, text=True) + m = re.search(r"phys_footprint:\s*(\d+)", out.stdout) + return int(m.group(1)) if m else None + + class Sampler(threading.Thread): """Samples a process's resident memory every quarter second (ps, so the - same reading for every game and OS).""" + same reading for every game and OS) and, on macOS, its physical footprint + every two seconds.""" def __init__(self, pid): super().__init__(daemon=True) - self.pid, self.samples, self.stop = pid, [], threading.Event() + self.pid, self.samples, self.footprints, self.stop = pid, [], [], threading.Event() def run(self): + tick = 0 while not self.stop.is_set(): out = subprocess.run(["ps", "-o", "rss=", "-p", str(self.pid)], capture_output=True, text=True) if out.stdout.strip(): self.samples.append((time.time(), int(out.stdout.split()[0]) * 1024)) + if tick % 8 == 0 and (fp := phys_footprint(self.pid)) is not None: + self.footprints.append((time.time(), fp)) + tick += 1 self.stop.wait(0.25) def summary(self, windows): @@ -205,11 +222,16 @@ class Sampler(threading.Thread): if not self.samples: return {} during = [rss for t, rss in self.samples if any(a <= t <= b for a, b in windows)] - return { + out = { "rss_peak_mb": max(rss for _, rss in self.samples) / 2**20, "rss_steady_mb": statistics.median(during) / 2**20 if during else None, "rss_samples": len(self.samples), } + if self.footprints: + steady = [fp for t, fp in self.footprints if any(a <= t <= b for a, b in windows)] + out["footprint_peak_mb"] = max(fp for _, fp in self.footprints) / 2**20 + out["footprint_steady_mb"] = statistics.median(steady) / 2**20 if steady else None + return out def nvidia_memory_mb(pid): @@ -342,6 +364,20 @@ def run_horizon(args, distance): for carry in dev_mods(args): command += ["--dev-mod", str(carry)] env = dict(os.environ, HORIZON_NO_AUDIO="1") + if args.warm: + # A first start to the title screen builds what later starts reuse (the content-mod + # root, the block behaviour snapshot). It is not measured: players see it once. + with (WORK / f"horizon-rd{distance}-warm.log").open("w") as log: + proc = subprocess.Popen(command, cwd=game_dir, env=env, stdout=log, stderr=subprocess.STDOUT) + try: + poll(Control(sock), lambda s: s.get("screen") == "title", 600) + try: + Control(sock)("quit", timeout=60) + except (OSError, ValueError, RuntimeError): + pass + finally: + stop(proc) + sock.unlink(missing_ok=True) log = (WORK / f"horizon-rd{distance}.log").open("w") started = time.time() proc = subprocess.Popen(command, cwd=game_dir, env=env, stdout=log, stderr=subprocess.STDOUT) @@ -685,6 +721,8 @@ def report(args, distance, runs, app): metric(f"memory.{name}.gpu_mb", [s.get("gpu_mb") for s in scenes]) metric("memory.rss_peak_mb", [r["memory"].get("rss_peak_mb") for r in runs]) metric("memory.rss_steady_mb", [r["memory"].get("rss_steady_mb") for r in runs]) + metric("memory.footprint_peak_mb", [r["memory"].get("footprint_peak_mb") for r in runs]) + metric("memory.footprint_steady_mb", [r["memory"].get("footprint_steady_mb") for r in runs]) return {"format": REPORT_FORMAT, "version": REPORT_VERSION, "kind": "game", "created": datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"), "app": app, "hardware": hw, "conditions": conditions, "metrics": metrics, "frames": entries, @@ -711,7 +749,7 @@ def table(labels): cells.append(" / ".join(cell(m.get(f"fps.{scene}.{k}")) for k in ("avg_fps", "low_1pct_fps", "low_01pct_fps"))) print(f"| {d} | {scene} | " + " | ".join(cells) + " |") print() - print("| RD | " + " | ".join(f"{l}: title / world s, mean scene load s, RSS steady / peak MB, load avg" for l in labels) + " |") + print("| RD | " + " | ".join(f"{l}: title / world s, mean scene load s, RSS steady / peak MB, footprint steady / peak MB, load avg" for l in labels) + " |") print("| --- |" + " --- |" * len(labels)) for d in distances: cells = [] @@ -726,7 +764,9 @@ def table(labels): mb = lambda k: f"{m[k]['mean']:.0f}" if k in m else "–" cells.append(f"{sec('startup.title_ms')} / {sec('startup.world_ms')}, " f"{statistics.mean(loads) / 1000 if loads else 0:.1f}, " - f"{mb('memory.rss_steady_mb')} / {mb('memory.rss_peak_mb')}, {r['conditions'].get('load_avg_1m')}") + f"{mb('memory.rss_steady_mb')} / {mb('memory.rss_peak_mb')}, " + f"{mb('memory.footprint_steady_mb')} / {mb('memory.footprint_peak_mb')}, " + f"{r['conditions'].get('load_avg_1m')}") print(f"| {d} | " + " | ".join(cells) + " |") @@ -748,6 +788,8 @@ def main(): p.add_argument("--build", default=str(ROOT / "target/release")) p.add_argument("--repeats", type=int, default=3, help="games per render distance; the report gives means and 95%% CIs") p.add_argument("--port-work", help="horizon: also load the mods carried over in this horizon-port-pack work folder") + p.add_argument("--warm", action="store_true", + help="horizon: measure a later start, after one start to the title screen built the caches") p.add_argument("--mod-jars", help="neoforge: the mod jars to load (the user's own; never committed)") p.add_argument("--deps", help="neoforge: their required libraries") p.add_argument("--table", nargs="+", metavar="LABEL", help="print markdown tables of saved runs, and exit") diff --git a/tools/pr_ready.py b/tools/pr_ready.py index 980080284..9ec216fea 100755 --- a/tools/pr_ready.py +++ b/tools/pr_ready.py @@ -29,6 +29,10 @@ import subprocess import sys import time +if os.name == "nt": + sys.stdout.reconfigure(encoding="utf-8", errors="replace") + sys.stderr.reconfigure(encoding="utf-8", errors="replace") + ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) FLEET_SHARED = Path.home() / "code" / "horizon-parity" / "shared-local" / "parity-shared" @@ -184,7 +188,7 @@ def line_counts(base, changed): for path in git("ls-files", "--others", "--exclude-standard").splitlines(): if path in changed: try: - counts[path] = (len((ROOT / path).read_text().splitlines()), 0) + counts[path] = (len((ROOT / path).read_text(encoding="utf-8").splitlines()), 0) except (UnicodeDecodeError, OSError): pass return counts @@ -303,6 +307,9 @@ def run_script(steps, host, verbose): """Runs the steps in one shell on host (here for "local"), each between markers, reading results as they come.""" if not steps: return + if host == "local" and os.name == "nt": + run_windows(steps, verbose) + return parts = [HOST_SETUP.get(host, ""), "set +e"] for index, step in enumerate(steps): parts.append(f"echo '@@pr_ready:begin:{index}'; started=$(date +%s)\n( {step.command}\n) 2>&1 < /dev/null\n" @@ -338,6 +345,61 @@ def run_script(steps, host, verbose): finish(step, verbose) +def run_windows(steps, verbose): + """Run local checks directly on Windows without requiring a Unix shell.""" + for step in steps: + command = shlex.split(step.command) + cwd = ROOT + env = os.environ.copy() + env["PYTHONUTF8"] = "1" + if step.name == "Public tree": + command = [sys.executable, "tools/check_public_tree.py"] + elif step.name == "Python tests": + command = [sys.executable, "-m", "unittest", "discover", "-s", "tests"] + elif step.name == "rustfmt": + command = ["cargo", "fmt", "--all", "--", "--check"] + elif step.name == "clippy": + command = ["cargo", "clippy", "--workspace", "--all-targets", "--locked", "--", "-D", "warnings"] + elif step.name.startswith("tests"): + command = shlex.split(step.command) + elif step.name == "tokens.css up to date": + command = shlex.split(step.command) + elif step.name == "launcher rustfmt": + command = ["cargo", "fmt", "--check"] + cwd = ROOT / "launcher" + elif step.name == "launcher clippy": + command = ["cargo", "clippy", "--all-targets", "--locked", "--", "-D", "warnings"] + cwd = ROOT / "launcher" + elif step.name == "launcher tests": + command = ["cargo", "test", "--locked", "--lib"] + cwd = ROOT / "launcher" + elif step.name == "Parity": + command = shlex.split(step.command) + if command and command[0] == "PARITY_HOSTS=": + command.pop(0) + if command and command[0] == "python3": + command[0] = sys.executable + env["PARITY_HOSTS"] = "" + elif command and command[0] == "python3": + command[0] = sys.executable + + print(f" {step.name}...", file=sys.stderr, flush=True) + started = time.monotonic() + try: + with subprocess.Popen(command, cwd=cwd, env=env, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, text=True, errors="replace") as process: + for line in process.stdout: + step.output += line + if verbose: + print(" " + line, end="", file=sys.stderr) + status = process.wait() + step.status = "pass" if status == 0 else "fail" + except OSError as error: + step.status, step.detail = "fail", str(error) + step.seconds = int(time.monotonic() - started) + finish(step, verbose) + + def finish(step, verbose): if step.status == "pass" and step.summary: step.detail = step.summary(step.output) diff --git a/tools/profile_behaviour_cache.py b/tools/profile_behaviour_cache.py new file mode 100644 index 000000000..00ba65a6d --- /dev/null +++ b/tools/profile_behaviour_cache.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +"""Measure the resource-dependent Behaviours::load portion of client startup. + +Build the behaviour_load_profile example in the checkout to be measured first: + cargo build --release -p horizon-world --example behaviour_load_profile + +Run the profiler from the repository root with the matching example binary. +This measures behaviour loading, not time to first rendered frame. With +--cache-dir, the example reads and refreshes the resolved snapshot there, as the +client does with its .horizon-cache folder: the first load of a run builds it +when it is missing, the next loads restore it. +""" +import argparse +import json +import re +import statistics +import subprocess +from pathlib import Path + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--binary", type=Path, default=Path("target/release/examples/behaviour_load_profile")) + parser.add_argument("--resources", type=Path, default=Path(".local/horizon-client/26.3")) + parser.add_argument("--cache-dir", type=Path, + help="snapshot folder (default: no snapshot, every load is uncached)") + parser.add_argument("--repeats", type=int, default=5) + parser.add_argument("--output", type=Path) + args = parser.parse_args() + if args.repeats < 1: + parser.error("--repeats must be at least one") + samples = [] + for _ in range(args.repeats): + command = [str(args.binary.resolve()), str(args.resources.resolve())] + if args.cache_dir: + command.append(str(args.cache_dir.resolve())) + run = subprocess.run(command, text=True, capture_output=True, check=True) + values = {key: float(value) for key, value in + re.findall(r"^(registry_ms|behaviours_[0-2]_ms)=([\d.]+)$", run.stderr, re.M)} + if len(values) != 4: + raise RuntimeError(f"incomplete profile output: {run.stderr}") + samples.append(values) + summary = {key: {"median_ms": round(statistics.median(row[key] for row in samples), 3), + "samples_ms": [row[key] for row in samples]} + for key in samples[0]} + result = {"scope": "behaviour loader only; not full client startup", + "binary": str(args.binary), "resources": str(args.resources), "repeats": args.repeats, + "cache_dir": str(args.cache_dir) if args.cache_dir else None, + "metrics": summary} + print(json.dumps(result, indent=2)) + if args.output: + args.output.parent.mkdir(parents=True, exist_ok=True) + args.output.write_text(json.dumps(result, indent=2) + "\n") + + +if __name__ == "__main__": + main() diff --git a/tools/release_train.py b/tools/release_train.py index a1af28449..70e7b0287 100644 --- a/tools/release_train.py +++ b/tools/release_train.py @@ -39,7 +39,17 @@ EXPORTERS = "mods/horizon/src/test/java/dev/horizonlab/parity/" # What players run. Documentation (*.md), tests, benchmarks and harness probes (examples/) never count. GAME_PATHS = ["crates/", "xtask/", "packaging/", "Cargo.toml", "Cargo.lock", "research-lock.json"] -LAUNCHER_PATHS = ["launcher/", "crates/horizon-prepare/", "crates/horizon-theme/", "crates/horizon-telemetry/"] +# Every crate the launcher builds in (cargo metadata --manifest-path launcher/Cargo.toml: the path packages). +LAUNCHER_PATHS = [ + "launcher/", + "crates/horizon-bench/", + "crates/horizon-friends/", + "crates/horizon-mods/", + "crates/horizon-port-pack/", + "crates/horizon-prepare/", + "crates/horizon-telemetry/", + "crates/horizon-theme/", +] LAUNCHER_IGNORED = ["launcher/links.json", "launcher/docs/", "launcher/tests/"] FOOTER = { diff --git a/tools/server.py b/tools/server.py deleted file mode 100644 index 82308289e..000000000 --- a/tools/server.py +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/env python3 -"""Prepare/run a vanilla-rules Fabric server with pinned memory and simulation optimizers.""" - -import argparse -import json -import os -from pathlib import Path -import re -import shutil -import subprocess - -try: - from . import lab, runtime -except ImportError: - import lab - import runtime - -ROOT = lab.ROOT - - -def prepare(instance, baseline=False, server_jar=None): - lock, directory = lab.config() - launcher = lab.import_server(lock, directory, server_jar) - server, libraries = lab.unpack_server(launcher, directory, lock["minecraft"]["version"]) - libraries = [server, *libraries, *runtime.fabric_libraries()] - pack_lock = json.loads((ROOT / "packs/mods.lock.json").read_text()) - mods = pack_lock["projects"] - selection = ["fabric-api"] if baseline else ["fabric-api", "lithium", "ferrite-core"] - mod_dir = instance / "mods" - mod_dir.mkdir(parents=True, exist_ok=True) - for name in selection: - entry = mods[name] - source = runtime.fetch(entry["url"], ROOT / ".local/pack-cache" / entry["filename"], - entry["hashes"]["sha512"], "sha512") - shutil.copyfile(source, mod_dir / entry["filename"]) - original = ROOT / "dist" / f"horizon-{pack_lock['pack_version']}.jar" - if not original.exists(): - raise FileNotFoundError("Build the original mod first with tools/build_mod.py, or use the server starter ZIP") - runtime.install_original_mod(original, mod_dir) - return libraries - - -def main(): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("command", choices=["prepare", "run"]) - parser.add_argument("--instance", type=Path, default=ROOT / ".local/server/optimized") - parser.add_argument("--heap", default="4G") - parser.add_argument("--server-jar", type=Path, help="the pinned server jar you downloaded yourself") - parser.add_argument("--baseline", action="store_true", help="Use a separate instance for the baseline") - parser.add_argument("--compact-headers", action="store_true", help="Enable Java 25 compact object headers for this server process") - parser.add_argument("--gc", choices=["default", "g1", "zgc"], default="default", help="Select a collector for this process; default leaves JVM policy in use") - args = parser.parse_args() - if not re.fullmatch(r"[1-9][0-9]*[MGmg]", args.heap): - parser.error("--heap must look like 4G") - instance = args.instance.resolve() - if args.baseline and args.instance == parser.get_default("instance"): - instance = ROOT / ".local/server/baseline" - libraries = prepare(instance, args.baseline, args.server_jar) - if args.command == "prepare": - print(f"Prepared server at {instance}. Read https://www.minecraft.net/eula before setting eula=true locally.") - return - eula = instance / "eula.txt" - if not eula.exists() or not re.search(r"(?m)^\s*eula\s*=\s*true\s*$", eula.read_text()): - if not eula.exists(): - eula.write_text("# Read https://www.minecraft.net/eula then change this value yourself.\neula=false\n") - print(f"Server prepared. Read Minecraft's EULA and set eula=true in {eula} before running.") - return - subprocess.run(["java", *runtime.jvm_options(args.heap, args.compact_headers, args.gc), - "--enable-native-access=ALL-UNNAMED", - "-cp", os.pathsep.join(map(str, libraries)), "net.fabricmc.loader.impl.launch.knot.KnotServer", - "--nogui"], cwd=instance, check=True) - - -if __name__ == "__main__": - main() diff --git a/tools/server_bench.py b/tools/server_bench.py index 5a64c80cf..ebd2a1a99 100755 --- a/tools/server_bench.py +++ b/tools/server_bench.py @@ -8,14 +8,33 @@ then measures for a while: - MSPT (average, P50, P95, P99) from each server's own `/tick query`, asked every 5 seconds on the console: vanilla's ServerTickRateManager numbers, which Horizon reproduces, so both measure themselves the same way; - RSS and CPU of the server's whole process tree, once a second; -- chunks the bots receive per second (new terrain while the `explore` bots fly outwards); +- chunks the bots receive per second (new terrain while the `explore` bots fly outwards), chunks sent again + while a bot still held them, and the moves the server refused (pull-backs; "moved too quickly" and + "moved wrongly" lines); - the start time to `Done`, and every bot that failed to join or was dropped. - python3 tools/server_bench.py --servers horizon,vanilla --bots 1,10,25 --seconds 120 + python3 tools/server_bench.py --servers horizon,vanilla --bots 1,10,25 --behaviour walk,explore + +The bots can run on another machine, so they don't take the server's CPU: `--bots-via 'ssh macmini'` with +`--bots-bin` naming horizon-bots there and `--connect` the address it reaches this machine at. `--nice 19` +runs every server at the lowest priority (a shared machine). The explore bots fly as fast as each server +sends them chunks, so a faster server gets more terrain to generate; `--explore-speed` slows them to a rate +both servers keep up with, for equal work. After the runs, the work table compares the servers at each bot +count: chunks sent and new chunks generated a second, against the busiest server's. A run whose work is more +than 10 % apart isn't a fair comparison of its tick times. The vanilla side runs the pinned server jar from tools/lab.py prepare (your own copy). It needs Java 25. The Horizon side runs target/release/horizon-server with the engine beside it; build both first: cargo build --release -p horizon-client --bin horizon-client -p horizon-server -p horizon-bots + +Two more Java servers, both dev-only downloads into .local/server-bench/jars (never committed or shipped): +- `paper`: Paper's newest build for the pinned version, from PaperMC's download API, checked against the + SHA-256 it publishes, with Paper's own defaults except `connection-throttle` (off: the bots all join from + one address). Paperclip patches the pinned server jar (your copy, put in + its cache so nothing is fetched from Mojang). +- `fabric`: the pinned vanilla server on the pinned Fabric loader (packs/fabric.lock.json) with Lithium from + packs/mods.lock.json (and Fabric API only if Lithium asks for it), checked against the lock's SHA-512. +Both run with the same heap as vanilla (`--heap`). """ import argparse @@ -32,11 +51,14 @@ import subprocess import sys import threading import time +import urllib.request +import zipfile try: - from . import lab, test_rust_parity as base + from . import lab, runtime, test_rust_parity as base except ImportError: import lab + import runtime import test_rust_parity as base ROOT = lab.ROOT @@ -149,10 +171,45 @@ class Server: self.log.close() +# The line every server logs once it is up: vanilla, Fabric and Horizon `[time] [Server thread/INFO]: Done (`, +# Paper `[time INFO]: Done (`. +DONE = r'INFO\]: Done \(' TICK_AVERAGE = re.compile(r'Average time per tick: ([0-9.]+) ?ms') TICK_PERCENTILES = re.compile(r'P50: ([0-9.]+) ?ms P95: ([0-9.]+) ?ms P99: ([0-9.]+) ?ms') +# The newest Paper build for the pinned version, from PaperMC's download API (dev-only, never committed). +PAPER_API = 'https://fill.papermc.io/v3/projects/paper/versions/{version}/builds/latest' +JARS = lab.LOCAL / 'server-bench/jars' + + +def paper_jar(): + """Paper's newest build for the pinned version, downloaded once and checked against its SHA-256.""" + lock, _ = lab.config() + version = lock['minecraft']['version'] + request = urllib.request.Request(PAPER_API.format(version=version), + headers={'User-Agent': 'horizon-server-bench (github.com/ludovic111/horizon)'}) + with urllib.request.urlopen(request, timeout=30) as response: + build = json.load(response) + download = build['downloads']['server:default'] + jar = runtime.fetch(download['url'], JARS / download['name'], download['checksums']['sha256'], 'sha256') + return jar, build['id'] + + +def fabric_mods(): + """Lithium from packs/mods.lock.json, plus Fabric API if Lithium depends on it (checked, cached).""" + lock = json.loads((ROOT / 'packs/mods.lock.json').read_text()) + def fetch(name): + project = lock['projects'][name] + return runtime.fetch(project['url'], JARS / project['filename'], project['hashes']['sha512'], 'sha512') + jars = [fetch('lithium')] + with zipfile.ZipFile(jars[0]) as archive: + depends = json.loads(archive.read('fabric.mod.json')).get('depends', {}) + if any(d == 'fabric-api' or d.startswith('fabric-') and d != 'fabricloader' for d in depends): + jars.append(fetch('fabric-api')) + return jars, {'fabric_loader': lock['fabric_loader'], 'lithium': lock['projects']['lithium']['version']} + + def tick_query(server): """Asks `tick query` and reads the average and percentiles it prints (both servers print the same).""" start = server.count() @@ -172,11 +229,38 @@ def tick_query(server): def server_command(kind, directory, args): + java = ['java', f'-Xms{args.heap}', f'-Xmx{args.heap}', '-Duser.timezone=UTC'] if kind == 'vanilla': _, jars, libraries = base.verify_artifacts() classpath = os.pathsep.join(map(str, [jars['server'], *libraries])) - return ['java', f'-Xms{args.heap}', f'-Xmx{args.heap}', '-Duser.timezone=UTC', '-cp', classpath, - 'net.minecraft.server.Main', 'nogui'] + return [*java, '-cp', classpath, 'net.minecraft.server.Main', 'nogui'] + if kind == 'paper': + lock, jars, _ = base.verify_artifacts() + jar, _ = paper_jar() + # Paperclip's cache, libraries and patched jar are shared by every run; the pinned server jar (your + # copy) seeds its cache, so it never downloads Mojang's. + shared = lab.LOCAL / 'server-bench/paper-shared' + cache = shared / 'cache' + cache.mkdir(parents=True, exist_ok=True) + mojang = cache / f'mojang_{lock["minecraft"]["version"]}.jar' + if not mojang.exists(): + shutil.copyfile(jars['server'].parent / 'server.jar', mojang) + for name in ('cache', 'libraries', 'versions'): + (shared / name).mkdir(exist_ok=True) + (directory / name).symlink_to(shared / name, target_is_directory=True) + # Paper refuses a second login from one address within 4 s (bukkit.yml's connection-throttle), and + # every bot comes from the same one. Off, as on vanilla; Paper fills in the rest of the file. + (directory / 'bukkit.yml').write_text('settings:\n connection-throttle: -1\n') + return [*java, '-jar', jar, '--nogui'] + if kind == 'fabric': + _, jars, libraries = base.verify_artifacts() + mods = directory / 'mods' + mods.mkdir() + for jar in fabric_mods()[0]: + shutil.copyfile(jar, mods / jar.name) + classpath = os.pathsep.join(map(str, [jars['server'], *libraries, *runtime.fabric_libraries()])) + return [*java, '-Dfabric.log.disableAnsi=true', '-cp', classpath, + 'net.fabricmc.loader.impl.launch.knot.KnotServer', 'nogui'] server = BIN / f'horizon-server{EXE}' subprocess.run([server, '--dir', directory, 'import', directory / 'server.properties'], check=True, capture_output=True) @@ -186,8 +270,16 @@ def server_command(kind, directory, args): return [server, '--dir', directory, 'run'] -def run_once(kind, bots, args, out): - directory = out / f'{kind}-{bots}' +def bots_command(args): + """horizon-bots here, or through `--bots-via` (for example `ssh macmini`) on another machine.""" + if args.bots_via: + return [*args.bots_via.split(), args.bots_bin or 'horizon-bots'] + return [args.bots_bin or BIN / f'horizon-bots{EXE}'] + + +def run_once(kind, bots, behaviour, args, out): + name = f'{kind}-{behaviour}-{bots}' + directory = out / name shutil.rmtree(directory, ignore_errors=True) directory.mkdir(parents=True) port = free_port() @@ -195,19 +287,27 @@ def run_once(kind, bots, args, out): (directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items())) (directory / 'eula.txt').write_text('eula=true\n') command = server_command(kind, directory, args) + if args.nice: + command = ['nice', '-n', str(args.nice), *command] started = time.time() - server = Server(command, directory, out / f'{kind}-{bots}.log') - result = {'server': kind, 'bots': bots} + server = Server(command, directory, out / f'{name}.log') + result = {'server': kind, 'bots': bots, 'behaviour': behaviour} try: - server.wait_for(r'/INFO\]: Done \(', 600) + server.wait_for(DONE, 600) result['start_seconds'] = round(time.time() - started, 2) + # Every bot appears on the world spawn itself, not spread around it, so both servers see the same paths. + server.console('gamerule respawn_radius 0') + for rule in filter(None, args.gamerule.split(',')): + server.console('gamerule ' + rule.replace('=', ' ')) time.sleep(5) - report = out / f'{kind}-{bots}-bots.json' + report = out / f'{name}-bots.json' total = bots * args.join_every + args.warmup + args.seconds + # The summary comes back on stdout after the per-second lines, so it works over ssh too. bot_process = subprocess.Popen( - [BIN / f'horizon-bots{EXE}', '--server', f'127.0.0.1:{port}', '--bots', str(bots), - '--behaviour', args.behaviour, '--join-every', str(args.join_every), '--seconds', str(int(total)), - '--seed', str(args.seed), '--report', report], + [*bots_command(args), '--server', f'{args.connect}:{port}', '--bots', str(bots), + '--behaviour', behaviour, '--join-every', str(args.join_every), '--seconds', str(int(total)), + '--seed', str(args.seed), '--report', '-', + *(['--explore-speed', str(args.explore_speed)] if args.explore_speed else [])], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) bot_lines = [] threading.Thread(target=lambda: bot_lines.extend(bot_process.stdout), daemon=True).start() @@ -229,11 +329,27 @@ def run_once(kind, bots, args, out): if (t := tick_query(server)) is not None: ticks.append(t) next_query = now + 5 - measured = [json.loads(l) for l in list(bot_lines) if l.startswith('{')] - window = [l for l in measured if l['t'] >= measured[-1]['t'] - args.seconds] if measured else [] bot_process.wait(timeout=total + 120) - summary = json.loads(report.read_text()) if report.exists() else {} + time.sleep(0.5) + measured, rest = [], [] + for line in list(bot_lines): + try: + sample = json.loads(line) + except ValueError: + sample = None + if isinstance(sample, dict) and 't' in sample: + measured.append(sample) + else: + rest.append(line) + window = [l for l in measured if l['t'] >= measured[-1]['t'] - args.seconds] if measured else [] + summary_text = ''.join(rest) + summary = json.loads(summary_text) if summary_text.strip() else {} + report.write_text(json.dumps(summary, indent=2) + '\n') mean = lambda xs: round(statistics.fmean(xs), 2) if xs else None + per_bot = summary.get('per_bot', []) + by_behaviour = {} + for b in per_bot: + by_behaviour.setdefault(b['behaviour'], []).append(b) result.update({ 'mspt': {k: mean([t[k] for t in ticks]) for k in ('average', 'p50', 'p95', 'p99')}, 'mspt_worst_p99': max((t['p99'] for t in ticks), default=None), @@ -242,6 +358,19 @@ def run_once(kind, bots, args, out): 'rss_mb': {'mean': mean([r / 1e6 for r in rss]), 'max': round(max(rss) / 1e6, 1) if rss else None}, 'cpu_percent': mean(cpu), 'chunks_per_second': mean([l['chunks_per_second'] for l in window]), + # Over the measured window: different chunks reaching the bots, chunks they had dropped and got + # again (walking back), and ticks the bots stood still waiting for their own chunk. + **({k + '_per_second': round((window[-1][k] - window[0][k]) / max(len(window) - 1, 1), 2) + for k in ('unique', 'forgotten', 'waited_ticks')} if len(window) > 1 else {}), + 'chunks_total': summary.get('chunks'), + # Per bot over the whole run: every chunk received, the different ones, and those sent again + # while the bot still held them. + 'chunks_per_bot': {k: mean([b['chunks'] for b in v]) for k, v in by_behaviour.items()}, + 'unique_chunks_per_bot': {k: mean([b['unique_chunks'] for b in v]) for k, v in by_behaviour.items()}, + 'resent': summary.get('resent'), + 'pulled_back': summary.get('pulled_back'), + 'moved_too_quickly': sum('moved too quickly!' in l for l in server.since(0)), + 'moved_wrongly': sum('moved wrongly!' in l for l in server.since(0)), 'bots_joined': summary.get('joined'), 'bots_failed': [b for b in summary.get('per_bot', []) if b.get('error')], 'tick_queries': len(ticks), @@ -253,47 +382,114 @@ def run_once(kind, bots, args, out): return result +# How far apart two servers' chunks a second (sent, and new) may be and still count as the same work: 10 %, +# or a chunk a second when there are few. +EQUAL_WORK = 0.10 + + +def work(runs): + """Each pass and bot count that ran on more than one server: every server's chunks sent and new chunks a + second as a share of the busiest one's, the ticks a second its bots stood waiting for their own chunk, + and whether the work was equal (within EQUAL_WORK on both).""" + groups = {} + for r in runs: + if 'error' not in r: + groups.setdefault((r['behaviour'], r['bots']), []).append(r) + rows = [] + for (behaviour, bots), group in groups.items(): + if len(group) < 2: + continue + row = {'behaviour': behaviour, 'bots': bots, 'servers': {}, 'equal': True} + for key in ('chunks_per_second', 'unique_per_second'): + top = max(r.get(key) or 0 for r in group) + for r in group: + value = r.get(key) or 0 + share = round(value / top, 3) if top else 1.0 + row['servers'].setdefault(r['server'], {})[key] = {'value': value, 'share': share} + if top - value > max(EQUAL_WORK * top, 1.0): + row['equal'] = False + for r in group: + row['servers'][r['server']]['waited_ticks_per_second'] = r.get('waited_ticks_per_second') + rows.append(row) + return rows + + +def versions(kinds): + """The Paper build and the Fabric loader and Lithium versions that ran, for the report.""" + out = {} + if 'paper' in kinds: + out['paper_build'] = paper_jar()[1] + if 'fabric' in kinds: + out.update(fabric_mods()[1]) + return out + + def main(): parser = argparse.ArgumentParser(description=__doc__.split('\n\n')[0]) - parser.add_argument('--servers', default='horizon,vanilla') + parser.add_argument('--servers', default='horizon,vanilla', help='horizon, vanilla, paper, fabric (Lithium)') parser.add_argument('--bots', default='1,10,25,50') - parser.add_argument('--behaviour', default='mixed') + parser.add_argument('--behaviour', default='walk,explore', help='one pass per behaviour (comma-separated)') parser.add_argument('--join-every', type=float, default=1.0) parser.add_argument('--warmup', type=int, default=60) parser.add_argument('--seconds', type=int, default=300) parser.add_argument('--seed', type=int, default=12345) - parser.add_argument('--heap', default='4g', help='the Java heap, -Xms and -Xmx (vanilla)') + parser.add_argument('--explore-speed', type=float, default=0, + help='blocks a second the explore bots fly (default: the bots\' 22, as fast as chunks ' + 'come); a speed both servers keep up with makes the explore pass equal work') + parser.add_argument('--gamerule', default='', + help="more game rules for both servers, 'name=value,...' (locator_bar=false: Horizon has " + "no locator bar yet)") + parser.add_argument('--heap', default='4g', help='the Java heap, -Xms and -Xmx (vanilla, Paper, Fabric)') parser.add_argument('--out', type=Path, default=None, help='the report (JSON)') + parser.add_argument('--nice', type=int, default=0, help='run the servers under nice -n N') + parser.add_argument('--bots-via', default='', help="a command prefix to run the bots elsewhere ('ssh host')") + parser.add_argument('--bots-bin', default='', help='horizon-bots (on the --bots-via machine)') + parser.add_argument('--connect', default='127.0.0.1', help='the address the bots reach the server at') args = parser.parse_args() out = lab.LOCAL / 'server-bench' out.mkdir(parents=True, exist_ok=True) runs = [] - for bots in [int(b) for b in args.bots.split(',')]: - for kind in args.servers.split(','): - print(f'{kind}, {bots} bots...', flush=True) - result = run_once(kind, bots, args, out) - runs.append(result) - print(json.dumps(result), flush=True) + for behaviour in args.behaviour.split(','): + for bots in [int(b) for b in args.bots.split(',')]: + for kind in args.servers.split(','): + print(f'{kind}, {bots} {behaviour} bots...', flush=True) + result = run_once(kind, bots, behaviour, args, out) + runs.append(result) + print(json.dumps(result), flush=True) report = { 'kind': 'horizon-server-benchmark', 'date': datetime.datetime.now(datetime.timezone.utc).isoformat(timespec='seconds'), 'machine': {'system': platform.system(), 'machine': platform.machine(), 'cpus': os.cpu_count()}, 'commit': subprocess.run(['git', 'rev-parse', 'HEAD'], cwd=ROOT, capture_output=True, text=True).stdout.strip(), 'java': subprocess.run(['java', '--version'], capture_output=True, text=True).stdout.splitlines()[:1], + 'versions': versions(args.servers.split(',')), 'settings': {**vars(args), 'out': str(args.out), 'properties': PROPERTIES}, 'runs': runs, + 'work': work(runs), } path = args.out or out / 'report.json' path.parent.mkdir(parents=True, exist_ok=True) path.write_text(json.dumps(report, indent=2) + '\n') - print(f'\n{"server":8} {"bots":>4} {"start s":>8} {"MSPT":>6} {"P95":>6} {"P99":>6} {"RSS MB":>7} {"CPU %":>6} {"chunks/s":>8} {"joined":>6}') + print(f'\n{"server":8} {"pass":8} {"bots":>4} {"start s":>8} {"MSPT":>6} {"P95":>6} {"P99":>6} {"RSS MB":>7} ' + f'{"CPU %":>6} {"chunks/s":>8} {"/bot":>6} {"resent":>6} {"pulled":>6} {"joined":>6}') for r in runs: if 'error' in r: - print(f'{r["server"]:8} {r["bots"]:>4} failed: {r["error"].splitlines()[0]}') + print(f'{r["server"]:8} {r["behaviour"]:8} {r["bots"]:>4} failed: {r["error"].splitlines()[0]}') continue - print(f'{r["server"]:8} {r["bots"]:>4} {r["start_seconds"]:>8} {r["mspt"]["average"]!s:>6} {r["mspt"]["p95"]!s:>6} ' - f'{r["mspt"]["p99"]!s:>6} {r["rss_mb"]["mean"]!s:>7} {r["cpu_percent"]!s:>6} {r["chunks_per_second"]!s:>8} ' + # A mixed pass: every bot's. + per_bot = r['chunks_per_bot'].get(r['behaviour']) or round((r['chunks_total'] or 0) / r['bots']) + print(f'{r["server"]:8} {r["behaviour"]:8} {r["bots"]:>4} {r["start_seconds"]:>8} {r["mspt"]["average"]!s:>6} ' + f'{r["mspt"]["p95"]!s:>6} {r["mspt"]["p99"]!s:>6} {r["rss_mb"]["mean"]!s:>7} {r["cpu_percent"]!s:>6} ' + f'{r["chunks_per_second"]!s:>8} {per_bot!s:>6} {r["resent"]!s:>6} {r["pulled_back"]!s:>6} ' f'{r["bots_joined"]!s:>6}') + if report['work']: + print(f'\nWork, against the busiest server (chunks a second sent, new; ticks a second bots waited):') + for row in report['work']: + cells = ' '.join( + f'{server} {w["chunks_per_second"]["value"]} ({w["chunks_per_second"]["share"]:.0%}), ' + f'{w["unique_per_second"]["value"]} ({w["unique_per_second"]["share"]:.0%}), ' + f'{w["waited_ticks_per_second"]}' for server, w in row['servers'].items()) + print(f'{row["behaviour"]:8} {row["bots"]:>4} {"equal" if row["equal"] else "NOT EQUAL"} {cells}') print(f'\nWrote {path}') diff --git a/tools/test_block_entity_tickers_parity.py b/tools/test_block_entity_tickers_parity.py new file mode 100644 index 000000000..459e7a926 --- /dev/null +++ b/tools/test_block_entity_tickers_parity.py @@ -0,0 +1,132 @@ +#!/usr/bin/env python3 +"""Compare the order block entities tick in with vanilla's `Level.blockEntityTickers`. + +Random scenarios load chunks with block entities in a save order (enough in +one chunk to make its block entity map grow), place, replace and remove block +entities, give jukeboxes a record and take it, and unload chunks, with ticks +in between. `VanillaBlockEntityTickerOracle` runs them on real `LevelChunk`s +of a server level and prints, after each tick, the positions of the live +tickers in list order; `examples/block_entity_tickers_fixture` runs the same +on `horizon_client::block_entities::BlockEntities`. The block entities are +the kinds the game ticks in that list (hoppers, furnaces, beacons, spawners, +trial spawners, vaults, hives, crafters, brewing stands, jukebox), with chests and barrels, +which have no ticker, among them. +""" +import argparse +import os +import random +import subprocess +import sys +try: + from . import lab, test_rust_parity as base +except ImportError: + import lab + import test_rust_parity as base + +PARITY = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity' +TICKING = ['hopper', 'furnace', 'blast_furnace', 'smoker', 'beacon', 'spawner', 'trial_spawner', 'vault', + 'beehive', 'bee_nest', 'crafter', 'brewing_stand', 'jukebox_record'] +KINDS = TICKING + ['jukebox', 'chest', 'barrel'] + + +def scenario(rng): + """One scenario's lines: a few chunks near the origin and the ops on them.""" + lines = ['reset'] + loaded, blocks = set(), {} + + def free(cx, cz): + while True: + p = (cx * 16 + rng.randrange(16), rng.randrange(-64, 320), cz * 16 + rng.randrange(16)) + if p not in blocks: + return p + + def load(): + options = [(x, z) for x in range(-2, 3) for z in range(-2, 3) if (x, z) not in loaded] + if not options: + return + cx, cz = rng.choice(options) + loaded.add((cx, cz)) + words = ['load', str(cx), str(cz)] + for _ in range(rng.choice([0, 1, 3, 8, 20, 30, 60])): + p, kind = free(cx, cz), rng.choice(KINDS) + blocks[p] = kind + words += [kind, *map(str, p)] + lines.append(' '.join(words)) + for _ in range(rng.randrange(1, 4)): + load() + lines.append('tick') + for _ in range(rng.randrange(10, 60)): + op = rng.random() + mine = sorted(blocks) + if op < 0.25 and loaded: + cx, cz = rng.choice(sorted(loaded)) + p, kind = free(cx, cz), rng.choice(KINDS) + blocks[p] = kind + lines.append(f'place {kind} {p[0]} {p[1]} {p[2]}') + elif op < 0.4 and mine: + p = rng.choice(mine) + lines.append(f'replace {p[0]} {p[1]} {p[2]}') + elif op < 0.6 and mine: + p = rng.choice(mine) + del blocks[p] + lines.append(f'remove {p[0]} {p[1]} {p[2]}') + elif op < 0.72: + jukeboxes = [p for p in mine if blocks[p].startswith('jukebox')] + if jukeboxes: + p = rng.choice(jukeboxes) + record = blocks[p] == 'jukebox' + blocks[p] = 'jukebox_record' if record else 'jukebox' + lines.append(f'record {p[0]} {p[1]} {p[2]} {int(record)}') + elif op < 0.78 and loaded: + cx, cz = rng.choice(sorted(loaded)) + loaded.discard((cx, cz)) + for p in [p for p in blocks if (p[0] >> 4, p[2] >> 4) == (cx, cz)]: + del blocks[p] + lines.append(f'unload {cx} {cz}') + elif op < 0.85: + load() + else: + lines.append('tick') + lines.append('tick') + return lines + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--scenarios', type=int, default=3000) + parser.add_argument('--seed', type=int, default=0x7469636b) + args = parser.parse_args() + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / 'rust-block-entity-tickers-parity' + build.mkdir(parents=True, exist_ok=True) + rng = random.Random(args.seed) + lines = [line for _ in range(args.scenarios) for line in scenario(rng)] + inputs = build / 'inputs.txt' + inputs.write_text('\n'.join(lines) + '\n') + classes = build / 'classes' + classes.mkdir(exist_ok=True) + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + + def run(command, name): + with (build / (name + '.log')).open('w') as log: + result = subprocess.run(list(map(str, command)), cwd=lab.ROOT, timeout=3600, stdout=log, + stderr=subprocess.STDOUT) + if result.returncode: + print((build / (name + '.log')).read_text()[-4000:]) + sys.exit(f'{name} failed') + run(['javac', '--release', lock['minecraft']['java_major'], '-proc:none', '-nowarn', '-cp', dependencies, + '-d', classes, *(PARITY / f'{n}.java' for n in ('ParityData', 'VanillaBlockEntityTickerOracle'))], + 'compile-java') + run(['java', '-Xmx1G', '-cp', str(classes) + os.pathsep + dependencies, + 'dev.horizonlab.parity.VanillaBlockEntityTickerOracle', inputs, build / 'vanilla.txt'], 'vanilla') + run(['cargo', 'run', '--quiet', '--profile', 'fast', '-p', 'horizon-client', '--example', + 'block_entity_tickers_fixture', '--', inputs, build / 'rust.txt'], 'rust') + matched = base.compare(build / 'vanilla.txt', build / 'rust.txt') + ticks = sum(1 for line in lines if line == 'tick') + listed = sum(len(line.split()) - 1 for line in (build / 'vanilla.txt').read_text().splitlines()) + print(f'block entity tickers: {args.scenarios} scenarios, {matched} of {ticks} ticks match vanilla ' + f'({listed} tickers listed)') + + +if __name__ == '__main__': + main() diff --git a/tools/test_block_removal_parity.py b/tools/test_block_removal_parity.py index 35b0fdd2b..6ebbbf1d3 100755 --- a/tools/test_block_removal_parity.py +++ b/tools/test_block_removal_parity.py @@ -5,8 +5,9 @@ A container, jukebox, lectern, campfire, decorated pot, chiseled bookshelf, shelf, shulker box or another block entity holder at the origin holds random contents; the case then replaces it with `Level.setBlock` (another block, the same block in another state, with or without UPDATE_SKIP_BLOCK_ENTITY_SIDEEFFECTS), -destroys it with `Level.destroyBlock` (with or without drops) or removes it with -`Level.removeBlock`. Vanilla runs over the headless level of the dispenser oracle +destroys it with `Level.destroyBlock` (with or without drops), removes it with +`Level.removeBlock`, or a survival player mines it (`ServerPlayerGameMode.destroyBlock`, +holding a tool or nothing, #844). Vanilla runs over the headless level of the dispenser oracle (`VanillaDispenserOracle`, "remove" cases), which replays `LevelChunk.setBlockState`'s `BlockEntity.preRemoveSideEffects`; Horizon runs its game glue in a headless game (`HORIZON_DISPENSE_FIXTURE`). Both print the block changes, the contents left, @@ -63,6 +64,10 @@ LISTED = {'chest', 'trapped_chest', 'barrel', 'hopper', 'dispenser', 'dropper', REPLACEMENTS = ['air', 'stone', 'glass', 'water', 'oak_planks', 'barrel', 'chest', 'hopper', 'blue_shulker_box', 'dirt', 'tnt', 'copper_chest', 'oxidized_copper_chest', 'lectern', 'jukebox'] FLAGS = [3, 3, 3, 2, 18, 2 | 256, 816, 3 | 64, 1] +# What a mining player holds: the right tool, a tool too weak or of another kind (swords and maces +# wear 2 a block, shears any block but fire), or nothing. +TOOLS = [plain('diamond_pickaxe'), plain('wooden_pickaxe'), plain('stone_axe'), plain('iron_shovel'), plain('iron_sword'), + plain('shears'), plain('mace'), '-'] def contents(rng, name, slots, pool): @@ -135,8 +140,12 @@ def cases(facts, count, seed, only): op = {'op': 'destroy', 'drop': True} elif roll < 0.85: op = {'op': 'destroy', 'drop': False} - else: + elif roll < 0.92: op = {'op': 'remove'} + else: + # ServerPlayerGameMode.destroyBlock: a survival player mines it (#844). The tool comes + # from the roll, not another draw, so the cases after this one stay the same. + op = {'op': 'mine', 'item': TOOLS[int(roll * 1000) % len(TOOLS)]} case = {**h.environment(rng), 'mode': 'remove', 'pos': [0, 0, 0], 'cells': cells, 'items': items, 'remove': op} case.pop('raining', None) case.pop('thundering', None) diff --git a/tools/test_dedicated_server_parity.py b/tools/test_dedicated_server_parity.py index c30abe4b0..23f304318 100644 --- a/tools/test_dedicated_server_parity.py +++ b/tools/test_dedicated_server_parity.py @@ -11,10 +11,14 @@ login, configuration and play, accepts the spawn teleport, receives the chunks around the spawn (their bytes are compared: the world is loaded from the same files), chats, runs /list and commands that fail (`/random value 7..7`, `/random value x`, `/trigger nope`), trades, is made an operator by -the console, runs /kill, respawns (as vanilla's LevelLoadTracker waits: until the "level -chunks load start" event and the chunk under the player), sets a furnace -beside itself, gives itself raw iron and coal, opens the furnace, shift-clicks -the ore and the coal in and the ingot out, and leaves. The +the console, runs /kill, runs a command while dead, respawns (as vanilla's +LevelLoadTracker waits: until the "level chunks load start" event and the +chunk under the player), sets a furnace +beside itself, gives itself raw iron and coal, sets and asks its own score +(`@s`), opens the furnace, shift-clicks +the ore and the coal in and the ingot out, right-clicks the ground with a +snowball in the main hand, then in the off hand, throws an ender pearl at +its feet, and leaves. The console then runs `list` and `say` and `stop`. Compared, all of them exactly: @@ -27,15 +31,24 @@ exactly: advancements, which vanilla keeps in hash maps, sorted); the command tree node by node; the chunks sent and their bytes; the replies to the chat message, /list and the failing commands, as text components; +- the reply to the command run while dead (ServerGamePacketListenerImpl + runs a dead player's commands, #973); - after /kill, the packets from the death screen to the respawned inventory (PlayerList.respawn; chat, chunk data, light and keep-alives left out, runs of a packet counted), then that the level loads where the player respawned (guests' death messages aren't Horizon's yet: left out); - the furnace menu: its type and title, its contents, the slots each shift-click changes and its lit and cooking totals; -- the console lines about the player, the chat, `list`, `say` and `stop`; +- the snowballs each right click throws: one, though the client sends the + block's part and the item's of each hand as separate packets + (handleUseItemOn, handleUseItem), and that the player is their owner; +- the ender pearl's teleport of the player who threw it, and the 5 damage; +- the console lines about the player (an operator's command feedback, which + vanilla broadcasts to the console, included), the chat, `list`, `say` and + `stop`; - the files the servers leave: server.properties (but the date line), - eula.txt, ops.json, whitelist.json, banned-players.json, banned-ips.json. + eula.txt, ops.json, whitelist.json, banned-players.json, banned-ips.json, + usercache.json (but the date the player's entry expires). Online mode needs Mojang's session servers and is not tested. """ @@ -208,10 +221,9 @@ CONSOLE_LINE = re.compile(r'^\[\d\d:\d\d:\d\d\] \[([^\]]+)\]: (.*)$') KEPT = re.compile(r'Tester|\[Server\]|\[Not Secure\]|There are|Stopping|Saving|ThreadedAnvilChunkStorage|All dimensions|Unknown or incomplete') -# Guests' death messages, the op's command feedback broadcast to the console and guests' -# advancement announcements aren't Horizon's yet (the client's own transcript leaves the -# death message out as well). -NOT_YET = re.compile(r'Tester was killed|Killed Tester|System chat: \[Tester: |Tester has made the advancement') +# Guests' death messages and advancement announcements aren't Horizon's yet (the client's +# own transcript leaves the death message out as well). +NOT_YET = re.compile(r'Tester was killed|Tester has made the advancement') def console_lines(lines): @@ -238,7 +250,8 @@ def console_lines(lines): def server_files(directory): out = [] - for name in ['server.properties', 'eula.txt', 'ops.json', 'whitelist.json', 'banned-players.json', 'banned-ips.json']: + for name in ['server.properties', 'eula.txt', 'ops.json', 'whitelist.json', 'banned-players.json', 'banned-ips.json', + 'usercache.json']: path = directory / name if not path.exists(): out.append(f'{name}: missing') @@ -249,6 +262,9 @@ def server_files(directory): text = re.sub(r'^(#[^\n]*\n)#[^\n]*\n', r'\1#<date>\n', text) # Random per server, and each side has its own port. text = re.sub(r'\n(management-server-secret|server-port)=[^\n]*', r'\n\1=<varies>', text) + if name == 'usercache.json': + # A month from when the player joined. + text = re.sub(r'"expiresOn":"[^"]*"', '"expiresOn":"<a month on>"', text) out.append(f'{name}: {text!r}') return out @@ -261,6 +277,12 @@ def run_side(label, command, directory, client): # it stays put; experience keeps its profession). server.console('summon minecraft:villager ~1 ~ ~ {NoAI:1b,Xp:1,VillagerData:' '{profession:"minecraft:librarian",level:2,type:"minecraft:plains"}}') + # The saved world still has fluid ticks due in the area the client is sent (a lava fall + # that never settles), and they run once their chunk ticks. When that is, against when the + # chunk is sent, follows how fast the server loads the chunks around the joining player: + # each run, vanilla sends a different set of them already flowed. Frozen until the client + # has its chunks, both servers send the world as saved; blocks tick again from the chat on. + server.console('tick freeze') port = int(PROPERTIES_PORTS[label]) legacy = legacy_ping(port) # The client asks to be made an operator once it has traded, runs /kill and respawns. @@ -275,7 +297,11 @@ def run_side(label, command, directory, client): joined = threading.Thread(target=join) joined.start() deadline = time.time() + 300 + frozen = True while joined.is_alive() and not any('<Tester> ready to respawn' in l for l in server.lines): + if frozen and any('<Tester> hello from the parity client' in l for l in server.lines): + server.console('tick unfreeze') + frozen = False if time.time() > deadline: break time.sleep(0.2) diff --git a/tools/test_game_loop_parity.py b/tools/test_game_loop_parity.py index d724dc6ac..101b3f238 100644 --- a/tools/test_game_loop_parity.py +++ b/tools/test_game_loop_parity.py @@ -120,8 +120,15 @@ def scroll_cases(rng): x = rng.choice([0.0, 0.0, 0.0, 1.0, -1.0, 0.25, -0.25, 0.4, 2.0, 5.5, -7.0, rng.uniform(-3, 3)]) y = rng.choice([0.0, 1.0, -1.0, 1.0, -1.0, 0.1, -0.3, 0.5, -0.5, 2.0, -3.0, 120.0, rng.uniform(-3, 3)]) ops.append([dbits(x), dbits(y)]) - yield dict(t='scroll', discrete=rng.random() < 0.4, sens=dbits(rng.choice([1.0, 0.01, 10.0, 0.5, 2.0, 3.1622776601683795])), - slot=rng.randrange(9), ops=ops) + case = dict(t='scroll', discrete=rng.random() < 0.4, sens=dbits(rng.choice([1.0, 0.01, 10.0, 0.5, 2.0, 3.1622776601683795])), + slot=rng.randrange(9), ops=ops) + yield case + yield dict(case, spectator=True) + # Bounds, recovery from each bound, fractional accumulation, and horizontal-only input. + yield dict(t='scroll', discrete=False, sens=dbits(1.0), slot=4, spectator=True, + ops=[[dbits(x), dbits(y)] for x, y in + [(0, 100), (0, -1), (0, -100), (0, 1), (0, .4), (0, .7), + (0, -.4), (0, -.7), (5, 0), (-5, 0)]]) def key_cases(rng): @@ -152,7 +159,7 @@ def main(): 'Window.calculateScale/setGuiScale and MouseHandler scaled positions, FramerateLimitTracker throttle reasons and ' 'limits (not the focus-lost exclusive-fullscreen branch, which reads SDL), MouseHandler.onMove/turnPlayer with ' 'sensitivity, cinematic smoothing, scoping and inversion, Entity.turn clamps, onScroll with ScrollWheelHandler ' - 'accumulation, discrete scrolling and hotbar selection, and ToggleKeyMapping/KeyMapping down, click and ' + 'accumulation, discrete scrolling, hotbar selection and spectator flight speed, and ToggleKeyMapping/KeyMapping down, click and ' 'screen-release state. The frame clock source (Blaze3D.getTime, System.nanoTime) is injected, so sleeping in ' 'FramerateLimiter and vsync present-mode selection are covered by Rust unit tests only.') harness.differential( diff --git a/tools/test_interaction_input_parity.py b/tools/test_interaction_input_parity.py new file mode 100644 index 000000000..1aa34a73f --- /dev/null +++ b/tools/test_interaction_input_parity.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""Compare queued interaction ordering with the real Minecraft.handleKeybinds.""" +import itertools +import json +import random + +try: + from . import lab, oracle_harness as harness +except ImportError: + import lab, oracle_harness as harness + + +def corpus(): + rng = random.Random(0x434C49434B) + for using, destroying, attack_down, use_down, entity in itertools.product([False, True], repeat=5): + for clicks in [[], [1], [0, 1], [1, 0], [2, 1, 0], [0, 0, 1, 1, 2]]: + for delay in [0, 3]: + yield dict(using=using, destroying=destroying, attack_down=attack_down, + use_down=use_down, entity=entity, clicks=clicks, delay=delay, + dig_on_attack=rng.choice([False, True]), + begin_using=rng.choice([False, True])) + # A player interaction returning PASS must go directly to the held item. + # The level still reports a solid block underneath the entity hit. + for entity in [False, True]: + for clicks in [[1], [1, 1], [0, 1], [1, 2]]: + yield dict(using=False, destroying=False, attack_down=False, use_down=False, + entity=entity, clicks=clicks, delay=0, dig_on_attack=False, + begin_using=False, passing=True) + + +def main(): + def inputs(build): + rows = list(corpus()) + path = build / "inputs.jsonl" + path.write_text("".join(json.dumps(row) + "\n" for row in rows)) + return path, len(rows) + + harness.differential( + "interaction-input-parity", "VanillaInteractionInputOracle", "interaction_input_fixture", inputs, + "Invokes Minecraft.handleKeybinds itself with real KeyMapping click counters. " + "Compares attack/use/pick ordering, release gating, drained clicks during use, " + "held-button repetition and use cooldowns for block/entity targets. " + "Includes passing entity interactions over solid blocks: only the held " + "item may be tried afterward. " + "Game-mode side effects and world simulation are controlled fixtures.", + sources=[lab.ROOT / "crates/horizon-client/src/interaction_input.rs", + lab.ROOT / "crates/horizon-client/src/game/remote.rs", + lab.ROOT / "crates/horizon-client/src/game/hand.rs", + lab.ROOT / "mods/horizon/src/test/java/dev/horizonlab/parity/ParityData.java"]) + + +if __name__ == "__main__": + main() diff --git a/tools/test_locator_bar_parity.py b/tools/test_locator_bar_parity.py new file mode 100644 index 000000000..bc7d21750 --- /dev/null +++ b/tools/test_locator_bar_parity.py @@ -0,0 +1,259 @@ +#!/usr/bin/env python3 +"""The locator bar on a dedicated server, against vanilla's, seen by vanilla clients. + +A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one flat +world vanilla generated first (players appear at its spawn), with the same server.properties (offline, +creative, view distance 4) and two operators. LocatorBarClient, two +players made of vanilla's protocol code (any byte vanilla would not read is an error), joins Alice and then +Bob, who fly as creative players do (a player floating without flying falls on the server every tick), and +records every ClientboundTrackedWaypointPacket each of them receives. Bob then: + +- teleports one block, three blocks, out of Alice's view, one chunk on, really far (an azimuth) and turns + around her, and back; +- turns invisible and visible, joins a red team (by the console), has it turned blue, leaves it; +- crouches and stands up, wears a carved pumpkin and takes it off; +- turns spectator and moves, then creative again; + +the console turns the `locator_bar` game rule off and on, and Bob leaves. The waypoint packets each player +received in each step are compared exactly: operation, player, style, colour and the block, chunk or angle. +""" +import os +from pathlib import Path +import platform +import shutil +import subprocess +import sys +import time + +try: + from . import lab, test_rust_parity as base + from .test_dedicated_server_parity import Server, free_port +except ImportError: + import lab + import test_rust_parity as base + from test_dedicated_server_parity import Server, free_port + +NAME = 'locator-bar-parity' +PROPERTIES = { + 'online-mode': 'false', + 'white-list': 'false', + 'level-seed': '12345', + 'level-type': 'minecraft\\:flat', + 'generate-structures': 'false', + 'gamemode': 'creative', + 'force-gamemode': 'true', + 'view-distance': '4', + 'simulation-distance': '4', + 'spawn-protection': '0', + 'max-players': '5', + 'pause-when-empty-seconds': '-1', +} +PLAYERS = ['Alice', 'Bob'] + +# (label, [what the console does], [what the players do]); every step then waits for its packets. +SCRIPT = [ + ('alice-joins', [], ['join Alice', 'fly Alice on', 'cmd Alice tp @s 0.5 100 0.5']), + ('bob-joins', [], ['join Bob', 'fly Bob on', 'cmd Bob tp @s 2.5 100 0.5']), + ('near', [], ['cmd Bob tp @s 5.5 100 0.5']), + ('one-block', [], ['cmd Bob tp @s 6.5 100 0.5']), + ('three-blocks', [], ['cmd Bob tp @s 9.5 100 0.5']), + ('out-of-view', [], ['cmd Bob tp @s 100.5 100 0.5']), + ('next-chunk', [], ['cmd Bob tp @s 116.5 100 0.5']), + ('really-far', [], ['cmd Bob tp @s 1000.5 100 0.5']), + ('turn-around', [], ['cmd Bob tp @s 1000.5 100 300.5']), + ('half-degree', [], ['cmd Bob tp @s 1000.5 100 302.5']), + ('invisible', [], ['cmd Bob effect give @s minecraft:invisibility infinite 0 true']), + ('visible', [], ['cmd Bob effect clear @s']), + ('back-near', [], ['cmd Bob tp @s 3.5 100 0.5']), + ('red-team', ['team add red', 'team modify red color red', 'team join red Bob'], []), + ('blue-team', ['team modify red color blue'], []), + ('no-team', ['team leave Bob'], []), + # Crouching needs the ground: a flying player who holds shift goes down instead. + ('landed', [], ['cmd Bob tp @s 3.5 -60 0.5', 'fly Bob off']), + ('crouch', [], ['shift Bob on']), + ('stand', [], ['shift Bob off']), + ('flying-again', [], ['fly Bob on', 'cmd Bob tp @s 3.5 100 0.5']), + ('pumpkin', [], ['cmd Bob item replace entity @s armor.head with minecraft:carved_pumpkin']), + ('no-pumpkin', [], ['cmd Bob item replace entity @s armor.head with minecraft:air']), + ('rule-off', ['gamerule locator_bar false'], []), + ('moves-without', [], ['cmd Bob tp @s 4.5 100 0.5']), + ('rule-on', ['gamerule locator_bar true'], []), + ('spectator', [], ['cmd Bob gamemode spectator', 'cmd Bob tp @s 5.5 100 0.5']), + ('creative', [], ['cmd Bob gamemode creative', 'cmd Bob tp @s 6.5 100 0.5']), + ('bob-leaves', [], ['quit Bob']), +] +STEP_SECONDS = 2.0 +# Vanilla's server moves a player who just joined on their own first ticks (a tiny step and back), which tracks +# them where they spawned; Horizon does not simulate its guests. These steps compare what the bar shows at +# their end (Bob has moved by then) rather than every packet. +SETTLING_STEPS = {'alice-joins', 'bob-joins'} + + +def shown(lines): + """The transcript with each settling step reduced to what each player's bar shows at its end.""" + out, state = [], {} + + def flush(step): + for (receiver, source), rest in sorted(state.items()): + out.append(f'{receiver} step={step} shows {source} {rest}') + state.clear() + + current = None + for line in lines: + receiver, step, *packet = line.split(' ') + step = step.removeprefix('step=') + if current in SETTLING_STEPS and step != current: + flush(current) + current = step + if step not in SETTLING_STEPS or len(packet) < 3: + out.append(line) + elif packet[0] == 'untrack': + state.pop((receiver, packet[1]), None) + else: + state[(receiver, packet[1])] = ' '.join(packet[2:]) + if current in SETTLING_STEPS: + flush(current) + return out + + +def ops_json(): + """Both players at level 4, by their offline UUIDs.""" + import hashlib + import json + import uuid as uuidlib + entries = [] + for name in PLAYERS: + digest = bytearray(hashlib.md5(f'OfflinePlayer:{name}'.encode()).digest()) + digest[6] = (digest[6] & 0x0F) | 0x30 + digest[8] = (digest[8] & 0x3F) | 0x80 + entries.append({'uuid': str(uuidlib.UUID(bytes=bytes(digest))), 'name': name, 'level': 4, + 'bypassesPlayerLimit': False}) + return json.dumps(entries, indent=2) + + +def server_dir(directory, world=None): + if directory.exists(): + shutil.rmtree(directory) + directory.mkdir(parents=True) + port = free_port() + props = dict(PROPERTIES, **{'server-port': str(port)}) + (directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items()), + encoding='iso-8859-1') + (directory / 'eula.txt').write_text('eula=true\n') + (directory / 'ops.json').write_text(ops_json()) + if world is not None: + shutil.copytree(world, directory / 'world') + (directory / 'world' / 'session.lock').unlink(missing_ok=True) + return port + + +def generate_world(server_command, build): + """The flat world both servers load, made once by vanilla: players appear at its spawn itself + (respawn_radius 0), on the ground at 0 -60 0, and the chunks around it are saved.""" + directory = build / 'generate' + server_dir(directory) + server = Server(server_command, directory, 'generate') + try: + server.wait_for(r'Done \(') + for command, reply in [('gamerule respawn_radius 0', 'respawn_radius'), + ('setworldspawn 0 -60 0', 'Set the world spawn'), + ('forceload add -96 -96 96 96', 'force loaded'), + ('save-all flush', 'Saved the game')]: + server.console(command) + server.wait_for(reply, timeout=120) + server.console('forceload remove all') + finally: + server.stop() + return directory / 'world' + + +def run_side(label, server_command, client_command, build, world): + directory = build / label + port = server_dir(directory, world) + out = build / f'{label}.txt' + out.unlink(missing_ok=True) + server = Server(server_command, directory, label) + try: + server.wait_for(r'Done \(') + with (build / f'{label}-client.log').open('w') as log: + client = subprocess.Popen([*map(str, client_command), '127.0.0.1', str(port), str(out)], + cwd=lab.ROOT, stdin=subprocess.PIPE, stdout=log, stderr=subprocess.STDOUT, + text=True) + try: + for step, console, actions in SCRIPT: + client.stdin.write(f'step {step}\n') + client.stdin.flush() + for command in console: + server.console(command) + for action in actions: + client.stdin.write(action + '\n') + client.stdin.flush() + if action.startswith('join'): + # Joining takes the world's first chunks: wait for the server to place the player. + server.wait_for(rf'{action.split()[1]} joined the game', timeout=120) + time.sleep(STEP_SECONDS) + client.stdin.write('end\n') + client.stdin.flush() + client.wait(timeout=60) + finally: + if client.poll() is None: + client.kill() + finally: + server.stop() + if not out.exists(): + raise AssertionError(f'{label}: the client wrote no transcript (see {build}/{label}-client.log)') + return out.read_text(encoding='utf-8').splitlines() + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java' + classes = build / 'classes' + shutil.rmtree(classes, ignore_errors=True) + classes.mkdir() + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-nowarn', '-sourcepath', + lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle], + cwd=lab.ROOT, check=True, capture_output=True) + subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, + check=True, capture_output=True) + import json + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] + horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + client = ['java', '-Xmx1g', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.LocatorBarClient'] + + world = generate_world(vanilla_server, build) + vanilla = run_side('vanilla', vanilla_server, client, build, world) + horizon = run_side('horizon', horizon_server, client, build, world) + errors = [l for l in vanilla + horizon if ' error ' in f' {l} ' or 'disconnected' in l] + vanilla, horizon = shown(vanilla), shown(horizon) + # Each player's packets in their order; which player's line comes first in a step doesn't matter. + if errors or vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=1, lineterm='')) + raise AssertionError(f'{len(errors)} errors, transcripts differ (see {build}/vanilla.txt and horizon.txt):\n' + + '\n'.join(errors[:10]) + '\n' + diff[:6000]) + if len(vanilla) < 20: + raise AssertionError(f'only {len(vanilla)} waypoint packets: the script did not run (see {build})') + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + packets=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), + scope='Locator bar waypoints (ClientboundTrackedWaypointPacket) two vanilla-codec players receive on a ' + 'dedicated server: block, chunk and azimuth connections as one moves, invisibility, team colours, ' + 'crouching, a carved pumpkin, the locator_bar game rule, spectator mode and leaving.')) + print(f'locator bar: {len(vanilla)} waypoint packets identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') diff --git a/tools/test_mob_ai_parity.py b/tools/test_mob_ai_parity.py index a32ec2cf9..9bb244ce9 100644 --- a/tools/test_mob_ai_parity.py +++ b/tools/test_mob_ai_parity.py @@ -262,6 +262,12 @@ def scenarios(): add('bee_hive_rain', 300, [HIVE, 'mob minecraft:bee 0.5 -58 0.5 0 162 persistent'], 'rain') add('bee_hive_morning', 1000, [HIVE, 'mob minecraft:bee 0.5 -58 0.5 0 163 persistent'], 'time=22900 daycycle') add('bee_hive_honey', 2600, [HIVE, 'mob minecraft:bee 0.5 -58 0.5 0 164 persistent nectar']) + # Mobs dropped into deep water (#542): FloatGoal users float at the surface (their navigation + # can float, so Mob.jumpInLiquid adds 0.04 a tick), zombies sink. + for species, seed in [('pig', 171), ('cow', 172), ('wolf', 173), ('cat', 174), ('horse', 175), + ('witch', 176), ('vindicator', 177), ('zombie', 178)]: + add(f'{species}_deep_water', 160, ['fill -4 -64 -4 4 -61 4 minecraft:water', + f'mob minecraft:{species} 0.5 -62 0.5 0 {seed} persistent']) return s diff --git a/tools/test_online_mode_parity.py b/tools/test_online_mode_parity.py index 45b4b3ac6..e6679b7b5 100644 --- a/tools/test_online_mode_parity.py +++ b/tools/test_online_mode_parity.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -"""Horizon's client joins vanilla's online-mode server, with no real account. +"""Horizon's client joins vanilla's online-mode server and Horizon's, with no real account. A small mock of the Minecraft services (dev only: the discovery document and the session service's join and hasJoined endpoints) stands in for Mojang's. @@ -20,7 +20,16 @@ in the world with its chunks, chats, runs /list, leaves). Also checked, with vanilla's messages: - a token the session service rejects ("Failed to log in: Invalid session"); - a join the session service forgets (the server: "Failed to verify username!"); -- no account at all (the client refuses before joining). +- no account at all (the client refuses before joining); +- a verified player the whitelist refuses; +- `whitelist add` in online mode: a player who joined before is known by name + and let in; another name is looked up through the services' profile lookup, + and one with no account is refused ("That player does not exist"). + +Horizon's dedicated server then runs the same steps in online mode +(`HORIZON_API_DISCOVERY` points it at the mock too): it must give the client +the same answers and log the same lines about the player as vanilla's +("UUID of player ...", "Disconnecting ...", "... lost connection: ..."). Chat is unsigned here: the mock serves no Mojang public keys, so the server can't validate profile keys and doesn't enforce secure chat. @@ -31,9 +40,11 @@ import os from pathlib import Path import platform import shutil +import re import subprocess import sys import threading +import time import urllib.parse try: @@ -92,6 +103,14 @@ class Services: query = dict(urllib.parse.parse_qsl(url.query)) if url.path == '/minecraft/client': return self.reply(200, services.discovery()) + if url.path.startswith('/users/profiles/minecraft/'): + # profiles/getByName: the account with that name, ignoring case. + name = urllib.parse.unquote(url.path.rsplit('/', 1)[1]) + with services.lock: + services.log.append(('lookup', name)) + if name.lower() == PLAYER.lower(): + return self.reply(200, {'id': PLAYER_UUID, 'name': PLAYER}) + return self.reply(404, {'error': 'NOT_FOUND', 'errorMessage': f'Couldn\'t find any profile with name {name}'}) if url.path == '/session/minecraft/hasJoined': with services.lock: ok = services.joined.get(query.get('username')) == query.get('serverId') @@ -99,11 +118,15 @@ class Services: if ok: return self.reply(200, {'id': PLAYER_UUID, 'name': query['username'], 'properties': []}) return self.reply(204) + with services.lock: + services.log.append(('unknown', url.path)) self.reply(404, {'error': 'NotFound', 'path': url.path}) def do_POST(self): body = json.loads(self.rfile.read(int(self.headers.get('Content-Length', 0))) or b'{}') if self.path != '/session/minecraft/join': + with services.lock: + services.log.append(('unknown', self.path)) return self.reply(404, {'error': 'NotFound'}) token, profile, server_id = body.get('accessToken'), body.get('selectedProfile'), body.get('serverId') accepted = token in (GOOD, FORGET) and profile == PLAYER_UUID @@ -127,7 +150,8 @@ class Services: 'session': endpoints(join='/session/minecraft/join', verify='/session/minecraft/hasJoined', getProfileById='/session/minecraft/profile/{profileId}'), 'player': {'endpoints': {}}, - 'profiles': {'endpoints': {'getTexture': {'validUris': [self.url + '/texture/{textureId}']}}}, + 'profiles': {'endpoints': {'getTexture': {'validUris': [self.url + '/texture/{textureId}']}, + 'getByName': {'uri': self.url + '/users/profiles/minecraft/{name}'}}}, 'telemetry': {'endpoints': {}}, }} @@ -167,43 +191,53 @@ def run_client(binary, build, port, label, token, discovery): got['session file left'] = (directory / 'session.json').exists() return got +# The server's lines about the player, as both servers print them. +PLAYER_LINE = re.compile(r'^\[\d\d:\d\d:\d\d\] \[([^\]]+)\]: (.*(?:Tester|Username|Disconnecting|verify username).*)$') -def main(): - lock, jars, libraries = base.verify_artifacts() - build = lab.LOCAL / NAME - build.mkdir(parents=True, exist_ok=True) - report = build / 'report.json' - report.unlink(missing_ok=True) - oh.run_logged(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], build, - 'compile-rust') - metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], - cwd=lab.ROOT, text=True)) - binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') - dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) - services = Services() - discovery = services.url + '/minecraft/client' +def player_lines(lines): + out = [] + for line in lines: + if m := PLAYER_LINE.match(line): + text = re.sub(r'/127\.0\.0\.1:\d+', '/127.0.0.1:_', m.group(2)) + text = re.sub(r'entity id \d+ at \(.*\)', 'entity id _ at _', text) + out.append(f'[{m.group(1)}] {text}') + return out + + +def wait_after(server, mark, pattern, timeout=30): + """A line matching `pattern` among those the server printed after `mark`.""" + regex = re.compile(pattern) + deadline = time.time() + timeout + while time.time() < deadline: + if any(regex.search(l) for l in server.lines[mark:]): + return + time.sleep(0.2) + raise AssertionError(f'no {pattern!r} after line {mark} of {server.log_path.name}') + + +def run_side(label, command, binary, build, services, discovery): + """The six logins against one server; its checks and its log about the player.""" port = free_port() - directory = build / 'server' + directory = build / label shutil.rmtree(directory, ignore_errors=True) directory.mkdir() props = dict(PROPERTIES, **{'server-port': str(port)}) (directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items())) (directory / 'eula.txt').write_text('eula=true\n') - command = ['java', '-Xmx2g', '-Duser.timezone=UTC', f'-Dminecraft.api.discovery.host={discovery}', - '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] - server = Server(command, directory, 'vanilla-online') + os.environ.update(HORIZON_NO_AUDIO='1', HORIZON_API_DISCOVERY=discovery) + server = Server(command, directory, f'{label}-online') checks = [] - def check(label, actual, expected): - checks.append(f'{label}: ' + ('ok' if actual == expected else f'expected {expected!r}, got {actual!r}')) + def check(name, actual, expected): + checks.append(f'{name}: ' + ('ok' if actual == expected else f'expected {expected!r}, got {actual!r}')) try: server.wait_for(r'Done \(', timeout=600) services.take_log() # 1. Signed in: the full encrypted handshake, then play. - got = run_client(binary, build, port, 'signed-in', GOOD, discovery) + got = run_client(binary, build, port, f'{label}-signed-in', GOOD, discovery) server.wait_for(r'Tester left the game', timeout=30) log = services.take_log() joins = [e for e in log if e[0] == 'join'] @@ -227,27 +261,89 @@ def main(): # 2. A token the session service rejects: vanilla's message, no key sent. mark = len(server.lines) - got = run_client(binary, build, port, 'rejected', BAD, discovery) + got = run_client(binary, build, port, f'{label}-rejected', BAD, discovery) check('rejected token: client message', got['error'], 'disconnected: Failed to log in: Invalid session (Try restarting your game and the launcher)') check('rejected token: no verification', [e[0] for e in services.take_log()], ['join']) # 3. The session service forgets the join: the server refuses the player. - got = run_client(binary, build, port, 'forgotten', FORGET, discovery) + got = run_client(binary, build, port, f'{label}-forgotten', FORGET, discovery) server.wait_for(r'Failed to verify username', timeout=30) check('forgotten join: client message', got['error'], 'disconnected: Failed to verify username!') check('forgotten join: server verified and refused', [(e[0], e[3]) for e in services.take_log()], [('join', True), ('verify', False)]) # 4. No account: refused by the client before anything is sent to the services. - got = run_client(binary, build, port, 'no-account', None, discovery) + got = run_client(binary, build, port, f'{label}-no-account', None, discovery) check('no account: client message', got['error'] or '', 'disconnected: This server is in online mode. ' 'Sign in with your Microsoft account in the Horizon launcher to join it.') check('no account: no service calls', services.take_log(), []) check('no player joined after the first', 'joined the game' in '\n'.join(server.lines[mark:]), False) + + # 5. Verified, but not on the whitelist. + server.console('whitelist on') + server.wait_for(r'Whitelist is now turned on', timeout=30) + got = run_client(binary, build, port, f'{label}-not-whitelisted', GOOD, discovery) + server.wait_for(r'Disconnecting Tester', timeout=30) + check('not whitelisted: client message', got['error'], 'disconnected: You are not white-listed on this server!') + check('not whitelisted: verified first', [(e[0], e[3]) for e in services.take_log()], + [('join', True), ('verify', True)]) + + # 6. Whitelisted by name: the server knows the player from their first join (its name cache), so it + # asks the services nothing, and lets them in. + mark = len(server.lines) + server.console('whitelist add tester') + wait_after(server, mark, r'Added Tester to the whitelist') + check('whitelist add: known player, no lookup', services.take_log(), []) + got = run_client(binary, build, port, f'{label}-whitelisted', GOOD, discovery) + wait_after(server, mark, r'Tester left the game') + check('whitelisted: connected throughout', got['error'], None) + services.take_log() + # A name nobody joined with: the server looks the account up, and there is none. + mark = len(server.lines) + server.console('whitelist add Nobody') + wait_after(server, mark, r'That player does not exist') + check('whitelist add: lookup failure logged', + any("Couldn't find profile with name: nobody" in l for l in server.lines[mark:]), True) + check('whitelist add: no such account', services.take_log(), [('lookup', 'nobody')]) + time.sleep(1) finally: server.stop() + return checks, player_lines(server.lines) + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + oh.run_logged(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], build, + 'compile-rust') + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + + services = Services() + discovery = services.url + '/minecraft/client' + vanilla = ['java', '-Xmx2g', '-Duser.timezone=UTC', f'-Dminecraft.api.discovery.host={discovery}', + '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] + horizon = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + checks, transcripts = [], {} + try: + for label, command in [('vanilla', vanilla), ('horizon', horizon)]: + side_checks, transcripts[label] = run_side(label, command, binary, build, services, discovery) + checks += [f'{label}: {c}' for c in side_checks] + finally: services.stop() + for label in transcripts: + (build / f'{label}.txt').write_text('\n'.join(transcripts[label]) + '\n', encoding='utf-8') + if 'horizon' in transcripts: + import difflib + diff = [l for l in difflib.unified_diff(transcripts['vanilla'], transcripts['horizon'], 'vanilla', 'horizon', + n=0, lineterm='') if l[:1] in '+-' and l[:3] not in ('+++', '---')] + checks.append('server log about the player: ' + ('ok' if not diff else 'differs:\n ' + '\n '.join(diff))) for line in checks: print(line) @@ -257,8 +353,9 @@ def main(): lab.write_json(report, dict( schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], checks=len(checks), - scope='Online-mode login against vanilla\'s dedicated server and a mock session service: server hash, ' - 'RSA key packet, AES/CFB8, session join and verify, play; rejected token, forgotten join, no account.')) + scope='Online-mode login against vanilla\'s dedicated server and Horizon\'s, with a mock session service: ' + 'server hash, RSA key packet, AES/CFB8, session join and verify, play; rejected token, forgotten ' + 'join, no account, whitelist, whitelist add by account name; the servers\' log lines about the player.')) print(f'online mode: {len(checks)} checks ok') diff --git a/tools/test_options_parity.py b/tools/test_options_parity.py index 95732663d..cf7698e5c 100644 --- a/tools/test_options_parity.py +++ b/tools/test_options_parity.py @@ -1,7 +1,9 @@ #!/usr/bin/env python3 """Compare options.txt loading and saving, and the key mapping table, with the pinned client: every file is loaded through the real Options.load and written -back with Options.save, and the Rust port must produce identical text.""" +back with Options.save. The Rust probe normalizes Horizon's intentional 33..64 +render-distance extension to vanilla's default; a Rust slider/persistence +regression covers the extended range.""" from pathlib import Path import random import shutil @@ -182,7 +184,8 @@ def main(): 'fields, lenient Gson parsing and syntax-error aborts, key mappings (names, scancode-number forms, unbinding), ' 'sound volumes, model parts, line endings, the OPTIONS data fixers across version boundaries, and the ' 'KeyMapping table (order, defaults, categories, toggle mappings). Does not cover option screens or ' - 'runtime side effects of setting an option.') + 'runtime side effects of setting an option. Horizon render distances 33..64 are normalized to vanilla\'s ' + 'default by options_probe; the slider/persistence unit regression checks that intentional extension.') harness.differential( 'rust-options-parity', 'VanillaOptionsOracle', 'options_probe', make_cases, scope, sources=[lab.ROOT / 'crates/horizon-client/src/options_file.rs', lab.ROOT / 'crates/horizon-client/src/options_fixes.rs', diff --git a/tools/test_player_sync_parity.py b/tools/test_player_sync_parity.py new file mode 100644 index 000000000..4c78b439f --- /dev/null +++ b/tools/test_player_sync_parity.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python3 +"""Compare movement packet selection and teleport velocity with the pinned client.""" +import json +import random +import struct + +try: + from . import lab, oracle_harness as harness +except ImportError: + import lab, oracle_harness as harness + + +def corpus(): + # Stationary heartbeats, rotation-only updates, status changes and corrected + # positions followed by unchanged looks must retain vanilla's packet types. + yield dict(kind="movement", ops=[[0, 0, 0, 0, 0, False, False]] * 61) + yield dict(kind="movement", ops=[ + [0, 64, 0, 90, 0, True, False], + [0, 64, 0, 90, 0, True, False], + [0, 64, 0, 91, 0, True, False], + [100, 80, -20, 180, 20, False, False], + [100, 80, -20, 180, 20, True, False], + [100, 80, -20, 180, 20, True, True], + ]) + rng = random.Random(0x53594E43) + for _ in range(300): + state = [0, 0, 0, 0, 0, False, False] + ops = [] + for _ in range(80): + if rng.random() < 0.5: + axis = rng.randrange(3) + state[axis] += rng.choice([0, 0.0001, 0.0002, 0.0002000001, 0.1, 10]) + if rng.random() < 0.3: + state[3] += rng.choice([-360, -1, 0, 0.001, 1, 360]) + state[4] = rng.choice([-90, 0, 30, 90]) + if rng.random() < 0.2: + state[rng.randrange(5, 7)] = rng.choice([False, True]) + ops.append(state.copy()) + yield dict(kind="movement", ops=ops) + for flags in range(16): + for _ in range(40): + yield dict(kind="velocity", flags=flags << 5, + velocity=[rng.uniform(-4, 4) for _ in range(3)], + delta=[rng.uniform(-4, 4) for _ in range(3)], + old=[rng.uniform(-720, 720), rng.uniform(-90, 90)], + new=[rng.uniform(-720, 720), rng.uniform(-90, 90)]) + + +def main(): + def inputs(build): + rows = list(corpus()) + def bits(v): + return struct.pack(">d", v).hex() + for row in rows: + if row["kind"] == "movement": + row["ops"] = [[*map(bits, op[:5]), *op[5:]] for op in row["ops"]] + else: + for key in ["velocity", "delta", "old", "new"]: + row[key] = list(map(bits, row[key])) + path = build / "inputs.jsonl" + path.write_text("".join(json.dumps(row) + "\n" for row in rows)) + return path, len(rows) + + harness.differential( + "player-sync-parity", "VanillaPlayerSyncOracle", "player_sync_fixture", inputs, + "Actual LocalPlayer.sendPosition packet payloads over persistent movement " + "sequences; PositionMoveRotation.calculateAbsolute velocity with every " + "relative-delta/rotate-delta combination. World simulation, connection " + "scheduling and live anticheat behaviour are outside this harness.", + sources=[lab.ROOT / "crates/horizon-client/src/player_sync.rs", + lab.ROOT / "crates/horizon-client/src/game/remote.rs", + lab.ROOT / "crates/horizon-client/src/net.rs"]) + + +if __name__ == "__main__": + main() diff --git a/tools/test_secure_chat_parity.py b/tools/test_secure_chat_parity.py new file mode 100644 index 000000000..13bf494dd --- /dev/null +++ b/tools/test_secure_chat_parity.py @@ -0,0 +1,317 @@ +#!/usr/bin/env python3 +"""Secure chat on vanilla's dedicated server and Horizon's, with no real account. + +A mock of the Minecraft services stands in for Mojang's: its key set (the +`authentication` / `getPublicKeys` endpoint) holds a key pair the harness +makes, so the profile keys it signs are as good as Mojang's to both servers. +Vanilla finds the mock through authlib's `-Dminecraft.api.discovery.host`, +Horizon through `HORIZON_API_DISCOVERY`. SecureChatClient (vanilla's own +codecs and chat classes) plays several players at once: each signs its chat +with a profile key the mock signed, acknowledges what it saw, and checks the +signature of every chat it receives against the sender's session from the +player list, as vanilla's client does. + +Two servers, each run on vanilla then on Horizon: +- offline mode (enforce-secure-profile is then off): signed chat with the + last-seen window and the signature cache, unsigned chat beside it, /say, + /me and /msg with signed and unsigned arguments, a signed argument under + the wrong name, a forged signature and the broken chain after it, and an + acknowledgement past the window; +- online mode with enforce-secure-profile: the status and the login packet + say so, unsigned chat and unsigned commands with a message are refused, and + a key signed for another player is refused. + +Compared: every player's transcript, the status' `enforcesSecureChat`, and +the servers' log lines about chat. +""" +import hashlib +import json +import os +from pathlib import Path +import platform +import re +import shutil +import socket +import subprocess +import sys +import time +import threading +import urllib.parse +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +try: + from . import lab, oracle_harness as oh, test_rust_parity as base + from .test_dedicated_server_parity import Server, free_port +except ImportError: + import lab + import oracle_harness as oh + import test_rust_parity as base + from test_dedicated_server_parity import Server, free_port + +NAME = 'secure-chat-parity' +SCENARIOS = { + 'offline': {'online-mode': 'false'}, + 'online': {'online-mode': 'true', 'enforce-secure-profile': 'true'}, +} +PROPERTIES = { + 'white-list': 'false', + 'level-seed': '12345', + 'view-distance': '3', + 'simulation-distance': '3', + 'spawn-protection': '0', + 'max-players': '8', + 'pause-when-empty-seconds': '-1', +} + + +def account(name): + """The mock services' account id for a name.""" + return hashlib.md5(f'account:{name}'.encode()).hexdigest() + + +class Services: + """Discovery, the key set, the session service's join and hasJoined, profiles by name.""" + + def __init__(self, key_set): + self.joined = {} + self.lock = threading.Lock() + services = self + + class Handler(BaseHTTPRequestHandler): + def log_message(self, *args): + pass + + def reply(self, status, body=None): + data = b'' if body is None else json.dumps(body).encode() + self.send_response(status) + if body is not None: + self.send_header('Content-Type', 'application/json') + self.send_header('Content-Length', str(len(data))) + self.end_headers() + self.wfile.write(data) + + def do_GET(self): + url = urllib.parse.urlparse(self.path) + query = dict(urllib.parse.parse_qsl(url.query)) + if url.path == '/minecraft/client': + return self.reply(200, services.discovery()) + if url.path == '/publickeys': + return self.reply(200, key_set) + if url.path.startswith('/users/profiles/minecraft/'): + name = urllib.parse.unquote(url.path.rsplit('/', 1)[1]) + return self.reply(200, {'id': account(name), 'name': name}) + if url.path == '/session/minecraft/hasJoined': + name = query.get('username') + with services.lock: + ok = services.joined.get(name) == query.get('serverId') + if ok: + return self.reply(200, {'id': account(name), 'name': name, 'properties': []}) + return self.reply(204) + self.reply(404, {'error': 'NotFound'}) + + def do_POST(self): + body = json.loads(self.rfile.read(int(self.headers.get('Content-Length', 0))) or b'{}') + if self.path != '/session/minecraft/join': + return self.reply(404, {'error': 'NotFound'}) + # The test client names its account instead of its id. + with services.lock: + services.joined[body.get('selectedProfile')] = body.get('serverId') + self.reply(204) + + self.server = ThreadingHTTPServer(('127.0.0.1', 0), Handler) + self.url = f'http://127.0.0.1:{self.server.server_address[1]}' + threading.Thread(target=self.server.serve_forever, daemon=True).start() + + def discovery(self): + def endpoints(**names): + return {'endpoints': {k: {'uri': self.url + v} for k, v in names.items()}} + return {'environment': 'mock', 'product': 'minecraft', 'discovery': { + 'authentication': endpoints(getPublicKeys='/publickeys'), + 'session': endpoints(join='/session/minecraft/join', verify='/session/minecraft/hasJoined', + getProfileById='/session/minecraft/profile/{profileId}'), + 'player': {'endpoints': {}}, + 'profiles': {'endpoints': {'getTexture': {'validUris': [self.url + '/texture/{textureId}']}, + 'getByName': {'uri': self.url + '/users/profiles/minecraft/{name}'}}}, + 'telemetry': {'endpoints': {}}, + }} + + def stop(self): + self.server.shutdown() + + +def player_id(name, scenario): + """The player's id: the account's online, UUIDUtil.createOfflinePlayerUUID offline.""" + if scenario == 'online': + return account(name) + digest = bytearray(hashlib.md5(f'OfflinePlayer:{name}'.encode()).digest()) + digest[6] = digest[6] & 0x0F | 0x30 + digest[8] = digest[8] & 0x3F | 0x80 + return digest.hex() + + +def dashed(u): + return f'{u[:8]}-{u[8:12]}-{u[12:16]}-{u[16:20]}-{u[20:]}' + + +def varint(n): + out = b'' + while True: + b = n & 0x7F + n >>= 7 + out += bytes([b | (0x80 if n else 0)]) + if not n: + return out + + +def status(port): + """The status response's JSON (ServerStatus).""" + def packet(body): + return varint(len(body)) + body + + def read_varint(s): + value = 0 + for i in range(5): + b = s.recv(1)[0] + value |= (b & 0x7F) << (7 * i) + if not b & 0x80: + return value + with socket.create_connection(('127.0.0.1', port), timeout=10) as s: + host = b'127.0.0.1' + s.sendall(packet(b'\x00' + varint(777) + varint(len(host)) + host + port.to_bytes(2, 'big') + b'\x01')) + s.sendall(packet(b'\x00')) + length = read_varint(s) + data = b'' + while len(data) < length: + data += s.recv(length - len(data)) + text = data[1:] + n, shift, i = 0, 0, 0 + while True: + b = text[i] + n |= (b & 0x7F) << shift + shift += 7 + i += 1 + if not b & 0x80: + break + return json.loads(text[i:i + n]) + + +# The servers' lines about chat, with their level. The thread is left out: +# vanilla handles acknowledgements on its network threads, named per platform. +CHAT_LINE = re.compile(r'^\[\d\d:\d\d:\d\d\] \[([^\]]+)\]: (.*(?:<\w+>|\[Not Secure\]|\[\w+\] |\* \w+ |secure chat|' + r'signable|Signed command|acknowledgement|profile key|chat session|expired chat|lost connection|' + r'Disconnecting).*)$') + + +def chat_lines(lines): + out = [] + for line in lines: + if m := CHAT_LINE.match(line): + text = re.sub(r'/127\.0\.0\.1:\d+', '/127.0.0.1:_', m.group(2)) + out.append(f'[{m.group(1).rsplit("/", 1)[-1]}] {text}') + return out + + +def run_side(label, scenario, command, directory, client): + port = free_port() + shutil.rmtree(directory, ignore_errors=True) + directory.mkdir(parents=True) + props = dict(PROPERTIES, **SCENARIOS[scenario], **{'server-port': str(port)}) + (directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items())) + (directory / 'eula.txt').write_text('eula=true\n') + # Alice and Carol are operators (/say needs it), by their ids. + ops = [{'uuid': dashed(player_id(name, scenario)), 'name': name, 'level': 4, 'bypassesPlayerLimit': False} + for name in ('Alice', 'Carol')] + (directory / 'ops.json').write_text(json.dumps(ops, indent=2)) + server = Server(command, directory, f'{label}-{scenario}') + try: + server.wait_for(r'Done \(', timeout=600) + # A server can drop a ping in its first moments (the network thread still starting): try again. + for attempt in range(5): + try: + enforces = status(port).get('enforcesSecureChat', False) + break + except (OSError, IndexError): + if attempt == 4: + raise + time.sleep(2) + out = directory.parent / f'{label}-{scenario}.txt' + client(port, scenario, out) + transcript = [f'status enforcesSecureChat {enforces}'] + out.read_text(encoding='utf-8').splitlines() + finally: + server.stop() + return transcript, chat_lines(server.lines) + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + classes = build / 'classes' + shutil.rmtree(classes, ignore_errors=True) + classes.mkdir() + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/SecureChatClient.java' + oh.run_logged(['javac', '--release', lock['minecraft']['java_major'], '-proc:none', '-nowarn', '-sourcepath', + lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle], + build, 'compile-java') + oh.run_logged(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], build, + 'compile-rust') + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + java = ['java', '-Xmx1g', '-Duser.timezone=UTC', '-cp', str(classes) + os.pathsep + dependencies, + 'dev.horizonlab.parity.SecureChatClient'] + oh.run_logged([*java, 'keys', build], build, 'keys') + services = Services(json.loads((build / 'publickeys.json').read_text())) + discovery = services.url + '/minecraft/client' + os.environ.update(HORIZON_NO_AUDIO='1', HORIZON_API_DISCOVERY=discovery) + servers = { + 'vanilla': ['java', '-Xmx2g', '-Duser.timezone=UTC', f'-Dminecraft.api.discovery.host={discovery}', + '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'], + 'horizon': [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'], + } + + def client(port, scenario, out): + oh.run_logged([*java, '127.0.0.1', port, build / 'services-private.der', out, scenario, services.url], + build, out.stem, timeout=300) + + differences = [] + try: + for scenario in SCENARIOS: + results = {} + for label, command in servers.items(): + results[label] = run_side(label, scenario, command, build / f'{label}-{scenario}', client) + transcript, log = results[label] + (build / f'{label}-{scenario}-all.txt').write_text('\n'.join(transcript + ['--- log'] + log) + '\n', + encoding='utf-8') + import difflib + for part, index in (('transcript', 0), ('log', 1)): + diff = [l for l in difflib.unified_diff(results['vanilla'][index], results['horizon'][index], + 'vanilla', 'horizon', n=0, lineterm='') + if l[:1] in '+-' and l[:3] not in ('+++', '---')] + if diff: + differences.append(f'{scenario} {part}:\n ' + '\n '.join(diff[:60])) + errors = [l for l in results['vanilla'][0] if l.startswith('error')] + if errors: + differences.append(f'{scenario}: the vanilla run failed: {errors}') + finally: + services.stop() + if differences: + raise AssertionError('\n'.join(differences) + f'\n(transcripts in {build})') + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + scope='Secure chat against vanilla\'s dedicated server: chat sessions validated against mock services keys, ' + 'signed chat with last seen and the signature cache, unsigned chat, /say /me /msg with signed and ' + 'unsigned arguments, mismatched and forged signatures, the broken chain, a bad acknowledgement; ' + 'enforce-secure-profile in online mode (status, login, refusals, a borrowed key); the chat log lines.')) + print('secure chat: transcripts and chat log lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') diff --git a/tools/test_server_dimension_players_parity.py b/tools/test_server_dimension_players_parity.py new file mode 100644 index 000000000..77e1428e9 --- /dev/null +++ b/tools/test_server_dimension_players_parity.py @@ -0,0 +1,150 @@ +#!/usr/bin/env python3 +"""Players in different dimensions on a dedicated server, against vanilla's, seen by vanilla clients. + +A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one flat +world vanilla generated first, with Alice and Bob as operators in creative. LocatorBarClient joins them both +(players made of vanilla's protocol code) and records, step by step, the other players each is sent and loses +(ClientboundAddEntityPacket, ClientboundRemoveEntitiesPacket) and the dimensions it respawns into +(ClientboundRespawnPacket), with the locator bar's waypoints, while they change dimensions (ServerPlayer.teleport +through `execute in ... run tp`, and an End portal): Bob goes to the Nether and back, Alice to the End, Bob +follows her there, both come back to the Overworld, then Bob steps into an End portal and comes back. Between +the steps each client sends its position (the reminder a still client sends every second). Each player's lines in each step are compared exactly. +""" +import os +from pathlib import Path +import platform +import shutil +import subprocess +import sys +import time + +try: + from . import lab, test_rust_parity as base + from .test_dedicated_server_parity import Server + from .test_locator_bar_parity import generate_world, server_dir +except ImportError: + import lab + import test_rust_parity as base + from test_dedicated_server_parity import Server + from test_locator_bar_parity import generate_world, server_dir + +NAME = 'server-dimension-players-parity' +NETHER, END, OVERWORLD = 'minecraft:the_nether', 'minecraft:the_end', 'minecraft:overworld' +# (label, what the players do). +SCRIPT = [ + ('bob-nether', [f'cmd Bob execute in {NETHER} run tp @s 0.5 100 0.5']), + ('bob-back', [f'cmd Bob execute in {OVERWORLD} run tp @s 2.5 100 0.5']), + ('alice-end', [f'cmd Alice execute in {END} run tp @s 0.5 100 0.5']), + ('bob-end', [f'cmd Bob execute in {END} run tp @s 2.5 100 0.5']), + ('alice-back', [f'cmd Alice execute in {OVERWORLD} run tp @s 0.5 100 0.5']), + ('bob-back-again', [f'cmd Bob execute in {OVERWORLD} run tp @s 2.5 100 0.5']), + # Through an End portal (Entity.handlePortal), and back by command. + ('bob-portal', ['cmd Bob setblock 2 101 3 minecraft:end_portal', 'cmd Bob tp @s 2.5 101 3.5']), + ('bob-portal-back', [f'cmd Bob execute in {OVERWORLD} run tp @s 2.5 100 0.5']), +] +STEP_SECONDS = 5.0 + + +def run_side(label, server_command, client_command, build, world): + directory = build / label + port = server_dir(directory, world) + out = build / f'{label}.txt' + out.unlink(missing_ok=True) + server = Server(server_command, directory, label) + try: + server.wait_for(r'Done \(') + with (build / f'{label}-client.log').open('w') as log: + client = subprocess.Popen([*map(str, client_command), '127.0.0.1', str(port), str(out)], + cwd=lab.ROOT, stdin=subprocess.PIPE, stdout=log, stderr=subprocess.STDOUT, + text=True) + + def tell(line): + client.stdin.write(line + '\n') + client.stdin.flush() + try: + tell('players on') + tell('step joins') + for name, x in (('Alice', 0.5), ('Bob', 2.5)): + tell(f'join {name}') + server.wait_for(rf'{name} joined the game', timeout=120) + tell(f'fly {name} on') + tell(f'cmd {name} tp @s {x} 100 0.5') + time.sleep(STEP_SECONDS) + for step, actions in SCRIPT: + tell(f'step {step}') + for action in actions: + tell(action) + # Standing still, a client still sends its position every 20 ticks + # (LocalPlayer.sendPosition): the server then tracks to it what it has sent the + # chunks of since (ChunkMap.move). + time.sleep(STEP_SECONDS / 2) + tell('still Alice') + tell('still Bob') + time.sleep(STEP_SECONDS / 2) + tell('end') + client.wait(timeout=60) + finally: + if client.poll() is None: + client.kill() + finally: + server.stop() + if not out.exists(): + raise AssertionError(f'{label}: the client wrote no transcript (see {build}/{label}-client.log)') + return out.read_text(encoding='utf-8').splitlines() + + +def shown(lines): + """Each player's adds, removes and respawns, without the waypoints (the locator bar harness has those).""" + return [l for l in lines if l.split(' ', 2)[2:3] and l.split(' ', 2)[2].split(' ')[0] in ('add', 'remove', 'respawn')] + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java' + classes = build / 'classes' + shutil.rmtree(classes, ignore_errors=True) + classes.mkdir() + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-nowarn', '-sourcepath', + lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle], + cwd=lab.ROOT, check=True, capture_output=True) + subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, + check=True, capture_output=True) + import json + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] + horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + client = ['java', '-Xmx1g', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.LocatorBarClient'] + + world = generate_world(vanilla_server, build) + vanilla_all = run_side('vanilla', vanilla_server, client, build, world) + horizon_all = run_side('horizon', horizon_server, client, build, world) + errors = [l for l in vanilla_all + horizon_all if ' error ' in f' {l} ' or 'disconnected' in l] + vanilla, horizon = shown(vanilla_all), shown(horizon_all) + if errors or vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=1, lineterm='')) + raise AssertionError(f'{len(errors)} errors, transcripts differ (see {build}/vanilla.txt and horizon.txt):\n' + + '\n'.join(errors[:10]) + '\n' + diff[:6000]) + if sum(' respawn ' in l for l in vanilla) < 6: + raise AssertionError(f'vanilla sent too few respawns: the script did not run (see {build})') + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), + scope='Two players on a dedicated server changing dimensions (Nether, End, back): the other players each ' + 'is sent and loses, and the dimensions it respawns into.')) + print(f'server dimension players: {len(vanilla)} lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') diff --git a/tools/test_server_entities_parity.py b/tools/test_server_entities_parity.py new file mode 100644 index 000000000..500497275 --- /dev/null +++ b/tools/test_server_entities_parity.py @@ -0,0 +1,198 @@ +#!/usr/bin/env python3 +"""Entities other than mobs and items, tracked to a vanilla client, against vanilla's dedicated server. + +A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one flat +world vanilla generated first, with the same server.properties (offline, view distance 4). A vanilla-codec +client (VanillaProtocolClient in its entity mode) joins, and once its chunks are in it asks for the scene: the +console freezes the ticks, builds a wall and a rail and summons, around the spawn, every kind of minecart (one +set rolling along the rail), boats and a raft (one falling onto the ground), armor stands (posed, small, with +arms, wearing a helmet, one falling), a chicken jockey, a slime, a pig whose scale `/attribute` sets, item +frames (one holding a turned diamond), paintings, primed TNT (one falling), a floating falling block, an arrow +that lands and an end crystal. The client records for 20 seconds, the ticks running for 200 more after 3, what +ClientPacketListener would make of the packets (EntityTranscript): each entity's spawn +packet (position, rotations, data, motion), the entity data it was paired with and all it was sent since, its +equipment, its attribute instances (ClientboundUpdateAttributesPacket: the ones its first tick creates, the +type's set by finalizeSpawn, the baby speed modifier, effects', equipment's and /attribute's) and passengers, and +where its position syncs leave it; and the attribute instances the player itself is sent from its login on. Both +transcripts must match exactly, entity by entity (named by type and the block it was added in), once the amounts +of vanilla's random spawn modifiers are normalised (only client-syncable attributes are sent, so follow range and +knockback resistance never show). +""" +import os +from pathlib import Path +import platform +import re +import shutil +import subprocess +import sys +import time + +try: + from . import lab, test_rust_parity as base + from .test_dedicated_server_parity import Server + from .test_locator_bar_parity import generate_world, server_dir +except ImportError: + import lab + import test_rust_parity as base + from test_dedicated_server_parity import Server + from test_locator_bar_parity import generate_world, server_dir + +NAME = 'server-entities-parity' +# Ticks the scene runs for once it is summoned: 10 seconds, within the client's 20. +STEPS = 200 +PLAYER = 'Tester' + +# Around the spawn at 0 -60 0 on the flat world's grass: a wall to the north for the frames and paintings, +# a rail to the south. +SCENE = [ + 'fill -7 -60 -6 7 -57 -6 minecraft:stone', + 'fill -7 -60 9 7 -60 9 minecraft:rail', + # Minecarts. + 'summon minecraft:minecart -5.5 -60 3.5', + 'summon minecraft:chest_minecart -3.5 -60 3.5', + 'summon minecraft:furnace_minecart -1.5 -60 3.5', + 'summon minecraft:hopper_minecart 1.5 -60 3.5', + 'summon minecraft:tnt_minecart 3.5 -60 3.5', + 'summon minecraft:command_block_minecart 5.5 -60 1.5 {Command:"say hi"}', + 'summon minecraft:minecart -6.5 -60 9.5 {Motion:[0.5d,0d,0d]}', + # Boats. + 'summon minecraft:oak_boat -5.5 -60 6.5 {Rotation:[45f,0f]}', + 'summon minecraft:birch_chest_boat -2.5 -60 6.5', + 'summon minecraft:bamboo_raft 0.5 -60 6.5', + 'summon minecraft:oak_boat 4.5 -56 6.5', + # Armor stands. + 'summon minecraft:armor_stand -2.5 -60 -2.5 {Rotation:[90f,0f]}', + 'summon minecraft:armor_stand 6.5 -57 -2.5', + 'summon minecraft:armor_stand -4.5 -60 -2.5 {ShowArms:1b,Small:1b,NoBasePlate:1b,' + 'Pose:{Head:[10f,20f,0f],RightArm:[-30f,0f,15f]},equipment:{head:{id:"minecraft:diamond_helmet",count:1}}}', + # A chicken jockey (passengers). + 'summon minecraft:chicken -6.5 -60 -2.5 {NoAI:1b,Passengers:[{id:"minecraft:husk",IsBaby:1b,NoAI:1b}]}', + # Attributes: a slime's size sets its bases, /attribute a pig's scale. + 'summon minecraft:slime 2.5 -60 1.5 {Size:1,NoAI:1b}', + 'summon minecraft:pig -0.5 -60 1.5 {NoAI:1b}', + 'attribute @e[type=minecraft:pig,limit=1,x=-0.5,y=-60,z=1.5,distance=..1] minecraft:scale base set 1.5', + # Frames and paintings on the wall's south face. + 'summon minecraft:item_frame -3.5 -59 -4.97 {block_pos:[I;-4,-59,-5],Facing:3b,' + 'Item:{id:"minecraft:diamond",count:1},ItemRotation:3b}', + 'summon minecraft:glow_item_frame -1.5 -59 -4.97 {block_pos:[I;-2,-59,-5],Facing:3b}', + 'summon minecraft:painting 1.5 -59 -4.97 {block_pos:[I;1,-59,-5],facing:0b,variant:"minecraft:kebab"}', + 'summon minecraft:painting 4 -59 -4.97 {block_pos:[I;4,-59,-5],facing:0b,variant:"minecraft:courbet"}', + # Primed TNT that won't go off (one falling), a floating falling block, an arrow that lands, an end crystal. + 'summon minecraft:tnt 6.5 -60 6.5 {fuse:2000s}', + 'summon minecraft:tnt 6.5 -56 4.5 {fuse:2000s}', + 'summon minecraft:falling_block 2.5 -58 -0.5 {BlockState:{Name:"minecraft:sand"},NoGravity:1b}', + 'summon minecraft:arrow 0.5 -55 -1.5 {Motion:[0.3d,0d,0d]}', + 'summon minecraft:end_crystal -6.5 -60 6.5 {ShowBottom:0b}', +] + + +# finalizeSpawn's random modifiers on client-syncable attributes (a leader zombie's max health); their amounts +# differ from one server to the other. +RANDOM_MODIFIERS = re.compile(r'(id=minecraft:(?:random_spawn_bonus|leader_zombie_bonus), amount=)[^,]+') + + +def scene_lines(lines): + """The transcript's entities in the scene's area (the world's own animals and slimes wander elsewhere), the + amounts of random spawn modifiers normalised.""" + out = [] + for line in lines: + m = re.match(r'play-entities entity \S+ (-?\d+),-?\d+,(-?\d+)', line) + if m and -8 <= int(m.group(1)) <= 8 and -7 <= int(m.group(2)) <= 10 or line.startswith('play-entities self '): + out.append(RANDOM_MODIFIERS.sub(r'\1~', line)) + return out + + +def run_side(label, server_command, client_command, build, world): + directory = build / label + port = server_dir(directory, world) + out = build / f'{label}-client.txt' + out.unlink(missing_ok=True) + server = Server(server_command, directory, label) + try: + server.wait_for(r'Done \(') + with (build / f'{label}-client.log').open('w') as log: + client = subprocess.Popen([*map(str, client_command), '127.0.0.1', str(port), PLAYER, str(out), 'entities'], + cwd=lab.ROOT, stdout=log, stderr=subprocess.STDOUT, text=True) + try: + server.wait_for(rf'<{PLAYER}> summon the entities', timeout=180) + # Frozen, so that every entity is paired as it was summoned whenever each server tracks it in + # its tick; then they fall and roll. + server.console('tick freeze') + for command in SCENE: + server.console(command) + time.sleep(3) + # A fixed number of ticks, so that what still changes (a TNT fuse) ends the same. + server.console(f'tick step {STEPS}') + client.wait(timeout=120) + finally: + if client.poll() is None: + client.kill() + finally: + server.stop() + if not out.exists(): + raise AssertionError(f'{label}: the client wrote no transcript (see {build}/{label}-client.log)') + lines = out.read_text(encoding='utf-8').splitlines() + (build / f'{label}.txt').write_text('\n'.join(lines) + '\n', encoding='utf-8') + return lines + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java' + classes = build / 'classes' + shutil.rmtree(classes, ignore_errors=True) + classes.mkdir() + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-nowarn', '-sourcepath', + lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle], + cwd=lab.ROOT, check=True, capture_output=True) + # SERVER_ENTITIES_SIDES=vanilla (or horizon) runs one side and reads the other's last transcript. + sides = [s for s in os.environ.get('SERVER_ENTITIES_SIDES', 'vanilla,horizon').split(',') if s] + if 'horizon' in sides: + subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, + check=True, capture_output=True) + import json + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] + horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + client = ['java', '-Xmx1g', '-Duser.timezone=UTC', '-cp', str(classes) + os.pathsep + dependencies, + 'dev.horizonlab.parity.VanillaProtocolClient'] + + world = generate_world(vanilla_server, build) + results = {} + for label, command in [('vanilla', vanilla_server), ('horizon', horizon_server)]: + if label in sides: + results[label] = run_side(label, command, client, build, world) + else: + results[label] = (build / f'{label}.txt').read_text(encoding='utf-8').splitlines() + vanilla, horizon = scene_lines(results['vanilla']), scene_lines(results['horizon']) + errors = [l for l in results['vanilla'] + results['horizon'] if l.startswith('error')] + if errors or vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=0, lineterm='')) + raise AssertionError(f'{len(errors)} errors, transcripts differ (see {build}/vanilla.txt and horizon.txt):\n' + + '\n'.join(errors[:10]) + '\n' + diff[:8000]) + if len(vanilla) < len(SCENE) - 2: + raise AssertionError(f'only {len(vanilla)} entities tracked: the scene did not run (see {build})') + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + entities=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), + scope='Minecarts (one rolling), boats (one falling), armor stands, a chicken jockey, a slime, a pig with a ' + 'scale, item frames, paintings, primed TNT, a falling block, an arrow and an end crystal ' + 'tracked to a vanilla-codec client by a dedicated server: spawn packets, entity data, equipment, ' + 'attributes, passengers and the positions the syncs leave them at.')) + print(f'server entities: {len(vanilla)} entities identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') diff --git a/tools/test_server_ops_feedback_parity.py b/tools/test_server_ops_feedback_parity.py new file mode 100644 index 000000000..2dd405517 --- /dev/null +++ b/tools/test_server_ops_feedback_parity.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Command feedback the operators get on a dedicated server (`[Server: ...]`), against vanilla's. + +CommandSourceStack.broadcastToAdmins: a command's success that vanilla sends with `broadcast` goes, gray and +italic as `[name: message]`, to every other operator while `send_command_feedback` is on, including the +console's own commands when `broadcast-console-to-ops` is (DedicatedServer.shouldInformAdmins). + +A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one flat +world vanilla generated first, with the same server.properties and Alice and Bob as operators. LocatorBarClient +joins them both (players made of vanilla's protocol code) and records the system chat each receives while, step +by step, the console sets the time, asks for the difficulty (a query, told to nobody), whitelists a player, +deops Bob (who stops hearing of it) and ops him again, turns `send_command_feedback` off and on and saves, and +Alice runs a command herself (Bob hears of it as an operator). Then again with `broadcast-console-to-ops=false`. +Each player's chat in each step is compared exactly. +""" +import os +from pathlib import Path +import platform +import shutil +import subprocess +import sys +import time + +try: + from . import lab, test_rust_parity as base + from .test_dedicated_server_parity import Server + from .test_locator_bar_parity import generate_world, server_dir +except ImportError: + import lab + import test_rust_parity as base + from test_dedicated_server_parity import Server + from test_locator_bar_parity import generate_world, server_dir + +NAME = 'server-ops-feedback-parity' +# (label, what the console does, what the players do). +BROADCAST = [ + ('console-time', ['time set 1000'], []), + ('console-query', ['difficulty'], []), + ('console-whitelist', ['whitelist add Alice'], []), + ('console-deop', ['deop Bob'], []), + ('bob-not-op', ['time set 2000'], []), + ('alice-bob-not-op', [], ['cmd Alice time set 3000']), + ('console-op', ['op Bob'], []), + ('alice-bob-op', [], ['cmd Alice time set 4000']), + ('feedback-off', ['gamerule send_command_feedback false'], []), + ('console-feedback-off', ['time set 5000'], []), + ('feedback-on', ['gamerule send_command_feedback true'], []), + ('console-save', ['save-all'], []), +] +QUIET = [ + ('console-time', ['time set 1000'], []), + ('console-whitelist', ['whitelist add Alice'], []), + ('alice', [], ['cmd Alice time set 3000']), +] +CASES = {'broadcast': ({}, BROADCAST), 'quiet': ({'broadcast-console-to-ops': 'false'}, QUIET)} +STEP_SECONDS = 2.0 + + +def run_side(label, case, server_command, client_command, build, world): + properties, script = CASES[case] + directory = build / label / case + directory.parent.mkdir(parents=True, exist_ok=True) + port = server_dir(directory, world) + with (directory / 'server.properties').open('a', encoding='iso-8859-1') as f: + f.writelines(f'{k}={v}\n' for k, v in properties.items()) + out = build / f'{label}-{case}.txt' + out.unlink(missing_ok=True) + server = Server(server_command, directory, f'{label}-{case}') + try: + server.wait_for(r'Done \(') + with (build / f'{label}-{case}-client.log').open('w') as log: + client = subprocess.Popen([*map(str, client_command), '127.0.0.1', str(port), str(out)], + cwd=lab.ROOT, stdin=subprocess.PIPE, stdout=log, stderr=subprocess.STDOUT, + text=True) + + def tell(line): + client.stdin.write(line + '\n') + client.stdin.flush() + try: + for name in ('Alice', 'Bob'): + tell(f'join {name}') + server.wait_for(rf'{name} joined the game', timeout=120) + time.sleep(STEP_SECONDS) + tell('chat on') + for step, console, actions in script: + tell(f'step {step}') + for command in console: + server.console(command) + for action in actions: + tell(action) + time.sleep(STEP_SECONDS) + tell('end') + client.wait(timeout=60) + finally: + if client.poll() is None: + client.kill() + finally: + server.stop() + if not out.exists(): + raise AssertionError(f'{label} {case}: the client wrote no transcript (see {build}/{label}-{case}-client.log)') + lines = out.read_text(encoding='utf-8').splitlines() + errors = [l for l in lines if ' error ' in f' {l} ' or 'disconnected' in l] + return [f'{case} {l}' for l in lines if l.split(' ', 2)[2:3] and l.split(' ', 2)[2].startswith('chat ')], errors + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java' + classes = build / 'classes' + shutil.rmtree(classes, ignore_errors=True) + classes.mkdir() + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-nowarn', '-sourcepath', + lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle], + cwd=lab.ROOT, check=True, capture_output=True) + subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, + check=True, capture_output=True) + import json + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] + horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + client = ['java', '-Xmx1g', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.LocatorBarClient'] + + world = generate_world(vanilla_server, build) + sides = {'vanilla': ([], []), 'horizon': ([], [])} + for case in CASES: + for label, command in [('vanilla', vanilla_server), ('horizon', horizon_server)]: + lines, errors = run_side(label, case, command, client, build, world) + sides[label][0].extend(lines) + sides[label][1].extend(errors) + (vanilla, vanilla_errors), (horizon, horizon_errors) = sides['vanilla'], sides['horizon'] + (build / 'vanilla.txt').write_text('\n'.join(vanilla) + '\n', encoding='utf-8') + (build / 'horizon.txt').write_text('\n'.join(horizon) + '\n', encoding='utf-8') + errors = vanilla_errors + horizon_errors + if errors or vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=1, lineterm='')) + raise AssertionError(f'{len(errors)} errors, transcripts differ (see {build}/vanilla.txt and horizon.txt):\n' + + '\n'.join(errors[:10]) + '\n' + diff[:6000]) + if sum('[Server' in l or 'chat.type.admin' in l for l in vanilla) < 5: + raise AssertionError(f'vanilla sent the operators too little: the script did not run (see {build})') + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), + scope='The system chat two operators get on a dedicated server while the console and one of them run ' + 'commands: broadcastToAdmins for the console (broadcast-console-to-ops on and off) and a player, ' + 'queries, deop and op, send_command_feedback off and on, and save-all.')) + print(f'server ops feedback: {len(vanilla)} chat lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') diff --git a/tools/test_server_player_targets_parity.py b/tools/test_server_player_targets_parity.py new file mode 100644 index 000000000..f8f457143 --- /dev/null +++ b/tools/test_server_player_targets_parity.py @@ -0,0 +1,199 @@ +#!/usr/bin/env python3 +"""Commands acting on other players of a dedicated server, against vanilla's, seen by vanilla clients. + +EntitySelector.findPlayers and EntityArgument's player targets: names, UUIDs, `@a`, `@p`, `@r`, `@s` under +`execute as`, from the console and from an operator, with `give`, `gamemode`, `tp` (to a place, to a player, with a +rotation, and into another level by `execute in`, the console's own level or the destination's), `effect give` and +`effect clear`, and `kill`. + +A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one flat world +vanilla generated first, with Alice and Bob as operators in creative. LocatorBarClient joins them both (players made +of vanilla's protocol code) and records, step by step, the system chat each receives and what the server changes +about it: game mode events, teleports, inventory slots, effects, its death (with the death message), the other +players it is sent or loses and the levels it respawns into. Each player's lines in each step are compared exactly, +in sorted order (the packets of one step may leave in another order within the tick). The locator bar's waypoints +and what a death does besides its messages are left to their own harnesses. +""" +import hashlib +import os +from pathlib import Path +import platform +import shutil +import subprocess +import sys +import time +import uuid as uuidlib + +try: + from . import lab, test_rust_parity as base + from .test_dedicated_server_parity import Server + from .test_locator_bar_parity import generate_world, server_dir +except ImportError: + import lab + import test_rust_parity as base + from test_dedicated_server_parity import Server + from test_locator_bar_parity import generate_world, server_dir + +NAME = 'server-player-targets-parity' +NETHER, OVERWORLD = 'minecraft:the_nether', 'minecraft:overworld' + + +def offline_uuid(name): + """UUIDUtil.createOfflinePlayerUUID.""" + digest = bytearray(hashlib.md5(f'OfflinePlayer:{name}'.encode()).digest()) + digest[6] = (digest[6] & 0x0F) | 0x30 + digest[8] = (digest[8] & 0x3F) | 0x80 + return str(uuidlib.UUID(bytes=bytes(digest))) + + +# (label, what the console does, what the players do). +SCRIPT = [ + ('console-give', ['give Bob minecraft:diamond 3'], []), + ('console-give-all', ['give @a minecraft:apple'], []), + ('alice-give-bob', [], ['cmd Alice give Bob minecraft:stick 2']), + ('console-give-uuid', [f'give {offline_uuid("Bob")} minecraft:iron_ingot'], []), + ('console-give-nearest', ['give @p minecraft:emerald'], []), + ('alice-give-random', [], ['cmd Alice give @r[name=Bob] minecraft:gold_ingot']), + ('console-gamemode', ['gamemode survival Bob'], []), + ('alice-gamemode-bob', [], ['cmd Alice gamemode creative Bob']), + ('alice-gamemode-all', [], ['cmd Alice gamemode adventure @a']), + ('console-gamemode-back', ['gamemode creative @a'], []), + ('console-tp', ['tp Bob 8.5 -60 8.5'], []), + ('alice-tp-bob-to-alice', [], ['cmd Alice tp Bob Alice']), + ('console-tp-rotation', ['tp Bob 4.5 -60 0.5 90 10'], []), + ('bob-tp-self', [], ['cmd Bob tp @s 6.5 -60 0.5']), + ('console-execute-as', ['execute as Bob run tp @s ~ ~ ~2'], []), + ('console-execute-at', ['execute at Bob run tp Alice ~1 ~ ~'], []), + ('alice-execute-as-all', [], ['cmd Alice execute as @a run give @s minecraft:bread']), + ('console-effect', ['effect give Bob minecraft:speed 30 1'], []), + ('alice-effect-all', [], ['cmd Alice effect give @a minecraft:haste infinite']), + ('console-effect-clear', ['effect clear Bob'], []), + ('alice-effect-clear-all', [], ['cmd Alice effect clear @a minecraft:haste']), + ('console-nether', [f'execute in {NETHER} run tp Bob 0.5 100 0.5'], []), + ('console-tp-home', ['tp Bob 4.5 -60 0.5'], []), + ('alice-nether', [], [f'cmd Alice execute in {NETHER} run tp Bob 0.5 100 0.5']), + ('console-tp-to-alice', ['tp Bob Alice'], []), + ('alice-kill-missing', [], ['cmd Alice kill Carol']), + ('console-kill', ['kill Bob'], []), +] +# What the players' locator bars show has its own harness (test_locator_bar_parity), and what a death does +# besides its messages (drops, the body leaving) the player combat ones: here only the messages of `kill`. +WAYPOINTS = ('track ', 'untrack ', 'update ') +DEATH_STEP = 'step=console-kill' +STEP_SECONDS = 2.5 + + +def run_side(label, server_command, client_command, build, world): + directory = build / label + port = server_dir(directory, world) + out = build / f'{label}.txt' + out.unlink(missing_ok=True) + server = Server(server_command, directory, label) + try: + server.wait_for(r'Done \(') + with (build / f'{label}-client.log').open('w') as log: + client = subprocess.Popen([*map(str, client_command), '127.0.0.1', str(port), str(out)], + cwd=lab.ROOT, stdin=subprocess.PIPE, stdout=log, stderr=subprocess.STDOUT, + text=True) + + def tell(line): + client.stdin.write(line + '\n') + client.stdin.flush() + try: + for name, x in (('Alice', 0.5), ('Bob', 4.5)): + tell(f'join {name}') + server.wait_for(rf'{name} joined the game', timeout=120) + tell(f'fly {name} on') + tell(f'cmd {name} tp @s {x} -60 0.5 0 0') + time.sleep(STEP_SECONDS) + for flag in ('chat', 'state', 'players'): + tell(f'{flag} on') + for step, console, actions in SCRIPT: + tell(f'step {step}') + for command in console: + server.console(command) + for action in actions: + tell(action) + time.sleep(STEP_SECONDS) + for name in ('Alice', 'Bob'): + tell(f'still {name}') + tell('end') + client.wait(timeout=60) + finally: + if client.poll() is None: + client.kill() + finally: + server.stop() + if not out.exists(): + raise AssertionError(f'{label}: the client wrote no transcript (see {build}/{label}-client.log)') + lines = out.read_text(encoding='utf-8').splitlines() + errors = [l for l in lines if ' error ' in f' {l} ' or 'disconnected' in l] + steps = {} + for line in lines: + who, step, rest = line.split(' ', 2) + # At the death, Alice picks up Bob's drops (and gets their advancements) in vanilla. + death_side = step == DEATH_STEP and (not rest.startswith(('chat ', 'killed ')) + or 'chat.type.advancement' in rest) + if rest.startswith(WAYPOINTS) or death_side: + continue + if step != 'step=start': + steps.setdefault((who, step), []).append(line) + order = {f'step={s}': i for i, (s, _, _) in enumerate(SCRIPT)} + keys = sorted(steps, key=lambda k: (k[0], order.get(k[1], -1))) + return [l for k in keys for l in sorted(steps[k])], errors + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java' + classes = build / 'classes' + shutil.rmtree(classes, ignore_errors=True) + classes.mkdir() + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-nowarn', '-sourcepath', + lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle], + cwd=lab.ROOT, check=True, capture_output=True) + subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, + check=True, capture_output=True) + import json + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] + horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + client = ['java', '-Xmx1g', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.LocatorBarClient'] + + world = generate_world(vanilla_server, build) + vanilla, vanilla_errors = run_side('vanilla', vanilla_server, client, build, world) + horizon, horizon_errors = run_side('horizon', horizon_server, client, build, world) + (build / 'vanilla.txt').write_text('\n'.join(vanilla) + '\n', encoding='utf-8') + (build / 'horizon.txt').write_text('\n'.join(horizon) + '\n', encoding='utf-8') + errors = vanilla_errors + horizon_errors + if errors or vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=1, lineterm='')) + raise AssertionError(f'{len(errors)} errors, transcripts differ (see {build}/vanilla.txt and horizon.txt):\n' + + '\n'.join(errors[:10]) + '\n' + diff[:8000]) + for needle in ('gamemode 0', 'minecraft:diamond x3', 'effect minecraft:speed', 'respawn ' + NETHER, + 'death.attack.genericKill'): + if not any(needle in l for l in vanilla): + raise AssertionError(f'vanilla never sent {needle!r}: the script did not run (see {build})') + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), + scope='Commands from the console and an operator acting on another player of a dedicated server: names, ' + 'UUIDs, @a, @p, @r and @s under execute as/at, with give, gamemode, tp (place, player, rotation, ' + 'execute in, across levels), effect give and clear and kill, as the players see them.')) + print(f'server player targets: {len(vanilla)} lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') diff --git a/tools/test_server_world_gen_parity.py b/tools/test_server_world_gen_parity.py new file mode 100644 index 000000000..f55e55809 --- /dev/null +++ b/tools/test_server_world_gen_parity.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +"""A new world's generator settings from server.properties, against vanilla's dedicated server. + +DedicatedServerProperties.WorldDimensionData makes a world that doesn't exist yet from `level-type` (a world +preset: `flat`, `minecraft:normal`, or a name it can't find, which it warns about and replaces with +`minecraft:normal`), a flat world's `generator-settings` (FlatLevelGeneratorSettings: its layers, biome, +lakes and features; a layer that doesn't parse is left out and logged, and settings without layers fall back +to the preset's classic ones) and `generate-structures`. + +Each case starts vanilla's server and Horizon's (`horizon-client --dedicated --nogui`) in a new folder, waits +for `Done` and stops them. Compared exactly: the world's `data/minecraft/world_gen_settings.dat`, tag types +included, and the warnings and errors logged while the world's settings are read (the `Worker-Main` lines; +the worker's number is left out). In a world without structures (`generate-structures=false`), also what +`locate structure` answers on the console for a village and a mineshaft (ChunkGenerator.findNearestMapStructure +finds none) and how many of the saved chunks hold structure starts or references (none: +ChunkStatusTasks.generateStructureStarts does nothing). + +Horizon generates the normal and flat presets only: `large_biomes`, `amplified`, `single_biome_surface` and +`debug_all_block_states` aren't tested. +""" +import json +import os +from pathlib import Path +import platform +import re +import shutil +import subprocess +import sys +import time + +try: + from . import anvil_dump, lab, test_rust_parity as base + from .test_dedicated_server_parity import Server, free_port +except ImportError: + import anvil_dump + import lab + import test_rust_parity as base + from test_dedicated_server_parity import Server, free_port + +NAME = 'server-world-gen-parity' +PROPERTIES = { + 'online-mode': 'false', + 'view-distance': '2', + 'simulation-distance': '2', + 'spawn-protection': '0', +} +CASES = { + # server.properties' default generator-settings ({}) has no layers: the preset's classic ones. + 'flat': {'level-type': 'flat', 'level-seed': '1'}, + 'flat-layers': { + 'level-type': 'minecraft:flat', + 'level-seed': '-7', + 'generator-settings': json.dumps({ + 'layers': [{'block': 'minecraft:bedrock', 'height': 1}, {'block': 'stone', 'height': 10}, + {'block': 'minecraft:no_such_block', 'height': 2}, {'block': 'sand', 'height': 3}], + 'biome': 'minecraft:desert', 'lakes': True, 'features': True}, separators=(',', ':')), + }, + 'flat-biome': { + 'level-type': 'FLAT', + 'level-seed': 'flat', + 'generator-settings': '{"layers":[{"block":"minecraft:glass","height":5}],"biome":"nowhere"}', + }, + 'normal': {'level-type': 'minecraft:normal', 'level-seed': 'abc', 'generate-structures': 'false'}, + 'unknown': {'level-type': 'Some Type', 'level-seed': '42'}, +} +WORKER = re.compile(r'\[Worker-Main-\d+/(WARN|ERROR)\]: (.*)') +LOCATE = ['locate structure #minecraft:village', 'locate structure minecraft:mineshaft'] +REPLY = re.compile(r'\]: (?:System chat: )?((?:Could not find|The nearest|Unknown|Incorrect).*)') + + +def console_reply(server, command, timeout=300): + """The first command reply the console logs after `command`.""" + start = len(server.lines) + server.console(command) + deadline = time.time() + timeout + while time.time() < deadline: + for line in server.lines[start:]: + if m := REPLY.search(line): + return m.group(1) + time.sleep(0.2) + raise AssertionError(f'no reply to {command!r} in {timeout} s:\n' + '\n'.join(server.lines[-20:])) + + +def structure_chunks(world): + """How many saved overworld chunks hold structure starts, and how many references.""" + starts = references = 0 + for region in sorted((world / 'region').glob('*.mca')): + for chunk in anvil_dump.region_chunks(region).values(): + structures = anvil_dump.plain(chunk, True).get('structures', {}) + starts += bool(structures.get('starts')) + references += bool(structures.get('References')) + return f'chunks with structure starts: {starts}, with references: {references}' + + +def escape(value): + """java.util.Properties.store's escaping of a value.""" + return value.replace('\\', '\\\\').replace(':', '\\:').replace('=', '\\=').replace('#', '\\#') + + +def run_case(label, command, build, case): + directory = build / label / case + if directory.exists(): + shutil.rmtree(directory) + directory.mkdir(parents=True) + props = dict(PROPERTIES, **CASES[case], **{'server-port': str(free_port())}) + (directory / 'server.properties').write_text(''.join(f'{k}={escape(v)}\n' for k, v in props.items()), + encoding='iso-8859-1') + (directory / 'eula.txt').write_text('eula=true\n') + server = Server(command, directory, f'{label}-{case}') + replies = [] + try: + server.wait_for(r'Done \(') + if CASES[case].get('generate-structures') == 'false': + replies = [f'{c}: {console_reply(server, c)}' for c in LOCATE] + finally: + server.stop() + out = [f'# {case}', *replies] + if replies: + out.append(structure_chunks(directory / 'world')) + out += [f'{m.group(1)}: {m.group(2)}' for l in server.lines if (m := WORKER.search(l))] + settings = anvil_dump.plain(anvil_dump.read_dat(directory / 'world/data/minecraft/world_gen_settings.dat'), + True, typed=True) + out += json.dumps(settings, indent=1, sort_keys=True).splitlines() + return out + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, + check=True, capture_output=True) + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', + 'nogui'] + horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + vanilla, horizon = [], [] + for case in CASES: + vanilla += run_case('vanilla', vanilla_server, build, case) + horizon += run_case('horizon', horizon_server, build, case) + (build / 'vanilla.txt').write_text('\n'.join(vanilla) + '\n', encoding='utf-8') + (build / 'horizon.txt').write_text('\n'.join(horizon) + '\n', encoding='utf-8') + if vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=2, lineterm='')) + raise AssertionError(f'world settings differ (see {build}/vanilla.txt and horizon.txt):\n' + diff[:6000]) + if not any('minecraft:flat' in l for l in vanilla) or not any(l.startswith('ERROR: ') for l in vanilla): + raise AssertionError(f'vanilla made no flat world or logged no error (see {build})') + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + cases=list(CASES), lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), + scope='a new dedicated-server world: level-type (flat, normal, unknown), a flat world\'s ' + 'generator-settings, generate-structures, as world_gen_settings.dat and the lines logged; without ' + 'structures, locate structure\'s replies and the saved chunks\' structure starts and references.')) + print(f'server world gen: {len(CASES)} cases, {len(vanilla)} lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') diff --git a/tools/test_tooltip_display_parity.py b/tools/test_tooltip_display_parity.py new file mode 100644 index 000000000..22b485320 --- /dev/null +++ b/tools/test_tooltip_display_parity.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +"""Compare item tooltip display visibility with the pinned vanilla client.""" +try: + from . import lab, oracle_harness as harness +except ImportError: + import lab, oracle_harness as harness + + +def cases(build): + values = [ + (False, False, False), + (False, False, True), + (False, True, True), + (True, False, False), + (True, True, False), + (True, True, True), + ] + path = build / 'cases.txt' + # Lowercase: Rust's bool parser takes only true/false (Java's parseBoolean ignores case). + path.write_text('\n'.join('\t'.join(str(v).lower() for v in row) for row in values) + '\n') + return path, len(values) + + +if __name__ == '__main__': + harness.differential( + 'tooltip-display-parity', 'VanillaTooltipDisplayOracle', 'tooltip_display_fixture', cases, + 'Diamond sword name and attribute tooltip visibility for hide_tooltip, hidden_components, and creative flags', + sources=[ + lab.ROOT / 'crates/horizon-client/src/game/stations.rs', + lab.ROOT / 'crates/horizon-client/src/item_tooltip.rs', + ], + expected_lines=6) diff --git a/tools/test_user_cache_parity.py b/tools/test_user_cache_parity.py new file mode 100644 index 000000000..621514cc8 --- /dev/null +++ b/tools/test_user_cache_parity.py @@ -0,0 +1,246 @@ +#!/usr/bin/env python3 +"""usercache.json and the console's player names, against vanilla's dedicated server. + +The console finds a player by name through the user cache (CachedUserNameToIdResolver): a name it holds +in any case, else the profile service's `getByName` (asked with the lower-case name), else, in offline +mode, the offline player of that lower-case name; what it finds is cached for a month and the file +rewritten, most recently used first. A small mock of the Minecraft services (dev only: the discovery +document and `getByName`, which knows one account) stands in for Mojang's; vanilla finds it through +`-Dminecraft.api.discovery.host`, Horizon through `HORIZON_API_DISCOVERY`. + +Each server starts in offline mode in a folder holding a usercache.json with a live entry, an expired +one, entries without a date, with a bad date, without an id, and not an object. The console then +whitelists, ops, deops and bans players: the cached one in another case, the expired one, the account the +service knows, names it doesn't know, a name too long to ask about. The server restarts in online mode, +where a name nobody knows is no player at all, and stops. Compared exactly: the console's replies, the +names asked of the service in order, and usercache.json, whitelist.json, ops.json and +banned-players.json after each run (the dates the servers wrote are placeholders). + +Players who join are added by the dedicated-server harness, which compares usercache.json too. +""" +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +import json +import os +from pathlib import Path +import platform +import re +import shutil +import subprocess +import sys +import threading +import time +import urllib.parse + +try: + from . import lab, test_rust_parity as base + from .test_dedicated_server_parity import Server, free_port +except ImportError: + import lab + import test_rust_parity as base + from test_dedicated_server_parity import Server, free_port + +NAME = 'user-cache-parity' +PROPERTIES = { + 'online-mode': 'false', + 'level-seed': '12345', + 'generate-structures': 'false', + 'view-distance': '2', + 'simulation-distance': '2', + 'spawn-protection': '0', + 'pause-when-empty-seconds': '-1', +} +NOTCH = '069a79f444e94726a5befca90e38aaf5' +SEEDED = [ + {'uuid': '853c80ef-3c37-49fd-aa49-938b674adae6', 'name': 'Seeded', 'expiresOn': '2099-01-01 00:00:00 +0000'}, + {'uuid': '61699b2e-d327-4a01-9f1e-0ea8c3f06bc6', 'name': 'Expired', 'expiresOn': '2001-02-03 04:05:06 +0000'}, + {'uuid': '00000000-0000-0000-0000-000000000001', 'name': 'NoDate'}, + {'uuid': '00000000-0000-0000-0000-000000000002', 'name': 'BadDate', 'expiresOn': 'tomorrow'}, + {'name': 'NoId', 'expiresOn': '2099-01-01 00:00:00 +0000'}, + 'not an object', + {'uuid': '00000000-0000-0000-0000-000000000003', 'name': 'Later', 'expiresOn': '2098-05-06 07:08:09 +0100'}, +] +# (console command, the reply it waits for) +OFFLINE = [ + ('whitelist add SEEDED', r'System chat: (Added|Player is already)'), + ('whitelist add Notch', r'System chat: (Added|Player is already)'), + ('op expired', r'System chat: (Made|Nothing changed)'), + ('ban Nobody_Here', r'System chat: (Banned|Nothing changed)'), + ('whitelist add NameMuchTooLongForMinecraft', r'System chat: (Added|Player is already)'), + ('deop NOTCH', r'System chat: (Made|Nothing changed)'), + ('whitelist add later', r'System chat: (Added|Player is already)'), + ('whitelist remove notch', r'System chat: (Removed|Player is not)'), +] +ONLINE = [ + ('whitelist add Somebody_Else', r'System chat: (Added|That player does not exist)'), + ('whitelist add nobody_here', r'System chat: (Added|Player is already)'), +] +FILES = ['usercache.json', 'whitelist.json', 'ops.json', 'banned-players.json'] + + +class Services: + """The mock discovery document and profile lookup by name.""" + + def __init__(self): + self.asked = [] + self.lock = threading.Lock() + services = self + + class Handler(BaseHTTPRequestHandler): + def log_message(self, *args): + pass + + def reply(self, status, body): + data = json.dumps(body).encode() + self.send_response(status) + self.send_header('Content-Type', 'application/json') + self.send_header('Content-Length', str(len(data))) + self.end_headers() + self.wfile.write(data) + + def do_GET(self): + url = urllib.parse.urlparse(self.path) + if url.path == '/minecraft/client': + return self.reply(200, services.discovery()) + if url.path.startswith('/profiles/name/'): + name = urllib.parse.unquote(url.path.removeprefix('/profiles/name/')) + with services.lock: + services.asked.append(name) + if name.lower() == 'notch': + return self.reply(200, {'id': NOTCH, 'name': 'Notch'}) + return self.reply(404, {'path': url.path, 'errorMessage': f"Couldn't find any profile with name {name}"}) + self.reply(404, {'error': 'NotFound', 'path': url.path}) + + self.server = ThreadingHTTPServer(('127.0.0.1', 0), Handler) + self.url = f'http://127.0.0.1:{self.server.server_address[1]}' + threading.Thread(target=self.server.serve_forever, daemon=True).start() + + def discovery(self): + def endpoints(**names): + return {'endpoints': {k: {'uri': self.url + v} for k, v in names.items()}} + return {'environment': 'mock', 'product': 'minecraft', 'discovery': { + 'authentication': endpoints(getPublicKeys='/publickeys'), + 'session': endpoints(join='/session/minecraft/join', verify='/session/minecraft/hasJoined', + getProfileById='/session/minecraft/profile/{profileId}'), + 'player': endpoints(), + 'profiles': endpoints(getByName='/profiles/name/{name}'), + 'telemetry': endpoints(), + }} + + def take(self): + with self.lock: + asked, self.asked = self.asked, [] + return asked + + def stop(self): + self.server.shutdown() + + +def write_properties(directory, online): + props = dict(PROPERTIES, **{'server-port': str(free_port()), 'online-mode': str(online).lower()}) + (directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items()), + encoding='iso-8859-1') + + +def files(directory): + out = [] + for name in FILES: + path = directory / name + if not path.exists(): + out.append(f'{name}: missing') + continue + text = path.read_text(encoding='utf-8') + # The dates written now: a month on for the cache, the ban's creation. + text = re.sub(r'"(expiresOn|created)": ?"20[2-3]\d-[^"]*"', r'"\1":"<now>"', text) + out.append(f'{name}: {text}') + return out + + +def wait_after(server, seen, pattern, timeout=60): + """Waits for a line matching `pattern` after the first `seen` lines.""" + deadline = time.time() + timeout + regex = re.compile(pattern) + while not any(regex.search(l) for l in server.lines[seen:]): + if time.time() > deadline or server.process.poll() is not None: + raise AssertionError(f'{server.log_path.name}: no reply matching {pattern!r}:\n' + + '\n'.join(server.lines[-20:])) + time.sleep(0.1) + + +def run(label, command, build, services, steps, online, transcript): + directory = build / label + write_properties(directory, online) + server = Server(command, directory, f'{label}-{"online" if online else "offline"}') + try: + server.wait_for(r'Done \(') + services.take() + for line, reply in steps: + seen = len(server.lines) + server.console(line) + wait_after(server, seen, reply) + replies = [l for l in server.lines[seen:] if 'System chat:' in l] + transcript.append(f'> {line}') + transcript += [re.sub(r'^\[[^]]*\] ', '', l) for l in replies] + transcript.append(f'asked: {services.take()}') + finally: + server.stop() + transcript += files(directory) + + +def run_side(label, command, build, services): + directory = build / label + if directory.exists(): + shutil.rmtree(directory) + directory.mkdir(parents=True) + (directory / 'eula.txt').write_text('eula=true\n') + (directory / 'usercache.json').write_text(json.dumps(SEEDED), encoding='utf-8') + transcript = [] + run(label, command, build, services, OFFLINE, False, transcript) + run(label, command, build, services, ONLINE, True, transcript) + return transcript + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, + check=True, capture_output=True) + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + services = Services() + discovery = services.url + '/minecraft/client' + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', f'-Dminecraft.api.discovery.host={discovery}', + '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] + horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + os.environ['HORIZON_API_DISCOVERY'] = discovery + try: + vanilla = run_side('vanilla', vanilla_server, build, services) + horizon = run_side('horizon', horizon_server, build, services) + finally: + services.stop() + (build / 'vanilla.txt').write_text('\n'.join(vanilla) + '\n', encoding='utf-8') + (build / 'horizon.txt').write_text('\n'.join(horizon) + '\n', encoding='utf-8') + if vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=1, lineterm='')) + raise AssertionError(f'transcripts differ (see {build}/vanilla.txt and horizon.txt):\n' + diff[:6000]) + if not any('Notch' in l for l in vanilla if l.startswith('usercache.json')): + raise AssertionError(f'the cache never held the looked-up account (see {build})') + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), + scope='usercache.json (load, expiry, most recently used order, Gson output) and the console finding ' + 'players by name through it and the profile service, offline and online.')) + print(f'user cache: {len(vanilla)} lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}') diff --git a/tools/test_world_movement_parity.py b/tools/test_world_movement_parity.py index 8cffb456c..e01a989f5 100644 --- a/tools/test_world_movement_parity.py +++ b/tools/test_world_movement_parity.py @@ -18,18 +18,28 @@ except ImportError: def corpus(facts): def bits(v):return struct.pack('>d',float(v)).hex() - def row(state,delta=(1,-0.08,0),step=0.6,grounded=True,context=2,offset=0,fail=-1,extra=(),entities=(),border=None): + def row(state,delta=(1,-0.08,0),step=0.6,grounded=True,context=2,offset=0,fail=-1,extra=(),entities=(),border=None,box=None): blocks=[[x+offset,-1,z,facts['defaults']['minecraft:stone']] for x in range(-2,4) for z in range(-2,3)] blocks.append([offset,0,0,state]);blocks.extend(extra) return dict(chunks=[[x,z] for x in range((offset>>4)-1,(offset>>4)+2) for z in range(-1,2)], blocks=blocks,context=context,y=bits(0),entities=[0,0],fail=fail,limit=100000,suffocating=False, - box=list(map(bits,[-0.6+offset,0,0.2,-0.2+offset,1.8,0.8])), + box=list(map(bits,box if box is not None else [-0.6+offset,0,0.2,-0.2+offset,1.8,0.8])), movement=dict(delta=list(map(bits,delta)),step=bits(step),grounded=grounded, entities=[list(map(bits,b)) for b in entities],border=None if border is None else list(map(bits,border)))) states=facts['states'] for state in range(len(states)): yield row(state,context=state%4) defaults=facts['defaults'] + falling_surfaces=[('minecraft:stone_slab',0.5),('minecraft:oak_stairs',1.0),('minecraft:snow',0.125), + ('minecraft:oak_fence',1.5),('minecraft:iron_bars',1.0),('minecraft:soul_sand',0.875), + ('minecraft:farmland',0.9375)] + for block_id,top in falling_surfaces: + state=defaults[block_id] + size=struct.unpack('>f',struct.pack('>f',0.98))[0] + half=size/2 + bottom=top+0.1 + box=[0.5-half,bottom,0.5-half,0.5+half,bottom+size,0.5+half] + yield row(state,delta=(0,-0.25,0),step=0,grounded=False,context=2,box=box) obstacles=[i for i,s in enumerate(states) if s['id'] in ['minecraft:oak_stairs','minecraft:stone_slab', 'minecraft:snow','minecraft:scaffolding','minecraft:powder_snow','minecraft:water', 'minecraft:shulker_box','minecraft:moving_piston','minecraft:piston','minecraft:soul_sand', @@ -114,7 +124,7 @@ def main(): 'definition_counts':dict(collections.Counter(definitions.values())), 'native_chunk_storage':'ProtoChunk', 'rust_chunk_storage':'TerrainChunk', 'fixtures':count, 'sources_sha256':tested, 'fixtures_sha256':fixtures, 'rust_output_sha256':lab.digest(build/'rust.txt', 'sha256'), 'references':references, - 'scope':'Actual native Entity.collide executes initial collision and step-up decisions over BlockCollisions, registered block shapes and resident ProtoChunk sections. Rust combines owned/fallible step resolution with registry-bound TerrainChunk scanning. All registered states are used as obstacles, plus step heights, falling/grounded motion, zero and underflow motion, partial blocks, fluids, dynamic blocks, ceilings, chunk boundaries and query failures. Entity collider lists and world-border proximity/shape are controlled inputs; query order and reuse, shape collection and resulting displacement are compared. Contexts are deliberately supplied empty/position/placement snapshots in both worlds. This does not establish entity broad-phase selection, actual world-border proximity, travel/gravity/velocity or grounded side effects, experimental minecarts or a playable game.'} + 'scope':'Actual native Entity.collide executes initial collision and step-up decisions over BlockCollisions, registered block shapes and resident ProtoChunk sections. Rust combines owned/fallible step resolution with registry-bound TerrainChunk scanning. All registered states are used as obstacles, plus step heights, falling/grounded motion, zero and underflow motion, partial blocks including exact falling-block-sized boxes over slabs, stairs, snow, fences, iron bars, soul sand and farmland, fluids, dynamic blocks, ceilings, chunk boundaries and query failures. Entity collider lists and world-border proximity/shape are controlled inputs; query order and reuse, shape collection and resulting displacement are compared. Contexts are deliberately supplied empty/position/placement snapshots in both worlds. This does not establish entity broad-phase selection, actual world-border proximity, travel/gravity/velocity or grounded side effects, experimental minecarts or a playable game.'} lab.write_json(report_path, report);print(f'Scoped world movement parity passed: {report_path}', flush=True) diff --git a/tools/vanilla_client.py b/tools/vanilla_client.py index 88c6aba5b..41ae38556 100644 --- a/tools/vanilla_client.py +++ b/tools/vanilla_client.py @@ -5,6 +5,10 @@ servers: --offlineDeveloperMode) and optionally join a server straight away vscreen start python3 tools/vanilla_client.py --name Guest --join localhost:25565 +On Linux (x86_64) and on macOS (Apple silicon, from the logged-in screen session), from the files +tools/lab.py prepared on that machine. On macOS vsync is off: with it on, a window nobody can see (a +locked session, or covered) blocks in SDL_GL_SwapWindow and the client stops before it joins. + Sound goes to OpenAL's null backend. The game directory gets an options.txt that skips the first-run screens, keeps the game running unfocused and renders few chunks, so a harness can drive it with vscreen keys. @@ -21,6 +25,9 @@ from pathlib import Path ROOT = Path(__file__).resolve().parents[1] GAME = ROOT / ".local/minecraft/26.3" +MAC = sys.platform == "darwin" +# The natives of this machine's platform, and the shared libraries in them. +NATIVES, LIBRARY_SUFFIX = ("natives-macos-arm64.jar", ".dylib") if MAC else ("natives-linux.jar", ".so") OPTIONS = { "onboardAccessibility": "false", @@ -40,6 +47,8 @@ OPTIONS = { "onlyShowSecureChat": "false", "rawMouseInput": "false", } +if sys.platform == "darwin": + OPTIONS["enableVsync"] = "false" def offline_uuid(name): @@ -49,10 +58,12 @@ def offline_uuid(name): return uuid.UUID(bytes=bytes(digest)) -def linux_library(path): +def host_library(path): name = Path(path).name if "natives-" in name: - return name.endswith("natives-linux.jar") + return name.endswith(NATIVES) + if MAC: + return "netty-transport-native" not in name or "linux" not in name if "netty-transport-native" in name and ("osx" in name or "aarch_64" in name): return False return "java-objc-bridge" not in name @@ -60,18 +71,18 @@ def linux_library(path): def classpath(): prepared = json.loads((GAME / "prepared.json").read_text()) - jars = [ROOT / p for p in prepared["libraries"]["client"] if linux_library(p)] + jars = [ROOT / p for p in prepared["libraries"]["client"] if host_library(p)] return [GAME / "jars/client.jar", *jars] def extract_natives(jars, target): target.mkdir(parents=True, exist_ok=True) for jar in jars: - if not jar.name.endswith("natives-linux.jar"): + if not jar.name.endswith(NATIVES): continue with zipfile.ZipFile(jar) as archive: for name in archive.namelist(): - if name.endswith(".so") and "arm" not in name and "aarch" not in name: + if name.endswith(LIBRARY_SUFFIX) and (MAC or ("arm" not in name and "aarch" not in name)): out = target / Path(name).name if not out.exists(): out.write_bytes(archive.read(name)) @@ -105,7 +116,7 @@ def command(args): extract_natives(jars, natives) assets = args.assets or (args.game_dir / "assets") (assets / "indexes").mkdir(parents=True, exist_ok=True) - cmd = ["java", f"-Xmx{args.heap}", "-XX:StackShadowPages=32", + cmd = ["java", f"-Xmx{args.heap}", *(["-XstartOnFirstThread"] if MAC else []), "-XX:StackShadowPages=32", "--enable-native-access=ALL-UNNAMED", "--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED", f"-Djava.library.path={natives}", f"-Dorg.lwjgl.system.SharedLibraryExtractPath={natives}", diff --git a/website/Caddyfile b/website/Caddyfile index 7b919821e..9ede5bf9d 100644 --- a/website/Caddyfile +++ b/website/Caddyfile @@ -28,7 +28,8 @@ @media path /media/* /fonts/* /og.png /favicon* /apple-touch-icon.png header @media Cache-Control "public, max-age=604800" rewrite /privacy /privacy.html - @pages path / /index.html /404.html /privacy.html + rewrite /server /server.html + @pages path / /index.html /404.html /privacy.html /server.html header @pages Cache-Control "no-cache" @legal path /LEGAL.md /fonts/*.txt header @legal Content-Type "text/plain; charset=utf-8" diff --git a/website/LEGAL.md b/website/LEGAL.md index a0ef8ad05..9700017dd 100644 --- a/website/LEGAL.md +++ b/website/LEGAL.md @@ -43,8 +43,9 @@ the repository, so the launcher's screenshots no longer show it. ## Screenshots All captured in Horizon's Rust client, with vanilla resources prepared from the maintainer's -own copy of Minecraft: Java Edition 26.3, and no mods or shaders. Converted to WebP at 1600 -and 800 pixels wide by `scripts/assets.mjs`. +own copy of Minecraft: Java Edition 26.3, at the default graphics settings and with no mods or shaders +(the game and menus last captured on 2026-10-17). Converted to WebP at 1600 and 800 pixels wide +by `scripts/assets.mjs`. | Files in `public/media/` | Source | What it shows | | --- | --- | --- | @@ -54,7 +55,7 @@ and 800 pixels wide by `scripts/assets.mjs`. | `menu-pause*.webp` | `docs/media/menu-pause.png` | Horizon's own pause screen | | `launcher*.webp` | `docs/media/launcher-play.png` | The Horizon launcher's Play page (no background image) | | `settings*.webp` | `docs/media/settings-simple.png` | Horizon's own Video settings, Simple view | -| `hero*.webp`, `sunset*.webp`, `village*.webp`, `survival*.webp`, `nether*.webp`, `night*.webp`, `underwater*.webp` | `docs/media/*.jpg` | In-game views; none shows a Mojang logo or splash text | +| `sunset*.webp`, `village*.webp`, `survival*.webp`, `nether*.webp`, `night*.webp`, `underwater*.webp` | `docs/media/<name>.jpg` | In-game views of one fixed-seed world; none shows a Mojang logo or splash text | Horizon's interface in these images is Horizon's original work (MIT). The block, mob and item textures and the HUD sprites drawn in them belong to Mojang and Microsoft; they are diff --git a/website/README.md b/website/README.md index 8d47cf076..cfea517c3 100644 --- a/website/README.md +++ b/website/README.md @@ -18,6 +18,7 @@ serve.mjs a tiny static server for local checks src/index.html the page, with {{placeholders}} build.mjs fills src/404.html the not-found page src/privacy.html what the launcher can send when players allow it, retention, deletion (/privacy) +src/server.html host a server (/server): docs/hosting.md rendered into the page src/tokens.css the design tokens, generated from crates/horizon-theme (do not edit) src/style.css every style, on top of the tokens src/main.js download buttons, patch notes, links, contributors, nav and menu, tabs, lightbox, motion @@ -35,7 +36,7 @@ shots/ (gitignored) full-page screenshots at 360–1920 px, a 1250× ## What comes from where - **Built in** (`build.mjs`): the parity table, its score, harness count and audit date, - all from `README.md`; the Discord and Sponsor links from + all from `README.md`; the server guide (`/server`) from `docs/hosting.md`; the Discord and Sponsor links from `launcher/links.json`; the latest releases and the contributors (`/repos/ludovic111/horizon/contributors`, bots left out, the owner first), when GitHub answers; without an answer the build still succeeds and the Contributors block and its menu entries stay hidden until the page loads them. Locally these come from diff --git a/website/build.mjs b/website/build.mjs index 86fbed910..0a921d9d1 100644 --- a/website/build.mjs +++ b/website/build.mjs @@ -29,6 +29,10 @@ async function repoFile(path) { // The README: required, a site with a stale or missing parity table is worse than no deploy. const [readme, readmeFrom] = await repoFile("README.md"); +// The server guide (/server) is docs/hosting.md from its first section on: the page has its own title and lede. +const [hosting] = await repoFile("docs/hosting.md"); +const guideStart = hosting.search(/^## /m); +if (guideStart < 0) throw new Error("docs/hosting.md: no '## ' section"); let links = { ...DEFAULT_LINKS }; try { @@ -198,9 +202,11 @@ const values = { CONTRIBUTORS_HIDDEN: contributors.length ? "" : " hidden", BAKED: JSON.stringify(baked).replace(/</g, "\\u003c"), YEAR: String(new Date().getUTCFullYear()), + SERVER_GUIDE: markdown(hosting.slice(guideStart)), }; -for (const page of ["index.html", "404.html", "privacy.html"]) { +const pages = ["index.html", "404.html", "privacy.html", "server.html"]; +for (const page of pages) { let html = await readFile(join(site, "src", page), "utf8"); html = html.replace(/\{\{(\w+)\}\}/g, (all, key) => { if (!(key in values)) throw new Error(`${page}: unknown placeholder ${all}`); @@ -210,10 +216,10 @@ for (const page of ["index.html", "404.html", "privacy.html"]) { await writeFile(join(dist, page), html); } -// The page's inline script (it marks the page as scripted before CSS applies) and its -// inlined stylesheet: the Content-Security-Policy in the Caddyfile allows them by hash. -const indexHtml = await readFile(join(dist, "index.html"), "utf8"); -const hashes = (re) => [...new Set([...indexHtml.matchAll(re)].map((m) => `'sha256-${createHash("sha256").update(m[1]).digest("base64")}'`))].join(" "); +// The pages' inline scripts (they mark the page as scripted and apply the saved theme before CSS +// applies) and the inlined stylesheet: the Content-Security-Policy in the Caddyfile allows them by hash. +const allHtml = (await Promise.all(pages.map((page) => readFile(join(dist, page), "utf8")))).join("\n"); +const hashes = (re) => [...new Set([...allHtml.matchAll(re)].map((m) => `'sha256-${createHash("sha256").update(m[1]).digest("base64")}'`))].join(" "); await writeFile(join(dist, ".csp-script"), hashes(/<script>([\s\S]*?)<\/script>/g)); await writeFile(join(dist, ".csp-style"), hashes(/<style>([\s\S]*?)<\/style>/g)); diff --git a/website/public/media/menu-options-800.webp b/website/public/media/menu-options-800.webp index e55192a9f..6e36e5043 100644 Binary files a/website/public/media/menu-options-800.webp and b/website/public/media/menu-options-800.webp differ diff --git a/website/public/media/menu-options.webp b/website/public/media/menu-options.webp index c57c623c0..9ad5252c9 100644 Binary files a/website/public/media/menu-options.webp and b/website/public/media/menu-options.webp differ diff --git a/website/public/media/menu-pause-800.webp b/website/public/media/menu-pause-800.webp index bd1cf3bf4..b0bec7861 100644 Binary files a/website/public/media/menu-pause-800.webp and b/website/public/media/menu-pause-800.webp differ diff --git a/website/public/media/menu-pause.webp b/website/public/media/menu-pause.webp index 49c3d2ac5..0a5bf70e1 100644 Binary files a/website/public/media/menu-pause.webp and b/website/public/media/menu-pause.webp differ diff --git a/website/public/media/menu-title-800.webp b/website/public/media/menu-title-800.webp index 7e7f725de..fe5e2efa0 100644 Binary files a/website/public/media/menu-title-800.webp and b/website/public/media/menu-title-800.webp differ diff --git a/website/public/media/menu-title.webp b/website/public/media/menu-title.webp index 77dcf4776..cf41ccf67 100644 Binary files a/website/public/media/menu-title.webp and b/website/public/media/menu-title.webp differ diff --git a/website/public/media/menu-worlds-800.webp b/website/public/media/menu-worlds-800.webp index 7a2b8dfbc..cbc867ac4 100644 Binary files a/website/public/media/menu-worlds-800.webp and b/website/public/media/menu-worlds-800.webp differ diff --git a/website/public/media/menu-worlds.webp b/website/public/media/menu-worlds.webp index a80ad754d..b70168080 100644 Binary files a/website/public/media/menu-worlds.webp and b/website/public/media/menu-worlds.webp differ diff --git a/website/public/media/nether-800.webp b/website/public/media/nether-800.webp index 100b5adf1..d5363ea10 100644 Binary files a/website/public/media/nether-800.webp and b/website/public/media/nether-800.webp differ diff --git a/website/public/media/nether.webp b/website/public/media/nether.webp index 06faf4b32..2f9cdc5c1 100644 Binary files a/website/public/media/nether.webp and b/website/public/media/nether.webp differ diff --git a/website/public/media/night-800.webp b/website/public/media/night-800.webp index e81b1b0a4..d392ee3e6 100644 Binary files a/website/public/media/night-800.webp and b/website/public/media/night-800.webp differ diff --git a/website/public/media/night.webp b/website/public/media/night.webp index b0c245a02..fac8958da 100644 Binary files a/website/public/media/night.webp and b/website/public/media/night.webp differ diff --git a/website/public/media/settings-800.webp b/website/public/media/settings-800.webp index e55192a9f..6e36e5043 100644 Binary files a/website/public/media/settings-800.webp and b/website/public/media/settings-800.webp differ diff --git a/website/public/media/settings.webp b/website/public/media/settings.webp index c57c623c0..9ad5252c9 100644 Binary files a/website/public/media/settings.webp and b/website/public/media/settings.webp differ diff --git a/website/public/media/sunset-800.webp b/website/public/media/sunset-800.webp index a8dc39401..b0eb6957c 100644 Binary files a/website/public/media/sunset-800.webp and b/website/public/media/sunset-800.webp differ diff --git a/website/public/media/sunset.webp b/website/public/media/sunset.webp index 0245a534c..0a8b82762 100644 Binary files a/website/public/media/sunset.webp and b/website/public/media/sunset.webp differ diff --git a/website/public/media/survival-800.webp b/website/public/media/survival-800.webp index 4e189b69b..d519ddd67 100644 Binary files a/website/public/media/survival-800.webp and b/website/public/media/survival-800.webp differ diff --git a/website/public/media/survival.webp b/website/public/media/survival.webp index fc16cdc9b..a53713eb0 100644 Binary files a/website/public/media/survival.webp and b/website/public/media/survival.webp differ diff --git a/website/public/media/underwater-800.webp b/website/public/media/underwater-800.webp index 08b19380e..f9e25f08d 100644 Binary files a/website/public/media/underwater-800.webp and b/website/public/media/underwater-800.webp differ diff --git a/website/public/media/underwater.webp b/website/public/media/underwater.webp index 77ef97735..55aa643c8 100644 Binary files a/website/public/media/underwater.webp and b/website/public/media/underwater.webp differ diff --git a/website/public/media/village-800.webp b/website/public/media/village-800.webp index 09cb161e5..c9edfba41 100644 Binary files a/website/public/media/village-800.webp and b/website/public/media/village-800.webp differ diff --git a/website/public/media/village.webp b/website/public/media/village.webp index 800354dd2..40864e507 100644 Binary files a/website/public/media/village.webp and b/website/public/media/village.webp differ diff --git a/website/public/sitemap.xml b/website/public/sitemap.xml index cd377327b..fb04f5188 100644 --- a/website/public/sitemap.xml +++ b/website/public/sitemap.xml @@ -1,5 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url><loc>https://playhorizon.dev/</loc></url> + <url><loc>https://playhorizon.dev/server</loc></url> <url><loc>https://playhorizon.dev/privacy</loc></url> </urlset> diff --git a/website/src/index.html b/website/src/index.html index 0962b1a70..eac74b409 100644 --- a/website/src/index.html +++ b/website/src/index.html @@ -45,6 +45,7 @@ <a href="#patch-notes">Notes</a> <a href="#community">Community</a> <a href="#contributors" data-contributors{{CONTRIBUTORS_HIDDEN}}>Contributors</a> + <a href="/server">Server</a> </nav> <div class="nav-end"> <button class="theme-toggle" type="button" aria-label="Switch between light and dark"><span data-theme-label>Dark</span></button> @@ -64,6 +65,7 @@ <a href="#patch-notes">Patch notes</a> <a href="#community">Community</a> <a href="#contributors" data-contributors{{CONTRIBUTORS_HIDDEN}}>Contributors</a> + <a href="/server">Host a server</a> </nav> <div class="menu-foot"> <a class="button secondary large" href="https://github.com/ludovic111/horizon">GitHub</a> @@ -124,11 +126,11 @@ <p class="lede reveal">Captured in the Rust client itself, with vanilla assets from the player's own copy and no mods or shaders.</p> </div> <div class="mosaic"> - <figure class="tile tile-wide reveal-media"><button type="button" class="zoom" aria-label="Enlarge: a plains village"><img loading="lazy" decoding="async" src="/media/village-800.webp" srcset="/media/village-800.webp 800w, /media/village.webp 1600w" sizes="(min-width: 1000px) 800px, 92vw" width="1600" height="900" alt="A plains village with farms, an iron golem and animals"></button><figcaption><span>01</span>A plains village</figcaption></figure> + <figure class="tile tile-wide reveal-media"><button type="button" class="zoom" aria-label="Enlarge: a plains village"><img loading="lazy" decoding="async" src="/media/village-800.webp" srcset="/media/village-800.webp 800w, /media/village.webp 1600w" sizes="(min-width: 1000px) 800px, 92vw" width="1600" height="900" alt="A plains village among savanna hills, with farms, villagers and an iron golem"></button><figcaption><span>01</span>A plains village</figcaption></figure> <figure class="tile tile-tall reveal-media"><button type="button" class="zoom" aria-label="Enlarge: moonrise over a village"><img loading="lazy" decoding="async" src="/media/night-800.webp" srcset="/media/night-800.webp 800w, /media/night.webp 1600w" sizes="(min-width: 1000px) 400px, 92vw" width="1600" height="900" alt="Moonrise over a torch-lit village and river"></button><figcaption><span>02</span>Moonrise</figcaption></figure> <figure class="tile reveal-media"><button type="button" class="zoom" aria-label="Enlarge: a crimson forest"><img loading="lazy" decoding="async" src="/media/nether-800.webp" srcset="/media/nether-800.webp 800w, /media/nether.webp 1600w" sizes="(min-width: 1000px) 400px, 92vw" width="1600" height="900" alt="A crimson forest in the Nether with a zombified piglin"></button><figcaption><span>03</span>A crimson forest in the Nether</figcaption></figure> - <figure class="tile reveal-media"><button type="button" class="zoom" aria-label="Enlarge: salmon in a river"><img loading="lazy" decoding="async" src="/media/underwater-800.webp" srcset="/media/underwater-800.webp 800w, /media/underwater.webp 1600w" sizes="(min-width: 1000px) 400px, 92vw" width="1600" height="900" alt="Salmon swimming in a river"></button><figcaption><span>04</span>Salmon in a river</figcaption></figure> - <figure class="tile reveal-media"><button type="button" class="zoom" aria-label="Enlarge: survival at dusk"><img loading="lazy" decoding="async" src="/media/survival-800.webp" srcset="/media/survival-800.webp 800w, /media/survival.webp 1600w" sizes="(min-width: 1000px) 400px, 92vw" width="1600" height="900" alt="Survival at dusk: a zombie attacks near the village, with the hotbar and chat"></button><figcaption><span>05</span>Survival at dusk</figcaption></figure> + <figure class="tile reveal-media"><button type="button" class="zoom" aria-label="Enlarge: a coral reef"><img loading="lazy" decoding="async" src="/media/underwater-800.webp" srcset="/media/underwater-800.webp 800w, /media/underwater.webp 1600w" sizes="(min-width: 1000px) 400px, 92vw" width="1600" height="900" alt="Tropical fish over a warm-ocean coral reef"></button><figcaption><span>04</span>A coral reef</figcaption></figure> + <figure class="tile reveal-media"><button type="button" class="zoom" aria-label="Enlarge: survival at dusk"><img loading="lazy" decoding="async" src="/media/survival-800.webp" srcset="/media/survival-800.webp 800w, /media/survival.webp 1600w" sizes="(min-width: 1000px) 400px, 92vw" width="1600" height="900" alt="Survival at dusk: a zombie walks up, with the hotbar, health, hunger and experience"></button><figcaption><span>05</span>Survival at dusk</figcaption></figure> </div> </div> </section> @@ -233,7 +235,7 @@ </div> <div class="footer-bar"> <span class="footer-mark">Made in the open, in Rust.</span> - <nav class="footer-links" aria-label="Elsewhere"><a href="https://github.com/ludovic111/horizon">GitHub</a><a href="https://github.com/ludovic111/horizon/releases">Releases</a><a href="https://github.com/ludovic111/horizon/blob/main/LICENSE">Licence</a><a href="/LEGAL.md">Assets</a><a href="/privacy">Privacy</a></nav> + <nav class="footer-links" aria-label="Elsewhere"><a href="https://github.com/ludovic111/horizon">GitHub</a><a href="https://github.com/ludovic111/horizon/releases">Releases</a><a href="https://github.com/ludovic111/horizon/blob/main/LICENSE">Licence</a><a href="/server">Host a server</a><a href="/LEGAL.md">Assets</a><a href="/privacy">Privacy</a></nav> </div> </div> <p class="footer-word" aria-hidden="true">horizon</p> diff --git a/website/src/main.js b/website/src/main.js index 8717aad98..5178d4207 100644 --- a/website/src/main.js +++ b/website/src/main.js @@ -125,7 +125,8 @@ async function setUpLinks() { function setUpNav() { const bar = document.querySelector(".nav"); - const links = [...document.querySelectorAll(".nav-links a")]; + // The sections on this page (the nav also links to other pages, such as /server). + const links = [...document.querySelectorAll('.nav-links a[href^="#"]')]; const sections = links.map((a) => document.querySelector(a.getAttribute("href"))); let current = null; diff --git a/website/src/server.html b/website/src/server.html new file mode 100644 index 000000000..94f470678 --- /dev/null +++ b/website/src/server.html @@ -0,0 +1,31 @@ +<!doctype html> +<html lang="en"> + <head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>Host a server · Horizon + + + + + + + + + + +
+ +
+ + + diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 05b84553b..28b591626 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -6,6 +6,10 @@ //! them to its own), and PLAY.txt. It holds none of Minecraft's files: players prepare those //! from their own copy. .github/workflows/release.yml makes the release folders with it. //! +//! `package-server ENGINE OUTDIR`: a server folder (docs/server.md): the server build of the game +//! (`cargo build --release -p horizon-client --no-default-features`, without sound or controllers) +//! as horizon-engine, with horizon-prepare and horizon-server from beside it and the same sources. +//! //! `tokens`: writes `website/src/tokens.css`, the design tokens of `crates/horizon-theme` as CSS //! custom properties (a horizon-theme test fails when the file is out of date). //! @@ -17,7 +21,7 @@ use std::fs; use std::path::{Component, Path, PathBuf}; use std::process::{Command, ExitCode}; -const USAGE: &str = "usage: cargo run -p xtask -- package BINARY OUTDIR | tokens | icons"; +const USAGE: &str = "usage: cargo run -p xtask -- package BINARY OUTDIR | package-server ENGINE OUTDIR | tokens | icons"; /// What the release folder takes from the repository at HEAD, as `git archive` writes it. const ARCHIVED: [&str; 5] = [ @@ -34,10 +38,16 @@ const ARCHIVED: [&str; 5] = [ /// (docs/server.md). const TOOLS: [&str; 3] = ["horizon-ctl", "horizon-prepare", "horizon-server"]; +/// The programs beside the engine in a server folder. +const SERVER_TOOLS: [&str; 2] = ["horizon-prepare", "horizon-server"]; + fn main() -> ExitCode { let args: Vec = std::env::args().skip(1).collect(); let result = match args.as_slice() { [task, binary, out] if task == "package" => package(Path::new(binary), Path::new(out)), + [task, engine, out] if task == "package-server" => { + package_server(Path::new(engine), Path::new(out)) + } [task] if task == "tokens" => tokens(), [task] if task == "icons" => icons(), _ => Err(USAGE.to_string()), @@ -209,6 +219,21 @@ fn icons() -> Result<(), String> { } fn package(binary: &Path, out: &Path) -> Result<(), String> { + let repo = new_folder(out)?; + copy_into(binary, out)?; + copy_beside(binary, &TOOLS, out)?; + let play = repo.join("packaging/PLAY.txt"); + copy(&play, &out.join("PLAY.txt")) +} + +fn package_server(engine: &Path, out: &Path) -> Result<(), String> { + new_folder(out)?; + copy(engine, &out.join(program_name(engine, "horizon-engine")))?; + copy_beside(engine, &SERVER_TOOLS, out) +} + +/// `OUT`, emptied, with the sources the release folders take (`ARCHIVED`); the repository's path. +fn new_folder(out: &Path) -> Result { let top = git( Path::new(env!("CARGO_MANIFEST_DIR")), &["rev-parse", "--show-toplevel"], @@ -224,15 +249,25 @@ fn package(binary: &Path, out: &Path) -> Result<(), String> { let mut args = vec!["archive", "--format=tar", "HEAD"]; args.extend(ARCHIVED); extract(&git(&repo, &args)?, out)?; - copy_into(binary, out)?; - let exe = binary.extension().is_some_and(|e| e == "exe"); - for tool in TOOLS { - let name = if exe { - format!("{tool}.exe") - } else { - tool.to_string() - }; - let path = binary.parent().unwrap_or(Path::new("")).join(name); + Ok(repo) +} + +/// `name`, with `.exe` when `program` has it. +fn program_name(program: &Path, name: &str) -> String { + if program.extension().is_some_and(|e| e == "exe") { + format!("{name}.exe") + } else { + name.to_string() + } +} + +/// Copies `tools`, built beside `program`, into `out`. +fn copy_beside(program: &Path, tools: &[&str], out: &Path) -> Result<(), String> { + for tool in tools { + let path = program + .parent() + .unwrap_or(Path::new("")) + .join(program_name(program, tool)); if !path.is_file() { return Err(format!( "{} is missing; build it too: cargo build --release -p horizon-client --bins -p horizon-prepare -p horizon-server", @@ -241,8 +276,7 @@ fn package(binary: &Path, out: &Path) -> Result<(), String> { } copy_into(&path, out)?; } - let play = repo.join("packaging/PLAY.txt"); - copy(&play, &out.join("PLAY.txt")) + Ok(()) } /// `cp FILE DIR/`.