From e1d9ae1dd7d8d579e4b5923f85c81c2e30535c00 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 22:34:29 +0200 Subject: [PATCH] horizon-shaderpack: plan a Java pack's full-screen passes like Iris The begin, prepare, deferred, composite and final passes of a program folder: what each writes (the later of DRAWBUFFERS and RENDERTARGETS), which side of every ping-ponged colour target it reads (Iris's BufferFlipper, with flip.* overrides), mipmapped targets, scale.*, and the targets' formats, clearing and sizes from the consts of every pass in Iris's order. The preprocessor now keeps the directive comments of active code, as Iris's does, so a DRAWBUFFERS in an #if'd out branch doesn't count. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-shaderpack/src/lib.rs | 1 + crates/horizon-shaderpack/src/pipeline.rs | 640 ++++++++++++++++++ crates/horizon-shaderpack/src/preprocess.rs | 94 ++- .../fixtures/passes/shaders/composite.fsh | 17 + .../fixtures/passes/shaders/composite.vsh | 3 + .../fixtures/passes/shaders/composite1.fsh | 7 + .../fixtures/passes/shaders/composite1.vsh | 3 + .../fixtures/passes/shaders/composite2.fsh | 5 + .../fixtures/passes/shaders/composite2.vsh | 3 + .../fixtures/passes/shaders/deferred.fsh | 11 + .../fixtures/passes/shaders/deferred.vsh | 3 + .../tests/fixtures/passes/shaders/final.fsh | 6 + .../tests/fixtures/passes/shaders/final.vsh | 3 + .../passes/shaders/shaders.properties | 6 + crates/horizon-shaderpack/tests/pack.rs | 96 ++- docs/shader-packs.md | 1 + 16 files changed, 897 insertions(+), 2 deletions(-) create mode 100644 crates/horizon-shaderpack/src/pipeline.rs create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite.fsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite.vsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite1.fsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite1.vsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite2.fsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite2.vsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/deferred.fsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/deferred.vsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/final.fsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/final.vsh create mode 100644 crates/horizon-shaderpack/tests/fixtures/passes/shaders/shaders.properties diff --git a/crates/horizon-shaderpack/src/lib.rs b/crates/horizon-shaderpack/src/lib.rs index d65f4ee39..0cfec68f9 100644 --- a/crates/horizon-shaderpack/src/lib.rs +++ b/crates/horizon-shaderpack/src/lib.rs @@ -7,6 +7,7 @@ pub mod include; pub mod options; +pub mod pipeline; pub mod preprocess; pub mod properties; pub mod source; diff --git a/crates/horizon-shaderpack/src/pipeline.rs b/crates/horizon-shaderpack/src/pipeline.rs new file mode 100644 index 000000000..f9dc00cc5 --- /dev/null +++ b/crates/horizon-shaderpack/src/pipeline.rs @@ -0,0 +1,640 @@ +//! Iris's full-screen passes and the colour targets they draw to: which +//! programs run (`begin*`, `prepare*`, `deferred*`, `composite*`, `final`), +//! the targets each one writes (`/* DRAWBUFFERS:01 */`, `/* RENDERTARGETS: +//! 0,1 */`), the targets' formats and clearing (`const int colortex0Format +//! = RGBA16F;` and friends), and which side of each ping-ponged target a +//! pass reads (`BufferFlipper`). Read from the preprocessed sources with +//! their comments, as Iris's `ProgramDirectives`, `PackRenderTargetDirectives` +//! and `CompositeRenderer` do. + +use crate::{ + JavaPack, options::OptionValues, preprocess::Environment, properties::LEGACY_TARGETS, + properties::ShaderProperties, +}; +use std::collections::{BTreeMap, BTreeSet}; + +/// Colour targets Iris supports (`IrisLimits.MAX_COLOR_BUFFERS`). +pub const MAX_COLOR_BUFFERS: u32 = 32; + +/// The formats `const int colortexNFormat` may name (`InternalTextureFormat`). +pub const TEXTURE_FORMATS: &[&str] = &[ + "RGBA", + "R8", + "RG8", + "RGB8", + "RGBA8", + "R8_SNORM", + "RG8_SNORM", + "RGB8_SNORM", + "RGBA8_SNORM", + "R16", + "RG16", + "RGB16", + "RGBA16", + "R16_SNORM", + "RG16_SNORM", + "RGB16_SNORM", + "RGBA16_SNORM", + "R16F", + "RG16F", + "RGB16F", + "RGBA16F", + "R32F", + "RG32F", + "RGB32F", + "RGBA32F", + "R8I", + "RG8I", + "RGB8I", + "RGBA8I", + "R8UI", + "RG8UI", + "RGB8UI", + "RGBA8UI", + "R16I", + "RG16I", + "RGB16I", + "RGBA16I", + "R16UI", + "RG16UI", + "RGB16UI", + "RGBA16UI", + "R32I", + "RG32I", + "RGB32I", + "RGBA32I", + "R32UI", + "RG32UI", + "RGB32UI", + "RGBA32UI", + "RGBA2", + "RGBA4", + "R3_G3_B2", + "RGB5_A1", + "RGB565", + "RGB10_A2", + "RGB10_A2UI", + "R11F_G11F_B10F", + "RGB9_E5", +]; + +/// The value of the last `/* NAME:value */` in `text` and where it starts +/// (`CommentDirectiveParser.findDirective`). +pub fn comment_directive(text: &str, name: &str) -> Option<(usize, String)> { + let prefix = format!("{name}:"); + let at = text.rfind(&prefix)?; + if !text[..at].trim_end().ends_with("/*") { + return None; + } + let rest = &text[at + prefix.len()..]; + let end = rest.find("*/")?; + Some((at, rest[..end].trim().to_owned())) +} + +/// A `const = ;` line (`ConstDirectiveParser`). +#[derive(Clone, Debug, PartialEq)] +pub struct ConstDirective { + /// `int`, `float`, `vec2`, `ivec3`, `vec4` or `bool`. + pub ty: &'static str, + pub key: String, + pub value: String, +} + +/// Every line of `text` that starts with a `const` Iris reads, in order. +pub fn const_directives(text: &str) -> Vec { + text.lines().filter_map(const_directive).collect() +} + +fn const_directive(line: &str) -> Option { + if !line.contains("const") || !line.contains('=') || !line.contains(';') { + return None; + } + let rest = line.trim().strip_prefix("const")?; + if !rest.starts_with(char::is_whitespace) { + return None; + } + let rest = rest.trim_start(); + // In Iris's order: `ivec3` before `vec4` doesn't matter, but `int` must + // not take `ivec3`'s place, and it can't (`ivec3` starts with `i`, `v`). + let ty = ["int", "float", "vec2", "ivec3", "vec4", "bool"] + .into_iter() + .find(|t| rest.starts_with(t))?; + let rest = &rest[ty.len()..]; + if !rest.starts_with(char::is_whitespace) { + return None; + } + let (key, value) = rest.split_once('=')?; + let key = key.trim(); + if key.is_empty() || !key.chars().all(|c| c.is_alphanumeric() || c == '_') { + return None; + } + let value = value.split_once(';')?.0.trim(); + Some(ConstDirective { + ty, + key: key.to_owned(), + value: value.to_owned(), + }) +} + +/// The targets a fragment program writes, by output location: the later of +/// its `DRAWBUFFERS` and `RENDERTARGETS` directives, or `None` without one +/// (Iris then takes the `gl_FragData` indices it writes). +pub fn draw_buffers(fragment: &str) -> Result>, String> { + let draw = comment_directive(fragment, "DRAWBUFFERS"); + let render = comment_directive(fragment, "RENDERTARGETS"); + let (digits, value) = match (draw, render) { + (Some(d), Some(r)) if d.0 > r.0 => (true, d.1), + (_, Some(r)) => (false, r.1), + (Some(d), None) => (true, d.1), + (None, None) => return Ok(None), + }; + let bad = || format!("invalid render targets `{value}`"); + let targets: Vec = if digits { + value + .chars() + .map(|c| c.to_digit(10).ok_or_else(bad)) + .collect::>()? + } else { + value + .split(',') + .map(|t| t.trim().parse().map_err(|_| bad())) + .collect::>()? + }; + match targets.iter().find(|&&t| t >= MAX_COLOR_BUFFERS) { + Some(t) => Err(format!( + "render target {t} is not supported, only {MAX_COLOR_BUFFERS} are" + )), + None => Ok(Some(targets)), + } +} + +/// The index of a target name, `colortex` or a legacy one (`gaux1`). +pub fn target_index(name: &str) -> Option { + match LEGACY_TARGETS.iter().position(|t| *t == name) { + Some(i) => Some(i as u32), + None => name + .strip_prefix("colortex")? + .parse() + .ok() + .filter(|&i| i < MAX_COLOR_BUFFERS), + } +} + +/// A key naming a target and a suffix (`colortex4Format`, `gaux1Clear`). +fn target_key(key: &str, suffix: &str) -> Option { + target_index(key.strip_suffix(suffix)?) +} + +/// A colour target's settings (`RenderTargetSettings`, `size.buffer.*`). +#[derive(Clone, Debug, PartialEq)] +pub struct TargetSettings { + /// One of [`TEXTURE_FORMATS`]; `RGBA` (8 bits per channel) by default. + pub format: String, + /// Cleared every frame (the default), or kept for the next frame. + pub clear: bool, + /// Iris clears to black, or `colortex0` to the fog colour, without one. + pub clear_color: Option<[f32; 4]>, + /// `size.buffer.`: a fraction of the screen or a size in pixels. + pub size: Option, +} + +impl Default for TargetSettings { + fn default() -> Self { + Self { + format: "RGBA".into(), + clear: true, + clear_color: None, + size: None, + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum TargetSize { + Relative(f32, f32), + Pixels(u32, u32), +} + +/// Every target's settings from the consts of `fragments`, read in order so +/// the last one wins, then `size.buffer.*` from the properties. +pub fn target_settings<'a>( + fragments: impl IntoIterator, + properties: &ShaderProperties, +) -> Vec { + let mut targets = vec![TargetSettings::default(); MAX_COLOR_BUFFERS as usize]; + for fragment in fragments { + for c in const_directives(fragment) { + match c.ty { + "int" => { + if let Some(i) = target_key(&c.key, "Format") { + let format = c.value.to_uppercase(); + if TEXTURE_FORMATS.contains(&format.as_str()) { + targets[i as usize].format = format; + } + } + } + "bool" => { + if let (Some(i), Ok(clear)) = (target_key(&c.key, "Clear"), c.value.parse()) { + targets[i as usize].clear = clear; + } + } + "vec4" => { + if let (Some(i), Some(color)) = + (target_key(&c.key, "ClearColor"), parse_vec4(&c.value)) + { + targets[i as usize].clear_color = Some(color); + } + } + _ => {} + } + } + } + for (key, value) in properties.preprocessed.iter() { + let Some(i) = key.strip_prefix("size.buffer.").and_then(target_index) else { + continue; + }; + let parts: Vec<&str> = value.split_whitespace().collect(); + if let [w, h] = parts[..] { + targets[i as usize].size = if w.contains('.') || h.contains('.') { + w.parse() + .ok() + .zip(h.parse().ok()) + .map(|(w, h)| TargetSize::Relative(w, h)) + } else { + w.parse() + .ok() + .zip(h.parse().ok()) + .map(|(w, h)| TargetSize::Pixels(w, h)) + }; + } + } + targets +} + +fn parse_vec4(value: &str) -> Option<[f32; 4]> { + let args = value.strip_prefix("vec4")?.trim(); + let args = args.strip_prefix('(')?.strip_suffix(')')?; + let parts: Vec = args + .split(',') + .map(|p| p.trim().parse().ok()) + .collect::>()?; + parts.try_into().ok() +} + +/// One full-screen pass. +#[derive(Clone, Debug, PartialEq)] +pub struct Pass { + /// The program's path without extension (`world0/composite1`). + pub program: String, + /// What it writes, by output location; `None`: the outputs it writes. + pub draw_buffers: Option>, + /// Targets it reads from their alternate side (it writes the other side). + pub reads_alt: BTreeSet, + /// Targets whose mipmaps it wants generated before it runs. + pub mipmapped: BTreeSet, + /// `scale.`: the fraction of the screen it draws. + pub scale: f32, +} + +/// The full-screen passes of one program folder, in order, and the +/// flipping state between them. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Passes { + pub targets: Vec, + pub begin: Vec, + pub prepare: Vec, + /// The targets gbuffers programs (and Horizon's world) write on their + /// alternate side, before and after translucent geometry. + pub flipped_after_prepare: BTreeSet, + pub deferred: Vec, + pub flipped_after_translucent: BTreeSet, + pub composite: Vec, + /// `final`, which writes the screen; `None` without one (Iris then + /// copies `colortex0` to the screen). + pub final_pass: Option, + /// After `final`, these targets' alternate side is copied back so the + /// next frame starts unflipped. + pub flipped_at_end: BTreeSet, +} + +/// Iris's single programs (`ProgramId`), in order. +const SINGLE_PROGRAMS: &[&str] = &[ + "shadow", + "shadow_solid", + "shadow_cutout", + "shadow_water", + "shadow_entities", + "shadow_lightning", + "shadow_block", + "gbuffers_basic", + "gbuffers_line", + "gbuffers_textured", + "gbuffers_textured_lit", + "gbuffers_skybasic", + "gbuffers_skytextured", + "gbuffers_clouds", + "gbuffers_terrain", + "gbuffers_terrain_solid", + "gbuffers_terrain_cutout", + "gbuffers_damagedblock", + "gbuffers_block", + "gbuffers_block_translucent", + "gbuffers_beaconbeam", + "gbuffers_item", + "gbuffers_entities", + "gbuffers_entities_translucent", + "gbuffers_lightning", + "gbuffers_particles", + "gbuffers_particles_translucent", + "gbuffers_entities_glowing", + "gbuffers_armor_glint", + "gbuffers_spidereyes", + "gbuffers_hand", + "gbuffers_weather", + "gbuffers_water", + "gbuffers_hand_water", + "dh_terrain", + "dh_water", + "dh_generic", + "dh_shadow", + "final", +]; + +/// The program groups that run as full-screen passes, in Iris's order. +const GROUPS: &[&str] = &["begin", "prepare", "deferred", "composite"]; + +impl JavaPack { + /// The full-screen passes of the programs in `dir` (`world0`, or `""` + /// for the pack's root), with the player's `values`. + pub fn passes( + &self, + dir: &str, + values: &OptionValues, + env: &Environment, + properties: &ShaderProperties, + ) -> Result { + let path = |name: &str| { + if dir.is_empty() { + name.to_owned() + } else { + format!("{dir}/{name}") + } + }; + // A pass needs both halves, and its `program.*.enabled` to hold. + let exists = |name: &str| { + let p = path(name); + self.programs.contains(&format!("{p}.vsh")) + && self.programs.contains(&format!("{p}.fsh")) + && properties.program_enabled(&p, &self.options, values) + }; + let fragment = |name: &str| -> Result { + let file = format!("{}.fsh", path(name)); + Ok(self.preprocess(&file, values, env)?.with_comments()) + }; + let group = |base: &str| -> Vec { + (0..100) + .map(|i| { + if i == 0 { + base.to_owned() + } else { + format!("{base}{i}") + } + }) + .filter(|name| exists(name)) + .collect() + }; + let groups: Vec> = GROUPS.iter().map(|g| group(g)).collect(); + + // Target settings come from the fragments of every pass, in Iris's + // order (`ProgramSet.locateDirectives`): some packs declare them in + // `final` only. + let mut sources: Vec = Vec::new(); + for name in group("shadowcomp") + .iter() + .chain(&groups[0]) + .chain(&groups[1]) + { + sources.push(fragment(name)?); + } + for name in SINGLE_PROGRAMS { + if exists(name) { + sources.push(fragment(name)?); + } + } + let mut fragments: BTreeMap = BTreeMap::new(); + for name in groups[2].iter().chain(&groups[3]) { + let text = fragment(name)?; + sources.push(text.clone()); + fragments.insert(name.clone(), text); + } + for name in &groups[0..2].concat() { + fragments.insert(name.clone(), fragment(name)?); + } + let mut out = Passes { + targets: target_settings(sources.iter().map(String::as_str), properties), + ..Passes::default() + }; + + let flips = explicit_flips(properties); + let run = |group: &[String], pre: &str, flipped: &mut BTreeSet| { + *flipped = flips_pre(&flips, pre, flipped); + let mut passes = Vec::new(); + for name in group { + let text = &fragments[name]; + let pass = Pass { + program: path(name), + draw_buffers: draw_buffers(text).map_err(|e| format!("{}: {e}", path(name)))?, + reads_alt: flipped.clone(), + mipmapped: mipmapped(text), + scale: properties + .preprocessed + .get(&format!("scale.{name}")) + .and_then(|v| v.split_whitespace().next()?.parse().ok()) + .unwrap_or(1.0), + }; + *flipped = flip_set(&pass, &flips); + passes.push(pass); + } + Ok::<_, String>(passes) + }; + let mut flipped = BTreeSet::new(); + out.begin = run(&groups[0], "begin_pre", &mut flipped)?; + out.prepare = run(&groups[1], "prepare_pre", &mut flipped)?; + out.flipped_after_prepare = flipped.clone(); + out.deferred = run(&groups[2], "deferred_pre", &mut flipped)?; + out.flipped_after_translucent = flipped.clone(); + out.composite = run(&groups[3], "composite_pre", &mut flipped)?; + out.flipped_at_end = flipped; + if exists("final") { + out.final_pass = Some(Pass { + program: path("final"), + draw_buffers: None, + reads_alt: out.flipped_at_end.clone(), + mipmapped: mipmapped(&fragment("final")?), + scale: 1.0, + }); + } + Ok(out) + } +} + +fn toggle(set: &mut BTreeSet, buffer: u32) { + if !set.remove(&buffer) { + set.insert(buffer); + } +} + +/// `flip.. = true|false`, by pass and target index. +fn explicit_flips(properties: &ShaderProperties) -> BTreeMap<&str, BTreeMap> { + let mut out: BTreeMap<&str, BTreeMap> = BTreeMap::new(); + for (key, value) in properties.preprocessed.iter() { + let Some((pass, buffer)) = key.strip_prefix("flip.").and_then(|k| k.split_once('.')) else { + continue; + }; + if let (Some(i), Ok(flip)) = (target_index(buffer), value.trim().parse()) { + out.entry(pass).or_default().insert(i, flip); + } + } + out +} + +/// The flipped set after `pass` ran: what it read, flipped by its writes +/// and its explicit flips. +fn flip_set(pass: &Pass, flips: &BTreeMap<&str, BTreeMap>) -> BTreeSet { + let name = pass.program.rsplit('/').next().unwrap_or(&pass.program); + let explicit = flips.get(name); + let mut set = pass.reads_alt.clone(); + for &buffer in pass.draw_buffers.as_deref().unwrap_or(&[0]) { + if explicit.and_then(|f| f.get(&buffer)) != Some(&false) { + toggle(&mut set, buffer); + } + } + for (&buffer, &flip) in explicit.into_iter().flatten() { + if flip { + toggle(&mut set, buffer); + } + } + set +} + +fn flips_pre( + flips: &BTreeMap<&str, BTreeMap>, + pre: &str, + before: &BTreeSet, +) -> BTreeSet { + let mut set = before.clone(); + for (&buffer, &flip) in flips.get(pre).into_iter().flatten() { + if flip { + toggle(&mut set, buffer); + } + } + set +} + +/// `const bool colortexNMipmapEnabled = true;` in a program's fragment. +fn mipmapped(fragment: &str) -> BTreeSet { + let mut out = BTreeSet::new(); + for c in const_directives(fragment) { + if c.ty != "bool" { + continue; + } + if let Some(i) = target_key(&c.key, "MipmapEnabled") { + match c.value.as_str() { + "true" => out.insert(i), + "false" => out.remove(&i), + _ => false, + }; + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn comment_directives_as_iris_finds_them() { + // Iris's own cases (`CommentDirectiveParser.Tests`). + let find = |text: &str, name: &str| comment_directive(text, name).map(|d| d.1); + let draw = |text: &str| find(text, "DRAWBUFFERS"); + assert_eq!( + draw("Some normal text that doesn't contain a DRAWBUFFERS directive"), + None + ); + assert_eq!( + draw("doesn't contain a /* DRAWBUFFERS: directive of any sort"), + None + ); + assert_eq!( + draw("/*DRAWBUFFERS:321*/ OptiFine will detect this").as_deref(), + Some("321") + ); + assert_eq!( + draw("a directive: /* DRAWBUFFERS:321 */").as_deref(), + Some("321") + ); + assert_eq!( + draw("This is a line /* DRAWBUFFERS:31 */ containing").as_deref(), + Some("31") + ); + assert_eq!( + draw("/* DRAWBUFFERS: */ invalid but matching").as_deref(), + Some("") + ); + assert_eq!(draw(" /*DRAWBUFFERS:12*/").as_deref(), Some("12")); + let lines = "/* RENDERTARGETS:Duplicate handling? */\nuniform sampler2D test;\n\ + /* RENDERTARGETS:Within a line? */ Let's see /* RENDERTARGETS:It works */\n"; + assert_eq!(find(lines, "RENDERTARGETS").as_deref(), Some("It works")); + + // The later directive wins, whichever kind it is. + assert_eq!( + draw_buffers("/* RENDERTARGETS: 4,10 */ /* DRAWBUFFERS:02 */"), + Ok(Some(vec![0, 2])) + ); + assert_eq!( + draw_buffers("/* DRAWBUFFERS:02 */ /* RENDERTARGETS: 4,10 */"), + Ok(Some(vec![4, 10])) + ); + assert_eq!(draw_buffers("void main() {}"), Ok(None)); + assert!(draw_buffers("/* RENDERTARGETS: 40 */").is_err()); + assert!(draw_buffers("/* DRAWBUFFERS:0a */").is_err()); + } + + #[test] + fn const_directives_and_target_settings() { + let text = "\ +const int colortex4Format = rgba16f; + const bool gaux2Clear = false; // kept between frames +const vec4 colortex0ClearColor = vec4(0.5, 0.25, 0.0, 1.0); +const int colortex5Format = NOT_A_FORMAT; +uniform const int x = 1; +const int colortex6Format=RGBA8; +const intx colortex7Format = RGBA8; +const ivec3 workGroups = ivec3(16, 8, 16); +"; + let consts = const_directives(text); + assert_eq!( + consts + .iter() + .map(|c| (c.ty, c.key.as_str(), c.value.as_str())) + .collect::>(), + [ + ("int", "colortex4Format", "rgba16f"), + ("bool", "gaux2Clear", "false"), + ("vec4", "colortex0ClearColor", "vec4(0.5, 0.25, 0.0, 1.0)"), + ("int", "colortex5Format", "NOT_A_FORMAT"), + ("int", "colortex6Format", "RGBA8"), + ("ivec3", "workGroups", "ivec3(16, 8, 16)"), + ], + "a type needs a space after it, the name doesn't" + ); + let targets = target_settings([text], &ShaderProperties::default()); + assert_eq!(targets.len(), MAX_COLOR_BUFFERS as usize); + assert_eq!(targets[4].format, "RGBA16F"); + assert_eq!(targets[5].format, "RGBA", "unknown formats are ignored"); + assert!(!targets[5].clear, "gaux2 is colortex5"); + assert_eq!(targets[0].clear_color, Some([0.5, 0.25, 0.0, 1.0])); + assert_eq!(targets[1], TargetSettings::default()); + } +} diff --git a/crates/horizon-shaderpack/src/preprocess.rs b/crates/horizon-shaderpack/src/preprocess.rs index f5b8478a6..18c890496 100644 --- a/crates/horizon-shaderpack/src/preprocess.rs +++ b/crates/horizon-shaderpack/src/preprocess.rs @@ -204,11 +204,92 @@ pub struct Preprocessed { pub version: Vec, /// `#extension` lines' words (`["GL_ARB_gpu_shader5", ":", "enable"]`). pub extensions: Vec>, + /// The comments Iris reads directives from (`/* DRAWBUFFERS:01 */`, + /// `const int colortex0Format = RGBA16F;`) that are outside `#if`'d out + /// blocks, with their output line. Iris keeps comments through its + /// preprocessor; pp-rs drops them. + pub comments: Vec<(u32, String)>, +} + +impl Preprocessed { + /// The text with its kept comments put back at the start of their + /// lines: what Iris's directive parsers read. + pub fn with_comments(&self) -> String { + let mut comments = self.comments.iter().peekable(); + let mut out = String::new(); + for (i, line) in self.text.lines().enumerate() { + while let Some((_, comment)) = comments.next_if(|(at, _)| *at as usize <= i + 1) { + out.push_str(comment); + out.push(' '); + } + out.push_str(line); + out.push('\n'); + } + out + } +} + +/// What a comment must hold for Iris to read a directive from it. +const DIRECTIVE_WORDS: &[&str] = &["DRAWBUFFERS:", "RENDERTARGETS:", "const"]; +/// The identifier a kept comment is replaced by while pp-rs runs. +const COMMENT_MARK: &str = "iris_comment_"; + +/// Replace the comments holding [`DIRECTIVE_WORDS`] with an identifier +/// (and their line breaks), so the ones in `#if`'d out blocks disappear with +/// them. Comments on directive lines are left alone: there the identifier +/// would end up in a macro or an `#if`. +fn mark_comments(text: &str) -> (String, Vec) { + let mut out = String::with_capacity(text.len()); + let mut comments = Vec::new(); + let (mut directive, mut line_start, mut continued) = (false, true, false); + let mut rest = text; + while let Some(c) = rest.chars().next() { + let comment_len = if rest.starts_with("//") { + rest.find('\n').unwrap_or(rest.len()) + } else if let Some(body) = rest.strip_prefix("/*") { + body.find("*/").map_or(rest.len(), |i| i + 4) + } else { + 0 + }; + if comment_len > 0 { + let comment = &rest[..comment_len]; + if !directive && DIRECTIVE_WORDS.iter().any(|w| comment.contains(w)) { + out.push_str(&format!(" {COMMENT_MARK}{} ", comments.len())); + out.extend(std::iter::repeat_n('\n', comment.matches('\n').count())); + comments.push(comment.to_owned()); + } else { + out.push_str(comment); + } + if comment.contains('\n') { + // A comment spanning lines ends a directive like a line break. + directive = false; + } + line_start = false; + rest = &rest[comment_len..]; + continue; + } + match c { + '\n' => { + directive = directive && continued; + line_start = !directive; + } + '#' if line_start => directive = true, + c if c.is_whitespace() => {} + _ => line_start = false, + } + if c != '\r' && !c.is_whitespace() || c == '\n' { + continued = c == '\\'; + } + out.push(c); + rest = &rest[c.len_utf8()..]; + } + (out, comments) } /// Expand `source` with `env`'s macros. Errors name the file and line. pub fn preprocess(source: &Inlined, env: &Environment) -> Result { - let text = &source.text; + let (marked, comments) = mark_comments(&source.text); + let text = &marked; let line_starts: Vec = std::iter::once(0) .chain(text.match_indices('\n').map(|(i, _)| i + 1)) .collect(); @@ -222,6 +303,7 @@ pub fn preprocess(source: &Inlined, env: &Environment) -> Result Result continue, + TokenValue::Ident(ref name) if name.starts_with(COMMENT_MARK) => { + if let Some(comment) = name[COMMENT_MARK.len()..] + .parse::() + .ok() + .and_then(|i| comments.get(i)) + { + out.comments.push((at, comment.clone())); + } + continue; + } value => spelling(value), }; if at > line { diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite.fsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite.fsh new file mode 100644 index 000000000..c6304265e --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite.fsh @@ -0,0 +1,17 @@ +#version 120 +uniform sampler2D colortex3; +varying vec2 texcoord; +#ifdef BLOOM +/* +const int colortex1Format = R8; +*/ +#endif +const bool colortex3MipmapEnabled = true; // read with mipmaps +void main() { +#if 0 + /* DRAWBUFFERS:12 */ +#endif + /* RENDERTARGETS: 0,1 */ + gl_FragData[0] = texture2D(colortex3, texcoord); + gl_FragData[1] = vec4(1.0); +} diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite.vsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite.vsh new file mode 100644 index 000000000..eb261aadd --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite.vsh @@ -0,0 +1,3 @@ +#version 120 +varying vec2 texcoord; +void main() { gl_Position = ftransform(); texcoord = gl_MultiTexCoord0.xy; } diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite1.fsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite1.fsh new file mode 100644 index 000000000..783c79b52 --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite1.fsh @@ -0,0 +1,7 @@ +#version 120 +uniform sampler2D colortex0; +varying vec2 texcoord; +void main() { + /* DRAWBUFFERS:0 */ + gl_FragData[0] = texture2D(colortex0, texcoord); +} diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite1.vsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite1.vsh new file mode 100644 index 000000000..eb261aadd --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite1.vsh @@ -0,0 +1,3 @@ +#version 120 +varying vec2 texcoord; +void main() { gl_Position = ftransform(); texcoord = gl_MultiTexCoord0.xy; } diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite2.fsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite2.fsh new file mode 100644 index 000000000..d105112b9 --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite2.fsh @@ -0,0 +1,5 @@ +#version 120 +void main() { + /* DRAWBUFFERS:7 */ + gl_FragData[0] = vec4(1.0); +} diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite2.vsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite2.vsh new file mode 100644 index 000000000..eb261aadd --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/composite2.vsh @@ -0,0 +1,3 @@ +#version 120 +varying vec2 texcoord; +void main() { gl_Position = ftransform(); texcoord = gl_MultiTexCoord0.xy; } diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/deferred.fsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/deferred.fsh new file mode 100644 index 000000000..6a24357c6 --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/deferred.fsh @@ -0,0 +1,11 @@ +#version 120 +varying vec2 texcoord; +/* +const int colortex1Format = RGBA16F; +const bool colortex2Clear = false; +const vec4 colortex3ClearColor = vec4(1.0, 0.0, 0.0, 1.0); +*/ +void main() { + /* DRAWBUFFERS:3 */ + gl_FragData[0] = vec4(texcoord, 0.0, 1.0); +} diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/deferred.vsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/deferred.vsh new file mode 100644 index 000000000..eb261aadd --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/deferred.vsh @@ -0,0 +1,3 @@ +#version 120 +varying vec2 texcoord; +void main() { gl_Position = ftransform(); texcoord = gl_MultiTexCoord0.xy; } diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/final.fsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/final.fsh new file mode 100644 index 000000000..65c6b0195 --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/final.fsh @@ -0,0 +1,6 @@ +#version 120 +uniform sampler2D colortex0; +varying vec2 texcoord; +void main() { + gl_FragColor = texture2D(colortex0, texcoord); +} diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/final.vsh b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/final.vsh new file mode 100644 index 000000000..eb261aadd --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/final.vsh @@ -0,0 +1,3 @@ +#version 120 +varying vec2 texcoord; +void main() { gl_Position = ftransform(); texcoord = gl_MultiTexCoord0.xy; } diff --git a/crates/horizon-shaderpack/tests/fixtures/passes/shaders/shaders.properties b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/shaders.properties new file mode 100644 index 000000000..0a9996c4c --- /dev/null +++ b/crates/horizon-shaderpack/tests/fixtures/passes/shaders/shaders.properties @@ -0,0 +1,6 @@ +# Full-screen passes: a smaller composite1 that also flips colortex2, +# composite2 off, colortex3 at half size. +scale.composite1 = 0.5 +flip.composite1.colortex2 = true +program.composite2.enabled = false +size.buffer.colortex3 = 0.5 0.5 diff --git a/crates/horizon-shaderpack/tests/pack.rs b/crates/horizon-shaderpack/tests/pack.rs index 4a7d4959e..ec193847a 100644 --- a/crates/horizon-shaderpack/tests/pack.rs +++ b/crates/horizon-shaderpack/tests/pack.rs @@ -3,7 +3,11 @@ //! `HORIZON_TEST_SHADERPACKS` names a folder of them (development only: //! third-party packs are never committed). -use horizon_shaderpack::{JavaPack, Stage, preprocess::Environment}; +use horizon_shaderpack::{ + JavaPack, Stage, + pipeline::{TargetSettings, TargetSize}, + preprocess::Environment, +}; use std::{ collections::BTreeMap, io::Write, @@ -187,6 +191,54 @@ fn opens_zips_with_shaders_in_a_subfolder() { std::fs::remove_dir_all(dir).unwrap(); } +#[test] +fn plans_the_full_screen_passes() { + let pack = JavaPack::open(&Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/passes")) + .unwrap(); + let values = pack.values(&BTreeMap::new()); + let properties = pack.properties(&values, &env()).unwrap(); + let passes = pack.passes("", &values, &env(), &properties).unwrap(); + + // Settings from the active comments only: colortex1's R8 is #ifdef'd out. + assert_eq!(passes.targets[1].format, "RGBA16F"); + assert!(!passes.targets[2].clear); + assert_eq!(passes.targets[3].clear_color, Some([1.0, 0.0, 0.0, 1.0])); + assert_eq!(passes.targets[3].size, Some(TargetSize::Relative(0.5, 0.5))); + assert_eq!(passes.targets[4], TargetSettings::default()); + + let summary = |p: &horizon_shaderpack::pipeline::Pass| { + ( + p.program.clone(), + p.draw_buffers.clone(), + p.reads_alt.iter().copied().collect::>(), + ) + }; + assert_eq!( + passes.deferred.iter().map(summary).collect::>(), + [("deferred".into(), Some(vec![3]), vec![])] + ); + // composite2 is off; RENDERTARGETS in `#if 0` doesn't count; each pass + // reads the side its predecessors wrote, and composite1 flips colortex2. + assert_eq!( + passes.composite.iter().map(summary).collect::>(), + [ + ("composite".into(), Some(vec![0, 1]), vec![3]), + ("composite1".into(), Some(vec![0]), vec![0, 1, 3]), + ] + ); + assert_eq!(passes.composite[0].mipmapped, [3].into()); + assert_eq!(passes.composite[1].scale, 0.5); + assert_eq!(passes.flipped_after_prepare, [].into()); + assert_eq!(passes.flipped_after_translucent, [3].into()); + assert_eq!(passes.flipped_at_end, [1, 2, 3].into()); + let final_pass = passes.final_pass.unwrap(); + assert_eq!( + (final_pass.program.as_str(), final_pass.draw_buffers), + ("final", None) + ); + assert_eq!(final_pass.reads_alt, [1, 2, 3].into()); +} + fn scratch(name: &str) -> PathBuf { std::env::temp_dir().join(format!("horizon-shaderpack-{name}-{}", std::process::id())) } @@ -276,3 +328,45 @@ fn real_packs_properties() { assert!(unknown.len() <= 2, "{unknown:?}"); } } + +/// The overworld's full-screen passes for the packs in +/// `HORIZON_TEST_SHADERPACKS`. Skipped when the variable isn't set. +#[test] +fn real_packs_passes() { + let Some(dir) = std::env::var_os("HORIZON_TEST_SHADERPACKS") else { + return; + }; + 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 properties = pack.properties(&values, &env()).unwrap(); + let folder = if pack.programs.iter().any(|p| p.starts_with("world0/")) { + "world0" + } else { + "" + }; + let passes = pack.passes(folder, &values, &env(), &properties).unwrap(); + println!("{}:", entry.file_name().to_string_lossy()); + for (i, t) in passes.targets.iter().enumerate() { + if *t != TargetSettings::default() { + println!(" colortex{i}: {t:?}"); + } + } + for p in passes + .begin + .iter() + .chain(&passes.prepare) + .chain(&passes.deferred) + .chain(&passes.composite) + .chain(&passes.final_pass) + { + println!( + " {} writes {:?} reads alt {:?} mipmaps {:?} scale {}", + p.program, p.draw_buffers, p.reads_alt, p.mipmapped, p.scale + ); + } + assert!(passes.final_pass.is_some() || !passes.composite.is_empty()); + } +} diff --git a/docs/shader-packs.md b/docs/shader-packs.md index 76e2e2771..bd9646df8 100644 --- a/docs/shader-packs.md +++ b/docs/shader-packs.md @@ -19,6 +19,7 @@ Both are off by default. With no pack selected the renderer draws exactly what i | 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: `shaders.properties` (`horizon_shaderpack::properties`: screens, sliders, profiles, `program.*.enabled`, image formats, blend modes) | done, no screen yet: read like Iris for the three test packs | +| Java shader packs: full-screen pass plan (`horizon_shaderpack::pipeline`: `DRAWBUFFERS`/`RENDERTARGETS`, `colortexN` formats and clearing, buffer flips) | done, not drawn yet: planned for the three test packs | | Java shader packs: Iris pipeline emulation | planned | ## How the renderer draws a frame today