From 505e8e960f9956a51bde92a52b737d4f7f413c69 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:01:48 +0100 Subject: [PATCH 01/11] Shader packs, clean room 16: custom uniforms `uniform..` and `variable..` of shaders.properties (3.4): an expression parser with C precedence, the function table (math, vectors, `if`, `between`, `equals`, `smooth`, `random`...), evaluated on the CPU once per frame in file order after the frame's own uniforms. `smooth` keeps its state per call site and halves the distance to its target every fade time (OQ-26 working answer). A name neither the frame nor the pack defines reads 0 and is listed once in the log. Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, section 3.4; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx/pack_post.rs | 36 +- .../horizon-client/src/gfx/pack_uniforms.rs | 41 ++ crates/horizon-shaders/src/custom.rs | 570 ++++++++++++++++++ crates/horizon-shaders/src/lib.rs | 2 + crates/horizon-shaders/src/post.rs | 4 + crates/horizon-shaders/tests/real_packs.rs | 7 + 6 files changed, 656 insertions(+), 4 deletions(-) create mode 100644 crates/horizon-shaders/src/custom.rs diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index 81a517dba..6520565ed 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -13,7 +13,7 @@ use std::collections::HashMap; -use horizon_shaders::{Format, Group, Kind, Post}; +use horizon_shaders::{Format, Group, Kind, Post, Val}; use super::pack_uniforms::{self, FrameUniforms, PackInputs}; @@ -83,6 +83,8 @@ pub struct PackPost { passes: Vec, /// What draws terrain (`gbuffers_terrain` or its fallback), between `prepare` and `deferred`. terrain: Option, + /// `shaders.properties`' custom uniforms, after the frame's in `uniforms.values`. + custom: horizon_shaders::Custom, atlas: Option, lightmap: wgpu::TextureView, deferred_pre: u16, @@ -342,8 +344,16 @@ fn build_pass( screen: wgpu::TextureFormat, notes: &mut Vec, geometry: Option<&Geometry<'_>>, + custom: &[String], ) -> Option { let features = device.features(); + // The frame's uniforms, then the pack's custom ones. + let index = |name: &str| { + pack_uniforms::index(name).or_else(|| { + let k = custom.iter().position(|c| c == name)?; + Some(pack_uniforms::NAMES.len() + k) + }) + }; let mask = |flips: &[(u32, bool)]| flips.iter().filter(|f| f.1).fold(0u16, |m, f| m | 1 << f.0); let interface = &p.program.interface; let mut entries = vec![entry( @@ -499,7 +509,7 @@ fn build_pass( let missing: Vec<&str> = interface .uniforms .iter() - .filter(|u| !u.name.starts_with("hz_") && pack_uniforms::index(&u.name).is_none()) + .filter(|u| !u.name.starts_with("hz_") && index(&u.name).is_none()) .map(|u| u.name.as_str()) .collect(); if !missing.is_empty() { @@ -516,7 +526,7 @@ fn build_pass( uniforms: interface .uniforms .iter() - .map(|u| (pack_uniforms::index(&u.name), u.ty.clone(), u.offset)) + .map(|u| (index(&u.name), u.ty.clone(), u.offset)) .collect(), name: p.name, group: p.group, @@ -552,11 +562,27 @@ impl PackPost { }); let mut notes = post.notes; notes.extend(post.config.notes); + let mut custom = post.custom; + let custom_names: Vec = custom.uniforms().map(|u| u.0.to_owned()).collect(); + // A first run finds the names neither the frame nor the pack defines. + custom.eval(0.0, &|n| pack_uniforms::index(n).map(|_| Val::scalar(0.0))); + if !custom.notes.is_empty() { + notes.push(format!("custom uniforms: {}", custom.notes.join("; "))); + custom.notes.clear(); + } let mask = |flips: &[(u32, bool)]| flips.iter().filter(|f| f.1).fold(0u16, |m, f| m | 1 << f.0); let mut used = 1u16; let mut build = |p: horizon_shaders::PostProgram, geometry: Option<&Geometry<'_>>| { - let pass = build_pass(device, p, &formats, screen, &mut notes, geometry)?; + let pass = build_pass( + device, + p, + &formats, + screen, + &mut notes, + geometry, + &custom_names, + )?; used |= pass.reads | pass.written | pass.flip; Some(pass) }; @@ -646,6 +672,7 @@ impl PackPost { notes, passes, terrain, + custom, atlas: world.atlas.as_ref().map(|a| a.0.clone()), lightmap: world.lightmap, deferred_pre: mask(&post.deferred_pre), @@ -907,6 +934,7 @@ impl PackPost { ) { self.resize(device, size); self.uniforms.update(frame, inputs, size); + self.uniforms.eval_custom(&mut self.custom); let Some(targets) = &self.targets else { return }; let main = |m: u16, i: usize| usize::from(m & 1 << i != 0); let view = |m: u16, i: usize, alt: bool| { diff --git a/crates/horizon-client/src/gfx/pack_uniforms.rs b/crates/horizon-client/src/gfx/pack_uniforms.rs index 92eac4a75..5a27e62a0 100644 --- a/crates/horizon-client/src/gfx/pack_uniforms.rs +++ b/crates/horizon-client/src/gfx/pack_uniforms.rs @@ -304,6 +304,47 @@ impl FrameUniforms { } } +impl FrameUniforms { + /// Evaluates the pack's custom uniforms with this frame's values and puts them after + /// `NAMES`' values, in `custom.uniforms()` order. + pub fn eval_custom(&mut self, custom: &mut horizon_shaders::Custom) { + self.values.truncate(NAMES.len()); + let values = &self.values; + let builtin = |name: &str| { + let v = values.get(index(name)?)?; + let n = components(name); + Some(match *v { + Value::F(f) => horizon_shaders::Val::vector(&f.map(f64::from)[..n]), + Value::I(i) => horizon_shaders::Val::vector(&i.map(f64::from)[..n.min(3)]), + }) + }; + let dt = match index("frameTime").and_then(|i| values.get(i)) { + Some(Value::F(f)) => f64::from(f[0]), + _ => 0.0, + }; + let out = custom.eval(dt, &builtin); + self.values + .extend(out.iter().map(|v| Value::f(&v.v.map(|x| x as f32)[..v.n]))); + } +} + +/// How many components the uniform `name` has (16 for a matrix). +fn components(name: &str) -> usize { + match name { + "cameraPosition" + | "previousCameraPosition" + | "upPosition" + | "sunPosition" + | "moonPosition" + | "shadowLightPosition" + | "fogColor" + | "skyColor" => 3, + "eyeBrightness" | "eyeBrightnessSmooth" | "atlasSize" => 2, + n if n.starts_with("gbuffer") || n.starts_with("shadow") && n != "shadowAngle" => 16, + _ => 1, + } +} + /// The position of `name` in [`NAMES`]. pub fn index(name: &str) -> Option { NAMES.iter().position(|n| *n == name) diff --git a/crates/horizon-shaders/src/custom.rs b/crates/horizon-shaders/src/custom.rs new file mode 100644 index 000000000..34510247e --- /dev/null +++ b/crates/horizon-shaders/src/custom.rs @@ -0,0 +1,570 @@ +//! Custom uniforms and variables of `shaders.properties` (spec 3.4): `uniform..` and +//! `variable..` expressions, parsed once and evaluated on the CPU every frame in +//! file order. Uniforms reach every program; variables only later expressions. +//! +//! Decisions (impl-plan.md): +//! - OQ-25: numbers are doubles; booleans are 1 and 0; `%` is C's remainder (`fmod` floors); +//! comparisons and logic read the first component; a value takes its declared type at the +//! end (`int` truncates toward zero, `bool` is "not zero"). Unknown names read 0 and are +//! reported once. +//! - OQ-26: `smooth` runs per frame with the frame time; its fade times are seconds (1 by +//! default) to cover half of the remaining distance, `fadeIn` while rising, `fadeOut` while +//! falling. Each call keeps its own state, so the optional id is not needed. + +use std::collections::{HashMap, HashSet}; + +use crate::Properties; + +/// A value: up to 16 components (matrices only as names, for `m.row.column`). +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Val { + pub v: [f64; 16], + /// Components: 1 for a scalar. + pub n: usize, +} + +impl Val { + pub fn scalar(x: f64) -> Self { + let mut v = [0.0; 16]; + v[0] = x; + Self { v, n: 1 } + } + pub fn vector(c: &[f64]) -> Self { + let mut v = [0.0; 16]; + v[..c.len()].copy_from_slice(c); + Self { v, n: c.len() } + } + fn x(&self) -> f64 { + self.v[0] + } + fn truth(&self) -> bool { + self.v[0] != 0.0 + } + /// `f` per component; a scalar operand stretches to the other's size. + fn zip(self, o: Val, f: impl Fn(f64, f64) -> f64) -> Val { + let n = self.n.max(o.n).min(4); + let at = |a: &Val, i: usize| if a.n == 1 { a.v[0] } else { a.v[i] }; + let mut out = Val::scalar(0.0); + out.n = n; + for i in 0..n { + out.v[i] = f(at(&self, i), at(&o, i)); + } + out + } + fn map(self, f: impl Fn(f64) -> f64) -> Val { + let mut out = self; + for i in 0..self.n.min(4) { + out.v[i] = f(self.v[i]); + } + out + } +} + +#[derive(Clone, Debug)] +enum Expr { + Num(f64), + /// A name and its component (`x`, `2`, `2.1` as row 2, column 1). + Name(String, Option<(usize, Option)>), + Call(String, Vec, usize), + Neg(Box), + Not(Box), + Bin(&'static str, Box, Box), +} + +#[derive(Debug)] +struct Item { + name: String, + ty: String, + uniform: bool, + expr: Expr, +} + +/// The pack's custom uniforms and variables, with the state `smooth` and `random` keep. +#[derive(Debug, Default)] +pub struct Custom { + items: Vec, + /// `smooth` state per call site. + smooth: HashMap, + seed: u64, + unknown: HashSet, + /// Expressions that did not parse, and unknown names, as `key: message`. + pub notes: Vec, + calls: usize, +} + +impl Custom { + /// Reads every `uniform.*` and `variable.*` key of `properties`, in file order. + pub fn parse(properties: &Properties) -> Self { + let mut out = Custom { + seed: 0x9e37_79b9_7f4a_7c15, + ..Default::default() + }; + for e in &properties.entries { + let (uniform, rest) = match e.key.split_once('.') { + Some(("uniform", r)) => (true, r), + Some(("variable", r)) => (false, r), + _ => continue, + }; + let Some((ty, name)) = rest.split_once('.') else { + continue; + }; + if !matches!(ty, "float" | "int" | "bool" | "vec2" | "vec3" | "vec4") { + out.notes + .push(format!("{}: type `{ty}` is not supported", e.key)); + continue; + } + match Parser::new(&e.value, &mut out.calls).expr_all() { + Ok(expr) => out.items.push(Item { + name: name.to_owned(), + ty: ty.to_owned(), + uniform, + expr, + }), + Err(m) => out.notes.push(format!("{}: {m}", e.key)), + } + } + out + } + + /// The uniforms' names and declared types, in file order. + pub fn uniforms(&self) -> impl Iterator { + self.items + .iter() + .filter(|i| i.uniform) + .map(|i| (i.name.as_str(), i.ty.as_str())) + } + + /// Evaluates everything for a frame `dt` seconds after the last; `builtin` gives the + /// values of the game's uniforms and constants (`BIOME_*`...). Returns the uniforms' + /// values, in the order of `uniforms`. + pub fn eval(&mut self, dt: f64, builtin: &dyn Fn(&str) -> Option) -> Vec { + let mut vars: HashMap = HashMap::new(); + let mut out = Vec::new(); + let items = std::mem::take(&mut self.items); + for item in &items { + let v = self.value(&item.expr, dt, &vars, builtin); + let v = match item.ty.as_str() { + "int" => Val::scalar(v.x().trunc()), + "bool" => Val::scalar(f64::from(u8::from(v.truth()))), + "float" => Val::scalar(v.x()), + t => { + let n = (t.as_bytes()[3] - b'0') as usize; + let mut v = v; + if v.n == 1 { + let x = v.v[0]; + v.v[1..n].fill(x); + } + v.n = n; + v + } + }; + vars.insert(item.name.clone(), v); + if item.uniform { + out.push(v); + } + } + self.items = items; + out + } + + fn random(&mut self) -> f64 { + self.seed ^= self.seed << 13; + self.seed ^= self.seed >> 7; + self.seed ^= self.seed << 17; + (self.seed >> 11) as f64 / (1u64 << 53) as f64 + } + + fn value( + &mut self, + e: &Expr, + dt: f64, + vars: &HashMap, + builtin: &dyn Fn(&str) -> Option, + ) -> Val { + let ev = |e: &Expr, s: &mut Self| s.value(e, dt, vars, builtin); + match e { + Expr::Num(x) => Val::scalar(*x), + Expr::Name(name, component) => { + let v = match name.as_str() { + "pi" => Some(Val::scalar(std::f64::consts::PI)), + "true" => Some(Val::scalar(1.0)), + "false" => Some(Val::scalar(0.0)), + n => vars.get(n).copied().or_else(|| builtin(n)), + }; + let Some(v) = v else { + if self.unknown.insert(name.clone()) { + self.notes.push(format!("`{name}` is unknown; it reads 0")); + } + return Val::scalar(0.0); + }; + match component { + None => v, + // GLSL's m[column][row] is stored column after column. + Some((row, Some(column))) => Val::scalar(v.v[column * 4 + row]), + Some((i, None)) => Val::scalar(v.v[*i]), + } + } + Expr::Neg(a) => ev(a, self).map(|x| -x), + Expr::Not(a) => Val::scalar(f64::from(u8::from(!ev(a, self).truth()))), + Expr::Bin(op, a, b) => { + let (a, b) = (ev(a, self), ev(b, self)); + let bool_ = |c: bool| Val::scalar(f64::from(u8::from(c))); + match *op { + "+" => a.zip(b, |x, y| x + y), + "-" => a.zip(b, |x, y| x - y), + "*" => a.zip(b, |x, y| x * y), + "/" => a.zip(b, |x, y| x / y), + "%" => a.zip(b, |x, y| x % y), + "<" => bool_(a.x() < b.x()), + "<=" => bool_(a.x() <= b.x()), + ">" => bool_(a.x() > b.x()), + ">=" => bool_(a.x() >= b.x()), + "==" => bool_(a.x() == b.x()), + "!=" => bool_(a.x() != b.x()), + "&&" => bool_(a.truth() && b.truth()), + _ => bool_(a.truth() || b.truth()), + } + } + Expr::Call(f, args, site) => { + let a: Vec = args.iter().map(|e| ev(e, self)).collect(); + let arg = |i: usize| a.get(i).copied().unwrap_or(Val::scalar(0.0)); + let x = arg(0).x(); + let one = |g: fn(f64) -> f64| arg(0).map(g); + match f.as_str() { + "sin" => one(f64::sin), + "cos" => one(f64::cos), + "tan" => one(f64::tan), + "asin" => one(f64::asin), + "acos" => one(f64::acos), + "atan" if a.len() == 2 => Val::scalar(x.atan2(arg(1).x())), + "atan" => one(f64::atan), + "atan2" => Val::scalar(x.atan2(arg(1).x())), + "torad" | "radians" => one(f64::to_radians), + "todeg" | "degrees" => one(f64::to_degrees), + "min" => a + .iter() + .copied() + .reduce(|p, q| p.zip(q, f64::min)) + .unwrap_or(arg(0)), + "max" => a + .iter() + .copied() + .reduce(|p, q| p.zip(q, f64::max)) + .unwrap_or(arg(0)), + "clamp" => arg(0).zip(arg(1), f64::max).zip(arg(2), f64::min), + "abs" => one(f64::abs), + "floor" => one(f64::floor), + "ceil" => one(f64::ceil), + "exp" => one(f64::exp), + "exp2" => one(f64::exp2), + "exp10" => one(|v| 10f64.powf(v)), + "frac" => one(|v| v - v.floor()), + "log" if a.len() == 2 => Val::scalar(arg(1).x().log(x)), + "log" => one(f64::ln), + "log2" => one(f64::log2), + "log10" => one(f64::log10), + "sqrt" => one(f64::sqrt), + "round" => one(f64::round), + "signum" | "sign" => one(|v| if v == 0.0 { 0.0 } else { v.signum() }), + "pow" => arg(0).zip(arg(1), f64::powf), + "fmod" => arg(0).zip(arg(1), |p, q| p - q * (p / q).floor()), + "lerp" => arg(1).zip(arg(2), |p, q| p + (q - p) * x), + "mix" => arg(0).zip(arg(1), |p, q| p + (q - p) * arg(2).x()), + "edge" => Val::scalar(if arg(1).x() < x { 0.0 } else { 1.0 }), + "random" if a.len() == 2 => Val::scalar(x + (arg(1).x() - x) * self.random()), + "random" => Val::scalar(self.random()), + "randomInt" if a.len() == 2 => { + Val::scalar((x + (arg(1).x() - x) * self.random()).floor()) + } + "randomInt" => Val::scalar((self.random() * f64::from(i32::MAX)).floor()), + "if" | "ifb" => { + let mut i = 0; + while i + 1 < a.len() { + if a[i].truth() { + return a[i + 1]; + } + i += 2; + } + a.last().copied().unwrap_or(Val::scalar(0.0)) + } + "smooth" => { + let (val, fade_in, fade_out) = match a.len() { + 4 => (arg(1).x(), arg(2).x(), arg(3).x()), + 3 => (x, arg(1).x(), arg(2).x()), + 2 => (x, arg(1).x(), arg(1).x()), + _ => (x, 1.0, 1.0), + }; + let state = self.smooth.entry(*site).or_insert(val); + let fade = if val > *state { fade_in } else { fade_out }; + if fade > 0.0 { + *state = val + (*state - val) * 0.5f64.powf(dt / fade); + } else { + *state = val; + } + Val::scalar(*state) + } + "print" => arg(2), + "between" => { + Val::scalar(f64::from(u8::from(x >= arg(1).x() && x <= arg(2).x()))) + } + "equals" => { + Val::scalar(f64::from(u8::from((x - arg(1).x()).abs() <= arg(2).x()))) + } + "in" => Val::scalar(f64::from(u8::from(a[1..].iter().any(|v| v.x() == x)))), + "vec2" | "vec3" | "vec4" => { + let n = (f.as_bytes()[3] - b'0') as usize; + let mut c: Vec = a.iter().flat_map(|v| v.v[..v.n].to_vec()).collect(); + if c.len() == 1 { + c = vec![c[0]; n]; + } + c.resize(n, 0.0); + Val::vector(&c) + } + _ => { + if self.unknown.insert(f.clone()) { + self.notes.push(format!("`{f}()` is unknown; it gives 0")); + } + Val::scalar(0.0) + } + } + } + } + } +} + +struct Parser<'a> { + tokens: Vec, + at: usize, + calls: &'a mut usize, +} + +const OPERATORS: [&str; 18] = [ + "||", "&&", "==", "!=", "<=", ">=", "<", ">", "+", "-", "*", "/", "%", "!", "(", ")", ",", ".", +]; + +impl<'a> Parser<'a> { + fn new(text: &str, calls: &'a mut usize) -> Self { + let mut tokens = Vec::new(); + let b = text.as_bytes(); + let mut i = 0; + while i < b.len() { + let c = b[i] as char; + if c.is_whitespace() { + i += 1; + } else if c.is_ascii_digit() + || (c == '.' + && b.get(i + 1).is_some_and(u8::is_ascii_digit) + && !tokens + .last() + .is_some_and(|t: &String| t == "." || is_name(t))) + { + let start = i; + while i < b.len() + && (b[i].is_ascii_alphanumeric() + || b[i] == b'.' + || ((b[i] == b'-' || b[i] == b'+') && matches!(b[i - 1], b'e' | b'E'))) + { + i += 1; + } + tokens.push(text[start..i].to_owned()); + } else if c.is_ascii_alphabetic() || c == '_' { + let start = i; + while i < b.len() && (b[i].is_ascii_alphanumeric() || b[i] == b'_') { + i += 1; + } + tokens.push(text[start..i].to_owned()); + } else { + let op = OPERATORS + .iter() + .find(|o| text[i..].starts_with(**o)) + .map_or(&text[i..i + c.len_utf8()], |o| o); + i += op.len(); + tokens.push(op.to_owned()); + } + } + Parser { + tokens, + at: 0, + calls, + } + } + fn peek(&self) -> Option<&str> { + self.tokens.get(self.at).map(String::as_str) + } + fn eat(&mut self, t: &str) -> bool { + let hit = self.peek() == Some(t); + self.at += usize::from(hit); + hit + } + fn expr_all(&mut self) -> Result { + let e = self.binary(0)?; + match self.peek() { + None => Ok(e), + Some(t) => Err(format!("unexpected `{t}`")), + } + } + /// Precedence levels from the loosest: `||`, `&&`, equality, relations, sums, products. + fn binary(&mut self, level: usize) -> Result { + const LEVELS: [&[&str]; 6] = [ + &["||"], + &["&&"], + &["==", "!="], + &["<", "<=", ">", ">="], + &["+", "-"], + &["*", "/", "%"], + ]; + if level == LEVELS.len() { + return self.unary(); + } + let mut left = self.binary(level + 1)?; + while let Some(op) = self + .peek() + .and_then(|t| LEVELS[level].iter().find(|o| **o == t)) + { + self.at += 1; + let right = self.binary(level + 1)?; + left = Expr::Bin(op, Box::new(left), Box::new(right)); + } + Ok(left) + } + fn unary(&mut self) -> Result { + if self.eat("-") { + return Ok(Expr::Neg(Box::new(self.unary()?))); + } + if self.eat("!") { + return Ok(Expr::Not(Box::new(self.unary()?))); + } + self.eat("+"); + self.primary() + } + fn primary(&mut self) -> Result { + let Some(t) = self.peek().map(str::to_owned) else { + return Err("the expression ends early".into()); + }; + self.at += 1; + if t == "(" { + let e = self.binary(0)?; + return if self.eat(")") { + Ok(e) + } else { + Err("missing `)`".into()) + }; + } + if t.starts_with(|c: char| c.is_ascii_digit() || c == '.') { + return t + .trim_end_matches(['f', 'F']) + .parse() + .map(Expr::Num) + .map_err(|_| format!("bad number `{t}`")); + } + if !is_name(&t) { + return Err(format!("unexpected `{t}`")); + } + if self.eat("(") { + let mut args = Vec::new(); + if !self.eat(")") { + loop { + args.push(self.binary(0)?); + if self.eat(")") { + break; + } + if !self.eat(",") { + return Err(format!("missing `)` after the arguments of `{t}`")); + } + } + } + *self.calls += 1; + return Ok(Expr::Call(t, args, *self.calls)); + } + let component = if self.eat(".") { + let c = self.peek().map(str::to_owned).ok_or("nothing after `.`")?; + self.at += 1; + let index = |s: &str| match s { + "x" | "r" | "s" | "0" => Some(0), + "y" | "g" | "t" | "1" => Some(1), + "z" | "b" | "p" | "2" => Some(2), + "w" | "a" | "q" | "3" => Some(3), + _ => None, + }; + Some(match c.split_once('.') { + Some((r, col)) => ( + index(r).ok_or(format!("bad component `{c}`"))?, + Some(index(col).ok_or(format!("bad component `{c}`"))?), + ), + None => (index(&c).ok_or(format!("bad component `{c}`"))?, None), + }) + } else { + None + }; + Ok(Expr::Name(t, component)) + } +} + +fn is_name(t: &str) -> bool { + t.starts_with(|c: char| c.is_ascii_alphabetic() || c == '_') +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::Macros; + + fn custom(text: &str) -> Custom { + Custom::parse(&Properties::parse(text, &Macros::standard())) + } + + #[test] + fn expressions_follow_c_precedence_and_file_order() { + let mut c = custom( + "variable.float.a = 1 + 2 * 3 - 4 % 3\n\ + uniform.float.b = a / 2\n\ + uniform.bool.c = a > 5 && !(b < 1) || false\n\ + uniform.int.d = -7 / 2\n\ + uniform.vec3.e = vec3(a, cameraPosition.y, 2) * 2\n\ + uniform.float.f = if(a < 0, 1, a < 7, 2, 3) + clamp(fmod(-1, 4), 0, 2.5) + min(4, 2, 3)\n\ + uniform.float.g = gbufferModelView.1.0 + frac(sunAngle - 0.25) + pi * 0\n\ + uniform.float.h = unknownThing + in(biome, 3, BIOME_DESERT)\n", + ); + assert!(c.notes.is_empty(), "{:?}", c.notes); + let names: Vec<_> = c.uniforms().map(|u| u.0).collect(); + assert_eq!(names, ["b", "c", "d", "e", "f", "g", "h"]); + let builtin = |n: &str| match n { + "cameraPosition" => Some(Val::vector(&[1.0, 64.0, 2.0])), + "sunAngle" => Some(Val::scalar(0.125)), + "biome" => Some(Val::scalar(5.0)), + "BIOME_DESERT" => Some(Val::scalar(5.0)), + "gbufferModelView" => { + let mut m = Val::scalar(0.0); + m.v[1] = 9.0; // column 0, row 1 + m.n = 16; + Some(m) + } + _ => None, + }; + let v = c.eval(0.016, &builtin); + let x: Vec = v.iter().map(|v| v.v[0]).collect(); + assert_eq!(x, [3.0, 1.0, -3.0, 12.0, 2.0 + 2.5 + 2.0, 9.0 + 0.875, 1.0]); + assert_eq!(&v[3].v[..3], &[12.0, 128.0, 4.0]); + assert_eq!(c.notes, ["`unknownThing` is unknown; it reads 0"]); + } + + #[test] + fn smooth_halves_the_distance_per_fade_time() { + let mut c = custom("uniform.float.s = smooth(1, target, 2, 0.5)\n"); + let mut target = 0.0; + let mut run = |t: f64, dt: f64| { + target = t; + c.eval(dt, &|n: &str| (n == "target").then(|| Val::scalar(target)))[0].v[0] + }; + assert_eq!(run(0.0, 0.0), 0.0); + assert!((run(1.0, 2.0) - 0.5).abs() < 1e-9, "rising: fadeIn"); + assert!((run(0.0, 0.5) - 0.25).abs() < 1e-9, "falling: fadeOut"); + } + + #[test] + fn bad_expressions_are_reported() { + let c = custom("uniform.float.a = (1 + \nuniform.mat4.m = 1\nuniform.float.ok = 2\n"); + assert_eq!(c.uniforms().count(), 1); + assert_eq!(c.notes.len(), 2, "{:?}", c.notes); + } +} diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index d29a0a632..47cbcca11 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -14,6 +14,7 @@ mod condition; mod config; +mod custom; mod discover; mod lang; mod macros; @@ -26,6 +27,7 @@ mod translate; pub use condition::eval_condition; pub use config::{BUFFERS, Buffer, Config, Format, Kind, buffer_index, draw_buffers}; +pub use custom::{Custom, Val}; pub use discover::{Found, discover}; pub use lang::Lang; pub use macros::{MC_VERSION, Macros, RENDER_STAGES}; diff --git a/crates/horizon-shaders/src/post.rs b/crates/horizon-shaders/src/post.rs index c96730f0a..d6142c70a 100644 --- a/crates/horizon-shaders/src/post.rs +++ b/crates/horizon-shaders/src/post.rs @@ -13,6 +13,7 @@ use std::collections::BTreeSet; use crate::config::{self, Config}; +use crate::custom::Custom; use crate::{GbuffersInputs, Macros, Pack, Preprocessed, Program, Properties, Target}; use crate::{eval_condition, preprocess, translate_composite, translate_gbuffers}; @@ -62,6 +63,8 @@ pub struct Post { pub composite_pre: Vec<(u32, bool)>, /// What draws solid and cutout terrain, when the game asked for it and one loads. pub terrain: Option, + /// The custom uniforms of `shaders.properties` (3.4). + pub custom: Custom, pub config: Config, /// Programs skipped and why, as `program: message` or `file:line: message`. pub notes: Vec, @@ -93,6 +96,7 @@ pub fn load_post( deferred_pre: flips(properties, "deferred_pre"), composite_pre: flips(properties, "composite_pre"), terrain: None, + custom: Custom::parse(properties), config: Config::default(), notes: Vec::new(), }; diff --git a/crates/horizon-shaders/tests/real_packs.rs b/crates/horizon-shaders/tests/real_packs.rs index d0a28d71d..78ba97bfb 100644 --- a/crates/horizon-shaders/tests/real_packs.rs +++ b/crates/horizon-shaders/tests/real_packs.rs @@ -182,6 +182,13 @@ fn real_packs_read() { post.notes.len() + post.config.notes.len(), start.elapsed().as_secs_f64() * 1000.0 ); + let mut custom = post.custom; + custom.eval(0.016, &|_| None); + println!( + " {} custom uniforms; notes: {:?}", + custom.uniforms().count(), + custom.notes.iter().take(4).collect::>() + ); for n in post.notes.iter().chain(&post.config.notes).take(12) { println!(" {n}"); } From a2ec3fc791e773c69b852922ca53af1be298cd47 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:03:17 +0100 Subject: [PATCH 02/11] Shader packs, clean room 17: real packs' buffers and samplers - RGBA16 and R11F_G11F_B10F buffers get their own formats where the device has them (16-bit normalized render targets need the adapter's format features): the float stand-in kept NaN and values past 1, and BSL's TAA history went black for good. - Textures share one sampler variable per kind (linear, nearest, repeat, nearest-repeat, comparison): devices have as few as 16 sampler slots, Complementary's terrain program declares 23 textures. - A gbuffers pass reading the depth buffer or a buffer it draws into reads a blank texture instead (a feedback loop: undefined in GL, refused by wgpu, and a crash here). - A bind group layout that fails validation skips the program with the error's details instead of ending the game. - shadowModelView / shadowProjection (and inverses) for programs that read them before the shadow pass exists: an orthographic camera 160 blocks each side, toward the shadow light. - HORIZON_SHADER_PACK_SKIP=a,b leaves programs out (a developers' aid to bisect a pack). Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, sections 6.1, 6.7, 5.2; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 9 +- crates/horizon-client/src/gfx/pack_post.rs | 243 ++++++++++++++---- .../horizon-client/src/gfx/pack_uniforms.rs | 16 ++ 3 files changed, 209 insertions(+), 59 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index c117b190f..a33d54fa3 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -816,12 +816,17 @@ async fn open_device( continue; } // Mappable primary buffers: section pages that no longer fit in - // video memory go to system memory. + // video memory go to system memory. Shader packs' 16-bit normalized buffers (render + // targets with the adapter's own format features) and R11F_G11F_B10F: a float + // stand-in neither clamps nor drops NaN. let features = timing::GpuTimer::features(&adapter) | cull::Cull::features(&adapter) | (adapter.features() & (wgpu::Features::INDIRECT_FIRST_INSTANCE - | wgpu::Features::MAPPABLE_PRIMARY_BUFFERS)); + | wgpu::Features::MAPPABLE_PRIMARY_BUFFERS + | wgpu::Features::TEXTURE_FORMAT_16BIT_NORM + | wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES + | wgpu::Features::RG11B10UFLOAT_RENDERABLE)); let driver = format!("{} {}", info.driver, info.driver_info); startup_step(format_args!( "requesting the device (driver {}) with features [{features}] and the adapter's \ diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index 6520565ed..0201c7976 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -39,6 +39,82 @@ struct Binding { binding: u32, /// A filtering sampler (the texture is filterable). filtering: bool, + /// Where this texture's sampler object is bound: the first texture of each kind binds it + /// (`binding + 1`), the others share it (devices have as few as 16 sampler slots). + sampler: u32, +} + +/// The sampler objects a pass binds, one slot per kind it uses. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +enum SamplerKind { + Linear, + Nearest, + Repeat, + NearestRepeat, + Comparison, +} + +impl SamplerKind { + fn of(source: Source, filtering: bool) -> Self { + match source { + Source::Color(_) | Source::Dummy if !filtering => Self::Nearest, + Source::Depth => Self::Nearest, + Source::Noise => Self::Repeat, + Source::Shadow => Self::Comparison, + // Blocks look as the game draws them: nearest texels. + Source::Atlas => Self::NearestRepeat, + Source::Color(_) | Source::Dummy | Source::Lightmap => Self::Linear, + } + } + + fn binding_type(self) -> wgpu::SamplerBindingType { + match self { + Self::Linear | Self::Repeat => wgpu::SamplerBindingType::Filtering, + Self::Nearest | Self::NearestRepeat => wgpu::SamplerBindingType::NonFiltering, + Self::Comparison => wgpu::SamplerBindingType::Comparison, + } + } +} + +/// Points every sampler object of `module` at the slot `slot` gives its binding, so that +/// samplers sharing a slot become one variable (two variables may not share a binding). +fn share_samplers(module: &mut naga::Module, slot: &HashMap) { + let mut first = HashMap::new(); + let mut same = HashMap::new(); + for (h, g) in module.global_variables.iter_mut() { + let Some(b) = g.binding.as_mut() else { + continue; + }; + if !matches!(module.types[g.ty].inner, naga::TypeInner::Sampler { .. }) { + continue; + } + let Some(&to) = slot.get(&b.binding) else { + continue; + }; + b.binding = to; + match first.entry(to) { + std::collections::hash_map::Entry::Occupied(e) => _ = same.insert(h, *e.get()), + std::collections::hash_map::Entry::Vacant(e) => _ = e.insert(h), + } + } + if same.is_empty() { + return; + } + let fix = |expressions: &mut naga::Arena| { + for (_, e) in expressions.iter_mut() { + if let naga::Expression::GlobalVariable(g) = e + && let Some(&to) = same.get(g) + { + *g = to; + } + } + }; + for (_, f) in module.functions.iter_mut() { + fix(&mut f.expressions); + } + for ep in &mut module.entry_points { + fix(&mut ep.function.expressions); + } } struct Pass { @@ -128,7 +204,12 @@ fn texture_format(f: Format, features: wgpu::Features) -> wgpu::TextureFormat { let half = pick(T::R16Float, T::Rg16Float, T::Rgba16Float); match (f.kind, f.bits) { (Kind::Unorm, 10) => T::Rgb10a2Unorm, - (Kind::Unorm, 16) if features.contains(wgpu::Features::TEXTURE_FORMAT_16BIT_NORM) => { + (Kind::Unorm, 16) + if features.contains( + wgpu::Features::TEXTURE_FORMAT_16BIT_NORM + | wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES, + ) => + { pick(T::R16Unorm, T::Rg16Unorm, T::Rgba16Unorm) } (Kind::Unorm, b) if b <= 8 => pick(T::R8Unorm, T::Rg8Unorm, T::Rgba8Unorm), @@ -229,8 +310,11 @@ fn checked(device: &wgpu::Device, create: impl FnOnce() -> T) -> Result { - // The details are in the error's sources. - let mut message = e.to_string(); + // The details are in the error's description or its sources. + let mut message = match &e { + wgpu::Error::Validation { source, .. } => format!("{source:?}"), + e => e.to_string(), + }; let mut source = std::error::Error::source(&e); while let Some(s) = source { message = format!("{message}: {s}"); @@ -365,12 +449,22 @@ fn build_pass( }, )]; let mut samplers = Vec::new(); + let (mut kinds, mut slots) = (HashMap::new(), HashMap::new()); let mut reads = 0u16; + // A gbuffers pass draws into its buffers and the depth buffer: reading them as well would + // be a feedback loop (undefined in GL, refused by wgpu), so those read a blank texture. + let in_place = match geometry { + Some(_) => p.buffers.iter().fold(0u16, |m, &b| m | 1 << b), + None => 0, + }; for s in &interface.samplers { let mut src = source(&s.name); - if src == Source::Atlas && geometry.is_none() { - src = Source::Dummy; - } + let blank = match src { + Source::Atlas => geometry.is_none(), + Source::Depth => geometry.is_some(), + Source::Color(n) => in_place & 1 << n != 0, + _ => false, + }; let float = wgpu::TextureSampleType::Float { filterable: true }; let mut sample_type = match src { Source::Color(n) => formats[n] @@ -399,25 +493,32 @@ fn build_pass( )); (src, sample_type) = (Source::Dummy, float); } + if blank { + // The blank texture is float: an integer sampler then fails the pipeline (a note). + src = Source::Dummy; + if int { + sample_type = float; + } + } let filtering = sample_type == float; if let Source::Color(n) = src { reads |= 1 << n; } entries.push(texture_entry(s.binding, sample_type)); - entries.push(entry( - s.binding + 1, - wgpu::BindingType::Sampler(if src == Source::Shadow { - wgpu::SamplerBindingType::Comparison - } else if filtering { - wgpu::SamplerBindingType::Filtering - } else { - wgpu::SamplerBindingType::NonFiltering - }), - )); + let kind = SamplerKind::of(src, filtering); + let sampler = *kinds.entry(kind).or_insert_with(|| { + entries.push(entry( + s.binding + 1, + wgpu::BindingType::Sampler(kind.binding_type()), + )); + s.binding + 1 + }); + slots.insert(s.binding + 1, sampler); samplers.push(Binding { source: src, binding: s.binding, filtering, + sampler, }); } let final_pass = p.group == Group::Final; @@ -453,24 +554,25 @@ fn build_pass( } else { (written & !kept) | mask(&p.flips) }; - let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - label: Some(&p.name), - entries: &entries, - }); - let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - label: Some(&p.name), - bind_group_layouts: &[Some(&layout), geometry.and_then(|g| g.table)], - ..Default::default() - }); let created = checked(device, || { - let module = |m: naga::Module| { + let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some(&p.name), + entries: &entries, + }); + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some(&p.name), + bind_group_layouts: &[Some(&layout), geometry.and_then(|g| g.table)], + ..Default::default() + }); + let module = |mut m: naga::Module| { + share_samplers(&mut m, &slots); device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some(&p.name), source: wgpu::ShaderSource::Naga(std::borrow::Cow::Owned(m)), }) }; let (vs, fs) = (module(p.program.vertex), module(p.program.fragment)); - device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: Some(&p.name), layout: Some(&pipeline_layout), vertex: wgpu::VertexState { @@ -496,10 +598,11 @@ fn build_pass( }), multiview_mask: None, cache: None, - }) + }); + (layout, pipeline) }); - let pipeline = match created { - Ok(p) => p, + let (layout, pipeline) = match created { + Ok(created) => created, Err(e) => { notes.push(format!("{}: skipped: {e}", p.name)); return None; @@ -590,9 +693,12 @@ impl PackPost { .terrain .zip(world.terrain.as_ref()) .and_then(|(p, g)| build(p, Some(g))); + // Developers' aid: `HORIZON_SHADER_PACK_SKIP=composite,final` leaves programs out. + let skip = std::env::var("HORIZON_SHADER_PACK_SKIP").unwrap_or_default(); let passes: Vec = post .programs .into_iter() + .filter(|p| !skip.split(',').any(|s| s == p.name)) .filter_map(|p| build(p, None)) .collect(); if let Some(t) = &terrain { @@ -775,37 +881,34 @@ impl PackPost { resource: pass.ubo.as_entire_binding(), }]; for s in &pass.samplers { - let filter = if s.filtering { - &self.linear - } else { - &self.nearest + let texture = match s.source { + Source::Color(i) => targets.color[i] + .as_ref() + .map_or(&self.dummy, |t| &t[usize::from(key & 1 << i != 0)]), + Source::Depth => &targets.depth, + Source::Noise => &self.noise, + Source::Dummy => &self.dummy, + Source::Shadow => &self.shadow.0, + Source::Atlas => self.atlas.as_ref().unwrap_or(&self.dummy), + Source::Lightmap => &self.lightmap, }; - let (texture, sampler) = match s.source { - Source::Color(i) => ( - targets.color[i] - .as_ref() - .map_or(&self.dummy, |t| &t[usize::from(key & 1 << i != 0)]), - filter, - ), - Source::Depth => (&targets.depth, &self.nearest), - Source::Noise => (&self.noise, &self.repeat), - Source::Dummy => (&self.dummy, &self.linear), - Source::Shadow => (&self.shadow.0, &self.shadow.1), - // Blocks look as the game draws them: nearest texels. - Source::Atlas => ( - self.atlas.as_ref().unwrap_or(&self.dummy), - &self.nearest_repeat, - ), - Source::Lightmap => (&self.lightmap, &self.linear), + let sampler = match SamplerKind::of(s.source, s.filtering) { + SamplerKind::Linear => &self.linear, + SamplerKind::Nearest => &self.nearest, + SamplerKind::Repeat => &self.repeat, + SamplerKind::NearestRepeat => &self.nearest_repeat, + SamplerKind::Comparison => &self.shadow.1, }; entries.push(wgpu::BindGroupEntry { binding: s.binding, resource: wgpu::BindingResource::TextureView(texture), }); - entries.push(wgpu::BindGroupEntry { - binding: s.binding + 1, - resource: wgpu::BindingResource::Sampler(sampler), - }); + if s.sampler == s.binding + 1 { + entries.push(wgpu::BindGroupEntry { + binding: s.sampler, + resource: wgpu::BindingResource::Sampler(sampler), + }); + } } device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some(&pass.name), @@ -1130,6 +1233,31 @@ mod tests { use super::*; use wgpu::TextureFormat as T; + /// Two textures with a sampler each become two textures sharing one sampler variable. + #[test] + fn samplers_of_a_kind_share_one_variable() { + let mut module = naga::front::wgsl::parse_str( + "@group(0) @binding(1) var a: texture_2d;\n\ + @group(0) @binding(2) var sa: sampler;\n\ + @group(0) @binding(3) var b: texture_2d;\n\ + @group(0) @binding(4) var sb: sampler;\n\ + @fragment fn main() -> @location(0) vec4 {\n\ + return textureSample(a, sa, vec2(0.5)) + textureSample(b, sb, vec2(0.5));\n}\n", + ) + .unwrap(); + share_samplers(&mut module, &HashMap::from([(2, 2), (4, 2)])); + let info = naga::valid::Validator::new(Default::default(), Default::default()) + .validate(&module) + .unwrap(); + let used: Vec = module + .global_variables + .iter() + .filter(|(h, _)| !info.get_entry_point(0)[*h].is_empty()) + .filter_map(|(_, g)| g.binding.as_ref().map(|b| b.binding)) + .collect(); + assert_eq!(used, [1, 2, 3]); + } + #[test] fn pack_formats_map_to_renderable_ones() { let f = |name| texture_format(Format::parse(name).unwrap(), wgpu::Features::empty()); @@ -1142,8 +1270,9 @@ mod tests { assert_eq!(f("RGBA16"), T::Rgba16Float, "16-bit norm needs a feature"); assert_eq!(f("RGBA8_SNORM"), T::Rgba16Float, "snorm is not renderable"); assert_eq!(f("R11F_G11F_B10F"), T::Rgba16Float); - let all = - wgpu::Features::TEXTURE_FORMAT_16BIT_NORM | wgpu::Features::RG11B10UFLOAT_RENDERABLE; + let all = wgpu::Features::TEXTURE_FORMAT_16BIT_NORM + | wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES + | wgpu::Features::RG11B10UFLOAT_RENDERABLE; let g = |name| texture_format(Format::parse(name).unwrap(), all); assert_eq!(g("RGBA16"), T::Rgba16Unorm); assert_eq!(g("R11F_G11F_B10F"), T::Rg11b10Ufloat); diff --git a/crates/horizon-client/src/gfx/pack_uniforms.rs b/crates/horizon-client/src/gfx/pack_uniforms.rs index 5a27e62a0..c7c4cfd8b 100644 --- a/crates/horizon-client/src/gfx/pack_uniforms.rs +++ b/crates/horizon-client/src/gfx/pack_uniforms.rs @@ -141,6 +141,10 @@ pub const NAMES: &[&str] = &[ "gbufferPreviousModelView", "gbufferPreviousProjection", "atlasSize", + "shadowModelView", + "shadowModelViewInverse", + "shadowProjection", + "shadowProjectionInverse", ]; /// Values carried from frame to frame. @@ -226,6 +230,14 @@ impl FrameUniforms { ), None => (frame.sky.fog_color, frame.distance * 0.9, frame.distance), }; + // The shadow camera (OQ-6, impl-plan.md): orthographic, `shadowDistance` (160) blocks + // each side, 100 blocks from the player toward the shadow light, world +z up. + let light = view + .inverse() + .transform_vector3(if day { sun } else { moon }) + .normalize(); + let shadow_view = Mat4::look_at_rh(light * 100.0, Vec3::ZERO, Vec3::Z); + let shadow_projection = Mat4::orthographic_rh_gl(-160.0, 160.0, -160.0, 160.0, 0.05, 256.0); let day_time = inputs.day_time.rem_euclid(24000) as i32; let world_day = inputs.day_time.div_euclid(24000); let seconds = (now - self.start).as_secs_f32() % 3600.0; @@ -247,6 +259,10 @@ impl FrameUniforms { "worldDay" => Value::i(&[world_day as i32]), "moonPhase" => Value::i(&[world_day.rem_euclid(8) as i32]), "frameCounter" => Value::i(&[self.frame as i32]), + "shadowModelView" => m4(shadow_view), + "shadowModelViewInverse" => m4(shadow_view.inverse()), + "shadowProjection" => m4(shadow_projection), + "shadowProjectionInverse" => m4(shadow_projection.inverse()), "atlasSize" => Value::i(&[self.atlas[0] as i32, self.atlas[1] as i32]), "frameTime" => Value::f(&[dt]), "frameTimeCounter" => Value::f(&[seconds]), From 3e2c30a852452a3ee0a4cf778d85aee55b98c205 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:47:14 +0100 Subject: [PATCH 03/11] Shader packs, clean room 18: translucent terrain drawn by the pack With a pack whose gbuffers_water (or a fallback down gbuffers_terrain's chain) loads, translucent terrain (water, stained glass) is drawn by it after the deferred programs and before the composites, in place, sorted back to front as the game sorts it, and Horizon's own translucent terrain draw is skipped. - blend. (off, or four GL factors); translucent terrain defaults to SRC_ALPHA ONE_MINUS_SRC_ALPHA ONE ONE_MINUS_SRC_ALPHA, and only blendable buffer formats blend. - depthtex1/depthtex2: depth without translucent terrain, a copy taken just before it when a later program reads them (before it, depthtex0 itself). The hand is drawn after the pack, so depthtex2 = depthtex1. Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, sections 2.1, 2.3, 3.2 (blend), 6.2 (depthtex1/2); no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 26 ++- crates/horizon-client/src/gfx/pack_post.rs | 213 +++++++++++++++++---- crates/horizon-shaders/src/lib.rs | 4 +- crates/horizon-shaders/src/post.rs | 130 ++++++++++++- crates/horizon-shaders/tests/real_packs.rs | 3 +- 5 files changed, 327 insertions(+), 49 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index a33d54fa3..9a7356d6e 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -3709,6 +3709,12 @@ impl Renderer { let pack_post = !frame.title && self.pack_post.as_ref().is_some_and(|p| p.1.is_some()); // The pack's program reads the vertices this frame's draws bind. let pack_draws_terrain = pack_terrain && flat_frame == self.pack_terrain_flat; + let pack_draws_water = pack_draws_terrain + && self + .pack_post + .as_ref() + .and_then(|p| p.1.as_ref()) + .is_some_and(|p| p.has_water()); let scaled = self.world_post.prepare( &self.device, [self.config.width, self.config.height], @@ -3940,6 +3946,8 @@ impl Renderer { pass.draw(0..4, debug_depth..debug_depth + debug_top); } match self.flat.as_ref().filter(|_| flat_frame) { + // The pack draws translucent terrain after its deferred programs. + _ if pack_draws_water => {} Some(flat) => { pass.set_pipeline(&flat.translucent); flat.draw(&mut pass, &flat.calls[flat.solid_calls..], |p| { @@ -4016,20 +4024,28 @@ impl Renderer { { let size = self.world_post.size().unwrap_or([1, 1]); let flat = self.flat.as_ref().filter(|_| flat_frame); - let mut terrain = |rp: &mut wgpu::RenderPass<'_>| { + let mut terrain = |rp: &mut wgpu::RenderPass<'_>, translucent: bool| { if !pack_draws_terrain { return; } match flat { Some(flat) => { - flat.draw(rp, &flat.calls[..flat.solid_calls], |p| { - self.arena.buffer(p) - }); + let calls = if translucent { + &flat.calls[flat.solid_calls..] + } else { + &flat.calls[..flat.solid_calls] + }; + flat.draw(rp, calls, |p| self.arena.buffer(p)); } None => { rp.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); rp.set_vertex_buffer(1, self.instances.slice(..)); - self.draw_sections(rp, &self.indirect, 0, 0, solid_draws); + let (first, count) = if translucent { + (solid_draws, translucent_draws) + } else { + (0, solid_draws) + }; + self.draw_sections(rp, &self.indirect, 0, first, count); } } }; diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index 0201c7976..b96da2599 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -24,6 +24,8 @@ const BUFFERS: usize = horizon_shaders::BUFFERS; enum Source { Color(usize), Depth, + /// `depthtex1`/`depthtex2`: depth without translucent terrain (a copy taken before it). + Depth1, Noise, /// A 1×1 stand-in for names this slice does not provide yet (custom textures...). Dummy, @@ -58,7 +60,7 @@ impl SamplerKind { fn of(source: Source, filtering: bool) -> Self { match source { Source::Color(_) | Source::Dummy if !filtering => Self::Nearest, - Source::Depth => Self::Nearest, + Source::Depth | Source::Depth1 => Self::Nearest, Source::Noise => Self::Repeat, Source::Shadow => Self::Comparison, // Blocks look as the game draws them: nearest texels. @@ -143,6 +145,9 @@ struct Targets { /// A and B of each buffer used. color: [Option<[wgpu::TextureView; 2]>; BUFFERS], depth: wgpu::TextureView, + depth_texture: wgpu::Texture, + /// `depthtex1`, when a program after translucent terrain reads it. + depth1: Option<(wgpu::Texture, wgpu::TextureView)>, } /// What a pack's gbuffers programs read and draw from the game's world. @@ -159,6 +164,10 @@ pub struct PackPost { passes: Vec, /// What draws terrain (`gbuffers_terrain` or its fallback), between `prepare` and `deferred`. terrain: Option, + /// What draws translucent terrain (`gbuffers_water` or its fallback), after `deferred`. + water: Option, + /// A program after translucent terrain reads `depthtex1`: it is copied before water. + depth1: bool, /// `shaders.properties`' custom uniforms, after the frame's in `uniforms.values`. custom: horizon_shaders::Custom, atlas: Option, @@ -231,7 +240,9 @@ fn texture_format(f: Format, features: wgpu::Features) -> wgpu::TextureFormat { fn source(name: &str) -> Source { match name { - "depthtex0" | "gdepthtex" | "depthtex1" | "depthtex2" => Source::Depth, + "depthtex0" | "gdepthtex" => Source::Depth, + // The hand is drawn after the pack, so `depthtex2` (without it) is `depthtex1`. + "depthtex1" | "depthtex2" => Source::Depth1, "noisetex" => Source::Noise, "texture" | "gtexture" | "tex" => Source::Atlas, "lightmap" => Source::Lightmap, @@ -392,6 +403,34 @@ fn texture_entry(binding: u32, sample_type: wgpu::TextureSampleType) -> wgpu::Bi ) } +/// `blend.`'s factors as wgpu's, adding both terms. +fn blend_state(b: horizon_shaders::Blend) -> wgpu::BlendState { + use horizon_shaders::Factor as F; + use wgpu::BlendFactor as W; + let f = |f| match f { + F::Zero => W::Zero, + F::One => W::One, + F::SrcColor => W::Src, + F::OneMinusSrcColor => W::OneMinusSrc, + F::DstColor => W::Dst, + F::OneMinusDstColor => W::OneMinusDst, + F::SrcAlpha => W::SrcAlpha, + F::OneMinusSrcAlpha => W::OneMinusSrcAlpha, + F::DstAlpha => W::DstAlpha, + F::OneMinusDstAlpha => W::OneMinusDstAlpha, + F::SrcAlphaSaturate => W::SrcAlphaSaturated, + }; + let component = |src, dst| wgpu::BlendComponent { + src_factor: f(src), + dst_factor: f(dst), + operation: wgpu::BlendOperation::Add, + }; + wgpu::BlendState { + color: component(b[0], b[1]), + alpha: component(b[2], b[3]), + } +} + fn target(format: wgpu::TextureFormat) -> Option { Some(wgpu::ColorTargetState { format, @@ -462,6 +501,8 @@ fn build_pass( let blank = match src { Source::Atlas => geometry.is_none(), Source::Depth => geometry.is_some(), + // Translucent terrain reads the copy; opaque terrain would read itself. + Source::Depth1 => p.group == Group::Gbuffers, Source::Color(n) => in_place & 1 << n != 0, _ => false, }; @@ -470,7 +511,7 @@ fn build_pass( Source::Color(n) => formats[n] .sample_type(None, Some(features)) .unwrap_or(float), - Source::Depth => wgpu::TextureSampleType::Float { filterable: false }, + Source::Depth | Source::Depth1 => wgpu::TextureSampleType::Float { filterable: false }, // Unfilterable on WebGPU-level devices; the lightmap is sampled at texel centres. Source::Lightmap => float, _ => float, @@ -534,7 +575,17 @@ fn build_pass( targets[0] = target(screen); } } else if let Some(&buffer) = p.buffers.get(l) { - targets[l] = target(formats[buffer as usize]); + let format = formats[buffer as usize]; + let mut state = target(format); + if let (Some(t), Some(b)) = (&mut state, p.blend) + && format + .guaranteed_format_features(features) + .flags + .contains(wgpu::TextureFormatFeatureFlags::BLENDABLE) + { + t.blend = Some(blend_state(b)); + } + targets[l] = state; writes.push((location, buffer as usize)); } } @@ -693,6 +744,10 @@ impl PackPost { .terrain .zip(world.terrain.as_ref()) .and_then(|(p, g)| build(p, Some(g))); + let water = post + .water + .zip(world.terrain.as_ref()) + .and_then(|(p, g)| build(p, Some(g))); // Developers' aid: `HORIZON_SHADER_PACK_SKIP=composite,final` leaves programs out. let skip = std::env::var("HORIZON_SHADER_PACK_SKIP").unwrap_or_default(); let passes: Vec = post @@ -704,25 +759,43 @@ impl PackPost { if let Some(t) = &terrain { notes.push(format!("terrain draws with {}", t.name)); } + if let Some(w) = &water { + notes.push(format!("translucent terrain draws with {}", w.name)); + } + let depth1 = water.is_some() + && passes + .iter() + .filter(|p| matches!(p.group, Group::Composite | Group::Final)) + .chain(&water) + .any(|p| p.samplers.iter().any(|s| s.source == Source::Depth1)); // A clear only matters when a program reads the buffer before one writes all of it // (composite passes cover their whole target, terrain does not); the others are skipped. let mut cleared = Vec::new(); for b in 1..BUFFERS { let bit = 1u16 << b; let touched = |p: &&Pass| (p.reads | p.flip | p.written) & bit != 0; - let first = terrain.iter().find(touched).map(|_| true).or_else(|| { - let p = passes.iter().find(touched)?; - Some(p.reads & bit != 0 || p.written & bit == 0) - }); + // Gbuffers passes cover part of their targets: a clear is needed if they touch it. + let first = terrain + .iter() + .chain(&water) + .find(touched) + .map(|_| true) + .or_else(|| { + let p = passes.iter().find(touched)?; + Some(p.reads & bit != 0 || p.written & bit == 0) + }); if clears[b].is_some() && first == Some(true) { cleared.push(b); } } // Terrain tests against the world's depth (entities...), so the blit copies it then. let blit_depth = terrain.is_some() - || passes - .iter() - .any(|p| p.samplers.iter().any(|s| s.source == Source::Depth)); + || water.is_some() + || passes.iter().any(|p| { + p.samplers + .iter() + .any(|s| matches!(s.source, Source::Depth | Source::Depth1)) + }); let room = 8 - 1; let other_clears = cleared.split_off(room.min(cleared.len())); let float = wgpu::TextureSampleType::Float { filterable: true }; @@ -778,6 +851,8 @@ impl PackPost { notes, passes, terrain, + water, + depth1, custom, atlas: world.atlas.as_ref().map(|a| a.0.clone()), lightmap: world.lightmap, @@ -886,6 +961,18 @@ impl PackPost { .as_ref() .map_or(&self.dummy, |t| &t[usize::from(key & 1 << i != 0)]), Source::Depth => &targets.depth, + // Before translucent terrain the two are the same. + Source::Depth1 => match &targets.depth1 { + Some(d) + if matches!( + pass.group, + Group::Composite | Group::Final | Group::Translucent + ) => + { + &d.1 + } + _ => &targets.depth, + }, Source::Noise => &self.noise, Source::Dummy => &self.dummy, Source::Shadow => &self.shadow.0, @@ -919,19 +1006,36 @@ impl PackPost { group.clone() } - /// Terrain with the pack's program into the buffers it writes, in place, tested against - /// the world's depth; `draw` issues the game's draws. + /// Terrain (or, `translucent`, translucent terrain) with the pack's program into the + /// buffers it writes, in place, tested against the world's depth; `draw` issues the + /// game's draws. fn draw_terrain( &mut self, device: &wgpu::Device, queue: &wgpu::Queue, encoder: &mut wgpu::CommandEncoder, size: [u32; 2], - draw: &mut dyn FnMut(&mut wgpu::RenderPass<'_>), + translucent: bool, + draw: &mut dyn FnMut(&mut wgpu::RenderPass<'_>, bool), ) { - let Some(mut pass) = self.terrain.take() else { + let slot = if translucent { + &mut self.water + } else { + &mut self.terrain + }; + let Some(mut pass) = slot.take() else { return; }; + if translucent + && let Some(t) = &self.targets + && let Some((copy, _)) = &t.depth1 + { + encoder.copy_texture_to_texture( + t.depth_texture.as_image_copy(), + copy.as_image_copy(), + t.depth_texture.size(), + ); + } let group = self.bind(device, queue, &mut pass, size); let targets = self.targets.as_ref().expect("resized"); let load = wgpu::Operations { @@ -964,9 +1068,13 @@ impl PackPost { }); rp.set_pipeline(&pass.pipeline); rp.set_bind_group(0, &group, &[]); - draw(&mut rp); + draw(&mut rp, translucent); + } + if translucent { + self.water = Some(pass); + } else { + self.terrain = Some(pass); } - self.terrain = Some(pass); } /// Whether the pack draws terrain itself. @@ -974,6 +1082,11 @@ impl PackPost { self.terrain.is_some() } + /// Whether the pack draws translucent terrain itself. + pub fn has_water(&self) -> bool { + self.water.is_some() + } + /// The names of the programs that run, in order. pub fn programs(&self) -> Vec<&str> { self.passes.iter().map(|p| p.name.as_str()).collect() @@ -983,23 +1096,24 @@ impl PackPost { if self.targets.as_ref().is_some_and(|t| t.size == size) { return; } + let texture = |label: &str, format, usage| { + device.create_texture(&wgpu::TextureDescriptor { + label: Some(label), + size: wgpu::Extent3d { + width: size[0], + height: size[1], + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage: usage | wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }) + }; let make = |label: &str, format| { - device - .create_texture(&wgpu::TextureDescriptor { - label: Some(label), - size: wgpu::Extent3d { - width: size[0], - height: size[1], - depth_or_array_layers: 1, - }, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format, - usage: wgpu::TextureUsages::RENDER_ATTACHMENT - | wgpu::TextureUsages::TEXTURE_BINDING, - view_formats: &[], - }) + texture(label, format, wgpu::TextureUsages::RENDER_ATTACHMENT) .create_view(&Default::default()) }; let color = std::array::from_fn(|i| { @@ -1008,12 +1122,26 @@ impl PackPost { [make(&label, self.formats[i]), make(&label, self.formats[i])] }) }); + let depth32 = wgpu::TextureFormat::Depth32Float; + let depth_texture = texture( + "depthtex0", + depth32, + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC, + ); + let depth1 = self.depth1.then(|| { + let t = texture("depthtex1", depth32, wgpu::TextureUsages::COPY_DST); + let view = t.create_view(&Default::default()); + (t, view) + }); self.targets = Some(Targets { size, color, - depth: make("depthtex0", wgpu::TextureFormat::Depth32Float), + depth: depth_texture.create_view(&Default::default()), + depth_texture, + depth1, }); - for pass in self.passes.iter_mut().chain(&mut self.terrain) { + let gbuffers = self.terrain.iter_mut().chain(&mut self.water); + for pass in self.passes.iter_mut().chain(gbuffers) { pass.groups.clear(); } } @@ -1033,7 +1161,7 @@ impl PackPost { inputs: &PackInputs, size: [u32; 2], window: [u32; 2], - terrain: &mut dyn FnMut(&mut wgpu::RenderPass<'_>), + terrain: &mut dyn FnMut(&mut wgpu::RenderPass<'_>, bool), ) { self.resize(device, size); self.uniforms.update(frame, inputs, size); @@ -1143,10 +1271,15 @@ impl PackPost { let mut seen = (false, false); let mut shown = false; let mut terrain_drawn = self.terrain.is_none(); + let mut water_drawn = self.water.is_none(); for pass in &mut passes { if !terrain_drawn && pass.group != Group::Prepare { terrain_drawn = true; - self.draw_terrain(device, queue, encoder, size, terrain); + self.draw_terrain(device, queue, encoder, size, false, terrain); + } + if !water_drawn && matches!(pass.group, Group::Composite | Group::Final) { + water_drawn = true; + self.draw_terrain(device, queue, encoder, size, true, terrain); } if pass.group == Group::Deferred && !seen.0 { seen.0 = true; @@ -1191,7 +1324,10 @@ impl PackPost { } self.passes = passes; if !terrain_drawn { - self.draw_terrain(device, queue, encoder, size, terrain); + self.draw_terrain(device, queue, encoder, size, false, terrain); + } + if !water_drawn { + self.draw_terrain(device, queue, encoder, size, true, terrain); } let targets = self.targets.as_ref().expect("resized"); let view = |m: u16, i: usize, alt: bool| { @@ -1282,6 +1418,7 @@ mod tests { fn sampler_names() { assert!(source("gaux1") == Source::Color(4)); assert!(source("gdepthtex") == Source::Depth); + assert!(source("depthtex2") == Source::Depth1); assert!(source("noisetex") == Source::Noise); assert!(source("shadowcolor0") == Source::Dummy); } diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index 47cbcca11..f5819d435 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -33,7 +33,9 @@ pub use lang::Lang; pub use macros::{MC_VERSION, Macros, RENDER_STAGES}; pub use options::switches_on; pub use pack::{Pack, PackError}; -pub use post::{Group, Post, PostProgram, dimension_folder, load_post}; +pub use post::{ + Blend, Factor, Group, Post, PostProgram, TRANSLUCENT_BLEND, dimension_folder, load_post, +}; pub use preprocess::{Diagnostic, Origin, Preprocessed, preprocess}; pub use properties::{Entry, Note, Properties}; pub use translate::{ diff --git a/crates/horizon-shaders/src/post.rs b/crates/horizon-shaders/src/post.rs index d6142c70a..e50eb4797 100644 --- a/crates/horizon-shaders/src/post.rs +++ b/crates/horizon-shaders/src/post.rs @@ -24,10 +24,67 @@ pub enum Group { Deferred, Composite, Final, - /// A gbuffers program: the game draws its geometry with it. + /// A gbuffers program: the game draws its geometry with it, before deferred. Gbuffers, + /// A gbuffers program drawing translucent geometry, after deferred (2.1). + Translucent, } +/// A blend factor of `blend.` (3.2), as GL names it. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Factor { + Zero, + One, + SrcColor, + OneMinusSrcColor, + DstColor, + OneMinusDstColor, + SrcAlpha, + OneMinusSrcAlpha, + DstAlpha, + OneMinusDstAlpha, + SrcAlphaSaturate, +} + +impl Factor { + fn parse(word: &str) -> Option { + Some(match word { + "ZERO" => Self::Zero, + "ONE" => Self::One, + "SRC_COLOR" => Self::SrcColor, + "ONE_MINUS_SRC_COLOR" => Self::OneMinusSrcColor, + "DST_COLOR" => Self::DstColor, + "ONE_MINUS_DST_COLOR" => Self::OneMinusDstColor, + "SRC_ALPHA" => Self::SrcAlpha, + "ONE_MINUS_SRC_ALPHA" => Self::OneMinusSrcAlpha, + "DST_ALPHA" => Self::DstAlpha, + "ONE_MINUS_DST_ALPHA" => Self::OneMinusDstAlpha, + "SRC_ALPHA_SATURATE" => Self::SrcAlphaSaturate, + _ => return None, + }) + } +} + +/// Source and destination factors for colour, then for alpha. +pub type Blend = [Factor; 4]; + +/// The default blend of translucent geometry (3.2). +pub const TRANSLUCENT_BLEND: Blend = [ + Factor::SrcAlpha, + Factor::OneMinusSrcAlpha, + Factor::One, + Factor::OneMinusSrcAlpha, +]; + +/// `gbuffers_water` and its fallbacks (2.3). +const WATER: [&str; 5] = [ + "gbuffers_water", + "gbuffers_terrain", + "gbuffers_textured_lit", + "gbuffers_textured", + "gbuffers_basic", +]; + /// `gbuffers_terrain` and its fallbacks (2.3). const TERRAIN: [&str; 4] = [ "gbuffers_terrain", @@ -52,6 +109,8 @@ pub struct PostProgram { pub buffers: Vec, /// `flip..`: buffers forced to flip (true) or kept (false) after this program. pub flips: Vec<(u32, bool)>, + /// How a gbuffers program's output blends into its buffers; `None` writes them. + pub blend: Option, } pub struct Post { @@ -63,6 +122,8 @@ pub struct Post { pub composite_pre: Vec<(u32, bool)>, /// What draws solid and cutout terrain, when the game asked for it and one loads. pub terrain: Option, + /// What draws translucent terrain (water, stained glass) after deferred. + pub water: Option, /// The custom uniforms of `shaders.properties` (3.4). pub custom: Custom, pub config: Config, @@ -96,6 +157,7 @@ pub fn load_post( deferred_pre: flips(properties, "deferred_pre"), composite_pre: flips(properties, "composite_pre"), terrain: None, + water: None, custom: Custom::parse(properties), config: Config::default(), notes: Vec::new(), @@ -127,11 +189,24 @@ pub fn load_post( } } if let Some(inputs) = terrain { - loader.post.terrain = TERRAIN.iter().find_map(|name| { - loader.load(name.to_string(), Group::Gbuffers, |v, f| { - translate_gbuffers(v, f, Target::Buffers, inputs) + let mut gbuffers = |names: &[&str], group| { + names.iter().find_map(|name| { + loader.load(name.to_string(), group, |v, f| { + translate_gbuffers(v, f, Target::Buffers, inputs) + }) }) + }; + let terrain = gbuffers(&TERRAIN, Group::Gbuffers); + // gbuffers_water falls back to gbuffers_terrain's chain (2.3). + let water = gbuffers(&WATER, Group::Translucent); + loader.post.terrain = terrain; + loader.post.water = water.map(|mut p| { + p.blend = blend(properties, &p.name).unwrap_or(Some(TRANSLUCENT_BLEND)); + p }); + if let Some(p) = &mut loader.post.terrain { + p.blend = blend(properties, &p.name).flatten(); + } } post.config = std::mem::take(&mut post.config).finish(); post @@ -211,6 +286,7 @@ impl Loader<'_> { group, program, buffers, + blend: None, }) } } @@ -260,6 +336,20 @@ fn enabled( eval_condition(&condition, macros).ok() != Some(0) } +/// `blend.`: `Some(None)` for `off`, `Some(factors)`, or `None` when absent or not +/// four known factors. +fn blend(properties: &Properties, program: &str) -> Option> { + let value = properties.get(&format!("blend.{program}"))?.trim(); + if value == "off" { + return Some(None); + } + let factors: Vec = value + .split_whitespace() + .map(Factor::parse) + .collect::>()?; + Some(Some(factors.try_into().ok()?)) +} + /// `flip..=` keys of one program. fn flips(properties: &Properties, program: &str) -> Vec<(u32, bool)> { let prefix = format!("flip.{program}."); @@ -355,6 +445,38 @@ mod tests { assert!(post.programs.iter().any(|p| p.name == "composite3")); } + /// Water falls back to the terrain chain and blends by default; `blend.*` overrides. + #[test] + fn water_falls_back_to_terrain_and_blends() { + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/packs/terrain-test"); + let pack = Pack::open(&dir).unwrap(); + let macros = Macros::standard(); + let inputs = crate::GbuffersInputs { + declarations: "layout(location = 0) in vec3 hz_v_pos;".into(), + body: " hz_Vertex = vec4(hz_v_pos, 1.0);\n hz_Color = vec4(1.0);\n".into(), + attributes: Vec::new(), + }; + let post = load_post(&pack, "", &Properties::default(), ¯os, Some(&inputs)); + let (terrain, water) = (post.terrain.unwrap(), post.water.unwrap()); + assert_eq!((terrain.group, terrain.blend), (Group::Gbuffers, None)); + assert_eq!(water.name, "gbuffers_terrain"); + assert_eq!( + (water.group, water.blend), + (Group::Translucent, Some(TRANSLUCENT_BLEND)) + ); + let properties = Properties::parse( + "blend.gbuffers_terrain = ONE ONE ZERO ONE\nblend.gbuffers_water = off\n", + ¯os, + ); + let terrain_blend = blend(&properties, "gbuffers_terrain"); + assert_eq!( + terrain_blend, + Some(Some([Factor::One, Factor::One, Factor::Zero, Factor::One])) + ); + assert_eq!(blend(&properties, "gbuffers_water"), Some(None)); + assert_eq!(blend(&properties, "gbuffers_basic"), None); + } + #[test] fn horizons_test_pack_loads() { let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/packs/post-test"); diff --git a/crates/horizon-shaders/tests/real_packs.rs b/crates/horizon-shaders/tests/real_packs.rs index 78ba97bfb..c5d4defac 100644 --- a/crates/horizon-shaders/tests/real_packs.rs +++ b/crates/horizon-shaders/tests/real_packs.rs @@ -176,9 +176,10 @@ fn real_packs_read() { Some(&terrain), ); println!( - " dimension {id} (folder `{folder}`): {} post programs, terrain {:?}, {} notes, {:.0} ms", + " dimension {id} (folder `{folder}`): {} post programs, terrain {:?}, water {:?}, {} notes, {:.0} ms", post.programs.len(), post.terrain.as_ref().map(|t| &t.name), + post.water.as_ref().map(|t| &t.name), post.notes.len() + post.config.notes.len(), start.elapsed().as_secs_f64() * 1000.0 ); From b6345fc61d640de07afc9a2f3695039e02ac3201 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:56:31 +0100 Subject: [PATCH 04/11] Shader packs, clean room 19: GPU time of each pass (developers' aid) HORIZON_SHADER_PACK_TIMING=1 puts timestamps around every pass of a shader pack, one frame in 30, and logs each pass's median every 30 timed frames. The frame after a timed one waits for the GPU before reading them (Metal resolves timestamps without waiting for the passes that write them). Passes overlap on tiled GPUs, so a pass is charged from the later of its beginning and the previous pass's end. This is what attributes a pack's cost to its own programs or to ours when the machine is shared and FPS comparisons are noisy. Clean-room: Horizon's own tooling, no spec section; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 1 + crates/horizon-client/src/gfx/pack_post.rs | 11 ++ crates/horizon-client/src/gfx/pack_timing.rs | 147 +++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 crates/horizon-client/src/gfx/pack_timing.rs diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 9a7356d6e..f0987dddd 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -70,6 +70,7 @@ mod lod; mod occlusion; mod pack_atlas; mod pack_post; +mod pack_timing; pub mod pack_uniforms; mod page_runs; pub mod panorama; diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index b96da2599..739688784 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -168,6 +168,8 @@ pub struct PackPost { water: Option, /// A program after translucent terrain reads `depthtex1`: it is copied before water. depth1: bool, + /// `HORIZON_SHADER_PACK_TIMING`: GPU time of each pass, in the log. + timing: Option, /// `shaders.properties`' custom uniforms, after the frame's in `uniforms.values`. custom: horizon_shaders::Custom, atlas: Option, @@ -853,6 +855,7 @@ impl PackPost { terrain, water, depth1, + timing: super::pack_timing::PassTiming::new(device, queue), custom, atlas: world.atlas.as_ref().map(|a| a.0.clone()), lightmap: world.lightmap, @@ -1053,9 +1056,11 @@ impl PackPost { .and_then(|t| attach(&t[usize::from(self.main & 1 << buffer != 0)], load)); } { + let timestamp_writes = self.timing.as_mut().and_then(|t| t.writes(&pass.name)); let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some(&pass.name), color_attachments: &attachments, + timestamp_writes, depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { view: &targets.depth, depth_ops: Some(wgpu::Operations { @@ -1164,6 +1169,9 @@ impl PackPost { terrain: &mut dyn FnMut(&mut wgpu::RenderPass<'_>, bool), ) { self.resize(device, size); + if let Some(t) = &mut self.timing { + t.begin(device, queue); + } self.uniforms.update(frame, inputs, size); self.uniforms.eval_custom(&mut self.custom); let Some(targets) = &self.targets else { return }; @@ -1214,6 +1222,7 @@ impl PackPost { encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("Shader pack clear"), color_attachments: &attachments, + timestamp_writes: self.timing.as_mut().and_then(|t| t.writes("clear")), ..Default::default() }); } @@ -1250,6 +1259,7 @@ impl PackPost { let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("Shader pack world"), color_attachments: &attachments, + timestamp_writes: self.timing.as_mut().and_then(|t| t.writes("world")), depth_stencil_attachment: self.blit_depth.then_some( wgpu::RenderPassDepthStencilAttachment { view: &targets.depth, @@ -1314,6 +1324,7 @@ impl PackPost { let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some(&pass.name), color_attachments: &attachments, + timestamp_writes: self.timing.as_mut().and_then(|t| t.writes(&pass.name)), ..Default::default() }); rp.set_pipeline(&pass.pipeline); diff --git a/crates/horizon-client/src/gfx/pack_timing.rs b/crates/horizon-client/src/gfx/pack_timing.rs new file mode 100644 index 000000000..61ab721e5 --- /dev/null +++ b/crates/horizon-client/src/gfx/pack_timing.rs @@ -0,0 +1,147 @@ +//! Developers' aid: `HORIZON_SHADER_PACK_TIMING=1` times every pass of a shader pack on the +//! GPU, one frame in `EVERY`, and logs each pass's median every `REPORT` timed frames. The +//! frame after a timed one waits for the GPU before reading the timestamps (Metal resolves +//! them without waiting for the passes that write them), so timing slows the game. + +const EVERY: u32 = 30; +const REPORT: usize = 30; +/// Passes timed per frame at most (two timestamps each). +const CAPACITY: u32 = 64; + +pub(super) struct PassTiming { + queries: wgpu::QuerySet, + resolve: wgpu::Buffer, + read: wgpu::Buffer, + period_ns: f32, + frame: u32, + /// The passes timed in the frame being recorded or waiting to be read, in order. + names: Vec, + timed: bool, + samples: Vec<(String, Vec)>, + reports: usize, +} + +impl PassTiming { + pub fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Option { + if std::env::var_os("HORIZON_SHADER_PACK_TIMING").is_none() + || !device.features().contains(wgpu::Features::TIMESTAMP_QUERY) + { + return None; + } + let size = u64::from(CAPACITY) * 2 * 8; + let buffer = |label, usage| { + device.create_buffer(&wgpu::BufferDescriptor { + label: Some(label), + size, + usage, + mapped_at_creation: false, + }) + }; + Some(Self { + queries: device.create_query_set(&wgpu::QuerySetDescriptor { + label: Some("Shader pack pass timing"), + ty: wgpu::QueryType::Timestamp, + count: CAPACITY * 2, + }), + resolve: buffer( + "Shader pack timing resolve", + wgpu::BufferUsages::QUERY_RESOLVE | wgpu::BufferUsages::COPY_SRC, + ), + read: buffer( + "Shader pack timing read", + wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, + ), + period_ns: queue.get_timestamp_period(), + frame: 0, + names: Vec::new(), + timed: false, + samples: Vec::new(), + reports: 0, + }) + } + + /// At the start of a frame: reads the last timed frame's passes, and decides whether this + /// one is timed. + pub fn begin(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) { + if self.timed && !self.names.is_empty() { + self.collect(device, queue); + } + self.names.clear(); + self.frame += 1; + self.timed = self.frame.is_multiple_of(EVERY); + } + + /// Timestamp writes for the pass `name`, in a timed frame. + pub fn writes(&mut self, name: &str) -> Option> { + let i = self.names.len() as u32; + if !self.timed || i >= CAPACITY { + return None; + } + self.names.push(name.to_owned()); + Some(wgpu::RenderPassTimestampWrites { + query_set: &self.queries, + beginning_of_pass_write_index: Some(2 * i), + end_of_pass_write_index: Some(2 * i + 1), + }) + } + + fn collect(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) { + // The timed frame first, then the resolve, so every timestamp is written. + _ = device.poll(wgpu::PollType::wait_indefinitely()); + let count = self.names.len() as u32 * 2; + let bytes = u64::from(count) * 8; + let mut encoder = device.create_command_encoder(&Default::default()); + encoder.resolve_query_set(&self.queries, 0..count, &self.resolve, 0); + encoder.copy_buffer_to_buffer(&self.resolve, 0, &self.read, 0, bytes); + queue.submit([encoder.finish()]); + let slice = self.read.slice(..bytes); + slice.map_async(wgpu::MapMode::Read, |_| {}); + _ = device.poll(wgpu::PollType::wait_indefinitely()); + let Ok(data) = slice.get_mapped_range() else { + return; + }; + let stamps: Vec = data + .as_chunks::<8>() + .0 + .iter() + .map(|b| u64::from_le_bytes(*b)) + .collect(); + drop(data); + self.read.unmap(); + // Passes overlap on tiled GPUs (a pass begins before the one before it ends): a pass + // costs the time from the later of its beginning and the previous pass's end. + let mut previous_end = 0; + for (name, pair) in self.names.iter().zip(stamps.as_chunks::<2>().0) { + if pair[0] == 0 || pair[1] < pair[0] { + continue; + } + let from = pair[0].max(previous_end).min(pair[1]); + previous_end = pair[1]; + let ms = (pair[1] - from) as f32 * self.period_ns / 1e6; + match self.samples.iter_mut().find(|s| s.0 == *name) { + Some(s) => s.1.push(ms), + None => self.samples.push((name.clone(), vec![ms])), + } + } + self.reports += 1; + if self.reports.is_multiple_of(REPORT) { + self.report(); + } + } + + fn report(&mut self) { + let mut total = 0.0; + let mut parts = Vec::new(); + for (name, v) in &mut self.samples { + v.sort_by(f32::total_cmp); + let median = v[v.len() / 2]; + total += median; + parts.push(format!("{name} {median:.3}")); + v.clear(); + } + log::info!( + "shader pack GPU time per pass (median ms of {REPORT} frames): {} = {total:.3}", + parts.join(", ") + ); + } +} From 5e348aa479aafa0ab2f03320fb637a5596f00900 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 03:03:54 +0100 Subject: [PATCH 05/11] Shader packs, clean room 20: buffer mipmaps `const bool MipmapEnabled = true;` in a deferred, composite or final program's fragment stage now regenerates that buffer's mip chain right before the program (6.1.7), on the side programs read, and only when the buffer changed since its last chain this frame. Each level is the average of the 2x2 texels above it; levels attach one at a time and programs sample all of them. Unfilterable buffers (integer, 32-bit float) get none, with a note. BSL's bloom read every tap from the full-size buffer before; with its chain composite4 drops from 0.38 to 0.29 ms at 720p on the Mac mini (HORIZON_SHADER_PACK_TIMING, the chains included), and blurs as intended. Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, section 6.1.7; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx/pack_post.rs | 156 +++++++++++++++++-- crates/horizon-client/src/gfx/pack_post.wgsl | 8 + crates/horizon-shaders/src/post.rs | 18 ++- 3 files changed, 172 insertions(+), 10 deletions(-) diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index 739688784..2f152b893 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -137,9 +137,19 @@ struct Pass { reads: u16, /// Buffers written. written: u16, + /// Buffers whose mip chain is regenerated right before it (6.1.7). + mipmaps: u16, groups: HashMap, } +/// One side of a buffer with mipmaps. +struct Mips { + /// Every level, for sampling. + all: wgpu::TextureView, + /// From the second level down: its attachment, and a bind group over the level above. + levels: Vec<(wgpu::TextureView, wgpu::BindGroup)>, +} + struct Targets { size: [u32; 2], /// A and B of each buffer used. @@ -148,6 +158,8 @@ struct Targets { depth_texture: wgpu::Texture, /// `depthtex1`, when a program after translucent terrain reads it. depth1: Option<(wgpu::Texture, wgpu::TextureView)>, + /// Both sides' mip chains of the buffers with mipmaps (`color` is their first level). + mips: [Option<[Mips; 2]>; BUFFERS], } /// What a pack's gbuffers programs read and draw from the game's world. @@ -168,6 +180,12 @@ pub struct PackPost { water: Option, /// A program after translucent terrain reads `depthtex1`: it is copied before water. depth1: bool, + /// Buffers with mipmaps (filterable ones a program asks them for). + mipped: u16, + /// Buffers whose mip chain matches their first level, this frame. + mips_valid: u16, + /// The downsampling pipeline of each format with mipmaps. + down: HashMap, /// `HORIZON_SHADER_PACK_TIMING`: GPU time of each pass, in the log. timing: Option, /// `shaders.properties`' custom uniforms, after the frame's in `uniforms.values`. @@ -693,6 +711,7 @@ fn build_pass( flip, reads, written, + mipmaps: p.mipmaps, groups: HashMap::new(), }) } @@ -848,6 +867,39 @@ impl PackPost { &[target(screen)], None, ); + let filterable = |b: usize| { + formats[b].sample_type(None, Some(device.features())) + == Some(wgpu::TextureSampleType::Float { filterable: true }) + }; + let asked = passes.iter().fold(0u16, |m, p| m | p.mipmaps); + let mipped = (0..BUFFERS) + .filter(|&b| asked & 1 << b != 0 && filterable(b)) + .fold(0u16, |m, b| m | 1 << b); + if asked & !mipped != 0 { + notes.push(format!( + "mipmaps of unfilterable buffers are not generated (buffers {:#b})", + asked & !mipped + )); + } + let mut down = HashMap::new(); + for b in (0..BUFFERS).filter(|b| mipped & 1 << b != 0) { + down.entry(formats[b]).or_insert_with(|| { + helper( + device, + "vs_full", + "fs_down", + &[ + texture_entry(0, float), + entry( + 2, + wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), + ), + ], + &[target(formats[b])], + None, + ) + }); + } let white = [255u8; 4]; Self { notes, @@ -855,6 +907,9 @@ impl PackPost { terrain, water, depth1, + mipped, + mips_valid: 0, + down, timing: super::pack_timing::PassTiming::new(device, queue), custom, atlas: world.atlas.as_ref().map(|a| a.0.clone()), @@ -960,9 +1015,14 @@ impl PackPost { }]; for s in &pass.samplers { let texture = match s.source { - Source::Color(i) => targets.color[i] - .as_ref() - .map_or(&self.dummy, |t| &t[usize::from(key & 1 << i != 0)]), + Source::Color(i) => { + let side = usize::from(key & 1 << i != 0); + match (&targets.mips[i], &targets.color[i]) { + (Some(m), _) => &m[side].all, + (None, Some(t)) => &t[side], + (None, None) => &self.dummy, + } + } Source::Depth => &targets.depth, // Before translucent terrain the two are the same. Source::Depth1 => match &targets.depth1 { @@ -1075,6 +1135,7 @@ impl PackPost { rp.set_bind_group(0, &group, &[]); draw(&mut rp, translucent); } + self.mips_valid &= !pass.written; if translucent { self.water = Some(pass); } else { @@ -1082,6 +1143,32 @@ impl PackPost { } } + /// Regenerates the mip chain of buffer `b`'s side that programs read. + fn mipmap(&self, encoder: &mut wgpu::CommandEncoder, b: usize) { + let (Some(targets), Some((pipeline, _))) = (&self.targets, self.down.get(&self.formats[b])) + else { + return; + }; + let Some(mips) = &targets.mips[b] else { return }; + let side = &mips[usize::from(self.main & 1 << b != 0)]; + for (attachment, group) in &side.levels { + let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Shader pack mipmap"), + color_attachments: &[attach( + attachment, + wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), + store: wgpu::StoreOp::Store, + }, + )], + ..Default::default() + }); + rp.set_pipeline(pipeline); + rp.set_bind_group(0, group, &[]); + rp.draw(0..3, 0..1); + } + } + /// Whether the pack draws terrain itself. pub fn has_terrain(&self) -> bool { self.terrain.is_some() @@ -1101,7 +1188,8 @@ impl PackPost { if self.targets.as_ref().is_some_and(|t| t.size == size) { return; } - let texture = |label: &str, format, usage| { + let full_chain = 32 - size[0].max(size[1]).max(1).leading_zeros(); + let texture_levels = |label: &str, format, usage, levels| { device.create_texture(&wgpu::TextureDescriptor { label: Some(label), size: wgpu::Extent3d { @@ -1109,7 +1197,7 @@ impl PackPost { height: size[1], depth_or_array_layers: 1, }, - mip_level_count: 1, + mip_level_count: levels, sample_count: 1, dimension: wgpu::TextureDimension::D2, format, @@ -1117,14 +1205,55 @@ impl PackPost { view_formats: &[], }) }; - let make = |label: &str, format| { - texture(label, format, wgpu::TextureUsages::RENDER_ATTACHMENT) - .create_view(&Default::default()) + let texture = |label: &str, format, usage| texture_levels(label, format, usage, 1); + let level = |t: &wgpu::Texture, level: u32| { + t.create_view(&wgpu::TextureViewDescriptor { + base_mip_level: level, + mip_level_count: Some(1), + ..Default::default() + }) }; + let mut mips: [Option<[Mips; 2]>; BUFFERS] = Default::default(); let color = std::array::from_fn(|i| { (self.used & 1 << i != 0).then(|| { let label = format!("colortex{i}"); - [make(&label, self.formats[i]), make(&label, self.formats[i])] + let mipped = self.mipped & 1 << i != 0; + let levels = if mipped { full_chain } else { 1 }; + let attachment = wgpu::TextureUsages::RENDER_ATTACHMENT; + let sides = + [0, 1].map(|_| texture_levels(&label, self.formats[i], attachment, levels)); + if mipped && let Some((_, layout)) = self.down.get(&self.formats[i]) { + mips[i] = Some(sides.each_ref().map(|t| { + Mips { + all: t.create_view(&Default::default()), + levels: (1..levels) + .map(|l| { + let group = + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some(&label), + layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::TextureView( + &level(t, l - 1), + ), + }, + wgpu::BindGroupEntry { + binding: 2, + resource: wgpu::BindingResource::Sampler( + &self.linear, + ), + }, + ], + }); + (level(t, l), group) + }) + .collect(), + } + })); + } + sides.map(|t| level(&t, 0)) }) }); let depth32 = wgpu::TextureFormat::Depth32Float; @@ -1144,6 +1273,7 @@ impl PackPost { depth: depth_texture.create_view(&Default::default()), depth_texture, depth1, + mips, }); let gbuffers = self.terrain.iter_mut().chain(&mut self.water); for pass in self.passes.iter_mut().chain(gbuffers) { @@ -1277,6 +1407,8 @@ impl PackPost { pass.draw(0..3, 0..1); } + // The world and the clears just rewrote the buffers' first levels. + self.mips_valid = 0; let mut passes = std::mem::take(&mut self.passes); let mut seen = (false, false); let mut shown = false; @@ -1299,6 +1431,11 @@ impl PackPost { seen.1 = true; self.main ^= self.composite_pre; } + let stale = pass.mipmaps & self.mipped & !self.mips_valid; + for b in (0..BUFFERS).filter(|b| stale & 1 << b != 0) { + self.mipmap(encoder, b); + } + self.mips_valid |= stale; let final_pass = pass.group == Group::Final; let group = self.bind(device, queue, pass, if final_pass { window } else { size }); let targets = self.targets.as_ref().expect("resized"); @@ -1332,6 +1469,7 @@ impl PackPost { rp.draw(0..6, 0..1); } self.main ^= pass.flip; + self.mips_valid &= !pass.written; } self.passes = passes; if !terrain_drawn { diff --git a/crates/horizon-client/src/gfx/pack_post.wgsl b/crates/horizon-client/src/gfx/pack_post.wgsl index 75877dddc..299e9207b 100644 --- a/crates/horizon-client/src/gfx/pack_post.wgsl +++ b/crates/horizon-client/src/gfx/pack_post.wgsl @@ -53,3 +53,11 @@ fn vs_uv(@builtin(vertex_index) i: u32) -> Full { fn fs_copy(v: Full) -> @location(0) vec4 { return textureSample(source, linear, v.uv); } + +// One level of a buffer's mip chain from the level above (6.1.7): the average of the 2×2 +// texels under each destination texel, by one bilinear sample at their shared corner. +@fragment +fn fs_down(@builtin(position) p: vec4) -> @location(0) vec4 { + let size = max(textureDimensions(source) / 2u, vec2(1u)); + return textureSampleLevel(source, linear, p.xy / vec2(size), 0.0); +} diff --git a/crates/horizon-shaders/src/post.rs b/crates/horizon-shaders/src/post.rs index e50eb4797..e155cefbe 100644 --- a/crates/horizon-shaders/src/post.rs +++ b/crates/horizon-shaders/src/post.rs @@ -111,6 +111,9 @@ pub struct PostProgram { pub flips: Vec<(u32, bool)>, /// How a gbuffers program's output blends into its buffers; `None` writes them. pub blend: Option, + /// Bit n: `colortexMipmapEnabled` in its fragment stage, so buffer n's mip chain is + /// regenerated right before it (6.1.7; not for gbuffers programs). + pub mipmaps: u16, } pub struct Post { @@ -280,6 +283,16 @@ impl Loader<'_> { self.post.config.read(&vertex); self.post.config.read(&fragment); let buffers = config::draw_buffers(&fragment).unwrap_or_else(|| (0..8).collect()); + let mipmaps = match group { + Group::Gbuffers | Group::Translucent => 0, + _ => { + let mut own = Config::default(); + own.read(&fragment); + (own.buffers.iter().enumerate()) + .filter(|b| b.1.mipmap) + .fold(0, |m, (i, _)| m | 1 << i) + } + }; Some(PostProgram { flips: flips(self.properties, &name), name, @@ -287,6 +300,7 @@ impl Loader<'_> { program, buffers, blend: None, + mipmaps, }) } } @@ -391,7 +405,8 @@ mod tests { ("shaders/deferred.vsh", VSH), ( "shaders/deferred.fsh", - "#version 120\nvoid main() { gl_FragColor = vec4(1.0); }\n", + "#version 120\nconst bool colortex5MipmapEnabled = true;\n\ + void main() { gl_FragColor = vec4(1.0); }\n", ), ("shaders/composite1.fsh", FSH), ("shaders/composite3.vsh", VSH), @@ -423,6 +438,7 @@ mod tests { assert_eq!(names, ["deferred", "composite", "composite2", "final"]); let p = &post.programs; assert_eq!(p[0].buffers, (0..8).collect::>()); + assert_eq!((p[0].mipmaps, p[1].mipmaps), (1 << 5, 0)); assert_eq!(p[1].buffers, [3]); assert_eq!(p[1].flips, [(3, false)]); assert_eq!(p[2].flips, [(4, true)]); From b9b47f12dabd79ba8c48998d9aa98974e26a3eb4 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 03:29:21 +0100 Subject: [PATCH 06/11] Shader packs, clean room 21: per-draw uniforms (renderStage) A gbuffers program's renderStage, entityId, blockEntityId and entityColor (spec 5.3, updated per draw) move to a block of their own at set 2 that the game picks per draw with a dynamic offset: one slot per render stage. Terrain draws as TERRAIN_SOLID, translucent terrain as TERRAIN_TRANSLUCENT. The terrain test pack paints any other stage red. Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, 5.3, 5.4; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 1 + crates/horizon-client/src/gfx/pack_draw.rs | 67 +++++++++++++++++++ crates/horizon-client/src/gfx/pack_post.rs | 25 ++++++- crates/horizon-shaders/src/lib.rs | 6 +- crates/horizon-shaders/src/macros.rs | 8 +++ crates/horizon-shaders/src/translate/mod.rs | 47 ++++++++++++- .../terrain-test/shaders/gbuffers_terrain.fsh | 3 + 7 files changed, 150 insertions(+), 7 deletions(-) create mode 100644 crates/horizon-client/src/gfx/pack_draw.rs diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index f0987dddd..354dc8d07 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -69,6 +69,7 @@ pub mod lightning; mod lod; mod occlusion; mod pack_atlas; +mod pack_draw; mod pack_post; mod pack_timing; pub mod pack_uniforms; diff --git a/crates/horizon-client/src/gfx/pack_draw.rs b/crates/horizon-client/src/gfx/pack_draw.rs new file mode 100644 index 000000000..91c77a6bf --- /dev/null +++ b/crates/horizon-client/src/gfx/pack_draw.rs @@ -0,0 +1,67 @@ +//! The uniforms a shader pack's gbuffers programs read per draw (`HzDraw` at set 2, the +//! translator's `DRAW_UNIFORMS`): one slot per render stage of a uniform buffer, picked with a +//! dynamic offset at each draw. + +use horizon_shaders::{RENDER_STAGES, render_stage}; + +/// `HzDraw`'s size: `renderStage`, `entityId`, `blockEntityId`, a pad, `entityColor`. +const SIZE: u32 = 32; + +pub(super) struct DrawUniforms { + pub layout: wgpu::BindGroupLayout, + group: wgpu::BindGroup, + stride: u32, +} + +impl DrawUniforms { + pub fn new(device: &wgpu::Device) -> Self { + let stride = SIZE.max(device.limits().min_uniform_buffer_offset_alignment); + let mut contents = vec![0u8; stride as usize * RENDER_STAGES.len()]; + for (i, slot) in contents.chunks_mut(stride as usize).enumerate() { + slot[..4].copy_from_slice(&(i as i32).to_le_bytes()); + } + let buffer = wgpu::util::DeviceExt::create_buffer_init( + device, + &wgpu::util::BufferInitDescriptor { + label: Some("Shader pack draws"), + contents: &contents, + usage: wgpu::BufferUsages::UNIFORM, + }, + ); + let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("Shader pack draws"), + entries: &[wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: true, + min_binding_size: wgpu::BufferSize::new(u64::from(SIZE)), + }, + count: None, + }], + }); + let group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Shader pack draws"), + layout: &layout, + entries: &[wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { + buffer: &buffer, + offset: 0, + size: wgpu::BufferSize::new(u64::from(SIZE)), + }), + }], + }); + Self { + layout, + group, + stride, + } + } + + /// Draws that follow read `renderStage` = `MC_RENDER_STAGE_`, other values zero. + pub fn bind(&self, pass: &mut wgpu::RenderPass<'_>, stage: &str) { + pass.set_bind_group(2, &self.group, &[self.stride * render_stage(stage)]); + } +} diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index 2f152b893..08a2dedcc 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -15,6 +15,7 @@ use std::collections::HashMap; use horizon_shaders::{Format, Group, Kind, Post, Val}; +use super::pack_draw::DrawUniforms; use super::pack_uniforms::{self, FrameUniforms, PackInputs}; const BUFFERS: usize = horizon_shaders::BUFFERS; @@ -139,6 +140,8 @@ struct Pass { written: u16, /// Buffers whose mip chain is regenerated right before it (6.1.7). mipmaps: u16, + /// Reads the per-draw block (`DrawUniforms`). + draw: bool, groups: HashMap, } @@ -186,6 +189,8 @@ pub struct PackPost { mips_valid: u16, /// The downsampling pipeline of each format with mipmaps. down: HashMap, + /// The per-draw uniforms of gbuffers programs. + draws: DrawUniforms, /// `HORIZON_SHADER_PACK_TIMING`: GPU time of each pass, in the log. timing: Option, /// `shaders.properties`' custom uniforms, after the frame's in `uniforms.values`. @@ -488,6 +493,7 @@ fn build_pass( notes: &mut Vec, geometry: Option<&Geometry<'_>>, custom: &[String], + draws: &wgpu::BindGroupLayout, ) -> Option { let features = device.features(); // The frame's uniforms, then the pack's custom ones. @@ -499,6 +505,7 @@ fn build_pass( }; let mask = |flips: &[(u32, bool)]| flips.iter().filter(|f| f.1).fold(0u16, |m, f| m | 1 << f.0); let interface = &p.program.interface; + let draw = interface.draw; let mut entries = vec![entry( 0, wgpu::BindingType::Buffer { @@ -632,7 +639,11 @@ fn build_pass( }); let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: Some(&p.name), - bind_group_layouts: &[Some(&layout), geometry.and_then(|g| g.table)], + bind_group_layouts: &[ + Some(&layout), + geometry.and_then(|g| g.table), + draw.then_some(draws), + ], ..Default::default() }); let module = |mut m: naga::Module| { @@ -712,6 +723,7 @@ fn build_pass( reads, written, mipmaps: p.mipmaps, + draw, groups: HashMap::new(), }) } @@ -748,6 +760,7 @@ impl PackPost { let mask = |flips: &[(u32, bool)]| flips.iter().filter(|f| f.1).fold(0u16, |m, f| m | 1 << f.0); let mut used = 1u16; + let draws = DrawUniforms::new(device); let mut build = |p: horizon_shaders::PostProgram, geometry: Option<&Geometry<'_>>| { let pass = build_pass( device, @@ -757,6 +770,7 @@ impl PackPost { &mut notes, geometry, &custom_names, + &draws.layout, )?; used |= pass.reads | pass.written | pass.flip; Some(pass) @@ -910,6 +924,7 @@ impl PackPost { mipped, mips_valid: 0, down, + draws, timing: super::pack_timing::PassTiming::new(device, queue), custom, atlas: world.atlas.as_ref().map(|a| a.0.clone()), @@ -1133,6 +1148,14 @@ impl PackPost { }); rp.set_pipeline(&pass.pipeline); rp.set_bind_group(0, &group, &[]); + if pass.draw { + let stage = if translucent { + "TERRAIN_TRANSLUCENT" + } else { + "TERRAIN_SOLID" + }; + self.draws.bind(&mut rp, stage); + } draw(&mut rp, translucent); } self.mips_valid &= !pass.written; diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index f5819d435..bed86758e 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -30,7 +30,7 @@ pub use config::{BUFFERS, Buffer, Config, Format, Kind, buffer_index, draw_buffe pub use custom::{Custom, Val}; pub use discover::{Found, discover}; pub use lang::Lang; -pub use macros::{MC_VERSION, Macros, RENDER_STAGES}; +pub use macros::{MC_VERSION, Macros, RENDER_STAGES, render_stage}; pub use options::switches_on; pub use pack::{Pack, PackError}; pub use post::{ @@ -39,6 +39,6 @@ pub use post::{ pub use preprocess::{Diagnostic, Origin, Preprocessed, preprocess}; pub use properties::{Entry, Note, Properties}; pub use translate::{ - Error, GbuffersInputs, Interface, Program, Sampler, Target, Uniform, translate_composite, - translate_gbuffers, + DRAW_UNIFORMS, Error, GbuffersInputs, Interface, Program, Sampler, Target, Uniform, + translate_composite, translate_gbuffers, }; diff --git a/crates/horizon-shaders/src/macros.rs b/crates/horizon-shaders/src/macros.rs index d8a184f41..6c7fb2678 100644 --- a/crates/horizon-shaders/src/macros.rs +++ b/crates/horizon-shaders/src/macros.rs @@ -60,6 +60,14 @@ pub const RENDER_STAGES: [&str; 24] = [ "HAND_TRANSLUCENT", ]; +/// The value of `MC_RENDER_STAGE_` (OQ-7: its index in `RENDER_STAGES`). +pub fn render_stage(name: &str) -> u32 { + RENDER_STAGES + .iter() + .position(|s| *s == name) + .unwrap_or_else(|| panic!("no render stage {name}")) as u32 +} + impl Macros { /// The standard macros that need no renderer (spec 1.5.1): `MC_VERSION`, this system's /// `MC_OS_*`, the GL and GLSL versions Horizon's translation accepts, the render stages and diff --git a/crates/horizon-shaders/src/translate/mod.rs b/crates/horizon-shaders/src/translate/mod.rs index 83dcb3af9..fe736e4d8 100644 --- a/crates/horizon-shaders/src/translate/mod.rs +++ b/crates/horizon-shaders/src/translate/mod.rs @@ -45,8 +45,19 @@ pub struct Interface { pub samplers: Vec, /// The fragment stage's output locations. pub outputs: Vec, + /// A gbuffers program reads `DRAW_UNIFORMS`: the block at set 2, binding 0, set per draw. + pub draw: bool, } +/// The uniforms a gbuffers program reads per draw (5.3's "D"), in a block of their own +/// (`HzDraw`, std140: three ints, a pad, a vec4; 32 bytes) the game sets for each draw. +pub const DRAW_UNIFORMS: [(&str, &str); 4] = [ + ("renderStage", "int"), + ("entityId", "int"), + ("blockEntityId", "int"), + ("entityColor", "vec4"), +]; + #[derive(Clone, Debug, PartialEq)] pub struct Uniform { pub name: String, @@ -170,6 +181,17 @@ fn translate( samplers.insert(name.clone(), ty.clone()); } } + // Per-draw values move to their own block; another type than the documented one keeps + // the frame's block (and reads zero). + let mut draw = false; + if gbuffers.is_some() { + for (name, ty) in DRAW_UNIFORMS { + if uniforms.get(name).is_some_and(|t| t == ty) { + uniforms.remove(name); + draw = true; + } + } + } let samplers: Vec = samplers .into_iter() .enumerate() @@ -203,6 +225,12 @@ fn translate( .unwrap(); } h.push_str(" };\n"); + if draw { + h.push_str( + "layout(set = 2, binding = 0) uniform HzDraw { int renderStage; int entityId; \ + int blockEntityId; int hz_draw_pad; vec4 entityColor; };\n", + ); + } for s in &samplers { let (texture, sampler) = split_sampler(&s.ty); writeln!( @@ -327,6 +355,7 @@ fn translate( uniforms, samplers, outputs, + draw, }, }) } @@ -394,7 +423,11 @@ fn compile(stage: &Stage, text: &str, header_lines: usize) -> Result (u32, Vec<(String, u32)>) { for (_, var) in module.global_variables.iter() { - if var.space != naga::AddressSpace::Uniform { + let frame = var + .binding + .as_ref() + .is_some_and(|b| b.group == 0 && b.binding == 0); + if var.space != naga::AddressSpace::Uniform || !frame { continue; } if let naga::TypeInner::Struct { members, span } = &module.types[var.ty].inner { @@ -546,9 +579,9 @@ mod tests { light = (gl_TextureMatrix[1] * gl_MultiTexCoord1).xy;\n color = gl_Color;\n \ normal = normalize(gl_NormalMatrix * gl_Normal);\n id = mc_Entity.x + mc_midTexCoord.x;\n}\n"; let fsh = "#version 120\nuniform sampler2D texture;\nuniform sampler2D lightmap;\nuniform mat4 gbufferModelView;\n\ - varying vec2 uv;\nvarying vec2 light;\nvarying vec4 color;\nvarying vec3 normal;\nvarying float id;\n\ + uniform int renderStage;\nvarying vec2 uv;\nvarying vec2 light;\nvarying vec4 color;\nvarying vec3 normal;\nvarying float id;\n\ void main() {\n vec4 c = texture2D(texture, uv) * texture2D(lightmap, light) * color;\n \ - /* DRAWBUFFERS:02 */\n gl_FragData[0] = c * gl_FragCoord.z;\n \ + /* DRAWBUFFERS:02 */\n gl_FragData[0] = c * gl_FragCoord.z * float(renderStage);\n \ gl_FragData[1] = vec4(normal * 0.5 + 0.5, id);\n}\n"; let files: Vec<(String, &str)> = [("gbuffers_terrain.vsh", vsh), ("gbuffers_terrain.fsh", fsh)] @@ -586,6 +619,14 @@ mod tests { assert!(names.contains(&name), "{name} missing from {names:?}"); } assert_eq!(p.interface.outputs, vec![0, 1]); + // renderStage is set per draw, in its own block. + assert!(p.interface.draw && !names.contains(&"renderStage")); + let per_draw = p.fragment.global_variables.iter().any(|(_, v)| { + v.binding + .as_ref() + .is_some_and(|b| b.group == 2 && b.binding == 0) + }); + assert!(per_draw); let inputs: Vec<&str> = p.vertex.entry_points[0] .function .arguments diff --git a/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.fsh b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.fsh index 902cf3525..8be409929 100644 --- a/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.fsh +++ b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.fsh @@ -3,6 +3,7 @@ uniform sampler2D texture; uniform sampler2D lightmap; uniform ivec2 atlasSize; +uniform int renderStage; varying vec2 texcoord; varying vec2 lmcoord; @@ -17,6 +18,8 @@ void main() { color.rgb = mix(color.rgb * vec3(0.6, 0.7, 1.2), color.rgb, lmcoord.y); // A dot at the face's centre, one texel wide. if (length((texcoord - mid) * vec2(atlasSize)) < 1.0) color.rgb = vec3(1.0, 0.0, 1.0); + // Any other stage than solid terrain (translucent terrain falls back here): red. + if (renderStage != MC_RENDER_STAGE_TERRAIN_SOLID) color.rgb = vec3(1.0, 0.0, 0.0); /* DRAWBUFFERS:0 */ gl_FragData[0] = color; } From 7d6583dc4d088d794398391aad4c0ced94009883 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 03:45:24 +0100 Subject: [PATCH 07/11] Shader packs, clean room 22: the sky (gbuffers_skybasic) The pack's gbuffers_skybasic (or gbuffers_basic) draws vanilla's sky geometry, as Horizon's own sky builds it: the disc at y 16, the sunset fan while the sun is low, the stars and the void disc, each with its render stage. One static vertex buffer; the frame's colours and rotations sit in a small uniform the vertex inputs read. The sky draws after the world copy and before terrain, at the far plane: only where nothing else drew, without writing depth. It blends with the translucent default unless blend. says otherwise. Horizon's world pass then skips its own sky disc, stars and void. The sun and moon stay Horizon's for now (gbuffers_skytextured is next): the pack's sky covers them. load_post takes the game's inputs per geometry (GameInputs); GbuffersInputs gains statements run after the pack's main. Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, 2.1, 2.3, 5.4; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/src/game/shader_packs.rs | 4 +- crates/horizon-client/src/gfx.rs | 46 +++- crates/horizon-client/src/gfx/pack_atlas.rs | 1 + crates/horizon-client/src/gfx/pack_post.rs | 157 +++++++++--- crates/horizon-client/src/gfx/pack_sky.rs | 229 ++++++++++++++++++ crates/horizon-shaders/src/lib.rs | 3 +- crates/horizon-shaders/src/post.rs | 88 +++++-- crates/horizon-shaders/src/translate/mod.rs | 6 +- .../tests/packs/terrain-test/README.md | 4 +- .../shaders/gbuffers_skybasic.fsh | 15 ++ .../shaders/gbuffers_skybasic.vsh | 8 + crates/horizon-shaders/tests/real_packs.rs | 16 +- 12 files changed, 502 insertions(+), 75 deletions(-) create mode 100644 crates/horizon-client/src/gfx/pack_sky.rs create mode 100644 crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_skybasic.fsh create mode 100644 crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_skybasic.vsh diff --git a/crates/horizon-client/src/game/shader_packs.rs b/crates/horizon-client/src/game/shader_packs.rs index d819eddef..360262640 100644 --- a/crates/horizon-client/src/game/shader_packs.rs +++ b/crates/horizon-client/src/game/shader_packs.rs @@ -42,8 +42,8 @@ impl Game<'_> { ); } let folder = dimension_folder(&pack, dimension); - let terrain = renderer.pack_terrain_inputs(); - let post = load_post(&pack, &folder, &properties, ¯os, terrain.as_ref()); + let inputs = renderer.pack_inputs(); + let post = load_post(&pack, &folder, &properties, ¯os, &inputs); if let Some((programs, notes)) = renderer.set_shader_pack(dimension, Some(post)) { for note in notes { diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 354dc8d07..7444b136a 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -71,6 +71,7 @@ mod occlusion; mod pack_atlas; mod pack_draw; mod pack_post; +mod pack_sky; mod pack_timing; pub mod pack_uniforms; mod page_runs; @@ -88,6 +89,19 @@ const COMPLETE_COLUMN: u32 = (1 << (crate::chunk::HEIGHT / 16)) - 1; /// its fixed-point UVs (`terrain.wgsl`'s `terrain_uv`). /// The world pass again after occlusion culling's test: it loads both /// targets (phase 2 draws over phase 1, reading the test's results). +/// LevelRenderer: no sky inside lava or powder snow (sky fog ends within 2 blocks). +fn sky_open(frame: &Frame) -> bool { + match frame.fog { + Some(f) => f.sky_end > 2.0, + None => !frame.underwater, + } +} + +/// The Overworld-style sky (disc, sunset, stars, void) draws this frame. +fn sky_visible(frame: &Frame) -> bool { + !frame.title && frame.skybox && sky_open(frame) +} + fn world_pass_after_culling<'e>( encoder: &'e mut wgpu::CommandEncoder, view: &wgpu::TextureView, @@ -2777,6 +2791,7 @@ impl Renderer { 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), + depth_write: true, }), }; pack_post::PackPost::new( @@ -2794,9 +2809,16 @@ impl Renderer { self.pack_post = Some((dimension, post)); report } - /// What a shader pack's terrain program reads from Horizon's terrain vertices, in the - /// pack's block atlas (made here when missing); `None` when the atlas does not fit. - pub fn pack_terrain_inputs(&mut self) -> Option { + /// What a shader pack's gbuffers programs read from Horizon's geometry: the sky's, and + /// terrain's in the pack's block atlas (made here when missing; none when it does not fit). + pub fn pack_inputs(&mut self) -> horizon_shaders::GameInputs { + horizon_shaders::GameInputs { + terrain: self.pack_terrain_inputs(), + sky: Some(pack_sky::inputs()), + } + } + + fn pack_terrain_inputs(&mut self) -> Option { if self.pack_atlas.is_none() { let mut encoder = self.device.create_command_encoder(&Default::default()); let layers = self @@ -3711,6 +3733,12 @@ impl Renderer { let pack_post = !frame.title && self.pack_post.as_ref().is_some_and(|p| p.1.is_some()); // The pack's program reads the vertices this frame's draws bind. let pack_draws_terrain = pack_terrain && flat_frame == self.pack_terrain_flat; + let pack_draws_sky = pack_post + && self + .pack_post + .as_ref() + .and_then(|p| p.1.as_ref()) + .is_some_and(|p| p.has_sky()); let pack_draws_water = pack_draws_terrain && self .pack_post @@ -3770,13 +3798,9 @@ impl Renderer { ..Default::default() }); pass.set_bind_group(0, &self.group, &[]); - // LevelRenderer: no sky inside lava or powder snow (sky fog ends within 2 blocks). - let sky_visible = match frame.fog { - Some(f) => f.sky_end > 2.0, - None => !frame.underwater, - }; + let sky_visible = sky_open(frame); if frame.title || (sky_visible && (frame.skybox || frame.end_sky)) { - if !frame.title && frame.skybox { + if !frame.title && frame.skybox && !pack_draws_sky { pass.set_pipeline(&self.sky); pass.draw(0..3, 0..1); } @@ -3796,11 +3820,11 @@ impl Renderer { } pass.set_bind_group(0, &self.group, &[]); } - if !frame.title && frame.stars.is_some() { + if !frame.title && frame.stars.is_some() && !pack_draws_sky { self.stars.draw(&mut pass); pass.set_bind_group(0, &self.group, &[]); } - if !frame.title && frame.dark_disc { + if !frame.title && frame.dark_disc && !pack_draws_sky { pass.set_pipeline(&self.dark_disc); pass.draw(0..3, 0..1); } diff --git a/crates/horizon-client/src/gfx/pack_atlas.rs b/crates/horizon-client/src/gfx/pack_atlas.rs index 011dff379..1a14a8da9 100644 --- a/crates/horizon-client/src/gfx/pack_atlas.rs +++ b/crates/horizon-client/src/gfx/pack_atlas.rs @@ -173,6 +173,7 @@ impl PackAtlas { .iter() .map(|&(n, v)| (n.to_owned(), v.to_owned())) .collect(), + after: String::new(), } } } diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index 08a2dedcc..2334b84c9 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -16,6 +16,7 @@ use std::collections::HashMap; use horizon_shaders::{Format, Group, Kind, Post, Val}; use super::pack_draw::DrawUniforms; +use super::pack_sky::PackSky; use super::pack_uniforms::{self, FrameUniforms, PackInputs}; const BUFFERS: usize = horizon_shaders::BUFFERS; @@ -120,6 +121,14 @@ fn share_samplers(module: &mut naga::Module, slot: &HashMap) { } } +/// The game's geometry a gbuffers pass draws. +#[derive(Clone, Copy, PartialEq)] +enum Gbuffers { + Sky, + Terrain, + Water, +} + struct Pass { name: String, group: Group, @@ -181,6 +190,8 @@ pub struct PackPost { terrain: Option, /// What draws translucent terrain (`gbuffers_water` or its fallback), after `deferred`. water: Option, + /// What draws the sky (`gbuffers_skybasic` or its fallback), before terrain, and its vertices. + sky: Option<(Pass, PackSky)>, /// A program after translucent terrain reads `depthtex1`: it is copied before water. depth1: bool, /// Buffers with mipmaps (filterable ones a program asks them for). @@ -481,6 +492,8 @@ fn attach( pub(super) struct Geometry<'a> { pub buffers: &'a [Option>], pub table: Option<&'a wgpu::BindGroupLayout>, + /// Writes depth (the sky does not). + pub depth_write: bool, } /// The pass of program `p`: a quad, or `geometry` drawn by the game. `None` (with a note) when @@ -664,9 +677,9 @@ fn build_pass( buffers: geometry.map_or(&[], |g| g.buffers), }, primitive: Default::default(), - depth_stencil: geometry.map(|_| wgpu::DepthStencilState { + depth_stencil: geometry.map(|g| wgpu::DepthStencilState { format: wgpu::TextureFormat::Depth32Float, - depth_write_enabled: Some(true), + depth_write_enabled: Some(g.depth_write), depth_compare: Some(wgpu::CompareFunction::LessEqual), stencil: Default::default(), bias: Default::default(), @@ -783,6 +796,15 @@ impl PackPost { .water .zip(world.terrain.as_ref()) .and_then(|(p, g)| build(p, Some(g))); + let sky = post.sky.and_then(|p| { + let sky = PackSky::new(device); + let geometry = Geometry { + buffers: &PackSky::buffers(), + table: Some(&sky.layout), + depth_write: false, + }; + Some((build(p, Some(&geometry))?, sky)) + }); // Developers' aid: `HORIZON_SHADER_PACK_SKIP=composite,final` leaves programs out. let skip = std::env::var("HORIZON_SHADER_PACK_SKIP").unwrap_or_default(); let passes: Vec = post @@ -797,6 +819,9 @@ impl PackPost { if let Some(w) = &water { notes.push(format!("translucent terrain draws with {}", w.name)); } + if let Some((s, _)) = &sky { + notes.push(format!("the sky draws with {}", s.name)); + } let depth1 = water.is_some() && passes .iter() @@ -810,8 +835,10 @@ impl PackPost { let bit = 1u16 << b; let touched = |p: &&Pass| (p.reads | p.flip | p.written) & bit != 0; // Gbuffers passes cover part of their targets: a clear is needed if they touch it. - let first = terrain + let first = sky .iter() + .map(|s| &s.0) + .chain(&terrain) .chain(&water) .find(touched) .map(|_| true) @@ -826,6 +853,7 @@ impl PackPost { // Terrain tests against the world's depth (entities...), so the blit copies it then. let blit_depth = terrain.is_some() || water.is_some() + || sky.is_some() || passes.iter().any(|p| { p.samplers .iter() @@ -920,6 +948,7 @@ impl PackPost { passes, terrain, water, + sky, depth1, mipped, mips_valid: 0, @@ -1084,25 +1113,34 @@ impl PackPost { group.clone() } - /// Terrain (or, `translucent`, translucent terrain) with the pack's program into the - /// buffers it writes, in place, tested against the world's depth; `draw` issues the - /// game's draws. - fn draw_terrain( + /// The sky, terrain or translucent terrain with the pack's program into the buffers it + /// writes, in place, tested against the world's depth; `draw` issues the game's terrain + /// draws (`true`: translucent), the sky draws itself. + #[allow(clippy::too_many_arguments)] + fn draw_gbuffers( &mut self, device: &wgpu::Device, queue: &wgpu::Queue, encoder: &mut wgpu::CommandEncoder, size: [u32; 2], - translucent: bool, + frame: &super::Frame, + which: Gbuffers, draw: &mut dyn FnMut(&mut wgpu::RenderPass<'_>, bool), ) { - let slot = if translucent { - &mut self.water - } else { - &mut self.terrain - }; - let Some(mut pass) = slot.take() else { - return; + let translucent = which == Gbuffers::Water; + let (mut pass, sky) = match which { + Gbuffers::Sky => match self.sky.take() { + Some((pass, sky)) => (pass, Some(sky)), + None => return, + }, + Gbuffers::Terrain => match self.terrain.take() { + Some(pass) => (pass, None), + None => return, + }, + Gbuffers::Water => match self.water.take() { + Some(pass) => (pass, None), + None => return, + }, }; if translucent && let Some(t) = &self.targets @@ -1148,24 +1186,58 @@ impl PackPost { }); rp.set_pipeline(&pass.pipeline); rp.set_bind_group(0, &group, &[]); - if pass.draw { - let stage = if translucent { - "TERRAIN_TRANSLUCENT" - } else { - "TERRAIN_SOLID" - }; - self.draws.bind(&mut rp, stage); + let draws = pass.draw.then_some(&self.draws); + match &sky { + Some(sky) => sky.draw(queue, &mut rp, frame, &|rp, stage| { + if let Some(d) = draws { + d.bind(rp, stage); + } + }), + None => { + if let Some(d) = draws { + let stage = if translucent { + "TERRAIN_TRANSLUCENT" + } else { + "TERRAIN_SOLID" + }; + d.bind(&mut rp, stage); + } + draw(&mut rp, translucent); + } } - draw(&mut rp, translucent); } self.mips_valid &= !pass.written; - if translucent { - self.water = Some(pass); - } else { - self.terrain = Some(pass); + match (which, sky) { + (Gbuffers::Sky, Some(sky)) => self.sky = Some((pass, sky)), + (Gbuffers::Water, _) => self.water = Some(pass), + _ => self.terrain = Some(pass), } } + /// The sky, when it shows, then terrain. + fn draw_opaque( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + size: [u32; 2], + frame: &super::Frame, + terrain: &mut dyn FnMut(&mut wgpu::RenderPass<'_>, bool), + ) { + if super::sky_visible(frame) { + self.draw_gbuffers(device, queue, encoder, size, frame, Gbuffers::Sky, terrain); + } + self.draw_gbuffers( + device, + queue, + encoder, + size, + frame, + Gbuffers::Terrain, + terrain, + ); + } + /// Regenerates the mip chain of buffer `b`'s side that programs read. fn mipmap(&self, encoder: &mut wgpu::CommandEncoder, b: usize) { let (Some(targets), Some((pipeline, _))) = (&self.targets, self.down.get(&self.formats[b])) @@ -1197,6 +1269,11 @@ impl PackPost { self.terrain.is_some() } + /// Whether the pack draws the sky itself. + pub fn has_sky(&self) -> bool { + self.sky.is_some() + } + /// Whether the pack draws translucent terrain itself. pub fn has_water(&self) -> bool { self.water.is_some() @@ -1435,16 +1512,24 @@ impl PackPost { let mut passes = std::mem::take(&mut self.passes); let mut seen = (false, false); let mut shown = false; - let mut terrain_drawn = self.terrain.is_none(); + let mut terrain_drawn = self.terrain.is_none() && self.sky.is_none(); let mut water_drawn = self.water.is_none(); for pass in &mut passes { if !terrain_drawn && pass.group != Group::Prepare { terrain_drawn = true; - self.draw_terrain(device, queue, encoder, size, false, terrain); + self.draw_opaque(device, queue, encoder, size, frame, terrain); } if !water_drawn && matches!(pass.group, Group::Composite | Group::Final) { water_drawn = true; - self.draw_terrain(device, queue, encoder, size, true, terrain); + self.draw_gbuffers( + device, + queue, + encoder, + size, + frame, + Gbuffers::Water, + terrain, + ); } if pass.group == Group::Deferred && !seen.0 { seen.0 = true; @@ -1496,10 +1581,18 @@ impl PackPost { } self.passes = passes; if !terrain_drawn { - self.draw_terrain(device, queue, encoder, size, false, terrain); + self.draw_opaque(device, queue, encoder, size, frame, terrain); } if !water_drawn { - self.draw_terrain(device, queue, encoder, size, true, terrain); + self.draw_gbuffers( + device, + queue, + encoder, + size, + frame, + Gbuffers::Water, + terrain, + ); } let targets = self.targets.as_ref().expect("resized"); let view = |m: u16, i: usize, alt: bool| { diff --git a/crates/horizon-client/src/gfx/pack_sky.rs b/crates/horizon-client/src/gfx/pack_sky.rs new file mode 100644 index 000000000..cd99d03b9 --- /dev/null +++ b/crates/horizon-client/src/gfx/pack_sky.rs @@ -0,0 +1,229 @@ +//! The vanilla sky's geometry for a shader pack's `gbuffers_skybasic` (spec 2.3): the sky disc, +//! the sunset fan, the stars and the void disc, as Horizon's own sky draws them (terrain.wgsl, +//! stars.rs), built once into a vertex buffer. The frame's colours and rotations sit in a small +//! uniform (set 1) that the vertex inputs read. + +use std::ops::Range; + +use wgpu::util::DeviceExt; + +/// A sky vertex: a position and what it belongs to (`SKY`...). +#[repr(C)] +#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] +struct Vertex { + pos: [f32; 3], + kind: u32, +} + +const SKY: u32 = 0; +const SUNSET_APEX: u32 = 1; +/// Rim of the sunset fan: `pos.xy` is (sin, cos) of its angle. +const SUNSET_RIM: u32 = 2; +const STAR: u32 = 3; +const VOID: u32 = 4; + +/// The frame's sky (std140). +#[repr(C)] +#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] +struct Uniform { + /// Camera-relative world from star space. + stars: [[f32; 4]; 4], + /// Sky colour, alpha 1. + color: [f32; 4], + /// Sunset colour and alpha. + sunrise: [f32; 4], + /// cos and sin of the sunset fan's turn, star brightness. + sun: [f32; 4], +} + +pub(super) const ATTRIBUTES: [wgpu::VertexAttribute; 2] = + wgpu::vertex_attr_array![0 => Float32x3, 1 => Uint32]; + +/// What `gbuffers_skybasic` reads from these vertices. +pub fn inputs() -> horizon_shaders::GbuffersInputs { + let declarations = + "layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in uint hz_v_kind;\n\ + layout(set = 1, binding = 0) uniform HzSky { mat4 hz_sky_stars; vec4 hz_sky_color; \ + vec4 hz_sky_sunrise; vec4 hz_sky_sun; };\n" + .to_owned(); + let body = format!( + " vec3 hz_p = hz_v_pos;\n \ + hz_Color = hz_sky_color;\n \ + if (hz_v_kind == {SUNSET_APEX}u || hz_v_kind == {SUNSET_RIM}u) {{\n \ + vec3 hz_l = vec3(0.0, 100.0, 0.0);\n \ + hz_Color = hz_sky_sunrise;\n \ + if (hz_v_kind == {SUNSET_RIM}u) {{\n \ + hz_l = vec3(hz_v_pos.xy * 120.0, -hz_v_pos.y * 40.0 * hz_sky_sunrise.a);\n \ + hz_Color.a = 0.0;\n \ + }}\n \ + vec3 hz_r = vec3(hz_l.x * hz_sky_sun.x - hz_l.y * hz_sky_sun.y, hz_l.x * hz_sky_sun.y + hz_l.y * hz_sky_sun.x, hz_l.z);\n \ + hz_p = vec3(hz_r.x, -hz_r.z, hz_r.y);\n \ + }} else if (hz_v_kind == {STAR}u) {{\n \ + hz_p = (hz_sky_stars * vec4(hz_v_pos, 1.0)).xyz;\n \ + hz_Color = vec4(hz_sky_sun.z);\n \ + }} else if (hz_v_kind == {VOID}u) {{\n \ + hz_Color = vec4(0.0, 0.0, 0.0, 1.0);\n \ + }}\n \ + hz_Vertex = vec4(hz_p, 1.0);\n \ + hz_MultiTexCoord0 = vec4(0.0, 0.0, 0.0, 1.0);\n \ + hz_MultiTexCoord1 = vec4(0.0, 240.0, 0.0, 1.0);\n \ + hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \ + hz_Normal = vec3(0.0, 1.0, 0.0);\n" + ); + horizon_shaders::GbuffersInputs { + declarations, + body, + attributes: vec![ + ("vaPosition".into(), "hz_Vertex.xyz".into()), + ("vaColor".into(), "hz_Color".into()), + ("vaNormal".into(), "hz_Normal".into()), + ], + // At the far plane: the sky shows only where nothing else drew, and it writes no depth. + after: " gl_Position.z = gl_Position.w;\n".into(), + } +} + +/// A disc fan at height `y`, radius 512, as triangles (SkyRenderer's sky and void discs). +fn disc(y: f32, kind: u32, out: &mut Vec) { + let sign = y.signum(); + let rim = |i: i32| { + let a = (i as f32).to_radians(); + Vertex { + pos: [sign * 512.0 * a.cos(), y, 512.0 * a.sin()], + kind, + } + }; + let centre = Vertex { + pos: [0.0, y, 0.0], + kind, + }; + for i in (-180..180).step_by(45) { + out.extend([centre, rim(i), rim(i + 45)]); + } +} + +pub(super) struct PackSky { + vertices: wgpu::Buffer, + uniform: wgpu::Buffer, + pub layout: wgpu::BindGroupLayout, + group: wgpu::BindGroup, + /// Vertex ranges of the sky disc, the sunset fan, the stars and the void. + ranges: [Range; 4], +} + +impl PackSky { + pub fn new(device: &wgpu::Device) -> Self { + let mut v = Vec::new(); + disc(16.0, SKY, &mut v); + let sky = 0..v.len() as u32; + let apex = Vertex { + pos: [0.0; 3], + kind: SUNSET_APEX, + }; + let rim = |i: u32| { + let a = i as f32 * std::f32::consts::TAU / 16.0; + Vertex { + pos: [a.sin(), a.cos(), 0.0], + kind: SUNSET_RIM, + } + }; + for i in 0..16 { + v.extend([apex, rim(i), rim(i + 1)]); + } + let sunset = sky.end..v.len() as u32; + v.extend( + super::stars::star_triangles() + .into_iter() + .map(|pos| Vertex { pos, kind: STAR }), + ); + let stars = sunset.end..v.len() as u32; + disc(-4.0, VOID, &mut v); + let void = stars.end..v.len() as u32; + let vertices = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("Shader pack sky"), + contents: bytemuck::cast_slice(&v), + usage: wgpu::BufferUsages::VERTEX, + }); + let uniform = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Shader pack sky"), + size: std::mem::size_of::() as u64, + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("Shader pack sky"), + entries: &[wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::VERTEX, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }, + count: None, + }], + }); + let group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Shader pack sky"), + layout: &layout, + entries: &[wgpu::BindGroupEntry { + binding: 0, + resource: uniform.as_entire_binding(), + }], + }); + Self { + vertices, + uniform, + layout, + group, + ranges: [sky, sunset, stars, void], + } + } + + pub fn buffers() -> [Option>; 1] { + [Some(wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::() as u64, + step_mode: wgpu::VertexStepMode::Vertex, + attributes: &ATTRIBUTES, + })] + } + + /// Draws the frame's sky, each part with its render stage: the disc, the sunset fan while + /// the sun is near the horizon, the stars and, with the camera low, the void. + pub fn draw( + &self, + queue: &wgpu::Queue, + pass: &mut wgpu::RenderPass<'_>, + frame: &super::Frame, + stage: &dyn Fn(&mut wgpu::RenderPass<'_>, &str), + ) { + let sun_angle = frame.sky.sun_angle.to_radians(); + // The fan turns to the other horizon in the afternoon (terrain.wgsl's sunrise_weight). + let flip = if sun_angle.sin() < 0.0 { 180f32 } else { 0.0 }; + let theta = (flip + 90.0).to_radians(); + let (rotation, brightness) = frame.stars.unwrap_or_default(); + let [r, g, b] = frame.sky.sky_color; + let uniform = Uniform { + stars: rotation.to_cols_array_2d(), + color: [r, g, b, 1.0], + sunrise: frame.sky.sunrise_color, + sun: [theta.cos(), theta.sin(), brightness.clamp(0.0, 1.0), 0.0], + }; + queue.write_buffer(&self.uniform, 0, bytemuck::bytes_of(&uniform)); + pass.set_bind_group(1, &self.group, &[]); + pass.set_vertex_buffer(0, self.vertices.slice(..)); + let [sky, sunset, stars, void] = self.ranges.clone(); + let parts = [ + ("SKY", sky, true), + ("SUNSET", sunset, frame.sky.sunrise_color[3] > 0.001), + ("STARS", stars, frame.stars.is_some()), + ("VOID", void, frame.dark_disc), + ]; + for (name, range, shown) in parts { + if shown { + stage(pass, name); + pass.draw(range, 0..1); + } + } + } +} diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index bed86758e..853f2f2ba 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -34,7 +34,8 @@ pub use macros::{MC_VERSION, Macros, RENDER_STAGES, render_stage}; pub use options::switches_on; pub use pack::{Pack, PackError}; pub use post::{ - Blend, Factor, Group, Post, PostProgram, TRANSLUCENT_BLEND, dimension_folder, load_post, + Blend, Factor, GameInputs, Group, Post, PostProgram, TRANSLUCENT_BLEND, dimension_folder, + load_post, }; pub use preprocess::{Diagnostic, Origin, Preprocessed, preprocess}; pub use properties::{Entry, Note, Properties}; diff --git a/crates/horizon-shaders/src/post.rs b/crates/horizon-shaders/src/post.rs index e155cefbe..56303e67b 100644 --- a/crates/horizon-shaders/src/post.rs +++ b/crates/horizon-shaders/src/post.rs @@ -93,6 +93,9 @@ const TERRAIN: [&str; 4] = [ "gbuffers_basic", ]; +/// `gbuffers_skybasic` and its fallback (2.3). +const SKY: [&str; 2] = ["gbuffers_skybasic", "gbuffers_basic"]; + const GROUPS: [(Group, &str); 4] = [ (Group::Prepare, "prepare"), (Group::Deferred, "deferred"), @@ -127,6 +130,8 @@ pub struct Post { pub terrain: Option, /// What draws translucent terrain (water, stained glass) after deferred. pub water: Option, + /// What draws the sky (disc, sunset, stars, void) before terrain. + pub sky: Option, /// The custom uniforms of `shaders.properties` (3.4). pub custom: Custom, pub config: Config, @@ -145,14 +150,22 @@ pub fn dimension_folder(pack: &Pack, id: i32) -> String { } } -/// Loads the post-processing programs of `folder` and, given the game's terrain vertices, the -/// program terrain draws with. +/// The game's geometry a pack may draw with its gbuffers programs: each kind's vertex inputs, +/// when the game can draw it with a pack. +#[derive(Clone, Debug, Default)] +pub struct GameInputs { + pub terrain: Option, + pub sky: Option, +} + +/// Loads the post-processing programs of `folder` and, for the geometry `inputs` describes, the +/// gbuffers programs it draws with. pub fn load_post( pack: &Pack, folder: &str, properties: &Properties, macros: &Macros, - terrain: Option<&GbuffersInputs>, + inputs: &GameInputs, ) -> Post { let mut post = Post { folder: folder.to_owned(), @@ -161,6 +174,7 @@ pub fn load_post( composite_pre: flips(properties, "composite_pre"), terrain: None, water: None, + sky: None, custom: Custom::parse(properties), config: Config::default(), notes: Vec::new(), @@ -191,17 +205,26 @@ pub fn load_post( } } } - if let Some(inputs) = terrain { - let mut gbuffers = |names: &[&str], group| { - names.iter().find_map(|name| { - loader.load(name.to_string(), group, |v, f| { - translate_gbuffers(v, f, Target::Buffers, inputs) - }) + let mut gbuffers = |names: &[&str], group, inputs: &GbuffersInputs| { + names.iter().find_map(|name| { + loader.load(name.to_string(), group, |v, f| { + translate_gbuffers(v, f, Target::Buffers, inputs) }) - }; - let terrain = gbuffers(&TERRAIN, Group::Gbuffers); + }) + }; + // The sky blends as vanilla draws its sunset fan and stars unless `blend.*` says otherwise. + let sky = inputs + .sky + .as_ref() + .and_then(|i| gbuffers(&SKY, Group::Gbuffers, i)); + let sky = sky.map(|mut p| { + p.blend = blend(properties, &p.name).unwrap_or(Some(TRANSLUCENT_BLEND)); + p + }); + if let Some(inputs) = &inputs.terrain { + let terrain = gbuffers(&TERRAIN, Group::Gbuffers, inputs); // gbuffers_water falls back to gbuffers_terrain's chain (2.3). - let water = gbuffers(&WATER, Group::Translucent); + let water = gbuffers(&WATER, Group::Translucent, inputs); loader.post.terrain = terrain; loader.post.water = water.map(|mut p| { p.blend = blend(properties, &p.name).unwrap_or(Some(TRANSLUCENT_BLEND)); @@ -211,6 +234,7 @@ pub fn load_post( p.blend = blend(properties, &p.name).flatten(); } } + loader.post.sky = sky; post.config = std::mem::take(&mut post.config).finish(); post } @@ -433,7 +457,13 @@ mod tests { ); assert_eq!(dimension_folder(&pack, 0), ""); assert_eq!(dimension_folder(&pack, -1), "world-1"); - let post = load_post(&pack, "", &properties, &Macros::standard(), None); + let post = load_post( + &pack, + "", + &properties, + &Macros::standard(), + &GameInputs::default(), + ); let names: Vec<&str> = post.programs.iter().map(|p| p.name.as_str()).collect(); assert_eq!(names, ["deferred", "composite", "composite2", "final"]); let p = &post.programs; @@ -454,14 +484,21 @@ mod tests { let mut macros = Macros::standard(); macros.define("HQ", ""); - let post = load_post(&pack, "world-1", &properties, ¯os, None); + let post = load_post( + &pack, + "world-1", + &properties, + ¯os, + &GameInputs::default(), + ); let names: Vec<&str> = post.programs.iter().map(|p| p.name.as_str()).collect(); assert_eq!(names, ["final"]); - let post = load_post(&pack, "", &properties, ¯os, None); + let post = load_post(&pack, "", &properties, ¯os, &GameInputs::default()); assert!(post.programs.iter().any(|p| p.name == "composite3")); } - /// Water falls back to the terrain chain and blends by default; `blend.*` overrides. + /// Water falls back to the terrain chain and blends by default, as the sky does; `blend.*` + /// overrides. #[test] fn water_falls_back_to_terrain_and_blends() { let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/packs/terrain-test"); @@ -470,12 +507,25 @@ mod tests { let inputs = crate::GbuffersInputs { declarations: "layout(location = 0) in vec3 hz_v_pos;".into(), body: " hz_Vertex = vec4(hz_v_pos, 1.0);\n hz_Color = vec4(1.0);\n".into(), - attributes: Vec::new(), + ..Default::default() }; - let post = load_post(&pack, "", &Properties::default(), ¯os, Some(&inputs)); + let inputs = GameInputs { + terrain: Some(inputs.clone()), + sky: Some(inputs), + }; + let post = load_post(&pack, "", &Properties::default(), ¯os, &inputs); let (terrain, water) = (post.terrain.unwrap(), post.water.unwrap()); assert_eq!((terrain.group, terrain.blend), (Group::Gbuffers, None)); assert_eq!(water.name, "gbuffers_terrain"); + let sky = post.sky.unwrap(); + assert_eq!( + (sky.name.as_str(), sky.group, sky.blend), + ( + "gbuffers_skybasic", + Group::Gbuffers, + Some(TRANSLUCENT_BLEND) + ) + ); assert_eq!( (water.group, water.blend), (Group::Translucent, Some(TRANSLUCENT_BLEND)) @@ -504,7 +554,7 @@ mod tests { &dimension_folder(&pack, 0), &properties, ¯os, - None, + &GameInputs::default(), ); assert!(post.notes.is_empty(), "{:?}", post.notes); let names: Vec<&str> = post.programs.iter().map(|p| p.name.as_str()).collect(); diff --git a/crates/horizon-shaders/src/translate/mod.rs b/crates/horizon-shaders/src/translate/mod.rs index fe736e4d8..35475ddab 100644 --- a/crates/horizon-shaders/src/translate/mod.rs +++ b/crates/horizon-shaders/src/translate/mod.rs @@ -109,6 +109,8 @@ pub struct GbuffersInputs { /// The value of each attribute a pack may declare (`mc_Entity`, `vaPosition`...), as a GLSL /// expression of the declared type's size; an attribute not listed reads zero. pub attributes: Vec<(String, String)>, + /// GLSL statements run last in `main`, with `gl_Position` in wgpu's clip space. + pub after: String, } /// Translates a composite-style program (`composite`, `deferred`, `final`...). @@ -292,7 +294,8 @@ fn translate( } write!( vertex_main, - " hz_main();\n gl_Position = vec4(gl_Position.x, {flip}gl_Position.y, (gl_Position.z + gl_Position.w) * 0.5, gl_Position.w);\n}}\n" + " hz_main();\n gl_Position = vec4(gl_Position.x, {flip}gl_Position.y, (gl_Position.z + gl_Position.w) * 0.5, gl_Position.w);\n{}}}\n", + gbuffers.map_or("", |g| &g.after) ) .unwrap(); let mut fragment_main = String::from("\nvoid main() {\n"); @@ -597,6 +600,7 @@ mod tests { hz_MultiTexCoord1 = vec4(240.0, 240.0, 0.0, 1.0);\n hz_Color = vec4(1.0);\n hz_Normal = vec3(0.0, 1.0, 0.0);\n" .into(), attributes: vec![("mc_midTexCoord".into(), "vec2(game_uv) / 32768.0".into())], + ..Default::default() }; let p = translate_gbuffers( &preprocess(&pack, "gbuffers_terrain.vsh", &m), diff --git a/crates/horizon-shaders/tests/packs/terrain-test/README.md b/crates/horizon-shaders/tests/packs/terrain-test/README.md index bb8ddd060..3fc37bf4d 100644 --- a/crates/horizon-shaders/tests/packs/terrain-test/README.md +++ b/crates/horizon-shaders/tests/packs/terrain-test/README.md @@ -3,6 +3,8 @@ Horizon's own shader pack for testing gbuffers terrain (written for Horizon, no other pack's code). `gbuffers_terrain` draws blocks from the atlas with their vertex colour and lightmap, tinted by the lightmap coordinate's sky light (cooler where the sky is hidden); `mc_midTexCoord` marks each -face's centre with a dot. `composite` puts the depth buffer's view-space distance into the blue +face's centre with a dot, and any stage but solid terrain (translucent terrain falls back to it) +is red. `gbuffers_skybasic` draws the sky in its vertex colour, a little greener, with magenta +stars and a dark red void. `composite` puts the depth buffer's view-space distance into the blue channel of the far half. Copy the folder into `shaderpacks/` and run the game with `HORIZON_SHADER_PACK=terrain-test`. diff --git a/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_skybasic.fsh b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_skybasic.fsh new file mode 100644 index 000000000..8d62f3291 --- /dev/null +++ b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_skybasic.fsh @@ -0,0 +1,15 @@ +#version 120 + +uniform int renderStage; + +varying vec4 glcolor; + +void main() { + vec4 color = glcolor; + // Stars magenta, the void dark red, the sky disc a little greener: each stage is told apart. + if (renderStage == MC_RENDER_STAGE_STARS) color.rgb = vec3(1.0, 0.0, 1.0); + if (renderStage == MC_RENDER_STAGE_VOID) color.rgb = vec3(0.3, 0.0, 0.0); + if (renderStage == MC_RENDER_STAGE_SKY) color.g = min(color.g + 0.2, 1.0); + /* DRAWBUFFERS:0 */ + gl_FragData[0] = color; +} diff --git a/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_skybasic.vsh b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_skybasic.vsh new file mode 100644 index 000000000..7b3bd59ce --- /dev/null +++ b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_skybasic.vsh @@ -0,0 +1,8 @@ +#version 120 + +varying vec4 glcolor; + +void main() { + gl_Position = ftransform(); + glcolor = gl_Color; +} diff --git a/crates/horizon-shaders/tests/real_packs.rs b/crates/horizon-shaders/tests/real_packs.rs index c5d4defac..a04a31b15 100644 --- a/crates/horizon-shaders/tests/real_packs.rs +++ b/crates/horizon-shaders/tests/real_packs.rs @@ -167,19 +167,19 @@ fn real_packs_read() { declarations: "layout(location = 0) in vec3 hz_v_pos;".into(), body: " hz_Vertex = vec4(hz_v_pos, 1.0);\n hz_Color = vec4(1.0);\n".into(), attributes: vec![("mc_Entity".into(), "vec4(-1.0)".into())], + ..Default::default() }; - let post = load_post( - &pack, - &folder, - &shaders, - &Macros::standard(), - Some(&terrain), - ); + let inputs = horizon_shaders::GameInputs { + terrain: Some(terrain.clone()), + sky: Some(terrain), + }; + let post = load_post(&pack, &folder, &shaders, &Macros::standard(), &inputs); println!( - " dimension {id} (folder `{folder}`): {} post programs, terrain {:?}, water {:?}, {} notes, {:.0} ms", + " dimension {id} (folder `{folder}`): {} post programs, terrain {:?}, water {:?}, sky {:?}, {} notes, {:.0} ms", post.programs.len(), post.terrain.as_ref().map(|t| &t.name), post.water.as_ref().map(|t| &t.name), + post.sky.as_ref().map(|t| &t.name), post.notes.len() + post.config.notes.len(), start.elapsed().as_secs_f64() * 1000.0 ); From 230730d1f5324ed3fc7a658e384767f6d706e713 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 04:02:13 +0100 Subject: [PATCH 08/11] Shader packs, clean room 23: the sun and moon (gbuffers_skytextured) gbuffers_skytextured (or gbuffers_textured, gbuffers_basic) draws the frame's sun and moon quads, with their render stages, `texture` reading the shared atlas their sprites live in. The sky's parts draw in vanilla's order (disc, sunset, sun, moon, stars, void), in one render pass while consecutive programs write the same buffers. The sun and moon add to the sky (SRC_ALPHA, ONE) unless blend. says otherwise. Horizon's world pass skips its own sun and moon when the pack draws them; a part without a program stays Horizon's. Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, 2.3, 5.4; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 15 +- crates/horizon-client/src/gfx/pack_post.rs | 298 +++++++++++------- crates/horizon-client/src/gfx/pack_sky.rs | 136 ++++++-- crates/horizon-shaders/src/lib.rs | 4 +- crates/horizon-shaders/src/post.rs | 35 +- .../tests/packs/terrain-test/README.md | 3 +- .../shaders/gbuffers_textured.fsh | 15 + .../shaders/gbuffers_textured.vsh | 10 + crates/horizon-shaders/tests/real_packs.rs | 6 +- 9 files changed, 370 insertions(+), 152 deletions(-) create mode 100644 crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_textured.fsh create mode 100644 crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_textured.vsh diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 7444b136a..bdd8250b8 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -2792,7 +2792,13 @@ impl Renderer { buffers: if flat { &buffers[..1] } else { &buffers }, table: self.flat.as_ref().filter(|_| flat).map(|f| &f.draw_layout), depth_write: true, + atlas: None, }), + celestial: self + .hud + .atlas_texture + .as_ref() + .map(|t| t.create_view(&Default::default())), }; pack_post::PackPost::new( &self.device, @@ -2815,6 +2821,7 @@ impl Renderer { horizon_shaders::GameInputs { terrain: self.pack_terrain_inputs(), sky: Some(pack_sky::inputs()), + celestial: Some(pack_sky::celestial_inputs()), } } @@ -3739,6 +3746,12 @@ impl Renderer { .as_ref() .and_then(|p| p.1.as_ref()) .is_some_and(|p| p.has_sky()); + let pack_draws_celestial = pack_post + && self + .pack_post + .as_ref() + .and_then(|p| p.1.as_ref()) + .is_some_and(|p| p.has_celestial()); let pack_draws_water = pack_draws_terrain && self .pack_post @@ -3814,7 +3827,7 @@ impl Renderer { if frame.end_sky { self.end_sky.draw(&mut pass); } - if celestial_vertices > 0 { + if celestial_vertices > 0 && !pack_draws_celestial { pass.set_vertex_buffer(0, self.celestial_buffer.slice(..)); pass.draw_indexed(0..(celestial_vertices as u32 / 4) * 6, 0, 0..1); } diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index 2334b84c9..9002ad963 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -121,12 +121,13 @@ fn share_samplers(module: &mut naga::Module, slot: &HashMap) { } } -/// The game's geometry a gbuffers pass draws. -#[derive(Clone, Copy, PartialEq)] -enum Gbuffers { - Sky, - Terrain, - Water, +/// The sky's geometry and the programs that draw it: `gbuffers_skybasic` (disc, sunset, stars, +/// void) and `gbuffers_skytextured` (sun, moon), each with its fallbacks. A part without a +/// program is Horizon's own. +struct Sky { + geometry: PackSky, + basic: Option, + textured: Option, } struct Pass { @@ -151,6 +152,8 @@ struct Pass { mipmaps: u16, /// Reads the per-draw block (`DrawUniforms`). draw: bool, + /// What `texture` reads instead of the block atlas. + atlas: Option, groups: HashMap, } @@ -181,6 +184,8 @@ pub(super) struct World<'a> { pub lightmap: wgpu::TextureView, /// Terrain's geometry, when the pack draws it. pub terrain: Option>, + /// The atlas of the sun and moon's sprites. + pub celestial: Option, } pub struct PackPost { @@ -191,7 +196,7 @@ pub struct PackPost { /// What draws translucent terrain (`gbuffers_water` or its fallback), after `deferred`. water: Option, /// What draws the sky (`gbuffers_skybasic` or its fallback), before terrain, and its vertices. - sky: Option<(Pass, PackSky)>, + sky: Option, /// A program after translucent terrain reads `depthtex1`: it is copied before water. depth1: bool, /// Buffers with mipmaps (filterable ones a program asks them for). @@ -494,6 +499,8 @@ pub(super) struct Geometry<'a> { pub table: Option<&'a wgpu::BindGroupLayout>, /// Writes depth (the sky does not). pub depth_write: bool, + /// What `texture` reads instead of the block atlas. + pub atlas: Option, } /// The pass of program `p`: a quad, or `geometry` drawn by the game. `None` (with a note) when @@ -737,6 +744,7 @@ fn build_pass( written, mipmaps: p.mipmaps, draw, + atlas: geometry.and_then(|g| g.atlas.clone()), groups: HashMap::new(), }) } @@ -796,15 +804,29 @@ impl PackPost { .water .zip(world.terrain.as_ref()) .and_then(|(p, g)| build(p, Some(g))); - let sky = post.sky.and_then(|p| { - let sky = PackSky::new(device); - let geometry = Geometry { + let sky = (post.sky.is_some() || post.sky_textured.is_some()).then(|| { + let geometry = PackSky::new(device); + let basic = Geometry { buffers: &PackSky::buffers(), - table: Some(&sky.layout), + table: Some(&geometry.layout), depth_write: false, + atlas: None, }; - Some((build(p, Some(&geometry))?, sky)) + let basic = post.sky.and_then(|p| build(p, Some(&basic))); + let textured = Geometry { + buffers: &PackSky::celestial_buffers(), + table: None, + depth_write: false, + atlas: world.celestial.clone(), + }; + let textured = post.sky_textured.and_then(|p| build(p, Some(&textured))); + Sky { + geometry, + basic, + textured, + } }); + let sky = sky.filter(|s| s.basic.is_some() || s.textured.is_some()); // Developers' aid: `HORIZON_SHADER_PACK_SKIP=composite,final` leaves programs out. let skip = std::env::var("HORIZON_SHADER_PACK_SKIP").unwrap_or_default(); let passes: Vec = post @@ -819,8 +841,13 @@ impl PackPost { if let Some(w) = &water { notes.push(format!("translucent terrain draws with {}", w.name)); } - if let Some((s, _)) = &sky { - notes.push(format!("the sky draws with {}", s.name)); + for (part, pass) in sky + .iter() + .flat_map(|s| [("sky", &s.basic), ("sun and moon", &s.textured)]) + { + if let Some(p) = pass { + notes.push(format!("the {part} draw with {}", p.name)); + } } let depth1 = water.is_some() && passes @@ -837,7 +864,7 @@ impl PackPost { // Gbuffers passes cover part of their targets: a clear is needed if they touch it. let first = sky .iter() - .map(|s| &s.0) + .flat_map(|s| s.basic.iter().chain(&s.textured)) .chain(&terrain) .chain(&water) .find(touched) @@ -1083,7 +1110,11 @@ impl PackPost { Source::Noise => &self.noise, Source::Dummy => &self.dummy, Source::Shadow => &self.shadow.0, - Source::Atlas => self.atlas.as_ref().unwrap_or(&self.dummy), + Source::Atlas => pass + .atlas + .as_ref() + .or(self.atlas.as_ref()) + .unwrap_or(&self.dummy), Source::Lightmap => &self.lightmap, }; let sampler = match SamplerKind::of(s.source, s.filtering) { @@ -1113,46 +1144,12 @@ impl PackPost { group.clone() } - /// The sky, terrain or translucent terrain with the pack's program into the buffers it - /// writes, in place, tested against the world's depth; `draw` issues the game's terrain - /// draws (`true`: translucent), the sky draws itself. - #[allow(clippy::too_many_arguments)] - fn draw_gbuffers( + /// A render pass into the buffers `pass` writes, in place, tested against the world's depth. + fn begin<'e>( &mut self, - device: &wgpu::Device, - queue: &wgpu::Queue, - encoder: &mut wgpu::CommandEncoder, - size: [u32; 2], - frame: &super::Frame, - which: Gbuffers, - draw: &mut dyn FnMut(&mut wgpu::RenderPass<'_>, bool), - ) { - let translucent = which == Gbuffers::Water; - let (mut pass, sky) = match which { - Gbuffers::Sky => match self.sky.take() { - Some((pass, sky)) => (pass, Some(sky)), - None => return, - }, - Gbuffers::Terrain => match self.terrain.take() { - Some(pass) => (pass, None), - None => return, - }, - Gbuffers::Water => match self.water.take() { - Some(pass) => (pass, None), - None => return, - }, - }; - if translucent - && let Some(t) = &self.targets - && let Some((copy, _)) = &t.depth1 - { - encoder.copy_texture_to_texture( - t.depth_texture.as_image_copy(), - copy.as_image_copy(), - t.depth_texture.size(), - ); - } - let group = self.bind(device, queue, &mut pass, size); + encoder: &'e mut wgpu::CommandEncoder, + pass: &Pass, + ) -> wgpu::RenderPass<'e> { let targets = self.targets.as_ref().expect("resized"); let load = wgpu::Operations { load: wgpu::LoadOp::Load, @@ -1168,52 +1165,128 @@ impl PackPost { .as_ref() .and_then(|t| attach(&t[usize::from(self.main & 1 << buffer != 0)], load)); } - { - let timestamp_writes = self.timing.as_mut().and_then(|t| t.writes(&pass.name)); - let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: Some(&pass.name), - color_attachments: &attachments, - timestamp_writes, - depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { - view: &targets.depth, - depth_ops: Some(wgpu::Operations { - load: wgpu::LoadOp::Load, - store: wgpu::StoreOp::Store, - }), - stencil_ops: None, + let timestamp_writes = self.timing.as_mut().and_then(|t| t.writes(&pass.name)); + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some(&pass.name), + color_attachments: &attachments, + timestamp_writes, + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: &targets.depth, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Load, + store: wgpu::StoreOp::Store, }), - ..Default::default() - }); + stencil_ops: None, + }), + ..Default::default() + }) + } + + /// Terrain (or, `translucent`, translucent terrain) with the pack's program; `draw` issues + /// the game's draws. + fn draw_terrain( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + size: [u32; 2], + translucent: bool, + draw: &mut dyn FnMut(&mut wgpu::RenderPass<'_>, bool), + ) { + let slot = if translucent { + &mut self.water + } else { + &mut self.terrain + }; + let Some(mut pass) = slot.take() else { + return; + }; + if translucent + && let Some(t) = &self.targets + && let Some((copy, _)) = &t.depth1 + { + encoder.copy_texture_to_texture( + t.depth_texture.as_image_copy(), + copy.as_image_copy(), + t.depth_texture.size(), + ); + } + let group = self.bind(device, queue, &mut pass, size); + { + let mut rp = self.begin(encoder, &pass); rp.set_pipeline(&pass.pipeline); rp.set_bind_group(0, &group, &[]); - let draws = pass.draw.then_some(&self.draws); - match &sky { - Some(sky) => sky.draw(queue, &mut rp, frame, &|rp, stage| { - if let Some(d) = draws { - d.bind(rp, stage); - } - }), - None => { - if let Some(d) = draws { - let stage = if translucent { - "TERRAIN_TRANSLUCENT" - } else { - "TERRAIN_SOLID" - }; - d.bind(&mut rp, stage); - } - draw(&mut rp, translucent); - } + if pass.draw { + let stage = if translucent { + "TERRAIN_TRANSLUCENT" + } else { + "TERRAIN_SOLID" + }; + self.draws.bind(&mut rp, stage); } + draw(&mut rp, translucent); } self.mips_valid &= !pass.written; - match (which, sky) { - (Gbuffers::Sky, Some(sky)) => self.sky = Some((pass, sky)), - (Gbuffers::Water, _) => self.water = Some(pass), - _ => self.terrain = Some(pass), + if translucent { + self.water = Some(pass); + } else { + self.terrain = Some(pass); } } + /// The sky's parts in vanilla's order, each with the program that draws it; one render pass + /// while consecutive programs write the same buffers. + fn draw_sky( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + size: [u32; 2], + frame: &super::Frame, + ) { + let Some(mut sky) = self.sky.take() else { + return; + }; + let parts = sky.geometry.prepare(queue, frame); + let mut groups = [None, None]; + for (i, pass) in [&mut sky.basic, &mut sky.textured].into_iter().enumerate() { + if let Some(pass) = pass { + groups[i] = Some(self.bind(device, queue, pass, size)); + self.mips_valid &= !pass.written; + } + } + let passes = [&sky.basic, &sky.textured]; + let mut open: Option<(wgpu::RenderPass<'_>, usize)> = None; + for part in parts { + let i = usize::from(part.textured); + let (Some(pass), Some(group)) = (passes[i], &groups[i]) else { + continue; + }; + let current = open.as_ref().map(|o| o.1); + if current != Some(i) { + let same = current + .and_then(|c| passes[c].as_ref()) + .is_some_and(|p| p.writes == pass.writes); + if !same { + drop(open.take()); + open = Some((self.begin(encoder, pass).forget_lifetime(), i)); + } + let rp = &mut open.as_mut().expect("open").0; + rp.set_pipeline(&pass.pipeline); + rp.set_bind_group(0, group, &[]); + sky.geometry.bind(rp, part.textured); + open.as_mut().expect("open").1 = i; + } + let rp = &mut open.as_mut().expect("open").0; + if pass.draw { + self.draws.bind(rp, part.stage); + } + rp.draw(part.range, 0..1); + } + drop(open); + self.sky = Some(sky); + } + /// The sky, when it shows, then terrain. fn draw_opaque( &mut self, @@ -1225,17 +1298,9 @@ impl PackPost { terrain: &mut dyn FnMut(&mut wgpu::RenderPass<'_>, bool), ) { if super::sky_visible(frame) { - self.draw_gbuffers(device, queue, encoder, size, frame, Gbuffers::Sky, terrain); + self.draw_sky(device, queue, encoder, size, frame); } - self.draw_gbuffers( - device, - queue, - encoder, - size, - frame, - Gbuffers::Terrain, - terrain, - ); + self.draw_terrain(device, queue, encoder, size, false, terrain); } /// Regenerates the mip chain of buffer `b`'s side that programs read. @@ -1269,9 +1334,14 @@ impl PackPost { self.terrain.is_some() } - /// Whether the pack draws the sky itself. + /// Whether the pack draws the sky (disc, sunset, stars, void) itself. pub fn has_sky(&self) -> bool { - self.sky.is_some() + self.sky.as_ref().is_some_and(|s| s.basic.is_some()) + } + + /// Whether the pack draws the sun and moon itself. + pub fn has_celestial(&self) -> bool { + self.sky.as_ref().is_some_and(|s| s.textured.is_some()) } /// Whether the pack draws translucent terrain itself. @@ -1521,15 +1591,7 @@ impl PackPost { } if !water_drawn && matches!(pass.group, Group::Composite | Group::Final) { water_drawn = true; - self.draw_gbuffers( - device, - queue, - encoder, - size, - frame, - Gbuffers::Water, - terrain, - ); + self.draw_terrain(device, queue, encoder, size, true, terrain); } if pass.group == Group::Deferred && !seen.0 { seen.0 = true; @@ -1584,15 +1646,7 @@ impl PackPost { self.draw_opaque(device, queue, encoder, size, frame, terrain); } if !water_drawn { - self.draw_gbuffers( - device, - queue, - encoder, - size, - frame, - Gbuffers::Water, - terrain, - ); + self.draw_terrain(device, queue, encoder, size, true, terrain); } let targets = self.targets.as_ref().expect("resized"); let view = |m: u16, i: usize, alt: bool| { diff --git a/crates/horizon-client/src/gfx/pack_sky.rs b/crates/horizon-client/src/gfx/pack_sky.rs index cd99d03b9..007fdc034 100644 --- a/crates/horizon-client/src/gfx/pack_sky.rs +++ b/crates/horizon-client/src/gfx/pack_sky.rs @@ -1,7 +1,8 @@ //! The vanilla sky's geometry for a shader pack's `gbuffers_skybasic` (spec 2.3): the sky disc, //! the sunset fan, the stars and the void disc, as Horizon's own sky draws them (terrain.wgsl, //! stars.rs), built once into a vertex buffer. The frame's colours and rotations sit in a small -//! uniform (set 1) that the vertex inputs read. +//! uniform (set 1) that the vertex inputs read. `gbuffers_skytextured` draws the frame's sun and +//! moon quads, textured from the shared atlas. use std::ops::Range; @@ -36,6 +37,21 @@ struct Uniform { sun: [f32; 4], } +/// A sun or moon vertex: position, atlas UV, RGBA8 colour. +#[repr(C)] +#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] +struct Celestial { + pos: [f32; 3], + uv: [f32; 2], + color: u32, +} + +/// Quads of the sun and moon (and room to spare). +const CELESTIAL_QUADS: usize = 4; + +const CELESTIAL_ATTRIBUTES: [wgpu::VertexAttribute; 3] = + wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x2, 2 => Unorm8x4]; + pub(super) const ATTRIBUTES: [wgpu::VertexAttribute; 2] = wgpu::vertex_attr_array![0 => Float32x3, 1 => Uint32]; @@ -83,6 +99,37 @@ pub fn inputs() -> horizon_shaders::GbuffersInputs { } } +/// What `gbuffers_skytextured` reads from the sun and moon's vertices. +pub fn celestial_inputs() -> horizon_shaders::GbuffersInputs { + horizon_shaders::GbuffersInputs { + declarations: + "layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in vec2 hz_v_uv;\n\ + layout(location = 2) in vec4 hz_v_color;\n" + .into(), + body: " hz_Vertex = vec4(hz_v_pos, 1.0);\n \ + hz_MultiTexCoord0 = vec4(hz_v_uv, 0.0, 1.0);\n \ + hz_MultiTexCoord1 = vec4(0.0, 240.0, 0.0, 1.0);\n \ + hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \ + hz_Color = hz_v_color;\n \ + hz_Normal = vec3(0.0, 0.0, 1.0);\n" + .into(), + attributes: vec![ + ("vaPosition".into(), "hz_Vertex.xyz".into()), + ("vaUV0".into(), "hz_v_uv".into()), + ("vaColor".into(), "hz_Color".into()), + ("vaNormal".into(), "hz_Normal".into()), + ], + after: " gl_Position.z = gl_Position.w;\n".into(), + } +} + +/// One draw of the sky: its render stage, whether `gbuffers_skytextured` draws it, its vertices. +pub(super) struct Part { + pub stage: &'static str, + pub textured: bool, + pub range: Range, +} + /// A disc fan at height `y`, radius 512, as triangles (SkyRenderer's sky and void discs). fn disc(y: f32, kind: u32, out: &mut Vec) { let sign = y.signum(); @@ -104,6 +151,7 @@ fn disc(y: f32, kind: u32, out: &mut Vec) { pub(super) struct PackSky { vertices: wgpu::Buffer, + celestial: wgpu::Buffer, uniform: wgpu::Buffer, pub layout: wgpu::BindGroupLayout, group: wgpu::BindGroup, @@ -144,6 +192,12 @@ impl PackSky { contents: bytemuck::cast_slice(&v), usage: wgpu::BufferUsages::VERTEX, }); + let celestial = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Shader pack sun and moon"), + size: (std::mem::size_of::() * 6 * CELESTIAL_QUADS) as u64, + usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); let uniform = device.create_buffer(&wgpu::BufferDescriptor { label: Some("Shader pack sky"), size: std::mem::size_of::() as u64, @@ -173,6 +227,7 @@ impl PackSky { }); Self { vertices, + celestial, uniform, layout, group, @@ -188,15 +243,18 @@ impl PackSky { })] } - /// Draws the frame's sky, each part with its render stage: the disc, the sunset fan while - /// the sun is near the horizon, the stars and, with the camera low, the void. - pub fn draw( - &self, - queue: &wgpu::Queue, - pass: &mut wgpu::RenderPass<'_>, - frame: &super::Frame, - stage: &dyn Fn(&mut wgpu::RenderPass<'_>, &str), - ) { + pub fn celestial_buffers() -> [Option>; 1] { + [Some(wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::() as u64, + step_mode: wgpu::VertexStepMode::Vertex, + attributes: &CELESTIAL_ATTRIBUTES, + })] + } + + /// Writes the frame's sky and returns its draws in vanilla's order: the disc, the sunset + /// fan while the sun is near the horizon, the sun and the moon, the stars and, with the + /// camera low, the void. + pub fn prepare(&self, queue: &wgpu::Queue, frame: &super::Frame) -> Vec { let sun_angle = frame.sky.sun_angle.to_radians(); // The fan turns to the other horizon in the afternoon (terrain.wgsl's sunrise_weight). let flip = if sun_angle.sin() < 0.0 { 180f32 } else { 0.0 }; @@ -210,20 +268,52 @@ impl PackSky { sun: [theta.cos(), theta.sin(), brightness.clamp(0.0, 1.0), 0.0], }; queue.write_buffer(&self.uniform, 0, bytemuck::bytes_of(&uniform)); - pass.set_bind_group(1, &self.group, &[]); - pass.set_vertex_buffer(0, self.vertices.slice(..)); + // The sun's quad, then the moon's (Game::celestial_geometry), as triangles. + let quads = frame + .celestial + .as_chunks::<4>() + .0 + .iter() + .take(CELESTIAL_QUADS); + let triangles: Vec = quads + .clone() + .flat_map(|q| [0, 1, 2, 0, 2, 3].map(|i| q[i])) + .map(|v| Celestial { + pos: v.pos, + uv: v.uv, + color: v.color, + }) + .collect(); + queue.write_buffer(&self.celestial, 0, bytemuck::cast_slice(&triangles)); let [sky, sunset, stars, void] = self.ranges.clone(); - let parts = [ - ("SKY", sky, true), - ("SUNSET", sunset, frame.sky.sunrise_color[3] > 0.001), - ("STARS", stars, frame.stars.is_some()), - ("VOID", void, frame.dark_disc), - ]; - for (name, range, shown) in parts { - if shown { - stage(pass, name); - pass.draw(range, 0..1); - } + let part = |stage, textured, range| Part { + stage, + textured, + range, + }; + let mut parts = vec![part("SKY", false, sky)]; + if frame.sky.sunrise_color[3] > 0.001 { + parts.push(part("SUNSET", false, sunset)); + } + for (i, stage) in ["SUN", "MOON"].into_iter().enumerate().take(quads.len()) { + parts.push(part(stage, true, i as u32 * 6..i as u32 * 6 + 6)); + } + if frame.stars.is_some() { + parts.push(part("STARS", false, stars)); + } + if frame.dark_disc { + parts.push(part("VOID", false, void)); + } + parts + } + + /// Binds the vertices of the `textured` (sun and moon) or other parts. + pub fn bind(&self, pass: &mut wgpu::RenderPass<'_>, textured: bool) { + if textured { + pass.set_vertex_buffer(0, self.celestial.slice(..)); + } else { + pass.set_bind_group(1, &self.group, &[]); + pass.set_vertex_buffer(0, self.vertices.slice(..)); } } } diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index 853f2f2ba..f33dfec57 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -34,8 +34,8 @@ pub use macros::{MC_VERSION, Macros, RENDER_STAGES, render_stage}; pub use options::switches_on; pub use pack::{Pack, PackError}; pub use post::{ - Blend, Factor, GameInputs, Group, Post, PostProgram, TRANSLUCENT_BLEND, dimension_folder, - load_post, + Blend, CELESTIAL_BLEND, Factor, GameInputs, Group, Post, PostProgram, TRANSLUCENT_BLEND, + dimension_folder, load_post, }; pub use preprocess::{Diagnostic, Origin, Preprocessed, preprocess}; pub use properties::{Entry, Note, Properties}; diff --git a/crates/horizon-shaders/src/post.rs b/crates/horizon-shaders/src/post.rs index 56303e67b..944cf10b8 100644 --- a/crates/horizon-shaders/src/post.rs +++ b/crates/horizon-shaders/src/post.rs @@ -96,6 +96,21 @@ const TERRAIN: [&str; 4] = [ /// `gbuffers_skybasic` and its fallback (2.3). const SKY: [&str; 2] = ["gbuffers_skybasic", "gbuffers_basic"]; +/// `gbuffers_skytextured` and its fallbacks (2.3). +const SKY_TEXTURED: [&str; 3] = [ + "gbuffers_skytextured", + "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, + Factor::One, + Factor::One, + Factor::OneMinusSrcAlpha, +]; + const GROUPS: [(Group, &str); 4] = [ (Group::Prepare, "prepare"), (Group::Deferred, "deferred"), @@ -132,6 +147,8 @@ pub struct Post { pub water: Option, /// What draws the sky (disc, sunset, stars, void) before terrain. pub sky: Option, + /// What draws the sun and moon, between the sunset and the stars. + pub sky_textured: Option, /// The custom uniforms of `shaders.properties` (3.4). pub custom: Custom, pub config: Config, @@ -156,6 +173,7 @@ pub fn dimension_folder(pack: &Pack, id: i32) -> String { pub struct GameInputs { pub terrain: Option, pub sky: Option, + pub celestial: Option, } /// Loads the post-processing programs of `folder` and, for the geometry `inputs` describes, the @@ -175,6 +193,7 @@ pub fn load_post( terrain: None, water: None, sky: None, + sky_textured: None, custom: Custom::parse(properties), config: Config::default(), notes: Vec::new(), @@ -221,6 +240,12 @@ pub fn load_post( p.blend = blend(properties, &p.name).unwrap_or(Some(TRANSLUCENT_BLEND)); p }); + let celestial = inputs.celestial.as_ref(); + let sky_textured = celestial.and_then(|i| gbuffers(&SKY_TEXTURED, Group::Gbuffers, i)); + let sky_textured = sky_textured.map(|mut p| { + p.blend = blend(properties, &p.name).unwrap_or(Some(CELESTIAL_BLEND)); + p + }); if let Some(inputs) = &inputs.terrain { let terrain = gbuffers(&TERRAIN, Group::Gbuffers, inputs); // gbuffers_water falls back to gbuffers_terrain's chain (2.3). @@ -235,6 +260,7 @@ pub fn load_post( } } loader.post.sky = sky; + loader.post.sky_textured = sky_textured; post.config = std::mem::take(&mut post.config).finish(); post } @@ -511,7 +537,8 @@ mod tests { }; let inputs = GameInputs { terrain: Some(inputs.clone()), - sky: Some(inputs), + sky: Some(inputs.clone()), + celestial: Some(inputs), }; let post = load_post(&pack, "", &Properties::default(), ¯os, &inputs); let (terrain, water) = (post.terrain.unwrap(), post.water.unwrap()); @@ -526,6 +553,12 @@ mod tests { Some(TRANSLUCENT_BLEND) ) ); + // gbuffers_skytextured falls back to gbuffers_textured, then gbuffers_basic. + let sun = post.sky_textured.unwrap(); + assert_eq!( + (sun.name.as_str(), sun.blend), + ("gbuffers_textured", Some(CELESTIAL_BLEND)) + ); assert_eq!( (water.group, water.blend), (Group::Translucent, Some(TRANSLUCENT_BLEND)) diff --git a/crates/horizon-shaders/tests/packs/terrain-test/README.md b/crates/horizon-shaders/tests/packs/terrain-test/README.md index 3fc37bf4d..7d2c168cb 100644 --- a/crates/horizon-shaders/tests/packs/terrain-test/README.md +++ b/crates/horizon-shaders/tests/packs/terrain-test/README.md @@ -5,6 +5,7 @@ Horizon's own shader pack for testing gbuffers terrain (written for Horizon, no the lightmap coordinate's sky light (cooler where the sky is hidden); `mc_midTexCoord` marks each face's centre with a dot, and any stage but solid terrain (translucent terrain falls back to it) is red. `gbuffers_skybasic` draws the sky in its vertex colour, a little greener, with magenta -stars and a dark red void. `composite` puts the depth buffer's view-space distance into the blue +stars and a dark red void; `gbuffers_textured` (the fallback of `gbuffers_skytextured`) the sun, +and the moon tinted green. `composite` puts the depth buffer's view-space distance into the blue channel of the far half. Copy the folder into `shaderpacks/` and run the game with `HORIZON_SHADER_PACK=terrain-test`. diff --git a/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_textured.fsh b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_textured.fsh new file mode 100644 index 000000000..563ddeb7c --- /dev/null +++ b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_textured.fsh @@ -0,0 +1,15 @@ +#version 120 + +uniform sampler2D texture; +uniform int renderStage; + +varying vec2 texcoord; +varying vec4 glcolor; + +void main() { + vec4 color = texture2D(texture, texcoord) * glcolor; + // The moon tinted green: the sun and the moon are told apart. + if (renderStage == MC_RENDER_STAGE_MOON) color.rb *= 0.3; + /* DRAWBUFFERS:0 */ + gl_FragData[0] = color; +} diff --git a/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_textured.vsh b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_textured.vsh new file mode 100644 index 000000000..c29b1bba0 --- /dev/null +++ b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_textured.vsh @@ -0,0 +1,10 @@ +#version 120 + +varying vec2 texcoord; +varying vec4 glcolor; + +void main() { + gl_Position = ftransform(); + texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy; + glcolor = gl_Color; +} diff --git a/crates/horizon-shaders/tests/real_packs.rs b/crates/horizon-shaders/tests/real_packs.rs index a04a31b15..5ed954898 100644 --- a/crates/horizon-shaders/tests/real_packs.rs +++ b/crates/horizon-shaders/tests/real_packs.rs @@ -171,15 +171,17 @@ fn real_packs_read() { }; let inputs = horizon_shaders::GameInputs { terrain: Some(terrain.clone()), - sky: Some(terrain), + sky: Some(terrain.clone()), + celestial: Some(terrain), }; let post = load_post(&pack, &folder, &shaders, &Macros::standard(), &inputs); println!( - " dimension {id} (folder `{folder}`): {} post programs, terrain {:?}, water {:?}, sky {:?}, {} notes, {:.0} ms", + " dimension {id} (folder `{folder}`): {} post programs, terrain {:?}, water {:?}, sky {:?} {:?}, {} notes, {:.0} ms", post.programs.len(), post.terrain.as_ref().map(|t| &t.name), post.water.as_ref().map(|t| &t.name), post.sky.as_ref().map(|t| &t.name), + post.sky_textured.as_ref().map(|t| &t.name), post.notes.len() + post.config.notes.len(), start.elapsed().as_secs_f64() * 1000.0 ); From b940754555666ba25fa8541e537b7b9a83aa0449 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 04:23:38 +0100 Subject: [PATCH 09/11] Terrain vertex arena readable as storage where flat draws run The terrain vertex arena's pages also get STORAGE usage on devices that run flat draws (vertex storage and compute), so a shader pack's terrain program can read the other corners of a vertex's quad to give packs a face normal and tangent. Horizon's own pipelines are unchanged; the A/B in the PR shows no frame-time change without a pack. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 5 ++++- crates/horizon-client/src/gfx/arena.rs | 15 +++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index bdd8250b8..1883e6077 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -2140,13 +2140,16 @@ impl Renderer { usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); + let flat_wanted = flat::wanted(&adapter, &device); + // Readable as storage where flat draws run: a shader pack's terrain program reads them. let arena = arena::Arena::new( &device, std::mem::size_of::() as u64, 1 << 20, + flat_wanted, ); // Sections as one index list per frame where multi-draw is emulated. - let flat = flat::wanted(&adapter, &device).then(|| { + let flat = flat_wanted.then(|| { let buffers = [Some(wgpu::VertexBufferLayout { array_stride: std::mem::size_of::() as u64, step_mode: wgpu::VertexStepMode::Vertex, diff --git a/crates/horizon-client/src/gfx/arena.rs b/crates/horizon-client/src/gfx/arena.rs index 8da835aa2..dbbf70140 100644 --- a/crates/horizon-client/src/gfx/arena.rs +++ b/crates/horizon-client/src/gfx/arena.rs @@ -65,8 +65,15 @@ pub struct Arena { } impl Arena { - pub fn new(device: &wgpu::Device, stride: u64, capacity: u64) -> Self { - Self::with_usage(device, stride, capacity, wgpu::BufferUsages::VERTEX) + /// Vertices; `storage` also lets shaders read them as a storage buffer (a shader pack's + /// terrain program reads the other corners of a vertex's quad for its normal). + pub fn new(device: &wgpu::Device, stride: u64, capacity: u64, storage: bool) -> Self { + let usage = if storage { + wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::STORAGE + } else { + wgpu::BufferUsages::VERTEX + }; + Self::with_usage(device, stride, capacity, usage) } /// Sorted translucent indices; `storage` when compute passes read them. pub fn indices(device: &wgpu::Device, capacity: u64, storage: bool) -> Self { @@ -352,7 +359,7 @@ mod tests { eprintln!("no GPU adapter: skipped"); return; }; - let mut arena = Arena::new(&device, 4, GRAIN); + let mut arena = Arena::new(&device, 4, GRAIN, false); arena.page_limit = 2 * GRAIN; assert_eq!(arena.bytes(), 0); let (first, a) = alloc(&mut arena, &device, &queue, GRAIN).unwrap(); @@ -395,7 +402,7 @@ mod tests { return; }; for mut arena in [ - Arena::new(&device, 28, GRAIN), + Arena::new(&device, 28, GRAIN, false), Arena::indices(&device, GRAIN, false), ] { alloc(&mut arena, &device, &queue, GRAIN).unwrap(); From 441f749c25507bfceedc1d618f72e72522e4cf4d Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 04:59:39 +0100 Subject: [PATCH 10/11] Shader packs, clean room 24: mc_Entity from block.properties block.properties (spec 4.1) is read: `block.=` names with optional namespaces and state filters; the most specific matching entry wins. Terrain vertices carry their sprite layer, not their block, so the game builds a table of the block sprite layers: each takes the id of the block named like its sprite, else the id most blocks drawing it share; y is 1 on the water and lava sprites. Flat terrain reads it from a storage buffer at set 3; other draws and packs without the file read (-1, -1). Building the table takes 14 ms for BSL, 39 ms for Complementary. Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, 4.1, 5.1, 5.3; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/src/game/shader_packs.rs | 101 ++++++++++++++- crates/horizon-client/src/gfx.rs | 6 +- crates/horizon-client/src/gfx/pack_atlas.rs | 11 +- crates/horizon-client/src/gfx/pack_post.rs | 71 ++++++++++- crates/horizon-shaders/src/block_ids.rs | 120 ++++++++++++++++++ crates/horizon-shaders/src/lib.rs | 2 + .../tests/packs/terrain-test/README.md | 2 +- .../terrain-test/shaders/block.properties | 3 + .../terrain-test/shaders/gbuffers_terrain.fsh | 3 + .../terrain-test/shaders/gbuffers_terrain.vsh | 3 + 10 files changed, 314 insertions(+), 8 deletions(-) create mode 100644 crates/horizon-shaders/src/block_ids.rs create mode 100644 crates/horizon-shaders/tests/packs/terrain-test/shaders/block.properties diff --git a/crates/horizon-client/src/game/shader_packs.rs b/crates/horizon-client/src/game/shader_packs.rs index 360262640..7fa2ea3f6 100644 --- a/crates/horizon-client/src/game/shader_packs.rs +++ b/crates/horizon-client/src/game/shader_packs.rs @@ -4,7 +4,7 @@ use horizon_client::camera::CameraFluid; use horizon_client::gfx::pack_uniforms::PackInputs; -use horizon_shaders::{Macros, Pack, Properties, dimension_folder, load_post}; +use horizon_shaders::{BlockIds, Macros, Pack, Properties, dimension_folder, load_post}; use super::{Dim, Game}; @@ -44,7 +44,21 @@ impl Game<'_> { let folder = dimension_folder(&pack, dimension); let inputs = renderer.pack_inputs(); let post = load_post(&pack, &folder, &properties, ¯os, &inputs); - if let Some((programs, notes)) = renderer.set_shader_pack(dimension, Some(post)) + let text = pack.read_text("block.properties").unwrap_or_default(); + let ids = BlockIds::parse(&Properties::parse(&text, ¯os)); + for note in &ids.notes { + log::warn!("shader pack {name}: block.properties: {note}"); + } + let started_ids = std::time::Instant::now(); + let table = block_id_table(&self.pack, &ids); + log::info!( + "shader pack {name}: mc_Entity for {} of {} block sprites in {} ms", + table.chunks(2).filter(|p| p[0] != -1).count(), + table.len() / 2, + started_ids.elapsed().as_millis() + ); + if let Some((programs, notes)) = + renderer.set_shader_pack(dimension, Some(post), &table) { for note in notes { log::warn!("shader pack {name}: {note}"); @@ -59,7 +73,7 @@ impl Game<'_> { } Err(e) => { log::warn!("shader pack {name}: {e}"); - renderer.set_shader_pack(dimension, None); + renderer.set_shader_pack(dimension, None, &[]); } } } @@ -83,3 +97,84 @@ impl Game<'_> { } } } + +/// `mc_Entity` (x, y) of each block sprite layer, as (id, fluid) pairs: terrain vertices carry +/// their sprite layer, not their block. A layer takes the id of the block named like its sprite +/// (`block/oak_leaves` for `oak_leaves`), else the id most blocks drawing it share (ties: the +/// lowest); -1 when no block drawing it has one. y is 1 for the fluid sprites, -1 otherwise. +fn block_id_table(pack: &super::PackResources, ids: &BlockIds) -> Vec { + use std::collections::{BTreeMap, BTreeSet}; + let layers = pack + .render + .sprite_layers + .values() + .max() + .map_or(0, |l| l + 1) as usize; + let mut table = vec![-1; layers * 2]; + if ids.is_empty() { + return table; + } + let registry = pack.assets.registry; + let catalog = &pack.render.model_catalog; + let sprite = |name: &str| pack.render.sprite_layers.get(name).copied(); + // (layer, id) -> (a block named like the sprite, the blocks). + let mut votes: BTreeMap<(u32, i32), (bool, BTreeSet<&str>)> = BTreeMap::new(); + for i in 0..registry.len() { + let state_id = horizon_world::registry::BlockStateId(i as u32); + let Some(state) = registry.state(state_id) else { + continue; + }; + let Some(id) = ids.id(&state.name, |k| state.properties.get(k).map(String::as_str)) else { + continue; + }; + let block = state.name.strip_prefix("minecraft:").unwrap_or(&state.name); + let mut vote = |layer: u32| { + let named = pack + .layer_names + .get(&layer) + .and_then(|n| n.strip_prefix("block/")) + == Some(block); + let v = votes.entry((layer, id)).or_default(); + v.0 |= named; + v.1.insert(&state.name); + }; + // Fluids draw from their own sprites. + let fluid = match block { + "water" => ["water_still", "water_flow"], + "lava" => ["lava_still", "lava_flow"], + _ => ["", ""], + }; + for f in fluid.iter().filter(|f| !f.is_empty()) { + if let Some(l) = sprite(&format!("minecraft:block/{f}")) { + vote(l); + } + } + let origin = horizon_core::position::BlockPos::new(0, 0, 0); + for (_, model) in catalog.collect(state_id, origin).unwrap_or_default() { + for face in &model.faces { + vote(face.layer); + } + } + } + let mut best: BTreeMap = BTreeMap::new(); + for ((layer, id), (named, blocks)) in votes { + let candidate = (named, blocks.len(), -id); + let b = best.entry(layer).or_insert(candidate); + if candidate > *b { + *b = candidate; + } + } + for (layer, (_, _, id)) in best { + if let Some(slot) = table.get_mut(layer as usize * 2) { + *slot = -id; + } + } + for f in ["water_still", "water_flow", "lava_still", "lava_flow"] { + if let Some(l) = sprite(&format!("minecraft:block/{f}")) + && let Some(slot) = table.get_mut(l as usize * 2 + 1) + { + *slot = 1; + } + } + table +} diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 1883e6077..9b7fcc303 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -2764,12 +2764,14 @@ impl Renderer { self.world_post.scale = scale.clamp(0.25, 2.0); self.world_post.antialias = antialias; } - /// Runs `post` over the world from now on (`None`: no pack), for the dimension `dimension`. + /// Runs `post` over the world from now on (`None`: no pack), for the dimension `dimension`; + /// `block_ids` gives each block sprite layer's `mc_Entity` (id, fluid) pair. /// Returns the programs that run and the notes of loading them. pub fn set_shader_pack( &mut self, dimension: i32, pack: Option, + block_ids: &[i32], ) -> Option<(Vec, Vec)> { if pack.is_none() { self.pack_atlas = None; @@ -2796,7 +2798,9 @@ impl Renderer { table: self.flat.as_ref().filter(|_| flat).map(|f| &f.draw_layout), depth_write: true, atlas: None, + ids: None, }), + block_ids, celestial: self .hud .atlas_texture diff --git a/crates/horizon-client/src/gfx/pack_atlas.rs b/crates/horizon-client/src/gfx/pack_atlas.rs index 1a14a8da9..ac56985fd 100644 --- a/crates/horizon-client/src/gfx/pack_atlas.rs +++ b/crates/horizon-client/src/gfx/pack_atlas.rs @@ -127,6 +127,10 @@ impl PackAtlas { "layout(set = 1, binding = 0) readonly buffer HzBlockSlots { uint hz_block_slots[]; };\n\ layout(set = 1, binding = 1) readonly buffer HzSlotOrigins { vec4 hz_slot_origins[]; };\n", ); + // `mc_Entity` of each sprite layer: id, then 1 for fluids or -1 (the game's table). + declarations.push_str( + "layout(set = 3, binding = 0) readonly buffer HzBlockIds { int hz_block_ids[]; };\n", + ); "hz_slot_origins[hz_block_slots[uint(gl_VertexIndex) >> 8u]]" } else { declarations.push_str("layout(location = 5) in vec4 hz_v_origin;\n"); @@ -153,8 +157,13 @@ impl PackAtlas { scale[0], scale[1], ); declarations.push_str("vec2 hz_cell; vec2 hz_scale;\n"); + let entity = if flat { + "vec4(float(hz_block_ids[2u * hz_layer]), float(hz_block_ids[2u * hz_layer + 1u]), 0.0, 0.0)" + } else { + "vec4(-1.0, -1.0, 0.0, 0.0)" + }; let attributes = [ - ("mc_Entity", "vec4(-1.0, -1.0, 0.0, 0.0)"), + ("mc_Entity", entity), ( "mc_midTexCoord", "vec4((hz_cell + 0.5) * hz_scale, 0.0, 1.0)", diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index 9002ad963..ba8fe62e7 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -154,6 +154,8 @@ struct Pass { draw: bool, /// What `texture` reads instead of the block atlas. atlas: Option, + /// Reads `mc_Entity`'s table (`PackPost::block_ids`). + ids: bool, groups: HashMap, } @@ -184,6 +186,8 @@ pub(super) struct World<'a> { pub lightmap: wgpu::TextureView, /// Terrain's geometry, when the pack draws it. pub terrain: Option>, + /// `mc_Entity` of each block sprite layer: (id, 1 for fluids or -1) pairs. + pub block_ids: &'a [i32], /// The atlas of the sun and moon's sprites. pub celestial: Option, } @@ -207,6 +211,8 @@ pub struct PackPost { down: HashMap, /// The per-draw uniforms of gbuffers programs. draws: DrawUniforms, + /// `mc_Entity`'s table of flat terrain, and its bind group. + block_ids: Option<(wgpu::BindGroupLayout, wgpu::BindGroup)>, /// `HORIZON_SHADER_PACK_TIMING`: GPU time of each pass, in the log. timing: Option, /// `shaders.properties`' custom uniforms, after the frame's in `uniforms.values`. @@ -492,6 +498,42 @@ fn attach( }) } +/// `mc_Entity`'s table as a storage buffer (vertex stage) at set 3; -1 pairs when empty. +fn block_ids(device: &wgpu::Device, table: &[i32]) -> (wgpu::BindGroupLayout, wgpu::BindGroup) { + let empty = [-1, -1]; + let table = if table.is_empty() { &empty[..] } else { table }; + let buffer = wgpu::util::DeviceExt::create_buffer_init( + device, + &wgpu::util::BufferInitDescriptor { + label: Some("Shader pack block ids"), + contents: bytemuck::cast_slice(table), + usage: wgpu::BufferUsages::STORAGE, + }, + ); + let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("Shader pack block ids"), + entries: &[wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::VERTEX, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Storage { read_only: true }, + has_dynamic_offset: false, + min_binding_size: None, + }, + count: None, + }], + }); + let group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Shader pack block ids"), + layout: &layout, + entries: &[wgpu::BindGroupEntry { + binding: 0, + resource: buffer.as_entire_binding(), + }], + }); + (layout, group) +} + /// The game's geometry a gbuffers program draws: its vertex buffers and, for flat draws /// (flat.rs), the layout of their bind group 1. pub(super) struct Geometry<'a> { @@ -501,6 +543,8 @@ pub(super) struct Geometry<'a> { pub depth_write: bool, /// What `texture` reads instead of the block atlas. pub atlas: Option, + /// `mc_Entity`'s table at set 3 (flat terrain). + pub ids: Option<&'a wgpu::BindGroupLayout>, } /// The pass of program `p`: a quad, or `geometry` drawn by the game. `None` (with a note) when @@ -663,6 +707,7 @@ fn build_pass( Some(&layout), geometry.and_then(|g| g.table), draw.then_some(draws), + geometry.and_then(|g| g.ids), ], ..Default::default() }); @@ -745,6 +790,7 @@ fn build_pass( mipmaps: p.mipmaps, draw, atlas: geometry.and_then(|g| g.atlas.clone()), + ids: geometry.is_some_and(|g| g.ids.is_some()), groups: HashMap::new(), }) } @@ -796,13 +842,26 @@ impl PackPost { used |= pass.reads | pass.written | pass.flip; Some(pass) }; + // Flat terrain reads `mc_Entity` from a table of the sprite layers. + let block_ids = world + .terrain + .as_ref() + .filter(|g| g.table.is_some()) + .map(|_| block_ids(device, world.block_ids)); + let terrain_geometry = world.terrain.as_ref().map(|g| Geometry { + buffers: g.buffers, + table: g.table, + depth_write: g.depth_write, + atlas: g.atlas.clone(), + ids: block_ids.as_ref().map(|i| &i.0), + }); let terrain = post .terrain - .zip(world.terrain.as_ref()) + .zip(terrain_geometry.as_ref()) .and_then(|(p, g)| build(p, Some(g))); let water = post .water - .zip(world.terrain.as_ref()) + .zip(terrain_geometry.as_ref()) .and_then(|(p, g)| build(p, Some(g))); let sky = (post.sky.is_some() || post.sky_textured.is_some()).then(|| { let geometry = PackSky::new(device); @@ -811,6 +870,7 @@ impl PackPost { table: Some(&geometry.layout), depth_write: false, atlas: None, + ids: None, }; let basic = post.sky.and_then(|p| build(p, Some(&basic))); let textured = Geometry { @@ -818,6 +878,7 @@ impl PackPost { table: None, depth_write: false, atlas: world.celestial.clone(), + ids: None, }; let textured = post.sky_textured.and_then(|p| build(p, Some(&textured))); Sky { @@ -981,6 +1042,7 @@ impl PackPost { mips_valid: 0, down, draws, + block_ids, timing: super::pack_timing::PassTiming::new(device, queue), custom, atlas: world.atlas.as_ref().map(|a| a.0.clone()), @@ -1216,6 +1278,11 @@ impl PackPost { let mut rp = self.begin(encoder, &pass); rp.set_pipeline(&pass.pipeline); rp.set_bind_group(0, &group, &[]); + if pass.ids + && let Some((_, ids)) = &self.block_ids + { + rp.set_bind_group(3, ids, &[]); + } if pass.draw { let stage = if translucent { "TERRAIN_TRANSLUCENT" diff --git a/crates/horizon-shaders/src/block_ids.rs b/crates/horizon-shaders/src/block_ids.rs new file mode 100644 index 000000000..623473283 --- /dev/null +++ b/crates/horizon-shaders/src/block_ids.rs @@ -0,0 +1,120 @@ +//! `block.properties` (spec 4.1): `block.= ...`, each block +//! `[namespace:]name` with optional state filters `:=,`. Values in one filter +//! are alternatives; separate filters must all match. Names the game does not know are simply +//! never asked for. Block tags (`%tag`, tier E) are not read yet. + +use crate::Properties; + +#[derive(Clone, Debug, PartialEq)] +struct Rule { + id: i32, + /// `minecraft:stone`. + name: String, + filters: Vec<(String, Vec)>, +} + +/// The ids `block.properties` gives block states. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct BlockIds { + rules: Vec, + /// Lines skipped and why. + pub notes: Vec, +} + +impl BlockIds { + pub fn parse(properties: &Properties) -> Self { + let mut out = Self::default(); + for e in &properties.entries { + let Some(id) = e.key.strip_prefix("block.") else { + continue; + }; + let Ok(id) = id.parse::() else { + // `layer.*` (4.2) and other keys are not ids. + continue; + }; + for word in e.value.split_whitespace() { + if word.starts_with('%') { + out.notes.push(format!( + "line {}: block tags are not read yet: {word}", + e.line + )); + continue; + } + let (names, filters): (Vec<&str>, Vec<&str>) = + word.split(':').partition(|p| !p.contains('=')); + let name = match names[..] { + [name] => format!("minecraft:{name}"), + [namespace, name] => format!("{namespace}:{name}"), + _ => { + out.notes + .push(format!("line {}: not a block name: {word}", e.line)); + continue; + } + }; + let filters = filters + .iter() + .filter_map(|f| f.split_once('=')) + .map(|(k, v)| (k.to_owned(), v.split(',').map(str::to_owned).collect())) + .collect(); + out.rules.push(Rule { id, name, filters }); + } + } + out + } + + /// The id of block `name` (`minecraft:stone`) in the state with `properties`. The rule with + /// the most filters that all match wins, then the first in the file (one name belongs to + /// one id; a filtered entry is the more specific one). + pub fn id<'a>(&self, name: &str, properties: impl Fn(&str) -> Option<&'a str>) -> Option { + self.rules + .iter() + .filter(|r| r.name == name) + .filter(|r| { + r.filters + .iter() + .all(|(k, vs)| properties(k).is_some_and(|v| vs.iter().any(|x| x == v))) + }) + .min_by_key(|r| std::cmp::Reverse(r.filters.len())) + .map(|r| r.id) + } + + pub fn is_empty(&self) -> bool { + self.rules.is_empty() + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::Macros; + + #[test] + fn names_states_and_precedence() { + let text = "block.10=stone minecraft:dirt mod:thing \\\n grass_block:snowy=false\n\ + block.11=grass_block\nblock.12=oak_leaves:persistent=true:distance=1,2 %leaves\n\ + layer.translucent=glass\n"; + let ids = BlockIds::parse(&Properties::parse(text, &Macros::standard())); + let none = |_: &str| None; + assert_eq!(ids.id("minecraft:stone", none), Some(10)); + assert_eq!(ids.id("mod:thing", none), Some(10)); + assert_eq!(ids.id("minecraft:glass", none), None); + let snowy = |s: &'static str| move |k: &str| (k == "snowy").then_some(s); + // The filtered entry is the more specific one. + assert_eq!(ids.id("minecraft:grass_block", snowy("false")), Some(10)); + assert_eq!(ids.id("minecraft:grass_block", snowy("true")), Some(11)); + let leaves = |p: &'static str, d: &'static str| { + move |k: &str| match k { + "persistent" => Some(p), + "distance" => Some(d), + _ => None, + } + }; + assert_eq!( + ids.id("minecraft:oak_leaves", leaves("true", "2")), + Some(12) + ); + assert_eq!(ids.id("minecraft:oak_leaves", leaves("true", "3")), None); + assert_eq!(ids.id("minecraft:oak_leaves", leaves("false", "1")), None); + assert_eq!(ids.notes.len(), 1, "{:?}", ids.notes); + } +} diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index f33dfec57..fbad9bee0 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -12,6 +12,7 @@ //! - [`load_post`]: a dimension's `prepare`, `deferred`, `composite` and `final` programs, in order (2.1). //! - [`Macros`] and [`eval_condition`]: the macros packs test, and the `#if` arithmetic (1.5). +mod block_ids; mod condition; mod config; mod custom; @@ -25,6 +26,7 @@ mod preprocess; mod properties; mod translate; +pub use block_ids::BlockIds; pub use condition::eval_condition; pub use config::{BUFFERS, Buffer, Config, Format, Kind, buffer_index, draw_buffers}; pub use custom::{Custom, Val}; diff --git a/crates/horizon-shaders/tests/packs/terrain-test/README.md b/crates/horizon-shaders/tests/packs/terrain-test/README.md index 7d2c168cb..13745ed66 100644 --- a/crates/horizon-shaders/tests/packs/terrain-test/README.md +++ b/crates/horizon-shaders/tests/packs/terrain-test/README.md @@ -3,7 +3,7 @@ Horizon's own shader pack for testing gbuffers terrain (written for Horizon, no other pack's code). `gbuffers_terrain` draws blocks from the atlas with their vertex colour and lightmap, tinted by the lightmap coordinate's sky light (cooler where the sky is hidden); `mc_midTexCoord` marks each -face's centre with a dot, and any stage but solid terrain (translucent terrain falls back to it) +face's centre with a dot; leaves (id 1 in `block.properties`) are yellow, and any stage but solid terrain (translucent terrain falls back to it) is red. `gbuffers_skybasic` draws the sky in its vertex colour, a little greener, with magenta stars and a dark red void; `gbuffers_textured` (the fallback of `gbuffers_skytextured`) the sun, and the moon tinted green. `composite` puts the depth buffer's view-space distance into the blue diff --git a/crates/horizon-shaders/tests/packs/terrain-test/shaders/block.properties b/crates/horizon-shaders/tests/packs/terrain-test/shaders/block.properties new file mode 100644 index 000000000..cd08c6a16 --- /dev/null +++ b/crates/horizon-shaders/tests/packs/terrain-test/shaders/block.properties @@ -0,0 +1,3 @@ +# Leaves draw yellow in gbuffers_terrain. +block.1=oak_leaves spruce_leaves birch_leaves jungle_leaves acacia_leaves dark_oak_leaves \ + mangrove_leaves cherry_leaves azalea_leaves flowering_azalea_leaves pale_oak_leaves diff --git a/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.fsh b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.fsh index 8be409929..4f6850575 100644 --- a/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.fsh +++ b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.fsh @@ -9,6 +9,7 @@ varying vec2 texcoord; varying vec2 lmcoord; varying vec2 mid; varying vec4 glcolor; +varying float blockId; void main() { vec4 color = texture2D(texture, texcoord) * glcolor; @@ -16,6 +17,8 @@ void main() { color.rgb *= texture2D(lightmap, lmcoord).rgb; // Less sky light: bluer. color.rgb = mix(color.rgb * vec3(0.6, 0.7, 1.2), color.rgb, lmcoord.y); + // Leaves (block.properties' id 1) yellow. + if (abs(blockId - 1.0) < 0.5) color.rgb = vec3(color.g, color.g, 0.0); // A dot at the face's centre, one texel wide. if (length((texcoord - mid) * vec2(atlasSize)) < 1.0) color.rgb = vec3(1.0, 0.0, 1.0); // Any other stage than solid terrain (translucent terrain falls back here): red. diff --git a/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.vsh b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.vsh index bc16f4aad..1f26fd658 100644 --- a/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.vsh +++ b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.vsh @@ -1,11 +1,13 @@ #version 120 attribute vec4 mc_midTexCoord; +attribute vec4 mc_Entity; varying vec2 texcoord; varying vec2 lmcoord; varying vec2 mid; varying vec4 glcolor; +varying float blockId; void main() { gl_Position = ftransform(); @@ -13,4 +15,5 @@ void main() { lmcoord = (gl_TextureMatrix[1] * gl_MultiTexCoord1).xy; mid = mc_midTexCoord.xy; glcolor = gl_Color; + blockId = mc_Entity.x; } From 732efe69bbe9d05c72f9d9dc9e92de920f37388b Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 05:16:38 +0100 Subject: [PATCH 11/11] Shader packs, clean room 25: terrain normals and tangents Flat terrain's vertex stage reads its quad's first three corners from the arena page (bound per page at set 3, beside mc_Entity's table) and gives gl_Normal / vaNormal the face normal and at_tangent the tangent along the texture's u, with w the handedness of bitangent = cross(tangent, normal) (spec 5.1). The pack's terrain and water pipelines cull back faces as the game's own terrain does (clockwise front faces, rows being flipped to GL's): water's surface no longer shows its downward copy. Flat draws call a hook per arena page (Flat::draw_with); the arena lists its page buffers. Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, 5.1; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 61 ++++++----- crates/horizon-client/src/gfx/arena.rs | 5 + crates/horizon-client/src/gfx/flat.rs | 12 +++ crates/horizon-client/src/gfx/pack_atlas.rs | 41 +++++++- crates/horizon-client/src/gfx/pack_post.rs | 106 +++++++++++++------- 5 files changed, 162 insertions(+), 63 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 9b7fcc303..f829b9eb1 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -2799,6 +2799,7 @@ impl Renderer { depth_write: true, atlas: None, ids: None, + cull: true, }), block_ids, celestial: self @@ -4070,31 +4071,42 @@ impl Renderer { { let size = self.world_post.size().unwrap_or([1, 1]); let flat = self.flat.as_ref().filter(|_| flat_frame); - let mut terrain = |rp: &mut wgpu::RenderPass<'_>, translucent: bool| { - if !pack_draws_terrain { - return; - } - match flat { - Some(flat) => { - let calls = if translucent { - &flat.calls[flat.solid_calls..] - } else { - &flat.calls[..flat.solid_calls] - }; - flat.draw(rp, calls, |p| self.arena.buffer(p)); - } - None => { - rp.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); - rp.set_vertex_buffer(1, self.instances.slice(..)); - let (first, count) = if translucent { - (solid_draws, translucent_draws) - } else { - (0, solid_draws) - }; - self.draw_sections(rp, &self.indirect, 0, first, count); - } - } + let pages: Vec<(u32, wgpu::Buffer)> = match flat { + Some(_) if pack_draws_terrain => self + .arena + .page_buffers() + .map(|(p, b)| (p, b.clone())) + .collect(), + _ => Vec::new(), }; + let mut terrain = + |rp: &mut wgpu::RenderPass<'_>, + translucent: bool, + page: &dyn Fn(&mut wgpu::RenderPass<'_>, u32)| { + if !pack_draws_terrain { + return; + } + match flat { + Some(flat) => { + let calls = if translucent { + &flat.calls[flat.solid_calls..] + } else { + &flat.calls[..flat.solid_calls] + }; + flat.draw_with(rp, calls, |p| self.arena.buffer(p), page); + } + None => { + rp.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); + rp.set_vertex_buffer(1, self.instances.slice(..)); + let (first, count) = if translucent { + (solid_draws, translucent_draws) + } else { + (0, solid_draws) + }; + self.draw_sections(rp, &self.indirect, 0, first, count); + } + } + }; post.run( &self.device, &self.queue, @@ -4106,6 +4118,7 @@ impl Renderer { &frame.pack, size, [self.config.width, self.config.height], + &pages, &mut terrain, ); self.pack_post = Some((dimension, Some(post))); diff --git a/crates/horizon-client/src/gfx/arena.rs b/crates/horizon-client/src/gfx/arena.rs index dbbf70140..34b4d332d 100644 --- a/crates/horizon-client/src/gfx/arena.rs +++ b/crates/horizon-client/src/gfx/arena.rs @@ -155,6 +155,11 @@ impl Arena { pub fn has_page(&self, page: u32) -> bool { self.pages.get(page as usize).is_some_and(Option::is_some) } + /// Each live page's buffer, by page. + pub fn page_buffers(&self) -> impl Iterator { + let pages = self.pages.iter().enumerate(); + pages.filter_map(|(i, p)| Some((i as u32, &p.as_ref()?.buffer))) + } pub fn buffer(&self, page: u32) -> &wgpu::Buffer { &self.pages[page as usize] .as_ref() diff --git a/crates/horizon-client/src/gfx/flat.rs b/crates/horizon-client/src/gfx/flat.rs index 4c337025a..d167e3f3c 100644 --- a/crates/horizon-client/src/gfx/flat.rs +++ b/crates/horizon-client/src/gfx/flat.rs @@ -649,10 +649,22 @@ impl Flat { pass: &mut wgpu::RenderPass<'_>, calls: &[Call], vertices: impl Fn(u32) -> &'a wgpu::Buffer, + ) { + self.draw_with(pass, calls, vertices, |_, _| {}); + } + /// `draw`, calling `page` before each run of calls from one arena page (a shader pack binds + /// that page's vertices for reading). + pub fn draw_with<'a>( + &self, + pass: &mut wgpu::RenderPass<'_>, + calls: &[Call], + vertices: impl Fn(u32) -> &'a wgpu::Buffer, + page: impl Fn(&mut wgpu::RenderPass<'_>, u32), ) { pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); for call in calls { self.bind_page(pass, call.page, &vertices); + page(pass, call.page); pass.draw_indexed(call.first..call.first + call.count, 0, 0..1); } } diff --git a/crates/horizon-client/src/gfx/pack_atlas.rs b/crates/horizon-client/src/gfx/pack_atlas.rs index ac56985fd..e1727f1fe 100644 --- a/crates/horizon-client/src/gfx/pack_atlas.rs +++ b/crates/horizon-client/src/gfx/pack_atlas.rs @@ -113,6 +113,30 @@ impl PackAtlas { } } +/// GLSL: the face normal and tangent of a flat-drawn vertex, from its quad's first three corners +/// (quads are four consecutive vertices of the page; position words 0-2, UV word 3). +const FACE: &str = " uint hz_q = (uint(gl_VertexIndex) & ~3u) * 6u;\n \ + vec3 hz_p0 = uintBitsToFloat(uvec3(hz_vertices[hz_q], hz_vertices[hz_q + 1u], hz_vertices[hz_q + 2u]));\n \ + vec3 hz_p1 = uintBitsToFloat(uvec3(hz_vertices[hz_q + 6u], hz_vertices[hz_q + 7u], hz_vertices[hz_q + 8u]));\n \ + vec3 hz_p2 = uintBitsToFloat(uvec3(hz_vertices[hz_q + 12u], hz_vertices[hz_q + 13u], hz_vertices[hz_q + 14u]));\n \ + uvec3 hz_w = uvec3(hz_vertices[hz_q + 3u], hz_vertices[hz_q + 9u], hz_vertices[hz_q + 15u]);\n \ + vec2 hz_t0 = vec2(hz_w.x & 65535u, hz_w.x >> 16u);\n \ + vec2 hz_d1 = vec2(hz_w.y & 65535u, hz_w.y >> 16u) - hz_t0;\n \ + vec2 hz_d2 = vec2(hz_w.z & 65535u, hz_w.z >> 16u) - hz_t0;\n \ + vec3 hz_e1 = hz_p1 - hz_p0;\n \ + vec3 hz_e2 = hz_p2 - hz_p0;\n \ + vec3 hz_n = cross(hz_e1, hz_e2);\n \ + if (dot(hz_n, hz_n) > 1e-12) {\n \ + hz_Normal = normalize(hz_n);\n \ + float hz_r = hz_d1.x * hz_d2.y - hz_d2.x * hz_d1.y;\n \ + vec3 hz_t = (hz_e1 * hz_d2.y - hz_e2 * hz_d1.y) * sign(hz_r);\n \ + vec3 hz_b = (hz_e2 * hz_d1.x - hz_e1 * hz_d2.x) * sign(hz_r);\n \ + if (dot(hz_t, hz_t) > 1e-12) {\n \ + hz_t = normalize(hz_t - hz_Normal * dot(hz_Normal, hz_t));\n \ + hz_tangent = vec4(hz_t, dot(cross(hz_t, hz_Normal), hz_b) < 0.0 ? -1.0 : 1.0);\n \ + }\n \ + }\n"; + impl PackAtlas { /// The vertex inputs of a gbuffers program drawing terrain (`TerrainVertex`, terrain.wgsl's /// `vs_terrain` or, for `flat` draws, `vs_terrain_flat`), in atlas coordinates. @@ -127,9 +151,11 @@ impl PackAtlas { "layout(set = 1, binding = 0) readonly buffer HzBlockSlots { uint hz_block_slots[]; };\n\ layout(set = 1, binding = 1) readonly buffer HzSlotOrigins { vec4 hz_slot_origins[]; };\n", ); - // `mc_Entity` of each sprite layer: id, then 1 for fluids or -1 (the game's table). + // `mc_Entity` of each sprite layer: id, then 1 for fluids or -1 (the game's table), + // and the arena page's vertices (`TerrainVertex`, six words each). declarations.push_str( - "layout(set = 3, binding = 0) readonly buffer HzBlockIds { int hz_block_ids[]; };\n", + "layout(set = 3, binding = 0) readonly buffer HzBlockIds { int hz_block_ids[]; };\n\ + layout(set = 3, binding = 1) readonly buffer HzVertices { uint hz_vertices[]; };\n", ); "hz_slot_origins[hz_block_slots[uint(gl_VertexIndex) >> 8u]]" } else { @@ -153,10 +179,15 @@ impl PackAtlas { hz_MultiTexCoord1 = vec4(float(hz_v_light.y), float(hz_v_light.x), 0.0, 1.0);\n \ hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \ hz_Color = hz_v_color;\n \ - hz_Normal = vec3(0.0, 1.0, 0.0);\n", + hz_Normal = vec3(0.0, 1.0, 0.0);\n \ + hz_tangent = vec4(1.0, 0.0, 0.0, 1.0);\n", scale[0], scale[1], ); - declarations.push_str("vec2 hz_cell; vec2 hz_scale;\n"); + let mut body = body; + if flat { + body.push_str(FACE); + } + declarations.push_str("vec2 hz_cell; vec2 hz_scale; vec4 hz_tangent;\n"); let entity = if flat { "vec4(float(hz_block_ids[2u * hz_layer]), float(hz_block_ids[2u * hz_layer + 1u]), 0.0, 0.0)" } else { @@ -168,7 +199,7 @@ impl PackAtlas { "mc_midTexCoord", "vec4((hz_cell + 0.5) * hz_scale, 0.0, 1.0)", ), - ("at_tangent", "vec4(1.0, 0.0, 0.0, 1.0)"), + ("at_tangent", "hz_tangent"), ("vaPosition", "hz_Vertex.xyz"), ("vaUV0", "hz_MultiTexCoord0.xy"), ("vaUV2", "hz_MultiTexCoord1.xy"), diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index ba8fe62e7..972ac82f3 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -211,8 +211,10 @@ pub struct PackPost { down: HashMap, /// The per-draw uniforms of gbuffers programs. draws: DrawUniforms, - /// `mc_Entity`'s table of flat terrain, and its bind group. - block_ids: Option<(wgpu::BindGroupLayout, wgpu::BindGroup)>, + /// Set 3 of flat terrain (`mc_Entity`'s table) and its bind group for each arena page, + /// with the page buffer it was made for. + block_ids: Option<(wgpu::BindGroupLayout, wgpu::Buffer)>, + page_groups: HashMap, /// `HORIZON_SHADER_PACK_TIMING`: GPU time of each pass, in the log. timing: Option, /// `shaders.properties`' custom uniforms, after the frame's in `uniforms.values`. @@ -498,8 +500,14 @@ fn attach( }) } -/// `mc_Entity`'s table as a storage buffer (vertex stage) at set 3; -1 pairs when empty. -fn block_ids(device: &wgpu::Device, table: &[i32]) -> (wgpu::BindGroupLayout, wgpu::BindGroup) { +/// Draws the game's terrain (`true`: translucent) into a pass; calls its third argument with +/// each arena page before drawing from it. +pub(super) type TerrainDraw<'a> = + dyn FnMut(&mut wgpu::RenderPass<'_>, bool, &dyn Fn(&mut wgpu::RenderPass<'_>, u32)) + 'a; + +/// Set 3 of flat terrain, read by the vertex stage: `mc_Entity`'s table (-1 pairs when empty) +/// and, per arena page, the page's vertices (a vertex reads its quad's corners for its normal). +fn block_ids(device: &wgpu::Device, table: &[i32]) -> (wgpu::BindGroupLayout, wgpu::Buffer) { let empty = [-1, -1]; let table = if table.is_empty() { &empty[..] } else { table }; let buffer = wgpu::util::DeviceExt::create_buffer_init( @@ -510,28 +518,21 @@ fn block_ids(device: &wgpu::Device, table: &[i32]) -> (wgpu::BindGroupLayout, wg usage: wgpu::BufferUsages::STORAGE, }, ); + let storage = |binding| wgpu::BindGroupLayoutEntry { + binding, + visibility: wgpu::ShaderStages::VERTEX, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Storage { read_only: true }, + has_dynamic_offset: false, + min_binding_size: None, + }, + count: None, + }; let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - label: Some("Shader pack block ids"), - entries: &[wgpu::BindGroupLayoutEntry { - binding: 0, - visibility: wgpu::ShaderStages::VERTEX, - ty: wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Storage { read_only: true }, - has_dynamic_offset: false, - min_binding_size: None, - }, - count: None, - }], + label: Some("Shader pack terrain"), + entries: &[storage(0), storage(1)], }); - let group = device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("Shader pack block ids"), - layout: &layout, - entries: &[wgpu::BindGroupEntry { - binding: 0, - resource: buffer.as_entire_binding(), - }], - }); - (layout, group) + (layout, buffer) } /// The game's geometry a gbuffers program draws: its vertex buffers and, for flat draws @@ -545,6 +546,8 @@ pub(super) struct Geometry<'a> { pub atlas: Option, /// `mc_Entity`'s table at set 3 (flat terrain). pub ids: Option<&'a wgpu::BindGroupLayout>, + /// Back faces are culled, as for the game's own terrain. + pub cull: bool, } /// The pass of program `p`: a quad, or `geometry` drawn by the game. `None` (with a note) when @@ -728,7 +731,12 @@ fn build_pass( compilation_options: Default::default(), buffers: geometry.map_or(&[], |g| g.buffers), }, - primitive: Default::default(), + // Rows flipped to GL's turn front faces clockwise. + primitive: wgpu::PrimitiveState { + front_face: wgpu::FrontFace::Cw, + cull_mode: geometry.filter(|g| g.cull).map(|_| wgpu::Face::Back), + ..Default::default() + }, depth_stencil: geometry.map(|g| wgpu::DepthStencilState { format: wgpu::TextureFormat::Depth32Float, depth_write_enabled: Some(g.depth_write), @@ -854,6 +862,7 @@ impl PackPost { depth_write: g.depth_write, atlas: g.atlas.clone(), ids: block_ids.as_ref().map(|i| &i.0), + cull: g.cull, }); let terrain = post .terrain @@ -871,6 +880,7 @@ impl PackPost { depth_write: false, atlas: None, ids: None, + cull: false, }; let basic = post.sky.and_then(|p| build(p, Some(&basic))); let textured = Geometry { @@ -879,6 +889,7 @@ impl PackPost { depth_write: false, atlas: world.celestial.clone(), ids: None, + cull: false, }; let textured = post.sky_textured.and_then(|p| build(p, Some(&textured))); Sky { @@ -1043,6 +1054,7 @@ impl PackPost { down, draws, block_ids, + page_groups: HashMap::new(), timing: super::pack_timing::PassTiming::new(device, queue), custom, atlas: world.atlas.as_ref().map(|a| a.0.clone()), @@ -1253,7 +1265,7 @@ impl PackPost { encoder: &mut wgpu::CommandEncoder, size: [u32; 2], translucent: bool, - draw: &mut dyn FnMut(&mut wgpu::RenderPass<'_>, bool), + draw: &mut TerrainDraw<'_>, ) { let slot = if translucent { &mut self.water @@ -1278,11 +1290,6 @@ impl PackPost { let mut rp = self.begin(encoder, &pass); rp.set_pipeline(&pass.pipeline); rp.set_bind_group(0, &group, &[]); - if pass.ids - && let Some((_, ids)) = &self.block_ids - { - rp.set_bind_group(3, ids, &[]); - } if pass.draw { let stage = if translucent { "TERRAIN_TRANSLUCENT" @@ -1291,7 +1298,13 @@ impl PackPost { }; self.draws.bind(&mut rp, stage); } - draw(&mut rp, translucent); + let (groups, ids) = (&self.page_groups, pass.ids); + let page = |rp: &mut wgpu::RenderPass<'_>, page: u32| { + if let Some((_, group)) = groups.get(&page).filter(|_| ids) { + rp.set_bind_group(3, group, &[]); + } + }; + draw(&mut rp, translucent, &page); } self.mips_valid &= !pass.written; if translucent { @@ -1362,7 +1375,7 @@ impl PackPost { encoder: &mut wgpu::CommandEncoder, size: [u32; 2], frame: &super::Frame, - terrain: &mut dyn FnMut(&mut wgpu::RenderPass<'_>, bool), + terrain: &mut TerrainDraw<'_>, ) { if super::sky_visible(frame) { self.draw_sky(device, queue, encoder, size, frame); @@ -1533,9 +1546,34 @@ impl PackPost { inputs: &PackInputs, size: [u32; 2], window: [u32; 2], - terrain: &mut dyn FnMut(&mut wgpu::RenderPass<'_>, bool), + pages: &[(u32, wgpu::Buffer)], + terrain: &mut TerrainDraw<'_>, ) { self.resize(device, size); + if let Some((layout, ids)) = &self.block_ids { + // A page that grew has a new buffer. + self.page_groups + .retain(|p, (b, _)| pages.iter().any(|(q, c)| q == p && c == b)); + for (page, buffer) in pages { + self.page_groups.entry(*page).or_insert_with(|| { + let group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Shader pack terrain"), + layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: ids.as_entire_binding(), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: buffer.as_entire_binding(), + }, + ], + }); + (buffer.clone(), group) + }); + } + } if let Some(t) = &mut self.timing { t.begin(device, queue); }