diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 180d38ab0..bddf59490 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -6784,12 +6784,10 @@ impl Game<'_> { self.fangs_geometry(camera, alpha, &mut out); let mut see_through = std::mem::take(&mut self.see_through_vertices); see_through.clear(); - // Name tags and shadows blend: Horizon keeps drawing them until the pack's entity - // programs draw translucent parts (`minecraft:name_tag`, `minecraft:entity_shadow`). - self.mark_stage(out.len(), "NONE"); self.name_tag_geometry(camera, alpha, &mut out, &mut see_through); self.see_through_vertices = see_through; // Entity shadows (EntityRenderer.extractShadow). + self.mark_translucent_entity(out.len(), "minecraft:entity_shadow"); if let Some(sprite) = self.gui.atlas.sprites.get("misc/shadow").copied() { let ambient = if self.dim == Dim::Nether { 0.1 } else { 0.0 }; let darken = self.sky_darken(); diff --git a/crates/horizon-client/src/game/names.rs b/crates/horizon-client/src/game/names.rs index be829f1bb..3c97a9368 100644 --- a/crates/horizon-client/src/game/names.rs +++ b/crates/horizon-client/src/game/names.rs @@ -253,12 +253,16 @@ impl Game<'_> { const FADED: u32 = 0x7F; for tag in self.name_tags(camera, alpha) { let o = tag.at - camera; + // `gbuffers_entities` draws name tag text (2.3); a sneaking player's faded tag, on its + // background, blends. if tag.discrete { + self.mark_translucent_entity(out.len(), "minecraft:name_tag"); self.name_tag_quads(o, &tag.lines, FADED, true, 0, vertex_light(tag.light), out); continue; } // LightCoordsUtil.lightCoordsWithEmission(lightCoords, 2). let lit = (tag.light >> 4).max(2) << 4 | (tag.light & 15).max(2); + self.mark_entity(out.len(), "minecraft:name_tag"); self.name_tag_quads(o, &tag.lines, 0xFF, false, 0, vertex_light(lit), out); // RenderPipelines.TEXT_SEE_THROUGH: no depth test, no lightmap. self.name_tag_quads(o, &tag.lines, FADED, true, 2, 0, see_through); diff --git a/crates/horizon-client/src/game/shader_packs.rs b/crates/horizon-client/src/game/shader_packs.rs index 6699a753d..88a73c108 100644 --- a/crates/horizon-client/src/game/shader_packs.rs +++ b/crates/horizon-client/src/game/shader_packs.rs @@ -146,9 +146,21 @@ impl Game<'_> { /// Starts a span of the entity geometry at vertex `start` for entity type `name` /// (`minecraft:zombie`; "" for one the pack cannot name). Nothing without a pack. pub(super) fn mark_entity(&self, start: usize, name: &str) { + self.mark_entity_part(start, name, false); + } + + /// Starts a span for entity geometry that blends (name tags, shadows). + pub(super) fn mark_translucent_entity(&self, start: usize, name: &str) { + self.mark_entity_part(start, name, true); + } + + fn mark_entity_part(&self, start: usize, name: &str, translucent: bool) { if let Some(ids) = &self.pack_ids { let id = ids.entities.get(name).copied(); self.push_span(start, "ENTITIES", id.unwrap_or(ids.missing_entity)); + if let Some(last) = self.entity_spans.borrow_mut().last_mut() { + last.translucent = translucent; + } } } @@ -185,6 +197,7 @@ impl Game<'_> { start: start as u32, stage: horizon_shaders::render_stage(stage), id, + translucent: false, }; // A span that drew nothing gives way to the next. match spans.last_mut() { diff --git a/crates/horizon-client/src/gfx/pack_draw.rs b/crates/horizon-client/src/gfx/pack_draw.rs index f365c484f..d19c3b4db 100644 --- a/crates/horizon-client/src/gfx/pack_draw.rs +++ b/crates/horizon-client/src/gfx/pack_draw.rs @@ -16,6 +16,8 @@ pub struct EntitySpan { pub stage: u32, /// `entityId` for entities, `blockEntityId` for block entities (5.3), else 0. pub id: i32, + /// An entity part that blends (a name tag, a shadow): `gbuffers_entities_translucent`. + pub translucent: bool, } /// `HzDraw`'s size: `renderStage`, `entityId`, `blockEntityId`, a pad, `entityColor`. diff --git a/crates/horizon-client/src/gfx/pack_entities.rs b/crates/horizon-client/src/gfx/pack_entities.rs index b30aa4fdb..6f7de5fe1 100644 --- a/crates/horizon-client/src/gfx/pack_entities.rs +++ b/crates/horizon-client/src/gfx/pack_entities.rs @@ -1,5 +1,6 @@ -//! Entities and block entities drawn by a shader pack's `gbuffers_entities` and `gbuffers_block` -//! (spec 2.3, 5.3, 5.4): the runs of `Frame::entities` the game marked (`EntitySpan`), each its +//! Entities, block entities, translucent entity parts and particles drawn by a shader pack's +//! `gbuffers_entities`, `gbuffers_block`, `gbuffers_entities_translucent` and +//! `gbuffers_particles` (spec 2.3, 5.3, 5.4): the runs of `Frame::entities` the game marked (`EntitySpan`), each its //! own draw with its `entityId` or `blockEntityId` and `entityColor` in a per-draw slot of //! `HzDraw` (set 2). Horizon draws whatever the pack does not. @@ -125,8 +126,8 @@ pub(super) struct Draw { /// The first vertex. pub base: u32, pub quads: u32, - /// `gbuffers_block` (or its fallback) draws it, not `gbuffers_entities`. - pub block: bool, + /// The program that draws it (`ENTITY`, `BLOCK`, `TRANSLUCENT`, `PARTICLE`). + pub program: usize, pub slot: u32, } @@ -253,7 +254,7 @@ impl PackEntities { layout: &wgpu::BindGroupLayout, entities: &[Vertex], spans: &[EntitySpan], - drawn: [bool; 2], + drawn: [bool; PROGRAMS], rest: &mut Vec, ) { self.draws.clear(); @@ -264,7 +265,7 @@ impl PackEntities { rest.extend_from_slice(&entities[..first.min(entities.len())]); for (span, range) in runs(entities.len(), spans) { let run = &entities[range]; - let Some(block) = drawn_by_pack(span.stage, drawn).filter(|_| plain(run)) else { + let Some(program) = drawn_by_pack(&span, drawn).filter(|_| plain(run)) else { rest.extend_from_slice(run); continue; }; @@ -273,7 +274,10 @@ impl PackEntities { } let start = self.scratch.len() as u32; self.scratch.extend_from_slice(run); - let (entity_id, block_id) = if block { (0, span.id) } else { (span.id, 0) }; + let (entity_id, block_id) = match program { + BLOCK => (0, span.id), + _ => (span.id, 0), + }; let mut bytes = [0u8; SLOT as usize]; bytes[0..4].copy_from_slice(&(span.stage as i32).to_le_bytes()); bytes[4..8].copy_from_slice(&entity_id.to_le_bytes()); @@ -287,7 +291,7 @@ impl PackEntities { self.draws.push(Draw { base: start, quads: run.len() as u32 / 4, - block, + program, slot: self.draws.len() as u32, }); } @@ -340,16 +344,30 @@ fn plain(run: &[Vertex]) -> bool { .all(|v| !matches!(v.layer, 2 | 4 | 6 | 9 | 10 | 11 | 12)) } -/// Whether the pack draws a run of render stage `stage` (`Some(true)` with `gbuffers_block`), -/// given which of `gbuffers_entities` and `gbuffers_block` (or their fallbacks) it has. -pub fn drawn_by_pack(stage: u32, drawn: [bool; 2]) -> Option { - if stage == render_stage("ENTITIES") && drawn[0] { - Some(false) - } else if stage == render_stage("BLOCK_ENTITIES") && drawn[1] { - Some(true) +/// The programs of entity runs, in the order they draw. +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; + +/// Which program draws `span`'s run, given which of them (or their fallbacks) the pack has; +/// `None`: Horizon draws it. +pub fn drawn_by_pack(span: &EntitySpan, drawn: [bool; PROGRAMS]) -> Option { + let program = if span.stage == render_stage("ENTITIES") { + if span.translucent { + TRANSLUCENT + } else { + ENTITY + } + } else if span.stage == render_stage("BLOCK_ENTITIES") { + BLOCK + } else if span.stage == render_stage("PARTICLES") { + PARTICLE } else { - None - } + return None; + }; + drawn[program].then_some(program) } /// Each span with its vertex range: from its start to the next span's (or the end). @@ -378,6 +396,7 @@ mod tests { start, stage: render_stage(stage), id, + translucent: false, }; let spans = [ span(2, "ENTITIES", 7), @@ -386,10 +405,18 @@ mod tests { ]; let ranges: Vec<_> = runs(10, &spans).map(|(_, r)| r).collect(); assert_eq!(ranges, [2..5, 5..6, 6..10]); - assert_eq!(drawn_by_pack(spans[0].stage, [true, false]), Some(false)); - assert_eq!(drawn_by_pack(spans[1].stage, [true, true]), None); - assert_eq!(drawn_by_pack(spans[2].stage, [true, false]), None); - assert_eq!(drawn_by_pack(spans[2].stage, [false, true]), Some(true)); + 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[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); // 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)])); diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index 8d6d12d94..fc372ab95 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -16,7 +16,7 @@ use std::collections::HashMap; use horizon_shaders::{Format, Group, Kind, Post, Val}; use super::pack_draw::DrawUniforms; -use super::pack_entities::PackEntities; +use super::pack_entities::{PROGRAMS, PackEntities}; use super::pack_sky::PackSky; use super::pack_uniforms::{self, FrameUniforms, PackInputs}; @@ -131,12 +131,13 @@ struct Sky { textured: Option, } -/// Entities and block entities a pack draws (`gbuffers_entities`, `gbuffers_block` or their -/// fallbacks), after terrain; Horizon draws the others. +/// Entities, block entities, translucent entity parts and particles a pack draws +/// (`pack_entities::ENTITY`... order), after terrain or, `late`, after deferred; Horizon draws +/// the others. struct Entities { geometry: PackEntities, - entity: Option, - block: Option, + passes: [Option; PROGRAMS], + late: [bool; PROGRAMS], } struct Pass { @@ -912,7 +913,16 @@ impl PackPost { } }); let sky = sky.filter(|s| s.basic.is_some() || s.textured.is_some()); - let entity_vertices = (post.entities.is_some() || post.block.is_some()) + let programs = [ + post.entities, + post.block, + post.entities_translucent, + post.particles, + ]; + let late = [false, false, post.translucent_late, post.particles_late]; + let entity_vertices = programs + .iter() + .any(Option::is_some) .then(|| PackEntities::new(device, &draws.layout)); let entity_buffers = super::pack_entities::buffers(); let entity_geometry = entity_vertices.as_ref().map(|e| Geometry { @@ -923,16 +933,16 @@ impl PackPost { ids: None, cull: false, }); - let entity = post.entities.zip(entity_geometry.as_ref()); - let entity = entity.and_then(|(p, g)| build(p, Some(g))); - let block = post.block.zip(entity_geometry.as_ref()); - let block = block.and_then(|(p, g)| build(p, Some(g))); + let entity_passes = programs.map(|p| { + p.zip(entity_geometry.as_ref()) + .and_then(|(p, g)| build(p, Some(g))) + }); let entities = entity_vertices - .filter(|_| entity.is_some() || block.is_some()) + .filter(|_| entity_passes.iter().any(Option::is_some)) .map(|geometry| Entities { geometry, - entity, - block, + passes: entity_passes, + late, }); // Developers' aid: `HORIZON_SHADER_PACK_SKIP=composite,final` leaves programs out. let skip = std::env::var("HORIZON_SHADER_PACK_SKIP").unwrap_or_default(); @@ -956,10 +966,13 @@ impl PackPost { notes.push(format!("the {part} draw with {}", p.name)); } } - for (part, pass) in entities - .iter() - .flat_map(|e| [("entities", &e.entity), ("block entities", &e.block)]) - { + let parts = [ + "entities", + "block entities", + "translucent entities", + "particles", + ]; + for (part, pass) in entities.iter().flat_map(|e| parts.iter().zip(&e.passes)) { if let Some(p) = pass { notes.push(format!("{part} draw with {}", p.name)); } @@ -981,11 +994,7 @@ impl PackPost { .iter() .flat_map(|s| s.basic.iter().chain(&s.textured)) .chain(&terrain) - .chain( - entities - .iter() - .flat_map(|e| e.entity.iter().chain(&e.block)), - ) + .chain(entities.iter().flat_map(|e| e.passes.iter().flatten())) .chain(&water) .find(touched) .map(|_| true) @@ -1420,7 +1429,8 @@ impl PackPost { self.sky = Some(sky); } - /// Entities and block entities with the pack's programs: each run `prepare_entities` kept + /// Entities, block entities, translucent entity parts and particles with the pack's + /// programs, those drawn before deferred or (`late`) after: each run `prepare_entities` kept /// is one draw with its own `entityId` or `blockEntityId` and `entityColor`. fn draw_entities( &mut self, @@ -1428,17 +1438,20 @@ impl PackPost { queue: &wgpu::Queue, encoder: &mut wgpu::CommandEncoder, size: [u32; 2], + late: bool, ) { let Some(mut entities) = self.entities.take() else { return; }; let geometry = &entities.geometry; - for (block, pass) in [(false, &mut entities.entity), (true, &mut entities.block)] { - let Some(pass) = pass else { continue }; + for (program, pass) in entities.passes.iter_mut().enumerate() { + let Some(pass) = pass.as_mut().filter(|_| entities.late[program] == late) else { + continue; + }; let mut draws = geometry .draws .iter() - .filter(|d| d.block == block) + .filter(|d| d.program == program) .peekable(); if draws.peek().is_none() { continue; @@ -1478,7 +1491,7 @@ impl PackPost { rest.extend_from_slice(entities); return; }; - let drawn = [e.entity.is_some(), e.block.is_some()]; + let drawn = e.passes.each_ref().map(Option::is_some); e.geometry.prepare( device, queue, @@ -1509,7 +1522,20 @@ impl PackPost { self.draw_sky(device, queue, encoder, size, frame); } self.draw_terrain(device, queue, encoder, size, false, terrain); - self.draw_entities(device, queue, encoder, size); + self.draw_entities(device, queue, encoder, size, false); + } + + /// Translucent terrain, then the entity parts drawn after deferred. + fn draw_translucent( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + size: [u32; 2], + terrain: &mut TerrainDraw<'_>, + ) { + self.draw_terrain(device, queue, encoder, size, true, terrain); + self.draw_entities(device, queue, encoder, size, true); } /// Regenerates the mip chain of buffer `b`'s side that programs read. @@ -1818,7 +1844,13 @@ impl PackPost { let mut shown = false; let mut terrain_drawn = self.terrain.is_none() && self.sky.is_none() && self.entities.is_none(); - let mut water_drawn = self.water.is_none(); + let late_entities = self.entities.as_ref().is_some_and(|e| { + e.passes + .iter() + .zip(e.late) + .any(|(p, late)| p.is_some() && late) + }); + let mut water_drawn = self.water.is_none() && !late_entities; for pass in &mut passes { if !terrain_drawn && pass.group != Group::Prepare { terrain_drawn = true; @@ -1826,7 +1858,7 @@ impl PackPost { } if !water_drawn && matches!(pass.group, Group::Composite | Group::Final) { water_drawn = true; - self.draw_terrain(device, queue, encoder, size, true, terrain); + self.draw_translucent(device, queue, encoder, size, terrain); } if pass.group == Group::Deferred && !seen.0 { seen.0 = true; @@ -1881,7 +1913,7 @@ impl PackPost { self.draw_opaque(device, queue, encoder, size, frame, terrain); } if !water_drawn { - self.draw_terrain(device, queue, encoder, size, true, terrain); + self.draw_translucent(device, queue, encoder, size, terrain); } let targets = self.targets.as_ref().expect("resized"); let view = |m: u16, i: usize, alt: bool| { diff --git a/crates/horizon-shaders/src/post.rs b/crates/horizon-shaders/src/post.rs index 71c055c2b..a31cf5ce7 100644 --- a/crates/horizon-shaders/src/post.rs +++ b/crates/horizon-shaders/src/post.rs @@ -120,6 +120,23 @@ const BLOCK: [&str; 5] = [ "gbuffers_basic", ]; +/// `gbuffers_entities_translucent` and its fallbacks (2.3). +const ENTITIES_TRANSLUCENT: [&str; 5] = [ + "gbuffers_entities_translucent", + "gbuffers_entities", + "gbuffers_textured_lit", + "gbuffers_textured", + "gbuffers_basic", +]; + +/// `gbuffers_particles` and its fallbacks (2.3). +const PARTICLES: [&str; 4] = [ + "gbuffers_particles", + "gbuffers_textured_lit", + "gbuffers_textured", + "gbuffers_basic", +]; + /// The sun and moon's default blend: added to the sky, as vanilla draws them. pub const CELESTIAL_BLEND: Blend = [ Factor::SrcAlpha, @@ -170,6 +187,14 @@ pub struct Post { pub entities: Option, /// What draws block entities, after terrain. pub block: Option, + /// What draws translucent entities (name tags, shadows), after the others. + pub entities_translucent: Option, + /// What draws particles. + pub particles: Option, + /// Translucent entities draw after deferred (`separateEntityDraws`, 3.2). + pub translucent_late: bool, + /// Particles draw after deferred (`particles.ordering`, `particles.before.deferred`, 3.2). + pub particles_late: bool, /// The custom uniforms of `shaders.properties` (3.4). pub custom: Custom, pub config: Config, @@ -219,6 +244,10 @@ pub fn load_post( sky_textured: None, entities: None, block: None, + entities_translucent: None, + particles: None, + translucent_late: false, + particles_late: false, custom: Custom::parse(properties), config: Config::default(), notes: Vec::new(), @@ -293,6 +322,26 @@ pub fn load_post( ), None => (None, None), }; + // 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 = + |names: &[&str], + cut: f32, + gbuffers: &mut dyn FnMut(&[&str], &GbuffersInputs) -> Option| { + let i = inputs.entities.as_ref()?; + let mut i = i.clone(); + i.alpha_test = Some(crate::AlphaTest { + op: ">", + reference: cut, + }); + gbuffers(names, &i).map(|mut p| { + p.blend = blend(properties, &p.name).unwrap_or(Some(TRANSLUCENT_BLEND)); + p + }) + }; + let mut gbuffers_dyn = |names: &[&str], i: &GbuffersInputs| gbuffers(names, Group::Gbuffers, i); + let entities_translucent = translucent(&ENTITIES_TRANSLUCENT, 0.0, &mut gbuffers_dyn); + let particles = translucent(&PARTICLES, 0.1, &mut gbuffers_dyn); if let Some(inputs) = &inputs.terrain { let terrain = gbuffers(&TERRAIN, Group::Gbuffers, inputs); // gbuffers_water falls back to gbuffers_terrain's chain (2.3). @@ -310,6 +359,17 @@ pub fn load_post( loader.post.sky_textured = sky_textured; loader.post.entities = entities; loader.post.block = block; + loader.post.entities_translucent = entities_translucent; + loader.post.particles = particles; + let flag = |key: &str| properties.get(key).map(|v| v.trim() == "true"); + loader.post.translucent_late = flag("separateEntityDraws").unwrap_or(false); + // `after` when the pack has a deferred program, else `mixed`; ours are all translucent, so + // `mixed` draws them after deferred too. + loader.post.particles_late = match properties.get("particles.ordering").map(str::trim) { + Some("before") => false, + Some(_) => true, + None => !flag("particles.before.deferred").unwrap_or(false), + }; post.config = std::mem::take(&mut post.config).finish(); post } @@ -624,6 +684,24 @@ mod tests { (block.name.as_str(), block.blend), ("gbuffers_terrain", None) ); + // Translucent entity parts and particles blend; particles draw after deferred unless + // the pack says otherwise, translucent entities before unless `separateEntityDraws`. + let (translucent, particles) = ( + post.entities_translucent.as_ref().unwrap(), + post.particles.as_ref().unwrap(), + ); + assert_eq!( + (translucent.name.as_str(), translucent.blend), + ("gbuffers_textured", Some(TRANSLUCENT_BLEND)) + ); + assert_eq!(particles.blend, Some(TRANSLUCENT_BLEND)); + assert!(post.particles_late && !post.translucent_late); + let ordering = Properties::parse( + "particles.ordering = before\nseparateEntityDraws = true\n", + ¯os, + ); + let post = load_post(&pack, "", &ordering, ¯os, &inputs); + assert!(!post.particles_late && post.translucent_late); let properties = Properties::parse( "blend.gbuffers_terrain = ONE ONE ZERO ONE\nblend.gbuffers_water = off\n", ¯os,