use super::*;
use nsi_ffi_wrap::nsi_sys::NSIParamFlags;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Interpolation {
Constant,
Uniform,
Vertex,
FaceVarying,
}
#[derive(Debug, Clone)]
pub struct PrimitiveVariable<'a> {
values: &'a OwnedArgument,
interpolation: Interpolation,
indices: Option<&'a [i32]>,
vertex_indices: Option<&'a [i32]>,
face_vertex_counts: Vec<usize>,
}
impl<'a> PrimitiveVariable<'a> {
#[must_use]
pub fn values(&self) -> &'a OwnedArgument {
self.values
}
#[must_use]
pub fn interpolation(&self) -> Interpolation {
self.interpolation
}
#[must_use]
pub fn indices(&self) -> Option<&'a [i32]> {
self.indices
}
#[must_use]
pub fn face_vertex_count(&self) -> usize {
self.face_vertex_counts.iter().sum()
}
pub fn face_varying_indices(&self) -> impl Iterator<Item = usize> + '_ {
let mut face_vertex = 0usize;
self.face_vertex_counts
.iter()
.enumerate()
.flat_map(move |(face, count)| {
let start = face_vertex;
face_vertex += count;
(0..*count).map(move |corner| (face, start + corner))
})
.map(move |(face, position)| match self.interpolation {
Interpolation::Constant => 0,
Interpolation::Uniform => face,
Interpolation::FaceVarying => self
.indices
.map_or(position, |indices| indices[position] as usize),
Interpolation::Vertex => self.indices.map_or_else(
|| {
self.vertex_indices.map_or(position, |vertices| {
vertices[position] as usize
})
},
|indices| indices[position] as usize,
),
})
}
}
impl Scene {
pub fn primitive_variable(
&self,
handle: &str,
name: &str,
) -> Result<Option<PrimitiveVariable<'_>>, ResolveError> {
let Some(node) = self.node(handle) else {
return Ok(None);
};
let Some(values) = node.effective(name) else {
return Ok(None);
};
let face_vertex_counts: Vec<usize> =
self.faces(handle)?.map(|face| face.vertices()).collect();
let face_vertices: usize = face_vertex_counts.iter().sum();
let faces = face_vertex_counts.len();
let points = node
.effective(POSITIONS)
.map_or(0, OwnedArgument::element_count);
let vertex_indices = node
.effective(&format!("{POSITIONS}{INDICES}"))
.and_then(OwnedArgument::as_i32s);
let count = values.element_count();
let indices = node
.effective(&format!("{name}{INDICES}"))
.and_then(OwnedArgument::as_i32s);
if let Some(indices) = indices
&& (indices.len() != face_vertices
|| indices.iter().any(|index| *index as usize >= count))
{
return Err(ResolveError::MalformedIndices {
handle: handle.to_string(),
attribute: name.to_string(),
indices: indices.len(),
face_vertices,
values: count,
});
}
let flags = NSIParamFlags::from_bits_truncate(values.flags);
let per_face = flags.contains(NSIParamFlags::PerFace) && count == faces;
let per_vertex =
flags.contains(NSIParamFlags::PerVertex) && count == points;
if count == faces
&& count == points
&& count != face_vertices
&& !per_face
&& !per_vertex
{
return Err(ResolveError::AmbiguousInterpolation {
handle: handle.to_string(),
attribute: name.to_string(),
values: count,
faces,
vertices: points,
face_vertices,
});
}
let interpolation = if indices.is_some() {
Interpolation::FaceVarying
} else if per_face
|| (!per_vertex && count == faces && count != face_vertices)
{
Interpolation::Uniform
} else if !per_vertex && count == face_vertices {
Interpolation::FaceVarying
} else if count == points && points != 0 {
Interpolation::Vertex
} else if count == 1 {
Interpolation::Constant
} else {
return Err(ResolveError::AmbiguousInterpolation {
handle: handle.to_string(),
attribute: name.to_string(),
values: count,
faces,
vertices: points,
face_vertices,
});
};
Ok(Some(PrimitiveVariable {
values,
interpolation,
indices,
vertex_indices,
face_vertex_counts,
}))
}
}