From 22f87aa7f8561ca62dc108071deb5365744cf434 Mon Sep 17 00:00:00 2001 From: chyyran Date: Mon, 5 Dec 2022 23:09:59 -0500 Subject: [PATCH] reflect: add `FromCompilation` for SpirV --- librashader-reflect/src/back/mod.rs | 1 + librashader-reflect/src/back/spirv.rs | 56 +++++++++++++++++++++++++ librashader-reflect/src/back/targets.rs | 4 +- librashader/src/lib.rs | 2 +- 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 librashader-reflect/src/back/spirv.rs diff --git a/librashader-reflect/src/back/mod.rs b/librashader-reflect/src/back/mod.rs index 6784118..3faeb13 100644 --- a/librashader-reflect/src/back/mod.rs +++ b/librashader-reflect/src/back/mod.rs @@ -1,5 +1,6 @@ pub mod cross; pub mod targets; +mod spirv; use crate::back::targets::OutputTarget; use crate::error::{ShaderCompileError, ShaderReflectError}; diff --git a/librashader-reflect/src/back/spirv.rs b/librashader-reflect/src/back/spirv.rs new file mode 100644 index 0000000..4b7ace8 --- /dev/null +++ b/librashader-reflect/src/back/spirv.rs @@ -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, + fragment: Vec +} + +impl FromCompilation for SpirV { + type Target = SpirV; + type Options = Option<()>; + type Context = (); + type Output = impl CompileShader, Context = ()> + + ReflectShader; + + fn from_compilation( + compile: GlslangCompilation, + ) -> Result, 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 { + self.reflect.reflect(pass_number, semantics) + } +} + +impl CompileShader for WriteSpirV { + type Options = Option<()>; + type Context = (); + + fn compile(self, _options: Self::Options) -> Result, Self::Context>, ShaderCompileError> { + Ok(ShaderCompilerOutput { + vertex: self.vertex, + fragment: self.fragment, + context: (), + }) + } +} \ No newline at end of file diff --git a/librashader-reflect/src/back/targets.rs b/librashader-reflect/src/back/targets.rs index d1e5955..8999bf9 100644 --- a/librashader-reflect/src/back/targets.rs +++ b/librashader-reflect/src/back/targets.rs @@ -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; } diff --git a/librashader/src/lib.rs b/librashader/src/lib.rs index 2fe4c4e..86e7cf6 100644 --- a/librashader/src/lib.rs +++ b/librashader/src/lib.rs @@ -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::*;