Skip to content

Minecraft: Java Edition 26.3 rewritten from scratch in Rust, checked against vanilla with 281 differential harnesses. Get it with the Horizon launcher.

Merge main into #6422

Ludovic Marie4fb8955bf446
diff --cc crates/horizon-client-render/src/gfx/panorama.rs
index cb6a15517,9e44e902c..4c295e7ed
--- a/crates/horizon-client-render/src/gfx/panorama.rs
+++ b/crates/horizon-client-render/src/gfx/panorama.rs
@@@ -9,10 -6,9 +9,12 @@@ use wgpu::util::DeviceExt
/// Native cube order: +X, -X, +Y, -Y, +Z, -Z (CubeMapTexture.SUFFIXES).
const FACE_ORDER: [u8; 6] = [1, 3, 5, 4, 0, 2];
+ /// Where the cube's faces live in a resource root.
+ const BACKGROUND: &str = "assets/minecraft/textures/gui/title/background";
+/// The title screen's six faces. The cube is on the GPU only while the title
+/// screen shows, and its texels are decoded from the faces' files each time it
+/// is made: kept decoded, they held 24 MiB through the whole game.
#[derive(Debug)]
pub struct Faces {
pub size: u32,
@@@ -25,91 -16,75 +27,148 @@@
}
impl Faces {
- pub fn load(root: &Path) -> Self {
- Self::try_load(root).unwrap_or_else(|error| {
- log::warn!("Horizon: title panorama unavailable ({error})");
+ /// The title artwork of `root` (the packs' overlay) over `defaults` (the
+ /// game's own resources). `CubeMapTexture` registers each face with the
+ /// resource manager, which takes the topmost pack that has it: a pack that
+ /// leaves a face alone keeps vanilla's, and only a cube no layer completes
+ /// falls back to the grey placeholder that an installation without the
+ /// imported panorama shows.
+ pub fn load(root: &Path, defaults: &Path) -> Self {
+ Self::try_load(root, defaults).unwrap_or_else(|error| {
+ log::warn!(
+ "Horizon: no title panorama ({error}); prepare the game again in the launcher \
+ (or run horizon-prepare panorama)"
+ );
- Self {
- size: 1,
- pixels: [96, 110, 111, 255].repeat(6),
- }
+ Self::colour([96, 110, 111, 255])
})
}
+ /// A one-texel cube of one colour.
+ pub fn colour(colour: [u8; 4]) -> Self {
+ Self {
+ size: 1,
+ files: Vec::new(),
+ colour,
+ decoded: Mutex::new(None),
+ }
+ }
+
- fn try_load(root: &Path) -> Result<Self> {
- let background = root.join("assets/minecraft/textures/gui/title/background");
+ fn try_load(root: &Path, defaults: &Path) -> Result<Self> {
+ let mut size = 0;
- let mut pixels = Vec::new();
+ let mut files = Vec::with_capacity(FACE_ORDER.len());
++ let mut texels = Vec::new();
for face in FACE_ORDER {
- let path = background.join(format!("panorama_{face}.png"));
- files.push(std::fs::read(&path).map_err(|e| format!("{}: {e}", path.display()))?);
- let (path, w, h, data) = load_face(root, defaults, face)?;
++ let (path, file, (w, h, data)) = load_face(root, defaults, face)?;
+ if w != h || (size != 0 && w != size) {
+ return Err(format!(
+ "{}: panorama faces must be equally sized squares",
+ path.display()
+ )
+ .into());
+ }
+ size = w;
+ // CubeMapTexture copies each original image with flipY=true. Keep every texel.
+ for row in data.chunks_exact((w * 4) as usize).rev() {
- pixels.extend_from_slice(row);
++ texels.extend_from_slice(row);
+ }
++ files.push(file);
+ }
+ // Decoded once here to check them, and kept for the first cube.
- let (size, texels) = decode_faces(&files)?;
+ Ok(Self {
+ size,
+ files,
+ colour: [0; 4],
+ decoded: Mutex::new(Some(texels)),
+ })
+ }
+
+ /// The cube's texels, layer after layer.
+ pub fn texels(&self) -> Vec<u8> {
+ if self.files.is_empty() {
+ return self.colour.repeat(6);
}
- Ok(Self { size, pixels })
+ let loaded = self
+ .decoded
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .take();
+ loaded
+ .or_else(|| decode_faces(&self.files).ok().map(|(_, texels)| texels))
+ .unwrap_or_else(|| vec![0; (self.size * self.size * 4 * 6) as usize])
}
}
+/// The faces' files decoded, side by side, into one array of six layers of
+/// equal squares.
+fn decode_faces(files: &[Vec<u8>]) -> Result<(u32, Vec<u8>)> {
+ let decoded: Vec<_> = std::thread::scope(|scope| {
+ let threads: Vec<_> = files
+ .iter()
+ .map(|file| scope.spawn(|| decode_bytes(file).map_err(|e| e.to_string())))
+ .collect();
+ threads
+ .into_iter()
+ .map(|t| t.join().unwrap_or_else(|p| std::panic::resume_unwind(p)))
+ .collect()
+ });
+ let mut size = 0;
+ let mut texels = Vec::new();
+ for (face, image) in FACE_ORDER.iter().zip(decoded) {
+ let (w, h, data) = image?;
+ if w != h || (size != 0 && w != size) {
+ return Err(format!(
+ "panorama_{face}.png: panorama faces must be equally sized squares"
+ )
+ .into());
+ }
+ if size == 0 {
+ size = w;
+ texels.reserve_exact((w * w * 4) as usize * files.len());
+ }
+ // CubeMapTexture copies each original image with flipY=true. Keep every texel.
+ for row in data.chunks_exact((w * 4) as usize).rev() {
+ texels.extend_from_slice(row);
+ }
+ }
+ Ok((size, texels))
+}
+
+ /// `panorama_{face}.png` from the first layer that has one, with the path it
-/// came from. Reading a face that no layer provides fails naming every path
-/// tried, so a report of a missing panorama says where the game looked.
++/// came from, its file and its decoded image. Reading a face that no layer
++/// provides fails naming every path tried, so a report of a missing panorama
++/// says where the game looked.
++#[allow(clippy::type_complexity)]
+ fn load_face(
+ root: &Path,
+ defaults: &Path,
+ face: u8,
-) -> Result<(std::path::PathBuf, u32, u32, Vec<u8>)> {
++) -> Result<(std::path::PathBuf, Vec<u8>, (u32, u32, Vec<u8>))> {
+ let name = format!("panorama_{face}.png");
+ let mut tried: Vec<(std::path::PathBuf, String)> = Vec::new();
+ for layer in [root, defaults] {
+ let path = layer.join(BACKGROUND).join(&name);
+ // A vanilla-only installation loads one root twice.
+ if tried.first().is_some_and(|(seen, _)| *seen == path) {
+ continue;
+ }
- match decode(&path) {
- Ok((w, h, data)) => return Ok((path, w, h, data)),
- Err(error) => tried.push((path, error.to_string())),
++ let read = std::fs::read(&path)
++ .map_err(|e| e.to_string())
++ .and_then(|file| {
++ let image = decode_bytes(&file).map_err(|e| e.to_string())?;
++ Ok((file, image))
++ });
++ match read {
++ Ok((file, image)) => return Ok((path, file, image)),
++ Err(error) => tried.push((path, error)),
+ }
+ }
+ let tried: Vec<String> = tried
+ .iter()
+ .map(|(path, error)| format!("{}: {error}", path.display()))
+ .collect();
+ Err(tried.join("; ").into())
+ }
+
fn projection(width: u32, height: u32, yaw: f32, pitch: f32) -> Mat4 {
// CubeMap.render: 85-degree FOV, near/far 0.05/10, rotationX(PI), rotateX, rotateY.
Mat4::perspective_rh(
@@@ -395,12 -352,9 +460,12 @@@ mod tests
#[test]
fn original_resolution_texels_and_vanilla_cube_orientation_are_preserved() {
let f = Fixture::new(1024);
- let faces = Faces::try_load(&f.0).unwrap();
+ let faces = Faces::try_load(&f.0, &f.0).unwrap();
assert_eq!(faces.size, 1024);
- assert_eq!(faces.pixels.len(), 6 * 1024 * 1024 * 4);
+ // The texels decoded while loading, then decoded again for the next cube.
+ let (loaded, again) = (faces.texels(), faces.texels());
+ assert_eq!(loaded, again);
+ assert_eq!(again.len(), 6 * 1024 * 1024 * 4);
// CubeMapTexture's actual layer suffixes, with each source row flipped vertically.
for (layer, source_face) in [1, 3, 5, 4, 0, 2].into_iter().enumerate() {
for y in 0..1024 {
@@@ -426,13 -380,48 +491,49 @@@
.to_string()
.contains("equally sized squares")
);
- let fallback = Faces::load(&f.0);
+ let fallback = Faces::load(&f.0, &f.0);
- assert_eq!(fallback.pixels.len(), 24);
+ assert_eq!(fallback.texels(), [96, 110, 111, 255].repeat(6));
f.write(5, 2, 2);
- assert!(Faces::try_load(&f.0).is_err());
+ assert!(Faces::try_load(&f.0, &f.0).is_err());
fs::remove_file(f.background().join("panorama_5.png")).unwrap();
- assert!(Faces::try_load(&f.0).is_err());
- assert_eq!(Faces::load(&f.0).size, 1);
+ assert!(Faces::try_load(&f.0, &f.0).is_err());
+ assert_eq!(Faces::load(&f.0, &f.0).size, 1);
+ }
+
+ #[test]
+ fn a_pack_without_a_face_keeps_vanillas_and_its_own_face_wins() {
+ let vanilla = Fixture::new(4);
+ // A pack that replaces one face of the cube: the other five are
+ // still the game's, as the bottom of the pack stack provides them.
+ let pack = Fixture::bare();
+ pack.write(2, 4, 4);
+ let faces = Faces::try_load(&pack.0, &vanilla.0).unwrap();
+ assert_eq!(faces.size, 4);
- assert_eq!(faces.pixels.len(), 6 * 4 * 4 * 4);
++ let texels = faces.texels();
++ assert_eq!(texels.len(), 6 * 4 * 4 * 4);
+ // Each face's red channel is its index (`face` above), and the cube
+ // holds them in FACE_ORDER: layer 5 is face 2, the pack's, and layer 4
+ // is face 0, which only the game's resources have.
- let red = |layer: usize, texel: usize| faces.pixels[(layer * 16 + texel) * 4];
++ let red = |layer: usize, texel: usize| texels[(layer * 16 + texel) * 4];
+ assert_eq!(red(5, 0), 2);
+ assert_eq!(red(4, 0), 0);
+ }
+
+ #[test]
+ fn a_missing_cube_names_every_path_it_tried() {
+ let vanilla = Fixture::bare();
+ let pack = Fixture::bare();
+ let error = Faces::try_load(&pack.0, &vanilla.0)
+ .unwrap_err()
+ .to_string();
+ for root in [&pack, &vanilla] {
+ let path = root.background().join("panorama_1.png");
+ assert!(error.contains(&path.display().to_string()), "{error}");
+ }
+ // No layer completes the cube: the one-pixel grey placeholder, and the
+ // window still has a title.
+ let fallback = Faces::load(&pack.0, &vanilla.0);
- assert_eq!((fallback.size, fallback.pixels.len()), (1, 24));
++ assert_eq!((fallback.size, fallback.texels().len()), (1, 24));
}
#[test]