diff --git a/Cargo.lock b/Cargo.lock index 76c8d226c..00da30cf1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1371,6 +1371,7 @@ dependencies = [ "horizon-mods", "horizon-protocol", "horizon-server-files", + "horizon-shader-api", "horizon-shaders", "horizon-telemetry", "horizon-theme", diff --git a/crates/horizon-client/Cargo.toml b/crates/horizon-client/Cargo.toml index 4877c18f2..a585ce19e 100644 --- a/crates/horizon-client/Cargo.toml +++ b/crates/horizon-client/Cargo.toml @@ -18,6 +18,8 @@ horizon-protocol = { path = "../horizon-protocol" } horizon-server-files = { path = "../horizon-server-files" } # Shader packs: reading them and turning their GLSL into naga modules. horizon-shaders = { path = "../horizon-shaders" } +# Horizon's own shader packs: the format, the stages and the check (docs/shaders.md). +horizon-shader-api = { path = "../horizon-shader-api" } horizon-friends = { path = "../horizon-friends" } # Horizon mods: sandboxed WebAssembly components, off unless installed and turned on (docs/mods.md). # Their runtime is the `mods` feature. diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 9a3503666..4638a4667 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -106,6 +106,7 @@ mod mounts; mod moving_pistons; mod music; mod names; +mod native_shaders; mod natural_spawning; mod nautili; mod net_harness; @@ -1507,6 +1508,8 @@ struct Game<'a> { extras: extras::ExtrasState, /// Horizon mods (`game/mods.rs`), none running unless turned on. mods: mods::ModsState, + /// Horizon's own shader pack, while one is picked (`game/native_shaders.rs`). + native_shaders: native_shaders::NativeShaders, /// Meshed sections waiting for an upload budget (Horizon settings). pending_uploads: Vec<((i32, i32, i32), horizon_client::mesher::SectionMesh)>, /// This frame's view frustum, for culling entities and block entities. @@ -12309,6 +12312,7 @@ impl<'a> Game<'a> { frame_graph: Default::default(), extras: Default::default(), mods: Default::default(), + native_shaders: Default::default(), sky_darken_memo: Default::default(), persist: Default::default(), remote: None, diff --git a/crates/horizon-client/src/game/native_shaders.rs b/crates/horizon-client/src/game/native_shaders.rs new file mode 100644 index 000000000..62aaafc42 --- /dev/null +++ b/crates/horizon-client/src/game/native_shaders.rs @@ -0,0 +1,110 @@ +//! Horizon's own shader packs in the game (docs/shaders.md), for development until the Shader +//! Packs page exists: `HORIZON_SHADER_PACK=` naming one in `shaderpacks/` (a folder with +//! `horizon-shaders.toml`, or a `.hshader`) runs it once it passes `horizon_shader_api::check`. +//! The pack is looked at twice a second: a saved file is checked and the pack rebuilt, and a +//! pack that fails is refused with its problems in the chat while the last good one keeps +//! drawing. + +use std::{ + hash::{Hash, Hasher}, + path::Path, + time::{Duration, Instant}, +}; + +use horizon_client::text::Text; +use horizon_shader_api::{Pack, check}; + +use super::Game; + +const LOOK_EVERY: Duration = Duration::from_millis(500); +const RED: u32 = 0xFF5555; + +#[derive(Default)] +pub(super) struct NativeShaders { + /// The pack's files when it was last read (`fingerprint`). + stamp: Option, + looked: Option, +} + +impl Game<'_> { + /// Builds the pack at `path` when its files changed since it was last read. + pub(super) fn tick_native_shader_pack(&mut self, path: &Path) { + let now = Instant::now(); + let state = &mut self.native_shaders; + if state.looked.is_some_and(|t| now - t < LOOK_EVERY) { + return; + } + state.looked = Some(now); + let stamp = fingerprint(path); + if stamp == state.stamp { + return; + } + let reload = state.stamp.is_some(); + state.stamp = stamp; + let Some(renderer) = &mut self.renderer else { + return; + }; + let result = Pack::read(path).map_err(|e| vec![e]).and_then(|pack| { + let report = check(&pack) + .map_err(|p| p.iter().map(ToString::to_string).collect::>())?; + let constants = pack + .manifest + .options + .iter() + .map(|o| (o.id.clone(), o.kind.default_constant())) + .collect(); + let labels = renderer + .set_native_shader_pack(Some((report, constants))) + .map_err(|e| vec![e])?; + Ok((pack.manifest, labels)) + }); + let name = path.file_name().unwrap_or_default().to_string_lossy(); + match result { + Ok((m, labels)) => { + log::info!( + "shader pack {} {}: {} in {} ms", + m.id, + m.version, + labels.join(", "), + now.elapsed().as_millis() + ); + if reload { + self.system_message(Text::literal(format!("Shader pack {} reloaded", m.name))); + } + } + Err(problems) => { + log::warn!("shader pack {name} refused: {}", problems.join("; ")); + self.system_message(Text::colored(format!("Shader pack {name} refused:"), RED)); + for p in problems { + self.system_message(Text::colored(p, RED)); + } + } + } + } +} + +/// A hash of the pack's file names, sizes and modification times (`None` when unreadable). +fn fingerprint(path: &Path) -> Option { + fn walk(path: &Path, hasher: &mut std::hash::DefaultHasher) -> Option<()> { + let meta = std::fs::metadata(path).ok()?; + path.hash(hasher); + if meta.is_dir() { + let mut entries: Vec<_> = std::fs::read_dir(path) + .ok()? + .flatten() + .map(|e| e.path()) + .collect(); + entries.sort(); + for entry in entries { + walk(&entry, hasher)?; + } + } else { + meta.len().hash(hasher); + meta.modified().ok()?.hash(hasher); + } + Some(()) + } + let mut hasher = std::hash::DefaultHasher::new(); + walk(path, &mut hasher)?; + Some(hasher.finish()) +} diff --git a/crates/horizon-client/src/game/shader_packs.rs b/crates/horizon-client/src/game/shader_packs.rs index f534e8391..ed8ad61b7 100644 --- a/crates/horizon-client/src/game/shader_packs.rs +++ b/crates/horizon-client/src/game/shader_packs.rs @@ -1,5 +1,6 @@ //! Shader packs, for development until the Shader Packs page exists: `HORIZON_SHADER_PACK=` -//! runs `shaderpacks/` (a folder or a zip beside `options.txt`) over the world. The +//! runs `shaderpacks/` (a folder or a zip beside `options.txt`) over the world; Horizon's +//! own packs go to `game/native_shaders.rs`. The //! programs of the current dimension's folder are loaded when the dimension changes (spec 1.2). use horizon_client::camera::CameraFluid; @@ -41,15 +42,18 @@ impl Game<'_> { Dim::Nether => -1, Dim::End => 1, }; - if let Some(renderer) = &mut self.renderer + let path = std::env::current_dir() + .unwrap_or_default() + .join("shaderpacks") + .join(&name); + if horizon_shader_api::package::is_pack(&path) { + // One of Horizon's own packs (game/native_shaders.rs). + self.tick_native_shader_pack(&path); + } else if let Some(renderer) = &mut self.renderer && renderer.shader_pack_dimension() != Some(dimension) { let name = name.to_string_lossy(); let started = std::time::Instant::now(); - let path = std::env::current_dir() - .unwrap_or_default() - .join("shaderpacks") - .join(&*name); match Pack::open(&path) { Ok(pack) => { let macros = Macros::standard(); diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 419f83c4a..5db82b19a 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -67,6 +67,7 @@ mod end_sky; mod flat; pub mod lightning; mod lod; +mod native_post; mod occlusion; mod pack_atlas; pub mod pack_draw; @@ -499,6 +500,8 @@ pub struct Renderer { world_post: world_post::Post, /// The shader pack's post-processing, and the dimension (legacy id) it was loaded for. pack_post: Option<(i32, Option)>, + /// The post passes of one of Horizon's own shader packs; never beside `pack_post`'s. + native_post: Option, /// The entity vertices Horizon draws itself while a shader pack draws the others. pack_entity_rest: Vec, /// Set 1 of the cloud pipeline, for a shader pack's `gbuffers_clouds`. @@ -2409,6 +2412,7 @@ impl Renderer { occlusion: occlusion::Occlusion::new(crate::chunk::MIN_Y, crate::chunk::HEIGHT / 16), world_post, pack_post: None, + native_post: None, pack_entity_rest: Vec::new(), cloud_bindings, pack_weather: None, @@ -2916,8 +2920,38 @@ impl Renderer { (programs, p.notes.clone()) }); self.pack_post = Some((dimension, post)); + if self.pack_post.as_ref().is_some_and(|p| p.1.is_some()) { + self.native_post = None; + } report } + /// Runs the post passes of one of Horizon's own shader packs over the world from now on + /// (`None`: no pack), with its options' values; replaces a `shaders/` pack. Returns the + /// passes' labels; on an error the passes that ran keep running. + pub fn set_native_shader_pack( + &mut self, + pack: Option<(horizon_shader_api::Report, Vec<(String, f64)>)>, + ) -> std::result::Result, String> { + let stages = pack.map(|(report, constants)| { + let post: Vec<_> = report + .stages + .into_iter() + .filter(|s| s.kind == horizon_shader_api::StageKind::Post) + .collect(); + (post, constants) + }); + let Some((post, constants)) = stages.filter(|(post, _)| !post.is_empty()) else { + self.native_post = None; + return Ok(Vec::new()); + }; + let format = self.config.format.remove_srgb_suffix(); + let post = native_post::NativePost::new(&self.device, post, &constants, format)?; + let labels = post.labels().into_iter().map(str::to_owned).collect(); + self.pack_post = None; + self.pack_atlas = None; + self.native_post = Some(post); + Ok(labels) + } /// What a shader pack's gbuffers programs read from Horizon's geometry: the sky's, and /// terrain's in the pack's block atlas (made here when missing; none when it does not fit). pub fn pack_inputs(&mut self) -> horizon_shaders::GameInputs { @@ -3980,6 +4014,9 @@ impl Renderer { } // The world's targets: the screen, or the scaled target of `post`. let pack_post = !frame.title && self.pack_post.as_ref().is_some_and(|p| p.1.is_some()); + // One of Horizon's own packs reads the same world target. + let native_post = !frame.title && self.native_post.is_some(); + let pack_reads_world = pack_post || native_post; // The pack's program reads the vertices this frame's draws bind. let pack_draws_terrain = pack_terrain && flat_frame == self.pack_terrain_flat; // The shadow camera's lists, drawn by the pack's shadow programs. @@ -4023,7 +4060,7 @@ impl Renderer { let scaled = self.world_post.prepare( &self.device, [self.config.width, self.config.height], - pack_post, + pack_reads_world, ); let (world_view, world_depth) = match &scaled { Some((color, depth)) => (color, depth), @@ -4062,7 +4099,7 @@ impl Renderer { depth_ops: Some(wgpu::Operations { load: wgpu::LoadOp::Clear(0.0), // Phase 2 of occlusion culling draws over it; a shader pack reads it. - store: if cull.is_some() || pack_post { + store: if cull.is_some() || pack_reads_world { wgpu::StoreOp::Store } else { wgpu::StoreOp::Discard @@ -4128,7 +4165,7 @@ impl Renderer { &mut encoder, world_view, world_depth, - pack_post, + pack_reads_world, ); pass.set_bind_group(0, &self.group, &[]); pass.set_pipeline(&flat.solid); @@ -4157,7 +4194,7 @@ impl Renderer { &mut encoder, world_view, world_depth, - pack_post, + pack_reads_world, ); pass.set_bind_group(0, &self.group, &[]); pass.set_pipeline(&self.solid); @@ -4427,6 +4464,20 @@ impl Renderer { &mut terrain, ); self.pack_post = Some((dimension, Some(post))); + } else if let Some((color, depth)) = &scaled + && native_post + && let Some(post) = &mut self.native_post + { + let size = self.world_post.size().unwrap_or([1, 1]); + post.run( + &self.device, + &self.queue, + &mut encoder, + (color, depth), + &view_target, + frame, + size, + ); } else if scaled.is_some() { self.world_post.finish(&mut encoder, &view_target); } diff --git a/crates/horizon-client/src/gfx/native_post.rs b/crates/horizon-client/src/gfx/native_post.rs new file mode 100644 index 000000000..c4af862d7 --- /dev/null +++ b/crates/horizon-client/src/gfx/native_post.rs @@ -0,0 +1,509 @@ +//! The post stage of Horizon's own shader packs (docs/shaders.md): each `[[post]]` pass is a +//! full-screen pipeline built from its checked naga module (`horizon_shader_api::check`), with +//! the pack's options as pipeline constants. A pass reads bind group 3: the frame, the world's +//! colour and depth, the previous pass's output and two samplers. Passes before the last write +//! half-float targets of the world's size, so values over 1 carry to the next; the last draws +//! onto the screen. + +use glam::{Mat4, Vec3}; +use horizon_shader_api::{Frame as FrameInputs, check::CheckedStage, stages::GROUP}; + +use super::Frame; + +const INTERMEDIATE: wgpu::TextureFormat = wgpu::TextureFormat::Rgba16Float; + +/// The world's colour and depth views a set of bind groups was made for. +type WorldViews = (wgpu::TextureView, wgpu::TextureView); + +pub struct NativePost { + passes: Vec<(String, wgpu::RenderPipeline)>, + layout: wgpu::BindGroupLayout, + frame: wgpu::Buffer, + linear: wgpu::Sampler, + nearest: wgpu::Sampler, + /// Two targets of the world's size, written in turn (none with a single pass). + targets: Option<([u32; 2], [wgpu::TextureView; 2])>, + /// One bind group per pass, for these world views. + groups: Option<(WorldViews, Vec)>, + start: std::time::Instant, + frames: u32, +} + +fn texture_entry(binding: u32, sample_type: wgpu::TextureSampleType) -> wgpu::BindGroupLayoutEntry { + wgpu::BindGroupLayoutEntry { + binding, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + count: None, + } +} + +fn sampler_entry(binding: u32) -> wgpu::BindGroupLayoutEntry { + wgpu::BindGroupLayoutEntry { + binding, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), + count: None, + } +} + +impl NativePost { + /// Pipelines for `stages` (the pack's post passes, in order) with the option values + /// `constants`, drawing onto a screen of `format`. Errors name the pass wgpu refused. + pub fn new( + device: &wgpu::Device, + stages: Vec, + constants: &[(String, f64)], + format: wgpu::TextureFormat, + ) -> Result { + let float = wgpu::TextureSampleType::Float { filterable: true }; + let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("Native shader pack post"), + entries: &[ + wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: wgpu::BufferSize::new(size_of::() as u64), + }, + count: None, + }, + texture_entry(1, float), + texture_entry(2, wgpu::TextureSampleType::Depth), + texture_entry(3, float), + sampler_entry(4), + sampler_entry(5), + ], + }); + let mut groups: [Option<&wgpu::BindGroupLayout>; GROUP as usize + 1] = Default::default(); + groups[GROUP as usize] = Some(&layout); + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("Native shader pack post"), + bind_group_layouts: &groups, + ..Default::default() + }); + let count = stages.len(); + let mut passes = Vec::with_capacity(count); + for (i, stage) in stages.into_iter().enumerate() { + let present: Vec<(&str, f64)> = constants + .iter() + .filter(|(id, _)| { + stage + .module + .overrides + .iter() + .any(|(_, o)| o.name.as_ref() == Some(id)) + }) + .map(|(id, v)| (id.as_str(), *v)) + .collect(); + let scope = device.push_error_scope(wgpu::ErrorFilter::Validation); + let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some(&stage.label), + source: wgpu::ShaderSource::Naga(std::borrow::Cow::Owned(stage.module)), + }); + let options = wgpu::PipelineCompilationOptions { + constants: &present, + ..Default::default() + }; + let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some(&stage.label), + layout: Some(&pipeline_layout), + vertex: wgpu::VertexState { + module: &module, + entry_point: Some("horizon_vs_post"), + compilation_options: options.clone(), + buffers: &[], + }, + primitive: Default::default(), + depth_stencil: None, + multisample: Default::default(), + fragment: Some(wgpu::FragmentState { + module: &module, + entry_point: Some("horizon_fs_post"), + compilation_options: options, + targets: &[Some(wgpu::ColorTargetState { + format: if i + 1 == count { format } else { INTERMEDIATE }, + blend: None, + write_mask: wgpu::ColorWrites::ALL, + })], + }), + multiview_mask: None, + cache: None, + }); + if let Some(e) = pollster::block_on(scope.pop()) { + return Err(format!("{}: {e}", stage.file)); + } + passes.push((stage.label, pipeline)); + } + let frame = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("Native shader pack frame"), + size: size_of::() as u64, + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + let sampler = |label, filter| { + device.create_sampler(&wgpu::SamplerDescriptor { + label: Some(label), + mag_filter: filter, + min_filter: filter, + ..Default::default() + }) + }; + Ok(Self { + passes, + layout, + frame, + linear: sampler("Native shader pack linear", wgpu::FilterMode::Linear), + nearest: sampler("Native shader pack nearest", wgpu::FilterMode::Nearest), + targets: None, + groups: None, + start: std::time::Instant::now(), + frames: 0, + }) + } + + /// The passes' labels, in order. + pub fn labels(&self) -> Vec<&str> { + self.passes.iter().map(|(l, _)| l.as_str()).collect() + } + + fn prepare( + &mut self, + device: &wgpu::Device, + world: (&wgpu::TextureView, &wgpu::TextureView), + size: [u32; 2], + ) { + if self.passes.len() > 1 && self.targets.as_ref().is_none_or(|t| t.0 != size) { + let target = |label| { + device + .create_texture(&wgpu::TextureDescriptor { + label: Some(label), + size: wgpu::Extent3d { + width: size[0], + height: size[1], + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: INTERMEDIATE, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }) + .create_view(&Default::default()) + }; + self.targets = Some(( + size, + [ + target("Native shader pack A"), + target("Native shader pack B"), + ], + )); + self.groups = None; + } + if self + .groups + .as_ref() + .is_some_and(|((color, depth), _)| color == world.0 && depth == world.1) + { + return; + } + let groups = (0..self.passes.len()) + .map(|i| { + // Pass i reads what pass i - 1 wrote: target (i - 1) % 2, the world for the first. + let previous = match (&self.targets, i) { + (Some((_, t)), 1..) => &t[(i - 1) % 2], + _ => world.0, + }; + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("Native shader pack post"), + layout: &self.layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: self.frame.as_entire_binding(), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::TextureView(world.0), + }, + wgpu::BindGroupEntry { + binding: 2, + resource: wgpu::BindingResource::TextureView(world.1), + }, + wgpu::BindGroupEntry { + binding: 3, + resource: wgpu::BindingResource::TextureView(previous), + }, + wgpu::BindGroupEntry { + binding: 4, + resource: wgpu::BindingResource::Sampler(&self.linear), + }, + wgpu::BindGroupEntry { + binding: 5, + resource: wgpu::BindingResource::Sampler(&self.nearest), + }, + ], + }) + }) + .collect(); + self.groups = Some(((world.0.clone(), world.1.clone()), groups)); + } + + /// Runs the passes over the world target `world` (colour, depth) of `size`, the last onto + /// `screen`. + pub fn run( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + world: (&wgpu::TextureView, &wgpu::TextureView), + screen: &wgpu::TextureView, + frame: &Frame, + size: [u32; 2], + ) { + let seconds = self.start.elapsed().as_secs_f32() % 3600.0; + let inputs = frame_inputs(frame, size, seconds, self.frames); + self.frames = self.frames.wrapping_add(1); + self.draw(device, queue, encoder, world, screen, &inputs, size); + } + + fn draw( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + world: (&wgpu::TextureView, &wgpu::TextureView), + screen: &wgpu::TextureView, + inputs: &FrameInputs, + size: [u32; 2], + ) { + self.prepare(device, world, size); + queue.write_buffer(&self.frame, 0, bytemuck::bytes_of(inputs)); + let Some((_, groups)) = &self.groups else { + return; + }; + for (i, ((label, pipeline), group)) in self.passes.iter().zip(groups).enumerate() { + let view = match &self.targets { + Some((_, t)) if i + 1 < self.passes.len() => &t[i % 2], + _ => screen, + }; + let attachments = [Some(wgpu::RenderPassColorAttachment { + view, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::BLACK), + store: wgpu::StoreOp::Store, + }, + })]; + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some(label), + color_attachments: &attachments, + ..Default::default() + }); + pass.set_pipeline(pipeline); + pass.set_bind_group(GROUP, group, &[]); + pass.draw(0..3, 0..1); + } + } +} + +/// The frame as packs read it (docs/api/shaders-reference.md), from the renderer's `Frame`. +pub fn frame_inputs(frame: &Frame, size: [u32; 2], seconds: f32, index: u32) -> FrameInputs { + let aspect = size[0] as f32 / size[1].max(1) as f32; + let view = crate::camera::view_matrix(frame.yaw, frame.pitch); + let projection = + super::world_projection(frame.fov_degrees, aspect, frame.depth_far) * frame.bob; + // A celestial body's direction at `angle` degrees (SkyRenderer: Y -90°, then X by the + // angle, of straight up): overhead at 0, rising in the east (+x). + let body = |angle: f32| { + let a = angle.to_radians(); + Vec3::new(-a.sin(), a.cos(), 0.0) + }; + let (fog_color, fog_start, fog_end) = match &frame.fog { + Some(f) => ( + [f.color[0], f.color[1], f.color[2]], + f.environmental_start.min(f.render_distance_start), + f.environmental_end.min(f.render_distance_end), + ), + None => (frame.sky.fog_color, frame.distance * 0.9, frame.distance), + }; + let pack = &frame.pack; + let level = |l: i32| l as f32 / 15.0; + let m = |m: Mat4| m.to_cols_array_2d(); + FrameInputs { + view: m(view), + projection: m(projection), + view_inverse: m(view.inverse()), + projection_inverse: m(projection.inverse()), + camera_position: frame.camera.as_vec3().to_array(), + time: seconds, + sun_direction: body(frame.sky.sun_angle).to_array(), + day_time: pack.day_time.rem_euclid(24000) as f32 / 24000.0, + moon_direction: body(frame.sky.moon_angle).to_array(), + game_time: frame.shader_game_time, + sky_color: frame.sky.sky_color, + rain: pack.rain, + fog_color, + thunder: pack.thunder, + resolution: [size[0] as f32, size[1] as f32], + eye_light: [level(pack.eye_light[0]), level(pack.eye_light[1])], + fog_start, + fog_end, + render_distance: frame.distance, + far: frame.depth_far, + night_vision: frame.night_vision, + blindness: pack.blindness, + darkness: frame.darkness.1.clamp(0.0, 1.0), + brightness: pack.brightness, + eye_in: pack.eye_in as u32, + dimension: if frame.end_sky { + 1 + } else if frame.nether_lighting { + -1 + } else { + 0 + }, + frame_index: index, + hide_gui: u32::from(pack.hide_gui), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use horizon_shader_api::{MANIFEST, Pack, check}; + + fn device() -> Option<(wgpu::Device, wgpu::Queue)> { + let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle()); + let adapter = pollster::block_on(instance.request_adapter(&Default::default())).ok()?; + pollster::block_on(adapter.request_device(&Default::default())).ok() + } + + fn texture( + device: &wgpu::Device, + format: wgpu::TextureFormat, + usage: wgpu::TextureUsages, + ) -> wgpu::Texture { + device.create_texture(&wgpu::TextureDescriptor { + label: None, + size: wgpu::Extent3d { + width: 4, + height: 4, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage, + view_formats: &[], + }) + } + + /// Two passes: the first doubles the world's colour (over 1, so the half-float target + /// must keep it) times an option, the second halves it and writes the depth as alpha. + #[test] + fn passes_chain_through_half_float_targets_with_options_and_depth() { + let Some((device, queue)) = device() else { + return; + }; + let manifest = "schema = 1\nid = \"t\"\nname = \"T\"\nversion = \"1\"\napi = \"0.1\"\n\ + [[post]]\nfile = \"a.wgsl\"\n[[post]]\nfile = \"b.wgsl\"\n\ + [[options]]\nid = \"gain\"\nname = \"Gain\"\ntype = \"float\"\ndefault = 0.0\nmin = 0.0\nmax = 2.0\n"; + let a = "override gain: f32;\nfn post(input: PostInput) -> vec4 { return vec4(input.color.rgb * 2.0 * gain, 1.0); }\n"; + let b = "fn post(input: PostInput) -> vec4 { return vec4(input.color.rgb * 0.5, input.depth); }\n"; + let files = [(MANIFEST, manifest), ("a.wgsl", a), ("b.wgsl", b)] + .map(|(n, t)| (n.to_owned(), t.as_bytes().to_vec())) + .into(); + let report = check(&Pack::from_files(files).unwrap()).unwrap(); + let format = wgpu::TextureFormat::Rgba8Unorm; + let mut post = + NativePost::new(&device, report.stages, &[("gain".into(), 1.0)], format).unwrap(); + assert_eq!(post.labels(), ["a.wgsl", "b.wgsl"]); + + let usage = wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING; + let color = texture(&device, format, usage).create_view(&Default::default()); + let depth = texture(&device, wgpu::TextureFormat::Depth32Float, usage) + .create_view(&Default::default()); + let screen = texture( + &device, + format, + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC, + ); + let mut encoder = device.create_command_encoder(&Default::default()); + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: None, + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: &color, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color { + r: 0.8, + g: 0.4, + b: 0.2, + a: 1.0, + }), + store: wgpu::StoreOp::Store, + }, + })], + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: &depth, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(0.5), + store: wgpu::StoreOp::Store, + }), + stencil_ops: None, + }), + ..Default::default() + }); + let view = screen.create_view(&Default::default()); + post.draw( + &device, + &queue, + &mut encoder, + (&color, &depth), + &view, + &FrameInputs::default(), + [4, 4], + ); + let buffer = device.create_buffer(&wgpu::BufferDescriptor { + label: None, + size: u64::from(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT * 4), + usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + encoder.copy_texture_to_buffer( + screen.as_image_copy(), + wgpu::TexelCopyBufferInfo { + buffer: &buffer, + layout: wgpu::TexelCopyBufferLayout { + offset: 0, + bytes_per_row: Some(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT), + rows_per_image: Some(4), + }, + }, + screen.size(), + ); + queue.submit([encoder.finish()]); + buffer + .slice(..) + .map_async(wgpu::MapMode::Read, |r| r.unwrap()); + device.poll(wgpu::PollType::wait_indefinitely()).unwrap(); + let pixel = buffer.slice(..).get_mapped_range().unwrap()[..4].to_vec(); + let near = |got: u8, want: u8| got.abs_diff(want) <= 1; + assert!( + near(pixel[0], 204) && near(pixel[1], 102) && near(pixel[2], 51) && near(pixel[3], 128), + "{pixel:?}" + ); + } +} diff --git a/crates/horizon-shader-api/packs/golden_hour/post/glow.wgsl b/crates/horizon-shader-api/packs/golden_hour/post/glow.wgsl index 4d523c9ef..e27b98244 100644 --- a/crates/horizon-shader-api/packs/golden_hour/post/glow.wgsl +++ b/crates/horizon-shader-api/packs/golden_hour/post/glow.wgsl @@ -1,9 +1,12 @@ -// A soft halo around bright pixels: twelve taps on two rings, scaled with -// the picture's height so it looks the same at any resolution. +// A soft halo around the brightest pixels (the sun, lava, torches at night): twelve taps on two +// rings, scaled with the picture's height so it looks the same at any resolution. The daytime +// sky is bright too, so it glows only a quarter as much. override glow: f32 = 0.35; -fn bright(c: vec3) -> vec3 { - return c * smoothstep(0.7, 1.0, luminance(c)); +fn bright(uv: vec2) -> vec3 { + let c = textureSampleLevel(previous, linear_sampler, uv, 0.0).rgb; + let sky = select(1.0, 0.25, depth_at(uv) == 0.0); + return c * smoothstep(0.8, 1.0, luminance(c)) * sky; } fn post(input: PostInput) -> vec4 { @@ -16,7 +19,7 @@ fn post(input: PostInput) -> vec4 { let angle = f32(i) * 0.5235988; let radius = select(3.0, 7.0, (i & 1) == 1); let offset = vec2(cos(angle), sin(angle)) * radius * step; - sum += bright(textureSampleLevel(previous, linear_sampler, input.uv + offset, 0.0).rgb); + sum += bright(input.uv + offset); } - return vec4(input.color.rgb + sum * (glow / 6.0), input.color.a); + return vec4(input.color.rgb + sum * (glow / 8.0), input.color.a); } diff --git a/crates/horizon-shader-api/src/inputs.rs b/crates/horizon-shader-api/src/inputs.rs index 5bc82fd82..f25fe2847 100644 --- a/crates/horizon-shader-api/src/inputs.rs +++ b/crates/horizon-shader-api/src/inputs.rs @@ -158,7 +158,7 @@ pub const POST_INPUT: Struct = Struct { doc: "One pixel of the picture, for `fn post(input: PostInput) -> vec4`.", fields: fields! { uv: "vec2" = "Where the pixel is, 0..1 from the top left.", - pixel: "vec2" = "The pixel's centre, in pixels from the top left.", + pixel: "vec2" = "The pixel's centre, in pixels from the top left of what the pass draws (the world's size; the window's for the last pass).", color: "vec4" = "The previous pass's colour here (the world's picture for the first pass).", depth: "f32" = "The world's depth here, reversed: 0 is the sky.", }, diff --git a/crates/horizon-shader-api/src/package.rs b/crates/horizon-shader-api/src/package.rs index e2e644a04..b148d95c5 100644 --- a/crates/horizon-shader-api/src/package.rs +++ b/crates/horizon-shader-api/src/package.rs @@ -46,6 +46,13 @@ pub fn plain_path(path: &str) -> bool { }) } +/// Whether `path` is a pack of this format: a folder holding +/// `horizon-shaders.toml`, or a `.hshader` file. +pub fn is_pack(path: &Path) -> bool { + path.join(MANIFEST).is_file() + || (path.is_file() && path.extension().is_some_and(|e| e == EXTENSION)) +} + impl Pack { /// Reads `path`: a `.hshader` file, or a folder (its hidden files and /// folders, such as `.git`, are skipped). diff --git a/docs/api/shaders-reference.md b/docs/api/shaders-reference.md index f9682a1a4..400d5029b 100644 --- a/docs/api/shaders-reference.md +++ b/docs/api/shaders-reference.md @@ -77,7 +77,7 @@ One pixel of the picture, for `fn post(input: PostInput) -> vec4`. | Field | Type | Meaning | | --- | --- | --- | | `uv` | `vec2` | Where the pixel is, 0..1 from the top left. | -| `pixel` | `vec2` | The pixel's centre, in pixels from the top left. | +| `pixel` | `vec2` | The pixel's centre, in pixels from the top left of what the pass draws (the world's size; the window's for the last pass). | | `color` | `vec4` | The previous pass's colour here (the world's picture for the first pass). | | `depth` | `f32` | The world's depth here, reversed: 0 is the sky. | diff --git a/docs/api/shaders.md b/docs/api/shaders.md index 8e9e6919c..675c5e3fc 100644 --- a/docs/api/shaders.md +++ b/docs/api/shaders.md @@ -96,6 +96,7 @@ stages with options and hot reload, then the overlay, lighting, sky and shadow s ## Changelog +- 2026-10-26: the post stage in the game (`HORIZON_SHADER_PACK`, hot reload with the last good pack kept). - 2026-10-25: `horizon-shader` (`new`, `check`, `pack`), three templates, the example pack and the guide. - 2026-10-25: the check (naga validation, signatures, options, budgets, SPIR-V/Metal/HLSL translation). - 2026-10-25: the stages of API 0.1 (sky, material, post), their inputs and the generated reference. diff --git a/docs/shaders.md b/docs/shaders.md index 99a08fa9a..ef8521cfe 100644 --- a/docs/shaders.md +++ b/docs/shaders.md @@ -13,7 +13,8 @@ Horizon's own format. | Authoring tool | `horizon-shader` (same crate) | `new`, `check`, `pack` | | Templates | `crates/horizon-shader-api/templates/` | post, sky, material | | Example pack | `crates/horizon-shader-api/packs/golden_hour` | a warm sky and two post passes | -| In the game, the launcher, the registry | | not yet: the renderer hooks come next | +| In the game: the post stage | `HORIZON_SHADER_PACK=` (for development, until the Shader Packs page) | post passes, hot reload | +| In the game: sky and material; the launcher and the registry | | not yet | ## Quick start @@ -29,9 +30,9 @@ horizon-shader pack my_pack # check, then write my_pack- ``` golden_hour 1.0.0 (shader API 0.1): ok sky Warm horizon 99 expressions, 0 texture reads - post Glow 185 expressions, 3 texture reads + post Glow 192 expressions, 3 texture reads post Colour grade 201 expressions, 2 texture reads - sha256 fd92a294... + sha256 b5e0d98c... ``` or every problem, at its file, line and column: @@ -43,6 +44,15 @@ my_pack 0.1.0 (shader API 0.1): FAILED `--json` prints the same for tools (`ok`, `id`, `version`, `api`, `sha256`, `stages`, `problems`). +## Trying a pack in the game + +Until the Shader Packs page lists them, put the pack folder (or its `.hshader`) in `shaderpacks/` beside `options.txt` +and start the game with `HORIZON_SHADER_PACK=`. The game checks the pack first, and refuses +one that fails, with its problems in the chat. While it runs, the game looks at the pack twice a second: save a file +and the pack is checked and rebuilt in place ("Shader pack ... reloaded"); if the new version fails, the chat says +where and the last good version keeps drawing. The post passes run today; the sky and material stages are checked +but not drawn yet. + ## A pack A folder (or the same files zipped as a `.hshader`) with `horizon-shaders.toml` and WGSL files: