Skip to main content

dxil_reader/disasm/
mod.rs

1//! A disassembler of DXIL programs: the listing `dxc -dumpbin` (dxc 1.9.2609) prints of a container, byte for byte.
2//!
3//! The listing is dxc's comment header (`dxcdisassembler.cpp`: the feature flags, the signature parts, the debug name
4//! and hash, the pipeline state validation runtime info, and from the program's metadata its signatures, buffer
5//! layouts, resource bindings, view-id state and a library's subobjects), then the program's module as LLVM 3.7 prints
6//! it, with dxc's comment after each call of a DXIL operation. What the reader can't read is an error, never a listing
7//! that differs from dxc's.
8//!
9//! ```no_run
10//! # fn main() -> Result<(), dxil_reader::Error> {
11//! let container = std::fs::read("shader.dxil").expect("a compiled shader");
12//! print!("{}", dxil_reader::disasm::disassemble(&container)?);
13//! # Ok(()) }
14//! ```
15mod annotate;
16mod asm;
17mod header;
18mod layout;
19mod ops;
20
21use crate::{Module, Result, error, psv, rdat};
22
23/// The listing `dxc -dumpbin` prints of a DXBC container holding a DXIL program, to the newline it ends with.
24pub fn disassemble(container: &[u8]) -> Result<String> {
25    let parts = crate::parts(container)?;
26    let find = |kind: &[u8; 4]| parts.iter().find(|c| &c.kind == kind).map(|c| c.data);
27    let mut s = String::new();
28    if let Some(sfi0) = find(b"SFI0") {
29        header::feature_info(&mut s, sfi0)?;
30    }
31    for (kind, name, input) in [
32        (b"ISG1", "Input", true),
33        (b"OSG1", "Output", false),
34        (b"PSG1", "Patch Constant", false),
35    ] {
36        if let Some(part) = find(kind) {
37            header::part_signature(&mut s, name, part, input)?;
38        }
39    }
40    if let Some(ildn) = find(b"ILDN") {
41        header::debug_name(&mut s, ildn);
42    }
43    if let Some(hash) = find(b"HASH") {
44        header::hash(&mut s, hash)?;
45    }
46    // the debug module, where the container has one, is the one listed
47    let program = find(b"ILDB")
48        .or_else(|| find(b"DXIL"))
49        .ok_or_else(|| error(0, "the container has no DXIL part"))?;
50    let program = crate::program(program)?;
51    if let Some(part) = find(b"PSV0") {
52        header::psv_runtime(&mut s, &psv::read(part)?)?;
53    }
54    let module = read(program.bitcode)?;
55    // the STAT part's module holds the reflection the program part's may be stripped of; dxc ignores one whose header
56    // it can't read
57    let reflection = match find(b"STAT").map(crate::program) {
58        Some(Ok(part)) => Some(read(part.bitcode)?),
59        _ => None,
60    };
61    if module.named_metadata.iter().any(|(n, _)| n == "dx.version") {
62        let kind = crate::reflection::shader_model(&module)?
63            .map(|(k, _, _)| k)
64            .ok_or_else(|| error(module.at, "the module has no dx.shaderModel metadata"))?;
65        if kind != "lib" {
66            header::dxil_signatures(&mut s, &module, &kind)?;
67        }
68        header::reflection_sections(&mut s, reflection.as_ref().unwrap_or(&module))?;
69        let subobjects = match find(b"RDAT") {
70            Some(part) => rdat::subobjects(part)?,
71            None => Vec::new(),
72        };
73        if subobjects.is_empty() && module.named_metadata.iter().any(|(n, _)| n == "dx.subobjects") {
74            return Err(error(
75                module.at,
76                "subobjects in dx.subobjects metadata are not supported",
77            ));
78        }
79        header::subobjects(&mut s, &subobjects);
80    }
81    asm::write(&mut s, &module)?;
82    // dxc's console writer ends the text with a newline of its own
83    s.push('\n');
84    Ok(s)
85}
86
87/// The module part of the listing: from `target datalayout` on, the program's IR as dxc prints it.
88pub fn module_text(m: &Module) -> Result<String> {
89    let mut s = String::new();
90    asm::write(&mut s, m)?;
91    Ok(s)
92}
93
94fn read(bitcode: &[u8]) -> Result<Module> {
95    Module::read(&crate::bitstream::read(bitcode, *b"BC\xc0\xde")?)
96}