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.

Shader packs: route destroy-stage overlays through gbuffers_damagedblock

gigamans442667dd7902ad4
diff --git a/crates/horizon-client-render/src/gfx.rs b/crates/horizon-client-render/src/gfx.rs
index 5b3ee3b82..abf91cd6a 100644
--- a/crates/horizon-client-render/src/gfx.rs
+++ b/crates/horizon-client-render/src/gfx.rs
@@ -4861,6 +4861,11 @@ impl Renderer {
let world = pack_post::World {
atlas: self.pack_atlas.as_ref().map(|a| (a.view.clone(), a.size())),
lightmap: self.lightmap_view.clone(),
+ gui_size: self
+ .hud
+ .atlas_texture
+ .as_ref()
+ .map_or([1, 1], |t| [t.width(), t.height()]),
terrain: self.pack_atlas.as_ref().map(|_| pack_post::Geometry {
buffers: if flat { &buffers[..1] } else { &buffers },
table: self.flat.as_ref().filter(|_| flat).map(|f| &f.draw_layout),
@@ -6314,6 +6319,7 @@ impl Renderer {
&self.queue,
frame.entities,
frame.entity_spans,
+ frame.crumbling_sprite,
&mut self.pack_entity_rest,
);
entities = &self.pack_entity_rest;
diff --git a/crates/horizon-client-render/src/gfx/pack_entities.rs b/crates/horizon-client-render/src/gfx/pack_entities.rs
index f4ec2db43..71a3997d9 100644
--- a/crates/horizon-client-render/src/gfx/pack_entities.rs
+++ b/crates/horizon-client-render/src/gfx/pack_entities.rs
@@ -319,6 +319,7 @@ impl PackEntities {
entities: &[Vertex],
spans: &[EntitySpan],
drawn: [bool; PROGRAMS],
+ crack: ([f32; 4], [u32; 2]),
shadow: Casters,
rest: &mut Vec<Vertex>,
) {
@@ -330,7 +331,9 @@ impl PackEntities {
rest.extend_from_slice(&entities[..first.min(entities.len())]);
for (span, range) in runs(entities.len(), spans) {
let run = &entities[range];
- let program = program_of(&span).filter(|_| plain(run));
+ let program = program_of(&span).filter(|&p| {
+ plain(run) || (p == DESTROY && run.iter().all(|v| matches!(v.layer, 11 | 12)))
+ });
let pack = program.is_some_and(|p| drawn[p]) && !span.hidden;
let casts = program.is_some_and(|p| shadow.cast(&span, p));
if !pack && !span.hidden {
@@ -341,6 +344,9 @@ impl PackEntities {
};
let start = self.scratch.len() as u32;
self.scratch.extend_from_slice(run);
+ if program == DESTROY {
+ project_cracks(&mut self.scratch[start as usize..], crack.0, crack.1);
+ }
let (entity_id, block_id) = match program {
BLOCK => (0, span.id),
_ => (span.id, 0),
@@ -419,7 +425,8 @@ pub const ENTITY: usize = 0;
pub const BLOCK: usize = 1;
pub const TRANSLUCENT: usize = 2;
pub const PARTICLE: usize = 3;
-pub const PROGRAMS: usize = 4;
+pub const DESTROY: usize = 4;
+pub const PROGRAMS: usize = 5;
/// Which program draws `span`'s run, given which of them (or their fallbacks) the pack has;
/// `None`: Horizon draws it.
@@ -440,6 +447,8 @@ pub fn program_of(span: &EntitySpan) -> Option<usize> {
BLOCK
} else if span.stage == render_stage("PARTICLES") {
PARTICLE
+ } else if span.stage == render_stage("DESTROY") {
+ DESTROY
} else {
return None;
})
@@ -461,6 +470,16 @@ pub fn runs(
mod tests {
use super::*;
+ #[test]
+ fn destroy_stage_uses_the_pack_only_when_a_program_is_available() {
+ let destroy = EntitySpan {
+ stage: render_stage("DESTROY"),
+ ..Default::default()
+ };
+ assert!(drawn_by_pack(&destroy, [true; PROGRAMS]).is_some());
+ assert_eq!(drawn_by_pack(&destroy, [false; PROGRAMS]), None);
+ }
+
#[test]
fn runs_split_the_list_between_pack_and_horizon() {
let v = |layer| Vertex {
@@ -483,15 +502,21 @@ mod tests {
let all = [true; PROGRAMS];
assert_eq!(drawn_by_pack(&spans[0], all), Some(ENTITY));
assert_eq!(drawn_by_pack(&spans[1], all), Some(PARTICLE));
- assert_eq!(drawn_by_pack(&spans[1], [true, true, true, false]), None);
- assert_eq!(drawn_by_pack(&spans[2], [true, false, true, true]), None);
+ assert_eq!(
+ drawn_by_pack(&spans[1], [true, true, true, false, true]),
+ None
+ );
+ assert_eq!(
+ drawn_by_pack(&spans[2], [true, false, true, true, true]),
+ None
+ );
assert_eq!(drawn_by_pack(&spans[2], all), Some(BLOCK));
let shadow = EntitySpan {
translucent: true,
..spans[0]
};
assert_eq!(drawn_by_pack(&shadow, all), Some(TRANSLUCENT));
- assert_eq!(drawn_by_pack(&span(0, "DESTROY", 0), all), None);
+ assert_eq!(drawn_by_pack(&span(0, "DESTROY", 0), all), Some(DESTROY));
// A beacon beam's quads stay Horizon's.
assert!(plain(&[v(0), v(0), v(0), v(0), v(1)]));
assert!(!plain(&[v(0), v(0), v(0), v(0), v(6)]));
@@ -523,3 +548,53 @@ mod tests {
assert!(!some.cast(&spans[2], BLOCK) && some.cast(&lit, BLOCK));
}
}
+
+/// Decode the destroy-stage origin stored in the light bytes and project one continuous
+/// sprite over each model face. Pack geometry uses ordinary atlas UVs, not Horizon's overlay codes.
+fn project_cracks(vertices: &mut [Vertex], sprite: [f32; 4], atlas: [u32; 2]) {
+ let dims = [atlas[0].max(1) as f32, atlas[1].max(1) as f32];
+ for quad in vertices.chunks_exact_mut(4) {
+ let origin = [
+ (quad[0].light & 65535) as f32 / 65535.0,
+ (quad[0].light >> 16) as f32 / 65535.0,
+ ];
+ let origin = [0, 1].map(|i| (origin[i] * dims[i]).round() / dims[i]);
+ let min = [0, 1]
+ .map(|i| (quad.iter().map(|v| v.uv[i]).fold(f32::INFINITY, f32::min) + 0.0001).floor());
+ for v in quad {
+ v.uv =
+ [0, 1].map(|i| origin[i] + (v.uv[i] - min[i]).clamp(0.001, 0.999) * sprite[i + 2]);
+ v.layer = 0;
+ v.light = 0x0000_f0f0;
+ }
+ }
+}
+
+#[cfg(test)]
+mod crack_projection_tests {
+ use super::*;
+ #[test]
+ fn crack_uvs_keep_a_full_sprite_and_decode_each_stage() {
+ let mut vertices: Vec<Vertex> = [[-1.0, -1.0], [-1.0, 0.0], [0.0, 0.0], [0.0, -1.0]]
+ .into_iter()
+ .map(|uv| Vertex {
+ uv,
+ layer: 11,
+ light: crate::breaking::stage_light([0.25, 0.5]),
+ ..bytemuck::Zeroable::zeroed()
+ })
+ .collect();
+ project_cracks(
+ &mut vertices,
+ [0.0, 0.0, 16.0 / 4096.0, 16.0 / 9216.0],
+ [4096, 9216],
+ );
+ assert!(
+ vertices
+ .iter()
+ .all(|v| v.layer == 0 && v.uv[0] >= 0.25 && v.uv[0] < 0.25 + 16.0 / 4096.0)
+ );
+ assert!((vertices[2].uv[0] - vertices[0].uv[0]) > 15.0 / 4096.0);
+ assert!((vertices[1].uv[1] - vertices[0].uv[1]) > 15.0 / 9216.0);
+ }
+}
diff --git a/crates/horizon-client-render/src/gfx/pack_post.rs b/crates/horizon-client-render/src/gfx/pack_post.rs
index d205f855c..dc46e6e06 100644
--- a/crates/horizon-client-render/src/gfx/pack_post.rs
+++ b/crates/horizon-client-render/src/gfx/pack_post.rs
@@ -408,6 +408,7 @@ pub(super) struct World<'a> {
pub block_ids: &'a [i32],
/// The atlas of the sun and moon's sprites.
pub celestial: Option<wgpu::TextureView>,
+ pub gui_size: [u32; 2],
/// Set 1 of the clouds' draw: their offset and palette.
pub clouds: Option<&'a wgpu::BindGroupLayout>,
/// The game can hand its rain and snow to the pack.
@@ -510,6 +511,7 @@ pub struct PackPost {
/// with the page buffer it was made for.
block_ids: Option<(wgpu::BindGroupLayout, wgpu::Buffer)>,
page_groups: HashMap<u32, (wgpu::Buffer, wgpu::BindGroup)>,
+ gui_size: [u32; 2],
/// GPU time of each pass, while the renderer asks for it (`pack_timing.rs`).
pub timing: Option<super::pack_timing::PassTiming>,
/// `depthtex0` at the screen's centre, read back for `centerDepthSmooth`.
@@ -1433,7 +1435,15 @@ fn build_pass(
depth_write_enabled: Some(g.depth_write),
depth_compare: Some(wgpu::CompareFunction::LessEqual),
stencil: Default::default(),
- bias: Default::default(),
+ bias: if p.name == "gbuffers_damagedblock" {
+ wgpu::DepthBiasState {
+ constant: -10,
+ slope_scale: -1.0,
+ clamp: 0.0,
+ }
+ } else {
+ Default::default()
+ },
}),
multisample: Default::default(),
fragment: Some(wgpu::FragmentState {
@@ -1893,8 +1903,15 @@ impl PackPost {
post.block,
post.entities_translucent,
post.particles,
+ post.damaged_block,
+ ];
+ let late = [
+ false,
+ false,
+ post.translucent_late,
+ post.particles_late,
+ false,
];
- let late = [false, false, post.translucent_late, post.particles_late];
let shadow_only = (post.shadow_only_player, post.shadow_only_lit_blocks);
let shadow_programs = [post.shadow_entities, post.shadow_block];
let entity_vertices = (programs.iter().chain(&shadow_programs))
@@ -1910,8 +1927,21 @@ impl PackPost {
cull: false,
});
let entity_passes = programs.map(|p| {
- p.zip(entity_geometry.as_ref())
- .and_then(|(p, g)| build(p, Some(g)))
+ p.zip(entity_geometry.as_ref()).and_then(|(p, g)| {
+ if p.name == "gbuffers_damagedblock" {
+ let decal = Geometry {
+ buffers: g.buffers,
+ table: g.table,
+ depth_write: false,
+ atlas: g.atlas.clone(),
+ ids: g.ids,
+ cull: false,
+ };
+ build(p, Some(&decal))
+ } else {
+ build(p, Some(g))
+ }
+ })
});
let shadow_entities = shadow_programs.map(|p| {
p.zip(entity_geometry.as_ref())
@@ -2243,6 +2273,7 @@ impl PackPost {
draws,
block_ids,
page_groups: HashMap::new(),
+ gui_size: world.gui_size,
timing: None,
center_depth: None,
custom,
@@ -2868,6 +2899,7 @@ impl PackPost {
queue: &wgpu::Queue,
entities: &[super::Vertex],
spans: &[super::pack_draw::EntitySpan],
+ crumbling: [f32; 4],
rest: &mut Vec<super::Vertex>,
) {
let shadow = self.shadow_casters();
@@ -2884,6 +2916,7 @@ impl PackPost {
entities,
spans,
drawn,
+ (crumbling, self.gui_size),
shadow,
rest,
);
diff --git a/crates/horizon-shaders/src/post.rs b/crates/horizon-shaders/src/post.rs
index b3fd83eb7..5f5a5eb74 100644
--- a/crates/horizon-shaders/src/post.rs
+++ b/crates/horizon-shaders/src/post.rs
@@ -385,6 +385,7 @@ pub struct Post {
pub entities_translucent: Option<PostProgram>,
/// What draws particles.
pub particles: Option<PostProgram>,
+ pub damaged_block: Option<PostProgram>,
/// What draws the game's clouds.
pub clouds: Option<PostProgram>,
/// What draws rain and snow, after deferred.
@@ -482,6 +483,7 @@ pub fn load_post(
block: None,
entities_translucent: None,
particles: None,
+ damaged_block: None,
clouds: None,
weather: None,
hand: None,
@@ -609,6 +611,19 @@ pub fn load_post(
),
None => (None, None),
};
+ let damaged_block = inputs
+ .entities
+ .as_ref()
+ .and_then(|i| gbuffers(&["gbuffers_damagedblock"], Group::Gbuffers, i))
+ .map(|mut p| {
+ p.blend = blend(properties, &p.name).unwrap_or(Some([
+ Factor::DstColor,
+ Factor::SrcColor,
+ Factor::One,
+ Factor::Zero,
+ ]));
+ p
+ });
// Translucent entities and particles blend (OQ-23). Particles keep vanilla's alpha cut at
// 0.1; translucent entities lose only fully transparent texels (a shadow fades out).
let translucent =
@@ -682,6 +697,7 @@ pub fn load_post(
loader.post.block = block;
loader.post.entities_translucent = entities_translucent;
loader.post.particles = particles;
+ loader.post.damaged_block = damaged_block;
loader.post.clouds = clouds;
loader.post.weather = weather;
loader.post.hand = hand;
@@ -723,6 +739,7 @@ fn load_shadow(
&post.block,
&post.entities_translucent,
&post.particles,
+ &post.damaged_block,
&post.clouds,
&post.weather,
&post.hand,