use crate::permutation::resource_id;
use crate::{Rd, Result, bad};
const W: &str = "input layout";
pub const RTYPE: u32 = 0x7D98_F632;
const HEADER: usize = 0x20;
const ELEMENT: usize = 20;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Element {
pub first: u32,
pub last: u32,
pub words: [u32; 2],
pub format: u32,
}
impl Element {
pub fn dxgi(&self) -> u8 {
self.format.to_le_bytes()[1]
}
}
pub fn semantic_name(usage: u8) -> &'static str {
match usage {
1 => "POSITION",
6 => "NORMAL",
151 => "COLOR",
_ => "TEXCOORD",
}
}
pub fn dxgi_components(format: u8) -> Option<u8> {
Some(match format {
1..=4 | 9..=14 | 24 | 27..=32 => 4,
5..=8 | 26 => 3,
15..=18 | 33..=38 | 48..=52 => 2,
41..=43 | 53..=58 | 61..=65 => 1,
_ => return None,
})
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Found {
ByName(usize),
ByElimination(usize),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InputLayout {
pub id: u64,
pub elements: Vec<Element>,
pub usages: Vec<u8>,
}
impl InputLayout {
pub fn resolve(&self, inputs: &[(&str, u32)]) -> Vec<Option<Found>> {
let of_index = |index: u32| {
(self.elements.iter().enumerate()).filter_map(move |(i, e)| (e.first == index).then_some(i))
};
let usage_name = |i: usize| self.usages.get(i).map(|&u| semantic_name(u));
let mut found: Vec<Option<Found>> = inputs
.iter()
.map(|&(name, index)| {
let mut named = of_index(index).filter(|&i| usage_name(i) == Some(name));
let one = named.next()?;
named.next().is_none().then_some(Found::ByName(one))
})
.collect();
let mut taken: Vec<usize> = found
.iter()
.filter_map(|f| match f {
Some(Found::ByName(i) | Found::ByElimination(i)) => Some(*i),
None => None,
})
.collect();
for (slot, &(_, index)) in found.iter_mut().zip(inputs) {
if slot.is_none() {
let mut left = of_index(index).filter(|i| !taken.contains(i));
if let (Some(one), None) = (left.next(), left.next()) {
*slot = Some(Found::ByElimination(one));
taken.push(one);
}
}
}
found
}
}
pub fn read(d: &[u8], meta: &[u8; 16]) -> Result<InputLayout> {
let m = Rd::new(meta, W);
let (size, reloc) = (m.u32le(0)? as usize, m.u32le(4)? as usize);
if size.checked_add(reloc) != Some(d.len()) {
return Err(bad(
W,
0,
format!("size {size} + relocations {reloc} != {}", d.len()),
));
}
let r = Rd::new(d, W);
let n = r.u32le(0)? as usize;
if n == 0 || n > 32 {
return Err(bad(W, 0, format!("{n} elements")));
}
let usages_at = HEADER.saturating_add(ELEMENT.saturating_mul(n).next_multiple_of(16));
if r.u64le(4)? != HEADER as u64
|| r.u64le(0xc)? != usages_at as u64
|| r.bytes(0x14, 12)?.iter().any(|&b| b != 0)
{
return Err(bad(
W,
4,
"pointers are not the elements at 0x20 and the usages after them",
));
}
if size != usages_at.saturating_add(n.next_multiple_of(16))
|| reloc != 8
|| r.u32le(size)? != 4
|| r.u32le(crate::at(size, 1, 4))? != 0xc
{
return Err(bad(
W,
size,
format!("{size} bytes for {n} elements, or relocations other than the two pointers"),
));
}
let elements = (0..n)
.map(|i| {
let at = crate::at(HEADER, i, ELEMENT);
Ok(Element {
first: r.u32le(at)?,
last: r.u32le(crate::at(at, 1, 4))?,
words: [r.u32le(crate::at(at, 2, 4))?, r.u32le(crate::at(at, 3, 4))?],
format: r.u32le(crate::at(at, 4, 4))?,
})
})
.collect::<Result<Vec<_>>>()?;
Ok(InputLayout {
id: resource_id(meta),
elements,
usages: r.bytes(usages_at, n)?.to_vec(),
})
}