From b22583d0577f83652e275b4560de483ab42594d8 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 21:42:48 +0200 Subject: [PATCH 01/97] horizon-shaderpack: open Java shader packs and preprocess them like Iris A new crate for the OptiFine/Iris translation (docs/shader-packs.md, order step 1, first half): open a pack folder or zip (shaders/ found as Iris finds it), list its programs per dimension folder, build the #include graph, discover options per include component (OptionAnnotatedSource rules: boolean defines referenced by #ifdef, value defines and configurable consts with a [...] list, conflicting defaults dropped), write the player's values into the lines, and expand Iris's standard macros with pp-rs, keeping line numbers so errors name file:line. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 17 + Cargo.toml | 2 +- crates/horizon-shaderpack/Cargo.toml | 18 + crates/horizon-shaderpack/src/include.rs | 198 ++++++ crates/horizon-shaderpack/src/lib.rs | 122 ++++ crates/horizon-shaderpack/src/options.rs | 659 ++++++++++++++++++ crates/horizon-shaderpack/src/preprocess.rs | 332 +++++++++ crates/horizon-shaderpack/src/source.rs | 141 ++++ .../tests/fixtures/mini/shaders/composite.fsh | 10 + .../tests/fixtures/mini/shaders/composite.vsh | 3 + .../fixtures/mini/shaders/lib/common.glsl | 7 + .../fixtures/mini/shaders/lib/settings.glsl | 7 + .../tests/fixtures/mini/shaders/notes.txt | 1 + .../mini/shaders/world-1/composite.fsh | 4 + crates/horizon-shaderpack/tests/pack.rs | 223 ++++++ docs/shader-packs.md | 1 + 16 files changed, 1744 insertions(+), 1 deletion(-) create mode 100644 crates/horizon-shaderpack/Cargo.toml create mode 100644 crates/horizon-shaderpack/src/include.rs create mode 100644 crates/horizon-shaderpack/src/lib.rs create mode 100644 crates/horizon-shaderpack/src/options.rs create mode 100644 crates/horizon-shaderpack/src/preprocess.rs create mode 100644 crates/horizon-shaderpack/src/source.rs create mode 100644 crates/horizon-shaderpack/tests/fixtures/mini/shaders/composite.fsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/mini/shaders/composite.vsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/mini/shaders/lib/common.glsl create mode 100644 crates/horizon-shaderpack/tests/fixtures/mini/shaders/lib/settings.glsl create mode 100644 crates/horizon-shaderpack/tests/fixtures/mini/shaders/notes.txt create mode 100644 crates/horizon-shaderpack/tests/fixtures/mini/shaders/world-1/composite.fsh create mode 100644 crates/horizon-shaderpack/tests/pack.rs diff --git a/Cargo.lock b/Cargo.lock index d6bbf30d9..536ba6a55 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1364,6 +1364,14 @@ dependencies = [ "serde_json", ] +[[package]] +name = "horizon-shaderpack" +version = "0.1.0" +dependencies = [ + "pp-rs", + "zip", +] + [[package]] name = "horizon-telemetry" version = "0.1.0" @@ -2542,6 +2550,15 @@ dependencies = [ "zerovec", ] +[[package]] +name = "pp-rs" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb458bb7f6e250e6eb79d5026badc10a3ebb8f9a15d1fff0f13d17c71f4d6dee" +dependencies = [ + "unicode-xid", +] + [[package]] name = "presser" version = "0.3.1" diff --git a/Cargo.toml b/Cargo.toml index 19fbabc55..49ef5785f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] exclude = ["launcher", "crates/horizon-telemetry-server"] -members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-world", "crates/voxel-kernels", "crates/horizon-client", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/cargo-horizon-mod", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"] +members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-world", "crates/voxel-kernels", "crates/horizon-client", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-shaderpack", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/cargo-horizon-mod", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"] resolver = "2" [workspace.package] diff --git a/crates/horizon-shaderpack/Cargo.toml b/crates/horizon-shaderpack/Cargo.toml new file mode 100644 index 000000000..da9ae1a12 --- /dev/null +++ b/crates/horizon-shaderpack/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "horizon-shaderpack" +version = "0.1.0" +edition.workspace = true +license.workspace = true +rust-version.workspace = true +description = "Java (OptiFine/Iris) shader packs for Horizon: Iris's preprocessor and the GLSL to WGSL translation" + +[dependencies] +# The C preprocessor naga's GLSL front end uses. +pp-rs = "0.2.1" +zip = { version = "2", default-features = false, features = ["deflate"] } + +[lints] +workspace = true + +[dev-dependencies] +zip = { version = "2", default-features = false, features = ["deflate"] } diff --git a/crates/horizon-shaderpack/src/include.rs b/crates/horizon-shaderpack/src/include.rs new file mode 100644 index 000000000..34455a88e --- /dev/null +++ b/crates/horizon-shaderpack/src/include.rs @@ -0,0 +1,198 @@ +//! `#include` the way Iris resolves it (`IncludeGraph`, `IncludeProcessor`): +//! before any other directive, so include guards don't apply; `/lib/x.glsl` +//! from `shaders/`, `x.glsl` from the including file's folder. Every file +//! reachable from the programs is read once into a graph, and a program is +//! the graph inlined from its file. + +use crate::source::PackFiles; +use std::collections::{BTreeMap, BTreeSet}; + +/// One file of the graph: its lines and the include on each `#include` line. +pub struct FileNode { + pub lines: Vec, + /// Line index → included file (a normalised path relative to `shaders/`). + pub includes: BTreeMap, +} + +pub struct IncludeGraph { + pub files: BTreeMap, + /// Files that couldn't be read or include themselves, with the reason. + pub failures: BTreeMap, +} + +/// A program after inlining: the source and, for each of its lines, the +/// file and 1-based line it came from (to report errors where they are). +pub struct Inlined { + pub text: String, + pub origin: Vec<(String, u32)>, +} + +impl Inlined { + /// `file:line` of 1-based `line` of the inlined text. + pub fn locate(&self, line: u32) -> String { + match self.origin.get(line.saturating_sub(1) as usize) { + Some((file, at)) => format!("{file}:{at}"), + None => format!("line {line}"), + } + } +} + +/// `target` from an `#include` in `from`, as a path relative to `shaders/` +/// with `.` and `..` resolved (Iris's `AbsolutePackPath`). +pub fn resolve(from: &str, target: &str) -> String { + let joined = match target.strip_prefix('/') { + Some(absolute) => absolute.to_owned(), + None => match from.rsplit_once('/') { + Some((dir, _)) => format!("{dir}/{target}"), + None => target.to_owned(), + }, + }; + let mut parts: Vec<&str> = Vec::new(); + for segment in joined.split('/') { + match segment { + "" | "." => {} + ".." => { + parts.pop(); + } + s => parts.push(s), + } + } + parts.join("/") +} + +/// The target of an `#include` line, or `None` for any other line. +fn include_target(line: &str) -> Option<&str> { + let t = line.trim(); + if !t.starts_with("#include") { + return None; + } + let target = t.get("#include ".len()..).unwrap_or("").trim(); + let target = target.strip_prefix('"').unwrap_or(target); + Some(target.strip_suffix('"').unwrap_or(target)) +} + +impl IncludeGraph { + /// Read `starts` and every file they include, transitively. + pub fn build(files: &PackFiles, starts: &[String]) -> Self { + let mut graph = Self { + files: BTreeMap::new(), + failures: BTreeMap::new(), + }; + let mut seen: BTreeSet = starts.iter().cloned().collect(); + let mut queue: Vec<(String, Option<(String, usize)>)> = + starts.iter().map(|s| (s.clone(), None)).collect(); + while let Some((path, from)) = queue.pop() { + let Some(text) = files.read(&path) else { + let why = match from { + Some((file, line)) => { + format!("{file}:{}: #include \"{path}\": file not found", line + 1) + } + None => format!("{path}: file not found"), + }; + graph.failures.insert(path, why); + continue; + }; + let lines: Vec = text.lines().map(str::to_owned).collect(); + let includes: BTreeMap = lines + .iter() + .enumerate() + .filter_map(|(i, l)| Some((i, resolve(&path, include_target(l)?)))) + .collect(); + if let Some((line, _)) = includes.iter().find(|(_, target)| **target == path) { + graph.failures.insert( + path.clone(), + format!("{path}:{}: file includes itself", line + 1), + ); + continue; + } + for (&line, target) in &includes { + if seen.insert(target.clone()) { + queue.push((target.clone(), Some((path.clone(), line)))); + } + } + graph.files.insert(path, FileNode { lines, includes }); + } + graph + } + + /// The graph's weakly connected components (files linked by includes in + /// either direction), each sorted. Iris discovers options per component. + pub fn components(&self) -> Vec> { + let names: Vec<&String> = self.files.keys().collect(); + let index: BTreeMap<&str, usize> = names + .iter() + .enumerate() + .map(|(i, n)| (n.as_str(), i)) + .collect(); + let mut parent: Vec = (0..names.len()).collect(); + fn root(parent: &mut [usize], mut i: usize) -> usize { + while parent[i] != i { + parent[i] = parent[parent[i]]; + i = parent[i]; + } + i + } + for (i, node) in self.files.values().enumerate() { + for target in node.includes.values() { + if let Some(&j) = index.get(target.as_str()) { + let (a, b) = (root(&mut parent, i), root(&mut parent, j)); + parent[a] = b; + } + } + } + let mut groups: BTreeMap> = BTreeMap::new(); + for (i, name) in names.iter().enumerate() { + let r = root(&mut parent, i); + groups.entry(r).or_default().push((*name).clone()); + } + groups.into_values().collect() + } + + /// Inline `start`, passing every line that isn't an `#include` through + /// `edit(file, line index, text)` first (Iris applies option values there). + pub fn inline( + &self, + start: &str, + edit: &dyn Fn(&str, usize, &str) -> String, + ) -> Result { + let mut out = Inlined { + text: String::new(), + origin: Vec::new(), + }; + let mut stack = Vec::new(); + self.inline_into(start, edit, &mut stack, &mut out)?; + Ok(out) + } + + fn inline_into( + &self, + path: &str, + edit: &dyn Fn(&str, usize, &str) -> String, + stack: &mut Vec, + out: &mut Inlined, + ) -> Result<(), String> { + if let Some(why) = self.failures.get(path) { + return Err(why.clone()); + } + if stack.iter().any(|p| p == path) { + return Err(format!("#include cycle: {} -> {path}", stack.join(" -> "))); + } + let node = self + .files + .get(path) + .ok_or_else(|| format!("{path}: file not found"))?; + stack.push(path.to_owned()); + for (i, line) in node.lines.iter().enumerate() { + match node.includes.get(&i) { + Some(target) => self.inline_into(target, edit, stack, out)?, + None => { + out.text.push_str(&edit(path, i, line)); + out.text.push('\n'); + out.origin.push((path.to_owned(), i as u32 + 1)); + } + } + } + stack.pop(); + Ok(()) + } +} diff --git a/crates/horizon-shaderpack/src/lib.rs b/crates/horizon-shaderpack/src/lib.rs new file mode 100644 index 000000000..36bfaf37d --- /dev/null +++ b/crates/horizon-shaderpack/src/lib.rs @@ -0,0 +1,122 @@ +//! Java shader packs (OptiFine/Iris) for Horizon (docs/shader-packs.md, +//! "Java shader packs"). This crate does the part that needs no GPU: open a +//! pack, find its programs and options, and turn a program into plain GLSL +//! the way Iris does before compiling it (`#include`s inlined, the player's +//! option values written into the source, Iris's macros expanded). + +pub mod include; +pub mod options; +pub mod preprocess; +pub mod source; + +use include::{IncludeGraph, Inlined}; +use options::{Annotated, OptionSet, OptionValues}; +use preprocess::{Environment, Preprocessed}; +use source::PackFiles; +use std::{ + collections::{BTreeMap, BTreeSet}, + path::Path, +}; + +/// A program's pipeline stage, from its file extension. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Stage { + Vertex, + Fragment, + Geometry, + Compute, + TessControl, + TessEval, +} + +impl Stage { + /// The stage of `composite1.fsh` and the like; `None` for other files. + pub fn of(program: &str) -> Option { + Some(match program.rsplit_once('.')?.1 { + "vsh" => Self::Vertex, + "fsh" => Self::Fragment, + "gsh" => Self::Geometry, + "csh" => Self::Compute, + "tcs" => Self::TessControl, + "tes" => Self::TessEval, + _ => return None, + }) + } +} + +/// An opened Java shader pack. +pub struct JavaPack { + pub files: PackFiles, + /// Every program source file (`composite1.fsh`, `world-1/final.vsh`). + pub programs: Vec, + pub graph: IncludeGraph, + annotations: BTreeMap, + /// The options the player can set (`ShaderPackOptions`). + pub options: OptionSet, +} + +impl JavaPack { + /// Open the pack folder or zip at `path` and read every program and file + /// they include. + pub fn open(path: &Path) -> Result { + let files = PackFiles::open(path)?; + let programs = files.programs(); + if programs.is_empty() { + return Err("no programs in shaders/".into()); + } + let graph = IncludeGraph::build(&files, &programs); + let annotations: BTreeMap = graph + .files + .iter() + .map(|(path, node)| (path.clone(), Annotated::parse(&node.lines))) + .collect(); + // A boolean #define is an option when an #ifdef in its component + // refers to it. + let mut options = OptionSet::default(); + for component in graph.components() { + let refs: BTreeSet<&String> = component + .iter() + .flat_map(|f| &annotations[f].ifdef_refs) + .collect(); + for file in &component { + let a = &annotations[file]; + a.bools + .values() + .filter(|o| refs.contains(&o.name)) + .for_each(|o| options.add_bool(o)); + a.strings.values().for_each(|o| options.add_string(o)); + } + } + Ok(Self { + files, + programs, + graph, + annotations, + options, + }) + } + + /// The player's values from saved `name=value` pairs (`shaderpacks/.txt`). + pub fn values(&self, saved: &BTreeMap) -> OptionValues { + OptionValues::new(&self.options, saved) + } + + /// `program` with its includes inlined and `values` applied. + pub fn source(&self, program: &str, values: &OptionValues) -> Result { + self.graph + .inline(program, &|file, i, line| match self.annotations.get(file) { + Some(a) => a.edit(values, i, line), + None => line.to_owned(), + }) + } + + /// `program` as plain GLSL: [`JavaPack::source`], then the preprocessor. + pub fn preprocess( + &self, + program: &str, + values: &OptionValues, + env: &Environment, + ) -> Result { + preprocess::preprocess(&self.source(program, values)?, env) + } +} diff --git a/crates/horizon-shaderpack/src/options.rs b/crates/horizon-shaderpack/src/options.rs new file mode 100644 index 000000000..d25a3d776 --- /dev/null +++ b/crates/horizon-shaderpack/src/options.rs @@ -0,0 +1,659 @@ +//! A pack's options, found in its source the way Iris finds them +//! (`OptionAnnotatedSource`, `ParsedString`, `OptionSet`), and the player's +//! values written back into the lines they came from. +//! +//! - `#define NAME` / `//#define NAME` (optionally `// comment`): a boolean, +//! on unless commented out, and an option only if some `#ifdef NAME` / +//! `#ifndef NAME` in the same include component refers to it. +//! - `#define NAME VALUE // comment [A B C]`: a value with its allowed list. +//! - `const int|float|bool NAME = VALUE; // [A B C]`: only for the constants +//! Iris lets players configure ([`CONST_OPTIONS`]). + +use std::collections::{BTreeMap, BTreeSet}; + +/// The `const` names Iris configures (`VALID_CONST_OPTION_NAMES`), apart from +/// the per-buffer ones [`is_const_option`] adds. +pub const CONST_OPTIONS: &[&str] = &[ + "shadowMapResolution", + "shadowDistance", + "voxelDistance", + "shadowDistanceRenderMul", + "entityShadowDistanceMul", + "shadowIntervalSize", + "generateShadowMipmap", + "generateShadowColorMipmap", + "shadowHardwareFiltering", + "shadowtex0Mipmap", + "shadowtexMipmap", + "shadowtex1Mipmap", + "shadowtex0Nearest", + "shadowtexNearest", + "shadow0MinMagNearest", + "shadowtex1Nearest", + "shadow1MinMagNearest", + "wetnessHalflife", + "drynessHalflife", + "eyeBrightnessHalflife", + "centerDepthHalflife", + "sunPathRotation", + "ambientOcclusionLevel", + "superSamplingLevel", + "noiseTextureResolution", +]; + +/// Iris's shadow colour buffer count (`MAX_SHADOW_COLOR_BUFFERS_IRIS`). +const SHADOW_COLOR_BUFFERS: u32 = 8; + +pub fn is_const_option(name: &str) -> bool { + CONST_OPTIONS.contains(&name) + || (0..SHADOW_COLOR_BUFFERS).any(|i| { + [ + format!("shadowcolor{i}Mipmap"), + format!("shadowColor{i}Mipmap"), + format!("shadowcolor{i}Nearest"), + format!("shadowColor{i}Nearest"), + format!("shadowcolor{i}MinMagNearest"), + format!("shadowColor{i}MinMagNearest"), + format!("shadowHardwareFiltering{i}"), + ] + .iter() + .any(|n| n == name) + }) +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum OptionKind { + Define, + Const, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct BoolOption { + pub kind: OptionKind, + pub name: String, + pub comment: Option, + pub default: bool, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct StringOption { + pub kind: OptionKind, + pub name: String, + pub comment: Option, + pub default: String, + /// The bracketed list, with the default added at the end when missing. + pub allowed: Vec, +} + +impl StringOption { + /// `StringOption.create`: `None` without a `[...]` list in the comment. + fn create(kind: OptionKind, name: &str, comment: Option<&str>, default: &str) -> Option { + let comment = comment?; + let open = comment.find('[')?; + let close = open + comment[open..].find(']')?; + // Java's split(" "): empty strings between doubled spaces stay, + // trailing ones go. + let mut allowed: Vec = comment[open + 1..close] + .split(' ') + .map(str::to_owned) + .collect(); + while allowed.last().is_some_and(String::is_empty) { + allowed.pop(); + } + if !allowed.iter().any(|v| v == default) { + allowed.push(default.to_owned()); + } + let rest = format!("{}{}", &comment[..open], &comment[close + 1..]); + Some(Self { + kind, + name: name.to_owned(), + comment: non_empty(&rest), + default: default.to_owned(), + allowed, + }) + } +} + +/// An option's comment; empty is none (`BaseOption`). +fn non_empty(comment: &str) -> Option { + let c = comment.trim(); + (!c.is_empty()).then(|| c.to_owned()) +} + +/// Iris's `ParsedString`: a cursor over a trimmed line. +struct Cursor(Vec); + +impl Cursor { + fn new(s: &str) -> Self { + Self(s.chars().collect()) + } + fn literal(&mut self, token: &str) -> bool { + let t: Vec = token.chars().collect(); + if self.0.starts_with(&t) { + self.0.drain(..t.len()); + true + } else { + false + } + } + /// Whitespace first, then trims both ends (as Iris does). + fn whitespace(&mut self) -> bool { + if !self.0.first().is_some_and(|c| c.is_whitespace()) { + return false; + } + let s: String = self.0.iter().collect(); + self.0 = s.trim().chars().collect(); + true + } + /// `//` and any further slashes. + fn comments(&mut self) -> bool { + if !self.literal("//") { + return false; + } + while self.literal("/") {} + true + } + fn contains(&self, s: &str) -> bool { + self.rest().contains(s) + } + fn is_end(&self) -> bool { + self.0.is_empty() + } + fn rest(&self) -> String { + self.0.iter().collect() + } + fn take(&mut self, n: usize) -> String { + self.0.drain(..n).collect() + } + fn word(&mut self) -> Option { + let n = self + .0 + .iter() + .take_while(|c| c.is_alphanumeric() || **c == '_') + .count(); + (n > 0).then(|| self.take(n)) + } + /// Iris's `takeNumber`: digits and any character next to a digit, an `f` + /// suffix, and the whole must parse as a Java float. + fn number(&mut self) -> Option { + let c = &self.0; + let mut n = 0; + while n < c.len() { + if n + 1 < c.len() { + if !c[n].is_ascii_digit() && !c[n + 1].is_ascii_digit() { + break; + } + } else if !c[n].is_ascii_digit() { + break; + } + n += 1; + } + if n > 0 && n + 1 < c.len() && (c[n] == 'f' || c[n] == 'F') { + n += 1; + } + let text: String = c[..n].iter().collect(); + java_float(&text).then(|| self.take(n)) + } + fn word_or_number(&mut self) -> Option { + self.number().or_else(|| self.word()) + } +} + +/// Whether Java's `Float.parseFloat` accepts `s` (decimal forms; a trailing +/// `f`/`d` is allowed). +fn java_float(s: &str) -> bool { + let s = s.trim(); + let s = s.strip_suffix(['f', 'F', 'd', 'D']).unwrap_or(s); + !s.is_empty() && s.parse::().is_ok() +} + +/// The options and `#ifdef` references of one file, by line index. +#[derive(Default, Debug)] +pub struct Annotated { + pub bools: BTreeMap, + pub strings: BTreeMap, + /// Names tested by a bare `#ifdef NAME` / `#ifndef NAME` line. + pub ifdef_refs: BTreeSet, + /// Why a line that looks like an option isn't one. + pub diagnostics: BTreeMap, +} + +impl Annotated { + pub fn parse(lines: &[String]) -> Self { + let mut out = Self::default(); + for (i, line) in lines.iter().enumerate() { + if !["#define", "const", "#ifdef", "#ifndef"] + .iter() + .any(|k| line.contains(k)) + { + continue; + } + let mut c = Cursor::new(line.trim()); + if c.literal("#ifdef") || c.literal("#ifndef") { + if !c.whitespace() { + continue; + } + let name = c.word(); + c.whitespace(); + if let (Some(name), true) = (name, c.is_end()) { + out.ifdef_refs.insert(name); + } + } else if c.literal("const") { + if let Err(why) = out.parse_const(i, &mut c) { + out.diagnostics.insert(i, why.into()); + } + } else if c.contains("#define") { + if let Err(why) = out.parse_define(i, &mut c) { + out.diagnostics.insert(i, why.into()); + } + } + } + out + } + + fn parse_const(&mut self, i: usize, c: &mut Cursor) -> Result<(), &'static str> { + if !c.whitespace() { + return Err("expected whitespace after const"); + } + let is_bool = if c.literal("int") || c.literal("float") { + false + } else if c.literal("bool") { + true + } else { + return Err("only int, float and bool consts are options"); + }; + if !c.whitespace() { + return Err("expected whitespace after the type"); + } + let name = c.word().ok_or("expected a name after the type")?; + c.whitespace(); + if !c.literal("=") { + return Err("expected = after the name"); + } + c.whitespace(); + let value = c + .word_or_number() + .ok_or("expected a word or number after =")?; + c.whitespace(); + if !c.literal(";") { + return Err("expected ; after the value"); + } + c.whitespace(); + let comment = if c.comments() { + non_empty(&c.rest()) + } else if !c.is_end() { + return Err("only a comment may follow the ;"); + } else { + None + }; + if !is_const_option(&name) { + return Err("not one of the configurable consts"); + } + if is_bool { + let default = match value.as_str() { + "true" => true, + "false" => false, + _ => return Err("a bool const option must be true or false"), + }; + self.bools.insert( + i, + BoolOption { + kind: OptionKind::Const, + name, + comment, + default, + }, + ); + return Ok(()); + } + let option = StringOption::create(OptionKind::Const, &name, comment.as_deref(), &value) + .ok_or("a const option needs a [...] list of values in its comment")?; + self.strings.insert(i, option); + Ok(()) + } + + fn parse_define(&mut self, i: usize, c: &mut Cursor) -> Result<(), &'static str> { + let commented = c.comments(); + c.whitespace(); + if !c.literal("#define") { + return Err("#define isn't where an option's would be"); + } + if !c.whitespace() { + return Err("expected whitespace after #define"); + } + let name = c.word().ok_or("expected a name after #define")?; + let mut spaced = c.whitespace(); + if c.is_end() { + self.bools.insert( + i, + BoolOption { + kind: OptionKind::Define, + name, + comment: None, + default: !commented, + }, + ); + return Ok(()); + } + if c.comments() { + let comment = non_empty(&c.rest()); + self.bools.insert( + i, + BoolOption { + kind: OptionKind::Define, + name, + comment, + default: !commented, + }, + ); + return Ok(()); + } + if !spaced { + return Err("only letters, digits and _ may be in an option's name"); + } + if commented { + return Err("only boolean #define options may be commented out"); + } + let value = c + .word_or_number() + .ok_or("the value is neither a number nor a word")?; + spaced = c.whitespace(); + if c.is_end() { + return Err("a value #define needs a comment with a [...] list of values"); + } + if !c.comments() { + return Err(if spaced { + "only a comment may follow the value" + } else { + "unexpected characters after the value" + }); + } + let comment = c.rest().trim().to_owned(); + let option = StringOption::create(OptionKind::Define, &name, Some(&comment), &value) + .ok_or("a value #define needs a [...] list of values in its comment")?; + self.strings.insert(i, option); + Ok(()) + } + + /// Line `i` (text `line`) with the player's values applied + /// (`OptionAnnotatedSource.edit`). + pub fn edit(&self, values: &OptionValues, i: usize, line: &str) -> String { + if let Some(o) = self.bools.get(&i) { + let value = values.bools.get(&o.name).copied(); + return match o.kind { + OptionKind::Define => { + let on = value.unwrap_or(o.default); + let commented = line.trim().starts_with("//"); + if commented && on { + let mut c = Cursor::new(line); + c.whitespace(); + c.comments(); + c.rest() + } else if !on { + format!("//{line}") + } else { + line.to_owned() + } + } + OptionKind::Const => match value { + Some(v) => edit_const(line, &o.default.to_string(), &v.to_string()), + None => line.to_owned(), + }, + }; + } + if let Some(o) = self.strings.get(&i) { + if let Some(v) = values.strings.get(&o.name) { + return match o.kind { + OptionKind::Define => format!( + "#define {} {v} // OptionAnnotatedSource: Changed option", + o.name + ), + OptionKind::Const => edit_const(line, &o.default, v), + }; + } + } + line.to_owned() + } +} + +/// The first `current` after the `=` replaced by `new`. +fn edit_const(line: &str, current: &str, new: &str) -> String { + match line.find('=') { + Some(eq) => format!("{}{}", &line[..eq], line[eq..].replacen(current, new, 1)), + None => line.to_owned(), + } +} + +/// Every option of a pack by name. Two definitions with different defaults +/// make the name ambiguous: Iris drops it. +#[derive(Default, Debug)] +pub struct OptionSet { + pub bools: BTreeMap, + pub strings: BTreeMap, +} + +impl OptionSet { + pub fn add_bool(&mut self, o: &BoolOption) { + match self.bools.get_mut(&o.name) { + Some(existing) if existing.default != o.default => { + self.bools.remove(&o.name); + } + Some(existing) => { + if existing.comment.is_none() { + *existing = o.clone(); + } + } + None => { + self.bools.insert(o.name.clone(), o.clone()); + } + } + } + + pub fn add_string(&mut self, o: &StringOption) { + match self.strings.get_mut(&o.name) { + Some(existing) if existing.default != o.default => { + self.strings.remove(&o.name); + } + Some(existing) => { + if existing.comment.is_none() { + *existing = o.clone(); + } + } + None => { + self.strings.insert(o.name.clone(), o.clone()); + } + } + } +} + +/// The player's values that differ from the defaults (`MutableOptionValues`). +#[derive(Default, Debug, Clone, PartialEq)] +pub struct OptionValues { + pub bools: BTreeMap, + pub strings: BTreeMap, +} + +impl OptionValues { + /// From saved `name=value` pairs. A boolean other than `true`/`false` is + /// ignored; a value outside an option's allowed list is kept (profiles and + /// hand-edited files may set one, as in OptiFine). + pub fn new(options: &OptionSet, saved: &BTreeMap) -> Self { + let mut out = Self::default(); + for (name, o) in &options.bools { + let value = match saved.get(name).map(String::as_str) { + Some("true") => true, + Some("false") => false, + _ => continue, + }; + if value != o.default { + out.bools.insert(name.clone(), value); + } + } + for (name, o) in &options.strings { + if let Some(v) = saved.get(name) { + if *v != o.default { + out.strings.insert(name.clone(), v.clone()); + } + } + } + out + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn one(line: &str) -> Annotated { + Annotated::parse(&[line.to_owned()]) + } + + fn string(line: &str) -> Option<(String, String, Vec)> { + one(line) + .strings + .into_values() + .next() + .map(|o| (o.name, o.default, o.allowed)) + } + + #[test] + fn value_defines_need_an_allowed_list() { + // Iris's OptionDiscoverTest cases. + let s = |v: &[&str]| v.iter().map(|x| x.to_string()).collect::>(); + assert_eq!( + string("#define SECRET 0 //[0]"), + Some(("SECRET".into(), "0".into(), s(&["0"]))) + ); + assert_eq!( + string("#define SECRET 0//[0]"), + Some(("SECRET".into(), "0".into(), s(&["0"]))) + ); + assert_eq!( + string("#define OPT 1 //[0]"), + Some(("OPT".into(), "1".into(), s(&["0", "1"]))) + ); + for none in [ + "#define PI 3.14 // It's PI.", + "#define PI 3.14", + "const int noiseTextureResolution = 512;", + "const int noiseTextureResolution = 512; // Default noise texture size [", + ] { + let a = one(none); + assert!(a.strings.is_empty() && a.bools.is_empty(), "{none}"); + } + assert_eq!( + string(" #define SHADOW_QUALITY 2.0f // Shadows [1.0f 2.0f 3.0f]"), + Some(( + "SHADOW_QUALITY".into(), + "2.0f".into(), + s(&["1.0f", "2.0f", "3.0f"]) + )) + ); + assert_eq!( + string("#define SKY_STYLE SKY_REALISTIC // [SKY_REALISTIC SKY_STYLISED]") + .unwrap() + .1, + "SKY_REALISTIC" + ); + assert_eq!( + string("const int shadowMapResolution = 2048; // [1024 2048 4096]") + .unwrap() + .0, + "shadowMapResolution" + ); + assert!(string("const int myResolution = 2048; // [1024 2048 4096]").is_none()); + assert!(string("//#define SHADOW_QUALITY 2 // [1 2 3]").is_none()); + } + + #[test] + fn boolean_defines_and_references() { + let a = Annotated::parse(&[ + "#define BLOOM // Bloom".into(), + "//#define DOF".into(), + "// #define MOTION_BLUR // Blur".into(), + "#define FOG(x) x".into(), + "#ifdef BLOOM".into(), + "#ifndef DOF // trailing text: not a reference".into(), + "#if defined MOTION_BLUR".into(), + "const bool shadowHardwareFiltering = true; // Filtering".into(), + ]); + let b: Vec<(&str, bool, Option<&str>)> = a + .bools + .values() + .map(|o| (o.name.as_str(), o.default, o.comment.as_deref())) + .collect(); + assert_eq!( + b, + [ + ("BLOOM", true, Some("Bloom")), + ("DOF", false, None), + ("MOTION_BLUR", false, Some("Blur")), + ("shadowHardwareFiltering", true, Some("Filtering")) + ] + ); + assert_eq!(a.ifdef_refs.iter().collect::>(), ["BLOOM"]); + } + + #[test] + fn values_are_written_into_the_lines() { + let lines: Vec = [ + "#define BLOOM", + "//#define DOF", + "#define QUALITY 2 // [1 2 3]", + "const float sunPathRotation = -40.0; // [-40.0 0.0 40.0]", + "const bool shadowHardwareFiltering = false;", + ] + .map(String::from) + .into(); + let a = Annotated::parse(&lines); + let mut set = OptionSet::default(); + a.bools.values().for_each(|o| set.add_bool(o)); + a.strings.values().for_each(|o| set.add_string(o)); + let saved: BTreeMap = [ + ("BLOOM", "false"), + ("DOF", "true"), + ("QUALITY", "3"), + ("sunPathRotation", "0.0"), + ("shadowHardwareFiltering", "yes"), + ] + .map(|(k, v)| (k.to_owned(), v.to_owned())) + .into(); + let values = OptionValues::new(&set, &saved); + assert!( + !values.bools.contains_key("shadowHardwareFiltering"), + "not a boolean: ignored" + ); + let edited: Vec = lines + .iter() + .enumerate() + .map(|(i, l)| a.edit(&values, i, l)) + .collect(); + assert_eq!( + edited, + [ + "//#define BLOOM", + "#define DOF", + "#define QUALITY 3 // OptionAnnotatedSource: Changed option", + "const float sunPathRotation = 0.0; // [-40.0 0.0 40.0]", + "const bool shadowHardwareFiltering = false;", + ] + ); + } + + #[test] + fn conflicting_defaults_drop_the_option() { + let mut set = OptionSet::default(); + let a = Annotated::parse(&["#define Q 1 // [1 2]".into()]); + let b = Annotated::parse(&["#define Q 2 // Quality [1 2]".into()]); + let c = Annotated::parse(&["#define Q 1 // Quality [1 2]".into()]); + set.add_string(&a.strings[&0]); + set.add_string(&c.strings[&0]); + assert_eq!( + set.strings["Q"].comment.as_deref(), + Some("Quality"), + "the commented one wins" + ); + set.add_string(&b.strings[&0]); + assert!(set.strings.is_empty()); + } +} diff --git a/crates/horizon-shaderpack/src/preprocess.rs b/crates/horizon-shaderpack/src/preprocess.rs new file mode 100644 index 000000000..f5b8478a6 --- /dev/null +++ b/crates/horizon-shaderpack/src/preprocess.rs @@ -0,0 +1,332 @@ +//! The C preprocessor pass: Iris's standard macros (`StandardMacros`, +//! `IrisDefines`) defined, then every directive and macro expanded by pp-rs, +//! the preprocessor naga's GLSL front end uses. The output keeps the inlined +//! source's line numbers, so an error later on still names `file:line`. + +use crate::include::Inlined; +use pp_rs::{ + pp::Preprocessor, + token::{Punct, TokenValue}, +}; + +/// The version of Minecraft packs are told they run on (`MC_VERSION`, +/// Iris's `formatVersionString("26.3")`). +pub const MC_VERSION: &str = "260300"; +/// The Iris release whose behaviour the translation follows (`IRIS_VERSION`, 1.10.0). +pub const IRIS_VERSION: &str = "11000"; + +/// Iris's `WorldRenderingPhase`, in order: `MC_RENDER_STAGE_` is the index. +pub const RENDER_STAGES: &[&str] = &[ + "NONE", + "SKY", + "SUNSET", + "CUSTOM_SKY", + "SUN", + "MOON", + "STARS", + "VOID", + "TERRAIN_SOLID", + "TERRAIN_CUTOUT_MIPPED", + "TERRAIN_CUTOUT", + "ENTITIES", + "BLOCK_ENTITIES", + "DESTROY", + "OUTLINE", + "DEBUG", + "HAND_SOLID", + "TERRAIN_TRANSLUCENT", + "TRIPWIRE", + "PARTICLES", + "CLOUDS", + "RAIN_SNOW", + "WORLD_BORDER", + "HAND_TRANSLUCENT", +]; + +/// Iris's `BiomeCategories`, in order: `CAT_` is the index. +pub const BIOME_CATEGORIES: &[&str] = &[ + "NONE", + "TAIGA", + "EXTREME_HILLS", + "JUNGLE", + "MESA", + "PLAINS", + "SAVANNA", + "ICY", + "THE_END", + "BEACH", + "FOREST", + "OCEAN", + "DESERT", + "RIVER", + "SWAMP", + "MUSHROOM", + "NETHER", + "MOUNTAIN", + "UNDERGROUND", +]; + +/// Distant Horizons block kinds Iris always defines (`DH_BLOCK_`). +const DH_BLOCKS: &[&str] = &[ + "UNKNOWN", + "LEAVES", + "STONE", + "WOOD", + "METAL", + "DIRT", + "LAVA", + "DEEPSLATE", + "SNOW", + "SAND", + "TERRACOTTA", + "NETHER_STONE", + "WATER", + "GRASS", + "AIR", + "ILLUMINATED", +]; + +/// The macros every program is preprocessed with. +#[derive(Clone, Debug)] +pub struct Environment { + pub defines: Vec<(String, String)>, +} + +impl Environment { + /// What Iris defines (`IrisDefines.createIrisReplacements`) for a GPU + /// whose adapter reports `vendor` and `renderer`, without any GL + /// extension (`MC_GL_*`) or mod macro: packs then take their portable + /// paths. Biomes (`BIOME_*`) and the texture format come from the game: + /// add them with [`Environment::define`]. + pub fn iris(vendor: &str, renderer: &str, mipmap_levels: u32) -> Self { + let mut env = Self { + defines: Vec::new(), + }; + env.define("MC_VERSION", MC_VERSION); + env.define("MC_MIPMAP_LEVEL", &mipmap_levels.to_string()); + env.define("IRIS_VERSION", IRIS_VERSION); + // Horizon's renderer stands in for GL 4.6 / GLSL 4.60. + env.define("MC_GL_VERSION", "460"); + env.define("MC_GLSL_VERSION", "460"); + env.define(os_macro(), ""); + env.define(vendor_macro(vendor), ""); + env.define(renderer_macro(renderer), ""); + for flag in [ + "IS_IRIS", + "IRIS_REQUIRES_SEPARATE_ENTITY_DRAWS", + "IRIS_HAS_TRANSLUCENCY_SORTING", + ] { + env.define(flag, ""); + } + env.define("MAX_COLOR_BUFFERS", "32"); + env.define("IRIS_TAG_SUPPORT", "2"); + for (i, name) in DH_BLOCKS.iter().enumerate() { + env.define(&format!("DH_BLOCK_{name}"), &i.to_string()); + } + env.define("MC_NORMAL_MAP", ""); + env.define("MC_SPECULAR_MAP", ""); + env.define("MC_RENDER_QUALITY", "1.0"); + env.define("MC_SHADOW_QUALITY", "1.0"); + env.define("MC_HAND_DEPTH", "0.125"); + for (i, name) in RENDER_STAGES.iter().enumerate() { + env.define(&format!("MC_RENDER_STAGE_{name}"), &i.to_string()); + } + for (i, name) in BIOME_CATEGORIES.iter().enumerate() { + env.define(&format!("CAT_{name}"), &i.to_string()); + } + env.define("PPT_NONE", "0"); + env.define("PPT_RAIN", "1"); + env.define("PPT_SNOW", "2"); + env + } + + /// Define `name` (replacing an earlier value). + pub fn define(&mut self, name: &str, value: &str) { + self.defines.retain(|(n, _)| n != name); + self.defines.push((name.to_owned(), value.to_owned())); + } +} + +fn os_macro() -> &'static str { + if cfg!(target_os = "macos") { + "MC_OS_MAC" + } else if cfg!(windows) { + "MC_OS_WINDOWS" + } else { + "MC_OS_LINUX" + } +} + +/// `StandardMacros.getVendor`. +pub fn vendor_macro(vendor: &str) -> &'static str { + let v = vendor.to_lowercase(); + const TABLE: &[(&str, &str)] = &[ + ("ati", "MC_GL_VENDOR_ATI"), + ("intel", "MC_GL_VENDOR_INTEL"), + ("nvidia", "MC_GL_VENDOR_NVIDIA"), + ("amd", "MC_GL_VENDOR_AMD"), + ("x.org", "MC_GL_VENDOR_XORG"), + ]; + TABLE + .iter() + .find(|(prefix, _)| v.starts_with(prefix)) + .map_or("MC_GL_VENDOR_OTHER", |(_, m)| m) +} + +/// `StandardMacros.getRenderer`. +pub fn renderer_macro(renderer: &str) -> &'static str { + let r = renderer.to_lowercase(); + const TABLE: &[(&str, &str)] = &[ + ("amd", "MC_GL_RENDERER_RADEON"), + ("ati", "MC_GL_RENDERER_RADEON"), + ("radeon", "MC_GL_RENDERER_RADEON"), + ("gallium", "MC_GL_RENDERER_GALLIUM"), + ("intel", "MC_GL_RENDERER_INTEL"), + ("geforce", "MC_GL_RENDERER_GEFORCE"), + ("nvidia", "MC_GL_RENDERER_GEFORCE"), + ("quadro", "MC_GL_RENDERER_QUADRO"), + ("nvs", "MC_GL_RENDERER_QUADRO"), + ("mesa", "MC_GL_RENDERER_MESA"), + ("apple", "MC_GL_RENDERER_APPLE"), + ]; + TABLE + .iter() + .find(|(prefix, _)| r.starts_with(prefix)) + .map_or("MC_GL_RENDERER_OTHER", |(_, m)| m) +} + +/// A program after the preprocessor: plain GLSL with no directives. +#[derive(Debug)] +pub struct Preprocessed { + /// The source, one output line per inlined line (macro expansions joined). + pub text: String, + /// The `#version` line's words (`["330", "compatibility"]`), if any. + pub version: Vec, + /// `#extension` lines' words (`["GL_ARB_gpu_shader5", ":", "enable"]`). + pub extensions: Vec>, +} + +/// Expand `source` with `env`'s macros. Errors name the file and line. +pub fn preprocess(source: &Inlined, env: &Environment) -> Result { + let text = &source.text; + let line_starts: Vec = std::iter::once(0) + .chain(text.match_indices('\n').map(|(i, _)| i + 1)) + .collect(); + let line_of = |offset: u32| line_starts.partition_point(|&s| s <= offset as usize) as u32; + let mut pp = Preprocessor::new(text); + for (name, value) in &env.defines { + pp.add_define(name, value) + .map_err(|e| format!("macro {name}: {:?}", e.0))?; + } + let mut out = Preprocessed { + text: String::new(), + version: Vec::new(), + extensions: Vec::new(), + }; + let mut line = 1; + for item in pp { + let token = + item.map_err(|(e, at)| format!("{}: {e:?}", source.locate(line_of(at.start))))?; + let at = line_of(token.location.start); + let piece = match token.value { + TokenValue::Version(v) => { + out.version = v.tokens.into_iter().map(|t| spelling(t.value)).collect(); + continue; + } + TokenValue::Extension(e) => { + out.extensions + .push(e.tokens.into_iter().map(|t| spelling(t.value)).collect()); + continue; + } + TokenValue::Pragma(_) => continue, + value => spelling(value), + }; + if at > line { + out.text + .extend(std::iter::repeat_n('\n', (at - line) as usize)); + line = at; + } else if !out.text.is_empty() { + out.text.push(' '); + } + out.text.push_str(&piece); + } + out.text.push('\n'); + Ok(out) +} + +/// A token as GLSL source. +fn spelling(value: TokenValue) -> String { + match value { + TokenValue::Ident(s) => s, + TokenValue::Integer(i) => { + let suffix = if i.signed { "" } else { "u" }; + // Bit patterns such as 0xFFFFFFFF stay hexadecimal: as decimals + // they would overflow a signed int. + if i.value > i32::MAX as u64 { + format!("{:#X}{suffix}", i.value) + } else { + format!("{}{suffix}", i.value) + } + } + TokenValue::Float(f) => { + let mut s = format!("{:?}", f.value); + if !s.contains(['.', 'e', 'i', 'N']) { + s.push_str(".0"); + } + s + } + TokenValue::Punct(p) => punct(p).to_owned(), + TokenValue::Version(_) | TokenValue::Extension(_) | TokenValue::Pragma(_) => String::new(), + } +} + +fn punct(p: Punct) -> &'static str { + use Punct::*; + match p { + AddAssign => "+=", + SubAssign => "-=", + MulAssign => "*=", + DivAssign => "/=", + ModAssign => "%=", + LeftShiftAssign => "<<=", + RightShiftAssign => ">>=", + AndAssign => "&=", + XorAssign => "^=", + OrAssign => "|=", + Increment => "++", + Decrement => "--", + LogicalAnd => "&&", + LogicalOr => "||", + LogicalXor => "^^", + LessEqual => "<=", + GreaterEqual => ">=", + EqualEqual => "==", + NotEqual => "!=", + LeftShift => "<<", + RightShift => ">>", + LeftBrace => "{", + RightBrace => "}", + LeftParen => "(", + RightParen => ")", + LeftBracket => "[", + RightBracket => "]", + LeftAngle => "<", + RightAngle => ">", + Semicolon => ";", + Comma => ",", + Colon => ":", + Dot => ".", + Equal => "=", + Bang => "!", + Minus => "-", + Tilde => "~", + Plus => "+", + Star => "*", + Slash => "/", + Percent => "%", + Pipe => "|", + Caret => "^", + Ampersand => "&", + Question => "?", + } +} diff --git a/crates/horizon-shaderpack/src/source.rs b/crates/horizon-shaderpack/src/source.rs new file mode 100644 index 000000000..0501661ce --- /dev/null +++ b/crates/horizon-shaderpack/src/source.rs @@ -0,0 +1,141 @@ +//! A Java shader pack's files: a folder or a zip holding a `shaders/` folder, +//! found the way Iris finds it (`Iris.loadExternalZipShaderpack`). + +use std::{ + collections::BTreeMap, + fs, + io::Read, + path::{Path, PathBuf}, +}; + +/// Program source extensions Iris loads (vertex, fragment, geometry, +/// compute, tessellation control and evaluation). +pub const PROGRAM_EXTENSIONS: &[&str] = &["vsh", "fsh", "gsh", "csh", "tcs", "tes"]; + +/// The files under a pack's `shaders/` folder, addressed by `/`-separated +/// paths relative to it (`lib/common.glsl`). +pub enum PackFiles { + Folder(PathBuf), + /// A zip is small (a few MB of text), so it is read once. + Zip(BTreeMap>), +} + +impl PackFiles { + /// Open `path`, a pack folder (with `shaders/` in it) or a `.zip`. + pub fn open(path: &Path) -> Result { + if path.is_dir() { + let shaders = path.join("shaders"); + return if shaders.is_dir() { + Ok(Self::Folder(shaders)) + } else { + Err("no shaders folder".into()) + }; + } + let file = fs::File::open(path).map_err(|e| e.to_string())?; + let mut zip = zip::ZipArchive::new(file).map_err(|e| format!("not a zip: {e}"))?; + let names: Vec = zip.file_names().map(str::to_owned).collect(); + // `shaders/` at the root, else the first folder ending in `shaders` + // (`Pack-main/shaders/`), as Iris walks the zip. + let mut prefixes: Vec<&str> = names + .iter() + .filter_map(|n| { + let at = n.find("shaders/")?; + (at == 0 || n.as_bytes()[at - 1] == b'/').then(|| &n[..at + "shaders/".len()]) + }) + .collect(); + prefixes.sort_by_key(|p| (p.len(), *p)); + let prefix = prefixes.first().ok_or("no shaders folder")?.to_string(); + let mut files = BTreeMap::new(); + for name in &names { + let Some(rel) = name.strip_prefix(&prefix) else { + continue; + }; + if rel.is_empty() || rel.ends_with('/') { + continue; + } + let mut entry = zip.by_name(name).map_err(|e| e.to_string())?; + let mut bytes = Vec::new(); + entry + .read_to_end(&mut bytes) + .map_err(|e| format!("{name}: {e}"))?; + files.insert(rel.to_owned(), bytes); + } + Ok(Self::Zip(files)) + } + + /// A text file, or `None` when it doesn't exist. Invalid UTF-8 is + /// replaced, as packs are often saved in Latin-1. + pub fn read(&self, rel: &str) -> Option { + let bytes = match self { + Self::Folder(root) => fs::read(root.join(rel)).ok()?, + Self::Zip(files) => files.get(rel)?.clone(), + }; + Some(String::from_utf8_lossy(&bytes).into_owned()) + } + + /// The files directly in folder `dir` (`""` for `shaders/` itself), sorted. + pub fn list(&self, dir: &str) -> Vec { + let prefix = if dir.is_empty() { + String::new() + } else { + format!("{dir}/") + }; + let mut out: Vec = match self { + Self::Folder(root) => fs::read_dir(root.join(dir)) + .into_iter() + .flatten() + .flatten() + .filter(|e| e.file_type().is_ok_and(|t| t.is_file())) + .filter_map(|e| Some(format!("{prefix}{}", e.file_name().to_str()?))) + .collect(), + Self::Zip(files) => files + .keys() + .filter(|k| { + k.strip_prefix(&prefix) + .is_some_and(|rest| !rest.contains('/')) + }) + .cloned() + .collect(), + }; + out.sort(); + out + } + + /// The program folders: `shaders/` itself and the per-dimension folders, + /// from `dimension.properties` (`dimension.world-1=minecraft:the_nether`) + /// or, without one, Iris's defaults `world0`, `world-1` and `world1`. + pub fn program_dirs(&self) -> Vec { + let mut dirs = vec![String::new()]; + match self.read("dimension.properties") { + Some(text) => { + for line in text.lines() { + if let Some((key, _)) = line.trim().split_once('=') { + if let Some(dir) = key.trim().strip_prefix("dimension.") { + dirs.push(dir.to_owned()); + } + } + } + } + None => dirs.extend(["world0", "world-1", "world1"].map(String::from)), + } + dirs.dedup(); + dirs + } + + /// Every program source file of the pack (`composite1.fsh`, + /// `world-1/gbuffers_water.vsh`), sorted. + pub fn programs(&self) -> Vec { + let mut out: Vec = self + .program_dirs() + .iter() + .flat_map(|d| self.list(d)) + .filter(|f| { + f.rsplit_once('.') + .is_some_and(|(_, ext)| PROGRAM_EXTENSIONS.contains(&ext)) + }) + .collect(); + out.sort(); + out.dedup(); + out + } +} diff --git a/crates/horizon-shaderpack/tests/fixtures/mini/shaders/composite.fsh b/crates/horizon-shaderpack/tests/fixtures/mini/shaders/composite.fsh new file mode 100644 index 000000000..948ee25c6 --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/mini/shaders/composite.fsh @@ -0,0 +1,10 @@ +#version 120 +#extension GL_ARB_shader_texture_lod : enable +#include "/lib/common.glsl" +uniform sampler2D colortex0; +varying vec2 texcoord; +void main() { +#if MC_VERSION >= 260300 && defined IS_IRIS + gl_FragData[0] = texture2D(colortex0, texcoord) * float(MC_RENDER_STAGE_TERRAIN_SOLID); +#endif +} diff --git a/crates/horizon-shaderpack/tests/fixtures/mini/shaders/composite.vsh b/crates/horizon-shaderpack/tests/fixtures/mini/shaders/composite.vsh new file mode 100644 index 000000000..eb261aadd --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/mini/shaders/composite.vsh @@ -0,0 +1,3 @@ +#version 120 +varying vec2 texcoord; +void main() { gl_Position = ftransform(); texcoord = gl_MultiTexCoord0.xy; } diff --git a/crates/horizon-shaderpack/tests/fixtures/mini/shaders/lib/common.glsl b/crates/horizon-shaderpack/tests/fixtures/mini/shaders/lib/common.glsl new file mode 100644 index 000000000..bb88b82c3 --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/mini/shaders/lib/common.glsl @@ -0,0 +1,7 @@ +#include "settings.glsl" +#ifdef BLOOM +float bloomStrength = 0.5 * QUALITY; +#endif +#ifdef DOF +float dofStrength = 1.0; +#endif diff --git a/crates/horizon-shaderpack/tests/fixtures/mini/shaders/lib/settings.glsl b/crates/horizon-shaderpack/tests/fixtures/mini/shaders/lib/settings.glsl new file mode 100644 index 000000000..0d5b4b1c9 --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/mini/shaders/lib/settings.glsl @@ -0,0 +1,7 @@ +// Options, as packs write them. +#define BLOOM // Bloom on bright light +//#define DOF // Depth of field +//#define UNREFERENCED +#define QUALITY 2 // Quality [1 2 3] +#define NOT_AN_OPTION 3 +const float sunPathRotation = -40.0; // [-40.0 0.0 40.0] diff --git a/crates/horizon-shaderpack/tests/fixtures/mini/shaders/notes.txt b/crates/horizon-shaderpack/tests/fixtures/mini/shaders/notes.txt new file mode 100644 index 000000000..528b86abe --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/mini/shaders/notes.txt @@ -0,0 +1 @@ +Not a program. diff --git a/crates/horizon-shaderpack/tests/fixtures/mini/shaders/world-1/composite.fsh b/crates/horizon-shaderpack/tests/fixtures/mini/shaders/world-1/composite.fsh new file mode 100644 index 000000000..7d2f794da --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/mini/shaders/world-1/composite.fsh @@ -0,0 +1,4 @@ +#version 120 +#include "../lib/common.glsl" +#include "/lib/missing.glsl" +void main() {} diff --git a/crates/horizon-shaderpack/tests/pack.rs b/crates/horizon-shaderpack/tests/pack.rs new file mode 100644 index 000000000..a21f48fde --- /dev/null +++ b/crates/horizon-shaderpack/tests/pack.rs @@ -0,0 +1,223 @@ +//! The preprocessing half of the Java shader pack translation on a small +//! hand-written pack (`tests/fixtures/mini`), and on real packs when +//! `HORIZON_TEST_SHADERPACKS` names a folder of them (development only: +//! third-party packs are never committed). + +use horizon_shaderpack::{JavaPack, Stage, preprocess::Environment}; +use std::{ + collections::BTreeMap, + io::Write, + path::{Path, PathBuf}, +}; + +fn mini() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/mini") +} + +fn env() -> Environment { + Environment::iris("Apple", "Apple M4 Pro", 4) +} + +fn saved(pairs: &[(&str, &str)]) -> BTreeMap { + pairs + .iter() + .map(|(k, v)| (k.to_string(), v.to_string())) + .collect() +} + +#[test] +fn finds_programs_and_options() { + let pack = JavaPack::open(&mini()).unwrap(); + assert_eq!( + pack.programs, + ["composite.fsh", "composite.vsh", "world-1/composite.fsh"] + ); + assert_eq!(Stage::of(&pack.programs[1]), Some(Stage::Vertex)); + let bools: Vec<(&str, bool)> = pack + .options + .bools + .values() + .map(|o| (o.name.as_str(), o.default)) + .collect(); + assert_eq!( + bools, + [("BLOOM", true), ("DOF", false)], + "UNREFERENCED has no #ifdef" + ); + let strings: Vec<(&str, &str)> = pack + .options + .strings + .values() + .map(|o| (o.name.as_str(), o.default.as_str())) + .collect(); + assert_eq!(strings, [("QUALITY", "2"), ("sunPathRotation", "-40.0")]); + assert_eq!( + pack.options.strings["QUALITY"].comment.as_deref(), + Some("Quality") + ); +} + +#[test] +fn preprocesses_with_defaults_and_with_values() { + let pack = JavaPack::open(&mini()).unwrap(); + let defaults = pack + .preprocess("composite.fsh", &pack.values(&BTreeMap::new()), &env()) + .unwrap(); + assert_eq!(defaults.version, ["120"]); + assert_eq!( + defaults.extensions, + [["GL_ARB_shader_texture_lod", ":", "enable"]] + ); + let text = defaults + .text + .split_whitespace() + .collect::>() + .join(" "); + assert!(text.contains("float bloomStrength = 0.5 * 2 ;"), "{text}"); + assert!(!text.contains("dofStrength"), "{text}"); + assert!( + text.contains("float ( 8 )"), + "Iris's render stage macros: {text}" + ); + assert!(!text.contains('#'), "no directive is left: {text}"); + + let values = pack.values(&saved(&[ + ("BLOOM", "false"), + ("DOF", "true"), + ("QUALITY", "3"), + ])); + let changed = pack + .preprocess("composite.fsh", &values, &env()) + .unwrap() + .text; + assert!( + !changed.contains("bloomStrength") && changed.contains("dofStrength"), + "{changed}" + ); + + let mut source = pack.source("composite.fsh", &values).unwrap(); + assert!( + source + .text + .contains("#define QUALITY 3 // OptionAnnotatedSource: Changed option") + ); + // Output lines are the inlined lines: errors can name file:line. + let line = defaults + .text + .lines() + .position(|l| l.contains("bloomStrength")) + .unwrap() as u32 + + 1; + assert_eq!(source.locate(line), "lib/common.glsl:3"); + source.text = "#if\n".into(); + assert!( + horizon_shaderpack::preprocess::preprocess(&source, &env()) + .unwrap_err() + .starts_with("composite.fsh:1:") + ); +} + +#[test] +fn reports_missing_includes_and_cycles() { + let pack = JavaPack::open(&mini()).unwrap(); + let err = pack + .source("world-1/composite.fsh", &Default::default()) + .err() + .unwrap(); + assert_eq!( + err, + "world-1/composite.fsh:3: #include \"lib/missing.glsl\": file not found" + ); + + let dir = scratch("cycle"); + std::fs::create_dir_all(dir.join("shaders/lib")).unwrap(); + std::fs::write( + dir.join("shaders/final.fsh"), + "#include \"/lib/a.glsl\"\nvoid main() {}\n", + ) + .unwrap(); + std::fs::write(dir.join("shaders/lib/a.glsl"), "#include \"b.glsl\"\n").unwrap(); + std::fs::write(dir.join("shaders/lib/b.glsl"), "#include \"a.glsl\"\n").unwrap(); + let pack = JavaPack::open(&dir).unwrap(); + let err = pack.source("final.fsh", &Default::default()).err().unwrap(); + assert_eq!( + err, + "#include cycle: final.fsh -> lib/a.glsl -> lib/b.glsl -> lib/a.glsl" + ); + std::fs::remove_dir_all(dir).unwrap(); +} + +#[test] +fn opens_zips_with_shaders_in_a_subfolder() { + let dir = scratch("zip"); + std::fs::create_dir_all(&dir).unwrap(); + let zip_path = dir.join("Mini-main.zip"); + let mut zip = zip::ZipWriter::new(std::fs::File::create(&zip_path).unwrap()); + let options = zip::write::SimpleFileOptions::default(); + let root = mini().join("shaders"); + for rel in [ + "composite.fsh", + "composite.vsh", + "lib/common.glsl", + "lib/settings.glsl", + "world-1/composite.fsh", + ] { + zip.start_file(format!("Mini-main/shaders/{rel}"), options) + .unwrap(); + zip.write_all(&std::fs::read(root.join(rel)).unwrap()) + .unwrap(); + } + zip.start_file("Mini-main/docs/shaders/readme.txt", options) + .unwrap(); + zip.finish().unwrap(); + let zipped = JavaPack::open(&zip_path).unwrap(); + let folder = JavaPack::open(&mini()).unwrap(); + assert_eq!(zipped.programs, folder.programs); + let values = zipped.values(&BTreeMap::new()); + assert_eq!( + zipped + .preprocess("composite.fsh", &values, &env()) + .unwrap() + .text, + folder + .preprocess("composite.fsh", &values, &env()) + .unwrap() + .text + ); + std::fs::remove_dir_all(dir).unwrap(); +} + +fn scratch(name: &str) -> PathBuf { + std::env::temp_dir().join(format!("horizon-shaderpack-{name}-{}", std::process::id())) +} + +/// Every program of the packs in `HORIZON_TEST_SHADERPACKS` (folders or +/// zips) preprocesses. Skipped when the variable isn't set. +#[test] +fn real_packs_preprocess() { + let Some(dir) = std::env::var_os("HORIZON_TEST_SHADERPACKS") else { + return; + }; + let mut failures = Vec::new(); + for entry in std::fs::read_dir(dir).unwrap().flatten() { + let Ok(pack) = JavaPack::open(&entry.path()) else { + continue; + }; + let values = pack.values(&BTreeMap::new()); + let mut ok = 0; + for program in &pack.programs { + match pack.preprocess(program, &values, &env()) { + Ok(_) => ok += 1, + Err(e) => failures.push(format!("{}: {program}: {e}", entry.path().display())), + } + } + println!( + "{}: {ok}/{} programs, {} boolean and {} value options", + entry.file_name().to_string_lossy(), + pack.programs.len(), + pack.options.bools.len(), + pack.options.strings.len() + ); + } + assert!(failures.is_empty(), "{}", failures.join("\n")); +} diff --git a/docs/shader-packs.md b/docs/shader-packs.md index e3dd5fbca..025e28894 100644 --- a/docs/shader-packs.md +++ b/docs/shader-packs.md @@ -16,6 +16,7 @@ Both are off by default. With no pack selected the renderer draws exactly what i | Horizon shader packs: `composite` stage, uniforms, hot reload, Shader Packs screen | first slice planned | | Horizon shader packs: `deferred`, `gbuffer`, `shadow` stages, custom textures | planned | | Java shader packs: preprocessing + GLSL parse coverage | spike done: 450 of 470 programs reach valid WGSL (below) | +| Java shader packs: preprocessor (`crates/horizon-shaderpack`: includes, options, Iris's macros) | done: every program of the three test packs preprocesses | | Java shader packs: Iris pipeline emulation | planned | ## How the renderer draws a frame today From 98ebdcad7b27100b569ca55b717507e2dd896bbc Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 22:07:42 +0200 Subject: [PATCH 02/97] horizon-shaderpack: translate Java shader pack programs to WGSL The legaliser of docs/shader-packs.md step 1, on a syntax tree (glsl-lang) instead of the spike's tokens: attribute/varying to located in/out with matrix and array varyings split, loose uniforms in one std140 block that extends across a pass, combined samplers split into texture + sampler bindings (through function parameters, respecting locals that hide them), compatibility built-ins and Iris's core-profile names declared, legacy texture and shadow calls, hoisted global initialisers, sized unsized arrays, isnan/isinf/modf helpers, and dead code after a jump dropped (the spike's 'naga constructor bug' in BSL was code after discard). naga then parses, validates and writes WGSL. The programs of one pass share a Link so locations, bindings and the uniform layout agree. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 384 +++++- crates/horizon-shaderpack/Cargo.toml | 2 + crates/horizon-shaderpack/src/lib.rs | 18 + crates/horizon-shaderpack/src/translate.rs | 1297 ++++++++++++++++++ crates/horizon-shaderpack/tests/translate.rs | 352 +++++ docs/shader-packs.md | 7 + 6 files changed, 2054 insertions(+), 6 deletions(-) create mode 100644 crates/horizon-shaderpack/src/translate.rs create mode 100644 crates/horizon-shaderpack/tests/translate.rs diff --git a/Cargo.lock b/Cargo.lock index 536ba6a55..8d5cd15fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -178,6 +178,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b" +[[package]] +name = "ascii-canvas" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef1e3e699d84ab1b0911a1010c5c106aa34ae89aeac103be5ce0c3859db1e891" +dependencies = [ + "term", +] + [[package]] name = "ash" version = "0.38.0+1.3.281" @@ -205,15 +214,30 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" +[[package]] +name = "bit-set" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3" +dependencies = [ + "bit-vec 0.8.0", +] + [[package]] name = "bit-set" version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2f926cc3060f09db9ebc5b52823d85268d24bb917e472c0c4bea35780a7d" dependencies = [ - "bit-vec", + "bit-vec 0.9.1", ] +[[package]] +name = "bit-vec" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7" + [[package]] name = "bit-vec" version = "0.9.1" @@ -259,6 +283,16 @@ dependencies = [ "objc2 0.6.5", ] +[[package]] +name = "borsh" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "553c5d846a6ba5150c65e3b1b8ec073bcf1abc20f9b7220de384a4443ea4e20a" +dependencies = [ + "bytes", + "cfg_aliases", +] + [[package]] name = "bumpalo" version = "3.20.3" @@ -450,6 +484,15 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "convert_case" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "633458d4ef8c78b72454de2d54fd6ab2e60f9e02be22f3c6104cdc8a4e0fceb9" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "coords-hud" version = "0.1.0" @@ -754,6 +797,41 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "darling" +version = "0.20.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc7f46116c46ff9ab3eb1597a45688b6715c6e628b5c133e288e709a29bcb4ee" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d00b9596d185e565c2207a0b01f8bd1a135483d02d9b7b0a54b11da8d53412e" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 2.0.119", +] + +[[package]] +name = "darling_macro" +version = "0.20.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc34b93ccb385b40dc71c6fceac4b2ad23662c7eeb248cf10d529b7e055b6ead" +dependencies = [ + "darling_core", + "quote", + "syn 2.0.119", +] + [[package]] name = "dasp_sample" version = "0.11.0" @@ -771,6 +849,29 @@ dependencies = [ "syn 3.0.6", ] +[[package]] +name = "derive_more" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d751e9e49156b02b44f9c1815bcb94b984cdcc4396ecc32521c739452808b134" +dependencies = [ + "derive_more-impl", +] + +[[package]] +name = "derive_more-impl" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "799a97264921d8623a957f6c3b9011f3b5492f557bbb7a5a19b7fa6d06ba8dcb" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "rustc_version", + "syn 2.0.119", + "unicode-xid", +] + [[package]] name = "digest" version = "0.10.7" @@ -857,6 +958,15 @@ version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" +[[package]] +name = "ena" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eabffdaee24bd1bf95c5ef7cec31260444317e72ea56c4c91750e8b7ee58d5f1" +dependencies = [ + "log", +] + [[package]] name = "encoding_rs" version = "0.8.42" @@ -1120,6 +1230,58 @@ version = "0.30.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19fc433e8437a212d1b6f1e68c7824af3aed907da60afa994e7f542d18d12aa9" +[[package]] +name = "glsl-lang" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "983a18d0c2ecad18ac9ff3c94b069d6bd03b47405a200836d9d8d63def8e34de" +dependencies = [ + "glsl-lang-lexer", + "glsl-lang-types", + "lalrpop", + "lalrpop-util", + "lang-util", + "once_cell", + "thiserror 2.0.21", +] + +[[package]] +name = "glsl-lang-lexer" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abdbc0bdfbc81d557f4865e695457eb23d68031bfb061fda3444f56511b26eee" +dependencies = [ + "glsl-lang-pp", + "glsl-lang-types", + "lalrpop-util", + "lang-util", + "thiserror 2.0.21", +] + +[[package]] +name = "glsl-lang-pp" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88677876fb27f116015d0ecebda6d77bdf65f3a6fa3f5dfeed10b8c8f04cb2c5" +dependencies = [ + "arrayvec", + "derive_more", + "lang-util", + "string_cache", + "string_cache_codegen", + "thiserror 2.0.21", +] + +[[package]] +name = "glsl-lang-types" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd8b1532b424d40df634cf2dc2be56d59f994b9ac07fa7c1e3183fd33db9cb07" +dependencies = [ + "lang-util", + "thiserror 2.0.21", +] + [[package]] name = "gpu-allocator" version = "0.28.0" @@ -1368,6 +1530,8 @@ dependencies = [ name = "horizon-shaderpack" version = "0.1.0" dependencies = [ + "glsl-lang", + "naga", "pp-rs", "zip", ] @@ -1512,6 +1676,12 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "idna" version = "1.1.0" @@ -1694,6 +1864,73 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "keccak" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb26cec98cce3a3d96cbb7bced3c4b16e3d13f27ec56dbd62cbc8f39cfb9d653" +dependencies = [ + "cpufeatures", +] + +[[package]] +name = "lalrpop" +version = "0.22.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba4ebbd48ce411c1d10fb35185f5a51a7bfa3d8b24b4e330d30c9e3a34129501" +dependencies = [ + "ascii-canvas", + "bit-set 0.8.0", + "ena", + "itertools", + "lalrpop-util", + "petgraph 0.7.1", + "pico-args", + "regex", + "regex-syntax", + "sha3", + "string_cache", + "term", + "unicode-xid", + "walkdir", +] + +[[package]] +name = "lalrpop-util" +version = "0.22.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5baa5e9ff84f1aefd264e6869907646538a52147a755d494517a8007fb48733" +dependencies = [ + "regex-automata", + "rustversion", +] + +[[package]] +name = "lang-util" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1715a1e56998f119cd6d49ac8a69802dcbe4fca96d8291243d6dae87abeb913" +dependencies = [ + "derive_more", + "lalrpop-util", + "lang-util-derive", + "line-span", + "smol_str 0.3.6", + "text-size", +] + +[[package]] +name = "lang-util-derive" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f2acc66aedf0e445534e0a6b3f0f6ee20ef6ebb9647dbe10735e52ea8d6a096" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "lazy_static" version = "1.5.1" @@ -1750,6 +1987,12 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "line-span" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29fc123b2f6600099ca18248f69e3ee02b09c4188c0d98e9a690d90dec3e408a" + [[package]] name = "linux-raw-sys" version = "0.4.15" @@ -1861,7 +2104,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a616d2fb8c89516ac2723a581f69d6c18576046bed761bd6b305e5618e6ae130" dependencies = [ "arrayvec", - "bit-set", + "bit-set 0.10.0", "bitflags 2.13.2", "cfg-if", "cfg_aliases", @@ -1874,6 +2117,7 @@ dependencies = [ "naga-types", "num-traits", "once_cell", + "pp-rs", "rustc-hash 1.1.0", "spirv", "thiserror 2.0.21", @@ -1922,6 +2166,12 @@ dependencies = [ "jni-sys 0.3.1", ] +[[package]] +name = "new_debug_unreachable" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" + [[package]] name = "nix" version = "0.31.3" @@ -2421,6 +2671,16 @@ version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" +[[package]] +name = "petgraph" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3672b37090dbd86368a4145bc067582552b29c27377cad4e0a306c97f9bd7772" +dependencies = [ + "fixedbitset", + "indexmap", +] + [[package]] name = "petgraph" version = "0.8.3" @@ -2432,6 +2692,31 @@ dependencies = [ "indexmap", ] +[[package]] +name = "phf_generator" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c80231409c20246a13fddb31776fb942c38553c51e871f8cbd687a4cfb5843d" +dependencies = [ + "phf_shared", + "rand", +] + +[[package]] +name = "phf_shared" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67eabc2ef2a60eb7faa00097bd1ffdb5bd28e62bf39990626a582201b7a754e5" +dependencies = [ + "siphasher", +] + +[[package]] +name = "pico-args" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315" + [[package]] name = "pin-project" version = "1.1.13" @@ -2559,6 +2844,12 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "precomputed-hash" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" + [[package]] name = "presser" version = "0.3.1" @@ -2652,6 +2943,15 @@ version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" +[[package]] +name = "rand" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c" +dependencies = [ + "rand_core", +] + [[package]] name = "rand_core" version = "0.6.4" @@ -2984,6 +3284,16 @@ dependencies = [ "digest", ] +[[package]] +name = "sha3" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77fd7028345d415a4034cf8777cd4f8ab1851274233b45f84e3d955502d93874" +dependencies = [ + "digest", + "keccak", +] + [[package]] name = "shlex" version = "2.0.1" @@ -3012,6 +3322,12 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" +[[package]] +name = "siphasher" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33f4fe9184a62d842c9ef383018f3306d8ba224fd9d836f56d7288308847c256" + [[package]] name = "slab" version = "0.4.12" @@ -3061,6 +3377,16 @@ dependencies = [ "serde", ] +[[package]] +name = "smol_str" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4aaa7368fcf4852a4c2dd92df0cace6a71f2091ca0a23391ce7f3a31833f1523" +dependencies = [ + "borsh", + "serde_core", +] + [[package]] name = "socket2" version = "0.5.10" @@ -3110,6 +3436,37 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731" +[[package]] +name = "string_cache" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf776ba3fa74f83bf4b63c3dcbbf82173db2632ed8452cb2d891d33f459de70f" +dependencies = [ + "new_debug_unreachable", + "parking_lot", + "phf_shared", + "precomputed-hash", + "serde", +] + +[[package]] +name = "string_cache_codegen" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c711928715f1fe0fe509c53b43e993a9a557babc2d0a3567d0a3006f1ac931a0" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro2", + "quote", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "subtle" version = "2.6.1" @@ -3226,6 +3583,15 @@ version = "0.13.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adb6935a6f5c20170eeceb1a3835a49e12e19d792f6dd344ccc76a985ca5a6ca" +[[package]] +name = "term" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8c27177b12a6399ffc08b98f76f7c9a1f4fe9fc967c784c5a071fa8d93cf7e1" +dependencies = [ + "windows-sys 0.61.2", +] + [[package]] name = "termcolor" version = "1.4.1" @@ -3235,6 +3601,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "text-size" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f18aa187839b2bdb1ad2fa35ead8c4c2976b64e4363c386d45ac0f7ee85c9233" + [[package]] name = "thiserror" version = "1.0.69" @@ -3394,7 +3766,7 @@ checksum = "b8765b90061cba6c22b5831f675da109ae5561588290f9fa2317adab2714d5a6" dependencies = [ "memchr", "nom", - "petgraph", + "petgraph 0.8.3", ] [[package]] @@ -4115,8 +4487,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c018fce9b6270aa203c2fdd56f3cce996713534bd757e4ea58c8560b121f14" dependencies = [ "arrayvec", - "bit-set", - "bit-vec", + "bit-set 0.10.0", + "bit-vec 0.9.1", "bitflags 2.13.2", "bytemuck", "cfg_aliases", @@ -4662,7 +5034,7 @@ dependencies = [ "rustix 0.38.44", "sctk-adwaita", "smithay-client-toolkit", - "smol_str", + "smol_str 0.2.2", "tracing", "unicode-segmentation", "wasm-bindgen", diff --git a/crates/horizon-shaderpack/Cargo.toml b/crates/horizon-shaderpack/Cargo.toml index da9ae1a12..91a2251bc 100644 --- a/crates/horizon-shaderpack/Cargo.toml +++ b/crates/horizon-shaderpack/Cargo.toml @@ -7,6 +7,8 @@ rust-version.workspace = true description = "Java (OptiFine/Iris) shader packs for Horizon: Iris's preprocessor and the GLSL to WGSL translation" [dependencies] +glsl-lang = "0.8.1" +naga = { version = "30.0.1", features = ["glsl-in", "wgsl-in", "wgsl-out"] } # The C preprocessor naga's GLSL front end uses. pp-rs = "0.2.1" zip = { version = "2", default-features = false, features = ["deflate"] } diff --git a/crates/horizon-shaderpack/src/lib.rs b/crates/horizon-shaderpack/src/lib.rs index 36bfaf37d..18a69d635 100644 --- a/crates/horizon-shaderpack/src/lib.rs +++ b/crates/horizon-shaderpack/src/lib.rs @@ -8,6 +8,7 @@ pub mod include; pub mod options; pub mod preprocess; pub mod source; +pub mod translate; use include::{IncludeGraph, Inlined}; use options::{Annotated, OptionSet, OptionValues}; @@ -17,6 +18,7 @@ use std::{ collections::{BTreeMap, BTreeSet}, path::Path, }; +use translate::{Link, Translated}; /// A program's pipeline stage, from its file extension. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -119,4 +121,20 @@ impl JavaPack { ) -> Result { preprocess::preprocess(&self.source(program, values)?, env) } + + /// `program` translated to WGSL. The programs of one pass (its `.vsh`, + /// then its `.fsh`) share `link`. + pub fn translate( + &self, + program: &str, + values: &OptionValues, + env: &Environment, + link: &mut Link, + ) -> Result { + let stage = Stage::of(program).ok_or_else(|| format!("{program}: not a program"))?; + let source = self.source(program, values)?; + let pre = preprocess::preprocess(&source, env)?; + translate::translate(stage, &pre.text, link, &|line| source.locate(line)) + .map_err(|e| format!("{program}: {e}")) + } } diff --git a/crates/horizon-shaderpack/src/translate.rs b/crates/horizon-shaderpack/src/translate.rs new file mode 100644 index 000000000..fdd3ad128 --- /dev/null +++ b/crates/horizon-shaderpack/src/translate.rs @@ -0,0 +1,1297 @@ +//! Preprocessed pack GLSL to WGSL. Packs are written for `#version 120`..`460 +//! compatibility` and lean on the compatibility profile; naga's GLSL front end +//! takes Vulkan-style GLSL 450. The legaliser parses the program into a +//! syntax tree (glsl-lang), rewrites it the way Iris's glsl-transformer +//! patches do for the core profile, prints GLSL 450 and has naga turn that +//! into a validated module and WGSL: +//! +//! - `attribute`/`varying` become `in`/`out` with locations; matrix and array +//! varyings are split into vectors (WGSL can pass neither between stages); +//! - loose uniforms go into one std140 block (`bool`s stored as `int`); +//! - `sampler2D` & co. become a texture and a sampler binding, rebuilt as +//! `sampler2D(t, s)` where used, also through function parameters, and not +//! where a local hides them; +//! - compatibility built-ins (`gl_Vertex`, `gl_Color`, `gl_FragData[n]`, +//! `gl_ModelViewMatrix`, `ftransform()`, ...) and Iris's core-profile names +//! (`vaPosition`, `modelViewMatrix`, ...) are declared; +//! - legacy texture calls are renamed, `shadow2D` returns a `vec4` again and a +//! swizzled shadow lookup is widened first (naga has no scalar swizzle); +//! - global initialisers run at the start of `main` (drivers accept +//! non-constant ones), unsized global arrays are sized from them; +//! - `isnan`, `isinf` and `modf` are helper functions (WGSL has none). +//! +//! The programs of one pass share a [`Link`], so their locations, bindings and +//! uniform block agree. + +use crate::Stage; +use glsl_lang::{ + ast::{self, NodeContent}, + parse::DefaultParse, + transpiler::glsl::{ + FormattingState, show_declaration, show_expr, show_external_declaration, + show_type_specifier, + }, +}; +use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; + +/// Locations, bindings and uniforms shared by the programs of one pass: a +/// pass's vertex program is translated first, its fragment program second. +#[derive(Default, Debug)] +pub struct Link { + /// Varying name → location (split matrices and arrays as `name#i`). + pub varyings: BTreeMap, + /// Vertex attribute name → location. + pub attributes: BTreeMap, + /// Texture, sampler, image and block name → binding in group 0 (the + /// uniform block is binding 0). + pub bindings: BTreeMap, + /// The uniform block's members in order (`"mat4 gbufferModelView"`); a + /// later program's block extends an earlier one's, so offsets agree. + pub uniforms: Vec<(String, String)>, + /// Storage formats of images whose declaration has none. + pub image_formats: BTreeMap, +} + +impl Link { + fn location(map: &mut BTreeMap, name: &str) -> u32 { + let next = map.len() as u32; + *map.entry(name.to_owned()).or_insert(next) + } + + fn binding(&mut self, name: &str) -> u32 { + let next = self.bindings.len() as u32 + 1; + *self.bindings.entry(name.to_owned()).or_insert(next) + } + + fn uniform(&mut self, name: &str, decl: String) -> Result<(), String> { + match self.uniforms.iter().find(|(n, _)| n == name) { + Some((_, d)) if *d != decl => Err(format!( + "uniform {name} is declared as both `{d}` and `{decl}`" + )), + Some(_) => Ok(()), + None => { + self.uniforms.push((name.to_owned(), decl)); + Ok(()) + } + } + } +} + +/// What a translated program binds and passes, for the pipeline that runs it. +#[derive(Default, Debug)] +pub struct Interface { + /// `(name, kind)` of each sampler the program reads (`colortex0`, + /// `sampler2D`); its bindings are `{name}_t` and `{name}_s` in the [`Link`]. + pub samplers: Vec<(String, String)>, + /// `(name, format)` of each image. + pub images: Vec<(String, String)>, + /// Uniform names the program declares or uses. + pub uniforms: BTreeSet, + /// Vertex attributes read (vertex programs). + pub attributes: BTreeSet, + /// Fragment outputs: location → name (`iris_FragData0` for `gl_FragData[0]`). + pub outputs: BTreeMap, +} + +pub struct Translated { + /// The legalised GLSL 450 naga read (kept for diagnostics). + pub glsl: String, + pub module: naga::Module, + pub info: naga::valid::ModuleInfo, + pub wgsl: String, + pub interface: Interface, +} + +/// Combined sampler types: (texture type, sampler type, compares depth). +const OPAQUE: &[(&str, &str, &str, bool)] = &[ + ("sampler1D", "texture1D", "sampler", false), + ("sampler2D", "texture2D", "sampler", false), + ("sampler3D", "texture3D", "sampler", false), + ("samplerCube", "textureCube", "sampler", false), + ("sampler2DArray", "texture2DArray", "sampler", false), + ("sampler2DShadow", "texture2D", "samplerShadow", true), + ( + "sampler2DArrayShadow", + "texture2DArray", + "samplerShadow", + true, + ), + ("samplerCubeShadow", "textureCube", "samplerShadow", true), + ("isampler2D", "itexture2D", "sampler", false), + ("usampler2D", "utexture2D", "sampler", false), + ("isampler3D", "itexture3D", "sampler", false), + ("usampler3D", "utexture3D", "sampler", false), + ("isampler2DArray", "itexture2DArray", "sampler", false), + ("usampler2DArray", "utexture2DArray", "sampler", false), +]; + +fn opaque(ty: &str) -> Option<&'static (&'static str, &'static str, &'static str, bool)> { + OPAQUE.iter().find(|o| o.0 == ty) +} + +/// Compatibility-profile and ARB calls and their core names. +const LEGACY_CALLS: &[(&str, &str)] = &[ + ("texture1D", "texture"), + ("texture2D", "texture"), + ("texture3D", "texture"), + ("textureCube", "texture"), + ("texture2DLod", "textureLod"), + ("texture2DLodARB", "textureLod"), + ("texture3DLod", "textureLod"), + ("textureCubeLod", "textureLod"), + ("texture2DGrad", "textureGrad"), + ("texture2DGradARB", "textureGrad"), + ("texture2DProj", "textureProj"), + ("texture2DProjLod", "textureProjLod"), + ("texelFetch2D", "texelFetch"), + ("isnan", "iris_isnan"), + ("isinf", "iris_isinf"), + ("modf", "iris_modf"), +]; + +/// Legacy shadow lookups, which return a `vec4`. +const SHADOW_CALLS: &[(&str, &str)] = &[ + ("shadow2D", "texture"), + ("shadow2DLod", "textureLod"), + ("shadow2DProj", "textureProj"), +]; + +/// Compatibility-profile uniforms: (name, member declaration). +const LEGACY_UNIFORMS: &[(&str, &str)] = &[ + ("gl_ModelViewMatrix", "mat4 iris_ModelViewMatrix"), + ("gl_ProjectionMatrix", "mat4 iris_ProjectionMatrix"), + ( + "gl_ModelViewProjectionMatrix", + "mat4 iris_ModelViewProjectionMatrix", + ), + ("gl_NormalMatrix", "mat3 iris_NormalMatrix"), + ( + "gl_ModelViewMatrixInverse", + "mat4 iris_ModelViewMatrixInverse", + ), + ( + "gl_ProjectionMatrixInverse", + "mat4 iris_ProjectionMatrixInverse", + ), + ("gl_TextureMatrix", "mat4 iris_TextureMatrix[8]"), + ("gl_Fog", "iris_FogParameters iris_Fog"), +]; + +/// Compatibility-profile vertex attributes: (name, type). +const LEGACY_ATTRIBUTES: &[(&str, &str)] = &[ + ("gl_Vertex", "vec4"), + ("gl_Normal", "vec3"), + ("gl_Color", "vec4"), + ("gl_MultiTexCoord0", "vec4"), + ("gl_MultiTexCoord1", "vec4"), + ("gl_MultiTexCoord2", "vec4"), + ("gl_MultiTexCoord3", "vec4"), + ("gl_FogCoord", "float"), +]; + +/// Iris's core-profile names, declared when a program uses them without +/// declaring them: (name, type, is an attribute). +const IRIS_IMPLICIT: &[(&str, &str, bool)] = &[ + ("vaPosition", "vec3", true), + ("vaColor", "vec4", true), + ("vaUV0", "vec2", true), + ("vaUV1", "ivec2", true), + ("vaUV2", "ivec2", true), + ("vaNormal", "vec3", true), + ("modelViewMatrix", "mat4", false), + ("modelViewMatrixInverse", "mat4", false), + ("projectionMatrix", "mat4", false), + ("projectionMatrixInverse", "mat4", false), + ("normalMatrix", "mat3", false), + ("textureMatrix", "mat4", false), + ("chunkOffset", "vec3", false), + ("alphaTestRef", "float", false), +]; + +const HELPERS: &str = "\ +struct iris_FogParameters { vec4 color; float density; float start; float end; float scale; }; +bool iris_isnan(float x) { return x != x; } +bvec2 iris_isnan(vec2 x) { return notEqual(x, x); } +bvec3 iris_isnan(vec3 x) { return notEqual(x, x); } +bvec4 iris_isnan(vec4 x) { return notEqual(x, x); } +bool iris_isinf(float x) { return abs(x) > 3.4028235e38; } +bvec2 iris_isinf(vec2 x) { return greaterThan(abs(x), vec2(3.4028235e38)); } +bvec3 iris_isinf(vec3 x) { return greaterThan(abs(x), vec3(3.4028235e38)); } +bvec4 iris_isinf(vec4 x) { return greaterThan(abs(x), vec4(3.4028235e38)); } +float iris_modf(float x, out float i) { i = trunc(x); return x - i; } +vec2 iris_modf(vec2 x, out vec2 i) { i = trunc(x); return x - i; } +vec3 iris_modf(vec3 x, out vec3 i) { i = trunc(x); return x - i; } +vec4 iris_modf(vec4 x, out vec4 i) { i = trunc(x); return x - i; } +"; + +/// Translate one preprocessed program. `locate` turns a line of `source` +/// into `file:line` for errors. +pub fn translate( + stage: Stage, + source: &str, + link: &mut Link, + locate: &dyn Fn(u32) -> String, +) -> Result { + let naga_stage = match stage { + Stage::Vertex => naga::ShaderStage::Vertex, + Stage::Fragment => naga::ShaderStage::Fragment, + Stage::Compute => naga::ShaderStage::Compute, + _ => return Err(format!("{stage:?} programs are not supported")), + }; + let mut unit = ast::TranslationUnit::parse(source).map_err(|e| { + let line = e.line() + 1; + format!("{}: {}", locate(line), e.inner()) + })?; + let mut legaliser = Legaliser::new(stage, link); + legaliser.unit(&mut unit)?; + let glsl = legaliser.finish(&unit)?; + let options = naga::front::glsl::Options { + stage: naga_stage, + defines: Default::default(), + }; + let module = naga::front::glsl::Frontend::default() + .parse(&options, &glsl) + .map_err(|e| first_lines(&e.emit_to_string(&glsl)))?; + let info = naga::valid::Validator::new( + naga::valid::ValidationFlags::all(), + naga::valid::Capabilities::all(), + ) + .validate(&module) + .map_err(|e| first_lines(&e.emit_to_string(&glsl)))?; + let wgsl = + naga::back::wgsl::write_string(&module, &info, naga::back::wgsl::WriterFlags::empty()) + .map_err(|e| e.to_string())?; + let interface = std::mem::take(&mut legaliser.interface); + Ok(Translated { + glsl, + module, + info, + wgsl, + interface, + }) +} + +fn first_lines(s: &str) -> String { + s.lines().take(8).collect::>().join("\n") +} + +/// What a name means inside a function. +#[derive(Clone, Copy)] +enum Local { + Plain, + /// A sampler parameter, split into `{name}_t`, `{name}_s`. + Sampler(&'static str), +} + +struct Legaliser<'a> { + stage: Stage, + link: &'a mut Link, + interface: Interface, + /// Global samplers: name → combined type. + samplers: HashMap, + /// User functions → indices of their sampler parameters. + sampler_params: HashMap>, + functions: HashSet, + scopes: Vec>, + /// Declarations before the program (bindings, block, inputs, outputs). + header: Vec, + /// Statements run at the start and end of `main`. + prologue: Vec, + epilogue: Vec, + /// Names the program declares at global scope. + declared: HashSet, + /// Identifiers used, for the implicit declarations. + used: HashSet, + errors: Vec, +} + +fn var(name: &str) -> ast::Expr { + ast::ExprData::Variable(ast::IdentifierData::from(name).into_node()).into_node() +} + +fn call(name: &str, args: Vec) -> ast::Expr { + ast::ExprData::FunCall( + ast::FunIdentifierData::Expr(Box::new(var(name))).into_node(), + args, + ) + .into_node() +} + +/// The name a call is made by: a function, or a constructor's type. +fn callee(f: &ast::FunIdentifier) -> Option { + match &f.content { + ast::FunIdentifierData::Expr(e) => match &e.content { + ast::ExprData::Variable(id) => Some(id.as_str().to_owned()), + _ => None, + }, + ast::FunIdentifierData::TypeSpecifier(t) if t.array_specifier.is_none() => { + Some(type_text(t)) + } + ast::FunIdentifierData::TypeSpecifier(_) => None, + } +} + +fn type_text(t: &ast::TypeSpecifier) -> String { + let mut s = String::new(); + let _ = show_type_specifier(&mut s, t, &mut FormattingState::default()); + s +} + +fn expr_text(e: &ast::Expr) -> String { + let mut s = String::new(); + let _ = show_expr(&mut s, e, &mut FormattingState::default()); + s +} + +fn storage(q: &Option) -> Vec { + q.iter() + .flat_map(|q| &q.qualifiers) + .filter_map(|s| match &s.content { + ast::TypeQualifierSpecData::Storage(s) => Some(s.content.clone()), + _ => None, + }) + .collect() +} + +/// The interpolation and `centroid` words of a declaration. +fn interpolation(q: &Option) -> String { + let mut out = String::new(); + for s in q.iter().flat_map(|q| &q.qualifiers) { + match &s.content { + ast::TypeQualifierSpecData::Interpolation(i) => out.push_str(match i.content { + ast::InterpolationQualifierData::Flat => "flat ", + ast::InterpolationQualifierData::Smooth => "smooth ", + ast::InterpolationQualifierData::NoPerspective => "noperspective ", + }), + ast::TypeQualifierSpecData::Storage(s) + if s.content == ast::StorageQualifierData::Centroid => + { + out.push_str("centroid ") + } + _ => {} + } + } + out +} + +/// `layout(...)` entries of a declaration, as text (`rgba16f`, `location = 0`). +fn layout_ids(q: &Option) -> Vec<(String, Option)> { + q.iter() + .flat_map(|q| &q.qualifiers) + .filter_map(|s| match &s.content { + ast::TypeQualifierSpecData::Layout(l) => Some(l), + _ => None, + }) + .flat_map(|l| &l.ids) + .map(|id| match &id.content { + ast::LayoutQualifierSpecData::Identifier(i, e) => { + (i.as_str().to_owned(), e.as_ref().map(|e| expr_text(e))) + } + ast::LayoutQualifierSpecData::Shared => ("shared".to_owned(), None), + }) + .collect() +} + +/// Array sizes as text (`[3]`), `[]` for unsized ones. +fn array_text(a: &Option) -> String { + a.iter() + .flat_map(|a| &a.dimensions) + .map(|d| match &d.content { + ast::ArraySpecifierDimensionData::Unsized => "[]".to_owned(), + ast::ArraySpecifierDimensionData::ExplicitlySized(e) => format!("[{}]", expr_text(e)), + }) + .collect() +} + +/// The element count of `T[](a, b, c)`. +fn constructor_len(init: &Option) -> Option { + match &init.as_ref()?.content { + ast::InitializerData::Simple(e) => match &e.content { + ast::ExprData::FunCall(f, args) => match &f.content { + ast::FunIdentifierData::TypeSpecifier(t) if t.array_specifier.is_some() => { + Some(args.len()) + } + _ => None, + }, + _ => None, + }, + ast::InitializerData::List(items) => Some(items.len()), + } +} + +/// Size `[]` in a declaration and in its `T[](...)` initialiser. +fn size_arrays( + len: usize, + decl_array: &mut Option, + ty: &mut ast::TypeSpecifier, + init: &mut Option, +) { + let sized = || { + ast::ArraySpecifierData { + dimensions: vec![ + ast::ArraySpecifierDimensionData::ExplicitlySized(Box::new( + ast::ExprData::IntConst(len as i32).into_node(), + )) + .into_node(), + ], + } + .into_node() + }; + let is_unsized = |a: &Option| { + a.as_ref().is_some_and(|a| { + a.dimensions.len() == 1 + && a.dimensions[0].content == ast::ArraySpecifierDimensionData::Unsized + }) + }; + if is_unsized(decl_array) { + *decl_array = Some(sized()); + } + if is_unsized(&ty.array_specifier) { + ty.array_specifier = Some(sized()); + } + if let Some(ast::InitializerData::Simple(e)) = init.as_mut().map(|i| &mut i.content) { + if let ast::ExprData::FunCall(f, _) = &mut e.content { + if let ast::FunIdentifierData::TypeSpecifier(t) = &mut f.content { + if is_unsized(&t.array_specifier) { + t.array_specifier = Some(sized()); + } + } + } + } +} + +/// Columns of a matrix type (`mat3` → 3 × `vec3`, `mat2x4` → 2 × `vec4`). +fn matrix_columns(ty: &str) -> Option<(usize, String)> { + let rest = ty.strip_prefix("mat").or_else(|| ty.strip_prefix("dmat"))?; + let (cols, rows) = match rest.split_once('x') { + Some((c, r)) => (c.parse().ok()?, r.parse::().ok()?), + None => (rest.parse().ok()?, rest.parse().ok()?), + }; + Some((cols, format!("vec{rows}"))) +} + +fn is_integer(ty: &str) -> bool { + ty.starts_with("int") + || ty.starts_with("uint") + || ty.starts_with("ivec") + || ty.starts_with("uvec") +} + +impl<'a> Legaliser<'a> { + fn new(stage: Stage, link: &'a mut Link) -> Self { + Self { + stage, + link, + interface: Interface::default(), + samplers: HashMap::new(), + sampler_params: HashMap::new(), + functions: HashSet::new(), + scopes: Vec::new(), + header: Vec::new(), + prologue: Vec::new(), + epilogue: Vec::new(), + declared: HashSet::new(), + used: HashSet::new(), + errors: Vec::new(), + } + } + + fn unit(&mut self, unit: &mut ast::TranslationUnit) -> Result<(), String> { + // Functions and their sampler parameters first: calls may precede + // definitions through prototypes. + for ed in &unit.0 { + let proto = match &ed.content { + ast::ExternalDeclarationData::FunctionDefinition(f) => &f.prototype, + ast::ExternalDeclarationData::Declaration(d) => match &d.content { + ast::DeclarationData::FunctionPrototype(p) => p, + _ => continue, + }, + _ => continue, + }; + let name = proto.name.as_str().to_owned(); + for (i, p) in proto.parameters.iter().enumerate() { + let ty = match &p.content { + ast::FunctionParameterDeclarationData::Named(_, d) => &d.ty, + ast::FunctionParameterDeclarationData::Unnamed(_, t) => t, + }; + if opaque(&type_text(ty)).is_some() { + self.sampler_params + .entry(name.clone()) + .or_default() + .insert(i); + } + } + self.functions.insert(name); + } + let mut kept = Vec::with_capacity(unit.0.len()); + for mut ed in std::mem::take(&mut unit.0) { + let keep = match &mut ed.content { + ast::ExternalDeclarationData::Preprocessor(_) => false, + ast::ExternalDeclarationData::FunctionDefinition(f) => { + self.function(f); + true + } + ast::ExternalDeclarationData::Declaration(d) => self.global(d)?, + }; + if keep { + kept.push(ed); + } + } + unit.0 = kept; + if self.errors.is_empty() { + Ok(()) + } else { + Err(self.errors.join("\n")) + } + } + + /// A global declaration; `false` when it moves into the header. + fn global(&mut self, d: &mut ast::Declaration) -> Result { + match &mut d.content { + ast::DeclarationData::FunctionPrototype(p) => { + self.prototype(p); + Ok(true) + } + ast::DeclarationData::Precision(..) => Ok(false), + ast::DeclarationData::Block(b) => { + let binding = self.link.binding(b.name.as_str()); + let buffer = storage(&Some(b.qualifier.clone())) + .contains(&ast::StorageQualifierData::Buffer); + let kind = if buffer { "std430" } else { "std140" }; + b.qualifier + .qualifiers + .retain(|q| !matches!(q.content, ast::TypeQualifierSpecData::Layout(_))); + let mut text = String::new(); + show_declaration(&mut text, d, &mut FormattingState::default()) + .map_err(|e| e.to_string())?; + self.header.push(format!( + "layout({kind}, set = 0, binding = {binding}) {}", + text.trim() + )); + Ok(false) + } + ast::DeclarationData::Invariant(_) | ast::DeclarationData::TypeOnly(_) => Ok(true), + ast::DeclarationData::InitDeclaratorList(list) => self.global_list(list), + } + } + + fn global_list(&mut self, list: &mut ast::InitDeclaratorList) -> Result { + let qualifier = list.head.ty.qualifier.clone(); + let storage = storage(&qualifier); + let has = |s: ast::StorageQualifierData| storage.contains(&s); + let ty = type_text(&list.head.ty.ty); + let ty_array = array_text(&list.head.ty.ty.array_specifier); + let mut names: Vec<(String, String)> = Vec::new(); + if let Some(n) = &list.head.name { + names.push(( + n.as_str().to_owned(), + array_text(&list.head.array_specifier), + )); + } + for t in &list.tail { + names.push(( + t.ident.ident.as_str().to_owned(), + array_text(&t.ident.array_spec), + )); + } + self.declared.extend(names.iter().map(|(n, _)| n.clone())); + let input = has(ast::StorageQualifierData::Attribute) + || has(ast::StorageQualifierData::In) && self.stage == Stage::Vertex; + let varying = has(ast::StorageQualifierData::Varying) + || has(ast::StorageQualifierData::Out) && self.stage == Stage::Vertex + || has(ast::StorageQualifierData::In) && self.stage == Stage::Fragment; + if has(ast::StorageQualifierData::Uniform) { + if let Some(&(combined, texture, sampler, _)) = opaque(&ty) { + for (name, _) in &names { + let t = self.link.binding(&format!("{name}_t")); + let s = self.link.binding(&format!("{name}_s")); + self.header.push(format!( + "layout(set = 0, binding = {t}) uniform {texture} {name}_t;" + )); + self.header.push(format!( + "layout(set = 0, binding = {s}) uniform {sampler} {name}_s;" + )); + self.samplers.insert(name.clone(), combined); + self.interface + .samplers + .push((name.clone(), combined.to_owned())); + } + } else if ["image", "iimage", "uimage"] + .iter() + .any(|p| ty.starts_with(p)) + { + let mut layout: Vec = Vec::new(); + let ids = layout_ids(&qualifier); + let mut format = ids + .iter() + .find(|(k, v)| v.is_none() && k != "shared") + .map(|(k, _)| k.clone()); + let memory: String = storage + .iter() + .filter_map(|s| match s { + ast::StorageQualifierData::ReadOnly => Some("readonly "), + ast::StorageQualifierData::WriteOnly => Some("writeonly "), + ast::StorageQualifierData::Coherent => Some("coherent "), + ast::StorageQualifierData::Restrict => Some("restrict "), + ast::StorageQualifierData::Volatile => Some("volatile "), + _ => None, + }) + .collect(); + for (name, _) in &names { + let f = format + .take() + .or_else(|| self.link.image_formats.get(name).cloned()) + .unwrap_or_else(|| "rgba8".to_owned()); + let binding = self.link.binding(name); + layout.push(format!( + "layout({f}, set = 0, binding = {binding}) {memory}uniform {ty} {name};" + )); + self.interface.images.push((name.clone(), f)); + } + self.header.extend(layout); + } else { + if ty.starts_with("struct") || ty_array.contains("[]") { + return Err(format!( + "uniform {}: unsupported type {ty}{ty_array}", + names[0].0 + )); + } + for (name, array) in &names { + if ty == "bool" { + self.link + .uniform(name, format!("int iris_bool_{name}{array}"))?; + self.header.push(format!("bool {name}{array};")); + self.prologue + .push(format!("{name} = iris_bool_{name} != 0;")); + } else { + self.link + .uniform(name, format!("{ty}{ty_array} {name}{array}"))?; + } + self.interface.uniforms.insert(name.clone()); + } + } + return Ok(false); + } + if input || varying || has(ast::StorageQualifierData::Out) && self.stage == Stage::Fragment + { + let dir = if input || varying && self.stage == Stage::Fragment { + "in" + } else { + "out" + }; + let interp = interpolation(&qualifier); + let explicit = layout_ids(&qualifier) + .into_iter() + .find(|(k, _)| k == "location") + .and_then(|(_, v)| v?.parse().ok()); + for (i, (name, array)) in names.iter().enumerate() { + let full = format!("{ty}{ty_array}"); + if input { + let at = Link::location(&mut self.link.attributes, name); + self.header + .push(format!("layout(location = {at}) in {full} {name}{array};")); + self.interface.attributes.insert(name.clone()); + } else if !varying { + let at = explicit + .map(|l: u32| l + i as u32) + .unwrap_or_else(|| self.next_output()); + self.header + .push(format!("layout(location = {at}) out {full} {name}{array};")); + self.interface.outputs.insert(at, name.clone()); + } else { + self.varying(dir, &interp, &full, name, array); + } + } + return Ok(false); + } + if has(ast::StorageQualifierData::Const) || has(ast::StorageQualifierData::Shared) { + self.sized(list); + self.global_initialisers(list, false); + return Ok(true); + } + self.sized(list); + self.global_initialisers(list, true); + Ok(true) + } + + fn next_output(&self) -> u32 { + (0..) + .find(|l| !self.interface.outputs.contains_key(l)) + .unwrap() + } + + /// One varying: matrices and arrays split into vectors that a private + /// global of the original type is packed from or into. + fn varying(&mut self, dir: &str, interp: &str, ty: &str, name: &str, array: &str) { + let flat = if is_integer(ty) && !interp.contains("flat") { + "flat " + } else { + "" + }; + let count: Option = array + .strip_prefix('[') + .and_then(|a| a.strip_suffix(']')) + .and_then(|n| n.parse().ok()); + let parts: Vec<(String, String, String)> = match (matrix_columns(ty), count) { + (Some((cols, column)), None) => (0..cols) + .map(|c| { + ( + format!("{name}#{c}"), + column.clone(), + format!("{name}[{c}]"), + ) + }) + .collect(), + (None, Some(n)) => (0..n) + .map(|e| (format!("{name}#{e}"), ty.to_owned(), format!("{name}[{e}]"))) + .collect(), + (None, None) => { + let at = Link::location(&mut self.link.varyings, name); + self.header.push(format!( + "layout(location = {at}) {flat}{interp}{dir} {ty} {name};" + )); + return; + } + (Some(_), Some(_)) => { + self.errors.push(format!( + "varying {name}: arrays of matrices are not supported" + )); + return; + } + }; + self.header.push(format!("{ty} {name}{array};")); + let mut columns = Vec::new(); + for (key, part_ty, element) in &parts { + let at = Link::location(&mut self.link.varyings, key); + let part = key.replace('#', "_iris"); + self.header.push(format!( + "layout(location = {at}) {flat}{interp}{dir} {part_ty} {part};" + )); + if dir == "out" { + self.epilogue.push(format!("{part} = {element};")); + } else if count.is_some() { + self.prologue.push(format!("{element} = {part};")); + } + columns.push(part); + } + if dir == "in" && count.is_none() { + self.prologue + .push(format!("{name} = {ty}({});", columns.join(", "))); + } + } + + fn sized(&mut self, list: &mut ast::InitDeclaratorList) { + let head_ty = list.head.ty.ty.clone(); + let head = &mut list.head.content; + if let Some(len) = constructor_len(&head.initializer) { + size_arrays( + len, + &mut head.array_specifier, + &mut head.ty.content.ty, + &mut head.initializer, + ); + } + for t in &mut list.tail { + let t = &mut t.content; + if let Some(len) = constructor_len(&t.initializer) { + let mut ty = head_ty.clone(); + size_arrays( + len, + &mut t.ident.content.array_spec, + &mut ty, + &mut t.initializer, + ); + } + } + } + + /// Rewrite global initialisers; with `hoist`, move simple ones to the + /// start of `main`. + fn global_initialisers(&mut self, list: &mut ast::InitDeclaratorList, hoist: bool) { + let take = |this: &mut Self, name: &str, init: &mut Option| { + let Some(i) = init else { return }; + match &mut i.content { + ast::InitializerData::Simple(e) => { + this.expr(e); + if hoist { + this.prologue.push(format!("{name} = {};", expr_text(e))); + *init = None; + } + } + ast::InitializerData::List(_) => {} + } + }; + if let Some(n) = list.head.name.clone() { + take(self, n.as_str(), &mut list.head.initializer); + } + for t in &mut list.tail { + let n = t.ident.ident.as_str().to_owned(); + take(self, &n, &mut t.initializer); + } + } + + fn prototype(&mut self, p: &mut ast::FunctionPrototype) -> Vec<(String, Local)> { + if p.name.as_str() == "main" { + p.name = ast::IdentifierData::from("iris_main").into_node(); + } + let mut locals = Vec::new(); + let mut params = Vec::with_capacity(p.parameters.len()); + for param in std::mem::take(&mut p.parameters) { + let (ty, name) = match ¶m.content { + ast::FunctionParameterDeclarationData::Named(_, d) => { + (type_text(&d.ty), Some(d.ident.ident.as_str().to_owned())) + } + ast::FunctionParameterDeclarationData::Unnamed(_, t) => (type_text(t), None), + }; + match (opaque(&ty), name) { + (Some(&(combined, texture, sampler, _)), Some(name)) => { + for (t, n) in [ + (texture, format!("{name}_t")), + (sampler, format!("{name}_s")), + ] { + let spec = ast::TypeSpecifierData { + ty: ast::TypeSpecifierNonArrayData::TypeName( + ast::TypeNameData::from(t).into_node(), + ) + .into_node(), + array_specifier: None, + } + .into_node(); + let ident = ast::ArrayedIdentifierData { + ident: ast::IdentifierData::from(n.as_str()).into_node(), + array_spec: None, + } + .into_node(); + let decl = + ast::FunctionParameterDeclaratorData { ty: spec, ident }.into_node(); + params.push( + ast::FunctionParameterDeclarationData::Named(None, decl).into_node(), + ); + } + locals.push((name, Local::Sampler(combined))); + } + (_, name) => { + locals.extend(name.map(|n| (n, Local::Plain))); + params.push(param); + } + } + } + p.parameters = params; + locals + } + + fn function(&mut self, f: &mut ast::FunctionDefinition) { + let locals = self.prototype(&mut f.prototype); + self.scopes.push(locals.into_iter().collect()); + self.statements(&mut f.statement.statement_list); + self.scopes.pop(); + } + + fn declare(&mut self, name: &str) { + if let Some(scope) = self.scopes.last_mut() { + scope.insert(name.to_owned(), Local::Plain); + } + } + + fn local_declaration(&mut self, d: &mut ast::Declaration) { + let ast::DeclarationData::InitDeclaratorList(list) = &mut d.content else { + return; + }; + self.sized(list); + if let Some(i) = &mut list.head.initializer { + self.initializer(i); + } + if let Some(n) = &list.head.name { + let n = n.as_str().to_owned(); + self.declare(&n); + } + for t in &mut list.tail { + if let Some(i) = &mut t.initializer { + self.initializer(i); + } + let n = t.ident.ident.as_str().to_owned(); + self.declare(&n); + } + } + + fn initializer(&mut self, i: &mut ast::Initializer) { + match &mut i.content { + ast::InitializerData::Simple(e) => self.expr(e), + ast::InitializerData::List(items) => items.iter_mut().for_each(|i| self.initializer(i)), + } + } + + /// A list of statements, without the dead code after an unconditional + /// `return`, `discard`, `break` or `continue` (up to the next `case`): + /// naga leaves such code unvisited and rejects the function. + fn statements(&mut self, list: &mut Vec) { + let mut dead = false; + list.retain_mut(|s| { + match s.content { + ast::StatementData::CaseLabel(_) => dead = false, + _ if dead => return false, + ast::StatementData::Jump(_) => dead = true, + _ => {} + } + true + }); + list.iter_mut().for_each(|s| self.statement(s)); + } + + fn scoped(&mut self, s: &mut ast::Statement) { + self.scopes.push(HashMap::new()); + self.statement(s); + self.scopes.pop(); + } + + fn statement(&mut self, s: &mut ast::Statement) { + match &mut s.content { + ast::StatementData::Declaration(d) => self.local_declaration(d), + ast::StatementData::Expression(e) => { + if let Some(e) = &mut e.0 { + self.expr(e); + } + } + ast::StatementData::Selection(sel) => { + self.expr(&mut sel.cond); + match &mut sel.rest.content { + ast::SelectionRestStatementData::Statement(a) => self.scoped(a), + ast::SelectionRestStatementData::Else(a, b) => { + self.scoped(a); + self.scoped(b); + } + } + } + ast::StatementData::Switch(sw) => { + self.expr(&mut sw.head); + self.scopes.push(HashMap::new()); + self.statements(&mut sw.body); + self.scopes.pop(); + } + ast::StatementData::CaseLabel(c) => { + if let ast::CaseLabelData::Case(e) = &mut c.content { + self.expr(e); + } + } + ast::StatementData::Iteration(it) => { + self.scopes.push(HashMap::new()); + match &mut it.content { + ast::IterationStatementData::While(c, body) => { + self.condition(c); + self.scoped(body); + } + ast::IterationStatementData::DoWhile(body, e) => { + self.scoped(body); + self.expr(e); + } + ast::IterationStatementData::For(init, rest, body) => { + match &mut init.content { + ast::ForInitStatementData::Expression(Some(e)) => self.expr(e), + ast::ForInitStatementData::Expression(None) => {} + ast::ForInitStatementData::Declaration(d) => self.local_declaration(d), + } + if let Some(c) = &mut rest.condition { + self.condition(c); + } + if let Some(e) = &mut rest.post_expr { + self.expr(e); + } + self.scoped(body); + } + } + self.scopes.pop(); + } + ast::StatementData::Jump(j) => { + if let ast::JumpStatementData::Return(Some(e)) = &mut j.content { + self.expr(e); + } + } + ast::StatementData::Compound(c) => { + self.scopes.push(HashMap::new()); + self.statements(&mut c.statement_list); + self.scopes.pop(); + } + } + } + + fn condition(&mut self, c: &mut ast::Condition) { + match &mut c.content { + ast::ConditionData::Expr(e) => self.expr(e), + ast::ConditionData::Assignment(_, name, init) => { + self.initializer(init); + let n = name.as_str().to_owned(); + self.declare(&n); + } + } + } + + fn local(&self, name: &str) -> Option { + self.scopes.iter().rev().find_map(|s| s.get(name).copied()) + } + + /// What a variable reads, if it must change. + fn variable(&mut self, name: &str) -> Option { + let combined = match self.local(name) { + Some(Local::Sampler(c)) => Some(c), + Some(Local::Plain) => return None, + None => self.samplers.get(name).copied(), + }; + if let Some(c) = combined { + return Some(call( + c, + vec![var(&format!("{name}_t")), var(&format!("{name}_s"))], + )); + } + self.used.insert(name.to_owned()); + if let Some((_, decl)) = LEGACY_UNIFORMS.iter().find(|(n, _)| *n == name) { + let member = decl.split(' ').nth(1).unwrap().split('[').next().unwrap(); + if let Err(e) = self.link.uniform(member, (*decl).to_owned()) { + self.errors.push(e); + } + self.interface.uniforms.insert(member.to_owned()); + return Some(var(member)); + } + let renamed = match (name, self.stage) { + ("gl_FragColor", Stage::Fragment) => return Some(self.frag_data(0)), + ("gl_VertexID", _) => "gl_VertexIndex".to_owned(), + ("gl_InstanceID", _) => "gl_InstanceIndex".to_owned(), + ("gl_FrontColor", Stage::Vertex) | ("gl_Color", Stage::Fragment) => { + self.legacy_varying("iris_FrontColor", "vec4"); + "iris_FrontColor".to_owned() + } + ("gl_FogFragCoord", Stage::Vertex | Stage::Fragment) => { + self.legacy_varying("iris_FogFragCoord", "float"); + "iris_FogFragCoord".to_owned() + } + (n, Stage::Vertex) if LEGACY_ATTRIBUTES.iter().any(|(a, _)| *a == n) => { + let ty = LEGACY_ATTRIBUTES.iter().find(|(a, _)| *a == n).unwrap().1; + let renamed = n.replacen("gl_", "iris_", 1); + if self.interface.attributes.insert(renamed.clone()) { + let at = Link::location(&mut self.link.attributes, &renamed); + self.header + .push(format!("layout(location = {at}) in {ty} {renamed};")); + } + renamed + } + _ => return None, + }; + Some(var(&renamed)) + } + + fn legacy_varying(&mut self, name: &str, ty: &str) { + if self.declared.insert(name.to_owned()) { + let dir = if self.stage == Stage::Vertex { + "out" + } else { + "in" + }; + let at = Link::location(&mut self.link.varyings, name); + self.header + .push(format!("layout(location = {at}) {dir} {ty} {name};")); + } + } + + fn frag_data(&mut self, n: u32) -> ast::Expr { + let name = format!("iris_FragData{n}"); + if self.interface.outputs.insert(n, name.clone()).is_none() { + self.header + .push(format!("layout(location = {n}) out vec4 {name};")); + } + var(&name) + } + + fn expr(&mut self, e: &mut ast::Expr) { + use ast::ExprData as E; + match &mut e.content { + E::Variable(id) => { + let name = id.as_str().to_owned(); + if let Some(new) = self.variable(&name) { + *e = new; + } + } + E::IntConst(_) + | E::UIntConst(_) + | E::BoolConst(_) + | E::FloatConst(_) + | E::DoubleConst(_) => {} + E::Unary(_, a) | E::PostInc(a) | E::PostDec(a) => self.expr(a), + E::Binary(_, a, b) | E::Assignment(a, _, b) | E::Comma(a, b) => { + self.expr(a); + self.expr(b); + } + E::Ternary(a, b, c) => { + self.expr(a); + self.expr(b); + self.expr(c); + } + E::Bracket(a, i) => { + let array = match &a.content { + E::Variable(id) if self.local(id.as_str()).is_none() => id.as_str().to_owned(), + _ => String::new(), + }; + let index = match i.content { + E::IntConst(n) => Some(n as u32), + E::UIntConst(n) => Some(n), + _ => None, + }; + match (array.as_str(), index) { + ("gl_FragData", Some(n)) if self.stage == Stage::Fragment => { + *e = self.frag_data(n) + } + ("gl_TexCoord", Some(n)) => { + let name = format!("iris_TexCoord{n}"); + self.legacy_varying(&name, "vec4"); + *e = var(&name); + } + ("gl_FragData" | "gl_TexCoord", None) => self.errors.push(format!( + "{array} indexed by a non-constant: {}", + expr_text(e) + )), + _ => { + self.expr(a); + self.expr(i); + } + } + } + E::FunCall(_, args) => { + args.iter_mut().for_each(|a| self.expr(a)); + self.call(e); + } + E::Dot(a, _) => { + self.expr(a); + if self.shadow_lookup(a) { + let inner = std::mem::replace(&mut **a, var("_")); + **a = call("vec4", vec![inner]); + } + } + } + } + + /// Whether `e` is a `texture*` call on a depth-comparing sampler (a float). + fn shadow_lookup(&self, e: &ast::Expr) -> bool { + let ast::ExprData::FunCall(f, args) = &e.content else { + return false; + }; + let Some(name) = callee(f) else { return false }; + const LOOKUPS: &[&str] = &[ + "texture", + "textureLod", + "textureOffset", + "textureLodOffset", + "textureProj", + "textureProjLod", + "textureProjOffset", + "textureGrad", + "textureGradOffset", + ]; + if !LOOKUPS.contains(&name.as_str()) || self.functions.contains(&name) { + return false; + } + let Some(first) = args.first() else { + return false; + }; + match &first.content { + ast::ExprData::FunCall(f, _) => callee(f).and_then(|c| opaque(&c)).is_some_and(|o| o.3), + _ => false, + } + } + + fn call(&mut self, e: &mut ast::Expr) { + let ast::ExprData::FunCall(f, args) = &mut e.content else { + return; + }; + let Some(name) = callee(f) else { return }; + if name == "ftransform" && args.is_empty() && !self.functions.contains(&name) { + let mvp = self.variable("gl_ModelViewProjectionMatrix").unwrap(); + let vertex = self + .variable("gl_Vertex") + .unwrap_or_else(|| var("gl_Vertex")); + *e = ast::ExprData::Binary( + ast::BinaryOpData::Mult.into_node(), + Box::new(mvp), + Box::new(vertex), + ) + .into_node(); + return; + } + if let Some(params) = self.sampler_params.get(&name) { + // sampler2D(a_t, a_s) passed to a split parameter: a_t, a_s. + let mut spliced = Vec::with_capacity(args.len() + params.len()); + for (i, a) in std::mem::take(args).into_iter().enumerate() { + match &a.content { + ast::ExprData::FunCall(g, inner) + if params.contains(&i) + && inner.len() == 2 + && callee(g).is_some_and(|c| opaque(&c).is_some()) => + { + spliced.extend(inner.iter().cloned()) + } + _ => spliced.push(a), + } + } + *args = spliced; + return; + } + if self.functions.contains(&name) { + return; + } + if let Some((_, new)) = LEGACY_CALLS.iter().find(|(old, _)| *old == name) { + *f = ast::FunIdentifierData::Expr(Box::new(var(new))).into_node(); + } else if let Some((_, new)) = SHADOW_CALLS.iter().find(|(old, _)| *old == name) { + let args = std::mem::take(args); + *e = call("vec4", vec![call(new, args)]); + } + } + + /// The legalised source: header, the rewritten program, then a `main` + /// that runs the prologue, the program's own `main` and the epilogue. + fn finish(&mut self, unit: &ast::TranslationUnit) -> Result { + for &(name, ty, attribute) in IRIS_IMPLICIT { + if !self.used.contains(name) || self.declared.contains(name) { + continue; + } + if attribute && self.stage == Stage::Vertex { + let at = Link::location(&mut self.link.attributes, name); + self.header + .push(format!("layout(location = {at}) in {ty} {name};")); + self.interface.attributes.insert(name.to_owned()); + } else if !attribute { + self.link.uniform(name, format!("{ty} {name}"))?; + self.interface.uniforms.insert(name.to_owned()); + } + } + if !self.errors.is_empty() { + return Err(self.errors.join("\n")); + } + let mut out = String::from("#version 450 core\n"); + out.push_str(HELPERS); + if !self.link.uniforms.is_empty() { + out.push_str("layout(std140, set = 0, binding = 0) uniform IrisUniforms {\n"); + for (_, decl) in &self.link.uniforms { + out.push_str(&format!(" {decl};\n")); + } + out.push_str("};\n"); + } + for h in &self.header { + out.push_str(h); + out.push('\n'); + } + let mut state = FormattingState::default(); + for ed in &unit.0 { + show_external_declaration(&mut out, ed, &mut state).map_err(|e| e.to_string())?; + } + out.push_str("\nvoid main() {\n"); + for s in self + .prologue + .iter() + .chain(["iris_main();".to_owned()].iter()) + .chain(&self.epilogue) + { + out.push_str(" "); + out.push_str(s); + out.push('\n'); + } + out.push_str("}\n"); + Ok(out) + } +} diff --git a/crates/horizon-shaderpack/tests/translate.rs b/crates/horizon-shaderpack/tests/translate.rs new file mode 100644 index 000000000..862fb375e --- /dev/null +++ b/crates/horizon-shaderpack/tests/translate.rs @@ -0,0 +1,352 @@ +//! The GLSL → WGSL translation on hand-written compatibility-profile +//! programs, checked through naga's IR (locations, bindings, uniform block), +//! and on real packs when `HORIZON_TEST_SHADERPACKS` names a folder of them. + +use horizon_shaderpack::{ + JavaPack, Stage, + include::Inlined, + preprocess::{Environment, preprocess}, + translate::{Link, Translated, translate}, +}; +use std::collections::{BTreeMap, BTreeSet}; + +fn env() -> Environment { + Environment::iris("Apple", "Apple M4 Pro", 4) +} + +fn run(stage: Stage, source: &str, link: &mut Link) -> Result { + let inlined = Inlined { + text: source.to_owned(), + origin: Vec::new(), + }; + let pre = preprocess(&inlined, &env())?; + translate(stage, &pre.text, link, &|line| format!("line {line}")) +} + +/// `(name, location)` of an entry point's inputs (`result: false`) or outputs. +fn io(t: &Translated, result: bool) -> BTreeMap { + let f = &t.module.entry_points[0].function; + let mut out = BTreeMap::new(); + let mut add = + |name: Option<&String>, binding: &Option, ty: naga::Handle| { + match (binding, &t.module.types[ty].inner) { + (Some(naga::Binding::Location { location, .. }), _) => { + out.insert(name.cloned().unwrap_or_default(), *location); + } + (None, naga::TypeInner::Struct { members, .. }) => { + for m in members { + if let Some(naga::Binding::Location { location, .. }) = &m.binding { + out.insert(m.name.clone().unwrap_or_default(), *location); + } + } + } + _ => {} + } + }; + if result { + if let Some(r) = &f.result { + add(None, &r.binding, r.ty); + } + } else { + for a in &f.arguments { + add(a.name.as_ref(), &a.binding, a.ty); + } + } + out +} + +/// Group 0 bindings of the module's resources, by variable name. +fn bindings(t: &Translated) -> BTreeMap { + t.module + .global_variables + .iter() + .filter_map(|(_, v)| Some((v.name.clone()?, v.binding.as_ref()?.binding))) + .collect() +} + +/// The uniform block's member names, in order. +fn block_members(t: &Translated) -> Vec { + t.module + .global_variables + .iter() + .find(|(_, v)| v.space == naga::AddressSpace::Uniform) + .map(|(_, v)| match &t.module.types[v.ty].inner { + naga::TypeInner::Struct { members, .. } => { + members.iter().filter_map(|m| m.name.clone()).collect() + } + _ => Vec::new(), + }) + .unwrap_or_default() +} + +const VSH: &str = r#" +#version 120 +attribute vec4 mc_Entity; +uniform mat4 gbufferModelView; +uniform bool hasSkylight; +varying vec2 texcoord; +varying vec4 color; +varying mat3 tbn; +varying float entity; +void main() { + gl_Position = ftransform(); + texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy; + color = gl_Color; + gl_FrontColor = gl_Color; + tbn = mat3(gbufferModelView) * gl_NormalMatrix; + entity = hasSkylight ? mc_Entity.x : 0.0; +} +"#; + +const FSH: &str = r#" +#version 120 +uniform sampler2D texture; +uniform sampler2DShadow shadowtex0; +uniform float frameTimeCounter; +varying vec2 texcoord; +varying vec4 color; +varying mat3 tbn; +float phase = frameTimeCounter * 2.0; +const float weights[] = float[](0.25, 0.5, 0.25); +vec4 sampleTwice(sampler2D tex, vec2 uv) { + return texture2D(tex, uv) + texture2D(tex, uv + vec2(phase)); +} +float shade(vec3 p) { + float texture = shadow2D(shadowtex0, p).x; + return texture * weights[1]; +} +void main() { + vec4 albedo = sampleTwice(texture, texcoord) * color; + if (isnan(albedo.r)) { + discard; + albedo = vec4(0.0); + } + gl_FragData[0] = albedo * shade(tbn[2]) * gl_Color; + gl_FragData[1] = vec4(texture(shadowtex0, vec3(texcoord, 0.5)).x); +} +"#; + +#[test] +fn translates_a_compatibility_profile_pass() { + let mut link = Link::default(); + let vs = run(Stage::Vertex, VSH, &mut link).unwrap(); + let fs = run(Stage::Fragment, FSH, &mut link).unwrap(); + + // The varyings meet at the same locations, the mat3 as three columns. + let outs = io(&vs, true); + let ins = io(&fs, false); + for name in [ + "texcoord", + "color", + "tbn_iris0", + "tbn_iris1", + "tbn_iris2", + "iris_FrontColor", + ] { + assert_eq!( + outs.get(name), + ins.get(name), + "{name}: vs {outs:?} fs {ins:?}" + ); + assert!(outs.contains_key(name), "{name} in {outs:?}"); + } + assert!(outs.contains_key("entity") && !ins.contains_key("entity")); + // Attributes: the pack's, then the compatibility ones it reads. + let attributes: Vec = io(&vs, false).into_keys().collect(); + assert_eq!( + attributes, + [ + "iris_Color", + "iris_MultiTexCoord0", + "iris_Vertex", + "mc_Entity" + ] + ); + // gl_FragData[n] are the outputs at location n. + assert_eq!( + fs.interface.outputs.keys().copied().collect::>(), + [0, 1] + ); + assert_eq!( + io(&fs, true).values().copied().collect::>(), + [0, 1].into() + ); + + // One block whose members extend across the pass: the fragment program's + // starts with the vertex program's, so offsets agree. + let vs_block = block_members(&vs); + let fs_block = block_members(&fs); + assert_eq!( + vs_block, + [ + "gbufferModelView", + "iris_bool_hasSkylight", + "iris_ModelViewProjectionMatrix", + "iris_TextureMatrix", + "iris_NormalMatrix" + ] + ); + assert_eq!(fs_block[..vs_block.len()], vs_block[..]); + assert_eq!(fs_block[vs_block.len()..], ["frameTimeCounter"]); + + // Samplers split into texture + sampler bindings, shared by the pass. + let b = bindings(&fs); + let block = fs + .module + .global_variables + .iter() + .find(|(_, v)| v.space == naga::AddressSpace::Uniform) + .unwrap() + .1; + assert_eq!( + block.binding.as_ref().map(|b| (b.group, b.binding)), + Some((0, 0)) + ); + for name in ["texture_t", "texture_s", "shadowtex0_t", "shadowtex0_s"] { + assert_eq!(b[name], link.bindings[name], "{name}"); + } + assert_eq!( + fs.interface.samplers, + [ + ("texture".into(), "sampler2D".into()), + ("shadowtex0".into(), "sampler2DShadow".into()) + ] + ); + // A local named `texture` hides the sampler; the sampler parameter is split. + assert!( + fs.glsl.contains( + "float texture = vec4(texture(sampler2DShadow(shadowtex0_t, shadowtex0_s), p)).x;" + ), + "{}", + fs.glsl + ); + assert!( + fs.glsl + .contains("vec4 sampleTwice(texture2D tex_t, sampler tex_s, vec2 uv)"), + "{}", + fs.glsl + ); + assert!( + fs.glsl + .contains("sampleTwice(texture_t, texture_s, texcoord)"), + "{}", + fs.glsl + ); + // Global initialisers run first; the unsized array is sized. + assert!( + fs.glsl.contains("phase = frameTimeCounter * 2.;"), + "{}", + fs.glsl + ); + assert!( + fs.glsl + .contains("const float weights[3] = float[3](0.25, 0.5, 0.25);"), + "{}", + fs.glsl + ); + // The code after `discard` is gone, isnan is a helper. + assert!(!fs.glsl.contains("albedo = vec4(0.);"), "{}", fs.glsl); + assert!(fs.glsl.contains("iris_isnan(albedo.r)"), "{}", fs.glsl); + assert!(fs.wgsl.contains("@fragment")); +} + +#[test] +fn reports_errors_with_their_place() { + let mut link = Link::default(); + let err = run( + Stage::Fragment, + "void main() {\n float x = ;\n}\n", + &mut link, + ) + .err() + .unwrap(); + assert!(err.starts_with("line 2:"), "{err}"); + let err = run(Stage::Geometry, "void main() {}", &mut link) + .err() + .unwrap(); + assert_eq!(err, "Geometry programs are not supported"); + let mut link = Link::default(); + run( + Stage::Vertex, + "uniform float a;\nvoid main() { gl_Position = vec4(a); }", + &mut link, + ) + .unwrap(); + let err = run( + Stage::Fragment, + "uniform int a;\nvoid main() { gl_FragColor = vec4(a); }", + &mut link, + ) + .err() + .unwrap(); + assert_eq!(err, "uniform a is declared as both `float a` and `int a`"); +} + +/// The written WGSL, read back and validated: what wgpu is given. +fn reparse(wgsl: &str) -> Result<(), String> { + let module = naga::front::wgsl::parse_str(wgsl).map_err(|e| e.emit_to_string(wgsl))?; + naga::valid::Validator::new( + naga::valid::ValidationFlags::all(), + naga::valid::Capabilities::all(), + ) + .validate(&module) + .map(|_| ()) + .map_err(|e| e.emit_to_string(wgsl)) +} + +/// Every program the packs in `HORIZON_TEST_SHADERPACKS` enable by default +/// translates to WGSL that reads back (each pass's `.vsh` before its `.fsh`). Skipped when the +/// variable isn't set. Distant Horizons, Colorwheel and Voxy programs are +/// left out: Iris loads them only with those mods. +#[test] +fn real_packs_translate() { + let Some(dir) = std::env::var_os("HORIZON_TEST_SHADERPACKS") else { + return; + }; + // Off by default (`program.*/shadowcomp.enabled` in shaders.properties). + let disabled = ["shadowcomp.csh"]; + let mut failures = Vec::new(); + for entry in std::fs::read_dir(dir).unwrap().flatten() { + let Ok(pack) = JavaPack::open(&entry.path()) else { + continue; + }; + let values = pack.values(&BTreeMap::new()); + let mut passes: BTreeMap<&str, Vec<&String>> = BTreeMap::new(); + for p in &pack.programs { + let file = p.rsplit('/').next().unwrap(); + if ["dh_", "clrwl_", "voxy"] + .iter() + .any(|m| file.starts_with(m)) + || disabled.contains(&file) + { + continue; + } + passes + .entry(p.rsplit_once('.').unwrap().0) + .or_default() + .push(p); + } + let (mut ok, mut total) = (0, 0); + for programs in passes.values_mut() { + programs.sort_by_key(|p| !p.ends_with(".vsh")); + let mut link = Link::default(); + for p in programs.iter() { + total += 1; + match pack + .translate(p, &values, &env(), &mut link) + .and_then(|t| reparse(&t.wgsl)) + { + Ok(()) => ok += 1, + Err(e) => { + failures.push(format!("{}: {e}", entry.file_name().to_string_lossy())) + } + } + } + } + println!( + "{}: {ok}/{total} programs translated", + entry.file_name().to_string_lossy() + ); + } + assert!(failures.is_empty(), "{}", failures.join("\n\n")); +} diff --git a/docs/shader-packs.md b/docs/shader-packs.md index 025e28894..3374831aa 100644 --- a/docs/shader-packs.md +++ b/docs/shader-packs.md @@ -17,6 +17,7 @@ Both are off by default. With no pack selected the renderer draws exactly what i | Horizon shader packs: `deferred`, `gbuffer`, `shadow` stages, custom textures | planned | | Java shader packs: preprocessing + GLSL parse coverage | spike done: 450 of 470 programs reach valid WGSL (below) | | Java shader packs: preprocessor (`crates/horizon-shaderpack`: includes, options, Iris's macros) | done: every program of the three test packs preprocesses | +| Java shader packs: GLSL → WGSL (`horizon_shaderpack::translate`: syntax-tree legaliser, naga) | done: the 464 programs the three test packs enable by default translate (below) | | Java shader packs: Iris pipeline emulation | planned | ## How the renderer draws a frame today @@ -305,6 +306,12 @@ What is left, by cause: | 6 | `writeonly image2D` without a format (`shadowcomp.csh` in Complementary and BSL) | take the format from the pack's image declaration (`shaders.properties` / `const int` format) | | 5 | naga GLSL front-end bug: a call result inside a constructor (`vec3(TAAJitter(..), z)`) leaves an unvisited expression (BSL) | spill call results to temporaries; report to naga | +The translator in `crates/horizon-shaderpack` now passes all of them that the packs enable by default (464 of 464, +the WGSL read back and validated): matrix varyings are split into columns; the five BSL failures were not a naga bug +but dead code after a `discard;` (how BSL turns its clouds and weather off), which the legaliser drops; the six +`shadowcomp.csh` are off by default (`program.*/shadowcomp.enabled` in `shaders.properties`) and use images and arrays +that only exist with their options on, so they wait for `shaders.properties`. + So the GLSL side is feasible: naga's front end plus a legaliser reaches 96% on the three packs, and every remaining failure has a mechanical fix. Valid WGSL isn't a working pack yet: the output still has to be bound to Horizon's pipeline (render targets, the uniforms' values, vertex attributes for `gbuffers_*`), which is the emulation work. From 38a9f6543f8e8ed8c00a844fc64751f507ab6b0e Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 22:22:46 +0200 Subject: [PATCH 03/97] horizon-shaderpack: read shaders.properties like Iris Java .properties parsing, the two reads Iris makes (the raw file for screens, sliders and profiles; the preprocessed one, with enabled options and the environment's macros defined, for everything else), option screens, profiles and the current one, program.*.enabled through a port of Iris's BooleanParser, image formats for shadowcomp's writeonly images, and blend overrides. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-shaderpack/src/lib.rs | 23 +- crates/horizon-shaderpack/src/properties.rs | 783 +++++++++++++++++++ crates/horizon-shaderpack/tests/pack.rs | 55 ++ crates/horizon-shaderpack/tests/translate.rs | 12 +- docs/shader-packs.md | 8 + 5 files changed, 876 insertions(+), 5 deletions(-) create mode 100644 crates/horizon-shaderpack/src/properties.rs diff --git a/crates/horizon-shaderpack/src/lib.rs b/crates/horizon-shaderpack/src/lib.rs index 18a69d635..d65f4ee39 100644 --- a/crates/horizon-shaderpack/src/lib.rs +++ b/crates/horizon-shaderpack/src/lib.rs @@ -2,17 +2,20 @@ //! "Java shader packs"). This crate does the part that needs no GPU: open a //! pack, find its programs and options, and turn a program into plain GLSL //! the way Iris does before compiling it (`#include`s inlined, the player's -//! option values written into the source, Iris's macros expanded). +//! option values written into the source, Iris's macros expanded), read its +//! `shaders.properties`, and translate programs to WGSL. pub mod include; pub mod options; pub mod preprocess; +pub mod properties; pub mod source; pub mod translate; use include::{IncludeGraph, Inlined}; use options::{Annotated, OptionSet, OptionValues}; use preprocess::{Environment, Preprocessed}; +use properties::ShaderProperties; use source::PackFiles; use std::{ collections::{BTreeMap, BTreeSet}, @@ -137,4 +140,22 @@ impl JavaPack { translate::translate(stage, &pre.text, link, &|line| source.locate(line)) .map_err(|e| format!("{program}: {e}")) } + + /// The pack's `shaders.properties` with `values` (empty when it has none). + pub fn properties( + &self, + values: &OptionValues, + env: &Environment, + ) -> Result { + match self.files.read("shaders.properties") { + Some(text) => ShaderProperties::new(&text, env, &self.options, values), + None => Ok(ShaderProperties::default()), + } + } +} + +/// A program's name in `shaders.properties` (`world-1/composite1.fsh` is +/// `world-1/composite1`). +pub fn program_name(program: &str) -> &str { + program.rsplit_once('.').map_or(program, |(name, _)| name) } diff --git a/crates/horizon-shaderpack/src/properties.rs b/crates/horizon-shaderpack/src/properties.rs new file mode 100644 index 000000000..cc3069620 --- /dev/null +++ b/crates/horizon-shaderpack/src/properties.rs @@ -0,0 +1,783 @@ +//! `shaders.properties`, read the way Iris reads it (`ShaderProperties`, +//! `PropertiesPreprocessor`): the option screens, sliders, profiles and +//! feature flags come from the file as written; everything else from the file +//! after the C preprocessor, run with Iris's macros and the player's options +//! (an enabled boolean option is defined as `1`, a value option as its value), +//! so `#if` blocks pick the directives that apply. + +use crate::options::{OptionSet, OptionValues}; +use crate::preprocess::Environment; +use pp_rs::{pp::Preprocessor, token::TokenValue}; +use std::collections::BTreeMap; + +/// A Java `.properties` file: keys in the order they first appear, the last +/// value of a repeated key kept (Iris's `OrderBackedProperties`). +#[derive(Debug, Default, Clone)] +pub struct Properties(Vec<(String, String)>); + +impl Properties { + /// `java.util.Properties.load`: `#`/`!` comment lines, `key=value`, + /// `key:value` or `key value`, backslash continuations and escapes. + pub fn parse(text: &str) -> Self { + let mut out = Self::default(); + let mut lines = text.lines(); + while let Some(first) = lines.next() { + let mut line = first.trim_start().to_owned(); + if line.is_empty() || line.starts_with('#') || line.starts_with('!') { + continue; + } + while ends_with_odd_backslashes(&line) { + line.pop(); + match lines.next() { + Some(next) => line.push_str(next.trim_start()), + None => break, + } + } + let (key, value) = split_entry(&line); + out.set(unescape(&key), unescape(&value)); + } + out + } + + fn set(&mut self, key: String, value: String) { + match self.0.iter_mut().find(|(k, _)| *k == key) { + Some(entry) => entry.1 = value, + None => self.0.push((key, value)), + } + } + + pub fn get(&self, key: &str) -> Option<&str> { + self.0 + .iter() + .find(|(k, _)| k == key) + .map(|(_, v)| v.as_str()) + } + + pub fn iter(&self) -> impl Iterator { + self.0.iter().map(|(k, v)| (k.as_str(), v.as_str())) + } +} + +fn ends_with_odd_backslashes(s: &str) -> bool { + s.chars().rev().take_while(|&c| c == '\\').count() % 2 == 1 +} + +/// The key ends at the first unescaped `=`, `:` or whitespace; then +/// whitespace and at most one `=` or `:` separate it from the value. +fn split_entry(line: &str) -> (String, String) { + let chars: Vec = line.chars().collect(); + let mut i = 0; + while i < chars.len() { + match chars[i] { + '\\' => i += 2, + '=' | ':' => break, + c if c.is_whitespace() => break, + _ => i += 1, + } + } + let i = i.min(chars.len()); + let key: String = chars[..i].iter().collect(); + let mut j = i; + while j < chars.len() && chars[j].is_whitespace() { + j += 1; + } + if j < chars.len() && (chars[j] == '=' || chars[j] == ':') { + j += 1; + } + while j < chars.len() && chars[j].is_whitespace() { + j += 1; + } + (key, chars[j..].iter().collect()) +} + +fn unescape(s: &str) -> String { + let mut out = String::with_capacity(s.len()); + let mut chars = s.chars(); + while let Some(c) = chars.next() { + if c != '\\' { + out.push(c); + continue; + } + match chars.next() { + Some('t') => out.push('\t'), + Some('n') => out.push('\n'), + Some('r') => out.push('\r'), + Some('f') => out.push('\u{c}'), + Some('u') => { + let hex: String = chars.by_ref().take(4).collect(); + out.extend(u32::from_str_radix(&hex, 16).ok().and_then(char::from_u32)); + } + Some(other) => out.push(other), + None => {} + } + } + out +} + +/// The directives the C preprocessor knows: other `#` lines are comments. +const DIRECTIVES: &[&str] = &[ + "#define", "#undef", "#ifdef", "#ifndef", "#if", "#elif", "#else", "#endif", "#error", + "#warning", "#pragma", "#line", "#include", +]; + +/// `PropertiesPreprocessor.preprocessSource`: comment lines dropped, other +/// `#`s removed, then the macros expanded. Tokens keep their source spelling +/// and spacing (`0.50` stays `0.50`, `` stays one word). +pub fn preprocess( + text: &str, + env: &Environment, + options: &OptionSet, + values: &OptionValues, +) -> Result { + let filtered: String = text + .lines() + .map(str::trim) + .filter(|l| !l.is_empty()) + .map(|l| match l.starts_with('#') { + // JCPP lets a #define replace a macro (Complementary sets + // COLORED_LIGHTING 0 on macOS); pp-rs refuses, so undefine first. + true if l.starts_with("#define") => { + match l["#define".len()..].split_whitespace().next() { + Some(name) => format!("#undef {}\n{l}", name.split('(').next().unwrap_or(name)), + None => l.to_owned(), + } + } + true if DIRECTIVES.iter().any(|d| l.starts_with(d)) => l.to_owned(), + true => String::new(), + false => l.replace('#', ""), + }) + .map(|l| l + "\n") + .collect(); + let mut pp = Preprocessor::new(&filtered); + let mut define = |name: &str, value: &str| { + pp.add_define(name, value) + .map_err(|e| format!("{name}: {:?}", e.0)) + }; + for (name, o) in &options.bools { + if values.bools.get(name).copied().unwrap_or(o.default) { + define(name, "1")?; + } + } + for (name, value) in &env.defines { + define(name, if value.is_empty() { "1" } else { value })?; + } + for (name, o) in &options.strings { + define(name, values.strings.get(name).unwrap_or(&o.default))?; + } + let line_starts: Vec = std::iter::once(0) + .chain(filtered.match_indices('\n').map(|(i, _)| i + 1)) + .collect(); + let line_of = |offset: u32| line_starts.partition_point(|&s| s <= offset as usize); + let mut out = String::new(); + let (mut line, mut last_end) = (0, None::); + for item in pp { + let token = + item.map_err(|(e, at)| format!("shaders.properties:{}: {e:?}", line_of(at.start)))?; + let (start, end) = (token.location.start, token.location.end); + let at = line_of(start); + if at > line { + if line > 0 { + out.push('\n'); + } + line = at; + } else if last_end != Some(start) { + out.push(' '); + } + last_end = Some(end); + let source = filtered.get(start as usize..end as usize).unwrap_or(""); + out.push_str(&match token.value { + TokenValue::Ident(s) => s, + TokenValue::Integer(i) if source.parse::().ok() == Some(i.value) => { + source.to_owned() + } + TokenValue::Integer(i) => i.value.to_string(), + TokenValue::Float(f) + if source.trim_end_matches(['f', 'F']).parse::().ok() == Some(f.value) => + { + source.to_owned() + } + TokenValue::Float(f) => f.value.to_string(), + TokenValue::Punct(_) => source.to_owned(), + TokenValue::Version(_) | TokenValue::Extension(_) | TokenValue::Pragma(_) => continue, + }); + } + out.push('\n'); + Ok(out) +} + +/// One element of an option screen (`OptionMenuElement`). +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum MenuElement { + /// ``: a blank cell. + Empty, + /// ``: the profile button (only when the pack has profiles). + Profile, + /// `*`: every option no screen lists. + Rest, + /// `[NAME]`: a button to sub-screen `screen.NAME`. + Screen(String), + /// An option, by name. A name the source doesn't define is shown as an + /// empty cell, as Iris does (packs keep removed options on screens). + Option(String), +} + +impl MenuElement { + fn parse(word: &str) -> Self { + match word { + "" => Self::Empty, + "" => Self::Profile, + "*" => Self::Rest, + w if w.starts_with('[') && w.ends_with(']') && w.len() > 1 => { + Self::Screen(w[1..w.len() - 1].to_owned()) + } + w => Self::Option(w.to_owned()), + } + } +} + +/// A named set of option values (`profile.LOW = SHADOWS:1 !BLOOM`). +#[derive(Clone, Debug, PartialEq)] +pub struct Profile { + pub name: String, + pub values: BTreeMap, + pub disabled_programs: Vec, +} + +impl Profile { + /// Whether the current values are this profile's (`Profile.matches`). + pub fn matches(&self, options: &OptionSet, values: &OptionValues) -> bool { + self.values.iter().all(|(name, value)| { + if let Some(o) = options.bools.get(name) { + let on = values.bools.get(name).copied().unwrap_or(o.default); + return on.to_string() == *value; + } + if let Some(o) = options.strings.get(name) { + return values.strings.get(name).unwrap_or(&o.default) == value; + } + true + }) + } +} + +/// A blend override: `off`, or the four GL factors (source and +/// destination colour, then alpha), as written (`SRC_ALPHA`, ...). +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Blend { + Off, + Factors([String; 4]), +} + +/// The GL blend factors packs may name (`BlendModeFunction`). +pub const BLEND_FACTORS: &[&str] = &[ + "ZERO", + "ONE", + "SRC_COLOR", + "ONE_MINUS_SRC_COLOR", + "DST_COLOR", + "ONE_MINUS_DST_COLOR", + "SRC_ALPHA", + "ONE_MINUS_SRC_ALPHA", + "DST_ALPHA", + "ONE_MINUS_DST_ALPHA", + "SRC_ALPHA_SATURATE", +]; + +/// Colour targets by their pre-`colortex` names (`gcolor` is `colortex0`). +pub const LEGACY_TARGETS: &[&str] = &[ + "gcolor", + "gdepth", + "gnormal", + "composite", + "gaux1", + "gaux2", + "gaux3", + "gaux4", +]; + +fn parse_blend(value: &str) -> Result { + if value == "off" { + return Ok(Blend::Off); + } + let words: Vec<&str> = value.split(' ').collect(); + match words.as_slice() { + [a, b, c, d] if words.iter().all(|w| BLEND_FACTORS.contains(w)) => { + Ok(Blend::Factors([a, b, c, d].map(|w| w.to_string()))) + } + _ => Err(format!("invalid blend mode `{value}`")), + } +} + +/// A pack's `shaders.properties`. +#[derive(Debug, Default)] +pub struct ShaderProperties { + /// As written: screens, sliders, profiles, feature flags. + pub original: Properties, + /// After the preprocessor: every other directive. + pub preprocessed: Properties, +} + +impl ShaderProperties { + pub fn new( + text: &str, + env: &Environment, + options: &OptionSet, + values: &OptionValues, + ) -> Result { + Ok(Self { + original: Properties::parse(text), + preprocessed: Properties::parse(&preprocess(text, env, options, values)?), + }) + } + + /// The main screen (`screen`, `None`) or sub-screen `screen.NAME`; the + /// main screen defaults to `*`. + pub fn screen(&self, name: Option<&str>) -> Option> { + let value = match name { + None => self.original.get("screen").or(Some("*")), + Some(n) => self.original.get(&format!("screen.{n}")), + }?; + Some(value.split_whitespace().map(MenuElement::parse).collect()) + } + + /// The sub-screens, by name. + pub fn screens(&self) -> Vec<&str> { + self.original + .iter() + .filter_map(|(k, _)| k.strip_prefix("screen.")) + .filter(|n| !n.ends_with(".columns") && *n != "columns") + .collect() + } + + /// A screen's column count (`screen.columns`, `screen.NAME.columns`). + pub fn columns(&self, name: Option<&str>) -> Option { + let key = name.map_or("screen.columns".to_owned(), |n| { + format!("screen.{n}.columns") + }); + self.original.get(&key)?.trim().parse().ok() + } + + /// Value options shown as sliders rather than buttons. + pub fn sliders(&self) -> Vec<&str> { + self.original + .get("sliders") + .map(|v| v.split_whitespace().collect()) + .unwrap_or_default() + } + + /// `iris.features.required` and `iris.features.optional`. + pub fn features(&self) -> (Vec<&str>, Vec<&str>) { + let list = |k: &str| { + self.original + .get(k) + .map(|v| v.split_whitespace().collect()) + .unwrap_or_default() + }; + ( + list("iris.features.required"), + list("iris.features.optional"), + ) + } + + /// The profiles in file order, `profile.OTHER` references expanded + /// (`ProfileSet.fromTree`). + pub fn profiles(&self, options: &OptionSet) -> Result, String> { + let tree: Vec<(&str, Vec<&str>)> = self + .original + .iter() + .filter_map(|(k, v)| { + Some((k.strip_prefix("profile.")?, v.split_whitespace().collect())) + }) + .collect(); + fn build( + name: &str, + parents: &mut Vec, + tree: &[(&str, Vec<&str>)], + options: &OptionSet, + out: &mut Profile, + ) -> Result<(), String> { + let words = &tree + .iter() + .find(|(n, _)| *n == name) + .ok_or(format!("profile {name} does not exist"))? + .1; + for &w in words { + if let Some(program) = w.strip_prefix("!program.") { + out.disabled_programs.push(program.to_owned()); + } else if let Some(other) = w.strip_prefix("profile.") { + if parents.iter().any(|p| p == other) { + return Err(format!( + "profile {name} includes itself through {}", + parents.join(", ") + )); + } + parents.push(other.to_owned()); + build(other, parents, tree, options, out)?; + } else if let Some(option) = w.strip_prefix('!') { + out.values.insert(option.to_owned(), "false".to_owned()); + } else if let Some((option, value)) = + w.split_once('=').or_else(|| w.split_once(':')) + { + out.values.insert(option.to_owned(), value.to_owned()); + } else if options.bools.contains_key(w) { + out.values.insert(w.to_owned(), "true".to_owned()); + } + } + Ok(()) + } + tree.iter() + .map(|(name, _)| { + let mut p = Profile { + name: name.to_string(), + values: BTreeMap::new(), + disabled_programs: Vec::new(), + }; + build(name, &mut Vec::new(), &tree, options, &mut p)?; + Ok(p) + }) + .collect() + } + + /// The profile the values match, trying those that set the most options + /// first (`ProfileSet.scan`). + pub fn current_profile<'p>( + profiles: &'p [Profile], + options: &OptionSet, + values: &OptionValues, + ) -> Option<&'p Profile> { + let mut sorted: Vec<&Profile> = profiles.iter().collect(); + sorted.sort_by_key(|p| std::cmp::Reverse(p.values.len())); + sorted.into_iter().find(|p| p.matches(options, values)) + } + + /// Whether `program` (`composite1`, `world-1/shadowcomp`) runs: not + /// turned off by `program..enabled` or by the current profile. + pub fn program_enabled( + &self, + program: &str, + options: &OptionSet, + values: &OptionValues, + ) -> bool { + if let Ok(profiles) = self.profiles(options) { + if let Some(p) = Self::current_profile(&profiles, options, values) { + if p.disabled_programs.iter().any(|d| d == program) { + return false; + } + } + } + match self.preprocessed.get(&format!("program.{program}.enabled")) { + Some(expr) => boolean_expression(expr, options, values), + None => true, + } + } + + /// `image. = ...`: each + /// custom image's storage format (`rgba16f`), for [`crate::translate::Link`]. + pub fn image_formats(&self) -> BTreeMap { + self.preprocessed + .iter() + .filter_map(|(k, v)| { + Some(( + k.strip_prefix("image.")?.to_owned(), + v.split(' ').nth(2)?.to_lowercase(), + )) + }) + .collect() + } + + /// `blend.`: the program's blend override. + pub fn blend(&self, program: &str) -> Option> { + self.preprocessed + .get(&format!("blend.{program}")) + .map(parse_blend) + } + + /// `blend..`: per colour target overrides, by target index. + pub fn buffer_blends(&self, program: &str) -> Vec<(u32, Result)> { + let prefix = format!("blend.{program}."); + self.preprocessed + .iter() + .filter_map(|(k, v)| { + let buffer = k.strip_prefix(&prefix)?; + let index = match LEGACY_TARGETS.iter().position(|t| *t == buffer) { + Some(i) => i as u32, + None => buffer.strip_prefix("colortex")?.parse().ok()?, + }; + Some((index, parse_blend(v))) + }) + .collect() + } +} + +/// Iris's `BooleanParser`: options joined by `!`, `&&`, `||` and brackets, +/// `&&` binding tighter. `true`/`1` and `false`/`0` are literals; a name that +/// isn't a boolean option is true; a malformed expression is true. +pub fn boolean_expression(expr: &str, options: &OptionSet, values: &OptionValues) -> bool { + #[derive(Clone, Copy, PartialEq)] + enum Op { + And, + Or, + Not, + Open, + } + let value_of = |name: &str| match name { + "true" | "1" => true, + "false" | "0" => false, + n => match options.bools.get(n) { + Some(o) => values.bools.get(n).copied().unwrap_or(o.default), + None => true, + }, + }; + fn push_value(name: &mut String, ops: &mut Vec, vals: &mut Vec, value: bool) { + name.clear(); + if ops.last() == Some(&Op::Not) { + ops.pop(); + vals.push(!value); + } else { + vals.push(value); + } + } + fn evaluate(ops: &mut Vec, vals: &mut Vec, bracket: bool) -> Option<()> { + let mut value = vals.pop()?; + while let Some(&op) = ops.last() { + if bracket && op == Op::Open { + break; + } + ops.pop(); + value = match op { + Op::And => vals.pop()? && value, + Op::Or => vals.pop()? || value, + Op::Not => !value, + Op::Open => value, + }; + } + if ops.last() == Some(&Op::Open) { + ops.pop(); + if ops.last() == Some(&Op::Not) { + ops.pop(); + value = !value; + } + } + vals.push(value); + Some(()) + } + let run = || -> Option { + let (mut ops, mut vals, mut name) = (Vec::new(), Vec::new(), String::new()); + let chars: Vec = expr.chars().collect(); + let mut i = 0; + while i < chars.len() { + match chars[i] { + '!' => ops.push(Op::Not), + c @ ('&' | '|') => { + if !name.is_empty() { + let v = value_of(&name); + push_value(&mut name, &mut ops, &mut vals, v); + } + if c == '&' { + if ops.last() != Some(&Op::And) { + ops.push(Op::Open); + } + ops.push(Op::And); + } else { + if ops.last() == Some(&Op::And) { + evaluate(&mut ops, &mut vals, true)?; + } + ops.push(Op::Or); + } + i += 1; + } + '(' => ops.push(Op::Open), + ')' => { + if !name.is_empty() { + let v = value_of(&name); + push_value(&mut name, &mut ops, &mut vals, v); + } + if ops.last() == Some(&Op::And) { + evaluate(&mut ops, &mut vals, true)?; + } + evaluate(&mut ops, &mut vals, true)?; + } + ' ' => {} + c => name.push(c), + } + i += 1; + } + if !name.is_empty() { + let v = value_of(&name); + push_value(&mut name, &mut ops, &mut vals, v); + } + evaluate(&mut ops, &mut vals, false)?; + let result = vals.pop()?; + (vals.is_empty() && ops.is_empty()).then_some(result) + }; + run().unwrap_or(true) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::options::{Annotated, OptionSet}; + + fn options(lines: &[&str]) -> OptionSet { + let lines: Vec = lines.iter().map(|l| l.to_string()).collect(); + let a = Annotated::parse(&lines); + let mut set = OptionSet::default(); + a.bools.values().for_each(|o| set.add_bool(o)); + a.strings.values().for_each(|o| set.add_string(o)); + set + } + + #[test] + fn java_properties_syntax() { + let p = Properties::parse( + "# comment\n! also\n a = 1\nb:2\nc 3\nlong = x \\\n y\nd=\\u0041\\\\z\na=last\nempty\n", + ); + let all: Vec<(&str, &str)> = p.iter().collect(); + assert_eq!( + all, + [ + ("a", "last"), + ("b", "2"), + ("c", "3"), + ("long", "x y"), + ("d", "A\\z"), + ("empty", "") + ] + ); + } + + #[test] + fn boolean_expressions() { + let set = options(&["#define A", "//#define B", "#define C"]); + let values = OptionValues::default(); + let eval = |e: &str| boolean_expression(e, &set, &values); + assert!(eval("A") && !eval("B") && !eval("!A") && eval("!B")); + assert!(!eval("A && B") && eval("A || B") && eval("B || C")); + assert!(eval("A && B || C"), "&& binds tighter"); + assert!(!eval("B || C && B")); + assert!(!eval("!(A && C)") && eval("!(A && B)") && eval("A && !B")); + assert!(eval("(B || C) && A")); + assert!(eval("UNKNOWN") && eval("1") && !eval("0") && !eval("false")); + assert!(eval("A &&"), "malformed: true"); + let mut on = OptionValues::default(); + on.bools.insert("B".into(), true); + assert!(boolean_expression("A && B", &set, &on)); + } + + #[test] + fn preprocessed_and_original_reads() { + let set = options(&["//#define COLORED", "#define QUALITY 2 // [1 2 3]"]); + let text = "\ +screen = QUALITY [LIGHT] * +screen.LIGHT = COLORED +screen.LIGHT.columns = 1 +sliders = QUALITY +profile.LOW = QUALITY:1 !COLORED !program.composite2 +profile.HIGH = profile.LOW QUALITY:3 COLORED +#ifdef MC_OS_MAC +#define QUALITY 5 +#endif +#if QUALITY >= 5 && defined MC_OS_MAC + program.composite1.enabled = false +#else + program.composite1.enabled = true +#endif +program.world0/shadowcomp.enabled = COLORED +blend.gbuffers_water = SRC_ALPHA ONE_MINUS_SRC_ALPHA ONE ZERO +blend.gbuffers_water.gaux2 = off +blend.gbuffers_water.colortex9 = ONE ONE ONE ONE +size.buffer.colortex4 = 0.50 QUALITY +image.lightimg0 = lighttex0 rgba RGBA16F half_float false false 128 64 128 +# a comment, not a directive +iris.features.optional = CUSTOM_IMAGES SSBO +"; + let mut env = Environment { + defines: Vec::new(), + }; + env.define("MC_OS_MAC", ""); + let values = OptionValues::default(); + let p = ShaderProperties::new(text, &env, &set, &values).unwrap(); + use MenuElement::*; + assert_eq!( + p.screen(None).unwrap(), + [ + Option("QUALITY".into()), + Empty, + Screen("LIGHT".into()), + Profile, + Rest + ] + ); + assert_eq!(p.screens(), ["LIGHT"]); + assert_eq!((p.columns(None), p.columns(Some("LIGHT"))), (None, Some(1))); + assert_eq!(p.sliders(), ["QUALITY"]); + assert_eq!( + p.image_formats(), + [("lightimg0".to_owned(), "rgba16f".to_owned())].into() + ); + assert_eq!(p.features(), (vec![], vec!["CUSTOM_IMAGES", "SSBO"])); + // The preprocessed read: #if picked, values expanded, spelling kept. + assert!(!p.program_enabled("composite1", &set, &values)); + assert!(!p.program_enabled("world0/shadowcomp", &set, &values)); + assert_eq!( + p.preprocessed.get("size.buffer.colortex4"), + Some("0.50 5"), + "QUALITY redefined on macOS" + ); + assert_eq!( + p.blend("gbuffers_water"), + Some(Ok(Blend::Factors( + ["SRC_ALPHA", "ONE_MINUS_SRC_ALPHA", "ONE", "ZERO"].map(String::from) + ))) + ); + assert_eq!( + p.buffer_blends("gbuffers_water"), + [ + (5, Ok(Blend::Off)), + (9, Ok(Blend::Factors(["ONE"; 4].map(String::from)))) + ] + ); + + let profiles = p.profiles(&set).unwrap(); + assert_eq!( + profiles[1].values.get("QUALITY").map(String::as_str), + Some("3") + ); + assert_eq!(profiles[1].disabled_programs, ["composite2"]); + // Defaults (QUALITY 2) match no profile; LOW's values match LOW, which + // also turns composite2 off. + assert_eq!( + ShaderProperties::current_profile(&profiles, &set, &values), + None + ); + let low = OptionValues::new(&set, &[("QUALITY".to_owned(), "1".to_owned())].into()); + assert_eq!( + ShaderProperties::current_profile(&profiles, &set, &low).map(|p| p.name.as_str()), + Some("LOW") + ); + assert!( + !p.program_enabled("composite2", &set, &low) + && p.program_enabled("composite2", &set, &values) + ); + + // With COLORED on and QUALITY 1, the other branches apply. + let high = OptionValues::new( + &set, + &[ + ("COLORED".to_owned(), "true".to_owned()), + ("QUALITY".to_owned(), "1".to_owned()), + ] + .into(), + ); + let p = ShaderProperties::new( + text, + &Environment { + defines: Vec::new(), + }, + &set, + &high, + ) + .unwrap(); + assert!(p.program_enabled("composite1", &set, &high)); + assert!(p.program_enabled("world0/shadowcomp", &set, &high)); + } +} diff --git a/crates/horizon-shaderpack/tests/pack.rs b/crates/horizon-shaderpack/tests/pack.rs index a21f48fde..4a7d4959e 100644 --- a/crates/horizon-shaderpack/tests/pack.rs +++ b/crates/horizon-shaderpack/tests/pack.rs @@ -221,3 +221,58 @@ fn real_packs_preprocess() { } assert!(failures.is_empty(), "{}", failures.join("\n")); } + +/// The `shaders.properties` of the packs in `HORIZON_TEST_SHADERPACKS` read +/// with their defaults. Skipped when the variable isn't set. +#[test] +fn real_packs_properties() { + let Some(dir) = std::env::var_os("HORIZON_TEST_SHADERPACKS") else { + return; + }; + for entry in std::fs::read_dir(dir).unwrap().flatten() { + let Ok(pack) = JavaPack::open(&entry.path()) else { + continue; + }; + let values = pack.values(&BTreeMap::new()); + let properties = pack.properties(&values, &env()).unwrap(); + let profiles = properties.profiles(&pack.options).unwrap(); + let current = horizon_shaderpack::properties::ShaderProperties::current_profile( + &profiles, + &pack.options, + &values, + ); + let disabled: std::collections::BTreeSet<&str> = pack + .programs + .iter() + .map(|p| horizon_shaderpack::program_name(p)) + .filter(|p| !properties.program_enabled(p, &pack.options, &values)) + .collect(); + let main = properties.screen(None).unwrap(); + println!( + "{}: {} main screen elements, {} sub-screens, {} sliders, profiles {:?} (current {:?}), off: {}", + entry.file_name().to_string_lossy(), + main.len(), + properties.screens().len(), + properties.sliders().len(), + profiles.iter().map(|p| p.name.as_str()).collect::>(), + current.map(|p| p.name.as_str()), + disabled.into_iter().collect::>().join(" ") + ); + // Screens name the pack's options (a few stale names are tolerated: + // Iris shows them as empty cells). + let mut unknown = Vec::new(); + for screen in std::iter::once(None).chain(properties.screens().into_iter().map(Some)) { + for e in properties.screen(screen).unwrap() { + if let horizon_shaderpack::properties::MenuElement::Option(o) = e { + if !pack.options.bools.contains_key(&o) + && !pack.options.strings.contains_key(&o) + { + unknown.push(o); + } + } + } + } + println!(" unknown screen options: {unknown:?}"); + assert!(unknown.len() <= 2, "{unknown:?}"); + } +} diff --git a/crates/horizon-shaderpack/tests/translate.rs b/crates/horizon-shaderpack/tests/translate.rs index 862fb375e..3e58166c8 100644 --- a/crates/horizon-shaderpack/tests/translate.rs +++ b/crates/horizon-shaderpack/tests/translate.rs @@ -6,6 +6,7 @@ use horizon_shaderpack::{ JavaPack, Stage, include::Inlined, preprocess::{Environment, preprocess}, + program_name, translate::{Link, Translated, translate}, }; use std::collections::{BTreeMap, BTreeSet}; @@ -295,6 +296,7 @@ fn reparse(wgsl: &str) -> Result<(), String> { } /// Every program the packs in `HORIZON_TEST_SHADERPACKS` enable by default +/// (`program.*.enabled` in their shaders.properties) /// translates to WGSL that reads back (each pass's `.vsh` before its `.fsh`). Skipped when the /// variable isn't set. Distant Horizons, Colorwheel and Voxy programs are /// left out: Iris loads them only with those mods. @@ -303,21 +305,20 @@ fn real_packs_translate() { let Some(dir) = std::env::var_os("HORIZON_TEST_SHADERPACKS") else { return; }; - // Off by default (`program.*/shadowcomp.enabled` in shaders.properties). - let disabled = ["shadowcomp.csh"]; let mut failures = Vec::new(); for entry in std::fs::read_dir(dir).unwrap().flatten() { let Ok(pack) = JavaPack::open(&entry.path()) else { continue; }; let values = pack.values(&BTreeMap::new()); + let properties = pack.properties(&values, &env()).unwrap(); let mut passes: BTreeMap<&str, Vec<&String>> = BTreeMap::new(); for p in &pack.programs { let file = p.rsplit('/').next().unwrap(); if ["dh_", "clrwl_", "voxy"] .iter() .any(|m| file.starts_with(m)) - || disabled.contains(&file) + || !properties.program_enabled(program_name(p), &pack.options, &values) { continue; } @@ -329,7 +330,10 @@ fn real_packs_translate() { let (mut ok, mut total) = (0, 0); for programs in passes.values_mut() { programs.sort_by_key(|p| !p.ends_with(".vsh")); - let mut link = Link::default(); + let mut link = Link { + image_formats: properties.image_formats(), + ..Link::default() + }; for p in programs.iter() { total += 1; match pack diff --git a/docs/shader-packs.md b/docs/shader-packs.md index 3374831aa..76e2e2771 100644 --- a/docs/shader-packs.md +++ b/docs/shader-packs.md @@ -18,6 +18,7 @@ Both are off by default. With no pack selected the renderer draws exactly what i | Java shader packs: preprocessing + GLSL parse coverage | spike done: 450 of 470 programs reach valid WGSL (below) | | Java shader packs: preprocessor (`crates/horizon-shaderpack`: includes, options, Iris's macros) | done: every program of the three test packs preprocesses | | Java shader packs: GLSL → WGSL (`horizon_shaderpack::translate`: syntax-tree legaliser, naga) | done: the 464 programs the three test packs enable by default translate (below) | +| Java shader packs: `shaders.properties` (`horizon_shaderpack::properties`: screens, sliders, profiles, `program.*.enabled`, image formats, blend modes) | done, no screen yet: read like Iris for the three test packs | | Java shader packs: Iris pipeline emulation | planned | ## How the renderer draws a frame today @@ -312,6 +313,13 @@ but dead code after a `discard;` (how BSL turns its clouds and weather off), whi `shadowcomp.csh` are off by default (`program.*/shadowcomp.enabled` in `shaders.properties`) and use images and arrays that only exist with their options on, so they wait for `shaders.properties`. +With `shaders.properties` read (`program.*.enabled` evaluated like Iris, image formats taken from `image.*`), the +programs the packs enable translate: 180 of 180 for Complementary, 114 of 114 for Sildur's and 154 of 154 for BSL with +its profile (it turns `composite2`/`composite3` off). With their colored lighting on, BSL's `shadowcomp.csh` translates +too, and so do all 183 of Complementary's on Linux once `IRIS_FEATURE_CUSTOM_IMAGES` is defined: without it, the pack +declares no voxel images but its `shaders.properties` still turns `shadowcomp` on. Which `IRIS_FEATURE_*` Horizon +defines is decided when the stage that backs them exists. + So the GLSL side is feasible: naga's front end plus a legaliser reaches 96% on the three packs, and every remaining failure has a mechanical fix. Valid WGSL isn't a working pack yet: the output still has to be bound to Horizon's pipeline (render targets, the uniforms' values, vertex attributes for `gbuffers_*`), which is the emulation work. From e1d9ae1dd7d8d579e4b5923f85c81c2e30535c00 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 22:34:29 +0200 Subject: [PATCH 04/97] horizon-shaderpack: plan a Java pack's full-screen passes like Iris The begin, prepare, deferred, composite and final passes of a program folder: what each writes (the later of DRAWBUFFERS and RENDERTARGETS), which side of every ping-ponged colour target it reads (Iris's BufferFlipper, with flip.* overrides), mipmapped targets, scale.*, and the targets' formats, clearing and sizes from the consts of every pass in Iris's order. The preprocessor now keeps the directive comments of active code, as Iris's does, so a DRAWBUFFERS in an #if'd out branch doesn't count. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-shaderpack/src/lib.rs | 1 + crates/horizon-shaderpack/src/pipeline.rs | 640 ++++++++++++++++++ crates/horizon-shaderpack/src/preprocess.rs | 94 ++- .../fixtures/passes/shaders/composite.fsh | 17 + .../fixtures/passes/shaders/composite.vsh | 3 + .../fixtures/passes/shaders/composite1.fsh | 7 + .../fixtures/passes/shaders/composite1.vsh | 3 + .../fixtures/passes/shaders/composite2.fsh | 5 + .../fixtures/passes/shaders/composite2.vsh | 3 + .../fixtures/passes/shaders/deferred.fsh | 11 + .../fixtures/passes/shaders/deferred.vsh | 3 + .../tests/fixtures/passes/shaders/final.fsh | 6 + .../tests/fixtures/passes/shaders/final.vsh | 3 + .../passes/shaders/shaders.properties | 6 + crates/horizon-shaderpack/tests/pack.rs | 96 ++- docs/shader-packs.md | 1 + 16 files changed, 897 insertions(+), 2 deletions(-) create mode 100644 crates/horizon-shaderpack/src/pipeline.rs create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite.fsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite.vsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite1.fsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite1.vsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite2.fsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite2.vsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/deferred.fsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/deferred.vsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/final.fsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/final.vsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/shaders.properties diff --git a/crates/horizon-shaderpack/src/lib.rs b/crates/horizon-shaderpack/src/lib.rs index d65f4ee39..0cfec68f9 100644 --- a/crates/horizon-shaderpack/src/lib.rs +++ b/crates/horizon-shaderpack/src/lib.rs @@ -7,6 +7,7 @@ pub mod include; pub mod options; +pub mod pipeline; pub mod preprocess; pub mod properties; pub mod source; diff --git a/crates/horizon-shaderpack/src/pipeline.rs b/crates/horizon-shaderpack/src/pipeline.rs new file mode 100644 index 000000000..f9dc00cc5 --- /dev/null +++ b/crates/horizon-shaderpack/src/pipeline.rs @@ -0,0 +1,640 @@ +//! Iris's full-screen passes and the colour targets they draw to: which +//! programs run (`begin*`, `prepare*`, `deferred*`, `composite*`, `final`), +//! the targets each one writes (`/* DRAWBUFFERS:01 */`, `/* RENDERTARGETS: +//! 0,1 */`), the targets' formats and clearing (`const int colortex0Format +//! = RGBA16F;` and friends), and which side of each ping-ponged target a +//! pass reads (`BufferFlipper`). Read from the preprocessed sources with +//! their comments, as Iris's `ProgramDirectives`, `PackRenderTargetDirectives` +//! and `CompositeRenderer` do. + +use crate::{ + JavaPack, options::OptionValues, preprocess::Environment, properties::LEGACY_TARGETS, + properties::ShaderProperties, +}; +use std::collections::{BTreeMap, BTreeSet}; + +/// Colour targets Iris supports (`IrisLimits.MAX_COLOR_BUFFERS`). +pub const MAX_COLOR_BUFFERS: u32 = 32; + +/// The formats `const int colortexNFormat` may name (`InternalTextureFormat`). +pub const TEXTURE_FORMATS: &[&str] = &[ + "RGBA", + "R8", + "RG8", + "RGB8", + "RGBA8", + "R8_SNORM", + "RG8_SNORM", + "RGB8_SNORM", + "RGBA8_SNORM", + "R16", + "RG16", + "RGB16", + "RGBA16", + "R16_SNORM", + "RG16_SNORM", + "RGB16_SNORM", + "RGBA16_SNORM", + "R16F", + "RG16F", + "RGB16F", + "RGBA16F", + "R32F", + "RG32F", + "RGB32F", + "RGBA32F", + "R8I", + "RG8I", + "RGB8I", + "RGBA8I", + "R8UI", + "RG8UI", + "RGB8UI", + "RGBA8UI", + "R16I", + "RG16I", + "RGB16I", + "RGBA16I", + "R16UI", + "RG16UI", + "RGB16UI", + "RGBA16UI", + "R32I", + "RG32I", + "RGB32I", + "RGBA32I", + "R32UI", + "RG32UI", + "RGB32UI", + "RGBA32UI", + "RGBA2", + "RGBA4", + "R3_G3_B2", + "RGB5_A1", + "RGB565", + "RGB10_A2", + "RGB10_A2UI", + "R11F_G11F_B10F", + "RGB9_E5", +]; + +/// The value of the last `/* NAME:value */` in `text` and where it starts +/// (`CommentDirectiveParser.findDirective`). +pub fn comment_directive(text: &str, name: &str) -> Option<(usize, String)> { + let prefix = format!("{name}:"); + let at = text.rfind(&prefix)?; + if !text[..at].trim_end().ends_with("/*") { + return None; + } + let rest = &text[at + prefix.len()..]; + let end = rest.find("*/")?; + Some((at, rest[..end].trim().to_owned())) +} + +/// A `const = ;` line (`ConstDirectiveParser`). +#[derive(Clone, Debug, PartialEq)] +pub struct ConstDirective { + /// `int`, `float`, `vec2`, `ivec3`, `vec4` or `bool`. + pub ty: &'static str, + pub key: String, + pub value: String, +} + +/// Every line of `text` that starts with a `const` Iris reads, in order. +pub fn const_directives(text: &str) -> Vec { + text.lines().filter_map(const_directive).collect() +} + +fn const_directive(line: &str) -> Option { + if !line.contains("const") || !line.contains('=') || !line.contains(';') { + return None; + } + let rest = line.trim().strip_prefix("const")?; + if !rest.starts_with(char::is_whitespace) { + return None; + } + let rest = rest.trim_start(); + // In Iris's order: `ivec3` before `vec4` doesn't matter, but `int` must + // not take `ivec3`'s place, and it can't (`ivec3` starts with `i`, `v`). + let ty = ["int", "float", "vec2", "ivec3", "vec4", "bool"] + .into_iter() + .find(|t| rest.starts_with(t))?; + let rest = &rest[ty.len()..]; + if !rest.starts_with(char::is_whitespace) { + return None; + } + let (key, value) = rest.split_once('=')?; + let key = key.trim(); + if key.is_empty() || !key.chars().all(|c| c.is_alphanumeric() || c == '_') { + return None; + } + let value = value.split_once(';')?.0.trim(); + Some(ConstDirective { + ty, + key: key.to_owned(), + value: value.to_owned(), + }) +} + +/// The targets a fragment program writes, by output location: the later of +/// its `DRAWBUFFERS` and `RENDERTARGETS` directives, or `None` without one +/// (Iris then takes the `gl_FragData` indices it writes). +pub fn draw_buffers(fragment: &str) -> Result>, String> { + let draw = comment_directive(fragment, "DRAWBUFFERS"); + let render = comment_directive(fragment, "RENDERTARGETS"); + let (digits, value) = match (draw, render) { + (Some(d), Some(r)) if d.0 > r.0 => (true, d.1), + (_, Some(r)) => (false, r.1), + (Some(d), None) => (true, d.1), + (None, None) => return Ok(None), + }; + let bad = || format!("invalid render targets `{value}`"); + let targets: Vec = if digits { + value + .chars() + .map(|c| c.to_digit(10).ok_or_else(bad)) + .collect::>()? + } else { + value + .split(',') + .map(|t| t.trim().parse().map_err(|_| bad())) + .collect::>()? + }; + match targets.iter().find(|&&t| t >= MAX_COLOR_BUFFERS) { + Some(t) => Err(format!( + "render target {t} is not supported, only {MAX_COLOR_BUFFERS} are" + )), + None => Ok(Some(targets)), + } +} + +/// The index of a target name, `colortex` or a legacy one (`gaux1`). +pub fn target_index(name: &str) -> Option { + match LEGACY_TARGETS.iter().position(|t| *t == name) { + Some(i) => Some(i as u32), + None => name + .strip_prefix("colortex")? + .parse() + .ok() + .filter(|&i| i < MAX_COLOR_BUFFERS), + } +} + +/// A key naming a target and a suffix (`colortex4Format`, `gaux1Clear`). +fn target_key(key: &str, suffix: &str) -> Option { + target_index(key.strip_suffix(suffix)?) +} + +/// A colour target's settings (`RenderTargetSettings`, `size.buffer.*`). +#[derive(Clone, Debug, PartialEq)] +pub struct TargetSettings { + /// One of [`TEXTURE_FORMATS`]; `RGBA` (8 bits per channel) by default. + pub format: String, + /// Cleared every frame (the default), or kept for the next frame. + pub clear: bool, + /// Iris clears to black, or `colortex0` to the fog colour, without one. + pub clear_color: Option<[f32; 4]>, + /// `size.buffer.`: a fraction of the screen or a size in pixels. + pub size: Option, +} + +impl Default for TargetSettings { + fn default() -> Self { + Self { + format: "RGBA".into(), + clear: true, + clear_color: None, + size: None, + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum TargetSize { + Relative(f32, f32), + Pixels(u32, u32), +} + +/// Every target's settings from the consts of `fragments`, read in order so +/// the last one wins, then `size.buffer.*` from the properties. +pub fn target_settings<'a>( + fragments: impl IntoIterator, + properties: &ShaderProperties, +) -> Vec { + let mut targets = vec![TargetSettings::default(); MAX_COLOR_BUFFERS as usize]; + for fragment in fragments { + for c in const_directives(fragment) { + match c.ty { + "int" => { + if let Some(i) = target_key(&c.key, "Format") { + let format = c.value.to_uppercase(); + if TEXTURE_FORMATS.contains(&format.as_str()) { + targets[i as usize].format = format; + } + } + } + "bool" => { + if let (Some(i), Ok(clear)) = (target_key(&c.key, "Clear"), c.value.parse()) { + targets[i as usize].clear = clear; + } + } + "vec4" => { + if let (Some(i), Some(color)) = + (target_key(&c.key, "ClearColor"), parse_vec4(&c.value)) + { + targets[i as usize].clear_color = Some(color); + } + } + _ => {} + } + } + } + for (key, value) in properties.preprocessed.iter() { + let Some(i) = key.strip_prefix("size.buffer.").and_then(target_index) else { + continue; + }; + let parts: Vec<&str> = value.split_whitespace().collect(); + if let [w, h] = parts[..] { + targets[i as usize].size = if w.contains('.') || h.contains('.') { + w.parse() + .ok() + .zip(h.parse().ok()) + .map(|(w, h)| TargetSize::Relative(w, h)) + } else { + w.parse() + .ok() + .zip(h.parse().ok()) + .map(|(w, h)| TargetSize::Pixels(w, h)) + }; + } + } + targets +} + +fn parse_vec4(value: &str) -> Option<[f32; 4]> { + let args = value.strip_prefix("vec4")?.trim(); + let args = args.strip_prefix('(')?.strip_suffix(')')?; + let parts: Vec = args + .split(',') + .map(|p| p.trim().parse().ok()) + .collect::>()?; + parts.try_into().ok() +} + +/// One full-screen pass. +#[derive(Clone, Debug, PartialEq)] +pub struct Pass { + /// The program's path without extension (`world0/composite1`). + pub program: String, + /// What it writes, by output location; `None`: the outputs it writes. + pub draw_buffers: Option>, + /// Targets it reads from their alternate side (it writes the other side). + pub reads_alt: BTreeSet, + /// Targets whose mipmaps it wants generated before it runs. + pub mipmapped: BTreeSet, + /// `scale.`: the fraction of the screen it draws. + pub scale: f32, +} + +/// The full-screen passes of one program folder, in order, and the +/// flipping state between them. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Passes { + pub targets: Vec, + pub begin: Vec, + pub prepare: Vec, + /// The targets gbuffers programs (and Horizon's world) write on their + /// alternate side, before and after translucent geometry. + pub flipped_after_prepare: BTreeSet, + pub deferred: Vec, + pub flipped_after_translucent: BTreeSet, + pub composite: Vec, + /// `final`, which writes the screen; `None` without one (Iris then + /// copies `colortex0` to the screen). + pub final_pass: Option, + /// After `final`, these targets' alternate side is copied back so the + /// next frame starts unflipped. + pub flipped_at_end: BTreeSet, +} + +/// Iris's single programs (`ProgramId`), in order. +const SINGLE_PROGRAMS: &[&str] = &[ + "shadow", + "shadow_solid", + "shadow_cutout", + "shadow_water", + "shadow_entities", + "shadow_lightning", + "shadow_block", + "gbuffers_basic", + "gbuffers_line", + "gbuffers_textured", + "gbuffers_textured_lit", + "gbuffers_skybasic", + "gbuffers_skytextured", + "gbuffers_clouds", + "gbuffers_terrain", + "gbuffers_terrain_solid", + "gbuffers_terrain_cutout", + "gbuffers_damagedblock", + "gbuffers_block", + "gbuffers_block_translucent", + "gbuffers_beaconbeam", + "gbuffers_item", + "gbuffers_entities", + "gbuffers_entities_translucent", + "gbuffers_lightning", + "gbuffers_particles", + "gbuffers_particles_translucent", + "gbuffers_entities_glowing", + "gbuffers_armor_glint", + "gbuffers_spidereyes", + "gbuffers_hand", + "gbuffers_weather", + "gbuffers_water", + "gbuffers_hand_water", + "dh_terrain", + "dh_water", + "dh_generic", + "dh_shadow", + "final", +]; + +/// The program groups that run as full-screen passes, in Iris's order. +const GROUPS: &[&str] = &["begin", "prepare", "deferred", "composite"]; + +impl JavaPack { + /// The full-screen passes of the programs in `dir` (`world0`, or `""` + /// for the pack's root), with the player's `values`. + pub fn passes( + &self, + dir: &str, + values: &OptionValues, + env: &Environment, + properties: &ShaderProperties, + ) -> Result { + let path = |name: &str| { + if dir.is_empty() { + name.to_owned() + } else { + format!("{dir}/{name}") + } + }; + // A pass needs both halves, and its `program.*.enabled` to hold. + let exists = |name: &str| { + let p = path(name); + self.programs.contains(&format!("{p}.vsh")) + && self.programs.contains(&format!("{p}.fsh")) + && properties.program_enabled(&p, &self.options, values) + }; + let fragment = |name: &str| -> Result { + let file = format!("{}.fsh", path(name)); + Ok(self.preprocess(&file, values, env)?.with_comments()) + }; + let group = |base: &str| -> Vec { + (0..100) + .map(|i| { + if i == 0 { + base.to_owned() + } else { + format!("{base}{i}") + } + }) + .filter(|name| exists(name)) + .collect() + }; + let groups: Vec> = GROUPS.iter().map(|g| group(g)).collect(); + + // Target settings come from the fragments of every pass, in Iris's + // order (`ProgramSet.locateDirectives`): some packs declare them in + // `final` only. + let mut sources: Vec = Vec::new(); + for name in group("shadowcomp") + .iter() + .chain(&groups[0]) + .chain(&groups[1]) + { + sources.push(fragment(name)?); + } + for name in SINGLE_PROGRAMS { + if exists(name) { + sources.push(fragment(name)?); + } + } + let mut fragments: BTreeMap = BTreeMap::new(); + for name in groups[2].iter().chain(&groups[3]) { + let text = fragment(name)?; + sources.push(text.clone()); + fragments.insert(name.clone(), text); + } + for name in &groups[0..2].concat() { + fragments.insert(name.clone(), fragment(name)?); + } + let mut out = Passes { + targets: target_settings(sources.iter().map(String::as_str), properties), + ..Passes::default() + }; + + let flips = explicit_flips(properties); + let run = |group: &[String], pre: &str, flipped: &mut BTreeSet| { + *flipped = flips_pre(&flips, pre, flipped); + let mut passes = Vec::new(); + for name in group { + let text = &fragments[name]; + let pass = Pass { + program: path(name), + draw_buffers: draw_buffers(text).map_err(|e| format!("{}: {e}", path(name)))?, + reads_alt: flipped.clone(), + mipmapped: mipmapped(text), + scale: properties + .preprocessed + .get(&format!("scale.{name}")) + .and_then(|v| v.split_whitespace().next()?.parse().ok()) + .unwrap_or(1.0), + }; + *flipped = flip_set(&pass, &flips); + passes.push(pass); + } + Ok::<_, String>(passes) + }; + let mut flipped = BTreeSet::new(); + out.begin = run(&groups[0], "begin_pre", &mut flipped)?; + out.prepare = run(&groups[1], "prepare_pre", &mut flipped)?; + out.flipped_after_prepare = flipped.clone(); + out.deferred = run(&groups[2], "deferred_pre", &mut flipped)?; + out.flipped_after_translucent = flipped.clone(); + out.composite = run(&groups[3], "composite_pre", &mut flipped)?; + out.flipped_at_end = flipped; + if exists("final") { + out.final_pass = Some(Pass { + program: path("final"), + draw_buffers: None, + reads_alt: out.flipped_at_end.clone(), + mipmapped: mipmapped(&fragment("final")?), + scale: 1.0, + }); + } + Ok(out) + } +} + +fn toggle(set: &mut BTreeSet, buffer: u32) { + if !set.remove(&buffer) { + set.insert(buffer); + } +} + +/// `flip.. = true|false`, by pass and target index. +fn explicit_flips(properties: &ShaderProperties) -> BTreeMap<&str, BTreeMap> { + let mut out: BTreeMap<&str, BTreeMap> = BTreeMap::new(); + for (key, value) in properties.preprocessed.iter() { + let Some((pass, buffer)) = key.strip_prefix("flip.").and_then(|k| k.split_once('.')) else { + continue; + }; + if let (Some(i), Ok(flip)) = (target_index(buffer), value.trim().parse()) { + out.entry(pass).or_default().insert(i, flip); + } + } + out +} + +/// The flipped set after `pass` ran: what it read, flipped by its writes +/// and its explicit flips. +fn flip_set(pass: &Pass, flips: &BTreeMap<&str, BTreeMap>) -> BTreeSet { + let name = pass.program.rsplit('/').next().unwrap_or(&pass.program); + let explicit = flips.get(name); + let mut set = pass.reads_alt.clone(); + for &buffer in pass.draw_buffers.as_deref().unwrap_or(&[0]) { + if explicit.and_then(|f| f.get(&buffer)) != Some(&false) { + toggle(&mut set, buffer); + } + } + for (&buffer, &flip) in explicit.into_iter().flatten() { + if flip { + toggle(&mut set, buffer); + } + } + set +} + +fn flips_pre( + flips: &BTreeMap<&str, BTreeMap>, + pre: &str, + before: &BTreeSet, +) -> BTreeSet { + let mut set = before.clone(); + for (&buffer, &flip) in flips.get(pre).into_iter().flatten() { + if flip { + toggle(&mut set, buffer); + } + } + set +} + +/// `const bool colortexNMipmapEnabled = true;` in a program's fragment. +fn mipmapped(fragment: &str) -> BTreeSet { + let mut out = BTreeSet::new(); + for c in const_directives(fragment) { + if c.ty != "bool" { + continue; + } + if let Some(i) = target_key(&c.key, "MipmapEnabled") { + match c.value.as_str() { + "true" => out.insert(i), + "false" => out.remove(&i), + _ => false, + }; + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn comment_directives_as_iris_finds_them() { + // Iris's own cases (`CommentDirectiveParser.Tests`). + let find = |text: &str, name: &str| comment_directive(text, name).map(|d| d.1); + let draw = |text: &str| find(text, "DRAWBUFFERS"); + assert_eq!( + draw("Some normal text that doesn't contain a DRAWBUFFERS directive"), + None + ); + assert_eq!( + draw("doesn't contain a /* DRAWBUFFERS: directive of any sort"), + None + ); + assert_eq!( + draw("/*DRAWBUFFERS:321*/ OptiFine will detect this").as_deref(), + Some("321") + ); + assert_eq!( + draw("a directive: /* DRAWBUFFERS:321 */").as_deref(), + Some("321") + ); + assert_eq!( + draw("This is a line /* DRAWBUFFERS:31 */ containing").as_deref(), + Some("31") + ); + assert_eq!( + draw("/* DRAWBUFFERS: */ invalid but matching").as_deref(), + Some("") + ); + assert_eq!(draw(" /*DRAWBUFFERS:12*/").as_deref(), Some("12")); + let lines = "/* RENDERTARGETS:Duplicate handling? */\nuniform sampler2D test;\n\ + /* RENDERTARGETS:Within a line? */ Let's see /* RENDERTARGETS:It works */\n"; + assert_eq!(find(lines, "RENDERTARGETS").as_deref(), Some("It works")); + + // The later directive wins, whichever kind it is. + assert_eq!( + draw_buffers("/* RENDERTARGETS: 4,10 */ /* DRAWBUFFERS:02 */"), + Ok(Some(vec![0, 2])) + ); + assert_eq!( + draw_buffers("/* DRAWBUFFERS:02 */ /* RENDERTARGETS: 4,10 */"), + Ok(Some(vec![4, 10])) + ); + assert_eq!(draw_buffers("void main() {}"), Ok(None)); + assert!(draw_buffers("/* RENDERTARGETS: 40 */").is_err()); + assert!(draw_buffers("/* DRAWBUFFERS:0a */").is_err()); + } + + #[test] + fn const_directives_and_target_settings() { + let text = "\ +const int colortex4Format = rgba16f; + const bool gaux2Clear = false; // kept between frames +const vec4 colortex0ClearColor = vec4(0.5, 0.25, 0.0, 1.0); +const int colortex5Format = NOT_A_FORMAT; +uniform const int x = 1; +const int colortex6Format=RGBA8; +const intx colortex7Format = RGBA8; +const ivec3 workGroups = ivec3(16, 8, 16); +"; + let consts = const_directives(text); + assert_eq!( + consts + .iter() + .map(|c| (c.ty, c.key.as_str(), c.value.as_str())) + .collect::>(), + [ + ("int", "colortex4Format", "rgba16f"), + ("bool", "gaux2Clear", "false"), + ("vec4", "colortex0ClearColor", "vec4(0.5, 0.25, 0.0, 1.0)"), + ("int", "colortex5Format", "NOT_A_FORMAT"), + ("int", "colortex6Format", "RGBA8"), + ("ivec3", "workGroups", "ivec3(16, 8, 16)"), + ], + "a type needs a space after it, the name doesn't" + ); + let targets = target_settings([text], &ShaderProperties::default()); + assert_eq!(targets.len(), MAX_COLOR_BUFFERS as usize); + assert_eq!(targets[4].format, "RGBA16F"); + assert_eq!(targets[5].format, "RGBA", "unknown formats are ignored"); + assert!(!targets[5].clear, "gaux2 is colortex5"); + assert_eq!(targets[0].clear_color, Some([0.5, 0.25, 0.0, 1.0])); + assert_eq!(targets[1], TargetSettings::default()); + } +} diff --git a/crates/horizon-shaderpack/src/preprocess.rs b/crates/horizon-shaderpack/src/preprocess.rs index f5b8478a6..18c890496 100644 --- a/crates/horizon-shaderpack/src/preprocess.rs +++ b/crates/horizon-shaderpack/src/preprocess.rs @@ -204,11 +204,92 @@ pub struct Preprocessed { pub version: Vec, /// `#extension` lines' words (`["GL_ARB_gpu_shader5", ":", "enable"]`). pub extensions: Vec>, + /// The comments Iris reads directives from (`/* DRAWBUFFERS:01 */`, + /// `const int colortex0Format = RGBA16F;`) that are outside `#if`'d out + /// blocks, with their output line. Iris keeps comments through its + /// preprocessor; pp-rs drops them. + pub comments: Vec<(u32, String)>, +} + +impl Preprocessed { + /// The text with its kept comments put back at the start of their + /// lines: what Iris's directive parsers read. + pub fn with_comments(&self) -> String { + let mut comments = self.comments.iter().peekable(); + let mut out = String::new(); + for (i, line) in self.text.lines().enumerate() { + while let Some((_, comment)) = comments.next_if(|(at, _)| *at as usize <= i + 1) { + out.push_str(comment); + out.push(' '); + } + out.push_str(line); + out.push('\n'); + } + out + } +} + +/// What a comment must hold for Iris to read a directive from it. +const DIRECTIVE_WORDS: &[&str] = &["DRAWBUFFERS:", "RENDERTARGETS:", "const"]; +/// The identifier a kept comment is replaced by while pp-rs runs. +const COMMENT_MARK: &str = "iris_comment_"; + +/// Replace the comments holding [`DIRECTIVE_WORDS`] with an identifier +/// (and their line breaks), so the ones in `#if`'d out blocks disappear with +/// them. Comments on directive lines are left alone: there the identifier +/// would end up in a macro or an `#if`. +fn mark_comments(text: &str) -> (String, Vec) { + let mut out = String::with_capacity(text.len()); + let mut comments = Vec::new(); + let (mut directive, mut line_start, mut continued) = (false, true, false); + let mut rest = text; + while let Some(c) = rest.chars().next() { + let comment_len = if rest.starts_with("//") { + rest.find('\n').unwrap_or(rest.len()) + } else if let Some(body) = rest.strip_prefix("/*") { + body.find("*/").map_or(rest.len(), |i| i + 4) + } else { + 0 + }; + if comment_len > 0 { + let comment = &rest[..comment_len]; + if !directive && DIRECTIVE_WORDS.iter().any(|w| comment.contains(w)) { + out.push_str(&format!(" {COMMENT_MARK}{} ", comments.len())); + out.extend(std::iter::repeat_n('\n', comment.matches('\n').count())); + comments.push(comment.to_owned()); + } else { + out.push_str(comment); + } + if comment.contains('\n') { + // A comment spanning lines ends a directive like a line break. + directive = false; + } + line_start = false; + rest = &rest[comment_len..]; + continue; + } + match c { + '\n' => { + directive = directive && continued; + line_start = !directive; + } + '#' if line_start => directive = true, + c if c.is_whitespace() => {} + _ => line_start = false, + } + if c != '\r' && !c.is_whitespace() || c == '\n' { + continued = c == '\\'; + } + out.push(c); + rest = &rest[c.len_utf8()..]; + } + (out, comments) } /// Expand `source` with `env`'s macros. Errors name the file and line. pub fn preprocess(source: &Inlined, env: &Environment) -> Result { - let text = &source.text; + let (marked, comments) = mark_comments(&source.text); + let text = &marked; let line_starts: Vec = std::iter::once(0) .chain(text.match_indices('\n').map(|(i, _)| i + 1)) .collect(); @@ -222,6 +303,7 @@ pub fn preprocess(source: &Inlined, env: &Environment) -> Result Result continue, + TokenValue::Ident(ref name) if name.starts_with(COMMENT_MARK) => { + if let Some(comment) = name[COMMENT_MARK.len()..] + .parse::() + .ok() + .and_then(|i| comments.get(i)) + { + out.comments.push((at, comment.clone())); + } + continue; + } value => spelling(value), }; if at > line { diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite.fsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite.fsh new file mode 100644 index 000000000..c6304265e --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite.fsh @@ -0,0 +1,17 @@ +#version 120 +uniform sampler2D colortex3; +varying vec2 texcoord; +#ifdef BLOOM +/* +const int colortex1Format = R8; +*/ +#endif +const bool colortex3MipmapEnabled = true; // read with mipmaps +void main() { +#if 0 + /* DRAWBUFFERS:12 */ +#endif + /* RENDERTARGETS: 0,1 */ + gl_FragData[0] = texture2D(colortex3, texcoord); + gl_FragData[1] = vec4(1.0); +} diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite.vsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite.vsh new file mode 100644 index 000000000..eb261aadd --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite.vsh @@ -0,0 +1,3 @@ +#version 120 +varying vec2 texcoord; +void main() { gl_Position = ftransform(); texcoord = gl_MultiTexCoord0.xy; } diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite1.fsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite1.fsh new file mode 100644 index 000000000..783c79b52 --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite1.fsh @@ -0,0 +1,7 @@ +#version 120 +uniform sampler2D colortex0; +varying vec2 texcoord; +void main() { + /* DRAWBUFFERS:0 */ + gl_FragData[0] = texture2D(colortex0, texcoord); +} diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite1.vsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite1.vsh new file mode 100644 index 000000000..eb261aadd --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite1.vsh @@ -0,0 +1,3 @@ +#version 120 +varying vec2 texcoord; +void main() { gl_Position = ftransform(); texcoord = gl_MultiTexCoord0.xy; } diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite2.fsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite2.fsh new file mode 100644 index 000000000..d105112b9 --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite2.fsh @@ -0,0 +1,5 @@ +#version 120 +void main() { + /* DRAWBUFFERS:7 */ + gl_FragData[0] = vec4(1.0); +} diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite2.vsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite2.vsh new file mode 100644 index 000000000..eb261aadd --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite2.vsh @@ -0,0 +1,3 @@ +#version 120 +varying vec2 texcoord; +void main() { gl_Position = ftransform(); texcoord = gl_MultiTexCoord0.xy; } diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/deferred.fsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/deferred.fsh new file mode 100644 index 000000000..6a24357c6 --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/deferred.fsh @@ -0,0 +1,11 @@ +#version 120 +varying vec2 texcoord; +/* +const int colortex1Format = RGBA16F; +const bool colortex2Clear = false; +const vec4 colortex3ClearColor = vec4(1.0, 0.0, 0.0, 1.0); +*/ +void main() { + /* DRAWBUFFERS:3 */ + gl_FragData[0] = vec4(texcoord, 0.0, 1.0); +} diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/deferred.vsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/deferred.vsh new file mode 100644 index 000000000..eb261aadd --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/deferred.vsh @@ -0,0 +1,3 @@ +#version 120 +varying vec2 texcoord; +void main() { gl_Position = ftransform(); texcoord = gl_MultiTexCoord0.xy; } diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/final.fsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/final.fsh new file mode 100644 index 000000000..65c6b0195 --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/final.fsh @@ -0,0 +1,6 @@ +#version 120 +uniform sampler2D colortex0; +varying vec2 texcoord; +void main() { + gl_FragColor = texture2D(colortex0, texcoord); +} diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/final.vsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/final.vsh new file mode 100644 index 000000000..eb261aadd --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/final.vsh @@ -0,0 +1,3 @@ +#version 120 +varying vec2 texcoord; +void main() { gl_Position = ftransform(); texcoord = gl_MultiTexCoord0.xy; } diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/shaders.properties b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/shaders.properties new file mode 100644 index 000000000..0a9996c4c --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/shaders.properties @@ -0,0 +1,6 @@ +# Full-screen passes: a smaller composite1 that also flips colortex2, +# composite2 off, colortex3 at half size. +scale.composite1 = 0.5 +flip.composite1.colortex2 = true +program.composite2.enabled = false +size.buffer.colortex3 = 0.5 0.5 diff --git a/crates/horizon-shaderpack/tests/pack.rs b/crates/horizon-shaderpack/tests/pack.rs index 4a7d4959e..ec193847a 100644 --- a/crates/horizon-shaderpack/tests/pack.rs +++ b/crates/horizon-shaderpack/tests/pack.rs @@ -3,7 +3,11 @@ //! `HORIZON_TEST_SHADERPACKS` names a folder of them (development only: //! third-party packs are never committed). -use horizon_shaderpack::{JavaPack, Stage, preprocess::Environment}; +use horizon_shaderpack::{ + JavaPack, Stage, + pipeline::{TargetSettings, TargetSize}, + preprocess::Environment, +}; use std::{ collections::BTreeMap, io::Write, @@ -187,6 +191,54 @@ fn opens_zips_with_shaders_in_a_subfolder() { std::fs::remove_dir_all(dir).unwrap(); } +#[test] +fn plans_the_full_screen_passes() { + let pack = JavaPack::open(&Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/passes")) + .unwrap(); + let values = pack.values(&BTreeMap::new()); + let properties = pack.properties(&values, &env()).unwrap(); + let passes = pack.passes("", &values, &env(), &properties).unwrap(); + + // Settings from the active comments only: colortex1's R8 is #ifdef'd out. + assert_eq!(passes.targets[1].format, "RGBA16F"); + assert!(!passes.targets[2].clear); + assert_eq!(passes.targets[3].clear_color, Some([1.0, 0.0, 0.0, 1.0])); + assert_eq!(passes.targets[3].size, Some(TargetSize::Relative(0.5, 0.5))); + assert_eq!(passes.targets[4], TargetSettings::default()); + + let summary = |p: &horizon_shaderpack::pipeline::Pass| { + ( + p.program.clone(), + p.draw_buffers.clone(), + p.reads_alt.iter().copied().collect::>(), + ) + }; + assert_eq!( + passes.deferred.iter().map(summary).collect::>(), + [("deferred".into(), Some(vec![3]), vec![])] + ); + // composite2 is off; RENDERTARGETS in `#if 0` doesn't count; each pass + // reads the side its predecessors wrote, and composite1 flips colortex2. + assert_eq!( + passes.composite.iter().map(summary).collect::>(), + [ + ("composite".into(), Some(vec![0, 1]), vec![3]), + ("composite1".into(), Some(vec![0]), vec![0, 1, 3]), + ] + ); + assert_eq!(passes.composite[0].mipmapped, [3].into()); + assert_eq!(passes.composite[1].scale, 0.5); + assert_eq!(passes.flipped_after_prepare, [].into()); + assert_eq!(passes.flipped_after_translucent, [3].into()); + assert_eq!(passes.flipped_at_end, [1, 2, 3].into()); + let final_pass = passes.final_pass.unwrap(); + assert_eq!( + (final_pass.program.as_str(), final_pass.draw_buffers), + ("final", None) + ); + assert_eq!(final_pass.reads_alt, [1, 2, 3].into()); +} + fn scratch(name: &str) -> PathBuf { std::env::temp_dir().join(format!("horizon-shaderpack-{name}-{}", std::process::id())) } @@ -276,3 +328,45 @@ fn real_packs_properties() { assert!(unknown.len() <= 2, "{unknown:?}"); } } + +/// The overworld's full-screen passes for the packs in +/// `HORIZON_TEST_SHADERPACKS`. Skipped when the variable isn't set. +#[test] +fn real_packs_passes() { + let Some(dir) = std::env::var_os("HORIZON_TEST_SHADERPACKS") else { + return; + }; + for entry in std::fs::read_dir(dir).unwrap().flatten() { + let Ok(pack) = JavaPack::open(&entry.path()) else { + continue; + }; + let values = pack.values(&BTreeMap::new()); + let properties = pack.properties(&values, &env()).unwrap(); + let folder = if pack.programs.iter().any(|p| p.starts_with("world0/")) { + "world0" + } else { + "" + }; + let passes = pack.passes(folder, &values, &env(), &properties).unwrap(); + println!("{}:", entry.file_name().to_string_lossy()); + for (i, t) in passes.targets.iter().enumerate() { + if *t != TargetSettings::default() { + println!(" colortex{i}: {t:?}"); + } + } + for p in passes + .begin + .iter() + .chain(&passes.prepare) + .chain(&passes.deferred) + .chain(&passes.composite) + .chain(&passes.final_pass) + { + println!( + " {} writes {:?} reads alt {:?} mipmaps {:?} scale {}", + p.program, p.draw_buffers, p.reads_alt, p.mipmapped, p.scale + ); + } + assert!(passes.final_pass.is_some() || !passes.composite.is_empty()); + } +} diff --git a/docs/shader-packs.md b/docs/shader-packs.md index 76e2e2771..bd9646df8 100644 --- a/docs/shader-packs.md +++ b/docs/shader-packs.md @@ -19,6 +19,7 @@ Both are off by default. With no pack selected the renderer draws exactly what i | Java shader packs: preprocessor (`crates/horizon-shaderpack`: includes, options, Iris's macros) | done: every program of the three test packs preprocesses | | Java shader packs: GLSL → WGSL (`horizon_shaderpack::translate`: syntax-tree legaliser, naga) | done: the 464 programs the three test packs enable by default translate (below) | | Java shader packs: `shaders.properties` (`horizon_shaderpack::properties`: screens, sliders, profiles, `program.*.enabled`, image formats, blend modes) | done, no screen yet: read like Iris for the three test packs | +| Java shader packs: full-screen pass plan (`horizon_shaderpack::pipeline`: `DRAWBUFFERS`/`RENDERTARGETS`, `colortexN` formats and clearing, buffer flips) | done, not drawn yet: planned for the three test packs | | Java shader packs: Iris pipeline emulation | planned | ## How the renderer draws a frame today From 2862f17e86b28dac9bbb68b9917a1a3772ecb5bf Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 00:22:39 +0200 Subject: [PATCH 05/97] horizon-client: run a Java pack's full-screen passes over the world A folder holding shaders/, or a .zip, in shaderpacks/ now loads from the Shader Packs screen: every planned begin, prepare, deferred, composite and final pass is translated and drawn each frame over Horizon's world, with Iris's formats, clears and buffer flips. The targets keep OpenGL's layout (the translator flips gl_Position and remaps its depth), the world enters as OpenGL depth, and the pack gets OpenGL matrices. Textures share four samplers by kind (Metal allows 16 per stage), and isnan/isinf test bits (Metal's fast math folds x != x). Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + crates/horizon-client/Cargo.toml | 2 + .../horizon-client/src/game/shader_packs.rs | 11 +- crates/horizon-client/src/gfx.rs | 63 +- crates/horizon-client/src/gfx/java_stages.rs | 1584 +++++++++++++++++ crates/horizon-client/src/java_pack.rs | 165 ++ crates/horizon-client/src/lib.rs | 1 + crates/horizon-client/src/shader_pack.rs | 63 +- crates/horizon-shaderpack/src/pipeline.rs | 92 +- crates/horizon-shaderpack/src/translate.rs | 84 +- crates/horizon-shaderpack/tests/pack.rs | 26 + crates/horizon-shaderpack/tests/translate.rs | 51 + docs/shader-packs.md | 41 +- 13 files changed, 2136 insertions(+), 48 deletions(-) create mode 100644 crates/horizon-client/src/gfx/java_stages.rs create mode 100644 crates/horizon-client/src/java_pack.rs diff --git a/Cargo.lock b/Cargo.lock index 8d5cd15fd..68d0b250d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1417,6 +1417,7 @@ dependencies = [ "horizon-friends", "horizon-mods", "horizon-protocol", + "horizon-shaderpack", "horizon-telemetry", "horizon-theme", "horizon-world", diff --git a/crates/horizon-client/Cargo.toml b/crates/horizon-client/Cargo.toml index b9aeb7aea..19b4c81db 100644 --- a/crates/horizon-client/Cargo.toml +++ b/crates/horizon-client/Cargo.toml @@ -27,6 +27,8 @@ serde_json = { version = "1.0.151", features = ["raw_value"] } # Shader packs: pack.toml, and WGSL checked before it reaches wgpu. toml = "1.1" naga = { version = "30.0.1", features = ["wgsl-in"] } +# Java (OptiFine/Iris) shader packs, translated to WGSL. +horizon-shaderpack = { path = "../horizon-shaderpack" } cpal = "0.16" symphonia = { version = "0.5", default-features = false, features = ["ogg", "vorbis"] } miniz_oxide = "0.8" diff --git a/crates/horizon-client/src/game/shader_packs.rs b/crates/horizon-client/src/game/shader_packs.rs index ddccdcee6..da4e8f594 100644 --- a/crates/horizon-client/src/game/shader_packs.rs +++ b/crates/horizon-client/src/game/shader_packs.rs @@ -48,7 +48,16 @@ impl Game<'_> { None } else { state.stamp = Stamp::of(&dir.join(&id)); - match ShaderPack::load(&dir.join(&id), &shader_pack::load_values(&dir, &id)) { + // Java packs are told which GPU they run on. + let (vendor, renderer) = self.renderer.as_ref().map_or(("Unknown", ""), |r| { + let name = r.info.name.as_str(); + ( + horizon_client::java_pack::vendor_name(r.info.vendor, name), + name, + ) + }); + let values = shader_pack::load_values(&dir, &id); + match ShaderPack::load_any(&dir.join(&id), &values, vendor, renderer) { Ok(pack) => Some(pack), Err(e) => return self.shader_pack_error(&id, &e), } diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 1120830e6..c2e4775b7 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -58,6 +58,7 @@ pub fn outline_vertices(edges: &[[f64; 6]], camera: glam::DVec3) -> Vec<[f32; 6] } mod arena; +mod java_stages; mod occlusion; mod pack_stages; pub mod panorama; @@ -308,7 +309,7 @@ pub struct Renderer { /// Resolution scale and FXAA (Horizon settings). world_post: world_post::Post, /// The selected shader pack's stages (Horizon setting; none by default). - pack: Option, + pack: Option, pub hud: HudRenderer, /// The menus' backdrop blur, made on first use. post: Option, @@ -1679,19 +1680,28 @@ impl Renderer { /// Run `pack`'s stages over the world from now on (`None`: no pack, the /// vanilla frame). On error the previous pack stays. pub fn set_shader_pack(&mut self, pack: Option<&crate::shader_pack::ShaderPack>) -> Result<()> { + let format = self.config.format.remove_srgb_suffix(); self.pack = match pack { - Some(pack) => Some(pack_stages::PackStages::new( - &self.device, - self.config.format.remove_srgb_suffix(), - pack, - )?), + Some(pack) => Some(match &pack.java { + Some(java) => Pack::Java(java_stages::JavaStages::new( + &self.device, + &self.queue, + format, + &pack.id, + java, + )?), + None => Pack::Horizon(pack_stages::PackStages::new(&self.device, format, pack)?), + }), None => None, }; Ok(()) } /// The folder name of the pack `set_shader_pack` gave. pub fn shader_pack_id(&self) -> Option<&str> { - self.pack.as_ref().map(|p| p.id.as_str()) + self.pack.as_ref().map(|p| match p { + Pack::Horizon(p) => p.id.as_str(), + Pack::Java(p) => p.id.as_str(), + }) } /// Horizon's resolution scale (world pixels per window pixel) and FXAA. pub fn set_world_post(&mut self, scale: f32, antialias: bool) { @@ -2380,16 +2390,28 @@ impl Renderer { if let (true, Some((color, depth)), Some(size), Some(stages)) = (pack, &scaled, self.world_post.size(), self.pack.as_mut()) { - let output = stages.run( - &self.device, - &self.queue, - &mut encoder, - color, - depth, - size, - &self.world_post, - pack_uniform, - ); + let output = match stages { + Pack::Horizon(stages) => stages.run( + &self.device, + &self.queue, + &mut encoder, + color, + depth, + size, + &self.world_post, + pack_uniform, + ), + Pack::Java(stages) => stages.run( + &self.device, + &self.queue, + &mut encoder, + color, + depth, + size, + &self.world_post, + pack_uniform, + ), + }; self.world_post .finish_with(&mut encoder, &view_target, output); } else if scaled.is_some() { @@ -2710,6 +2732,13 @@ fn shader_pack_uniform( } } +/// The selected shader pack on the GPU: a Horizon pack's WGSL stages, or a +/// Java pack's translated programs. +enum Pack { + Horizon(pack_stages::PackStages), + Java(java_stages::JavaStages), +} + fn world_projection(fov_degrees: f32, aspect: f32, far: f32) -> Mat4 { Mat4::perspective_rh(fov_degrees.to_radians(), aspect, far, 0.05) } diff --git a/crates/horizon-client/src/gfx/java_stages.rs b/crates/horizon-client/src/gfx/java_stages.rs new file mode 100644 index 000000000..d3e393e07 --- /dev/null +++ b/crates/horizon-client/src/gfx/java_stages.rs @@ -0,0 +1,1584 @@ +//! A Java (OptiFine/Iris) shader pack on the GPU (docs/shader-packs.md): +//! Iris's colour targets (`colortex0..`, each with a main and an alternate +//! side) and the pack's `begin`, `prepare`, `deferred`, `composite` and +//! `final` programs as full-screen passes over them, as Iris's +//! `CompositeRenderer` runs them. Horizon's world enters `colortex0` and +//! `depthtex0..2` after `prepare`, where Iris's gbuffers programs would have +//! drawn, and the last picture goes to `world_post`. The targets keep GL's +//! layout (row 0 at the bottom, which the translated vertex programs +//! produce), so the world is flipped going in and the picture coming out. +use super::pack_stages::FrameUniform; +use crate::java_pack::{JavaFolder, JavaPasses}; +use glam::{Mat3, Mat4, Vec3, Vec4}; +use horizon_shaderpack::pipeline::TargetSettings; +use horizon_shaderpack::pipeline::{Program, target_index}; +use std::collections::{BTreeMap, BTreeSet}; + +/// Iris's `noisetex` size (`noiseTextureResolution`'s default). +const NOISE_SIZE: u32 = 256; +/// The vertex buffers a pass may read (wgpu's default limit). +const MAX_ATTRIBUTES: usize = 8; + +const BLIT: &str = r#" +@group(0) @binding(0) var color: texture_2d; +@group(0) @binding(1) var depth: texture_depth_2d; + +@vertex +fn vs(@builtin(vertex_index) i: u32) -> @builtin(position) vec4f { + let uv = vec2f(f32((i << 1u) & 2u), f32(i & 2u)); + return vec4f(uv * 2.0 - 1.0, 0.0, 1.0); +} + +struct World { + @location(0) color: vec4f, + @location(1) depth: vec4f, +} + +// The world's colour and depth, upside down into GL's layout; Horizon's +// reversed depth (near 1) as GL's (near 0). +@fragment +fn world_in(@builtin(position) p: vec4f) -> World { + let size = textureDimensions(depth); + let xy = vec2u(u32(p.x), size.y - 1u - u32(p.y)); + return World(textureLoad(color, xy, 0), vec4f(1.0 - textureLoad(depth, xy, 0))); +} + +// A target, upside down back into wgpu's layout. +@fragment +fn present(@builtin(position) p: vec4f) -> @location(0) vec4f { + let size = textureDimensions(color); + return textureLoad(color, vec2u(u32(p.x), size.y - 1u - u32(p.y)), 0); +} +"#; + +/// A wgpu format for each of Iris's (`InternalTextureFormat`), renderable +/// everywhere: 8-bit unsigned normalized formats stay, signed normalized, +/// 16-bit and packed float ones become half floats, integer ones 32-bit. +fn target_format(iris: &str) -> wgpu::TextureFormat { + use wgpu::TextureFormat as F; + let channels = if iris.starts_with("RGB") || iris.contains("_B") { + 4 + } else if iris.starts_with("RG") { + 2 + } else { + 1 + }; + let pick = |one, two, four| match channels { + 1 => one, + 2 => two, + _ => four, + }; + match iris { + "R8" | "RG8" | "RGB8" | "RGBA8" | "RGBA" | "RGBA2" | "RGBA4" | "R3_G3_B2" | "RGB5_A1" + | "RGB565" => pick(F::R8Unorm, F::Rg8Unorm, F::Rgba8Unorm), + "RGB10_A2" => F::Rgb10a2Unorm, + "RGB10_A2UI" => F::Rgb10a2Uint, + f if f.ends_with("32F") => pick(F::R32Float, F::Rg32Float, F::Rgba32Float), + f if f.ends_with("UI") => pick(F::R32Uint, F::Rg32Uint, F::Rgba32Uint), + f if f.ends_with('I') => pick(F::R32Sint, F::Rg32Sint, F::Rgba32Sint), + _ => pick(F::R16Float, F::Rg16Float, F::Rgba16Float), + } +} + +/// How a pass reads a texture of `format`. +fn sample_type(format: wgpu::TextureFormat) -> wgpu::TextureSampleType { + match format.sample_type(None, None) { + Some(wgpu::TextureSampleType::Float { .. }) => wgpu::TextureSampleType::Float { + filterable: !matches!( + format, + wgpu::TextureFormat::R32Float + | wgpu::TextureFormat::Rg32Float + | wgpu::TextureFormat::Rgba32Float + ), + }, + Some(t) => t, + None => wgpu::TextureSampleType::Float { filterable: false }, + } +} + +/// A colour target's two sides. +struct Target { + format: wgpu::TextureFormat, + main: wgpu::Texture, + alt: wgpu::Texture, + main_view: wgpu::TextureView, + alt_view: wgpu::TextureView, + clear: Option, +} + +/// What a sampler of the pack reads. +#[derive(Clone, Copy, Debug, PartialEq)] +enum Source { + Target(u32), + /// `depthtex0..2`: the world's depth. + Depth, + Noise, + /// `shadowtex*`: no shadow map yet, so a far depth (nothing in shadow); + /// also Distant Horizons' and Voxy's depth, as without those mods. + Shadow, + /// Anything else (`shadowcolor*`, custom textures): white or black. + Blank(bool), +} + +fn source(name: &str) -> Source { + match name { + "depthtex0" | "depthtex1" | "depthtex2" | "gdepthtex" => Source::Depth, + "noisetex" => Source::Noise, + "shadowtex0" | "shadowtex1" | "shadow" | "watershadow" => Source::Shadow, + n if n.starts_with("dhDepthTex") || n.starts_with("vxDepthTex") => Source::Shadow, + n if n.starts_with("shadowcolor") => Source::Blank(true), + n => target_index(n).map_or(Source::Blank(false), Source::Target), + } +} + +/// A sampler of a pass: its texture and sampler bindings. +struct SamplerBinding { + source: Source, + dimension: wgpu::TextureViewDimension, + /// `isampler` (true) or `usampler` (false). + signed: Option, + texture: u32, + sampler: u32, + /// The shared sampler it uses (`iris_linear`), if any. + shared: Option, + compare: bool, +} + +/// A scalar slot of the uniform block: byte offset, and whether it holds +/// an integer (`int`, `uint` or `bool`). +type Slot = (u32, bool); + +struct PassGpu { + program: String, + pipeline: wgpu::RenderPipeline, + layout: wgpu::BindGroupLayout, + samplers: Vec, + /// Uniform members by name, their scalar slots in order. + uniforms: Vec<(String, Vec)>, + uniform: Option<(u32, wgpu::Buffer)>, + /// Each vertex buffer slot's data: 0 position, 1 white, 2 up, 3 zero. + attributes: Vec, + draw_buffers: Vec, + reads_alt: BTreeSet, + scale: f32, +} + +/// What every pass of the pack shares. +struct Common { + linear: wgpu::Sampler, + nearest: wgpu::Sampler, + repeat: wgpu::Sampler, + compare: wgpu::Sampler, + noise: wgpu::TextureView, + /// Full-screen triangle data per attribute kind (see `PassGpu::attributes`). + vertices: [wgpu::Buffer; 4], + world_layout: wgpu::BindGroupLayout, + present_layout: wgpu::BindGroupLayout, + blit: wgpu::ShaderModule, +} + +/// The textures of one folder at one size. +struct Bound { + folder: usize, + size: [u32; 2], + targets: BTreeMap, + depth: wgpu::TextureView, + /// Per pass, then `final`. + groups: Vec, + world_in: (wgpu::RenderPipeline, wgpu::BindGroup), + /// The world goes to colortex0's alternate side. + world_alt: bool, + final_target: Option, + present: (wgpu::RenderPipeline, wgpu::BindGroup), + output_view: wgpu::TextureView, + output: wgpu::BindGroup, + /// Blank textures, kept alive by the bind groups too. + _blank: Vec, +} + +struct Folder { + targets: Vec, + world_at: usize, + flipped_after_prepare: BTreeSet, + flipped_at_end: BTreeSet, + passes: Vec, + final_pass: Option, +} + +pub struct JavaStages { + pub id: String, + format: wgpu::TextureFormat, + common: Common, + folders: Vec, + folder_for: [Option; 3], + bound: Option, + started: std::time::Instant, + last: Option, + frames: u32, + previous: Option<(Mat4, Mat4, Vec3)>, +} + +impl JavaStages { + /// The pipelines of a loaded Java pack, or wgpu's error. + pub fn new( + device: &wgpu::Device, + queue: &wgpu::Queue, + format: wgpu::TextureFormat, + id: &str, + pack: &JavaPasses, + ) -> Result { + let common = Common::new(device, queue); + let folder_for = [-1, 0, 1].map(|d| pack.folder_for(d)); + let scope = device.push_error_scope(wgpu::ErrorFilter::Validation); + let folders = pack + .folders + .iter() + .map(|plan: &JavaFolder| { + let formats: Vec = plan + .targets + .iter() + .map(|t| target_format(&t.format)) + .collect(); + let passes = plan + .passes + .iter() + .map(|p| PassGpu::new(device, p, &formats, None)) + .collect::, _>>()?; + let final_pass = plan + .final_pass + .as_ref() + .map(|p| PassGpu::new(device, p, &formats, Some(format))) + .transpose()?; + Ok::<_, String>(Folder { + targets: plan.targets.clone(), + world_at: plan.world_at, + flipped_after_prepare: plan.flipped_after_prepare.clone(), + flipped_at_end: plan.flipped_at_end.clone(), + passes, + final_pass, + }) + }) + .collect::, _>>()?; + if let Some(error) = pollster::block_on(scope.pop()) { + return Err(error.to_string()); + } + Ok(Self { + id: id.to_owned(), + format, + common, + folders, + folder_for, + bound: None, + started: std::time::Instant::now(), + last: None, + frames: 0, + previous: None, + }) + } + + /// Run the passes of the world's dimension over its `color` and `depth` + /// (`size`) and return `post`'s bindings of the picture. + #[allow(clippy::too_many_arguments)] + pub fn run( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + color: &wgpu::TextureView, + depth: &wgpu::TextureView, + size: [u32; 2], + post: &super::world_post::Post, + frame: FrameUniform, + ) -> &wgpu::BindGroup { + let dimension = (frame.dimension[0] as i32).clamp(-1, 1); + let folder = self.folder_for[(dimension + 1) as usize].unwrap_or(0); + if self + .bound + .as_ref() + .is_none_or(|b| b.folder != folder || b.size != size) + { + self.bound = Some(self.bind(device, queue, folder, color, depth, size, post)); + } + let now = std::time::Instant::now(); + let frame_time = self.last.map_or(0.0, |t| (now - t).as_secs_f32()); + self.last = Some(now); + let inputs = Inputs::new( + &frame, + size, + self.started.elapsed().as_secs_f32(), + frame_time, + self.frames, + self.previous, + ); + self.previous = Some((inputs.view, inputs.projection, inputs.camera)); + self.frames = self.frames.wrapping_add(1); + let bound = self.bound.as_ref().expect("bound above"); + let folder = &self.folders[bound.folder]; + + // Iris clears both sides of the targets that ask for it. + for target in bound.targets.values() { + if let Some(clear) = target.clear { + for view in [&target.main_view, &target.alt_view] { + clear_view(encoder, view, clear); + } + } + } + let world_at = folder.world_at; + for (i, (pass, group)) in folder.passes.iter().zip(&bound.groups).enumerate() { + if i == world_at { + world_in(encoder, bound); + } + pass.draw(queue, encoder, bound, group, &self.common, &inputs); + } + if folder.passes.len() <= world_at { + world_in(encoder, bound); + } + if let (Some(pass), Some(view)) = (&folder.final_pass, &bound.final_target) { + let group = bound.groups.last().expect("final's group"); + pass.draw_to( + queue, + encoder, + &[Some(view)], + size, + group, + &self.common, + &inputs, + ); + } + { + let attachments = [Some(color_attachment(&bound.output_view, None))]; + let mut render = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Java shader pack picture"), + color_attachments: &attachments, + ..Default::default() + }); + render.set_pipeline(&bound.present.0); + render.set_bind_group(0, &bound.present.1, &[]); + render.draw(0..3, 0..1); + } + // The next frame starts unflipped: the last writes go back to main. + for index in &folder.flipped_at_end { + if let Some(t) = bound.targets.get(index) { + encoder.copy_texture_to_texture( + t.alt.as_image_copy(), + t.main.as_image_copy(), + t.main.size(), + ); + } + } + &bound.output + } + + #[allow(clippy::too_many_arguments)] + fn bind( + &self, + device: &wgpu::Device, + queue: &wgpu::Queue, + folder_index: usize, + color: &wgpu::TextureView, + world_depth: &wgpu::TextureView, + size: [u32; 2], + post: &super::world_post::Post, + ) -> Bound { + let folder = &self.folders[folder_index]; + let plan = folder; + let all = folder.passes.iter().chain(&folder.final_pass); + // Every target a pass reads or writes, and colortex0 (the world). + let mut used: BTreeSet = [0].into(); + for pass in all.clone() { + used.extend(pass.draw_buffers.iter().copied()); + used.extend(pass.samplers.iter().filter_map(|s| match s.source { + Source::Target(i) => Some(i), + _ => None, + })); + } + let texture = |label, format, usage, size: [u32; 2]| { + device.create_texture(&wgpu::TextureDescriptor { + label: Some(label), + size: wgpu::Extent3d { + width: size[0].max(1), + height: size[1].max(1), + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage, + view_formats: &[], + }) + }; + let target_usage = wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TEXTURE_BINDING + | wgpu::TextureUsages::COPY_SRC + | wgpu::TextureUsages::COPY_DST; + let targets: BTreeMap = used + .iter() + .filter_map(|&i| { + let settings = plan.targets.get(i as usize)?; + let format = target_format(&settings.format); + let target_size = match settings.size { + Some(horizon_shaderpack::pipeline::TargetSize::Relative(w, h)) if i != 0 => [ + (size[0] as f32 * w).round() as u32, + (size[1] as f32 * h).round() as u32, + ], + Some(horizon_shaderpack::pipeline::TargetSize::Pixels(w, h)) if i != 0 => { + [w, h] + } + _ => size, + }; + let main = texture("Java shader pack target", format, target_usage, target_size); + let alt = texture("Java shader pack target", format, target_usage, target_size); + // Iris: colortex0 to the fog colour, colortex1 to white. + let clear = settings.clear.then(|| { + let [r, g, b, a] = settings.clear_color.unwrap_or(match i { + 1 => [1.0; 4], + _ => [0.0; 4], + }); + wgpu::Color { + r: r.into(), + g: g.into(), + b: b.into(), + a: a.into(), + } + }); + Some(( + i, + Target { + format, + main_view: main.create_view(&Default::default()), + alt_view: alt.create_view(&Default::default()), + main, + alt, + clear, + }, + )) + }) + .collect(); + let depth = texture( + "Java shader pack depth", + wgpu::TextureFormat::R32Float, + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, + size, + ) + .create_view(&Default::default()); + let mut blank = Vec::new(); + let groups = all + .clone() + .map(|pass| pass.bind(device, queue, &targets, &depth, &self.common, &mut blank)) + .collect(); + + // The world goes into colortex0's side the gbuffers would write. + let colortex0 = &targets[&0]; + let world_pipeline = blit_pipeline( + device, + &self.common, + &self.common.world_layout, + "world_in", + &[colortex0.format, wgpu::TextureFormat::R32Float], + ); + let world_group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Java shader pack world"), + layout: &self.common.world_layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::TextureView(color), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::TextureView(world_depth), + }, + ], + }); + + let final_target = folder.final_pass.as_ref().map(|_| { + texture( + "Java shader pack final", + self.format, + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, + size, + ) + .create_view(&Default::default()) + }); + // Without `final`, Iris shows colortex0 as the last pass left it. + let shown = final_target.clone().unwrap_or_else(|| { + if plan.flipped_at_end.contains(&0) { + colortex0.alt_view.clone() + } else { + colortex0.main_view.clone() + } + }); + let present_pipeline = blit_pipeline( + device, + &self.common, + &self.common.present_layout, + "present", + &[self.format], + ); + let present_group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Java shader pack picture"), + layout: &self.common.present_layout, + entries: &[wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::TextureView(&shown), + }], + }); + let output_view = texture( + "Java shader pack picture", + self.format, + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, + size, + ) + .create_view(&Default::default()); + let output = post.bind(device, &output_view); + Bound { + folder: folder_index, + size, + targets, + depth, + groups, + world_in: (world_pipeline, world_group), + world_alt: plan.flipped_after_prepare.contains(&0), + final_target, + present: (present_pipeline, present_group), + output_view, + output, + _blank: blank, + } + } +} + +fn world_in(encoder: &mut wgpu::CommandEncoder, bound: &Bound) { + let colortex0 = &bound.targets[&0]; + let side = if bound.world_alt { + &colortex0.alt_view + } else { + &colortex0.main_view + }; + let attachments = [ + Some(color_attachment(side, None)), + Some(color_attachment(&bound.depth, None)), + ]; + let mut render = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Java shader pack world"), + color_attachments: &attachments, + ..Default::default() + }); + render.set_pipeline(&bound.world_in.0); + render.set_bind_group(0, &bound.world_in.1, &[]); + render.draw(0..3, 0..1); +} + +fn color_attachment( + view: &wgpu::TextureView, + clear: Option, +) -> wgpu::RenderPassColorAttachment<'_> { + wgpu::RenderPassColorAttachment { + view, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { + load: clear.map_or(wgpu::LoadOp::Load, wgpu::LoadOp::Clear), + store: wgpu::StoreOp::Store, + }, + } +} + +fn clear_view(encoder: &mut wgpu::CommandEncoder, view: &wgpu::TextureView, color: wgpu::Color) { + let attachments = [Some(color_attachment(view, Some(color)))]; + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Java shader pack clear"), + color_attachments: &attachments, + ..Default::default() + }); +} + +fn blit_pipeline( + device: &wgpu::Device, + common: &Common, + layout: &wgpu::BindGroupLayout, + entry: &str, + formats: &[wgpu::TextureFormat], +) -> wgpu::RenderPipeline { + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("Java shader pack blit"), + bind_group_layouts: &[Some(layout)], + ..Default::default() + }); + let targets: Vec> = + formats.iter().map(|&f| Some(f.into())).collect(); + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some(entry), + layout: Some(&pipeline_layout), + vertex: wgpu::VertexState { + module: &common.blit, + entry_point: Some("vs"), + compilation_options: Default::default(), + buffers: &[], + }, + primitive: Default::default(), + depth_stencil: None, + multisample: Default::default(), + fragment: Some(wgpu::FragmentState { + module: &common.blit, + entry_point: Some(entry), + compilation_options: Default::default(), + targets: &targets, + }), + multiview_mask: None, + cache: None, + }) +} + +impl Common { + fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Self { + let sampler = |filter, address, compare| { + device.create_sampler(&wgpu::SamplerDescriptor { + label: Some("Java shader pack"), + address_mode_u: address, + address_mode_v: address, + address_mode_w: address, + mag_filter: filter, + min_filter: filter, + compare, + ..Default::default() + }) + }; + use wgpu::{AddressMode::*, FilterMode::*}; + // Iris's noise: random RGB, opaque. + let mut seed = 0x2545_f491_u32; + let noise_bytes: Vec = (0..NOISE_SIZE * NOISE_SIZE * 4) + .map(|i| { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + if i % 4 == 3 { 255 } else { (seed >> 24) as u8 } + }) + .collect(); + let noise = device.create_texture(&wgpu::TextureDescriptor { + label: Some("noisetex"), + size: wgpu::Extent3d { + width: NOISE_SIZE, + height: NOISE_SIZE, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }); + queue.write_texture( + noise.as_image_copy(), + &noise_bytes, + wgpu::TexelCopyBufferLayout { + offset: 0, + bytes_per_row: Some(NOISE_SIZE * 4), + rows_per_image: None, + }, + noise.size(), + ); + // The full-screen triangle (0, 0), (2, 0), (0, 2): the pack's + // projection maps 0..1 to the screen. Texture coordinates are the + // position; colours white; normals up. + let vertices = [ + [ + [0.0, 0.0, 0.0, 1.0], + [2.0, 0.0, 0.0, 1.0], + [0.0, 2.0, 0.0, 1.0], + ], + [[1.0f32; 4]; 3], + [[0.0, 0.0, 1.0, 0.0]; 3], + [[0.0; 4]; 3], + ] + .map(|data| { + let buffer = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Java shader pack vertices"), + size: std::mem::size_of_val(&data) as u64, + usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + queue.write_buffer(&buffer, 0, bytemuck::cast_slice(&data)); + buffer + }); + let texture_entry = |binding, sample_type| wgpu::BindGroupLayoutEntry { + binding, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + count: None, + }; + let unfilterable = wgpu::TextureSampleType::Float { filterable: false }; + Self { + linear: sampler(Linear, ClampToEdge, None), + nearest: sampler(Nearest, ClampToEdge, None), + repeat: sampler(Linear, Repeat, None), + compare: sampler(Linear, ClampToEdge, Some(wgpu::CompareFunction::LessEqual)), + noise: noise.create_view(&Default::default()), + vertices, + world_layout: device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("Java shader pack world"), + entries: &[ + texture_entry(0, unfilterable), + texture_entry(1, wgpu::TextureSampleType::Depth), + ], + }), + present_layout: device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("Java shader pack picture"), + entries: &[texture_entry(0, unfilterable)], + }), + blit: device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("Java shader pack blit"), + source: wgpu::ShaderSource::Wgsl(BLIT.into()), + }), + } + } +} + +/// The view dimension and integer kind of a GLSL sampler type. +fn sampler_kind(kind: &str) -> Option<(wgpu::TextureViewDimension, Option, bool)> { + use wgpu::TextureViewDimension as D; + let signed = match kind.as_bytes().first() { + Some(b'i') => Some(true), + Some(b'u') => Some(false), + _ => None, + }; + let rest = kind + .trim_start_matches(['i', 'u']) + .strip_prefix("sampler")?; + let dimension = if rest.starts_with("2DArray") { + D::D2Array + } else if rest.starts_with("2D") || rest.starts_with("2DRect") { + D::D2 + } else if rest.starts_with("3D") { + D::D3 + } else if rest.starts_with("CubeArray") { + D::CubeArray + } else if rest.starts_with("Cube") { + D::Cube + } else if rest.starts_with("1D") { + D::D1 + } else { + return None; + }; + Some((dimension, signed, rest.ends_with("Shadow"))) +} + +impl PassGpu { + fn new( + device: &wgpu::Device, + program: &Program, + formats: &[wgpu::TextureFormat], + screen: Option, + ) -> Result { + let name = program.pass.program.clone(); + let link = &program.link; + let mut samplers = Vec::new(); + let mut seen = BTreeSet::new(); + for (sampler, kind) in program + .vertex + .interface + .samplers + .iter() + .chain(&program.fragment.interface.samplers) + { + if !seen.insert(sampler.clone()) { + continue; + } + let (dimension, signed, compare) = sampler_kind(kind) + .ok_or_else(|| format!("{name}: {kind} {sampler} isn't supported"))?; + let binding = |var: &str| { + link.bindings + .get(var) + .copied() + .ok_or_else(|| format!("{name}: {sampler} has no binding")) + }; + let var = program + .fragment + .interface + .sampler_vars + .get(sampler) + .or_else(|| program.vertex.interface.sampler_vars.get(sampler)) + .cloned() + .unwrap_or_else(|| format!("{sampler}_s")); + samplers.push(SamplerBinding { + source: source(sampler), + dimension, + signed, + texture: binding(&format!("{sampler}_t"))?, + sampler: binding(&var)?, + shared: var.starts_with("iris_").then_some(var), + compare, + }); + } + // The layout: the uniform block, then each sampler's texture and sampler. + let block = [&program.fragment, &program.vertex] + .into_iter() + .find_map(|t| uniform_block(&t.module)); + let mut entries = Vec::new(); + if block.is_some() { + entries.push(wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }, + count: None, + }); + } + let mut sampler_bindings = BTreeSet::new(); + for s in &samplers { + let (sample_type, filtering) = match (s.compare, s.source) { + (true, _) => (wgpu::TextureSampleType::Depth, None), + (false, Source::Target(i)) => { + let format = formats + .get(i as usize) + .copied() + .unwrap_or(wgpu::TextureFormat::Rgba8Unorm); + let t = sample_type(format); + ( + t, + Some(matches!( + t, + wgpu::TextureSampleType::Float { filterable: true } + )), + ) + } + (false, Source::Depth | Source::Shadow) => ( + wgpu::TextureSampleType::Float { filterable: false }, + Some(false), + ), + (false, _) => ( + wgpu::TextureSampleType::Float { filterable: true }, + Some(true), + ), + }; + let (sample_type, filtering) = match s.signed { + Some(true) => (wgpu::TextureSampleType::Sint, Some(false)), + Some(false) => (wgpu::TextureSampleType::Uint, Some(false)), + None => (sample_type, filtering), + }; + // A shared sampler filters as its name says. + let filtering = match s.shared.as_deref() { + Some("iris_compare") => None, + Some("iris_nearest") => Some(false), + Some(_) => Some(true), + None => filtering, + }; + entries.push(wgpu::BindGroupLayoutEntry { + binding: s.texture, + visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type, + view_dimension: s.dimension, + multisampled: false, + }, + count: None, + }); + if !sampler_bindings.insert(s.sampler) { + continue; + } + entries.push(wgpu::BindGroupLayoutEntry { + binding: s.sampler, + visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, + ty: wgpu::BindingType::Sampler(match filtering { + None => wgpu::SamplerBindingType::Comparison, + Some(true) => wgpu::SamplerBindingType::Filtering, + Some(false) => wgpu::SamplerBindingType::NonFiltering, + }), + count: None, + }); + } + let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some(&name), + entries: &entries, + }); + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some(&name), + bind_group_layouts: &[Some(&layout)], + ..Default::default() + }); + + // Vertex inputs, each from its own buffer of the full-screen triangle. + let inputs = vertex_inputs(&program.vertex.module); + if inputs.len() > MAX_ATTRIBUTES { + return Err(format!( + "{name}: more than {MAX_ATTRIBUTES} vertex attributes" + )); + } + let attribute_layouts: Vec<[wgpu::VertexAttribute; 1]> = inputs + .iter() + .map(|(_, location, format)| { + [wgpu::VertexAttribute { + format: *format, + offset: 0, + shader_location: *location, + }] + }) + .collect(); + let buffers: Vec> = attribute_layouts + .iter() + .map(|attributes| { + Some(wgpu::VertexBufferLayout { + array_stride: 16, + step_mode: wgpu::VertexStepMode::Vertex, + attributes, + }) + }) + .collect(); + let attributes = inputs + .iter() + .map(|(name, _, _)| match name.as_str() { + n if n == "iris_Vertex" || n.starts_with("iris_MultiTexCoord") => 0, + "iris_Color" => 1, + "iris_Normal" => 2, + _ => 3, + }) + .collect(); + + // Output location `l` writes target `draw_buffers[l]` (or the screen). + let outputs = &program.fragment.interface.outputs; + let target_states: Vec> = match screen { + Some(format) => vec![Some(wgpu::ColorTargetState { + format, + blend: None, + write_mask: if outputs.contains_key(&0) { + wgpu::ColorWrites::ALL + } else { + wgpu::ColorWrites::empty() + }, + })], + None => program + .draw_buffers + .iter() + .enumerate() + .map(|(l, &i)| { + Some(wgpu::ColorTargetState { + format: formats + .get(i as usize) + .copied() + .unwrap_or(wgpu::TextureFormat::Rgba8Unorm), + blend: None, + write_mask: if outputs.contains_key(&(l as u32)) { + wgpu::ColorWrites::ALL + } else { + wgpu::ColorWrites::empty() + }, + }) + }) + .collect(), + }; + let module = |t: &horizon_shaderpack::translate::Translated, label: &str| { + device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some(label), + source: wgpu::ShaderSource::Wgsl(t.wgsl.as_str().into()), + }) + }; + let vertex_module = module(&program.vertex, &format!("{name}.vsh")); + let fragment_module = module(&program.fragment, &format!("{name}.fsh")); + let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some(&name), + layout: Some(&pipeline_layout), + vertex: wgpu::VertexState { + module: &vertex_module, + entry_point: Some("main"), + compilation_options: Default::default(), + buffers: &buffers, + }, + primitive: Default::default(), + depth_stencil: None, + multisample: Default::default(), + fragment: Some(wgpu::FragmentState { + module: &fragment_module, + entry_point: Some("main"), + compilation_options: Default::default(), + targets: &target_states, + }), + multiview_mask: None, + cache: None, + }); + let uniform = block.as_ref().map(|(size, _)| { + ( + *size, + device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Java shader pack uniforms"), + size: u64::from(*size).max(16), + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }), + ) + }); + Ok(Self { + program: name, + pipeline, + layout, + samplers, + uniforms: block.map(|(_, members)| members).unwrap_or_default(), + uniform, + attributes, + draw_buffers: if screen.is_some() { + Vec::new() + } else { + program.draw_buffers.clone() + }, + reads_alt: program.pass.reads_alt.clone(), + scale: program.pass.scale.clamp(0.0, 1.0), + }) + } + + /// The pass's bind group over `targets`. + fn bind( + &self, + device: &wgpu::Device, + queue: &wgpu::Queue, + targets: &BTreeMap, + depth: &wgpu::TextureView, + common: &Common, + blank: &mut Vec, + ) -> wgpu::BindGroup { + let mut views = Vec::new(); + for s in &self.samplers { + let view = match (s.compare, s.source) { + (true, _) => blank_view(device, queue, blank, s, BlankKind::FarDepth), + (false, Source::Target(i)) if s.dimension == wgpu::TextureViewDimension::D2 => { + match targets.get(&i) { + Some(t) if self.reads_alt.contains(&i) => t.alt_view.clone(), + Some(t) => t.main_view.clone(), + None => blank_view(device, queue, blank, s, BlankKind::Color(false)), + } + } + (false, Source::Depth) if s.dimension == wgpu::TextureViewDimension::D2 => { + depth.clone() + } + (false, Source::Shadow) => blank_view(device, queue, blank, s, BlankKind::Far), + (false, Source::Noise) if s.dimension == wgpu::TextureViewDimension::D2 => { + common.noise.clone() + } + (false, Source::Blank(white)) => { + blank_view(device, queue, blank, s, BlankKind::Color(white)) + } + _ => blank_view(device, queue, blank, s, BlankKind::Color(false)), + }; + let sampler = match (s.compare, s.source) { + _ if s.shared.is_some() => match s.shared.as_deref() { + Some("iris_compare") => &common.compare, + Some("iris_nearest") => &common.nearest, + Some("iris_repeat") => &common.repeat, + _ => &common.linear, + }, + (true, _) => &common.compare, + _ if s.signed.is_some() => &common.nearest, + (false, Source::Noise) => &common.repeat, + (false, Source::Depth | Source::Shadow) => &common.nearest, + (false, Source::Target(i)) => { + match targets.get(&i).map(|t| sample_type(t.format)) { + Some(wgpu::TextureSampleType::Float { filterable: true }) | None => { + &common.linear + } + _ => &common.nearest, + } + } + _ => &common.linear, + }; + views.push((s.texture, view, s.sampler, sampler)); + } + let mut entries = Vec::new(); + if let Some((_, buffer)) = &self.uniform { + entries.push(wgpu::BindGroupEntry { + binding: 0, + resource: buffer.as_entire_binding(), + }); + } + let mut sampler_bindings = BTreeSet::new(); + for (texture, view, sampler_binding, sampler) in &views { + entries.push(wgpu::BindGroupEntry { + binding: *texture, + resource: wgpu::BindingResource::TextureView(view), + }); + if !sampler_bindings.insert(*sampler_binding) { + continue; + } + entries.push(wgpu::BindGroupEntry { + binding: *sampler_binding, + resource: wgpu::BindingResource::Sampler(sampler), + }); + } + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some(&self.program), + layout: &self.layout, + entries: &entries, + }) + } + + /// Draw into the targets of `draw_buffers`, on the side it doesn't read. + fn draw( + &self, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + bound: &Bound, + group: &wgpu::BindGroup, + common: &Common, + inputs: &Inputs, + ) { + let views: Vec> = self + .draw_buffers + .iter() + .map(|i| { + bound.targets.get(i).map(|t| { + if self.reads_alt.contains(i) { + &t.main_view + } else { + &t.alt_view + } + }) + }) + .collect(); + let size = self + .draw_buffers + .first() + .and_then(|i| bound.targets.get(i)) + .map_or(bound.size, |t| { + let s = t.main.size(); + [s.width, s.height] + }); + self.draw_to(queue, encoder, &views, size, group, common, inputs); + } + + #[allow(clippy::too_many_arguments)] + fn draw_to( + &self, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + views: &[Option<&wgpu::TextureView>], + size: [u32; 2], + group: &wgpu::BindGroup, + common: &Common, + inputs: &Inputs, + ) { + if let Some((block_size, buffer)) = &self.uniform { + let mut bytes = vec![0u8; *block_size as usize]; + for (name, slots) in &self.uniforms { + if let Some(values) = inputs.value(name) { + for (&(offset, int), value) in slots.iter().zip(values) { + let at = offset as usize..offset as usize + 4; + bytes[at].copy_from_slice(&if int { + (value as i32).to_ne_bytes() + } else { + value.to_ne_bytes() + }); + } + } + } + queue.write_buffer(buffer, 0, &bytes); + } + let attachments: Vec> = views + .iter() + .map(|v| v.map(|v| color_attachment(v, None))) + .collect(); + let mut render = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some(&self.program), + color_attachments: &attachments, + ..Default::default() + }); + render.set_pipeline(&self.pipeline); + render.set_bind_group(0, group, &[]); + for (slot, &data) in self.attributes.iter().enumerate() { + render.set_vertex_buffer(slot as u32, common.vertices[data].slice(..)); + } + let [w, h] = size.map(|v| (v as f32 * self.scale).max(1.0)); + render.set_viewport(0.0, 0.0, w, h, 0.0, 1.0); + render.draw(0..3, 0..1); + } +} + +enum BlankKind { + Color(bool), + /// An `R32Float` 1.0: the far plane. + Far, + /// A depth texture at the far plane, for comparison samplers. + FarDepth, +} + +/// A one-texel stand-in of the dimension `s` needs. +fn blank_view( + device: &wgpu::Device, + queue: &wgpu::Queue, + keep: &mut Vec, + s: &SamplerBinding, + kind: BlankKind, +) -> wgpu::TextureView { + use wgpu::TextureViewDimension as D; + let (format, bytes): (wgpu::TextureFormat, Vec) = match (kind, s.signed) { + (BlankKind::Color(_), Some(true)) => (wgpu::TextureFormat::R32Sint, vec![0; 4]), + (BlankKind::Color(_), Some(false)) => (wgpu::TextureFormat::R32Uint, vec![0; 4]), + (BlankKind::Color(white), None) => ( + wgpu::TextureFormat::Rgba8Unorm, + vec![if white { 255 } else { 0 }; 4], + ), + (BlankKind::Far, _) => (wgpu::TextureFormat::R32Float, 1.0f32.to_ne_bytes().to_vec()), + (BlankKind::FarDepth, _) => (wgpu::TextureFormat::Depth32Float, Vec::new()), + }; + let layers = match s.dimension { + D::Cube | D::CubeArray => 6, + _ => 1, + }; + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some("Java shader pack blank"), + size: wgpu::Extent3d { + width: 1, + height: 1, + depth_or_array_layers: layers, + }, + mip_level_count: 1, + sample_count: 1, + dimension: match s.dimension { + D::D1 => wgpu::TextureDimension::D1, + D::D3 => wgpu::TextureDimension::D3, + _ => wgpu::TextureDimension::D2, + }, + format, + usage: wgpu::TextureUsages::TEXTURE_BINDING + | wgpu::TextureUsages::COPY_DST + | wgpu::TextureUsages::RENDER_ATTACHMENT, + view_formats: &[], + }); + if bytes.is_empty() { + // Depth can't be written by a copy: clear it to the far plane. + let view = texture.create_view(&Default::default()); + let mut encoder = device.create_command_encoder(&Default::default()); + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Java shader pack blank"), + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: &view, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(1.0), + store: wgpu::StoreOp::Store, + }), + stencil_ops: None, + }), + ..Default::default() + }); + queue.submit([encoder.finish()]); + } else { + for layer in 0..layers { + queue.write_texture( + wgpu::TexelCopyTextureInfo { + texture: &texture, + mip_level: 0, + origin: wgpu::Origin3d { + x: 0, + y: 0, + z: layer, + }, + aspect: wgpu::TextureAspect::All, + }, + &bytes, + wgpu::TexelCopyBufferLayout { + offset: 0, + bytes_per_row: Some(bytes.len() as u32), + rows_per_image: None, + }, + wgpu::Extent3d { + width: 1, + height: 1, + depth_or_array_layers: 1, + }, + ); + } + } + let view = texture.create_view(&wgpu::TextureViewDescriptor { + dimension: Some(s.dimension), + ..Default::default() + }); + keep.push(texture); + view +} + +/// The uniform block of a translated program: its size and each member's +/// scalar slots. +fn uniform_block(module: &naga::Module) -> Option<(u32, Vec<(String, Vec)>)> { + let (_, var) = module + .global_variables + .iter() + .find(|(_, v)| v.space == naga::AddressSpace::Uniform)?; + let naga::TypeInner::Struct { members, span } = &module.types[var.ty].inner else { + return None; + }; + let members = members + .iter() + .map(|m| { + let mut slots = Vec::new(); + scalar_slots(module, m.ty, m.offset, &mut slots); + (m.name.clone().unwrap_or_default(), slots) + }) + .collect(); + Some((*span, members)) +} + +/// The scalars of a value of type `ty` at `offset`: vectors in order, +/// matrices column by column, arrays element by element. +fn scalar_slots( + module: &naga::Module, + ty: naga::Handle, + offset: u32, + out: &mut Vec, +) { + let int = |s: naga::Scalar| s.kind != naga::ScalarKind::Float; + match &module.types[ty].inner { + naga::TypeInner::Scalar(s) => out.push((offset, int(*s))), + naga::TypeInner::Vector { size, scalar } => { + out.extend((0..*size as u32).map(|i| (offset + 4 * i, int(*scalar)))) + } + naga::TypeInner::Matrix { columns, rows, .. } => { + let stride = if *rows == naga::VectorSize::Bi { 8 } else { 16 }; + for c in 0..*columns as u32 { + out.extend((0..*rows as u32).map(|r| (offset + c * stride + 4 * r, false))); + } + } + naga::TypeInner::Array { + base, + size: naga::ArraySize::Constant(n), + stride, + } => { + for i in 0..n.get() { + scalar_slots(module, *base, offset + i * stride, out); + } + } + _ => {} + } +} + +/// The vertex entry point's inputs: name, location and a vertex format +/// matching their scalar kind. +fn vertex_inputs(module: &naga::Module) -> Vec<(String, u32, wgpu::VertexFormat)> { + let Some(entry) = module.entry_points.first() else { + return Vec::new(); + }; + let format = |ty: naga::Handle| { + let kind = match &module.types[ty].inner { + naga::TypeInner::Scalar(s) | naga::TypeInner::Vector { scalar: s, .. } => s.kind, + _ => naga::ScalarKind::Float, + }; + match kind { + naga::ScalarKind::Sint => wgpu::VertexFormat::Sint32x4, + naga::ScalarKind::Uint => wgpu::VertexFormat::Uint32x4, + _ => wgpu::VertexFormat::Float32x4, + } + }; + let mut out = Vec::new(); + for arg in &entry.function.arguments { + match (&arg.binding, &module.types[arg.ty].inner) { + (Some(naga::Binding::Location { location, .. }), _) => out.push(( + arg.name.clone().unwrap_or_default(), + *location, + format(arg.ty), + )), + (None, naga::TypeInner::Struct { members, .. }) => { + for m in members { + if let Some(naga::Binding::Location { location, .. }) = &m.binding { + out.push((m.name.clone().unwrap_or_default(), *location, format(m.ty))); + } + } + } + _ => {} + } + } + out +} + +/// The game state Iris's uniforms describe, this frame. +struct Inputs { + view: Mat4, + /// GL's: near at -1, far at 1. + projection: Mat4, + camera: Vec3, + previous: (Mat4, Mat4, Vec3), + size: [f32; 2], + time: f32, + frame_time: f32, + frames: u32, + frame: FrameUniform, +} + +impl Inputs { + fn new( + frame: &FrameUniform, + size: [u32; 2], + time: f32, + frame_time: f32, + frames: u32, + previous: Option<(Mat4, Mat4, Vec3)>, + ) -> Self { + let view = Mat4::from_cols_array_2d(&frame.view); + // Horizon's reversed depth (z/w from 1 near to 0 far) to GL's + // (-1 near, 1 far): z' = w - 2z. + let to_gl = Mat4::from_cols( + Vec4::X, + Vec4::Y, + Vec4::new(0.0, 0.0, -2.0, 0.0), + Vec4::new(0.0, 0.0, 1.0, 1.0), + ); + let projection = to_gl * Mat4::from_cols_array_2d(&frame.projection); + let camera = Vec4::from(frame.camera_position).truncate(); + Self { + view, + projection, + camera, + previous: previous.unwrap_or((view, projection, camera)), + size: size.map(|v| v as f32), + time, + frame_time, + frames, + frame: *frame, + } + } + + /// The value of Iris uniform `name`, as its scalars in order; `None` for + /// one Horizon doesn't provide (it stays zero). + fn value(&self, name: &str) -> Option> { + if let Some(flag) = name.strip_prefix("iris_bool_") { + return self.value(flag); + } + let f = &self.frame; + let mat = |m: Mat4| m.to_cols_array().to_vec(); + let vec3 = |v: Vec3| v.to_array().to_vec(); + let in_view = |v: Vec3| (self.view * v.extend(0.0)).truncate() * 100.0; + let sun = Vec4::from(f.sun).truncate(); + let day = f.time[2].rem_euclid(1.0); + // The full-screen passes' own matrices: 0..1 onto the screen. + let screen = Mat4::orthographic_rh_gl(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); + Some(match name { + "gbufferModelView" => mat(self.view), + "gbufferModelViewInverse" => mat(self.view.inverse()), + "gbufferProjection" => mat(self.projection), + "gbufferProjectionInverse" => mat(self.projection.inverse()), + "gbufferPreviousModelView" => mat(self.previous.0), + "gbufferPreviousProjection" => mat(self.previous.1), + "cameraPosition" => vec3(self.camera), + "previousCameraPosition" => vec3(self.previous.2), + "cameraPositionInt" => vec3(self.camera.floor()), + "cameraPositionFract" => vec3(self.camera - self.camera.floor()), + "eyeAltitude" => vec![self.camera.y], + "viewWidth" => vec![self.size[0]], + "viewHeight" => vec![self.size[1]], + "aspectRatio" => vec![self.size[0] / self.size[1].max(1.0)], + "near" => vec![f.view_params[0]], + "far" => vec![f.view_params[2]], + "frameTimeCounter" => vec![self.time % 3600.0], + "frameTime" => vec![self.frame_time], + "frameCounter" => vec![(self.frames % 720_720) as f32], + "worldTime" => vec![(day * 24000.0).floor()], + "worldDay" => vec![0.0], + "sunAngle" => vec![day], + "shadowAngle" => vec![if day < 0.5 { day } else { day - 0.5 }], + "sunPosition" => vec3(in_view(sun)), + "moonPosition" => vec3(in_view(-sun)), + "shadowLightPosition" => vec3(in_view(if sun.y >= 0.0 { sun } else { -sun })), + "upPosition" => vec3(in_view(Vec3::Y)), + "moonPhase" => vec![f.moon[3]], + "rainStrength" | "wetness" => vec![f.weather[0]], + "thunderStrength" => vec![f.weather[1]], + "fogColor" => f.fog_color[..3].to_vec(), + "skyColor" => f.sky_color[..3].to_vec(), + "isEyeInWater" => vec![f.camera_state[0]], + "nightVision" => vec![f.camera_state[1]], + "darknessFactor" => vec![f.camera_state[2]], + "screenBrightness" => vec![1.0], + // Sky light at full with a sky, no block light: Horizon doesn't + // give the light at the eye yet. + "eyeBrightness" | "eyeBrightnessSmooth" => vec![0.0, 240.0 * f.dimension[1]], + "hasSkylight" => vec![f.dimension[1]], + "iris_ModelViewMatrix" | "iris_ModelViewMatrixInverse" => mat(Mat4::IDENTITY), + "iris_ProjectionMatrix" | "iris_ModelViewProjectionMatrix" => mat(screen), + "iris_ProjectionMatrixInverse" => mat(screen.inverse()), + "iris_TextureMatrix" => (0..8).flat_map(|_| mat(Mat4::IDENTITY)).collect(), + "iris_NormalMatrix" => Mat3::IDENTITY.to_cols_array().to_vec(), + "shadowModelView" + | "shadowModelViewInverse" + | "shadowProjection" + | "shadowProjectionInverse" => mat(Mat4::IDENTITY), + _ => return None, + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn iris_formats_become_renderable_wgpu_formats() { + use wgpu::TextureFormat as F; + for (iris, wgpu) in [ + ("RGBA", F::Rgba8Unorm), + ("RGB8", F::Rgba8Unorm), + ("R8", F::R8Unorm), + ("RG16F", F::Rg16Float), + ("R11F_G11F_B10F", F::Rgba16Float), + ("RGB8_SNORM", F::Rgba16Float), + ("RGBA16", F::Rgba16Float), + ("R32F", F::R32Float), + ("RGBA32F", F::Rgba32Float), + ("RGB10_A2", F::Rgb10a2Unorm), + ("R32UI", F::R32Uint), + ("RGBA16I", F::Rgba32Sint), + ] { + assert_eq!(target_format(iris), wgpu, "{iris}"); + } + assert_eq!( + sample_type(F::R32Float), + wgpu::TextureSampleType::Float { filterable: false } + ); + } + + #[test] + fn samplers_read_what_iris_binds() { + assert_eq!(source("colortex4"), Source::Target(4)); + assert_eq!(source("gaux1"), Source::Target(4)); + assert_eq!(source("gdepthtex"), Source::Depth); + assert_eq!(source("shadowtex1"), Source::Shadow); + assert_eq!(source("dhDepthTex0"), Source::Shadow); + assert_eq!(source("shadowcolor0"), Source::Blank(true)); + assert_eq!(source("cloudTexture"), Source::Blank(false)); + let (d, signed, shadow) = sampler_kind("usampler2D").unwrap(); + assert_eq!( + (d, signed, shadow), + (wgpu::TextureViewDimension::D2, Some(false), false) + ); + assert!(sampler_kind("sampler2DShadow").unwrap().2); + assert_eq!( + sampler_kind("sampler3D").unwrap().0, + wgpu::TextureViewDimension::D3 + ); + } + + #[test] + fn gl_projection_from_reversed_depth() { + // A point at the near plane is GL's -1, at the far plane 1. + let (near, far) = (0.05, 512.0); + let frame = FrameUniform { + view: Mat4::IDENTITY.to_cols_array_2d(), + projection: Mat4::perspective_rh(1.2, 1.5, far, near).to_cols_array_2d(), + ..Default::default() + }; + let inputs = Inputs::new(&frame, [300, 200], 0.0, 0.0, 0, None); + let depth = |d: f32| { + let clip = inputs.projection * Vec4::new(0.0, 0.0, -d, 1.0); + clip.z / clip.w + }; + assert!((depth(near) + 1.0).abs() < 1e-4, "{}", depth(near)); + assert!((depth(far) - 1.0).abs() < 1e-3, "{}", depth(far)); + assert_eq!(inputs.value("viewWidth"), Some(vec![300.0])); + assert_eq!( + inputs.value("iris_bool_hasSkylight"), + inputs.value("hasSkylight") + ); + } +} diff --git a/crates/horizon-client/src/java_pack.rs b/crates/horizon-client/src/java_pack.rs new file mode 100644 index 000000000..f051cf2b6 --- /dev/null +++ b/crates/horizon-client/src/java_pack.rs @@ -0,0 +1,165 @@ +//! Java (OptiFine/Iris) shader packs in `shaderpacks/` (docs/shader-packs.md): +//! a folder holding `shaders/`, or a `.zip`. `horizon-shaderpack` translates +//! their full-screen programs (`begin`, `prepare`, `deferred`, `composite`, +//! `final`) to WGSL, and `gfx/java_stages.rs` runs them over Horizon's world +//! as Iris runs them over its own. Geometry programs (`gbuffers_*`, +//! `shadow`) don't run yet: the world is drawn by Horizon. +use horizon_shaderpack::{ + JavaPack, + pipeline::{Program, TargetSettings}, + preprocess::Environment, +}; +use std::{ + collections::{BTreeMap, BTreeSet}, + path::Path, +}; + +/// The passes of one program folder (`world0`, or `""` for the pack's root). +pub struct JavaFolder { + pub folder: String, + pub targets: Vec, + /// `begin*`, `prepare*`, `deferred*`, `composite*`, in order. + pub passes: Vec, + /// How many of `passes` run before the world (`begin*`, `prepare*`): the + /// world's colour and depth enter `colortex0` and `depthtex*` after them. + pub world_at: usize, + /// The targets whose alternate side the world is written to. + pub flipped_after_prepare: BTreeSet, + pub final_pass: Option, + /// The targets whose alternate side holds the frame's last write. + pub flipped_at_end: BTreeSet, +} + +/// A loaded Java pack. +pub struct JavaPasses { + pub folders: Vec, +} + +impl JavaPasses { + /// The folder Iris uses in `dimension` (0 overworld, -1 the Nether, 1 the + /// End): `world` when the pack has one, else `world0`, else the root. + pub fn folder_for(&self, dimension: i8) -> Option { + let find = |name: &str| self.folders.iter().position(|f| f.folder == name); + find(&format!("world{dimension}")) + .or_else(|| find("world0")) + .or_else(|| find("")) + } +} + +/// A folder holding `shaders/`, or a zip. +pub fn is_java_pack(path: &Path) -> bool { + path.join("shaders").is_dir() + || path.is_file() + && path + .extension() + .is_some_and(|e| e.eq_ignore_ascii_case("zip")) +} + +/// The `MC_GL_VENDOR_*` name of a wgpu adapter (`AdapterInfo::vendor`, a +/// PCI id; Apple's GPUs report 0). +pub fn vendor_name(vendor: u32, adapter: &str) -> &'static str { + match vendor { + 0x10de => "NVIDIA Corporation", + 0x1002 | 0x1022 => "AMD", + 0x8086 => "Intel", + _ if adapter.starts_with("Apple") => "Apple", + _ => "Unknown", + } +} + +/// Open the pack at `path` and translate its full-screen passes with the +/// player's `values`; `vendor` and `renderer` name the GPU to the pack. +pub fn load( + path: &Path, + values: &BTreeMap, + vendor: &str, + renderer: &str, +) -> Result { + let pack = JavaPack::open(path)?; + let values = pack.values(values); + let env = Environment::iris(vendor, renderer, 4); + let properties = pack.properties(&values, &env)?; + let mut names: Vec<&str> = ["world0", "world-1", "world1"] + .into_iter() + .filter(|d| { + pack.programs + .iter() + .any(|p| p.starts_with(&format!("{d}/"))) + }) + .collect(); + if pack.programs.iter().any(|p| !p.contains('/')) { + names.push(""); + } + let mut folders = Vec::new(); + for name in names { + let plan = pack.passes(name, &values, &env, &properties)?; + let compile = |pass| { + let program = pack.compile(pass, &plan.targets, &values, &env, &properties)?; + if let Some((image, _)) = program.fragment.interface.images.first() { + return Err(format!( + "{}: images ({image}) aren't supported yet", + program.pass.program + )); + } + Ok::<_, String>(program) + }; + let early = plan.begin.iter().chain(&plan.prepare); + let passes = early + .clone() + .chain(&plan.deferred) + .chain(&plan.composite) + .map(compile) + .collect::, _>>()?; + let final_pass = plan.final_pass.as_ref().map(compile).transpose()?; + if passes.is_empty() && final_pass.is_none() { + continue; + } + folders.push(JavaFolder { + folder: name.to_owned(), + targets: plan.targets, + world_at: early.count(), + passes, + flipped_after_prepare: plan.flipped_after_prepare, + final_pass, + flipped_at_end: plan.flipped_at_end, + }); + } + if folders.is_empty() { + return Err("no deferred, composite or final programs".into()); + } + Ok(JavaPasses { folders }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn loads_the_full_screen_passes_of_a_java_pack() { + let dir = Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../horizon-shaderpack/tests/fixtures/passes"); + assert!(is_java_pack(&dir)); + let pack = load(&dir, &BTreeMap::new(), "Apple", "Apple M4 Pro").unwrap(); + let [folder] = &pack.folders[..] else { + panic!("one folder, the root"); + }; + assert_eq!(folder.folder, ""); + assert_eq!( + folder + .passes + .iter() + .map(|p| p.pass.program.as_str()) + .collect::>(), + ["deferred", "composite", "composite1"] + ); + assert_eq!(folder.world_at, 0); + assert!(folder.final_pass.is_some()); + // The overworld, the Nether and the End all use the root. + assert_eq!([-1, 0, 1].map(|d| pack.folder_for(d)), [Some(0); 3]); + assert_eq!( + vendor_name(0x10de, "NVIDIA GeForce RTX 4080"), + "NVIDIA Corporation" + ); + assert_eq!(vendor_name(0, "Apple M4 Pro"), "Apple"); + } +} diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index 266bde023..66c50d0e7 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -85,6 +85,7 @@ pub mod item_data; pub mod item_render; pub mod item_tooltip; pub mod items; +pub mod java_pack; pub mod java_text; pub mod key_names; pub mod keybinds; diff --git a/crates/horizon-client/src/shader_pack.rs b/crates/horizon-client/src/shader_pack.rs index 55fdb992f..ec86edc77 100644 --- a/crates/horizon-client/src/shader_pack.rs +++ b/crates/horizon-client/src/shader_pack.rs @@ -109,6 +109,8 @@ pub struct ShaderPack { pub info: Info, pub stages: Vec, pub options: Vec, + /// A Java (OptiFine/Iris) pack's translated passes, instead of `stages`. + pub java: Option, dir: PathBuf, stamp: Stamp, } @@ -120,6 +122,11 @@ pub struct Stamp(Vec<(PathBuf, u64, Option)>); impl Stamp { pub fn of(dir: &Path) -> Self { let mut files = Vec::new(); + if let Ok(meta) = fs::metadata(dir) + && meta.is_file() + { + files.push((dir.to_path_buf(), meta.len(), meta.modified().ok())); + } let mut stack = vec![dir.to_path_buf()]; while let Some(d) = stack.pop() { let Ok(entries) = fs::read_dir(&d) else { @@ -141,23 +148,41 @@ impl Stamp { } /// The packs in `dir` (`shaderpacks/`): folders holding a `pack.toml`, by -/// folder name, with their manifest or why it can't be read. +/// folder name, with their manifest or why it can't be read, and Java packs +/// (folders holding `shaders/`, zips). pub fn list(dir: &Path) -> Vec<(String, Result)> { let mut packs: Vec<_> = fs::read_dir(dir) .into_iter() .flatten() .flatten() - .filter(|e| e.path().join("pack.toml").is_file()) - .map(|e| { + .filter_map(|e| { let id = e.file_name().to_string_lossy().into_owned(); - let info = manifest(&e.path()).map(|m| m.pack); - (id, info) + let path = e.path(); + if path.join("pack.toml").is_file() { + Some((id, manifest(&path).map(|m| m.pack))) + } else if crate::java_pack::is_java_pack(&path) { + Some((id.clone(), Ok(java_info(id)))) + } else { + None + } }) .collect(); packs.sort_by(|a, b| a.0.cmp(&b.0)); packs } +/// A Java pack has no manifest: its name is its file name. +fn java_info(name: String) -> Info { + Info { + name, + description: String::new(), + authors: Vec::new(), + version: String::new(), + license: String::new(), + api: API, + } +} + fn manifest(dir: &Path) -> Result { let text = fs::read_to_string(dir.join("pack.toml")).map_err(|e| format!("pack.toml: {e}"))?; let manifest: Manifest = toml::from_str(&text).map_err(|e| format!("pack.toml: {e}"))?; @@ -228,10 +253,38 @@ impl ShaderPack { info: manifest.pack, stages, options, + java: None, dir: dir.to_path_buf(), stamp, }) } + /// Load the pack at `path` (a folder or a zip): a Horizon pack, or a + /// Java pack translated for the GPU `vendor` and `renderer` names. + pub fn load_any( + path: &Path, + values: &BTreeMap, + vendor: &str, + renderer: &str, + ) -> Result { + if path.join("pack.toml").is_file() || !crate::java_pack::is_java_pack(path) { + return Self::load(path, values); + } + let stamp = Stamp::of(path); + let java = crate::java_pack::load(path, values, vendor, renderer)?; + let id = path + .file_name() + .map(|n| n.to_string_lossy().into_owned()) + .unwrap_or_default(); + Ok(Self { + info: java_info(id.clone()), + id, + stages: Vec::new(), + options: Vec::new(), + java: Some(java), + dir: path.to_path_buf(), + stamp, + }) + } /// The pipeline-overridable constants every stage gets: the option values. pub fn constants(&self) -> Vec<(&str, f64)> { self.options diff --git a/crates/horizon-shaderpack/src/pipeline.rs b/crates/horizon-shaderpack/src/pipeline.rs index f9dc00cc5..dfff469c4 100644 --- a/crates/horizon-shaderpack/src/pipeline.rs +++ b/crates/horizon-shaderpack/src/pipeline.rs @@ -8,8 +8,11 @@ //! and `CompositeRenderer` do. use crate::{ - JavaPack, options::OptionValues, preprocess::Environment, properties::LEGACY_TARGETS, - properties::ShaderProperties, + JavaPack, + options::OptionValues, + preprocess::Environment, + properties::{LEGACY_TARGETS, ShaderProperties}, + translate::{Link, Translated}, }; use std::collections::{BTreeMap, BTreeSet}; @@ -478,6 +481,91 @@ impl JavaPack { } } +/// A pass's programs translated, with the targets it draws to. +pub struct Program { + pub pass: Pass, + pub vertex: Translated, + pub fragment: Translated, + /// The target each output location writes: the pass's directive, or + /// as Iris does without one, location `i` to target `i`. + pub draw_buffers: Vec, + /// The bindings both programs share. + pub link: Link, +} + +impl JavaPack { + /// Translate `pass`'s `.vsh` and `.fsh`. + pub fn compile( + &self, + pass: &Pass, + targets: &[TargetSettings], + values: &OptionValues, + env: &Environment, + properties: &ShaderProperties, + ) -> Result { + let mut link = Link { + image_formats: properties.image_formats(), + shared_samplers: shared_samplers(targets), + ..Link::default() + }; + let vertex = self.translate(&format!("{}.vsh", pass.program), values, env, &mut link)?; + let fragment = self.translate(&format!("{}.fsh", pass.program), values, env, &mut link)?; + let draw_buffers = match &pass.draw_buffers { + Some(buffers) => buffers.clone(), + None => (0..fragment.interface.outputs.keys().max().map_or(1, |m| m + 1)).collect(), + }; + Ok(Program { + pass: pass.clone(), + vertex, + fragment, + draw_buffers, + link, + }) + } +} + +/// Iris's sampler states for the textures it binds: colour targets +/// linear (nearest for integer formats, and for 32-bit floats, which wgpu +/// can't filter everywhere), depth nearest, noise repeating. For +/// [`Link::shared_samplers`]. +pub fn shared_samplers(targets: &[TargetSettings]) -> BTreeMap { + let mut out = BTreeMap::new(); + for (i, t) in targets.iter().enumerate() { + let f = &t.format; + let class = if f.ends_with("32F") || f.ends_with('I') { + "iris_nearest" + } else { + "iris_linear" + }; + out.insert(format!("colortex{i}"), class.to_owned()); + if let Some(legacy) = LEGACY_TARGETS.get(i) { + out.insert((*legacy).to_owned(), class.to_owned()); + } + } + for name in [ + "depthtex0", + "depthtex1", + "depthtex2", + "gdepthtex", + "shadowtex0", + "shadowtex1", + "shadow", + "watershadow", + "dhDepthTex", + "dhDepthTex0", + "dhDepthTex1", + "vxDepthTexOpaque", + "vxDepthTexTrans", + ] { + out.insert(name.to_owned(), "iris_nearest".to_owned()); + } + for name in ["shadowcolor", "shadowcolor0", "shadowcolor1"] { + out.insert(name.to_owned(), "iris_linear".to_owned()); + } + out.insert("noisetex".to_owned(), "iris_repeat".to_owned()); + out +} + fn toggle(set: &mut BTreeSet, buffer: u32) { if !set.remove(&buffer) { set.insert(buffer); diff --git a/crates/horizon-shaderpack/src/translate.rs b/crates/horizon-shaderpack/src/translate.rs index fdd3ad128..f9a61169e 100644 --- a/crates/horizon-shaderpack/src/translate.rs +++ b/crates/horizon-shaderpack/src/translate.rs @@ -50,6 +50,11 @@ pub struct Link { pub uniforms: Vec<(String, String)>, /// Storage formats of images whose declaration has none. pub image_formats: BTreeMap, + /// Samplers shared by texture name (`colortex0` → `iris_linear`), so a + /// pass binds a few samplers rather than one per texture (Metal allows + /// 16). Once any is named, comparison samplers share `iris_compare` and + /// integer ones `iris_nearest`; textures not named keep `{name}_s`. + pub shared_samplers: BTreeMap, } impl Link { @@ -81,8 +86,11 @@ impl Link { #[derive(Default, Debug)] pub struct Interface { /// `(name, kind)` of each sampler the program reads (`colortex0`, - /// `sampler2D`); its bindings are `{name}_t` and `{name}_s` in the [`Link`]. + /// `sampler2D`); its bindings are `{name}_t` and its sampler's in the [`Link`]. pub samplers: Vec<(String, String)>, + /// Each sampler's sampler variable: `{name}_s`, or a shared one + /// ([`Link::shared_samplers`]). + pub sampler_vars: BTreeMap, /// `(name, format)` of each image. pub images: Vec<(String, String)>, /// Uniform names the program declares or uses. @@ -208,16 +216,18 @@ const IRIS_IMPLICIT: &[(&str, &str, bool)] = &[ ("alphaTestRef", "float", false), ]; +/// Built-ins naga lacks. `isnan` and `isinf` test the bits: Metal compiles +/// with fast math, which folds `x != x` to false. const HELPERS: &str = "\ struct iris_FogParameters { vec4 color; float density; float start; float end; float scale; }; -bool iris_isnan(float x) { return x != x; } -bvec2 iris_isnan(vec2 x) { return notEqual(x, x); } -bvec3 iris_isnan(vec3 x) { return notEqual(x, x); } -bvec4 iris_isnan(vec4 x) { return notEqual(x, x); } -bool iris_isinf(float x) { return abs(x) > 3.4028235e38; } -bvec2 iris_isinf(vec2 x) { return greaterThan(abs(x), vec2(3.4028235e38)); } -bvec3 iris_isinf(vec3 x) { return greaterThan(abs(x), vec3(3.4028235e38)); } -bvec4 iris_isinf(vec4 x) { return greaterThan(abs(x), vec4(3.4028235e38)); } +bool iris_isnan(float x) { return (floatBitsToUint(x) & 0x7fffffffu) > 0x7f800000u; } +bvec2 iris_isnan(vec2 x) { return greaterThan(floatBitsToUint(x) & 0x7fffffffu, uvec2(0x7f800000u)); } +bvec3 iris_isnan(vec3 x) { return greaterThan(floatBitsToUint(x) & 0x7fffffffu, uvec3(0x7f800000u)); } +bvec4 iris_isnan(vec4 x) { return greaterThan(floatBitsToUint(x) & 0x7fffffffu, uvec4(0x7f800000u)); } +bool iris_isinf(float x) { return (floatBitsToUint(x) & 0x7fffffffu) == 0x7f800000u; } +bvec2 iris_isinf(vec2 x) { return equal(floatBitsToUint(x) & 0x7fffffffu, uvec2(0x7f800000u)); } +bvec3 iris_isinf(vec3 x) { return equal(floatBitsToUint(x) & 0x7fffffffu, uvec3(0x7f800000u)); } +bvec4 iris_isinf(vec4 x) { return equal(floatBitsToUint(x) & 0x7fffffffu, uvec4(0x7f800000u)); } float iris_modf(float x, out float i) { i = trunc(x); return x - i; } vec2 iris_modf(vec2 x, out vec2 i) { i = trunc(x); return x - i; } vec3 iris_modf(vec3 x, out vec3 i) { i = trunc(x); return x - i; } @@ -288,7 +298,10 @@ struct Legaliser<'a> { link: &'a mut Link, interface: Interface, /// Global samplers: name → combined type. - samplers: HashMap, + /// Sampler uniforms: their combined type and sampler variable. + samplers: HashMap, + /// Sampler variables declared so far (shared ones once). + sampler_vars: HashSet, /// User functions → indices of their sampler parameters. sampler_params: HashMap>, functions: HashSet, @@ -484,6 +497,7 @@ impl<'a> Legaliser<'a> { link, interface: Interface::default(), samplers: HashMap::new(), + sampler_vars: HashSet::new(), sampler_params: HashMap::new(), functions: HashSet::new(), scopes: Vec::new(), @@ -601,17 +615,35 @@ impl<'a> Legaliser<'a> { || has(ast::StorageQualifierData::Out) && self.stage == Stage::Vertex || has(ast::StorageQualifierData::In) && self.stage == Stage::Fragment; if has(ast::StorageQualifierData::Uniform) { - if let Some(&(combined, texture, sampler, _)) = opaque(&ty) { + if let Some(&(combined, texture, sampler, compare)) = opaque(&ty) { for (name, _) in &names { let t = self.link.binding(&format!("{name}_t")); - let s = self.link.binding(&format!("{name}_s")); self.header.push(format!( "layout(set = 0, binding = {t}) uniform {texture} {name}_t;" )); - self.header.push(format!( - "layout(set = 0, binding = {s}) uniform {sampler} {name}_s;" - )); - self.samplers.insert(name.clone(), combined); + let shared = &self.link.shared_samplers; + let var = if shared.is_empty() { + format!("{name}_s") + } else if compare { + "iris_compare".to_owned() + } else if combined.starts_with(['i', 'u']) { + "iris_nearest".to_owned() + } else { + shared + .get(name) + .cloned() + .unwrap_or_else(|| format!("{name}_s")) + }; + if self.sampler_vars.insert(var.clone()) { + let s = self.link.binding(&var); + self.header.push(format!( + "layout(set = 0, binding = {s}) uniform {sampler} {var};" + )); + } + self.interface + .sampler_vars + .insert(name.clone(), var.clone()); + self.samplers.insert(name.clone(), (combined, var)); self.interface .samplers .push((name.clone(), combined.to_owned())); @@ -1034,13 +1066,14 @@ impl<'a> Legaliser<'a> { let combined = match self.local(name) { Some(Local::Sampler(c)) => Some(c), Some(Local::Plain) => return None, - None => self.samplers.get(name).copied(), + None => self.samplers.get(name).map(|(c, _)| *c), }; if let Some(c) = combined { - return Some(call( - c, - vec![var(&format!("{name}_t")), var(&format!("{name}_s"))], - )); + let sampler = match self.samplers.get(name) { + Some((_, v)) if self.local(name).is_none() => v.clone(), + _ => format!("{name}_s"), + }; + return Some(call(c, vec![var(&format!("{name}_t")), var(&sampler)])); } self.used.insert(name.to_owned()); if let Some((_, decl)) = LEGACY_UNIFORMS.iter().find(|(n, _)| *n == name) { @@ -1263,6 +1296,15 @@ impl<'a> Legaliser<'a> { if !self.errors.is_empty() { return Err(self.errors.join("\n")); } + if self.stage == Stage::Vertex { + // GL's clip space to wgpu's: y flipped, so texture row 0 stays + // the bottom of the picture as in GL (texture coordinates and + // `gl_FragCoord` then agree with the pack's), and z from + // [-w, w] to [0, w]. + self.epilogue.push("gl_Position.y = -gl_Position.y;".into()); + self.epilogue + .push("gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;".into()); + } let mut out = String::from("#version 450 core\n"); out.push_str(HELPERS); if !self.link.uniforms.is_empty() { diff --git a/crates/horizon-shaderpack/tests/pack.rs b/crates/horizon-shaderpack/tests/pack.rs index ec193847a..ce5115b37 100644 --- a/crates/horizon-shaderpack/tests/pack.rs +++ b/crates/horizon-shaderpack/tests/pack.rs @@ -368,5 +368,31 @@ fn real_packs_passes() { ); } assert!(passes.final_pass.is_some() || !passes.composite.is_empty()); + // Every planned pass translates, its outputs within its targets. + for p in passes + .begin + .iter() + .chain(&passes.prepare) + .chain(&passes.deferred) + .chain(&passes.composite) + .chain(&passes.final_pass) + { + let program = pack + .compile(p, &passes.targets, &values, &env(), &properties) + .unwrap(); + assert!( + program + .fragment + .interface + .outputs + .keys() + .all(|&l| (l as usize) < program.draw_buffers.len()) + || p.program.ends_with("final"), + "{}: outputs {:?}, targets {:?}", + p.program, + program.fragment.interface.outputs, + program.draw_buffers + ); + } } } diff --git a/crates/horizon-shaderpack/tests/translate.rs b/crates/horizon-shaderpack/tests/translate.rs index 3e58166c8..da8dc7e32 100644 --- a/crates/horizon-shaderpack/tests/translate.rs +++ b/crates/horizon-shaderpack/tests/translate.rs @@ -249,6 +249,13 @@ fn translates_a_compatibility_profile_pass() { assert!(!fs.glsl.contains("albedo = vec4(0.);"), "{}", fs.glsl); assert!(fs.glsl.contains("iris_isnan(albedo.r)"), "{}", fs.glsl); assert!(fs.wgsl.contains("@fragment")); + // GL's clip space becomes wgpu's after the program's own `main`. + assert!( + vs.glsl + .contains("gl_Position.y = -gl_Position.y;\n gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n}"), + "{}", + vs.glsl + ); } #[test] @@ -354,3 +361,47 @@ fn real_packs_translate() { } assert!(failures.is_empty(), "{}", failures.join("\n\n")); } + +#[test] +fn textures_share_samplers_by_name() { + let mut link = Link { + shared_samplers: [("colortex0", "iris_linear"), ("colortex1", "iris_linear")] + .map(|(a, b)| (a.to_owned(), b.to_owned())) + .into(), + ..Link::default() + }; + let fs = run( + Stage::Fragment, + "uniform sampler2D colortex0;\nuniform sampler2D colortex1;\nuniform sampler2D custom;\n\ + uniform sampler2DShadow shadowtex0;\nvarying vec2 uv;\n\ + void main() { gl_FragColor = texture2D(colortex0, uv) + texture2D(colortex1, uv)\n\ + + texture2D(custom, uv) + shadow2D(shadowtex0, vec3(uv, 0.5)); }", + &mut link, + ) + .unwrap(); + assert_eq!( + fs.glsl.matches("uniform sampler iris_linear;").count(), + 1, + "{}", + fs.glsl + ); + assert!( + fs.glsl.contains("sampler2D(colortex1_t, iris_linear)"), + "{}", + fs.glsl + ); + assert!(fs.glsl.contains("uniform sampler custom_s;"), "{}", fs.glsl); + assert!( + fs.glsl.contains("uniform samplerShadow iris_compare;"), + "{}", + fs.glsl + ); + assert_eq!(fs.interface.sampler_vars["colortex0"], "iris_linear"); + // Three sampler bindings for four textures. + assert_eq!( + bindings(&fs).keys().filter(|k| !k.ends_with("_t")).count(), + 3, + "{:?}", + bindings(&fs) + ); +} diff --git a/docs/shader-packs.md b/docs/shader-packs.md index bd9646df8..812b540f0 100644 --- a/docs/shader-packs.md +++ b/docs/shader-packs.md @@ -19,8 +19,9 @@ Both are off by default. With no pack selected the renderer draws exactly what i | Java shader packs: preprocessor (`crates/horizon-shaderpack`: includes, options, Iris's macros) | done: every program of the three test packs preprocesses | | Java shader packs: GLSL → WGSL (`horizon_shaderpack::translate`: syntax-tree legaliser, naga) | done: the 464 programs the three test packs enable by default translate (below) | | Java shader packs: `shaders.properties` (`horizon_shaderpack::properties`: screens, sliders, profiles, `program.*.enabled`, image formats, blend modes) | done, no screen yet: read like Iris for the three test packs | -| Java shader packs: full-screen pass plan (`horizon_shaderpack::pipeline`: `DRAWBUFFERS`/`RENDERTARGETS`, `colortexN` formats and clearing, buffer flips) | done, not drawn yet: planned for the three test packs | -| Java shader packs: Iris pipeline emulation | planned | +| Java shader packs: full-screen pass plan (`horizon_shaderpack::pipeline`: `DRAWBUFFERS`/`RENDERTARGETS`, `colortexN` formats and clearing, buffer flips) | done: planned for the three test packs | +| Java shader packs: full-screen passes drawn (`java_pack.rs`, `gfx/java_stages.rs`: `begin`, `prepare`, `deferred*`, `composite*`, `final` over Horizon's world) | first slice: BSL and Complementary run (below) | +| Java shader packs: `gbuffers_*`, `shadow`, custom textures, images, compute, option screen | planned | ## How the renderer draws a frame today @@ -328,6 +329,42 @@ The spike also showed what the real translator needs that a token rewriter does uniform), types (which `texture` call returns a float) and declarations split across `#if`s, so it should rewrite a GLSL syntax tree after preprocessing, not tokens. +### Running a Java pack's full-screen passes + +A folder holding `shaders/`, or a `.zip`, in `shaderpacks/` shows in the Shader Packs screen next to Horizon packs. +Picking one translates every planned pass of each dimension folder (`java_pack.rs`) and runs them each frame +(`gfx/java_stages.rs`): + +1. the targets the pack clears are cleared; +2. `begin*` and `prepare*` run, then Horizon's world enters `colortex0` and `depthtex0..2` on the side the flips give; +3. `deferred*` and `composite*` run in order, each reading the side its plan gives and writing the other; +4. `final` draws into a target that replaces the world's picture, where Horizon packs' `composite` output goes: the + hand, menus and HUD are drawn over it. + +What makes Horizon look like OpenGL to the pack: + +- **Layout.** The translator ends every vertex shader by flipping `gl_Position.y` and moving depth from `[-w, w]` to + `[0, w]`, so the targets keep OpenGL's layout (row 0 at the bottom). The world is flipped going in, `final` coming out. +- **Depth.** Horizon's depth is reversed (1 near, 0 sky); the world goes in as `1 - depth`, and the matrices the pack + gets (`gbufferProjection` and its inverse, previous frame too) are OpenGL's, rebuilt from Horizon's. +- **Formats.** Iris formats become renderable wgpu formats: 8-bit unorm ones and `RGB10_A2` stay, `*32F` stay (sampled + unfiltered), integer formats become 32-bit, the rest (`R11F_G11F_B10F`, `*16F`, `*16`) 16-bit float, with the + channel count of the name. +- **Samplers.** Metal allows 16 samplers per stage and Complementary binds 23 textures, so textures share four + samplers by kind (`iris_linear`, `iris_nearest` for depth and 32-bit or integer targets, `iris_repeat` for + `noisetex`, `iris_compare` for shadow lookups) instead of one each. +- **`isnan`/`isinf`** test the float's bits: Metal compiles with fast math, which folds `x != x` to false, and a NaN + then stays forever in Complementary's TAA history (the picture turns black). +- **Uniforms.** Matrices, camera, view size, time, sun and moon, weather, fog and sky colour, eye state come from the + frame; `eyeBrightness(Smooth)` is `(0, 240)` under the sky; `noisetex` is a fixed 256² noise. + +Measured on the Mac mini (M4 Pro) with the packs' default options: BSL v10.1.8 and Complementary Reimagined r5.9.3 +grade and light Horizon's world as their post-processing would. Sildur's Vibrant v2.02 draws only its sky: its +`deferred` reads the albedo `gbuffers_*` write to `colortex4`. Without `gbuffers_*` and `shadow`, no pack has its +shadows, water or sky look yet; Distant Horizons' and Voxy's depth read as far. Not supported yet: images and +compute (`.csh`; a pack that needs them doesn't load), mipmapped targets (`colortexNMipmapEnabled` is read but +ignored), custom textures, custom uniforms, the option screen; shadow matrices are identity. + ### Order to implement 1. **Translator crate** (`horizon-shaderpack`): the preprocessor (includes, option `#define`s with the player's From 9cec0dfb45f805dba77c8d2d961b605d5b43ab15 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 02:25:54 +0200 Subject: [PATCH 06/97] World list: hovered rows show vanilla's join arrow, a click on the icon joins WorldListEntry.extractContent dims a hovered row's icon (0xA0909090) and draws world_list/join over it, join_highlighted under the pointer; mouseClicked joins the world when the click lands on the icon. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/title.rs | 69 +++++++++++++++++------- crates/horizon-client/src/title/tests.rs | 24 +++++++++ 2 files changed, 74 insertions(+), 19 deletions(-) diff --git a/crates/horizon-client/src/title.rs b/crates/horizon-client/src/title.rs index aa78981f5..bb19d09d6 100644 --- a/crates/horizon-client/src/title.rs +++ b/crates/horizon-client/src/title.rs @@ -337,6 +337,26 @@ impl Title { let left = (w / 2.0).floor() - ROW_WIDTH / 2.0; (LIST_TOP, h - LIST_BOTTOM, left, content) } + /// The visible row under the pointer and the pointer relative to its + /// content (`AbstractSelectionList.getEntryAtPosition`). + fn world_row_at( + &self, + worlds: &[WorldEntry], + (x, y): (f32, f32), + size: (f32, f32), + ) -> Option<(usize, (f32, f32))> { + let (top, bottom, left, _) = self.world_list(worlds, size); + if y < top || y >= bottom || x < left || x >= left + ROW_WIDTH { + return None; + } + let offset = self.scroll.get().amount.trunc(); + let row = ((y - top - 2.0 + offset) / ROW_HEIGHT).floor(); + if row < 0.0 || row as usize >= self.visible_worlds(worlds).count() { + return None; + } + let content_y = top + 4.0 + row * ROW_HEIGHT - offset; + Some((row as usize, (x - left - 2.0, y - content_y))) + } /// The pointer moved with the button held (`mouseDragged`). pub fn drag(&mut self, cursor: (f32, f32), size: (f32, f32)) { if let Mode::Multiplayer(screen) = &mut self.mode { @@ -819,6 +839,7 @@ impl Title { // Entry content sits 2 pixels inside its row. let x = left + 2.0; let mut slot = 0; + let hovered = self.world_row_at(worlds, cursor, (w, h)); for (row, (i, world)) in self.visible_worlds(worlds).enumerate() { let y = top + 4.0 + row as f32 * ROW_HEIGHT - offset; if y + 32.0 <= top || y >= bottom { @@ -884,6 +905,18 @@ impl Title { ); self.icon(atlas, batch, world, slot, x, y); slot = (slot + 1) % crate::hud::WORLD_ICON_SLOTS; + // WorldListEntry.extractContent: a hovered row dims its + // icon under the join arrow, lit under the pointer. + if let Some((_, (rx, ry))) = hovered.filter(|(r, _)| *r == row) { + batch.fill(atlas, x, y, 32.0, 32.0, 0xA0909090); + let on_icon = (0.0..32.0).contains(&rx) && (0.0..32.0).contains(&ry); + let sprite = if on_icon { + "gui/sprites/world_list/join_highlighted" + } else { + "gui/sprites/world_list/join" + }; + batch.sprite_scaled(atlas, sprite, x, y, 32.0, 32.0, u32::MAX); + } } // Clip list quads, including their UVs, to the content area. for quad in batch.vertices[first..].as_chunks_mut::<6>().0 { @@ -1221,25 +1254,23 @@ impl Title { { return Action::None; } - if y >= top && y < bottom && x >= left && x < left + ROW_WIDTH { - let offset = self.scroll.get().amount.trunc(); - let row = ((y - top - 2.0 + offset) / ROW_HEIGHT).floor(); - if row >= 0.0 { - let Mode::Select { worlds, selected } = &self.mode else { - unreachable!() - }; - let chosen = self - .visible_worlds(worlds) - .nth(row as usize) - .map(|(index, world)| (index, world.folder.clone())); - if let Some((index, folder)) = chosen { - if *selected == Some(index) { - return Action::Play { folder }; - } - if let Mode::Select { selected, .. } = &mut self.mode { - *selected = Some(index); - } - } + let Mode::Select { worlds, selected } = &self.mode else { + unreachable!() + }; + let chosen = self + .world_row_at(worlds, (x, y), (w, h)) + .and_then(|(row, rel)| { + let (index, world) = self.visible_worlds(worlds).nth(row)?; + Some((index, world.folder.clone(), rel)) + }); + if let Some((index, folder, (rx, ry))) = chosen { + // WorldListEntry.mouseClicked: the icon joins at once. + let on_icon = (0.0..32.0).contains(&rx) && (0.0..32.0).contains(&ry); + if on_icon || *selected == Some(index) { + return Action::Play { folder }; + } + if let Mode::Select { selected, .. } = &mut self.mode { + *selected = Some(index); } } Action::None diff --git a/crates/horizon-client/src/title/tests.rs b/crates/horizon-client/src/title/tests.rs index 9a671601a..744e9f700 100644 --- a/crates/horizon-client/src/title/tests.rs +++ b/crates/horizon-client/src/title/tests.rs @@ -483,6 +483,30 @@ fn classic_world_selection_is_silent_until_the_entry_launches() { ); } +#[test] +fn clicking_a_world_icon_joins_it_at_once() { + let mut f = Fixture::new(); + f.world("World", "World"); + f.title.enter_singleplayer(); + // WorldListEntry.mouseOverIcon: the row's 32x32 icon, left of its text. + let (icon_x, icon_y) = (240.0 - 135.0 + 2.0, LIST_TOP + 4.0); + assert_eq!( + f.title.click(icon_x + 31.0, icon_y + 16.0, (480.0, 262.0)), + ( + Action::Play { + folder: "World".into() + }, + true + ) + ); + // Right of the icon only selects the row. + f.title.enter_singleplayer(); + assert_eq!( + f.title.click(icon_x + 33.0, icon_y + 16.0, (480.0, 262.0)), + (Action::None, false) + ); +} + #[test] fn world_management_buttons_sound_when_enabled() { let mut f = Fixture::new(); From 8ca9cda5f690d195a1d1d6006ffd9dc548bc5470 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 02:26:36 +0200 Subject: [PATCH 07/97] Pause menu: icon sprites centred and untinted like SpriteIconButton CenteredIcon draws its 15x15 sprite at x + 20/2 - 15/2 = x + 3 (integer division), y + 3, with the widget's alpha only: Horizon drew it at +2 and tinted it grey. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/screens.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/crates/horizon-client/src/game/screens.rs b/crates/horizon-client/src/game/screens.rs index 91fba4365..94891cd25 100644 --- a/crates/horizon-client/src/game/screens.rs +++ b/crates/horizon-client/src/game/screens.rs @@ -112,15 +112,10 @@ impl Game<'_> { } for (x, y, sprite) in pause_icons(w, h) { ui::button(batch, atlas, x, y, 20.0, 20.0, &Text::empty(), false, false); - batch.sprite_scaled( - atlas, - sprite, - x + 2.0, - y + 2.0, - 15.0, - 15.0, - rgba(160, 160, 160, 255), - ); + // SpriteIconButton.CenteredIcon: the 15x15 sprite at + // x + width / 2 - 15 / 2 in integer pixels, untinted even + // when the button is inactive. + batch.sprite_scaled(atlas, sprite, x + 3.0, y + 3.0, 15.0, 15.0, u32::MAX); } } pub(super) fn pause_click(&mut self) -> Result<()> { From a8e0dead355c23d0f38ad0a431a5888ea9eeb7d9 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 02:42:22 +0200 Subject: [PATCH 08/97] Wayland: VSync off draws uncapped in a window pre_present_notify ties winit's RedrawRequested to the compositor's frame callbacks on Wayland, one per refresh, so a window drew at the monitor's rate whatever the present mode. Only Fifo (VSync) asks for that pacing now; Mailbox and Immediate draw as fast as the frame limit allows, like vanilla's GLFW window. Fixes #441 Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 6b0d046ee..c8b8f728b 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -2534,7 +2534,12 @@ impl Renderer { } let present = Instant::now(); if let Some(surface_frame) = surface_frame { - self.window.pre_present_notify(); + // On Wayland this ties RedrawRequested to the compositor's frame + // callbacks, one per refresh. Only VSync (Fifo) waits for the + // display: with it off, vanilla's window draws uncapped. + if self.config.present_mode == wgpu::PresentMode::Fifo { + self.window.pre_present_notify(); + } self.queue.present(surface_frame); if reconfigure && let Some(surface) = &self.surface { surface.configure(&self.device, &self.config); From 9f7c1c3ce545903a063c1b9d33e141ee464d5388 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 02:46:19 +0200 Subject: [PATCH 09/97] World Options: vanilla layout, headings, port box, lock, game rules and restrictions WorldOptionsScreen in a HeaderAndFooterLayout: the title in the 33 pixel header, the content in a ScrollableLayout over the in-world list background between the separators, the footer buttons at h - 27. Bold underlined category headings (FocusableTextWidget), the difficulty button 130 wide beside its lock, Edit Game Rules... and Restrictions..., the port EditBox always shown with its hint and error colour, and the buttons' active states from updateButton and the update*Button methods. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 3 + crates/horizon-client/src/game/lan_screen.rs | 267 +++++++++++++------ crates/horizon-client/src/game/options.rs | 2 +- 3 files changed, 191 insertions(+), 81 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index b4787eb0b..04968c1df 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -9181,6 +9181,9 @@ impl ApplicationHandler for Game<'_> { if self.screen == Screen::Stats && d != 0.0 { self.stats_scroll(d); } + if self.screen == Screen::WorldOptions && d != 0.0 { + self.world_options_scroll(d); + } if self.screen == Screen::Advancements { self.advancements_scroll(scaled(raw_x) as f32, d); } diff --git a/crates/horizon-client/src/game/lan_screen.rs b/crates/horizon-client/src/game/lan_screen.rs index f0eb57b3e..a46efacc7 100644 --- a/crates/horizon-client/src/game/lan_screen.rs +++ b/crates/horizon-client/src/game/lan_screen.rs @@ -22,7 +22,10 @@ pub(super) struct WorldOptions { hint: u16, guest_commands: bool, force: bool, + hardcore: bool, initial: Snapshot, + /// ScrollableLayout's scroll amount. + scroll: f32, } #[derive(Clone, PartialEq)] struct Snapshot { @@ -39,12 +42,28 @@ struct Snapshot { const MODES: [&str; 4] = ["survival", "creative", "adventure", "spectator"]; const DIFFICULTIES: [&str; 4] = ["peaceful", "easy", "normal", "hard"]; +/// HeaderAndFooterLayout's header and footer heights. +const HEADER: f32 = 33.0; +const FOOTER: f32 = 33.0; +/// The scrolled content: both grids, each padded by 8 and 8 apart. +const CONTENT: f32 = 218.0; +/// The category headings' tops (FocusableTextWidget, padding 4). +const GENERAL_TOP: f32 = 8.0; +const MULTIPLAYER_TOP: f32 = 145.0; +/// The port box's top beside the LAN button. +const PORT_TOP: f32 = 166.0; + #[derive(Clone, Copy, PartialEq)] enum Button { DefaultMode, PersonalMode, Cheats, Difficulty, + /// LockIconButton: Horizon can't lock a world's difficulty yet. + Lock, + /// InWorldGameRulesScreen and RestrictionsScreen are not in Horizon yet. + GameRules, + Restrictions, Lan, CommandAccess, Force, @@ -86,26 +105,56 @@ impl WorldOptions { let valid = !self.lan || self.parsed_port().is_some(); valid && self.snapshot() != self.initial } - fn buttons(w: f32) -> Vec<(Button, f32, f32, f32)> { - let x = (w / 2.0 - 154.0).floor(); + /// The scrolled content's top: under the header, less the scroll. + fn top(&self) -> f32 { + HEADER - self.scroll.trunc() + } + /// The grids' left edge: the content (308 wide, padded by 8) centred + /// in a ScrollableLayout reserving 10 pixels for the scrollbar each side. + fn left(w: f32) -> f32 { + (w / 2.0).floor() - 154.0 + } + /// The scrolled widgets: each grid's rows 4 apart, columns 8 apart. + fn buttons(w: f32, top: f32) -> Vec<(Button, f32, f32, f32)> { + let x = Self::left(w); let right = x + 158.0; vec![ - (Button::DefaultMode, x, 38.0, 308.0), - (Button::PersonalMode, x, 62.0, 308.0), - (Button::Cheats, x, 86.0, 150.0), - (Button::Difficulty, right, 86.0, 150.0), - (Button::Lan, x, 126.0, 150.0), - (Button::CommandAccess, x, 150.0, 150.0), - (Button::Force, right, 150.0, 150.0), + (Button::DefaultMode, x, top + 29.0, 308.0), + (Button::PersonalMode, x, top + 53.0, 308.0), + (Button::Cheats, x, top + 77.0, 150.0), + // DifficultyButtons: the cycle button gives the lock its 20 pixels. + (Button::Difficulty, right, top + 77.0, 130.0), + (Button::Lock, right + 130.0, top + 77.0, 20.0), + (Button::GameRules, x, top + 101.0, 150.0), + (Button::Restrictions, right, top + 101.0, 150.0), + (Button::Lan, x, top + 166.0, 150.0), + (Button::CommandAccess, x, top + 190.0, 150.0), + (Button::Force, right, top + 190.0, 150.0), ] } + /// The footer's two buttons, centred in its 33 pixels (FrameLayout + /// truncates the 6.5 pixel offset). fn footer(w: f32, h: f32) -> [(Button, f32, f32, f32); 2] { - let x = (w / 2.0 - 154.0).floor(); + let x = Self::left(w); [ - (Button::Apply, x, h - 28.0, 150.0), - (Button::Cancel, x + 158.0, h - 28.0, 150.0), + (Button::Apply, x, h - FOOTER + 6.0, 150.0), + (Button::Cancel, x + 158.0, h - FOOTER + 6.0, 150.0), ] } + /// The widgets under the pointer's row: scrolled ones only inside the + /// scroll area. + fn hit(&self, (x, y): (f32, f32), (w, h): (f32, f32)) -> Option