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.