Skip to content
Merged
9 changes: 9 additions & 0 deletions aravath_runtime/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,7 @@ fn light_the_world(renderer: &mut Renderer, volumetrics: bool) {
// map. The default strength darkens that soft lobe so it reads on the ground.
renderer.set_cloud_shadows_enabled(true);
renderer.set_cloud_shadow_strength(astraweave_render::volumetric_clouds::CLOUD_SHADOW_STRENGTH);
renderer.set_atmosphere_enabled(astraweave_render::PresentationQuality::default().atmosphere());
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
renderer
.scene_environment_mut()
.set_biome(BiomeType::Grassland);
Expand Down Expand Up @@ -1436,6 +1437,14 @@ fn smoke_test(volumetrics: bool) -> anyhow::Result<()> {
Err(err) => return Err(err),
};
startup::stage("headless renderer ready");
let quality = astraweave_render::PresentationQuality::default();
renderer.set_atmosphere_enabled(quality.atmosphere());
if renderer.atmosphere_enabled() != quality.atmosphere() {
anyhow::bail!(
"atmosphere flag does not follow PresentationQuality. {}",
startup::log_locations()
);
}
if volumetrics {
startup::stage("volumetrics on");
renderer.set_volumetric_fog_config(
Expand Down
2 changes: 1 addition & 1 deletion astraweave-camera/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ pub use freefly::{CameraController, CameraMode, FreeFly};
pub use producer::CameraProducer;
pub use projection::Projection;
pub use render_view::RenderView;
pub use third_person::ThirdPersonCamera;
pub use third_person::{ThirdPersonCamera, GAME_ZFAR, GAME_ZNEAR};
10 changes: 8 additions & 2 deletions astraweave-camera/src/third_person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ use glam::{Mat4, Vec3};
use crate::freefly::FreeFly;
use crate::{CameraProducer, Projection, RenderView};

/// Near plane of the Aravath third-person camera. The heavy-scene budget uses it.
pub const GAME_ZNEAR: f32 = 0.1;

/// Far plane of the Aravath third-person camera. The heavy-scene budget uses it.
pub const GAME_ZFAR: f32 = 500.0;

/// Follow camera orbiting a pivot (usually the character center).
pub struct ThirdPersonCamera {
pub pivot: Vec3,
Expand All @@ -33,8 +39,8 @@ impl ThirdPersonCamera {
distance: 6.0,
fovy: 60_f32.to_radians(),
aspect: 16.0 / 9.0,
znear: 0.1,
zfar: 500.0,
znear: GAME_ZNEAR,
zfar: GAME_ZFAR,
}
}

Expand Down
17 changes: 11 additions & 6 deletions astraweave-render/shaders/atmosphere/aerial_perspective.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct AerialParams {
@group(0) @binding(3) var t_transmittance: texture_2d<f32>; // atmosphere LUT
@group(0) @binding(4) var s_linear: sampler;
@group(0) @binding(5) var t_output: texture_storage_2d<rgba16float, write>;
@group(0) @binding(6) var t_sky: texture_2d<f32>;

// PI, TWO_PI, HALF_PI, INV_PI provided by constants.wgsl (prepended on Rust side).
const AERIAL_STEPS: u32 = 16u;
Expand Down Expand Up @@ -76,7 +77,8 @@ fn linearize_depth(d: f32) -> f32 {
}

fn reconstruct_world_pos(uv: vec2<f32>, depth: f32) -> vec3<f32> {
let ndc = vec4<f32>(uv * 2.0 - 1.0, depth, 1.0);
// uv.y 0 is the top of the texture. This view is Y-up, so NDC y is +1 there.
let ndc = vec4<f32>(uv.x * 2.0 - 1.0, 1.0 - uv.y * 2.0, depth, 1.0);
let world_h = params.inv_view_proj * ndc;
return world_h.xyz / world_h.w;
}
Expand All @@ -97,9 +99,11 @@ fn aerial_perspective_main(@builtin(global_invocation_id) gid: vec3<u32>) {
let scene_color = textureSampleLevel(t_scene, s_linear, uv, 0.0).rgb;
let depth_raw = textureLoad(t_depth, pixel, 0);

// Sky pixels: pass through unchanged (already rendered by sky pass)
if (depth_raw >= 0.9999) {
textureStore(t_output, pixel, vec4<f32>(scene_color, 1.0));
// Only the cleared far plane is sky. A looser cutoff treats distant
// geometry (about 333 m at zfar 500) as sky.
if (depth_raw == 1.0) {
let sky = textureLoad(t_sky, pixel, 0);
textureStore(t_output, pixel, vec4<f32>(sky.rgb, 1.0));
Comment on lines +104 to +106

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve transparent water when replacing sky pixels

When atmosphere is enabled and water is visible over the far-plane clear, this branch replaces the already-rendered water with t_sky. renderer.rs:8067-8077 records run_water_pass before Bruneton, while water.rs:584 disables water depth writes, so those water pixels still have depth 1.0; the previous shader preserved scene_color for them. Record Bruneton before transparent water/overlays or otherwise preserve their color.

Useful? React with 👍 / 👎.

return;
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
}

Expand All @@ -108,10 +112,11 @@ fn aerial_perspective_main(@builtin(global_invocation_id) gid: vec3<u32>) {
let distance_m = length(world_pos - params.view_pos);
let distance_km = distance_m * 0.001;

// Clamp integration distance
// Clamp integration distance. A max of 0 keeps the scene color: fog and
// god rays already scattered this pixel, and a second integral would
// scatter it again. Sky pixels returned above.
let max_dist = min(distance_km, params.max_distance);

// Short-circuit for very close geometry
if (max_dist < 0.001) {
textureStore(t_output, pixel, vec4<f32>(scene_color, 1.0));
return;
Expand Down
3 changes: 2 additions & 1 deletion astraweave-render/shaders/atmosphere/sky_render.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ fn ray_sphere_dist(origin_h: f32, cos_zenith: f32, sphere_r: f32) -> f32 {

fn pixel_to_ray(pixel: vec2<f32>) -> vec3<f32> {
let uv = pixel * params.inv_resolution;
let ndc = vec4<f32>(uv * 2.0 - 1.0, 1.0, 1.0);
// uv.y 0 is the top of the texture. This view is Y-up, so NDC y is +1 there.
let ndc = vec4<f32>(uv.x * 2.0 - 1.0, 1.0 - uv.y * 2.0, 1.0, 1.0);
let world_h = params.inv_view_proj * ndc;
let world_pos = world_h.xyz / world_h.w;
return normalize(world_pos - params.view_pos);
Expand Down
7 changes: 6 additions & 1 deletion astraweave-render/shaders/volumetrics/fog_density.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ fn fbm(p: vec3<f32>) -> f32 {
}

// ---- Froxel world position ----
// uv.y 0 is the top of the texture. NDC y is +1 there. Same pair as pbr.wgsl.

fn screen_uv_to_ndc(uv: vec2<f32>) -> vec2<f32> {
return vec2<f32>(uv.x * 2.0 - 1.0, 1.0 - uv.y * 2.0);
}

fn froxel_to_world(froxel: vec3<u32>) -> vec3<f32> {
let dims = vec3<f32>(params.froxel_dims);
Expand All @@ -124,7 +129,7 @@ fn froxel_to_world(froxel: vec3<u32>) -> vec3<f32> {
let depth = z_near * pow(z_far / z_near, linear_t);

// Unproject screen UV + depth to world space
let ndc_xy = screen_uv * 2.0 - 1.0;
let ndc_xy = screen_uv_to_ndc(screen_uv);
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
// Convert linear depth to NDC depth (reverse-Z or standard)
let ndc_z = (z_far * (depth - z_near)) / (depth * (z_far - z_near));

Expand Down
8 changes: 7 additions & 1 deletion astraweave-render/shaders/volumetrics/scatter.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,20 @@ fn phase_hg(cos_theta: f32, g: f32) -> f32 {
}

// ---- Froxel → world position (same exponential distribution as density pass) ----
// uv.y 0 is the top of the texture. NDC y is +1 there. Same pair as pbr.wgsl.

fn screen_uv_to_ndc(uv: vec2<f32>) -> vec2<f32> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

screen_uv_to_ndc here is a second copy of the function in fog_density.wgsl. The new pixel assert does not read this copy.

integrate.wgsl samples the scatter volume with the screen UV as the froxel xy. The density in that voxel was written by fog_density.wgsl. scatter_main multiplies that density by the phase and the shadow, and it also adds ambient_color * density * 0.25, which does not use world_pos. A mirrored ndc.y in this file moves the phase and the shadow sample. The ambient term still follows fog_density.wgsl, so fog_lower > fog_upper + 8 still passes.

The two functions match today (ndc.y = 1 - 2*uv.y). Nothing compares the source. ndc_to_screen_uv in god_rays.rs is the CPU pair and does not compile this shader. A source compare of the two WGSL functions would fail a mirrored copy without a device.

return vec2<f32>(uv.x * 2.0 - 1.0, 1.0 - uv.y * 2.0);
}

fn froxel_to_world(froxel: vec3<u32>) -> vec3<f32> {
let dims = vec3<f32>(params.froxel_dims);
let uvw = (vec3<f32>(froxel) + 0.5) / dims;
let screen_uv = uvw.xy;
let z_near = params.near_plane;
let z_far = params.far_plane;
let depth = z_near * pow(z_far / z_near, uvw.z);
let ndc_xy = screen_uv * 2.0 - 1.0;
let ndc_xy = screen_uv_to_ndc(screen_uv);
let ndc_z = (z_far * (depth - z_near)) / (depth * (z_far - z_near));
let clip = vec4<f32>(ndc_xy, ndc_z, 1.0);
let world_h = params.inv_view_proj * clip;
Expand Down
28 changes: 27 additions & 1 deletion astraweave-render/src/atmosphere.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ impl AtmospherePass {
bgl_texture_2d(3), // transmittance LUT
bgl_sampler(4),
bgl_storage_2d_rw(5, fmt),
bgl_texture_load(6), // Bruneton sky, textureLoad
],
});

Expand Down Expand Up @@ -467,6 +468,7 @@ impl AtmospherePass {
moon_dir: Vec3,
near: f32,
far: f32,
scatter_geometry: bool,
) {
let c = &self.config;

Expand Down Expand Up @@ -540,7 +542,14 @@ impl AtmospherePass {
sun_dir: sun_dir.to_array(),
sun_intensity: c.sun_intensity,
sun_color: c.sun_color.to_array(),
max_distance: c.max_aerial_distance,
// Fog and god rays already scattered the scene into HDR. A second
// integral would scatter those pixels again, so geometry keeps
// that color. Sky pixels still take the Bruneton sky.
max_distance: if scatter_geometry {
c.max_aerial_distance
} else {
0.0
},
resolution: [self.width as f32, self.height as f32],
inv_resolution: [1.0 / self.width as f32, 1.0 / self.height as f32],
near_plane: near,
Expand Down Expand Up @@ -686,6 +695,10 @@ impl AtmospherePass {
binding: 5,
resource: wgpu::BindingResource::TextureView(&self.aerial_view),
},
wgpu::BindGroupEntry {
binding: 6,
resource: wgpu::BindingResource::TextureView(&self.sky_view),
},
],
});
self.cached_aerial_bg =
Expand Down Expand Up @@ -773,6 +786,19 @@ fn bgl_depth(binding: u32) -> wgpu::BindGroupLayoutEntry {
}
}

fn bgl_texture_load(binding: u32) -> wgpu::BindGroupLayoutEntry {
wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: false },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
}
}

fn bgl_texture_2d(binding: u32) -> wgpu::BindGroupLayoutEntry {
wgpu::BindGroupLayoutEntry {
binding,
Expand Down
97 changes: 84 additions & 13 deletions astraweave-render/src/bin/gpu_frame_budget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
//! ```text
//! cargo run -p astraweave-render --release --features gpu-frame-budget --bin gpu_frame_budget -- --scene heavy --debug-timestamps --glb "assets/The Complete KayKit Collection v4/KayKit Adventurers 2.0/Characters/gltf/Rogue.glb"
//!
//! An enabled gated flag is rejected, including `--atmosphere`.
//! `--flag=false` and `--flag false` are off and do not bail. The printed
//! `median_gpu_frame_ms` is the baseline frame. No gated cell is filled here.
//! `--atmosphere` is the one gated flag that draws a frame. It replaces
//! `SkyRenderer`, keeps the HDR and depth clear, and turns on the same fog
//! and god-ray path Aravath uses. The printed median is that whole frame.
//! `--atmosphere false` is off. `--ssgi`, `--taa`, `--upscale`, `--dof`,
//! and `--clouds` are rejected when enabled.
//! ```

use anyhow::Context;
Expand Down Expand Up @@ -36,7 +38,7 @@ const SAMPLES: usize = 120;
fn main() -> anyhow::Result<()> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn")).init();
let args: Vec<String> = std::env::args().skip(1).collect();
astraweave_render::budget_gate::reject_gated_flags(&args)?;
let atmosphere = astraweave_render::budget_gate::reject_gated_flags(&args)?;
let glb = glb_path();
let heavy = scene_is_heavy();
let debug_timestamps = debug_timestamps();
Expand Down Expand Up @@ -148,13 +150,38 @@ fn main() -> anyhow::Result<()> {
add_heavy_terrain(&mut renderer)?;
renderer.set_particle_emitter(Some(emitter()))?;
// Cloud shadows stay at the renderer default (on). That is
// `CloudShadowPass`, not `VolumetricCloudsPass`. Volumetric fog, god
// rays, and every gated presentation pass stay off. GPU indirect culling
// is not the draw path on any adapter: each model still binds its own
// group 3, so one indirect buffer cannot select those groups. The CPU
// frustum remains the filter.
println!("gated_atmosphere=off");
println!("gated_measure=baseline");
// `CloudShadowPass`, not `VolumetricCloudsPass`. `--atmosphere` writes
// the Bruneton sky first, then Aravath's fog and god rays composite
// over it. A failed fog or shaft build is not that frame.
// GPU indirect culling is not the draw path on any adapter: each model
// still binds its own group 3, so one indirect buffer cannot select
// those groups. The CPU frustum remains the filter.
if atmosphere {
{
let clock = renderer.time_of_day_mut();
clock.current_time = 16.4;
clock.time_scale = 0.0;
}
renderer.set_volumetric_fog_config(
astraweave_render::volumetric_fog::VolumetricFogPass::gameplay_config(),
);
renderer.set_god_ray_config(astraweave_render::GodRayConfig {
enabled: true,
half_res: true,
num_samples: 32,
exposure: 0.22,
..astraweave_render::GodRayConfig::default()
});
renderer.set_atmosphere_enabled(true);
}
let fog = renderer.volumetric_fog_enabled();
let shafts = renderer.god_rays_enabled();
let measure = gated_measure_label(atmosphere, fog, shafts);
println!("gated_atmosphere={}", if atmosphere { "on" } else { "off" });
println!("gated_measure={measure}");
if atmosphere && (!fog || !shafts) {
anyhow::bail!("degraded atmosphere measurement: {measure}");
}
let gpu_cull = "cpu_fallback";

let device = renderer.device().clone();
Expand Down Expand Up @@ -777,6 +804,18 @@ fn renderer_limits(limits: &wgpu::Limits) -> [(&'static str, u64); 15] {
]
}

fn gated_measure_label(atmosphere: bool, fog: bool, shafts: bool) -> &'static str {
if !atmosphere {
return "baseline";
}
match (fog, shafts) {
(true, true) => "atmosphere_with_aravath_fog",
(true, false) => "atmosphere_without_shafts",
(false, true) => "atmosphere_without_fog",
(false, false) => "atmosphere_without_volumetrics",
}
}

fn debug_timestamps() -> bool {
std::env::args().any(|arg| arg == "--debug-timestamps" || arg == "--debug-timestamps=true")
}
Expand Down Expand Up @@ -888,8 +927,8 @@ fn fixed_camera() -> RenderView {
let projection = Projection::perspective(
60_f32.to_radians(),
WIDTH as f32 / HEIGHT as f32,
0.1,
200.0,
astraweave_camera::GAME_ZNEAR,
astraweave_camera::GAME_ZFAR,
);
RenderView::new(
view,
Expand All @@ -908,3 +947,35 @@ fn median(values: &mut [f64]) -> f64 {
values[mid]
}
}

#[cfg(test)]
mod measure_label_tests {
use super::gated_measure_label;

#[test]
fn degraded_volumetrics_are_not_the_aravath_fog_frame() {
assert_eq!(gated_measure_label(false, false, false), "baseline");
assert_eq!(
gated_measure_label(true, true, true),
"atmosphere_with_aravath_fog"
);
assert_eq!(
gated_measure_label(true, false, true),
"atmosphere_without_fog"
);
assert_eq!(
gated_measure_label(true, true, false),
"atmosphere_without_shafts"
);
assert_eq!(
gated_measure_label(true, false, false),
"atmosphere_without_volumetrics"
);
for (fog, shafts) in [(false, true), (true, false), (false, false)] {
assert_ne!(
gated_measure_label(true, fog, shafts),
"atmosphere_with_aravath_fog"
);
}
}
}
Loading
Loading