From 44d69219872ae75420b953a8c9853aa6b57a29a3 Mon Sep 17 00:00:00 2001 From: Logan Date: Tue, 24 Nov 2026 01:01:17 -0500 Subject: [PATCH] D3D12.X: pipelines that share a shader share one copy of its bytes An async pipeline keeps its description with copies of its shader bytecode until it is built. A title that creates tens of thousands of pipelines from a few thousand shaders held one copy per pipeline (about 84,000 pending pipelines on a retail title); with the copies shared, its commit at 80 s was about 1 GB lower (27,016 MB against 27,972 MB). own_shader and own_stream now intern the bytes (a weak map keyed by hash and length, compared in full), so every pipeline with the same shader holds the same Arc<[u8]>. shaders_key takes any byte slices. --- crates/xcl/src/hle/d3d12x/real.rs | 51 ++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index aed907586c..c5add5634b 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -3768,19 +3768,19 @@ unsafe fn translated_graphics_pso(this: *mut c_void, f: PsoFn, desc: *const c_vo /// The root signature pointer is kept as is (the translated paths replace it). #[allow(dead_code)] // the vectors only keep the copies alive enum OwnedDesc { - Compute(Box, Vec>), - Graphics(Box, Vec>, Vec, Vec), - Stream(Box<[usize; 2]>, Vec, Vec>), + Compute(Box, Vec>), + Graphics(Box, Vec>, Vec, Vec), + Stream(Box<[usize; 2]>, Vec, Vec>), } unsafe impl Send for OwnedDesc {} /// A hash of a pipeline's shaders in their order (never 0): what `pso_async` knows a used /// pipeline by in the next run. -fn shaders_key(blobs: &[Vec]) -> u64 { +fn shaders_key>(blobs: &[B]) -> u64 { let mut h = 0xcbf2_9ce4_8422_2325u64; for b in blobs { - h = (h ^ crate::gpu::shader::fnv64(b)).wrapping_mul(0x0000_0100_0000_01b3); + h = (h ^ crate::gpu::shader::fnv64(b.as_ref())).wrapping_mul(0x0000_0100_0000_01b3); } h | 1 } @@ -3795,7 +3795,21 @@ mod shaders_key_tests { assert_ne!(key, super::shaders_key(&[b.clone(), a.clone()])); assert_ne!(key, super::shaders_key(&[a.clone()])); assert_ne!(key, super::shaders_key(&[a, vec![4u8, 6]])); - assert_ne!(super::shaders_key(&[]), 0); + assert_ne!(super::shaders_key::>(&[]), 0); + } + + #[test] + fn pipelines_with_the_same_shader_share_one_copy_of_its_bytes() { + let a = super::intern_shader_blob(&[7u8, 1, 2, 3, 0x5a]); + let b = super::intern_shader_blob(&[7u8, 1, 2, 3, 0x5a]); + assert!(std::sync::Arc::ptr_eq(&a, &b)); + let c = super::intern_shader_blob(&[7u8, 1, 2, 3, 0x5b]); + assert!(!std::sync::Arc::ptr_eq(&a, &c)); + assert_eq!(&*c, &[7u8, 1, 2, 3, 0x5b]); + assert_eq!(super::shaders_key(&[a.clone()]), super::shaders_key(&[vec![7u8, 1, 2, 3, 0x5a]])); + drop((a, b)); + let d = super::intern_shader_blob(&[7u8, 1, 2, 3, 0x5a]); + assert_eq!(&*d, &[7u8, 1, 2, 3, 0x5a], "a copy every pipeline released is made again"); } } @@ -3817,10 +3831,24 @@ impl OwnedDesc { } } -/// Copy a shader blob into `blobs` and point `b` at the copy. -unsafe fn own_shader(b: &mut D3D12_SHADER_BYTECODE, blobs: &mut Vec>) { +fn intern_shader_blob(bytes: &[u8]) -> std::sync::Arc<[u8]> { + static BLOBS: Mutex>>> = Mutex::new(None); + let key = (crate::gpu::shader::fnv64(bytes), bytes.len()); + let mut g = BLOBS.lock().unwrap(); + let m = g.get_or_insert_with(HashMap::new); + if let Some(arc) = m.get(&key).and_then(|w| w.upgrade()) { + if &*arc == bytes { + return arc; + } + } + let arc: std::sync::Arc<[u8]> = std::sync::Arc::from(bytes); + m.insert(key, std::sync::Arc::downgrade(&arc)); + arc +} + +unsafe fn own_shader(b: &mut D3D12_SHADER_BYTECODE, blobs: &mut Vec>) { if let Some(bytes) = shader_bytes(b) { - blobs.push(bytes.to_vec()); + blobs.push(intern_shader_blob(bytes)); let v = blobs.last().unwrap(); *b = bytecode(v); } else { @@ -3869,7 +3897,7 @@ unsafe fn own_stream(desc: *const c_void) -> Option { return None; } let mut buf = std::slice::from_raw_parts(base as *const u8, size).to_vec(); - let mut blobs: Vec> = Vec::new(); + let mut blobs: Vec> = Vec::new(); let mut off = 0usize; while off + 8 <= size { let ty = u32::from_le_bytes(buf[off..off + 4].try_into().ok()?); @@ -3880,7 +3908,8 @@ unsafe fn own_stream(desc: *const c_void) -> Option { 1..=6 | 24 | 25 => { let (p, n) = (word(&buf, 0) as usize, word(&buf, 1) as usize); if p != 0 && n != 0 && crate::gpu::shader::readable(p, n) { - blobs.push(std::slice::from_raw_parts(p as *const u8, n).to_vec()); + let slice = std::slice::from_raw_parts(p as *const u8, n); + blobs.push(intern_shader_blob(slice)); buf[at..at + 8].copy_from_slice(&(blobs.last().unwrap().as_ptr() as u64).to_le_bytes()); } }