diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index d01131433..072090e33 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -72,7 +72,9 @@ mod cull; mod end_sky; mod flat; mod hd; +mod hd_noise; mod hd_sky; +mod hd_volumes; mod hdr; pub mod lightning; mod lod; @@ -4278,6 +4280,17 @@ impl Renderer { water: !frame.underwater, sunrise: frame.sky.sunrise_color, sky_fog_end: frame.fog.map_or(frame.distance.min(512.0), |f| f.sky_end), + // AirDef fork: volumetric clouds stand where vanilla's would (option on, Overworld). + clouds: frame.clouds.is_some() && hd::clouds_enabled(), + camera_world: [ + camera.x.rem_euclid(65536.0) as f32, + camera.z.rem_euclid(65536.0) as f32, + camera.y as f32, + ], + seconds: frame + .clouds + .as_ref() + .map_or(0.0, |c| ((c.time as f64 + c.partial_tick) / 20.0) as f32), }; let (shadow_dir, shadow_strength, _) = hd::light_of(&hd_scene); let mut hd_cascades = hd::cascades(camera, shadow_dir); @@ -5071,6 +5084,7 @@ impl Renderer { } } let pack_reads_world = pack_post || native_post || mod_post || hd_on; + let hd_volumetric_clouds = hd_on && hd::clouds_enabled(); // 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. @@ -5128,9 +5142,9 @@ impl Renderer { let format = self.config.format.remove_srgb_suffix(); let device = &self.device; let globals_size = std::mem::size_of::() as u64; - let hd = self - .hd - .get_or_insert_with(|| hd::Hd::new(device, size, format, globals_size)); + let hd = self.hd.get_or_insert_with(|| { + hd::Hd::new(device, &self.queue, size, format, globals_size) + }); hd.resize(device, size, [self.config.width, self.config.height]); // Translucent terrain on screen: the water pass has something to draw. hd.water_geometry = match self.flat.as_ref().filter(|_| flat_frame) { @@ -5691,7 +5705,8 @@ impl Renderer { 0..1, ); } - if cloud_quads > 0 && !pack_draws_clouds { + // AirDef fork: HD draws the clouds volumetrically (in the lighting) instead. + if cloud_quads > 0 && !pack_draws_clouds && !hd_volumetric_clouds { pass.set_pipeline(&self.cloud_pipeline); pass.set_bind_group(0, &self.entity_group, &[]); pass.set_bind_group(1, &self.cloud_group, &[]); @@ -5821,6 +5836,7 @@ impl Renderer { // AirDef fork: lit by the mods' lights into the HD target, the mod's post pass on // that, then bloom and the tone curve onto the screen. hd.sky(&mut encoder); + hd.volumes(&self.device, &mut encoder, depth); hd.light( &self.device, &mut encoder, diff --git a/crates/horizon-client/src/gfx/THIRD_PARTY_PHOTON.md b/crates/horizon-client/src/gfx/THIRD_PARTY_PHOTON.md index 03d945001..fd9f4d029 100644 --- a/crates/horizon-client/src/gfx/THIRD_PARTY_PHOTON.md +++ b/crates/horizon-client/src/gfx/THIRD_PARTY_PHOTON.md @@ -1,14 +1,26 @@ # Third-party code: Photon Parts of the HD renderer's atmosphere (`hd_sky.rs`) are ported from, or follow closely, the -Photon shader pack by sixthsurge, `shaders/include/sky/atmosphere.glsl` (and the idea of the water +Photon shader pack by sixthsurge, `shaders/include/sky/atmosphere.glsl`, `shaders/include/lighting/colors/light_color.glsl`, `shaders/include/utility/phase_functions.glsl`, `shaders/include/sky/clouds/{common,cumulus}.glsl`, `shaders/include/lighting/cloud_shadows.glsl`, `shaders/include/fog/air_fog_vl.glsl` (and the idea of the water waves in `shaders/include/misc/water_normal.glsl`) at commit 26c2906ee02cc484359b835fba6993d443d45966, https://github.com/sixthsurge/photon -What is taken: the air's scattering and absorption coefficients (Rayleigh, Mie, ozone), the scale -heights, the ozone density profile, the Mie albedo and Henyey-Greenstein anisotropy, the sunlight -colour outside the atmosphere, and the Chapman-function approximation of the sun's transmittance. -The sky-view texture, its march, the sun's colour and everything else are our own. +What is taken from `atmosphere.glsl`: the air's scattering and absorption coefficients (Rayleigh, Mie, +ozone) moved to Rec. 2020 as Photon does (the march runs in that space, the result goes back to Rec. 709), +the scale heights, the ozone density profile, the Mie albedo, the Klein-Nishina Mie phase and the +depolarised Rayleigh phase (`phase_functions.glsl`), the sunlight colour outside the atmosphere, and the +Chapman-function approximation of the sun's transmittance. From `light_color.glsl`: `get_sun_tint` and +the blue-hour / sunset exposure lift (`sun_light_tint`, `sun_tint`), and `pulse`. The sky-view texture and +its march are our own. + +The volumetric clouds and fog (`hd_volumes.rs`, `hd_noise.rs` for the CPU-made noise textures) port +from `include/sky/clouds/common.glsl` and `cumulus.glsl` (the density shaping with the egg shape and +edge sharpening, Worley erosion, the single and multiple scattering phase functions, the powder effect, +the eight-octave multiple-scattering loop, the light march with doubling steps, the transmittance remap, +the aerial perspective), `include/lighting/cloud_shadows.glsl` (the cloud shadow map, here a lookup into a +small map) and `include/fog/air_fog_vl.glsl` (the phase mix of two Henyey-Greenstein lobes, the evening +glow). The 3D Worley and 2D noise textures are generated on our side; the ray march, the passes, the +reprojection and the integration are our own. Photon is distributed under the MIT License: diff --git a/crates/horizon-client/src/gfx/hd.rs b/crates/horizon-client/src/gfx/hd.rs index f078d423c..3b9daabf4 100644 --- a/crates/horizon-client/src/gfx/hd.rs +++ b/crates/horizon-client/src/gfx/hd.rs @@ -6,6 +6,8 @@ //! bright parts bloom and a tone curve brings it to the screen — vanilla's //! range (up to about 0.8) left as it is, only what is brighter rolled off. use super::hd_sky; +pub use super::hd_volumes::clouds_enabled; +use super::hd_volumes::{self, HdVolumes}; use super::pack_timing::PassTiming; use glam::Mat4; use std::sync::atomic::{AtomicU64, Ordering}; @@ -108,6 +110,18 @@ struct HdFrame { sky_extra: [f32; 4], /// Vanilla's sunrise colour fan (rgb, alpha), taken out of the sky before the atmosphere is put in. sunrise: [f32; 4], + /// x: the cloud slab's base height relative to the camera; y: its thickness; z: 1 when the + /// volumetric clouds are drawn; w: seconds (the wind). + clouds: [f32; 4], + /// xy: the camera's x and z in the world modulo 65536; z: extra coverage (rain); w: 1 when the + /// clouds' history is usable. + cloud_cam: [f32; 4], + /// xyz: the direction the clouds are lit from (the sun, or the moon at night). + cloud_dir: [f32; 4], + /// rgb: the colour of that light at the clouds; w: its strength. + cloud_sun: [f32; 4], + /// x: 1 when the volumetric fog is drawn; y: the camera's height in the world; z: the morning mist. + vfog: [f32; 4], } /// What the world around looks like this frame (from the renderer's globals). @@ -141,6 +155,11 @@ pub struct HdScene { /// Vanilla's sunrise fan colour and its sky fog end (to subtract vanilla's sky from the world). pub sunrise: [f32; 4], pub sky_fog_end: f32, + /// The volumetric clouds are drawn (vanilla's option is on, in the Overworld). + pub clouds: bool, + /// The camera's world position, x and z modulo 65536 (precision), and y; seconds of game time. + pub camera_world: [f32; 3], + pub seconds: f32, } /// The cascades' half sizes (blocks) and shadow map size. @@ -168,6 +187,11 @@ struct HdFrame { sky_sun: vec4, sky_extra: vec4, sunrise: vec4, + clouds: vec4, + cloud_cam: vec4, + cloud_dir: vec4, + cloud_sun: vec4, + vfog: vec4, } struct FullOut { @builtin(position) position: vec4, @@ -209,6 +233,13 @@ struct Light { // The water pass's depth: the nearest water surface (0 where there is none). @group(0) @binding(14) var water_depth: texture_depth_2d; //SKY15 +// The volumetric fog (half resolution: light in rgb, transmittance in a), the distance it was marched +// to, the volumetric clouds (half resolution: light, transmittance) and the clouds' shadow map. +@group(0) @binding(16) var fog_tex: texture_2d; +@group(0) @binding(17) var fog_dist_tex: texture_2d; +@group(0) @binding(18) var cloud_tex: texture_2d; +@group(0) @binding(19) var cloud_shadow_tex: texture_2d; +const CLOUD_SHADOW_R: f32 = 1200.0; // The pixel being lit, for the shadow filter's noise. var frag_px: vec2; @@ -327,6 +358,51 @@ fn linear_fog(d: f32, start: f32, end: f32) -> f32 { return (d - start) / (end - start); } +// The share of the sunlight the clouds let through at a camera-relative point: the shadow map +// holds, for the sun's way down through the slab from a point of its base, the optical depth +// (Photon's cloud shadow map, as a lookup rather than a pass). +fn cloud_shadow(p: vec3) -> f32 { + if frame.clouds.z < 0.5 { return 1.0; } + let l = frame.cloud_dir.xyz; + if l.y < 0.1 { return 1.0; } + let t = (frame.clouds.x - p.y) / l.y; + if t <= 0.0 { return 1.0; } + let q = p.xz + l.xz * t; + let uv = q / (2.0 * CLOUD_SHADOW_R) + vec2(0.5); + if any(uv < vec2(0.0)) || any(uv > vec2(1.0)) { return 1.0; } + let od = textureSampleLevel(cloud_shadow_tex, linear, uv, 0.0).r; + // Fade out as the sun gets low (the shadow then falls far from the cloud). + let fade = smoothstep(0.1, 0.25, l.y); + return mix(1.0, mix(1.0, exp(-od * 0.9), 0.9), fade); +} + +// The volumetric fog at a full-resolution pixel `dist` away: the four nearest half-resolution +// texels weighted by closeness and by how near their march's length is (so it does not bleed over edges). +fn fog_at(px: vec2, dist: f32) -> vec4 { + let size = vec2(textureDimensions(fog_tex)); + let f = vec2(px) * 0.5 - vec2(0.25); + let base = vec2(floor(f)); + let t = f - floor(f); + var sum = vec4(0.0); + var total = 0.0; + for (var j = 0; j < 2; j++) { + for (var i = 0; i < 2; i++) { + let q = clamp(base + vec2(i, j), vec2(0), size - 1); + let s = textureLoad(fog_tex, q, 0); + let sd = textureLoad(fog_dist_tex, q, 0).x; + let bw = select(1.0 - t.x, t.x, i == 1) * select(1.0 - t.y, t.y, j == 1); + let dz = (sd - dist) / (0.06 * dist + 0.4); + let w = bw / (1.0 + dz * dz * dz * dz); + sum += s * w; + total += w; + } + } + if total < 0.0001 { + return textureLoad(fog_tex, clamp(px / 2, vec2(0), size - 1), 0); + } + return sum / total; +} + @fragment fn fs_light(in: FullOut) -> @location(0) vec4 { let px = vec2(in.position.xy); @@ -373,10 +449,15 @@ fn fs_light(in: FullOut) -> @location(0) vec4 { let angle = acos(clamp(toward, -1.0, 1.0)); sky += frame.sun_color.rgb * shine * (pow(glow, 24.0) * 0.07 + pow(glow, 400.0) * 0.4 + 6.0 * (1.0 - smoothstep(0.016, 0.022, angle))); c = sky + kept; + // The volumetric clouds over the sky and the sun. + if frame.clouds.z > 0.5 { + let cl = textureSampleLevel(cloud_tex, linear, in.uv, 0.0); + c = c * cl.a + cl.rgb; + } } // Clouds (vanilla's, drawn into the world with depth but not into the G-buffer): lit by the low // sun's colour at dusk and dawn instead of vanilla's flat white. - if atmosphere && !water && d > 0.0 && g <= 0.0 && p.y > 8.0 && dot(p, p) > 2304.0 { + if atmosphere && frame.clouds.z < 0.5 && !water && d > 0.0 && g <= 0.0 && p.y > 8.0 && dot(p, p) > 2304.0 { let low = 1.0 - smoothstep(0.05, 0.5, frame.sky_sun.y); c = scene * mix(vec3(1.0), mix(frame.sun_color.rgb, vec3(1.0), 0.45) * 1.1, low * 0.7 * (1.0 - frame.sky_extra.z)); } @@ -391,7 +472,7 @@ fn fs_light(in: FullOut) -> @location(0) vec4 { let sky_reach = smoothstep(0.35, 0.95, lm.x); var s = 1.0; let ndl = dot(n, sun); - if sun_on > 0.0 && ndl > 0.0 && sky_reach > 0.0 { s = shadow_taps(p, n, select(1, 0, water)); } else { s = 0.0; } + if sun_on > 0.0 && ndl > 0.0 && sky_reach > 0.0 { s = shadow_taps(p, n, select(1, 0, water)) * cloud_shadow(p); } else { s = 0.0; } // Shadowed sky light (ambient) keeps vanilla's look, blocks' light stays as is; the sun adds // direct light by the face's angle. let direct = frame.sun_color.rgb * sun_on * max(ndl, 0.0) * s * sky_reach; @@ -436,10 +517,27 @@ fn fs_light(in: FullOut) -> @location(0) vec4 { c = mix(c, haze, (1.0 - exp(-sph / 700.0)) * 0.5); } } - // Light in the air: the sun's rays between the shadows, and halos round the mods' lights. + // The volumetric fog: the light the air scatters towards the camera (shafts through the trees, + // mist, haze), and how much of the pixel it hides. let far = select(min(length(p), frame.shadow.z), frame.shadow.z, d <= 0.0); let density = frame.ambient.z; - if sun_on > 0.0 && density > 0.0 { + let volumetric = frame.vfog.x > 0.5 && atmosphere; + if volumetric { + var fog_len = 192.0; + if d > 0.0 { + var pf = p; + if water { + let hf = frame.inverse_view_proj * vec4(in.uv.x * 2.0 - 1.0, 1.0 - in.uv.y * 2.0, max(d, 0.000001), 1.0); + pf = hf.xyz / hf.w; + } + fog_len = min(length(pf), 192.0); + } + let fg = fog_at(px, fog_len); + c = c * fg.a + fg.rgb; + } + // Light in the air (without the volumetric fog): the sun's rays between the shadows; always the + // halos round the mods' lights. + if !volumetric && sun_on > 0.0 && density > 0.0 { let steps = 14; let jitter = ign(in.position.xy); var acc = 0.0; @@ -1185,11 +1283,11 @@ fn fs_taau(in: FullOut) -> TaaOut { } "#; -struct Target { - view: wgpu::TextureView, +pub(super) struct Target { + pub(super) view: wgpu::TextureView, } -fn target( +pub(super) fn target( device: &wgpu::Device, label: &str, size: [u32; 2], @@ -1324,6 +1422,9 @@ pub struct Hd { /// The frame's translucent terrain exists (set by the renderer before `write`). pub water_geometry: bool, water_composited: bool, + /// The volumetric clouds and the fog run this frame. + clouds_active: bool, + fog_active: bool, /// The sky-view texture (see `hd_sky`), the pass that marches it and its bind group. sky_lut: wgpu::TextureView, sky_pipeline: wgpu::RenderPipeline, @@ -1346,7 +1447,9 @@ pub struct Hd { bloom: Vec, /// Bind groups, rebuilt only when their views change: `resize` drops all of them, the /// lighting and the source-reading ones are keyed by the views they read. - light_group: Option<(LightKey, wgpu::BindGroup)>, + light_group: [Option<(LightKey, wgpu::BindGroup)>; 2], + /// The volumetric clouds and fog (see `hd_volumes`). + volumes: HdVolumes, prefilter_group: Option<(wgpu::TextureView, wgpu::BindGroup)>, tone_group: Option<(wgpu::TextureView, wgpu::BindGroup)>, /// Reading bloom level k - 1 (for the down pass into level k, index k - 1) and level k (for @@ -1537,25 +1640,76 @@ fn ao_strength_default() -> f32 { /// The shader's source: the common part, then the body with the sky-reading functions where it /// has a marker (`//SKY15`: the sky-view texture is binding 15, `//SKY10`: binding 10). -fn assemble(body: &str) -> String { +pub(super) fn assemble(body: &str) -> String { let mut body = body.to_owned(); - for binding in ["15", "10"] { + // Highest number first: `//SKY1` must not eat the start of `//SKY15`. + for binding in (0..32).rev() { body = body.replace( &format!("//SKY{binding}"), - &hd_sky::SKY_FUNCS.replace("SKYBIND", binding), + &hd_sky::SKY_FUNCS.replace("SKYBIND", &binding.to_string()), ); } format!("{COMMON}{body}") } -fn module(device: &wgpu::Device, label: &str, body: &str) -> wgpu::ShaderModule { +pub(super) fn module(device: &wgpu::Device, label: &str, body: &str) -> wgpu::ShaderModule { + // Development: with `HORIZON_HD_SHADER_DIR` set, a shader's body is read from + // `/