2
0

dxil.rs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //! The DXIL program inside a container's `DXIL` (or `STAT`) part.
  2. //!
  3. //! ```text
  4. //! 0x00 program version u32 (kind << 16 | major << 4 | minor)
  5. //! 0x04 size in dwords u32
  6. //! 0x08 "DXIL" <- bitcode offset is relative to here
  7. //! 0x0C dxil version u32
  8. //! 0x10 bitcode offset u32 (16 in standard PC DXIL)
  9. //! 0x14 bitcode size u32
  10. //! ```
  11. use crate::ShaderError;
  12. use crate::dxbc::u32_at;
  13. const LLVM_BITCODE_MAGIC: [u8; 4] = [0x42, 0x43, 0xC0, 0xDE];
  14. #[cfg(test)]
  15. const STANDARD_BITCODE_OFFSET: u32 = 16;
  16. #[derive(Debug, Clone, Copy, PartialEq, Eq)]
  17. pub enum ShaderKind {
  18. Pixel,
  19. Vertex,
  20. Geometry,
  21. Hull,
  22. Domain,
  23. Compute,
  24. Library,
  25. RayGeneration,
  26. Intersection,
  27. AnyHit,
  28. ClosestHit,
  29. Miss,
  30. Callable,
  31. Mesh,
  32. Amplification,
  33. Node,
  34. Unknown(u16),
  35. }
  36. impl ShaderKind {
  37. fn from_raw(v: u16) -> Self {
  38. use ShaderKind::*;
  39. match v {
  40. 0 => Pixel,
  41. 1 => Vertex,
  42. 2 => Geometry,
  43. 3 => Hull,
  44. 4 => Domain,
  45. 5 => Compute,
  46. 6 => Library,
  47. 7 => RayGeneration,
  48. 8 => Intersection,
  49. 9 => AnyHit,
  50. 10 => ClosestHit,
  51. 11 => Miss,
  52. 12 => Callable,
  53. 13 => Mesh,
  54. 14 => Amplification,
  55. 15 => Node,
  56. other => Unknown(other),
  57. }
  58. }
  59. }
  60. #[derive(Debug, Clone)]
  61. pub struct DxilProgram<'a> {
  62. pub kind: ShaderKind,
  63. pub shader_model: (u8, u8),
  64. pub dxil_version: u32,
  65. /// LLVM bitcode (starts with `BC C0 DE`).
  66. pub bitcode: &'a [u8],
  67. /// Bytes between the bitcode header and the bitcode. Empty on PC.
  68. pub prefix: &'a [u8],
  69. /// Bytes after the bitcode, up to the program size. Empty on PC.
  70. pub trailer: &'a [u8],
  71. }
  72. /// Xbox additions recognised around the bitcode.
  73. #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
  74. pub struct XboxExtras {
  75. /// The prefix starts with an `XDAT` table.
  76. pub xdat: bool,
  77. /// The trailer contains an `xbop` blob (compiler options).
  78. pub xbop: bool,
  79. }
  80. impl<'a> DxilProgram<'a> {
  81. pub fn parse(part: &'a [u8]) -> Result<Self, ShaderError> {
  82. let version = u32_at(part, 0)?;
  83. let size = (u32_at(part, 4)? as usize)
  84. .checked_mul(4)
  85. .filter(|&s| s <= part.len() && s >= 24)
  86. .ok_or(ShaderError::Malformed(
  87. "program size disagrees with part size",
  88. ))?;
  89. let part = &part[..size];
  90. if part.get(8..12) != Some(b"DXIL") {
  91. return Err(ShaderError::Malformed("missing DXIL magic"));
  92. }
  93. let dxil_version = u32_at(part, 12)?;
  94. let offset = u32_at(part, 16)?;
  95. let len = u32_at(part, 20)? as usize;
  96. let start = 8 + offset as usize;
  97. let bitcode = part
  98. .get(start..start + len)
  99. .ok_or(ShaderError::Malformed("bitcode out of bounds"))?;
  100. if bitcode.get(0..4) != Some(&LLVM_BITCODE_MAGIC) {
  101. return Err(ShaderError::Malformed("bitcode lacks LLVM magic"));
  102. }
  103. let prefix = part
  104. .get(24..start)
  105. .ok_or(ShaderError::Malformed("bitcode offset too small"))?;
  106. Ok(DxilProgram {
  107. kind: ShaderKind::from_raw((version >> 16) as u16),
  108. shader_model: (((version >> 4) & 0xF) as u8, (version & 0xF) as u8),
  109. dxil_version,
  110. bitcode,
  111. prefix,
  112. trailer: &part[start + len..],
  113. })
  114. }
  115. /// True for a program laid out exactly like PC DXIL.
  116. pub fn is_standard_layout(&self) -> bool {
  117. self.prefix.is_empty() && self.trailer.is_empty()
  118. }
  119. pub fn xbox_extras(&self) -> XboxExtras {
  120. XboxExtras {
  121. xdat: self.prefix.starts_with(b"XDAT"),
  122. xbop: self.trailer.windows(4).any(|w| w == b"xbop"),
  123. }
  124. }
  125. }
  126. /// Builds a standard PC-layout program around `bitcode` (test helper).
  127. #[cfg(test)]
  128. pub fn build_standard(kind: u16, sm: (u8, u8), dxil_version: u32, bitcode: &[u8]) -> Vec<u8> {
  129. let mut out = Vec::new();
  130. let version = (u32::from(kind) << 16) | (u32::from(sm.0) << 4) | u32::from(sm.1);
  131. let padded = bitcode.len().next_multiple_of(4);
  132. let size_dwords = (24 + padded) / 4;
  133. out.extend_from_slice(&version.to_le_bytes());
  134. out.extend_from_slice(&(size_dwords as u32).to_le_bytes());
  135. out.extend_from_slice(b"DXIL");
  136. out.extend_from_slice(&dxil_version.to_le_bytes());
  137. out.extend_from_slice(&STANDARD_BITCODE_OFFSET.to_le_bytes());
  138. out.extend_from_slice(&(bitcode.len() as u32).to_le_bytes());
  139. out.extend_from_slice(bitcode);
  140. out.resize(24 + padded, 0);
  141. out
  142. }
  143. #[cfg(test)]
  144. mod tests {
  145. use super::*;
  146. fn fake_bitcode() -> Vec<u8> {
  147. let mut b = LLVM_BITCODE_MAGIC.to_vec();
  148. b.extend_from_slice(&[0xAA; 12]);
  149. b
  150. }
  151. #[test]
  152. fn parses_standard_layout() {
  153. let bytes = build_standard(0, (6, 0), 0x100, &fake_bitcode());
  154. let p = DxilProgram::parse(&bytes).unwrap();
  155. assert_eq!(p.kind, ShaderKind::Pixel);
  156. assert_eq!(p.shader_model, (6, 0));
  157. assert!(p.is_standard_layout());
  158. assert_eq!(p.xbox_extras(), XboxExtras::default());
  159. }
  160. #[test]
  161. fn detects_xbox_layout() {
  162. // Xbox layout: XDAT table before the bitcode, xbop blob after it.
  163. let bitcode = fake_bitcode();
  164. let xdat = b"XDAT\x01\x00\x00\x00";
  165. let trailer = b"xbop\x02\x00\x00\x00";
  166. let mut bytes = Vec::new();
  167. let size = 24 + xdat.len() + bitcode.len() + trailer.len();
  168. bytes.extend_from_slice(&((5u32 << 16) | 0x65).to_le_bytes()); // cs_6_5
  169. bytes.extend_from_slice(&((size / 4) as u32).to_le_bytes());
  170. bytes.extend_from_slice(b"DXIL");
  171. bytes.extend_from_slice(&0x105u32.to_le_bytes());
  172. bytes.extend_from_slice(&(16 + xdat.len() as u32).to_le_bytes());
  173. bytes.extend_from_slice(&(bitcode.len() as u32).to_le_bytes());
  174. bytes.extend_from_slice(xdat);
  175. bytes.extend_from_slice(&bitcode);
  176. bytes.extend_from_slice(trailer);
  177. let p = DxilProgram::parse(&bytes).unwrap();
  178. assert_eq!(p.kind, ShaderKind::Compute);
  179. assert_eq!(p.shader_model, (6, 5));
  180. assert!(!p.is_standard_layout());
  181. assert_eq!(
  182. p.xbox_extras(),
  183. XboxExtras {
  184. xdat: true,
  185. xbop: true
  186. }
  187. );
  188. assert_eq!(p.bitcode, &bitcode[..]);
  189. }
  190. #[test]
  191. fn rejects_missing_bitcode_magic() {
  192. let bytes = build_standard(0, (6, 0), 0x100, &[0u8; 16]);
  193. assert!(DxilProgram::parse(&bytes).is_err());
  194. }
  195. }