Shader packs, clean room 8: a dimension's post-processing programs

What the renderer needs to run a pack's post-processing, still without
wgpu: the programs of one dimension (prepare, deferred, composite and
final with their numbered variants, in the order they run), each
preprocessed and translated, with the buffers it writes and the flips
that follow it, and the buffer settings its stage files carry.

- A dimension's folder (world0, world-1, world1) replaces the base folder
  when it exists. program.<name>.enabled expressions over switch options,
  flip.<program>.<buffer> keys and the virtual deferred_pre and
  composite_pre. Programs with a geometry stage, or that fail to
  preprocess or translate, are reported and skipped.
- tests/packs/post-test: Horizon's own small test pack (tint, depth in
  colortex2, depth bands on half the screen, vignette).

Black box over the public test packs: every dimension of every pack
loads its post-processing programs (2 to 9) with no note.

Nothing uses it yet: players see no change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-24 22:23:43 +02:00
parent d064dba945
commit 6b2ec5f830
11 changed files with 407 additions and 1 deletions
+3
View File
@@ -9,6 +9,7 @@
//! - [`preprocess`]: a stage file through `#include` and the GLSL preprocessor, lines mapped back (1.4, 1.5).
//! - [`translate_composite`]: a composite-style program into naga modules and its interface (1.6, 2.2).
//! - [`Config`] and [`draw_buffers`]: the buffer settings and output lists stage files carry (1.7, 6.1).
//! - [`load_post`]: a dimension's `prepare`, `deferred`, `composite` and `final` programs, in order (2.1).
//! - [`Macros`] and [`eval_condition`]: the macros packs test, and the `#if` arithmetic (1.5).
mod condition;
@@ -17,6 +18,7 @@ mod discover;
mod lang;
mod macros;
mod pack;
mod post;
mod preprocess;
mod properties;
mod translate;
@@ -27,6 +29,7 @@ pub use discover::{Found, discover};
pub use lang::Lang;
pub use macros::{MC_VERSION, Macros, RENDER_STAGES};
pub use pack::{Pack, PackError};
pub use post::{Group, Post, PostProgram, dimension_folder, load_post};
pub use preprocess::{Diagnostic, Origin, Preprocessed, preprocess};
pub use properties::{Entry, Note, Properties};
pub use translate::{Error, Interface, Program, Sampler, Target, Uniform, translate_composite};
+295
View File
@@ -0,0 +1,295 @@
//! The post-processing programs of one dimension, ready for the renderer: `prepare`, `deferred`,
//! `composite` and `final` with their numbered variants, in the order they run (spec 2.1), each
//! preprocessed and translated, with the buffers it writes and the flips that follow it, and the
//! buffer settings their files carry (6.1).
//!
//! Decisions (impl-plan.md):
//! - OQ-12: a dimension's folder (`world0`, `world-1`, `world1`) replaces the base folder for that
//! dimension when it exists; every stage file is read from it.
//! - OQ-29: a program with a geometry stage is reported and treated as missing.
//! - A program that fails to translate is reported and treated as missing (2.5).
use crate::config::{self, Config};
use crate::{Macros, Pack, Preprocessed, Program, Properties, Target, eval_condition};
use crate::{preprocess, translate_composite};
/// Which group of 2.1 a program belongs to.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Group {
Prepare,
Deferred,
Composite,
Final,
}
const GROUPS: [(Group, &str); 4] = [
(Group::Prepare, "prepare"),
(Group::Deferred, "deferred"),
(Group::Composite, "composite"),
(Group::Final, "final"),
];
pub struct PostProgram {
/// The program's name (`composite3`).
pub name: String,
pub group: Group,
pub program: Program,
/// colortex buffer of each fragment output location (location n writes `buffers[n]`).
pub buffers: Vec<u32>,
/// `flip.<name>.<buffer>`: buffers forced to flip (true) or kept (false) after this program.
pub flips: Vec<(u32, bool)>,
}
pub struct Post {
/// The folder the programs came from: `""` for `shaders` itself, else `world0`...
pub folder: String,
pub programs: Vec<PostProgram>,
/// The flips of the virtual `deferred_pre` and `composite_pre` (6.1.6).
pub deferred_pre: Vec<(u32, bool)>,
pub composite_pre: Vec<(u32, bool)>,
pub config: Config,
/// Programs skipped and why, as `program: message` or `file:line: message`.
pub notes: Vec<String>,
}
/// The folder a dimension reads its programs from: `world<id>` when the pack has it, else the
/// base folder `""` (1.2). `id` is the legacy number: 0 the Overworld, -1 the Nether, 1 the End.
pub fn dimension_folder(pack: &Pack, id: i32) -> String {
let folder = format!("world{id}");
if pack.list("").iter().any(|f| f == &folder) || !pack.list(&folder).is_empty() {
folder
} else {
String::new()
}
}
/// Loads the post-processing programs of `folder`.
pub fn load_post(pack: &Pack, folder: &str, properties: &Properties, macros: &Macros) -> Post {
let mut post = Post {
folder: folder.to_owned(),
programs: Vec::new(),
deferred_pre: flips(properties, "deferred_pre"),
composite_pre: flips(properties, "composite_pre"),
config: Config::default(),
notes: Vec::new(),
};
let path = |name: &str, ext: &str| match folder {
"" => format!("{name}.{ext}"),
f => format!("{f}/{name}.{ext}"),
};
for (group, base) in GROUPS {
let names: Vec<String> = if group == Group::Final {
vec![base.to_owned()]
} else {
std::iter::once(base.to_owned())
.chain((1..=99).map(|n| format!("{base}{n}")))
.collect()
};
for name in names {
let (vsh, fsh) = (path(&name, "vsh"), path(&name, "fsh"));
if !pack.exists(&fsh) || !pack.exists(&vsh) {
continue;
}
if !enabled(properties, folder, &name, macros) {
continue;
}
if pack.exists(&path(&name, "gsh")) {
post.notes.push(format!(
"{name}: uses a geometry stage (not supported); skipped"
));
continue;
}
let vertex = preprocess(pack, &vsh, macros);
let fragment = preprocess(pack, &fsh, macros);
let problems: Vec<String> = [&vertex, &fragment]
.iter()
.flat_map(|pp| diagnostics(pp))
.collect();
if !problems.is_empty() {
post.notes.extend(problems);
post.notes.push(format!("{name}: skipped"));
continue;
}
let target = match group {
Group::Final => Target::Window,
_ => Target::Buffers,
};
let program = match translate_composite(&vertex, &fragment, target) {
Ok(p) => p,
Err(errors) => {
post.notes.extend(
errors
.iter()
.map(|e| format!("{}:{}: {}", e.file, e.line, e.message)),
);
post.notes.push(format!("{name}: skipped"));
continue;
}
};
post.config.read(&vertex);
post.config.read(&fragment);
let buffers = config::draw_buffers(&fragment).unwrap_or_else(|| (0..8).collect());
post.programs.push(PostProgram {
flips: flips(properties, &name),
name,
group,
program,
buffers,
});
}
}
post.config = std::mem::take(&mut post.config).finish();
post
}
fn diagnostics(pp: &Preprocessed) -> Vec<String> {
pp.diagnostics
.iter()
.map(|d| {
let file = pp
.files
.get(d.origin.file as usize)
.map_or("?", String::as_str);
format!("{file}:{}: {}", d.origin.line, d.message)
})
.collect()
}
/// `program.<name>.enabled` (or `program.<folder>/<name>.enabled`): a boolean expression over
/// switch options (3.3). An option is on when its macro is defined.
fn enabled(properties: &Properties, folder: &str, name: &str, macros: &Macros) -> bool {
let keys = [
format!("program.{folder}/{name}.enabled"),
format!("program.{name}.enabled"),
];
let Some(expr) = keys.iter().find_map(|k| properties.get(k)) else {
return true;
};
// `true`/`false` as numbers, every other name as `defined(name)`, then the `#if` arithmetic.
let mut condition = String::new();
for token in crate::macros::lex(expr) {
match token {
"true" => condition.push('1'),
"false" => condition.push('0'),
t if t.starts_with(|c: char| c.is_alphabetic() || c == '_') => {
condition.push_str(&format!("defined({t})"));
}
t => condition.push_str(t),
}
}
eval_condition(&condition, macros).ok() != Some(0)
}
/// `flip.<program>.<buffer>=<bool>` keys of one program.
fn flips(properties: &Properties, program: &str) -> Vec<(u32, bool)> {
let prefix = format!("flip.{program}.");
properties
.entries
.iter()
.filter_map(|e| {
let buffer = config::buffer_index(e.key.strip_prefix(&prefix)?)?;
let value = match e.value.trim() {
"true" => true,
"false" => false,
_ => return None,
};
Some((buffer as u32, value))
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::pack::tests::zip;
const VSH: &str = "#version 120\nvarying vec2 uv;\nvoid main() {\n\
gl_Position = ftransform();\nuv = gl_MultiTexCoord0.xy;\n}\n";
const FSH: &str = "#version 120\nuniform sampler2D colortex0;\nvarying vec2 uv;\n\
void main() {\n/* DRAWBUFFERS:3 */\n\
gl_FragData[0] = texture2D(colortex0, uv);\n}\n";
#[test]
fn programs_load_in_order_with_their_settings() {
let pack = Pack::from_zip_bytes(
"t",
zip(&[
("shaders/composite.vsh", VSH),
("shaders/composite.fsh", FSH),
("shaders/composite2.vsh", VSH),
("shaders/composite2.fsh", FSH),
("shaders/deferred.vsh", VSH),
(
"shaders/deferred.fsh",
"#version 120\nvoid main() { gl_FragColor = vec4(1.0); }\n",
),
("shaders/composite1.fsh", FSH),
("shaders/composite3.vsh", VSH),
("shaders/composite3.fsh", FSH),
("shaders/composite4.vsh", VSH),
("shaders/composite4.fsh", FSH),
("shaders/composite4.gsh", "x"),
("shaders/composite5.vsh", VSH),
(
"shaders/composite5.fsh",
"#version 120\nvoid main() { undefined(); }\n",
),
("shaders/final.vsh", VSH),
("shaders/final.fsh", FSH),
("shaders/world-1/final.vsh", VSH),
("shaders/world-1/final.fsh", FSH),
]),
)
.unwrap();
let properties = Properties::parse(
"program.composite3.enabled = HQ && !LOW\nflip.composite2.gaux1=true\n\
flip.composite.colortex3 = false\nflip.composite_pre.colortex1=true\n",
&Macros::default(),
);
assert_eq!(dimension_folder(&pack, 0), "");
assert_eq!(dimension_folder(&pack, -1), "world-1");
let post = load_post(&pack, "", &properties, &Macros::standard());
let names: Vec<&str> = post.programs.iter().map(|p| p.name.as_str()).collect();
assert_eq!(names, ["deferred", "composite", "composite2", "final"]);
let p = &post.programs;
assert_eq!(p[0].buffers, (0..8).collect::<Vec<_>>());
assert_eq!(p[1].buffers, [3]);
assert_eq!(p[1].flips, [(3, false)]);
assert_eq!(p[2].flips, [(4, true)]);
assert_eq!(post.composite_pre, [(1, true)]);
assert_eq!(p[3].group, Group::Final);
let notes = post.notes.join("\n");
assert!(
notes.contains("composite4: uses a geometry stage"),
"{notes}"
);
assert!(notes.contains("composite5.fsh:2:"), "{notes}");
assert!(notes.contains("composite5: skipped"), "{notes}");
let mut macros = Macros::standard();
macros.define("HQ", "");
let post = load_post(&pack, "world-1", &properties, &macros);
let names: Vec<&str> = post.programs.iter().map(|p| p.name.as_str()).collect();
assert_eq!(names, ["final"]);
let post = load_post(&pack, "", &properties, &macros);
assert!(post.programs.iter().any(|p| p.name == "composite3"));
}
#[test]
fn horizons_test_pack_loads() {
let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/packs/post-test");
let pack = Pack::open(&dir).unwrap();
let macros = Macros::standard();
let properties = Properties::parse(&pack.read_text("shaders.properties").unwrap(), &macros);
let post = load_post(&pack, &dimension_folder(&pack, 0), &properties, &macros);
assert!(post.notes.is_empty(), "{:?}", post.notes);
let names: Vec<&str> = post.programs.iter().map(|p| p.name.as_str()).collect();
assert_eq!(names, ["composite", "composite1", "final"]);
assert_eq!(post.programs[0].buffers, [0, 2]);
assert_eq!(post.programs[1].buffers, [0]);
assert_eq!(
post.config.buffers[2].format,
crate::Format::parse("RGBA16F").unwrap()
);
}
}
@@ -0,0 +1,6 @@
# post-test
Horizon's own shader pack for testing post-processing (written for Horizon, no other pack's code).
`composite` tints the world warm and stores linear depth in colortex2, `composite1` shows that
depth as fog bands on the right half with a little noise, and `final` adds a vignette. Copy the
folder into `shaderpacks/` and run the game with `HORIZON_SHADER_PACK=post-test`.
@@ -0,0 +1,23 @@
#version 120
/*
const int colortex2Format = RGBA16F;
*/
uniform sampler2D colortex0;
uniform sampler2D depthtex0;
uniform mat4 gbufferProjectionInverse;
uniform float far;
varying vec2 texcoord;
void main() {
vec3 color = texture2D(colortex0, texcoord).rgb;
float depth = texture2D(depthtex0, texcoord).r;
// Window depth back to a view-space distance.
vec4 view = gbufferProjectionInverse * vec4(texcoord * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
float distance = -view.z / view.w;
/* DRAWBUFFERS:02 */
gl_FragData[0] = vec4(color * vec3(1.08, 1.0, 0.86), 1.0);
gl_FragData[1] = vec4(distance / far, depth, 0.0, 1.0);
}
@@ -0,0 +1,8 @@
#version 120
varying vec2 texcoord;
void main() {
gl_Position = ftransform();
texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;
}
@@ -0,0 +1,25 @@
#version 330 compatibility
uniform sampler2D colortex0;
uniform sampler2D colortex2;
uniform sampler2D noisetex;
uniform float viewWidth;
uniform float viewHeight;
in vec2 texcoord;
/* RENDERTARGETS: 0 */
layout(location = 0) out vec4 outColor;
void main() {
vec3 color = texture(colortex0, texcoord).rgb;
if (gl_FragCoord.x > viewWidth * 0.5) {
float d = clamp(texture(colortex2, texcoord).r, 0.0, 1.0);
float band = step(0.5, fract(d * 8.0));
vec3 fog = mix(vec3(0.2, 0.45, 0.9), vec3(0.95, 0.75, 0.3), d);
color = mix(color, fog, 0.35 + 0.15 * band);
float grain = texture(noisetex, gl_FragCoord.xy / 256.0).r - 0.5;
color += grain * 0.04;
}
outColor = vec4(color, 1.0);
}
@@ -0,0 +1,8 @@
#version 330 compatibility
out vec2 texcoord;
void main() {
gl_Position = ftransform();
texcoord = gl_MultiTexCoord0.xy;
}
@@ -0,0 +1,12 @@
#version 120
uniform sampler2D colortex0;
varying vec2 texcoord;
void main() {
vec3 color = texture2D(colortex0, texcoord).rgb;
vec2 centred = texcoord - 0.5;
float vignette = 1.0 - dot(centred, centred) * 0.9;
gl_FragColor = vec4(color * vignette, 1.0);
}
@@ -0,0 +1,8 @@
#version 120
varying vec2 texcoord;
void main() {
gl_Position = ftransform();
texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;
}
@@ -0,0 +1,2 @@
# Horizon's post-processing test pack.
program.composite1.enabled = !NO_SPLIT
+17 -1
View File
@@ -8,7 +8,8 @@ use std::time::Instant;
use std::collections::BTreeMap;
use horizon_shaders::{
Lang, Macros, Pack, Properties, Target, discover, preprocess, translate_composite,
Lang, Macros, Pack, Properties, Target, dimension_folder, discover, load_post, preprocess,
translate_composite,
};
#[test]
@@ -157,6 +158,21 @@ fn real_packs_read() {
for (cause, n) in causes.iter().take(6) {
println!(" {n} x {cause}");
}
// The post-processing set of each dimension, as the game loads it.
for id in [0, -1, 1] {
let start = std::time::Instant::now();
let folder = dimension_folder(&pack, id);
let post = load_post(&pack, &folder, &shaders, &Macros::standard());
println!(
" dimension {id} (folder `{folder}`): {} post programs, {} notes, {:.0} ms",
post.programs.len(),
post.notes.len() + post.config.notes.len(),
start.elapsed().as_secs_f64() * 1000.0
);
for n in post.notes.iter().chain(&post.config.notes).take(5) {
println!(" {n}");
}
}
assert!(!shaders.entries.is_empty(), "{}: no keys read", f.name);
read += 1;
}