Merge remote-tracking branch 'origin/main' into mm1-1447

This commit is contained in:
Ludovic Marie
2026-10-23 20:38:10 +02:00
5 changed files with 365 additions and 27 deletions
+22
View File
@@ -236,6 +236,24 @@ mod tests {
assert_eq!(vendor_name(0, "Apple M4 Pro"), "Apple");
}
/// wgpu is given the WGSL text: it must parse again.
fn reparse_error(pack: &JavaPasses) -> Option<String> {
let passes = pack
.folders
.iter()
.flat_map(|f| f.passes.iter().chain(&f.final_pass));
passes
.flat_map(|p| [(p, &p.vertex, "vsh"), (p, &p.fragment, "fsh")])
.find_map(|(p, t, ext)| {
let e = naga::front::wgsl::parse_str(&t.wgsl).err()?;
Some(format!(
"{}.{ext}: {}",
p.pass.program,
e.emit_to_string(&t.wgsl)
))
})
}
/// The packs in `HORIZON_TEST_SHADERPACKS` (folders or zips) load as the
/// game loads them, with their default options: the compatibility table
/// of docs/shader-packs.md. Skipped when the variable isn't set.
@@ -255,6 +273,10 @@ mod tests {
for path in entries {
let name = path.file_name().unwrap().to_string_lossy().into_owned();
match load(&path, &BTreeMap::new(), "Apple", "Apple M4 Pro") {
Ok(pack) if let Some(e) = reparse_error(&pack) => {
println!("{name}: {e}");
failures.push(name);
}
Ok(pack) => {
let folders: Vec<String> = pack
.folders
+24 -2
View File
@@ -199,8 +199,11 @@ pub fn preprocess(
let (start, end) = (token.location.start, token.location.end);
let at = line_of(start);
if at > line {
// Lines the preprocessor left empty stay empty lines, so a value
// continued with `\` before an `#else` (Photon's
// `moon_phase_brightness`) ends there, as in Iris.
if line > 0 {
out.push('\n');
out.extend(std::iter::repeat_n('\n', at - line));
}
line = at;
} else if last_end != Some(start) {
@@ -799,7 +802,26 @@ b = 1
};
let values = OptionValues::default();
let out = preprocess(text, &env, &set, &values).unwrap();
assert_eq!(out, "a = 0.75\nb = 1\n");
let lines: Vec<&str> = out.lines().filter(|l| !l.is_empty()).collect();
assert_eq!(lines, ["a = 0.75", "b = 1"]);
// Photon ends an `#if`'d value with `) \` before `#else`: the value
// stops at the empty line the directive leaves.
let text = "\
#if FOG == 0
a = if( \\
moonPhase == 0, 1.0, \\
2.0 \\
) \\
#else
a = 1.0
#endif
b = 2
";
let out = preprocess(text, &env, &set, &values).unwrap();
let parsed = Properties::parse(&out);
assert_eq!(parsed.get("a"), Some("if( moonPhase == 0, 1.0, 2.0 ) "));
assert_eq!(parsed.get("b"), Some("2"));
assert_eq!(
jcpp_condition("#elif FOG == 0.75 && X > 2.5", &set, &values),
"#elif 0 == 0 && X > 2"
+236 -23
View File
@@ -17,8 +17,9 @@
//! (`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);
//! - vertex and compute `texture(s, uv)` calls without an explicit LOD become
//! `textureLod(s, uv, 0.0)` so naga doesn't reject implicit derivatives;
//! - outside fragment programs, `texture`, `textureProj` and their `Offset`
//! forms become their `Lod` forms at level 0 (no derivatives there; naga
//! refuses the implicit ones), a bias dropped;
//! - 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);
@@ -380,9 +381,10 @@ pub fn translate(
stage: naga_stage,
defines: Default::default(),
};
let module = naga::front::glsl::Frontend::default()
let mut module = naga::front::glsl::Frontend::default()
.parse(&options, &glsl)
.map_err(|e| first_lines(&e.emit_to_string(&glsl)))?;
unpad_private_arrays(&mut module);
let info = naga::valid::Validator::new(
naga::valid::ValidationFlags::all(),
naga::valid::Capabilities::all(),
@@ -508,6 +510,79 @@ fn negative_constant(e: &ast::Expr) -> bool {
}
}
/// naga's GLSL front end lays every struct out as std140, so an array field
/// of a struct that never reaches a buffer (Photon's ACES spline parameters,
/// `float[6]`) gets a stride of 16, and constructing it from a `float[6](..)`
/// (stride 4) fails validation. Give such fields their natural stride.
fn unpad_private_arrays(module: &mut naga::Module) {
use naga::TypeInner as T;
let mut shared = HashSet::new();
let mut stack: Vec<_> = module
.global_variables
.iter()
.filter(|(_, v)| {
matches!(
v.space,
naga::AddressSpace::Uniform
| naga::AddressSpace::Storage { .. }
| naga::AddressSpace::Immediate
)
})
.map(|(_, v)| v.ty)
.collect();
while let Some(ty) = stack.pop() {
if !shared.insert(ty) {
continue;
}
match &module.types[ty].inner {
T::Struct { members, .. } => stack.extend(members.iter().map(|m| m.ty)),
T::Array { base, .. } | T::BindingArray { base, .. } => stack.push(*base),
_ => {}
}
}
let mut layouter = naga::proc::Layouter::default();
if layouter.update(module.to_ctx()).is_err() {
return;
}
let structs: Vec<_> = module
.types
.iter()
.filter(|(h, t)| !shared.contains(h) && matches!(t.inner, T::Struct { .. }))
.map(|(h, _)| h)
.collect();
for ty in structs {
let mut new = module.types[ty].clone();
let T::Struct { members, .. } = &mut new.inner else {
continue;
};
let mut changed = false;
for m in members.iter_mut() {
let T::Array { base, size, stride } = module.types[m.ty].inner else {
continue;
};
let natural = layouter[base].to_stride();
if stride != natural {
let span = module.types.get_span(m.ty);
m.ty = module.types.insert(
naga::Type {
name: None,
inner: T::Array {
base,
size,
stride: natural,
},
},
span,
);
changed = true;
}
}
if changed {
module.types.replace(ty, new);
}
}
}
fn first_lines(s: &str) -> String {
s.lines().take(8).collect::<Vec<_>>().join("\n")
}
@@ -554,6 +629,8 @@ struct Legaliser<'a> {
/// their helpers go in.
modulo: bool,
nan: bool,
/// `textureGather` was used: [`GATHER`] is added.
gather: bool,
mat2_ctor: bool,
/// Constants turned into globals ([`needs_runtime`]).
runtime_constants: HashSet<String>,
@@ -571,6 +648,94 @@ fn call(name: &str, args: Vec<ast::Expr>) -> ast::Expr {
.into_node()
}
/// `textureGather(sampler2D, uv, comp)`: component `comp` of the four texels
/// bilinear filtering at `uv` would blend, in GL's order, read with
/// `texelFetch` (edges clamped).
const GATHER: &str = "\
vec4 iris_gather(texture2D t, sampler s, vec2 uv, int c) {
ivec2 hi = textureSize(sampler2D(t, s), 0) - 1;
ivec2 i = ivec2(floor(uv * vec2(hi + 1) - 0.5));
vec4 a = texelFetch(sampler2D(t, s), clamp(i + ivec2(0, 1), ivec2(0), hi), 0);
vec4 b = texelFetch(sampler2D(t, s), clamp(i + ivec2(1, 1), ivec2(0), hi), 0);
vec4 d = texelFetch(sampler2D(t, s), clamp(i + ivec2(1, 0), ivec2(0), hi), 0);
vec4 e = texelFetch(sampler2D(t, s), clamp(i, ivec2(0), hi), 0);
return vec4(a[c], b[c], d[c], e[c]);
}
";
/// GL converts a `bool` given to a numeric constructor (`vec3(r < radius)`,
/// Photon's sun disc); naga refuses. A comparison or logical expression
/// passed to one becomes `(cond ? 1.0 : 0.0)` (or `1`/`1u`).
fn bool_to_number(e: &mut ast::Expr) {
use ast::BinaryOpData as B;
let ast::ExprData::FunCall(f, args) = &mut e.content else {
return;
};
let Some(name) = callee(f) else { return };
let base = name.trim_start_matches('d');
let (one, zero) = if base.starts_with("ivec") || name == "int" {
(ast::ExprData::IntConst(1), ast::ExprData::IntConst(0))
} else if base.starts_with("uvec") || name == "uint" {
(ast::ExprData::UIntConst(1), ast::ExprData::UIntConst(0))
} else if base.starts_with("vec") || base.starts_with("mat") || name == "float" {
(
ast::ExprData::FloatConst(1.0),
ast::ExprData::FloatConst(0.0),
)
} else {
return;
};
for a in args.iter_mut() {
let boolean = match &a.content {
ast::ExprData::Binary(op, ..) => matches!(
op.content,
B::Or | B::Xor | B::And | B::Equal | B::NonEqual | B::Lt | B::Gt | B::Lte | B::Gte
),
ast::ExprData::Unary(op, _) => op.content == ast::UnaryOpData::Not,
_ => false,
};
if boolean {
let cond = std::mem::replace(a, var("_"));
*a = ast::ExprData::Ternary(
Box::new(cond),
Box::new(one.clone().into_node()),
Box::new(zero.clone().into_node()),
)
.into_node();
}
}
}
/// Outside fragment shaders there are no derivatives, so GL samples level 0
/// where `texture` and its kin would pick one; naga refuses them there (the
/// vertex programs of Chocapic13', MakeUp and Bliss). Turn them into their
/// `Lod` forms at level 0 (a bias is dropped), also inside `vec4(..)` as
/// legacy shadow lookups are wrapped.
fn explicit_lod(e: &mut ast::Expr) {
let ast::ExprData::FunCall(f, args) = &mut e.content else {
return;
};
let Some(name) = callee(f) else { return };
if name == "vec4" && args.len() == 1 {
return explicit_lod(&mut args[0]);
}
// (implicit, explicit, where the level goes, how many arguments without a bias)
let (lod, at, plain) = match name.as_str() {
"texture" => ("textureLod", 2, 2),
"textureProj" => ("textureProjLod", 2, 2),
"textureOffset" => ("textureLodOffset", 2, 3),
"textureProjOffset" => ("textureProjLodOffset", 2, 3),
_ => return,
};
if args.len() < plain {
return;
}
args.truncate(plain);
let zero = ast::ExprData::FloatConst(0.0).into_node();
args.insert(at, zero);
*f = ast::FunIdentifierData::Expr(Box::new(var(lod))).into_node();
}
/// The name a call is made by: a function, or a constructor's type.
fn callee(f: &ast::FunIdentifier) -> Option<String> {
match &f.content {
@@ -784,6 +949,7 @@ impl<'a> Legaliser<'a> {
constant: false,
modulo: false,
nan: false,
gather: false,
mat2_ctor: false,
runtime_constants: HashSet::new(),
}
@@ -1253,7 +1419,23 @@ impl<'a> Legaliser<'a> {
}
let mut locals = Vec::new();
let mut params = Vec::with_capacity(p.parameters.len());
for param in std::mem::take(&mut p.parameters) {
for mut param in std::mem::take(&mut p.parameters) {
// `const in float x` (Bliss): naga's front end takes no `const`
// on parameters, and it changes nothing for the caller.
match &mut param.content {
ast::FunctionParameterDeclarationData::Named(q, _)
| ast::FunctionParameterDeclarationData::Unnamed(q, _) => {
if let Some(list) = q {
list.qualifiers.retain(|q| {
!matches!(&q.content, ast::TypeQualifierSpecData::Storage(s)
if s.content == ast::StorageQualifierData::Const)
});
if list.qualifiers.is_empty() {
*q = None;
}
}
}
}
let (ty, name) = match &param.content {
ast::FunctionParameterDeclarationData::Named(_, d) => {
(type_text(&d.ty), Some(d.ident.ident.as_str().to_owned()))
@@ -1713,32 +1895,60 @@ impl<'a> Legaliser<'a> {
self.nan = true;
return;
}
let mut target_name = name.as_str();
if name == "textureGather"
&& (2..=3).contains(&args.len())
&& let ast::ExprData::FunCall(g, inner) = &args[0].content
&& inner.len() == 2
&& callee(g).as_deref() == Some("sampler2D")
{
// naga's GLSL front end has no `textureGather`.
let mut call_args = inner.clone();
call_args.push(args[1].clone());
call_args.push(
args.get(2)
.cloned()
.unwrap_or_else(|| ast::ExprData::IntConst(0).into_node()),
);
*e = call("iris_gather", call_args);
self.gather = true;
return;
}
if name == "modf"
&& args.len() == 2
&& !matches!(args[1].content, ast::ExprData::Variable(_))
{
// `modf(x, v.y)` (Photon): WGSL can't point at a vector
// component, so no `out` parameter: `+(v.y = trunc(x), x - trunc(x))`
// (the unary `+` has the comma printed in brackets, also in an
// initialiser).
let (x, whole) = (args[0].clone(), args[1].clone());
let assign = ast::ExprData::Assignment(
Box::new(whole),
ast::AssignmentOpData::Equal.into_node(),
Box::new(call("trunc", vec![x.clone()])),
)
.into_node();
let fraction = ast::ExprData::Binary(
ast::BinaryOpData::Sub.into_node(),
Box::new(x.clone()),
Box::new(call("trunc", vec![x])),
)
.into_node();
let comma = ast::ExprData::Comma(Box::new(assign), Box::new(fraction)).into_node();
*e = ast::ExprData::Unary(ast::UnaryOpData::Add.into_node(), Box::new(comma))
.into_node();
return;
}
if let Some((_, new)) = LEGACY_CALLS.iter().find(|(old, _)| *old == name) {
*f = ast::FunIdentifierData::Expr(Box::new(var(new))).into_node();
target_name = new;
} 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)]);
return;
}
// In vertex and compute shaders, `texture(sampler, coord)` uses implicit
// derivatives that WGSL/naga forbid outside fragment shaders (Bliss):
// rewrite 2-argument non-shadow `texture(s, uv)` to `textureLod(s, uv, 0.0)`.
if self.stage != Stage::Fragment && target_name == "texture" && args.len() == 2 {
let is_shadow = match &args[0].content {
ast::ExprData::FunCall(g, _) => {
callee(g).and_then(|c| opaque(&c)).is_some_and(|o| o.3)
}
_ => false,
};
if !is_shadow {
if let ast::ExprData::FunCall(f, args) = &mut e.content {
*f = ast::FunIdentifierData::Expr(Box::new(var("textureLod"))).into_node();
args.push(ast::ExprData::FloatConst(0.0).into_node());
}
}
if self.stage != Stage::Fragment {
explicit_lod(e);
}
bool_to_number(e);
}
/// The legalised source: header, the rewritten program, then a `main`
@@ -1775,6 +1985,9 @@ impl<'a> Legaliser<'a> {
if self.modulo {
out.push_str(&modulo_helpers());
}
if self.gather {
out.push_str(GATHER);
}
if self.nan {
out.push_str("float iris_sqrt(float x) { return sqrt(x); }\n");
}
@@ -368,6 +368,78 @@ void main() {
reparse(&fs.wgsl).unwrap();
}
/// What Photon, Chocapic13', MakeUp Ultra Fast and Bliss write and GL takes:
/// lookups without a level in a vertex shader, a struct passed between
/// stages, `textureGather`, a `bool` given to `vec3(..)`, `const in`
/// parameters and an array field in a struct built by its constructor.
#[test]
fn legalises_what_photon_and_chocapic_write() {
let mut link = Link::default();
let header = "uniform sampler2D colortex0;
uniform sampler2DShadow shadowtex0;
struct Clouds { float coverage; vec2 wind; mat3 turn; int layers; };
";
let vs = run(
Stage::Vertex,
&format!(
"{header}flat varying Clouds clouds;
void main() {{
clouds.coverage = texture2D(colortex0, vec2(0.5)).r + shadow2D(shadowtex0, vec3(0.5)).r;
clouds.wind = textureOffset(colortex0, vec2(0.5), ivec2(1, 0), 2.0).rg;
clouds.turn = mat3(1.0);
clouds.layers = 2;
gl_Position = ftransform();
}}
"
),
&mut link,
)
.unwrap();
for wanted in [
"textureLod(sampler2D(colortex0_t",
"vec2(0.5), 0.)",
"textureLodOffset(sampler2D(colortex0_t",
"clouds_iriscoverage = clouds.coverage;",
"clouds_iristurn_iris1 = clouds.turn[1];",
] {
assert!(vs.glsl.contains(wanted), "{wanted}\n{}", vs.glsl);
}
reparse(&vs.wgsl).unwrap();
let fs = run(
Stage::Fragment,
&format!(
"{header}flat varying Clouds clouds;
struct Spline {{ float slope; float[3] coeffs; }};
float edge(const in float x, const float r) {{ return float(x < r); }}
void main() {{
const Spline spline = Spline(0.0, float[3](1.0, 2.0, 3.0));
vec4 four = textureGather(colortex0, vec2(0.5), 1);
vec3 disc = vec3(clouds.coverage < 0.5);
vec2 cell = vec2(0.0);
float part = modf(clouds.coverage * 4.0, cell.y);
gl_FragColor = vec4(disc * edge(four.x, spline.coeffs[1]), clouds.wind.x + float(clouds.layers) + part + cell.y);
}}
"
),
&mut link,
)
.unwrap();
for wanted in [
"iris_gather(colortex0_t",
"clouds.coverage = clouds_iriscoverage;",
"float edge(in float x, float r)",
"cell.y = trunc(",
] {
assert!(fs.glsl.contains(wanted), "{wanted}\n{}", fs.glsl);
}
reparse(&fs.wgsl).unwrap();
// The vertex program's outputs are the fragment program's inputs.
assert_eq!(
io(&vs, true).get("clouds_iriswind"),
io(&fs, false).get("clouds_iriswind")
);
}
/// A fragment output narrower than its four-channel target (Steadfast's
/// `final` writes `out vec3 color`) is written through a `vec4` with alpha 1:
/// GL fills the missing channels, wgpu refuses the pipeline.
+11 -2
View File
@@ -451,10 +451,10 @@ The MakeUp, Chocapic13' and Photon rows are from 2026-10-23, on Metal and on Vul
| Steadfast | 0.8.0 | drawn: 3 passes | |
| MakeUp Ultra Fast | 9.6 | drawn: 6 passes, its fog, light shafts and grade over Horizon's world | |
| Chocapic13' | V9 High | drawn: 16 passes; its default TAA upscaling shrinks the world in its `gbuffers_*` vertex programs, which don't run, so the picture is magnified 1/0.7: turn TAA Upscaling off in its settings | |
| Bliss | 2.1.2 | fails | `world0/deferred.vsh`: an operation naga forbids in a vertex shader |
| Photon | v1.4 | loads: 27 passes; draws black: it lights the scene from the G-buffer its `gbuffers_*` write | |
| Bliss | 2.1.2 | fails | `world0/composite1.fsh`: a vector component passed to an `inout` parameter (WGSL can't point at one) |
| Bloop | 1.8.0-Alpha-3 | fails | `world0/prepare1.fsh`: `mat2(vec4)` and other constructors naga's GLSL front end refuses |
| Noble | v1.9.9 | fails (Apple and NVIDIA) | `world0/deferred4.vsh`: a `vec3[9][9]` varying (nested arrays between stages) |
| Photon | v1.4 | fails | `world0/deferred1.fsh`: `textureGather` |
| Rethinking Voxels | r0.1-beta9 | fails | `world0/prepare.fsh`: `1 << 31` overflows naga's constant folding; it also needs images |
What the translator does for these packs, as Iris (JCPP and its GLSL transformer) does:
@@ -472,6 +472,15 @@ What the translator does for these packs, as Iris (JCPP and its GLSL transformer
(`mix`, `?:`, `smoothstep`) becomes a global set at the start of `main`, `sqrt` of a negative constant is
evaluated at run time, and a fragment output narrower than four channels (`out vec3`, Steadfast's `final`) is
written through a `vec4` with alpha 1 as OpenGL fills it.
- **What GL takes and naga doesn't** (Photon, Chocapic13', MakeUp Ultra Fast, Bliss): `texture2D` and its kin without a
level outside fragment shaders sample level 0 (`textureLod`); a struct passed between stages travels as one varying
per field (matrices by column, arrays by element); `textureGather` reads the four texels with `texelFetch`; a
`bool` given to `vec3(..)` and other numeric constructors becomes `cond ? 1.0 : 0.0`; `const in` parameters lose
the `const`; an array field of a struct that never reaches a buffer gets its natural stride (naga lays every struct
out as std140, so `Spline(.., float[6](..))` didn't match); `modf` into a vector component assigns it instead of
passing a pointer.
- **Every translated program parses again as WGSL** in `real_packs_load`: wgpu is given the text, and naga's WGSL
writer can emit what its WGSL parser refuses (`&v.y`).
### Order to implement