2023-02-13 14:36:50 +11:00
|
|
|
//! Cache implementations for D3D blob types that need to live
|
|
|
|
//! here because of the orphan rule.
|
|
|
|
|
2023-02-15 19:06:34 +11:00
|
|
|
use crate::{CacheKey, Cacheable};
|
2024-08-02 14:53:13 +10:00
|
|
|
use windows::core::Interface;
|
2023-02-13 14:36:50 +11:00
|
|
|
|
|
|
|
impl CacheKey for windows::Win32::Graphics::Direct3D::ID3DBlob {
|
|
|
|
fn hash_bytes(&self) -> &[u8] {
|
|
|
|
unsafe { std::slice::from_raw_parts(self.GetBufferPointer().cast(), self.GetBufferSize()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CacheKey for windows::Win32::Graphics::Direct3D::Dxc::IDxcBlob {
|
|
|
|
fn hash_bytes(&self) -> &[u8] {
|
|
|
|
unsafe { std::slice::from_raw_parts(self.GetBufferPointer().cast(), self.GetBufferSize()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Cacheable for windows::Win32::Graphics::Direct3D::ID3DBlob {
|
|
|
|
fn from_bytes(bytes: &[u8]) -> Option<Self> {
|
2023-07-20 15:13:22 +10:00
|
|
|
let Some(blob) =
|
|
|
|
(unsafe { windows::Win32::Graphics::Direct3D::Fxc::D3DCreateBlob(bytes.len()).ok() })
|
|
|
|
else {
|
2023-02-13 14:36:50 +11:00
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
|
|
|
let slice = unsafe {
|
|
|
|
std::slice::from_raw_parts_mut(blob.GetBufferPointer().cast(), blob.GetBufferSize())
|
|
|
|
};
|
|
|
|
|
|
|
|
slice.copy_from_slice(bytes);
|
|
|
|
|
|
|
|
Some(blob)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_bytes(&self) -> Option<Vec<u8>> {
|
|
|
|
let slice = unsafe {
|
|
|
|
std::slice::from_raw_parts(self.GetBufferPointer().cast(), self.GetBufferSize())
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(Vec::from(slice))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Cacheable for windows::Win32::Graphics::Direct3D::Dxc::IDxcBlob {
|
|
|
|
fn from_bytes(bytes: &[u8]) -> Option<Self> {
|
|
|
|
let Some(blob) = (unsafe {
|
|
|
|
windows::Win32::Graphics::Direct3D::Dxc::DxcCreateInstance(
|
2023-07-20 15:13:22 +10:00
|
|
|
&windows::Win32::Graphics::Direct3D::Dxc::CLSID_DxcLibrary,
|
|
|
|
)
|
|
|
|
.and_then(
|
|
|
|
|library: windows::Win32::Graphics::Direct3D::Dxc::IDxcUtils| {
|
|
|
|
library.CreateBlob(
|
|
|
|
bytes.as_ptr().cast(),
|
|
|
|
bytes.len() as u32,
|
|
|
|
windows::Win32::Graphics::Direct3D::Dxc::DXC_CP(0),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.ok()
|
2023-02-13 14:36:50 +11:00
|
|
|
}) else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
2023-04-23 15:13:31 +10:00
|
|
|
Some(blob.cast().ok()?)
|
2023-02-13 14:36:50 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn to_bytes(&self) -> Option<Vec<u8>> {
|
|
|
|
let slice = unsafe {
|
|
|
|
std::slice::from_raw_parts(self.GetBufferPointer().cast(), self.GetBufferSize())
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(Vec::from(slice))
|
|
|
|
}
|
|
|
|
}
|