Merge main into optifine/emf-animations

This commit is contained in:
Ludovic Marie committed 2026-11-01 15:59:03 +01:00
commit 84bf62028b
205 files changed
+8596 -561

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
name: mojang republish
# Hourly: does Mojang's version manifest list a pinned-version JSON that research-lock.json does not know?
# If so, one issue "Mojang republished <version>: <sha8>" with the hashes and a lock snippet
# (tools/ci/mojang_republish.py). Reads only Mojang's public manifest.
# gh workflow run "mojang republish" -f dry_run=true
on:
schedule:
- cron: '23 * * * *' # hourly: a republished JSON breaks horizon-prepare for every new player until accepted
workflow_dispatch:
inputs:
dry_run:
description: Only print what it would do with issues
type: boolean
default: false
permissions:
contents: read
issues: write
concurrency:
group: mojang-republish
cancel-in-progress: false
jobs:
check:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- name: Check the manifest
env:
GH_TOKEN: ${{ github.token }}
run: python3 tools/ci/mojang_republish.py ${{ inputs.dry_run && '--dry-run' || '' }}
Generated
+1
View File
@@ -1479,6 +1479,7 @@ dependencies = [
"horizon-client-core",
"horizon-core",
"horizon-world",
"log",
"serde",
"serde_json",
]
@@ -6,5 +6,5 @@ authors = []
# A content mod: blocks, items and mob effects from data/ and assets/, no code (API 0.2).
api = "0.2"
side = "both"
# registries: "May add blocks, items and mob effects to your worlds".
# registries: "May add blocks, items, mob effects and entity types to your worlds".
capabilities = ["registries"]
@@ -1179,6 +1179,15 @@ impl Audio {
self.sync_volumes();
self.engine.tick(paused, ctx, &mut self.backend);
}
/// The `uiSoundReserve` extra: positional sounds leave
/// `sound_engine::HEARD_RESERVE` static channels free (off: vanilla's pool).
pub fn set_listener_reserve(&mut self, on: bool) {
self.engine.listener_reserve = if on {
crate::sound_engine::HEARD_RESERVE
} else {
0
};
}
/// `SoundManager.pauseAllExcept(MUSIC, UI)`.
pub fn pause(&mut self) {
self.engine.pause_all_except(
+121
View File
@@ -387,6 +387,52 @@ impl Biomes {
}
}
/// A biome's colours as a mod may change them (`native_biome_colors`), each
/// 0xRRGGBB: what tints grass (before the dark forest's modifier; the swamp's
/// noise colours replace it), leaves and vines, dry foliage (leaf litter) and
/// water.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BiomeColors {
pub grass: u32,
pub foliage: u32,
pub dry_foliage: u32,
pub water: u32,
}
impl Biomes {
/// This table with each biome's colours passed through `f`, given the
/// biome's name (`minecraft:plains`) and its colours; None when `f`
/// changed none. Bits past 0xFFFFFF are dropped.
pub fn with_colors(&self, mut f: impl FnMut(&str, &mut BiomeColors)) -> Option<Self> {
let mut out = self.clone();
let mut changed = false;
for b in &mut out.list {
let before = BiomeColors {
grass: b.grass,
foliage: b.foliage,
dry_foliage: b.dry_foliage,
water: b.water,
};
let mut colors = before;
f(&b.name, &mut colors);
let after = BiomeColors {
grass: colors.grass & 0xFF_FFFF,
foliage: colors.foliage & 0xFF_FFFF,
dry_foliage: colors.dry_foliage & 0xFF_FFFF,
water: colors.water & 0xFF_FFFF,
};
if after != before {
changed = true;
b.grass = after.grass;
b.foliage = after.foliage;
b.dry_foliage = after.dry_foliage;
b.water = after.water;
}
}
changed.then_some(out)
}
}
/// A 24-bit colour as sRGB components in 0..1.
pub fn rgb(c: u32) -> [f32; 3] {
[16, 8, 0].map(|shift| (c >> shift & 255) as f32 / 255.0)
@@ -534,6 +580,81 @@ mod pack_tests {
}
}
#[cfg(test)]
mod color_tests {
use super::*;
use serde_json::json;
fn biomes() -> Biomes {
let maps = Colormaps {
grass: Vec::new(),
foliage: Vec::new(),
dry: Vec::new(),
};
let effects = |grass: u32, modifier: &str| {
json!({"effects": {"grass_color": grass, "foliage_color": 0x112233,
"dry_foliage_color": 0x445566, "water_color": 0x3F76E4,
"grass_color_modifier": modifier}})
};
let list = vec![
BiomeInfo::from_json("minecraft:plains".into(), &effects(0x91BD59, "none"), &maps),
BiomeInfo::from_json(
"minecraft:dark_forest".into(),
&effects(0x91BD59, "dark_forest"),
&maps,
),
];
Biomes {
builtin: Arc::new(list.clone()),
index: list
.iter()
.enumerate()
.map(|(i, b)| (b.name.clone(), i as u16))
.collect(),
list,
info_noise: SimplexNoise::with_skipped_offsets(&mut LegacyRandom::new(2345), true),
colormaps: Arc::new(maps),
packs: BTreeMap::new(),
}
}
#[test]
fn a_change_reaches_the_tints_the_mesher_reads() {
let autumn = biomes()
.with_colors(|name, c| {
c.foliage = 0xD2691E;
if name == "minecraft:plains" {
c.grass = 0xBFA055;
}
})
.expect("changed");
let plains = autumn.id("minecraft:plains");
assert_eq!(autumn.grass(plains, 0.0, 0.0), 0xBFA055);
assert_eq!(autumn.get(plains).foliage, 0xD2691E);
assert_eq!(autumn.get(plains).dry_foliage, 0x445566);
assert_eq!(autumn.get(plains).water, 0x3F76E4);
// The dark forest's modifier still darkens its (unchanged) grass:
// ((0x91BD59 & 0xFEFEFE) + 0x28340A) >> 1.
let dark = autumn.id("minecraft:dark_forest");
assert_eq!(autumn.grass(dark, 0.0, 0.0), 0x5C7831);
assert_eq!(autumn.get(dark).foliage, 0xD2691E);
}
#[test]
fn no_change_gives_none_and_alpha_bits_are_dropped() {
assert!(biomes().with_colors(|_, _| {}).is_none());
assert!(
biomes()
.with_colors(|_, c| c.water |= 0xFF00_0000)
.is_none()
);
let blue = biomes()
.with_colors(|_, c| c.water = 0xFF00_00FF)
.expect("changed");
assert_eq!(blue.get(0).water, 0x0000FF);
}
}
#[cfg(test)]
mod blend_tests {
use super::*;
+3 -1
View File
@@ -205,7 +205,9 @@ impl Brewing {
}
// The gate answers the merged file, as the crafting loader's does:
// a false-conditioned pack file leaves the lower one gone too.
sources.retain(|(_, v)| conditions_hold(v));
// The data's recipe filters, over the final set.
let filters = horizon_items::recipe_filters::RecipeFilters::load(root, overlay);
sources.retain_mut(|(id, v)| conditions_hold(v) && filters.apply(id, v));
sources.sort_by(|a, b| a.0.cmp(&b.0));
let mut recipes = Vec::new();
for (id, v) in sources {
+115 -4
View File
@@ -501,6 +501,19 @@ impl Camera {
entity: &CameraEntity,
partial: f32,
camera_type: CameraType,
clip: impl FnMut([f64; 3], [f64; 3]) -> Option<[f64; 3]>,
) {
self.align_with_entity_distance(entity, partial, camera_type, |distance| distance, clip);
}
/// Adjust the detached distance after entity/mount scaling, before collision clipping.
/// Only finite overrides in 0..=1024 blocks are accepted (within the game's ray budget).
/// First-person views do not call `adjust_distance`.
pub fn align_with_entity_distance(
&mut self,
entity: &CameraEntity,
partial: f32,
camera_type: CameraType,
adjust_distance: impl FnOnce(f32) -> f32,
mut clip: impl FnMut([f64; 3], [f64; 3]) -> Option<[f64; 3]>,
) {
self.set_rotation(entity.view_y_rot(partial), entity.view_x_rot(partial));
@@ -528,10 +541,14 @@ impl Camera {
mount_scale = s;
mount_distance = d as f32;
}
let zoom = self.max_zoom(
java_max(scale * distance, mount_scale * mount_distance),
&mut clip,
);
let vanilla = java_max(scale * distance, mount_scale * mount_distance);
let adjusted = adjust_distance(vanilla);
let distance = if adjusted.is_finite() && (0.0..=1024.0).contains(&adjusted) {
adjusted
} else {
vanilla
};
let zoom = self.max_zoom(distance, &mut clip);
self.move_by(-zoom, 0.0, 0.0);
} else if let Some(LivingCamera {
sleeping: Some(bed),
@@ -969,6 +986,100 @@ mod tests {
assert!(camera.position[2] > -1.1 && camera.position[2] < -0.9);
}
#[test]
fn detached_distance_override_keeps_collision_clipping() {
let entity = CameraEntity {
position: [0.0; 3],
previous: [0.0; 3],
x_rot: 0.0,
y_rot: 0.0,
x_rot_o: 0.0,
y_rot_o: 0.0,
eye_height: 1.62,
living: Some(LivingCamera {
scale: 2.0,
camera_distance: 3.0,
sleeping: None,
dead: None,
}),
mount: Some((3.0, 4.0)),
};
for kind in [CameraType::ThirdPersonBack, CameraType::ThirdPersonFront] {
let mut camera = Camera::default();
let mut calls = 0;
let mut rays = 0;
let z = if kind.is_mirrored() { 1.0 } else { -1.0 };
camera.align_with_entity_distance(
&entity,
1.0,
kind,
|distance| {
assert_eq!(distance, 12.0);
8.0
},
|_, _| None,
);
assert!((camera.position[2] - z * 8.0).abs() < 1e-5);
camera.align_with_entity_distance(
&entity,
1.0,
kind,
|distance| {
calls += 1;
assert_eq!(distance, 12.0);
8.0
},
|_, _| {
rays += 1;
Some([0.0, 0.0, z])
},
);
assert_eq!((calls, rays), (1, 8));
assert!((camera.position[2] - z).abs() < 0.2);
}
}
#[test]
fn detached_distance_rejects_invalid_values_and_accepts_zero() {
let entity = CameraEntity {
position: [0.0; 3],
previous: [0.0; 3],
x_rot: 0.0,
y_rot: 0.0,
x_rot_o: 0.0,
y_rot_o: 0.0,
eye_height: 1.62,
living: None,
mount: None,
};
for distance in [f32::NAN, f32::INFINITY, -1.0, 1025.0] {
let mut camera = Camera::default();
camera.align_with_entity_distance(
&entity,
1.0,
CameraType::ThirdPersonBack,
|_| distance,
|_, _| None,
);
assert!((camera.position[2] + 4.0).abs() < 1e-6);
}
let mut camera = Camera::default();
camera.align_with_entity_distance(
&entity,
1.0,
CameraType::ThirdPersonBack,
|_| 0.0,
|_, _| None,
);
assert_eq!(camera.position, camera.eye);
camera.align_with_entity_distance(
&entity,
1.0,
CameraType::FirstPerson,
|_| panic!("first person has no detached distance"),
|_, _| None,
);
assert_eq!(camera.position, camera.eye);
}
#[test]
fn fov_modifier_combines_speed_flight_and_bow() {
let mut p = FovPlayer {
flying: false,
+36
View File
@@ -195,6 +195,14 @@ fn read(path: &str) -> Option<String> {
std::fs::read_to_string(path).ok()
}
fn windows_processor_name(query: &str) -> Option<String> {
query.lines().find_map(|line| {
let (_, value) = line.split_once("REG_SZ")?;
let value = value.trim();
(!value.is_empty()).then(|| value.to_owned())
})
}
/// `PRETTY_NAME` of /etc/os-release and the kernel, or the system's own version string.
fn operating_system() -> String {
let arch = std::env::consts::ARCH;
@@ -235,6 +243,18 @@ pub fn cpu() -> String {
})
} else if cfg!(target_os = "macos") {
command_output("sysctl", &["-n", "machdep.cpu.brand_string"])
} else if cfg!(windows) {
command_output(
"reg",
&[
"query",
r"HKLM\HARDWARE\DESCRIPTION\System\CentralProcessor\0",
"/v",
"ProcessorNameString",
],
)
.and_then(|query| windows_processor_name(&query))
.or_else(|| std::env::var("PROCESSOR_IDENTIFIER").ok())
} else {
std::env::var("PROCESSOR_IDENTIFIER").ok()
};
@@ -636,6 +656,22 @@ pub fn debug_crash_after() -> Option<std::time::Duration> {
mod tests {
use super::*;
#[test]
fn windows_registry_cpu_name_is_read_without_the_registry_columns() {
let query = r#"
HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0
ProcessorNameString REG_SZ 13th Gen Intel(R) Core(TM) i7-13700F
"#;
assert_eq!(
windows_processor_name(query).as_deref(),
Some("13th Gen Intel(R) Core(TM) i7-13700F")
);
assert_eq!(
windows_processor_name("ProcessorNameString REG_SZ "),
None
);
}
#[test]
fn a_release_reports_its_built_in_version() {
// release.yml builds with HORIZON_VERSION (docs/versioning.md): `v0.2.3`, wherever it is installed.
+5 -3
View File
@@ -575,7 +575,7 @@ pub struct DataStore {
/// `item_modifier` files, for `/item modify`.
pub item_modifiers: BTreeMap<String, serde_json::Value>,
/// The content tables' raw files from the packs after the built-in one:
/// `recipe/`, `loot_table/`, `advancement/`, `dialog/` and `tags/…/` files, by
/// `recipe/`, `recipe_filter/`, `loot_table/`, `advancement/`, `dialog/` and `tags/…/` files, by
/// `<kind-dir>/<ns>:<path>`, each id's files in pack order. The tables
/// merge them over what they read from the resource root.
pub files: BTreeMap<String, Vec<Vec<u8>>>,
@@ -583,7 +583,7 @@ pub struct DataStore {
pub errors: Vec<(String, String)>,
}
/// `<kind-dir>/<ns>:<path>` for a pack file the content tables read:
/// `recipe/`, `loot_table/`, `advancement/`, `trade_set/`,
/// `recipe/`, `recipe_filter/`, `loot_table/`, `advancement/`, `trade_set/`,
/// `villager_trade/`, `dialog/` or `tags/<sub>/`, where the id keeps the sub
/// path so a nested tag kind stays addressable.
fn content_id(file: &str) -> Option<String> {
@@ -591,7 +591,8 @@ fn content_id(file: &str) -> Option<String> {
let (ns, rest) = rest.split_once('/')?;
let (kind, rest) = rest.split_once('/')?;
let kind = match kind {
"recipe" | "loot_table" | "advancement" | "trade_set" | "villager_trade" | "dialog" => kind,
"recipe" | "recipe_filter" | "loot_table" | "advancement" | "trade_set"
| "villager_trade" | "dialog" => kind,
// A tag id is `{ns}:{path-under-tags}` (`tags/worldgen/biome/
// test:sub/mytag`), the whole path after the namespace, so nested
// registries keep their directory (`TagLoader`'s id shape).
@@ -628,6 +629,7 @@ impl DataStore {
"loot_table",
"predicate",
"recipe",
"recipe_filter",
"tags",
"trade_set",
"villager_trade",
@@ -320,6 +320,13 @@ fn shadow_color(in: Out) -> vec4<f32> {
}
fn celestial_color(in: Out) -> vec4<f32> {
// RenderPipelines.DRAGON_RAYS / rendertype_lightning.fsh: untextured
// vertex colour, with fog fading both RGB and alpha before additive blending.
if in.overlay == 37u {
let fog = max(linear_fog(in.spherical, globals.fog.x, globals.fog.y),
linear_fog(in.cylindrical, globals.fog.z, globals.fog.w));
return in.color * (1.0 - fog);
}
return textureSample(atlas, atlas_sampler, in.uv) * in.color * in.light_color;
}
@fragment fn fs_celestial(in: Out) -> @location(0) vec4<f32> {
+43 -4
View File
@@ -96,8 +96,13 @@ pub struct Extras {
pub minimap_rotate: bool,
/// Retry lost multiplayer connections (`reconnect`).
pub auto_reconnect: bool,
/// Positional sounds leave a few static sound channels free for UI sounds
/// and others played to the listener (`sound_engine::HEARD_RESERVE`).
pub ui_sound_reserve: bool,
/// The player's own name tag over its model in third person.
pub own_name_tag: bool,
/// No particles from breaking or mining a block (other particles stay).
pub hide_block_particles: bool,
/// Mouse Tweaks-style drags in the inventory and storage containers.
pub mouse_tweaks: bool,
/// With them, the wheel over a slot moves its items one at a time.
@@ -140,7 +145,9 @@ impl Default for Extras {
minimap_zoom: 1.0,
minimap_rotate: false,
auto_reconnect: false,
ui_sound_reserve: false,
own_name_tag: false,
hide_block_particles: false,
mouse_tweaks: false,
mouse_tweaks_wheel: true,
sort: false,
@@ -216,7 +223,9 @@ impl Extras {
}
"minimapRotate" => e.minimap_rotate = flag(e.minimap_rotate),
"autoReconnect" => e.auto_reconnect = flag(e.auto_reconnect),
"uiSoundReserve" => e.ui_sound_reserve = flag(e.ui_sound_reserve),
"ownNameTag" => e.own_name_tag = flag(e.own_name_tag),
"hideBlockParticles" => e.hide_block_particles = flag(e.hide_block_particles),
"mouseTweaks" => e.mouse_tweaks = flag(e.mouse_tweaks),
"mouseTweaksWheel" => e.mouse_tweaks_wheel = flag(e.mouse_tweaks_wheel),
"sort" => e.sort = flag(e.sort),
@@ -232,7 +241,7 @@ impl Extras {
}
pub fn serialize(&self) -> String {
format!(
"fullbright:{}\ndynamicLights:{}\nzoom:{}\nzoomKey:{}\nzoomLevel:{}\nzoomSmooth:{}\nzoomSensitivity:{}\nfps:{}\nfpsCorner:{}\nfpsLows:{}\nkeystrokes:{}\nkeystrokesCorner:{}\nkeystrokesCps:{}\nmovedToMods:{}\nminimap:{}\nminimapCorner:{}\nminimapSize:{}\nminimapShape:{}\nminimapZoom:{}\nminimapRotate:{}\nautoReconnect:{}\nownNameTag:{}\nmouseTweaks:{}\nmouseTweaksWheel:{}\nsort:{}\nsortKey:{}\n{}:{}\nagentControl:{}\ndiscordPresence:{}\nsplitscreen:{}\n",
"fullbright:{}\ndynamicLights:{}\nzoom:{}\nzoomKey:{}\nzoomLevel:{}\nzoomSmooth:{}\nzoomSensitivity:{}\nfps:{}\nfpsCorner:{}\nfpsLows:{}\nkeystrokes:{}\nkeystrokesCorner:{}\nkeystrokesCps:{}\nmovedToMods:{}\nminimap:{}\nminimapCorner:{}\nminimapSize:{}\nminimapShape:{}\nminimapZoom:{}\nminimapRotate:{}\nautoReconnect:{}\nuiSoundReserve:{}\nownNameTag:{}\nhideBlockParticles:{}\nmouseTweaks:{}\nmouseTweaksWheel:{}\nsort:{}\nsortKey:{}\n{}:{}\nagentControl:{}\ndiscordPresence:{}\nsplitscreen:{}\n",
bool_text(self.fullbright),
bool_text(self.dynamic_lights),
bool_text(self.zoom),
@@ -258,7 +267,9 @@ impl Extras {
self.minimap_zoom,
bool_text(self.minimap_rotate),
bool_text(self.auto_reconnect),
bool_text(self.ui_sound_reserve),
bool_text(self.own_name_tag),
bool_text(self.hide_block_particles),
bool_text(self.mouse_tweaks),
bool_text(self.mouse_tweaks_wheel),
bool_text(self.sort),
@@ -388,7 +399,9 @@ pub enum Item {
Fullbright,
DynamicLights,
OwnNameTag,
HideBlockParticles,
AutoReconnect,
UiSoundReserve,
MouseTweaks,
MouseTweaksWheel,
Sort,
@@ -410,6 +423,8 @@ pub fn rows() -> Vec<Result<Item, &'static str>> {
Ok(DynamicLights),
Err("Third person"),
Ok(OwnNameTag),
Err("Particles"),
Ok(HideBlockParticles),
Err("Inventory"),
Ok(MouseTweaks),
Ok(MouseTweaksWheel),
@@ -417,6 +432,8 @@ pub fn rows() -> Vec<Result<Item, &'static str>> {
Ok(SortKey),
Err("Multiplayer"),
Ok(AutoReconnect),
Err("Sound"),
Ok(UiSoundReserve),
]
}
impl Item {
@@ -425,7 +442,9 @@ impl Item {
Item::Fullbright => "Fullbright",
Item::DynamicLights => "Dynamic lights",
Item::OwnNameTag => "Own name tag",
Item::HideBlockParticles => "Hide block-break particles",
Item::AutoReconnect => "Auto-reconnect",
Item::UiSoundReserve => "Keep channels for UI sounds",
Item::MouseTweaks => "Mouse Tweaks controls",
Item::MouseTweaksWheel => "Scroll wheel moves items",
Item::Sort => "Sort buttons",
@@ -440,10 +459,11 @@ impl Item {
pub fn parent(self) -> Option<Item> {
use Item::*;
Some(match self {
UiSoundReserve => return None,
MouseTweaksWheel => MouseTweaks,
SortKey => Sort,
Fullbright | DynamicLights | OwnNameTag | MouseTweaks | Sort | AutoReconnect | NoAi
| AgentControl | DiscordPresence | Splitscreen => {
Fullbright | DynamicLights | OwnNameTag | HideBlockParticles | MouseTweaks | Sort
| AutoReconnect | NoAi | AgentControl | DiscordPresence | Splitscreen => {
return None;
}
})
@@ -458,9 +478,15 @@ impl Item {
Item::OwnNameTag => {
"Shows your name above your head in third person, as other players see it. Hidden when the server hides your team's names or you're invisible."
}
Item::HideBlockParticles => {
"Hides the debris particles that burst from a broken block and crumble off the one you mine. Other particles stay."
}
Item::AutoReconnect => {
"Rejoins a server after a lost connection or a restart, never after a kick or ban."
}
Item::UiSoundReserve => {
"Sounds in the world leave 16 of the 247 sound channels free, so a flood of them (a big explosion) can't silence menu clicks."
}
Item::MouseTweaks => {
"Right-drag places one per slot, left-drag gathers the held item, Shift + left-drag moves every slot."
}
@@ -492,8 +518,10 @@ impl Item {
use crate::graphics_style::Cost;
match self {
Item::Fullbright => Some(Cost::None),
Item::UiSoundReserve => Some(Cost::None),
Item::DynamicLights => Some(Cost::Low),
Item::OwnNameTag
| Item::HideBlockParticles
| Item::AutoReconnect
| Item::MouseTweaks
| Item::Sort
@@ -507,7 +535,9 @@ impl Item {
Item::Fullbright => e.fullbright,
Item::DynamicLights => e.dynamic_lights,
Item::OwnNameTag => e.own_name_tag,
Item::HideBlockParticles => e.hide_block_particles,
Item::AutoReconnect => e.auto_reconnect,
Item::UiSoundReserve => e.ui_sound_reserve,
Item::MouseTweaks => e.mouse_tweaks,
Item::MouseTweaksWheel => e.mouse_tweaks_wheel,
Item::Sort => e.sort,
@@ -535,7 +565,9 @@ impl Item {
Item::Fullbright => e.fullbright = !e.fullbright,
Item::DynamicLights => e.dynamic_lights = !e.dynamic_lights,
Item::OwnNameTag => e.own_name_tag = !e.own_name_tag,
Item::HideBlockParticles => e.hide_block_particles = !e.hide_block_particles,
Item::AutoReconnect => e.auto_reconnect = !e.auto_reconnect,
Item::UiSoundReserve => e.ui_sound_reserve = !e.ui_sound_reserve,
Item::MouseTweaks => e.mouse_tweaks = !e.mouse_tweaks,
Item::MouseTweaksWheel => e.mouse_tweaks_wheel = !e.mouse_tweaks_wheel,
Item::Sort => e.sort = !e.sort,
@@ -564,7 +596,8 @@ mod tests {
let d = Extras::default();
assert!(!d.fullbright && !d.dynamic_lights);
assert!(!d.zoom && !d.fps && !d.keystrokes && !d.minimap && !d.auto_reconnect);
assert!(!d.mouse_tweaks && !d.sort && !d.own_name_tag);
assert!(!d.mouse_tweaks && !d.sort && !d.own_name_tag && !d.ui_sound_reserve);
assert!(!d.hide_block_particles);
assert!(d.discord_presence);
assert_eq!(Extras::parse(""), d);
assert_eq!(Extras::parse(&d.serialize()), d);
@@ -590,6 +623,12 @@ mod tests {
e.auto_reconnect = true;
Item::OwnNameTag.cycle(&mut e, false);
assert_eq!(Item::OwnNameTag.switch(&e), Some(true));
assert_eq!(Item::HideBlockParticles.switch(&e), Some(false));
Item::HideBlockParticles.cycle(&mut e, false);
assert!(e.hide_block_particles);
assert_eq!(Item::HideBlockParticles.switch(&e), Some(true));
let only = Extras::parse("hideBlockParticles:true\n");
assert!(only.hide_block_particles && !only.own_name_tag);
e.mouse_tweaks = true;
e.mouse_tweaks_wheel = false;
e.sort = true;
+77 -1
View File
@@ -112,6 +112,8 @@ struct Region {
cursor: (u32, u32),
row: u32,
pending: Vec<Patch>,
/// A glyph did not fit since the last `Font::begin_frame`.
full: bool,
}
struct Cache {
/// Every font by id, the default one first (the common lookup).
@@ -546,6 +548,7 @@ impl Region {
self.row = 0;
}
if y + h > region.1 {
self.full = true;
return None;
}
let (ax, ay) = (x, region.0 + y);
@@ -603,6 +606,7 @@ impl Font {
cursor: (0, 0),
row: 0,
pending: Vec::new(),
full: false,
},
random: horizon_core::random::LegacyRandom::new(
std::time::SystemTime::now()
@@ -669,8 +673,19 @@ impl Font {
}
cache.region.cursor = (0, 0);
cache.region.row = 0;
cache.region.full = false;
self.generation.set(next_generation());
}
/// Call once per frame before any text is laid out. When the dynamic
/// region ran out of room (vanilla's glyph textures grow instead), the
/// glyphs are forgotten so they are rasterized again from the start; it
/// waits for the frame boundary because quads already queued hold the old
/// rects.
pub fn begin_frame(&self) {
if self.cache.borrow().region.full {
self.reset_cache();
}
}
/// Changes whenever a glyph may come out different: another font, a
/// cache reset (glyphs move in the atlas) or other font options. Text laid
/// out under one epoch can be kept until it changes.
@@ -706,7 +721,16 @@ impl Font {
if let Some(g) = set.glyphs.get(&c) {
return *g;
}
let glyph = self.resolve(set, region, c).unwrap_or(self.missing);
let was_full = std::mem::replace(&mut region.full, false);
let found = self.resolve(set, region, c);
// No room: the missing glyph for now, not remembered, so the
// character is tried again once `begin_frame` cleared the region.
let overflowed = std::mem::replace(&mut region.full, was_full);
region.full |= overflowed;
if overflowed {
return self.missing;
}
let glyph = found.unwrap_or(self.missing);
set.glyphs.insert(c, glyph);
glyph
}
@@ -1418,4 +1442,56 @@ mod tests {
font.reset_cache();
assert_eq!(font.glyph('a').advance, 2.0);
}
#[test]
fn a_full_glyph_region_recovers_at_the_next_frame() {
// Two 8x8 glyphs from a sheet outside the atlas; the region holds one row.
let sheet = png(16, 8, |x, _| x % 8 < 4);
let default = br#"{"providers":[{"type":"bitmap","file":"pack:font/s.png","ascent":7,"chars":["ab"]}]}"#;
let root = root(
"full",
&[
("assets/minecraft/font/default.json", default),
("assets/pack/textures/font/s.png", &sheet),
],
);
// A 10 wide atlas fits one 8 pixel glyph per row; the region two rows.
let font = Font::load(&root, &HashMap::new(), &[], (10, 64), (0, 8), white());
let _ = std::fs::remove_dir_all(&root);
let epoch = font.epoch();
let a = font.glyph('a');
assert!(a.rect.is_some());
// No room for 'b': missing for now, and asked again next time.
assert!(font.glyph('b').rect.is_none());
assert!(font.glyph('b').rect.is_none());
assert_eq!(font.epoch(), epoch);
font.begin_frame();
assert_ne!(font.epoch(), epoch);
let b = font.glyph('b');
assert!(b.rect.is_some());
assert_eq!(b.advance, a.advance);
// Nothing overflowed since: the next frame keeps the glyphs.
let epoch = font.epoch();
font.begin_frame();
assert_eq!(font.epoch(), epoch);
}
#[test]
fn a_character_without_a_provider_does_not_reset_the_region() {
let sheet = png(8, 8, |x, _| x < 4);
let default = br#"{"providers":[{"type":"bitmap","file":"pack:font/s.png","ascent":7,"chars":["a"]}]}"#;
let root = root(
"nofill",
&[
("assets/minecraft/font/default.json", default),
("assets/pack/textures/font/s.png", &sheet),
],
);
let font = Font::load(&root, &HashMap::new(), &[], (10, 64), (0, 8), white());
let _ = std::fs::remove_dir_all(&root);
let epoch = font.epoch();
assert!(font.glyph('z').rect.is_none());
font.begin_frame();
assert_eq!(font.epoch(), epoch);
}
}
File diff suppressed because it is too large. Load diff
@@ -299,6 +299,7 @@ impl SpriteAnimator {
uploads: &mut Uploads,
tick: u64,
active: Option<&[u64]>,
timing: Option<&mut super::pack_timing::PassTiming>,
) {
if self.collect(tick, active) == 0 {
return;
@@ -341,7 +342,7 @@ impl SpriteAnimator {
}
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("Animated sprites"),
timestamp_writes: None,
timestamp_writes: timing.and_then(|t| t.compute_writes("Animated sprites")),
});
pass.set_pipeline(pipeline);
for group in &mut self.groups {
@@ -859,7 +860,7 @@ mod tests {
for tick in [0, 0, 1, 2, 3, 4, 7, 7, 12, 30] {
let mut encoder = device.create_command_encoder(&Default::default());
let shown: Vec<_> = animator.sprites.iter().map(|s| s.shown).collect();
animator.animate(&mut encoder, &mut uploads, tick, None);
animator.animate(&mut encoder, &mut uploads, tick, None, None);
writes.push(
animator
.sprites
+306 -18
View File
@@ -1,12 +1,21 @@
//! One vertex buffer for every terrain section: sections own ranges of it, so
//! a frame binds it once and draws every section with indirect multi-draw.
//! Translucent quads can have an arena of their own (`Arena::translucent`),
//! with pages up to `LARGE_PAGE_BYTES`, and their sorted indices too
//! (`Arena::translucent_indices`): their back-to-front list then stays in one
//! page of each and draws in a few multi-draws.
use std::collections::BTreeMap;
/// Ranges are rounded up to this many vertices to limit fragmentation, and
/// start at multiples of it (`flat` names sections by these blocks).
pub const GRAIN: u64 = 256;
/// The largest page, in bytes.
/// The largest page of the section arena, and of arenas shaders bind whole
/// as storage, in bytes.
pub const PAGE_BYTES: u64 = 256 << 20;
/// The largest page of an arena no shader binds as storage: under 2 GiB, where
/// DirectX 12 and OpenGL cap buffers (`i32::MAX`), with room for the bytes
/// wgpu-core adds to vertex buffers; the device's own limit still applies.
pub const LARGE_PAGE_BYTES: u64 = (1 << 31) - (1 << 20);
/// Free vertex ranges: start -> length, never adjacent.
#[derive(Debug, PartialEq)]
@@ -118,7 +127,7 @@ impl Arena {
} else {
wgpu::BufferUsages::VERTEX
};
Self::with_usage(device, stride, capacity, usage)
Self::with_usage(device, stride, capacity, usage, PAGE_BYTES)
}
/// Sorted translucent indices; `storage` when compute passes read them.
pub fn indices(device: &wgpu::Device, capacity: u64, storage: bool) -> Self {
@@ -127,16 +136,54 @@ impl Arena {
} else {
wgpu::BufferUsages::INDEX
};
Self::with_usage(device, 4, capacity, usage)
Self::with_usage(device, 4, capacity, usage, PAGE_BYTES)
}
/// Translucent quads' vertices, which no shader reads as storage: pages
/// up to `LARGE_PAGE_BYTES`. The section arena's compaction never runs on
/// it (`Renderer::compact_arena`): a page this large has no others to
/// empty into, and the compaction moves sections' `page` ranges, not their
/// `translucent_at`.
pub fn translucent(device: &wgpu::Device, stride: u64, capacity: u64) -> Self {
let usage = wgpu::BufferUsages::VERTEX;
Self::with_usage(device, stride, capacity, usage, LARGE_PAGE_BYTES)
}
/// Sorted indices of the translucent arena's quads: pages up to
/// `LARGE_PAGE_BYTES` as its own, so its draws keep to one index page too.
pub fn translucent_indices(device: &wgpu::Device, capacity: u64) -> Self {
let usage = wgpu::BufferUsages::INDEX;
Self::with_usage(device, 4, capacity, usage, LARGE_PAGE_BYTES)
}
/// The sorted-index arena and, when `split`, the translucent arena
/// (vertices of `stride`), both with large pages, which no shader binds;
/// otherwise translucent quads stay in the section arena, and the indices
/// in pages flat draws (`flat`) bind as storage.
pub fn translucent_arenas(
device: &wgpu::Device,
stride: u64,
flat: bool,
split: bool,
) -> (Self, Option<Self>) {
if split {
let indices = Self::translucent_indices(device, 1 << 16);
(indices, Some(Self::translucent(device, stride, 1 << 18)))
} else {
(Self::indices(device, 1 << 16, flat), None)
}
}
fn with_usage(
device: &wgpu::Device,
stride: u64,
capacity: u64,
usage: wgpu::BufferUsages,
page_bytes: u64,
) -> Self {
// 256 MiB pages, or less where the device's buffers are smaller.
let bytes = device.limits().max_buffer_size.min(PAGE_BYTES);
// Pages of `page_bytes`, or less where the device's buffers (or the
// storage bindings that read them whole) are smaller.
let limits = device.limits();
let mut bytes = limits.max_buffer_size.min(page_bytes);
if usage.contains(wgpu::BufferUsages::STORAGE) {
bytes = bytes.min(limits.max_storage_buffer_binding_size);
}
let page_limit = (bytes / stride) / GRAIN * GRAIN;
let capacity = capacity.next_multiple_of(GRAIN).min(page_limit);
Self {
@@ -151,11 +198,16 @@ impl Arena {
video_memory_full: false,
}
}
/// A buffer of `capacity` vertices in video memory or, when that is full
/// and the device allows it, in system memory: Vulkan drivers fail an
/// allocation rather than page like OpenGL ones, and wgpu's default error
/// handler panics. `None` when neither has room.
fn create(&mut self, device: &wgpu::Device, capacity: u64) -> Option<(wgpu::Buffer, bool)> {
/// A buffer of `capacity` vertices in video memory or, when that is full,
/// `spill` allows it and so does the device, in system memory: Vulkan
/// drivers fail an allocation rather than page like OpenGL ones, and
/// wgpu's default error handler panics. `None` when neither has room.
fn create(
&mut self,
device: &wgpu::Device,
capacity: u64,
spill: bool,
) -> Option<(wgpu::Buffer, bool)> {
let usage = self.usage | wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC;
#[cfg(test)]
let local = (!self.video_memory_full)
@@ -167,9 +219,10 @@ impl Arena {
local.or_else(|| {
// Mappable buffers are placed in host-visible memory, which the
// allocator takes from system memory once video memory is full.
if !device
.features()
.contains(wgpu::Features::MAPPABLE_PRIMARY_BUFFERS)
if !spill
|| !device
.features()
.contains(wgpu::Features::MAPPABLE_PRIMARY_BUFFERS)
{
return None;
}
@@ -234,7 +287,7 @@ impl Arena {
&& self.closing != Some(last as u32)
{
let current = page.capacity;
let grown = (current * 2).max(current + len).min(self.page_limit);
let grown = self.grown(current, len);
if (grown - current >= len || grown == self.page_limit)
&& self.grow(device, encoder, last, grown)
&& let Some(start) = self.pages[last].as_mut().unwrap().free.take(len)
@@ -250,9 +303,9 @@ impl Arena {
.max(len)
.min(self.page_limit);
// A page just large enough when memory is short.
let ((buffer, host), capacity) = match self.create(device, capacity) {
let ((buffer, host), capacity) = match self.create(device, capacity, true) {
Some(buffer) => (buffer, capacity),
None if capacity > len => (self.create(device, len)?, len),
None if capacity > len => (self.create(device, len, true)?, len),
None => return None,
};
let mut free = FreeList::new(capacity);
@@ -392,8 +445,17 @@ impl Arena {
}
}
}
/// The capacity the newest page grows to for a range of `len`: double, but
/// by at most `PAGE_BYTES` past that size, so a large page is never left
/// half empty (nor held twice during the copy) by hundreds of MiB.
fn grown(&self, current: u64, len: u64) -> u64 {
let step = current.min(PAGE_BYTES / self.stride);
(current + step).max(current + len).min(self.page_limit)
}
/// Moves a page into a larger buffer; false (the page unchanged) when
/// there is no memory for it.
/// there is no memory for it. A page past `PAGE_BYTES` grows in video
/// memory only: moving all of it to system memory would slow every frame
/// that draws it, so a new page takes the range instead (and may spill).
fn grow(
&mut self,
device: &wgpu::Device,
@@ -402,7 +464,8 @@ impl Arena {
capacity: u64,
) -> bool {
let capacity = capacity.next_multiple_of(GRAIN).min(self.page_limit);
let Some((buffer, host)) = self.create(device, capacity) else {
let spill = capacity * self.stride <= PAGE_BYTES;
let Some((buffer, host)) = self.create(device, capacity, spill) else {
return false;
};
let old = self.pages[page].as_mut().expect("live arena page");
@@ -474,6 +537,26 @@ mod tests {
.ok()
}
/// A device with the adapter's own limits, as the renderer opens it: page
/// caps past the default 256 MiB buffer limit show there.
fn device_at_limits() -> Option<(wgpu::Device, wgpu::Queue)> {
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle());
let adapter = pollster::block_on(instance.request_adapter(&Default::default())).ok()?;
pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor {
required_limits: adapter.limits(),
..Default::default()
}))
.ok()
}
/// `device` and `device_at_limits`: the default limits bind 128 MiB of a storage
/// buffer, half their 256 MiB buffers, so storage pages shrink to the binding there;
/// the adapter's own let large pages past 256 MiB.
fn devices() -> Vec<(wgpu::Device, wgpu::Queue)> {
let devices = [device(), device_at_limits()];
devices.into_iter().flatten().collect()
}
/// `Arena::alloc`, with the copies of a grow submitted at once.
fn alloc(
arena: &mut Arena,
@@ -588,6 +671,119 @@ mod tests {
}
}
/// The renderer's half of that compaction (`move_off_page`) moves what each
/// section holds in the section arena, data and all. Split, that is its
/// solid quads alone, and nothing of a section without any, whose (0, 0)
/// names no range of page 0; else solid and translucent quads together,
/// `translucent_at` along.
#[test]
fn compaction_moves_what_sections_hold_in_the_section_arena() {
use super::super::{SectionGpu, move_off_page};
let Some((device, queue)) = device() else {
eprintln!("no GPU adapter: skipped");
return;
};
let stride = std::mem::size_of::<crate::mesher::TerrainVertex>() as u64;
let section = |(page, start), translucent_at, solid, translucent| SectionGpu {
page,
start,
translucent_at,
solid,
sides: crate::mesher::Sides::unsorted(solid),
translucent,
sort: super::super::translucency::SortState::new(Vec::new()),
sorted_indices: None,
flat_id: None,
fading: None,
leaving: None,
animated_layers: Box::new([]),
};
for split in [true, false] {
let mut arena = Arena::new(&device, stride, 4 * GRAIN, false);
arena.page_limit = 4 * GRAIN;
// Page 0: two sections' grains and two free ones; page 1: one taken.
let (_, a) = alloc(&mut arena, &device, &queue, GRAIN).unwrap();
let (_, b) = alloc(&mut arena, &device, &queue, GRAIN).unwrap();
let (_, rest) = alloc(&mut arena, &device, &queue, 2 * GRAIN).unwrap();
assert_eq!(alloc(&mut arena, &device, &queue, GRAIN), Some((1, 0)));
arena.release(0, rest, 2 * GRAIN);
// Split, a grain of solid quads each (the first's translucent ones, and
// all of a water section's, in the translucent arena); else a grain of
// solid and translucent quads, and one of translucent quads alone.
let mut sections = if split {
vec![
section((0, a), (0, 0), 64, 64),
section((0, 0), (0, GRAIN), 0, 64),
section((0, b), (0, 0), 64, 0),
]
} else {
vec![
section((0, a), (0, a + 128), 32, 32),
section((0, b), (0, b), 0, 64),
]
};
// A range's first and last vertex hold its section's number, and the
// first translucent one of a mixed range that plus 100.
let write = |(page, at): (u32, u64), value: usize| {
let bytes = (value as u32).to_le_bytes();
queue.write_buffer(arena.buffer(page), at * stride, &bytes);
};
for (i, s) in sections.iter().enumerate() {
if let Some((page, start, len)) = s.vertex_ranges(split).0 {
write((page, start), i);
write((page, start + len - 1), i);
if !split && s.solid > 0 && s.translucent > 0 {
write(s.translucent_at, 100 + i);
}
}
}
let before: Vec<_> = sections
.iter()
.map(|s| (s.page, s.start, s.translucent_at))
.collect();
arena.close(0);
let mut encoder = device.create_command_encoder(&Default::default());
let meshes = sections.iter_mut();
assert!(move_off_page(
&mut arena,
&mut encoder,
None,
meshes,
0,
split,
u64::MAX
));
queue.submit([encoder.finish()]);
let word =
|(page, at): (u32, u64)| read(&device, &queue, arena.buffer(page), at * stride);
for (i, s) in sections.iter().enumerate() {
let Some((page, start, len)) = s.vertex_ranges(split).0 else {
assert_eq!(
(s.page, s.start, s.translucent_at),
before[i],
"split {split}"
);
continue;
};
assert_eq!(page, 1, "split {split}");
assert_eq!(word((page, start)), i as u32);
assert_eq!(word((page, start + len - 1)), i as u32);
if !split {
assert_eq!(s.translucent_at, (page, start + u64::from(s.solid) * 4));
if s.solid > 0 {
assert_eq!(word(s.translucent_at), 100 + i as u32);
}
}
}
// Page 0 empties without a range given twice, and page 1 took a grain
// per section moved: one is left.
arena.trim();
assert!(!arena.has_page(0), "split {split}");
assert!(arena.alloc_existing(GRAIN).is_some(), "split {split}");
assert_eq!(arena.alloc_existing(GRAIN), None, "split {split}");
}
}
#[test]
fn empty_pages_are_reclaimed_without_moving_live_ranges() {
let Some((device, queue)) = device() else {
@@ -630,6 +826,96 @@ mod tests {
assert!(arena.pages.is_empty());
}
/// Every arena's pages stay within its cap and the device's limits: the
/// whole-page storage bindings of flat draws and shader packs included.
#[test]
fn pages_stay_within_their_caps_and_the_device_limits() {
let devices = devices();
if devices.is_empty() {
eprintln!("no GPU adapter: skipped");
return;
}
let bytes = |a: &Arena| a.page_vertices() * a.stride;
for (device, _) in &devices {
let limits = device.limits();
for storage in [false, true] {
for arena in [
Arena::new(device, 24, GRAIN, storage),
Arena::indices(device, GRAIN, storage),
] {
assert!(bytes(&arena) <= PAGE_BYTES.min(limits.max_buffer_size));
let binding = limits.max_storage_buffer_binding_size;
assert!(!storage || bytes(&arena) <= binding);
}
}
for large in [
Arena::translucent(device, 24, GRAIN),
Arena::translucent_indices(device, GRAIN),
] {
assert!(bytes(&large) <= LARGE_PAGE_BYTES.min(limits.max_buffer_size));
assert!(!large.usage.contains(wgpu::BufferUsages::STORAGE));
}
}
}
/// The translucent arena comes with the split layout alone, and only then
/// are the index pages large. Otherwise, as while a shader pack's terrain
/// draws flat (gfx's `translucent_split`), translucent quads keep to the
/// section arena, and flat draws (`flat`) bind the index pages as storage.
#[test]
fn translucent_arenas_follow_the_layout() {
let devices = devices();
if devices.is_empty() {
eprintln!("no GPU adapter: skipped");
return;
}
let mut exercised = false;
for (device, _) in &devices {
let limits = device.limits();
let large = limits.max_buffer_size > PAGE_BYTES;
exercised |= large;
for flat in [false, true] {
for split in [false, true] {
let case = format!("large {large}, flat {flat}, split {split}");
let (indices, translucent) = Arena::translucent_arenas(device, 24, flat, split);
assert_eq!(translucent.is_some(), split, "{case}");
let bytes = indices.page_vertices() * 4;
let storage = indices.usage.contains(wgpu::BufferUsages::STORAGE);
assert_eq!(storage, flat && !split, "{case}");
assert!(!storage || bytes <= limits.max_storage_buffer_binding_size);
assert_eq!(bytes > PAGE_BYTES, split && large, "{case}");
if let Some(t) = translucent {
assert_eq!(t.page_vertices() * 24 > PAGE_BYTES, large, "{case}");
}
}
}
}
if !exercised {
eprintln!("buffers end at PAGE_BYTES here: large pages not exercised");
}
}
/// Pages double up to `PAGE_BYTES`, then grow by `PAGE_BYTES` at most.
#[test]
fn large_pages_grow_by_bounded_steps() {
let Some((device, _)) = device() else {
eprintln!("no GPU adapter: skipped");
return;
};
let mut arena = Arena::translucent(&device, 24, GRAIN);
// Exercise the policy past the test device's own buffer limit.
arena.page_limit = LARGE_PAGE_BYTES / 24 / GRAIN * GRAIN;
let step = PAGE_BYTES / 24;
assert_eq!(arena.grown(1 << 18, GRAIN), 1 << 19);
assert_eq!(arena.grown(step, GRAIN), 2 * step);
assert_eq!(arena.grown(3 * step, GRAIN), 4 * step);
assert_eq!(arena.grown(3 * step, step + GRAIN), 4 * step + GRAIN);
assert_eq!(
arena.grown(arena.page_limit - GRAIN, GRAIN),
arena.page_limit
);
}
#[test]
fn clearing_an_arena_releases_buffers_and_can_allocate_again() {
let Some((device, queue)) = device() else {
@@ -638,7 +924,9 @@ mod tests {
};
for mut arena in [
Arena::new(&device, 28, GRAIN, false),
Arena::translucent(&device, 24, GRAIN),
Arena::indices(&device, GRAIN, false),
Arena::translucent_indices(&device, GRAIN),
] {
alloc(&mut arena, &device, &queue, GRAIN).unwrap();
assert_eq!(arena.bytes(), arena.stride * GRAIN);
+6 -1
View File
@@ -528,7 +528,11 @@ impl Cull {
/// Between the phases (`InShader`): draw the tested boxes in a pass of
/// their own that reads phase 1's depth, bound by `bind_depth`.
pub fn test_pass(&self, encoder: &mut wgpu::CommandEncoder) {
pub fn test_pass(
&self,
encoder: &mut wgpu::CommandEncoder,
timing: Option<&mut super::pack_timing::PassTiming>,
) {
let (Some(Group { group, .. }), Some(DepthGroup { view, group: depth })) =
(&self.group, &self.depth_group)
else {
@@ -539,6 +543,7 @@ impl Cull {
}
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Section culling"),
timestamp_writes: timing.and_then(|t| t.writes("Occlusion test")),
color_attachments: &[],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view,
@@ -20,6 +20,8 @@ const MATERIALS: usize = 10;
/// (`RenderTypes.entityTranslucent`, `entityTranslucentEmissive`, `eyes`,
/// `breezeWind`). Only the improved transparency path reads it.
pub const OIT_ENTITY: u32 = 1 << 24;
/// `RenderPipelines.DRAGON_RAYS`: untextured colour, additive with depth writes.
pub const DRAGON_RAYS_OVERLAY: u32 = 37;
/// Tags quads as drawn by a render type vanilla gives an `OitPipelineSet`
/// (`OIT_ENTITY`): improved transparency draws them in its stages.
pub fn mark_oit(quads: &mut [Vertex]) {
@@ -258,13 +258,20 @@ impl EntityOutline {
frame: &wgpu::TextureView,
entity_group: &wgpu::BindGroup,
indices: &wgpu::Buffer,
mut timing: Option<&mut super::pack_timing::PassTiming>,
) {
let Some(t) = self.targets.as_ref().filter(|_| self.quads > 0) else {
return;
};
let clear = wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT);
{
let mut pass = begin(encoder, "Entity outline", &t.outline, clear);
let mut pass = begin(
encoder,
"Entity outline",
&t.outline,
clear,
timing.as_deref_mut(),
);
pass.set_pipeline(&self.silhouette);
pass.set_bind_group(0, entity_group, &[]);
pass.set_index_buffer(indices.slice(..), wgpu::IndexFormat::Uint32);
@@ -280,7 +287,7 @@ impl EntityOutline {
("Entity outline blit", frame, wgpu::LoadOp::Load),
];
for (i, (label, view, load)) in steps.into_iter().enumerate() {
let mut pass = begin(encoder, label, view, load);
let mut pass = begin(encoder, label, view, load, timing.as_deref_mut());
pass.set_pipeline(&self.passes[i]);
pass.set_bind_group(0, &t.groups[i], &[]);
pass.draw(0..3, 0..1);
@@ -293,6 +300,7 @@ fn begin<'e>(
label: &str,
view: &wgpu::TextureView,
load: wgpu::LoadOp<wgpu::Color>,
timing: Option<&mut super::pack_timing::PassTiming>,
) -> wgpu::RenderPass<'e> {
let attachments = [Some(wgpu::RenderPassColorAttachment {
view,
@@ -305,6 +313,7 @@ fn begin<'e>(
})];
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some(label),
timestamp_writes: timing.and_then(|t| t.writes(label)),
color_attachments: &attachments,
..Default::default()
})
+42 -5
View File
@@ -62,7 +62,13 @@ pub struct Call {
}
pub struct Flat {
/// The solid draws in one pass (`fs_solid`), where the GPU doesn't hide pixels
/// by tiles (`split_solid` in `Renderer::draw_frame`: elsewhere than Metal).
pub solid: wgpu::RenderPipeline,
/// The solid draws in two passes over the same draws (terrain.wgsl's `opaque_quad`):
/// the quads with no cutout first, through a shader that never discards, then the rest.
pub solid_opaque: wgpu::RenderPipeline,
pub solid_cutout: wgpu::RenderPipeline,
/// The solid draws as the water mirror's camera draws them (`fs_solid_mirror`).
pub solid_mirror: wgpu::RenderPipeline,
pub translucent: wgpu::RenderPipeline,
@@ -286,7 +292,7 @@ fn plan(
}
impl Flat {
/// `pipeline(layout, label, fragment entry, blend, cull)` builds a
/// `pipeline(layout, label, vertex entry, fragment entry, blend, cull)` builds a
/// terrain pipeline as the per-section ones, without the origin buffer.
pub fn new(
device: &wgpu::Device,
@@ -297,6 +303,7 @@ impl Flat {
&wgpu::PipelineLayout,
&str,
&str,
&str,
Option<wgpu::BlendState>,
Option<wgpu::Face>,
) -> wgpu::RenderPipeline,
@@ -354,11 +361,35 @@ impl Flat {
let storage_usage = wgpu::BufferUsages::STORAGE;
let back = Some(wgpu::Face::Back);
Self {
solid: pipeline(&layout, "Terrain solid (flat)", "fs_solid", None, back),
solid: pipeline(
&layout,
"Terrain solid (flat)",
"vs_terrain_flat",
"fs_solid",
None,
back,
),
solid_opaque: pipeline(
&layout,
"Terrain solid opaque (flat)",
"vs_terrain_flat_opaque",
"fs_opaque",
None,
back,
),
solid_cutout: pipeline(
&layout,
"Terrain solid cutout (flat)",
"vs_terrain_flat_cutout",
"fs_solid",
None,
back,
),
// The mirror flips the winding: its solid terrain culls the front.
solid_mirror: pipeline(
&layout,
"Terrain solid mirrored (flat)",
"vs_terrain_flat",
"fs_solid_mirror",
None,
Some(wgpu::Face::Front),
@@ -366,6 +397,7 @@ impl Flat {
translucent: pipeline(
&layout,
"Terrain translucent (flat)",
"vs_terrain_flat",
"fs_translucent",
Some(wgpu::BlendState::ALPHA_BLENDING),
back,
@@ -490,6 +522,7 @@ impl Flat {
phase2: Option<&wgpu::Buffer>,
origins: impl Iterator<Item = (u32, [f32; 4])>,
sorted_page: impl Fn(u32) -> wgpu::Buffer,
timing: Option<&mut super::pack_timing::PassTiming>,
) -> bool {
self.phase2_group = None;
let phase1_draws = if phase2.is_some() {
@@ -668,7 +701,7 @@ impl Flat {
.collect();
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("Flatten section draws"),
timestamp_writes: None,
timestamp_writes: timing.and_then(|t| t.compute_writes("Flatten")),
});
pass.set_pipeline(&self.flatten);
for (group, count) in &groups {
@@ -700,13 +733,17 @@ impl Flat {
true
}
/// After occlusion culling's test: copies the phase 2 draws it saw.
pub fn flatten_phase2(&self, encoder: &mut wgpu::CommandEncoder) {
pub fn flatten_phase2(
&self,
encoder: &mut wgpu::CommandEncoder,
timing: Option<&mut super::pack_timing::PassTiming>,
) {
let Some((group, count)) = &self.phase2_group else {
return;
};
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("Flatten phase 2 section draws"),
timestamp_writes: None,
timestamp_writes: timing.and_then(|t| t.compute_writes("Flatten phase 2")),
});
pass.set_pipeline(&self.flatten_seen);
pass.set_bind_group(0, group, &[]);
@@ -1,9 +1,11 @@
//! Frame generation: FSR 3.1's frame interpolation will show a frame halfway between each
//! two real ones. So far this holds its optical flow and interpolation; the setting that
//! runs them comes next.
//! two real ones. So far this holds its optical flow, its interpolation and the motion
//! vectors of what moves on its own; the setting that runs them comes next.
// Unused until the renderer runs them.
#[allow(dead_code)]
mod interpolation;
#[allow(dead_code)]
mod motion;
#[allow(dead_code)]
mod optical_flow;
@@ -10,8 +10,8 @@
//! priorities and vectors) and fill the game field's holes from a pyramid; mask what each
//! frame can't see; blend the two frames along both fields; fill what is left from a
//! pyramid of the result. What differs from AMD's, all forced by WGSL or by Horizon:
//! the game's motion vectors are computed from the depth and `Inputs::reproject` (the
//! camera's, so what moves on its own relies on the optical flow); textures AMD updates
//! the game's motion vectors are the camera's, from the depth and `Inputs::reproject`,
//! except where `Inputs::object_motion` has an entity's own; textures AMD updates
//! with image atomics are storage buffers; SPD's single-pass pyramids are a pass per
//! level; and there is no HUD-less pass, as Horizon draws its HUD over the made frame.
@@ -33,6 +33,9 @@ pub struct Inputs<'a> {
/// `world_post::reprojection`): the motion vectors are the camera's, from this and
/// the depth.
pub reproject: Mat4,
/// What moved on its own: `motion::FORMAT` at `render_size`, xy its motion vector
/// (FSR's convention, in UV) where z is 1, used over the camera's.
pub object_motion: &'a wgpu::TextureView,
pub near: f32,
pub far: f32,
/// Vertical field of view, radians.
@@ -442,6 +445,7 @@ impl Interpolation {
&[
(0, params()),
(2, view(inputs.depth)),
(34, view(inputs.object_motion)),
(5, view(&r.dilated_depth)),
(6, view(&r.dilated_mv)),
(9, whole(&r.rec_prev_depth)),
@@ -644,10 +648,12 @@ mod tests {
/// Runs `frames` (pattern shifts) through the optical flow and the interpolation with
/// a camera that moves by `reproject` each frame, and returns the mean error of the
/// last made frame's interior against `expected`, and the last two real frames'.
/// `object`: every pixel's own motion vector (UV), as if all of it moved on its own.
fn interpolate(
seed: u32,
shifts: &[[i32; 2]],
reproject: Mat4,
object: Option<[f32; 2]>,
expected: [i32; 2],
) -> Option<[f32; 3]> {
let (device, queue) = of::device()?;
@@ -682,6 +688,35 @@ mod tests {
..Default::default()
});
queue.submit([encoder.finish()]);
let object_motion = device.create_texture(&wgpu::TextureDescriptor {
label: None,
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: super::super::motion::FORMAT,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
if let Some(mv) = object {
let texel = [mv[0], mv[1], 1.0, 0.0].map(|v| half::f16::from_f32(v).to_bits());
let texels: Vec<u16> = (0..size[0] * size[1]).flat_map(|_| texel).collect();
queue.write_texture(
object_motion.as_image_copy(),
bytemuck::cast_slice(&texels),
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(size[0] * 8),
rows_per_image: None,
},
object_motion.size(),
);
}
let object_motion = object_motion.create_view(&Default::default());
let mut flow = OpticalFlow::new(&device, size);
let mut interpolation = Interpolation::new(&device, size);
let frames: Vec<_> = shifts
@@ -731,6 +766,7 @@ mod tests {
render_size: size,
jitter: [0.0; 2],
reproject: if i == 0 { Mat4::IDENTITY } else { reproject },
object_motion: &object_motion,
near: 0.05,
far: 512.0,
fov_y: 1.2,
@@ -776,7 +812,8 @@ mod tests {
fn moves_what_the_optical_flow_follows_halfway() {
let mut shifts = vec![[0, 0]; 20];
shifts.push([16, 8]);
let Some([made, previous, current]) = interpolate(100, &shifts, Mat4::IDENTITY, [8, 4])
let Some([made, previous, current]) =
interpolate(100, &shifts, Mat4::IDENTITY, None, [8, 4])
else {
return;
};
@@ -795,7 +832,26 @@ mod tests {
// What is at a pixel now was 16 pixels to its right last frame.
let reproject = Mat4::from_translation(glam::Vec3::new(32.0 / size[0], 0.0, 0.0));
let shifts = [[0, 0], [-16, 0], [-32, 0], [-48, 0]];
let Some([made, previous, current]) = interpolate(1, &shifts, reproject, [-40, 0]) else {
let Some([made, previous, current]) = interpolate(1, &shifts, reproject, None, [-40, 0])
else {
return;
};
assert!(
made * 4.0 < previous.min(current),
"{made} against {previous} and {current}"
);
}
/// Something moving on its own with its own motion vectors (the camera still): the
/// made frame is halfway, as with the camera's.
#[test]
fn moves_what_has_its_own_motion_vectors_halfway() {
let size = [512.0, 288.0];
let shifts = [[0, 0], [16, 8], [32, 16], [48, 24]];
let mv = [-16.0 / size[0], -8.0 / size[1]];
let Some([made, previous, current]) =
interpolate(1, &shifts, Mat4::IDENTITY, Some(mv), [40, 20])
else {
return;
};
assert!(
@@ -24,7 +24,8 @@
// for Horizon, for an SDR back buffer, inverted finite depth, the whole screen as the
// interpolation rect and no distortion field. What differs from AMD's (interpolation.rs
// lists why):
// - the motion vectors come from the depth and the camera (`prepare`), already unjittered;
// - the motion vectors are the camera's, from the depth (`prepare`), already unjittered,
// or an entity's own where `object_motion` has one;
// - the vector fields and reconstructed depths are storage buffers of atomics;
// - both inpainting pyramids are a pass per level, and textures of their own;
// - the interpolation writes one texture and the inpainting another (rgba16float can't
@@ -85,6 +86,8 @@ struct Params {
@group(0) @binding(31) var interpolated_out: texture_storage_2d<rgba16float, write>;
@group(0) @binding(32) var interpolated: texture_2d<f32>;
@group(0) @binding(33) var output: texture_storage_2d<rgba16float, write>;
// What moved on its own (motion.wgsl): xy its motion vector where z is 1.
@group(0) @binding(34) var object_motion: texture_2d<f32>;
const EPSILON: f32 = 1e-03;
const RECONSTRUCTED_DEPTH_WEIGHT_THRESHOLD: f32 = EPSILON;
@@ -355,7 +358,11 @@ fn prepare_pass(@builtin(global_invocation_id) gid: vec3<u32>) {
}
}
}
let mv = camera_motion_vector(nearest_coord, load_input_depth(nearest_coord));
let object = textureLoad(object_motion, nearest_coord, 0);
var mv = camera_motion_vector(nearest_coord, load_input_depth(nearest_coord));
if object.z > 0.5 {
mv = object.xy;
}
textureStore(dilated_depth_out, p, vec4(nearest, 0.0, 0.0, 0.0));
textureStore(dilated_mv_out, p, vec4(mv, 0.0, 0.0));
// ReconstructPrevDepth
@@ -0,0 +1,306 @@
//! Motion vectors for what moves on its own, for frame generation: the camera's motion
//! vectors (from the depth) see mobs and dropped items as still. The game tags each
//! entity's vertices with its id (`Frame::entity_motion`); an entity whose vertices have
//! the same shape as last frame's (the same count, UVs and layers) gets each vertex's
//! own motion, limbs and spin included, drawn into a texture at the render size
//! (`motion.wgsl`) that the interpolation reads before the camera's.
use crate::mesher::Vertex;
use glam::{DVec3, Mat4, Vec3};
use std::collections::HashMap;
/// `motion.wgsl`'s `Params`.
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct Params {
jittered: [[f32; 4]; 4],
current: [[f32; 4]; 4],
previous: [[f32; 4]; 4],
}
/// A vertex this frame and where it was last frame, both relative to this frame's camera.
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct MotionVertex {
position: [f32; 3],
previous: [f32; 3],
uv: [f32; 2],
/// 1 for a translucent item, whose texture says where it shows.
translucent: u32,
}
pub struct ObjectMotion {
pipeline: wgpu::RenderPipeline,
atlas: wgpu::TextureView,
sampler: wgpu::Sampler,
params: wgpu::Buffer,
vertices: wgpu::Buffer,
/// Triangles of the entities matched this frame.
staged: Vec<MotionVertex>,
/// Last frame's vertices by entity, and its camera.
tracked: HashMap<u64, Vec<Vertex>>,
camera: DVec3,
target: Option<([u32; 2], wgpu::TextureView)>,
}
pub const FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba16Float;
impl ObjectMotion {
/// `atlas`: the entities' texture atlas.
pub fn new(device: &wgpu::Device, atlas: wgpu::TextureView) -> Self {
let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("Frame generation motion"),
source: wgpu::ShaderSource::Wgsl(include_str!("motion.wgsl").into()),
});
let attributes =
wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3, 2 => Float32x2, 3 => Uint32];
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Frame generation motion"),
layout: None,
vertex: wgpu::VertexState {
module: &module,
entry_point: Some("vs_motion"),
compilation_options: Default::default(),
buffers: &[Some(wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<MotionVertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &attributes,
})],
},
primitive: Default::default(),
depth_stencil: None,
multisample: Default::default(),
fragment: Some(wgpu::FragmentState {
module: &module,
entry_point: Some("fs_motion"),
compilation_options: Default::default(),
targets: &[Some(FORMAT.into())],
}),
multiview_mask: None,
cache: None,
});
Self {
pipeline,
atlas,
sampler: device.create_sampler(&Default::default()),
params: device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Frame generation motion"),
size: std::mem::size_of::<Params>() as u64,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
}),
vertices: vertex_buffer(device, 1 << 12),
staged: Vec::new(),
tracked: HashMap::new(),
camera: DVec3::ZERO,
target: None,
}
}
/// Forget last frame's entities: the next frame has no motion of its own.
pub fn reset(&mut self) {
self.tracked.clear();
}
/// Match this frame's `entities` (relative to `camera`) to last frame's by the ids
/// `spans` gives their vertices (start, id; 0 for none, each up to the next start), and
/// keep them for the next frame.
pub fn track(&mut self, entities: &[Vertex], spans: &[(u32, u64)], camera: DVec3) {
self.staged.clear();
let shift = (self.camera - camera).as_vec3();
let mut tracked = HashMap::with_capacity(self.tracked.len());
for (i, &(start, id)) in spans.iter().enumerate() {
let end = spans.get(i + 1).map_or(entities.len(), |s| s.0 as usize);
let Some(now) = entities
.get(start as usize..end)
.filter(|v| id != 0 && !v.is_empty())
else {
continue;
};
if let Some(then) = self.tracked.remove(&id)
&& then.len() == now.len()
&& then
.iter()
.zip(now)
.all(|(a, b)| a.uv == b.uv && a.layer == b.layer)
{
let pair = |i: usize| MotionVertex {
position: now[i].pos,
previous: (Vec3::from(then[i].pos) + shift).into(),
uv: now[i].uv,
translucent: u32::from(matches!(
now[i].layer,
super::super::entity_batch::TRANSLUCENT_ITEM_OVERLAY
| super::super::entity_batch::TRANSLUCENT_GLINT_ITEM_OVERLAY
)),
};
for q in (0..now.len() / 4).map(|q| q * 4) {
self.staged
.extend([q, q + 1, q + 2, q, q + 2, q + 3].map(pair));
}
}
tracked.insert(id, now.to_vec());
}
self.tracked = tracked;
self.camera = camera;
}
/// Draw the matched entities' motion vectors at `size` (the world's render size),
/// where `depth` (the world's) shows them; the rest of the texture is 0.
#[allow(clippy::too_many_arguments)]
pub fn draw(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
depth: &wgpu::TextureView,
size: [u32; 2],
jittered: Mat4,
current: Mat4,
previous: Mat4,
) -> wgpu::TextureView {
if self.target.as_ref().is_none_or(|t| t.0 != size) {
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("Frame generation motion"),
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: FORMAT,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT
| wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::COPY_SRC,
view_formats: &[],
});
self.target = Some((size, texture.create_view(&Default::default())));
}
let target = self.target.as_ref().expect("made above").1.clone();
let bytes: &[u8] = bytemuck::cast_slice(&self.staged);
if bytes.len() as u64 > self.vertices.size() {
self.vertices = vertex_buffer(device, (bytes.len() as u64).next_power_of_two());
}
if !bytes.is_empty() {
queue.write_buffer(&self.vertices, 0, bytes);
}
let params = Params {
jittered: jittered.to_cols_array_2d(),
current: current.to_cols_array_2d(),
previous: previous.to_cols_array_2d(),
};
queue.write_buffer(&self.params, 0, bytemuck::bytes_of(&params));
let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Frame generation motion"),
layout: &self.pipeline.get_bind_group_layout(0),
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.params.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(depth),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::TextureView(&self.atlas),
},
wgpu::BindGroupEntry {
binding: 3,
resource: wgpu::BindingResource::Sampler(&self.sampler),
},
],
});
let attachments = [Some(wgpu::RenderPassColorAttachment {
view: &target,
resolve_target: None,
depth_slice: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
store: wgpu::StoreOp::Store,
},
})];
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Frame generation motion"),
color_attachments: &attachments,
..Default::default()
});
if !self.staged.is_empty() {
pass.set_pipeline(&self.pipeline);
pass.set_bind_group(0, &group, &[]);
pass.set_vertex_buffer(0, self.vertices.slice(..bytes.len() as u64));
pass.draw(0..self.staged.len() as u32, 0..1);
}
drop(pass);
target
}
}
fn vertex_buffer(device: &wgpu::Device, size: u64) -> wgpu::Buffer {
device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Frame generation motion vertices"),
size,
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn shader_validates() {
let module = naga::front::wgsl::parse_str(include_str!("motion.wgsl")).unwrap();
naga::valid::Validator::new(
naga::valid::ValidationFlags::all(),
naga::valid::Capabilities::empty(),
)
.validate(&module)
.unwrap();
}
fn quad(x: f32, uv: f32) -> Vec<Vertex> {
(0..4)
.map(|i| Vertex {
pos: [x + i as f32, 0.0, -5.0],
uv: [uv, 0.0],
..bytemuck::Zeroable::zeroed()
})
.collect()
}
/// An entity seen last frame gets its vertices' own motion, in this frame's camera
/// space; a new one, one whose shape changed and untagged geometry get none.
#[test]
fn tracks_each_entity_by_id() {
let Some((device, queue)) = super::super::optical_flow::tests::device() else {
return;
};
let atlas = super::super::optical_flow::tests::upload(&device, &queue, [1, 1], &[0; 4]);
let mut motion = ObjectMotion::new(&device, atlas);
let mut entities = quad(0.0, 0.0);
entities.extend(quad(10.0, 0.5));
motion.track(&entities, &[(0, 7), (4, 8)], DVec3::ZERO);
assert!(motion.staged.is_empty());
// Entity 7 moved 1 block right, the camera 2 blocks right; entity 8 became
// another item; 9 is new; then untagged geometry.
let mut entities = quad(-1.0, 0.0);
entities.extend(quad(10.0, 0.25));
entities.extend(quad(20.0, 0.0));
entities.extend(quad(30.0, 0.0));
motion.track(
&entities,
&[(0, 7), (4, 8), (8, 9), (12, 0)],
DVec3::new(2.0, 0.0, 0.0),
);
assert_eq!(motion.staged.len(), 6);
let v = motion.staged[0];
assert_eq!(v.position, [-1.0, 0.0, -5.0]);
assert_eq!(v.previous, [-2.0, 0.0, -5.0]);
assert_eq!(motion.tracked.len(), 3);
}
}
@@ -0,0 +1,58 @@
// Frame generation's motion vectors for what moves on its own (mobs, dropped items): each
// vertex drawn where the world pass drew it, with where it was last frame; only the pixels
// the world shows it at keep its vector: where the depth is its own, or, for translucent
// items (which don't write depth), where its texture shows and nothing is in front.
struct Params {
// The world's projection this frame (TAA's jitter in it), the same without the jitter,
// and last frame's projection from this frame's camera.
jittered: mat4x4<f32>,
current: mat4x4<f32>,
previous: mat4x4<f32>,
};
@group(0) @binding(0) var<uniform> params: Params;
@group(0) @binding(1) var depth: texture_depth_2d;
@group(0) @binding(2) var atlas: texture_2d<f32>;
@group(0) @binding(3) var atlas_sampler: sampler;
struct Out {
@builtin(position) clip: vec4<f32>,
@location(0) current: vec4<f32>,
@location(1) previous: vec4<f32>,
@location(2) uv: vec2<f32>,
// 1: a translucent item.
@location(3) @interpolate(flat) translucent: u32,
};
@vertex fn vs_motion(@location(0) position: vec3<f32>, @location(1) previous: vec3<f32>,
@location(2) uv: vec2<f32>, @location(3) translucent: u32) -> Out {
var out: Out;
out.uv = uv;
out.translucent = translucent;
out.clip = params.jittered * vec4(position, 1.0);
out.current = params.current * vec4(position, 1.0);
out.previous = params.previous * vec4(previous, 1.0);
return out;
}
// xy: the motion vector in UV (to where the pixel was last frame, FSR's convention), z: 1.
@fragment fn fs_motion(in: Out) -> @location(0) vec4<f32> {
let stored = textureLoad(depth, vec2<i32>(in.clip.xy), 0);
let tolerance = stored * 1e-3 + 1e-7;
// Not what the world shows here: a transparent texel, or behind something.
if in.translucent != 0u {
let alpha = textureSampleLevel(atlas, atlas_sampler, in.uv, 0.0).a;
if alpha < 0.1 || in.clip.z < stored - tolerance {
discard;
}
} else if abs(in.clip.z - stored) > tolerance {
discard;
}
if in.previous.w <= 0.0 {
discard;
}
let now = in.current.xy / in.current.w;
let then = in.previous.xy / in.previous.w;
return vec4((then - now) * vec2(0.5, -0.5), 1.0, 0.0);
}
+17 -4
View File
@@ -366,7 +366,11 @@ impl Hdr {
/// Keeps the world as drawn so far, before the hand and HUD cover it, then zeroes the
/// frame's alpha: the world's marks live on in the copy, and whatever draws from here
/// on raises the alpha where it covers the world.
pub fn keep_world(&self, encoder: &mut wgpu::CommandEncoder) {
pub fn keep_world(
&self,
encoder: &mut wgpu::CommandEncoder,
timing: Option<&mut super::pack_timing::PassTiming>,
) {
let Some(t) = &self.targets else {
return;
};
@@ -387,6 +391,7 @@ impl Hdr {
})];
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Horizon HDR UI mask"),
timestamp_writes: timing.and_then(|t| t.writes("HDR UI mask")),
color_attachments: &attachments,
..Default::default()
});
@@ -397,7 +402,11 @@ impl Hdr {
/// After the hand: its pixels' alpha goes down to 1/255, below anything the HUD and
/// menus leave, so HDR output tells the hand (the world's brightness, never lifted)
/// from the UI (UI Brightness).
pub fn mark_hand(&self, encoder: &mut wgpu::CommandEncoder) {
pub fn mark_hand(
&self,
encoder: &mut wgpu::CommandEncoder,
timing: Option<&mut super::pack_timing::PassTiming>,
) {
let Some(t) = &self.targets else {
return;
};
@@ -413,6 +422,7 @@ impl Hdr {
})];
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Horizon HDR hand mark"),
timestamp_writes: timing.and_then(|t| t.writes("HDR hand mark")),
color_attachments: &attachments,
..Default::default()
});
@@ -434,6 +444,7 @@ impl Hdr {
encoder: &mut wgpu::CommandEncoder,
target: &wgpu::Texture,
world: bool,
timing: Option<&mut super::pack_timing::PassTiming>,
) {
let Some(t) = &self.targets else {
return;
@@ -443,7 +454,7 @@ impl Hdr {
0,
bytemuck::cast_slice(&self.params(world, self.output == Output::Pq)),
);
Self::draw(encoder, &self.pipeline, &t.group, target);
Self::draw(encoder, &self.pipeline, &t.group, target, timing);
}
/// The frame `present` writes, as HDR10 (BT.2020, PQ, 10 bits a channel) whatever
@@ -494,7 +505,7 @@ impl Hdr {
0,
bytemuck::cast_slice(&self.params(world, true)),
);
Self::draw(encoder, &self.capture_pipeline, &group, &texture);
Self::draw(encoder, &self.capture_pipeline, &group, &texture, None);
Some(texture)
}
@@ -528,6 +539,7 @@ impl Hdr {
pipeline: &wgpu::RenderPipeline,
group: &wgpu::BindGroup,
target: &wgpu::Texture,
timing: Option<&mut super::pack_timing::PassTiming>,
) {
let view = target.create_view(&Default::default());
let attachments = [Some(wgpu::RenderPassColorAttachment {
@@ -541,6 +553,7 @@ impl Hdr {
})];
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Horizon HDR"),
timestamp_writes: timing.and_then(|t| t.writes("HDR output")),
color_attachments: &attachments,
..Default::default()
});
+1 -1
View File
@@ -1,4 +1,4 @@
//! Far meshes share the terrain arena and passes, with their actual bounds
//! Far meshes share the terrain arenas and passes, with their actual bounds
//! rather than the 16³ boxes used by the near-section residency walk.
use super::{Frustum, Renderer, SectionGpu};
use crate::{
@@ -102,16 +102,38 @@ fn horizon_block_pbr(uv: vec2<f32>, layer: u32, lod: f32, map: u32) -> vec4<f32>
return select(vec4<f32>(0.5, 0.5, 1.0, 1.0), vec4<f32>(0.0), map == 1u);
}
let s = horizon_terrain_sprite_sampler;
// The maps may be smaller than the colour: the same footprint is fewer of their texels.
let scale = f32(textureDimensions(horizon_terrain_sprites).x)
/ f32(textureDimensions(horizon_pbr_normals).x);
let l = max(lod - log2(scale), 0.0);
if map == 0u {
return textureSampleLevel(horizon_pbr_normals, s, uv, i32(layer), lod);
return textureSampleLevel(horizon_pbr_normals, s, uv, i32(layer), l);
}
return textureSampleLevel(horizon_pbr_specular, s, uv, i32(layer), l);
}
// The prelude's `block_texture`: a sprite of the terrain's arrays at a mip level.
fn horizon_block_size() -> vec2<f32> {
return vec2<f32>(textureDimensions(horizon_terrain_sprites).xy);
}
fn horizon_block_sample(uv: vec2<f32>, layer: u32, lod: f32) -> vec4<f32> {
let s = horizon_terrain_sprite_sampler;
let l = i32(layer % horizon_terrain_SPRITE_SPLIT);
switch layer / horizon_terrain_SPRITE_SPLIT {
case 0u: { return textureSampleLevel(horizon_terrain_sprites, s, uv, l, lod); }
case 1u: { return textureSampleLevel(horizon_terrain_sprites_1, s, uv, l, lod); }
case 2u: { return textureSampleLevel(horizon_terrain_sprites_2, s, uv, l, lod); }
case 3u: { return textureSampleLevel(horizon_terrain_sprites_3, s, uv, l, lod); }
case 4u: { return textureSampleLevel(horizon_terrain_sprites_4, s, uv, l, lod); }
case 5u: { return textureSampleLevel(horizon_terrain_sprites_5, s, uv, l, lod); }
case 6u: { return textureSampleLevel(horizon_terrain_sprites_6, s, uv, l, lod); }
default: { return textureSampleLevel(horizon_terrain_sprites_7, s, uv, l, lod); }
}
return textureSampleLevel(horizon_pbr_specular, s, uv, i32(layer), lod);
}
fn horizon_material_color(in: HorizonMaterialOut, translucent: bool) -> vec4<f32> {
let albedo = horizon_terrain_sprite(in.uv, in.layer & 0x7fffu);
horizon_block_layer = in.layer & 0x7fffu;
// The mip level of this point in the maps: their own texels, as the sampler would pick.
let texels = in.uv * horizon_block_pbr_size();
// The mip level of this point in the texture: its texels, as the sampler would pick.
let texels = in.uv * horizon_block_size();
let footprint = max(dot(dpdx(texels), dpdx(texels)), dot(dpdy(texels), dpdy(texels)));
horizon_block_lod = max(0.5 * log2(max(footprint, 1e-8)), 0.0);
// Water draws translucent and lava solid, so the pass resolves the fluid kind.
@@ -225,8 +225,9 @@ impl ShadowMap {
&self,
encoder: &'e mut wgpu::CommandEncoder,
label: &str,
timing: Option<&mut super::pack_timing::PassTiming>,
) -> wgpu::RenderPass<'e> {
self.depth_pass(encoder, label, wgpu::LoadOp::Clear(1.0))
self.depth_pass(encoder, label, wgpu::LoadOp::Clear(1.0), timing)
}
/// A depth pass that adds to the map. Entities draw here, after the translucent map was
@@ -236,7 +237,7 @@ impl ShadowMap {
encoder: &'e mut wgpu::CommandEncoder,
label: &str,
) -> wgpu::RenderPass<'e> {
self.depth_pass(encoder, label, wgpu::LoadOp::Load)
self.depth_pass(encoder, label, wgpu::LoadOp::Load, None)
}
fn depth_pass<'e>(
@@ -244,9 +245,11 @@ impl ShadowMap {
encoder: &'e mut wgpu::CommandEncoder,
label: &str,
load: wgpu::LoadOp<f32>,
timing: Option<&mut super::pack_timing::PassTiming>,
) -> wgpu::RenderPass<'e> {
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some(label),
timestamp_writes: timing.and_then(|t| t.writes(label)),
color_attachments: &[],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &self.view,
@@ -266,6 +269,7 @@ impl ShadowMap {
&self,
encoder: &'e mut wgpu::CommandEncoder,
label: &str,
timing: Option<&mut super::pack_timing::PassTiming>,
) -> wgpu::RenderPass<'e> {
encoder.copy_texture_to_texture(
self.depth.as_image_copy(),
@@ -274,6 +278,7 @@ impl ShadowMap {
);
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some(label),
timestamp_writes: timing.and_then(|t| t.writes(label)),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &self.color_view,
resolve_target: None,
@@ -298,8 +303,8 @@ impl ShadowMap {
/// Clears the 1×1 map of a pack without `[shadow]` once.
pub fn clear_blank(&mut self, encoder: &mut wgpu::CommandEncoder) {
if std::mem::take(&mut self.blank_uncleared) {
drop(self.begin(encoder, "Native shader pack shadow map clear"));
drop(self.begin_translucent(encoder, "Native shader pack shadow colour clear"));
drop(self.begin(encoder, "Native shader pack shadow map clear", None));
drop(self.begin_translucent(encoder, "Native shader pack shadow colour clear", None));
}
}
}
+15 -3
View File
@@ -620,6 +620,7 @@ impl Oit {
depth: &wgpu::TextureView,
draw: &dyn Fn(&mut wgpu::RenderPass<'_>, Stage),
extras: &Extras<'_>,
mut timing: Option<&mut super::pack_timing::PassTiming>,
) {
let Some(t) = &self.targets else { return };
let attachment = |view, load| {
@@ -645,7 +646,7 @@ impl Oit {
let water_mask = extras.water_mask.zip(t.terrain_depth.as_ref());
let cloud_depth = extras.clouds.zip(t.cloud_depth.as_ref());
if let Some((mask, _)) = water_mask {
self.execute_water_mask(encoder, t, color, depth, mask);
self.execute_water_mask(encoder, t, color, depth, mask, timing.as_deref_mut());
}
let terrain_depth = water_mask.map_or(depth, |(_, d)| &d.view);
let [c0, c1] = &t.views.coefficients;
@@ -680,6 +681,7 @@ impl Oit {
{
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some(label),
timestamp_writes: timing.as_deref_mut().and_then(|t| t.writes(label)),
color_attachments: &first,
depth_stencil_attachment: read_only(terrain_depth),
..Default::default()
@@ -692,6 +694,7 @@ impl Oit {
// the terrain's: the masks only hide water.
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some(label),
timestamp_writes: timing.as_deref_mut().and_then(|t| t.writes("OIT features")),
color_attachments: &[attachment(
match stage {
Stage::DepthBounds => &t.views.bounds,
@@ -710,7 +713,7 @@ impl Oit {
features(&mut pass, stage);
}
if stage == Stage::DepthBounds {
self.execute_depth_bounds_cull(encoder, t, depth);
self.execute_depth_bounds_cull(encoder, t, depth, timing.as_deref_mut());
}
if let Some((clouds, cloud_depth)) = cloud_depth {
self.render_clouds(
@@ -720,16 +723,18 @@ impl Oit {
depth,
(label, group, &then),
(clouds, cloud_depth),
timing.as_deref_mut(),
);
}
if stage == Stage::DepthBounds
&& let Some((mask, _)) = water_mask
{
self.execute_water_mask(encoder, t, color, depth, mask);
self.execute_water_mask(encoder, t, color, depth, mask, timing.as_deref_mut());
}
}
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("OIT composite"),
timestamp_writes: timing.and_then(|t| t.writes("OIT composite")),
color_attachments: &[attachment(color, wgpu::LoadOp::Load)],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: depth,
@@ -749,6 +754,7 @@ impl Oit {
/// `CloudRenderer.renderOit` for one stage. The bounds stage first copies
/// the world's depth (after the cull) and draws into it, so the later
/// stages, which only read it, keep each pixel's nearest cloud face.
#[allow(clippy::too_many_arguments)]
fn render_clouds(
&self,
encoder: &mut wgpu::CommandEncoder,
@@ -764,6 +770,7 @@ impl Oit {
&dyn Fn(&mut wgpu::RenderPass<'_>, &wgpu::RenderPipeline),
&Depth,
),
timing: Option<&mut super::pack_timing::PassTiming>,
) {
if stage == Stage::DepthBounds {
encoder.copy_texture_to_texture(
@@ -774,6 +781,7 @@ impl Oit {
}
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some(label),
timestamp_writes: timing.and_then(|t| t.writes("OIT clouds")),
color_attachments: targets,
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &cloud_depth.view,
@@ -798,6 +806,7 @@ impl Oit {
color: &wgpu::TextureView,
depth: &wgpu::TextureView,
mask: &dyn Fn(&mut wgpu::RenderPass<'_>),
timing: Option<&mut super::pack_timing::PassTiming>,
) {
let Some(terrain) = &t.terrain_depth else {
return;
@@ -810,6 +819,7 @@ impl Oit {
// The mask pipeline writes depth only but has a colour target.
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("OIT water mask"),
timestamp_writes: timing.and_then(|t| t.writes("OIT water mask")),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: color,
resolve_target: None,
@@ -840,6 +850,7 @@ impl Oit {
encoder: &mut wgpu::CommandEncoder,
t: &Targets,
depth: &wgpu::TextureView,
timing: Option<&mut super::pack_timing::PassTiming>,
) {
encoder.copy_texture_to_texture(
t.bounds.as_image_copy(),
@@ -852,6 +863,7 @@ impl Oit {
);
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("OIT cull depth bounds"),
timestamp_writes: timing.and_then(|t| t.writes("OIT cull depth bounds")),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &t.views.culled,
resolve_target: None,
@@ -217,7 +217,12 @@ impl Features {
&entity_module,
entities,
&entity_buffers,
kind("additive entities", "lightning", None, PLAIN),
kind(
"additive entities",
"lightning",
Some(wgpu::Face::Back),
PLAIN,
),
),
weather: set(
&entity_module,
@@ -3117,8 +3117,7 @@ impl PackPost {
/// What the shadow camera draws of the terrain this frame, when the pack draws it.
pub fn shadow_view(&self, frame: &super::Frame) -> Option<ShadowView> {
self.shadow_targets.as_ref()?;
if self.shadow_terrain.is_none() && self.shadow_water.is_none() {
if !self.has_shadow() {
return None;
}
let shadow = &self.uniforms.shadow;
@@ -3139,6 +3138,12 @@ impl PackPost {
self.terrain.is_some()
}
/// Whether the pack's shadow pass draws terrain (`shadow_view`).
pub fn has_shadow(&self) -> bool {
self.shadow_targets.is_some()
&& (self.shadow_terrain.is_some() || self.shadow_water.is_some())
}
/// Whether the pack draws the sky (disc, sunset, stars, void) itself.
/// What the world's background is when the pack draws the sky: colortex0's clear colour
/// (`None`: the fog colour, its default).
@@ -1,5 +1,6 @@
//! GPU time of each pass of a shader pack, of either kind: shown in the F3 overlay while it is
//! open, and logged every `REPORT` timed frames with `HORIZON_SHADER_PACK_TIMING=1`. One frame
//! GPU time of each pass of a shader pack, of either kind, and of Horizon's own passes: shown in
//! the F3 overlay while it is open, and logged every `REPORT` timed frames with
//! `HORIZON_SHADER_PACK_TIMING=1`. One frame
//! at a time is timed, and the CPU never waits for it: its timestamps are resolved in a later
//! frame, once the GPU has finished it (Metal resolves them without waiting for the passes that
//! write them), then read back when mapped. The next frame after that is timed again.
@@ -139,6 +140,12 @@ impl PassTiming {
Some(stamps(&self.queries, i))
}
/// Timestamp writes for the compute pass `name`, in a timed frame.
pub fn compute_writes(&mut self, name: &str) -> Option<wgpu::ComputePassTimestampWrites<'_>> {
let i = self.push(name)?;
Some(compute_stamps(&self.queries, i))
}
/// The index of the timed frame's next pass, `name`.
fn push(&mut self, name: &str) -> Option<u32> {
let i = self.names.len() as u32;
@@ -274,6 +281,15 @@ fn stamps(queries: &wgpu::QuerySet, i: u32) -> wgpu::RenderPassTimestampWrites<'
}
}
/// The compute pass's twin of `stamps`.
fn compute_stamps(queries: &wgpu::QuerySet, i: u32) -> wgpu::ComputePassTimestampWrites<'_> {
wgpu::ComputePassTimestampWrites {
query_set: queries,
beginning_of_pass_write_index: Some(2 * i),
end_of_pass_write_index: Some(2 * i + 1),
}
}
/// Each pass's time in milliseconds from its (beginning, end) timestamps, `None` for a pass
/// the GPU left unstamped. 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.
@@ -35,6 +35,16 @@ pub struct PackInputs {
/// `heldItemId`, `heldItemId2` (item.properties, 5.3) and `heldBlockLightValue(2)`.
pub held_items: [i32; 2],
pub held_light: [i32; 2],
/// `isRightHanded` (the Main Hand option) and `is_on_ground`.
pub right_handed: bool,
pub on_ground: bool,
/// The dimension type's lowest block (`bedrockLevel`), build height (`heightLimit`),
/// logical height, sky light and ceiling.
pub min_y: i32,
pub height: i32,
pub logical_height: i32,
pub has_skylight: bool,
pub has_ceiling: bool,
/// The values native mods hand their packs (`frame.mods`, `native_shader_params`).
pub mod_params: [[f32; 4]; 8],
}
@@ -173,6 +183,13 @@ pub const NAMES: &[&str] = &[
"playerLookVector",
"playerBodyVector",
"firstPersonCamera",
"isRightHanded",
"is_on_ground",
"bedrockLevel",
"heightLimit",
"logicalHeightLimit",
"hasSkylight",
"hasCeiling",
];
/// Values carried from frame to frame.
@@ -380,6 +397,13 @@ impl FrameUniforms {
"relativeEyePosition" => v3((frame.camera - frame.eye).as_vec3()),
"playerLookVector" | "playerBodyVector" => v3(look),
"firstPersonCamera" => Value::i(&[i32::from(frame.first_person)]),
"isRightHanded" => Value::i(&[i32::from(inputs.right_handed)]),
"is_on_ground" => Value::i(&[i32::from(inputs.on_ground)]),
"bedrockLevel" => Value::i(&[inputs.min_y]),
"heightLimit" => Value::i(&[inputs.height]),
"logicalHeightLimit" => Value::i(&[inputs.logical_height]),
"hasSkylight" => Value::i(&[i32::from(inputs.has_skylight)]),
"hasCeiling" => Value::i(&[i32::from(inputs.has_ceiling)]),
"eyeAltitude" => Value::f(&[frame.camera.y as f32]),
"upPosition" => v3(to_view(Vec3::new(0.0, 100.0, 0.0))),
"sunPosition" => v3(sun),
@@ -437,14 +437,15 @@ impl Post {
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
screen: &wgpu::TextureView,
mut timing: Option<&mut super::pack_timing::PassTiming>,
) {
let Some(target) = &self.target else { return };
let Some(taa) = target.taa.as_ref().filter(|_| self.taa_previous.is_some()) else {
self.finish_with(encoder, screen, &target.group);
self.finish_with(encoder, screen, &target.group, timing.as_deref_mut());
return;
};
let Some(res) = &self.taa_resources else {
self.finish_with(encoder, screen, &target.group);
self.finish_with(encoder, screen, &target.group, timing.as_deref_mut());
return;
};
let i = (self.taa_frame % 2) as usize;
@@ -460,6 +461,7 @@ impl Post {
})];
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Horizon TAA"),
timestamp_writes: timing.as_deref_mut().and_then(|t| t.writes("TAA")),
color_attachments: &attachments,
..Default::default()
});
@@ -467,7 +469,7 @@ impl Post {
pass.set_bind_group(0, &taa.resolve[i], &[]);
pass.draw(0..3, 0..1);
drop(pass);
self.finish_with(encoder, screen, &taa.show[i]);
self.finish_with(encoder, screen, &taa.show[i], timing);
}
/// Draw what `group` binds (`bind`) onto `screen`.
@@ -476,6 +478,7 @@ impl Post {
encoder: &mut wgpu::CommandEncoder,
screen: &wgpu::TextureView,
group: &wgpu::BindGroup,
timing: Option<&mut super::pack_timing::PassTiming>,
) {
let attachments = [Some(wgpu::RenderPassColorAttachment {
view: screen,
@@ -488,6 +491,7 @@ impl Post {
})];
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Horizon post"),
timestamp_writes: timing.and_then(|t| t.writes("Post")),
color_attachments: &attachments,
..Default::default()
});
+25 -1
View File
@@ -325,6 +325,19 @@ pub fn hdr_offered() -> bool {
HDR_OFFERED.load(std::sync::atomic::Ordering::Relaxed)
}
static TRANSLUCENT_BATCHING_OFFERED: std::sync::atomic::AtomicBool =
std::sync::atomic::AtomicBool::new(false);
/// Whether the device can batch translucent quads (`Graphics::translucent_batching`): set when
/// the renderer starts; not without indirect multi-draw or on OpenGL, which loops over a
/// multi-draw's entries on the CPU, nor where vanilla frames draw flat (they read the section
/// arena's translucent quads).
pub fn offer_translucent_batching(on: bool) {
TRANSLUCENT_BATCHING_OFFERED.store(on, std::sync::atomic::Ordering::Relaxed);
}
pub fn translucent_batching_offered() -> bool {
TRANSLUCENT_BATCHING_OFFERED.load(std::sync::atomic::Ordering::Relaxed)
}
/// The farthest LOD Distance, in chunks, on integrated GPUs other than
/// Apple's: they share the CPU's memory bandwidth, and past it far terrain
/// costs them more frame time than it is worth.
@@ -415,6 +428,11 @@ pub struct Graphics {
pub shadow_cache: bool,
/// The graphics API (Windows only; see `GraphicsApi`).
pub api: GraphicsApi,
/// Translucent quads in vertex buffers of their own (`gfx`'s translucent arena), so their
/// back-to-front draws go out in a few calls, for a little more video memory. Off by
/// default for now; read when a renderer is made (start, resource-pack reload), where it is
/// offered (`translucent_batching_offered`).
pub translucent_batching: bool,
/// The shader pack picked in `shaderpacks/` (`shader_pack_list`); empty: none. Its key is
/// not `shaderPack`, which versions before the clean-room rewrite saved and which stays
/// ignored.
@@ -454,6 +472,7 @@ impl Default for Graphics {
battery: false,
shadow_cache: false,
api: GraphicsApi::Auto,
translucent_batching: false,
shader_pack: String::new(),
}
}
@@ -544,6 +563,7 @@ impl Graphics {
"hdrKey" if value.starts_with("key.") => g.hdr_key = value.to_owned(),
"batteryMode" => g.battery = flag(g.battery),
"shadowCache" => g.shadow_cache = flag(g.shadow_cache),
"translucentBatching" => g.translucent_batching = flag(g.translucent_batching),
"graphicsApi" => g.api = GraphicsApi::from_name(value).unwrap_or_default(),
"pickedShaderPack" => g.shader_pack = value.to_owned(),
"overlay" => {
@@ -560,7 +580,7 @@ impl Graphics {
}
pub fn serialize(&self) -> String {
format!(
"preset:{}\nlodDistance:{}\nfarDetail:{}\ncaveCulling:{}\nentityCulling:{}\nchunkUpdates:{}\nuploadBudgetKiB:{}\ntextureBudgetMiB:{}\nsky:{}\nfog:{}\nweather:{}\nanimatedTextures:{}\nditherCutoutMips:{}\npresentMode:{}\nframePacing:{}\nworkers:{}\noverlay:{}\nresolutionScale:{}\nfxaa:{}\ntaa:{}\nupscaling:{}\nscreenshotQuality:{}\nhdr:{}\nhdrPaperWhite:{}\nhdrPeak:{}\nhdrBlackLevel:{}\nhdrUiBrightness:{}\nhdrKey:{}\nbatteryMode:{}\nshadowCache:{}\ngraphicsApi:{}\npickedShaderPack:{}\n",
"preset:{}\nlodDistance:{}\nfarDetail:{}\ncaveCulling:{}\nentityCulling:{}\nchunkUpdates:{}\nuploadBudgetKiB:{}\ntextureBudgetMiB:{}\nsky:{}\nfog:{}\nweather:{}\nanimatedTextures:{}\nditherCutoutMips:{}\npresentMode:{}\nframePacing:{}\nworkers:{}\noverlay:{}\nresolutionScale:{}\nfxaa:{}\ntaa:{}\nupscaling:{}\nscreenshotQuality:{}\nhdr:{}\nhdrPaperWhite:{}\nhdrPeak:{}\nhdrBlackLevel:{}\nhdrUiBrightness:{}\nhdrKey:{}\nbatteryMode:{}\nshadowCache:{}\ngraphicsApi:{}\npickedShaderPack:{}\ntranslucentBatching:{}\n",
self.preset.name(),
self.lod_distance,
self.far_detail.name(),
@@ -600,6 +620,7 @@ impl Graphics {
bool_text(self.shadow_cache),
self.api.name(),
self.shader_pack,
bool_text(self.translucent_batching),
)
}
/// Binds Horizon's graphics key `name` (`HDR_KEY`); false for another name.
@@ -747,10 +768,13 @@ mod tests {
battery: true,
shadow_cache: true,
api: GraphicsApi::Dx12,
translucent_batching: true,
shader_pack: "Golden Hour".into(),
};
assert_eq!(Graphics::parse(&g.serialize()), g);
assert_eq!(Graphics::parse(""), Graphics::default());
// Translucent Batching is opt-in for now: only the file turns it on.
assert!(!Graphics::default().translucent_batching);
// 0 is Auto; anything else is a peak the slider reaches.
assert_eq!(Graphics::parse("hdrPeak:0\n").hdr_peak, 0);
assert_eq!(Graphics::parse("hdrPeak:50\n").hdr_peak, 200);
+4
View File
@@ -2017,9 +2017,11 @@ impl HudRenderer {
batch,
size,
0..batch.vertices.len() as u32,
None,
);
}
/// Draw part of the prepared batch, preserving its blend/depth runs.
#[allow(clippy::too_many_arguments)]
pub fn draw_range(
&self,
encoder: &mut wgpu::CommandEncoder,
@@ -2028,6 +2030,7 @@ impl HudRenderer {
batch: &HudBatch,
size: [u32; 2],
vertices: std::ops::Range<u32>,
mut timing: Option<&mut crate::gfx::pack_timing::PassTiming>,
) {
if vertices.is_empty() || size.contains(&0) {
return;
@@ -2058,6 +2061,7 @@ impl HudRenderer {
};
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("HUD"),
timestamp_writes: timing.as_deref_mut().and_then(|t| t.writes("HUD")),
color_attachments: &attachments,
depth_stencil_attachment: clip.map(|_| wgpu::RenderPassDepthStencilAttachment {
view: depth,
+240 -23
View File
@@ -494,7 +494,20 @@ struct Plan {
/// selection is rebuilt, without sorting the whole set.
by_level: [Vec<TileKey>; MAX_LEVEL as usize + 1],
elevation: i32,
camera: [i32; 2],
/// The squared distance within which no coarser tile stands in for
/// missing detail (`stands_in`): the seam with the near chunks waits
/// for its own tiles instead of showing broad boxes beside them.
near: i64,
/// The squared distance within which planned tiles load before the
/// coarse backdrop (`wanted_tiles`).
first: i64,
}
/// Chunks past the render distance where only planned tiles are drawn (two
/// past the last near chunk, `streaming::in_view_distance`), and where they
/// load first.
const SEAM_CHUNKS: i32 = 3;
const FIRST_CHUNKS: i32 = 4;
impl Plan {
#[cfg(test)]
fn new(camera: [i32; 2], detail: i32, distance: u32) -> Self {
@@ -518,6 +531,9 @@ impl Plan {
let size = 16 << level;
let mut result = Self {
elevation,
camera,
near: i64::from((detail + SEAM_CHUNKS) * 16).pow(2),
first: i64::from((detail + FIRST_CHUNKS) * 16).pow(2),
..Self::default()
};
for z in (camera[1] - range).div_euclid(size)..=(camera[1] + range).div_euclid(size) {
@@ -589,7 +605,7 @@ impl Plan {
if complete {
return true;
}
if ready.contains_key(&key) {
if ready.contains_key(&key) && self.stands_in(key) {
// Its mesh is clipped around every selected finer tile.
out.push(key);
return true;
@@ -602,6 +618,26 @@ impl Plan {
false
}
}
/// Whether a split tile may draw for its missing children: only away
/// from the near chunks, where its cells are small on screen.
fn stands_in(&self, key: TileKey) -> bool {
key.reach(self.camera) >= self.near
}
/// The coarse tile sampled to cover `leaf` while it loads: two levels
/// up (16 times fewer tiles, each as cheap as a leaf to sample), one
/// where that would reach the seam, none beside it.
fn backdrop(&self, leaf: TileKey) -> Option<TileKey> {
let mut backdrop = None;
let mut key = leaf;
for _ in 0..2 {
key = key.parent();
if !self.nodes.contains(&key) || !self.stands_in(key) {
break;
}
backdrop = Some(key);
}
backdrop
}
/// Whether chunk column (x, z) is drawn by the tile planned there (ready;
/// at level 0, also `filled`) rather than by a coarser ancestor standing
/// in for it, or is past the far terrain.
@@ -625,6 +661,47 @@ impl Plan {
self.split.contains(&key) || (ready.contains_key(&key) && (key.level > 0 || filled(key)))
}
}
/// The tiles to sample, `Wanted`'s order best first. First the planned
/// tiles at the seam with the near chunks, nearest first: coarse tiles
/// there cost a far tile's work for broad boxes that fine tiles replace
/// moments later. Then a coarse backdrop (`Plan::backdrop`) out to the
/// horizon, then every other planned tile nearest first, each replacing its
/// part of the backdrop: the horizon fills in early and sharpens from the
/// player outward, whichever way they look.
fn wanted_tiles(
plan: &Plan,
ready: &FxHashMap<TileKey, impl Sized>,
camera: [i32; 2],
) -> impl Iterator<Item = Wanted> {
let wanted = |class, key: TileKey| Wanted {
class,
distance: i64::from(key.distance(camera)),
x: key.x,
z: key.z,
level: key.level,
};
let mut backdrop = FxHashSet::default();
let mut tiles: Vec<_> = plan
.nodes
.iter()
.filter(|key| !plan.split.contains(key) && !ready.contains_key(key))
.map(|&key| {
if let Some(coarse) = plan.backdrop(key)
&& !ready.contains_key(&coarse)
{
backdrop.insert(coarse);
}
let first = key.reach(camera) < plan.first;
wanted(if first { SEAM } else { REST }, key)
})
.collect();
tiles.extend(backdrop.into_iter().map(|key| wanted(BACKDROP, key)));
tiles.into_iter()
}
/// `Wanted::class`es, best first.
const SEAM: u8 = 0;
const BACKDROP: u8 = 1;
const REST: u8 = 2;
/// Visited chunks' summaries (`Lod::saved`) and their reductions.
type Saved = FxHashMap<(i32, i32), Arc<[Column; 256]>>;
type Reduced = FxHashMap<(i32, i32), Arc<ReducedColumns>>;
@@ -791,9 +868,11 @@ impl Ord for MeshQueued {
.then(other.order.cmp(&self.order))
}
}
/// A tile the sampler should produce next, nearest first.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
/// A tile the sampler should produce next: by `class`
/// (`wanted_tiles`), then nearest first.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Wanted {
class: u8,
distance: i64,
x: i32,
z: i32,
@@ -1411,14 +1490,15 @@ impl Lod {
let present = self.ready.contains_key(&key);
// Covered level-0 stand-ins complete their parent but are never drawn.
let ready = present && !(key.level == 0 && self.covered.contains(&(key.x, key.z)));
// Mirrors `Plan::select`: a ready split tile is a complete
// stand-in even while its children are still incomplete, so no
// second fallback is drawn above it.
// Mirrors `Plan::select`: a ready split tile away from the seam is a
// complete stand-in even while its children are still incomplete,
// so no second fallback is drawn above it.
let (complete, active) = if self.plan.split.contains(&key) {
let children = key.children().into_iter().all(|child| {
!self.plan.nodes.contains(&child)
|| self.complete.get(&child).copied().unwrap_or(false)
});
let ready = ready && self.plan.stands_in(key);
(children || ready, ready && !children)
} else {
(present, ready)
@@ -2183,11 +2263,10 @@ impl Lod {
z: wanted.z,
level: wanted.level,
};
// Final tiles only: a coarse tile under the near area is work
// that finer tiles replace moments later.
// Final tiles and the backdrop only (`wanted_tiles`).
if self.pending.contains(&key)
|| self.ready.contains_key(&key)
|| self.plan.split.contains(&key)
|| (self.plan.split.contains(&key) && wanted.class != BACKDROP)
|| !self.plan.nodes.contains(&key)
{
continue;
@@ -2203,17 +2282,8 @@ impl Lod {
}
fn rebuild_wanted(&mut self, camera: [i32; 2]) {
self.wanted.clear();
for &key in &self.plan.nodes {
if self.plan.split.contains(&key) || self.ready.contains_key(&key) {
continue;
}
self.wanted.push(std::cmp::Reverse(Wanted {
distance: i64::from(key.distance(camera)),
x: key.x,
z: key.z,
level: key.level,
}));
}
self.wanted
.extend(wanted_tiles(&self.plan, &self.ready, camera).map(std::cmp::Reverse));
}
#[cfg(test)]
@@ -2607,14 +2677,14 @@ mod tests {
(ox..ox + key.size()).contains(&(x * 16)) && (oz..oz + key.size()).contains(&(z * 16))
};
// 24 chunks out, now past the render distance: its planned tile was
// split before, so never sampled, and a root stands in for it.
// split before, so never sampled. The ready root reaches the seam, so
// it stands in for nothing: the column stays until its tile is in.
let column = (24, 0);
let mut selected = Vec::new();
for &root in &after.roots {
after.select(root, &ready, &mut selected);
}
let drawn = selected.iter().filter(|key| contains(key, column));
assert_eq!(drawn.map(|key| key.level).min(), Some(6));
assert!(!selected.iter().any(|key| contains(key, column)));
assert!(!after.replaces(column, &ready, |_| true));
let planned = TileKey {
x: 12,
@@ -3258,6 +3328,153 @@ mod tests {
Ok(())
}
#[test]
fn far_cells_past_render_distance_12_start_one_block_wide() {
// Leaves' cells: 1 block to 256 blocks out, 2 to 512 (Full); Reduced
// keeps 1 to the near terrain's edge and 2 to 256.
for (reduced, [one, two]) in [(false, [256, 512]), (true, [208, 256])] {
let plan = Plan::with_previous([8, 8], 12, 128, &Plan::default(), 0, reduced);
for key in plan.nodes.iter().filter(|key| !plan.split.contains(key)) {
let distance = key.distance([8, 8]);
if distance < one {
assert_eq!(key.size() / 16, 1, "{key:?} reduced {reduced}");
} else if distance < two {
assert!(key.size() / 16 <= 2, "{key:?} reduced {reduced}");
}
}
}
}
/// Loads a render distance 12 view one sample job at a time, in
/// `wanted_tiles`' order. Counts the jobs, and their density lattice
/// points (each job's cost), before every far column within two chunks
/// of the near terrain is drawn as finely as the plan wants it (one
/// block wide in Full): until then nothing is drawn there. And before
/// every far column is drawn, by its tile or a coarse stand-in: until
/// then the far terrain ends short of the horizon. Checks that no coarse
/// tile is ever drawn at the seam.
fn jobs_until_fine_seam(camera: [i32; 2], reduced: bool) -> ([(usize, i32); 2], usize) {
let (detail, distance) = (12, 128);
let center = (camera[0].div_euclid(16), camera[1].div_euclid(16));
let mut covered = FxHashSet::default();
for z in center.1 - detail..=center.1 + detail {
for x in center.0 - detail..=center.0 + detail {
if crate::streaming::in_view_distance(center, detail, x, z) {
covered.insert((x, z));
}
}
}
let plan = Plan::with_previous(camera, detail, distance, &Plan::default(), 0, reduced);
let leaf = |x: i32, z: i32| {
(0..=MAX_LEVEL)
.map(|level| TileKey {
x: x.div_euclid(16 << level),
z: z.div_euclid(16 << level),
level,
})
.find(|key| plan.nodes.contains(key) && !plan.split.contains(key))
};
let mut seam = Vec::new();
for z in (camera[1] - 256..camera[1] + 256).step_by(2) {
for x in (camera[0] - 256..camera[0] + 256).step_by(2) {
let chunk = (x.div_euclid(16), z.div_euclid(16));
let near = (-2..=2)
.any(|dz| (-2..=2).any(|dx| covered.contains(&(chunk.0 + dx, chunk.1 + dz))));
if !covered.contains(&chunk) && near {
seam.push((x, z, leaf(x, z).unwrap()));
}
}
}
let mut horizon = Vec::new();
for z in (camera[1] - 2048..camera[1] + 2048).step_by(64) {
for x in (camera[0] - 2048..camera[0] + 2048).step_by(64) {
if !covered.contains(&(x.div_euclid(16), z.div_euclid(16))) && leaf(x, z).is_some()
{
horizon.push((x, z));
}
}
}
let mut ready: FxHashMap<TileKey, ()> = FxHashMap::default();
for key in &plan.nodes {
if key.level == 0 && covered.contains(&(key.x, key.z)) {
ready.insert(*key, ());
}
}
let leaves = plan
.nodes
.iter()
.filter(|k| !plan.split.contains(k))
.count();
let (mut cost, mut fine, mut reached) = (0, None, None);
for jobs in 0.. {
let mut selected = Vec::new();
for &root in &plan.roots {
plan.select(root, &ready, &mut selected);
}
let active: FxHashSet<_> = selected.into_iter().collect();
let drawn = |x: i32, z: i32, levels: u8| {
(0..=levels).any(|level| {
active.contains(&TileKey {
x: x.div_euclid(16 << level),
z: z.div_euclid(16 << level),
level,
})
})
};
for &(x, z, leaf) in &seam {
assert!(drawn(x, z, leaf.level) || !drawn(x, z, MAX_LEVEL));
}
if fine.is_none() && seam.iter().all(|&(x, z, leaf)| drawn(x, z, leaf.level)) {
fine = Some((jobs, cost));
}
if reached.is_none() && horizon.iter().all(|&(x, z)| drawn(x, z, MAX_LEVEL)) {
reached = Some((jobs, cost));
}
if let (Some(fine), Some(reached)) = (fine, reached) {
return ([fine, reached], leaves);
}
let next = wanted_tiles(&plan, &ready, camera).min().unwrap();
let next = TileKey {
x: next.x,
z: next.z,
level: next.level,
};
// `lod_grid`'s density lattice: one point per max(stride, 4) blocks.
let stride = next.size() / 16;
let lattice = 16 * stride / stride.max(4) + 1;
cost += lattice * lattice;
ready.insert(next, ());
}
unreachable!()
}
#[test]
fn the_far_terrain_next_to_the_near_chunks_loads_first() {
// Measured: 270 to 301 jobs, all of them fine tiles (7500 to 8500
// lattice points). A coarse tile first costs up to 289 points; the
// order that filled a ring ahead first and refined broad tiles every
// other job took 54000 to 119000 for the seam in view alone.
for camera in [[8, 8], [40, -9], [-300, 517]] {
for reduced in [false, true] {
let ([(jobs, cost), _], _) = jobs_until_fine_seam(camera, reduced);
assert!(jobs < 320, "{jobs} jobs");
assert!(cost < 9000, "{cost} lattice points");
}
}
}
#[test]
fn a_coarse_backdrop_reaches_the_horizon_early() {
// Sampling only planned tiles, nearest first, reached the horizon
// with the last of ~3000 jobs in Full (500000 lattice points), ~1050
// in Reduced (200000): for minutes on a slow machine the far terrain
// ended in a hard edge. The backdrop adds ~230 coarse tiles (~80 in
// Reduced), about a tenth more work in all.
for camera in [[8, 8], [40, -9], [-300, 517]] {
for reduced in [false, true] {
let ([_, (jobs, cost)], leaves) = jobs_until_fine_seam(camera, reduced);
assert!(jobs < 1000 && jobs < leaves, "{jobs} jobs of {leaves}");
assert!(cost < 100_000, "{cost} lattice points");
}
}
}
#[test]
fn extra_sampling_threads_only_on_large_machines() {
assert_eq!([1, 2, 4, 8, 11].map(sampling_threads), [1, 1, 1, 4, 5]);
+8 -1
View File
@@ -160,7 +160,14 @@ impl Mesher {
for slot in &mut faces {
let Some(face) = matching.next() else { break };
*slot = Some(Style {
layer: face.layer as u16 | if leaves { OPAQUE_LAYER } else { 0 },
// As the mesher marks them: opaque leaves and faces over opaque
// texels skip the cutout and draw in the opaque pass.
layer: face.layer as u16
| if leaves || face.opaque {
OPAQUE_LAYER
} else {
0
},
tint: if face.quad.tint_index >= 0 {
tint
} else {
@@ -172,6 +172,7 @@ impl MenuBlur {
target: &wgpu::TextureView,
size: [u32; 2],
radius: f32,
mut timing: Option<&mut crate::gfx::pack_timing::PassTiming>,
) {
self.ensure(device, size);
encoder.copy_texture_to_texture(
@@ -224,6 +225,7 @@ impl MenuBlur {
})];
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Menu blur pass"),
timestamp_writes: timing.as_deref_mut().and_then(|t| t.writes("Menu blur")),
color_attachments: &attachments,
..Default::default()
});
@@ -1024,6 +1024,7 @@ impl PostEffects {
size: [u32; 2],
depth: Option<&wgpu::TextureView>,
req: &Request,
mut timing: Option<&mut crate::gfx::pack_timing::PassTiming>,
) -> bool {
self.use_root(req.root);
let mut applied = Vec::new();
@@ -1099,6 +1100,7 @@ impl PostEffects {
(&work.1, size),
0.0,
format,
timing.as_deref_mut(),
);
self.staging = Some(staging);
for id in &applied {
@@ -1113,6 +1115,7 @@ impl PostEffects {
depth,
req.menu_blur_radius,
format,
timing.as_deref_mut(),
);
self.chains.insert(id.clone(), Some(chain));
}
@@ -1126,6 +1129,7 @@ impl PostEffects {
(target, size),
0.0,
format,
timing,
);
self.blit = Some(blit);
self.work = Some(work);
@@ -1144,6 +1148,7 @@ impl PostEffects {
main_depth: Option<&wgpu::TextureView>,
menu_radius: f32,
format: wgpu::TextureFormat,
mut timing: Option<&mut crate::gfx::pack_timing::PassTiming>,
) {
for pass in &mut chain.passes {
if let Some(translated) = &mut pass.translated {
@@ -1203,6 +1208,7 @@ impl PostEffects {
{
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Post effect depth clear"),
timestamp_writes: timing.as_deref_mut().and_then(|t| t.writes("Post clear")),
color_attachments: &[],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view,
@@ -1221,6 +1227,7 @@ impl PostEffects {
let c = t.clear.map(f64::from);
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Post effect target clear"),
timestamp_writes: timing.as_deref_mut().and_then(|t| t.writes("Post clear")),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &chain.textures[i - 1].1,
resolve_target: None,
@@ -1274,6 +1281,7 @@ impl PostEffects {
&inputs,
(view(pass.output), sizes[pass.output]),
format,
timing.as_deref_mut(),
);
continue;
}
@@ -1287,6 +1295,7 @@ impl PostEffects {
(view(pass.output), sizes[pass.output]),
menu_radius,
format,
timing.as_deref_mut(),
);
}
}
@@ -1303,6 +1312,7 @@ impl PostEffects {
inputs: &[(&wgpu::TextureView, [u32; 2])],
output: (&wgpu::TextureView, [u32; 2]),
format: wgpu::TextureFormat,
timing: Option<&mut crate::gfx::pack_timing::PassTiming>,
) {
let Some(pipeline) = translated.pipelines.get(&format) else {
return;
@@ -1353,6 +1363,7 @@ impl PostEffects {
});
let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Post effect pass"),
timestamp_writes: timing.and_then(|t| t.writes("Post effect")),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: output.0,
resolve_target: None,
@@ -1381,6 +1392,7 @@ impl PostEffects {
output: (&wgpu::TextureView, [u32; 2]),
menu_radius: f32,
format: wgpu::TextureFormat,
timing: Option<&mut crate::gfx::pack_timing::PassTiming>,
) {
// SamplerInfo: OutSize, then each input's size.
let info = [
@@ -1441,6 +1453,7 @@ impl PostEffects {
});
let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Post effect pass"),
timestamp_writes: timing.and_then(|t| t.writes("Post effect")),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: output.0,
resolve_target: None,
@@ -1715,6 +1728,7 @@ mod tests {
menu_blur_radius: 5.0,
globals: Globals::default(),
},
None,
);
let buffer = crate::gfx::copy_frame(device, &mut encoder, &frame, [8, 4]);
queue.submit([encoder.finish()]);
@@ -1845,6 +1859,7 @@ mod tests {
menu_blur_radius: 0.0,
globals: Globals::default(),
},
None,
));
let buffer = crate::gfx::copy_frame(device, &mut encoder, &frame, [8, 4]);
queue.submit([encoder.finish()]);
@@ -25,6 +25,9 @@ pub const UI: usize = 10;
/// sources: `clamp((int) sqrt(255), 2, 8)` streaming, the rest static.
pub const STATIC_CHANNELS: usize = 247;
pub const STREAMING_CHANNELS: usize = 8;
/// Static channels only sounds played to the listener (no position) may take
/// while the `uiSoundReserve` extra is on.
pub const HEARD_RESERVE: usize = 16;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PlayResult {
@@ -792,6 +795,9 @@ pub struct SoundEngine {
pub source_volumes: [f32; 11],
/// A `SoundEventListener` is registered.
pub listening: bool,
/// Static channels positional sounds leave free for the ones played to the
/// listener: 0 as vanilla, `HEARD_RESERVE` with the `uiSoundReserve` extra.
pub listener_reserve: usize,
pub notifications: Vec<Notification>,
}
fn handle_channel(h: &Handle) -> Option<ChannelId> {
@@ -819,6 +825,7 @@ impl SoundEngine {
limits: [STATIC_CHANNELS, STREAMING_CHANNELS],
source_volumes: [1.0; 11],
listening: false,
listener_reserve: 0,
notifications: Vec::new(),
}
}
@@ -1100,7 +1107,15 @@ impl SoundEngine {
let looping = instance.looping && instance.delay <= 0;
let streaming = sound.stream;
let pool = usize::from(streaming);
let channel = if self.used[pool] >= self.limits[pool] {
// With the `uiSoundReserve` extra, sounds out in the world leave a few static channels free, so a flood of
// them (a crater's worth of blocks breaking) can't stop the ones played to the listener.
let positional = !instance.relative && instance.attenuation != Attenuation::None;
let reserved = if positional && !streaming {
self.listener_reserve
} else {
0
};
let channel = if self.used[pool] + reserved >= self.limits[pool] {
None
} else {
backend.create(streaming)
@@ -1549,4 +1564,47 @@ mod tests {
assert_eq!(s.volume, 0.0);
assert!(!s.stopped);
}
#[test]
fn world_sounds_leave_channels_for_the_listener_only_with_the_extra() {
use test_backend::Null;
let mut registry = SoundRegistry::default();
registry.add_file(
"minecraft",
br#"{"block.stone.break": {"sounds": ["dig/stone1"]},
"ui.button.click": {"sounds": ["random/click"]}}"#,
&|_, _| true,
);
let random = || LegacyRandom::new(0);
let block = |i: usize| {
let at = [i as f64, 64.0, 0.0];
let source = crate::settings::sound_source("minecraft:block.stone.break");
SoundInstance::at(
"minecraft:block.stone.break",
source,
1.0,
1.0,
random(),
at,
)
};
let click = || SoundInstance::for_ui("minecraft:ui.button.click", 1.0, 0.25, random());
for reserve in [0, HEARD_RESERVE] {
let mut engine = SoundEngine::new(registry.clone());
engine.listener_reserve = reserve;
let mut backend = Null::default();
// Positional sounds take the static channels the reserve leaves; then their plays fail.
let started = (0..STATIC_CHANNELS)
.filter(|&i| {
engine.play_new(block(i), false, &mut backend).1 == PlayResult::Started
})
.count();
assert_eq!(started, STATIC_CHANNELS - reserve);
let heard = engine.play_new(click(), false, &mut backend).1;
assert_eq!(
heard == PlayResult::Started,
reserve > 0,
"reserve {reserve}"
);
}
}
}
+80 -6
View File
@@ -92,19 +92,29 @@ fn sprite_level(uv: vec2<f32>, layer: u32, lod: f32) -> vec4<f32> {
}
// texture_sampling.glsl's sampleNearest: the sprite's texels stay square pixels, blending only
// across the one screen pixel where two meet, so a linear (and anisotropic) sampler keeps them crisp.
// `tiled`: a distant tile's face takes its sprite again per block, so its gradients span many
// sprites while `uv` stays inside one (see `tile_uv` below).
fn sample_nearest(uv: vec2<f32>, layer: u32, pixel: vec2<f32>, du: vec2<f32>, dv: vec2<f32>,
texel_screen: vec2<f32>) -> vec4<f32> {
texel_screen: vec2<f32>, tiled: bool) -> vec4<f32> {
let texel_coords = uv / pixel;
let center = round(texel_coords) - 0.5;
var offset = texel_coords - center;
offset = (offset - 0.5) * pixel / max(texel_screen, vec2(1e-12)) + 0.5;
offset = clamp(offset, vec2(0.0), vec2(1.0));
return sprite_grad((center + offset) * pixel, layer, du, dv);
let at = (center + offset) * pixel;
if tiled {
// An anisotropic sampler lays its taps along those gradients, past the sprite's edge,
// where the clamped sampler reads that edge texel again: long streaks on the far tiles.
// Asking for the level the gradients give takes the one tap an anisotropy of 1 would.
let level = max(length(du), length(dv)) / min(pixel.x, pixel.y);
return sprite_level(at, layer, max(0.0, log2(level)));
}
return sprite_grad(at, layer, du, dv);
}
// sampleRGSS: four rotated-grid samples of the two nearest mip levels, faded in as the texels
// shrink below two screen pixels, over sample_nearest.
fn sample_rgss(uv: vec2<f32>, layer: u32, pixel: vec2<f32>, du: vec2<f32>, dv: vec2<f32>,
texel_screen: vec2<f32>) -> vec4<f32> {
texel_screen: vec2<f32>, tiled: bool) -> vec4<f32> {
let max_texel = max(texel_screen.x, texel_screen.y);
let min_pixel = min(pixel.x, pixel.y);
let blend = smoothstep(min_pixel, min_pixel * 2.0, max_texel);
@@ -124,7 +134,7 @@ fn sample_rgss(uv: vec2<f32>, layer: u32, pixel: vec2<f32>, du: vec2<f32>, dv: v
sum_high += sprite_level(p, layer, low + 1.0);
}
let rgss = mix(sum_low * 0.25, sum_high * 0.25, mip_blend);
return mix(sample_nearest(uv, layer, pixel, du, dv, texel_screen), rgss, blend);
return mix(sample_nearest(uv, layer, pixel, du, dv, texel_screen, tiled), rgss, blend);
}
// Distant terrain tiles (lod/mesh.rs) count their UVs in blocks past 1: each block takes the
// sprite again, as separate faces would under the clamped sampler. Sprite UVs in [0, 1] stay.
@@ -137,12 +147,15 @@ fn sprite(tiled: vec2<f32>, layer: u32) -> vec4<f32> {
let du = dpdx(tiled);
let dv = dpdy(tiled);
let uv = tile_uv(tiled);
// The wrap shifts a face's UVs by whole blocks, so half of one tells a tile's face from a
// block's (whose UVs stay inside its sprite).
let tiled_face = any(tiled - uv >= vec2(0.5));
let pixel = vec2(1.0 / globals.filtering.y);
let texel_screen = sqrt(du * du + dv * dv);
if u32(globals.filtering.x) == 1u {
return sample_rgss(uv, layer, pixel, du, dv, texel_screen);
return sample_rgss(uv, layer, pixel, du, dv, texel_screen, tiled_face);
}
return sample_nearest(uv, layer, pixel, du, dv, texel_screen);
return sample_nearest(uv, layer, pixel, du, dv, texel_screen, tiled_face);
}
struct TerrainOut {
@@ -295,6 +308,56 @@ fn fade_code(w: f32) -> u32 {
return terrain_vertex(position, terrain_uv(uv, origin.w), layer, color, light, origin.xyz,
origin.w);
}
// Solid terrain draws in two passes from the same draws: first the quads with no
// cutout that are not fading, through `fs_opaque`, which never discards, so a tiled
// GPU (Apple's) keeps hiding the pixels behind them; then the rest through
// `fs_solid`. One discard anywhere in a pipeline's shader turned that hiding off for
// all the terrain it drew. A quad the pass leaves out is decided before any of the
// vertex's work and lies outside the clip volume: it makes no pixel.
fn opaque_quad(layer: u32, w: f32) -> bool {
return (layer & 0x8000u) != 0u && fade_code(w) == 0u;
}
fn left_out() -> TerrainOut {
var out: TerrainOut;
out.clip = vec4(2.0, 2.0, 2.0, 1.0);
return out;
}
@vertex fn vs_terrain_opaque(
@location(0) position: vec3<f32>, @location(1) uv: vec2<u32>,
@location(2) layer: u32, @location(3) color: vec4<f32>, @location(4) light: vec2<u32>,
@location(5) origin: vec4<f32>) -> TerrainOut {
if !opaque_quad(layer, origin.w) { return left_out(); }
return terrain_vertex(position, terrain_uv(uv, origin.w), layer, color, light, origin.xyz,
origin.w);
}
@vertex fn vs_terrain_cutout(
@location(0) position: vec3<f32>, @location(1) uv: vec2<u32>,
@location(2) layer: u32, @location(3) color: vec4<f32>, @location(4) light: vec2<u32>,
@location(5) origin: vec4<f32>) -> TerrainOut {
if opaque_quad(layer, origin.w) { return left_out(); }
return terrain_vertex(position, terrain_uv(uv, origin.w), layer, color, light, origin.xyz,
origin.w);
}
@vertex fn vs_terrain_flat_opaque(
@builtin(vertex_index) vertex: u32,
@location(0) position: vec3<f32>, @location(1) uv: vec2<u32>,
@location(2) layer: u32, @location(3) color: vec4<f32>, @location(4) light: vec2<u32>
) -> TerrainOut {
let origin = slot_origins[block_slots[vertex >> 8u]];
if !opaque_quad(layer, origin.w) { return left_out(); }
return terrain_vertex(position, terrain_uv(uv, origin.w), layer, color, light, origin.xyz,
origin.w);
}
@vertex fn vs_terrain_flat_cutout(
@builtin(vertex_index) vertex: u32,
@location(0) position: vec3<f32>, @location(1) uv: vec2<u32>,
@location(2) layer: u32, @location(3) color: vec4<f32>, @location(4) light: vec2<u32>
) -> TerrainOut {
let origin = slot_origins[block_slots[vertex >> 8u]];
if opaque_quad(layer, origin.w) { return left_out(); }
return terrain_vertex(position, terrain_uv(uv, origin.w), layer, color, light, origin.xyz,
origin.w);
}
fn terrain_vertex(position: vec3<f32>, uv: vec2<f32>, layer: u32, color: vec4<f32>,
light: vec2<u32>, origin: vec3<f32>, w: f32) -> TerrainOut {
let pos = position + origin;
@@ -344,6 +407,13 @@ fn brightness(c: vec3<f32>) -> f32 {
@fragment fn fs_solid(in: TerrainOut) -> @location(0) vec4<f32> {
return solid_color(in);
}
// `fs_solid` for the opaque pass (`opaque_quad`): no fade, no cutout, no discard.
@fragment fn fs_opaque(in: TerrainOut) -> @location(0) vec4<f32> {
let texel = sprite(in.uv, in.layer & 0x7fffu);
var color = texel * in.color;
color = mix(globals.fog_color * vec4(1.0, 1.0, 1.0, color.a), color, in.visibility);
return solid_finish(in, texel, color);
}
// The water mirror (gfx.rs's reflection pass): what lies below the water plane
// (`fog_ends.w`, camera-relative) would show above it in the mirror, so it is cut away.
@fragment fn fs_solid_mirror(in: TerrainOut) -> @location(0) vec4<f32> {
@@ -360,6 +430,10 @@ fn solid_color(in: TerrainOut) -> vec4<f32> {
// terrain.fsh: the fade-in mixes toward the fog colour.
color = mix(globals.fog_color * vec4(1.0, 1.0, 1.0, color.a), color, in.visibility);
if color.a < 0.5 && (in.layer & 0x8000u) == 0u { discard; }
return solid_finish(in, texel, color);
}
// The rest of a solid pixel's colour once it is kept: emission and fog.
fn solid_finish(in: TerrainOut, texel: vec4<f32>, color: vec4<f32>) -> vec4<f32> {
// Below 0.75, alpha tells HDR output (hdr.wgsl) a light source's pixel may
// shine, and at which tier (mesher::hdr_tier): 0.525 at the lowest, 0 at the
// brightest. Only pixels as bright as their sprite's threshold may: its
+1 -1
View File
@@ -478,7 +478,7 @@ pub fn blocks_inside(from: Vec3, to: Vec3) -> Vec<BlockPos> {
inside
}
/// `Projectile.lerpRotation`.
fn lerp_rotation(mut old: f32, new: f32) -> f32 {
pub fn lerp_rotation(mut old: f32, new: f32) -> f32 {
while new - old < -180.0 {
old -= 360.0;
}
@@ -445,6 +445,7 @@ pub const COMMANDS: &[Cmd] = &[
["horizon-ctl mcp"], &[], mcp: false, local: true),
cmd!("bench", Scripting, "bench SECONDS", "reset frame stats, wait, reply with the summary", "", ["horizon-ctl bench 10"], &[p("seconds", Ty::Num, "how long to measure")], mcp: true, local: false),
cmd!("stats", Scripting, "stats [reset]", "frame-time summary since the last reset", "", ["horizon-ctl stats"], &[opt("reset", Ty::Bool, "start counting again")], mcp: true, local: false),
cmd!("gpu-passes", Scripting, "gpu-passes [on|off]", "time Horizon's own render passes on the GPU, shown in F3 and in stats and bench", "", ["horizon-ctl gpu-passes on"], &[opt("mode", Ty::Enum(&["on", "off"]), "on or off; without it, only reports")], mcp: false, local: false),
cmd!("fly", Scripting, "fly X1 Y1 Z1 X2 Y2 Z2 SECONDS [PITCH [raw]]", "fly the camera along a line, then reply with the frame summary",
"The reply's tick_ms and tick_ms_max are the world's smoothed tick time over the flight (the server thread's when single player runs on one). With raw, the reply also lists every frame time in milliseconds (frame_ms).",
["horizon-ctl fly 0 100 0 500 100 0 20"], &[], mcp: false, local: false),
@@ -6,7 +6,19 @@ use crate::{Session, commands};
use serde_json::{Value, json};
use std::io::{BufRead, Write};
const PROTOCOL: &str = "2025-06-18";
/// Revisions this server implements. Older ones are not claimed: only the revision actually
/// implemented is advertised (no JSON-RPC batches, no other capability than `tools`).
const SUPPORTED: &[&str] = &["2025-06-18"];
/// MCP lifecycle: answer with the client's version if supported, otherwise one we support.
fn negotiate_version(params: &Value) -> &'static str {
let wanted = params.get("protocolVersion").and_then(Value::as_str);
SUPPORTED
.iter()
.copied()
.find(|v| Some(*v) == wanted)
.unwrap_or(SUPPORTED[0])
}
fn tools() -> Vec<Value> {
commands::COMMANDS
@@ -134,7 +146,7 @@ impl Session {
fn handle(&mut self, method: &str, params: &Value) -> Result<Value, (i64, String)> {
match method {
"initialize" => Ok(json!({
"protocolVersion": params.get("protocolVersion").and_then(Value::as_str).unwrap_or(PROTOCOL),
"protocolVersion": negotiate_version(params),
"capabilities": { "tools": {} },
"serverInfo": { "name": "horizon-ctl", "version": horizon_core::release::VERSION },
"instructions": "Play Horizon, a Rust reimplementation of Minecraft Java Edition. Start with launch (it finds the game installed by the Horizon launcher and opens or creates a world), then read the world with state, inventory, screen, entities and find_block, and act with goto, look_at_block, break, place, interact, click_slot and click_button. Actions block until they finish and report what happened. Coordinates are block coordinates; yaw 0 faces south (+Z).",
@@ -194,7 +206,17 @@ pub fn serve(session: &mut Session) -> Result<(), String> {
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn version_negotiation() {
let v = |p: Value| negotiate_version(&p);
assert_eq!(v(json!({ "protocolVersion": "2025-06-18" })), "2025-06-18");
assert_eq!(v(json!({ "protocolVersion": "2026-07-28" })), "2025-06-18");
assert_eq!(v(json!({ "protocolVersion": "1999-01-01" })), "2025-06-18");
assert_eq!(v(json!({ "protocolVersion": 7 })), "2025-06-18");
assert_eq!(v(json!({})), "2025-06-18");
}
#[test]
fn base64_matches_rfc_4648() {
@@ -234,9 +256,9 @@ mod tests {
fn protocol_basics() {
let mut s = Session::new(std::env::temp_dir().join("horizon-ctl-mcp-test.sock"), None);
let init = s
.handle("initialize", &json!({ "protocolVersion": "2025-03-26" }))
.handle("initialize", &json!({ "protocolVersion": "2025-06-18" }))
.unwrap();
assert_eq!(init["protocolVersion"], "2025-03-26");
assert_eq!(init["protocolVersion"], "2025-06-18");
assert!(s.handle("nope", &json!({})).is_err());
let r = s
.handle(
+48 -1
View File
@@ -1356,10 +1356,11 @@ impl Recipes {
None => sources.push((name, bytes)),
}
}
let filters = horizon_items::recipe_filters::RecipeFilters::load(root, overlay);
for (name, bytes) in sources {
// Vanilla's RecipeManager logs a file that does not parse and
// goes on ("Couldn't parse data file").
let v: Value = match serde_json::from_slice(&bytes) {
let mut v: Value = match serde_json::from_slice(&bytes) {
Ok(v) => v,
Err(e) => {
log::warn!("Horizon: recipe {name} not loaded: {e}");
@@ -1373,6 +1374,14 @@ impl Recipes {
}) {
continue;
}
// The data's recipe filters, over the final set.
let full_id = match name.contains(':') {
true => name.clone(),
false => format!("minecraft:{name}"),
};
if !filters.apply(&full_id, &mut v) {
continue;
}
if let Some(kind) = custom_type(&v) {
let id = if name.contains(':') {
name
@@ -2163,6 +2172,44 @@ mod tests {
assert!(ids.iter().any(|r| r.id == "minecraft:pack_yes"));
let _ = fs::remove_dir_all(&root);
}
/// A pack's recipe filter removes a recipe and swaps a mod recipe's input
/// in the loaded tables, after the packs are merged.
#[test]
fn recipe_filters_remove_and_swap() {
let root = std::env::temp_dir().join(format!("horizon-filters-{}", std::process::id()));
let _ = fs::remove_dir_all(&root);
fs::create_dir_all(root.join("data/minecraft/recipe")).unwrap();
let crusher = |input: &str| {
format!(
r#"{{"type":"mymod:crusher","input":"{input}","result":{{"id":"mymod:dust"}}}}"#
)
.into_bytes()
};
fs::write(
root.join("data/minecraft/recipe/iron.json"),
crusher("minecraft:iron_ingot"),
)
.unwrap();
fs::write(
root.join("data/minecraft/recipe/gold.json"),
crusher("minecraft:gold_ingot"),
)
.unwrap();
let filter = br##"{"filters":[
{"id":"minecraft:gold","remove":true},
{"type":"mymod:*","replace_input":{"from":"minecraft:iron_ingot","to":"#c:ingots/iron"}}
]}"##;
let packs = [("recipe_filter/pack:f".to_owned(), vec![filter.to_vec()])];
let items = Items::for_tests(HashMap::new());
let recipes = Recipes::load(&root, &items, &PackOverlay { files: &packs }).unwrap();
let crushed = &recipes.custom["mymod:crusher"];
assert_eq!(
crushed.iter().map(|r| r.id.as_str()).collect::<Vec<_>>(),
["minecraft:iron"]
);
assert_eq!(crushed[0].json["input"], "#c:ingots/iron");
let _ = fs::remove_dir_all(&root);
}
#[test]
fn cook_finds_the_first_recipe_the_scan_finds() {
let recipes = cooking_book();
+6 -4
View File
@@ -86,15 +86,17 @@ impl Displayer {
/// Horizon's own lines: the shader pack's GPU time, then its costliest passes (`passes`: each
/// pass's median milliseconds, in the order they run).
pub fn shader_pass_lines(passes: &[(String, f32)]) -> Vec<String> {
labelled_pass_lines("Shader pack GPU", passes)
}
/// `label` and the passes' total, then the costliest passes by GPU time.
pub fn labelled_pass_lines(label: &str, passes: &[(String, f32)]) -> Vec<String> {
const SHOWN: usize = 8;
if passes.is_empty() {
return Vec::new();
}
let total: f32 = passes.iter().map(|p| p.1).sum();
let mut lines = vec![format!(
"Shader pack GPU: {total:.2} ms in {} passes",
passes.len()
)];
let mut lines = vec![format!("{label}: {total:.2} ms in {} passes", passes.len())];
let mut costliest: Vec<&(String, f32)> = passes.iter().collect();
costliest.sort_by(|a, b| b.1.total_cmp(&a.1));
lines.extend(
+137 -7
View File
@@ -154,6 +154,7 @@ mod lan_camera;
mod lan_chat;
mod lan_crafting;
mod lan_entities;
mod lan_far;
mod lan_mobs;
mod lan_mount_menu;
mod lan_particles;
@@ -187,6 +188,7 @@ mod moving_pistons;
mod music;
mod names;
mod narrator;
mod native_biome_colors;
mod native_block_entity_draw;
mod native_components;
mod native_criteria;
@@ -196,6 +198,7 @@ mod native_falling;
mod native_hooks;
mod native_lifecycle;
mod native_menus;
mod native_mob_draw;
mod native_mobs;
mod native_motion;
mod native_net;
@@ -765,6 +768,9 @@ enum Screen {
Win,
/// DebugOptionsScreen, F3+F6 (game/debug_options.rs).
DebugOptions,
/// A screen of a native mod's own (`native_mod_screen_open`): it draws and
/// takes its input through the screen hooks.
Mod,
}
impl Screen {
/// The id native mods see for this screen (`NativeScreenView.id`, the
@@ -791,6 +797,7 @@ impl Screen {
Screen::DialogWarning => "dialog_warning",
Screen::Win => "win",
Screen::DebugOptions => "debug_options",
Screen::Mod => "mod",
}
}
}
@@ -872,6 +879,13 @@ struct GameLevel<'w> {
dropped: &'w [ItemEntity],
orbs: &'w [ExperienceOrb],
projectiles: &'w [horizon_client::projectiles::Projectile],
/// The tridents, firework rockets and fishing hooks here. None ignores
/// block triggers (`Entity.isIgnoringBlockTriggers`), so each counts on a
/// plate or tripwire (`getEntitiesOfClass`). The flag says whether it also
/// reaches `entityInside` through this list (`applyEffectsFromBlocks`):
/// not a rocket boosting a player or a hook on an entity, whose ticks
/// skip that step, nor a trident, which `tick_blocks` sweeps itself.
trigger_projectiles: Vec<(Aabb, bool)>,
biomes: &'w Biomes,
seed: i64,
behaviours: &'w Behaviours,
@@ -1549,6 +1563,7 @@ impl LevelAccess for GameLevel<'_> {
};
let others = || {
self.other_trigger_boxes()
.chain(self.trigger_projectiles.iter().map(|&(bounds, _)| bounds))
.filter(|b| area.intersects(*b))
.count()
};
@@ -2219,6 +2234,8 @@ struct Game<'a> {
/// Minecraft.pause: a single-player screen that pauses the game is open.
pause: bool,
debug: bool,
/// `horizon-ctl gpu-passes`: time Horizon's own passes on the GPU with F3 shut.
gpu_passes: bool,
screen: Screen,
window: Option<Arc<Window>>,
renderer: Option<Renderer>,
@@ -2235,6 +2252,8 @@ struct Game<'a> {
/// 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<Vec<horizon_client::gfx::pack_draw::EntitySpan>>,
/// Which entity this frame's entity vertices belong to (`Frame::entity_motion`).
entity_motion: std::cell::RefCell<Vec<(u32, u64)>>,
/// The glowing entities' silhouettes this frame (game/glowing.rs).
glowing_vertices: std::cell::RefCell<Vec<Vertex>>,
/// The shader pack's entity and block entity ids, while one draws entities.
@@ -2623,6 +2642,8 @@ struct Game<'a> {
native_lifecycle: native_lifecycle::NativeLifecycle,
/// Native mods' messages waiting (`game/native_net.rs`).
native_net: native_net::NativeNet,
/// The mod whose own screen is open (`Screen::Mod`).
native_mod_screen: Option<String>,
/// Meshed sections waiting for an upload budget (Horizon settings).
pending_uploads: Vec<((i32, i32, i32), horizon_client::mesher::SectionMesh)>,
/// This frame's view frustum, for culling entities and block entities.
@@ -2815,6 +2836,8 @@ struct Game<'a> {
border: horizon_client::worldborder::WorldBorder,
/// `WorldBorderRenderer` and its render state.
border_renderer: horizon_client::border_render::BorderRenderer,
/// The camera's 16-block section when the wall was last invalidated.
border_section: Option<[i32; 3]>,
border_state: horizon_client::border_render::BorderRenderState,
/// `Hud.tabList`: the player list's header, footer and heart states.
tab_list: horizon_client::tab_list::TabList,
@@ -3437,6 +3460,9 @@ impl Game<'_> {
}
/// ClientLevel.addDestroyBlockEffect: a burst per outline box.
fn destroy_particles(&mut self, pos: [i32; 3], state: BlockStateId) {
if self.settings.extras.hide_block_particles {
return;
}
let Some((sprite, tint)) = self.particle_sprite(state, pos) else {
return;
};
@@ -3448,6 +3474,9 @@ impl Game<'_> {
}
/// ClientLevel.addBreakingParticles: one crumb on the face being mined.
fn crack_particle(&mut self, pos: [i32; 3], face: [i32; 3], state: BlockStateId) {
if self.settings.extras.hide_block_particles {
return;
}
let Some((sprite, tint)) = self.particle_sprite(state, pos) else {
return;
};
@@ -3902,6 +3931,18 @@ impl Game<'_> {
.map(|g| g.position)
}))
.collect();
let mut trigger_projectiles: Vec<(Aabb, bool)> = self
.tridents
.iter()
.map(|t| (horizon_client::arrows::arrow_bounds(t.position), false))
.chain(self.rockets.iter().map(|r| (r.bounds(), !r.attached)))
.chain(
self.hook
.iter()
.map(|h| (h.bounds(), h.applies_block_effects())),
)
.collect();
self.lan_hook_triggers(&mut trigger_projectiles);
let blockers = Blockers {
mobs: &self.mobs,
armor_stands: &self.armor_stands,
@@ -3919,6 +3960,7 @@ impl Game<'_> {
dropped: &self.dropped,
orbs: &self.orbs,
projectiles: &self.creatures.projectiles,
trigger_projectiles,
world: &mut self.world,
facts: self.res.facts(),
collision: self.collision,
@@ -4290,7 +4332,16 @@ impl Game<'_> {
}
// The mobs and the other entities (items, orbs, projectiles...) too.
let mut touched: Vec<BlockPos> = Vec::new();
for bounds in level.mob_trigger_boxes().chain(level.other_trigger_boxes()) {
let projectiles = level
.trigger_projectiles
.iter()
.filter(|(_, applies)| *applies)
.map(|&(bounds, _)| bounds);
for bounds in level
.mob_trigger_boxes()
.chain(level.other_trigger_boxes())
.chain(projectiles)
{
let (lo, hi) = (bounds.min_position(), bounds.max_position());
for x in (lo.x + 1e-7).floor() as i32..=(hi.x - 1e-7).floor() as i32 {
for y in (lo.y + 1e-7).floor() as i32..=(hi.y - 1e-7).floor() as i32 {
@@ -4772,7 +4823,7 @@ impl Game<'_> {
}
PlayerEffect::InteractLoot(_) => {}
},
LevelEvent::PrimeTnt { pos, rot } => self.add_primed_tnt(pos, rot, 80),
LevelEvent::PrimeTnt { pos, rot } => self.add_primed_tnt(pos, rot, 80, None, None),
LevelEvent::BlockEvent {
pos,
state,
@@ -8118,6 +8169,7 @@ impl Game<'_> {
Screen::Dead => self.draw_death_screen(&mut batch),
Screen::Stats => self.draw_stats(&mut batch),
Screen::Advancements => self.draw_advancements(&mut batch),
Screen::Mod => {}
Screen::WorldOptions => self.draw_world_options(&mut batch),
Screen::Friends => self.draw_friends_screen(&mut batch),
Screen::ServerLinks => self.draw_server_links(&mut batch),
@@ -8157,11 +8209,12 @@ impl Game<'_> {
/// `native_screen_render_post` (`ScreenEvent.Render.Post`) for the open
/// screen, once a frame; the Win and Options screens call this on their
/// own path. Reads `self.screen`, the one `hud_layers` drew. Not while
/// playing with no screen. Client only, read-only: what a mod draws is
/// its own.
/// playing with no screen, nor while the server pack prompt hides the
/// open screen (`hud_layers` draws `Playing` then). Client only,
/// read-only: what a mod draws is its own.
fn fire_screen_render_post(&self, batch: &mut HudBatch) {
let screen = self.screen;
if screen == Screen::Playing {
if screen == Screen::Playing || self.server_packs.prompt.is_some() {
return;
}
// slot_count mirrors slots() without allocating; keep them in step.
@@ -10111,6 +10164,11 @@ impl Game<'_> {
pub(super) fn camera_orientation(&self) -> glam::Quat {
camera_rotation(self.camera_type, self.yaw, self.pitch)
}
/// The entity whose geometry starts at vertex `start` (0: none), for frame
/// generation to follow its own motion. Each run lasts until the next mark.
pub(super) fn mark_motion(&self, start: usize, id: u64) {
self.entity_motion.borrow_mut().push((start as u32, id));
}
/// ItemEntityRenderer: items bob and spin; blocks render as small cubes.
fn entity_geometry(
&mut self,
@@ -10119,6 +10177,7 @@ impl Game<'_> {
mut out: Vec<Vertex>,
) -> Vec<Vertex> {
self.entity_spans.borrow_mut().clear();
self.entity_motion.borrow_mut().clear();
// LevelExtractor.extract: the transient blocks queued since the last frame.
self.drain_transient_block_queue();
self.prepare_entity_culling(camera);
@@ -10223,6 +10282,7 @@ impl Game<'_> {
// `ItemEntityRenderer`.
self.mark_entity(out.len(), "minecraft:item");
self.dropped_item_geometry(camera, alpha, &mut out);
self.mark_motion(out.len(), 0);
// `VaultRenderer`.
self.mark_entity(out.len(), "");
self.vault_geometry(camera, alpha, &mut out);
@@ -10235,6 +10295,7 @@ impl Game<'_> {
self.ominous_item_spawner_geometry(camera, alpha, &mut out);
// Mobs and their projectiles (a span each).
self.mob_geometry(camera, alpha, &mut out);
self.mark_motion(out.len(), 0);
// In first person only a shader pack's shadow pass draws the player.
self.mark_player(out.len(), !self.own_body_in_view());
let player_start = out.len();
@@ -11394,6 +11455,9 @@ impl Game<'_> {
&self.options.resources,
&pack_files(&self.world_tables),
);
// TagsUpdatedEvent (SERVER_DATA_LOAD): the world's data is in, before
// its chunks and players are.
self.native_server_data_loaded(false);
self.bosses = Default::default();
reset_world_progress(&mut self.adv, &mut self.stats);
self.prefetch_advancement_tree();
@@ -13260,6 +13324,8 @@ impl Game<'_> {
self.renderer.as_ref(),
None,
self.settings.horizon.api,
self.settings.horizon.translucent_batching,
shader_packs::picked_pack_draws_flat(&self.settings),
)
}
fn apply_settings(&mut self) {
@@ -14341,6 +14407,8 @@ impl Game<'_> {
});
}
fn render(&mut self) -> Result<bool> {
// Before any text of this frame: a full glyph region starts over.
self.gui.atlas.font.begin_frame();
self.glint_frame = self.capture_glint_frame();
if self.renderer.as_mut().is_some_and(|r| r.display_changed()) {
self.apply_ui_settings();
@@ -14734,7 +14802,9 @@ impl Game<'_> {
self.bolt_instances(camera)
};
let mut additive = Vec::new();
if !title && self.dim == Dim::End {
// The dragon is drawn in every dimension, as any entity: a dragon
// summoned outside the End (the server's AddEntity) is shown too.
if !title {
let mut dragon = Vec::new();
self.dragon_geometry(camera, alpha, &mut dragon, &mut additive);
self.dragon_fireball_geometry(camera, alpha, &mut dragon);
@@ -14802,6 +14872,7 @@ impl Game<'_> {
// map_patches any earlier leaves them a frame behind.
hud.patches.append(&mut self.map_patches);
let entity_spans = std::mem::take(self.entity_spans.get_mut());
let entity_motion = std::mem::take(self.entity_motion.get_mut());
let glowing = std::mem::take(self.glowing_vertices.get_mut());
let shadow_only = std::mem::take(self.shadow_only_vertices.get_mut());
let frame = Frame {
@@ -14885,6 +14956,7 @@ impl Game<'_> {
color: sky.cloud_color,
}),
entities: &entities,
entity_motion: &entity_motion,
entity_spans: &entity_spans,
glowing: &glowing,
dynamic_lights: &self.dynamic_lights,
@@ -14955,7 +15027,7 @@ impl Game<'_> {
};
let result = match self.renderer.as_mut() {
Some(renderer) => {
renderer.set_pass_timing(self.debug);
renderer.set_pass_timing(self.debug || self.gpu_passes);
horizon_client::spans::time("renderer", || renderer.render(&frame))
}
None => Err("renderer".into()),
@@ -14963,6 +15035,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.entity_motion.get_mut() = entity_motion;
*self.glowing_vertices.get_mut() = glowing;
*self.shadow_only_vertices.get_mut() = shadow_only;
self.see_through_vertices = see_through;
@@ -15090,6 +15163,13 @@ impl Game<'_> {
distance,
&mut self.border_state,
);
// `LevelRenderer.repositionCamera`: the wall is rebuilt when the camera
// enters another `SectionPos` (it only holds the stretch in range).
let section = camera.map(|c| (c.floor() as i32) >> 4);
if self.border_section != Some(section) {
self.border_renderer.invalidate();
self.border_section = Some(section);
}
// Camera.depthFar, as the renderer's far plane.
let depth_far =
horizon_client::camera::depth_far(self.terrain_distance(), self.settings.cloud_range);
@@ -15169,6 +15249,7 @@ impl Game<'_> {
Screen::Chat => self.close_chat(chat::ChatExit::Intentional),
Screen::Win => self.close_win_screen()?,
Screen::DebugOptions => self.close_debug_options(),
Screen::Mod => self.close_mod_screen(),
},
KeyCode::Digit1
| KeyCode::Digit2
@@ -15452,6 +15533,16 @@ impl Game<'_> {
if let Err(e) = self.poll_remote() {
log::warn!("Horizon: {e}");
}
// The renderer laid translucent quads out again (a shader pack's flat terrain came
// or went) and dropped every section and far tile: meshed again, as for a new one.
if self
.renderer
.as_mut()
.is_some_and(Renderer::take_dropped_sections)
{
self.clear_lod();
self.all_sections_changed();
}
self.prepare_lod();
if self.lod.is_some() {
for &((x, _, z), _, _) in &self.world.light_edits {
@@ -15770,6 +15861,30 @@ impl Game<'_> {
self.physical_buttons.remove(button);
}
}
let mouse = if !self.pad_synthetic_input()
&& let WindowEvent::MouseInput { state, button, .. } = &event
{
Some((
glfw_button(*button),
*state == ElementState::Pressed,
glfw_modifiers(self.modifiers),
))
} else {
None
};
if let Some((button, pressed, modifiers)) = mouse {
let mut canceled = false;
self.native_mouse_button_pre(button, pressed, modifiers, &mut canceled);
if canceled {
return;
}
}
self.handle_window_event_inner(event_loop, event);
if let Some((button, pressed, modifiers)) = mouse {
self.native_mouse_button_post(button, pressed, modifiers);
}
}
fn handle_window_event_inner(&mut self, event_loop: &dyn EventLoopCtl, mut event: WindowEvent) {
if let WindowEvent::Focused(false) = &event {
self.physical_keys.clear();
self.physical_buttons.clear();
@@ -17295,6 +17410,7 @@ impl<'a> Game<'a> {
was_playing: false,
pause: false,
debug: false,
gpu_passes: false,
screen: Screen::Playing,
window: None,
renderer: None,
@@ -17304,6 +17420,7 @@ impl<'a> Game<'a> {
see_through_vertices: Vec::new(),
shadow_only_vertices: Default::default(),
entity_spans: Default::default(),
entity_motion: Default::default(),
glowing_vertices: Default::default(),
pack_ids: None,
health_hud: hud_layout::HealthHud::default(),
@@ -17548,6 +17665,7 @@ impl<'a> Game<'a> {
native_shaders: Default::default(),
native_lifecycle: Default::default(),
native_net: Default::default(),
native_mod_screen: None,
sky_darken_memo: Default::default(),
water_plane_memo: Default::default(),
water_plane_scan: Default::default(),
@@ -17635,6 +17753,7 @@ impl<'a> Game<'a> {
brushing: Default::default(),
border: Default::default(),
border_renderer: Default::default(),
border_section: None,
border_state: Default::default(),
tab_list: Default::default(),
subtitles: Default::default(),
@@ -18028,6 +18147,7 @@ impl Game<'_> {
&overlay,
));
self.adv.reset_tree();
self.native_server_data_loaded(true);
}
fn join_world_rest(&mut self) -> Result<()> {
@@ -18697,6 +18817,8 @@ fn build_renderer(
previous: Option<&Renderer>,
prepared: Option<horizon_client::gfx::PreparedTextures>,
api: horizon_client::graphics::GraphicsApi,
translucent_batching: bool,
shader_pack: bool,
) -> Result<Renderer> {
let assets = &pack.render;
let atlas = &gui.atlas;
@@ -18731,6 +18853,8 @@ fn build_renderer(
prepared,
previous,
api,
translucent_batching,
shader_pack,
))?;
renderer.hud.sprite_animations = atlas
.sprite_animations
@@ -18792,6 +18916,9 @@ fn loading_renderer(
None,
previous,
api,
// It draws no terrain.
false,
false,
))?;
if let Some(atlas) = atlas {
renderer.hud.sprite_animations = atlas
@@ -18860,6 +18987,7 @@ fn loading_frame(hud: &HudBatch) -> Frame<'_> {
glint_ticks: 0.0,
nether_lighting: false,
entities: &[],
entity_motion: &[],
entity_spans: &[],
glowing: &[],
dynamic_lights: &[],
@@ -19333,6 +19461,8 @@ impl Loading {
self.renderer.as_ref(),
loaded.renderer_cpu,
self.settings.horizon.api,
self.settings.horizon.translucent_batching,
shader_packs::picked_pack_draws_flat(&self.settings),
)?;
log::info!(
"Horizon: world renderer ready after {} ms",
+23 -6
View File
@@ -4,7 +4,7 @@
use super::{Dim, Game};
use horizon_client::{
advancements::{self as adv, EntityView, Frame, ItemView, Progress, Tree},
hud::HudBatch,
hud::{HudBatch, rgba},
items::ItemStack,
text::{self, Style, Text},
toasts::Slide,
@@ -1827,11 +1827,7 @@ impl Game<'_> {
}
let atlas = &self.gui.atlas;
batch.sprite(atlas, "gui/sprites/toast/advancement", x, y);
let (key, color) = match frame {
Frame::Task => ("advancements.toast.task", 0xFFFF_FF00),
Frame::Goal => ("advancements.toast.goal", 0xFFFF_FF00),
Frame::Challenge => ("advancements.toast.challenge", 0xFFFF_88FF),
};
let (key, color) = toast_label(frame);
// AdvancementToast.extractRenderState: a title of one line sits under
// the frame's label; a longer one is wrapped at 125, the label fading
// out first (to 1.5 s) and the lines fading in after it over 0.3 s.
@@ -1868,6 +1864,15 @@ impl Game<'_> {
self.draw_item(batch, &ItemStack::new(&icon, 1), x + 8.0, y + 8.0);
}
}
/// The toast's frame label and its colour (`AdvancementToast`: -30465, pink,
/// for a challenge, else -256, yellow), packed as `hud::rgba` for the batch.
fn toast_label(frame: Frame) -> (&'static str, u32) {
match frame {
Frame::Task => ("advancements.toast.task", rgba(255, 255, 0, 255)),
Frame::Goal => ("advancements.toast.goal", rgba(255, 255, 0, 255)),
Frame::Challenge => ("advancements.toast.challenge", rgba(255, 136, 255, 255)),
}
}
/// `Advancement.name`: the bracketed title in the frame's colour, with the
/// description on hover; the id when there is no display.
pub(super) fn adv_name(a: &adv::Advancement) -> Text {
@@ -2065,6 +2070,18 @@ pub(super) fn text_of(v: &Value) -> Text {
mod tests {
use super::*;
#[test]
fn toast_labels_are_yellow_or_pink() {
// The batch takes colours as `rgba`: 0xFFFF_FF00 there is cyan (#5850).
for frame in [Frame::Task, Frame::Goal] {
assert_eq!(toast_label(frame).1.to_le_bytes(), [255, 255, 0, 255]);
}
assert_eq!(
toast_label(Frame::Challenge).1.to_le_bytes(),
[255, 136, 255, 255]
);
}
fn recipes(n: usize) -> Toast {
Toast::recipe(
(0..n)
+1
View File
@@ -424,6 +424,7 @@ impl Game<'_> {
Screen::BlockEditor => "block-editor",
Screen::Win => "win",
Screen::DebugOptions => "debug-options",
Screen::Mod => "mod",
}
}
pub(super) fn inventory_counts(&self) -> BTreeMap<String, i64> {
+27 -1
View File
@@ -1119,7 +1119,7 @@ impl Game<'_> {
if let Some((h, _)) = self.fluid_height_of(x, y, z, None)
&& p[1] <= f64::from(y) + f64::from(h)
{
fluid = Some(([p[0], f64::from(y) + f64::from(h), p[2]], t));
fluid = Some((fluid_hit_point(p, y, h), t));
break;
}
t += 0.05;
@@ -1454,10 +1454,36 @@ impl Game<'_> {
}
}
/// Where a fluid pick lands in the cell the ray sample `p` is in. Vanilla's
/// `ClipContext.Fluid.ANY` clips against the fluid shape: a full-height cell
/// (the same fluid above it) is a full cube, so the hit is the ray's own
/// point and a boat placed under water stays at the eye; a partial cell is
/// clipped to its surface.
fn fluid_hit_point(p: [f64; 3], y: i32, h: f32) -> [f64; 3] {
if h >= 1.0 {
p
} else {
[p[0], f64::from(y) + f64::from(h), p[2]]
}
}
#[cfg(test)]
mod tests {
use super::*;
/// `VoxelShape.clip` on a full-cube fluid cell keeps the true hit point
/// (a ray starting inside hits at `from + delta * 0.001`); only a partial
/// cell is clipped to its surface.
#[test]
fn a_full_height_fluid_cell_keeps_the_ray_point_and_a_partial_one_snaps_to_its_surface() {
let eye = [0.5, 3.62, 0.5];
assert_eq!(fluid_hit_point(eye, 3, 1.0), eye);
assert_eq!(
fluid_hit_point(eye, 3, 8.0 / 9.0),
[0.5, 3.0 + f64::from(8.0f32 / 9.0), 0.5]
);
}
fn boat() -> Boat {
Boat::new(1, Arc::from("oak"), Vec3::ZERO, 0.0)
}
+6
View File
@@ -94,6 +94,12 @@ pub(super) struct CombatTracker {
/// known (`AbstractArrow.onHitEntity` makes an ownerless arrow its own
/// cause).
pub(super) projectile_direct: Option<Text>,
/// The player whose primed TNT is going off now (`PrimedTnt.getOwner`):
/// its entity id and display name, the blast's causing entity.
pub(super) blast_owner: Option<(u32, Text)>,
/// The guest behind `blast_owner`, when that player is a LAN guest; the
/// host's own blast leaves it None.
pub(super) blast_guest: Option<u32>,
pub(super) projectile_attacker: Option<(u32, Text)>,
/// The projectile hitting the player now is another player's that
/// `ServerPlayer.canHarmPlayer` refuses (the `pvp` rule, an ally).
+20 -3
View File
@@ -2459,6 +2459,11 @@ impl Game<'_> {
ctx.has("keep"),
);
let mut count = 0;
// Whether the filter takes a state, worked out once per state: a fill visits up to 32768 blocks of a handful
// of states, and matching a tag is a name lookup.
let mut matched: std::collections::HashMap<BlockStateId, bool> =
std::collections::HashMap::new();
let plain = !(destroy || hollow || outline);
for y in min[1]..=max[1] {
for z in min[2]..=max[2] {
for x in min[0]..=max[0] {
@@ -2466,11 +2471,23 @@ impl Game<'_> {
if keep && current != air {
continue;
}
if let Some(f) = &filter
&& !self.block_matches(f, current)
{
// Already the block it would become: nothing to place (place_state would do nothing either).
if plain && current == state {
continue;
}
if let Some(f) = &filter {
let ok = match matched.get(&current) {
Some(&ok) => ok,
None => {
let ok = self.block_matches(f, current);
matched.insert(current, ok);
ok
}
};
if !ok {
continue;
}
}
let edge = x == min[0]
|| x == max[0]
|| y == min[1]
+30 -2
View File
@@ -109,10 +109,28 @@ impl Game<'_> {
let _ = flight.reply.send(summary.to_string());
}
}
/// The frame summary, with Horizon's own passes' median GPU times while they are timed.
fn summary_with_passes(&self) -> Value {
let mut summary = self.frame_stats.summary();
let passes = self
.renderer
.as_ref()
.map(|r| r.own_pass_times())
.unwrap_or_default();
if !passes.is_empty() {
summary["gpu_passes"] = Value::Object(
passes
.into_iter()
.map(|(name, ms)| (name, json!(ms)))
.collect(),
);
}
summary
}
pub(super) fn poll_control(&mut self, event_loop: &dyn EventLoopCtl) {
if let Some((deadline, reply)) = self.pending_bench.take() {
if Instant::now() >= deadline {
let _ = reply.send(self.frame_stats.summary().to_string());
let _ = reply.send(self.summary_with_passes().to_string());
} else {
self.pending_bench = Some((deadline, reply));
}
@@ -312,6 +330,15 @@ impl Game<'_> {
}
Ok(Some(json!({ "ok": true, "cull": r.section_cull() })))
}
"gpu-passes" => {
match words.get(1).copied() {
None => {}
Some("on") => self.gpu_passes = true,
Some("off") => self.gpu_passes = false,
Some(other) => return Err(format!("unknown gpu pass mode {other}").into()),
}
Ok(Some(json!({ "ok": true, "gpu_passes": self.gpu_passes })))
}
"section-sides" => {
let r = self.renderer.as_mut().ok_or("no renderer")?;
match words.get(1).copied() {
@@ -820,7 +847,7 @@ impl Game<'_> {
})))
}
"stats" => {
let summary = self.frame_stats.summary();
let summary = self.summary_with_passes();
if words.get(1) == Some(&"reset") {
self.frame_stats.reset();
}
@@ -1032,6 +1059,7 @@ impl Game<'_> {
Screen::BlockEditor => "block-editor",
Screen::Win => "win",
Screen::DebugOptions => "debug-options",
Screen::Mod => "mod",
}
}
+33 -1
View File
@@ -2382,6 +2382,16 @@ impl Game<'_> {
self.particles.add(&options, at, velocity);
}
}
MobEvent::ParticleColored {
at,
color: [r, g, b],
} => {
let options = horizon_client::particles::ParticleOptions::colored(
horizon_client::particles::ParticleType::EntityEffect,
[r, g, b, 1.0],
);
self.particles.add(&options, at, [0.0; 3]);
}
MobEvent::Experience { at, amount } => self.spawn_breeding_orb(at, amount),
MobEvent::AxolotlSupportPlayer { mob } => {
let previous = self
@@ -3788,7 +3798,7 @@ impl Game<'_> {
}
ArrowEvent::Block { pos, reaction } => match reaction {
BlockReaction::PrimeTnt => {
self.spawn_primed_tnt(pos, false);
self.spawn_primed_tnt(pos, false, None, None);
self.with_level(|b, level, ticks| b.remove_block(level, ticks, pos, false));
let _ = self.process_level_events();
}
@@ -5916,6 +5926,7 @@ impl Game<'_> {
});
for mob in &self.mobs {
let entity_start = out.len();
self.mark_motion(entity_start, (1 << 32) | u64::from(mob.id));
let outline = self.mob_outline(mob);
// Entity.DATA_SHARED_FLAGS_ID invisible: only the name tag draws (and
// the outline of a glowing mob, LivingEntityRenderer.getRenderType).
@@ -5989,6 +6000,25 @@ impl Game<'_> {
let block = horizon_client::mobs::aquatic::glow_squid_block_light(mob, lit);
packed = (packed & 0xFF) | ((block * 16) << 8);
}
// A native mod's own model in place of the vanilla one
// (`native_mob_submit`), before any of the vanilla model's parts
// (a sulfur cube's swallowed block) are drawn.
if !is_player
&& !layers_only
&& self.mod_mob_geometry(mob, alpha as f32, camera, packed, out)
{
if let Some(rgb) = outline {
self.mark_outline(&out[entity_start..], rgb);
if mob.invisible {
out.truncate(entity_start);
continue;
}
}
if outline.is_none() {
self.cull_entity_vertices(out, entity_start);
}
continue;
}
if mob.species() == Species::SulfurCube
&& !horizon_client::mobs::trader::is_invisible(mob)
{
@@ -6090,6 +6120,8 @@ impl Game<'_> {
);
}
}
// What follows isn't a tracked mob: close the last one's span.
self.mark_motion(out.len(), 0);
self.elder_apparition_geometry(alpha, out);
self.spawner_geometry(camera, alpha, out);
// Projectiles: the baked vanilla arrow model, the rest as item billboards.
@@ -162,6 +162,13 @@ impl Game<'_> {
if self.debug && !pass_lines.is_empty() {
d.group("shader pack", pass_lines);
}
// Horizon's own passes, in a group of their own.
let own_lines = self.renderer.as_ref().map_or_else(Vec::new, |r| {
overlay::labelled_pass_lines("Horizon GPU", &r.own_pass_times())
});
if self.debug && !own_lines.is_empty() {
d.group("Horizon passes", own_lines);
}
}
"tps" => self.debug_tps_line(d),
// DebugEntryChunkGeneration, where the world is generated
@@ -877,6 +877,9 @@ impl Game<'_> {
.raw
.get(horizon_client::lan::HORIZON_CLIENTS_ONLY)
.is_some_and(|v| v.trim().eq_ignore_ascii_case("true")),
lod_distance: horizon_client::lod_net::distance_property(
props.raw.get(horizon_client::lod_net::PROPERTY),
),
server_links: if props.bug_report_link.is_empty() {
Vec::new()
} else if super::server_links::valid_untrusted_uri(&props.bug_report_link) {
@@ -6,6 +6,7 @@
use super::{ServerState, log};
use crate::game::Game;
use horizon_client::{
command::vanilla::DIFFICULTIES,
server::{
profiles,
user_lists::{BanEntry, NameAndId, OpEntry, WhiteEntry},
@@ -48,6 +49,27 @@ impl Game<'_> {
let entries = body["entries"].as_array().cloned().unwrap_or_default();
let lists = state.lists.clone();
match body["method"].as_str().unwrap_or_default() {
"serversettings/difficulty" => {
return json!({ "value": DIFFICULTIES[self.level.difficulty_id() as usize] });
}
"serversettings/difficulty/set" => {
let Some(value) = body["value"].as_str() else {
return json!({ "error": "difficulty must be a string" });
};
let Some(index) = DIFFICULTIES.iter().position(|name| *name == value) else {
return json!({ "error": format!("unknown difficulty {value:?}") });
};
rpc(format!(
"Update difficulty from '{}' to '{}'",
DIFFICULTIES[self.level.difficulty_id() as usize],
value
));
// DedicatedServer.setDifficulty stores the property and then forces the world
// difficulty, including hardcore's always-hard rule and the player broadcast.
self.store_property("difficulty", value.to_owned());
self.set_difficulty_forced(if self.level.hardcore { 3 } else { index as u8 });
return json!({ "value": DIFFICULTIES[self.level.difficulty_id() as usize] });
}
"allowlist/add" => {
for user in entries.iter().filter_map(|e| self.management_user(e)) {
rpc(format!("Add player '{}' to allowlist", shown(&user)));
+2 -2
View File
@@ -1377,7 +1377,7 @@ impl Game<'_> {
);
} else if &*name == "minecraft:tnt" {
if self.ui.rules.bool("tnt_explodes") {
self.spawn_primed_tnt(target_pos, false);
self.spawn_primed_tnt(target_pos, false, None, None);
self.with_level(|b, level, ticks| b.remove_block(level, ticks, target_pos, false));
self.process_level_events()?;
} else {
@@ -1441,7 +1441,7 @@ impl Game<'_> {
// new PrimedTnt(level, x + 0.5, y, z + 0.5, null), its sound and ENTITY_PLACE.
let target = Self::pos_of(front);
let rot = self.world.random.next_double() * f64::from(std::f32::consts::TAU);
self.add_primed_tnt(target, rot, 80);
self.add_primed_tnt(target, rot, 80, None, None);
self.audio.play(
"minecraft:entity.tnt.primed",
Some([
+53 -2
View File
@@ -438,8 +438,10 @@ impl Dragon {
fn sample(&self, delay: usize) -> (f64, f32) {
self.history[(self.head + 64 - (delay & 63)) & 63]
}
/// DragonFlightHistory.get(delay, partial).
/// `DragonFlightHistory.get(delay, partial)`, with
/// `EnderDragonRenderer.extractRenderState`'s frozen history on death.
fn sample_lerp(&self, delay: usize, partial: f32) -> (f64, f32) {
let partial = if self.alive() { partial } else { 0.0 };
let a = self.sample(delay);
let b = self.sample(delay + 1);
let yaw = b.1 + wrap(a.1 - b.1) * partial;
@@ -3874,7 +3876,7 @@ fn dragon_rays(base: glam::Mat4, t: f32, white: horizon_client::hud::Rect, out:
normal: [0; 4],
pos: m.transform_point3(p).to_array(),
uv,
layer: 2,
layer: horizon_client::gfx::DRAGON_RAYS_OVERLAY,
color: c,
light: 0,
@@ -3911,6 +3913,55 @@ fn push_seen_with_hurt(
mod tests {
use super::*;
#[test]
fn dying_dragons_keep_their_flight_pose_between_frames() {
let mut dragon = Dragon::new(Vec3::new(0.0, 80.0, 0.0), 0.0, 432);
dragon.history.fill((70.0, 170.0));
dragon.position.y = 90.0;
dragon.yaw = -170.0;
dragon.record();
assert_eq!(dragon.sample_lerp(0, 0.5), (80.0, 180.0));
dragon.health = 0.0;
for partial in [0.0, 0.25, 0.5, 0.75, 1.0] {
assert_eq!(dragon.sample_lerp(0, partial), (70.0, 170.0));
}
}
/// Driven by `tools/test_dragon_render_parity.py`; compares the actual
/// ray vertices with EnderDragonRenderer's captured VertexConsumer output.
#[test]
#[ignore]
fn dragon_render_parity_fixture() {
let path = std::env::var("HORIZON_DRAGON_RENDER_OUTPUT").expect("harness output path");
let mut cases = Vec::new();
for t in [0.0, 0.05, 0.25, 0.5, 0.8, 0.9, 0.95, 1.0] {
let mut vertices = Vec::new();
dragon_rays(
glam::Mat4::IDENTITY,
t,
horizon_client::hud::Rect {
u0: 0.0,
v0: 0.0,
u1: 1.0,
v1: 1.0,
w: 1,
h: 1,
},
&mut vertices,
);
// The fourth quad vertex only pads a triangle for our index buffer.
let triangles: Vec<_> = vertices
.as_chunks::<4>()
.0
.iter()
.flat_map(|q| &q[..3])
.map(|v| serde_json::json!([v.pos[0], v.pos[1], v.pos[2], v.color]))
.collect();
cases.push(triangles);
}
std::fs::write(path, serde_json::to_vec(&cases).unwrap()).unwrap();
}
#[test]
fn death_rays_sample_inside_the_white_sprite() {
// A four-texel white sprite surrounded by transparent atlas pixels.
+25 -8
View File
@@ -4,7 +4,7 @@
//! merged drops and TNT chain priming, and optional fire.
use super::Game;
use horizon_client::{
Result, blocks::Fluid, collisions::SceneCollisions, items::ItemStack, text::Text,
Result, blocks::Fluid, collisions::SceneCollisions, items::ItemStack, mobs::Who, text::Text,
};
use horizon_core::{
geometry::{Vec3, VoxelShape},
@@ -175,12 +175,17 @@ pub(super) fn client_float() -> f32 {
impl Game<'_> {
/// `blast_cause` for the blast going off now.
pub(super) fn blast_attacker(&self) -> Option<(u32, Text)> {
blast_cause(
self.combat.explosion_owner,
self.combat.explosion_source,
|id| self.mob_display_name(id),
&self.combat.exploded,
)
// `Explosion.getIndirectSourceEntity`: a primed TNT's owner names the
// blast before any mob does, which makes the damage type
// `minecraft:player_explosion` (`server_explode` asks this).
self.combat.blast_owner.clone().or_else(|| {
blast_cause(
self.combat.explosion_owner,
self.combat.explosion_source,
|id| self.mob_display_name(id),
&self.combat.exploded,
)
})
}
/// `ServerExplosion.getSeenPercent` for an entity box.
pub(super) fn seen_percent(&self, center: [f64; 3], min: [f64; 3], max: [f64; 3]) -> f32 {
@@ -976,7 +981,19 @@ impl Game<'_> {
let r = &mut self.world.random;
let rot = r.next_double() * f64::from(std::f32::consts::TAU);
let fuse = r.next_int_bound(20).unwrap_or(0) + 10;
self.add_primed_tnt(BlockPos::new(pos[0], pos[1], pos[2]), rot, fuse);
// TntBlock.wasExploded hands the new TNT the blast's indirect
// source, so a chain reaction keeps the player who started it.
let (owner, guest) = match &self.combat.blast_owner {
Some(_) => (Some(Who::Player), self.combat.blast_guest),
None => (None, None),
};
self.add_primed_tnt(
BlockPos::new(pos[0], pos[1], pos[2]),
rot,
fuse,
owner,
guest,
);
}
Ok(())
}
+19 -2
View File
@@ -123,6 +123,18 @@ impl Rocket {
)
}
}
/// `EntityType.FIREWORK_ROCKET`: 0.25 x 0.25.
pub(super) fn bounds(&self) -> Aabb {
let h = 0.125;
Aabb::new(
Vec3::new(self.position.x - h, self.position.y, self.position.z - h),
Vec3::new(
self.position.x + h,
self.position.y + 2.0 * h,
self.position.z + h,
),
)
}
/// `FireworkRocketEntity.tick` for a rocket attached to no entity: speed
/// up (an angled one keeps its motion) and rise, then move.
fn free_flight(&mut self, scene: &mut SceneCollisions) {
@@ -479,8 +491,13 @@ impl Game<'_> {
}
/// The owner as `rocket_targets` names it: a guest by their entity id.
fn rocket_owner(&self, r: &Rocket) -> Option<Who> {
let owner = r.owner?;
let Some(conn) = r.guest.filter(|_| owner == Who::Player) else {
self.shooter_who(r.owner, r.guest)
}
/// `Projectile.getOwner` as a target's `Who`: a guest's `Who::Player`
/// (kept with the connection that shot it) is their entity, not the host.
pub(super) fn shooter_who(&self, owner: Option<Who>, guest: Option<u32>) -> Option<Who> {
let owner = owner?;
let Some(conn) = guest.filter(|_| owner == Who::Player) else {
return Some(owner);
};
let host = self.lan.as_ref()?;
+10
View File
@@ -64,6 +64,16 @@ fn bounds(p: Vec3) -> Aabb {
Vec3::new(p.x + h, p.y + SIZE, p.z + h),
)
}
impl Hook {
pub(super) fn bounds(&self) -> Aabb {
bounds(self.position)
}
/// `FishingHook.tick` returns before `applyEffectsFromBlocks` while it
/// holds an entity.
pub(super) fn applies_block_effects(&self) -> bool {
!matches!(self.state, State::Hooked(_))
}
}
/// RandomSource.triangle(mode, deviation).
fn triangle(r: &mut impl RandomSource, mode: f64, deviation: f64) -> f64 {
mode + deviation * (f64::from(r.next_float()) - f64::from(r.next_float()))
+8 -1
View File
@@ -124,7 +124,14 @@ impl Game<'_> {
self.camera_fluid(camera),
false,
);
self.zoomed_fov_degrees(fov)
let vanilla = self.zoomed_fov_degrees(fov);
let mut degrees = vanilla;
self.native_compute_fov(partial, camera, &mut degrees);
if degrees.is_finite() && (1.0..=179.0).contains(&degrees) {
degrees
} else {
vanilla
}
}
/// `Camera.calculateHudFov`: the first-person hand's field of view.
pub(super) fn hud_fov_degrees(&self, partial: f32, camera: [f64; 3]) -> f32 {
+4 -1
View File
@@ -928,11 +928,14 @@ impl Game<'_> {
}
}
}
/// `ReloadCommand.reloadPacks`: no message of its own.
/// `ReloadCommand.reloadPacks`: no message of its own. It reloads the
/// server's data as `/reload` does (`reload_datapacks`), the content
/// tables with the functions.
fn apply_pack_change(&mut self) {
self.save_datapack_settings();
self.rebuild_store();
self.fns.load_pending = true;
self.reload_world_tables();
}
// ---- predicates -------------------------------------------------------------------
@@ -421,6 +421,7 @@ impl Game<'_> {
});
for (item, at, partial, entity) in live.chain(taken) {
let entity_start = out.len();
self.mark_motion(entity_start, (2 << 32) | u64::from(item.id));
let q = item.position;
let pos = glam::DVec3::from_array(at) - camera;
if pos.length_squared() > 64.0 * 64.0 {
+7
View File
@@ -187,6 +187,9 @@ pub(super) struct LanHost {
pub commands: Vec<super::lan_actions::GuestCommand>,
/// Mods' quiet commands waiting to run a few a tick (game/lan_quiet.rs).
pub quiet: std::collections::VecDeque<super::lan_quiet::QuietBatch>,
/// Far terrain sampled for Horizon clients (game/lan_far.rs); None when
/// `horizon-lod-distance` is 0.
pub far: Option<horizon_client::lod_net::Service>,
/// Native mods' commands and the level each needs, once asked
/// (game/native_server.rs).
pub native_commands: Option<HashMap<String, u8>>,
@@ -1395,6 +1398,8 @@ impl Game<'_> {
Game::native_block_entity_update_tag(pos, entity, &mut tag);
tag
});
let far = (net.lod_distance > 0)
.then(|| horizon_client::lod_net::Service::new(net.lod_distance, biome_names.clone()));
let lan = Lan::start_with(sync.clone(), net)?;
if let Some(uuid) = host {
lan.reserve(uuid);
@@ -1429,6 +1434,7 @@ impl Game<'_> {
border_sent: None,
commands: Vec::new(),
quiet: std::collections::VecDeque::new(),
far,
native_commands: None,
sleep_ignored: Default::default(),
states: Default::default(),
@@ -2351,6 +2357,7 @@ impl Game<'_> {
host.hub.anticheat_ping();
drop(holding);
self.flush_native_net(&mut host);
self.flush_far_terrain(&mut host);
lan::flush_all(host.hub.guests.iter().map(|g| &g.sender));
self.lan = Some(host);
self.flush_native_sounds();
@@ -2096,6 +2096,9 @@ impl Game<'_> {
S::CustomPayload(p) if p.channel == horizon_protocol::mod_net::CHANNEL => {
self.guest_native_net(host, conn, &p.data)
}
S::CustomPayload(p) if p.channel == horizon_client::lod_net::CHANNEL => {
self.guest_far_terrain(host, conn, &p.data)
}
S::ChatCommandSigned(p) => self.guest_signed_command(host, conn, p),
S::ChangeDifficulty(p) if host.hub.guests[gi].permission >= 2 => {
if !self.level.difficulty_locked && (0..=3).contains(&p.difficulty) {
@@ -128,7 +128,11 @@ impl Game<'_> {
e.velocity = [b.velocity.x, b.velocity.y, b.velocity.z];
e.meta = vehicle_meta(b.hurt_time, b.hurt_dir, b.damage);
// AbstractBoat.DATA_ID_PADDLE_LEFT and DATA_ID_PADDLE_RIGHT.
// Omitted false values must reset clients that saw a true value,
// including when the controlling passenger dismounts mid-stroke.
for (k, paddling) in b.paddling.iter().enumerate() {
e.defaults
.push((11 + k as u8, serializer::BOOLEAN, Value::Bool(false)));
if *paddling {
e.meta
.push((11 + k as u8, serializer::BOOLEAN, Value::Bool(true)));
+79
View File
@@ -0,0 +1,79 @@
//! Far terrain for a server's Horizon clients (`horizon_client::lod_net`): a
//! `horizon:lod` request is checked against the player's limits and position,
//! then sampled from the level the player is in on the far terrain thread.
//! The replies come back here and leave with the tick's other packets. A
//! refused request is answered at once, so the client knows to ask again.
use super::{Dim, Game, lan::LanHost};
use horizon_client::{
lan::conn::PlayerSender,
lod_net::{CHANNEL, Delivery, Job, Verdict},
};
use horizon_protocol::{
packets::{configuration::CustomPayload, play},
text::translatable,
};
use std::sync::atomic::Ordering;
/// Bytes a player's connection may have waiting before far terrain holds its
/// replies back: a client that reads slowly gets the game's packets first.
const BACKLOG: usize = 1 << 20;
fn send(sender: &PlayerSender, data: Vec<u8>) {
let packet = play::Clientbound::CustomPayload(CustomPayload {
channel: CHANNEL.into(),
data,
});
sender.send(packet.encode());
}
impl Game<'_> {
/// A `horizon:lod` payload from `conn`.
pub(super) fn guest_far_terrain(&mut self, host: &mut LanHost, conn: u32, data: &[u8]) {
let Some(far) = host.far.as_mut() else {
return;
};
// Only the clients that heard the offer (lan::conn) may ask.
let Some(guest) = host.hub.guest(conn).filter(|g| g.brand == "horizon") else {
return;
};
// Players who left are forgotten once they could matter.
if far.players() > host.hub.guests.len() + 8 {
let guests = &host.hub.guests;
far.retain(|c| guests.iter().any(|g| g.conn == c));
}
let dim = Dim::from_id(&guest.dimension);
let source = self.lod_source_in(dim);
let now = std::time::Instant::now();
match far.request(conn, data, guest.position, source.is_some(), now) {
Verdict::Serve(request) => far.push(Job {
conn,
level: dim.id().to_owned(),
source: source.expect("served when open"),
request,
}),
Verdict::Refuse(reply) => send(&guest.sender, reply),
Verdict::Ignore => {}
Verdict::Kick => host.hub.kick(
conn,
translatable("multiplayer.disconnect.invalid_packet", &[]),
),
}
}
/// The tiles the far terrain thread finished go to their players, once
/// their connections have room.
pub(super) fn flush_far_terrain(&mut self, host: &mut LanHost) {
let Some(far) = host.far.as_mut() else {
return;
};
let guests = &host.hub.guests;
far.deliver(|conn, data| match guests.iter().find(|g| g.conn == conn) {
None => Delivery::Gone,
Some(g) if g.sender.pending.load(Ordering::Relaxed) > BACKLOG => Delivery::Busy,
Some(g) => {
send(&g.sender, std::mem::take(data));
Delivery::Sent
}
});
}
}
@@ -226,6 +226,20 @@ impl Game<'_> {
})
.collect()
}
/// The same guests' cast fishing hooks, for `GameLevel::trigger_projectiles`.
pub(super) fn lan_hook_triggers(&self, out: &mut Vec<(Aabb, bool)>) {
let Some(host) = self.lan.as_ref() else {
return;
};
out.extend(
host.hub
.guests
.iter()
.filter(|g| host.hub.here(g) && g.loaded && !g.dead && !g.spectator())
.filter_map(|g| host.play.get(&g.conn)?.hook.as_ref())
.map(|hook| (hook.bounds(), hook.applies_block_effects())),
);
}
/// The same guests' feet and whether they stand on the ground, for the
/// blocks that also ask that (`BigDripleafBlock.canEntityTilt`).
pub(super) fn lan_guest_feet(&self) -> Vec<([f64; 3], bool)> {
+1 -1
View File
@@ -297,7 +297,7 @@ impl Game<'_> {
result.unwrap_or(Ok(()))
}
fn player_attacker(&self, host: &LanHost, conn: Option<u32>) -> Option<(u32, Text)> {
pub(super) fn player_attacker(&self, host: &LanHost, conn: Option<u32>) -> Option<(u32, Text)> {
match conn {
Some(conn) => {
let g = host.hub.guest(conn)?;
+2
View File
@@ -164,6 +164,8 @@ impl Game<'_> {
player_fall_flying,
ridden,
};
self.audio
.set_listener_reserve(self.settings.extras.ui_sound_reserve);
self.audio.tick(self.pause, &ctx);
}
/// `Minecraft.setLevel` -> `updateLevelInEngines`: `SoundManager.stop()`.
@@ -0,0 +1,39 @@
//! Biome colours a client mod changes (`native_biome_colors`), for seasons:
//! the grass, foliage, dry foliage and water tints the mesher blends, as a
//! resource pack's colormaps and biome effects set them, changed while the
//! game runs.
use super::Game;
use horizon_client::streaming::WorkerContext;
use std::sync::Arc;
// Native mods call it; nothing in Horizon does.
#[allow(dead_code)]
impl Game<'_> {
/// Asks `native_biome_colors` again for every biome and, when the colours
/// differ from those in use, rebuilds every loaded section and the far
/// terrain with them (`LevelRenderer.allChanged`, as changing Biome Blend
/// does). Call it when your colours change (a new season, a new day of a
/// slow fade) and once when the player joins, not every frame: a rebuild
/// takes the workers a few seconds. API @API:biome_colors@.
pub(super) fn native_refresh_biome_colors(&mut self) {
let base = self.res.biomes().clone();
let biomes = base
.with_colors(|biome, colors| self.native_biome_colors(biome, colors))
.map_or(base, Arc::new);
if Arc::ptr_eq(&biomes, &self.pack.context.biomes) {
return;
}
let old = &self.pack.context;
let context = Arc::new(WorkerContext {
assets: old.assets.clone(),
render: old.render.clone(),
tables: old.tables.clone(),
biomes,
light: old.light.clone(),
});
self.pack.context = context.clone();
self.clear_lod();
self.streaming.reload_resources(&mut self.world, context);
}
}
@@ -63,6 +63,33 @@ pub struct BlockEntityDraw {
#[allow(dead_code)]
impl BlockEntityDraw {
/// A frame drawn from `pose` (relative to the camera), lit by `light` and
/// `lights`, adding to `vertices`; `block` is where `native_draw_item`'s
/// owner stands. A mob's frame (`native_mob_submit`) is made this way.
pub(super) fn posed(
partial: f32,
game_time: i64,
light: u32,
block: [i32; 3],
pose: Mat4,
lights: (Vec3, Vec3),
vertices: Vec<Vertex>,
) -> Self {
Self {
partial,
game_time,
light,
block,
poses: vec![pose],
lights,
vertices,
kept: false,
}
}
/// The vertices drawn, with those it was made with first.
pub(super) fn into_vertices(self) -> Vec<Vertex> {
self.vertices
}
/// `PoseStack.pushPose`: `pop` comes back to the pose as it is now.
pub fn push(&mut self) {
let pose = self.pose();
+238 -3
View File
@@ -277,11 +277,13 @@ use super::{
Game, GameLevel, LevelDimension, Screen,
execution_role::ExecutionRole,
native_block_entity_draw::BlockEntityDraw,
native_mob_draw::NativeMobRender,
native_panel_recipes::RecipePage,
native_server::{NativeCommand, NativeServerPlayer},
};
use horizon_client::{
atlas_sources::qualified,
biome::BiomeColors,
block_entities::Pos,
chat::ChatSource,
containers::{BlockEntity, MOD_MENUS, ModBlockEntity, ModContainer, ModFluidTanks},
@@ -1183,6 +1185,19 @@ impl Game<'_> {
) {
let _ = (context, phase, error_text);
}
/// The server's data tables are built (NeoForge's `TagsUpdatedEvent`
/// with `SERVER_DATA_LOAD`): the recipes, tags, loot and advancements of
/// the world's data packs, and the files mods added through
/// `native_data_pack_files`, are what the helpers read from now on.
/// `reload` is false on a world's first open, before its chunks and
/// players are loaded, and true after `/reload` rebuilt them, once the
/// players were sent the new data. The place to
/// drop caches derived from the data and to rebind what depends on it.
/// Server-side (the single player's integrated server included); a client
/// connected to a server never fires it. API @API:server_data_loaded@.
pub(super) fn native_server_data_loaded(&mut self, reload: bool) {
let _ = reload;
}
/// The server is assembling its data tables from the world's data packs
/// (`ReloadableServerResources`): a world is opening, or `/reload` was
/// run, and the recipes, tags, loot and advancements are built after
@@ -1308,6 +1323,21 @@ impl Game<'_> {
pub(super) fn native_key(&mut self, key: &str, pressed: bool) {
let _ = (key, pressed);
}
/// Raw mouse button input on any screen, before dispatch; cancel skips dispatch and Post.
/// Button/modifiers use the same GLFW encoding as screen hooks. Synthetic clicks skip it.
pub(super) fn native_mouse_button_pre(
&mut self,
button: i32,
pressed: bool,
modifiers: u32,
canceled: &mut bool,
) {
let _ = (button, pressed, modifiers, canceled);
}
/// An accepted raw button event finished dispatch, including a screen handler's early return.
pub(super) fn native_mouse_button_post(&mut self, button: i32, pressed: bool, modifiers: u32) {
let _ = (button, pressed, modifiers);
}
/// Wheel steps while playing: set `claimed` to keep them from the hotbar.
pub(super) fn native_wheel(&mut self, steps: f32, claimed: &mut bool) {
let _ = (steps, claimed);
@@ -1325,11 +1355,28 @@ impl Game<'_> {
pub(super) fn native_commands(&mut self, commands: &mut Vec<&'static str>) {
let _ = commands;
}
/// World field of view, after normal calculation and zoom and before projection/Nausea.
/// Called for world rendering and near-plane overlays; hand/HUD FOV uses its own path.
/// Only finite values in 1..=179 degrees replace the computed FOV.
pub(super) fn native_compute_fov(&self, partial: f32, camera: [f64; 3], degrees: &mut f32) {
let _ = (partial, camera, degrees);
}
/// The zoom a native mod asks for, applied while playing (the spyglass's
/// own view wins): set `zoom` when it is none yet.
pub(super) fn native_zoom(&self, zoom: &mut Option<NativeZoom>) {
let _ = zoom;
}
/// Computed world fog RGB, after ordinary effects and before upload. Title panoramas skip it.
/// Only finite RGB components in 0..=1 are accepted; alpha and distances stay unchanged.
pub(super) fn native_compute_fog_color(
&self,
partial: f32,
camera: [f64; 3],
fog_type: horizon_client::fog::FogType,
rgb: &mut [f32; 3],
) {
let _ = (partial, camera, fog_type, rgb);
}
/// The camera shake native mods ask for in this frame, `partial` ticks
/// past the last tick (0 to 1): add your pitch, yaw and roll in degrees
/// and your offset in blocks, along the camera's right, up and back, to
@@ -1355,6 +1402,18 @@ impl Game<'_> {
let _ = (hand, stack, partial, swing, equip, canceled);
}
/// Final scaled third-person camera distance, before visual-shape collision clipping.
/// `entity` includes its living scale/distance and optional mount scale/distance.
/// Only finite requests in 0..=1024 blocks replace the vanilla distance.
pub(super) fn native_camera_distance(
&self,
partial: f32,
mirrored: bool,
entity: &horizon_client::camera::CameraEntity,
distance: &mut f32,
) {
let _ = (partial, mirrored, entity, distance);
}
/// The values native mods hand their shader packs in this frame,
/// `partial` ticks past the last tick (0 to 1): a pack's WGSL reads
/// them as `frame.mods` (eight `vec4<f32>`). Write the slots your own
@@ -1362,6 +1421,17 @@ impl Game<'_> {
/// one that pack is for. Asked once a frame while a pack is on; a value
/// that isn't finite reads as 0 (`native_shader_params_now`). API
/// @API:native_shader_params@.
/// Change the colours of `biome` (`minecraft:plains`) on this client: the
/// grass, foliage (leaves, vines), dry foliage (leaf litter) and water
/// tints, 0xRRGGBB, as its biome file and the colormaps set them. Asked
/// for every biome when a mod calls `native_refresh_biome_colors`, which
/// rebuilds the terrain with them; a season mod answers from its season
/// and calls that when the season turns. The dark forest's modifier
/// still applies to the grass set here; the swamp's noise colours
/// replace it. API @API:biome_colors@.
pub(super) fn native_biome_colors(&self, biome: &str, colors: &mut BiomeColors) {
let _ = (biome, colors);
}
pub(super) fn native_shader_params(&self, partial: f32, params: &mut [[f32; 4]; 8]) {
let _ = (partial, params);
}
@@ -1535,6 +1605,29 @@ impl Game<'_> {
) {
let _ = (pos, block, state, entity, bounds);
}
/// Draw the mob `mob` (its id, kind, custom name and the frame's body
/// and head turns and walk cycle, `LivingEntityRenderState`) on this
/// client with a model of your own, once a frame while it is in view
/// (vanilla's `EntityRenderer.submit`), and set `drawn` to leave out its
/// vanilla model, armour and held items (NeoForge's `RenderLivingEvent.Pre`,
/// cancelled). Add geometry with `native_draw_cube`, `native_draw_quad`,
/// `native_draw_block` and `native_draw_item` under `draw`'s pose stack,
/// which starts at the mob's feet turned with its body: +z is the way it
/// faces, +x its left, +y up, in blocks. `draw` also has the partial tick,
/// the game time and the mob's light. Its name tag, shadow, flames and
/// glowing outline draw as for any mob. Asked for every mob in view but
/// players; a mob your server half spawned (`native_spawn_mob`) is told
/// apart by its id, sent with mod messages, or by its custom name.
/// A mob is drawn afresh each frame: `draw.keep()` does nothing here.
/// API @API:mob_submit@.
pub(super) fn native_mob_submit(
&self,
mob: &NativeMobRender<'_>,
draw: &mut BlockEntityDraw,
drawn: &mut bool,
) {
let _ = (mob, draw, drawn);
}
/// A menu opens on this client for block `block`, a mod's: set `screen`
/// when the block is yours, to lay its screen out (`native_screen_draw`
/// draws it). The server asks it too: a slot it hides is not in the menu
@@ -1569,8 +1662,10 @@ impl Game<'_> {
/// It does not fire on the title screen, which returns before its point,
/// nor for the screens drawn over gameplay after it (a server dialog, the
/// server pack, link and code-of-conduct prompts, a book, a sign editor):
/// those keep `Screen::Playing`, which it skips, and draw over what it
/// drew, while vanilla's `ScreenEvent.Render.Post` fires for them.
/// those keep `Screen::Playing`, which it skips (the server pack prompt
/// hides an open screen too, and that screen does not fire under it),
/// and draw over what it drew, while vanilla's `ScreenEvent.Render.Post`
/// fires for them.
/// API 78.
pub(super) fn native_screen_render_post(
&self,
@@ -3297,6 +3392,94 @@ impl Game<'_> {
batch.scale_since(start, at[0], at[1], size / 16.0, size / 16.0);
true
}
/// Item `item`'s icon drawn with model `model` (`minecraft:paper` as
/// `mymod:wheel_icon`, an `item_model` of a content mod's), as `native_item`
/// draws one; false for an unknown item. API @API:native_mod_screen@.
pub(super) fn native_item_model(
&self,
batch: &mut HudBatch,
item: &str,
model: &str,
at: [f32; 2],
size: f32,
) -> bool {
if self.res.items().get(item).is_none() {
return false;
}
let mut stack = ItemStack::new(item, 1);
let mut data = horizon_client::item_data::StackData::default();
data.set_other(
"minecraft:item_model",
Some(horizon_world::nbt::Tag::String(model.into())),
);
stack.extra = Some(Box::new(data));
let start = batch.vertices.len();
self.draw_item_model(batch, &stack, at[0], at[1]);
batch.scale_since(start, at[0], at[1], size / 16.0, size / 16.0);
true
}
/// A quad of corners `points` (in order round it) in GUI pixels, `argb`:
/// a wedge, a slanted bar, what `native_fill`'s rectangles can't draw.
/// API @API:native_mod_screen@.
pub(super) fn native_quad(&self, batch: &mut HudBatch, points: [[f32; 2]; 4], argb: u32) {
let [a, r, g, b] = argb.to_be_bytes();
batch.fill_quad(&self.gui.atlas, points, rgba(r, g, b, a));
}
/// Opens a screen of mod `mod_id`'s own over the world: the screen hooks
/// fire over it as over any other, so the mod draws it in
/// `native_screen_render_post` and takes its clicks, scroll, keys and
/// typing there; the mouse is freed, and Escape closes it. Every mod's own
/// screen has the `NativeScreenView.id` `mod`, so a mod's screen hooks
/// check `native_mod_screen()` is its own before drawing. It doesn't pause
/// a single-player game (as the inventory doesn't). True when it is open:
/// false while another screen is (or another mod's), over the title
/// screen, a book, a sign editor, a dialog or a prompt, while the world
/// loads, and anywhere but the window's own client (the server thread, a
/// dedicated server). A gamepad can close it (B, Start) but not click on
/// it: its clicks don't reach the mouse hooks. API @API:native_mod_screen@.
pub(super) fn native_mod_screen_open(&mut self, mod_id: &str) -> bool {
// What `screen_hooks_open` would refuse over the world: the mod's
// screen would be up with no hook to draw or click it.
let blocked = self.title.is_some()
|| self.renderer.is_none()
|| self.lan_acting.is_some()
|| self.player.is_none()
|| self.screen_over_playing()
|| self.remote_loading();
let owner = self.native_mod_screen();
if let Some(open) = mod_screen_opens(self.screen, blocked, owner, mod_id) {
return open;
}
self.native_mod_screen = Some(mod_id.to_owned());
self.screen = Screen::Mod;
self.keys.clear();
self.left = false;
self.right = false;
self.mining = None;
self.capture(false);
true
}
/// Closes mod `mod_id`'s own screen, if it is open. API @API:native_mod_screen@.
pub(super) fn native_mod_screen_close(&mut self, mod_id: &str) {
if self.native_mod_screen().is_some_and(|id| id == mod_id) {
self.close_mod_screen();
}
}
/// The mod whose own screen is open, if one is (Escape, or another screen
/// replacing it, closes it). API @API:native_mod_screen@.
pub(super) fn native_mod_screen(&self) -> Option<&str> {
self.native_mod_screen
.as_deref()
.filter(|_| self.screen == Screen::Mod)
}
/// A mod's own screen closes (Escape, or `native_mod_screen_close`).
pub(super) fn close_mod_screen(&mut self) {
self.native_mod_screen = None;
if self.screen == Screen::Mod {
self.screen = Screen::Playing;
self.capture(true);
}
}
/// The open menu's title (the block's name, or the one it was given),
/// as plain text. API 20.
pub(super) fn native_screen_title(&self) -> Option<String> {
@@ -4508,9 +4691,57 @@ fn box_meets(b: &horizon_core::geometry::Aabb, min: [f64; 3], max: [f64; 3]) ->
&& hi.z > min[2]
}
/// What `native_mod_screen_open(mod_id)` does over `screen` (`blocked`: an
/// overlay, the title screen, no window or no player; `owner`'s own screen
/// open): `None` opens it, `Some` answers without changing anything (true:
/// `mod_id`'s is already open).
fn mod_screen_opens(
screen: Screen,
blocked: bool,
owner: Option<&str>,
mod_id: &str,
) -> Option<bool> {
if screen == Screen::Mod {
return Some(owner == Some(mod_id));
}
if screen != Screen::Playing || blocked {
return Some(false);
}
None
}
#[cfg(test)]
mod tests {
/// A mod's own screen opens only over the world, never over another
/// screen, an overlay (the title screen, a book, loading, no window) or
/// another mod's; opening it again is a no-op.
#[test]
fn a_mod_screen_opens_only_over_the_world() {
use super::{Screen, mod_screen_opens};
assert_eq!(mod_screen_opens(Screen::Playing, false, None, "a"), None);
assert_eq!(
mod_screen_opens(Screen::Playing, true, None, "a"),
Some(false)
);
for other in [
Screen::Paused,
Screen::Inventory,
Screen::Chat,
Screen::Options,
] {
assert_eq!(mod_screen_opens(other, false, None, "a"), Some(false));
}
assert_eq!(
mod_screen_opens(Screen::Mod, false, Some("a"), "a"),
Some(true)
);
assert_eq!(
mod_screen_opens(Screen::Mod, false, Some("b"), "a"),
Some(false)
);
}
/// The characters a `ScreenEvent.CharacterTyped.Pre` sees are the
/// printable ones of a text: a cancelled one leaves that text and a
/// control character (a key, not text) is never offered a hook.
@@ -4765,7 +4996,11 @@ mod tests {
// From its doc comment's last line, so an attribute above it counts.
let hook = source.find("pub fn native_loot_table_load(").unwrap();
let start = source[..hook].rfind("///").unwrap();
let body = &source[start..start + source[start..].find("\n}\n").unwrap()];
let end = source[start..]
.find("\n}\n")
.or_else(|| source[start..].find("\r\n}\r\n"))
.unwrap();
let body = &source[start..start + end];
for line in body.lines() {
assert!(
!line.contains("cfg"),
@@ -0,0 +1,155 @@
//! Mobs drawn by native mods on the client: `native_mob_submit` (vanilla's
//! `EntityRenderer.submit` for a mob, NeoForge's `RenderLivingEvent.Pre` when
//! it cancels) gets the mob's render state and a [`BlockEntityDraw`] posed at
//! its feet and turned with its body, so a mod draws a model of its own with
//! the helpers block entities draw with (`native_draw_cube`,
//! `native_draw_quad`, `native_draw_block`, `native_draw_item`).
use super::{Dim, Game, native_block_entity_draw::BlockEntityDraw};
use glam::{DVec3, Mat4};
use horizon_client::{
block_entity_render::diffuse_lights,
mesher::Vertex,
mobs::{Mob, anim, living::wrap},
};
/// One mob's frame, as `EntityRenderer.extractRenderState` fills a
/// `LivingEntityRenderState`: what a model reads to pose and animate itself.
/// Angles are in degrees, interpolated to the frame's partial tick.
// Native mods read it; nothing in Horizon does.
#[allow(dead_code)]
#[derive(Clone, Debug)]
pub struct NativeMobRender<'a> {
/// The mob's network id, as the mob helpers and mod messages name it.
pub id: u32,
/// Its entity type (`minecraft:villager`).
pub kind: &'a str,
/// Its `CustomName` as plain text, when it has one.
pub custom_name: Option<&'a str>,
/// `isBaby`.
pub baby: bool,
/// The frame's partial tick, 0 to 1.
pub partial: f32,
/// `ageInTicks`: ticks lived plus the partial tick, for idle motion.
pub age: f32,
/// `bodyRot`: the way the body faces (0 is south, 90 west).
pub body_yaw: f32,
/// `yRot`: the head's turn from the body, -180 to 180.
pub head_yaw: f32,
/// `xRot`: the head's pitch, positive looking down.
pub pitch: f32,
/// `walkAnimationPos`: how far the legs have swung, for a walk cycle
/// (a vanilla quadruped's legs swing `cos(pos * 0.6662) * 1.4 * speed` radians).
pub walk_position: f32,
/// `walkAnimationSpeed`: how fast it walks, 0 standing to about 1.
pub walk_speed: f32,
/// Ticks left of the red hurt flash (10 when hit).
pub hurt_time: i32,
/// Ticks since it died, 0 while alive (vanilla tips the body over 20).
pub death_time: i32,
pub on_ground: bool,
}
impl<'a> NativeMobRender<'a> {
/// The render state of `m` at `partial` (`LivingEntityRenderer.extractRenderState`).
pub(super) fn of(m: &'a Mob, partial: f32) -> Self {
let body = m.previous_body_yaw + wrap(m.body_yaw - m.previous_body_yaw) * partial;
// The walk cycle, head turn and age as the vanilla models read them:
// no walking while riding or dead, a baby's legs three times as far
// (`WalkAnimationState.position(partial)`, `positionScale`).
let pose = anim::state(m, partial);
Self {
id: m.id,
kind: &m.kind.id,
custom_name: m.custom_name.as_deref(),
baby: m.is_baby(),
partial,
age: pose.age,
body_yaw: body,
head_yaw: pose.y_rot,
pitch: pose.x_rot,
walk_position: pose.pos,
walk_speed: pose.speed,
hurt_time: m.hurt_time,
death_time: m.death_time,
on_ground: m.on_ground,
}
}
}
/// The pose a mod's mob model starts from: at the feet, `at` (relative to the
/// camera), turned with the body so +z is the way it faces, +x its left and +y
/// up, in blocks.
pub(super) fn mob_pose(at: DVec3, body_yaw: f32) -> Mat4 {
Mat4::from_translation(at.as_vec3()) * Mat4::from_rotation_y((-body_yaw).to_radians())
}
impl Game<'_> {
/// `native_mob_submit` for `mob`, lit by `light` (packed as the entity
/// pass packs it): true when a mod drew it, and its vanilla model, armour
/// and held items are left out. The name tag, shadow, fire and a glowing
/// outline still draw as for any mob.
pub(super) fn mod_mob_geometry(
&self,
mob: &Mob,
partial: f32,
camera: DVec3,
light: u32,
out: &mut Vec<Vertex>,
) -> bool {
let render = NativeMobRender::of(mob, partial);
let a = f64::from(partial);
let feet = mob.previous.lerp(mob.position, a);
let feet = DVec3::new(feet.x, feet.y, feet.z);
let start = out.len();
let mut draw = BlockEntityDraw::posed(
partial,
self.world.game_time,
light,
[feet.x, feet.y, feet.z].map(|c| c.floor() as i32),
mob_pose(feet - camera, render.body_yaw),
diffuse_lights(self.dim == Dim::Nether),
std::mem::take(out),
);
let mut drawn = false;
self.native_mob_submit(&render, &mut draw, &mut drawn);
*out = draw.into_vertices();
if !drawn {
out.truncate(start);
}
drawn
}
}
#[cfg(test)]
mod tests {
use super::*;
use glam::Vec3;
fn close(a: Vec3, b: Vec3) -> bool {
(a - b).length() < 1e-5
}
#[test]
fn the_pose_faces_where_the_body_faces() {
// Entity yaw 0 faces south (+z), 90 west (-x), 180 north, 270 east.
for (yaw, ahead, left) in [
(0.0, Vec3::Z, Vec3::X),
(90.0, Vec3::NEG_X, Vec3::Z),
(180.0, Vec3::NEG_Z, Vec3::NEG_X),
(270.0, Vec3::X, Vec3::NEG_Z),
] {
let pose = mob_pose(DVec3::ZERO, yaw);
assert!(close(pose.transform_vector3(Vec3::Z), ahead), "{yaw}");
assert!(close(pose.transform_vector3(Vec3::X), left), "{yaw}");
assert!(close(pose.transform_vector3(Vec3::Y), Vec3::Y), "{yaw}");
}
}
#[test]
fn the_pose_starts_at_the_feet() {
let at = DVec3::new(3.0, -1.5, 0.25);
let pose = mob_pose(at, 37.0);
assert!(close(pose.transform_point3(Vec3::ZERO), at.as_vec3()));
}
}
+2
View File
@@ -299,6 +299,8 @@ impl Game<'_> {
self.renderer.as_ref(),
Some(textures),
self.settings.horizon.api,
self.settings.horizon.translucent_batching,
super::shader_packs::picked_pack_draws_flat(&self.settings),
)
})
.transpose()
+68 -9
View File
@@ -455,6 +455,14 @@ impl Game<'_> {
Button::South | Button::West => {
self.pad_take_pointer(event_loop);
self.pad_pointer_button(event_loop, button == Button::West, true);
// A on a text field in pointer mode opens the keyboard, as
// in focus mode (not the right click).
if button == Button::South
&& !self.pad_focusing()
&& field_at(&self.pad_targets(), self.cursor)
{
self.pad_open_keyboard();
}
}
// Y in an inventory: a shift-click (quick move).
Button::North if matches!(self.screen, Screen::Inventory | Screen::Creative) => {
@@ -1311,17 +1319,17 @@ impl Game<'_> {
self.pad.menu.osk = Some(Keyboard::default());
}
/// Chat or a sign opened by the pad in focus mode: the keyboard opens
/// with it (once per screen; B closes it for good).
/// Chat or a sign opened by the pad (either menu mode): the keyboard
/// opens with it (once per screen; B closes it for good).
fn pad_auto_keyboard(&mut self) {
let menu = &self.pad.menu;
if menu.osk.is_none()
&& !menu.osk_dismissed
&& self.pad.in_use
&& menu.open_polls >= 3
&& self.pad_focus_mode()
&& (self.screen == Screen::Chat || self.sign_edit.is_some())
{
if osk_auto_opens(
menu.osk.is_some(),
menu.osk_dismissed,
self.pad.in_use,
menu.open_polls,
self.screen == Screen::Chat || self.sign_edit.is_some(),
) {
self.pad.menu.osk_dismissed = true;
self.pad_open_keyboard();
}
@@ -1468,3 +1476,54 @@ impl Game<'_> {
key_focus::draw_outline(&self.gui.atlas, batch, focus);
}
}
/// Whether the keyboard opens by itself over chat or a sign: the pad is in
/// use and the screen has been open for a few polls, and it is neither
/// open nor dismissed already. Independent of the menu mode.
fn osk_auto_opens(
osk_open: bool,
dismissed: bool,
pad_in_use: bool,
polls: u32,
typing_screen: bool,
) -> bool {
!osk_open && !dismissed && pad_in_use && polls >= 3 && typing_screen
}
/// Whether a text field in `targets` contains the point `at` (GUI pixels).
fn field_at(targets: &[Target], at: (f32, f32)) -> bool {
targets.iter().any(|t| {
let [x, y, w, h] = t.rect;
t.kind == Kind::Field && at.0 >= x && at.0 < x + w && at.1 >= y && at.1 < y + h
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn keyboard_auto_opens_for_pad_on_typing_screen() {
assert!(osk_auto_opens(false, false, true, 3, true));
assert!(!osk_auto_opens(false, false, true, 2, true));
assert!(!osk_auto_opens(false, false, false, 5, true));
assert!(!osk_auto_opens(false, true, true, 5, true));
assert!(!osk_auto_opens(true, false, true, 5, true));
assert!(!osk_auto_opens(false, false, true, 5, false));
}
#[test]
fn field_hit_test() {
let t = |rect, kind| Target { rect, kind };
let targets = [
t([0.0, 0.0, 50.0, 20.0], Kind::Button),
t([10.0, 30.0, 100.0, 20.0], Kind::Field),
];
assert!(field_at(&targets, (10.0, 30.0)));
assert!(field_at(&targets, (109.0, 49.0)));
assert!(!field_at(&targets, (110.0, 40.0)));
assert!(!field_at(&targets, (50.0, 29.0)));
assert!(!field_at(&targets, (5.0, 5.0)), "a button is not a field");
assert!(!field_at(&[], (5.0, 5.0)));
}
}
+89 -8
View File
@@ -79,6 +79,20 @@ impl PaintingVariants {
fn overlaps(a: ([f64; 3], [f64; 3]), b: ([f64; 3], [f64; 3])) -> bool {
(0..3).all(|i| a.0[i] < b.1[i] && b.0[i] < a.1[i])
}
/// `HangingEntityItem.mayPlace` for a painting: not a floor or ceiling.
/// Direction.get2DDataValue: south 0, west 1, north 2, east 3.
fn painting_facing(face: [i32; 3]) -> Option<u8> {
if face[1] != 0 {
return None;
}
Some(match face {
[0, 0, 1] => 0,
[-1, 0, 0] => 1,
[0, 0, -1] => 2,
_ => 3,
})
}
impl Game<'_> {
/// `HangingEntity.survives`: no block collision in the box, solid blocks
/// (or diodes) behind it and no other painting overlapping it.
@@ -117,16 +131,58 @@ impl Game<'_> {
.iter()
.any(|o| o.id != p.id && overlaps(o.bounds(), (lo, hi)))
}
/// `HangingEntityItem.useOn` for a painting against `hit`'s face.
pub(super) fn place_painting(&mut self, hit: &Hit, stack: &ItemStack) -> bool {
if &*stack.item != "minecraft:painting" || hit.face[1] != 0 {
/// `HangingEntityItem.useOn` on a client level: SUCCESS once
/// `Painting.create` finds a surviving variant (and the stack's
/// `painting/variant`, if any, also survives). The random pick and
/// spawn are the server's.
pub(super) fn painting_fits(&self, hit: &Hit, stack: &ItemStack) -> bool {
if &*stack.item != "minecraft:painting" {
return false;
}
let facing = match hit.face {
[0, 0, 1] => 0,
[-1, 0, 0] => 1,
[0, 0, -1] => 2,
_ => 3,
let Some(facing) = painting_facing(hit.face) else {
return false;
};
let pos = [
hit.pos[0] + hit.face[0],
hit.pos[1],
hit.pos[2] + hit.face[2],
];
let mut candidate = Painting {
id: self.next_entity.wrapping_add(1),
pos,
facing,
variant: Arc::from(""),
width: 1,
height: 1,
check_interval: 0,
};
let mut any = false;
for id in &self.painting_variants.placeable {
let Some(v) = self.painting_variants.get(id) else {
continue;
};
(candidate.width, candidate.height) = (v.width, v.height);
if self.painting_survives(&candidate) {
any = true;
break;
}
}
if !any {
return false;
}
if let Some(v) = self.painting_variants.of_stack(stack).cloned() {
(candidate.variant, candidate.width, candidate.height) = (v.id, v.width, v.height);
return self.painting_survives(&candidate);
}
true
}
/// `HangingEntityItem.useOn` for a painting against `hit`'s face.
pub(super) fn place_painting(&mut self, hit: &Hit, stack: &ItemStack) -> bool {
if &*stack.item != "minecraft:painting" {
return false;
}
let Some(facing) = painting_facing(hit.face) else {
return false;
};
let pos = [
hit.pos[0] + hit.face[0],
@@ -187,6 +243,8 @@ impl Game<'_> {
if !self.creative {
self.consume_held();
}
// HangingEntityItem.useOn returns SUCCESS: the hand swings.
self.swing_arm();
true
}
/// The painting under the crosshair within reach and its distance.
@@ -408,3 +466,26 @@ impl Game<'_> {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// `HangingEntityItem.useOn` returns SUCCESS (the hand swings) when a
/// painting is created on a wall. Floor, ceiling, or a failed create is
/// CONSUME: no swing.
fn painting_use_on_swings(face: [i32; 3], created: bool) -> bool {
painting_facing(face).is_some() && created
}
#[test]
fn placing_a_painting_on_a_wall_swings() {
assert!(painting_use_on_swings([0, 0, 1], true));
assert!(painting_use_on_swings([-1, 0, 0], true));
assert!(painting_use_on_swings([0, 0, -1], true));
assert!(painting_use_on_swings([1, 0, 0], true));
assert!(!painting_use_on_swings([0, 1, 0], true));
assert!(!painting_use_on_swings([0, -1, 0], true));
assert!(!painting_use_on_swings([0, 0, 1], false));
}
}
@@ -40,6 +40,12 @@ impl Game<'_> {
}
let boats: std::collections::HashSet<u32> = self.boats.iter().map(|b| b.id).collect();
let carts: std::collections::HashSet<u32> = self.carts.iter().map(|c| c.id).collect();
let tnts: std::collections::HashSet<u32> = self
.tnts
.iter()
.map(|t| t.id)
.filter(|&id| id != 0)
.collect();
let index: HashMap<u32, usize> = self
.mobs
.iter()
@@ -57,6 +63,7 @@ impl Game<'_> {
&& !self.mobs[i].removed
&& (boats.contains(&v)
|| carts.contains(&v)
|| tnts.contains(&v)
|| index
.get(&v)
.is_some_and(|&j| self.mobs[j].alive() && !self.mobs[j].removed));
@@ -79,6 +86,7 @@ impl Game<'_> {
pub(super) fn position_riders(&mut self) {
self.seat_boat_mobs();
self.seat_cart_mobs();
self.seat_tnt_mobs();
let index: HashMap<u32, usize> = self
.mobs
.iter()
+11 -1
View File
@@ -432,20 +432,30 @@ impl Game<'_> {
}
}
// PrimedTnt.readAdditionalSaveData: its fuse (80 when absent) and motion.
// Its Passengers ride it (EntityType.loadEntityRecursive forces them on).
"minecraft:tnt" => {
let mut tag = tag;
let riders = self.take_mob_passengers(&mut tag);
if let Some(position) = entity_nbt::position(&tag) {
self.next_entity += 1;
let id = self.next_entity;
let fuse = match tag.get("fuse") {
Some(Tag::Short(f)) => i32::from(*f),
_ => 80,
};
self.tnts.push(super::tnt::PrimedTnt {
id: 0,
id,
position,
previous: position,
velocity: entity_nbt::motion(&tag),
fuse,
on_ground: matches!(tag.get("OnGround"), Some(Tag::Byte(1))),
owner: None,
guest: None,
});
for rider in riders {
self.load_mob(rider, Some(id));
}
}
}
k if super::boats::any_boat_wood(k)
+22 -12
View File
@@ -275,18 +275,28 @@ impl Game<'_> {
facts: self.res.facts(),
context,
};
view.align_with_entity(&entity, partial, kind, |from, to| {
let hit = clip_world(
&mut rays,
Vec3::new(from[0], from[1], from[2]),
Vec3::new(to[0], to[1], to[2]),
RayBlockMode::Visual,
RayFluidMode::None,
4096,
)
.ok()?;
(!hit.miss).then_some([hit.hit.location.x, hit.hit.location.y, hit.hit.location.z])
});
view.align_with_entity_distance(
&entity,
partial,
kind,
|distance| {
let mut distance = distance;
self.native_camera_distance(partial, kind.is_mirrored(), &entity, &mut distance);
distance
},
|from, to| {
let hit = clip_world(
&mut rays,
Vec3::new(from[0], from[1], from[2]),
Vec3::new(to[0], to[1], to[2]),
RayBlockMode::Visual,
RayFluidMode::None,
4096,
)
.ok()?;
(!hit.miss).then_some([hit.hit.location.x, hit.hit.location.y, hit.hit.location.z])
},
);
self.view = view;
Some((
glam::DVec3::from_array(view.position),
+4 -1
View File
@@ -1,4 +1,4 @@
//! `ItemPickupParticle`: a taken item entity, experience orb or arrow flies
//! `ItemPickupParticle`: a taken item entity, experience orb, arrow or trident flies
//! into whoever took it over three ticks (`ClientPacketListener.handleTakeItemEntity`
//! adds one for every `ClientboundTakeItemEntityPacket`), drawn with the render
//! state it had when it was taken (`extractEntity(from, 1.0F)`).
@@ -16,6 +16,7 @@ pub(super) enum Taken {
Item(Box<ItemEntity>),
Orb(Box<ExperienceOrb>),
Arrow(Box<Projectile>),
Trident(Box<super::trident::ThrownTrident>),
}
impl Taken {
/// The render state's position: the entity's at partial tick 1.
@@ -24,6 +25,7 @@ impl Taken {
Taken::Item(i) => i.position,
Taken::Orb(o) => o.position,
Taken::Arrow(a) => a.position,
Taken::Trident(t) => t.position,
};
[p.x, p.y, p.z]
}
@@ -122,6 +124,7 @@ impl Game<'_> {
Taken::Item(_) => "item",
Taken::Orb(_) => "orb",
Taken::Arrow(_) => "arrow",
Taken::Trident(_) => "trident",
};
*self.pickups.made.entry(kind).or_default() += 1;
self.pickups.added.push(Pickup {
+185 -9
View File
@@ -24,7 +24,7 @@ use horizon_client::{
text::Text,
};
use horizon_core::{
geometry::{Aabb, Direction, Vec3},
geometry::{Aabb, Axis, Direction, Vec3},
random::{LegacyRandom, RandomSource},
};
@@ -42,6 +42,44 @@ pub(super) struct ThrownPotion {
/// `Projectile.getOwner`'s display name: the player who threw it (none
/// for a dispenser's).
owner: Option<Text>,
/// `Projectile.getOwner` as a target: the thrower (`Who::Player`, a
/// guest's with `guest` set), none for a dispenser's or a server's.
thrower: Option<Who>,
/// On a server, the guest (by connection) who threw it.
guest: Option<u32>,
/// `Projectile.leftOwner`: until then it can't hit its thrower.
left_owner: bool,
}
/// The player (not one of `skip`, the thrower and who rides with them before
/// it has left them) whose box, grown by `margin` (`ProjectileUtil.computeMargin`),
/// holds `p` (`Projectile.canHitEntity`).
fn thrown_player_at<'a>(
players: &'a [(Who, Aabb)],
skip: &[Who],
p: Vec3,
margin: f64,
) -> Option<&'a (Who, Aabb)> {
players.iter().find(|(who, b)| {
!skip.contains(who) && b.inflate(Vec3::new(margin, margin, margin)).contains(p)
})
}
/// `ProjectileUtil.computeMargin`: how far the pick box is grown at `age`.
fn thrown_margin(age: u32) -> f64 {
((f64::from(age) - 2.0) / 20.0).clamp(0.0, 0.3)
}
/// `Projectile.isOutsideOwnerCollisionRange` (`checkLeftOwner`): none of the
/// boxes of the thrower's vehicle tree (`owner.getRootVehicle().getSelfAndPassengers()`,
/// here `ridden`) is within a block of the potion's (0.25 wide) grown by its
/// motion; true too when there is no thrower (`ridden` empty).
fn thrown_left_owner(ridden: &[Aabb], position: Vec3, velocity: Vec3) -> bool {
let h = 0.125;
let reach = Aabb::new(
Vec3::new(position.x - h, position.y, position.z - h),
Vec3::new(position.x + h, position.y + 2.0 * h, position.z + h),
)
.expand_towards(velocity)
.inflate(Vec3::new(1.0, 1.0, 1.0));
!ridden.iter().any(|b| b.intersects(reach))
}
/// `AreaEffectCloud` wait time set by `ThrownLingeringPotion.onHitAsPotion`.
pub(super) const LINGERING_WAIT: i32 = 10;
@@ -296,7 +334,10 @@ impl Game<'_> {
position,
velocity,
age: 0,
thrower: owner.is_some().then_some(Who::Player),
guest: self.lan_acting.filter(|_| owner.is_some()),
owner,
left_owner: false,
});
}
}
@@ -378,6 +419,9 @@ impl PotionState {
velocity,
age: 0,
owner: None,
thrower: None,
guest: None,
left_owner: false,
});
}
/// ThrowableItemProjectile.DATA_ITEM_STACK: the potion it shows.
@@ -959,14 +1003,86 @@ impl Game<'_> {
p.status = status;
}
}
/// The players a thrown potion can hit (`Projectile.canHitEntity`): the
/// host and the guests, but not a spectator or a dead one.
fn thrown_player_targets(&self) -> Vec<(Who, Aabb)> {
let mut targets = Vec::new();
if let Some(p) = self.player_view()
&& p.alive
&& !self.spectator()
{
targets.push((Who::Player, p.bounds));
}
targets.extend(self.lan_projectile_targets());
targets
}
/// The thrower's vehicle tree as `Projectile` sees it
/// (`owner.getRootVehicle().getSelfAndPassengers()`): the players in it (the
/// thrower and who shares their vehicle), the mob that is the vehicle, and
/// the boxes of all of them. Until the potion has left it, none can be hit
/// (`canHitEntity`: `isPassengerOfSameVehicle`) and any one near keeps it
/// from leaving (`isOutsideOwnerCollisionRange`).
fn thrown_ridden(
&self,
owner: Option<Who>,
players: &[(Who, Aabb)],
) -> (Vec<Who>, Option<u32>, Vec<Aabb>) {
let Some(owner) = owner else {
return (Vec::new(), None, Vec::new());
};
let vehicle_of = |who: Who| match who {
Who::Player => self.riding,
Who::Mob(id) => self
.lan
.as_ref()
.and_then(|h| h.hub.guests.iter().find(|g| g.entity_id as u32 == id))
.and_then(|g| g.vehicle),
};
let vehicle = vehicle_of(owner);
let mut who = vec![owner];
let mut boxes = Vec::new();
for (w, b) in players {
if *w == owner || (vehicle.is_some() && vehicle_of(*w) == vehicle) {
if *w != owner {
who.push(*w);
}
boxes.push(*b);
}
}
if let Some(v) = vehicle {
if let Some(m) = self.mobs.iter().find(|m| m.id == v) {
boxes.push(m.bounds());
}
if let Some(b) = self.boats.iter().find(|b| b.id == v) {
boxes.push(b.bounds());
}
}
(who, vehicle, boxes)
}
/// `ThrowableProjectile.tick` for the potions in flight: the motion, then
/// the first block or mob the step meets (`ProjectileUtil.getHitResultOnMoveVector`),
/// where the potion splashes (`Projectile.onHit`).
/// the first block, mob or player the step meets
/// (`ProjectileUtil.getHitResultOnMoveVector`), where the potion splashes
/// (`Projectile.onHit`). Its thrower is not met until it has left them
/// (`Projectile.leftOwner`, checked before the move like `Projectile.tick`),
/// nor anything else in their vehicle tree.
fn tick_thrown_potions(&mut self) {
let mut thrown = std::mem::take(&mut self.potions.thrown);
let players = if thrown.is_empty() {
Vec::new()
} else {
self.thrown_player_targets()
};
let mut landed = Vec::new();
thrown.retain_mut(|t| {
t.age += 1;
// Projectile.tick: whether it has left its thrower's vehicle tree,
// before it moves.
let owner = self.shooter_who(t.thrower, t.guest);
let (ridden, vehicle, boxes) = self.thrown_ridden(owner, &players);
if !t.left_owner {
t.left_owner = thrown_left_owner(&boxes, t.position, t.velocity);
}
let skip: &[Who] = if t.left_owner { &[] } else { &ridden };
t.previous = t.position;
self.thrown_potion_motion(t);
let next = Vec3::new(
@@ -974,7 +1090,9 @@ impl Game<'_> {
t.position.y + t.velocity.y,
t.position.z + t.velocity.z,
);
// Hit a block along the step (sampled), a mob, or fell out of the world.
// Hit a block along the step (sampled), a mob, a player, or fell out
// of the world.
let margin = thrown_margin(t.age);
let steps = 4;
let mut hit = None;
for i in 1..=steps {
@@ -989,18 +1107,27 @@ impl Game<'_> {
.block(p.x.floor() as i32, p.y.floor() as i32, p.z.floor() as i32);
let mob = self.mobs.iter().find(|m| {
m.alive()
&& (t.left_owner || Some(m.id) != vehicle)
&& (m.position.x - p.x).abs() < m.kind.width / 2.0 + 0.125
&& (m.position.z - p.z).abs() < m.kind.width / 2.0 + 0.125
&& p.y >= m.position.y
&& p.y <= m.position.y + m.kind.height
});
if self.res.facts().get(state).full_collision || mob.is_some() {
// A player (`canHitEntity`: not the thrower before it has left
// them), the box grown by the 0.3 pick margin.
let player = thrown_player_at(&players, skip, p, margin);
if self.res.facts().get(state).full_collision || mob.is_some() || player.is_some() {
// A block hit has the face it came in by
// (`onHitBlock`); a mob hit has none.
let block = mob
.is_none()
// (`onHitBlock`); an entity hit has none.
let block = (mob.is_none() && player.is_none())
.then(|| thrown_block_hit(t.position, p, t.velocity));
hit = Some((p, mob.map(|m| m.position), block));
let at = mob.map(|m| m.position).or_else(|| {
player.map(|(_, b)| {
let c = b.center();
Vec3::new(c.x, b.min(Axis::Y), c.z)
})
});
hit = Some((p, at, block));
break;
}
}
@@ -1615,6 +1742,55 @@ mod tests {
use super::*;
use horizon_client::screen_effects::nausea_blend_added;
/// #5876: a potion thrown straight up from inside its thrower's box does
/// not hit them while it climbs out, then hits them on the way down;
/// another player in its path is hit at once.
#[test]
fn a_thrown_potion_hits_its_thrower_only_after_leaving_them() {
// A player's box: 0.6 wide, 1.8 high, feet at the origin.
let body = |x: f64| Aabb::new(Vec3::new(x - 0.3, 0.0, -0.3), Vec3::new(x + 0.3, 1.8, 0.3));
let thrower = Who::Mob(1);
let players = [(thrower, body(0.0)), (Who::Mob(2), body(5.0))];
let ridden = [body(0.0)];
let eye = Vec3::new(0.0, 1.52, 0.0);
let up = Vec3::new(0.0, 0.5, 0.0);
let m = thrown_margin(10);
assert!((m - 0.3).abs() < 1e-12 && thrown_margin(1) == 0.0);
// Thrown from the eye: still within a block of its thrower.
assert!(!thrown_left_owner(&ridden, eye, up));
assert!(thrown_player_at(&players, &[thrower], eye, m).is_none());
// Without the gate the thrower's box does hold it.
assert_eq!(
thrown_player_at(&players, &[], eye, m).map(|t| t.0),
Some(thrower)
);
// Four blocks up and rising: clear of the box grown by a block.
let high = Vec3::new(0.0, 5.0, 0.0);
assert!(thrown_left_owner(&ridden, high, up));
// Falling back (left_owner now set), the thrower is hit at their head.
let head = Vec3::new(0.0, 2.0, 0.0);
assert_eq!(
thrown_player_at(&players, &[], head, m).map(|t| t.0),
Some(thrower)
);
// Another player is hit though the thrower has not left; a miss is none.
let beside = Vec3::new(5.2, 1.0, 0.0);
assert_eq!(
thrown_player_at(&players, &[thrower], beside, m).map(|t| t.0),
Some(Who::Mob(2))
);
assert!(thrown_player_at(&players, &[thrower], Vec3::new(2.5, 1.0, 0.0), m).is_none());
// A passenger sharing the thrower's vehicle is skipped like the thrower.
assert!(thrown_player_at(&players, &[thrower, Who::Mob(2)], beside, m).is_none());
// The vehicle's box keeps it from leaving: a mount 2 blocks wide.
let mount = Aabb::new(Vec3::new(-1.0, -1.0, -1.0), Vec3::new(1.0, 1.0, 1.0));
let beside_mount = Vec3::new(1.9, 0.0, 0.0);
assert!(thrown_left_owner(&[body(0.0)], beside_mount, up));
assert!(!thrown_left_owner(&[body(0.0), mount], beside_mount, up));
// No thrower (a dispenser's potion): it has left them already.
assert!(thrown_left_owner(&[], eye, up));
}
/// A server's client (multiplayer and the default single player) fades
/// the darkness effect in over its 22 blend ticks, holds it, and fades
/// it out over the last 22 (#3393).
+27 -4
View File
@@ -3587,9 +3587,15 @@ impl Game<'_> {
.projectiles
.iter()
.find(|s| s.id == p.item as u32);
let shot = shot.map(|s| s.position).or_else(|| {
self.tridents
.iter()
.find(|t| t.id == p.item as u32)
.map(|t| t.position)
});
let at = match (item, shot) {
(Some(d), _) => Some([d.position.x, d.position.y, d.position.z]),
(None, Some(s)) => Some([s.position.x, s.position.y, s.position.z]),
(None, Some(s)) => Some([s.x, s.y, s.z]),
(None, None)
if self
.remote
@@ -3630,8 +3636,12 @@ impl Game<'_> {
.find(|s| s.id == p.item as u32)
.cloned()
{
// An arrow or trident flies to its collector too, then goes.
// An arrow flies to its collector too, then goes.
self.add_pickup(super::pickups::Taken::Arrow(Box::new(shot)), collector);
} else if let Some(i) = self.tridents.iter().position(|t| t.id == p.item as u32) {
// So does a trident: a server's client keeps them apart.
let trident = self.tridents.remove(i);
self.add_pickup(super::pickups::Taken::Trident(Box::new(trident)), collector);
}
// An orb stays: the server may still hold merged ones in it.
if !orb {
@@ -5526,6 +5536,10 @@ impl Game<'_> {
} else {
Keys::default()
};
// The held lead's end eases from the look at the start of the tick
// (`yRotO`/`xRotO`), as the local tick does; `tick` returns before it
// reaches its own write when this window is a server client.
self.rope_rot_old = (self.yaw, self.pitch);
// A passenger moves with its vehicle (Entity.rideTick), not by itself.
let riding = self.screen != Screen::Dead && self.remote_ride(keys);
// The server sends no passenger's position: they sit where their
@@ -6076,13 +6090,17 @@ impl Game<'_> {
// The particles these mobs' own ticks draw on the client side.
{
use horizon_client::mobs::{
Species as S, cube, illusioner, nautilus, tame, wither,
Species as S, cube, illager, illusioner, nautilus, tame, wither,
};
match m.species() {
S::Wolf => tame::client_tick(m, rain, &mut mob_fx),
S::Slime | S::MagmaCube | S::SulfurCube => cube::client_tick(m, &mut mob_fx),
S::Nautilus | S::ZombieNautilus => nautilus::client_tick(m, &mut mob_fx),
S::Illusioner => illusioner::client_tick(m, &mut mob_fx),
S::Illusioner => {
illusioner::client_tick(m, &mut mob_fx);
illager::cast_particles(m, &mut mob_fx);
}
S::Evoker => illager::cast_particles(m, &mut mob_fx),
S::Wither => wither::client_smoke(m, &mut mob_fx),
_ => {}
}
@@ -7172,6 +7190,11 @@ impl Game<'_> {
// CushionItem.useOn on a client level: SUCCESS once
// `canBePlacedAt` holds, before the server's own checks.
self.cushion_spawn(hit).is_some()
} else if &*stack.item == "minecraft:painting" {
// HangingEntityItem.useOn on a client level: SUCCESS once
// `Painting.create` finds a surviving variant, before the
// server spawns it.
self.painting_fits(hit, stack)
} else {
false
}
@@ -127,6 +127,12 @@ fn remote_arrow_tick(p: &mut Projectile, w: &impl ProjectileWorld) {
if water {
p.velocity = p.velocity.scale(0.6);
}
let (movement_x, movement_y, movement_z) = (p.velocity.x, p.velocity.y, p.velocity.z);
let sd = (movement_x * movement_x + movement_z * movement_z).sqrt();
let y_rot = horizon_core::geometry::trig::atan2_degrees(movement_x, movement_z);
let x_rot = horizon_core::geometry::trig::atan2_degrees(movement_y, sd);
p.pitch = horizon_client::arrows::lerp_rotation(p.pitch, x_rot);
p.yaw = horizon_client::arrows::lerp_rotation(p.yaw, y_rot);
let to = p.position.add_vector(p.velocity);
if let Some((at, _, _)) = w.clip(p.position, to) {
p.position = at;
@@ -286,6 +292,8 @@ impl Game<'_> {
velocity,
fuse: 80,
on_ground: false,
owner: None,
guest: None,
}),
// FallingBlockEntity.recreateFromPacket: the block state is the data.
"minecraft:falling_block" => self.falling.push(FallingBlock {
@@ -140,8 +140,27 @@ impl Game<'_> {
if !self.settings.horizon.fog {
horizon_client::fog::clear_distance_fog(&mut fog, &input);
}
if self.title.is_none() {
fog.color =
self.native_fog_color_now(self.clock.fraction() as f32, at, fog_type, fog.color);
}
fog
}
fn native_fog_color_now(
&self,
partial: f32,
camera: [f64; 3],
fog_type: FogType,
color: [f32; 4],
) -> [f32; 4] {
let mut rgb = [color[0], color[1], color[2]];
self.native_compute_fog_color(partial, camera, fog_type, &mut rgb);
if rgb.iter().all(|v| v.is_finite() && (0.0..=1.0).contains(v)) {
[rgb[0], rgb[1], rgb[2], color[3]]
} else {
color
}
}
/// `FogRenderer.computeFogColor`'s night vision brightenFactor:
/// `GameRenderer.nightVisionScale`, but not while Darkness is on.
fn fog_night_vision(&self) -> f32 {
+2 -1
View File
@@ -35,7 +35,8 @@ impl Screen {
| Self::Inventory
| Self::Dead
| Self::CommandBlock
| Self::BlockEditor => false,
| Self::BlockEditor
| Self::Mod => false,
// WinScreen.extractBackground: the poem fills the screen with the End portal, the
// credits alone blur. Game::menu_blur knows which one rolls.
Self::Win => false,
@@ -99,6 +99,16 @@ impl Rideable for minecarts::Minecart {
/// The vehicle `id` leaves the level it is in, at the point its rider
/// arrives. It keeps its entity id, so the ride is never broken.
/// A guest leaving for another level leaves its fishing hook behind in the
/// old one, where vanilla's hook stays until its distance check ends it. A
/// guest's hook lives on the guest here, so it ends now: it must not act on
/// the blocks of the level the guest arrives in.
fn drop_hook(host: &mut LanHost, conn: u32) {
if let Some(play) = host.play.get_mut(&conn) {
play.hook = None;
}
}
fn take_vehicle<T: Rideable>(list: &mut Vec<T>, id: u32, at: Vec3, yaw: f32) -> Option<T> {
let i = list.iter().position(|v| v.id() == id)?;
let mut v = list.remove(i);
@@ -337,6 +347,9 @@ impl Game<'_> {
let Some(i) = host.hub.guests.iter().position(|g| g.conn == conn) else {
return;
};
if target != self.dim {
drop_hook(host, conn);
}
let g = &host.hub.guests[i];
let from = Vec3::new(g.position[0], g.position[1], g.position[2]);
let yaw = g.yaw;
@@ -685,6 +698,9 @@ impl Game<'_> {
continue;
};
let source = self.dim;
if t.dim != source {
drop_hook(host, t.conn);
}
if let Err(e) = self.enter_level(Some(host), t.dim) {
log::error!("could not open {}: {e}", t.dim.id());
continue;
+60 -7
View File
@@ -68,6 +68,33 @@ pub(super) fn write_shader_options(options: &ShaderOptions) {
}
}
/// The pack that runs over the world ("": none): `HORIZON_SHADER_PACK`, else the pick
/// (`active_pack`).
fn picked_pack(settings: &horizon_client::settings::Settings) -> &str {
static DEV_PACK: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
let dev = DEV_PACK.get_or_init(|| std::env::var("HORIZON_SHADER_PACK").ok());
active_pack(
dev.as_deref().unwrap_or(&settings.horizon.shader_pack),
&settings.mods,
)
}
/// Whether the pack picked (`picked_pack`) draws flat where the device can (`pack_draws_flat`):
/// a renderer made now lays the sections out for it before the first world frame loads it
/// (`Renderer::new`).
pub(super) fn picked_pack_draws_flat(settings: &horizon_client::settings::Settings) -> bool {
let picked = picked_pack(settings);
!picked.is_empty() && pack_draws_flat(&Game::shader_packs_dir().join(picked))
}
/// Whether the pack at `path` has passes that draw the terrain flat where the device can,
/// reading translucent quads beside their section's solid ones: a `shaders/` pack, taken to
/// draw terrain, or one of Horizon's own with a `[shadow]` pass.
fn pack_draws_flat(path: &std::path::Path) -> bool {
!horizon_shader_api::package::is_pack(path)
|| horizon_shader_api::Pack::read(path).is_ok_and(|p| p.manifest.shadow.is_some())
}
// A native mod can bundle a pack as `native-<mod-id>`; its Mods toggle controls it too.
fn active_pack<'a>(picked: &'a str, mods: &horizon_client::mod_list::ModList) -> &'a str {
if picked.strip_prefix("native-").is_some_and(|id| {
@@ -124,12 +151,7 @@ impl Game<'_> {
self.streaming.shader_pack_lighting = pack_lighting;
self.all_sections_changed();
}
static DEV_PACK: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
let dev = DEV_PACK.get_or_init(|| std::env::var("HORIZON_SHADER_PACK").ok());
let picked = active_pack(
dev.as_deref().unwrap_or(&self.settings.horizon.shader_pack),
&self.settings.mods,
);
let picked = picked_pack(&self.settings);
if self.native_shaders.picked != picked {
// Another pack, or none: drop what ran.
self.native_shaders = super::native_shaders::NativeShaders::picking(picked);
@@ -285,6 +307,13 @@ impl Game<'_> {
rainfall: info.map_or(0.0, |b| b.downfall),
held_items,
held_light,
right_handed: !self.settings.main_hand_left,
on_ground: self.player.as_ref().is_some_and(|p| p.ground.on_ground),
min_y: self.world.shape.min_y,
height: self.world.shape.height,
logical_height: self.world.shape.logical_height,
has_skylight: self.world.shape.sky_light,
has_ceiling: self.dim == Dim::Nether,
mod_params: self.native_shader_params_now(partial),
}
}
@@ -562,9 +591,33 @@ fn block_id_table(pack: &super::PackResources, ids: &BlockIds) -> Vec<i32> {
#[cfg(test)]
mod native_pack_tests {
use super::active_pack;
use super::{active_pack, pack_draws_flat};
use horizon_client::mod_list::{Installed, ModList, NativePage};
#[test]
fn shaders_packs_and_packs_with_a_shadow_pass_draw_flat() {
let root = std::env::temp_dir().join(format!("horizon-flat-packs-{}", std::process::id()));
// A folder without a manifest is a `shaders/` pack; with one, a pack of Horizon's own.
let pack = |name: &str, manifest: Option<&str>| {
let dir = root.join(name);
std::fs::create_dir_all(&dir).unwrap();
if let Some(extra) = manifest {
std::fs::write(dir.join("material.wgsl"), "").unwrap();
let text = format!(
"schema = 1\nid = \"{name}\"\nname = \"{name}\"\nversion = \"1\"\napi = \"{}\"\n\
[material]\nfile = \"material.wgsl\"\n{extra}",
horizon_shader_api::API
);
std::fs::write(dir.join(horizon_shader_api::MANIFEST), text).unwrap();
}
dir
};
assert!(pack_draws_flat(&pack("shaders_pack", None)));
assert!(!pack_draws_flat(&pack("unshadowed", Some(""))));
assert!(pack_draws_flat(&pack("shadowed", Some("[shadow]\n"))));
std::fs::remove_dir_all(&root).ok();
}
#[test]
fn native_shader_follows_its_mod_without_affecting_other_packs() {
let mut mods = ModList::default();
+8
View File
@@ -375,6 +375,8 @@ impl<'a> Split<'a> {
let mut old = self.host.renderer.take().ok_or("no renderer")?;
old.release_surface();
let api = self.host.settings.horizon.api;
let batching = self.host.settings.horizon.translucent_batching;
let shader_pack = super::shader_packs::picked_pack_draws_flat(&self.host.settings);
let built = build_renderer(
window.clone(),
true,
@@ -383,6 +385,8 @@ impl<'a> Split<'a> {
Some(&old),
None,
api,
batching,
shader_pack,
);
let renderer = match built {
Ok(r) => r,
@@ -446,6 +450,8 @@ impl<'a> Split<'a> {
let old = self.host.renderer.take().ok_or("no renderer")?;
old.take_back(main.compositor);
let api = self.host.settings.horizon.api;
let batching = self.host.settings.horizon.translucent_batching;
let shader_pack = super::shader_packs::picked_pack_draws_flat(&self.host.settings);
let built = build_renderer(
window.clone(),
false,
@@ -454,6 +460,8 @@ impl<'a> Split<'a> {
Some(&old),
None,
api,
batching,
shader_pack,
);
let renderer = match built {
Ok(r) => r,
@@ -323,6 +323,9 @@ impl Split<'_> {
host.renderer.as_ref(),
None,
host.settings.horizon.api,
host.settings.horizon.translucent_batching,
// The pack its own game loads, from its profile.
crate::game::shader_packs::picked_pack_draws_flat(&guest.settings),
)?);
guest.window = Some(window);
guest.world_renderer_ready = true;
+81 -5
View File
@@ -2,7 +2,9 @@
//! power 4, `TntRenderer` swell and white flash, and `TntBlock.useItemOn`
//! (flint and steel / fire charge).
use super::Game;
use horizon_client::{Result, collisions::SceneCollisions, items::ItemStack, mesher::Vertex};
use horizon_client::{
Result, collisions::SceneCollisions, items::ItemStack, mesher::Vertex, mobs::Who,
};
use horizon_core::{
geometry::{Aabb, Vec3},
position::BlockPos,
@@ -19,6 +21,8 @@ pub(super) struct PrimedTnt {
pub velocity: Vec3,
pub fuse: i32,
pub on_ground: bool,
pub owner: Option<Who>,
pub guest: Option<u32>,
}
impl PrimedTnt {
const SIZE: f64 = 0.98;
@@ -110,7 +114,13 @@ impl PrimedTnt {
impl Game<'_> {
/// `new PrimedTnt(level, x + 0.5, y, z + 0.5, owner)`; `short` applies
/// `wasExploded`'s random short fuse. Plays `entity.tnt.primed` for primes.
pub(super) fn spawn_primed_tnt(&mut self, pos: BlockPos, short: bool) {
pub(super) fn spawn_primed_tnt(
&mut self,
pos: BlockPos,
short: bool,
owner: Option<Who>,
guest: Option<u32>,
) {
// PrimedTnt: the fuse is lit.
let who = self.sculk_actor();
self.post_block_game_event("minecraft:prime_fuse", [pos.x, pos.y, pos.z], who, None);
@@ -132,10 +142,17 @@ impl Game<'_> {
);
80
};
self.add_primed_tnt(pos, rot, fuse);
self.add_primed_tnt(pos, rot, fuse, owner, guest);
}
/// Adds primed TNT at the block's bottom centre, pushed along `rot`.
pub(super) fn add_primed_tnt(&mut self, pos: BlockPos, rot: f64, fuse: i32) {
pub(super) fn add_primed_tnt(
&mut self,
pos: BlockPos,
rot: f64,
fuse: i32,
owner: Option<Who>,
guest: Option<u32>,
) {
let position = Vec3::new(
f64::from(pos.x) + 0.5,
f64::from(pos.y),
@@ -148,6 +165,8 @@ impl Game<'_> {
velocity: Vec3::new(-rot.sin() * 0.02, f64::from(0.2f32), -rot.cos() * 0.02),
fuse,
on_ground: false,
owner,
guest,
});
}
/// `PrimedTnt.tick` for every primed TNT, in order; an expired fuse
@@ -177,15 +196,28 @@ impl Game<'_> {
t.position.z,
];
let bounds = t.bounds();
let (owner, guest) = (t.owner, t.guest);
let in_water = self.bounds_in_water(bounds);
self.tnts.remove(i);
count -= 1;
// ExplosionInteraction.TNT: the tnt_explosion_drop_decay rule.
let interaction = self.destroy_type("tnt_explosion_drop_decay");
self.combat.explosion_in_water = in_water;
// `Explosion.getIndirectSourceEntity`: a player-lit TNT names
// its igniter, which `blast_attacker` and the damage type read.
self.combat.blast_guest = guest;
self.combat.blast_owner = match owner {
Some(Who::Player) => self
.lan
.as_ref()
.and_then(|host| self.player_attacker(host, guest)),
Some(Who::Mob(_)) | None => None,
};
let result = self.with_hive_emptying_blast(|g| {
g.server_explode(center, 4.0, interaction, false)
});
self.combat.blast_owner = None;
self.combat.blast_guest = None;
self.combat.explosion_in_water = false;
result?;
} else {
@@ -208,7 +240,8 @@ impl Game<'_> {
return Ok(false);
}
let pos = BlockPos::new(hit.pos[0], hit.pos[1], hit.pos[2]);
self.spawn_primed_tnt(pos, false);
let guest = self.lan_acting;
self.spawn_primed_tnt(pos, false, Some(Who::Player), guest);
let air = self.world.air;
self.with_level(|b, level, ticks| {
b.set_block(level, ticks, pos, air, level_flags::UPDATE_ALL_IMMEDIATE)
@@ -492,10 +525,49 @@ impl FluidFlowWorld for TntFluidWorld<'_> {
}
}
impl Game<'_> {
/// `positionRider` for the mobs riding primed TNT: the passenger attachment
/// (the top of its 0.98 box), less the rider's own vehicle attachment.
pub(super) fn seat_tnt_mobs(&mut self) {
for i in 0..self.tnts.len() {
let (id, pos, prev) = (
self.tnts[i].id,
self.tnts[i].position,
self.tnts[i].previous,
);
if id == 0 {
continue;
}
for m in self.mobs.iter_mut().filter(|m| m.extra.mount == Some(id)) {
let offset = horizon_client::mobs::riders::vehicle_attachment(m);
m.position = tnt_seat(pos, offset);
m.previous = tnt_seat(prev, offset);
m.velocity = Vec3::ZERO;
m.fall_distance = 0.0;
m.on_ground = false;
m.extra.passenger = true;
m.ai.nav.stop();
}
}
}
}
fn tnt_seat(tnt: Vec3, rider_offset: f64) -> Vec3 {
Vec3::new(tnt.x, tnt.y + PrimedTnt::SIZE - rider_offset, tnt.z)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_rider_sits_on_top_of_the_tnt_less_its_own_attachment() {
// 64 + 0.98 (the box's top) - 0.14 (a rider's vehicle attachment).
let seat = tnt_seat(Vec3::new(1.5, 64.0, -2.5), 0.14);
assert_eq!((seat.x, seat.z), (1.5, -2.5));
assert!((seat.y - 64.84).abs() < 1e-9);
}
#[test]
fn a_piston_slime_bounce_launches_primed_tnt() {
use horizon_core::geometry::Direction;
@@ -507,6 +579,8 @@ mod tests {
velocity: Vec3::ZERO,
fuse: 80,
on_ground: true,
owner: None,
guest: None,
};
tnt.velocity = bounce_velocity(Direction::Up, tnt.velocity);
tnt.on_ground = false;
@@ -547,6 +621,8 @@ mod tests {
velocity: Vec3::ZERO,
fuse: 80,
on_ground: false,
owner: None,
guest: None,
};
tnt.tick(&world, &collision, &assets, &facts).unwrap();
assert_eq!(
+40 -24
View File
@@ -483,11 +483,6 @@ impl Game<'_> {
return false;
};
let rest = self.as_guest(&mut host, conn, |g| g.give(t.stack.clone()));
// Player.take: the guest's client plays the pickup sound.
if rest.is_some() {
let at = [t.position.x, t.position.y, t.position.z];
host.hub.take_item(t.id as i32, by.id, 1, at);
}
self.lan = Some(host);
let Some(rest) = rest else {
return false;
@@ -499,6 +494,14 @@ impl Game<'_> {
self.spawn_item(rest, [by.feet.x, by.feet.y, by.feet.z], [0.0; 3], 0);
}
}
// Player.take, for a creative thrower's trident too: the clients near
// play the pickup sound and fly the trident to its collector.
if by.guest.is_some()
&& let Some(host) = self.lan.as_ref()
{
let at = [t.position.x, t.position.y, t.position.z];
host.hub.take_item(t.id as i32, by.id, 1, at);
}
if by.guest.is_none() {
let pitch = ((self.world.random.next_float() - self.world.random.next_float()) * 0.7
+ 1.0)
@@ -723,25 +726,38 @@ impl Game<'_> {
glam::DVec3::splat(2.0 * HALF),
self.view_scale(),
);
for t in &self.tridents {
// EntityRenderer.shouldRender; ThrownTridentRenderer grows the box by 1.5.
if !self.standing_entity_in_view(
camera,
super::entity_culling::at(t.previous),
super::entity_culling::at(t.position),
alpha,
2.0 * HALF,
2.0 * HALF,
range,
1.5,
) {
continue;
}
let p = glam::DVec3::new(
t.previous.x + (t.position.x - t.previous.x) * alpha,
t.previous.y + (t.position.y - t.previous.y) * alpha,
t.previous.z + (t.position.z - t.previous.z) * alpha,
) - camera;
let live = self
.tridents
.iter()
.filter(|t| {
// EntityRenderer.shouldRender; ThrownTridentRenderer grows the box by 1.5.
self.standing_entity_in_view(
camera,
super::entity_culling::at(t.previous),
super::entity_culling::at(t.position),
alpha,
2.0 * HALF,
2.0 * HALF,
range,
1.5,
)
})
.map(|t| {
let at = [
t.previous.x + (t.position.x - t.previous.x) * alpha,
t.previous.y + (t.position.y - t.previous.y) * alpha,
t.previous.z + (t.position.z - t.previous.z) * alpha,
];
(t, at)
});
// ItemPickupParticle: a picked-up trident flying to its collector,
// drawn with the render state extracted at partial tick 1.
let taken = self.pickups.live.iter().filter_map(|p| match &p.taken {
super::pickups::Taken::Trident(t) => Some((&**t, super::pickups::position(p, alpha))),
_ => None,
});
for (t, at) in live.chain(taken) {
let p = glam::DVec3::from_array(at) - camera;
let base = glam::Mat4::from_translation(p.as_vec3())
* glam::Mat4::from_rotation_y((t.yaw - 90.0).to_radians())
* glam::Mat4::from_rotation_z((t.pitch + 90.0).to_radians());
+10
View File
@@ -993,6 +993,16 @@ fn login_and_configure(
}),
)?;
}
// And how far it samples far terrain for it (crate::lod_net).
if brand == "horizon" && shared.config.lod_distance > 0 {
send(
c,
cfg::Clientbound::CustomPayload(cfg::CustomPayload {
channel: crate::lod_net::CHANNEL.into(),
data: crate::lod_net::Offer::new(shared.config.lod_distance).encode(),
}),
)?;
}
if brand == "horizon"
&& let Some(hash) = sync.content_identity
{
+8
View File
@@ -2870,6 +2870,14 @@ impl Hub {
break;
}
}
// handleUseItem uses the player's position and known movement now:
// a later move must not overtake a throw before the game runs it.
S::UseItem(p) => {
events.push(HostEvent::Packet(conn, Box::new(S::UseItem(p))));
if self.move_queued(i) {
break;
}
}
// handleUseItemOn is the game's too, and BlockItem.canPlace checks
// the player's box where they clicked from (Level.isUnobstructed), as
// their client's prediction did. After the move behind it, a player
+5
View File
@@ -198,6 +198,10 @@ pub struct NetConfig {
/// client whose `minecraft:brand` isn't Horizon's is turned away during
/// configuration. A brand can be spoofed: a gate, not security.
pub horizon_clients_only: bool,
/// `horizon-lod-distance` (Horizon's own server.properties key, 64): how
/// far, in chunks, the server samples far terrain for Horizon clients
/// (`lod_net`); 0 serves none.
pub lod_distance: u32,
/// The shapes of the world's levels: their dimension types go to
/// clients when the world's data packs changed them.
pub levels: crate::chunk::LevelShapes,
@@ -232,6 +236,7 @@ impl NetConfig {
code_of_conducts: Vec::new(),
server_links: Vec::new(),
horizon_clients_only: false,
lod_distance: crate::lod_net::DEFAULT_DISTANCE,
levels: crate::chunk::LevelShapes::VANILLA,
owner_profile: None,
}
+3
View File
@@ -89,6 +89,9 @@ pub fn update_recipes(
let mut all = BTreeMap::new();
recipes(&resources.join("data/minecraft/recipe"), "", &mut all);
overlay_recipes(overlay, &mut all);
// The data's recipe filters, as the server's own tables apply them.
let filters = horizon_items::recipe_filters::RecipeFilters::load(resources, overlay);
all.retain(|id, v| filters.apply(id, v));
let tags: BTreeMap<&str, &[i32]> = sync
.tags
.registries
+1
View File
@@ -78,6 +78,7 @@ pub mod item_tooltip;
pub mod lan;
pub mod landing;
pub mod locator_bar;
pub mod lod_net;
pub mod menu;
pub mod merchant;
pub mod mobs;
Loaded 100 of 205 files, more files were not shown because too many files have changed in this diff. Show more