162 lines
5.5 KiB
Rust
162 lines
5.5 KiB
Rust
//! Console primitive types the host lacks. A rectangle list gives three
|
|||
|
|
//! corners per rectangle, the second the one between the other two, and
|
||
|
|
//! the hardware adds the fourth at `v0 + v2 - v1` (Grand Theft Auto VI's
|
||
|
|
//! full-screen rectangles come as top left, bottom left, bottom right); on
|
||
|
|
//! the host each rectangle becomes two triangles, with the fourth vertex
|
||
|
|
//! made from the other three on the CPU.
|
||
|
|
//!
|
||
|
|
//! shortcut: the fourth vertex is computed from vertex inputs, not from
|
||
|
|
//! vertex shader outputs as the hardware does; that is the same for vertex
|
||
|
|
//! shaders linear in their inputs (blits, 2D quads). Inputs in formats
|
||
|
|
//! [`components`] does not know keep the first vertex's value.
|
||
|
|
|
||
|
|
/// Expands `rects` rectangles of `stride`-byte vertices from `src` into six
|
||
|
|
/// vertices each in `dst`: `(v0, v1, v2)` and `(v0, v2, v3)`, so both
|
||
|
|
/// triangles turn the same way. `elements` are (byte offset, format).
|
||
|
|
pub(crate) fn expand_rects(src: &[u8], dst: &mut [u8], stride: usize, elements: &[(u32, u32)]) {
|
||
|
|
for (rect, out) in src
|
||
|
|
.chunks_exact(3 * stride)
|
||
|
|
.zip(dst.chunks_exact_mut(6 * stride))
|
||
|
|
{
|
||
|
|
let v = |i: usize| &rect[i * stride..(i + 1) * stride];
|
||
|
|
for (slot, i) in [0, 1, 2, 0, 2].into_iter().enumerate() {
|
||
|
|
out[slot * stride..(slot + 1) * stride].copy_from_slice(v(i));
|
||
|
|
}
|
||
|
|
let fourth = &mut out[5 * stride..];
|
||
|
|
fourth.copy_from_slice(v(0));
|
||
|
|
for &(offset, format) in elements {
|
||
|
|
let at = offset as usize;
|
||
|
|
let Some((n, kind)) = components(format) else {
|
||
|
|
continue;
|
||
|
|
};
|
||
|
|
for c in 0..n {
|
||
|
|
let get = |i: usize| kind.read(&v(i)[at..], c);
|
||
|
|
let value = get(0) + get(2) - get(1);
|
||
|
|
kind.write(&mut fourth[at..], c, value);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Clone, Copy)]
|
||
|
|
enum Kind {
|
||
|
|
F32,
|
||
|
|
F16,
|
||
|
|
Unorm8,
|
||
|
|
Unorm16,
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Component count and kind of the vertex formats rectangles use.
|
||
|
|
fn components(format: u32) -> Option<(usize, Kind)> {
|
||
|
|
Some(match format {
|
||
|
|
2 => (4, Kind::F32),
|
||
|
|
6 => (3, Kind::F32),
|
||
|
|
16 => (2, Kind::F32),
|
||
|
|
41 => (1, Kind::F32),
|
||
|
|
10 => (4, Kind::F16),
|
||
|
|
34 => (2, Kind::F16),
|
||
|
|
54 => (1, Kind::F16),
|
||
|
|
11 => (4, Kind::Unorm16),
|
||
|
|
35 => (2, Kind::Unorm16),
|
||
|
|
28 | 29 | 87 | 91 => (4, Kind::Unorm8),
|
||
|
|
_ => return None,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Kind {
|
||
|
|
fn bytes(self) -> usize {
|
||
|
|
match self {
|
||
|
|
Kind::F32 => 4,
|
||
|
|
Kind::F16 | Kind::Unorm16 => 2,
|
||
|
|
Kind::Unorm8 => 1,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn read(self, at: &[u8], c: usize) -> f32 {
|
||
|
|
let b = &at[c * self.bytes()..];
|
||
|
|
match self {
|
||
|
|
Kind::F32 => f32::from_le_bytes([b[0], b[1], b[2], b[3]]),
|
||
|
|
Kind::F16 => half_to_f32(u16::from_le_bytes([b[0], b[1]])),
|
||
|
|
Kind::Unorm16 => f32::from(u16::from_le_bytes([b[0], b[1]])) / 65535.0,
|
||
|
|
Kind::Unorm8 => f32::from(b[0]) / 255.0,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn write(self, at: &mut [u8], c: usize, v: f32) {
|
||
|
|
let n = self.bytes();
|
||
|
|
let b = &mut at[c * n..(c + 1) * n];
|
||
|
|
match self {
|
||
|
|
Kind::F32 => b.copy_from_slice(&v.to_le_bytes()),
|
||
|
|
Kind::F16 => b.copy_from_slice(&f32_to_half(v).to_le_bytes()),
|
||
|
|
Kind::Unorm16 => {
|
||
|
|
b.copy_from_slice(&((v.clamp(0.0, 1.0) * 65535.0).round() as u16).to_le_bytes())
|
||
|
|
}
|
||
|
|
Kind::Unorm8 => b[0] = (v.clamp(0.0, 1.0) * 255.0).round() as u8,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn half_to_f32(h: u16) -> f32 {
|
||
|
|
let sign = u32::from(h >> 15) << 31;
|
||
|
|
let exp = u32::from((h >> 10) & 0x1f);
|
||
|
|
let man = u32::from(h & 0x3ff);
|
||
|
|
let bits = match (exp, man) {
|
||
|
|
(0, 0) => sign,
|
||
|
|
// Subnormal: scale the mantissa.
|
||
|
|
(0, m) => {
|
||
|
|
let v = m as f32 * 2f32.powi(-24);
|
||
|
|
return if sign != 0 { -v } else { v };
|
||
|
|
}
|
||
|
|
(31, m) => sign | 0x7f80_0000 | m << 13,
|
||
|
|
(e, m) => sign | (e + 112) << 23 | m << 13,
|
||
|
|
};
|
||
|
|
f32::from_bits(bits)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn f32_to_half(v: f32) -> u16 {
|
||
|
|
let bits = v.to_bits();
|
||
|
|
let sign = ((bits >> 16) & 0x8000) as u16;
|
||
|
|
let exp = ((bits >> 23) & 0xff) as i32 - 127 + 15;
|
||
|
|
let man = bits & 0x7f_ffff;
|
||
|
|
if exp >= 31 {
|
||
|
|
return sign | 0x7c00; // too large: infinity
|
||
|
|
}
|
||
|
|
if exp <= 0 {
|
||
|
|
return sign; // too small: zero
|
||
|
|
}
|
||
|
|
sign | (exp as u16) << 10 | ((man + 0x1000) >> 13) as u16
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(test)]
|
||
|
|
mod tests {
|
||
|
|
use super::*;
|
||
|
|
|
||
|
|
/// A rectangle of float2 positions and RGBA8 colors, given as top
|
||
|
|
/// left, bottom left, bottom right: the fourth corner completes the
|
||
|
|
/// parallelogram at the top right.
|
||
|
|
#[test]
|
||
|
|
fn rectangles_get_their_fourth_corner() {
|
||
|
|
let vertex = |x: f32, y: f32, r: u8| {
|
||
|
|
let mut v = Vec::new();
|
||
|
|
v.extend(x.to_le_bytes());
|
||
|
|
v.extend(y.to_le_bytes());
|
||
|
|
v.extend([r, 0, 0, 255]);
|
||
|
|
v
|
||
|
|
};
|
||
|
|
let src: Vec<u8> = [
|
||
|
|
vertex(-1.0, 1.0, 0),
|
||
|
|
vertex(-1.0, -1.0, 100),
|
||
|
|
vertex(1.0, -1.0, 150),
|
||
|
|
]
|
||
|
|
.concat();
|
||
|
|
let mut dst = vec![0u8; 6 * 12];
|
||
|
|
expand_rects(&src, &mut dst, 12, &[(0, 16), (8, 28)]);
|
||
|
|
assert_eq!(dst[..36], src[..]);
|
||
|
|
assert_eq!(dst[36..48], src[..12]);
|
||
|
|
assert_eq!(dst[48..60], src[24..36]);
|
||
|
|
assert_eq!(dst[60..], vertex(1.0, 1.0, 50)[..]);
|
||
|
|
assert_eq!(f32_to_half(1.5), 0x3e00);
|
||
|
|
assert_eq!(half_to_f32(0x3e00), 1.5);
|
||
|
|
assert_eq!(half_to_f32(0xc000), -2.0);
|
||
|
|
}
|
||
|
|
}
|