feat: Add arbitrary precision numbers at deep zooms
This commit is contained in:
+1
-1
@@ -9,7 +9,7 @@ pub mod share;
|
||||
|
||||
pub use buddhabrot::{BuddhabrotCallback, BuddhabrotRenderer, BuddhabrotUniforms};
|
||||
pub use kind::FractalKind;
|
||||
pub use reference::{compute_reference, compute_set_reference};
|
||||
pub use reference::{RefOrbit, compute_reference, compute_set_reference};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub use renderer::PipelineKey;
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
|
||||
+135
-15
@@ -39,6 +39,86 @@ const REFERENCE_ESCAPE_SQ: f64 = 1.0e10;
|
||||
/// below 0.1% of a pixel.
|
||||
const F64_MAX_PRECISION: usize = 80;
|
||||
|
||||
/// Orbit points with a magnitude below `2^TINY_LOG2` are stored normalized
|
||||
/// (mantissa + exponent, see [`RefOrbit::exps`]): f32's smallest normal is
|
||||
/// ~2^-126, and the GPU's deep (rescaled) phase needs these points' exact
|
||||
/// value to decide rebasing. The margin keeps a few mantissa bits clear of
|
||||
/// the subnormal range for the smaller component.
|
||||
const TINY_LOG2: i32 = -100;
|
||||
|
||||
/// A reference orbit as uploaded to the GPU.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct RefOrbit {
|
||||
/// `Z_n` as f32 pairs. For points with a non-zero `exps[n]`, a mantissa
|
||||
/// instead: the true value is `points[n] * 2^exps[n]`.
|
||||
pub points: Vec<[f32; 2]>,
|
||||
/// Per-point binary exponent (same length as `points`). Non-zero only for
|
||||
/// points too small for f32's exponent range (see [`TINY_LOG2`]); only
|
||||
/// the deep shader pipeline reads it, so [`Self::has_scaled`] forces it.
|
||||
pub exps: Vec<i32>,
|
||||
}
|
||||
|
||||
impl RefOrbit {
|
||||
fn with_capacity(n: usize) -> Self {
|
||||
Self {
|
||||
points: Vec::with_capacity(n),
|
||||
exps: Vec::with_capacity(n),
|
||||
}
|
||||
}
|
||||
|
||||
fn push(&mut self, point: [f32; 2]) {
|
||||
self.points.push(point);
|
||||
self.exps.push(0);
|
||||
}
|
||||
|
||||
/// Whether any point is stored as mantissa + exponent, i.e. the orbit
|
||||
/// can only be read by the deep pipeline.
|
||||
pub fn has_scaled(&self) -> bool {
|
||||
self.exps.iter().any(|&e| e != 0)
|
||||
}
|
||||
}
|
||||
|
||||
impl core::ops::Deref for RefOrbit {
|
||||
type Target = [[f32; 2]];
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.points
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoIterator for &'a RefOrbit {
|
||||
type Item = &'a [f32; 2];
|
||||
type IntoIter = core::slice::Iter<'a, [f32; 2]>;
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.points.iter()
|
||||
}
|
||||
}
|
||||
|
||||
/// `floor(log2|x|)`, or `None` for zero. Exact (from the binary
|
||||
/// representation), and works far below f64's range.
|
||||
fn big_log2_floor(x: &Big) -> Option<isize> {
|
||||
let repr = x.repr();
|
||||
let digits = repr.digits();
|
||||
(digits > 0).then(|| repr.exponent() + digits as isize - 1)
|
||||
}
|
||||
|
||||
/// Store `(zr, zi)` into `orbit`, as plain f32 unless its magnitude is below
|
||||
/// `2^TINY_LOG2`, in which case both components share an exponent `k` and
|
||||
/// the stored mantissa `Z * 2^-k` has its larger component in `[0.5, 1)`.
|
||||
fn push_big_point(orbit: &mut RefOrbit, zr: &Big, zi: &Big) {
|
||||
let (lr, li) = (big_log2_floor(zr), big_log2_floor(zi));
|
||||
let top = lr.max(li);
|
||||
match top {
|
||||
Some(top) if top < TINY_LOG2 as isize => {
|
||||
let k = top + 1;
|
||||
let mr = (zr.clone() << -k).to_f64().value() as f32;
|
||||
let mi = (zi.clone() << -k).to_f64().value() as f32;
|
||||
orbit.points.push([mr, mi]);
|
||||
orbit.exps.push(k as i32);
|
||||
}
|
||||
_ => orbit.push([zr.to_f64().value() as f32, zi.to_f64().value() as f32]),
|
||||
}
|
||||
}
|
||||
|
||||
/// Compute the reference orbit `Z_0..Z_{len-1}` where `Z_0 = z0` and
|
||||
/// `Z_{n+1} = f(Z_n, c)` for the given `kind` (and `power`, for Multibrot), up
|
||||
/// to `max_iter` steps at `precision` bits. Each entry is `[re, im]` in f32.
|
||||
@@ -59,7 +139,7 @@ pub fn compute_reference(
|
||||
lambda_l: (f64, f64),
|
||||
complex_power: (f64, f64),
|
||||
morph: Option<(FractalKind, f64)>,
|
||||
) -> Vec<[f32; 2]> {
|
||||
) -> RefOrbit {
|
||||
// A zero-weight morph is just the plain kind; skip the second formula.
|
||||
let morph = morph.filter(|&(_, w)| w != 0.0);
|
||||
if precision <= F64_MAX_PRECISION {
|
||||
@@ -110,12 +190,12 @@ fn compute_reference_f64(
|
||||
kind: FractalKind,
|
||||
k: &StepConstsF64,
|
||||
morph: Option<(FractalKind, f64)>,
|
||||
) -> Vec<[f32; 2]> {
|
||||
) -> RefOrbit {
|
||||
let (mut zr, mut zi) = z0;
|
||||
// Previous iterate, for the Phoenix two-term recurrence (Y_{-1} = 0).
|
||||
let mut prev = (0.0f64, 0.0f64);
|
||||
|
||||
let mut points: Vec<[f32; 2]> = Vec::with_capacity(max_iter as usize + 1);
|
||||
let mut points = RefOrbit::with_capacity(max_iter as usize + 1);
|
||||
for _ in 0..=max_iter {
|
||||
points.push([zr as f32, zi as f32]);
|
||||
if zr * zr + zi * zi > REFERENCE_ESCAPE_SQ {
|
||||
@@ -217,7 +297,7 @@ fn compute_reference_big(
|
||||
kind: FractalKind,
|
||||
k: &StepConsts,
|
||||
morph: Option<(FractalKind, f64)>,
|
||||
) -> Vec<[f32; 2]> {
|
||||
) -> RefOrbit {
|
||||
let precision = k.precision;
|
||||
let morph = morph.map(|(from, w)| (from, big_from_f64(w, precision)));
|
||||
|
||||
@@ -227,13 +307,12 @@ fn compute_reference_big(
|
||||
let mut zr_prev = big_zero(precision);
|
||||
let mut zi_prev = big_zero(precision);
|
||||
|
||||
let mut points: Vec<[f32; 2]> = Vec::with_capacity(max_iter as usize + 1);
|
||||
let mut points = RefOrbit::with_capacity(max_iter as usize + 1);
|
||||
|
||||
for _ in 0..=max_iter {
|
||||
let fr = zr.to_f64().value() as f32;
|
||||
let fi = zi.to_f64().value() as f32;
|
||||
points.push([fr, fi]);
|
||||
push_big_point(&mut points, &zr, &zi);
|
||||
|
||||
let [fr, fi] = *points.points.last().unwrap();
|
||||
let mag = (fr as f64) * (fr as f64) + (fi as f64) * (fi as f64);
|
||||
if mag > REFERENCE_ESCAPE_SQ {
|
||||
break;
|
||||
@@ -403,7 +482,7 @@ pub fn compute_set_reference(
|
||||
lambda_l: (f64, f64),
|
||||
complex_power: (f64, f64),
|
||||
morph: Option<(FractalKind, f64)>,
|
||||
) -> Vec<[f32; 2]> {
|
||||
) -> RefOrbit {
|
||||
let zero = big_zero(precision);
|
||||
compute_reference(
|
||||
&zero,
|
||||
@@ -510,6 +589,52 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Orbit points below f32's range are stored as a normalized mantissa
|
||||
/// plus exponent (for the deep GPU phase); every other point stays a
|
||||
/// plain f32 with exponent 0.
|
||||
#[test]
|
||||
fn tiny_points_are_stored_normalized() {
|
||||
let bits = 400;
|
||||
// c = -1 + δ: X_2 = c(c + 1) = -δ + δ², far below f32's range.
|
||||
let delta = 1e-45_f64;
|
||||
let cr = big_from_f64(-1.0, bits) + big_from_f64(delta, bits);
|
||||
let ci = big_from_f64(0.0, bits);
|
||||
let orbit = compute_set_reference(
|
||||
&cr,
|
||||
&ci,
|
||||
3,
|
||||
bits,
|
||||
FractalKind::Mandelbrot,
|
||||
2,
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
None,
|
||||
);
|
||||
assert_eq!(orbit.exps.len(), orbit.points.len());
|
||||
assert!(orbit.has_scaled());
|
||||
assert_eq!(&orbit.exps[..2], &[0, 0], "X_0 = 0 and X_1 = c are plain");
|
||||
let [mr, mi] = orbit.points[2];
|
||||
let k = orbit.exps[2];
|
||||
assert!(k < TINY_LOG2, "exponent {k}");
|
||||
assert!(
|
||||
(0.5..1.0).contains(&mr.abs()),
|
||||
"mantissa {mr} not normalized"
|
||||
);
|
||||
assert_eq!(mi, 0.0);
|
||||
let x2 = mr as f64 * 2f64.powi(k);
|
||||
assert!(
|
||||
(x2 + delta).abs() < 1e-6 * delta,
|
||||
"X_2 = {x2}, expected {}",
|
||||
-delta
|
||||
);
|
||||
|
||||
// A shallow orbit stays entirely plain.
|
||||
let plain = set_ref(-0.75, 0.1, FractalKind::Mandelbrot, None);
|
||||
assert!(!plain.has_scaled());
|
||||
assert!(plain.exps.iter().all(|&e| e == 0));
|
||||
}
|
||||
|
||||
/// A point inside the main cardioid never escapes: full-length orbit.
|
||||
#[test]
|
||||
fn interior_orbit_runs_full_length() {
|
||||
@@ -842,12 +967,7 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn set_ref(
|
||||
cr: f64,
|
||||
ci: f64,
|
||||
kind: FractalKind,
|
||||
morph: Option<(FractalKind, f64)>,
|
||||
) -> Vec<[f32; 2]> {
|
||||
fn set_ref(cr: f64, ci: f64, kind: FractalKind, morph: Option<(FractalKind, f64)>) -> RefOrbit {
|
||||
compute_set_reference(
|
||||
&Big::try_from(cr).unwrap(),
|
||||
&Big::try_from(ci).unwrap(),
|
||||
|
||||
+60
-4
@@ -14,6 +14,7 @@ use std::sync::Arc;
|
||||
|
||||
use eframe::egui_wgpu::{self, wgpu};
|
||||
|
||||
use super::reference::RefOrbit;
|
||||
use crate::lights::{GpuLight, Light, MAX_LIGHT_COUNT, gpu_lights};
|
||||
|
||||
/// Maximum reference-orbit length (points) the storage buffer can hold. Also
|
||||
@@ -51,6 +52,7 @@ fn geom_differs(a: &Uniforms, b: &Uniforms) -> bool {
|
||||
|| a.power != b.power
|
||||
|| a.complex_power != b.complex_power
|
||||
|| a.dc_offset != b.dc_offset
|
||||
|| a.scale_exp != b.scale_exp
|
||||
|| a.phoenix_p != b.phoenix_p
|
||||
|| a.lambda_l != b.lambda_l
|
||||
|| a.morph_from != b.morph_from
|
||||
@@ -87,6 +89,9 @@ pub struct PipelineKey {
|
||||
de: bool,
|
||||
/// A kind-switch morph is in progress (`morph_w > 0`).
|
||||
morph: bool,
|
||||
/// Deep view: the delta starts out in rescaled (mantissa + exponent)
|
||||
/// form (`scale_exp != 0`).
|
||||
deep: bool,
|
||||
}
|
||||
|
||||
impl PipelineKey {
|
||||
@@ -96,15 +101,17 @@ impl PipelineKey {
|
||||
julia: u.is_julia != 0,
|
||||
de: u.de_coloring != 0,
|
||||
morph: u.morph_w > 0.0,
|
||||
deep: u.scale_exp != 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn constants(&self) -> [(&'static str, f64); 4] {
|
||||
fn constants(&self) -> [(&'static str, f64); 5] {
|
||||
[
|
||||
("KIND", self.kind as f64),
|
||||
("IS_JULIA", self.julia as u32 as f64),
|
||||
("DE", self.de as u32 as f64),
|
||||
("MORPH", self.morph as u32 as f64),
|
||||
("DEEP", self.deep as u32 as f64),
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -217,7 +224,11 @@ pub struct Uniforms {
|
||||
/// 0 = no morph (and the iteration pipeline is then specialized without
|
||||
/// the morph path, see [`PipelineKey`]).
|
||||
pub morph_w: f32,
|
||||
pub _pad3: [u32; 1],
|
||||
/// Binary exponent `E` of the deep (rescaled) view scale: `span` and
|
||||
/// `dc_offset` are uploaded multiplied by `2^-E`, so they stay inside
|
||||
/// f32's exponent range at any depth. Non-zero exactly when the deep
|
||||
/// pipeline is used (see [`PipelineKey`] and `mandelbrot.wgsl`'s `DEEP`).
|
||||
pub scale_exp: i32,
|
||||
/// Complex binomial coefficients `C(complex_power, k)`, k = 1..16, two per
|
||||
/// row (odd k in `[0..2]`, even k in `[2..4]`), for the Complex Multibrot
|
||||
/// delta series. Derived from `complex_power` alone.
|
||||
@@ -279,6 +290,7 @@ pub struct FractalRenderer {
|
||||
bind_group_layout: wgpu::BindGroupLayout,
|
||||
uniform_buffer: wgpu::Buffer,
|
||||
ref_buffer: wgpu::Buffer,
|
||||
ref_exp_buffer: wgpu::Buffer,
|
||||
lights_buffer: wgpu::Buffer,
|
||||
bind_group: wgpu::BindGroup,
|
||||
target_format: wgpu::TextureFormat,
|
||||
@@ -331,6 +343,15 @@ impl FractalRenderer {
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
|
||||
// Per-point exponents of the reference orbit (`RefOrbit::exps`), only
|
||||
// read by deep pipelines.
|
||||
let ref_exp_buffer = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("reference orbit exponents"),
|
||||
size: (MAX_REF_POINTS * std::mem::size_of::<i32>()) as u64,
|
||||
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
|
||||
let lights_buffer = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("lights parameters"),
|
||||
size: std::mem::size_of::<[GpuLight; MAX_LIGHT_COUNT]>() as u64,
|
||||
@@ -374,6 +395,16 @@ impl FractalRenderer {
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 3,
|
||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: true },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
@@ -393,6 +424,10 @@ impl FractalRenderer {
|
||||
binding: 2,
|
||||
resource: lights_buffer.as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 3,
|
||||
resource: ref_exp_buffer.as_entire_binding(),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
@@ -590,6 +625,7 @@ impl FractalRenderer {
|
||||
bind_group_layout,
|
||||
uniform_buffer,
|
||||
ref_buffer,
|
||||
ref_exp_buffer,
|
||||
lights_buffer,
|
||||
bind_group,
|
||||
target_format,
|
||||
@@ -892,7 +928,7 @@ impl ExportRender {
|
||||
width: u32,
|
||||
height: u32,
|
||||
uniforms: Uniforms,
|
||||
reference: &[[f32; 2]],
|
||||
reference: &RefOrbit,
|
||||
lights: &[Light],
|
||||
) -> Self {
|
||||
let uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
@@ -910,8 +946,19 @@ impl ExportRender {
|
||||
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
let ref_exp_buffer = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("export reference orbit exponents"),
|
||||
size: (count.max(1) * std::mem::size_of::<i32>()) as u64,
|
||||
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
if count > 0 {
|
||||
queue.write_buffer(&ref_buffer, 0, bytemuck::cast_slice(&reference[..count]));
|
||||
queue.write_buffer(
|
||||
&ref_exp_buffer,
|
||||
0,
|
||||
bytemuck::cast_slice(&reference.exps[..count]),
|
||||
);
|
||||
}
|
||||
|
||||
// Only read by the shadow branch's custom-lights palette; harmless
|
||||
@@ -942,6 +989,10 @@ impl ExportRender {
|
||||
binding: 2,
|
||||
resource: lights_buffer.as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 3,
|
||||
resource: ref_exp_buffer.as_entire_binding(),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
@@ -1391,7 +1442,7 @@ pub struct FractalCallback {
|
||||
/// Lights buffer contents, from [`gpu_lights`] (its count is in
|
||||
/// `uniforms.light_count`).
|
||||
pub lights: [GpuLight; MAX_LIGHT_COUNT],
|
||||
pub reference: Arc<Vec<[f32; 2]>>,
|
||||
pub reference: Arc<RefOrbit>,
|
||||
pub generation: u64,
|
||||
/// Widget size in physical pixels — the cache texture resolution.
|
||||
pub size_px: [u32; 2],
|
||||
@@ -1457,6 +1508,11 @@ impl egui_wgpu::CallbackTrait for FractalCallback {
|
||||
0,
|
||||
bytemuck::cast_slice(&self.reference[..count]),
|
||||
);
|
||||
queue.write_buffer(
|
||||
&renderer.ref_exp_buffer,
|
||||
0,
|
||||
bytemuck::cast_slice(&self.reference.exps[..count]),
|
||||
);
|
||||
renderer.uploaded_generation = self.generation;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user