api: clean up spirv-cross related apis

This commit is contained in:
chyyran 2023-01-16 19:35:23 -05:00
parent acca9ce6f6
commit 479015d223
7 changed files with 34 additions and 13 deletions

1
Cargo.lock generated
View file

@ -630,6 +630,7 @@ dependencies = [
"librashader-runtime-d3d11", "librashader-runtime-d3d11",
"librashader-runtime-gl", "librashader-runtime-gl",
"librashader-runtime-vk", "librashader-runtime-vk",
"spirv_cross",
"windows", "windows",
] ]

View file

@ -23,7 +23,7 @@ pub enum PreprocessError {
/// The given pragma was declared multiple times with differing values. /// The given pragma was declared multiple times with differing values.
#[error("duplicate pragma found")] #[error("duplicate pragma found")]
DuplicatePragmaError(String), DuplicatePragmaError(String),
/// The imaged format requested by the shader was unknown or not supported. /// The image format requested by the shader was unknown or not supported.
#[error("shader format is unknown or not found")] #[error("shader format is unknown or not found")]
UnknownImageFormat, UnknownImageFormat,
/// The stage declared by the shader source was not `vertex` or `fragment`. /// The stage declared by the shader source was not `vertex` or `fragment`.

View file

@ -20,7 +20,7 @@ impl FromCompilation<GlslangCompilation> for GLSL {
type Target = GLSL; type Target = GLSL;
type Options = GlslVersion; type Options = GlslVersion;
type Context = CrossGlslContext; type Context = CrossGlslContext;
type Output = impl CompileShader<Self::Target, Options = GlslVersion, Context = CrossGlslContext> type Output = impl CompileShader<Self::Target, Options = GlslVersion, Context = Self::Context>
+ ReflectShader; + ReflectShader;
fn from_compilation( fn from_compilation(
@ -34,7 +34,8 @@ impl FromCompilation<GlslangCompilation> for GLSL {
/// The context for a HLSL compilation via spirv-cross. /// The context for a HLSL compilation via spirv-cross.
pub struct CrossHlslContext { pub struct CrossHlslContext {
pub compiler: CompiledProgram<spirv_cross::hlsl::Target>, /// The compiled HLSL program.
pub artifact: CompiledProgram<spirv_cross::hlsl::Target>,
} }
impl FromCompilation<GlslangCompilation> for HLSL { impl FromCompilation<GlslangCompilation> for HLSL {

View file

@ -17,8 +17,8 @@ impl FromCompilation<GlslangCompilation> for SPIRV {
type Target = SPIRV; type Target = SPIRV;
type Options = Option<()>; type Options = Option<()>;
type Context = (); type Context = ();
type Output = type Output = impl CompileShader<Self::Target, Options = Self::Options, Context = Self::Context>
impl CompileShader<Self::Target, Options = Option<()>, Context = ()> + ReflectShader; + ReflectShader;
fn from_compilation( fn from_compilation(
compile: GlslangCompilation, compile: GlslangCompilation,

View file

@ -18,7 +18,7 @@ use crate::back::targets::{GLSL, HLSL};
use crate::back::{CompileShader, ShaderCompilerOutput}; use crate::back::{CompileShader, ShaderCompilerOutput};
use crate::reflect::helper::{SemanticErrorBlame, TextureData, UboData}; use crate::reflect::helper::{SemanticErrorBlame, TextureData, UboData};
pub struct CrossReflect<T> pub(crate) struct CrossReflect<T>
where where
T: spirv_cross::spirv::Target, T: spirv_cross::spirv::Target,
{ {
@ -26,9 +26,9 @@ where
fragment: Ast<T>, fragment: Ast<T>,
} }
///! The output of the SPIR-V AST after compilation. ///The output of the SPIR-V AST after compilation.
///! ///
///! This output is immutable and can not be recompiled later. /// This output is immutable and can not be recompiled later.
pub struct CompiledAst<T: spirv_cross::spirv::Target>(Ast<T>); pub struct CompiledAst<T: spirv_cross::spirv::Target>(Ast<T>);
impl<T: spirv_cross::spirv::Target> Deref for CompiledAst<T> { impl<T: spirv_cross::spirv::Target> Deref for CompiledAst<T> {
type Target = Ast<T>; type Target = Ast<T>;
@ -38,7 +38,7 @@ impl<T: spirv_cross::spirv::Target> Deref for CompiledAst<T> {
} }
} }
///! The compiled SPIR-V program after compilation. /// The compiled SPIR-V program after compilation.
pub struct CompiledProgram<T> pub struct CompiledProgram<T>
where where
T: spirv_cross::spirv::Target, T: spirv_cross::spirv::Target,
@ -820,7 +820,7 @@ impl CompileShader<HLSL> for CrossReflect<hlsl::Target> {
vertex: self.vertex.compile()?, vertex: self.vertex.compile()?,
fragment: self.fragment.compile()?, fragment: self.fragment.compile()?,
context: CrossHlslContext { context: CrossHlslContext {
compiler: CompiledProgram { artifact: CompiledProgram {
vertex: CompiledAst(self.vertex), vertex: CompiledAst(self.vertex),
fragment: CompiledAst(self.fragment), fragment: CompiledAst(self.fragment),
}, },

View file

@ -23,6 +23,7 @@ librashader-runtime-gl = { path = "../librashader-runtime-gl", version = "0.1.0-
librashader-runtime-vk = { path = "../librashader-runtime-vk", version = "0.1.0-beta.7", optional = true } librashader-runtime-vk = { path = "../librashader-runtime-vk", version = "0.1.0-beta.7", optional = true }
ash = { version = "0.37.1+1.3.235", optional = true } ash = { version = "0.37.1+1.3.235", optional = true }
spirv_cross = { version = "0.23.1", optional = true, features = ["glsl"] }
[dependencies.windows] [dependencies.windows]
version = "0.44.0" version = "0.44.0"
@ -33,7 +34,7 @@ optional = true
[features] [features]
default = ["gl", "d3d11", "vk", "reflect", "preprocess", "presets" ] default = ["gl", "d3d11", "vk", "reflect", "preprocess", "presets" ]
gl = [ "runtime", "librashader-common/opengl", "librashader-runtime-gl" ] gl = [ "runtime", "librashader-common/opengl", "librashader-runtime-gl", "spirv_cross" ]
d3d11 = [ "runtime", "librashader-common/d3d11", "librashader-runtime-d3d11", "windows" ] d3d11 = [ "runtime", "librashader-common/d3d11", "librashader-runtime-d3d11", "windows" ]
vk = ["runtime", "librashader-common/vulkan", "librashader-runtime-vk", "ash" ] vk = ["runtime", "librashader-common/vulkan", "librashader-runtime-vk", "ash" ]
runtime = [] runtime = []

View file

@ -82,7 +82,25 @@ pub mod reflect {
targets::OutputTarget, CompileShader, CompilerBackend, FromCompilation, targets::OutputTarget, CompileShader, CompilerBackend, FromCompilation,
ShaderCompilerOutput, ShaderCompilerOutput,
}; };
pub use librashader_reflect::front::shaderc::GlslangCompilation;
/// Reflection via SPIRV-Cross.
pub mod cross {
pub use librashader_reflect::front::shaderc::GlslangCompilation;
#[cfg(feature = "gl")]
/// The version of GLSL to compile to.
pub use spirv_cross::glsl::Version as GlslVersion;
#[cfg(feature = "gl")]
pub use librashader_reflect::back::cross::CrossGlslContext;
#[cfg(any(feature = "d3d11", feature = "d3d12"))]
pub use librashader_reflect::back::cross::CrossHlslContext;
pub use librashader_reflect::reflect::cross::CompiledAst;
pub use librashader_reflect::reflect::cross::CompiledProgram;
}
pub use librashader_reflect::reflect::semantics::BindingMeta; pub use librashader_reflect::reflect::semantics::BindingMeta;
/// Helpers to deal with image loading. /// Helpers to deal with image loading.