Merge origin/main into feat/vanilla-menus
Conflict: #1237's weather now returns its own vertices; the title draws none (the live title backdrop is gone). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -11,6 +11,7 @@ fn bits(value: f32) -> String {
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let args: Vec<_> = std::env::args().collect();
|
||||
let mut output = String::new();
|
||||
let mut parameters = weather::ParameterCache::default();
|
||||
for (case, line) in fs::read_to_string(&args[1])?.lines().enumerate() {
|
||||
let p: Vec<&str> = line.split_whitespace().collect();
|
||||
let int = |i: usize| p[i].parse::<i32>().unwrap();
|
||||
@@ -38,6 +39,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
bits(column.v_offset),
|
||||
column.light_coords
|
||||
)?;
|
||||
assert_eq!(
|
||||
parameters
|
||||
.get(x, z)
|
||||
.column(kind != 0, ticks, x, bottom, top, z, light, partial),
|
||||
column,
|
||||
"cached parameters differ from original random source at case {case}"
|
||||
);
|
||||
let max_alpha = if kind == 0 { 1.0 } else { 0.8 };
|
||||
for v in weather::column_quad(&column, camera, max_alpha, radius, intensity) {
|
||||
write!(
|
||||
|
||||
@@ -43,6 +43,7 @@ struct Out {
|
||||
@location(8) back_color: vec4<f32>,
|
||||
// entity.vsh's lightMapColor, multiplied in after the overlay.
|
||||
@location(9) light_color: vec4<f32>,
|
||||
@location(10) @interpolate(flat) weather_rect: vec4<f32>,
|
||||
}
|
||||
// Shipped light.glsl; normals are already transformed and RGBA8_SNORM packed.
|
||||
fn entity_light(normal: vec3<f32>, nether: bool) -> f32 {
|
||||
@@ -216,3 +217,135 @@ fn linear_fog(d: f32, start: f32, end: f32) -> f32 {
|
||||
@fragment fn fs_water_mask() -> @location(0) vec4<f32> {
|
||||
return vec4(0.0);
|
||||
}
|
||||
|
||||
// WEATHER_MATH_BEGIN
|
||||
// Unsigned 64-bit helpers (low word first). Snow adds a random double to a
|
||||
// float drift before casting to float. Retaining the 53-bit base avoids early
|
||||
// rounding, including ties, without requiring optional GPU float64 support.
|
||||
fn weather_shr_jam(v: vec2<u32>, n: u32) -> vec2<u32> {
|
||||
if n == 0u { return v; }
|
||||
if n < 32u {
|
||||
return vec2((v.x >> n) | (v.y << (32u - n)) | select(0u, 1u, (v.x << (32u - n)) != 0u), v.y >> n);
|
||||
}
|
||||
if n == 32u { return vec2(v.y | select(0u, 1u, v.x != 0u), 0u); }
|
||||
if n < 64u {
|
||||
return vec2((v.y >> (n - 32u)) | select(0u, 1u, v.x != 0u || (v.y << (64u - n)) != 0u), 0u);
|
||||
}
|
||||
return vec2(select(0u, 1u, any(v != vec2(0u))), 0u);
|
||||
}
|
||||
fn weather_add64(a: vec2<u32>, b: vec2<u32>) -> vec2<u32> {
|
||||
let lo = a.x + b.x;
|
||||
return vec2(lo, a.y + b.y + select(0u, 1u, lo < a.x));
|
||||
}
|
||||
fn weather_sub64(a: vec2<u32>, b: vec2<u32>) -> vec2<u32> {
|
||||
return vec2(a.x - b.x, a.y - b.y - select(0u, 1u, a.x < b.x));
|
||||
}
|
||||
fn weather_double_plus_float(base: vec2<u32>, delta: f32) -> f32 {
|
||||
// The random base is nonnegative and either zero or >= 2^-53.
|
||||
if all(base == vec2(0u)) { return select(delta, 0.0, delta == 0.0); }
|
||||
let db = bitcast<u32>(delta);
|
||||
let mag = db & 0x7fffffffu;
|
||||
let be = i32((base.y >> 20u) & 2047u) - 1023;
|
||||
var de = i32(mag >> 23u) - 127;
|
||||
var dm = (mag & 0x7fffffu) | 0x800000u;
|
||||
if mag < 0x800000u {
|
||||
dm = mag;
|
||||
if dm == 0u { de = -200; }
|
||||
else {
|
||||
let shift = countLeadingZeros(dm) - 8u;
|
||||
dm <<= shift;
|
||||
de = -126 - i32(shift);
|
||||
}
|
||||
}
|
||||
let e = max(be, de);
|
||||
// Leading significand bit at 61, with room for addition and guard bits.
|
||||
var a = vec2(base.x << 9u, ((base.y & 0xfffffu) | 0x100000u) << 9u | (base.x >> 23u));
|
||||
var b = vec2(0u, dm << 6u);
|
||||
a = weather_shr_jam(a, u32(e - be));
|
||||
b = weather_shr_jam(b, u32(e - de));
|
||||
var sign = 0u;
|
||||
var value: vec2<u32>;
|
||||
if (db >> 31u) == 0u { value = weather_add64(a, b); }
|
||||
else if a.y > b.y || (a.y == b.y && a.x >= b.x) { value = weather_sub64(a, b); }
|
||||
else { value = weather_sub64(b, a); sign = 0x80000000u; }
|
||||
if all(value == vec2(0u)) { return 0.0; }
|
||||
let leading = select(31u - countLeadingZeros(value.x), 63u - countLeadingZeros(value.y), value.y != 0u);
|
||||
var exponent = e + i32(leading) - 61;
|
||||
// Normalize the leading bit to 62. After jamming, bit 38 is the guard
|
||||
// bit and bits 0..37 distinguish a tie from a value above the midpoint.
|
||||
let shift = 62u - leading;
|
||||
if shift >= 32u { value = vec2(0u, value.x << (shift - 32u)); }
|
||||
else if shift != 0u { value = vec2(value.x << shift, (value.y << shift) | (value.x >> (32u - shift))); }
|
||||
// Vanilla first rounds the addition to double, then casts to float.
|
||||
// Round at bit 10 (53 retained bits) before the 24-bit rounding below;
|
||||
// otherwise a double-rounding midpoint can select the wrong float.
|
||||
let double_guard = (value.x & 512u) != 0u;
|
||||
let double_sticky = (value.x & 511u) != 0u;
|
||||
let double_odd = (value.x & 1024u) != 0u;
|
||||
value.x &= 0xfffffc00u;
|
||||
if double_guard && (double_sticky || double_odd) {
|
||||
value = weather_add64(value, vec2(1024u, 0u));
|
||||
}
|
||||
if (value.y & 0x80000000u) != 0u {
|
||||
value = weather_shr_jam(value, 1u);
|
||||
exponent += 1;
|
||||
}
|
||||
var mantissa = value.y >> 7u;
|
||||
let guard = (value.y & 64u) != 0u;
|
||||
let sticky = (value.y & 63u) != 0u || value.x != 0u;
|
||||
if guard && (sticky || (mantissa & 1u) != 0u) { mantissa += 1u; }
|
||||
if mantissa >= 0x1000000u { mantissa >>= 1u; exponent += 1; }
|
||||
return bitcast<f32>(sign | (u32(exponent + 127) << 23u) | (mantissa & 0x7fffffu));
|
||||
}
|
||||
fn weather_phase(animation: vec4<f32>, bases: vec4<u32>, snow: u32, time: vec4<f32>) -> vec2<f32> {
|
||||
if snow == 0u {
|
||||
let ticks = time.x + animation.y;
|
||||
let t = ticks + time.z;
|
||||
let step = -t / 32.0;
|
||||
let offset = step * animation.x;
|
||||
return vec2(0.0, offset % 32.0);
|
||||
}
|
||||
let t = time.x + time.z;
|
||||
let u_time = t * 0.01;
|
||||
let du = u_time * animation.z;
|
||||
let v_time = t * animation.w;
|
||||
let dv = v_time * 0.001;
|
||||
let u = weather_double_plus_float(bases.xy, du);
|
||||
let v = weather_double_plus_float(bases.zw, dv);
|
||||
let fall_time = time.y + time.z;
|
||||
let fall = -fall_time / 512.0;
|
||||
return vec2(u, fall + v);
|
||||
}
|
||||
// Atlas sampler is nearest with no mipmaps. Clamp the integer texel inside
|
||||
// the sprite even when a fractional coordinate rounds up at its far edge.
|
||||
fn weather_texel(uv: vec2<f32>, rect: vec4<f32>, dimensions: vec2<u32>) -> vec2<i32> {
|
||||
let size = vec2<f32>(dimensions);
|
||||
let origin = vec2<i32>(round(rect.xy * size));
|
||||
let extent = max(vec2<i32>(round(rect.zw * size)), vec2(1));
|
||||
let local = min(vec2<i32>(floor(fract(uv) * vec2<f32>(extent))), extent - vec2(1));
|
||||
return origin + local;
|
||||
}
|
||||
// WEATHER_MATH_END
|
||||
|
||||
struct WeatherTime { values: vec4<f32> }
|
||||
@group(1) @binding(1) var<uniform> weather_time: WeatherTime;
|
||||
@vertex fn vs_weather(@location(0) position: vec3<f32>, @location(1) uv: vec2<f32>,
|
||||
@location(2) rect: vec4<f32>, @location(3) animation: vec4<f32>,
|
||||
@location(4) bases: vec4<u32>, @location(5) color: vec4<f32>,
|
||||
@location(6) light: vec4<u32>, @location(7) snow: u32) -> Out {
|
||||
let phase = weather_phase(animation, bases, snow, weather_time.values);
|
||||
var out = entity_vertex(position, uv + phase, 0u, color, light, vec4(0.0));
|
||||
out.weather_rect = rect;
|
||||
return out;
|
||||
}
|
||||
@fragment fn fs_weather(in: Out) -> @location(0) vec4<f32> {
|
||||
// Repeat within this sprite, never across the atlas. The atlas uses nearest
|
||||
// filtering and no mipmaps, so fractional UVs have no seam derivatives.
|
||||
let texel = weather_texel(in.uv, in.weather_rect, textureDimensions(atlas));
|
||||
var color = textureLoad(atlas, texel, 0) * in.color;
|
||||
if color.a < 0.1 { discard; }
|
||||
color *= in.light_color;
|
||||
let f = max(linear_fog(in.spherical, globals.fog.x, globals.fog.y),
|
||||
linear_fog(in.cylindrical, globals.fog.z, globals.fog.w));
|
||||
return vec4(mix(color.rgb, globals.fog_color.rgb, f * globals.fog_color.a), color.a);
|
||||
}
|
||||
|
||||
@@ -1114,6 +1114,7 @@ struct Game<'a> {
|
||||
/// `FogRenderer`'s evolving state.
|
||||
fog: horizon_client::fog::FogState,
|
||||
screen_fx: horizon_client::screen_effects::ScreenEffects,
|
||||
weather_parameters: horizon_client::weather::ParameterCache,
|
||||
panorama_rotation: horizon_client::gfx::panorama::Rotation,
|
||||
world: World,
|
||||
streaming: Streaming,
|
||||
@@ -8944,89 +8945,84 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
/// WeatherEffectRenderer: rain and snow columns within the weather radius.
|
||||
fn weather_geometry(&self, camera: glam::DVec3, alpha: f64, out: &mut Vec<Vertex>) {
|
||||
fn weather_geometry(
|
||||
&mut self,
|
||||
camera: glam::DVec3,
|
||||
alpha: f64,
|
||||
) -> Vec<horizon_client::weather::Vertex> {
|
||||
use horizon_client::weather;
|
||||
let (intensity, _) = self.world.weather.levels(alpha as f32);
|
||||
if intensity <= 0.0 {
|
||||
return;
|
||||
return Vec::new();
|
||||
}
|
||||
let atlas = &self.gui.atlas;
|
||||
let (Some(rain_tex), Some(snow_tex)) = (
|
||||
atlas.sprites.get("environment/rain").copied(),
|
||||
atlas.sprites.get("environment/snow").copied(),
|
||||
) else {
|
||||
return;
|
||||
return Vec::new();
|
||||
};
|
||||
let (cx, cy, cz) = (
|
||||
camera.x.floor() as i32,
|
||||
camera.y.floor() as i32,
|
||||
camera.z.floor() as i32,
|
||||
);
|
||||
let radius = self.settings.weather_radius;
|
||||
let ticks = self.world.game_time;
|
||||
let partial = alpha as f32;
|
||||
let eye = [camera.x, camera.y, camera.z];
|
||||
// Vanilla draws every rain column before the snow ones.
|
||||
let mut columns = (Vec::new(), Vec::new());
|
||||
for z in cz - radius..=cz + radius {
|
||||
for x in cx - radius..=cx + radius {
|
||||
// The 32x32 direction table supports at most a 15-block radius.
|
||||
let radius = self.settings.weather_radius.clamp(0, 15);
|
||||
let eye = camera.to_array();
|
||||
let mut rain = Vec::new();
|
||||
let mut snow_vertices = Vec::new();
|
||||
// Saturation keeps extreme camera coordinates from overflowing the loop.
|
||||
for z in cz.saturating_sub(radius)..=cz.saturating_add(radius) {
|
||||
for x in cx.saturating_sub(radius)..=cx.saturating_add(radius) {
|
||||
let top = self.rain_height(x, z);
|
||||
let y0 = (cy - radius).max(top);
|
||||
let y1 = (cy + radius).max(top);
|
||||
let y0 = cy.saturating_sub(radius).max(top);
|
||||
let y1 = cy.saturating_add(radius).max(top);
|
||||
if y1 == y0 {
|
||||
continue;
|
||||
}
|
||||
let Some(snow) = self.precipitation(x, cy, z) else {
|
||||
continue;
|
||||
};
|
||||
let mut random = weather::column_random(x, z);
|
||||
let parameters = self.weather_parameters.get(x, z);
|
||||
let light = self.world.light(x, top.max(cy), z);
|
||||
let coords = i32::from(light & 15) << 4 | i32::from(light >> 4) << 20;
|
||||
if snow {
|
||||
columns.1.push(weather::snow_column(
|
||||
&mut random,
|
||||
ticks,
|
||||
x,
|
||||
y0,
|
||||
y1,
|
||||
z,
|
||||
coords,
|
||||
partial,
|
||||
));
|
||||
// Only light brightening is needed here; UV phases stay zero.
|
||||
let lc = if snow {
|
||||
let (block, sky) = (coords >> 4 & 15, coords >> 20 & 15);
|
||||
((block * 3 + 15) / 4) << 4 | ((sky * 3 + 15) / 4) << 20
|
||||
} else {
|
||||
columns.0.push(weather::rain_column(
|
||||
&mut random,
|
||||
ticks,
|
||||
x,
|
||||
y0,
|
||||
y1,
|
||||
z,
|
||||
coords,
|
||||
partial,
|
||||
));
|
||||
coords
|
||||
};
|
||||
let column = weather::Column {
|
||||
x,
|
||||
z,
|
||||
bottom_y: y0,
|
||||
top_y: y1,
|
||||
u_offset: 0.0,
|
||||
v_offset: 0.0,
|
||||
light_coords: lc,
|
||||
};
|
||||
let r = if snow { snow_tex } else { rain_tex };
|
||||
let vertices = weather::vertices(
|
||||
&column,
|
||||
parameters,
|
||||
snow,
|
||||
eye,
|
||||
radius,
|
||||
intensity,
|
||||
[r.u0, r.v0, r.u1 - r.u0, r.v1 - r.v0],
|
||||
);
|
||||
if snow {
|
||||
snow_vertices.extend(vertices);
|
||||
} else {
|
||||
rain.extend(vertices);
|
||||
}
|
||||
}
|
||||
}
|
||||
let rain = columns.0.iter().map(|c| (c, 1.0, rain_tex));
|
||||
let snow = columns.1.iter().map(|c| (c, 0.8, snow_tex));
|
||||
for (column, max_alpha, r) in rain.chain(snow) {
|
||||
let quad = weather::column_quad(column, eye, max_alpha, radius, intensity);
|
||||
let color = quad[0].color;
|
||||
let lc = quad[0].light_coords;
|
||||
let light = ((lc >> 20 & 15) * 16) as u32 | (((lc >> 4 & 15) * 16) as u32) << 8;
|
||||
weather::tile(&quad, |pos, uvs| {
|
||||
for (p, [u, v]) in pos.into_iter().zip(uvs) {
|
||||
out.push(Vertex {
|
||||
normal: [0; 4],
|
||||
pos: p,
|
||||
uv: [r.u0 + (r.u1 - r.u0) * u, r.v0 + (r.v1 - r.v0) * v],
|
||||
layer: 0,
|
||||
color,
|
||||
light,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
// Vanilla draws every rain column before the snow ones.
|
||||
rain.extend(snow_vertices);
|
||||
rain
|
||||
}
|
||||
/// Player.startSleepInBed: night or thunder, no monsters within 8 blocks;
|
||||
/// the bed becomes the respawn point either way.
|
||||
@@ -9825,11 +9821,11 @@ impl Game<'_> {
|
||||
if !title {
|
||||
self.end_geometry(camera, &mut entities);
|
||||
}
|
||||
if !title && self.dim == Dim::Overworld && self.settings.horizon.weather {
|
||||
horizon_client::spans::time("weather", || {
|
||||
self.weather_geometry(camera, alpha, &mut entities)
|
||||
});
|
||||
}
|
||||
let weather = if !title && self.dim == Dim::Overworld && self.settings.horizon.weather {
|
||||
horizon_client::spans::time("weather", || self.weather_geometry(camera, alpha))
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let border = (!title)
|
||||
.then(|| self.border_draw(camera.to_array(), alpha as f32))
|
||||
.flatten();
|
||||
@@ -9840,6 +9836,8 @@ impl Game<'_> {
|
||||
self.gather_dynamic_lights(camera, alpha);
|
||||
}
|
||||
let frame = Frame {
|
||||
weather: &weather,
|
||||
weather_time: horizon_client::weather::Time::new(self.world.game_time, alpha as f32),
|
||||
shader: self.shader_inputs(camera, alpha as f32),
|
||||
nether_lighting: self.dim == Dim::Nether,
|
||||
camera,
|
||||
@@ -11413,6 +11411,7 @@ impl<'a> Game<'a> {
|
||||
gui,
|
||||
fog: Default::default(),
|
||||
screen_fx: Default::default(),
|
||||
weather_parameters: Default::default(),
|
||||
panorama_rotation: Default::default(),
|
||||
world,
|
||||
streaming,
|
||||
@@ -12321,6 +12320,8 @@ fn loading_renderer(
|
||||
/// A title-shaped frame: black clear and panorama, only the HUD is drawn.
|
||||
fn loading_frame(hud: &HudBatch) -> Frame<'_> {
|
||||
Frame {
|
||||
weather: &[],
|
||||
weather_time: horizon_client::weather::Time::new(0, 0.0),
|
||||
camera: glam::DVec3::new(0.5, 100.0, 0.5),
|
||||
yaw: 0.0,
|
||||
pitch: 10.0,
|
||||
|
||||
@@ -33,6 +33,7 @@ fn entity_shader() -> String {
|
||||
}
|
||||
|
||||
mod entity_batch;
|
||||
mod weather;
|
||||
use entity_batch::EntityBatch;
|
||||
pub use entity_batch::WATER_MASK_OVERLAY;
|
||||
|
||||
@@ -193,6 +194,8 @@ pub struct Frame<'a> {
|
||||
pub nether_lighting: bool,
|
||||
/// Camera-relative entity geometry textured from the shared atlas.
|
||||
pub entities: &'a [Vertex],
|
||||
pub weather: &'a [crate::weather::Vertex],
|
||||
pub weather_time: crate::weather::Time,
|
||||
/// Horizon Extras' dynamic lights, nearest first (`dynamic_light::select`); empty when off.
|
||||
pub dynamic_lights: &'a [[f32; 4]],
|
||||
/// Cloud mesh inputs; geometry and GPU storage are retained by the renderer.
|
||||
@@ -328,6 +331,7 @@ pub struct Renderer {
|
||||
crosshair_group: wgpu::BindGroup,
|
||||
crosshair_globals: wgpu::Buffer,
|
||||
entity_pipeline: wgpu::RenderPipeline,
|
||||
weather: weather::Weather,
|
||||
crumbling_pipeline: wgpu::RenderPipeline,
|
||||
/// `RenderPipelines.WATER_MASK`: depth writes only, no colour.
|
||||
water_mask_pipeline: wgpu::RenderPipeline,
|
||||
@@ -1450,6 +1454,12 @@ impl Renderer {
|
||||
),
|
||||
],
|
||||
});
|
||||
let weather = weather::Weather::new(
|
||||
&device,
|
||||
config.format.remove_srgb_suffix(),
|
||||
&entity_bindings,
|
||||
&entity_shader,
|
||||
);
|
||||
let atlas_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
||||
label: Some("Atlas"),
|
||||
..Default::default()
|
||||
@@ -2125,6 +2135,7 @@ impl Renderer {
|
||||
crosshair_group,
|
||||
crosshair_globals,
|
||||
entity_pipeline,
|
||||
weather,
|
||||
crumbling_pipeline,
|
||||
water_mask_pipeline,
|
||||
cloud_pipeline,
|
||||
@@ -3255,6 +3266,13 @@ impl Renderer {
|
||||
bytemuck::cast_slice(&all),
|
||||
);
|
||||
}
|
||||
self.weather.prepare(
|
||||
&self.device,
|
||||
&mut encoder,
|
||||
&mut self.uploads,
|
||||
frame.weather,
|
||||
frame.weather_time,
|
||||
);
|
||||
let outline_edges = frame.outline.len().min(MAX_OUTLINE_EDGES as usize);
|
||||
if outline_edges > 0 {
|
||||
let lines = outline_vertices(&frame.outline[..outline_edges], camera);
|
||||
@@ -3272,6 +3290,7 @@ impl Renderer {
|
||||
frame.hand,
|
||||
frame.see_through,
|
||||
);
|
||||
let weather_quads = frame.weather.len() as u32 / 4;
|
||||
let world_quads =
|
||||
entity_quads + crumbling_quads + translucent_crumbling_quads + water_mask_quads;
|
||||
let cloud_quads = if let Some(input) = &frame.clouds {
|
||||
@@ -3360,7 +3379,8 @@ impl Renderer {
|
||||
0
|
||||
} else {
|
||||
lightning::QUADS
|
||||
});
|
||||
})
|
||||
.max(weather_quads);
|
||||
if max_quads > self.index_quads {
|
||||
self.index_quads = max_quads.next_power_of_two();
|
||||
self.indices = Self::quad_indices(&self.device, self.index_quads);
|
||||
@@ -3648,6 +3668,8 @@ impl Renderer {
|
||||
pass.draw_indexed(0..entity_quads * 6, 0, 0..1);
|
||||
pass.set_bind_group(0, &self.group, &[]);
|
||||
}
|
||||
self.weather.draw(&mut pass, &self.entity_group);
|
||||
pass.set_bind_group(0, &self.group, &[]);
|
||||
if crumbling_quads > 0 {
|
||||
pass.set_pipeline(&self.crumbling_pipeline);
|
||||
pass.set_bind_group(0, &self.entity_group, &[]);
|
||||
|
||||
@@ -0,0 +1,330 @@
|
||||
//! Weather's unsplit quads, using the entity atlas/lightmap resources.
|
||||
use crate::weather::{Time, Vertex};
|
||||
|
||||
pub(super) struct Weather {
|
||||
pipeline: wgpu::RenderPipeline,
|
||||
group: wgpu::BindGroup,
|
||||
uniform: wgpu::Buffer,
|
||||
vertices: wgpu::Buffer,
|
||||
capacity: usize,
|
||||
count: u32,
|
||||
}
|
||||
impl Weather {
|
||||
pub fn new(
|
||||
device: &wgpu::Device,
|
||||
format: wgpu::TextureFormat,
|
||||
entities: &wgpu::BindGroupLayout,
|
||||
shader: &wgpu::ShaderModule,
|
||||
) -> Self {
|
||||
let bindings = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("Weather time"),
|
||||
entries: &[wgpu::BindGroupLayoutEntry {
|
||||
binding: 1,
|
||||
visibility: wgpu::ShaderStages::VERTEX,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Uniform,
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
}],
|
||||
});
|
||||
let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("Weather layout"),
|
||||
bind_group_layouts: &[Some(entities), Some(&bindings)],
|
||||
..Default::default()
|
||||
});
|
||||
let attributes = wgpu::vertex_attr_array![0=>Float32x3,1=>Float32x2,2=>Float32x4,
|
||||
3=>Float32x4,4=>Uint32x4,5=>Unorm8x4,6=>Uint8x4,7=>Uint32];
|
||||
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Weather"),
|
||||
layout: Some(&layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: shader,
|
||||
entry_point: Some("vs_weather"),
|
||||
compilation_options: Default::default(),
|
||||
buffers: &[Some(wgpu::VertexBufferLayout {
|
||||
array_stride: std::mem::size_of::<Vertex>() as u64,
|
||||
step_mode: wgpu::VertexStepMode::Vertex,
|
||||
attributes: &attributes,
|
||||
})],
|
||||
},
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
..Default::default()
|
||||
},
|
||||
depth_stencil: Some(wgpu::DepthStencilState {
|
||||
format: wgpu::TextureFormat::Depth32Float,
|
||||
depth_write_enabled: Some(true),
|
||||
depth_compare: Some(wgpu::CompareFunction::GreaterEqual),
|
||||
stencil: Default::default(),
|
||||
bias: Default::default(),
|
||||
}),
|
||||
multisample: Default::default(),
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: shader,
|
||||
entry_point: Some("fs_weather"),
|
||||
compilation_options: Default::default(),
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
format,
|
||||
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
}),
|
||||
multiview_mask: None,
|
||||
cache: None,
|
||||
});
|
||||
let uniform = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("Weather time"),
|
||||
size: 16,
|
||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("Weather time"),
|
||||
layout: &bindings,
|
||||
entries: &[wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: uniform.as_entire_binding(),
|
||||
}],
|
||||
});
|
||||
Self {
|
||||
pipeline,
|
||||
group,
|
||||
uniform,
|
||||
vertices: Self::buffer(device, 4),
|
||||
capacity: 4,
|
||||
count: 0,
|
||||
}
|
||||
}
|
||||
fn buffer(device: &wgpu::Device, capacity: usize) -> wgpu::Buffer {
|
||||
device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("Weather vertices"),
|
||||
size: (capacity * std::mem::size_of::<Vertex>()) as u64,
|
||||
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
})
|
||||
}
|
||||
pub fn prepare(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
uploads: &mut super::upload::Uploads,
|
||||
vertices: &[Vertex],
|
||||
time: Time,
|
||||
) {
|
||||
self.count = vertices.len() as u32;
|
||||
if vertices.is_empty() {
|
||||
return;
|
||||
}
|
||||
if vertices.len() > self.capacity {
|
||||
self.capacity = vertices.len().next_power_of_two();
|
||||
self.vertices = Self::buffer(device, self.capacity);
|
||||
}
|
||||
uploads.buffer(encoder, &self.vertices, 0, bytemuck::cast_slice(vertices));
|
||||
uploads.buffer(encoder, &self.uniform, 0, bytemuck::bytes_of(&time));
|
||||
}
|
||||
pub fn draw(&self, pass: &mut wgpu::RenderPass<'_>, entities: &wgpu::BindGroup) {
|
||||
if self.count == 0 {
|
||||
return;
|
||||
}
|
||||
pass.set_pipeline(&self.pipeline);
|
||||
pass.set_bind_group(0, entities, &[]);
|
||||
pass.set_bind_group(1, &self.group, &[]);
|
||||
pass.set_vertex_buffer(0, self.vertices.slice(..));
|
||||
// The renderer binds the shared triangle-list quad indices.
|
||||
pass.draw_indexed(0..self.count / 4 * 6, 0, 0..1);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::weather::{Parameters, Time};
|
||||
use wgpu::util::DeviceExt;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
struct Case {
|
||||
animation: [f32; 4],
|
||||
bases: [u32; 4],
|
||||
time: [f32; 4],
|
||||
uv: [f32; 2],
|
||||
snow: u32,
|
||||
_pad: u32,
|
||||
}
|
||||
|
||||
/// Execute the production WGSL math, rather than a Rust translation of it.
|
||||
/// Requires a GPU; run explicitly on renderer changes, not on headless CI.
|
||||
#[test]
|
||||
#[ignore = "requires a Vulkan/Metal/DX12 adapter"]
|
||||
fn weather_shader_uvs_match_legacy_bits() {
|
||||
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle());
|
||||
let adapter =
|
||||
pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default()))
|
||||
.unwrap();
|
||||
eprintln!("weather parity adapter: {:?}", adapter.get_info());
|
||||
let (device, queue) =
|
||||
pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default())).unwrap();
|
||||
let mut cases = Vec::new();
|
||||
let mut expected = Vec::new();
|
||||
for i in 0..4096i32 {
|
||||
let x = i.wrapping_mul(45_238_971);
|
||||
let z = i.wrapping_mul(-418_711).wrapping_add(i32::MIN);
|
||||
let p = Parameters::new(x, z);
|
||||
let bases = p.snow_base.map(f64::to_bits);
|
||||
for ticks in [-1, 0, 511, 512, 131_071, 131_072, i64::MAX] {
|
||||
let partial = [0.0, 0.5, 0.999, f32::from_bits(0x3f7fffff)][i as usize % 4];
|
||||
for snow in [false, true] {
|
||||
let column = p.column(snow, ticks, x, -64, 320, z, 0, partial);
|
||||
let uv = [1.0, if i & 1 == 0 { -16.0 } else { 80.0 }];
|
||||
cases.push(Case {
|
||||
animation: [
|
||||
p.rain_speed,
|
||||
p.rain_tick_offset,
|
||||
p.snow_drift[0],
|
||||
p.snow_drift[1],
|
||||
],
|
||||
bases: [
|
||||
bases[0] as u32,
|
||||
(bases[0] >> 32) as u32,
|
||||
bases[1] as u32,
|
||||
(bases[1] >> 32) as u32,
|
||||
],
|
||||
time: Time::new(ticks, partial).values,
|
||||
uv,
|
||||
snow: u32::from(snow),
|
||||
_pad: 0,
|
||||
});
|
||||
expected.push([
|
||||
(uv[0] + column.u_offset).to_bits(),
|
||||
(uv[1] + column.v_offset).to_bits(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Force halfway rounding, cancellation, zero, tiny bases and either
|
||||
// sign of drift. Gaussian seeds alone rarely land on exact midpoints.
|
||||
for base in [
|
||||
0.0,
|
||||
2.0f64.powi(-53),
|
||||
0.5,
|
||||
0.5 + 2.0f64.powi(-25),
|
||||
0.5 + 2.0f64.powi(-25) + 2.0f64.powi(-53),
|
||||
0.5 + 2.0f64.powi(-14) + 2.0f64.powi(-53),
|
||||
1.0 - 2.0f64.powi(-53),
|
||||
] {
|
||||
for delta in [0.0f32, -0.0, -0.5, 0.5, 1024.0, -1024.0, 2.0f32.powi(-40)] {
|
||||
// time=100, u scale=.01 -> delta exactly, v is unused.
|
||||
let bits = base.to_bits();
|
||||
cases.push(Case {
|
||||
animation: [3.0, 0.0, delta, 0.0],
|
||||
bases: [bits as u32, (bits >> 32) as u32, 0, 0],
|
||||
time: [100.0, 0.0, 0.0, 0.0],
|
||||
uv: [0.0, 0.0],
|
||||
snow: 1,
|
||||
_pad: 0,
|
||||
});
|
||||
expected.push([((base + f64::from(delta)) as f32).to_bits(), 0]);
|
||||
}
|
||||
}
|
||||
let expected: Vec<[u32; 4]> = expected
|
||||
.into_iter()
|
||||
.map(|bits| {
|
||||
let uv = bits.map(f32::from_bits);
|
||||
let texel = [
|
||||
256 + ((uv[0] - uv[0].floor()) * 16.0).floor() as u32,
|
||||
512 + ((uv[1] - uv[1].floor()) * 32.0).floor() as u32,
|
||||
];
|
||||
[bits[0], bits[1], texel[0], texel[1]]
|
||||
})
|
||||
.collect();
|
||||
let math = include_str!("../entity.wgsl")
|
||||
.split("// WEATHER_MATH_BEGIN")
|
||||
.nth(1)
|
||||
.unwrap()
|
||||
.split("// WEATHER_MATH_END")
|
||||
.next()
|
||||
.unwrap();
|
||||
let source = format!(
|
||||
"{math}\n{}",
|
||||
r#"
|
||||
struct Case { animation: vec4<f32>, bases: vec4<u32>, time: vec4<f32>, uv: vec2<f32>, snow: u32, pad: u32 }
|
||||
@group(0) @binding(0) var<storage, read> cases: array<Case>;
|
||||
@group(0) @binding(1) var<storage, read_write> results: array<vec4<u32>>;
|
||||
@compute @workgroup_size(64) fn main(@builtin(global_invocation_id) id: vec3<u32>) {
|
||||
if id.x >= arrayLength(&cases) { return; }
|
||||
let c = cases[id.x];
|
||||
let uv = c.uv + weather_phase(c.animation, c.bases, c.snow, c.time);
|
||||
let texel = weather_texel(uv, vec4(0.25, 0.5, 0.015625, 0.03125), vec2(1024u));
|
||||
results[id.x] = vec4(bitcast<vec2<u32>>(uv), vec2<u32>(texel));
|
||||
}"#
|
||||
);
|
||||
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||
label: Some("Weather parity"),
|
||||
source: wgpu::ShaderSource::Wgsl(source.into()),
|
||||
});
|
||||
let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
|
||||
label: Some("Weather parity"),
|
||||
layout: None,
|
||||
module: &shader,
|
||||
entry_point: Some("main"),
|
||||
compilation_options: Default::default(),
|
||||
cache: None,
|
||||
});
|
||||
let input = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: None,
|
||||
contents: bytemuck::cast_slice(&cases),
|
||||
usage: wgpu::BufferUsages::STORAGE,
|
||||
});
|
||||
let size = (expected.len() * 16) as u64;
|
||||
let output = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: None,
|
||||
size,
|
||||
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
let readback = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: None,
|
||||
size,
|
||||
usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: None,
|
||||
layout: &pipeline.get_bind_group_layout(0),
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: input.as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: output.as_entire_binding(),
|
||||
},
|
||||
],
|
||||
});
|
||||
let mut encoder = device.create_command_encoder(&Default::default());
|
||||
{
|
||||
let mut pass = encoder.begin_compute_pass(&Default::default());
|
||||
pass.set_pipeline(&pipeline);
|
||||
pass.set_bind_group(0, &group, &[]);
|
||||
pass.dispatch_workgroups((cases.len() as u32).div_ceil(64), 1, 1);
|
||||
}
|
||||
encoder.copy_buffer_to_buffer(&output, 0, &readback, 0, size);
|
||||
queue.submit([encoder.finish()]);
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
readback.map_async(wgpu::MapMode::Read, .., move |r| tx.send(r).unwrap());
|
||||
device.poll(wgpu::PollType::wait_indefinitely()).unwrap();
|
||||
rx.recv().unwrap().unwrap();
|
||||
let mapped = readback.get_mapped_range(..).unwrap();
|
||||
let actual: &[[u32; 4]] = bytemuck::cast_slice(&mapped);
|
||||
for (i, (actual, expected)) in actual.iter().zip(&expected).enumerate() {
|
||||
assert_eq!(
|
||||
actual, expected,
|
||||
"case {i}, snow={}, time={:?}, animation={:?}",
|
||||
cases[i].snow, cases[i].time, cases[i].animation
|
||||
);
|
||||
}
|
||||
eprintln!("{} GPU weather UV cases matched bit for bit", cases.len());
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,9 @@
|
||||
//! Vanilla draws each column as one camera-facing quad whose texture
|
||||
//! coordinates run past 0..1 (`v` advances a quarter of the texture per block,
|
||||
//! so `rain.png` repeats every 4 blocks) and relies on a repeating sampler.
|
||||
//! Horizon samples the sprite from its atlas, so [`tile`] splits a quad at
|
||||
//! every whole texture repeat into pieces that stay inside the sprite.
|
||||
//! The live renderer repeats UVs inside the atlas sprite in its shader, using
|
||||
//! cached coordinate-only [`Parameters`]. The reference [`tile`] path splits
|
||||
//! at whole repeats and remains available for CPU callers and parity probes.
|
||||
use horizon_core::random::{LegacyRandom, RandomSource};
|
||||
|
||||
/// `WeatherEffectRenderer.ColumnInstance`.
|
||||
@@ -47,6 +48,176 @@ pub fn column_random(x: i32, z: i32) -> LegacyRandom {
|
||||
LegacyRandom::new(i64::from(column_seed(x, z)))
|
||||
}
|
||||
|
||||
/// Coordinate-only random values. Keep the snow bases as doubles: rounding them
|
||||
/// before adding the float drift changes vanilla's UVs at rounding boundaries.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Parameters {
|
||||
pub rain_speed: f32,
|
||||
pub rain_tick_offset: f32,
|
||||
pub snow_base: [f64; 2],
|
||||
pub snow_drift: [f32; 2],
|
||||
}
|
||||
impl Parameters {
|
||||
pub fn new(x: i32, z: i32) -> Self {
|
||||
let rain_speed = 3.0 + column_random(x, z).next_float();
|
||||
let mut random = column_random(x, z);
|
||||
let u = random.next_double();
|
||||
let du = random.next_gaussian() as f32;
|
||||
let v = random.next_double();
|
||||
let dv = random.next_gaussian() as f32;
|
||||
Self {
|
||||
rain_speed,
|
||||
rain_tick_offset: (x
|
||||
.wrapping_mul(x)
|
||||
.wrapping_mul(3121)
|
||||
.wrapping_add(x.wrapping_mul(45_238_971))
|
||||
.wrapping_add(z.wrapping_mul(z).wrapping_mul(418_711))
|
||||
.wrapping_add(z.wrapping_mul(13761))
|
||||
& 255) as f32,
|
||||
snow_base: [u, v],
|
||||
snow_drift: [du, dv],
|
||||
}
|
||||
}
|
||||
|
||||
/// CPU reference for fixtures and GPU differential tests. The public random
|
||||
/// source helpers below retain their original draw/advancement semantics.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn column(
|
||||
self,
|
||||
snow: bool,
|
||||
ticks: i64,
|
||||
x: i32,
|
||||
bottom_y: i32,
|
||||
top_y: i32,
|
||||
z: i32,
|
||||
light_coords: i32,
|
||||
partial: f32,
|
||||
) -> Column {
|
||||
let time = (ticks & 131_071) as f32 + partial;
|
||||
let (u_offset, v_offset, light_coords) = if snow {
|
||||
let u = (self.snow_base[0] + f64::from(time * 0.01 * self.snow_drift[0])) as f32;
|
||||
let v = (self.snow_base[1] + f64::from(time * self.snow_drift[1] * 0.001)) as f32;
|
||||
let fall = -((ticks & 511) as f32 + partial) / 512.0;
|
||||
let (block, sky) = (light_coords >> 4 & 15, light_coords >> 20 & 15);
|
||||
(
|
||||
u,
|
||||
fall + v,
|
||||
((block * 3 + 15) / 4) << 4 | ((sky * 3 + 15) / 4) << 20,
|
||||
)
|
||||
} else {
|
||||
let offset = -(((ticks & 131_071) as f32 + self.rain_tick_offset) + partial) / 32.0
|
||||
* self.rain_speed;
|
||||
(0.0, offset % 32.0, light_coords)
|
||||
};
|
||||
Column {
|
||||
x,
|
||||
z,
|
||||
bottom_y,
|
||||
top_y,
|
||||
u_offset,
|
||||
v_offset,
|
||||
light_coords,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Direct-mapped 32x32 cache: at most 1024 coordinate keys, including after
|
||||
/// teleports or radius changes. Full keys disambiguate collisions and wrapping.
|
||||
/// Nothing world-dependent is cached, so dimension/biome/pack changes need no
|
||||
/// invalidation; the caller still samples heights, precipitation and light.
|
||||
pub struct ParameterCache {
|
||||
entries: Box<[Option<((i32, i32), Parameters)>]>,
|
||||
}
|
||||
impl Default for ParameterCache {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
entries: vec![None; 32 * 32].into_boxed_slice(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl ParameterCache {
|
||||
pub fn get(&mut self, x: i32, z: i32) -> Parameters {
|
||||
let index = ((z as u32 & 31) * 32 + (x as u32 & 31)) as usize;
|
||||
let entry = &mut self.entries[index];
|
||||
if let Some((key, parameters)) = entry
|
||||
&& *key == (x, z)
|
||||
{
|
||||
return *parameters;
|
||||
}
|
||||
let parameters = Parameters::new(x, z);
|
||||
*entry = Some(((x, z), parameters));
|
||||
parameters
|
||||
}
|
||||
}
|
||||
|
||||
/// One unsplit camera-relative weather vertex. UV animation and atlas-local
|
||||
/// repetition happen in the weather shader, rather than rebuilding tiled quads.
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct Vertex {
|
||||
pub pos: [f32; 3],
|
||||
pub uv: [f32; 2],
|
||||
pub rect: [f32; 4],
|
||||
pub animation: [f32; 4],
|
||||
pub bases: [u32; 4],
|
||||
pub color: u32,
|
||||
pub light: u32,
|
||||
pub snow: u32,
|
||||
}
|
||||
|
||||
/// Current ticks are masked on the CPU before conversion to float, including
|
||||
/// negative/large tick values. Do not use the renderer's unbounded game_time.
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct Time {
|
||||
pub values: [f32; 4],
|
||||
}
|
||||
impl Time {
|
||||
pub fn new(ticks: i64, partial: f32) -> Self {
|
||||
Self {
|
||||
values: [(ticks & 131_071) as f32, (ticks & 511) as f32, partial, 0.0],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Emit a single quad, retaining column_quad's position, fade, UV rounding and
|
||||
/// light semantics. The zero phase leaves the shader to animate both UV axes.
|
||||
pub fn vertices(
|
||||
column: &Column,
|
||||
parameters: Parameters,
|
||||
snow: bool,
|
||||
camera: [f64; 3],
|
||||
radius: i32,
|
||||
intensity: f32,
|
||||
rect: [f32; 4],
|
||||
) -> [Vertex; 4] {
|
||||
let quad = column_quad(
|
||||
column,
|
||||
camera,
|
||||
if snow { 0.8 } else { 1.0 },
|
||||
radius,
|
||||
intensity,
|
||||
);
|
||||
let lc = column.light_coords;
|
||||
let light = ((lc >> 20 & 15) * 16) as u32 | (((lc >> 4 & 15) * 16) as u32) << 8;
|
||||
let [u, v] = parameters.snow_base.map(f64::to_bits);
|
||||
quad.map(|q| Vertex {
|
||||
pos: q.pos,
|
||||
uv: q.uv,
|
||||
rect,
|
||||
animation: [
|
||||
parameters.rain_speed,
|
||||
parameters.rain_tick_offset,
|
||||
parameters.snow_drift[0],
|
||||
parameters.snow_drift[1],
|
||||
],
|
||||
bases: [u as u32, (u >> 32) as u32, v as u32, (v >> 32) as u32],
|
||||
color: q.color,
|
||||
light,
|
||||
snow: u32::from(snow),
|
||||
})
|
||||
}
|
||||
|
||||
/// `createRainColumnInstance`.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn rain_column(
|
||||
@@ -140,8 +311,8 @@ pub fn column_quad(
|
||||
let t = (distance_sq / radius_sq).min(1.0);
|
||||
let alpha = (max_alpha + t * (0.5 - max_alpha)) * intensity;
|
||||
let color = ((alpha * 255.0).floor() as i32 as u32) << 24 | 0xFF_FFFF;
|
||||
let index =
|
||||
(column.z - camera[2].floor() as i32 + 16) * 32 + column.x - camera[0].floor() as i32 + 16;
|
||||
let index = (column.z - camera[2].floor() as i32 + 16) * 32
|
||||
+ (column.x - camera[0].floor() as i32 + 16);
|
||||
let (size_x, size_z) = column_size(index as usize);
|
||||
let (half_x, half_z) = (size_x / 2.0, size_z / 2.0);
|
||||
let (x0, x1) = (relative_x - half_x, relative_x + half_x);
|
||||
@@ -245,6 +416,82 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cached_parameters_match_original_random_draws() {
|
||||
let mut cache = ParameterCache::default();
|
||||
for i in 0..2048i32 {
|
||||
let x = i.wrapping_mul(45_238_971);
|
||||
let z = i.wrapping_mul(-418_711).wrapping_add(i32::MIN);
|
||||
for ticks in [i64::MIN, -1, 0, 511, 512, 131_071, 131_072, i64::MAX] {
|
||||
for partial in [0.0, 0.5, 0.999] {
|
||||
for snow in [false, true] {
|
||||
let mut random = column_random(x, z);
|
||||
let reference = if snow {
|
||||
snow_column(&mut random, ticks, x, -64, 320, z, 0xf000f0, partial)
|
||||
} else {
|
||||
rain_column(&mut random, ticks, x, -64, 320, z, 0xf000f0, partial)
|
||||
};
|
||||
assert_eq!(
|
||||
cache
|
||||
.get(x, z)
|
||||
.column(snow, ticks, x, -64, 320, z, 0xf000f0, partial),
|
||||
reference
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parameter_cache_reuses_full_keys_and_stays_bounded() {
|
||||
let mut cache = ParameterCache::default();
|
||||
let expected = cache.get(-1, i32::MAX);
|
||||
// A changed entry proves that a hit does not regenerate random values.
|
||||
let index = 31 * 32 + 31;
|
||||
cache.entries[index].as_mut().unwrap().1.rain_speed = 123.0;
|
||||
assert_eq!(cache.get(-1, i32::MAX).rain_speed, 123.0);
|
||||
// Colliding positions must replace the entry, rather than reuse it.
|
||||
cache.get(31, -1);
|
||||
assert_eq!(cache.get(-1, i32::MAX).rain_speed, expected.rain_speed);
|
||||
for i in 0..100_000i32 {
|
||||
cache.get(i.wrapping_mul(3121), i.wrapping_mul(418_711));
|
||||
}
|
||||
assert_eq!(cache.entries.len(), 1024);
|
||||
assert!(cache.entries.iter().flatten().count() <= 1024);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsplit_vertices_keep_live_geometry_light_and_atlas_inputs() {
|
||||
let x = i32::MAX - 5;
|
||||
let z = i32::MIN + 5;
|
||||
let parameters = Parameters::new(x, z);
|
||||
let camera = [f64::from(x) - 2.25, 80.75, f64::from(z) + 0.25];
|
||||
for (bottom_y, top_y, light_coords, radius, intensity, rect) in [
|
||||
(64, 90, 0, 5, 0.25, [0.1, 0.2, 0.03, 0.04]),
|
||||
(85, 95, 0xf000f0, 10, 1.0, [0.5, 0.5, 0.1, 0.2]),
|
||||
] {
|
||||
let column = Column {
|
||||
x,
|
||||
z,
|
||||
bottom_y,
|
||||
top_y,
|
||||
light_coords,
|
||||
u_offset: 0.0,
|
||||
v_offset: 0.0,
|
||||
};
|
||||
let quad = column_quad(&column, camera, 1.0, radius, intensity);
|
||||
let vertices = vertices(&column, parameters, false, camera, radius, intensity, rect);
|
||||
for (a, b) in vertices.iter().zip(quad) {
|
||||
assert_eq!(a.pos, b.pos);
|
||||
assert_eq!(a.uv, b.uv);
|
||||
assert_eq!(a.color, b.color);
|
||||
assert_eq!(a.rect, rect);
|
||||
assert_eq!(a.light, if light_coords == 0 { 0 } else { 0xf0f0 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rain_repeats_every_four_blocks() {
|
||||
let column = Column {
|
||||
|
||||
@@ -50,6 +50,64 @@ impl EndIslandSampler {
|
||||
}
|
||||
height
|
||||
}
|
||||
// Nearby volume columns visit overlapping 25-by-25 island neighborhoods.
|
||||
// Collect the eligible islands once, retaining their original X/Z order.
|
||||
// Larger or coordinate-wrapping volumes keep the scalar evaluation path.
|
||||
fn volume_islands(&self, volume: DensityVolume) -> Option<Vec<(i32, i32, f32)>> {
|
||||
let sizes = volume.sizes();
|
||||
if sizes[0] * sizes[2] < 4 {
|
||||
return None;
|
||||
}
|
||||
let minimum = volume.minimum();
|
||||
let steps = volume.steps();
|
||||
let end = |axis: usize| {
|
||||
i32::try_from(
|
||||
i64::from(minimum[axis]) + (sizes[axis] - 1) as i64 * i64::from(steps[axis]),
|
||||
)
|
||||
.ok()
|
||||
};
|
||||
let first_x = minimum[0] / 8 / 2 - 12;
|
||||
let first_z = minimum[2] / 8 / 2 - 12;
|
||||
let last_x = end(0)? / 8 / 2 + 12;
|
||||
let last_z = end(2)? / 8 / 2 + 12;
|
||||
if last_x - first_x >= 64 || last_z - first_z >= 64 {
|
||||
return None;
|
||||
}
|
||||
let mut islands = Vec::new();
|
||||
for x in first_x..=last_x {
|
||||
for z in first_z..=last_z {
|
||||
if i64::from(x) * i64::from(x) + i64::from(z) * i64::from(z) <= 4096
|
||||
|| self.noise.get2(f64::from(x), f64::from(z)) >= -0.9
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let scale = ((x as f32).abs() * 3439.0 + (z as f32).abs() * 147.0) % 13.0 + 9.0;
|
||||
islands.push((x, z, scale));
|
||||
}
|
||||
}
|
||||
Some(islands)
|
||||
}
|
||||
fn density_from_islands(x: i32, z: i32, islands: &[(i32, i32, f32)]) -> f32 {
|
||||
let x = x / 8;
|
||||
let z = z / 8;
|
||||
let center_x = x / 2;
|
||||
let center_z = z / 2;
|
||||
let mut height = -100.0;
|
||||
for &(island_x, island_z, scale) in islands {
|
||||
let offset_x = island_x - center_x;
|
||||
let offset_z = island_z - center_z;
|
||||
if !(-12..=12).contains(&offset_x) || !(-12..=12).contains(&offset_z) {
|
||||
continue;
|
||||
}
|
||||
let dx = (x % 2 - offset_x * 2) as f32;
|
||||
let dz = (z % 2 - offset_z * 2) as f32;
|
||||
let candidate = 100.0 - ((dx * dx + dz * dz) as f64).sqrt() as f32 * scale;
|
||||
let candidate =
|
||||
super::arithmetic::java_min(super::arithmetic::java_max(candidate, -100.0), 80.0);
|
||||
height = super::arithmetic::java_max(height, candidate);
|
||||
}
|
||||
(height - 8.0) / 128.0
|
||||
}
|
||||
fn density(&self, x: i32, z: i32) -> f32 {
|
||||
(self.height(x / 8, z / 8) - 8.0) / 128.0
|
||||
}
|
||||
@@ -66,10 +124,15 @@ impl DensitySampler for EndIslandSampler {
|
||||
) -> Result<(), SampleError> {
|
||||
check_buffer(buffer, volume)?;
|
||||
let [sx, sy, sz] = volume.sizes();
|
||||
let islands = self.volume_islands(volume);
|
||||
for z in 0..sz {
|
||||
let block_z = volume.block(Axis::Z, z).unwrap();
|
||||
for x in 0..sx {
|
||||
let value = self.density(volume.block(Axis::X, x).unwrap(), block_z);
|
||||
let block_x = volume.block(Axis::X, x).unwrap();
|
||||
let value = match &islands {
|
||||
Some(islands) => Self::density_from_islands(block_x, block_z, islands),
|
||||
None => self.density(block_x, block_z),
|
||||
};
|
||||
let start = (x + z * sx) * sy;
|
||||
buffer[start..start + sy].fill(value);
|
||||
}
|
||||
@@ -77,3 +140,78 @@ impl DensitySampler for EndIslandSampler {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn end_island_volumes_match_scalar_at_truncation_and_world_boundaries() {
|
||||
let cases = [
|
||||
([5, 3, 5], [0, -64, 0], [4, 8, 4]),
|
||||
([5, 3, 5], [1920, -64, 2240], [8, 8, 8]),
|
||||
([5, 3, 5], [-2049, -64, 3071], [4, 8, 4]),
|
||||
([5, 3, 5], [1023, -64, -1025], [1, 8, 1]),
|
||||
([5, 3, 5], [30_000_000, 0, -30_000_000], [8, 1, 8]),
|
||||
([5, 3, 5], [i32::MIN, 0, i32::MIN], [8, 1, 8]),
|
||||
([5, 3, 5], [i32::MAX - 32, 0, i32::MAX - 32], [8, 1, 8]),
|
||||
// Also exercise wrapping, wide, sparse and single-column volumes.
|
||||
([5, 3, 5], [i32::MAX - 12, 0, i32::MAX - 3], [8, 1, 8]),
|
||||
([5, 3, 5], [4096, 0, -8192], [1024, 1, 1024]),
|
||||
([2, 3, 2], [-4096, 0, 8192], [599, 1, 599]),
|
||||
([1, 4, 1], [-15, 0, -17], [1, 1, 1]),
|
||||
];
|
||||
for seed in [0, 12345, -1, i64::MIN, i64::MAX] {
|
||||
let sampler = EndIslandSampler::new(seed);
|
||||
let mut context = SamplerContext::default();
|
||||
let mut compare = |volume: DensityVolume| {
|
||||
let sentinel = 123.0_f32;
|
||||
let mut output = vec![sentinel; volume.size() + 1];
|
||||
sampler
|
||||
.sample_volume(&mut context, &mut output, volume)
|
||||
.unwrap();
|
||||
visit(volume, |index, position| {
|
||||
let expected = sampler.sample_value(&mut context, position)?;
|
||||
assert_eq!(
|
||||
output[index].to_bits(),
|
||||
expected.to_bits(),
|
||||
"seed {seed}, position {position:?}, volume {volume:?}"
|
||||
);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(output[volume.size()], sentinel);
|
||||
let mut short = vec![sentinel; volume.size() - 1];
|
||||
assert_eq!(
|
||||
sampler.sample_volume(&mut context, &mut short, volume),
|
||||
Err(DensityError::BufferTooShort.into())
|
||||
);
|
||||
assert!(short.iter().all(|&value| value == sentinel));
|
||||
};
|
||||
for (sizes, minimum, steps) in cases {
|
||||
compare(DensityVolume::new(sizes, minimum, steps).unwrap());
|
||||
}
|
||||
// Every signed remainder around both coordinate divisions, in a
|
||||
// neighborhood that straddles the central-island exclusion circle.
|
||||
for offset in -17..=17 {
|
||||
compare(
|
||||
DensityVolume::new([5, 2, 5], [1024 + offset, -32, -offset], [3, 16, 5])
|
||||
.unwrap(),
|
||||
);
|
||||
}
|
||||
}
|
||||
let sampler = EndIslandSampler::new(12345);
|
||||
assert!(
|
||||
sampler
|
||||
.volume_islands(DensityVolume::new([5, 2, 5], [-2049, 0, 3071], [4, 8, 4]).unwrap())
|
||||
.is_some()
|
||||
);
|
||||
for (sizes, minimum, steps) in [cases[7], cases[8], cases[10]] {
|
||||
assert!(
|
||||
sampler
|
||||
.volume_islands(DensityVolume::new(sizes, minimum, steps).unwrap())
|
||||
.is_none()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1155,6 +1155,11 @@ where
|
||||
pipeline: self,
|
||||
temperatures: &mut worker.temperatures,
|
||||
biome: Biomes::Region(&biomes),
|
||||
uniform_biomes: Some(UniformSurfaceBiomes::new(
|
||||
pos,
|
||||
self.settings.level_min_y,
|
||||
self.settings.level_height,
|
||||
)),
|
||||
possible: Some(possible),
|
||||
},
|
||||
preliminary_surface: self.preliminary.clone(),
|
||||
@@ -1191,6 +1196,7 @@ where
|
||||
pipeline: self,
|
||||
temperatures: &mut worker.temperatures,
|
||||
biome: Biomes::Resolver(&mut resolver, &mut biome_context),
|
||||
uniform_biomes: None,
|
||||
possible: None,
|
||||
},
|
||||
preliminary_surface: self.preliminary.clone(),
|
||||
@@ -1435,6 +1441,63 @@ enum Biomes<'a, 'b> {
|
||||
&'a mut SamplerContext,
|
||||
),
|
||||
}
|
||||
// Lazy equality proofs for the eight quart corners of a block-biome cell.
|
||||
// Adjacent blocks share these candidates even though their zoom distances differ.
|
||||
struct UniformSurfaceBiomes {
|
||||
origin: [i32; 3],
|
||||
height: i32,
|
||||
cells: Vec<Option<Option<BiomeId>>>,
|
||||
}
|
||||
impl UniformSurfaceBiomes {
|
||||
fn new(pos: ChunkPos, min_y: i32, height: i32) -> Self {
|
||||
let height = (height >> 2) + 1;
|
||||
Self {
|
||||
origin: [
|
||||
pos.x.wrapping_shl(4).wrapping_sub(2) >> 2,
|
||||
min_y.wrapping_sub(2) >> 2,
|
||||
pos.z.wrapping_shl(4).wrapping_sub(2) >> 2,
|
||||
],
|
||||
height,
|
||||
cells: vec![None; 25 * height as usize],
|
||||
}
|
||||
}
|
||||
fn get(&mut self, region: &BiomeRegion<BiomeId>, p: BlockPos) -> Option<BiomeId> {
|
||||
let base = [p.x, p.y, p.z].map(|v| v.wrapping_sub(2) >> 2);
|
||||
let d: [i32; 3] = std::array::from_fn(|i| base[i].wrapping_sub(self.origin[i]));
|
||||
if !(0..5).contains(&d[0]) || !(0..self.height).contains(&d[1]) || !(0..5).contains(&d[2]) {
|
||||
return None;
|
||||
}
|
||||
let slot = &mut self.cells[((d[0] * 5 + d[2]) * self.height + d[1]) as usize];
|
||||
if let Some(value) = slot {
|
||||
return *value;
|
||||
}
|
||||
let value = (|| {
|
||||
// These are immutable stored lookups. A missing candidate prevents
|
||||
// the shortcut; only the original selected lookup may report errors.
|
||||
let first = *region.noise_biome(base).ok()?;
|
||||
for x in 0..2 {
|
||||
for y in 0..2 {
|
||||
for z in 0..2 {
|
||||
if *region
|
||||
.noise_biome([
|
||||
base[0].wrapping_add(x),
|
||||
base[1].wrapping_add(y),
|
||||
base[2].wrapping_add(z),
|
||||
])
|
||||
.ok()?
|
||||
!= first
|
||||
{
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(first)
|
||||
})();
|
||||
*slot = Some(value);
|
||||
value
|
||||
}
|
||||
}
|
||||
/// Material-rule world queries for one chunk: shared seed-bound noises, the
|
||||
/// worker's temperature caches and either stored or raw-resolver biomes.
|
||||
struct SurfaceQueries<'a, 'b, R: RandomSource> {
|
||||
@@ -1442,6 +1505,7 @@ struct SurfaceQueries<'a, 'b, R: RandomSource> {
|
||||
temperatures: &'a mut [BiomeTemperature],
|
||||
biome: Biomes<'a, 'b>,
|
||||
possible: Option<BTreeSet<String>>,
|
||||
uniform_biomes: Option<UniformSurfaceBiomes>,
|
||||
}
|
||||
impl<R: RandomSource> SurfaceQueries<'_, '_, R> {
|
||||
fn temperature(&mut self, name: &str) -> Result<&mut BiomeTemperature, MaterialRuleError> {
|
||||
@@ -1479,9 +1543,16 @@ impl<R: RandomSource> MaterialSurfaceQueries for SurfaceQueries<'_, '_, R> {
|
||||
fn biome(&mut self, p: BlockPos) -> Result<Option<Arc<str>>, MaterialRuleError> {
|
||||
let data = &self.pipeline.data;
|
||||
let id = match &mut self.biome {
|
||||
Biomes::Region(region) => *region
|
||||
.biome(p)
|
||||
.map_err(|_| MaterialRuleError::MissingBiome)?,
|
||||
Biomes::Region(region) => match self
|
||||
.uniform_biomes
|
||||
.as_mut()
|
||||
.and_then(|cache| cache.get(region, p))
|
||||
{
|
||||
Some(id) => id,
|
||||
None => *region
|
||||
.biome(p)
|
||||
.map_err(|_| MaterialRuleError::MissingBiome)?,
|
||||
},
|
||||
Biomes::Resolver(resolver, context) => {
|
||||
let name = resolver
|
||||
.sample(context, self.pipeline.zoom.quart_at_block(p))
|
||||
@@ -1820,6 +1891,7 @@ where
|
||||
pipeline: self,
|
||||
temperatures: &mut worker.temperatures,
|
||||
biome: Biomes::Resolver(&mut resolver, &mut biome_context),
|
||||
uniform_biomes: None,
|
||||
possible: None,
|
||||
};
|
||||
let sources = MaterialDensitySources::new(
|
||||
@@ -1986,6 +2058,7 @@ where
|
||||
pipeline: self,
|
||||
temperatures: &mut worker.temperatures,
|
||||
biome: Biomes::Resolver(&mut resolver, &mut biome_context),
|
||||
uniform_biomes: None,
|
||||
possible: None,
|
||||
};
|
||||
let sources = MaterialDensitySources::new(
|
||||
@@ -2552,6 +2625,89 @@ mod tests {
|
||||
assert!(!pipeline.identity_end_surface(ChunkPos::new(0, 0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn uniform_surface_biomes_preserve_zoom_and_missing_corner_errors() {
|
||||
use super::super::biome_chunk::BiomeChunk;
|
||||
use std::convert::Infallible;
|
||||
|
||||
for pos in [
|
||||
ChunkPos::new(0, 0),
|
||||
ChunkPos::new(-17, 23),
|
||||
ChunkPos::new(i32::MIN >> 4, i32::MAX >> 4),
|
||||
] {
|
||||
for seed in [0, -1, i64::MIN] {
|
||||
for mixed in [false, true] {
|
||||
let mut chunks = Vec::new();
|
||||
for dz in -1..=1 {
|
||||
for dx in -1..=1 {
|
||||
let mut chunk = BiomeChunk::new(
|
||||
ChunkPos::new(pos.x + dx, pos.z + dz),
|
||||
-64,
|
||||
128,
|
||||
BiomeId(7),
|
||||
)
|
||||
.unwrap();
|
||||
chunk
|
||||
.fill(-64, 63, |[x, y, z]| {
|
||||
Ok::<_, Infallible>(if mixed {
|
||||
BiomeId(
|
||||
(x.div_euclid(3) + y.div_euclid(4) + z.div_euclid(2))
|
||||
.rem_euclid(5)
|
||||
as u32,
|
||||
)
|
||||
} else {
|
||||
BiomeId(7)
|
||||
})
|
||||
})
|
||||
.unwrap();
|
||||
chunks.push(chunk);
|
||||
}
|
||||
}
|
||||
let region = BiomeRegion::new(chunks, seed).unwrap();
|
||||
let mut cache = UniformSurfaceBiomes::new(pos, -64, 128);
|
||||
let mut hits = 0;
|
||||
for x in 0..16 {
|
||||
for z in 0..16 {
|
||||
// Include clamped Y, out-of-cache Y, and revisit each
|
||||
// cached proof in the ordinary descending traversal.
|
||||
for y in (-70..70).rev() {
|
||||
let p = BlockPos::new(pos.x * 16 + x, y, pos.z * 16 + z);
|
||||
let shortcut = cache.get(®ion, p);
|
||||
hits += usize::from(shortcut.is_some());
|
||||
let actual = shortcut.map_or_else(|| region.biome(p).copied(), Ok);
|
||||
assert_eq!(actual, region.biome(p).copied());
|
||||
}
|
||||
}
|
||||
}
|
||||
if !mixed && pos.x != i32::MIN >> 4 {
|
||||
assert!(hits > 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Some unselected corners are missing. Probing them must neither invent
|
||||
// an error nor hide the original selected-corner error.
|
||||
let pos = ChunkPos::new(0, 0);
|
||||
let region =
|
||||
BiomeRegion::new(vec![BiomeChunk::new(pos, 0, 16, BiomeId(7)).unwrap()], -1).unwrap();
|
||||
let mut cache = UniformSurfaceBiomes::new(pos, 0, 16);
|
||||
let mut successes = 0;
|
||||
let mut failures = 0;
|
||||
for x in -2..18 {
|
||||
for z in -2..18 {
|
||||
let p = BlockPos::new(x, 7, z);
|
||||
let actual = cache
|
||||
.get(®ion, p)
|
||||
.map_or_else(|| region.biome(p).copied(), Ok);
|
||||
assert_eq!(actual, region.biome(p).copied());
|
||||
successes += usize::from(actual.is_ok());
|
||||
failures += usize::from(actual.is_err());
|
||||
}
|
||||
}
|
||||
assert!(successes > 0 && failures > 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn far_surface_matches_undecorated_land_and_keeps_end_void() {
|
||||
let root = std::env::var_os("HORIZON_TEST_RESOURCES")
|
||||
|
||||
@@ -12,8 +12,8 @@ def make_input(build):
|
||||
rng = random.Random(71)
|
||||
cases = []
|
||||
for kind in (0, 1):
|
||||
for ticks in (0, 1, 511, 512, 131071, 131072, 24000 * 37 + 5, 9_000_000_123):
|
||||
for x, z in ((0, 0), (1, 0), (0, -1), (3, 7), (-10, 10), (10, -10), (-4, -9), (1_000_003, -2_000_011)):
|
||||
for ticks in (-9223372036854775808, -1, 0, 1, 511, 512, 131071, 131072, 24000 * 37 + 5, 9_000_000_123, 9223372036854775807):
|
||||
for x, z in ((0, 0), (1, 0), (0, -1), (3, 7), (-10, 10), (10, -10), (-4, -9), (1_000_003, -2_000_011), (30_000_000, -30_000_000), (2_147_483_631, -2_147_483_632)):
|
||||
for bottom, top in ((60, 80), (64, 65), (-64, -44), (300, 310)):
|
||||
light = (rng.randrange(16) << 4) | (rng.randrange(16) << 20)
|
||||
partial = rng.choice(("0.0", "0.5", "0.999", f"{rng.random():.6f}"))
|
||||
|
||||
Reference in New Issue
Block a user