Files
horizon-airdef/crates/horizon-shaderpack/src/lib.rs
T
Ludovic Marie 98ebdcad7b 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 <noreply@anthropic.com>
2026-10-15 22:36:18 +02:00

141 lines
4.6 KiB
Rust

//! 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;
pub mod translate;
use include::{IncludeGraph, Inlined};
use options::{Annotated, OptionSet, OptionValues};
use preprocess::{Environment, Preprocessed};
use source::PackFiles;
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)]
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<Self> {
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<String>,
pub graph: IncludeGraph,
annotations: BTreeMap<String, Annotated>,
/// 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<Self, String> {
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<String, Annotated> = 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/<pack>.txt`).
pub fn values(&self, saved: &BTreeMap<String, String>) -> OptionValues {
OptionValues::new(&self.options, saved)
}
/// `program` with its includes inlined and `values` applied.
pub fn source(&self, program: &str, values: &OptionValues) -> Result<Inlined, String> {
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<Preprocessed, String> {
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<Translated, String> {
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}"))
}
}