Rendering and memory (GPL-3.0 code ported in; credits in the source): - Water: 64-lane compute programs on 32-lane waves keep each wave together through loops and set up shared memory from the first thread, so the water's tiled light no longer leaves dark blocks. It runs by default now (VICTORY_WATER_LIGHT=0 leaves it out; an empty value, an earlier version's saved OFF, runs it). Also cloud tiles and the screen probes' ray activation. - Placed views: their padding cleared as before; folding a buffer view's first element into its address is VICTORY_PLACED_FOLD=1, off: with it on, clothing and caps swapped to other variants for single frames in the boat cutscene. - Texture sweep: textures whose copy was let go leave the looks until used again. Static samplers made first, in one order. Small buffers made ahead of the first 3D frame. - play mode: VICTORY_TEXSPK_PASS_MS=8, VICTORY_PASSES_HOT=1, VICTORY_HELPER_PRIORITY=1. - Memory switches, all off: VICTORY_SLOT_EVICT, VICTORY_ALLOCATOR_TRIM, VICTORY_BUDGET_PACK (and VICTORY_LIST_POOL_IDLE_S). Input: DualShock 4 (0x05C4, 0x09CC, 0x0BA0 adapter) read over HID, USB and Bluetooth, beside the DualSense; PlayStation pads rumble, and stop when the game ends (issue #352). F1 menu: every change is saved at once; it saves to victory-settings.txt in VICTORY_HOME or %LOCALAPPDATA%\victory however the game was started (no more "NO SETTINGS FILE"). Warm-up start: its 30 s count from the first frame of 100 draws or more, not from the start: waiting at the title screen restarted it too early and the next run still sat at 14 fps. Co-Authored-By: Claude Opus 5.5 <[email protected]>
273 lines
11 KiB
Rust
273 lines
11 KiB
Rust
//! Survey shader containers: bitcode, console additions and native code.
|
|
|
|
use std::borrow::Cow;
|
|
use std::collections::BTreeMap;
|
|
use std::path::PathBuf;
|
|
|
|
use anyhow::Result;
|
|
use victory_game::sgd5;
|
|
use victory_shader::gfx10::{self, Instruction};
|
|
use victory_shader::{Container, DxilProgram, FourCC, HwStage, ShaderError, native_programs};
|
|
|
|
/// Files above this size are skipped (read whole into memory for now).
|
|
pub(crate) const MAX_FILE: u64 = 2 << 30;
|
|
|
|
/// A shader container in a file: as is, or packed in an SGD5 record.
|
|
pub(crate) enum Source<'a> {
|
|
Raw { offset: usize, data: &'a [u8] },
|
|
Packed(sgd5::Record<'a>),
|
|
}
|
|
|
|
impl Source<'_> {
|
|
/// The container bytes; `None` if a packed record is corrupt.
|
|
pub(crate) fn bytes(&self) -> Option<Cow<'_, [u8]>> {
|
|
match self {
|
|
Source::Raw { data, .. } => Some(Cow::Borrowed(data)),
|
|
Source::Packed(r) => r.unpack().map(Cow::Owned),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn label(&self) -> String {
|
|
match self {
|
|
Source::Raw { offset, .. } => format!("@{offset:#x}"),
|
|
Source::Packed(r) => format!("@{:#x} {}", r.offset, r.name.unwrap_or("?")),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The packed records of SGD5 data, and every container stored as is (the
|
|
/// executable keeps some records uncompressed), found by its magic.
|
|
pub(crate) fn sources(data: &[u8]) -> Vec<Source<'_>> {
|
|
let mut out: Vec<Source<'_>> = Vec::new();
|
|
if sgd5::is_sgd5(data) {
|
|
out.extend(sgd5::records(data).map(Source::Packed));
|
|
}
|
|
let raw = memchr::memmem::find_iter(data, b"DXBC")
|
|
.filter_map(|offset| {
|
|
let tail = &data[offset..];
|
|
// DXBC total size, validated below. Hash the container, not the
|
|
// rest of its carrier, so offline warming uses the runtime key.
|
|
let size = u32::from_le_bytes(tail.get(0x18..0x1c)?.try_into().ok()?) as usize;
|
|
let data = tail.get(..size)?;
|
|
Container::parse(data).ok()?;
|
|
Some(Source::Raw { offset, data })
|
|
});
|
|
out.extend(raw);
|
|
out
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::sources;
|
|
|
|
#[test]
|
|
fn embedded_containers_keep_runtime_hashes() {
|
|
// A 32-byte DXBC with no parts; the scan should not add carrier bytes.
|
|
let shader = b"DXBC\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x01\0\0\0\x20\0\0\0\0\0\0\0";
|
|
let first = [b"MZ".as_slice(), shader, b"first suffix"].concat();
|
|
let second = [b"PE header".as_slice(), shader, b"other suffix", b"DXBC"].concat();
|
|
let runtime_hash = blake3::hash(shader);
|
|
for (carrier, label) in [(&first, "@0x2"), (&second, "@0x9")] {
|
|
let entries = sources(carrier);
|
|
assert_eq!(entries.len(), 1);
|
|
assert_eq!(entries[0].label(), label);
|
|
assert_eq!(blake3::hash(&entries[0].bytes().unwrap()), runtime_hash);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Default)]
|
|
struct Totals {
|
|
containers: usize,
|
|
with_dxil_part: usize,
|
|
with_bitcode: usize,
|
|
standard_layout: usize,
|
|
xdat: usize,
|
|
xbop: usize,
|
|
kinds: BTreeMap<String, usize>,
|
|
with_native: usize,
|
|
native_stages: BTreeMap<HwStage, usize>,
|
|
native_errors: BTreeMap<String, usize>,
|
|
decoded: usize,
|
|
instructions: usize,
|
|
unnamed: BTreeMap<String, usize>,
|
|
decode_errors: BTreeMap<&'static str, usize>,
|
|
files_with_shaders: usize,
|
|
}
|
|
|
|
pub fn run(paths: Vec<PathBuf>, verbose: bool, disasm: bool) -> Result<()> {
|
|
let paths = crate::default_paths(paths)?;
|
|
let mut t = Totals::default();
|
|
// `VICTORY_SHADER_ONLY=<hash>,<hash>`: only the containers with these hashes (the 16
|
|
// digits a run's log names a shader by); `VICTORY_SHADER_SAVE=<folder>`: each one's
|
|
// bytes written there as `<hash>.bin`.
|
|
let only: Option<Vec<String>> = std::env::var("VICTORY_SHADER_ONLY")
|
|
.ok()
|
|
.map(|v| v.split(',').map(|h| h.trim().to_ascii_lowercase()).filter(|h| !h.is_empty()).collect());
|
|
let save = std::env::var_os("VICTORY_SHADER_SAVE").map(PathBuf::from);
|
|
for root in &paths {
|
|
for entry in walkdir::WalkDir::new(root)
|
|
.into_iter()
|
|
.filter_map(Result::ok)
|
|
{
|
|
if !entry.file_type().is_file() {
|
|
continue;
|
|
}
|
|
let path = entry.path();
|
|
if entry.metadata().map(|m| m.len()).unwrap_or(0) > MAX_FILE {
|
|
eprintln!("skipping large file {}", path.display());
|
|
continue;
|
|
}
|
|
let Ok(data) = std::fs::read(path) else {
|
|
continue;
|
|
};
|
|
let mut found_here = 0;
|
|
for source in sources(&data) {
|
|
let Some(bytes) = source.bytes() else {
|
|
eprintln!("corrupt record {}{}", path.display(), source.label());
|
|
continue;
|
|
};
|
|
if let Some(only) = &only {
|
|
let hash = blake3::hash(&bytes).to_hex();
|
|
if !only.iter().any(|h| h == &hash[..16]) {
|
|
continue;
|
|
}
|
|
if let Some(dir) = &save {
|
|
std::fs::create_dir_all(dir)?;
|
|
std::fs::write(dir.join(format!("{}.bin", &hash[..16])), &bytes)?;
|
|
}
|
|
}
|
|
let Ok(container) = Container::parse(&bytes) else {
|
|
continue;
|
|
};
|
|
found_here += 1;
|
|
t.containers += 1;
|
|
let dxil = container.part(FourCC::DXIL).or(container.part(FourCC::ACCC));
|
|
t.with_dxil_part += usize::from(dxil.is_some());
|
|
let program = dxil.and_then(|p| DxilProgram::parse(p.data).ok());
|
|
let mut listing = Vec::new();
|
|
// Independent of the bitcode: titles may strip it.
|
|
let native = match dxil.map(|p| native_programs(p.data)) {
|
|
Some(Ok(programs)) if !programs.is_empty() => {
|
|
t.with_native += 1;
|
|
let mut list = Vec::new();
|
|
for n in &programs {
|
|
*t.native_stages.entry(n.stage).or_default() += 1;
|
|
list.push(format!("{:?} {} B", n.stage, n.code.len()));
|
|
if disasm {
|
|
listing.push(format!(" {:?} stage I/O: {:?}", n.stage, n.io));
|
|
listing.extend(n.layout.iter().map(|p| format!(" {p:?}")));
|
|
listing.extend(n.used.iter().map(|u| format!(" {u:?}")));
|
|
}
|
|
match gfx10::decode(&n.code) {
|
|
Ok(insts) => {
|
|
t.decoded += 1;
|
|
t.instructions += insts.len();
|
|
for i in insts.iter().filter(|i| i.name().is_none()) {
|
|
let key = format!("{:?} {:#x}", i.encoding, i.opcode);
|
|
*t.unnamed.entry(key).or_default() += 1;
|
|
}
|
|
if disasm {
|
|
listing.extend(
|
|
insts.iter().map(|i| format_instruction(i, n.wave64)),
|
|
);
|
|
}
|
|
}
|
|
Err(e) => {
|
|
let reason = match e {
|
|
ShaderError::Isa { reason, .. } => reason,
|
|
_ => "other",
|
|
};
|
|
*t.decode_errors.entry(reason).or_default() += 1;
|
|
list.push(format!("({e})"));
|
|
}
|
|
}
|
|
}
|
|
format!(", native {}", list.join(" "))
|
|
}
|
|
Some(Err(e)) => {
|
|
*t.native_errors.entry(e.to_string()).or_default() += 1;
|
|
format!(", native: {e}")
|
|
}
|
|
_ => String::new(),
|
|
};
|
|
let line = match &program {
|
|
Some(p) => {
|
|
t.with_bitcode += 1;
|
|
let x = p.xbox_extras();
|
|
t.standard_layout += usize::from(p.is_standard_layout());
|
|
t.xdat += usize::from(x.xdat);
|
|
t.xbop += usize::from(x.xbop);
|
|
*t.kinds.entry(format!("{:?}", p.kind)).or_default() += 1;
|
|
format!(
|
|
"{:?} sm{}.{} bitcode {} B, xdat {}, xbop {}",
|
|
p.kind,
|
|
p.shader_model.0,
|
|
p.shader_model.1,
|
|
p.bitcode.len(),
|
|
x.xdat,
|
|
x.xbop
|
|
)
|
|
}
|
|
None => "no parsable DXIL program".to_owned(),
|
|
} + &native;
|
|
if verbose {
|
|
// As a run's log and a replay name it (`hash=`).
|
|
let hash = blake3::hash(&bytes).to_hex();
|
|
println!("{}{} #{}: {line}", path.display(), source.label(), &hash[..16]);
|
|
for l in &listing {
|
|
println!("{l}");
|
|
}
|
|
}
|
|
}
|
|
t.files_with_shaders += usize::from(found_here > 0);
|
|
}
|
|
}
|
|
println!(
|
|
"containers found {} (in {} files)",
|
|
t.containers, t.files_with_shaders
|
|
);
|
|
println!(" with DXIL part {}", t.with_dxil_part);
|
|
println!(" with LLVM bitcode {}", t.with_bitcode);
|
|
println!(" PC-standard layout {}", t.standard_layout);
|
|
println!(" XDAT prefix {}", t.xdat);
|
|
println!(" xbop trailer {}", t.xbop);
|
|
for (k, n) in &t.kinds {
|
|
println!(" {k:<20} {n}");
|
|
}
|
|
println!(" with native code {}", t.with_native);
|
|
for (stage, n) in &t.native_stages {
|
|
println!(" {:<18} {n}", format!("{stage:?}"));
|
|
}
|
|
for (e, n) in &t.native_errors {
|
|
println!(" native unreadable {n}: {e}");
|
|
}
|
|
if t.with_native > 0 {
|
|
println!(
|
|
" decoded programs {} ({} instructions)",
|
|
t.decoded, t.instructions
|
|
);
|
|
for (e, n) in &t.decode_errors {
|
|
println!(" decode failed {n}: {e}");
|
|
}
|
|
let mut unnamed: Vec<_> = t.unnamed.iter().collect();
|
|
unnamed.sort_by_key(|&(_, n)| std::cmp::Reverse(*n));
|
|
for (op, n) in unnamed.iter().take(10) {
|
|
println!(" opcode not in table {op} x{n}");
|
|
}
|
|
}
|
|
if t.containers == 0 {
|
|
println!("(none found: shaders may sit in compressed or encrypted archives)");
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn format_instruction(i: &Instruction, wave64: bool) -> String {
|
|
let words: Vec<_> = i.words.iter().map(|w| format!("{w:08X}")).collect();
|
|
let text = match i.name() {
|
|
Some(_) => i.text(wave64),
|
|
None => format!("<{:?} {:#x}>", i.encoding, i.opcode),
|
|
};
|
|
format!(" {:06x}: {:<44} {text}", i.offset, words.join(" "))
|
|
}
|