diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index bec0ef1c3..31853ad92 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -68,6 +68,7 @@ mod flat; pub mod lightning; mod lod; mod occlusion; +mod pack_atlas; mod pack_post; pub mod pack_uniforms; mod page_runs; @@ -472,6 +473,8 @@ pub struct Renderer { world_post: world_post::Post, /// The shader pack's post-processing, and the dimension (legacy id) it was loaded for. pack_post: Option<(i32, Option)>, + /// The block sprites as one atlas, while a shader pack is loaded. + pack_atlas: Option, pub hud: HudRenderer, /// Vanilla's menu background blur, made on first use. menu_blur: Option, @@ -1320,6 +1323,8 @@ impl Renderer { format: wgpu::TextureFormat::Rgba8Unorm, usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST + // Shader packs' atlas copies them (pack_atlas.rs). + | wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::STORAGE_BINDING, view_formats: &[], }, @@ -2318,6 +2323,7 @@ impl Renderer { occlusion: occlusion::Occlusion::new(crate::chunk::MIN_Y, crate::chunk::HEIGHT / 16), world_post, pack_post: None, + pack_atlas: None, hud, menu_blur: None, info, @@ -2763,6 +2769,25 @@ impl Renderer { self.config.format.remove_srgb_suffix(), ) }); + if post.is_none() { + self.pack_atlas = None; + } else if self.pack_atlas.is_none() { + let mut encoder = self.device.create_command_encoder(&Default::default()); + let layers = self + .sprite_textures + .iter() + .map(|t| t.depth_or_array_layers()) + .sum(); + self.pack_atlas = pack_atlas::PackAtlas::new( + &self.device, + &mut encoder, + &self.sprite_textures, + sprite_split(), + layers, + self.sprite_textures[0].width(), + ); + self.queue.submit([encoder.finish()]); + } let report = post.as_ref().map(|p| { let programs = p.programs().into_iter().map(str::to_owned).collect(); (programs, p.notes.clone()) @@ -2893,6 +2918,13 @@ impl Renderer { } fn animate(&mut self, encoder: &mut wgpu::CommandEncoder, tick: u64) { self.animations.animate(encoder, &mut self.uploads, tick); + if let Some(atlas) = &self.pack_atlas { + atlas.copy( + encoder, + &self.sprite_textures, + self.animations.changed().iter().copied(), + ); + } } pub fn render(&mut self, frame: &Frame) -> Result { let rendered = self.draw_frame(frame); diff --git a/crates/horizon-client/src/gfx/animate.rs b/crates/horizon-client/src/gfx/animate.rs index f4528902b..b7c44429b 100644 --- a/crates/horizon-client/src/gfx/animate.rs +++ b/crates/horizon-client/src/gfx/animate.rs @@ -51,6 +51,8 @@ pub(super) struct SpriteAnimator { pipeline: wgpu::ComputePipeline, /// Edge of the arrays' layers. size: u32, + /// The layers the last `animate` rewrote, counted over all arrays. + changed: Vec, } impl SpriteAnimator { @@ -205,9 +207,15 @@ impl SpriteAnimator { jobs, pipeline, size, + changed: Vec::new(), } } + /// The layers the last `animate` rewrote (for the shader pack atlas). + pub fn changed(&self) -> &[u32] { + &self.changed + } + /// Show every sprite's frame at `tick`, rewriting only the sprites whose /// frame or mix changed (`SpriteContents.AnimationState.tick`). pub fn animate( @@ -245,12 +253,14 @@ impl SpriteAnimator { /// its layer shows; the number of jobs. fn collect(&mut self, tick: u64) -> usize { let mut count = 0; + self.changed.clear(); for sprite in &mut self.sprites { let blend = sprite.animation.blend(tick); if sprite.shown == Some(blend) { continue; } sprite.shown = Some(blend); + self.changed.push(sprite.animation.layer); let job = [ sprite.layer, sprite.frames[blend.frame], diff --git a/crates/horizon-client/src/gfx/pack_atlas.rs b/crates/horizon-client/src/gfx/pack_atlas.rs new file mode 100644 index 000000000..c90fa6ac1 --- /dev/null +++ b/crates/horizon-client/src/gfx/pack_atlas.rs @@ -0,0 +1,197 @@ +//! The block atlas of shader packs (spec 5.1, `atlasSize` in 5.2): packs sample one 2D atlas +//! with atlas coordinates, Horizon keeps block sprites in texture arrays. With a pack, every +//! layer is copied into a grid, row by row from the top left, and an animated layer again +//! whenever its frame changes. + +#[expect(dead_code, reason = "the gbuffers pass binds it (next slice)")] +pub(super) struct PackAtlas { + texture: wgpu::Texture, + pub view: wgpu::TextureView, + /// Sprites per row. + pub columns: u32, + /// A sprite's edge in pixels, at the first level. + pub sprite: u32, + /// Layers per sprite array. + split: u32, +} + +impl PackAtlas { + /// Copies `layers` layers of `arrays` (`split` per array, `sprite` pixels wide, `levels` + /// mip levels); `None` when the grid is larger than the device allows. + pub fn new( + device: &wgpu::Device, + encoder: &mut wgpu::CommandEncoder, + arrays: &[wgpu::Texture], + split: u32, + layers: u32, + sprite: u32, + ) -> Option { + let layers = layers.max(1); + let columns = (f64::from(layers).sqrt().ceil() as u32).max(1); + let rows = layers.div_ceil(columns); + let max = device.limits().max_texture_dimension_2d; + if columns * sprite > max || rows * sprite > max { + return None; + } + // Levels past a sprite's single pixel would mix neighbours: stop there. + let levels = arrays[0].mip_level_count().min(sprite.ilog2() + 1); + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some("Shader pack block atlas"), + size: wgpu::Extent3d { + width: columns * sprite, + height: rows * sprite, + depth_or_array_layers: 1, + }, + mip_level_count: levels, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::TEXTURE_BINDING + | wgpu::TextureUsages::COPY_DST + | wgpu::TextureUsages::COPY_SRC, + view_formats: &[], + }); + let view = texture.create_view(&Default::default()); + let atlas = Self { + texture, + view, + columns, + sprite, + split, + }; + atlas.copy(encoder, arrays, 0..layers); + Some(atlas) + } + + /// The atlas's size in pixels (`atlasSize`). + #[cfg_attr( + not(test), + expect(dead_code, reason = "the gbuffers pass reads it (next slice)") + )] + pub fn size(&self) -> [u32; 2] { + [self.texture.width(), self.texture.height()] + } + + /// Copies `layers` from the sprite arrays again, every level. + pub fn copy( + &self, + encoder: &mut wgpu::CommandEncoder, + arrays: &[wgpu::Texture], + layers: impl IntoIterator, + ) { + for layer in layers { + let Some(array) = arrays.get((layer / self.split) as usize) else { + continue; + }; + let (column, row) = (layer % self.columns, layer / self.columns); + for level in 0..self.texture.mip_level_count() { + let edge = self.sprite >> level; + encoder.copy_texture_to_texture( + wgpu::TexelCopyTextureInfo { + texture: array, + mip_level: level, + origin: wgpu::Origin3d { + x: 0, + y: 0, + z: layer % self.split, + }, + aspect: wgpu::TextureAspect::All, + }, + wgpu::TexelCopyTextureInfo { + texture: &self.texture, + mip_level: level, + origin: wgpu::Origin3d { + x: column * edge, + y: row * edge, + z: 0, + }, + aspect: wgpu::TextureAspect::All, + }, + wgpu::Extent3d { + width: edge, + height: edge, + depth_or_array_layers: 1, + }, + ); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Two arrays of 2x2 sprites, one layer split; each layer a colour of its own. + #[test] + fn layers_land_in_their_cells() { + let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle()); + let Ok(adapter) = pollster::block_on(instance.request_adapter(&Default::default())) else { + return; + }; + let (device, queue) = + pollster::block_on(adapter.request_device(&Default::default())).unwrap(); + let (split, layers, sprite) = (3u32, 5u32, 2u32); + let arrays: Vec = (0..2) + .map(|a| { + let count = (layers - a * split).min(split); + let pixels: Vec = (0..count) + .flat_map(|l| [(a * split + l) as u8 * 40, 0, 0, 255].repeat(4)) + .collect(); + wgpu::util::DeviceExt::create_texture_with_data( + &device, + &queue, + &wgpu::TextureDescriptor { + label: None, + size: wgpu::Extent3d { + width: sprite, + height: sprite, + depth_or_array_layers: count, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_SRC, + view_formats: &[], + }, + wgpu::util::TextureDataOrder::LayerMajor, + &pixels, + ) + }) + .collect(); + let mut encoder = device.create_command_encoder(&Default::default()); + let atlas = PackAtlas::new(&device, &mut encoder, &arrays, split, layers, sprite).unwrap(); + assert_eq!((atlas.columns, atlas.size()), (3, [6, 4])); + let read = device.create_buffer(&wgpu::BufferDescriptor { + label: None, + size: 256 * 4, + usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, + mapped_at_creation: false, + }); + encoder.copy_texture_to_buffer( + atlas.texture.as_image_copy(), + wgpu::TexelCopyBufferInfo { + buffer: &read, + layout: wgpu::TexelCopyBufferLayout { + offset: 0, + bytes_per_row: Some(256), + rows_per_image: None, + }, + }, + wgpu::Extent3d { + width: 6, + height: 4, + depth_or_array_layers: 1, + }, + ); + queue.submit([encoder.finish()]); + read.slice(..).map_async(wgpu::MapMode::Read, |_| {}); + device.poll(wgpu::PollType::wait_indefinitely()).unwrap(); + let data = read.slice(..).get_mapped_range().unwrap(); + let red = |x: usize, y: usize| data[y * 256 + x * 4]; + // Layer 4 (second array, its second layer) is in row 1, column 1. + assert_eq!([red(0, 0), red(2, 0), red(4, 0)], [0, 40, 80]); + assert_eq!([red(1, 3), red(3, 2), red(5, 2)], [120, 160, 0]); + } +}