reflect: implement stable FromCompilation

This commit is contained in:
chyyran 2024-09-15 01:36:14 -04:00 committed by Ronny Chan
parent 4cc3c875bf
commit e0a5c90103
9 changed files with 155 additions and 6 deletions

View file

@ -46,5 +46,6 @@ cross = [ "dep:spirv-cross2", "spirv-cross2/glsl", "spirv-cross2/hlsl", "spirv-c
naga = [ "dep:rspirv", "dep:spirv", "naga/spv-in", "naga/spv-out", "naga/wgsl-out", "naga/msl-out" ] naga = [ "dep:rspirv", "dep:spirv", "naga/spv-in", "naga/spv-out", "naga/wgsl-out", "naga/msl-out" ]
serialize = [ "dep:serde" ] serialize = [ "dep:serde" ]
msl = [ "spirv-cross2/msl", "naga/msl-out" ] msl = [ "spirv-cross2/msl", "naga/msl-out" ]
stable = []
unstable-naga-in = ["naga/glsl-in"] unstable-naga-in = ["naga/glsl-in"]

View file

@ -17,6 +17,7 @@ impl OutputTarget for DXIL {
type Output = DxilObject; type Output = DxilObject;
} }
#[cfg(not(feature = "stable"))]
impl FromCompilation<SpirvCompilation, SpirvCross> for DXIL { impl FromCompilation<SpirvCompilation, SpirvCross> for DXIL {
type Target = DXIL; type Target = DXIL;
type Options = Option<ShaderModel>; type Options = Option<ShaderModel>;
@ -38,6 +39,28 @@ impl FromCompilation<SpirvCompilation, SpirvCross> for DXIL {
} }
} }
#[cfg(feature = "stable")]
impl FromCompilation<SpirvCompilation, SpirvCross> for DXIL {
type Target = DXIL;
type Options = Option<ShaderModel>;
type Context = ();
type Output = Box<dyn CompileReflectShader<Self::Target, SpirvCompilation, SpirvCross> + Send>;
fn from_compilation(
compile: SpirvCompilation,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
let reflect = GlslReflect::try_from(&compile)?;
Ok(CompilerBackend {
// we can just reuse WriteSpirV as the backend.
backend: Box::new(WriteSpirV {
reflect,
vertex: compile.vertex,
fragment: compile.fragment,
}),
})
}
}
impl CompileShader<DXIL> for WriteSpirV { impl CompileShader<DXIL> for WriteSpirV {
type Options = Option<ShaderModel>; type Options = Option<ShaderModel>;
type Context = (); type Context = ();

View file

@ -17,6 +17,7 @@ pub struct CrossGlslContext {
pub artifact: CompiledProgram<spirv_cross2::targets::Glsl>, pub artifact: CompiledProgram<spirv_cross2::targets::Glsl>,
} }
#[cfg(not(feature = "stable"))]
impl FromCompilation<SpirvCompilation, SpirvCross> for GLSL { impl FromCompilation<SpirvCompilation, SpirvCross> for GLSL {
type Target = GLSL; type Target = GLSL;
type Options = GlslVersion; type Options = GlslVersion;
@ -31,3 +32,19 @@ impl FromCompilation<SpirvCompilation, SpirvCross> for GLSL {
}) })
} }
} }
#[cfg(feature = "stable")]
impl FromCompilation<SpirvCompilation, SpirvCross> for GLSL {
type Target = GLSL;
type Options = GlslVersion;
type Context = CrossGlslContext;
type Output = Box<dyn CompileReflectShader<Self::Target, SpirvCompilation, SpirvCross> + Send>;
fn from_compilation(
compile: SpirvCompilation,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
Ok(CompilerBackend {
backend: Box::new(GlslReflect::try_from(&compile)?),
})
}
}

View file

