Skip to content
Draft
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
64 changes: 48 additions & 16 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5893,23 +5893,25 @@ fn keep_driver_blob<K: Copy + Eq + std::hash::Hash>(
}

/// The driver's compiled form of translated graphics pipelines (`GetCachedBlob`),
/// keyed by `(full_state_and_shader_hash, total_dxil_bytes)`, up to `GRAPHICS_BLOBS_MAX`.
static GRAPHICS_BLOBS: DriverBlobs<(u64, usize)> = DriverBlobs::new();
/// keyed by `(full_state_and_shader_hash, total_dxil_bytes, effective_root_signature)`, up to
/// `GRAPHICS_BLOBS_MAX`. The root address scopes RAM entries to the device; persisted paths are
/// already adapter-scoped and encode the root's static-sampler ABI in the state hash.
static GRAPHICS_BLOBS: DriverBlobs<(u64, usize, usize)> = DriverBlobs::new();
const GRAPHICS_BLOBS_MAX: usize = 256 << 20;

fn graphics_blob(key: (u64, usize)) -> Option<std::sync::Arc<Vec<u8>>> {
fn graphics_blob(key: (u64, usize, usize)) -> Option<std::sync::Arc<Vec<u8>>> {
GRAPHICS_BLOBS.get(key)
}

fn forget_graphics_blob(key: (u64, usize), rejected: &std::sync::Arc<Vec<u8>>) -> bool {
fn forget_graphics_blob(key: (u64, usize, usize), rejected: &std::sync::Arc<Vec<u8>>) -> bool {
GRAPHICS_BLOBS.forget_if(key, rejected)
}

fn remember_graphics_blob(key: (u64, usize), blob: std::sync::Arc<Vec<u8>>) {
fn remember_graphics_blob(key: (u64, usize, usize), blob: std::sync::Arc<Vec<u8>>) {
GRAPHICS_BLOBS.remember(key, blob, GRAPHICS_BLOBS_MAX);
}

fn forget_graphics_blob_if(key: (u64, usize), expected: &std::sync::Arc<Vec<u8>>) -> bool {
fn forget_graphics_blob_if(key: (u64, usize, usize), expected: &std::sync::Arc<Vec<u8>>) -> bool {
GRAPHICS_BLOBS.forget_if(key, expected)
}

Expand All @@ -5920,7 +5922,7 @@ unsafe fn graphics_blob_key(
copy: &D3D12_GRAPHICS_PIPELINE_STATE_DESC,
vs_dxil: &[u8],
ps_dxil: Option<&[u8]>,
rect_gs: Option<&[u8]>,
gs_dxil: Option<&[u8]>,
samplers: &[crate::gpu::recomp::pso::StaticSampler],
) -> (u64, usize) {
let mut h = 0xcbf2_9ce4_8422_2325u64;
Expand All @@ -5942,7 +5944,7 @@ unsafe fn graphics_blob_key(
} else {
feed_u64(&mut h, 0);
}
if let Some(gs) = rect_gs {
if let Some(gs) = gs_dxil {
feed_u64(&mut h, crate::gpu::shader::fnv64(gs));
feed_u64(&mut h, gs.len() as u64);
} else {
Expand Down Expand Up @@ -6027,10 +6029,19 @@ unsafe fn graphics_blob_key(
feed_u32(&mut h, copy.SampleDesc.Quality);
feed_u32(&mut h, copy.NodeMask);
feed_u32(&mut h, copy.Flags.0 as u32);
let total_bytes = vs_dxil.len() + ps_dxil.map_or(0, <[u8]>::len) + rect_gs.map_or(0, <[u8]>::len);
let total_bytes = vs_dxil.len() + ps_dxil.map_or(0, <[u8]>::len) + gs_dxil.map_or(0, <[u8]>::len);
(h, total_bytes)
}

/// Stream-output declarations contain pointer-backed arrays that are not represented in the
/// graphics blob key. Keep those PSOs out of this cache rather than aliasing distinct layouts.
fn graphics_blob_cacheable(requested: bool, desc: &D3D12_GRAPHICS_PIPELINE_STATE_DESC, gs_readable: bool) -> bool {
requested
&& desc.StreamOutput.NumEntries == 0
&& desc.StreamOutput.NumStrides == 0
&& (desc.GS.BytecodeLength == 0 || gs_readable)
}

#[cfg(test)]
mod graphics_blob_tests {
use super::*;
Expand All @@ -6056,6 +6067,22 @@ mod graphics_blob_tests {
}
}

#[test]
fn graphics_blob_cache_skips_pointer_backed_stream_output() {
let mut d = D3D12_GRAPHICS_PIPELINE_STATE_DESC::default();
assert!(graphics_blob_cacheable(true, &d, false));
assert!(!graphics_blob_cacheable(false, &d, false));
d.StreamOutput.NumEntries = 1;
assert!(!graphics_blob_cacheable(true, &d, false));
d.StreamOutput.NumEntries = 0;
d.StreamOutput.NumStrides = 1;
assert!(!graphics_blob_cacheable(true, &d, false));
d.StreamOutput.NumStrides = 0;
d.GS.BytecodeLength = 4;
assert!(!graphics_blob_cacheable(true, &d, false), "an unreadable native GS has no safe key");
assert!(graphics_blob_cacheable(true, &d, true));
}

#[test]
fn graphics_blob_key_ignores_struct_padding_bytes() {
unsafe {
Expand Down Expand Up @@ -6385,10 +6412,13 @@ unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const
if debug_pso_switch("XCL_DEBUG_NO_CULL", ps_hash) {
copy.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
}
let bytes = vs_dxil.len() + ps_dxil.as_ref().map_or(0, Vec::len) + rect_gs.as_ref().map_or(0, Vec::len);
let gkey = graphics_blob_key(&copy, &vs_dxil, ps_dxil.as_deref(), rect_gs.as_deref(), &samplers);
let final_gs = shader_bytes(&copy.GS);
let bytes = vs_dxil.len() + ps_dxil.as_ref().map_or(0, Vec::len) + final_gs.map_or(0, <[u8]>::len);
let disk_key = graphics_blob_key(&copy, &vs_dxil, ps_dxil.as_deref(), final_gs, &samplers);
let gkey = (disk_key.0, disk_key.1, rs as usize);
let cacheable = graphics_blob_cacheable(cacheable, &copy, final_gs.is_some());
let mut cached = cacheable.then(|| graphics_blob(gkey)).flatten();
let disk = cacheable.then(|| driver_cache::graphics_path(gkey.0, gkey.1)).flatten();
let disk = cacheable.then(|| driver_cache::graphics_path(disk_key.0, disk_key.1)).flatten();
let from_disk = cached.is_none();
let mut loaded = None;
if from_disk {
Expand Down Expand Up @@ -7200,10 +7230,11 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu:
*ppv = std::ptr::null_mut();
let Some(rs) = abi_rs_for(this, &tp.samplers) else { return E_INVALIDARG };
let mesh_bytes = stream.shader_bytes();
let gkey = (mesh_bytes != 0).then(|| two_pass_driver_id(this)).flatten().and_then(|driver| {
let disk_key = (mesh_bytes != 0).then(|| two_pass_driver_id(this)).flatten().and_then(|driver| {
two_pass_identity::two_pass_stream_key(&stream.0, ps_samplers, driver, crate::gpu::recomp::pso::mem_windows())
});
let gdisk = gkey.and_then(|(hash, bytes)| driver_cache::graphics_path(hash, bytes));
let gkey = disk_key.map(|(hash, bytes)| (hash, bytes, mesh_rs as usize));
let gdisk = disk_key.and_then(|(hash, bytes)| driver_cache::graphics_path(hash, bytes));
let mut gcached = gkey.and_then(graphics_blob);
let gfrom_disk = gcached.is_none();
let mut gloaded = None;
Expand Down Expand Up @@ -8269,9 +8300,10 @@ unsafe fn translated_graphics_stream_once(this: *mut c_void, desc: *const c_void
let fs: PsoFn = std::mem::transmute(orig_fn(this, slot_of(this, "CreatePipelineState")));
let stream = rewrite_graphics_stream(&subs, rs, &vs_c.dxil, ps_dxil.as_deref(), rect_gs.as_deref(), no_stencil, no_cull);
let without_rect_gs = rect_gs.as_ref().map(|_| rewrite_graphics_stream(&subs, rs, &vs_c.dxil, ps_dxil.as_deref(), None, no_stencil, no_cull));
let key = two_pass_identity::graphics_stream_key(&stream.0, &samplers, crate::gpu::recomp::pso::mem_windows());
let disk_key = two_pass_identity::graphics_stream_key(&stream.0, &samplers, crate::gpu::recomp::pso::mem_windows());
let key = disk_key.map(|(hash, bytes)| (hash, bytes, rs as usize));
let mut cached = key.and_then(graphics_blob);
let disk = key.and_then(|k| driver_cache::graphics_path(k.0, k.1));
let disk = disk_key.and_then(|k| driver_cache::graphics_path(k.0, k.1));
let from_disk = key.is_some() && cached.is_none();
let mut loaded = None;
if from_disk {
Expand Down