#![allow(clippy::float_cmp)]
use super::{FieldSampler, GpuField};
use crate::headless::Gpu;
use crate::{GridParams, read_buffer};
fn gpu() -> &'static Gpu {
crate::headless::shared()
}
struct Deviation {
exact: usize,
total: usize,
worst_abs: f32,
worst_ulps: u32,
at: [f32; 3],
}
fn measure(gpu: &Gpu, sampler: &FieldSampler, field: GpuField, grid: GridParams) -> Deviation {
let buffer = sampler
.sample(gpu.device(), gpu.queue(), grid, field)
.expect("gpu sample");
let got = read_buffer(
gpu.device(),
gpu.queue(),
buffer.buffer(),
grid.field_buffer_size(),
)
.expect("read back");
let [sx, sy, sz] = grid.samples();
let mut deviation = Deviation {
exact: 0,
total: 0,
worst_abs: 0.0,
worst_ulps: 0,
at: [0.0; 3],
};
let mut i = 0usize;
for z in 0..sz {
for y in 0..sy {
for x in 0..sx {
let p = grid.sample_position([x, y, z]);
let cpu = field.sample_on_cpu(p);
let gpu_value = got[i];
deviation.total += 1;
if gpu_value.to_bits() == cpu.to_bits() {
deviation.exact += 1;
} else {
let abs = (gpu_value - cpu).abs();
let ulps = gpu_value.to_bits().abs_diff(cpu.to_bits());
if abs > deviation.worst_abs {
deviation.worst_abs = abs;
deviation.at = p;
}
deviation.worst_ulps = deviation.worst_ulps.max(ulps);
}
i += 1;
}
}
}
deviation
}
#[test]
fn the_gpu_field_matches_the_cpu_within_a_measured_bound() {
let gpu = gpu();
let sampler = FieldSampler::new(gpu.device()).expect("pipeline");
let grid = GridParams::new([33; 3], [-2.0; 3], 0.125).expect("grid");
println!(
"{:<12} {:>10} {:>8} {:>12} {:>8}",
"field", "bit-exact", "of", "worst abs", "ulps"
);
for field in GpuField::ALL {
let d = measure(gpu, &sampler, field, grid);
println!(
"{:<12} {:>10} {:>8} {:>12.3e} {:>8} worst at {:?}",
field.name(),
d.exact,
d.total,
d.worst_abs,
d.worst_ulps,
d.at
);
assert!(
d.worst_abs < 1e-6,
"{}: worst deviation {:e} is too large to be rounding",
field.name(),
d.worst_abs
);
}
}
#[test]
fn no_field_agrees_with_libm_bit_for_bit() {
let gpu = gpu();
let sampler = FieldSampler::new(gpu.device()).expect("pipeline");
let grid = GridParams::new([33; 3], [-2.0; 3], 0.125).expect("grid");
for field in GpuField::ALL {
let d = measure(gpu, &sampler, field, grid);
assert!(
d.exact < d.total,
"{} now agrees with libm on all {} samples -- the GPU/CPU divergence \
this ticket measured has gone away for it, and everything resting \
on the deviation table needs re-reading",
field.name(),
d.total
);
}
}
#[test]
fn the_field_of_exact_operations_is_the_closest() {
let gpu = gpu();
let sampler = FieldSampler::new(gpu.device()).expect("pipeline");
let grid = GridParams::new([33; 3], [-2.0; 3], 0.125).expect("grid");
let boxed = measure(gpu, &sampler, GpuField::BoxExact, grid);
assert_eq!(
boxed.worst_ulps, 1,
"box_exact's worst deviation is {} ULPs, not 1 -- its arithmetic is \
supposed to be exact except on the exterior shell",
boxed.worst_ulps
);
for other in [GpuField::Sphere, GpuField::Torus, GpuField::Gyroid] {
let d = measure(gpu, &sampler, other, grid);
assert!(
d.exact < boxed.exact,
"{} matches on {} samples, box_exact on {} -- box_exact should be \
the closest",
other.name(),
d.exact,
boxed.exact
);
}
}
#[test]
fn the_flat_index_is_not_transposed() {
let gpu = gpu();
let sampler = FieldSampler::new(gpu.device()).expect("pipeline");
let grid = GridParams::new([5, 7, 3], [-1.0, -2.0, -0.5], 0.25).expect("grid");
let buffer = sampler
.sample(gpu.device(), gpu.queue(), grid, GpuField::BoxExact)
.expect("sample");
let got = read_buffer(
gpu.device(),
gpu.queue(),
buffer.buffer(),
grid.field_buffer_size(),
)
.expect("read back");
let [sx, sy, _] = grid.samples();
for (index, value) in got.iter().enumerate() {
let i = index as u32;
let at = [i % sx, (i / sx) % sy, i / (sx * sy)];
let expected = GpuField::BoxExact.sample_on_cpu(grid.sample_position(at));
assert_eq!(
value.to_bits(),
expected.to_bits(),
"sample {index} holds the value for a different position"
);
}
}
#[test]
fn a_gpu_evaluated_field_extracts_the_same_mesh() {
use crate::{FieldBuffer, MarchingCubesGpu};
let gpu = gpu();
let sampler = FieldSampler::new(gpu.device()).expect("pipeline");
let mc = MarchingCubesGpu::new(gpu.device(), gpu.queue()).expect("pipeline");
for (field, samples, cell) in [
(GpuField::Sphere, 65u32, 0.0625f32),
(GpuField::Torus, 65, 0.0625),
(GpuField::BoxExact, 65, 0.0625),
(GpuField::Gyroid, 49, 4.0 / 48.0),
] {
let grid = GridParams::new([samples; 3], [-2.0; 3], cell).expect("grid");
let uploaded = FieldBuffer::sampled(gpu.device(), gpu.queue(), grid, &FieldOf(field))
.expect("cpu sample");
let produced = sampler
.sample(gpu.device(), gpu.queue(), grid, field)
.expect("gpu sample");
let from_cpu = mc
.extract_buffers(gpu.device(), gpu.queue(), &uploaded)
.expect("extract");
let from_gpu = mc
.extract_buffers(gpu.device(), gpu.queue(), &produced)
.expect("extract");
assert_eq!(
from_cpu.triangles,
from_gpu.triangles,
"{} at {samples}^3: a GPU-evaluated field changed the triangle count \
({} against {}), so a sample crossed the sign test",
field.name(),
from_gpu.triangles,
from_cpu.triangles
);
}
}
struct FieldOf(GpuField);
impl isomesh::Sdf for FieldOf {
type Scalar = f32;
fn sample(&self, p: [f32; 3]) -> f32 {
self.0.sample_on_cpu(p)
}
}
use super::{GpuBrush, GpuOp, GpuShape};
fn cpu_stack(base: GpuField, brushes: &[GpuBrush], p: [f32; 3]) -> f32 {
use isomesh::Sdf;
use isomesh::brush::{Brush, BrushStack};
struct Base(GpuField);
impl Sdf for Base {
type Scalar = f32;
fn sample(&self, p: [f32; 3]) -> f32 {
self.0.sample_on_cpu(p)
}
}
let ops: Vec<Brush<GpuShape>> = brushes.iter().map(|b| b.to_cpu()).collect();
BrushStack {
base: Base(base),
brushes: &ops,
}
.sample(p)
}
fn check_stack(
gpu: &Gpu,
sampler: &FieldSampler,
base: GpuField,
brushes: &[GpuBrush],
what: &str,
) {
let grid = GridParams::new([25; 3], [-2.0; 3], 0.125).expect("grid");
let buffer = sampler
.sample_stack(gpu.device(), gpu.queue(), grid, base, brushes)
.expect("gpu stack");
let got = read_buffer(
gpu.device(),
gpu.queue(),
buffer.buffer(),
grid.field_buffer_size(),
)
.expect("read back");
let [sx, sy, sz] = grid.samples();
let mut worst = 0.0f32;
let mut i = 0usize;
for z in 0..sz {
for y in 0..sy {
for x in 0..sx {
let p = grid.sample_position([x, y, z]);
worst = worst.max((got[i] - cpu_stack(base, brushes, p)).abs());
i += 1;
}
}
}
assert!(
worst < 1e-5,
"{what}: worst deviation {worst:e} from BrushStack -- too large to be rounding"
);
println!("{what:<44} worst {worst:e}");
}
#[test]
fn every_brush_op_matches_the_cpu_fold() {
let gpu = gpu();
let sampler = FieldSampler::new(gpu.device()).expect("pipeline");
let shape = GpuShape::Sphere {
center: [0.4, 0.1, -0.2],
radius: 0.7,
};
for (op, name) in [
(GpuOp::Add, "add"),
(GpuOp::Subtract, "subtract"),
(GpuOp::SmoothAdd { k: 0.25 }, "smooth_add k=0.25"),
(GpuOp::SmoothAdd { k: 0.0 }, "smooth_add k=0"),
] {
check_stack(
gpu,
&sampler,
GpuField::BoxExact,
&[GpuBrush { shape, op }],
name,
);
}
}
#[test]
fn every_brush_shape_matches_the_cpu() {
let gpu = gpu();
let sampler = FieldSampler::new(gpu.device()).expect("pipeline");
for (shape, name) in [
(
GpuShape::Sphere {
center: [0.3, -0.2, 0.1],
radius: 0.6,
},
"sphere brush",
),
(
GpuShape::BoxExact {
center: [-0.2, 0.3, 0.0],
half_extents: [0.5, 0.3, 0.7],
},
"box brush",
),
(
GpuShape::Capsule {
a: [-0.6, 0.0, 0.0],
b: [0.6, 0.4, -0.3],
radius: 0.35,
},
"capsule brush",
),
(
GpuShape::Capsule {
a: [0.1, 0.1, 0.1],
b: [0.1, 0.1, 0.1],
radius: 0.4,
},
"degenerate capsule",
),
] {
check_stack(
gpu,
&sampler,
GpuField::Sphere,
&[GpuBrush {
shape,
op: GpuOp::Subtract,
}],
name,
);
}
}
#[test]
fn a_mixed_log_matches_the_cpu_in_order() {
let gpu = gpu();
let sampler = FieldSampler::new(gpu.device()).expect("pipeline");
let mut log = Vec::new();
for i in 0..12u32 {
let t = i as f32;
let shape = match i % 3 {
0 => GpuShape::Sphere {
center: [0.9 * (t * 0.7).sin(), 0.6 * (t * 1.1).cos(), 0.4 * t.sin()],
radius: 0.3 + 0.05 * (t % 3.0),
},
1 => GpuShape::BoxExact {
center: [0.5 * (t * 0.9).cos(), 0.2 * t.sin(), 0.7 * (t * 0.3).sin()],
half_extents: [0.25, 0.35, 0.2],
},
_ => GpuShape::Capsule {
a: [-0.5, 0.2 * t.sin(), 0.0],
b: [0.5, 0.1 * t.cos(), 0.2],
radius: 0.2,
},
};
let op = match i % 4 {
0 => GpuOp::Add,
1 => GpuOp::Subtract,
2 => GpuOp::SmoothAdd { k: 0.2 },
_ => GpuOp::Subtract,
};
log.push(GpuBrush { shape, op });
}
check_stack(gpu, &sampler, GpuField::Gyroid, &log, "12-brush mixed log");
}
#[test]
fn an_empty_log_is_the_base_field() {
let gpu = gpu();
let sampler = FieldSampler::new(gpu.device()).expect("pipeline");
let grid = GridParams::new([17; 3], [-2.0; 3], 0.25).expect("grid");
for field in GpuField::ALL {
let plain = sampler
.sample(gpu.device(), gpu.queue(), grid, field)
.expect("sample");
let empty = sampler
.sample_stack(gpu.device(), gpu.queue(), grid, field, &[])
.expect("sample_stack");
let a = read_buffer(
gpu.device(),
gpu.queue(),
plain.buffer(),
grid.field_buffer_size(),
)
.expect("read");
let b = read_buffer(
gpu.device(),
gpu.queue(),
empty.buffer(),
grid.field_buffer_size(),
)
.expect("read");
assert_eq!(
a.iter().map(|v| v.to_bits()).collect::<Vec<_>>(),
b.iter().map(|v| v.to_bits()).collect::<Vec<_>>(),
"{}: an empty log changed the field",
field.name()
);
}
}
#[test]
fn a_gpu_folded_log_extracts_the_same_mesh() {
use crate::{FieldBuffer, MarchingCubesGpu};
use isomesh::Sdf;
use isomesh::brush::{Brush, BrushStack};
let gpu = gpu();
let sampler = FieldSampler::new(gpu.device()).expect("pipeline");
let mc = MarchingCubesGpu::new(gpu.device(), gpu.queue()).expect("pipeline");
let grid = GridParams::new([49; 3], [-2.0; 3], 4.0 / 48.0).expect("grid");
let log = [
GpuBrush {
shape: GpuShape::Sphere {
center: [0.6, 0.2, 0.0],
radius: 0.55,
},
op: GpuOp::Subtract,
},
GpuBrush {
shape: GpuShape::Capsule {
a: [-0.8, -0.3, 0.0],
b: [0.4, 0.5, 0.2],
radius: 0.22,
},
op: GpuOp::Add,
},
GpuBrush {
shape: GpuShape::BoxExact {
center: [-0.3, 0.4, 0.1],
half_extents: [0.3, 0.2, 0.4],
},
op: GpuOp::SmoothAdd { k: 0.15 },
},
];
struct Base;
impl Sdf for Base {
type Scalar = f32;
fn sample(&self, p: [f32; 3]) -> f32 {
GpuField::Sphere.sample_on_cpu(p)
}
}
let ops: Vec<Brush<GpuShape>> = log.iter().map(|b| b.to_cpu()).collect();
let stack = BrushStack {
base: Base,
brushes: &ops,
};
let uploaded =
FieldBuffer::sampled(gpu.device(), gpu.queue(), grid, &stack).expect("cpu sample");
let folded = sampler
.sample_stack(gpu.device(), gpu.queue(), grid, GpuField::Sphere, &log)
.expect("gpu fold");
let from_cpu = mc
.extract_buffers(gpu.device(), gpu.queue(), &uploaded)
.expect("extract");
let from_gpu = mc
.extract_buffers(gpu.device(), gpu.queue(), &folded)
.expect("extract");
assert!(from_cpu.triangles > 0, "the fixture extracted nothing");
assert_eq!(
from_cpu.triangles, from_gpu.triangles,
"a GPU-folded edit log changed the triangle count: {} against {}",
from_gpu.triangles, from_cpu.triangles
);
}
use crate::FieldBuffer;
struct WavyGround;
impl isomesh::Sdf for WavyGround {
type Scalar = f32;
fn sample(&self, p: [f32; 3]) -> f32 {
let height = 0.35 * (p[0] * 0.9).sin() * (p[2] * 0.7).cos() + 0.15 * (p[0] * 2.1).sin();
p[1] - height
}
}
fn fold_grid() -> GridParams {
GridParams::new([25; 3], [-2.0; 3], 0.125).expect("grid")
}
fn ground_samples(grid: GridParams) -> Vec<f32> {
use isomesh::Sdf;
let [sx, sy, sz] = grid.samples();
let mut out = Vec::with_capacity(grid.sample_count() as usize);
for z in 0..sz {
for y in 0..sy {
for x in 0..sx {
out.push(WavyGround.sample(grid.sample_position([x, y, z])));
}
}
}
out
}
#[test]
fn a_fold_over_an_uploaded_base_matches_the_cpu() {
use isomesh::Sdf;
use isomesh::brush::{Brush, BrushStack};
let gpu = gpu();
let sampler = FieldSampler::new(gpu.device()).expect("pipeline");
let grid = fold_grid();
let base = FieldBuffer::uploaded(gpu.device(), gpu.queue(), grid, &ground_samples(grid))
.expect("base upload");
let out = FieldBuffer::new(gpu.device(), grid);
let log = [
GpuBrush {
shape: GpuShape::Sphere {
center: [0.3, -0.1, 0.2],
radius: 0.6,
},
op: GpuOp::Add,
},
GpuBrush {
shape: GpuShape::BoxExact {
center: [-0.4, 0.2, 0.1],
half_extents: [0.5, 0.3, 0.4],
},
op: GpuOp::Subtract,
},
GpuBrush {
shape: GpuShape::Capsule {
a: [-0.5, 0.1, -0.2],
b: [0.5, 0.3, 0.2],
radius: 0.18,
},
op: GpuOp::SmoothAdd { k: 0.2 },
},
];
sampler
.fold_into(gpu.device(), gpu.queue(), &base, &out, &log)
.expect("fold");
let got = read_buffer(
gpu.device(),
gpu.queue(),
out.buffer(),
grid.field_buffer_size(),
)
.expect("read back");
let ops: Vec<Brush<GpuShape>> = log.iter().map(|b| b.to_cpu()).collect();
let stack = BrushStack {
base: WavyGround,
brushes: &ops,
};
let [sx, sy, sz] = grid.samples();
let mut worst = 0.0f32;
let mut i = 0usize;
for z in 0..sz {
for y in 0..sy {
for x in 0..sx {
worst = worst.max((got[i] - stack.sample(grid.sample_position([x, y, z]))).abs());
i += 1;
}
}
}
assert!(
worst <= 1e-6,
"worst deviation {worst:e} from BrushStack over an uploaded base -- \
too large to be rounding"
);
println!("fold over an uploaded base worst {worst:e}");
}
#[test]
fn an_empty_fold_copies_the_base_bit_for_bit() {
let gpu = gpu();
let sampler = FieldSampler::new(gpu.device()).expect("pipeline");
let grid = fold_grid();
let samples = ground_samples(grid);
let base =
FieldBuffer::uploaded(gpu.device(), gpu.queue(), grid, &samples).expect("base upload");
let out = FieldBuffer::new(gpu.device(), grid);
sampler
.fold_into(gpu.device(), gpu.queue(), &base, &out, &[])
.expect("fold");
let got = read_buffer(
gpu.device(),
gpu.queue(),
out.buffer(),
grid.field_buffer_size(),
)
.expect("read back");
assert_eq!(
got.iter().map(|v| v.to_bits()).collect::<Vec<_>>(),
samples.iter().map(|v| v.to_bits()).collect::<Vec<_>>(),
"an empty fold changed the base it was handed"
);
}
#[test]
fn a_base_of_a_different_size_is_refused() {
let gpu = gpu();
let sampler = FieldSampler::new(gpu.device()).expect("pipeline");
let small = GridParams::new([9; 3], [-2.0; 3], 0.125).expect("grid");
let large = GridParams::new([17; 3], [-2.0; 3], 0.125).expect("grid");
let base = FieldBuffer::new(gpu.device(), small);
let out = FieldBuffer::new(gpu.device(), large);
assert_eq!(
sampler.fold_into(gpu.device(), gpu.queue(), &base, &out, &[]),
Err(crate::Error::SampleCountMismatch {
expected: small.sample_count(),
got: large.sample_count(),
}),
"a fold across two grids was accepted"
);
}