@ -46,7 +46,7 @@ impl HlslBufferAssignments {
{ {
return true; return true;
} }
return false; false
} }
// Check if the mangled name matches. // Check if the mangled name matches.
@ -91,7 +91,7 @@ impl HlslBufferAssignments {
return true; return true;
} }
return false; false
} }
} }
@ -103,6 +103,7 @@ pub struct CrossHlslContext {
pub fragment_buffers: HlslBufferAssignments, pub fragment_buffers: HlslBufferAssignments,
} }
#[cfg(not(feature = "stable"))]
impl FromCompilation<SpirvCompilation, SpirvCross> for HLSL { impl FromCompilation<SpirvCompilation, SpirvCross> for HLSL {
type Target = HLSL; type Target = HLSL;
type Options = Option<HlslShaderModel>; type Options = Option<HlslShaderModel>;
@ -118,6 +119,22 @@ impl FromCompilation<SpirvCompilation, SpirvCross> for HLSL {
} }
} }
#[cfg(feature = "stable")]
impl FromCompilation<SpirvCompilation, SpirvCross> for HLSL {
type Target = HLSL;
type Options = Option<HlslShaderModel>;
type Context = CrossHlslContext;
type Output = Box<dyn CompileReflectShader<Self::Target, SpirvCompilation, SpirvCross> + Send>;
fn from_compilation(
compile: SpirvCompilation,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
Ok(CompilerBackend {
backend: Box::new(HlslReflect::try_from(&compile)?),
})
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::back::hlsl::HlslBufferAssignments; use crate::back::hlsl::HlslBufferAssignments;

View file

@ -5,7 +5,6 @@ use crate::front::SpirvCompilation;
use crate::reflect::cross::msl::MslReflect; use crate::reflect::cross::msl::MslReflect;
use crate::reflect::cross::{CompiledProgram, SpirvCross}; use crate::reflect::cross::{CompiledProgram, SpirvCross};
use crate::reflect::naga::{Naga, NagaReflect}; use crate::reflect::naga::{Naga, NagaReflect};
use crate::reflect::ReflectShader;
use naga::back::msl::TranslationInfo; use naga::back::msl::TranslationInfo;
use naga::Module; use naga::Module;
@ -25,6 +24,7 @@ pub struct CrossMslContext {
pub artifact: CompiledProgram<spirv_cross2::targets::Msl>, pub artifact: CompiledProgram<spirv_cross2::targets::Msl>,
} }
#[cfg(not(feature = "stable"))]
impl FromCompilation<SpirvCompilation, SpirvCross> for MSL { impl FromCompilation<SpirvCompilation, SpirvCross> for MSL {
type Target = MSL; type Target = MSL;
type Options = Option<self::MslVersion>; type Options = Option<self::MslVersion>;
@ -40,6 +40,22 @@ impl FromCompilation<SpirvCompilation, SpirvCross> for MSL {
} }
} }
#[cfg(feature = "stable")]
impl FromCompilation<SpirvCompilation, SpirvCross> for MSL {
type Target = MSL;
type Options = Option<self::MslVersion>;
type Context = CrossMslContext;
type Output = Box<dyn CompileReflectShader<Self::Target, SpirvCompilation, SpirvCross> + Send>;
fn from_compilation(
compile: SpirvCompilation,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
Ok(CompilerBackend {
backend: Box::new(MslReflect::try_from(&compile)?),
})
}
}
/// The naga module for a shader after compilation /// The naga module for a shader after compilation
pub struct NagaMslModule { pub struct NagaMslModule {
pub translation_info: TranslationInfo, pub translation_info: TranslationInfo,
@ -52,6 +68,7 @@ pub struct NagaMslContext {
pub next_free_binding: u32, pub next_free_binding: u32,
} }
#[cfg(not(feature = "stable"))]
impl FromCompilation<SpirvCompilation, Naga> for MSL { impl FromCompilation<SpirvCompilation, Naga> for MSL {
type Target = MSL; type Target = MSL;
type Options = Option<self::MslVersion>; type Options = Option<self::MslVersion>;
@ -66,3 +83,19 @@ impl FromCompilation<SpirvCompilation, Naga> for MSL {
}) })
} }
} }
#[cfg(feature = "stable")]
impl FromCompilation<SpirvCompilation, Naga> for MSL {
type Target = MSL;
type Options = Option<self::MslVersion>;
type Context = NagaMslContext;
type Output = Box<dyn CompileReflectShader<Self::Target, SpirvCompilation, Naga> + Send>;
fn from_compilation(
compile: SpirvCompilation,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
Ok(CompilerBackend {
backend: Box::new(NagaReflect::try_from(&compile)?),
})
}
}

View file

