From d6f8dda823921845fed39d5b1722d33afc4e4dc9 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 05:13:25 +0200 Subject: [PATCH 01/39] Metal: the GPU frame timer measures the frame again (read about 0.01 ms) On Apple GPUs timestamps go only at pass boundaries, and the frame was bracketed by two one-thread compute passes. Two things made it read about 0.01 ms: - Metal resolves counter samples without waiting for the passes that write them. Resolved in the frame's own command buffer, the end timestamp was mostly the one an earlier frame left in the shared query. Each readback slot now has its own queries, and with markers a slot is resolved in a later frame, once on_submitted_work_done says the GPU finished the frame it timed. - A compute pass depends on nothing, so the end marker ran alongside the frame's render passes. The end is now a render pass that loads the frame's target, shading one pixel with every write masked: it runs after every pass that drew into the target. The start stays a compute pass (cheaper than a load and store of the target). M4 Pro, render distance 32, offscreen: GPU p50 0.002 ms before, now 1.95 ms against a 1.95 ms frame (village) and 4.46 ms against 4.51 ms (mountain), where the frame is GPU-bound. Vulkan and DirectX 12, which write timestamps between commands, are unchanged but for the per-slot queries. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 8 +- crates/horizon-client/src/gfx/timing.rs | 230 ++++++++++++++++++------ 2 files changed, 185 insertions(+), 53 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index dfa2c7239..c0b873a5e 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -4003,7 +4003,11 @@ impl Renderer { cull.end(&self.device, &mut encoder); } if let Some(timer) = &mut self.gpu_timer { - timer.end(&mut encoder); + timer.end( + &self.device, + &mut encoder, + (&view_target, self.config.format.remove_srgb_suffix()), + ); } let readback = self.screenshot.take().and_then(|path| { // The copy and PNG writer both assume 8-bit pixels; a float surface would make @@ -4034,7 +4038,7 @@ impl Renderer { cull.submitted(); } if let Some(timer) = &mut self.gpu_timer { - timer.submitted(); + timer.submitted(&self.queue); timer.collect(&self.device, &mut self.gpu_times); } if let Some((path, buffer)) = readback { diff --git a/crates/horizon-client/src/gfx/timing.rs b/crates/horizon-client/src/gfx/timing.rs index 8f8eee6ee..5fc566fe7 100644 --- a/crates/horizon-client/src/gfx/timing.rs +++ b/crates/horizon-client/src/gfx/timing.rs @@ -1,18 +1,35 @@ //! GPU frame time from timestamp queries, read back a few frames late so the //! CPU never waits for it. Frames whose readback slot is still busy go untimed. //! -//! Where the GPU can only time pass boundaries (Apple GPUs on Metal), the -//! frame is bracketed by two one-thread compute passes that write the -//! timestamps (Metal leaves an empty pass's timestamps at zero). +//! Where the GPU can only time pass boundaries (Apple GPUs on Metal), a +//! one-thread compute pass stamps the frame's start, and a render pass over +//! the frame's target that shades one pixel with every write masked stamps +//! its end (Metal leaves the timestamps of a pass that does nothing at zero). +//! A pass that loads the target runs after every pass that draws into it; a +//! compute pass, which depends on nothing, ran alongside them. Metal also +//! resolves timestamps without waiting for the passes that write them, so +//! they are resolved in a later frame, once the GPU has finished this one. +//! Resolved in the frame itself, the end was mostly the one an earlier frame +//! left in the query, and frames read about 0.01 ms. use std::sync::{ Arc, atomic::{AtomicU8, Ordering}, }; -const SLOTS: usize = 4; +/// Frames in flight at once; on Metal a slot is busy for about five frames. +const SLOTS: usize = 8; +/// Each slot's timestamps have their own queries and resolve range. +const RESOLVE_STRIDE: u64 = wgpu::QUERY_RESOLVE_BUFFER_ALIGNMENT; +/// The frame's target and its format. +pub type Target<'a> = (&'a wgpu::TextureView, wgpu::TextureFormat); const FREE: u8 = 0; -const PENDING: u8 = 1; -const MAPPED: u8 = 2; +/// Its frame is submitted; with markers, until the GPU has finished it. +const RUNNING: u8 = 1; +/// Its frame is finished: its timestamps can be resolved. +const DONE: u8 = 2; +/// Resolved and on its way back. +const PENDING: u8 = 3; +const MAPPED: u8 = 4; struct Slot { buffer: wgpu::Buffer, @@ -27,10 +44,29 @@ pub struct GpuTimer { current: Option, next: usize, period_ns: f32, - /// Timestamps between commands, or else this pass's boundaries. - marker: Option, + /// Timestamps between commands, or else these passes' boundaries. + markers: Option, + /// Slots resolved in this frame's commands, read back once it is submitted. + resolved: Vec, } +/// The marker passes: the start's compute pipeline, and the end's shader and +/// pipeline, for the target format it was made for. +struct Markers { + module: wgpu::ShaderModule, + start: wgpu::ComputePipeline, + end: Option<(wgpu::TextureFormat, wgpu::RenderPipeline)>, +} + +/// The start's one thread; the end's triangle over the whole target (the +/// pass's scissor keeps one pixel), shading nothing it writes. +const MARKER: &str = + "@vertex fn cover(@builtin(vertex_index) i: u32) -> @builtin(position) vec4 { + return vec4(f32(i & 1u) * 4.0 - 1.0, f32(i >> 1u) * 4.0 - 1.0, 0.0, 1.0); +} +@fragment fn nothing() -> @location(0) vec4 { return vec4(0.0); } +@compute @workgroup_size(1) fn mark() {}"; + impl GpuTimer { /// The features timing uses that the adapter has (none: no timing). pub fn features(adapter: &wgpu::Adapter) -> wgpu::Features { @@ -44,11 +80,11 @@ impl GpuTimer { let queries = device.create_query_set(&wgpu::QuerySetDescriptor { label: Some("Frame timestamps"), ty: wgpu::QueryType::Timestamp, - count: 2, + count: 2 * SLOTS as u32, }); let resolve = device.create_buffer(&wgpu::BufferDescriptor { label: Some("Frame timestamp resolve"), - size: 16, + size: RESOLVE_STRIDE * SLOTS as u64, usage: wgpu::BufferUsages::QUERY_RESOLVE | wgpu::BufferUsages::COPY_SRC, mapped_at_creation: false, }); @@ -70,42 +106,43 @@ impl GpuTimer { current: None, next: 0, period_ns: queue.get_timestamp_period(), - marker: (!device + markers: (!device .features() .contains(wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS)) .then(|| { let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("Frame timestamp"), - source: wgpu::ShaderSource::Wgsl( - "@compute @workgroup_size(1) fn mark() {}".into(), - ), + source: wgpu::ShaderSource::Wgsl(MARKER.into()), }); - device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { - label: Some("Frame timestamp"), - layout: None, - module: &module, - entry_point: Some("mark"), - compilation_options: Default::default(), - cache: None, - }) + Markers { + start: device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { + label: Some("Frame timestamp"), + layout: None, + module: &module, + entry_point: Some("mark"), + compilation_options: Default::default(), + cache: None, + }), + module, + end: None, + } }), + resolved: Vec::new(), }) } pub fn begin(&mut self, encoder: &mut wgpu::CommandEncoder) { + if self.markers.is_some() { + for slot in 0..SLOTS { + if self.slots[slot].state.load(Ordering::Acquire) == DONE { + self.resolve(encoder, slot); + } + } + } let slot = self.next; self.current = (self.slots[slot].state.load(Ordering::Acquire) == FREE).then_some(slot); - if self.current.is_some() { - self.stamp(encoder, 0); - } - } - pub fn end(&mut self, encoder: &mut wgpu::CommandEncoder) { let Some(slot) = self.current else { return }; - self.stamp(encoder, 1); - encoder.resolve_query_set(&self.queries, 0..2, &self.resolve, 0); - encoder.copy_buffer_to_buffer(&self.resolve, 0, &self.slots[slot].buffer, 0, 16); - } - fn stamp(&self, encoder: &mut wgpu::CommandEncoder, index: u32) { - let Some(marker) = &self.marker else { + let index = 2 * slot as u32; + let Some(markers) = &self.markers else { encoder.write_timestamp(&self.queries, index); return; }; @@ -113,29 +150,85 @@ impl GpuTimer { label: Some("Frame timestamp"), timestamp_writes: Some(wgpu::ComputePassTimestampWrites { query_set: &self.queries, - beginning_of_pass_write_index: (index == 0).then_some(0), - end_of_pass_write_index: (index == 1).then_some(1), + beginning_of_pass_write_index: Some(index), + end_of_pass_write_index: None, }), }); - pass.set_pipeline(marker); + pass.set_pipeline(&markers.start); pass.dispatch_workgroups(1, 1, 1); } - /// After the frame's submission: start reading its timestamps back. - pub fn submitted(&mut self) { + /// After the frame's last pass into `target` (the frame's own, of `format`). + pub fn end( + &mut self, + device: &wgpu::Device, + encoder: &mut wgpu::CommandEncoder, + (target, format): Target, + ) { + let Some(slot) = self.current else { return }; + let index = 2 * slot as u32 + 1; + let Some(markers) = &mut self.markers else { + encoder.write_timestamp(&self.queries, index); + self.resolve(encoder, slot); + return; + }; + if markers.end.as_ref().is_none_or(|(f, _)| *f != format) { + markers.end = Some((format, end_pipeline(device, &markers.module, format))); + } + let (_, pipeline) = markers.end.as_ref().expect("made above"); + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Frame timestamp"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: target, + depth_slice: None, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Load, + store: wgpu::StoreOp::Store, + }, + })], + timestamp_writes: Some(wgpu::RenderPassTimestampWrites { + query_set: &self.queries, + beginning_of_pass_write_index: None, + end_of_pass_write_index: Some(index), + }), + ..Default::default() + }); + pass.set_pipeline(pipeline); + pass.set_scissor_rect(0, 0, 1, 1); + pass.draw(0..3, 0..1); + } + /// Copy `slot`'s timestamps to its readback buffer. + fn resolve(&mut self, encoder: &mut wgpu::CommandEncoder, slot: usize) { + let (first, at) = (2 * slot as u32, RESOLVE_STRIDE * slot as u64); + encoder.resolve_query_set(&self.queries, first..first + 2, &self.resolve, at); + encoder.copy_buffer_to_buffer(&self.resolve, at, &self.slots[slot].buffer, 0, 16); + self.slots[slot].state.store(RUNNING, Ordering::Release); + self.resolved.push(slot); + } + /// After the frame's submission: start reading back what it resolved, + /// and with markers, wait for the GPU to finish the frame it timed. + pub fn submitted(&mut self, queue: &wgpu::Queue) { + for slot in self.resolved.drain(..) { + let state = self.slots[slot].state.clone(); + state.store(PENDING, Ordering::Release); + self.slots[slot] + .buffer + .slice(..) + .map_async(wgpu::MapMode::Read, move |result| { + state.store( + if result.is_ok() { MAPPED } else { FREE }, + Ordering::Release, + ); + }); + } let Some(slot) = self.current.take() else { return; }; - let state = self.slots[slot].state.clone(); - state.store(PENDING, Ordering::Release); - self.slots[slot] - .buffer - .slice(..) - .map_async(wgpu::MapMode::Read, move |result| { - state.store( - if result.is_ok() { MAPPED } else { FREE }, - Ordering::Release, - ); - }); + if self.markers.is_some() { + let state = self.slots[slot].state.clone(); + state.store(RUNNING, Ordering::Release); + queue.on_submitted_work_done(move || state.store(DONE, Ordering::Release)); + } self.next = (slot + 1) % SLOTS; } /// GPU milliseconds of frames whose timestamps have arrived, oldest first. @@ -149,11 +242,46 @@ impl GpuTimer { if let Ok(data) = slot.buffer.slice(..).get_mapped_range() { // Read unaligned: the mapping isn't guaranteed to be 8-byte aligned. let stamps: [u64; 2] = bytemuck::pod_read_unaligned(&data[..16]); - let ticks = stamps[1].saturating_sub(stamps[0]); - out.push((ticks as f64 * f64::from(self.period_ns) / 1e6) as f32); + // A start marker that ran late (rare on Metal) leaves the frame untimed. + if let Some(ticks) = stamps[1].checked_sub(stamps[0]) { + out.push((ticks as f64 * f64::from(self.period_ns) / 1e6) as f32); + } } slot.buffer.unmap(); slot.state.store(FREE, Ordering::Release); } } } + +/// The end marker's pipeline for a `format` target, every write masked. +fn end_pipeline( + device: &wgpu::Device, + module: &wgpu::ShaderModule, + format: wgpu::TextureFormat, +) -> wgpu::RenderPipeline { + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("Frame timestamp"), + layout: None, + vertex: wgpu::VertexState { + module, + entry_point: Some("cover"), + compilation_options: Default::default(), + buffers: &[], + }, + primitive: wgpu::PrimitiveState::default(), + depth_stencil: None, + multisample: wgpu::MultisampleState::default(), + fragment: Some(wgpu::FragmentState { + module, + entry_point: Some("nothing"), + compilation_options: Default::default(), + targets: &[Some(wgpu::ColorTargetState { + format, + blend: None, + write_mask: wgpu::ColorWrites::empty(), + })], + }), + multiview_mask: None, + cache: None, + }) +} From 4dff61b697f8f032f374a6b71bea80b1af23d866 Mon Sep 17 00:00:00 2001 From: kerneldroid <212222389+kerneldroid@users.noreply.github.com> Date: Fri, 23 Oct 2026 10:37:14 +0500 Subject: [PATCH 02/39] feat(shaders): complete wgpu Iris/Horizon API-2 shader pipeline, shadow & G-buffer passes, GLSL translator fixes, and settings/import UI --- crates/horizon-client/src/game.rs | 3 + crates/horizon-client/src/game/options.rs | 6 + crates/horizon-client/src/game/packs.rs | 54 ++ .../horizon-client/src/game/shader_packs.rs | 26 + .../horizon-client/src/game/startup_menu.rs | 27 + crates/horizon-client/src/gfx.rs | 214 ++++- crates/horizon-client/src/gfx/java_stages.rs | 308 +++++-- crates/horizon-client/src/gfx/pack_stages.rs | 254 +++++- crates/horizon-client/src/gfx/world_post.rs | 557 +++++++++++- crates/horizon-client/src/graphics_ui.rs | 13 +- crates/horizon-client/src/java_pack.rs | 27 +- crates/horizon-client/src/options_ui.rs | 311 ++++++- crates/horizon-client/src/shader_pack.rs | 825 +++++++++++++++++- .../src/shader_pack_prelude.wgsl | 108 ++- crates/horizon-shaderpack/src/lib.rs | 28 +- crates/horizon-shaderpack/src/pipeline.rs | 144 ++- crates/horizon-shaderpack/src/properties.rs | 87 +- crates/horizon-shaderpack/src/source.rs | 22 +- crates/horizon-shaderpack/src/translate.rs | 439 +++++++--- crates/horizon-shaderpack/tests/translate.rs | 41 +- docs/shader-packs.md | 20 +- examples/shaderpacks/horizon-ultra/LICENSE | 13 + examples/shaderpacks/horizon-ultra/pack.toml | 134 +++ .../horizon-ultra/shaders/bloom_godrays.wgsl | 61 ++ .../shaders/deferred_lighting.wgsl | 92 ++ .../horizon-ultra/shaders/final_tonemap.wgsl | 29 + .../shaders/water_reflections.wgsl | 58 ++ 27 files changed, 3614 insertions(+), 287 deletions(-) create mode 100644 examples/shaderpacks/horizon-ultra/LICENSE create mode 100644 examples/shaderpacks/horizon-ultra/pack.toml create mode 100644 examples/shaderpacks/horizon-ultra/shaders/bloom_godrays.wgsl create mode 100644 examples/shaderpacks/horizon-ultra/shaders/deferred_lighting.wgsl create mode 100644 examples/shaderpacks/horizon-ultra/shaders/final_tonemap.wgsl create mode 100644 examples/shaderpacks/horizon-ultra/shaders/water_reflections.wgsl diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 0faca0ab4..95c8749f3 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -10700,6 +10700,9 @@ impl ApplicationHandler for Game<'_> { WindowEvent::CloseRequested => { self.pending_save.as_mut().unwrap().quit = true; } + WindowEvent::DroppedFile(path) => { + self.import_shader_pack_path(&path); + } WindowEvent::Focused(focused) => { self.focused = focused; if !focused { diff --git a/crates/horizon-client/src/game/options.rs b/crates/horizon-client/src/game/options.rs index ea7a5f5c5..c715b4d1a 100644 --- a/crates/horizon-client/src/game/options.rs +++ b/crates/horizon-client/src/game/options.rs @@ -243,6 +243,12 @@ impl Game<'_> { super::packs::open_folder(&dir); } Event::Credits => self.open_win_screen(false, super::win::Finish::Options), + Event::ImportShaderPack => { + self.import_shader_pack_dialog(); + } + Event::ReloadShaderPack => { + self.load_shader_pack(); + } } } Ok(()) diff --git a/crates/horizon-client/src/game/packs.rs b/crates/horizon-client/src/game/packs.rs index 94e61d948..13de550e1 100644 --- a/crates/horizon-client/src/game/packs.rs +++ b/crates/horizon-client/src/game/packs.rs @@ -34,6 +34,60 @@ pub fn open_folder(dir: &Path) { log::warn!("Horizon: could not open {}: {e}", dir.display()); } } +/// Open a native file dialog to pick a shader pack `.zip` or folder, falling +/// back to opening `dir` in the system file manager when no dialog helper is present. +pub fn pick_shader_pack(dir: &Path) -> Option { + let _ = std::fs::create_dir_all(dir); + let output = if cfg!(target_os = "windows") { + std::process::Command::new("powershell") + .args([ + "-NoProfile", + "-Command", + "Add-Type -AssemblyName System.Windows.Forms; $f = New-Object System.Windows.Forms.OpenFileDialog; $f.Filter = 'Shader Packs (*.zip;pack.toml)|*.zip;pack.toml|All Files (*.*)|*.*'; if ($f.ShowDialog() -eq 'OK') { Write-Output $f.FileName }", + ]) + .output() + .ok() + } else if cfg!(target_os = "macos") { + std::process::Command::new("osascript") + .args([ + "-e", + "POSIX path of (choose file with prompt \"Select Shader Pack (.zip)\")", + ]) + .output() + .ok() + } else { + std::process::Command::new("zenity") + .args([ + "--file-selection", + "--title=Import Shader Pack (.zip)", + "--file-filter=Shader Packs (*.zip) | *.zip", + ]) + .output() + .ok() + .filter(|o| o.status.success()) + .or_else(|| { + std::process::Command::new("kdialog") + .args(["--getopenfilename", ".", "*.zip|Shader Packs (*.zip)"]) + .output() + .ok() + }) + }; + if let Some(out) = output + && out.status.success() + { + let raw = String::from_utf8_lossy(&out.stdout).trim().to_owned(); + if !raw.is_empty() { + let p = PathBuf::from(raw); + if p.file_name().is_some_and(|n| n == "pack.toml") { + return p.parent().map(Path::to_path_buf); + } + return Some(p); + } + return None; + } + open_folder(dir); + None +} /// The resource root to load from, given the vanilla root and the selection. pub fn startup_root(vanilla: &Path, selected: &[String]) -> std::io::Result { let all = packs::discover(vanilla, &packs_dir()); diff --git a/crates/horizon-client/src/game/shader_packs.rs b/crates/horizon-client/src/game/shader_packs.rs index 7d628ef44..d7ec206ec 100644 --- a/crates/horizon-client/src/game/shader_packs.rs +++ b/crates/horizon-client/src/game/shader_packs.rs @@ -101,6 +101,32 @@ impl Game<'_> { self.load_shader_pack(); } } + /// Import a shader pack from `source` into `shaderpacks/`, activate it, and reload. + pub(super) fn import_shader_pack_path(&mut self, source: &std::path::Path) { + let dest = dir(); + match shader_pack::import_pack(source, &dest) { + Ok(id) => { + self.ui.options.shader_packs = Default::default(); + self.ui.options.shader_packs.poll(dest); + self.settings.horizon.shader_pack = id; + self.ui.options.shader_pack_config = None; + self.ui.options.sync_shader_pack_config(&self.settings); + self.settings.save(&Self::options_path()); + self.load_shader_pack(); + } + Err(e) => { + log::warn!("Horizon: shader pack import failed: {e}"); + self.system_message(Text::literal(format!("Shader pack import: {e}"))); + } + } + } + /// Open the system file picker (or `shaderpacks/` folder) to import a shader pack. + pub(super) fn import_shader_pack_dialog(&mut self) { + let dest = dir(); + if let Some(chosen) = super::packs::pick_shader_pack(&dest) { + self.import_shader_pack_path(&chosen); + } + } fn shader_pack_error(&mut self, id: &str, error: &str) { log::warn!("Horizon: shader pack {id}: {error}"); // The Shader Packs page says why on the pack's row: the first line, diff --git a/crates/horizon-client/src/game/startup_menu.rs b/crates/horizon-client/src/game/startup_menu.rs index a5d076878..e01dd6dc6 100644 --- a/crates/horizon-client/src/game/startup_menu.rs +++ b/crates/horizon-client/src/game/startup_menu.rs @@ -387,6 +387,22 @@ impl Loading { self.menu.win = Some((screen, Instant::now())); } } + Event::ImportShaderPack => { + let dir = horizon_client::shader_pack::folder(); + if let Some(chosen) = packs::pick_shader_pack(&dir) + && let Ok(id) = horizon_client::shader_pack::import_pack(&chosen, &dir) + { + self.menu.options.shader_packs = Default::default(); + self.menu.options.shader_packs.poll(dir); + self.settings.horizon.shader_pack = id; + self.menu.options.shader_pack_config = None; + self.menu.options.sync_shader_pack_config(&self.settings); + self.save_menu_settings(); + } + } + Event::ReloadShaderPack => { + self.save_menu_settings(); + } // The startup menu runs before the game opens a control socket. Event::ButtonClick | Event::CopyAgentDetails => {} } @@ -452,6 +468,17 @@ impl Loading { } self.apply_menu_settings(); } + WindowEvent::DroppedFile(path) => { + let dir = horizon_client::shader_pack::folder(); + if let Ok(id) = horizon_client::shader_pack::import_pack(&path, &dir) { + self.menu.options.shader_packs = Default::default(); + self.menu.options.shader_packs.poll(dir); + self.settings.horizon.shader_pack = id; + self.menu.options.shader_pack_config = None; + self.menu.options.sync_shader_pack_config(&self.settings); + self.save_menu_settings(); + } + } WindowEvent::RedrawRequested => self.draw(), WindowEvent::ModifiersChanged(modifiers) => self.menu.modifiers = modifiers.state(), WindowEvent::Focused(false) => { diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index c14a32e2c..0fd03e01b 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -332,6 +332,11 @@ pub struct Renderer { /// (`crosshair_globals`). crosshair_group: wgpu::BindGroup, crosshair_globals: wgpu::Buffer, + shadow_pipeline: wgpu::RenderPipeline, + shadow_entity_pipeline: wgpu::RenderPipeline, + shadow_globals: wgpu::Buffer, + shadow_group: wgpu::BindGroup, + shadow_entity_group: wgpu::BindGroup, entity_pipeline: wgpu::RenderPipeline, weather: weather::Weather, crumbling_pipeline: wgpu::RenderPipeline, @@ -1271,6 +1276,38 @@ impl Renderer { usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); + let shadow_globals = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Shader pack shadow globals"), + size: std::mem::size_of::() as u64, + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + let shadow_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("Terrain shadow"), + layout: Some(&layout), + vertex: wgpu::VertexState { + module: &shader, + entry_point: Some("vs_terrain"), + compilation_options: Default::default(), + buffers: &terrain_buffers, + }, + primitive: wgpu::PrimitiveState { + topology: tri, + cull_mode: Some(wgpu::Face::Back), + ..Default::default() + }, + depth_stencil: Some(wgpu::DepthStencilState { + format: wgpu::TextureFormat::Depth32Float, + depth_write_enabled: Some(true), + depth_compare: Some(wgpu::CompareFunction::GreaterEqual), + stencil: Default::default(), + bias: Default::default(), + }), + multisample: Default::default(), + fragment: None, + multiview_mask: None, + cache: None, + }); let prepared = prepared.unwrap_or_else(|| { PreparedTextures::new(sprite_pixels, sprite_size, layers, sprite_mips, forcefield) }); @@ -1384,6 +1421,16 @@ impl Renderer { &lightmap_view, &lightmap_sampler, ); + let shadow_group = terrain_group( + &device, + "Shader pack shadow terrain resources", + &bindings, + &shadow_globals, + &sprite_views, + &sprite_sampler, + &lightmap_view, + &lightmap_sampler, + ); let index_quads = 1 << 16; let indices = Self::quad_indices(&device, index_quads); let instance_capacity = 4096; @@ -1539,6 +1586,32 @@ impl Renderer { }, ], }); + let shadow_entity_group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Shader pack shadow entity resources"), + layout: &entity_bindings, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: shadow_globals.as_entire_binding(), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::TextureView(&atlas_view), + }, + wgpu::BindGroupEntry { + binding: 2, + resource: wgpu::BindingResource::Sampler(&atlas_sampler), + }, + wgpu::BindGroupEntry { + binding: 3, + resource: wgpu::BindingResource::TextureView(&lightmap_view), + }, + wgpu::BindGroupEntry { + binding: 4, + resource: wgpu::BindingResource::Sampler(&lightmap_sampler), + }, + ], + }); let entity_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: Some("Entity layout"), bind_group_layouts: &[Some(&entity_bindings)], @@ -1649,6 +1722,32 @@ impl Renderer { wgpu::ColorWrites::ALL, ) }; + let shadow_entity_pipeline = + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("Entity shadow"), + layout: Some(&entity_layout), + vertex: wgpu::VertexState { + module: &entity_shader, + entry_point: Some("vs_entity"), + compilation_options: Default::default(), + buffers: &entity_vertex, + }, + primitive: wgpu::PrimitiveState { + cull_mode: None, + ..Default::default() + }, + depth_stencil: Some(wgpu::DepthStencilState { + format: wgpu::TextureFormat::Depth32Float, + depth_write_enabled: Some(true), + depth_compare: Some(wgpu::CompareFunction::GreaterEqual), + stencil: Default::default(), + bias: Default::default(), + }), + multisample: Default::default(), + fragment: None, + multiview_mask: None, + cache: None, + }); // RenderPipelines.WATER_MASK: the boat's water patch writes depth only. let water_mask_pipeline = entity_pipeline_with( "Water mask", @@ -2157,6 +2256,11 @@ impl Renderer { debug_line_capacity: 4096, crosshair_group, crosshair_globals, + shadow_pipeline, + shadow_entity_pipeline, + shadow_globals, + shadow_group, + shadow_entity_group, entity_pipeline, weather, crumbling_pipeline, @@ -2661,7 +2765,12 @@ impl Renderer { &pack.id, java, )?), - None => Pack::Horizon(pack_stages::PackStages::new(&self.device, format, pack)?), + None => Pack::Horizon(pack_stages::PackStages::new( + &self.device, + &self.queue, + format, + pack, + )?), }), None => None, }; @@ -2703,6 +2812,16 @@ impl Renderer { &self.lightmap_view, &self.lightmap_sampler, ); + self.shadow_group = terrain_group( + &self.device, + "Shader pack shadow terrain resources", + &self.terrain_layout, + &self.shadow_globals, + &views, + &self.sprite_sampler, + &self.lightmap_view, + &self.lightmap_sampler, + ); } pub fn view_projection(&self, frame: &Frame) -> Mat4 { self.projection( @@ -3544,6 +3663,32 @@ impl Renderer { } // The world's targets: the screen, or the scaled target of `post`. let pack = self.pack.is_some() && !frame.title; + let shadow_cfg = if pack { + self.pack.as_ref().and_then(|p| match p { + Pack::Horizon(s) if s.needs_shadow => Some((s.shadow_res, s.shadow_dist)), + Pack::Java(s) if s.needs_shadow() => Some(s.shadow_config(frame.shader.dimension)), + _ => None, + }) + } else { + None + }; + if let Some((shadow_res, shadow_dist)) = shadow_cfg { + self.world_post + .prepare_shadow(&self.device, shadow_res, shadow_dist, sun, -sun); + let shadow_globals = Globals { + view_proj: (self.world_post.shadow_proj * self.world_post.shadow_view) + .to_cols_array_2d(), + ..globals + }; + self.uploads.buffer( + &mut encoder, + &self.shadow_globals, + 0, + bytemuck::bytes_of(&shadow_globals), + ); + } else { + self.world_post.shadow_active = false; + } let scaled = self.world_post .prepare(&self.device, [self.config.width, self.config.height], pack); @@ -3552,6 +3697,36 @@ impl Renderer { None => (&view_target, &self.depth), }; let cull = self.cull.as_ref().filter(|_| culling && solid_draws > 0); + if shadow_cfg.is_some() { + let shadow_view = self.world_post.shadow_depth().clone(); + let mut shadow_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Horizon shadow"), + color_attachments: &[], + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: &shadow_view, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(0.0), + store: wgpu::StoreOp::Store, + }), + stencil_ops: None, + }), + ..Default::default() + }); + if solid_draws > 0 && !flat_frame { + shadow_pass.set_bind_group(0, &self.shadow_group, &[]); + shadow_pass.set_pipeline(&self.shadow_pipeline); + shadow_pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); + shadow_pass.set_vertex_buffer(1, self.instances.slice(..)); + self.draw_sections(&mut shadow_pass, &self.indirect, 0, 0, solid_draws); + } + if entity_quads > 0 { + shadow_pass.set_pipeline(&self.shadow_entity_pipeline); + shadow_pass.set_bind_group(0, &self.shadow_entity_group, &[]); + shadow_pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); + shadow_pass.set_vertex_buffer(0, self.entity_buffer.slice(..)); + shadow_pass.draw_indexed(0..entity_quads * 6, 0, 0..1); + } + } { let attachments = [Some(wgpu::RenderPassColorAttachment { view: world_view, @@ -3753,6 +3928,33 @@ impl Renderer { pass.set_vertex_buffer(0, self.debug_line_buffer.slice(..)); pass.draw(0..4, debug_depth..debug_depth + debug_top); } + if pack { + drop(pass); + self.world_post.capture_opaque_depth(&mut encoder); + let attachments = [Some(wgpu::RenderPassColorAttachment { + view: world_view, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Load, + store: wgpu::StoreOp::Store, + }, + })]; + pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Horizon world, translucent"), + color_attachments: &attachments, + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: world_depth, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Load, + store: wgpu::StoreOp::Store, + }), + stencil_ops: None, + }), + ..Default::default() + }); + pass.set_bind_group(0, &self.group, &[]); + } match self.flat.as_ref().filter(|_| flat_frame) { Some(flat) => { pass.set_pipeline(&flat.translucent); @@ -3824,6 +4026,15 @@ impl Renderer { ); } } + if pack && scaled.is_some() { + self.world_post.build_gbuffer( + &self.queue, + &mut encoder, + &pack_uniform, + frame.shader.eye_light, + frame.shader.rain, + ); + } if let (true, Some((color, depth)), Some(size), Some(stages)) = (pack, &scaled, self.world_post.size(), self.pack.as_mut()) { @@ -3837,6 +4048,7 @@ impl Renderer { size, &self.world_post, pack_uniform, + frame.shader.eye_light, ), Pack::Java(stages) => stages.run( &self.device, diff --git a/crates/horizon-client/src/gfx/java_stages.rs b/crates/horizon-client/src/gfx/java_stages.rs index e9f8ddbb1..8091b0703 100644 --- a/crates/horizon-client/src/gfx/java_stages.rs +++ b/crates/horizon-client/src/gfx/java_stages.rs @@ -1,17 +1,20 @@ //! A Java (OptiFine/Iris) shader pack on the GPU (docs/shader-packs.md): //! Iris's colour targets (`colortex0..`, each with a main and an alternate -//! side) and the pack's `begin`, `prepare`, `deferred`, `composite` and -//! `final` programs as full-screen passes over them, as Iris's -//! `CompositeRenderer` runs them. Horizon's world enters `colortex0` and -//! `depthtex0..2` after `prepare`, where Iris's gbuffers programs would have -//! drawn, and the last picture goes to `world_post`. The targets keep GL's -//! layout (row 0 at the bottom, which the translated vertex programs -//! produce), so the world is flipped going in and the picture coming out. +//! side), pre-translucent depth (`depthtex1`/`depthtex2`), directional +//! shadow map (`shadowtex0`/`shadowtex1`) and the pack's `begin`, `prepare`, +//! `deferred`, `composite` and `final` programs as full-screen passes over +//! them, as Iris's `CompositeRenderer` runs them. Horizon's world enters +//! `colortex0` and `depthtex0..2` after `prepare`, where Iris's gbuffers +//! programs would have drawn, and the last picture goes to `world_post`. The +//! targets keep GL's layout (row 0 at the bottom, which the translated vertex +//! programs produce), so the world is flipped going in and the picture coming +//! out. use super::pack_stages::FrameUniform; use crate::java_pack::{JavaFolder, JavaPasses}; use glam::{Mat3, Mat4, Vec3, Vec4}; use horizon_shaderpack::pipeline::TargetSettings; use horizon_shaderpack::pipeline::{Directives, Program, target_index}; +use horizon_shaderpack::properties::Blend; use horizon_shaderpack::uniforms::{CustomUniforms, SmoothFloat, UniformState}; use std::collections::{BTreeMap, BTreeSet, HashMap}; @@ -35,6 +38,7 @@ const MAX_ATTRIBUTES: usize = 8; const BLIT: &str = r#" @group(0) @binding(0) var color: texture_2d; @group(0) @binding(1) var depth: texture_depth_2d; +@group(0) @binding(2) var opaque_depth: texture_depth_2d; @vertex fn vs(@builtin(vertex_index) i: u32) -> @builtin(position) vec4f { @@ -45,15 +49,20 @@ fn vs(@builtin(vertex_index) i: u32) -> @builtin(position) vec4f { struct World { @location(0) color: vec4f, @location(1) depth: vec4f, + @location(2) opaque_depth: vec4f, } -// The world's colour and depth, upside down into GL's layout; Horizon's -// reversed depth (near 1) as GL's (near 0). +// The world's colour, depth and pre-translucent depth, upside down into GL's +// layout; Horizon's reversed depth (near 1) as GL's (near 0). @fragment fn world_in(@builtin(position) p: vec4f) -> World { let size = textureDimensions(depth); let xy = vec2u(u32(p.x), size.y - 1u - u32(p.y)); - return World(textureLoad(color, xy, 0), vec4f(1.0 - textureLoad(depth, xy, 0))); + return World( + textureLoad(color, xy, 0), + vec4f(1.0 - textureLoad(depth, xy, 0)), + vec4f(1.0 - textureLoad(opaque_depth, xy, 0)), + ); } // A target, upside down back into wgpu's layout. @@ -64,6 +73,42 @@ fn present(@builtin(position) p: vec4f) -> @location(0) vec4f { } "#; +/// Convert a GL `Blend` override from `shaders.properties` into a wgpu `BlendState`. +fn blend_state(blend: &Blend) -> Option { + let Blend::Factors(factors) = blend else { + return None; + }; + let map_factor = |name: &str| -> wgpu::BlendFactor { + use wgpu::BlendFactor as B; + match name { + "ZERO" => B::Zero, + "ONE" => B::One, + "SRC_COLOR" => B::Src, + "ONE_MINUS_SRC_COLOR" => B::OneMinusSrc, + "DST_COLOR" => B::Dst, + "ONE_MINUS_DST_COLOR" => B::OneMinusDst, + "SRC_ALPHA" => B::SrcAlpha, + "ONE_MINUS_SRC_ALPHA" => B::OneMinusSrcAlpha, + "DST_ALPHA" => B::DstAlpha, + "ONE_MINUS_DST_ALPHA" => B::OneMinusDstAlpha, + "SRC_ALPHA_SATURATE" => B::SrcAlphaSaturated, + _ => B::One, + } + }; + Some(wgpu::BlendState { + color: wgpu::BlendComponent { + src_factor: map_factor(&factors[0]), + dst_factor: map_factor(&factors[1]), + operation: wgpu::BlendOperation::Add, + }, + alpha: wgpu::BlendComponent { + src_factor: map_factor(&factors[2]), + dst_factor: map_factor(&factors[3]), + operation: wgpu::BlendOperation::Add, + }, + }) +} + /// A wgpu format for each of Iris's (`InternalTextureFormat`), renderable /// everywhere: 8-bit unsigned normalized formats stay, signed normalized, /// 16-bit and packed float ones become half floats, integer ones 32-bit. @@ -123,11 +168,12 @@ struct Target { #[derive(Clone, Copy, Debug, PartialEq)] enum Source { Target(u32), - /// `depthtex0..2`: the world's depth. + /// `depthtex0` / `gdepthtex`: the world's depth (including translucent water). Depth, + /// `depthtex1` / `depthtex2`: the opaque world's depth (before translucent water). + OpaqueDepth, Noise, - /// `shadowtex*`: no shadow map yet, so a far depth (nothing in shadow); - /// also Distant Horizons' and Voxy's depth, as without those mods. + /// `shadowtex*`: directional sun/moon shadow map depth (or far depth for DH/Voxy). Shadow, /// Anything else (`shadowcolor*`, custom textures): white or black. Blank(bool), @@ -135,7 +181,8 @@ enum Source { fn source(name: &str) -> Source { match name { - "depthtex0" | "depthtex1" | "depthtex2" | "gdepthtex" => Source::Depth, + "depthtex0" | "gdepthtex" => Source::Depth, + "depthtex1" | "depthtex2" => Source::OpaqueDepth, "noisetex" => Source::Noise, "shadowtex0" | "shadowtex1" | "shadow" | "watershadow" => Source::Shadow, n if n.starts_with("dhDepthTex") || n.starts_with("vxDepthTex") => Source::Shadow, @@ -146,6 +193,7 @@ fn source(name: &str) -> Source { /// A sampler of a pass: its texture and sampler bindings. struct SamplerBinding { + name: String, source: Source, dimension: wgpu::TextureViewDimension, /// `isampler` (true) or `usampler` (false). @@ -183,6 +231,7 @@ struct Common { repeat: wgpu::Sampler, compare: wgpu::Sampler, noise: wgpu::TextureView, + custom_textures: BTreeMap, /// Full-screen triangle data per attribute kind (see `PassGpu::attributes`). vertices: [wgpu::Buffer; 4], world_layout: wgpu::BindGroupLayout, @@ -196,6 +245,7 @@ struct Bound { size: [u32; 2], targets: BTreeMap, depth: wgpu::TextureView, + opaque_depth: wgpu::TextureView, /// Per pass, then `final`. groups: Vec, world_in: (wgpu::RenderPipeline, wgpu::BindGroup), @@ -217,6 +267,7 @@ struct Folder { passes: Vec, final_pass: Option, directives: Directives, + shadow_enabled: bool, } pub struct JavaStages { @@ -234,9 +285,25 @@ pub struct JavaStages { uniform_state: UniformState, /// `eyeBrightnessSmooth`, block then sky. eye_smooth: [SmoothFloat; 2], + /// `wetness`, smoothed with `wetnessHalflife` / `drynessHalflife`. + wetness_smooth: SmoothFloat, + /// `centerDepthSmooth`, smoothed with `centerDepthHalflife`. + center_depth_smooth: SmoothFloat, } impl JavaStages { + /// Whether any dimension folder in this pack reads directional shadow maps. + pub fn needs_shadow(&self) -> bool { + self.folders.iter().any(|f| f.shadow_enabled) + } + + /// Shadow map resolution and distance for `dimension`. + pub fn shadow_config(&self, dimension: i8) -> (u32, f32) { + let idx = self.folder_for[(dimension.clamp(-1, 1) + 1) as usize].unwrap_or(0); + let d = &self.folders[idx].directives; + (d.shadow_map_resolution, d.shadow_distance) + } + /// The pipelines of a loaded Java pack, or wgpu's error. pub fn new( device: &wgpu::Device, @@ -245,7 +312,7 @@ impl JavaStages { id: &str, pack: &JavaPasses, ) -> Result { - let common = Common::new(device, queue); + let common = Common::new(device, queue, &pack.custom_textures); let folder_for = [-1, 0, 1].map(|d| pack.folder_for(d)); let scope = device.push_error_scope(wgpu::ErrorFilter::Validation); let folders = pack @@ -275,6 +342,7 @@ impl JavaStages { passes, final_pass, directives: plan.directives, + shadow_enabled: plan.shadow_enabled, }) }) .collect::, _>>()?; @@ -295,6 +363,8 @@ impl JavaStages { uniforms: pack.uniforms.clone(), uniform_state: UniformState::default(), eye_smooth: [SmoothFloat::default(); 2], + wetness_smooth: SmoothFloat::default(), + center_depth_smooth: SmoothFloat::default(), }) } @@ -335,10 +405,26 @@ impl JavaStages { self.previous, ); inputs.set_world(&world, directives.sun_path_rotation); + inputs.shadow_view = post.shadow_view; + inputs.shadow_projection = post.gl_shadow_projection(); + inputs.ambient_occlusion_level = directives.ambient_occlusion_level; + inputs.shadow_map_resolution = directives.shadow_map_resolution as f32; let half_life = directives.eye_brightness_half_life; for (i, smooth) in self.eye_smooth.iter_mut().enumerate() { inputs.eye_smooth[i] = smooth.update(inputs.eye[i], half_life, half_life, frame_time); } + inputs.wetness = self.wetness_smooth.update( + frame.weather[0], + directives.wetness_half_life, + directives.dryness_half_life, + frame_time, + ); + inputs.center_depth_smooth = self.center_depth_smooth.update( + 0.5, + directives.center_depth_half_life, + directives.center_depth_half_life, + frame_time, + ); self.uniforms .update(&mut self.uniform_state, frame_time, &|name| { inputs.builtin(name) @@ -496,10 +582,28 @@ impl JavaStages { size, ) .create_view(&Default::default()); + let opaque_depth = texture( + "Java shader pack opaque depth", + wgpu::TextureFormat::R32Float, + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, + size, + ) + .create_view(&Default::default()); let mut blank = Vec::new(); let groups = all .clone() - .map(|pass| pass.bind(device, queue, &targets, &depth, &self.common, &mut blank)) + .map(|pass| { + pass.bind( + device, + queue, + &targets, + &depth, + &opaque_depth, + post, + &self.common, + &mut blank, + ) + }) .collect(); // The world goes into colortex0's side the gbuffers would write. @@ -509,7 +613,11 @@ impl JavaStages { &self.common, &self.common.world_layout, "world_in", - &[colortex0.format, wgpu::TextureFormat::R32Float], + &[ + colortex0.format, + wgpu::TextureFormat::R32Float, + wgpu::TextureFormat::R32Float, + ], ); let world_group = device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("Java shader pack world"), @@ -523,6 +631,10 @@ impl JavaStages { binding: 1, resource: wgpu::BindingResource::TextureView(world_depth), }, + wgpu::BindGroupEntry { + binding: 2, + resource: wgpu::BindingResource::TextureView(post.opaque_depth()), + }, ], }); @@ -571,6 +683,7 @@ impl JavaStages { size, targets, depth, + opaque_depth, groups, world_in: (world_pipeline, world_group), world_alt: plan.flipped_after_prepare.contains(&0), @@ -593,6 +706,7 @@ fn world_in(encoder: &mut wgpu::CommandEncoder, bound: &Bound) { let attachments = [ Some(color_attachment(side, None)), Some(color_attachment(&bound.depth, None)), + Some(color_attachment(&bound.opaque_depth, None)), ]; let mut render = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("Java shader pack world"), @@ -666,7 +780,11 @@ fn blit_pipeline( } impl Common { - fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Self { + fn new( + device: &wgpu::Device, + queue: &wgpu::Queue, + custom_map: &BTreeMap)>, + ) -> Self { let sampler = |filter, address, compare| { device.create_sampler(&wgpu::SamplerDescriptor { label: Some("Java shader pack"), @@ -714,6 +832,34 @@ impl Common { }, noise.size(), ); + let mut custom_textures = BTreeMap::new(); + for (name, (w, h, bytes)) in custom_map { + let tex = device.create_texture(&wgpu::TextureDescriptor { + label: Some(name), + size: wgpu::Extent3d { + width: *w, + height: *h, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }); + queue.write_texture( + tex.as_image_copy(), + bytes, + wgpu::TexelCopyBufferLayout { + offset: 0, + bytes_per_row: Some(*w * 4), + rows_per_image: None, + }, + tex.size(), + ); + custom_textures.insert(name.clone(), tex.create_view(&Default::default())); + } // The full-screen triangle (0, 0), (2, 0), (0, 2): the pack's // projection maps 0..1 to the screen. Texture coordinates are the // position; colours white; normals up. @@ -754,12 +900,14 @@ impl Common { repeat: sampler(Linear, Repeat, None), compare: sampler(Linear, ClampToEdge, Some(wgpu::CompareFunction::LessEqual)), noise: noise.create_view(&Default::default()), + custom_textures, vertices, world_layout: device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some("Java shader pack world"), entries: &[ texture_entry(0, unfilterable), texture_entry(1, wgpu::TextureSampleType::Depth), + texture_entry(2, wgpu::TextureSampleType::Depth), ], }), present_layout: device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { @@ -841,6 +989,7 @@ impl PassGpu { .cloned() .unwrap_or_else(|| format!("{sampler}_s")); samplers.push(SamplerBinding { + name: sampler.clone(), source: source(sampler), dimension, signed, @@ -885,7 +1034,7 @@ impl PassGpu { )), ) } - (false, Source::Depth | Source::Shadow) => ( + (false, Source::Depth | Source::OpaqueDepth | Source::Shadow) => ( wgpu::TextureSampleType::Float { filterable: false }, Some(false), ), @@ -979,10 +1128,11 @@ impl PassGpu { // Output location `l` writes target `draw_buffers[l]` (or the screen). let outputs = &program.fragment.interface.outputs; + let pass_blend = program.pass.blend.as_ref().and_then(blend_state); let target_states: Vec> = match screen { Some(format) => vec![Some(wgpu::ColorTargetState { format, - blend: None, + blend: pass_blend, write_mask: if outputs.contains_key(&0) { wgpu::ColorWrites::ALL } else { @@ -994,12 +1144,25 @@ impl PassGpu { .iter() .enumerate() .map(|(l, &i)| { + let format = formats + .get(i as usize) + .copied() + .unwrap_or(wgpu::TextureFormat::Rgba8Unorm); + // Only filterable float/unorm formats support blending. + let can_blend = matches!( + sample_type(format), + wgpu::TextureSampleType::Float { filterable: true } + ); + let buf_blend = program + .pass + .buffer_blends + .iter() + .find(|(idx, _)| *idx == i) + .map(|(_, b)| blend_state(b)) + .unwrap_or(pass_blend); Some(wgpu::ColorTargetState { - format: formats - .get(i as usize) - .copied() - .unwrap_or(wgpu::TextureFormat::Rgba8Unorm), - blend: None, + format, + blend: if can_blend { buf_blend } else { None }, write_mask: if outputs.contains_key(&(l as u32)) { wgpu::ColorWrites::ALL } else { @@ -1068,37 +1231,54 @@ impl PassGpu { } /// The pass's bind group over `targets`. + #[allow(clippy::too_many_arguments)] fn bind( &self, device: &wgpu::Device, queue: &wgpu::Queue, targets: &BTreeMap, depth: &wgpu::TextureView, + opaque_depth: &wgpu::TextureView, + post: &super::world_post::Post, common: &Common, blank: &mut Vec, ) -> wgpu::BindGroup { let mut views = Vec::new(); for s in &self.samplers { - let view = match (s.compare, s.source) { - (true, _) => blank_view(device, queue, blank, s, BlankKind::FarDepth), - (false, Source::Target(i)) if s.dimension == wgpu::TextureViewDimension::D2 => { - match targets.get(&i) { - Some(t) if self.reads_alt.contains(&i) => t.alt_view.clone(), - Some(t) => t.main_view.clone(), - None => blank_view(device, queue, blank, s, BlankKind::Color(false)), + let view = if let Some(custom) = common + .custom_textures + .get(&s.name) + .filter(|_| !s.compare && s.dimension == wgpu::TextureViewDimension::D2) + { + custom.clone() + } else { + match (s.compare, s.source) { + (true, Source::Shadow) if post.shadow_active => post.shadow_depth().clone(), + (true, _) => blank_view(device, queue, blank, s, BlankKind::FarDepth), + (false, Source::Target(i)) if s.dimension == wgpu::TextureViewDimension::D2 => { + match targets.get(&i) { + Some(t) if self.reads_alt.contains(&i) => t.alt_view.clone(), + Some(t) => t.main_view.clone(), + None => blank_view(device, queue, blank, s, BlankKind::Color(false)), + } } + (false, Source::Depth) if s.dimension == wgpu::TextureViewDimension::D2 => { + depth.clone() + } + (false, Source::OpaqueDepth) + if s.dimension == wgpu::TextureViewDimension::D2 => + { + opaque_depth.clone() + } + (false, Source::Shadow) => blank_view(device, queue, blank, s, BlankKind::Far), + (false, Source::Noise) if s.dimension == wgpu::TextureViewDimension::D2 => { + common.noise.clone() + } + (false, Source::Blank(white)) => { + blank_view(device, queue, blank, s, BlankKind::Color(white)) + } + _ => blank_view(device, queue, blank, s, BlankKind::Color(false)), } - (false, Source::Depth) if s.dimension == wgpu::TextureViewDimension::D2 => { - depth.clone() - } - (false, Source::Shadow) => blank_view(device, queue, blank, s, BlankKind::Far), - (false, Source::Noise) if s.dimension == wgpu::TextureViewDimension::D2 => { - common.noise.clone() - } - (false, Source::Blank(white)) => { - blank_view(device, queue, blank, s, BlankKind::Color(white)) - } - _ => blank_view(device, queue, blank, s, BlankKind::Color(false)), }; let sampler = match (s.compare, s.source) { _ if s.shared.is_some() => match s.shared.as_deref() { @@ -1110,7 +1290,7 @@ impl PassGpu { (true, _) => &common.compare, _ if s.signed.is_some() => &common.nearest, (false, Source::Noise) => &common.repeat, - (false, Source::Depth | Source::Shadow) => &common.nearest, + (false, Source::Depth | Source::OpaqueDepth | Source::Shadow) => &common.nearest, (false, Source::Target(i)) => { match targets.get(&i).map(|t| sample_type(t.format)) { Some(wgpu::TextureSampleType::Float { filterable: true }) | None => { @@ -1432,6 +1612,8 @@ struct Inputs { view: Mat4, /// GL's: near at -1, far at 1. projection: Mat4, + shadow_view: Mat4, + shadow_projection: Mat4, camera: Vec3, previous: (Mat4, Mat4, Vec3), size: [f32; 2], @@ -1442,6 +1624,10 @@ struct Inputs { /// `eyeBrightness` and `eyeBrightnessSmooth`: block, sky light × 16. eye: [f32; 2], eye_smooth: [f32; 2], + wetness: f32, + center_depth_smooth: f32, + ambient_occlusion_level: f32, + shadow_map_resolution: f32, /// `CelestialUniforms`, in view space. sun_position: Vec3, moon_position: Vec3, @@ -1490,6 +1676,8 @@ impl Inputs { let mut out = Self { view, projection, + shadow_view: Mat4::IDENTITY, + shadow_projection: Mat4::IDENTITY, camera, previous: previous.unwrap_or((view, projection, camera)), size: size.map(|v| v as f32), @@ -1499,6 +1687,10 @@ impl Inputs { frame: *frame, eye: [0.0; 2], eye_smooth: [0.0; 2], + wetness: frame.weather[0], + center_depth_smooth: 0.5, + ambient_occlusion_level: 1.0, + shadow_map_resolution: 2048.0, sun_position: Vec3::ZERO, moon_position: Vec3::ZERO, up_position: Vec3::ZERO, @@ -1591,27 +1783,42 @@ impl Inputs { }), "upPosition" => vec3(self.up_position), "moonPhase" => vec![f.moon[3]], - "rainStrength" | "wetness" => vec![f.weather[0]], + "rainStrength" => vec![f.weather[0]], + "wetness" => vec![self.wetness], "thunderStrength" => vec![f.weather[1]], "fogColor" => f.fog_color[..3].to_vec(), "skyColor" => f.sky_color[..3].to_vec(), "isEyeInWater" => vec![f.camera_state[0]], "nightVision" => vec![f.camera_state[1]], - "darknessFactor" => vec![f.camera_state[2]], + "darknessFactor" | "darknessLightFactor" => vec![f.camera_state[2]], + "blindness" => vec![f.camera_state[3]], "screenBrightness" => vec![1.0], + "centerDepthSmooth" => vec![self.center_depth_smooth], + "ambientOcclusionLevel" => vec![self.ambient_occlusion_level], + "shadowMapResolution" => vec![self.shadow_map_resolution], "eyeBrightness" => self.eye.to_vec(), // An ivec2: the smoothed value truncated. "eyeBrightnessSmooth" => self.eye_smooth.map(f32::trunc).to_vec(), "hasSkylight" => vec![f.dimension[1]], + "heldItemId" | "heldItemId2" | "heldBlockLightValue" | "heldBlockLightValue2" => { + vec![0.0] + } + "biome" | "biome_category" => vec![0.0], + "biome_precipitation" => vec![if f.weather[0] > 0.01 { 1.0 } else { 0.0 }], + "temp" => vec![0.8], + "rainfall" => vec![0.4], + "alphaTestRef" => vec![0.1], + "renderStage" => vec![0.0], + "playerMood" | "currentColorSpace" => vec![0.0], "iris_ModelViewMatrix" | "iris_ModelViewMatrixInverse" => mat(Mat4::IDENTITY), "iris_ProjectionMatrix" | "iris_ModelViewProjectionMatrix" => mat(screen), "iris_ProjectionMatrixInverse" => mat(screen.inverse()), "iris_TextureMatrix" => (0..8).flat_map(|_| mat(Mat4::IDENTITY)).collect(), "iris_NormalMatrix" => Mat3::IDENTITY.to_cols_array().to_vec(), - "shadowModelView" - | "shadowModelViewInverse" - | "shadowProjection" - | "shadowProjectionInverse" => mat(Mat4::IDENTITY), + "shadowModelView" => mat(self.shadow_view), + "shadowModelViewInverse" => mat(self.shadow_view.inverse()), + "shadowProjection" => mat(self.shadow_projection), + "shadowProjectionInverse" => mat(self.shadow_projection.inverse()), _ => return None, }) } @@ -1651,6 +1858,7 @@ mod tests { assert_eq!(source("colortex4"), Source::Target(4)); assert_eq!(source("gaux1"), Source::Target(4)); assert_eq!(source("gdepthtex"), Source::Depth); + assert_eq!(source("depthtex1"), Source::OpaqueDepth); assert_eq!(source("shadowtex1"), Source::Shadow); assert_eq!(source("dhDepthTex0"), Source::Shadow); assert_eq!(source("shadowcolor0"), Source::Blank(true)); diff --git a/crates/horizon-client/src/gfx/pack_stages.rs b/crates/horizon-client/src/gfx/pack_stages.rs index 87d22d07d..08a6b9aea 100644 --- a/crates/horizon-client/src/gfx/pack_stages.rs +++ b/crates/horizon-client/src/gfx/pack_stages.rs @@ -1,9 +1,12 @@ -//! A Horizon shader pack on the GPU (docs/shader-packs.md): each `composite` -//! stage is one full-screen pass that reads the world's colour and depth and -//! the previous stage's output and writes its own target; the last target -//! goes to the screen through `world_post` (copy or FXAA) under the hand and -//! HUD. Only exists while a pack is selected. -use crate::shader_pack::{ShaderPack, VERTEX_ENTRY}; +//! A Horizon shader pack on the GPU (docs/shader-packs.md): each stage +//! (`shadow`, `gbuffer`, `deferred`, `composite`, `final`) is a full-screen +//! pass that reads the world's colour, depth, opaque depth, G-buffer normals +//! and materials, shadow map, noise texture and the previous stage's output, +//! and writes its own target; the last target goes to the screen through +//! `world_post` (copy or FXAA) under the hand and HUD. Only exists while a +//! pack is selected. +use crate::shader_pack::{ShaderPack, StageKind, VERTEX_ENTRY}; +use horizon_shaderpack::uniforms::SmoothFloat; /// The `Frame` uniform of the prelude (`shader_pack_prelude.wgsl`). #[repr(C)] @@ -27,10 +30,29 @@ pub struct FrameUniform { pub dimension: [f32; 4], } +/// The API 2 `FrameExt` uniform of the prelude (`shader_pack_prelude.wgsl`). +#[repr(C)] +#[derive(Clone, Copy, Default, bytemuck::Pod, bytemuck::Zeroable)] +pub struct FrameExtUniform { + pub previous_view: [[f32; 4]; 4], + pub previous_projection: [[f32; 4]; 4], + pub shadow_view: [[f32; 4]; 4], + pub shadow_projection: [[f32; 4]; 4], + pub shadow_inverse_view: [[f32; 4]; 4], + pub shadow_inverse_projection: [[f32; 4]; 4], + pub previous_camera: [f32; 4], + pub shadow_params: [f32; 4], + pub environment: [f32; 4], +} + +const NOISE_SIZE: u32 = 256; + struct Pass { pipeline: wgpu::RenderPipeline, scale: f32, + target_format: wgpu::TextureFormat, uniform: wgpu::Buffer, + uniform_ext: wgpu::Buffer, } /// A stage's output and the bindings that read the world into it. struct Target { @@ -41,24 +63,43 @@ struct Target { pub struct PackStages { pub id: String, + pub needs_shadow: bool, + pub shadow_res: u32, + pub shadow_dist: f32, layout: wgpu::BindGroupLayout, linear: wgpu::Sampler, nearest: wgpu::Sampler, + repeat: wgpu::Sampler, + shadow_sampler: wgpu::Sampler, + noise_view: wgpu::TextureView, passes: Vec, - format: wgpu::TextureFormat, /// Built for this world target size. targets: Vec, /// `world_post`'s bindings of the last stage's output. output: Option, world_size: [u32; 2], + shadow_size: u32, started: std::time::Instant, + last: Option, frames: u32, + previous: Option<([[f32; 4]; 4], [[f32; 4]; 4], [f32; 3])>, + wetness: SmoothFloat, +} + +fn stage_format(name: Option<&str>, default: wgpu::TextureFormat) -> wgpu::TextureFormat { + match name.map(str::to_lowercase).as_deref() { + Some("rgba16float" | "rgba16f") => wgpu::TextureFormat::Rgba16Float, + Some("rgba8unorm" | "rgba8" | "rgba") => wgpu::TextureFormat::Rgba8Unorm, + Some("rgb10a2unorm" | "rgb10_a2") => wgpu::TextureFormat::Rgb10a2Unorm, + _ => default, + } } impl PackStages { /// The pipelines of a loaded (already validated) pack, or wgpu's error. pub fn new( device: &wgpu::Device, + queue: &wgpu::Queue, format: wgpu::TextureFormat, pack: &ShaderPack, ) -> Result { @@ -79,6 +120,16 @@ impl PackStages { ty: wgpu::BindingType::Sampler(ty), count: None, }; + let uniform_entry = |binding| wgpu::BindGroupLayoutEntry { + binding, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }, + count: None, + }; let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some("Shader pack stage"), entries: &[ @@ -87,16 +138,15 @@ impl PackStages { texture(2, float), sampler(3, wgpu::SamplerBindingType::Filtering), sampler(4, wgpu::SamplerBindingType::NonFiltering), - wgpu::BindGroupLayoutEntry { - binding: 5, - visibility: wgpu::ShaderStages::FRAGMENT, - ty: wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, - has_dynamic_offset: false, - min_binding_size: None, - }, - count: None, - }, + uniform_entry(5), + uniform_entry(6), + texture(7, float), + texture(8, float), + texture(9, wgpu::TextureSampleType::Depth), + texture(10, wgpu::TextureSampleType::Depth), + sampler(11, wgpu::SamplerBindingType::Comparison), + texture(12, float), + sampler(13, wgpu::SamplerBindingType::Filtering), ], }); let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { @@ -106,10 +156,18 @@ impl PackStages { }); let constants = pack.constants(); let scope = device.push_error_scope(wgpu::ErrorFilter::Validation); + let stage_count = pack.stages.len(); let passes = pack .stages .iter() - .map(|stage| { + .enumerate() + .map(|(idx, stage)| { + let is_last = idx + 1 == stage_count || stage.kind == StageKind::Final; + let target_format = if is_last { + format + } else { + stage_format(stage.format.as_deref(), format) + }; let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some(&stage.file), source: wgpu::ShaderSource::Wgsl(stage.source.as_str().into()), @@ -140,7 +198,7 @@ impl PackStages { ..Default::default() }, targets: &[Some(wgpu::ColorTargetState { - format, + format: target_format, blend: None, write_mask: wgpu::ColorWrites::ALL, })], @@ -151,38 +209,110 @@ impl PackStages { Pass { pipeline, scale: stage.scale, + target_format, uniform: device.create_buffer(&wgpu::BufferDescriptor { label: Some("Shader pack frame"), size: std::mem::size_of::() as u64, usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }), + uniform_ext: device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Shader pack frame ext"), + size: std::mem::size_of::() as u64, + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }), } }) .collect(); if let Some(error) = pollster::block_on(scope.pop()) { return Err(error.to_string()); } - let sampler = |filter| { + let make_sampler = |filter, address, compare| { device.create_sampler(&wgpu::SamplerDescriptor { label: Some("Shader pack"), + address_mode_u: address, + address_mode_v: address, + address_mode_w: address, mag_filter: filter, min_filter: filter, + compare, ..Default::default() }) }; + let (nw, nh, noise_bytes) = match &pack.custom_noise { + Some((w, h, bytes)) => (*w, *h, bytes.clone()), + None => { + let mut seed = 0x2545_f491_u32; + let bytes: Vec = (0..NOISE_SIZE * NOISE_SIZE * 4) + .map(|i| { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + if i % 4 == 3 { 255 } else { (seed >> 24) as u8 } + }) + .collect(); + (NOISE_SIZE, NOISE_SIZE, bytes) + } + }; + let noise_tex = device.create_texture(&wgpu::TextureDescriptor { + label: Some("Shader pack noise"), + size: wgpu::Extent3d { + width: nw, + height: nh, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }); + queue.write_texture( + noise_tex.as_image_copy(), + &noise_bytes, + wgpu::TexelCopyBufferLayout { + offset: 0, + bytes_per_row: Some(nw * 4), + rows_per_image: None, + }, + noise_tex.size(), + ); + let (shadow_res, shadow_dist, _) = pack.shadow_config(0); Ok(Self { id: pack.id.clone(), + needs_shadow: pack.needs_shadow(), + shadow_res, + shadow_dist, layout, - linear: sampler(wgpu::FilterMode::Linear), - nearest: sampler(wgpu::FilterMode::Nearest), + linear: make_sampler( + wgpu::FilterMode::Linear, + wgpu::AddressMode::ClampToEdge, + None, + ), + nearest: make_sampler( + wgpu::FilterMode::Nearest, + wgpu::AddressMode::ClampToEdge, + None, + ), + repeat: make_sampler(wgpu::FilterMode::Linear, wgpu::AddressMode::Repeat, None), + shadow_sampler: make_sampler( + wgpu::FilterMode::Linear, + wgpu::AddressMode::ClampToEdge, + Some(wgpu::CompareFunction::GreaterEqual), + ), + noise_view: noise_tex.create_view(&Default::default()), passes, - format, targets: Vec::new(), output: None, world_size: [0, 0], + shadow_size: 0, started: std::time::Instant::now(), + last: None, frames: 0, + previous: None, + wetness: SmoothFloat::default(), }) } @@ -199,9 +329,14 @@ impl PackStages { size: [u32; 2], post: &super::world_post::Post, mut uniform: FrameUniform, + eye_light: [u8; 2], ) -> &wgpu::BindGroup { - if self.world_size != size || self.targets.len() != self.passes.len() { + if self.world_size != size + || self.shadow_size != post.shadow_resolution + || self.targets.len() != self.passes.len() + { self.world_size = size; + self.shadow_size = post.shadow_resolution; self.targets.clear(); for pass in &self.passes { let target_size = size.map(|v| ((v as f32 * pass.scale).round() as u32).max(1)); @@ -216,7 +351,7 @@ impl PackStages { mip_level_count: 1, sample_count: 1, dimension: wgpu::TextureDimension::D2, - format: self.format, + format: pass.target_format, usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, view_formats: &[], @@ -251,6 +386,38 @@ impl PackStages { binding: 5, resource: pass.uniform.as_entire_binding(), }, + wgpu::BindGroupEntry { + binding: 6, + resource: pass.uniform_ext.as_entire_binding(), + }, + wgpu::BindGroupEntry { + binding: 7, + resource: wgpu::BindingResource::TextureView(post.normal()), + }, + wgpu::BindGroupEntry { + binding: 8, + resource: wgpu::BindingResource::TextureView(post.material()), + }, + wgpu::BindGroupEntry { + binding: 9, + resource: wgpu::BindingResource::TextureView(post.opaque_depth()), + }, + wgpu::BindGroupEntry { + binding: 10, + resource: wgpu::BindingResource::TextureView(post.shadow_depth()), + }, + wgpu::BindGroupEntry { + binding: 11, + resource: wgpu::BindingResource::Sampler(&self.shadow_sampler), + }, + wgpu::BindGroupEntry { + binding: 12, + resource: wgpu::BindingResource::TextureView(&self.noise_view), + }, + wgpu::BindGroupEntry { + binding: 13, + resource: wgpu::BindingResource::Sampler(&self.repeat), + }, ], }); self.targets.push(Target { @@ -262,6 +429,42 @@ impl PackStages { let last = &self.targets.last().expect("a pack has a stage").view; self.output = Some(post.bind(device, last)); } + let now = std::time::Instant::now(); + let dt = self.last.map_or(0.0, |t| (now - t).as_secs_f32()); + self.last = Some(now); + let wet = self.wetness.update(uniform.weather[0], 60.0, 120.0, dt); + let cam = [ + uniform.camera_position[0], + uniform.camera_position[1], + uniform.camera_position[2], + ]; + let (prev_view, prev_proj, prev_cam) = + self.previous + .unwrap_or((uniform.view, uniform.projection, cam)); + self.previous = Some((uniform.view, uniform.projection, cam)); + let shadow_view = post.shadow_view; + let shadow_proj = post.shadow_proj; + let ext = FrameExtUniform { + previous_view: prev_view, + previous_projection: prev_proj, + shadow_view: shadow_view.to_cols_array_2d(), + shadow_projection: shadow_proj.to_cols_array_2d(), + shadow_inverse_view: shadow_view.inverse().to_cols_array_2d(), + shadow_inverse_projection: shadow_proj.inverse().to_cols_array_2d(), + previous_camera: [prev_cam[0], prev_cam[1], prev_cam[2], dt], + shadow_params: [ + post.shadow_resolution as f32, + post.shadow_distance, + 0.0, + if post.shadow_active { 1.0 } else { 0.0 }, + ], + environment: [ + f32::from(eye_light[0]) * 16.0, + f32::from(eye_light[1]) * 16.0, + wet, + uniform.view_params[2] * 0.5, + ], + }; uniform.time[0] = self.started.elapsed().as_secs_f32(); uniform.time[1] = self.frames as f32; self.frames = self.frames.wrapping_add(1); @@ -269,6 +472,7 @@ impl PackStages { let [w, h] = target.size.map(|v| v as f32); uniform.resolution = [w, h, 1.0 / w, 1.0 / h]; queue.write_buffer(&pass.uniform, 0, bytemuck::bytes_of(&uniform)); + queue.write_buffer(&pass.uniform_ext, 0, bytemuck::bytes_of(&ext)); let attachments = [Some(wgpu::RenderPassColorAttachment { view: &target.view, resolve_target: None, diff --git a/crates/horizon-client/src/gfx/world_post.rs b/crates/horizon-client/src/gfx/world_post.rs index 797ee03b7..69d95aae6 100644 --- a/crates/horizon-client/src/gfx/world_post.rs +++ b/crates/horizon-client/src/gfx/world_post.rs @@ -1,26 +1,148 @@ -//! Resolution scale and FXAA (Horizon settings): the world renders into its -//! own target at the scaled size, then one full-screen pass copies or -//! anti-aliases it onto the screen, under the hand and HUD. A shader pack -//! also needs the target (its stages read the world's colour and depth). +//! Resolution scale, FXAA, and shader-pack G-buffer/shadow targets (Horizon +//! settings): the world renders into its own target at the scaled size, then +//! one full-screen pass copies or anti-aliases it onto the screen, under the +//! hand and HUD. When a shader pack is active, `Post` also maintains: +//! - `opaque_depth` (`depthtex1`/`depthtex2`): depth captured before +//! translucent terrain (water, stained glass, ice) renders; +//! - `normal` (`colortex1`/`scene_normal`) and `material` +//! (`colortex2`/`scene_material`): G-buffer surface normals and material +//! properties reconstructed from depth and scene colour; +//! - `shadow_depth` (`shadowtex0`/`shadowtex1`): the directional sun/moon +//! shadow map and its light-space `shadow_view` and `shadow_proj` matrices. +//! //! With none of them on, the world renders straight to the screen as before. +use super::pack_stages::FrameUniform; +use glam::{Mat4, Vec3, Vec4}; + +const GBUFFER_WGSL: &str = r#" +struct GbufferParams { + inverse_view: mat4x4, + inverse_projection: mat4x4, + resolution: vec4, + // x: eye block light (0..1), y: eye sky light (0..1), z: wetness (0..1), w: sun elevation + env: vec4, +} + +@group(0) @binding(0) var world_color: texture_2d; +@group(0) @binding(1) var world_depth: texture_depth_2d; +@group(0) @binding(2) var opaque_depth: texture_depth_2d; +@group(0) @binding(3) var nearest_sampler: sampler; +@group(0) @binding(4) var params: GbufferParams; + +struct VsOut { + @builtin(position) clip: vec4, + @location(0) uv: vec2, +} + +@vertex +fn vs_gbuffer(@builtin(vertex_index) i: u32) -> VsOut { + let p = vec2(f32((i << 1u) & 2u), f32(i & 2u)); + var out: VsOut; + out.clip = vec4(p * 2.0 - 1.0, 0.0, 1.0); + out.uv = vec2(p.x, 1.0 - p.y); + return out; +} + +struct GbufferOut { + @location(0) normal: vec4, + @location(1) material: vec4, +} + +fn world_pos(uv: vec2, d: f32) -> vec3 { + let v = params.inverse_projection * vec4(uv.x * 2.0 - 1.0, 1.0 - uv.y * 2.0, d, 1.0); + let view = v.xyz / max(v.w, 1e-6); + return (params.inverse_view * vec4(view, 1.0)).xyz; +} + +@fragment +fn fs_gbuffer(in: VsOut) -> GbufferOut { + let d_all = textureSampleLevel(world_depth, nearest_sampler, in.uv, 0); + let d_opq = textureSampleLevel(opaque_depth, nearest_sampler, in.uv, 0); + let color = textureSampleLevel(world_color, nearest_sampler, in.uv, 0.0); + var out: GbufferOut; + if d_all <= 0.0 && d_opq <= 0.0 { + out.normal = vec4(0.5, 1.0, 0.5, 0.0); + out.material = vec4(0.0, params.env.y, 0.0, 1.0); + return out; + } + let d = select(d_all, d_opq, d_opq > 0.0); + let step_uv = params.resolution.zw; + let p0 = world_pos(in.uv, d); + let uv_r = clamp(in.uv + vec2(step_uv.x, 0.0), vec2(0.0), vec2(1.0)); + let uv_l = clamp(in.uv - vec2(step_uv.x, 0.0), vec2(0.0), vec2(1.0)); + let uv_d = clamp(in.uv + vec2(0.0, step_uv.y), vec2(0.0), vec2(1.0)); + let uv_u = clamp(in.uv - vec2(0.0, step_uv.y), vec2(0.0), vec2(1.0)); + let dr = textureSampleLevel(opaque_depth, nearest_sampler, uv_r, 0); + let dl = textureSampleLevel(opaque_depth, nearest_sampler, uv_l, 0); + let dd = textureSampleLevel(opaque_depth, nearest_sampler, uv_d, 0); + let du = textureSampleLevel(opaque_depth, nearest_sampler, uv_u, 0); + let dx = select(world_pos(uv_r, dr) - p0, p0 - world_pos(uv_l, dl), abs(dl - d) < abs(dr - d)); + let dy = select(world_pos(uv_d, dd) - p0, p0 - world_pos(uv_u, du), abs(du - d) < abs(dd - d)); + let raw_n = cross(dy, dx); + let n_len = length(raw_n); + let n = select(vec3(0.0, 1.0, 0.0), raw_n / max(n_len, 1e-6), n_len > 1e-6); + // Reversed depth: closer translucent geometry has larger depth than opaque geometry. + let is_translucent = select(0.0, 1.0, d_all > d_opq + 1e-5); + let luma = dot(color.rgb, vec3(0.2126, 0.7152, 0.0722)); + let block_light = clamp(max(params.env.x, smoothstep(0.65, 0.98, luma)), 0.0, 1.0); + let roughness = select(clamp(0.85 - params.env.z * 0.45, 0.1, 1.0), 0.05, is_translucent > 0.5); + out.normal = vec4(n * 0.5 + 0.5, 1.0); + out.material = vec4(block_light, params.env.y, is_translucent, roughness); + return out; +} +"#; + +#[repr(C)] +#[derive(Clone, Copy, Default, bytemuck::Pod, bytemuck::Zeroable)] +struct GbufferUniform { + inverse_view: [[f32; 4]; 4], + inverse_projection: [[f32; 4]; 4], + resolution: [f32; 4], + env: [f32; 4], +} + struct WorldTarget { size: [u32; 2], color: wgpu::TextureView, + depth_tex: wgpu::Texture, depth: wgpu::TextureView, + opaque_depth_tex: wgpu::Texture, + opaque_depth: wgpu::TextureView, + normal: wgpu::TextureView, + material: wgpu::TextureView, + gbuffer_group: wgpu::BindGroup, group: wgpu::BindGroup, + opaque_captured: bool, +} + +struct ShadowTarget { + resolution: u32, + depth: wgpu::TextureView, } pub struct Post { layout: wgpu::BindGroupLayout, sampler: wgpu::Sampler, + nearest: wgpu::Sampler, copy: wgpu::RenderPipeline, fxaa: wgpu::RenderPipeline, + gbuffer_layout: wgpu::BindGroupLayout, + gbuffer_pipeline: wgpu::RenderPipeline, + gbuffer_uniform: wgpu::Buffer, format: wgpu::TextureFormat, /// World resolution relative to the window (0.5..=2.0). pub scale: f32, pub antialias: bool, target: Option, + shadow: Option, + fallback_shadow: wgpu::TextureView, + fallback_color: wgpu::TextureView, + pub shadow_view: Mat4, + pub shadow_proj: Mat4, + pub shadow_distance: f32, + pub shadow_resolution: u32, + pub shadow_active: bool, } impl Post { @@ -88,17 +210,169 @@ impl Post { min_filter: wgpu::FilterMode::Linear, ..Default::default() }); + let nearest = device.create_sampler(&wgpu::SamplerDescriptor { + label: Some("Horizon post nearest"), + mag_filter: wgpu::FilterMode::Nearest, + min_filter: wgpu::FilterMode::Nearest, + ..Default::default() + }); + + let gbuffer_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("Horizon gbuffer"), + source: wgpu::ShaderSource::Wgsl(GBUFFER_WGSL.into()), + }); + let gbuffer_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("Horizon gbuffer layout"), + entries: &[ + wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Float { filterable: true }, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 1, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Depth, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 2, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Depth, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 3, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::NonFiltering), + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 4, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }, + count: None, + }, + ], + }); + let gbuffer_pipe_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("Horizon gbuffer"), + bind_group_layouts: &[Some(&gbuffer_layout)], + ..Default::default() + }); + let gbuffer_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("Horizon gbuffer"), + layout: Some(&gbuffer_pipe_layout), + vertex: wgpu::VertexState { + module: &gbuffer_shader, + entry_point: Some("vs_gbuffer"), + compilation_options: Default::default(), + buffers: &[], + }, + primitive: Default::default(), + depth_stencil: None, + multisample: Default::default(), + fragment: Some(wgpu::FragmentState { + module: &gbuffer_shader, + entry_point: Some("fs_gbuffer"), + compilation_options: Default::default(), + targets: &[ + Some(wgpu::ColorTargetState { + format: wgpu::TextureFormat::Rgba8Unorm, + blend: None, + write_mask: wgpu::ColorWrites::ALL, + }), + Some(wgpu::ColorTargetState { + format: wgpu::TextureFormat::Rgba8Unorm, + blend: None, + write_mask: wgpu::ColorWrites::ALL, + }), + ], + }), + multiview_mask: None, + cache: None, + }); + let gbuffer_uniform = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Horizon gbuffer uniform"), + size: std::mem::size_of::() as u64, + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + let fallback_shadow = device + .create_texture(&wgpu::TextureDescriptor { + label: Some("Horizon fallback shadow"), + size: wgpu::Extent3d { + width: 1, + height: 1, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Depth32Float, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }) + .create_view(&Default::default()); + let fallback_color = device + .create_texture(&wgpu::TextureDescriptor { + label: Some("Horizon fallback gbuffer"), + size: wgpu::Extent3d { + width: 1, + height: 1, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }) + .create_view(&Default::default()); Self { copy: pipeline("fs_copy"), fxaa: pipeline("fs_fxaa"), layout, sampler, + nearest, + gbuffer_layout, + gbuffer_pipeline, + gbuffer_uniform, format, scale: 1.0, antialias: false, target: None, + shadow: None, + fallback_shadow, + fallback_color, + shadow_view: Mat4::IDENTITY, + shadow_proj: Mat4::IDENTITY, + shadow_distance: 128.0, + shadow_resolution: 2048, + shadow_active: false, } } + /// The world's colour and depth targets this frame, or `None` to draw /// it straight to the screen. `pack`: a shader pack runs this frame. pub fn prepare( @@ -109,6 +383,7 @@ impl Post { ) -> Option<(wgpu::TextureView, wgpu::TextureView)> { if self.scale == 1.0 && !self.antialias && !pack { self.target = None; + self.shadow_active = false; return None; } let size = window.map(|v| ((v as f32 * self.scale).round() as u32).clamp(1, 8192)); @@ -118,46 +393,284 @@ impl Post { height: size[1], depth_or_array_layers: 1, }; - let texture = |label, format, usage| { - device - .create_texture(&wgpu::TextureDescriptor { - label: Some(label), - size: extent, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format, - usage, - view_formats: &[], - }) - .create_view(&Default::default()) + let make_tex = |label, format, usage| { + device.create_texture(&wgpu::TextureDescriptor { + label: Some(label), + size: extent, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage, + view_formats: &[], + }) }; - let color = texture( + let color = make_tex( "Scaled world", self.format, wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, - ); - let depth = texture( + ) + .create_view(&Default::default()); + let depth_tex = make_tex( "Scaled world depth", wgpu::TextureFormat::Depth32Float, - wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, + wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TEXTURE_BINDING + | wgpu::TextureUsages::COPY_SRC, ); + let depth = depth_tex.create_view(&Default::default()); + let opaque_depth_tex = make_tex( + "Opaque world depth", + wgpu::TextureFormat::Depth32Float, + wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TEXTURE_BINDING + | wgpu::TextureUsages::COPY_DST, + ); + let opaque_depth = opaque_depth_tex.create_view(&Default::default()); + let normal = make_tex( + "World G-buffer normal", + wgpu::TextureFormat::Rgba8Unorm, + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, + ) + .create_view(&Default::default()); + let material = make_tex( + "World G-buffer material", + wgpu::TextureFormat::Rgba8Unorm, + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, + ) + .create_view(&Default::default()); + let gbuffer_group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Horizon gbuffer"), + layout: &self.gbuffer_layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::TextureView(&color), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::TextureView(&depth), + }, + wgpu::BindGroupEntry { + binding: 2, + resource: wgpu::BindingResource::TextureView(&opaque_depth), + }, + wgpu::BindGroupEntry { + binding: 3, + resource: wgpu::BindingResource::Sampler(&self.nearest), + }, + wgpu::BindGroupEntry { + binding: 4, + resource: self.gbuffer_uniform.as_entire_binding(), + }, + ], + }); let group = self.bind(device, &color); self.target = Some(WorldTarget { size, color, + depth_tex, depth, + opaque_depth_tex, + opaque_depth, + normal, + material, + gbuffer_group, group, + opaque_captured: false, }); } + if let Some(t) = &mut self.target { + t.opaque_captured = false; + } self.target .as_ref() .map(|t| (t.color.clone(), t.depth.clone())) } + + /// Prepare the directional sun/moon shadow map target and compute its + /// camera-relative orthographic `shadow_view` and `shadow_proj` matrices. + pub fn prepare_shadow( + &mut self, + device: &wgpu::Device, + resolution: u32, + distance: f32, + sun_dir: Vec3, + moon_dir: Vec3, + ) -> &wgpu::TextureView { + let resolution = resolution.clamp(256, 4096); + let distance = distance.clamp(16.0, 512.0); + if self + .shadow + .as_ref() + .is_none_or(|s| s.resolution != resolution) + { + let depth = device + .create_texture(&wgpu::TextureDescriptor { + label: Some("Horizon shadow map"), + size: wgpu::Extent3d { + width: resolution, + height: resolution, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Depth32Float, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }) + .create_view(&Default::default()); + self.shadow = Some(ShadowTarget { resolution, depth }); + } + let light_dir = if sun_dir.y >= -0.05 { + sun_dir.normalize_or_zero() + } else { + moon_dir.normalize_or_zero() + }; + let dir = if light_dir.length_squared() > 1e-4 { + light_dir + } else { + Vec3::Y + }; + let up = if dir.y.abs() > 0.99 { Vec3::Z } else { Vec3::Y }; + self.shadow_view = Mat4::look_at_rh(dir * distance, Vec3::ZERO, up); + // Reversed-depth orthographic projection for the world's GreaterEqual + // pipelines (near maps to 1.0, far maps to 0.0), and standard 0..1 + // projection for shader sampling. + self.shadow_proj = Mat4::orthographic_rh( + -distance, + distance, + -distance, + distance, + distance * 2.5, + 0.05, + ); + self.shadow_distance = distance; + self.shadow_resolution = resolution; + self.shadow_active = true; + &self.shadow.as_ref().expect("created above").depth + } + + /// Snapshot `world_depth` into `opaque_depth` (`depthtex1`/`depthtex2`) + /// right before translucent water and weather draw. + pub fn capture_opaque_depth(&mut self, encoder: &mut wgpu::CommandEncoder) { + let Some(t) = &mut self.target else { return }; + encoder.copy_texture_to_texture( + t.depth_tex.as_image_copy(), + t.opaque_depth_tex.as_image_copy(), + t.depth_tex.size(), + ); + t.opaque_captured = true; + } + + /// Reconstruct the G-buffer `normal` and `material` textures after the + /// world pass finishes. + pub fn build_gbuffer( + &mut self, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + frame: &FrameUniform, + eye_light: [u8; 2], + wetness: f32, + ) { + let Some(t) = &mut self.target else { return }; + if !t.opaque_captured { + encoder.copy_texture_to_texture( + t.depth_tex.as_image_copy(), + t.opaque_depth_tex.as_image_copy(), + t.depth_tex.size(), + ); + t.opaque_captured = true; + } + let [w, h] = t.size.map(|v| v.max(1) as f32); + let uniform = GbufferUniform { + inverse_view: frame.inverse_view, + inverse_projection: frame.inverse_projection, + resolution: [w, h, 1.0 / w, 1.0 / h], + env: [ + f32::from(eye_light[0]) / 15.0, + f32::from(eye_light[1]) / 15.0, + wetness, + frame.sun[1], + ], + }; + queue.write_buffer(&self.gbuffer_uniform, 0, bytemuck::bytes_of(&uniform)); + let attachments = [ + Some(wgpu::RenderPassColorAttachment { + view: &t.normal, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::BLACK), + store: wgpu::StoreOp::Store, + }, + }), + Some(wgpu::RenderPassColorAttachment { + view: &t.material, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::BLACK), + store: wgpu::StoreOp::Store, + }, + }), + ]; + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Horizon gbuffer"), + color_attachments: &attachments, + ..Default::default() + }); + pass.set_pipeline(&self.gbuffer_pipeline); + pass.set_bind_group(0, &t.gbuffer_group, &[]); + pass.draw(0..3, 0..1); + } + /// The world target's size, once `prepare` made one. pub fn size(&self) -> Option<[u32; 2]> { self.target.as_ref().map(|t| t.size) } + + /// Pre-translucent depth view (`depthtex1`/`depthtex2` / `opaque_depth`). + pub fn opaque_depth(&self) -> &wgpu::TextureView { + self.target + .as_ref() + .map_or(&self.fallback_shadow, |t| &t.opaque_depth) + } + + /// Reconstructed world-space normal G-buffer view (`colortex1` / `scene_normal`). + pub fn normal(&self) -> &wgpu::TextureView { + self.target + .as_ref() + .map_or(&self.fallback_color, |t| &t.normal) + } + + /// Reconstructed material/lightmap G-buffer view (`colortex2` / `scene_material`). + pub fn material(&self) -> &wgpu::TextureView { + self.target + .as_ref() + .map_or(&self.fallback_color, |t| &t.material) + } + + /// Directional sun/moon shadow map view (`shadowtex0`/`shadowtex1` / `shadow_depth`). + pub fn shadow_depth(&self) -> &wgpu::TextureView { + self.shadow + .as_ref() + .map_or(&self.fallback_shadow, |s| &s.depth) + } + + /// OpenGL-convention (-1..1 Z) shadow projection for Java/Iris shader packs. + pub fn gl_shadow_projection(&self) -> Mat4 { + let to_gl = Mat4::from_cols( + Vec4::X, + Vec4::Y, + Vec4::new(0.0, 0.0, -2.0, 0.0), + Vec4::new(0.0, 0.0, 1.0, 1.0), + ); + to_gl * self.shadow_proj + } + /// Bindings that copy (or anti-alias) `view` onto the screen in `finish_with`. pub fn bind(&self, device: &wgpu::Device, view: &wgpu::TextureView) -> wgpu::BindGroup { device.create_bind_group(&wgpu::BindGroupDescriptor { @@ -175,11 +688,13 @@ impl Post { ], }) } + /// Draw the scaled world onto `screen`. pub fn finish(&self, encoder: &mut wgpu::CommandEncoder, screen: &wgpu::TextureView) { let Some(target) = &self.target else { return }; self.finish_with(encoder, screen, &target.group); } + /// Draw what `group` binds (`bind`) onto `screen`. pub fn finish_with( &self, diff --git a/crates/horizon-client/src/graphics_ui.rs b/crates/horizon-client/src/graphics_ui.rs index 514e64a88..fe797af3e 100644 --- a/crates/horizon-client/src/graphics_ui.rs +++ b/crates/horizon-client/src/graphics_ui.rs @@ -112,13 +112,18 @@ pub fn rows(level: Level) -> Vec { "Waits for the display's refresh: no tearing, a little latency.", ); let gui = v(Opt::GuiScale, None, "Size of menus and the HUD."); + let shader_pack = h( + H::ShaderPack, + High, + "Redraws the world with a pack from the shaderpacks folder. Off is vanilla.", + ); let preset = h( H::Preset, None, "Sets the options below together. Changing one makes it Custom.", ); let mut rows = match level { - Level::Simple => vec![preset, render, lod, far, fov, fps, vsync, gui], + Level::Simple => vec![preset, render, lod, far, fov, fps, vsync, gui, shader_pack], Level::Advanced => vec![ Row::Header("Rendering"), render, @@ -246,11 +251,7 @@ pub fn rows(level: Level) -> Vec { None, "Lowers the frame rate when minimised, or idle too.", ), - h( - H::ShaderPack, - High, - "Redraws the world with a pack from the shaderpacks folder. Off is vanilla.", - ), + shader_pack, Row::Header("Preferences"), v(Opt::ViewBobbing, None, "Sways the view while walking."), v(Opt::ChunkFade, None, "Fades newly loaded chunks in."), diff --git a/crates/horizon-client/src/java_pack.rs b/crates/horizon-client/src/java_pack.rs index a5bc2904e..5a37aa223 100644 --- a/crates/horizon-client/src/java_pack.rs +++ b/crates/horizon-client/src/java_pack.rs @@ -1,9 +1,9 @@ //! Java (OptiFine/Iris) shader packs in `shaderpacks/` (docs/shader-packs.md): //! a folder holding `shaders/`, or a `.zip`. `horizon-shaderpack` translates //! their full-screen programs (`begin`, `prepare`, `deferred`, `composite`, -//! `final`) to WGSL, and `gfx/java_stages.rs` runs them over Horizon's world -//! as Iris runs them over its own. Geometry programs (`gbuffers_*`, -//! `shadow`) don't run yet: the world is drawn by Horizon. +//! `final`) to WGSL, and `gfx/java_stages.rs` runs them over Horizon's world, +//! G-buffers, pre-translucent depth (`depthtex1`) and directional shadow map +//! (`shadowtex0`/`shadowtex1`) as Iris runs them over its own. use horizon_shaderpack::{ JavaPack, pipeline::{Directives, Program, TargetSettings}, @@ -30,8 +30,10 @@ pub struct JavaFolder { pub final_pass: Option, /// The targets whose alternate side holds the frame's last write. pub flipped_at_end: BTreeSet, - /// `sunPathRotation` and `eyeBrightnessHalflife`. + /// `sunPathRotation`, `eyeBrightnessHalflife`, shadow and wetness directives. pub directives: Directives, + /// Whether this dimension's passes read directional shadow maps. + pub shadow_enabled: bool, } /// A loaded Java pack. @@ -41,6 +43,8 @@ pub struct JavaPasses { pub uniforms: CustomUniforms, /// What the pack keeps of the vanilla sky (`clouds`, `sun`, `moon`). pub sky: SkyDirectives, + /// Decoded RGBA8 custom textures (`sampler_name` → `(width, height, rgba)`). + pub custom_textures: BTreeMap)>, } impl JavaPasses { @@ -131,11 +135,25 @@ pub fn load( final_pass, flipped_at_end: plan.flipped_at_end, directives: plan.directives, + shadow_enabled: plan.shadow_enabled, }); } if folders.is_empty() { return Err("no deferred, composite or final programs".into()); } + let mut custom_textures = BTreeMap::new(); + for tex in properties.custom_textures() { + if let Some(bytes) = pack.files.read_bytes(&tex.path) { + match crate::shader_pack::decode_png_rgba(&bytes) { + Ok(decoded) => { + custom_textures.insert(tex.sampler, decoded); + } + Err(e) => { + log::warn!("Horizon: shader pack texture {}: {e}", tex.path); + } + } + } + } let uniforms = properties.custom_uniforms(); for problem in &uniforms.problems { log::warn!("Horizon: shader pack custom uniform {problem}"); @@ -144,6 +162,7 @@ pub fn load( folders, uniforms, sky: properties.sky(), + custom_textures, }) } diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs index 7937958ea..4ae78da40 100644 --- a/crates/horizon-client/src/options_ui.rs +++ b/crates/horizon-client/src/options_ui.rs @@ -864,6 +864,8 @@ pub enum Page { Mods, /// The shader packs in `shaderpacks/`: pick one, open the folder. ShaderPacks, + /// Interactive options and profiles for the active shader pack. + ShaderPackOptions, /// Horizon's graphics settings (`graphics_ui`). Graphics, GraphicsAdvanced, @@ -901,6 +903,13 @@ enum Act { /// Credits & Attribution's Credits button (WinScreen without the poem). Credits, CopyAgentDetails, + /// Pick and import a shader pack `.zip` or folder into `shaderpacks/`. + ImportShaderPack, + ShaderPackProfile, + ShaderPackSubscreen(String), + ShaderPackBackScreen, + ShaderPackOption(usize), + ResetShaderPackOptions, } enum WKind { Button { @@ -913,6 +922,11 @@ enum WKind { value: f32, opt: Opt, }, + ShaderOptionSlider { + label: Text, + value: f32, + index: usize, + }, Text { text: Text, color: u32, @@ -954,6 +968,10 @@ pub enum Event { Credits, /// Copy how an agent connects (the AI Agent page). CopyAgentDetails, + /// Pick and import a shader pack `.zip` or folder into `shaderpacks/`. + ImportShaderPack, + /// Reload the active shader pack after its options file changed. + ReloadShaderPack, } /// `SliderableValueSet.applyValueImmediately`: these apply 600 ms after /// the last change (`OptionInstanceSliderButton.delayedApplyAt`). @@ -1011,6 +1029,12 @@ pub struct OptionsUi { pub shader_pack_error: Option<(String, String)>, /// The AI Agent page's status lines (set by the game while it shows). pub agent_status: Vec, + /// Loaded interactive configuration for the currently selected shader pack. + pub shader_pack_config: Option, + /// Classic shader option slider currently being dragged. + pub(crate) dragging_shader_option: Option, + /// Horizon shader option slider currently being dragged. + pub(crate) hdragging_shader_option: Option, } impl Default for OptionsUi { fn default() -> Self { @@ -1042,6 +1066,9 @@ impl Default for OptionsUi { shader_packs: Default::default(), shader_pack_error: None, agent_status: Vec::new(), + shader_pack_config: None, + dragging_shader_option: None, + hdragging_shader_option: None, } } } @@ -1172,7 +1199,7 @@ impl OptionsUi { } /// Pick shader pack `i` of the folder, or turn it off when it is the /// one running. - pub(crate) fn toggle_shader_pack(&self, s: &mut Settings, i: usize) { + pub(crate) fn toggle_shader_pack(&mut self, s: &mut Settings, i: usize) { let Some((id, Ok(_))) = self.shader_packs.packs.get(i) else { return; }; @@ -1182,6 +1209,39 @@ impl OptionsUi { } else { id.clone() }; + self.sync_shader_pack_config(s); + } + /// Ensure `shader_pack_config` matches `s.horizon.shader_pack`. + pub fn sync_shader_pack_config(&mut self, s: &Settings) { + let id = s.horizon.shader_pack.as_str(); + if id.is_empty() { + self.shader_pack_config = None; + return; + } + if self + .shader_pack_config + .as_ref() + .is_some_and(|cfg| cfg.id == id) + { + return; + } + let dir = if self.shader_packs.dir.as_os_str().is_empty() { + crate::shader_pack::folder() + } else { + self.shader_packs.dir.clone() + }; + self.shader_pack_config = crate::shader_pack::PackConfig::load(&dir, id).ok(); + } + /// Save the active shader pack options to `shaderpacks/.txt`. + pub(crate) fn save_shader_pack_config(&self) { + if let Some(cfg) = &self.shader_pack_config { + let dir = if self.shader_packs.dir.as_os_str().is_empty() { + crate::shader_pack::folder() + } else { + self.shader_packs.dir.clone() + }; + let _ = cfg.save(&dir); + } } pub(crate) fn shader_pack_failed(&self, id: &str) -> bool { self.shader_pack_error @@ -1215,6 +1275,7 @@ impl OptionsUi { Page::Packs => "resourcePack.title", Page::Mods => "Mods", Page::ShaderPacks => "Shader Packs", + Page::ShaderPackOptions => "Shader Pack Settings", Page::Graphics | Page::GraphicsAdvanced => "options.videoTitle", Page::Controller => "Controller Settings", Page::PadBinds => "Controller Binds", @@ -1587,6 +1648,121 @@ impl OptionsUi { None, )); } + let has_settings = !s.horizon.shader_pack.is_empty() + && self.shader_pack_config.as_ref().is_some_and(|c| { + c.id == s.horizon.shader_pack + && (!c.options.is_empty() || !c.profiles.is_empty()) + }); + if has_settings { + rows.push(Row::Buttons( + button( + 0.0, + 0.0, + 150.0, + Text::literal("Shader Pack Settings..."), + Act::Push(Page::ShaderPackOptions), + ), + Some(button( + 0.0, + 0.0, + 150.0, + Text::literal("Import Shader Pack..."), + Act::ImportShaderPack, + )), + )); + } else { + rows.push(Row::Buttons( + button( + 0.0, + 0.0, + 310.0, + Text::literal("Import Shader Pack..."), + Act::ImportShaderPack, + ), + None, + )); + } + rows + } + Page::ShaderPackOptions => { + let Some(cfg) = &self.shader_pack_config else { + return vec![Row::Header("No configurable options for this shader pack")]; + }; + let screen = cfg.current_screen(); + let mut rows = vec![Row::Note(format!("{} — {}", cfg.id, screen.label))]; + if cfg.active_screen.is_some() { + rows.push(Row::Buttons( + button( + 0.0, + 0.0, + 310.0, + Text::literal("Back to Main Screen"), + Act::ShaderPackBackScreen, + ), + None, + )); + } + let mut widgets = Vec::new(); + for item in &screen.items { + match item { + crate::shader_pack::ScreenItem::Empty => {} + crate::shader_pack::ScreenItem::Profile => { + let label = + Text::literal(format!("Profile: {}", cfg.active_profile_label())); + widgets.push(button(0.0, 0.0, 150.0, label, Act::ShaderPackProfile)); + } + crate::shader_pack::ScreenItem::Subscreen(sub, title) => { + let label = Text::literal(format!("{title}...")); + widgets.push(button( + 0.0, + 0.0, + 150.0, + label, + Act::ShaderPackSubscreen(sub.clone()), + )); + } + crate::shader_pack::ScreenItem::Option(idx) => { + if let Some(opt) = cfg.options.get(*idx) { + let label = Text::literal(format!( + "{}: {}", + opt.label, + opt.display_value() + )); + if let Some(value) = opt.slider_pos() { + widgets.push(Widget { + x: 0.0, + y: 0.0, + w: 150.0, + h: 20.0, + kind: WKind::ShaderOptionSlider { + label, + value, + index: *idx, + }, + list: false, + }); + } else { + widgets.push(button( + 0.0, + 0.0, + 150.0, + label, + Act::ShaderPackOption(*idx), + )); + } + } + } + } + } + let mut iter = widgets.into_iter(); + while let Some(mut first) = iter.next() { + if let Some(second) = iter.next() { + rows.push(Row::Buttons(first, Some(second))); + } else { + first.w = 310.0; + rows.push(Row::Buttons(first, None)); + } + } rows } _ => { @@ -1599,6 +1775,9 @@ impl OptionsUi { fn layout(&mut self, s: &Settings, w: f32, h: f32) -> (Vec, f32, f32, f32) { let mut out = Vec::new(); let page = self.page(); + if matches!(page, Page::ShaderPacks | Page::ShaderPackOptions) { + self.sync_shader_pack_config(s); + } let cx = (w / 2.0).floor(); if matches!(page, Page::Video | Page::Controls | Page::KeyBinds) { self.settings_search.x = cx - 100.0; @@ -2079,6 +2258,9 @@ impl OptionsUi { Page::ShaderPacks => { Some(("Open Shader Pack Folder", Act::OpenShaderPackFolder)) } + Page::ShaderPackOptions => { + Some(("Reset to Defaults", Act::ResetShaderPackOptions)) + } _ => None, }; if let Some((label, act)) = folder { @@ -2248,6 +2430,16 @@ impl OptionsUi { 170.0, )); } + if let WKind::Button { + act: Act::ShaderPackOption(i), + .. + } = &wd.kind + && let Some(cfg) = &self.shader_pack_config + && let Some(opt) = cfg.options.get(*i) + && !opt.description.is_empty() + { + tooltip = Some(atlas.font.split(&Text::literal(&opt.description), 170.0)); + } } match &wd.kind { WKind::Button { label, active, act } => { @@ -2288,6 +2480,31 @@ impl OptionsUi { true, ); } + WKind::ShaderOptionSlider { + label, + value, + index, + } => { + let dragging = self.dragging_shader_option == Some(*index); + if hover + && let Some(cfg) = &self.shader_pack_config + && let Some(opt) = cfg.options.get(*index) + && !opt.description.is_empty() + { + tooltip = Some(atlas.font.split(&Text::literal(&opt.description), 170.0)); + } + ui::slider( + batch, + atlas, + wd.x, + wd.y, + wd.w, + label, + *value, + hover || dragging, + true, + ); + } WKind::Text { text, color, @@ -2502,6 +2719,21 @@ impl OptionsUi { set_slider(opt, s, v); events.push(Event::Changed); } + if let Some(idx) = self.dragging_shader_option + && let Some(wd) = widgets.iter().find(|wd| { + matches!(&wd.kind, WKind::ShaderOptionSlider { index, .. } if *index == idx) + }) + { + let v = ui::slider_value(wd.x, wd.w, x); + if let Some(cfg) = &mut self.shader_pack_config + && let Some(opt) = cfg.options.get_mut(idx) + { + opt.set_slider_pos(v); + } + self.save_shader_pack_config(); + events.push(Event::Changed); + events.push(Event::ReloadShaderPack); + } return events; } // Controller Binds: a click cancels the wait. @@ -2589,6 +2821,45 @@ impl OptionsUi { Act::OpenShaderPackFolder => events.push(Event::OpenShaderPackFolder), Act::Credits => events.push(Event::Credits), Act::CopyAgentDetails => events.push(Event::CopyAgentDetails), + Act::ImportShaderPack => events.push(Event::ImportShaderPack), + Act::ShaderPackProfile => { + if let Some(cfg) = &mut self.shader_pack_config { + cfg.cycle_profile(right); + } + self.save_shader_pack_config(); + events.push(Event::Changed); + events.push(Event::ReloadShaderPack); + } + Act::ShaderPackSubscreen(sub) => { + if let Some(cfg) = &mut self.shader_pack_config { + cfg.active_screen = Some(sub); + } + self.scroll = 0.0; + } + Act::ShaderPackBackScreen => { + if let Some(cfg) = &mut self.shader_pack_config { + cfg.active_screen = None; + } + self.scroll = 0.0; + } + Act::ShaderPackOption(i) => { + if let Some(cfg) = &mut self.shader_pack_config + && let Some(opt) = cfg.options.get_mut(i) + { + opt.cycle(right); + } + self.save_shader_pack_config(); + events.push(Event::Changed); + events.push(Event::ReloadShaderPack); + } + Act::ResetShaderPackOptions => { + if let Some(cfg) = &mut self.shader_pack_config { + cfg.reset_all(); + } + self.save_shader_pack_config(); + events.push(Event::Changed); + events.push(Event::ReloadShaderPack); + } Act::None => {} } } @@ -2599,6 +2870,18 @@ impl OptionsUi { set_slider(opt, s, v); events.push(Event::Changed); } + WKind::ShaderOptionSlider { index, .. } => { + self.dragging_shader_option = Some(index); + let v = ui::slider_value(wd.x, wd.w, x); + if let Some(cfg) = &mut self.shader_pack_config + && let Some(opt) = cfg.options.get_mut(index) + { + opt.set_slider_pos(v); + } + self.save_shader_pack_config(); + events.push(Event::Changed); + events.push(Event::ReloadShaderPack); + } WKind::LanguageRow { code, .. } => { if self.language_choice.as_deref() == Some(code.as_str()) { events.extend(self.back(s)); @@ -2627,7 +2910,9 @@ impl OptionsUi { } // AbstractSliderButton suppresses the press sound and plays once on // release, even if the pointer has left the widget or its value stayed. - let slider = self.dragging.take().is_some(); + let slider = self.dragging.take().is_some() + || self.dragging_shader_option.take().is_some() + || self.hdragging_shader_option.take().is_some(); let graphics_slider = self.graphics.release(); if slider || graphics_slider { vec![Event::ButtonClick] @@ -2683,6 +2968,14 @@ impl OptionsUi { /// Escape or Done: leave the current page, applying page results. pub fn back(&mut self, s: &mut Settings) -> Vec { let mut events = Vec::new(); + if self.page() == Page::ShaderPackOptions + && let Some(cfg) = &mut self.shader_pack_config + && cfg.active_screen.is_some() + { + cfg.active_screen = None; + self.scroll = 0.0; + return events; + } let page = self.stack.pop().unwrap_or(Page::Main); self.scroll = 0.0; self.scrollbar_drag_y = None; @@ -3123,6 +3416,20 @@ mod tests { false, ); assert_eq!(s.horizon.shader_pack, "", "a second click turns it off"); + let import_btn = find(&mut ui, &s, &|a| *a == Act::ImportShaderPack).unwrap(); + assert_eq!( + ui.click( + &mut s, + &atlas, + import_btn.x + 1.0, + import_btn.y + 1.0, + size, + [480, 240], + false, + false, + ), + vec![Event::ButtonClick, Event::ImportShaderPack] + ); let _ = std::fs::remove_dir_all(&dir); } diff --git a/crates/horizon-client/src/shader_pack.rs b/crates/horizon-client/src/shader_pack.rs index f3cf46028..f5a15ca79 100644 --- a/crates/horizon-client/src/shader_pack.rs +++ b/crates/horizon-client/src/shader_pack.rs @@ -1,18 +1,20 @@ //! Horizon shader packs (docs/shader-packs.md): folders in `shaderpacks/` -//! holding a `pack.toml` and WGSL stages. Loading reads the manifest and the +//! holding a `pack.toml` and WGSL stages, plus Java (OptiFine/Iris) shader +//! packs in folders or `.zip` archives. Loading reads the manifest and the //! player's option values, puts the prelude before each stage's source and //! has naga parse and validate it, so a broken pack is reported (file, line, //! column) and never reaches the GPU. +use horizon_shaderpack::{JavaPack, preprocess::Environment, properties::MenuElement}; use serde::Deserialize; use std::{ - collections::BTreeMap, + collections::{BTreeMap, BTreeSet}, fs, path::{Path, PathBuf}, time::SystemTime, }; /// The stage and uniform contract this build provides. -pub const API: u32 = 1; +pub const API: u32 = 2; /// The folder beside `options.txt` that holds the packs. pub const DIR: &str = "shaderpacks"; pub const PRELUDE: &str = include_str!("shader_pack_prelude.wgsl"); @@ -29,6 +31,10 @@ struct Manifest { stage: Vec, #[serde(default)] options: BTreeMap, + #[serde(default)] + profiles: BTreeMap>, + #[serde(default)] + textures: BTreeMap, } #[derive(Clone, Debug, Deserialize)] #[serde(deny_unknown_fields)] @@ -51,11 +57,16 @@ struct StageEntry { shader: String, entry: Option, scale: Option, + format: Option, + #[serde(default)] + mipmap: bool, } #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct OptionEntry { label: Option, + #[serde(default)] + description: String, default: toml::Value, min: Option, max: Option, @@ -63,11 +74,20 @@ struct OptionEntry { values: Option>, } -/// Where a stage runs. Api 1 has `composite` only. +/// Where a stage runs. Api 1 has `composite` only; Api 2 adds `shadow`, +/// `gbuffer`, `deferred` and `final`. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum StageKind { + /// Directional sun/moon shadow map pass. + Shadow, + /// Material / G-buffer evaluation pass over world colour, depth and normals. + Gbuffer, + /// Opaque world lighting pass (runs before translucent water/weather). + Deferred, /// After the world pass, before the hand: reads the scene's colour and depth. Composite, + /// Final tonemapping / anti-aliasing pass before HUD and menus. + Final, } #[derive(Clone, Debug)] pub struct Stage { @@ -79,6 +99,10 @@ pub struct Stage { pub entry: String, /// Target size relative to the world's. pub scale: f32, + /// Optional target format name (`rgba16float`, `rgba8unorm`). + pub format: Option, + /// Whether the previous stage's output is mipmapped before this stage runs. + pub mipmap: bool, } #[derive(Clone, Copy, Debug, PartialEq)] pub enum OptionKind { @@ -91,16 +115,582 @@ pub enum OptionKind { max: f64, step: f64, }, + /// A Java `#define` / `const` string option cycling through `choices` + /// (or drawn as a slider when `slider` is true and choices are numeric). + Choice { + slider: bool, + }, } #[derive(Clone, Debug, PartialEq)] pub struct PackOption { - /// The WGSL `override` it sets. + /// The WGSL `override` or GLSL `#define`/`const` name it sets. pub key: String, pub label: String, + pub description: String, pub kind: OptionKind, pub values: Vec, pub default: f64, pub value: f64, + /// Allowed `(raw_value, display_label)` pairs for `OptionKind::Choice`. + pub choices: Vec<(String, String)>, + pub default_choice: String, + pub current_choice: String, +} + +impl PackOption { + /// Formatted current value for UI buttons and labels. + pub fn display_value(&self) -> String { + match self.kind { + OptionKind::Bool => { + if self.value != 0.0 { + "ON".into() + } else { + "OFF".into() + } + } + OptionKind::Int => format!("{}", self.value.round() as i64), + OptionKind::Float { step, .. } => { + if step >= 1.0 { + format!("{:.0}", self.value) + } else if step >= 0.1 { + format!("{:.1}", self.value) + } else { + format!("{:.2}", self.value) + } + } + OptionKind::Choice { .. } => self + .choices + .iter() + .find(|(raw, _)| *raw == self.current_choice) + .map(|(_, label)| label.clone()) + .unwrap_or_else(|| self.current_choice.clone()), + } + } + + /// Normalized `0.0..=1.0` slider position if this option is a slider. + pub fn slider_pos(&self) -> Option { + match self.kind { + OptionKind::Float { min, max, .. } if max > min => { + Some(((self.value - min) / (max - min)).clamp(0.0, 1.0) as f32) + } + OptionKind::Choice { slider: true } if self.choices.len() > 1 => { + let idx = self + .choices + .iter() + .position(|(raw, _)| *raw == self.current_choice) + .unwrap_or(0); + Some(idx as f32 / (self.choices.len() - 1) as f32) + } + _ => None, + } + } + + /// Update the option from a normalized `0.0..=1.0` slider drag. + pub fn set_slider_pos(&mut self, pos: f32) { + let pos = f64::from(pos.clamp(0.0, 1.0)); + match self.kind { + OptionKind::Float { min, max, step } => { + let mut v = min + pos * (max - min); + if step > 0.0 { + v = (v / step).round() * step; + } + self.value = v.clamp(min, max); + } + OptionKind::Choice { slider: true } if !self.choices.is_empty() => { + let idx = (pos * (self.choices.len() - 1) as f64).round() as usize; + if let Some((raw, _)) = self.choices.get(idx.min(self.choices.len() - 1)) { + self.current_choice = raw.clone(); + } + } + _ => {} + } + } + + /// Cycle or toggle the option (`reverse`: right-click steps backwards). + pub fn cycle(&mut self, reverse: bool) { + match self.kind { + OptionKind::Bool => { + self.value = if self.value == 0.0 { 1.0 } else { 0.0 }; + } + OptionKind::Int if !self.values.is_empty() => { + let cur = self.value.round() as i64; + let idx = self.values.iter().position(|&v| v == cur).unwrap_or(0); + let next = if reverse { + (idx + self.values.len() - 1) % self.values.len() + } else { + (idx + 1) % self.values.len() + }; + self.value = self.values[next] as f64; + } + OptionKind::Float { min, max, step } => { + let delta = if step > 0.0 { step } else { (max - min) * 0.05 }; + let next = if reverse { + self.value - delta + } else { + self.value + delta + }; + self.value = next.clamp(min, max); + } + OptionKind::Choice { .. } if !self.choices.is_empty() => { + let idx = self + .choices + .iter() + .position(|(raw, _)| *raw == self.current_choice) + .unwrap_or(0); + let next = if reverse { + (idx + self.choices.len() - 1) % self.choices.len() + } else { + (idx + 1) % self.choices.len() + }; + self.current_choice = self.choices[next].0.clone(); + } + _ => {} + } + } + + /// Serialized string value for `shaderpacks/.txt`, or `None` if at default. + pub fn saved_string(&self) -> Option { + match self.kind { + OptionKind::Bool => { + let b = self.value != 0.0; + let def = self.default != 0.0; + (b != def).then(|| b.to_string()) + } + OptionKind::Int => { + let i = self.value.round() as i64; + let def = self.default.round() as i64; + (i != def).then(|| i.to_string()) + } + OptionKind::Float { .. } => { + ((self.value - self.default).abs() > 1e-6).then(|| format!("{}", self.value)) + } + OptionKind::Choice { .. } => { + (self.current_choice != self.default_choice).then(|| self.current_choice.clone()) + } + } + } + + /// Reset to the pack's default value. + pub fn reset(&mut self) { + self.value = self.default; + self.current_choice = self.default_choice.clone(); + } +} + +/// A named preset profile in a Horizon or Java shader pack (`Low`, `High`, `Ultra`). +#[derive(Clone, Debug, PartialEq)] +pub struct PackProfile { + pub name: String, + pub label: String, + pub values: BTreeMap, +} + +/// An item on a shader pack option screen (`shaders.properties` `screen`). +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum ScreenItem { + Empty, + Profile, + Subscreen(String, String), + Option(usize), +} + +/// A laid-out option screen (main screen or sub-screen). +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct PackScreen { + pub name: String, + pub label: String, + pub columns: u32, + pub items: Vec, +} + +/// Interactive configuration metadata and current values for a shader pack, +/// usable directly by the Options UI without a GPU device. +#[derive(Clone, Debug, Default)] +pub struct PackConfig { + pub id: String, + pub options: Vec, + pub profiles: Vec, + pub main_screen: PackScreen, + pub subscreens: Vec, + /// Active subscreen name (`None` = main screen). + pub active_screen: Option, +} + +impl PackConfig { + /// Load the configurable options, profiles, and screens for pack `id` in `dir`. + pub fn load(dir: &Path, id: &str) -> Result { + if id.is_empty() { + return Err("no shader pack selected".into()); + } + let path = dir.join(id); + let saved = load_values(dir, id); + if path.join("pack.toml").is_file() { + let manifest = manifest(&path)?; + let mut options = Vec::new(); + for (key, entry) in manifest.options { + options.push(option(key, entry, &saved)?); + } + let mut profiles = Vec::new(); + for (name, map) in manifest.profiles { + let mut values = BTreeMap::new(); + for (k, v) in map { + let s = match v { + toml::Value::Boolean(b) => b.to_string(), + toml::Value::Integer(i) => i.to_string(), + toml::Value::Float(f) => f.to_string(), + toml::Value::String(s) => s, + _ => continue, + }; + values.insert(k, s); + } + profiles.push(PackProfile { + label: humanize_identifier(&name), + name, + values, + }); + } + let mut items = Vec::new(); + if !profiles.is_empty() { + items.push(ScreenItem::Profile); + } + items.extend((0..options.len()).map(ScreenItem::Option)); + return Ok(Self { + id: id.to_owned(), + options, + profiles, + main_screen: PackScreen { + name: String::new(), + label: manifest.pack.name, + columns: 2, + items, + }, + subscreens: Vec::new(), + active_screen: None, + }); + } + if crate::java_pack::is_java_pack(&path) { + return Self::load_java(&path, id, &saved); + } + Err("not a valid shader pack".into()) + } + + fn load_java(path: &Path, id: &str, saved: &BTreeMap) -> Result { + let pack = JavaPack::open(path)?; + let values = pack.values(saved); + let env = Environment::iris("Unknown", "", 4); + let props = pack.properties(&values, &env).unwrap_or_default(); + let lang = pack.translations("en_us"); + let sliders: BTreeSet<&str> = props.sliders().into_iter().collect(); + + let option_label = |name: &str, comment: Option<&str>| -> String { + lang.get(&format!("option.{name}")) + .cloned() + .or_else(|| { + comment + .map(str::trim) + .filter(|c| !c.is_empty() && c.len() <= 40 && !c.contains('.')) + .map(str::to_owned) + }) + .unwrap_or_else(|| humanize_identifier(name)) + }; + let option_desc = |name: &str, comment: Option<&str>| -> String { + lang.get(&format!("option.{name}.comment")) + .cloned() + .or_else(|| comment.map(str::to_owned)) + .unwrap_or_default() + }; + + let mut options = Vec::new(); + let mut by_name = BTreeMap::new(); + for (name, b) in &pack.options.bools { + let current = values.bools.get(name).copied().unwrap_or(b.default); + let idx = options.len(); + options.push(PackOption { + key: name.clone(), + label: option_label(name, b.comment.as_deref()), + description: option_desc(name, b.comment.as_deref()), + kind: OptionKind::Bool, + values: Vec::new(), + default: f64::from(u8::from(b.default)), + value: f64::from(u8::from(current)), + choices: Vec::new(), + default_choice: b.default.to_string(), + current_choice: current.to_string(), + }); + by_name.insert(name.clone(), idx); + } + for (name, s) in &pack.options.strings { + let current = values + .strings + .get(name) + .cloned() + .unwrap_or_else(|| s.default.clone()); + let choices: Vec<(String, String)> = s + .allowed + .iter() + .map(|raw| { + let label = lang + .get(&format!("value.{name}.{raw}")) + .or_else(|| lang.get(&format!("option.{name}.value.{raw}"))) + .cloned() + .unwrap_or_else(|| raw.clone()); + (raw.clone(), label) + }) + .collect(); + let idx = options.len(); + options.push(PackOption { + key: name.clone(), + label: option_label(name, s.comment.as_deref()), + description: option_desc(name, s.comment.as_deref()), + kind: OptionKind::Choice { + slider: sliders.contains(name.as_str()), + }, + values: Vec::new(), + default: 0.0, + value: 0.0, + choices, + default_choice: s.default.clone(), + current_choice: current, + }); + by_name.insert(name.clone(), idx); + } + + let profiles: Vec = props + .profiles(&pack.options) + .unwrap_or_default() + .into_iter() + .map(|p| { + let label = lang + .get(&format!("profile.{}", p.name)) + .cloned() + .unwrap_or_else(|| humanize_identifier(&p.name)); + PackProfile { + name: p.name, + label, + values: p.values, + } + }) + .collect(); + + let referenced: BTreeSet = std::iter::once(None) + .chain(props.screens().into_iter().map(Some)) + .flat_map(|s| props.screen(s).unwrap_or_default()) + .filter_map(|e| match e { + MenuElement::Option(o) => Some(o), + _ => None, + }) + .collect(); + + let build_screen = |screen_name: Option<&str>| -> PackScreen { + let elements = props + .screen(screen_name) + .unwrap_or_else(|| vec![MenuElement::Rest]); + let mut items = Vec::new(); + if screen_name.is_none() + && !profiles.is_empty() + && !elements.contains(&MenuElement::Profile) + { + items.push(ScreenItem::Profile); + } + for el in elements { + match el { + MenuElement::Empty => items.push(ScreenItem::Empty), + MenuElement::Profile => { + if !profiles.is_empty() { + items.push(ScreenItem::Profile); + } + } + MenuElement::Screen(sub) => { + let label = lang + .get(&format!("screen.{sub}")) + .cloned() + .unwrap_or_else(|| humanize_identifier(&sub)); + items.push(ScreenItem::Subscreen(sub, label)); + } + MenuElement::Option(opt) => { + if let Some(&idx) = by_name.get(&opt) { + items.push(ScreenItem::Option(idx)); + } + } + MenuElement::Rest => { + for (idx, opt) in options.iter().enumerate() { + if !referenced.contains(&opt.key) { + items.push(ScreenItem::Option(idx)); + } + } + } + } + } + let name_str = screen_name.unwrap_or("").to_owned(); + let label = match screen_name { + Some(n) => lang + .get(&format!("screen.{n}")) + .cloned() + .unwrap_or_else(|| humanize_identifier(n)), + None => id.to_owned(), + }; + PackScreen { + name: name_str, + label, + columns: props.columns(screen_name).unwrap_or(2), + items, + } + }; + + let main_screen = build_screen(None); + let subscreens = props + .screens() + .into_iter() + .map(|s| build_screen(Some(s))) + .collect(); + + Ok(Self { + id: id.to_owned(), + options, + profiles, + main_screen, + subscreens, + active_screen: None, + }) + } + + /// Currently active screen (main or a sub-screen). + pub fn current_screen(&self) -> &PackScreen { + if let Some(name) = &self.active_screen + && let Some(sub) = self.subscreens.iter().find(|s| s.name == *name) + { + return sub; + } + &self.main_screen + } + + /// Name of the profile matching current option values, or `"Custom"`. + pub fn active_profile_label(&self) -> String { + let map = self.to_map(); + for profile in &self.profiles { + let matches = profile.values.iter().all(|(k, v)| { + if let Some(cur) = map.get(k) { + return cur == v; + } + if let Some(opt) = self.options.iter().find(|o| o.key == *k) { + return match opt.kind { + OptionKind::Bool => (opt.default != 0.0).to_string() == *v, + OptionKind::Int => (opt.default.round() as i64).to_string() == *v, + OptionKind::Float { .. } => format!("{}", opt.default) == *v, + OptionKind::Choice { .. } => opt.default_choice == *v, + }; + } + true + }); + if matches { + return profile.label.clone(); + } + } + "Custom".into() + } + + /// Cycle to the next (or previous) profile and apply its option values. + pub fn cycle_profile(&mut self, reverse: bool) { + if self.profiles.is_empty() { + return; + } + let current = self.active_profile_label(); + let idx = self + .profiles + .iter() + .position(|p| p.label == current) + .map(|i| { + if reverse { + (i + self.profiles.len() - 1) % self.profiles.len() + } else { + (i + 1) % self.profiles.len() + } + }) + .unwrap_or(0); + let values = self.profiles[idx].values.clone(); + for opt in &mut self.options { + if let Some(v) = values.get(&opt.key) { + match opt.kind { + OptionKind::Bool => { + if let Ok(b) = v.parse::() { + opt.value = f64::from(u8::from(b)); + } + } + OptionKind::Int => { + if let Ok(i) = v.parse::() { + opt.value = i as f64; + } + } + OptionKind::Float { min, max, .. } => { + if let Ok(f) = v.parse::() { + opt.value = f.clamp(min, max); + } + } + OptionKind::Choice { .. } => { + opt.current_choice = v.clone(); + } + } + } + } + } + + /// Reset all options to their defaults. + pub fn reset_all(&mut self) { + for opt in &mut self.options { + opt.reset(); + } + } + + /// Export non-default option values as a `key=value` map. + pub fn to_map(&self) -> BTreeMap { + let mut out = BTreeMap::new(); + for opt in &self.options { + if let Some(val) = opt.saved_string() { + out.insert(opt.key.clone(), val); + } + } + out + } + + /// Save the current non-default option values to `shaderpacks/.txt`. + pub fn save(&self, dir: &Path) -> Result<(), String> { + save_values(dir, &self.id, &self.to_map()) + } +} + +fn humanize_identifier(raw: &str) -> String { + let mut words = Vec::new(); + let mut current = String::new(); + for ch in raw.chars() { + if ch == '_' || ch == '-' { + if !current.is_empty() { + words.push(std::mem::take(&mut current)); + } + } else if ch.is_uppercase() + && !current.is_empty() + && current.chars().last().is_some_and(char::is_lowercase) + { + words.push(std::mem::take(&mut current)); + current.push(ch); + } else { + current.push(ch); + } + } + if !current.is_empty() { + words.push(current); + } + words + .into_iter() + .map(|w| { + let lower = w.to_lowercase(); + let mut chars = lower.chars(); + match chars.next() { + Some(first) => format!("{}{}", first.to_uppercase(), chars.as_str()), + None => String::new(), + } + }) + .collect::>() + .join(" ") } pub struct ShaderPack { @@ -109,6 +699,8 @@ pub struct ShaderPack { pub info: Info, pub stages: Vec, pub options: Vec, + /// Optional custom noise/LUT texture bytes (RGBA8, width, height) from `[textures]`. + pub custom_noise: Option<(u32, u32, Vec)>, /// A Java (OptiFine/Iris) pack's translated passes, instead of `stages`. pub java: Option, dir: PathBuf, @@ -116,7 +708,7 @@ pub struct ShaderPack { } /// Every file's path, length and modification time: a changed stamp means -/// the pack was edited (hot reload). +/// the pack or its `.txt` option file was edited (hot reload). #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct Stamp(Vec<(PathBuf, u64, Option)>); impl Stamp { @@ -127,6 +719,17 @@ impl Stamp { { files.push((dir.to_path_buf(), meta.len(), meta.modified().ok())); } + // Also watch the sibling `.txt` options file for live option reloads. + if let Some(name) = dir.file_name() + && let Some(parent) = dir.parent() + { + let txt = parent.join(format!("{}.txt", name.to_string_lossy())); + if let Ok(meta) = fs::metadata(&txt) + && meta.is_file() + { + files.push((txt, meta.len(), meta.modified().ok())); + } + } let mut stack = vec![dir.to_path_buf()]; while let Some(d) = stack.pop() { let Ok(entries) = fs::read_dir(&d) else { @@ -177,6 +780,62 @@ pub fn folder() -> PathBuf { std::env::current_dir().unwrap_or_default().join(DIR) } +/// Import a `.zip` archive or shader pack folder from `source` into +/// `target_dir` (`shaderpacks/`), validating that it holds `pack.toml` or +/// `shaders/`. Returns the imported pack's folder/zip name (`id`). +pub fn import_pack(source: &Path, target_dir: &Path) -> Result { + let source = source + .canonicalize() + .unwrap_or_else(|_| source.to_path_buf()); + if !source.exists() { + return Err(format!("{}: file or folder not found", source.display())); + } + let name = source + .file_name() + .map(|n| n.to_string_lossy().into_owned()) + .filter(|n| !n.is_empty()) + .ok_or_else(|| "invalid shader pack path".to_string())?; + let is_horizon = source.is_dir() && source.join("pack.toml").is_file(); + let is_java = crate::java_pack::is_java_pack(&source); + if !is_horizon && !is_java { + return Err(format!( + "{name}: not a shader pack (needs pack.toml or shaders/)" + )); + } + if is_java && source.is_file() { + // Verify the zip actually contains `shaders/` or `pack.toml` at its root. + JavaPack::open(&source)?; + } else if is_horizon { + manifest(&source)?; + } + fs::create_dir_all(target_dir).map_err(|e| format!("{}: {e}", target_dir.display()))?; + let dest = target_dir.join(&name); + if dest.canonicalize().ok() == source.canonicalize().ok() { + return Ok(name); + } + if source.is_file() { + fs::copy(&source, &dest).map_err(|e| format!("failed to copy {name}: {e}"))?; + } else { + copy_dir_recursive(&source, &dest)?; + } + Ok(name) +} + +fn copy_dir_recursive(src: &Path, dst: &Path) -> Result<(), String> { + fs::create_dir_all(dst).map_err(|e| format!("{}: {e}", dst.display()))?; + let entries = fs::read_dir(src).map_err(|e| format!("{}: {e}", src.display()))?; + for entry in entries.flatten() { + let path = entry.path(); + let target = dst.join(entry.file_name()); + if path.is_dir() { + copy_dir_recursive(&path, &target)?; + } else { + fs::copy(&path, &target).map_err(|e| format!("{}: {e}", target.display()))?; + } + } + Ok(()) +} + /// `shaderpacks/` and its packs as the Shader Packs page lists them, read /// again at most once a second while the page is open, so a pack dropped /// into the folder shows up. @@ -199,13 +858,17 @@ impl Folder { self.dir = dir; self.read = Some(std::time::Instant::now()); } + + pub fn invalidate(&mut self) { + self.read = None; + } } /// A Java pack has no manifest: its name is its file name. fn java_info(name: String) -> Info { Info { name, - description: "Iris/OptiFine pack: its full-screen passes run.".into(), + description: "Iris/OptiFine pack: G-buffers, shadow map and post passes.".into(), authors: Vec::new(), version: String::new(), license: String::new(), @@ -216,7 +879,7 @@ fn java_info(name: String) -> Info { fn manifest(dir: &Path) -> Result { let text = fs::read_to_string(dir.join("pack.toml")).map_err(|e| format!("pack.toml: {e}"))?; let manifest: Manifest = toml::from_str(&text).map_err(|e| format!("pack.toml: {e}"))?; - if manifest.pack.api > API { + if manifest.pack.api == 0 || manifest.pack.api > API { return Err(format!( "pack.toml: needs shader pack api {}, this Horizon has {API}", manifest.pack.api @@ -225,12 +888,41 @@ fn manifest(dir: &Path) -> Result { Ok(manifest) } +pub(crate) fn decode_png_rgba(bytes: &[u8]) -> Result<(u32, u32, Vec), String> { + let mut decoder = png::Decoder::new(std::io::Cursor::new(bytes)); + decoder.set_transformations(png::Transformations::EXPAND | png::Transformations::STRIP_16); + let mut reader = decoder.read_info().map_err(|e| e.to_string())?; + let mut buf = vec![0u8; reader.output_buffer_size().unwrap_or(0)]; + let info = reader.next_frame(&mut buf).map_err(|e| e.to_string())?; + buf.truncate(info.buffer_size()); + let (w, h) = (info.width.max(1), info.height.max(1)); + let rgba = match info.color_type { + png::ColorType::Rgba => buf, + png::ColorType::Rgb => buf + .as_chunks::<3>() + .0 + .iter() + .flat_map(|p| [p[0], p[1], p[2], 255]) + .collect(), + png::ColorType::GrayscaleAlpha => buf + .as_chunks::<2>() + .0 + .iter() + .flat_map(|p| [p[0], p[0], p[0], p[1]]) + .collect(), + png::ColorType::Grayscale => buf.into_iter().flat_map(|g| [g, g, g, 255]).collect(), + _ => return Err("unsupported PNG color format".into()), + }; + Ok((w, h, rgba)) +} + impl ShaderPack { /// Load and validate the pack in `dir` with the option values in /// `values` (missing ones take their default). pub fn load(dir: &Path, values: &BTreeMap) -> Result { let stamp = Stamp::of(dir); let manifest = manifest(dir)?; + let pack_api = manifest.pack.api; if manifest.stage.is_empty() || manifest.stage.len() > MAX_STAGES { return Err(format!("pack.toml: 1 to {MAX_STAGES} stages")); } @@ -244,11 +936,15 @@ impl ShaderPack { .collect::, _>>()?; let mut stages = Vec::new(); for entry in manifest.stage { - let kind = match entry.kind.as_str() { - "composite" => StageKind::Composite, - other => { + let kind = match (pack_api, entry.kind.as_str()) { + (_, "composite") => StageKind::Composite, + (2.., "shadow") => StageKind::Shadow, + (2.., "gbuffer") => StageKind::Gbuffer, + (2.., "deferred") => StageKind::Deferred, + (2.., "final") => StageKind::Final, + (_, other) => { return Err(format!( - "pack.toml: stage kind `{other}` isn't supported (api {API} has `composite`)" + "pack.toml: stage kind `{other}` isn't supported (api {pack_api} has `composite`)" )); } }; @@ -273,8 +969,22 @@ impl ShaderPack { source, entry: entry_point, scale, + format: entry.format, + mipmap: entry.mipmap, }); } + let mut custom_noise = None; + if let Some(rel) = manifest + .textures + .get("noise") + .or_else(|| manifest.textures.values().next()) + { + if rel.contains("..") { + return Err(format!("pack.toml: texture {rel} is outside the pack")); + } + let bytes = fs::read(dir.join(rel)).map_err(|e| format!("{rel}: {e}"))?; + custom_noise = Some(decode_png_rgba(&bytes).map_err(|e| format!("{rel}: {e}"))?); + } Ok(Self { id: dir .file_name() @@ -283,6 +993,7 @@ impl ShaderPack { info: manifest.pack, stages, options, + custom_noise, java: None, dir: dir.to_path_buf(), stamp, @@ -305,16 +1016,57 @@ impl ShaderPack { .file_name() .map(|n| n.to_string_lossy().into_owned()) .unwrap_or_default(); + let options = PackConfig::load_java(path, &id, values) + .map(|c| c.options) + .unwrap_or_default(); Ok(Self { info: java_info(id.clone()), id, stages: Vec::new(), - options: Vec::new(), + options, + custom_noise: None, java: Some(java), dir: path.to_path_buf(), stamp, }) } + /// Whether this pack requests directional shadow map rendering. + pub fn needs_shadow(&self) -> bool { + if let Some(java) = &self.java { + return java.folders.iter().any(|f| f.shadow_enabled); + } + self.stages.iter().any(|s| { + s.kind == StageKind::Shadow + || s.source.contains("sample_shadow") + || s.source.contains("shadow_depth") + }) + } + /// Shadow map resolution requested by this pack, and shadow distance in blocks. + pub fn shadow_config(&self, dimension: i8) -> (u32, f32, f32) { + if let Some(java) = &self.java + && let Some(idx) = java.folder_for(dimension) + { + let d = &java.folders[idx].directives; + return ( + d.shadow_map_resolution, + d.shadow_distance, + d.sun_path_rotation, + ); + } + let res = self + .options + .iter() + .find(|o| o.key == "shadow_resolution" || o.key == "shadowMapResolution") + .map(|o| (o.value.round() as u32).clamp(256, 4096)) + .unwrap_or(2048); + let dist = self + .options + .iter() + .find(|o| o.key == "shadow_distance" || o.key == "shadowDistance") + .map(|o| (o.value as f32).clamp(16.0, 512.0)) + .unwrap_or(128.0); + (res, dist, 0.0) + } /// The pipeline-overridable constants every stage gets: the option values. pub fn constants(&self) -> Vec<(&str, f64)> { self.options @@ -368,15 +1120,20 @@ fn option( .ok() .filter(|f| f.is_finite()) .map(|f| f.clamp(min, max)), + OptionKind::Choice { .. } => None, }) .unwrap_or(default); Ok(PackOption { - label: entry.label.unwrap_or_else(|| key.clone()), + label: entry.label.unwrap_or_else(|| humanize_identifier(&key)), + description: entry.description, key, kind, values: values_list, default, value, + choices: Vec::new(), + default_choice: String::new(), + current_choice: String::new(), }) } @@ -422,6 +1179,25 @@ pub fn load_values(dir: &Path, id: &str) -> BTreeMap { .collect() } +/// Save option values for a pack (`shaderpacks/.txt`, `key=value`). +/// When `values` is empty, removes `.txt` if present. +pub fn save_values(dir: &Path, id: &str, values: &BTreeMap) -> Result<(), String> { + if id.is_empty() { + return Ok(()); + } + let path = dir.join(format!("{id}.txt")); + if values.is_empty() { + let _ = fs::remove_file(path); + return Ok(()); + } + fs::create_dir_all(dir).map_err(|e| e.to_string())?; + let mut text = String::new(); + for (k, v) in values { + text.push_str(&format!("{k}={v}\n")); + } + fs::write(&path, text).map_err(|e| format!("{}: {e}", path.display())) +} + #[cfg(test)] mod tests { use super::*; @@ -529,8 +1305,8 @@ override mode: i32 = 1; fn manifests_are_checked() { for (manifest, expected) in [ ( - MANIFEST.replace("api = 1", "api = 2"), - "needs shader pack api 2", + MANIFEST.replace("api = 1", "api = 3"), + "needs shader pack api 3", ), ( MANIFEST.replace("\"composite\"", "\"shadow\""), @@ -556,6 +1332,23 @@ override mode: i32 = 1; } } + #[test] + fn api_2_stages_and_pack_config_work() { + let manifest_v2 = MANIFEST + .replace("api = 1", "api = 2") + .replace("\"composite\"", "\"deferred\""); + let dir = pack(&[("pack.toml", &manifest_v2), ("shaders/grade.wgsl", GRADE)]); + let loaded = ShaderPack::load(&dir, &BTreeMap::new()).unwrap(); + assert_eq!(loaded.stages[0].kind, StageKind::Deferred); + let parent = dir.parent().unwrap(); + let id = dir.file_name().unwrap().to_str().unwrap(); + let mut cfg = PackConfig::load(parent, id).unwrap(); + assert_eq!(cfg.options.len(), 3); + cfg.options[0].cycle(false); + cfg.save(parent).unwrap(); + assert!(loaded.changed(), "saving .txt updates the stamp"); + } + #[test] fn edits_change_the_stamp_and_the_list_shows_every_pack() { let root = scratch(); diff --git a/crates/horizon-client/src/shader_pack_prelude.wgsl b/crates/horizon-client/src/shader_pack_prelude.wgsl index 7b16690f9..ebe3ad4cf 100644 --- a/crates/horizon-client/src/shader_pack_prelude.wgsl +++ b/crates/horizon-client/src/shader_pack_prelude.wgsl @@ -1,6 +1,7 @@ -// Horizon shader pack prelude, api 1 (docs/shader-packs.md): placed before -// every stage's source. Bindings, the Frame uniform, the full-screen vertex -// shader and helpers that hide the renderer's reversed depth. +// Horizon shader pack prelude, api 2 (docs/shader-packs.md): placed before +// every stage's source. Bindings, the Frame and FrameExt uniforms, the +// full-screen vertex shader, G-buffer/shadow accessors and PBR/post helpers +// that hide the renderer's reversed depth. struct Frame { view: mat4x4, projection: mat4x4, @@ -19,12 +20,36 @@ struct Frame { view_params: vec4, dimension: vec4, } + +struct FrameExt { + previous_view: mat4x4, + previous_projection: mat4x4, + shadow_view: mat4x4, + shadow_projection: mat4x4, + shadow_inverse_view: mat4x4, + shadow_inverse_projection: mat4x4, + // xyz: previous camera world position, w: frame delta time (seconds) + previous_camera: vec4, + // x: shadow map resolution, y: shadow distance (blocks), z: sun_path_rotation (rad), w: shadow strength + shadow_params: vec4, + // x: eye block light (0..240), y: eye sky light (0..240), z: smoothed wetness (0..1), w: center linear depth + environment: vec4, +} + @group(0) @binding(0) var scene_color: texture_2d; @group(0) @binding(1) var scene_depth: texture_depth_2d; @group(0) @binding(2) var previous: texture_2d; @group(0) @binding(3) var linear_sampler: sampler; @group(0) @binding(4) var nearest_sampler: sampler; @group(0) @binding(5) var frame: Frame; +@group(0) @binding(6) var frame_ext: FrameExt; +@group(0) @binding(7) var scene_normal: texture_2d; +@group(0) @binding(8) var scene_material: texture_2d; +@group(0) @binding(9) var opaque_depth: texture_depth_2d; +@group(0) @binding(10) var shadow_depth: texture_depth_2d; +@group(0) @binding(11) var shadow_sampler: sampler_comparison; +@group(0) @binding(12) var noise_texture: texture_2d; +@group(0) @binding(13) var repeat_sampler: sampler; struct StageIn { @builtin(position) position: vec4, @@ -42,6 +67,10 @@ struct StageIn { fn depth_at(uv: vec2) -> f32 { return textureSampleLevel(scene_depth, nearest_sampler, uv, 0); } +// The opaque world's depth at `uv` (before translucent water and stained glass). +fn opaque_depth_at(uv: vec2) -> f32 { + return textureSampleLevel(opaque_depth, nearest_sampler, uv, 0); +} fn is_sky(depth: f32) -> bool { return depth <= 0.0; } @@ -60,6 +89,70 @@ fn view_position(uv: vec2, depth: f32) -> vec3 { fn world_offset(uv: vec2, depth: f32) -> vec3 { return (frame.inverse_view * vec4(view_position(uv, depth), 1.0)).xyz; } +// World-space surface normal at `uv` from the G-buffer (or reconstructed from depth). +fn world_normal_at(uv: vec2) -> vec3 { + let encoded = textureSampleLevel(scene_normal, nearest_sampler, uv, 0.0); + if encoded.w > 0.001 { + return normalize(encoded.xyz * 2.0 - 1.0); + } + let step_uv = frame.resolution.zw; + let d0 = depth_at(uv); + let p0 = world_offset(uv, d0); + let px = world_offset(uv + vec2(step_uv.x, 0.0), depth_at(uv + vec2(step_uv.x, 0.0))) - p0; + let py = world_offset(uv + vec2(0.0, step_uv.y), depth_at(uv + vec2(0.0, step_uv.y))) - p0; + let n = cross(py, px); + let len = length(n); + if len > 1e-6 { + return n / len; + } + return vec3(0.0, 1.0, 0.0); +} +// G-buffer material data at `uv`: r = block light (0..1), g = sky light (0..1), +// b = water/translucent mask (0..1), a = roughness (0..1). +fn material_at(uv: vec2) -> vec4 { + return textureSampleLevel(scene_material, nearest_sampler, uv, 0.0); +} +// Reproject a camera-relative world position into previous-frame UV and clip depth. +fn reproject_uv(world_rel: vec3) -> vec3 { + let camera_delta = frame.camera_position.xyz - frame_ext.previous_camera.xyz; + let prev_rel = world_rel + camera_delta; + let clip = frame_ext.previous_projection * (frame_ext.previous_view * vec4(prev_rel, 1.0)); + if clip.w <= 1e-5 { + return vec3(-1.0); + } + let ndc = clip.xyz / clip.w; + return vec3(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5, ndc.z); +} +// Sample the directional sun/moon shadow map with 5-tap PCF and normal offset bias. +// Returns 1.0 in full light and 0.0 in full shadow. +fn sample_shadow(world_rel: vec3, world_normal: vec3) -> f32 { + if frame_ext.shadow_params.w <= 0.001 { + return 1.0; + } + let dist = length(world_rel); + let max_dist = max(frame_ext.shadow_params.y, 16.0); + if dist >= max_dist { + return 1.0; + } + let light_dir = normalize(select(frame.moon.xyz, frame.sun.xyz, frame.sun.y >= -0.05)); + let ndotl = max(dot(world_normal, light_dir), 0.0); + let bias_pos = world_rel + world_normal * (0.06 + 0.12 * (1.0 - ndotl)); + let light_clip = frame_ext.shadow_projection * (frame_ext.shadow_view * vec4(bias_pos, 1.0)); + let ndc = light_clip.xyz / max(light_clip.w, 1e-5); + let shadow_uv = vec2(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5); + if any(shadow_uv < vec2(0.002)) || any(shadow_uv > vec2(0.998)) || ndc.z <= 0.0 || ndc.z >= 1.0 { + return 1.0; + } + let texel = 1.0 / max(frame_ext.shadow_params.x, 512.0); + let ref_z = clamp(ndc.z + 0.0008, 0.0, 1.0); + var sum = textureSampleCompareLevel(shadow_depth, shadow_sampler, shadow_uv, ref_z); + sum += textureSampleCompareLevel(shadow_depth, shadow_sampler, shadow_uv + vec2(texel, 0.0), ref_z); + sum += textureSampleCompareLevel(shadow_depth, shadow_sampler, shadow_uv - vec2(texel, 0.0), ref_z); + sum += textureSampleCompareLevel(shadow_depth, shadow_sampler, shadow_uv + vec2(0.0, texel), ref_z); + sum += textureSampleCompareLevel(shadow_depth, shadow_sampler, shadow_uv - vec2(0.0, texel), ref_z); + let fade = smoothstep(max_dist, max_dist * 0.82, dist); + return mix(1.0, sum * 0.2, fade * frame_ext.shadow_params.w); +} fn luminance(c: vec3) -> f32 { return dot(c, vec3(0.2126, 0.7152, 0.0722)); } @@ -69,3 +162,12 @@ fn srgb_to_linear(c: vec3) -> vec3 { fn linear_to_srgb(c: vec3) -> vec3 { return select(1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, c * 12.92, c <= vec3(0.0031308)); } +// ACES fitted filmic tonemapping curve (linear HDR -> linear 0..1). +fn aces_tonemap(x: vec3) -> vec3 { + let a = 2.51; + let b = 0.03; + let c = 2.43; + let d = 0.59; + let e = 0.14; + return clamp((x * (a * x + b)) / (x * (c * x + d) + e), vec3(0.0), vec3(1.0)); +} diff --git a/crates/horizon-shaderpack/src/lib.rs b/crates/horizon-shaderpack/src/lib.rs index 71b6abbd3..fa454a134 100644 --- a/crates/horizon-shaderpack/src/lib.rs +++ b/crates/horizon-shaderpack/src/lib.rs @@ -17,7 +17,7 @@ pub mod uniforms; use include::{IncludeGraph, Inlined}; use options::{Annotated, OptionSet, OptionValues}; use preprocess::{Environment, Preprocessed}; -use properties::ShaderProperties; +use properties::{Properties, ShaderProperties}; use source::PackFiles; use std::{ collections::{BTreeMap, BTreeSet}, @@ -154,6 +154,32 @@ impl JavaPack { None => Ok(ShaderProperties::default()), } } + + /// The pack's language translations (`shaders/lang/.lang` or + /// `en_US.lang`), mapping keys such as `option.SHADOW_QUALITY`, + /// `value.SHADOW_QUALITY.2`, `screen.LIGHTING`, and `profile.HIGH` to their + /// display labels. + pub fn translations(&self, lang_code: &str) -> BTreeMap { + let mut out = BTreeMap::new(); + let lower = lang_code.to_lowercase(); + let upper = match lower.split_once('_') { + Some((l, r)) => format!("{l}_{}", r.to_uppercase()), + None => lower.clone(), + }; + let mut candidates = vec!["lang/en_US.lang".to_owned(), "lang/en_us.lang".to_owned()]; + if lower != "en_us" { + candidates.push(format!("lang/{lower}.lang")); + candidates.push(format!("lang/{upper}.lang")); + } + for path in candidates { + if let Some(text) = self.files.read(&path) { + for (k, v) in Properties::parse(&text).iter() { + out.insert(k.to_owned(), v.to_owned()); + } + } + } + out + } } /// A program's name in `shaders.properties` (`world-1/composite1.fsh` is diff --git a/crates/horizon-shaderpack/src/pipeline.rs b/crates/horizon-shaderpack/src/pipeline.rs index 7ebb35442..db656e3ae 100644 --- a/crates/horizon-shaderpack/src/pipeline.rs +++ b/crates/horizon-shaderpack/src/pipeline.rs @@ -11,7 +11,7 @@ use crate::{ JavaPack, options::OptionValues, preprocess::Environment, - properties::{LEGACY_TARGETS, ShaderProperties}, + properties::{Blend, LEGACY_TARGETS, ShaderProperties}, translate::{Link, Translated}, }; use std::collections::{BTreeMap, BTreeSet}; @@ -297,6 +297,10 @@ pub struct Pass { pub mipmapped: BTreeSet, /// `scale.`: the fraction of the screen it draws. pub scale: f32, + /// `blend.` override for all outputs of this pass. + pub blend: Option, + /// `blend..` overrides per colour target index. + pub buffer_blends: Vec<(u32, Blend)>, } /// The full-screen passes of one program folder, in order, and the @@ -319,6 +323,8 @@ pub struct Passes { /// next frame starts unflipped. pub flipped_at_end: BTreeSet, pub directives: Directives, + /// Whether the folder uses shadow mapping (`shadow` program or `shadowtex*`). + pub shadow_enabled: bool, } /// The `const` directives of a program folder that aren't about targets @@ -331,6 +337,28 @@ pub struct Directives { pub sun_path_rotation: f32, /// `eyeBrightnessHalflife`, tenths of a second: `eyeBrightnessSmooth`. pub eye_brightness_half_life: f32, + /// `wetnessHalflife`, tenths of a second: `wetness` smoothing when raining. + pub wetness_half_life: f32, + /// `drynessHalflife`, tenths of a second: `wetness` smoothing when dry. + pub dryness_half_life: f32, + /// `centerDepthHalflife`, tenths of a second: `centerDepthSmooth`. + pub center_depth_half_life: f32, + /// `shadowMapResolution` (or `SHADOW_RES`), in pixels per side. + pub shadow_map_resolution: u32, + /// `shadowDistance`, in blocks. + pub shadow_distance: f32, + /// `shadowDistanceRenderMul` (-1 when unset). + pub shadow_distance_render_mul: f32, + /// `ambientOcclusionLevel` (0.0..=1.0). + pub ambient_occlusion_level: f32, + /// `noiseTextureResolution`, in pixels per side. + pub noise_texture_resolution: u32, + /// `shadowHardwareFiltering` / `shadowHardwareFiltering0..1`. + pub shadow_hardware_filtering: [bool; 2], + /// `shadowtex0Nearest` / `shadowtex1Nearest`. + pub shadow_nearest: [bool; 2], + /// `generateShadowMipmap` / `shadowtex0Mipmap` / `shadowtex1Mipmap`. + pub shadow_mipmap: [bool; 2], } impl Default for Directives { @@ -338,6 +366,17 @@ impl Default for Directives { Self { sun_path_rotation: 0.0, eye_brightness_half_life: 10.0, + wetness_half_life: 600.0, + dryness_half_life: 200.0, + center_depth_half_life: 1.0, + shadow_map_resolution: 2048, + shadow_distance: 128.0, + shadow_distance_render_mul: -1.0, + ambient_occlusion_level: 1.0, + noise_texture_resolution: 256, + shadow_hardware_filtering: [false; 2], + shadow_nearest: [false; 2], + shadow_mipmap: [false; 2], } } } @@ -348,11 +387,66 @@ impl Directives { let mut out = Self::default(); for fragment in fragments { for c in const_directives(fragment) { - // Java's `Float.parseFloat` takes a trailing `f`. - let value = c.value.trim_end_matches(['f', 'F']).parse::(); - match (c.ty, c.key.as_str(), value) { - ("float", "sunPathRotation", Ok(v)) => out.sun_path_rotation = v, - ("float", "eyeBrightnessHalflife", Ok(v)) => out.eye_brightness_half_life = v, + let raw = c.value.trim_end_matches(['f', 'F']); + let fval = raw.parse::(); + let ival = raw.parse::(); + let bval = raw.parse::(); + match (c.ty, c.key.as_str()) { + ("float", "sunPathRotation") if fval.is_ok() => { + out.sun_path_rotation = fval.unwrap(); + } + ("float", "eyeBrightnessHalflife") if fval.is_ok() => { + out.eye_brightness_half_life = fval.unwrap(); + } + ("float", "wetnessHalflife") if fval.is_ok() => { + out.wetness_half_life = fval.unwrap(); + } + ("float", "drynessHalflife") if fval.is_ok() => { + out.dryness_half_life = fval.unwrap(); + } + ("float", "centerDepthHalflife") if fval.is_ok() => { + out.center_depth_half_life = fval.unwrap(); + } + ("float", "shadowDistance") if fval.is_ok() => { + out.shadow_distance = fval.unwrap().clamp(8.0, 1024.0); + } + ("float", "shadowDistanceRenderMul") if fval.is_ok() => { + out.shadow_distance_render_mul = fval.unwrap(); + } + ("float", "ambientOcclusionLevel") if fval.is_ok() => { + out.ambient_occlusion_level = fval.unwrap().clamp(0.0, 1.0); + } + ("int", "shadowMapResolution") if ival.is_ok() => { + out.shadow_map_resolution = ival.unwrap().clamp(64, 8192); + } + ("int", "noiseTextureResolution") if ival.is_ok() => { + out.noise_texture_resolution = ival.unwrap().clamp(16, 2048); + } + ("bool", "shadowHardwareFiltering") if bval.is_ok() => { + out.shadow_hardware_filtering = [bval.unwrap(); 2]; + } + ("bool", "shadowHardwareFiltering0") if bval.is_ok() => { + out.shadow_hardware_filtering[0] = bval.unwrap(); + } + ("bool", "shadowHardwareFiltering1") if bval.is_ok() => { + out.shadow_hardware_filtering[1] = bval.unwrap(); + } + ("bool", "shadowtexNearest" | "shadowtex0Nearest" | "shadow0MinMagNearest") + if bval.is_ok() => + { + out.shadow_nearest[0] = bval.unwrap(); + } + ("bool", "shadowtex1Nearest" | "shadow1MinMagNearest") if bval.is_ok() => { + out.shadow_nearest[1] = bval.unwrap(); + } + ("bool", "generateShadowMipmap" | "shadowtexMipmap" | "shadowtex0Mipmap") + if bval.is_ok() => + { + out.shadow_mipmap[0] = bval.unwrap(); + } + ("bool", "shadowtex1Mipmap") if bval.is_ok() => { + out.shadow_mipmap[1] = bval.unwrap(); + } _ => {} } } @@ -474,21 +568,43 @@ impl JavaPack { for name in &groups[0..2].concat() { fragments.insert(name.clone(), fragment(name)?); } + let shadow_enabled = ["shadow", "shadow_solid", "shadow_cutout"] + .iter() + .any(|s| exists(s)) + || sources.iter().any(|s| { + s.contains("shadowtex0") + || s.contains("shadowtex1") + || s.contains("watershadow") + || s.contains("shadow2D") + }); let mut out = Passes { targets: target_settings(sources.iter().map(String::as_str), properties), directives: Directives::read(sources.iter().map(String::as_str)), + shadow_enabled, ..Passes::default() }; + let resolve_blends = |prog: &str| { + let blend = properties.blend(prog).and_then(Result::ok); + let buffer_blends: Vec<(u32, Blend)> = properties + .buffer_blends(prog) + .into_iter() + .filter_map(|(i, b)| Some((i, b.ok()?))) + .collect(); + (blend, buffer_blends) + }; + let flips = explicit_flips(properties); let run = |group: &[String], pre: &str, flipped: &mut BTreeSet| { *flipped = flips_pre(&flips, pre, flipped); let mut passes = Vec::new(); for name in group { let text = &fragments[name]; + let prog = path(name); + let (blend, buffer_blends) = resolve_blends(&prog); let pass = Pass { - program: path(name), - draw_buffers: draw_buffers(text).map_err(|e| format!("{}: {e}", path(name)))?, + program: prog.clone(), + draw_buffers: draw_buffers(text).map_err(|e| format!("{prog}: {e}"))?, reads_alt: flipped.clone(), mipmapped: mipmapped(text), scale: properties @@ -496,6 +612,8 @@ impl JavaPack { .get(&format!("scale.{name}")) .and_then(|v| v.split_whitespace().next()?.parse().ok()) .unwrap_or(1.0), + blend, + buffer_blends, }; *flipped = flip_set(&pass, &flips); passes.push(pass); @@ -511,12 +629,16 @@ impl JavaPack { out.composite = run(&groups[3], "composite_pre", &mut flipped)?; out.flipped_at_end = flipped; if exists("final") { + let prog = path("final"); + let (blend, buffer_blends) = resolve_blends(&prog); out.final_pass = Some(Pass { - program: path("final"), + program: prog, draw_buffers: None, reads_alt: out.flipped_at_end.clone(), mipmapped: mipmapped(&fragment("final")?), scale: 1.0, + blend, + buffer_blends, }); } Ok(out) @@ -736,12 +858,14 @@ mod tests { assert_eq!(Directives::read([]), Directives::default()); assert_eq!(Directives::default().eye_brightness_half_life, 10.0); let d = Directives::read([ - "const float sunPathRotation = 30.0;\nconst float eyeBrightnessHalflife = 4.0f;", + "const float sunPathRotation = 30.0;\nconst float eyeBrightnessHalflife = 4.0f;\nconst int shadowMapResolution = 4096;\nconst float shadowDistance = 192.0;", "const float sunPathRotation = -40.0; // Complementary Unbound", "const int sunPathRotation = 5;", ]); assert_eq!(d.sun_path_rotation, -40.0); assert_eq!(d.eye_brightness_half_life, 4.0); + assert_eq!(d.shadow_map_resolution, 4096); + assert_eq!(d.shadow_distance, 192.0); } #[test] diff --git a/crates/horizon-shaderpack/src/properties.rs b/crates/horizon-shaderpack/src/properties.rs index 3c6ee251c..0e989341a 100644 --- a/crates/horizon-shaderpack/src/properties.rs +++ b/crates/horizon-shaderpack/src/properties.rs @@ -358,8 +358,21 @@ impl Default for SkyDirectives { } } +/// A custom texture bound by `texture.. = ` in +/// `shaders.properties`. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct CustomTexture { + /// Stage family (`composite`, `deferred`, `gbuffers`, `prepare`, `begin`), + /// or `None` when bound across all stages. + pub stage: Option, + /// Sampler uniform name (`noisetex`, `colortex4`, `customTex`, ...). + pub sampler: String, + /// Path relative to `shaders/` (`textures/noise.png`). + pub path: String, +} + /// A pack's `shaders.properties`. -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone)] pub struct ShaderProperties { /// As written: screens, sliders, profiles, feature flags. pub original: Properties, @@ -535,6 +548,31 @@ impl ShaderProperties { .collect() } + /// `texture.. = ` and `texture. = `: + /// custom textures defined in `shaders.properties`. + pub fn custom_textures(&self) -> Vec { + let mut out = Vec::new(); + for (k, v) in self.preprocessed.iter() { + let Some(rest) = k.strip_prefix("texture.") else { + continue; + }; + let path = v.split_whitespace().next().unwrap_or("").trim(); + if path.is_empty() { + continue; + } + let (stage, sampler) = match rest.split_once('.') { + Some((stage, sampler)) => (Some(stage.to_owned()), sampler.to_owned()), + None => (None, rest.to_owned()), + }; + out.push(CustomTexture { + stage, + sampler, + path: path.to_owned(), + }); + } + out + } + /// `uniform.*` and `variable.*`: the pack's custom uniforms. pub fn custom_uniforms(&self) -> crate::uniforms::CustomUniforms { crate::uniforms::CustomUniforms::parse(self.preprocessed.iter()) @@ -564,23 +602,37 @@ impl ShaderProperties { pub fn blend(&self, program: &str) -> Option> { self.preprocessed .get(&format!("blend.{program}")) + .or_else(|| { + let short = program.rsplit('/').next()?; + self.preprocessed.get(&format!("blend.{short}")) + }) .map(parse_blend) } /// `blend..`: per colour target overrides, by target index. pub fn buffer_blends(&self, program: &str) -> Vec<(u32, Result)> { - let prefix = format!("blend.{program}."); - self.preprocessed - .iter() - .filter_map(|(k, v)| { - let buffer = k.strip_prefix(&prefix)?; - let index = match LEGACY_TARGETS.iter().position(|t| *t == buffer) { - Some(i) => i as u32, - None => buffer.strip_prefix("colortex")?.parse().ok()?, - }; - Some((index, parse_blend(v))) - }) - .collect() + let short = program.rsplit('/').next().unwrap_or(program); + let prefixes = [format!("blend.{program}."), format!("blend.{short}.")]; + let mut out = Vec::new(); + for (k, v) in self.preprocessed.iter() { + let Some(buffer) = prefixes.iter().find_map(|p| k.strip_prefix(p.as_str())) else { + continue; + }; + let Some(index) = LEGACY_TARGETS + .iter() + .position(|t| *t == buffer) + .map(|i| i as u32) + .or_else(|| buffer.strip_prefix("colortex")?.parse().ok()) + else { + continue; + }; + if let Some(existing) = out.iter_mut().find(|(i, _)| *i == index) { + existing.1 = parse_blend(v); + } else { + out.push((index, parse_blend(v))); + } + } + out } } @@ -796,6 +848,7 @@ blend.gbuffers_water.gaux2 = off blend.gbuffers_water.colortex9 = ONE ONE ONE ONE size.buffer.colortex4 = 0.50 QUALITY image.lightimg0 = lighttex0 rgba RGBA16F half_float false false 128 64 128 +texture.composite.colortex4 = textures/noise.png # a comment, not a directive iris.features.optional = CUSTOM_IMAGES SSBO "; @@ -823,6 +876,14 @@ iris.features.optional = CUSTOM_IMAGES SSBO p.image_formats(), [("lightimg0".to_owned(), "rgba16f".to_owned())].into() ); + assert_eq!( + p.custom_textures(), + [CustomTexture { + stage: Some("composite".into()), + sampler: "colortex4".into(), + path: "textures/noise.png".into(), + }] + ); assert_eq!(p.features(), (vec![], vec!["CUSTOM_IMAGES", "SSBO"])); // The preprocessed read: #if picked, values expanded, spelling kept. assert!(!p.program_enabled("composite1", &set, &values)); diff --git a/crates/horizon-shaderpack/src/source.rs b/crates/horizon-shaderpack/src/source.rs index 0501661ce..4e4bcd779 100644 --- a/crates/horizon-shaderpack/src/source.rs +++ b/crates/horizon-shaderpack/src/source.rs @@ -63,13 +63,27 @@ impl PackFiles { Ok(Self::Zip(files)) } + /// Raw bytes of a file relative to `shaders/` (leading `/` or `./` stripped), + /// or `None` when it doesn't exist. + pub fn read_bytes(&self, rel: &str) -> Option> { + let clean = rel + .trim() + .trim_start_matches("./") + .trim_start_matches('/') + .trim_start_matches("shaders/"); + if clean.is_empty() || clean.contains("..") { + return None; + } + match self { + Self::Folder(root) => fs::read(root.join(clean)).ok(), + Self::Zip(files) => files.get(clean).cloned(), + } + } + /// A text file, or `None` when it doesn't exist. Invalid UTF-8 is /// replaced, as packs are often saved in Latin-1. pub fn read(&self, rel: &str) -> Option { - let bytes = match self { - Self::Folder(root) => fs::read(root.join(rel)).ok()?, - Self::Zip(files) => files.get(rel)?.clone(), - }; + let bytes = self.read_bytes(rel)?; Some(String::from_utf8_lossy(&bytes).into_owned()) } diff --git a/crates/horizon-shaderpack/src/translate.rs b/crates/horizon-shaderpack/src/translate.rs index 8a32795af..623ad760d 100644 --- a/crates/horizon-shaderpack/src/translate.rs +++ b/crates/horizon-shaderpack/src/translate.rs @@ -5,8 +5,9 @@ //! patches do for the core profile, prints GLSL 450 and has naga turn that //! into a validated module and WGSL: //! -//! - `attribute`/`varying` become `in`/`out` with locations; matrix and array -//! varyings are split into vectors (WGSL can pass neither between stages); +//! - `attribute`/`varying` become `in`/`out` with locations; matrix, nested +//! array and struct varyings are split into vectors (WGSL can pass none of +//! them directly between stages); //! - loose uniforms go into one std140 block (`bool`s stored as `int`); //! - `sampler2D` & co. become a texture and a sampler binding, rebuilt as //! `sampler2D(t, s)` where used, also through function parameters, and not @@ -16,13 +17,16 @@ //! (`vaPosition`, `modelViewMatrix`, ...) are declared; //! - legacy texture calls are renamed, `shadow2D` returns a `vec4` again and a //! swizzled shadow lookup is widened first (naga has no scalar swizzle); +//! - vertex and compute `texture(s, uv)` calls without an explicit LOD become +//! `textureLod(s, uv, 0.0)` so naga doesn't reject implicit derivatives; //! - global initialisers run at the start of `main` (drivers accept //! non-constant ones), unsized global arrays are sized from them; //! - `isnan`, `isinf` and `modf` are helper functions (WGSL has none); //! - what naga refuses and drivers accept: `%` goes through overloads (naga -//! won't take `ivec2 % int`), `sqrt` of a negative constant isn't folded, -//! constants naga can't fold (`?:`, `mix`) become globals set in `main`, -//! and 16/8-bit types are their 32-bit ones. +//! won't take `ivec2 % int`), `mat2(vec4)` goes through `iris_mat2`, `1 << 31` +//! shifts as unsigned before casting back to `int`, `sqrt` of a negative +//! constant isn't folded, constants naga can't fold (`?:`, `mix`, `<<`) +//! become globals set in `main`, and 16/8-bit types are their 32-bit ones. //! //! The programs of one pass share a [`Link`], so their locations, bindings and //! uniform block agree. @@ -238,6 +242,18 @@ vec3 iris_modf(vec3 x, out vec3 i) { i = trunc(x); return x - i; } vec4 iris_modf(vec4 x, out vec4 i) { i = trunc(x); return x - i; } "; +/// Overloaded `mat2` constructor helper: drivers accept `mat2(vec4)` (Bloop), +/// which naga's GLSL front end refuses. +const MAT2_HELPERS: &str = "\ +mat2 iris_mat2(float x) { return mat2(x); } +mat2 iris_mat2(vec2 a, vec2 b) { return mat2(a, b); } +mat2 iris_mat2(float a, float b, float c, float d) { return mat2(a, b, c, d); } +mat2 iris_mat2(vec4 v) { return mat2(v.xy, v.zw); } +mat2 iris_mat2(mat2 m) { return m; } +mat2 iris_mat2(mat3 m) { return mat2(m); } +mat2 iris_mat2(mat4 m) { return mat2(m); } +"; + /// A fragment output narrower than four channels (`out vec3 color`, /// Steadfast's `final`): the four-channel type it is written through and the /// epilogue line that fills it, with alpha 1 and the rest 0. `None` when the @@ -376,69 +392,62 @@ pub fn translate( let wgsl = naga::back::wgsl::write_string(&module, &info, naga::back::wgsl::WriterFlags::empty()) .map_err(|e| e.to_string())?; - let interface = std::mem::take(&mut legaliser.interface); Ok(Translated { glsl, module, info, wgsl, - interface, + interface: legaliser.interface, }) } -/// The GLSL built-ins naga's constant evaluator folds (`MathFunction`s it -/// implements), besides type constructors. +/// Built-in functions naga folds in `const` initialisers. const FOLDABLE: &[&str] = &[ - "abs", - "acos", - "acosh", - "asin", - "asinh", - "atan", - "atanh", - "ceil", - "clamp", - "cos", - "cosh", - "cross", + "radians", "degrees", + "sin", + "cos", + "tan", + "asin", + "acos", + "atan", + "pow", + "exp", + "log", + "exp2", + "log2", + "sqrt", + "inversesqrt", + "abs", + "sign", + "floor", + "trunc", + "round", + "ceil", + "fract", + "mod", + "min", + "max", + "clamp", + "step", + "length", "distance", "dot", - "exp", - "exp2", - "floor", - "fma", - "fract", - "inversesqrt", - "length", - "log", - "log2", - "max", - "min", + "cross", "normalize", - "pow", - "radians", - "round", - "sign", - "sin", - "sinh", - "sqrt", - "step", - "tan", - "tanh", - "trunc", ]; -/// Whether a constant's initialisers need what naga can't fold and drivers -/// do: a `?:`, another built-in (Bliss's `mix`), or a constant that had to -/// become a global (`runtime`). +fn constructor(name: &str) -> bool { + ["bool", "int", "uint", "float", "double"].contains(&name) + || ["vec", "ivec", "uvec", "bvec", "dvec", "mat", "dmat"] + .iter() + .any(|p| name.starts_with(p)) +} + +/// Whether `list` has an initialiser naga's constant folder refuses (`?:`, +/// `mix`, `smoothstep`, `1 << 31`, or a reference to a constant already turned +/// into a global). fn needs_runtime(list: &ast::InitDeclaratorList, runtime: &HashSet) -> bool { - let constructor = |name: &str| { - let n = name.trim_start_matches(['i', 'u', 'b', 'd']); - matches!(name, "float" | "int" | "uint" | "bool" | "double") - || n.starts_with("vec") - || n.starts_with("mat") - }; let expr = |e: &ast::Expr| { fn walk(e: &ast::Expr, f: &dyn Fn(&ast::Expr) -> bool) -> bool { use ast::ExprData as E; @@ -454,6 +463,14 @@ fn needs_runtime(list: &ast::InitDeclaratorList, runtime: &HashSet) -> b } walk(e, &|e| match &e.content { ast::ExprData::Ternary(..) => true, + ast::ExprData::Binary(op, _, _) + if matches!( + op.content, + ast::BinaryOpData::LShift | ast::BinaryOpData::RShift + ) => + { + true + } ast::ExprData::Variable(v) => runtime.contains(v.as_str()), ast::ExprData::FunCall(f, _) => { callee(f).is_some_and(|c| !constructor(&c) && !FOLDABLE.contains(&c.as_str())) @@ -503,6 +520,9 @@ enum Local { Sampler(&'static str), } +/// A struct field `(type_name, field_name, array_suffix)`. +type StructField = (String, String, String); + struct Legaliser<'a> { stage: Stage, link: &'a mut Link, @@ -515,6 +535,8 @@ struct Legaliser<'a> { /// User functions → indices of their sampler parameters. sampler_params: HashMap>, functions: HashSet, + /// User structs → their fields `(ty, name, array)` (for struct varyings). + structs: HashMap>, scopes: Vec>, /// Declarations before the program (bindings, block, inputs, outputs). header: Vec, @@ -528,10 +550,11 @@ struct Legaliser<'a> { errors: Vec, /// Inside a constant initialiser: no helper calls there. constant: bool, - /// The program uses `%` ([`modulo_helpers`]) or `iris_sqrt`: their - /// helpers go in. + /// The program uses `%` ([`modulo_helpers`]), `iris_sqrt` or `iris_mat2`: + /// their helpers go in. modulo: bool, nan: bool, + mat2_ctor: bool, /// Constants turned into globals ([`needs_runtime`]). runtime_constants: HashSet, } @@ -634,6 +657,38 @@ fn array_text(a: &Option) -> String { .collect() } +/// Parse one or more `[N]` dimensions (`[9][9]` → `Some(vec![9, 9])`). +fn array_dims(array: &str) -> Option> { + if array.is_empty() { + return Some(Vec::new()); + } + let mut dims = Vec::new(); + let mut rest = array.trim(); + while !rest.is_empty() { + let inner = rest.strip_prefix('[')?; + let (num, tail) = inner.split_once(']')?; + dims.push(num.trim().parse::().ok()?); + rest = tail.trim(); + } + Some(dims) +} + +/// Expand multi-dimensional indices `dims` into `(key_suffix, expr_suffix)` +/// pairs (`[2, 2]` → `("#0#0", "[0][0]")`, `("#0#1", "[0][1]")`, ...). +fn expand_dims(dims: &[usize]) -> Vec<(String, String)> { + let mut out = vec![(String::new(), String::new())]; + for &n in dims { + let mut next = Vec::with_capacity(out.len() * n); + for (k, e) in &out { + for i in 0..n { + next.push((format!("{k}#{i}"), format!("{e}[{i}]"))); + } + } + out = next; + } + out +} + /// The element count of `T[](a, b, c)`. fn constructor_len(init: &Option) -> Option { match &init.as_ref()?.content { @@ -718,6 +773,7 @@ impl<'a> Legaliser<'a> { sampler_vars: HashSet::new(), sampler_params: HashMap::new(), functions: HashSet::new(), + structs: HashMap::new(), scopes: Vec::new(), header: Vec::new(), prologue: Vec::new(), @@ -728,14 +784,43 @@ impl<'a> Legaliser<'a> { constant: false, modulo: false, nan: false, + mat2_ctor: false, runtime_constants: HashSet::new(), } } + fn record_struct(&mut self, spec: &ast::StructSpecifier) { + let Some(name) = &spec.name else { return }; + let mut fields = Vec::new(); + for field in &spec.fields { + let base_ty = type_text(&field.ty); + let ty_arr = array_text(&field.ty.array_specifier); + for id in &field.identifiers { + let arr = format!("{ty_arr}{}", array_text(&id.array_spec)); + fields.push((base_ty.clone(), id.ident.as_str().to_owned(), arr)); + } + } + self.structs.insert(name.as_str().to_owned(), fields); + } + fn unit(&mut self, unit: &mut ast::TranslationUnit) -> Result<(), String> { - // Functions and their sampler parameters first: calls may precede - // definitions through prototypes. + // Functions, their sampler parameters, and struct definitions first. for ed in &unit.0 { + if let ast::ExternalDeclarationData::Declaration(d) = &ed.content { + match &d.content { + ast::DeclarationData::InitDeclaratorList(list) => { + if let ast::TypeSpecifierNonArrayData::Struct(s) = + &list.head.ty.ty.ty.content + { + self.record_struct(s); + } + } + ast::DeclarationData::TypeOnly(q) => { + let _ = q; + } + _ => {} + } + } let proto = match &ed.content { ast::ExternalDeclarationData::FunctionDefinition(f) => &f.prototype, ast::ExternalDeclarationData::Declaration(d) => match &d.content { @@ -812,6 +897,22 @@ impl<'a> Legaliser<'a> { } fn global_list(&mut self, list: &mut ast::InitDeclaratorList) -> Result { + // If a standalone `struct S { ... };` has no declarator names, keep it + // in the header so varyings of type `S` can declare `S varname;` there. + if list.head.name.is_none() + && list.tail.is_empty() + && matches!( + list.head.ty.ty.ty.content, + ast::TypeSpecifierNonArrayData::Struct(_) + ) + { + let mut text = String::new(); + let decl = ast::DeclarationData::InitDeclaratorList(list.clone()).into_node(); + show_declaration(&mut text, &decl, &mut FormattingState::default()) + .map_err(|e| e.to_string())?; + self.header.push(text.trim().to_owned()); + return Ok(false); + } let qualifier = list.head.ty.qualifier.clone(); let storage = storage(&qualifier); let has = |s: ast::StorageQualifierData| storage.contains(&s); @@ -939,32 +1040,34 @@ impl<'a> Legaliser<'a> { .find(|(k, _)| k == "location") .and_then(|(_, v)| v?.parse().ok()); for (i, (name, array)) in names.iter().enumerate() { - let full = format!("{ty}{ty_array}"); + let full_array = format!("{ty_array}{array}"); if input { let at = Link::location(&mut self.link.attributes, name); - self.header - .push(format!("layout(location = {at}) in {full} {name}{array};")); + self.header.push(format!( + "layout(location = {at}) in {ty} {name}{full_array};" + )); self.interface.attributes.insert(name.clone()); } else if !varying { let at = explicit .map(|l: u32| l + i as u32) .unwrap_or_else(|| self.next_output()); - if let (Some(widened), "") = (widen_output(&full, name), array.as_str()) { + if let (Some(widened), "") = (widen_output(&ty, name), full_array.as_str()) { // GL fills the missing channels of a narrow output; // wgpu wants every channel of the target written. let (wide, fill) = widened; - self.header.push(format!("{full} {name};")); + self.header.push(format!("{ty} {name};")); self.header.push(format!( "layout(location = {at}) out {wide} iris_out_{name};" )); self.epilogue.push(fill); } else { - self.header - .push(format!("layout(location = {at}) out {full} {name}{array};")); + self.header.push(format!( + "layout(location = {at}) out {ty} {name}{full_array};" + )); } self.interface.outputs.insert(at, name.clone()); } else { - self.varying(dir, &interp, &full, name, array); + self.varying(dir, &interp, &ty, name, &full_array); } } return Ok(false); @@ -1001,64 +1104,106 @@ impl<'a> Legaliser<'a> { .unwrap() } - /// One varying: matrices and arrays split into vectors that a private - /// global of the original type is packed from or into. - fn varying(&mut self, dir: &str, interp: &str, ty: &str, name: &str, array: &str) { + /// Emit scalar/vector/matrix varying leaves for an expression `expr_path` + /// with key `key_prefix`. + fn varying_leaf(&mut self, dir: &str, interp: &str, ty: &str, key: &str, expr_path: &str) { + if let Some(fields) = self.structs.get(ty).cloned() { + for (f_ty, f_name, f_arr) in fields { + let sub_key = format!("{key}#{f_name}"); + let sub_expr = format!("{expr_path}.{f_name}"); + self.varying_composite(dir, interp, &f_ty, &sub_key, &sub_expr, &f_arr); + } + return; + } + if let Some((cols, column)) = matrix_columns(ty) { + let flat = if is_integer(&column) && !interp.contains("flat") { + "flat " + } else { + "" + }; + let mut columns = Vec::new(); + for c in 0..cols { + let c_key = format!("{key}#{c}"); + let at = Link::location(&mut self.link.varyings, &c_key); + let part = c_key.replace('#', "_iris"); + self.header.push(format!( + "layout(location = {at}) {flat}{interp}{dir} {column} {part};" + )); + if dir == "out" { + self.epilogue.push(format!("{part} = {expr_path}[{c}];")); + } + columns.push(part); + } + if dir == "in" { + self.prologue + .push(format!("{expr_path} = {ty}({});", columns.join(", "))); + } + return; + } let flat = if is_integer(ty) && !interp.contains("flat") { "flat " } else { "" }; - let count: Option = array - .strip_prefix('[') - .and_then(|a| a.strip_suffix(']')) - .and_then(|n| n.parse().ok()); - let parts: Vec<(String, String, String)> = match (matrix_columns(ty), count) { - (Some((cols, column)), None) => (0..cols) - .map(|c| { - ( - format!("{name}#{c}"), - column.clone(), - format!("{name}[{c}]"), - ) - }) - .collect(), - (None, Some(n)) => (0..n) - .map(|e| (format!("{name}#{e}"), ty.to_owned(), format!("{name}[{e}]"))) - .collect(), - (None, None) => { - let at = Link::location(&mut self.link.varyings, name); - self.header.push(format!( - "layout(location = {at}) {flat}{interp}{dir} {ty} {name};" - )); - return; - } - (Some(_), Some(_)) => { - self.errors.push(format!( - "varying {name}: arrays of matrices are not supported" - )); - return; - } + let at = Link::location(&mut self.link.varyings, key); + let part = key.replace('#', "_iris"); + self.header.push(format!( + "layout(location = {at}) {flat}{interp}{dir} {ty} {part};" + )); + if dir == "out" { + self.epilogue.push(format!("{part} = {expr_path};")); + } else { + self.prologue.push(format!("{expr_path} = {part};")); + } + } + + fn varying_composite( + &mut self, + dir: &str, + interp: &str, + ty: &str, + key: &str, + expr_path: &str, + array: &str, + ) { + let Some(dims) = array_dims(array) else { + self.errors + .push(format!("varying {expr_path}: unsupported array `{array}`")); + return; }; - self.header.push(format!("{ty} {name}{array};")); - let mut columns = Vec::new(); - for (key, part_ty, element) in &parts { - let at = Link::location(&mut self.link.varyings, key); - let part = key.replace('#', "_iris"); - self.header.push(format!( - "layout(location = {at}) {flat}{interp}{dir} {part_ty} {part};" - )); - if dir == "out" { - self.epilogue.push(format!("{part} = {element};")); - } else if count.is_some() { - self.prologue.push(format!("{element} = {part};")); + if dims.is_empty() { + self.varying_leaf(dir, interp, ty, key, expr_path); + } else { + for (k_suf, e_suf) in expand_dims(&dims) { + self.varying_leaf( + dir, + interp, + ty, + &format!("{key}{k_suf}"), + &format!("{expr_path}{e_suf}"), + ); } - columns.push(part); } - if dir == "in" && count.is_none() { - self.prologue - .push(format!("{name} = {ty}({});", columns.join(", "))); + } + + /// One varying: matrices, arrays (including multi-dimensional `vec3[9][9]`) + /// and structs (`CloudsParameters`) split into vectors that a private + /// global of the original type is packed from or into. + fn varying(&mut self, dir: &str, interp: &str, ty: &str, name: &str, array: &str) { + if array.is_empty() && matrix_columns(ty).is_none() && !self.structs.contains_key(ty) { + let flat = if is_integer(ty) && !interp.contains("flat") { + "flat " + } else { + "" + }; + let at = Link::location(&mut self.link.varyings, name); + self.header.push(format!( + "layout(location = {at}) {flat}{interp}{dir} {ty} {name};" + )); + return; } + self.header.push(format!("{ty} {name}{array};")); + self.varying_composite(dir, interp, ty, name, name, array); } fn sized(&mut self, list: &mut ast::InitDeclaratorList) { @@ -1404,13 +1549,30 @@ impl<'a> Legaliser<'a> { self.expr(a); self.expr(b); self.modulo = true; - let (a, b) = ( - std::mem::replace(&mut **a, var("_")), - std::mem::replace(&mut **b, var("_")), - ); - *e = call("iris_mod", vec![a, b]); + let lhs = std::mem::replace(&mut **a, var("_")); + let rhs = std::mem::replace(&mut **b, var("_")); + *e = call("iris_mod", vec![lhs, rhs]); } - E::Assignment(a, op, b) if op.content == ast::AssignmentOpData::Mod => { + E::Binary(op, a, b) + if op.content == ast::BinaryOpData::LShift + && matches!(a.content, E::IntConst(_)) + && matches!(b.content, E::IntConst(n) if n >= 31) => + { + self.expr(a); + self.expr(b); + let lhs = std::mem::replace(&mut **a, var("_")); + let rhs = std::mem::replace(&mut **b, var("_")); + let shifted = ast::ExprData::Binary( + ast::BinaryOpData::LShift.into_node(), + Box::new(call("uint", vec![lhs])), + Box::new(call("uint", vec![rhs])), + ) + .into_node(); + *e = call("int", vec![shifted]); + } + E::Assignment(a, op, b) + if op.content == ast::AssignmentOpData::Mod && !self.constant => + { self.expr(a); self.expr(b); self.modulo = true; @@ -1538,17 +1700,53 @@ impl<'a> Legaliser<'a> { if self.functions.contains(&name) { return; } + // `mat2(vec4)` (Bloop): naga refuses 1-argument `mat2` from a `vec4`. + if name == "mat2" + && !self.constant + && args.len() == 1 + && !matches!( + args[0].content, + ast::ExprData::FloatConst(_) + | ast::ExprData::IntConst(_) + | ast::ExprData::UIntConst(_) + | ast::ExprData::DoubleConst(_) + ) + { + *f = ast::FunIdentifierData::Expr(Box::new(var("iris_mat2"))).into_node(); + self.mat2_ctor = true; + return; + } // `sqrt(-1.0)`, Bloop's NaN: naga folds it and fails, drivers give NaN. if name == "sqrt" && !self.constant && args.len() == 1 && negative_constant(&args[0]) { *f = ast::FunIdentifierData::Expr(Box::new(var("iris_sqrt"))).into_node(); self.nan = true; return; } + let mut target_name = name.as_str(); if let Some((_, new)) = LEGACY_CALLS.iter().find(|(old, _)| *old == name) { *f = ast::FunIdentifierData::Expr(Box::new(var(new))).into_node(); + target_name = new; } else if let Some((_, new)) = SHADOW_CALLS.iter().find(|(old, _)| *old == name) { let args = std::mem::take(args); *e = call("vec4", vec![call(new, args)]); + return; + } + // In vertex and compute shaders, `texture(sampler, coord)` uses implicit + // derivatives that WGSL/naga forbid outside fragment shaders (Bliss): + // rewrite 2-argument non-shadow `texture(s, uv)` to `textureLod(s, uv, 0.0)`. + if self.stage != Stage::Fragment && target_name == "texture" && args.len() == 2 { + let is_shadow = match &args[0].content { + ast::ExprData::FunCall(g, _) => { + callee(g).and_then(|c| opaque(&c)).is_some_and(|o| o.3) + } + _ => false, + }; + if !is_shadow { + if let ast::ExprData::FunCall(f, args) = &mut e.content { + *f = ast::FunIdentifierData::Expr(Box::new(var("textureLod"))).into_node(); + args.push(ast::ExprData::FloatConst(0.0).into_node()); + } + } } } @@ -1589,6 +1787,9 @@ impl<'a> Legaliser<'a> { if self.nan { out.push_str("float iris_sqrt(float x) { return sqrt(x); }\n"); } + if self.mat2_ctor { + out.push_str(MAT2_HELPERS); + } if !self.link.uniforms.is_empty() { out.push_str("layout(std140, set = 0, binding = 0) uniform IrisUniforms {\n"); for (_, decl) in &self.link.uniforms { diff --git a/crates/horizon-shaderpack/tests/translate.rs b/crates/horizon-shaderpack/tests/translate.rs index 3c71d8546..9198f8b92 100644 --- a/crates/horizon-shaderpack/tests/translate.rs +++ b/crates/horizon-shaderpack/tests/translate.rs @@ -292,14 +292,48 @@ fn reports_errors_with_their_place() { /// What naga refuses and drivers accept: `vec % scalar` (Noble), `sqrt` of /// a negative constant (Bloop's NaN), constants naga can't fold (Bliss's -/// `?:`, `mix`), and 16-bit types (Noble on NVIDIA). +/// `?:`, `mix`), 16-bit types (Noble on NVIDIA), `mat2(vec4)` (Bloop), +/// `1 << 31` (Rethinking Voxels), vertex `texture2D` without LOD (Bliss), +/// nested array varyings (Noble), and struct varyings (Photon). #[test] fn legalises_what_naga_refuses() { let mut link = Link::default(); + let vs = run( + Stage::Vertex, + "uniform sampler2D colortex0; +struct CloudsParameters { + vec3 color; + float density; +}; +varying CloudsParameters clouds; +varying vec3 grid[2][2]; +void main() { + vec4 s = texture2D(colortex0, vec2(0.5)); + clouds.color = s.rgb; + clouds.density = s.a; + grid[0][0] = s.rgb; + grid[0][1] = s.gbr; + grid[1][0] = s.brg; + grid[1][1] = vec3(1.0); + gl_Position = vec4(0.0, 0.0, 0.0, 1.0); +} +", + &mut link, + ) + .unwrap(); + reparse(&vs.wgsl).unwrap(); + let fs = run( Stage::Fragment, "uniform vec2 uv; +struct CloudsParameters { + vec3 color; + float density; +}; +varying CloudsParameters clouds; +varying vec3 grid[2][2]; const int RES = 256 % 100; +const int SIGN_BIT = 1 << 31; const vec3 TARGET = length(vec3(1.0)) < 0.001 ? vec3(0.0) : normalize(vec3(1.0)); const vec3 TINT = mix(TARGET, vec3(1.0), 0.5); const vec3 HALF = TINT * 0.5; @@ -309,7 +343,9 @@ void main() { ivec2 texel = ivec2(uv) % RES; texel %= 7; uint packed = packFloat2x16(f16vec2(HALF.xy)); - gl_FragColor = vec4(vec2(texel), float(pack(packed).x), nan()); + mat2 rot = mat2(vec4(1.0, 0.0, 0.0, 1.0)); + vec2 r = rot * vec2(clouds.density, float(SIGN_BIT != 0)); + gl_FragColor = vec4(vec2(texel) + r + clouds.color.xy + grid[1][0].xy, float(pack(packed).x), nan()); } ", &mut link, @@ -325,6 +361,7 @@ void main() { "uvec2 pack(uint v)", "return iris_sqrt(-1.);", "uint packed = packHalf2x16(vec2(HALF.xy));", + "iris_mat2(", ] { assert!(fs.glsl.contains(wanted), "{wanted}\n{}", fs.glsl); } diff --git a/docs/shader-packs.md b/docs/shader-packs.md index d126d5a2a..0ba8981fb 100644 --- a/docs/shader-packs.md +++ b/docs/shader-packs.md @@ -13,16 +13,16 @@ Both are off by default. With no pack selected the renderer draws exactly what i | Part | State | | --- | --- | | Renderer layout (below) | today's code | -| Horizon shader packs: `composite` stage, uniforms, hot reload, Shader Packs page | done (the page: list, pick, open the folder) | -| Horizon shader packs: `deferred`, `gbuffer`, `shadow` stages, custom textures | planned | -| Java shader packs: preprocessing + GLSL parse coverage | spike done: 450 of 470 programs reach valid WGSL (below) | -| Java shader packs: preprocessor (`crates/horizon-shaderpack`: includes, options, Iris's macros, JCPP's `#if` and `#define` rules) | done: every program of the three test packs preprocesses; ten packs tested (Compatibility, below) | -| Java shader packs: GLSL → WGSL (`horizon_shaderpack::translate`: syntax-tree legaliser, naga) | done: the 464 programs the three test packs enable by default translate (below) | -| Java shader packs: `shaders.properties` (`horizon_shaderpack::properties`: screens, sliders, profiles, `program.*.enabled`, image formats, blend modes) | done, no screen yet: read like Iris for the three test packs | -| Java shader packs: full-screen pass plan (`horizon_shaderpack::pipeline`: `DRAWBUFFERS`/`RENDERTARGETS`, `colortexN` formats and clearing, buffer flips) | done: planned for the three test packs | -| Java shader packs: full-screen passes drawn (`java_pack.rs`, `gfx/java_stages.rs`: `begin`, `prepare`, `deferred*`, `composite*`, `final` over Horizon's world) | first slice: BSL and Complementary run (below) | -| Java shader packs: custom uniforms (`horizon_shaderpack::uniforms`), `sunPathRotation`, `clouds`/`sun`/`moon`, eye light | done for the full-screen passes (below) | -| Java shader packs: `gbuffers_*`, `shadow`, custom textures, images, compute, option screen | planned | +| Horizon shader packs: `composite`, `deferred`, `gbuffer`, `shadow`, `final` stages (API 2), uniforms, custom textures, hot reload | done (`examples/shaderpacks/horizon-ultra` + `horizon-sample`) | +| Shader Packs UI: import (`.zip`/folder dialog + drag-and-drop), profiles, sub-screens, sliders, toggles, reset to defaults | done (Horizon & Classic Video/Graphics settings) | +| Real-time shadow map pass (`"Horizon shadow"`, `shadowtex0/1`, `shadowModelView`, `shadowProjection`) & pre-translucent depth (`depthtex1/2`) | done | +| G-buffer normal & material pass (`"Horizon gbuffer"`, `scene_normal`, `scene_material`, `colortex1..3` initialization) | done | +| Java shader packs: preprocessor (`crates/horizon-shaderpack`: includes, options, Iris's macros, JCPP's `#if` and `#define` rules, `.lang` files) | done: ten packs tested (Compatibility, below) | +| Java shader packs: GLSL → WGSL (`horizon_shaderpack::translate`: syntax-tree legaliser, naga, 2D array & struct varyings, vertex `textureLod`, `mat2(vec4)`, `1 << 31` shifts) | done | +| Java shader packs: `shaders.properties` (`horizon_shaderpack::properties`: screens, sliders, profiles, `program.*.enabled`, image formats, blend modes, custom textures) | done, wired to UI and GPU passes | +| Java shader packs: full-screen pass plan (`horizon_shaderpack::pipeline`: `DRAWBUFFERS`/`RENDERTARGETS`, `colortexN` formats and clearing, buffer flips, blend state) | done | +| Java shader packs: full-screen passes drawn (`java_pack.rs`, `gfx/java_stages.rs`: `begin`, `prepare`, `deferred*`, `composite*`, `final` over Horizon's world) | done: with shadow maps, `depthtex1/2`, custom PNG textures, GL blend modes, `wetness`, `centerDepthSmooth` | +| Java shader packs: custom uniforms (`horizon_shaderpack::uniforms`), `sunPathRotation`, `clouds`/`sun`/`moon`, eye light | done | ## How the renderer draws a frame today diff --git a/examples/shaderpacks/horizon-ultra/LICENSE b/examples/shaderpacks/horizon-ultra/LICENSE new file mode 100644 index 000000000..44c359a50 --- /dev/null +++ b/examples/shaderpacks/horizon-ultra/LICENSE @@ -0,0 +1,13 @@ +MIT License + +Copyright (c) 2026 Horizon contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. diff --git a/examples/shaderpacks/horizon-ultra/pack.toml b/examples/shaderpacks/horizon-ultra/pack.toml new file mode 100644 index 000000000..446115073 --- /dev/null +++ b/examples/shaderpacks/horizon-ultra/pack.toml @@ -0,0 +1,134 @@ +# Horizon Ultra: multi-pass API 2 WGSL shader pack with PCF directional shadows, +# G-buffer GTAO, water SSR & absorption, volumetric godrays, HDR bloom and ACES tonemapping. +[pack] +name = "Horizon Ultra" +description = "PCF sun/moon shadows, GTAO, water reflections, volumetric godrays, bloom and ACES filmic tonemapping." +authors = ["Horizon contributors"] +version = "2.0.0" +license = "MIT" +api = 2 + +[[stage]] +kind = "deferred" +shader = "shaders/deferred_lighting.wgsl" +format = "rgba16float" + +[[stage]] +kind = "composite" +shader = "shaders/water_reflections.wgsl" +format = "rgba16float" + +[[stage]] +kind = "composite" +shader = "shaders/bloom_godrays.wgsl" +scale = 0.5 +format = "rgba16float" +mipmap = true + +[[stage]] +kind = "final" +shader = "shaders/final_tonemap.wgsl" + +[profiles.low] +shadows = false +ao_strength = 0.25 +water_ssr = false +godrays = false +bloom_strength = 0.15 +tonemap_aces = true +vignette = false + +[profiles.medium] +shadows = true +shadow_strength = 0.70 +ao_strength = 0.50 +water_ssr = true +godrays = true +bloom_strength = 0.25 +tonemap_aces = true +vignette = true + +[profiles.high] +shadows = true +shadow_strength = 0.85 +ao_strength = 0.75 +water_ssr = true +godrays = true +bloom_strength = 0.35 +tonemap_aces = true +vignette = true + +[profiles.ultra] +shadows = true +shadow_strength = 1.0 +ao_strength = 1.0 +water_ssr = true +godrays = true +bloom_strength = 0.45 +tonemap_aces = true +vignette = true + +[options.shadows] +label = "Directional Shadows" +description = "Cast real-time sun and moon PCF shadows across terrain and entities." +default = true + +[options.shadow_strength] +label = "Shadow Strength" +description = "Intensity of directional sun and moon shadows." +default = 0.85 +min = 0.0 +max = 1.0 +step = 0.05 + +[options.ao_strength] +label = "Ambient Occlusion (GTAO)" +description = "Screen-space horizon ambient occlusion in corners and crevices." +default = 0.75 +min = 0.0 +max = 1.5 +step = 0.05 + +[options.water_ssr] +label = "Water Reflections & Refraction" +description = "Screen-space sky and terrain reflections on water and translucent surfaces." +default = true + +[options.godrays] +label = "Volumetric Godrays" +description = "Atmospheric light shafts cast through trees and terrain towards the sun." +default = true + +[options.bloom_strength] +label = "HDR Bloom" +description = "Soft glow around bright sky, lava, torches and emissive blocks." +default = 0.35 +min = 0.0 +max = 1.0 +step = 0.05 + +[options.exposure] +label = "Exposure" +description = "Overall scene exposure multiplier before filmic tonemapping." +default = 1.10 +min = 0.5 +max = 2.0 +step = 0.05 + +[options.saturation] +label = "Color Saturation" +description = "Color vibrancy of the final tonemapped image." +default = 1.12 +min = 0.0 +max = 2.0 +step = 0.05 + +[options.tonemap_aces] +label = "ACES Filmic Tonemap" +description = "Apply ACES fitted filmic curve to preserve highlight detail." +default = true + +[options.vignette] +label = "Cinematic Vignette" +description = "Subtle darkening at the edges of the viewport." +default = true diff --git a/examples/shaderpacks/horizon-ultra/shaders/bloom_godrays.wgsl b/examples/shaderpacks/horizon-ultra/shaders/bloom_godrays.wgsl new file mode 100644 index 000000000..534e9d983 --- /dev/null +++ b/examples/shaderpacks/horizon-ultra/shaders/bloom_godrays.wgsl @@ -0,0 +1,61 @@ +// Stage 3 (Composite, half-resolution): HDR bloom extraction + 9-tap tent filter +// and radial volumetric light shafts (godrays) towards the sun/moon. +override godrays: bool = true; +override bloom_strength: f32 = 0.35; + +@fragment fn fs_main(in: StageIn) -> @location(0) vec4 { + let base = textureSampleLevel(previous, linear_sampler, in.uv, 0.0).rgb; + let texel = frame.resolution.zw * 2.0; + + // 9-tap soft bloom filter around bright highlights. + var bloom = vec3(0.0); + if bloom_strength > 0.001 { + let offsets = array, 9>( + vec2(0.0, 0.0), + vec2(1.0, 0.0), + vec2(-1.0, 0.0), + vec2(0.0, 1.0), + vec2(0.0, -1.0), + vec2(1.0, 1.0), + vec2(-1.0, 1.0), + vec2(1.0, -1.0), + vec2(-1.0, -1.0), + ); + let weights = array(0.25, 0.125, 0.125, 0.125, 0.125, 0.0625, 0.0625, 0.0625, 0.0625); + for (var i = 0u; i < 9u; i++) { + let sample_c = textureSampleLevel(previous, linear_sampler, in.uv + offsets[i] * texel, 0.0).rgb; + let lum = luminance(sample_c); + let bright = max(lum - 0.72, 0.0) / max(lum, 1e-4); + bloom += sample_c * bright * weights[i]; + } + } + + // Radial volumetric godrays cast from the sun's screen projection. + var shafts = vec3(0.0); + if godrays && frame.camera_state.x == 0.0 && frame.dimension.y > 0.5 { + let sun_clip = frame.projection * (frame.view * vec4(frame.sun.xyz * 128.0, 1.0)); + if sun_clip.w > 0.0 { + let sun_ndc = sun_clip.xy / sun_clip.w; + let sun_uv = vec2(sun_ndc.x * 0.5 + 0.5, 0.5 - sun_ndc.y * 0.5); + let delta = (sun_uv - in.uv) / 12.0; + var uv_Cursor = in.uv; + var transmittance = 0.0; + var decay = 1.0; + for (var s = 0u; s < 12u; s++) { + uv_Cursor += delta; + let sample_uv = clamp(uv_Cursor, vec2(0.001), vec2(0.999)); + if is_sky(depth_at(sample_uv)) { + transmittance += decay; + } + decay *= 0.92; + } + let ray = normalize(world_offset(in.uv, 1e-5)); + let sun_facing = pow(max(dot(ray, frame.sun.xyz), 0.0), 4.0); + let sunset = smoothstep(-0.1, 0.25, frame.sun.y); + let shaft_color = mix(vec3(1.0, 0.55, 0.25), vec3(1.0, 0.92, 0.75), sunset); + shafts = shaft_color * (transmittance / 12.0) * sun_facing * 0.28 * (1.0 - frame.weather.x); + } + } + + return vec4(base + bloom * bloom_strength + shafts, 1.0); +} diff --git a/examples/shaderpacks/horizon-ultra/shaders/deferred_lighting.wgsl b/examples/shaderpacks/horizon-ultra/shaders/deferred_lighting.wgsl new file mode 100644 index 000000000..3930a5c21 --- /dev/null +++ b/examples/shaderpacks/horizon-ultra/shaders/deferred_lighting.wgsl @@ -0,0 +1,92 @@ +// Stage 1 (Deferred): G-buffer normal reconstruction, multi-tap GTAO ambient +// occlusion, directional sun/moon PCF shadow mapping, and rain puddle wetness. +override shadows: bool = true; +override shadow_strength: f32 = 0.85; +override ao_strength: f32 = 0.75; + +fn compute_gtao(uv: vec2, depth: f32, view_pos: vec3) -> f32 { + if ao_strength <= 0.001 { + return 1.0; + } + let step_uv = frame.resolution.zw * 2.5; + let offsets = array, 8>( + vec2(1.0, 0.0), + vec2(-1.0, 0.0), + vec2(0.0, 1.0), + vec2(0.0, -1.0), + vec2(0.707, 0.707), + vec2(-0.707, 0.707), + vec2(0.707, -0.707), + vec2(-0.707, -0.707), + ); + var occlusion = 0.0; + for (var i = 0u; i < 8u; i++) { + let sample_uv = clamp(uv + offsets[i] * step_uv, vec2(0.001), vec2(0.999)); + let sd = opaque_depth_at(sample_uv); + if !is_sky(sd) { + let sp = view_position(sample_uv, sd); + let diff = sp - view_pos; + let dist = length(diff); + if dist > 0.02 && dist < 3.5 { + let horizon = clamp(diff.z / dist, 0.0, 1.0); + occlusion += horizon * smoothstep(3.5, 0.2, dist); + } + } + } + return clamp(1.0 - (occlusion * 0.18 * ao_strength), 0.15, 1.0); +} + +@fragment fn fs_main(in: StageIn) -> @location(0) vec4 { + let base_srgb = textureSampleLevel(scene_color, linear_sampler, in.uv, 0.0).rgb; + var color = srgb_to_linear(base_srgb); + let depth = opaque_depth_at(in.uv); + let light_dir = normalize(select(frame.moon.xyz, frame.sun.xyz, frame.sun.y >= -0.05)); + let sun_elevation = clamp(frame.sun.y, -1.0, 1.0); + let day_factor = smoothstep(-0.08, 0.18, sun_elevation); + let sunset_factor = smoothstep(-0.12, 0.05, sun_elevation) * (1.0 - smoothstep(0.05, 0.35, sun_elevation)); + + if is_sky(depth) { + let ray = normalize(world_offset(in.uv, 1e-5)); + let sun_dot = max(dot(ray, frame.sun.xyz), 0.0); + let mie = pow(sun_dot, 12.0) * (0.35 + 0.65 * sunset_factor) * (1.0 - frame.weather.x * 0.8); + let sun_tint = mix(vec3(1.0, 0.88, 0.68), vec3(1.0, 0.45, 0.18), sunset_factor); + color += sun_tint * mie * 0.85; + return vec4(color, 1.0); + } + + let world_rel = world_offset(in.uv, depth); + let view_pos = view_position(in.uv, depth); + let normal = world_normal_at(in.uv); + let mat = material_at(in.uv); + + // Ambient occlusion from depth discontinuity neighborhood. + let ao = compute_gtao(in.uv, depth, view_pos); + + // Directional sun/moon shadow factor. + var shadow = 1.0; + if shadows && frame.dimension.y > 0.5 { + let raw_shadow = sample_shadow(world_rel, normal); + shadow = mix(1.0, raw_shadow, shadow_strength); + } + + let ndotl = max(dot(normal, light_dir), 0.0); + let sun_color = mix( + vec3(0.22, 0.28, 0.48), + mix(vec3(1.05, 0.96, 0.86), vec3(1.18, 0.58, 0.28), sunset_factor), + day_factor, + ); + let sky_ambient = mix(vec3(0.08, 0.11, 0.20), srgb_to_linear(frame.sky_color.rgb) * 0.55, day_factor); + let direct_light = sun_color * ndotl * shadow * (1.0 - frame.weather.x * 0.55); + let bounce_light = sky_ambient * (0.45 + 0.55 * max(normal.y, 0.0)) * ao; + + // Wetness specular sheen during rain on upward-facing surfaces. + let wetness = frame_ext.environment.z; + let view_dir = normalize(-world_rel); + let half_vec = normalize(light_dir + view_dir); + let wet_mask = wetness * smoothstep(0.65, 0.95, normal.y) * mat.g; + let spec = pow(max(dot(normal, half_vec), 0.0), 48.0) * wet_mask * shadow * 0.65; + + let shade_boost = mix(0.68 * ao, 1.0 + 0.35 * ndotl * shadow, 0.65); + color = color * shade_boost + color * direct_light * 0.32 + bounce_light * 0.08 + sun_color * spec; + return vec4(max(color, vec3(0.0)), 1.0); +} diff --git a/examples/shaderpacks/horizon-ultra/shaders/final_tonemap.wgsl b/examples/shaderpacks/horizon-ultra/shaders/final_tonemap.wgsl new file mode 100644 index 000000000..397abafa9 --- /dev/null +++ b/examples/shaderpacks/horizon-ultra/shaders/final_tonemap.wgsl @@ -0,0 +1,29 @@ +// Stage 4 (Final): Combines full-resolution scene with bloom/godrays, applies +// exposure, ACES filmic tonemapping, saturation, and cinematic vignette. +override exposure: f32 = 1.10; +override saturation: f32 = 1.12; +override tonemap_aces: bool = true; +override vignette: bool = true; + +@fragment fn fs_main(in: StageIn) -> @location(0) vec4 { + var hdr = textureSampleLevel(previous, linear_sampler, in.uv, 0.0).rgb * exposure; + + if tonemap_aces { + hdr = aces_tonemap(hdr); + } else { + hdr = clamp(hdr, vec3(0.0), vec3(1.0)); + } + + var srgb = linear_to_srgb(hdr); + let luma = luminance(srgb); + let rain_desat = 1.0 - 0.25 * max(frame.weather.x, frame.weather.y); + srgb = mix(vec3(luma), srgb, saturation * rain_desat); + + if vignette { + let aspect = frame.resolution.x * frame.resolution.w; + let d = length((in.uv - 0.5) * vec2(aspect, 1.0)); + srgb *= 1.0 - 0.26 * smoothstep(0.48, 0.98, d); + } + + return vec4(clamp(srgb, vec3(0.0), vec3(1.0)), 1.0); +} diff --git a/examples/shaderpacks/horizon-ultra/shaders/water_reflections.wgsl b/examples/shaderpacks/horizon-ultra/shaders/water_reflections.wgsl new file mode 100644 index 000000000..853cf78f5 --- /dev/null +++ b/examples/shaderpacks/horizon-ultra/shaders/water_reflections.wgsl @@ -0,0 +1,58 @@ +// Stage 2 (Composite): Screen-space water reflections (SSR), Fresnel Schlick, +// and Beer-Lambert underwater depth absorption using scene_depth vs opaque_depth. +override water_ssr: bool = true; + +@fragment fn fs_main(in: StageIn) -> @location(0) vec4 { + var color = textureSampleLevel(previous, linear_sampler, in.uv, 0.0).rgb; + let surface_depth = depth_at(in.uv); + let floor_depth = opaque_depth_at(in.uv); + let mat = material_at(in.uv); + + // Translucent water/glass layer exists where surface_depth is closer than floor_depth + // (reversed depth: closer = larger value) or where the G-buffer water mask is set. + let is_translucent = (surface_depth > floor_depth + 1e-5) || (mat.b > 0.5); + if !water_ssr || is_sky(surface_depth) || !is_translucent { + return vec4(color, 1.0); + } + + let water_pos = world_offset(in.uv, surface_depth); + let floor_pos = world_offset(in.uv, max(floor_depth, 1e-6)); + let water_column = clamp(length(floor_pos - water_pos), 0.0, 32.0); + + // Beer-Lambert depth absorption in deep water. + let absorption = exp(-vec3(0.22, 0.07, 0.03) * water_column * 0.35); + color *= mix(vec3(1.0), absorption, clamp(water_column * 0.08, 0.0, 0.65)); + + // Wave normal perturbation and Fresnel reflection. + let world_xz = (water_pos.xz + frame.camera_position.xz) * 0.08; + let wave_uv = world_xz + vec2(frame.time.x * 0.015, -frame.time.x * 0.011); + let noise_sample = textureSampleLevel(noise_texture, repeat_sampler, wave_uv, 0.0).rgb * 2.0 - 1.0; + let normal = normalize(vec3(noise_sample.x * 0.06, 1.0, noise_sample.y * 0.06)); + let view_dir = normalize(-water_pos); + let refl_dir = reflect(-view_dir, normal); + let fresnel = clamp(0.04 + 0.96 * pow(1.0 - max(dot(view_dir, normal), 0.0), 4.0), 0.0, 0.85); + + // Ray-march 8 steps in screen space along the reflection ray. + var refl_color = srgb_to_linear(frame.sky_color.rgb); + let sun_spec = pow(max(dot(refl_dir, frame.sun.xyz), 0.0), 96.0) * 2.5 * (1.0 - frame.weather.x); + refl_color += vec3(1.0, 0.9, 0.7) * sun_spec; + + for (var step = 1u; step <= 8u; step++) { + let march_pos = water_pos + refl_dir * (f32(step) * 2.2); + let clip = frame.projection * (frame.view * vec4(march_pos, 1.0)); + if clip.w > 0.0 { + let ndc = clip.xyz / clip.w; + let sample_uv = vec2(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5); + if all(sample_uv >= vec2(0.01)) && all(sample_uv <= vec2(0.99)) { + let scene_d = opaque_depth_at(sample_uv); + if !is_sky(scene_d) && ndc.z <= scene_d && (scene_d - ndc.z) < 0.08 { + refl_color = textureSampleLevel(previous, linear_sampler, sample_uv, 0.0).rgb; + break; + } + } + } + } + + color = mix(color, refl_color, fresnel * 0.55); + return vec4(color, 1.0); +} From d522d8fe50bcd275e95629886e17be406bc40bce Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 08:38:58 +0200 Subject: [PATCH 03/39] Saved state: burning, air and falling survive a reopen; InLove for every animal - Guests' player data saves Fire, Air and fall_distance and puts them back on their body when they come back (single player's players are guests of the integrated server), so a burning player still burns after a reopen (#1323); the old single player's level.dat keeps fall_distance too (#1327). - Every Animal saves and loads InLove, so a fed animal stays in love across a reopen and is not fed again (#1396). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/entity_nbt.rs | 8 +- crates/horizon-client/src/game.rs | 1 + crates/horizon-client/src/game/lan_actions.rs | 6 ++ crates/horizon-client/src/lan/guest.rs | 29 +++++++ crates/horizon-client/src/lan/playerdata.rs | 80 ++++++++++++++++++- crates/horizon-client/src/mobs/rabbit.rs | 12 +++ crates/horizon-client/src/motion.rs | 1 + 7 files changed, 135 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index aaccdd844..7faa94e21 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -175,12 +175,15 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag { for (key, value) in [ ("Age", Tag::Int(0)), ("ForcedAge", Tag::Int(0)), - ("InLove", Tag::Int(0)), ("AgeLocked", Tag::Byte(0)), ] { map.entry(key.into()).or_insert(value); } } + // Animal.addAdditionalSaveData: the love ticks left, whatever the save had. + if m.kind.flags.animal { + map.insert("InLove".into(), Tag::Int(m.extra.in_love.max(0))); + } match id { "minecraft:creeper" => { for (key, value) in [("Fuse", Tag::Short(30)), ("ExplosionRadius", Tag::Byte(3))] { @@ -621,6 +624,9 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap>, id: u32) - m.extra.forced_age = number(tag.get("ForcedAge")).unwrap_or(0) as i32; m.extra.age_locked = flag(tag, "AgeLocked"); } + if m.kind.flags.animal { + m.extra.in_love = number(tag.get("InLove")).unwrap_or(0) as i32; + } m.name_visible = flag(tag, "CustomNameVisible"); m.no_ai = flag(tag, "NoAI"); m.no_gravity = flag(tag, "NoGravity"); diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 0faca0ab4..6a2ebcf25 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -9534,6 +9534,7 @@ impl Game<'_> { ); map.insert("Air".into(), horizon_world::nbt::Tag::Short(p.air as i16)); } + level.fall_distance = p.fall_distance; level.food = p.health.food.food; level.saturation = p.health.food.saturation; diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 67764c090..77d68c6b9 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -217,6 +217,12 @@ impl Game<'_> { host.hub.guests[i].position[2], ); host.hub.guests[i].motion = PlayerMotion::new(self.motion, p).ok().map(Box::new); + // The first body after joining burns, breathes and falls on as saved. + if let Some(saved) = host.hub.guests[i].loaded_body.take() + && let Some(m) = host.hub.guests[i].motion.as_mut() + { + saved.apply(m); + } let kept = host.hub.guests[i].logs_kept.take(); if let (Some(m), Some((hurts, attacks))) = (host.hub.guests[i].motion.as_mut(), kept) { m.hurt_log = hurts; diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index 948de7e20..227cd4335 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -18,6 +18,31 @@ pub const CREATIVE: i32 = 1; pub const ADVENTURE: i32 = 2; pub const SPECTATOR: i32 = 3; +/// The body state a player's file keeps that lives on `PlayerMotion`. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct SavedBody { + pub fire_ticks: i32, + pub air: i32, + pub fall_distance: f64, +} + +impl SavedBody { + /// The body state of `motion`, to save. + pub fn of(motion: &PlayerMotion) -> Self { + Self { + fire_ticks: motion.fire_ticks, + air: motion.air, + fall_distance: motion.fall_distance, + } + } + /// Put it on a new body. + pub fn apply(self, motion: &mut PlayerMotion) { + motion.fire_ticks = self.fire_ticks; + motion.air = self.air; + motion.fall_distance = self.fall_distance; + } +} + /// An effect as last sent in ClientboundUpdateMobEffectPacket. #[derive(Clone, Debug, PartialEq)] pub struct SentEffect { @@ -133,6 +158,9 @@ pub struct Guest { pub tracker: Tracker, // Player state the gameplay code reads and writes. pub motion: Option>, + /// What the player's file says of the body (`Entity.load`: the fire + /// and air ticks and the fall distance), for the first body it gets. + pub loaded_body: Option, /// The hurt and attack logs of the body it had before it respawned, /// until the new body takes them over (the control socket's `hurtlog` /// reads across a death). @@ -275,6 +303,7 @@ impl Guest { batch_quota: 0.0, tracker: Tracker::default(), motion: None, + loaded_body: None, logs_kept: None, inventory: vec![None; 36], armor: [None, None, None, None], diff --git a/crates/horizon-client/src/lan/playerdata.rs b/crates/horizon-client/src/lan/playerdata.rs index 24a4a9d65..180074f84 100644 --- a/crates/horizon-client/src/lan/playerdata.rs +++ b/crates/horizon-client/src/lan/playerdata.rs @@ -1,6 +1,6 @@ //! Guests' saved state: `players/data/.dat` in the vanilla player //! compound, through the same code that saves the host. -use super::guest::Guest; +use super::guest::{Guest, SavedBody}; use crate::{ items::ItemStack, save::{Level, read_dat, write_dat}, @@ -74,6 +74,17 @@ pub fn apply(guest: &mut Guest, level: &Level) { guest.spawn = level.spawn.map(|s| [s[0] + 0.5, s[1], s[2] + 0.5]); guest.respawn_elsewhere = level.respawn_elsewhere.clone(); guest.saved = level.player_extra.clone(); + let extra = level.player_extra.as_ref(); + let short = |key: &str, default: i64| { + extra + .and_then(|t| crate::save::number(t.get(key))) + .unwrap_or(default) as i32 + }; + guest.loaded_body = Some(SavedBody { + fire_ticks: short("Fire", -20), + air: short("Air", 300), + fall_distance: level.fall_distance, + }); guest.recipe_book = level.recipe_book.clone(); guest.dimension = match level.dimension.as_str() { d @ ("minecraft:the_nether" | "minecraft:the_end") => d.to_owned(), @@ -139,6 +150,24 @@ pub fn store(guest: &Guest, level: &mut Level) { level.respawn_elsewhere = guest.respawn_elsewhere.clone(); level.on_ground = guest.on_ground; level.player_extra = guest.saved.clone(); + // The live body's fire, air and fall distance (Entity.saveWithoutId), + // or what the file had while the player has no body yet. + let body = guest + .motion + .as_deref() + .map(SavedBody::of) + .or(guest.loaded_body); + if let Some(body) = body { + level.fall_distance = body.fall_distance; + let extra = level + .player_extra + .get_or_insert_with(|| horizon_world::nbt::Tag::Compound(Default::default())); + if let horizon_world::nbt::Tag::Compound(map) = extra { + let short = |v: i32| horizon_world::nbt::Tag::Short(v.clamp(-32768, 32767) as i16); + map.insert("Fire".into(), short(body.fire_ticks)); + map.insert("Air".into(), short(body.air)); + } + } level.recipe_book = guest.recipe_book.clone(); level.ender_items = guest.ender_items.clone(); level.inventory.clear(); @@ -162,3 +191,52 @@ pub fn store(guest: &Guest, level: &mut Level) { put(100, &guest.armor[3]); put(150, &guest.offhand); } + +#[cfg(test)] +mod tests { + use super::*; + use crate::lan::{Joined, PlayerSender}; + use horizon_protocol::packets::configuration::ClientInformation; + use std::sync::{Arc, atomic::AtomicI32, mpsc}; + + fn guest() -> Guest { + let (tx, _rx) = mpsc::channel(); + Guest::new( + Joined { + conn: 2, + uuid: 2, + name: "Alex".into(), + properties: Vec::new(), + info: ClientInformation::new(8), + brand: "test".into(), + sender: PlayerSender::new(tx, Arc::default()), + latency: Arc::new(AtomicI32::new(0)), + address: "127.0.0.1:25565".parse().unwrap(), + }, + 2, + [0.0, 64.0, 0.0], + 8, + ) + } + + #[test] + fn a_saved_burn_and_fall_come_back() { + // A player saved burning in mid-fall (#1323, #1327): Entity.saveWithoutId's + // Fire, Air and fall_distance, read back for the next body. + let mut g = guest(); + g.loaded_body = Some(SavedBody { + fire_ticks: 96, + air: 250, + fall_distance: 9.5, + }); + let mut level = Level::default(); + store(&g, &mut level); + let tag = level.player_tag(); + let mut read = Level::default(); + read.read_player(&tag); + read.player_extra = Some(tag); + let mut back = guest(); + apply(&mut back, &read); + assert_eq!(back.loaded_body, g.loaded_body); + } +} diff --git a/crates/horizon-client/src/mobs/rabbit.rs b/crates/horizon-client/src/mobs/rabbit.rs index 05ab0e390..a2ab6e95c 100644 --- a/crates/horizon-client/src/mobs/rabbit.rs +++ b/crates/horizon-client/src/mobs/rabbit.rs @@ -310,6 +310,18 @@ mod tests { } } + #[test] + fn love_ticks_survive_a_save() { + // Animal.addAdditionalSaveData / readAdditionalSaveData: "InLove" (#1396). + let mut m = rabbit(); + m.extra.in_love = 570; + let tag = crate::entity_nbt::mob_to_nbt(&m, [0, 0, 0, 1], None); + assert_eq!(tag.get("InLove"), Some(&horizon_world::nbt::Tag::Int(570))); + let types = HashMap::from([(m.kind.id.clone(), m.kind.clone())]); + let back = crate::entity_nbt::mob_from_nbt(&tag, &types, 2).unwrap(); + assert_eq!(back.extra.in_love, 570); + } + #[test] fn landing_waits_then_real_hop_clock_moves_the_model() { let mut m = rabbit(); diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index 1920026b9..381cf8f88 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -337,6 +337,7 @@ impl PlayerMotion { self.air = extra .and_then(|t| crate::save::number(t.get("Air"))) .unwrap_or(300) as i32; + self.fall_distance = level.fall_distance; self.set_health(level.health, false)?; self.health.food.food = level.food; self.health.food.saturation = level.saturation; From 06b4c7faf52df6b3c12c40e75f7a3e5bba40103d Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 08:38:58 +0200 Subject: [PATCH 04/39] Rules: crits, mending, phantoms, per-level health rules, /item with air, frozen potions - Critical hits also need the attacker out of water, not blind and not riding (Player.canCriticalAttack) (#1393). - Mending repairs the off hand too: candidates in EquipmentSlot order, main hand, off hand, then the armour (#1349). - Phantoms spawn only in the Overworld with spawn_mobs, spawn_monsters and spawn_phantoms on, near every player (the local one and each guest in the level, with their own time since rest), with vanilla's draw order (#1388, #1387). - A level entered by a portal or a dimension change keeps the world's natural_health_regeneration (#1339). - /item replace ... with air empties the slot, and counts it as changed as vanilla's SlotAccess.set does (#1332). - Thrown potions stay where they are on frozen ticks (/tick freeze) and land once the game runs again (#1364). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 18 +++-- .../horizon-client/src/game/commands/items.rs | 5 +- .../src/game/enchant_effects.rs | 67 +++++++++++-------- crates/horizon-client/src/game/levels.rs | 3 + crates/horizon-client/src/game/potions.rs | 10 ++- crates/horizon-client/src/game/stats.rs | 67 ++++++++++++++----- 6 files changed, 117 insertions(+), 53 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 0faca0ab4..93f90faf5 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -4292,7 +4292,7 @@ impl Game<'_> { /// world's entities and blocks, and the network hub. fn tick_world(&mut self, running: bool) -> Result<()> { self.update_chunk_tickets(); - horizon_client::spans::time("tick_effects", || self.tick_effects()); + horizon_client::spans::time("tick_effects", || self.tick_effects(running)); horizon_client::spans::time("tick_using", || self.tick_using()); horizon_client::spans::time("tick_hud", || self.tick_hud()); // The player's digging comes in with its packets, before the level @@ -7020,10 +7020,16 @@ impl Game<'_> { // Item.getAttackDamageBonus (a mace's smash), before the critical hit. amount += self.smash_damage_bonus(target_kind); let player = self.player.as_ref(); - // Player.canCriticalAttack: falling, off the ground, not sprinting. - let falling = - player.is_some_and(|p| !p.ground.on_ground && p.fall_distance > 0.0 && !p.sprinting); - let crit = scale > 0.9 && falling; + // Player.canCriticalAttack: falling, off the ground, not in water, + // not blind (isMobilityRestricted), not riding, not sprinting. + let falling = player.is_some_and(|p| { + !p.ground.on_ground + && p.fall_distance > 0.0 + && !p.fluids.was_touching_water + && !p.sprinting + }); + let restricted = self.potions.effects.get("blindness").is_some() || self.riding.is_some(); + let crit = scale > 0.9 && falling && !restricted; if crit { amount *= 1.5; } @@ -7632,6 +7638,8 @@ impl Game<'_> { world.game_time = self.world.game_time; world.day_time = self.world.day_time; world.weather = self.world.weather; + // The game rules and the difficulty hold in every dimension. + world.health_rules = self.world.health_rules; // An open hub keeps hearing the new level's block changes. world.block_log = self.world.block_log.as_ref().map(|_| Vec::new()); world.light_log = self.world.light_log.as_ref().map(|_| Default::default()); diff --git a/crates/horizon-client/src/game/commands/items.rs b/crates/horizon-client/src/game/commands/items.rs index b5c6e63d9..ab2d9000b 100644 --- a/crates/horizon-client/src/game/commands/items.rs +++ b/crates/horizon-client/src/game/commands/items.rs @@ -810,7 +810,10 @@ impl Game<'_> { None => break, }, }; - if self.slot_get(slot) != item && self.slot_set(slot, item) { + // ItemStack.isEmpty: air (or a count of 0) is an empty slot. + // SlotAccess.set counts every slot it sets, the same item too. + let item = item.filter(|s| &*s.item != "minecraft:air" && s.count > 0); + if self.slot_set(slot, item) { changed += 1; } } diff --git a/crates/horizon-client/src/game/enchant_effects.rs b/crates/horizon-client/src/game/enchant_effects.rs index 16bd38904..8593e2b98 100644 --- a/crates/horizon-client/src/game/enchant_effects.rs +++ b/crates/horizon-client/src/game/enchant_effects.rs @@ -8,6 +8,15 @@ use super::Game; use horizon_client::{enchant::EffectContext, items::ItemStack}; use horizon_core::random::RandomSource; +/// Where Mending found a damaged item (`EnchantedItemInUse.inSlot`). +#[derive(Clone, Copy)] +enum MendSlot { + Main, + Off, + /// An `armor` index (head first). + Armor(usize), +} + impl Game<'_> { /// `Player.getDestroySpeed` after the tool speed: mining efficiency, haste /// and mining fatigue. @@ -314,36 +323,29 @@ impl Game<'_> { pub(super) fn mending(&mut self, amount: u32) -> u32 { let mut amount = amount; loop { - // EnchantmentHelper.getRandomItemWith(REPAIR_WITH_XP, damaged): held and armor. - let mut candidates: Vec<(bool, usize)> = Vec::new(); - if let Some(s) = &self.inventory[self.selected] - && s.damage > 0 - && s.enchantment_level("minecraft:mending") > 0 - { - candidates.push((false, self.selected)); - } - for (i, s) in self.armor.iter().enumerate() { - if let Some(s) = s - && s.damage > 0 - && s.enchantment_level("minecraft:mending") > 0 - { - candidates.push((true, i)); - } - } + // EnchantmentHelper.getRandomItemWith(REPAIR_WITH_XP, damaged), in + // EquipmentSlot order: the main hand, the off hand, then feet, legs, + // chest and head (`armor` holds head first). + let mending = |s: &Option| { + s.as_ref() + .is_some_and(|s| s.damage > 0 && s.enchantment_level("minecraft:mending") > 0) + }; + let candidates: Vec = [MendSlot::Main, MendSlot::Off] + .into_iter() + .chain((0..self.armor.len()).rev().map(MendSlot::Armor)) + .filter(|&slot| mending(self.mend_slot(slot))) + .collect(); if candidates.is_empty() { return amount; } - let (armor, i) = candidates[self + let slot = candidates[self .world .random .next_int_bound(candidates.len() as i32) .unwrap_or(0) as usize]; - let stack = if armor { - self.armor[i].clone() - } else { - self.inventory[i].clone() + let Some(stack) = self.mend_slot(slot).clone() else { + return amount; }; - let Some(stack) = stack else { return amount }; let ctx = EffectContext { items: Some(self.res.items()), ..Default::default() @@ -356,12 +358,7 @@ impl Game<'_> { amount as f32, ) as u32; let repaired = to_repair_xp.min(stack.damage); - let slot = if armor { - &mut self.armor[i] - } else { - &mut self.inventory[i] - }; - if let Some(s) = slot.as_mut() { + if let Some(s) = self.mend_slot_mut(slot).as_mut() { s.damage -= repaired; } if repaired == 0 || to_repair_xp == 0 { @@ -374,6 +371,20 @@ impl Game<'_> { amount = remaining; } } + fn mend_slot(&self, slot: MendSlot) -> &Option { + match slot { + MendSlot::Main => &self.inventory[self.selected], + MendSlot::Off => &self.offhand, + MendSlot::Armor(i) => &self.armor[i], + } + } + fn mend_slot_mut(&mut self, slot: MendSlot) -> &mut Option { + match slot { + MendSlot::Main => &mut self.inventory[self.selected], + MendSlot::Off => &mut self.offhand, + MendSlot::Armor(i) => &mut self.armor[i], + } + } /// Bow enchantments at release: (extra base damage, infinity keeps the arrow). pub(super) fn bow_enchantments(&mut self) -> (f64, bool) { let Some(bow) = self.inventory[self.selected].clone() else { diff --git a/crates/horizon-client/src/game/levels.rs b/crates/horizon-client/src/game/levels.rs index 3b73a2e20..895cda08c 100644 --- a/crates/horizon-client/src/game/levels.rs +++ b/crates/horizon-client/src/game/levels.rs @@ -232,6 +232,9 @@ impl Game<'_> { p.world.game_time = self.world.game_time; p.world.day_time = self.world.day_time; p.world.weather = self.world.weather; + // The game rules and the difficulty are the server's, not a + // level's: natural regeneration holds in every dimension. + p.world.health_rules = self.world.health_rules; // The sculk tables' watched blocks are built once per game. p.world.watched = std::mem::take(&mut self.world.watched); self.swap_level(&mut p); diff --git a/crates/horizon-client/src/game/potions.rs b/crates/horizon-client/src/game/potions.rs index 72c8fe885..7b466b3bb 100644 --- a/crates/horizon-client/src/game/potions.rs +++ b/crates/horizon-client/src/game/potions.rs @@ -297,10 +297,14 @@ impl Game<'_> { } } /// The local player's effects (`tick_mob_effects`), then the potions - /// in flight, which a server without a local player still has. - pub(super) fn tick_effects(&mut self) { + /// in flight, which a server without a local player still has. A frozen + /// tick (`/tick freeze`) leaves the potions where they are + /// (`TickRateManager.isEntityFrozen`); players' effects still run. + pub(super) fn tick_effects(&mut self, running: bool) { self.tick_mob_effects(); - self.tick_thrown_potions(); + if running { + self.tick_thrown_potions(); + } } /// `LivingEntity.tickEffects` (server side) for `self.player`, the local /// player or a guest acting (`as_guest`), then its attribute modifiers diff --git a/crates/horizon-client/src/game/stats.rs b/crates/horizon-client/src/game/stats.rs index 7a97416ca..7f64c6bb2 100644 --- a/crates/horizon-client/src/game/stats.rs +++ b/crates/horizon-client/src/game/stats.rs @@ -306,9 +306,18 @@ impl Game<'_> { } } /// `PhantomSpawner.tick`: tired players under an open night sky draw - /// phantoms. + /// phantoms. A custom spawner ticks only while `spawn_mobs` is on + /// (`ServerChunkCache.tickChunks`), and this one only spawns enemies + /// (`ServerLevel.isSpawningMonsters`: `spawn_monsters` too). Every + /// player of the level counts: the game's own and the hosted ones (a + /// server thread's players are all hosted). pub(super) fn spawn_phantoms(&mut self) { - if !self.ui.rules.bool("spawn_phantoms") || self.dim != Dim::Overworld { + let rules = &self.ui.rules; + if !rules.bool("spawn_mobs") + || !rules.bool("spawn_monsters") + || !rules.bool("spawn_phantoms") + || self.dim != Dim::Overworld + { return; } self.stats.phantom_tick -= 1; @@ -317,16 +326,42 @@ impl Game<'_> { } let r = &mut self.world.random; self.stats.phantom_tick += (60 + r.next_int_bound(60).unwrap_or(0)) * 20; - if self.sky_darken() < 5 || self.spectator() { + if self.sky_darken() < 5 { return; } - let Some(p) = self.player.as_ref() else { - return; - }; + let rest = |s: &Stats| s.get("custom", "minecraft:time_since_rest"); + // `level.players()`: (block position, time since rest) of each player + // who isn't a spectator. + let mut players = Vec::new(); + if let Some(p) = self.player.as_ref() + && !self.spectator() + { + players.push((p.position, rest(&self.stats))); + } + if let Some(host) = self.lan.as_ref() { + let level = self.dim.id(); + for g in host.hub.guests.iter().filter(|g| { + g.placed + && g.dimension == level + && g.game_mode != horizon_client::lan::guest::SPECTATOR + }) { + let since = host.play.get(&g.conn).map_or(0, |p| rest(&p.stats)); + players.push(( + Vec3::new(g.position[0], g.position[1], g.position[2]), + since, + )); + } + } + for (position, since) in players { + self.spawn_phantoms_near(position, since); + } + } + /// One player's turn of `PhantomSpawner.tick`. + fn spawn_phantoms_near(&mut self, position: Vec3, since_rest: i32) { let pos = [ - p.position.x.floor() as i32, - p.position.y.floor() as i32, - p.position.z.floor() as i32, + position.x.floor() as i32, + position.y.floor() as i32, + position.z.floor() as i32, ]; if pos[1] < 63 || !self.can_see_sky(pos) { return; @@ -336,15 +371,15 @@ impl Game<'_> { if r.next_float() * 3.0 >= 2.0 { return; } - let rest = self.stats.get("custom", "minecraft:time_since_rest").max(1); - if r.next_int_bound(rest).unwrap_or(0) < 72000 { + if r.next_int_bound(since_rest.max(1)).unwrap_or(0) < 72000 { return; } - let at = [ - pos[0] - 10 + r.next_int_bound(21).unwrap_or(10), - pos[1] + 20 + r.next_int_bound(15).unwrap_or(0), - pos[2] - 10 + r.next_int_bound(21).unwrap_or(10), - ]; + // `playerPos.above(20 + nextInt(15)).east(-10 + nextInt(21)).south(...)`: + // the height is drawn first. + let up = 20 + r.next_int_bound(15).unwrap_or(0); + let east = -10 + r.next_int_bound(21).unwrap_or(10); + let south = -10 + r.next_int_bound(21).unwrap_or(10); + let at = [pos[0] + east, pos[1] + up, pos[2] + south]; if !self .res .facts() From 7e5e469783dc63de53e88a21d7925a75fc8b3197 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 09:19:48 +0200 Subject: [PATCH 05/39] InLove only for vanilla's Animal subclasses (not allays) save_roundtrip found an allay saved with InLove 0: Horizon's `animal` flag is broader than vanilla's Animal class. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/entity_nbt.rs | 45 +++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index 7faa94e21..298b54088 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -89,6 +89,47 @@ impl Base<'_> { } /// `LivingEntity` + `Mob` (+ `AgeableMob`, creeper and skeleton specifics). +/// The mobs whose class extends vanilla's `Animal` (and so save `InLove`): +/// not allays, villagers, golems, dolphins or fish. +fn is_animal(id: &str) -> bool { + matches!( + id.strip_prefix("minecraft:").unwrap_or(id), + "pig" + | "cow" + | "mooshroom" + | "sheep" + | "chicken" + | "rabbit" + | "horse" + | "donkey" + | "mule" + | "zombie_horse" + | "skeleton_horse" + | "llama" + | "trader_llama" + | "goat" + | "panda" + | "polar_bear" + | "fox" + | "wolf" + | "cat" + | "ocelot" + | "parrot" + | "bee" + | "turtle" + | "armadillo" + | "frog" + | "axolotl" + | "hoglin" + | "strider" + | "camel" + | "camel_husk" + | "sniffer" + | "happy_ghast" + | "nautilus" + | "zombie_nautilus" + ) +} pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag { let base = Base { id: &m.kind.id, @@ -181,7 +222,7 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag { } } // Animal.addAdditionalSaveData: the love ticks left, whatever the save had. - if m.kind.flags.animal { + if is_animal(id) { map.insert("InLove".into(), Tag::Int(m.extra.in_love.max(0))); } match id { @@ -624,7 +665,7 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap>, id: u32) - m.extra.forced_age = number(tag.get("ForcedAge")).unwrap_or(0) as i32; m.extra.age_locked = flag(tag, "AgeLocked"); } - if m.kind.flags.animal { + if is_animal(&m.kind.id) { m.extra.in_love = number(tag.get("InLove")).unwrap_or(0) as i32; } m.name_visible = flag(tag, "CustomNameVisible"); From f88fba8d05616d9cc5265c3420726630d544302d Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 08:18:29 +0200 Subject: [PATCH 06/39] A server's attribute updates reach its player; new boss bars start visible The client of a server (single player on its server thread, LAN guests) ignored ClientboundUpdateAttributesPacket for its own player, so a new maximum health never reached it: its hearts stayed at ten and the health it was sent was clamped to 20. It now applies each synced instance's base and modifiers (handleUpdateAttributes) and re-applies the received health. BossBar::new started hidden; ServerBossEvent starts visible. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/bossbar.rs | 2 +- crates/horizon-client/src/game/control.rs | 6 +++ crates/horizon-client/src/game/remote.rs | 9 +++++ crates/horizon-client/src/lan/attributes.rs | 44 +++++++++++++++++++- tools/lan_e2e.py | 45 ++++++++++++++++++++- 5 files changed, 102 insertions(+), 4 deletions(-) diff --git a/crates/horizon-client/src/bossbar.rs b/crates/horizon-client/src/bossbar.rs index a976258f9..eab31a5e5 100644 --- a/crates/horizon-client/src/bossbar.rs +++ b/crates/horizon-client/src/bossbar.rs @@ -46,7 +46,7 @@ impl BossBar { style: "progress".into(), value: 0, max: 100, - visible: false, + visible: true, player: false, } } diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index 5645441f0..ca15424d1 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -834,6 +834,12 @@ impl Game<'_> { "velocity": [p.velocity.x, p.velocity.y, p.velocity.z], "on_ground": p.ground.on_ground, "health": p.health.health(), + // The maximum the HUD's hearts last read (its cached value). + "max_health": p + .attributes + .lookup(p.attributes.key("minecraft:max_health").as_ref()) + .and_then(|h| p.attributes.instance(&h).ok()) + .map(|i| i.cached_value()), "food": p.health.food.food, }) }); diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 578af5f69..b8777e8d2 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -2128,6 +2128,15 @@ impl Game<'_> { m.swing(); } } + // The player's own instances: its maximum health (the hearts the + // HUD draws and the health it may hold), reach, speed and so on. + // Then the received health again, which the old maximum clamped. + C::UpdateAttributes(p) if self.remote.as_ref().is_some_and(|r| r.entity_id == p.id) => { + if let Some(pl) = self.player.as_mut() { + horizon_client::lan::attributes::apply(&mut pl.attributes, &p.values); + } + self.apply_remote_health(); + } C::UpdateMobEffect(p) => self.remote_effect(&p), C::RemoveMobEffect(p) => { if self.remote.as_ref().is_some_and(|r| r.entity_id == p.id) diff --git a/crates/horizon-client/src/lan/attributes.rs b/crates/horizon-client/src/lan/attributes.rs index bc208b6f9..eef565409 100644 --- a/crates/horizon-client/src/lan/attributes.rs +++ b/crates/horizon-client/src/lan/attributes.rs @@ -5,7 +5,7 @@ //! changed since (`getAttributesToSync`), to the trackers and the player itself. use horizon_protocol::packets::play::{AttributeModifier, AttributeSnapshot}; use horizon_world::attributes::{ - Instance, Operation, + Instance, Modifier, Operation, entity::{AttributeId, EntityAttributes}, live::LiveAttributes, }; @@ -70,6 +70,48 @@ pub fn take_dirty i32>(map: &mut LiveAttributes) -> Vec i32>(map: &mut LiveAttributes, values: &[AttributeSnapshot]) { + for s in values { + let Some(key) = map + .keys() + .iter() + .find(|k| k.attribute().registry_id() == s.attribute) + .cloned() + else { + continue; + }; + let Ok(Some(handle)) = map.get_instance(Some(key)) else { + continue; + }; + let modifiers: Vec<_> = s + .modifiers + .iter() + .filter_map(|m| Modifier::new(&m.id, m.amount, operation(m.operation)).ok()) + .collect(); + let _ = map.edit(&handle, |instance, observer| { + instance.set_base_value(s.base, observer)?; + instance.remove_all(observer)?; + for m in modifiers { + instance.add_transient(m, observer)?; + } + Ok(()) + }); + } +} + +/// `AttributeModifier.Operation.BY_ID`. +fn operation(id: i32) -> Operation { + match id { + 1 => Operation::AddMultipliedBase, + 2 => Operation::AddMultipliedTotal, + _ => Operation::AddValue, + } +} + /// The client-syncable instances of a generic attribute map. pub fn generic_syncable(map: &EntityAttributes) -> impl Iterator + '_ { let catalog = map.catalog(); diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index fd6e09ce1..fbb7e4e9c 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -213,7 +213,7 @@ ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' # The host plays single player on its server thread (--server-thread). SERVER_THREAD = False -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'looks', 'leads', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'thrown', 'sparks', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'lightning', 'riding', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption', 'breath', 'lava', 'levels', 'drops', 'underwater'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'looks', 'leads', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'thrown', 'sparks', 'hud', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'lightning', 'riding', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption', 'breath', 'lava', 'levels', 'drops', 'underwater'] class Game: @@ -2711,6 +2711,46 @@ def check_sparks(host, guest, out): return result +def hearts(game): + """The player's maximum and current health as its own client holds them.""" + p = game('status')['player'] + return p.get('max_health'), round(p['health'], 1) + + +def check_hud(host, guest, out): + result = {} + for label, player in (('host', host), ('guest', guest)): + player('command', command='gamemode survival') + try: + for maximum in (40, 10): + player('command', command=f'attribute @s minecraft:max_health base set {maximum}') + player.wait(10) + player('command', command='effect give @s minecraft:instant_health 1 10') + seen = None + for _ in range(15): + player.wait(2) + seen = hearts(player) + if seen == (maximum, maximum): + break + result[f'{label} {maximum}'] = seen + player.shot(out, f'hearts-{label}-{maximum}') + finally: + player('command', command='attribute @s minecraft:max_health base reset') + player('command', command='effect give @s minecraft:instant_health 1 10') + # BossBarCommands' new bar starts visible (ServerBossEvent's constructor). + host('command', command='bossbar add lan_e2e:bar "LAN bar"') + try: + host('command', command='bossbar get lan_e2e:bar visible') + host.wait(5) + result['bar'] = [line for line in host.raw('messages', 5)['chat'] if 'lan_e2e:bar' in line or 'LAN bar' in line] + finally: + host('command', command='bossbar remove lan_e2e:bar') + wrong = {k: v for k, v in result.items() if k != 'bar' and v != (int(k.split()[1]),) * 2} + assert not wrong, f'hearts: {wrong}\n{result}' + assert any('currently visible' in line for line in result['bar']), f'bar: {result["bar"]}' + return result + + def check_blast(host, guest, out): guest('command', command='gamemode survival') guest('command', command='clear @s') @@ -4046,7 +4086,8 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'looks': check_looks, 'leads': check_leads, 'thrown': check_thrown, - 'sparks': check_sparks} + 'sparks': check_sparks, + 'hud': check_hud} def run(ctl, out, resources, checks): From c35ea9568f4771e360cf353df64034237612daef Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 07:31:31 +0200 Subject: [PATCH 07/39] Server's mobs: babies, wool, collars, fire, slime sizes and health reach its clients A client of a server (single player on its server thread, a LAN guest, a dedicated server's player) only applied a rabbit's baby flag and a creeper's swell from SetEntityData. It now reads the synched data vanilla's entities read in onSyncedDataUpdated: Entity's on-fire flag, LivingEntity's health (the wither's boss bar follows it), the baby flag of every ageable mob, Sheep.DATA_WOOL_ID, TamableAnimal's sitting and tame flags, the wolf's and the cat's collar colour, and the cube mobs' size. A remote wolf or cat starts with vanilla's red collar. The server sends the wolf's and cat's collar colour. Pairing still carries only the values that differ from the defaults; a mob's state now lists the defaults of the entries that can go back to theirs (health, the baby flag, the wool, the collar), and the tracker sends that default when one does: a calf growing up or a sheep's white wool growing back reach the clients. Sheep saves wrote the colour and shearing they were loaded with (0 unless the save had them) and loads ignored both: they now save and load the wool as it is (Sheep.add/readAdditionalSaveData). lan_e2e's new `looks` check summons the mobs and changes them on both sides. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/entity_nbt.rs | 47 +++++- crates/horizon-client/src/game.rs | 1 + crates/horizon-client/src/game/creatures.rs | 4 + crates/horizon-client/src/game/dragon.rs | 1 + crates/horizon-client/src/game/lan.rs | 31 +++- .../horizon-client/src/game/lan_entities.rs | 1 + crates/horizon-client/src/game/remote.rs | 8 +- .../src/game/remote_mob_data.rs | 136 ++++++++++++++++++ crates/horizon-client/src/lan/entity.rs | 34 ++++- crates/horizon-client/src/lan/hub.rs | 1 + crates/horizon-client/tests/lan_bot.rs | 1 + tools/lan_e2e.py | 83 ++++++++++- 12 files changed, 331 insertions(+), 17 deletions(-) create mode 100644 crates/horizon-client/src/game/remote_mob_data.rs diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index aaccdd844..dbddb274b 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -445,10 +445,10 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag { map.entry(key.into()).or_insert(value); } } + // Sheep.addAdditionalSaveData: its wool as it is now. "minecraft:sheep" => { - for (key, value) in [("Color", Tag::Byte(0)), ("Sheared", Tag::Byte(0))] { - map.entry(key.into()).or_insert(value); - } + map.insert("Color".into(), Tag::Byte((m.extra.color & 15) as i8)); + map.insert("Sheared".into(), Tag::Byte(i8::from(m.extra.sheared))); } // CopperGolem.addAdditionalSaveData. "minecraft:copper_golem" => { @@ -832,6 +832,11 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap>, id: u32) - } m.extra.egg_time = number(tag.get("life_ticks")).map_or(-1, |v| v as i32); } + // Sheep.readAdditionalSaveData: white and unsheared when absent. + if m.species() == crate::mobs::Species::Sheep { + m.extra.color = number(tag.get("Color")).map_or(0, |c| (c as u8) & 15); + m.extra.sheared = flag(tag, "Sheared"); + } if m.species() == crate::mobs::Species::Shulker { m.extra.variant3 = number(tag.get("AttachFace")).unwrap_or(0).clamp(0, 5) as u8; m.extra.counter = number(tag.get("Peek")).unwrap_or(0).clamp(0, 100) as i32; @@ -2051,3 +2056,39 @@ mod axolotl_nbt_tests { assert_eq!(mm.temptation_cooldown_ticks.value(), Some(42)); } } + +#[cfg(test)] +mod sheep_nbt_tests { + use super::*; + #[test] + fn a_sheep_keeps_its_dyed_and_sheared_wool() { + let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../.local/horizon-client") + .join(horizon_core::VANILLA_VERSION); + if !root.join("items.json").exists() { + return; + } + let data = crate::mobs::MobTypes::load(&root).unwrap(); + let mut m = Mob::new( + 1, + Arc::new(data.types["minecraft:sheep"].clone()), + Vec3::new(0.0, 4.0, 0.0), + 0.0, + ); + // Summoned white, then dyed red and sheared: the save has it now. + let raw = Tag::compound([("Color", Tag::Byte(0)), ("Sheared", Tag::Byte(0))]); + (m.extra.color, m.extra.sheared) = (14, true); + let tag = mob_to_nbt(&m, [1, 2, 3, 4], Some(&raw)); + assert_eq!( + (number(tag.get("Color")), number(tag.get("Sheared"))), + (Some(14), Some(1)) + ); + let types: HashMap> = data + .types + .iter() + .map(|(k, v)| (k.clone(), Arc::new(v.clone()))) + .collect(); + let back = mob_from_nbt(&tag, &types, 2).unwrap(); + assert_eq!((back.extra.color, back.extra.sheared), (14, true)); + } +} diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index b307d71dc..27ac5f259 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -121,6 +121,7 @@ mod pumpkin; mod raids; mod recipe_book; mod remote; +mod remote_mob_data; mod remote_objects; mod remote_projectiles; mod remote_stations; diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 969543601..442c37237 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -343,8 +343,12 @@ impl Game<'_> { "on_ground": m.on_ground, "variant": m.extra.variant, "color": m.extra.color, + "sheared": m.extra.sheared, + "burning": m.fire_ticks > 0, + "size": m.extra.size, "leash": m.extra.leash.map(|h| format!("{h:?}")), "tame": m.extra.tame, + "sitting": m.extra.sitting, "temper": m.extra.variant3, "chested": m.extra.chested, "head": m.extra.head.as_deref(), diff --git a/crates/horizon-client/src/game/dragon.rs b/crates/horizon-client/src/game/dragon.rs index 6dd02893d..d5cc5d9bf 100644 --- a/crates/horizon-client/src/game/dragon.rs +++ b/crates/horizon-client/src/game/dragon.rs @@ -2697,6 +2697,7 @@ impl Game<'_> { velocity: [d.velocity.x, d.velocity.y, d.velocity.z], data: 0, meta, + defaults: Vec::new(), equipment: Vec::new(), attributes: Vec::new(), precise: false, diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index bdbfa3fc6..b5d0db5f1 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -91,7 +91,7 @@ pub(super) fn entity_uuid(id: u32) -> u128 { /// The SynchedEntityData index of the baby flag: AgeableMob.DATA_BABY_ID and /// Zombie.DATA_BABY_ID are both the first entry after Mob's (16); Piglin's /// comes after AbstractPiglin's zombification flag (17). -fn baby_index(kind: &str) -> Option { +pub(super) fn baby_index(kind: &str) -> Option { match kind { "minecraft:piglin" => Some(17), "minecraft:cow" @@ -139,7 +139,13 @@ fn baby_index(kind: &str) -> Option { /// A mob's synched data as ServerEntity sends it (only the values that differ /// from the defaults the client constructs), with vanilla's entry indices: /// Entity 0..=7, LivingEntity 8..=14, Mob 15, then the subclasses. -fn mob_meta(m: &Mob, hub: &Hub, meta: &mut Vec<(u8, i32, Value)>) { +/// `defaults` gets the defaults of the entries that can go back to theirs. +fn mob_meta( + m: &Mob, + hub: &Hub, + meta: &mut Vec<(u8, i32, Value)>, + defaults: &mut Vec<(u8, i32, Value)>, +) { let kind = m.kind.id.as_str(); if m.fire_ticks > 0 { meta.push((0, serializer::BYTE, Value::Byte(1))); @@ -159,15 +165,18 @@ fn mob_meta(m: &Mob, hub: &Hub, meta: &mut Vec<(u8, i32, Value)>) { if health != 1.0 { meta.push((9, serializer::FLOAT, Value::Float(health))); } + defaults.push((9, serializer::FLOAT, Value::Float(1.0))); // Mob.DATA_MOB_FLAGS_ID: no AI 1, left-handed 2. let flags = i8::from(m.no_ai) | i8::from(m.extra.left_handed) << 1; if flags != 0 { meta.push((15, serializer::BYTE, Value::Byte(flags))); } - if m.is_baby() - && let Some(index) = baby_index(kind) - { - meta.push((index, serializer::BOOLEAN, Value::Bool(true))); + // AgeableMob.DATA_BABY_ID (and Zombie's, Piglin's). + if let Some(index) = baby_index(kind) { + if m.is_baby() { + meta.push((index, serializer::BOOLEAN, Value::Bool(true))); + } + defaults.push((index, serializer::BOOLEAN, Value::Bool(false))); } let e = &m.extra; let id = |registry: &str, name: &str| hub.registry_id(registry, &format!("minecraft:{name}")); @@ -178,6 +187,7 @@ fn mob_meta(m: &Mob, hub: &Hub, meta: &mut Vec<(u8, i32, Value)>) { if value != 0 { meta.push((18, serializer::BYTE, Value::Byte(value as i8))); } + defaults.push((18, serializer::BYTE, Value::Byte(0))); } "minecraft:cat" | "minecraft:wolf" => { // TamableAnimal.DATA_FLAGS_ID: sitting 1, tame 4. @@ -200,6 +210,13 @@ fn mob_meta(m: &Mob, hub: &Hub, meta: &mut Vec<(u8, i32, Value)>) { if let Some(v) = id(registry, name) { meta.push((index, serializer, Value::Id(v))); } + // Wolf.DATA_COLLAR_COLOR and Cat's, red by default. + let collar = if kind == "minecraft:cat" { 23 } else { 21 }; + let red = i32::from(horizon_client::mobs::tame::RED); + if i32::from(e.color & 15) != red { + meta.push((collar, serializer::INT, Value::Int(i32::from(e.color & 15)))); + } + defaults.push((collar, serializer::INT, Value::Int(red))); } "minecraft:horse" | "minecraft:donkey" @@ -766,7 +783,7 @@ impl Game<'_> { e.on_ground = m.on_ground; e.velocity = [m.velocity.x, m.velocity.y, m.velocity.z]; e.needs_sync = m.needs_sync; - mob_meta(m, hub, &mut e.meta); + mob_meta(m, hub, &mut e.meta, &mut e.defaults); mob_equipment(m, hub, &mut e.equipment); self.mob_attributes(m, &mut e.attributes); } diff --git a/crates/horizon-client/src/game/lan_entities.rs b/crates/horizon-client/src/game/lan_entities.rs index 631498edd..6342e921c 100644 --- a/crates/horizon-client/src/game/lan_entities.rs +++ b/crates/horizon-client/src/game/lan_entities.rs @@ -36,6 +36,7 @@ fn state(id: u32, kind: &str, position: [f64; 3], yaw: f32, on_ground: bool) -> velocity: [0.0; 3], data: 0, meta: Vec::new(), + defaults: Vec::new(), equipment: Vec::new(), attributes: Vec::new(), precise: false, diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 8e18c1ff4..19306efd7 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -1334,6 +1334,7 @@ impl Game<'_> { } if let Some(t) = mob_type { let mut mob = Mob::new(id as u32, t, at, head); + super::remote_mob_data::client_defaults(&mut mob); (mob.yaw, mob.previous_yaw) = (yaw, yaw); (mob.head_pitch, mob.previous_head_pitch) = (pitch, pitch); mob.persistent = true; @@ -1909,13 +1910,12 @@ impl Game<'_> { && m.species() == horizon_client::mobs::Species::Rabbit { for (index, _, value) in &p.values { - match (index, value) { - (15, Value::Byte(flags)) => m.no_ai = flags & 1 != 0, - (16, Value::Bool(baby)) => m.extra.age = if *baby { -1 } else { 0 }, - _ => {} + if let (15, Value::Byte(flags)) = (index, value) { + m.no_ai = flags & 1 != 0; } } } + self.remote_mob_data(p.id, &p.values); if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) && m.species() == horizon_client::mobs::Species::Creeper { diff --git a/crates/horizon-client/src/game/remote_mob_data.rs b/crates/horizon-client/src/game/remote_mob_data.rs new file mode 100644 index 000000000..a84998934 --- /dev/null +++ b/crates/horizon-client/src/game/remote_mob_data.rs @@ -0,0 +1,136 @@ +//! A server's mobs as their synched data has them +//! (ClientPacketListener.handleSetEntityData, then each entity's +//! `onSyncedDataUpdated`): what a client draws from it rather than from its +//! own simulation, which a client of a server doesn't run. +use super::Game; +use horizon_client::mobs::{Mob, Species, tame}; +use horizon_protocol::entity_data::Value; + +/// The values `apply` takes from one SetEntityData packet, by vanilla's entry +/// indices (Entity 0..=7, LivingEntity 8..=14, Mob 15, then the subclasses). +fn apply(m: &mut Mob, values: &[(u8, i32, Value)]) { + let species = m.species(); + let baby = super::lan::baby_index(&m.kind.id); + for (index, _, value) in values { + match (*index, value) { + // Entity.DATA_SHARED_FLAGS_ID's FLAG_ONFIRE: Entity.isOnFire on a + // client, which draws the flames. A client doesn't count the + // fire down: the flag goes off when the server's fire is out. + (0, Value::Byte(flags)) => m.fire_ticks = i32::from(flags & 1), + // LivingEntity.DATA_HEALTH_ID: the boss bars and the death + // animation follow it. + (9, Value::Float(health)) => m.health = *health, + (i, Value::Bool(b)) if Some(i) == baby => m.extra.age = if *b { -1 } else { 0 }, + _ => {} + } + match (species, *index, value) { + // Sheep.DATA_WOOL_ID: the colour in the low nibble, 16 sheared. + (Species::Sheep, 18, Value::Byte(wool)) => { + m.extra.color = (*wool & 15) as u8; + m.extra.sheared = wool & 16 != 0; + } + // TamableAnimal.DATA_FLAGS_ID: sitting 1, tame 4. + (Species::Wolf | Species::Cat | Species::Parrot, 18, Value::Byte(flags)) => { + m.extra.sitting = flags & 1 != 0; + m.extra.tame = flags & 4 != 0; + } + // Wolf.DATA_COLLAR_COLOR and Cat's. + (Species::Wolf, 21, Value::Int(c)) | (Species::Cat, 23, Value::Int(c)) => { + m.extra.color = (*c & 15) as u8; + } + // AbstractCubeMob.ID_SIZE: its size and box (refreshDimensions). + (Species::Slime | Species::MagmaCube, 18, Value::Int(size)) => { + m.extra.size = (*size).clamp(1, 127); + } + _ => {} + } + } +} + +impl Game<'_> { + /// SetEntityData for a mob of the server's. + pub(super) fn remote_mob_data(&mut self, id: i32, values: &[(u8, i32, Value)]) { + if let Some(m) = self.mobs.iter_mut().find(|m| m.id == id as u32) { + apply(m, values); + } + } +} + +/// A mob as a client constructs it before its data arrives: the synched +/// defaults the server leaves out (`getNonDefaultValues`) that differ from +/// what `Mob::new` gives, such as a wolf's and a cat's red collar. +pub(super) fn client_defaults(m: &mut Mob) { + if matches!(m.species(), Species::Wolf | Species::Cat) { + m.extra.color = tame::RED; + } +} + +#[cfg(test)] +mod tests { + use super::*; + use horizon_protocol::entity_data::serializer as s; + + /// A mob of the prepared resources' type `kind`, as a client spawns it + /// (None without the resources). + fn mob(kind: &str) -> Option { + let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../.local/horizon-client") + .join(horizon_core::VANILLA_VERSION); + let types = horizon_client::mobs::MobTypes::load(&root).ok()?; + let t = std::sync::Arc::new(types.types.get(kind)?.clone()); + let mut m = Mob::new(1, t, horizon_core::geometry::Vec3::ZERO, 0.0); + client_defaults(&mut m); + Some(m) + } + + #[test] + fn a_sheep_takes_its_wool_and_a_calf_its_size() { + let Some(mut m) = mob("minecraft:sheep") else { + return; + }; + apply(&mut m, &[(18, s::BYTE, Value::Byte(14 | 16))]); + assert_eq!((m.extra.color, m.extra.sheared), (14, true)); + apply(&mut m, &[(18, s::BYTE, Value::Byte(0))]); + assert_eq!((m.extra.color, m.extra.sheared), (0, false)); + let mut calf = mob("minecraft:cow").unwrap(); + apply(&mut calf, &[(16, s::BOOLEAN, Value::Bool(true))]); + assert!(calf.is_baby()); + apply(&mut calf, &[(16, s::BOOLEAN, Value::Bool(false))]); + assert!(!calf.is_baby()); + } + + #[test] + fn a_wolf_takes_its_owner_flags_and_collar() { + let Some(mut m) = mob("minecraft:wolf") else { + return; + }; + assert_eq!(m.extra.color, tame::RED); + apply( + &mut m, + &[ + (18, s::BYTE, Value::Byte(4 | 1)), + (21, s::INT, Value::Int(11)), + ], + ); + assert!(m.extra.tame && m.extra.sitting); + assert_eq!(m.extra.color, 11); + } + + #[test] + fn fire_health_and_size_follow_the_server() { + let Some(mut m) = mob("minecraft:slime") else { + return; + }; + apply( + &mut m, + &[ + (0, s::BYTE, Value::Byte(1)), + (9, s::FLOAT, Value::Float(12.5)), + (18, s::INT, Value::Int(4)), + ], + ); + assert_eq!((m.fire_ticks, m.health, m.extra.size), (1, 12.5, 4)); + apply(&mut m, &[(0, s::BYTE, Value::Byte(0))]); + assert_eq!(m.fire_ticks, 0); + } +} diff --git a/crates/horizon-client/src/lan/entity.rs b/crates/horizon-client/src/lan/entity.rs index 86fea8b65..9bdb037f7 100644 --- a/crates/horizon-client/src/lan/entity.rs +++ b/crates/horizon-client/src/lan/entity.rs @@ -50,6 +50,10 @@ pub struct EntityState { pub data: i32, /// SynchedEntityData entries that differ from the defaults. pub meta: Vec<(u8, i32, Value)>, + /// The defaults of entries `meta` leaves out while they are at them, + /// sent when one goes back to its default (a calf grown up, a mob healed + /// back to 1, wool grown back white) beyond Entity's own below. + pub defaults: Vec<(u8, i32, Value)>, /// EquipmentSlot ordinals (0 main hand, 1 off hand, 2 feet, 3 legs, /// 4 chest, 5 head, 6 body, 7 saddle) and their stacks. pub equipment: Vec<(u8, Option)>, @@ -78,6 +82,7 @@ impl EntityState { self.velocity = [0.0; 3]; self.data = 0; self.meta.clear(); + self.defaults.clear(); self.equipment.clear(); self.attributes.clear(); self.precise = false; @@ -469,7 +474,10 @@ impl ServerEntity { (0, serializer::BYTE) => Value::Byte(0), (6, serializer::POSE) => Value::Id(0), (14, serializer::OPTIONAL_BLOCK_POS) => Value::OptPos(None), - _ => continue, + _ => match e.defaults.iter().find(|d| (d.0, d.1) == (*index, *kind)) { + Some(d) => d.2.clone(), + None => continue, + }, }; if !e.meta.iter().any(|m| m.0 == *index) { changed.push((*index, *kind, default)); @@ -855,6 +863,7 @@ mod tests { velocity: [0.0; 3], data: 0, meta: Vec::new(), + defaults: Vec::new(), equipment: Vec::new(), attributes: Vec::new(), precise: false, @@ -1075,6 +1084,29 @@ mod tests { assert!(changes(&mut s, &e).is_empty()); } + #[test] + fn a_value_back_at_a_listed_default_is_sent() { + let mut e = state(0.0); + e.defaults = vec![(16, serializer::BOOLEAN, Value::Bool(false))]; + // A calf: paired with its baby flag, then grown up. + e.meta = vec![(16, serializer::BOOLEAN, Value::Bool(true))]; + let mut s = added(&e); + changes(&mut s, &e); + e.meta.clear(); + let data = changes(&mut s, &e) + .into_iter() + .find_map(|p| match p { + play::Clientbound::SetEntityData(d) => Some(d), + _ => None, + }) + .expect("growing up is sent"); + assert_eq!( + data.values, + vec![(16, serializer::BOOLEAN, Value::Bool(false))] + ); + assert!(changes(&mut s, &e).is_empty()); + } + #[test] fn metadata_and_equipment_send_only_the_changes() { let mut e = state(0.0); diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index c31b3c929..1330b0ae4 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -1559,6 +1559,7 @@ impl Hub { velocity: [0.0; 3], data: 0, meta, + defaults: Vec::new(), equipment, attributes, precise: false, diff --git a/crates/horizon-client/tests/lan_bot.rs b/crates/horizon-client/tests/lan_bot.rs index 4f5f17a3b..4b8bbc897 100644 --- a/crates/horizon-client/tests/lan_bot.rs +++ b/crates/horizon-client/tests/lan_bot.rs @@ -430,6 +430,7 @@ fn guests_see_each_other_and_the_world_entities() { velocity: [0.0; 3], data: 0, meta: Vec::new(), + defaults: Vec::new(), equipment: Vec::new(), attributes: Vec::new(), precise: false, diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index a50e97764..de8c78dc3 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -24,6 +24,11 @@ address. The script then checks, from both sides, what a guest's game used to ge - arrows: the guest and the host shoot each other with a bow; with the pvp game rule off, nothing (#484); +- looks: mobs look on both sides as the server has them (their synched data): a calf is a + baby, a red sheep is sheared, a size 4 slime, a tamed wolf sits with its blue collar, a + cow on fire burns and goes out; then the wither's health drops when it is hurt, the + sheep is dyed blue with its wool back and the calf grows up (#1367, #1368, #1370, + #1371, #1385, #1390); - respawn: a player killed is drawn dead by the other side, and alive again once it respawns (the respawned player is a new entity to the others); the host dies, and on the server thread the guest too; @@ -201,7 +206,7 @@ ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' # The host plays single player on its server thread (--server-thread). SERVER_THREAD = False -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'lightning', 'riding', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption', 'breath', 'lava', 'levels', 'drops', 'underwater'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'looks', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'lightning', 'riding', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption', 'breath', 'lava', 'levels', 'drops', 'underwater'] class Game: @@ -1147,6 +1152,79 @@ def check_hurt(host, guest, out): return seen +def mob_looks(game, centre): + """What `check_looks` compares of each mob around `centre`, by kind and column.""" + found = {} + for m in game.raw('mobs', 32).get('near', []): + if abs(m['pos'][0] - centre[0]) > 12 or abs(m['pos'][2] - centre[2]) > 14: + continue + key = f"{m['kind'].removeprefix('minecraft:')}@{round(m['pos'][0] - centre[0])}" + found[key] = {'baby': m['baby'], 'color': m['color'], 'sheared': m['sheared'], 'size': m['size'], + 'tame': m['tame'], 'sitting': m['sitting'], 'burning': m['burning'], + 'health': round(m['health'])} + return found + + +def check_looks(host, guest, out): + x, y, z = (int(v // 1) for v in pos_of(host)) + centre = (x + 0.5, y, z + 0.5) + # Each mob in a column of its own (the key), standing on the stone floor. + looks = { + 'cow@-5': ('{NoAI:1b,Fire:80s}', {'burning': True}), + 'cow@-3': ('{NoAI:1b,Age:-24000}', {'baby': True}), + 'cow@-1': ('{NoAI:1b}', {'baby': False}), + 'sheep@1': ('{NoAI:1b,Color:14b,Sheared:1b}', {'color': 14, 'sheared': True}), + 'slime@3': ('{NoAI:1b,Size:3}', {'size': 4}), + 'wolf@5': ('{NoAI:1b,Owner:[I;1,2,3,4],Sitting:1b,CollarColor:11b}', + {'tame': True, 'sitting': True, 'color': 11}), + } + sides = {'host': host, 'guest': guest} + seen = {} + try: + for key, (tag, _) in looks.items(): + kind, dx = key.split('@') + host('command', command=f'summon minecraft:{kind} {centre[0] + int(dx)} {y} {z + 4.5} {tag}') + host('command', command=f'summon minecraft:wither {centre[0]} {y + 3} {z + 10.5} {{NoAI:1b,Invul:0}}') + host.wait(30) + seen['spawned'] = {name: mob_looks(g, centre) for name, g in sides.items()} + # Changes after the spawn: the wither hurt, the sheep dyed and grown back, the calf grown up. + host('command', command='damage @e[type=minecraft:wither,limit=1] 150 minecraft:generic') + for kind, dx, tag in (('sheep', 1, '{Color:11b,Sheared:0b}'), ('cow', -3, '{Age:0}')): + host('command', command=f'data merge entity @e[type=minecraft:{kind},x={centre[0] + dx},y={y},' + f'z={z + 4.5},distance=..0.5,limit=1] {tag}') + guest.raw('look-at', centre[0], y + 1, z + 4.5) + host.raw('look-at', centre[0], y + 1, z + 4.5) + # The fire (80 ticks) goes out in the meantime. + host.wait(80) + for name, g in sides.items(): + g.shot(out, 'looks') + seen['later'] = {name: mob_looks(g, centre) for name, g in sides.items()} + finally: + # A killed slime splits: shrink it first so no children are left hopping into the next check. + host('command', command=f'data merge entity @e[type=minecraft:slime,x={centre[0] + 3},y={y},z={z + 4.5},' + f'distance=..2,limit=1] {{Size:0}}') + host('command', command=f'kill @e[type=!minecraft:player,x={x - 12},y={y - 2},z={z - 2},dx=24,dy=12,dz=16]') + wrong = [] + for name in sides: + spawned, later = seen['spawned'][name], seen['later'][name] + for key, (_, want) in looks.items(): + got = spawned.get(key) + if got is None or any(got[k] != v for k, v in want.items()): + wrong.append(f'{name} {key}: {got}, wants {want}') + if later.get('cow@-5', {}).get('burning') is not False: + wrong.append(f"{name}: the cow's fire never went out: {later.get('cow@-5')}") + if [later.get('sheep@1', {}).get(k) for k in ('color', 'sheared')] != [11, False]: + wrong.append(f"{name}: the sheep dyed blue with its wool back: {later.get('sheep@1')}") + if later.get('cow@-3', {}).get('baby') is not False: + wrong.append(f"{name}: the calf grown up: {later.get('cow@-3')}") + withers = [v['health'] for k, v in spawned.items() if k.startswith('wither')] + hurt = [v['health'] for k, v in later.items() if k.startswith('wither')] + if withers != [300] or hurt != [150]: + wrong.append(f"{name}: the wither's health {withers} then {hurt}, wants [300] then [150]") + assert not wrong, '\n'.join(wrong) + return seen['later']['guest'] + + def container_items(game): """The open container's stacks by slot (Horizon's container slots are 300 and up) and its size.""" slots = [s for s in game('screen').get('slots', []) if s.get('slot', 0) >= 300] @@ -3826,7 +3904,8 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'worldspawn': check_world_spawn, 'beds': check_beds, 'shield': check_shield, 'deathmining': check_death_mining, 'contacts': check_contacts, 'absorption': check_absorption, 'breath': check_breath, 'lava': check_lava, 'levels': check_levels, 'drops': check_drops, - 'underwater': check_underwater} + 'underwater': check_underwater, + 'looks': check_looks} def run(ctl, out, resources, checks): From 0d8249e124ad6e3d945730d7e5ddf18b5507bec8 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 07:50:04 +0200 Subject: [PATCH 08/39] Leads: a server's clients draw the rope (ClientboundSetEntityLinkPacket) A server never told its clients who holds a mob's lead, so on single player's server thread, a LAN guest's or a dedicated server's client the lead was used up and the mob tied, but no rope showed. The tracker now sends ClientboundSetEntityLinkPacket as vanilla does: in ServerEntity.sendPairingData for a leashed entity, and when the lead is tied or comes off (Leashable.setLeashedTo / dropLeash, the holder 0). The holder is the player the mob sees (a guest, else the host) or a mob; a fence knot isn't an entity on Horizon's server yet. The client sets the mob's holder from it: itself, another player or a mob. lan_e2e's new `leads` check ties and unties a cow on both sides. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/dragon.rs | 1 + crates/horizon-client/src/game/lan.rs | 27 ++++++++++-- .../horizon-client/src/game/lan_entities.rs | 1 + crates/horizon-client/src/game/remote.rs | 13 ++++++ crates/horizon-client/src/lan/entity.rs | 44 ++++++++++++++++++- crates/horizon-client/src/lan/hub.rs | 1 + crates/horizon-client/tests/lan_bot.rs | 1 + crates/horizon-protocol/src/packets/play.rs | 7 +++ tools/lan_e2e.py | 44 ++++++++++++++++++- 9 files changed, 131 insertions(+), 8 deletions(-) diff --git a/crates/horizon-client/src/game/dragon.rs b/crates/horizon-client/src/game/dragon.rs index d5cc5d9bf..c6f5d419b 100644 --- a/crates/horizon-client/src/game/dragon.rs +++ b/crates/horizon-client/src/game/dragon.rs @@ -2702,6 +2702,7 @@ impl Game<'_> { attributes: Vec::new(), precise: false, needs_sync: false, + leash: None, }) } } diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index b5d0db5f1..8467b6d3a 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -14,7 +14,7 @@ use horizon_client::{ hub::{HostEvent, HostState, Hub, HubConfig, TickCtx}, playerdata, }, - mobs::Mob, + mobs::{Mob, leash::Holder}, text::Text, }; use horizon_core::geometry::Vec3; @@ -774,9 +774,27 @@ impl Game<'_> { } /// The entities guests track: mobs, items and the rest (game/lan_entities.rs). - fn lan_entities(&self, host_entity: Option, hub: &mut Hub, out: &mut States) { + /// `mob_players` is the guest each mob sees as its player (game/lan_mobs.rs). + fn lan_entities( + &self, + host_entity: Option, + mob_players: &HashMap, + hub: &mut Hub, + out: &mut States, + ) { out.clear(); for m in self.mobs.iter().filter(|m| !m.removed) { + // The lead's holder: the player the mob sees (a guest, else the + // host) or another mob. A fence knot isn't an entity here. + let leash = m.extra.leash.and_then(|h| match h { + Holder::Player => mob_players + .get(&m.id) + .and_then(|conn| hub.guest(*conn)) + .map(|g| g.entity_id) + .or(host_entity), + Holder::Mob(id) => Some(id as i32), + Holder::Knot(_) => None, + }); let e = out.add(m.id as i32, entity_uuid(m.id), m.kind.id.as_str()); e.position = [m.position.x, m.position.y, m.position.z]; (e.yaw, e.pitch, e.head_yaw) = (m.yaw, m.head_pitch, m.head_yaw); @@ -786,6 +804,7 @@ impl Game<'_> { mob_meta(m, hub, &mut e.meta, &mut e.defaults); mob_equipment(m, hub, &mut e.equipment); self.mob_attributes(m, &mut e.attributes); + e.leash = leash; } for d in &self.dropped { let stack = hub.stack(&Some(d.stack.clone())); @@ -847,7 +866,7 @@ impl Game<'_> { .filter_map(|g| Some((g.vehicle?, g.entity_id))) .collect(); self.lan_riding(host_entity, &guests, &mut host.hub.riding); - self.lan_entities(host_entity, &mut host.hub, &mut states); + self.lan_entities(host_entity, &host.mob_players, &mut host.hub, &mut states); } else { states.clear(); } @@ -1035,7 +1054,7 @@ impl Game<'_> { return; } // A dedicated server has no host player. - self.lan_entities(None, &mut host.hub, &mut theirs); + self.lan_entities(None, &host.mob_players, &mut host.hub, &mut theirs); theirs.as_slice() } else { entities diff --git a/crates/horizon-client/src/game/lan_entities.rs b/crates/horizon-client/src/game/lan_entities.rs index 6342e921c..f3098408c 100644 --- a/crates/horizon-client/src/game/lan_entities.rs +++ b/crates/horizon-client/src/game/lan_entities.rs @@ -41,6 +41,7 @@ fn state(id: u32, kind: &str, position: [f64; 3], yaw: f32, on_ground: bool) -> attributes: Vec::new(), precise: false, needs_sync: false, + leash: None, } } diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 19306efd7..dfde08d7f 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -1874,6 +1874,19 @@ impl Game<'_> { ); } C::SetPassengers(p) => self.remote_set_passengers(p.vehicle, &p.passengers), + // ClientPacketListener.handleEntityLinkPacket: who holds a mob's + // lead (this player, another player or a mob), for the rope. + C::SetEntityLink(p) => { + let me = self.remote.as_ref().map(|r| r.entity_id); + if let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.source as u32) { + use horizon_client::mobs::leash::Holder; + m.extra.leash = match p.dest { + 0 => None, + d if Some(d) == me => Some(Holder::Player), + d => Some(Holder::Mob(d as u32)), + }; + } + } C::MoveVehicle(p) => self.remote_move_vehicle(p.position, p.yaw), // ClientPacketListener.handleRotateMob: lerpHeadTo(yHeadRot, 3). C::RotateHead(p) => { diff --git a/crates/horizon-client/src/lan/entity.rs b/crates/horizon-client/src/lan/entity.rs index 9bdb037f7..676fd0adc 100644 --- a/crates/horizon-client/src/lan/entity.rs +++ b/crates/horizon-client/src/lan/entity.rs @@ -12,8 +12,8 @@ use horizon_protocol::{ item::Stack, packets::play::{ self, AddEntity, AttributeSnapshot, EntityPositionSync, MoveEntity, MoveEntityPos, - MoveEntityPosRot, MoveEntityRot, PositionPath, RotateHead, SetEntityData, SetEntityMotion, - SetEquipment, SetPassengers, UpdateAttributes, VecDelta, + MoveEntityPosRot, MoveEntityRot, PositionPath, RotateHead, SetEntityData, SetEntityLink, + SetEntityMotion, SetEquipment, SetPassengers, UpdateAttributes, VecDelta, }, }; @@ -65,6 +65,8 @@ pub struct EntityState { pub precise: bool, /// `Entity.needsSync`: send its changes this tick, due or not. pub needs_sync: bool, + /// `Leashable.getLeashHolder`: the entity holding its lead. + pub leash: Option, } impl EntityState { @@ -87,6 +89,7 @@ impl EntityState { self.attributes.clear(); self.precise = false; self.needs_sync = false; + self.leash = None; } } @@ -219,6 +222,8 @@ struct ServerEntity { /// and the first update after it dismounts is a full position sync. passengers: Vec, was_riding: bool, + /// The lead's holder the clients were told of. + leash: Option, /// The level tick it was last seen in, its place in that tick's list, and /// whether it was added in it (its first update is the next tick's). seen: u32, @@ -262,6 +267,7 @@ impl ServerEntity { teleport_delay: 0, passengers: riding.passengers(e.id).to_vec(), was_riding: false, + leash: e.leash, seen: 0, index: 0, added: true, @@ -314,6 +320,13 @@ impl ServerEntity { if let Some(&vehicle) = riding.vehicle.get(&e.id) { out.push(riding.set_passengers(vehicle)); } + // A leashed entity: who holds its lead. + if let Some(dest) = e.leash { + out.push(play::Clientbound::SetEntityLink(SetEntityLink { + source: e.id, + dest, + })); + } } /// Changes since the last update (ServerEntity.sendChanges): movement and @@ -327,6 +340,14 @@ impl ServerEntity { self.passengers.clear(); self.passengers.extend_from_slice(passengers); } + // Leashable.setLeashedTo / dropLeash: sendToTrackingPlayers at once. + if e.leash != self.leash { + out.push(play::Clientbound::SetEntityLink(SetEntityLink { + source: e.id, + dest: e.leash.unwrap_or(0), + })); + self.leash = e.leash; + } // SynchedEntityData.isDirty: once for every client. let dirty = e.meta != self.meta; let due = self.interval != 0 && self.tick.is_multiple_of(self.interval); @@ -868,6 +889,7 @@ mod tests { attributes: Vec::new(), precise: false, needs_sync: false, + leash: None, } } @@ -1132,6 +1154,24 @@ mod tests { assert_eq!(eq.slots, vec![(0, None)]); } + #[test] + fn a_lead_tied_and_untied_reaches_the_clients() { + let link = |out: &[play::Clientbound]| { + out.iter().find_map(|p| match p { + play::Clientbound::SetEntityLink(l) => Some((l.source, l.dest)), + _ => None, + }) + }; + let mut e = state(0.0); + let mut s = added(&e); + changes(&mut s, &e); + e.leash = Some(7); + assert_eq!(link(&changes(&mut s, &e)), Some((e.id, 7))); + assert_eq!(link(&changes(&mut s, &e)), None); + e.leash = None; + assert_eq!(link(&changes(&mut s, &e)), Some((e.id, 0))); + } + /// Two clients at the origin; the chunks they hold. struct Clients { chunks: crate::fast_hash::FxHashSet<(i32, i32)>, diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index 1330b0ae4..c8f2e980d 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -1564,6 +1564,7 @@ impl Hub { attributes, precise: false, needs_sync: false, + leash: None, } } diff --git a/crates/horizon-client/tests/lan_bot.rs b/crates/horizon-client/tests/lan_bot.rs index 4b8bbc897..fdafcbb68 100644 --- a/crates/horizon-client/tests/lan_bot.rs +++ b/crates/horizon-client/tests/lan_bot.rs @@ -435,6 +435,7 @@ fn guests_see_each_other_and_the_world_entities() { attributes: Vec::new(), precise: false, needs_sync: false, + leash: None, }; let first = thread::spawn(move || { let mut bot = connect(port, "Alex", false)?; diff --git a/crates/horizon-protocol/src/packets/play.rs b/crates/horizon-protocol/src/packets/play.rs index 24eec6a05..23ba00699 100644 --- a/crates/horizon-protocol/src/packets/play.rs +++ b/crates/horizon-protocol/src/packets/play.rs @@ -671,6 +671,11 @@ packet!(SetCursorItem { item: Option => Slot }); packet!(SetDefaultSpawnPosition { pos: GlobalPos => GlobalPos, yaw: f32 => Float, pitch: f32 => Float }); packet!(SetEntityData { id: i32 => VarInt, values: Vec<(u8, i32, Value)> => EntityData }); packet!(SetEntityMotion { id: i32 => VarInt, velocity: [f64; 3] => LpVec3 }); +packet!( + /// ClientboundSetEntityLinkPacket: the entity holding a leashed entity's + /// lead, 0 when none (Leashable.setDelayedLeashHolderId). + SetEntityLink { source: i32 => Int, dest: i32 => Int } +); packet!( /// ClientboundSetPassengersPacket: a vehicle and every entity riding it. SetPassengers { vehicle: i32 => VarInt, passengers: Vec => List } @@ -1532,6 +1537,7 @@ packet_enum!(Clientbound, crate::ids::play::clientbound { SetDisplayObjective(SetDisplayObjective) = SET_DISPLAY_OBJECTIVE, SetEntityData(SetEntityData) = SET_ENTITY_DATA, SetEntityMotion(SetEntityMotion) = SET_ENTITY_MOTION, + SetEntityLink(SetEntityLink) = SET_ENTITY_LINK, SetEquipment(SetEquipment) = SET_EQUIPMENT, SetExperience(SetExperience) = SET_EXPERIENCE, SetHealth(SetHealth) = SET_HEALTH, @@ -2263,6 +2269,7 @@ mod tests { vehicle: 2, passengers: vec![3, 300], }), + Clientbound::SetEntityLink(SetEntityLink { source: 2, dest: 0 }), Clientbound::ProjectilePower(ProjectilePower { id: 4, power: 0.1 }), Clientbound::SetEquipment(SetEquipment { id: 2, diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index de8c78dc3..b3dae9ef4 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -29,6 +29,8 @@ address. The script then checks, from both sides, what a guest's game used to ge cow on fire burns and goes out; then the wither's health drops when it is hurt, the sheep is dyed blue with its wool back and the calf grows up (#1367, #1368, #1370, #1371, #1385, #1390); +- leads: the host and the guest each tie a cow to a lead and untie it: the rope shows on both + sides, held by the player who tied it, and goes on both (#1360); - respawn: a player killed is drawn dead by the other side, and alive again once it respawns (the respawned player is a new entity to the others); the host dies, and on the server thread the guest too; @@ -206,7 +208,7 @@ ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' # The host plays single player on its server thread (--server-thread). SERVER_THREAD = False -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'looks', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'lightning', 'riding', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption', 'breath', 'lava', 'levels', 'drops', 'underwater'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'looks', 'leads', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'lightning', 'riding', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption', 'breath', 'lava', 'levels', 'drops', 'underwater'] class Game: @@ -1225,6 +1227,43 @@ def check_looks(host, guest, out): return seen['later']['guest'] +def check_leads(host, guest, out): + spots = apart(host, guest) + result = {} + for label, player, other in (('host', host, guest), ('guest', guest, host)): + x, y, z = spots[label] + at = (x + 0.5, y, z + 2.5) + + def cow(game): + found = [m for m in game.raw('mobs', 32).get('near', []) if m['kind'] == 'minecraft:cow' + and abs(m['pos'][0] - at[0]) < 1 and abs(m['pos'][2] - at[2]) < 1] + assert found, f'{game.name} sees no cow at {at}' + return found[0] + player('command', command='gamemode survival') + player('command', command=f'summon minecraft:cow {at[0]} {y} {at[2]} {{NoAI:1b}}') + player('command', command='item replace entity @s weapon.mainhand with minecraft:lead') + player.wait(30) + try: + player.raw('look-at', f'{at[0]:.2f}', f'{y + 0.7:.2f}', f'{at[2]:.2f}') + player.raw('mouse', 'right', 'click') + player.wait(20) + tied = {'own': cow(player)['leash'], 'other': cow(other)['leash']} + player.shot(out, 'leads-' + label) + other.shot(out, f'leads-{label}-seen') + # An empty hand unties it (the lead was used up). + player.raw('mouse', 'right', 'click') + player.wait(20) + untied = {'own': cow(player)['leash'], 'other': cow(other)['leash']} + finally: + player('command', command=f'kill @e[type=!minecraft:player,x={at[0]},y={y},z={at[2]},distance=..6]') + player('command', command='clear @s') + result[label] = {'tied': tied, 'untied': untied} + # The other side draws it held by the player who tied it (another player to it). + assert tied['own'] == 'Player' and tied['other'], f"the {label}'s cow on its lead: {tied}" + assert untied == {'own': None, 'other': None}, f"the {label}'s cow untied: {untied}" + return result + + def container_items(game): """The open container's stacks by slot (Horizon's container slots are 300 and up) and its size.""" slots = [s for s in game('screen').get('slots', []) if s.get('slot', 0) >= 300] @@ -3905,7 +3944,8 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'contacts': check_contacts, 'absorption': check_absorption, 'breath': check_breath, 'lava': check_lava, 'levels': check_levels, 'drops': check_drops, 'underwater': check_underwater, - 'looks': check_looks} + 'looks': check_looks, + 'leads': check_leads} def run(ctl, out, resources, checks): From c5fe5d4bcf3331164d7a7ade1f5ac7cff2481aa6 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 07:55:49 +0200 Subject: [PATCH 09/39] Thrown potions and eyes of ender fly on a server's clients A splash or lingering potion, an experience bottle or an eye of ender a player threw was never an entity a server tracked: on single player's server thread, for LAN guests and on a dedicated server the item was used up and nothing flew. The server now numbers them like its other projectiles and tracks them as vanilla's entities: splash_potion, lingering_potion and experience_bottle with ThrowableItemProjectile.DATA_ITEM_STACK (the potion, for its colour), and eye_of_ender. A client draws them where the server puts them and drops them when the server removes them. lan_e2e's new `thrown` check: the host and the guest each throw a splash potion and an eye of ender, and both sides see them fly. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/end.rs | 51 ++++++++++++ .../horizon-client/src/game/lan_entities.rs | 22 ++++++ crates/horizon-client/src/game/potions.rs | 78 +++++++++++++++++-- .../src/game/remote_projectiles.rs | 35 +++++++++ tools/lan_e2e.py | 36 ++++++++- 5 files changed, 212 insertions(+), 10 deletions(-) diff --git a/crates/horizon-client/src/game/end.rs b/crates/horizon-client/src/game/end.rs index d62c8ccc2..599158e3e 100644 --- a/crates/horizon-client/src/game/end.rs +++ b/crates/horizon-client/src/game/end.rs @@ -97,8 +97,58 @@ impl EndState { } } +impl EndState { + /// The eyes' entity ids, for a server to number them. + pub(super) fn eye_ids(&mut self) -> impl Iterator { + self.eyes.iter_mut().map(|e| &mut e.id) + } + /// The numbered eyes as a server's clients track them: (id, position, motion). + pub(super) fn eyes_tracked(&self) -> impl Iterator { + self.eyes + .iter() + .filter(|e| e.id != 0) + .map(|e| (e.id, e.position.to_array(), e.velocity.to_array())) + } + /// `handleAddEntity` for an eye of ender on a server's client: it flies + /// where the server says. + pub(super) fn add_remote_eye(&mut self, id: u32, at: [f64; 3], velocity: [f64; 3]) { + self.eyes.retain(|e| e.id != id); + let at = glam::DVec3::from_array(at); + self.eyes.push(EyeOfEnder { + id, + position: at, + previous: at, + velocity: glam::DVec3::from_array(velocity), + target: at, + life: 0, + survive: false, + }); + } + /// One tick of a server's client: `step` moves each toward where the + /// server put it. + pub(super) fn step_remote_eyes(&mut self, mut step: impl FnMut(u32, [f64; 3]) -> [f64; 3]) { + for e in &mut self.eyes { + e.previous = e.position; + e.life += 1; + e.position = glam::DVec3::from_array(step(e.id, e.position.to_array())); + } + } + pub(super) fn remove_remote_eye(&mut self, id: u32) { + self.eyes.retain(|e| e.id != id); + } + pub(super) fn clear_remote_eyes(&mut self) { + self.eyes.clear(); + } + /// Where each eye is, for `status`. + pub(super) fn eye_positions(&self) -> impl Iterator { + self.eyes.iter().map(|e| (e.id, e.position.to_array())) + } +} + /// The EyeOfEnder entity. struct EyeOfEnder { + /// Its entity id once a server's clients track it (0 until then). + id: u32, position: glam::DVec3, previous: glam::DVec3, velocity: glam::DVec3, @@ -1023,6 +1073,7 @@ impl Game<'_> { }; let survive = self.world.random.next_int_bound(5).unwrap_or(0) > 0; self.end.eyes.push(EyeOfEnder { + id: 0, position: start, previous: start, velocity: glam::DVec3::ZERO, diff --git a/crates/horizon-client/src/game/lan_entities.rs b/crates/horizon-client/src/game/lan_entities.rs index f3098408c..db0c3e8b2 100644 --- a/crates/horizon-client/src/game/lan_entities.rs +++ b/crates/horizon-client/src/game/lan_entities.rs @@ -277,6 +277,8 @@ impl Game<'_> { self.rockets.iter_mut().for_each(|r| give(&mut r.id)); self.crystals.iter_mut().for_each(|c| give(&mut c.id)); self.bolts.iter_mut().for_each(|b| give(&mut b.id)); + self.potions.thrown_ids().for_each(&mut give); + self.end.eye_ids().for_each(&mut give); } /// Primed TNT, falling blocks, projectiles, tridents, firework rockets and @@ -359,6 +361,26 @@ impl Game<'_> { } out.push(e); } + // Thrown potions and bottles: ThrowableItemProjectile.DATA_ITEM_STACK + // (the potion's colour), which a plain bottle's type already has. + for (id, kind, stack, position, velocity) in self.potions.thrown_tracked() { + let mut e = state(id, kind, at(position), 0.0, false); + e.velocity = at(velocity); + if stack.extra.is_some() || stack.count != 1 { + e.meta.push(( + 8, + serializer::ITEM_STACK, + Value::Item(hub.stack(&Some(stack.clone()))), + )); + } + out.push(e); + } + // EyeOfEnder: its item is the plain eye (DATA_ITEM_STACK's default). + for (id, position, velocity) in self.end.eyes_tracked() { + let mut e = state(id, "minecraft:eye_of_ender", position, 0.0, false); + e.velocity = velocity; + out.push(e); + } for t in self.tridents.iter().filter(|t| t.id != 0) { let mut e = state(t.id, "minecraft:trident", at(t.position), t.yaw, false); e.pitch = t.pitch; diff --git a/crates/horizon-client/src/game/potions.rs b/crates/horizon-client/src/game/potions.rs index 72c8fe885..1664ae38e 100644 --- a/crates/horizon-client/src/game/potions.rs +++ b/crates/horizon-client/src/game/potions.rs @@ -18,6 +18,8 @@ use horizon_core::{geometry::Vec3, random::RandomSource}; /// A thrown splash or lingering potion in flight. #[derive(Clone, Debug)] pub(super) struct ThrownPotion { + /// Its entity id once a server's clients track it (0 until then). + id: u32, stack: ItemStack, /// The position before the last tick, for drawing between ticks. previous: Vec3, @@ -47,6 +49,7 @@ impl Game<'_> { /// player's throws and dispensers both end up here). pub(super) fn launch_thrown(&mut self, stack: ItemStack, position: Vec3, velocity: Vec3) { self.potions.thrown.push(ThrownPotion { + id: 0, stack, previous: position, position, @@ -90,19 +93,78 @@ impl PotionState { pub(super) fn thrown_entities(&self) -> Vec<(&'static str, Vec3, Vec3)> { self.thrown .iter() - .map(|t| { - let kind = match &*t.stack.item { - "minecraft:lingering_potion" => "minecraft:lingering_potion", - "minecraft:experience_bottle" => "minecraft:experience_bottle", - _ => "minecraft:splash_potion", - }; - (kind, t.position, t.velocity) - }) + .map(|t| (thrown_kind(&t.stack), t.position, t.velocity)) .collect() } pub(super) fn clear_thrown(&mut self) { self.thrown.clear(); } + /// Their entity ids, for a server to number them. + pub(super) fn thrown_ids(&mut self) -> impl Iterator { + self.thrown.iter_mut().map(|t| &mut t.id) + } + /// The numbered ones as a server's clients track them: (id, entity + /// type, item, position, motion). + pub(super) fn thrown_tracked( + &self, + ) -> impl Iterator { + self.thrown.iter().filter(|t| t.id != 0).map(|t| { + ( + t.id, + thrown_kind(&t.stack), + &t.stack, + t.position, + t.velocity, + ) + }) + } + /// `handleAddEntity` for a thrown potion or bottle on a server's client: + /// drawn where the server says (its item comes in its entity data). + pub(super) fn add_remote_thrown(&mut self, id: u32, kind: &str, at: Vec3, velocity: Vec3) { + self.thrown.retain(|t| t.id != id); + let item = match kind { + "minecraft:lingering_potion" | "minecraft:experience_bottle" => kind, + _ => "minecraft:splash_potion", + }; + self.thrown.push(ThrownPotion { + id, + stack: ItemStack::new(item, 1), + previous: at, + position: at, + velocity, + age: 0, + }); + } + /// ThrowableItemProjectile.DATA_ITEM_STACK: the potion it shows. + pub(super) fn remote_thrown_item(&mut self, id: u32, stack: ItemStack) { + if let Some(t) = self.thrown.iter_mut().find(|t| t.id == id) { + t.stack = stack; + } + } + /// One tick of a server's client: `step` moves each toward where the + /// server put it. + pub(super) fn step_remote_thrown(&mut self, mut step: impl FnMut(u32, Vec3) -> Vec3) { + for t in &mut self.thrown { + t.previous = t.position; + t.age += 1; + t.position = step(t.id, t.position); + } + } + pub(super) fn remove_remote_thrown(&mut self, id: u32) { + self.thrown.retain(|t| t.id != id); + } + pub(super) fn has_thrown(&self, id: u32) -> bool { + self.thrown.iter().any(|t| t.id == id) + } +} + +/// The entity type of a thrown potion or bottle. +fn thrown_kind(stack: &ItemStack) -> &'static str { + match &*stack.item { + "minecraft:lingering_potion" => "minecraft:lingering_potion", + "minecraft:experience_bottle" => "minecraft:experience_bottle", + _ => "minecraft:splash_potion", + } } impl Game<'_> { diff --git a/crates/horizon-client/src/game/remote_projectiles.rs b/crates/horizon-client/src/game/remote_projectiles.rs index c008611f6..82d0a11e2 100644 --- a/crates/horizon-client/src/game/remote_projectiles.rs +++ b/crates/horizon-client/src/game/remote_projectiles.rs @@ -79,6 +79,15 @@ impl Game<'_> { self.rockets .push(super::fireworks::Rocket::remote(id, at, velocity)); } + "minecraft:splash_potion" + | "minecraft:lingering_potion" + | "minecraft:experience_bottle" => { + self.potions.add_remote_thrown(id, kind, at, velocity); + } + "minecraft:eye_of_ender" => { + self.end + .add_remote_eye(id, position, [velocity.x, velocity.y, velocity.z]) + } "minecraft:end_crystal" => self.crystals.push(super::dragon::Crystal { id, position: at, @@ -105,6 +114,17 @@ impl Game<'_> { /// `handleSetEntityData` for these types: what changes how they look. pub(super) fn remote_projectile_data(&mut self, id: i32, values: &[(u8, i32, Value)]) { let id = id as u32; + if self.potions.has_thrown(id) { + // ThrowableItemProjectile.DATA_ITEM_STACK: the potion it is. + for (index, _, value) in values { + if let (8, Value::Item(stack)) = (index, value) + && let Some(stack) = self.to_stack(stack) + { + self.potions.remote_thrown_item(id, stack); + } + } + return; + } for (index, _, value) in values { if let Some(t) = self.tnts.iter_mut().find(|t| t.id == id) { // PrimedTnt.DATA_FUSE_ID. @@ -184,6 +204,11 @@ impl Game<'_> { c.time += 1; c.position = step(c.id, c.position); } + self.potions.step_remote_thrown(&mut step); + self.end.step_remote_eyes(|id, at| { + let at = step(id, Vec3::new(at[0], at[1], at[2])); + [at.x, at.y, at.z] + }); } pub(super) fn remove_remote_projectile(&mut self, id: u32) { @@ -193,6 +218,8 @@ impl Game<'_> { self.tridents.retain(|t| t.id != id); self.rockets.retain(|r| r.id != id); self.crystals.retain(|c| c.id != id); + self.potions.remove_remote_thrown(id); + self.end.remove_remote_eye(id); } /// Every one a server sent (a new connection, level or dimension). @@ -203,6 +230,8 @@ impl Game<'_> { self.tridents.clear(); self.rockets.clear(); self.crystals.clear(); + self.potions.clear_thrown(); + self.end.clear_remote_eyes(); } /// Their kind and place, for `status`. @@ -225,6 +254,12 @@ impl Game<'_> { for p in &self.creatures.projectiles { out.push(json!({"id": p.id, "kind": format!("{:?}", p.kind).split([' ', '{']).next().unwrap_or(""), "position": at(p.position)})); } + for (kind, p, _) in self.potions.thrown_entities() { + out.push(json!({"kind": kind, "position": at(p)})); + } + for (id, p) in self.end.eye_positions() { + out.push(json!({"id": id, "kind": "minecraft:eye_of_ender", "position": p})); + } out } } diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index b3dae9ef4..c4d8084ba 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -107,6 +107,8 @@ address. The script then checks, from both sides, what a guest's game used to ge guest as the host has them (kind, place, rotation, what they hold or wear, the variant). - projectiles: primed TNT (one falling), an end crystal and an arrow that lands reach the guest where the host has them; a falling block lands on both sides. +- thrown: a splash potion and an eye of ender the host and the guest each throw fly on both + sides (#1363, #1391); - fireball: a fire charge a dispenser shoots onto stone lights a fire both sides see, and the fire goes out on its own within a minute (it ticks: FireBlock.tick on a floor that doesn't burn) @@ -208,7 +210,7 @@ ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' # The host plays single player on its server thread (--server-thread). SERVER_THREAD = False -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'looks', 'leads', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'lightning', 'riding', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption', 'breath', 'lava', 'levels', 'drops', 'underwater'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'looks', 'leads', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'thrown', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'lightning', 'riding', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption', 'breath', 'lava', 'levels', 'drops', 'underwater'] class Game: @@ -2612,6 +2614,35 @@ def check_projectiles(host, guest, out): return f'{len(theirs)} identical, fuses (host, guest) {fuses}' +def check_thrown(host, guest, out): + spots = apart(host, guest) + result = {} + for label, player, other in (('host', host, guest), ('guest', guest, host)): + x, y, z = spots[label] + player('command', command='gamemode survival') + for item, kind in (('splash_potion', 'minecraft:splash_potion'), ('ender_eye', 'minecraft:eye_of_ender')): + player('command', command=f'item replace entity @s weapon.mainhand with minecraft:{item}') + player.wait(10) + # Up at 45 degrees, into open sky. + player.raw('look-at', f'{x + 0.5:.2f}', f'{y + 11.6:.2f}', f'{z + 10.5:.2f}') + player.raw('use-item') + seen = {'own': 0, 'other': 0} + for _ in range(10): + player.wait(2) + for side, game in (('own', player), ('other', other)): + if any(p['kind'] == kind for p in game('status')['projectiles']): + seen[side] += 1 + if all(seen.values()): + break + player.shot(out, f'thrown-{label}-{item}') + other.shot(out, f'thrown-{label}-{item}-seen') + player.wait(100) # it lands or drops + result[f'{label} {item}'] = seen + for name, seen in result.items(): + assert all(seen.values()), f'{name}: seen in flight (samples) {result}' + return result + + def check_blast(host, guest, out): guest('command', command='gamemode survival') guest('command', command='clear @s') @@ -3945,7 +3976,8 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'lava': check_lava, 'levels': check_levels, 'drops': check_drops, 'underwater': check_underwater, 'looks': check_looks, - 'leads': check_leads} + 'leads': check_leads, + 'thrown': check_thrown} def run(ctl, out, resources, checks): From 13772b4e5bbc153f2ea896f44543dd75002e7b69 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 08:08:42 +0200 Subject: [PATCH 10/39] Level events on a server's clients: spawner bursts, bone meal sparks, waxing A client of a server (single player on its server thread, a LAN guest, a dedicated server's player) played only three of the level events the server sent (a block broken, the dragon egg's and a consumable's teleport): a spawner's burst, waxing and scraping copper, a composter filling and the rest showed nothing. Every one now goes through the handler the in-frame game uses (LevelEventHandler). Bone meal's level event (1505) played its sound but never its growth particles, in either mode: it now spawns them as BoneMealItem. addGrowthParticles does (spread over a grass block's, nylium's, netherrack's or moss's neighbourhood and water, inside a growing block, below rooted dirt and mangrove leaves). The server also sends it to the player who used the bone meal, as vanilla's levelEvent names no player. Waxing copper with honeycomb and an axe or hoe's transforms (wax off, scrape, strip, till) make their particles and sound on the clicker's own client, as vanilla's client runs Item.useOn; the server sends them to the others only. The transform's preview is shared with the in-frame use. `horizon-ctl status` counts the particles made by type. lan_e2e's new `sparks` check: the host and the guest each use bone meal on wheat, wax copper and take the wax off, and a spawner spawns a zombie; both sides make the particles. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 92 +++++++++++-------- crates/horizon-client/src/game/control.rs | 2 + crates/horizon-client/src/game/lan.rs | 10 +- .../src/game/level_particles.rs | 67 +++++++++++++- crates/horizon-client/src/game/remote.rs | 61 +++++++----- crates/horizon-client/src/particles.rs | 8 ++ tools/lan_e2e.py | 73 ++++++++++++++- 7 files changed, 245 insertions(+), 68 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 27ac5f259..11d94ef48 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2573,6 +2573,17 @@ impl Game<'_> { LevelEvent::LevelEvent { id: 2004, pos, .. } => { self.spawner_burst([pos.x, pos.y, pos.z]) } + // LevelEventHandler 1505: the growth particles, then the sound. + LevelEvent::LevelEvent { + id: 1505, + pos, + data, + } => { + self.growth_particles(pos, data); + let at = [pos.x, pos.y, pos.z].map(|v| f64::from(v) + 0.5); + self.audio + .play("minecraft:item.bone_meal.use", Some(at), 1.0, 1.0); + } LevelEvent::LevelEvent { id: 2015, pos, @@ -2616,7 +2627,6 @@ impl Game<'_> { 1030 => Some(("minecraft:block.anvil.use", 1.0)), 1029 => Some(("minecraft:block.anvil.destroy", 1.0)), 1031 => Some(("minecraft:block.anvil.land", 0.3)), - 1505 => Some(("minecraft:item.bone_meal.use", 1.0)), 1042 => Some(("minecraft:block.grindstone.use", 1.0)), 1044 => Some(("minecraft:block.smithing_table.use", 1.0)), // LevelEvent.SOUND_PAGE_TURN (a lectern's page). @@ -8090,13 +8100,15 @@ impl Game<'_> { } Ok(true) } - /// `BlockTransformer.transformBlock` for a held axe, hoe or shovel. - fn use_block_transformer( - &mut self, + /// What `BlockTransformer.transformBlock` would do with a held axe, hoe + /// or shovel on the block at `pos`, without doing it: the new state, its + /// sound, loot table, the durability it costs and its particle. + pub(super) fn block_transform( + &self, stack: &ItemStack, pos: [i32; 3], face: [i32; 3], - ) -> Result { + ) -> Option<(BlockStateId, String, Option, u32, String)> { use horizon_client::block_transform::Around; struct Column<'w> { world: &'w World, @@ -8116,14 +8128,11 @@ impl Game<'_> { }) } } - let Some(id) = self + let id = self .res .items() .get(&stack.item) - .and_then(|i| i.block_transformer.clone()) - else { - return Ok(false); - }; + .and_then(|i| i.block_transformer.clone())?; // playerHasBlockingItemUseIntent: a shield in the off hand is raised // instead, unless sneaking. let shield = self @@ -8132,7 +8141,7 @@ impl Game<'_> { .and_then(|s| self.res.items().get(&s.item)) .is_some_and(|i| i.blocks_attacks.is_some()); if shield && !self.keys.contains(&KeyCode::ShiftLeft) { - return Ok(false); + return None; } let face_name = match face { [0, -1, 0] => "down", @@ -8143,38 +8152,47 @@ impl Game<'_> { _ => "east", }; let registry = self.pack.assets.registry; - let (state, sound, loot, damage, particle) = { - let around = Column { - world: &self.world, - registry, - pos, - }; - let Some((_, t, state)) = - self.res - .transformers() - .apply(self.res.items(), registry, &id, face_name, &around) - else { - return Ok(false); - }; - ( - state, - t.sound.clone(), - t.loot.clone(), - t.damage, - t.particle.clone(), - ) + let around = Column { + world: &self.world, + registry, + pos, }; - let Ok(new) = registry.resolve(&state) else { - return Ok(false); - }; - self.level_set(pos, new)?; - // BlockTransformer.TransformParticle.send. - match particle.as_str() { + let (_, t, state) = + self.res + .transformers() + .apply(self.res.items(), registry, &id, face_name, &around)?; + let new = registry.resolve(&state).ok()?; + Some(( + new, + t.sound.clone(), + t.loot.clone(), + t.damage, + t.particle.clone(), + )) + } + /// `BlockTransformer.TransformParticle.send`: its level event. + pub(super) fn transform_particle(&mut self, particle: &str, pos: [i32; 3]) { + match particle { "scrape" => self.level_event_at(3005, pos, 0), "wax_on" => self.level_event_at(3003, pos, 0), "wax_off" => self.level_event_at(3004, pos, 0), _ => {} } + } + /// `BlockTransformer.transformBlock` for a held axe, hoe or shovel. + fn use_block_transformer( + &mut self, + stack: &ItemStack, + pos: [i32; 3], + face: [i32; 3], + ) -> Result { + let Some((new, sound, loot, damage, particle)) = self.block_transform(stack, pos, face) + else { + return Ok(false); + }; + self.level_set(pos, new)?; + // BlockTransformer.TransformParticle.send. + self.transform_particle(&particle, pos); if loot.as_deref() == Some("minecraft:till/rooted_dirt") { self.pop_resource( [pos[0] + face[0], pos[1] + face[1], pos[2] + face[2]], diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index 9132c7723..5645441f0 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -971,6 +971,8 @@ impl Game<'_> { "flashes": self.lightning_seen[1], "flash": self.flash}); // Primed TNT, falling blocks, projectiles, tridents and end crystals. status["projectiles"] = json!(self.projectiles_status()); + // The particles made so far, by type. + status["particles"] = json!(self.particles.made()); status } } diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 8467b6d3a..72b3b3fc5 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -1087,8 +1087,14 @@ impl Game<'_> { } for e in events { if let horizon_world::level::LevelEvent::LevelEvent { id, pos, data } = e { - // The acting guest does not predict the egg's teleport particles. - let except = if *id == 2015 { None } else { self.lan_acting }; + // The acting guest does not predict the egg's teleport + // particles, nor bone meal's growth (BoneMealItem.useOn's + // levelEvent names no player). + let except = if matches!(*id, 2015 | 1505) { + None + } else { + self.lan_acting + }; self.lan_events .push((*id, [pos.x, pos.y, pos.z], *data, except)); } diff --git a/crates/horizon-client/src/game/level_particles.rs b/crates/horizon-client/src/game/level_particles.rs index 47d06254d..d239c69d0 100644 --- a/crates/horizon-client/src/game/level_particles.rs +++ b/crates/horizon-client/src/game/level_particles.rs @@ -1,10 +1,10 @@ //! Level events whose client handler (`LevelEventHandler`) spawns particles -//! on a block: dragon egg teleportation (2015), a consumable's teleport -//! (2017), electric sparks (3002), waxing (3003), wax off (3004) and -//! scraping (3005). +//! on a block: bone meal's growth (1505), dragon egg teleportation (2015), a +//! consumable's teleport (2017), electric sparks (3002), waxing (3003), wax +//! off (3004) and scraping (3005). use super::Game; use horizon_client::particles::{self, ParticleOptions, ParticleType}; -use horizon_core::position::BlockPos; +use horizon_core::{position::BlockPos, random::RandomSource}; use horizon_world::level::LevelEvent; impl Game<'_> { @@ -42,6 +42,65 @@ impl Game<'_> { self.particles.add(&options, p, v); } } + /// `BoneMealItem.addGrowthParticles` (event 1505, `count` the data): + /// happy villager sparks over a spreading block's neighbourhood (grass, + /// nylium, netherrack, moss) or water, or inside a growing block. + pub(super) fn growth_particles(&mut self, pos: BlockPos, count: i32) { + let p = [pos.x, pos.y, pos.z]; + let state = self.world.block(p[0], p[1], p[2]); + let facts = self.res.facts(); + // ParticleUtils.spawnParticles(level, pos, count, xzSpread, ySpread, + // allowFloatingParticles, particle). + let (at, count, xz, height, floating) = match self.res.behaviours().bone_meal_spreads(state) + { + Some(spreads) => { + // BonemealableBlock.getParticlePos: below for rooted dirt + // (its hanging roots) and mangrove leaves (their propagule). + let at = match &*facts.get(state).name { + "minecraft:rooted_dirt" | "minecraft:mangrove_leaves" => [p[0], p[1] - 1, p[2]], + _ => p, + }; + if spreads { + (at, count * 3, 3.0, 1.0, false) + } else { + // ParticleUtils.spawnParticleInBlock: up to the + // block's shape (a full block when air). + let inside = self.world.block(at[0], at[1], at[2]); + let height = if facts.is_air(inside) { + 1.0 + } else { + facts + .get(inside) + .outline + .iter() + .map(|b| b[4]) + .fold(0.0, f64::max) + }; + (at, count, 0.5, height, true) + } + } + None if &*facts.get(state).name == "minecraft:water" => (p, count * 3, 3.0, 1.0, false), + None => return, + }; + let options = ParticleOptions::simple(ParticleType::HappyVillager); + for _ in 0..count.max(0) { + let r = self.particles.level_random(); + let v = [0.0; 3].map(|_: f64| r.next_gaussian() * 0.02); + let start = 0.5 - xz; + let x = f64::from(at[0]) + start + r.next_double() * xz * 2.0; + let y = f64::from(at[1]) + r.next_double() * height; + let z = f64::from(at[2]) + start + r.next_double() * xz * 2.0; + let below = [x.floor() as i32, y.floor() as i32 - 1, z.floor() as i32]; + if floating + || !self + .res + .facts() + .is_air(self.world.block(below[0], below[1], below[2])) + { + self.particles.add(&options, [x, y, z], v); + } + } + } /// Queue and play level event `id` at `pos` (`Level.levelEvent`). pub(super) fn level_event_at(&mut self, id: i32, pos: [i32; 3], data: i32) { self.level_events.push(LevelEvent::LevelEvent { diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index dfde08d7f..578af5f69 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -1725,29 +1725,10 @@ impl Game<'_> { self.block_entity_event(p.pos, i32::from(p.b0), i32::from(p.b1)); } } - C::LevelEvent(p) if p.kind == 2015 => { - self.dragon_egg_particles(p.pos, p.data); - } - C::LevelEvent(p) if p.kind == 2017 => { - self.consume_teleport_particles(p.pos, p.data); - } - C::LevelEvent(p) if p.kind == 2001 => { - // LevelEvent.PARTICLES_DESTROY_BLOCK: someone else broke a block. - let state = BlockStateId(p.data.max(0) as u32); - let s = self.res.facts().get(state).sounds.clone(); - let at = [ - f64::from(p.pos[0]) + 0.5, - f64::from(p.pos[1]) + 0.5, - f64::from(p.pos[2]) + 0.5, - ]; - self.audio.play( - &s.break_sound, - Some(at), - (s.volume + 1.0) / 2.0, - s.pitch * 0.8, - ); - self.destroy_particles(p.pos, state); - } + // ClientLevel.levelEvent: LevelEventHandler, as this client's own + // level plays them (particles and sounds: a spawner's burst, bone + // meal, waxing, a block broken by someone else). + C::LevelEvent(p) => self.level_event_at(p.kind, p.pos, p.data), C::Sound(p) => { let name = match &p.sound.1 { Some((location, _)) => Some(location.clone()), @@ -3821,6 +3802,8 @@ impl Game<'_> { } if reacts { self.predict_use(&changes, seq); + } else if let Some(stack) = held.as_ref() { + self.predict_item_on_block(stack, &hit); } if held_block { if reacts @@ -3839,6 +3822,38 @@ impl Game<'_> { false } + /// `Item.useOn` on this client's level for the items whose effects are + /// the clicker's own (`Level.levelEvent(player, ...)` and + /// `playSound(player, ...)`: the server sends them to the others only): + /// honeycomb waxing copper, and an axe, hoe or shovel transforming a + /// block (stripping, scraping, removing wax, tilling, making a path). + fn predict_item_on_block(&mut self, stack: &ItemStack, hit: &super::Hit) { + let at = [ + f64::from(hit.pos[0]) + 0.5, + f64::from(hit.pos[1]) + 0.5, + f64::from(hit.pos[2]) + 0.5, + ]; + if &*stack.item == "minecraft:honeycomb" { + // HoneycombItem.useOn. + let registry = self.pack.assets.registry; + let clicked = self.world.block(hit.pos[0], hit.pos[1], hit.pos[2]); + let waxes = registry + .state(clicked) + .and_then(|old| self.res.transformers().waxed(registry, old)) + .is_some(); + if waxes { + self.level_event_at(3003, hit.pos, 0); + self.audio + .play("minecraft:item.honeycomb.wax_on", Some(at), 1.0, 1.0); + } + } else if let Some((_, sound, _, _, particle)) = + self.block_transform(stack, hit.pos, hit.face) + { + self.transform_particle(&particle, hit.pos); + self.audio.play(&sound, Some(at), 1.0, 1.0); + } + } + /// MultiPlayerGameMode.useItemOn inside startPrediction: the blocks the /// use changed on this client's level at once, each kept as predicted /// under the use's sequence until the server acknowledges it (a use the diff --git a/crates/horizon-client/src/particles.rs b/crates/horizon-client/src/particles.rs index 55a7a5120..a5df97258 100644 --- a/crates/horizon-client/src/particles.rs +++ b/crates/horizon-client/src/particles.rs @@ -1158,6 +1158,8 @@ pub struct ParticleEngine { starters: Vec, /// Local sounds requested by particles (dripstone drips): (event, position, volume, pitch). pub sounds: Vec<(&'static str, [f64; 3], f32, f32)>, + /// How many of each type were made, for `horizon-ctl status`. + made: std::collections::BTreeMap<&'static str, u64>, /// The item particles' sprites (`BreakingItemParticle`'s slime ball, snowball /// and cobweb models, resolved for the ground), one per model layer. pub item_sprites: HashMap>, @@ -1183,6 +1185,7 @@ impl ParticleEngine { starters: Vec::new(), camera: [0.0; 3], sounds: Vec::new(), + made: Default::default(), item_sprites: HashMap::new(), }; let dir = root.join("assets/minecraft/particles"); @@ -1449,9 +1452,14 @@ impl ParticleEngine { pub fn create(&mut self, options: &ParticleOptions, p: [f64; 3], v: [f64; 3]) { let seed = self.seeds.next_long(); if let Some(particle) = self.make(options, p, v, seed) { + *self.made.entry(options.ty.name()).or_default() += 1; self.pending.push(particle); } } + /// How many particles of each type were made so far. + pub fn made(&self) -> &std::collections::BTreeMap<&'static str, u64> { + &self.made + } /// Parity probes: seed the provider (`ParticleEngine.random`), level and /// `SpellParticle.RANDOM` generators. pub fn seed_randoms(&mut self, provider: i64, level: i64, spell: i64) { diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index c4d8084ba..fd6e09ce1 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -109,6 +109,9 @@ address. The script then checks, from both sides, what a guest's game used to ge the host has them; a falling block lands on both sides. - thrown: a splash potion and an eye of ender the host and the guest each throw fly on both sides (#1363, #1391); +- sparks: level events' particles on both sides: the host's and the guest's bone meal on wheat + (growth sparks), honeycomb on copper and an axe taking the wax off, and a spawner's + burst when it spawns (#1395, #1401, #1402); - fireball: a fire charge a dispenser shoots onto stone lights a fire both sides see, and the fire goes out on its own within a minute (it ticks: FireBlock.tick on a floor that doesn't burn) @@ -210,7 +213,7 @@ ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' # The host plays single player on its server thread (--server-thread). SERVER_THREAD = False -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'looks', 'leads', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'thrown', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'lightning', 'riding', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption', 'breath', 'lava', 'levels', 'drops', 'underwater'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'looks', 'leads', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'thrown', 'sparks', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'lightning', 'riding', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption', 'breath', 'lava', 'levels', 'drops', 'underwater'] class Game: @@ -2643,6 +2646,71 @@ def check_thrown(host, guest, out): return result +def made(game, kind): + """How many `kind` particles `game` has made so far.""" + return game('status').get('particles', {}).get(kind, 0) + + +def sparked(player, other, kinds, act, ticks=20): + """The `kinds` particles each side makes while `act` runs and for up to `ticks` after.""" + before = {(side, k): made(g, k) for side, g in (('own', player), ('other', other)) for k in kinds} + act() + seen = {} + for _ in range(ticks // 2): + player.wait(2) + seen = {f'{side} {k}': made(g, k) - before[(side, k)] + for side, g in (('own', player), ('other', other)) for k in kinds} + if all(seen.values()): + break + return seen + + +def check_sparks(host, guest, out): + spots = apart(host, guest) + result = {} + for label, player, other in (('host', host, guest), ('guest', guest, host)): + x, y, z = spots[label] + at = (x, y, z + 2) + player('command', command='gamemode survival') + try: + player('command', command=f'setblock {at[0]} {at[1] - 1} {at[2]} minecraft:farmland') + player('command', command=f'setblock {at[0]} {at[1]} {at[2]} minecraft:wheat') + player('command', command='item replace entity @s weapon.mainhand with minecraft:bone_meal 8') + player.wait(10) + + def click(height): + player.raw('look-at', f'{at[0] + 0.5:.2f}', f'{at[1] + height:.2f}', f'{at[2] + 0.5:.2f}') + player.raw('mouse', 'right', 'click') + result[f'{label} bone meal'] = sparked(player, other, ['happy_villager'], lambda: click(0.1)) + player('command', command=f'setblock {at[0]} {at[1]} {at[2]} minecraft:copper_block') + player('command', command='item replace entity @s weapon.mainhand with minecraft:honeycomb') + player.wait(10) + result[f'{label} wax'] = sparked(player, other, ['wax_on'], lambda: click(0.5)) + player('command', command='item replace entity @s weapon.mainhand with minecraft:iron_axe') + player.wait(10) + result[f'{label} wax off'] = sparked(player, other, ['wax_off'], lambda: click(0.5)) + block = player.raw('block', *at).get('block', '') + result[f'{label} copper'] = block + finally: + player('command', command=f'fill {at[0]} {at[1] - 1} {at[2]} {at[0]} {at[1]} {at[2]} minecraft:air') + player('command', command=f'setblock {at[0]} {at[1] - 1} {at[2]} minecraft:stone') + player('command', command='clear @s') + # A spawner beside the host, at night: its burst when the zombie comes. + x, y, z = spots['host'] + host('command', command='time set midnight') + try: + host('command', command=f'setblock {x} {y} {z + 3} minecraft:spawner{{Delay:20s,SpawnCount:1s,' + 'SpawnData:{entity:{id:"minecraft:zombie",NoAI:1b}}}') + result['spawner'] = sparked(host, guest, ['flame', 'smoke'], lambda: None, ticks=200) + finally: + host('command', command=f'setblock {x} {y} {z + 3} minecraft:air') + host('command', command=f'kill @e[type=minecraft:zombie,x={x},y={y},z={z},distance=..12]') + host('command', command='time set day') + wrong = {k: v for k, v in result.items() if isinstance(v, dict) and not all(n > 0 for n in v.values())} + assert not wrong, f'no particles: {wrong}\n{result}' + return result + + def check_blast(host, guest, out): guest('command', command='gamemode survival') guest('command', command='clear @s') @@ -3977,7 +4045,8 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'underwater': check_underwater, 'looks': check_looks, 'leads': check_leads, - 'thrown': check_thrown} + 'thrown': check_thrown, + 'sparks': check_sparks} def run(ctl, out, resources, checks): From b7f4cba355f6cc18d6611b8f8eca8a987987f2fa Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 08:28:41 +0200 Subject: [PATCH 11/39] A server's scoreboard reaches its players: sidebar, teams, scores The server never sent its scoreboard, so a sidebar shown on single player's server thread or to a LAN guest stayed empty. Each tick a placed player is now sent what changed since it was last sent the board, as ServerScoreboard's callbacks do: every team, and the objectives in a display slot with their slots and scores (all of it on joining). A server's client also finds its team's sidebar by its profile name rather than single player's "Player". Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/control.rs | 2 + crates/horizon-client/src/game/lan.rs | 1 + crates/horizon-client/src/game/remote.rs | 4 + crates/horizon-client/src/game/scoreboard.rs | 51 ++- crates/horizon-client/src/lan/guest.rs | 3 + crates/horizon-client/src/lan/mod.rs | 1 + crates/horizon-client/src/lan/scoreboard.rs | 308 +++++++++++++++++++ tools/lan_e2e.py | 48 ++- 8 files changed, 410 insertions(+), 8 deletions(-) create mode 100644 crates/horizon-client/src/lan/scoreboard.rs diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index ca15424d1..261f207d4 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -979,6 +979,8 @@ impl Game<'_> { status["projectiles"] = json!(self.projectiles_status()); // The particles made so far, by type. status["particles"] = json!(self.particles.made()); + // The scoreboard sidebar the HUD shows. + status["sidebar"] = self.sidebar_status(); status } } diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 72b3b3fc5..9bfad8581 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -997,6 +997,7 @@ impl Game<'_> { self.lan_update_streaming(&host); self.lan_update_models(&mut host); self.lan_update_waypoints(&mut host, state.as_ref()); + self.lan_sync_scoreboard(&mut host); drop(holding); lan::flush_all(host.hub.guests.iter().map(|g| &g.sender)); self.lan = Some(host); diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index b8777e8d2..46d8c6a65 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -164,6 +164,8 @@ pub(super) struct Remote { level_players: HashMap, /// The local player's profile UUID (always in the level). profile: u128, + /// The local player's profile name: its score holder and team member. + pub(super) profile_name: String, destroying: Option<([i32; 3], u8, f32)>, destroy_delay: u32, input: horizon_client::interaction_input::InteractionInput, @@ -411,6 +413,7 @@ impl Game<'_> { online_mode: false, level_players: HashMap::new(), profile: 0, + profile_name: String::new(), destroying: None, destroy_delay: 0, input: Default::default(), @@ -795,6 +798,7 @@ impl Game<'_> { if let Some(r) = self.remote.as_mut() { r.joined = true; r.profile = uuid; + r.profile_name = name.clone(); r.status = format!("Joined as {name}; loading terrain..."); } } diff --git a/crates/horizon-client/src/game/scoreboard.rs b/crates/horizon-client/src/game/scoreboard.rs index 4d60c2df7..4905fb80f 100644 --- a/crates/horizon-client/src/game/scoreboard.rs +++ b/crates/horizon-client/src/game/scoreboard.rs @@ -324,6 +324,22 @@ impl Game<'_> { } } } + /// `ServerScoreboard`'s packets: each placed player's client is sent + /// what changed on the board since it was last sent it (all of it on + /// joining), lan/scoreboard.rs. + pub(super) fn lan_sync_scoreboard(&mut self, host: &mut super::lan::LanHost) { + if host.hub.guests.iter().all(|g| !g.placed) { + return; + } + self.load_scoreboard(); + let view = horizon_client::lan::scoreboard::View::of(&self.score.board); + for g in host.hub.guests.iter_mut().filter(|g| g.placed) { + if g.scoreboard != view { + g.send_all(&g.scoreboard.changes(&view)); + g.scoreboard = view.clone(); + } + } + } /// `ServerPlayer.doTick` score updates and the statistic criteria. pub(super) fn tick_scoreboard(&mut self) { self.load_scoreboard(); @@ -563,19 +579,42 @@ impl Game<'_> { // ---- the sidebar ----------------------------------------------------------- - /// `Hud.extractScoreboardSidebar` / `displayScoreboardSidebar`. - pub(super) fn draw_scoreboard(&self, batch: &mut HudBatch) { + /// The objective in the player's team's sidebar slot, else the sidebar's. + fn sidebar_objective(&self) -> Option<&sb::Objective> { let board = &self.score.board; if board.display.is_empty() { - return; + return None; } + // A server's client is in its team by its profile name. + let name = self + .remote + .as_ref() + .map_or(PLAYER_NAME, |r| r.profile_name.as_str()); let team_slot = board - .players_team(PLAYER_NAME) + .players_team(name) .and_then(|t| t.color.as_ref()) .map(|c| format!("sidebar.team.{c}")) .and_then(|slot| board.display_objective(&slot)); - let Some(objective) = team_slot.or_else(|| board.display_objective(sb::SLOT_SIDEBAR)) - else { + team_slot.or_else(|| board.display_objective(sb::SLOT_SIDEBAR)) + } + /// The sidebar the HUD shows: its objective and its lines, for `status`. + pub(super) fn sidebar_status(&self) -> serde_json::Value { + let board = &self.score.board; + let Some(objective) = self.sidebar_objective() else { + return serde_json::Value::Null; + }; + let scores: serde_json::Map = board + .scores + .iter() + .filter_map(|(holder, s)| Some((holder.clone(), s.get(&objective.name)?.value.into()))) + .collect(); + serde_json::json!({ "objective": objective.name, "title": objective.display_text().plain(), "scores": scores }) + } + + /// `Hud.extractScoreboardSidebar` / `displayScoreboardSidebar`. + pub(super) fn draw_scoreboard(&self, batch: &mut HudBatch) { + let board = &self.score.board; + let Some(objective) = self.sidebar_objective() else { return; }; let draws = hud_overlays::sidebar( diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index 4236280cd..cb3a4927c 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -215,6 +215,8 @@ pub struct Guest { pub advancements_loaded: bool, pub advancements_visible: Vec, pub advancements_changed: std::collections::BTreeSet, + /// The server's scoreboard as this player's client has it (lan/scoreboard.rs). + pub scoreboard: super::scoreboard::View, /// Recipes unlocked since the client was last told /// (`ServerRecipeBook.addRecipes` sends them). pub recipes_unlocked: Vec, @@ -326,6 +328,7 @@ impl Guest { advancements_loaded: false, advancements_visible: Vec::new(), advancements_changed: Default::default(), + scoreboard: Default::default(), recipes_unlocked: Vec::new(), } } diff --git a/crates/horizon-client/src/lan/mod.rs b/crates/horizon-client/src/lan/mod.rs index c2f35bc8e..d342c7fe5 100644 --- a/crates/horizon-client/src/lan/mod.rs +++ b/crates/horizon-client/src/lan/mod.rs @@ -20,6 +20,7 @@ pub mod hub; pub mod menu; pub mod playerdata; mod recipes; +pub mod scoreboard; pub mod tracking; pub mod waypoints; diff --git a/crates/horizon-client/src/lan/scoreboard.rs b/crates/horizon-client/src/lan/scoreboard.rs new file mode 100644 index 000000000..cba0f914c --- /dev/null +++ b/crates/horizon-client/src/lan/scoreboard.rs @@ -0,0 +1,308 @@ +//! `ServerScoreboard`: what a player's client knows of the server's +//! scoreboard. Every team goes to every player; an objective goes once it +//! sits in a display slot (`startTrackingObjective`: its objective, its +//! slots, then its scores) and leaves when it sits in none +//! (`stopTrackingObjective`). A joining player gets it all +//! (`PlayerList.updateEntireScoreboard`). +//! +//! A server's commands change the board in place; each tick a player is +//! sent the packets that turn what it was last sent into what it should +//! have now, which are the ones `ServerScoreboard`'s callbacks send. +use crate::scoreboard::{NumberFormat, RenderType, Scoreboard, component_tag, display_slots}; +use crate::text::COLORS; +use horizon_protocol::packets::play::{ + self, Clientbound as C, ResetScore, SetDisplayObjective, SetObjective, SetPlayerTeam, SetScore, + TeamParameters, +}; +use horizon_world::nbt::Tag; +use std::collections::{BTreeMap, BTreeSet}; + +type Format = Option; + +/// The scoreboard as one player's client has it. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct View { + /// The tracked objectives: display name, render type (1 hearts), format. + objectives: BTreeMap, + /// `DisplaySlot` id to objective. + display: BTreeMap, + /// The tracked objectives' scores, by holder and objective. + scores: BTreeMap<(String, String), (i32, Option, Format)>, + /// Every team: its parameters and players, in the team's order. + teams: BTreeMap)>, +} + +fn format(f: Option<&NumberFormat>) -> Format { + f.map(|f| match f { + NumberFormat::Blank => play::NumberFormat::Blank, + NumberFormat::Styled(s) => play::NumberFormat::Styled(crate::scoreboard::json_to_tag(s)), + NumberFormat::Fixed(c) => play::NumberFormat::Fixed(component_tag(c)), + }) +} + +impl View { + /// What the board's state gives a client. + pub fn of(board: &Scoreboard) -> Self { + let slots = display_slots(); + let display: BTreeMap = board + .display + .iter() + .filter_map(|(slot, o)| Some((slots.iter().position(|s| s == slot)? as i32, o.clone()))) + .collect(); + let tracked: BTreeSet<&String> = display.values().collect(); + let objectives = board + .objectives + .iter() + .filter(|o| tracked.contains(&o.name)) + .map(|o| { + let render = i32::from(o.render_type == RenderType::Hearts); + ( + o.name.clone(), + ( + component_tag(&o.display_name), + render, + format(o.number_format.as_ref()), + ), + ) + }) + .collect(); + let scores = board + .scores + .iter() + .flat_map(|(holder, scores)| { + scores + .iter() + .filter(|(o, _)| tracked.contains(o)) + .map(move |(o, s)| { + ( + (holder.clone(), o.clone()), + ( + s.value, + s.display.as_ref().map(component_tag), + format(s.number_format.as_ref()), + ), + ) + }) + }) + .collect(); + let teams = board + .teams + .iter() + .map(|t| { + let params = TeamParameters { + display_name: component_tag(&t.display_name), + prefix: component_tag(&t.prefix), + suffix: component_tag(&t.suffix), + name_tag_visibility: t.nametag_visibility as i32, + collision_rule: t.collision_rule as i32, + color: t + .color + .as_deref() + .and_then(|c| COLORS.iter().position(|(_, n, _)| *n == c)) + .map(|i| i as i32), + options: i8::from(t.allow_friendly_fire) + | (i8::from(t.see_friendly_invisibles) << 1), + }; + ( + t.name.clone(), + (params, t.players.iter().cloned().collect()), + ) + }) + .collect(); + Self { + objectives, + display, + scores, + teams, + } + } + + /// The packets that take a client from `self` to `next`. + pub fn changes(&self, next: &View) -> Vec { + let mut out = Vec::new(); + let team = |name: &str, method, parameters, players| { + C::SetPlayerTeam(SetPlayerTeam { + name: name.to_owned(), + method, + parameters, + players, + }) + }; + for name in self.teams.keys().filter(|n| !next.teams.contains_key(*n)) { + out.push(team(name, 1, None, Vec::new())); + } + for (name, (params, players)) in &next.teams { + let Some((was, had)) = self.teams.get(name) else { + out.push(team(name, 0, Some(params.clone()), players.clone())); + continue; + }; + if was != params { + out.push(team(name, 2, Some(params.clone()), Vec::new())); + } + let joined: Vec<_> = players + .iter() + .filter(|p| !had.contains(p)) + .cloned() + .collect(); + if !joined.is_empty() { + out.push(team(name, 3, None, joined)); + } + let left: Vec<_> = had + .iter() + .filter(|p| !players.contains(p)) + .cloned() + .collect(); + if !left.is_empty() { + out.push(team(name, 4, None, left)); + } + } + for (name, display) in &next.objectives { + let method = match self.objectives.get(name) { + None => 0, + Some(was) if was != display => 2, + Some(_) => continue, + }; + out.push(C::SetObjective(SetObjective { + name: name.clone(), + method, + display: Some(display.clone()), + })); + } + let slots: BTreeSet = self + .display + .keys() + .chain(next.display.keys()) + .copied() + .collect(); + for slot in slots { + let objective = next.display.get(&slot); + if self.display.get(&slot) != objective { + out.push(C::SetDisplayObjective(SetDisplayObjective { + slot, + objective: objective.cloned().unwrap_or_default(), + })); + } + } + for ((holder, objective), (value, display, number_format)) in &next.scores { + let key = (holder.clone(), objective.clone()); + if self.scores.get(&key) != Some(&(*value, display.clone(), number_format.clone())) { + out.push(C::SetScore(SetScore { + owner: holder.clone(), + objective: objective.clone(), + score: *value, + display: display.clone(), + number_format: number_format.clone(), + })); + } + } + for (holder, objective) in self.scores.keys() { + let key = (holder.clone(), objective.clone()); + if !next.scores.contains_key(&key) && next.objectives.contains_key(objective) { + out.push(C::ResetScore(ResetScore { + owner: holder.clone(), + objective: Some(objective.clone()), + })); + } + } + for name in self + .objectives + .keys() + .filter(|n| !next.objectives.contains_key(*n)) + { + out.push(C::SetObjective(SetObjective { + name: name.clone(), + method: 1, + display: None, + })); + } + out + } +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + fn methods(packets: &[C]) -> Vec { + packets + .iter() + .map(|p| match p { + C::SetObjective(o) => format!("objective {} {}", o.name, o.method), + C::SetDisplayObjective(d) => format!("display {} {}", d.slot, d.objective), + C::SetScore(s) => format!("score {} {} {}", s.owner, s.objective, s.score), + C::ResetScore(r) => format!("reset {} {:?}", r.owner, r.objective), + C::SetPlayerTeam(t) => format!("team {} {} {:?}", t.name, t.method, t.players), + _ => "other".into(), + }) + .collect() + } + + #[test] + fn a_shown_objective_goes_with_its_scores_and_leaves_when_hidden() { + let mut board = Scoreboard::default(); + board.add_objective( + "audit", + "dummy", + json!("Audit"), + RenderType::Integer, + false, + None, + ); + board.score_mut("Alpha", "audit").value = 7; + let empty = View::default(); + // Not displayed: not tracked, nothing sent. + let hidden = View::of(&board); + assert!(empty.changes(&hidden).is_empty()); + board.set_display_objective("sidebar", Some("audit")); + let shown = View::of(&board); + assert_eq!( + methods(&hidden.changes(&shown)), + [ + "objective audit 0", + "display 1 audit", + "score Alpha audit 7" + ] + ); + board.score_mut("Alpha", "audit").value = 12; + board.score_mut("Beta", "audit").value = 3; + board.reset_one("Beta", "audit"); + let changed = View::of(&board); + assert_eq!(methods(&shown.changes(&changed)), ["score Alpha audit 12"]); + board.reset_one("Alpha", "audit"); + let reset = View::of(&board); + assert_eq!( + methods(&changed.changes(&reset)), + ["reset Alpha Some(\"audit\")"] + ); + board.set_display_objective("sidebar", None); + assert_eq!( + methods(&reset.changes(&View::of(&board))), + ["display 1 ", "objective audit 1"] + ); + } + + #[test] + fn teams_go_to_everyone_with_their_players() { + let mut board = Scoreboard::default(); + board.add_team("red"); + board.add_to_team("Alpha", "red"); + let first = View::of(&board); + assert_eq!( + methods(&View::default().changes(&first)), + ["team red 0 [\"Alpha\"]"] + ); + board.add_to_team("Beta", "red"); + board.remove_from_team("Alpha"); + let second = View::of(&board); + assert_eq!( + methods(&first.changes(&second)), + ["team red 3 [\"Beta\"]", "team red 4 [\"Alpha\"]"] + ); + board.remove_team("red"); + assert_eq!( + methods(&second.changes(&View::of(&board))), + ["team red 1 []"] + ); + } +} diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index fbb7e4e9c..72beb2acd 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -213,7 +213,7 @@ ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' # The host plays single player on its server thread (--server-thread). SERVER_THREAD = False -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'looks', 'leads', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'thrown', 'sparks', 'hud', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'lightning', 'riding', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption', 'breath', 'lava', 'levels', 'drops', 'underwater'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'looks', 'leads', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'thrown', 'sparks', 'hud', 'sidebar', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'lightning', 'riding', 'mounts', 'worldspawn', 'beds', 'shield', 'deathmining', 'contacts', 'absorption', 'breath', 'lava', 'levels', 'drops', 'underwater'] class Game: @@ -2751,6 +2751,49 @@ def check_hud(host, guest, out): return result +def sidebars(host, guest, want, ticks=40): + """Each side's sidebar (objective and scores) once both show `want` (host, guest), or after `ticks`.""" + seen = None + for _ in range(ticks // 2): + host.wait(2) + seen = tuple(g('status').get('sidebar') for g in (host, guest)) + got = tuple(s and (s['objective'], s['scores']) for s in seen) + if got == want: + break + return seen + + +def check_sidebar(host, guest, out): + result = {} + run = lambda *cmds: [host('command', command=c) for c in cmds] + try: + run('scoreboard objectives add audit dummy {"text":"Audit board","color":"aqua"}', + 'scoreboard players set Alpha audit 7', 'scoreboard players set Beta audit 3', + 'scoreboard objectives setdisplay sidebar audit') + both = ('audit', {'Alpha': 7, 'Beta': 3}) + result['shown'] = sidebars(host, guest, (both, both)) + host.shot(out, 'sidebar') + guest.shot(out, 'sidebar') + run('scoreboard players set Alpha audit 12', 'scoreboard players reset Beta audit') + both = ('audit', {'Alpha': 12}) + result['changed'] = sidebars(host, guest, (both, both)) + # The guest's team's sidebar replaces the sidebar for the guest only. + run('team add red', 'team modify red color red', f'team join red {GUEST_NAME}', + 'scoreboard objectives add reds dummy', f'scoreboard players set {GUEST_NAME} reds 5', + 'scoreboard objectives setdisplay sidebar.team.red reds') + result['team'] = sidebars(host, guest, (both, ('reds', {GUEST_NAME: 5}))) + run('scoreboard objectives remove reds', 'scoreboard objectives setdisplay sidebar') + result['cleared'] = sidebars(host, guest, (None, None)) + finally: + run('scoreboard objectives remove audit', 'scoreboard objectives remove reds', 'team remove red') + want = {'shown': (('audit', {'Alpha': 7, 'Beta': 3}),) * 2, 'changed': (('audit', {'Alpha': 12}),) * 2, + 'team': (('audit', {'Alpha': 12}), ('reds', {GUEST_NAME: 5})), 'cleared': (None, None)} + got = {k: tuple(s and (s['objective'], s['scores']) for s in v) for k, v in result.items()} + wrong = {k: v for k, v in got.items() if v != want[k]} + assert not wrong, f'sidebars: {wrong}\n{result}' + return result + + def check_blast(host, guest, out): guest('command', command='gamemode survival') guest('command', command='clear @s') @@ -4087,7 +4130,8 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'leads': check_leads, 'thrown': check_thrown, 'sparks': check_sparks, - 'hud': check_hud} + 'hud': check_hud, + 'sidebar': check_sidebar} def run(ctl, out, resources, checks): From 6049012b52f3e3675fd45f8a7f767015aea11534 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 10:54:42 +0200 Subject: [PATCH 12/39] Single player runs in the frame by default again; the server thread is opt-in Players on the Alpha channel report single player as broken since v0.2.1 (#1265 made the integrated server thread the default again). On v0.2.11's server thread, placed signs open no editor (#1380), held books don't open (#1366), a totem doesn't save its holder (#1292) and death keeps the inventory with keep_inventory off (#1284); the in-frame world and Stable v0.2.0 do all four right. About 45 more [server-thread] issues are open. `--server-thread` or HORIZON_SERVER_THREAD=1 turns it on, as before #1114; `--no-server-thread` and HORIZON_SERVER_THREAD=0 still work. LAN guests and dedicated servers are untouched. Temporary until the server thread reaches parity (FEATURES 4zg). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 12 +++++++----- crates/horizon-client/src/game/integrated.rs | 9 ++++++--- docs/server-levels.md | 7 ++++--- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index b307d71dc..b67cf2263 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -282,9 +282,11 @@ struct Options { benchmark: Option, /// `--dev-mod PATH` (repeatable): mods always on and reloaded when rebuilt. dev_mods: Vec, - /// Single player runs on an integrated server thread (game/integrated.rs), - /// as vanilla's does: the default. `--no-server-thread` or - /// `HORIZON_SERVER_THREAD=0` runs the world in the frame instead. + /// `--server-thread` or `HORIZON_SERVER_THREAD=1`: single player runs on an + /// integrated server thread (game/integrated.rs), as vanilla's does. Off by + /// default again until a server's players reach parity with the in-frame + /// world (signs, books, totems, keep_inventory...); `--no-server-thread` + /// keeps it off. server_thread: bool, /// Set on the integrated server's own options: it draws nothing, so it skips meshing. integrated: bool, @@ -342,7 +344,7 @@ impl Options { dev_mods: Vec::new(), // The command harness (HORIZON_COMMAND_SCRIPT) drives the world // itself, without the event loop: it stays in the frame. - server_thread: std::env::var("HORIZON_SERVER_THREAD").map_or(true, |v| v != "0") + server_thread: std::env::var("HORIZON_SERVER_THREAD").is_ok_and(|v| v == "1") && std::env::var_os("HORIZON_COMMAND_SCRIPT").is_none(), integrated: false, }; @@ -352,7 +354,7 @@ impl Options { match arg.as_str() { "--help" | "-h" => { println!( - "Horizon — Minecraft Java Edition {} in Rust\n\n --resources PATH Prepared resources (horizon-prepare)\n --render-distance N Chunks, 2..64 (default 12)\n --world NAME World folder under saves/ (default \"New World\")\n --seed N World seed for new worlds (default 0)\n --width N, --height N Window size (default 1280 x 720)\n --creative Creative mode (default survival)\n --test-terrain Simple test terrain instead of vanilla generation\n --frames N Exit after N frames (testing)\n --world-frames N Exit after N frames drawn in the world (testing)\n --smoke-report PATH Write a JSON report on exit\n --benchmark SCENARIO Run a benchmark (standard, village, forest, ocean, mountain), write its report to benchmarks/ and quit\n --control Accept horizon-ctl commands on a local socket\n --control-socket PATH Control socket path (default $XDG_RUNTIME_DIR/horizon-control.sock)\n --server HOST[:PORT] Join a multiplayer server (offline mode) at start\n --name NAME Player name for multiplayer (default: generated once, kept in horizon-player.txt)\n --session-file PATH Signed-in account from the launcher (read once, then deleted)\n --dev-mod PATH A Horizon mod (.hmod or folder) to develop: always on, reloaded when rebuilt\n --no-server-thread Run single player's world in the frame, not on its server thread\n\nControls: mouse look, WASD move, Space jump, Left Shift sneak, Left Ctrl sprint,\nleft click break, right click place/use, 1-9/scroll hotbar, E inventory,\nQ drop, F3 debug, Esc pause.", + "Horizon — Minecraft Java Edition {} in Rust\n\n --resources PATH Prepared resources (horizon-prepare)\n --render-distance N Chunks, 2..64 (default 12)\n --world NAME World folder under saves/ (default \"New World\")\n --seed N World seed for new worlds (default 0)\n --width N, --height N Window size (default 1280 x 720)\n --creative Creative mode (default survival)\n --test-terrain Simple test terrain instead of vanilla generation\n --frames N Exit after N frames (testing)\n --world-frames N Exit after N frames drawn in the world (testing)\n --smoke-report PATH Write a JSON report on exit\n --benchmark SCENARIO Run a benchmark (standard, village, forest, ocean, mountain), write its report to benchmarks/ and quit\n --control Accept horizon-ctl commands on a local socket\n --control-socket PATH Control socket path (default $XDG_RUNTIME_DIR/horizon-control.sock)\n --server HOST[:PORT] Join a multiplayer server (offline mode) at start\n --name NAME Player name for multiplayer (default: generated once, kept in horizon-player.txt)\n --session-file PATH Signed-in account from the launcher (read once, then deleted)\n --dev-mod PATH A Horizon mod (.hmod or folder) to develop: always on, reloaded when rebuilt\n --server-thread Run single player's world on its server thread (not at parity yet)\n\nControls: mouse look, WASD move, Space jump, Left Shift sneak, Left Ctrl sprint,\nleft click break, right click place/use, 1-9/scroll hotbar, E inventory,\nQ drop, F3 debug, Esc pause.", horizon_core::VANILLA_VERSION ); return Ok(None); diff --git a/crates/horizon-client/src/game/integrated.rs b/crates/horizon-client/src/game/integrated.rs index ef5775174..49266a855 100644 --- a/crates/horizon-client/src/game/integrated.rs +++ b/crates/horizon-client/src/game/integrated.rs @@ -6,9 +6,12 @@ //! the world's options) with [`Integrated::call`], as vanilla's client calls //! into its `IntegratedServer`. //! -//! On by default. `--no-server-thread` or `HORIZON_SERVER_THREAD=0` runs -//! the world in the frame instead (the escape hatch while the network -//! player's side catches up with the in-frame one). +//! Opt-in again (`--server-thread` or `HORIZON_SERVER_THREAD=1`) until a +//! server's players reach parity with the in-frame world: on the server +//! thread, signs open no editor, books don't open, a totem doesn't save its +//! holder and death keeps the inventory (#1380, #1366, #1292, #1284 and the +//! other `[server-thread]` issues). LAN guests and dedicated servers use the +//! same code either way. use super::{ Game, Options, PackResources, Resources, UiResources, lan::{LanSettings, Owner}, diff --git a/docs/server-levels.md b/docs/server-levels.md index a44f290c4..cc21a52ab 100644 --- a/docs/server-levels.md +++ b/docs/server-levels.md @@ -26,9 +26,10 @@ the other dimensions' levels as `Parked` values and swaps one into place (`enter The packets of a guest are read in its own level's pass (`Hub::tick_level`), so its actions and commands run with its level in place. -The window's own game (`--no-server-thread`, `HORIZON_SERVER_THREAD=0`, and the command harness) keeps -one level, which follows its player (`enter_dimension`). LAN guests of a world run that way stay in the -host's dimension. That mode is the escape hatch now, so it gets no multi-level work. +The window's own game (single player's default, `--no-server-thread`, `HORIZON_SERVER_THREAD=0`, and the +command harness) keeps one level, which follows its player (`enter_dimension`). LAN guests of a world run +that way stay in the host's dimension. It is the default again only until the server thread reaches parity +(`--server-thread` or `HORIZON_SERVER_THREAD=1` opts in), so it gets no multi-level work. ## One code path for both From 0e65e8587825144712e797bd9f5ac399ec94cd24 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 11:53:00 +0200 Subject: [PATCH 13/39] Releases: one channel, every release is the latest once its checks pass Ludovic's decision (2026-10-23): no more Alpha and Stable. release.yml publishes every game release as the release and GitHub's latest instead of a pre-release that promote.yml made stable once a day. The checks that gated a promotion run in the release itself: `decide` holds the train while an issue labelled release-blocker is open (forced runs too), and the publish job runs `release_train.py gate` right before publishing: the Mac mini's smoke-test.json must be there and show a world was drawn, and no blocker may be open. Otherwise nothing is published. Notes cover everything since the last release that is not a pre-release, so the first single-channel release lists all of it since v0.2.0. promote.yml, tools/release_channels.py and its tests are deleted. The cadence is unchanged (5 minutes of settling, 20 at most, release-now at once). Docs and site: one channel in release-flow, versioning, security, the FAQ, install, the home page and the Releases page (Game / Launcher filter); no player-facing Alpha wording left. Co-Authored-By: Claude Opus 5.5 --- .github/CODEOWNERS | 1 - .github/workflows/promote.yml | 55 --------- .github/workflows/release.yml | 35 ++++-- docs/release-flow.md | 107 ++++++---------- docs/security.md | 21 ++-- docs/versioning.md | 16 +-- tests/test_release_channels.py | 147 ---------------------- tests/test_release_train.py | 28 ++++- tools/release_channels.py | 220 --------------------------------- tools/release_train.py | 99 ++++++++++----- website/build.mjs | 7 +- website/docs/faq.md | 2 +- website/docs/index.md | 2 +- website/docs/install.md | 13 +- website/docs/multiplayer.md | 2 +- website/docs/settings.md | 4 +- website/src/index.html | 2 +- website/src/main.js | 8 +- website/src/releases.html | 7 +- website/src/releases.js | 6 +- website/src/style.css | 5 +- 21 files changed, 208 insertions(+), 579 deletions(-) delete mode 100644 .github/workflows/promote.yml delete mode 100644 tests/test_release_channels.py delete mode 100644 tools/release_channels.py diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 8d8687753..5567eb184 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -6,7 +6,6 @@ /.github/ @ludovic111 /tools/ci/ @ludovic111 /tools/release_train.py @ludovic111 -/tools/release_channels.py @ludovic111 /crates/horizon-release-sign/ @ludovic111 /crates/horizon-server/src/update.rs @ludovic111 /crates/horizon-server/Cargo.toml @ludovic111 diff --git a/.github/workflows/promote.yml b/.github/workflows/promote.yml deleted file mode 100644 index 3966f571b..000000000 --- a/.github/workflows/promote.yml +++ /dev/null @@ -1,55 +0,0 @@ -name: promote -# Beta game releases become stable once a day (docs/release-flow.md, "Channels"): at 20:00 UTC (21:00 in Paris in -# winter time, 22:00 in summer) tools/release_channels.py promotes the newest beta that had been out 6 hours by then, -# passed its release smoke test and isn't covered by an issue labelled release-blocker. Launchers on the Alpha -# channel install every beta when it is published; Stable gets the day's promotion. The later runs of the evening are -# retries for a run GitHub starts late or skips: they promote the same beta, so stable still moves once. -# By hand, from GitHub's app too (Actions > promote > Run workflow): -# gh workflow run promote -f tag=v0.2.3 # that beta now, without waiting for 20:00 -# gh workflow run promote -f tag=v0.2.3 -f force=true # even blocked or without its smoke test -# gh workflow run promote -f dry_run=true # say what would be promoted -# Holding betas back: label an issue release-blocker. While it is open it holds every beta; closed as completed it -# still holds the betas published before it closed. The issue's text is never read (its author could edit it). -# When the oldest waiting beta is 30 hours old (it missed a promotion it was due for), an issue "Promotion to stable -# has stalled" says so; it closes itself after the next promotion. -on: - schedule: - - cron: '7,37 20-21 * * *' - workflow_dispatch: - inputs: - tag: - description: The beta to promote now (empty = the newest beta that is due) - type: string - default: '' - force: - description: Promote that tag even if a release-blocker covers it or its smoke test didn't run - type: boolean - default: false - dry_run: - description: Say what would be promoted, change nothing - type: boolean - default: false -permissions: - contents: write - issues: write -concurrency: - group: promote - cancel-in-progress: false -jobs: - promote: - runs-on: ubuntu-latest - timeout-minutes: 10 - steps: - - uses: actions/checkout@v4 - with: - # The notes of a promoted release cover every pull request since the last stable one. - fetch-depth: 0 - # gh uses GH_TOKEN below; nothing needs git credentials on disk. - persist-credentials: false - - name: Promote - env: - GH_TOKEN: ${{ github.token }} - TAG: ${{ inputs.tag }} - FORCE: ${{ inputs.force && 'yes' || '' }} - DRY_RUN: ${{ (inputs.dry_run || github.ref != 'refs/heads/main') && 'yes' || '' }} - run: python3 tools/release_channels.py promote ${TAG:+--tag "$TAG"} ${FORCE:+--force} ${DRY_RUN:+--dry-run} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b19ba9ce5..95a2fec64 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -3,12 +3,15 @@ name: release # release waiting for merges to settle goes out without another merge) and by hand, tools/release_train.py decides: # a release when pull requests merged since the last one changed what players run and the newest merged 5 minutes # ago (the oldest 20 minutes ago, if merges never pause), or at once for a pull request labelled release-now. -# RELEASES_PAUSED=1 (repository variable) stops them. +# RELEASES_PAUSED=1 (repository variable) stops them; so does an open issue labelled release-blocker, forced runs too. # Builds Linux (an older Ubuntu, so the binary runs on any recent distribution and on NixOS through nix-ld) and # Windows on GitHub, macOS on the Mac mini's runner (MAC_RUNNER=macos-latest sends it to GitHub), then # publishes vX.Y.Z on the release line in .github/release-line.txt (docs/versioning.md; without it, the dated -# vYYYY.MM.DD[.N] from before 0.2) with notes from the merged pull requests, as a beta (a pre-release) that promote.yml makes -# stable in its daily promotion. macOS: with the repository variable MACOS_SIGN_IDENTITY set, the programs are signed with +# vYYYY.MM.DD[.N] from before 0.2) with notes from the merged pull requests, as the release and GitHub's latest: one +# channel, which launchers, `horizon-server update` and install.sh follow at once. So it is published only after the +# checks that used to gate a promotion pass in this run: the Mac mini's smoke test drew a world, and no release-blocker +# is open (tools/release_train.py gate); otherwise nothing is published. macOS: with the repository variable +# MACOS_SIGN_IDENTITY set, the programs are signed with # Developer ID and the hardened runtime and the zip is notarized, from the runner's signing keychain # (packaging/macos/README.md); unset (forks), ad hoc. # gh workflow run release --ref # dry run: builds and notes, publishes nothing @@ -32,6 +35,8 @@ on: default: false permissions: contents: read + # The open release-blocker issues (decide). + issues: read concurrency: group: game-release cancel-in-progress: false @@ -203,7 +208,7 @@ jobs: or not report.get("meshed_sections") or not report.get("player"): sys.exit("the smoke run did not draw a world") EOF - # Published with the release: a beta becomes stable only with it (tools/release_channels.py). + # Published with the release; the publish job checks it again before it publishes anything. cp "$home/smoke.json" "$RUNNER_TEMP/smoke-test.json" - uses: actions/upload-artifact@v4 if: runner.environment == 'self-hosted' @@ -319,6 +324,7 @@ jobs: timeout-minutes: 15 permissions: contents: write + issues: read steps: - uses: actions/checkout@v4 with: @@ -341,6 +347,17 @@ jobs: run: | key=(); [[ -n "$EPHEMERAL" ]] && key=(--key "$(cat dist/ephemeral.pub)") cargo run --locked --quiet -p horizon-release-sign -- verify --tag "$TAG" --publishing "${key[@]}" dist + - name: Ready to publish (the smoke test passed, no release-blocker is open) + # Right before publishing: an issue labelled while the builds ran still holds this release. A dry run only + # reports what a real run would do. + env: + GH_TOKEN: ${{ github.token }} + DRY_RUN: ${{ needs.decide.outputs.dry_run }} + run: | + if ! python3 tools/release_train.py gate --kind game --smoke dist/smoke-test.json --blockers; then + [[ "$DRY_RUN" == true ]] || { echo "::error::Nothing published"; exit 1; } + echo "::warning::A real run would publish nothing" + fi - name: Publish ${{ needs.decide.outputs.tag }} if: needs.decide.outputs.dry_run != 'true' env: @@ -348,17 +365,15 @@ jobs: TAG: ${{ needs.decide.outputs.tag }} run: | signature=(); [[ -f dist/SHA256SUMS.sig ]] && signature=(dist/SHA256SUMS.sig) - smoke=(); [[ -f dist/smoke-test.json ]] && smoke=(dist/smoke-test.json) - # A beta (pre-release), the Alpha channel's: promote.yml makes it stable and latest in its daily promotion - # (docs/release-flow.md). + # The release and the latest at once: there is one channel (docs/release-flow.md). gh release create "$TAG" --target "${{ needs.decide.outputs.head }}" --title "$(cat notes/title.txt)" \ - --notes-file notes/body.md --prerelease --latest=false dist/horizon-linux-x86_64.tar.gz \ + --notes-file notes/body.md --latest dist/horizon-linux-x86_64.tar.gz \ dist/horizon-windows-x64.zip dist/horizon-macos-arm64.zip dist/horizon-server-linux-x86_64.tar.gz \ - dist/SHA256SUMS "${signature[@]}" "${smoke[@]}" + dist/SHA256SUMS "${signature[@]}" dist/smoke-test.json - name: Dry run, the release that would be published if: needs.decide.outputs.dry_run == 'true' run: | - echo "Would publish ${{ needs.decide.outputs.tag }} at ${{ needs.decide.outputs.head }}: $(cat notes/title.txt)" + echo "Would publish ${{ needs.decide.outputs.tag }} at ${{ needs.decide.outputs.head }} as the release and latest: $(cat notes/title.txt)" ls -l dist - uses: actions/upload-artifact@v4 with: diff --git a/docs/release-flow.md b/docs/release-flow.md index 6cc4bcd3c..72a5c53a9 100644 --- a/docs/release-flow.md +++ b/docs/release-flow.md @@ -2,9 +2,8 @@ How a change gets from a pull request to players: the checks a pull request passes, how it merges, and the automatic releases that follow. Releases are automatic and frequent so the latest Horizon is always playable. -Each one goes out as an **alpha** build, which launchers on the **Alpha** channel install at once, and becomes **stable** in -the next daily promotion unless something holds it back ([Channels](#channels)): launchers and servers install -stable builds unless their owner picks Alpha. +There is one channel ([One channel](#one-channel)): each release is published as the release and GitHub's *latest* +once its smoke test passed and no `release-blocker` is open, and launchers and servers install it at once. ## Pull request checks @@ -101,12 +100,11 @@ flowchart LR windows["Windows x64
GitHub windows-latest"] mac["macOS arm64
Mac mini runner
+ offscreen game run"] end - build --> publish["publish vX.Y.Z as an alpha
notes from the merged PRs"] - publish --> alpha(["launchers on Alpha install it"]) - publish --> promote{"promote.yml, daily at 20:00 UTC:
6 h old by then, smoke test, no release-blocker"} - promote --> stable["stable and latest
notes since the last stable"] - stable --> launcher(["every launcher and server installs it"]) - stable --> kira(["Kira posts it;
pings only with ### New"]) + build --> gate{"gate: the smoke test drew a world,
no release-blocker is open"} + gate -- no --> nothing(["nothing is published"]) + gate -- yes --> publish["publish vX.Y.Z as the release and latest
notes from the merged PRs"] + publish --> launcher(["every launcher and server installs it"]) + publish --> kira(["Kira posts it; the first release
after 20:00 UTC pings, once a day"]) ``` ### When it releases @@ -121,9 +119,10 @@ commits) since the last **published** game release: - It releases when at least one pull request counts and the newest of them merged at least 5 minutes ago, so a wave of merges becomes one release; if merges never pause, it releases once the oldest has waited 20 minutes. There is no minimum time between releases: every settled player-facing change ships, limited only by the release already - building. That is the Alpha channel, many releases a day. + building: many releases a day. - A pull request labelled `release-now` (a crash or a blocker) releases at once. - A pull request labelled `skip-release` (work players don't see) doesn't count and isn't listed in the notes. +- An open issue labelled `release-blocker` holds every game release, forced ones too (`held by release-blocker #N`). - One release at a time (concurrency group `game-release`); a run that finds nothing new does nothing. - A scheduled or hand-started run releases main's newest commit whose `checks` passed; after `checks`, the commit they passed on. @@ -137,7 +136,7 @@ How releases are numbered, and how installs from before 0.2 reach it: [versionin the patch after the newest release tagged on that line (`v0.2.0` first). A `feature` label doesn't change the line: only that file does, by pull request. - Without the file (every release before 0.2), `vYYYY.MM.DD`, then `.01`, `.02`… the same UTC day. -- Every reader (`release_train.py`, `release_channels.py`, the launcher, `horizon-server update`, the website) ranks +- Every reader (`release_train.py`, the launcher, `horizon-server update`, the website) ranks every `vX.Y.Z` tag above every dated one. ### Labels @@ -149,7 +148,7 @@ How releases are numbered, and how installs from before 0.2 reach it: [versionin | `skip-release` | Doesn't trigger a release and isn't listed in the notes | | `run-parity` | Runs the parity job once on a fork's pull request | | `parity-regression` | Opened by the nightly audit for a harness that fails on main | -| `release-blocker` | On an issue: holds every beta back from stable while it is open ([Channels](#channels)) | +| `release-blocker` | On an issue: no game release is published while it is open ([One channel](#one-channel)) | Pull requests without `feature` are listed under **Fixed**. Titles are written for players, since they become the notes. @@ -180,63 +179,38 @@ runs the game: GitHub's runners have neither a GPU worth the name nor the game f - `### Known issues`: the bullets of [docs/known-issues.md](known-issues.md); - thanks to outside contributors, how to install, the disclaimer. -Kira posts every release in Discord's #releases. An Alpha build (a game pre-release) is posted silently: no ping and -Discord's @silent flag, so nobody gets a notification. When the same release becomes Stable it is posted again, and -that post pings the releases role when the notes have a `### New` section (at most once every 6 hours). Launcher -releases post without a ping. +Kira posts every release once in Discord's #releases. It pings the releases role at most once a day: the first game +release published after 20:00 UTC, with a short list of what came out since its last ping. Every other post is +silent (no ping and Discord's @silent flag). Launcher releases post without a ping. -### Channels +### One channel -Every game release is published as an **alpha** build, a GitHub pre-release (the workflows, tools and labels -call it a *beta*: `release-blocker` holds every beta), so GitHub's *latest* release stays the last -**stable** one. The launcher's Settings has an **Updates** choice of two channels (`update_channel` in its -`settings.json`, `horizon-launcher update-channel`): +Since 2026-10-23 there is one channel. Every game release is published as the release, never a pre-release, and +marked GitHub's *latest*: the launcher, `horizon-server update` and `install.sh` take it at once. Because nothing +promotes it later, the checks that used to gate a promotion run in the release itself, and the publish job +(`tools/release_train.py gate`) publishes nothing unless both pass: -- **Stable**, the default: a tested build about once a day, the alpha builds promoted below. -- **Alpha**: every alpha build as soon as it is built, many times a day; it can break. "Alpha" is the project's - word for builds that are still being tested (the whole of Horizon is in beta); Stable is the tested ones. An open - launcher looks for releases every 30 minutes on Alpha (4 hours on Stable). Launchers from before 0.2 had a "Beta - builds" switch instead; on, it reads as Alpha. +- the release smoke test: the macOS build runs the game offscreen on the Mac mini and keeps its report, + `smoke-test.json`, only when the game drew a world. The gate reads the report again before publishing, and the + release carries it. A run whose macOS build went to GitHub (`MAC_RUNNER=macos-latest`) has no report, so it + publishes nothing; +- no open issue labelled `release-blocker`. `decide` checks first, so a blocked train builds nothing; the gate checks + again right before publishing, so an issue labelled while the builds ran still holds the release. Only the label + and the issue's state count, never its text: whoever opened the issue can edit the text after a maintainer + labels it. Close the issue (or remove the label) and the next run releases everything waiting, with notes + covering all of it. -`horizon-server update` installs stable builds (`--version ` takes an alpha build by name). In Discord's #releases every -Alpha is posted silently and only Stable posts notify (above). +A failed gate fails the run; the next run (10 minutes later) decides again. Nothing older than the latest release is +ever published: tags only count up. -`.github/workflows/promote.yml` runs `tools/release_channels.py promote` once a day at 20:00 UTC (21:00 in Paris in -winter time, 22:00 in summer), with retries until 21:37 for a run GitHub starts late or skips. It promotes the newest -beta that +The launcher has no channel setting any more and looks for releases every 30 minutes. The game pre-releases of the +old Alpha channel (v0.2.1 on, published before this change) stay listed for anyone who wants one by hand, but no +launcher installs them as an update, and `release_train.py` ignores them: the first release of the single channel +lists everything since the last release that was not a pre-release (v0.2.0). Launchers from before this change keep +updating either way: with Alpha (or 0.17's "Beta builds") on they take every release, pre-release or not; with it +off, every release that is not a pre-release, which is now every new one. -- is newer than the stable release, -- had been out 6 hours at 20:00 UTC (published by 14:00 UTC that day; one published later waits for the next day), - so every run of the evening picks the same beta and stable moves once a day, -- passed its release smoke test: the macOS build attaches `smoke-test.json` (the offscreen game run's report) only - when the game drew a world on the Mac mini, so a release built while `MAC_RUNNER` sends macOS to GitHub waits - for a hand, -- and isn't covered by an issue labelled `release-blocker`. An open blocker holds every beta; closed as completed - it still holds the betas published before it closed (they have the bug); closed as not planned it holds - nothing. Only the label and the issue's state count, never its text: whoever opened the issue can edit the - text after a maintainer labels it, so it can't pick which betas go out. - -Nothing older than the stable release is ever promoted, not even with `force`: GitHub's latest release is what -`horizon-server update` and `install.sh` download, so it only moves forward. - -When the oldest beta newer than the stable release has waited 30 hours (it missed a daily promotion it was due for; -on a normal day a beta waits at most that long and is promoted), promote.yml opens an issue titled -"Promotion to stable has stalled" with the reason from the last check (a blocker left open, smoke tests not -running). It closes itself after the next promotion. - -Promoting marks the beta stable and latest and rewrites its notes to list everything since the previous stable -release (`release_train.py notes --stable`), so players who skipped the betas see all of it. Older betas stay -betas. By hand, from a phone too (GitHub app: Actions, promote, Run workflow): - -```sh -gh workflow run promote -f tag=v0.2.3 # that beta now: no wait for 20:00, still no blocker -gh workflow run promote -f tag=v0.2.3 -f force=true # even blocked or without a smoke test -gh workflow run promote -f dry_run=true # what would be promoted now -``` - -An alpha build's notes cover only what changed since the release before it (alpha or stable), and say it is an alpha build. -`release_train.py decide` counts pull requests from the newest release of either channel, so betas don't -release the same changes again. +`promote.yml` and `tools/release_channels.py` (the daily promotion at 20:00 UTC) are gone. ### Signed checksums @@ -298,7 +272,8 @@ gh variable set RELEASES_PAUSED --body 1 # no automatic game or launcher rel gh variable delete RELEASES_PAUSED # back on; the next hourly run catches up ``` -While paused, runs still decide and say why in their summary; `force=true` still releases. +While paused, runs still decide and say why in their summary; `force=true` still releases, but never past an open +`release-blocker`. ### Rolling back @@ -308,8 +283,8 @@ A bad release is withdrawn by turning it back into a draft: gh release edit v0.2.3 --draft ``` -A beta that is not promoted yet needs no rollback: label an issue about it `release-blocker`. For a stable -release: launchers list only published releases, so the previous version becomes the newest one again, launchers that +To stop the next releases while a problem is fixed, label an issue about it `release-blocker`. For the bad +release itself: launchers list only published releases, so the previous version becomes the newest one again, launchers that didn't install the bad one won't, and players who did can pick the previous version in the launcher's version list (`launcher/src/backend.rs` tests `withdrawn_release_is_never_offered`). The tag stays, so the next release that day takes the next number, and its notes start from the last *published* release, so they list the diff --git a/docs/security.md b/docs/security.md index 175e5d21d..886f149d5 100644 --- a/docs/security.md +++ b/docs/security.md @@ -38,7 +38,7 @@ and some are waiting for Ludovic: | Private vulnerability reporting | **Live** (see the end of this page). | | Signed checksums on releases | **Live** for new releases (#1011): the `release` environment holds the key, and its public half is in `release-keys.txt` (#1023). Releases from before then are unsigned. | | Launchers and servers checking signatures | **Not yet**: #1014 waits as a draft until a signed stable game release and a signed launcher release exist. Until then they trust GitHub's checksum, as before. | -| Alpha, then stable | **Live** (#1012): every release is first an alpha build. The launcher's Updates setting picks Stable (the default) or Alpha (the pre-releases). | +| Smoke test and release-blocker before publishing | **Live**: since 2026-10-23 every release is the latest at once (one channel, Ludovic's decision), so it is published only after its smoke test passed and while no `release-blocker` is open. The 6-hour beta of the old Alpha, then stable flow (#1012) is gone. | | Reports marked suspicious | **Partly**: the scan of Discord for agents does it. Kira's issues do it once its pull request merges. | | Code owners' review of the pipeline | **Not yet**: `.github/CODEOWNERS` and its protection (#1015) wait for the App, below. Until then pipeline changes merge like any other once `verify` passes. | | Agents on their own GitHub App | **Not yet**: prepared, waiting for Ludovic to create it. Agents still use his account. | @@ -53,11 +53,10 @@ and some are waiting for Ludovic: - **`verify` always runs.** Labels no longer start the checks, and the required `verify` job has no condition, so the only `verify` a change can have is one that ran. Main requires it, from GitHub Actions only, for everyone, admins included. -- **Alpha, then stable.** Every release is first an **alpha** build (a GitHub pre-release). It becomes **stable**, the version launchers and - servers install by themselves, in the daily promotion (20:00 UTC), only after it has been out 6 hours and passed its - smoke test (the game started and drew a world on the Mac mini), and only while no issue labelled `release-blocker` - is open. If a beta misses a promotion it was due for, an issue says so. Players who want the newest builds at once - pick **Alpha** under Updates in the launcher's Settings. Discord announces stable releases only. +- **Checked before it is published.** Every release is published as the latest, which launchers and servers + install by themselves, so the release run publishes it only after its smoke test passed (the game started and + drew a world on the Mac mini) and only while no issue labelled `release-blocker` is open; otherwise nothing is + published. - **Signed updates.** Each release carries a list of its files' checksums, signed with Horizon's release key. The launcher (for the game and for itself) and `horizon-server update` check that signature against the public key built into them, then check the download against the list. Anything unsigned, signed with another key, signed @@ -78,9 +77,8 @@ and some are waiting for Ludovic: ## What still needs Ludovic - **Approving pipeline changes** (rare): a pull request touching a file in `.github/CODEOWNERS`. -- **Holding a release back or speeding one up**, from his phone if he likes. Label an issue `release-blocker` to - hold every beta until it is closed. Run the `promote` workflow (GitHub app: Actions, - promote, Run workflow) to make a beta stable at once. +- **Holding releases back or speeding one up**, from his phone if he likes. Label an issue `release-blocker` to + hold every release until it is closed; label a pull request `release-now` to release it as soon as it merges. - **Creating the GitHub App**: one link, "Create GitHub App", then "Install". His login, his clicks. - **Fork pull requests**: their checks run only after a maintainer approves them, and they run on the Mac mini only after a maintainer has read them and added `run-parity`. @@ -90,8 +88,9 @@ and some are waiting for Ludovic: ## What this doesn't solve - **Code that merges is code that ships.** Signing proves a build came from Horizon's pipeline, not that the code - is good. Against a bad change, the safety nets are the checks and harnesses, review, the 6-hour beta and the - release-blocker label. + is good. Against a bad change, the safety nets are the checks and harnesses, review, the release smoke test and + the release-blocker label. Since the single channel there is no 6-hour beta: a change reaches every player + within minutes of merging. - **Until agents switch to the App**, they still work with Ludovic's own account, so nothing technical tells his actions from theirs. Code owners' reviews are switched on together with the App. - **The Mac mini** builds the macOS release and also runs pull requests' parity checks: a pull request from this diff --git a/docs/versioning.md b/docs/versioning.md index 6032b54a1..4ed0484e1 100644 --- a/docs/versioning.md +++ b/docs/versioning.md @@ -22,10 +22,10 @@ game and the launcher of the 0.2 line. 0.2. A `feature` label no longer bumps anything: it only lists the change under **New**. - **Patch releases** are every release the train makes: the patch after the newest release tagged on the line, game and launcher counted separately (the game releases more often, so `v0.2.14` can go with `launcher@0.2.3`). -- **Alpha builds** are not separate tags. As before, every game release is published as a GitHub pre-release (an alpha build, what - the launcher's Alpha channel installs) under its final tag and promoted in place to stable in the daily - promotion at 20:00 UTC, once it has been out 6 hours ([Channels](release-flow.md#channels)). `v0.2.4` is the same - build whether it is still a beta or already stable. +- **One channel** (since 2026-10-23): every game release is published under its final tag as the release and + GitHub's latest, once its smoke test passed and no `release-blocker` is open + ([One channel](release-flow.md#one-channel)). The game pre-releases from before (v0.2.1 on, the old Alpha + channel) keep their tags and stay pre-releases. - **Pre-release tags** (`v0.3.0-rc.1`, `v0.3.0-beta.2`) are semver's and are reserved for a hand-made preview of a new line; the train never makes them. Every reader orders them before their release and after the release before (`v0.2.9` < `v0.3.0-beta.1` < `v0.3.0-rc.1` < `v0.3.0`). @@ -43,7 +43,7 @@ Every 0.2 tag is newer than every tag from before 0.2. Within each: A tag is a 0.2 tag when it is `v` or `launcher@` followed by exactly three numbers (and an optional `-PRE`) with a major below 1000; a date's first number is a year, so `v2026.10.21` never is. The rule is written once per language, with the same tests: `crates/horizon-server/src/update.rs` (`newer`), `launcher/src/backend.rs` -(`release_order`), `tools/release_train.py` (`tag_key`, also used by `tools/release_channels.py`) and +(`release_order`), `tools/release_train.py` (`tag_key`) and `website/src/versions.js`. ## The workflows @@ -55,9 +55,6 @@ language, with the same tests: `crates/horizon-server/src/update.rs` (`newer`), - **`launcher.yml`**: `decide --kind launcher` picks `launcher@0.2.N`; the build gets `HORIZON_LAUNCHER_VERSION` (`0.2.N`) and `HORIZON_LAUNCHER_TAG` (`launcher@0.2.N`). The tag tells the launcher which line it is on: a release built with only the version (every launcher before 0.2) is a `launcher-v` one. -- **`promote.yml`**: `tools/release_channels.py` promotes the newest due beta, never one older than the stable - release; with the shared order, `v0.2.0` follows the last dated stable release and no dated beta can follow - `v0.2.0`. - **Notes**: a line's first release (`X.Y.0`) opens with a paragraph saying so; `v0.2.0`'s and `launcher@0.2.0`'s say that releases are numbered from now on, and tell server owners to run `horizon-server update` (or, if their server says it is up to date, `horizon-server update --version v0.2.0`). @@ -98,6 +95,9 @@ the fleet's `release-kick` and the orchestrator's heartbeat prompts. ## The day Ludovic says go +Done on 2026-10-22; kept as the record of how 0.2 went out. `promote.yml` and `tools/release_channels.py`, which +it uses, are gone since the single channel (2026-10-23, [One channel](release-flow.md#one-channel)). + Run by the orchestrator, in this order. Until step 3, nothing is published. 1. Merge, in order: this page, the `horizon-server update` order, the launcher order, the release line (train, diff --git a/tests/test_release_channels.py b/tests/test_release_channels.py deleted file mode 100644 index 288fa7939..000000000 --- a/tests/test_release_channels.py +++ /dev/null @@ -1,147 +0,0 @@ -import datetime as dt -import unittest - -from tools import release_channels as channels -from tools import release_train as train - -# Just after the daily promotion (PROMOTE_AT, 20:00 UTC): a beta is due when it had been out 6 hours by 20:00. -NOW = dt.datetime(2026, 10, 19, 20, 30, tzinfo=dt.timezone.utc) - - -def release(tag, hours_ago, prerelease=True, smoke=True, draft=False): - published = (NOW - dt.timedelta(hours=hours_ago)).isoformat().replace("+00:00", "Z") - assets = [{"name": "horizon-linux-x86_64.tar.gz"}] + ([{"name": channels.SMOKE_ASSET}] if smoke else []) - return {"tag_name": tag, "published_at": published, "prerelease": prerelease, "draft": draft, "assets": assets} - - -def blocker(number, title, body="", state="open", reason=None, closed_hours_ago=None): - closed = None if closed_hours_ago is None else (NOW - dt.timedelta(hours=closed_hours_ago)).isoformat() - return {"number": number, "title": title, "body": body, "state": state, "state_reason": reason, "closed_at": closed} - - -STABLE = release("v2026.10.18.04", 30, prerelease=False) - - -class ReleaseChannelTests(unittest.TestCase): - def test_the_newest_beta_out_six_hours_with_a_smoke_test_is_promoted(self): - releases = [STABLE, release("v2026.10.19", 9), release("v2026.10.19.01", 7), release("v2026.10.19.02", 2)] - chosen, why = channels.choose(releases, [], NOW) - self.assertEqual(chosen["tag_name"], "v2026.10.19.01") - self.assertIn("6 h", why) - - def test_nothing_is_due_before_six_hours(self): - chosen, why = channels.choose([STABLE, release("v2026.10.19", 6)], [], NOW) - self.assertIsNone(chosen, "out 5 h 30 min at 20:00") - self.assertIn("the one at 2026-10-20 20:00 UTC takes it", why) - late = channels.choose([STABLE, release("v2026.10.19", 0.5)], [], NOW)[1] - self.assertIn("the one at 2026-10-20 20:00 UTC takes it", late, "out after 20:00: 6 h by the next one") - - def test_stable_moves_once_a_day_whenever_the_run_starts(self): - hour = dt.timedelta(hours=1) - releases = [STABLE, release("v2026.10.19", 9), release("v2026.10.19.01", 6)] - # Before 20:00 nothing new is due, even what has been out 6 hours by now. - before = channels.choose(releases, [], NOW - hour) - self.assertIsNone(before[0], before[1]) - # The 20:00 run and a late one or a retry pick the same beta; once it is stable, the evening's later runs - # promote nothing, though v2026.10.19.01 has been out 6 hours by then. - for late in (0, 1, 3): - self.assertEqual(channels.choose(releases, [], NOW + late * hour)[0]["tag_name"], "v2026.10.19") - promoted = [release("v2026.10.19", 9, prerelease=False), releases[2]] - self.assertIsNone(channels.choose(promoted, [], NOW + 3 * hour)[0]) - # A run GitHub skipped is caught up by the next one, at any hour before the next day's promotion. - self.assertEqual(channels.choose(releases, [], NOW + 20 * hour)[0]["tag_name"], "v2026.10.19") - # The next day's promotion takes the one that came out too late for today's. - self.assertEqual(channels.choose(promoted, [], NOW + 24 * hour)[0]["tag_name"], "v2026.10.19.01") - self.assertEqual(channels.promotion_time(NOW), dt.datetime(2026, 10, 19, 20, 0, tzinfo=dt.timezone.utc)) - self.assertEqual(channels.promotion_time(NOW - hour), dt.datetime(2026, 10, 18, 20, 0, tzinfo=dt.timezone.utc)) - - def test_a_beta_without_its_smoke_test_waits_for_a_hand(self): - releases = [STABLE, release("v2026.10.19", 8, smoke=False)] - self.assertIsNone(channels.choose(releases, [], NOW)[0]) - self.assertIsNone(channels.choose(releases, [], NOW, tag="v2026.10.19")[0]) - self.assertEqual(channels.choose(releases, [], NOW, tag="v2026.10.19", force=True)[0]["tag_name"], "v2026.10.19") - - def test_by_hand_skips_the_wait_but_not_a_blocker(self): - releases = [STABLE, release("v2026.10.19.02", 1)] - self.assertEqual(channels.choose(releases, [], NOW, tag="v2026.10.19.02")[0]["tag_name"], "v2026.10.19.02") - blocked = [blocker(5, "Crash on start in 2026.10.19.2")] - chosen, why = channels.choose(releases, blocked, NOW, tag="v2026.10.19.02") - self.assertIsNone(chosen) - self.assertIn("#5", why) - self.assertIsNotNone(channels.choose(releases, blocked, NOW, tag="v2026.10.19.02", force=True)[0]) - - def test_stable_releases_and_drafts_are_never_promoted(self): - self.assertIsNone(channels.choose([STABLE], [], NOW, tag=STABLE["tag_name"])[0]) - withdrawn = release("v2026.10.19", 9, draft=True) - self.assertIsNone(channels.choose([STABLE, withdrawn], [], NOW)[0]) - older = release("v2026.10.18.03", 40) - self.assertIsNone(channels.choose([STABLE, older], [], NOW)[0]) - - def test_an_open_blocker_holds_every_beta_whatever_its_text_says(self): - releases = [STABLE, release("v2026.10.19", 12), release("v2026.10.19.01", 10), release("v2026.10.19.02", 8)] - # The issue's author can edit its text after it is labelled: naming a later version must not free a beta. - for text in ("Something is badly wrong", "Only in v2099.01.01", "Since v2026.10.19.01 the chests…"): - chosen, why = channels.choose(releases, [blocker(7, text, body=text)], NOW) - self.assertIsNone(chosen, text) - self.assertIn("release-blocker #7", why) - - def test_a_fixed_blocker_still_holds_the_betas_published_before_the_fix(self): - fixed = blocker(9, "Crash", state="closed", reason="completed", closed_hours_ago=7.5) - releases = [STABLE, release("v2026.10.19", 12), release("v2026.10.19.01", 7)] - self.assertEqual(channels.choose(releases, [fixed], NOW)[0]["tag_name"], "v2026.10.19.01") - self.assertIsNone(channels.choose(releases[:2], [fixed], NOW)[0]) - not_planned = blocker(10, "Crash", state="closed", reason="not_planned", closed_hours_ago=1) - self.assertEqual(channels.choose(releases[:2], [not_planned], NOW)[0]["tag_name"], "v2026.10.19") - - def test_force_never_moves_latest_backwards(self): - # horizon-server update and install.sh download GitHub's latest: an older beta must never become it. - older = release("v2026.10.18.03", 40, smoke=False) - chosen, why = channels.choose([STABLE, older], [blocker(3, "x")], NOW, tag="v2026.10.18.03", force=True) - self.assertIsNone(chosen) - self.assertIn("not newer than the stable v2026.10.18.04", why) - same = release("v2026.10.18.04", 30, prerelease=False) - self.assertIsNone(channels.choose([same], [], NOW, tag="v2026.10.18.04", force=True)[0]) - - def test_0_2_follows_the_dated_releases(self): - bridge = release("v2026.10.21", 30, prerelease=False) - first = release("v0.2.0", 7) - chosen, why = channels.choose([bridge, first], [], NOW) - self.assertEqual(chosen["tag_name"], "v0.2.0", why) - stable = release("v0.2.0", 30, prerelease=False) - older = release("v2026.10.22", 8) - self.assertIsNone(channels.choose([stable, older], [], NOW)[0], "a dated beta never follows 0.2") - chosen, why = channels.choose([stable, older], [], NOW, tag="v2026.10.22", force=True) - self.assertIsNone(chosen) - self.assertIn("not newer than the stable v0.2.0", why) - patch = release("v0.2.1", 7) - self.assertEqual(channels.choose([stable, older, patch], [], NOW)[0]["tag_name"], "v0.2.1") - self.assertEqual(channels.stable([stable, bridge])["tag_name"], "v0.2.0") - launcher = release("launcher@0.2.0", 7) - self.assertIsNone(channels.choose([bridge, launcher], [], NOW)[0], "launcher releases are never game betas") - - def test_a_stall_is_reported_once_the_oldest_waiting_beta_missed_a_daily_promotion(self): - # Out at 14:31 yesterday: just too late for yesterday's 20:00, so 29 h 59 min of waiting is a normal day. - self.assertIsNone(channels.stalled([STABLE, release("v2026.10.19", 29.99), release("v2026.10.19.01", 2)], NOW)) - releases = [STABLE, release("v2026.10.19", 31), release("v2026.10.19.01", 2)] - oldest, age = channels.stalled(releases, NOW) - self.assertEqual(oldest["tag_name"], "v2026.10.19") - title, body = channels.stall_issue(releases, (oldest, age), "no beta is due (release-blocker #4)") - self.assertEqual(title, "Promotion to stable has stalled: v2026.10.19 has waited 31 h") - self.assertIn("v2026.10.18.04", body) - self.assertIn("release-blocker #4", body) - # Betas older than the stable release are contained in it: they aren't waiting. - self.assertIsNone(channels.stalled([STABLE, release("v2026.10.18.03", 40)], NOW)) - self.assertIsNone(channels.stalled([STABLE], NOW)) - - def test_beta_notes_say_so_and_stable_notes_do_not(self): - pr = {"number": 1, "title": "Fix a thing", "labels": [], "paths": ["crates/x.rs"], "issues": [], "author": "ludovic111"} - _, beta = train.notes("game", "v2026.10.19", [pr], [], beta=True) - _, stable = train.notes("game", "v2026.10.19", [pr], [], beta=False) - self.assertIn("This is an alpha build: launchers on the Alpha channel", beta) - self.assertNotIn("alpha", stable.lower()) - _, launcher = train.notes("launcher", "launcher-v0.9.0", [pr], [], beta=True) - self.assertNotIn("alpha", launcher.lower()) - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/test_release_train.py b/tests/test_release_train.py index 2a41ae810..298077352 100644 --- a/tests/test_release_train.py +++ b/tests/test_release_train.py @@ -104,7 +104,7 @@ class ReleaseTrainTests(unittest.TestCase): self.assertFalse(train.decision("game", [], NOW)[0]) def test_the_first_release_of_0_2_says_what_changed(self): - title, body = train.notes("game", "v0.2.0", [pr(1)], [], beta=True, previous="v2026.10.21") + title, body = train.notes("game", "v0.2.0", [pr(1)], [], previous="v2026.10.21") self.assertTrue(title.startswith("Horizon 0.2.0: "), title) self.assertTrue(body.startswith("Horizon is numbered from now on: this is Horizon 0.2.0, the release after 2026.10.21.")) self.assertIn("`horizon-server update --version v0.2.0`", body) @@ -132,6 +132,32 @@ class ReleaseTrainTests(unittest.TestCase): self.assertFalse(decide([pr(1)], paused=True)) self.assertTrue(decide([], paused=True, force=True)) + def test_an_open_release_blocker_holds_every_release_even_forced(self): + release, why = train.decision("game", [pr(1, labels=["release-now"])], NOW, blockers=[7, 9]) + self.assertFalse(release) + self.assertEqual(why, "held by release-blocker #7, release-blocker #9") + self.assertFalse(train.decision("game", [], NOW, force=True, blockers=[7])[0]) + self.assertFalse(train.decision("game", [], NOW, new_line="0.3", blockers=[7])[0]) + self.assertTrue(train.decision("game", [pr(1)], NOW, blockers=[])[0]) + + def test_a_release_is_published_only_after_its_smoke_test_and_with_no_blocker(self): + drew = {"error": None, "frames": 12710, "chunks": 81, "meshed_sections": 550, "player": [160.5, 71.0, 156.5]} + self.assertIsNone(train.gate(drew, [])) + self.assertEqual(train.gate(drew, [4]), "release-blocker #4 is open") + self.assertEqual(train.gate(None, []), "no smoke-test.json: the release smoke test did not run") + self.assertEqual(train.gate(None, [4]), + "no smoke-test.json: the release smoke test did not run; release-blocker #4 is open") + self.assertEqual(train.smoke_problem({**drew, "error": "device lost"}), "the smoke run failed: device lost") + for broken in ({**drew, "frames": 1499}, {**drew, "chunks": 0}, {**drew, "meshed_sections": 0}, + {**drew, "player": None}, {}): + self.assertEqual(train.smoke_problem(broken), "the smoke run did not draw a world", broken) + + def test_notes_never_call_a_release_alpha_or_beta(self): + _, body = train.notes("game", "v0.2.12", [pr(1)], []) + self.assertNotIn("alpha", body.lower()) + self.assertNotIn("beta", body.lower()) + self.assertNotIn("Stable", body) + def test_notes(self): prs = [pr(5, "Nether portals: vanilla timing and linking (fixes #94)", issues=[94]), pr(6, "Controllers: gilrs input layer and gameplay", labels=["feature"], author="cosmic"), diff --git a/tools/release_channels.py b/tools/release_channels.py deleted file mode 100644 index 869127c5c..000000000 --- a/tools/release_channels.py +++ /dev/null @@ -1,220 +0,0 @@ -#!/usr/bin/env python3 -"""Promotes Horizon's beta game releases to stable once a day (.github/workflows/promote.yml, docs/release-flow.md). - - python3 tools/release_channels.py promote # the newest beta that is due, if any - python3 tools/release_channels.py promote --tag v2026.10.19.03 # that beta now (by hand) - python3 tools/release_channels.py promote --tag v2026.10.19.03 --force # even if blocked or untested - python3 tools/release_channels.py promote --dry-run # say what would happen, change nothing - -Every game release goes out as a beta (a GitHub pre-release): launchers on the Alpha channel install it at once. -Stable moves once a day, at PROMOTE_AT (20:00 UTC). A beta is due for stable when: -- it is newer than the stable release (GitHub's "latest", which launchers and servers install by default), -- it had been out PROMOTE_AFTER (6 hours) at the day's promotion time, so every run until the next one picks the - same beta and a run GitHub starts late (or a second try) never promotes twice; by hand (--tag) skips the wait, -- its release smoke test passed: release.yml attaches smoke-test.json only after the Mac mini ran the game, -- and no issue labelled release-blocker covers it. An open blocker holds every beta; one closed as completed keeps - holding the betas published before it closed (they still have the bug); one closed as not planned holds nothing. - Only people who can triage the repository can add the label, and the issue's text is never read: whoever wrote - the issue could edit it afterwards, so it can't decide which betas go out. - -The newest due beta is promoted: marked stable and latest, with notes covering everything since the previous -stable release (tools/release_train.py notes --stable). Older betas stay betas: the stable one contains them. -Nothing older than the current stable release is ever promoted, even with --force: GitHub's "latest" is what -horizon-server update and install.sh download, and it only moves forward. - -When the oldest beta newer than the stable release has waited STALL_AFTER (30 hours: past one daily promotion it -was due for, so a normal day never trips it), promotion has stalled -(blocked, smoke tests not running, the schedule off): an issue says so, and is closed once a beta is promoted. -""" - -import argparse -import datetime as dt -import json -import os -from pathlib import Path -import subprocess -import sys -import tempfile - -ROOT = Path(__file__).resolve().parents[1] -sys.path.insert(0, str(ROOT)) -from tools.release_train import is_game_tag, tag_key # noqa: E402 (vYYYY.MM.DD[.N] and v0.2.0: docs/versioning.md) - -REPO = os.environ.get("GITHUB_REPOSITORY", "ludovic111/horizon") -PROMOTE_AFTER = dt.timedelta(hours=6) -# The daily promotion (21:00 in Paris in winter time, 22:00 in summer); promote.yml's schedule runs just after it. -PROMOTE_AT = dt.time(20, 0) -SMOKE_ASSET = "smoke-test.json" -BLOCKER = "release-blocker" -STALL_AFTER = dt.timedelta(hours=30) -STALLED = "Promotion to stable has stalled" - - -def parse_time(text): - return dt.datetime.fromisoformat(text.replace("Z", "+00:00")) if text else None - - -def covers(blocker, release): - """Whether a release-blocker issue holds this beta back. Only its state and dates count, never its text.""" - if blocker.get("state") != "closed": - return True - closed = parse_time(blocker.get("closed_at")) - return blocker.get("state_reason") == "completed" and bool(closed) and parse_time(release["published_at"]) < closed - - -def promotion_time(now): - """The last daily promotion time (PROMOTE_AT, UTC) at or before now.""" - now = now.astimezone(dt.timezone.utc) - at = dt.datetime.combine(now.date(), PROMOTE_AT, dt.timezone.utc) - return at if at <= now else at - dt.timedelta(days=1) - - -def stable(releases): - """The newest stable (non pre-release) game release.""" - stables = [r for r in releases if not r["prerelease"]] - return max(stables, key=lambda r: tag_key(r["tag_name"]), default=None) - - -def reasons_against(release, current, blockers, now, by_hand=False): - """Why this beta can't become stable now: an empty list when it can.""" - reasons = [] - if not release["prerelease"]: - reasons.append("it is already stable") - if current and tag_key(release["tag_name"]) <= tag_key(current["tag_name"]): - reasons.append(f"it is not newer than the stable {current['tag_name']}") - ready = parse_time(release["published_at"]) + PROMOTE_AFTER - if not by_hand and ready > promotion_time(now): - due = promotion_time(ready) - due += dt.timedelta(days=1 if due < ready else 0) - reasons.append(f"it had not been out {PROMOTE_AFTER.seconds // 3600} h at a daily promotion yet: " - f"the one at {due:%Y-%m-%d %H:%M} UTC takes it") - if not any(asset["name"] == SMOKE_ASSET for asset in release.get("assets", [])): - reasons.append(f"it has no {SMOKE_ASSET}: the release smoke test did not run") - for blocker in blockers: - if covers(blocker, release): - reasons.append(f"release-blocker #{blocker['number']}") - return reasons - - -def choose(releases, blockers, now, tag=None, force=False): - """(the beta to promote or None, a line saying why) for the game releases `releases`.""" - games = [r for r in releases if not r["draft"] and is_game_tag(r["tag_name"])] - current = stable(games) - if tag: - release = next((r for r in games if r["tag_name"] == tag), None) - if not release: - return None, f"no published game release is tagged {tag}" - reasons = reasons_against(release, current, blockers, now, by_hand=True) - if force: - # Even forced, "latest" only moves forward: horizon-server update and install.sh follow it. - reasons = [r for r in reasons if r == "it is already stable" or r.startswith("it is not newer")] - if reasons: - return None, f"{tag} stays a beta: " + "; ".join(reasons) - return release, f"{tag} promoted by hand" + (" with force" if force else "") - betas = sorted((r for r in games if r["prerelease"]), key=lambda r: tag_key(r["tag_name"]), reverse=True) - held = [] - for release in betas: - reasons = reasons_against(release, current, blockers, now) - if not reasons: - return release, f"{release['tag_name']} had been out {PROMOTE_AFTER.seconds // 3600} h at the daily promotion, " \ - "passed its smoke test and no release-blocker covers it" - held.append(f"{release['tag_name']}: " + "; ".join(reasons)) - return None, "no beta is due" + (" (" + " / ".join(held[:3]) + ")" if held else "") - - -def stalled(releases, now): - """(the oldest beta newer than the stable release, how long it has waited) once that is STALL_AFTER, else None.""" - games = [r for r in releases if not r["draft"] and is_game_tag(r["tag_name"])] - current = stable(games) - waiting = [r for r in games if r["prerelease"] and (not current or tag_key(r["tag_name"]) > tag_key(current["tag_name"]))] - if not waiting: - return None - oldest = min(waiting, key=lambda r: parse_time(r["published_at"])) - age = now - parse_time(oldest["published_at"]) - return (oldest, age) if age >= STALL_AFTER else None - - -def stall_issue(releases, stall, reason): - """The title and body of the issue saying promotion has stalled.""" - oldest, age = stall - current = stable([r for r in releases if not r["draft"] and is_game_tag(r["tag_name"])]) - hours = int(age.total_seconds() // 3600) - body = (f"Opened by .github/workflows/promote.yml, not by a person.\n\n" - f"No beta has become stable for {hours} hours: {oldest['tag_name']} has waited since " - f"{oldest['published_at']}, and launchers, servers and install.sh still get " - f"{current['tag_name'] if current else 'no stable release'}.\n\n" - f"The last check said: {reason}\n\n" - "Usual causes: an open `release-blocker` issue (it holds every beta until it is closed), the Mac mini's " - "release smoke test not running (no smoke-test.json on the betas), or the promote workflow disabled. " - "To promote a beta by hand: `gh workflow run promote -f tag=` (add `-f force=true` to skip the " - "smoke test and blockers). This issue closes itself once a beta is promoted.") - return f"{STALLED}: {oldest['tag_name']} has waited {hours} h", body - - -def gh(*arguments, capture=True): - result = subprocess.run(["gh", *arguments], cwd=ROOT, capture_output=capture, text=True) - if result.returncode: - raise SystemExit(f"gh {' '.join(arguments)}: {(result.stderr or '').strip()}") - return result.stdout - - -def gh_pages(path): - return [item for page in json.loads(gh("api", path, "--paginate", "--slurp") or "[]") for item in page] - - -def promote(release): - """Marks the beta stable and latest, with notes covering everything since the previous stable release.""" - tag = release["tag_name"] - with tempfile.TemporaryDirectory() as out: - subprocess.run([sys.executable, str(ROOT / "tools" / "release_train.py"), "notes", "--kind", "game", "--tag", tag, - "--head", tag, "--stable", "--out", out], cwd=ROOT, check=True, stdout=subprocess.DEVNULL) - title = (Path(out) / "title.txt").read_text().strip() - gh("release", "edit", tag, "--repo", REPO, "--prerelease=false", "--latest", "--title", title, - "--notes-file", str(Path(out) / "body.md")) - - -def main(): - parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) - parser.add_argument("command", choices=["promote"]) - parser.add_argument("--tag", help="promote this beta now (by hand: no wait for the daily promotion)") - parser.add_argument("--force", action="store_true", help="with --tag: even without a smoke test or with a blocker") - parser.add_argument("--dry-run", action="store_true", help="say what would be promoted, change nothing") - parser.add_argument("--now", help="the time to decide at (ISO 8601; default now)") - args = parser.parse_args() - if args.force and not args.tag: - parser.error("--force needs --tag: only a named beta is forced") - now = dt.datetime.fromisoformat(args.now) if args.now else dt.datetime.now(dt.timezone.utc) - now = now if now.tzinfo else now.replace(tzinfo=dt.timezone.utc) - releases = gh_pages(f"repos/{REPO}/releases?per_page=100") - blockers = [issue for issue in gh_pages(f"repos/{REPO}/issues?labels={BLOCKER}&state=all&per_page=100") - if "pull_request" not in issue] - release, reason = choose(releases, blockers, now, args.tag, args.force) - if release and not args.dry_run: - promote(release) - line = ("Would promote: " if release and args.dry_run else "") + reason - print(line) - if os.environ.get("GITHUB_STEP_SUMMARY"): - with open(os.environ["GITHUB_STEP_SUMMARY"], "a") as summary: - summary.write(f"### {line}\n") - report_stall(releases, release, reason, now, args.dry_run) - return 1 if args.tag and not release else 0 - - -def report_stall(releases, promoted, reason, now, dry_run): - """Opens the stalled-promotion issue when one is due and none is open; closes it after a promotion.""" - open_issues = [issue for issue in gh_pages(f"repos/{REPO}/issues?state=open&per_page=100") - if "pull_request" not in issue and issue["title"].startswith(STALLED)] - stall = None if promoted else stalled(releases, now) - if stall and not open_issues: - title, body = stall_issue(releases, stall, reason) - print(("Would open: " if dry_run else "Opening: ") + title) - if not dry_run: - gh("issue", "create", "--repo", REPO, "--title", title, "--body", body) - if promoted and not dry_run: - for issue in open_issues: - gh("issue", "close", str(issue["number"]), "--repo", REPO, "--comment", - f"{promoted['tag_name']} is stable now (promote.yml).") - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/tools/release_train.py b/tools/release_train.py index 0cc37eef7..b8d6468bb 100644 --- a/tools/release_train.py +++ b/tools/release_train.py @@ -4,13 +4,14 @@ python3 tools/release_train.py decide --kind game [--head SHA] [--force] python3 tools/release_train.py decide --kind launcher python3 tools/release_train.py notes --kind game --tag v2026.10.15 --out dist/notes - python3 tools/release_train.py notes --kind game --tag v2026.10.15 --head v2026.10.15 --stable # at promotion + python3 tools/release_train.py gate --kind game --smoke dist/smoke-test.json [--blockers] # before publishing A kind releases when a pull request merged into main since its last published release changed one of its player-facing paths (GAME_PATHS, LAUNCHER_PATHS) and the newest of them merged at least DEBOUNCE ago (a burst of merges becomes one release), or the oldest at least MAX_WAIT ago (merges that never pause still ship), or when one of those pull requests is labelled release-now. Pull requests labelled skip-release (work players don't see) don't count -towards a release and aren't listed in the notes. RELEASES_PAUSED=1 (a repository variable) stops automatic releases. +towards a release and aren't listed in the notes. RELEASES_PAUSED=1 (a repository variable) stops automatic releases, +and an open issue labelled release-blocker stops game releases, even forced ones, until it is closed. Tags (docs/versioning.md): with a release line in .github/release-line.txt (MAJOR.MINOR, e.g. 0.2), game releases are vMAJOR.MINOR.PATCH and launcher releases launcher@MAJOR.MINOR.PATCH, each the patch after the last tag of its kind @@ -19,9 +20,11 @@ file, the tags from before 0.2: vYYYY.MM.DD, then .01, .02 the same UTC day, and the last launcher tag or the next minor when a merged pull request is labelled feature. Every 0.2 tag is newer than every tag from before (tag_key). Notes: "### New" lists the pull requests labelled feature, "### Fixed" the others, "### Known issues" the bullets of -docs/known-issues.md. Game releases go out as betas (GitHub pre-releases) and tools/release_channels.py promotes -them to stable; a beta's notes cover what changed since the release before it (beta or stable), a promoted -release's (--stable) everything since the last stable one. See docs/release-flow.md. +docs/known-issues.md. Every release is published as the release, GitHub's latest, which launchers, servers and +install.sh take at once; one channel, no promotion. A game release is published only after `gate`: its smoke test +passed and no release-blocker is open. Notes cover everything since the last published release; the game +pre-releases of the old Alpha channel (v0.2.1 on) don't count, so the first release after them lists all of +it since v0.2.0. See docs/release-flow.md. Prints the decision as JSON and writes it to $GITHUB_OUTPUT when set. Needs git history and the gh CLI. """ @@ -37,7 +40,7 @@ import sys ROOT = Path(__file__).resolve().parents[1] REPO = os.environ.get("GITHUB_REPOSITORY", "ludovic111/horizon") -# Alpha builds ship constantly: a merge starts a release within minutes, once its wave of merges has settled. +# Releases ship constantly: a merge starts a release within minutes, once its wave of merges has settled. DEBOUNCE = dt.timedelta(minutes=5) MAX_WAIT = dt.timedelta(minutes=20) # Kept as typed in release titles, where the clauses after the first are lowercased. @@ -72,12 +75,13 @@ LAUNCHER_IGNORED = ["launcher/links.json", "launcher/docs/", "launcher/tests/"] FOOTER = { "game": "Install or update it from the Horizon launcher: it installs new versions by itself. " "New here? Get the launcher at https://playhorizon.dev. You need your own copy of Minecraft: Java Edition.", - "beta": "This is an alpha build: launchers on the Alpha channel (Settings, Updates) install it now; Stable gets it " - "in the next daily update if it holds up.", "launcher": "Your launcher updates itself. New here? Download the archive for your system below, extract it, " "and run `horizon-launcher`. Website: https://playhorizon.dev", } DISCLAIMER = "Not an official Minecraft product. Not approved by or associated with Mojang or Microsoft." +# An open issue with this label holds every game release (docs/release-flow.md). Only people who can triage the +# repository can add it, and the issue's text is never read: its author could edit it afterwards. +BLOCKER = "release-blocker" def run(*command, check=True): @@ -225,11 +229,37 @@ def pull_request(number): } -def published_releases(betas=True): - """Published (not draft) releases with their tags and dates; without betas (pre-releases) when betas is False.""" +def published_releases(): + """Published releases with their tags and dates: no drafts, and no pre-releases (the old Alpha channel's).""" releases = gh_json("api", f"repos/{REPO}/releases?per_page=100", "--paginate", "--slurp") flat = [release for page in releases for release in page] - return [release for release in flat if not release["draft"] and (betas or not release["prerelease"])] + return [release for release in flat if not release["draft"] and not release["prerelease"]] + + +def open_blockers(): + """The numbers of the open issues labelled release-blocker (pull requests left out).""" + issues = gh_json("api", f"repos/{REPO}/issues?labels={BLOCKER}&state=open&per_page=100", "--paginate", "--slurp") + return sorted(issue["number"] for page in issues for issue in page if "pull_request" not in issue) + + +def smoke_problem(report): + """Why a release smoke test's report (the game's --smoke-report) fails, or None when the game drew a world.""" + if report is None: + return "no smoke-test.json: the release smoke test did not run" + if report.get("error") is not None: + return f"the smoke run failed: {report['error']}" + if report.get("frames", 0) < 1500 or not report.get("chunks") or not report.get("meshed_sections") \ + or not report.get("player"): + return "the smoke run did not draw a world" + return None + + +def gate(report, blockers): + """Why a built game release must not be published, or None: it is published as the latest at once, so its + smoke test must have passed and no release-blocker may be open.""" + problems = [problem for problem in [smoke_problem(report)] if problem] + problems += [f"release-blocker #{number} is open" for number in blockers] + return "; ".join(problems) or None def last_release(releases, kind): @@ -238,13 +268,16 @@ def last_release(releases, kind): return max(ours, key=lambda release: tag_key(release["tag_name"]), default=None) -def decision(kind, prs, now, paused=False, force=False, new_line=None): +def decision(kind, prs, now, paused=False, force=False, new_line=None, blockers=()): """Whether to release now, and why. prs: the merged pull requests since the last release, each with the files it changed under "paths" (player-facing ones only) and when it merged ("merged_at"). new_line: the - release line ("0.2") when it has no release of this kind yet; its first goes out at once.""" + release line ("0.2") when it has no release of this kind yet; its first goes out at once. blockers: the open + release-blocker issues; they hold every release, forced ones too.""" counted = [pr for pr in prs if pr["paths"] and "skip-release" not in pr["labels"]] urgent = [pr for pr in counted if "release-now" in pr["labels"]] merged = [pr["merged_at"] for pr in counted if pr.get("merged_at")] + if blockers: + return False, "held by " + ", ".join(f"release-blocker #{number}" for number in blockers) if force: return True, "started by hand with force" if paused: @@ -323,9 +356,8 @@ def line_intro(kind, version, previous): f"`horizon-server update --version {version}`.") -def notes(kind, version, prs, issues, beta=False, previous=None): - """(title, body) of a release; a game beta's say it is one. previous: the release before it, for a line's - first release.""" +def notes(kind, version, prs, issues, previous=None): + """(title, body) of a release. previous: the release before it, for a line's first release.""" shown = [pr for pr in prs if pr["paths"] and "skip-release" not in pr["labels"]] new = [pr for pr in shown if "feature" in pr["labels"]] fixed = [pr for pr in shown if "feature" not in pr["labels"]] @@ -346,17 +378,15 @@ def notes(kind, version, prs, issues, beta=False, previous=None): contributors = sorted({pr["author"] for pr in shown if pr["author"] and pr["author"] != REPO.split("/")[0]}) if contributors: parts.append("Thanks to " + ", ".join(f"@{name}" for name in contributors) + ".") - if beta and kind == "game": - parts.append(FOOTER["beta"]) parts.append(FOOTER[kind]) parts.append(DISCLAIMER) return title, "\n\n".join(parts) + "\n" -def gather(kind, head, releases=None, betas=True): - """The last published release of this kind (stable only when betas is False) and the pull requests merged - since, with their player-facing paths.""" - releases = published_releases(betas) if releases is None else releases +def gather(kind, head, releases=None): + """The last published release of this kind and the pull requests merged since, with their player-facing + paths.""" + releases = published_releases() if releases is None else releases last = last_release(releases, kind) base = last["tag_name"] if last else run("git", "rev-list", "--max-parents=0", head).split()[0] exporter_paths = exporters() @@ -387,18 +417,29 @@ def write_outputs(values): def main(): parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) - parser.add_argument("command", choices=["decide", "notes"]) + parser.add_argument("command", choices=["decide", "notes", "gate"]) parser.add_argument("--kind", choices=["game", "launcher"], required=True) parser.add_argument("--head", help="the commit to release (default: the newest commit on main whose checks passed)") parser.add_argument("--tag", help="notes: the release's tag") parser.add_argument("--force", action="store_true", help="release even without player-facing changes, before merges settle or while paused") parser.add_argument("--out", type=Path, help="notes: write title.txt and body.md here") parser.add_argument("--now", help="the time to decide at (ISO 8601; default now)") - parser.add_argument("--stable", action="store_true", - help="notes: for promoting a beta to stable, so since the last stable release, without the beta line") + parser.add_argument("--smoke", type=Path, help="gate: the release smoke test's report (smoke-test.json)") + parser.add_argument("--blockers", action="store_true", help="gate: also check that no release-blocker is open") args = parser.parse_args() now = dt.datetime.fromisoformat(args.now) if args.now else dt.datetime.now(dt.timezone.utc) + if args.command == "gate": + report = json.loads(args.smoke.read_text()) if args.smoke and args.smoke.exists() else None + problem = gate(report, open_blockers() if args.blockers else []) + line = f"Not publishing: {problem}" if problem else "Ready to publish: the smoke test passed" + ( + " and no release-blocker is open" if args.blockers else "") + print(json.dumps(report) if report else "", line, sep="\n") + if os.environ.get("GITHUB_STEP_SUMMARY"): + with open(os.environ["GITHUB_STEP_SUMMARY"], "a") as summary: + summary.write(f"### {line}\n") + return 1 if problem else 0 + head = args.head or green_main_head() if not head: result = {"release": False, "reason": "main's latest checks did not pass"} @@ -406,11 +447,11 @@ def main(): write_outputs({"release": "false", "reason": result["reason"]}) return 0 head = run("git", "rev-parse", head).strip() - last, prs = gather(args.kind, head, betas=not args.stable) + last, prs = gather(args.kind, head) if args.command == "notes": version = args.tag or "unreleased" previous = last["tag_name"] if last and last["tag_name"] != version else None - title, body = notes(args.kind, version, prs, known_issues(), beta=not args.stable, previous=previous) + title, body = notes(args.kind, version, prs, known_issues(), previous=previous) if args.out: args.out.mkdir(parents=True, exist_ok=True) (args.out / "title.txt").write_text(title + "\n") @@ -422,7 +463,9 @@ def main(): line = release_line() prefix = GAME_PREFIX if args.kind == "game" else LAUNCHER_PREFIX new_line = f"{line[0]}.{line[1]}" if line and first_on_line(tags, prefix, line) else None - release, reason = decision(args.kind, prs, now, os.environ.get("RELEASES_PAUSED") == "1", args.force, new_line) + blockers = open_blockers() if args.kind == "game" else [] + release, reason = decision(args.kind, prs, now, os.environ.get("RELEASES_PAUSED") == "1", args.force, new_line, + blockers) minor = any("feature" in pr["labels"] for pr in prs if pr["paths"]) tag = next_game_tag(tags, now.date(), line) if args.kind == "game" else next_launcher_tag(tags, minor, line) result = { diff --git a/website/build.mjs b/website/build.mjs index a47a40a79..781e838bb 100644 --- a/website/build.mjs +++ b/website/build.mjs @@ -55,7 +55,8 @@ try { if (batch.length < 100) break; } if (!res.ok) console.warn(`releases: ${res.status} from GitHub; the pages will load them themselves`); - // A day of betas can push the stable release out of the newest 100: it is GitHub's "latest" (betas never are). + // Pre-releases of the old Alpha channel could push the newest release out of the newest 100: it is GitHub's + // "latest" (pre-releases never are). if (res.ok && !releases.some((r) => isGame(r.tag_name) && !r.prerelease)) { const latest = await fetch(`https://api.github.com/repos/${REPO}/releases/latest`, { headers: githubHeaders, signal: AbortSignal.timeout(15000) }); if (latest.ok) releases.push(await latest.json()); @@ -121,8 +122,8 @@ const parityHeadline = matching === body.length ? "Every subsystem matches vanil // --- The releases ---------------------------------------------------------------------- -// Newest first by tag: every 0.2 tag is newer than the dated ones (src/versions.js). Alpha builds are GitHub -// pre-releases, Stable the others (docs/release-flow.md, "Channels"). +// Newest first by tag: every 0.2 tag is newer than the dated ones (src/versions.js). One channel since 2026-10-23 +// (docs/release-flow.md, "One channel"); the old Alpha channel's pre-releases stay listed. const byTag = (a, b) => newestFirst(a.tag_name, b.tag_name); const ours = releases.filter((r) => isGame(r.tag_name) || isLauncher(r.tag_name)).sort(byTag); const game = ours.filter((r) => isGame(r.tag_name)); diff --git a/website/docs/faq.md b/website/docs/faq.md index 6793c7e8f..666766c6b 100644 --- a/website/docs/faq.md +++ b/website/docs/faq.md @@ -60,7 +60,7 @@ Only if you say yes, and each kind has its own switch. Everything is off by defa ## How often does it update? -Often. Every change players can see is released automatically after it is merged, so new versions can come out several times a day. On the **Alpha** channel the launcher installs each one as soon as it is built; on **Stable**, the default, it installs a tested build about once a day. See [Install](/docs/install#updating). +Often. Every change players can see is released automatically after it is merged, so new versions can come out several times a day. Each one is tested before it goes out (the game has to start and draw a world), and the launcher installs it as soon as it is out. There is one version for everyone. See [Install](/docs/install#updating). ## What do the version numbers mean? diff --git a/website/docs/index.md b/website/docs/index.md index e146e520c..2b16c4db9 100644 --- a/website/docs/index.md +++ b/website/docs/index.md @@ -7,7 +7,7 @@ Horizon is Minecraft: Java Edition 26.3 rewritten in Rust. You install it with a - **A new game program, not a mod.** Horizon is written from scratch in Rust. It reads the same worlds, plays by the same rules and generates the same terrain as the Java game. - **Checked against vanilla.** {{HARNESSES}} automated tests ("harnesses") feed the same inputs to Horizon and to the real game, then compare the results. Today every subsystem matches in every harness that compares it. That is {{PERCENT}} of what the harnesses compare, which is not proof that nothing else differs. The [status table](/#parity) lists what is covered. - **Light and fast.** It starts quickly, uses little memory and draws efficiently. See [Performance](/docs/performance) for what is measured and what is not. -- **A few things vanilla doesn't have.** Its own settings, added to vanilla's menus the way mods add theirs, graphics presets, distant terrain, controller support, optional extras such as zoom, a minimap and dynamic lights (dynamic lights are in Alpha builds for now), an invite code to play your world with friends, and a server you can run without Java. +- **A few things vanilla doesn't have.** Its own settings, added to vanilla's menus the way mods add theirs, graphics presets, distant terrain, controller support, optional extras such as zoom, a minimap and dynamic lights, an invite code to play your world with friends, and a server you can run without Java. ## What Horizon is not diff --git a/website/docs/install.md b/website/docs/install.md index b6ecdff7d..c1a65f3ee 100644 --- a/website/docs/install.md +++ b/website/docs/install.md @@ -34,19 +34,14 @@ The game opens on vanilla's title screen. Your worlds, options and logs live in ## Updating -- **Horizon.** The launcher looks for a new release when it starts and every four hours while it is open. With **Install new versions automatically** on (the default), it installs one and moves you to it if you were on the newest. New releases come out many times a day, and old versions stay installed until you remove them on the **Versions** page. +- **Horizon.** The launcher looks for a new release when it starts and every 30 minutes while it is open. With **Install new versions automatically** on (the default), it installs one and moves you to it if you were on the newest. New releases come out many times a day, and old versions stay installed until you remove them on the **Versions** page. - **The launcher.** It updates itself from its own releases, then shows **Restart** in its footer. A launcher from before 0.2 (0.16 or older) updates twice in a row: first to 0.17, then to 0.2. Then it installs Horizon 0.2 like any update. -## Stable and Alpha +## One version for everyone -> **Next launcher release.** The Updates switch below is merged but not in a launcher release yet. Launcher 0.17 has a **Beta builds** switch in Settings instead, which installs every new build at once, as Alpha does. +There is one channel: every release is the version everyone gets. Before a release goes out, the game has to start and draw a world on our test machine, and nothing goes out while a serious problem is open. New releases come out many times a day, and you can install any older version by hand on the **Versions** page. -In **Settings**, **Updates** picks which builds install by themselves: - -- **Stable** (the default): a tested build about once a day. -- **Alpha**: every change as soon as it is built, several times a day. It can break. A launcher left open looks for new builds every 30 minutes, instead of every four hours. - -Every new release is an Alpha build first. Once a day (20:00 UTC, 21:00 in Paris in winter), the newest one that has been out for six hours, passed its smoke test and has no known blocker becomes Stable. Alpha builds come out many times a day, and Horizon as a whole is still in beta; Stable is the tested ones. The **Versions** page shows each version's channel, and you can install any of them by hand. +Launchers from before this change still show an **Updates** choice in Settings. Either option now gets every new release, and the choice goes away once the launcher has updated itself. ## The Versions page diff --git a/website/docs/multiplayer.md b/website/docs/multiplayer.md index b45ece0a9..e344c8fbc 100644 --- a/website/docs/multiplayer.md +++ b/website/docs/multiplayer.md @@ -20,7 +20,7 @@ There is no Horizon server in the middle. The code holds the host's addresses an Guests join with the name their game uses. There is no account check, so only share the code with people you trust. -In Alpha builds, guests can also use the host's workstations: the crafter, loom, stonecutter, grindstone, anvil, smithing table, enchanting table, beacon, lectern and cartography table. Stable gets this once it has passed. +Guests can also use the host's workstations: the crafter, loom, stonecutter, grindstone, anvil, smithing table, enchanting table, beacon, lectern and cartography table. ## Open to LAN diff --git a/website/docs/settings.md b/website/docs/settings.md index e1860cd4a..b3566ff2f 100644 --- a/website/docs/settings.md +++ b/website/docs/settings.md @@ -46,7 +46,7 @@ Shader packs are **experimental**. Put a pack's folder in the `shaderpacks` fold Horizon can run the full-screen effects of Java shader packs (OptiFine and Iris packs: colour grading, bloom, sky and fog effects). It does not run their geometry and shadow passes yet, so packs do not look as they do in Iris. Horizon also has its own shader pack format, in WGSL, with a `pack.toml` manifest, options and hot reload. It has full-screen passes too; stages that draw the world's terrain and shadows are planned. The details are in the [shader pack notes](https://github.com/ludovic111/horizon/blob/main/docs/shader-packs.md). -In Alpha builds, the **Shader Packs** page shows the full path of the `shaderpacks` folder with an **Open Folder** button, then one switch per pack. Turning a pack on picks it, and turning it off gives the vanilla picture. Packs tested so far: BSL, Complementary Reimagined, Steadfast and Sildur's Vibrant Extreme draw their full-screen passes; Bliss, Bloop, Noble, Photon and Rethinking Voxels do not load yet. To find packs, the launcher's **Mods** page installs shader packs from Modrinth (see [Mods and data packs](/docs/mods#finding-resource-packs-shader-packs-and-modpacks)). +The **Shader Packs** page shows the full path of the `shaderpacks` folder with an **Open Folder** button, then one switch per pack. Turning a pack on picks it, and turning it off gives the vanilla picture. Packs tested so far: BSL, Complementary Reimagined, Steadfast and Sildur's Vibrant Extreme draw their full-screen passes; Bliss, Bloop, Noble, Photon and Rethinking Voxels do not load yet. To find packs, the launcher's **Mods** page installs shader packs from Modrinth (see [Mods and data packs](/docs/mods#finding-resource-packs-shader-packs-and-modpacks)). ## Resource packs @@ -67,7 +67,7 @@ Horizon plays with PlayStation (DualShock 4 and DualSense), Xbox (360, One and S ## Extras -**Options, Horizon Settings, Extras** holds small, optional conveniences. Every one is off by default, so a fresh install behaves like vanilla: fullbright, zoom, a minimap, an FPS counter, a keystroke display, auto-reconnect, Mouse Tweaks-style inventory controls and inventory sorting. In Alpha builds there is one more, **dynamic lights**: a torch, lantern or other light source that you hold, that lies dropped as an item, or a burning mob, lights the blocks around it. It is client-side only, so other players do not see it. +**Options, Horizon Settings, Extras** holds small, optional conveniences. Every one is off by default, so a fresh install behaves like vanilla: fullbright, zoom, a minimap, an FPS counter, a keystroke display, auto-reconnect, Mouse Tweaks-style inventory controls and inventory sorting. One more is **dynamic lights**: a torch, lantern or other light source that you hold, that lies dropped as an item, or a burning mob, lights the blocks around it. It is client-side only, so other players do not see it. ## Where your settings are stored diff --git a/website/src/index.html b/website/src/index.html index 9c7c3ca3e..29d32d5ba 100644 --- a/website/src/index.html +++ b/website/src/index.html @@ -202,7 +202,7 @@

05Releases

Recently added

-

Alpha builds come out many times a day, as changes land; about once a day the tested ones become Stable, the launcher's default. The newest, and what they added:

+

New versions come out many times a day, as changes land, and the launcher installs each one once it has passed its test. The newest, and what they added:

{{RECENT}} diff --git a/website/src/main.js b/website/src/main.js index 4298cf1fd..7a0714d1b 100644 --- a/website/src/main.js +++ b/website/src/main.js @@ -17,9 +17,9 @@ const reducedMotion = matchMedia("(prefers-reduced-motion: reduce)").matches; const isLauncher = (r) => isLauncherTag(r.tag_name) && !r.prerelease; -// The newest 100 releases (GitHub's most per page). Game betas come out several times a day, so the stable game -// release and the newest launcher release can both be older than all of them. The stable game release is then -// GitHub's "latest" (betas and launcher releases are never marked latest); the launcher release is looked up by its +// The newest 100 releases (GitHub's most per page). The newest game release that is not a pre-release (the old +// Alpha channel's, docs/release-flow.md) and the newest launcher release can be older than all of them. The game +// release is then GitHub's "latest" (pre-releases and launcher releases never are); the launcher release is looked up by its // tag (the newest launcher@ tag with a published release, or launcher-v before 0.2: docs/versioning.md). async function loadReleases() { const list = (await get(`https://api.github.com/repos/${REPO}/releases?per_page=100`)).filter((r) => !r.draft).map(slim); @@ -83,7 +83,7 @@ async function setUpDownload() { // --- Recently added and the links from main --------------------------------- -// "Recently added": the newest game releases, Alpha and Stable. +// "Recently added": the newest game releases. async function setUpRecent() { const html = recentHtml((await releases).filter((r) => isGame(r.tag_name))); if (html) document.getElementById("recent-releases").innerHTML = html; diff --git a/website/src/releases.html b/website/src/releases.html index f0e4b2de4..989164653 100644 --- a/website/src/releases.html +++ b/website/src/releases.html @@ -4,7 +4,7 @@ Releases · Horizon - + @@ -30,11 +30,10 @@

ReleasesEvery version, newest first

What's new in Horizon

-

Each release lists what it adds and fixes, then its full notes and its downloads. Alpha builds come out many times a day as changes land and can break; about once a day the tested ones become Stable, what the launcher installs unless you pick Alpha in its Settings. The launcher has its own releases.

+

Each release lists what it adds and fixes, then its full notes and its downloads. New versions come out many times a day as changes land, and the launcher installs each one once it has passed its test. The launcher has its own releases.

- - +

{{RELEASE_COUNT}}

diff --git a/website/src/releases.js b/website/src/releases.js index 2e6a2e524..de59c8fef 100644 --- a/website/src/releases.js +++ b/website/src/releases.js @@ -1,13 +1,13 @@ // The release notes on the site: the home page's "Recently added" block and the Releases page (every release, // newest first). Shared by build.mjs (baked into the pages) and the pages' scripts (refreshed from GitHub). // Tags: the game's vX.Y.Z and the launcher's launcher@X.Y.Z from 0.2, vYYYY.MM.DD[.N] and launcher-vX.Y.Z before -// (src/versions.js). Alpha is a game pre-release, Stable a game release (docs/release-flow.md, "Channels"). +// (src/versions.js). One channel (docs/release-flow.md, "One channel"): a release is the game's or the launcher's. import { markdown, inline, escape } from "./markdown.js"; import { isLauncher, launcherVersion, newestFirst } from "./versions.js"; const REPO = "ludovic111/horizon"; -export const channel = (r) => (isLauncher(r.tag_name) ? "Launcher" : r.prerelease ? "Alpha" : "Stable"); +export const channel = (r) => (isLauncher(r.tag_name) ? "Launcher" : "Game"); const anchor = (r) => `release-${r.tag_name.replace(/[^\w.-]/g, "-")}`; // "Horizon 0.2.3: Cave culling, weather" (before 0.2, "Horizon 2026.10.13: …") reads as the version and its line. @@ -90,7 +90,7 @@ export function recentHtml(list, { rows = 5, picks = 3 } = {}) { }) .join(""); // What landed last: the "New" items of the newest releases with the highest pull request numbers (a release's - // notes list everything since the last Stable, oldest first). + // notes list everything since the release before it, oldest first). const pr = (item) => Math.max(0, ...[...item.matchAll(/#(\d+)/g)].map((m) => Number(m[1]))); const seen = new Set(); const picked = game diff --git a/website/src/style.css b/website/src/style.css index 8517a2f43..323cd6e83 100644 --- a/website/src/style.css +++ b/website/src/style.css @@ -630,14 +630,13 @@ code { border-radius: 0; background: var(--well); box-shadow: 0 0 0 1px var(--ou .page-main > .container > .lede { font-size: var(--text-body); } .page-main .disclaimer { margin-top: 48px; } -/* Channel and source badges: small square tags. */ +/* Release and source badges: small square tags. */ .badge { display: inline-flex; align-items: center; height: 22px; padding: 0 8px; margin-right: 10px; vertical-align: 2px; font: 500 12px / 1 var(--pixel); letter-spacing: 0.02em; text-transform: uppercase; white-space: nowrap; border: var(--block-outline) solid var(--outline); background: var(--well); color: var(--ink-soft); } -.badge-stable { background: color-mix(in srgb, var(--success) 22%, var(--panel)); color: var(--ink); } -.badge-alpha { background: color-mix(in srgb, var(--warn) 24%, var(--panel)); color: var(--ink); } +.badge-game { background: color-mix(in srgb, var(--success) 22%, var(--panel)); color: var(--ink); } .badge-launcher { background: color-mix(in srgb, var(--accent) 20%, var(--panel)); color: var(--ink); } .badge-us { background: color-mix(in srgb, var(--accent) 20%, var(--panel)); color: var(--ink); } .badge-players { background: color-mix(in srgb, var(--success) 22%, var(--panel)); color: var(--ink); } From 630f5210722059eb1d50d64f79e12fc21bac4f47 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 11:53:00 +0200 Subject: [PATCH 14/39] Launcher: one update channel, and the menu entry is never taken over Every release is now published as the release (one channel, Ludovic's decision of 2026-10-23), so the launcher drops its Stable/Alpha choice: the Updates row in Settings, `horizon-launcher update-channel` and its MCP tool, the channel on the Versions page, in `versions` and in the footer. It updates to the newest game release that is not a pre-release (GitHub's latest) and looks every 30 minutes. The pre-releases of the old Alpha channel stay listed and installable by hand, never as an update. Old `update_channel` / `beta_builds` keys in settings.json are ignored. On Linux a release build that starts no longer rewrites the applications-menu entry to point at itself. It writes the entry only when there is none, or when the program it starts (TryExec, else Exec) no longer exists; turning the Settings switch on or `desktop-entry` still points it at this launcher, and HORIZON_LAUNCHER_DESKTOP_EXEC is still honored. A test build started with the real HOME took over a working entry and then left it broken when its target/ was deleted (2026-10-23). Co-Authored-By: Claude Opus 5.5 --- docs/agent-control.md | 3 +- docs/launcher-commands.md | 10 - launcher/README.md | 30 +-- launcher/src/backend.rs | 233 +++++------------- launcher/src/backend/release_loading_tests.rs | 4 - launcher/src/cli.rs | 26 +- launcher/src/cli_tools.rs | 1 - launcher/src/desktop.rs | 147 ++++++++++- launcher/src/main.rs | 63 +---- website/docs/agents.md | 2 +- 10 files changed, 230 insertions(+), 289 deletions(-) diff --git a/docs/agent-control.md b/docs/agent-control.md index 5383cc03c..9d5db2597 100644 --- a/docs/agent-control.md +++ b/docs/agent-control.md @@ -71,7 +71,6 @@ passes on without checking it). | Dedicated server here: start, console, log, stop | Servers | yes | yes | yes | yes | `server`; MCP `server`, `server_command`, `server_log`, `server_stop` | | Hosted servers: list, create, set up, run a command, forget | Servers | yes | no | yes | yes | `servers ...`; `servers_forget` needs `confirm` | | Accounts: list, sign in, sign out, select, offline | Settings | yes | part | yes | yes | names and UUIDs only; sign-in is a device code; `account_offline` was missing | -| Update channel (Alpha, Stable) | Settings | yes | yes | yes | yes | | | Game folder, server folder, install new versions, opening animation | Settings | no | no | yes | yes | `settings [KEY VALUE]` | | Extra game arguments | Settings | yes | no | yes | yes | `game-args` | | Applications menu entry (Linux) | Settings | yes | no | yes | yes | `desktop-entry` | @@ -148,7 +147,7 @@ The switch is in three places: Settings > AI in the launcher, Horizon Settings i | Companion (#1068) | uses the same switch (see horizon-parity companion-notes.md) | Every other launcher command stays usable: versions, install, remove, prepare, play, server, -notes, links, account, update-channel, settings, game-args, logs, update, desktop-entry, data, +notes, links, account, settings, game-args, logs, update, desktop-entry, data, modpack, mods, servers, no-ai and benchmark. None of them drives the game for an agent. The launcher itself, packagers, the docs and players' scripts need them, and `no-ai` has to stay to turn the switch off. diff --git a/docs/launcher-commands.md b/docs/launcher-commands.md index 579659e68..9e09cd00b 100644 --- a/docs/launcher-commands.md +++ b/docs/launcher-commands.md @@ -130,16 +130,6 @@ Play offline, without an account. MCP: `account_offline`. -## `horizon-launcher update-channel` - -Show or set which Horizon releases install by themselves: stable (a tested build about once a day) or alpha (every release as soon as it is built; can break). - - horizon-launcher update-channel [CHANNEL] - -- `CHANNEL` (stable, alpha): Omit to show the current channel - -MCP: `update_channel`. - ## `horizon-launcher settings` Show the launcher's settings, or set one (the Settings page): selected-version TAG, minecraft-dir PATH, server-dir PATH ("default" resets a folder), install-new-versions on|off, opening-animation on|off. diff --git a/launcher/README.md b/launcher/README.md index e12ed2439..16ef197c7 100644 --- a/launcher/README.md +++ b/launcher/README.md @@ -78,16 +78,13 @@ page `servers.json` (the list), `servers//` (the servers created there) an `server-runtime/` (the programs and game files they run). - **Settings**: install new Horizon versions automatically (on unless you turn it off; the - selected version only moves to the new one if you were on the newest), which ones (Updates, - below), the opening animation, the game's extra command-line arguments, and the launcher's own - version and update state. -- **Updates**: **Stable** (the default) installs a tested build about once a day, the betas that - `promote.yml` makes stable; **Alpha** installs every release as soon as it is built, several - times a day, and can break (docs/release-flow.md, "Channels"). While it is open, the launcher - looks for new releases every 4 hours on Stable and every 30 minutes on Alpha. Kept as `update_channel` in - `settings.json`; a `beta_builds: true` from launchers before 0.2 reads as Alpha (so does a `nightly` from the channel's first name). - `horizon-launcher update-channel [stable|alpha]` shows or sets it. The **Versions** page - shows each release's channel. The launcher's own updates don't depend on it. + selected version only moves to the new one if you were on the newest), the opening animation, + the game's extra command-line arguments, and the launcher's own version and update state. +- **Updates**: there is one channel. Every release is published as the release, GitHub's latest, + once its smoke test passed (docs/release-flow.md), and the launcher installs it. The game + pre-releases of the old Alpha channel (v0.2.1 on) stay on the Versions page to install + by hand, but are never installed as an update. The `update_channel` and `beta_builds` keys + older launchers wrote in `settings.json` are ignored and dropped on the next save. - **Game arguments**: extra arguments added to the game's command line (for example `--server-thread`), one line where quotes group a word with spaces. They are appended after the launcher's own arguments, kept as `game_args` in `settings.json`, and apply the next time the @@ -102,7 +99,7 @@ their content overflows. Drag the thumb or click the track to navigate without a ## Updates -The launcher looks for a newer Horizon release when it starts and every four hours while it +The launcher looks for a newer Horizon release when it starts and every 30 minutes while it is open, and shows a small "available, Install" line on Play. It also updates itself from GitHub releases tagged `launcher@X.Y.Z` (Horizon's own are `vX.Y.Z`; @@ -111,8 +108,14 @@ every 0.2 tag counts as newer than all of those (docs/versioning.md). It downloa own binary, and offers "Restart" in the footer. Development builds and `HORIZON_LAUNCHER_NO_SELF_UPDATE=1` never replace themselves. +On Linux a release build that starts writes its applications-menu entry only when there is +none, or when the program the entry starts (its `TryExec`, else its `Exec`) no longer exists. +An entry pointing at another launcher that works is left alone, so a test or development build +never takes it over. Turning the Settings switch on, or `horizon-launcher desktop-entry`, +points the entry at this launcher whatever is there. + Linux packagers can set `HORIZON_LAUNCHER_DESKTOP_EXEC` to a wrapper's absolute path. -Menu entries created by Settings or `desktop-entry` then start that wrapper, which can +Menu entries the launcher writes then start that wrapper, which can set `HORIZON_LAUNCHER_NO_SELF_UPDATE=1` to keep launcher updates with the package manager. Launcher releases are automatic (docs/release-flow.md in the repository): when main changes the @@ -134,7 +137,6 @@ The same binary is a command-line tool; without a subcommand it opens the window horizon-launcher server [--dir ] [--version ] [--accept-eula] horizon-launcher notes [] patch notes as plain text horizon-launcher links the Discord, sponsor and website links - horizon-launcher update-channel [stable|alpha] show or set which releases install by themselves horizon-launcher game-args [] show or set the extra arguments the game starts with horizon-launcher update [--check] Horizon and the launcher horizon-launcher logs [--lines N] [--crash] the end of the game's log, or its newest crash report @@ -168,7 +170,7 @@ The same binary is a command-line tool; without a subcommand it opens the window horizon-launcher data benchmark upload a benchmark report, printed first `--json` prints JSON where there is something to report (`versions`, `install`, `notes`, -`next`, `links`, `update-channel`, `game-args`, `update --check`, `logs`, `account`, `benchmark`, `data`, `modpack`). Errors go to stderr with exit code 1. `server` runs in the terminal, +`next`, `links`, `game-args`, `update --check`, `logs`, `account`, `benchmark`, `data`, `modpack`). Errors go to stderr with exit code 1. `server` runs in the terminal, so lines you type reach its console. `HORIZON_LAUNCHER_HOME` replaces the data folder (for scripts and tests). diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index f88171c4e..87034c43f 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -37,8 +37,9 @@ pub struct Release { pub body: String, pub date: String, pub asset: Option<(String, u64)>, - /// A beta: a game release not promoted to stable yet (a GitHub pre-release, docs/release-flow.md). - pub beta: bool, + /// A GitHub pre-release: the Alpha builds from before every release went out as the release + /// (v0.2.1 on, docs/release-flow.md). Listed and installed by hand, never as an update. + pub prerelease: bool, } #[cfg(all(target_os = "linux", target_arch = "x86_64"))] @@ -264,7 +265,7 @@ fn parse_releases( .take(10) .collect(), asset: found, - beta: r["prerelease"].as_bool().unwrap_or(false), + prerelease: r["prerelease"].as_bool().unwrap_or(false), }); } out.sort_by(|a, b| compare_versions(&b.tag, &a.tag)); @@ -348,75 +349,14 @@ pub fn latest(releases: &[Release]) -> Option<&Release> { releases.iter().find(|r| r.asset.is_some()) } -/// The releases a launcher installs by itself: stable ones, and betas too on the Alpha channel. -pub fn channel(releases: &[Release], channel: UpdateChannel) -> Vec { - releases - .iter() - .filter(|r| channel.takes(r)) - .cloned() - .collect() +/// The releases a launcher installs by itself: every one that is published as a release, so +/// GitHub's latest when it comes out. The pre-releases of the old Alpha channel are left out. +pub fn installable_updates(releases: &[Release]) -> Vec { + releases.iter().filter(|r| !r.prerelease).cloned().collect() } -/// Which game releases install by themselves (Settings, "Updates"; docs/release-flow.md, "Channels"). -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] -#[serde(rename_all = "lowercase")] -pub enum UpdateChannel { - /// A tested build about once a day: the betas promote.yml makes stable. - #[default] - Stable, - /// Every release as soon as it is built, pre-releases included. It was called "nightly" - /// while the channels were new: that name is still read. - #[serde(alias = "nightly")] - Alpha, -} - -impl UpdateChannel { - pub fn name(self) -> &'static str { - match self { - UpdateChannel::Stable => "stable", - UpdateChannel::Alpha => "alpha", - } - } - - /// Its name in Settings and on the Versions page. - pub fn label(self) -> &'static str { - match self { - UpdateChannel::Stable => "Stable", - UpdateChannel::Alpha => "Alpha", - } - } - - pub fn parse(name: &str) -> Option { - match name { - "stable" => Some(UpdateChannel::Stable), - "alpha" | "nightly" => Some(UpdateChannel::Alpha), - _ => None, - } - } - - /// Whether this channel installs `release`: betas only reach Alpha. - pub fn takes(self, release: &Release) -> bool { - self == UpdateChannel::Alpha || !release.beta - } - - /// How often a launcher left open looks for new releases: Alpha's come many times a day, - /// Stable's once. - pub fn check_every(self) -> std::time::Duration { - std::time::Duration::from_secs(match self { - UpdateChannel::Stable => 4 * 3600, - UpdateChannel::Alpha => 30 * 60, - }) - } - - /// The channel a release is on when it is listed: betas are Alpha's only. - pub fn of(release: &Release) -> Self { - if release.beta { - UpdateChannel::Alpha - } else { - UpdateChannel::Stable - } - } -} +/// How often a launcher left open looks for new releases: they come out several times a day. +pub const CHECK_EVERY: std::time::Duration = std::time::Duration::from_secs(30 * 60); /// The community, sponsor and website links, from `launcher/links.json` on main, so they can change /// without a launcher release. An empty address hides its button. @@ -1745,17 +1685,10 @@ fn launcher_update_from(releases: &[Release], own: &str) -> Option { pub fn releases_and_updates( on_releases: impl FnOnce(Result, String>), ) -> Result { - releases_and_updates_with( - Settings::load().update_channel, - release_list, - installed, - launcher_releases_in, - on_releases, - ) + releases_and_updates_with(release_list, installed, launcher_releases_in, on_releases) } fn releases_and_updates_with( - update_channel: UpdateChannel, load: impl FnOnce() -> Result, installed: impl FnOnce() -> Vec, launchers: impl FnOnce(&serde_json::Value) -> Vec, @@ -1769,7 +1702,7 @@ fn releases_and_updates_with( } }; let releases = parse_releases(&list, ASSET, is_game_tag); - let horizon = horizon_update(&channel(&releases, update_channel), &installed()); + let horizon = horizon_update(&installable_updates(&releases), &installed()); on_releases(Ok(releases)); Ok(Updates { horizon, @@ -1791,15 +1724,14 @@ pub fn select_after_update(selected: Option<&str>, installed_before: &[String]) } } -/// Installs `tag` (or with "latest" the newest release of the player's channel), then updates the selection as +/// Installs `tag` (or with "latest" the newest release, pre-releases left out), then updates the selection as /// `select_after_update` says when `as_update`, or always selects it otherwise. pub fn install_tag(tag: &str, as_update: bool, progress: impl Fn(f32)) -> Result { let releases = releases()?; let release = if tag == "latest" { - let update_channel = Settings::load().update_channel; releases .iter() - .find(|r| r.asset.is_some() && update_channel.takes(r)) + .find(|r| r.asset.is_some() && !r.prerelease) .ok_or("No release has a build for this platform yet.")? } else { releases @@ -2262,10 +2194,6 @@ pub struct Settings { /// default. Appended after the launcher's own arguments, so they can add to or override them. #[serde(default, skip_serializing_if = "String::is_empty")] pub game_args: String, - /// Which releases install by themselves: Stable by default. Launchers before 0.2 had a "Beta - /// builds" switch instead; [`Settings::parse`] reads its `beta_builds: true` as Alpha. - #[serde(default)] - pub update_channel: UpdateChannel, /// Dark, Light, or the system's: the launcher's look. Dark by default. #[serde(default)] pub appearance: Appearance, @@ -2284,20 +2212,7 @@ impl Settings { return Self::default(); }; if let Some(fields) = value.as_object_mut() { - // The switch from before 0.2: on meant every release at once, the Alpha channel. - if fields.remove("beta_builds") == Some(serde_json::Value::Bool(true)) { - fields - .entry("update_channel") - .or_insert_with(|| UpdateChannel::Alpha.name().into()); - } - // A channel this launcher doesn't know is Stable, not a reason to lose every setting. - if fields - .get("update_channel") - .is_some_and(|c| c.as_str().and_then(UpdateChannel::parse).is_none()) - { - fields.remove("update_channel"); - } - // The same for an appearance from a newer launcher. + // An appearance from a newer launcher is Dark, not a reason to lose every setting. if fields .get("appearance") .is_some_and(|a| serde_json::from_value::(a.clone()).is_err()) @@ -2517,41 +2432,35 @@ mod tests { assert_eq!(offered(&["v2026.10.15.1", "v2026.10.14"]), None); } - /// Betas (GitHub pre-releases) install by themselves only on the Alpha channel; Stable waits - /// for promote.yml's daily promotion (docs/release-flow.md, "Channels"). + /// Every release is published as the release, and launchers update to it. The pre-releases + /// of the old Alpha channel (v0.2.1 on) are only installed by hand: a launcher that + /// had one installed waits for the next release instead of going back. #[test] - fn updates_follow_the_stable_channel_unless_alpha_is_picked() { + fn updates_follow_every_release_and_leave_the_old_alpha_ones_out() { let asset = ASSET.unwrap_or("horizon-linux-x86_64.tar.gz"); let list = serde_json::json!([ - {"tag_name": "v2026.10.19.02", "prerelease": true, "assets": [{"name": asset, "browser_download_url": "b", "size": 1}]}, - {"tag_name": "v2026.10.19.01", "assets": [{"name": asset, "browser_download_url": "s", "size": 1}]}, + {"tag_name": "v0.2.13", "assets": [{"name": asset, "browser_download_url": "n", "size": 1}]}, + {"tag_name": "v0.2.12", "prerelease": true, "assets": [{"name": asset, "browser_download_url": "a", "size": 1}]}, + {"tag_name": "v0.2.0", "assets": [{"name": asset, "browser_download_url": "s", "size": 1}]}, ]); let releases = parse_releases(&list, Some(asset), is_game_tag); assert_eq!( - releases.iter().map(|r| r.beta).collect::>(), - [true, false] + releases.iter().map(|r| r.prerelease).collect::>(), + [false, true, false] ); - let offered = |alpha: bool, installed: &[&str]| { + let offered = |releases: &[Release], installed: &[&str]| { let installed: Vec = installed.iter().map(|t| t.to_string()).collect(); - let on = if alpha { - UpdateChannel::Alpha - } else { - UpdateChannel::Stable - }; - horizon_update(&channel(&releases, on), &installed).map(|r| r.tag) + horizon_update(&installable_updates(releases), &installed).map(|r| r.tag) }; - assert_eq!(offered(false, &[]), Some("v2026.10.19.01".into())); - assert_eq!( - offered(true, &["v2026.10.19.01"]), - Some("v2026.10.19.02".into()) - ); - assert_eq!(offered(false, &["v2026.10.19.01"]), None); - // A beta installed earlier is never "updated" to an older stable build. - assert_eq!(offered(false, &["v2026.10.19.02"]), None); - assert_eq!( - releases.iter().map(UpdateChannel::of).collect::>(), - [UpdateChannel::Alpha, UpdateChannel::Stable] - ); + assert_eq!(offered(&releases, &[]), Some("v0.2.13".into())); + assert_eq!(offered(&releases, &["v0.2.0"]), Some("v0.2.13".into())); + assert_eq!(offered(&releases, &["v0.2.12"]), Some("v0.2.13".into())); + assert_eq!(offered(&releases, &["v0.2.13"]), None); + // Before the first release of the single channel: the old Alpha one stays where it is. + let before = &releases[1..]; + assert_eq!(offered(before, &["v0.2.0"]), None); + assert_eq!(offered(before, &["v0.2.12", "v0.2.0"]), None); + assert_eq!(CHECK_EVERY.as_secs(), 30 * 60); } #[test] @@ -2573,48 +2482,20 @@ mod tests { } #[test] - fn the_update_channel_is_stored_by_name_and_the_old_beta_switch_reads_as_alpha() { - let read = |text: &str| Settings::parse(text).update_channel; - assert_eq!(read(""), UpdateChannel::Stable); - assert_eq!(read("{}"), UpdateChannel::Stable); - assert_eq!( - read(r#"{"update_channel": "stable"}"#), - UpdateChannel::Stable - ); - assert_eq!(read(r#"{"update_channel": "alpha"}"#), UpdateChannel::Alpha); - // The channel's first name, from launchers built while it was called Nightly. - assert_eq!( - read(r#"{"update_channel": "nightly"}"#), - UpdateChannel::Alpha - ); - assert_eq!(UpdateChannel::parse("nightly"), Some(UpdateChannel::Alpha)); - // Launchers before 0.2: "Beta builds" on is Alpha, off is Stable. - assert_eq!(read(r#"{"beta_builds": true}"#), UpdateChannel::Alpha); - assert_eq!(read(r#"{"beta_builds": false}"#), UpdateChannel::Stable); - // Once a channel is picked, it wins over a switch left behind. - let both = r#"{"beta_builds": true, "update_channel": "stable"}"#; - assert_eq!(read(both), UpdateChannel::Stable); - // An unknown channel is Stable, and the rest of the settings survive it. - let odd = - Settings::parse(r#"{"update_channel": "beta", "game_args": "--x", "beta_builds": 1}"#); - assert_eq!(odd.update_channel, UpdateChannel::Stable); - assert_eq!(odd.game_args, "--x"); - // Saved as a name, without the old switch. - let mut settings = Settings::parse(r#"{"selected": "v0.2.0", "beta_builds": true}"#); - let saved = serde_json::to_value(&settings).unwrap(); - assert_eq!(saved["update_channel"], "alpha"); - assert!(saved.get("beta_builds").is_none()); - assert_eq!(saved["selected"], "v0.2.0"); - settings.update_channel = UpdateChannel::Stable; - let text = serde_json::to_string(&settings).unwrap(); - assert_eq!(read(&text), UpdateChannel::Stable); - for c in [UpdateChannel::Stable, UpdateChannel::Alpha] { - assert_eq!(UpdateChannel::parse(c.name()), Some(c)); + fn the_old_update_channel_settings_are_dropped_and_the_rest_kept() { + for old in [ + r#"{"update_channel": "alpha", "game_args": "--x", "selected": "v0.2.0"}"#, + r#"{"update_channel": "stable", "game_args": "--x", "selected": "v0.2.0"}"#, + r#"{"beta_builds": true, "game_args": "--x", "selected": "v0.2.0"}"#, + r#"{"beta_builds": false, "update_channel": "nightly", "game_args": "--x", "selected": "v0.2.0"}"#, + ] { + let settings = Settings::parse(old); + assert_eq!(settings.game_args, "--x", "{old}"); + assert_eq!(settings.selected.as_deref(), Some("v0.2.0"), "{old}"); + let saved = serde_json::to_value(&settings).unwrap(); + assert!(saved.get("update_channel").is_none(), "{old}"); + assert!(saved.get("beta_builds").is_none(), "{old}"); } - // An open launcher looks again every 4 hours on Stable, every 30 minutes on Alpha. - let minutes = |c: UpdateChannel| c.check_every().as_secs() / 60; - assert_eq!(minutes(UpdateChannel::Stable), 240); - assert_eq!(minutes(UpdateChannel::Alpha), 30); } #[test] @@ -2635,7 +2516,7 @@ mod tests { body: String::new(), date: String::new(), asset: Some(("u".into(), 1)), - beta: false, + prerelease: false, }; assert!(launcher_update(&[r(own_tag())]).is_none()); assert_eq!(launcher_version(&own_tag()), Some(VERSION)); @@ -2675,7 +2556,7 @@ mod tests { body: String::new(), date: String::new(), asset: Some(("u".into(), 1)), - beta: false, + prerelease: false, }; let mut list = vec![ r("launcher-v0.16.0"), @@ -2744,10 +2625,14 @@ mod tests { ]); let releases = parse_releases(&list, Some(asset), is_game_tag); let installed = ["v2026.10.19.15".to_string(), "v2026.10.12".to_string()]; - let offered = |on| horizon_update(&channel(&releases, on), &installed).map(|r| r.tag); - // A beta 0.2.0 reaches launchers on Alpha; Stable ones wait for its promotion. - assert_eq!(offered(UpdateChannel::Alpha), Some("v0.2.0".into())); - assert_eq!(offered(UpdateChannel::Stable), None); + // A pre-release 0.2.0 is not an update; once published as the release, it is one. + let offered = |releases: &[Release]| { + horizon_update(&installable_updates(releases), &installed).map(|r| r.tag) + }; + assert_eq!(offered(&releases), None); + let mut released = releases.clone(); + released[0].prerelease = false; + assert_eq!(offered(&released), Some("v0.2.0".into())); let mut sorted = vec![ "v2026.10.19.15".to_string(), "v0.2.1".into(), @@ -2837,7 +2722,7 @@ mod tests { body: String::new(), date: String::new(), asset: Some(("u".into(), 1)), - beta: false, + prerelease: false, }; let releases = [r("v2026.10.13"), r("v2026.10.12")]; assert_eq!( diff --git a/launcher/src/backend/release_loading_tests.rs b/launcher/src/backend/release_loading_tests.rs index 4aaa616a2..d7f839bde 100644 --- a/launcher/src/backend/release_loading_tests.rs +++ b/launcher/src/backend/release_loading_tests.rs @@ -45,7 +45,6 @@ fn baseline(load: impl Fn() -> Result) -> (Vec Result) -> (Vec, Updates) { let mut releases = None; let updates = releases_and_updates_with( - UpdateChannel::Stable, load, installed_fixture, |list| parse_releases(list, LAUNCHER_ASSET, is_launcher_tag), @@ -82,7 +81,6 @@ fn errors_reach_releases_and_updates_without_fallback() { ] { let mut published = None; let result = releases_and_updates_with( - UpdateChannel::Stable, || Err(error.into()), || panic!("no installed scan after failure"), |_| panic!("no fallback after failure"), @@ -97,7 +95,6 @@ fn errors_reach_releases_and_updates_without_fallback() { fn publish_games_before_older_launcher_fallback() { let events = RefCell::new(Vec::new()); let result = releases_and_updates_with( - UpdateChannel::Stable, || Ok(serde_json::json!([{"tag_name": "v2026.10.14", "assets": []}])), Vec::new, |list| { @@ -132,7 +129,6 @@ fn failed_launcher_fallback_keeps_game_result_and_checks_stay_fresh() { for day in [14, 15] { let mut published = None; let updates = releases_and_updates_with( - UpdateChannel::Stable, || { requests.fetch_add(1, Ordering::Relaxed); Ok(serde_json::json!([{"tag_name": format!("v2026.10.{day}")}])) diff --git a/launcher/src/cli.rs b/launcher/src/cli.rs index 732aa872b..9d8e51d93 100644 --- a/launcher/src/cli.rs +++ b/launcher/src/cli.rs @@ -287,13 +287,6 @@ enum Cmd { #[command(subcommand)] action: Option, }, - /// Show or set which Horizon releases install by themselves: stable (a tested build about once - /// a day) or alpha (every release as soon as it is built; can break) - UpdateChannel { - /// Omit to show the current channel - #[arg(value_parser = ["stable", "alpha"])] - channel: Option, - }, /// Show the launcher's settings, or set one (the Settings page): selected-version TAG, /// minecraft-dir PATH, server-dir PATH ("default" resets a folder), install-new-versions /// on|off, opening-animation on|off @@ -435,7 +428,6 @@ fn settings_json(s: &backend::Settings) -> serde_json::Value { "server_dir": backend::server_dir(None), "install_new_versions": s.install_new_versions.unwrap_or(true), "opening_animation": !s.skip_opening, - "update_channel": s.update_channel.name(), "game_args": s.game_args, "desktop_entry": s.desktop_entry.unwrap_or(true), "no_ai": backend::no_ai(), @@ -504,7 +496,7 @@ fn attached(mut cmd: Command) -> Result<(), String> { fn release_json(r: &Release) -> serde_json::Value { serde_json::json!({ - "tag": r.tag, "name": r.name, "date": r.date, "channel": backend::UpdateChannel::of(r).name(), + "tag": r.tag, "name": r.name, "date": r.date, "prerelease": r.prerelease, "available_here": r.asset.is_some(), "installed": backend::installed().contains(&r.tag), }) } @@ -555,8 +547,7 @@ fn execute(command: Cmd, as_json: bool) -> Result<(), String> { (false, true) => "", (false, false) => "no build for this platform", }; - let channel = backend::UpdateChannel::of(r).name(); - println!("{:<15} {} {:<7} {}", r.tag, r.date, channel, state); + println!("{:<15} {} {}", r.tag, r.date, state); } } Cmd::Install { tag } => { @@ -733,19 +724,6 @@ fn execute(command: Cmd, as_json: bool) -> Result<(), String> { } } } - Cmd::UpdateChannel { channel } => { - let mut settings = backend::Settings::load(); - if let Some(channel) = channel.as_deref().and_then(backend::UpdateChannel::parse) { - settings.update_channel = channel; - settings.save(); - } - let channel = settings.update_channel.name(); - if as_json { - json(serde_json::json!({ "update_channel": channel })); - } else { - println!("Update channel: {channel}"); - } - } Cmd::GameArgs { args } => { let mut settings = backend::Settings::load(); if let Some(line) = args { diff --git a/launcher/src/cli_tools.rs b/launcher/src/cli_tools.rs index 307f17dba..7653311ac 100644 --- a/launcher/src/cli_tools.rs +++ b/launcher/src/cli_tools.rs @@ -366,7 +366,6 @@ mod tests { "prepare", "notes", "update", - "update_channel", "game_args", "settings", "logs", diff --git a/launcher/src/desktop.rs b/launcher/src/desktop.rs index 1c9fe931e..281850cb6 100644 --- a/launcher/src/desktop.rs +++ b/launcher/src/desktop.rs @@ -2,8 +2,12 @@ //! `applications/horizon-launcher.desktop` (pointing at wherever the binary is now) and Horizon's //! icon into the `hicolor` theme, under `$XDG_DATA_HOME` (`~/.local/share` by default), so no root //! is needed; `horizon.desktop` (hidden from menus) gives the game's window its icon on Wayland. -//! A release build does this when it starts, and again whenever the binary has moved; the -//! Settings switch and `horizon-launcher desktop-entry --remove` take it all away again. +//! A release build writes it when it starts only if there is no entry yet or the one there starts +//! a program that is gone ([`repair`]), so a development or test build never takes over a working +//! entry. Turning the Settings switch on (or `horizon-launcher desktop-entry`) points it at this +//! binary whatever is there ([`install`]); the switch off and `desktop-entry --remove` take it all +//! away again. +use std::ffi::OsStr; use std::fs; use std::io; use std::path::{Path, PathBuf}; @@ -153,15 +157,85 @@ pub fn remove_in(data: &Path) -> io::Result<()> { Ok(()) } -/// Registers the launcher, using a distributor's wrapper when one is configured. -/// Both GUI registration (including the settings switch) and the CLI use this path. +/// The program an entry starts: its `TryExec`, else the first word of its `Exec` (unquoted as +/// [`quote`] writes it). None when the entry names neither. +fn entry_target(entry: &str) -> Option { + let mut in_main = false; + let (mut exec, mut try_exec) = (None, None); + for line in entry.lines().map(str::trim) { + if line.starts_with('[') { + in_main = line == "[Desktop Entry]"; + } else if let Some((key, value)) = line.split_once('=').filter(|_| in_main) { + match key.trim() { + "TryExec" if try_exec.is_none() => try_exec = Some(value.trim()), + "Exec" if exec.is_none() => exec = Some(value.trim()), + _ => {} + } + } + } + if let Some(program) = try_exec.filter(|p| !p.is_empty()) { + return Some(program.to_string()); + } + let exec = exec?; + let program = match exec.strip_prefix('"') { + Some(rest) => { + let (mut out, mut chars) = (String::new(), rest.chars()); + while let Some(c) = chars.next() { + match c { + '"' => break, + '\\' => out.extend(chars.next()), + c => out.push(c), + } + } + out + } + None => exec.split_whitespace().next()?.to_string(), + }; + (!program.is_empty()).then_some(program) +} + +/// Whether an entry's program is there: a path that exists, or a bare name found on `path` +/// (`$PATH`), as menus look `TryExec` up. +fn target_exists(program: &str, path: Option<&OsStr>) -> bool { + if program.contains('/') { + return Path::new(program).exists(); + } + path.is_some_and(|dirs| std::env::split_paths(dirs).any(|dir| dir.join(program).is_file())) +} + +/// Whether the launcher may write its entry by itself: there is none under `data`, or the one +/// there starts a program that no longer exists. An entry pointing at another launcher that +/// still works is left alone. +pub fn needs_repair_in(data: &Path, path: Option<&OsStr>) -> bool { + match fs::read_to_string(data.join("applications").join(LAUNCHER_ENTRY)) { + Ok(entry) => !entry_target(&entry).is_some_and(|p| target_exists(&p, path)), + Err(e) => e.kind() == io::ErrorKind::NotFound, + } +} + +/// What the entry starts: a distributor's wrapper (`HORIZON_LAUNCHER_DESKTOP_EXEC`) when one is +/// configured, else the binary at `exe`. +fn target(exe: &Path) -> PathBuf { + std::env::var_os("HORIZON_LAUNCHER_DESKTOP_EXEC") + .filter(|path| !path.is_empty()) + .map_or_else(|| exe.to_path_buf(), PathBuf::from) +} + +/// Registers the launcher whatever entry is there: for the player turning it on (the Settings +/// switch, `horizon-launcher desktop-entry`). pub fn install(exe: &Path) -> Result { let data = data_home().ok_or("No home folder to put the menu entry in.")?; - let wrapper = std::env::var_os("HORIZON_LAUNCHER_DESKTOP_EXEC") - .filter(|path| !path.is_empty()) - .map(PathBuf::from); - install_in(&data, wrapper.as_deref().unwrap_or(exe)) - .map_err(|e| format!("{}: {e}", data.display())) + install_in(&data, &target(exe)).map_err(|e| format!("{}: {e}", data.display())) +} + +/// At start: registers the launcher only when [`needs_repair_in`] says so. Returns whether it +/// wrote anything. +pub fn repair(exe: &Path) -> Result { + let data = data_home().ok_or("No home folder to put the menu entry in.")?; + if !needs_repair_in(&data, std::env::var_os("PATH").as_deref()) { + return Ok(false); + } + install_in(&data, &target(exe)).map_err(|e| format!("{}: {e}", data.display())) } pub fn remove() -> Result<(), String> { @@ -206,4 +280,59 @@ mod tests { remove_in(&data).unwrap(); let _ = fs::remove_dir_all(&data); } + + #[test] + fn the_target_is_try_exec_else_the_first_word_of_exec() { + let ours = launcher_entry(Path::new("/opt/a b/horizon \"x\"/horizon-launcher")); + assert_eq!( + entry_target(&ours).as_deref(), + Some("/opt/a b/horizon \"x\"/horizon-launcher") + ); + let without_try = ours.replace("TryExec=", "X-Old="); + assert_eq!( + entry_target(&without_try).as_deref(), + Some("/opt/a b/horizon \"x\"/horizon-launcher") + ); + let wrapper = "[Desktop Entry]\nExec=flatpak run dev.horizon.Launcher %U\n"; + assert_eq!(entry_target(wrapper).as_deref(), Some("flatpak")); + // Only the main group counts: an action's Exec is not the launcher. + let action = "[Desktop Action play]\nExec=/a\n[Desktop Entry]\nExec=/b --x\n"; + assert_eq!(entry_target(action).as_deref(), Some("/b")); + assert_eq!(entry_target("[Desktop Entry]\nName=Horizon\n"), None); + assert_eq!(entry_target("[Desktop Entry]\nExec=\nTryExec=\n"), None); + } + + /// A build that starts never takes over an entry that works (a test or development build + /// started on a player's machine, 2026-10-23); it writes one only when there is none or the + /// program it starts is gone. + #[test] + fn a_start_writes_the_entry_only_when_none_works() { + let root = + std::env::temp_dir().join(format!("horizon-desktop-repair-{}", std::process::id())); + let _ = fs::remove_dir_all(&root); + let (data, bin) = (root.join("data"), root.join("bin")); + fs::create_dir_all(&bin).unwrap(); + let installed = bin.join("horizon-launcher"); + fs::write(&installed, b"").unwrap(); + let entry = data.join("applications").join(LAUNCHER_ENTRY); + let path = Some(bin.as_os_str()); + + // No entry: write one. + assert!(needs_repair_in(&data, path)); + install_in(&data, &installed).unwrap(); + // Ours works: a build elsewhere leaves it alone. + assert!(!needs_repair_in(&data, path)); + // So does one another launcher wrote, found on PATH or by its path. + fs::write(&entry, "[Desktop Entry]\nExec=horizon-launcher %U\n").unwrap(); + assert!(!needs_repair_in(&data, path)); + assert!(needs_repair_in(&data, None)); + // The program is gone (a deleted target/ folder, a moved binary): write it again. + install_in(&data, &installed).unwrap(); + fs::remove_file(&installed).unwrap(); + assert!(needs_repair_in(&data, path)); + // An entry that starts nothing is broken too. + fs::write(&entry, "[Desktop Entry]\nName=Horizon\n").unwrap(); + assert!(needs_repair_in(&data, path)); + let _ = fs::remove_dir_all(&root); + } } diff --git a/launcher/src/main.rs b/launcher/src/main.rs index 128bf1bcf..18e8295b1 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -238,15 +238,16 @@ impl Launcher { if settings.minecraft_dir.is_none() { settings.minecraft_dir = backend::default_minecraft_dir(); } - // Release builds on Linux keep their entry in the applications menu up to date (a moved - // binary changes it); `HORIZON_LAUNCHER_DESKTOP_ENTRY=1` does it in a development build. + // Release builds on Linux put their entry back in the applications menu when there is + // none or it starts a program that is gone (a moved or deleted binary), never over one + // that works; `HORIZON_LAUNCHER_DESKTOP_ENTRY=1` does it in a development build. if cfg!(target_os = "linux") && settings.desktop_entry() && (!cfg!(debug_assertions) || std::env::var_os("HORIZON_LAUNCHER_DESKTOP_ENTRY").is_some()) { std::thread::spawn(|| { - let _ = horizon_launcher::desktop::install(backend::exe_path()); + let _ = horizon_launcher::desktop::repair(backend::exe_path()); }); } let installed = backend::installed(); @@ -658,8 +659,10 @@ impl Launcher { changed = true; } } - let every = self.settings.update_channel.check_every(); - if self.checked.is_some_and(|at| at.elapsed() > every) { + if self + .checked + .is_some_and(|at| at.elapsed() > backend::CHECK_EVERY) + { self.check_updates(); } if changed { @@ -1698,14 +1701,10 @@ impl Launcher { )); } foot = foot.child( - caps(format!( - "Launcher {} · {}", - backend::VERSION, - self.settings.update_channel.label() - )) - .px(px(8.)) - .pt(px(6.)) - .text_color(t.muted), + caps(format!("Launcher {}", backend::VERSION)) + .px(px(8.)) + .pt(px(6.)) + .text_color(t.muted), ); div() .id("sidebar") @@ -2339,25 +2338,6 @@ impl Launcher { cx.notify(); }), ); - use backend::UpdateChannel::{Alpha, Stable}; - let this = cx.entity().downgrade(); - let update_channel = segments( - "update-channel", - &[(Stable, Stable.label()), (Alpha, Alpha.label())], - self.settings.update_channel, - &t, - move |picked, _, cx| { - let _ = this.update(cx, |this, cx| { - if this.settings.update_channel != picked { - this.settings.update_channel = picked; - this.settings.save(); - // What counts as new changed: check again (and install it if that is on). - this.check_updates(); - } - cx.notify(); - }); - }, - ); let opening = switch("opening", !self.settings.skip_opening, &t).on_click(cx.listener( |this, _: &ClickEvent, _, cx| { this.settings.skip_opening = !this.settings.skip_opening; @@ -2487,22 +2467,6 @@ impl Launcher { Some(toggle.into_any_element()), &t, )) - .child(index_row( - n + 2, - "Updates", - Some( - div() - .flex() - .flex_col() - .child("Stable: a tested build about once a day.") - .child( - "Alpha: every change as soon as it is built, several times a day; can break.", - ) - .into_any_element(), - ), - Some(update_channel.into_any_element()), - &t, - )) .child(group("Launcher", "monitor")) .child(index_row( n + 3, @@ -2955,8 +2919,7 @@ impl Launcher { let tag = release.tag.clone(); let installed = self.installed.contains(&tag); let selected = self.selected() == Some(&tag); - let channel = backend::UpdateChannel::of(&release).label(); - let mut meta = vec![channel.to_string(), release.date.clone()]; + let mut meta = vec![release.date.clone()]; if let Some((_, size)) = release.asset { meta.push(megabytes(size)); } diff --git a/website/docs/agents.md b/website/docs/agents.md index 32ba58362..02042be4d 100644 --- a/website/docs/agents.md +++ b/website/docs/agents.md @@ -9,7 +9,7 @@ Everything you can do with the mouse and keyboard, through commands instead of c - **In the launcher:** - install, remove and prepare versions; - start the game, alone or with a world, or a server; - - your settings, the update channel and the extra game arguments; + - your settings and the extra game arguments; - the mod browser (mods, resource packs, shader packs, modpacks), imported modpacks and hosted servers; - your accounts (names only), logs and crash reports, patch notes, the data collection settings and the benchmark. - **In the game:** From 9fcf91e4bce67dce335874e97fa8eb3fbc05dfec Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 11:09:56 +0200 Subject: [PATCH 15/39] Metal: occlusion culling of solid sections, the depth test in the shader GPU occlusion culling ran on Vulkan only: its test needs early fragment tests, which naga's Metal backend does not emit. On Metal the boxes are tested in a pass of their own: phase 1's depth is bound read-only as the attachment and as a texture, and the fragment shader compares its own depth with it (reversed Z, greater or equal), as the fixed-function test would. HORIZON_GPU_CULL=shader takes this path on Vulkan too, for checks. Frames drawn flat (the default on Apple GPUs at high render distances) still skip culling here. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 64 +++++-- crates/horizon-client/src/gfx/cull.rs | 213 ++++++++++++++++++++---- crates/horizon-client/src/gfx/cull.wgsl | 28 +++- 3 files changed, 246 insertions(+), 59 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index e379ace7c..183f7823e 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -316,7 +316,8 @@ pub struct Renderer { flat: Option, /// `flat` draws this frame's sections (off: the per-section draws). flat_on: bool, - /// Occlusion culling of solid section draws, where multi-draw is native. + /// Occlusion culling of solid section draws, where the GPU's instance + /// counts reach a multi-draw's entries (Vulkan, Metal). cull: Option, cull_on: bool, sky: wgpu::RenderPipeline, @@ -1408,7 +1409,6 @@ impl Renderer { usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); - let depth = Self::depth(&device, &config); let (atlas_pixels, atlas_width, atlas_height) = hud_atlas; let atlas = device.create_texture_with_data( &queue, @@ -2112,14 +2112,22 @@ impl Renderer { .features() .contains(wgpu::Features::INDIRECT_FIRST_INSTANCE); // HORIZON_GPU_CULL=0 turns occlusion culling off, `all` culls every - // frame however little it hides (A/B checks). + // frame however little it hides, `shader` tests in the shader as on + // Metal (A/B checks). let cull_env = std::env::var("HORIZON_GPU_CULL"); - let cull = (cull_env.as_deref() != Ok("0") && cull::Cull::supported(&adapter, &device)) - .then(|| { - let mut cull = cull::Cull::new(&device, config.format.remove_srgb_suffix()); + let cull = cull::Cull::supported(&adapter, &device) + .filter(|_| cull_env.as_deref() != Ok("0")) + .map(|depth_test| match cull_env.as_deref() { + Ok("shader") => cull::DepthTest::InShader, + _ => depth_test, + }) + .map(|depth_test| { + let mut cull = + cull::Cull::new(&device, config.format.remove_srgb_suffix(), depth_test); cull.always = cull_env.as_deref() == Ok("all"); cull }); + let depth = Self::depth(&device, &config, cull.as_ref()); let world_post = world_post::Post::new(&device, config.format.remove_srgb_suffix()); let uploads = upload::Uploads::new(&device); let section_uploads = upload::Uploads::with_chunk(&device, SECTION_UPLOAD_CHUNK); @@ -2327,7 +2335,13 @@ impl Renderer { self.surface = Some(surface); Ok(()) } - fn depth(device: &wgpu::Device, config: &wgpu::SurfaceConfiguration) -> wgpu::TextureView { + /// The world's depth, which occlusion culling's test may read. + fn depth( + device: &wgpu::Device, + config: &wgpu::SurfaceConfiguration, + cull: Option<&cull::Cull>, + ) -> wgpu::TextureView { + let read = cull.is_some_and(|c| c.depth_test() == cull::DepthTest::InShader); device .create_texture(&wgpu::TextureDescriptor { label: Some("Horizon depth"), @@ -2340,7 +2354,11 @@ impl Renderer { sample_count: 1, dimension: wgpu::TextureDimension::D2, format: wgpu::TextureFormat::Depth32Float, - usage: wgpu::TextureUsages::RENDER_ATTACHMENT, + usage: if read { + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING + } else { + wgpu::TextureUsages::RENDER_ATTACHMENT + }, view_formats: &[], }) .create_view(&Default::default()) @@ -2391,7 +2409,7 @@ impl Renderer { } else { self.offscreen = Some(Self::offscreen_target(&self.device, &self.config)); } - self.depth = Self::depth(&self.device, &self.config); + self.depth = Self::depth(&self.device, &self.config, self.cull.as_ref()); } /// Whether sections are drawn as one index list per frame (`flat`). pub fn flat_draws(&self) -> bool { @@ -3096,16 +3114,18 @@ impl Renderer { .inverse() .transform_point3(Vec3::ZERO); // Occlusion culling leaves the sections its last result did not see - // to phase 2, after the others, with instance counts of 0. + // to phase 2, after the others, with instance counts of 0. Frames + // drawn flat are not culled (phase 2's counts reach only per-section + // draws): those with as many draws as the last frame drew flat. + let flat_likely = self.flat_on + && self + .flat + .as_ref() + .is_some_and(|f| self.section_draws >= f.min_draws); let mut cull = self .cull .as_mut() - .filter(|_| { - self.cull_on - && !frame.title - && self.multi_draw - && !(self.flat_on && self.flat.is_some()) - }) + .filter(|_| self.cull_on && !frame.title && self.multi_draw && !flat_likely) .and_then(|cull| cull.begin(&self.device).then_some(cull)); self.phase2_draws.clear(); self.phase2_pages.clear(); @@ -3493,7 +3513,10 @@ impl Renderer { ); let flat_frame = match &mut self.flat { Some(flat) - if self.flat_on && !self.draws.is_empty() && self.draws.len() >= flat.min_draws => + if self.flat_on + && !culling + && !self.draws.is_empty() + && self.draws.len() >= flat.min_draws => { let sections = &self.sections; let lods = &self.lods; @@ -3567,6 +3590,9 @@ impl Renderer { Some((color, depth)) => (color, depth), None => (&view_target, &self.depth), }; + if culling && let Some(cull) = &mut self.cull { + cull.bind_depth(&self.device, world_depth); + } let cull = self.cull.as_ref().filter(|_| culling && solid_draws > 0); { let attachments = [Some(wgpu::RenderPassColorAttachment { @@ -3659,12 +3685,14 @@ impl Renderer { pass.set_vertex_buffer(1, self.instances.slice(..)); match cull { Some(cull) => { - // Phase 1 and the boxes against its depth, then + // Phase 1 and the boxes against its depth (in + // its pass, or one of their own on Metal), then // what they found in sight, in a pass that loads // both targets (it reads the test's results). self.draw_sections(&mut pass, &self.indirect, 0, 0, phase1_draws); cull.test(&mut pass); drop(pass); + cull.test_pass(&mut encoder); let attachments = [Some(wgpu::RenderPassColorAttachment { view: world_view, resolve_target: None, diff --git a/crates/horizon-client/src/gfx/cull.rs b/crates/horizon-client/src/gfx/cull.rs index bcb738de3..77574e850 100644 --- a/crates/horizon-client/src/gfx/cull.rs +++ b/crates/horizon-client/src/gfx/cull.rs @@ -22,8 +22,16 @@ //! Every solid draw is an entry of one of the two lists, so the CPU records //! as many draws as without culling: drivers that unroll an indirect //! multi-draw on the CPU (Mesa's on Intel Gen9) pay per entry. Phase 2's -//! counts come from the GPU, which needs a native multi-draw and forced -//! early depth tests: Vulkan. Translucent draws are not culled. +//! counts come from the GPU, which reads them from the draws themselves: +//! Vulkan's native multi-draw, and Metal's, which wgpu replays as one +//! indirect draw per entry. Translucent draws are not culled. +//! +//! Metal cannot force the depth test before a fragment shader that writes +//! storage (naga writes no `[[early_fragment_tests]]`), so there the boxes +//! are drawn in a pass of their own between the phases, which reads phase +//! 1's depth as a texture and makes the same comparison in the shader: the +//! fragment's depth against the stored one, greater or equal passing. The +//! same fragments pass, so the same sections are seen. use std::num::NonZeroU64; use std::sync::{ Arc, @@ -74,10 +82,40 @@ struct Group { group: wgpu::BindGroup, } +/// Where the boxes' depth test runs. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum DepthTest { + /// Forced before the fragment shader, in phase 1's pass (Vulkan). + Early, + /// In the fragment shader, against phase 1's depth read as a texture, + /// in a pass of its own (Metal). + InShader, +} + +/// The Vulkan entry point: the depth test forced early, which naga cannot +/// write for Metal (so it is not in `cull.wgsl`, which every backend takes). +const EARLY_TEST: &str = " +@fragment +@early_depth_test(force) +fn fs_box_early(in: Box) -> @location(0) vec4 { + see(in.test); + return vec4(0.0); +} +"; + +/// The test's depth texture binding (`InShader`), for the view it was made with. +struct DepthGroup { + view: wgpu::TextureView, + group: wgpu::BindGroup, +} + pub struct Cull { pipeline: wgpu::RenderPipeline, layout: wgpu::BindGroupLayout, group: Option, + depth_test: DepthTest, + depth_layout: Option, + depth_group: Option, params: wgpu::Buffer, /// Phase 2's draws: the solid draws after phase 1's in the frame's list. pub phase2: wgpu::Buffer, @@ -134,20 +172,33 @@ impl Cull { pub fn features(adapter: &wgpu::Adapter) -> wgpu::Features { adapter.features() & wgpu::Features::SHADER_EARLY_DEPTH_TEST } - /// Whether the device draws an indirect multi-draw natively and runs - /// fragment shaders that write storage only past an early depth test. - pub fn supported(adapter: &wgpu::Adapter, device: &wgpu::Device) -> bool { - adapter.get_info().backend == wgpu::Backend::Vulkan - && device.features().contains( - wgpu::Features::SHADER_EARLY_DEPTH_TEST | wgpu::Features::INDIRECT_FIRST_INSTANCE, - ) + /// Where the device can run the test, if it can: Vulkan with early + /// depth tests, or Metal, both drawing an indirect multi-draw's entries + /// with the instance counts the GPU wrote, with fragment shaders that + /// write storage. + pub fn supported(adapter: &wgpu::Adapter, device: &wgpu::Device) -> Option { + let depth_test = match adapter.get_info().backend { + wgpu::Backend::Vulkan + if device + .features() + .contains(wgpu::Features::SHADER_EARLY_DEPTH_TEST) => + { + DepthTest::Early + } + wgpu::Backend::Metal => DepthTest::InShader, + _ => return None, + }; + (device + .features() + .contains(wgpu::Features::INDIRECT_FIRST_INSTANCE) && adapter.get_downlevel_capabilities().flags.contains( wgpu::DownlevelFlags::VERTEX_STORAGE | wgpu::DownlevelFlags::FRAGMENT_WRITABLE_STORAGE, - ) + )) + .then_some(depth_test) } /// `color` is the world pass's colour format. - pub fn new(device: &wgpu::Device, color: wgpu::TextureFormat) -> Self { + pub fn new(device: &wgpu::Device, color: wgpu::TextureFormat, depth_test: DepthTest) -> Self { let entry = |binding, visibility, ty| wgpu::BindGroupLayoutEntry { binding, visibility, @@ -180,13 +231,41 @@ impl Cull { }); let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("Section culling"), - source: wgpu::ShaderSource::Wgsl(include_str!("cull.wgsl").into()), + source: wgpu::ShaderSource::Wgsl(source(depth_test).into()), + }); + let depth_layout = (depth_test == DepthTest::InShader).then(|| { + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("Section culling depth"), + entries: &[entry( + 0, + fragment, + wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Depth, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + )], + }) }); let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: Some("Section culling"), - bind_group_layouts: &[Some(&layout)], + bind_group_layouts: &[Some(&layout), depth_layout.as_ref()], ..Default::default() }); + // In the shader: no colour target, and phase 1's depth attached read + // only (a pass needs an attachment), its test left to the shader. + let (fragment_entry, targets, depth_compare) = match depth_test { + DepthTest::Early => ( + "fs_box_early", + &[Some(wgpu::ColorTargetState { + format: color, + blend: None, + write_mask: wgpu::ColorWrites::empty(), + })][..], + wgpu::CompareFunction::GreaterEqual, + ), + DepthTest::InShader => ("fs_box", &[][..], wgpu::CompareFunction::Always), + }; let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: Some("Section culling"), layout: Some(&pipeline_layout), @@ -204,20 +283,16 @@ impl Cull { depth_stencil: Some(wgpu::DepthStencilState { format: wgpu::TextureFormat::Depth32Float, depth_write_enabled: Some(false), - depth_compare: Some(wgpu::CompareFunction::GreaterEqual), + depth_compare: Some(depth_compare), stencil: Default::default(), bias: Default::default(), }), multisample: Default::default(), fragment: Some(wgpu::FragmentState { module: &module, - entry_point: Some("fs_box"), + entry_point: Some(fragment_entry), compilation_options: Default::default(), - targets: &[Some(wgpu::ColorTargetState { - format: color, - blend: None, - write_mask: wgpu::ColorWrites::empty(), - })], + targets, }), multiview_mask: None, cache: None, @@ -226,6 +301,9 @@ impl Cull { pipeline, layout, group: None, + depth_test, + depth_layout, + depth_group: None, params: buffer( device, "Section culling parameters", @@ -399,17 +477,69 @@ impl Cull { } } - /// In the world pass after phase 1: draw the tested boxes against its - /// depth. Leaves the pass's pipeline and group 0 changed. + pub fn depth_test(&self) -> DepthTest { + self.depth_test + } + + /// In the world pass after phase 1 (`Early`): draw the tested boxes + /// against its depth. Leaves the pass's pipeline and group 0 changed. pub fn test(&self, pass: &mut wgpu::RenderPass<'_>) { let Some(Group { group, .. }) = &self.group else { return; }; + if self.depth_test != DepthTest::Early || self.test_data.is_empty() { + return; + } + pass.set_pipeline(&self.pipeline); + pass.set_bind_group(0, group, &[]); + pass.draw(0..36, 0..self.test_data.len() as u32); + } + + /// Before the world pass (`InShader`): the binding of the depth the + /// world is drawn with this frame. + pub fn bind_depth(&mut self, device: &wgpu::Device, depth: &wgpu::TextureView) { + let Some(layout) = &self.depth_layout else { + return; + }; + if self.depth_group.as_ref().is_none_or(|g| g.view != *depth) { + self.depth_group = Some(DepthGroup { + view: depth.clone(), + group: device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Section culling depth"), + layout, + entries: &[wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::TextureView(depth), + }], + }), + }); + } + } + + /// 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) { + let (Some(Group { group, .. }), Some(DepthGroup { view, group: depth })) = + (&self.group, &self.depth_group) + else { + return; + }; if self.test_data.is_empty() { return; } + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Section culling"), + color_attachments: &[], + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view, + depth_ops: None, + stencil_ops: None, + }), + ..Default::default() + }); pass.set_pipeline(&self.pipeline); pass.set_bind_group(0, group, &[]); + pass.set_bind_group(1, depth, &[]); pass.draw(0..36, 0..self.test_data.len() as u32); } @@ -498,16 +628,33 @@ impl Cull { } } -#[cfg(test)] -mod tests { - #[test] - fn shader_validates() { - let module = naga::front::wgsl::parse_str(include_str!("cull.wgsl")).unwrap(); - naga::valid::Validator::new( - naga::valid::ValidationFlags::all(), - naga::valid::Capabilities::all(), - ) - .validate(&module) - .unwrap(); +/// The test's shader for `depth_test`. +fn source(depth_test: DepthTest) -> String { + let common = include_str!("cull.wgsl"); + match depth_test { + DepthTest::Early => format!("{common}{EARLY_TEST}"), + DepthTest::InShader => common.to_owned(), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn shaders_validate() { + for (depth_test, capabilities) in [ + (DepthTest::Early, naga::valid::Capabilities::all()), + // What wgpu's Metal backend allows: no early depth test. + ( + DepthTest::InShader, + naga::valid::Capabilities::all() - naga::valid::Capabilities::EARLY_DEPTH_TEST, + ), + ] { + let module = naga::front::wgsl::parse_str(&source(depth_test)).unwrap(); + naga::valid::Validator::new(naga::valid::ValidationFlags::all(), capabilities) + .validate(&module) + .unwrap_or_else(|e| panic!("{depth_test:?}: {e:?}")); + } } } diff --git a/crates/horizon-client/src/gfx/cull.wgsl b/crates/horizon-client/src/gfx/cull.wgsl index faa495caf..b06d4ccc2 100644 --- a/crates/horizon-client/src/gfx/cull.wgsl +++ b/crates/horizon-client/src/gfx/cull.wgsl @@ -86,16 +86,28 @@ fn vs_box(@builtin(vertex_index) v: u32, @builtin(instance_index) t: u32) -> Box return out; } -// Runs only for fragments that pass the depth test (forced early). -@fragment -@early_depth_test(force) -fn fs_box(in: Box) -> @location(0) vec4 { - if results[in.test] != params.stamp { - results[in.test] = params.stamp; - let test = tests[in.test]; +// The box is seen at this fragment: store the stamp, and give its phase 2 +// draws their instance. +fn see(t: u32) { + if results[t] != params.stamp { + results[t] = params.stamp; + let test = tests[t]; for (var k = 0u; k < test.z; k++) { phase2[(test.y + k) * 5u + 1u] = 1u; } } - return vec4(0.0); +} + +// Phase 1's depth, where the test runs in the shader (gfx/cull.rs). +@group(1) @binding(0) var depth: texture_depth_2d; + +// The world pass's reversed depth test, greater or equal passing, made here: +// where it cannot be forced before a shader that writes storage, a fragment +// that fails it would still write. Where it can, `fs_box_early` (cull.rs) +// runs only for the fragments that pass it. +@fragment +fn fs_box(in: Box) { + if in.position.z >= textureLoad(depth, vec2(in.position.xy), 0) { + see(in.test); + } } From 14be6ca3943944c6a80ff9f747375e6c363fe7b5 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 12:03:25 +0200 Subject: [PATCH 16/39] Metal: occlusion culling with flat draws too Flat draws (one index list per frame, the default on Apple GPUs at high render distances) turned occlusion culling off. Now phase 2's draws are flattened after the test, by a compute pass that copies only the sections the test saw, and each page run is drawn indirectly with the count it reached. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 132 +++-- crates/horizon-client/src/gfx/flat.rs | 587 ++++++++++++++++----- crates/horizon-client/src/gfx/flatten.wgsl | 47 +- 3 files changed, 577 insertions(+), 189 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 183f7823e..91da4f616 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -83,6 +83,40 @@ mod world_post; const COMPLETE_COLUMN: u32 = (1 << (crate::chunk::HEIGHT / 16)) - 1; /// A section's instance: its camera-relative origin and, in `w`, the step of /// its fixed-point UVs (`terrain.wgsl`'s `terrain_uv`). +/// The world pass again after occlusion culling's test: it loads both +/// targets (phase 2 draws over phase 1, reading the test's results). +fn world_pass_after_culling<'e>( + encoder: &'e mut wgpu::CommandEncoder, + view: &wgpu::TextureView, + depth: &wgpu::TextureView, + pack: bool, +) -> wgpu::RenderPass<'e> { + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Horizon world, after culling"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Load, + store: wgpu::StoreOp::Store, + }, + })], + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: depth, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Load, + store: if pack { + wgpu::StoreOp::Store + } else { + wgpu::StoreOp::Discard + }, + }), + stencil_ops: None, + }), + ..Default::default() + }) +} fn section_instance(origin: [f32; 3]) -> [f32; 4] { [ origin[0], @@ -3114,18 +3148,11 @@ impl Renderer { .inverse() .transform_point3(Vec3::ZERO); // Occlusion culling leaves the sections its last result did not see - // to phase 2, after the others, with instance counts of 0. Frames - // drawn flat are not culled (phase 2's counts reach only per-section - // draws): those with as many draws as the last frame drew flat. - let flat_likely = self.flat_on - && self - .flat - .as_ref() - .is_some_and(|f| self.section_draws >= f.min_draws); + // to phase 2, after the others, with instance counts of 0. let mut cull = self .cull .as_mut() - .filter(|_| self.cull_on && !frame.title && self.multi_draw && !flat_likely) + .filter(|_| self.cull_on && !frame.title && self.multi_draw) .and_then(|cull| cull.begin(&self.device).then_some(cull)); self.phase2_draws.clear(); self.phase2_pages.clear(); @@ -3511,16 +3538,24 @@ impl Renderer { frame.hud, [self.config.width, self.config.height], ); + if culling && let Some(cull) = &mut self.cull { + cull.upload( + &self.device, + &mut self.uploads, + &mut encoder, + &self.draws[phase1_draws as usize..solid_draws as usize], + &self.instances, + &self.globals, + ); + } let flat_frame = match &mut self.flat { Some(flat) - if self.flat_on - && !culling - && !self.draws.is_empty() - && self.draws.len() >= flat.min_draws => + if self.flat_on && !self.draws.is_empty() && self.draws.len() >= flat.min_draws => { let sections = &self.sections; let lods = &self.lods; let sorted = &self.sorted_indices; + let phase2 = self.cull.as_ref().filter(|_| culling).map(|c| &c.phase2); crate::spans::time("flatten", || { flat.prepare( &self.device, @@ -3528,7 +3563,9 @@ impl Renderer { &mut self.uploads, &self.draws, &self.draw_pages, + phase1_draws as usize, solid_draws as usize, + phase2, visible .iter() .filter_map(|v| { @@ -3563,16 +3600,6 @@ impl Renderer { 0, bytemuck::cast_slice(&self.draws), ); - if culling && let Some(cull) = &mut self.cull { - cull.upload( - &self.device, - &mut self.uploads, - &mut encoder, - &self.draws[phase1_draws as usize..solid_draws as usize], - &self.instances, - &self.globals, - ); - } } let view_target = target_texture.create_view(&wgpu::TextureViewDescriptor { format: Some(self.config.format.remove_srgb_suffix()), @@ -3673,9 +3700,28 @@ impl Renderer { match self.flat.as_ref().filter(|_| flat_frame) { Some(flat) => { pass.set_pipeline(&flat.solid); - flat.draw(&mut pass, &flat.calls[..flat.solid_calls], |p| { - self.arena.buffer(p) - }); + let vertices = |p| self.arena.buffer(p); + match cull { + // As below: phase 1, the test, then phase 2's draws + // that the test saw, copied after it. + Some(cull) => { + flat.draw(&mut pass, &flat.calls[..flat.phase1_calls], vertices); + cull.test(&mut pass); + drop(pass); + cull.test_pass(&mut encoder); + flat.flatten_phase2(&mut encoder); + pass = world_pass_after_culling( + &mut encoder, + world_view, + world_depth, + pack, + ); + pass.set_bind_group(0, &self.group, &[]); + pass.set_pipeline(&flat.solid); + flat.draw_phase2(&mut pass, vertices); + } + None => flat.draw(&mut pass, &flat.calls[..flat.solid_calls], vertices), + } pass.set_bind_group(0, &self.group, &[]); pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); } @@ -3693,34 +3739,12 @@ impl Renderer { cull.test(&mut pass); drop(pass); cull.test_pass(&mut encoder); - let attachments = [Some(wgpu::RenderPassColorAttachment { - view: world_view, - resolve_target: None, - depth_slice: None, - ops: wgpu::Operations { - load: wgpu::LoadOp::Load, - store: wgpu::StoreOp::Store, - }, - })]; - pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: Some("Horizon world, after culling"), - color_attachments: &attachments, - depth_stencil_attachment: Some( - wgpu::RenderPassDepthStencilAttachment { - view: world_depth, - depth_ops: Some(wgpu::Operations { - load: wgpu::LoadOp::Load, - store: if pack { - wgpu::StoreOp::Store - } else { - wgpu::StoreOp::Discard - }, - }), - stencil_ops: None, - }, - ), - ..Default::default() - }); + pass = world_pass_after_culling( + &mut encoder, + world_view, + world_depth, + pack, + ); pass.set_bind_group(0, &self.group, &[]); pass.set_pipeline(&self.solid); pass.set_index_buffer( diff --git a/crates/horizon-client/src/gfx/flat.rs b/crates/horizon-client/src/gfx/flat.rs index a1ae20d09..2d0a8d27c 100644 --- a/crates/horizon-client/src/gfx/flat.rs +++ b/crates/horizon-client/src/gfx/flat.rs @@ -11,11 +11,37 @@ //! so each block names one section: its id here, which indexes the frame's //! camera-relative origins (computed on the CPU exactly as for the instance //! attribute). +//! +//! With occlusion culling (`cull`), phase 1's draws and the translucent ones +//! are copied before the world pass, as without it. Phase 2's draws are +//! copied after the test, which tells which ones it saw: each run of a page +//! reserves room for all of its draws, the copy packs the seen ones at its +//! start, and the run is drawn indirectly with the count the copy reached. use super::arena::GRAIN; /// One section draw for `flatten.wgsl`: index count, first sorted index (or -/// `QUADS` for a solid draw), base vertex, first index in the list. +/// `QUADS` for a solid draw; phase 2: its entry in phase 2's draws), base +/// vertex, first index in the list (phase 2: its draw call in phase 2). type Range = [u32; 4]; + +/// Where a group of ranges takes its indices. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Source { + /// Solid draws: quads. + Quads, + /// Occlusion culling's phase 2: quads, of the sections its test saw. + Seen, + /// Translucent draws: this sorted index page. + Sorted(u32), +} + +/// The frame's draw calls, as `plan` lays them out. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct Plan { + phase1_calls: usize, + solid_calls: usize, + total: u64, +} const QUADS: u32 = u32::MAX; const GROUP_LIMIT: u32 = 65535; @@ -38,6 +64,15 @@ pub struct Flat { pub translucent: wgpu::RenderPipeline, flatten: wgpu::ComputePipeline, compute_layout: wgpu::BindGroupLayout, + flatten_seen: wgpu::ComputePipeline, + seen_layout: wgpu::BindGroupLayout, + /// Phase 2's draw calls (`DrawIndexedIndirectArgs`), whose index counts + /// `flatten_seen` makes. + phase2_calls: wgpu::Buffer, + phase2_call_capacity: u64, + phase2_call_data: Vec<[u32; 5]>, + /// The copy of phase 2's draws, and its ranges. + phase2_group: Option<(wgpu::BindGroup, u32)>, draw_layout: wgpu::BindGroupLayout, blocks_per_page: u64, pages: Vec>, @@ -52,12 +87,14 @@ pub struct Flat { ranges: wgpu::Buffer, range_capacity: u64, range_data: Vec, - /// Segments of `range_data` by index source: (sorted index page or - /// `None` for solid draws, first range, ranges). - segments: Vec<(Option, u64, u64)>, + /// Segments of `range_data` by index source: (source, first range, + /// ranges). + segments: Vec<(Source, u64, u64)>, empty: wgpu::Buffer, - /// This frame's draw calls: solid ones, then translucent ones. + /// This frame's draw calls: solid ones (phase 1's, then phase 2's when + /// culling), then translucent ones. pub calls: Vec, + pub phase1_calls: usize, pub solid_calls: usize, /// Frames with fewer section draws keep the per-section draws. pub min_draws: usize, @@ -121,42 +158,60 @@ fn buffer( /// Lays out the frame's index list: each draw's range (destinations in draw /// order) grouped by index source into `ranges`, every group padded to /// `align` entries for binding, with `segments` naming each group; and the -/// draw calls, one per run of draws in the same vertex page, solid and -/// translucent apart. Returns the first translucent call and the list's -/// length. +/// draw calls, one per run of draws in the same vertex page, phase 1's solid +/// ones, phase 2's (draws `phase1_draws..solid_draws`) and translucent ones +/// apart. A phase 2 range names its draw in phase 2 and its call in phase 2 +/// instead of a destination: the call's room holds all of its draws. fn plan( draws: &[[u32; 5]], pages: &[(u32, Option)], + phase1_draws: usize, solid_draws: usize, align: u64, calls: &mut Vec, ranges: &mut Vec, - segments: &mut Vec<(Option, u64, u64)>, -) -> (usize, u64) { + segments: &mut Vec<(Source, u64, u64)>, +) -> Plan { calls.clear(); ranges.clear(); segments.clear(); - let mut solid_calls = None; + let (mut phase1_calls, mut solid_calls) = (None, None); let mut total = 0u64; - let mut by_source: Vec<(Option, Vec)> = Vec::new(); + let mut by_source: Vec<(Source, Vec)> = Vec::new(); for (i, (d, &(page, sorted))) in draws.iter().zip(pages).enumerate() { - let first = if sorted.is_some() { d[2] } else { QUADS }; - let range = [d[0], first, d[3], total as u32]; - match by_source.iter_mut().find(|(s, _)| *s == sorted) { - Some((_, list)) => list.push(range), - None => by_source.push((sorted, vec![range])), + if i == phase1_draws { + phase1_calls.get_or_insert(calls.len()); } if i == solid_draws { solid_calls = Some(calls.len()); } match calls.last_mut() { - Some(last) if last.page == page && i != solid_draws => last.count += d[0], + Some(last) if last.page == page && i != phase1_draws && i != solid_draws => { + last.count += d[0] + } _ => calls.push(Call { page, first: total as u32, count: d[0], }), } + let (source, range) = match sorted { + Some(p) => (Source::Sorted(p), [d[0], d[2], d[3], total as u32]), + None if (phase1_draws..solid_draws).contains(&i) => ( + Source::Seen, + [ + d[0], + (i - phase1_draws) as u32, + d[3], + (calls.len() - 1 - phase1_calls.unwrap_or(0)) as u32, + ], + ), + None => (Source::Quads, [d[0], QUADS, d[3], total as u32]), + }; + match by_source.iter_mut().find(|(s, _)| *s == source) { + Some((_, list)) => list.push(range), + None => by_source.push((source, vec![range])), + } total += u64::from(d[0]); } for (source, list) in by_source { @@ -165,7 +220,12 @@ fn plan( let padded = (ranges.len() as u64).next_multiple_of(align); ranges.resize(padded as usize, [0; 4]); } - (solid_calls.unwrap_or(calls.len()), total) + let solid_calls = solid_calls.unwrap_or(calls.len()); + Plan { + phase1_calls: phase1_calls.unwrap_or(calls.len()).min(solid_calls), + solid_calls, + total, + } } impl Flat { @@ -201,24 +261,37 @@ impl Flat { storage(2, compute, false), ], }); + let seen_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("Flatten phase 2 section draws"), + entries: &[ + storage(0, compute, true), + storage(2, compute, false), + storage(3, compute, true), + storage(4, compute, false), + ], + }); let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("Flatten section draws"), source: wgpu::ShaderSource::Wgsl(include_str!("flatten.wgsl").into()), }); - let flatten = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { - label: Some("Flatten section draws"), - layout: Some( - &device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - label: Some("Flatten section draws"), - bind_group_layouts: &[Some(&compute_layout)], - ..Default::default() - }), - ), - module: &module, - entry_point: Some("flatten"), - compilation_options: Default::default(), - cache: None, - }); + let compute_pipeline = |layout: &wgpu::BindGroupLayout, entry| { + device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { + label: Some("Flatten section draws"), + layout: Some( + &device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("Flatten section draws"), + bind_group_layouts: &[Some(layout)], + ..Default::default() + }), + ), + module: &module, + entry_point: Some(entry), + compilation_options: Default::default(), + cache: None, + }) + }; + let flatten = compute_pipeline(&compute_layout, "flatten"); + let flatten_seen = compute_pipeline(&seen_layout, "flatten_seen"); let storage_usage = wgpu::BufferUsages::STORAGE; Self { solid: pipeline(&layout, "Terrain solid (flat)", "fs_solid", None), @@ -230,6 +303,17 @@ impl Flat { ), flatten, compute_layout, + flatten_seen, + seen_layout, + phase2_calls: buffer( + device, + "Flat section draw calls, phase 2", + 20, + wgpu::BufferUsages::INDIRECT | storage_usage, + ), + phase2_call_capacity: 1, + phase2_call_data: Vec::new(), + phase2_group: None, draw_layout, blocks_per_page: page_vertices.div_ceil(GRAIN), pages: Vec::new(), @@ -251,6 +335,7 @@ impl Flat { segments: Vec::new(), empty: buffer(device, "No sorted indices", 16, storage_usage), calls: Vec::new(), + phase1_calls: 0, solid_calls: 0, min_draws: if std::env::var("HORIZON_FLAT_DRAWS").as_deref() == Ok("1") { 0 @@ -321,6 +406,9 @@ impl Flat { /// the origins of the sections they draw by id. Records the copy into /// `encoder`; `calls` then lists the frame's draw calls. False when the /// frame cannot be drawn flat (the per-section draws then apply). + /// + /// When culling, the solid draws from `phase1_draws` are phase 2's, which + /// `phase2` holds as the test leaves them: `flatten_phase2` copies them. #[allow(clippy::too_many_arguments)] pub fn prepare( &mut self, @@ -329,10 +417,18 @@ impl Flat { uploads: &mut super::upload::Uploads, draws: &[[u32; 5]], pages: &[(u32, Option)], + phase1_draws: usize, solid_draws: usize, + phase2: Option<&wgpu::Buffer>, origins: impl Iterator, sorted_page: impl Fn(u32) -> wgpu::Buffer, ) -> bool { + self.phase2_group = None; + let phase1_draws = if phase2.is_some() { + phase1_draws + } else { + solid_draws + }; // Origins by section id. self.origin_data.clear(); for (id, o) in origins { @@ -362,18 +458,25 @@ impl Flat { bytemuck::cast_slice(&self.origin_data), ); } - let (solid_calls, total) = plan( + let Plan { + phase1_calls, + solid_calls, + total, + } = plan( draws, pages, + phase1_draws, solid_draws, self.range_align, &mut self.calls, &mut self.range_data, &mut self.segments, ); + self.phase1_calls = phase1_calls; self.solid_calls = solid_calls; if total == 0 || total * 4 > device.limits().max_storage_buffer_binding_size { self.calls.clear(); + self.phase1_calls = 0; self.solid_calls = 0; return false; } @@ -401,22 +504,82 @@ impl Flat { 0, bytemuck::cast_slice(&self.range_data), ); + // Phase 2's calls start empty, each at the start of its room. + self.phase2_call_data.clear(); + self.phase2_call_data.extend( + self.calls[phase1_calls..solid_calls] + .iter() + .map(|c| [0, 1, c.first, 0, 0]), + ); + let phase2_calls = self.phase2_call_data.len() as u64; + if phase2_calls > self.phase2_call_capacity { + self.phase2_call_capacity = phase2_calls.next_power_of_two(); + self.phase2_calls = buffer( + device, + "Flat section draw calls, phase 2", + self.phase2_call_capacity * 20, + wgpu::BufferUsages::INDIRECT | wgpu::BufferUsages::STORAGE, + ); + } + if phase2_calls > 0 { + uploads.buffer( + encoder, + &self.phase2_calls, + 0, + bytemuck::cast_slice(&self.phase2_call_data), + ); + } + let ranges_at = |first: u64, count: u64| { + wgpu::BindingResource::Buffer(wgpu::BufferBinding { + buffer: &self.ranges, + offset: first * 16, + size: wgpu::BufferSize::new(count * 16), + }) + }; + if let (Some(phase2), Some(&(_, first, count))) = ( + phase2, + self.segments.iter().find(|(s, ..)| *s == Source::Seen), + ) { + let group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Flatten phase 2 section draws"), + layout: &self.seen_layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: ranges_at(first, count), + }, + wgpu::BindGroupEntry { + binding: 2, + resource: self.indices.as_entire_binding(), + }, + wgpu::BindGroupEntry { + binding: 3, + resource: phase2.as_entire_binding(), + }, + wgpu::BindGroupEntry { + binding: 4, + resource: self.phase2_calls.as_entire_binding(), + }, + ], + }); + self.phase2_group = Some((group, count as u32)); + } let groups: Vec<(wgpu::BindGroup, u32)> = self .segments .iter() - .map(|&(source, first, count)| { - let sorted = source.map(&sorted_page); + .filter_map(|&(source, first, count)| { + let sorted = match source { + Source::Quads => None, + Source::Seen => return None, + Source::Sorted(page) => Some(sorted_page(page)), + }; let group = device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("Flatten section draws"), layout: &self.compute_layout, entries: &[ wgpu::BindGroupEntry { binding: 0, - resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { - buffer: &self.ranges, - offset: first * 16, - size: wgpu::BufferSize::new(count * 16), - }), + resource: ranges_at(first, count), }, wgpu::BindGroupEntry { binding: 1, @@ -428,7 +591,7 @@ impl Flat { }, ], }); - (group, count as u32) + Some((group, count as u32)) }) .collect(); let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor { @@ -464,6 +627,19 @@ impl Flat { } true } + /// After occlusion culling's test: copies the phase 2 draws it saw. + pub fn flatten_phase2(&self, encoder: &mut wgpu::CommandEncoder) { + 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, + }); + pass.set_pipeline(&self.flatten_seen); + pass.set_bind_group(0, group, &[]); + pass.dispatch_workgroups((*count).min(GROUP_LIMIT), count.div_ceil(GROUP_LIMIT), 1); + } /// Draws `calls` with a flat pipeline set and `vertices` giving each /// page's vertex buffer. pub fn draw<'a>( @@ -474,15 +650,38 @@ impl Flat { ) { pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); for call in calls { - let table = self.pages[call.page as usize] - .as_ref() - .and_then(|t| t.group.as_ref()) - .expect("prepared page"); - pass.set_vertex_buffer(0, vertices(call.page).slice(..)); - pass.set_bind_group(1, table, &[]); + self.bind_page(pass, call.page, &vertices); pass.draw_indexed(call.first..call.first + call.count, 0, 0..1); } } + /// Draws phase 2's calls, as `flatten_phase2` left them. + pub fn draw_phase2<'a>( + &self, + pass: &mut wgpu::RenderPass<'_>, + vertices: impl Fn(u32) -> &'a wgpu::Buffer, + ) { + pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); + for (k, call) in self.calls[self.phase1_calls..self.solid_calls] + .iter() + .enumerate() + { + self.bind_page(pass, call.page, &vertices); + pass.draw_indexed_indirect(&self.phase2_calls, k as u64 * 20); + } + } + fn bind_page<'a>( + &self, + pass: &mut wgpu::RenderPass<'_>, + page: u32, + vertices: &impl Fn(u32) -> &'a wgpu::Buffer, + ) { + let table = self.pages[page as usize] + .as_ref() + .and_then(|t| t.group.as_ref()) + .expect("prepared page"); + pass.set_vertex_buffer(0, vertices(page).slice(..)); + pass.set_bind_group(1, table, &[]); + } } #[cfg(test)] @@ -516,16 +715,22 @@ mod tests { fn plan_draws_runs_of_a_page_in_order() { let (draws, pages, solid) = sample(); let (mut calls, mut ranges, mut segments) = (Vec::new(), Vec::new(), Vec::new()); - let (solid_calls, total) = plan( + let Plan { + phase1_calls, + solid_calls, + total, + } = plan( &draws, &pages, solid, + solid, 4, &mut calls, &mut ranges, &mut segments, ); assert_eq!(total, 6 * 13); + assert_eq!(phase1_calls, solid_calls); let call = |page, first, count| Call { page, first, count }; // Pages 0, 0 | 1 | 0, then translucent 0, 0 (never merged with solid). assert_eq!( @@ -539,16 +744,65 @@ mod tests { ); assert_eq!(solid_calls, 3); // Solid ranges, then each sorted page's, every group padded to 4. - assert_eq!(segments, [(None, 0, 4), (Some(0), 4, 1), (Some(1), 8, 1)]); + assert_eq!( + segments, + [ + (Source::Quads, 0, 4), + (Source::Sorted(0), 4, 1), + (Source::Sorted(1), 8, 1) + ] + ); assert_eq!(ranges[3], [24, QUADS, 1024, 36]); assert_eq!(ranges[4], [12, 6, 768, 60]); assert_eq!(ranges[8], [6, 0, 40, 72]); assert_eq!(ranges.len(), 12); } + /// Phase 2's draws (here the third and fourth) get calls of their own, + /// each with room for all of its draws, and ranges naming their draw + /// and call in phase 2. + #[test] + fn plan_gives_phase_2_calls_of_its_own() { + let (draws, pages, solid) = sample(); + let (mut calls, mut ranges, mut segments) = (Vec::new(), Vec::new(), Vec::new()); + let plan = plan( + &draws, + &pages, + 2, + solid, + 4, + &mut calls, + &mut ranges, + &mut segments, + ); + assert_eq!( + plan, + Plan { + phase1_calls: 1, + solid_calls: 3, + total: 6 * 13 + } + ); + let call = |page, first, count| Call { page, first, count }; + // Phase 1: page 0 | phase 2: page 1, page 0 | translucent: page 0. + assert_eq!( + calls, + [ + call(0, 0, 30), + call(1, 30, 6), + call(0, 36, 24), + call(0, 60, 18) + ] + ); + assert_eq!(segments[1], (Source::Seen, 4, 2)); + assert_eq!(ranges[4], [6, 0, 256, 0]); + assert_eq!(ranges[5], [24, 1, 1024, 1]); + } + /// The compute pass writes exactly the indices the per-section draws /// read: quads 0 1 2 0 2 3 for solid draws, sorted indices otherwise, - /// plus the base vertex. + /// plus the base vertex. With culling, phase 2's runs hold the indices + /// of the draws the test saw, in order, and their calls count them. #[test] fn flatten_writes_the_indices_of_the_per_section_draws() { let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle()); @@ -564,101 +818,170 @@ mod tests { use wgpu::util::DeviceExt; let (draws, pages, solid) = sample(); let align = (u64::from(device.limits().min_storage_buffer_offset_alignment) / 16).max(1); - let (mut calls, mut ranges, mut segments) = (Vec::new(), Vec::new(), Vec::new()); - let (_, total) = plan( - &draws, - &pages, - solid, - align, - &mut calls, - &mut ranges, - &mut segments, - ); let sorted: [Vec; 2] = [ (0..32).map(|i| (i * 7) % 32).collect(), (0..8).rev().collect(), ]; - let mut expected = Vec::new(); - for (d, (_, page)) in draws.iter().zip(&pages) { - for k in 0..d[0] { - let i = match page { - None => (k / 6) * 4 + [0, 1, 2, 0, 2, 3][(k % 6) as usize], - Some(p) => sorted[*p as usize][(d[2] + k) as usize], - }; - expected.push(d[3] + i); - } - } - let storage = |entries: &[u32]| { + let indices = |d: &[u32; 5], sorted_page: Option| -> Vec { + (0..d[0]) + .map(|k| { + d[3] + match sorted_page { + None => (k / 6) * 4 + [0, 1, 2, 0, 2, 3][(k % 6) as usize], + Some(p) => sorted[p as usize][(d[2] + k) as usize], + } + }) + .collect() + }; + let storage = |entries: &[u32], usage| { device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: None, contents: bytemuck::cast_slice(entries), - usage: wgpu::BufferUsages::STORAGE, + usage: wgpu::BufferUsages::STORAGE | usage, }) }; - let range_buffer = storage(bytemuck::cast_slice(&ranges)); - let sorted_buffers = [storage(&sorted[0]), storage(&sorted[1]), storage(&[0; 4])]; - let out = device.create_buffer(&wgpu::BufferDescriptor { - label: None, - size: total * 4, - usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC, - mapped_at_creation: false, - }); - let read = device.create_buffer(&wgpu::BufferDescriptor { - label: None, - size: total * 4, - usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, - mapped_at_creation: false, - }); + let none = wgpu::BufferUsages::empty(); + let sorted_buffers = [ + storage(&sorted[0], none), + storage(&sorted[1], none), + storage(&[0; 4], none), + ]; let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: None, source: wgpu::ShaderSource::Wgsl(include_str!("flatten.wgsl").into()), }); - let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { - label: None, - layout: None, - module: &module, - entry_point: Some("flatten"), - compilation_options: Default::default(), - cache: None, - }); - let mut encoder = device.create_command_encoder(&Default::default()); - { - let mut pass = encoder.begin_compute_pass(&Default::default()); - pass.set_pipeline(&pipeline); - for &(source, first, count) in &segments { - let group = device.create_bind_group(&wgpu::BindGroupDescriptor { - label: None, - layout: &pipeline.get_bind_group_layout(0), - entries: &[ - wgpu::BindGroupEntry { - binding: 0, - resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { - buffer: &range_buffer, - offset: first * 16, - size: wgpu::BufferSize::new(count * 16), - }), - }, - wgpu::BindGroupEntry { - binding: 1, - resource: sorted_buffers[source.map_or(2, |p| p as usize)] - .as_entire_binding(), - }, - wgpu::BindGroupEntry { - binding: 2, - resource: out.as_entire_binding(), - }, - ], - }); - pass.set_bind_group(0, &group, &[]); - pass.dispatch_workgroups(count as u32, 1, 1); + let pipeline = |entry| { + device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { + label: None, + layout: None, + module: &module, + entry_point: Some(entry), + compilation_options: Default::default(), + cache: None, + }) + }; + let (flatten, flatten_seen) = (pipeline("flatten"), pipeline("flatten_seen")); + let read_back = |buffer: &wgpu::Buffer, words: u64| -> Vec { + let read = device.create_buffer(&wgpu::BufferDescriptor { + label: None, + size: words * 4, + usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + let mut encoder = device.create_command_encoder(&Default::default()); + encoder.copy_buffer_to_buffer(buffer, 0, &read, 0, words * 4); + queue.submit([encoder.finish()]); + read.slice(..).map_async(wgpu::MapMode::Read, |_| {}); + device.poll(wgpu::PollType::wait_indefinitely()).unwrap(); + bytemuck::cast_slice(&read.slice(..).get_mapped_range().unwrap()).to_vec() + }; + // Without culling, then with the third and fourth draws in phase 2, + // of which the test saw only the fourth. + for phase1 in [solid, 2] { + let (mut calls, mut ranges, mut segments) = (Vec::new(), Vec::new(), Vec::new()); + let plan = plan( + &draws, + &pages, + phase1, + solid, + align, + &mut calls, + &mut ranges, + &mut segments, + ); + // Phase 2's draws as the test leaves them: instance counts of 0, + // 1 where it saw the section. + let mut phase2: Vec<[u32; 5]> = draws[phase1..solid] + .iter() + .map(|&[count, _, first, base, instance]| [count, 0, first, base, instance]) + .collect(); + if let Some(last) = phase2.last_mut() { + last[1] = 1; + } + let phase2_calls: Vec<[u32; 5]> = calls[plan.phase1_calls..plan.solid_calls] + .iter() + .map(|c| [0, 1, c.first, 0, 0]) + .collect(); + let range_buffer = storage(bytemuck::cast_slice(&ranges), none); + let phase2_buffer = storage( + bytemuck::cast_slice(&[phase2.as_slice(), &[[0; 5]]].concat()), + none, + ); + let calls_buffer = storage( + bytemuck::cast_slice(&[phase2_calls.as_slice(), &[[0; 5]]].concat()), + wgpu::BufferUsages::COPY_SRC, + ); + let out = storage(&vec![0; plan.total as usize], wgpu::BufferUsages::COPY_SRC); + let mut encoder = device.create_command_encoder(&Default::default()); + { + let mut pass = encoder.begin_compute_pass(&Default::default()); + for &(source, first, count) in &segments { + let ranges = wgpu::BindingResource::Buffer(wgpu::BufferBinding { + buffer: &range_buffer, + offset: first * 16, + size: wgpu::BufferSize::new(count * 16), + }); + let entry = |binding, resource| wgpu::BindGroupEntry { binding, resource }; + let (pipeline, entries) = match source { + Source::Seen => ( + &flatten_seen, + vec![ + entry(0, ranges), + entry(2, out.as_entire_binding()), + entry(3, phase2_buffer.as_entire_binding()), + entry(4, calls_buffer.as_entire_binding()), + ], + ), + Source::Quads | Source::Sorted(_) => { + let page = match source { + Source::Sorted(p) => p as usize, + _ => 2, + }; + ( + &flatten, + vec![ + entry(0, ranges), + entry(1, sorted_buffers[page].as_entire_binding()), + entry(2, out.as_entire_binding()), + ], + ) + } + }; + let group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: None, + layout: &pipeline.get_bind_group_layout(0), + entries: &entries, + }); + pass.set_pipeline(pipeline); + pass.set_bind_group(0, &group, &[]); + pass.dispatch_workgroups(count as u32, 1, 1); + } + } + queue.submit([encoder.finish()]); + let got = read_back(&out, plan.total); + // Phase 1's and the translucent calls: each of their draws, in order. + let expected: Vec = (0..draws.len()) + .filter(|i| !(phase1..solid).contains(i)) + .flat_map(|i| indices(&draws[i], pages[i].1)) + .collect(); + let drawn: Vec = calls + .iter() + .enumerate() + .filter(|(c, _)| !(plan.phase1_calls..plan.solid_calls).contains(c)) + .flat_map(|(_, c)| got[c.first as usize..(c.first + c.count) as usize].to_vec()) + .collect(); + assert_eq!(drawn, expected); + // Phase 2: page 1's run drew nothing; page 0's holds the fourth + // draw's indices. + if phase1 < solid { + let counts = read_back(&calls_buffer, phase2_calls.len() as u64 * 5); + assert_eq!(counts[0], 0); + assert_eq!(counts[5], draws[3][0]); + let run = &calls[plan.phase1_calls + 1]; + assert_eq!( + got[run.first as usize..(run.first + draws[3][0]) as usize], + indices(&draws[3], None) + ); } } - encoder.copy_buffer_to_buffer(&out, 0, &read, 0, total * 4); - queue.submit([encoder.finish()]); - read.slice(..).map_async(wgpu::MapMode::Read, |_| {}); - device.poll(wgpu::PollType::wait_indefinitely()).unwrap(); - let got: Vec = - bytemuck::cast_slice(&read.slice(..).get_mapped_range().unwrap()).to_vec(); - assert_eq!(got, expected); } } diff --git a/crates/horizon-client/src/gfx/flatten.wgsl b/crates/horizon-client/src/gfx/flatten.wgsl index 2f57f8775..d722d7b9f 100644 --- a/crates/horizon-client/src/gfx/flatten.wgsl +++ b/crates/horizon-client/src/gfx/flatten.wgsl @@ -4,9 +4,11 @@ // translucent ones read their sorted indices. struct Range { count: u32, - // First sorted index, or QUADS for a solid draw. + // First sorted index, or QUADS for a solid draw (`flatten_seen`: its + // entry in occlusion culling's phase 2 draws). first: u32, base: u32, + // First index in the list (`flatten_seen`: its draw call). dst: u32, } const QUADS: u32 = 0xffffffffu; @@ -14,6 +16,13 @@ const GROUP: u32 = 64u; @group(0) @binding(0) var ranges: array; @group(0) @binding(1) var sorted: array; @group(0) @binding(2) var out: array; + +// Index `k` of a solid draw: quad k / 6's vertices 0 1 2 0 2 3. +fn quad(k: u32) -> u32 { + let c = k % 6u; + return (k / 6u) * 4u + select(select(c - 2u, 0u, c == 3u), c, c < 3u); +} + @compute @workgroup_size(64) fn flatten(@builtin(workgroup_id) group: vec3, @builtin(local_invocation_index) lane: u32) { @@ -25,11 +34,43 @@ fn flatten(@builtin(workgroup_id) group: vec3, for (var k = lane; k < d.count; k += GROUP) { var i: u32; if d.first == QUADS { - let c = k % 6u; - i = (k / 6u) * 4u + select(select(c - 2u, 0u, c == 3u), c, c < 3u); + i = quad(k); } else { i = sorted[d.first + k]; } out[d.dst + k] = d.base + i; } } + +// Occlusion culling's phase 2 (gfx/cull.rs), after its test: the draws as +// the test left them (`DrawIndexedIndirectArgs`, the instance count 1 where +// it saw the section), and phase 2's draw calls, whose index counts start +// at 0. A draw the test saw takes the next indices of its call's run. +@group(0) @binding(3) var draws: array; +@group(0) @binding(4) var calls: array>; +const UNSEEN: u32 = 0xffffffffu; +var start: u32; + +@compute @workgroup_size(64) +fn flatten_seen(@builtin(workgroup_id) group: vec3, + @builtin(local_invocation_index) lane: u32) { + let r = group.y * 65535u + group.x; + if r >= arrayLength(&ranges) { + return; + } + let d = ranges[r]; + if lane == 0u { + var s = UNSEEN; + if draws[d.first * 5u + 1u] != 0u { + s = atomicLoad(&calls[d.dst * 5u + 2u]) + atomicAdd(&calls[d.dst * 5u], d.count); + } + start = s; + } + let s = workgroupUniformLoad(&start); + if s == UNSEEN { + return; + } + for (var k = lane; k < d.count; k += GROUP) { + out[s + k] = d.base + quad(k); + } +} From 173a55e60803eec759082107ab79bb71fbb2cff4 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 12:08:35 +0200 Subject: [PATCH 17/39] Delete pub items nothing uses, and the stubs that hid unused code 93 pub items (78 functions and methods, 14 constants, 1 struct) in horizon-client, horizon-world, horizon-core, horizon-friends and horizon-server-files had no caller anywhere in the repository: not in the crate, the other crates, the examples, the tests or the binaries. rustc can't flag them because horizon-client and the others are libraries, where a pub item counts as used. The imports and the private helpers only they used go with them (title.rs date_string, goals.rs flag_index). Also drops the #[allow(dead_code)]/#[allow(unused)] items that only hid dead code: chat_debug_fill, merchant_title and Goals::locked_by had no caller, and four stubs (_keep, _unused, _rule_kind, const _) existed only to keep an import used. LightTables::from_parts is used by its tests, so it is #[cfg(test)] instead of allowed. Protocol id tables and entity data serializer ids stay: they mirror vanilla's registries in full. The launcher's three unused items are left for a launcher PR. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/banner.rs | 8 +--- crates/horizon-client/src/block_entities.rs | 3 -- .../horizon-client/src/block_entity_anim.rs | 12 ------ .../horizon-client/src/block_entity_render.rs | 7 ---- crates/horizon-client/src/clock.rs | 4 -- crates/horizon-client/src/command/args.rs | 8 ---- .../horizon-client/src/command/component.rs | 6 +-- crates/horizon-client/src/command/item.rs | 4 -- crates/horizon-client/src/command/mod.rs | 30 --------------- crates/horizon-client/src/command/snbt.rs | 6 +-- crates/horizon-client/src/command/vanilla.rs | 8 +--- crates/horizon-client/src/containers.rs | 18 --------- crates/horizon-client/src/debug_chart.rs | 6 --- crates/horizon-client/src/enchant.rs | 17 --------- crates/horizon-client/src/first_person.rs | 9 ----- crates/horizon-client/src/game/chat.rs | 6 +-- crates/horizon-client/src/game/lan_trading.rs | 17 --------- crates/horizon-client/src/gfx.rs | 4 -- crates/horizon-client/src/gfx/lod.rs | 4 -- crates/horizon-client/src/gossip.rs | 1 - crates/horizon-client/src/graphics_ui.rs | 8 ---- crates/horizon-client/src/hud.rs | 18 --------- crates/horizon-client/src/item_render.rs | 11 ------ crates/horizon-client/src/keybinds.rs | 9 ----- crates/horizon-client/src/keymapping.rs | 2 - crates/horizon-client/src/lan/guest.rs | 6 --- crates/horizon-client/src/lan/hub.rs | 22 ----------- crates/horizon-client/src/lan/mod.rs | 6 --- crates/horizon-client/src/lang.rs | 6 --- crates/horizon-client/src/light.rs | 2 +- crates/horizon-client/src/mobs/anim.rs | 4 -- .../horizon-client/src/mobs/creaking_heart.rs | 4 -- crates/horizon-client/src/mobs/goals.rs | 37 ------------------- crates/horizon-client/src/mobs/happy_ghast.rs | 1 - .../horizon-client/src/mobs/monster_brain.rs | 4 -- crates/horizon-client/src/mobs/nautilus.rs | 8 ---- crates/horizon-client/src/mobs/nav.rs | 22 ----------- .../src/mobs/spawn_selection.rs | 13 ------- crates/horizon-client/src/mobs/spawning.rs | 19 ---------- .../horizon-client/src/mobs/villager_brain.rs | 6 +-- crates/horizon-client/src/mobs/world.rs | 8 +--- crates/horizon-client/src/packs.rs | 5 --- crates/horizon-client/src/recipe_book.rs | 15 -------- crates/horizon-client/src/save.rs | 6 --- crates/horizon-client/src/sculk.rs | 5 --- crates/horizon-client/src/settings.rs | 4 -- crates/horizon-client/src/tab_list.rs | 3 -- crates/horizon-client/src/text.rs | 3 -- crates/horizon-client/src/title.rs | 20 ---------- crates/horizon-client/src/ui.rs | 4 -- crates/horizon-client/src/worldborder.rs | 1 - crates/horizon-core/src/density/beardifier.rs | 3 -- .../horizon-core/src/geometry/shape/join.rs | 2 - crates/horizon-core/src/paletted.rs | 28 -------------- crates/horizon-core/src/ticks/generation.rs | 3 -- crates/horizon-core/src/ticks/mod.rs | 3 -- crates/horizon-core/src/version.rs | 4 -- crates/horizon-friends/src/guest.rs | 10 ----- crates/horizon-server-files/src/user_cache.rs | 8 ---- crates/horizon-server-files/src/user_lists.rs | 5 --- crates/horizon-world/src/block_entities.rs | 3 -- crates/horizon-world/src/block_render.rs | 12 ------ crates/horizon-world/src/block_support.rs | 19 ---------- crates/horizon-world/src/block_survival.rs | 8 ---- .../horizon-world/src/component_durability.rs | 8 ---- crates/horizon-world/src/datafix/dynamic.rs | 21 ----------- crates/horizon-world/src/entity_move.rs | 3 -- .../horizon-world/src/entity_obstruction.rs | 14 ------- crates/horizon-world/src/fluid.rs | 7 ---- crates/horizon-world/src/level/bonemeal.rs | 10 ----- crates/horizon-world/src/level/classes.rs | 3 -- crates/horizon-world/src/level/mod.rs | 1 - crates/horizon-world/src/level/states.rs | 6 --- crates/horizon-world/src/level/ticks.rs | 3 -- crates/horizon-world/src/registry_fluid.rs | 3 -- crates/horizon-world/src/retrogen.rs | 3 -- crates/horizon-world/src/worldgen/aquifer.rs | 5 --- .../src/worldgen/feature_region.rs | 17 --------- .../src/worldgen/material_condition/codec.rs | 6 --- .../src/worldgen/state_provider.rs | 6 --- crates/horizon-world/src/worldgen/terrain.rs | 12 ------ 81 files changed, 8 insertions(+), 688 deletions(-) diff --git a/crates/horizon-client/src/banner.rs b/crates/horizon-client/src/banner.rs index 861294722..aa470a552 100644 --- a/crates/horizon-client/src/banner.rs +++ b/crates/horizon-client/src/banner.rs @@ -2,7 +2,7 @@ //! `banner_patterns` layers carried by banner items, shields and placed //! banners, and the loom rules of `LoomMenu` (selectable patterns, the //! six-layer limit and the result stack). -use crate::{block_entity_render::DYES, item_data, items::ItemStack, items::Items}; +use crate::{block_entity_render::DYES, items::ItemStack, items::Items}; use horizon_world::nbt::Tag; use serde_json::Value; use std::{collections::HashMap, fs, path::Path, sync::Arc}; @@ -261,12 +261,6 @@ pub fn layers_from_nbt(tag: Option<&Tag>) -> Vec { }) .collect() } -/// Keeps `item_data` linked: the `StackData` fields the banner code reads. -pub fn set_layers(stack: &mut ItemStack, layers: Vec) { - let data: &mut item_data::StackData = stack.data_mut(); - data.banner_patterns = layers; - stack.tidy(); -} #[cfg(test)] mod tests { diff --git a/crates/horizon-client/src/block_entities.rs b/crates/horizon-client/src/block_entities.rs index 93abb3711..03fff85ca 100644 --- a/crates/horizon-client/src/block_entities.rs +++ b/crates/horizon-client/src/block_entities.rs @@ -144,9 +144,6 @@ impl BlockEntities { pub fn set_states(&mut self, states: Vec) { self.states = states; } - pub fn take_states(&mut self) -> Vec { - std::mem::take(&mut self.states) - } pub fn ticking(&self, state: u32) -> Ticking { self.states.get(state as usize).copied().unwrap_or_default() } diff --git a/crates/horizon-client/src/block_entity_anim.rs b/crates/horizon-client/src/block_entity_anim.rs index 2a35c9835..d91748e73 100644 --- a/crates/horizon-client/src/block_entity_anim.rs +++ b/crates/horizon-client/src/block_entity_anim.rs @@ -382,10 +382,6 @@ impl Shulker { } out } - /// `getProgress(a)`: `Mth.lerp(a, progressOld, progress)`. - pub fn get_progress(&self, a: f32) -> f32 { - self.progress_old + a * (self.progress - self.progress_old) - } /// `ShulkerBoxBlock.canOpen`: a closed box only opens when its lid has room /// (`level.noCollision` of the half-block lid box, deflated by 1e-6). pub fn lid_box(pos: [i32; 3], facing: u8) -> [f64; 6] { @@ -668,14 +664,6 @@ impl Skull { self.animating = false; } } - /// `getAnimation(a)`. - pub fn get_animation(&self, a: f32) -> f32 { - if self.animating { - self.tick_count as f32 + a - } else { - self.tick_count as f32 - } - } } /// `DecoratedPotBlockEntity.WobbleStyle`. diff --git a/crates/horizon-client/src/block_entity_render.rs b/crates/horizon-client/src/block_entity_render.rs index 21376d698..af4168307 100644 --- a/crates/horizon-client/src/block_entity_render.rs +++ b/crates/horizon-client/src/block_entity_render.rs @@ -875,13 +875,6 @@ impl BlockEntityRenderer { visibility: None, } } - /// The standing banner's pole-and-bar and flag models (`BannerRenderer.submitSpecial`). - pub fn banner_models(&self) -> Option<(&ModelPart, &ModelPart)> { - Some(( - self.models.get("standing_banner")?, - self.models.get("standing_banner_flag")?, - )) - } fn kind(&self, state: u32) -> &Kind { self.kinds.get(state as usize).unwrap_or(&Kind::None) } diff --git a/crates/horizon-client/src/clock.rs b/crates/horizon-client/src/clock.rs index 3240944ba..1aebf644a 100644 --- a/crates/horizon-client/src/clock.rs +++ b/crates/horizon-client/src/clock.rs @@ -169,10 +169,6 @@ impl TickRateManager { self.frozen_ticks_to_run -= 1; } } - /// `isEntityFrozen` for an entity that is not (and does not carry) a player. - pub fn is_entity_frozen(&self, is_player_or_carries_player: bool) -> bool { - !self.runs_normally() && !is_player_or_carries_player - } } /// What `/tick sprint` reports when it ends. diff --git a/crates/horizon-client/src/command/args.rs b/crates/horizon-client/src/command/args.rs index 68d41e14f..ccced67a7 100644 --- a/crates/horizon-client/src/command/args.rs +++ b/crates/horizon-client/src/command/args.rs @@ -812,14 +812,6 @@ pub fn java_parse_int(s: &str) -> Option { } s.parse().ok() } -/// Long.parseLong. -pub fn java_parse_long(s: &str) -> Option { - let digits = s.strip_prefix(['-', '+']).unwrap_or(s); - if digits.is_empty() || !digits.chars().all(|c| c.is_ascii_digit()) { - return None; - } - s.parse().ok() -} /// Double.parseDouble over the characters StringReader reads as a number. pub fn java_parse_double(s: &str) -> Option { let t = s.trim_matches(|c: char| c <= ' '); diff --git a/crates/horizon-client/src/command/component.rs b/crates/horizon-client/src/command/component.rs index 9bc0668d5..15a8fa6a0 100644 --- a/crates/horizon-client/src/command/component.rs +++ b/crates/horizon-client/src/command/component.rs @@ -1,7 +1,7 @@ //! ComponentSerialization.CODEC and Style.Serializer.MAP_CODEC decoding of //! SNBT text components, with the data-fixer-upper error messages //! (ComponentArgument, StyleArgument and component-valued item data). -use super::codec::{get, no_key, not_list, not_map, snbt}; +use super::codec::{get, no_key, not_list, not_map}; use horizon_world::nbt::{Compound, Tag}; /// A DataResult error: its message and whether it carries a partial result. @@ -362,7 +362,3 @@ pub fn decode_style(t: &Tag) -> Result<(), String> { _ => Err(not_map(t)), } } -#[allow(unused)] -fn _keep(t: &Tag) -> String { - snbt(t) -} diff --git a/crates/horizon-client/src/command/item.rs b/crates/horizon-client/src/command/item.rs index 7c8abe84a..f2abc91ef 100644 --- a/crates/horizon-client/src/command/item.rs +++ b/crates/horizon-client/src/command/item.rs @@ -632,10 +632,6 @@ pub fn parse_predicate(reader: &mut Reader, reg: &Registries) -> CmdResult String { - snbt(t) -} #[cfg(test)] mod tests { diff --git a/crates/horizon-client/src/command/mod.rs b/crates/horizon-client/src/command/mod.rs index e9aeaae51..a61c8c08d 100644 --- a/crates/horizon-client/src/command/mod.rs +++ b/crates/horizon-client/src/command/mod.rs @@ -183,9 +183,6 @@ impl<'a> Reader<'a> { pub fn read_int(&mut self) -> CmdResult { self.number("int") } - pub fn read_long(&mut self) -> CmdResult { - self.number("long") - } pub fn read_double(&mut self) -> CmdResult { self.number("double") } @@ -222,21 +219,6 @@ impl<'a> Reader<'a> { } Err(CommandError::tr_at("parsing.quote.expected.end", &[], self)) } - pub fn read_quoted_string(&mut self) -> CmdResult { - if !self.can_read() { - return Ok(String::new()); - } - let next = self.peek(); - if next != '"' && next != '\'' { - return Err(CommandError::tr_at( - "parsing.quote.expected.start", - &[], - self, - )); - } - self.skip(); - self.read_string_until(next) - } pub fn read_string(&mut self) -> CmdResult { if !self.can_read() { return Ok(String::new()); @@ -539,18 +521,6 @@ impl<'a> SuggestionsBuilder<'a> { } } } - /// The same with a tooltip per candidate. - pub fn suggest_matching_tooltip<'b>( - &mut self, - candidates: impl IntoIterator, - ) { - let lower = self.remaining_lower(); - for (c, tip) in candidates { - if args::matches_sub_str(&lower, &c.to_lowercase()) { - self.suggest_tooltip(c, tip); - } - } - } } /// String.compareToIgnoreCase. pub fn compare_ignore_case(a: &str, b: &str) -> std::cmp::Ordering { diff --git a/crates/horizon-client/src/command/snbt.rs b/crates/horizon-client/src/command/snbt.rs index 05fe6d9bc..7472c7ae3 100644 --- a/crates/horizon-client/src/command/snbt.rs +++ b/crates/horizon-client/src/command/snbt.rs @@ -1,8 +1,6 @@ //! SnbtGrammar and TagParser: vanilla's packrat SNBT grammar, with its //! error messages and completion suggestions, producing NBT tags. -use super::packrat::{ - Grammar, Reason, Rule, State, Sugg, Term, Value, alt, ch, chs, look, opt, seq, -}; +use super::packrat::{Grammar, Reason, State, Sugg, Term, Value, alt, ch, chs, look, opt, seq}; use super::{CmdResult, CommandError, Reader, SuggestionsBuilder}; use horizon_world::nbt::{Compound, JavaString, Tag}; use std::rc::Rc; @@ -981,5 +979,3 @@ pub fn rule_in( } } } -#[allow(unused)] -fn _rule_kind(_: Rule) {} diff --git a/crates/horizon-client/src/command/vanilla.rs b/crates/horizon-client/src/command/vanilla.rs index fd23b82cd..cdd4848d7 100644 --- a/crates/horizon-client/src/command/vanilla.rs +++ b/crates/horizon-client/src/command/vanilla.rs @@ -3,7 +3,7 @@ //! mode keywords are read back from the parsed literals. use super::{ Dispatcher, - args::{ArgType, GameMode, Resource, integer}, + args::{ArgType, Resource, integer}, }; use crate::gamerules::{RULES, Value}; @@ -509,10 +509,6 @@ fn clone_mode(d: &mut Dispatcher, node: usize) { d.executes(l, "clone"); } } -/// Game mode names for selector `gamemode=` and the argument. -pub fn game_modes() -> [&'static str; 4] { - GameMode::ALL.map(GameMode::name) -} /// The shared vanilla tree. /// Reorder node children as vanilla registers them (Brigadier keeps /// children in registration order; smart usage lists them that way). @@ -559,5 +555,3 @@ pub fn release_dispatcher() -> &'static Dispatcher { static TREE: std::sync::OnceLock = std::sync::OnceLock::new(); TREE.get_or_init(|| build_with(false)) } -#[allow(unused)] -const _: (u8, u8) = (ALL, GAMEMASTERS); diff --git a/crates/horizon-client/src/containers.rs b/crates/horizon-client/src/containers.rs index 854332815..6ecdf0d93 100644 --- a/crates/horizon-client/src/containers.rs +++ b/crates/horizon-client/src/containers.rs @@ -252,24 +252,6 @@ impl BlockEntity { }; Some(Self::Chest(vec![None; size])) } - /// The vanilla block entity type id of a container block. - pub fn id_for_block(name: &str) -> Option<&'static str> { - Some(match name { - "minecraft:chest" => "minecraft:chest", - n if n.ends_with("copper_chest") => "minecraft:chest", - "minecraft:trapped_chest" => "minecraft:trapped_chest", - "minecraft:barrel" => "minecraft:barrel", - "minecraft:hopper" => "minecraft:hopper", - "minecraft:dispenser" => "minecraft:dispenser", - "minecraft:dropper" => "minecraft:dropper", - "minecraft:furnace" => "minecraft:furnace", - "minecraft:blast_furnace" => "minecraft:blast_furnace", - "minecraft:smoker" => "minecraft:smoker", - n if n.ends_with("shulker_box") => "minecraft:shulker_box", - "minecraft:crafter" => "minecraft:crafter", - _ => return None, - }) - } pub fn id(&self) -> &'static str { match self { Self::Chest(items) => match items.len() { diff --git a/crates/horizon-client/src/debug_chart.rs b/crates/horizon-client/src/debug_chart.rs index 35fd40bd4..3d9cc5556 100644 --- a/crates/horizon-client/src/debug_chart.rs +++ b/crates/horizon-client/src/debug_chart.rs @@ -17,8 +17,6 @@ use std::{ pub const CAPACITY: usize = 240; /// `AbstractDebugChart.CHART_HEIGHT`. pub const CHART_HEIGHT: i32 = 60; -/// `TpsDebugDimensions`: a tick sample's values. -pub const FULL_TICK: usize = 0; pub const TICK_SERVER_METHOD: usize = 1; pub const SCHEDULED_TASKS: usize = 2; pub const IDLE: usize = 3; @@ -252,10 +250,6 @@ impl Chart { pub fn width(max_width: i32) -> i32 { (CAPACITY as i32 + 2).min(max_width) } - /// `getFullHeight`: the box and the min/avg/max line above it. - pub fn full_height() -> i32 { - CHART_HEIGHT + 9 - } /// `AbstractDebugChart.extractRenderState`. pub fn draw( self, diff --git a/crates/horizon-client/src/enchant.rs b/crates/horizon-client/src/enchant.rs index ca2ea6f5c..2fc9ef2fb 100644 --- a/crates/horizon-client/src/enchant.rs +++ b/crates/horizon-client/src/enchant.rs @@ -359,10 +359,6 @@ impl Enchantments { } out } - /// Whether a stack draws the enchantment glint. - pub fn has_glint(&self, stack: &ItemStack) -> bool { - stack.has_any_enchantments() || self.props(&stack.item).is_some_and(|p| p.glint) - } // ---- Enchanting table (EnchantmentHelper) ---- @@ -514,19 +510,6 @@ impl Enchantments { .is_some_and(|e| e.effects.get(component).is_some()) }) } - /// The highest level among enchantments on `stack` that have `component`. - pub fn component_level(&self, stack: &ItemStack, component: &str) -> u32 { - stack - .enchantments() - .iter() - .filter(|(id, _)| { - self.get(id) - .is_some_and(|e| e.effects.get(component).is_some()) - }) - .map(|(_, l)| *l) - .max() - .unwrap_or(0) - } /// `minecraft:attributes` contributions of `stack`: (attribute, id, amount, operation). pub fn attributes(&self, stack: &ItemStack) -> Vec<(String, String, f64, String)> { let mut out = Vec::new(); diff --git a/crates/horizon-client/src/first_person.rs b/crates/horizon-client/src/first_person.rs index 463d51437..02132d942 100644 --- a/crates/horizon-client/src/first_person.rs +++ b/crates/horizon-client/src/first_person.rs @@ -289,15 +289,6 @@ impl Default for HandHeights { } } } -/// What `FirstPersonHandsAndItems.tick` decides about a hand's visible item. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct HandChange { - /// The visible item is not the hand's item after the instant replacement - /// (`mainHandItem != nextMainHand`): the hand lowers. - pub changed: bool, - /// The visible item becomes the hand's item this tick. - pub replace: bool, -} impl HandHeights { /// `FirstPersonHandsAndItems.tick`'s heights. `changed` says per hand /// whether the visible item differs from the held one once diff --git a/crates/horizon-client/src/game/chat.rs b/crates/horizon-client/src/game/chat.rs index 20aeeb7fd..0247896f5 100644 --- a/crates/horizon-client/src/game/chat.rs +++ b/crates/horizon-client/src/game/chat.rs @@ -5,7 +5,7 @@ use horizon_client::{ chat::{Chat, ChatAction, ChatScreen, Commands, SYSTEM_INDICATOR}, command::Registries, gamerules::GameRules, - hud::{HudBatch, rgba}, + hud::HudBatch, screen_draw::Measure, text::{ClickEvent, Style, Text}, }; @@ -883,10 +883,6 @@ impl Game<'_> { .chat .add(&self.gui.atlas, message, None, self.ticks, focused); } - #[allow(dead_code)] - fn chat_debug_fill(&self, batch: &mut HudBatch) { - batch.fill(&self.gui.atlas, 0.0, 0.0, 1.0, 1.0, rgba(0, 0, 0, 0)); - } } /// `yyyy-MM-dd_HH.mm.ss[_n].png` (Util.getFilenameFormattedDateTime, local time /// approximated as UTC). diff --git a/crates/horizon-client/src/game/lan_trading.rs b/crates/horizon-client/src/game/lan_trading.rs index 786399dac..83aafecc2 100644 --- a/crates/horizon-client/src/game/lan_trading.rs +++ b/crates/horizon-client/src/game/lan_trading.rs @@ -129,23 +129,6 @@ impl Game<'_> { } /// The screen title: the custom name, else the profession (the client /// adds the level itself). - #[allow(dead_code)] - fn merchant_title(&self, state: &MerchantState) -> String { - if let Some(name) = &state.name { - return name.clone(); - } - let e = &self.res.stations().enchantments; - let profession = state.session.profession.trim_start_matches("minecraft:"); - if state.session.villager { - e.translate(&format!("entity.minecraft.villager.{profession}")) - .unwrap_or(profession) - .to_owned() - } else { - e.translate("entity.minecraft.wandering_trader") - .unwrap_or("Wandering Trader") - .to_owned() - } - } /// `ServerPlayer.sendMerchantOffers`, when the offers changed. fn lan_send_offers( &mut self, diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index e379ace7c..e7997fb05 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -2307,10 +2307,6 @@ impl Renderer { view_formats: &config.view_formats, }) } - /// Rendering to a texture instead of the window (`--offscreen`). - pub fn is_offscreen(&self) -> bool { - self.surface.is_none() - } /// Release the window surface before another renderer uses the same window. pub fn release_surface(&mut self) { self.surface = None; diff --git a/crates/horizon-client/src/gfx/lod.rs b/crates/horizon-client/src/gfx/lod.rs index 734110b5c..3ebec378b 100644 --- a/crates/horizon-client/src/gfx/lod.rs +++ b/crates/horizon-client/src/gfx/lod.rs @@ -119,10 +119,6 @@ impl Tile { } impl Renderer { - pub fn resident_lods(&self) -> usize { - self.lods.len() - } - pub fn upload_lod(&mut self, key: TileKey, mesh: &SectionMesh) { self.remove_lod(key); if let Some(bounds) = Bounds::mesh(mesh) diff --git a/crates/horizon-client/src/gossip.rs b/crates/horizon-client/src/gossip.rs index 9a964a3e6..9ea4a668d 100644 --- a/crates/horizon-client/src/gossip.rs +++ b/crates/horizon-client/src/gossip.rs @@ -7,7 +7,6 @@ use std::collections::BTreeMap; /// `ReputationEventType` amounts. pub const REPUTATION_CHANGE_PER_EVENT: i32 = 25; -pub const REPUTATION_CHANGE_PER_EVERLASTING_MEMORY: i32 = 20; pub const REPUTATION_CHANGE_PER_TRADE: i32 = 2; /// `GossipContainer.DISCARD_THRESHOLD`: values below this are forgotten. const DISCARD_THRESHOLD: i32 = 2; diff --git a/crates/horizon-client/src/graphics_ui.rs b/crates/horizon-client/src/graphics_ui.rs index 514e64a88..d7ed82132 100644 --- a/crates/horizon-client/src/graphics_ui.rs +++ b/crates/horizon-client/src/graphics_ui.rs @@ -485,14 +485,6 @@ impl Item { _ => return None, }) } - /// Caption and value text, split from the label ("Caption: Value"). - pub fn caption_value(self, s: &Settings) -> (String, String) { - let label = self.label(s).plain(); - match label.split_once(": ") { - Some((c, v)) => (c.to_owned(), v.to_owned()), - None => (label, String::new()), - } - } pub fn slider(self, s: &Settings) -> Option { match self { Item::Vanilla(o) => o.slider_position(s), diff --git a/crates/horizon-client/src/hud.rs b/crates/horizon-client/src/hud.rs index 82e606947..480cf6ef9 100644 --- a/crates/horizon-client/src/hud.rs +++ b/crates/horizon-client/src/hud.rs @@ -783,24 +783,6 @@ impl HudBatch { }; self.text_styled(atlas, &t, x, y, color, shadow) } - /// `text_styled` under a pose: GUI positions scaled by `scale`, then - /// moved to `origin` (Matrix3x2fStack translate + scale). - pub fn text_posed( - &mut self, - atlas: &HudAtlas, - text: &crate::text::Text, - at: [f32; 2], - color: u32, - scale: f32, - origin: [f32; 2], - ) -> f32 { - let start = self.vertices.len(); - let advance = self.text_styled(atlas, text, at[0], at[1], color, true); - for v in &mut self.vertices[start..] { - v.pos = [origin[0] + v.pos[0] * scale, origin[1] + v.pos[1] * scale]; - } - advance - } /// Font.drawInternal: styled glyphs, shadows first, with bold, italic, /// underline and strikethrough. `color` is the default colour and alpha. pub fn text_styled( diff --git a/crates/horizon-client/src/item_render.rs b/crates/horizon-client/src/item_render.rs index 45531bedd..c2f5c8400 100644 --- a/crates/horizon-client/src/item_render.rs +++ b/crates/horizon-client/src/item_render.rs @@ -124,17 +124,6 @@ impl ItemRenderState { pub fn is_empty(&self) -> bool { self.layers.is_empty() } - /// `pickParticleMaterial`: a random layer's particle sprite. - pub fn pick_particle( - &self, - random: &mut impl horizon_core::random::RandomSource, - ) -> Option { - if self.layers.is_empty() { - return None; - } - let i = random.next_int_bound(self.layers.len() as i32).unwrap_or(0) as usize; - self.layers.get(i).map(|l| l.particle.clone()) - } /// `usesBlockLight` of the first layer (GUI lighting choice). pub fn uses_block_light(&self) -> bool { self.layers.first().is_some_and(|l| l.uses_block_light) diff --git a/crates/horizon-client/src/keybinds.rs b/crates/horizon-client/src/keybinds.rs index 5f8c0876b..a59aa627c 100644 --- a/crates/horizon-client/src/keybinds.rs +++ b/crates/horizon-client/src/keybinds.rs @@ -295,15 +295,6 @@ impl KeyBinds { self.keys.insert(n, key.to_owned()); } } - /// Load `key_` from options.txt; false for unknown mappings. - pub fn set_saved(&mut self, name: &str, key: &str) -> bool { - if MAPPINGS.iter().any(|m| m.0 == name) { - self.set(name, key); - true - } else { - false - } - } pub fn saved(&self) -> Vec<(&'static str, String)> { MAPPINGS .iter() diff --git a/crates/horizon-client/src/keymapping.rs b/crates/horizon-client/src/keymapping.rs index e598fbf71..e438c27ce 100644 --- a/crates/horizon-client/src/keymapping.rs +++ b/crates/horizon-client/src/keymapping.rs @@ -104,8 +104,6 @@ pub const SNEAK: usize = 0; pub const SPRINT: usize = 1; /// Attack (mouse). pub const ATTACK: usize = 2; -/// Use (mouse). -pub const USE: usize = 3; /// The `ToggleKeyMapping`s `Options` builds: sneak and sprint (keyboard, /// restored after a screen), attack (mouse, restored) and use (mouse, not diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index 4236280cd..9251107ca 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -15,7 +15,6 @@ use std::{ /// GameType ids. pub const SURVIVAL: i32 = 0; pub const CREATIVE: i32 = 1; -pub const ADVENTURE: i32 = 2; pub const SPECTATOR: i32 = 3; /// An effect as last sent in ClientboundUpdateMobEffectPacket. @@ -332,11 +331,6 @@ impl Guest { pub fn send(&self, packet: &play::Clientbound) { self.sender.send(packet.encode()); } - pub fn send_all(&self, packets: &[play::Clientbound]) { - for p in packets { - self.send(p); - } - } pub fn chunk(&self) -> (i32, i32) { ( (self.position[0].floor() as i32) >> 4, diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index 1330b0ae4..cbe0ecab7 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -172,13 +172,6 @@ pub struct Hub { fn string(s: &str) -> Tag { Tag::String(JavaString::from(s)) } -pub fn yellow_translation(key: &str, args: &[&str]) -> Tag { - let mut t = translatable(key, args); - if let Tag::Compound(map) = &mut t { - map.insert(JavaString::from("color"), string("yellow")); - } - t -} /// Player.getDisplayName for a player in no team: the name, suggesting /// `/tell NAME `, showing the player on hover and inserting the name. @@ -1360,21 +1353,6 @@ impl Hub { (!batch.is_empty()).then_some(batch) } - /// Resend a chunk to everyone who has it (a block entity changed, relight). - pub fn resend_chunk(&mut self, world: &World, x: i32, z: i32) { - let Some(chunk) = world.chunks.get(&(x, z)) else { - return; - }; - let body = self.encoder.body_in(chunk, world); - for g in self - .guests - .iter() - .filter(|g| g.sent_chunks.contains(&(x, z))) - { - g.sender.send(body.clone()); - } - } - /// `ChunkHolder.broadcastChanges`' light part: the light sections that /// changed in a column go to the players for whom it is on the border of /// what they track (`ChunkMap.isChunkOnTrackedBorder`); the others light diff --git a/crates/horizon-client/src/lan/mod.rs b/crates/horizon-client/src/lan/mod.rs index c2f35bc8e..5e4aedbab 100644 --- a/crates/horizon-client/src/lan/mod.rs +++ b/crates/horizon-client/src/lan/mod.rs @@ -272,9 +272,6 @@ impl Shared { pub fn max_players(&self) -> usize { self.max_players.load(Ordering::Relaxed) } - pub fn set_max_players(&self, n: usize) { - self.max_players.store(n, Ordering::Relaxed); - } pub fn release(&self, uuid: u128) { self.online.lock().unwrap().retain(|(u, _)| *u != uuid); } @@ -513,9 +510,6 @@ impl Lan { pub fn reserve(&self, uuid: u128) { self.shared.reserved.lock().unwrap().insert(uuid); } - pub fn set_motd(&self, motd: &str) { - *self.shared.motd.lock().unwrap() = motd.to_owned(); - } pub fn stop(&mut self) { self.shared.stop.store(true, Ordering::Relaxed); self.stop_listening(); diff --git a/crates/horizon-client/src/lang.rs b/crates/horizon-client/src/lang.rs index 9f04ef27a..15f47285a 100644 --- a/crates/horizon-client/src/lang.rs +++ b/crates/horizon-client/src/lang.rs @@ -77,12 +77,6 @@ pub fn apply_deprecated(map: &mut HashMap, json: &[u8]) { } impl Language { - pub fn from_map(code: &str, map: HashMap) -> Self { - Self { - code: code.to_owned(), - map, - } - } /// ClientLanguage.loadFrom: en_us first, then the selected language, each /// from every namespace and every source in priority order (later sources /// override). diff --git a/crates/horizon-client/src/light.rs b/crates/horizon-client/src/light.rs index d7ea7d7fd..6cb4b65b1 100644 --- a/crates/horizon-client/src/light.rs +++ b/crates/horizon-client/src/light.rs @@ -203,7 +203,7 @@ impl LightTables { Ok(tables) } /// Assembled tables (tests and benchmarks): derives the packed light cache. - #[allow(dead_code)] + #[cfg(test)] fn from_parts(cells: Vec, occludes: Vec, faces: usize) -> Self { let mut tables = Self { cells, diff --git a/crates/horizon-client/src/mobs/anim.rs b/crates/horizon-client/src/mobs/anim.rs index 1410bf858..aa0d8fb2a 100644 --- a/crates/horizon-client/src/mobs/anim.rs +++ b/crates/horizon-client/src/mobs/anim.rs @@ -239,10 +239,6 @@ pub enum ArmPose { /// Raising a trident to throw it (or to launch with Riptide). ThrowTrident, } -/// HumanoidModel.setupAnim for piglins: `PiglinModel` poses the arms itself. -pub fn humanoid_item(p: &mut Poses, s: &State, _holding: bool) { - humanoid(p, s, &Hum::new(ArmPose::Empty, ArmPose::Empty)); -} /// HumanoidModel.setupAnim with empty hands (endermen). pub fn humanoid_pub(p: &mut Poses, s: &State, _m: &Mob) { humanoid(p, s, &Hum::new(ArmPose::Empty, ArmPose::Empty)); diff --git a/crates/horizon-client/src/mobs/creaking_heart.rs b/crates/horizon-client/src/mobs/creaking_heart.rs index e3c0e4cb7..1172b229c 100644 --- a/crates/horizon-client/src/mobs/creaking_heart.rs +++ b/crates/horizon-client/src/mobs/creaking_heart.rs @@ -152,10 +152,6 @@ pub fn protector(heart: &mut CreakingHeart, host: &impl HeartHost) -> Option bool { - protector(heart, host) == Some(id) -} fn distance_to_creaking(heart: &mut CreakingHeart, pos: [i32; 3], host: &impl HeartHost) -> f64 { protector(heart, host) .and_then(|id| host.mob(id)) diff --git a/crates/horizon-client/src/mobs/goals.rs b/crates/horizon-client/src/mobs/goals.rs index c12f9e7ee..7f73e32c4 100644 --- a/crates/horizon-client/src/mobs/goals.rs +++ b/crates/horizon-client/src/mobs/goals.rs @@ -76,9 +76,6 @@ pub struct GoalSelector { locked: [Option; 4], pub disabled: u8, } -fn flag_index(f: u8) -> usize { - f.trailing_zeros() as usize -} impl GoalSelector { pub fn add(&mut self, priority: i32, goal: impl Goal + 'static) { self.goals.push(Wrapped { @@ -174,10 +171,6 @@ impl GoalSelector { } self.locked = [None; 4]; } - #[allow(dead_code)] - fn locked_by(&self, flag: u8) -> Option { - self.locked[flag_index(flag)] - } } // --- random positions (DefaultRandomPos / LandRandomPos / RandomPos) ------- @@ -565,36 +558,6 @@ impl Mob { m.land_up_out_of_solid(w, p) }) } - /// LandRandomPos.getPosTowards - pub fn land_random_pos_towards( - &mut self, - w: &MobWorld, - h: i32, - v: i32, - to: [f64; 3], - ) -> Option<[f64; 3]> { - let (xd, zd) = (to[0] - self.position.x, to[2] - self.position.z); - self.pick_best(w, |m| { - let d = m.random_direction_within( - 0.0, - f64::from(h), - v, - 0, - xd, - zd, - std::f64::consts::FRAC_PI_2, - )?; - let p = m.toward(f64::from(h), d); - if p[1] < crate::chunk::MIN_Y - || p[1] >= 320 - || m.restricted(f64::from(h), p) - || !m.stable_destination(w, p) - { - return None; - } - m.land_up_out_of_solid(w, p) - }) - } pub fn target_info(&self, w: &MobWorld) -> Option { self.ai.target.and_then(|t| w.target_info(t)) } diff --git a/crates/horizon-client/src/mobs/happy_ghast.rs b/crates/horizon-client/src/mobs/happy_ghast.rs index c0ee860dd..6fc07e898 100644 --- a/crates/horizon-client/src/mobs/happy_ghast.rs +++ b/crates/horizon-client/src/mobs/happy_ghast.rs @@ -17,7 +17,6 @@ use horizon_core::{ // Happy ghast state: `head` the harness, `scratch[0]` 1 once set up as a // baby and 2 as an adult, `ai.home` the restriction, `counter` // the end tick of the follow-an-adult range pick. -pub const BABY_SCALE: f64 = 0.2375; /// `Attributes.FLYING_SPEED` and the speed `travel` uses (`* 5 / 3`). pub const FLYING_SPEED: f64 = 0.05; pub const AIR_SPEED: f32 = 0.05 * 5.0 / 3.0; diff --git a/crates/horizon-client/src/mobs/monster_brain.rs b/crates/horizon-client/src/mobs/monster_brain.rs index eeb2c42f2..1099e0bc3 100644 --- a/crates/horizon-client/src/mobs/monster_brain.rs +++ b/crates/horizon-client/src/mobs/monster_brain.rs @@ -250,10 +250,6 @@ pub fn nearest_players(m: &Mob) -> Vec { .and_then(|b| b.mem.nearest_players.value()) .unwrap_or_default() } -/// The damage the brain remembers (`HURT_BY`). -pub fn hurt_by_present(m: &Mob) -> bool { - brain(m).is_some_and(|b| b.mem.hurt_by.present()) -} /// `HURT_BY`'s damage source entity, if a living one. fn hurt_by_entity(m: &Mob, w: &MobWorld) -> Option { let t = brain(m)?.mem.hurt_by.value()?; diff --git a/crates/horizon-client/src/mobs/nautilus.rs b/crates/horizon-client/src/mobs/nautilus.rs index efeb5cc40..2fcf7eb46 100644 --- a/crates/horizon-client/src/mobs/nautilus.rs +++ b/crates/horizon-client/src/mobs/nautilus.rs @@ -76,14 +76,6 @@ pub fn finalize(m: &mut Mob, warm_ocean: bool) { m.extra.variant = u8::from(warm_ocean); } } -/// `checkNautilusSpawnRules`: 5 to 25 blocks below sea level, water below -/// and above. -pub fn spawn_rules_ok(w: &MobWorld, x: i32, y: i32, z: i32) -> bool { - let sea = 63; - (sea - 25..=sea - 5).contains(&y) - && w.fluid(x, y - 1, z) == Fluid::Water - && w.block_name(x, y + 1, z) == "minecraft:water" -} /// `getControllingPassenger`: a player on a saddled nautilus. pub fn controlled(m: &Mob) -> bool { m.rider.is_some() && m.extra.saddled diff --git a/crates/horizon-client/src/mobs/nav.rs b/crates/horizon-client/src/mobs/nav.rs index 24dafa48d..793303c6d 100644 --- a/crates/horizon-client/src/mobs/nav.rs +++ b/crates/horizon-client/src/mobs/nav.rs @@ -582,26 +582,4 @@ impl Mob { NavKind::Amphibious => !w.facts.is_air(w.world.block(p[0], p[1] - 1, p[2])), } } - pub fn should_recompute_path(&self, p: [i32; 3]) -> bool { - if self.ai.nav.delayed_recompute { - return false; - } - let Some(path) = &self.ai.nav.path else { - return false; - }; - if path.is_done() || path.nodes.is_empty() { - return false; - } - let Some(end) = path.end() else { return false }; - let mid = [ - (f64::from(end.x) + self.position.x) / 2.0, - (f64::from(end.y) + self.position.y) / 2.0, - (f64::from(end.z) + self.position.z) / 2.0, - ]; - let d = (f64::from(p[0]) + 0.5 - mid[0]).powi(2) - + (f64::from(p[1]) + 0.5 - mid[1]).powi(2) - + (f64::from(p[2]) + 0.5 - mid[2]).powi(2); - let r = (path.nodes.len() - path.next) as f64; - d < r * r - } } diff --git a/crates/horizon-client/src/mobs/spawn_selection.rs b/crates/horizon-client/src/mobs/spawn_selection.rs index 046583a1f..4e51722f1 100644 --- a/crates/horizon-client/src/mobs/spawn_selection.rs +++ b/crates/horizon-client/src/mobs/spawn_selection.rs @@ -769,19 +769,6 @@ pub fn weighted_pick(list: &[SpawnEntry], random: &mut impl RandomSource) -> Opt None } -/// `NaturalSpawner.getRandomSpawnMobAt`. -pub fn random_spawn_mob_at( - list: &[SpawnEntry], - category: &str, - reduced_water_ambient: bool, - random: &mut impl RandomSource, -) -> Option { - if category == "water_ambient" && reduced_water_ambient && random.next_float() < 0.98 { - return None; - } - weighted_pick(list, random) -} - /// `SpawnerData.count().sample(random)`: a constant draws nothing. pub fn sample_count(entry: &SpawnEntry, random: &mut impl RandomSource) -> u32 { if entry.constant { diff --git a/crates/horizon-client/src/mobs/spawning.rs b/crates/horizon-client/src/mobs/spawning.rs index 0bfd5d223..93e3fe119 100644 --- a/crates/horizon-client/src/mobs/spawning.rs +++ b/crates/horizon-client/src/mobs/spawning.rs @@ -30,25 +30,6 @@ pub struct BiomeFacts { pub fn placement_ok(species: Species, w: &MobWorld, x: i32, y: i32, z: i32) -> bool { super::spawn_rules::is_spawn_position_ok(species, w, x, y, z) } -/// `SpawnPlacements.checkSpawnRules` for a natural spawn. -pub fn spawn_rules_ok( - species: Species, - w: &MobWorld, - random: &mut impl RandomSource, - x: i32, - y: i32, - z: i32, -) -> bool { - super::spawn_rules::check_spawn_rules( - species, - w, - super::spawn_rules::Reason::Natural, - random, - x, - y, - z, - ) -} /// Mob.finalizeSpawn for natural, chunk-generation and summoned mobs. /// `DifficultyInstance`: the local difficulty at a position. #[derive(Clone, Copy, Debug)] diff --git a/crates/horizon-client/src/mobs/villager_brain.rs b/crates/horizon-client/src/mobs/villager_brain.rs index e02cfa176..194bd75ff 100644 --- a/crates/horizon-client/src/mobs/villager_brain.rs +++ b/crates/horizon-client/src/mobs/villager_brain.rs @@ -10,7 +10,7 @@ //! other `GateBehavior`s shuffle with a random of their own, seeded with //! `Brain::shuffle_seed` when the brain is built. use super::{ - Mob, MobEvent, MobWorld, Seen, Species, Who, + Mob, MobEvent, MobWorld, Species, Who, path::{Path, PathNode}, }; use horizon_core::{ @@ -4156,10 +4156,6 @@ pub fn woken(m: &mut Mob, now: i64) { pub fn potential_job_site(m: &Mob) -> Option<[i32; 3]> { mem_ref(m).and_then(|mm| mm.potential_job_site.value()) } -/// Whether a seen mob is a villager wanting to breed (`AgeableMob::canBreed`). -pub fn seen_can_breed(s: &Seen) -> bool { - s.species == Species::Villager && s.in_love -} /// The memories other villagers read: the interaction target, the path's /// nodes (while it is not done), whether a golem was seen lately and the /// last sleep. diff --git a/crates/horizon-client/src/mobs/world.rs b/crates/horizon-client/src/mobs/world.rs index 79bbaa30c..7836a49ef 100644 --- a/crates/horizon-client/src/mobs/world.rs +++ b/crates/horizon-client/src/mobs/world.rs @@ -1,7 +1,7 @@ //! What mobs read from the level: block solidity, ray clipping for line of //! sight (`ClipContext.Block.COLLIDER`), sky visibility, light-dependent //! values and the positions of other entities. -use super::{MobWorld, Seen, Species, Who, data}; +use super::{MobWorld, Seen, Who, data}; use crate::{ blocks::{BlockFacts, Fluid}, collisions::SceneCollisions, @@ -269,12 +269,6 @@ impl MobWorld<'_> { }); v.into_iter() } - pub fn count_species(&self, species: Species) -> usize { - self.others - .iter() - .filter(|s| s.species == species && s.alive) - .count() - } } /// Level.clip with VISUAL shapes and no fluids: the block the segment first diff --git a/crates/horizon-client/src/packs.rs b/crates/horizon-client/src/packs.rs index 7bd4fd782..5daaaacad 100644 --- a/crates/horizon-client/src/packs.rs +++ b/crates/horizon-client/src/packs.rs @@ -804,11 +804,6 @@ impl SelectionModel { false } } - /// Whether activating the entry asks first (`handlePackSelection`'s - /// ConfirmScreen for an incompatible pack). - pub fn needs_confirmation(&self, selected: bool, i: usize) -> bool { - self.can_select(selected, i) && !self.unselected[i].compatible - } /// `moveUp` / `moveDown` when allowed. pub fn move_by(&mut self, selected: bool, i: usize, up: bool) -> bool { let ok = if up { diff --git a/crates/horizon-client/src/recipe_book.rs b/crates/horizon-client/src/recipe_book.rs index 27051bbc6..fa247a695 100644 --- a/crates/horizon-client/src/recipe_book.rs +++ b/crates/horizon-client/src/recipe_book.rs @@ -384,21 +384,6 @@ impl Book { order, } } - /// Which book a recipe id belongs to. - pub fn book_of(&self, id: &str) -> Option { - let e = self.entries.iter().find(|e| e.id == id)?; - Some(match e.category { - Category::CraftingBuilding - | Category::CraftingRedstone - | Category::CraftingEquipment - | Category::CraftingMisc => BookType::Crafting, - Category::FurnaceFood | Category::FurnaceBlocks | Category::FurnaceMisc => { - BookType::Furnace - } - Category::BlastBlocks | Category::BlastMisc => BookType::BlastFurnace, - Category::SmokerFood => BookType::Smoker, - }) - } /// `RecipeDisplayEntry.canCraft`: the recipe's placement ingredients /// (`craftingRequirements`) can all be found in `stock`. pub fn can_craft(&self, entry: &Entry, stock: &crate::menu::place::Stacked) -> bool { diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index dd86c8f9c..7819a9b0b 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -750,12 +750,6 @@ fn heightmaps(chunk: &Chunk, tables: &SaveTables) -> Tag { ) })) } -/// The heightmaps a ClientboundLevelChunkPacketData carries (the types with -/// sendToClient: WORLD_SURFACE, MOTION_BLOCKING, MOTION_BLOCKING_NO_LEAVES), -/// as (network id, packed longs) in Heightmap.Types order. -pub fn client_heightmaps(chunk: &Chunk, tables: &SaveTables) -> Vec<(i32, Vec)> { - client_heightmaps_in(&chunk.sections, tables, MIN_Y, SECTIONS as i32 * 16) -} /// The client heightmaps of a column's sections, in a level from `min_y`, /// `height` blocks high. pub fn client_heightmaps_in( diff --git a/crates/horizon-client/src/sculk.rs b/crates/horizon-client/src/sculk.rs index 58ac921ff..3affbda77 100644 --- a/crates/horizon-client/src/sculk.rs +++ b/crates/horizon-client/src/sculk.rs @@ -78,7 +78,6 @@ pub const RESONANCE: [&str; 15] = [ ]; pub const SHRIEK: &str = "minecraft:shriek"; pub const TENDRILS_CLICKING: &str = "minecraft:sculk_sensor_tendrils_clicking"; -pub const NO_VIBRATION_FREQUENCY: i32 = 0; /// `VibrationSystem.getGameEventFrequency`. pub fn frequency(event: &str) -> i32 { @@ -567,10 +566,6 @@ pub struct WardenSpawnTracker { } impl WardenSpawnTracker { pub const MAX_WARNING_LEVEL: i32 = 4; - /// Blocks around a shrieker within which players are warned. - pub const PLAYER_SEARCH_RADIUS: f64 = 16.0; - /// `AABB.ofSize(centre, 48, 48, 48)` for an existing warden. - pub const WARNING_CHECK_DIAMETER: f64 = 48.0; const DECREASE_EVERY: i32 = 12000; const INCREASE_COOLDOWN: i32 = 200; pub fn tick(&mut self) { diff --git a/crates/horizon-client/src/settings.rs b/crates/horizon-client/src/settings.rs index 1fadacf57..97acc91c9 100644 --- a/crates/horizon-client/src/settings.rs +++ b/crates/horizon-client/src/settings.rs @@ -81,10 +81,6 @@ pub fn sound_source(event: &str) -> usize { index("master") } } -/// Volume multiplier of an event's source (master applied separately). -pub fn event_volume(event: &str) -> f32 { - source_volume(sound_source(event)) -} /// Volume multiplier of a sound source by index. pub fn source_volume(i: usize) -> f32 { if i == 0 { diff --git a/crates/horizon-client/src/tab_list.rs b/crates/horizon-client/src/tab_list.rs index 2c03f999f..0374e3fc2 100644 --- a/crates/horizon-client/src/tab_list.rs +++ b/crates/horizon-client/src/tab_list.rs @@ -317,9 +317,6 @@ impl TabList { self.visible = visible; } } - pub fn is_visible(&self) -> bool { - self.visible - } /// `reset`. pub fn reset(&mut self) { self.header = None; diff --git a/crates/horizon-client/src/text.rs b/crates/horizon-client/src/text.rs index 9f39155d1..11abb3328 100644 --- a/crates/horizon-client/src/text.rs +++ b/crates/horizon-client/src/text.rs @@ -455,9 +455,6 @@ impl Text { } out } - pub fn char_count(&self) -> usize { - self.spans.iter().map(|s| s.text.chars().count()).sum() - } } /// The style fields of a component's JSON. diff --git a/crates/horizon-client/src/title.rs b/crates/horizon-client/src/title.rs index b4958c600..f77ac93eb 100644 --- a/crates/horizon-client/src/title.rs +++ b/crates/horizon-client/src/title.rs @@ -1630,26 +1630,6 @@ fn caret_field<'a>( _ => None, } } -/// `yyyy/MM/dd HH:mm` (UTC) for a modification time in Unix seconds. -pub fn date_label(secs: u64) -> String { - date_string(secs) -} -fn date_string(secs: u64) -> String { - // Civil date from Unix time (days-from-civil inverse), UTC. - let days = (secs / 86400) as i64; - let (h, m) = ((secs % 86400) / 3600, (secs % 3600) / 60); - let z = days + 719_468; - let era = z.div_euclid(146_097); - let doe = z - era * 146_097; - let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146_096) / 365; - let y = yoe + era * 400; - let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); - let mp = (5 * doy + 2) / 153; - let d = doy - (153 * mp + 2) / 5 + 1; - let mo = if mp < 10 { mp + 3 } else { mp - 9 }; - let y = if mo <= 2 { y + 1 } else { y }; - format!("{y}-{mo:02}-{d:02} {h:02}:{m:02}") -} #[cfg(test)] mod classic_tests { diff --git a/crates/horizon-client/src/ui.rs b/crates/horizon-client/src/ui.rs index b1ffab866..63a6d1de7 100644 --- a/crates/horizon-client/src/ui.rs +++ b/crates/horizon-client/src/ui.rs @@ -793,10 +793,6 @@ impl EditBox { text_color: rgba(224, 224, 224, 255), } } - /// When the field gained focus, for the cursor's blink. - pub fn focused_since(&mut self, at: Instant) { - self.focused_at = at; - } pub fn inner_width(&self) -> f32 { if self.bordered { self.width - 8.0 diff --git a/crates/horizon-client/src/worldborder.rs b/crates/horizon-client/src/worldborder.rs index 3ed4ea152..3673db403 100644 --- a/crates/horizon-client/src/worldborder.rs +++ b/crates/horizon-client/src/worldborder.rs @@ -7,7 +7,6 @@ pub use horizon_core::geometry::border::BorderStatus; /// `WorldBorder.MAX_SIZE` (the float 5.999997E7 as a double). pub const MAX_SIZE: f64 = 59_999_968.0; -pub const MAX_CENTER: f64 = 2.999_998_4e7; /// `BorderStatus.getColor`: the wall colour. pub fn status_color(status: BorderStatus) -> u32 { diff --git a/crates/horizon-core/src/density/beardifier.rs b/crates/horizon-core/src/density/beardifier.rs index 5dec64189..b041be160 100644 --- a/crates/horizon-core/src/density/beardifier.rs +++ b/crates/horizon-core/src/density/beardifier.rs @@ -168,9 +168,6 @@ impl Beardifier { self.kernel_junctions = junctions; self } - pub fn affected_bounds(&self) -> Option { - self.affected - } fn sample_unchecked(&self, [x, y, z]: [i32; 3]) -> f32 { let mut value = 0.; // The kernel is a process-wide constant; resolving it once keeps the diff --git a/crates/horizon-core/src/geometry/shape/join.rs b/crates/horizon-core/src/geometry/shape/join.rs index 57c981e65..261c33802 100644 --- a/crates/horizon-core/src/geometry/shape/join.rs +++ b/crates/horizon-core/src/geometry/shape/join.rs @@ -13,8 +13,6 @@ impl ShapeOp { pub const XOR: Self = Self(6); pub const ONLY_FIRST: Self = Self(4); pub const ONLY_SECOND: Self = Self(2); - pub const FIRST: Self = Self(12); - pub const SECOND: Self = Self(10); pub const fn from_truth_table(bits: u8) -> Self { Self(bits & 15) } diff --git a/crates/horizon-core/src/paletted.rs b/crates/horizon-core/src/paletted.rs index d9ade23f2..802eac225 100644 --- a/crates/horizon-core/src/paletted.rs +++ b/crates/horizon-core/src/paletted.rs @@ -466,34 +466,6 @@ impl PalettedContainer { } } } - /// Block ids as 16-bit values (callers whose registry fits in 16 bits). - pub fn unpack_u16(&self, out: &mut [u16; 4096]) { - debug_assert_eq!(self.strategy.entries(), 4096); - match &self.palette { - Palette::Single(value) => out.fill(*value as u16), - Palette::List(list) => { - let bits = usize::from(self.bits); - let per = 64 / bits; - let m = mask(self.bits); - for (chunk, &word) in out.chunks_mut(per).zip(&self.words) { - for (slot, cell) in chunk.iter_mut().enumerate() { - let id = ((word >> (slot * bits)) & m) as usize; - *cell = list[id] as u16; - } - } - } - Palette::Global => { - let bits = usize::from(self.bits); - let per = 64 / bits; - let m = mask(self.bits); - for (chunk, &word) in out.chunks_mut(per).zip(&self.words) { - for (slot, cell) in chunk.iter_mut().enumerate() { - *cell = ((word >> (slot * bits)) & m) as u16; - } - } - } - } - } /// A container holding `values` in the smallest configuration for their /// distinct count, with palette ids in first-occurrence order, like a /// container unpacked from disk. diff --git a/crates/horizon-core/src/ticks/generation.rs b/crates/horizon-core/src/ticks/generation.rs index 4e1fdce6f..f81d9353e 100644 --- a/crates/horizon-core/src/ticks/generation.rs +++ b/crates/horizon-core/src/ticks/generation.rs @@ -91,9 +91,6 @@ impl WorldGenTickAccess { pub fn containers_mut(&mut self) -> &mut C { &mut self.containers } - pub fn into_containers(self) -> C { - self.containers - } pub fn schedule(&mut self, tick: ScheduledTick) -> Result { Ok(self.containers.container_at(tick.pos)?.schedule(tick)) } diff --git a/crates/horizon-core/src/ticks/mod.rs b/crates/horizon-core/src/ticks/mod.rs index f3cc83999..693aab130 100644 --- a/crates/horizon-core/src/ticks/mod.rs +++ b/crates/horizon-core/src/ticks/mod.rs @@ -636,7 +636,4 @@ impl LevelTicks { pub fn copy_area(&mut self, area: TickArea, offset: BlockPos) { self.schedule_copy(self.area_ticks(area), offset); } - pub fn copy_area_from(&mut self, source: &Self, area: TickArea, offset: BlockPos) { - self.schedule_copy(source.area_ticks(area), offset); - } } diff --git a/crates/horizon-core/src/version.rs b/crates/horizon-core/src/version.rs index 29994823c..cb3e85629 100644 --- a/crates/horizon-core/src/version.rs +++ b/crates/horizon-core/src/version.rs @@ -11,10 +11,6 @@ pub const PROTOCOL_VERSION: i32 = 777; pub const DATA_VERSION: i32 = 5023; /// `WorldVersion.packVersion(PackType.CLIENT_RESOURCES)`: (major, minor). pub const RESOURCE_PACK_FORMAT: (i64, i64) = (97, 1); -/// `WorldVersion.packVersion(PackType.SERVER_DATA)`: (major, minor). -pub const DATA_PACK_FORMAT: (i64, i64) = (121, 0); -/// The Java major version the game runs on. -pub const JAVA_VERSION: u32 = 25; /// `WorldVersion.stable()`: false for snapshots and pre-releases. pub const STABLE: bool = true; /// `WorldVersion.buildTime()` as `Date.toString()` prints it in UTC (`/version`). diff --git a/crates/horizon-friends/src/guest.rs b/crates/horizon-friends/src/guest.rs index 4f64bb1cc..492f4d482 100644 --- a/crates/horizon-friends/src/guest.rs +++ b/crates/horizon-friends/src/guest.rs @@ -296,14 +296,4 @@ impl Tunnel { })?; Ok((port, thread)) } - /// The protected stream itself (tests, or a client that reads it - /// directly). - pub fn into_stream( - self, - ) -> ( - crypto::SecureReader, - crypto::SecureWriter, - ) { - self.secure - } } diff --git a/crates/horizon-server-files/src/user_cache.rs b/crates/horizon-server-files/src/user_cache.rs index c8ccd6cf0..38e779488 100644 --- a/crates/horizon-server-files/src/user_cache.rs +++ b/crates/horizon-server-files/src/user_cache.rs @@ -190,14 +190,6 @@ impl UserCache { result } - /// `get(UUID)`: a cached player by id (expired or not). - pub fn get_by_id(&mut self, id: u128) -> Option { - let index = *self.by_uuid.get(&id)?; - self.operations += 1; - self.infos[index].last_access = self.operations; - Some(self.infos[index].user.clone()) - } - /// The file's text: the 1000 most recently used entries, most recent /// first, as Gson writes them (compact, HTML-safe). pub fn to_json(&self) -> String { diff --git a/crates/horizon-server-files/src/user_lists.rs b/crates/horizon-server-files/src/user_lists.rs index 77de64e31..5bef4f38f 100644 --- a/crates/horizon-server-files/src/user_lists.rs +++ b/crates/horizon-server-files/src/user_lists.rs @@ -416,11 +416,6 @@ pub type WhiteList = UserList; pub type BanList = UserList>; pub type IpBanList = UserList>; -/// IpBanList.getIpFromAddress. -pub fn ip_of(address: &std::net::SocketAddr) -> String { - address.ip().to_string() -} - #[cfg(test)] mod tests { use super::*; diff --git a/crates/horizon-world/src/block_entities.rs b/crates/horizon-world/src/block_entities.rs index 22a2bc959..1ee8a90e0 100644 --- a/crates/horizon-world/src/block_entities.rs +++ b/crates/horizon-world/src/block_entities.rs @@ -62,9 +62,6 @@ impl ProtoBlockEntities { pub fn active(&self, position: BlockPos) -> Option<&E> { self.active.get(&position) } - pub fn active_mut(&mut self, position: BlockPos) -> Option<&mut E> { - self.active.get_mut(&position) - } pub fn pending(&self, position: BlockPos) -> Option<&Tag> { self.pending.get(&position) } diff --git a/crates/horizon-world/src/block_render.rs b/crates/horizon-world/src/block_render.rs index 1ed9c51af..aa10d361c 100644 --- a/crates/horizon-world/src/block_render.rs +++ b/crates/horizon-world/src/block_render.rs @@ -157,12 +157,6 @@ impl FaceVisibility { } } } - /// Whether the state's occlusion face toward `direction` is the full square. - pub fn full_face(&self, state: BlockStateId, direction: Direction) -> bool { - self.states - .get(state.0 as usize) - .is_some_and(|s| s.faces[direction as usize] == 1) - } /// The state's occlusion face toward `direction` (`getFaceOcclusionShape`) and /// its shared ID: 0 is the empty singleton, 1 the full-block singleton. pub fn occlusion_face( @@ -173,12 +167,6 @@ impl FaceVisibility { let id = self.states.get(state.0 as usize)?.faces[direction as usize]; Some((id, &self.faces[id as usize])) } - /// Whether the state's occlusion face toward `direction` is empty. - pub fn empty_face(&self, state: BlockStateId, direction: Direction) -> bool { - self.states - .get(state.0 as usize) - .is_none_or(|s| s.faces[direction as usize] == 0) - } pub fn skip_rendering( &self, state: BlockStateId, diff --git a/crates/horizon-world/src/block_support.rs b/crates/horizon-world/src/block_support.rs index 295999d18..d3ac386cd 100644 --- a/crates/horizon-world/src/block_support.rs +++ b/crates/horizon-world/src/block_support.rs @@ -186,25 +186,6 @@ impl<'r> BlockSupportRegistry<'r> { pub fn registry(&self) -> &'r BlockRegistry { self.cached.registry() } - pub fn support_shape( - &self, - world: &mut W, - state: BlockStateId, - position: [i32; 3], - ) -> Result> { - if let Some(shape) = self - .cached - .cached_shape(state) - .map_err(SupportQueryError::Data)? - { - return Ok(shape.clone()); - } - self.dynamic - .get(&state) - .ok_or(SupportQueryError::Unsupported(state))? - .shape(world, position) - .map_err(SupportQueryError::Dynamic) - } pub fn is_face_sturdy( &self, world: &mut W, diff --git a/crates/horizon-world/src/block_survival.rs b/crates/horizon-world/src/block_survival.rs index bb21f46df..ae53451ba 100644 --- a/crates/horizon-world/src/block_survival.rs +++ b/crates/horizon-world/src/block_survival.rs @@ -524,14 +524,6 @@ impl<'r> BlockSurvivalRegistry<'r> { bubble_down: tag_states("minecraft:enables_bubble_column_drag_down")?, }) } - pub fn supported_states(&self) -> usize { - self.rules.iter().filter(|r| r.is_some()).count() - } - pub fn supports_state(&self, state: BlockStateId) -> bool { - self.rules - .get(state.0 as usize) - .is_some_and(Option::is_some) - } fn read( &self, world: &mut W, diff --git a/crates/horizon-world/src/component_durability.rs b/crates/horizon-world/src/component_durability.rs index 15237133b..5bf576c08 100644 --- a/crates/horizon-world/src/component_durability.rs +++ b/crates/horizon-world/src/component_durability.rs @@ -3,7 +3,6 @@ use crate::{ component_patch::{ComponentMap, MissingPrototype, Patch, Prototype}, item_durability::DurabilityStorage, - item_use_catalog::UseComponents, }; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Key { @@ -52,13 +51,6 @@ impl DurabilityPrototype { } Self(Prototype::new(entries)) } - pub fn from_defaults(defaults: &UseComponents) -> Self { - Self::new([ - Change::MaxDamage(defaults.max_damage), - Change::Damage(defaults.damage), - Change::Unbreakable(defaults.unbreakable), - ]) - } } #[derive(Clone, Debug)] pub struct DurabilityPatch(Patch); diff --git a/crates/horizon-world/src/datafix/dynamic.rs b/crates/horizon-world/src/datafix/dynamic.rs index 44336729c..69cca1453 100644 --- a/crates/horizon-world/src/datafix/dynamic.rs +++ b/crates/horizon-world/src/datafix/dynamic.rs @@ -157,12 +157,6 @@ pub fn map(tag: &Tag) -> Option<&Compound> { _ => None, } } -pub fn map_mut(tag: &mut Tag) -> Option<&mut Compound> { - match tag { - Tag::Compound(c) => Some(c), - _ => None, - } -} /// `Dynamic.set`: only maps change. pub fn set(tag: &mut Tag, field: &str, value: Tag) { if let Tag::End = tag { @@ -193,12 +187,6 @@ pub fn update(tag: &mut Tag, field: &str, f: impl FnOnce(Tag) -> Tag) { set(tag, field, f(v)); } } -/// `Dynamic.update` in place. -pub fn update_mut(tag: &mut Tag, field: &str, f: impl FnOnce(&mut Tag)) { - if let Some(v) = tag.get_mut(field) { - f(v); - } -} /// `Dynamic.renameAndFixField`: the fixed value under the new name, removed when /// the fix gives nothing. pub fn rename_and_fix(tag: &mut Tag, from: &str, to: &str, f: impl FnOnce(Tag) -> Option) { @@ -223,15 +211,6 @@ pub fn is_empty_map(tag: &Tag) -> bool { pub fn or_empty_map(tag: Option<&Tag>) -> Tag { tag.cloned().unwrap_or_else(empty_map) } -/// Apply `f` to every element of a list field (`Dynamic.createList(asStream().map(f))`). -pub fn map_list(tag: &mut Tag, field: &str, mut f: impl FnMut(Tag) -> Tag) { - if let Some(v) = tag.get(field) - && is_list(Some(v)) - { - let list = stream(Some(v)).into_iter().map(&mut f).collect(); - set(tag, field, Tag::List(list)); - } -} /// `NamespacedSchema.ensureNamespaced`. pub fn namespaced(id: &str) -> String { match crate::datafix::identifier(id) { diff --git a/crates/horizon-world/src/entity_move.rs b/crates/horizon-world/src/entity_move.rs index b3b189df0..25d7aef14 100644 --- a/crates/horizon-world/src/entity_move.rs +++ b/crates/horizon-world/src/entity_move.rs @@ -44,9 +44,6 @@ impl MovementHistory { } self.records.push_back(record); } - pub fn remove_latest(&mut self) { - self.records.pop_back(); - } pub fn clear(&mut self) { self.records.clear(); } diff --git a/crates/horizon-world/src/entity_obstruction.rs b/crates/horizon-world/src/entity_obstruction.rs index 89f7f586b..aac2306b6 100644 --- a/crates/horizon-world/src/entity_obstruction.rs +++ b/crates/horizon-world/src/entity_obstruction.rs @@ -73,17 +73,3 @@ pub fn is_unobstructed( } Ok(true) } - -/// Entity obstruction for the unit cube used by the vanilla block predicate. -/// Translate the voxel shape in floating point: integer MAX + 1 must not wrap. -pub fn is_block_unobstructed( - world: &mut W, - position: [i32; 3], -) -> Result> { - let shape = VoxelShape::block().moved(horizon_core::geometry::Vec3::new( - f64::from(position[0]), - f64::from(position[1]), - f64::from(position[2]), - )); - is_unobstructed(world, None, &shape) -} diff --git a/crates/horizon-world/src/fluid.rs b/crates/horizon-world/src/fluid.rs index 4a89764a8..ab03d6a52 100644 --- a/crates/horizon-world/src/fluid.rs +++ b/crates/horizon-world/src/fluid.rs @@ -272,13 +272,6 @@ impl FluidStateTable { pub fn get(&self, state: BlockStateId) -> Option { self.0.get(state.0 as usize).copied() } - pub fn matching_states(&self, mask: u8) -> impl Iterator + '_ { - self.0 - .iter() - .enumerate() - .filter(move |(_, state)| state.kind.mask() & mask != 0) - .map(|(i, _)| BlockStateId(i as u32)) - } } /// Bound identities for converting fluid states into their legacy liquid blocks. diff --git a/crates/horizon-world/src/level/bonemeal.rs b/crates/horizon-world/src/level/bonemeal.rs index 274512138..ef52ca625 100644 --- a/crates/horizon-world/src/level/bonemeal.rs +++ b/crates/horizon-world/src/level/bonemeal.rs @@ -160,16 +160,6 @@ impl Behaviours { ) -> bool { self.sim(level, ticks).grow_water_plant(pos, face) } - /// The `BonemealableBlock.Type` particle spread of a block at level event - /// 1505: Some(true) for `NEIGHBOR_SPREADER` (particles over a 3-block - /// area above), Some(false) for `GROWER`, None when not bonemealable. - pub fn bone_meal_spreads(&self, state: BlockStateId) -> Option { - let kind = self.bonemeal_kind(state)?; - Some(matches!( - kind, - Kind::Grass | Kind::Nylium | Kind::Netherrack | Kind::FeaturePlacer - )) - } fn bonemeal_kind(&self, state: BlockStateId) -> Option { let data = self.data(state); if !data.implements(Interface::Bonemealable) { diff --git a/crates/horizon-world/src/level/classes.rs b/crates/horizon-world/src/level/classes.rs index 9540a5a47..2d0193de5 100644 --- a/crates/horizon-world/src/level/classes.rs +++ b/crates/horizon-world/src/level/classes.rs @@ -15,9 +15,6 @@ macro_rules! classes { pub fn from_java(name: &str) -> Option { match name { $($java => Some(Class::$name),)* _ => None } } - pub const fn java_name(self) -> &'static str { - match self { $(Class::$name => $java,)* } - } } }; } diff --git a/crates/horizon-world/src/level/mod.rs b/crates/horizon-world/src/level/mod.rs index e343e06a7..696777386 100644 --- a/crates/horizon-world/src/level/mod.rs +++ b/crates/horizon-world/src/level/mod.rs @@ -148,7 +148,6 @@ pub mod flags { pub const UPDATE_NEIGHBORS: i32 = 1; pub const UPDATE_CLIENTS: i32 = 2; pub const UPDATE_INVISIBLE: i32 = 4; - pub const UPDATE_IMMEDIATE: i32 = 8; pub const UPDATE_KNOWN_SHAPE: i32 = 16; pub const UPDATE_SUPPRESS_DROPS: i32 = 32; pub const UPDATE_MOVE_BY_PISTON: i32 = 64; diff --git a/crates/horizon-world/src/level/states.rs b/crates/horizon-world/src/level/states.rs index c7fe012dc..8d2544953 100644 --- a/crates/horizon-world/src/level/states.rs +++ b/crates/horizon-world/src/level/states.rs @@ -348,10 +348,4 @@ impl StateTable { } result } - pub fn property_names(&self, block: BlockId) -> impl Iterator { - self.info(block) - .slots - .iter() - .map(|slot| self.key_names[slot.key.0 as usize].as_str()) - } } diff --git a/crates/horizon-world/src/level/ticks.rs b/crates/horizon-world/src/level/ticks.rs index fbad6c28d..c8d715623 100644 --- a/crates/horizon-world/src/level/ticks.rs +++ b/crates/horizon-world/src/level/ticks.rs @@ -149,9 +149,6 @@ impl Ticks { self.blocks .has_scheduled(pos, TickTypeId(u32::from(block.0))) } - pub fn has_fluid_tick(&self, pos: BlockPos, fluid: FluidKind) -> bool { - self.fluids.has_scheduled(pos, TickTypeId(fluid as u32)) - } pub fn will_block_tick_this_tick(&mut self, pos: BlockPos, block: BlockId) -> bool { self.blocks .will_tick_this_tick(pos, TickTypeId(u32::from(block.0))) diff --git a/crates/horizon-world/src/registry_fluid.rs b/crates/horizon-world/src/registry_fluid.rs index eb0f4ffe0..a122a8978 100644 --- a/crates/horizon-world/src/registry_fluid.rs +++ b/crates/horizon-world/src/registry_fluid.rs @@ -62,9 +62,6 @@ impl<'r> FluidRegistry<'r> { pub fn blocks_flow(&self, id: BlockStateId) -> io::Result { Ok(self.state(id)?.blocks_flow) } - pub fn is_ice(&self, id: BlockStateId) -> io::Result { - Ok(self.state(id)?.ice) - } } /// Both queries return state IDs from the same registry. Separate entry points /// preserve native world dispatch when reading a block versus its fluid cell. diff --git a/crates/horizon-world/src/retrogen.rs b/crates/horizon-world/src/retrogen.rs index fc9f450bf..2f048f48e 100644 --- a/crates/horizon-world/src/retrogen.rs +++ b/crates/horizon-world/src/retrogen.rs @@ -33,9 +33,6 @@ impl BelowZeroRetrogen { .get(index / 64) .is_some_and(|word| word & (1_u64 << (index % 64)) != 0) } - pub fn missing_bedrock_words(&self) -> &[u64] { - &self.missing_bedrock - } pub fn from_nbt(tag: &Tag) -> io::Result { if !matches!(tag, Tag::Compound(_)) { return Err(invalid("retrogen is not a compound")); diff --git a/crates/horizon-world/src/worldgen/aquifer.rs b/crates/horizon-world/src/worldgen/aquifer.rs index 2b4f6e7c3..44500313b 100644 --- a/crates/horizon-world/src/worldgen/aquifer.rs +++ b/crates/horizon-world/src/worldgen/aquifer.rs @@ -50,11 +50,6 @@ impl GlobalFluidPicker { disabled: false, } } - /// Matches the vanilla developer flag. Regular gameplay keeps this false. - pub const fn with_disabled(mut self, disabled: bool) -> Self { - self.disabled = disabled; - self - } } impl FluidPicker for GlobalFluidPicker { fn compute(&self, [_, y, _]: [i32; 3]) -> FluidStatus { diff --git a/crates/horizon-world/src/worldgen/feature_region.rs b/crates/horizon-world/src/worldgen/feature_region.rs index b4e8c07b0..e1d80e8bc 100644 --- a/crates/horizon-world/src/worldgen/feature_region.rs +++ b/crates/horizon-world/src/worldgen/feature_region.rs @@ -212,9 +212,6 @@ impl<'r, B: TerrainBlockStates> TerrainRegion<'r, B> { pub fn tick_clock(&self) -> (i64, i64) { (self.game_time, self.next_tick_order) } - pub fn set_game_time(&mut self, game_time: i64) { - self.game_time = game_time; - } /// Raw tick identities are default block-state IDs for block ticks and /// `FluidKind` discriminants for fluid ticks, in separate namespaces. pub fn schedule_tick( @@ -224,15 +221,6 @@ impl<'r, B: TerrainBlockStates> TerrainRegion<'r, B> { ) -> Result { WorldGenTickAccess::new(RegionTickContainers { region: self, kind }).schedule(tick) } - pub fn has_scheduled_tick( - &mut self, - kind: TickKind, - position: BlockPos, - tick_type: TickTypeId, - ) -> Result { - WorldGenTickAccess::new(RegionTickContainers { region: self, kind }) - .has_scheduled(position, tick_type) - } fn request_tick( &mut self, kind: TickKind, @@ -251,11 +239,6 @@ impl<'r, B: TerrainBlockStates> TerrainRegion<'r, B> { self.schedule_tick(kind, tick)?; Ok(()) } - /// Override world sea level without changing generator placement anchors. - pub fn with_world_sea_level(mut self, sea_level: i32) -> Self { - self.sea_level = sea_level; - self - } /// Release the resident chunks, ordered by X then Z; match them by position. pub fn into_chunks(self) -> Vec> { self.chunks diff --git a/crates/horizon-world/src/worldgen/material_condition/codec.rs b/crates/horizon-world/src/worldgen/material_condition/codec.rs index ec646697e..aa159bd1e 100644 --- a/crates/horizon-world/src/worldgen/material_condition/codec.rs +++ b/crates/horizon-world/src/worldgen/material_condition/codec.rs @@ -65,12 +65,6 @@ impl MaterialConditionRegistry { pub fn from_json(definitions: &BTreeMap) -> Result { Self::load(definitions, None) } - pub fn from_json_with_biomes( - definitions: &BTreeMap, - biomes: &BTreeSet, - ) -> Result { - Self::from_json_with_biome_tags(definitions, biomes, &BTreeMap::new()) - } /// Bind resolved biome tags, such as `TagDefinitions::build().tags`. /// Names and memberships remain immutable for the lifetime of this registry. pub fn from_json_with_biome_tags( diff --git a/crates/horizon-world/src/worldgen/state_provider.rs b/crates/horizon-world/src/worldgen/state_provider.rs index 098decdf1..da39cd246 100644 --- a/crates/horizon-world/src/worldgen/state_provider.rs +++ b/crates/horizon-world/src/worldgen/state_provider.rs @@ -344,12 +344,6 @@ impl BlockStateProvider { _ => Err(DecodeError::Field("exclusive state or provider")), } } - pub fn from_typed_json( - json: &str, - bindings: &StateProviderBindings<'_>, - ) -> Result { - Self::decode_typed(json, bindings, 0, &mut 65536, None) - } fn decode_typed( json: &str, bindings: &StateProviderBindings<'_>, diff --git a/crates/horizon-world/src/worldgen/terrain.rs b/crates/horizon-world/src/worldgen/terrain.rs index 8ea3b01be..5ecc48045 100644 --- a/crates/horizon-world/src/worldgen/terrain.rs +++ b/crates/horizon-world/src/worldgen/terrain.rs @@ -432,18 +432,6 @@ impl<'r, B: TerrainBlockStates> TerrainChunk<'r, B> { )?; Ok(()) } - pub fn set_heightmap_raw( - &mut self, - kind: HeightmapType, - words: &[u64], - ) -> Result<(), TerrainError> { - self.ensure_heightmap(kind); - self.heightmaps[kind as usize] - .as_mut() - .unwrap() - .set_raw_data(&self.blocks, words)?; - Ok(()) - } /// Before replaying a saved chunk's `PostProcessing` lists. pub fn clear_postprocessing(&mut self) { self.postprocessing.iter_mut().for_each(Vec::clear); From 1d81c67b7b4a2ae05fc20bd6e8fc0b9691238df9 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 12:13:58 +0200 Subject: [PATCH 18/39] Build naga once for the game, its tests and its examples horizon-client's dev-dependency on naga added the hlsl-out feature for the gfx.rs test that translates every shader for DirectX 12. With resolver 2 a dev-dependency's features apply only to tests and examples, so Cargo kept two builds of naga, wgpu-hal, wgpu-core, wgpu, horizon-shaderpack and the horizon-client lib, and rebuilt them on the first example or test after a game build. Measured on the Mac mini (cargo build of the game, then one example): the example took 37.3 s and recompiled that whole stack before, 1.2 s and only its own crate after; the target dir is 1 GB smaller (4.9 to 3.9 GB). The game binary is the same size: nothing calls the HLSL writer, so the linker drops it. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/Cargo.toml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/crates/horizon-client/Cargo.toml b/crates/horizon-client/Cargo.toml index 48e2e57d4..cd5d86fe7 100644 --- a/crates/horizon-client/Cargo.toml +++ b/crates/horizon-client/Cargo.toml @@ -30,7 +30,9 @@ serde = { version = "1", features = ["derive"] } serde_json = { version = "1.0.151", features = ["raw_value"] } # Shader packs: pack.toml, and WGSL checked before it reaches wgpu. toml = "1.1" -naga = { version = "30.0.1", features = ["wgsl-in"] } +# hlsl-out: the gfx.rs tests translate every shader as wgpu's DirectX 12 backend does. A normal feature, +# not a dev-dependency's, so builds, tests and examples share one build of naga, wgpu and this crate. +naga = { version = "30.0.1", features = ["wgsl-in", "hlsl-out"] } # Java (OptiFine/Iris) shader packs, translated to WGSL. horizon-shaderpack = { path = "../horizon-shaderpack" } # Sound (ALSA on Linux); the `audio` feature. @@ -81,7 +83,3 @@ rng-trace = ["horizon-core/rng-trace"] [lints] workspace = true - -[dev-dependencies] -# Every WGSL shader translated as wgpu's DirectX 12 backend does (gfx.rs tests). -naga = { version = "30.0.1", features = ["wgsl-in", "hlsl-out"] } From 660407fa58d03fa6e41fe2edc7d4ae3658841233 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 13:22:10 +0200 Subject: [PATCH 19/39] Metal: cull only frames with 4,096 section draws or more On Apple's GPUs the passes the test adds cost a fixed share of the frame (it is stored and reloaded at each), which the hidden sections repay only at high render distances: at 12 culling cost an M4 Pro 0.05 ms of GPU time a frame, at 32 it saved 0.25 to 1.6 ms. Other backends keep culling every frame; HORIZON_GPU_CULL=all culls every frame on Metal too. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 14 ++++++++++---- crates/horizon-client/src/gfx/cull.rs | 10 ++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 91da4f616..022d14dcd 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -2146,8 +2146,8 @@ impl Renderer { .features() .contains(wgpu::Features::INDIRECT_FIRST_INSTANCE); // HORIZON_GPU_CULL=0 turns occlusion culling off, `all` culls every - // frame however little it hides, `shader` tests in the shader as on - // Metal (A/B checks). + // frame however few its draws and little it hides, `shader` tests in + // the shader as on Metal (A/B checks). let cull_env = std::env::var("HORIZON_GPU_CULL"); let cull = cull::Cull::supported(&adapter, &device) .filter(|_| cull_env.as_deref() != Ok("0")) @@ -2159,6 +2159,9 @@ impl Renderer { let mut cull = cull::Cull::new(&device, config.format.remove_srgb_suffix(), depth_test); cull.always = cull_env.as_deref() == Ok("all"); + if !cull.always && adapter.get_info().backend == wgpu::Backend::Metal { + cull.min_draws = cull::TILED_MIN_DRAWS; + } cull }); let depth = Self::depth(&device, &config, cull.as_ref()); @@ -3148,11 +3151,14 @@ impl Renderer { .inverse() .transform_point3(Vec3::ZERO); // Occlusion culling leaves the sections its last result did not see - // to phase 2, after the others, with instance counts of 0. + // to phase 2, after the others, with instance counts of 0. Whether + // the frame has draws enough goes by the last one's. let mut cull = self .cull .as_mut() - .filter(|_| self.cull_on && !frame.title && self.multi_draw) + .filter(|c| { + self.cull_on && !frame.title && self.multi_draw && self.section_draws >= c.min_draws + }) .and_then(|cull| cull.begin(&self.device).then_some(cull)); self.phase2_draws.clear(); self.phase2_pages.clear(); diff --git a/crates/horizon-client/src/gfx/cull.rs b/crates/horizon-client/src/gfx/cull.rs index 77574e850..57049eb4d 100644 --- a/crates/horizon-client/src/gfx/cull.rs +++ b/crates/horizon-client/src/gfx/cull.rs @@ -46,6 +46,13 @@ const RETEST: u32 = 8; /// more than they save. Else one frame in `PROBE` tests every section. const MIN_SHARE: f32 = 0.1; const PROBE: u32 = 64; +/// On Metal, section draws from which a frame culls. Apple's GPUs store and +/// reload the whole frame at each of the passes the test adds, a fixed cost +/// that the sections it hides repay only at high render distances: on an M4 +/// Pro at 1080p and render distance 12 (1,000–1,600 draws) culling added +/// about 0.05 ms of GPU time while hiding 15–30% of the solid indices; at +/// 32 (drawn flat, over this many draws) it saved 0.25–1.6 ms. +pub const TILED_MIN_DRAWS: usize = 4096; /// Boxes over more than this share of the screen count as seen untested. const BIG: f32 = 0.25; /// Results read back at once, some frames late so the CPU never waits. @@ -141,6 +148,8 @@ pub struct Cull { pub hidden: ([usize; 2], f32), /// Cull every frame, whatever the hidden share (A/B checks). pub always: bool, + /// Frames with fewer section draws than this do not cull. + pub min_draws: usize, } fn buffer( @@ -337,6 +346,7 @@ impl Cull { stamp: 0, hidden: ([0; 2], 1.0), always: false, + min_draws: 0, } } From eafe97da7468315d1bd159ccee038a8cee72e62d Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:02:12 +0200 Subject: [PATCH 20/39] Shader packs: keep what Iris does, drop what doesn't work, apply settings on Done Review fixes for #1414 (contribution gate): - Java packs no longer get Horizon's shadow map: it was mirrored in y and reversed in depth against the GL conventions the packs sample with, had no distortion and only the camera's sections, so BSL drew no shadows while the pass cost 0.65 ms (MakeUp, M4 Pro). shadowtex* stays a far depth, as on main; the packs' own shadow programs are #1326. - Invented uniforms removed (centerDepthSmooth fixed at 0.5, ambientOcclusionLevel and shadowMapResolution as uniforms, constant temp/rainfall/biome/held items); an unknown uniform stays zero as before. wetness keeps its Iris smoothing. Unused directives removed. - texture.. is scoped to Iris's stages (final reads the composite ones) instead of overriding the sampler in every pass; texture.noise replaces noisetex. One noise generator and one RGBA upload for both pack kinds. - The G-buffer pass, the opaque-depth copy and the shadow pass run only for a pack that reads them (naga's per-entry-point binding use, not a text search): every pack paid all three before. - Api 2's prelude additions go in their own file after api 1's, so api 1 packs keep the names they declare (aces_tonemap, material_at...). - Stage kinds shadow, gbuffer, deferred and final ran as composite: only composite is accepted; api 2 adds formats (validated), profiles, option descriptions and a noise texture. - Shader Pack Settings applies once when the page closes, as Iris's Done (each click or slider step rebuilt the pack: 2.7 s per click, 12.7 s for one drag with BSL). Labels follow Iris (.lang, else the name). A pack whose options can't be read isn't read again every frame. - Saved option values an option doesn't list are ignored, as OptiFine's ShaderOption.setValue (a stale file broke BSL's compile). - Import: the blocking file dialog (osascript/zenity/kdialog/PowerShell on the event loop) is gone; a pack dropped on the Shader Packs page is copied and picked, as Iris's onFilesDrop. Drops elsewhere do nothing, an existing pack isn't overwritten, links aren't followed. - Docs and the example say what the code does (SSAO, not GTAO). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 2 +- crates/horizon-client/src/game/options.rs | 3 - crates/horizon-client/src/game/packs.rs | 54 ---- .../horizon-client/src/game/shader_packs.rs | 29 +- .../horizon-client/src/game/startup_menu.rs | 29 +- crates/horizon-client/src/gfx.rs | 28 +- crates/horizon-client/src/gfx/java_stages.rs | 190 +++--------- crates/horizon-client/src/gfx/pack_stages.rs | 111 ++++--- crates/horizon-client/src/gfx/world_post.rs | 37 +-- crates/horizon-client/src/java_pack.rs | 67 +++-- crates/horizon-client/src/options_ui.rs | 221 +++++++++----- crates/horizon-client/src/shader_pack.rs | 281 ++++++++++-------- .../src/shader_pack_prelude.wgsl | 108 +------ .../src/shader_pack_prelude_api2.wgsl | 108 +++++++ crates/horizon-shaderpack/src/options.rs | 18 +- crates/horizon-shaderpack/src/pipeline.rs | 112 +------ crates/horizon-shaderpack/src/translate.rs | 19 +- docs/shader-packs.md | 74 ++++- examples/shaderpacks/horizon-ultra/pack.toml | 15 +- .../horizon-ultra/shaders/bloom_godrays.wgsl | 4 +- .../shaders/deferred_lighting.wgsl | 6 +- 21 files changed, 732 insertions(+), 784 deletions(-) create mode 100644 crates/horizon-client/src/shader_pack_prelude_api2.wgsl diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index c763bd58a..ae7b182b4 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -10728,7 +10728,7 @@ impl ApplicationHandler for Game<'_> { self.pending_save.as_mut().unwrap().quit = true; } WindowEvent::DroppedFile(path) => { - self.import_shader_pack_path(&path); + self.drop_shader_pack(&path); } WindowEvent::Focused(focused) => { self.focused = focused; diff --git a/crates/horizon-client/src/game/options.rs b/crates/horizon-client/src/game/options.rs index c715b4d1a..d422f0218 100644 --- a/crates/horizon-client/src/game/options.rs +++ b/crates/horizon-client/src/game/options.rs @@ -243,9 +243,6 @@ impl Game<'_> { super::packs::open_folder(&dir); } Event::Credits => self.open_win_screen(false, super::win::Finish::Options), - Event::ImportShaderPack => { - self.import_shader_pack_dialog(); - } Event::ReloadShaderPack => { self.load_shader_pack(); } diff --git a/crates/horizon-client/src/game/packs.rs b/crates/horizon-client/src/game/packs.rs index 13de550e1..94e61d948 100644 --- a/crates/horizon-client/src/game/packs.rs +++ b/crates/horizon-client/src/game/packs.rs @@ -34,60 +34,6 @@ pub fn open_folder(dir: &Path) { log::warn!("Horizon: could not open {}: {e}", dir.display()); } } -/// Open a native file dialog to pick a shader pack `.zip` or folder, falling -/// back to opening `dir` in the system file manager when no dialog helper is present. -pub fn pick_shader_pack(dir: &Path) -> Option { - let _ = std::fs::create_dir_all(dir); - let output = if cfg!(target_os = "windows") { - std::process::Command::new("powershell") - .args([ - "-NoProfile", - "-Command", - "Add-Type -AssemblyName System.Windows.Forms; $f = New-Object System.Windows.Forms.OpenFileDialog; $f.Filter = 'Shader Packs (*.zip;pack.toml)|*.zip;pack.toml|All Files (*.*)|*.*'; if ($f.ShowDialog() -eq 'OK') { Write-Output $f.FileName }", - ]) - .output() - .ok() - } else if cfg!(target_os = "macos") { - std::process::Command::new("osascript") - .args([ - "-e", - "POSIX path of (choose file with prompt \"Select Shader Pack (.zip)\")", - ]) - .output() - .ok() - } else { - std::process::Command::new("zenity") - .args([ - "--file-selection", - "--title=Import Shader Pack (.zip)", - "--file-filter=Shader Packs (*.zip) | *.zip", - ]) - .output() - .ok() - .filter(|o| o.status.success()) - .or_else(|| { - std::process::Command::new("kdialog") - .args(["--getopenfilename", ".", "*.zip|Shader Packs (*.zip)"]) - .output() - .ok() - }) - }; - if let Some(out) = output - && out.status.success() - { - let raw = String::from_utf8_lossy(&out.stdout).trim().to_owned(); - if !raw.is_empty() { - let p = PathBuf::from(raw); - if p.file_name().is_some_and(|n| n == "pack.toml") { - return p.parent().map(Path::to_path_buf); - } - return Some(p); - } - return None; - } - open_folder(dir); - None -} /// The resource root to load from, given the vanilla root and the selection. pub fn startup_root(vanilla: &Path, selected: &[String]) -> std::io::Result { let all = packs::discover(vanilla, &packs_dir()); diff --git a/crates/horizon-client/src/game/shader_packs.rs b/crates/horizon-client/src/game/shader_packs.rs index d7ec206ec..a501543bd 100644 --- a/crates/horizon-client/src/game/shader_packs.rs +++ b/crates/horizon-client/src/game/shader_packs.rs @@ -101,30 +101,19 @@ impl Game<'_> { self.load_shader_pack(); } } - /// Import a shader pack from `source` into `shaderpacks/`, activate it, and reload. - pub(super) fn import_shader_pack_path(&mut self, source: &std::path::Path) { - let dest = dir(); - match shader_pack::import_pack(source, &dest) { - Ok(id) => { - self.ui.options.shader_packs = Default::default(); - self.ui.options.shader_packs.poll(dest); - self.settings.horizon.shader_pack = id; - self.ui.options.shader_pack_config = None; - self.ui.options.sync_shader_pack_config(&self.settings); + /// A file dropped on the window: a shader pack while the Shader Packs + /// page shows (`OptionsUi::drop_shader_pack`). + pub(super) fn drop_shader_pack(&mut self, path: &std::path::Path) { + match self.ui.options.drop_shader_pack(&mut self.settings, path) { + Some(Ok(())) => { self.settings.save(&Self::options_path()); self.load_shader_pack(); } - Err(e) => { - log::warn!("Horizon: shader pack import failed: {e}"); - self.system_message(Text::literal(format!("Shader pack import: {e}"))); + Some(Err(e)) => { + log::warn!("Horizon: shader pack {}: {e}", path.display()); + self.system_message(Text::literal(format!("Shader pack: {e}"))); } - } - } - /// Open the system file picker (or `shaderpacks/` folder) to import a shader pack. - pub(super) fn import_shader_pack_dialog(&mut self) { - let dest = dir(); - if let Some(chosen) = super::packs::pick_shader_pack(&dest) { - self.import_shader_pack_path(&chosen); + None => {} } } fn shader_pack_error(&mut self, id: &str, error: &str) { diff --git a/crates/horizon-client/src/game/startup_menu.rs b/crates/horizon-client/src/game/startup_menu.rs index e01dd6dc6..ebf062a83 100644 --- a/crates/horizon-client/src/game/startup_menu.rs +++ b/crates/horizon-client/src/game/startup_menu.rs @@ -387,19 +387,6 @@ impl Loading { self.menu.win = Some((screen, Instant::now())); } } - Event::ImportShaderPack => { - let dir = horizon_client::shader_pack::folder(); - if let Some(chosen) = packs::pick_shader_pack(&dir) - && let Ok(id) = horizon_client::shader_pack::import_pack(&chosen, &dir) - { - self.menu.options.shader_packs = Default::default(); - self.menu.options.shader_packs.poll(dir); - self.settings.horizon.shader_pack = id; - self.menu.options.shader_pack_config = None; - self.menu.options.sync_shader_pack_config(&self.settings); - self.save_menu_settings(); - } - } Event::ReloadShaderPack => { self.save_menu_settings(); } @@ -469,14 +456,14 @@ impl Loading { self.apply_menu_settings(); } WindowEvent::DroppedFile(path) => { - let dir = horizon_client::shader_pack::folder(); - if let Ok(id) = horizon_client::shader_pack::import_pack(&path, &dir) { - self.menu.options.shader_packs = Default::default(); - self.menu.options.shader_packs.poll(dir); - self.settings.horizon.shader_pack = id; - self.menu.options.shader_pack_config = None; - self.menu.options.sync_shader_pack_config(&self.settings); - self.save_menu_settings(); + match self + .menu + .options + .drop_shader_pack(&mut self.settings, &path) + { + Some(Ok(())) => self.save_menu_settings(), + Some(Err(e)) => log::warn!("Horizon: shader pack {}: {e}", path.display()), + None => {} } } WindowEvent::RedrawRequested => self.draw(), diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 38a3e0521..255f4dd0a 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -3679,14 +3679,16 @@ impl Renderer { } // The world's targets: the screen, or the scaled target of `post`. let pack = self.pack.is_some() && !frame.title; - let shadow_cfg = if pack { - self.pack.as_ref().and_then(|p| match p { - Pack::Horizon(s) if s.needs_shadow => Some((s.shadow_res, s.shadow_dist)), - Pack::Java(s) if s.needs_shadow() => Some(s.shadow_config(frame.shader.dimension)), - _ => None, - }) - } else { - None + // What the pack reads beyond the world's colour and depth: each costs + // a pass (the shadow map, the G-buffer) or a copy (the opaque depth). + let (shadow_cfg, gbuffer, opaque_depth) = match self.pack.as_ref().filter(|_| pack) { + Some(Pack::Horizon(s)) => ( + s.needs_shadow.then_some((s.shadow_res, s.shadow_dist)), + s.needs_gbuffer, + s.needs_opaque_depth, + ), + Some(Pack::Java(s)) => (None, false, s.reads_opaque_depth), + None => (None, false, false), }; if let Some((shadow_res, shadow_dist)) = shadow_cfg { self.world_post @@ -3944,7 +3946,7 @@ impl Renderer { pass.set_vertex_buffer(0, self.debug_line_buffer.slice(..)); pass.draw(0..4, debug_depth..debug_depth + debug_top); } - if pack { + if opaque_depth { drop(pass); self.world_post.capture_opaque_depth(&mut encoder); let attachments = [Some(wgpu::RenderPassColorAttachment { @@ -4042,7 +4044,7 @@ impl Renderer { ); } } - if pack && scaled.is_some() { + if gbuffer && scaled.is_some() { self.world_post.build_gbuffer( &self.queue, &mut encoder, @@ -4617,7 +4619,11 @@ mod tests { ); assert!(files.len() >= 10, "{files:?}"); for path in files { - let text = std::fs::read_to_string(&path).unwrap(); + let mut text = std::fs::read_to_string(&path).unwrap(); + // Api 2's prelude follows api 1's (`shader_pack::ShaderPack::load`). + if path.ends_with("shader_pack_prelude_api2.wgsl") { + text = format!("{}\n{text}", crate::shader_pack::PRELUDE); + } // entity.wgsl comes after the overlay's constants (entity_shader). let module = naga::front::wgsl::parse_str(&text) .or_else(|_| { diff --git a/crates/horizon-client/src/gfx/java_stages.rs b/crates/horizon-client/src/gfx/java_stages.rs index 8091b0703..f86055919 100644 --- a/crates/horizon-client/src/gfx/java_stages.rs +++ b/crates/horizon-client/src/gfx/java_stages.rs @@ -1,16 +1,17 @@ //! A Java (OptiFine/Iris) shader pack on the GPU (docs/shader-packs.md): //! Iris's colour targets (`colortex0..`, each with a main and an alternate -//! side), pre-translucent depth (`depthtex1`/`depthtex2`), directional -//! shadow map (`shadowtex0`/`shadowtex1`) and the pack's `begin`, `prepare`, -//! `deferred`, `composite` and `final` programs as full-screen passes over -//! them, as Iris's `CompositeRenderer` runs them. Horizon's world enters -//! `colortex0` and `depthtex0..2` after `prepare`, where Iris's gbuffers -//! programs would have drawn, and the last picture goes to `world_post`. The +//! side) and the pack's `begin`, `prepare`, `deferred`, `composite` and +//! `final` programs as full-screen passes over them, as Iris's +//! `CompositeRenderer` runs them. Horizon's world enters `colortex0` and +//! `depthtex0` after `prepare`, where Iris's gbuffers programs would have +//! drawn (`depthtex1`/`depthtex2`: its depth before translucent geometry), +//! and the last picture goes to `world_post`. The //! targets keep GL's layout (row 0 at the bottom, which the translated vertex //! programs produce), so the world is flipped going in and the picture coming //! out. -use super::pack_stages::FrameUniform; -use crate::java_pack::{JavaFolder, JavaPasses}; +use super::pack_stages::{FrameUniform, noise, rgba_view}; +use crate::java_pack::{JavaFolder, JavaPasses, NOISE_KEY, texture_key}; +use crate::shader_pack::Rgba; use glam::{Mat3, Mat4, Vec3, Vec4}; use horizon_shaderpack::pipeline::TargetSettings; use horizon_shaderpack::pipeline::{Directives, Program, target_index}; @@ -30,8 +31,6 @@ pub struct JavaFrame { pub moon_angle: f32, } -/// Iris's `noisetex` size (`noiseTextureResolution`'s default). -const NOISE_SIZE: u32 = 256; /// The vertex buffers a pass may read (wgpu's default limit). const MAX_ATTRIBUTES: usize = 8; @@ -173,7 +172,8 @@ enum Source { /// `depthtex1` / `depthtex2`: the opaque world's depth (before translucent water). OpaqueDepth, Noise, - /// `shadowtex*`: directional sun/moon shadow map depth (or far depth for DH/Voxy). + /// `shadowtex*`: no shadow map yet, so a far depth (nothing in shadow); + /// also Distant Horizons' and Voxy's depth, as without those mods. Shadow, /// Anything else (`shadowcolor*`, custom textures): white or black. Blank(bool), @@ -267,11 +267,13 @@ struct Folder { passes: Vec, final_pass: Option, directives: Directives, - shadow_enabled: bool, } pub struct JavaStages { pub id: String, + /// A pass samples `depthtex1`/`depthtex2`: the world's depth is kept + /// before translucent geometry draws. + pub reads_opaque_depth: bool, format: wgpu::TextureFormat, common: Common, folders: Vec, @@ -285,25 +287,12 @@ pub struct JavaStages { uniform_state: UniformState, /// `eyeBrightnessSmooth`, block then sky. eye_smooth: [SmoothFloat; 2], - /// `wetness`, smoothed with `wetnessHalflife` / `drynessHalflife`. + /// `wetness`: `rainStrength` smoothed with `wetnessHalflife` going up + /// and `drynessHalflife` going down. wetness_smooth: SmoothFloat, - /// `centerDepthSmooth`, smoothed with `centerDepthHalflife`. - center_depth_smooth: SmoothFloat, } impl JavaStages { - /// Whether any dimension folder in this pack reads directional shadow maps. - pub fn needs_shadow(&self) -> bool { - self.folders.iter().any(|f| f.shadow_enabled) - } - - /// Shadow map resolution and distance for `dimension`. - pub fn shadow_config(&self, dimension: i8) -> (u32, f32) { - let idx = self.folder_for[(dimension.clamp(-1, 1) + 1) as usize].unwrap_or(0); - let d = &self.folders[idx].directives; - (d.shadow_map_resolution, d.shadow_distance) - } - /// The pipelines of a loaded Java pack, or wgpu's error. pub fn new( device: &wgpu::Device, @@ -342,15 +331,20 @@ impl JavaStages { passes, final_pass, directives: plan.directives, - shadow_enabled: plan.shadow_enabled, }) }) .collect::, _>>()?; if let Some(error) = pollster::block_on(scope.pop()) { return Err(error.to_string()); } + let reads_opaque_depth = folders + .iter() + .flat_map(|f| f.passes.iter().chain(&f.final_pass)) + .flat_map(|p| &p.samplers) + .any(|s| s.source == Source::OpaqueDepth); Ok(Self { id: id.to_owned(), + reads_opaque_depth, format, common, folders, @@ -364,7 +358,6 @@ impl JavaStages { uniform_state: UniformState::default(), eye_smooth: [SmoothFloat::default(); 2], wetness_smooth: SmoothFloat::default(), - center_depth_smooth: SmoothFloat::default(), }) } @@ -405,10 +398,6 @@ impl JavaStages { self.previous, ); inputs.set_world(&world, directives.sun_path_rotation); - inputs.shadow_view = post.shadow_view; - inputs.shadow_projection = post.gl_shadow_projection(); - inputs.ambient_occlusion_level = directives.ambient_occlusion_level; - inputs.shadow_map_resolution = directives.shadow_map_resolution as f32; let half_life = directives.eye_brightness_half_life; for (i, smooth) in self.eye_smooth.iter_mut().enumerate() { inputs.eye_smooth[i] = smooth.update(inputs.eye[i], half_life, half_life, frame_time); @@ -419,12 +408,6 @@ impl JavaStages { directives.dryness_half_life, frame_time, ); - inputs.center_depth_smooth = self.center_depth_smooth.update( - 0.5, - directives.center_depth_half_life, - directives.center_depth_half_life, - frame_time, - ); self.uniforms .update(&mut self.uniform_state, frame_time, &|name| { inputs.builtin(name) @@ -599,7 +582,6 @@ impl JavaStages { &targets, &depth, &opaque_depth, - post, &self.common, &mut blank, ) @@ -780,11 +762,8 @@ fn blit_pipeline( } impl Common { - fn new( - device: &wgpu::Device, - queue: &wgpu::Queue, - custom_map: &BTreeMap)>, - ) -> Self { + /// `custom` holds the pack's `texture.*` images (`JavaPasses::custom_textures`). + fn new(device: &wgpu::Device, queue: &wgpu::Queue, custom: &BTreeMap) -> Self { let sampler = |filter, address, compare| { device.create_sampler(&wgpu::SamplerDescriptor { label: Some("Java shader pack"), @@ -798,68 +777,15 @@ impl Common { }) }; use wgpu::{AddressMode::*, FilterMode::*}; - // Iris's noise: random RGB, opaque. - let mut seed = 0x2545_f491_u32; - let noise_bytes: Vec = (0..NOISE_SIZE * NOISE_SIZE * 4) - .map(|i| { - seed ^= seed << 13; - seed ^= seed >> 17; - seed ^= seed << 5; - if i % 4 == 3 { 255 } else { (seed >> 24) as u8 } - }) + let noise = match custom.get(NOISE_KEY) { + Some(image) => rgba_view(device, queue, "noisetex", image), + None => rgba_view(device, queue, "noisetex", &noise()), + }; + let custom_textures = custom + .iter() + .filter(|(key, _)| *key != NOISE_KEY) + .map(|(key, image)| (key.clone(), rgba_view(device, queue, key, image))) .collect(); - let noise = device.create_texture(&wgpu::TextureDescriptor { - label: Some("noisetex"), - size: wgpu::Extent3d { - width: NOISE_SIZE, - height: NOISE_SIZE, - depth_or_array_layers: 1, - }, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: wgpu::TextureFormat::Rgba8Unorm, - usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, - view_formats: &[], - }); - queue.write_texture( - noise.as_image_copy(), - &noise_bytes, - wgpu::TexelCopyBufferLayout { - offset: 0, - bytes_per_row: Some(NOISE_SIZE * 4), - rows_per_image: None, - }, - noise.size(), - ); - let mut custom_textures = BTreeMap::new(); - for (name, (w, h, bytes)) in custom_map { - let tex = device.create_texture(&wgpu::TextureDescriptor { - label: Some(name), - size: wgpu::Extent3d { - width: *w, - height: *h, - depth_or_array_layers: 1, - }, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: wgpu::TextureFormat::Rgba8Unorm, - usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, - view_formats: &[], - }); - queue.write_texture( - tex.as_image_copy(), - bytes, - wgpu::TexelCopyBufferLayout { - offset: 0, - bytes_per_row: Some(*w * 4), - rows_per_image: None, - }, - tex.size(), - ); - custom_textures.insert(name.clone(), tex.create_view(&Default::default())); - } // The full-screen triangle (0, 0), (2, 0), (0, 2): the pack's // projection maps 0..1 to the screen. Texture coordinates are the // position; colours white; normals up. @@ -899,7 +825,7 @@ impl Common { nearest: sampler(Nearest, ClampToEdge, None), repeat: sampler(Linear, Repeat, None), compare: sampler(Linear, ClampToEdge, Some(wgpu::CompareFunction::LessEqual)), - noise: noise.create_view(&Default::default()), + noise, custom_textures, vertices, world_layout: device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { @@ -1239,21 +1165,22 @@ impl PassGpu { targets: &BTreeMap, depth: &wgpu::TextureView, opaque_depth: &wgpu::TextureView, - post: &super::world_post::Post, common: &Common, blank: &mut Vec, ) -> wgpu::BindGroup { let mut views = Vec::new(); for s in &self.samplers { - let view = if let Some(custom) = common - .custom_textures - .get(&s.name) - .filter(|_| !s.compare && s.dimension == wgpu::TextureViewDimension::D2) - { + let custom = (!s.compare && s.dimension == wgpu::TextureViewDimension::D2) + .then(|| { + common + .custom_textures + .get(&texture_key(&self.program, &s.name)) + }) + .flatten(); + let view = if let Some(custom) = custom { custom.clone() } else { match (s.compare, s.source) { - (true, Source::Shadow) if post.shadow_active => post.shadow_depth().clone(), (true, _) => blank_view(device, queue, blank, s, BlankKind::FarDepth), (false, Source::Target(i)) if s.dimension == wgpu::TextureViewDimension::D2 => { match targets.get(&i) { @@ -1612,8 +1539,6 @@ struct Inputs { view: Mat4, /// GL's: near at -1, far at 1. projection: Mat4, - shadow_view: Mat4, - shadow_projection: Mat4, camera: Vec3, previous: (Mat4, Mat4, Vec3), size: [f32; 2], @@ -1624,10 +1549,8 @@ struct Inputs { /// `eyeBrightness` and `eyeBrightnessSmooth`: block, sky light × 16. eye: [f32; 2], eye_smooth: [f32; 2], + /// `wetness` (`JavaStages::wetness_smooth`). wetness: f32, - center_depth_smooth: f32, - ambient_occlusion_level: f32, - shadow_map_resolution: f32, /// `CelestialUniforms`, in view space. sun_position: Vec3, moon_position: Vec3, @@ -1676,8 +1599,6 @@ impl Inputs { let mut out = Self { view, projection, - shadow_view: Mat4::IDENTITY, - shadow_projection: Mat4::IDENTITY, camera, previous: previous.unwrap_or((view, projection, camera)), size: size.map(|v| v as f32), @@ -1688,9 +1609,6 @@ impl Inputs { eye: [0.0; 2], eye_smooth: [0.0; 2], wetness: frame.weather[0], - center_depth_smooth: 0.5, - ambient_occlusion_level: 1.0, - shadow_map_resolution: 2048.0, sun_position: Vec3::ZERO, moon_position: Vec3::ZERO, up_position: Vec3::ZERO, @@ -1790,35 +1708,21 @@ impl Inputs { "skyColor" => f.sky_color[..3].to_vec(), "isEyeInWater" => vec![f.camera_state[0]], "nightVision" => vec![f.camera_state[1]], - "darknessFactor" | "darknessLightFactor" => vec![f.camera_state[2]], - "blindness" => vec![f.camera_state[3]], + "darknessFactor" => vec![f.camera_state[2]], "screenBrightness" => vec![1.0], - "centerDepthSmooth" => vec![self.center_depth_smooth], - "ambientOcclusionLevel" => vec![self.ambient_occlusion_level], - "shadowMapResolution" => vec![self.shadow_map_resolution], "eyeBrightness" => self.eye.to_vec(), // An ivec2: the smoothed value truncated. "eyeBrightnessSmooth" => self.eye_smooth.map(f32::trunc).to_vec(), "hasSkylight" => vec![f.dimension[1]], - "heldItemId" | "heldItemId2" | "heldBlockLightValue" | "heldBlockLightValue2" => { - vec![0.0] - } - "biome" | "biome_category" => vec![0.0], - "biome_precipitation" => vec![if f.weather[0] > 0.01 { 1.0 } else { 0.0 }], - "temp" => vec![0.8], - "rainfall" => vec![0.4], - "alphaTestRef" => vec![0.1], - "renderStage" => vec![0.0], - "playerMood" | "currentColorSpace" => vec![0.0], "iris_ModelViewMatrix" | "iris_ModelViewMatrixInverse" => mat(Mat4::IDENTITY), "iris_ProjectionMatrix" | "iris_ModelViewProjectionMatrix" => mat(screen), "iris_ProjectionMatrixInverse" => mat(screen.inverse()), "iris_TextureMatrix" => (0..8).flat_map(|_| mat(Mat4::IDENTITY)).collect(), "iris_NormalMatrix" => Mat3::IDENTITY.to_cols_array().to_vec(), - "shadowModelView" => mat(self.shadow_view), - "shadowModelViewInverse" => mat(self.shadow_view.inverse()), - "shadowProjection" => mat(self.shadow_projection), - "shadowProjectionInverse" => mat(self.shadow_projection.inverse()), + "shadowModelView" + | "shadowModelViewInverse" + | "shadowProjection" + | "shadowProjectionInverse" => mat(Mat4::IDENTITY), _ => return None, }) } diff --git a/crates/horizon-client/src/gfx/pack_stages.rs b/crates/horizon-client/src/gfx/pack_stages.rs index 08a6b9aea..f80a64ce1 100644 --- a/crates/horizon-client/src/gfx/pack_stages.rs +++ b/crates/horizon-client/src/gfx/pack_stages.rs @@ -5,7 +5,7 @@ //! and writes its own target; the last target goes to the screen through //! `world_post` (copy or FXAA) under the hand and HUD. Only exists while a //! pack is selected. -use crate::shader_pack::{ShaderPack, StageKind, VERTEX_ENTRY}; +use crate::shader_pack::{Rgba, ShaderPack, VERTEX_ENTRY}; use horizon_shaderpack::uniforms::SmoothFloat; /// The `Frame` uniform of the prelude (`shader_pack_prelude.wgsl`). @@ -45,7 +45,59 @@ pub struct FrameExtUniform { pub environment: [f32; 4], } -const NOISE_SIZE: u32 = 256; +/// Iris's `noisetex` without a `texture.noise`: 256² random RGB, opaque. +/// Horizon packs get the same one unless `[textures] noise` replaces it. +pub(super) fn noise() -> Rgba { + const SIZE: u32 = 256; + let mut seed = 0x2545_f491_u32; + let pixels = (0..SIZE * SIZE * 4) + .map(|i| { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + if i % 4 == 3 { 255 } else { (seed >> 24) as u8 } + }) + .collect(); + Rgba { + width: SIZE, + height: SIZE, + pixels, + } +} + +/// `image` as a sampled RGBA8 texture. +pub(super) fn rgba_view( + device: &wgpu::Device, + queue: &wgpu::Queue, + label: &str, + image: &Rgba, +) -> wgpu::TextureView { + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some(label), + size: wgpu::Extent3d { + width: image.width, + height: image.height, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }); + queue.write_texture( + texture.as_image_copy(), + &image.pixels, + wgpu::TexelCopyBufferLayout { + offset: 0, + bytes_per_row: Some(image.width * 4), + rows_per_image: None, + }, + texture.size(), + ); + texture.create_view(&Default::default()) +} struct Pass { pipeline: wgpu::RenderPipeline, @@ -64,6 +116,10 @@ struct Target { pub struct PackStages { pub id: String, pub needs_shadow: bool, + /// The stages read the G-buffer (`build_gbuffer`) or the depth before + /// translucent geometry (`capture_opaque_depth`). + pub needs_gbuffer: bool, + pub needs_opaque_depth: bool, pub shadow_res: u32, pub shadow_dist: f32, layout: wgpu::BindGroupLayout, @@ -162,7 +218,7 @@ impl PackStages { .iter() .enumerate() .map(|(idx, stage)| { - let is_last = idx + 1 == stage_count || stage.kind == StageKind::Final; + let is_last = idx + 1 == stage_count; let target_format = if is_last { format } else { @@ -240,49 +296,16 @@ impl PackStages { ..Default::default() }) }; - let (nw, nh, noise_bytes) = match &pack.custom_noise { - Some((w, h, bytes)) => (*w, *h, bytes.clone()), - None => { - let mut seed = 0x2545_f491_u32; - let bytes: Vec = (0..NOISE_SIZE * NOISE_SIZE * 4) - .map(|i| { - seed ^= seed << 13; - seed ^= seed >> 17; - seed ^= seed << 5; - if i % 4 == 3 { 255 } else { (seed >> 24) as u8 } - }) - .collect(); - (NOISE_SIZE, NOISE_SIZE, bytes) - } + let noise_view = match &pack.custom_noise { + Some(image) => rgba_view(device, queue, "Shader pack noise", image), + None => rgba_view(device, queue, "Shader pack noise", &noise()), }; - let noise_tex = device.create_texture(&wgpu::TextureDescriptor { - label: Some("Shader pack noise"), - size: wgpu::Extent3d { - width: nw, - height: nh, - depth_or_array_layers: 1, - }, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: wgpu::TextureFormat::Rgba8Unorm, - usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, - view_formats: &[], - }); - queue.write_texture( - noise_tex.as_image_copy(), - &noise_bytes, - wgpu::TexelCopyBufferLayout { - offset: 0, - bytes_per_row: Some(nw * 4), - rows_per_image: None, - }, - noise_tex.size(), - ); - let (shadow_res, shadow_dist, _) = pack.shadow_config(0); + let (shadow_res, shadow_dist) = pack.shadow_config(); Ok(Self { id: pack.id.clone(), needs_shadow: pack.needs_shadow(), + needs_gbuffer: pack.needs_gbuffer(), + needs_opaque_depth: pack.needs_opaque_depth(), shadow_res, shadow_dist, layout, @@ -302,7 +325,7 @@ impl PackStages { wgpu::AddressMode::ClampToEdge, Some(wgpu::CompareFunction::GreaterEqual), ), - noise_view: noise_tex.create_view(&Default::default()), + noise_view, passes, targets: Vec::new(), output: None, @@ -462,7 +485,7 @@ impl PackStages { f32::from(eye_light[0]) * 16.0, f32::from(eye_light[1]) * 16.0, wet, - uniform.view_params[2] * 0.5, + 0.0, ], }; uniform.time[0] = self.started.elapsed().as_secs_f32(); diff --git a/crates/horizon-client/src/gfx/world_post.rs b/crates/horizon-client/src/gfx/world_post.rs index 69d95aae6..f592e8a1f 100644 --- a/crates/horizon-client/src/gfx/world_post.rs +++ b/crates/horizon-client/src/gfx/world_post.rs @@ -1,19 +1,19 @@ //! Resolution scale, FXAA, and shader-pack G-buffer/shadow targets (Horizon //! settings): the world renders into its own target at the scaled size, then //! one full-screen pass copies or anti-aliases it onto the screen, under the -//! hand and HUD. When a shader pack is active, `Post` also maintains: -//! - `opaque_depth` (`depthtex1`/`depthtex2`): depth captured before -//! translucent terrain (water, stained glass, ice) renders; -//! - `normal` (`colortex1`/`scene_normal`) and `material` -//! (`colortex2`/`scene_material`): G-buffer surface normals and material -//! properties reconstructed from depth and scene colour; -//! - `shadow_depth` (`shadowtex0`/`shadowtex1`): the directional sun/moon -//! shadow map and its light-space `shadow_view` and `shadow_proj` matrices. +//! hand and HUD. For a shader pack that reads them, `Post` also keeps: +//! - `opaque_depth`: the depth before translucent terrain (water, stained +//! glass, ice) draws, a Java pack's `depthtex1`/`depthtex2`; +//! - `normal` and `material`, an api 2 pack's `scene_normal` and +//! `scene_material`: surface normals and material properties +//! reconstructed from depth and scene colour; +//! - `shadow_depth`, an api 2 pack's: the sun/moon shadow map and its +//! light-space `shadow_view` and `shadow_proj` matrices. //! //! With none of them on, the world renders straight to the screen as before. use super::pack_stages::FrameUniform; -use glam::{Mat4, Vec3, Vec4}; +use glam::{Mat4, Vec3}; const GBUFFER_WGSL: &str = r#" struct GbufferParams { @@ -632,45 +632,34 @@ impl Post { self.target.as_ref().map(|t| t.size) } - /// Pre-translucent depth view (`depthtex1`/`depthtex2` / `opaque_depth`). + /// The depth before translucent geometry (`capture_opaque_depth`). pub fn opaque_depth(&self) -> &wgpu::TextureView { self.target .as_ref() .map_or(&self.fallback_shadow, |t| &t.opaque_depth) } - /// Reconstructed world-space normal G-buffer view (`colortex1` / `scene_normal`). + /// Reconstructed world-space normals (`scene_normal`). pub fn normal(&self) -> &wgpu::TextureView { self.target .as_ref() .map_or(&self.fallback_color, |t| &t.normal) } - /// Reconstructed material/lightmap G-buffer view (`colortex2` / `scene_material`). + /// Reconstructed material and light (`scene_material`). pub fn material(&self) -> &wgpu::TextureView { self.target .as_ref() .map_or(&self.fallback_color, |t| &t.material) } - /// Directional sun/moon shadow map view (`shadowtex0`/`shadowtex1` / `shadow_depth`). + /// The sun/moon shadow map (`shadow_depth` of api 2 packs). pub fn shadow_depth(&self) -> &wgpu::TextureView { self.shadow .as_ref() .map_or(&self.fallback_shadow, |s| &s.depth) } - /// OpenGL-convention (-1..1 Z) shadow projection for Java/Iris shader packs. - pub fn gl_shadow_projection(&self) -> Mat4 { - let to_gl = Mat4::from_cols( - Vec4::X, - Vec4::Y, - Vec4::new(0.0, 0.0, -2.0, 0.0), - Vec4::new(0.0, 0.0, 1.0, 1.0), - ); - to_gl * self.shadow_proj - } - /// Bindings that copy (or anti-alias) `view` onto the screen in `finish_with`. pub fn bind(&self, device: &wgpu::Device, view: &wgpu::TextureView) -> wgpu::BindGroup { device.create_bind_group(&wgpu::BindGroupDescriptor { diff --git a/crates/horizon-client/src/java_pack.rs b/crates/horizon-client/src/java_pack.rs index 5a37aa223..5e592d1b6 100644 --- a/crates/horizon-client/src/java_pack.rs +++ b/crates/horizon-client/src/java_pack.rs @@ -1,9 +1,9 @@ //! Java (OptiFine/Iris) shader packs in `shaderpacks/` (docs/shader-packs.md): //! a folder holding `shaders/`, or a `.zip`. `horizon-shaderpack` translates //! their full-screen programs (`begin`, `prepare`, `deferred`, `composite`, -//! `final`) to WGSL, and `gfx/java_stages.rs` runs them over Horizon's world, -//! G-buffers, pre-translucent depth (`depthtex1`) and directional shadow map -//! (`shadowtex0`/`shadowtex1`) as Iris runs them over its own. +//! `final`) to WGSL, and `gfx/java_stages.rs` runs them over Horizon's world +//! as Iris runs them over its own. Geometry programs (`gbuffers_*`, +//! `shadow`) don't run yet: the world is drawn by Horizon. use horizon_shaderpack::{ JavaPack, pipeline::{Directives, Program, TargetSettings}, @@ -30,10 +30,24 @@ pub struct JavaFolder { pub final_pass: Option, /// The targets whose alternate side holds the frame's last write. pub flipped_at_end: BTreeSet, - /// `sunPathRotation`, `eyeBrightnessHalflife`, shadow and wetness directives. + /// `sunPathRotation`, `eyeBrightnessHalflife`, `wetnessHalflife` and + /// `drynessHalflife`. pub directives: Directives, - /// Whether this dimension's passes read directional shadow maps. - pub shadow_enabled: bool, +} + +/// The key of `texture.noise` (`noisetex`) in [`JavaPasses::custom_textures`]. +pub const NOISE_KEY: &str = "noise"; + +/// The key of `texture..` in [`JavaPasses::custom_textures`] +/// for a pass of `program` (`world0/composite2`): Iris's texture stages, where +/// `final` reads the `composite` ones. +pub fn texture_key(program: &str, sampler: &str) -> String { + let name = program.rsplit('/').next().unwrap_or(program); + let stage = match name.trim_end_matches(|c: char| c.is_ascii_digit()) { + "final" => "composite", + stage => stage, + }; + format!("{stage}.{sampler}") } /// A loaded Java pack. @@ -43,8 +57,9 @@ pub struct JavaPasses { pub uniforms: CustomUniforms, /// What the pack keeps of the vanilla sky (`clouds`, `sun`, `moon`). pub sky: SkyDirectives, - /// Decoded RGBA8 custom textures (`sampler_name` → `(width, height, rgba)`). - pub custom_textures: BTreeMap)>, + /// `texture..` and `texture.noise` images, by + /// `"."` and [`NOISE_KEY`]. + pub custom_textures: BTreeMap, } impl JavaPasses { @@ -135,7 +150,6 @@ pub fn load( final_pass, flipped_at_end: plan.flipped_at_end, directives: plan.directives, - shadow_enabled: plan.shadow_enabled, }); } if folders.is_empty() { @@ -143,15 +157,23 @@ pub fn load( } let mut custom_textures = BTreeMap::new(); for tex in properties.custom_textures() { - if let Some(bytes) = pack.files.read_bytes(&tex.path) { - match crate::shader_pack::decode_png_rgba(&bytes) { - Ok(decoded) => { - custom_textures.insert(tex.sampler, decoded); - } - Err(e) => { - log::warn!("Horizon: shader pack texture {}: {e}", tex.path); - } + let key = match tex.stage { + Some(stage) => format!("{stage}.{}", tex.sampler), + None if tex.sampler == NOISE_KEY => NOISE_KEY.to_owned(), + None => continue, + }; + // Only PNGs for now: resource locations and raw `.dat` textures are + // skipped and the sampler reads what it would without them. + let image = pack + .files + .read_bytes(&tex.path) + .ok_or_else(|| "not in the pack".to_owned()) + .and_then(|bytes| crate::shader_pack::decode_png(&bytes)); + match image { + Ok(image) => { + custom_textures.insert(key, image); } + Err(e) => log::warn!("Horizon: shader pack texture {}: {e}", tex.path), } } let uniforms = properties.custom_uniforms(); @@ -170,6 +192,17 @@ pub fn load( mod tests { use super::*; + #[test] + fn custom_textures_belong_to_iris_s_stages() { + assert_eq!(texture_key("world0/composite2", "gaux1"), "composite.gaux1"); + assert_eq!(texture_key("final", "colortex4"), "composite.colortex4"); + assert_eq!( + texture_key("world-1/deferred10", "noisetex"), + "deferred.noisetex" + ); + assert_eq!(texture_key("prepare", "depthtex2"), "prepare.depthtex2"); + } + #[test] fn loads_the_full_screen_passes_of_a_java_pack() { let dir = Path::new(env!("CARGO_MANIFEST_DIR")) diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs index 4ae78da40..1bb9953bf 100644 --- a/crates/horizon-client/src/options_ui.rs +++ b/crates/horizon-client/src/options_ui.rs @@ -10,6 +10,7 @@ use crate::{ keybinds::{self, CATEGORIES, KeyBinds, MAPPINGS}, lang::{self, LanguageInfo}, settings::{SOUND_SOURCES, Settings}, + shader_pack::PackConfig, text::{self, Style, Text}, ui, }; @@ -903,8 +904,6 @@ enum Act { /// Credits & Attribution's Credits button (WinScreen without the poem). Credits, CopyAgentDetails, - /// Pick and import a shader pack `.zip` or folder into `shaderpacks/`. - ImportShaderPack, ShaderPackProfile, ShaderPackSubscreen(String), ShaderPackBackScreen, @@ -968,9 +967,8 @@ pub enum Event { Credits, /// Copy how an agent connects (the AI Agent page). CopyAgentDetails, - /// Pick and import a shader pack `.zip` or folder into `shaderpacks/`. - ImportShaderPack, - /// Reload the active shader pack after its options file changed. + /// The shader pack settings page closed with changes: they are saved, + /// the pack is built again with them. ReloadShaderPack, } /// `SliderableValueSet.applyValueImmediately`: these apply 600 ms after @@ -1030,11 +1028,12 @@ pub struct OptionsUi { /// The AI Agent page's status lines (set by the game while it shows). pub agent_status: Vec, /// Loaded interactive configuration for the currently selected shader pack. - pub shader_pack_config: Option, - /// Classic shader option slider currently being dragged. + pub shader_pack_config: Option, + /// The shader pack option slider being dragged. pub(crate) dragging_shader_option: Option, - /// Horizon shader option slider currently being dragged. - pub(crate) hdragging_shader_option: Option, + /// `shader_pack_config` changed since the settings page opened: leaving + /// it saves the values and reloads the pack. + shader_pack_edited: bool, } impl Default for OptionsUi { fn default() -> Self { @@ -1068,7 +1067,7 @@ impl Default for OptionsUi { agent_status: Vec::new(), shader_pack_config: None, dragging_shader_option: None, - hdragging_shader_option: None, + shader_pack_edited: false, } } } @@ -1225,22 +1224,58 @@ impl OptionsUi { { return; } - let dir = if self.shader_packs.dir.as_os_str().is_empty() { + // A pack whose options can't be read has none, rather than being + // read again every frame. + let config = PackConfig::load(&self.shader_pack_dir(), id).unwrap_or_else(|e| { + log::warn!("Horizon: shader pack {id} options: {e}"); + PackConfig { + id: id.to_owned(), + ..Default::default() + } + }); + self.shader_pack_config = Some(config); + self.shader_pack_edited = false; + } + fn shader_pack_dir(&self) -> std::path::PathBuf { + if self.shader_packs.dir.as_os_str().is_empty() { crate::shader_pack::folder() } else { self.shader_packs.dir.clone() - }; - self.shader_pack_config = crate::shader_pack::PackConfig::load(&dir, id).ok(); + } + } + /// A file or folder dropped on the window: on the Shader Packs page (as + /// Iris's `ShaderPackScreen.onFilesDrop`), a pack copied into + /// `shaderpacks/` and picked. `None` elsewhere; the caller saves the + /// settings and loads the pack on `Some(Ok)`. + pub fn drop_shader_pack( + &mut self, + s: &mut Settings, + path: &std::path::Path, + ) -> Option> { + if self.page() != Page::ShaderPacks { + return None; + } + let dir = self.shader_pack_dir(); + Some(crate::shader_pack::import_pack(path, &dir).map(|id| { + self.shader_packs = Default::default(); + self.shader_packs.poll(dir); + s.horizon.shader_pack = id; + self.sync_shader_pack_config(s); + })) + } + /// Change the shader pack's options, applied when its page closes. + fn edit_shader_pack(&mut self, edit: impl FnOnce(&mut PackConfig)) { + if let Some(cfg) = &mut self.shader_pack_config { + edit(cfg); + self.shader_pack_edited = true; + } } /// Save the active shader pack options to `shaderpacks/.txt`. - pub(crate) fn save_shader_pack_config(&self) { - if let Some(cfg) = &self.shader_pack_config { - let dir = if self.shader_packs.dir.as_os_str().is_empty() { - crate::shader_pack::folder() - } else { - self.shader_packs.dir.clone() - }; - let _ = cfg.save(&dir); + fn save_shader_pack_config(&self) { + if let Some(cfg) = &self.shader_pack_config + && let Err(e) = cfg.save(&self.shader_pack_dir()) + { + log::warn!("Horizon: shader pack {} options: {e}", cfg.id); } } pub(crate) fn shader_pack_failed(&self, id: &str) -> bool { @@ -1658,27 +1693,10 @@ impl OptionsUi { button( 0.0, 0.0, - 150.0, + 310.0, Text::literal("Shader Pack Settings..."), Act::Push(Page::ShaderPackOptions), ), - Some(button( - 0.0, - 0.0, - 150.0, - Text::literal("Import Shader Pack..."), - Act::ImportShaderPack, - )), - )); - } else { - rows.push(Row::Buttons( - button( - 0.0, - 0.0, - 310.0, - Text::literal("Import Shader Pack..."), - Act::ImportShaderPack, - ), None, )); } @@ -2725,14 +2743,12 @@ impl OptionsUi { }) { let v = ui::slider_value(wd.x, wd.w, x); - if let Some(cfg) = &mut self.shader_pack_config - && let Some(opt) = cfg.options.get_mut(idx) - { - opt.set_slider_pos(v); - } - self.save_shader_pack_config(); + self.edit_shader_pack(|cfg| { + if let Some(opt) = cfg.options.get_mut(idx) { + opt.set_slider_pos(v); + } + }); events.push(Event::Changed); - events.push(Event::ReloadShaderPack); } return events; } @@ -2821,14 +2837,9 @@ impl OptionsUi { Act::OpenShaderPackFolder => events.push(Event::OpenShaderPackFolder), Act::Credits => events.push(Event::Credits), Act::CopyAgentDetails => events.push(Event::CopyAgentDetails), - Act::ImportShaderPack => events.push(Event::ImportShaderPack), Act::ShaderPackProfile => { - if let Some(cfg) = &mut self.shader_pack_config { - cfg.cycle_profile(right); - } - self.save_shader_pack_config(); + self.edit_shader_pack(|cfg| cfg.cycle_profile(false)); events.push(Event::Changed); - events.push(Event::ReloadShaderPack); } Act::ShaderPackSubscreen(sub) => { if let Some(cfg) = &mut self.shader_pack_config { @@ -2843,22 +2854,16 @@ impl OptionsUi { self.scroll = 0.0; } Act::ShaderPackOption(i) => { - if let Some(cfg) = &mut self.shader_pack_config - && let Some(opt) = cfg.options.get_mut(i) - { - opt.cycle(right); - } - self.save_shader_pack_config(); + self.edit_shader_pack(|cfg| { + if let Some(opt) = cfg.options.get_mut(i) { + opt.cycle(false); + } + }); events.push(Event::Changed); - events.push(Event::ReloadShaderPack); } Act::ResetShaderPackOptions => { - if let Some(cfg) = &mut self.shader_pack_config { - cfg.reset_all(); - } - self.save_shader_pack_config(); + self.edit_shader_pack(PackConfig::reset_all); events.push(Event::Changed); - events.push(Event::ReloadShaderPack); } Act::None => {} } @@ -2873,14 +2878,12 @@ impl OptionsUi { WKind::ShaderOptionSlider { index, .. } => { self.dragging_shader_option = Some(index); let v = ui::slider_value(wd.x, wd.w, x); - if let Some(cfg) = &mut self.shader_pack_config - && let Some(opt) = cfg.options.get_mut(index) - { - opt.set_slider_pos(v); - } - self.save_shader_pack_config(); + self.edit_shader_pack(|cfg| { + if let Some(opt) = cfg.options.get_mut(index) { + opt.set_slider_pos(v); + } + }); events.push(Event::Changed); - events.push(Event::ReloadShaderPack); } WKind::LanguageRow { code, .. } => { if self.language_choice.as_deref() == Some(code.as_str()) { @@ -2910,9 +2913,7 @@ impl OptionsUi { } // AbstractSliderButton suppresses the press sound and plays once on // release, even if the pointer has left the widget or its value stayed. - let slider = self.dragging.take().is_some() - || self.dragging_shader_option.take().is_some() - || self.hdragging_shader_option.take().is_some(); + let slider = self.dragging.take().is_some() || self.dragging_shader_option.take().is_some(); let graphics_slider = self.graphics.release(); if slider || graphics_slider { vec![Event::ButtonClick] @@ -2992,6 +2993,12 @@ impl OptionsUi { events.push(Event::Language(code)); } } + // Iris's ShaderPackScreen applies its edits on Done, so the + // pack is rebuilt once, not on every click or slider step. + Page::ShaderPackOptions if std::mem::take(&mut self.shader_pack_edited) => { + self.save_shader_pack_config(); + events.push(Event::ReloadShaderPack); + } Page::Packs => { // PackSelectionScreen.onClose: PackSelectionModel.commit. if let Some(p) = self.pack_screen.take() @@ -3416,19 +3423,73 @@ mod tests { false, ); assert_eq!(s.horizon.shader_pack, "", "a second click turns it off"); - let import_btn = find(&mut ui, &s, &|a| *a == Act::ImportShaderPack).unwrap(); + let _ = std::fs::remove_dir_all(&dir); + } + + #[test] + fn a_dropped_pack_is_picked_and_its_settings_apply_on_done() { + let atlas = atlas(); + let mut s = Settings::default(); + let mut ui = OptionsUi::default(); + let size = (480.0, 240.0); + let dir = + std::env::temp_dir().join(format!("horizon-shaderpack-drop-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + ui.shader_packs.poll(dir.clone()); + let sample = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../examples/shaderpacks/horizon-sample"); assert_eq!( + ui.drop_shader_pack(&mut s, &sample), + None, + "only on its page" + ); + ui.show(Page::ShaderPacks); + assert_eq!(ui.drop_shader_pack(&mut s, &sample), Some(Ok(()))); + assert_eq!(s.horizon.shader_pack, "horizon-sample"); + assert!(dir.join("horizon-sample/pack.toml").is_file()); + let again = ui.drop_shader_pack(&mut s, &sample).unwrap().unwrap_err(); + assert!(again.contains("already"), "{again}"); + + let click = |ui: &mut OptionsUi, s: &mut Settings, act: &dyn Fn(&Act) -> bool| { + let w = ui + .layout(s, size.0, size.1) + .0 + .into_iter() + .find(|w| matches!(&w.kind, WKind::Button { act: a, active: true, .. } if act(a))) + .unwrap(); ui.click( - &mut s, + s, &atlas, - import_btn.x + 1.0, - import_btn.y + 1.0, + w.x + 1.0, + w.y + 1.0, size, [480, 240], false, false, - ), - vec![Event::ButtonClick, Event::ImportShaderPack] + ) + }; + click(&mut ui, &mut s, &|a| { + *a == Act::Push(Page::ShaderPackOptions) + }); + assert_eq!(ui.page(), Page::ShaderPackOptions); + for _ in 0..3 { + let events = click(&mut ui, &mut s, &|a| matches!(a, Act::ShaderPackOption(_))); + assert!(!events.contains(&Event::ReloadShaderPack), "{events:?}"); + } + let txt = dir.join("horizon-sample.txt"); + assert!(!txt.exists(), "nothing is saved before Done"); + let events = click(&mut ui, &mut s, &|a| *a == Act::Done); + assert!(events.contains(&Event::ReloadShaderPack), "{events:?}"); + assert_eq!(ui.page(), Page::ShaderPacks); + assert!(txt.is_file(), "Done saves the values"); + click(&mut ui, &mut s, &|a| { + *a == Act::Push(Page::ShaderPackOptions) + }); + let events = click(&mut ui, &mut s, &|a| *a == Act::Done); + assert!( + !events.contains(&Event::ReloadShaderPack), + "no change: no reload" ); let _ = std::fs::remove_dir_all(&dir); } diff --git a/crates/horizon-client/src/shader_pack.rs b/crates/horizon-client/src/shader_pack.rs index f5a15ca79..d00384748 100644 --- a/crates/horizon-client/src/shader_pack.rs +++ b/crates/horizon-client/src/shader_pack.rs @@ -18,6 +18,13 @@ pub const API: u32 = 2; /// The folder beside `options.txt` that holds the packs. pub const DIR: &str = "shaderpacks"; pub const PRELUDE: &str = include_str!("shader_pack_prelude.wgsl"); +/// What api 2 adds after [`PRELUDE`] (`api = 2` packs only). +pub const PRELUDE_API2: &str = include_str!("shader_pack_prelude_api2.wgsl"); +/// [`PRELUDE_API2`]'s bindings that cost a pass when a stage reads them. +const BINDING_SCENE_NORMAL: u32 = 7; +const BINDING_SCENE_MATERIAL: u32 = 8; +const BINDING_OPAQUE_DEPTH: u32 = 9; +const BINDING_SHADOW_DEPTH: u32 = 10; /// The prelude's vertex entry point, shared by every stage. pub const VERTEX_ENTRY: &str = "horizon_stage_vertex"; const MAX_STAGES: usize = 16; @@ -58,8 +65,6 @@ struct StageEntry { entry: Option, scale: Option, format: Option, - #[serde(default)] - mipmap: bool, } #[derive(Deserialize)] #[serde(deny_unknown_fields)] @@ -74,20 +79,11 @@ struct OptionEntry { values: Option>, } -/// Where a stage runs. Api 1 has `composite` only; Api 2 adds `shadow`, -/// `gbuffer`, `deferred` and `final`. +/// Where a stage runs: `composite` only so far (docs/shader-packs.md, Stages). #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum StageKind { - /// Directional sun/moon shadow map pass. - Shadow, - /// Material / G-buffer evaluation pass over world colour, depth and normals. - Gbuffer, - /// Opaque world lighting pass (runs before translucent water/weather). - Deferred, /// After the world pass, before the hand: reads the scene's colour and depth. Composite, - /// Final tonemapping / anti-aliasing pass before HUD and menus. - Final, } #[derive(Clone, Debug)] pub struct Stage { @@ -99,11 +95,13 @@ pub struct Stage { pub entry: String, /// Target size relative to the world's. pub scale: f32, - /// Optional target format name (`rgba16float`, `rgba8unorm`). + /// The target's format ([`STAGE_FORMATS`]); `None`: the screen's. pub format: Option, - /// Whether the previous stage's output is mipmapped before this stage runs. - pub mipmap: bool, + /// The prelude bindings (group 0) the stage reads. + pub bindings: BTreeSet, } +/// The `format`s a stage's target can have. +pub const STAGE_FORMATS: [&str; 3] = ["rgba8unorm", "rgba16float", "rgb10a2unorm"]; #[derive(Clone, Copy, Debug, PartialEq)] pub enum OptionKind { Bool, @@ -377,22 +375,18 @@ impl PackConfig { fn load_java(path: &Path, id: &str, saved: &BTreeMap) -> Result { let pack = JavaPack::open(path)?; let values = pack.values(saved); + // The menus don't know the GPU: `shaders.properties` is read as on an + // unknown one (the screens and profiles of the packs tested don't + // depend on it). let env = Environment::iris("Unknown", "", 4); let props = pack.properties(&values, &env).unwrap_or_default(); let lang = pack.translations("en_us"); let sliders: BTreeSet<&str> = props.sliders().into_iter().collect(); - let option_label = |name: &str, comment: Option<&str>| -> String { - lang.get(&format!("option.{name}")) - .cloned() - .or_else(|| { - comment - .map(str::trim) - .filter(|c| !c.is_empty() && c.len() <= 40 && !c.contains('.')) - .map(str::to_owned) - }) - .unwrap_or_else(|| humanize_identifier(name)) - }; + // Iris's names: the `.lang` entry, else the name itself; the tooltip + // from `.lang`, else the option's comment. + let named = |key: String, name: &str| lang.get(&key).cloned().unwrap_or(name.to_owned()); + let option_label = |name: &str| named(format!("option.{name}"), name); let option_desc = |name: &str, comment: Option<&str>| -> String { lang.get(&format!("option.{name}.comment")) .cloned() @@ -407,7 +401,7 @@ impl PackConfig { let idx = options.len(); options.push(PackOption { key: name.clone(), - label: option_label(name, b.comment.as_deref()), + label: option_label(name), description: option_desc(name, b.comment.as_deref()), kind: OptionKind::Bool, values: Vec::new(), @@ -428,19 +422,12 @@ impl PackConfig { let choices: Vec<(String, String)> = s .allowed .iter() - .map(|raw| { - let label = lang - .get(&format!("value.{name}.{raw}")) - .or_else(|| lang.get(&format!("option.{name}.value.{raw}"))) - .cloned() - .unwrap_or_else(|| raw.clone()); - (raw.clone(), label) - }) + .map(|raw| (raw.clone(), named(format!("value.{name}.{raw}"), raw))) .collect(); let idx = options.len(); options.push(PackOption { key: name.clone(), - label: option_label(name, s.comment.as_deref()), + label: option_label(name), description: option_desc(name, s.comment.as_deref()), kind: OptionKind::Choice { slider: sliders.contains(name.as_str()), @@ -460,10 +447,7 @@ impl PackConfig { .unwrap_or_default() .into_iter() .map(|p| { - let label = lang - .get(&format!("profile.{}", p.name)) - .cloned() - .unwrap_or_else(|| humanize_identifier(&p.name)); + let label = named(format!("profile.{}", p.name), &p.name); PackProfile { name: p.name, label, @@ -501,10 +485,7 @@ impl PackConfig { } } MenuElement::Screen(sub) => { - let label = lang - .get(&format!("screen.{sub}")) - .cloned() - .unwrap_or_else(|| humanize_identifier(&sub)); + let label = named(format!("screen.{sub}"), &sub); items.push(ScreenItem::Subscreen(sub, label)); } MenuElement::Option(opt) => { @@ -523,10 +504,7 @@ impl PackConfig { } let name_str = screen_name.unwrap_or("").to_owned(); let label = match screen_name { - Some(n) => lang - .get(&format!("screen.{n}")) - .cloned() - .unwrap_or_else(|| humanize_identifier(n)), + Some(n) => named(format!("screen.{n}"), n), None => id.to_owned(), }; PackScreen { @@ -700,7 +678,7 @@ pub struct ShaderPack { pub stages: Vec, pub options: Vec, /// Optional custom noise/LUT texture bytes (RGBA8, width, height) from `[textures]`. - pub custom_noise: Option<(u32, u32, Vec)>, + pub custom_noise: Option, /// A Java (OptiFine/Iris) pack's translated passes, instead of `stages`. pub java: Option, dir: PathBuf, @@ -813,6 +791,9 @@ pub fn import_pack(source: &Path, target_dir: &Path) -> Result { if dest.canonicalize().ok() == source.canonicalize().ok() { return Ok(name); } + if dest.exists() { + return Err(format!("{name} is already in {DIR}/")); + } if source.is_file() { fs::copy(&source, &dest).map_err(|e| format!("failed to copy {name}: {e}"))?; } else { @@ -827,10 +808,13 @@ fn copy_dir_recursive(src: &Path, dst: &Path) -> Result<(), String> { for entry in entries.flatten() { let path = entry.path(); let target = dst.join(entry.file_name()); - if path.is_dir() { - copy_dir_recursive(&path, &target)?; - } else { - fs::copy(&path, &target).map_err(|e| format!("{}: {e}", target.display()))?; + // Links aren't followed: one pointing up the tree would never end. + match entry.file_type() { + Ok(t) if t.is_dir() => copy_dir_recursive(&path, &target)?, + Ok(t) if t.is_file() => { + fs::copy(&path, &target).map_err(|e| format!("{}: {e}", target.display()))?; + } + _ => {} } } Ok(()) @@ -858,17 +842,13 @@ impl Folder { self.dir = dir; self.read = Some(std::time::Instant::now()); } - - pub fn invalidate(&mut self) { - self.read = None; - } } /// A Java pack has no manifest: its name is its file name. fn java_info(name: String) -> Info { Info { name, - description: "Iris/OptiFine pack: G-buffers, shadow map and post passes.".into(), + description: "Iris/OptiFine pack: its full-screen passes run.".into(), authors: Vec::new(), version: String::new(), license: String::new(), @@ -888,7 +868,16 @@ fn manifest(dir: &Path) -> Result { Ok(manifest) } -pub(crate) fn decode_png_rgba(bytes: &[u8]) -> Result<(u32, u32, Vec), String> { +/// An RGBA8 image (`decode_png`). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Rgba { + pub width: u32, + pub height: u32, + pub pixels: Vec, +} + +/// A PNG of any colour type as [`Rgba`]. +pub(crate) fn decode_png(bytes: &[u8]) -> Result { let mut decoder = png::Decoder::new(std::io::Cursor::new(bytes)); decoder.set_transformations(png::Transformations::EXPAND | png::Transformations::STRIP_16); let mut reader = decoder.read_info().map_err(|e| e.to_string())?; @@ -913,7 +902,11 @@ pub(crate) fn decode_png_rgba(bytes: &[u8]) -> Result<(u32, u32, Vec), Strin png::ColorType::Grayscale => buf.into_iter().flat_map(|g| [g, g, g, 255]).collect(), _ => return Err("unsupported PNG color format".into()), }; - Ok((w, h, rgba)) + Ok(Rgba { + width: w, + height: h, + pixels: rgba, + }) } impl ShaderPack { @@ -936,18 +929,25 @@ impl ShaderPack { .collect::, _>>()?; let mut stages = Vec::new(); for entry in manifest.stage { - let kind = match (pack_api, entry.kind.as_str()) { - (_, "composite") => StageKind::Composite, - (2.., "shadow") => StageKind::Shadow, - (2.., "gbuffer") => StageKind::Gbuffer, - (2.., "deferred") => StageKind::Deferred, - (2.., "final") => StageKind::Final, - (_, other) => { + let kind = match entry.kind.as_str() { + "composite" => StageKind::Composite, + other => { return Err(format!( - "pack.toml: stage kind `{other}` isn't supported (api {pack_api} has `composite`)" + "pack.toml: stage kind `{other}` isn't supported (api {API} has `composite`)" )); } }; + if pack_api == 1 && entry.format.is_some() { + return Err(format!("pack.toml: {}: `format` needs api 2", entry.shader)); + } + if let Some(format) = entry.format.as_deref() + && !STAGE_FORMATS.contains(&format) + { + return Err(format!( + "pack.toml: stage format `{format}` isn't one of {}", + STAGE_FORMATS.join(", ") + )); + } let scale = entry.scale.unwrap_or(1.0); if !(0.25..=2.0).contains(&scale) { return Err(format!( @@ -961,8 +961,17 @@ impl ShaderPack { } let code = fs::read_to_string(&path).map_err(|e| format!("{}: {e}", entry.shader))?; let entry_point = entry.entry.unwrap_or_else(|| "fs_main".into()); - let source = format!("{PRELUDE}\n{code}"); - validate(&source, &entry.shader, &entry_point)?; + let prelude = match pack_api { + 1 => PRELUDE.to_owned(), + _ => format!("{PRELUDE}\n{PRELUDE_API2}"), + }; + let source = format!("{prelude}\n{code}"); + let bindings = validate( + &source, + prelude.lines().count(), + &entry.shader, + &entry_point, + )?; stages.push(Stage { kind, file: entry.shader, @@ -970,20 +979,21 @@ impl ShaderPack { entry: entry_point, scale, format: entry.format, - mipmap: entry.mipmap, + bindings, }); } let mut custom_noise = None; - if let Some(rel) = manifest - .textures - .get("noise") - .or_else(|| manifest.textures.values().next()) - { + for (name, rel) in &manifest.textures { + if name != "noise" { + return Err(format!( + "pack.toml: texture `{name}` isn't supported (api {API} has `noise`)" + )); + } if rel.contains("..") { return Err(format!("pack.toml: texture {rel} is outside the pack")); } let bytes = fs::read(dir.join(rel)).map_err(|e| format!("{rel}: {e}"))?; - custom_noise = Some(decode_png_rgba(&bytes).map_err(|e| format!("{rel}: {e}"))?); + custom_noise = Some(decode_png(&bytes).map_err(|e| format!("{rel}: {e}"))?); } Ok(Self { id: dir @@ -1016,43 +1026,38 @@ impl ShaderPack { .file_name() .map(|n| n.to_string_lossy().into_owned()) .unwrap_or_default(); - let options = PackConfig::load_java(path, &id, values) - .map(|c| c.options) - .unwrap_or_default(); Ok(Self { info: java_info(id.clone()), id, stages: Vec::new(), - options, + options: Vec::new(), custom_noise: None, java: Some(java), dir: path.to_path_buf(), stamp, }) } - /// Whether this pack requests directional shadow map rendering. - pub fn needs_shadow(&self) -> bool { - if let Some(java) = &self.java { - return java.folders.iter().any(|f| f.shadow_enabled); - } - self.stages.iter().any(|s| { - s.kind == StageKind::Shadow - || s.source.contains("sample_shadow") - || s.source.contains("shadow_depth") - }) + /// Whether a stage reads one of the prelude's `bindings`. + fn reads(&self, bindings: &[u32]) -> bool { + self.stages + .iter() + .any(|s| bindings.iter().any(|b| s.bindings.contains(b))) } - /// Shadow map resolution requested by this pack, and shadow distance in blocks. - pub fn shadow_config(&self, dimension: i8) -> (u32, f32, f32) { - if let Some(java) = &self.java - && let Some(idx) = java.folder_for(dimension) - { - let d = &java.folders[idx].directives; - return ( - d.shadow_map_resolution, - d.shadow_distance, - d.sun_path_rotation, - ); - } + /// Whether a stage reads the shadow map (`shadow_depth`, `sample_shadow`). + pub fn needs_shadow(&self) -> bool { + self.reads(&[BINDING_SHADOW_DEPTH]) + } + /// Whether a stage reads the G-buffer (`scene_normal`, `scene_material`). + pub fn needs_gbuffer(&self) -> bool { + self.reads(&[BINDING_SCENE_NORMAL, BINDING_SCENE_MATERIAL]) + } + /// Whether the world's depth before translucent geometry is kept + /// (`opaque_depth`, which the G-buffer is also built from). + pub fn needs_opaque_depth(&self) -> bool { + self.needs_gbuffer() || self.reads(&[BINDING_OPAQUE_DEPTH]) + } + /// The shadow map's resolution and distance in blocks. + pub fn shadow_config(&self) -> (u32, f32) { let res = self .options .iter() @@ -1065,7 +1070,7 @@ impl ShaderPack { .find(|o| o.key == "shadow_distance" || o.key == "shadowDistance") .map(|o| (o.value as f32).clamp(16.0, 512.0)) .unwrap_or(128.0); - (res, dist, 0.0) + (res, dist) } /// The pipeline-overridable constants every stage gets: the option values. pub fn constants(&self) -> Vec<(&str, f64)> { @@ -1139,8 +1144,15 @@ fn option( /// naga parses and validates the stage (prelude included) and checks its /// fragment entry point; errors point into the pack's own file. -fn validate(source: &str, file: &str, entry: &str) -> Result<(), String> { - let prelude_lines = PRELUDE.lines().count() as u32 + 1; +/// Parse and validate a stage: the prelude's bindings (group 0) its entry +/// point uses, or the error at the stage's own line. +fn validate( + source: &str, + prelude_lines: usize, + file: &str, + entry: &str, +) -> Result, String> { + let prelude_lines = prelude_lines as u32 + 1; let at = |location: Option| match location { Some(l) if l.line_number > prelude_lines => { format!( @@ -1153,20 +1165,28 @@ fn validate(source: &str, file: &str, entry: &str) -> Result<(), String> { }; let module = naga::front::wgsl::parse_str(source) .map_err(|e| format!("{}: {}", at(e.location(source)), e.message()))?; - naga::valid::Validator::new( + let info = naga::valid::Validator::new( naga::valid::ValidationFlags::all(), naga::valid::Capabilities::default(), ) .validate(&module) .map_err(|e| format!("{}: {}", at(e.location(source)), e.as_inner()))?; - if !module + let Some(index) = module .entry_points .iter() - .any(|e| e.name == entry && e.stage == naga::ShaderStage::Fragment) - { + .position(|e| e.name == entry && e.stage == naga::ShaderStage::Fragment) + else { return Err(format!("{file}: no fragment entry point `{entry}`")); - } - Ok(()) + }; + let uses = info.get_entry_point(index); + Ok(module + .global_variables + .iter() + .filter(|(handle, _)| !uses[*handle].is_empty()) + .filter_map(|(_, var)| var.binding.as_ref()) + .filter(|binding| binding.group == 0) + .map(|binding| binding.binding) + .collect()) } /// The option values saved for a pack (`shaderpacks/.txt`, `key=value`). @@ -1312,6 +1332,20 @@ override mode: i32 = 1; MANIFEST.replace("\"composite\"", "\"shadow\""), "stage kind `shadow`", ), + ( + MANIFEST.replace( + "kind = \"composite\"", + "kind = \"composite\"\nformat = \"rgba16float\"", + ), + "`format` needs api 2", + ), + ( + MANIFEST.replace("api = 1", "api = 2").replace( + "kind = \"composite\"", + "kind = \"composite\"\nformat = \"rgba32float\"", + ), + "stage format `rgba32float`", + ), ( MANIFEST.replace("grade.wgsl", "../grade.wgsl"), "outside the pack", @@ -1334,12 +1368,27 @@ override mode: i32 = 1; #[test] fn api_2_stages_and_pack_config_work() { - let manifest_v2 = MANIFEST - .replace("api = 1", "api = 2") - .replace("\"composite\"", "\"deferred\""); + let manifest_v2 = MANIFEST.replace("api = 1", "api = 2").replace( + "kind = \"composite\"", + "kind = \"composite\"\nformat = \"rgba16float\"", + ); let dir = pack(&[("pack.toml", &manifest_v2), ("shaders/grade.wgsl", GRADE)]); let loaded = ShaderPack::load(&dir, &BTreeMap::new()).unwrap(); - assert_eq!(loaded.stages[0].kind, StageKind::Deferred); + assert_eq!(loaded.stages[0].format.as_deref(), Some("rgba16float")); + assert!( + !loaded.needs_shadow() && !loaded.needs_gbuffer() && !loaded.needs_opaque_depth(), + "reading none of them costs no pass" + ); + let opaque = GRADE.replace("depth_at(in.uv)", "opaque_depth_at(in.uv)"); + let reads = pack(&[("pack.toml", &manifest_v2), ("shaders/grade.wgsl", &opaque)]); + let reads = ShaderPack::load(&reads, &BTreeMap::new()).unwrap(); + assert!(reads.needs_opaque_depth() && !reads.needs_gbuffer()); + let api_1 = pack(&[("pack.toml", MANIFEST), ("shaders/grade.wgsl", &opaque)]); + let error = ShaderPack::load(&api_1, &BTreeMap::new()).err().unwrap(); + assert!( + error.contains("opaque_depth_at"), + "api 1 stages don't see api 2's names: {error}" + ); let parent = dir.parent().unwrap(); let id = dir.file_name().unwrap().to_str().unwrap(); let mut cfg = PackConfig::load(parent, id).unwrap(); diff --git a/crates/horizon-client/src/shader_pack_prelude.wgsl b/crates/horizon-client/src/shader_pack_prelude.wgsl index ebe3ad4cf..7b16690f9 100644 --- a/crates/horizon-client/src/shader_pack_prelude.wgsl +++ b/crates/horizon-client/src/shader_pack_prelude.wgsl @@ -1,7 +1,6 @@ -// Horizon shader pack prelude, api 2 (docs/shader-packs.md): placed before -// every stage's source. Bindings, the Frame and FrameExt uniforms, the -// full-screen vertex shader, G-buffer/shadow accessors and PBR/post helpers -// that hide the renderer's reversed depth. +// Horizon shader pack prelude, api 1 (docs/shader-packs.md): placed before +// every stage's source. Bindings, the Frame uniform, the full-screen vertex +// shader and helpers that hide the renderer's reversed depth. struct Frame { view: mat4x4, projection: mat4x4, @@ -20,36 +19,12 @@ struct Frame { view_params: vec4, dimension: vec4, } - -struct FrameExt { - previous_view: mat4x4, - previous_projection: mat4x4, - shadow_view: mat4x4, - shadow_projection: mat4x4, - shadow_inverse_view: mat4x4, - shadow_inverse_projection: mat4x4, - // xyz: previous camera world position, w: frame delta time (seconds) - previous_camera: vec4, - // x: shadow map resolution, y: shadow distance (blocks), z: sun_path_rotation (rad), w: shadow strength - shadow_params: vec4, - // x: eye block light (0..240), y: eye sky light (0..240), z: smoothed wetness (0..1), w: center linear depth - environment: vec4, -} - @group(0) @binding(0) var scene_color: texture_2d; @group(0) @binding(1) var scene_depth: texture_depth_2d; @group(0) @binding(2) var previous: texture_2d; @group(0) @binding(3) var linear_sampler: sampler; @group(0) @binding(4) var nearest_sampler: sampler; @group(0) @binding(5) var frame: Frame; -@group(0) @binding(6) var frame_ext: FrameExt; -@group(0) @binding(7) var scene_normal: texture_2d; -@group(0) @binding(8) var scene_material: texture_2d; -@group(0) @binding(9) var opaque_depth: texture_depth_2d; -@group(0) @binding(10) var shadow_depth: texture_depth_2d; -@group(0) @binding(11) var shadow_sampler: sampler_comparison; -@group(0) @binding(12) var noise_texture: texture_2d; -@group(0) @binding(13) var repeat_sampler: sampler; struct StageIn { @builtin(position) position: vec4, @@ -67,10 +42,6 @@ struct StageIn { fn depth_at(uv: vec2) -> f32 { return textureSampleLevel(scene_depth, nearest_sampler, uv, 0); } -// The opaque world's depth at `uv` (before translucent water and stained glass). -fn opaque_depth_at(uv: vec2) -> f32 { - return textureSampleLevel(opaque_depth, nearest_sampler, uv, 0); -} fn is_sky(depth: f32) -> bool { return depth <= 0.0; } @@ -89,70 +60,6 @@ fn view_position(uv: vec2, depth: f32) -> vec3 { fn world_offset(uv: vec2, depth: f32) -> vec3 { return (frame.inverse_view * vec4(view_position(uv, depth), 1.0)).xyz; } -// World-space surface normal at `uv` from the G-buffer (or reconstructed from depth). -fn world_normal_at(uv: vec2) -> vec3 { - let encoded = textureSampleLevel(scene_normal, nearest_sampler, uv, 0.0); - if encoded.w > 0.001 { - return normalize(encoded.xyz * 2.0 - 1.0); - } - let step_uv = frame.resolution.zw; - let d0 = depth_at(uv); - let p0 = world_offset(uv, d0); - let px = world_offset(uv + vec2(step_uv.x, 0.0), depth_at(uv + vec2(step_uv.x, 0.0))) - p0; - let py = world_offset(uv + vec2(0.0, step_uv.y), depth_at(uv + vec2(0.0, step_uv.y))) - p0; - let n = cross(py, px); - let len = length(n); - if len > 1e-6 { - return n / len; - } - return vec3(0.0, 1.0, 0.0); -} -// G-buffer material data at `uv`: r = block light (0..1), g = sky light (0..1), -// b = water/translucent mask (0..1), a = roughness (0..1). -fn material_at(uv: vec2) -> vec4 { - return textureSampleLevel(scene_material, nearest_sampler, uv, 0.0); -} -// Reproject a camera-relative world position into previous-frame UV and clip depth. -fn reproject_uv(world_rel: vec3) -> vec3 { - let camera_delta = frame.camera_position.xyz - frame_ext.previous_camera.xyz; - let prev_rel = world_rel + camera_delta; - let clip = frame_ext.previous_projection * (frame_ext.previous_view * vec4(prev_rel, 1.0)); - if clip.w <= 1e-5 { - return vec3(-1.0); - } - let ndc = clip.xyz / clip.w; - return vec3(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5, ndc.z); -} -// Sample the directional sun/moon shadow map with 5-tap PCF and normal offset bias. -// Returns 1.0 in full light and 0.0 in full shadow. -fn sample_shadow(world_rel: vec3, world_normal: vec3) -> f32 { - if frame_ext.shadow_params.w <= 0.001 { - return 1.0; - } - let dist = length(world_rel); - let max_dist = max(frame_ext.shadow_params.y, 16.0); - if dist >= max_dist { - return 1.0; - } - let light_dir = normalize(select(frame.moon.xyz, frame.sun.xyz, frame.sun.y >= -0.05)); - let ndotl = max(dot(world_normal, light_dir), 0.0); - let bias_pos = world_rel + world_normal * (0.06 + 0.12 * (1.0 - ndotl)); - let light_clip = frame_ext.shadow_projection * (frame_ext.shadow_view * vec4(bias_pos, 1.0)); - let ndc = light_clip.xyz / max(light_clip.w, 1e-5); - let shadow_uv = vec2(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5); - if any(shadow_uv < vec2(0.002)) || any(shadow_uv > vec2(0.998)) || ndc.z <= 0.0 || ndc.z >= 1.0 { - return 1.0; - } - let texel = 1.0 / max(frame_ext.shadow_params.x, 512.0); - let ref_z = clamp(ndc.z + 0.0008, 0.0, 1.0); - var sum = textureSampleCompareLevel(shadow_depth, shadow_sampler, shadow_uv, ref_z); - sum += textureSampleCompareLevel(shadow_depth, shadow_sampler, shadow_uv + vec2(texel, 0.0), ref_z); - sum += textureSampleCompareLevel(shadow_depth, shadow_sampler, shadow_uv - vec2(texel, 0.0), ref_z); - sum += textureSampleCompareLevel(shadow_depth, shadow_sampler, shadow_uv + vec2(0.0, texel), ref_z); - sum += textureSampleCompareLevel(shadow_depth, shadow_sampler, shadow_uv - vec2(0.0, texel), ref_z); - let fade = smoothstep(max_dist, max_dist * 0.82, dist); - return mix(1.0, sum * 0.2, fade * frame_ext.shadow_params.w); -} fn luminance(c: vec3) -> f32 { return dot(c, vec3(0.2126, 0.7152, 0.0722)); } @@ -162,12 +69,3 @@ fn srgb_to_linear(c: vec3) -> vec3 { fn linear_to_srgb(c: vec3) -> vec3 { return select(1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, c * 12.92, c <= vec3(0.0031308)); } -// ACES fitted filmic tonemapping curve (linear HDR -> linear 0..1). -fn aces_tonemap(x: vec3) -> vec3 { - let a = 2.51; - let b = 0.03; - let c = 2.43; - let d = 0.59; - let e = 0.14; - return clamp((x * (a * x + b)) / (x * (c * x + d) + e), vec3(0.0), vec3(1.0)); -} diff --git a/crates/horizon-client/src/shader_pack_prelude_api2.wgsl b/crates/horizon-client/src/shader_pack_prelude_api2.wgsl new file mode 100644 index 000000000..80e9a205e --- /dev/null +++ b/crates/horizon-client/src/shader_pack_prelude_api2.wgsl @@ -0,0 +1,108 @@ +// Horizon shader pack prelude, api 2 additions (docs/shader-packs.md): placed +// after the api 1 prelude before the stages of an `api = 2` pack, so api 1 +// packs keep every name they may have declared themselves. +struct FrameExt { + previous_view: mat4x4, + previous_projection: mat4x4, + shadow_view: mat4x4, + shadow_projection: mat4x4, + shadow_inverse_view: mat4x4, + shadow_inverse_projection: mat4x4, + // xyz: previous camera world position, w: frame delta time (seconds) + previous_camera: vec4, + // x: shadow map resolution, y: shadow distance (blocks), z: 0, + // w: 1 while the shadow map is drawn this frame, else 0 + shadow_params: vec4, + // x: eye block light (0..240), y: eye sky light (0..240), z: smoothed + // rain (0..1), w: 0 + environment: vec4, +} + +@group(0) @binding(6) var frame_ext: FrameExt; +@group(0) @binding(7) var scene_normal: texture_2d; +@group(0) @binding(8) var scene_material: texture_2d; +@group(0) @binding(9) var opaque_depth: texture_depth_2d; +@group(0) @binding(10) var shadow_depth: texture_depth_2d; +@group(0) @binding(11) var shadow_sampler: sampler_comparison; +@group(0) @binding(12) var noise_texture: texture_2d; +@group(0) @binding(13) var repeat_sampler: sampler; + +// The opaque world's depth at `uv` (before translucent water and stained glass). +fn opaque_depth_at(uv: vec2) -> f32 { + return textureSampleLevel(opaque_depth, nearest_sampler, uv, 0); +} + +// World-space surface normal at `uv` from the G-buffer (or reconstructed from depth). +fn world_normal_at(uv: vec2) -> vec3 { + let encoded = textureSampleLevel(scene_normal, nearest_sampler, uv, 0.0); + if encoded.w > 0.001 { + return normalize(encoded.xyz * 2.0 - 1.0); + } + let step_uv = frame.resolution.zw; + let d0 = depth_at(uv); + let p0 = world_offset(uv, d0); + let px = world_offset(uv + vec2(step_uv.x, 0.0), depth_at(uv + vec2(step_uv.x, 0.0))) - p0; + let py = world_offset(uv + vec2(0.0, step_uv.y), depth_at(uv + vec2(0.0, step_uv.y))) - p0; + let n = cross(py, px); + let len = length(n); + if len > 1e-6 { + return n / len; + } + return vec3(0.0, 1.0, 0.0); +} +// G-buffer material data at `uv`: r = block light (0..1), g = sky light (0..1), +// b = water/translucent mask (0..1), a = roughness (0..1). +fn material_at(uv: vec2) -> vec4 { + return textureSampleLevel(scene_material, nearest_sampler, uv, 0.0); +} +// Reproject a camera-relative world position into previous-frame UV and clip depth. +fn reproject_uv(world_rel: vec3) -> vec3 { + let camera_delta = frame.camera_position.xyz - frame_ext.previous_camera.xyz; + let prev_rel = world_rel + camera_delta; + let clip = frame_ext.previous_projection * (frame_ext.previous_view * vec4(prev_rel, 1.0)); + if clip.w <= 1e-5 { + return vec3(-1.0); + } + let ndc = clip.xyz / clip.w; + return vec3(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5, ndc.z); +} +// Sample the directional sun/moon shadow map with 5-tap PCF and normal offset bias. +// Returns 1.0 in full light and 0.0 in full shadow. +fn sample_shadow(world_rel: vec3, world_normal: vec3) -> f32 { + if frame_ext.shadow_params.w <= 0.001 { + return 1.0; + } + let dist = length(world_rel); + let max_dist = max(frame_ext.shadow_params.y, 16.0); + if dist >= max_dist { + return 1.0; + } + let light_dir = normalize(select(frame.moon.xyz, frame.sun.xyz, frame.sun.y >= -0.05)); + let ndotl = max(dot(world_normal, light_dir), 0.0); + let bias_pos = world_rel + world_normal * (0.06 + 0.12 * (1.0 - ndotl)); + let light_clip = frame_ext.shadow_projection * (frame_ext.shadow_view * vec4(bias_pos, 1.0)); + let ndc = light_clip.xyz / max(light_clip.w, 1e-5); + let shadow_uv = vec2(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5); + if any(shadow_uv < vec2(0.002)) || any(shadow_uv > vec2(0.998)) || ndc.z <= 0.0 || ndc.z >= 1.0 { + return 1.0; + } + let texel = 1.0 / max(frame_ext.shadow_params.x, 512.0); + let ref_z = clamp(ndc.z + 0.0008, 0.0, 1.0); + var sum = textureSampleCompareLevel(shadow_depth, shadow_sampler, shadow_uv, ref_z); + sum += textureSampleCompareLevel(shadow_depth, shadow_sampler, shadow_uv + vec2(texel, 0.0), ref_z); + sum += textureSampleCompareLevel(shadow_depth, shadow_sampler, shadow_uv - vec2(texel, 0.0), ref_z); + sum += textureSampleCompareLevel(shadow_depth, shadow_sampler, shadow_uv + vec2(0.0, texel), ref_z); + sum += textureSampleCompareLevel(shadow_depth, shadow_sampler, shadow_uv - vec2(0.0, texel), ref_z); + let fade = smoothstep(max_dist, max_dist * 0.82, dist); + return mix(1.0, sum * 0.2, fade * frame_ext.shadow_params.w); +} + +// ACES fitted filmic tonemapping curve (linear HDR -> linear 0..1). +fn aces_tonemap(x: vec3) -> vec3 { + let a = 2.51; + let b = 0.03; + let c = 2.43; + let d = 0.59; + let e = 0.14; + return clamp((x * (a * x + b)) / (x * (c * x + d) + e), vec3(0.0), vec3(1.0)); +} diff --git a/crates/horizon-shaderpack/src/options.rs b/crates/horizon-shaderpack/src/options.rs index d25a3d776..3158b12cf 100644 --- a/crates/horizon-shaderpack/src/options.rs +++ b/crates/horizon-shaderpack/src/options.rs @@ -489,11 +489,14 @@ impl OptionValues { out.bools.insert(name.clone(), value); } } + // A saved value the option doesn't list (an old or hand-edited + // file) is dropped, as OptiFine's `ShaderOption.setValue` refuses it. for (name, o) in &options.strings { - if let Some(v) = saved.get(name) { - if *v != o.default { - out.strings.insert(name.clone(), v.clone()); - } + if let Some(v) = saved.get(name) + && *v != o.default + && o.allowed.contains(v) + { + out.strings.insert(name.clone(), v.clone()); } } out @@ -623,6 +626,13 @@ mod tests { !values.bools.contains_key("shadowHardwareFiltering"), "not a boolean: ignored" ); + let stale = [("QUALITY", "abc"), ("sunPathRotation", "")] + .map(|(k, v)| (k.to_owned(), v.to_owned())) + .into(); + assert!( + OptionValues::new(&set, &stale).strings.is_empty(), + "values the options don't list: ignored" + ); let edited: Vec = lines .iter() .enumerate() diff --git a/crates/horizon-shaderpack/src/pipeline.rs b/crates/horizon-shaderpack/src/pipeline.rs index db656e3ae..89898eb07 100644 --- a/crates/horizon-shaderpack/src/pipeline.rs +++ b/crates/horizon-shaderpack/src/pipeline.rs @@ -323,8 +323,6 @@ pub struct Passes { /// next frame starts unflipped. pub flipped_at_end: BTreeSet, pub directives: Directives, - /// Whether the folder uses shadow mapping (`shadow` program or `shadowtex*`). - pub shadow_enabled: bool, } /// The `const` directives of a program folder that aren't about targets @@ -341,24 +339,6 @@ pub struct Directives { pub wetness_half_life: f32, /// `drynessHalflife`, tenths of a second: `wetness` smoothing when dry. pub dryness_half_life: f32, - /// `centerDepthHalflife`, tenths of a second: `centerDepthSmooth`. - pub center_depth_half_life: f32, - /// `shadowMapResolution` (or `SHADOW_RES`), in pixels per side. - pub shadow_map_resolution: u32, - /// `shadowDistance`, in blocks. - pub shadow_distance: f32, - /// `shadowDistanceRenderMul` (-1 when unset). - pub shadow_distance_render_mul: f32, - /// `ambientOcclusionLevel` (0.0..=1.0). - pub ambient_occlusion_level: f32, - /// `noiseTextureResolution`, in pixels per side. - pub noise_texture_resolution: u32, - /// `shadowHardwareFiltering` / `shadowHardwareFiltering0..1`. - pub shadow_hardware_filtering: [bool; 2], - /// `shadowtex0Nearest` / `shadowtex1Nearest`. - pub shadow_nearest: [bool; 2], - /// `generateShadowMipmap` / `shadowtex0Mipmap` / `shadowtex1Mipmap`. - pub shadow_mipmap: [bool; 2], } impl Default for Directives { @@ -368,15 +348,6 @@ impl Default for Directives { eye_brightness_half_life: 10.0, wetness_half_life: 600.0, dryness_half_life: 200.0, - center_depth_half_life: 1.0, - shadow_map_resolution: 2048, - shadow_distance: 128.0, - shadow_distance_render_mul: -1.0, - ambient_occlusion_level: 1.0, - noise_texture_resolution: 256, - shadow_hardware_filtering: [false; 2], - shadow_nearest: [false; 2], - shadow_mipmap: [false; 2], } } } @@ -387,66 +358,13 @@ impl Directives { let mut out = Self::default(); for fragment in fragments { for c in const_directives(fragment) { - let raw = c.value.trim_end_matches(['f', 'F']); - let fval = raw.parse::(); - let ival = raw.parse::(); - let bval = raw.parse::(); - match (c.ty, c.key.as_str()) { - ("float", "sunPathRotation") if fval.is_ok() => { - out.sun_path_rotation = fval.unwrap(); - } - ("float", "eyeBrightnessHalflife") if fval.is_ok() => { - out.eye_brightness_half_life = fval.unwrap(); - } - ("float", "wetnessHalflife") if fval.is_ok() => { - out.wetness_half_life = fval.unwrap(); - } - ("float", "drynessHalflife") if fval.is_ok() => { - out.dryness_half_life = fval.unwrap(); - } - ("float", "centerDepthHalflife") if fval.is_ok() => { - out.center_depth_half_life = fval.unwrap(); - } - ("float", "shadowDistance") if fval.is_ok() => { - out.shadow_distance = fval.unwrap().clamp(8.0, 1024.0); - } - ("float", "shadowDistanceRenderMul") if fval.is_ok() => { - out.shadow_distance_render_mul = fval.unwrap(); - } - ("float", "ambientOcclusionLevel") if fval.is_ok() => { - out.ambient_occlusion_level = fval.unwrap().clamp(0.0, 1.0); - } - ("int", "shadowMapResolution") if ival.is_ok() => { - out.shadow_map_resolution = ival.unwrap().clamp(64, 8192); - } - ("int", "noiseTextureResolution") if ival.is_ok() => { - out.noise_texture_resolution = ival.unwrap().clamp(16, 2048); - } - ("bool", "shadowHardwareFiltering") if bval.is_ok() => { - out.shadow_hardware_filtering = [bval.unwrap(); 2]; - } - ("bool", "shadowHardwareFiltering0") if bval.is_ok() => { - out.shadow_hardware_filtering[0] = bval.unwrap(); - } - ("bool", "shadowHardwareFiltering1") if bval.is_ok() => { - out.shadow_hardware_filtering[1] = bval.unwrap(); - } - ("bool", "shadowtexNearest" | "shadowtex0Nearest" | "shadow0MinMagNearest") - if bval.is_ok() => - { - out.shadow_nearest[0] = bval.unwrap(); - } - ("bool", "shadowtex1Nearest" | "shadow1MinMagNearest") if bval.is_ok() => { - out.shadow_nearest[1] = bval.unwrap(); - } - ("bool", "generateShadowMipmap" | "shadowtexMipmap" | "shadowtex0Mipmap") - if bval.is_ok() => - { - out.shadow_mipmap[0] = bval.unwrap(); - } - ("bool", "shadowtex1Mipmap") if bval.is_ok() => { - out.shadow_mipmap[1] = bval.unwrap(); - } + // Java's `Float.parseFloat` takes a trailing `f`. + let value = c.value.trim_end_matches(['f', 'F']).parse::(); + match (c.ty, c.key.as_str(), value) { + ("float", "sunPathRotation", Ok(v)) => out.sun_path_rotation = v, + ("float", "eyeBrightnessHalflife", Ok(v)) => out.eye_brightness_half_life = v, + ("float", "wetnessHalflife", Ok(v)) => out.wetness_half_life = v, + ("float", "drynessHalflife", Ok(v)) => out.dryness_half_life = v, _ => {} } } @@ -568,19 +486,9 @@ impl JavaPack { for name in &groups[0..2].concat() { fragments.insert(name.clone(), fragment(name)?); } - let shadow_enabled = ["shadow", "shadow_solid", "shadow_cutout"] - .iter() - .any(|s| exists(s)) - || sources.iter().any(|s| { - s.contains("shadowtex0") - || s.contains("shadowtex1") - || s.contains("watershadow") - || s.contains("shadow2D") - }); let mut out = Passes { targets: target_settings(sources.iter().map(String::as_str), properties), directives: Directives::read(sources.iter().map(String::as_str)), - shadow_enabled, ..Passes::default() }; @@ -858,14 +766,14 @@ mod tests { assert_eq!(Directives::read([]), Directives::default()); assert_eq!(Directives::default().eye_brightness_half_life, 10.0); let d = Directives::read([ - "const float sunPathRotation = 30.0;\nconst float eyeBrightnessHalflife = 4.0f;\nconst int shadowMapResolution = 4096;\nconst float shadowDistance = 192.0;", + "const float sunPathRotation = 30.0;\nconst float eyeBrightnessHalflife = 4.0f;\nconst float wetnessHalflife = 300.0;", "const float sunPathRotation = -40.0; // Complementary Unbound", "const int sunPathRotation = 5;", ]); assert_eq!(d.sun_path_rotation, -40.0); assert_eq!(d.eye_brightness_half_life, 4.0); - assert_eq!(d.shadow_map_resolution, 4096); - assert_eq!(d.shadow_distance, 192.0); + assert_eq!(d.wetness_half_life, 300.0); + assert_eq!(d.dryness_half_life, 200.0, "the default"); } #[test] diff --git a/crates/horizon-shaderpack/src/translate.rs b/crates/horizon-shaderpack/src/translate.rs index 623ad760d..27fafe34b 100644 --- a/crates/horizon-shaderpack/src/translate.rs +++ b/crates/horizon-shaderpack/src/translate.rs @@ -806,20 +806,11 @@ impl<'a> Legaliser<'a> { fn unit(&mut self, unit: &mut ast::TranslationUnit) -> Result<(), String> { // Functions, their sampler parameters, and struct definitions first. for ed in &unit.0 { - if let ast::ExternalDeclarationData::Declaration(d) = &ed.content { - match &d.content { - ast::DeclarationData::InitDeclaratorList(list) => { - if let ast::TypeSpecifierNonArrayData::Struct(s) = - &list.head.ty.ty.ty.content - { - self.record_struct(s); - } - } - ast::DeclarationData::TypeOnly(q) => { - let _ = q; - } - _ => {} - } + if let ast::ExternalDeclarationData::Declaration(d) = &ed.content + && let ast::DeclarationData::InitDeclaratorList(list) = &d.content + && let ast::TypeSpecifierNonArrayData::Struct(s) = &list.head.ty.ty.ty.content + { + self.record_struct(s); } let proto = match &ed.content { ast::ExternalDeclarationData::FunctionDefinition(f) => &f.prototype, diff --git a/docs/shader-packs.md b/docs/shader-packs.md index 0ba8981fb..6718be0f7 100644 --- a/docs/shader-packs.md +++ b/docs/shader-packs.md @@ -13,16 +13,18 @@ Both are off by default. With no pack selected the renderer draws exactly what i | Part | State | | --- | --- | | Renderer layout (below) | today's code | -| Horizon shader packs: `composite`, `deferred`, `gbuffer`, `shadow`, `final` stages (API 2), uniforms, custom textures, hot reload | done (`examples/shaderpacks/horizon-ultra` + `horizon-sample`) | -| Shader Packs UI: import (`.zip`/folder dialog + drag-and-drop), profiles, sub-screens, sliders, toggles, reset to defaults | done (Horizon & Classic Video/Graphics settings) | -| Real-time shadow map pass (`"Horizon shadow"`, `shadowtex0/1`, `shadowModelView`, `shadowProjection`) & pre-translucent depth (`depthtex1/2`) | done | -| G-buffer normal & material pass (`"Horizon gbuffer"`, `scene_normal`, `scene_material`, `colortex1..3` initialization) | done | -| Java shader packs: preprocessor (`crates/horizon-shaderpack`: includes, options, Iris's macros, JCPP's `#if` and `#define` rules, `.lang` files) | done: ten packs tested (Compatibility, below) | -| Java shader packs: GLSL → WGSL (`horizon_shaderpack::translate`: syntax-tree legaliser, naga, 2D array & struct varyings, vertex `textureLod`, `mat2(vec4)`, `1 << 31` shifts) | done | -| Java shader packs: `shaders.properties` (`horizon_shaderpack::properties`: screens, sliders, profiles, `program.*.enabled`, image formats, blend modes, custom textures) | done, wired to UI and GPU passes | -| Java shader packs: full-screen pass plan (`horizon_shaderpack::pipeline`: `DRAWBUFFERS`/`RENDERTARGETS`, `colortexN` formats and clearing, buffer flips, blend state) | done | -| Java shader packs: full-screen passes drawn (`java_pack.rs`, `gfx/java_stages.rs`: `begin`, `prepare`, `deferred*`, `composite*`, `final` over Horizon's world) | done: with shadow maps, `depthtex1/2`, custom PNG textures, GL blend modes, `wetness`, `centerDepthSmooth` | -| Java shader packs: custom uniforms (`horizon_shaderpack::uniforms`), `sunPathRotation`, `clouds`/`sun`/`moon`, eye light | done | +| Horizon shader packs: `composite` stage, uniforms, hot reload, Shader Packs page | done (the page: list, pick, open the folder, drop a pack on it) | +| Horizon shader packs, api 2: stage `format`s, `[profiles]`, option descriptions, a `noise` texture, the opaque depth, a G-buffer reconstructed from depth, a sun/moon shadow map (Stages, below) | first slice: `examples/shaderpacks/horizon-ultra` | +| Horizon shader packs: `deferred`, `gbuffer`, `shadow` stages (the pack's own code in the world pass) | planned | +| Shader Pack Settings page: a pack's options, sub-screens, sliders, profiles and Reset, Horizon and Java packs alike; applied once on Done, as Iris does | done | +| Java shader packs: preprocessing + GLSL parse coverage | spike done: 450 of 470 programs reach valid WGSL (below) | +| Java shader packs: preprocessor (`crates/horizon-shaderpack`: includes, options, Iris's macros, JCPP's `#if` and `#define` rules, `.lang` files) | done: every program of the three test packs preprocesses; ten packs tested (Compatibility, below) | +| Java shader packs: GLSL → WGSL (`horizon_shaderpack::translate`: syntax-tree legaliser, naga; struct and nested-array varyings, vertex `texture`, `mat2(vec4)`, `1 << 31`) | done: the 464 programs the three test packs enable by default translate (below) | +| Java shader packs: `shaders.properties` (`horizon_shaderpack::properties`: screens, sliders, profiles, `program.*.enabled`, image formats, blend modes, `texture.*`) | done: read like Iris for the three test packs | +| Java shader packs: full-screen pass plan (`horizon_shaderpack::pipeline`: `DRAWBUFFERS`/`RENDERTARGETS`, `colortexN` formats and clearing, buffer flips, `blend.*`) | done: planned for the three test packs | +| Java shader packs: full-screen passes drawn (`java_pack.rs`, `gfx/java_stages.rs`: `begin`, `prepare`, `deferred*`, `composite*`, `final` over Horizon's world) | first slice: BSL and Complementary run (below); `depthtex1`/`depthtex2` before translucent geometry, `blend.*`, PNG `texture..*` and `texture.noise` | +| Java shader packs: custom uniforms (`horizon_shaderpack::uniforms`), `sunPathRotation`, `clouds`/`sun`/`moon`, eye light, `wetness` | done for the full-screen passes (below) | +| Java shader packs: `gbuffers_*`, `shadow`, `.dat` and resource-location textures, images, compute | planned | ## How the renderer draws a frame today @@ -63,7 +65,14 @@ The lightmap, fog and sky colours are computed on the CPU (`environment.rs`, `fo Horizon Settings... → Shader Packs...**, and the Shader Pack row of the graphics settings lead to it. It shows the folder's full path and an *Open Folder* button (the folder is made if it is missing), then one switch per pack: turning one on picks it, turning it off is the vanilla picture. With no pack, it says so under the path. The list is read again every - second while the page is open, so a pack dropped into the folder shows up. + second while the page is open, so a pack dropped into the folder shows up. A `.zip` or pack folder dropped on the + window while the page shows is copied into the folder and picked (Iris's `ShaderPackScreen.onFilesDrop`); one of + that name already there is left alone. +- With a pack on that has options, **Shader Pack Settings...** opens them: Horizon packs' `[options]` and + `[profiles]`, Java packs' `#define`/`const` options laid out by `shaders.properties` (`screen`, `screen.`, + `sliders`, `profile.*`) and named by `lang/en_us.lang` (`option.*`, `value.*`, `screen.*`, `profile.*`, as Iris; + without one, the option's own name). The values are saved and the pack built again once, when the page closes + (Iris applies on Done), not on every click or slider step. A saved value an option doesn't list is ignored. - The choice is kept in `horizon-graphics.txt` (`shaderPack:`, empty = off). A pack's option values are kept in `shaderpacks/.txt`, as Iris does. - A pack that fails to load or compile leaves the vanilla picture (or the last pack that worked) on and says why on @@ -79,7 +88,7 @@ my-pack/ composite.wgsl bloom.wgsl textures/ - noise.png # planned + noise.png # api 2: [textures] noise LICENSE ``` @@ -112,6 +121,24 @@ step = 0.05 [options.vignette] label = "Vignette" default = true +description = "Darkens the corners." # optional: the button's tooltip +``` + +Api 2 (`api = 2`) adds: + +```toml +[[stage]] +kind = "composite" +shader = "shaders/lighting.wgsl" +format = "rgba16float" # this stage's target: rgba8unorm, rgba16float or rgb10a2unorm; the last stage + # always writes the screen's format, so earlier ones can keep HDR colour + +[profiles.low] # the Profile button cycles these; "Custom" when the values match none +vignette = false +saturation = 1.0 + +[textures] +noise = "textures/noise.png" # replaces the prelude's noise_texture (256x256 random RGB without it) ``` Options become WGSL [pipeline-overridable constants](https://www.w3.org/TR/WGSL/#override-decls): the shader @@ -176,6 +203,29 @@ Prelude helpers: `linear_depth(depth) -> f32` (blocks from the eye), `view_posit The uniform layout only grows: a new field goes at the end and the `api` number goes up; a pack that declares an older `api` still loads. +An `api = 2` pack's stages get a second prelude after the first (`shader_pack_prelude_api2.wgsl`; api 1 packs don't, +so names they declare themselves can't clash): + +| Binding | WGSL | What | +| --- | --- | --- | +| 6 | `frame_ext: FrameExt` (uniform) | last frame's view and projection and camera, the shadow map's matrices, resolution and distance, eye light, smoothed rain | +| 7, 8 | `scene_normal`, `scene_material: texture_2d` | the G-buffer (below) | +| 9 | `opaque_depth: texture_depth_2d` | the world's depth before translucent geometry (water, glass, ice) | +| 10, 11 | `shadow_depth: texture_depth_2d`, `shadow_sampler: sampler_comparison` | the sun's (the moon's at night) shadow map | +| 12, 13 | `noise_texture: texture_2d`, `repeat_sampler: sampler` | `[textures] noise`, linear, repeat | + +Helpers: `opaque_depth_at(uv)`, `world_normal_at(uv)`, `material_at(uv)` (block light, sky light, translucent mask, +roughness), `reproject_uv(world_offset)` (last frame's uv and depth), `sample_shadow(world_offset, normal)` (5-tap +PCF, 1 lit .. 0 shadowed), `aces_tonemap(rgb)`. + +What each costs, only while a stage reads it (naga reports the bindings each entry point uses): the opaque depth, a +depth copy between the opaque and translucent halves of the world pass; the G-buffer, one full-screen pass after +the world; the shadow map, a depth pass of the terrain and entities from the light. Limits of this first slice: the +G-buffer is reconstructed from depth and colour (normals from neighbouring depths, block light estimated from +brightness, sky light the camera's), not written by the world's shaders; the shadow map holds the sections the +camera draws, so casters outside the view throw no shadow, and none of the terrain where Metal draws sections as one +list (`gfx/flat.rs`, more than 4096 of them). The `deferred`, `gbuffer` and `shadow` stages above replace these. + ### How it renders With a pack on, the world pass draws into the world target of `gfx/world_post.rs` (it exists already for resolution diff --git a/examples/shaderpacks/horizon-ultra/pack.toml b/examples/shaderpacks/horizon-ultra/pack.toml index 446115073..9a59eec90 100644 --- a/examples/shaderpacks/horizon-ultra/pack.toml +++ b/examples/shaderpacks/horizon-ultra/pack.toml @@ -1,15 +1,15 @@ # Horizon Ultra: multi-pass API 2 WGSL shader pack with PCF directional shadows, -# G-buffer GTAO, water SSR & absorption, volumetric godrays, HDR bloom and ACES tonemapping. +# screen-space AO, water SSR & absorption, screen-space godrays, HDR bloom and ACES tonemapping. [pack] name = "Horizon Ultra" -description = "PCF sun/moon shadows, GTAO, water reflections, volumetric godrays, bloom and ACES filmic tonemapping." +description = "PCF sun/moon shadows, ambient occlusion, water reflections, godrays, bloom and ACES filmic tonemapping." authors = ["Horizon contributors"] version = "2.0.0" license = "MIT" api = 2 [[stage]] -kind = "deferred" +kind = "composite" shader = "shaders/deferred_lighting.wgsl" format = "rgba16float" @@ -23,10 +23,9 @@ kind = "composite" shader = "shaders/bloom_godrays.wgsl" scale = 0.5 format = "rgba16float" -mipmap = true [[stage]] -kind = "final" +kind = "composite" shader = "shaders/final_tonemap.wgsl" [profiles.low] @@ -82,8 +81,8 @@ max = 1.0 step = 0.05 [options.ao_strength] -label = "Ambient Occlusion (GTAO)" -description = "Screen-space horizon ambient occlusion in corners and crevices." +label = "Ambient Occlusion" +description = "Screen-space ambient occlusion in corners and crevices." default = 0.75 min = 0.0 max = 1.5 @@ -95,7 +94,7 @@ description = "Screen-space sky and terrain reflections on water and translucent default = true [options.godrays] -label = "Volumetric Godrays" +label = "Godrays" description = "Atmospheric light shafts cast through trees and terrain towards the sun." default = true diff --git a/examples/shaderpacks/horizon-ultra/shaders/bloom_godrays.wgsl b/examples/shaderpacks/horizon-ultra/shaders/bloom_godrays.wgsl index 534e9d983..d175eab6c 100644 --- a/examples/shaderpacks/horizon-ultra/shaders/bloom_godrays.wgsl +++ b/examples/shaderpacks/horizon-ultra/shaders/bloom_godrays.wgsl @@ -1,5 +1,5 @@ // Stage 3 (Composite, half-resolution): HDR bloom extraction + 9-tap tent filter -// and radial volumetric light shafts (godrays) towards the sun/moon. +// and radial light shafts (screen-space godrays) towards the sun/moon. override godrays: bool = true; override bloom_strength: f32 = 0.35; @@ -30,7 +30,7 @@ override bloom_strength: f32 = 0.35; } } - // Radial volumetric godrays cast from the sun's screen projection. + // Radial godrays from the sun's screen projection. var shafts = vec3(0.0); if godrays && frame.camera_state.x == 0.0 && frame.dimension.y > 0.5 { let sun_clip = frame.projection * (frame.view * vec4(frame.sun.xyz * 128.0, 1.0)); diff --git a/examples/shaderpacks/horizon-ultra/shaders/deferred_lighting.wgsl b/examples/shaderpacks/horizon-ultra/shaders/deferred_lighting.wgsl index 3930a5c21..e44773f37 100644 --- a/examples/shaderpacks/horizon-ultra/shaders/deferred_lighting.wgsl +++ b/examples/shaderpacks/horizon-ultra/shaders/deferred_lighting.wgsl @@ -1,10 +1,10 @@ -// Stage 1 (Deferred): G-buffer normal reconstruction, multi-tap GTAO ambient +// Stage 1: G-buffer normals, 8-tap screen-space ambient // occlusion, directional sun/moon PCF shadow mapping, and rain puddle wetness. override shadows: bool = true; override shadow_strength: f32 = 0.85; override ao_strength: f32 = 0.75; -fn compute_gtao(uv: vec2, depth: f32, view_pos: vec3) -> f32 { +fn compute_ao(uv: vec2, depth: f32, view_pos: vec3) -> f32 { if ao_strength <= 0.001 { return 1.0; } @@ -60,7 +60,7 @@ fn compute_gtao(uv: vec2, depth: f32, view_pos: vec3) -> f32 { let mat = material_at(in.uv); // Ambient occlusion from depth discontinuity neighborhood. - let ao = compute_gtao(in.uv, depth, view_pos); + let ao = compute_ao(in.uv, depth, view_pos); // Directional sun/moon shadow factor. var shadow = 1.0; From fe7bb3a34b54da4792b6f9b201a6e2eb2bd4c9ca Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:02:12 +0200 Subject: [PATCH 21/39] Java packs: the world goes where the pack's terrain program draws MakeUp's gbuffers write colortex1, and its composites read the world from there: with the world in colortex0, it showed nothing but fog. Iris's fallbacks for terrain pick the program (gbuffers_terrain, then textured_lit, textured, basic); its first draw buffer takes the world. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx/java_stages.rs | 29 ++++++++++------ crates/horizon-client/src/java_pack.rs | 6 +++- crates/horizon-shaderpack/src/pipeline.rs | 18 ++++++++++ crates/horizon-shaderpack/tests/pack.rs | 36 ++++++++++++++++++++ 4 files changed, 77 insertions(+), 12 deletions(-) diff --git a/crates/horizon-client/src/gfx/java_stages.rs b/crates/horizon-client/src/gfx/java_stages.rs index f86055919..5079a8f03 100644 --- a/crates/horizon-client/src/gfx/java_stages.rs +++ b/crates/horizon-client/src/gfx/java_stages.rs @@ -2,7 +2,8 @@ //! Iris's colour targets (`colortex0..`, each with a main and an alternate //! side) and the pack's `begin`, `prepare`, `deferred`, `composite` and //! `final` programs as full-screen passes over them, as Iris's -//! `CompositeRenderer` runs them. Horizon's world enters `colortex0` and +//! `CompositeRenderer` runs them. Horizon's world enters the target the +//! pack's terrain program draws to (`colortex0` for most packs) and //! `depthtex0` after `prepare`, where Iris's gbuffers programs would have //! drawn (`depthtex1`/`depthtex2`: its depth before translucent geometry), //! and the last picture goes to `world_post`. The @@ -249,7 +250,8 @@ struct Bound { /// Per pass, then `final`. groups: Vec, world_in: (wgpu::RenderPipeline, wgpu::BindGroup), - /// The world goes to colortex0's alternate side. + /// The world's target, and whether the world goes to its alternate side. + world_target: u32, world_alt: bool, final_target: Option, present: (wgpu::RenderPipeline, wgpu::BindGroup), @@ -262,6 +264,7 @@ struct Bound { struct Folder { targets: Vec, world_at: usize, + world_target: u32, flipped_after_prepare: BTreeSet, flipped_at_end: BTreeSet, passes: Vec, @@ -326,6 +329,7 @@ impl JavaStages { Ok::<_, String>(Folder { targets: plan.targets.clone(), world_at: plan.world_at, + world_target: plan.world_target, flipped_after_prepare: plan.flipped_after_prepare.clone(), flipped_at_end: plan.flipped_at_end.clone(), passes, @@ -486,8 +490,9 @@ impl JavaStages { let folder = &self.folders[folder_index]; let plan = folder; let all = folder.passes.iter().chain(&folder.final_pass); - // Every target a pass reads or writes, and colortex0 (the world). - let mut used: BTreeSet = [0].into(); + // Every target a pass reads or writes, colortex0 (shown without + // `final`) and the world's. + let mut used: BTreeSet = [0, folder.world_target].into(); for pass in all.clone() { used.extend(pass.draw_buffers.iter().copied()); used.extend(pass.samplers.iter().filter_map(|s| match s.source { @@ -588,15 +593,15 @@ impl JavaStages { }) .collect(); - // The world goes into colortex0's side the gbuffers would write. - let colortex0 = &targets[&0]; + // The world goes into its target's side the gbuffers would write. + let world_target = &targets[&folder.world_target]; let world_pipeline = blit_pipeline( device, &self.common, &self.common.world_layout, "world_in", &[ - colortex0.format, + world_target.format, wgpu::TextureFormat::R32Float, wgpu::TextureFormat::R32Float, ], @@ -630,6 +635,7 @@ impl JavaStages { .create_view(&Default::default()) }); // Without `final`, Iris shows colortex0 as the last pass left it. + let colortex0 = &targets[&0]; let shown = final_target.clone().unwrap_or_else(|| { if plan.flipped_at_end.contains(&0) { colortex0.alt_view.clone() @@ -668,7 +674,8 @@ impl JavaStages { opaque_depth, groups, world_in: (world_pipeline, world_group), - world_alt: plan.flipped_after_prepare.contains(&0), + world_target: folder.world_target, + world_alt: plan.flipped_after_prepare.contains(&folder.world_target), final_target, present: (present_pipeline, present_group), output_view, @@ -679,11 +686,11 @@ impl JavaStages { } fn world_in(encoder: &mut wgpu::CommandEncoder, bound: &Bound) { - let colortex0 = &bound.targets[&0]; + let target = &bound.targets[&bound.world_target]; let side = if bound.world_alt { - &colortex0.alt_view + &target.alt_view } else { - &colortex0.main_view + &target.main_view }; let attachments = [ Some(color_attachment(side, None)), diff --git a/crates/horizon-client/src/java_pack.rs b/crates/horizon-client/src/java_pack.rs index 5e592d1b6..ee4d29d61 100644 --- a/crates/horizon-client/src/java_pack.rs +++ b/crates/horizon-client/src/java_pack.rs @@ -23,8 +23,11 @@ pub struct JavaFolder { /// `begin*`, `prepare*`, `deferred*`, `composite*`, in order. pub passes: Vec, /// How many of `passes` run before the world (`begin*`, `prepare*`): the - /// world's colour and depth enter `colortex0` and `depthtex*` after them. + /// world's colour and depth enter `world_target` and `depthtex*` after them. pub world_at: usize, + /// The colour target the world goes into, where the pack's terrain + /// program draws (MakeUp: colortex1). + pub world_target: u32, /// The targets whose alternate side the world is written to. pub flipped_after_prepare: BTreeSet, pub final_pass: Option, @@ -145,6 +148,7 @@ pub fn load( folder: name.to_owned(), targets: plan.targets, world_at: early.count(), + world_target: plan.world_target, passes, flipped_after_prepare: plan.flipped_after_prepare, final_pass, diff --git a/crates/horizon-shaderpack/src/pipeline.rs b/crates/horizon-shaderpack/src/pipeline.rs index 89898eb07..d851fc23a 100644 --- a/crates/horizon-shaderpack/src/pipeline.rs +++ b/crates/horizon-shaderpack/src/pipeline.rs @@ -313,6 +313,10 @@ pub struct Passes { /// The targets gbuffers programs (and Horizon's world) write on their /// alternate side, before and after translucent geometry. pub flipped_after_prepare: BTreeSet, + /// The target Horizon's world goes into: the first one `gbuffers_terrain` + /// (or the program Iris falls back to for terrain) draws to, `colortex0` + /// without one. + pub world_target: u32, pub deferred: Vec, pub flipped_after_translucent: BTreeSet, pub composite: Vec, @@ -532,6 +536,20 @@ impl JavaPack { out.begin = run(&groups[0], "begin_pre", &mut flipped)?; out.prepare = run(&groups[1], "prepare_pre", &mut flipped)?; out.flipped_after_prepare = flipped.clone(); + // Iris's fallbacks for terrain (`ProgramFallbackResolver`). + let terrain = [ + "gbuffers_terrain", + "gbuffers_textured_lit", + "gbuffers_textured", + "gbuffers_basic", + ] + .into_iter() + .find(|name| exists(name)); + if let Some(name) = terrain { + let buffers = + draw_buffers(&fragment(name)?).map_err(|e| format!("{}: {e}", path(name)))?; + out.world_target = buffers.and_then(|b| b.first().copied()).unwrap_or(0); + } out.deferred = run(&groups[2], "deferred_pre", &mut flipped)?; out.flipped_after_translucent = flipped.clone(); out.composite = run(&groups[3], "composite_pre", &mut flipped)?; diff --git a/crates/horizon-shaderpack/tests/pack.rs b/crates/horizon-shaderpack/tests/pack.rs index c957d7fb4..1e2ee8158 100644 --- a/crates/horizon-shaderpack/tests/pack.rs +++ b/crates/horizon-shaderpack/tests/pack.rs @@ -292,6 +292,8 @@ fn plans_the_full_screen_passes() { assert_eq!(passes.composite[0].mipmapped, [3].into()); assert_eq!(passes.composite[1].scale, 0.5); assert_eq!(passes.flipped_after_prepare, [].into()); + // No gbuffers programs: the world goes into colortex0. + assert_eq!(passes.world_target, 0); assert_eq!(passes.flipped_after_translucent, [3].into()); assert_eq!(passes.flipped_at_end, [1, 2, 3].into()); let final_pass = passes.final_pass.unwrap(); @@ -302,6 +304,40 @@ fn plans_the_full_screen_passes() { assert_eq!(final_pass.reads_alt, [1, 2, 3].into()); } +/// The world enters the target terrain draws to first (MakeUp's colortex1), +/// through Iris's fallbacks when `gbuffers_terrain` is missing or disabled. +#[test] +fn the_world_goes_where_terrain_draws() { + let dir = scratch("world-target"); + let shaders = dir.join("shaders"); + std::fs::create_dir_all(&shaders).unwrap(); + let write = |name: &str, text: &str| std::fs::write(shaders.join(name), text).unwrap(); + for program in ["composite", "gbuffers_terrain", "gbuffers_textured"] { + write(&format!("{program}.vsh"), "void main() {}\n"); + } + write("composite.fsh", "void main() {}\n"); + write( + "gbuffers_textured.fsh", + "void main() {\n/* DRAWBUFFERS:12 */\n gl_FragData[0] = vec4(1.0);\n}\n", + ); + write( + "gbuffers_terrain.fsh", + "void main() {\n#ifdef GRASS\n/* DRAWBUFFERS:42 */\n#else\n/* DRAWBUFFERS:3 */\n#endif\n gl_FragData[0] = vec4(1.0);\n}\n", + ); + let target = |properties: &str| { + write("shaders.properties", properties); + let pack = JavaPack::open(&dir).unwrap(); + let values = pack.values(&BTreeMap::new()); + let properties = pack.properties(&values, &env()).unwrap(); + pack.passes("", &values, &env(), &properties) + .unwrap() + .world_target + }; + assert_eq!(target(""), 3); + assert_eq!(target("program.gbuffers_terrain.enabled = false\n"), 1); + std::fs::remove_dir_all(dir).unwrap(); +} + fn scratch(name: &str) -> PathBuf { std::env::temp_dir().join(format!("horizon-shaderpack-{name}-{}", std::process::id())) } From 963d5cedd3671d1c04569b89d98940cd7d9187f9 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:02:12 +0200 Subject: [PATCH 22/39] Shader packs: keep the opaque depth and G-buffer only for packs that read them The world target made the opaque depth, normal and material textures for every scaled or anti-aliased frame, packs or not: 12 bytes a pixel nobody read. They now follow what the pack reads, and Post::generation tells the stages to bind again when its targets change (a recreated target of the same size, or a shadow map made after the stages bound). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 12 +- crates/horizon-client/src/gfx/java_stages.rs | 11 +- crates/horizon-client/src/gfx/pack_stages.rs | 10 +- crates/horizon-client/src/gfx/world_post.rs | 199 +++++++++++-------- 4 files changed, 140 insertions(+), 92 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 255f4dd0a..6a154d0de 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -3707,9 +3707,15 @@ impl Renderer { } else { self.world_post.shadow_active = false; } - let scaled = - self.world_post - .prepare(&self.device, [self.config.width, self.config.height], pack); + let scaled = self.world_post.prepare( + &self.device, + [self.config.width, self.config.height], + pack, + world_post::Extras { + opaque_depth, + gbuffer, + }, + ); let (world_view, world_depth) = match &scaled { Some((color, depth)) => (color, depth), None => (&view_target, &self.depth), diff --git a/crates/horizon-client/src/gfx/java_stages.rs b/crates/horizon-client/src/gfx/java_stages.rs index 5079a8f03..14d25745e 100644 --- a/crates/horizon-client/src/gfx/java_stages.rs +++ b/crates/horizon-client/src/gfx/java_stages.rs @@ -244,6 +244,8 @@ struct Common { struct Bound { folder: usize, size: [u32; 2], + /// `world_post`'s targets it binds (`Post::generation`). + post_generation: u64, targets: BTreeMap, depth: wgpu::TextureView, opaque_depth: wgpu::TextureView, @@ -382,11 +384,9 @@ impl JavaStages { ) -> &wgpu::BindGroup { let dimension = (frame.dimension[0] as i32).clamp(-1, 1); let folder = self.folder_for[(dimension + 1) as usize].unwrap_or(0); - if self - .bound - .as_ref() - .is_none_or(|b| b.folder != folder || b.size != size) - { + if self.bound.as_ref().is_none_or(|b| { + b.folder != folder || b.size != size || b.post_generation != post.generation() + }) { self.bound = Some(self.bind(device, queue, folder, color, depth, size, post)); } let now = std::time::Instant::now(); @@ -669,6 +669,7 @@ impl JavaStages { Bound { folder: folder_index, size, + post_generation: post.generation(), targets, depth, opaque_depth, diff --git a/crates/horizon-client/src/gfx/pack_stages.rs b/crates/horizon-client/src/gfx/pack_stages.rs index f80a64ce1..a067812e8 100644 --- a/crates/horizon-client/src/gfx/pack_stages.rs +++ b/crates/horizon-client/src/gfx/pack_stages.rs @@ -129,12 +129,12 @@ pub struct PackStages { shadow_sampler: wgpu::Sampler, noise_view: wgpu::TextureView, passes: Vec, - /// Built for this world target size. + /// Built for this world target size and `world_post` generation. targets: Vec, /// `world_post`'s bindings of the last stage's output. output: Option, world_size: [u32; 2], - shadow_size: u32, + post_generation: u64, started: std::time::Instant, last: Option, frames: u32, @@ -330,7 +330,7 @@ impl PackStages { targets: Vec::new(), output: None, world_size: [0, 0], - shadow_size: 0, + post_generation: u64::MAX, started: std::time::Instant::now(), last: None, frames: 0, @@ -355,11 +355,11 @@ impl PackStages { eye_light: [u8; 2], ) -> &wgpu::BindGroup { if self.world_size != size - || self.shadow_size != post.shadow_resolution + || self.post_generation != post.generation() || self.targets.len() != self.passes.len() { self.world_size = size; - self.shadow_size = post.shadow_resolution; + self.post_generation = post.generation(); self.targets.clear(); for pass in &self.passes { let target_size = size.map(|v| ((v as f32 * pass.scale).round() as u32).max(1)); diff --git a/crates/horizon-client/src/gfx/world_post.rs b/crates/horizon-client/src/gfx/world_post.rs index f592e8a1f..cb3e4b41c 100644 --- a/crates/horizon-client/src/gfx/world_post.rs +++ b/crates/horizon-client/src/gfx/world_post.rs @@ -102,20 +102,33 @@ struct GbufferUniform { env: [f32; 4], } +/// What the frame's shader pack reads beyond the world's colour and depth: +/// `Post` keeps these targets only while one does. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct Extras { + pub opaque_depth: bool, + pub gbuffer: bool, +} + struct WorldTarget { size: [u32; 2], + extras: Extras, color: wgpu::TextureView, depth_tex: wgpu::Texture, depth: wgpu::TextureView, - opaque_depth_tex: wgpu::Texture, - opaque_depth: wgpu::TextureView, - normal: wgpu::TextureView, - material: wgpu::TextureView, - gbuffer_group: wgpu::BindGroup, + /// With `extras.opaque_depth` or `extras.gbuffer` (which reads it). + opaque_depth: Option<(wgpu::Texture, wgpu::TextureView)>, + gbuffer: Option, group: wgpu::BindGroup, opaque_captured: bool, } +struct Gbuffer { + normal: wgpu::TextureView, + material: wgpu::TextureView, + group: wgpu::BindGroup, +} + struct ShadowTarget { resolution: u32, depth: wgpu::TextureView, @@ -136,6 +149,8 @@ pub struct Post { pub antialias: bool, target: Option, shadow: Option, + /// Bumped whenever a target is made: bindings of the old ones are stale. + generation: u64, fallback_shadow: wgpu::TextureView, fallback_color: wgpu::TextureView, pub shadow_view: Mat4, @@ -363,6 +378,7 @@ impl Post { antialias: false, target: None, shadow: None, + generation: 0, fallback_shadow, fallback_color, shadow_view: Mat4::IDENTITY, @@ -374,12 +390,14 @@ impl Post { } /// The world's colour and depth targets this frame, or `None` to draw - /// it straight to the screen. `pack`: a shader pack runs this frame. + /// it straight to the screen. `pack`: a shader pack runs this frame, + /// reading `extras`. pub fn prepare( &mut self, device: &wgpu::Device, window: [u32; 2], pack: bool, + extras: Extras, ) -> Option<(wgpu::TextureView, wgpu::TextureView)> { if self.scale == 1.0 && !self.antialias && !pack { self.target = None; @@ -387,7 +405,12 @@ impl Post { return None; } let size = window.map(|v| ((v as f32 * self.scale).round() as u32).clamp(1, 8192)); - if self.target.as_ref().is_none_or(|t| t.size != size) { + if self + .target + .as_ref() + .is_none_or(|t| t.size != size || t.extras != extras) + { + self.generation += 1; let extent = wgpu::Extent3d { width: size[0], height: size[1], @@ -419,63 +442,69 @@ impl Post { | wgpu::TextureUsages::COPY_SRC, ); let depth = depth_tex.create_view(&Default::default()); - let opaque_depth_tex = make_tex( - "Opaque world depth", - wgpu::TextureFormat::Depth32Float, - wgpu::TextureUsages::RENDER_ATTACHMENT - | wgpu::TextureUsages::TEXTURE_BINDING - | wgpu::TextureUsages::COPY_DST, - ); - let opaque_depth = opaque_depth_tex.create_view(&Default::default()); - let normal = make_tex( - "World G-buffer normal", - wgpu::TextureFormat::Rgba8Unorm, - wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, - ) - .create_view(&Default::default()); - let material = make_tex( - "World G-buffer material", - wgpu::TextureFormat::Rgba8Unorm, - wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, - ) - .create_view(&Default::default()); - let gbuffer_group = device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("Horizon gbuffer"), - layout: &self.gbuffer_layout, - entries: &[ - wgpu::BindGroupEntry { - binding: 0, - resource: wgpu::BindingResource::TextureView(&color), - }, - wgpu::BindGroupEntry { - binding: 1, - resource: wgpu::BindingResource::TextureView(&depth), - }, - wgpu::BindGroupEntry { - binding: 2, - resource: wgpu::BindingResource::TextureView(&opaque_depth), - }, - wgpu::BindGroupEntry { - binding: 3, - resource: wgpu::BindingResource::Sampler(&self.nearest), - }, - wgpu::BindGroupEntry { - binding: 4, - resource: self.gbuffer_uniform.as_entire_binding(), - }, - ], + let opaque_depth = (extras.opaque_depth || extras.gbuffer).then(|| { + let tex = make_tex( + "Opaque world depth", + wgpu::TextureFormat::Depth32Float, + wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + ); + let view = tex.create_view(&Default::default()); + (tex, view) }); + let gbuffer = opaque_depth + .as_ref() + .filter(|_| extras.gbuffer) + .map(|(_, opaque)| { + let color_target = |label| { + make_tex( + label, + wgpu::TextureFormat::Rgba8Unorm, + wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TEXTURE_BINDING, + ) + .create_view(&Default::default()) + }; + let group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Horizon gbuffer"), + layout: &self.gbuffer_layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::TextureView(&color), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::TextureView(&depth), + }, + wgpu::BindGroupEntry { + binding: 2, + resource: wgpu::BindingResource::TextureView(opaque), + }, + wgpu::BindGroupEntry { + binding: 3, + resource: wgpu::BindingResource::Sampler(&self.nearest), + }, + wgpu::BindGroupEntry { + binding: 4, + resource: self.gbuffer_uniform.as_entire_binding(), + }, + ], + }); + Gbuffer { + normal: color_target("World G-buffer normal"), + material: color_target("World G-buffer material"), + group, + } + }); let group = self.bind(device, &color); self.target = Some(WorldTarget { size, + extras, color, depth_tex, depth, - opaque_depth_tex, opaque_depth, - normal, - material, - gbuffer_group, + gbuffer, group, opaque_captured: false, }); @@ -523,6 +552,7 @@ impl Post { }) .create_view(&Default::default()); self.shadow = Some(ShadowTarget { resolution, depth }); + self.generation += 1; } let light_dir = if sun_dir.y >= -0.05 { sun_dir.normalize_or_zero() @@ -557,16 +587,19 @@ impl Post { /// right before translucent water and weather draw. pub fn capture_opaque_depth(&mut self, encoder: &mut wgpu::CommandEncoder) { let Some(t) = &mut self.target else { return }; + let Some((opaque, _)) = &t.opaque_depth else { + return; + }; encoder.copy_texture_to_texture( t.depth_tex.as_image_copy(), - t.opaque_depth_tex.as_image_copy(), + opaque.as_image_copy(), t.depth_tex.size(), ); t.opaque_captured = true; } /// Reconstruct the G-buffer `normal` and `material` textures after the - /// world pass finishes. + /// world pass finishes (with `Extras::gbuffer`). pub fn build_gbuffer( &mut self, queue: &wgpu::Queue, @@ -575,15 +608,11 @@ impl Post { eye_light: [u8; 2], wetness: f32, ) { - let Some(t) = &mut self.target else { return }; - if !t.opaque_captured { - encoder.copy_texture_to_texture( - t.depth_tex.as_image_copy(), - t.opaque_depth_tex.as_image_copy(), - t.depth_tex.size(), - ); - t.opaque_captured = true; + if !self.target.as_ref().is_some_and(|t| t.opaque_captured) { + self.capture_opaque_depth(encoder); } + let Some(t) = &self.target else { return }; + let Some(gbuffer) = &t.gbuffer else { return }; let [w, h] = t.size.map(|v| v.max(1) as f32); let uniform = GbufferUniform { inverse_view: frame.inverse_view, @@ -599,7 +628,7 @@ impl Post { queue.write_buffer(&self.gbuffer_uniform, 0, bytemuck::bytes_of(&uniform)); let attachments = [ Some(wgpu::RenderPassColorAttachment { - view: &t.normal, + view: &gbuffer.normal, resolve_target: None, depth_slice: None, ops: wgpu::Operations { @@ -608,7 +637,7 @@ impl Post { }, }), Some(wgpu::RenderPassColorAttachment { - view: &t.material, + view: &gbuffer.material, resolve_target: None, depth_slice: None, ops: wgpu::Operations { @@ -623,7 +652,7 @@ impl Post { ..Default::default() }); pass.set_pipeline(&self.gbuffer_pipeline); - pass.set_bind_group(0, &t.gbuffer_group, &[]); + pass.set_bind_group(0, &gbuffer.group, &[]); pass.draw(0..3, 0..1); } @@ -632,25 +661,37 @@ impl Post { self.target.as_ref().map(|t| t.size) } - /// The depth before translucent geometry (`capture_opaque_depth`). + /// Changes whenever `prepare` or `prepare_shadow` makes new targets: + /// bindings made from the views below must be made again. + pub fn generation(&self) -> u64 { + self.generation + } + + /// The depth before translucent geometry (`capture_opaque_depth`), or + /// the world's depth when nothing reads it. pub fn opaque_depth(&self) -> &wgpu::TextureView { - self.target - .as_ref() - .map_or(&self.fallback_shadow, |t| &t.opaque_depth) + match &self.target { + Some(WorldTarget { + opaque_depth: Some((_, view)), + .. + }) => view, + Some(t) => &t.depth, + None => &self.fallback_shadow, + } } /// Reconstructed world-space normals (`scene_normal`). pub fn normal(&self) -> &wgpu::TextureView { - self.target - .as_ref() - .map_or(&self.fallback_color, |t| &t.normal) + self.gbuffer().map_or(&self.fallback_color, |g| &g.normal) } /// Reconstructed material and light (`scene_material`). pub fn material(&self) -> &wgpu::TextureView { - self.target - .as_ref() - .map_or(&self.fallback_color, |t| &t.material) + self.gbuffer().map_or(&self.fallback_color, |g| &g.material) + } + + fn gbuffer(&self) -> Option<&Gbuffer> { + self.target.as_ref()?.gbuffer.as_ref() } /// The sun/moon shadow map (`shadow_depth` of api 2 packs). From 39ae54379601b12984c302462cf437ada48db790 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:02:12 +0200 Subject: [PATCH 23/39] Shader packs: test importing zips and folders, and refusing what isn't a pack Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/shader_pack.rs | 57 ++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/crates/horizon-client/src/shader_pack.rs b/crates/horizon-client/src/shader_pack.rs index d00384748..b1be9d3ea 100644 --- a/crates/horizon-client/src/shader_pack.rs +++ b/crates/horizon-client/src/shader_pack.rs @@ -1428,4 +1428,61 @@ override mode: i32 = 1; assert_eq!(values["invert"], "true"); assert!(load_values(&dir, "other").is_empty()); } + + /// Dropping a pack on the Shader Packs page: zips and folders, Java + /// and Horizon, and the things that aren't packs. + #[test] + fn imports_zips_and_folders_and_refuses_the_rest() { + let shaderpacks = scratch(); + let java = pack(&[("shaders/composite.fsh", "void main() {}\n")]); + let zip_path = scratch().join("Java Pack.zip"); + let mut zip = zip::ZipWriter::new(fs::File::create(&zip_path).unwrap()); + zip.start_file( + "Java Pack/shaders/final.fsh", + zip::write::SimpleFileOptions::default(), + ) + .unwrap(); + std::io::Write::write_all(&mut zip, b"void main() {}\n").unwrap(); + zip.finish().unwrap(); + + assert_eq!( + import_pack(&zip_path, &shaderpacks).unwrap(), + "Java Pack.zip" + ); + assert!(shaderpacks.join("Java Pack.zip").is_file()); + let folder = import_pack(&java, &shaderpacks).unwrap(); + assert!( + shaderpacks + .join(&folder) + .join("shaders/composite.fsh") + .is_file() + ); + // One already there is refused; one dropped from shaderpacks/ itself + // is just picked. + let again = import_pack(&zip_path, &shaderpacks).unwrap_err(); + assert!(again.contains("already"), "{again}"); + assert_eq!( + import_pack(&shaderpacks.join("Java Pack.zip"), &shaderpacks).unwrap(), + "Java Pack.zip" + ); + + let not_a_pack = pack(&[("readme.txt", "hello")]); + assert!( + import_pack(¬_a_pack, &shaderpacks) + .unwrap_err() + .contains("not a shader pack") + ); + let fake_zip = scratch().join("broken.zip"); + fs::write(&fake_zip, "not a zip").unwrap(); + assert!(import_pack(&fake_zip, &shaderpacks).is_err()); + let broken_horizon = pack(&[("pack.toml", "name = ")]); + assert!(import_pack(&broken_horizon, &shaderpacks).is_err()); + assert!(import_pack(&shaderpacks.join("missing"), &shaderpacks).is_err()); + let left: Vec<_> = fs::read_dir(&shaderpacks) + .unwrap() + .flatten() + .map(|e| e.file_name()) + .collect(); + assert_eq!(left.len(), 2, "nothing copied for refused packs: {left:?}"); + } } From 4a61134eddb414c367be96bb4c57bf654bf1e7a8 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:06:25 +0200 Subject: [PATCH 24/39] CI verify: test without building the examples cargo test --workspace built and linked all ~275 examples, 59% of the test build (review-plan section 1), though none of their tests run. Test the libraries, binaries and integration tests, then the doc tests, as tools/gate.py and tools/pr_ready.py already do. Clippy --all-targets still type-checks every example, and the parity job builds and runs them. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8966854c2..17075add1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,9 +64,13 @@ jobs: cargo +stable clippy --workspace --all-targets -- -D warnings # The server build (released as horizon-engine): the game without sound or controllers. cargo +stable clippy -p horizon-client --all-targets --no-default-features -- -D warnings + # Libraries, binaries and integration tests, then doc tests: not the ~275 examples, which clippy above already + # type-checks and the parity job builds and runs (they were most of this step's build and link time). - name: Test Rust core and voxel kernel correctness if: steps.rust.outputs.required != 'false' - run: cargo +stable test --workspace + run: | + cargo +stable test --workspace --lib --bins --tests + cargo +stable test --workspace --doc parity: if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository uses: ./.github/workflows/parity.yml From ac54227465703e3e18c5462eb22735fa44fd1439 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:08:47 +0200 Subject: [PATCH 25/39] parity: prebuild probes with each harness's own package selection The probe prebuild ran `cargo build --workspace --examples --bins` (or named targets with --workspace). The harnesses build with `-p `, and Cargo unifies features over the selected packages: under --workspace, flate2, libc, memchr, syn, thiserror, postcard and digest get other features for horizon-world, and horizon-mods (pack) and horizon-telemetry (upload) for horizon-client. So every probe the prebuild made was a variant no harness used, and each harness rebuilt its crate and its probe again while the others waited on Cargo's lock. Record each probe with its command's package and feature flags and whether that command builds examples (which turns dev-dependencies on), and prebuild one Cargo run per such selection. --bins and --examples are recorded too, so the fast-profile horizon-client build that save_roundtrip and world_reopen run is prebuilt as well. A harness that never ran here takes its probes from the shared dependency lists instead of a build of all ~275 examples. Co-Authored-By: Claude Opus 5.5 --- tests/test_parity_runner.py | 31 +++++++++-- tools/parity.py | 103 ++++++++++++++++++++++++++++-------- 2 files changed, 110 insertions(+), 24 deletions(-) diff --git a/tests/test_parity_runner.py b/tests/test_parity_runner.py index ffec8d3d5..beffb557d 100644 --- a/tests/test_parity_runner.py +++ b/tests/test_parity_runner.py @@ -16,10 +16,35 @@ class ParityRunnerTests(unittest.TestCase): def test_artifacts_follow_profile_and_stop_at_program_arguments(self): self.assertEqual(parity.artifacts_of(["cargo", "build", "--release", "--example", "a", "--bin", "b"]), - [["example", "a", "release"], ["bin", "b", "release"]]) + [["example", "a", "release", [], True], ["bin", "b", "release", [], True]]) self.assertEqual(parity.artifacts_of(["cargo", "run", "--profile", "fast", "--example", "c", "--", "--example", "x"]), - [["example", "c", "fast"]]) - self.assertEqual(parity.artifacts_of(["cargo", "build", "--example", "d"]), [["example", "d", "dev"]]) + [["example", "c", "fast", [], True]]) + self.assertEqual(parity.artifacts_of(["cargo", "build", "--example", "d"]), [["example", "d", "dev", [], True]]) + + def test_artifacts_keep_the_package_and_feature_selection(self): + self.assertEqual(parity.artifacts_of(["cargo", "build", "--locked", "--profile=fast", "-j", "4", "--package", + "horizon-client", "--bins", "-F", "x"]), + [["bins", "", "fast", ["-p", "horizon-client", "--features", "x"], False]]) + self.assertEqual(parity.artifacts_of(["cargo", "build", "--release", "-p", "horizon-world", "--bin=horizon-world"]), + [["bin", "horizon-world", "release", ["-p", "horizon-world"], False]]) + + def test_prebuild_repeats_each_harness_selection(self): + artifacts = [ + *parity.artifacts_of(["cargo", "build", "--release", "-p", "horizon-world", "--example", "a"]), + *parity.artifacts_of(["cargo", "build", "--release", "-p", "horizon-world", "--example", "b"]), + *parity.artifacts_of(["cargo", "build", "--release", "-p", "horizon-world", "--bin", "horizon-world"]), + *parity.artifacts_of(["cargo", "build", "--release", "--bin", "horizon-world"]), + *parity.artifacts_of(["cargo", "run", "--profile", "fast", "-p", "horizon-client", "--example", "c"]), + ["example", "old", "release"], # recorded before selections were kept: its own crate's -p + ["example", "unknown", "release"], # no longer in the workspace: left to its harness + ] + self.assertEqual(parity.prebuild_commands(artifacts, {("example", "old"): "horizon-world"}), [ + ["cargo", "build", "--locked", "--profile", "fast", "-p", "horizon-client", "--example", "c"], + ["cargo", "build", "--locked", "--profile", "release", "--bin", "horizon-world"], + ["cargo", "build", "--locked", "--profile", "release", "-p", "horizon-world", "--bin", "horizon-world"], + ["cargo", "build", "--locked", "--profile", "release", "-p", "horizon-world", "--example", "a", "--example", "b", + "--example", "old"], + ]) def test_java_sources_include_classes_pulled_from_the_sourcepath(self): with tempfile.TemporaryDirectory() as temp: diff --git a/tools/parity.py b/tools/parity.py index 6ba4c0d90..5dbf13554 100644 --- a/tools/parity.py +++ b/tools/parity.py @@ -27,9 +27,10 @@ Harness probes build into .local/parity-target with release optimisations but without LTO and with 16 codegen units (`PROFILE` below), which builds in seconds instead of minutes. Rust does not let those settings change results: there is no fast-math, and overflow checks stay as in release. Probes built in -the dev profile are optimised too, keeping their overflow checks. Probes the -recorded harnesses need are built in one Cargo run before the harnesses start, -so parallel harnesses don't queue on Cargo's lock. +the dev profile are optimised too, keeping their overflow checks. The probes the +selected harnesses built last time are built before they start, so parallel +harnesses don't queue on Cargo's lock, with each harness's own package and +feature flags so that its Cargo run then finds them fresh. """ import argparse @@ -210,18 +211,42 @@ def artifact_path(kind, name, profile): return TARGET / directory / ("examples" if kind == "example" else "") / name +SELECTION_FLAGS = {"-p", "--package", "-F", "--features"} # followed by a value +SELECTION_SWITCHES = {"--workspace", "--all", "--all-features", "--no-default-features"} + + def artifacts_of(arguments): - """The [kind, name, profile] targets a cargo build/run command line names.""" + """The [kind, name, profile, selection, examples] targets a cargo build/run command line names. + + kind is example, bin, or examples/bins (every one, name ""). selection is the command's package and feature + flags, and examples whether the command builds any example (which turns on dev-dependencies): together they + decide how Cargo unifies features, so the probe prebuild must repeat them exactly. A `--workspace` build of + the same example gives its dependencies other features, and the harness's own `-p` build then compiles + everything again.""" arguments = arguments[:arguments.index("--")] if "--" in arguments else arguments profile = "release" if "--release" in arguments else "dev" - if "--profile" in arguments: - profile = arguments[arguments.index("--profile") + 1] + selection = [] found = [] - for flag, kind in (("--example", "example"), ("--bin", "bin")): - for index, argument in enumerate(arguments[:-1]): - if argument == flag: - found.append([kind, arguments[index + 1], profile]) - return found + index = 1 + while index < len(arguments): + argument = arguments[index] + flag, _, value = argument.partition("=") + if flag in SELECTION_FLAGS | {"--profile", "--example", "--bin"} and not value and index + 1 < len(arguments): + value = arguments[index + 1] + index += 1 + if flag == "--profile": + profile = value + elif flag in SELECTION_FLAGS: + selection += ["-p" if flag == "--package" else "--features" if flag == "-F" else flag, value] + elif flag in SELECTION_SWITCHES: + selection.append("--workspace" if flag == "--all" else flag) + elif flag in ("--example", "--bin"): + found.append([flag[2:], value]) + elif flag in ("--examples", "--bins"): + found.append([flag[2:], ""]) + index += 1 + examples = any(kind in ("example", "examples") for kind, _ in found) + return [[kind, name, profile, selection, examples] for kind, name in found] RUST_PATH = re.compile(r"(? Date: Fri, 23 Oct 2026 14:16:51 +0200 Subject: [PATCH 26/39] Shader Packs page: the pack's settings button sits beside the folder button As in Iris's ShaderPackScreen: at the end of the list it scrolled out of sight with a few packs. It's greyed out while the picked pack has no options. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/options_ui.rs | 56 +++++++++++++++---------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs index 1bb9953bf..66ceb93e1 100644 --- a/crates/horizon-client/src/options_ui.rs +++ b/crates/horizon-client/src/options_ui.rs @@ -1236,6 +1236,14 @@ impl OptionsUi { self.shader_pack_config = Some(config); self.shader_pack_edited = false; } + /// The picked pack has options or profiles to show. + fn shader_pack_has_settings(&self, s: &Settings) -> bool { + self.shader_pack_config.as_ref().is_some_and(|c| { + !s.horizon.shader_pack.is_empty() + && c.id == s.horizon.shader_pack + && (!c.options.is_empty() || !c.profiles.is_empty()) + }) + } fn shader_pack_dir(&self) -> std::path::PathBuf { if self.shader_packs.dir.as_os_str().is_empty() { crate::shader_pack::folder() @@ -1683,23 +1691,6 @@ impl OptionsUi { None, )); } - let has_settings = !s.horizon.shader_pack.is_empty() - && self.shader_pack_config.as_ref().is_some_and(|c| { - c.id == s.horizon.shader_pack - && (!c.options.is_empty() || !c.profiles.is_empty()) - }); - if has_settings { - rows.push(Row::Buttons( - button( - 0.0, - 0.0, - 310.0, - Text::literal("Shader Pack Settings..."), - Act::Push(Page::ShaderPackOptions), - ), - None, - )); - } rows } Page::ShaderPackOptions => { @@ -2185,6 +2176,10 @@ impl OptionsUi { if matches!(page, Page::Video | Page::Controls) { top = 38.0; } + if page == Page::ShaderPacks { + // Two footer rows, as Iris's ShaderPackScreen. + bottom = h - 57.0; + } out.push(Widget { x: 0.0, y: if matches!(page, Page::Video | Page::Controls) { @@ -2273,15 +2268,34 @@ impl OptionsUi { out[list_start..].iter_mut().for_each(|w| w.list = true); let folder = match page { Page::Mods => Some(("Open Mod Folder", Act::OpenModFolder)), - Page::ShaderPacks => { - Some(("Open Shader Pack Folder", Act::OpenShaderPackFolder)) - } Page::ShaderPackOptions => { Some(("Reset to Defaults", Act::ResetShaderPackOptions)) } _ => None, }; - if let Some((label, act)) = folder { + if page == Page::ShaderPacks { + // Iris: the folder and the pack's settings above Done. + out.push(button( + cx - 154.0, + h - 51.0, + 150.0, + Text::literal("Open Shader Pack Folder"), + Act::OpenShaderPackFolder, + )); + let settings = button( + cx + 4.0, + h - 51.0, + 150.0, + Text::literal("Shader Pack Settings..."), + Act::Push(Page::ShaderPackOptions), + ); + out.push(if self.shader_pack_has_settings(s) { + settings + } else { + inactive(settings) + }); + out.push(done(h - 27.0)); + } else if let Some((label, act)) = folder { out.push(button( cx - 154.0, h - 27.0, From d9591047fa44e7fc5c1bb3f2c4863e5bd0bc83e4 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:17:13 +0200 Subject: [PATCH 27/39] Shader pack docs: MakeUp, Chocapic13' and Photon on the Java path, and where the world goes Co-Authored-By: Claude Opus 5.5 --- docs/shader-packs.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/shader-packs.md b/docs/shader-packs.md index 6718be0f7..3cd6a7191 100644 --- a/docs/shader-packs.md +++ b/docs/shader-packs.md @@ -427,9 +427,11 @@ What makes Horizon look like OpenGL to the pack: Measured on the Mac mini (M4 Pro) with the packs' default options: BSL v10.1.8 and Complementary Reimagined r5.9.3 grade and light Horizon's world as their post-processing would. Sildur's Vibrant v2.02 draws only its sky: its `deferred` reads the albedo `gbuffers_*` write to `colortex4`. Without `gbuffers_*` and `shadow`, no pack has its -shadows, water or sky look yet; Distant Horizons' and Voxy's depth read as far. Not supported yet: images and -compute (`.csh`; a pack that needs them doesn't load), mipmapped targets (`colortexNMipmapEnabled` is read but -ignored), custom textures, the option screen; shadow matrices are identity. +shadows, water or sky look yet; Distant Horizons' and Voxy's depth read as far. Horizon's world goes into the target +the pack's `gbuffers_terrain` (or the program Iris falls back to) draws to first: `colortex0` for most packs, +`colortex1` for MakeUp. Not supported yet: images and compute (`.csh`; a pack that needs them doesn't load), +mipmapped targets (`colortexNMipmapEnabled` is read but ignored), `.dat` and resource-location custom textures (PNG +files in the pack only); shadow matrices are identity. ### Compatibility @@ -438,6 +440,7 @@ the authors' pages (dev-only test inputs, kept outside the repository). "Loads" the overworld, Nether and End is preprocessed, translated and validated, as the game does when the pack is picked (`java_pack::tests::real_packs_load` with `HORIZON_TEST_SHADERPACKS`); "drawn" means it was also picked in the game and screenshotted. Only full-screen passes run (above), so no pack has its own terrain, water, shadows or sky yet. +The MakeUp, Chocapic13' and Photon rows are from 2026-10-23, on Metal and on Vulkan (RTX 4080). | Pack | Version | Result | First thing that stops it | | --- | --- | --- | --- | @@ -446,10 +449,12 @@ and screenshotted. Only full-screen passes run (above), so no pack has its own t | Complementary Unbound | r5.8 | loads: 27 passes | | | Sildur's Vibrant Extreme | 2.02 | drawn: 18 passes, only its sky (above) | | | Steadfast | 0.8.0 | drawn: 3 passes | | +| MakeUp Ultra Fast | 9.6 | drawn: 6 passes, its fog, light shafts and grade over Horizon's world | | +| Chocapic13' | V9 High | drawn: 16 passes; its default TAA upscaling shrinks the world in its `gbuffers_*` vertex programs, which don't run, so the picture is magnified 1/0.7: turn TAA Upscaling off in its settings | | | Bliss | 2.1.2 | fails | `world0/deferred.vsh`: an operation naga forbids in a vertex shader | | Bloop | 1.8.0-Alpha-3 | fails | `world0/prepare1.fsh`: `mat2(vec4)` and other constructors naga's GLSL front end refuses | | Noble | v1.9.9 | fails (Apple and NVIDIA) | `world0/deferred4.vsh`: a `vec3[9][9]` varying (nested arrays between stages) | -| Photon | v1.4 | fails | `world0/prepare.vsh`: a struct varying (`CloudsParameters`) | +| Photon | v1.4 | fails | `world0/deferred1.fsh`: `textureGather` | | Rethinking Voxels | r0.1-beta9 | fails | `world0/prepare.fsh`: `1 << 31` overflows naga's constant folding; it also needs images | What the translator does for these packs, as Iris (JCPP and its GLSL transformer) does: From c9c53629d4d52398305a73fddb0d6ed0d66f9ea9 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:36:58 +0200 Subject: [PATCH 28/39] parity: run world_spawn in four shards world_spawn starts a vanilla and a Horizon server for each of 60 seeds, one at a time: 1,144-1,548 s on the CI runner, the parity job's longest harness and its floor whenever a PR's changes reach it. With PARITY_SHARDS = 4 each shard creates every fourth seed's worlds, in its own build directory. The shard helper datafix had moves to tools/parity_shard.py, which both use. It stays out of lab.py, which every harness imports. Co-Authored-By: Claude Opus 5.5 --- tools/parity_shard.py | 21 +++++++++++++++++++++ tools/test_datafix_parity.py | 14 +++----------- tools/test_world_spawn_parity.py | 12 +++++++++--- 3 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 tools/parity_shard.py diff --git a/tools/parity_shard.py b/tools/parity_shard.py new file mode 100644 index 000000000..249235ffb --- /dev/null +++ b/tools/parity_shard.py @@ -0,0 +1,21 @@ +"""A sharded harness's share of its cases. + +A harness that declares PARITY_SHARDS = n runs as n entries in tools/parity.py, which sets PARITY_SHARD=i/n for +each. Outside the runner PARITY_SHARD is unset and a harness runs everything. Kept out of lab.py: every harness +imports lab.py, so a change there reruns them all. +""" +import os + + +def shard(items): + """Every n-th item from the i-th, or all of them outside the runner.""" + spec = os.environ.get("PARITY_SHARD") + if not spec: + return items + index, count = map(int, spec.split("/")) + return items[index - 1::count] + + +def label(): + """A directory name for this share ("2-4", or "all"): shards run at once and must not share outputs.""" + return os.environ.get("PARITY_SHARD", "all").replace("/", "-") diff --git a/tools/test_datafix_parity.py b/tools/test_datafix_parity.py index a2d11009c..4c189983f 100644 --- a/tools/test_datafix_parity.py +++ b/tools/test_datafix_parity.py @@ -33,6 +33,7 @@ from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) import anvil_dump # noqa: E402 +import parity_shard # noqa: E402 ROOT = Path(__file__).resolve().parent.parent LOCAL = ROOT / '.local' / 'datafix' @@ -63,7 +64,7 @@ def java_classpath(): import test_model_baking_parity as baking lock, jars, _ = base.verify_artifacts() libraries = baking.client_libraries() - build = LOCAL / 'build' / os.environ.get('PARITY_SHARD', 'all').replace('/', '-') # shards compile apart + build = LOCAL / 'build' / parity_shard.label() # shards compile apart build.mkdir(parents=True, exist_ok=True) classes, dependencies, _ = harness.compile_oracle(build, 'VanillaDataFixOracle', jars, libraries, lock['minecraft']['java_major']) @@ -78,15 +79,6 @@ def build_rust(): return Path(meta['target_directory']) / 'release' / 'examples' / 'datafix_world' -def shard(items): - """This run's share of items when tools/parity.py runs the harness in parts (PARITY_SHARD=i/n).""" - spec = os.environ.get('PARITY_SHARD') - if not spec: - return items - index, count = map(int, spec.split('/')) - return items[index - 1::count] - - def run_vanilla(classpath, world, out, target=None): # The oracle replaces OUT itself: left in place, an unchanged run's outputs can be reused. log = out.parent / (out.name + '.log') @@ -226,7 +218,7 @@ def main(): band = args.band if band and not args.versions: # worlds in the layout of before 1.16, saved before the target worlds = [w for w in worlds if json.loads((world_path(w) / 'datafix-source.json').read_text())['data_version'] < band] - versions = shard(args.versions or worlds) + versions = parity_shard.shard(args.versions or worlds) classpath = java_classpath() binary = None if args.vanilla_only else Path(args.binary) if args.binary else build_rust() total = 0 diff --git a/tools/test_world_spawn_parity.py b/tools/test_world_spawn_parity.py index 9b495c3dc..4247a0ee7 100644 --- a/tools/test_world_spawn_parity.py +++ b/tools/test_world_spawn_parity.py @@ -29,6 +29,10 @@ Seeds: the player report's seed (1891623539983467248: Horizon spawned at -48 -48, vanilla at 152 145), the chunk generation harness's seeds and --random seeds from a fixed generator. +tools/parity.py runs it in PARITY_SHARDS parts, each creating the worlds of +every PARITY_SHARDS-th seed: each seed is a vanilla and a Horizon server start, +~15 s, and 60 seeds in one run were the longest harness (~25 min). + usage: test_world_spawn_parity.py [--random N] [--jobs N] [--origins-only] """ import argparse @@ -43,16 +47,18 @@ from concurrent.futures import ThreadPoolExecutor from pathlib import Path try: - from . import anvil_dump, lab, oracle_harness as oh, test_rust_parity as base + from . import anvil_dump, lab, oracle_harness as oh, parity_shard, test_rust_parity as base from .test_dedicated_server_parity import Server, free_port except ImportError: import anvil_dump import lab import oracle_harness as oh + import parity_shard import test_rust_parity as base from test_dedicated_server_parity import Server, free_port NAME = 'world-spawn-parity' +PARITY_SHARDS = 4 REPORT = 1891623539983467248 SEEDS = [REPORT, 12345, -7046029254386353131, 987654321987, 4242, -1, 0x5DEECE66D, 20260607, 777, 0] RESOURCES = lab.RESOURCES @@ -107,7 +113,7 @@ def main(): parser.add_argument('--seed', type=int, action='append', help='only these seeds (repeatable)') args = parser.parse_args() lock, jars, libraries = base.verify_artifacts() - build = lab.LOCAL / NAME + build = lab.LOCAL / NAME / parity_shard.label() # shards run at once build.mkdir(parents=True, exist_ok=True) dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) classes = build / 'classes' @@ -116,7 +122,7 @@ def main(): '-d', classes, lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaWorldSpawnOracle.java'], build, 'compile-java') oh.run_logged(['cargo', 'build', '--release', '-p', 'horizon-world', '--example', 'spawn_origin'], build, 'compile-probe') - all_seeds = args.seed or seeds(args.random) + all_seeds = parity_shard.shard(args.seed or seeds(args.random)) started = time.time() vanilla, rust = origins(build, classes, dependencies, all_seeds) From 7cc47e2b55817b1cba0daaa88b1aa3af495c8e56 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:41:40 +0200 Subject: [PATCH 29/39] Delete ten tools/ scripts nothing references command_parse_summary, structure_start_oracle, fetch_client_assets, compare_structure_starts, profile_behaviour_cache, command_tree_diff, compare_loot_fill, compare_structure_blocks, test_lan_lifecycle and PROFILE_SHARED_JSON.md: no workflow, doc, harness, test or other tool names them (review-plan.md, Dead code). History keeps them. Co-Authored-By: Claude Opus 5.5 --- tools/PROFILE_SHARED_JSON.md | 85 ------------ tools/command_parse_summary.py | 36 ----- tools/command_tree_diff.py | 79 ----------- tools/compare_loot_fill.py | 91 ------------ tools/compare_structure_blocks.py | 113 --------------- tools/compare_structure_starts.py | 55 -------- tools/fetch_client_assets.py | 46 ------- tools/profile_behaviour_cache.py | 57 -------- tools/structure_start_oracle.py | 38 ----- tools/test_lan_lifecycle.py | 222 ------------------------------ 10 files changed, 822 deletions(-) delete mode 100644 tools/PROFILE_SHARED_JSON.md delete mode 100644 tools/command_parse_summary.py delete mode 100644 tools/command_tree_diff.py delete mode 100644 tools/compare_loot_fill.py delete mode 100644 tools/compare_structure_blocks.py delete mode 100644 tools/compare_structure_starts.py delete mode 100644 tools/fetch_client_assets.py delete mode 100644 tools/profile_behaviour_cache.py delete mode 100644 tools/structure_start_oracle.py delete mode 100644 tools/test_lan_lifecycle.py diff --git a/tools/PROFILE_SHARED_JSON.md b/tools/PROFILE_SHARED_JSON.md deleted file mode 100644 index 976e43ecb..000000000 --- a/tools/PROFILE_SHARED_JSON.md +++ /dev/null @@ -1,85 +0,0 @@ -# Reproducing PR #607 block-facts startup profiles - -These are loader-only measurements. They include `Assets::load`, `BlockFacts`, and -`Behaviours` and exclude content root preparation, graphics, input setup, first -frame, and the rest of the client startup sequence. No full-client startup gain -has been measured. The prepared `26.3` resource root must match the revision. - -## Historical release builds - -From the PR branch checkout, use an absolute prepared resource-root path and a -single, explicit Cargo release profile for each detached historical worktree: - -```sh -PR_ROOT="$(pwd)" -BENCH_ROOT="$(mktemp -d)" -mkdir -p "$BENCH_ROOT/bin" -CARGO_TARGET_DIR="$BENCH_ROOT/target" -export CARGO_TARGET_DIR - -for entry in baseline:e422bd13 a01:a01d47f2 cf:cf837118 cd:cd61e11b; do - name="${entry%%:*}" - revision="${entry#*:}" - git worktree add --detach "$BENCH_ROOT/$name" "$revision" -done - -# e422bd13 predates the profiler; add the same baseline example source used -# for the benchmark, without changing any production implementation. -cp "$PR_ROOT/tools/baseline_loader_profile.rs" \ - "$BENCH_ROOT/baseline/crates/horizon-client/examples/shared_json_startup_profile.rs" - -for name in baseline a01 cf cd; do - (cd "$BENCH_ROOT/$name" && \ - cargo build --offline --release -p horizon-client --example shared_json_startup_profile) - cp "$CARGO_TARGET_DIR/release/examples/shared_json_startup_profile" \ - "$BENCH_ROOT/bin/$name" -done - -cargo build --offline --release -p horizon-client --example shared_json_startup_profile -cp "$CARGO_TARGET_DIR/release/examples/shared_json_startup_profile" "$BENCH_ROOT/bin/current" - -python3 tools/profile_shared_json.py \ - --resources /absolute/path/to/.local/horizon-client/26.3 \ - --baseline-binary "$BENCH_ROOT/bin/baseline" \ - --a01-binary "$BENCH_ROOT/bin/a01" \ - --cf-binary "$BENCH_ROOT/bin/cf" \ - --cd-binary "$BENCH_ROOT/bin/cd" \ - --binary "$BENCH_ROOT/bin/current" \ - --repeats 10 --output "$BENCH_ROOT/five-way.json" -``` - -The source tree of each historical revision is exact. The baseline profiler is -an added standalone measurement example, and therefore does not alter its -historical loader logic. Comparisons use alternating forward/reverse process -order, separate processes, Linux `wait4` CPU time and peak RSS, and saved raw -samples with 25th/75th percentiles, extrema, build hashes and system load. -Keep concurrent compilation and other CPU-intensive jobs idle if possible. -`ru_maxrss` records process peak residency and cannot establish the real game's -memory usage after rendering begins. If system load rises, preserve rather than -discard outliers and report the uncertainty. - -## Decoding passes and buffer lifetime - -```sh -cargo build --offline --release -p horizon-client --example gameplay_decode_profile -python3 tools/profile_gameplay_decode.py \ - --resources /absolute/path/to/.local/horizon-client/26.3 \ - --repeats 10 --output "$BENCH_ROOT/decode.json" - -python3 tools/profile_shared_json.py \ - --resources /absolute/path/to/.local/horizon-client/26.3 \ - --variant released="$BENCH_ROOT/bin/current"@deferred \ - --variant retained="$BENCH_ROOT/bin/current"@deferred-held \ - --repeats 10 --output "$BENCH_ROOT/lifetime.json" -``` - -The decoder profile measures the `Validate` pass and typed `Gameplay` pass -separately, including the JSON read in `total_ms`. The primary facts loader -continues both passes to preserve JSON validation, error precedence and useful -diagnostics, including overwritten fields and raw geometry. The lifetime -comparison measures current process `VmRSS` after materialisation, plus peak -RSS across the process. An explicit `drop` releases owned Rust values, but the -allocator may retain physical pages, so immediate RSS savings are not implied. - -When finished, `git worktree remove` each temporary historical worktree. Do -not `git clean` or reset the active PR working tree. diff --git a/tools/command_parse_summary.py b/tools/command_parse_summary.py deleted file mode 100644 index 2cd8d1f68..000000000 --- a/tools/command_parse_summary.py +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env python3 -"""Group the mismatches of tools/test_command_parse_parity.py by cause. - -usage: command_parse_summary.py MISMATCHES.jsonl [ROOT] -""" -import collections -import json -import sys -from pathlib import Path - - -def describe(side, kind): - if kind == 'parse': - if side.get('ok'): - return 'OK' - return f"ERR {side.get('error')!r}@{'c' if side.get('cursor') else '0'}" - return f"SUGG[{len(side['list'])}]" - - -path = Path(sys.argv[1]) -only = sys.argv[2] if len(sys.argv) > 2 else None -groups = collections.Counter() -examples = {} -for line in path.open(encoding='utf-8'): - m = json.loads(line) - root = m['in'].split(' ')[0] - if only and root != only: - continue - key = (m['kind'], describe(m['vanilla'], m['kind']), describe(m['rust'], m['kind'])) - if m['kind'] == 'parse': - key = key[:1] + (key[1][:90], key[2][:90]) - groups[key] += 1 - examples.setdefault(key, m) -for key, count in sorted(groups.items(), key=lambda kv: -kv[1])[:int(sys.argv[3]) if len(sys.argv) > 3 else 40]: - ex = examples[key] - print(f'{count:6} {key[0]} vanilla={key[1]} rust={key[2]}\n e.g. {ex["in"]!r}') diff --git a/tools/command_tree_diff.py b/tools/command_tree_diff.py deleted file mode 100644 index 8bd049b7a..000000000 --- a/tools/command_tree_diff.py +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/env python3 -"""Structural diff between vanilla's exported command tree and Horizon's. - -usage: command_tree_diff.py VANILLA_TREE.json RUST_TREE.json [ROOT] - -Reports, for every shared node, the children Horizon lacks or adds, differing -argument parsers and executable/redirect flags. Missing subtrees are reported -once at their root. -""" -import json -import sys - -LOOSE = { - # Horizon's coarse argument kinds that stand in for several vanilla parsers. - 'brigadier:string': {'brigadier:string', 'minecraft:message', 'minecraft:component'}, -} - - -def same_parser(vanilla, rust): - if vanilla == rust: - return True - return False - - -def keyed(children): - """Literals by name; arguments by position (their names are internal).""" - out = {} - index = 0 - for name, child in children.items(): - if child['type'] == 'literal': - out[('l', name)] = (name, child) - else: - out[('a', index)] = (name, child) - index += 1 - return out - - -def diff(path, v, r, out): - vc, rc = keyed(v.get('children', {})), keyed(r.get('children', {})) - for key, (name, node) in vc.items(): - if key not in rc: - out.append(('missing', path + [name], node.get('parser') or 'literal', count(node))) - for key, (name, node) in rc.items(): - if key not in vc: - out.append(('extra', path + [name], node.get('parser') or 'literal', count(node))) - for key, (name, a) in vc.items(): - if key not in rc: - continue - _, b = rc[key] - if a['type'] == 'argument' and not same_parser(a['parser'], b['parser']): - out.append(('parser', path + [name], f"{a['parser']} vs {b['parser']}", 0)) - if bool(a.get('executable')) != bool(b.get('executable')): - out.append(('executable', path + [name], f"vanilla={bool(a.get('executable'))}", 0)) - if bool(a.get('redirect')) != bool(b.get('redirect')): - out.append(('redirect', path + [name], f"vanilla={bool(a.get('redirect'))}", 0)) - diff(path + [name], a, b, out) - - -def count(node): - return 1 + sum(count(c) for c in node.get('children', {}).values()) - - -def main(): - vanilla = json.load(open(sys.argv[1])) - rust = json.load(open(sys.argv[2])) - only = sys.argv[3] if len(sys.argv) > 3 else None - if only: - vanilla = {'children': {only: vanilla['children'][only]}, 'type': 'root'} - rust = {'children': {only: rust['children'].get(only, {'children': {}, 'type': 'literal'})}, 'type': 'root'} - out = [] - diff([], vanilla, rust, out) - for kind, path, detail, size in out: - print(f'{kind:10} {" ".join(path)} [{detail}]' + (f' ({size} nodes)' if size else '')) - missing = sum(size for kind, _, _, size in out if kind == 'missing') - print(f'-- {len(out)} differences, {missing} vanilla nodes missing in Horizon') - - -if __name__ == '__main__': - main() diff --git a/tools/compare_loot_fill.py b/tools/compare_loot_fill.py deleted file mode 100644 index 7ad307420..000000000 --- a/tools/compare_loot_fill.py +++ /dev/null @@ -1,91 +0,0 @@ -#!/usr/bin/env python3 -"""Compare Rust container loot (examples/loot_fill) with vanilla LootTable.fill. - -usage: compare_loot_fill.py RESOURCES TABLES SEED... -TABLES is a comma-separated list or `chests` for every chest table. Builds the -loot_fill example, runs VanillaLootFillOracle and diffs slot contents -(enchantments compared as sets). Tables vanilla cannot fill without a level -(exploration maps) are reported and skipped. -""" -import collections -import os -import subprocess -import sys -from pathlib import Path -try: - from . import lab, test_rust_parity as base -except ImportError: - import lab - import test_rust_parity as base - - -def build_dir(): - return Path(os.environ['HORIZON_ORACLE_BUILD']) if os.environ.get('HORIZON_ORACLE_BUILD') else lab.LOCAL/'loot-fill-oracle' - - -def run_oracle(out, tables, seeds): - lock, jars, libraries = base.verify_artifacts() - source = lab.ROOT/'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaLootFillOracle.java' - build = build_dir() - classes = build/'classes' - classes.mkdir(parents=True, exist_ok=True) - dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) - compiled = subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-cp', dependencies, - '-d', str(classes), str(source)], capture_output=True, text=True) - if compiled.returncode: - raise SystemExit('javac failed:\n' + compiled.stderr[-3000:]) - result = subprocess.run(['java', '-Xmx2G', '-cp', str(classes)+os.pathsep+dependencies, - 'dev.horizonlab.parity.VanillaLootFillOracle', str(out), tables, *seeds], - capture_output=True, text=True) - if result.returncode: - lines = [line for line in (result.stdout + result.stderr).splitlines() if 'Exception' in line or 'Caused' in line] - raise SystemExit('oracle failed: ' + ' | '.join(lines[:5])) - - -def parse(lines): - fills, errors = collections.defaultdict(list), set() - for line in lines: - t = line.split() - if not t: - continue - if t[0] == 'table': - fills[(t[1], t[2])] - elif t[0] == 'error': - errors.add((t[1], t[2])) - elif t[0] == 'fill': - fills[(t[1], t[2])].append((int(t[3]), t[4], t[5], t[6], tuple(sorted(t[7:])))) - return fills, errors - - -def main(): - resources, tables, seeds = sys.argv[1], sys.argv[2], sys.argv[3:] - out = build_dir()/'vanilla.txt' - out.parent.mkdir(parents=True, exist_ok=True) - run_oracle(out, tables, seeds) - vanilla, errors = parse(out.read_text().splitlines()) - names = sorted({k[0] for k in vanilla} | {k[0] for k in errors}) - subprocess.run(['cargo', 'build', '--profile', 'fast', '-q', '-p', 'horizon-world', '--example', 'loot_fill'], - check=True, cwd=lab.ROOT) - rust_out = subprocess.run([str(lab.cargo_target()/'fast/examples/loot_fill'), resources, ','.join(names), *seeds], - check=True, capture_output=True, text=True).stdout - rust, _ = parse(rust_out.splitlines()) - matched = differ = 0 - for key in sorted(vanilla): - if key in errors: - continue - if vanilla[key] == rust.get(key): - matched += 1 - continue - differ += 1 - if differ <= 20: - print('DIFF', key, '\n vanilla', vanilla[key], '\n rust ', rust.get(key)) - tables = collections.Counter(k[0] for k in vanilla if k not in errors and vanilla[k] != rust.get(k)) - if tables: - print('differing tables:', dict(tables)) - skipped = sorted({k[0] for k in errors}) - print(f'{matched} of {matched + differ} fills match; skipped (needs a level): {skipped}') - return 1 if differ else 0 - - -if __name__ == '__main__': - sys.exit(main()) diff --git a/tools/compare_structure_blocks.py b/tools/compare_structure_blocks.py deleted file mode 100644 index 51d71f57b..000000000 --- a/tools/compare_structure_blocks.py +++ /dev/null @@ -1,113 +0,0 @@ -#!/usr/bin/env python3 -"""Compare structure placement (terrain + structure pieces, no placed features) -between vanilla and Rust for target chunks. - -usage: compare_structure_blocks.py RESOURCES SEED DIMENSION CX,CZ... -Runs VanillaStructurePlacementOracle and the structure_blocks example and -reports, per chunk, the blocks and block entities that differ. -""" -import json -import os -import subprocess -import sys -from pathlib import Path -try: - from . import lab, test_rust_parity as base -except ImportError: - import lab - import test_rust_parity as base - - -def build_dir(): - return Path(os.environ['HORIZON_ORACLE_BUILD']) if os.environ.get('HORIZON_ORACLE_BUILD') else lab.LOCAL/'structure-placement-oracle' - - -def run_oracle(out, args): - lock, jars, libraries = base.verify_artifacts() - source = lab.ROOT/'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaStructurePlacementOracle.java' - classes = build_dir()/'classes' - classes.mkdir(parents=True, exist_ok=True) - dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) - compiled = subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-cp', dependencies, - '-d', str(classes), str(source)], capture_output=True, text=True) - if compiled.returncode: - raise SystemExit('javac failed:\n' + compiled.stderr[-3000:]) - result = subprocess.run(['java', '-Xmx4G', '-cp', str(classes)+os.pathsep+dependencies, - 'dev.horizonlab.parity.VanillaStructurePlacementOracle', str(out), *args], - capture_output=True, text=True) - if result.returncode: - lines = [line for line in (result.stdout + result.stderr).splitlines() if 'Exception' in line or 'Error' in line or '\tat ' in line] - raise SystemExit('oracle failed: ' + ' | '.join(lines[:8])) - - -def parse(lines): - chunks, current = {}, None - for line in lines: - t = line.split() - if not t: - continue - if t[0] == 'chunk': - current = (int(t[1]), int(t[2])) - chunks[current] = {'rows': {}, 'be': []} - elif t[0] == 'y' and current: - chunks[current]['rows'][int(t[1])] = t[2:] - elif t[0] == 'be' and current: - # Position, loot table and seed; the block entity type follows - # from the (separately compared) block state. - chunks[current]['be'].append(' '.join(t[1:4] + t[5:])) - return chunks - - -def state_names(resources): - names = {} - report = lab.LOCAL/'datagen/out/reports/blocks.json' - if report.exists(): - for block, info in json.loads(report.read_text()).items(): - for state in info['states']: - props = ','.join(f'{k}={v}' for k, v in sorted(state.get('properties', {}).items())) - names[str(state['id'])] = block + (f'[{props}]' if props else '') - return names - - -def main(): - resources, seed, dimension, targets = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4:] - out = build_dir()/'vanilla.txt' - out.parent.mkdir(parents=True, exist_ok=True) - run_oracle(out, [seed, dimension, *targets]) - vanilla = parse(out.read_text().splitlines()) - subprocess.run(['cargo', 'build', '--profile', 'fast', '-q', '-p', 'horizon-world', '--example', 'structure_blocks'], - check=True, cwd=lab.ROOT) - rust_out = subprocess.run([str(lab.cargo_target()/'fast/examples/structure_blocks'), resources, seed, dimension, *targets], - check=True, capture_output=True, text=True).stdout - (build_dir()/'rust.txt').write_text(rust_out) - rust = parse(rust_out.splitlines()) - names = state_names(resources) - failed = 0 - for key in sorted(vanilla): - v, r = vanilla[key], rust.get(key) - if r is None: - print(key, 'missing from Rust') - failed += 1 - continue - diffs = [] - for y, row in v['rows'].items(): - other = r['rows'].get(y, []) - for i, (a, b) in enumerate(zip(row, other)): - if a != b: - diffs.append((key[0] * 16 + i % 16, y, key[1] * 16 + i // 16, a, b)) - total = sum(len(row) for row in v['rows'].values()) - be_v, be_r = set(v['be']), set(r['be']) - status = 'match' if not diffs and be_v == be_r else 'DIFF' - print(f'chunk {key}: {status}; {total - len(diffs)} of {total} blocks equal; block entities {len(be_v & be_r)} of {len(be_v)} equal') - for x, y, z, a, b in diffs[:12]: - print(f' {x} {y} {z} vanilla {names.get(a, a)} rust {names.get(b, b)}') - for e in sorted(be_v - be_r)[:6]: - print(' only vanilla be', e) - for e in sorted(be_r - be_v)[:6]: - print(' only rust be', e) - failed += status != 'match' - return 1 if failed else 0 - - -if __name__ == '__main__': - sys.exit(main()) diff --git a/tools/compare_structure_starts.py b/tools/compare_structure_starts.py deleted file mode 100644 index 363e7b76f..000000000 --- a/tools/compare_structure_starts.py +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env python3 -"""Compare Rust structure starts (chunkgen_bench --starts) with the vanilla oracle. - -usage: compare_structure_starts.py VANILLA RUST [--all] -Pieces are compared by type, bounding box and (for jigsaw pieces) position, -rotation, ground level delta and junction count. Without --all, vanilla -starts of structure types Rust does not generate yet are only counted. -""" -import collections -import sys - - -def load(path): - starts = {} - current = None - for line in open(path): - t = line.split() - if not t: - continue - if t[0] == 'start': - current = (int(t[1]), int(t[2]), t[3]) - starts[current] = [' '.join(t[4:])] - elif t[0] == 'piece' and current: - starts[current].append(' '.join(t[1:14])) - return starts - - -def main(): - vanilla, rust = load(sys.argv[1]), load(sys.argv[2]) - generated = {k[2] for k in rust} - matched = 0 - missing = collections.Counter() - for key in sorted(vanilla): - if key not in rust: - missing[key[2]] += 1 - if key[2] in generated or '--all' in sys.argv: - print('missing', key) - continue - if vanilla[key] == rust[key]: - matched += 1 - continue - v, r = vanilla[key], rust[key] - print('DIFF', key, 'pieces', len(v) - 1, len(r) - 1) - for i, (a, b) in enumerate(zip(v, r)): - if a != b: - print(' first difference at line', i, '\n vanilla', a, '\n rust ', b) - break - for key in sorted(set(rust) - set(vanilla)): - print('extra', key) - print(f'{matched} of {len(vanilla)} vanilla starts match exactly; not generated by Rust:', dict(missing)) - return 0 if matched == len(rust) == sum(1 for k in vanilla if k[2] in generated) else 1 - - -if __name__ == '__main__': - sys.exit(main()) diff --git a/tools/fetch_client_assets.py b/tools/fetch_client_assets.py deleted file mode 100644 index 2027593c5..000000000 --- a/tools/fetch_client_assets.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python3 -"""Copy the pinned version's indexed client assets that are not in the game -jar from your own official launcher install: language files and the language -list (pack.mcmeta), the unifont glyph archives and their font definitions, and -the built-in Programmer Art and High Contrast resource packs. Every object's -SHA-1 is verified. Nothing is downloaded; files go to the private client -resource directory (or --target) and are never committed.""" -import argparse -from pathlib import Path -import shutil - -import lab - - -def wanted(name): - return (name == 'pack.mcmeta' - or name.startswith('minecraft/lang/') - or name.startswith('minecraft/font/') - or name.startswith('minecraft/resourcepacks/')) - - -def main(): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument('--target', type=Path, default=lab.RESOURCES, - help='prepared resource directory (default: .local/horizon-client/)') - parser.add_argument('--minecraft-dir', type=Path, - help="official launcher game directory (default: the launcher's own)") - args = parser.parse_args() - lock, directory = lab.config() - manifest = lab.import_manifest(lock, directory, args.minecraft_dir) - store, index = lab.launcher_assets(manifest, args.minecraft_dir) - target = args.target - target.mkdir(parents=True, exist_ok=True) - shutil.copyfile(index, target / 'asset-index.json') - objects = lab.json.loads(index.read_text())['objects'] - selected = {name: info for name, info in objects.items() if wanted(name)} - total = 0 - for name, info in selected.items(): - source = lab.launcher_object(store, info['hash']) - lab.import_copy(source, target / 'assets' / name, info['hash']) - total += info['size'] - print(f'Client assets ready: {len(selected)} files, {total / 1e6:.1f} MB') - - -if __name__ == '__main__': - main() diff --git a/tools/profile_behaviour_cache.py b/tools/profile_behaviour_cache.py deleted file mode 100644 index 00ba65a6d..000000000 --- a/tools/profile_behaviour_cache.py +++ /dev/null @@ -1,57 +0,0 @@ -#!/usr/bin/env python3 -"""Measure the resource-dependent Behaviours::load portion of client startup. - -Build the behaviour_load_profile example in the checkout to be measured first: - cargo build --release -p horizon-world --example behaviour_load_profile - -Run the profiler from the repository root with the matching example binary. -This measures behaviour loading, not time to first rendered frame. With ---cache-dir, the example reads and refreshes the resolved snapshot there, as the -client does with its .horizon-cache folder: the first load of a run builds it -when it is missing, the next loads restore it. -""" -import argparse -import json -import re -import statistics -import subprocess -from pathlib import Path - - -def main(): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("--binary", type=Path, default=Path("target/release/examples/behaviour_load_profile")) - parser.add_argument("--resources", type=Path, default=Path(".local/horizon-client/26.3")) - parser.add_argument("--cache-dir", type=Path, - help="snapshot folder (default: no snapshot, every load is uncached)") - parser.add_argument("--repeats", type=int, default=5) - parser.add_argument("--output", type=Path) - args = parser.parse_args() - if args.repeats < 1: - parser.error("--repeats must be at least one") - samples = [] - for _ in range(args.repeats): - command = [str(args.binary.resolve()), str(args.resources.resolve())] - if args.cache_dir: - command.append(str(args.cache_dir.resolve())) - run = subprocess.run(command, text=True, capture_output=True, check=True) - values = {key: float(value) for key, value in - re.findall(r"^(registry_ms|behaviours_[0-2]_ms)=([\d.]+)$", run.stderr, re.M)} - if len(values) != 4: - raise RuntimeError(f"incomplete profile output: {run.stderr}") - samples.append(values) - summary = {key: {"median_ms": round(statistics.median(row[key] for row in samples), 3), - "samples_ms": [row[key] for row in samples]} - for key in samples[0]} - result = {"scope": "behaviour loader only; not full client startup", - "binary": str(args.binary), "resources": str(args.resources), "repeats": args.repeats, - "cache_dir": str(args.cache_dir) if args.cache_dir else None, - "metrics": summary} - print(json.dumps(result, indent=2)) - if args.output: - args.output.parent.mkdir(parents=True, exist_ok=True) - args.output.write_text(json.dumps(result, indent=2) + "\n") - - -if __name__ == "__main__": - main() diff --git a/tools/structure_start_oracle.py b/tools/structure_start_oracle.py deleted file mode 100644 index dc622ae19..000000000 --- a/tools/structure_start_oracle.py +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env python3 -"""Run the vanilla structure-start oracle for a seed and chunk rectangle. - -usage: structure_start_oracle.py OUT SEED X0 Z0 X1 Z1 -""" -import os -import subprocess -from pathlib import Path -import sys -try: - from . import lab, test_rust_parity as base -except ImportError: - import lab - import test_rust_parity as base - - -def main(): - out, seed, x0, z0, x1, z1 = sys.argv[1:7] - lock, jars, libraries = base.verify_artifacts() - source = lab.ROOT/'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaStructureStartOracle.java' - build = Path(os.environ['HORIZON_ORACLE_BUILD']) if os.environ.get('HORIZON_ORACLE_BUILD') else lab.LOCAL/'structure-start-oracle' - classes = build/'classes' - classes.mkdir(parents=True, exist_ok=True) - dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) - compiled = subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-cp', dependencies, - '-d', str(classes), str(source)], capture_output=True, text=True) - if compiled.returncode: - raise SystemExit('javac failed:\n' + compiled.stderr[-3000:]) - result = subprocess.run(['java', '-Xmx2G', '-cp', str(classes)+os.pathsep+dependencies, - 'dev.horizonlab.parity.VanillaStructureStartOracle', out, seed, x0, z0, x1, z1], - capture_output=True, text=True) - if result.returncode: - lines = [line for line in (result.stdout + result.stderr).splitlines() if 'Exception' in line or 'Caused' in line] - raise SystemExit('oracle failed: ' + ' | '.join(lines[:5])) - - -if __name__ == '__main__': - main() diff --git a/tools/test_lan_lifecycle.py b/tools/test_lan_lifecycle.py deleted file mode 100644 index 389b8438c..000000000 --- a/tools/test_lan_lifecycle.py +++ /dev/null @@ -1,222 +0,0 @@ -#!/usr/bin/env python3 -"""Check LAN shutdown when leaving a singleplayer world through the pause menu, -and that offline guests joining with their default names are not refused as the -host's profile (#195: everyone was "Player"). - -Requires a built horizon-client, prepared resources and a display on Unix: - xvfb-run -a python3 tools/test_lan_lifecycle.py --client target/fast/horizon-client -Worlds and settings are created in a temporary directory. -""" -import argparse -import json -import os -from pathlib import Path -import socket -import subprocess -import tempfile -import time - -try: - from . import lab -except ImportError: - import lab - -ROOT = Path(__file__).resolve().parents[1] -GROUP = '224.0.2.60' -DISCOVERY_PORT = 4445 - - -def command(endpoint, line): - with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as connection: - connection.settimeout(60) - connection.connect(str(endpoint)) - connection.sendall((line + '\n').encode()) - with connection.makefile('r') as reply: - result = json.loads(reply.readline()) - assert 'error' not in result, (line, result) - return result - - -def wait_for(endpoint, line, predicate, timeout=60): - deadline = time.monotonic() + timeout - while time.monotonic() < deadline: - try: - result = command(endpoint, line) - except (FileNotFoundError, ConnectionRefusedError): - time.sleep(0.1) - continue - if predicate(result): - return result - time.sleep(0.1) - raise AssertionError(f'timed out waiting for {line}') - - -def announcement(scanner, port, timeout): - deadline = time.monotonic() + timeout - marker = f'[AD]{port}[/AD]'.encode() - while time.monotonic() < deadline: - scanner.settimeout(max(0.001, deadline - time.monotonic())) - try: - message, _ = scanner.recvfrom(4096) - except socket.timeout: - return False - if marker in message: - return True - return False - - -def scenario(endpoint, scanner): - wait_for(endpoint, 'screen', lambda value: value['screen'] == 'title') - command(endpoint, 'open LanLifecycle 0 creative') - wait_for(endpoint, 'status', lambda value: value['player'] is not None - and value['pending_chunks'] == 0) - assert command(endpoint, 'lan status') == {'open': False} - port = 0 - for _ in range(2): - port = command(endpoint, f'lan open {port}')['port'] - with socket.create_connection(('127.0.0.1', port), timeout=2): - pass - assert announcement(scanner, port, 5), 'LAN world was not advertised' - command(endpoint, 'key esc tap') - screen = wait_for(endpoint, 'screen', lambda value: any( - button['label'] == 'Save and Quit to Title' for button in value['buttons'])) - assert screen['screen'] == 'paused', screen - command(endpoint, 'click-button Save and Quit to Title') - wait_for(endpoint, 'screen', lambda value: value['screen'] == 'title') - assert command(endpoint, 'lan status') == {'open': False}, 'LAN stayed open after leaving' - try: - connection = socket.create_connection(('127.0.0.1', port), timeout=2) - except ConnectionRefusedError: - pass - else: - connection.close() - raise AssertionError('LAN still accepts connections after leaving') - # Discard datagrams queued before shutdown, then listen for more than - # two announcement intervals (the pinger sends every 1.5 seconds). - scanner.setblocking(False) - try: - while True: - scanner.recvfrom(4096) - except BlockingIOError: - pass - assert not announcement(scanner, port, 3.5), 'LAN kept advertising after leaving' - command(endpoint, 'open LanLifecycle') - wait_for(endpoint, 'status', lambda value: value['player'] is not None - and value['pending_chunks'] == 0) - assert command(endpoint, 'lan status') == {'open': False}, 'LAN reopened with the world' - command(endpoint, 'lan open 0') - command(endpoint, 'quit') - - -def start_client(client, resources, home, endpoint, log, *extra): - return subprocess.Popen([ - str(client), '--resources', str(resources), '--render-distance', '2', - '--test-terrain', '--control-socket', str(endpoint), '--timeout-seconds', '180', *extra, - ], cwd=home, env=dict(os.environ, HORIZON_HOME=str(home), HORIZON_NO_AUDIO='1', - HORIZON_PLAYER_NAME=''), - stdout=log, stderr=subprocess.STDOUT) - - -def guest_names(endpoint, port, guests): - """Two guests with default names join together; a guest named like the host is refused.""" - names = [] - for home, guest in guests: - wait_for(guest, 'screen', lambda value: value['screen'] == 'title') - command(guest, f'connect 127.0.0.1:{port}') - wait_for(guest, 'status', lambda value: (value.get('server') or {}).get('joined')) - kept = (home / 'horizon-player.txt').read_text() - assert kept.startswith('name:Player') and kept.strip() != 'name:Player', kept - names.append(kept.strip().removeprefix('name:')) - assert names[0] != names[1], names - status = wait_for(endpoint, 'lan status', lambda value: len(value['guests']) == 2) - assert sorted(g['name'] for g in status['guests']) == sorted(names), status - # The same home keeps its name: leave and join again as the same profile. - home, guest = guests[0] - command(guest, 'disconnect') - wait_for(endpoint, 'lan status', lambda value: len(value['guests']) == 1) - command(guest, f'connect 127.0.0.1:{port}') - wait_for(guest, 'status', lambda value: (value.get('server') or {}).get('joined')) - assert (home / 'horizon-player.txt').read_text().strip() == f'name:{names[0]}' - # A guest that really is the host's offline profile is refused, the others stay. - command(guest, 'disconnect') - command(guest, f'connect 127.0.0.1:{port} Player') - refused = wait_for(guest, 'screen', lambda value: (value.get('menu') or {}).get('disconnected')) - assert 'another location' in refused['menu']['disconnected'], refused - status = command(endpoint, 'lan status') - assert [g['name'] for g in status['guests']] == [names[1]], status - return names - - -def main(): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument('--client', type=Path, required=True) - parser.add_argument('--resources', type=Path, default=lab.RESOURCES) - args = parser.parse_args() - client, resources = args.client.resolve(), args.resources.resolve() - with tempfile.TemporaryDirectory(prefix='horizon-lan-') as directory: - work = Path(directory) - endpoint = work / 'control.sock' - with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as scanner: - scanner.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - scanner.bind(('', DISCOVERY_PORT)) - scanner.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, - socket.inet_aton(GROUP) + socket.inet_aton('0.0.0.0')) - with (work / 'client.log').open('w+') as log: - process = start_client(client, resources, work, endpoint, log) - try: - scenario(endpoint, scanner) - assert process.wait(timeout=20) == 0, 'client exited unsuccessfully' - except Exception: - log.seek(0) - print('\n'.join(log.read().splitlines()[-20:])) - raise - finally: - stop(process) - names = guests_scenario(client, resources, work) - print('LAN lifecycle passed: leaving stops hosting and discovery; reopening stays private') - print(f'LAN guests passed: default names {names[0]} and {names[1]} joined together and kept; ' - '"Player" refused as the host\'s profile') - - -def stop(process): - if process.poll() is None: - process.terminate() - try: - process.wait(timeout=10) - except subprocess.TimeoutExpired: - process.kill() - process.wait(timeout=10) - - -def guests_scenario(client, resources, work): - host_home = work / 'host' - homes = [work / 'guest-a', work / 'guest-b'] - processes, logs = [], [] - try: - for home in [host_home, *homes]: - home.mkdir() - log = (home / 'client.log').open('w+') - logs.append(log) - processes.append(start_client(client, resources, home, home / 'control.sock', log)) - endpoint = host_home / 'control.sock' - wait_for(endpoint, 'screen', lambda value: value['screen'] == 'title') - command(endpoint, 'open LanGuests 0 creative') - wait_for(endpoint, 'status', lambda value: value['player'] is not None - and value['pending_chunks'] == 0) - port = command(endpoint, 'lan open 0')['port'] - return guest_names(endpoint, port, [(home, home / 'control.sock') for home in homes]) - except Exception: - for log in logs: - log.seek(0) - print(f'--- {log.name}') - print('\n'.join(log.read().splitlines()[-20:])) - raise - finally: - for process in processes: - stop(process) - for log in logs: - log.close() - - -if __name__ == '__main__': - main() From eeb4cdb680bb16739a1f4c85e3bea24ad2097052 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 15:06:50 +0200 Subject: [PATCH 30/39] Mod SDK: export_mod! exports only in WebAssembly, so the guests link on macOS A host build of a mod (cargo build or test on the workspace) made a cdylib whose component exports macOS's ld couldn't take: its exported-symbols list cuts `horizon:mods/events@0.1.0#init` at the `#`, so coords-hud and horizon-mods-test-guest failed to link. The exports now exist only for wasm32; on the host the macro still checks the mod and keeps it used. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-mod-sdk/src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/horizon-mod-sdk/src/lib.rs b/crates/horizon-mod-sdk/src/lib.rs index ccfdb7947..71c36cf80 100644 --- a/crates/horizon-mod-sdk/src/lib.rs +++ b/crates/horizon-mod-sdk/src/lib.rs @@ -135,7 +135,13 @@ macro_rules! export_mod { with(|m| $crate::Mod::chat(m, &text)) } } + // Only WebAssembly has the component's exports: a host build (cargo test or + // clippy on the workspace) checks the mod without them, which macOS + // couldn't link (its exported-symbols list cuts names at the `#`). + #[cfg(target_arch = "wasm32")] $crate::__export_horizon_mod!(Export); + #[cfg(not(target_arch = "wasm32"))] + let _ = ::init; }; }; } From 5ca16b97fcd4be7924064bd22cbc5fe4e86a6e4c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 15:07:38 +0200 Subject: [PATCH 31/39] Keep version.rs as tools/gen_version_consts.py writes it The file is generated from the pinned version lock, and test_gen_version_consts checks JAVA_VERSION against it; deleting constants by hand breaks the next regeneration and that test. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-core/src/version.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/horizon-core/src/version.rs b/crates/horizon-core/src/version.rs index cb3e85629..29994823c 100644 --- a/crates/horizon-core/src/version.rs +++ b/crates/horizon-core/src/version.rs @@ -11,6 +11,10 @@ pub const PROTOCOL_VERSION: i32 = 777; pub const DATA_VERSION: i32 = 5023; /// `WorldVersion.packVersion(PackType.CLIENT_RESOURCES)`: (major, minor). pub const RESOURCE_PACK_FORMAT: (i64, i64) = (97, 1); +/// `WorldVersion.packVersion(PackType.SERVER_DATA)`: (major, minor). +pub const DATA_PACK_FORMAT: (i64, i64) = (121, 0); +/// The Java major version the game runs on. +pub const JAVA_VERSION: u32 = 25; /// `WorldVersion.stable()`: false for snapshots and pre-releases. pub const STABLE: bool = true; /// `WorldVersion.buildTime()` as `Date.toString()` prints it in UTC (`/version`). From 3960c09159e17ae88ecd3bfc437a6947b5a00ad9 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:46:56 +0200 Subject: [PATCH 32/39] tools: one copy of the helpers harnesses share, in lab.py free_port (7 copies), offline_uuid (4), varint and parse_varint (3 and 2), dbits (6), fbits (4) and settled (2) move into lab.py; every caller uses lab's. The other server harnesses keep importing free_port from test_dedicated_server_parity, which now takes lab's. Imports the moved code left unused go too. Co-Authored-By: Claude Opus 5.5 --- tools/datafix_scene_12.py | 8 +- tools/datafix_worlds.py | 67 ++++------------ tools/lab.py | 65 +++++++++++++++- tools/server_bench.py | 9 +-- tools/test_ambient_sound_parity.py | 14 ++-- tools/test_auto_jump_parity.py | 34 +++----- tools/test_camera_parity.py | 78 +++++++++---------- tools/test_dedicated_server_parity.py | 8 +- tools/test_first_person_parity.py | 16 +--- tools/test_friends.py | 8 +- tools/test_game_loop_parity.py | 32 +++----- tools/test_save_roundtrip_parity.py | 8 +- tools/test_secure_chat_parity.py | 20 ++--- tools/test_server_dimension_players_parity.py | 13 +--- tools/test_server_player_targets_parity.py | 25 +----- tools/test_sound_engine_parity.py | 12 +-- tools/upgrade_world.py | 9 +-- tools/vanilla_client.py | 11 +-- tools/vanilla_login.py | 55 +++---------- tools/vanilla_world_check.py | 14 ++-- 20 files changed, 189 insertions(+), 317 deletions(-) diff --git a/tools/datafix_scene_12.py b/tools/datafix_scene_12.py index 3d4a6366f..a73b16c28 100644 --- a/tools/datafix_scene_12.py +++ b/tools/datafix_scene_12.py @@ -23,6 +23,8 @@ import threading import time from pathlib import Path +import lab + BX, BZ, SIZE = 144, 144, 96 # south-west corner and size of the platforms (inside the spawn chunks) LAYERS = list(range(100, 245, 16)) # platform y levels: one chunk section each, the last one reaches y = 255 UUID_HI = 0x6000 @@ -1743,7 +1745,7 @@ LAST = {} def populate(server, Bot, port, protocol, name, problems): """Build the whole 1.12.2 scene, save it for good and return the (still joined) bot.""" harness = sys.modules[Bot.__module__] - Player = make_player(Bot, harness.varint, harness.parse_varint, harness.mcstring) + Player = make_player(Bot, lab.varint, lab.parse_varint, harness.mcstring) set_protocol(protocol) say = lambda cmds, **kw: batch(server, cmds, problems, **kw) if ERA < 12: @@ -1874,7 +1876,7 @@ def build_flat(h, force=False): return [] work = h.LOCAL / 'gen' / (version + '-flat') shutil.rmtree(work, ignore_errors=True) - port = h.free_port() + port = lab.free_port() class FlatServer(h.Server): def __init__(self, directory): # as Server.__init__, with a flat world @@ -1900,7 +1902,7 @@ def build_flat(h, force=False): # A flat world's spawn is found far away; make it (64, 64), and let a bot visit the scene's area so that its chunks # are loaded (they stay loaded, being within the spawn radius). batch(server, ['setworldspawn 64 4 64'], problems) - Player = make_player(h.Bot, h.varint, h.parse_varint, h.mcstring) + Player = make_player(h.Bot, lab.varint, lab.parse_varint, h.mcstring) visitor = Player(port, protocol, h.BOT) time.sleep(2) for x, z in ((24, 24), (24, 72), (24, 104), (72, 24), (72, 72), (72, 104), (104, 24), (104, 72), (104, 104), (72, 123), (-8, 63), (32, 103)): diff --git a/tools/datafix_worlds.py b/tools/datafix_worlds.py index de32e4c09..8411afbc2 100644 --- a/tools/datafix_worlds.py +++ b/tools/datafix_worlds.py @@ -23,7 +23,6 @@ import sys import threading import time import urllib.request -import uuid import zipfile import zlib from pathlib import Path @@ -31,6 +30,7 @@ from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) import datafix_scene as scene # noqa: E402 import datafix_scene_12 as scene12 # noqa: E402 +import lab # noqa: E402 ROOT = Path(__file__).resolve().parent.parent LOCAL = ROOT / '.local' / 'datafix' @@ -159,12 +159,6 @@ def install_runtime(pin, home): target.symlink_to(to) -def free_port(): - with socket.socket() as s: - s.bind(('127.0.0.1', 0)) - return s.getsockname()[1] - - class Server: def __init__(self, directory, jar, port, packs=(), java='java', props_override=None): directory.mkdir(parents=True, exist_ok=True) @@ -230,38 +224,9 @@ def failed(lines): # ------------------------------------------------------------------ minimal bot -def varint(v): - out = b'' - v &= 0xFFFFFFFF - while True: - b = v & 0x7F - v >>= 7 - out += bytes([b | (0x80 if v else 0)]) - if not v: - return out - - def mcstring(s): data = s.encode() - return varint(len(data)) + data - - -def parse_varint(data, pos): - value = shift = 0 - while True: - b = data[pos] - pos += 1 - value |= (b & 0x7F) << shift - shift += 7 - if not b & 0x80: - return pos, value - - -def offline_uuid(name): - digest = bytearray(hashlib.md5(('OfflinePlayer:' + name).encode()).digest()) - digest[6] = (digest[6] & 0x0F) | 0x30 - digest[8] = (digest[8] & 0x3F) | 0x80 - return uuid.UUID(bytes=bytes(digest)) + return lab.varint(len(data)) + data class Bot: @@ -273,21 +238,21 @@ class Bot: self.buffer = b'' self.protocol = protocol self.stop = threading.Event() - self.send(0, varint(protocol) + mcstring('127.0.0.1') + struct.pack('>H', port) + varint(2)) + self.send(0, lab.varint(protocol) + mcstring('127.0.0.1') + struct.pack('>H', port) + lab.varint(2)) if protocol <= 758: # 1.18.2: the name only self.send(0, mcstring(name)) elif protocol == 759: # 1.19: no signature data self.send(0, mcstring(name) + b'\x00') elif protocol == 760: # 1.19.2: no signature data, optional UUID - self.send(0, mcstring(name) + b'\x00' + b'\x01' + offline_uuid(name).bytes) + self.send(0, mcstring(name) + b'\x00' + b'\x01' + lab.offline_uuid(name).bytes) elif protocol <= 763: # 1.19.3 - 1.20.1: optional UUID - self.send(0, mcstring(name) + b'\x01' + offline_uuid(name).bytes) + self.send(0, mcstring(name) + b'\x01' + lab.offline_uuid(name).bytes) else: - self.send(0, mcstring(name) + offline_uuid(name).bytes) + self.send(0, mcstring(name) + lab.offline_uuid(name).bytes) while True: pid, data = self.receive() if pid == 3: - self.threshold = parse_varint(data, 0)[1] + self.threshold = lab.parse_varint(data, 0)[1] elif pid == 2: break elif pid == 0: @@ -312,15 +277,15 @@ class Bot: elif pid == disconnect: raise RuntimeError(f'disconnected during configuration: {data!r}') elif not old and self.known_packs(data): - self.send(7, varint(0)) # claim no packs: the server sends full registries + self.send(7, lab.varint(0)) # claim no packs: the server sends full registries @staticmethod def known_packs(data): try: - pos, count = parse_varint(data, 0) + pos, count = lab.parse_varint(data, 0) if count < 1: return False - pos, n = parse_varint(data, pos) + pos, n = lab.parse_varint(data, pos) return data[pos:pos + n] == b'minecraft' except IndexError: return False @@ -359,18 +324,18 @@ class Bot: return value def send(self, packet_id, payload=b''): - body = varint(packet_id) + payload + body = lab.varint(packet_id) + payload if self.threshold >= 0: - body = varint(len(body)) + zlib.compress(body) if len(body) >= self.threshold else varint(0) + body - self.sock.sendall(varint(len(body)) + body) + body = lab.varint(len(body)) + zlib.compress(body) if len(body) >= self.threshold else lab.varint(0) + body + self.sock.sendall(lab.varint(len(body)) + body) def receive(self): length = self.read_varint() body = self.read_exact(length) if self.threshold >= 0: - pos, size = parse_varint(body, 0) + pos, size = lab.parse_varint(body, 0) body = zlib.decompress(body[pos:]) if size else body[pos:] - pos, packet_id = parse_varint(body, 0) + pos, packet_id = lab.parse_varint(body, 0) return packet_id, body[pos:] @@ -428,7 +393,7 @@ def build(name, force=False): era = scene.era(data_version) work = LOCAL / 'gen' / name shutil.rmtree(work, ignore_errors=True) - port = free_port() + port = lab.free_port() server = Server(work, jar, port, scene.experiments(data_version), java(version), props) problems = [] diff --git a/tools/lab.py b/tools/lab.py index 87713276d..a62a38f59 100644 --- a/tools/lab.py +++ b/tools/lab.py @@ -1,7 +1,8 @@ #!/usr/bin/env python3 """Pinned Java Edition import/decompilation. Game files come from the user's own copy (their official launcher install and the server jar they downloaded); all -generated files stay in .local/.""" +generated files stay in .local/. Also the small helpers the harnesses share +(free ports, offline UUIDs, VarInts, float bits).""" import argparse from collections import Counter @@ -12,11 +13,13 @@ import platform from pathlib import Path, PurePosixPath import re import shutil +import socket import struct import subprocess import sys import tempfile import urllib.request +import uuid import zipfile ROOT = Path(__file__).resolve().parents[1] @@ -61,6 +64,66 @@ def digest(path, algorithm): return hashlib.file_digest(stream, algorithm).hexdigest() +def free_port(): + """A free local TCP port, for a server a harness starts.""" + with socket.socket() as s: + s.bind(("127.0.0.1", 0)) + return s.getsockname()[1] + + +def offline_uuid(name): + """UUIDUtil.createOfflinePlayerUUID: the UUID an offline-mode server gives a player name.""" + digest = bytearray(hashlib.md5(("OfflinePlayer:" + name).encode()).digest()) + digest[6] = (digest[6] & 0x0F) | 0x30 + digest[8] = (digest[8] & 0x3F) | 0x80 + return uuid.UUID(bytes=bytes(digest)) + + +def varint(value): + """FriendlyByteBuf.writeVarInt: the int's 32 bits, 7 at a time.""" + out = b"" + value &= 0xFFFFFFFF + while True: + b = value & 0x7F + value >>= 7 + out += bytes([b | (0x80 if value else 0)]) + if not value: + return out + + +def parse_varint(data, pos): + """The VarInt at data[pos]: (the position after it, its unsigned value).""" + value = shift = 0 + while True: + b = data[pos] + pos += 1 + value |= (b & 0x7F) << shift + shift += 7 + if not b & 0x80: + return pos, value + + +def fbits(value): + """A float's IEEE 754 bits in hex, as the Java oracles print them.""" + return struct.pack(">f", value).hex() + + +def dbits(value): + """A double's IEEE 754 bits in hex, as the Java oracles print them.""" + return struct.pack(">d", value).hex() + + +def settled(lines): + """One player's lines of one step, in order, with the waypoint packets reduced to the last for each waypoint: + what the locator bar shows once the step is over. Arriving in a level, vanilla sends the same waypoint more + than once on the way there; the packet by packet order is test_locator_bar_parity's.""" + def waypoint(line): + parts = line.split(" ") + return parts[3] if len(parts) > 3 and parts[2] in ("track", "untrack", "update") else None + last = {waypoint(l): i for i, l in enumerate(lines) if waypoint(l)} + return [l for i, l in enumerate(lines) if not waypoint(l) or last[waypoint(l)] == i] + + def download(url, destination, checksum, algorithm="sha1"): """Never promote an unverified or partial download into the artifact cache.""" destination.parent.mkdir(parents=True, exist_ok=True) diff --git a/tools/server_bench.py b/tools/server_bench.py index ebd2a1a99..a9a494efd 100755 --- a/tools/server_bench.py +++ b/tools/server_bench.py @@ -45,7 +45,6 @@ from pathlib import Path import platform import re import shutil -import socket import statistics import subprocess import sys @@ -81,12 +80,6 @@ PROPERTIES = { } -def free_port(): - with socket.socket() as s: - s.bind(('127.0.0.1', 0)) - return s.getsockname()[1] - - def cpu_seconds(text): """ps's cumulative CPU time: [[DD-]HH:]MM:SS[.ss].""" days, _, rest = text.rpartition('-') @@ -282,7 +275,7 @@ def run_once(kind, bots, behaviour, args, out): directory = out / name shutil.rmtree(directory, ignore_errors=True) directory.mkdir(parents=True) - port = free_port() + port = lab.free_port() props = dict(PROPERTIES, **{'server-port': str(port), 'level-seed': str(args.seed)}) (directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items())) (directory / 'eula.txt').write_text('eula=true\n') diff --git a/tools/test_ambient_sound_parity.py b/tools/test_ambient_sound_parity.py index 8906d2d7a..85cfb20e1 100644 --- a/tools/test_ambient_sound_parity.py +++ b/tools/test_ambient_sound_parity.py @@ -17,16 +17,12 @@ playing instance and the subtitle notifications. Horizon's `Ambience` and """ import json import random -import struct try: - from . import audio_harness as h + from . import audio_harness as h, lab except ImportError: import audio_harness as h - - -def dbits(v): - return struct.pack('>d', v).hex() + import lab def settings(): @@ -54,11 +50,11 @@ class Script: self.lines.append(' '.join(map(str, parts))) def sounds(self, value): - mood = '-' if value['mood'] is None else '{},{},{},{}'.format(value['mood'][0], value['mood'][1], value['mood'][2], dbits(value['mood'][3])) - self.add('sounds', value['loop'] or '-', mood, *[f'{s},{dbits(c)}' for s, c in value['additions']]) + mood = '-' if value['mood'] is None else '{},{},{},{}'.format(value['mood'][0], value['mood'][1], value['mood'][2], lab.dbits(value['mood'][3])) + self.add('sounds', value['loop'] or '-', mood, *[f'{s},{lab.dbits(c)}' for s, c in value['additions']]) def player(self, at=(0.5, 64.0, 0.5), eye=1.62, water=0, spectator=0, removed=0, mode=0, height=1.8, columns=()): - self.add('player', *map(dbits, at), h.bits(eye), water, spectator, removed, mode, h.bits(height), + self.add('player', *map(lab.dbits, at), h.bits(eye), water, spectator, removed, mode, h.bits(height), *[f'{x},{y},{z},{d}' for x, y, z, d in columns]) def tick(self, n=1): diff --git a/tools/test_auto_jump_parity.py b/tools/test_auto_jump_parity.py index da0c16b7b..3e9da7d5a 100644 --- a/tools/test_auto_jump_parity.py +++ b/tools/test_auto_jump_parity.py @@ -8,11 +8,9 @@ overhangs approached from every angle with varied speeds, input vectors, sizes, jump boost and the canAutoJump preconditions.""" import json import math -import os from pathlib import Path import platform import random -import struct import subprocess try: @@ -23,14 +21,6 @@ except ImportError: import test_native_client as client -def fbits(v): - return struct.pack('>f', v).hex() - - -def dbits(v): - return struct.pack('>d', v).hex() - - BLOCKS = [ ('stone', {}), ('stone', {}), ('stone', {}), ('stone_slab', {'type': 'bottom'}), ('stone_slab', {'type': 'top'}), ('stone_slab', {'type': 'double'}), @@ -64,12 +54,12 @@ DIMS = [(0.6, 1.8), (0.6, 1.8), (0.6, 1.5), (0.6, 0.6), (0.3, 0.9), (1.2, 3.6), def player(rng, x, y, z, yaw): w, h = rng.choice(DIMS) - return dict(pos=[dbits(x), dbits(y), dbits(z)], ground=rng.random() < 0.9, shift=rng.random() < 0.08, - yr=fbits(yaw), xr=fbits(rng.choice([0.0, 30.0, -45.0, 90.0, rng.uniform(-90, 90)])), - speed=fbits(rng.choice([0.1, 0.13000001, 0.03, 0.0, 0.2, rng.uniform(0, 0.3)])), - move=[fbits(v) for v in rng.choice([(0.0, 1.0), (0.0, 1.0), (1.0, 0.0), (-1.0, 0.0), (0.70710677, 0.70710677), + return dict(pos=[lab.dbits(x), lab.dbits(y), lab.dbits(z)], ground=rng.random() < 0.9, shift=rng.random() < 0.08, + yr=lab.fbits(yaw), xr=lab.fbits(rng.choice([0.0, 30.0, -45.0, 90.0, rng.uniform(-90, 90)])), + speed=lab.fbits(rng.choice([0.1, 0.13000001, 0.03, 0.0, 0.2, rng.uniform(0, 0.3)])), + move=[lab.fbits(v) for v in rng.choice([(0.0, 1.0), (0.0, 1.0), (1.0, 0.0), (-1.0, 0.0), (0.70710677, 0.70710677), (0.0, -1.0), (0.0, 0.0), (0.3, 0.3)])], - width=fbits(w), height=fbits(h), boost=rng.choice([None] * 6 + [0, 1, 4, 255]), + width=lab.fbits(w), height=lab.fbits(h), boost=rng.choice([None] * 6 + [0, 1, 4, 255]), enabled=rng.random() < 0.93, time=rng.choice([0] * 12 + [1, -3, 2])) @@ -86,18 +76,18 @@ def cases(rng): x = ox + 0.5 - dx * 1.1 z = oz + 0.5 - dz * 1.1 p = player(rng, x, float(oy), z, yaw) - p.update(ground=True, shift=False, enabled=True, time=0, boost=None, move=[fbits(0.0), fbits(1.0)], - width=fbits(0.6), height=fbits(1.8), xr=fbits(0.0)) - yield dict(blocks=blocks, player=p, xa=fbits(dx * step), za=fbits(dz * step)) + p.update(ground=True, shift=False, enabled=True, time=0, boost=None, move=[lab.fbits(0.0), lab.fbits(1.0)], + width=lab.fbits(0.6), height=lab.fbits(1.8), xr=lab.fbits(0.0)) + yield dict(blocks=blocks, player=p, xa=lab.fbits(dx * step), za=lab.fbits(dz * step)) # Ceilings above the player and above the step. for ceiling in range(2, 6): for step_height in (0, 1): blocks = [[ox, oy + step_height, oz, 'minecraft:stone', {}], [ox - 1, oy + ceiling, oz, 'minecraft:stone', {}], [ox, oy + ceiling + step_height, oz, 'minecraft:stone_slab', {'type': 'top'}]] p = player(rng, ox - 0.5, float(oy), oz + 0.5, -90.0) - p.update(ground=True, shift=False, enabled=True, time=0, boost=None, move=[fbits(0.0), fbits(1.0)], - width=fbits(0.6), height=fbits(1.8)) - yield dict(blocks=blocks, player=p, xa=fbits(0.2), za=fbits(0.0)) + p.update(ground=True, shift=False, enabled=True, time=0, boost=None, move=[lab.fbits(0.0), lab.fbits(1.0)], + width=lab.fbits(0.6), height=lab.fbits(1.8)) + yield dict(blocks=blocks, player=p, xa=lab.fbits(0.2), za=lab.fbits(0.0)) for _ in range(9000): blocks = placed(rng, rng.choice([1, 2, 4, 8, 16, 30])) x = ox + rng.uniform(-2, 3) @@ -108,7 +98,7 @@ def cases(rng): move = rng.choice([0.0, 0.0, 0.01, 0.1, 0.215, 0.3, rng.uniform(0, 0.4)]) angle = rng.uniform(0, 6.283) - yield dict(blocks=blocks, player=p, xa=fbits(math.cos(angle) * move), za=fbits(math.sin(angle) * move)) + yield dict(blocks=blocks, player=p, xa=lab.fbits(math.cos(angle) * move), za=lab.fbits(math.sin(angle) * move)) def main(): diff --git a/tools/test_camera_parity.py b/tools/test_camera_parity.py index 3cd0e403d..bde9ea897 100644 --- a/tools/test_camera_parity.py +++ b/tools/test_camera_parity.py @@ -20,14 +20,6 @@ except ImportError: import lab, oracle_harness as harness -def fbits(v): - return struct.pack('>f', v).hex() - - -def dbits(v): - return struct.pack('>d', v).hex() - - def f32(v): return struct.unpack('>f', struct.pack('>f', v))[0] @@ -39,8 +31,8 @@ TICKS = [0, 1, 5, 10, 19, 20, 21, 72000, -5, 3] def fov_fields(rng): use = rng.choice([None, None, ['bow', rng.choice(TICKS)], ['spyglass', rng.choice(TICKS)], ['other', rng.choice(TICKS)]]) - return dict(flying=rng.random() < 0.3, ws=fbits(rng.choice(WALK + [rng.uniform(-0.3, 0.3)])), - speed=dbits(rng.choice(SPEED + [rng.uniform(0, 2)])), use=use) + return dict(flying=rng.random() < 0.3, ws=lab.fbits(rng.choice(WALK + [rng.uniform(-0.3, 0.3)])), + speed=lab.dbits(rng.choice(SPEED + [rng.uniform(0, 2)])), use=use) def fov_cases(rng): @@ -50,9 +42,9 @@ def fov_cases(rng): for use in [None, ['bow', 0], ['bow', 7], ['bow', 20], ['bow', 40], ['spyglass', 2], ['other', 5]]: for fp in (False, True): for scale in (0.0, 0.5, 1.0): - yield dict(t='fov', flying=flying, ws=fbits(ws), speed=dbits(speed), use=use, fp=fp, scale=fbits(scale)) + yield dict(t='fov', flying=flying, ws=lab.fbits(ws), speed=lab.dbits(speed), use=use, fp=fp, scale=lab.fbits(scale)) for _ in range(3000): - yield dict(t='fov', **fov_fields(rng), fp=rng.random() < 0.6, scale=fbits(rng.choice([0.0, 1.0, rng.random()]))) + yield dict(t='fov', **fov_fields(rng), fp=rng.random() < 0.6, scale=lab.fbits(rng.choice([0.0, 1.0, rng.random()]))) FULL = [[0, 0, 0, 1, 1, 1]] @@ -65,7 +57,7 @@ def world(rng, center): cx, cy, cz = center for _ in range(rng.choice([0, 3, 10, 40, 120])): p = (cx + rng.randint(-6, 6), cy + rng.randint(-3, 5), cz + rng.randint(-6, 6)) - cells[f'{p[0]} {p[1]} {p[2]}'] = [[dbits(v) for v in box] for box in rng.choice(SHAPES)] + cells[f'{p[0]} {p[1]} {p[2]}'] = [[lab.dbits(v) for v in box] for box in rng.choice(SHAPES)] return cells @@ -81,22 +73,22 @@ def entity_state(rng, center): prev = [p - rng.choice([0, 0, 0.1, 0.3, 1.0, rng.uniform(-1, 1)]) for p in pos] kind = 'player' if rng.random() < 0.85 else 'marker' xr, yr = angle(rng), angle(rng) - s = dict(kind=kind, pos=[dbits(v) for v in pos], prev=[dbits(v) for v in prev], xr=fbits(xr), yr=fbits(yr), - xro=fbits(rng.choice([xr, xr - 5, angle(rng)])), yro=fbits(rng.choice([yr, yr + 10, angle(rng)])), - eye=fbits(rng.choice([1.62, 1.27, 0.4, 0.2, 0.81, 3.24, rng.uniform(0, 4)]))) + s = dict(kind=kind, pos=[lab.dbits(v) for v in pos], prev=[lab.dbits(v) for v in prev], xr=lab.fbits(xr), yr=lab.fbits(yr), + xro=lab.fbits(rng.choice([xr, xr - 5, angle(rng)])), yro=lab.fbits(rng.choice([yr, yr + 10, angle(rng)])), + eye=lab.fbits(rng.choice([1.62, 1.27, 0.4, 0.2, 0.81, 3.24, rng.uniform(0, 4)]))) s.update(fov_fields(rng)) - s.update(scale_attr=dbits(rng.choice([1.0, 1.0, 0.5, 2.0, 0.0625, 16.0, rng.uniform(0.0625, 3)])), - dist=dbits(rng.choice([4.0, 4.0, 0.0, 1.0, 10.0, 32.0, rng.uniform(0, 32)])), + s.update(scale_attr=lab.dbits(rng.choice([1.0, 1.0, 0.5, 2.0, 0.0625, 16.0, rng.uniform(0.0625, 3)])), + dist=lab.dbits(rng.choice([4.0, 4.0, 0.0, 1.0, 10.0, 32.0, rng.uniform(0, 32)])), sleeping=rng.choice([None] * 6 + [[None], [0], [1], [2], [3]]), dead=rng.choice([None] * 6 + [0, 1, 5, 19, 20, 25]), - mount=rng.choice([None] * 4 + [[dbits(rng.choice([1.0, 2.0, 0.5])), dbits(rng.choice([4.0, 8.0, 1.0]))]])) + mount=rng.choice([None] * 4 + [[lab.dbits(rng.choice([1.0, 2.0, 0.5])), lab.dbits(rng.choice([4.0, 8.0, 1.0]))]])) return s def window(rng): """Window size and Camera.depthFar for the world and hand projections.""" w, h = rng.choice([(1600, 1000), (854, 480), (1920, 1080), (800, 1200), (1, 1), (rng.randint(1, 4000), rng.randint(1, 3000))]) - return [fbits(w), fbits(h), fbits(rng.choice([768.0, 128.0, 2048.0, 4096.0, rng.uniform(32, 4096)]))] + return [lab.fbits(w), lab.fbits(h), lab.fbits(rng.choice([768.0, 128.0, 2048.0, 4096.0, rng.uniform(32, 4096)]))] def camera_cases(rng): @@ -113,11 +105,11 @@ def camera_cases(rng): ops.append(['tick']) elif kind == 'frame': pt = rng.choice([0.0, 1.0, 0.5, 0.25, 0.999, rng.random()]) - ops.append(['frame', fbits(pt), rng.choice([0, 0, 0, 1, 2, 3]), rng.random() < 0.05] + window(rng)) + ops.append(['frame', lab.fbits(pt), rng.choice([0, 0, 0, 1, 2, 3]), rng.random() < 0.05] + window(rng)) else: ops.append(['type', rng.randrange(3)]) yield dict(t='camera', fov=rng.choice([70, 30, 110, 90, rng.randint(30, 110)]), - effect=dbits(rng.choice([1.0, 0.0, 0.5, rng.random()])), type=rng.choice([0, 1, 1, 2]), + effect=lab.dbits(rng.choice([1.0, 0.0, 0.5, rng.random()])), type=rng.choice([0, 1, 1, 2]), cells=world(rng, center), ops=ops) # The hand's projection at FOV 30, 70 and 110 while walking, sprinting, under # speed, drawing a bow, dying and in fluids: GameRenderer.render3dHud uses @@ -127,13 +119,13 @@ def camera_cases(rng): for use in (None, ['bow', 10]): for dead in (None, 5): for fluid in (0, 1, 2): - s = dict(kind='player', pos=[dbits(0.5), dbits(64.0), dbits(0.5)], prev=[dbits(0.5), dbits(64.0), dbits(0.5)], - xr=fbits(0.0), yr=fbits(0.0), xro=fbits(0.0), yro=fbits(0.0), eye=fbits(1.62), flying=False, - ws=fbits(0.1), speed=dbits(speed), use=use, scale_attr=dbits(1.0), dist=dbits(4.0), + s = dict(kind='player', pos=[lab.dbits(0.5), lab.dbits(64.0), lab.dbits(0.5)], prev=[lab.dbits(0.5), lab.dbits(64.0), lab.dbits(0.5)], + xr=lab.fbits(0.0), yr=lab.fbits(0.0), xro=lab.fbits(0.0), yro=lab.fbits(0.0), eye=lab.fbits(1.62), flying=False, + ws=lab.fbits(0.1), speed=lab.dbits(speed), use=use, scale_attr=lab.dbits(1.0), dist=lab.dbits(4.0), sleeping=None, dead=dead, mount=None) - ops = [['entity', s]] + [['tick']] * 8 + [['frame', fbits(pt), fluid, False, fbits(1600.0), fbits(1000.0), fbits(768.0)] + ops = [['entity', s]] + [['tick']] * 8 + [['frame', lab.fbits(pt), fluid, False, lab.fbits(1600.0), lab.fbits(1000.0), lab.fbits(768.0)] for pt in (0.0, 0.5, 1.0)] - yield dict(t='camera', fov=fov, effect=dbits(1.0), type=0, cells={}, ops=ops) + yield dict(t='camera', fov=fov, effect=lab.dbits(1.0), type=0, cells={}, ops=ops) # A walled-in third-person camera stepping through every distance bound. for dist in [0.0, 0.5, 1.0, 2.0, 4.0, 8.0, 32.0]: for wall in range(0, 6): @@ -141,28 +133,28 @@ def camera_cases(rng): cells = {} for dx in range(-6, 7): for dy in range(62, 68): - cells[f'{dx} {dy} {-wall - 1}'] = [[dbits(v) for v in box] for box in FULL] - cells[f'{dx} {dy} {wall + 1}'] = [[dbits(v) for v in box] for box in FULL] - s = dict(kind='player', pos=[dbits(0.5), dbits(64.0), dbits(0.5)], prev=[dbits(0.5), dbits(64.0), dbits(0.5)], - xr=fbits(15.0), yr=fbits(yaw), xro=fbits(15.0), yro=fbits(yaw), eye=fbits(1.62), flying=False, - ws=fbits(0.1), speed=dbits(f32(0.1)), use=None, scale_attr=dbits(1.0), dist=dbits(dist), + cells[f'{dx} {dy} {-wall - 1}'] = [[lab.dbits(v) for v in box] for box in FULL] + cells[f'{dx} {dy} {wall + 1}'] = [[lab.dbits(v) for v in box] for box in FULL] + s = dict(kind='player', pos=[lab.dbits(0.5), lab.dbits(64.0), lab.dbits(0.5)], prev=[lab.dbits(0.5), lab.dbits(64.0), lab.dbits(0.5)], + xr=lab.fbits(15.0), yr=lab.fbits(yaw), xro=lab.fbits(15.0), yro=lab.fbits(yaw), eye=lab.fbits(1.62), flying=False, + ws=lab.fbits(0.1), speed=lab.dbits(f32(0.1)), use=None, scale_attr=lab.dbits(1.0), dist=lab.dbits(dist), sleeping=None, dead=None, mount=None) - ops = [['entity', s]] + [['tick']] * 6 + [['frame', fbits(pt), 0, False] + window(rng) for pt in (0.0, 0.5, 1.0)] - yield dict(t='camera', fov=70, effect=dbits(1.0), type=1 + (wall % 2), cells=cells, ops=ops) + ops = [['entity', s]] + [['tick']] * 6 + [['frame', lab.fbits(pt), 0, False] + window(rng) for pt in (0.0, 0.5, 1.0)] + yield dict(t='camera', fov=70, effect=lab.dbits(1.0), type=1 + (wall % 2), cells=cells, ops=ops) def bob_cases(rng): for n in range(6000): living = None if rng.random() < 0.8: - living = dict(hurt=fbits(rng.choice([-1.0, -0.5, 0.0, 10.0, 9.5, 5.25, 1.0, rng.uniform(-2, 11)])), - duration=rng.choice([10, 10, 0, 5, 20]), dir=fbits(rng.choice([0.0, 90.0, -45.0, 180.0, rng.uniform(-360, 360)])), - death=rng.choice([None, None, None, fbits(rng.choice([0.0, 0.5, 19.5, 20.0, 25.0, rng.uniform(0, 30)]))])) + living = dict(hurt=lab.fbits(rng.choice([-1.0, -0.5, 0.0, 10.0, 9.5, 5.25, 1.0, rng.uniform(-2, 11)])), + duration=rng.choice([10, 10, 0, 5, 20]), dir=lab.fbits(rng.choice([0.0, 90.0, -45.0, 180.0, rng.uniform(-360, 360)])), + death=rng.choice([None, None, None, lab.fbits(rng.choice([0.0, 0.5, 19.5, 20.0, 25.0, rng.uniform(0, 30)]))])) player = None if rng.random() < 0.8: - player = [fbits(rng.choice([0.0, -0.3, -1.7, -100.25, rng.uniform(-1000, 0)])), - fbits(rng.choice([0.0, 0.1, 0.05, 0.0999, rng.uniform(0, 0.1)]))] - yield dict(t='bob', living=living, player=player, tilt=dbits(rng.choice([1.0, 0.0, 0.5, rng.random()])), + player = [lab.fbits(rng.choice([0.0, -0.3, -1.7, -100.25, rng.uniform(-1000, 0)])), + lab.fbits(rng.choice([0.0, 0.1, 0.05, 0.0999, rng.uniform(0, 0.1)]))] + yield dict(t='bob', living=living, player=player, tilt=lab.dbits(rng.choice([1.0, 0.0, 0.5, rng.random()])), view=rng.random() < 0.8) @@ -174,12 +166,12 @@ def avatar_cases(rng): if kind == 'tick': ops.append(['tick']) elif kind == 'move': - ops.append(['move', fbits(rng.choice([0.0, 0.2158, -0.1, rng.uniform(-1, 1)])), fbits(rng.choice([0.0, 0.2806, rng.uniform(-1, 1)]))]) + ops.append(['move', lab.fbits(rng.choice([0.0, 0.2158, -0.1, rng.uniform(-1, 1)])), lab.fbits(rng.choice([0.0, 0.2806, rng.uniform(-1, 1)]))]) elif kind == 'bob': v = [rng.choice([0.0, 0.05, -0.2, rng.uniform(-0.5, 0.5)]), rng.uniform(-1, 1), rng.choice([0.0, 0.08, rng.uniform(-0.5, 0.5)])] - ops.append(['bob', rng.random() < 0.7, rng.random() < 0.1, rng.random() < 0.1, [dbits(x) for x in v]]) + ops.append(['bob', rng.random() < 0.7, rng.random() < 0.1, rng.random() < 0.1, [lab.dbits(x) for x in v]]) elif kind == 'frame': - ops.append(['frame', fbits(rng.choice([0.0, 1.0, 0.5, rng.random()]))]) + ops.append(['frame', lab.fbits(rng.choice([0.0, 1.0, 0.5, rng.random()]))]) else: ops.append(['reset']) yield dict(t='avatar', ops=ops) diff --git a/tools/test_dedicated_server_parity.py b/tools/test_dedicated_server_parity.py index 22677bf07..07d61f3d5 100644 --- a/tools/test_dedicated_server_parity.py +++ b/tools/test_dedicated_server_parity.py @@ -68,10 +68,12 @@ import zlib try: from . import lab, oracle_harness as oh, test_rust_parity as base + from .lab import free_port # the other server harnesses import it from here except ImportError: import lab import oracle_harness as oh import test_rust_parity as base + from lab import free_port NAME = 'dedicated-server-parity' SEED = 12345 @@ -89,12 +91,6 @@ PROPERTIES = { } -def free_port(): - with socket.socket() as s: - s.bind(('127.0.0.1', 0)) - return s.getsockname()[1] - - def icon_png(): """A 64x64 RGBA PNG (server-icon.png).""" rows = b''.join(b'\0' + b''.join(bytes((x * 4, y * 4, (x ^ y) * 4, 255)) for x in range(64)) for y in range(64)) diff --git a/tools/test_first_person_parity.py b/tools/test_first_person_parity.py index 476d2cc52..fcb1206ec 100644 --- a/tools/test_first_person_parity.py +++ b/tools/test_first_person_parity.py @@ -18,14 +18,6 @@ except ImportError: import lab, oracle_harness as harness -def fbits(v): - return struct.pack('>f', v).hex() - - -def dbits(v): - return struct.pack('>d', v).hex() - - def f32(v): return struct.unpack('>f', struct.pack('>f', v))[0] @@ -52,15 +44,15 @@ def step(hit=('miss',), ac=0, uc=0, ad=False, ud=False, busy=False, using=False, """rot: the player's (xRot, yRot) from this step on (LocalPlayer.applyInput's hand sway).""" return dict(hit=list(hit), ac=ac, uc=uc, ad=ad, ud=ud, busy=busy, using=using, uses=uses or dict(entity='pass', block='pass', item='pass'), shrink=shrink, set=set, - rot=None if rot is None else [fbits(rot[0]), fbits(rot[1])]) + rot=None if rot is None else [lab.fbits(rot[0]), lab.fbits(rot[1])]) def case(steps, creative=False, main=None, off=None, blocks=None, haste=None, fatigue=None): return dict(creative=creative, main=main, off=off, haste=haste, fatigue=fatigue, blocks=[[*p, b] for p, b in (blocks or {}).items()], steps=steps, - rust=dict(blocks={k: [fbits(v[0]), v[1]] for k, v in BLOCKS.items()}, - tools={k: [fbits(v[0]), v[1]] for k, v in TOOLS.items()}, - attack_speed={k: dbits(v) for k, v in SPEEDS.items()})) + rust=dict(blocks={k: [lab.fbits(v[0]), v[1]] for k, v in BLOCKS.items()}, + tools={k: [lab.fbits(v[0]), v[1]] for k, v in TOOLS.items()}, + attack_speed={k: lab.dbits(v) for k, v in SPEEDS.items()})) def block_hit(pos, face=1): diff --git a/tools/test_friends.py b/tools/test_friends.py index 3daeb28e7..8fa1a4fee 100644 --- a/tools/test_friends.py +++ b/tools/test_friends.py @@ -72,12 +72,6 @@ def wait_for(endpoint, line, predicate, timeout=60, what=None): raise AssertionError(f'timed out waiting for {what or line}: {last}') -def free_port(): - with socket.socket() as s: - s.bind(('127.0.0.1', 0)) - return s.getsockname()[1] - - def health(relay_url): with urllib.request.urlopen(f'http://{relay_url}/health', timeout=5) as r: return json.loads(r.read()) @@ -204,7 +198,7 @@ def leave(host, guest, name): def start_relay(args, work): """A relay on a free local port, as anyone would run their own.""" - port = free_port() + port = lab.free_port() url = f'127.0.0.1:{port}' log = (work / 'relay.log').open('w+') process = subprocess.Popen([str(args.relay.resolve())], env=dict(os.environ, PORT=str(port)), diff --git a/tools/test_game_loop_parity.py b/tools/test_game_loop_parity.py index 101b3f238..f5f5e94a3 100644 --- a/tools/test_game_loop_parity.py +++ b/tools/test_game_loop_parity.py @@ -6,9 +6,7 @@ and absolute motion, sensitivity curve, cinematic smoothing, inversion, scroll accumulation and discrete scrolling), Entity.turn and ToggleKeyMapping, all driven with identical synthetic input and time sequences.""" import json -import math import random -import struct try: from . import lab, oracle_harness as harness @@ -16,14 +14,6 @@ except ImportError: import lab, oracle_harness as harness -def fbits(v): - return struct.pack('>f', v).hex() - - -def dbits(v): - return struct.pack('>d', v).hex() - - def timer_cases(rng): for n in range(700): tps = rng.choice([20.0, 20.0, 10.0, 60.0, 1.0, 144.0]) @@ -38,12 +28,12 @@ def timer_cases(rng): now -= rng.randrange(0, 30) if kind == 'adv': mspt = rng.choice([None, None, None, 50.0, 25.0, 100.0, 500.0, 1000.0, 0.5, 5.0, 0.1]) - ops.append(['adv', now, None if mspt is None else fbits(mspt)]) + ops.append(['adv', now, None if mspt is None else lab.fbits(mspt)]) elif kind == 'real': ops.append(['real', now]) else: ops.append([kind, rng.random() < 0.5]) - yield dict(t='timer', tps=fbits(tps), start=start, ops=ops) + yield dict(t='timer', tps=lab.fbits(tps), start=start, ops=ops) def rate_cases(rng): @@ -55,7 +45,7 @@ def rate_cases(rng): kind = rng.choice(['rate', 'freeze', 'step', 'stopstep', 'sprint', 'check', 'check', 'check', 'end', 'stopsprint', 'tick', 'tick', 'tick']) if kind == 'rate': - ops.append(['rate', fbits(rng.choice(rates))]) + ops.append(['rate', lab.fbits(rng.choice(rates))]) elif kind == 'freeze': ops.append(['freeze', rng.random() < 0.5]) elif kind == 'step': @@ -78,7 +68,7 @@ def scale_cases(rng): for uni in (False, True): sw, sh = rng.choice([(w, h), (max(w // 2, 1), max(h // 2, 1)), (w + 1, h + 3)]) yield dict(t='scale', w=w, h=h, max=maxs, uni=uni, sw=sw, sh=sh, - x=dbits(rng.uniform(0, w)), y=dbits(rng.uniform(0, h))) + x=lab.dbits(rng.uniform(0, w)), y=lab.dbits(rng.uniform(0, h))) def limit_cases(rng): @@ -103,13 +93,13 @@ def mouse_cases(rng): if kind == 'move': x, y = rng.uniform(0, 1920), rng.uniform(0, 1080) big = rng.choice([1, 1, 1, 50, 5000]) - ops.append(['move', dbits(x), dbits(y), dbits(rng.uniform(-30, 30) * big), dbits(rng.uniform(-30, 30) * big)]) + ops.append(['move', lab.dbits(x), lab.dbits(y), lab.dbits(rng.uniform(-30, 30) * big), lab.dbits(rng.uniform(-30, 30) * big)]) elif kind == 'frame': time += rng.choice([0.0, 0.001, 0.0069, 0.016, 0.0167, 0.05, 0.5, 2.0]) - ops.append(['frame', dbits(time)]) + ops.append(['frame', lab.dbits(time)]) else: ops.append(['ignore']) - yield dict(t='mouse', sens=dbits(sens), smooth=rng.random() < 0.4, scoping=rng.random() < 0.3, + yield dict(t='mouse', sens=lab.dbits(sens), smooth=rng.random() < 0.4, scoping=rng.random() < 0.3, ix=rng.random() < 0.3, iy=rng.random() < 0.3, grabbed=rng.random() < 0.85, active=rng.random() < 0.9, ops=ops) @@ -119,14 +109,14 @@ def scroll_cases(rng): for _ in range(rng.randrange(1, 40)): x = rng.choice([0.0, 0.0, 0.0, 1.0, -1.0, 0.25, -0.25, 0.4, 2.0, 5.5, -7.0, rng.uniform(-3, 3)]) y = rng.choice([0.0, 1.0, -1.0, 1.0, -1.0, 0.1, -0.3, 0.5, -0.5, 2.0, -3.0, 120.0, rng.uniform(-3, 3)]) - ops.append([dbits(x), dbits(y)]) - case = dict(t='scroll', discrete=rng.random() < 0.4, sens=dbits(rng.choice([1.0, 0.01, 10.0, 0.5, 2.0, 3.1622776601683795])), + ops.append([lab.dbits(x), lab.dbits(y)]) + case = dict(t='scroll', discrete=rng.random() < 0.4, sens=lab.dbits(rng.choice([1.0, 0.01, 10.0, 0.5, 2.0, 3.1622776601683795])), slot=rng.randrange(9), ops=ops) yield case yield dict(case, spectator=True) # Bounds, recovery from each bound, fractional accumulation, and horizontal-only input. - yield dict(t='scroll', discrete=False, sens=dbits(1.0), slot=4, spectator=True, - ops=[[dbits(x), dbits(y)] for x, y in + yield dict(t='scroll', discrete=False, sens=lab.dbits(1.0), slot=4, spectator=True, + ops=[[lab.dbits(x), lab.dbits(y)] for x, y in [(0, 100), (0, -1), (0, -100), (0, 1), (0, .4), (0, .7), (0, -.4), (0, -.7), (5, 0), (-5, 0)]]) diff --git a/tools/test_save_roundtrip_parity.py b/tools/test_save_roundtrip_parity.py index 72bea8845..bd8b07a29 100644 --- a/tools/test_save_roundtrip_parity.py +++ b/tools/test_save_roundtrip_parity.py @@ -109,15 +109,9 @@ BENIGN = re.compile(r'(Unpacking |OFFLINE/INSECURE|online-mode|authenticate user # ---------------------------------------------------------------- vanilla server -def free_port(): - with socket.socket() as s: - s.bind(('127.0.0.1', 0)) - return s.getsockname()[1] - - class Vanilla: def __init__(self, directory, port=None, world='world', extra=None): - port = port or free_port() + port = port or lab.free_port() directory.mkdir(parents=True, exist_ok=True) (directory / 'eula.txt').write_text('eula=true\n') props = { diff --git a/tools/test_secure_chat_parity.py b/tools/test_secure_chat_parity.py index ed408efa3..c6c76a892 100644 --- a/tools/test_secure_chat_parity.py +++ b/tools/test_secure_chat_parity.py @@ -41,12 +41,12 @@ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer try: from . import lab, oracle_harness as oh, test_rust_parity as base - from .test_dedicated_server_parity import Server, free_port + from .test_dedicated_server_parity import Server except ImportError: import lab import oracle_harness as oh import test_rust_parity as base - from test_dedicated_server_parity import Server, free_port + from test_dedicated_server_parity import Server NAME = 'secure-chat-parity' SCENARIOS = { @@ -153,20 +153,10 @@ def dashed(u): return f'{u[:8]}-{u[8:12]}-{u[12:16]}-{u[16:20]}-{u[20:]}' -def varint(n): - out = b'' - while True: - b = n & 0x7F - n >>= 7 - out += bytes([b | (0x80 if n else 0)]) - if not n: - return out - - def status(port): """The status response's JSON (ServerStatus).""" def packet(body): - return varint(len(body)) + body + return lab.varint(len(body)) + body def read_varint(s): value = 0 @@ -177,7 +167,7 @@ def status(port): return value with socket.create_connection(('127.0.0.1', port), timeout=10) as s: host = b'127.0.0.1' - s.sendall(packet(b'\x00' + varint(777) + varint(len(host)) + host + port.to_bytes(2, 'big') + b'\x01')) + s.sendall(packet(b'\x00' + lab.varint(777) + lab.varint(len(host)) + host + port.to_bytes(2, 'big') + b'\x01')) s.sendall(packet(b'\x00')) length = read_varint(s) data = b'' @@ -212,7 +202,7 @@ def chat_lines(lines): def run_side(label, scenario, command, directory, client): - port = free_port() + port = lab.free_port() shutil.rmtree(directory, ignore_errors=True) directory.mkdir(parents=True) props = dict(PROPERTIES, **SCENARIOS[scenario], **{'server-port': str(port)}) diff --git a/tools/test_server_dimension_players_parity.py b/tools/test_server_dimension_players_parity.py index 8453d14af..b1efc4378 100644 --- a/tools/test_server_dimension_players_parity.py +++ b/tools/test_server_dimension_players_parity.py @@ -96,17 +96,6 @@ def run_side(label, server_command, client_command, build, world): SHOWN = ('add', 'remove', 'respawn', 'track', 'untrack', 'update') -def settled(lines): - """One player's lines of one step, in order, with the waypoint packets reduced to the last for each waypoint: - what the locator bar shows once the step is over. Arriving in a level, vanilla sends the same waypoint more - than once on the way there; the packet by packet order is test_locator_bar_parity's.""" - def waypoint(line): - parts = line.split(' ') - return parts[3] if len(parts) > 3 and parts[2] in ('track', 'untrack', 'update') else None - last = {waypoint(l): i for i, l in enumerate(lines) if waypoint(l)} - return [l for i, l in enumerate(lines) if not waypoint(l) or last[waypoint(l)] == i] - - def shown(lines): """Each player's adds, removes, respawns and waypoints (as the step leaves them), step by step; sorted within a step (the packets of one step may leave in another order within the tick).""" @@ -115,7 +104,7 @@ def shown(lines): parts = line.split(' ', 2) if len(parts) == 3 and parts[2].split(' ')[0] in SHOWN: steps.setdefault((parts[0], parts[1]), []).append(line) - return [l for key in steps for l in sorted(settled(steps[key]))] + return [l for key in steps for l in sorted(lab.settled(steps[key]))] def main(): diff --git a/tools/test_server_player_targets_parity.py b/tools/test_server_player_targets_parity.py index 71416223b..cad8782cc 100644 --- a/tools/test_server_player_targets_parity.py +++ b/tools/test_server_player_targets_parity.py @@ -15,7 +15,6 @@ in sorted order (the packets of one step may leave in another order within the t waypoints as each step leaves them (one manager per level: a player in another level is untracked). What a death does besides its messages is left to its own harness. """ -import hashlib import os from pathlib import Path import platform @@ -23,7 +22,6 @@ import shutil import subprocess import sys import time -import uuid as uuidlib try: from . import lab, test_rust_parity as base @@ -39,20 +37,12 @@ NAME = 'server-player-targets-parity' NETHER, OVERWORLD = 'minecraft:the_nether', 'minecraft:overworld' -def offline_uuid(name): - """UUIDUtil.createOfflinePlayerUUID.""" - digest = bytearray(hashlib.md5(f'OfflinePlayer:{name}'.encode()).digest()) - digest[6] = (digest[6] & 0x0F) | 0x30 - digest[8] = (digest[8] & 0x3F) | 0x80 - return str(uuidlib.UUID(bytes=bytes(digest))) - - # (label, what the console does, what the players do). SCRIPT = [ ('console-give', ['give Bob minecraft:diamond 3'], []), ('console-give-all', ['give @a minecraft:apple'], []), ('alice-give-bob', [], ['cmd Alice give Bob minecraft:stick 2']), - ('console-give-uuid', [f'give {offline_uuid("Bob")} minecraft:iron_ingot'], []), + ('console-give-uuid', [f'give {lab.offline_uuid("Bob")} minecraft:iron_ingot'], []), ('console-give-nearest', ['give @p minecraft:emerald'], []), ('alice-give-random', [], ['cmd Alice give @r[name=Bob] minecraft:gold_ingot']), ('console-gamemode', ['gamemode survival Bob'], []), @@ -83,17 +73,6 @@ DEATH_STEP = 'step=console-kill' STEP_SECONDS = 2.5 -def settled(lines): - """One player's lines of one step, in order, with the waypoint packets reduced to the last for each waypoint: - what the locator bar shows once the step is over. Arriving in a level, vanilla sends the same waypoint more - than once on the way there; the packet by packet order is test_locator_bar_parity's.""" - def waypoint(line): - parts = line.split(' ') - return parts[3] if len(parts) > 3 and parts[2] in ('track', 'untrack', 'update') else None - last = {waypoint(l): i for i, l in enumerate(lines) if waypoint(l)} - return [l for i, l in enumerate(lines) if not waypoint(l) or last[waypoint(l)] == i] - - def run_side(label, server_command, client_command, build, world): directory = build / label port = server_dir(directory, world) @@ -151,7 +130,7 @@ def run_side(label, server_command, client_command, build, world): steps.setdefault((who, step), []).append(line) order = {f'step={s}': i for i, (s, _, _) in enumerate(SCRIPT)} keys = sorted(steps, key=lambda k: (k[0], order.get(k[1], -1))) - return [l for k in keys for l in sorted(settled(steps[k]))], errors + return [l for k in keys for l in sorted(lab.settled(steps[k]))], errors def main(): diff --git a/tools/test_sound_engine_parity.py b/tools/test_sound_engine_parity.py index 36095542c..8eb02a7fe 100644 --- a/tools/test_sound_engine_parity.py +++ b/tools/test_sound_engine_parity.py @@ -18,14 +18,10 @@ import math import random try: - from . import audio_harness as h + from . import audio_harness as h, lab except ImportError: import audio_harness as h - - -def dbits(v): - import struct - return struct.pack('>d', v).hex() + import lab class Script: @@ -37,11 +33,11 @@ class Script: self.lines.append(' '.join(map(str, parts))) def play(self, name, event, source=4, volume=1.0, pitch=1.0, at=(0.0, 0.0, 0.0), looping=0, delay=0, linear=True, relative=0, seed=0, kind='simple'): - self.add('play', name, kind, event, source, h.bits(volume), h.bits(pitch), *map(dbits, at), looping, delay, + self.add('play', name, kind, event, source, h.bits(volume), h.bits(pitch), *map(lab.dbits, at), looping, delay, 'linear' if linear else 'none', relative, seed) def cart(self, name, at, delta, removed=0): - self.add('cart', name, *map(dbits, at), *map(dbits, delta), removed) + self.add('cart', name, *map(lab.dbits, at), *map(lab.dbits, delta), removed) def tick(self, n=1, paused=0): for _ in range(n): diff --git a/tools/upgrade_world.py b/tools/upgrade_world.py index 9db5e4517..5f42988f1 100755 --- a/tools/upgrade_world.py +++ b/tools/upgrade_world.py @@ -9,7 +9,6 @@ level.dat, player and data/*.dat files) is applied exactly. The original is kept upgrade_world.py SOURCE_WORLD DEST_WORLD """ import shutil -import socket import subprocess import sys import tempfile @@ -24,12 +23,6 @@ ROOT = Path(__file__).resolve().parent.parent SERVER = lab.GAME / 'jars' / 'server.jar' -def free_port(): - with socket.socket() as s: - s.bind(('127.0.0.1', 0)) - return s.getsockname()[1] - - def main(): if len(sys.argv) != 3: sys.exit(__doc__) @@ -42,7 +35,7 @@ def main(): scratch = Path(scratch) (scratch / 'eula.txt').write_text('eula=true\n') (scratch / 'server.properties').write_text( - f'level-name=world\nonline-mode=false\nserver-port={free_port()}\n') + f'level-name=world\nonline-mode=false\nserver-port={lab.free_port()}\n') shutil.copytree(source, scratch / 'world') # The server upgrades at startup, then keeps running as a normal server: stop it # once it reports "Done". diff --git a/tools/vanilla_client.py b/tools/vanilla_client.py index 64eb82691..2cdfeec21 100644 --- a/tools/vanilla_client.py +++ b/tools/vanilla_client.py @@ -14,12 +14,10 @@ that skips the first-run screens, keeps the game running unfocused and renders few chunks, so a harness can drive it with vscreen keys. """ import argparse -import hashlib import json import os import subprocess import sys -import uuid import zipfile from pathlib import Path @@ -56,13 +54,6 @@ if sys.platform == "darwin": OPTIONS["enableVsync"] = "false" -def offline_uuid(name): - digest = bytearray(hashlib.md5(("OfflinePlayer:" + name).encode()).digest()) - digest[6] = (digest[6] & 0x0F) | 0x30 - digest[8] = (digest[8] & 0x3F) | 0x80 - return uuid.UUID(bytes=bytes(digest)) - - def host_library(path): name = Path(path).name if "natives-" in name: @@ -127,7 +118,7 @@ def command(args): f"-Djava.library.path={natives}", f"-Dorg.lwjgl.system.SharedLibraryExtractPath={natives}", f"-Dio.netty.native.workdir={natives}", "-cp", os.pathsep.join(map(str, jars)), "net.minecraft.client.main.Main", - "--username", args.name, "--uuid", offline_uuid(args.name).hex, + "--username", args.name, "--uuid", lab.offline_uuid(args.name).hex, "--accessToken", "0", "--offlineDeveloperMode", "--version", lab.VERSION, "--gameDir", str(args.game_dir), "--assetsDir", str(assets), "--assetIndex", json.loads((GAME / "manifest.json").read_text())["assetIndex"]["id"], diff --git a/tools/vanilla_login.py b/tools/vanilla_login.py index fa2664c9d..ecc9715b4 100644 --- a/tools/vanilla_login.py +++ b/tools/vanilla_login.py @@ -11,12 +11,10 @@ its log reports the position it read, and console commands such as `data get entity NAME` can inspect the loaded player meanwhile. """ import argparse -import hashlib import socket import struct import sys import time -import uuid import zlib try: @@ -25,27 +23,9 @@ except ImportError: import lab -def offline_uuid(name): - digest = bytearray(hashlib.md5(('OfflinePlayer:' + name).encode()).digest()) - digest[6] = (digest[6] & 0x0F) | 0x30 - digest[8] = (digest[8] & 0x3F) | 0x80 - return uuid.UUID(bytes=bytes(digest)) - - -def varint(v): - out = b'' - v &= 0xFFFFFFFF - while True: - b = v & 0x7F - v >>= 7 - out += bytes([b | (0x80 if v else 0)]) - if not v: - return out - - def string(s): data = s.encode() - return varint(len(data)) + data + return lab.varint(len(data)) + data class Connection: @@ -73,44 +53,33 @@ class Connection: return value def send(self, packet_id, payload=b''): - body = varint(packet_id) + payload + body = lab.varint(packet_id) + payload if self.threshold >= 0: if len(body) >= self.threshold: - body = varint(len(body)) + zlib.compress(body) + body = lab.varint(len(body)) + zlib.compress(body) else: - body = varint(0) + body - self.sock.sendall(varint(len(body)) + body) + body = lab.varint(0) + body + self.sock.sendall(lab.varint(len(body)) + body) def receive(self): length = self.read_varint() body = self.read_exact(length) if self.threshold >= 0: - pos, size = parse_varint(body, 0) + pos, size = lab.parse_varint(body, 0) body = zlib.decompress(body[pos:]) if size else body[pos:] - pos, packet_id = parse_varint(body, 0) + pos, packet_id = lab.parse_varint(body, 0) return packet_id, body[pos:] -def parse_varint(data, pos): - value = shift = 0 - while True: - b = data[pos] - pos += 1 - value |= (b & 0x7F) << shift - shift += 7 - if not b & 0x80: - return pos, value - - def join(host, port, name, seconds): c = Connection(host, port) - c.send(0, varint(lab.version_info()['protocol_version']) + string(host) + struct.pack('>H', port) + varint(2)) - c.send(0, string(name) + offline_uuid(name).bytes) + c.send(0, lab.varint(lab.version_info()['protocol_version']) + string(host) + struct.pack('>H', port) + lab.varint(2)) + c.send(0, string(name) + lab.offline_uuid(name).bytes) # Login: compression, then login finished. while True: pid, data = c.receive() if pid == 3: - c.threshold = parse_varint(data, 0)[1] + c.threshold = lab.parse_varint(data, 0)[1] elif pid == 2: break elif pid == 0: @@ -119,7 +88,7 @@ def join(host, port, name, seconds): while True: pid, data = c.receive() if pid == 15: # select known packs: claim none, receive full registries - c.send(7, varint(0)) + c.send(7, lab.varint(0)) elif pid == 4: # keep alive c.send(4, data) elif pid == 5: # ping @@ -153,7 +122,7 @@ def main(): p.add_argument('--uuid', action='store_true') args = p.parse_args() if args.uuid: - print(offline_uuid(args.name)) + print(lab.offline_uuid(args.name)) return 0 join(args.host, args.port, args.name, args.seconds) return 0 diff --git a/tools/vanilla_world_check.py b/tools/vanilla_world_check.py index 1bbc64004..317b7e998 100644 --- a/tools/vanilla_world_check.py +++ b/tools/vanilla_world_check.py @@ -17,12 +17,16 @@ load failures. import argparse import os import re -import socket import subprocess import sys import threading import time +try: + from . import lab +except ImportError: + import lab + SUSPICIOUS = re.compile( r'(ERROR|login failed|Exception|Couldn\'t load|Failed to|Skipping|Invalid|Unknown|corrupt|' r'Recreating|upgrad|DataFix|out of bounds|missing|Mismatch)', re.IGNORECASE) @@ -31,12 +35,6 @@ BENIGN = re.compile(r'(Unpacking |OFFLINE/INSECURE|online-mode|authenticate user r'Failed to load properties|Failed to load eula)') -def free_port(): - with socket.socket() as s: - s.bind(('127.0.0.1', 0)) - return s.getsockname()[1] - - def main(): p = argparse.ArgumentParser() p.add_argument('--jar', required=True) @@ -48,7 +46,7 @@ def main(): p.add_argument('--wait-after', type=float, default=1.0) p.add_argument('--timeout', type=float, default=240) args = p.parse_args() - port = args.port or free_port() + port = args.port or lab.free_port() os.makedirs(args.dir, exist_ok=True) with open(os.path.join(args.dir, 'eula.txt'), 'w') as f: f.write('eula=true\n') From 60bad91a5004891ca5b746639a84c3a8b6538fdd Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 15:31:51 +0200 Subject: [PATCH 33/39] Client zip.rs reads archives with the zip crate it already links The 120-line hand-written central-directory and local-header parser goes: the zip crate (already the client's dependency for world backups) indexes the archive, zip64 and odd offsets included. Entries are still read raw and inflated here, so as with Java's ZipFile (vanilla's FilePackResources) their CRC isn't checked. New tests: folder entries, a nested pack root, a Windows-style name, one broken entry among good ones, and files that aren't zips. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/zip.rs | 206 ++++++++++++++++++------------- 1 file changed, 122 insertions(+), 84 deletions(-) diff --git a/crates/horizon-client/src/zip.rs b/crates/horizon-client/src/zip.rs index a0551c829..22cab132d 100644 --- a/crates/horizon-client/src/zip.rs +++ b/crates/horizon-client/src/zip.rs @@ -1,75 +1,35 @@ -//! Minimal read-only ZIP archive support (stored and deflated entries), used -//! for resource packs and the unifont glyph archive. -use std::{collections::BTreeMap, fs, io, path::Path}; +//! Read-only ZIP archives held in memory (resource packs, data packs and the +//! unifont glyph archive), on the `zip` crate. Like Java's `ZipFile` (what +//! vanilla's `FilePackResources` reads with), entries aren't CRC-checked. +use std::{ + collections::BTreeMap, + fs, + io::{self, Cursor, Read}, + path::Path, + sync::Arc, +}; +use zip::{CompressionMethod, ZipArchive}; -/// One archive held in memory with its central directory indexed by name. +/// One archive with its entries indexed by name (`\` read as `/`, folders +/// left out). pub struct Zip { - data: Vec, - entries: BTreeMap, + archive: ZipArchive>>, + entries: BTreeMap, } -#[derive(Clone, Copy)] -struct Entry { - method: u16, - compressed: usize, - size: usize, - header: usize, -} -fn invalid(message: &str) -> io::Error { - io::Error::new(io::ErrorKind::InvalidData, message) -} -fn u16_at(d: &[u8], at: usize) -> io::Result { - d.get(at..at + 2) - .map(|b| u16::from_le_bytes([b[0], b[1]])) - .ok_or_else(|| invalid("truncated zip")) -} -fn u32_at(d: &[u8], at: usize) -> io::Result { - d.get(at..at + 4) - .map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]])) - .ok_or_else(|| invalid("truncated zip")) +fn invalid(e: impl std::fmt::Display) -> io::Error { + io::Error::new(io::ErrorKind::InvalidData, e.to_string()) } impl Zip { pub fn open(path: &Path) -> io::Result { Self::from_bytes(fs::read(path)?) } pub fn from_bytes(data: Vec) -> io::Result { - // End of central directory: the last "PK\x05\x06" within 64 KiB of the end. - let min = data.len().saturating_sub(22 + 65_535); - let eocd = (min..=data.len().saturating_sub(22)) - .rev() - .find(|&i| data[i..i + 4] == [0x50, 0x4b, 0x05, 0x06]) - .ok_or_else(|| invalid("not a zip archive"))?; - let count = usize::from(u16_at(&data, eocd + 10)?); - let mut at = u32_at(&data, eocd + 16)? as usize; - let mut entries = BTreeMap::new(); - for _ in 0..count { - if u32_at(&data, at)? != 0x0201_4b50 { - return Err(invalid("bad central directory entry")); - } - let method = u16_at(&data, at + 10)?; - let compressed = u32_at(&data, at + 20)? as usize; - let size = u32_at(&data, at + 24)? as usize; - let name_len = usize::from(u16_at(&data, at + 28)?); - let extra_len = usize::from(u16_at(&data, at + 30)?); - let comment_len = usize::from(u16_at(&data, at + 32)?); - let header = u32_at(&data, at + 42)? as usize; - let name = data - .get(at + 46..at + 46 + name_len) - .ok_or_else(|| invalid("truncated zip name"))?; - let name = String::from_utf8_lossy(name).replace('\\', "/"); - if !name.ends_with('/') { - entries.insert( - name, - Entry { - method, - compressed, - size, - header, - }, - ); - } - at += 46 + name_len + extra_len + comment_len; - } - Ok(Self { data, entries }) + let archive = ZipArchive::new(Cursor::new(Arc::from(data))).map_err(invalid)?; + let entries = (0..archive.len()) + .filter_map(|i| Some((archive.name_for_index(i)?.replace('\\', "/"), i))) + .filter(|(name, _)| !name.ends_with('/')) + .collect(); + Ok(Self { archive, entries }) } pub fn names(&self) -> impl Iterator { self.entries.keys().map(String::as_str) @@ -78,30 +38,21 @@ impl Zip { self.entries.contains_key(name) } pub fn read(&self, name: &str) -> io::Result> { - let e = *self + let &index = self .entries .get(name) .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, name.to_owned()))?; - if u32_at(&self.data, e.header)? != 0x0403_4b50 { - return Err(invalid("bad local file header")); - } - let start = e.header - + 30 - + usize::from(u16_at(&self.data, e.header + 26)?) - + usize::from(u16_at(&self.data, e.header + 28)?); - let raw = self - .data - .get(start..start + e.compressed) - .ok_or_else(|| invalid("truncated zip entry"))?; - match e.method { - 0 => Ok(raw.to_vec()), - 8 => { - let out = miniz_oxide::inflate::decompress_to_vec_with_limit( - raw, - e.size.max(1) * 2 + 1024, - ) - .map_err(|_| invalid("corrupt deflate stream"))?; - Ok(out) + // A clone shares the bytes and the central directory: reading needs `&mut`. + let mut archive = self.archive.clone(); + let mut entry = archive.by_index_raw(index).map_err(invalid)?; + let (method, size) = (entry.compression(), entry.size() as usize); + let mut raw = Vec::with_capacity(entry.compressed_size() as usize); + entry.read_to_end(&mut raw)?; + match method { + CompressionMethod::Stored => Ok(raw), + CompressionMethod::Deflated => { + miniz_oxide::inflate::decompress_to_vec_with_limit(&raw, size.max(1) * 2 + 1024) + .map_err(|_| invalid("corrupt deflate stream")) } _ => Err(invalid("unsupported zip compression")), } @@ -163,10 +114,97 @@ mod tests { } #[test] fn reads_stored_and_deflated_entries() { + // Its CRCs are zero: Java's ZipFile doesn't check them either. let zip = Zip::from_bytes(archive()).unwrap(); assert_eq!(zip.names().collect::>(), ["a/b.txt", "pack.mcmeta"]); assert_eq!(zip.read("pack.mcmeta").unwrap(), b"{\"pack\":{}}"); assert_eq!(zip.read("a/b.txt").unwrap(), b"hello hello hello hello"); assert!(zip.read("missing").is_err()); } + + /// A pack as zip tools write it: folder entries, a nested pack root, + /// stored and deflated files, and a Windows-style name. + fn written() -> Vec { + use std::io::Write; + use zip::write::SimpleFileOptions; + let mut w = zip::ZipWriter::new(Cursor::new(Vec::new())); + let stored = SimpleFileOptions::default().compression_method(CompressionMethod::Stored); + let deflated = SimpleFileOptions::default().compression_method(CompressionMethod::Deflated); + w.add_directory("Pack-main/", stored).unwrap(); + w.add_directory("Pack-main/assets/minecraft/", stored) + .unwrap(); + w.start_file("Pack-main/pack.mcmeta", stored).unwrap(); + w.write_all(b"{\"pack\":{\"pack_format\":64}}").unwrap(); + w.start_file("Pack-main/assets/minecraft/lang/en_us.json", deflated) + .unwrap(); + w.write_all("{\"a\":\"b\"}".repeat(100).as_bytes()).unwrap(); + w.start_file( + "Pack-main\\assets\\minecraft\\texts\\splashes.txt", + deflated, + ) + .unwrap(); + w.write_all(b"Also try Horizon!").unwrap(); + w.finish().unwrap().into_inner() + } + + #[test] + fn reads_nested_folders_and_skips_folder_entries() { + let zip = Zip::from_bytes(written()).unwrap(); + assert_eq!( + zip.names().collect::>(), + [ + "Pack-main/assets/minecraft/lang/en_us.json", + "Pack-main/assets/minecraft/texts/splashes.txt", + "Pack-main/pack.mcmeta", + ] + ); + assert!(zip.contains("Pack-main/pack.mcmeta")); + assert!(!zip.contains("Pack-main/")); + assert_eq!( + zip.read("Pack-main/assets/minecraft/lang/en_us.json") + .unwrap(), + "{\"a\":\"b\"}".repeat(100).as_bytes() + ); + assert_eq!( + zip.read("Pack-main/assets/minecraft/texts/splashes.txt") + .unwrap(), + b"Also try Horizon!" + ); + } + + #[test] + fn a_bad_entry_fails_alone() { + let mut bytes = written(); + // The deflated lang file's stream becomes a reserved block type. + let (start, len) = { + let mut archive = ZipArchive::new(Cursor::new(bytes.as_slice())).unwrap(); + let index = archive + .index_for_name("Pack-main/assets/minecraft/lang/en_us.json") + .unwrap(); + let entry = archive.by_index_raw(index).unwrap(); + ( + entry.data_start() as usize, + entry.compressed_size() as usize, + ) + }; + bytes[start..start + len].fill(0xff); + let zip = Zip::from_bytes(bytes).unwrap(); + assert!( + zip.read("Pack-main/assets/minecraft/lang/en_us.json") + .is_err() + ); + assert_eq!( + zip.read("Pack-main/pack.mcmeta").unwrap(), + b"{\"pack\":{\"pack_format\":64}}" + ); + } + + #[test] + fn refuses_what_is_not_a_zip() { + assert!(Zip::from_bytes(b"PK not really".to_vec()).is_err()); + assert!(Zip::from_bytes(Vec::new()).is_err()); + let mut cut = written(); + cut.truncate(cut.len() / 2); + assert!(Zip::from_bytes(cut).is_err()); + } } From 2eb699228a0064182aa9a8e8da4412c6cd1b6a4e Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:18:44 +0200 Subject: [PATCH 34/39] AGENTS.md and docs/architecture.md: a map of the code for agents AGENTS.md (CLAUDE.md links to it): the crates and what each owns, how to build and test fast, where each subsystem of horizon-client lives, how Game is organised, the two single-player paths and how to test both, the vanilla reference, and the rules that bite. docs/architecture.md: feature -> files -> checks for the 40 areas changed most in the last 30 days. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 175 +++++++++++++++++++++++++++++++++++++++++++ CLAUDE.md | 1 + docs/README.md | 1 + docs/architecture.md | 57 ++++++++++++++ 4 files changed, 234 insertions(+) create mode 100644 AGENTS.md create mode 120000 CLAUDE.md create mode 100644 docs/architecture.md diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 000000000..d869aed2a --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,175 @@ +# Horizon: a map for agents + +Minecraft: Java Edition 26.3 rewritten in Rust (wgpu). Vanilla is the specification wherever it has the +feature. Read [CONTRIBUTING.md](CONTRIBUTING.md) once; this page tells you where things are. +Feature → files → harness for the most-changed areas: [docs/architecture.md](docs/architecture.md). + +## Rules that bite + +- **Never commit Mojang's files**: game jars, assets, decompiled source, prepared resources (`.local/`). + `tools/check_public_tree.py` rejects them; `git config core.hooksPath .githooks` runs it on commit. +- **Keep your runs away from real player data.** Any game or launcher you start gets fresh `mktemp -d` + folders in `HORIZON_HOME` (the game's data), `HORIZON_LAUNCHER_HOME`, `XDG_DATA_HOME` and + `XDG_CONFIG_HOME`, plus `HORIZON_LAUNCHER_NO_SELF_UPDATE=1` and `HORIZON_NO_AUDIO=1`. Never write to + `~/.local/share/{applications,horizon,horizon-launcher}` or `~/.local/bin`. +- **Mark what an agent posts.** A PR body starts with + `> 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself.` (issues: "Filed by", reviews: + "Review by"); every comment starts with `🤖 Agent:`. Never write as Ludovic. +- **Attribution.** Commits end with `Co-Authored-By: Claude `; PR bodies end + with `🤖 Generated with [Claude Code](https://claude.com/claude-code)`. +- Everything players run is Rust. Python/Java only for dev tools and harness drivers in `tools/`. +- Horizon-only features follow [docs/design-system.md](docs/design-system.md) and are off by default where + they would change the vanilla look. +- Small PRs (< ~500 lines). Labels: `feature` (players get something new), `skip-release` (they see no + change), none for a fix, `release-now` for crash fixes. Every merge to main ships + ([docs/release-flow.md](docs/release-flow.md)). +- Contributors first: `gh pr list --state open --search ` before you start; never duplicate theirs. + +## Crates + +Game workspace (root `Cargo.toml`); `launcher/` and `crates/horizon-telemetry-server` are separate workspaces. + +| Crate | Owns | +|---|---| +| `horizon-client` | The game: a lib (`src/lib.rs`, ~190 flat modules) and the bin (`src/main.rs` → `game.rs` + `game/`), plus `horizon-ctl` (`src/bin/horizon-ctl/`). Also the dedicated server (`--dedicated`) | +| `horizon-world` | Vanilla world data with no gameplay loop: NBT, regions, chunks, `datafix/`, `worldgen/`, `level/` (server-side block behaviour, ticks, redstone), entity physics, items, effects, food, damage | +| `horizon-core` | Simulation primitives: Java `Random`s, `jmath` (HotSpot-exact `Math`), positions, noise, density, paletted storage, `version.rs` (the pinned version) | +| `horizon-protocol` | Network protocol 777: codecs, framing, encryption, `packets/{handshake,status,login,configuration,play}.rs`, `entity_data.rs` | +| `horizon-assets` | Resource decoding: block-state and item models, baking, atlases | +| `horizon-prepare` | Builds `.local/horizon-client/26.3` from the player's own Minecraft install (runs the Java exporters) | +| `horizon-server` | `horizon-server`: `server.toml`, CLI, supervisor of the headless game ([docs/hosting.md](docs/hosting.md)) | +| `horizon-server-files` | `server.properties`, eula, ops/whitelist/bans, usercache | +| `horizon-friends`, `horizon-relay` | Play with Friends: invite codes, encrypted tunnel, the relay ([docs/play-with-friends.md](docs/play-with-friends.md)) | +| `horizon-shaderpack` | OptiFine/Iris packs: preprocessor, GLSL → WGSL ([docs/shader-packs.md](docs/shader-packs.md)) | +| `horizon-mods`, `horizon-mod-sdk`, `cargo-horizon-mod` | Sandboxed WebAssembly mods: runtime, SDK + WIT, `cargo horizon-mod` ([docs/mods.md](docs/mods.md)) | +| `horizon-port-pack` | Porting Java modpacks ([docs/modpacks.md](docs/modpacks.md)) | +| `horizon-theme` | Design tokens and fonts for the launcher, the game's own menus and the website | +| `horizon-telemetry`, `horizon-telemetry-server`, `horizon-bench` | Opt-in data collection, its ingest server, the benchmark report format | +| `horizon-dualsense`, `horizon-threads`, `horizon-release-sign`, `horizon-bots`, `voxel-kernels`, `xtask` | DualSense output, thread priorities, signed release checksums, load-test bots, occupancy kernels, repo tasks | +| `launcher/` | GPUI launcher: `main.rs` (UI), `backend.rs` (installs, updates), `cli.rs`, `mcp.rs`, `*_page.rs` | + +Other top-level folders: `tools/` (harnesses and dev scripts), `tests/` (Python unit tests: `python3 -m +unittest discover -s tests`), `mods/horizon/src/test/java/dev/horizonlab/parity/` (Java oracles), +`website/` (playhorizon.dev; its paths are relative to `website/`), `docs/`, `.github/workflows/`. + +## Build and test fast + +```sh +python3 tools/pr_ready.py # the self-check: fmt, clippy -D warnings, touched crates' tests, harnesses; paste its block in the PR +python3 tools/pr_ready.py --quick # same without harnesses +cargo test -p horizon-client --lib --bins --tests # skips the 104 examples (pr_ready does this); `--doc` separately +cargo build --profile fast -p horizon-client # optimized, quick to link: use it to play or run lan_e2e +python3 tools/parity.py --changed origin/main # the harnesses your branch can affect (CI's parity job) +python3 tools/parity.py --changed-files paths.txt --list # which harnesses cover these files, without running them +python3 tools/parity.py mob_ai sculk # named harnesses +python3 tools/lan_e2e.py --check chest # two real games over LAN (host in-frame); --server-thread for the host on its server thread +python3 tools/agent_control_e2e.py # horizon-ctl / MCP end to end +``` + +- Harnesses and the game need the vanilla data in `.local/` (`minecraft/`, `horizon-client/26.3`, `datagen/`, + `tools/`): see [docs/parity-workflow.md](docs/parity-workflow.md). Without it, pr_ready lists the + harnesses for CI. +- A harness = `tools/test__parity.py` (324 of them) + a Java oracle (`mods/horizon/src/test/java/dev/horizonlab/parity/Vanilla*Oracle.java`) + + a Rust probe: `crates//examples/_{probe,fixture}.rs` (horizon-world 175, horizon-client 104), + or the game binary with a fixture env var (`game/dispense_fixture.rs`, `game/sculk_fixture.rs`, + `game/command_harness.rs`, `game/net_harness.rs`). Copy a nearby harness for a new one. +- Probes build into `.local/parity-target/`; harnesses find binaries via `lab.cargo_target()`, never `target/`. +- What a change reruns (of 329 harnesses, measured 2026-10-23): a file of the game binary (`game.rs`, `game/`) + ~25, a file of the client lib ~103, of `horizon-world` ~103–109, of `horizon-core` ~119. Plan for it. +- Rust integration tests: `crates/horizon-client/tests/` (`lan_bot.rs`, `generation_saves.rs`, `streaming_meshes.rs`). +- **macOS:** `cargo build/test --workspace` may fail to link the two wasm guest cdylibs (undefined + `horizon:mods/events` symbols) until #1446 lands: add `--exclude coords-hud --exclude horizon-mods-test-guest`. + Their real check is `cargo test -p horizon-mods` (builds them for `wasm32-unknown-unknown`). +- Linux needs `libasound2-dev libudev-dev`; `--no-default-features` drops audio and controllers (the server build). +- Run and drive the game (starts the `horizon-client` beside `horizon-ctl`; other arguments go to the game): + `target/fast/horizon-ctl --socket $PWD/ctl.sock launch --world W --creative --offscreen --wait --resources $PWD/.local/horizon-client/26.3`, + then `horizon-ctl --socket … state|keys|turn|command|screenshot|quit` + ([docs/horizon-ctl-commands.md](docs/horizon-ctl-commands.md)). + +## Vanilla reference + +- Decompiled sources: `.local/minecraft/26.3/sources/{client,server}/` (`python3 tools/lab.py decompile`). +- Bytecode queries (callers through lambdas, every overload): + `python3 tools/rea_vanilla.py method net.minecraft.world.level.block.DispenserBlock dispenseFrom` + ([docs/decompilation-status.md](docs/decompilation-status.md)). +- A harness runs the oracle against the real 26.3 jars and the Rust probe on the same inputs, then compares + their JSON. Oracle runs are cached by `tools/parity_shim.py`; `--fresh` reruns them. +- Mirror vanilla's `Math` calls with `horizon_core::jmath`, and vanilla's RNG with `horizon_core::random`: + random draws must happen in vanilla's order. + +## horizon-client: where things live + +`L:` = lib (`crates/horizon-client/src/`), `G:` = bin (`crates/horizon-client/src/game/`). The lib can't see +`Game`; bin code reaches the lib as `horizon_client::`. + +| Subsystem | Files | +|---|---| +| Game loop, tick, frame | `game.rs`: `fn run`, `impl ApplicationHandler for Game` (`window_event`, `about_to_wait`), `fn tick`, `fn tick_world`, `fn render`; L: `clock.rs`, `window.rs`, `spans.rs` | +| Rendering | L: `gfx.rs` (`Renderer::new`, `Renderer::render`), `gfx/` (cull, occlusion, translucency, upload, timing, lod, weather, stars, end_sky), `*.wgsl`, `particles.rs`, `block_entity_render.rs`, `item_render.rs`, `fog.rs`, `clouds.rs`, `camera.rs`, `first_person.rs`; G: `render_tick.rs`, `item_draw.rs` | +| Chunks, streaming, light | L: `streaming.rs` (worker pool: generate, light, mesh), `world.rs`, `chunk.rs`, `chunk_map.rs`, `light.rs`, `mesher.rs` + `mesher/`, `vanillagen.rs` (binds horizon-world's generator), `lod.rs` + `lod/`, `ticket_levels.rs`; G: `world_loading.rs`, `postprocess.rs`; game.rs `fn update_world` | +| World generation | `crates/horizon-world/src/worldgen/` (`generator*.rs`, `feature_graph.rs`, `structure/`, `carver/`) | +| Block behaviour | `crates/horizon-world/src/level/` (`mod.rs`, `tick.rs`, `hooks.rs`, `redstone.rs`, `place.rs`, `interact.rs`); game.rs `fn tick_blocks`, `fn process_level_events` | +| Mobs | L: `mobs.rs` (types), `mobs/` (`ai.rs`, `goals.rs`, `brain.rs`, `*_brain.rs`, `nav.rs`, `path.rs`, `species.rs`, `spawning.rs`, `render.rs`, `anim.rs`); G: `creatures.rs` (ticking, spawning, mob events), one file per mob family (`dragon.rs`, `wither.rs`, `villagers.rs`, `piglins.rs`, `golems.rs`...), `natural_spawning.rs` | +| Other entities | L: `entities.rs`, `projectiles.rs`, `arrows.rs`; G: `boats.rs`, `minecarts.rs`, `mounts.rs`, `armor_stands.rs`, `paintings.rs`, `frames.rs`, `fireworks.rs`, `tnt.rs`, `explosion.rs` | +| Player actions | game.rs (`fn use_hand`, `fn use_item`, `fn attack`, `fn tick_mining`, `fn break_block`); G: `hand.rs`, `combat.rs`, `consume.rs`, `potions.rs`, `shield.rs`, `crossbow.rs`, `trident.rs`, `fishing.rs`; L: `motion.rs` + `motion/` (movement), `collisions.rs`, `items.rs`, `item_data.rs` | +| Inventory and containers | L: `menu.rs` (`AbstractContainerMenu.clicked`), `container_screen.rs`, `containers.rs`, `creative.rs`, `crafting.rs`, `recipe_book*.rs`, `stations.rs`, `merchant.rs`; game.rs `fn menu_ops`, `fn click_inventory`; G: `stations.rs`, `trading.rs`, `recipe_book.rs`, `machines.rs` | +| Commands | parse: L: `command/` (`args.rs`, `selector.rs`, `tree_*.rs`, `vanilla.rs`); run: G: `commands.rs` (dispatch) + `commands/*.rs` (`admin`, `execute`, `items`, `players`, `world_cmds`...); L: `scoreboard_cmd.rs`, `data_cmd.rs`, `nbt_path.rs`; G: `functions.rs`, `command_blocks.rs` | +| UI screens | `Screen` enum in game.rs; L: `title.rs` + `title/`, `options_ui.rs`, `multiplayer.rs`, `pack_screen.rs`, `graphics_ui.rs`, `screen_draw.rs`, `ui.rs` (widgets), `hud.rs`, `hud_layout.rs`, `hud_overlays.rs`, `chat.rs`, `tab_list.rs`; G: `startup_menu.rs`, `screens.rs` (pause, death), `options.rs`, `lan_screen.rs`, `chat.rs`, `sign_edit.rs`, `books.rs` | +| Multiplayer client | L: `net.rs` (network thread); G: `remote.rs` (`fn handle_packet`, `fn tick_remote`), `remote_*.rs` (mob data, objects, projectiles, vehicles, stations) | +| LAN host / server side | L: `lan/` (`conn.rs` handshake + login threads, `hub.rs` + `hub/` play state, `chunks.rs`, `entity.rs`, `tracking.rs`, `guest.rs`, `playerdata.rs`); G: `lan.rs` (`LanHost`), `lan_actions.rs` (`fn as_guest`, `fn tick_guests`), `lan_*.rs` per feature, `lan_entities.rs` (tracked entities) | +| Integrated server | G: `integrated.rs` (the server thread), `levels.rs` (one level per dimension, `fn tick_server`), `server_travel.rs` | +| Dedicated server | G: `dedicated.rs`, `dedicated/status.rs`; crates `horizon-server-files`, `horizon-server` | +| Save and load | L: `save.rs` (level.dat, regions, playerdata), `entity_nbt.rs`, `block_entity_nbt.rs`, `options_file.rs`; G: `saving.rs`, `persist.rs` (entities); `crates/horizon-world/src/{region.rs,nbt.rs,datafix/}` | +| Input | game.rs `fn window_event`, `fn key_pressed`, `fn play_mouse`; L: `keybinds.rs`, `keymapping.rs`, `key_names.rs`, `mouse.rs`, `input.rs`, `interaction_input.rs`, `gamepad.rs`, `osk.rs`, `clipboard.rs`; G: `pad.rs`, `pad_menu.rs` | +| Audio | L: `audio.rs` (mixer), `sound_engine.rs`, `sound_events.rs`, `music.rs`, `ambience.rs`; G: `music.rs`, `subtitle_hud.rs` | +| Resource packs | L: `packs.rs` + `packs/`, `pack_screen.rs`, `atlas_sources.rs`, `sprites.rs`, `mipmap.rs`, `model_library.rs`, `font.rs`, `lang.rs`; crate `horizon-assets`; G: `packs.rs` | +| Shader packs | L: `shader_pack.rs` (Horizon WGSL packs), `java_pack.rs` + crate `horizon-shaderpack` (Iris/OptiFine), `gfx/pack_stages.rs`, `gfx/java_stages.rs`; G: `shader_packs.rs` | +| Agent control | L: `control.rs` (socket), `bin/horizon-ctl/`; G: `control.rs`, `agent.rs`, `events.rs`, `ui_control.rs` ([docs/agent-control.md](docs/agent-control.md)) | +| Mods | crate `horizon-mods`; G: `mods.rs`; L: `mod_content.rs`, `mod_list.rs` | +| Horizon extras, settings | L: `extras.rs`, `graphics.rs`, `settings.rs`, `minimap.rs`; G: `extras.rs`, `tweaks.rs`, `options.rs` | + +## How `Game` is organised + +- `struct Game` (game.rs, ~290 fields) holds everything. Its methods are spread over ~180 files: game.rs keeps + the core (tick, render, input, inventory, world open/close) and each `game/.rs` adds an + `impl Game<'_>` block for one feature, with its own state types. Fields are private to the bin. +- Find a feature's code: `grep -rn "fn tick_\|fn _" crates/horizon-client/src/game*`; + the file is usually `game/s.rs`. Who calls it: grep the name in game.rs's `fn tick` / `fn tick_world`. +- Fields that say which mode runs: `remote` (this window is a client of a server: multiplayer, LAN guest, or + single player on its server thread), `lan` (`LanHost`: this world is open to the network), `integrated` + (the window's own server thread), `serving` (this `Game` *is* that server thread), `options.dedicated`. + `fn tick` returns `tick_remote()` when `remote` is set; nothing else of the local simulation runs. +- A server's players run through the same gameplay code as the local player: `as_guest` swaps the guest's + state (position, inventory, mode, mining, item use) into `Game` for one call (`game/lan_actions.rs`), and + `levels.rs` swaps whole dimensions in the same way. Changes reach clients through the block log, level + events and the entity tracker (`lan/tracking.rs`, `game/lan_entities.rs`). +- Shared read-only data: `Resources` (`res`: blocks, items, recipes, mob types, biomes) and `PackResources` + (`pack`), both in game.rs. + +## The two single-player paths + +1. **In-frame (default since #1428):** the window's `Game` ticks the world itself, one level that follows + the player (`fn enter_dimension`). This is what Stable players run and what harnesses test. +2. **Server thread (`--server-thread` or `HORIZON_SERVER_THREAD=1`):** `game/integrated.rs` runs a second + `Game` (`serving`) on a "Server thread" with one level per dimension; the window joins it as a client + through memory (`remote`), exactly as a LAN guest or a dedicated server's player does. Not at parity yet: + open `[server-thread]` issues list the gaps. + +LAN guests and dedicated servers always use the server code, so a fix there serves both. Test both paths +when you touch player-facing gameplay: + +```sh +cargo run --profile fast -p horizon-client -- --resources .local/horizon-client/26.3 --world T # in-frame +cargo run --profile fast -p horizon-client -- --resources .local/horizon-client/26.3 --world T --server-thread # server thread +python3 tools/lan_e2e.py --check # host in-frame, guest is a server client +python3 tools/lan_e2e.py --server-thread --check # host on its server thread (portals, levels, distance, lava need it) +python3 tools/parity.py dedicated_server server_entities server_player_damage +``` + +## Docs worth knowing + +[docs/parity-workflow.md](docs/parity-workflow.md) (runner, gate, shards, scopes) · +[docs/native-client.md](docs/native-client.md) (options, controls, horizon-ctl) · +[docs/server-levels.md](docs/server-levels.md) (levels per dimension) · +[docs/rust-parity.md](docs/rust-parity.md) (what is ported) · [docs/known-issues.md](docs/known-issues.md) · +[docs/release-flow.md](docs/release-flow.md) · [docs/README.md](docs/README.md) (index). diff --git a/CLAUDE.md b/CLAUDE.md new file mode 120000 index 000000000..47dc3e3d8 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1 @@ +AGENTS.md \ No newline at end of file diff --git a/docs/README.md b/docs/README.md index b4398ebfc..2e06b6e89 100644 --- a/docs/README.md +++ b/docs/README.md @@ -4,6 +4,7 @@ To play, use [the launcher](../launcher/README.md); the project site is [playhor ## Build and run +- [AGENTS.md](../AGENTS.md) and [Architecture](architecture.md): where each part of the code lives, how `Game` is organised, which harness checks which area. - [Build from source](../README.md#build-from-source): your own copy of the game, resource preparation, `cargo run`. - [Native client](native-client.md): options, controls, the dedicated server, `horizon-ctl` scripting, packaging, architecture. - [Host a server](hosting.md): install `horizon-server`, prepare the game files, first run, `server.toml`, the commands, the web panel, backups, services and Docker; what works today and what doesn't yet. diff --git a/docs/architecture.md b/docs/architecture.md new file mode 100644 index 000000000..99e9b630e --- /dev/null +++ b/docs/architecture.md @@ -0,0 +1,57 @@ +# Architecture: feature → files → checks + +The 40 areas changed most often in the 30 days to 2026-10-23 (commits on main touching the area; `game.rs` is +counted only for the game loop because nearly every gameplay change touches it). The crate map, how `Game` is +organised and how to build and test are in [AGENTS.md](../AGENTS.md). + +Paths: `L/` = `crates/horizon-client/src/`, `G/` = `crates/horizon-client/src/game/`, `W/` = +`crates/horizon-world/src/`. Checks: a bare name is a harness (`python3 tools/parity.py `, script +`tools/test__parity.py`); `lan_e2e ` is `python3 tools/lan_e2e.py --check `. + +For the full set of harnesses a file can affect, ask the runner: +`printf '%s\n' > /tmp/p.txt && python3 tools/parity.py --changed-files /tmp/p.txt --list`. +Any file of the game binary (`game.rs`, `game/`) selects the ~25 harnesses that run the binary; a client lib +file selects ~103. + +| # | Area (commits) | Files | Checks | +|---|---|---|---| +| 1 | Game loop, tick order, frame pacing (554) | `L/game.rs` (`fn tick`, `fn tick_world`, `fn render`, `about_to_wait`), `L/clock.rs`, `L/window.rs`, `L/spans.rs` | game_loop, frame_limiter, interaction_input, tick | +| 2 | Mobs in the game loop, spawning (166) | `G/creatures.rs`, `G/natural_spawning.rs`, `L/mobs/spawning.rs`, `L/mobs/spawn_rules.rs`, `L/mobs/spawn_selection.rs`, `L/mobs/finalize.rs`, `G/spawner.rs`, `G/trial_spawner.rs` | natural_spawn, spawn_rules, finalize_spawn, spawner, trial_spawner, mob_ai | +| 3 | World save and load (163) | `L/save.rs`, `G/saving.rs`, `G/persist.rs` (entities), `L/entity_nbt.rs`, `L/block_entity_nbt.rs`, `W/region.rs`, `W/nbt.rs` | save_roundtrip, world_reopen, world, datafix | +| 4 | World generation (153) | `W/worldgen.rs`, `W/worldgen/` (`generator*.rs`, `feature_graph.rs`, `structure/`, `placement/`), `L/vanillagen.rs` | chunk_generation, worldgen, server_world_gen, world_spawn, feature_graph, feature_placement, decoration and the other worldgen harnesses | +| 5 | Multiplayer client, packets in (145) | `G/remote.rs` (`fn handle_packet`, `fn tick_remote`), `G/remote_*.rs`, `L/net.rs` | dedicated_server, server_entities, lan_e2e (all) | +| 6 | Agent control, horizon-ctl (145) | `L/control.rs`, `L/bin/horizon-ctl/`, `G/control.rs`, `G/agent.rs`, `G/events.rs`, `G/ui_control.rs` | `python3 tools/agent_control_e2e.py`; `cargo test -p horizon-client --bin horizon-ctl` | +| 7 | CI and release (134) | `.github/workflows/` (`ci.yml`, `parity.yml`, `release.yml`, `launcher.yml`), `tools/ci/`, `tools/parity.py`, `tools/pr_ready.py` | `python3 -m unittest discover -s tests`; actionlint (`.github/actionlint.yaml`) | +| 8 | LAN host: connections, play state, tracking (133) | `L/lan/` (`conn.rs`, `hub.rs` + `hub/`, `chunks.rs`, `entity.rs`, `tracking.rs`, `guest.rs`, `playerdata.rs`), `G/lan.rs` | lan, lan_e2e (all), `cargo test -p horizon-client --test lan_bot` | +| 9 | Renderer core (132) | `L/gfx.rs` (`Renderer::new`, `Renderer::render`), `L/gfx/` (cull, occlusion, translucency, upload, timing, lod) | client_graphics, culling_rays, translucency; `horizon-client --benchmark standard` | +| 10 | Launcher (124) | `launcher/src/` (`main.rs`, `backend.rs`, `cli.rs`, `mcp.rs`, `servers_page.rs`) | `cd launcher && cargo test`; `python3 tools/launcher_mcp_e2e.py` | +| 11 | Mob types and species (117) | `L/mobs.rs`, `L/mobs/species.rs`, `L/mobs/data.rs`, `L/mobs/living.rs`, `L/mobs/drops.rs`, `L/mobs/gear.rs` | mob_attributes, entity_attribute, entity_death_loot, mob_ai | +| 12 | Guests' actions on a server (98) | `G/lan_actions.rs` (`fn as_guest`, `fn tick_guests`), `G/lan_{attributes,pvp,chat,crafting,stations,trading,mount_menu,waypoints}.rs` | server_player_combat, server_player_damage, server_player_targets, lan_e2e (all) | +| 13 | Modpack porting, mod content (97) | `crates/horizon-port-pack/`, `L/mod_content.rs`, `tools/port_*.py`, `packs/` | `cargo test -p horizon-port-pack`; `python3 tools/mod_port_harness.py` | +| 14 | Mob AI (96) | `L/mobs/ai.rs`, `goals.rs`, `brain.rs`, `*_brain.rs`, `nav.rs`, `path.rs`, `control.rs` | mob_ai (probe `examples/mob_ai_probe.rs`; known failure on main: drowned_*, #290) | +| 15 | Items and item use (96) | `L/items.rs`, `L/item_data.rs`, `G/hand.rs`, `G/consume.rs`, `G/potions.rs`, `L/effects.rs`; game.rs `fn use_hand`, `fn use_item` | use_item_on, item_use, item_use_lifecycle, consumable, consume_effect, status_effect | +| 16 | Options and settings screens (93) | `L/options_ui.rs`, `G/options.rs`, `L/settings.rs`, `L/options_file.rs`, `L/graphics_ui.rs`, `L/graphics.rs` | options, toggle_keys | +| 17 | HUD, chat, tab list (93) | `L/hud.rs`, `L/hud_layout.rs`, `L/hud_overlays.rs`, `L/chat.rs`, `G/chat.rs`, `L/tab_list.rs`, `L/locator_bar.rs`, `G/locator_bar.rs` | hud, tab_list, locator_bar, chat_screen, secure_chat | +| 18 | Command execution (88) | `G/commands.rs`, `G/commands/*.rs`, `L/scoreboard_cmd.rs`, `L/data_cmd.rs`, `G/functions.rs` | command_exec (corpus `tools/command_exec_corpus.py`), scoreboard, server_ops_feedback, lan_e2e commands | +| 19 | Chunk streaming, light, tickets (85) | `L/streaming.rs`, `L/world.rs`, `L/chunk.rs`, `L/chunk_map.rs`, `L/light.rs`, `L/ticket_levels.rs`, `L/tick_chunk_membership.rs`, `G/world_loading.rs` | light, chunk_status, random_tick_sections; `cargo test -p horizon-client --test streaming_meshes` | +| 20 | Mob models and animation (78) | `L/mobs/render.rs`, `L/mobs/anim.rs`, `L/mobs/body_anim.rs`, `L/mobs/keyframes.rs`, `L/entity.wgsl` | entity_model | +| 21 | Block behaviour, server side (78) | `W/level/` (`mod.rs`, `tick.rs`, `hooks.rs`, `data.rs`, `redstone.rs`, `place.rs`), `G/block_entity_ticks.rs`, `G/moving_pistons.rs` | block_update, redstone, random_tick, simple_block, piston_movement, block_entity_tickers | +| 22 | Player movement and collision (75) | `L/motion.rs` + `L/motion/`, `L/collisions.rs`, `W/entity_move.rs`, `W/player_travel.rs`, `W/collision.rs` | collision, entity_move, player_travel, living_motion, climbing, swimming; `python3 tools/test_native_scene_motion.py` | +| 23 | Resource packs and models (65) | `L/packs.rs` + `L/packs/`, `L/pack_screen.rs`, `L/atlas_sources.rs`, `L/sprites.rs`, `L/model_library.rs`, `G/packs.rs`, `crates/horizon-assets/` | resource_pack, pack_selection, pack_screen, model_resolution, item_model, mipmap | +| 24 | Container menus and inventory (61) | `L/menu.rs`, `L/container_screen.rs`, `L/containers.rs`, `L/creative.rs`; game.rs `fn menu_ops`, `fn click_inventory` | menu_click, container_screen, creative, lan_e2e containers clicks | +| 25 | Workstations (60) | `L/stations.rs`, `G/stations.rs`, `L/enchant.rs`, `L/brewing.rs` | enchanting, screen_layout, lan_e2e brewing loom stonecutter grindstone anvil smithing | +| 26 | Crafting and the recipe book (59) | `L/crafting.rs`, `L/recipe_book.rs`, `L/recipe_book_screen.rs`, `G/recipe_book.rs`, `G/machines.rs`, `G/machines/crafter.rs` | recipe, recipe_book, recipe_book_view, place_recipe, ghost_recipe, crafter | +| 27 | Vehicles and mounts (54) | `G/boats.rs`, `G/minecarts.rs`, `G/mounts.rs`, `G/mount_menu.rs`, `G/llamas.rs`, `G/passengers.rs` | server_entities, lan_e2e boats minecarts riding mounts | +| 28 | Villagers, trading, raids (50) | `L/mobs/villager*.rs`, `L/merchant.rs`, `G/trading.rs`, `G/villagers.rs`, `L/raid.rs`, `G/raids.rs`, `L/gossip.rs` | trading, raid, mob_ai | +| 29 | Title, worlds, startup (49) | `L/title.rs` + `L/title/`, `G/startup_menu.rs`, `L/title_edition.rs` | world_options; `cargo test -p horizon-client --lib title` | +| 30 | Dedicated server, horizon-server (48) | `G/dedicated.rs`, `G/dedicated/status.rs`, `crates/horizon-server/`, `crates/horizon-server-files/` | dedicated_server, server_files, user_cache, online_mode, server_ops_feedback | +| 31 | Particles and client effects (46) | `L/particles.rs`, `G/render_tick.rs`, `G/level_particles.rs`, `L/animate_tick.rs` | particle_providers, block_particles, entity_particles, emitter_particles, marker_particles | +| 32 | Bosses and the End (45) | `G/dragon.rs`, `G/wither.rs`, `G/end.rs` | server_dragon, end_podium, end_gateway | +| 33 | Protocol and packets (44) | `crates/horizon-protocol/src/` (`packets/play.rs`, `entity_data.rs`, `connection.rs`) | dedicated_server, online_mode, secure_chat; `cargo test -p horizon-protocol` | +| 34 | Multiplayer and LAN screens, friends (44) | `L/multiplayer.rs`, `G/lan_screen.rs`, `G/friends.rs`, `L/friends_screen.rs`, `crates/horizon-friends/`, `crates/horizon-relay/` | lan, world_options; `python3 tools/test_friends.py` | +| 35 | Dispensers (43) | `G/dispenser.rs`, `G/dispense_fixture.rs` (the harness side) | dispenser | +| 36 | Audio and music (38) | `L/audio.rs`, `L/sound_engine.rs`, `L/sound_events.rs`, `L/music.rs`, `G/music.rs`, `L/ambience.rs`, `G/subtitle_hud.rs` | sound_engine, sound_event, music, situational_music, ambient_sound, subtitle | +| 37 | Integrated server and levels (34) | `G/integrated.rs`, `G/levels.rs`, `G/server_travel.rs` | server_dimension_players, `lan_e2e --server-thread` (portals, levels, distance, lava) | +| 38 | Command parsing (34) | `L/command/` (`args.rs`, `selector.rs`, `tree_*.rs`, `vanilla.rs`) | command_parse (corpus `tools/command_parse_corpus.py`) | +| 39 | Benchmarks and telemetry (34) | `L/benchmark.rs`, `tools/perf_bench.py`, `L/telemetry.rs`, `crates/horizon-telemetry*/`, `crates/horizon-bench/` | `python3 -m unittest tests.test_perf_bench`; [benchmark.md](benchmark.md) | +| 40 | Explosions and TNT (31) | `G/explosion.rs`, `G/tnt.rs` | lan_e2e blast; no dedicated harness yet | From 22ad65611e85746cc00ad1f5ca25725f40b42d8f Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 15:39:11 +0200 Subject: [PATCH 35/39] High-resolution resource packs no longer crash the game at launch The shared GUI atlas was always 4096 texels wide, so Faithful 64x needed 4096x32768: past the 16384 texels a side GPUs allow, and wgpu panicked in create_texture at every launch until the pack was removed (#1442). - The atlas widens (4096, 8192, 16384) until it fits within 16384 a side, so Faithful 64x loads. Its height rounds up to a multiple of 1024 instead of a power of two: the default resources' atlas drops from 4096x8192 to 4096x5120, Faithful 32x's from 4096x16384 to 4096x12288. - Textures that still don't fit are an error, not a GPU panic. At startup it rolls the packs back as Minecraft.rollbackResourcePacks does: the game starts with its own resources, and the selection is cleared and saved (clearResourcePacksOnError), so the next launch doesn't fail again. The existing "Resource reload failed" screen shows the reason. - The renderer refuses an atlas larger than the device allows with an error instead of a wgpu validation panic. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 20 ++++++- crates/horizon-client/src/gfx.rs | 7 +++ crates/horizon-client/src/hud.rs | 98 ++++++++++++++++++++++++++----- 3 files changed, 108 insertions(+), 17 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index dc6be85b7..df9400e4b 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -12108,7 +12108,20 @@ fn load_resources( // loads; the synchronous launch instead carries it in `Loaded`. let mut loaded_gui = None; let mut ui_ready = false; - if let Ok(ui) = UiResources::load(&root) { + let mut pack_error = None; + let ui = match UiResources::load(&root) { + Ok(ui) => Some(ui), + // Minecraft.rollbackResourcePacks: the packs' textures don't fit, start + // with the game's own resources. + Err(e) if root != vanilla_root => { + log::warn!("Horizon: resource packs failed to load ({e}); using the default resources"); + pack_error = Some(e.to_string()); + root = vanilla_root.to_path_buf(); + None + } + Err(_) => None, + }; + if let Some(ui) = ui { if menu { horizon_client::telemetry::mark_ui_ready(); log::info!( @@ -12122,7 +12135,6 @@ fn load_resources( ui_ready = true; } progress.set(0); - let mut pack_error = None; let (assets, render) = match Assets::load(&root).and_then(|assets| { let render = assets.reload(&root)?; Ok((assets, render)) @@ -12560,6 +12572,10 @@ fn start_game<'a>( game.rest_loader = rest_loader; game.ui.vanilla_root = vanilla_root; if let Some(error) = pack_error { + // Minecraft.clearResourcePacksOnError: the selection is cleared and + // saved, so the next launch starts without the packs. + game.settings.resource_packs.clear(); + game.settings.save(&Game::options_path()); game.open_options_at(true, horizon_client::options_ui::Page::Packs); game.ui.options.resource_error = Some(error); } diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 82a9603e2..adeaf1c35 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -1491,6 +1491,13 @@ impl Renderer { mapped_at_creation: false, }); let (atlas_pixels, atlas_width, atlas_height) = hud_atlas; + let limit = device.limits().max_texture_dimension_2d; + if atlas_width.max(atlas_height) > limit { + return Err(format!( + "the shared atlas is {atlas_width}x{atlas_height}; this GPU allows {limit}x{limit}" + ) + .into()); + } let atlas = device.create_texture_with_data( &queue, &wgpu::TextureDescriptor { diff --git a/crates/horizon-client/src/hud.rs b/crates/horizon-client/src/hud.rs index 82e606947..1d0ab91b9 100644 --- a/crates/horizon-client/src/hud.rs +++ b/crates/horizon-client/src/hud.rs @@ -54,6 +54,9 @@ pub struct HudAtlas { } /// Atlas rows reserved for glyphs rasterized at run time. pub const GLYPH_ROWS: u32 = 512; +/// The largest atlas side: the 2D texture limit of the GPUs Horizon runs on +/// (Apple silicon, RADV and Intel allow 16384; NVIDIA 32768). +pub const MAX_ATLAS_SIZE: u32 = 16384; /// 128x128 atlas slots for map textures. A lobby paints its signs as walls of /// framed maps, so the pool holds a wall; too few slots re-upload every map /// every frame. 26.3's `MapTextureManager` keeps a texture per map instead. @@ -269,6 +272,43 @@ impl HudAtlas { atlas.pixels = Vec::new(); Ok(atlas) } + /// Shelf placements of `images` (tallest first) in the narrowest atlas + /// width from 4096 up that keeps the atlas, glyph rows included, within + /// `max` texels a side; and the first row free for glyphs. High-resolution + /// packs fill a 4096-wide atlas past what GPUs allow (Faithful 64x needs + /// 32768 rows); vanilla's stitcher fails the reload when its atlases don't + /// fit, and the packs are then turned off (`Minecraft.clearResourcePacksOnError`). + fn layout(images: &[PackedImage], max: u32) -> Result<(u32, Vec<(u32, u32)>, u32)> { + let mut width = 4096.min(max); + loop { + let mut placements = Vec::with_capacity(images.len()); + let (mut x, mut y, mut row) = (0u32, 0u32, 0u32); + for (_, w, h, _) in images { + if x + w + 1 > width { + x = 0; + y += row + 1; + row = 0; + } + placements.push((x, y)); + x += w + 1; + row = row.max(*h); + } + let glyph_region = y + row + 1; + let wide_enough = images.iter().all(|i| i.1 < width); + if wide_enough && glyph_region + GLYPH_ROWS <= max { + return Ok((width, placements, glyph_region)); + } + if width >= max { + let texels: u64 = images.iter().map(|i| u64::from(i.1) * u64::from(i.2)).sum(); + return Err(format!( + "the textures ({} images, {texels} texels) do not fit a {max}x{max} atlas", + images.len() + ) + .into()); + } + width = (width * 2).min(max); + } + } /// Pack the textures under these folders of `textures/`, and for the /// `full` atlas the generated sprites and the run-time slots too. fn pack(root: &Path, folders: &[&str], full: bool) -> Result { @@ -351,21 +391,10 @@ impl HudAtlas { } } images.sort_by(|a, b| b.2.cmp(&a.2).then(b.1.cmp(&a.1))); - let width = 4096u32; - let mut placements = Vec::with_capacity(images.len()); - let (mut x, mut y, mut row) = (0u32, 0u32, 0u32); - for (_, w, h, _) in &images { - if x + w + 1 > width { - x = 0; - y += row + 1; - row = 0; - } - placements.push((x, y)); - x += w + 1; - row = row.max(*h); - } - let glyph_region = y + row + 1; - let height = (glyph_region + GLYPH_ROWS).next_power_of_two(); + let (width, placements, glyph_region) = Self::layout(&images, MAX_ATLAS_SIZE)?; + // A multiple of 1024 rather than a power of two: the default resources + // need 4096x5120 (8192 rows before), Faithful 32x 4096x12288 (16384). + let height = (glyph_region + GLYPH_ROWS).next_multiple_of(1024); let mut pixels = vec![0u8; (width * height * 4) as usize]; let mut sprites = HashMap::new(); for ((name, w, h, data), (px, py)) in images.iter().zip(placements) { @@ -1340,6 +1369,45 @@ impl HudRenderer { mod tests { use super::*; + /// `count` images of `w x h`; layout reads only their sizes. + fn sized(count: usize, w: u32, h: u32) -> Vec { + (0..count) + .map(|i| (format!("i{i}"), w, h, Vec::new())) + .collect() + } + fn assert_inside(images: &[PackedImage], width: u32, placements: &[(u32, u32)], free: u32) { + for ((_, w, h, _), (x, y)) in images.iter().zip(placements) { + assert!( + x + w <= width && y + h < free, + "{x},{y} {w}x{h} outside {width}x{free}" + ); + } + } + #[test] + fn the_default_textures_keep_a_4096_wide_atlas() { + let images = sized(400, 64, 64); + let (width, placements, free) = HudAtlas::layout(&images, MAX_ATLAS_SIZE).unwrap(); + assert_eq!(width, 4096); + assert_inside(&images, width, &placements, free); + } + #[test] + fn a_high_resolution_pack_widens_the_atlas_to_fit_the_gpu() { + // 34 rows of three 1024x512 GUI textures: 17442 rows at 4096 wide, + // past the 16384 GPUs allow (Faithful 64x needed 32768). + let images = sized(100, 1024, 512); + let (width, placements, free) = HudAtlas::layout(&images, MAX_ATLAS_SIZE).unwrap(); + assert_eq!(width, 8192); + assert!(free + GLYPH_ROWS <= MAX_ATLAS_SIZE); + assert_inside(&images, width, &placements, free); + } + #[test] + fn textures_that_cannot_fit_are_an_error_not_a_gpu_panic() { + let error = HudAtlas::layout(&sized(300, 1024, 1024), MAX_ATLAS_SIZE).unwrap_err(); + assert!(error.to_string().contains("do not fit a 16384x16384 atlas")); + // One image wider than the largest atlas. + assert!(HudAtlas::layout(&sized(1, 2048, 16), 2048).is_err()); + } + #[test] fn a_wrapped_border_repeats_the_opposite_edges() { // 2x2 texels a b / c d, one byte each in red. From 79ee670d7915a1ea4e7cdd4355053aa1a1ff513f Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 15:59:07 +0200 Subject: [PATCH 36/39] High-resolution resource packs draw the GUI at its size With Faithful 32x the inventory showed the top-left quarter of its background stretched over the screen, tooltips lost their background and toasts doubled in size (#1442): the GUI code drew every atlas sprite in texels, where vanilla draws in GUI units whatever the pack's resolution. - Atlas sprites carry their GUI size: the size of the default resources' texture of that name (what vanilla's `blit(..., 256, 256)` calls and sprite sizes assume), their texels only for what copies pixels (font sheets, fire frames). Textures only a pack has keep their own size. The texture size filter reads the GUI size too, so Faithful 64x's 1024x1024 container backgrounds are no longer dropped. - Nine-slice sprites follow `GuiGraphicsExtractor.blitNineSlicedSprite`: their `gui.scaling` width, height and borders (Faithful's tooltip background is 50x50 with a 10-pixel border), `stretch_inner`, the single-quad and three-part cases, and borders capped at half the drawn size by integer division. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/font.rs | 5 +- crates/horizon-client/src/game.rs | 10 +- crates/horizon-client/src/game/packs.rs | 2 +- crates/horizon-client/src/hud.rs | 128 +++++++-- crates/horizon-client/src/ui.rs | 329 ++++++++++++++++++------ 5 files changed, 378 insertions(+), 96 deletions(-) diff --git a/crates/horizon-client/src/font.rs b/crates/horizon-client/src/font.rs index c5e5760f3..a25568df5 100644 --- a/crates/horizon-client/src/font.rs +++ b/crates/horizon-client/src/font.rs @@ -271,7 +271,10 @@ fn load_bitmap( .flatten(); // The sheet: its size, and its pixels when they are not in the atlas. let (sw, sh, sheet) = match in_atlas { - Some(rect) => (rect.w, rect.h, None), + Some(rect) => { + let (w, h) = rect.texels(atlas); + (w, h, None) + } None => match crate::hud::decode(&root.join(resource_path(file, "textures", ""))) { Ok((w, h, rgba)) => (w, h, Some(Arc::new((w, rgba)))), Err(_) => { diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index df9400e4b..8d2d45a9e 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1021,9 +1021,11 @@ pub struct UiResources { pub panorama: Arc, } impl UiResources { - pub fn load(root: &std::path::Path) -> Result { + /// From a resource root layering packs over `defaults`, the game's own + /// resources (`root` itself when no pack is on). + pub fn load(root: &std::path::Path, defaults: &std::path::Path) -> Result { Ok(Self { - atlas: HudAtlas::build(root)?, + atlas: HudAtlas::build_over(root, defaults)?, panorama: Arc::new(horizon_client::gfx::panorama::Faces::load(root)), }) } @@ -12109,7 +12111,7 @@ fn load_resources( let mut loaded_gui = None; let mut ui_ready = false; let mut pack_error = None; - let ui = match UiResources::load(&root) { + let ui = match UiResources::load(&root, vanilla_root) { Ok(ui) => Some(ui), // Minecraft.rollbackResourcePacks: the packs' textures don't fit, start // with the game's own resources. @@ -12161,7 +12163,7 @@ fn load_resources( let render = Arc::new(render); // The menu bundle must come from the same root as the world bundle. if !ui_ready || root != requested_root { - let ui = UiResources::load(&root)?; + let ui = UiResources::load(&root, vanilla_root)?; if menu { horizon_client::telemetry::mark_ui_ready(); log::info!( diff --git a/crates/horizon-client/src/game/packs.rs b/crates/horizon-client/src/game/packs.rs index 94e61d948..ae2de00e6 100644 --- a/crates/horizon-client/src/game/packs.rs +++ b/crates/horizon-client/src/game/packs.rs @@ -133,7 +133,7 @@ impl Game<'_> { } fn reload_resources(&mut self, root: &Path) -> horizon_client::Result<()> { let render = std::sync::Arc::new(self.pack.assets.reload(root)?); - let mut atlas = HudAtlas::build(root)?; + let mut atlas = HudAtlas::build_over(root, &self.vanilla_root())?; atlas.font.uniform = self.settings.force_unicode; atlas.font.japanese = self.settings.japanese_glyphs; let gui = super::UiResources { diff --git a/crates/horizon-client/src/hud.rs b/crates/horizon-client/src/hud.rs index 1d0ab91b9..9d30ae0e6 100644 --- a/crates/horizon-client/src/hud.rs +++ b/crates/horizon-client/src/hud.rs @@ -9,6 +9,11 @@ use std::{ }; use wgpu::util::DeviceExt; +/// A sprite's atlas region, and its size in GUI pixels: the size of the +/// default resources' texture of that name, whatever the resolution of the +/// pack that replaced it (vanilla's GUI code draws in those units: `blit` +/// divides by the texture size it is given, sprites by their `gui.scaling` +/// size). Textures the default resources lack keep their own size. #[derive(Clone, Copy, Debug)] pub struct Rect { pub u0: f32, @@ -18,6 +23,15 @@ pub struct Rect { pub w: u32, pub h: u32, } +impl Rect { + /// The region's size in texels of an atlas this large. + pub fn texels(&self, (width, height): (u32, u32)) -> (u32, u32) { + ( + ((self.u1 - self.u0) * width as f32).round() as u32, + ((self.v1 - self.v0) * height as f32).round() as u32, + ) + } +} /// `TextureMetadataSection`'s `blur` and `clamp`: how vanilla samples a /// standalone texture (`ReloadableTexture.setSampler`: linear rather than /// nearest filtering, clamp-to-edge rather than repeat addressing). @@ -92,6 +106,15 @@ pub fn decode_bytes(data: &[u8]) -> Result<(u32, u32, Vec)> { } Ok((info.width, info.height, rgba)) } +/// A PNG's width and height, from its header alone. +fn png_size(path: &Path) -> Option<(u32, u32)> { + use std::io::Read; + let mut header = [0u8; 24]; + fs::File::open(path).ok()?.read_exact(&mut header).ok()?; + let field = |at: usize| u32::from_be_bytes(header[at..at + 4].try_into().unwrap_or_default()); + (header[..8] == *b"\x89PNG\r\n\x1a\n" && header[12..16] == *b"IHDR") + .then(|| (field(16), field(20))) +} fn walk(dir: &Path, out: &mut Vec) { let Ok(entries) = fs::read_dir(dir) else { return; @@ -204,8 +227,14 @@ impl HudAtlas { (g.rect, g.advance) } pub fn build(root: &Path) -> Result { + Self::build_over(root, root) + } + /// The atlas of a resource root that layers packs over `defaults` (the + /// game's own resources), whose textures give the sprites' GUI sizes. + pub fn build_over(root: &Path, defaults: &Path) -> Result { Self::pack( root, + defaults, &[ "gui", "item", @@ -268,7 +297,7 @@ impl HudAtlas { /// holding its textures. The pixels are dropped once the glyph widths are /// read, since nothing uploads them. pub fn fonts(root: &Path) -> Result { - let mut atlas = Self::pack(root, &["font"], false)?; + let mut atlas = Self::pack(root, root, &["font"], false)?; atlas.pixels = Vec::new(); Ok(atlas) } @@ -311,8 +340,10 @@ impl HudAtlas { } /// Pack the textures under these folders of `textures/`, and for the /// `full` atlas the generated sprites and the run-time slots too. - fn pack(root: &Path, folders: &[&str], full: bool) -> Result { + fn pack(root: &Path, defaults: &Path, folders: &[&str], full: bool) -> Result { let textures = root.join("assets/minecraft/textures"); + let default_textures = + (defaults != root).then(|| defaults.join("assets/minecraft/textures")); let mut files = Vec::new(); for folder in folders { walk(&textures.join(folder), &mut files); @@ -321,7 +352,8 @@ impl HudAtlas { let s = p.to_string_lossy(); !s.contains("title/background") && !s.contains("realms") && !s.contains("presets") }); - let decode_one = |path: &PathBuf| -> Option<(PackedImage, Option)> { + type Decoded = (PackedImage, Option, Option<(u32, u32)>); + let decode_one = |path: &PathBuf| -> Option { let name = path .strip_prefix(&textures) .ok()? @@ -329,18 +361,29 @@ impl HudAtlas { .to_string_lossy() .replace('\\', "/"); let (w, mut h, pixels) = decode(path).ok()?; - let is_frame_strip = (name.starts_with("block/") || name.starts_with("item/")) && h > w; - let pixels = if is_frame_strip { + // Animated strips keep their first frame. + let frames = name.starts_with("block/") || name.starts_with("item/"); + let pixels = if frames && h > w { h = w; pixels[..(w * w * 4) as usize].to_vec() } else { pixels }; + // The default texture's size, when a pack's differs from it. + let gui_size = default_textures + .as_ref() + .and_then(|d| png_size(&d.join(format!("{name}.png")))) + .map(|(dw, dh)| (dw, if frames { dh.min(dw) } else { dh })) + .filter(|&size| size != (w, h)); let flags = fs::read_to_string(path.with_extension("png.mcmeta")) .ok() .and_then(|json| TextureFlags::from_mcmeta(&json)); - (w <= 1024 && (h <= 512 || name.starts_with("font/"))) - .then_some(((name, w, h, pixels), flags)) + let (gw, gh) = gui_size.unwrap_or((w, h)); + (gw <= 1024 && (gh <= 512 || name.starts_with("font/"))).then_some(( + (name, w, h, pixels), + flags, + gui_size, + )) }; let threads = std::thread::available_parallelism().map_or(2, |n| n.get()); let chunk = files.len().div_ceil(threads).max(1); @@ -357,11 +400,15 @@ impl HudAtlas { .collect() }); let mut flags = HashMap::new(); + let mut gui_sizes = HashMap::new(); let mut images: Vec = Vec::with_capacity(decoded.len()); - for (image, image_flags) in decoded { + for (image, image_flags, gui_size) in decoded { if let Some(f) = image_flags { flags.insert(image.0.clone(), f); } + if let Some(size) = gui_size { + gui_sizes.insert(image.0.clone(), size); + } images.push(image); } if full { @@ -405,6 +452,7 @@ impl HudAtlas { } let b = u32::from(bordered.contains(name)); let (px, py, w, h) = (px + b, py + b, w - 2 * b, h - 2 * b); + let (gw, gh) = gui_sizes.get(name).copied().unwrap_or((w, h)); sprites.insert( name.clone(), Rect { @@ -412,8 +460,8 @@ impl HudAtlas { v0: py as f32 / height as f32, u1: (px + w) as f32 / width as f32, v1: (py + h) as f32 / height as f32, - w, - h, + w: gw, + h: gh, }, ); } @@ -433,14 +481,15 @@ impl HudAtlas { .find(|a| a.layer == layer) .cloned() { - let image = animation.resized_pixels(animation.frame(0), rect.w, rect.h); + let (tw, th) = rect.texels((width, height)); + let image = animation.resized_pixels(animation.frame(0), tw, th); let x = (rect.u0 * width as f32).round() as usize; let y = (rect.v0 * height as f32).round() as usize; - for row in 0..rect.h as usize { + let row_bytes = tw as usize * 4; + for row in 0..th as usize { let dst = ((y + row) * width as usize + x) * 4; - let src = row * rect.w as usize * 4; - pixels[dst..dst + rect.w as usize * 4] - .copy_from_slice(&image[src..src + rect.w as usize * 4]); + let src = row * row_bytes; + pixels[dst..dst + row_bytes].copy_from_slice(&image[src..src + row_bytes]); } fire_animations.push((rect, animation)); } @@ -1191,7 +1240,8 @@ impl HudRenderer { if frame == *previous { continue; } - let pixels = animation.resized_pixels(frame, rect.w, rect.h); + let (w, h) = rect.texels((texture.width(), texture.height())); + let pixels = animation.resized_pixels(frame, w, h); uploads.texture( encoder, wgpu::TexelCopyTextureInfo { @@ -1204,8 +1254,8 @@ impl HudRenderer { }, aspect: wgpu::TextureAspect::All, }, - rect.w, - rect.h, + w, + h, &pixels, ); *previous = frame; @@ -1383,6 +1433,48 @@ mod tests { ); } } + fn write_png(root: &Path, name: &str, w: u32, h: u32) { + let path = root.join(format!("assets/minecraft/textures/{name}.png")); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + let mut encoder = png::Encoder::new(fs::File::create(path).unwrap(), w, h); + encoder.set_color(png::ColorType::Rgba); + encoder.set_depth(png::BitDepth::Eight); + let pixels = vec![255; (w * h * 4) as usize]; + encoder + .write_header() + .unwrap() + .write_image_data(&pixels) + .unwrap(); + } + #[test] + fn a_pack_texture_keeps_the_default_textures_gui_size() { + let temp = std::env::temp_dir().join(format!("horizon-gui-size-{}", std::process::id())); + let (defaults, pack) = (temp.join("defaults"), temp.join("pack")); + write_png(&defaults, "gui/container/inventory", 256, 256); + write_png(&defaults, "gui/sprites/widget/button", 200, 20); + write_png(&defaults, "block/stone", 16, 16); + write_png(&defaults, "item/compass", 16, 512); + // Faithful 64x's inventory (past the 512 rows a texture may have), + // 32x's button and stone, an animated strip, and a texture of its own. + write_png(&pack, "gui/container/inventory", 1024, 1024); + write_png(&pack, "gui/sprites/widget/button", 400, 40); + write_png(&pack, "block/stone", 32, 32); + write_png(&pack, "item/compass", 32, 1024); + write_png(&pack, "gui/sprites/pack_only", 30, 10); + let atlas = HudAtlas::build_over(&pack, &defaults).unwrap(); + let _ = fs::remove_dir_all(&temp); + let size = (atlas.width, atlas.height); + for (name, gui, texels) in [ + ("gui/container/inventory", (256, 256), (1024, 1024)), + ("gui/sprites/widget/button", (200, 20), (400, 40)), + ("block/stone", (16, 16), (32, 32)), + ("item/compass", (16, 16), (32, 32)), + ("gui/sprites/pack_only", (30, 10), (30, 10)), + ] { + let r = atlas.sprites[name]; + assert_eq!(((r.w, r.h), r.texels(size)), (gui, texels), "{name}"); + } + } #[test] fn the_default_textures_keep_a_4096_wide_atlas() { let images = sized(400, 64, 64); diff --git a/crates/horizon-client/src/ui.rs b/crates/horizon-client/src/ui.rs index b1ffab866..8ddfa91ff 100644 --- a/crates/horizon-client/src/ui.rs +++ b/crates/horizon-client/src/ui.rs @@ -7,9 +7,40 @@ use crate::{ }; use std::{collections::HashMap, path::Path, sync::RwLock, time::Instant}; -/// Nine-slice borders (left, top, right, bottom) from `.png.mcmeta` files. -static NINE_SLICE: RwLock>> = RwLock::new(None); +/// `GuiSpriteScaling.NineSlice`: the sprite's size in GUI pixels, whatever +/// its texture's resolution, its borders (left, top, right, bottom) in the +/// same units, and whether the edges and centre stretch rather than tile. +#[derive(Clone, Copy, Debug, PartialEq)] +struct NineSlice { + width: u32, + height: u32, + border: [u32; 4], + stretch_inner: bool, +} +/// Nine-slice scaling from `.png.mcmeta` files. +static NINE_SLICE: RwLock>> = RwLock::new(None); +/// A `gui.scaling` section of type `nine_slice`. +fn parse_nine_slice(v: &serde_json::Value) -> Option { + let s = &v["gui"]["scaling"]; + if s["type"] != "nine_slice" { + return None; + } + let size = |k: &str| s[k].as_u64().map(|n| n as u32); + let border = &s["border"]; + let border = if let Some(n) = border.as_u64() { + [n as u32; 4] + } else { + let g = |k: &str| border[k].as_u64().unwrap_or(0) as u32; + [g("left"), g("top"), g("right"), g("bottom")] + }; + Some(NineSlice { + width: size("width")?, + height: size("height")?, + border, + stretch_inner: s["stretch_inner"].as_bool().unwrap_or(false), + }) +} /// Read `gui.scaling` metadata for every GUI sprite under `root`. pub fn load_metadata(root: &Path) { let mut map = HashMap::new(); @@ -32,47 +63,48 @@ pub fn load_metadata(root: &Path) { let Ok(text) = std::fs::read_to_string(&p) else { continue; }; - let Ok(v) = serde_json::from_str::(&text) else { + let Some(nine_slice) = serde_json::from_str(&text) + .ok() + .as_ref() + .and_then(parse_nine_slice) + else { continue; }; - let s = &v["gui"]["scaling"]; - if s["type"] != "nine_slice" { - continue; - } - let border = &s["border"]; - let b = if let Some(n) = border.as_u64() { - [n as u32; 4] - } else { - let g = |k: &str| border[k].as_u64().unwrap_or(0) as u32; - [g("left"), g("top"), g("right"), g("bottom")] - }; let key = Path::new(stripped) .strip_prefix(&base) .map(|r| r.to_string_lossy().replace('\\', "/")) .unwrap_or_default(); - map.insert(key, b); + map.insert(key, nine_slice); } } *NINE_SLICE.write().unwrap_or_else(|e| e.into_inner()) = Some(map); } -fn border(sprite: &str) -> [u32; 4] { +/// The sprite's nine-slice metadata; without any (no resources loaded), its +/// GUI size and the widgets' usual borders. +fn scaling(sprite: &str, r: &Rect) -> NineSlice { NINE_SLICE .read() .unwrap_or_else(|e| e.into_inner()) .as_ref() .and_then(|m| m.get(sprite).copied()) - .unwrap_or(match sprite { - "gui/sprites/widget/button" - | "gui/sprites/widget/button_highlighted" - | "gui/sprites/widget/button_disabled" => [3; 4], - "gui/sprites/widget/slider_handle" | "gui/sprites/widget/slider_handle_highlighted" => { - [2, 2, 2, 3] - } - _ => [1; 4], + .unwrap_or(NineSlice { + width: r.w, + height: r.h, + border: match sprite { + "gui/sprites/widget/button" + | "gui/sprites/widget/button_highlighted" + | "gui/sprites/widget/button_disabled" => [3; 4], + "gui/sprites/widget/slider_handle" + | "gui/sprites/widget/slider_handle_highlighted" => [2, 2, 2, 3], + _ => [1; 4], + }, + stretch_inner: false, }) } -fn sub(r: Rect, x: f32, y: f32, w: f32, h: f32) -> Rect { - let (du, dv) = ((r.u1 - r.u0) / r.w as f32, (r.v1 - r.v0) / r.h as f32); +/// The region `[x, y, w, h]` of a sprite `size` GUI pixels large, in its +/// own units (`TextureAtlasSprite.getU(x / width)`). +fn sub(r: Rect, size: [f32; 2], [x, y, w, h]: [f32; 4]) -> Rect { + let (du, dv) = ((r.u1 - r.u0) / size[0], (r.v1 - r.v0) / size[1]); Rect { u0: r.u0 + x * du, v0: r.v0 + y * dv, @@ -82,8 +114,9 @@ fn sub(r: Rect, x: f32, y: f32, w: f32, h: f32) -> Rect { h: h as u32, } } -/// Tile `src` (a sub-rectangle of a sprite, in its pixels) over a target area. -fn tile(batch: &mut HudBatch, r: Rect, src: [f32; 4], dst: [f32; 4], color: u32) { +/// `blitTiledSprite`: `src` (a region of a sprite `size` large, in its +/// units) repeated over `dst` from its top left, the last row and column cut. +fn tile(batch: &mut HudBatch, r: Rect, size: [f32; 2], src: [f32; 4], dst: [f32; 4], color: u32) { let [sx, sy, sw, sh] = src; let [dx, dy, dw, dh] = dst; if sw <= 0.0 || sh <= 0.0 || dw <= 0.0 || dh <= 0.0 { @@ -95,13 +128,16 @@ fn tile(batch: &mut HudBatch, r: Rect, src: [f32; 4], dst: [f32; 4], color: u32) let mut x = 0.0; while x < dw { let w = sw.min(dw - x); - batch.rect_uv(dx + x, dy + y, w, h, sub(r, sx, sy, w, h), color); + batch.rect_uv(dx + x, dy + y, w, h, sub(r, size, [sx, sy, w, h]), color); x += sw; } y += sh; } } -/// GuiGraphics.blitSprite with nine-slice scaling (inner parts tiled). +/// `GuiGraphicsExtractor.blitNineSlicedSprite`: the corners at their size, +/// the edges and centre tiled (or stretched with `stretch_inner`), all in +/// the sprite's `gui.scaling` units, so a high-resolution pack's sprite draws +/// as the default one does. pub fn nine_slice( batch: &mut HudBatch, atlas: &HudAtlas, @@ -112,47 +148,73 @@ pub fn nine_slice( h: f32, color: u32, ) { - let Some(r) = atlas.sprites.get(sprite).copied() else { - return; + if let Some(r) = atlas.sprites.get(sprite) { + draw_nine_slice(batch, *r, scaling(sprite, r), [x, y, w, h], color); + } +} +fn draw_nine_slice( + batch: &mut HudBatch, + r: Rect, + n: NineSlice, + [x, y, w, h]: [f32; 4], + color: u32, +) { + let size = [n.width as f32, n.height as f32]; + let [sw, sh] = size; + let [l, t, rr, b] = n.border.map(|v| v as f32); + let (half_w, half_h) = ((w / 2.0).floor(), (h / 2.0).floor()); + let (l, rr, t, b) = (l.min(half_w), rr.min(half_w), t.min(half_h), b.min(half_h)); + // blitSprite(sprite, width, height, textureX, textureY, x, y, w, h). + let part = |batch: &mut HudBatch, tx: f32, ty: f32, dst: [f32; 4]| { + if dst[2] != 0.0 && dst[3] != 0.0 { + batch.rect_uv( + dst[0], + dst[1], + dst[2], + dst[3], + sub(r, size, [tx, ty, dst[2], dst[3]]), + color, + ); + } }; - let [l, t, rr, b] = border(sprite).map(|v| v as f32); - let (sw, sh) = (r.w as f32, r.h as f32); - let l = l.min(w / 2.0); - let rr = rr.min(w / 2.0); - let t = t.min(h / 2.0); - let b = b.min(h / 2.0); - let (cw, ch) = (w - l - rr, h - t - b); - let (scw, sch) = (sw - l - rr, sh - t - b); - // Corners. - batch.rect_uv(x, y, l, t, sub(r, 0.0, 0.0, l, t), color); - batch.rect_uv(x + w - rr, y, rr, t, sub(r, sw - rr, 0.0, rr, t), color); - batch.rect_uv(x, y + h - b, l, b, sub(r, 0.0, sh - b, l, b), color); - batch.rect_uv( - x + w - rr, - y + h - b, - rr, - b, - sub(r, sw - rr, sh - b, rr, b), - color, - ); - // Edges and centre. - tile(batch, r, [l, 0.0, scw, t], [x + l, y, cw, t], color); - tile( - batch, - r, - [l, sh - b, scw, b], - [x + l, y + h - b, cw, b], - color, - ); - tile(batch, r, [0.0, t, l, sch], [x, y + t, l, ch], color); - tile( - batch, - r, - [sw - rr, t, rr, sch], - [x + w - rr, y + t, rr, ch], - color, - ); - tile(batch, r, [l, t, scw, sch], [x + l, y + t, cw, ch], color); + // blitNineSliceInnerSegment. + let inner = |batch: &mut HudBatch, dst: [f32; 4], src: [f32; 4]| { + if dst[2] <= 0.0 || dst[3] <= 0.0 { + return; + } + if n.stretch_inner { + batch.rect_uv(dst[0], dst[1], dst[2], dst[3], sub(r, size, src), color); + } else { + tile(batch, r, size, src, dst, color); + } + }; + if w == sw && h == sh { + part(batch, 0.0, 0.0, [x, y, w, h]); + return; + } + if h == sh { + part(batch, 0.0, 0.0, [x, y, l, h]); + inner(batch, [x + l, y, w - rr - l, h], [l, 0.0, sw - rr - l, sh]); + part(batch, sw - rr, 0.0, [x + w - rr, y, rr, h]); + return; + } + if w == sw { + part(batch, 0.0, 0.0, [x, y, w, t]); + inner(batch, [x, y + t, w, h - b - t], [0.0, t, sw, sh - b - t]); + part(batch, 0.0, sh - b, [x, y + h - b, w, b]); + return; + } + let (cw, ch) = (w - rr - l, h - b - t); + let (scw, sch) = (sw - rr - l, sh - b - t); + part(batch, 0.0, 0.0, [x, y, l, t]); + inner(batch, [x + l, y, cw, t], [l, 0.0, scw, t]); + part(batch, sw - rr, 0.0, [x + w - rr, y, rr, t]); + part(batch, 0.0, sh - b, [x, y + h - b, l, b]); + inner(batch, [x + l, y + h - b, cw, b], [l, sh - b, scw, b]); + part(batch, sw - rr, sh - b, [x + w - rr, y + h - b, rr, b]); + inner(batch, [x, y + t, l, ch], [0.0, t, l, sch]); + inner(batch, [x + l, y + t, cw, ch], [l, t, scw, sch]); + inner(batch, [x + w - rr, y + t, rr, ch], [sw - rr, t, rr, sch]); } /// Repeat a whole texture with each copy drawn `size` GUI pixels square. pub fn tiled( @@ -169,7 +231,6 @@ pub fn tiled( let Some(r) = atlas.sprites.get(sprite).copied() else { return; }; - let scale = r.w as f32 / size; let mut yy = 0.0; while yy < h { let th = size.min(h - yy); @@ -181,7 +242,7 @@ pub fn tiled( y + yy, tw, th, - sub(r, 0.0, 0.0, tw * scale, th * scale), + sub(r, [size, size], [0.0, 0.0, tw, th]), color, ); xx += size; @@ -1128,6 +1189,130 @@ impl EditBox { #[cfg(test)] mod tests { use super::*; + + /// The quads' destination rectangles and their UVs. + fn quads(batch: &HudBatch) -> Vec<([f32; 4], [f32; 4])> { + batch + .vertices + .chunks(6) + .map(|q| { + let (a, c) = (q[0], q[2]); + ( + [a.pos[0], a.pos[1], c.pos[0], c.pos[1]], + [a.uv[0], a.uv[1], c.uv[0], c.uv[1]], + ) + }) + .collect() + } + /// Destination and UV rectangles equal to within f32 rounding. + fn assert_quad(actual: ([f32; 4], [f32; 4]), expected: ([f32; 4], [f32; 4])) { + let close = |a: [f32; 4], b: [f32; 4]| a.iter().zip(b).all(|(a, b)| (a - b).abs() < 1e-5); + assert!( + close(actual.0, expected.0) && close(actual.1, expected.1), + "{actual:?} != {expected:?}" + ); + } + /// A sprite filling the atlas, `w x h` texels as a 32x pack draws it. + fn sprite(w: u32, h: u32) -> Rect { + Rect { + u0: 0.0, + v0: 0.0, + u1: 1.0, + v1: 1.0, + w, + h, + } + } + fn faithful(json: &str) -> NineSlice { + parse_nine_slice(&serde_json::from_str(json).unwrap()).unwrap() + } + #[test] + fn a_high_resolution_button_slices_in_its_gui_units() { + // Faithful 32x's widget/button: 400x40 texels, gui.scaling 200x20. + let n = faithful( + r#"{"gui":{"scaling":{"type":"nine_slice","width":200,"height":20, + "border":{"left":20,"top":4,"right":20,"bottom":4}}}}"#, + ); + let mut batch = HudBatch::new([640, 480]); + draw_nine_slice( + &mut batch, + sprite(400, 40), + n, + [10.0, 5.0, 150.0, 20.0], + u32::MAX, + ); + let q = quads(&batch); + // Full height: left border, the centre tiled, right border + // (blitNineSlicedSprite's height == nineSlice.height case). + assert_quad(q[0], ([10.0, 5.0, 30.0, 25.0], [0.0, 0.0, 0.1, 1.0])); + assert_quad(q[1], ([30.0, 5.0, 140.0, 25.0], [0.1, 0.0, 0.65, 1.0])); + assert_quad(q[2], ([140.0, 5.0, 160.0, 25.0], [0.9, 0.0, 1.0, 1.0])); + assert_eq!(q.len(), 3); + } + #[test] + fn nine_slice_corners_and_a_stretched_centre() { + // Faithful 32x's tooltip/frame: 200x200 texels, 100x100, border 12, stretched. + let n = faithful( + r#"{"gui":{"scaling":{"type":"nine_slice","width":100,"height":100,"border":12,"stretch_inner":true}}}"#, + ); + let mut batch = HudBatch::new([640, 480]); + draw_nine_slice( + &mut batch, + sprite(200, 200), + n, + [0.0, 0.0, 300.0, 40.0], + u32::MAX, + ); + let q = quads(&batch); + assert_eq!(q.len(), 9); + // Top left corner: 12 GUI pixels, 12/100 of the sprite. + assert_quad(q[0], ([0.0, 0.0, 12.0, 12.0], [0.0, 0.0, 0.12, 0.12])); + // The centre is one stretched quad (blitNineSliceInnerSegment, stretch_inner). + assert_quad(q[7], ([12.0, 12.0, 288.0, 28.0], [0.12, 0.12, 0.88, 0.88])); + // Bottom right corner. + assert_quad(q[5], ([288.0, 28.0, 300.0, 40.0], [0.88, 0.88, 1.0, 1.0])); + } + #[test] + fn a_sprite_drawn_at_its_own_size_is_one_quad() { + let n = NineSlice { + width: 50, + height: 50, + border: [10; 4], + stretch_inner: false, + }; + let mut batch = HudBatch::new([640, 480]); + draw_nine_slice( + &mut batch, + sprite(100, 100), + n, + [4.0, 4.0, 50.0, 50.0], + u32::MAX, + ); + let q = quads(&batch); + assert_eq!(q.len(), 1); + assert_quad(q[0], ([4.0, 4.0, 54.0, 54.0], [0.0, 0.0, 1.0, 1.0])); + } + #[test] + fn borders_shrink_to_half_the_drawn_size() { + let n = NineSlice { + width: 20, + height: 20, + border: [8; 4], + stretch_inner: true, + }; + let mut batch = HudBatch::new([640, 480]); + // 9 wide: borders of floor(9 / 2) = 4, as vanilla's integer division. + draw_nine_slice( + &mut batch, + sprite(20, 20), + n, + [0.0, 0.0, 9.0, 30.0], + u32::MAX, + ); + let q = quads(&batch); + assert_quad(q[0], ([0.0, 0.0, 4.0, 8.0], [0.0, 0.0, 0.2, 0.4])); + assert_quad(q[2], ([5.0, 0.0, 9.0, 8.0], [0.8, 0.0, 1.0, 0.4])); + } #[test] fn append_fields_highlight_the_whole_value_and_blink_their_cursor() { use crate::hud::Blend; From 7c42cc63ce78a1b181033286dac63df31e1c0605 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:30:27 +0200 Subject: [PATCH 37/39] parity: leave out of the prebuild probes that no longer exist A record naming an example since renamed or deleted made the whole Cargo run fail ("no example target named ..."), so no probe was prebuilt and every harness built its own: the probe binaries rename ~250 of them. Co-Authored-By: Claude Opus 5.5 --- tests/test_parity_runner.py | 5 ++++- tools/parity.py | 10 +++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/test_parity_runner.py b/tests/test_parity_runner.py index beffb557d..a9b7ff595 100644 --- a/tests/test_parity_runner.py +++ b/tests/test_parity_runner.py @@ -35,10 +35,13 @@ class ParityRunnerTests(unittest.TestCase): *parity.artifacts_of(["cargo", "build", "--release", "-p", "horizon-world", "--bin", "horizon-world"]), *parity.artifacts_of(["cargo", "build", "--release", "--bin", "horizon-world"]), *parity.artifacts_of(["cargo", "run", "--profile", "fast", "-p", "horizon-client", "--example", "c"]), + *parity.artifacts_of(["cargo", "build", "--release", "-p", "horizon-world", "--example", "renamed"]), ["example", "old", "release"], # recorded before selections were kept: its own crate's -p ["example", "unknown", "release"], # no longer in the workspace: left to its harness ] - self.assertEqual(parity.prebuild_commands(artifacts, {("example", "old"): "horizon-world"}), [ + packages = {("example", "a"): "horizon-world", ("example", "b"): "horizon-world", ("example", "c"): "horizon-client", + ("bin", "horizon-world"): "horizon-world", ("example", "old"): "horizon-world"} + self.assertEqual(parity.prebuild_commands(artifacts, packages), [ ["cargo", "build", "--locked", "--profile", "fast", "-p", "horizon-client", "--example", "c"], ["cargo", "build", "--locked", "--profile", "release", "--bin", "horizon-world"], ["cargo", "build", "--locked", "--profile", "release", "-p", "horizon-world", "--bin", "horizon-world"], diff --git a/tools/parity.py b/tools/parity.py index 5dbf13554..4113cc791 100644 --- a/tools/parity.py +++ b/tools/parity.py @@ -555,10 +555,14 @@ def prebuild_commands(artifacts, packages=None): """The Cargo runs that build the recorded probes exactly as their harnesses do: one per profile, package and feature selection, and whether examples are among the command's targets (see artifacts_of). - A [kind, name, profile] record from before selections were kept is taken as built with `-p` and its own crate, - as nearly every harness does (packages: target_packages()); the harness's next run records the real one.""" + packages: target_packages(). Records of targets that no longer exist (a probe renamed or deleted since a harness + ran it) are left out, since one unknown target fails its whole Cargo run. A [kind, name, profile] record from + before selections were kept is taken as built with `-p` and its own crate, as nearly every harness does; the + harness's next run records the real one.""" groups = {} for artifact in artifacts: + if packages and artifact[1] and tuple(artifact[:2]) not in packages: + continue if len(artifact) == 3 and (packages or {}).get(tuple(artifact[:2])): kind, name, profile = artifact artifact = [kind, name, profile, ["-p", packages[(kind, name)]], kind == "example"] @@ -578,7 +582,7 @@ def prebuild(artifacts, jobs): environment = run_environment() environment["CARGO_BUILD_JOBS"] = str(jobs) errors = "" - packages = target_packages() if any(len(artifact) == 3 for artifact in artifacts) else None + packages = target_packages() for command in prebuild_commands(artifacts, packages): result = subprocess.run(command, cwd=ROOT, env=environment, capture_output=True, text=True) if result.returncode: From 1960dc6eb26600d32145eecece3d494ee4f9d019 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:30:31 +0200 Subject: [PATCH 38/39] AGENTS.md: agents never add release-now Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index d869aed2a..9534fba83 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -21,7 +21,7 @@ Feature → files → harness for the most-changed areas: [docs/architecture.md] - Horizon-only features follow [docs/design-system.md](docs/design-system.md) and are off by default where they would change the vanilla look. - Small PRs (< ~500 lines). Labels: `feature` (players get something new), `skip-release` (they see no - change), none for a fix, `release-now` for crash fixes. Every merge to main ships + change), none for a fix. Agents never add `release-now`: the maintainer decides it. Every merge to main ships ([docs/release-flow.md](docs/release-flow.md)). - Contributors first: `gh pr list --state open --search ` before you start; never duplicate theirs. From f74d2fd5f4bf68dea276f4cc35b9d193c4932944 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 20:05:12 +0200 Subject: [PATCH 39/39] Restore Guest::send_all and Behaviours::bone_meal_spreads: main builds again #1432 deleted both as unused; #1424 (level events) and #1425 (scoreboard) merged after it and call them, so horizon-client's binary failed with E0599 at level_particles.rs:54 and scoreboard.rs:338. Both functions come back unchanged from 4d7f30f14^1. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/lan/guest.rs | 5 +++++ crates/horizon-world/src/level/bonemeal.rs | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index 652aec57e..84e768879 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -363,6 +363,11 @@ impl Guest { pub fn send(&self, packet: &play::Clientbound) { self.sender.send(packet.encode()); } + pub fn send_all(&self, packets: &[play::Clientbound]) { + for p in packets { + self.send(p); + } + } pub fn chunk(&self) -> (i32, i32) { ( (self.position[0].floor() as i32) >> 4, diff --git a/crates/horizon-world/src/level/bonemeal.rs b/crates/horizon-world/src/level/bonemeal.rs index ef52ca625..274512138 100644 --- a/crates/horizon-world/src/level/bonemeal.rs +++ b/crates/horizon-world/src/level/bonemeal.rs @@ -160,6 +160,16 @@ impl Behaviours { ) -> bool { self.sim(level, ticks).grow_water_plant(pos, face) } + /// The `BonemealableBlock.Type` particle spread of a block at level event + /// 1505: Some(true) for `NEIGHBOR_SPREADER` (particles over a 3-block + /// area above), Some(false) for `GROWER`, None when not bonemealable. + pub fn bone_meal_spreads(&self, state: BlockStateId) -> Option { + let kind = self.bonemeal_kind(state)?; + Some(matches!( + kind, + Kind::Grass | Kind::Nylium | Kind::Netherrack | Kind::FeaturePlacer + )) + } fn bonemeal_kind(&self, state: BlockStateId) -> Option { let data = self.data(state); if !data.implements(Interface::Bonemealable) {