use crate::far::PatchType;
pub mod patch_descriptor {
use crate::far::PatchType;
pub const QUADS: u32 = 3;
pub const TRIANGLES: u32 = 4;
pub const LOOP: u32 = 5;
pub const REGULAR: u32 = 6;
pub const GREGORY_BASIS: u32 = 9;
pub const GREGORY_TRIANGLE: u32 = 10;
pub const fn from_patch_type(patch_type: PatchType) -> u32 {
match patch_type {
PatchType::Regular => REGULAR,
PatchType::GregoryBasis => GREGORY_BASIS,
PatchType::Quads => QUADS,
PatchType::Loop => LOOP,
PatchType::GregoryTriangle => GREGORY_TRIANGLE,
PatchType::Triangles => TRIANGLES,
}
}
pub const fn to_patch_type(code: u32) -> Option<PatchType> {
match code {
REGULAR => Some(PatchType::Regular),
GREGORY_BASIS => Some(PatchType::GregoryBasis),
QUADS => Some(PatchType::Quads),
LOOP => Some(PatchType::Loop),
GREGORY_TRIANGLE => Some(PatchType::GregoryTriangle),
TRIANGLES => Some(PatchType::Triangles),
_ => None,
}
}
pub const fn num_control_vertices(code: u32) -> usize {
match code {
REGULAR => 16,
GREGORY_BASIS => 20,
QUADS => 4,
LOOP => 12,
GREGORY_TRIANGLE => 18,
TRIANGLES => 3,
_ => 0,
}
}
pub const fn is_triangular(code: u32) -> bool {
matches!(code, LOOP | GREGORY_TRIANGLE | TRIANGLES)
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PatchArray {
pub reg_desc: u32,
pub desc: u32,
pub num_patches: u32,
pub index_base: u32,
pub stride: u32,
pub primitive_id_base: u32,
}
impl PatchArray {
pub fn patch_type(&self) -> Option<PatchType> {
patch_descriptor::to_patch_type(self.desc)
}
pub fn to_words(&self) -> [u32; 6] {
[
self.reg_desc,
self.desc,
self.num_patches,
self.index_base,
self.stride,
self.primitive_id_base,
]
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PatchParam {
pub field0: u32,
pub field1: u32,
pub sharpness: f32,
}
impl PatchParam {
pub const MAX_DEPTH: u32 = 10;
#[allow(clippy::too_many_arguments)]
pub fn new(
face_id: u32,
u: u32,
v: u32,
depth: u32,
non_quad_root: bool,
boundary: u32,
transition: u32,
regular: bool,
sharpness: f32,
) -> Self {
let pack = |value: u32, width: u32, offset: u32| (value & ((1 << width) - 1)) << offset;
PatchParam {
field0: pack(face_id, 28, 0) | pack(transition, 4, 28),
field1: pack(u, 10, 22)
| pack(v, 10, 12)
| pack(boundary, 5, 7)
| pack(regular as u32, 1, 5)
| pack(non_quad_root as u32, 1, 4)
| pack(depth, 4, 0),
sharpness,
}
}
pub fn face_id(&self) -> u32 {
self.field0 & 0x0fff_ffff
}
pub fn transition(&self) -> u32 {
self.field0 >> 28
}
pub fn u(&self) -> u32 {
(self.field1 >> 22) & 0x3ff
}
pub fn v(&self) -> u32 {
(self.field1 >> 12) & 0x3ff
}
pub fn boundary(&self) -> u32 {
(self.field1 >> 7) & 0x1f
}
pub fn is_regular(&self) -> bool {
(self.field1 >> 5) & 1 != 0
}
pub fn non_quad_root(&self) -> bool {
(self.field1 >> 4) & 1 != 0
}
pub fn depth(&self) -> u32 {
self.field1 & 0xf
}
pub fn param_fraction(&self) -> f32 {
1.0 / (1u32 << (self.depth() - self.non_quad_root() as u32)) as f32
}
pub fn is_triangle_rotated(&self) -> bool {
self.u() + self.v() >= 1 << self.depth()
}
pub fn normalize(&self, u: f32, v: f32) -> (f32, f32) {
let inv = 1.0 / self.param_fraction();
(u * inv - self.u() as f32, v * inv - self.v() as f32)
}
pub fn unnormalize(&self, s: f32, t: f32) -> (f32, f32) {
let frac = self.param_fraction();
((s + self.u() as f32) * frac, (t + self.v() as f32) * frac)
}
pub fn normalize_triangle(&self, u: f32, v: f32) -> (f32, f32) {
if self.is_triangle_rotated() {
let inv = 1.0 / self.param_fraction();
let n = (1u32 << self.depth()) as f32;
(n - self.u() as f32 - u * inv, n - self.v() as f32 - v * inv)
} else {
self.normalize(u, v)
}
}
pub fn unnormalize_triangle(&self, s: f32, t: f32) -> (f32, f32) {
if self.is_triangle_rotated() {
let frac = self.param_fraction();
let n = (1u32 << self.depth()) as f32;
(
(n - self.u() as f32 - s) * frac,
(n - self.v() as f32 - t) * frac,
)
} else {
self.unnormalize(s, t)
}
}
pub fn to_words(&self) -> [u32; 3] {
[self.field0, self.field1, self.sharpness.to_bits()]
}
}
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PatchCoord {
pub array_index: u32,
pub patch_index: u32,
pub vert_index: u32,
pub s: f32,
pub t: f32,
}
impl PatchCoord {
pub fn to_words(&self) -> [u32; 5] {
[
self.array_index,
self.patch_index,
self.vert_index,
self.s.to_bits(),
self.t.to_bits(),
]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn records_have_the_shader_layouts() {
assert_eq!(std::mem::size_of::<PatchArray>(), 24);
assert_eq!(std::mem::size_of::<PatchParam>(), 12);
assert_eq!(std::mem::size_of::<PatchCoord>(), 20);
}
#[test]
fn patch_param_round_trips_its_fields() {
let p = PatchParam::new(0x0abc_def1, 1023, 517, 11, true, 0x15, 0xa, true, 2.5);
assert_eq!(p.face_id(), 0x0abc_def1);
assert_eq!(p.u(), 1023);
assert_eq!(p.v(), 517);
assert_eq!(p.depth(), 11);
assert!(p.non_quad_root());
assert_eq!(p.boundary(), 0x15);
assert_eq!(p.transition(), 0xa);
assert!(p.is_regular());
assert_eq!(p.to_words()[2], 2.5f32.to_bits());
let q = PatchParam::new(7, 3, 5, 2, false, 0b1001, 0, false, 0.0);
assert_eq!(q.field0, 7);
assert_eq!(q.field1, 3 << 22 | 5 << 12 | 0b1001 << 7 | 2);
}
#[test]
fn triangle_normalization_inverts() {
let p = PatchParam::new(0, 1, 1, 1, false, 0, 0, true, 0.0);
assert!(p.is_triangle_rotated());
assert_eq!(p.unnormalize_triangle(0.0, 0.0), (0.5, 0.5));
assert_eq!(p.unnormalize_triangle(1.0, 0.0), (0.0, 0.5));
let (s, t) = p.normalize_triangle(0.3, 0.4);
assert!((s - 0.4).abs() < 1e-6 && (t - 0.2).abs() < 1e-6);
}
#[test]
fn descriptor_codes_round_trip() {
for t in [
PatchType::Regular,
PatchType::GregoryBasis,
PatchType::Quads,
PatchType::Loop,
PatchType::GregoryTriangle,
PatchType::Triangles,
] {
let code = patch_descriptor::from_patch_type(t);
assert_eq!(patch_descriptor::to_patch_type(code), Some(t));
assert!(patch_descriptor::num_control_vertices(code) > 0);
}
assert_eq!(patch_descriptor::to_patch_type(0), None);
}
}