@ -18,6 +18,7 @@ pub(crate) struct WriteSpirV {
pub(crate) fragment: Vec<u32>, pub(crate) fragment: Vec<u32>,
} }
#[cfg(not(feature = "stable"))]
impl FromCompilation<SpirvCompilation, SpirvCross> for SPIRV { impl FromCompilation<SpirvCompilation, SpirvCross> for SPIRV {
type Target = SPIRV; type Target = SPIRV;
type Options = Option<()>; type Options = Option<()>;
@ -40,6 +41,29 @@ impl FromCompilation<SpirvCompilation, SpirvCross> for SPIRV {
} }
} }
#[cfg(feature = "stable")]
impl FromCompilation<SpirvCompilation, SpirvCross> for SPIRV {
type Target = SPIRV;
type Options = Option<()>;
type Context = ();
type Output = Box<dyn CompileReflectShader<Self::Target, SpirvCompilation, SpirvCross> + Send>;
fn from_compilation(
compile: SpirvCompilation,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
let reflect = GlslReflect::try_from(&compile)?;
let vertex = compile.vertex;
let fragment = compile.fragment;
Ok(CompilerBackend {
backend: Box::new(WriteSpirV {
reflect,
vertex,
fragment,
}),
})
}
}
impl ReflectShader for WriteSpirV { impl ReflectShader for WriteSpirV {
fn reflect( fn reflect(
&mut self, &mut self,
@ -83,6 +107,7 @@ pub struct NagaSpirvContext {
pub vertex: Module, pub vertex: Module,
} }
#[cfg(not(feature = "stable"))]
impl FromCompilation<SpirvCompilation, Naga> for SPIRV { impl FromCompilation<SpirvCompilation, Naga> for SPIRV {
type Target = SPIRV; type Target = SPIRV;
type Options = NagaSpirvOptions; type Options = NagaSpirvOptions;
@ -98,6 +123,22 @@ impl FromCompilation<SpirvCompilation, Naga> for SPIRV {
} }
} }
#[cfg(feature = "stable")]
impl FromCompilation<SpirvCompilation, Naga> for SPIRV {
type Target = SPIRV;
type Options = NagaSpirvOptions;
type Context = NagaSpirvContext;
type Output = Box<dyn CompileReflectShader<Self::Target, SpirvCompilation, Naga> + Send>;
fn from_compilation(
compile: SpirvCompilation,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
Ok(CompilerBackend {
backend: Box::new(NagaReflect::try_from(&compile)?),
})
}
}
pub struct NagaSpirvOptions { pub struct NagaSpirvOptions {
pub lowering: NagaLoweringOptions, pub lowering: NagaLoweringOptions,
pub version: (u8, u8), pub version: (u8, u8),

View file

@ -11,6 +11,7 @@ pub struct NagaWgslContext {
pub vertex: Module, pub vertex: Module,
} }
#[cfg(not(feature = "stable"))]
impl FromCompilation<SpirvCompilation, Naga> for WGSL { impl FromCompilation<SpirvCompilation, Naga> for WGSL {
type Target = WGSL; type Target = WGSL;
type Options = NagaLoweringOptions; type Options = NagaLoweringOptions;
@ -26,6 +27,22 @@ impl FromCompilation<SpirvCompilation, Naga> for WGSL {
} }
} }
#[cfg(feature = "stable")]
impl FromCompilation<SpirvCompilation, Naga> for WGSL {
type Target = WGSL;
type Options = NagaLoweringOptions;
type Context = NagaWgslContext;
type Output = Box<dyn CompileReflectShader<Self::Target, SpirvCompilation, Naga> + Send>;
fn from_compilation(
compile: SpirvCompilation,
) -> Result<CompilerBackend<Self::Output>, ShaderReflectError> {
Ok(CompilerBackend {
backend: Box::new(NagaReflect::try_from(&compile)?),
})
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::back::targets::WGSL; use crate::back::targets::WGSL;

View file

@ -43,9 +43,9 @@
//! librashader-reflect is designed to be compiler-agnostic. [naga](https://docs.rs/naga/latest/naga/index.html), //! librashader-reflect is designed to be compiler-agnostic. [naga](https://docs.rs/naga/latest/naga/index.html),
//! a pure-Rust shader compiler, as well as SPIRV-Cross via [SpirvCompilation](crate::front::SpirvCompilation) //! a pure-Rust shader compiler, as well as SPIRV-Cross via [SpirvCompilation](crate::front::SpirvCompilation)
//! is supported. //! is supported.
#![feature(impl_trait_in_assoc_type)] #![cfg_attr(not(feature="stable"), feature(impl_trait_in_assoc_type))]
#![allow(stable_features)] #![allow(stable_features)]
#![feature(c_str_literals)] #![cfg_attr(not(feature="stable"), feature(c_str_literals))]
/// Shader codegen backends. /// Shader codegen backends.
pub mod back; pub mod back;
/// Error types. /// Error types.

View file

@ -5,7 +5,7 @@
//! This crate should not be used directly. //! This crate should not be used directly.
//! See [`librashader::runtime::d3d11`](https://docs.rs/librashader/latest/librashader/runtime/d3d11/index.html) instead. //! See [`librashader::runtime::d3d11`](https://docs.rs/librashader/latest/librashader/runtime/d3d11/index.html) instead.
#![feature(type_alias_impl_trait)] #![cfg_attr(not(feature = "stable"), feature(type_alias_impl_trait))]
mod draw_quad; mod draw_quad;
mod filter_chain; mod filter_chain;