Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions crates/victory-gpu/src/pipeline/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
),
};
Expand Down Expand Up @@ -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<u8> {
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::<u8>(), code.BytecodeLength) }.to_vec()
}

fn root_signature(
device: &ID3D12Device,
params: &[D3D12_ROOT_PARAMETER1],
Expand Down
44 changes: 38 additions & 6 deletions crates/victory-gpu/src/pipeline/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,12 +572,28 @@ fn open(device: &ID3D12Device1, blob: Blob) -> Option<Library> {
/// 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
Expand Down Expand Up @@ -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"));
Expand Down