primitive.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //! Console primitive types the host lacks. A rectangle list gives three
  2. //! corners per rectangle, the second the one between the other two, and
  3. //! the hardware adds the fourth at `v0 + v2 - v1` (Grand Theft Auto VI's
  4. //! full-screen rectangles come as top left, bottom left, bottom right); on
  5. //! the host each rectangle becomes two triangles, with the fourth vertex
  6. //! made from the other three on the CPU.
  7. //!
  8. //! shortcut: the fourth vertex is computed from vertex inputs, not from
  9. //! vertex shader outputs as the hardware does; that is the same for vertex
  10. //! shaders linear in their inputs (blits, 2D quads). Inputs in formats
  11. //! [`components`] does not know keep the first vertex's value.
  12. /// Expands `rects` rectangles of `stride`-byte vertices from `src` into six
  13. /// vertices each in `dst`: `(v0, v1, v2)` and `(v0, v2, v3)`, so both
  14. /// triangles turn the same way. `elements` are (byte offset, format).
  15. pub(crate) fn expand_rects(src: &[u8], dst: &mut [u8], stride: usize, elements: &[(u32, u32)]) {
  16. for (rect, out) in src
  17. .chunks_exact(3 * stride)
  18. .zip(dst.chunks_exact_mut(6 * stride))
  19. {
  20. let v = |i: usize| &rect[i * stride..(i + 1) * stride];
  21. for (slot, i) in [0, 1, 2, 0, 2].into_iter().enumerate() {
  22. out[slot * stride..(slot + 1) * stride].copy_from_slice(v(i));
  23. }
  24. let fourth = &mut out[5 * stride..];
  25. fourth.copy_from_slice(v(0));
  26. for &(offset, format) in elements {
  27. let at = offset as usize;
  28. let Some((n, kind)) = components(format) else {
  29. continue;
  30. };
  31. for c in 0..n {
  32. let get = |i: usize| kind.read(&v(i)[at..], c);
  33. let value = get(0) + get(2) - get(1);
  34. kind.write(&mut fourth[at..], c, value);
  35. }
  36. }
  37. }
  38. }
  39. #[derive(Clone, Copy)]
  40. enum Kind {
  41. F32,
  42. F16,
  43. Unorm8,
  44. Unorm16,
  45. }
  46. /// Component count and kind of the vertex formats rectangles use.
  47. fn components(format: u32) -> Option<(usize, Kind)> {
  48. Some(match format {
  49. 2 => (4, Kind::F32),
  50. 6 => (3, Kind::F32),
  51. 16 => (2, Kind::F32),
  52. 41 => (1, Kind::F32),
  53. 10 => (4, Kind::F16),
  54. 34 => (2, Kind::F16),
  55. 54 => (1, Kind::F16),
  56. 11 => (4, Kind::Unorm16),
  57. 35 => (2, Kind::Unorm16),
  58. 28 | 29 | 87 | 91 => (4, Kind::Unorm8),
  59. _ => return None,
  60. })
  61. }
  62. impl Kind {
  63. fn bytes(self) -> usize {
  64. match self {
  65. Kind::F32 => 4,
  66. Kind::F16 | Kind::Unorm16 => 2,
  67. Kind::Unorm8 => 1,
  68. }
  69. }
  70. fn read(self, at: &[u8], c: usize) -> f32 {
  71. let b = &at[c * self.bytes()..];
  72. match self {
  73. Kind::F32 => f32::from_le_bytes([b[0], b[1], b[2], b[3]]),
  74. Kind::F16 => half_to_f32(u16::from_le_bytes([b[0], b[1]])),
  75. Kind::Unorm16 => f32::from(u16::from_le_bytes([b[0], b[1]])) / 65535.0,
  76. Kind::Unorm8 => f32::from(b[0]) / 255.0,
  77. }
  78. }
  79. fn write(self, at: &mut [u8], c: usize, v: f32) {
  80. let n = self.bytes();
  81. let b = &mut at[c * n..(c + 1) * n];
  82. match self {
  83. Kind::F32 => b.copy_from_slice(&v.to_le_bytes()),
  84. Kind::F16 => b.copy_from_slice(&f32_to_half(v).to_le_bytes()),
  85. Kind::Unorm16 => {
  86. b.copy_from_slice(&((v.clamp(0.0, 1.0) * 65535.0).round() as u16).to_le_bytes())
  87. }
  88. Kind::Unorm8 => b[0] = (v.clamp(0.0, 1.0) * 255.0).round() as u8,
  89. }
  90. }
  91. }
  92. fn half_to_f32(h: u16) -> f32 {
  93. let sign = u32::from(h >> 15) << 31;
  94. let exp = u32::from((h >> 10) & 0x1f);
  95. let man = u32::from(h & 0x3ff);
  96. let bits = match (exp, man) {
  97. (0, 0) => sign,
  98. // Subnormal: scale the mantissa.
  99. (0, m) => {
  100. let v = m as f32 * 2f32.powi(-24);
  101. return if sign != 0 { -v } else { v };
  102. }
  103. (31, m) => sign | 0x7f80_0000 | m << 13,
  104. (e, m) => sign | (e + 112) << 23 | m << 13,
  105. };
  106. f32::from_bits(bits)
  107. }
  108. fn f32_to_half(v: f32) -> u16 {
  109. let bits = v.to_bits();
  110. let sign = ((bits >> 16) & 0x8000) as u16;
  111. let exp = ((bits >> 23) & 0xff) as i32 - 127 + 15;
  112. let man = bits & 0x7f_ffff;
  113. if exp >= 31 {
  114. return sign | 0x7c00; // too large: infinity
  115. }
  116. if exp <= 0 {
  117. return sign; // too small: zero
  118. }
  119. sign | (exp as u16) << 10 | ((man + 0x1000) >> 13) as u16
  120. }
  121. #[cfg(test)]
  122. mod tests {
  123. use super::*;
  124. /// A rectangle of float2 positions and RGBA8 colors, given as top
  125. /// left, bottom left, bottom right: the fourth corner completes the
  126. /// parallelogram at the top right.
  127. #[test]
  128. fn rectangles_get_their_fourth_corner() {
  129. let vertex = |x: f32, y: f32, r: u8| {
  130. let mut v = Vec::new();
  131. v.extend(x.to_le_bytes());
  132. v.extend(y.to_le_bytes());
  133. v.extend([r, 0, 0, 255]);
  134. v
  135. };
  136. let src: Vec<u8> = [
  137. vertex(-1.0, 1.0, 0),
  138. vertex(-1.0, -1.0, 100),
  139. vertex(1.0, -1.0, 150),
  140. ]
  141. .concat();
  142. let mut dst = vec![0u8; 6 * 12];
  143. expand_rects(&src, &mut dst, 12, &[(0, 16), (8, 28)]);
  144. assert_eq!(dst[..36], src[..]);
  145. assert_eq!(dst[36..48], src[..12]);
  146. assert_eq!(dst[48..60], src[24..36]);
  147. assert_eq!(dst[60..], vertex(1.0, 1.0, 50)[..]);
  148. assert_eq!(f32_to_half(1.5), 0x3e00);
  149. assert_eq!(half_to_f32(0x3e00), 1.5);
  150. assert_eq!(half_to_f32(0xc000), -2.0);
  151. }
  152. }