reflect: abstract away input compiler from compilation

This commit is contained in:
chyyran 2024-02-10 17:47:05 -05:00 committed by Ronny Chan
parent 11d12730eb
commit a7ca391ef6
19 changed files with 145 additions and 122 deletions

View file

@ -6,21 +6,25 @@ use librashader_reflect::back::targets::{GLSL, HLSL, SPIRV};
use librashader_reflect::back::{CompilerBackend, FromCompilation}; use librashader_reflect::back::{CompilerBackend, FromCompilation};
use librashader_reflect::error::{ShaderCompileError, ShaderReflectError}; use librashader_reflect::error::{ShaderCompileError, ShaderReflectError};
use librashader_reflect::front::{GlslangCompilation, ShaderCompilation}; use librashader_reflect::front::{Glslang, ShaderInputCompiler, ShaderReflectObject, SpirvCompilation};
pub struct CachedCompilation<T> { pub struct CachedCompilation<T> {
compilation: T, compilation: T,
} }
impl<T: ShaderCompilation + for<'de> serde::Deserialize<'de> + serde::Serialize + Clone> impl <T: ShaderReflectObject> ShaderReflectObject for CachedCompilation<T> {
ShaderCompilation for CachedCompilation<T>
}
impl<T: ShaderReflectObject + for<'de> serde::Deserialize<'de> + serde::Serialize + Clone>
ShaderInputCompiler<CachedCompilation<T>> for Glslang where Glslang: ShaderInputCompiler<T>
{ {
fn compile(source: &ShaderSource) -> Result<Self, ShaderCompileError> { fn compile(source: &ShaderSource) -> Result<CachedCompilation<T>, ShaderCompileError> {
let cache = crate::cache::internal::get_cache(); let cache = crate::cache::internal::get_cache();
let Ok(cache) = cache else { let Ok(cache) = cache else {
return Ok(CachedCompilation { return Ok(CachedCompilation {
compilation: T::compile(source)?, compilation: Glslang::compile(source)?,
}); });
}; };
@ -45,7 +49,7 @@ impl<T: ShaderCompilation + for<'de> serde::Deserialize<'de> + serde::Serialize
} }
CachedCompilation { CachedCompilation {
compilation: T::compile(source)?, compilation: Glslang::compile(source)?,
} }
}; };
@ -60,53 +64,53 @@ impl<T: ShaderCompilation + for<'de> serde::Deserialize<'de> + serde::Serialize
} }
#[cfg(all(target_os = "windows", feature = "d3d"))] #[cfg(all(target_os = "windows", feature = "d3d"))]
impl FromCompilation<CachedCompilation<GlslangCompilation>> for DXIL { impl FromCompilation<CachedCompilation<SpirvCompilation>> for DXIL {
type Target = <DXIL as FromCompilation<GlslangCompilation>>::Target; type Target = <DXIL as FromCompilation<SpirvCompilation>>::Target;
type Options = <DXIL as FromCompilation<GlslangCompilation>>::Options; type Options = <DXIL as FromCompilation<SpirvCompilation>>::Options;
type Context = <DXIL as FromCompilation<GlslangCompilation>>::Context; type Context = <DXIL as FromCompilation<SpirvCompilation>>::Context;
type Output = <DXIL as FromCompilation<GlslangCompilation>>::Output; type Output = <DXIL as FromCompilation<SpirvCompilation>>::Output;
fn from_compilation( fn from_compilation(
compile: CachedCompilation<GlslangCompilation>, compile: CachedCompilation<SpirvCompilation>,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> { ) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
DXIL::from_compilation(compile.compilation) DXIL::from_compilation(compile.compilation)
} }
} }
impl FromCompilation<CachedCompilation<GlslangCompilation>> for HLSL { impl FromCompilation<CachedCompilation<SpirvCompilation>> for HLSL {
type Target = <HLSL as FromCompilation<GlslangCompilation>>::Target; type Target = <HLSL as FromCompilation<SpirvCompilation>>::Target;
type Options = <HLSL as FromCompilation<GlslangCompilation>>::Options; type Options = <HLSL as FromCompilation<SpirvCompilation>>::Options;
type Context = <HLSL as FromCompilation<GlslangCompilation>>::Context; type Context = <HLSL as FromCompilation<SpirvCompilation>>::Context;
type Output = <HLSL as FromCompilation<GlslangCompilation>>::Output; type Output = <HLSL as FromCompilation<SpirvCompilation>>::Output;
fn from_compilation( fn from_compilation(
compile: CachedCompilation<GlslangCompilation>, compile: CachedCompilation<SpirvCompilation>,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> { ) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
HLSL::from_compilation(compile.compilation) HLSL::from_compilation(compile.compilation)
} }
} }
impl FromCompilation<CachedCompilation<GlslangCompilation>> for GLSL { impl FromCompilation<CachedCompilation<SpirvCompilation>> for GLSL {
type Target = <GLSL as FromCompilation<GlslangCompilation>>::Target; type Target = <GLSL as FromCompilation<SpirvCompilation>>::Target;
type Options = <GLSL as FromCompilation<GlslangCompilation>>::Options; type Options = <GLSL as FromCompilation<SpirvCompilation>>::Options;
type Context = <GLSL as FromCompilation<GlslangCompilation>>::Context; type Context = <GLSL as FromCompilation<SpirvCompilation>>::Context;
type Output = <GLSL as FromCompilation<GlslangCompilation>>::Output; type Output = <GLSL as FromCompilation<SpirvCompilation>>::Output;
fn from_compilation( fn from_compilation(
compile: CachedCompilation<GlslangCompilation>, compile: CachedCompilation<SpirvCompilation>,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> { ) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
GLSL::from_compilation(compile.compilation) GLSL::from_compilation(compile.compilation)
} }
} }
impl FromCompilation<CachedCompilation<GlslangCompilation>> for SPIRV { impl FromCompilation<CachedCompilation<SpirvCompilation>> for SPIRV {
type Target = <SPIRV as FromCompilation<GlslangCompilation>>::Target; type Target = <SPIRV as FromCompilation<SpirvCompilation>>::Target;
type Options = <SPIRV as FromCompilation<GlslangCompilation>>::Options; type Options = <SPIRV as FromCompilation<SpirvCompilation>>::Options;
type Context = <SPIRV as FromCompilation<GlslangCompilation>>::Context; type Context = <SPIRV as FromCompilation<SpirvCompilation>>::Context;
type Output = <SPIRV as FromCompilation<GlslangCompilation>>::Output; type Output = <SPIRV as FromCompilation<SpirvCompilation>>::Output;
fn from_compilation( fn from_compilation(
compile: CachedCompilation<GlslangCompilation>, compile: CachedCompilation<SpirvCompilation>,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> { ) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
SPIRV::from_compilation(compile.compilation) SPIRV::from_compilation(compile.compilation)
} }

View file

@ -1,7 +1,7 @@
use crate::back::targets::{GLSL, HLSL}; use crate::back::targets::{GLSL, HLSL};
use crate::back::{CompileShader, CompilerBackend, FromCompilation}; use crate::back::{CompileShader, CompilerBackend, FromCompilation};
use crate::error::ShaderReflectError; use crate::error::ShaderReflectError;
use crate::front::GlslangCompilation; use crate::front::SpirvCompilation;
use crate::reflect::cross::{CompiledProgram, GlslReflect, HlslReflect}; use crate::reflect::cross::{CompiledProgram, GlslReflect, HlslReflect};
use crate::reflect::ReflectShader; use crate::reflect::ReflectShader;
@ -19,7 +19,7 @@ pub struct CrossGlslContext {
pub artifact: CompiledProgram<spirv_cross::glsl::Target>, pub artifact: CompiledProgram<spirv_cross::glsl::Target>,
} }
impl FromCompilation<GlslangCompilation> for GLSL { impl FromCompilation<SpirvCompilation> for GLSL {
type Target = GLSL; type Target = GLSL;
type Options = GlslVersion; type Options = GlslVersion;
type Context = CrossGlslContext; type Context = CrossGlslContext;
@ -27,7 +27,7 @@ impl FromCompilation<GlslangCompilation> for GLSL {
+ ReflectShader; + ReflectShader;
fn from_compilation( fn from_compilation(
compile: GlslangCompilation, compile: SpirvCompilation,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> { ) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
Ok(CompilerBackend { Ok(CompilerBackend {
backend: GlslReflect::try_from(&compile)?, backend: GlslReflect::try_from(&compile)?,
@ -41,7 +41,7 @@ pub struct CrossHlslContext {
pub artifact: CompiledProgram<spirv_cross::hlsl::Target>, pub artifact: CompiledProgram<spirv_cross::hlsl::Target>,
} }
impl FromCompilation<GlslangCompilation> for HLSL { impl FromCompilation<SpirvCompilation> for HLSL {
type Target = HLSL; type Target = HLSL;
type Options = Option<HlslShaderModel>; type Options = Option<HlslShaderModel>;
type Context = CrossHlslContext; type Context = CrossHlslContext;
@ -49,7 +49,7 @@ impl FromCompilation<GlslangCompilation> for HLSL {
+ ReflectShader; + ReflectShader;
fn from_compilation( fn from_compilation(
compile: GlslangCompilation, compile: SpirvCompilation,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> { ) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
Ok(CompilerBackend { Ok(CompilerBackend {
backend: HlslReflect::try_from(&compile)?, backend: HlslReflect::try_from(&compile)?,

View file

@ -8,7 +8,7 @@ use spirv_to_dxil::{
use crate::back::targets::{OutputTarget, DXIL}; use crate::back::targets::{OutputTarget, DXIL};
use crate::error::{ShaderCompileError, ShaderReflectError}; use crate::error::{ShaderCompileError, ShaderReflectError};
use crate::front::GlslangCompilation; use crate::front::SpirvCompilation;
use crate::reflect::cross::GlslReflect; use crate::reflect::cross::GlslReflect;
use crate::reflect::ReflectShader; use crate::reflect::ReflectShader;
@ -16,7 +16,7 @@ impl OutputTarget for DXIL {
type Output = DxilObject; type Output = DxilObject;
} }
impl FromCompilation<GlslangCompilation> for DXIL { impl FromCompilation<SpirvCompilation> for DXIL {
type Target = DXIL; type Target = DXIL;
type Options = Option<ShaderModel>; type Options = Option<ShaderModel>;
type Context = (); type Context = ();
@ -24,7 +24,7 @@ impl FromCompilation<GlslangCompilation> for DXIL {
+ ReflectShader; + ReflectShader;
fn from_compilation( fn from_compilation(
compile: GlslangCompilation, compile: SpirvCompilation,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> { ) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
let reflect = GlslReflect::try_from(&compile)?; let reflect = GlslReflect::try_from(&compile)?;
let vertex = compile.vertex; let vertex = compile.vertex;

View file

@ -4,12 +4,14 @@ pub mod dxil;
mod spirv; mod spirv;
pub mod targets; pub mod targets;
pub mod wgsl; pub mod wgsl;
mod msl;
use crate::back::targets::OutputTarget; use crate::back::targets::OutputTarget;
use crate::error::{ShaderCompileError, ShaderReflectError}; use crate::error::{ShaderCompileError, ShaderReflectError};
use crate::reflect::semantics::ShaderSemantics; use crate::reflect::semantics::ShaderSemantics;
use crate::reflect::{ReflectShader, ShaderReflection}; use crate::reflect::{ReflectShader, ShaderReflection};
use std::fmt::Debug; use std::fmt::Debug;
use crate::front::ShaderReflectObject;
/// The output of the shader compiler. /// The output of the shader compiler.
#[derive(Debug)] #[derive(Debug)]
@ -118,11 +120,11 @@ where
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::front::GlslangCompilation; use crate::front::{Glslang, ShaderInputCompiler, SpirvCompilation};
use librashader_preprocess::ShaderSource; use librashader_preprocess::ShaderSource;
pub fn test() { pub fn test() {
let result = ShaderSource::load("../test/basic.slang").unwrap(); let result = ShaderSource::load("../test/basic.slang").unwrap();
let _cross = GlslangCompilation::compile(&result).unwrap(); let _cross = Glslang::compile(&result).unwrap();
} }
} }

View file

View file

@ -1,7 +1,7 @@
use crate::back::targets::SPIRV; use crate::back::targets::SPIRV;
use crate::back::{CompileShader, CompilerBackend, FromCompilation, ShaderCompilerOutput}; use crate::back::{CompileShader, CompilerBackend, FromCompilation, ShaderCompilerOutput};
use crate::error::{ShaderCompileError, ShaderReflectError}; use crate::error::{ShaderCompileError, ShaderReflectError};
use crate::front::GlslangCompilation; use crate::front::SpirvCompilation;
use crate::reflect::cross::GlslReflect; use crate::reflect::cross::GlslReflect;
use crate::reflect::semantics::ShaderSemantics; use crate::reflect::semantics::ShaderSemantics;
use crate::reflect::{ReflectShader, ShaderReflection}; use crate::reflect::{ReflectShader, ShaderReflection};
@ -13,7 +13,7 @@ pub(crate) struct WriteSpirV {
pub(crate) fragment: Vec<u32>, pub(crate) fragment: Vec<u32>,
} }
impl FromCompilation<GlslangCompilation> for SPIRV { impl FromCompilation<SpirvCompilation> for SPIRV {
type Target = SPIRV; type Target = SPIRV;
type Options = Option<()>; type Options = Option<()>;
type Context = (); type Context = ();
@ -21,7 +21,7 @@ impl FromCompilation<GlslangCompilation> for SPIRV {
+ ReflectShader; + ReflectShader;
fn from_compilation( fn from_compilation(
compile: GlslangCompilation, compile: SpirvCompilation,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> { ) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
let reflect = GlslReflect::try_from(&compile)?; let reflect = GlslReflect::try_from(&compile)?;
let vertex = compile.vertex; let vertex = compile.vertex;

View file

@ -34,6 +34,9 @@ impl OutputTarget for HLSL {
impl OutputTarget for WGSL { impl OutputTarget for WGSL {
type Output = String; type Output = String;
} }
impl OutputTarget for MSL {
type Output = String;
}
impl OutputTarget for SPIRV { impl OutputTarget for SPIRV {
type Output = Vec<u32>; type Output = Vec<u32>;
} }
@ -42,9 +45,9 @@ impl OutputTarget for SPIRV {
mod test { mod test {
use crate::back::targets::GLSL; use crate::back::targets::GLSL;
use crate::back::FromCompilation; use crate::back::FromCompilation;
use crate::front::GlslangCompilation; use crate::front::SpirvCompilation;
#[allow(dead_code)] #[allow(dead_code)]
pub fn test_compile(value: GlslangCompilation) { pub fn test_compile(value: SpirvCompilation) {
let _x = GLSL::from_compilation(value).unwrap(); let _x = GLSL::from_compilation(value).unwrap();
} }
} }

View file

@ -4,7 +4,7 @@ use crate::back::targets::WGSL;
use crate::back::wgsl::lower_samplers::LowerCombinedImageSamplerPass; use crate::back::wgsl::lower_samplers::LowerCombinedImageSamplerPass;
use crate::back::{CompileShader, CompilerBackend, FromCompilation, ShaderCompilerOutput}; use crate::back::{CompileShader, CompilerBackend, FromCompilation, ShaderCompilerOutput};
use crate::error::{ShaderCompileError, ShaderReflectError}; use crate::error::{ShaderCompileError, ShaderReflectError};
use crate::front::GlslangCompilation; use crate::front::SpirvCompilation;
use crate::reflect::naga::NagaReflect; use crate::reflect::naga::NagaReflect;
use crate::reflect::ReflectShader; use crate::reflect::ReflectShader;
use naga::back::wgsl::WriterFlags; use naga::back::wgsl::WriterFlags;
@ -26,7 +26,7 @@ pub struct WgslCompileOptions {
pub sampler_bind_group: u32, pub sampler_bind_group: u32,
} }
impl FromCompilation<GlslangCompilation> for WGSL { impl FromCompilation<SpirvCompilation> for WGSL {
type Target = WGSL; type Target = WGSL;
type Options = WgslCompileOptions; type Options = WgslCompileOptions;
type Context = NagaWgslContext; type Context = NagaWgslContext;
@ -34,7 +34,7 @@ impl FromCompilation<GlslangCompilation> for WGSL {
+ ReflectShader; + ReflectShader;
fn from_compilation( fn from_compilation(
compile: GlslangCompilation, compile: SpirvCompilation,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> { ) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
fn lower_fragment_shader(words: &[u32]) -> Vec<u32> { fn lower_fragment_shader(words: &[u32]) -> Vec<u32> {
let mut loader = rspirv::dr::Loader::new(); let mut loader = rspirv::dr::Loader::new();
@ -189,7 +189,7 @@ mod test {
); );
} }
let compilation = crate::front::GlslangCompilation::try_from(&result).unwrap(); let compilation = crate::front::SpirvCompilation::try_from(&result).unwrap();
let mut wgsl = WGSL::from_compilation(compilation).unwrap(); let mut wgsl = WGSL::from_compilation(compilation).unwrap();

View file

@ -4,34 +4,20 @@ use librashader_preprocess::ShaderSource;
#[cfg(feature = "serialize")] #[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::front::{ShaderInputCompiler, SpirvCompilation};
/// A reflectable shader compilation via glslang. /// glslang compiler
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))] pub struct Glslang;
#[derive(Debug, Clone)]
pub struct GlslangCompilation {
pub(crate) vertex: Vec<u32>,
pub(crate) fragment: Vec<u32>,
}
impl GlslangCompilation { impl ShaderInputCompiler<SpirvCompilation> for Glslang {
/// Tries to compile SPIR-V from the provided shader source. fn compile(source: &ShaderSource) -> Result<SpirvCompilation, ShaderCompileError> {
pub fn compile(source: &ShaderSource) -> Result<Self, ShaderCompileError> {
compile_spirv(source) compile_spirv(source)
} }
} }
impl TryFrom<&ShaderSource> for GlslangCompilation {
type Error = ShaderCompileError;
/// Tries to compile SPIR-V from the provided shader source.
fn try_from(source: &ShaderSource) -> Result<Self, Self::Error> {
GlslangCompilation::compile(source)
}
}
pub(crate) fn compile_spirv( pub(crate) fn compile_spirv(
source: &ShaderSource, source: &ShaderSource,
) -> Result<GlslangCompilation, ShaderCompileError> { ) -> Result<SpirvCompilation, ShaderCompileError> {
let compiler = glslang::Compiler::acquire().ok_or(ShaderCompileError::CompilerInitError)?; let compiler = glslang::Compiler::acquire().ok_or(ShaderCompileError::CompilerInitError)?;
let options = CompilerOptions { let options = CompilerOptions {
source_language: glslang::SourceLanguage::GLSL, source_language: glslang::SourceLanguage::GLSL,
@ -53,7 +39,7 @@ pub(crate) fn compile_spirv(
let vertex = Vec::from(vertex.compile()?); let vertex = Vec::from(vertex.compile()?);
let fragment = Vec::from(fragment.compile()?); let fragment = Vec::from(fragment.compile()?);
Ok(GlslangCompilation { vertex, fragment }) Ok(SpirvCompilation { vertex, fragment })
} }
#[cfg(test)] #[cfg(test)]

View file

@ -1,18 +1,44 @@
use crate::error::ShaderCompileError; use crate::error::ShaderCompileError;
use librashader_preprocess::ShaderSource; use librashader_preprocess::ShaderSource;
use serde::{Deserialize, Serialize};
mod glslang; mod glslang;
pub use crate::front::glslang::GlslangCompilation;
pub trait ShaderReflectObject : Sized {
}
pub use glslang::Glslang;
/// Trait for types that can compile shader sources into a compilation unit. /// Trait for types that can compile shader sources into a compilation unit.
pub trait ShaderCompilation: Sized { pub trait ShaderInputCompiler<O: ShaderReflectObject>: Sized {
/// Compile the input shader source file into a compilation unit. /// Compile the input shader source file into a compilation unit.
fn compile(source: &ShaderSource) -> Result<Self, ShaderCompileError>; fn compile(source: &ShaderSource) -> Result<O, ShaderCompileError>;
} }
impl<T: for<'a> TryFrom<&'a ShaderSource, Error = ShaderCompileError>> ShaderCompilation for T { impl ShaderReflectObject for SpirvCompilation { }
fn compile(source: &ShaderSource) -> Result<Self, ShaderCompileError> { /// A reflectable shader compilation via glslang.
source.try_into() #[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct SpirvCompilation {
pub(crate) vertex: Vec<u32>,
pub(crate) fragment: Vec<u32>,
}
impl SpirvCompilation {
/// Tries to compile SPIR-V from the provided shader source.
pub fn compile(source: &ShaderSource) -> Result<Self, ShaderCompileError> {
glslang::compile_spirv(source)
} }
} }
impl TryFrom<&ShaderSource> for SpirvCompilation {
type Error = ShaderCompileError;
/// Tries to compile SPIR-V from the provided shader source.
fn try_from(source: &ShaderSource) -> Result<Self, Self::Error> {
Glslang::compile(source)
}
}

View file

@ -13,10 +13,10 @@
//! use librashader_presets::ShaderPreset; //! use librashader_presets::ShaderPreset;
//! use librashader_reflect::back::{CompileReflectShader, FromCompilation}; //! use librashader_reflect::back::{CompileReflectShader, FromCompilation};
//! use librashader_reflect::back::targets::SPIRV; //! use librashader_reflect::back::targets::SPIRV;
//! use librashader_reflect::front::GlslangCompilation; //! use librashader_reflect::front::{Glslang, ShaderInputCompiler, SpirvCompilation};
//! use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact}; //! use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact};
//! use librashader_reflect::reflect::semantics::ShaderSemantics; //! use librashader_reflect::reflect::semantics::ShaderSemantics;
//! type Artifact = impl CompileReflectShader<SPIRV, GlslangCompilation>; //! type Artifact = impl CompileReflectShader<SPIRV, SpirvCompilation>;
//! type ShaderPassMeta = ShaderPassArtifact<Artifact>; //! type ShaderPassMeta = ShaderPassArtifact<Artifact>;
//! //!
//! // Compile single shader //! // Compile single shader
@ -24,7 +24,7 @@
//! source: &ShaderSource, //! source: &ShaderSource,
//! ) -> Result<Artifact, Box<dyn Error>> //! ) -> Result<Artifact, Box<dyn Error>>
//! { //! {
//! let compilation = GlslangCompilation::compile(&source)?; //! let artifact = Glslang::compile(&source)?;
//! let spirv = SPIRV::from_compilation(artifact)?; //! let spirv = SPIRV::from_compilation(artifact)?;
//! Ok(spirv) //! Ok(spirv)
//! } //! }
@ -32,7 +32,7 @@
//! // Compile preset //! // Compile preset
//! pub fn compile_preset(preset: ShaderPreset) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), Box<dyn Error>> //! pub fn compile_preset(preset: ShaderPreset) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), Box<dyn Error>>
//! { //! {
//! let (passes, semantics) = SPIRV::compile_preset_passes::<GlslangCompilation, Box<dyn Error>>( //! let (passes, semantics) = SPIRV::compile_preset_passes::<Glslang, SpirvCompilation, Box<dyn Error>>(
//! preset.shaders, &preset.textures)?; //! preset.shaders, &preset.textures)?;
//! Ok((passes, semantics)) //! Ok((passes, semantics))
//! } //! }
@ -43,7 +43,7 @@
//! [naga](https://docs.rs/naga/latest/naga/index.html), a pure-Rust shader compiler, when it has //! [naga](https://docs.rs/naga/latest/naga/index.html), a pure-Rust shader compiler, when it has
//! matured enough to support [the features librashader needs](https://github.com/gfx-rs/naga/issues/1012). //! matured enough to support [the features librashader needs](https://github.com/gfx-rs/naga/issues/1012).
//! //!
//! In the meanwhile, the only supported compilation type is [GlslangCompilation](crate::front::GlslangCompilation), //! In the meanwhile, the only supported compilation type is [GlslangCompilation](crate::front::SpirvCompilation),
//! which does transpilation via [glslang](https://github.com/KhronosGroup/glslang) and [SPIRV-Cross](https://github.com/KhronosGroup/SPIRV-Cross). //! which does transpilation via [glslang](https://github.com/KhronosGroup/glslang) and [SPIRV-Cross](https://github.com/KhronosGroup/SPIRV-Cross).
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)] #![feature(impl_trait_in_assoc_type)]

View file

@ -1,5 +1,5 @@
use crate::error::{SemanticsErrorKind, ShaderCompileError, ShaderReflectError}; use crate::error::{SemanticsErrorKind, ShaderCompileError, ShaderReflectError};
use crate::front::GlslangCompilation; use crate::front::SpirvCompilation;
use crate::reflect::semantics::{ use crate::reflect::semantics::{
BindingMeta, BindingStage, BufferReflection, MemberOffset, ShaderReflection, ShaderSemantics, BindingMeta, BindingStage, BufferReflection, MemberOffset, ShaderReflection, ShaderSemantics,
TextureBinding, TextureSemanticMap, TextureSemantics, TextureSizeMeta, TypeInfo, TextureBinding, TextureSemanticMap, TextureSemantics, TextureSizeMeta, TypeInfo,
@ -134,7 +134,7 @@ impl ValidateTypeSemantics<Type> for TextureSemantics {
} }
} }
impl<T> TryFrom<&GlslangCompilation> for CrossReflect<T> impl<T> TryFrom<&SpirvCompilation> for CrossReflect<T>
where where
T: spirv_cross::spirv::Target, T: spirv_cross::spirv::Target,
Ast<T>: spirv_cross::spirv::Compile<T>, Ast<T>: spirv_cross::spirv::Compile<T>,
@ -142,7 +142,7 @@ where
{ {
type Error = ShaderReflectError; type Error = ShaderReflectError;
fn try_from(value: &GlslangCompilation) -> Result<Self, Self::Error> { fn try_from(value: &SpirvCompilation) -> Result<Self, Self::Error> {
let vertex_module = Module::from_words(&value.vertex); let vertex_module = Module::from_words(&value.vertex);
let fragment_module = Module::from_words(&value.fragment); let fragment_module = Module::from_words(&value.fragment);
@ -890,7 +890,7 @@ mod test {
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use crate::back::CompileShader; use crate::back::CompileShader;
use crate::front::GlslangCompilation; use crate::front::{Glslang, ShaderInputCompiler, SpirvCompilation};
use crate::reflect::semantics::{Semantic, ShaderSemantics, UniformSemantic, UniqueSemantics}; use crate::reflect::semantics::{Semantic, ShaderSemantics, UniformSemantic, UniqueSemantics};
use librashader_preprocess::ShaderSource; use librashader_preprocess::ShaderSource;
use spirv_cross::glsl; use spirv_cross::glsl;
@ -910,7 +910,7 @@ mod test {
}), }),
); );
} }
let spirv = GlslangCompilation::compile(&result).unwrap(); let spirv = Glslang::compile(&result).unwrap();
let mut reflect = CrossReflect::<glsl::Target>::try_from(&spirv).unwrap(); let mut reflect = CrossReflect::<glsl::Target>::try_from(&spirv).unwrap();
let _shader_reflection = reflect let _shader_reflection = reflect
.reflect( .reflect(

View file

@ -1,7 +1,7 @@
use crate::back::targets::OutputTarget; use crate::back::targets::OutputTarget;
use crate::back::{CompilerBackend, FromCompilation}; use crate::back::{CompilerBackend, FromCompilation};
use crate::error::{ShaderCompileError, ShaderReflectError}; use crate::error::{ShaderCompileError, ShaderReflectError};
use crate::front::ShaderCompilation; use crate::front::{ShaderInputCompiler, ShaderReflectObject, SpirvCompilation};
use crate::reflect::semantics::{ use crate::reflect::semantics::{
Semantic, ShaderSemantics, TextureSemantics, UniformSemantic, UniqueSemantics, Semantic, ShaderSemantics, TextureSemantics, UniformSemantic, UniqueSemantics,
}; };
@ -19,10 +19,10 @@ use rustc_hash::FxHashMap;
/// #![feature(type_alias_impl_trait)] /// #![feature(type_alias_impl_trait)]
/// use librashader_reflect::back::CompileReflectShader; /// use librashader_reflect::back::CompileReflectShader;
/// use librashader_reflect::back::targets::SPIRV; /// use librashader_reflect::back::targets::SPIRV;
/// use librashader_reflect::front::GlslangCompilation; /// use librashader_reflect::front::SpirvCompilation;
/// use librashader_reflect::reflect::presets::ShaderPassArtifact; /// use librashader_reflect::reflect::presets::ShaderPassArtifact;
/// ///
/// type VulkanPassMeta = ShaderPassArtifact<impl CompileReflectShader<SPIRV, GlslangCompilation>>; /// type VulkanPassMeta = ShaderPassArtifact<impl CompileReflectShader<SPIRV, SpirvCompilation>>;
/// ``` /// ```
/// ///
/// This allows a runtime to not name the backing type of the compiled artifact if not necessary. /// This allows a runtime to not name the backing type of the compiled artifact if not necessary.
@ -35,44 +35,46 @@ impl<T: OutputTarget> CompilePresetTarget for T {}
pub trait CompilePresetTarget: OutputTarget { pub trait CompilePresetTarget: OutputTarget {
/// Compile passes of a shader preset given the applicable /// Compile passes of a shader preset given the applicable
/// shader output target, compilation type, and resulting error. /// shader output target, compilation type, and resulting error.
fn compile_preset_passes<C, E>( fn compile_preset_passes<C, I, E>(
passes: Vec<ShaderPassConfig>, passes: Vec<ShaderPassConfig>,
textures: &[TextureConfig], textures: &[TextureConfig],
) -> Result< ) -> Result<
( (
Vec<ShaderPassArtifact<<Self as FromCompilation<C>>::Output>>, Vec<ShaderPassArtifact<<Self as FromCompilation<I>>::Output>>,
ShaderSemantics, ShaderSemantics,
), ),
E, E,
> >
where where
I: ShaderReflectObject,
Self: Sized, Self: Sized,
Self: FromCompilation<C>, Self: FromCompilation<I>,
C: ShaderCompilation, C: ShaderInputCompiler<I>,
E: From<PreprocessError>, E: From<PreprocessError>,
E: From<ShaderReflectError>, E: From<ShaderReflectError>,
E: From<ShaderCompileError>, E: From<ShaderCompileError>,
{ {
compile_preset_passes::<Self, C, E>(passes, textures) compile_preset_passes::<Self, C, I, E>(passes, textures)
} }
} }
/// Compile passes of a shader preset given the applicable /// Compile passes of a shader preset given the applicable
/// shader output target, compilation type, and resulting error. /// shader output target, compilation type, and resulting error.
fn compile_preset_passes<T, C, E>( fn compile_preset_passes<T, C, I, E>(
passes: Vec<ShaderPassConfig>, passes: Vec<ShaderPassConfig>,
textures: &[TextureConfig], textures: &[TextureConfig],
) -> Result< ) -> Result<
( (
Vec<ShaderPassArtifact<<T as FromCompilation<C>>::Output>>, Vec<ShaderPassArtifact<<T as FromCompilation<I>>::Output>>,
ShaderSemantics, ShaderSemantics,
), ),
E, E,
> >
where where
I: ShaderReflectObject,
T: OutputTarget, T: OutputTarget,
T: FromCompilation<C>, T: FromCompilation<I>,
C: ShaderCompilation, C: ShaderInputCompiler<I>,
E: From<PreprocessError>, E: From<PreprocessError>,
E: From<ShaderReflectError>, E: From<ShaderReflectError>,
E: From<ShaderCompileError>, E: From<ShaderCompileError>,

View file

@ -4,7 +4,7 @@ use librashader_common::{ImageFormat, Size, Viewport};
use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig}; use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig};
use librashader_reflect::back::targets::HLSL; use librashader_reflect::back::targets::HLSL;
use librashader_reflect::back::{CompileReflectShader, CompileShader}; use librashader_reflect::back::{CompileReflectShader, CompileShader};
use librashader_reflect::front::GlslangCompilation; use librashader_reflect::front::{Glslang, SpirvCompilation};
use librashader_reflect::reflect::semantics::ShaderSemantics; use librashader_reflect::reflect::semantics::ShaderSemantics;
use librashader_reflect::reflect::ReflectShader; use librashader_reflect::reflect::ReflectShader;
use librashader_runtime::image::{Image, ImageError, UVDirection}; use librashader_runtime::image::{Image, ImageError, UVDirection};
@ -74,18 +74,18 @@ pub(crate) struct FilterCommon {
} }
type ShaderPassMeta = type ShaderPassMeta =
ShaderPassArtifact<impl CompileReflectShader<HLSL, GlslangCompilation> + Send>; ShaderPassArtifact<impl CompileReflectShader<HLSL, SpirvCompilation> + Send>;
fn compile_passes( fn compile_passes(
shaders: Vec<ShaderPassConfig>, shaders: Vec<ShaderPassConfig>,
textures: &[TextureConfig], textures: &[TextureConfig],
disable_cache: bool, disable_cache: bool,
) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), FilterChainError> { ) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), FilterChainError> {
let (passes, semantics) = if !disable_cache { let (passes, semantics) = if !disable_cache {
HLSL::compile_preset_passes::<CachedCompilation<GlslangCompilation>, FilterChainError>( HLSL::compile_preset_passes::<Glslang, CachedCompilation<SpirvCompilation>, FilterChainError>(
shaders, &textures, shaders, &textures,
)? )?
} else { } else {
HLSL::compile_preset_passes::<GlslangCompilation, FilterChainError>(shaders, &textures)? HLSL::compile_preset_passes::<Glslang, SpirvCompilation, FilterChainError>(shaders, &textures)?
}; };
Ok((passes, semantics)) Ok((passes, semantics))

View file

@ -18,7 +18,7 @@ use librashader_common::{ImageFormat, Size, Viewport};
use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig}; use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig};
use librashader_reflect::back::targets::{DXIL, HLSL}; use librashader_reflect::back::targets::{DXIL, HLSL};
use librashader_reflect::back::{CompileReflectShader, CompileShader}; use librashader_reflect::back::{CompileReflectShader, CompileShader};
use librashader_reflect::front::GlslangCompilation; use librashader_reflect::front::{Glslang, SpirvCompilation};
use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact}; use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact};
use librashader_reflect::reflect::semantics::{ShaderSemantics, MAX_BINDINGS_COUNT}; use librashader_reflect::reflect::semantics::{ShaderSemantics, MAX_BINDINGS_COUNT};
use librashader_reflect::reflect::ReflectShader; use librashader_reflect::reflect::ReflectShader;
@ -145,35 +145,35 @@ impl Drop for FrameResiduals {
} }
type DxilShaderPassMeta = type DxilShaderPassMeta =
ShaderPassArtifact<impl CompileReflectShader<DXIL, GlslangCompilation> + Send>; ShaderPassArtifact<impl CompileReflectShader<DXIL, SpirvCompilation> + Send>;
fn compile_passes_dxil( fn compile_passes_dxil(
shaders: Vec<ShaderPassConfig>, shaders: Vec<ShaderPassConfig>,
textures: &[TextureConfig], textures: &[TextureConfig],
disable_cache: bool, disable_cache: bool,
) -> Result<(Vec<DxilShaderPassMeta>, ShaderSemantics), FilterChainError> { ) -> Result<(Vec<DxilShaderPassMeta>, ShaderSemantics), FilterChainError> {
let (passes, semantics) = if !disable_cache { let (passes, semantics) = if !disable_cache {
DXIL::compile_preset_passes::<CachedCompilation<GlslangCompilation>, FilterChainError>( DXIL::compile_preset_passes::<Glslang, CachedCompilation<SpirvCompilation>, FilterChainError>(
shaders, &textures, shaders, &textures,
)? )?
} else { } else {
DXIL::compile_preset_passes::<GlslangCompilation, FilterChainError>(shaders, &textures)? DXIL::compile_preset_passes::<Glslang, SpirvCompilation, FilterChainError>(shaders, &textures)?
}; };
Ok((passes, semantics)) Ok((passes, semantics))
} }
type HlslShaderPassMeta = type HlslShaderPassMeta =
ShaderPassArtifact<impl CompileReflectShader<HLSL, GlslangCompilation> + Send>; ShaderPassArtifact<impl CompileReflectShader<HLSL, SpirvCompilation> + Send>;
fn compile_passes_hlsl( fn compile_passes_hlsl(
shaders: Vec<ShaderPassConfig>, shaders: Vec<ShaderPassConfig>,
textures: &[TextureConfig], textures: &[TextureConfig],
disable_cache: bool, disable_cache: bool,
) -> Result<(Vec<HlslShaderPassMeta>, ShaderSemantics), FilterChainError> { ) -> Result<(Vec<HlslShaderPassMeta>, ShaderSemantics), FilterChainError> {
let (passes, semantics) = if !disable_cache { let (passes, semantics) = if !disable_cache {
HLSL::compile_preset_passes::<CachedCompilation<GlslangCompilation>, FilterChainError>( HLSL::compile_preset_passes::<Glslang, CachedCompilation<SpirvCompilation>, FilterChainError>(
shaders, &textures, shaders, &textures,
)? )?
} else { } else {
HLSL::compile_preset_passes::<GlslangCompilation, FilterChainError>(shaders, &textures)? HLSL::compile_preset_passes::<Glslang, SpirvCompilation, FilterChainError>(shaders, &textures)?
}; };
Ok((passes, semantics)) Ok((passes, semantics))

View file

@ -16,7 +16,7 @@ use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig};
use librashader_reflect::back::cross::GlslVersion; use librashader_reflect::back::cross::GlslVersion;
use librashader_reflect::back::targets::GLSL; use librashader_reflect::back::targets::GLSL;
use librashader_reflect::back::{CompileReflectShader, CompileShader}; use librashader_reflect::back::{CompileReflectShader, CompileShader};
use librashader_reflect::front::GlslangCompilation; use librashader_reflect::front::{Glslang, SpirvCompilation};
use librashader_reflect::reflect::semantics::{ShaderSemantics, UniformMeta}; use librashader_reflect::reflect::semantics::{ShaderSemantics, UniformMeta};
use librashader_cache::CachedCompilation; use librashader_cache::CachedCompilation;
@ -97,18 +97,18 @@ impl<T: GLInterface> FilterChainImpl<T> {
} }
} }
type ShaderPassMeta = ShaderPassArtifact<impl CompileReflectShader<GLSL, GlslangCompilation>>; type ShaderPassMeta = ShaderPassArtifact<impl CompileReflectShader<GLSL, SpirvCompilation>>;
fn compile_passes( fn compile_passes(
shaders: Vec<ShaderPassConfig>, shaders: Vec<ShaderPassConfig>,
textures: &[TextureConfig], textures: &[TextureConfig],
disable_cache: bool, disable_cache: bool,
) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), FilterChainError> { ) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), FilterChainError> {
let (passes, semantics) = if !disable_cache { let (passes, semantics) = if !disable_cache {
GLSL::compile_preset_passes::<CachedCompilation<GlslangCompilation>, FilterChainError>( GLSL::compile_preset_passes::<Glslang, CachedCompilation<SpirvCompilation>, FilterChainError>(
shaders, &textures, shaders, &textures,
)? )?
} else { } else {
GLSL::compile_preset_passes::<GlslangCompilation, FilterChainError>(shaders, &textures)? GLSL::compile_preset_passes::<Glslang, SpirvCompilation, FilterChainError>(shaders, &textures)?
}; };
Ok((passes, semantics)) Ok((passes, semantics))

View file

@ -17,7 +17,7 @@ use gpu_allocator::vulkan::Allocator;
use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig}; use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig};
use librashader_reflect::back::targets::SPIRV; use librashader_reflect::back::targets::SPIRV;
use librashader_reflect::back::{CompileReflectShader, CompileShader}; use librashader_reflect::back::{CompileReflectShader, CompileShader};
use librashader_reflect::front::GlslangCompilation; use librashader_reflect::front::{Glslang, SpirvCompilation};
use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact}; use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact};
use librashader_reflect::reflect::semantics::ShaderSemantics; use librashader_reflect::reflect::semantics::ShaderSemantics;
use librashader_reflect::reflect::ReflectShader; use librashader_reflect::reflect::ReflectShader;
@ -208,18 +208,18 @@ impl Drop for FrameResiduals {
} }
type ShaderPassMeta = type ShaderPassMeta =
ShaderPassArtifact<impl CompileReflectShader<SPIRV, GlslangCompilation> + Send>; ShaderPassArtifact<impl CompileReflectShader<SPIRV, SpirvCompilation> + Send>;
fn compile_passes( fn compile_passes(
shaders: Vec<ShaderPassConfig>, shaders: Vec<ShaderPassConfig>,
textures: &[TextureConfig], textures: &[TextureConfig],
disable_cache: bool, disable_cache: bool,
) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), FilterChainError> { ) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), FilterChainError> {
let (passes, semantics) = if !disable_cache { let (passes, semantics) = if !disable_cache {
SPIRV::compile_preset_passes::<CachedCompilation<GlslangCompilation>, FilterChainError>( SPIRV::compile_preset_passes::<Glslang, CachedCompilation<SpirvCompilation>, FilterChainError>(
shaders, &textures, shaders, &textures,
)? )?
} else { } else {
SPIRV::compile_preset_passes::<GlslangCompilation, FilterChainError>(shaders, &textures)? SPIRV::compile_preset_passes::<Glslang, SpirvCompilation, FilterChainError>(shaders, &textures)?
}; };
Ok((passes, semantics)) Ok((passes, semantics))

View file

@ -1,7 +1,7 @@
use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig}; use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig};
use librashader_reflect::back::targets::WGSL; use librashader_reflect::back::targets::WGSL;
use librashader_reflect::back::{CompileReflectShader, CompileShader}; use librashader_reflect::back::{CompileReflectShader, CompileShader};
use librashader_reflect::front::GlslangCompilation; use librashader_reflect::front::{Glslang, SpirvCompilation};
use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact}; use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact};
use librashader_reflect::reflect::semantics::ShaderSemantics; use librashader_reflect::reflect::semantics::ShaderSemantics;
use librashader_reflect::reflect::ReflectShader; use librashader_reflect::reflect::ReflectShader;
@ -38,13 +38,13 @@ use crate::samplers::SamplerSet;
use crate::texture::{InputImage, OwnedImage}; use crate::texture::{InputImage, OwnedImage};
type ShaderPassMeta = type ShaderPassMeta =
ShaderPassArtifact<impl CompileReflectShader<WGSL, GlslangCompilation> + Send>; ShaderPassArtifact<impl CompileReflectShader<WGSL, SpirvCompilation> + Send>;
fn compile_passes( fn compile_passes(
shaders: Vec<ShaderPassConfig>, shaders: Vec<ShaderPassConfig>,
textures: &[TextureConfig], textures: &[TextureConfig],
) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), FilterChainError> { ) -> Result<(Vec<ShaderPassMeta>, ShaderSemantics), FilterChainError> {
let (passes, semantics) = let (passes, semantics) =
WGSL::compile_preset_passes::<GlslangCompilation, FilterChainError>(shaders, &textures)?; WGSL::compile_preset_passes::<Glslang, SpirvCompilation, FilterChainError>(shaders, &textures)?;
Ok((passes, semantics)) Ok((passes, semantics))
} }

View file

@ -150,7 +150,7 @@ pub mod reflect {
FromCompilation, ShaderCompilerOutput, FromCompilation, ShaderCompilerOutput,
}; };
pub use librashader_reflect::front::GlslangCompilation; pub use librashader_reflect::front::SpirvCompilation;
/// Reflection via SPIRV-Cross. /// Reflection via SPIRV-Cross.
#[cfg(feature = "reflect-cross")] #[cfg(feature = "reflect-cross")]
@ -196,7 +196,7 @@ pub mod reflect {
pub use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact}; pub use librashader_reflect::reflect::presets::{CompilePresetTarget, ShaderPassArtifact};
pub use librashader_reflect::front::ShaderCompilation; pub use librashader_reflect::front::ShaderInputCompiler;
#[doc(hidden)] #[doc(hidden)]
#[cfg(feature = "internal")] #[cfg(feature = "internal")]
/// Helper methods for runtimes. /// Helper methods for runtimes.