diff --git a/crates/victory-gpu/src/pipeline/host.rs b/crates/victory-gpu/src/pipeline/host.rs index d5343c84b..c818eba9c 100644 --- a/crates/victory-gpu/src/pipeline/host.rs +++ b/crates/victory-gpu/src/pipeline/host.rs @@ -603,14 +603,14 @@ impl HostPipeline { // Not from the library if it has static samplers (their places in the // driver's table differ between runs: `library::with_static_samplers`). let _samplers = super::library::with_static_samplers(!samplers.is_empty()); + let dxil = |i: usize| { + stages.get(i).and_then(|(s, _)| s.translated()).map(|t| t.host.dxil.as_slice()) + }; let code = |i: usize| { - stages.get(i).and_then(|(s, _)| s.translated()).map_or( - D3D12_SHADER_BYTECODE::default(), - |t| D3D12_SHADER_BYTECODE { - pShaderBytecode: t.host.dxil.as_ptr().cast(), - BytecodeLength: t.host.dxil.len(), - }, - ) + dxil(i).map_or(D3D12_SHADER_BYTECODE::default(), |bytes| D3D12_SHADER_BYTECODE { + pShaderBytecode: bytes.as_ptr().cast(), + BytecodeLength: bytes.len(), + }) }; if compute.is_some() { let desc = D3D12_COMPUTE_PIPELINE_STATE_DESC { @@ -619,7 +619,7 @@ impl HostPipeline { CS: code(0), ..Default::default() }; - let name = super::library::name("compute", &root_blob, &[&code_bytes(&code(0))], ""); + let name = super::library::name("compute", &root_blob, &[dxil(0).unwrap_or_default()], ""); let made = super::library::pipeline( &name, // SAFETY: the description points at live data. @@ -677,7 +677,7 @@ impl HostPipeline { let name = super::library::name( "mesh triangles", &root_blob, - &[&code_bytes(&code(0)), &code_bytes(&code(1))], + &[dxil(0).unwrap_or_default(), dxil(1).unwrap_or_default()], &state, ); let pso = match mesh_pipeline(&host.device, &root, code(0), code(1), g, depth_bounds, &name) { @@ -755,13 +755,13 @@ impl HostPipeline { Some(gs) => super::library::name( "graphics rectangles", &root_blob, - &[&code_bytes(&code(0)), &code_bytes(&code(1)), &gs[..]], + &[dxil(0).unwrap_or_default(), dxil(1).unwrap_or_default(), &gs[..]], &state, ), None => super::library::name( "graphics", &root_blob, - &[&code_bytes(&code(0)), &code_bytes(&code(1))], + &[dxil(0).unwrap_or_default(), dxil(1).unwrap_or_default()], &state, ), }; @@ -1012,15 +1012,6 @@ fn mesh_pipeline( } } -/// The bytes of shader code (none for an absent stage). -fn code_bytes(code: &D3D12_SHADER_BYTECODE) -> Vec { - if code.pShaderBytecode.is_null() { - return Vec::new(); - } - // SAFETY: a translation's own bytes, alive while its shader is. - unsafe { std::slice::from_raw_parts(code.pShaderBytecode.cast::(), code.BytecodeLength) }.to_vec() -} - fn root_signature( device: &ID3D12Device, params: &[D3D12_ROOT_PARAMETER1], diff --git a/crates/victory-gpu/src/pipeline/library.rs b/crates/victory-gpu/src/pipeline/library.rs index 06db5a1e9..5987877e9 100644 --- a/crates/victory-gpu/src/pipeline/library.rs +++ b/crates/victory-gpu/src/pipeline/library.rs @@ -572,12 +572,28 @@ fn open(device: &ID3D12Device1, blob: Blob) -> Option { /// A pipeline's name in the library: the hash of what its host /// description holds. pub(crate) fn name(kind: &str, root: &[u8], code: &[&[u8]], state: &str) -> String { - let mut h = blake3::Hasher::new(); - for part in [kind.as_bytes(), root].into_iter().chain(code.iter().copied()).chain([state.as_bytes()]) { - h.update(&(part.len() as u64).to_le_bytes()); - h.update(part); + let parts = [kind.as_bytes(), root].into_iter().chain(code.iter().copied()).chain([state.as_bytes()]); + // One contiguous input lets BLAKE3 hash multiple short shader chunks in + // parallel. Keep the original streaming path for large pipeline descriptions. + let size = parts.clone().try_fold(0usize, |n, p| n.checked_add(8)?.checked_add(p.len())); + let mut hash = blake3::Hasher::new(); + if let Some(size) = size.filter(|&n| n <= 8 * blake3::CHUNK_LEN) { + let mut frame = Vec::with_capacity(size); + for part in parts { + frame.extend_from_slice(&(part.len() as u64).to_le_bytes()); + frame.extend_from_slice(part); + } + hash.update(&frame); + } else { + for part in parts { + hash.update(&(part.len() as u64).to_le_bytes()); + hash.update(part); + } } - h.finalize().to_hex()[..32].to_owned() + // Anything appended to the hash after this (for example a later + // per-process suffix such as static-sampler places) still goes through + // the same hasher, so the framed and streaming paths stay identical. + hash.finalize().to_hex()[..32].to_owned() } /// The pipeline named `name`: loaded from the library by `load` if it is @@ -852,10 +868,26 @@ pub(crate) fn note_used(key: &str) { mod tests { use super::*; + #[test] + fn library_names_preserve_the_length_framing_at_the_buffer_limit() { + // One compute stage plus four u64 lengths and the seven-byte kind. + const OVERHEAD: usize = 4 * 8 + 7; + for size in [8191, 8192, 8193] { + let shader = vec![0x5a; size - OVERHEAD]; + let mut expected = blake3::Hasher::new(); + for part in [b"compute".as_slice(), &[], shader.as_slice(), &[]] { + expected.update(&(part.len() as u64).to_le_bytes()); + expected.update(part); + } + assert_eq!(name("compute", &[], &[&shader], ""), expected.finalize().to_hex()[..32]); + } + } + #[test] fn names_change_with_what_goes_into_the_pipeline() { let a = name("graphics", b"root", &[b"vs", b"ps"], "state"); - assert_eq!(a, name("graphics", b"root", &[b"vs", b"ps"], "state")); + // A name from the previous writer must still load from disk. + assert_eq!(a, "9402d2bc73e94e1548e2a550a55f878f"); assert_ne!(a, name("graphics", b"root", &[b"vs", b"ps2"], "state")); assert_ne!(a, name("graphics", b"root", &[b"vs", b"ps"], "state2")); assert_ne!(a, name("graphics", b"root2", &[b"vs", b"ps"], "state"));