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>
This commit is contained in:
@@ -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"] }
|
||||
|
||||
@@ -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<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}"))
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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<Translated, String> {
|
||||
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<String, u32> {
|
||||
let f = &t.module.entry_points[0].function;
|
||||
let mut out = BTreeMap::new();
|
||||
let mut add =
|
||||
|name: Option<&String>, binding: &Option<naga::Binding>, ty: naga::Handle<naga::Type>| {
|
||||
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<String, u32> {
|
||||
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<String> {
|
||||
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<String> = 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::<Vec<_>>(),
|
||||
[0, 1]
|
||||
);
|
||||
assert_eq!(
|
||||
io(&fs, true).values().copied().collect::<BTreeSet<_>>(),
|
||||
[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"));
|
||||
}
|
||||
Reference in New Issue
Block a user