reflect: add FromCompilation<GlslangCompilation> for SpirV

This commit is contained in:
chyyran 2022-12-05 23:09:59 -05:00
parent a8840829aa
commit 22f87aa7f8
4 changed files with 60 additions and 3 deletions

View file

@ -1,5 +1,6 @@
pub mod cross;
pub mod targets;
mod spirv;
use crate::back::targets::OutputTarget;
use crate::error::{ShaderCompileError, ShaderReflectError};

View file

@ -0,0 +1,56 @@
use crate::back::{CompilerBackend, CompileShader, FromCompilation, ShaderCompilerOutput};
use crate::back::targets::SpirV;
use crate::error::{ShaderCompileError, ShaderReflectError};
use crate::front::shaderc::GlslangCompilation;
use crate::reflect::{ReflectShader, ShaderReflection};
use crate::reflect::cross::{GlslReflect};
use crate::reflect::semantics::ShaderSemantics;
struct WriteSpirV {
// rely on GLSL to provide out reflection but we don't actually need the AST.
reflect: GlslReflect,
vertex: Vec<u32>,
fragment: Vec<u32>
}
impl FromCompilation<GlslangCompilation> for SpirV {
type Target = SpirV;
type Options = Option<()>;
type Context = ();
type Output = impl CompileShader<Self::Target, Options = Option<()>, Context = ()>
+ ReflectShader;
fn from_compilation(
compile: GlslangCompilation,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
let vertex = compile.vertex.as_binary().to_vec();
let fragment = compile.vertex.as_binary().to_vec();
let reflect = GlslReflect::try_from(compile)?;
Ok(CompilerBackend {
backend: WriteSpirV {
reflect,
vertex,
fragment,
},
})
}
}
impl ReflectShader for WriteSpirV {
fn reflect(&mut self, pass_number: usize, semantics: &ShaderSemantics) -> Result<ShaderReflection, ShaderReflectError> {
self.reflect.reflect(pass_number, semantics)
}
}
impl CompileShader<SpirV> for WriteSpirV {
type Options = Option<()>;
type Context = ();
fn compile(self, _options: Self::Options) -> Result<ShaderCompilerOutput<Vec<u32>, Self::Context>, ShaderCompileError> {
Ok(ShaderCompilerOutput {
vertex: self.vertex,
fragment: self.fragment,
context: (),
})
}
}

View file

@ -9,7 +9,7 @@ pub struct GLSL;
/// Shader compiler target for HLSL.
pub struct HLSL;
/// Shader compiler target for SPIR-V.
pub struct SPIRV;
pub struct SpirV;
/// Shader compiler target for MSL
pub struct MSL;
@ -19,7 +19,7 @@ impl OutputTarget for GLSL {
impl OutputTarget for HLSL {
type Output = String;
}
impl OutputTarget for SPIRV {
impl OutputTarget for SpirV {
type Output = Vec<u32>;
}

View file

@ -68,7 +68,7 @@ pub mod reflect {
pub mod targets {
pub use librashader_reflect::back::targets::GLSL;
pub use librashader_reflect::back::targets::HLSL;
pub use librashader_reflect::back::targets::SPIRV;
pub use librashader_reflect::back::targets::SpirV;
}
pub use librashader_reflect::error::*;