Prepare startup handoff CPU resources on the loader worker

This commit is contained in:
rdbtCVS
2026-10-19 18:31:52 +01:00
parent defaa7cd2a
commit 4ffb073b59
4 changed files with 281 additions and 44 deletions
+162 -28
View File
@@ -8362,6 +8362,7 @@ impl Game<'_> {
pack,
gui,
self.renderer.as_ref(),
None,
self.settings.horizon.api,
)
}
@@ -10799,6 +10800,7 @@ impl<'a> Game<'a> {
options: Options,
settings: Settings,
audio: Audio,
prepared: Option<GameCpuResources>,
) -> Result<Self> {
let root = options.resources.clone();
let registry = pack.assets.registry;
@@ -10815,15 +10817,9 @@ impl<'a> Game<'a> {
.join("saves")
.join(".horizon-title"),
));
// These batches share the exported document instead of reparsing it per layer.
let (armor_stand_assets, boat_models, cart_models) = {
let document = horizon_client::mobs::EntityModelDocument::load(&root).ok();
(
armor_stands::ArmorStandAssets::load(&root, document.as_ref()),
boats::BoatModels::load(document.as_ref()),
minecarts::CartModels::load(document.as_ref()),
)
};
let prepared = prepared
.map(Ok)
.unwrap_or_else(|| GameCpuResources::load(&root))?;
let world = World::new(res.facts().air, 0);
let creative = options.creative;
let inventory = vec![None; 36];
@@ -11004,18 +11000,18 @@ impl<'a> Game<'a> {
level_events: Vec::new(),
falling: Vec::new(),
paintings: Vec::new(),
painting_variants: paintings::PaintingVariants::load(&root),
painting_variants: prepared.painting_variants,
item_frames: Vec::new(),
frame_model: frames::bake_frame(&root, "item_frame"),
glow_frame_model: frames::bake_frame(&root, "glow_item_frame"),
map_frame_model: frames::bake_frame(&root, "item_frame_map"),
glow_map_frame_model: frames::bake_frame(&root, "glow_item_frame_map"),
frame_model: prepared.frame_model,
glow_frame_model: prepared.glow_frame_model,
map_frame_model: prepared.map_frame_model,
glow_map_frame_model: prepared.glow_map_frame_model,
armor_stands: Vec::new(),
others: Vec::new(),
armor_stand_assets,
armor_stand_assets: prepared.armor_stand_assets,
boats: Vec::new(),
boat_models,
crystal_model: horizon_client::mobs::load_model(&root, "minecraft:end_crystal#main"),
boat_models: prepared.boat_models,
crystal_model: prepared.crystal_model,
carts: Vec::new(),
jukebox_sounds: HashMap::new(),
bolts: Vec::new(),
@@ -11025,7 +11021,7 @@ impl<'a> Game<'a> {
crystals: Vec::new(),
dragon_fireballs: Vec::new(),
breath_clouds: Vec::new(),
dragon_model: horizon_client::mobs::load_model(&root, "minecraft:ender_dragon#main"),
dragon_model: prepared.dragon_model,
map_patches: Vec::new(),
skin_downloads: horizon_client::skins::Downloader::new(
std::env::current_dir()
@@ -11033,7 +11029,7 @@ impl<'a> Game<'a> {
.join("assets/skins"),
),
skin_slots: Vec::new(),
cart_models,
cart_models: prepared.cart_models,
riding: None,
ender_items: vec![None; 27],
ender_open: None,
@@ -11047,11 +11043,11 @@ impl<'a> Game<'a> {
ominous_spawners: Vec::new(),
creaking: Default::default(),
spawners: Default::default(),
trident_model: horizon_client::mobs::load_model(&root, "minecraft:trident#main"),
arrow_model: horizon_client::mobs::load_model(&root, "minecraft:arrow#main"),
trident_model: prepared.trident_model,
arrow_model: prepared.arrow_model,
tnts: Vec::new(),
hoppers: HashMap::new(),
creative_screen: Creative::load(&root)?,
creative_screen: prepared.creative_screen,
control: None,
frame_stats: Default::default(),
perf: Default::default(),
@@ -11126,8 +11122,49 @@ impl<'a> Game<'a> {
}
}
/// CPU-only startup data, owned by one loader generation. No file reads or
/// model baking are needed when the event loop installs this bundle.
struct GameCpuResources {
painting_variants: paintings::PaintingVariants,
frame_model: frames::FrameModel,
glow_frame_model: frames::FrameModel,
map_frame_model: frames::FrameModel,
glow_map_frame_model: frames::FrameModel,
armor_stand_assets: armor_stands::ArmorStandAssets,
boat_models: boats::BoatModels,
cart_models: minecarts::CartModels,
crystal_model: Option<horizon_client::mobs::Part>,
dragon_model: Option<horizon_client::mobs::Part>,
arrow_model: Option<horizon_client::mobs::Part>,
trident_model: Option<horizon_client::mobs::Part>,
creative_screen: Creative,
}
impl GameCpuResources {
fn load(root: &std::path::Path) -> Result<Self> {
let document = horizon_client::mobs::EntityModelDocument::load(root).ok();
let model = |key| document.as_ref().and_then(|document| document.model(key));
Ok(Self {
painting_variants: paintings::PaintingVariants::load(root),
frame_model: frames::bake_frame(root, "item_frame"),
glow_frame_model: frames::bake_frame(root, "glow_item_frame"),
map_frame_model: frames::bake_frame(root, "item_frame_map"),
glow_map_frame_model: frames::bake_frame(root, "glow_item_frame_map"),
armor_stand_assets: armor_stands::ArmorStandAssets::load(root, document.as_ref()),
boat_models: boats::BoatModels::load(document.as_ref()),
cart_models: minecarts::CartModels::load(document.as_ref()),
crystal_model: model("minecraft:end_crystal#main"),
dragon_model: model("minecraft:ender_dragon#main"),
arrow_model: model("minecraft:arrow#main"),
trident_model: model("minecraft:trident#main"),
creative_screen: Creative::load(root)?,
})
}
}
/// The data the background loader produces for `Game::new`.
struct Loaded {
game_cpu: Option<GameCpuResources>,
renderer_cpu: Option<horizon_client::gfx::PreparedTextures>,
root: PathBuf,
pack_error: Option<String>,
resources: Arc<Resources>,
@@ -11451,6 +11488,17 @@ fn load_resources(
started.elapsed().as_millis()
);
}
// Finish fallible CPU preparation before starting the tail, so an error
// cannot detach a tail worker that races the next pack-loader generation.
let prepared_at = Instant::now();
let game_cpu = menu.then(|| GameCpuResources::load(&root)).transpose()?;
let renderer_cpu = menu.then(|| prepare_renderer_textures(&pack));
if menu {
log::info!(
"Horizon: handoff CPU resources prepared on worker in {} ms",
prepared_at.elapsed().as_millis()
);
}
let rest_loader = if menu {
let resources = resources.clone();
let root = root.clone();
@@ -11502,6 +11550,8 @@ fn load_resources(
None
};
Ok(Loaded {
game_cpu,
renderer_cpu,
root,
pack_error,
resources,
@@ -11580,14 +11630,26 @@ fn load_world_rest(
})
}
/// The renderer from a pack's block sprites, its shared atlas and its border
/// texture; `previous` shares the device and window surface with an old one.
/// CPU mip chains for the block sprites and world border.
fn prepare_renderer_textures(pack: &PackResources) -> horizon_client::gfx::PreparedTextures {
let assets = &pack.render;
horizon_client::gfx::PreparedTextures::new(
&assets.pixels,
assets.sprite_size,
assets.texture_layers,
&assets.sprite_mips,
(&pack.forcefield.2, pack.forcefield.0),
)
}
/// Upload pack textures and create the renderer on the window's device.
fn build_renderer(
window: Arc<Window>,
offscreen: bool,
pack: &PackResources,
gui: &UiResources,
previous: Option<&Renderer>,
prepared: Option<horizon_client::gfx::PreparedTextures>,
api: horizon_client::graphics::GraphicsApi,
) -> Result<Renderer> {
let assets = &pack.render;
@@ -11604,6 +11666,7 @@ fn build_renderer(
(&atlas.pixels, atlas.width, atlas.height),
(&pack.forcefield.2, pack.forcefield.0),
&gui.panorama,
prepared,
previous,
api,
))?;
@@ -11649,6 +11712,7 @@ fn loading_renderer(
(pixels, width, height),
(&FORCEFIELD[..], 1),
panorama,
None,
previous,
api,
))?;
@@ -11728,9 +11792,17 @@ fn start_game<'a>(
vanilla_root: PathBuf,
started: Instant,
rest_loader: Option<WorldRestLoader>,
settings: Option<Settings>,
prepared: Option<GameCpuResources>,
) -> Result<Option<Game<'a>>> {
let mut settings = Settings::load(&std::env::current_dir()?.join("options.txt"));
horizon_client::ui_theme::load_preferences(&std::env::current_dir()?);
let handoff = prepared.is_some();
let mut settings = match settings {
Some(settings) => settings,
None => {
horizon_client::ui_theme::load_preferences(&std::env::current_dir()?);
Settings::load(&std::env::current_dir()?.join("options.txt"))
}
};
// Scripted and frame-limited runs (the parity harnesses) see vanilla's
// screens unless HORIZON_MENU_STYLE asks otherwise.
if (options.frames.is_some() || options.control.is_some())
@@ -11741,7 +11813,7 @@ fn start_game<'a>(
if let Some(d) = options.distance_override {
settings.render_distance = d;
}
let mut game = Game::new(res, pack, gui, options, settings, audio)?;
let mut game = Game::new(res, pack, gui, options, settings, audio, prepared)?;
game.rest_loader = rest_loader;
game.ui.vanilla_root = vanilla_root;
if let Some(error) = pack_error {
@@ -11819,6 +11891,7 @@ fn start_game<'a>(
let (seed, creative) = (game.options.seed, game.options.creative);
game.open_world(&folder, None, Some(seed), creative, None)?;
}
None if handoff => {}
None => {
let mut title = Title::new(
std::env::current_dir()?.join("saves"),
@@ -12025,6 +12098,7 @@ impl Loading {
}
}
fn finish(&mut self, loaded: Loaded) -> Result<Option<Game<'static>>> {
let handoff_at = Instant::now();
let mut options = self.options.take().ok_or("loading already finished")?;
options.resources = loaded.root.clone();
// StartupMenu owns world selection until it can hand it to Game.
@@ -12036,18 +12110,21 @@ impl Loading {
if let Some(renderer) = &mut self.renderer {
renderer.release_surface();
}
let renderer_at = Instant::now();
let renderer = build_renderer(
window.clone(),
options.offscreen,
&loaded.pack,
&gui,
self.renderer.as_ref(),
loaded.renderer_cpu,
self.settings.horizon.api,
)?;
log::info!(
"Horizon: world renderer ready after {} ms",
self.started.elapsed().as_millis()
);
let renderer_ms = renderer_at.elapsed().as_millis();
// Audio is created on the main thread, after the core is ready.
let audio = if options.dedicated.is_some() {
Audio::silent(&options.resources)
@@ -12058,6 +12135,7 @@ impl Loading {
// The game borrows its shared resources for the process's lifetime,
// like the single copy an integrated server would share.
let res: &'static Arc<Resources> = Box::leak(Box::new(loaded.resources));
let game_at = Instant::now();
let Some(mut game) = start_game(
res,
loaded.pack,
@@ -12068,18 +12146,26 @@ impl Loading {
self.vanilla_root.clone(),
self.started,
loaded.rest_loader,
Some(self.settings.clone()),
loaded.game_cpu,
)?
else {
return Ok(None);
};
let game_ms = game_at.elapsed().as_millis();
game.window = Some(window);
game.renderer = Some(renderer);
game.world_renderer_ready = true;
game.fullscreen = self.fullscreen;
self.menu.handoff(&mut game);
game.settings = self.settings.clone();
game.apply_settings();
game.previous = Instant::now();
log::info!(
"Horizon: startup handoff {} ms (renderer {} ms, start_game {} ms)",
handoff_at.elapsed().as_millis(),
renderer_ms,
game_ms
);
Ok(Some(game))
}
}
@@ -12266,6 +12352,8 @@ pub fn run() -> Result<()> {
vanilla_root,
started,
loaded.rest_loader,
None,
None,
)?;
if let Some(game) = game {
run_event_loop(game)?;
@@ -12289,6 +12377,52 @@ fn reset_world_progress(adv: &mut advancements::AdvState, stats: &mut stats::Sta
*stats = Default::default();
}
#[cfg(test)]
mod startup_cpu_tests {
use super::*;
#[test]
fn worker_prepares_owned_models_and_inventory_before_handoff() {
let root = std::env::temp_dir().join(format!("horizon-startup-cpu-{}", std::process::id()));
std::fs::create_dir_all(&root).unwrap();
std::fs::write(
root.join("entity-models.json"),
r#"{
"minecraft:end_crystal#main": {}, "minecraft:ender_dragon#main": {},
"minecraft:arrow#main": {}, "minecraft:trident#main": {},
"minecraft:armor_stand#main": {}
}"#,
)
.unwrap();
std::fs::write(
root.join("creative-tabs.json"),
r#"[{
"id":"minecraft:building_blocks", "items":["minecraft:stone"]
}]"#,
)
.unwrap();
let worker_root = root.clone();
let prepared = std::thread::spawn(move || {
GameCpuResources::load(&worker_root).map_err(|error| error.to_string())
})
.join()
.unwrap()
.unwrap();
std::fs::remove_dir_all(&root).unwrap();
assert!(prepared.crystal_model.is_some());
assert!(prepared.dragon_model.is_some());
assert!(prepared.arrow_model.is_some());
assert!(prepared.trident_model.is_some());
assert!(prepared.armor_stand_assets.main.is_some());
assert_eq!(
prepared.creative_screen.tabs[0].items[0].item.as_ref(),
"minecraft:stone"
);
// No documents from the old generation remain available to reload.
assert!(GameCpuResources::load(&root).is_err());
}
}
#[cfg(test)]
mod runtime_reset_tests {
use super::*;
+1 -1
View File
@@ -660,6 +660,6 @@ fn serve(
}),
};
let audio = Audio::silent(&options.resources);
let mut server = Game::new(res, pack, gui, options, settings, audio)?;
let mut server = Game::new(res, pack, gui, options, settings, audio, None)?;
server.run_integrated(launch, flags, tasks, ready)
}
+96 -15
View File
@@ -524,6 +524,46 @@ fn terrain_group(
],
})
}
/// CPU texture uploads prepared by the resource worker. Consumed by renderer
/// construction so the temporary mip chains are released after GPU upload.
pub struct PreparedTextures {
sprites: Vec<(Vec<u8>, u32)>,
forcefield: (Vec<u8>, u32),
}
impl PreparedTextures {
pub fn new(
pixels: &[u8],
size: u32,
layers: u32,
settings: &[crate::mipmap::Settings],
forcefield: (&[u8], u32),
) -> Self {
Self::with_split(pixels, size, layers, settings, forcefield, sprite_split())
}
fn with_split(
pixels: &[u8],
size: u32,
layers: u32,
settings: &[crate::mipmap::Settings],
forcefield: (&[u8], u32),
split: u32,
) -> Self {
let layer_bytes = (size * size * 4) as usize;
Self {
sprites: (0..layers.div_ceil(split).max(1))
.map(|array| {
let first = array * split;
let count = layers.saturating_sub(first).clamp(1, split);
let start = (first as usize * layer_bytes).min(pixels.len());
mip_chain(&pixels[start..], size, settings, first, count)
})
.collect(),
forcefield: mip_chain(forcefield.0, forcefield.1, &[], 0, 1),
}
}
}
fn mip_chain(
pixels: &[u8],
size: u32,
@@ -807,6 +847,7 @@ impl Renderer {
hud_atlas: (&[u8], u32, u32),
forcefield: (&[u8], u32),
panorama_faces: &panorama::Faces,
prepared: Option<PreparedTextures>,
previous: Option<&Self>,
api: GraphicsApi,
) -> Result<Self> {
@@ -1180,20 +1221,17 @@ impl Renderer {
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let layer_bytes = (sprite_size * sprite_size * 4) as usize;
let prepared = prepared.unwrap_or_else(|| {
PreparedTextures::new(sprite_pixels, sprite_size, layers, sprite_mips, forcefield)
});
let mut mip_levels = 1;
let sprite_textures: Vec<wgpu::Texture> = (0..sprite_arrays)
.map(|array| {
let first = array * split;
let sprite_textures: Vec<wgpu::Texture> = prepared
.sprites
.into_iter()
.enumerate()
.map(|(array, (mips, levels))| {
let first = array as u32 * split;
let count = layers.saturating_sub(first).clamp(1, split);
let start = (first as usize * layer_bytes).min(sprite_pixels.len());
let (mips, levels) = mip_chain(
&sprite_pixels[start..],
sprite_size,
sprite_mips,
first,
count,
);
mip_levels = levels;
device.create_texture_with_data(
&queue,
@@ -1654,9 +1692,8 @@ impl Renderer {
});
// WorldBorderRenderer: misc/forcefield.png as a MipmappedTexture
// (repeat, linear minification with mipmaps, nearest magnification).
let (forcefield_pixels, forcefield_size) = forcefield;
let (forcefield_mips, forcefield_levels) =
mip_chain(forcefield_pixels, forcefield_size, &[], 0, 1);
let forcefield_size = forcefield.1;
let (forcefield_mips, forcefield_levels) = prepared.forcefield;
let forcefield_texture = device.create_texture_with_data(
&queue,
&wgpu::TextureDescriptor {
@@ -4540,6 +4577,50 @@ mod sprite_upload_tests {
}
}
#[test]
fn prepared_texture_uploads_preserve_layer_order_and_settings() {
let size = 4;
let layers = 5;
let pixels: Vec<u8> = (0..size * size * layers)
.flat_map(|i| [i as u8, (i * 3) as u8, 99, if i % 3 == 0 { 0 } else { 255 }])
.collect();
let settings: Vec<_> = (0..layers)
.map(|i| crate::mipmap::Settings {
strategy: if i % 2 == 0 {
crate::mipmap::Strategy::Mean
} else {
crate::mipmap::Strategy::StrictCutout
},
..Default::default()
})
.collect();
let border = [20, 30, 40, 128].repeat(16);
let expected = mip_chain(&pixels, size, &settings, 0, layers);
// A partial final array and an exact boundary exercise the global
// layer offset used to select each sprite's mipmap settings.
for split in [1, 2, 5, 8] {
let prepared = PreparedTextures::with_split(
&pixels,
size,
layers,
&settings,
(&border, size),
split,
);
assert_eq!(prepared.sprites.len(), layers.div_ceil(split) as usize);
let actual: Vec<_> = prepared
.sprites
.iter()
.flat_map(|(bytes, levels)| {
assert_eq!(*levels, expected.1);
bytes.iter().copied()
})
.collect();
assert_eq!(actual, expected.0);
assert_eq!(prepared.forcefield, mip_chain(&border, size, &[], 0, 1));
}
}
#[test]
fn mip_chain_allocates_exactly_its_size() {
for size in [16, 64] {
+22
View File
@@ -16,6 +16,28 @@ The menu still needs its own artwork and renderer before interaction. The core h
Regression coverage publishes menu artwork while a fake core worker is blocked, edits a world draft, replaces fallback artwork without losing it, queues entry and draws progress, and checks that loader failure reaches the app. Additional checks cover server identity and cancelled-entry timing.
## CPU preparation before handoff
The resource loader now prepares the game's entity models, item-frame models,
painting variants and creative inventory, plus the block-sprite and world-border
mip chains, before publishing `Loaded`. All startup entity models share one parsed
`entity-models.json` document. Each loader generation owns its prepared data;
restarting after a pack change discards that generation's models and uploads.
Fallible preparation finishes before the world-rest worker starts, so preparation
failure cannot leave a detached tail writing into the next loader generation.
The event-loop handoff consumes the prepared game data and mip chains. GPU
pipeline creation, texture uploads, surface operations and audio initialization
still run on the main thread. Upload buffers are released after construction.
The handoff uses the current menu settings and title object directly, avoiding
another options-file read, theme-preference load and temporary title construction.
Synchronous harnesses and integrated servers prepare their game data inline.
Logs now report worker CPU preparation time and the handoff's total, renderer and
`start_game` times separately. The reported 245 ms handoff (103 ms renderer,
118 ms `start_game`) predates this change; the new timings should be compared in
an otherwise identical build and GPU environment.
## Render facts assessment
`MeshTables::new` reads names, registry properties, class membership, collision shape flags, fluid and render properties from `BlockFacts`. `LightTables::new/load` reads emission, dampening, occlusion and shape-occlusion flags; geometry comes from `occlusion.nbt`. Neither table needs recipes, item data, AI or the other tail tables.