Only changed dwords go back into title memory; cheap safe copies; title's own arguments; more shader forms
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
7913a98a3d
commit
66afb2b4cb
16 files changed
+973
-29
No files matched your search
@@ -120,6 +120,18 @@ const CS_THREADS: usize = 0x28;
|
||||
const RECORD_LDS: usize = 0x4c;
|
||||
const MS_CLIP_CNTL: usize = 0x194;
|
||||
const MS_INFO: usize = 0x448;
|
||||
const FSPX_VERSION: u32 = 1;
|
||||
const FSPX_HEADER: usize = 0x10;
|
||||
const FSPX_PACKET: u32 = 0;
|
||||
const FSPX_CODE: u32 = 6;
|
||||
const FSPX_METADATA: u32 = 7;
|
||||
const PACKET_THREADS: usize = 0x0c;
|
||||
const PACKET_FLAGS: usize = 0x12;
|
||||
const PACKET_WAVE32: u16 = 1 << 4;
|
||||
const PACKET_RSRC1: usize = 0x14;
|
||||
const PACKET_RSRC2: usize = 0x18;
|
||||
const FSPX_RECORDS: usize = 0x18;
|
||||
const FSPX_RECORD: usize = 32;
|
||||
/// Unpacked blobs are a few KiB; refuse to allocate for absurd sizes.
|
||||
const MAX_UNPACKED: u64 = 16 << 20;
|
||||
|
||||
@@ -409,8 +421,12 @@ pub struct UsedResource {
|
||||
pub slot: u8,
|
||||
}
|
||||
|
||||
/// The native programs in a `DXIL` part's data; empty for the PC layout.
|
||||
/// The native programs in a `DXIL` part's data, or in an `ACCC` part's
|
||||
/// ([`fspx_program`]); empty for the PC layout.
|
||||
pub fn native_programs(part: &[u8]) -> Result<Vec<NativeProgram>, ShaderError> {
|
||||
if part.starts_with(b"FSPX") {
|
||||
return Ok(vec![fspx_program(part)?]);
|
||||
}
|
||||
if part.get(XDAT_OFFSET..XDAT_OFFSET + 4) != Some(b"XDAT") {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
@@ -432,6 +448,129 @@ pub fn native_programs(part: &[u8]) -> Result<Vec<NativeProgram>, ShaderError> {
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// The compute program of an `ACCC` part, which a container carries in place
|
||||
/// of `DXIL` when another compiler made the native code (the AMD
|
||||
/// FidelityFX upscaler's shaders). Layout as observed (offsets from "FSPX"):
|
||||
///
|
||||
/// ```text
|
||||
/// +0x00 "FSPX" u32 version (1), u32 entry count, u32 size; entries of 16 B
|
||||
/// from +0x10: u32 kind, u32 offset, u32 size, u32 0
|
||||
/// kind 0 pipeline registers, 0x1c B: +0x0c u16 x3 thread-group size,
|
||||
/// +0x12 u16 flags (bit 4 wave32), +0x14 COMPUTE_PGM_RSRC1,
|
||||
/// +0x18 COMPUTE_PGM_RSRC2
|
||||
/// kind 1 pipeline metadata (a hash); kind 6 the GFX10 code
|
||||
/// kind 7 program metadata: u32 0x403, u32 size - 0x14, u32 code dwords,
|
||||
/// u64 0, u8 0x40, u8 first user-data dword, u8 count of 0x41
|
||||
/// records, u8 0; then 32-byte records from +0x18, each a u32 tag:
|
||||
/// 0x41 a resource the code reads: u8 resource type (as FSSM's used
|
||||
/// entries), u8 root type, u16 0, u32 table offset (dwords), u32
|
||||
/// register, u32 view dimension (1 buffer, 4 2D, 0 root view),
|
||||
/// u8 descriptor dwords, u8 user-data dword, u8 root slot, u8 0,
|
||||
/// u32 -1, u32 0
|
||||
/// 0x42 a view declaration: u32 0, u8 1, u8 class (0 SRV, 1 UAV),
|
||||
/// u8 resource kind (DXIL's), u8 0, u32 register
|
||||
/// 0x43 the shader hash (16 B); the last record
|
||||
/// ```
|
||||
pub fn fspx_program(part: &[u8]) -> Result<NativeProgram, ShaderError> {
|
||||
if u32_at(part, 4)? != FSPX_VERSION {
|
||||
return Err(Malformed("unknown FSPX version"));
|
||||
}
|
||||
let count = u32_at(part, 8)? as usize;
|
||||
if count > MAX_XDAT_ENTRIES {
|
||||
return Err(Malformed("implausible FSPX entry count"));
|
||||
}
|
||||
let entry = |kind: u32| -> Result<&[u8], ShaderError> {
|
||||
for i in 0..count {
|
||||
let at = FSPX_HEADER + 16 * i;
|
||||
if u32_at(part, at)? == kind {
|
||||
let (offset, size) = (u32_at(part, at + 4)?, u32_at(part, at + 8)?);
|
||||
return bytes(part, offset.into(), size.into())
|
||||
.ok_or(Malformed("FSPX entry out of bounds"));
|
||||
}
|
||||
}
|
||||
Err(Malformed("FSPX entry missing"))
|
||||
};
|
||||
let packet = entry(FSPX_PACKET)?;
|
||||
let code = entry(FSPX_CODE)?;
|
||||
let meta = entry(FSPX_METADATA)?;
|
||||
if u32_at(meta, 8)? as usize * 4 != code.len() {
|
||||
return Err(Malformed("FSPX code size disagrees"));
|
||||
}
|
||||
let wave64 = u16_at(packet, PACKET_FLAGS)? & PACKET_WAVE32 == 0;
|
||||
// VGPRs in granules of 4 (wave64) or 8 (wave32).
|
||||
let granules = (u32_at(packet, PACKET_RSRC1)? & 0x3F) + 1;
|
||||
let vgprs = (granules * if wave64 { 4 } else { 8 }).min(256) as u16;
|
||||
let threads = |i: usize| u16_at(packet, PACKET_THREADS + 2 * i).map(u32::from);
|
||||
let mut layout: Vec<RootParam> = Vec::new();
|
||||
let mut used = Vec::new();
|
||||
for e in meta
|
||||
.get(FSPX_RECORDS..)
|
||||
.unwrap_or(&[])
|
||||
.as_chunks::<FSPX_RECORD>()
|
||||
.0
|
||||
{
|
||||
match e[0] {
|
||||
0x41 => {}
|
||||
0x42 => continue,
|
||||
0x43 => break,
|
||||
_ => return Err(Malformed("unknown FSPX metadata record")),
|
||||
}
|
||||
let root = RootType::from_raw(e[5].into());
|
||||
let u = UsedResource {
|
||||
kind: match e[4] {
|
||||
0 => ResourceType::Srv,
|
||||
1 => ResourceType::Sampler,
|
||||
2 => ResourceType::Cbv,
|
||||
3 => ResourceType::Uav,
|
||||
n => ResourceType::Other(n),
|
||||
},
|
||||
root,
|
||||
table_offset_dwords: le32(e, 8),
|
||||
lower_bound: le32(e, 12),
|
||||
stride: 0,
|
||||
dwords: e[20],
|
||||
user_data: e[21],
|
||||
slot: e[22],
|
||||
};
|
||||
let sgprs = match root {
|
||||
RootType::Table => 1,
|
||||
RootType::Descriptor => 2,
|
||||
// The code holds its words; no user data.
|
||||
RootType::ImmediateSampler => 0,
|
||||
_ => return Err(Malformed("an FSPX resource of an unknown root type")),
|
||||
};
|
||||
if sgprs > 0 && !layout.iter().any(|p| p.slot == u.slot) {
|
||||
layout.push(RootParam {
|
||||
root,
|
||||
table_offset_dwords: 0,
|
||||
sgprs,
|
||||
base_register: u.lower_bound,
|
||||
user_data: u.user_data,
|
||||
slot: u.slot,
|
||||
});
|
||||
}
|
||||
used.push(u);
|
||||
}
|
||||
Ok(NativeProgram {
|
||||
stage: HwStage::Cs,
|
||||
wave64,
|
||||
vgprs,
|
||||
layout,
|
||||
used,
|
||||
io: StageIo::Cs(ComputeIo {
|
||||
rsrc2: u32_at(packet, PACKET_RSRC2)?,
|
||||
threads: [threads(0)?, threads(1)?, threads(2)?],
|
||||
}),
|
||||
code: code.to_vec(),
|
||||
})
|
||||
}
|
||||
|
||||
fn u16_at(data: &[u8], at: usize) -> Result<u16, ShaderError> {
|
||||
data.get(at..at + 2)
|
||||
.map(|b| u16::from_le_bytes([b[0], b[1]]))
|
||||
.ok_or(Malformed("FSPX data out of bounds"))
|
||||
}
|
||||
|
||||
fn unpack(blob: &[u8]) -> Result<Vec<u8>, ShaderError> {
|
||||
if blob.get(0..4) != Some(b"XBCH") {
|
||||
return Err(Malformed("native blob lacks XBCH magic"));
|
||||
|
||||
Reference in new issue
Block a user