use super::patch_basis::evaluate_patch_basis;
use super::types::{patch_descriptor, PatchArray, PatchCoord, PatchParam};
use crate::far::patch_table::PatchRef;
use crate::far::{PatchTable, Primvar, StencilTable};
use crate::vtr::TopologyError;
use crate::{Index, INDEX_INVALID};
#[derive(Debug, Clone)]
pub struct GpuPatchTable {
arrays: Vec<PatchArray>,
indices: Vec<Index>,
params: Vec<PatchParam>,
num_control_values: usize,
local_point_stencils: StencilTable,
}
impl GpuPatchTable {
pub fn new(table: &PatchTable) -> Result<GpuPatchTable, TopologyError> {
let num_control_values = table.num_control_values();
let ptex = table.ptex_indices();
assert!(
ptex.num_faces() <= 1 << 28,
"{} ptex faces exceed the 2^28 a packed patch parameter can address",
ptex.num_faces()
);
let mut arrays: Vec<PatchArray> = Vec::new();
let mut indices: Vec<Index> = Vec::with_capacity(table.num_patches() * 16);
let mut params = Vec::with_capacity(table.num_patches());
let mut local_points: Vec<Vec<(Index, f32)>> = Vec::new();
for patch in 0..table.num_patches() {
let desc = patch_descriptor::from_patch_type(table.patch_type(patch));
if arrays.last().is_none_or(|array| array.desc != desc) {
arrays.push(PatchArray {
reg_desc: desc,
desc,
num_patches: 0,
index_base: indices.len() as u32,
stride: patch_descriptor::num_control_vertices(desc) as u32,
primitive_id_base: patch as u32,
});
}
arrays.last_mut().unwrap().num_patches += 1;
let far_param = table.patch_param(patch);
if u32::from(far_param.depth) > PatchParam::MAX_DEPTH {
return Err(TopologyError::PatchDepthTooDeep {
depth: far_param.depth as usize,
max: PatchParam::MAX_DEPTH as usize,
});
}
let depth = u32::from(far_param.depth);
let n = 1i64 << depth;
let rotation = u32::from(far_param.rotation % 4);
let corner = CORNER[rotation as usize];
let cell = [0, 1].map(|k| (far_param.origin[k] * n as f32).round() as i64 - corner[k]);
debug_assert!(cell.iter().all(|&c| (0..n).contains(&c)));
let (u, v) = if far_param.triangular && rotation == 2 {
(n - 1 - cell[0], n - 1 - cell[1])
} else {
(cell[0], cell[1])
};
let quad_rotation = if far_param.triangular { 0 } else { rotation };
let base_face = ptex.base_face(far_param.ptex_face as usize) as usize;
let non_quad_root = !far_param.triangular && ptex.face_ptex_count(base_face) > 1;
let mut boundary = 0;
let mut sharpness = 0.0;
let mut regular = false;
let mut end_cap = |range, num_points: usize, permute: &dyn Fn(usize) -> usize| {
let first = local_points.len();
local_points.resize(first + num_points, Vec::new());
table.for_each_end_cap_entry(range, num_points, |point, cv, w| {
local_points[first + permute(point)].push((cv, w));
});
for k in 0..num_points {
indices.push((num_control_values + first + k) as Index);
}
};
match table.patch_ref(patch) {
PatchRef::Regular(cvs) => {
let grid = rotate_grid(cvs, quad_rotation);
let phantom = |slot: usize| grid[slot] == INDEX_INVALID;
boundary = phantom(1) as u32
| (phantom(7) as u32) << 1
| (phantom(13) as u32) << 2
| (phantom(4) as u32) << 3;
for (slot, &cv) in grid.iter().enumerate() {
indices.push(if cv == INDEX_INVALID {
let (i, j) = (slot % 4, slot / 4);
grid[4 * j.clamp(1, 2) + i.clamp(1, 2)]
} else {
cv
});
}
regular = true;
}
PatchRef::SingleCrease {
cvs,
edge,
sharpness: crease,
} => {
indices.extend_from_slice(&rotate_grid(cvs, quad_rotation));
boundary = 1 << ((u32::from(edge) + quad_rotation) % 4);
sharpness = crease;
regular = true;
}
PatchRef::Gregory(range) => {
let permute = |p: usize| 5 * ((p / 5 + quad_rotation as usize) % 4) + p % 5;
end_cap(range, 20, &permute);
}
PatchRef::Quads(cvs) => {
let mut out = [0; 4];
for (k, &cv) in cvs.iter().enumerate() {
out[(k + quad_rotation as usize) % 4] = cv;
}
indices.extend_from_slice(&out);
}
PatchRef::Loop(cvs) => {
boundary = box_spline_boundary_mask(cvs);
indices.extend(
cvs.iter()
.map(|&cv| if cv == INDEX_INVALID { cvs[4] } else { cv }),
);
regular = true;
}
PatchRef::GregoryTriangle(range) => end_cap(range, 18, &|p| p),
PatchRef::Triangles(cvs) => indices.extend_from_slice(cvs),
}
params.push(PatchParam::new(
far_param.ptex_face,
u as u32,
v as u32,
depth + non_quad_root as u32,
non_quad_root,
boundary,
0,
regular,
sharpness,
));
}
Ok(GpuPatchTable {
arrays,
indices,
params,
num_control_values,
local_point_stencils: StencilTable::from_stencils(num_control_values, local_points),
})
}
pub fn patch_arrays(&self) -> &[PatchArray] {
&self.arrays
}
pub fn patch_index_buffer(&self) -> &[Index] {
&self.indices
}
pub fn patch_param_buffer(&self) -> &[PatchParam] {
&self.params
}
pub fn patch_array_words(&self) -> Vec<u32> {
self.arrays.iter().flat_map(PatchArray::to_words).collect()
}
pub fn patch_param_words(&self) -> Vec<u32> {
self.params.iter().flat_map(PatchParam::to_words).collect()
}
pub fn num_patches(&self) -> usize {
self.params.len()
}
pub fn num_control_values(&self) -> usize {
self.num_control_values
}
pub fn num_local_points(&self) -> usize {
self.local_point_stencils.num_stencils()
}
pub fn local_point_stencils(&self) -> &StencilTable {
&self.local_point_stencils
}
pub fn patch_coord(&self, patch: usize, u: f32, v: f32) -> PatchCoord {
assert!(
patch < self.num_patches(),
"patch {patch} out of range ({} patches)",
self.num_patches()
);
let array_index = self
.arrays
.partition_point(|array| array.primitive_id_base as usize <= patch)
- 1;
let array = &self.arrays[array_index];
PatchCoord {
array_index: array_index as u32,
patch_index: patch as u32,
vert_index: array.index_base + (patch as u32 - array.primitive_id_base) * array.stride,
s: u,
t: v,
}
}
pub fn evaluate<T: Primvar>(&self, coord: &PatchCoord, values: &[T]) -> (T, T, T) {
assert!(
values.len() >= self.num_control_values + self.num_local_points(),
"{} values given, {} needed",
values.len(),
self.num_control_values + self.num_local_points()
);
let array = &self.arrays[coord.array_index as usize];
let param = &self.params[coord.patch_index as usize];
let weights = evaluate_patch_basis(array.desc, param, coord.s, coord.t)
.expect("tables hold known patch types");
let cvs = &self.indices[coord.vert_index as usize..][..weights.num_points];
let mut point = values[0].clone();
point.clear();
let (mut du, mut dv) = (point.clone(), point.clone());
for (k, &cv) in cvs.iter().enumerate() {
let value = &values[cv as usize];
point.add_with_weight(value, weights.point[k]);
du.add_with_weight(value, weights.du[k]);
dv.add_with_weight(value, weights.dv[k]);
}
(point, du, dv)
}
}
const CORNER: [[i64; 2]; 4] = [[0, 0], [1, 0], [1, 1], [0, 1]];
fn rotate_grid(cvs: &[Index; 16], rotation: u32) -> [Index; 16] {
let corner = CORNER[rotation as usize];
let mut out = [INDEX_INVALID; 16];
for (slot, &cv) in cvs.iter().enumerate() {
let (x, y) = ((slot % 4) as i64 - 1, (slot / 4) as i64 - 1);
let (rx, ry) = match rotation {
0 => (x, y),
1 => (-y, x),
2 => (-x, -y),
_ => (y, -x),
};
let (i, j) = (corner[0] + rx + 1, corner[1] + ry + 1);
out[(4 * j + i) as usize] = cv;
}
out
}
fn box_spline_boundary_mask(cvs: &[Index; 12]) -> u32 {
let phantom = |slot: usize| cvs[slot] == INDEX_INVALID;
let edges = phantom(1) as u32 | (phantom(9) as u32) << 1 | (phantom(7) as u32) << 2;
let e = |k: u32| edges & (1 << k) != 0;
let vertices = (!e(0) && !e(2) && phantom(0)) as u32
| ((!e(0) && !e(1) && phantom(2)) as u32) << 1
| ((!e(1) && !e(2) && phantom(10)) as u32) << 2;
match (edges, vertices) {
(0, 0) => 0,
(0, v) => v | 1 << 3,
(e, 0) => e,
(e, v) => {
debug_assert_eq!(
v,
((e & 1) << 2) | (e >> 1),
"a vertex wall opposite the edge"
);
e | 2 << 3
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rotated_grids_keep_the_face_in_the_center() {
let cvs: [Index; 16] = std::array::from_fn(|k| k as Index);
let corners = [5usize, 6, 10, 9];
for r in 0..4u32 {
let out = rotate_grid(&cvs, r);
for k in 0..4 {
assert_eq!(out[corners[(k + r as usize) % 4]], corners[k] as Index);
}
let mut sorted = out;
sorted.sort();
assert_eq!(sorted, cvs);
}
}
}