diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml
index 51f9e89cd..60bbbf057 100644
--- a/.github/workflows/nightly.yml
+++ b/.github/workflows/nightly.yml
@@ -29,7 +29,8 @@ env:
jobs:
audit:
- runs-on: [self-hosted, horizon-game]
+ # The Mac mini's runners only: the audit compares with the previous one, so it stays on one platform.
+ runs-on: [self-hosted, horizon-game, macOS]
timeout-minutes: 150
steps:
- name: 03:00 in Paris
diff --git a/.github/workflows/parity-main.yml b/.github/workflows/parity-main.yml
index 1ddfb24bb..13be3c36d 100644
--- a/.github/workflows/parity-main.yml
+++ b/.github/workflows/parity-main.yml
@@ -23,7 +23,11 @@ env:
jobs:
parity-main:
- runs-on: [self-hosted, horizon-game]
+ # The Mac mini's release runner (label added 2026-10-26), not the two `horizon-game` runners: behind PR parity jobs
+ # this run waited 2 h while every merge replaced the pending one, and releases wait for it. Releases need it green
+ # first, so the two never compete. Not the desktop: a job slowed down because the maintainer came back could time
+ # out, and a failure here bisects and comments on merges.
+ runs-on: [self-hosted, macOS, horizon-parity-main]
timeout-minutes: 240 # a core-crate batch reruns ~300 harnesses (~60-90 min), a bisect a few rounds of the failures
steps:
- uses: actions/checkout@v4
diff --git a/.github/workflows/parity.yml b/.github/workflows/parity.yml
index 577380338..b48be1d05 100644
--- a/.github/workflows/parity.yml
+++ b/.github/workflows/parity.yml
@@ -36,7 +36,19 @@ jobs:
- name: Tools from the runner
run: bash tools/ci/runner-env.sh
- name: Vanilla data, and resources prepared by this preparer
- run: bash tools/ci/vanilla-data.sh
+ env:
+ BASE_SHA: ${{ inputs.base }}
+ run: |
+ # main's script: one written before a fix to it left a real .local/horizon-client without sounds.json on
+ # the runner, and every later job there failed `subtitle`. A branch that changes the script runs its own.
+ set -euo pipefail
+ if git rev-parse --verify -q origin/main >/dev/null \
+ && git diff --quiet "$(git merge-base "$BASE_SHA" HEAD)" HEAD -- tools/ci/vanilla-data.sh; then
+ git show origin/main:tools/ci/vanilla-data.sh > "$RUNNER_TEMP/vanilla-data.sh"
+ bash "$RUNNER_TEMP/vanilla-data.sh"
+ else
+ bash tools/ci/vanilla-data.sh
+ fi
- name: The prepared output is the one the preparer's OUTPUT_VERSION says
env:
BASE_SHA: ${{ inputs.base }}
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 05f590096..d879f0d61 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -79,9 +79,50 @@ jobs:
# A dry run builds whatever the decision, to test the builds.
if [[ "${{ steps.mode.outputs.dry_run }}" == true ]]; then echo "release=true" >> "$GITHUB_OUTPUT"; fi
- build:
+ exporters:
needs: decide
if: needs.decide.outputs.release == 'true'
+ # This runner holds the maintainer's own pinned Minecraft jars. Only our classes leave it.
+ runs-on: ${{ vars.MAC_RUNNER || 'ludovics-mac-mini' }}
+ # Three full preparations (two isolated, one batched) besides the compile.
+ timeout-minutes: 60
+ steps:
+ - uses: actions/checkout@v4
+ with:
+ ref: ${{ needs.decide.outputs.head }}
+ persist-credentials: false
+ clean: true
+ - uses: dtolnay/rust-toolchain@stable
+ - run: bash tools/ci/runner-env.sh
+ - run: bash tools/ci/vanilla-data.sh
+ - name: Compile and validate original exporters against the pinned jars
+ shell: bash
+ run: |
+ set -euo pipefail
+ # target/ is gone after the clean checkout, and vanilla-data.sh builds the preparer only when it
+ # prepares afresh.
+ cargo build --release --locked -p horizon-prepare
+ # Compiled here, never taken from the runner's shared preparations: pull requests' parity jobs
+ # write those (vanilla-data.sh links .local/exporters to them).
+ rm -rf .local/exporters
+ bundle=$(target/release/horizon-prepare compile-exporters --root "$PWD" | tail -1)
+ mkdir -p exporters
+ cp -R "$bundle/server" "$bundle/client" "$bundle/manifest.json" exporters/
+ # Only our classes and their manifest leave this runner.
+ stray=$(find exporters -type f ! -path exporters/manifest.json \
+ ! \( -name '*.class' \( -path 'exporters/server/dev/horizonlab/parity/*' -o -path 'exporters/client/dev/horizonlab/parity/*' \) \))
+ [ -z "$stray" ] || { echo "::error::not one of our classes: $stray"; exit 1; }
+ python3 tools/check_prepare_exporters.py --binary target/release/horizon-prepare
+ - uses: actions/upload-artifact@v4
+ with:
+ name: original-exporters
+ path: exporters/
+ if-no-files-found: error
+ retention-days: 3
+
+ build:
+ needs: [decide, exporters]
+ if: needs.decide.outputs.release == 'true'
name: build ${{ matrix.name }}
strategy:
fail-fast: false
@@ -220,6 +261,10 @@ jobs:
path: ${{ runner.temp }}/smoke-test.json
if-no-files-found: error
retention-days: 3
+ - uses: actions/download-artifact@v4
+ with:
+ name: original-exporters
+ path: exporters
- name: Package
run: |
set -euo pipefail
diff --git a/.gitignore b/.gitignore
index 7fffd6a99..6bdae649a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -31,3 +31,6 @@
/.horizon-cache/
/crash-reports/
/debug/
+
+# Release-only compiled original exporter bundle.
+/exporters/
diff --git a/Cargo.lock b/Cargo.lock
index cb5f2f554..85059a49a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1408,6 +1408,8 @@ dependencies = [
name = "horizon-prepare"
version = "0.1.0"
dependencies = [
+ "crc32fast",
+ "libc",
"regex",
"sha1",
"sha2",
diff --git a/README.md b/README.md
index ceee29249..65078464e 100644
--- a/README.md
+++ b/README.md
@@ -31,7 +31,7 @@ Horizon is Minecraft: Java Edition 26.3 rewritten in Rust: a native game that re
Horizon is installed through **the Horizon launcher**. Get it from [playhorizon.dev](https://playhorizon.dev) (the Download button picks your system) or from [the launcher releases](https://github.com/ludovic111/horizon/releases?q=launcher-v&expanded=true), for Linux x86-64, macOS on Apple Silicon or Windows x64. The launcher installs and updates the game, prepares its files from your own copy of Minecraft, and updates itself.
-**You need** Minecraft: Java Edition 26.3, played once (the official launcher, Prism, MultiMC and other launchers all work); a Java 25 JDK, for the one-time preparation; and a GPU with Vulkan on Linux and Windows, or any Apple Silicon Mac. Step by step, with macOS's first-open prompt and the first launch: [Install and first launch](https://playhorizon.dev/docs/install).
+**You need** Minecraft: Java Edition 26.3, played once (the official launcher, Prism, MultiMC and other launchers all work); a Java 25 runtime, for the one-time preparation; and a GPU with Vulkan on Linux and Windows, or any Apple Silicon Mac. Step by step, with macOS's first-open prompt and the first launch: [Install and first launch](https://playhorizon.dev/docs/install).
diff --git a/crates/cargo-horizon-mod/src/check.rs b/crates/cargo-horizon-mod/src/check.rs
index cefddf966..a10d27804 100644
--- a/crates/cargo-horizon-mod/src/check.rs
+++ b/crates/cargo-horizon-mod/src/check.rs
@@ -208,6 +208,9 @@ fn play(path: &Path, report: &mut Report) -> Result<(), String> {
game_time: t,
day_time: 1000 + t,
}),
+ // The GUI of a 1280 x 720 window at scale 3.
+ gui_size: (427.0, 240.0),
+ held: None,
});
mods.tick(t);
for _ in 0..FRAMES_PER_TICK {
@@ -228,8 +231,15 @@ fn play(path: &Path, report: &mut Report) -> Result<(), String> {
Output::Stopped { reason, .. } => return Err(format!("it stopped: {reason}")),
Output::OpenScreen { .. } => "opened a screen".to_string(),
Output::CloseScreen { .. } => "closed its screen".to_string(),
+ Output::Shake { strength, seconds } => {
+ format!("shook the camera: {strength:.2} for {seconds:.1} s")
+ }
});
}
+ let shapes = mods.hud_shapes().count();
+ if shapes > 0 {
+ report.output.push(format!("drew {shapes} HUD shape(s)"));
+ }
match failed(&mods) {
Some(reason) => Err(format!("it stopped: {reason}")),
None => Ok(()),
diff --git a/crates/horizon-client/examples/probe/mob_ai_probe.rs b/crates/horizon-client/examples/probe/mob_ai_probe.rs
index 157baa74c..49f56c250 100644
--- a/crates/horizon-client/examples/probe/mob_ai_probe.rs
+++ b/crates/horizon-client/examples/probe/mob_ai_probe.rs
@@ -598,6 +598,7 @@ fn print(out: &mut String, t: i32, i: usize, m: &Mob, player_down: bool, now: i6
let _ = writeln!(out, " removed {reason}");
return;
}
+ let _ = write!(out, "\tleashed {}", u8::from(m.extra.leash.is_some()));
let p = m.position;
let v = m.velocity;
let _ = write!(
@@ -977,6 +978,7 @@ pub fn main() -> horizon_client::Result<()> {
}
// Set once the brain exists (below).
x if x.starts_with("cooldown=") => {}
+ "dead" => m.health = 0.0,
"leashed" => m.extra.leash = Some(horizon_client::mobs::leash::Holder::Player),
x if x.starts_with("leashto=") => {
let n: u32 = x[8..].parse().unwrap();
@@ -1097,6 +1099,7 @@ pub fn main() -> horizon_client::Result<()> {
}
};
}
+ let mut dropped_leads = 0;
for t in 1..=s.ticks {
world.game_time += 1;
if s.daycycle {
@@ -1208,6 +1211,16 @@ pub fn main() -> horizon_client::Result<()> {
if !alive {
m.removed = true;
}
+ // Apply the same detach/drop event as Game::break_leash.
+ for event in &events[from..] {
+ if let MobEvent::LeashBreak { mob, drop, .. } = event
+ && let Some(m) = mobs.iter_mut().find(|m| m.id == *mob)
+ && horizon_client::mobs::leash::remove(m).is_some()
+ && *drop
+ {
+ dropped_leads += 1;
+ }
+ }
horizon_client::mobs::apply_brain_events(&mut mobs, &mut events, from);
villager_events(
&mut mobs,
@@ -1427,6 +1440,9 @@ pub fn main() -> horizon_client::Result<()> {
world.game_time,
);
}
+ if s.name.starts_with("leash_death_") {
+ let _ = writeln!(out, "{t} leads {dropped_leads}");
+ }
for e in &events {
if let MobEvent::Sound(name, _, volume, pitch) = e {
let _ = writeln!(
diff --git a/crates/horizon-client/src/enchant.rs b/crates/horizon-client/src/enchant.rs
index 2fc9ef2fb..2f418ae82 100644
--- a/crates/horizon-client/src/enchant.rs
+++ b/crates/horizon-client/src/enchant.rs
@@ -33,7 +33,11 @@ impl Cost {
#[derive(Clone, Debug)]
pub struct Enchantment {
pub id: Arc,
+ /// The name the pinned English map gave at load: the fallback for a
+ /// language that has no entry for the key below.
pub description: String,
+ /// `description`'s translation key (`enchantment.minecraft.`).
+ pub description_key: String,
pub max_level: u32,
pub weight: u32,
pub anvil_cost: u32,
@@ -224,6 +228,7 @@ impl Enchantments {
.get(key)
.cloned()
.unwrap_or_else(|| id.trim_start_matches("minecraft:").replace('_', " ")),
+ description_key: key.to_owned(),
max_level: v["max_level"].as_u64().unwrap_or(1) as u32,
weight: v["weight"].as_u64().unwrap_or(1) as u32,
anvil_cost: v["anvil_cost"].as_u64().unwrap_or(0) as u32,
@@ -329,16 +334,26 @@ impl Enchantments {
}
/// `Enchantment.getFullname` as plain text and whether it is a curse (red).
pub fn full_name(&self, id: &str, level: u32) -> (String, bool) {
+ let language = crate::lang::current();
+ self.full_name_in(language.as_deref(), id, level)
+ }
+ /// `full_name` against `language`; `None` (no language loaded: a
+ /// dedicated server, the probes) keeps the English names loaded above.
+ pub fn full_name_in(
+ &self,
+ language: Option<&crate::lang::Language>,
+ id: &str,
+ level: u32,
+ ) -> (String, bool) {
+ let tr = |key: &str| language.and_then(|l| l.get(key).map(str::to_owned));
let Some(e) = self.get(id) else {
return (format!("{id} {}", level_name(level)), false);
};
- let mut name = e.description.clone();
+ let mut name = tr(&e.description_key).unwrap_or_else(|| e.description.clone());
if level != 1 || e.max_level != 1 {
name.push(' ');
name.push_str(
- self.lang
- .get(&format!("enchantment.level.{level}"))
- .map_or(&level_name(level), |s| s),
+ &tr(&format!("enchantment.level.{level}")).unwrap_or_else(|| level_name(level)),
);
}
(name, self.is_curse(id))
@@ -771,7 +786,7 @@ pub(crate) mod tests {
let defs = [
(
"minecraft:sharpness",
- json!({"anvil_cost":1,"exclusive_set":"#minecraft:exclusive_set/damage","max_cost":{"base":21,"per_level_above_first":11},"max_level":5,"min_cost":{"base":1,"per_level_above_first":11},"primary_items":"#minecraft:enchantable/melee_weapon","supported_items":"#minecraft:enchantable/sharp_weapon","weight":10,"slots":["mainhand"],"effects":{"minecraft:damage":[{"effect":{"type":"minecraft:add","value":{"type":"minecraft:linear","base":1.0,"per_level_above_first":0.5}}}]}}),
+ json!({"anvil_cost":1,"description":{"translate":"enchantment.minecraft.sharpness"},"exclusive_set":"#minecraft:exclusive_set/damage","max_cost":{"base":21,"per_level_above_first":11},"max_level":5,"min_cost":{"base":1,"per_level_above_first":11},"primary_items":"#minecraft:enchantable/melee_weapon","supported_items":"#minecraft:enchantable/sharp_weapon","weight":10,"slots":["mainhand"],"effects":{"minecraft:damage":[{"effect":{"type":"minecraft:add","value":{"type":"minecraft:linear","base":1.0,"per_level_above_first":0.5}}}]}}),
),
(
"minecraft:smite",
@@ -795,6 +810,7 @@ pub(crate) mod tests {
.map(|(id, v)| Enchantment {
id: Arc::from(*id),
description: id.trim_start_matches("minecraft:").into(),
+ description_key: v["description"]["translate"].as_str().unwrap_or("").into(),
max_level: v["max_level"].as_u64().unwrap() as u32,
weight: v["weight"].as_u64().unwrap() as u32,
anvil_cost: v["anvil_cost"].as_u64().unwrap() as u32,
@@ -1088,4 +1104,33 @@ pub(crate) mod tests {
assert!(!e.compatible("minecraft:sharpness", "minecraft:smite"));
assert!(e.compatible("minecraft:sharpness", "minecraft:unbreaking"));
}
+
+ /// The enchantment's name and its level come from the selected language,
+ /// with the English name loaded above as the fallback.
+ #[test]
+ fn names_follow_the_language() {
+ struct Mem(HashMap>);
+ impl crate::lang::ResourceSource for Mem {
+ fn read(&self, path: &str) -> Option> {
+ self.0.get(path).cloned()
+ }
+ }
+ let ru = Mem(HashMap::from([(
+ "assets/minecraft/lang/ru_ru.json".to_owned(),
+ r#"{"enchantment.minecraft.sharpness":"Острота","enchantment.level.5":"V"}"#
+ .as_bytes()
+ .to_vec(),
+ )]));
+ let ru = crate::lang::Language::load(&[&ru], "ru_ru");
+ let e = sample();
+ assert_eq!(
+ e.full_name_in(Some(&ru), "minecraft:sharpness", 5).0,
+ "Острота V"
+ );
+ // mending carries no key in the fixture: the English name stays.
+ assert_eq!(
+ e.full_name_in(Some(&ru), "minecraft:mending", 1).0,
+ "mending"
+ );
+ }
}
diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs
index 6fb3a611c..942e8699f 100644
--- a/crates/horizon-client/src/game.rs
+++ b/crates/horizon-client/src/game.rs
@@ -97,6 +97,8 @@ mod maps;
mod minecarts;
mod mob_blocks;
mod mob_gear;
+mod mod_input;
+mod mod_render;
mod mods;
mod mount_menu;
mod mounts;
@@ -1242,6 +1244,11 @@ struct Game<'a> {
luminance: Option,
/// Name tags drawn over everything (`RenderPipelines.TEXT_SEE_THROUGH`).
see_through_vertices: Vec,
+ /// Runs of `entity_vertices` by entity, for a shader pack's per-entity draws; recorded only
+ /// while `pack_ids` is set (game/shader_packs.rs).
+ entity_spans: std::cell::RefCell>,
+ /// The shader pack's entity and block entity ids, while one draws entities.
+ pack_ids: Option,
started: Instant,
previous: Instant,
/// FramerateLimitTracker.latestInputTime.
@@ -2769,7 +2776,13 @@ impl Game<'_> {
}
_ => 1.0,
};
- self.audio.play(name, c, volume, pitch);
+ if matches!(id, 1029 | 1030) {
+ // Clients play the anvil sound from its level event;
+ // capturing it also sends a duplicate sound packet.
+ self.audio.play_unshared(name, c, volume, pitch);
+ } else {
+ self.audio.play(name, c, volume, pitch);
+ }
}
}
LevelEvent::Sound {
@@ -4931,6 +4944,7 @@ impl Game<'_> {
// Hud: the action bar and titles draw over the hotbar (nextStratum).
self.draw_boss_bars(&mut batch);
self.draw_extras(&mut batch);
+ self.draw_mod_shapes(&mut batch);
}
if show_hud {
// Hud.nextContextualInfoState: the jump bar, the experience bar or
@@ -6752,8 +6766,10 @@ impl Game<'_> {
alpha: f64,
mut out: Vec,
) -> Vec {
+ self.entity_spans.borrow_mut().clear();
self.prepare_entity_culling(camera);
self.prepare_trims();
+ self.mark_entity(out.len(), "minecraft:entity_flame");
let fire_yaw = if self.camera_type == perspective::CameraType::ThirdPersonFront {
self.yaw + 180.0
} else {
@@ -6845,14 +6861,19 @@ impl Game<'_> {
}
}
// `ItemEntityRenderer`.
+ self.mark_entity(out.len(), "minecraft:item");
self.dropped_item_geometry(camera, alpha, &mut out);
// `VaultRenderer`.
+ self.mark_entity(out.len(), "");
self.vault_geometry(camera, alpha, &mut out);
+ self.mark_entity(out.len(), "minecraft:ominous_item_spawner");
self.ominous_item_spawner_geometry(camera, alpha, &mut out);
- // Mobs and their projectiles.
+ // Mobs and their projectiles (a span each).
self.mob_geometry(camera, alpha, &mut out);
+ self.mark_entity(out.len(), "minecraft:player");
self.player_model_geometry(camera, self.clock.camera_fraction(), &mut out);
self.guest_model_geometry(camera, alpha, &mut out);
+ self.mark_entity(out.len(), "minecraft:evoker_fangs");
self.fangs_geometry(camera, alpha, &mut out);
let mut see_through = std::mem::take(&mut self.see_through_vertices);
see_through.clear();
@@ -6860,6 +6881,7 @@ impl Game<'_> {
self.text_display_geometry(camera, alpha, &mut out, &mut see_through);
self.see_through_vertices = see_through;
// Entity shadows (EntityRenderer.extractShadow).
+ self.mark_translucent_entity(out.len(), "minecraft:entity_shadow");
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();
@@ -6953,13 +6975,14 @@ impl Game<'_> {
v.intersects(o - glam::Vec3::ONE, o + glam::Vec3::splat(2.0))
})
});
- if self.entity_culler.is_some() {
+ if self.entity_culler.is_some() || self.pack_ids.is_some() {
for block in visible {
let start = out.len();
+ self.mark_block_entity(start, block.1);
self.res
.block_entity_render()
.render(&scene, std::iter::once(block), &mut out);
- if !off_screen(block.1) {
+ if !off_screen(block.1) && self.entity_culler.is_some() {
self.cull_entity_vertices(&mut out, start);
}
}
@@ -6968,6 +6991,7 @@ impl Game<'_> {
.block_entity_render()
.render(&scene, visible, &mut out);
}
+ self.mark_stage(out.len(), "DESTROY");
for (pos, stage) in self.cracks(camera) {
let state = self.world.block(pos[0], pos[1], pos[2]);
self.res
@@ -6976,6 +7000,7 @@ impl Game<'_> {
}
}
// Experience orbs (ExperienceOrbRenderer): pulsing billboards from a 4x4 icon sheet.
+ self.mark_entity(out.len(), "minecraft:experience_orb");
if let Some(r) = self
.gui
.atlas
@@ -7039,6 +7064,7 @@ impl Game<'_> {
}
}
// Falling blocks use their baked model, including non-cubic shapes.
+ self.mark_entity(out.len(), "minecraft:falling_block");
for f in &self.falling {
let Some((_, tint)) = self.block_face_sprite(f.state) else {
continue;
@@ -7069,20 +7095,35 @@ impl Game<'_> {
vertex.color = horizon_client::hud_layout::multiply(vertex.color, tint);
}
}
+ self.mark_entity(out.len(), "");
self.moving_piston_geometry(camera, alpha, &mut out);
+ self.mark_entity(out.len(), "minecraft:tnt");
self.tnt_geometry(camera, alpha, &mut out);
+ self.mark_entity(out.len(), "minecraft:painting");
self.painting_geometry(camera, &mut out);
+ self.mark_entity(out.len(), "minecraft:item_frame");
self.frame_geometry(camera, &mut out);
+ self.mark_entity(out.len(), "minecraft:armor_stand");
self.armor_stand_geometry(camera, alpha, &mut out);
+ self.mark_entity(out.len(), "minecraft:oak_boat");
self.boat_geometry(camera, alpha, &mut out);
+ self.mark_entity(out.len(), "minecraft:minecart");
self.cart_geometry(camera, alpha, &mut out);
+ // Beams: `gbuffers_beaconbeam`, not a block entity's program (5.4 has no stage for them).
+ self.mark_stage(out.len(), "NONE");
self.beacon_geometry(camera, alpha, &mut out);
+ self.mark_entity(out.len(), "minecraft:fishing_bobber");
self.fishing_geometry(camera, alpha, &mut out);
+ self.mark_entity(out.len(), "minecraft:leash_knot");
self.leash_geometry(camera, alpha, &mut out);
+ self.mark_entity(out.len(), "minecraft:trident");
self.trident_geometry(camera, alpha, &mut out);
+ self.mark_entity(out.len(), "minecraft:firework_rocket");
self.rocket_geometry(camera, alpha, &mut out);
+ self.mark_entity(out.len(), "minecraft:splash_potion");
self.thrown_potion_geometry(camera, alpha, &mut out);
// Particles (ParticleEngine.extract): camera-facing quads.
+ self.mark_stage(out.len(), "PARTICLES");
let world = &self.world;
self.particles.render(
camera,
@@ -7093,6 +7134,7 @@ impl Game<'_> {
&mut out,
);
// Breaking progress overlays (destroy_stage_0..9): ours and the other players'.
+ self.mark_stage(out.len(), "DESTROY");
for (pos, stage) in self.cracks(camera) {
let o = (glam::DVec3::new(f64::from(pos[0]), f64::from(pos[1]), f64::from(pos[2]))
- camera)
@@ -8405,6 +8447,8 @@ impl Game<'_> {
1.0,
1.0,
);
+ // ItemStack.useOn: a use that acted counts as one of the item.
+ self.stats.add("used", &stack.item, 1);
self.damage_held(damage);
self.post_block_game_event("minecraft:block_change", pos, self.sculk_actor(), Some(new));
Ok(true)
@@ -8609,6 +8653,14 @@ impl Game<'_> {
1.0,
1.0,
);
+ // HoneycombItem.useOn's game event, and ItemStack.useOn's used statistic.
+ self.post_block_game_event(
+ "minecraft:block_change",
+ hit.pos,
+ self.sculk_actor(),
+ Some(state),
+ );
+ self.stats.add("used", &stack.item, 1);
if !self.creative {
self.consume_held();
}
@@ -9200,10 +9252,6 @@ impl Game<'_> {
)
}
fn apply_settings(&mut self) {
- if let Some(r) = &self.renderer {
- self.settings
- .clamp_gui_scale([r.config.width as i32, r.config.height as i32]);
- }
self.streaming.radius = self.settings.render_distance;
// Options.biomeBlendRadius, ambientOcclusion and cutoutLeaves:
// LevelExtractor.allChanged when they change.
@@ -9966,6 +10014,7 @@ impl Game<'_> {
self.pitch,
),
};
+ let (view_yaw, view_pitch) = self.shaken_view(view_yaw, view_pitch);
self.audio
.set_listener([camera.x, camera.y, camera.z], view_yaw, view_pitch);
let block = [
@@ -10197,6 +10246,7 @@ impl Game<'_> {
} else {
self.shader_pack_inputs(camera, alpha as f32)
};
+ let entity_spans = std::mem::take(self.entity_spans.get_mut());
let frame = Frame {
weather: &weather,
weather_time: horizon_client::weather::Time::new(self.world.game_time, alpha as f32),
@@ -10261,6 +10311,7 @@ impl Game<'_> {
color: sky.cloud_color,
}),
entities: &entities,
+ entity_spans: &entity_spans,
dynamic_lights: &self.dynamic_lights,
lightning: horizon_client::gfx::lightning::Input {
bolts: &bolts,
@@ -10312,6 +10363,7 @@ impl Game<'_> {
};
// Restore storage even when rendering fails (for example, a lost surface).
self.entity_vertices = entities;
+ *self.entity_spans.get_mut() = entity_spans;
self.see_through_vertices = see_through;
result
}
@@ -10577,6 +10629,10 @@ impl Game<'_> {
}
/// A mouse button while playing (window or control socket).
fn play_mouse(&mut self, button: MouseButton, pressed: bool) -> Result<()> {
+ // A mod's claimed item takes the use button (capability `input`).
+ if self.mod_use(button, pressed) {
+ return Ok(());
+ }
if self.sleeping.is_some() {
return Ok(());
}
@@ -11039,6 +11095,12 @@ impl ApplicationHandler for Game<'_> {
if self.screen == Screen::Advancements {
self.advancements_drag();
}
+ if self.screen == Screen::Stats {
+ self.stats_drag();
+ }
+ if self.screen == Screen::WorldOptions {
+ self.world_options_drag();
+ }
if self.screen == Screen::Creative {
let (_, oy) = Self::creative_origin(&b);
self.creative_screen.drag_scrollbar(self.cursor.1 - oy);
@@ -11318,6 +11380,9 @@ impl ApplicationHandler for Game<'_> {
Screen::WorldOptions if pressed && button == MouseButton::Left => {
self.world_options_click()
}
+ Screen::WorldOptions if button == MouseButton::Left => {
+ self.world_options_release()
+ }
Screen::Friends if pressed && button == MouseButton::Left => {
self.friends_screen_click()
}
@@ -11341,6 +11406,7 @@ impl ApplicationHandler for Game<'_> {
.map_or([1, 1], |r| [r.config.width, r.config.height]);
self.stats_click(size);
}
+ Screen::Stats if button == MouseButton::Left => self.stats_release(),
Screen::Mod if pressed && button == MouseButton::Left => {
self.mod_screen_click()
}
@@ -11485,6 +11551,7 @@ impl ApplicationHandler for Game<'_> {
return Ok(());
};
self.extras_key(key, event.state == ElementState::Pressed, event.repeat);
+ self.mod_key(key, event.state == ElementState::Pressed, event.repeat);
// key.smoothCamera toggles the cinematic camera.
if self.screen == Screen::Playing
&& event.state == ElementState::Pressed
@@ -11907,6 +11974,8 @@ impl<'a> Game<'a> {
dynamic_lights: Vec::new(),
luminance: None,
see_through_vertices: Vec::new(),
+ entity_spans: Default::default(),
+ pack_ids: None,
health_hud: hud_layout::HealthHud::default(),
started: now,
previous: now,
@@ -12842,6 +12911,7 @@ fn loading_frame(hud: &HudBatch) -> Frame<'_> {
hud,
nether_lighting: false,
entities: &[],
+ entity_spans: &[],
dynamic_lights: &[],
clouds: None,
lightning: Default::default(),
@@ -13102,10 +13172,6 @@ impl Loading {
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);
}
@@ -13144,8 +13210,6 @@ impl Loading {
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);
}
diff --git a/crates/horizon-client/src/game/commands.rs b/crates/horizon-client/src/game/commands.rs
index 7ddfc3d0f..78585e8be 100644
--- a/crates/horizon-client/src/game/commands.rs
+++ b/crates/horizon-client/src/game/commands.rs
@@ -2501,12 +2501,14 @@ impl Game<'_> {
};
let max = Self::int(ctx, "maxCount").unwrap_or(-1);
let counting = max == 0;
- // Inventory.clearOrCountMatchingItems: main, armor, crafting grid, carried.
+ // Inventory slots 0..40: main, armor feet to head, off hand;
+ // then clearOrCountMatchingItems visits the crafting grid and carried stack.
let mut removed = 0i32;
let mut budget = if max < 0 { i32::MAX } else { max };
let all = |g: &Self| -> Vec> {
let mut v: Vec > = g.inventory.clone();
- v.extend(g.armor.iter().cloned());
+ v.extend(g.armor.iter().rev().cloned());
+ v.push(g.offhand.clone());
v.extend(g.craft_grid.iter().cloned());
v.push(g.carried.clone());
v
@@ -2520,7 +2522,8 @@ impl Game<'_> {
.collect();
let mut slots: Vec<&mut Option> = Vec::new();
slots.extend(self.inventory.iter_mut());
- slots.extend(self.armor.iter_mut());
+ slots.extend(self.armor.iter_mut().rev());
+ slots.push(&mut self.offhand);
slots.extend(self.craft_grid.iter_mut());
slots.push(&mut self.carried);
for (slot, matches) in slots.into_iter().zip(matching) {
diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs
index 2e18c2df7..d0a139850 100644
--- a/crates/horizon-client/src/game/control.rs
+++ b/crates/horizon-client/src/game/control.rs
@@ -294,6 +294,11 @@ impl Game<'_> {
if action == "tap" {
self.extras_key(physical, false, false);
}
+ // So do mods' keys (capability `input`).
+ self.mod_key(physical, action != "up", action == "repeat");
+ if action == "tap" {
+ self.mod_key(physical, false, false);
+ }
// Text screens consume raw presses; releases follow key binds.
let typing = matches!(self.screen, Screen::Chat | Screen::Options);
let key = if typing && arg(2)? != "up" {
diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs
index f027a04fc..681cf3c0e 100644
--- a/crates/horizon-client/src/game/creatures.rs
+++ b/crates/horizon-client/src/game/creatures.rs
@@ -3031,6 +3031,7 @@ impl Game<'_> {
continue;
}
}
+ self.mark_entity(entity_start, &mob.kind.id);
let light = self.world.light(
mob.position.x.floor() as i32,
(mob.position.y + mob.eye_height()).floor() as i32,
diff --git a/crates/horizon-client/src/game/game_rules_screen.rs b/crates/horizon-client/src/game/game_rules_screen.rs
index d925c5081..0992f7689 100644
--- a/crates/horizon-client/src/game/game_rules_screen.rs
+++ b/crates/horizon-client/src/game/game_rules_screen.rs
@@ -45,7 +45,8 @@ pub(super) struct RulesScreen {
initial: GameRules,
/// IntegerRuleEntry's boxes, by rule.
numbers: Vec<(&'static str, EditBox)>,
- scroll: f32,
+ /// RuleList's scrolling: the wheel and the held scrollbar.
+ scroll: ui::Scroll,
/// ConfirmScreen "Game Rule Changes" in its place.
confirm: bool,
}
@@ -120,7 +121,7 @@ impl RulesScreen {
values: rules.clone(),
initial: rules.clone(),
numbers,
- scroll: 0.0,
+ scroll: ui::Scroll::default(),
confirm: false,
}
}
@@ -156,12 +157,17 @@ impl RulesScreen {
})
.collect()
}
- fn max_scroll(rows: usize, h: f32) -> f32 {
- (ROW * rows as f32 + 4.0 - (h - HEADER - FOOTER)).max(0.0)
- }
/// Entry `i`'s top: 2 under the list's top, less the scroll.
fn row_top(&self, i: usize) -> f32 {
- HEADER + 2.0 - self.scroll.trunc() + ROW * i as f32
+ HEADER + 2.0 - self.scroll.amount.trunc() + ROW * i as f32
+ }
+ /// The list's scrollbar column (AbstractSelectionList: 6 pixels right of
+ /// the entries) and the pixels it scrolls.
+ fn bar(w: f32) -> f32 {
+ row_left(w) + ROW_WIDTH + 6.0 + 2.0
+ }
+ fn content(&self) -> f32 {
+ ROW * self.rows().len() as f32 + 4.0
}
/// getEntryAtPosition: the row under the pointer, inside the list.
fn hovered(&self, rows: usize, (x, y): (f32, f32), (w, h): (f32, f32)) -> Option {
@@ -212,10 +218,28 @@ impl RulesScreen {
/// The mouse wheel (AbstractSelectionList: half an entry a step).
pub(super) fn wheel(&mut self, delta: f32, h: f32) {
if !self.confirm {
- let max = Self::max_scroll(self.rows().len(), h);
- self.scroll = (self.scroll - delta * ROW / 2.0).clamp(0.0, max);
+ let content = self.content();
+ self.scroll
+ .wheel(delta, ROW / 2.0, HEADER, h - FOOTER, content);
}
}
+ /// A press on the scrollbar holds it (AbstractSelectionList.updateScrolling).
+ pub(super) fn press_scrollbar(&mut self, cursor: (f32, f32), (w, h): (f32, f32)) -> bool {
+ let content = self.content();
+ self.scroll
+ .press(cursor, Self::bar(w), HEADER, h - FOOTER, content)
+ }
+ /// The pointer moved while the scrollbar is held.
+ pub(super) fn drag(&mut self, y: f32, h: f32) {
+ if self.scroll.held() {
+ let content = self.content();
+ self.scroll.drag(y, HEADER, h - FOOTER, content);
+ }
+ }
+ /// The button came up: the scrollbar lets go.
+ pub(super) fn release(&mut self) {
+ self.scroll.release();
+ }
fn focused(&mut self) -> Option<(Option<&'static str>, &mut EditBox)> {
if self.search.focused {
return Some((None, &mut self.search));
@@ -229,7 +253,7 @@ impl RulesScreen {
/// number sets its rule when valid (IntegerRuleEntry's responder).
fn edited(&mut self, rule: Option<&'static str>) {
let Some(name) = rule else {
- self.scroll = 0.0;
+ self.scroll.amount = 0.0;
return;
};
let Some(r) = gamerules::rule(name) else {
@@ -286,6 +310,11 @@ impl RulesScreen {
if on(cx, cy, 150.0, 20.0) {
return self.close();
}
+ // A press on the scrollbar holds it and clicks nothing else.
+ if self.press_scrollbar(cursor, size) {
+ self.unfocus();
+ return None;
+ }
// A click moves the focus to the widget under it.
let (sx, sy) = search_at(w);
if on(sx, sy, SEARCH_WIDTH, 15.0) {
@@ -360,7 +389,8 @@ impl RulesScreen {
return;
}
let rows = self.rows();
- self.scroll = self.scroll.clamp(0.0, Self::max_scroll(rows.len(), h));
+ let content = ROW * rows.len() as f32 + 4.0;
+ self.scroll.clamp(HEADER, h - FOOTER, content);
// RuleList: the in-world list background, its entries clipped to it,
// then the separators and scrollbar.
ui::list_background(batch, atlas, HEADER, h - FOOTER, true);
@@ -421,16 +451,14 @@ impl RulesScreen {
}
batch.clip_since(first, 0.0, HEADER, w, h - FOOTER);
ui::separators(batch, atlas, HEADER, h - FOOTER, true);
- let content = ROW * rows.len() as f32 + 4.0;
- let bar_x = left + ROW_WIDTH + 6.0 + 2.0;
ui::scrollbar(
batch,
atlas,
- bar_x,
+ Self::bar(w),
HEADER,
h - FOOTER,
content,
- self.scroll.trunc(),
+ self.scroll.amount.trunc(),
);
// The header: the title centred over the search box.
let title = Text::translate("editGamerule.title", &[]);
@@ -536,6 +564,29 @@ mod tests {
);
}
+ /// AbstractScrollArea: a press on the bar holds it, a drag moves the
+ /// list (to the ends past them), a release lets go.
+ #[test]
+ fn the_scroll_bar_can_be_held_dragged_and_let_go() {
+ let mut s = RulesScreen::new(&GameRules::default());
+ let (w, h) = (427.0, 240.0);
+ let (top, bottom, content) = (HEADER, h - FOOTER, s.content());
+ assert!(
+ content > bottom - top,
+ "the rules overflow a 240-high screen"
+ );
+ let bar = RulesScreen::bar(w);
+ assert!(!s.press_scrollbar((bar - 4.0, top + 5.0), (w, h)));
+ assert!(s.press_scrollbar((bar + 3.0, top + 5.0), (w, h)));
+ s.drag(top + 40.0, h);
+ assert!(s.scroll.amount > 0.0);
+ s.drag(bottom + 10.0, h);
+ assert_eq!(s.scroll.amount, content - (bottom - top));
+ s.release();
+ s.drag(top, h);
+ assert_eq!(s.scroll.amount, content - (bottom - top));
+ }
+
#[test]
fn edits_change_rules_and_bad_numbers_hold_done() {
let mut s = RulesScreen::new(&GameRules::default());
diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs
index ee7477f28..50fa222e6 100644
--- a/crates/horizon-client/src/game/lan.rs
+++ b/crates/horizon-client/src/game/lan.rs
@@ -1136,9 +1136,9 @@ impl Game<'_> {
for e in events {
if let horizon_world::level::LevelEvent::LevelEvent { id, pos, data } = e {
// The acting guest does not predict the egg's teleport
- // particles, nor bone meal's growth (BoneMealItem.useOn's
- // levelEvent names no player).
- let except = if matches!(*id, 2015 | 1505) {
+ // particles, bone meal's growth, or anvil result sounds
+ // (these levelEvent calls name no excluded player).
+ let except = if matches!(*id, 2015 | 1505 | 1029 | 1030) {
None
} else {
self.lan_acting
diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs
index 9d4d868e0..c64836724 100644
--- a/crates/horizon-client/src/game/lan_actions.rs
+++ b/crates/horizon-client/src/game/lan_actions.rs
@@ -1808,14 +1808,13 @@ impl Game<'_> {
g.use_delay -= 1;
}
});
- // ServerPlayerGameMode.handleBlockBreakAction: a creative
- // player's START breaks the block once (destroyAndAck) and
- // leaves nothing being destroyed; its client sends no STOP.
- // Kept, the dig broke whatever was put there next, every
- // destroy delay, for as long as the player broke nothing else.
+ // ServerPlayerGameMode.handleBlockBreakAction: creative and
+ // instant survival STARTs finish without a STOP from the client.
+ // tick_mining clears mining on completion; forget the START too,
+ // so a flower or crop planted here later is not broken again.
if digging.is_some()
- && host.hub.guests[gi].creative()
&& let Some(play) = host.play.get_mut(&conn)
+ && (host.hub.guests[gi].creative() || play.mining.is_none())
{
play.digging = None;
}
diff --git a/crates/horizon-client/src/game/lan_screen.rs b/crates/horizon-client/src/game/lan_screen.rs
index 335b5fbae..2d0416d72 100644
--- a/crates/horizon-client/src/game/lan_screen.rs
+++ b/crates/horizon-client/src/game/lan_screen.rs
@@ -802,6 +802,27 @@ impl Game<'_> {
}
}
+ /// The pointer moved while a list's scrollbar is held: the game rules'
+ /// list follows it (AbstractSelectionList.mouseDragged).
+ pub(super) fn world_options_drag(&mut self) {
+ let h = self.gui_size().1;
+ if let Some(o) = self.world_options.as_mut()
+ && o.view == View::GameRules
+ && let Some(rules) = o.rules.as_mut()
+ {
+ rules.drag(self.cursor.1, h);
+ }
+ }
+
+ /// The button came up: a held scrollbar lets go (onRelease).
+ pub(super) fn world_options_release(&mut self) {
+ if let Some(o) = self.world_options.as_mut()
+ && let Some(rules) = o.rules.as_mut()
+ {
+ rules.release();
+ }
+ }
+
pub(super) fn world_options_click(&mut self) {
let Some(mut o) = self.world_options.take() else {
return;
diff --git a/crates/horizon-client/src/game/leads.rs b/crates/horizon-client/src/game/leads.rs
index 3a5112967..8867157ee 100644
--- a/crates/horizon-client/src/game/leads.rs
+++ b/crates/horizon-client/src/game/leads.rs
@@ -79,6 +79,8 @@ impl Game<'_> {
if horizon_client::mobs::leash::remove(m).is_none() {
return;
}
+ // Leashable.tickLeash gates a dying entity's lead drop on entity_drops.
+ let drop = drop && (m.alive() || self.ui.rules.bool("entity_drops"));
let at = [m.position.x, m.position.y, m.position.z];
if sound {
self.lead_sound("break", at);
diff --git a/crates/horizon-client/src/game/mod_input.rs b/crates/horizon-client/src/game/mod_input.rs
new file mode 100644
index 000000000..ac80e8e42
--- /dev/null
+++ b/crates/horizon-client/src/game/mod_input.rs
@@ -0,0 +1,92 @@
+//! Capability `input` (docs/mods.md): the use button on items a mod claims,
+//! and the mod's own keys. With no mod claiming items or keys, each hook
+//! here returns after one check.
+use super::Game;
+use winit::{event::MouseButton, keyboard::KeyCode};
+#[cfg(feature = "mods")]
+use {super::Screen, horizon_mods::HeldItem};
+
+/// A claimed item's use while the button is down.
+#[cfg(feature = "mods")]
+pub(super) struct ModUse {
+ item: HeldItem,
+ /// `Game::ticks` when it started.
+ since: u64,
+}
+
+#[cfg(not(feature = "mods"))]
+impl Game<'_> {
+ pub(super) fn mod_use(&mut self, _button: MouseButton, _pressed: bool) -> bool {
+ false
+ }
+ pub(super) fn mod_key(&mut self, _key: KeyCode, _pressed: bool, _repeat: bool) {}
+}
+
+#[cfg(feature = "mods")]
+impl Game<'_> {
+ /// The main hand's stack as `input` shows it; none when it is empty.
+ pub(super) fn mod_held(&self) -> Option {
+ let stack = self.inventory.get(self.selected)?.as_ref()?;
+ Some(HeldItem {
+ id: stack.item.to_string(),
+ count: stack.count,
+ slot: self.selected as u32,
+ })
+ }
+ /// The use button while playing: true when a mod claimed the held item,
+ /// so the game does nothing else with it (no placing, eating, swing).
+ pub(super) fn mod_use(&mut self, button: MouseButton, pressed: bool) -> bool {
+ if button != MouseButton::Right || !self.mods.mods.claims_any() {
+ return false;
+ }
+ if !pressed {
+ return self.stop_mod_use();
+ }
+ let Some(item) = self.mod_held().filter(|h| self.mods.mods.claims(&h.id)) else {
+ return false;
+ };
+ self.mods.mods.use_start(&item);
+ self.mods.using = Some(ModUse {
+ item,
+ since: self.ticks,
+ });
+ true
+ }
+ /// Ends a claimed use: the button came up, the player switched slots,
+ /// or a screen opened. False when none was going.
+ pub(super) fn stop_mod_use(&mut self) -> bool {
+ let Some(u) = self.mods.using.take() else {
+ return false;
+ };
+ let ticks = self.ticks.saturating_sub(u.since).min(u64::from(u32::MAX)) as u32;
+ self.mods.mods.use_stop(&u.item, ticks);
+ true
+ }
+ /// Once a tick: a claimed use stops when its item leaves the hand or a
+ /// screen opens, as vanilla's `releaseUsingItem`.
+ pub(super) fn tick_mod_use(&mut self) {
+ let Some(u) = &self.mods.using else {
+ return;
+ };
+ let still = self
+ .mod_held()
+ .is_some_and(|h| h.id == u.item.id && h.slot == u.item.slot);
+ if !still || self.screen != Screen::Playing {
+ self.stop_mod_use();
+ }
+ }
+ /// A key went down or up: a mod's registered key while playing (no
+ /// screen) is its `key-down` / `key-up`. Vanilla's mapping on the same
+ /// key still runs.
+ pub(super) fn mod_key(&mut self, key: KeyCode, pressed: bool, repeat: bool) {
+ if repeat || !self.mods.mods.has_keys() {
+ return;
+ }
+ if pressed && (self.screen != Screen::Playing || self.title.is_some()) {
+ return;
+ }
+ if let Some(name) = horizon_client::keybinds::key_name(key) {
+ self.mods.mods.key(name, pressed);
+ }
+ }
+}
diff --git a/crates/horizon-client/src/game/mod_render.rs b/crates/horizon-client/src/game/mod_render.rs
new file mode 100644
index 000000000..1c3a20621
--- /dev/null
+++ b/crates/horizon-client/src/game/mod_render.rs
@@ -0,0 +1,176 @@
+//! Capability `render` (docs/mods.md): the shapes mods keep on the HUD,
+//! drawn by the game's own HUD renderer, and their camera shakes. Mods
+//! never supply shader code. With no mod drawing, each hook here returns
+//! after one check.
+use super::Game;
+use horizon_client::hud::HudBatch;
+#[cfg(feature = "mods")]
+use {
+ horizon_client::{hud::rgba, items::ItemStack, text::Text},
+ horizon_mods::Shape,
+ std::time::Instant,
+};
+
+/// The camera's jolts from `render.shake`, while any is fading.
+#[cfg(feature = "mods")]
+#[derive(Default)]
+pub(super) struct Shake {
+ jolts: Vec,
+}
+
+#[cfg(feature = "mods")]
+struct Jolt {
+ start: Instant,
+ strength: f32,
+ seconds: f32,
+}
+
+/// The HUD batch's colour (ABGR) from a mod's 0xAARRGGBB.
+#[cfg(feature = "mods")]
+fn argb(color: u32) -> u32 {
+ let [a, r, g, b] = color.to_be_bytes();
+ rgba(r, g, b, a)
+}
+
+/// The view's turn at full strength, in degrees: yaw, then pitch.
+#[cfg(feature = "mods")]
+const SHAKE_DEGREES: (f32, f32) = (3.0, 2.0);
+
+#[cfg(feature = "mods")]
+impl Shake {
+ pub(super) fn add(&mut self, strength: f32, seconds: f32) {
+ self.jolts.push(Jolt {
+ start: Instant::now(),
+ strength,
+ seconds,
+ });
+ }
+ /// How far the view turns now (yaw, pitch degrees), `scale` being the
+ /// player's Distortion Effects; fades jolts out and forgets ended ones.
+ fn offset(&mut self, now: Instant, scale: f32) -> (f32, f32) {
+ self.jolts
+ .retain(|j| now.duration_since(j.start).as_secs_f32() < j.seconds);
+ let mut strength = 0.0;
+ for j in &self.jolts {
+ let left = 1.0 - now.duration_since(j.start).as_secs_f32() / j.seconds;
+ strength += j.strength * left * left;
+ }
+ let strength = strength.min(1.0) * scale.clamp(0.0, 1.0);
+ if strength <= 0.0 {
+ return (0.0, 0.0);
+ }
+ // Smooth noise: sines at unrelated rates, so it never repeats visibly.
+ let t = self
+ .jolts
+ .first()
+ .map_or(0.0, |j| now.duration_since(j.start).as_secs_f32());
+ let wave = |a: f32, b: f32, c: f32| {
+ ((t * a).sin() * 0.5 + (t * b).sin() * 0.3 + (t * c).sin() * 0.2).clamp(-1.0, 1.0)
+ };
+ (
+ strength * SHAKE_DEGREES.0 * wave(23.0, 37.3, 51.7),
+ strength * SHAKE_DEGREES.1 * wave(29.1, 43.9, 61.3),
+ )
+ }
+}
+
+#[cfg(not(feature = "mods"))]
+impl Game<'_> {
+ pub(super) fn draw_mod_shapes(&self, _batch: &mut HudBatch) {}
+ pub(super) fn shaken_view(&mut self, yaw: f32, pitch: f32) -> (f32, f32) {
+ (yaw, pitch)
+ }
+}
+
+#[cfg(feature = "mods")]
+impl Game<'_> {
+ /// The running mods' HUD shapes, in order, over the HUD.
+ pub(super) fn draw_mod_shapes(&self, batch: &mut HudBatch) {
+ let atlas = &self.gui.atlas;
+ for shape in self.mods.mods.hud_shapes() {
+ match shape {
+ Shape::Fill(f) => {
+ let r = f.rect;
+ batch.fill(atlas, r.x, r.y, r.w, r.h, argb(f.color));
+ }
+ Shape::Quad(q) => {
+ let (a, b, c, d) = q.corners;
+ let corners = [[a.x, a.y], [b.x, b.y], [c.x, c.y], [d.x, d.y]];
+ batch.fill_quad(atlas, corners, argb(q.color));
+ }
+ Shape::Sprite(s) => {
+ let r = s.rect;
+ let name = s.sprite.strip_prefix("minecraft:").unwrap_or(&s.sprite);
+ let name = format!("gui/sprites/{name}");
+ batch.sprite_scaled(atlas, &name, r.x, r.y, r.w, r.h, argb(s.color));
+ }
+ Shape::Item(i) => {
+ if self.res.items().get(&i.item).is_none() {
+ continue;
+ }
+ let start = batch.vertices.len();
+ let stack = ItemStack::new(i.item.as_str(), 1);
+ self.draw_item_model(batch, &stack, i.at.x, i.at.y);
+ batch.scale_since(start, i.at.x, i.at.y, i.scale, i.scale);
+ }
+ Shape::Text(t) => {
+ let text = Text::literal(t.text.clone());
+ let x = if t.centered {
+ t.at.x - (HudBatch::text_width(atlas, &t.text) / 2.0).floor()
+ } else {
+ t.at.x
+ };
+ batch.text_styled(
+ atlas,
+ &text,
+ x,
+ t.at.y,
+ argb(t.color | 0xFF00_0000),
+ t.shadow,
+ );
+ }
+ }
+ }
+ }
+ /// The view's yaw and pitch after the mods' camera shakes; unchanged
+ /// while none is fading.
+ pub(super) fn shaken_view(&mut self, yaw: f32, pitch: f32) -> (f32, f32) {
+ if self.mods.shake.jolts.is_empty() {
+ return (yaw, pitch);
+ }
+ let scale = self.settings.file.double("screenEffectScale") as f32;
+ let (dy, dp) = self.mods.shake.offset(Instant::now(), scale);
+ (yaw + dy, (pitch + dp).clamp(-90.0, 90.0))
+ }
+}
+
+#[cfg(all(test, feature = "mods"))]
+mod tests {
+ use super::*;
+ use std::time::Duration;
+
+ #[test]
+ fn a_jolt_fades_out_and_distortion_scales_it() {
+ let mut shake = Shake::default();
+ shake.add(1.0, 2.0);
+ let start = shake.jolts[0].start;
+ let at = |s: f32| start + Duration::from_secs_f32(s);
+ let size = |(y, p): (f32, f32)| y.abs() + p.abs();
+ // Off with Distortion Effects at 0, inside the limits otherwise.
+ assert_eq!(shake.offset(at(0.3), 0.0), (0.0, 0.0));
+ let (y, p) = shake.offset(at(0.3), 1.0);
+ assert!(y.abs() <= SHAKE_DEGREES.0 && p.abs() <= SHAKE_DEGREES.1);
+ // Fading: the largest turn over a window shrinks.
+ let mut peak = |from: f32| {
+ (0..50)
+ .map(|i| size(shake.offset(at(from + i as f32 * 0.002), 1.0)))
+ .fold(0.0, f32::max)
+ };
+ let early = peak(0.1);
+ let late = peak(1.6);
+ assert!(late < early, "{late} !< {early}");
+ // Gone once it ends.
+ assert_eq!(shake.offset(at(2.1), 1.0), (0.0, 0.0));
+ assert!(shake.jolts.is_empty());
+ }
+}
diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs
index 931418931..667c06335 100644
--- a/crates/horizon-client/src/game/mods.rs
+++ b/crates/horizon-client/src/game/mods.rs
@@ -33,7 +33,13 @@ use {
#[derive(Default)]
pub(super) struct ModsState {
#[cfg(feature = "mods")]
- mods: Mods,
+ pub(super) mods: Mods,
+ /// Capability `input`: a claimed item's use while the button is down.
+ #[cfg(feature = "mods")]
+ pub(super) using: Option,
+ /// Capability `render`'s camera shakes.
+ #[cfg(feature = "mods")]
+ pub(super) shake: super::mod_render::Shake,
/// `--dev-mod PATH`: always on, reloaded when rebuilt.
pub(super) dev: Vec,
/// When dev mods were last checked for a rebuild.
@@ -338,7 +344,12 @@ impl Game<'_> {
game_time: self.world.game_time.max(0) as u64,
day_time: self.world.day_time.max(0) as u64,
});
- Snapshot { player, clock }
+ Snapshot {
+ player,
+ clock,
+ gui_size: self.gui_size(),
+ held: self.mod_held(),
+ }
}
/// A game tick.
pub(super) fn tick_mods(&mut self) {
@@ -350,6 +361,7 @@ impl Game<'_> {
if !self.mods.mods.is_running() {
return;
}
+ self.tick_mod_use();
let snapshot = self.mod_snapshot();
self.mods.mods.set_snapshot(snapshot);
self.mods.mods.tick(self.ticks);
@@ -430,6 +442,10 @@ impl Game<'_> {
self.open_mod_screen(owner, false);
continue;
}
+ Output::Shake { strength, seconds } => {
+ self.mods.shake.add(strength, seconds);
+ continue;
+ }
Output::CloseScreen { owner } => {
if self.mods.screen.as_ref().is_some_and(|s| s.owner == owner) {
self.close_mod_screen(false);
@@ -473,30 +489,28 @@ impl Game<'_> {
}
/// A `ui` screen's title and widgets as the mod last pushed them; None
- /// when the surface is gone (the mod stopped or closed it).
- fn mod_ui_view(&self) -> Option<(String, Vec)> {
- let ms = self.mods.screen.as_ref()?;
+ /// when the surface is gone (the mod stopped or closed it). Borrows
+ /// `mods` only, so `self.mods.screen` may stay borrowed for the scrolls.
+ fn mod_ui_view<'a>(mods: &'a Mods, ms: &ModScreen) -> Option<(&'a str, &'a [UiWidget])> {
if ms.settings {
- let widgets = self.mods.mods.settings_widgets(ms.owner);
+ let widgets = mods.settings_widgets(ms.owner);
if widgets.is_empty() {
return None;
}
- let name = self
- .mods
- .mods
+ let name = mods
.entries()
.get(ms.owner)
.and_then(|e| e.manifest.as_ref())
.map_or("Mod", |m| m.name.as_str());
- Some((name.to_string(), widgets.to_vec()))
+ Some((name, widgets))
} else {
- let s: &UiScreen = self.mods.mods.screen(ms.owner)?;
- Some((s.title.clone(), s.widgets.clone()))
+ let s: &UiScreen = mods.screen(ms.owner)?;
+ Some((s.title.as_str(), s.widgets.as_slice()))
}
}
/// The layout the mod screen's widgets get at `h`: (widget, top,
/// height), the column being `COLUMN` wide down the middle.
- fn mod_ui_layout(&self, h: f32, widgets: &[UiWidget]) -> Vec<(f32, f32)> {
+ fn mod_ui_layout(h: f32, widgets: &[UiWidget]) -> Vec<(f32, f32)> {
let mut out = Vec::with_capacity(widgets.len());
let mut y = MOD_UI_TOP;
for w in widgets {
@@ -561,13 +575,19 @@ impl Game<'_> {
/// The mod's screen or settings page: its title over widgets stacked
/// down the middle, a Done button, Esc to leave (vanilla's menus).
pub(super) fn draw_mod_screen(&mut self, batch: &mut HudBatch) {
- if self.mods.screen.is_none() {
+ let Some(ms) = self.mods.screen.as_mut() else {
+ // The screen was dropped while it was up: back to the world,
+ // mouse held again like any return to Playing.
self.screen = Screen::Playing;
+ if self.player.is_some() {
+ self.capture(true);
+ }
return;
- }
- let Some((title, widgets)) = self.mod_ui_view() else {
- self.mods.screen = None;
- self.screen = Screen::Playing;
+ };
+ let Some((title, widgets)) = Self::mod_ui_view(&self.mods.mods, ms) else {
+ // The mod's surface is gone (it stopped or cleared it): leave
+ // the screen like Done did, without `closed` — the mod ended it.
+ self.close_mod_screen(false);
return;
};
// Item icons draw in a second pass: `draw_item` needs `&mut self`,
@@ -587,7 +607,7 @@ impl Game<'_> {
);
let cx = (batch.width / 2.0).floor();
let x = cx - MOD_UI_COLUMN / 2.0;
- let layout = self.mod_ui_layout(batch.height, &widgets);
+ let layout = Self::mod_ui_layout(batch.height, widgets);
let mut list_i = 0usize;
for (w, &(wy, wh)) in widgets.iter().zip(&layout) {
match w {
@@ -628,11 +648,8 @@ impl Game<'_> {
);
}
UiWidget::List(l) => {
- let scroll = {
- let ms = self.mods.screen.as_mut().expect("checked");
- ms.scrolls.resize_with(list_i + 1, ui::Scroll::default);
- ms.scrolls[list_i]
- };
+ ms.scrolls.resize_with(list_i + 1, ui::Scroll::default);
+ let scroll = ms.scrolls[list_i];
list_i += 1;
let rows_top = wy + 4.0;
let rows_bottom = wy + wh - 4.0;
@@ -703,53 +720,53 @@ impl Game<'_> {
self.close_mod_screen(true);
return;
}
- let Some((_, widgets)) = self.mod_ui_view() else {
+ let Some(ms) = self.mods.screen.as_ref() else {
return;
};
+ let Some((_, widgets)) = Self::mod_ui_view(&self.mods.mods, ms) else {
+ return;
+ };
+ let owner = ms.owner;
let x = (w / 2.0).floor() - MOD_UI_COLUMN / 2.0;
- let layout = self.mod_ui_layout(h, &widgets);
+ let layout = Self::mod_ui_layout(h, widgets);
let mut list_i = 0usize;
+ // The `ui_select` call needs `&mut`: find the pick first.
+ let mut hit = None;
for (w, &(wy, wh)) in widgets.iter().zip(&layout) {
let UiWidget::List(l) = w else { continue };
- let scroll = self
- .mods
- .screen
- .as_ref()
- .and_then(|ms| ms.scrolls.get(list_i).copied())
- .unwrap_or_default();
+ let scroll = ms.scrolls.get(list_i).copied().unwrap_or_default();
list_i += 1;
let row = ((cursor.1 - (wy + 4.0) + scroll.amount) / 12.0) as i64;
if ui::hovered(cursor, x + 1.0, wy + 1.0, MOD_UI_COLUMN - 7.0, wh - 2.0)
&& row >= 0
&& (row as usize) < l.rows.len()
{
- self.audio.play_button_click();
- self.mods.mods.ui_select(
- self.mods.screen.as_ref().map_or(0, |ms| ms.owner),
- l.id,
- row as u32,
- );
- self.show_mod_output();
- return;
+ hit = Some((l.id, row as u32));
+ break;
}
}
+ if let Some((id, row)) = hit {
+ self.audio.play_button_click();
+ self.mods.mods.ui_select(owner, id, row);
+ self.show_mod_output();
+ }
}
/// The wheel over a `list` scrolls it.
pub(super) fn mod_screen_scroll(&mut self, delta: f32) {
let (w, h) = self.gui_size();
let cursor = self.cursor;
- let Some((_, widgets)) = self.mod_ui_view() else {
+ let Some(ms) = self.mods.screen.as_mut() else {
+ return;
+ };
+ let Some((_, widgets)) = Self::mod_ui_view(&self.mods.mods, ms) else {
return;
};
let x = (w / 2.0).floor() - MOD_UI_COLUMN / 2.0;
- let layout = self.mod_ui_layout(h, &widgets);
+ let layout = Self::mod_ui_layout(h, widgets);
let mut list_i = 0usize;
for (w, &(wy, wh)) in widgets.iter().zip(&layout) {
let UiWidget::List(l) = w else { continue };
let over = ui::hovered(cursor, x, wy, MOD_UI_COLUMN, wh);
- let Some(ms) = self.mods.screen.as_mut() else {
- return;
- };
ms.scrolls.resize_with(list_i + 1, ui::Scroll::default);
if over {
let content = l.rows.len() as f32 * 12.0;
diff --git a/crates/horizon-client/src/game/names.rs b/crates/horizon-client/src/game/names.rs
index 7655fb489..f249bd01f 100644
--- a/crates/horizon-client/src/game/names.rs
+++ b/crates/horizon-client/src/game/names.rs
@@ -240,6 +240,42 @@ impl Game<'_> {
discrete: false,
});
}
+ // ItemFrameRenderer.shouldShowName/getNameTag: only the targeted
+ // frame's custom-named item, with the HUD visible (also glow frames).
+ if !self.ui.hide_gui
+ && let Some((i, distance)) = self.frame_hit()
+ && self.before_block(distance)
+ && self.mob_hit().is_none_or(|(_, d)| d > distance)
+ && let Some(name) = self.item_frames[i]
+ .item
+ .as_ref()
+ .and_then(|item| item.custom_name())
+ {
+ let frame = &self.item_frames[i];
+ let (lo, hi) = frame.bounds();
+ let center = glam::DVec3::from_array(std::array::from_fn(|a| (lo[a] + hi[a]) / 2.0));
+ if center.distance_squared(camera) < 64.0 * 64.0 {
+ // NAME_TAG is at the entity height (0.5), then the name
+ // submitter lifts it 0.5. ItemFrameRenderer offsets rendering
+ // 0.3 toward its facing and -0.25 vertically.
+ let step = frame.step();
+ tags.push(NameTag {
+ at: center
+ + glam::DVec3::new(
+ f64::from(step[0] as f32 * 0.3),
+ 0.75,
+ f64::from(step[2] as f32 * 0.3),
+ ),
+ lines: vec![(Text::literal(name).resolve_legacy(), 0.0)],
+ light: self.world.light(
+ center.x.floor() as i32,
+ center.y.floor() as i32,
+ center.z.floor() as i32,
+ ),
+ discrete: false,
+ });
+ }
+ }
tags
}
/// `SubmitNodeCollection.submitNameTag`: a discrete name is drawn once,
@@ -257,12 +293,16 @@ impl Game<'_> {
const FADED: u32 = 0x7F;
for tag in self.name_tags(camera, alpha) {
let o = tag.at - camera;
+ // `gbuffers_entities` draws name tag text (2.3); a sneaking player's faded tag, on its
+ // background, blends.
if tag.discrete {
+ self.mark_translucent_entity(out.len(), "minecraft:name_tag");
self.name_tag_quads(o, &tag.lines, FADED, true, 0, vertex_light(tag.light), out);
continue;
}
// LightCoordsUtil.lightCoordsWithEmission(lightCoords, 2).
let lit = (tag.light >> 4).max(2) << 4 | (tag.light & 15).max(2);
+ self.mark_entity(out.len(), "minecraft:name_tag");
self.name_tag_quads(o, &tag.lines, 0xFF, false, 0, vertex_light(lit), out);
// RenderPipelines.TEXT_SEE_THROUGH: no depth test, no lightmap.
self.name_tag_quads(o, &tag.lines, FADED, true, 2, 0, see_through);
diff --git a/crates/horizon-client/src/game/packs.rs b/crates/horizon-client/src/game/packs.rs
index 733b19649..ee57e9f2b 100644
--- a/crates/horizon-client/src/game/packs.rs
+++ b/crates/horizon-client/src/game/packs.rs
@@ -142,6 +142,8 @@ impl Game<'_> {
}
let server = &self.server_packs.active;
let root = startup_root(&vanilla, &self.settings.resource_packs, server)?;
+ let root =
+ super::mods::content_root(&root, &self.settings.mods.enabled, &self.options.dev_mods);
self.reload_resources(&root)
}
fn reload_resources(&mut self, root: &Path) -> horizon_client::Result<()> {
@@ -192,6 +194,10 @@ impl Game<'_> {
let context = pack.context.clone();
self.pack = pack;
self.gui = gui;
+ // The atlas's sprites moved: the particle engine re-reads every rect
+ // and the packs' sprite sets, or every particle draws a stale region.
+ self.particles
+ .reload(root, &self.gui.atlas.sprites, &self.gui.atlas.translucent);
horizon_client::ui_theme::reset_atlas();
// Villager hats come from the pack's texture metadata (shared data, swapped in place).
*self
diff --git a/crates/horizon-client/src/game/sculk.rs b/crates/horizon-client/src/game/sculk.rs
index a11a643c1..63909c592 100644
--- a/crates/horizon-client/src/game/sculk.rs
+++ b/crates/horizon-client/src/game/sculk.rs
@@ -718,17 +718,24 @@ impl Game<'_> {
})
};
// The ticking ones in the index's order, with whether the chunks
- // around them tick too.
+ // around them tick too. Most sensors and catalysts wait for a
+ // vibration or a death: their tick would change nothing, so they
+ // are passed over here (a vibration scheduled during these ticks is
+ // chosen on the next tick at the earliest, as in vanilla).
+ let entities = &self.world.block_entities;
let due: Vec<((i32, i32, i32), bool)> = self
.sculk
.index
.iter()
.filter_map(|&key| {
let (cx, cz) = (key.0 >> 4, key.2 >> 4);
- ticks(cx, cz).then(|| {
- let adjacent = (-1..=1).all(|dx| (-1..=1).all(|dz| ticks(cx + dx, cz + dz)));
- (key, adjacent)
- })
+ if !ticks(cx, cz)
+ || matches!(entities.get(&key), Some(BlockEntity::Sculk(e)) if e.idle())
+ {
+ return None;
+ }
+ let adjacent = (-1..=1).all(|dx| (-1..=1).all(|dz| ticks(cx + dx, cz + dz)));
+ Some((key, adjacent))
})
.collect();
for (key, adjacent) in due {
diff --git a/crates/horizon-client/src/game/shader_packs.rs b/crates/horizon-client/src/game/shader_packs.rs
index 04af297b6..f0087c4b0 100644
--- a/crates/horizon-client/src/game/shader_packs.rs
+++ b/crates/horizon-client/src/game/shader_packs.rs
@@ -4,9 +4,30 @@
use horizon_client::camera::CameraFluid;
use horizon_client::gfx::pack_uniforms::PackInputs;
-use horizon_shaders::{Macros, Pack, Properties, dimension_folder, load_post};
+use horizon_shaders::{BlockIds, Macros, Pack, Properties, dimension_folder, load_post};
use super::{Dim, Game};
+use horizon_client::gfx::pack_draw::EntitySpan;
+use std::cell::RefCell;
+use std::collections::HashMap;
+
+/// The ids a pack gives entities and block entities (4.1, 5.3), for the spans of the entity
+/// geometry.
+pub(super) struct PackIds {
+ entities: HashMap,
+ blocks: BlockIds,
+ /// `blockEntityId` by block state, filled as states are drawn.
+ block_states: RefCell>,
+ /// The id of a name the file lacks: 65535, or 0 without the file.
+ missing_entity: i32,
+ missing_block: i32,
+ /// `item.properties` (4.1) and the id of a held item the file lacks, or of an empty hand:
+ /// −1, or 0 without the file (5.3).
+ items: HashMap,
+ missing_item: i32,
+ /// `oldHandLight` (3.2): the first held item and light are those of the brighter hand.
+ old_hand_light: bool,
+}
impl Game<'_> {
/// Loads the pack for the current dimension when it changed, and returns this frame's
@@ -42,8 +63,49 @@ impl Game<'_> {
);
}
let folder = dimension_folder(&pack, dimension);
- let post = load_post(&pack, &folder, &properties, ¯os, None);
- if let Some((programs, notes)) = renderer.set_shader_pack(dimension, Some(post))
+ let inputs = renderer.pack_inputs();
+ let mut post = load_post(&pack, &folder, &properties, ¯os, &inputs);
+ let biomes = self.res.biomes();
+ let names: Vec<&str> = biomes.list.iter().map(|b| b.name.as_str()).collect();
+ post.custom.set_biomes(&names);
+ let text = pack.read_text("block.properties").unwrap_or_default();
+ let ids = BlockIds::parse(&Properties::parse(&text, ¯os));
+ for note in &ids.notes {
+ log::warn!("shader pack {name}: block.properties: {note}");
+ }
+ let text = pack.read_text("entity.properties");
+ let entities = BlockIds::parse_kind(
+ &Properties::parse(text.as_deref().unwrap_or_default(), ¯os),
+ "entity",
+ );
+ let text = pack.read_text("item.properties");
+ let items = BlockIds::parse_kind(
+ &Properties::parse(text.as_deref().unwrap_or_default(), ¯os),
+ "item",
+ );
+ self.pack_ids = Some(PackIds {
+ items: items.plain(),
+ missing_item: if text.is_some() { -1 } else { 0 },
+ // OQ-21: on unless the pack turns it off.
+ old_hand_light: properties
+ .get("oldHandLight")
+ .is_none_or(|v| v.trim() != "false"),
+ entities: entities.plain(),
+ missing_entity: if text.is_some() { 65535 } else { 0 },
+ missing_block: if ids.is_empty() { 0 } else { 65535 },
+ blocks: ids.clone(),
+ block_states: RefCell::default(),
+ });
+ let started_ids = std::time::Instant::now();
+ let table = block_id_table(&self.pack, &ids);
+ log::info!(
+ "shader pack {name}: mc_Entity for {} of {} block sprites in {} ms",
+ table.chunks(2).filter(|p| p[0] != -1).count(),
+ table.len() / 2,
+ started_ids.elapsed().as_millis()
+ );
+ if let Some((programs, notes)) =
+ renderer.set_shader_pack(dimension, Some(post), &table)
{
for note in notes {
log::warn!("shader pack {name}: {note}");
@@ -57,14 +119,22 @@ impl Game<'_> {
}
}
Err(e) => {
+ self.pack_ids = None;
log::warn!("shader pack {name}: {e}");
- renderer.set_shader_pack(dimension, None);
+ renderer.set_shader_pack(dimension, None, &[]);
}
}
}
let lerp = |(old, new): (f32, f32)| old + (new - old) * partial;
let at = camera.floor().as_ivec3();
let light = self.world.light(at.x, at.y, at.z);
+ let biome = self
+ .world
+ .chunks
+ .get(&(at.x >> 4, at.z >> 4))
+ .map(|c| c.biome(at.x, at.y, at.z));
+ let (held_items, held_light) = self.held_for_pack();
+ let info = biome.map(|b| self.res.biomes().get(b));
PackInputs {
day_time: self.world.day_time,
rain: lerp(self.world.weather.rain_level),
@@ -79,6 +149,191 @@ impl Game<'_> {
eye_light: [i32::from(light & 15), i32::from(light >> 4)],
hide_gui: self.ui.hide_gui,
brightness: self.settings.gamma,
+ biome: biome.map_or(-1, i32::from),
+ biome_category: info.map_or(0, |b| horizon_shaders::biome_category(&b.name)),
+ biome_precipitation: info.map_or(0, |b| {
+ horizon_shaders::biome_precipitation(b.precipitation, b.temperature)
+ }),
+ temperature: info.map_or(0.0, |b| b.temperature),
+ rainfall: info.map_or(0.0, |b| b.downfall),
+ held_items,
+ held_light,
+ }
+ }
+
+ /// `heldItemId(2)` and `heldBlockLightValue(2)`: main hand, then off hand (5.2, 5.3).
+ fn held_for_pack(&mut self) -> ([i32; 2], [i32; 2]) {
+ if self.pack_ids.is_none() {
+ return ([0; 2], [0; 2]);
+ }
+ if self.luminance.is_none() {
+ self.luminance = Some(horizon_client::dynamic_light::Luminance::new(
+ self.res.facts(),
+ self.pack.assets.registry,
+ ));
+ }
+ let (Some(ids), Some(lum)) = (&self.pack_ids, &self.luminance) else {
+ return ([0; 2], [0; 2]);
+ };
+ let hands = [self.selected, super::shield::OFFHAND_SLOT].map(|slot| {
+ let item = self.stack_in(slot).map(|s| &*s.item);
+ let id = item.and_then(|i| ids.items.get(i).copied());
+ (
+ id.unwrap_or(ids.missing_item),
+ item.map_or(0, |i| i32::from(lum.of(i))),
+ )
+ });
+ let first = if ids.old_hand_light && hands[1].1 > hands[0].1 {
+ hands[1]
+ } else {
+ hands[0]
+ };
+ ([first.0, hands[1].0], [first.1, hands[1].1])
+ }
+}
+
+impl Game<'_> {
+ /// Starts a span of the entity geometry at vertex `start` for entity type `name`
+ /// (`minecraft:zombie`; "" for one the pack cannot name). Nothing without a pack.
+ pub(super) fn mark_entity(&self, start: usize, name: &str) {
+ self.mark_entity_part(start, name, false);
+ }
+
+ /// Starts a span for entity geometry that blends (name tags, shadows).
+ pub(super) fn mark_translucent_entity(&self, start: usize, name: &str) {
+ self.mark_entity_part(start, name, true);
+ }
+
+ fn mark_entity_part(&self, start: usize, name: &str, translucent: bool) {
+ if let Some(ids) = &self.pack_ids {
+ let id = ids.entities.get(name).copied();
+ self.push_span(start, "ENTITIES", id.unwrap_or(ids.missing_entity));
+ if let Some(last) = self.entity_spans.borrow_mut().last_mut() {
+ last.translucent = translucent;
+ }
+ }
+ }
+
+ /// Starts a span for the block entity of block state `state`.
+ pub(super) fn mark_block_entity(&self, start: usize, state: u32) {
+ let Some(ids) = &self.pack_ids else { return };
+ let id = *ids
+ .block_states
+ .borrow_mut()
+ .entry(state)
+ .or_insert_with(|| {
+ let registry = self.pack.assets.registry;
+ registry
+ .state(horizon_world::registry::BlockStateId(state))
+ .and_then(|s| {
+ ids.blocks
+ .id(&s.name, |k| s.properties.get(k).map(String::as_str))
+ })
+ .unwrap_or(ids.missing_block)
+ });
+ self.push_span(start, "BLOCK_ENTITIES", id);
+ }
+
+ /// Starts a span of geometry that is not an entity (`PARTICLES`, `DESTROY`...).
+ pub(super) fn mark_stage(&self, start: usize, stage: &str) {
+ if self.pack_ids.is_some() {
+ self.push_span(start, stage, 0);
+ }
+ }
+
+ fn push_span(&self, start: usize, stage: &str, id: i32) {
+ let mut spans = self.entity_spans.borrow_mut();
+ let span = EntitySpan {
+ start: start as u32,
+ stage: horizon_shaders::render_stage(stage),
+ id,
+ translucent: false,
+ };
+ // A span that drew nothing gives way to the next.
+ match spans.last_mut() {
+ Some(last) if last.start == span.start => *last = span,
+ _ => spans.push(span),
}
}
}
+
+/// `mc_Entity` (x, y) of each block sprite layer, as (id, fluid) pairs: terrain vertices carry
+/// their sprite layer, not their block. A layer takes the id of the block named like its sprite
+/// (`block/oak_leaves` for `oak_leaves`), else the id most blocks drawing it share (ties: the
+/// lowest); -1 when no block drawing it has one. y is 1 for the fluid sprites, -1 otherwise.
+fn block_id_table(pack: &super::PackResources, ids: &BlockIds) -> Vec {
+ use std::collections::{BTreeMap, BTreeSet};
+ let layers = pack
+ .render
+ .sprite_layers
+ .values()
+ .max()
+ .map_or(0, |l| l + 1) as usize;
+ let mut table = vec![-1; layers * 2];
+ if ids.is_empty() {
+ return table;
+ }
+ let registry = pack.assets.registry;
+ let catalog = &pack.render.model_catalog;
+ let sprite = |name: &str| pack.render.sprite_layers.get(name).copied();
+ // (layer, id) -> (a block named like the sprite, the blocks).
+ let mut votes: BTreeMap<(u32, i32), (bool, BTreeSet<&str>)> = BTreeMap::new();
+ for i in 0..registry.len() {
+ let state_id = horizon_world::registry::BlockStateId(i as u32);
+ let Some(state) = registry.state(state_id) else {
+ continue;
+ };
+ let Some(id) = ids.id(&state.name, |k| state.properties.get(k).map(String::as_str)) else {
+ continue;
+ };
+ let block = state.name.strip_prefix("minecraft:").unwrap_or(&state.name);
+ let mut vote = |layer: u32| {
+ let named = pack
+ .layer_names
+ .get(&layer)
+ .and_then(|n| n.strip_prefix("block/"))
+ == Some(block);
+ let v = votes.entry((layer, id)).or_default();
+ v.0 |= named;
+ v.1.insert(&state.name);
+ };
+ // Fluids draw from their own sprites.
+ let fluid = match block {
+ "water" => ["water_still", "water_flow"],
+ "lava" => ["lava_still", "lava_flow"],
+ _ => ["", ""],
+ };
+ for f in fluid.iter().filter(|f| !f.is_empty()) {
+ if let Some(l) = sprite(&format!("minecraft:block/{f}")) {
+ vote(l);
+ }
+ }
+ let origin = horizon_core::position::BlockPos::new(0, 0, 0);
+ for (_, model) in catalog.collect(state_id, origin).unwrap_or_default() {
+ for face in &model.faces {
+ vote(face.layer);
+ }
+ }
+ }
+ let mut best: BTreeMap = BTreeMap::new();
+ for ((layer, id), (named, blocks)) in votes {
+ let candidate = (named, blocks.len(), -id);
+ let b = best.entry(layer).or_insert(candidate);
+ if candidate > *b {
+ *b = candidate;
+ }
+ }
+ for (layer, (_, _, id)) in best {
+ if let Some(slot) = table.get_mut(layer as usize * 2) {
+ *slot = -id;
+ }
+ }
+ for f in ["water_still", "water_flow", "lava_still", "lava_flow"] {
+ if let Some(l) = sprite(&format!("minecraft:block/{f}"))
+ && let Some(slot) = table.get_mut(l as usize * 2 + 1)
+ {
+ *slot = 1;
+ }
+ }
+ table
+}
diff --git a/crates/horizon-client/src/game/startup_menu.rs b/crates/horizon-client/src/game/startup_menu.rs
index a5d658a4d..f7be6241f 100644
--- a/crates/horizon-client/src/game/startup_menu.rs
+++ b/crates/horizon-client/src/game/startup_menu.rs
@@ -272,8 +272,6 @@ impl Loading {
}
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);
}
diff --git a/crates/horizon-client/src/game/stats_screen.rs b/crates/horizon-client/src/game/stats_screen.rs
index 5505d423d..bd7663e49 100644
--- a/crates/horizon-client/src/game/stats_screen.rs
+++ b/crates/horizon-client/src/game/stats_screen.rs
@@ -158,7 +158,8 @@ const COLUMNS: [(&str, &str); 6] = [
#[derive(Default)]
pub(super) struct StatsUi {
tab: usize,
- scroll: f32,
+ /// The list's scrolling: the wheel and the held scrollbar.
+ scroll: horizon_client::ui::Scroll,
sort: Option<(usize, i32)>,
}
/// `MenuTabBar` geometry: three tabs over at most 400 pixels.
@@ -182,6 +183,15 @@ impl Game<'_> {
self.stats_ui = StatsUi::default();
self.screen = Screen::Stats;
}
+ /// The list's scrollbar column (right of the 280 pixel list) and the
+ /// pixels it scrolls.
+ fn stats_bar(w: f32) -> f32 {
+ ((w - 280.0) / 2.0).floor() + 280.0 + 6.0
+ }
+ fn stats_content(&self) -> f32 {
+ let (row_h, rows) = self.stats_rows();
+ row_h * rows as f32 + 4.0
+ }
fn stats_rows(&self) -> (f32, usize) {
match self.stats_ui.tab {
0 => (14.0, CUSTOM.len()),
@@ -260,10 +270,9 @@ impl Game<'_> {
// The list.
let (row_h, rows) = self.stats_rows();
let content = row_h * rows as f32 + 4.0;
- let max_scroll = (content - (bottom - top)).max(0.0);
- self.stats_ui.scroll = self.stats_ui.scroll.clamp(0.0, max_scroll);
+ self.stats_ui.scroll.clamp(top, bottom, content);
let left = ((w - 280.0) / 2.0).floor();
- let scroll = self.stats_ui.scroll;
+ let scroll = self.stats_ui.scroll.amount;
let row_y = |i: usize| top + 4.0 + i as f32 * row_h - scroll;
let visible = |y: f32| y >= top && y + row_h <= bottom;
match self.stats_ui.tab {
@@ -408,11 +417,11 @@ impl Game<'_> {
ui::scrollbar(
batch,
atlas,
- left + 280.0 + 6.0,
+ Self::stats_bar(w),
top,
bottom,
content,
- self.stats_ui.scroll,
+ self.stats_ui.scroll.amount,
);
self.draw_stats_footer(batch, w, h);
}
@@ -433,6 +442,10 @@ impl Game<'_> {
);
}
pub(super) fn stats_click(&mut self, size: [u32; 2]) {
+ // A press on the scrollbar holds it and clicks nothing else.
+ if self.stats_press_scrollbar(size) {
+ return;
+ }
let b = HudBatch::new(size);
let (w, h) = (b.width, b.height);
let (x0, tw) = tabs(w);
@@ -441,7 +454,7 @@ impl Game<'_> {
&& self.stats_ui.tab != i
{
self.stats_ui.tab = i;
- self.stats_ui.scroll = 0.0;
+ self.stats_ui.scroll.amount = 0.0;
self.audio
.play("minecraft:ui.button.click", None, 0.25, 1.0);
return;
@@ -461,7 +474,7 @@ impl Game<'_> {
}
if self.stats_ui.tab == 1 {
let left = ((w - 280.0) / 2.0).floor();
- let y = TAB_H + 4.0 - self.stats_ui.scroll;
+ let y = TAB_H + 4.0 - self.stats_ui.scroll.amount;
for c in 0..COLUMNS.len() {
let x = left + 75.0 + 40.0 * c as f32 - 18.0;
if ui::hovered(self.cursor, x, y + 1.0, 18.0, 18.0) {
@@ -479,6 +492,35 @@ impl Game<'_> {
}
pub(super) fn stats_scroll(&mut self, amount: f32) {
let (row_h, _) = self.stats_rows();
- self.stats_ui.scroll -= amount * row_h / 2.0;
+ let h = self.gui_size().1;
+ let content = self.stats_content();
+ self.stats_ui
+ .scroll
+ .wheel(amount, row_h / 2.0, TAB_H, h - FOOTER, content);
+ }
+ /// A press on the statistics list's scrollbar holds it
+ /// (`AbstractSelectionList.updateScrolling`).
+ pub(super) fn stats_press_scrollbar(&mut self, size: [u32; 2]) -> bool {
+ let b = HudBatch::new(size);
+ let (w, h) = (b.width, b.height);
+ let content = self.stats_content();
+ self.stats_ui
+ .scroll
+ .press(self.cursor, Self::stats_bar(w), TAB_H, h - FOOTER, content)
+ }
+ /// The pointer moved while the scrollbar is held.
+ pub(super) fn stats_drag(&mut self) {
+ if !self.stats_ui.scroll.held() {
+ return;
+ }
+ let h = self.gui_size().1;
+ let content = self.stats_content();
+ self.stats_ui
+ .scroll
+ .drag(self.cursor.1, TAB_H, h - FOOTER, content);
+ }
+ /// The button came up: the scrollbar lets go.
+ pub(super) fn stats_release(&mut self) {
+ self.stats_ui.scroll.release();
}
}
diff --git a/crates/horizon-client/src/game/world_loading.rs b/crates/horizon-client/src/game/world_loading.rs
index 998788960..0cea31fd0 100644
--- a/crates/horizon-client/src/game/world_loading.rs
+++ b/crates/horizon-client/src/game/world_loading.rs
@@ -246,6 +246,9 @@ pub(super) fn make_streaming(
let inner = Arc::new(VanillaTerrain::for_world(&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")
})?);
+ if dim == Dim::Overworld && structures {
+ inner.search_rings_behind();
+ }
locator = Some(inner.clone());
Streaming::new(
Arc::new(Persistent {
diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs
index cd40ea22e..d7d74e7a5 100644
--- a/crates/horizon-client/src/gfx.rs
+++ b/crates/horizon-client/src/gfx.rs
@@ -69,8 +69,14 @@ pub mod lightning;
mod lod;
mod occlusion;
mod pack_atlas;
+pub mod pack_draw;
+mod pack_entities;
+mod pack_hand;
mod pack_post;
+mod pack_sky;
+mod pack_timing;
pub mod pack_uniforms;
+mod pack_weather;
mod page_runs;
pub mod panorama;
mod resident;
@@ -86,6 +92,19 @@ const COMPLETE_COLUMN: u32 = (1 << (crate::chunk::HEIGHT / 16)) - 1;
/// its fixed-point UVs (`terrain.wgsl`'s `terrain_uv`).
/// The world pass again after occlusion culling's test: it loads both
/// targets (phase 2 draws over phase 1, reading the test's results).
+/// LevelRenderer: no sky inside lava or powder snow (sky fog ends within 2 blocks).
+fn sky_open(frame: &Frame) -> bool {
+ match frame.fog {
+ Some(f) => f.sky_end > 2.0,
+ None => !frame.underwater,
+ }
+}
+
+/// The Overworld-style sky (disc, sunset, stars, void) draws this frame.
+fn sky_visible(frame: &Frame) -> bool {
+ !frame.title && frame.skybox && sky_open(frame)
+}
+
fn world_pass_after_culling<'e>(
encoder: &'e mut wgpu::CommandEncoder,
view: &wgpu::TextureView,
@@ -232,6 +251,8 @@ pub struct Frame<'a> {
pub nether_lighting: bool,
/// Camera-relative entity geometry textured from the shared atlas.
pub entities: &'a [Vertex],
+ /// Runs of `entities` by entity, while a shader pack draws them (`pack_draw::EntitySpan`).
+ pub entity_spans: &'a [pack_draw::EntitySpan],
pub weather: &'a [crate::weather::Vertex],
pub weather_time: crate::weather::Time,
/// Horizon Extras' dynamic lights, nearest first (`dynamic_light::select`); empty when off.
@@ -473,8 +494,16 @@ pub struct Renderer {
world_post: world_post::Post,
/// The shader pack's post-processing, and the dimension (legacy id) it was loaded for.
pack_post: Option<(i32, Option)>,
+ /// The entity vertices Horizon draws itself while a shader pack draws the others.
+ pack_entity_rest: Vec,
+ /// Set 1 of the cloud pipeline, for a shader pack's `gbuffers_clouds`.
+ cloud_bindings: wgpu::BindGroupLayout,
+ /// Rain and snow handed to a shader pack's `gbuffers_weather`, once a pack draws them.
+ pack_weather: Option,
/// The block sprites as one atlas, while a shader pack is loaded.
pack_atlas: Option,
+ /// The pack's terrain program reads flat draws' vertices (`pack_terrain_inputs`).
+ pack_terrain_flat: bool,
pub hud: HudRenderer,
/// Vanilla's menu background blur, made on first use.
menu_blur: Option,
@@ -842,12 +871,17 @@ async fn open_device(
continue;
}
// Mappable primary buffers: section pages that no longer fit in
- // video memory go to system memory.
+ // video memory go to system memory. Shader packs' 16-bit normalized buffers (render
+ // targets with the adapter's own format features) and R11F_G11F_B10F: a float
+ // stand-in neither clamps nor drops NaN.
let features = timing::GpuTimer::features(&adapter)
| cull::Cull::features(&adapter)
| (adapter.features()
& (wgpu::Features::INDIRECT_FIRST_INSTANCE
- | wgpu::Features::MAPPABLE_PRIMARY_BUFFERS));
+ | wgpu::Features::MAPPABLE_PRIMARY_BUFFERS
+ | wgpu::Features::TEXTURE_FORMAT_16BIT_NORM
+ | wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES
+ | wgpu::Features::RG11B10UFLOAT_RENDERABLE));
let driver = format!("{} {}", info.driver, info.driver_info);
startup_step(format_args!(
"requesting the device (driver {}) with features [{features}] and the adapter's \
@@ -1484,7 +1518,10 @@ impl Renderer {
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8Unorm,
- usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
+ // COPY_SRC: a shader pack's weather copies the rain and snow sprites out.
+ usage: wgpu::TextureUsages::TEXTURE_BINDING
+ | wgpu::TextureUsages::COPY_DST
+ | wgpu::TextureUsages::COPY_SRC,
view_formats: &[],
},
wgpu::util::TextureDataOrder::LayerMajor,
@@ -2149,13 +2186,16 @@ impl Renderer {
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
+ let flat_wanted = flat::wanted(&adapter, &device);
+ // Readable as storage where flat draws run: a shader pack's terrain program reads them.
let arena = arena::Arena::new(
&device,
std::mem::size_of::() as u64,
1 << 20,
+ flat_wanted,
);
// Sections as one index list per frame where multi-draw is emulated.
- let flat = flat::wanted(&adapter, &device).then(|| {
+ let flat = flat_wanted.then(|| {
let buffers = [Some(wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
@@ -2362,7 +2402,11 @@ impl Renderer {
occlusion: occlusion::Occlusion::new(crate::chunk::MIN_Y, crate::chunk::HEIGHT / 16),
world_post,
pack_post: None,
+ pack_entity_rest: Vec::new(),
+ cloud_bindings,
+ pack_weather: None,
pack_atlas: None,
+ pack_terrain_flat: false,
hud,
menu_blur: None,
info,
@@ -2793,24 +2837,84 @@ impl Renderer {
self.world_post.scale = scale.clamp(0.25, 2.0);
self.world_post.antialias = antialias;
}
- /// Runs `post` over the world from now on (`None`: no pack), for the dimension `dimension`.
+ /// Runs `post` over the world from now on (`None`: no pack), for the dimension `dimension`;
+ /// `block_ids` gives each block sprite layer's `mc_Entity` (id, fluid) pair.
/// Returns the programs that run and the notes of loading them.
pub fn set_shader_pack(
&mut self,
dimension: i32,
pack: Option,
+ block_ids: &[i32],
) -> Option<(Vec, Vec)> {
+ if pack.is_none() {
+ self.pack_atlas = None;
+ }
+ let instance = wgpu::vertex_attr_array![5=>Float32x4];
+ let vertex = wgpu::VertexBufferLayout {
+ array_stride: std::mem::size_of::() as u64,
+ step_mode: wgpu::VertexStepMode::Vertex,
+ attributes: &TERRAIN_ATTRIBUTES,
+ };
+ let instances = wgpu::VertexBufferLayout {
+ array_stride: 16,
+ step_mode: wgpu::VertexStepMode::Instance,
+ attributes: &instance,
+ };
+ let buffers = [Some(vertex), Some(instances)];
+ let flat = self.pack_terrain_flat;
let post = pack.map(|post| {
+ let world = pack_post::World {
+ atlas: self.pack_atlas.as_ref().map(|a| (a.view.clone(), a.size())),
+ lightmap: self.lightmap_view.clone(),
+ terrain: self.pack_atlas.as_ref().map(|_| pack_post::Geometry {
+ buffers: if flat { &buffers[..1] } else { &buffers },
+ table: self.flat.as_ref().filter(|_| flat).map(|f| &f.draw_layout),
+ depth_write: true,
+ atlas: None,
+ ids: None,
+ cull: true,
+ }),
+ block_ids,
+ celestial: self
+ .hud
+ .atlas_texture
+ .as_ref()
+ .map(|t| t.create_view(&Default::default())),
+ clouds: Some(&self.cloud_bindings),
+ weather: true,
+ hand: true,
+ };
pack_post::PackPost::new(
&self.device,
&self.queue,
post,
self.config.format.remove_srgb_suffix(),
+ world,
)
});
- if post.is_none() {
- self.pack_atlas = None;
- } else if self.pack_atlas.is_none() {
+ let report = post.as_ref().map(|p| {
+ let programs = p.programs().into_iter().map(str::to_owned).collect();
+ (programs, p.notes.clone())
+ });
+ self.pack_post = Some((dimension, post));
+ report
+ }
+ /// What a shader pack's gbuffers programs read from Horizon's geometry: the sky's, and
+ /// terrain's in the pack's block atlas (made here when missing; none when it does not fit).
+ pub fn pack_inputs(&mut self) -> horizon_shaders::GameInputs {
+ horizon_shaders::GameInputs {
+ terrain: self.pack_terrain_inputs(),
+ sky: Some(pack_sky::inputs()),
+ celestial: Some(pack_sky::celestial_inputs()),
+ entities: Some(pack_entities::inputs()),
+ clouds: Some(pack_entities::cloud_inputs()),
+ weather: Some(pack_weather::inputs()),
+ hand: Some(pack_hand::inputs()),
+ }
+ }
+
+ fn pack_terrain_inputs(&mut self) -> Option {
+ if self.pack_atlas.is_none() {
let mut encoder = self.device.create_command_encoder(&Default::default());
let layers = self
.sprite_textures
@@ -2827,12 +2931,13 @@ impl Renderer {
);
self.queue.submit([encoder.finish()]);
}
- let report = post.as_ref().map(|p| {
- let programs = p.programs().into_iter().map(str::to_owned).collect();
- (programs, p.notes.clone())
- });
- self.pack_post = Some((dimension, post));
- report
+ // Flat draws whenever the device has them (`render` keeps them on with a pack).
+ self.pack_terrain_flat = self.flat.is_some() && self.flat_on;
+ Some(
+ self.pack_atlas
+ .as_ref()?
+ .terrain_inputs(self.pack_terrain_flat),
+ )
}
/// The dimension the shader pack was last set for.
pub fn shader_pack_dimension(&self) -> Option {
@@ -3243,11 +3348,23 @@ impl Renderer {
// Occlusion culling leaves the sections its last result did not see
// to phase 2, after the others, with instance counts of 0. Whether
// the frame has draws enough goes by the last one's.
+ // A shader pack drawing terrain draws all of it: occlusion culling reads the depth of
+ // Horizon's own terrain draw.
+ let pack_terrain = !frame.title
+ && self
+ .pack_post
+ .as_ref()
+ .and_then(|p| p.1.as_ref())
+ .is_some_and(|p| p.has_terrain());
let mut cull = self
.cull
.as_mut()
.filter(|c| {
- self.cull_on && !frame.title && self.multi_draw && visible.len() >= c.min_sections
+ self.cull_on
+ && !frame.title
+ && !pack_terrain
+ && self.multi_draw
+ && visible.len() >= c.min_sections
})
.and_then(|cull| cull.begin(&self.device).then_some(cull));
self.phase2_draws.clear();
@@ -3472,6 +3589,22 @@ impl Renderer {
self.uploads
.buffer(&mut encoder, &self.outline, 0, bytemuck::cast_slice(&lines));
}
+ // While a shader pack draws entities, the runs of `entity_spans` its programs draw are
+ // left out of Horizon's own entity draw.
+ let mut entities = frame.entities;
+ if !frame.title
+ && let Some((_, Some(post))) = &mut self.pack_post
+ && post.has_entities()
+ {
+ post.prepare_entities(
+ &self.device,
+ &self.queue,
+ frame.entities,
+ frame.entity_spans,
+ &mut self.pack_entity_rest,
+ );
+ entities = &self.pack_entity_rest;
+ }
let [
entity_quads,
crumbling_quads,
@@ -3479,12 +3612,9 @@ impl Renderer {
water_mask_quads,
shadow_quads,
translucent_particle_quads,
- ] = self.entity_batch.update(
- frame.entities,
- frame.additive,
- frame.hand,
- frame.see_through,
- );
+ ] = self
+ .entity_batch
+ .update(entities, frame.additive, frame.hand, frame.see_through);
let weather_quads = frame.weather.len() as u32 / 4;
let world_quads = entity_quads
+ crumbling_quads
@@ -3653,7 +3783,7 @@ impl Renderer {
Some(flat)
if self.flat_on
&& !self.draws.is_empty()
- && self.drawn_sections >= flat.min_sections =>
+ && (self.drawn_sections >= flat.min_sections || pack_terrain) =>
{
let sections = &self.sections;
let lods = &self.lods;
@@ -3713,6 +3843,44 @@ impl Renderer {
}
// The world's targets: the screen, or the scaled target of `post`.
let pack_post = !frame.title && self.pack_post.as_ref().is_some_and(|p| p.1.is_some());
+ // The pack's program reads the vertices this frame's draws bind.
+ let pack_draws_terrain = pack_terrain && flat_frame == self.pack_terrain_flat;
+ let pack_draws_sky = pack_post
+ && self
+ .pack_post
+ .as_ref()
+ .and_then(|p| p.1.as_ref())
+ .is_some_and(|p| p.has_sky());
+ let pack_draws_celestial = pack_post
+ && self
+ .pack_post
+ .as_ref()
+ .and_then(|p| p.1.as_ref())
+ .is_some_and(|p| p.has_celestial());
+ let pack_draws_water = pack_draws_terrain
+ && self
+ .pack_post
+ .as_ref()
+ .and_then(|p| p.1.as_ref())
+ .is_some_and(|p| p.has_water());
+ let pack_draws_clouds = pack_post
+ && self
+ .pack_post
+ .as_ref()
+ .and_then(|p| p.1.as_ref())
+ .is_some_and(|p| p.has_clouds());
+ let pack_draws_hand = pack_post
+ && self
+ .pack_post
+ .as_ref()
+ .and_then(|p| p.1.as_ref())
+ .is_some_and(|p| p.has_hand());
+ let pack_draws_weather = pack_post
+ && self
+ .pack_post
+ .as_ref()
+ .and_then(|p| p.1.as_ref())
+ .is_some_and(|p| p.has_weather());
let scaled = self.world_post.prepare(
&self.device,
[self.config.width, self.config.height],
@@ -3766,13 +3934,9 @@ impl Renderer {
..Default::default()
});
pass.set_bind_group(0, &self.group, &[]);
- // LevelRenderer: no sky inside lava or powder snow (sky fog ends within 2 blocks).
- let sky_visible = match frame.fog {
- Some(f) => f.sky_end > 2.0,
- None => !frame.underwater,
- };
+ let sky_visible = sky_open(frame);
if frame.title || (sky_visible && (frame.skybox || frame.end_sky)) {
- if !frame.title && frame.skybox {
+ if !frame.title && frame.skybox && !pack_draws_sky {
pass.set_pipeline(&self.sky);
pass.draw(0..3, 0..1);
}
@@ -3786,22 +3950,29 @@ impl Renderer {
if frame.end_sky {
self.end_sky.draw(&mut pass);
}
- if celestial_vertices > 0 {
+ if celestial_vertices > 0 && !pack_draws_celestial {
pass.set_vertex_buffer(0, self.celestial_buffer.slice(..));
pass.draw_indexed(0..(celestial_vertices as u32 / 4) * 6, 0, 0..1);
}
pass.set_bind_group(0, &self.group, &[]);
}
- if !frame.title && frame.stars.is_some() {
+ if !frame.title && frame.stars.is_some() && !pack_draws_sky {
self.stars.draw(&mut pass);
pass.set_bind_group(0, &self.group, &[]);
}
- if !frame.title && frame.dark_disc {
+ if !frame.title && frame.dark_disc && !pack_draws_sky {
pass.set_pipeline(&self.dark_disc);
pass.draw(0..3, 0..1);
}
}
- match self.flat.as_ref().filter(|_| flat_frame) {
+ match self
+ .flat
+ .as_ref()
+ .filter(|_| flat_frame && !pack_draws_terrain)
+ {
+ _ if pack_draws_terrain => {
+ pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
+ }
Some(flat) => {
pass.set_pipeline(&flat.solid);
let vertices = |p| self.arena.buffer(p);
@@ -3875,7 +4046,9 @@ impl Renderer {
pass.draw_indexed(0..entity_quads * 6, 0, 0..1);
pass.set_bind_group(0, &self.group, &[]);
}
- self.weather.draw(&mut pass, &self.entity_group);
+ if !pack_draws_weather {
+ self.weather.draw(&mut pass, &self.entity_group);
+ }
pass.set_bind_group(0, &self.group, &[]);
if crumbling_quads > 0 {
pass.set_pipeline(&self.crumbling_pipeline);
@@ -3937,6 +4110,8 @@ impl Renderer {
pass.draw(0..4, debug_depth..debug_depth + debug_top);
}
match self.flat.as_ref().filter(|_| flat_frame) {
+ // The pack draws translucent terrain after its deferred programs.
+ _ if pack_draws_water => {}
Some(flat) => {
pass.set_pipeline(&flat.translucent);
flat.draw(&mut pass, &flat.calls[flat.solid_calls..], |p| {
@@ -4004,7 +4179,7 @@ impl Renderer {
0..1,
);
}
- if cloud_quads > 0 {
+ if cloud_quads > 0 && !pack_draws_clouds {
pass.set_pipeline(&self.cloud_pipeline);
pass.set_bind_group(0, &self.entity_group, &[]);
pass.set_bind_group(1, &self.cloud_group, &[]);
@@ -4025,9 +4200,68 @@ impl Renderer {
}
if let Some((color, depth)) = &scaled
&& pack_post
- && let Some((_, Some(post))) = &mut self.pack_post
+ && let Some((dimension, Some(mut post))) = self.pack_post.take()
{
let size = self.world_post.size().unwrap_or([1, 1]);
+ let weather = match &self.hud.atlas_texture {
+ Some(atlas) if pack_draws_weather => self
+ .pack_weather
+ .get_or_insert_with(|| pack_weather::PackWeather::new(&self.device))
+ .prepare(
+ &self.device,
+ &mut encoder,
+ &mut self.uploads,
+ atlas,
+ frame.weather,
+ frame.weather_time,
+ &self.indices,
+ ),
+ _ => None,
+ };
+ post.set_weather(weather);
+ let flat = self.flat.as_ref().filter(|_| flat_frame);
+ let pages: Vec<(u32, wgpu::Buffer)> = match flat {
+ Some(_) if pack_draws_terrain => self
+ .arena
+ .page_buffers()
+ .map(|(p, b)| (p, b.clone()))
+ .collect(),
+ _ => Vec::new(),
+ };
+ let mut terrain =
+ |rp: &mut wgpu::RenderPass<'_>,
+ translucent: bool,
+ page: &dyn Fn(&mut wgpu::RenderPass<'_>, u32)| {
+ if !pack_draws_terrain {
+ return;
+ }
+ match flat {
+ Some(flat) => {
+ let calls = if translucent {
+ &flat.calls[flat.solid_calls..]
+ } else {
+ &flat.calls[..flat.solid_calls]
+ };
+ flat.draw_with(rp, calls, |p| self.arena.buffer(p), page);
+ }
+ None => {
+ rp.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
+ rp.set_vertex_buffer(1, self.instances.slice(..));
+ let (first, count) = if translucent {
+ (solid_draws, translucent_draws)
+ } else {
+ (0, solid_draws)
+ };
+ self.draw_sections(rp, &self.indirect, 0, first, count);
+ }
+ }
+ };
+ post.set_clouds(pack_draws_clouds.then(|| pack_post::Clouds {
+ vertices: self.cloud_buffer.clone(),
+ group: self.cloud_group.clone(),
+ indices: self.indices.clone(),
+ quads: cloud_quads,
+ }));
post.run(
&self.device,
&self.queue,
@@ -4039,11 +4273,14 @@ impl Renderer {
&frame.pack,
size,
[self.config.width, self.config.height],
+ &pages,
+ &mut terrain,
);
+ self.pack_post = Some((dimension, Some(post)));
} else if scaled.is_some() {
self.world_post.finish(&mut encoder, &view_target);
}
- if hand_quads > 0 {
+ if hand_quads > 0 && !pack_draws_hand {
let attachments = [Some(wgpu::RenderPassColorAttachment {
view: &view_target,
resolve_target: None,
diff --git a/crates/horizon-client/src/gfx/arena.rs b/crates/horizon-client/src/gfx/arena.rs
index 8da835aa2..34b4d332d 100644
--- a/crates/horizon-client/src/gfx/arena.rs
+++ b/crates/horizon-client/src/gfx/arena.rs
@@ -65,8 +65,15 @@ pub struct Arena {
}
impl Arena {
- pub fn new(device: &wgpu::Device, stride: u64, capacity: u64) -> Self {
- Self::with_usage(device, stride, capacity, wgpu::BufferUsages::VERTEX)
+ /// Vertices; `storage` also lets shaders read them as a storage buffer (a shader pack's
+ /// terrain program reads the other corners of a vertex's quad for its normal).
+ pub fn new(device: &wgpu::Device, stride: u64, capacity: u64, storage: bool) -> Self {
+ let usage = if storage {
+ wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::STORAGE
+ } else {
+ wgpu::BufferUsages::VERTEX
+ };
+ Self::with_usage(device, stride, capacity, usage)
}
/// Sorted translucent indices; `storage` when compute passes read them.
pub fn indices(device: &wgpu::Device, capacity: u64, storage: bool) -> Self {
@@ -148,6 +155,11 @@ impl Arena {
pub fn has_page(&self, page: u32) -> bool {
self.pages.get(page as usize).is_some_and(Option::is_some)
}
+ /// Each live page's buffer, by page.
+ pub fn page_buffers(&self) -> impl Iterator- {
+ let pages = self.pages.iter().enumerate();
+ pages.filter_map(|(i, p)| Some((i as u32, &p.as_ref()?.buffer)))
+ }
pub fn buffer(&self, page: u32) -> &wgpu::Buffer {
&self.pages[page as usize]
.as_ref()
@@ -352,7 +364,7 @@ mod tests {
eprintln!("no GPU adapter: skipped");
return;
};
- let mut arena = Arena::new(&device, 4, GRAIN);
+ let mut arena = Arena::new(&device, 4, GRAIN, false);
arena.page_limit = 2 * GRAIN;
assert_eq!(arena.bytes(), 0);
let (first, a) = alloc(&mut arena, &device, &queue, GRAIN).unwrap();
@@ -395,7 +407,7 @@ mod tests {
return;
};
for mut arena in [
- Arena::new(&device, 28, GRAIN),
+ Arena::new(&device, 28, GRAIN, false),
Arena::indices(&device, GRAIN, false),
] {
alloc(&mut arena, &device, &queue, GRAIN).unwrap();
diff --git a/crates/horizon-client/src/gfx/flat.rs b/crates/horizon-client/src/gfx/flat.rs
index ea6df0d5b..6b71a16f7 100644
--- a/crates/horizon-client/src/gfx/flat.rs
+++ b/crates/horizon-client/src/gfx/flat.rs
@@ -73,7 +73,8 @@ pub struct Flat {
phase2_call_data: Vec<[u32; 5]>,
/// The copy of phase 2's draws, and its ranges.
phase2_group: Option<(wgpu::BindGroup, u32)>,
- draw_layout: wgpu::BindGroupLayout,
+ /// The layout of a page's bind group 1 (shader packs' terrain pipelines share it).
+ pub(super) draw_layout: wgpu::BindGroupLayout,
blocks_per_page: u64,
pages: Vec
>,
free_ids: Vec,
@@ -650,10 +651,22 @@ impl Flat {
pass: &mut wgpu::RenderPass<'_>,
calls: &[Call],
vertices: impl Fn(u32) -> &'a wgpu::Buffer,
+ ) {
+ self.draw_with(pass, calls, vertices, |_, _| {});
+ }
+ /// `draw`, calling `page` before each run of calls from one arena page (a shader pack binds
+ /// that page's vertices for reading).
+ pub fn draw_with<'a>(
+ &self,
+ pass: &mut wgpu::RenderPass<'_>,
+ calls: &[Call],
+ vertices: impl Fn(u32) -> &'a wgpu::Buffer,
+ page: impl Fn(&mut wgpu::RenderPass<'_>, u32),
) {
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
for call in calls {
self.bind_page(pass, call.page, &vertices);
+ page(pass, call.page);
pass.draw_indexed(call.first..call.first + call.count, 0, 0..1);
}
}
diff --git a/crates/horizon-client/src/gfx/pack_atlas.rs b/crates/horizon-client/src/gfx/pack_atlas.rs
index c90fa6ac1..42a2abee1 100644
--- a/crates/horizon-client/src/gfx/pack_atlas.rs
+++ b/crates/horizon-client/src/gfx/pack_atlas.rs
@@ -3,7 +3,6 @@
//! layer is copied into a grid, row by row from the top left, and an animated layer again
//! whenever its frame changes.
-#[expect(dead_code, reason = "the gbuffers pass binds it (next slice)")]
pub(super) struct PackAtlas {
texture: wgpu::Texture,
pub view: wgpu::TextureView,
@@ -64,10 +63,6 @@ impl PackAtlas {
}
/// The atlas's size in pixels (`atlasSize`).
- #[cfg_attr(
- not(test),
- expect(dead_code, reason = "the gbuffers pass reads it (next slice)")
- )]
pub fn size(&self) -> [u32; 2] {
[self.texture.width(), self.texture.height()]
}
@@ -118,6 +113,112 @@ impl PackAtlas {
}
}
+/// GLSL: the face normal and tangent of a flat-drawn vertex, from its quad's first three corners
+/// (quads are four consecutive vertices of the page; position words 0-2, UV word 3).
+const FACE: &str = " uint hz_q = (uint(gl_VertexIndex) & ~3u) * 6u;\n \
+ vec3 hz_p0 = uintBitsToFloat(uvec3(hz_vertices[hz_q], hz_vertices[hz_q + 1u], hz_vertices[hz_q + 2u]));\n \
+ vec3 hz_p1 = uintBitsToFloat(uvec3(hz_vertices[hz_q + 6u], hz_vertices[hz_q + 7u], hz_vertices[hz_q + 8u]));\n \
+ vec3 hz_p2 = uintBitsToFloat(uvec3(hz_vertices[hz_q + 12u], hz_vertices[hz_q + 13u], hz_vertices[hz_q + 14u]));\n \
+ uvec3 hz_w = uvec3(hz_vertices[hz_q + 3u], hz_vertices[hz_q + 9u], hz_vertices[hz_q + 15u]);\n \
+ vec2 hz_t0 = vec2(hz_w.x & 65535u, hz_w.x >> 16u);\n \
+ vec2 hz_d1 = vec2(hz_w.y & 65535u, hz_w.y >> 16u) - hz_t0;\n \
+ vec2 hz_d2 = vec2(hz_w.z & 65535u, hz_w.z >> 16u) - hz_t0;\n \
+ vec3 hz_e1 = hz_p1 - hz_p0;\n \
+ vec3 hz_e2 = hz_p2 - hz_p0;\n \
+ vec3 hz_n = cross(hz_e1, hz_e2);\n \
+ if (dot(hz_n, hz_n) > 1e-12) {\n \
+ hz_Normal = normalize(hz_n);\n \
+ float hz_r = hz_d1.x * hz_d2.y - hz_d2.x * hz_d1.y;\n \
+ vec3 hz_t = (hz_e1 * hz_d2.y - hz_e2 * hz_d1.y) * sign(hz_r);\n \
+ vec3 hz_b = (hz_e2 * hz_d1.x - hz_e1 * hz_d2.x) * sign(hz_r);\n \
+ if (dot(hz_t, hz_t) > 1e-12) {\n \
+ hz_t = normalize(hz_t - hz_Normal * dot(hz_Normal, hz_t));\n \
+ hz_tangent = vec4(hz_t, dot(cross(hz_t, hz_Normal), hz_b) < 0.0 ? -1.0 : 1.0);\n \
+ }\n \
+ }\n";
+
+impl PackAtlas {
+ /// The vertex inputs of a gbuffers program drawing terrain (`TerrainVertex`, terrain.wgsl's
+ /// `vs_terrain` or, for `flat` draws, `vs_terrain_flat`), in atlas coordinates.
+ pub fn terrain_inputs(&self, flat: bool) -> horizon_shaders::GbuffersInputs {
+ let mut declarations = String::from(
+ "layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in uvec2 hz_v_uv;\n\
+ layout(location = 2) in uint hz_v_layer;\nlayout(location = 3) in vec4 hz_v_color;\n\
+ layout(location = 4) in uvec2 hz_v_light;\n",
+ );
+ let origin = if flat {
+ declarations.push_str(
+ "layout(set = 1, binding = 0) readonly buffer HzBlockSlots { uint hz_block_slots[]; };\n\
+ layout(set = 1, binding = 1) readonly buffer HzSlotOrigins { vec4 hz_slot_origins[]; };\n",
+ );
+ // `mc_Entity` of each sprite layer: id, then 1 for fluids or -1 (the game's table),
+ // and the arena page's vertices (`TerrainVertex`, six words each).
+ declarations.push_str(
+ "layout(set = 3, binding = 0) readonly buffer HzBlockIds { int hz_block_ids[]; };\n\
+ layout(set = 3, binding = 1) readonly buffer HzVertices { uint hz_vertices[]; };\n",
+ );
+ "hz_slot_origins[hz_block_slots[uint(gl_VertexIndex) >> 8u]]"
+ } else {
+ declarations.push_str("layout(location = 5) in vec4 hz_v_origin;\n");
+ "hz_v_origin"
+ };
+ let [width, height] = self.size();
+ let scale = [
+ self.sprite as f32 / width as f32,
+ self.sprite as f32 / height as f32,
+ ];
+ let columns = self.columns;
+ // UVs past a sprite (models up to 2, distant tiles) cannot repeat in an atlas: clamped.
+ let body = format!(
+ " vec4 hz_origin = {origin};\n \
+ uint hz_layer = hz_v_layer & 32767u;\n \
+ hz_cell = vec2(float(hz_layer % {columns}u), float(hz_layer / {columns}u));\n \
+ hz_scale = vec2({:?}, {:?});\n \
+ hz_Vertex = vec4(hz_v_pos + hz_origin.xyz, 1.0);\n \
+ hz_MultiTexCoord0 = vec4((hz_cell + clamp(vec2(hz_v_uv) * hz_origin.w, 0.0, 1.0)) * hz_scale, 0.0, 1.0);\n \
+ hz_MultiTexCoord1 = vec4(float(hz_v_light.y), float(hz_v_light.x), 0.0, 1.0);\n \
+ hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \
+ hz_Color = hz_v_color;\n \
+ hz_Normal = vec3(0.0, 1.0, 0.0);\n \
+ hz_tangent = vec4(1.0, 0.0, 0.0, 1.0);\n",
+ scale[0], scale[1],
+ );
+ let mut body = body;
+ if flat {
+ body.push_str(FACE);
+ }
+ declarations.push_str("vec2 hz_cell; vec2 hz_scale; vec4 hz_tangent;\n");
+ let entity = if flat {
+ "vec4(float(hz_block_ids[2u * hz_layer]), float(hz_block_ids[2u * hz_layer + 1u]), 0.0, 0.0)"
+ } else {
+ "vec4(-1.0, -1.0, 0.0, 0.0)"
+ };
+ let attributes = [
+ ("mc_Entity", entity),
+ (
+ "mc_midTexCoord",
+ "vec4((hz_cell + 0.5) * hz_scale, 0.0, 1.0)",
+ ),
+ ("at_tangent", "hz_tangent"),
+ ("vaPosition", "hz_Vertex.xyz"),
+ ("vaUV0", "hz_MultiTexCoord0.xy"),
+ ("vaUV2", "hz_MultiTexCoord1.xy"),
+ ("vaColor", "hz_Color"),
+ ("vaNormal", "hz_Normal"),
+ ];
+ horizon_shaders::GbuffersInputs {
+ declarations,
+ body,
+ attributes: attributes
+ .iter()
+ .map(|&(n, v)| (n.to_owned(), v.to_owned()))
+ .collect(),
+ after: String::new(),
+ alpha_test: None,
+ }
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/crates/horizon-client/src/gfx/pack_draw.rs b/crates/horizon-client/src/gfx/pack_draw.rs
new file mode 100644
index 000000000..d19c3b4db
--- /dev/null
+++ b/crates/horizon-client/src/gfx/pack_draw.rs
@@ -0,0 +1,83 @@
+//! The uniforms a shader pack's gbuffers programs read per draw (`HzDraw` at set 2, the
+//! translator's `DRAW_UNIFORMS`): one slot per render stage of a uniform buffer, picked with a
+//! dynamic offset at each draw.
+
+use horizon_shaders::{RENDER_STAGES, render_stage};
+
+/// Where a run of `Frame::entities` begins, and what it is (slice 7): the run lasts until the
+/// next span. Recorded only while a shader pack draws entities, so Horizon's own frames carry
+/// none; Horizon itself still draws the whole list at once.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub struct EntitySpan {
+ /// The first vertex.
+ pub start: u32,
+ /// The render stage (5.4: `ENTITIES`, `BLOCK_ENTITIES`, `PARTICLES`, `DESTROY`), which picks
+ /// the program.
+ pub stage: u32,
+ /// `entityId` for entities, `blockEntityId` for block entities (5.3), else 0.
+ pub id: i32,
+ /// An entity part that blends (a name tag, a shadow): `gbuffers_entities_translucent`.
+ pub translucent: bool,
+}
+
+/// `HzDraw`'s size: `renderStage`, `entityId`, `blockEntityId`, a pad, `entityColor`.
+const SIZE: u32 = 32;
+
+pub(super) struct DrawUniforms {
+ pub layout: wgpu::BindGroupLayout,
+ group: wgpu::BindGroup,
+ stride: u32,
+}
+
+impl DrawUniforms {
+ pub fn new(device: &wgpu::Device) -> Self {
+ let stride = SIZE.max(device.limits().min_uniform_buffer_offset_alignment);
+ let mut contents = vec![0u8; stride as usize * RENDER_STAGES.len()];
+ for (i, slot) in contents.chunks_mut(stride as usize).enumerate() {
+ slot[..4].copy_from_slice(&(i as i32).to_le_bytes());
+ }
+ let buffer = wgpu::util::DeviceExt::create_buffer_init(
+ device,
+ &wgpu::util::BufferInitDescriptor {
+ label: Some("Shader pack draws"),
+ contents: &contents,
+ usage: wgpu::BufferUsages::UNIFORM,
+ },
+ );
+ let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some("Shader pack draws"),
+ entries: &[wgpu::BindGroupLayoutEntry {
+ binding: 0,
+ visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
+ ty: wgpu::BindingType::Buffer {
+ ty: wgpu::BufferBindingType::Uniform,
+ has_dynamic_offset: true,
+ min_binding_size: wgpu::BufferSize::new(u64::from(SIZE)),
+ },
+ count: None,
+ }],
+ });
+ let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("Shader pack draws"),
+ layout: &layout,
+ entries: &[wgpu::BindGroupEntry {
+ binding: 0,
+ resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
+ buffer: &buffer,
+ offset: 0,
+ size: wgpu::BufferSize::new(u64::from(SIZE)),
+ }),
+ }],
+ });
+ Self {
+ layout,
+ group,
+ stride,
+ }
+ }
+
+ /// Draws that follow read `renderStage` = `MC_RENDER_STAGE_`, other values zero.
+ pub fn bind(&self, pass: &mut wgpu::RenderPass<'_>, stage: &str) {
+ pass.set_bind_group(2, &self.group, &[self.stride * render_stage(stage)]);
+ }
+}
diff --git a/crates/horizon-client/src/gfx/pack_entities.rs b/crates/horizon-client/src/gfx/pack_entities.rs
new file mode 100644
index 000000000..ffc55149f
--- /dev/null
+++ b/crates/horizon-client/src/gfx/pack_entities.rs
@@ -0,0 +1,462 @@
+//! Entities, block entities, translucent entity parts and particles drawn by a shader pack's
+//! `gbuffers_entities`, `gbuffers_block`, `gbuffers_entities_translucent` and
+//! `gbuffers_particles` (spec 2.3, 5.3, 5.4): the runs of `Frame::entities` the game marked (`EntitySpan`), each its
+//! own draw with its `entityId` or `blockEntityId` and `entityColor` in a per-draw slot of
+//! `HzDraw` (set 2). Horizon draws whatever the pack does not.
+
+use std::ops::Range;
+
+use horizon_shaders::render_stage;
+
+use super::Vertex;
+use super::pack_draw::EntitySpan;
+
+/// `HzDraw`'s size (pack_draw.rs).
+const SLOT: u64 = 32;
+
+const ATTRIBUTES: [wgpu::VertexAttribute; 6] = wgpu::vertex_attr_array![
+ 0 => Float32x3, 1 => Float32x2, 2 => Uint32, 3 => Unorm8x4, 4 => Uint8x4, 5 => Snorm8x4
+];
+
+/// The entity vertex: Horizon's own (`Vertex`).
+pub fn buffers() -> [Option>; 1] {
+ [Some(wgpu::VertexBufferLayout {
+ array_stride: std::mem::size_of::() as u64,
+ step_mode: wgpu::VertexStepMode::Vertex,
+ attributes: &ATTRIBUTES,
+ })]
+}
+
+/// What `gbuffers_entities` and `gbuffers_block` read from an entity vertex: camera-relative
+/// position, the shared atlas's UV, colour, light (block, sky in 16ths) and the world-oriented
+/// normal. The quad's corners, read from the same vertices at set 1, give the face normal where
+/// the vertex has none (block entities) and `at_tangent` along the texture's u, as for terrain.
+pub fn inputs() -> horizon_shaders::GbuffersInputs {
+ let declarations = "layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in vec2 hz_v_uv;\n\
+ layout(location = 2) in uint hz_v_overlay;\nlayout(location = 3) in vec4 hz_v_color;\n\
+ layout(location = 4) in uvec4 hz_v_light;\nlayout(location = 5) in vec4 hz_v_normal;\n\
+ layout(set = 1, binding = 0) readonly buffer HzEntityVertices { uint hz_entity_vertices[]; };\n\
+ vec4 hz_tangent; vec2 hz_mid;\n"
+ .to_owned();
+ let words = std::mem::size_of::() / 4;
+ let corner = |k: usize, field: &str| {
+ let at = |o: usize| format!("hz_entity_vertices[hz_q + {}u]", k * words + o);
+ match field {
+ "pos" => format!("uintBitsToFloat(uvec3({}, {}, {}))", at(0), at(1), at(2)),
+ _ => format!("uintBitsToFloat(uvec2({}, {}))", at(3), at(4)),
+ }
+ };
+ let body = format!(
+ " uint hz_q = (uint(gl_VertexIndex) & ~3u) * {words}u;\n \
+ vec3 hz_e1 = {p1} - {p0};\n \
+ vec3 hz_e2 = {p2} - {p0};\n \
+ vec2 hz_t0 = {t0};\n \
+ vec2 hz_d1 = {t1} - hz_t0;\n \
+ vec2 hz_d2 = {t2} - hz_t0;\n \
+ hz_mid = (hz_t0 + {t2}) * 0.5;\n \
+ vec3 hz_n = cross(hz_e1, hz_e2);\n \
+ hz_Normal = dot(hz_v_normal.xyz, hz_v_normal.xyz) > 0.01 ? normalize(hz_v_normal.xyz)\n \
+ : dot(hz_n, hz_n) > 1e-12 ? normalize(hz_n) : vec3(0.0, 1.0, 0.0);\n \
+ hz_tangent = vec4(1.0, 0.0, 0.0, 1.0);\n \
+ float hz_r = hz_d1.x * hz_d2.y - hz_d2.x * hz_d1.y;\n \
+ vec3 hz_t = (hz_e1 * hz_d2.y - hz_e2 * hz_d1.y) * sign(hz_r);\n \
+ vec3 hz_b = (hz_e2 * hz_d1.x - hz_e1 * hz_d2.x) * sign(hz_r);\n \
+ hz_t -= hz_Normal * dot(hz_Normal, hz_t);\n \
+ if (dot(hz_t, hz_t) > 1e-12) {{\n \
+ hz_t = normalize(hz_t);\n \
+ hz_tangent = vec4(hz_t, dot(cross(hz_t, hz_Normal), hz_b) < 0.0 ? -1.0 : 1.0);\n \
+ }}\n \
+ hz_Vertex = vec4(hz_v_pos, 1.0);\n \
+ hz_MultiTexCoord0 = vec4(hz_v_uv, 0.0, 1.0);\n \
+ hz_MultiTexCoord1 = vec4(float(hz_v_light.y), float(hz_v_light.x), 0.0, 1.0);\n \
+ hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \
+ hz_Color = hz_v_color;\n",
+ p0 = corner(0, "pos"),
+ p1 = corner(1, "pos"),
+ p2 = corner(2, "pos"),
+ t0 = corner(0, "uv"),
+ t1 = corner(1, "uv"),
+ t2 = corner(2, "uv"),
+ );
+ let attributes = [
+ ("at_tangent", "hz_tangent"),
+ ("mc_midTexCoord", "vec4(hz_mid, 0.0, 1.0)"),
+ ("vaPosition", "hz_Vertex.xyz"),
+ ("vaUV0", "hz_v_uv"),
+ ("vaUV1", "ivec2(0, 10)"),
+ ("vaUV2", "ivec2(hz_MultiTexCoord1.xy)"),
+ ("vaColor", "hz_Color"),
+ ("vaNormal", "hz_Normal"),
+ ];
+ horizon_shaders::GbuffersInputs {
+ declarations,
+ body,
+ attributes: attributes
+ .iter()
+ .map(|&(n, v)| (n.to_owned(), v.to_owned()))
+ .collect(),
+ after: String::new(),
+ // Cutout, as vanilla's entity types mostly are (OQ-23): the loader's test at 0.1.
+ alpha_test: Some(horizon_shaders::AlphaTest {
+ op: ">",
+ reference: 0.1,
+ }),
+ }
+}
+
+/// What `gbuffers_clouds` reads from the clouds' mesh (entity vertices, `vs_cloud`): the
+/// position moved by the clouds' offset, the colour of the face's shade from their palette,
+/// full sky light. Set 1 holds the clouds' uniform (`clouds::Uniform`).
+pub fn cloud_inputs() -> horizon_shaders::GbuffersInputs {
+ let declarations = "layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in vec2 hz_v_uv;\n\
+ layout(location = 2) in uint hz_v_overlay;\nlayout(location = 3) in vec4 hz_v_color;\n\
+ layout(location = 4) in uvec4 hz_v_light;\nlayout(location = 5) in vec4 hz_v_normal;\n\
+ layout(set = 1, binding = 0) uniform HzCloud { vec4 hz_cloud_offset; vec4 hz_cloud_colors[4]; };\n"
+ .to_owned();
+ let body = " hz_Vertex = vec4(hz_v_pos + hz_cloud_offset.xyz, 1.0);\n \
+ hz_MultiTexCoord0 = vec4(hz_v_uv, 0.0, 1.0);\n \
+ hz_MultiTexCoord1 = vec4(0.0, 240.0, 0.0, 1.0);\n \
+ hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \
+ hz_Color = hz_cloud_colors[min(hz_v_light.x, 3u)];\n \
+ hz_Normal = dot(hz_v_normal.xyz, hz_v_normal.xyz) > 0.01 ? normalize(hz_v_normal.xyz) : vec3(0.0, 1.0, 0.0);\n"
+ .to_owned();
+ let attributes = [
+ ("vaPosition", "hz_Vertex.xyz"),
+ ("vaUV0", "hz_v_uv"),
+ ("vaUV2", "ivec2(0, 240)"),
+ ("vaColor", "hz_Color"),
+ ("vaNormal", "hz_Normal"),
+ ];
+ horizon_shaders::GbuffersInputs {
+ declarations,
+ body,
+ attributes: attributes
+ .iter()
+ .map(|&(n, v)| (n.to_owned(), v.to_owned()))
+ .collect(),
+ after: String::new(),
+ alpha_test: None,
+ }
+}
+
+/// `entityColor` for a run whose first quad carries overlay code `overlay`: the hurt red or a
+/// white flash, its alpha how much of that colour shows over the entity (vanilla mixes the
+/// overlay texel's colour in by one minus the texel's alpha).
+pub fn entity_color(overlay: u32) -> [f32; 4] {
+ let [r, g, b, a] = crate::overlay::HURT_TEXEL.map(|v| f32::from(v) / 255.0);
+ let white = |u: u32| {
+ let a = f32::from(crate::overlay::white_alpha(u)) / 255.0;
+ [1.0, 1.0, 1.0, 1.0 - a]
+ };
+ match overlay {
+ 1 | 8 => [r, g, b, 1.0 - a],
+ 5 => white(15),
+ 16..32 => white(overlay - crate::overlay::WHITE),
+ _ => [0.0; 4],
+ }
+}
+
+/// One draw: its quads in `PackEntities`' buffer, its program and its slot of `HzDraw`.
+pub(super) struct Draw {
+ /// The first vertex.
+ pub base: u32,
+ pub quads: u32,
+ /// The program that draws it (`ENTITY`, `BLOCK`, `TRANSLUCENT`, `PARTICLE`).
+ pub program: usize,
+ pub slot: u32,
+}
+
+/// The vertices and per-draw uniforms of the runs a pack draws, rebuilt each frame.
+pub(super) struct PackEntities {
+ vertices: wgpu::Buffer,
+ /// Set 1: the vertices again, for the quads' corners.
+ pub table: wgpu::BindGroupLayout,
+ pub table_group: wgpu::BindGroup,
+ slots: wgpu::Buffer,
+ pub group: wgpu::BindGroup,
+ stride: u64,
+ /// Two triangles per quad, for `quads` quads.
+ pub indices: wgpu::Buffer,
+ quads: u32,
+ scratch: Vec,
+ uniforms: Vec,
+ pub draws: Vec,
+}
+
+impl PackEntities {
+ pub fn new(device: &wgpu::Device, layout: &wgpu::BindGroupLayout) -> Self {
+ let stride = SLOT.max(u64::from(
+ device.limits().min_uniform_buffer_offset_alignment,
+ ));
+ let table = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some("Shader pack entity vertices"),
+ entries: &[wgpu::BindGroupLayoutEntry {
+ binding: 0,
+ visibility: wgpu::ShaderStages::VERTEX,
+ ty: wgpu::BindingType::Buffer {
+ ty: wgpu::BufferBindingType::Storage { read_only: true },
+ has_dynamic_offset: false,
+ min_binding_size: None,
+ },
+ count: None,
+ }],
+ });
+ let (vertices, table_group) = Self::vertex_buffer(device, &table, 4096);
+ let (slots, group) = Self::slot_buffer(device, layout, 64, stride);
+ Self {
+ vertices,
+ table,
+ table_group,
+ slots,
+ group,
+ stride,
+ indices: quad_indices(device, 1024),
+ quads: 1024,
+ scratch: Vec::new(),
+ uniforms: Vec::new(),
+ draws: Vec::new(),
+ }
+ }
+
+ fn vertex_buffer(
+ device: &wgpu::Device,
+ table: &wgpu::BindGroupLayout,
+ vertices: u64,
+ ) -> (wgpu::Buffer, wgpu::BindGroup) {
+ let buffer = device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("Shader pack entity vertices"),
+ size: vertices * std::mem::size_of::() as u64,
+ usage: wgpu::BufferUsages::VERTEX
+ | wgpu::BufferUsages::STORAGE
+ | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ });
+ let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("Shader pack entity vertices"),
+ layout: table,
+ entries: &[wgpu::BindGroupEntry {
+ binding: 0,
+ resource: buffer.as_entire_binding(),
+ }],
+ });
+ (buffer, group)
+ }
+
+ fn slot_buffer(
+ device: &wgpu::Device,
+ layout: &wgpu::BindGroupLayout,
+ slots: u64,
+ stride: u64,
+ ) -> (wgpu::Buffer, wgpu::BindGroup) {
+ let buffer = device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("Shader pack entity draws"),
+ size: slots * stride,
+ usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ });
+ let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("Shader pack entity draws"),
+ layout,
+ entries: &[wgpu::BindGroupEntry {
+ binding: 0,
+ resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
+ buffer: &buffer,
+ offset: 0,
+ size: wgpu::BufferSize::new(SLOT),
+ }),
+ }],
+ });
+ (buffer, group)
+ }
+
+ pub fn vertices(&self) -> wgpu::BufferSlice<'_> {
+ self.vertices.slice(..)
+ }
+
+ /// The dynamic offset of draw slot `slot`.
+ pub fn offset(&self, slot: u32) -> u32 {
+ (u64::from(slot) * self.stride) as u32
+ }
+
+ /// Splits `entities` between the pack, which draws the runs of `spans` its programs cover
+ /// (`drawn`: whether it has `gbuffers_entities`, `gbuffers_block` or their fallbacks), and
+ /// Horizon, which draws the rest (`rest`). Uploads the pack's part.
+ #[allow(clippy::too_many_arguments)]
+ pub fn prepare(
+ &mut self,
+ device: &wgpu::Device,
+ queue: &wgpu::Queue,
+ layout: &wgpu::BindGroupLayout,
+ entities: &[Vertex],
+ spans: &[EntitySpan],
+ drawn: [bool; PROGRAMS],
+ rest: &mut Vec,
+ ) {
+ self.draws.clear();
+ self.scratch.clear();
+ self.uniforms.clear();
+ rest.clear();
+ let first = spans.first().map_or(entities.len(), |s| s.start as usize);
+ rest.extend_from_slice(&entities[..first.min(entities.len())]);
+ for (span, range) in runs(entities.len(), spans) {
+ let run = &entities[range];
+ let Some(program) = drawn_by_pack(&span, drawn).filter(|_| plain(run)) else {
+ rest.extend_from_slice(run);
+ continue;
+ };
+ if run.is_empty() {
+ continue;
+ }
+ let start = self.scratch.len() as u32;
+ self.scratch.extend_from_slice(run);
+ let (entity_id, block_id) = match program {
+ BLOCK => (0, span.id),
+ _ => (span.id, 0),
+ };
+ let mut bytes = [0u8; SLOT as usize];
+ bytes[0..4].copy_from_slice(&(span.stage as i32).to_le_bytes());
+ bytes[4..8].copy_from_slice(&entity_id.to_le_bytes());
+ bytes[8..12].copy_from_slice(&block_id.to_le_bytes());
+ for (i, c) in entity_color(run[0].layer).iter().enumerate() {
+ bytes[16 + 4 * i..20 + 4 * i].copy_from_slice(&c.to_le_bytes());
+ }
+ self.uniforms.extend_from_slice(&bytes);
+ self.uniforms
+ .resize(self.uniforms.len() + (self.stride - SLOT) as usize, 0);
+ self.draws.push(Draw {
+ base: start,
+ quads: run.len() as u32 / 4,
+ program,
+ slot: self.draws.len() as u32,
+ });
+ }
+ if self.draws.is_empty() {
+ return;
+ }
+ let vertex_size = std::mem::size_of::() as u64;
+ let (need_vertices, need_slots) = (self.scratch.len() as u64, self.draws.len() as u64);
+ let have_vertices = self.vertices.size() / vertex_size;
+ let have_slots = self.slots.size() / self.stride;
+ if need_vertices > have_vertices {
+ (self.vertices, self.table_group) =
+ Self::vertex_buffer(device, &self.table, need_vertices.next_power_of_two());
+ }
+ if need_slots > have_slots {
+ (self.slots, self.group) =
+ Self::slot_buffer(device, layout, need_slots.next_power_of_two(), self.stride);
+ }
+ let quads = self.draws.iter().map(|d| d.quads).max().unwrap_or(0);
+ if quads > self.quads {
+ self.quads = quads.next_power_of_two();
+ self.indices = quad_indices(device, self.quads);
+ }
+ queue.write_buffer(&self.vertices, 0, bytemuck::cast_slice(&self.scratch));
+ queue.write_buffer(&self.slots, 0, &self.uniforms);
+ }
+}
+
+/// Two triangles for each of `quads` quads, from vertex 0.
+pub(super) fn quad_indices(device: &wgpu::Device, quads: u32) -> wgpu::Buffer {
+ let indices: Vec = (0..quads)
+ .flat_map(|q| [0, 1, 2, 0, 2, 3].map(|i| q * 4 + i))
+ .collect();
+ wgpu::util::DeviceExt::create_buffer_init(
+ device,
+ &wgpu::util::BufferInitDescriptor {
+ label: Some("Shader pack entity indices"),
+ contents: bytemuck::cast_slice(&indices),
+ usage: wgpu::BufferUsages::INDEX,
+ },
+ )
+}
+
+/// Whether a run holds only geometry a gbuffers program can draw from the shared atlas: no
+/// celestial body, cloud, beam, End portal or crack (overlay codes Horizon's entity shader
+/// draws in its own way; entity.wgsl).
+fn plain(run: &[Vertex]) -> bool {
+ run.iter()
+ .step_by(4)
+ .all(|v| !matches!(v.layer, 2 | 4 | 6 | 9 | 10 | 11 | 12))
+}
+
+/// The programs of entity runs, in the order they draw.
+pub const ENTITY: usize = 0;
+pub const BLOCK: usize = 1;
+pub const TRANSLUCENT: usize = 2;
+pub const PARTICLE: usize = 3;
+pub const PROGRAMS: usize = 4;
+
+/// Which program draws `span`'s run, given which of them (or their fallbacks) the pack has;
+/// `None`: Horizon draws it.
+pub fn drawn_by_pack(span: &EntitySpan, drawn: [bool; PROGRAMS]) -> Option {
+ let program = if span.stage == render_stage("ENTITIES") {
+ if span.translucent {
+ TRANSLUCENT
+ } else {
+ ENTITY
+ }
+ } else if span.stage == render_stage("BLOCK_ENTITIES") {
+ BLOCK
+ } else if span.stage == render_stage("PARTICLES") {
+ PARTICLE
+ } else {
+ return None;
+ };
+ drawn[program].then_some(program)
+}
+
+/// Each span with its vertex range: from its start to the next span's (or the end).
+pub fn runs(
+ len: usize,
+ spans: &[EntitySpan],
+) -> impl Iterator- )> + '_ {
+ spans.iter().enumerate().map(move |(i, s)| {
+ let end = spans.get(i + 1).map_or(len, |n| n.start as usize);
+ let start = (s.start as usize).min(len);
+ (*s, start..end.clamp(start, len))
+ })
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn runs_split_the_list_between_pack_and_horizon() {
+ let v = |layer| Vertex {
+ layer,
+ ..bytemuck::Zeroable::zeroed()
+ };
+ let span = |start, stage: &str, id| EntitySpan {
+ start,
+ stage: render_stage(stage),
+ id,
+ translucent: false,
+ };
+ let spans = [
+ span(2, "ENTITIES", 7),
+ span(5, "PARTICLES", 0),
+ span(6, "BLOCK_ENTITIES", 9),
+ ];
+ let ranges: Vec<_> = runs(10, &spans).map(|(_, r)| r).collect();
+ assert_eq!(ranges, [2..5, 5..6, 6..10]);
+ let all = [true; PROGRAMS];
+ assert_eq!(drawn_by_pack(&spans[0], all), Some(ENTITY));
+ assert_eq!(drawn_by_pack(&spans[1], all), Some(PARTICLE));
+ assert_eq!(drawn_by_pack(&spans[1], [true, true, true, false]), None);
+ assert_eq!(drawn_by_pack(&spans[2], [true, false, true, true]), None);
+ assert_eq!(drawn_by_pack(&spans[2], all), Some(BLOCK));
+ let shadow = EntitySpan {
+ translucent: true,
+ ..spans[0]
+ };
+ assert_eq!(drawn_by_pack(&shadow, all), Some(TRANSLUCENT));
+ assert_eq!(drawn_by_pack(&span(0, "DESTROY", 0), all), None);
+ // A beacon beam's quads stay Horizon's.
+ assert!(plain(&[v(0), v(0), v(0), v(0), v(1)]));
+ assert!(!plain(&[v(0), v(0), v(0), v(0), v(6)]));
+ assert_eq!(entity_color(1)[0], 1.0);
+ assert!((entity_color(1)[3] - (1.0 - 178.0 / 255.0)).abs() < 1e-6);
+ assert_eq!(entity_color(0), [0.0; 4]);
+ }
+}
diff --git a/crates/horizon-client/src/gfx/pack_hand.rs b/crates/horizon-client/src/gfx/pack_hand.rs
new file mode 100644
index 000000000..096cc5bcf
--- /dev/null
+++ b/crates/horizon-client/src/gfx/pack_hand.rs
@@ -0,0 +1,169 @@
+//! The first-person hand and held items drawn by a shader pack's `gbuffers_hand` (spec 2.3):
+//! `Frame::hand`'s entity vertices, read as entities are (`pack_entities::inputs`), with the
+//! hand's own projection as `gl_ProjectionMatrix` (5.2: `gbufferProjection` stays the world's).
+
+use glam::Mat4;
+
+use super::Vertex;
+
+/// `MC_HAND_DEPTH`'s value (1.5): the hand's depth is scaled by it through its projection, so
+/// that it sits in front of the world in the shared depth buffer.
+const HAND_DEPTH: f32 = 0.125;
+
+/// `HzHand`: the hand's projection and its inverse.
+const UNIFORM: u64 = 2 * 64;
+
+/// What `gbuffers_hand` reads: the entity inputs, and the hand's projection from set 1.
+pub fn inputs() -> horizon_shaders::GbuffersInputs {
+ let mut inputs = super::pack_entities::inputs();
+ inputs.declarations.push_str(
+ "layout(set = 1, binding = 1) uniform HzHand { mat4 hz_hand_projection; mat4 hz_hand_projection_inverse; };\n",
+ );
+ inputs.body.push_str(
+ " hz_ProjectionMatrix = hz_hand_projection;\n \
+ hz_ProjectionMatrixInverse = hz_hand_projection_inverse;\n \
+ hz_ModelViewProjectionMatrix = hz_hand_projection * hz_ModelViewMatrix;\n",
+ );
+ inputs
+}
+
+/// The hand's projection in GL's conventions: `hud_fov` degrees, the view bob, depth scaled by
+/// `MC_HAND_DEPTH`.
+pub fn projection(hud_fov: f32, aspect: f32, far: f32, bob: Mat4) -> Mat4 {
+ Mat4::from_scale(glam::Vec3::new(1.0, 1.0, HAND_DEPTH))
+ * Mat4::perspective_rh_gl(hud_fov.to_radians(), aspect, 0.05, far)
+ * bob
+}
+
+/// The hand's vertices (also read at set 1 for normals and tangents), its uniform and quad
+/// indices.
+pub struct PackHand {
+ pub layout: wgpu::BindGroupLayout,
+ vertices: wgpu::Buffer,
+ uniform: wgpu::Buffer,
+ pub group: wgpu::BindGroup,
+ pub indices: wgpu::Buffer,
+ capacity: u32,
+ /// Quads uploaded this frame.
+ pub quads: u32,
+}
+
+impl PackHand {
+ pub fn new(device: &wgpu::Device) -> Self {
+ let entry = |binding, ty| wgpu::BindGroupLayoutEntry {
+ binding,
+ visibility: wgpu::ShaderStages::VERTEX,
+ ty: wgpu::BindingType::Buffer {
+ ty,
+ has_dynamic_offset: false,
+ min_binding_size: None,
+ },
+ count: None,
+ };
+ let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some("Shader pack hand"),
+ entries: &[
+ entry(0, wgpu::BufferBindingType::Storage { read_only: true }),
+ entry(1, wgpu::BufferBindingType::Uniform),
+ ],
+ });
+ let uniform = device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("Shader pack hand"),
+ size: UNIFORM,
+ usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ });
+ let capacity = 256;
+ let (vertices, group) = Self::vertex_buffer(device, &layout, &uniform, capacity);
+ Self {
+ indices: super::pack_entities::quad_indices(device, capacity),
+ layout,
+ vertices,
+ uniform,
+ group,
+ capacity,
+ quads: 0,
+ }
+ }
+
+ fn vertex_buffer(
+ device: &wgpu::Device,
+ layout: &wgpu::BindGroupLayout,
+ uniform: &wgpu::Buffer,
+ quads: u32,
+ ) -> (wgpu::Buffer, wgpu::BindGroup) {
+ let buffer = device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("Shader pack hand vertices"),
+ size: u64::from(quads) * 4 * std::mem::size_of::
() as u64,
+ usage: wgpu::BufferUsages::VERTEX
+ | wgpu::BufferUsages::STORAGE
+ | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ });
+ let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("Shader pack hand"),
+ layout,
+ entries: &[
+ wgpu::BindGroupEntry {
+ binding: 0,
+ resource: buffer.as_entire_binding(),
+ },
+ wgpu::BindGroupEntry {
+ binding: 1,
+ resource: uniform.as_entire_binding(),
+ },
+ ],
+ });
+ (buffer, group)
+ }
+
+ /// Uploads this frame's hand and its projection.
+ pub fn prepare(
+ &mut self,
+ device: &wgpu::Device,
+ queue: &wgpu::Queue,
+ hand: &[Vertex],
+ projection: Mat4,
+ ) {
+ self.quads = (hand.len() / 4) as u32;
+ if self.quads == 0 {
+ return;
+ }
+ if self.quads > self.capacity {
+ self.capacity = self.quads.next_power_of_two();
+ (self.vertices, self.group) =
+ Self::vertex_buffer(device, &self.layout, &self.uniform, self.capacity);
+ self.indices = super::pack_entities::quad_indices(device, self.capacity);
+ }
+ queue.write_buffer(&self.vertices, 0, bytemuck::cast_slice(hand));
+ let matrices = [projection, projection.inverse()].map(|m| m.to_cols_array());
+ queue.write_buffer(&self.uniform, 0, bytemuck::cast_slice(&matrices));
+ }
+
+ pub fn vertices(&self) -> wgpu::BufferSlice<'_> {
+ self.vertices.slice(..)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ /// The hand's window depth lies within `MC_HAND_DEPTH` of the middle (packs tell the hand
+ /// apart by it), and x, y match the plain projection.
+ #[test]
+ fn hand_depth_is_scaled_around_the_middle() {
+ let p = projection(70.0, 16.0 / 9.0, 512.0, Mat4::IDENTITY);
+ let plain = Mat4::perspective_rh_gl(70f32.to_radians(), 16.0 / 9.0, 0.05, 512.0);
+ for z in [-0.06f32, -0.5, -3.0, -400.0] {
+ let v = glam::Vec4::new(0.1, -0.2, z, 1.0);
+ let (a, b) = (p * v, plain * v);
+ let window = (a.z / a.w) * 0.5 + 0.5;
+ assert!(
+ (window - 0.5).abs() <= HAND_DEPTH * 0.5 + 1e-6,
+ "{z}: {window}"
+ );
+ assert!((a.x / a.w - b.x / b.w).abs() < 1e-6);
+ }
+ }
+}
diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs
index d5aa9fb7c..2be557c17 100644
--- a/crates/horizon-client/src/gfx/pack_post.rs
+++ b/crates/horizon-client/src/gfx/pack_post.rs
@@ -13,8 +13,11 @@
use std::collections::HashMap;
-use horizon_shaders::{Format, Group, Kind, Post};
+use horizon_shaders::{Format, Group, Kind, Post, Val};
+use super::pack_draw::DrawUniforms;
+use super::pack_entities::{PROGRAMS, PackEntities};
+use super::pack_sky::PackSky;
use super::pack_uniforms::{self, FrameUniforms, PackInputs};
const BUFFERS: usize = horizon_shaders::BUFFERS;
@@ -24,20 +27,121 @@ const BUFFERS: usize = horizon_shaders::BUFFERS;
enum Source {
Color(usize),
Depth,
+ /// `depthtex1`/`depthtex2`: depth without translucent terrain (a copy taken before it).
+ Depth1,
Noise,
/// A 1×1 stand-in for names this slice does not provide yet (custom textures...).
Dummy,
/// A 1×1 depth of 1.0 behind a comparison sampler, until shadow maps exist: no shadow.
Shadow,
+ /// The block atlas (pack_atlas.rs), in gbuffers programs.
+ Atlas,
+ Lightmap,
}
+#[derive(Clone)]
struct Binding {
source: Source,
binding: u32,
/// A filtering sampler (the texture is filterable).
filtering: bool,
+ /// Where this texture's sampler object is bound: the first texture of each kind binds it
+ /// (`binding + 1`), the others share it (devices have as few as 16 sampler slots).
+ sampler: u32,
}
+/// The sampler objects a pass binds, one slot per kind it uses.
+#[derive(Clone, Copy, PartialEq, Eq, Hash)]
+enum SamplerKind {
+ Linear,
+ Nearest,
+ Repeat,
+ NearestRepeat,
+ Comparison,
+}
+
+impl SamplerKind {
+ fn of(source: Source, filtering: bool) -> Self {
+ match source {
+ Source::Color(_) | Source::Dummy if !filtering => Self::Nearest,
+ Source::Depth | Source::Depth1 => Self::Nearest,
+ Source::Noise => Self::Repeat,
+ Source::Shadow => Self::Comparison,
+ // Blocks look as the game draws them: nearest texels.
+ Source::Atlas => Self::NearestRepeat,
+ Source::Color(_) | Source::Dummy | Source::Lightmap => Self::Linear,
+ }
+ }
+
+ fn binding_type(self) -> wgpu::SamplerBindingType {
+ match self {
+ Self::Linear | Self::Repeat => wgpu::SamplerBindingType::Filtering,
+ Self::Nearest | Self::NearestRepeat => wgpu::SamplerBindingType::NonFiltering,
+ Self::Comparison => wgpu::SamplerBindingType::Comparison,
+ }
+ }
+}
+
+/// Points every sampler object of `module` at the slot `slot` gives its binding, so that
+/// samplers sharing a slot become one variable (two variables may not share a binding).
+fn share_samplers(module: &mut naga::Module, slot: &HashMap) {
+ let mut first = HashMap::new();
+ let mut same = HashMap::new();
+ for (h, g) in module.global_variables.iter_mut() {
+ let Some(b) = g.binding.as_mut() else {
+ continue;
+ };
+ if !matches!(module.types[g.ty].inner, naga::TypeInner::Sampler { .. }) {
+ continue;
+ }
+ let Some(&to) = slot.get(&b.binding) else {
+ continue;
+ };
+ b.binding = to;
+ match first.entry(to) {
+ std::collections::hash_map::Entry::Occupied(e) => _ = same.insert(h, *e.get()),
+ std::collections::hash_map::Entry::Vacant(e) => _ = e.insert(h),
+ }
+ }
+ if same.is_empty() {
+ return;
+ }
+ let fix = |expressions: &mut naga::Arena| {
+ for (_, e) in expressions.iter_mut() {
+ if let naga::Expression::GlobalVariable(g) = e
+ && let Some(&to) = same.get(g)
+ {
+ *g = to;
+ }
+ }
+ };
+ for (_, f) in module.functions.iter_mut() {
+ fix(&mut f.expressions);
+ }
+ for ep in &mut module.entry_points {
+ fix(&mut ep.function.expressions);
+ }
+}
+
+/// The sky's geometry and the programs that draw it: `gbuffers_skybasic` (disc, sunset, stars,
+/// void) and `gbuffers_skytextured` (sun, moon), each with its fallbacks. A part without a
+/// program is Horizon's own.
+struct Sky {
+ geometry: PackSky,
+ basic: Option,
+ textured: Option,
+}
+
+/// Entities, block entities, translucent entity parts and particles a pack draws
+/// (`pack_entities::ENTITY`... order), after terrain or, `late`, after deferred; Horizon draws
+/// the others.
+struct Entities {
+ geometry: PackEntities,
+ passes: [Option; PROGRAMS],
+ late: [bool; PROGRAMS],
+}
+
+#[derive(Clone)]
struct Pass {
name: String,
group: Group,
@@ -56,7 +160,25 @@ struct Pass {
reads: u16,
/// Buffers written.
written: u16,
- groups: HashMap,
+ /// Buffers whose mip chain is regenerated right before it (6.1.7).
+ mipmaps: u16,
+ /// Reads the per-draw block (`DrawUniforms`).
+ draw: bool,
+ /// What `texture` reads instead of the block atlas.
+ atlas: Option,
+ /// Reads `mc_Entity`'s table (`PackPost::block_ids`).
+ ids: bool,
+ /// Bind groups by the sides read (low 16 bits) and the buffers whose mip chain is current
+ /// (high 16 bits).
+ groups: HashMap,
+}
+
+/// One side of a buffer with mipmaps.
+struct Mips {
+ /// Every level, for sampling.
+ all: wgpu::TextureView,
+ /// From the second level down: its attachment, and a bind group over the level above.
+ levels: Vec<(wgpu::TextureView, wgpu::BindGroup)>,
}
struct Targets {
@@ -64,11 +186,96 @@ struct Targets {
/// A and B of each buffer used.
color: [Option<[wgpu::TextureView; 2]>; BUFFERS],
depth: wgpu::TextureView,
+ depth_texture: wgpu::Texture,
+ /// `depthtex1`, when a program after translucent terrain reads it.
+ depth1: Option<(wgpu::Texture, wgpu::TextureView)>,
+ /// Both sides' mip chains of the buffers with mipmaps (`color` is their first level).
+ mips: [Option<[Mips; 2]>; BUFFERS],
+}
+
+/// What a pack's gbuffers programs read and draw from the game's world.
+pub(super) struct World<'a> {
+ /// The block atlas and its size.
+ pub atlas: Option<(wgpu::TextureView, [u32; 2])>,
+ pub lightmap: wgpu::TextureView,
+ /// Terrain's geometry, when the pack draws it.
+ pub terrain: Option>,
+ /// `mc_Entity` of each block sprite layer: (id, 1 for fluids or -1) pairs.
+ pub block_ids: &'a [i32],
+ /// The atlas of the sun and moon's sprites.
+ pub celestial: Option,
+ /// Set 1 of the clouds' draw: their offset and palette.
+ pub clouds: Option<&'a wgpu::BindGroupLayout>,
+ /// The game can hand its rain and snow to the pack.
+ pub weather: bool,
+ /// The game can hand its first-person hand to the pack.
+ pub hand: bool,
+}
+
+/// This frame's clouds, when the pack draws them: their vertices, uniform, quad indices and
+/// quad count.
+pub(super) struct Clouds {
+ pub vertices: wgpu::Buffer,
+ pub group: wgpu::BindGroup,
+ pub indices: wgpu::Buffer,
+ pub quads: u32,
+}
+
+/// This frame's rain and snow, when the pack draws them: their vertices, the quad indices, the
+/// quads of each kind (rain, snow), each kind's sprite, and a number that changes with them.
+pub(super) struct Weather {
+ pub vertices: wgpu::Buffer,
+ pub indices: wgpu::Buffer,
+ pub quads: [std::ops::Range; 2],
+ pub sprites: [Option; 2],
+ pub generation: u64,
}
pub struct PackPost {
pub notes: Vec,
passes: Vec,
+ /// What draws terrain (`gbuffers_terrain` or its fallback), between `prepare` and `deferred`.
+ terrain: Option,
+ /// What draws translucent terrain (`gbuffers_water` or its fallback), after `deferred`.
+ water: Option,
+ /// What draws the sky (`gbuffers_skybasic` or its fallback), before terrain, and its vertices.
+ sky: Option,
+ /// What draws entities and block entities, after terrain, and their vertices.
+ entities: Option,
+ /// What draws the clouds, after entities, and this frame's clouds.
+ clouds: Option,
+ cloud_draw: Option,
+ /// The pack has `gbuffers_clouds`, even one that writes nothing (a pack drawing its own
+ /// clouds discards vanilla's): the game's clouds are not drawn.
+ hides_clouds: bool,
+ /// What draws rain and snow (the same program, each with its kind's sprite as `texture`),
+ /// after translucent terrain, this frame's weather and the sprites' generation bound.
+ weather: Option<[Pass; 2]>,
+ weather_draw: Option,
+ weather_generation: u64,
+ /// What draws the first-person hand (`gbuffers_hand` or its fallback), after the opaque
+ /// world, and its vertices.
+ hand: Option<(Pass, super::pack_hand::PackHand)>,
+ /// A program after translucent terrain reads `depthtex1`: it is copied before water.
+ depth1: bool,
+ /// Buffers with mipmaps (filterable ones a program asks them for).
+ mipped: u16,
+ /// Buffers whose mip chain matches their first level, this frame.
+ mips_valid: u16,
+ /// The downsampling pipeline of each format with mipmaps.
+ down: HashMap,
+ /// The per-draw uniforms of gbuffers programs.
+ draws: DrawUniforms,
+ /// Set 3 of flat terrain (`mc_Entity`'s table) and its bind group for each arena page,
+ /// with the page buffer it was made for.
+ block_ids: Option<(wgpu::BindGroupLayout, wgpu::Buffer)>,
+ page_groups: HashMap,
+ /// `HORIZON_SHADER_PACK_TIMING`: GPU time of each pass, in the log.
+ timing: Option,
+ /// `shaders.properties`' custom uniforms, after the frame's in `uniforms.values`.
+ custom: horizon_shaders::Custom,
+ atlas: Option,
+ lightmap: wgpu::TextureView,
deferred_pre: u16,
composite_pre: u16,
formats: [wgpu::TextureFormat; BUFFERS],
@@ -85,6 +292,7 @@ pub struct PackPost {
linear: wgpu::Sampler,
nearest: wgpu::Sampler,
repeat: wgpu::Sampler,
+ nearest_repeat: wgpu::Sampler,
blit: (wgpu::RenderPipeline, wgpu::BindGroupLayout),
/// The blit also writes depthtex0 (some program reads it) and clears these buffers.
blit_depth: bool,
@@ -109,7 +317,12 @@ fn texture_format(f: Format, features: wgpu::Features) -> wgpu::TextureFormat {
let half = pick(T::R16Float, T::Rg16Float, T::Rgba16Float);
match (f.kind, f.bits) {
(Kind::Unorm, 10) => T::Rgb10a2Unorm,
- (Kind::Unorm, 16) if features.contains(wgpu::Features::TEXTURE_FORMAT_16BIT_NORM) => {
+ (Kind::Unorm, 16)
+ if features.contains(
+ wgpu::Features::TEXTURE_FORMAT_16BIT_NORM
+ | wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES,
+ ) =>
+ {
pick(T::R16Unorm, T::Rg16Unorm, T::Rgba16Unorm)
}
(Kind::Unorm, b) if b <= 8 => pick(T::R8Unorm, T::Rg8Unorm, T::Rgba8Unorm),
@@ -131,8 +344,12 @@ fn texture_format(f: Format, features: wgpu::Features) -> wgpu::TextureFormat {
fn source(name: &str) -> Source {
match name {
- "depthtex0" | "gdepthtex" | "depthtex1" | "depthtex2" => Source::Depth,
+ "depthtex0" | "gdepthtex" => Source::Depth,
+ // The hand is drawn after the pack, so `depthtex2` (without it) is `depthtex1`.
+ "depthtex1" | "depthtex2" => Source::Depth1,
"noisetex" => Source::Noise,
+ "texture" | "gtexture" | "tex" => Source::Atlas,
+ "lightmap" => Source::Lightmap,
n => horizon_shaders::buffer_index(n).map_or(Source::Dummy, Source::Color),
}
}
@@ -208,8 +425,11 @@ fn checked(device: &wgpu::Device, create: impl FnOnce() -> T) -> Result {
- // The details are in the error's sources.
- let mut message = e.to_string();
+ // The details are in the error's description or its sources.
+ let mut message = match &e {
+ wgpu::Error::Validation { source, .. } => format!("{source:?}"),
+ e => e.to_string(),
+ };
let mut source = std::error::Error::source(&e);
while let Some(s) = source {
message = format!("{message}: {s}");
@@ -228,6 +448,7 @@ fn helper(
entry: &str,
entries: &[wgpu::BindGroupLayoutEntry],
targets: &[Option],
+ depth: Option,
) -> (wgpu::RenderPipeline, wgpu::BindGroupLayout) {
let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("Shader pack helpers"),
@@ -252,7 +473,7 @@ fn helper(
buffers: &[],
},
primitive: Default::default(),
- depth_stencil: None,
+ depth_stencil: depth,
multisample: Default::default(),
fragment: Some(wgpu::FragmentState {
module: &module,
@@ -286,6 +507,34 @@ fn texture_entry(binding: u32, sample_type: wgpu::TextureSampleType) -> wgpu::Bi
)
}
+/// `blend.`'s factors as wgpu's, adding both terms.
+fn blend_state(b: horizon_shaders::Blend) -> wgpu::BlendState {
+ use horizon_shaders::Factor as F;
+ use wgpu::BlendFactor as W;
+ let f = |f| match f {
+ F::Zero => W::Zero,
+ F::One => W::One,
+ F::SrcColor => W::Src,
+ F::OneMinusSrcColor => W::OneMinusSrc,
+ F::DstColor => W::Dst,
+ F::OneMinusDstColor => W::OneMinusDst,
+ F::SrcAlpha => W::SrcAlpha,
+ F::OneMinusSrcAlpha => W::OneMinusSrcAlpha,
+ F::DstAlpha => W::DstAlpha,
+ F::OneMinusDstAlpha => W::OneMinusDstAlpha,
+ F::SrcAlphaSaturate => W::SrcAlphaSaturated,
+ };
+ let component = |src, dst| wgpu::BlendComponent {
+ src_factor: f(src),
+ dst_factor: f(dst),
+ operation: wgpu::BlendOperation::Add,
+ };
+ wgpu::BlendState {
+ color: component(b[0], b[1]),
+ alpha: component(b[2], b[3]),
+ }
+}
+
fn target(format: wgpu::TextureFormat) -> Option {
Some(wgpu::ColorTargetState {
format,
@@ -306,6 +555,309 @@ fn attach(
})
}
+/// Draws the game's terrain (`true`: translucent) into a pass; calls its third argument with
+/// each arena page before drawing from it.
+pub(super) type TerrainDraw<'a> =
+ dyn FnMut(&mut wgpu::RenderPass<'_>, bool, &dyn Fn(&mut wgpu::RenderPass<'_>, u32)) + 'a;
+
+/// Set 3 of flat terrain, read by the vertex stage: `mc_Entity`'s table (-1 pairs when empty)
+/// and, per arena page, the page's vertices (a vertex reads its quad's corners for its normal).
+fn block_ids(device: &wgpu::Device, table: &[i32]) -> (wgpu::BindGroupLayout, wgpu::Buffer) {
+ let empty = [-1, -1];
+ let table = if table.is_empty() { &empty[..] } else { table };
+ let buffer = wgpu::util::DeviceExt::create_buffer_init(
+ device,
+ &wgpu::util::BufferInitDescriptor {
+ label: Some("Shader pack block ids"),
+ contents: bytemuck::cast_slice(table),
+ usage: wgpu::BufferUsages::STORAGE,
+ },
+ );
+ let storage = |binding| wgpu::BindGroupLayoutEntry {
+ binding,
+ visibility: wgpu::ShaderStages::VERTEX,
+ ty: wgpu::BindingType::Buffer {
+ ty: wgpu::BufferBindingType::Storage { read_only: true },
+ has_dynamic_offset: false,
+ min_binding_size: None,
+ },
+ count: None,
+ };
+ let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some("Shader pack terrain"),
+ entries: &[storage(0), storage(1)],
+ });
+ (layout, buffer)
+}
+
+/// The game's geometry a gbuffers program draws: its vertex buffers and, for flat draws
+/// (flat.rs), the layout of their bind group 1.
+pub(super) struct Geometry<'a> {
+ pub buffers: &'a [Option>],
+ pub table: Option<&'a wgpu::BindGroupLayout>,
+ /// Writes depth (the sky does not).
+ pub depth_write: bool,
+ /// What `texture` reads instead of the block atlas.
+ pub atlas: Option,
+ /// `mc_Entity`'s table at set 3 (flat terrain).
+ pub ids: Option<&'a wgpu::BindGroupLayout>,
+ /// Back faces are culled, as for the game's own terrain.
+ pub cull: bool,
+}
+
+/// The pass of program `p`: a quad, or `geometry` drawn by the game. `None` (with a note) when
+/// wgpu refuses it or it writes nothing.
+fn build_pass(
+ device: &wgpu::Device,
+ p: horizon_shaders::PostProgram,
+ formats: &[wgpu::TextureFormat; BUFFERS],
+ screen: wgpu::TextureFormat,
+ notes: &mut Vec,
+ geometry: Option<&Geometry<'_>>,
+ custom: &[String],
+ draws: &wgpu::BindGroupLayout,
+) -> Option {
+ let features = device.features();
+ // The frame's uniforms, then the pack's custom ones.
+ let index = |name: &str| {
+ pack_uniforms::index(name).or_else(|| {
+ let k = custom.iter().position(|c| c == name)?;
+ Some(pack_uniforms::NAMES.len() + k)
+ })
+ };
+ let mask = |flips: &[(u32, bool)]| flips.iter().filter(|f| f.1).fold(0u16, |m, f| m | 1 << f.0);
+ let interface = &p.program.interface;
+ let draw = interface.draw;
+ let mut entries = vec![entry(
+ 0,
+ wgpu::BindingType::Buffer {
+ ty: wgpu::BufferBindingType::Uniform,
+ has_dynamic_offset: false,
+ min_binding_size: None,
+ },
+ )];
+ let mut samplers = Vec::new();
+ let (mut kinds, mut slots) = (HashMap::new(), HashMap::new());
+ let mut reads = 0u16;
+ // A gbuffers pass draws into its buffers and the depth buffer: reading them as well would
+ // be a feedback loop (undefined in GL, refused by wgpu), so those read a blank texture.
+ let in_place = match geometry {
+ Some(_) => p.buffers.iter().fold(0u16, |m, &b| m | 1 << b),
+ None => 0,
+ };
+ for s in &interface.samplers {
+ let mut src = source(&s.name);
+ let blank = match src {
+ Source::Atlas => geometry.is_none(),
+ Source::Depth => geometry.is_some(),
+ // Translucent terrain reads the copy; opaque terrain would read itself.
+ Source::Depth1 => p.group == Group::Gbuffers,
+ Source::Color(n) => in_place & 1 << n != 0,
+ _ => false,
+ };
+ let float = wgpu::TextureSampleType::Float { filterable: true };
+ let mut sample_type = match src {
+ Source::Color(n) => formats[n]
+ .sample_type(None, Some(features))
+ .unwrap_or(float),
+ Source::Depth | Source::Depth1 => wgpu::TextureSampleType::Float { filterable: false },
+ // Unfilterable on WebGPU-level devices; the lightmap is sampled at texel centres.
+ Source::Lightmap => float,
+ _ => float,
+ };
+ let int = !matches!(sample_type, wgpu::TextureSampleType::Float { .. });
+ if s.ty == "sampler2DShadow" {
+ (src, sample_type) = (Source::Shadow, wgpu::TextureSampleType::Depth);
+ }
+ let wanted = match s.ty.as_str() {
+ "sampler2DShadow" => true,
+ "sampler2D" => !int,
+ "isampler2D" => sample_type == wgpu::TextureSampleType::Sint,
+ "usampler2D" => sample_type == wgpu::TextureSampleType::Uint,
+ _ => false,
+ };
+ if !wanted {
+ notes.push(format!(
+ "{}: `{} {}` is not provided yet",
+ p.name, s.ty, s.name
+ ));
+ (src, sample_type) = (Source::Dummy, float);
+ }
+ if blank {
+ // The blank texture is float: an integer sampler then fails the pipeline (a note).
+ src = Source::Dummy;
+ if int {
+ sample_type = float;
+ }
+ }
+ let filtering = sample_type == float;
+ if let Source::Color(n) = src {
+ reads |= 1 << n;
+ }
+ entries.push(texture_entry(s.binding, sample_type));
+ let kind = SamplerKind::of(src, filtering);
+ let sampler = *kinds.entry(kind).or_insert_with(|| {
+ entries.push(entry(
+ s.binding + 1,
+ wgpu::BindingType::Sampler(kind.binding_type()),
+ ));
+ s.binding + 1
+ });
+ slots.insert(s.binding + 1, sampler);
+ samplers.push(Binding {
+ source: src,
+ binding: s.binding,
+ filtering,
+ sampler,
+ });
+ }
+ let final_pass = p.group == Group::Final;
+ let mut targets = Vec::new();
+ let mut writes = Vec::new();
+ for &location in &interface.outputs {
+ let l = location as usize;
+ if targets.len() <= l {
+ targets.resize(l + 1, None);
+ }
+ if final_pass {
+ if location == 0 {
+ targets[0] = target(screen);
+ }
+ } else if let Some(&buffer) = p.buffers.get(l) {
+ let format = formats[buffer as usize];
+ let mut state = target(format);
+ if let (Some(t), Some(b)) = (&mut state, p.blend)
+ && format
+ .guaranteed_format_features(features)
+ .flags
+ .contains(wgpu::TextureFormatFeatureFlags::BLENDABLE)
+ {
+ t.blend = Some(blend_state(b));
+ }
+ targets[l] = state;
+ writes.push((location, buffer as usize));
+ }
+ }
+ if !final_pass && writes.is_empty() {
+ notes.push(format!("{}: writes no buffer; skipped", p.name));
+ return None;
+ }
+ let written = writes.iter().fold(0u16, |m, w| m | 1 << w.1);
+ let kept = p
+ .flips
+ .iter()
+ .filter(|f| !f.1)
+ .fold(0u16, |m, f| m | 1 << f.0);
+ // Gbuffers programs write in place (6.1.6).
+ let flip = if final_pass || geometry.is_some() {
+ 0
+ } else {
+ (written & !kept) | mask(&p.flips)
+ };
+ let created = checked(device, || {
+ let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some(&p.name),
+ entries: &entries,
+ });
+ let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
+ label: Some(&p.name),
+ bind_group_layouts: &[
+ Some(&layout),
+ geometry.and_then(|g| g.table),
+ draw.then_some(draws),
+ geometry.and_then(|g| g.ids),
+ ],
+ ..Default::default()
+ });
+ let module = |mut m: naga::Module| {
+ share_samplers(&mut m, &slots);
+ device.create_shader_module(wgpu::ShaderModuleDescriptor {
+ label: Some(&p.name),
+ source: wgpu::ShaderSource::Naga(std::borrow::Cow::Owned(m)),
+ })
+ };
+ let (vs, fs) = (module(p.program.vertex), module(p.program.fragment));
+ let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
+ label: Some(&p.name),
+ layout: Some(&pipeline_layout),
+ vertex: wgpu::VertexState {
+ module: &vs,
+ entry_point: Some("main"),
+ compilation_options: Default::default(),
+ buffers: geometry.map_or(&[], |g| g.buffers),
+ },
+ // Rows flipped to GL's turn front faces clockwise.
+ primitive: wgpu::PrimitiveState {
+ front_face: wgpu::FrontFace::Cw,
+ cull_mode: geometry.filter(|g| g.cull).map(|_| wgpu::Face::Back),
+ ..Default::default()
+ },
+ depth_stencil: geometry.map(|g| wgpu::DepthStencilState {
+ format: wgpu::TextureFormat::Depth32Float,
+ depth_write_enabled: Some(g.depth_write),
+ depth_compare: Some(wgpu::CompareFunction::LessEqual),
+ stencil: Default::default(),
+ bias: Default::default(),
+ }),
+ multisample: Default::default(),
+ fragment: Some(wgpu::FragmentState {
+ module: &fs,
+ entry_point: Some("main"),
+ compilation_options: Default::default(),
+ targets: &targets,
+ }),
+ multiview_mask: None,
+ cache: None,
+ });
+ (layout, pipeline)
+ });
+ let (layout, pipeline) = match created {
+ Ok(created) => created,
+ Err(e) => {
+ notes.push(format!("{}: skipped: {e}", p.name));
+ return None;
+ }
+ };
+ let interface = &p.program.interface;
+ let missing: Vec<&str> = interface
+ .uniforms
+ .iter()
+ .filter(|u| !u.name.starts_with("hz_") && index(&u.name).is_none())
+ .map(|u| u.name.as_str())
+ .collect();
+ if !missing.is_empty() {
+ notes.push(format!("{}: zero for now: {}", p.name, missing.join(" ")));
+ }
+ Some(Pass {
+ ubo: device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some(&p.name),
+ size: u64::from(interface.uniform_size.max(16)),
+ usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ }),
+ block: vec![0; interface.uniform_size as usize],
+ uniforms: interface
+ .uniforms
+ .iter()
+ .map(|u| (index(&u.name), u.ty.clone(), u.offset))
+ .collect(),
+ name: p.name,
+ group: p.group,
+ pipeline,
+ layout,
+ samplers,
+ writes,
+ flip,
+ reads,
+ written,
+ mipmaps: p.mipmaps,
+ draw,
+ atlas: geometry.and_then(|g| g.atlas.clone()),
+ ids: geometry.is_some_and(|g| g.ids.is_some()),
+ groups: HashMap::new(),
+ })
+}
+
impl PackPost {
/// Builds the pipelines of `post` for a screen of `screen` format.
pub fn new(
@@ -313,6 +865,7 @@ impl PackPost {
queue: &wgpu::Queue,
post: Post,
screen: wgpu::TextureFormat,
+ world: World<'_>,
) -> Self {
let features = device.features();
let formats: [wgpu::TextureFormat; BUFFERS] =
@@ -326,208 +879,257 @@ impl PackPost {
});
let mut notes = post.notes;
notes.extend(post.config.notes);
+ let shadow_config = post.config.shadow.clone();
+ let mut custom = post.custom;
+ let custom_names: Vec = custom.uniforms().map(|u| u.0.to_owned()).collect();
+ // A first run finds the names neither the frame nor the pack defines.
+ custom.eval(0.0, &|n| pack_uniforms::index(n).map(|_| Val::scalar(0.0)));
+ if !custom.notes.is_empty() {
+ notes.push(format!("custom uniforms: {}", custom.notes.join("; ")));
+ custom.notes.clear();
+ }
let mask =
|flips: &[(u32, bool)]| flips.iter().filter(|f| f.1).fold(0u16, |m, f| m | 1 << f.0);
let mut used = 1u16;
- let mut passes = Vec::new();
- for p in post.programs {
- let interface = &p.program.interface;
- let mut entries = vec![entry(
- 0,
- wgpu::BindingType::Buffer {
- ty: wgpu::BufferBindingType::Uniform,
- has_dynamic_offset: false,
- min_binding_size: None,
- },
- )];
- let mut samplers = Vec::new();
- let mut reads = 0u16;
- for s in &interface.samplers {
- let mut src = source(&s.name);
- let float = wgpu::TextureSampleType::Float { filterable: true };
- let mut sample_type = match src {
- Source::Color(n) => formats[n]
- .sample_type(None, Some(features))
- .unwrap_or(float),
- Source::Depth => wgpu::TextureSampleType::Float { filterable: false },
- _ => float,
- };
- let int = !matches!(sample_type, wgpu::TextureSampleType::Float { .. });
- if s.ty == "sampler2DShadow" {
- (src, sample_type) = (Source::Shadow, wgpu::TextureSampleType::Depth);
- }
- let wanted = match s.ty.as_str() {
- "sampler2DShadow" => true,
- "sampler2D" => !int,
- "isampler2D" => sample_type == wgpu::TextureSampleType::Sint,
- "usampler2D" => sample_type == wgpu::TextureSampleType::Uint,
- _ => false,
- };
- if !wanted {
- notes.push(format!(
- "{}: `{} {}` is not provided yet",
- p.name, s.ty, s.name
- ));
- (src, sample_type) = (Source::Dummy, float);
- }
- let filtering = sample_type == float;
- if let Source::Color(n) = src {
- reads |= 1 << n;
- }
- entries.push(texture_entry(s.binding, sample_type));
- entries.push(entry(
- s.binding + 1,
- wgpu::BindingType::Sampler(if src == Source::Shadow {
- wgpu::SamplerBindingType::Comparison
- } else if filtering {
- wgpu::SamplerBindingType::Filtering
- } else {
- wgpu::SamplerBindingType::NonFiltering
- }),
- ));
- samplers.push(Binding {
- source: src,
- binding: s.binding,
- filtering,
- });
- }
- let final_pass = p.group == Group::Final;
- let mut targets = Vec::new();
- let mut writes = Vec::new();
- for &location in &interface.outputs {
- let l = location as usize;
- if targets.len() <= l {
- targets.resize(l + 1, None);
- }
- if final_pass {
- if location == 0 {
- targets[0] = target(screen);
- }
- } else if let Some(&buffer) = p.buffers.get(l) {
- targets[l] = target(formats[buffer as usize]);
- writes.push((location, buffer as usize));
- }
- }
- if !final_pass && writes.is_empty() {
- notes.push(format!("{}: writes no buffer; skipped", p.name));
- continue;
- }
- let written = writes.iter().fold(0u16, |m, w| m | 1 << w.1);
- let kept = p
- .flips
- .iter()
- .filter(|f| !f.1)
- .fold(0u16, |m, f| m | 1 << f.0);
- let flip = if final_pass {
- 0
- } else {
- (written & !kept) | mask(&p.flips)
+ let draws = DrawUniforms::new(device);
+ let mut build = |p: horizon_shaders::PostProgram, geometry: Option<&Geometry<'_>>| {
+ let pass = build_pass(
+ device,
+ p,
+ &formats,
+ screen,
+ &mut notes,
+ geometry,
+ &custom_names,
+ &draws.layout,
+ )?;
+ used |= pass.reads | pass.written | pass.flip;
+ Some(pass)
+ };
+ // Flat terrain reads `mc_Entity` from a table of the sprite layers.
+ let block_ids = world
+ .terrain
+ .as_ref()
+ .filter(|g| g.table.is_some())
+ .map(|_| block_ids(device, world.block_ids));
+ let terrain_geometry = world.terrain.as_ref().map(|g| Geometry {
+ buffers: g.buffers,
+ table: g.table,
+ depth_write: g.depth_write,
+ atlas: g.atlas.clone(),
+ ids: block_ids.as_ref().map(|i| &i.0),
+ cull: g.cull,
+ });
+ let terrain = post
+ .terrain
+ .zip(terrain_geometry.as_ref())
+ .and_then(|(p, g)| build(p, Some(g)));
+ let water = post
+ .water
+ .zip(terrain_geometry.as_ref())
+ .and_then(|(p, g)| build(p, Some(g)));
+ let sky = (post.sky.is_some() || post.sky_textured.is_some()).then(|| {
+ let geometry = PackSky::new(device);
+ let basic = Geometry {
+ buffers: &PackSky::buffers(),
+ table: Some(&geometry.layout),
+ depth_write: false,
+ atlas: None,
+ ids: None,
+ cull: false,
};
- used |= reads | written | flip;
- let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
- label: Some(&p.name),
- entries: &entries,
- });
- let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
- label: Some(&p.name),
- bind_group_layouts: &[Some(&layout)],
- ..Default::default()
- });
- let created = checked(device, || {
- let module = |m: naga::Module| {
- device.create_shader_module(wgpu::ShaderModuleDescriptor {
- label: Some(&p.name),
- source: wgpu::ShaderSource::Naga(std::borrow::Cow::Owned(m)),
- })
- };
- let (vs, fs) = (module(p.program.vertex), module(p.program.fragment));
- device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
- label: Some(&p.name),
- layout: Some(&pipeline_layout),
- vertex: wgpu::VertexState {
- module: &vs,
- entry_point: Some("main"),
- compilation_options: Default::default(),
- buffers: &[],
- },
- primitive: Default::default(),
- depth_stencil: None,
- multisample: Default::default(),
- fragment: Some(wgpu::FragmentState {
- module: &fs,
- entry_point: Some("main"),
- compilation_options: Default::default(),
- targets: &targets,
- }),
- multiview_mask: None,
- cache: None,
- })
- });
- let pipeline = match created {
- Ok(p) => p,
- Err(e) => {
- notes.push(format!("{}: skipped: {e}", p.name));
- continue;
- }
+ let basic = post.sky.and_then(|p| build(p, Some(&basic)));
+ let textured = Geometry {
+ buffers: &PackSky::celestial_buffers(),
+ table: None,
+ depth_write: false,
+ atlas: world.celestial.clone(),
+ ids: None,
+ cull: false,
};
- let interface = &p.program.interface;
- passes.push(Pass {
- ubo: device.create_buffer(&wgpu::BufferDescriptor {
- label: Some(&p.name),
- size: u64::from(interface.uniform_size.max(16)),
- usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
- mapped_at_creation: false,
- }),
- block: vec![0; interface.uniform_size as usize],
- uniforms: interface
- .uniforms
- .iter()
- .map(|u| (pack_uniforms::index(&u.name), u.ty.clone(), u.offset))
- .collect(),
- name: p.name,
- group: p.group,
- pipeline,
- layout,
- samplers,
- writes,
- flip,
- reads,
- written,
- groups: HashMap::new(),
+ let textured = post.sky_textured.and_then(|p| build(p, Some(&textured)));
+ Sky {
+ geometry,
+ basic,
+ textured,
+ }
+ });
+ let sky = sky.filter(|s| s.basic.is_some() || s.textured.is_some());
+ let programs = [
+ post.entities,
+ post.block,
+ post.entities_translucent,
+ post.particles,
+ ];
+ let late = [false, false, post.translucent_late, post.particles_late];
+ let entity_vertices = programs
+ .iter()
+ .any(Option::is_some)
+ .then(|| PackEntities::new(device, &draws.layout));
+ let entity_buffers = super::pack_entities::buffers();
+ let entity_geometry = entity_vertices.as_ref().map(|e| Geometry {
+ buffers: &entity_buffers,
+ table: Some(&e.table),
+ depth_write: true,
+ atlas: world.celestial.clone(),
+ ids: None,
+ cull: false,
+ });
+ let entity_passes = programs.map(|p| {
+ p.zip(entity_geometry.as_ref())
+ .and_then(|(p, g)| build(p, Some(g)))
+ });
+ let entities = entity_vertices
+ .filter(|_| entity_passes.iter().any(Option::is_some))
+ .map(|geometry| Entities {
+ geometry,
+ passes: entity_passes,
+ late,
});
+ let cloud_geometry = world.clouds.map(|layout| Geometry {
+ buffers: &entity_buffers,
+ table: Some(layout),
+ depth_write: true,
+ atlas: world.celestial.clone(),
+ ids: None,
+ cull: false,
+ });
+ let hides_clouds = post.clouds.is_some() && cloud_geometry.is_some();
+ let weather_geometry = world.weather.then(|| Geometry {
+ buffers: &entity_buffers,
+ table: None,
+ depth_write: true,
+ atlas: None,
+ ids: None,
+ cull: false,
+ });
+ let hand_geometry = world.hand.then(|| super::pack_hand::PackHand::new(device));
+ let hand = post.hand.zip(hand_geometry).and_then(|(p, geometry)| {
+ let g = Geometry {
+ buffers: &entity_buffers,
+ table: Some(&geometry.layout),
+ depth_write: true,
+ atlas: world.celestial.clone(),
+ ids: None,
+ cull: false,
+ };
+ build(p, Some(&g)).map(|pass| (pass, geometry))
+ });
+ let weather = post
+ .weather
+ .zip(weather_geometry.as_ref())
+ .and_then(|(p, g)| build(p, Some(g)))
+ .map(|rain| {
+ let mut snow = rain.clone();
+ snow.name = format!("{} (snow)", rain.name);
+ [rain, snow]
+ });
+ let clouds = post
+ .clouds
+ .zip(cloud_geometry.as_ref())
+ .and_then(|(p, g)| build(p, Some(g)));
+ // Developers' aid: `HORIZON_SHADER_PACK_SKIP=composite,final` leaves programs out.
+ let skip = std::env::var("HORIZON_SHADER_PACK_SKIP").unwrap_or_default();
+ let passes: Vec = post
+ .programs
+ .into_iter()
+ .filter(|p| !skip.split(',').any(|s| s == p.name))
+ .filter_map(|p| build(p, None))
+ .collect();
+ if let Some(t) = &terrain {
+ notes.push(format!("terrain draws with {}", t.name));
}
+ if let Some(w) = &water {
+ notes.push(format!("translucent terrain draws with {}", w.name));
+ }
+ for (part, pass) in sky
+ .iter()
+ .flat_map(|s| [("sky", &s.basic), ("sun and moon", &s.textured)])
+ {
+ if let Some(p) = pass {
+ notes.push(format!("the {part} draw with {}", p.name));
+ }
+ }
+ let parts = [
+ "entities",
+ "block entities",
+ "translucent entities",
+ "particles",
+ ];
+ if let Some(c) = &clouds {
+ notes.push(format!("clouds draw with {}", c.name));
+ }
+ if let Some((h, _)) = &hand {
+ notes.push(format!("the hand draws with {}", h.name));
+ }
+ if let Some([w, _]) = &weather {
+ notes.push(format!("rain and snow draw with {}", w.name));
+ }
+ for (part, pass) in entities.iter().flat_map(|e| parts.iter().zip(&e.passes)) {
+ if let Some(p) = pass {
+ notes.push(format!("{part} draw with {}", p.name));
+ }
+ }
+ let depth1 = water.is_some()
+ && passes
+ .iter()
+ .filter(|p| matches!(p.group, Group::Composite | Group::Final))
+ .chain(&water)
+ .chain(weather.iter().take(1).flatten())
+ .any(|p| p.samplers.iter().any(|s| s.source == Source::Depth1));
// A clear only matters when a program reads the buffer before one writes all of it
- // (composite passes cover their whole target); the others are skipped.
+ // (composite passes cover their whole target, terrain does not); the others are skipped.
let mut cleared = Vec::new();
for b in 1..BUFFERS {
let bit = 1u16 << b;
- let first = passes
+ let touched = |p: &&Pass| (p.reads | p.flip | p.written) & bit != 0;
+ // Gbuffers passes cover part of their targets: a clear is needed if they touch it.
+ let first = sky
.iter()
- .find(|p| (p.reads | p.flip | p.written) & bit != 0);
- if clears[b].is_some()
- && first.is_some_and(|p| p.reads & bit != 0 || p.written & bit == 0)
- {
+ .flat_map(|s| s.basic.iter().chain(&s.textured))
+ .chain(&terrain)
+ .chain(entities.iter().flat_map(|e| e.passes.iter().flatten()))
+ .chain(&clouds)
+ .chain(&water)
+ .chain(weather.iter().take(1).flatten())
+ .chain(hand.iter().map(|h| &h.0))
+ .find(touched)
+ .map(|_| true)
+ .or_else(|| {
+ let p = passes.iter().find(touched)?;
+ Some(p.reads & bit != 0 || p.written & bit == 0)
+ });
+ if clears[b].is_some() && first == Some(true) {
cleared.push(b);
}
}
- let blit_depth = passes
- .iter()
- .any(|p| p.samplers.iter().any(|s| s.source == Source::Depth));
- let room = 8 - 1 - usize::from(blit_depth);
+ // Terrain tests against the world's depth (entities...), so the blit copies it then.
+ let blit_depth = terrain.is_some()
+ || water.is_some()
+ || sky.is_some()
+ || entities.is_some()
+ || clouds.is_some()
+ || weather.is_some()
+ || hand.is_some()
+ || passes.iter().any(|p| {
+ p.samplers
+ .iter()
+ .any(|s| matches!(s.source, Source::Depth | Source::Depth1))
+ });
+ let room = 8 - 1;
let other_clears = cleared.split_off(room.min(cleared.len()));
let float = wgpu::TextureSampleType::Float { filterable: true };
- let mut blit_targets = vec![target(formats[0])];
- if blit_depth {
- blit_targets.push(target(wgpu::TextureFormat::R32Float));
- }
+ let blit_targets = std::iter::once(target(formats[0]));
// Cleared by the pass's load operation; the shader writes nothing there.
- blit_targets.extend(cleared.iter().map(|&b| {
- Some(wgpu::ColorTargetState {
- format: formats[b],
- blend: None,
- write_mask: wgpu::ColorWrites::empty(),
- })
- }));
+ let blit_targets: Vec<_> = blit_targets
+ .chain(cleared.iter().map(|&b| {
+ Some(wgpu::ColorTargetState {
+ format: formats[b],
+ blend: None,
+ write_mask: wgpu::ColorWrites::empty(),
+ })
+ }))
+ .collect();
let blit = helper(
device,
"vs_full",
@@ -541,6 +1143,14 @@ impl PackPost {
texture_entry(1, wgpu::TextureSampleType::Depth),
],
&blit_targets,
+ // GL's window depth, written for depthtex0 and the terrain's depth test.
+ blit_depth.then_some(wgpu::DepthStencilState {
+ format: wgpu::TextureFormat::Depth32Float,
+ depth_write_enabled: Some(true),
+ depth_compare: Some(wgpu::CompareFunction::Always),
+ stencil: Default::default(),
+ bias: Default::default(),
+ }),
);
let copy = helper(
device,
@@ -554,11 +1164,67 @@ impl PackPost {
),
],
&[target(screen)],
+ None,
);
+ let filterable = |b: usize| {
+ formats[b].sample_type(None, Some(device.features()))
+ == Some(wgpu::TextureSampleType::Float { filterable: true })
+ };
+ let asked = passes.iter().fold(0u16, |m, p| m | p.mipmaps);
+ let mipped = (0..BUFFERS)
+ .filter(|&b| asked & 1 << b != 0 && filterable(b))
+ .fold(0u16, |m, b| m | 1 << b);
+ if asked & !mipped != 0 {
+ notes.push(format!(
+ "mipmaps of unfilterable buffers are not generated (buffers {:#b})",
+ asked & !mipped
+ ));
+ }
+ let mut down = HashMap::new();
+ for b in (0..BUFFERS).filter(|b| mipped & 1 << b != 0) {
+ down.entry(formats[b]).or_insert_with(|| {
+ helper(
+ device,
+ "vs_full",
+ "fs_down",
+ &[
+ texture_entry(0, float),
+ entry(
+ 2,
+ wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
+ ),
+ ],
+ &[target(formats[b])],
+ None,
+ )
+ });
+ }
let white = [255u8; 4];
Self {
notes,
passes,
+ terrain,
+ water,
+ sky,
+ entities,
+ clouds,
+ cloud_draw: None,
+ hides_clouds,
+ weather,
+ weather_draw: None,
+ weather_generation: 0,
+ hand,
+ depth1,
+ mipped,
+ mips_valid: 0,
+ down,
+ draws,
+ block_ids,
+ page_groups: HashMap::new(),
+ timing: super::pack_timing::PassTiming::new(device, queue),
+ custom,
+ atlas: world.atlas.as_ref().map(|a| a.0.clone()),
+ lightmap: world.lightmap,
deferred_pre: mask(&post.deferred_pre),
composite_pre: mask(&post.composite_pre),
formats,
@@ -608,12 +1274,560 @@ impl PackPost {
wgpu::AddressMode::ClampToEdge,
),
repeat: sampler(device, wgpu::FilterMode::Linear, wgpu::AddressMode::Repeat),
+ nearest_repeat: sampler(device, wgpu::FilterMode::Nearest, wgpu::AddressMode::Repeat),
blit,
copy,
- uniforms: FrameUniforms::default(),
+ uniforms: {
+ let mut u = FrameUniforms::default();
+ u.atlas = world.atlas.map_or([0; 2], |a| a.1);
+ u.shadow = shadow_config;
+ u
+ },
}
}
+ /// Writes `pass`'s uniforms for a target of `out` pixels; its bind group for the buffers'
+ /// current mapping.
+ fn bind(
+ &self,
+ device: &wgpu::Device,
+ queue: &wgpu::Queue,
+ pass: &mut Pass,
+ out: [u32; 2],
+ ) -> wgpu::BindGroup {
+ let fog = self.uniforms.fog();
+ // Our block members first (`hz_target`, `hz_fog_color`, `hz_fog`), then the pack's.
+ let ours = [
+ [
+ out[0] as f32,
+ out[1] as f32,
+ 1.0 / out[0] as f32,
+ 1.0 / out[1] as f32,
+ ],
+ fog[0].to_array(),
+ fog[1].to_array(),
+ ];
+ let head: &[u8] = bytemuck::cast_slice(&ours);
+ let n = head.len().min(pass.block.len());
+ pass.block[..n].copy_from_slice(&head[..n]);
+ for (index, ty, offset) in &pass.uniforms {
+ if let Some(v) = index.and_then(|i| self.uniforms.values.get(i)) {
+ v.write(ty, &mut pass.block, *offset as usize);
+ }
+ }
+ if !pass.block.is_empty() {
+ queue.write_buffer(&pass.ubo, 0, &pass.block);
+ }
+ let targets = self.targets.as_ref().expect("resized");
+ // A chain is sampled only while it is current: a buffer written since its last chain
+ // reads its first level, never stale levels (6.1.7).
+ let current = self.mips_valid & self.mipped & pass.reads;
+ let key = u32::from(self.main & pass.reads) | u32::from(current) << 16;
+ let group = pass.groups.entry(key).or_insert_with(|| {
+ let mut entries = vec![wgpu::BindGroupEntry {
+ binding: 0,
+ resource: pass.ubo.as_entire_binding(),
+ }];
+ for s in &pass.samplers {
+ let texture = match s.source {
+ Source::Color(i) => {
+ let side = usize::from(key & 1 << i != 0);
+ match (&targets.mips[i], &targets.color[i]) {
+ (Some(m), _) if current & 1 << i != 0 => &m[side].all,
+ (_, Some(t)) => &t[side],
+ (_, None) => &self.dummy,
+ }
+ }
+ Source::Depth => &targets.depth,
+ // Before translucent terrain the two are the same.
+ Source::Depth1 => match &targets.depth1 {
+ Some(d)
+ if matches!(
+ pass.group,
+ Group::Composite | Group::Final | Group::Translucent
+ ) =>
+ {
+ &d.1
+ }
+ _ => &targets.depth,
+ },
+ Source::Noise => &self.noise,
+ Source::Dummy => &self.dummy,
+ Source::Shadow => &self.shadow.0,
+ Source::Atlas => pass
+ .atlas
+ .as_ref()
+ .or(self.atlas.as_ref())
+ .unwrap_or(&self.dummy),
+ Source::Lightmap => &self.lightmap,
+ };
+ let sampler = match SamplerKind::of(s.source, s.filtering) {
+ SamplerKind::Linear => &self.linear,
+ SamplerKind::Nearest => &self.nearest,
+ SamplerKind::Repeat => &self.repeat,
+ SamplerKind::NearestRepeat => &self.nearest_repeat,
+ SamplerKind::Comparison => &self.shadow.1,
+ };
+ entries.push(wgpu::BindGroupEntry {
+ binding: s.binding,
+ resource: wgpu::BindingResource::TextureView(texture),
+ });
+ if s.sampler == s.binding + 1 {
+ entries.push(wgpu::BindGroupEntry {
+ binding: s.sampler,
+ resource: wgpu::BindingResource::Sampler(sampler),
+ });
+ }
+ }
+ device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some(&pass.name),
+ layout: &pass.layout,
+ entries: &entries,
+ })
+ });
+ group.clone()
+ }
+
+ /// A render pass into the buffers `pass` writes, in place, tested against the world's depth.
+ fn begin<'e>(
+ &mut self,
+ encoder: &'e mut wgpu::CommandEncoder,
+ pass: &Pass,
+ ) -> wgpu::RenderPass<'e> {
+ let targets = self.targets.as_ref().expect("resized");
+ let load = wgpu::Operations {
+ load: wgpu::LoadOp::Load,
+ store: wgpu::StoreOp::Store,
+ };
+ let mut attachments: Vec> = Vec::new();
+ for &(location, buffer) in &pass.writes {
+ let l = location as usize;
+ if attachments.len() <= l {
+ attachments.resize(l + 1, None);
+ }
+ attachments[l] = targets.color[buffer]
+ .as_ref()
+ .and_then(|t| attach(&t[usize::from(self.main & 1 << buffer != 0)], load));
+ }
+ let timestamp_writes = self.timing.as_mut().and_then(|t| t.writes(&pass.name));
+ encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
+ label: Some(&pass.name),
+ color_attachments: &attachments,
+ timestamp_writes,
+ depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
+ view: &targets.depth,
+ depth_ops: Some(wgpu::Operations {
+ load: wgpu::LoadOp::Load,
+ store: wgpu::StoreOp::Store,
+ }),
+ stencil_ops: None,
+ }),
+ ..Default::default()
+ })
+ }
+
+ /// Terrain (or, `translucent`, translucent terrain) with the pack's program; `draw` issues
+ /// the game's draws.
+ fn draw_terrain(
+ &mut self,
+ device: &wgpu::Device,
+ queue: &wgpu::Queue,
+ encoder: &mut wgpu::CommandEncoder,
+ size: [u32; 2],
+ translucent: bool,
+ draw: &mut TerrainDraw<'_>,
+ ) {
+ let slot = if translucent {
+ &mut self.water
+ } else {
+ &mut self.terrain
+ };
+ let Some(mut pass) = slot.take() else {
+ return;
+ };
+ if translucent
+ && let Some(t) = &self.targets
+ && let Some((copy, _)) = &t.depth1
+ {
+ encoder.copy_texture_to_texture(
+ t.depth_texture.as_image_copy(),
+ copy.as_image_copy(),
+ t.depth_texture.size(),
+ );
+ }
+ let group = self.bind(device, queue, &mut pass, size);
+ {
+ let mut rp = self.begin(encoder, &pass);
+ rp.set_pipeline(&pass.pipeline);
+ rp.set_bind_group(0, &group, &[]);
+ if pass.draw {
+ let stage = if translucent {
+ "TERRAIN_TRANSLUCENT"
+ } else {
+ "TERRAIN_SOLID"
+ };
+ self.draws.bind(&mut rp, stage);
+ }
+ let (groups, ids) = (&self.page_groups, pass.ids);
+ let page = |rp: &mut wgpu::RenderPass<'_>, page: u32| {
+ if let Some((_, group)) = groups.get(&page).filter(|_| ids) {
+ rp.set_bind_group(3, group, &[]);
+ }
+ };
+ draw(&mut rp, translucent, &page);
+ }
+ self.mips_valid &= !pass.written;
+ if translucent {
+ self.water = Some(pass);
+ } else {
+ self.terrain = Some(pass);
+ }
+ }
+
+ /// The sky's parts in vanilla's order, each with the program that draws it; one render pass
+ /// while consecutive programs write the same buffers.
+ fn draw_sky(
+ &mut self,
+ device: &wgpu::Device,
+ queue: &wgpu::Queue,
+ encoder: &mut wgpu::CommandEncoder,
+ size: [u32; 2],
+ frame: &super::Frame,
+ ) {
+ let Some(mut sky) = self.sky.take() else {
+ return;
+ };
+ let parts = sky
+ .geometry
+ .prepare(queue, frame, self.uniforms.shadow.sun_path_rotation);
+ let mut groups = [None, None];
+ for (i, pass) in [&mut sky.basic, &mut sky.textured].into_iter().enumerate() {
+ if let Some(pass) = pass {
+ groups[i] = Some(self.bind(device, queue, pass, size));
+ self.mips_valid &= !pass.written;
+ }
+ }
+ let passes = [&sky.basic, &sky.textured];
+ let mut open: Option<(wgpu::RenderPass<'_>, usize)> = None;
+ for part in parts {
+ let i = usize::from(part.textured);
+ let (Some(pass), Some(group)) = (passes[i], &groups[i]) else {
+ continue;
+ };
+ let current = open.as_ref().map(|o| o.1);
+ if current != Some(i) {
+ let same = current
+ .and_then(|c| passes[c].as_ref())
+ .is_some_and(|p| p.writes == pass.writes);
+ if !same {
+ drop(open.take());
+ open = Some((self.begin(encoder, pass).forget_lifetime(), i));
+ }
+ let rp = &mut open.as_mut().expect("open").0;
+ rp.set_pipeline(&pass.pipeline);
+ rp.set_bind_group(0, group, &[]);
+ sky.geometry.bind(rp, part.textured);
+ open.as_mut().expect("open").1 = i;
+ }
+ let rp = &mut open.as_mut().expect("open").0;
+ if pass.draw {
+ self.draws.bind(rp, part.stage);
+ }
+ rp.draw(part.range, 0..1);
+ }
+ drop(open);
+ self.sky = Some(sky);
+ }
+
+ /// Entities, block entities, translucent entity parts and particles with the pack's
+ /// programs, those drawn before deferred or (`late`) after: each run `prepare_entities` kept
+ /// is one draw with its own `entityId` or `blockEntityId` and `entityColor`.
+ fn draw_entities(
+ &mut self,
+ device: &wgpu::Device,
+ queue: &wgpu::Queue,
+ encoder: &mut wgpu::CommandEncoder,
+ size: [u32; 2],
+ late: bool,
+ ) {
+ let Some(mut entities) = self.entities.take() else {
+ return;
+ };
+ let geometry = &entities.geometry;
+ for (program, pass) in entities.passes.iter_mut().enumerate() {
+ let Some(pass) = pass.as_mut().filter(|_| entities.late[program] == late) else {
+ continue;
+ };
+ let mut draws = geometry
+ .draws
+ .iter()
+ .filter(|d| d.program == program)
+ .peekable();
+ if draws.peek().is_none() {
+ continue;
+ }
+ let group = self.bind(device, queue, pass, size);
+ {
+ let mut rp = self.begin(encoder, pass);
+ rp.set_pipeline(&pass.pipeline);
+ rp.set_bind_group(0, &group, &[]);
+ rp.set_bind_group(1, &geometry.table_group, &[]);
+ rp.set_vertex_buffer(0, geometry.vertices());
+ rp.set_index_buffer(geometry.indices.slice(..), wgpu::IndexFormat::Uint32);
+ for d in draws {
+ if pass.draw {
+ rp.set_bind_group(2, &geometry.group, &[geometry.offset(d.slot)]);
+ }
+ rp.draw_indexed(0..d.quads * 6, d.base as i32, 0..1);
+ }
+ }
+ self.mips_valid &= !pass.written;
+ }
+ self.entities = Some(entities);
+ }
+
+ /// Splits `entities` (`Frame::entities`) between the pack and Horizon by its runs
+ /// (`spans`): the pack's part is uploaded for this frame's draw, Horizon's goes to `rest`.
+ pub fn prepare_entities(
+ &mut self,
+ device: &wgpu::Device,
+ queue: &wgpu::Queue,
+ entities: &[super::Vertex],
+ spans: &[super::pack_draw::EntitySpan],
+ rest: &mut Vec,
+ ) {
+ let Some(e) = &mut self.entities else {
+ rest.clear();
+ rest.extend_from_slice(entities);
+ return;
+ };
+ let drawn = e.passes.each_ref().map(Option::is_some);
+ e.geometry.prepare(
+ device,
+ queue,
+ &self.draws.layout,
+ entities,
+ spans,
+ drawn,
+ rest,
+ );
+ }
+
+ /// Whether the pack draws entities or block entities itself.
+ pub fn has_entities(&self) -> bool {
+ self.entities.is_some()
+ }
+
+ /// The sky, when it shows, then terrain, entities and block entities.
+ fn draw_opaque(
+ &mut self,
+ device: &wgpu::Device,
+ queue: &wgpu::Queue,
+ encoder: &mut wgpu::CommandEncoder,
+ size: [u32; 2],
+ frame: &super::Frame,
+ terrain: &mut TerrainDraw<'_>,
+ ) {
+ if super::sky_visible(frame) {
+ self.draw_sky(device, queue, encoder, size, frame);
+ }
+ self.draw_terrain(device, queue, encoder, size, false, terrain);
+ self.draw_entities(device, queue, encoder, size, false);
+ self.draw_clouds(device, queue, encoder, size);
+ self.draw_hand(device, queue, encoder, size, frame);
+ }
+
+ /// The first-person hand and held items, with `gbuffers_hand` at the hand's projection.
+ fn draw_hand(
+ &mut self,
+ device: &wgpu::Device,
+ queue: &wgpu::Queue,
+ encoder: &mut wgpu::CommandEncoder,
+ size: [u32; 2],
+ frame: &super::Frame,
+ ) {
+ let Some((mut pass, mut geometry)) = self.hand.take() else {
+ return;
+ };
+ let projection = super::pack_hand::projection(
+ frame.hand_fov_degrees,
+ size[0] as f32 / size[1].max(1) as f32,
+ frame.depth_far,
+ frame.bob,
+ );
+ geometry.prepare(device, queue, frame.hand, projection);
+ if geometry.quads > 0 {
+ let group = self.bind(device, queue, &mut pass, size);
+ {
+ let mut rp = self.begin(encoder, &pass);
+ rp.set_pipeline(&pass.pipeline);
+ rp.set_bind_group(0, &group, &[]);
+ rp.set_bind_group(1, &geometry.group, &[]);
+ if pass.draw {
+ self.draws.bind(&mut rp, "HAND_SOLID");
+ }
+ rp.set_vertex_buffer(0, geometry.vertices());
+ rp.set_index_buffer(geometry.indices.slice(..), wgpu::IndexFormat::Uint32);
+ rp.draw_indexed(0..geometry.quads * 6, 0, 0..1);
+ }
+ self.mips_valid &= !pass.written;
+ }
+ self.hand = Some((pass, geometry));
+ }
+
+ /// Whether the pack draws the first-person hand itself.
+ pub fn has_hand(&self) -> bool {
+ self.hand.is_some()
+ }
+
+ /// The clouds `set_clouds` gave for this frame, with `gbuffers_clouds`.
+ fn draw_clouds(
+ &mut self,
+ device: &wgpu::Device,
+ queue: &wgpu::Queue,
+ encoder: &mut wgpu::CommandEncoder,
+ size: [u32; 2],
+ ) {
+ let (Some(mut pass), Some(clouds)) = (self.clouds.take(), self.cloud_draw.take()) else {
+ return;
+ };
+ let group = self.bind(device, queue, &mut pass, size);
+ {
+ let mut rp = self.begin(encoder, &pass);
+ rp.set_pipeline(&pass.pipeline);
+ rp.set_bind_group(0, &group, &[]);
+ rp.set_bind_group(1, &clouds.group, &[]);
+ if pass.draw {
+ self.draws.bind(&mut rp, "CLOUDS");
+ }
+ rp.set_vertex_buffer(0, clouds.vertices.slice(..));
+ rp.set_index_buffer(clouds.indices.slice(..), wgpu::IndexFormat::Uint32);
+ rp.draw_indexed(0..clouds.quads * 6, 0, 0..1);
+ }
+ self.mips_valid &= !pass.written;
+ self.clouds = Some(pass);
+ }
+
+ /// Whether the pack draws the clouds itself (or hides them).
+ pub fn has_clouds(&self) -> bool {
+ self.hides_clouds
+ }
+
+ /// The clouds to draw this frame (`None`: none).
+ pub fn set_clouds(&mut self, clouds: Option) {
+ self.cloud_draw = clouds.filter(|c| c.quads > 0);
+ }
+
+ /// Translucent terrain, then the entity parts drawn after deferred.
+ fn draw_translucent(
+ &mut self,
+ device: &wgpu::Device,
+ queue: &wgpu::Queue,
+ encoder: &mut wgpu::CommandEncoder,
+ size: [u32; 2],
+ terrain: &mut TerrainDraw<'_>,
+ ) {
+ self.draw_terrain(device, queue, encoder, size, true, terrain);
+ self.draw_entities(device, queue, encoder, size, true);
+ self.draw_weather(device, queue, encoder, size);
+ }
+
+ /// The rain, then the snow, `set_weather` gave for this frame, with `gbuffers_weather`.
+ fn draw_weather(
+ &mut self,
+ device: &wgpu::Device,
+ queue: &wgpu::Queue,
+ encoder: &mut wgpu::CommandEncoder,
+ size: [u32; 2],
+ ) {
+ let (Some(mut passes), Some(weather)) = (self.weather.take(), self.weather_draw.take())
+ else {
+ return;
+ };
+ if weather.generation != self.weather_generation {
+ self.weather_generation = weather.generation;
+ for (pass, sprite) in passes.iter_mut().zip(&weather.sprites) {
+ pass.atlas = sprite.clone();
+ pass.groups.clear();
+ }
+ }
+ for (pass, quads) in passes.iter_mut().zip(weather.quads.clone()) {
+ if quads.is_empty() {
+ continue;
+ }
+ let group = self.bind(device, queue, pass, size);
+ {
+ let mut rp = self.begin(encoder, pass);
+ rp.set_pipeline(&pass.pipeline);
+ rp.set_bind_group(0, &group, &[]);
+ if pass.draw {
+ self.draws.bind(&mut rp, "RAIN_SNOW");
+ }
+ rp.set_vertex_buffer(0, weather.vertices.slice(..));
+ rp.set_index_buffer(weather.indices.slice(..), wgpu::IndexFormat::Uint32);
+ rp.draw_indexed(quads.start * 6..quads.end * 6, 0, 0..1);
+ }
+ self.mips_valid &= !pass.written;
+ }
+ self.weather = Some(passes);
+ }
+
+ /// Whether the pack draws rain and snow itself.
+ pub fn has_weather(&self) -> bool {
+ self.weather.is_some()
+ }
+
+ /// The rain and snow to draw this frame (`None`: none).
+ pub fn set_weather(&mut self, weather: Option) {
+ self.weather_draw = weather;
+ }
+
+ /// Regenerates the mip chain of buffer `b`'s side that programs read.
+ fn mipmap(&self, encoder: &mut wgpu::CommandEncoder, b: usize) {
+ let (Some(targets), Some((pipeline, _))) = (&self.targets, self.down.get(&self.formats[b]))
+ else {
+ return;
+ };
+ let Some(mips) = &targets.mips[b] else { return };
+ let side = &mips[usize::from(self.main & 1 << b != 0)];
+ for (attachment, group) in &side.levels {
+ let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
+ label: Some("Shader pack mipmap"),
+ color_attachments: &[attach(
+ attachment,
+ wgpu::Operations {
+ load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
+ store: wgpu::StoreOp::Store,
+ },
+ )],
+ ..Default::default()
+ });
+ rp.set_pipeline(pipeline);
+ rp.set_bind_group(0, group, &[]);
+ rp.draw(0..3, 0..1);
+ }
+ }
+
+ /// Whether the pack draws terrain itself.
+ pub fn has_terrain(&self) -> bool {
+ self.terrain.is_some()
+ }
+
+ /// Whether the pack draws the sky (disc, sunset, stars, void) itself.
+ pub fn has_sky(&self) -> bool {
+ self.sky.as_ref().is_some_and(|s| s.basic.is_some())
+ }
+
+ /// Whether the pack draws the sun and moon itself.
+ pub fn has_celestial(&self) -> bool {
+ self.sky.as_ref().is_some_and(|s| s.textured.is_some())
+ }
+
+ /// Whether the pack draws translucent terrain itself.
+ pub fn has_water(&self) -> bool {
+ self.water.is_some()
+ }
+
/// The names of the programs that run, in order.
pub fn programs(&self) -> Vec<&str> {
self.passes.iter().map(|p| p.name.as_str()).collect()
@@ -623,37 +1837,95 @@ impl PackPost {
if self.targets.as_ref().is_some_and(|t| t.size == size) {
return;
}
- let make = |label: &str, format| {
- device
- .create_texture(&wgpu::TextureDescriptor {
- label: Some(label),
- size: wgpu::Extent3d {
- width: size[0],
- height: size[1],
- depth_or_array_layers: 1,
- },
- mip_level_count: 1,
- sample_count: 1,
- dimension: wgpu::TextureDimension::D2,
- format,
- usage: wgpu::TextureUsages::RENDER_ATTACHMENT
- | wgpu::TextureUsages::TEXTURE_BINDING,
- view_formats: &[],
- })
- .create_view(&Default::default())
+ let full_chain = 32 - size[0].max(size[1]).max(1).leading_zeros();
+ let texture_levels = |label: &str, format, usage, levels| {
+ device.create_texture(&wgpu::TextureDescriptor {
+ label: Some(label),
+ size: wgpu::Extent3d {
+ width: size[0],
+ height: size[1],
+ depth_or_array_layers: 1,
+ },
+ mip_level_count: levels,
+ sample_count: 1,
+ dimension: wgpu::TextureDimension::D2,
+ format,
+ usage: usage | wgpu::TextureUsages::TEXTURE_BINDING,
+ view_formats: &[],
+ })
};
+ let texture = |label: &str, format, usage| texture_levels(label, format, usage, 1);
+ let level = |t: &wgpu::Texture, level: u32| {
+ t.create_view(&wgpu::TextureViewDescriptor {
+ base_mip_level: level,
+ mip_level_count: Some(1),
+ ..Default::default()
+ })
+ };
+ let mut mips: [Option<[Mips; 2]>; BUFFERS] = Default::default();
let color = std::array::from_fn(|i| {
(self.used & 1 << i != 0).then(|| {
let label = format!("colortex{i}");
- [make(&label, self.formats[i]), make(&label, self.formats[i])]
+ let mipped = self.mipped & 1 << i != 0;
+ let levels = if mipped { full_chain } else { 1 };
+ let attachment = wgpu::TextureUsages::RENDER_ATTACHMENT;
+ let sides =
+ [0, 1].map(|_| texture_levels(&label, self.formats[i], attachment, levels));
+ if mipped && let Some((_, layout)) = self.down.get(&self.formats[i]) {
+ mips[i] = Some(sides.each_ref().map(|t| {
+ Mips {
+ all: t.create_view(&Default::default()),
+ levels: (1..levels)
+ .map(|l| {
+ let group =
+ device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some(&label),
+ layout,
+ entries: &[
+ wgpu::BindGroupEntry {
+ binding: 0,
+ resource: wgpu::BindingResource::TextureView(
+ &level(t, l - 1),
+ ),
+ },
+ wgpu::BindGroupEntry {
+ binding: 2,
+ resource: wgpu::BindingResource::Sampler(
+ &self.linear,
+ ),
+ },
+ ],
+ });
+ (level(t, l), group)
+ })
+ .collect(),
+ }
+ }));
+ }
+ sides.map(|t| level(&t, 0))
})
});
+ let depth32 = wgpu::TextureFormat::Depth32Float;
+ let depth_texture = texture(
+ "depthtex0",
+ depth32,
+ wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
+ );
+ let depth1 = self.depth1.then(|| {
+ let t = texture("depthtex1", depth32, wgpu::TextureUsages::COPY_DST);
+ let view = t.create_view(&Default::default());
+ (t, view)
+ });
self.targets = Some(Targets {
size,
color,
- depth: make("depthtex0", wgpu::TextureFormat::R32Float),
+ depth: depth_texture.create_view(&Default::default()),
+ depth_texture,
+ depth1,
+ mips,
});
- for pass in &mut self.passes {
+ let gbuffers = self.terrain.iter_mut().chain(&mut self.water);
+ for pass in self.passes.iter_mut().chain(gbuffers) {
pass.groups.clear();
}
}
@@ -673,9 +1945,39 @@ impl PackPost {
inputs: &PackInputs,
size: [u32; 2],
window: [u32; 2],
+ pages: &[(u32, wgpu::Buffer)],
+ terrain: &mut TerrainDraw<'_>,
) {
self.resize(device, size);
+ if let Some((layout, ids)) = &self.block_ids {
+ // A page that grew has a new buffer.
+ self.page_groups
+ .retain(|p, (b, _)| pages.iter().any(|(q, c)| q == p && c == b));
+ for (page, buffer) in pages {
+ self.page_groups.entry(*page).or_insert_with(|| {
+ let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("Shader pack terrain"),
+ layout,
+ entries: &[
+ wgpu::BindGroupEntry {
+ binding: 0,
+ resource: ids.as_entire_binding(),
+ },
+ wgpu::BindGroupEntry {
+ binding: 1,
+ resource: buffer.as_entire_binding(),
+ },
+ ],
+ });
+ (buffer.clone(), group)
+ });
+ }
+ }
+ if let Some(t) = &mut self.timing {
+ t.begin(device, queue);
+ }
self.uniforms.update(frame, inputs, size);
+ self.uniforms.eval_custom(&mut self.custom);
let Some(targets) = &self.targets else { return };
let main = |m: u16, i: usize| usize::from(m & 1 << i != 0);
let view = |m: u16, i: usize, alt: bool| {
@@ -724,6 +2026,7 @@ impl PackPost {
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Shader pack clear"),
color_attachments: &attachments,
+ timestamp_writes: self.timing.as_mut().and_then(|t| t.writes("clear")),
..Default::default()
});
}
@@ -751,9 +2054,6 @@ impl PackPost {
view(self.main, 0, false).expect("colortex0"),
overwrite,
)];
- if self.blit_depth {
- attachments.push(attach(&targets.depth, overwrite));
- }
for &b in &self.blit_clears {
attachments.push(attach(
view(self.main, b, false).expect("used buffer"),
@@ -763,6 +2063,17 @@ impl PackPost {
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Shader pack world"),
color_attachments: &attachments,
+ timestamp_writes: self.timing.as_mut().and_then(|t| t.writes("world")),
+ depth_stencil_attachment: self.blit_depth.then_some(
+ wgpu::RenderPassDepthStencilAttachment {
+ view: &targets.depth,
+ depth_ops: Some(wgpu::Operations {
+ load: wgpu::LoadOp::Clear(1.0),
+ store: wgpu::StoreOp::Store,
+ }),
+ stencil_ops: None,
+ },
+ ),
..Default::default()
});
pass.set_pipeline(&self.blit.0);
@@ -770,10 +2081,32 @@ impl PackPost {
pass.draw(0..3, 0..1);
}
- let fog = self.uniforms.fog();
+ // The world and the clears just rewrote the buffers' first levels.
+ self.mips_valid = 0;
+ let mut passes = std::mem::take(&mut self.passes);
let mut seen = (false, false);
let mut shown = false;
- for pass in &mut self.passes {
+ let mut terrain_drawn = self.terrain.is_none()
+ && self.sky.is_none()
+ && self.entities.is_none()
+ && self.clouds.is_none()
+ && self.hand.is_none();
+ let late_entities = self.entities.as_ref().is_some_and(|e| {
+ e.passes
+ .iter()
+ .zip(e.late)
+ .any(|(p, late)| p.is_some() && late)
+ });
+ let mut water_drawn = self.water.is_none() && self.weather.is_none() && !late_entities;
+ for pass in &mut passes {
+ if !terrain_drawn && pass.group != Group::Prepare {
+ terrain_drawn = true;
+ self.draw_opaque(device, queue, encoder, size, frame, terrain);
+ }
+ if !water_drawn && matches!(pass.group, Group::Composite | Group::Final) {
+ water_drawn = true;
+ self.draw_translucent(device, queue, encoder, size, terrain);
+ }
if pass.group == Group::Deferred && !seen.0 {
seen.0 = true;
self.main ^= self.deferred_pre;
@@ -782,64 +2115,19 @@ impl PackPost {
seen.1 = true;
self.main ^= self.composite_pre;
}
+ let stale = pass.mipmaps & self.mipped & !self.mips_valid;
+ for b in (0..BUFFERS).filter(|b| stale & 1 << b != 0) {
+ self.mipmap(encoder, b);
+ }
+ self.mips_valid |= stale;
let final_pass = pass.group == Group::Final;
- let out = if final_pass { window } else { size };
- // Our block members first (`hz_target`, `hz_fog_color`, `hz_fog`), then the pack's.
- let ours = [
- [
- out[0] as f32,
- out[1] as f32,
- 1.0 / out[0] as f32,
- 1.0 / out[1] as f32,
- ],
- fog[0].to_array(),
- fog[1].to_array(),
- ];
- let head: &[u8] = bytemuck::cast_slice(&ours);
- let n = head.len().min(pass.block.len());
- pass.block[..n].copy_from_slice(&head[..n]);
- for (index, ty, offset) in &pass.uniforms {
- if let Some(v) = index.and_then(|i| self.uniforms.values.get(i)) {
- v.write(ty, &mut pass.block, *offset as usize);
- }
- }
- if !pass.block.is_empty() {
- queue.write_buffer(&pass.ubo, 0, &pass.block);
- }
- let key = self.main & pass.reads;
- let group = pass.groups.entry(key).or_insert_with(|| {
- let mut entries = vec![wgpu::BindGroupEntry {
- binding: 0,
- resource: pass.ubo.as_entire_binding(),
- }];
- for s in &pass.samplers {
- let filter = if s.filtering {
- &self.linear
- } else {
- &self.nearest
- };
- let (texture, sampler) = match s.source {
- Source::Color(i) => (view(key, i, false).unwrap_or(&self.dummy), filter),
- Source::Depth => (&targets.depth, &self.nearest),
- Source::Noise => (&self.noise, &self.repeat),
- Source::Dummy => (&self.dummy, &self.linear),
- Source::Shadow => (&self.shadow.0, &self.shadow.1),
- };
- entries.push(wgpu::BindGroupEntry {
- binding: s.binding,
- resource: wgpu::BindingResource::TextureView(texture),
- });
- entries.push(wgpu::BindGroupEntry {
- binding: s.binding + 1,
- resource: wgpu::BindingResource::Sampler(sampler),
- });
- }
- device.create_bind_group(&wgpu::BindGroupDescriptor {
- label: Some(&pass.name),
- layout: &pass.layout,
- entries: &entries,
- })
- });
+ let group = self.bind(device, queue, pass, if final_pass { window } else { size });
+ let targets = self.targets.as_ref().expect("resized");
+ let view = |i: usize| {
+ targets.color[i]
+ .as_ref()
+ .map(|t| &t[usize::from(self.main & 1 << i == 0)])
+ };
let mut attachments: Vec> = Vec::new();
if final_pass {
attachments.push(attach(screen, overwrite));
@@ -850,22 +2138,36 @@ impl PackPost {
if attachments.len() <= l {
attachments.resize(l + 1, None);
}
- attachments[l] =
- view(self.main, buffer, true).and_then(|v| attach(v, overwrite));
+ attachments[l] = view(buffer).and_then(|v| attach(v, overwrite));
}
}
{
let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some(&pass.name),
color_attachments: &attachments,
+ timestamp_writes: self.timing.as_mut().and_then(|t| t.writes(&pass.name)),
..Default::default()
});
rp.set_pipeline(&pass.pipeline);
- rp.set_bind_group(0, &*group, &[]);
+ rp.set_bind_group(0, &group, &[]);
rp.draw(0..6, 0..1);
}
self.main ^= pass.flip;
+ self.mips_valid &= !pass.written;
}
+ self.passes = passes;
+ if !terrain_drawn {
+ self.draw_opaque(device, queue, encoder, size, frame, terrain);
+ }
+ if !water_drawn {
+ self.draw_translucent(device, queue, encoder, size, terrain);
+ }
+ let targets = self.targets.as_ref().expect("resized");
+ let view = |m: u16, i: usize, alt: bool| {
+ targets.color[i]
+ .as_ref()
+ .map(|t| &t[main(m, i) ^ usize::from(alt)])
+ };
if !shown {
let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Shader pack colortex0"),
@@ -900,6 +2202,31 @@ mod tests {
use super::*;
use wgpu::TextureFormat as T;
+ /// Two textures with a sampler each become two textures sharing one sampler variable.
+ #[test]
+ fn samplers_of_a_kind_share_one_variable() {
+ let mut module = naga::front::wgsl::parse_str(
+ "@group(0) @binding(1) var a: texture_2d;\n\
+ @group(0) @binding(2) var sa: sampler;\n\
+ @group(0) @binding(3) var b: texture_2d;\n\
+ @group(0) @binding(4) var sb: sampler;\n\
+ @fragment fn main() -> @location(0) vec4 {\n\
+ return textureSample(a, sa, vec2(0.5)) + textureSample(b, sb, vec2(0.5));\n}\n",
+ )
+ .unwrap();
+ share_samplers(&mut module, &HashMap::from([(2, 2), (4, 2)]));
+ let info = naga::valid::Validator::new(Default::default(), Default::default())
+ .validate(&module)
+ .unwrap();
+ let used: Vec = module
+ .global_variables
+ .iter()
+ .filter(|(h, _)| !info.get_entry_point(0)[*h].is_empty())
+ .filter_map(|(_, g)| g.binding.as_ref().map(|b| b.binding))
+ .collect();
+ assert_eq!(used, [1, 2, 3]);
+ }
+
#[test]
fn pack_formats_map_to_renderable_ones() {
let f = |name| texture_format(Format::parse(name).unwrap(), wgpu::Features::empty());
@@ -912,8 +2239,9 @@ mod tests {
assert_eq!(f("RGBA16"), T::Rgba16Float, "16-bit norm needs a feature");
assert_eq!(f("RGBA8_SNORM"), T::Rgba16Float, "snorm is not renderable");
assert_eq!(f("R11F_G11F_B10F"), T::Rgba16Float);
- let all =
- wgpu::Features::TEXTURE_FORMAT_16BIT_NORM | wgpu::Features::RG11B10UFLOAT_RENDERABLE;
+ let all = wgpu::Features::TEXTURE_FORMAT_16BIT_NORM
+ | wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES
+ | wgpu::Features::RG11B10UFLOAT_RENDERABLE;
let g = |name| texture_format(Format::parse(name).unwrap(), all);
assert_eq!(g("RGBA16"), T::Rgba16Unorm);
assert_eq!(g("R11F_G11F_B10F"), T::Rg11b10Ufloat);
@@ -923,6 +2251,7 @@ mod tests {
fn sampler_names() {
assert!(source("gaux1") == Source::Color(4));
assert!(source("gdepthtex") == Source::Depth);
+ assert!(source("depthtex2") == Source::Depth1);
assert!(source("noisetex") == Source::Noise);
assert!(source("shadowcolor0") == Source::Dummy);
}
diff --git a/crates/horizon-client/src/gfx/pack_post.wgsl b/crates/horizon-client/src/gfx/pack_post.wgsl
index 30345b00f..299e9207b 100644
--- a/crates/horizon-client/src/gfx/pack_post.wgsl
+++ b/crates/horizon-client/src/gfx/pack_post.wgsl
@@ -1,5 +1,6 @@
// Helpers of a shader pack's post-processing (pack_post.rs): the world into colortex0 and
-// depthtex0, and colortex0 onto the screen for packs without `final`.
+// depthtex0 (a depth attachment, for terrain's depth test), and colortex0 onto the screen for
+// packs without `final`.
@vertex
fn vs_full(@builtin(vertex_index) i: u32) -> @builtin(position) vec4 {
@@ -12,7 +13,7 @@ fn vs_full(@builtin(vertex_index) i: u32) -> @builtin(position) vec4 {
struct World {
@location(0) color: vec4,
- @location(1) depth: vec4,
+ @builtin(frag_depth) depth: f32,
}
// Pack buffers keep GL's rows (row 0 at the bottom), and GL's window depth: Horizon's
@@ -23,7 +24,7 @@ fn fs_blit(@builtin(position) p: vec4) -> World {
let at = vec2(i32(p.x), size.y - 1 - i32(p.y));
var out: World;
out.color = textureLoad(source, at, 0);
- out.depth = vec4(1.0 - textureLoad(world_depth, at, 0), 0.0, 0.0, 1.0);
+ out.depth = 1.0 - textureLoad(world_depth, at, 0);
return out;
}
@@ -52,3 +53,11 @@ fn vs_uv(@builtin(vertex_index) i: u32) -> Full {
fn fs_copy(v: Full) -> @location(0) vec4 {
return textureSample(source, linear, v.uv);
}
+
+// One level of a buffer's mip chain from the level above (6.1.7): the average of the 2×2
+// texels under each destination texel, by one bilinear sample at their shared corner.
+@fragment
+fn fs_down(@builtin(position) p: vec4) -> @location(0) vec4 {
+ let size = max(textureDimensions(source) / 2u, vec2(1u));
+ return textureSampleLevel(source, linear, p.xy / vec2(size), 0.0);
+}
diff --git a/crates/horizon-client/src/gfx/pack_sky.rs b/crates/horizon-client/src/gfx/pack_sky.rs
new file mode 100644
index 000000000..4823da4a4
--- /dev/null
+++ b/crates/horizon-client/src/gfx/pack_sky.rs
@@ -0,0 +1,323 @@
+//! The vanilla sky's geometry for a shader pack's `gbuffers_skybasic` (spec 2.3): the sky disc,
+//! the sunset fan, the stars and the void disc, as Horizon's own sky draws them (terrain.wgsl,
+//! stars.rs), built once into a vertex buffer. The frame's colours and rotations sit in a small
+//! uniform (set 1) that the vertex inputs read. `gbuffers_skytextured` draws the frame's sun and
+//! moon quads, textured from the shared atlas.
+
+use std::ops::Range;
+
+use wgpu::util::DeviceExt;
+
+/// A sky vertex: a position and what it belongs to (`SKY`...).
+#[repr(C)]
+#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
+struct Vertex {
+ pos: [f32; 3],
+ kind: u32,
+}
+
+const SKY: u32 = 0;
+const SUNSET_APEX: u32 = 1;
+/// Rim of the sunset fan: `pos.xy` is (sin, cos) of its angle.
+const SUNSET_RIM: u32 = 2;
+const STAR: u32 = 3;
+const VOID: u32 = 4;
+
+/// The frame's sky (std140).
+#[repr(C)]
+#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
+struct Uniform {
+ /// Camera-relative world from star space.
+ stars: [[f32; 4]; 4],
+ /// Sky colour, alpha 1.
+ color: [f32; 4],
+ /// Sunset colour and alpha.
+ sunrise: [f32; 4],
+ /// cos and sin of the sunset fan's turn, star brightness.
+ sun: [f32; 4],
+}
+
+/// A sun or moon vertex: position, atlas UV, RGBA8 colour.
+#[repr(C)]
+#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
+struct Celestial {
+ pos: [f32; 3],
+ uv: [f32; 2],
+ color: u32,
+}
+
+/// Quads of the sun and moon (and room to spare).
+const CELESTIAL_QUADS: usize = 4;
+
+const CELESTIAL_ATTRIBUTES: [wgpu::VertexAttribute; 3] =
+ wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x2, 2 => Unorm8x4];
+
+pub(super) const ATTRIBUTES: [wgpu::VertexAttribute; 2] =
+ wgpu::vertex_attr_array![0 => Float32x3, 1 => Uint32];
+
+/// What `gbuffers_skybasic` reads from these vertices.
+pub fn inputs() -> horizon_shaders::GbuffersInputs {
+ let declarations =
+ "layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in uint hz_v_kind;\n\
+ layout(set = 1, binding = 0) uniform HzSky { mat4 hz_sky_stars; vec4 hz_sky_color; \
+ vec4 hz_sky_sunrise; vec4 hz_sky_sun; };\n"
+ .to_owned();
+ let body = format!(
+ " vec3 hz_p = hz_v_pos;\n \
+ hz_Color = hz_sky_color;\n \
+ if (hz_v_kind == {SUNSET_APEX}u || hz_v_kind == {SUNSET_RIM}u) {{\n \
+ vec3 hz_l = vec3(0.0, 100.0, 0.0);\n \
+ hz_Color = hz_sky_sunrise;\n \
+ if (hz_v_kind == {SUNSET_RIM}u) {{\n \
+ hz_l = vec3(hz_v_pos.xy * 120.0, -hz_v_pos.y * 40.0 * hz_sky_sunrise.a);\n \
+ hz_Color.a = 0.0;\n \
+ }}\n \
+ vec3 hz_r = vec3(hz_l.x * hz_sky_sun.x - hz_l.y * hz_sky_sun.y, hz_l.x * hz_sky_sun.y + hz_l.y * hz_sky_sun.x, hz_l.z);\n \
+ hz_p = vec3(hz_r.x, -hz_r.z, hz_r.y);\n \
+ }} else if (hz_v_kind == {STAR}u) {{\n \
+ hz_p = (hz_sky_stars * vec4(hz_v_pos, 1.0)).xyz;\n \
+ hz_Color = vec4(hz_sky_sun.z);\n \
+ }} else if (hz_v_kind == {VOID}u) {{\n \
+ hz_Color = vec4(0.0, 0.0, 0.0, 1.0);\n \
+ }}\n \
+ hz_Vertex = vec4(hz_p, 1.0);\n \
+ hz_MultiTexCoord0 = vec4(0.0, 0.0, 0.0, 1.0);\n \
+ hz_MultiTexCoord1 = vec4(0.0, 240.0, 0.0, 1.0);\n \
+ hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \
+ hz_Normal = vec3(0.0, 1.0, 0.0);\n"
+ );
+ horizon_shaders::GbuffersInputs {
+ declarations,
+ body,
+ attributes: vec![
+ ("vaPosition".into(), "hz_Vertex.xyz".into()),
+ ("vaColor".into(), "hz_Color".into()),
+ ("vaNormal".into(), "hz_Normal".into()),
+ ],
+ // At the far plane: the sky shows only where nothing else drew, and it writes no depth.
+ after: " gl_Position.z = gl_Position.w;\n".into(),
+ alpha_test: None,
+ }
+}
+
+/// What `gbuffers_skytextured` reads from the sun and moon's vertices.
+pub fn celestial_inputs() -> horizon_shaders::GbuffersInputs {
+ horizon_shaders::GbuffersInputs {
+ declarations:
+ "layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in vec2 hz_v_uv;\n\
+ layout(location = 2) in vec4 hz_v_color;\n"
+ .into(),
+ body: " hz_Vertex = vec4(hz_v_pos, 1.0);\n \
+ hz_MultiTexCoord0 = vec4(hz_v_uv, 0.0, 1.0);\n \
+ hz_MultiTexCoord1 = vec4(0.0, 240.0, 0.0, 1.0);\n \
+ hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \
+ hz_Color = hz_v_color;\n \
+ hz_Normal = vec3(0.0, 0.0, 1.0);\n"
+ .into(),
+ attributes: vec![
+ ("vaPosition".into(), "hz_Vertex.xyz".into()),
+ ("vaUV0".into(), "hz_v_uv".into()),
+ ("vaColor".into(), "hz_Color".into()),
+ ("vaNormal".into(), "hz_Normal".into()),
+ ],
+ after: " gl_Position.z = gl_Position.w;\n".into(),
+ alpha_test: None,
+ }
+}
+
+/// One draw of the sky: its render stage, whether `gbuffers_skytextured` draws it, its vertices.
+pub(super) struct Part {
+ pub stage: &'static str,
+ pub textured: bool,
+ pub range: Range,
+}
+
+/// A disc fan at height `y`, radius 512, as triangles (SkyRenderer's sky and void discs).
+fn disc(y: f32, kind: u32, out: &mut Vec) {
+ let sign = y.signum();
+ let rim = |i: i32| {
+ let a = (i as f32).to_radians();
+ Vertex {
+ pos: [sign * 512.0 * a.cos(), y, 512.0 * a.sin()],
+ kind,
+ }
+ };
+ let centre = Vertex {
+ pos: [0.0, y, 0.0],
+ kind,
+ };
+ for i in (-180..180).step_by(45) {
+ out.extend([centre, rim(i), rim(i + 45)]);
+ }
+}
+
+pub(super) struct PackSky {
+ vertices: wgpu::Buffer,
+ celestial: wgpu::Buffer,
+ uniform: wgpu::Buffer,
+ pub layout: wgpu::BindGroupLayout,
+ group: wgpu::BindGroup,
+ /// Vertex ranges of the sky disc, the sunset fan, the stars and the void.
+ ranges: [Range; 4],
+}
+
+impl PackSky {
+ pub fn new(device: &wgpu::Device) -> Self {
+ let mut v = Vec::new();
+ disc(16.0, SKY, &mut v);
+ let sky = 0..v.len() as u32;
+ let apex = Vertex {
+ pos: [0.0; 3],
+ kind: SUNSET_APEX,
+ };
+ let rim = |i: u32| {
+ let a = i as f32 * std::f32::consts::TAU / 16.0;
+ Vertex {
+ pos: [a.sin(), a.cos(), 0.0],
+ kind: SUNSET_RIM,
+ }
+ };
+ for i in 0..16 {
+ v.extend([apex, rim(i), rim(i + 1)]);
+ }
+ let sunset = sky.end..v.len() as u32;
+ v.extend(
+ super::stars::star_triangles()
+ .into_iter()
+ .map(|pos| Vertex { pos, kind: STAR }),
+ );
+ let stars = sunset.end..v.len() as u32;
+ disc(-4.0, VOID, &mut v);
+ let void = stars.end..v.len() as u32;
+ let vertices = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
+ label: Some("Shader pack sky"),
+ contents: bytemuck::cast_slice(&v),
+ usage: wgpu::BufferUsages::VERTEX,
+ });
+ let celestial = device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("Shader pack sun and moon"),
+ size: (std::mem::size_of::() * 6 * CELESTIAL_QUADS) as u64,
+ usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ });
+ let uniform = device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("Shader pack sky"),
+ size: std::mem::size_of::() as u64,
+ usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ });
+ let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some("Shader pack sky"),
+ entries: &[wgpu::BindGroupLayoutEntry {
+ binding: 0,
+ visibility: wgpu::ShaderStages::VERTEX,
+ ty: wgpu::BindingType::Buffer {
+ ty: wgpu::BufferBindingType::Uniform,
+ has_dynamic_offset: false,
+ min_binding_size: None,
+ },
+ count: None,
+ }],
+ });
+ let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("Shader pack sky"),
+ layout: &layout,
+ entries: &[wgpu::BindGroupEntry {
+ binding: 0,
+ resource: uniform.as_entire_binding(),
+ }],
+ });
+ Self {
+ vertices,
+ celestial,
+ uniform,
+ layout,
+ group,
+ ranges: [sky, sunset, stars, void],
+ }
+ }
+
+ pub fn buffers() -> [Option>; 1] {
+ [Some(wgpu::VertexBufferLayout {
+ array_stride: std::mem::size_of::() as u64,
+ step_mode: wgpu::VertexStepMode::Vertex,
+ attributes: &ATTRIBUTES,
+ })]
+ }
+
+ pub fn celestial_buffers() -> [Option>; 1] {
+ [Some(wgpu::VertexBufferLayout {
+ array_stride: std::mem::size_of::() as u64,
+ step_mode: wgpu::VertexStepMode::Vertex,
+ attributes: &CELESTIAL_ATTRIBUTES,
+ })]
+ }
+
+ /// Writes the frame's sky and returns its draws in vanilla's order: the disc, the sunset
+ /// fan while the sun is near the horizon, the sun and the moon, the stars and, with the
+ /// camera low, the void. `tilt` is the pack's `sunPathRotation` (degrees): the sun, moon
+ /// and stars turn with it (7.1).
+ pub fn prepare(&self, queue: &wgpu::Queue, frame: &super::Frame, tilt: f32) -> Vec {
+ let turn = super::pack_uniforms::tilt_matrix(tilt);
+ let sun_angle = frame.sky.sun_angle.to_radians();
+ // The fan turns to the other horizon in the afternoon (terrain.wgsl's sunrise_weight).
+ let flip = if sun_angle.sin() < 0.0 { 180f32 } else { 0.0 };
+ let theta = (flip + 90.0).to_radians();
+ let (rotation, brightness) = frame.stars.unwrap_or_default();
+ let [r, g, b] = frame.sky.sky_color;
+ let uniform = Uniform {
+ stars: (turn * rotation).to_cols_array_2d(),
+ color: [r, g, b, 1.0],
+ sunrise: frame.sky.sunrise_color,
+ sun: [theta.cos(), theta.sin(), brightness.clamp(0.0, 1.0), 0.0],
+ };
+ queue.write_buffer(&self.uniform, 0, bytemuck::bytes_of(&uniform));
+ // The sun's quad, then the moon's (Game::celestial_geometry), as triangles.
+ let quads = frame
+ .celestial
+ .as_chunks::<4>()
+ .0
+ .iter()
+ .take(CELESTIAL_QUADS);
+ let triangles: Vec = quads
+ .clone()
+ .flat_map(|q| [0, 1, 2, 0, 2, 3].map(|i| q[i]))
+ .map(|v| Celestial {
+ pos: turn.transform_vector3(v.pos.into()).into(),
+ uv: v.uv,
+ color: v.color,
+ })
+ .collect();
+ queue.write_buffer(&self.celestial, 0, bytemuck::cast_slice(&triangles));
+ let [sky, sunset, stars, void] = self.ranges.clone();
+ let part = |stage, textured, range| Part {
+ stage,
+ textured,
+ range,
+ };
+ let mut parts = vec![part("SKY", false, sky)];
+ if frame.sky.sunrise_color[3] > 0.001 {
+ parts.push(part("SUNSET", false, sunset));
+ }
+ for (i, stage) in ["SUN", "MOON"].into_iter().enumerate().take(quads.len()) {
+ parts.push(part(stage, true, i as u32 * 6..i as u32 * 6 + 6));
+ }
+ if frame.stars.is_some() {
+ parts.push(part("STARS", false, stars));
+ }
+ if frame.dark_disc {
+ parts.push(part("VOID", false, void));
+ }
+ parts
+ }
+
+ /// Binds the vertices of the `textured` (sun and moon) or other parts.
+ pub fn bind(&self, pass: &mut wgpu::RenderPass<'_>, textured: bool) {
+ if textured {
+ pass.set_vertex_buffer(0, self.celestial.slice(..));
+ } else {
+ pass.set_bind_group(1, &self.group, &[]);
+ pass.set_vertex_buffer(0, self.vertices.slice(..));
+ }
+ }
+}
diff --git a/crates/horizon-client/src/gfx/pack_timing.rs b/crates/horizon-client/src/gfx/pack_timing.rs
new file mode 100644
index 000000000..acfdb698f
--- /dev/null
+++ b/crates/horizon-client/src/gfx/pack_timing.rs
@@ -0,0 +1,148 @@
+//! Developers' aid: `HORIZON_SHADER_PACK_TIMING=1` times every pass of a shader pack on the
+//! GPU, one frame in `EVERY`, and logs each pass's median every `REPORT` timed frames. The
+//! frame after a timed one waits for the GPU before reading the timestamps (Metal resolves
+//! them without waiting for the passes that write them), so timing slows the game.
+
+const EVERY: u32 = 30;
+const REPORT: usize = 30;
+/// Passes timed per frame at most (two timestamps each).
+const CAPACITY: u32 = 64;
+
+pub(super) struct PassTiming {
+ queries: wgpu::QuerySet,
+ resolve: wgpu::Buffer,
+ read: wgpu::Buffer,
+ period_ns: f32,
+ frame: u32,
+ /// The passes timed in the frame being recorded or waiting to be read, in order.
+ names: Vec,
+ timed: bool,
+ samples: Vec<(String, Vec)>,
+ reports: usize,
+}
+
+impl PassTiming {
+ pub fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Option {
+ if std::env::var_os("HORIZON_SHADER_PACK_TIMING").is_none()
+ || !device.features().contains(wgpu::Features::TIMESTAMP_QUERY)
+ {
+ return None;
+ }
+ let size = u64::from(CAPACITY) * 2 * 8;
+ let buffer = |label, usage| {
+ device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some(label),
+ size,
+ usage,
+ mapped_at_creation: false,
+ })
+ };
+ Some(Self {
+ queries: device.create_query_set(&wgpu::QuerySetDescriptor {
+ label: Some("Shader pack pass timing"),
+ ty: wgpu::QueryType::Timestamp,
+ count: CAPACITY * 2,
+ }),
+ resolve: buffer(
+ "Shader pack timing resolve",
+ wgpu::BufferUsages::QUERY_RESOLVE | wgpu::BufferUsages::COPY_SRC,
+ ),
+ read: buffer(
+ "Shader pack timing read",
+ wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
+ ),
+ period_ns: queue.get_timestamp_period(),
+ frame: 0,
+ names: Vec::new(),
+ timed: false,
+ samples: Vec::new(),
+ reports: 0,
+ })
+ }
+
+ /// At the start of a frame: reads the last timed frame's passes, and decides whether this
+ /// one is timed.
+ pub fn begin(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) {
+ if self.timed && !self.names.is_empty() {
+ self.collect(device, queue);
+ }
+ self.names.clear();
+ self.frame += 1;
+ self.timed = self.frame.is_multiple_of(EVERY);
+ }
+
+ /// Timestamp writes for the pass `name`, in a timed frame.
+ pub fn writes(&mut self, name: &str) -> Option> {
+ let i = self.names.len() as u32;
+ if !self.timed || i >= CAPACITY {
+ return None;
+ }
+ self.names.push(name.to_owned());
+ Some(wgpu::RenderPassTimestampWrites {
+ query_set: &self.queries,
+ beginning_of_pass_write_index: Some(2 * i),
+ end_of_pass_write_index: Some(2 * i + 1),
+ })
+ }
+
+ fn collect(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) {
+ // The timed frame first, then the resolve, so every timestamp is written.
+ _ = device.poll(wgpu::PollType::wait_indefinitely());
+ let count = self.names.len() as u32 * 2;
+ let bytes = u64::from(count) * 8;
+ let mut encoder = device.create_command_encoder(&Default::default());
+ encoder.resolve_query_set(&self.queries, 0..count, &self.resolve, 0);
+ encoder.copy_buffer_to_buffer(&self.resolve, 0, &self.read, 0, bytes);
+ queue.submit([encoder.finish()]);
+ let slice = self.read.slice(..bytes);
+ slice.map_async(wgpu::MapMode::Read, |_| {});
+ _ = device.poll(wgpu::PollType::wait_indefinitely());
+ let Ok(data) = slice.get_mapped_range() else {
+ return;
+ };
+ let stamps: Vec = data
+ .as_chunks::<8>()
+ .0
+ .iter()
+ .map(|b| u64::from_le_bytes(*b))
+ .collect();
+ drop(data);
+ self.read.unmap();
+ // Passes overlap on tiled GPUs (a pass begins before the one before it ends): a pass
+ // costs the time from the later of its beginning and the previous pass's end.
+ let mut previous_end = 0;
+ for (name, pair) in self.names.iter().zip(stamps.as_chunks::<2>().0) {
+ if pair[0] == 0 || pair[1] < pair[0] {
+ continue;
+ }
+ let from = pair[0].max(previous_end).min(pair[1]);
+ previous_end = pair[1];
+ let ms = (pair[1] - from) as f32 * self.period_ns / 1e6;
+ match self.samples.iter_mut().find(|s| s.0 == *name) {
+ Some(s) => s.1.push(ms),
+ None => self.samples.push((name.clone(), vec![ms])),
+ }
+ }
+ self.reports += 1;
+ if self.reports.is_multiple_of(REPORT) {
+ self.report();
+ }
+ }
+
+ fn report(&mut self) {
+ let mut total = 0.0;
+ let mut parts = Vec::new();
+ // A pass that did not run in this window (no entities, the sky out of sight) is left out.
+ for (name, v) in self.samples.iter_mut().filter(|s| !s.1.is_empty()) {
+ v.sort_by(f32::total_cmp);
+ let median = v[v.len() / 2];
+ total += median;
+ parts.push(format!("{name} {median:.3}"));
+ v.clear();
+ }
+ log::info!(
+ "shader pack GPU time per pass (median ms of {REPORT} frames): {} = {total:.3}",
+ parts.join(", ")
+ );
+ }
+}
diff --git a/crates/horizon-client/src/gfx/pack_uniforms.rs b/crates/horizon-client/src/gfx/pack_uniforms.rs
index 7d2a73a6a..d1a85a1ba 100644
--- a/crates/horizon-client/src/gfx/pack_uniforms.rs
+++ b/crates/horizon-client/src/gfx/pack_uniforms.rs
@@ -3,7 +3,8 @@
//! Matrices follow GL's conventions (5.5): `gbufferProjection` maps view depth to clip z in
//! [-w, w], and the depth buffers packs read hold GL window depth.
-use glam::{Mat4, Vec3, Vec4};
+use glam::{DMat4, DVec3, Mat4, Vec3, Vec4};
+use horizon_shaders::Shadow;
use super::Frame;
@@ -23,6 +24,15 @@ pub struct PackInputs {
pub hide_gui: bool,
/// The brightness option, 0..1.
pub brightness: f32,
+ /// The biome at the camera (3.4): registry id, `CAT_*`, `PPT_*`, temperature, downfall.
+ pub biome: i32,
+ pub biome_category: i32,
+ pub biome_precipitation: i32,
+ pub temperature: f32,
+ pub rainfall: f32,
+ /// `heldItemId`, `heldItemId2` (item.properties, 5.3) and `heldBlockLightValue(2)`.
+ pub held_items: [i32; 2],
+ pub held_light: [i32; 2],
}
/// One uniform's value: up to 16 floats or 3 integers.
@@ -134,43 +144,116 @@ pub const NAMES: &[&str] = &[
"heldBlockLightValue",
"heldBlockLightValue2",
"renderStage",
+ "biome",
+ "biome_category",
+ "biome_precipitation",
+ "temperature",
+ "rainfall",
"gbufferModelView",
"gbufferModelViewInverse",
"gbufferProjection",
"gbufferProjectionInverse",
"gbufferPreviousModelView",
"gbufferPreviousProjection",
+ "atlasSize",
+ "shadowModelView",
+ "shadowModelViewInverse",
+ "shadowProjection",
+ "shadowProjectionInverse",
];
/// Values carried from frame to frame.
pub struct FrameUniforms {
pub values: Vec,
+ /// The block atlas's size (`atlasSize`), 0 without one.
+ pub atlas: [u32; 2],
start: std::time::Instant,
last: Option,
frame: u32,
previous: Option<(Vec3, Mat4, Mat4)>,
wetness: f32,
eye_smooth: [f32; 2],
+ /// The pack's shadow settings (7.1).
+ pub shadow: Shadow,
}
impl Default for FrameUniforms {
fn default() -> Self {
Self {
values: Vec::new(),
+ atlas: [0; 2],
start: std::time::Instant::now(),
last: None,
frame: 0,
previous: None,
wetness: 0.0,
eye_smooth: [0.0; 2],
+ shadow: Shadow::default(),
}
}
}
-/// `(0, 100, 0)` on the frame the sky draws a body on: Y -90°, then X by its angle in degrees.
-fn body(angle: f32) -> Vec3 {
- (Mat4::from_rotation_y((-90.0f32).to_radians()) * Mat4::from_rotation_x(angle.to_radians()))
- .transform_vector3(Vec3::new(0.0, 100.0, 0.0))
+/// `(0, 100, 0)` on the frame the sky draws a body on: Y -90°, then X by its angle in degrees;
+/// `sunPathRotation` (`tilt`, degrees) then turns the path about the world's X axis (7.1, OQ-6).
+fn body(angle: f32, tilt: f32) -> Vec3 {
+ (tilt_matrix(tilt)
+ * Mat4::from_rotation_y((-90.0f32).to_radians())
+ * Mat4::from_rotation_x(angle.to_radians()))
+ .transform_vector3(Vec3::new(0.0, 100.0, 0.0))
+}
+
+/// `sunPathRotation`'s turn of the sky's bodies: `tilt` degrees about the world's X axis, so a
+/// positive angle moves the noon sun toward +Z (south).
+pub fn tilt_matrix(tilt: f32) -> Mat4 {
+ Mat4::from_rotation_x(tilt.to_radians())
+}
+
+/// The world direction of the shadow light (7.1): the sun by day, the moon by night.
+fn shadow_light(frame: &Frame, shadow: &Shadow) -> Vec3 {
+ let sun_angle = (frame.sky.sun_angle / 360.0 + 0.25).rem_euclid(1.0);
+ let angle = if sun_angle < 0.5 {
+ frame.sky.sun_angle
+ } else {
+ frame.sky.moon_angle
+ };
+ body(angle, shadow.sun_path_rotation).normalize()
+}
+
+/// Distance from the snapped point to the shadow camera, toward the light: the middle of the
+/// default near and far planes (OQ-6).
+const SHADOW_EYE: f64 = 128.0;
+
+/// The shadow camera (7.1; OQ-6, impl-plan.md): its view (player space to shadow view space)
+/// and GL projection. The camera looks from the light, world +Z up (+Y when the light is near
+/// Z); the point it centres moves in steps of `shadowIntervalSize` along the shadow view's
+/// own axes, and it sits `SHADOW_EYE` blocks toward the light from it. Orthographic over
+/// `shadowDistance` each side, or perspective with `shadowMapFov`.
+pub fn shadow_camera(light: Vec3, camera: DVec3, shadow: &Shadow) -> (Mat4, Mat4) {
+ let light = light.as_dvec3();
+ let up = if light.z.abs() > 0.99 {
+ DVec3::Y
+ } else {
+ DVec3::Z
+ };
+ let rotation = DMat4::look_to_rh(DVec3::ZERO, -light, up);
+ let at = rotation.transform_vector3(camera);
+ let step = f64::from(shadow.interval);
+ let snapped = if step > 0.0 {
+ (at / step).floor() * step
+ } else {
+ at
+ };
+ let offset = at - snapped - DVec3::new(0.0, 0.0, SHADOW_EYE);
+ let view = (DMat4::from_translation(offset) * rotation).as_mat4();
+ let (near, far) = (shadow.near, shadow.far);
+ let projection = match shadow.fov {
+ Some(fov) => Mat4::perspective_rh_gl(fov.to_radians(), 1.0, near, far),
+ None => {
+ let d = shadow.distance;
+ Mat4::orthographic_rh_gl(-d, d, -d, d, near, far)
+ }
+ };
+ (view, projection)
}
/// `value` moved toward `target` with a half-life of `half_life` seconds over `dt` (5.6).
@@ -201,8 +284,9 @@ impl FrameUniforms {
let to_view = |v: Vec3| view.transform_vector3(v);
let sun_angle = (frame.sky.sun_angle / 360.0 + 0.25).rem_euclid(1.0);
let day = sun_angle < 0.5;
- let sun = to_view(body(frame.sky.sun_angle));
- let moon = to_view(body(frame.sky.moon_angle));
+ let tilt = self.shadow.sun_path_rotation;
+ let sun = to_view(body(frame.sky.sun_angle, tilt));
+ let moon = to_view(body(frame.sky.moon_angle, tilt));
// Half-lives are given in ticks (7.2); smoothing runs per frame in seconds (OQ-26).
let half_life = if inputs.rain > self.wetness {
600.0
@@ -214,14 +298,22 @@ impl FrameUniforms {
for (smoothed, target) in self.eye_smooth.iter_mut().zip(eye) {
*smoothed = smooth(*smoothed, target, 10.0 / 20.0, dt);
}
+ // Vanilla fades by the stronger of two linear fogs; a pack gets one: the pair (start
+ // and end together) of the one that is complete nearer (OQ-36). Mixing the nearer start
+ // of one with the nearer end of the other made fog begin at the camera on clear days.
let (fog_color, fog_start, fog_end) = match &frame.fog {
- Some(f) => (
- [f.color[0], f.color[1], f.color[2]],
- f.environmental_start.min(f.render_distance_start),
- f.environmental_end.min(f.render_distance_end),
- ),
+ Some(f) => {
+ let (start, end) = if f.environmental_end < f.render_distance_end {
+ (f.environmental_start, f.environmental_end)
+ } else {
+ (f.render_distance_start, f.render_distance_end)
+ };
+ ([f.color[0], f.color[1], f.color[2]], start, end)
+ }
None => (frame.sky.fog_color, frame.distance * 0.9, frame.distance),
};
+ let light = shadow_light(frame, &self.shadow);
+ let (shadow_view, shadow_projection) = shadow_camera(light, frame.camera, &self.shadow);
let day_time = inputs.day_time.rem_euclid(24000) as i32;
let world_day = inputs.day_time.div_euclid(24000);
let seconds = (now - self.start).as_secs_f32() % 3600.0;
@@ -243,6 +335,11 @@ impl FrameUniforms {
"worldDay" => Value::i(&[world_day as i32]),
"moonPhase" => Value::i(&[world_day.rem_euclid(8) as i32]),
"frameCounter" => Value::i(&[self.frame as i32]),
+ "shadowModelView" => m4(shadow_view),
+ "shadowModelViewInverse" => m4(shadow_view.inverse()),
+ "shadowProjection" => m4(shadow_projection),
+ "shadowProjectionInverse" => m4(shadow_projection.inverse()),
+ "atlasSize" => Value::i(&[self.atlas[0] as i32, self.atlas[1] as i32]),
"frameTime" => Value::f(&[dt]),
"frameTimeCounter" => Value::f(&[seconds]),
"viewWidth" => Value::f(&[size[0] as f32]),
@@ -269,6 +366,11 @@ impl FrameUniforms {
"screenBrightness" => Value::f(&[inputs.brightness]),
"hideGUI" => Value::i(&[inputs.hide_gui as i32]),
"eyeBrightness" => Value::i(&eye.map(|v| v as i32)),
+ "biome" => Value::i(&[inputs.biome]),
+ "biome_category" => Value::i(&[inputs.biome_category]),
+ "biome_precipitation" => Value::i(&[inputs.biome_precipitation]),
+ "temperature" => Value::f(&[inputs.temperature]),
+ "rainfall" => Value::f(&[inputs.rainfall]),
"eyeBrightnessSmooth" => Value::i(&self.eye_smooth.map(|v| v.round() as i32)),
"gbufferModelView" => m4(view),
"gbufferModelViewInverse" => m4(view.inverse()),
@@ -276,8 +378,11 @@ impl FrameUniforms {
"gbufferProjectionInverse" => m4(projection.inverse()),
"gbufferPreviousModelView" => m4(previous_view),
"gbufferPreviousProjection" => m4(previous_projection),
- // Held items wait for `item.properties` (5.3: 0 without the file); renderStage
- // is NONE outside gbuffers draws.
+ "heldItemId" => Value::i(&[inputs.held_items[0]]),
+ "heldItemId2" => Value::i(&[inputs.held_items[1]]),
+ "heldBlockLightValue" => Value::i(&[inputs.held_light[0]]),
+ "heldBlockLightValue2" => Value::i(&[inputs.held_light[1]]),
+ // renderStage is NONE outside gbuffers draws.
_ => Value::i(&[0]),
}
};
@@ -299,6 +404,47 @@ impl FrameUniforms {
}
}
+impl FrameUniforms {
+ /// Evaluates the pack's custom uniforms with this frame's values and puts them after
+ /// `NAMES`' values, in `custom.uniforms()` order.
+ pub fn eval_custom(&mut self, custom: &mut horizon_shaders::Custom) {
+ self.values.truncate(NAMES.len());
+ let values = &self.values;
+ let builtin = |name: &str| {
+ let v = values.get(index(name)?)?;
+ let n = components(name);
+ Some(match *v {
+ Value::F(f) => horizon_shaders::Val::vector(&f.map(f64::from)[..n]),
+ Value::I(i) => horizon_shaders::Val::vector(&i.map(f64::from)[..n.min(3)]),
+ })
+ };
+ let dt = match index("frameTime").and_then(|i| values.get(i)) {
+ Some(Value::F(f)) => f64::from(f[0]),
+ _ => 0.0,
+ };
+ let out = custom.eval(dt, &builtin);
+ self.values
+ .extend(out.iter().map(|v| Value::f(&v.v.map(|x| x as f32)[..v.n])));
+ }
+}
+
+/// How many components the uniform `name` has (16 for a matrix).
+fn components(name: &str) -> usize {
+ match name {
+ "cameraPosition"
+ | "previousCameraPosition"
+ | "upPosition"
+ | "sunPosition"
+ | "moonPosition"
+ | "shadowLightPosition"
+ | "fogColor"
+ | "skyColor" => 3,
+ "eyeBrightness" | "eyeBrightnessSmooth" | "atlasSize" => 2,
+ n if n.starts_with("gbuffer") || n.starts_with("shadow") && n != "shadowAngle" => 16,
+ _ => 1,
+ }
+}
+
/// The position of `name` in [`NAMES`].
pub fn index(name: &str) -> Option {
NAMES.iter().position(|n| *n == name)
@@ -323,7 +469,38 @@ mod tests {
#[test]
fn noon_is_straight_up_and_the_sun_sets_in_the_west() {
- assert!(body(0.0).abs_diff_eq(Vec3::new(0.0, 100.0, 0.0), 1e-3));
- assert!(body(90.0).abs_diff_eq(Vec3::new(-100.0, 0.0, 0.0), 1e-3));
+ assert!(body(0.0, 0.0).abs_diff_eq(Vec3::new(0.0, 100.0, 0.0), 1e-3));
+ assert!(body(90.0, 0.0).abs_diff_eq(Vec3::new(-100.0, 0.0, 0.0), 1e-3));
+ // sunPathRotation tilts noon toward +Z and leaves sunset on the horizon.
+ assert!(body(0.0, 30.0).abs_diff_eq(Vec3::new(0.0, 86.60254, 50.0), 1e-3));
+ assert!(body(90.0, 30.0).abs_diff_eq(Vec3::new(-100.0, 0.0, 0.0), 1e-3));
+ }
+
+ #[test]
+ fn the_shadow_camera_looks_down_from_the_light_in_steps() {
+ let shadow = Shadow::default();
+ let mut camera = DVec3::new(1000.3, 70.0, -20.7);
+ // Noon: the light is straight up.
+ let (view, projection) = shadow_camera(Vec3::Y, camera, &shadow);
+ let at = |p: Vec3| view.transform_point3(p);
+ // Straight down: a block below the player is farther from the light.
+ assert!(at(Vec3::new(0.0, -1.0, 0.0)).z < at(Vec3::ZERO).z);
+ // The player is less than a step from the centre, 128 blocks from the light.
+ let centre = at(Vec3::ZERO);
+ assert!(centre.x.abs() <= 2.0 && centre.y.abs() <= 2.0, "{centre}");
+ assert!((centre.z + 128.0).abs() < 1e-3, "{centre}");
+ // Moving within a step keeps the world where it was in the map.
+ camera.x = 1000.6;
+ let (moved, _) = shadow_camera(Vec3::Y, camera, &shadow);
+ let world = Vec3::new(5.0, -3.0, 9.0);
+ let shift = Vec3::new(0.3, 0.0, 0.0);
+ assert!(
+ moved
+ .transform_point3(world - shift)
+ .abs_diff_eq(at(world), 1e-3)
+ );
+ // shadowDistance blocks reach the map's edge.
+ let edge = projection.project_point3(Vec3::new(160.0, 0.0, -128.0));
+ assert!((edge.x.abs() - 1.0).abs() < 1e-4, "{edge}");
}
}
diff --git a/crates/horizon-client/src/gfx/pack_weather.rs b/crates/horizon-client/src/gfx/pack_weather.rs
new file mode 100644
index 000000000..3479450af
--- /dev/null
+++ b/crates/horizon-client/src/gfx/pack_weather.rs
@@ -0,0 +1,264 @@
+//! Rain and snow drawn by a shader pack's `gbuffers_weather` (spec 2.3): the game's weather
+//! quads as entity vertices, their UV animation applied here rather than in Horizon's weather
+//! shader, each kind textured from its own copy of its sprite so that the pack's `texture`
+//! repeats it across the column as a standalone rain or snow texture would.
+
+use crate::weather::{Time, Vertex as WeatherVertex};
+
+use super::Vertex;
+use super::pack_post::Weather;
+
+/// What `gbuffers_weather` reads from a weather vertex: camera-relative position, the UV in
+/// repeats of the sprite, colour (with vanilla's fade) and light. Weather has no normal; the
+/// pack sees straight up.
+pub fn inputs() -> horizon_shaders::GbuffersInputs {
+ let declarations =
+ "layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in vec2 hz_v_uv;\n\
+ layout(location = 2) in uint hz_v_overlay;\nlayout(location = 3) in vec4 hz_v_color;\n\
+ layout(location = 4) in uvec4 hz_v_light;\nlayout(location = 5) in vec4 hz_v_normal;\n"
+ .to_owned();
+ let body = " hz_Vertex = vec4(hz_v_pos, 1.0);\n \
+ hz_MultiTexCoord0 = vec4(hz_v_uv, 0.0, 1.0);\n \
+ hz_MultiTexCoord1 = vec4(float(hz_v_light.y), float(hz_v_light.x), 0.0, 1.0);\n \
+ hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \
+ hz_Color = hz_v_color;\n \
+ hz_Normal = vec3(0.0, 1.0, 0.0);\n"
+ .to_owned();
+ let attributes = [
+ ("vaPosition", "hz_Vertex.xyz"),
+ ("vaUV0", "hz_v_uv"),
+ ("vaUV2", "ivec2(hz_MultiTexCoord1.xy)"),
+ ("vaColor", "hz_Color"),
+ ("vaNormal", "hz_Normal"),
+ ];
+ horizon_shaders::GbuffersInputs {
+ declarations,
+ body,
+ attributes: attributes
+ .iter()
+ .map(|&(n, v)| (n.to_owned(), v.to_owned()))
+ .collect(),
+ after: String::new(),
+ // Vanilla's weather drops texels below 0.1, as Horizon's own weather shader does.
+ alpha_test: Some(horizon_shaders::AlphaTest {
+ op: ">",
+ reference: 0.1,
+ }),
+ }
+}
+
+/// The UV phase of `v` at `time`: `weather_phase` of entity.wgsl, on the CPU.
+pub fn phase(v: &WeatherVertex, time: Time) -> [f32; 2] {
+ let [ticks, fall_ticks, partial, _] = time.values;
+ let a = v.animation;
+ if v.snow == 0 {
+ let t = ticks + a[1] + partial;
+ return [0.0, (-t / 32.0 * a[0]) % 32.0];
+ }
+ let t = ticks + partial;
+ let du = t * 0.01 * a[2];
+ let dv = t * a[3] * 0.001;
+ let base = |lo: u32, hi: u32| f64::from_bits(u64::from(lo) | u64::from(hi) << 32);
+ let u = (base(v.bases[0], v.bases[1]) + f64::from(du)) as f32;
+ let v_base = (base(v.bases[2], v.bases[3]) + f64::from(dv)) as f32;
+ [u, -(fall_ticks + partial) / 512.0 + v_base]
+}
+
+/// One kind's sprite copied out of the shared atlas: the sprite's rectangle (bits) and its view.
+type Sprite = Option<([u32; 4], wgpu::TextureView)>;
+
+/// The pack's weather vertices and the rain and snow sprites, kept across frames.
+pub struct PackWeather {
+ vertices: wgpu::Buffer,
+ capacity: usize,
+ scratch: Vec,
+ sprites: [Sprite; 2],
+ /// Bumped when a sprite is copied again.
+ generation: u64,
+}
+
+impl PackWeather {
+ pub fn new(device: &wgpu::Device) -> Self {
+ Self {
+ vertices: Self::buffer(device, 64),
+ capacity: 64,
+ scratch: Vec::new(),
+ sprites: [None, None],
+ generation: 0,
+ }
+ }
+
+ fn buffer(device: &wgpu::Device, capacity: usize) -> wgpu::Buffer {
+ device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("Shader pack weather"),
+ size: (capacity * std::mem::size_of::()) as u64,
+ usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ })
+ }
+
+ /// This frame's rain then snow (the game emits them in that order) as entity vertices, and
+ /// the sprites they read. `None` when there is none.
+ pub fn prepare(
+ &mut self,
+ device: &wgpu::Device,
+ encoder: &mut wgpu::CommandEncoder,
+ uploads: &mut super::upload::Uploads,
+ atlas: &wgpu::Texture,
+ weather: &[WeatherVertex],
+ time: Time,
+ indices: &wgpu::Buffer,
+ ) -> Option {
+ if weather.is_empty() {
+ return None;
+ }
+ let rain = weather.iter().take_while(|v| v.snow == 0).count() as u32 / 4;
+ let snow = weather.len() as u32 / 4 - rain;
+ for (kind, first) in [(0, weather.first()), (1, weather.get(rain as usize * 4))] {
+ let Some(first) = first.filter(|v| v.snow as usize == kind) else {
+ continue;
+ };
+ let key = first.rect.map(f32::to_bits);
+ if self.sprites[kind].as_ref().is_none_or(|s| s.0 != key) {
+ let view = copy_sprite(device, encoder, atlas, first.rect);
+ self.sprites[kind] = Some((key, view));
+ self.generation += 1;
+ }
+ }
+ self.scratch.clear();
+ self.scratch.extend(weather.iter().map(|v| {
+ let [du, dv] = phase(v, time);
+ Vertex {
+ pos: v.pos,
+ uv: [v.uv[0] + du, v.uv[1] + dv],
+ layer: 0,
+ color: v.color,
+ light: v.light,
+ normal: [0; 4],
+ }
+ }));
+ if self.scratch.len() > self.capacity {
+ self.capacity = self.scratch.len().next_power_of_two();
+ self.vertices = Self::buffer(device, self.capacity);
+ }
+ uploads.buffer(
+ encoder,
+ &self.vertices,
+ 0,
+ bytemuck::cast_slice(&self.scratch),
+ );
+ let view = |k: usize| self.sprites[k].as_ref().map(|s| s.1.clone());
+ Some(Weather {
+ vertices: self.vertices.clone(),
+ indices: indices.clone(),
+ quads: [0..rain, rain..rain + snow],
+ sprites: [view(0), view(1)],
+ generation: self.generation,
+ })
+ }
+}
+
+/// A texture holding the atlas's texels under `rect` (normalised x, y, width, height), as
+/// Horizon's weather shader reads them (`weather_texel`).
+fn copy_sprite(
+ device: &wgpu::Device,
+ encoder: &mut wgpu::CommandEncoder,
+ atlas: &wgpu::Texture,
+ rect: [f32; 4],
+) -> wgpu::TextureView {
+ let size = [atlas.width() as f32, atlas.height() as f32];
+ let origin = [0, 1].map(|i| (rect[i] * size[i]).round() as u32);
+ let extent = [0, 1].map(|i| ((rect[i + 2] * size[i]).round() as u32).max(1));
+ let extent = [0, 1].map(|i| {
+ extent[i].min(
+ [atlas.width(), atlas.height()][i]
+ .saturating_sub(origin[i])
+ .max(1),
+ )
+ });
+ let size = wgpu::Extent3d {
+ width: extent[0],
+ height: extent[1],
+ depth_or_array_layers: 1,
+ };
+ let texture = device.create_texture(&wgpu::TextureDescriptor {
+ label: Some("Shader pack weather sprite"),
+ size,
+ mip_level_count: 1,
+ sample_count: 1,
+ dimension: wgpu::TextureDimension::D2,
+ format: atlas.format(),
+ usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
+ view_formats: &[],
+ });
+ encoder.copy_texture_to_texture(
+ wgpu::TexelCopyTextureInfo {
+ texture: atlas,
+ mip_level: 0,
+ origin: wgpu::Origin3d {
+ x: origin[0],
+ y: origin[1],
+ z: 0,
+ },
+ aspect: wgpu::TextureAspect::All,
+ },
+ wgpu::TexelCopyTextureInfo {
+ texture: &texture,
+ mip_level: 0,
+ origin: wgpu::Origin3d::ZERO,
+ aspect: wgpu::TextureAspect::All,
+ },
+ size,
+ );
+ texture.create_view(&Default::default())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::weather::Parameters;
+
+ /// The CPU phase matches the game's own weather columns (`Parameters::column`), which
+ /// Horizon's weather shader is tested against.
+ #[test]
+ fn phase_matches_the_weather_columns() {
+ for i in 0..512i32 {
+ let x = i.wrapping_mul(45_238_971);
+ let z = i.wrapping_mul(-418_711).wrapping_add(i32::MIN);
+ let p = Parameters::new(x, z);
+ let bases = p.snow_base.map(f64::to_bits);
+ for ticks in [0, 511, 512, 131_071, 131_072, 1_000_003] {
+ let partial = [0.0, 0.5, 0.999][i as usize % 3];
+ for snow in [false, true] {
+ let column = p.column(snow, ticks, x, -64, 320, z, 0, partial);
+ let v = WeatherVertex {
+ pos: [0.0; 3],
+ uv: [0.0; 2],
+ rect: [0.0; 4],
+ animation: [
+ p.rain_speed,
+ p.rain_tick_offset,
+ p.snow_drift[0],
+ p.snow_drift[1],
+ ],
+ bases: [
+ bases[0] as u32,
+ (bases[0] >> 32) as u32,
+ bases[1] as u32,
+ (bases[1] >> 32) as u32,
+ ],
+ color: 0,
+ light: 0,
+ snow: u32::from(snow),
+ };
+ let [u, v] = phase(&v, Time::new(ticks, partial));
+ assert_eq!(
+ [u.to_bits(), v.to_bits()],
+ [column.u_offset.to_bits(), column.v_offset.to_bits()],
+ "x {x} z {z} ticks {ticks} snow {snow}"
+ );
+ }
+ }
+ }
+ }
+}
diff --git a/crates/horizon-client/src/health.rs b/crates/horizon-client/src/health.rs
index da40f41fc..25c719bfb 100644
--- a/crates/horizon-client/src/health.rs
+++ b/crates/horizon-client/src/health.rs
@@ -261,11 +261,17 @@ impl PlayerHealth {
self.hurt_animation_time = 10;
}
/// `LocalPlayer.hurtTo` with `flashOnSetHealth`: a drop below the current
- /// health starts the hurt animation.
+ /// health starts the hurt animation; `damageCooldownTime` (20 on a drop,
+ /// 10 on a gain) is what lets `Hud.extractPlayerHealth` blink the hearts.
pub fn flash_health_change(&mut self, health: f32) {
let damage = self.health() - health;
- if damage > 0.0 {
+ if damage <= 0.0 {
+ if damage < 0.0 {
+ self.hurt.cooldown = 10;
+ }
+ } else {
self.hurt.last_hurt = damage;
+ self.hurt.cooldown = 20;
self.animate_hurt();
}
}
diff --git a/crates/horizon-client/src/hud.rs b/crates/horizon-client/src/hud.rs
index 28b24c1ad..2b6acda41 100644
--- a/crates/horizon-client/src/hud.rs
+++ b/crates/horizon-client/src/hud.rs
@@ -851,6 +851,12 @@ impl HudBatch {
r.v1 = cv;
self.rect_uv(x, y, w, h, r, color);
}
+ /// Any four-cornered shape in one colour, corners in order round it.
+ pub fn fill_quad(&mut self, atlas: &HudAtlas, corners: [[f32; 2]; 4], color: u32) {
+ let w = atlas.white;
+ let uv = [(w.u0 + w.u1) / 2.0, (w.v0 + w.v1) / 2.0];
+ self.quad(corners, [uv; 4], color);
+ }
/// GuiGraphics.fillGradient: `top` to `bottom`, down the rectangle.
pub fn fill_gradient(
&mut self,
diff --git a/crates/horizon-client/src/mobs/leash.rs b/crates/horizon-client/src/mobs/leash.rs
index b7c5011ba..3bd0513d7 100644
--- a/crates/horizon-client/src/mobs/leash.rs
+++ b/crates/horizon-client/src/mobs/leash.rs
@@ -198,6 +198,15 @@ pub fn tick(m: &mut Mob, w: &MobWorld, events: &mut Vec) {
m.extra.leash_spin = 0.0;
return;
};
+ // Leashable.tickLeash: a dying entity cannot interact with the level.
+ if !m.alive() {
+ events.push(MobEvent::LeashBreak {
+ mob: m.id,
+ drop: true,
+ sound: false,
+ });
+ return;
+ }
let Some(state) = holder_state(w, holder) else {
// The holder is gone: the lead drops.
if !matches!(holder, Holder::Player) {
diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs
index 5918bccd9..d5fd1720f 100644
--- a/crates/horizon-client/src/motion.rs
+++ b/crates/horizon-client/src/motion.rs
@@ -3076,8 +3076,13 @@ mod tests {
if client_side {
w.player.health.flash_health_change(20.0);
assert_eq!(w.player.health.hurt_animation_time, 0);
+ assert_eq!(w.player.health.hurt.cooldown, 0);
w.player.health.flash_health_change(12.0);
assert_eq!(w.player.health.hurt_animation_time, 10);
+ // LocalPlayer.hurtTo's damageCooldownTime: the HUD's hearts blink.
+ assert_eq!(w.player.health.hurt.cooldown, 20);
+ w.player.health.flash_health_change(25.0);
+ assert_eq!(w.player.health.hurt.cooldown, 10);
}
}
}
diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs
index 1d1f099eb..9f91254bd 100644
--- a/crates/horizon-client/src/options_ui.rs
+++ b/crates/horizon-client/src/options_ui.rs
@@ -3403,9 +3403,6 @@ mod tests {
assert_eq!(s.gui_scale, expected);
}
s.gui_scale = u32::MAX;
- s.clamp_gui_scale(framebuffer);
- assert_eq!(s.gui_scale, max);
- s.gui_scale = u32::MAX;
Opt::GuiScale.cycle(&mut s, false, framebuffer);
assert_eq!(s.gui_scale, 0);
s.gui_scale = u32::MAX;
diff --git a/crates/horizon-client/src/packs/cache.rs b/crates/horizon-client/src/packs/cache.rs
index efcf01e4a..51b7a9f1d 100644
--- a/crates/horizon-client/src/packs/cache.rs
+++ b/crates/horizon-client/src/packs/cache.rs
@@ -8,7 +8,8 @@ use std::io::Read;
use std::{collections::BTreeSet, fs, io, path::Path, path::PathBuf};
// Bump when overlay/merge semantics change. Markers are disposable cache data.
-const VERSION: u32 = 3;
+// Version 3 could omit untouched vanilla directories on Windows (#1649, #1688).
+const VERSION: u32 = 4;
#[derive(Default, Serialize, Deserialize)]
pub(super) struct Dependencies {
@@ -421,6 +422,73 @@ mod tests {
out
}
+ #[test]
+ #[cfg(not(unix))]
+ fn texture_only_packs_rebuild_incomplete_windows_v3_overlays() {
+ let f = Fixture::new();
+ let blockstate = "assets/minecraft/blockstates/oak_stairs.json";
+ let model = "assets/minecraft/models/block/oak_stairs.json";
+ let texture = "assets/minecraft/textures/block/stone.png";
+ let font = "assets/minecraft/textures/font/ascii.png";
+ f.write(&format!("vanilla/{blockstate}"), br#"{"variants":{}}"#);
+ f.write(&format!("vanilla/{model}"), br#"{"elements":[]}"#);
+ f.write(&format!("vanilla/{texture}"), b"vanilla texture");
+ f.write(&format!("vanilla/{font}"), b"vanilla font");
+ let folder = f.dir_pack("textures");
+ f.write(&format!("textures/{texture}"), b"pack texture");
+ f.write(
+ "textures.zip",
+ &archive(
+ &[
+ (
+ "pack.mcmeta",
+ br#"{"pack":{"description":"test","pack_format":97}}"#.to_vec(),
+ ),
+ (texture, b"pack texture".to_vec()),
+ ],
+ true,
+ ),
+ );
+ let zip = packs::pack(
+ "zip".into(),
+ Text::empty(),
+ Source::Zip(f.0.join("textures.zip")),
+ )
+ .unwrap();
+ let vanilla = f.0.join("vanilla");
+ for pack in [folder, zip] {
+ let out = f.0.join(format!("cached-{}", pack.id));
+ let selected = std::slice::from_ref(&pack);
+ packs::build_overlay(&vanilla, selected, &out).unwrap();
+ let check = || {
+ for name in [blockstate, model, font] {
+ assert_eq!(
+ fs::read(out.join(name)).unwrap(),
+ fs::read(vanilla.join(name)).unwrap()
+ );
+ }
+ assert_eq!(fs::read(out.join(texture)).unwrap(), b"pack texture");
+ };
+ check();
+ // Before #1688, Windows silently omitted untouched directories.
+ // A v3 marker still validates because it checks only source files.
+ let marker_path = marker_path(&out);
+ let mut marker: Marker =
+ serde_json::from_slice(&fs::read(&marker_path).unwrap()).unwrap();
+ marker.version = 3;
+ for dir in ["blockstates", "models", "textures/font"] {
+ fs::remove_dir_all(out.join("assets/minecraft").join(dir)).unwrap();
+ }
+ fs::write(&marker_path, serde_json::to_vec(&marker).unwrap()).unwrap();
+ assert!(marker.dependencies.valid());
+ packs::build_overlay(&vanilla, selected, &out).unwrap();
+ check();
+ // The repaired overlay also remains complete on a cache hit.
+ packs::build_overlay(&vanilla, selected, &out).unwrap();
+ check();
+ }
+ }
+
#[test]
fn directory_inputs_config_and_owned_output_recovery() {
let f = Fixture::new();
@@ -456,7 +524,10 @@ mod tests {
let path = f.0.join(format!("high/{resource}"));
let original_time = fs::metadata(&path).unwrap().modified().unwrap();
fs::write(&path, br#"{"p":"c"}"#).unwrap();
- fs::File::open(path)
+ // Windows requires a writable handle to restore the timestamp.
+ fs::OpenOptions::new()
+ .write(true)
+ .open(path)
.unwrap()
.set_times(fs::FileTimes::new().set_modified(original_time))
.unwrap();
diff --git a/crates/horizon-client/src/particles.rs b/crates/horizon-client/src/particles.rs
index be7f32551..fa9dc9c6e 100644
--- a/crates/horizon-client/src/particles.rs
+++ b/crates/horizon-client/src/particles.rs
@@ -1212,10 +1212,7 @@ impl ParticleEngine {
requested: None,
names: Vec::new(),
rects: Vec::new(),
- atlas: atlas
- .iter()
- .map(|(name, &rect)| (name.clone(), (rect, translucent.contains(name))))
- .collect(),
+ atlas: HashMap::new(),
sets: HashMap::new(),
set_sprites: Vec::new(),
emitters: Vec::new(),
@@ -1225,6 +1222,34 @@ impl ParticleEngine {
made: Default::default(),
item_sprites: HashMap::new(),
};
+ engine.reload(root, atlas, translucent);
+ engine
+ }
+ /// `onResourceManagerReload`: sprite rects and sets come from the
+ /// reloaded atlas and pack; a live particle keeps drawing its sprite by
+ /// name or index.
+ pub fn reload(
+ &mut self,
+ root: &Path,
+ atlas: &HashMap,
+ translucent: &HashSet,
+ ) {
+ self.atlas = atlas
+ .iter()
+ .map(|(name, &rect)| (name.clone(), (rect, translucent.contains(name))))
+ .collect();
+ self.rects = self
+ .names
+ .iter()
+ .map(|name| self.atlas.get(name).map(|&(rect, _)| rect))
+ .collect();
+ self.sets.clear();
+ self.set_sprites.clear();
+ self.item_sprites.clear();
+ self.load_sprites(root);
+ }
+ /// The sprite sets of every `particles/*.json` under `root`.
+ fn load_sprites(&mut self, root: &Path) {
let dir = root.join("assets/minecraft/particles");
for &ty in ParticleType::ALL {
let Ok(bytes) = std::fs::read(dir.join(format!("{}.json", ty.name()))) else {
@@ -1239,18 +1264,16 @@ impl ParticleEngine {
a.iter()
.filter_map(|t| t.as_str())
.map(|t| {
- engine
- .intern(&format!("particle/{}", t.trim_start_matches("minecraft:")))
+ self.intern(&format!("particle/{}", t.trim_start_matches("minecraft:")))
})
.collect()
})
.unwrap_or_default();
if !sprites.is_empty() {
- engine.sets.insert(ty, engine.set_sprites.len() as u32);
- engine.set_sprites.push(sprites);
+ self.sets.insert(ty, self.set_sprites.len() as u32);
+ self.set_sprites.push(sprites);
}
}
- engine
}
fn intern(&mut self, name: &str) -> u32 {
if let Some(i) = self.names.iter().position(|n| n == name) {
@@ -1601,7 +1624,10 @@ impl ParticleEngine {
return;
}
if let Some(set) = p.set {
- let sprites = &self.set_sprites[set as usize];
+ // A sprite set's index can outlive the reload that rebuilt them.
+ let Some(sprites) = self.set_sprites.get(set as usize) else {
+ return;
+ };
let lifetime = p.lifetime.max(1);
let index = (p.age.max(0) as i64 * (sprites.len() as i64 - 1) / i64::from(lifetime))
.clamp(0, sprites.len() as i64 - 1);
diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs
index 937519da9..03a36a2cd 100644
--- a/crates/horizon-client/src/save.rs
+++ b/crates/horizon-client/src/save.rs
@@ -572,6 +572,17 @@ fn merge_block_entity(original: &Tag, loaded: &Tag, current: &Tag) -> Tag {
}
merged
}
+/// `BannerBlockEntity.loadAdditional` (and the `components` a block entity keeps).
+pub fn decode_banner(tag: &Tag) -> crate::containers::Banner {
+ crate::containers::Banner {
+ layers: crate::banner::layers_from_nbt(tag.get("patterns")),
+ name: text(tag.get("CustomName")),
+ components: match tag.get("components") {
+ Some(Tag::Compound(c)) => c.clone(),
+ _ => Compound::new(),
+ },
+ }
+}
pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::containers::BlockEntity)> {
use crate::containers::BlockEntity;
let int = |k: &str| number(tag.get(k)).map(|v| v as i32);
@@ -605,17 +616,7 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain
return Some((pos, BlockEntity::Lectern(lectern)));
}
if id == "minecraft:banner" {
- return Some((
- pos,
- BlockEntity::Banner(crate::containers::Banner {
- layers: crate::banner::layers_from_nbt(tag.get("patterns")),
- name: text(tag.get("CustomName")),
- components: match tag.get("components") {
- Some(Tag::Compound(c)) => c.clone(),
- _ => Compound::new(),
- },
- }),
- ));
+ return Some((pos, BlockEntity::Banner(decode_banner(tag))));
}
if id == "minecraft:beacon" {
let beacon = crate::containers::Beacon {
diff --git a/crates/horizon-client/src/sculk.rs b/crates/horizon-client/src/sculk.rs
index 3affbda77..eefedaa98 100644
--- a/crates/horizon-client/src/sculk.rs
+++ b/crates/horizon-client/src/sculk.rs
@@ -413,6 +413,13 @@ impl SculkEntity {
spreader: SculkSpreader::default(),
}
}
+ /// Whether its block entity tick does nothing: no vibration chosen
+ /// or on its way, none waiting to be chosen, and no charge to spread.
+ pub fn idle(&self) -> bool {
+ self.vibration.current.is_none()
+ && self.vibration.selector.current().is_none()
+ && self.spreader.cursors.is_empty()
+ }
pub fn for_block(name: &str) -> Option {
Some(Self::new(match name {
"minecraft:sculk_sensor" => SculkKind::Sensor,
diff --git a/crates/horizon-client/src/settings.rs b/crates/horizon-client/src/settings.rs
index 5fc09e35b..2a53e1702 100644
--- a/crates/horizon-client/src/settings.rs
+++ b/crates/horizon-client/src/settings.rs
@@ -104,7 +104,7 @@ pub struct Settings {
pub master_volume: f32,
/// Volumes of every other sound source, indexed like SOUND_SOURCES.
pub volumes: [f32; 11],
- /// 0 = auto.
+ /// Requested scale (0 = auto); rendering caps it to the current framebuffer.
pub gui_scale: u32,
pub view_bobbing: bool,
/// Brightness 0..1 (gamma).
@@ -284,12 +284,6 @@ fn put_s(f: &mut OptionsFile, key: &str, v: &str) {
}
}
impl Settings {
- /// `ClampingLazyMaxIntRange.validateValue`: clamp once the framebuffer is known.
- pub fn clamp_gui_scale(&mut self, framebuffer: [i32; 2]) {
- let max = crate::window::calculate_scale(framebuffer, 0, self.force_unicode) as u32;
- self.gui_scale = self.gui_scale.min(max);
- }
-
pub fn load(path: &Path) -> Self {
let horizon = path
.parent()
@@ -597,6 +591,31 @@ impl Settings {
#[cfg(test)]
mod tests {
use super::*;
+ #[test]
+ fn gui_scale_survives_temporary_small_framebuffer() {
+ for requested in [0, 1, 2, 6] {
+ let settings = Settings::parse(&format!("guiScale:{requested}\n"));
+ // Hyprland's initial surface can be smaller than the fullscreen one.
+ for (framebuffer, maximum) in [([715, 860], 2), ([3400, 1913], 7)] {
+ let effective = crate::window::calculate_scale(
+ framebuffer,
+ settings.gui_scale as i32,
+ settings.force_unicode,
+ );
+ assert_eq!(
+ effective,
+ if requested == 0 {
+ maximum
+ } else {
+ (requested as i32).min(maximum)
+ }
+ );
+ let saved = Settings::parse(&settings.serialize());
+ assert_eq!(saved.gui_scale, requested);
+ }
+ }
+ }
+
#[test]
fn round_trips_vanilla_options_txt() {
let text = "version:4000\nfov:0.5\nrenderDistance:16\nmaxFps:260\nrenderClouds:\"fast\"\nkey_key.jump:key.keyboard.j\nsoundCategory_music:0.25\nresourcePacks:[\"vanilla\",\"file/x.zip\"]\nlang:fr_fr\nsomeFutureOption:42\n";
diff --git a/crates/horizon-client/src/structure_blocks.rs b/crates/horizon-client/src/structure_blocks.rs
index 1e1906f6d..9503294e0 100644
--- a/crates/horizon-client/src/structure_blocks.rs
+++ b/crates/horizon-client/src/structure_blocks.rs
@@ -128,6 +128,27 @@ fn text(nbt: &Compound, key: &str) -> Option {
}
}
+/// The client block entity for one of a chunk's generated block entities,
+/// before a container's loot is unpacked: `StructureTemplate.placeInWorld`
+/// hands the template's block entity NBT to the level's block entity.
+fn generated_block_entity(block: &str, nbt: &Compound) -> Option {
+ let mut entity = BlockEntity::for_block(block)?;
+ match &mut entity {
+ // MonsterRoomFeature and the structures' setEntityId.
+ BlockEntity::Spawner(s) => {
+ **s = crate::mobs::spawner::Spawner::load(&Tag::Compound(nbt.clone()));
+ }
+ BlockEntity::TrialSpawner(s) => {
+ **s = crate::mobs::trial_spawner::TrialSpawner::load(&Tag::Compound(nbt.clone()));
+ }
+ BlockEntity::Vault(v) => **v = crate::vault::Vault::load(&Tag::Compound(nbt.clone())),
+ // BannerBlockEntity.loadAdditional: the End cities' black triangles.
+ BlockEntity::Banner(b) => *b = crate::save::decode_banner(&Tag::Compound(nbt.clone())),
+ _ => {}
+ }
+ Some(entity)
+}
+
/// Client block entities for the generated block entities of a chunk.
pub fn block_entities(
chunk: &ProtoChunk,
@@ -137,19 +158,9 @@ pub fn block_entities(
for (pos, nbt) in &chunk.block_entities {
let [x, y, z] = *pos;
let block = chunk.block_name(x, y, z);
- let Some(mut entity) = BlockEntity::for_block(block) else {
+ let Some(mut entity) = generated_block_entity(block, nbt) else {
continue;
};
- // MonsterRoomFeature and the structures' setEntityId.
- if let BlockEntity::Spawner(s) = &mut entity {
- **s = crate::mobs::spawner::Spawner::load(&Tag::Compound(nbt.clone()));
- }
- if let BlockEntity::TrialSpawner(s) = &mut entity {
- **s = crate::mobs::trial_spawner::TrialSpawner::load(&Tag::Compound(nbt.clone()));
- }
- if let BlockEntity::Vault(v) = &mut entity {
- **v = crate::vault::Vault::load(&Tag::Compound(nbt.clone()));
- }
if let (BlockEntity::Chest(slots), Some(table), Some(loot)) =
(&mut entity, text(nbt, "LootTable"), loot)
{
@@ -165,14 +176,10 @@ pub fn block_entities(
.iter_mut()
.zip(loot.fill(&table, seed, 27, Some(biome)))
{
- *slot = stack.map(|s| {
- let mut item = loot_item(&s);
- // An ominous bottle's level (the default, 0, is no component).
- if let Some(level) = s.ominous_amplifier.filter(|l| *l > 0) {
- item.data_mut().ominous_amplifier = Some(level.clamp(0, 4) as u8);
- }
- item
- });
+ // With what the rolls put on them: enchantments
+ // (enchant_with_levels in End city chests), potions and an
+ // ominous bottle's level.
+ *slot = stack.map(|s| loot_item_with_components(&s));
}
}
out.push(((x, y, z), entity));
@@ -211,7 +218,7 @@ pub fn unpack_entity_loot(
let slots: Vec> = loot
.fill(&table, seed, 27, Some(biome))
.into_iter()
- .map(|s| s.map(|s| loot_item(&s)))
+ .map(|s| s.map(|s| loot_item_with_components(&s)))
.collect();
nbt.remove(&JavaString::from("LootTable"));
nbt.remove(&JavaString::from("LootTableSeed"));
@@ -224,6 +231,45 @@ mod tests {
use super::*;
use horizon_world::loot::{ExplorationMap, LootStack};
+ #[test]
+ fn placed_chest_loot_keeps_its_enchantments_and_potions() {
+ let stack =
+ |item: &str, enchantments: Vec<(String, i32)>, potion: Option<&str>| LootStack {
+ item: item.into(),
+ count: 1,
+ damage: 0,
+ enchantments,
+ potion: potion.map(str::to_owned),
+ map: false,
+ exploration: None,
+ ominous_amplifier: None,
+ };
+ // An End city chest's enchant_with_levels roll.
+ let boots = loot_item_with_components(&stack(
+ "minecraft:diamond_boots",
+ vec![
+ ("minecraft:protection".into(), 3),
+ ("minecraft:unbreaking".into(), 2),
+ ],
+ None,
+ ));
+ assert_eq!(boots.enchantments().len(), 2);
+ // A book's go into stored_enchantments.
+ let book = loot_item_with_components(&stack(
+ "minecraft:enchanted_book",
+ vec![("minecraft:mending".into(), 1)],
+ None,
+ ));
+ assert!(book.enchantments().is_empty());
+ assert_eq!(book.stored_enchantments().len(), 1);
+ let arrow = loot_item_with_components(&stack(
+ "minecraft:tipped_arrow",
+ Vec::new(),
+ Some("minecraft:slowness"),
+ ));
+ assert_eq!(arrow.potion(), Some("minecraft:slowness"));
+ }
+
#[test]
fn exploration_maps_wait_in_custom_data_until_searched() {
let mut stack = LootStack {
@@ -278,4 +324,43 @@ mod tests {
stack.exploration = None;
assert!(!has_pending_map(&loot_item(&stack)));
}
+
+ /// The End cities' tower tops save their banners' patterns in the
+ /// template; the placed banner must keep them (`EndCityPieces`).
+ #[test]
+ fn a_generated_banner_keeps_its_template_patterns() {
+ let patterns = Tag::List(vec![
+ Tag::compound([
+ ("color", Tag::String("black".into())),
+ ("pattern", Tag::String("minecraft:triangle_top".into())),
+ ]),
+ Tag::compound([
+ ("color", Tag::String("black".into())),
+ ("pattern", Tag::String("minecraft:triangle_bottom".into())),
+ ]),
+ ]);
+ let nbt = Tag::compound([
+ ("id", Tag::String("minecraft:banner".into())),
+ ("patterns", patterns),
+ ]);
+ let Tag::Compound(nbt) = nbt else {
+ unreachable!()
+ };
+ let Some(BlockEntity::Banner(banner)) =
+ generated_block_entity("minecraft:magenta_banner", &nbt)
+ else {
+ panic!("a banner block makes a banner block entity");
+ };
+ assert_eq!(banner.layers.len(), 2);
+ assert_eq!(&*banner.layers[0].pattern, "minecraft:triangle_top");
+ assert_eq!(&*banner.layers[1].pattern, "minecraft:triangle_bottom");
+ assert_eq!(&*banner.layers[0].color, "black");
+ // A banner the template saved without patterns stays bare.
+ let Some(BlockEntity::Banner(bare)) =
+ generated_block_entity("minecraft:white_banner", &Compound::new())
+ else {
+ panic!("a banner block makes a banner block entity");
+ };
+ assert!(bare.layers.is_empty());
+ }
}
diff --git a/crates/horizon-client/src/vanillagen.rs b/crates/horizon-client/src/vanillagen.rs
index c44e1f542..c890213c5 100644
--- a/crates/horizon-client/src/vanillagen.rs
+++ b/crates/horizon-client/src/vanillagen.rs
@@ -41,6 +41,10 @@ pub struct VanillaTerrain {
tick_states: Vec,
seed: i64,
dimension: String,
+ /// When the last proto chunk was generated, in milliseconds since
+ /// `created`: the ring search waits for a pause in generation.
+ last_proto: std::sync::atomic::AtomicU64,
+ created: std::time::Instant,
}
/// A proto chunk, or the position of one whose generation failed.
enum VanillaProto {
@@ -139,6 +143,8 @@ impl VanillaTerrain {
tick_states,
seed,
dimension: dimension.to_owned(),
+ last_proto: std::sync::atomic::AtomicU64::new(0),
+ created: std::time::Instant::now(),
})
}
/// A proto chunk's blocks, biomes, scheduled ticks and post-processing
@@ -272,9 +278,53 @@ impl VanillaTerrain {
pub fn origin(&self) -> crate::Result {
Ok(self.generator.origin()?)
}
- /// Concentric ring positions of a structure set (strongholds), once known.
+ /// Concentric ring positions of a structure set (strongholds), searched
+ /// for now if no chunk needed them yet.
pub fn ring_positions(&self, set: &str) -> Option> {
- self.generator.ring_positions(set)
+ let mut worker = self.query.lock().unwrap_or_else(|e| e.into_inner());
+ let worker = worker.get_or_insert_with(|| self.generator.worker());
+ worker.ring_positions(set)
+ }
+ /// Searches the concentric rings on a thread of their own, as vanilla's
+ /// background executor does, once generation pauses for two seconds
+ /// (at the latest a minute after the world opens) and in the idle
+ /// class: on a laptop's few cores, sharing them with the first chunks,
+ /// which don't need the rings, made those 3 s slower. An eye of ender
+ /// thrown later finds the rings ready, and a chunk or a query that
+ /// needs them first shares the remaining searches with this thread.
+ pub fn search_rings_behind(self: &Arc) {
+ const QUIET: u64 = 2_000;
+ const LATEST: u64 = 60_000;
+ let terrain = Arc::downgrade(self);
+ let spawned = std::thread::Builder::new()
+ .name("Ring search".into())
+ .spawn(move || {
+ horizon_threads::idle_current();
+ loop {
+ std::thread::sleep(std::time::Duration::from_millis(500));
+ // The world closed: nothing to search for.
+ let Some(terrain) = terrain.upgrade() else {
+ return;
+ };
+ let now = terrain.elapsed_ms();
+ let last = terrain
+ .last_proto
+ .load(std::sync::atomic::Ordering::Relaxed);
+ if now >= LATEST || (last > 0 && now - last >= QUIET) {
+ let _ = terrain
+ .generator
+ .worker()
+ .ring_positions("minecraft:strongholds");
+ return;
+ }
+ }
+ });
+ if let Err(e) = spawned {
+ log::warn!("ring search thread: {e}");
+ }
+ }
+ fn elapsed_ms(&self) -> u64 {
+ u64::try_from(self.created.elapsed().as_millis()).unwrap_or(u64::MAX)
}
}
/// The worldgen bundle at `dir`, loaded once and shared by every generator
@@ -323,6 +373,10 @@ impl ChunkGenerator for VanillaTerrain {
}
}
fn proto(&self, worker: &mut GeneratorWorker, x: i32, z: i32) -> Proto {
+ self.last_proto.store(
+ self.elapsed_ms().max(1),
+ std::sync::atomic::Ordering::Relaxed,
+ );
match worker.generate_proto(ChunkPos::new(x, z)) {
Ok(chunk) => Box::new(VanillaProto::Ready(Box::new(chunk))),
Err(e) => {
diff --git a/crates/horizon-mod-sdk/src/lib.rs b/crates/horizon-mod-sdk/src/lib.rs
index 11da3e897..734fffb89 100644
--- a/crates/horizon-mod-sdk/src/lib.rs
+++ b/crates/horizon-mod-sdk/src/lib.rs
@@ -38,7 +38,7 @@ wit_bindgen::generate!({
pub use exports::horizon::mods::events::{Guest as __Guest, Registration};
pub use exports::horizon::mods::ui_events::Guest as __UiEventsGuest;
-pub use horizon::mods::{chat, command, game, hud, log, storage, ui};
+pub use horizon::mods::{chat, command, game, hud, input, log, render, storage, ui};
/// The game's calls into a mod. Only the hooks [`Registration`] asks for
/// are called; the others may stay as their defaults.
diff --git a/crates/horizon-mod-sdk/wit/horizon.wit b/crates/horizon-mod-sdk/wit/horizon.wit
index a46cb133a..04d13dd13 100644
--- a/crates/horizon-mod-sdk/wit/horizon.wit
+++ b/crates/horizon-mod-sdk/wit/horizon.wit
@@ -171,6 +171,124 @@ interface ui-events {
settings-opened: func();
}
+/// Capability `input` (experimental: the manifest needs
+/// `experimental = ["input"]`): the use button on items the mod claims, and
+/// the mod's own keys. Events wait for `poll`, at most 256.
+interface input {
+ /// The main hand's stack.
+ record held-item {
+ /// `minecraft:spyglass`, `:`.
+ id: string,
+ count: u32,
+ /// The hotbar slot, 0 to 8.
+ slot: u32,
+ }
+ record item-use {
+ item: held-item,
+ /// Game ticks the button was held (0 on `use-start`).
+ ticks: u32,
+ }
+ variant event {
+ /// The use button went down with a claimed item in the main hand.
+ /// The game does nothing else with the click: no placing, no eating,
+ /// no swing.
+ use-start(item-use),
+ /// It came up again, or the item left the hand, or a screen opened.
+ use-stop(item-use),
+ /// A key `register-key` named went down or up while playing (no
+ /// screen open). The game's own mapping on that key still runs.
+ key-down(string),
+ key-up(string),
+ }
+ /// The items whose use the mod handles instead of the game, by id (at
+ /// most 64). Replaces the previous list.
+ claim-items: func(ids: list);
+ /// A key of the mod's: `name` comes back in `key-down` and `key-up`,
+ /// `label` says what it does, `key` is the key as options.txt writes
+ /// it (`key.keyboard.r`). At most 16; an unknown key is an error.
+ register-key: func(name: string, label: string, key: string) -> result<_, string>;
+ /// The main hand's stack, as of this tick; none when it is empty.
+ held: func() -> option;
+ /// The events since the last call, oldest first.
+ poll: func() -> list;
+}
+
+/// Capability `render` (experimental: the manifest needs
+/// `experimental = ["render"]`): shapes the game draws for the mod with its
+/// own renderer, kept until the mod replaces them (no call per frame). A mod
+/// never supplies shader code.
+interface render {
+ /// A rectangle in GUI pixels: the top left corner and the size.
+ record rect {
+ x: f32,
+ y: f32,
+ w: f32,
+ h: f32,
+ }
+ /// A point in GUI pixels from the screen's top left.
+ record point {
+ x: f32,
+ y: f32,
+ }
+ record fill-shape {
+ rect: rect,
+ /// 0xAARRGGBB.
+ color: u32,
+ }
+ /// Any four-cornered shape (a wheel's segment, an arrow): the corners
+ /// in order round it.
+ record quad-shape {
+ corners: tuple,
+ /// 0xAARRGGBB.
+ color: u32,
+ }
+ record sprite-shape {
+ /// One of the game's GUI sprites, as `gui/sprites/` names them
+ /// (`hud/heart/full`, `widget/button`), the resource packs' versions
+ /// included. An unknown one draws nothing.
+ sprite: string,
+ rect: rect,
+ /// A tint, 0xAARRGGBB: 0xFFFFFFFF draws it as it is.
+ color: u32,
+ }
+ record item-shape {
+ /// `minecraft:diamond`, `:`; an unknown one draws nothing.
+ item: string,
+ /// The icon's top left.
+ at: point,
+ /// 1.0 is the hotbar's 16 × 16.
+ scale: f32,
+ }
+ record text-shape {
+ /// Up to 256 characters, one line.
+ text: string,
+ at: point,
+ /// 0xRRGGBB, as `hud`'s lines.
+ color: u32,
+ /// Centred on `at.x` instead of starting there.
+ centered: bool,
+ shadow: bool,
+ }
+ variant shape {
+ fill(fill-shape),
+ quad(quad-shape),
+ sprite(sprite-shape),
+ item(item-shape),
+ text(text-shape),
+ }
+ /// Replaces the mod's HUD shapes: up to 1024, drawn every frame in
+ /// order over the HUD (after the hotbar, under chat and screens), hidden
+ /// with the HUD (F1). An empty list removes them.
+ set-hud: func(shapes: list);
+ /// The screen's size in GUI pixels (width, height), as of this tick.
+ gui-size: func() -> tuple;
+ /// Shakes the camera: a jolt of `strength` (0 to 1, 1 the hardest) that
+ /// fades out over `seconds` (at most 10). Jolts add up to 1. The view
+ /// turns, the player doesn't, and the player's Distortion Effects option
+ /// scales it (0 turns it off).
+ shake: func(strength: f32, seconds: f32);
+}
+
/// What the game calls. Only the hooks `init` asks for are ever called.
interface events {
record registration {
@@ -212,6 +330,8 @@ world horizon-mod {
world horizon-ui-mod {
include horizon-mod;
import ui;
+ import render;
+ import input;
export ui-events;
}
@@ -220,3 +340,9 @@ world ui-host {
import ui;
export ui-events;
}
+
+/// The game's side of capability `render` (imports only).
+world render-host {
+ import render;
+ import input;
+}
diff --git a/crates/horizon-mods/src/manifest.rs b/crates/horizon-mods/src/manifest.rs
index ccb90a9c6..966b367b5 100644
--- a/crates/horizon-mods/src/manifest.rs
+++ b/crates/horizon-mods/src/manifest.rs
@@ -86,9 +86,15 @@ pub enum Capability {
/// A screen the mod owns and a settings page in Options > Mods, drawn
/// with the menus' own widgets (interface `ui`, calls `ui-events`).
Ui,
+ /// Shapes on the HUD and the camera's shake, drawn by the game's own
+ /// renderer (interface `render`).
+ Render,
+ /// The use button on items the mod claims, and its own keys
+ /// (interface `input`).
+ Input,
}
impl Capability {
- pub const ALL: [Capability; 7] = [
+ pub const ALL: [Capability; 9] = [
Capability::GameRead,
Capability::Chat,
Capability::Hud,
@@ -96,6 +102,8 @@ impl Capability {
Capability::Command,
Capability::Registries,
Capability::Ui,
+ Capability::Render,
+ Capability::Input,
];
/// The WIT interface it grants, in `horizon:mods`.
pub fn interface(self) -> &'static str {
@@ -107,12 +115,17 @@ impl Capability {
Capability::Command => "command",
Capability::Registries => "registries",
Capability::Ui => "ui",
+ Capability::Render => "render",
+ Capability::Input => "input",
}
}
/// May change in any release: a mod uses it only with
/// `experimental = [""]` in its manifest.
pub fn is_experimental(self) -> bool {
- matches!(self, Capability::Ui)
+ matches!(
+ self,
+ Capability::Ui | Capability::Render | Capability::Input
+ )
}
/// What the player reads before turning the mod on: "May ".
pub fn describe(self) -> &'static str {
@@ -124,6 +137,8 @@ impl Capability {
Capability::Command => "run commands as you, with your permissions",
Capability::Registries => "add blocks and items to your worlds",
Capability::Ui => "show its own screens and a settings page",
+ Capability::Render => "draw on your screen and shake the camera",
+ Capability::Input => "take over the use button on its own items, and add keys",
}
}
/// The capability a component import needs, `Ok(None)` for one every
@@ -156,6 +171,8 @@ impl fmt::Display for Capability {
Capability::Command => "command",
Capability::Registries => "registries",
Capability::Ui => "ui",
+ Capability::Render => "render",
+ Capability::Input => "input",
};
f.write_str(name)
}
diff --git a/crates/horizon-mods/src/runtime.rs b/crates/horizon-mods/src/runtime.rs
index dfda140e1..a772dfb8c 100644
--- a/crates/horizon-mods/src/runtime.rs
+++ b/crates/horizon-mods/src/runtime.rs
@@ -40,6 +40,22 @@ pub use ui_bindings::horizon::mods::ui::{
ItemWidget, ListWidget, Screen as UiScreen, TextWidget, Widget as UiWidget,
};
use ui_bindings::{UiHost, horizon::mods::ui};
+/// Capability `render`'s imports (docs/mods.md): the game draws the shapes.
+mod render_bindings {
+ wasmtime::component::bindgen!({
+ path: "../horizon-mod-sdk/wit",
+ world: "render-host",
+ });
+}
+use render_bindings::horizon::mods::input;
+/// Capability `input`'s records (WIT `interface input`).
+pub use render_bindings::horizon::mods::input::{Event as InputEvent, HeldItem, ItemUse};
+/// What capability `render` asks the game to draw (WIT `interface render`).
+pub use render_bindings::horizon::mods::render::{
+ FillShape, ItemShape, Point as RenderPoint, QuadShape, Rect as RenderRect, Shape, SpriteShape,
+ TextShape,
+};
+use render_bindings::{RenderHost, horizon::mods::render};
/// A mod's linear memory cap.
const MEMORY: usize = 64 << 20;
@@ -70,6 +86,10 @@ const STORAGE_WRITE_BUDGET: u64 = 1 << 20;
pub struct Snapshot {
pub player: Option,
pub clock: Option,
+ /// The screen in GUI pixels (capability `render`'s `gui-size`).
+ pub gui_size: (f32, f32),
+ /// The main hand's stack (capability `input`'s `held`).
+ pub held: Option,
}
/// What mods asked the game to show since the last [`Mods::drain`].
@@ -89,6 +109,8 @@ pub enum Output {
OpenScreen { owner: usize },
/// `ui.close-screen`: the mod closed its own screen (no `closed` back).
CloseScreen { owner: usize },
+ /// `render.shake`: strength 0 to 1, fading out over `seconds` (≤ 10).
+ Shake { strength: f32, seconds: f32 },
}
/// A mod's text panel, drawn every frame until it changes.
@@ -156,6 +178,9 @@ struct State {
out: Vec,
panel: Option,
ui: UiState,
+ /// Capability `render`'s HUD shapes, drawn until replaced.
+ shapes: Vec,
+ input: InputState,
/// Capability `storage`'s folder; made on first `set`.
data_dir: PathBuf,
/// Bytes `storage::set` may still write before this hook call ends
@@ -234,6 +259,8 @@ impl Mods {
.map_err(|e| e.to_string())?;
UiHost::add_to_linker::>(&mut linker, |s| s)
.map_err(|e| e.to_string())?;
+ RenderHost::add_to_linker::>(&mut linker, |s| s)
+ .map_err(|e| e.to_string())?;
self.engine = Some((engine, linker));
}
Ok(self.engine.as_ref().expect("made above"))
@@ -293,6 +320,8 @@ impl Mods {
out: Vec::new(),
panel: None,
ui: UiState::default(),
+ shapes: Vec::new(),
+ input: InputState::default(),
data_dir,
storage_budget: STORAGE_WRITE_BUDGET,
limits: StoreLimitsBuilder::new()
@@ -484,6 +513,86 @@ impl Mods {
.iter()
.filter_map(|e| e.run.as_ref()?.store.data().panel.as_ref())
}
+ /// Some running mod claimed items (capability `input`).
+ pub fn claims_any(&self) -> bool {
+ self.inputs().any(|i| !i.claimed.is_empty())
+ }
+ /// Some running mod claimed the item `id`.
+ pub fn claims(&self, id: &str) -> bool {
+ self.inputs().any(|i| i.claimed.iter().any(|c| c == id))
+ }
+ /// Some running mod registered a key.
+ pub fn has_keys(&self) -> bool {
+ self.inputs().any(|i| !i.keys.is_empty())
+ }
+ /// The mods' keys: (mod name, key name, label, key), for Controls.
+ pub fn mod_keys(&self) -> impl Iterator- {
+ self.entries
+ .iter()
+ .filter_map(|e| e.run.as_ref())
+ .flat_map(|r| {
+ let d = r.store.data();
+ d.input.keys.iter().map(move |k| (d.name.as_str(), k))
+ })
+ }
+ /// The use button went down with `item`: `use-start` to each mod that
+ /// claimed it.
+ pub fn use_start(&mut self, item: &HeldItem) {
+ let event = InputEvent::UseStart(ItemUse {
+ item: item.clone(),
+ ticks: 0,
+ });
+ self.push_input(|i| i.claimed.contains(&item.id), &event);
+ }
+ /// The claimed use ended after `ticks`.
+ pub fn use_stop(&mut self, item: &HeldItem, ticks: u32) {
+ let event = InputEvent::UseStop(ItemUse {
+ item: item.clone(),
+ ticks,
+ });
+ self.push_input(|i| i.claimed.contains(&item.id), &event);
+ }
+ /// The key `key` (`key.keyboard.r`) went down or up: each mod with a
+ /// key on it hears its own name for it.
+ pub fn key(&mut self, key: &str, down: bool) {
+ for run in self.entries.iter_mut().filter_map(|e| e.run.as_mut()) {
+ let input = &mut run.store.data_mut().input;
+ let names: Vec
= input
+ .keys
+ .iter()
+ .filter(|k| k.key == key)
+ .map(|k| k.name.clone())
+ .collect();
+ for name in names {
+ input.push(if down {
+ InputEvent::KeyDown(name)
+ } else {
+ InputEvent::KeyUp(name)
+ });
+ }
+ }
+ }
+ fn inputs(&self) -> impl Iterator- {
+ self.entries
+ .iter()
+ .filter_map(|e| Some(&e.run.as_ref()?.store.data().input))
+ }
+ fn push_input(&mut self, to: impl Fn(&InputState) -> bool, event: &InputEvent) {
+ for run in self.entries.iter_mut().filter_map(|e| e.run.as_mut()) {
+ let input = &mut run.store.data_mut().input;
+ if to(input) {
+ input.push(event.clone());
+ }
+ }
+ }
+ /// The running mods' HUD shapes (capability `render`), each mod's in
+ /// its own order, mods in load order.
+ pub fn hud_shapes(&self) -> impl Iterator
- {
+ self.entries
+ .iter()
+ .filter_map(|e| e.run.as_ref())
+ .flat_map(|r| &r.store.data().shapes)
+ }
/// The screen mod `i` last pushed with `ui.open-screen`, while it is up.
pub fn screen(&self, i: usize) -> Option<&UiScreen> {
self.entries
@@ -671,6 +780,131 @@ impl chat::Host for State {
}
}
}
+/// A key a mod registered (capability `input`).
+#[derive(Clone, Debug, PartialEq)]
+pub struct ModKey {
+ /// The mod's own name for it, echoed in `key-down`.
+ pub name: String,
+ /// What it does, for Controls.
+ pub label: String,
+ /// `key.keyboard.r`.
+ pub key: String,
+}
+
+/// What capability `input` holds for one mod.
+#[derive(Default)]
+struct InputState {
+ claimed: Vec
,
+ keys: Vec,
+ events: std::collections::VecDeque,
+}
+/// Events kept for `poll`, items claimed and keys, per mod.
+const INPUT_EVENTS: usize = 256;
+const CLAIMS: usize = 64;
+const KEYS: usize = 16;
+
+/// `key.keyboard.` or `key.mouse.`, as options.txt writes keys;
+/// the game knows which names exist and ignores a key it has none for.
+fn key_ok(key: &str) -> bool {
+ let rest = key
+ .strip_prefix("key.keyboard.")
+ .or_else(|| key.strip_prefix("key.mouse."));
+ rest.is_some_and(|r| {
+ (1..=32).contains(&r.len())
+ && r.bytes()
+ .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'.')
+ })
+}
+
+impl InputState {
+ fn push(&mut self, event: InputEvent) {
+ if self.events.len() == INPUT_EVENTS {
+ self.events.pop_front();
+ }
+ self.events.push_back(event);
+ }
+}
+
+impl input::Host for State {
+ fn claim_items(&mut self, ids: Vec) {
+ self.input.claimed = ids.into_iter().take(CLAIMS).map(clip).collect();
+ }
+ fn register_key(&mut self, name: String, label: String, key: String) -> Result<(), String> {
+ if !key_ok(&key) {
+ return Err(format!(
+ "{key:?} is not a key as options.txt writes them (key.keyboard.r)"
+ ));
+ }
+ let (name, label) = (clip(name), clip(label));
+ if let Some(k) = self.input.keys.iter_mut().find(|k| k.name == name) {
+ (k.label, k.key) = (label, key);
+ return Ok(());
+ }
+ if self.input.keys.len() == KEYS {
+ return Err(format!("a mod may register {KEYS} keys"));
+ }
+ self.input.keys.push(ModKey { name, label, key });
+ Ok(())
+ }
+ fn held(&mut self) -> Option {
+ self.snapshot.held.clone()
+ }
+ fn poll(&mut self) -> Vec {
+ self.input.events.drain(..).collect()
+ }
+}
+
+/// HUD shapes one mod may keep (capability `render`).
+const SHAPES: usize = 1024;
+/// The longest jolt `render.shake` makes, in seconds.
+const SHAKE_SECONDS: f32 = 10.0;
+
+impl render::Host for State {
+ fn set_hud(&mut self, shapes: Vec) {
+ self.shapes = shapes
+ .into_iter()
+ .take(SHAPES)
+ .map(|shape| match shape {
+ Shape::Sprite(mut s) => {
+ s.sprite = clip(s.sprite);
+ Shape::Sprite(s)
+ }
+ Shape::Item(mut i) => {
+ i.item = clip(i.item);
+ i.scale = if i.scale.is_finite() {
+ i.scale.clamp(0.0, 16.0)
+ } else {
+ 1.0
+ };
+ Shape::Item(i)
+ }
+ Shape::Text(mut t) => {
+ t.text = clip(t.text);
+ t.color &= 0xFF_FFFF;
+ Shape::Text(t)
+ }
+ other => other,
+ })
+ .collect();
+ }
+ fn gui_size(&mut self) -> (f32, f32) {
+ self.snapshot.gui_size
+ }
+ fn shake(&mut self, strength: f32, seconds: f32) {
+ let ok = |v: f32, max: f32| {
+ if v.is_finite() {
+ v.clamp(0.0, max)
+ } else {
+ 0.0
+ }
+ };
+ let (strength, seconds) = (ok(strength, 1.0), ok(seconds, SHAKE_SECONDS));
+ if strength > 0.0 && seconds > 0.0 {
+ self.out.push(Output::Shake { strength, seconds });
+ }
+ }
+}
+
impl command::Host for State {
fn run(&mut self, line: String) {
let queued = self
diff --git a/crates/horizon-mods/tests/sandbox.rs b/crates/horizon-mods/tests/sandbox.rs
index eb4034a37..7f327af16 100644
--- a/crates/horizon-mods/tests/sandbox.rs
+++ b/crates/horizon-mods/tests/sandbox.rs
@@ -2,7 +2,8 @@
//! examples and a guest
//! that misbehaves on command. Needs `rustup target add wasm32-unknown-unknown`.
use horizon_mods::{
- Clock, Corner, Mods, Output, Package, Player, Snapshot, Status, manifest::componentize,
+ Clock, Corner, HeldItem, Mods, Output, Package, Player, Shape, Snapshot, Status,
+ manifest::componentize,
};
use std::{
path::{Path, PathBuf},
@@ -23,8 +24,8 @@ id = "test_ui_guest"
name = "Test UI Guest"
version = "0.1.0"
api = "0.1"
-capabilities = ["chat", "ui"]
-experimental = ["ui"]
+capabilities = ["chat", "ui", "render", "input"]
+experimental = ["ui", "render", "input"]
"#;
/// Builds the mods once and writes their `.hmod` files; returns the folder.
@@ -96,6 +97,8 @@ fn player() -> Snapshot {
game_time: 100,
day_time: 24000 + 6000,
}),
+ gui_size: (427.0, 240.0),
+ held: None,
}
}
@@ -453,6 +456,96 @@ fn ui_screens_settings_and_events() {
assert!(mods.screen(0).is_none());
}
+#[test]
+fn render_shapes_and_shakes() {
+ let mut mods = load("horizon_mods_test_ui_guest.hmod");
+ mods.set_snapshot(Snapshot {
+ gui_size: (427.0, 240.0),
+ held: None,
+ ..Snapshot::default()
+ });
+ assert_eq!(mods.hud_shapes().count(), 0);
+ assert!(mods.command("render_hud"));
+ let shapes: Vec<_> = mods.hud_shapes().cloned().collect();
+ assert_eq!(shapes.len(), 5);
+ // The guest read `gui-size`; text is clipped to 256 characters.
+ let Shape::Text(t) = &shapes[4] else {
+ panic!("{:?}", shapes[4])
+ };
+ assert!(t.text.starts_with("gui 427x240 "), "{}", t.text);
+ assert_eq!(t.text.chars().count(), 256);
+ assert!(mods.command("render_clear"));
+ assert_eq!(mods.hud_shapes().count(), 0);
+ // One jolt, clamped; zero and NaN shake nothing.
+ mods.drain();
+ assert!(mods.command("render_shake"));
+ assert_eq!(
+ mods.drain(),
+ [Output::Shake {
+ strength: 1.0,
+ seconds: 10.0
+ }]
+ );
+}
+
+#[test]
+fn input_claims_keys_and_events() {
+ let mut mods = load("horizon_mods_test_ui_guest.hmod");
+ let chat = |mods: &mut Mods| -> Vec {
+ mods.drain()
+ .into_iter()
+ .filter_map(|o| match o {
+ Output::Chat { text, .. } => Some(text),
+ _ => None,
+ })
+ .collect()
+ };
+ assert!(!mods.claims_any() && !mods.has_keys());
+ assert!(mods.command("input_claim"));
+ let said = chat(&mut mods);
+ assert!(
+ said[0].starts_with("claimed; \"R\" is not a key"),
+ "{said:?}"
+ );
+ assert!(mods.claims("minecraft:stick") && !mods.claims("minecraft:stone"));
+ let keys: Vec<_> = mods
+ .mod_keys()
+ .map(|(m, k)| (m.to_owned(), k.key.clone()))
+ .collect();
+ assert_eq!(
+ keys,
+ [("Test UI Guest".to_owned(), "key.keyboard.r".to_owned())]
+ );
+ // The game feeds the events; the mod reads them in order with `poll`.
+ let stick = HeldItem {
+ id: "minecraft:stick".into(),
+ count: 3,
+ slot: 2,
+ };
+ mods.use_start(&stick);
+ mods.key("key.keyboard.r", true);
+ mods.key("key.keyboard.t", true);
+ mods.key("key.keyboard.r", false);
+ mods.use_stop(&stick, 12);
+ assert!(mods.command("input_poll"));
+ assert_eq!(
+ chat(&mut mods),
+ [
+ "use-start minecraft:stick 0",
+ "key-down wheel",
+ "key-up wheel",
+ "use-stop minecraft:stick 12"
+ ]
+ );
+ // `held` reads the snapshot.
+ mods.set_snapshot(Snapshot {
+ held: Some(stick),
+ ..Snapshot::default()
+ });
+ assert!(mods.command("input_held"));
+ assert_eq!(chat(&mut mods), ["holding minecraft:stick x3 in 2"]);
+}
+
#[test]
fn content_only_mods_load_without_an_engine() {
let manifest = r#"
diff --git a/crates/horizon-mods/tests/ui_guest/src/lib.rs b/crates/horizon-mods/tests/ui_guest/src/lib.rs
index 1af3985a1..be2b8fcc4 100644
--- a/crates/horizon-mods/tests/ui_guest/src/lib.rs
+++ b/crates/horizon-mods/tests/ui_guest/src/lib.rs
@@ -1,11 +1,63 @@
//! A mod for the runtime's `ui` tests: `/ui_screen` opens a screen with one
//! of each widget, `/ui_settings` sets a settings page, `/ui_close` closes
//! the screen; the `ui-events` calls answer in chat so the host sees them.
+//! `/render_hud`, `/render_clear` and `/render_shake` test `render`;
+//! `/input_claim`, `/input_poll` and `/input_held` test `input`.
use horizon_mod_sdk::{
Mod, Registration, chat,
+ input::{self, Event},
+ render::{self, FillShape, ItemShape, Point, QuadShape, Rect, Shape, SpriteShape, TextShape},
ui::{self, ItemWidget, ListWidget, Screen, TextWidget, Widget},
};
+/// One of each shape, the text too long by 44 characters.
+fn shapes() -> Vec {
+ let (w, h) = render::gui_size();
+ let p = |x, y| Point { x, y };
+ vec![
+ Shape::Fill(FillShape {
+ rect: Rect {
+ x: 4.0,
+ y: 4.0,
+ w: 60.0,
+ h: 20.0,
+ },
+ color: 0x80FF0000,
+ }),
+ Shape::Quad(QuadShape {
+ corners: (
+ p(w / 2.0, 10.0),
+ p(w / 2.0 + 20.0, 30.0),
+ p(w / 2.0, 50.0),
+ p(w / 2.0 - 20.0, 30.0),
+ ),
+ color: 0xC000FF00,
+ }),
+ Shape::Sprite(SpriteShape {
+ sprite: "hud/heart/full".into(),
+ rect: Rect {
+ x: 70.0,
+ y: 4.0,
+ w: 18.0,
+ h: 18.0,
+ },
+ color: 0xFFFFFFFF,
+ }),
+ Shape::Item(ItemShape {
+ item: "minecraft:diamond".into(),
+ at: p(92.0, 4.0),
+ scale: 2.0,
+ }),
+ Shape::Text(TextShape {
+ text: format!("gui {w}x{h} {}", "x".repeat(300)),
+ at: p(w / 2.0, h / 2.0 - 30.0),
+ color: 0xFFFF55,
+ centered: true,
+ shadow: true,
+ }),
+ ]
+}
+
struct Guest;
fn widgets() -> Vec {
@@ -30,9 +82,19 @@ fn widgets() -> Vec {
impl Mod for Guest {
fn init() -> Result<(Self, Registration), String> {
- let hooks = ["ui_screen", "ui_settings", "ui_close"]
- .into_iter()
- .fold(Registration::new(), Registration::command);
+ let hooks = [
+ "ui_screen",
+ "ui_settings",
+ "ui_close",
+ "render_hud",
+ "render_clear",
+ "render_shake",
+ "input_claim",
+ "input_poll",
+ "input_held",
+ ]
+ .into_iter()
+ .fold(Registration::new(), Registration::command);
Ok((Guest, hooks))
}
fn command(&mut self, name: &str, _args: &str) -> Result, String> {
@@ -43,6 +105,37 @@ impl Mod for Guest {
}),
"ui_settings" => ui::set_settings(&widgets()),
"ui_close" => ui::close_screen(),
+ "render_hud" => render::set_hud(&shapes()),
+ "render_clear" => render::set_hud(&[]),
+ "input_claim" => {
+ input::claim_items(&["minecraft:stick".into()]);
+ input::register_key("wheel", "Open the wheel", "key.keyboard.r")?;
+ // Not a key: refused.
+ let bad = input::register_key("bad", "Bad", "R").unwrap_err();
+ chat::show(&format!("claimed; {bad}"));
+ }
+ "input_poll" => {
+ for event in input::poll() {
+ chat::show(&match event {
+ Event::UseStart(u) => format!("use-start {} {}", u.item.id, u.ticks),
+ Event::UseStop(u) => format!("use-stop {} {}", u.item.id, u.ticks),
+ Event::KeyDown(k) => format!("key-down {k}"),
+ Event::KeyUp(k) => format!("key-up {k}"),
+ });
+ }
+ }
+ "input_held" => {
+ let held = input::held().map_or("nothing".into(), |h| {
+ format!("{} x{} in {}", h.id, h.count, h.slot)
+ });
+ chat::show(&format!("holding {held}"));
+ }
+ // Clamped to (1, 10); the two after it shake nothing.
+ "render_shake" => {
+ render::shake(2.0, 99.0);
+ render::shake(0.0, 1.0);
+ render::shake(f32::NAN, 1.0);
+ }
_ => {}
}
Ok(None)
diff --git a/crates/horizon-prepare/Cargo.toml b/crates/horizon-prepare/Cargo.toml
index 3d62202bd..9bcf5464d 100644
--- a/crates/horizon-prepare/Cargo.toml
+++ b/crates/horizon-prepare/Cargo.toml
@@ -6,11 +6,15 @@ license = "MIT"
description = "Prepares Horizon's resources from the player's own Minecraft: imports and checks the pinned jars, runs the vanilla exporters, writes the resource folder"
[dependencies]
+crc32fast = "1"
regex = { version = "1", default-features = false, features = ["std", "unicode-perl"] }
sha1 = "0.10"
sha2 = "0.10"
zip = { version = "2", default-features = false, features = ["deflate"] }
+[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies]
+libc = "0.2"
+
[build-dependencies]
sha2 = "0.10"
diff --git a/crates/horizon-prepare/src/artifacts.rs b/crates/horizon-prepare/src/artifacts.rs
index 805dd80d4..a3bf945d5 100644
--- a/crates/horizon-prepare/src/artifacts.rs
+++ b/crates/horizon-prepare/src/artifacts.rs
@@ -143,31 +143,17 @@ pub(crate) fn import_copy(
algorithm: &str,
hint: &str,
) -> Result {
- fs::create_dir_all(destination.parent().unwrap()).map_err(|e| e.to_string())?;
- if destination.exists() && digest(destination, algorithm)? == checksum {
- return Ok(destination.to_path_buf());
- }
- if !source.is_file() {
- return Err(format!("Missing {}. {hint}", source.display())
- .trim()
- .to_string());
- }
- let actual = digest(source, algorithm)?;
- if actual != checksum {
- return Err(format!(
- "{} is not the pinned file: expected {algorithm} {checksum}, got {actual}",
- source.display()
- ));
- }
- let temporary = destination.with_file_name(format!(
- ".{}.partial-{}",
- destination.file_name().unwrap().to_string_lossy(),
- std::process::id()
- ));
- let copied = fs::copy(source, &temporary).and_then(|_| fs::rename(&temporary, destination));
- let _ = fs::remove_file(&temporary);
- copied.map_err(|e| format!("{}: {e}", destination.display()))?;
- Ok(destination.to_path_buf())
+ crate::files::Import::stage(source, destination, checksum, algorithm, false)
+ .map_err(|error| {
+ if !source.is_file() {
+ format!("Missing {}. {hint}", source.display())
+ .trim()
+ .to_string()
+ } else {
+ error
+ }
+ })?
+ .commit()
}
/// The pinned version JSON from the launcher's versions/ directory. The launcher's copy wins
@@ -653,9 +639,17 @@ pub struct Artifacts {
/// Reuses checked imports from this root or another installed version beside it.
pub fn reuse_imported(root: &Path, log: Log) -> Result {
+ Ok(reuse_verified(root, log)?.is_some())
+}
+
+/// Keep this preparation's verified artifacts and client libraries for the exporters.
+pub(crate) fn reuse_verified(
+ root: &Path,
+ log: Log,
+) -> Result)>, String> {
let root = fs::canonicalize(root).map_err(|e| format!("{}: {e}", root.display()))?;
let lock = Lock::read(&root)?;
- let checked = |directory: PathBuf| -> Result, String> {
+ let checked = |directory: PathBuf| -> Result<(Artifacts, Vec), String> {
let candidate = Lock {
json: lock.json.clone(),
version: lock.version.clone(),
@@ -664,17 +658,9 @@ pub fn reuse_imported(root: &Path, log: Log) -> Result {
};
let found = verify(&candidate)?;
let client = client_libraries(&candidate, &found.manifest)?;
- let mut files = vec![
- candidate.directory.join("manifest.json"),
- candidate.directory.join("jars/client.jar"),
- candidate.directory.join("jars/server.jar"),
- found.jars["server"].clone(),
- ];
- files.extend(found.libraries);
- files.extend(client);
- Ok(files)
+ Ok((found, client))
};
- if checked(lock.directory.clone()).is_ok() {
+ if let Ok((found, client_libraries)) = checked(lock.directory.clone()) {
if [
"prepared.json",
"reports/client-inventory.json",
@@ -683,8 +669,6 @@ pub fn reuse_imported(root: &Path, log: Log) -> Result {
.iter()
.any(|path| !lock.directory.join(path).is_file())
{
- let found = verify(&lock)?;
- let client_libraries = client_libraries(&lock, &found.manifest)?;
write_import_metadata(
&root,
&lock,
@@ -696,10 +680,10 @@ pub fn reuse_imported(root: &Path, log: Log) -> Result {
)?;
}
log(&format!("Using imported Minecraft {}", lock.version));
- return Ok(true);
+ return Ok(Some((found, client_libraries)));
}
let Some(versions) = root.parent() else {
- return Ok(false);
+ return Ok(None);
};
// Sibling discovery is intended for the launcher's versions/ folder.
let mut siblings: Vec<_> = fs::read_dir(versions)
@@ -711,9 +695,17 @@ pub fn reuse_imported(root: &Path, log: Log) -> Result {
siblings.sort();
for sibling in siblings {
let source = sibling.join(".local/minecraft").join(&lock.version);
- let Ok(files) = checked(source.clone()) else {
+ let Ok((found, client)) = checked(source.clone()) else {
continue;
};
+ let mut files = vec![
+ source.join("manifest.json"),
+ source.join("jars/client.jar"),
+ source.join("jars/server.jar"),
+ found.jars["server"].clone(),
+ ];
+ files.extend(found.libraries);
+ files.extend(client);
for file in files {
let relative = file.strip_prefix(&source).map_err(|e| e.to_string())?;
import_copy(
@@ -740,9 +732,9 @@ pub fn reuse_imported(root: &Path, log: Log) -> Result {
lock.version,
sibling.display()
));
- return Ok(true);
+ return Ok(Some((found, client_libraries)));
}
- Ok(false)
+ Ok(None)
}
pub fn verify(lock: &Lock) -> Result {
diff --git a/crates/horizon-prepare/src/exporters.rs b/crates/horizon-prepare/src/exporters.rs
new file mode 100644
index 000000000..35992c8b5
--- /dev/null
+++ b/crates/horizon-prepare/src/exporters.rs
@@ -0,0 +1,218 @@
+//! Only original Horizon classes are packaged. The pinned jars remain player supplied.
+use crate::*;
+
+const SETTINGS: &str = "exporters-v1 --release -proc:none -encoding UTF-8";
+const SERVER: &[&str] = &[
+ "VanillaHeightmapOracle",
+ "VanillaBlockLandingOracle",
+ "VanillaEntityAttributeOracle",
+ "VanillaPlayerHealthOracle",
+ "VanillaDamageSourceOracle",
+ "VanillaCollisionOracle",
+ "VanillaFaceVisibilityOracle",
+ "VanillaGameplayFactsExporter",
+ "VanillaEntityTypesExporter",
+ "VanillaBlockBehaviourExporter",
+ "VanillaCommandParseOracle",
+ "VanillaCreativeTabsExporter",
+ "VanillaSimpleBlockOracle",
+ "VanillaStructureBlockExporter",
+ "PrepareWorker",
+];
+const CLIENT: &[&str] = &[
+ "VanillaBlockStateBindingOracle",
+ "VanillaEntityModelExporter",
+ "VanillaMobDataExporter",
+ "PrepareWorker",
+];
+
+fn files(dir: &Path) -> Result, String> {
+ let mut result = Vec::new();
+ for entry in fs::read_dir(dir).map_err(|e| format!("{}: {e}", dir.display()))? {
+ let path = entry.map_err(|e| e.to_string())?.path();
+ if path.is_dir() {
+ result.extend(files(&path)?);
+ } else {
+ result.push(path);
+ }
+ }
+ result.sort();
+ Ok(result)
+}
+
+// research-lock pins both jars and every accepted client manifest (which pins its libraries).
+// artifacts::verify/client_libraries check those dependencies before any bundle is used.
+fn key(root: &Path) -> Result {
+ let mut hash = sha2::Sha256::new();
+ hash.update(SETTINGS);
+ // The launcher's bundled preparer may differ from the selected game's. Hash that
+ // version's compiler settings so a newer release still selects a runtime correctly.
+ let compiler = fs::read_to_string(root.join("crates/horizon-prepare/src/exporters.rs"))
+ .unwrap_or_else(|_| include_str!("exporters.rs").into());
+ hash.update(compiler.replace('\r', ""));
+ let mut sources = files(&root.join(PARITY))?;
+ sources.retain(|p| p.extension().is_some_and(|e| e == "java"));
+ sources.push(root.join("research-lock.json"));
+ for path in sources {
+ let data: Vec<_> = read(&path)?.into_iter().filter(|b| *b != b'\r').collect();
+ hash.update(format!(
+ "{}\0{}\0",
+ relative_display(&path, root)?.replace('\\', "/"),
+ data.len()
+ ));
+ hash.update(data);
+ }
+ Ok(hex(&hash.finalize()))
+}
+
+fn outputs(dir: &Path) -> Result {
+ let mut hashes = Value::Object(Vec::new());
+ for path in files(dir)? {
+ if path.extension().is_some_and(|e| e == "class") {
+ let name = relative_display(&path, dir)?.replace('\\', "/");
+ // javac's sourcepath contains our sources only; enforce that at packaging too.
+ if !name.starts_with("server/dev/horizonlab/parity/")
+ && !name.starts_with("client/dev/horizonlab/parity/")
+ {
+ return Err(format!("Non-Horizon class in exporter bundle: {name}"));
+ }
+ hashes.insert(&name, Value::Str(digest(&path, "sha256")?));
+ }
+ }
+ Ok(hashes)
+}
+
+fn valid(dir: &Path, key: &str) -> bool {
+ let Ok(manifest) = read_json(&dir.join("manifest.json")) else {
+ return false;
+ };
+ manifest.get("key").and_then(Value::as_str) == Some(key)
+ && outputs(dir).is_ok_and(|hashes| {
+ !hashes.as_object().unwrap().is_empty() && manifest.get("classes") == Some(&hashes)
+ })
+}
+
+/// Whether the version ships a matching, intact bundle and needs only a Java runtime.
+pub fn available(root: &Path) -> bool {
+ key(root).is_ok_and(|key| valid(&root.join("exporters"), &key))
+}
+
+pub(crate) fn classes(run: &Run) -> Result {
+ let key = key(&run.root)?;
+ let shipped = run.root.join("exporters");
+ if valid(&shipped, &key) {
+ return Ok(shipped);
+ }
+ let cached = run.local.join("exporters").join(&key);
+ if valid(&cached, &key) {
+ return Ok(cached);
+ }
+ // Build privately: an interrupted compiler or another preparer cannot publish partial classes.
+ let destination = cached;
+ let cached = run
+ .local
+ .join("exporters")
+ .join(format!(".{key}-{}", std::process::id()));
+ if cached.exists() {
+ fs::remove_dir_all(&cached).map_err(|e| e.to_string())?;
+ }
+ mkdir(&cached)?;
+ let jdk = java::find(run.java_major, &[run.jdk.home()])?;
+ for (side, mains, cp) in [
+ ("server", SERVER, &run.server_classpath),
+ ("client", CLIENT, &run.client_classpath),
+ ] {
+ (run.log)(&format!("Compiling {side} exporters (shared helpers once)"));
+ let classes = cached.join(side);
+ mkdir(&classes)?;
+ let log = run.log_file(&cached.join(format!("{side}.log")))?;
+ let mut args = vec![
+ "-encoding".into(),
+ "UTF-8".into(),
+ "-cp".into(),
+ cp.clone(),
+ "-sourcepath".into(),
+ arg(&run.root.join("mods/horizon/src/test/java")),
+ "-d".into(),
+ arg(&classes),
+ ];
+ args.extend(mains.iter().map(|n| arg(&run.oracle(&format!("{n}.java")))));
+ jdk.javac(run.java_major, &args, &log)?;
+ let validate = vec![
+ "-Xverify:all".into(),
+ "-cp".into(),
+ format!(
+ "{}{}{}",
+ classes.display(),
+ if cfg!(windows) { ";" } else { ":" },
+ cp
+ ),
+ "dev.horizonlab.parity.PrepareWorker".into(),
+ "validate".into(),
+ arg(&classes),
+ ];
+ // No exporter executes here: resolve every class and its signatures against pinned jars.
+ run.jdk.java(&validate, None, &log)?;
+ }
+ let manifest = Value::Object(vec![
+ ("key".into(), Value::Str(key.clone())),
+ ("classes".into(), outputs(&cached)?),
+ ]);
+ write_json(&cached.join("manifest.json"), &manifest)?;
+ if valid(&destination, &key) {
+ fs::remove_dir_all(&cached).map_err(|e| e.to_string())?;
+ } else {
+ if destination.exists() {
+ fs::remove_dir_all(&destination).map_err(|e| e.to_string())?;
+ }
+ fs::rename(&cached, &destination).map_err(|e| e.to_string())?;
+ }
+ Ok(destination)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn bundles_invalidate_sources_pins_and_damaged_classes() {
+ let root = std::env::temp_dir().join(format!("prepare-bundle-{}", std::process::id()));
+ let source = root.join(PARITY).join("VanillaHeightmapOracle.java");
+ mkdir(source.parent().unwrap()).unwrap();
+ write(&source, b"original\n").unwrap();
+ let compiler = root.join("crates/horizon-prepare/src/exporters.rs");
+ mkdir(compiler.parent().unwrap()).unwrap();
+ write(&compiler, b"--release 25 -proc:none").unwrap();
+ write(&root.join("research-lock.json"), b"pins").unwrap();
+ let original = key(&root).unwrap();
+ write(&compiler, b"--release 26 -proc:none").unwrap();
+ assert_ne!(key(&root).unwrap(), original);
+ write(&compiler, b"--release 25 -proc:none").unwrap();
+ write(&source, b"original\r\n").unwrap();
+ assert_eq!(key(&root).unwrap(), original);
+ write(&source, b"changed").unwrap();
+ assert_ne!(key(&root).unwrap(), original);
+ write(&source, b"original\n").unwrap();
+ write(&root.join("research-lock.json"), b"new dependencies").unwrap();
+ assert_ne!(key(&root).unwrap(), original);
+ write(&root.join("research-lock.json"), b"pins").unwrap();
+ let bundle = root.join("exporters");
+ let class = bundle.join("server/dev/horizonlab/parity/Helper.class");
+ mkdir(class.parent().unwrap()).unwrap();
+ write(&class, b"compiled").unwrap();
+ write_json(
+ &bundle.join("manifest.json"),
+ &Value::Object(vec![
+ ("key".into(), Value::Str(original)),
+ ("classes".into(), outputs(&bundle).unwrap()),
+ ]),
+ )
+ .unwrap();
+ assert!(available(&root));
+ write(&class, b"damaged").unwrap();
+ assert!(!available(&root));
+ fs::remove_file(&class).unwrap();
+ assert!(!available(&root));
+ fs::remove_dir_all(root).unwrap();
+ }
+}
diff --git a/crates/horizon-prepare/src/files.rs b/crates/horizon-prepare/src/files.rs
new file mode 100644
index 000000000..484c7bf7b
--- /dev/null
+++ b/crates/horizon-prepare/src/files.rs
@@ -0,0 +1,180 @@
+//! Independent copies, using filesystem cloning when available. Never hard links: exporters
+//! rewrite prepared resources in place. Checked imports publish only after verifying the copy.
+
+use crate::{digest, hex};
+use sha2::Digest;
+use std::fs;
+use std::io::{self, Read, Write};
+use std::path::{Path, PathBuf};
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+fn clone_file(from: &Path, to: &Path) -> io::Result<()> {
+ #[cfg(target_os = "linux")]
+ {
+ use std::os::fd::AsRawFd;
+ let source = fs::File::open(from)?;
+ let target = fs::File::create(to)?;
+ // SAFETY: FICLONE takes a source fd, both descriptors stay open for the call.
+ if unsafe { libc::ioctl(target.as_raw_fd(), libc::FICLONE, source.as_raw_fd()) } == 0 {
+ return Ok(());
+ }
+ Err(io::Error::last_os_error())
+ }
+ #[cfg(target_os = "macos")]
+ {
+ use std::ffi::CString;
+ use std::os::unix::ffi::OsStrExt;
+ let path = |p: &Path| {
+ CString::new(p.as_os_str().as_bytes())
+ .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))
+ };
+ let (source, target) = (path(from)?, path(to)?);
+ // SAFETY: both paths are valid NUL-terminated strings for the duration of the call.
+ if unsafe { libc::clonefile(source.as_ptr(), target.as_ptr(), 0) } == 0 {
+ return Ok(());
+ }
+ Err(io::Error::last_os_error())
+ }
+ #[cfg(not(any(target_os = "linux", target_os = "macos")))]
+ {
+ let _ = (from, to);
+ Err(io::ErrorKind::Unsupported.into())
+ }
+}
+
+/// Copies an independent file, attempting a reflink first. The destination may be replaced.
+pub fn clone_or_copy(from: &Path, to: &Path) -> io::Result<()> {
+ clone_file(from, to).or_else(|_| fs::copy(from, to).map(|_| ()))
+}
+
+fn copy_hash(from: &Path, to: &Path) -> io::Result {
+ let mut source = fs::File::open(from)?;
+ let mut target = fs::File::create(to)?;
+ let mut hash = D::new();
+ let mut buffer = vec![0; 64 * 1024];
+ loop {
+ let n = source.read(&mut buffer)?;
+ if n == 0 {
+ return Ok(hex(&hash.finalize()));
+ }
+ target.write_all(&buffer[..n])?;
+ hash.update(&buffer[..n]);
+ }
+}
+
+/// A verified replacement staged beside its destination. Dropping an unfinished set cleans
+/// every temporary file, so a corrupt late panorama face cannot replace earlier placeholders.
+pub(crate) struct Import {
+ temporary: Option,
+ destination: PathBuf,
+}
+
+impl Import {
+ pub(crate) fn stage(
+ source: &Path,
+ destination: &Path,
+ checksum: &str,
+ algorithm: &str,
+ verified: bool,
+ ) -> Result {
+ let mut import = Self {
+ temporary: None,
+ destination: destination.into(),
+ };
+ if verified || (destination.is_file() && digest(destination, algorithm)? == checksum) {
+ return Ok(import);
+ }
+ fs::create_dir_all(destination.parent().unwrap()).map_err(|e| e.to_string())?;
+ static COPIES: AtomicUsize = AtomicUsize::new(0);
+ let temporary = destination.with_file_name(format!(
+ ".{}.partial-{}-{}",
+ destination.file_name().unwrap().to_string_lossy(),
+ std::process::id(),
+ COPIES.fetch_add(1, Ordering::Relaxed)
+ ));
+ import.temporary = Some(temporary.clone());
+ let actual = if clone_file(source, &temporary).is_ok() {
+ digest(&temporary, algorithm)?
+ } else {
+ match algorithm {
+ "sha1" => copy_hash::(source, &temporary),
+ _ => copy_hash::(source, &temporary),
+ }
+ .map_err(|e| format!("{}: {e}", source.display()))?
+ };
+ if actual != checksum {
+ return Err(format!(
+ "{} is not the pinned file: expected {algorithm} {checksum}, got {actual}",
+ source.display()
+ ));
+ }
+ Ok(import)
+ }
+
+ pub(crate) fn commit(mut self) -> Result {
+ if let Some(temporary) = &self.temporary {
+ fs::rename(temporary, &self.destination)
+ .map_err(|e| format!("{}: {e}", self.destination.display()))?;
+ }
+ self.temporary = None;
+ Ok(self.destination.clone())
+ }
+}
+
+impl Drop for Import {
+ fn drop(&mut self) {
+ if let Some(temporary) = &self.temporary {
+ let _ = fs::remove_file(temporary);
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn copies_are_independent_and_checked_before_publication() {
+ let dir = std::env::temp_dir().join(format!("horizon-file-copy-{}", std::process::id()));
+ fs::create_dir_all(&dir).unwrap();
+ let source = dir.join("source");
+ let target = dir.join("target");
+ let data = vec![42; 200_000]; // More than one streaming buffer.
+ fs::write(&source, &data).unwrap();
+ for algorithm in ["sha1", "sha256"] {
+ let checksum = digest(&source, algorithm).unwrap();
+ Import::stage(&source, &target, &checksum, algorithm, false)
+ .unwrap()
+ .commit()
+ .unwrap();
+ assert_eq!(digest(&target, algorithm).unwrap(), checksum);
+ fs::write(&target, b"old destination").unwrap();
+ assert_eq!(fs::read(&source).unwrap(), data);
+ assert!(Import::stage(&source, &target, "bad hash", algorithm, false).is_err());
+ assert_eq!(fs::read(&target).unwrap(), b"old destination");
+ // Explicitly exercise ordinary streaming copies even on a reflink filesystem.
+ let streamed = if algorithm == "sha1" {
+ copy_hash::(&source, &target).unwrap()
+ } else {
+ copy_hash::(&source, &target).unwrap()
+ };
+ assert_eq!(streamed, checksum);
+ assert_eq!(fs::read(&target).unwrap(), data);
+ }
+ clone_or_copy(&source, &target).unwrap();
+ fs::write(&target, b"rewrite").unwrap();
+ assert_eq!(fs::read(&source).unwrap(), data);
+ let staged = Import::stage(
+ &source,
+ &target,
+ &digest(&source, "sha1").unwrap(),
+ "sha1",
+ false,
+ )
+ .unwrap();
+ assert_eq!(fs::read_dir(&dir).unwrap().count(), 3);
+ drop(staged);
+ assert_eq!(fs::read_dir(&dir).unwrap().count(), 2);
+ fs::remove_dir_all(dir).unwrap();
+ }
+}
diff --git a/crates/horizon-prepare/src/java.rs b/crates/horizon-prepare/src/java.rs
index d46cb9428..6343d8433 100644
--- a/crates/horizon-prepare/src/java.rs
+++ b/crates/horizon-prepare/src/java.rs
@@ -1,4 +1,4 @@
-//! The JDK that compiles and runs the vanilla exporters. They call Mojang's own code, so they
+//! Java discovery and execution for the vanilla exporters. They call Mojang's own code, so they
//! stay Java; everything around them is here.
use std::ffi::OsStr;
@@ -15,7 +15,7 @@ pub struct Jdk {
const TIMEOUT: Duration = Duration::from_secs(3600);
/// Exporters and JDK probes write to pipes or log files: on Windows they open no terminal.
-fn command(program: impl AsRef) -> Command {
+pub(crate) fn command(program: impl AsRef) -> Command {
let command = Command::new(program);
#[cfg(windows)]
let command = {
@@ -56,9 +56,9 @@ fn without_verbatim(path: PathBuf) -> PathBuf {
/// Resolve compiler symlinks before picking its sibling runtime. The first `java` on the
/// PATH may be an older, runtime-only install; it must not be paired with a newer `javac`.
-fn path_jdks(path: &OsStr) -> Vec {
+fn path_homes(path: &OsStr, tool: &str) -> Vec {
std::env::split_paths(path)
- .map(|dir| executable(&dir, "javac"))
+ .map(|dir| executable(&dir, tool))
.filter(|javac| javac.is_file())
.map(real_path)
.filter_map(|javac| {
@@ -162,7 +162,7 @@ fn jdk_parents() -> Vec {
/// The JDKs (folders with `bin/javac`) one level below `parents`; a macOS bundle's home is
/// its `Contents/Home`. Those named for `major` come first, then the others newest name first.
-fn installed_jdks(parents: &[PathBuf], major: i64) -> Vec {
+fn installed_jdks(parents: &[PathBuf], major: i64, tool: &str) -> Vec {
let mut homes: Vec = parents
.iter()
.filter_map(|parent| fs::read_dir(parent).ok())
@@ -176,7 +176,7 @@ fn installed_jdks(parents: &[PathBuf], major: i64) -> Vec {
entry.path()
}
})
- .filter(|home| executable(&home.join("bin"), "javac").is_file())
+ .filter(|home| executable(&home.join("bin"), tool).is_file())
.collect();
let named = |home: &Path| {
home.iter().any(|part| {
@@ -191,8 +191,7 @@ fn installed_jdks(parents: &[PathBuf], major: i64) -> Vec {
}
/// The Java runtimes the official launcher downloaded into `runtime` (a game directory's
-/// `runtime` folder: `///`, a macOS one in `jre.bundle`). Mojang's
-/// runtimes are whole JDKs, javac included, so a player who has run Minecraft has one already.
+/// `runtime` folder: `///`, a macOS one in `jre.bundle`).
pub fn minecraft_runtimes(runtime: &Path) -> Vec {
let children = |dir: &Path| -> Vec {
let mut paths: Vec = fs::read_dir(dir)
@@ -206,7 +205,7 @@ pub fn minecraft_runtimes(runtime: &Path) -> Vec {
for platform in children(&component) {
let home = platform.join(component.file_name().unwrap_or_default());
for home in [home.join("jre.bundle/Contents/Home"), home] {
- if executable(&home.join("bin"), "javac").is_file() {
+ if executable(&home.join("bin"), "java").is_file() {
homes.push(home);
break;
}
@@ -221,8 +220,18 @@ pub fn minecraft_runtimes(runtime: &Path) -> Vec {
/// `/usr/libexec/java_home -v ` reports, else the homes in `more` (the launcher's pick,
/// Minecraft's own runtimes), else the folders installers use (`jdk_parents`).
pub fn find(major: i64, more: &[PathBuf]) -> Result {
+ find_tools(major, more, true)
+}
+
+/// Finds a compatible runtime; precompiled exporters do not need javac.
+pub fn find_runtime(major: i64, more: &[PathBuf]) -> Result {
+ find_tools(major, more, false)
+}
+
+fn find_tools(major: i64, more: &[PathBuf], compiler: bool) -> Result {
+ let tool = if compiler { "javac" } else { "java" };
let need = format!(
- "Java {major} is needed (a JDK with javac, e.g. Eclipse Temurin {major}). \
+ "Java {major} is needed ({tool}, e.g. Eclipse Temurin {major}). \
Install it, or set JAVA_HOME to it, and try again."
);
let mut candidates = Vec::new();
@@ -235,7 +244,7 @@ pub fn find(major: i64, more: &[PathBuf]) -> Result {
));
}
if let Some(path) = std::env::var_os("PATH") {
- for home in path_jdks(&path) {
+ for home in path_homes(&path, tool) {
let bin = home.join("bin");
candidates.push((executable(&bin, "javac"), executable(&bin, "java"), "PATH"));
}
@@ -265,7 +274,7 @@ pub fn find(major: i64, more: &[PathBuf]) -> Result {
"Minecraft's Java",
));
}
- for home in installed_jdks(&jdk_parents(), major) {
+ for home in installed_jdks(&jdk_parents(), major, tool) {
let bin = home.join("bin");
candidates.push((
executable(&bin, "javac"),
@@ -275,15 +284,16 @@ pub fn find(major: i64, more: &[PathBuf]) -> Result {
}
let mut problems = Vec::new();
for (javac, java, from) in candidates {
- if !javac.is_file() || !java.is_file() {
+ if (compiler && !javac.is_file()) || !java.is_file() {
problems.push(format!(
- "{from} ({}) has no javac and java",
+ "{from} ({}) has no compatible Java tools",
javac.parent().unwrap().display()
));
continue;
}
let checked = [(&javac, "javac"), (&java, "java")]
.into_iter()
+ .filter(|(_, name)| compiler || *name == "java")
.try_for_each(|(program, name)| {
let found = tool_major(program, name)?;
if i64::from(found) >= major {
@@ -298,7 +308,7 @@ pub fn find(major: i64, more: &[PathBuf]) -> Result {
}
}
if problems.is_empty() {
- Err(format!("{need} No javac was found."))
+ Err(format!("{need} No {tool} was found."))
} else {
Err(format!("{need} Found: {}.", problems.join("; ")))
}
@@ -317,7 +327,7 @@ pub fn classpath<'a>(paths: impl IntoIterator- ) -> String {
impl Jdk {
/// The JDK's home, the folder above `bin` (what `JAVA_HOME` names).
pub fn home(&self) -> PathBuf {
- self.javac
+ self.java
.parent()
.and_then(Path::parent)
.map(Path::to_path_buf)
@@ -424,6 +434,7 @@ mod tests {
dir.join("missing"),
],
25,
+ "javac",
);
assert_eq!(
found,
@@ -445,7 +456,7 @@ mod tests {
let make = |home: PathBuf, javac: bool| {
fs::create_dir_all(home.join("bin")).unwrap();
if javac {
- fs::write(executable(&home.join("bin"), "javac"), b"").unwrap();
+ fs::write(executable(&home.join("bin"), "java"), b"").unwrap();
}
home
};
diff --git a/crates/horizon-prepare/src/lib.rs b/crates/horizon-prepare/src/lib.rs
index 5ea1121df..5d71e8448 100644
--- a/crates/horizon-prepare/src/lib.rs
+++ b/crates/horizon-prepare/src/lib.rs
@@ -11,12 +11,16 @@
//! run, keyed by the sha256 of their inputs and outputs recorded in the manifest, as before.
pub mod artifacts;
+pub mod exporters;
+mod files;
mod inventory;
pub mod java;
pub mod pyjson;
mod sounds;
mod survival;
+mod worker;
+pub use files::clone_or_copy;
pub use sounds::{fonts, languages, packs, panorama, sounds, BUILT_IN_PACKS};
use pyjson::Value;
@@ -74,7 +78,7 @@ pub fn sources_sha256(crate_dir: &Path) -> Option
{
pub struct Options {
/// The version folder (a release folder, or a checkout): research-lock.json, the exporters'
- /// Java sources, and `.local/` where everything is written.
+ /// Java sources, optional precompiled `exporters/`, and `.local/` where everything is written.
pub root: PathBuf,
/// Import the pinned game files from the player's own copy first (`--prepare`).
pub import: bool,
@@ -83,7 +87,7 @@ pub struct Options {
pub minecraft_dir: Option,
/// The pinned server jar the player downloaded (`--server-jar`, else HORIZON_SERVER_JAR).
pub server_jar: Option,
- /// The JDK to try after JAVA_HOME and the PATH (the launcher's pick, e.g. Minecraft's
+ /// The Java runtime to try after JAVA_HOME and the PATH (the launcher's pick, e.g. Minecraft's
/// own Java it downloaded from Mojang).
pub java_home: Option,
}
@@ -302,6 +306,8 @@ struct Run<'a> {
server_classpath: String,
/// The client jar and its libraries: the classpath of the client-side exporters.
client_classpath: String,
+ bundles: std::cell::OnceCell,
+ workers: std::cell::RefCell<[Option; 2]>,
previous: Value,
origin: Value,
log: Log<'a>,
@@ -310,6 +316,13 @@ struct Run<'a> {
const PARITY: &str = "mods/horizon/src/test/java/dev/horizonlab/parity";
impl Run<'_> {
+ fn bundle(&self) -> Result<&Path, String> {
+ if self.bundles.get().is_none() {
+ self.bundles.set(exporters::classes(self)?).unwrap();
+ }
+ Ok(self.bundles.get().unwrap())
+ }
+
fn oracle(&self, name: &str) -> PathBuf {
self.root.join(PARITY).join(name)
}
@@ -342,33 +355,31 @@ impl Run<'_> {
fs::File::create(path).map_err(|e| format!("{}: {e}", path.display()))
}
- /// `javac --release -proc:none -cp [extra] -d `.
- fn compile(
- &self,
- classpath: &str,
- extra: &[String],
- classes: &Path,
- sources: &[&Path],
- log: &fs::File,
- ) -> Result<(), String> {
- let mut args = vec!["-cp".to_string(), classpath.to_string()];
- args.extend_from_slice(extra);
- args.push("-d".into());
- args.push(classes.display().to_string());
- args.extend(sources.iter().map(|s| s.display().to_string()));
- self.jdk.javac(self.java_major, &args, log)
- }
-
/// `java -Xmx -cp : dev.horizonlab.parity. `.
fn run(
&self,
heap: &str,
- classes: &Path,
classpath: &str,
main: &str,
args: &[String],
log: &fs::File,
+ log_path: &Path,
) -> Result<(), String> {
+ let side = usize::from(classpath == self.client_classpath);
+ let classes = self
+ .bundle()?
+ .join(if side == 1 { "client" } else { "server" });
+ if !matches!(
+ main,
+ "VanillaCommandParseOracle" | "VanillaCreativeTabsExporter"
+ ) && std::env::var_os("HORIZON_PREPARE_ISOLATED").is_none()
+ {
+ let mut workers = self.workers.borrow_mut();
+ if workers[side].is_none() {
+ workers[side] = Some(worker::Worker::new(self, &classes, classpath)?);
+ }
+ return workers[side].as_mut().unwrap().run(main, args, log_path);
+ }
let separator = if cfg!(windows) { ";" } else { ":" };
let mut all = vec![
format!("-Xmx{heap}"),
@@ -380,7 +391,7 @@ impl Run<'_> {
self.jdk.java(&all, None, log)
}
- /// Compiles `sources` into `classes` and runs each of `mains` with `args`, their output in
+ /// Runs each exporter from the verified shared bundle, with output in
/// the log at `log`: the shape of every exporter step.
#[allow(clippy::too_many_arguments)]
fn export(
@@ -388,7 +399,7 @@ impl Run<'_> {
what: &str,
classpath: &str,
classes: &Path,
- sources: &[&Path],
+ _sources: &[&Path],
mains: &[&str],
heap: &str,
args: &[String],
@@ -398,10 +409,8 @@ impl Run<'_> {
mkdir(classes)?;
let file = self.log_file(log)?;
let fail = |e: String| format!("{e} while exporting {what}; see {}", log.display());
- self.compile(classpath, &[], classes, sources, &file)
- .map_err(fail)?;
for main in mains {
- self.run(heap, classes, classpath, main, args, &file)
+ self.run(heap, classpath, main, args, &file, log)
.map_err(fail)?;
}
Ok(())
@@ -410,6 +419,15 @@ impl Run<'_> {
/// Prepares the resources; returns their folder (`.local/horizon-client/