[PR #413] [MERGED] Java shader packs (2): translate programs to WGSL (syntax-tree legaliser + naga) #2545

Closed
opened 2026-11-02 15:57:04 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/413
Author: @ludovic111
Created: 10/15/2026
Status: ✅ Merged
Merged: 10/16/2026
Merged by: @ludovic111

Base: main ← Head: feat/shaderpack-translate


📝 Commits (2)

  • b22583d horizon-shaderpack: open Java shader packs and preprocess them like Iris
  • 98ebdca horizon-shaderpack: translate Java shader pack programs to WGSL

📊 Changes

18 files changed (+3798 additions, -7 deletions)

View changed files

📝 Cargo.lock (+395 -6)
📝 Cargo.toml (+1 -1)
➕ crates/horizon-shaderpack/Cargo.toml (+20 -0)
➕ crates/horizon-shaderpack/src/include.rs (+198 -0)
➕ crates/horizon-shaderpack/src/lib.rs (+140 -0)
➕ crates/horizon-shaderpack/src/options.rs (+659 -0)
➕ crates/horizon-shaderpack/src/preprocess.rs (+332 -0)
➕ crates/horizon-shaderpack/src/source.rs (+141 -0)
➕ crates/horizon-shaderpack/src/translate.rs (+1297 -0)
➕ crates/horizon-shaderpack/tests/fixtures/mini/shaders/composite.fsh (+10 -0)
➕ crates/horizon-shaderpack/tests/fixtures/mini/shaders/composite.vsh (+3 -0)
➕ crates/horizon-shaderpack/tests/fixtures/mini/shaders/lib/common.glsl (+7 -0)
➕ crates/horizon-shaderpack/tests/fixtures/mini/shaders/lib/settings.glsl (+7 -0)
➕ crates/horizon-shaderpack/tests/fixtures/mini/shaders/notes.txt (+1 -0)
➕ crates/horizon-shaderpack/tests/fixtures/mini/shaders/world-1/composite.fsh (+4 -0)
➕ crates/horizon-shaderpack/tests/pack.rs (+223 -0)
➕ crates/horizon-shaderpack/tests/translate.rs (+352 -0)
📝 docs/shader-packs.md (+8 -0)

📄 Description

Stacked on #405 (base feat/shaderpack-preprocess); merge #405 first.

What

The second half of docs/shader-packs.md "Order to implement", step 1: horizon_shaderpack::translate turns a preprocessed Java pack program into a validated naga module and WGSL. Nothing in the game uses it yet.

The spike rewrote tokens; this rewrites a syntax tree (new dependency: glsl-lang 0.8.1, BSD-3-Clause, a GLSL 4.6 parser and printer; it parses all 470 programs of the test packs). Rewrites, as Iris's glsl-transformer does for the core profile:

  • attribute/varying → in/out with locations; matrix and array varyings split into vectors (WGSL can pass neither between stages) and packed back into a private global;
  • loose uniforms → one std140 block at binding 0 (bool stored as int);
  • sampler2D & co. → a texture and a sampler binding, rebuilt as sampler2D(t, s) where used, also through function parameters, and not where a local hides the name (a local float texture in a pack with uniform sampler2D texture stays a float);
  • compatibility built-ins (gl_Vertex, gl_Color, gl_MultiTexCoordN, gl_FrontColor, gl_TexCoord[n], gl_FragData[n]/gl_FragColor, gl_ModelViewMatrix, gl_TextureMatrix[n], gl_Fog, ftransform(), ...) and Iris's core-profile names (vaPosition, modelViewMatrix, ...) declared;
  • legacy texture calls renamed; shadow2D returns a vec4 again and texture(shadowSampler, ..).x is widened first (naga has no scalar swizzle);
  • global initialisers run at the start of main (drivers accept non-constant ones), unsized arrays sized from their initialiser;
  • isnan/isinf/modf as helper functions (WGSL has none);
  • dead code after an unconditional discard/return/break/continue dropped. The spike blamed 5 BSL failures on a naga constructor bug; it was code after the discard; BSL uses to switch clouds and weather off.

The programs of one pass share a Link (vertex program first): varying locations, attribute locations, bindings and the uniform block agree between stages, and a later program's block extends the earlier one's so member offsets are the same. Each program reports an Interface (samplers, images, uniforms, attributes, outputs by location) for the pipeline that will run it.

How it was verified

  • Fixture tests (our own GLSL, in tests/translate.rs): a compatibility-profile vertex + fragment pass checked through naga's IR: varyings at the same locations in both stages (the mat3 as three columns), attributes, gl_FragData outputs, the uniform block's members and prefix property, bindings, the hidden-sampler local, sampler parameters, hoisted initialisers, sized arrays, dead code; errors name their line; a uniform declared with two types in one pass is an error.
  • Real packs, dev only (outside the repo, never committed), HORIZON_TEST_SHADERPACKS=<folder with the three zips>: every program the packs enable by default translates, and its WGSL is read back and validated:
    ComplementaryReimagined_r5.9.3.zip: 180/180 programs translated
    Sildurs-Vibrant-2.02-Extreme.zip: 114/114 programs translated
    BSL_v10.1.8.zip: 170/170 programs translated
    
    (Spike: 450/470.) Left out, as in the spike: the dh_*/clrwl_*/voxy* programs (Iris loads them only with those mods) and the 6 shadowcomp.csh, which the packs turn off by default (program.*/shadowcomp.enabled in shaders.properties) and which need the images and options declared there: next PR.

Self-check (Mac mini):

cargo fmt -p horizon-shaderpack                                   clean
cargo clippy -p horizon-shaderpack --all-targets -- -D warnings   clean
cargo test -p horizon-shaderpack --locked                         12 passed

No other crate depends on this one yet; no parity harness covers it.

What is left

shaders.properties (option screens, profiles, program.*.enabled, images, blend modes) next, then the full-screen programs in the renderer. Images without a format get rgba8 unless the caller gives one (Link::image_formats, filled from shaders.properties later). Geometry and tessellation programs are refused (naga has neither stage).

Needs Ludovic

Nothing; the new dependency (glsl-lang, BSD-3-Clause, plus lalrpop at build time) is the one design choice to know about.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/413 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/15/2026 **Status:** ✅ Merged **Merged:** 10/16/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `feat/shaderpack-translate` --- ### 📝 Commits (2) - [`b22583d`](https://github.com/horizon-rust/horizon/commit/b22583d0577f83652e275b4560de483ab42594d8) horizon-shaderpack: open Java shader packs and preprocess them like Iris - [`98ebdca`](https://github.com/horizon-rust/horizon/commit/98ebdcad7b27100b569ca55b717507e2dd896bbc) horizon-shaderpack: translate Java shader pack programs to WGSL ### 📊 Changes **18 files changed** (+3798 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `Cargo.lock` (+395 -6) 📝 `Cargo.toml` (+1 -1) ➕ `crates/horizon-shaderpack/Cargo.toml` (+20 -0) ➕ `crates/horizon-shaderpack/src/include.rs` (+198 -0) ➕ `crates/horizon-shaderpack/src/lib.rs` (+140 -0) ➕ `crates/horizon-shaderpack/src/options.rs` (+659 -0) ➕ `crates/horizon-shaderpack/src/preprocess.rs` (+332 -0) ➕ `crates/horizon-shaderpack/src/source.rs` (+141 -0) ➕ `crates/horizon-shaderpack/src/translate.rs` (+1297 -0) ➕ `crates/horizon-shaderpack/tests/fixtures/mini/shaders/composite.fsh` (+10 -0) ➕ `crates/horizon-shaderpack/tests/fixtures/mini/shaders/composite.vsh` (+3 -0) ➕ `crates/horizon-shaderpack/tests/fixtures/mini/shaders/lib/common.glsl` (+7 -0) ➕ `crates/horizon-shaderpack/tests/fixtures/mini/shaders/lib/settings.glsl` (+7 -0) ➕ `crates/horizon-shaderpack/tests/fixtures/mini/shaders/notes.txt` (+1 -0) ➕ `crates/horizon-shaderpack/tests/fixtures/mini/shaders/world-1/composite.fsh` (+4 -0) ➕ `crates/horizon-shaderpack/tests/pack.rs` (+223 -0) ➕ `crates/horizon-shaderpack/tests/translate.rs` (+352 -0) 📝 `docs/shader-packs.md` (+8 -0) </details> ### 📄 Description Stacked on #405 (base `feat/shaderpack-preprocess`); merge #405 first. ## What The second half of docs/shader-packs.md "Order to implement", step 1: `horizon_shaderpack::translate` turns a preprocessed Java pack program into a validated naga module and WGSL. Nothing in the game uses it yet. The spike rewrote tokens; this rewrites a syntax tree (new dependency: `glsl-lang` 0.8.1, BSD-3-Clause, a GLSL 4.6 parser and printer; it parses all 470 programs of the test packs). Rewrites, as Iris's glsl-transformer does for the core profile: - `attribute`/`varying` → `in`/`out` with locations; matrix and array varyings split into vectors (WGSL can pass neither between stages) and packed back into a private global; - loose uniforms → one std140 block at binding 0 (`bool` stored as `int`); - `sampler2D` & co. → a texture and a sampler binding, rebuilt as `sampler2D(t, s)` where used, also through function parameters, and not where a local hides the name (a local `float texture` in a pack with `uniform sampler2D texture` stays a float); - compatibility built-ins (`gl_Vertex`, `gl_Color`, `gl_MultiTexCoordN`, `gl_FrontColor`, `gl_TexCoord[n]`, `gl_FragData[n]`/`gl_FragColor`, `gl_ModelViewMatrix`, `gl_TextureMatrix[n]`, `gl_Fog`, `ftransform()`, ...) and Iris's core-profile names (`vaPosition`, `modelViewMatrix`, ...) declared; - legacy texture calls renamed; `shadow2D` returns a `vec4` again and `texture(shadowSampler, ..).x` is widened first (naga has no scalar swizzle); - global initialisers run at the start of `main` (drivers accept non-constant ones), unsized arrays sized from their initialiser; - `isnan`/`isinf`/`modf` as helper functions (WGSL has none); - dead code after an unconditional `discard`/`return`/`break`/`continue` dropped. The spike blamed 5 BSL failures on a naga constructor bug; it was code after the `discard;` BSL uses to switch clouds and weather off. The programs of one pass share a `Link` (vertex program first): varying locations, attribute locations, bindings and the uniform block agree between stages, and a later program's block extends the earlier one's so member offsets are the same. Each program reports an `Interface` (samplers, images, uniforms, attributes, outputs by location) for the pipeline that will run it. ## How it was verified - Fixture tests (our own GLSL, in `tests/translate.rs`): a compatibility-profile vertex + fragment pass checked through naga's IR: varyings at the same locations in both stages (the `mat3` as three columns), attributes, `gl_FragData` outputs, the uniform block's members and prefix property, bindings, the hidden-sampler local, sampler parameters, hoisted initialisers, sized arrays, dead code; errors name their line; a uniform declared with two types in one pass is an error. - Real packs, dev only (outside the repo, never committed), `HORIZON_TEST_SHADERPACKS=<folder with the three zips>`: every program the packs enable by default translates, and its WGSL is read back and validated: ``` ComplementaryReimagined_r5.9.3.zip: 180/180 programs translated Sildurs-Vibrant-2.02-Extreme.zip: 114/114 programs translated BSL_v10.1.8.zip: 170/170 programs translated ``` (Spike: 450/470.) Left out, as in the spike: the `dh_*`/`clrwl_*`/`voxy*` programs (Iris loads them only with those mods) and the 6 `shadowcomp.csh`, which the packs turn off by default (`program.*/shadowcomp.enabled` in `shaders.properties`) and which need the images and options declared there: next PR. Self-check (Mac mini): ``` cargo fmt -p horizon-shaderpack clean cargo clippy -p horizon-shaderpack --all-targets -- -D warnings clean cargo test -p horizon-shaderpack --locked 12 passed ``` No other crate depends on this one yet; no parity harness covers it. ## What is left `shaders.properties` (option screens, profiles, `program.*.enabled`, images, blend modes) next, then the full-screen programs in the renderer. Images without a format get `rgba8` unless the caller gives one (`Link::image_formats`, filled from `shaders.properties` later). Geometry and tessellation programs are refused (naga has neither stage). ## Needs Ludovic Nothing; the new dependency (`glsl-lang`, BSD-3-Clause, plus lalrpop at build time) is the one design choice to know about. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 15:57:04 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#2545
No description provided.