Merge pull request #2112 from ludovic111/feat/native-material-plumbing

🤖 Agent: verified: pr_ready --quick green, 30/30 affected harnesses pass locally, CI verify green. Internal: one WGSL prefixer for the shader-pack stages (no visible change).
This commit is contained in:
Ludovic Marie
2026-10-26 12:32:07 +01:00
committed by GitHub
3 changed files with 128 additions and 50 deletions
+1
View File
@@ -88,6 +88,7 @@ mod stars;
mod timing;
pub mod translucency;
pub(crate) mod upload;
mod wgsl_prefix;
mod world_post;
/// A column whose every section has its mesh (`Renderer::lod_detail_columns`).
+3 -50
View File
@@ -9,9 +9,9 @@
use horizon_shader_api::{check::CheckedStage, stages::GROUP};
use super::native_post::{NativeFrame, present_constants};
use super::wgsl_prefix::{TERRAIN, prefixed, section};
const PREFIX: &str = "horizon_sky_";
const TERRAIN: &str = include_str!("../terrain.wgsl");
/// The fragment entry that calls the pack: vanilla's colour in, an opaque colour out.
const PACK_ENTRY: &str = "
@@ -26,62 +26,15 @@ pub struct NativeSky {
pub label: String,
}
/// The text of `source` from the line starting with `start` up to (not including) the one
/// starting with `end`.
fn section<'a>(source: &'a str, start: &str, end: &str) -> &'a str {
let from = source
.find(&format!("\n{start}"))
.expect("terrain.wgsl has the section")
+ 1;
let to = from
+ source[from..]
.find(&format!("\n{end}"))
.expect("terrain.wgsl ends the section")
+ 1;
&source[from..to]
}
/// terrain.wgsl's sky with every name it declares (and `globals`) prefixed.
fn vanilla_sky() -> String {
let text = [
section(TERRAIN, "struct Globals {", "@group(0) @binding(0)"),
section(TERRAIN, "struct Globals {", "@group(0) @binding(1)"),
section(TERRAIN, "fn linear_fog(", "fn apply_fog("),
section(TERRAIN, "struct SkyOut {", "// rendertype_lines"),
]
.concat();
let mut names = vec!["globals"];
for line in text.lines() {
let line = line
.trim_start_matches("@vertex ")
.trim_start_matches("@fragment ");
for keyword in ["fn ", "struct ", "const "] {
if let Some(rest) = line.strip_prefix(keyword) {
let end = rest
.find(|c: char| !(c.is_ascii_alphanumeric() || c == '_'))
.unwrap_or(rest.len());
names.push(&rest[..end]);
}
}
}
let mut out = String::with_capacity(text.len() * 2);
let mut word = String::new();
let flush = |word: &mut String, out: &mut String| {
if names.contains(&word.as_str()) {
out.push_str(PREFIX);
}
out.push_str(word);
word.clear();
};
for c in text.chars() {
if c.is_ascii_alphanumeric() || c == '_' {
word.push(c);
} else {
flush(&mut word, &mut out);
out.push(c);
}
}
flush(&mut word, &mut out);
out + &format!("@group(0) @binding(0) var<uniform> {PREFIX}globals: {PREFIX}Globals;\n")
prefixed(&text, PREFIX)
}
/// The sky shader for the pack's sky stage `source`.
@@ -0,0 +1,124 @@
//! Copies of the renderer's own WGSL that run beside a shader pack's code in one module (the
//! sky and material stages of Horizon's packs, docs/shaders.md): every name such a copy
//! declares takes a prefix starting with `horizon_`, which the pack check refuses in a pack,
//! so no pack name can meet one of Horizon's.
/// terrain.wgsl, which the copies are taken from.
pub const TERRAIN: &str = include_str!("../terrain.wgsl");
/// The text of `source` from the line starting with `start` up to (not including) the one
/// starting with `end`.
pub fn section<'a>(source: &'a str, start: &str, end: &str) -> &'a str {
let from = source
.find(&format!("\n{start}"))
.expect("the source has the section")
+ 1;
let to = from
+ source[from..]
.find(&format!("\n{end}"))
.expect("the source ends the section")
+ 1;
&source[from..to]
}
/// The names `text` declares at its top level: functions, structs, constants, overrides and
/// module variables (bindings).
fn declared(text: &str) -> Vec<&str> {
let mut names = Vec::new();
for line in text.lines() {
// Top level only: a function's own `var` and `let` stay as they are.
if line.starts_with(char::is_whitespace) {
continue;
}
let mut line = line;
while let Some(rest) = line.strip_prefix('@') {
// An attribute: `@vertex`, `@group(0)`, `@binding(1)`.
let end = rest.find(' ').unwrap_or(rest.len());
line = rest[end..].trim_start();
}
let rest = ["fn ", "struct ", "const ", "override ", "var "]
.iter()
.find_map(|k| line.strip_prefix(k))
.or_else(|| {
// `var<uniform> name`, `var<storage, read> name`.
let rest = line.strip_prefix("var<")?;
Some(rest[rest.find('>')? + 1..].trim_start())
});
if let Some(rest) = rest {
let end = rest
.find(|c: char| !(c.is_ascii_alphanumeric() || c == '_'))
.unwrap_or(rest.len());
names.push(&rest[..end]);
}
}
names
}
/// `text` with every name it declares at its top level, and every use of one, prefixed by
/// `prefix`. Comments are rewritten too, which is harmless.
pub fn prefixed(text: &str, prefix: &str) -> String {
let names = declared(text);
let mut out = String::with_capacity(text.len() * 2);
let mut word = String::new();
let flush = |word: &mut String, out: &mut String| {
if names.contains(&word.as_str()) {
out.push_str(prefix);
}
out.push_str(word);
word.clear();
};
for c in text.chars() {
if c.is_ascii_alphanumeric() || c == '_' {
word.push(c);
} else {
flush(&mut word, &mut out);
out.push(c);
}
}
flush(&mut word, &mut out);
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn prefixes_top_level_names_and_their_uses_only() {
let text = "struct S { a: f32, }\n\
@group(0) @binding(0) var<uniform> g: S;\n\
@group(1) @binding(2) var<storage, read> list: array<u32>;\n\
override N: u32 = 1u;\n\
const K: f32 = 2.0;\n\
@fragment fn f(x: f32) -> @location(0) vec4<f32> {\n \
var a = g.a * K;\n \
return vec4(a + f32(list[N]) + x);\n\
}\n";
let out = prefixed(text, "p_");
assert_eq!(
out,
"struct p_S { a: f32, }\n\
@group(0) @binding(0) var<uniform> p_g: p_S;\n\
@group(1) @binding(2) var<storage, read> p_list: array<u32>;\n\
override p_N: u32 = 1u;\n\
const p_K: f32 = 2.0;\n\
@fragment fn p_f(x: f32) -> @location(0) vec4<f32> {\n \
var a = p_g.a * p_K;\n \
return vec4(a + f32(p_list[p_N]) + x);\n\
}\n"
);
// Every top-level declaration of terrain.wgsl is found.
let names = declared(TERRAIN);
for name in [
"Globals",
"globals",
"sprites_7",
"SPRITE_SPLIT",
"block_slots",
"fs_sky",
] {
assert!(names.contains(&name), "{name}");
}
assert!(!names.contains(&"out"));
}
}