Files
horizon-airdef/crates/horizon-shader-api/src/reference.rs
T
Ludovic Marie 923fdee42e Horizon shader packs: the check every pack passes before it runs
horizon_shader_api::check builds each stage as the renderer does and judges
it the same way wherever it runs (the game, the launcher, the registry's CI,
the coming horizon-shader tool): naga parses and validates it; the stage
function must have its signature; the pack's files declare no bindings, no
entry points and no horizon_ names; each override is an option of its type or
has a default, and each option is used; a stage stays within 20 000
expressions and 64 texture reads; and every entry point translates to SPIR-V,
Metal and HLSL with the options at their defaults, minimums and maximums.
Problems carry the pack's own file, line and column. Pack::sha256 gives the
reproducible package's hash.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 15:34:07 +01:00

98 lines
4.6 KiB
Rust

//! docs/api/shaders-reference.md, generated from the stages and inputs this
//! crate defines (`tests/reference.rs` fails when the file drifts).
use crate::check::{MAX_EXPRESSIONS, MAX_TEXTURE_READS};
use crate::inputs::{Frame, SURFACE, Struct};
use crate::manifest::{API, MAX_GPU_MS, MAX_OPTIONS, MAX_POST_PASSES};
use crate::package::{MAX_TOTAL, MAX_WGSL};
use crate::stages::{GROUP, StageKind};
fn table(s: &Struct, offsets: Option<&[usize]>) -> String {
let mut out = format!("### `{}`\n\n{}\n\n", s.name, one_line(s.doc));
out += if offsets.is_some() {
"| Field | Type | Offset | Meaning |\n| --- | --- | --- | --- |\n"
} else {
"| Field | Type | Meaning |\n| --- | --- | --- |\n"
};
for (i, f) in s.fields.iter().enumerate() {
let offset = offsets.map_or(String::new(), |o| format!(" {} |", o[i]));
out += &format!(
"| `{}` | `{}` |{offset} {} |\n",
f.name,
f.ty,
one_line(f.doc)
);
}
out + "\n"
}
fn one_line(doc: &str) -> String {
doc.split_whitespace().collect::<Vec<_>>().join(" ")
}
pub fn markdown() -> String {
let mut out = format!(
"# Shader API reference ({API})\n\n\
Generated from `crates/horizon-shader-api` (`HORIZON_WRITE_DOCS=1 cargo test -p horizon-shader-api --test reference`\n\
rewrites it; the test fails when it drifts). The API's design and pack format: [shaders.md](shaders.md).\n\n\
A stage is a function your WGSL defines, with the signature below; Horizon puts the prelude (these structs, the\n\
resources and helpers) before your files and calls the function from its own pass. Your files declare no\n\
bindings and no entry points, and no names starting with `horizon_`.\n\n\
## Stages\n\n\
| Stage | In `horizon-shaders.toml` | Your function |\n| --- | --- | --- |\n"
);
for kind in StageKind::ALL {
let key = match kind {
StageKind::Post => "[[post]]",
StageKind::Sky => "[sky]",
StageKind::Material => "[material]",
};
out += &format!("| {} | `{key}` | `{}` |\n", kind.name(), kind.signature());
}
out += "\n";
for kind in StageKind::ALL {
out += &format!("## {}\n\n`{}`\n\n", kind.name(), kind.signature());
out += &table(kind.input(), None);
if kind == StageKind::Material {
out += &table(&SURFACE, None);
}
out += &format!(
"Resources (bind group {GROUP}):\n\n| Binding | Declaration | Meaning |\n| --- | --- | --- |\n"
);
for r in kind.resources() {
out += &format!("| {} | `{}` | {} |\n", r.binding, r.ty, r.doc);
}
out += "\n";
if kind == StageKind::Post {
out += "Helpers:\n\n\
- `fn depth_at(uv: vec2<f32>) -> f32`: the world's depth at `uv`.\n\
- `fn view_position(uv: vec2<f32>, depth: f32) -> vec3<f32>`: the camera-relative position, in blocks, of\n \
the point at `uv` and `depth`.\n\n";
}
}
out += "## The frame\n\n";
out += &table(&Frame::WGSL, Some(Frame::OFFSETS));
out += &format!(
"## Options\n\n\
Each `[[options]]` entry sets the WGSL `override` of the same name when the pipelines are built:\n\n\
| `type` | Keys | The `override` |\n| --- | --- | --- |\n\
| `bool` | `default` | `override <id>: bool` |\n\
| `float` | `default`, `min`, `max`, `step` | `override <id>: f32` |\n\
| `int` | `default`, `min`, `max` | `override <id>: i32` |\n\
| `choice` | `values`, `default` (one of them) | `override <id>: u32`, the value's index |\n\n\
## Limits\n\n\
- {MAX_POST_PASSES} post passes, {MAX_OPTIONS} options, `budget.gpu_ms` up to {MAX_GPU_MS}.\n\
- {} KiB per WGSL file, {} MiB per pack.\n\
- Per stage: {MAX_EXPRESSIONS} naga expressions over all its functions, {MAX_TEXTURE_READS} texture reads (samples,\n \
gathers and loads, the prelude's included).\n\n\
## The check\n\n\
A pack runs only after it passes `horizon_shader_api::check` (the game when it loads a pack, the launcher before it\n\
installs one, the registry's CI): each stage is parsed and validated by naga, checked against its signature and\n\
the rules above, and translated for Vulkan (SPIR-V), Metal and DirectX 12 (HLSL) with its options at their\n\
defaults, minimums and maximums. Every option must be an `override` of its type in some stage; an `override`\n\
that is no option needs a default.\n",
MAX_WGSL >> 10,
MAX_TOTAL >> 20
);
out
}