feat(shaders): complete wgpu Iris/Horizon API-2 shader pipeline, shadow & G-buffer passes, GLSL translator fixes, and settings/import UI

This commit is contained in:
kerneldroid
2026-10-23 10:37:14 +05:00
parent 5bb492ca54
commit 4dff61b697
27 changed files with 3614 additions and 287 deletions
@@ -0,0 +1,13 @@
MIT License
Copyright (c) 2026 Horizon contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
@@ -0,0 +1,134 @@
# Horizon Ultra: multi-pass API 2 WGSL shader pack with PCF directional shadows,
# G-buffer GTAO, water SSR & absorption, volumetric godrays, HDR bloom and ACES tonemapping.
[pack]
name = "Horizon Ultra"
description = "PCF sun/moon shadows, GTAO, water reflections, volumetric godrays, bloom and ACES filmic tonemapping."
authors = ["Horizon contributors"]
version = "2.0.0"
license = "MIT"
api = 2
[[stage]]
kind = "deferred"
shader = "shaders/deferred_lighting.wgsl"
format = "rgba16float"
[[stage]]
kind = "composite"
shader = "shaders/water_reflections.wgsl"
format = "rgba16float"
[[stage]]
kind = "composite"
shader = "shaders/bloom_godrays.wgsl"
scale = 0.5
format = "rgba16float"
mipmap = true
[[stage]]
kind = "final"
shader = "shaders/final_tonemap.wgsl"
[profiles.low]
shadows = false
ao_strength = 0.25
water_ssr = false
godrays = false
bloom_strength = 0.15
tonemap_aces = true
vignette = false
[profiles.medium]
shadows = true
shadow_strength = 0.70
ao_strength = 0.50
water_ssr = true
godrays = true
bloom_strength = 0.25
tonemap_aces = true
vignette = true
[profiles.high]
shadows = true
shadow_strength = 0.85
ao_strength = 0.75
water_ssr = true
godrays = true
bloom_strength = 0.35
tonemap_aces = true
vignette = true
[profiles.ultra]
shadows = true
shadow_strength = 1.0
ao_strength = 1.0
water_ssr = true
godrays = true
bloom_strength = 0.45
tonemap_aces = true
vignette = true
[options.shadows]
label = "Directional Shadows"
description = "Cast real-time sun and moon PCF shadows across terrain and entities."
default = true
[options.shadow_strength]
label = "Shadow Strength"
description = "Intensity of directional sun and moon shadows."
default = 0.85
min = 0.0
max = 1.0
step = 0.05
[options.ao_strength]
label = "Ambient Occlusion (GTAO)"
description = "Screen-space horizon ambient occlusion in corners and crevices."
default = 0.75
min = 0.0
max = 1.5
step = 0.05
[options.water_ssr]
label = "Water Reflections & Refraction"
description = "Screen-space sky and terrain reflections on water and translucent surfaces."
default = true
[options.godrays]
label = "Volumetric Godrays"
description = "Atmospheric light shafts cast through trees and terrain towards the sun."
default = true
[options.bloom_strength]
label = "HDR Bloom"
description = "Soft glow around bright sky, lava, torches and emissive blocks."
default = 0.35
min = 0.0
max = 1.0
step = 0.05
[options.exposure]
label = "Exposure"
description = "Overall scene exposure multiplier before filmic tonemapping."
default = 1.10
min = 0.5
max = 2.0
step = 0.05
[options.saturation]
label = "Color Saturation"
description = "Color vibrancy of the final tonemapped image."
default = 1.12
min = 0.0
max = 2.0
step = 0.05
[options.tonemap_aces]
label = "ACES Filmic Tonemap"
description = "Apply ACES fitted filmic curve to preserve highlight detail."
default = true
[options.vignette]
label = "Cinematic Vignette"
description = "Subtle darkening at the edges of the viewport."
default = true
@@ -0,0 +1,61 @@
// Stage 3 (Composite, half-resolution): HDR bloom extraction + 9-tap tent filter
// and radial volumetric light shafts (godrays) towards the sun/moon.
override godrays: bool = true;
override bloom_strength: f32 = 0.35;
@fragment fn fs_main(in: StageIn) -> @location(0) vec4<f32> {
let base = textureSampleLevel(previous, linear_sampler, in.uv, 0.0).rgb;
let texel = frame.resolution.zw * 2.0;
// 9-tap soft bloom filter around bright highlights.
var bloom = vec3(0.0);
if bloom_strength > 0.001 {
let offsets = array<vec2<f32>, 9>(
vec2(0.0, 0.0),
vec2(1.0, 0.0),
vec2(-1.0, 0.0),
vec2(0.0, 1.0),
vec2(0.0, -1.0),
vec2(1.0, 1.0),
vec2(-1.0, 1.0),
vec2(1.0, -1.0),
vec2(-1.0, -1.0),
);
let weights = array<f32, 9>(0.25, 0.125, 0.125, 0.125, 0.125, 0.0625, 0.0625, 0.0625, 0.0625);
for (var i = 0u; i < 9u; i++) {
let sample_c = textureSampleLevel(previous, linear_sampler, in.uv + offsets[i] * texel, 0.0).rgb;
let lum = luminance(sample_c);
let bright = max(lum - 0.72, 0.0) / max(lum, 1e-4);
bloom += sample_c * bright * weights[i];
}
}
// Radial volumetric godrays cast from the sun's screen projection.
var shafts = vec3(0.0);
if godrays && frame.camera_state.x == 0.0 && frame.dimension.y > 0.5 {
let sun_clip = frame.projection * (frame.view * vec4(frame.sun.xyz * 128.0, 1.0));
if sun_clip.w > 0.0 {
let sun_ndc = sun_clip.xy / sun_clip.w;
let sun_uv = vec2(sun_ndc.x * 0.5 + 0.5, 0.5 - sun_ndc.y * 0.5);
let delta = (sun_uv - in.uv) / 12.0;
var uv_Cursor = in.uv;
var transmittance = 0.0;
var decay = 1.0;
for (var s = 0u; s < 12u; s++) {
uv_Cursor += delta;
let sample_uv = clamp(uv_Cursor, vec2(0.001), vec2(0.999));
if is_sky(depth_at(sample_uv)) {
transmittance += decay;
}
decay *= 0.92;
}
let ray = normalize(world_offset(in.uv, 1e-5));
let sun_facing = pow(max(dot(ray, frame.sun.xyz), 0.0), 4.0);
let sunset = smoothstep(-0.1, 0.25, frame.sun.y);
let shaft_color = mix(vec3(1.0, 0.55, 0.25), vec3(1.0, 0.92, 0.75), sunset);
shafts = shaft_color * (transmittance / 12.0) * sun_facing * 0.28 * (1.0 - frame.weather.x);
}
}
return vec4(base + bloom * bloom_strength + shafts, 1.0);
}
@@ -0,0 +1,92 @@
// Stage 1 (Deferred): G-buffer normal reconstruction, multi-tap GTAO ambient
// occlusion, directional sun/moon PCF shadow mapping, and rain puddle wetness.
override shadows: bool = true;
override shadow_strength: f32 = 0.85;
override ao_strength: f32 = 0.75;
fn compute_gtao(uv: vec2<f32>, depth: f32, view_pos: vec3<f32>) -> f32 {
if ao_strength <= 0.001 {
return 1.0;
}
let step_uv = frame.resolution.zw * 2.5;
let offsets = array<vec2<f32>, 8>(
vec2(1.0, 0.0),
vec2(-1.0, 0.0),
vec2(0.0, 1.0),
vec2(0.0, -1.0),
vec2(0.707, 0.707),
vec2(-0.707, 0.707),
vec2(0.707, -0.707),
vec2(-0.707, -0.707),
);
var occlusion = 0.0;
for (var i = 0u; i < 8u; i++) {
let sample_uv = clamp(uv + offsets[i] * step_uv, vec2(0.001), vec2(0.999));
let sd = opaque_depth_at(sample_uv);
if !is_sky(sd) {
let sp = view_position(sample_uv, sd);
let diff = sp - view_pos;
let dist = length(diff);
if dist > 0.02 && dist < 3.5 {
let horizon = clamp(diff.z / dist, 0.0, 1.0);
occlusion += horizon * smoothstep(3.5, 0.2, dist);
}
}
}
return clamp(1.0 - (occlusion * 0.18 * ao_strength), 0.15, 1.0);
}
@fragment fn fs_main(in: StageIn) -> @location(0) vec4<f32> {
let base_srgb = textureSampleLevel(scene_color, linear_sampler, in.uv, 0.0).rgb;
var color = srgb_to_linear(base_srgb);
let depth = opaque_depth_at(in.uv);
let light_dir = normalize(select(frame.moon.xyz, frame.sun.xyz, frame.sun.y >= -0.05));
let sun_elevation = clamp(frame.sun.y, -1.0, 1.0);
let day_factor = smoothstep(-0.08, 0.18, sun_elevation);
let sunset_factor = smoothstep(-0.12, 0.05, sun_elevation) * (1.0 - smoothstep(0.05, 0.35, sun_elevation));
if is_sky(depth) {
let ray = normalize(world_offset(in.uv, 1e-5));
let sun_dot = max(dot(ray, frame.sun.xyz), 0.0);
let mie = pow(sun_dot, 12.0) * (0.35 + 0.65 * sunset_factor) * (1.0 - frame.weather.x * 0.8);
let sun_tint = mix(vec3(1.0, 0.88, 0.68), vec3(1.0, 0.45, 0.18), sunset_factor);
color += sun_tint * mie * 0.85;
return vec4(color, 1.0);
}
let world_rel = world_offset(in.uv, depth);
let view_pos = view_position(in.uv, depth);
let normal = world_normal_at(in.uv);
let mat = material_at(in.uv);
// Ambient occlusion from depth discontinuity neighborhood.
let ao = compute_gtao(in.uv, depth, view_pos);
// Directional sun/moon shadow factor.
var shadow = 1.0;
if shadows && frame.dimension.y > 0.5 {
let raw_shadow = sample_shadow(world_rel, normal);
shadow = mix(1.0, raw_shadow, shadow_strength);
}
let ndotl = max(dot(normal, light_dir), 0.0);
let sun_color = mix(
vec3(0.22, 0.28, 0.48),
mix(vec3(1.05, 0.96, 0.86), vec3(1.18, 0.58, 0.28), sunset_factor),
day_factor,
);
let sky_ambient = mix(vec3(0.08, 0.11, 0.20), srgb_to_linear(frame.sky_color.rgb) * 0.55, day_factor);
let direct_light = sun_color * ndotl * shadow * (1.0 - frame.weather.x * 0.55);
let bounce_light = sky_ambient * (0.45 + 0.55 * max(normal.y, 0.0)) * ao;
// Wetness specular sheen during rain on upward-facing surfaces.
let wetness = frame_ext.environment.z;
let view_dir = normalize(-world_rel);
let half_vec = normalize(light_dir + view_dir);
let wet_mask = wetness * smoothstep(0.65, 0.95, normal.y) * mat.g;
let spec = pow(max(dot(normal, half_vec), 0.0), 48.0) * wet_mask * shadow * 0.65;
let shade_boost = mix(0.68 * ao, 1.0 + 0.35 * ndotl * shadow, 0.65);
color = color * shade_boost + color * direct_light * 0.32 + bounce_light * 0.08 + sun_color * spec;
return vec4(max(color, vec3(0.0)), 1.0);
}
@@ -0,0 +1,29 @@
// Stage 4 (Final): Combines full-resolution scene with bloom/godrays, applies
// exposure, ACES filmic tonemapping, saturation, and cinematic vignette.
override exposure: f32 = 1.10;
override saturation: f32 = 1.12;
override tonemap_aces: bool = true;
override vignette: bool = true;
@fragment fn fs_main(in: StageIn) -> @location(0) vec4<f32> {
var hdr = textureSampleLevel(previous, linear_sampler, in.uv, 0.0).rgb * exposure;
if tonemap_aces {
hdr = aces_tonemap(hdr);
} else {
hdr = clamp(hdr, vec3(0.0), vec3(1.0));
}
var srgb = linear_to_srgb(hdr);
let luma = luminance(srgb);
let rain_desat = 1.0 - 0.25 * max(frame.weather.x, frame.weather.y);
srgb = mix(vec3(luma), srgb, saturation * rain_desat);
if vignette {
let aspect = frame.resolution.x * frame.resolution.w;
let d = length((in.uv - 0.5) * vec2(aspect, 1.0));
srgb *= 1.0 - 0.26 * smoothstep(0.48, 0.98, d);
}
return vec4(clamp(srgb, vec3(0.0), vec3(1.0)), 1.0);
}
@@ -0,0 +1,58 @@
// Stage 2 (Composite): Screen-space water reflections (SSR), Fresnel Schlick,
// and Beer-Lambert underwater depth absorption using scene_depth vs opaque_depth.
override water_ssr: bool = true;
@fragment fn fs_main(in: StageIn) -> @location(0) vec4<f32> {
var color = textureSampleLevel(previous, linear_sampler, in.uv, 0.0).rgb;
let surface_depth = depth_at(in.uv);
let floor_depth = opaque_depth_at(in.uv);
let mat = material_at(in.uv);
// Translucent water/glass layer exists where surface_depth is closer than floor_depth
// (reversed depth: closer = larger value) or where the G-buffer water mask is set.
let is_translucent = (surface_depth > floor_depth + 1e-5) || (mat.b > 0.5);
if !water_ssr || is_sky(surface_depth) || !is_translucent {
return vec4(color, 1.0);
}
let water_pos = world_offset(in.uv, surface_depth);
let floor_pos = world_offset(in.uv, max(floor_depth, 1e-6));
let water_column = clamp(length(floor_pos - water_pos), 0.0, 32.0);
// Beer-Lambert depth absorption in deep water.
let absorption = exp(-vec3(0.22, 0.07, 0.03) * water_column * 0.35);
color *= mix(vec3(1.0), absorption, clamp(water_column * 0.08, 0.0, 0.65));
// Wave normal perturbation and Fresnel reflection.
let world_xz = (water_pos.xz + frame.camera_position.xz) * 0.08;
let wave_uv = world_xz + vec2(frame.time.x * 0.015, -frame.time.x * 0.011);
let noise_sample = textureSampleLevel(noise_texture, repeat_sampler, wave_uv, 0.0).rgb * 2.0 - 1.0;
let normal = normalize(vec3(noise_sample.x * 0.06, 1.0, noise_sample.y * 0.06));
let view_dir = normalize(-water_pos);
let refl_dir = reflect(-view_dir, normal);
let fresnel = clamp(0.04 + 0.96 * pow(1.0 - max(dot(view_dir, normal), 0.0), 4.0), 0.0, 0.85);
// Ray-march 8 steps in screen space along the reflection ray.
var refl_color = srgb_to_linear(frame.sky_color.rgb);
let sun_spec = pow(max(dot(refl_dir, frame.sun.xyz), 0.0), 96.0) * 2.5 * (1.0 - frame.weather.x);
refl_color += vec3(1.0, 0.9, 0.7) * sun_spec;
for (var step = 1u; step <= 8u; step++) {
let march_pos = water_pos + refl_dir * (f32(step) * 2.2);
let clip = frame.projection * (frame.view * vec4(march_pos, 1.0));
if clip.w > 0.0 {
let ndc = clip.xyz / clip.w;
let sample_uv = vec2(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5);
if all(sample_uv >= vec2(0.01)) && all(sample_uv <= vec2(0.99)) {
let scene_d = opaque_depth_at(sample_uv);
if !is_sky(scene_d) && ndc.z <= scene_d && (scene_d - ndc.z) < 0.08 {
refl_color = textureSampleLevel(previous, linear_sampler, sample_uv, 0.0).rgb;
break;
}
}
}
}
color = mix(color, refl_color, fresnel * 0.55);
return vec4(color, 1.0);
}