diff --git a/crates/xcl/Cargo.toml b/crates/xcl/Cargo.toml index 100dc5bad6..fa504c6eb2 100644 --- a/crates/xcl/Cargo.toml +++ b/crates/xcl/Cargo.toml @@ -77,3 +77,11 @@ harness = false [[bench]] name = "formats" harness = false + +[[bench]] +name = "gpu_cpu" +harness = false + +[[bench]] +name = "runtime" +harness = false diff --git a/crates/xcl/benches/gpu_cpu.rs b/crates/xcl/benches/gpu_cpu.rs new file mode 100644 index 0000000000..7bdf2079df --- /dev/null +++ b/crates/xcl/benches/gpu_cpu.rs @@ -0,0 +1,173 @@ +mod support; + +use std::hint::black_box; +use support::{BenchmarkId, Criterion, Throughput}; +use xcl::gpu::{desc, rdna2, recomp, shader, xbox}; + +const VGPR0: u64 = 256; + +fn instruction(name: &str, encoding: &str, fields: &[(&str, u64)]) -> Vec { + rdna2::encode(name, encoding, fields, None).unwrap() +} + +fn arithmetic_program(instructions: usize) -> Vec { + let add = instruction( + "V_ADD_NC_U32", + "ENC_VOP2", + &[("VDST", 1), ("SRC0", VGPR0 + 2), ("VSRC1", 3)], + ); + let xor = instruction( + "V_XOR_B32", + "ENC_VOP2", + &[("VDST", 4), ("SRC0", VGPR0 + 1), ("VSRC1", 5)], + ); + let mut code = Vec::with_capacity(instructions * 4 + 4); + for i in 0..instructions { + code.extend(if i & 1 == 0 { &add } else { &xor }); + } + code.extend(instruction("S_ENDPGM", "ENC_SOPP", &[])); + code +} + +fn branch_program(blocks: usize) -> Vec { + let mov = instruction("S_MOV_B32", "ENC_SOP1", &[("SDST", 0), ("SSRC0", 128)]); + let branch = instruction("S_CBRANCH_SCC0", "ENC_SOPP", &[("SIMM16", 1)]); + let add = instruction( + "V_ADD_NC_U32", + "ENC_VOP2", + &[("VDST", 1), ("SRC0", VGPR0 + 2), ("VSRC1", 3)], + ); + let mut code = Vec::with_capacity(blocks * 12 + 4); + for _ in 0..blocks { + code.extend(&mov); + code.extend(&branch); + code.extend(&add); + } + code.extend(instruction("S_ENDPGM", "ENC_SOPP", &[])); + code +} + +fn rdna2_frontend(c: &mut Criterion) { + let code = arithmetic_program(1024); + let mesh_control_flow = branch_program(256); + let mut group = c.benchmark_group("rdna2_frontend"); + group.throughput(Throughput::Bytes(code.len() as u64)); + group.bench_function("decode_stream", |b| { + b.iter(|| { + let mut offset = 0; + while offset < code.len() { + let decoded = rdna2::decode(black_box(&code[offset..]), offset as u64).unwrap(); + offset += black_box(decoded.len); + } + black_box(offset) + }) + }); + group.bench_function("disassemble", |b| { + b.iter(|| black_box(rdna2::disassemble(black_box(&code), 0, false))) + }); + group.bench_function("wave_hazard_scan", |b| { + b.iter(|| black_box(recomp::wave_hazards(black_box(&code)))) + }); + group.bench_function("geometry_wave32_hazard_scan", |b| { + b.iter(|| black_box(recomp::geometry_wave32_hazards(black_box(&code)))) + }); + group.throughput(Throughput::Bytes(mesh_control_flow.len() as u64)); + group.bench_function("mesh_wave_analysis", |b| { + b.iter(|| black_box(recomp::mesh_wave::analyze(black_box(&mesh_control_flow)))) + }); + group.finish(); +} + +fn translate(c: &mut Criterion) { + let mut group = c.benchmark_group("recompiler_translate"); + group.sample_size(20); + for size in [16usize, 64, 256] { + let code = arithmetic_program(size); + group.throughput(Throughput::Elements((size + 1) as u64)); + group.bench_with_input(BenchmarkId::new("linear", size), &code, |b, code| { + b.iter(|| { + let translated = + recomp::translate(black_box(code), black_box(&recomp::Options::default())) + .unwrap(); + black_box(( + translated.hlsl.len(), + translated.instructions, + translated.blocks, + )) + }) + }); + } + for blocks in [8usize, 32] { + let code = branch_program(blocks); + group.throughput(Throughput::Elements((blocks * 3 + 1) as u64)); + group.bench_with_input( + BenchmarkId::new("control_flow", blocks), + &code, + |b, code| { + b.iter(|| { + let translated = + recomp::translate(black_box(code), black_box(&recomp::Options::default())) + .unwrap(); + black_box(( + translated.hlsl.len(), + translated.instructions, + translated.blocks, + )) + }) + }, + ); + } + group.finish(); +} + +fn descriptors(c: &mut Criterion) { + let image = desc::xcl_image_for_view(7, 10, desc::ImageType::D2, 1920, 1080, 1, 11); + let buffer = desc::buffer_words(0x1_2345_6000, 16, 65_536, 74); + let sampler = [0x0000_0002, 0x0000_0101, 0x3f80_0000, 0x0000_0000]; + let mut group = c.benchmark_group("descriptor_conversion"); + group.bench_function("decode_image", |b| { + b.iter(|| black_box(desc::image(black_box(&image)))) + }); + group.bench_function("image_to_d3d12", |b| { + let decoded = desc::image(&image).unwrap(); + b.iter(|| black_box(desc::image_d3d12(black_box(&decoded)))) + }); + group.bench_function("decode_buffer", |b| { + b.iter(|| black_box(desc::buffer(black_box(&buffer)))) + }); + group.bench_function("sampler_to_d3d12", |b| { + b.iter(|| black_box(desc::sampler_d3d12(black_box(&sampler), false))) + }); + group.finish(); +} + +fn shader_scans(c: &mut Criterion) { + let program = arithmetic_program(1024); + let mut blob = vec![0u8; 4096]; + for _ in 0..16 { + blob.extend_from_slice(&program); + blob.extend_from_slice(&[0u8; 64]); + } + let mut group = c.benchmark_group("shader_scans"); + group.throughput(Throughput::Bytes(blob.len() as u64)); + group.bench_function("find_rdna2_programs", |b| { + b.iter(|| black_box(rdna2::find_programs(black_box(&blob)))) + }); + group.throughput(Throughput::Bytes(program.len() as u64)); + group.bench_function("classify_raw_rdna2", |b| { + b.iter(|| black_box(shader::classify(black_box(&program)))) + }); + group.throughput(Throughput::Bytes(blob.len() as u64)); + group.bench_function("fnv64", |b| { + b.iter(|| black_box(shader::fnv64(black_box(&blob)))) + }); + group.throughput(Throughput::Bytes(program.len() as u64)); + group.bench_function("split_segments", |b| { + b.iter(|| black_box(xbox::split_segments(black_box(&program)))) + }); + group.finish(); +} + +fn main() { + support::run(&[rdna2_frontend, translate, descriptors, shader_scans]); +} diff --git a/crates/xcl/benches/runtime.rs b/crates/xcl/benches/runtime.rs new file mode 100644 index 0000000000..3ff4a99f4a --- /dev/null +++ b/crates/xcl/benches/runtime.rs @@ -0,0 +1,205 @@ +mod support; + +use std::collections::BTreeMap; +use std::hint::black_box; +use std::path::Path; +use support::{BatchSize, BenchmarkId, Criterion, Throughput}; +use xcl::config::Config; +use xcl::manifest; +use xcl::util; +use xcl::vfs::Vfs; + +const MANIFEST: &str = r#" + + 4096 + + Advanced + Standard + + 12345678 + + + + +"#; + +const CONFIG: &str = r#" +device_type = "anaconda" +gamertag = "Player" +user_id = 281474976710655 +title_memory = "auto" +pls_mb = 0 +uma_mb = 0 +log_level = "info" +graphics = "d3d12" +d3d12_adapter = "auto" +present = "auto" +raytracing = "auto" +window_width = 1280 +window_height = 720 +keyboard_mouse = true +mouse_sensitivity = 1.0 + +[mounts] +"GAME:" = "{game}" +"G:" = "{game}" +"S:" = "{data}\\pls" + +[aliases] +"api-ms-win-*" = "@system" +"kernelx.dll" = "kernelx.dll" + +[stub_returns] +"example.dll!Example" = "S_OK" +"#; + +fn utility_benches(c: &mut Criterion) { + let mut group = c.benchmark_group("runtime/util"); + for (name, pattern, value) in [ + ("glob_exact", "kernelx.dll", "kernelx.dll"), + ( + "glob_prefix", + "api-ms-win-*", + "api-ms-win-core-file-l2-1-0.dll", + ), + ("glob_bounded", "xgame*.dll", "xgameruntime.dll"), + ] { + group.bench_function(name, |b| { + b.iter(|| util::glob_match(black_box(pattern), black_box(value))) + }); + } + for value in [ + "kernelx", + "D:\\Game\\D3D12_XS.DLL", + "api-ms-win-core-file-l2-1-0", + ] { + group.bench_with_input( + BenchmarkId::new("dll_key", value.len()), + value, + |b, value| { + b.iter(|| util::dll_key(black_box(value))); + }, + ); + } + for value in ["S_OK", "E_NOTIMPL", "0x80004005", "42"] { + group.bench_with_input( + BenchmarkId::new("parse_return", value), + value, + |b, value| { + b.iter(|| util::parse_ret(black_box(value))); + }, + ); + } + for value in [ + "short", + "D:\\Synthetic Game\\Content\\localization\\en-US\\strings.bin", + ] { + group.throughput(Throughput::Bytes(value.len() as u64)); + group.bench_with_input(BenchmarkId::new("utf16", value.len()), value, |b, value| { + b.iter(|| util::wide(black_box(value))); + }); + } + group.finish(); +} + +fn manifest_and_config_benches(c: &mut Criterion) { + let mut group = c.benchmark_group("runtime/config"); + group.throughput(Throughput::Bytes(MANIFEST.len() as u64)); + group.bench_function("manifest_parse", |b| { + b.iter(|| manifest::parse(black_box(MANIFEST))) + }); + + group.throughput(Throughput::Bytes(CONFIG.len() as u64)); + group.bench_function("toml_parse", |b| { + b.iter(|| toml::from_str::(black_box(CONFIG)).unwrap()); + }); + + let parsed_manifest = manifest::parse(MANIFEST); + group.throughput(Throughput::Elements(1)); + group.bench_function("resolve_defaults", |b| { + b.iter_batched( + Config::default, + |mut config| black_box(config.resolve(black_box(Some(&parsed_manifest)))), + BatchSize::SmallInput, + ); + }); + + let mut mounts = BTreeMap::new(); + mounts.insert("GAME:".to_owned(), "{game}".to_owned()); + mounts.insert("S:".to_owned(), "{data}\\pls".to_owned()); + let mut resolved = Config { + mounts, + ..Config::default() + }; + resolved.resolve(Some(&parsed_manifest)); + group.throughput(Throughput::Elements(1)); + group.bench_function("resolved_memory_queries", |b| { + b.iter(|| { + black_box(resolved.title_memory_budget_mb()); + black_box(resolved.pls_drive()); + black_box(resolved.raytracing_tier_override()); + }); + }); + group.finish(); +} + +fn vfs_benches(c: &mut Criterion) { + let vfs = Vfs::new( + Path::new(r"C:\bench\game"), + Path::new(r"C:\bench\data"), + vec![ + ("GAME:".to_owned(), r"C:\bench\game".to_owned()), + ("G:".to_owned(), r"C:\bench\game".to_owned()), + ("S:".to_owned(), r"C:\bench\data\pls".to_owned()), + ], + ); + let full = |path: &str| Some(path.replace('/', "\\")); + let cases = [ + ("mounted_game", r"GAME:\data\textures.bin"), + ("mounted_drive", r"G:\data\textures.bin"), + ("embedded_mount", r"C:\cwd\GAME:\data\textures.bin"), + ("direct_game", r"C:\bench\game\data\textures.bin"), + ("host_path", r"C:\Windows\System32\kernel32.dll"), + ("unc", r"\\server\share\file.bin"), + ]; + + let mut group = c.benchmark_group("runtime/vfs"); + for (name, path) in cases { + group.throughput(Throughput::Bytes(path.len() as u64)); + group.bench_with_input(BenchmarkId::new("classify", name), path, |b, path| { + b.iter(|| vfs.classify_with(black_box(path), black_box(full))); + }); + } + group.throughput(Throughput::Bytes( + cases.iter().map(|(_, path)| path.len() as u64).sum(), + )); + group.bench_function("classify_mixed_batch", |b| { + b.iter(|| { + for (_, path) in black_box(&cases) { + black_box(vfs.classify_with(path, full)); + } + }); + }); + for rel in ["", "data", r"data\textures\terrain.bin"] { + group.throughput(Throughput::Elements(1)); + group.bench_with_input( + BenchmarkId::new("overlay_path", rel.len()), + rel, + |b, rel| { + b.iter(|| vfs.overlay_path(black_box(rel))); + }, + ); + group.bench_with_input( + BenchmarkId::new("original_path", rel.len()), + rel, + |b, rel| { + b.iter(|| vfs.original_path(black_box(rel))); + }, + ); + } + group.finish(); +} + +fn main() { + support::run(&[utility_benches, manifest_and_config_benches, vfs_benches]); +}