From 2862f17e86b28dac9bbb68b9917a1a3772ecb5bf Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 16 Oct 2026 00:22:39 +0200 Subject: [PATCH] horizon-client: run a Java pack's full-screen passes over the world A folder holding shaders/, or a .zip, in shaderpacks/ now loads from the Shader Packs screen: every planned begin, prepare, deferred, composite and final pass is translated and drawn each frame over Horizon's world, with Iris's formats, clears and buffer flips. The targets keep OpenGL's layout (the translator flips gl_Position and remaps its depth), the world enters as OpenGL depth, and the pack gets OpenGL matrices. Textures share four samplers by kind (Metal allows 16 per stage), and isnan/isinf test bits (Metal's fast math folds x != x). Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + crates/horizon-client/Cargo.toml | 2 + .../horizon-client/src/game/shader_packs.rs | 11 +- crates/horizon-client/src/gfx.rs | 63 +- crates/horizon-client/src/gfx/java_stages.rs | 1584 +++++++++++++++++ crates/horizon-client/src/java_pack.rs | 165 ++ crates/horizon-client/src/lib.rs | 1 + crates/horizon-client/src/shader_pack.rs | 63 +- crates/horizon-shaderpack/src/pipeline.rs | 92 +- crates/horizon-shaderpack/src/translate.rs | 84 +- crates/horizon-shaderpack/tests/pack.rs | 26 + crates/horizon-shaderpack/tests/translate.rs | 51 + docs/shader-packs.md | 41 +- 13 files changed, 2136 insertions(+), 48 deletions(-) create mode 100644 crates/horizon-client/src/gfx/java_stages.rs create mode 100644 crates/horizon-client/src/java_pack.rs diff --git a/Cargo.lock b/Cargo.lock index 8d5cd15fd..68d0b250d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1417,6 +1417,7 @@ dependencies = [ "horizon-friends", "horizon-mods", "horizon-protocol", + "horizon-shaderpack", "horizon-telemetry", "horizon-theme", "horizon-world", diff --git a/crates/horizon-client/Cargo.toml b/crates/horizon-client/Cargo.toml index b9aeb7aea..19b4c81db 100644 --- a/crates/horizon-client/Cargo.toml +++ b/crates/horizon-client/Cargo.toml @@ -27,6 +27,8 @@ serde_json = { version = "1.0.151", features = ["raw_value"] } # Shader packs: pack.toml, and WGSL checked before it reaches wgpu. toml = "1.1" naga = { version = "30.0.1", features = ["wgsl-in"] } +# Java (OptiFine/Iris) shader packs, translated to WGSL. +horizon-shaderpack = { path = "../horizon-shaderpack" } cpal = "0.16" symphonia = { version = "0.5", default-features = false, features = ["ogg", "vorbis"] } miniz_oxide = "0.8" diff --git a/crates/horizon-client/src/game/shader_packs.rs b/crates/horizon-client/src/game/shader_packs.rs index ddccdcee6..da4e8f594 100644 --- a/crates/horizon-client/src/game/shader_packs.rs +++ b/crates/horizon-client/src/game/shader_packs.rs @@ -48,7 +48,16 @@ impl Game<'_> { None } else { state.stamp = Stamp::of(&dir.join(&id)); - match ShaderPack::load(&dir.join(&id), &shader_pack::load_values(&dir, &id)) { + // Java packs are told which GPU they run on. + let (vendor, renderer) = self.renderer.as_ref().map_or(("Unknown", ""), |r| { + let name = r.info.name.as_str(); + ( + horizon_client::java_pack::vendor_name(r.info.vendor, name), + name, + ) + }); + let values = shader_pack::load_values(&dir, &id); + match ShaderPack::load_any(&dir.join(&id), &values, vendor, renderer) { Ok(pack) => Some(pack), Err(e) => return self.shader_pack_error(&id, &e), } diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 1120830e6..c2e4775b7 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -58,6 +58,7 @@ pub fn outline_vertices(edges: &[[f64; 6]], camera: glam::DVec3) -> Vec<[f32; 6] } mod arena; +mod java_stages; mod occlusion; mod pack_stages; pub mod panorama; @@ -308,7 +309,7 @@ pub struct Renderer { /// Resolution scale and FXAA (Horizon settings). world_post: world_post::Post, /// The selected shader pack's stages (Horizon setting; none by default). - pack: Option, + pack: Option, pub hud: HudRenderer, /// The menus' backdrop blur, made on first use. post: Option, @@ -1679,19 +1680,28 @@ impl Renderer { /// Run `pack`'s stages over the world from now on (`None`: no pack, the /// vanilla frame). On error the previous pack stays. pub fn set_shader_pack(&mut self, pack: Option<&crate::shader_pack::ShaderPack>) -> Result<()> { + let format = self.config.format.remove_srgb_suffix(); self.pack = match pack { - Some(pack) => Some(pack_stages::PackStages::new( - &self.device, - self.config.format.remove_srgb_suffix(), - pack, - )?), + Some(pack) => Some(match &pack.java { + Some(java) => Pack::Java(java_stages::JavaStages::new( + &self.device, + &self.queue, + format, + &pack.id, + java, + )?), + None => Pack::Horizon(pack_stages::PackStages::new(&self.device, format, pack)?), + }), None => None, }; Ok(()) } /// The folder name of the pack `set_shader_pack` gave. pub fn shader_pack_id(&self) -> Option<&str> { - self.pack.as_ref().map(|p| p.id.as_str()) + self.pack.as_ref().map(|p| match p { + Pack::Horizon(p) => p.id.as_str(), + Pack::Java(p) => p.id.as_str(), + }) } /// Horizon's resolution scale (world pixels per window pixel) and FXAA. pub fn set_world_post(&mut self, scale: f32, antialias: bool) { @@ -2380,16 +2390,28 @@ impl Renderer { if let (true, Some((color, depth)), Some(size), Some(stages)) = (pack, &scaled, self.world_post.size(), self.pack.as_mut()) { - let output = stages.run( - &self.device, - &self.queue, - &mut encoder, - color, - depth, - size, - &self.world_post, - pack_uniform, - ); + let output = match stages { + Pack::Horizon(stages) => stages.run( + &self.device, + &self.queue, + &mut encoder, + color, + depth, + size, + &self.world_post, + pack_uniform, + ), + Pack::Java(stages) => stages.run( + &self.device, + &self.queue, + &mut encoder, + color, + depth, + size, + &self.world_post, + pack_uniform, + ), + }; self.world_post .finish_with(&mut encoder, &view_target, output); } else if scaled.is_some() { @@ -2710,6 +2732,13 @@ fn shader_pack_uniform( } } +/// The selected shader pack on the GPU: a Horizon pack's WGSL stages, or a +/// Java pack's translated programs. +enum Pack { + Horizon(pack_stages::PackStages), + Java(java_stages::JavaStages), +} + fn world_projection(fov_degrees: f32, aspect: f32, far: f32) -> Mat4 { Mat4::perspective_rh(fov_degrees.to_radians(), aspect, far, 0.05) } diff --git a/crates/horizon-client/src/gfx/java_stages.rs b/crates/horizon-client/src/gfx/java_stages.rs new file mode 100644 index 000000000..d3e393e07 --- /dev/null +++ b/crates/horizon-client/src/gfx/java_stages.rs @@ -0,0 +1,1584 @@ +//! A Java (OptiFine/Iris) shader pack on the GPU (docs/shader-packs.md): +//! Iris's colour targets (`colortex0..`, each with a main and an alternate +//! side) and the pack's `begin`, `prepare`, `deferred`, `composite` and +//! `final` programs as full-screen passes over them, as Iris's +//! `CompositeRenderer` runs them. Horizon's world enters `colortex0` and +//! `depthtex0..2` after `prepare`, where Iris's gbuffers programs would have +//! drawn, and the last picture goes to `world_post`. The targets keep GL's +//! layout (row 0 at the bottom, which the translated vertex programs +//! produce), so the world is flipped going in and the picture coming out. +use super::pack_stages::FrameUniform; +use crate::java_pack::{JavaFolder, JavaPasses}; +use glam::{Mat3, Mat4, Vec3, Vec4}; +use horizon_shaderpack::pipeline::TargetSettings; +use horizon_shaderpack::pipeline::{Program, target_index}; +use std::collections::{BTreeMap, BTreeSet}; + +/// Iris's `noisetex` size (`noiseTextureResolution`'s default). +const NOISE_SIZE: u32 = 256; +/// The vertex buffers a pass may read (wgpu's default limit). +const MAX_ATTRIBUTES: usize = 8; + +const BLIT: &str = r#" +@group(0) @binding(0) var color: texture_2d; +@group(0) @binding(1) var depth: texture_depth_2d; + +@vertex +fn vs(@builtin(vertex_index) i: u32) -> @builtin(position) vec4f { + let uv = vec2f(f32((i << 1u) & 2u), f32(i & 2u)); + return vec4f(uv * 2.0 - 1.0, 0.0, 1.0); +} + +struct World { + @location(0) color: vec4f, + @location(1) depth: vec4f, +} + +// The world's colour and depth, upside down into GL's layout; Horizon's +// reversed depth (near 1) as GL's (near 0). +@fragment +fn world_in(@builtin(position) p: vec4f) -> World { + let size = textureDimensions(depth); + let xy = vec2u(u32(p.x), size.y - 1u - u32(p.y)); + return World(textureLoad(color, xy, 0), vec4f(1.0 - textureLoad(depth, xy, 0))); +} + +// A target, upside down back into wgpu's layout. +@fragment +fn present(@builtin(position) p: vec4f) -> @location(0) vec4f { + let size = textureDimensions(color); + return textureLoad(color, vec2u(u32(p.x), size.y - 1u - u32(p.y)), 0); +} +"#; + +/// A wgpu format for each of Iris's (`InternalTextureFormat`), renderable +/// everywhere: 8-bit unsigned normalized formats stay, signed normalized, +/// 16-bit and packed float ones become half floats, integer ones 32-bit. +fn target_format(iris: &str) -> wgpu::TextureFormat { + use wgpu::TextureFormat as F; + let channels = if iris.starts_with("RGB") || iris.contains("_B") { + 4 + } else if iris.starts_with("RG") { + 2 + } else { + 1 + }; + let pick = |one, two, four| match channels { + 1 => one, + 2 => two, + _ => four, + }; + match iris { + "R8" | "RG8" | "RGB8" | "RGBA8" | "RGBA" | "RGBA2" | "RGBA4" | "R3_G3_B2" | "RGB5_A1" + | "RGB565" => pick(F::R8Unorm, F::Rg8Unorm, F::Rgba8Unorm), + "RGB10_A2" => F::Rgb10a2Unorm, + "RGB10_A2UI" => F::Rgb10a2Uint, + f if f.ends_with("32F") => pick(F::R32Float, F::Rg32Float, F::Rgba32Float), + f if f.ends_with("UI") => pick(F::R32Uint, F::Rg32Uint, F::Rgba32Uint), + f if f.ends_with('I') => pick(F::R32Sint, F::Rg32Sint, F::Rgba32Sint), + _ => pick(F::R16Float, F::Rg16Float, F::Rgba16Float), + } +} + +/// How a pass reads a texture of `format`. +fn sample_type(format: wgpu::TextureFormat) -> wgpu::TextureSampleType { + match format.sample_type(None, None) { + Some(wgpu::TextureSampleType::Float { .. }) => wgpu::TextureSampleType::Float { + filterable: !matches!( + format, + wgpu::TextureFormat::R32Float + | wgpu::TextureFormat::Rg32Float + | wgpu::TextureFormat::Rgba32Float + ), + }, + Some(t) => t, + None => wgpu::TextureSampleType::Float { filterable: false }, + } +} + +/// A colour target's two sides. +struct Target { + format: wgpu::TextureFormat, + main: wgpu::Texture, + alt: wgpu::Texture, + main_view: wgpu::TextureView, + alt_view: wgpu::TextureView, + clear: Option, +} + +/// What a sampler of the pack reads. +#[derive(Clone, Copy, Debug, PartialEq)] +enum Source { + Target(u32), + /// `depthtex0..2`: the world's depth. + Depth, + Noise, + /// `shadowtex*`: no shadow map yet, so a far depth (nothing in shadow); + /// also Distant Horizons' and Voxy's depth, as without those mods. + Shadow, + /// Anything else (`shadowcolor*`, custom textures): white or black. + Blank(bool), +} + +fn source(name: &str) -> Source { + match name { + "depthtex0" | "depthtex1" | "depthtex2" | "gdepthtex" => Source::Depth, + "noisetex" => Source::Noise, + "shadowtex0" | "shadowtex1" | "shadow" | "watershadow" => Source::Shadow, + n if n.starts_with("dhDepthTex") || n.starts_with("vxDepthTex") => Source::Shadow, + n if n.starts_with("shadowcolor") => Source::Blank(true), + n => target_index(n).map_or(Source::Blank(false), Source::Target), + } +} + +/// A sampler of a pass: its texture and sampler bindings. +struct SamplerBinding { + source: Source, + dimension: wgpu::TextureViewDimension, + /// `isampler` (true) or `usampler` (false). + signed: Option, + texture: u32, + sampler: u32, + /// The shared sampler it uses (`iris_linear`), if any. + shared: Option, + compare: bool, +} + +/// A scalar slot of the uniform block: byte offset, and whether it holds +/// an integer (`int`, `uint` or `bool`). +type Slot = (u32, bool); + +struct PassGpu { + program: String, + pipeline: wgpu::RenderPipeline, + layout: wgpu::BindGroupLayout, + samplers: Vec, + /// Uniform members by name, their scalar slots in order. + uniforms: Vec<(String, Vec)>, + uniform: Option<(u32, wgpu::Buffer)>, + /// Each vertex buffer slot's data: 0 position, 1 white, 2 up, 3 zero. + attributes: Vec, + draw_buffers: Vec, + reads_alt: BTreeSet, + scale: f32, +} + +/// What every pass of the pack shares. +struct Common { + linear: wgpu::Sampler, + nearest: wgpu::Sampler, + repeat: wgpu::Sampler, + compare: wgpu::Sampler, + noise: wgpu::TextureView, + /// Full-screen triangle data per attribute kind (see `PassGpu::attributes`). + vertices: [wgpu::Buffer; 4], + world_layout: wgpu::BindGroupLayout, + present_layout: wgpu::BindGroupLayout, + blit: wgpu::ShaderModule, +} + +/// The textures of one folder at one size. +struct Bound { + folder: usize, + size: [u32; 2], + targets: BTreeMap, + depth: wgpu::TextureView, + /// Per pass, then `final`. + groups: Vec, + world_in: (wgpu::RenderPipeline, wgpu::BindGroup), + /// The world goes to colortex0's alternate side. + world_alt: bool, + final_target: Option, + present: (wgpu::RenderPipeline, wgpu::BindGroup), + output_view: wgpu::TextureView, + output: wgpu::BindGroup, + /// Blank textures, kept alive by the bind groups too. + _blank: Vec, +} + +struct Folder { + targets: Vec, + world_at: usize, + flipped_after_prepare: BTreeSet, + flipped_at_end: BTreeSet, + passes: Vec, + final_pass: Option, +} + +pub struct JavaStages { + pub id: String, + format: wgpu::TextureFormat, + common: Common, + folders: Vec, + folder_for: [Option; 3], + bound: Option, + started: std::time::Instant, + last: Option, + frames: u32, + previous: Option<(Mat4, Mat4, Vec3)>, +} + +impl JavaStages { + /// The pipelines of a loaded Java pack, or wgpu's error. + pub fn new( + device: &wgpu::Device, + queue: &wgpu::Queue, + format: wgpu::TextureFormat, + id: &str, + pack: &JavaPasses, + ) -> Result { + let common = Common::new(device, queue); + let folder_for = [-1, 0, 1].map(|d| pack.folder_for(d)); + let scope = device.push_error_scope(wgpu::ErrorFilter::Validation); + let folders = pack + .folders + .iter() + .map(|plan: &JavaFolder| { + let formats: Vec = plan + .targets + .iter() + .map(|t| target_format(&t.format)) + .collect(); + let passes = plan + .passes + .iter() + .map(|p| PassGpu::new(device, p, &formats, None)) + .collect::, _>>()?; + let final_pass = plan + .final_pass + .as_ref() + .map(|p| PassGpu::new(device, p, &formats, Some(format))) + .transpose()?; + Ok::<_, String>(Folder { + targets: plan.targets.clone(), + world_at: plan.world_at, + flipped_after_prepare: plan.flipped_after_prepare.clone(), + flipped_at_end: plan.flipped_at_end.clone(), + passes, + final_pass, + }) + }) + .collect::, _>>()?; + if let Some(error) = pollster::block_on(scope.pop()) { + return Err(error.to_string()); + } + Ok(Self { + id: id.to_owned(), + format, + common, + folders, + folder_for, + bound: None, + started: std::time::Instant::now(), + last: None, + frames: 0, + previous: None, + }) + } + + /// Run the passes of the world's dimension over its `color` and `depth` + /// (`size`) and return `post`'s bindings of the picture. + #[allow(clippy::too_many_arguments)] + pub fn run( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + color: &wgpu::TextureView, + depth: &wgpu::TextureView, + size: [u32; 2], + post: &super::world_post::Post, + frame: FrameUniform, + ) -> &wgpu::BindGroup { + let dimension = (frame.dimension[0] as i32).clamp(-1, 1); + let folder = self.folder_for[(dimension + 1) as usize].unwrap_or(0); + if self + .bound + .as_ref() + .is_none_or(|b| b.folder != folder || b.size != size) + { + self.bound = Some(self.bind(device, queue, folder, color, depth, size, post)); + } + let now = std::time::Instant::now(); + let frame_time = self.last.map_or(0.0, |t| (now - t).as_secs_f32()); + self.last = Some(now); + let inputs = Inputs::new( + &frame, + size, + self.started.elapsed().as_secs_f32(), + frame_time, + self.frames, + self.previous, + ); + self.previous = Some((inputs.view, inputs.projection, inputs.camera)); + self.frames = self.frames.wrapping_add(1); + let bound = self.bound.as_ref().expect("bound above"); + let folder = &self.folders[bound.folder]; + + // Iris clears both sides of the targets that ask for it. + for target in bound.targets.values() { + if let Some(clear) = target.clear { + for view in [&target.main_view, &target.alt_view] { + clear_view(encoder, view, clear); + } + } + } + let world_at = folder.world_at; + for (i, (pass, group)) in folder.passes.iter().zip(&bound.groups).enumerate() { + if i == world_at { + world_in(encoder, bound); + } + pass.draw(queue, encoder, bound, group, &self.common, &inputs); + } + if folder.passes.len() <= world_at { + world_in(encoder, bound); + } + if let (Some(pass), Some(view)) = (&folder.final_pass, &bound.final_target) { + let group = bound.groups.last().expect("final's group"); + pass.draw_to( + queue, + encoder, + &[Some(view)], + size, + group, + &self.common, + &inputs, + ); + } + { + let attachments = [Some(color_attachment(&bound.output_view, None))]; + let mut render = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Java shader pack picture"), + color_attachments: &attachments, + ..Default::default() + }); + render.set_pipeline(&bound.present.0); + render.set_bind_group(0, &bound.present.1, &[]); + render.draw(0..3, 0..1); + } + // The next frame starts unflipped: the last writes go back to main. + for index in &folder.flipped_at_end { + if let Some(t) = bound.targets.get(index) { + encoder.copy_texture_to_texture( + t.alt.as_image_copy(), + t.main.as_image_copy(), + t.main.size(), + ); + } + } + &bound.output + } + + #[allow(clippy::too_many_arguments)] + fn bind( + &self, + device: &wgpu::Device, + queue: &wgpu::Queue, + folder_index: usize, + color: &wgpu::TextureView, + world_depth: &wgpu::TextureView, + size: [u32; 2], + post: &super::world_post::Post, + ) -> Bound { + let folder = &self.folders[folder_index]; + let plan = folder; + let all = folder.passes.iter().chain(&folder.final_pass); + // Every target a pass reads or writes, and colortex0 (the world). + let mut used: BTreeSet = [0].into(); + for pass in all.clone() { + used.extend(pass.draw_buffers.iter().copied()); + used.extend(pass.samplers.iter().filter_map(|s| match s.source { + Source::Target(i) => Some(i), + _ => None, + })); + } + let texture = |label, format, usage, size: [u32; 2]| { + device.create_texture(&wgpu::TextureDescriptor { + label: Some(label), + size: wgpu::Extent3d { + width: size[0].max(1), + height: size[1].max(1), + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage, + view_formats: &[], + }) + }; + let target_usage = wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TEXTURE_BINDING + | wgpu::TextureUsages::COPY_SRC + | wgpu::TextureUsages::COPY_DST; + let targets: BTreeMap = used + .iter() + .filter_map(|&i| { + let settings = plan.targets.get(i as usize)?; + let format = target_format(&settings.format); + let target_size = match settings.size { + Some(horizon_shaderpack::pipeline::TargetSize::Relative(w, h)) if i != 0 => [ + (size[0] as f32 * w).round() as u32, + (size[1] as f32 * h).round() as u32, + ], + Some(horizon_shaderpack::pipeline::TargetSize::Pixels(w, h)) if i != 0 => { + [w, h] + } + _ => size, + }; + let main = texture("Java shader pack target", format, target_usage, target_size); + let alt = texture("Java shader pack target", format, target_usage, target_size); + // Iris: colortex0 to the fog colour, colortex1 to white. + let clear = settings.clear.then(|| { + let [r, g, b, a] = settings.clear_color.unwrap_or(match i { + 1 => [1.0; 4], + _ => [0.0; 4], + }); + wgpu::Color { + r: r.into(), + g: g.into(), + b: b.into(), + a: a.into(), + } + }); + Some(( + i, + Target { + format, + main_view: main.create_view(&Default::default()), + alt_view: alt.create_view(&Default::default()), + main, + alt, + clear, + }, + )) + }) + .collect(); + let depth = texture( + "Java shader pack depth", + wgpu::TextureFormat::R32Float, + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, + size, + ) + .create_view(&Default::default()); + let mut blank = Vec::new(); + let groups = all + .clone() + .map(|pass| pass.bind(device, queue, &targets, &depth, &self.common, &mut blank)) + .collect(); + + // The world goes into colortex0's side the gbuffers would write. + let colortex0 = &targets[&0]; + let world_pipeline = blit_pipeline( + device, + &self.common, + &self.common.world_layout, + "world_in", + &[colortex0.format, wgpu::TextureFormat::R32Float], + ); + let world_group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Java shader pack world"), + layout: &self.common.world_layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::TextureView(color), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::TextureView(world_depth), + }, + ], + }); + + let final_target = folder.final_pass.as_ref().map(|_| { + texture( + "Java shader pack final", + self.format, + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, + size, + ) + .create_view(&Default::default()) + }); + // Without `final`, Iris shows colortex0 as the last pass left it. + let shown = final_target.clone().unwrap_or_else(|| { + if plan.flipped_at_end.contains(&0) { + colortex0.alt_view.clone() + } else { + colortex0.main_view.clone() + } + }); + let present_pipeline = blit_pipeline( + device, + &self.common, + &self.common.present_layout, + "present", + &[self.format], + ); + let present_group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Java shader pack picture"), + layout: &self.common.present_layout, + entries: &[wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::TextureView(&shown), + }], + }); + let output_view = texture( + "Java shader pack picture", + self.format, + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, + size, + ) + .create_view(&Default::default()); + let output = post.bind(device, &output_view); + Bound { + folder: folder_index, + size, + targets, + depth, + groups, + world_in: (world_pipeline, world_group), + world_alt: plan.flipped_after_prepare.contains(&0), + final_target, + present: (present_pipeline, present_group), + output_view, + output, + _blank: blank, + } + } +} + +fn world_in(encoder: &mut wgpu::CommandEncoder, bound: &Bound) { + let colortex0 = &bound.targets[&0]; + let side = if bound.world_alt { + &colortex0.alt_view + } else { + &colortex0.main_view + }; + let attachments = [ + Some(color_attachment(side, None)), + Some(color_attachment(&bound.depth, None)), + ]; + let mut render = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Java shader pack world"), + color_attachments: &attachments, + ..Default::default() + }); + render.set_pipeline(&bound.world_in.0); + render.set_bind_group(0, &bound.world_in.1, &[]); + render.draw(0..3, 0..1); +} + +fn color_attachment( + view: &wgpu::TextureView, + clear: Option, +) -> wgpu::RenderPassColorAttachment<'_> { + wgpu::RenderPassColorAttachment { + view, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { + load: clear.map_or(wgpu::LoadOp::Load, wgpu::LoadOp::Clear), + store: wgpu::StoreOp::Store, + }, + } +} + +fn clear_view(encoder: &mut wgpu::CommandEncoder, view: &wgpu::TextureView, color: wgpu::Color) { + let attachments = [Some(color_attachment(view, Some(color)))]; + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Java shader pack clear"), + color_attachments: &attachments, + ..Default::default() + }); +} + +fn blit_pipeline( + device: &wgpu::Device, + common: &Common, + layout: &wgpu::BindGroupLayout, + entry: &str, + formats: &[wgpu::TextureFormat], +) -> wgpu::RenderPipeline { + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("Java shader pack blit"), + bind_group_layouts: &[Some(layout)], + ..Default::default() + }); + let targets: Vec> = + formats.iter().map(|&f| Some(f.into())).collect(); + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some(entry), + layout: Some(&pipeline_layout), + vertex: wgpu::VertexState { + module: &common.blit, + entry_point: Some("vs"), + compilation_options: Default::default(), + buffers: &[], + }, + primitive: Default::default(), + depth_stencil: None, + multisample: Default::default(), + fragment: Some(wgpu::FragmentState { + module: &common.blit, + entry_point: Some(entry), + compilation_options: Default::default(), + targets: &targets, + }), + multiview_mask: None, + cache: None, + }) +} + +impl Common { + fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Self { + let sampler = |filter, address, compare| { + device.create_sampler(&wgpu::SamplerDescriptor { + label: Some("Java shader pack"), + address_mode_u: address, + address_mode_v: address, + address_mode_w: address, + mag_filter: filter, + min_filter: filter, + compare, + ..Default::default() + }) + }; + use wgpu::{AddressMode::*, FilterMode::*}; + // Iris's noise: random RGB, opaque. + let mut seed = 0x2545_f491_u32; + let noise_bytes: Vec = (0..NOISE_SIZE * NOISE_SIZE * 4) + .map(|i| { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + if i % 4 == 3 { 255 } else { (seed >> 24) as u8 } + }) + .collect(); + let noise = device.create_texture(&wgpu::TextureDescriptor { + label: Some("noisetex"), + size: wgpu::Extent3d { + width: NOISE_SIZE, + height: NOISE_SIZE, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }); + queue.write_texture( + noise.as_image_copy(), + &noise_bytes, + wgpu::TexelCopyBufferLayout { + offset: 0, + bytes_per_row: Some(NOISE_SIZE * 4), + rows_per_image: None, + }, + noise.size(), + ); + // The full-screen triangle (0, 0), (2, 0), (0, 2): the pack's + // projection maps 0..1 to the screen. Texture coordinates are the + // position; colours white; normals up. + let vertices = [ + [ + [0.0, 0.0, 0.0, 1.0], + [2.0, 0.0, 0.0, 1.0], + [0.0, 2.0, 0.0, 1.0], + ], + [[1.0f32; 4]; 3], + [[0.0, 0.0, 1.0, 0.0]; 3], + [[0.0; 4]; 3], + ] + .map(|data| { + let buffer = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Java shader pack vertices"), + size: std::mem::size_of_val(&data) as u64, + usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + queue.write_buffer(&buffer, 0, bytemuck::cast_slice(&data)); + buffer + }); + let texture_entry = |binding, sample_type| wgpu::BindGroupLayoutEntry { + binding, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + count: None, + }; + let unfilterable = wgpu::TextureSampleType::Float { filterable: false }; + Self { + linear: sampler(Linear, ClampToEdge, None), + nearest: sampler(Nearest, ClampToEdge, None), + repeat: sampler(Linear, Repeat, None), + compare: sampler(Linear, ClampToEdge, Some(wgpu::CompareFunction::LessEqual)), + noise: noise.create_view(&Default::default()), + vertices, + world_layout: device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("Java shader pack world"), + entries: &[ + texture_entry(0, unfilterable), + texture_entry(1, wgpu::TextureSampleType::Depth), + ], + }), + present_layout: device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("Java shader pack picture"), + entries: &[texture_entry(0, unfilterable)], + }), + blit: device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("Java shader pack blit"), + source: wgpu::ShaderSource::Wgsl(BLIT.into()), + }), + } + } +} + +/// The view dimension and integer kind of a GLSL sampler type. +fn sampler_kind(kind: &str) -> Option<(wgpu::TextureViewDimension, Option, bool)> { + use wgpu::TextureViewDimension as D; + let signed = match kind.as_bytes().first() { + Some(b'i') => Some(true), + Some(b'u') => Some(false), + _ => None, + }; + let rest = kind + .trim_start_matches(['i', 'u']) + .strip_prefix("sampler")?; + let dimension = if rest.starts_with("2DArray") { + D::D2Array + } else if rest.starts_with("2D") || rest.starts_with("2DRect") { + D::D2 + } else if rest.starts_with("3D") { + D::D3 + } else if rest.starts_with("CubeArray") { + D::CubeArray + } else if rest.starts_with("Cube") { + D::Cube + } else if rest.starts_with("1D") { + D::D1 + } else { + return None; + }; + Some((dimension, signed, rest.ends_with("Shadow"))) +} + +impl PassGpu { + fn new( + device: &wgpu::Device, + program: &Program, + formats: &[wgpu::TextureFormat], + screen: Option, + ) -> Result { + let name = program.pass.program.clone(); + let link = &program.link; + let mut samplers = Vec::new(); + let mut seen = BTreeSet::new(); + for (sampler, kind) in program + .vertex + .interface + .samplers + .iter() + .chain(&program.fragment.interface.samplers) + { + if !seen.insert(sampler.clone()) { + continue; + } + let (dimension, signed, compare) = sampler_kind(kind) + .ok_or_else(|| format!("{name}: {kind} {sampler} isn't supported"))?; + let binding = |var: &str| { + link.bindings + .get(var) + .copied() + .ok_or_else(|| format!("{name}: {sampler} has no binding")) + }; + let var = program + .fragment + .interface + .sampler_vars + .get(sampler) + .or_else(|| program.vertex.interface.sampler_vars.get(sampler)) + .cloned() + .unwrap_or_else(|| format!("{sampler}_s")); + samplers.push(SamplerBinding { + source: source(sampler), + dimension, + signed, + texture: binding(&format!("{sampler}_t"))?, + sampler: binding(&var)?, + shared: var.starts_with("iris_").then_some(var), + compare, + }); + } + // The layout: the uniform block, then each sampler's texture and sampler. + let block = [&program.fragment, &program.vertex] + .into_iter() + .find_map(|t| uniform_block(&t.module)); + let mut entries = Vec::new(); + if block.is_some() { + entries.push(wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }, + count: None, + }); + } + let mut sampler_bindings = BTreeSet::new(); + for s in &samplers { + let (sample_type, filtering) = match (s.compare, s.source) { + (true, _) => (wgpu::TextureSampleType::Depth, None), + (false, Source::Target(i)) => { + let format = formats + .get(i as usize) + .copied() + .unwrap_or(wgpu::TextureFormat::Rgba8Unorm); + let t = sample_type(format); + ( + t, + Some(matches!( + t, + wgpu::TextureSampleType::Float { filterable: true } + )), + ) + } + (false, Source::Depth | Source::Shadow) => ( + wgpu::TextureSampleType::Float { filterable: false }, + Some(false), + ), + (false, _) => ( + wgpu::TextureSampleType::Float { filterable: true }, + Some(true), + ), + }; + let (sample_type, filtering) = match s.signed { + Some(true) => (wgpu::TextureSampleType::Sint, Some(false)), + Some(false) => (wgpu::TextureSampleType::Uint, Some(false)), + None => (sample_type, filtering), + }; + // A shared sampler filters as its name says. + let filtering = match s.shared.as_deref() { + Some("iris_compare") => None, + Some("iris_nearest") => Some(false), + Some(_) => Some(true), + None => filtering, + }; + entries.push(wgpu::BindGroupLayoutEntry { + binding: s.texture, + visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type, + view_dimension: s.dimension, + multisampled: false, + }, + count: None, + }); + if !sampler_bindings.insert(s.sampler) { + continue; + } + entries.push(wgpu::BindGroupLayoutEntry { + binding: s.sampler, + visibility: wgpu::ShaderStages::VERTEX_FRAGMENT, + ty: wgpu::BindingType::Sampler(match filtering { + None => wgpu::SamplerBindingType::Comparison, + Some(true) => wgpu::SamplerBindingType::Filtering, + Some(false) => wgpu::SamplerBindingType::NonFiltering, + }), + count: None, + }); + } + let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some(&name), + entries: &entries, + }); + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some(&name), + bind_group_layouts: &[Some(&layout)], + ..Default::default() + }); + + // Vertex inputs, each from its own buffer of the full-screen triangle. + let inputs = vertex_inputs(&program.vertex.module); + if inputs.len() > MAX_ATTRIBUTES { + return Err(format!( + "{name}: more than {MAX_ATTRIBUTES} vertex attributes" + )); + } + let attribute_layouts: Vec<[wgpu::VertexAttribute; 1]> = inputs + .iter() + .map(|(_, location, format)| { + [wgpu::VertexAttribute { + format: *format, + offset: 0, + shader_location: *location, + }] + }) + .collect(); + let buffers: Vec> = attribute_layouts + .iter() + .map(|attributes| { + Some(wgpu::VertexBufferLayout { + array_stride: 16, + step_mode: wgpu::VertexStepMode::Vertex, + attributes, + }) + }) + .collect(); + let attributes = inputs + .iter() + .map(|(name, _, _)| match name.as_str() { + n if n == "iris_Vertex" || n.starts_with("iris_MultiTexCoord") => 0, + "iris_Color" => 1, + "iris_Normal" => 2, + _ => 3, + }) + .collect(); + + // Output location `l` writes target `draw_buffers[l]` (or the screen). + let outputs = &program.fragment.interface.outputs; + let target_states: Vec> = match screen { + Some(format) => vec![Some(wgpu::ColorTargetState { + format, + blend: None, + write_mask: if outputs.contains_key(&0) { + wgpu::ColorWrites::ALL + } else { + wgpu::ColorWrites::empty() + }, + })], + None => program + .draw_buffers + .iter() + .enumerate() + .map(|(l, &i)| { + Some(wgpu::ColorTargetState { + format: formats + .get(i as usize) + .copied() + .unwrap_or(wgpu::TextureFormat::Rgba8Unorm), + blend: None, + write_mask: if outputs.contains_key(&(l as u32)) { + wgpu::ColorWrites::ALL + } else { + wgpu::ColorWrites::empty() + }, + }) + }) + .collect(), + }; + let module = |t: &horizon_shaderpack::translate::Translated, label: &str| { + device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some(label), + source: wgpu::ShaderSource::Wgsl(t.wgsl.as_str().into()), + }) + }; + let vertex_module = module(&program.vertex, &format!("{name}.vsh")); + let fragment_module = module(&program.fragment, &format!("{name}.fsh")); + let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some(&name), + layout: Some(&pipeline_layout), + vertex: wgpu::VertexState { + module: &vertex_module, + entry_point: Some("main"), + compilation_options: Default::default(), + buffers: &buffers, + }, + primitive: Default::default(), + depth_stencil: None, + multisample: Default::default(), + fragment: Some(wgpu::FragmentState { + module: &fragment_module, + entry_point: Some("main"), + compilation_options: Default::default(), + targets: &target_states, + }), + multiview_mask: None, + cache: None, + }); + let uniform = block.as_ref().map(|(size, _)| { + ( + *size, + device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Java shader pack uniforms"), + size: u64::from(*size).max(16), + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }), + ) + }); + Ok(Self { + program: name, + pipeline, + layout, + samplers, + uniforms: block.map(|(_, members)| members).unwrap_or_default(), + uniform, + attributes, + draw_buffers: if screen.is_some() { + Vec::new() + } else { + program.draw_buffers.clone() + }, + reads_alt: program.pass.reads_alt.clone(), + scale: program.pass.scale.clamp(0.0, 1.0), + }) + } + + /// The pass's bind group over `targets`. + fn bind( + &self, + device: &wgpu::Device, + queue: &wgpu::Queue, + targets: &BTreeMap, + depth: &wgpu::TextureView, + common: &Common, + blank: &mut Vec, + ) -> wgpu::BindGroup { + let mut views = Vec::new(); + for s in &self.samplers { + let view = match (s.compare, s.source) { + (true, _) => blank_view(device, queue, blank, s, BlankKind::FarDepth), + (false, Source::Target(i)) if s.dimension == wgpu::TextureViewDimension::D2 => { + match targets.get(&i) { + Some(t) if self.reads_alt.contains(&i) => t.alt_view.clone(), + Some(t) => t.main_view.clone(), + None => blank_view(device, queue, blank, s, BlankKind::Color(false)), + } + } + (false, Source::Depth) if s.dimension == wgpu::TextureViewDimension::D2 => { + depth.clone() + } + (false, Source::Shadow) => blank_view(device, queue, blank, s, BlankKind::Far), + (false, Source::Noise) if s.dimension == wgpu::TextureViewDimension::D2 => { + common.noise.clone() + } + (false, Source::Blank(white)) => { + blank_view(device, queue, blank, s, BlankKind::Color(white)) + } + _ => blank_view(device, queue, blank, s, BlankKind::Color(false)), + }; + let sampler = match (s.compare, s.source) { + _ if s.shared.is_some() => match s.shared.as_deref() { + Some("iris_compare") => &common.compare, + Some("iris_nearest") => &common.nearest, + Some("iris_repeat") => &common.repeat, + _ => &common.linear, + }, + (true, _) => &common.compare, + _ if s.signed.is_some() => &common.nearest, + (false, Source::Noise) => &common.repeat, + (false, Source::Depth | Source::Shadow) => &common.nearest, + (false, Source::Target(i)) => { + match targets.get(&i).map(|t| sample_type(t.format)) { + Some(wgpu::TextureSampleType::Float { filterable: true }) | None => { + &common.linear + } + _ => &common.nearest, + } + } + _ => &common.linear, + }; + views.push((s.texture, view, s.sampler, sampler)); + } + let mut entries = Vec::new(); + if let Some((_, buffer)) = &self.uniform { + entries.push(wgpu::BindGroupEntry { + binding: 0, + resource: buffer.as_entire_binding(), + }); + } + let mut sampler_bindings = BTreeSet::new(); + for (texture, view, sampler_binding, sampler) in &views { + entries.push(wgpu::BindGroupEntry { + binding: *texture, + resource: wgpu::BindingResource::TextureView(view), + }); + if !sampler_bindings.insert(*sampler_binding) { + continue; + } + entries.push(wgpu::BindGroupEntry { + binding: *sampler_binding, + resource: wgpu::BindingResource::Sampler(sampler), + }); + } + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some(&self.program), + layout: &self.layout, + entries: &entries, + }) + } + + /// Draw into the targets of `draw_buffers`, on the side it doesn't read. + fn draw( + &self, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + bound: &Bound, + group: &wgpu::BindGroup, + common: &Common, + inputs: &Inputs, + ) { + let views: Vec> = self + .draw_buffers + .iter() + .map(|i| { + bound.targets.get(i).map(|t| { + if self.reads_alt.contains(i) { + &t.main_view + } else { + &t.alt_view + } + }) + }) + .collect(); + let size = self + .draw_buffers + .first() + .and_then(|i| bound.targets.get(i)) + .map_or(bound.size, |t| { + let s = t.main.size(); + [s.width, s.height] + }); + self.draw_to(queue, encoder, &views, size, group, common, inputs); + } + + #[allow(clippy::too_many_arguments)] + fn draw_to( + &self, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + views: &[Option<&wgpu::TextureView>], + size: [u32; 2], + group: &wgpu::BindGroup, + common: &Common, + inputs: &Inputs, + ) { + if let Some((block_size, buffer)) = &self.uniform { + let mut bytes = vec![0u8; *block_size as usize]; + for (name, slots) in &self.uniforms { + if let Some(values) = inputs.value(name) { + for (&(offset, int), value) in slots.iter().zip(values) { + let at = offset as usize..offset as usize + 4; + bytes[at].copy_from_slice(&if int { + (value as i32).to_ne_bytes() + } else { + value.to_ne_bytes() + }); + } + } + } + queue.write_buffer(buffer, 0, &bytes); + } + let attachments: Vec> = views + .iter() + .map(|v| v.map(|v| color_attachment(v, None))) + .collect(); + let mut render = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some(&self.program), + color_attachments: &attachments, + ..Default::default() + }); + render.set_pipeline(&self.pipeline); + render.set_bind_group(0, group, &[]); + for (slot, &data) in self.attributes.iter().enumerate() { + render.set_vertex_buffer(slot as u32, common.vertices[data].slice(..)); + } + let [w, h] = size.map(|v| (v as f32 * self.scale).max(1.0)); + render.set_viewport(0.0, 0.0, w, h, 0.0, 1.0); + render.draw(0..3, 0..1); + } +} + +enum BlankKind { + Color(bool), + /// An `R32Float` 1.0: the far plane. + Far, + /// A depth texture at the far plane, for comparison samplers. + FarDepth, +} + +/// A one-texel stand-in of the dimension `s` needs. +fn blank_view( + device: &wgpu::Device, + queue: &wgpu::Queue, + keep: &mut Vec, + s: &SamplerBinding, + kind: BlankKind, +) -> wgpu::TextureView { + use wgpu::TextureViewDimension as D; + let (format, bytes): (wgpu::TextureFormat, Vec) = match (kind, s.signed) { + (BlankKind::Color(_), Some(true)) => (wgpu::TextureFormat::R32Sint, vec![0; 4]), + (BlankKind::Color(_), Some(false)) => (wgpu::TextureFormat::R32Uint, vec![0; 4]), + (BlankKind::Color(white), None) => ( + wgpu::TextureFormat::Rgba8Unorm, + vec![if white { 255 } else { 0 }; 4], + ), + (BlankKind::Far, _) => (wgpu::TextureFormat::R32Float, 1.0f32.to_ne_bytes().to_vec()), + (BlankKind::FarDepth, _) => (wgpu::TextureFormat::Depth32Float, Vec::new()), + }; + let layers = match s.dimension { + D::Cube | D::CubeArray => 6, + _ => 1, + }; + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some("Java shader pack blank"), + size: wgpu::Extent3d { + width: 1, + height: 1, + depth_or_array_layers: layers, + }, + mip_level_count: 1, + sample_count: 1, + dimension: match s.dimension { + D::D1 => wgpu::TextureDimension::D1, + D::D3 => wgpu::TextureDimension::D3, + _ => wgpu::TextureDimension::D2, + }, + format, + usage: wgpu::TextureUsages::TEXTURE_BINDING + | wgpu::TextureUsages::COPY_DST + | wgpu::TextureUsages::RENDER_ATTACHMENT, + view_formats: &[], + }); + if bytes.is_empty() { + // Depth can't be written by a copy: clear it to the far plane. + let view = texture.create_view(&Default::default()); + let mut encoder = device.create_command_encoder(&Default::default()); + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("Java shader pack blank"), + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: &view, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(1.0), + store: wgpu::StoreOp::Store, + }), + stencil_ops: None, + }), + ..Default::default() + }); + queue.submit([encoder.finish()]); + } else { + for layer in 0..layers { + queue.write_texture( + wgpu::TexelCopyTextureInfo { + texture: &texture, + mip_level: 0, + origin: wgpu::Origin3d { + x: 0, + y: 0, + z: layer, + }, + aspect: wgpu::TextureAspect::All, + }, + &bytes, + wgpu::TexelCopyBufferLayout { + offset: 0, + bytes_per_row: Some(bytes.len() as u32), + rows_per_image: None, + }, + wgpu::Extent3d { + width: 1, + height: 1, + depth_or_array_layers: 1, + }, + ); + } + } + let view = texture.create_view(&wgpu::TextureViewDescriptor { + dimension: Some(s.dimension), + ..Default::default() + }); + keep.push(texture); + view +} + +/// The uniform block of a translated program: its size and each member's +/// scalar slots. +fn uniform_block(module: &naga::Module) -> Option<(u32, Vec<(String, Vec)>)> { + let (_, var) = module + .global_variables + .iter() + .find(|(_, v)| v.space == naga::AddressSpace::Uniform)?; + let naga::TypeInner::Struct { members, span } = &module.types[var.ty].inner else { + return None; + }; + let members = members + .iter() + .map(|m| { + let mut slots = Vec::new(); + scalar_slots(module, m.ty, m.offset, &mut slots); + (m.name.clone().unwrap_or_default(), slots) + }) + .collect(); + Some((*span, members)) +} + +/// The scalars of a value of type `ty` at `offset`: vectors in order, +/// matrices column by column, arrays element by element. +fn scalar_slots( + module: &naga::Module, + ty: naga::Handle, + offset: u32, + out: &mut Vec, +) { + let int = |s: naga::Scalar| s.kind != naga::ScalarKind::Float; + match &module.types[ty].inner { + naga::TypeInner::Scalar(s) => out.push((offset, int(*s))), + naga::TypeInner::Vector { size, scalar } => { + out.extend((0..*size as u32).map(|i| (offset + 4 * i, int(*scalar)))) + } + naga::TypeInner::Matrix { columns, rows, .. } => { + let stride = if *rows == naga::VectorSize::Bi { 8 } else { 16 }; + for c in 0..*columns as u32 { + out.extend((0..*rows as u32).map(|r| (offset + c * stride + 4 * r, false))); + } + } + naga::TypeInner::Array { + base, + size: naga::ArraySize::Constant(n), + stride, + } => { + for i in 0..n.get() { + scalar_slots(module, *base, offset + i * stride, out); + } + } + _ => {} + } +} + +/// The vertex entry point's inputs: name, location and a vertex format +/// matching their scalar kind. +fn vertex_inputs(module: &naga::Module) -> Vec<(String, u32, wgpu::VertexFormat)> { + let Some(entry) = module.entry_points.first() else { + return Vec::new(); + }; + let format = |ty: naga::Handle| { + let kind = match &module.types[ty].inner { + naga::TypeInner::Scalar(s) | naga::TypeInner::Vector { scalar: s, .. } => s.kind, + _ => naga::ScalarKind::Float, + }; + match kind { + naga::ScalarKind::Sint => wgpu::VertexFormat::Sint32x4, + naga::ScalarKind::Uint => wgpu::VertexFormat::Uint32x4, + _ => wgpu::VertexFormat::Float32x4, + } + }; + let mut out = Vec::new(); + for arg in &entry.function.arguments { + match (&arg.binding, &module.types[arg.ty].inner) { + (Some(naga::Binding::Location { location, .. }), _) => out.push(( + arg.name.clone().unwrap_or_default(), + *location, + format(arg.ty), + )), + (None, naga::TypeInner::Struct { members, .. }) => { + for m in members { + if let Some(naga::Binding::Location { location, .. }) = &m.binding { + out.push((m.name.clone().unwrap_or_default(), *location, format(m.ty))); + } + } + } + _ => {} + } + } + out +} + +/// The game state Iris's uniforms describe, this frame. +struct Inputs { + view: Mat4, + /// GL's: near at -1, far at 1. + projection: Mat4, + camera: Vec3, + previous: (Mat4, Mat4, Vec3), + size: [f32; 2], + time: f32, + frame_time: f32, + frames: u32, + frame: FrameUniform, +} + +impl Inputs { + fn new( + frame: &FrameUniform, + size: [u32; 2], + time: f32, + frame_time: f32, + frames: u32, + previous: Option<(Mat4, Mat4, Vec3)>, + ) -> Self { + let view = Mat4::from_cols_array_2d(&frame.view); + // Horizon's reversed depth (z/w from 1 near to 0 far) to GL's + // (-1 near, 1 far): z' = w - 2z. + let to_gl = Mat4::from_cols( + Vec4::X, + Vec4::Y, + Vec4::new(0.0, 0.0, -2.0, 0.0), + Vec4::new(0.0, 0.0, 1.0, 1.0), + ); + let projection = to_gl * Mat4::from_cols_array_2d(&frame.projection); + let camera = Vec4::from(frame.camera_position).truncate(); + Self { + view, + projection, + camera, + previous: previous.unwrap_or((view, projection, camera)), + size: size.map(|v| v as f32), + time, + frame_time, + frames, + frame: *frame, + } + } + + /// The value of Iris uniform `name`, as its scalars in order; `None` for + /// one Horizon doesn't provide (it stays zero). + fn value(&self, name: &str) -> Option> { + if let Some(flag) = name.strip_prefix("iris_bool_") { + return self.value(flag); + } + let f = &self.frame; + let mat = |m: Mat4| m.to_cols_array().to_vec(); + let vec3 = |v: Vec3| v.to_array().to_vec(); + let in_view = |v: Vec3| (self.view * v.extend(0.0)).truncate() * 100.0; + let sun = Vec4::from(f.sun).truncate(); + let day = f.time[2].rem_euclid(1.0); + // The full-screen passes' own matrices: 0..1 onto the screen. + let screen = Mat4::orthographic_rh_gl(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); + Some(match name { + "gbufferModelView" => mat(self.view), + "gbufferModelViewInverse" => mat(self.view.inverse()), + "gbufferProjection" => mat(self.projection), + "gbufferProjectionInverse" => mat(self.projection.inverse()), + "gbufferPreviousModelView" => mat(self.previous.0), + "gbufferPreviousProjection" => mat(self.previous.1), + "cameraPosition" => vec3(self.camera), + "previousCameraPosition" => vec3(self.previous.2), + "cameraPositionInt" => vec3(self.camera.floor()), + "cameraPositionFract" => vec3(self.camera - self.camera.floor()), + "eyeAltitude" => vec![self.camera.y], + "viewWidth" => vec![self.size[0]], + "viewHeight" => vec![self.size[1]], + "aspectRatio" => vec![self.size[0] / self.size[1].max(1.0)], + "near" => vec![f.view_params[0]], + "far" => vec![f.view_params[2]], + "frameTimeCounter" => vec![self.time % 3600.0], + "frameTime" => vec![self.frame_time], + "frameCounter" => vec![(self.frames % 720_720) as f32], + "worldTime" => vec![(day * 24000.0).floor()], + "worldDay" => vec![0.0], + "sunAngle" => vec![day], + "shadowAngle" => vec![if day < 0.5 { day } else { day - 0.5 }], + "sunPosition" => vec3(in_view(sun)), + "moonPosition" => vec3(in_view(-sun)), + "shadowLightPosition" => vec3(in_view(if sun.y >= 0.0 { sun } else { -sun })), + "upPosition" => vec3(in_view(Vec3::Y)), + "moonPhase" => vec![f.moon[3]], + "rainStrength" | "wetness" => vec![f.weather[0]], + "thunderStrength" => vec![f.weather[1]], + "fogColor" => f.fog_color[..3].to_vec(), + "skyColor" => f.sky_color[..3].to_vec(), + "isEyeInWater" => vec![f.camera_state[0]], + "nightVision" => vec![f.camera_state[1]], + "darknessFactor" => vec![f.camera_state[2]], + "screenBrightness" => vec![1.0], + // Sky light at full with a sky, no block light: Horizon doesn't + // give the light at the eye yet. + "eyeBrightness" | "eyeBrightnessSmooth" => vec![0.0, 240.0 * f.dimension[1]], + "hasSkylight" => vec![f.dimension[1]], + "iris_ModelViewMatrix" | "iris_ModelViewMatrixInverse" => mat(Mat4::IDENTITY), + "iris_ProjectionMatrix" | "iris_ModelViewProjectionMatrix" => mat(screen), + "iris_ProjectionMatrixInverse" => mat(screen.inverse()), + "iris_TextureMatrix" => (0..8).flat_map(|_| mat(Mat4::IDENTITY)).collect(), + "iris_NormalMatrix" => Mat3::IDENTITY.to_cols_array().to_vec(), + "shadowModelView" + | "shadowModelViewInverse" + | "shadowProjection" + | "shadowProjectionInverse" => mat(Mat4::IDENTITY), + _ => return None, + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn iris_formats_become_renderable_wgpu_formats() { + use wgpu::TextureFormat as F; + for (iris, wgpu) in [ + ("RGBA", F::Rgba8Unorm), + ("RGB8", F::Rgba8Unorm), + ("R8", F::R8Unorm), + ("RG16F", F::Rg16Float), + ("R11F_G11F_B10F", F::Rgba16Float), + ("RGB8_SNORM", F::Rgba16Float), + ("RGBA16", F::Rgba16Float), + ("R32F", F::R32Float), + ("RGBA32F", F::Rgba32Float), + ("RGB10_A2", F::Rgb10a2Unorm), + ("R32UI", F::R32Uint), + ("RGBA16I", F::Rgba32Sint), + ] { + assert_eq!(target_format(iris), wgpu, "{iris}"); + } + assert_eq!( + sample_type(F::R32Float), + wgpu::TextureSampleType::Float { filterable: false } + ); + } + + #[test] + fn samplers_read_what_iris_binds() { + assert_eq!(source("colortex4"), Source::Target(4)); + assert_eq!(source("gaux1"), Source::Target(4)); + assert_eq!(source("gdepthtex"), Source::Depth); + assert_eq!(source("shadowtex1"), Source::Shadow); + assert_eq!(source("dhDepthTex0"), Source::Shadow); + assert_eq!(source("shadowcolor0"), Source::Blank(true)); + assert_eq!(source("cloudTexture"), Source::Blank(false)); + let (d, signed, shadow) = sampler_kind("usampler2D").unwrap(); + assert_eq!( + (d, signed, shadow), + (wgpu::TextureViewDimension::D2, Some(false), false) + ); + assert!(sampler_kind("sampler2DShadow").unwrap().2); + assert_eq!( + sampler_kind("sampler3D").unwrap().0, + wgpu::TextureViewDimension::D3 + ); + } + + #[test] + fn gl_projection_from_reversed_depth() { + // A point at the near plane is GL's -1, at the far plane 1. + let (near, far) = (0.05, 512.0); + let frame = FrameUniform { + view: Mat4::IDENTITY.to_cols_array_2d(), + projection: Mat4::perspective_rh(1.2, 1.5, far, near).to_cols_array_2d(), + ..Default::default() + }; + let inputs = Inputs::new(&frame, [300, 200], 0.0, 0.0, 0, None); + let depth = |d: f32| { + let clip = inputs.projection * Vec4::new(0.0, 0.0, -d, 1.0); + clip.z / clip.w + }; + assert!((depth(near) + 1.0).abs() < 1e-4, "{}", depth(near)); + assert!((depth(far) - 1.0).abs() < 1e-3, "{}", depth(far)); + assert_eq!(inputs.value("viewWidth"), Some(vec![300.0])); + assert_eq!( + inputs.value("iris_bool_hasSkylight"), + inputs.value("hasSkylight") + ); + } +} diff --git a/crates/horizon-client/src/java_pack.rs b/crates/horizon-client/src/java_pack.rs new file mode 100644 index 000000000..f051cf2b6 --- /dev/null +++ b/crates/horizon-client/src/java_pack.rs @@ -0,0 +1,165 @@ +//! Java (OptiFine/Iris) shader packs in `shaderpacks/` (docs/shader-packs.md): +//! a folder holding `shaders/`, or a `.zip`. `horizon-shaderpack` translates +//! their full-screen programs (`begin`, `prepare`, `deferred`, `composite`, +//! `final`) to WGSL, and `gfx/java_stages.rs` runs them over Horizon's world +//! as Iris runs them over its own. Geometry programs (`gbuffers_*`, +//! `shadow`) don't run yet: the world is drawn by Horizon. +use horizon_shaderpack::{ + JavaPack, + pipeline::{Program, TargetSettings}, + preprocess::Environment, +}; +use std::{ + collections::{BTreeMap, BTreeSet}, + path::Path, +}; + +/// The passes of one program folder (`world0`, or `""` for the pack's root). +pub struct JavaFolder { + pub folder: String, + pub targets: Vec, + /// `begin*`, `prepare*`, `deferred*`, `composite*`, in order. + pub passes: Vec, + /// How many of `passes` run before the world (`begin*`, `prepare*`): the + /// world's colour and depth enter `colortex0` and `depthtex*` after them. + pub world_at: usize, + /// The targets whose alternate side the world is written to. + pub flipped_after_prepare: BTreeSet, + pub final_pass: Option, + /// The targets whose alternate side holds the frame's last write. + pub flipped_at_end: BTreeSet, +} + +/// A loaded Java pack. +pub struct JavaPasses { + pub folders: Vec, +} + +impl JavaPasses { + /// The folder Iris uses in `dimension` (0 overworld, -1 the Nether, 1 the + /// End): `world` when the pack has one, else `world0`, else the root. + pub fn folder_for(&self, dimension: i8) -> Option { + let find = |name: &str| self.folders.iter().position(|f| f.folder == name); + find(&format!("world{dimension}")) + .or_else(|| find("world0")) + .or_else(|| find("")) + } +} + +/// A folder holding `shaders/`, or a zip. +pub fn is_java_pack(path: &Path) -> bool { + path.join("shaders").is_dir() + || path.is_file() + && path + .extension() + .is_some_and(|e| e.eq_ignore_ascii_case("zip")) +} + +/// The `MC_GL_VENDOR_*` name of a wgpu adapter (`AdapterInfo::vendor`, a +/// PCI id; Apple's GPUs report 0). +pub fn vendor_name(vendor: u32, adapter: &str) -> &'static str { + match vendor { + 0x10de => "NVIDIA Corporation", + 0x1002 | 0x1022 => "AMD", + 0x8086 => "Intel", + _ if adapter.starts_with("Apple") => "Apple", + _ => "Unknown", + } +} + +/// Open the pack at `path` and translate its full-screen passes with the +/// player's `values`; `vendor` and `renderer` name the GPU to the pack. +pub fn load( + path: &Path, + values: &BTreeMap, + vendor: &str, + renderer: &str, +) -> Result { + let pack = JavaPack::open(path)?; + let values = pack.values(values); + let env = Environment::iris(vendor, renderer, 4); + let properties = pack.properties(&values, &env)?; + let mut names: Vec<&str> = ["world0", "world-1", "world1"] + .into_iter() + .filter(|d| { + pack.programs + .iter() + .any(|p| p.starts_with(&format!("{d}/"))) + }) + .collect(); + if pack.programs.iter().any(|p| !p.contains('/')) { + names.push(""); + } + let mut folders = Vec::new(); + for name in names { + let plan = pack.passes(name, &values, &env, &properties)?; + let compile = |pass| { + let program = pack.compile(pass, &plan.targets, &values, &env, &properties)?; + if let Some((image, _)) = program.fragment.interface.images.first() { + return Err(format!( + "{}: images ({image}) aren't supported yet", + program.pass.program + )); + } + Ok::<_, String>(program) + }; + let early = plan.begin.iter().chain(&plan.prepare); + let passes = early + .clone() + .chain(&plan.deferred) + .chain(&plan.composite) + .map(compile) + .collect::, _>>()?; + let final_pass = plan.final_pass.as_ref().map(compile).transpose()?; + if passes.is_empty() && final_pass.is_none() { + continue; + } + folders.push(JavaFolder { + folder: name.to_owned(), + targets: plan.targets, + world_at: early.count(), + passes, + flipped_after_prepare: plan.flipped_after_prepare, + final_pass, + flipped_at_end: plan.flipped_at_end, + }); + } + if folders.is_empty() { + return Err("no deferred, composite or final programs".into()); + } + Ok(JavaPasses { folders }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn loads_the_full_screen_passes_of_a_java_pack() { + let dir = Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../horizon-shaderpack/tests/fixtures/passes"); + assert!(is_java_pack(&dir)); + let pack = load(&dir, &BTreeMap::new(), "Apple", "Apple M4 Pro").unwrap(); + let [folder] = &pack.folders[..] else { + panic!("one folder, the root"); + }; + assert_eq!(folder.folder, ""); + assert_eq!( + folder + .passes + .iter() + .map(|p| p.pass.program.as_str()) + .collect::>(), + ["deferred", "composite", "composite1"] + ); + assert_eq!(folder.world_at, 0); + assert!(folder.final_pass.is_some()); + // The overworld, the Nether and the End all use the root. + assert_eq!([-1, 0, 1].map(|d| pack.folder_for(d)), [Some(0); 3]); + assert_eq!( + vendor_name(0x10de, "NVIDIA GeForce RTX 4080"), + "NVIDIA Corporation" + ); + assert_eq!(vendor_name(0, "Apple M4 Pro"), "Apple"); + } +} diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index 266bde023..66c50d0e7 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -85,6 +85,7 @@ pub mod item_data; pub mod item_render; pub mod item_tooltip; pub mod items; +pub mod java_pack; pub mod java_text; pub mod key_names; pub mod keybinds; diff --git a/crates/horizon-client/src/shader_pack.rs b/crates/horizon-client/src/shader_pack.rs index 55fdb992f..ec86edc77 100644 --- a/crates/horizon-client/src/shader_pack.rs +++ b/crates/horizon-client/src/shader_pack.rs @@ -109,6 +109,8 @@ pub struct ShaderPack { pub info: Info, pub stages: Vec, pub options: Vec, + /// A Java (OptiFine/Iris) pack's translated passes, instead of `stages`. + pub java: Option, dir: PathBuf, stamp: Stamp, } @@ -120,6 +122,11 @@ pub struct Stamp(Vec<(PathBuf, u64, Option)>); impl Stamp { pub fn of(dir: &Path) -> Self { let mut files = Vec::new(); + if let Ok(meta) = fs::metadata(dir) + && meta.is_file() + { + files.push((dir.to_path_buf(), meta.len(), meta.modified().ok())); + } let mut stack = vec![dir.to_path_buf()]; while let Some(d) = stack.pop() { let Ok(entries) = fs::read_dir(&d) else { @@ -141,23 +148,41 @@ impl Stamp { } /// The packs in `dir` (`shaderpacks/`): folders holding a `pack.toml`, by -/// folder name, with their manifest or why it can't be read. +/// folder name, with their manifest or why it can't be read, and Java packs +/// (folders holding `shaders/`, zips). pub fn list(dir: &Path) -> Vec<(String, Result)> { let mut packs: Vec<_> = fs::read_dir(dir) .into_iter() .flatten() .flatten() - .filter(|e| e.path().join("pack.toml").is_file()) - .map(|e| { + .filter_map(|e| { let id = e.file_name().to_string_lossy().into_owned(); - let info = manifest(&e.path()).map(|m| m.pack); - (id, info) + let path = e.path(); + if path.join("pack.toml").is_file() { + Some((id, manifest(&path).map(|m| m.pack))) + } else if crate::java_pack::is_java_pack(&path) { + Some((id.clone(), Ok(java_info(id)))) + } else { + None + } }) .collect(); packs.sort_by(|a, b| a.0.cmp(&b.0)); packs } +/// A Java pack has no manifest: its name is its file name. +fn java_info(name: String) -> Info { + Info { + name, + description: String::new(), + authors: Vec::new(), + version: String::new(), + license: String::new(), + api: API, + } +} + fn manifest(dir: &Path) -> Result { let text = fs::read_to_string(dir.join("pack.toml")).map_err(|e| format!("pack.toml: {e}"))?; let manifest: Manifest = toml::from_str(&text).map_err(|e| format!("pack.toml: {e}"))?; @@ -228,10 +253,38 @@ impl ShaderPack { info: manifest.pack, stages, options, + java: None, dir: dir.to_path_buf(), stamp, }) } + /// Load the pack at `path` (a folder or a zip): a Horizon pack, or a + /// Java pack translated for the GPU `vendor` and `renderer` names. + pub fn load_any( + path: &Path, + values: &BTreeMap, + vendor: &str, + renderer: &str, + ) -> Result { + if path.join("pack.toml").is_file() || !crate::java_pack::is_java_pack(path) { + return Self::load(path, values); + } + let stamp = Stamp::of(path); + let java = crate::java_pack::load(path, values, vendor, renderer)?; + let id = path + .file_name() + .map(|n| n.to_string_lossy().into_owned()) + .unwrap_or_default(); + Ok(Self { + info: java_info(id.clone()), + id, + stages: Vec::new(), + options: Vec::new(), + java: Some(java), + dir: path.to_path_buf(), + stamp, + }) + } /// The pipeline-overridable constants every stage gets: the option values. pub fn constants(&self) -> Vec<(&str, f64)> { self.options diff --git a/crates/horizon-shaderpack/src/pipeline.rs b/crates/horizon-shaderpack/src/pipeline.rs index f9dc00cc5..dfff469c4 100644 --- a/crates/horizon-shaderpack/src/pipeline.rs +++ b/crates/horizon-shaderpack/src/pipeline.rs @@ -8,8 +8,11 @@ //! and `CompositeRenderer` do. use crate::{ - JavaPack, options::OptionValues, preprocess::Environment, properties::LEGACY_TARGETS, - properties::ShaderProperties, + JavaPack, + options::OptionValues, + preprocess::Environment, + properties::{LEGACY_TARGETS, ShaderProperties}, + translate::{Link, Translated}, }; use std::collections::{BTreeMap, BTreeSet}; @@ -478,6 +481,91 @@ impl JavaPack { } } +/// A pass's programs translated, with the targets it draws to. +pub struct Program { + pub pass: Pass, + pub vertex: Translated, + pub fragment: Translated, + /// The target each output location writes: the pass's directive, or + /// as Iris does without one, location `i` to target `i`. + pub draw_buffers: Vec, + /// The bindings both programs share. + pub link: Link, +} + +impl JavaPack { + /// Translate `pass`'s `.vsh` and `.fsh`. + pub fn compile( + &self, + pass: &Pass, + targets: &[TargetSettings], + values: &OptionValues, + env: &Environment, + properties: &ShaderProperties, + ) -> Result { + let mut link = Link { + image_formats: properties.image_formats(), + shared_samplers: shared_samplers(targets), + ..Link::default() + }; + let vertex = self.translate(&format!("{}.vsh", pass.program), values, env, &mut link)?; + let fragment = self.translate(&format!("{}.fsh", pass.program), values, env, &mut link)?; + let draw_buffers = match &pass.draw_buffers { + Some(buffers) => buffers.clone(), + None => (0..fragment.interface.outputs.keys().max().map_or(1, |m| m + 1)).collect(), + }; + Ok(Program { + pass: pass.clone(), + vertex, + fragment, + draw_buffers, + link, + }) + } +} + +/// Iris's sampler states for the textures it binds: colour targets +/// linear (nearest for integer formats, and for 32-bit floats, which wgpu +/// can't filter everywhere), depth nearest, noise repeating. For +/// [`Link::shared_samplers`]. +pub fn shared_samplers(targets: &[TargetSettings]) -> BTreeMap { + let mut out = BTreeMap::new(); + for (i, t) in targets.iter().enumerate() { + let f = &t.format; + let class = if f.ends_with("32F") || f.ends_with('I') { + "iris_nearest" + } else { + "iris_linear" + }; + out.insert(format!("colortex{i}"), class.to_owned()); + if let Some(legacy) = LEGACY_TARGETS.get(i) { + out.insert((*legacy).to_owned(), class.to_owned()); + } + } + for name in [ + "depthtex0", + "depthtex1", + "depthtex2", + "gdepthtex", + "shadowtex0", + "shadowtex1", + "shadow", + "watershadow", + "dhDepthTex", + "dhDepthTex0", + "dhDepthTex1", + "vxDepthTexOpaque", + "vxDepthTexTrans", + ] { + out.insert(name.to_owned(), "iris_nearest".to_owned()); + } + for name in ["shadowcolor", "shadowcolor0", "shadowcolor1"] { + out.insert(name.to_owned(), "iris_linear".to_owned()); + } + out.insert("noisetex".to_owned(), "iris_repeat".to_owned()); + out +} + fn toggle(set: &mut BTreeSet, buffer: u32) { if !set.remove(&buffer) { set.insert(buffer); diff --git a/crates/horizon-shaderpack/src/translate.rs b/crates/horizon-shaderpack/src/translate.rs index fdd3ad128..f9a61169e 100644 --- a/crates/horizon-shaderpack/src/translate.rs +++ b/crates/horizon-shaderpack/src/translate.rs @@ -50,6 +50,11 @@ pub struct Link { pub uniforms: Vec<(String, String)>, /// Storage formats of images whose declaration has none. pub image_formats: BTreeMap, + /// Samplers shared by texture name (`colortex0` → `iris_linear`), so a + /// pass binds a few samplers rather than one per texture (Metal allows + /// 16). Once any is named, comparison samplers share `iris_compare` and + /// integer ones `iris_nearest`; textures not named keep `{name}_s`. + pub shared_samplers: BTreeMap, } impl Link { @@ -81,8 +86,11 @@ impl Link { #[derive(Default, Debug)] pub struct Interface { /// `(name, kind)` of each sampler the program reads (`colortex0`, - /// `sampler2D`); its bindings are `{name}_t` and `{name}_s` in the [`Link`]. + /// `sampler2D`); its bindings are `{name}_t` and its sampler's in the [`Link`]. pub samplers: Vec<(String, String)>, + /// Each sampler's sampler variable: `{name}_s`, or a shared one + /// ([`Link::shared_samplers`]). + pub sampler_vars: BTreeMap, /// `(name, format)` of each image. pub images: Vec<(String, String)>, /// Uniform names the program declares or uses. @@ -208,16 +216,18 @@ const IRIS_IMPLICIT: &[(&str, &str, bool)] = &[ ("alphaTestRef", "float", false), ]; +/// Built-ins naga lacks. `isnan` and `isinf` test the bits: Metal compiles +/// with fast math, which folds `x != x` to false. const HELPERS: &str = "\ struct iris_FogParameters { vec4 color; float density; float start; float end; float scale; }; -bool iris_isnan(float x) { return x != x; } -bvec2 iris_isnan(vec2 x) { return notEqual(x, x); } -bvec3 iris_isnan(vec3 x) { return notEqual(x, x); } -bvec4 iris_isnan(vec4 x) { return notEqual(x, x); } -bool iris_isinf(float x) { return abs(x) > 3.4028235e38; } -bvec2 iris_isinf(vec2 x) { return greaterThan(abs(x), vec2(3.4028235e38)); } -bvec3 iris_isinf(vec3 x) { return greaterThan(abs(x), vec3(3.4028235e38)); } -bvec4 iris_isinf(vec4 x) { return greaterThan(abs(x), vec4(3.4028235e38)); } +bool iris_isnan(float x) { return (floatBitsToUint(x) & 0x7fffffffu) > 0x7f800000u; } +bvec2 iris_isnan(vec2 x) { return greaterThan(floatBitsToUint(x) & 0x7fffffffu, uvec2(0x7f800000u)); } +bvec3 iris_isnan(vec3 x) { return greaterThan(floatBitsToUint(x) & 0x7fffffffu, uvec3(0x7f800000u)); } +bvec4 iris_isnan(vec4 x) { return greaterThan(floatBitsToUint(x) & 0x7fffffffu, uvec4(0x7f800000u)); } +bool iris_isinf(float x) { return (floatBitsToUint(x) & 0x7fffffffu) == 0x7f800000u; } +bvec2 iris_isinf(vec2 x) { return equal(floatBitsToUint(x) & 0x7fffffffu, uvec2(0x7f800000u)); } +bvec3 iris_isinf(vec3 x) { return equal(floatBitsToUint(x) & 0x7fffffffu, uvec3(0x7f800000u)); } +bvec4 iris_isinf(vec4 x) { return equal(floatBitsToUint(x) & 0x7fffffffu, uvec4(0x7f800000u)); } float iris_modf(float x, out float i) { i = trunc(x); return x - i; } vec2 iris_modf(vec2 x, out vec2 i) { i = trunc(x); return x - i; } vec3 iris_modf(vec3 x, out vec3 i) { i = trunc(x); return x - i; } @@ -288,7 +298,10 @@ struct Legaliser<'a> { link: &'a mut Link, interface: Interface, /// Global samplers: name → combined type. - samplers: HashMap, + /// Sampler uniforms: their combined type and sampler variable. + samplers: HashMap, + /// Sampler variables declared so far (shared ones once). + sampler_vars: HashSet, /// User functions → indices of their sampler parameters. sampler_params: HashMap>, functions: HashSet, @@ -484,6 +497,7 @@ impl<'a> Legaliser<'a> { link, interface: Interface::default(), samplers: HashMap::new(), + sampler_vars: HashSet::new(), sampler_params: HashMap::new(), functions: HashSet::new(), scopes: Vec::new(), @@ -601,17 +615,35 @@ impl<'a> Legaliser<'a> { || has(ast::StorageQualifierData::Out) && self.stage == Stage::Vertex || has(ast::StorageQualifierData::In) && self.stage == Stage::Fragment; if has(ast::StorageQualifierData::Uniform) { - if let Some(&(combined, texture, sampler, _)) = opaque(&ty) { + if let Some(&(combined, texture, sampler, compare)) = opaque(&ty) { for (name, _) in &names { let t = self.link.binding(&format!("{name}_t")); - let s = self.link.binding(&format!("{name}_s")); self.header.push(format!( "layout(set = 0, binding = {t}) uniform {texture} {name}_t;" )); - self.header.push(format!( - "layout(set = 0, binding = {s}) uniform {sampler} {name}_s;" - )); - self.samplers.insert(name.clone(), combined); + let shared = &self.link.shared_samplers; + let var = if shared.is_empty() { + format!("{name}_s") + } else if compare { + "iris_compare".to_owned() + } else if combined.starts_with(['i', 'u']) { + "iris_nearest".to_owned() + } else { + shared + .get(name) + .cloned() + .unwrap_or_else(|| format!("{name}_s")) + }; + if self.sampler_vars.insert(var.clone()) { + let s = self.link.binding(&var); + self.header.push(format!( + "layout(set = 0, binding = {s}) uniform {sampler} {var};" + )); + } + self.interface + .sampler_vars + .insert(name.clone(), var.clone()); + self.samplers.insert(name.clone(), (combined, var)); self.interface .samplers .push((name.clone(), combined.to_owned())); @@ -1034,13 +1066,14 @@ impl<'a> Legaliser<'a> { let combined = match self.local(name) { Some(Local::Sampler(c)) => Some(c), Some(Local::Plain) => return None, - None => self.samplers.get(name).copied(), + None => self.samplers.get(name).map(|(c, _)| *c), }; if let Some(c) = combined { - return Some(call( - c, - vec![var(&format!("{name}_t")), var(&format!("{name}_s"))], - )); + let sampler = match self.samplers.get(name) { + Some((_, v)) if self.local(name).is_none() => v.clone(), + _ => format!("{name}_s"), + }; + return Some(call(c, vec![var(&format!("{name}_t")), var(&sampler)])); } self.used.insert(name.to_owned()); if let Some((_, decl)) = LEGACY_UNIFORMS.iter().find(|(n, _)| *n == name) { @@ -1263,6 +1296,15 @@ impl<'a> Legaliser<'a> { if !self.errors.is_empty() { return Err(self.errors.join("\n")); } + if self.stage == Stage::Vertex { + // GL's clip space to wgpu's: y flipped, so texture row 0 stays + // the bottom of the picture as in GL (texture coordinates and + // `gl_FragCoord` then agree with the pack's), and z from + // [-w, w] to [0, w]. + self.epilogue.push("gl_Position.y = -gl_Position.y;".into()); + self.epilogue + .push("gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;".into()); + } let mut out = String::from("#version 450 core\n"); out.push_str(HELPERS); if !self.link.uniforms.is_empty() { diff --git a/crates/horizon-shaderpack/tests/pack.rs b/crates/horizon-shaderpack/tests/pack.rs index ec193847a..ce5115b37 100644 --- a/crates/horizon-shaderpack/tests/pack.rs +++ b/crates/horizon-shaderpack/tests/pack.rs @@ -368,5 +368,31 @@ fn real_packs_passes() { ); } assert!(passes.final_pass.is_some() || !passes.composite.is_empty()); + // Every planned pass translates, its outputs within its targets. + for p in passes + .begin + .iter() + .chain(&passes.prepare) + .chain(&passes.deferred) + .chain(&passes.composite) + .chain(&passes.final_pass) + { + let program = pack + .compile(p, &passes.targets, &values, &env(), &properties) + .unwrap(); + assert!( + program + .fragment + .interface + .outputs + .keys() + .all(|&l| (l as usize) < program.draw_buffers.len()) + || p.program.ends_with("final"), + "{}: outputs {:?}, targets {:?}", + p.program, + program.fragment.interface.outputs, + program.draw_buffers + ); + } } } diff --git a/crates/horizon-shaderpack/tests/translate.rs b/crates/horizon-shaderpack/tests/translate.rs index 3e58166c8..da8dc7e32 100644 --- a/crates/horizon-shaderpack/tests/translate.rs +++ b/crates/horizon-shaderpack/tests/translate.rs @@ -249,6 +249,13 @@ fn translates_a_compatibility_profile_pass() { assert!(!fs.glsl.contains("albedo = vec4(0.);"), "{}", fs.glsl); assert!(fs.glsl.contains("iris_isnan(albedo.r)"), "{}", fs.glsl); assert!(fs.wgsl.contains("@fragment")); + // GL's clip space becomes wgpu's after the program's own `main`. + assert!( + vs.glsl + .contains("gl_Position.y = -gl_Position.y;\n gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n}"), + "{}", + vs.glsl + ); } #[test] @@ -354,3 +361,47 @@ fn real_packs_translate() { } assert!(failures.is_empty(), "{}", failures.join("\n\n")); } + +#[test] +fn textures_share_samplers_by_name() { + let mut link = Link { + shared_samplers: [("colortex0", "iris_linear"), ("colortex1", "iris_linear")] + .map(|(a, b)| (a.to_owned(), b.to_owned())) + .into(), + ..Link::default() + }; + let fs = run( + Stage::Fragment, + "uniform sampler2D colortex0;\nuniform sampler2D colortex1;\nuniform sampler2D custom;\n\ + uniform sampler2DShadow shadowtex0;\nvarying vec2 uv;\n\ + void main() { gl_FragColor = texture2D(colortex0, uv) + texture2D(colortex1, uv)\n\ + + texture2D(custom, uv) + shadow2D(shadowtex0, vec3(uv, 0.5)); }", + &mut link, + ) + .unwrap(); + assert_eq!( + fs.glsl.matches("uniform sampler iris_linear;").count(), + 1, + "{}", + fs.glsl + ); + assert!( + fs.glsl.contains("sampler2D(colortex1_t, iris_linear)"), + "{}", + fs.glsl + ); + assert!(fs.glsl.contains("uniform sampler custom_s;"), "{}", fs.glsl); + assert!( + fs.glsl.contains("uniform samplerShadow iris_compare;"), + "{}", + fs.glsl + ); + assert_eq!(fs.interface.sampler_vars["colortex0"], "iris_linear"); + // Three sampler bindings for four textures. + assert_eq!( + bindings(&fs).keys().filter(|k| !k.ends_with("_t")).count(), + 3, + "{:?}", + bindings(&fs) + ); +} diff --git a/docs/shader-packs.md b/docs/shader-packs.md index bd9646df8..812b540f0 100644 --- a/docs/shader-packs.md +++ b/docs/shader-packs.md @@ -19,8 +19,9 @@ 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 | +| Java shader packs: full-screen pass plan (`horizon_shaderpack::pipeline`: `DRAWBUFFERS`/`RENDERTARGETS`, `colortexN` formats and clearing, buffer flips) | done: planned for the three test packs | +| Java shader packs: full-screen passes drawn (`java_pack.rs`, `gfx/java_stages.rs`: `begin`, `prepare`, `deferred*`, `composite*`, `final` over Horizon's world) | first slice: BSL and Complementary run (below) | +| Java shader packs: `gbuffers_*`, `shadow`, custom textures, images, compute, option screen | planned | ## How the renderer draws a frame today @@ -328,6 +329,42 @@ The spike also showed what the real translator needs that a token rewriter does uniform), types (which `texture` call returns a float) and declarations split across `#if`s, so it should rewrite a GLSL syntax tree after preprocessing, not tokens. +### Running a Java pack's full-screen passes + +A folder holding `shaders/`, or a `.zip`, in `shaderpacks/` shows in the Shader Packs screen next to Horizon packs. +Picking one translates every planned pass of each dimension folder (`java_pack.rs`) and runs them each frame +(`gfx/java_stages.rs`): + +1. the targets the pack clears are cleared; +2. `begin*` and `prepare*` run, then Horizon's world enters `colortex0` and `depthtex0..2` on the side the flips give; +3. `deferred*` and `composite*` run in order, each reading the side its plan gives and writing the other; +4. `final` draws into a target that replaces the world's picture, where Horizon packs' `composite` output goes: the + hand, menus and HUD are drawn over it. + +What makes Horizon look like OpenGL to the pack: + +- **Layout.** The translator ends every vertex shader by flipping `gl_Position.y` and moving depth from `[-w, w]` to + `[0, w]`, so the targets keep OpenGL's layout (row 0 at the bottom). The world is flipped going in, `final` coming out. +- **Depth.** Horizon's depth is reversed (1 near, 0 sky); the world goes in as `1 - depth`, and the matrices the pack + gets (`gbufferProjection` and its inverse, previous frame too) are OpenGL's, rebuilt from Horizon's. +- **Formats.** Iris formats become renderable wgpu formats: 8-bit unorm ones and `RGB10_A2` stay, `*32F` stay (sampled + unfiltered), integer formats become 32-bit, the rest (`R11F_G11F_B10F`, `*16F`, `*16`) 16-bit float, with the + channel count of the name. +- **Samplers.** Metal allows 16 samplers per stage and Complementary binds 23 textures, so textures share four + samplers by kind (`iris_linear`, `iris_nearest` for depth and 32-bit or integer targets, `iris_repeat` for + `noisetex`, `iris_compare` for shadow lookups) instead of one each. +- **`isnan`/`isinf`** test the float's bits: Metal compiles with fast math, which folds `x != x` to false, and a NaN + then stays forever in Complementary's TAA history (the picture turns black). +- **Uniforms.** Matrices, camera, view size, time, sun and moon, weather, fog and sky colour, eye state come from the + frame; `eyeBrightness(Smooth)` is `(0, 240)` under the sky; `noisetex` is a fixed 256² noise. + +Measured on the Mac mini (M4 Pro) with the packs' default options: BSL v10.1.8 and Complementary Reimagined r5.9.3 +grade and light Horizon's world as their post-processing would. Sildur's Vibrant v2.02 draws only its sky: its +`deferred` reads the albedo `gbuffers_*` write to `colortex4`. Without `gbuffers_*` and `shadow`, no pack has its +shadows, water or sky look yet; Distant Horizons' and Voxy's depth read as far. Not supported yet: images and +compute (`.csh`; a pack that needs them doesn't load), mipmapped targets (`colortexNMipmapEnabled` is read but +ignored), custom textures, custom uniforms, the option screen; shadow matrices are identity. + ### Order to implement 1. **Translator crate** (`horizon-shaderpack`): the preprocessor (includes, option `#define`s with the player's