7b35ca2799
Three things kept Sildurs Vibrant black or wrong: - Matrix and array varyings (`varying mat3 tbnMatrix;`): wgpu's stage interfaces take only scalars and vectors, so the translator now carries each column or element as its own varying and copies them out of and into a private global around the pack's `main`. - Switch options defined in an included file whose name is not a shader extension (`#include "shaders.settings"`) were not read, so every `program.<name>.enabled` that tested them was false and the composites were skipped (OQ-13). - `fogStart`/`fogEnd` mixed the start of one of vanilla's two fogs with the end of the other, so on clear days fog began at the camera and Sildurs' water turned white. They now come as the pair of the fog that is complete nearer (OQ-36). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
104 lines
4.2 KiB
Rust
104 lines
4.2 KiB
Rust
//! Switch options (spec 8.1), as far as program-enable expressions (3.3) need them: which are on
|
|
//! by default. A switch is `#define NAME` (on) or `//#define NAME` (off) with nothing after the
|
|
//! name but a comment, in any stage or included file of the pack. Packs keep their options in
|
|
//! included `.glsl` files (or files of any name a source includes) and test them elsewhere
|
|
//! (OQ-13), so the test the spec asks for (`#ifdef NAME` / `#ifndef NAME`) may be in any file
|
|
//! of the pack.
|
|
|
|
use std::collections::BTreeSet;
|
|
|
|
use crate::Pack;
|
|
|
|
const SOURCES: [&str; 8] = ["vsh", "fsh", "gsh", "csh", "tcs", "tes", "glsl", "inc"];
|
|
|
|
/// The switch options that are on by default.
|
|
pub fn switches_on(pack: &Pack) -> BTreeSet<String> {
|
|
let (mut on, mut off, mut tested) = (BTreeSet::new(), BTreeSet::new(), BTreeSet::new());
|
|
let files = pack.all_files();
|
|
let source = |path: &str| SOURCES.contains(&path.rsplit_once('.').map_or("", |(_, e)| e));
|
|
let texts: Vec<(&String, String)> = files
|
|
.iter()
|
|
.filter(|p| source(p))
|
|
.filter_map(|p| Some((p, pack.read_text(p)?)))
|
|
.collect();
|
|
// Files of other names count when a source includes them (`#include "shaders.settings"`).
|
|
let included: BTreeSet<&str> = texts
|
|
.iter()
|
|
.flat_map(|(_, t)| t.lines())
|
|
.filter_map(|l| l.trim().strip_prefix("#include"))
|
|
.map(|r| r.trim().trim_matches(|c| c == '"' || c == '<' || c == '>'))
|
|
.map(|r| r.rsplit('/').next().unwrap_or(r))
|
|
.collect();
|
|
let extra: Vec<(&String, String)> = files
|
|
.iter()
|
|
.filter(|p| !source(p) && included.contains(p.rsplit('/').next().unwrap_or(p)))
|
|
.filter_map(|p| Some((p, pack.read_text(p)?)))
|
|
.collect();
|
|
for (_, text) in texts.iter().chain(&extra) {
|
|
for line in text.lines() {
|
|
let line = line.trim();
|
|
let (commented, rest) = match line.strip_prefix("//") {
|
|
Some(rest) => (true, rest.trim_start()),
|
|
None => (false, line),
|
|
};
|
|
let Some((directive, rest)) = rest
|
|
.strip_prefix('#')
|
|
.map(str::trim_start)
|
|
.and_then(|r| r.split_once(|c: char| c.is_whitespace()))
|
|
else {
|
|
continue;
|
|
};
|
|
let mut words = rest.split_whitespace();
|
|
let Some(name) = words.next() else { continue };
|
|
match directive {
|
|
"ifdef" | "ifndef" if !commented => _ = tested.insert(name.to_owned()),
|
|
"define" if words.next().is_none_or(|w| w.starts_with("//")) => {
|
|
let set = if commented { &mut off } else { &mut on };
|
|
set.insert(name.to_owned());
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
// Defined both ways is ambiguous (8.1): it keeps its default only where it is on everywhere.
|
|
on.retain(|name| tested.contains(name) && !off.contains(name));
|
|
on
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::pack::tests::zip;
|
|
|
|
#[test]
|
|
fn switches_need_a_test_and_no_value() {
|
|
let pack = Pack::from_zip_bytes(
|
|
"P.zip",
|
|
zip(&[
|
|
(
|
|
"shaders/lib/settings.glsl",
|
|
" #define AO // ambient occlusion\n//#define BLOOM\n#define LEVEL 2 //[1 2]\n\
|
|
#define SPLIT\n#define UNTESTED\n",
|
|
),
|
|
(
|
|
"shaders/world0/deferred.fsh",
|
|
"#ifdef AO\n#endif\n#ifndef BLOOM\n#endif\n#ifdef SPLIT\n#endif\n#ifdef LEVEL\n#endif\n",
|
|
),
|
|
("shaders/world-1/deferred.fsh", "//#define SPLIT\n"),
|
|
(
|
|
"shaders/final.fsh",
|
|
"#include \"shaders.settings\"\n#ifdef GODRAYS\n#endif\n",
|
|
),
|
|
("shaders/shaders.settings", " #define GODRAYS //Toggle\n"),
|
|
("shaders/notes.settings", "#define NOT_INCLUDED\n#ifdef NOT_INCLUDED\n"),
|
|
("shaders/shaders.properties", "#define NOT_SOURCE\n#ifdef NOT_SOURCE\n"),
|
|
]),
|
|
)
|
|
.unwrap();
|
|
assert_eq!(
|
|
switches_on(&pack),
|
|
BTreeSet::from(["AO".to_owned(), "GODRAYS".to_owned()])
|
|
);
|
|
}
|
|
}
|