2023-02-16 09:40:24 +11:00
|
|
|
//! Cache helpers for `ShaderCompilation` objects to cache compiled SPIRV.
|
2023-02-13 14:36:50 +11:00
|
|
|
use librashader_preprocess::ShaderSource;
|
2023-12-14 11:24:00 +11:00
|
|
|
#[cfg(all(target_os = "windows", feature = "d3d"))]
|
|
|
|
use librashader_reflect::back::targets::DXIL;
|
2024-02-06 17:32:08 +11:00
|
|
|
use librashader_reflect::back::targets::{GLSL, HLSL, SPIRV};
|
2023-12-14 11:24:00 +11:00
|
|
|
|
2023-02-13 14:36:50 +11:00
|
|
|
use librashader_reflect::back::{CompilerBackend, FromCompilation};
|
|
|
|
use librashader_reflect::error::{ShaderCompileError, ShaderReflectError};
|
2024-02-11 10:54:57 +11:00
|
|
|
use librashader_reflect::front::{
|
|
|
|
Glslang, ShaderInputCompiler, ShaderReflectObject, SpirvCompilation,
|
|
|
|
};
|
2023-02-13 14:36:50 +11:00
|
|
|
|
|
|
|
pub struct CachedCompilation<T> {
|
|
|
|
compilation: T,
|
|
|
|
}
|
|
|
|
|
2024-02-15 11:22:25 +11:00
|
|
|
impl<T: ShaderReflectObject> ShaderReflectObject for CachedCompilation<T> {
|
|
|
|
type Compiler = T::Compiler;
|
|
|
|
}
|
2024-02-11 09:47:05 +11:00
|
|
|
|
|
|
|
impl<T: ShaderReflectObject + for<'de> serde::Deserialize<'de> + serde::Serialize + Clone>
|
2024-02-11 10:54:57 +11:00
|
|
|
ShaderInputCompiler<CachedCompilation<T>> for Glslang
|
|
|
|
where
|
|
|
|
Glslang: ShaderInputCompiler<T>,
|
2023-02-13 14:36:50 +11:00
|
|
|
{
|
2024-02-11 09:47:05 +11:00
|
|
|
fn compile(source: &ShaderSource) -> Result<CachedCompilation<T>, ShaderCompileError> {
|
2023-02-16 16:39:36 +11:00
|
|
|
let cache = crate::cache::internal::get_cache();
|
2023-02-13 14:36:50 +11:00
|
|
|
|
|
|
|
let Ok(cache) = cache else {
|
|
|
|
return Ok(CachedCompilation {
|
2024-02-11 09:47:05 +11:00
|
|
|
compilation: Glslang::compile(source)?,
|
2023-07-20 15:13:22 +10:00
|
|
|
});
|
2023-02-13 14:36:50 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
let key = {
|
|
|
|
let mut hasher = blake3::Hasher::new();
|
|
|
|
hasher.update(source.vertex.as_bytes());
|
|
|
|
hasher.update(source.fragment.as_bytes());
|
|
|
|
let hash = hasher.finalize();
|
|
|
|
hash
|
|
|
|
};
|
|
|
|
|
|
|
|
let compilation = 'cached: {
|
2024-02-15 11:22:25 +11:00
|
|
|
if let Ok(Some(cached)) =
|
|
|
|
crate::cache::internal::get_blob(&cache, "spirv", key.as_bytes())
|
|
|
|
{
|
2023-02-13 14:36:50 +11:00
|
|
|
let decoded =
|
|
|
|
bincode::serde::decode_from_slice(&cached, bincode::config::standard())
|
|
|
|
.map(|(compilation, _)| CachedCompilation { compilation })
|
|
|
|
.ok();
|
|
|
|
|
|
|
|
if let Some(compilation) = decoded {
|
|
|
|
break 'cached compilation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CachedCompilation {
|
2024-02-11 09:47:05 +11:00
|
|
|
compilation: Glslang::compile(source)?,
|
2023-02-13 14:36:50 +11:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Ok(updated) =
|
|
|
|
bincode::serde::encode_to_vec(&compilation.compilation, bincode::config::standard())
|
|
|
|
{
|
2024-02-15 11:22:25 +11:00
|
|
|
let Ok(()) =
|
|
|
|
crate::cache::internal::set_blob(&cache, "spirv", key.as_bytes(), &updated)
|
|
|
|
else {
|
2024-02-14 16:46:15 +11:00
|
|
|
return Ok(compilation);
|
|
|
|
};
|
2023-02-13 14:36:50 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(compilation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 18:06:20 +11:00
|
|
|
#[cfg(all(target_os = "windows", feature = "d3d"))]
|
2024-02-11 12:46:35 +11:00
|
|
|
impl<T> FromCompilation<CachedCompilation<SpirvCompilation>, T> for DXIL
|
|
|
|
where
|
|
|
|
DXIL: FromCompilation<SpirvCompilation, T>,
|
|
|
|
{
|
|
|
|
type Target = <DXIL as FromCompilation<SpirvCompilation, T>>::Target;
|
|
|
|
type Options = <DXIL as FromCompilation<SpirvCompilation, T>>::Options;
|
|
|
|
type Context = <DXIL as FromCompilation<SpirvCompilation, T>>::Context;
|
|
|
|
type Output = <DXIL as FromCompilation<SpirvCompilation, T>>::Output;
|
2023-02-13 14:36:50 +11:00
|
|
|
|
|
|
|
fn from_compilation(
|
2024-02-11 09:47:05 +11:00
|
|
|
compile: CachedCompilation<SpirvCompilation>,
|
2023-02-13 14:36:50 +11:00
|
|
|
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
|
|
|
|
DXIL::from_compilation(compile.compilation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-11 12:46:35 +11:00
|
|
|
impl<T> FromCompilation<CachedCompilation<SpirvCompilation>, T> for HLSL
|
|
|
|
where
|
|
|
|
HLSL: FromCompilation<SpirvCompilation, T>,
|
|
|
|
{
|
|
|
|
type Target = <HLSL as FromCompilation<SpirvCompilation, T>>::Target;
|
|
|
|
type Options = <HLSL as FromCompilation<SpirvCompilation, T>>::Options;
|
|
|
|
type Context = <HLSL as FromCompilation<SpirvCompilation, T>>::Context;
|
|
|
|
type Output = <HLSL as FromCompilation<SpirvCompilation, T>>::Output;
|
2023-02-13 14:36:50 +11:00
|
|
|
|
|
|
|
fn from_compilation(
|
2024-02-11 09:47:05 +11:00
|
|
|
compile: CachedCompilation<SpirvCompilation>,
|
2023-02-13 14:36:50 +11:00
|
|
|
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
|
|
|
|
HLSL::from_compilation(compile.compilation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-11 12:46:35 +11:00
|
|
|
impl<T> FromCompilation<CachedCompilation<SpirvCompilation>, T> for GLSL
|
|
|
|
where
|
|
|
|
GLSL: FromCompilation<SpirvCompilation, T>,
|
|
|
|
{
|
|
|
|
type Target = <GLSL as FromCompilation<SpirvCompilation, T>>::Target;
|
|
|
|
type Options = <GLSL as FromCompilation<SpirvCompilation, T>>::Options;
|
|
|
|
type Context = <GLSL as FromCompilation<SpirvCompilation, T>>::Context;
|
|
|
|
type Output = <GLSL as FromCompilation<SpirvCompilation, T>>::Output;
|
2023-02-13 14:36:50 +11:00
|
|
|
|
|
|
|
fn from_compilation(
|
2024-02-11 09:47:05 +11:00
|
|
|
compile: CachedCompilation<SpirvCompilation>,
|
2023-02-13 14:36:50 +11:00
|
|
|
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
|
|
|
|
GLSL::from_compilation(compile.compilation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-11 12:46:35 +11:00
|
|
|
impl<T> FromCompilation<CachedCompilation<SpirvCompilation>, T> for SPIRV
|
|
|
|
where
|
|
|
|
SPIRV: FromCompilation<SpirvCompilation, T>,
|
|
|
|
{
|
|
|
|
type Target = <SPIRV as FromCompilation<SpirvCompilation, T>>::Target;
|
|
|
|
type Options = <SPIRV as FromCompilation<SpirvCompilation, T>>::Options;
|
|
|
|
type Context = <SPIRV as FromCompilation<SpirvCompilation, T>>::Context;
|
|
|
|
type Output = <SPIRV as FromCompilation<SpirvCompilation, T>>::Output;
|
2023-02-13 14:36:50 +11:00
|
|
|
|
|
|
|
fn from_compilation(
|
2024-02-11 09:47:05 +11:00
|
|
|
compile: CachedCompilation<SpirvCompilation>,
|
2023-02-13 14:36:50 +11:00
|
|
|
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
|
|
|
|
SPIRV::from_compilation(compile.compilation)
|
|
|
|
}
|
|
|
|
}
|