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] 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.