From 479015d22341816f6045a79b9394f5e09040ae67 Mon Sep 17 00:00:00 2001 From: chyyran Date: Mon, 16 Jan 2023 19:35:23 -0500 Subject: [PATCH] api: clean up spirv-cross related apis --- Cargo.lock | 1 + librashader-preprocess/src/error.rs | 2 +- librashader-reflect/src/back/cross.rs | 5 +++-- librashader-reflect/src/back/spirv.rs | 4 ++-- librashader-reflect/src/reflect/cross.rs | 12 ++++++------ librashader/Cargo.toml | 3 ++- librashader/src/lib.rs | 20 +++++++++++++++++++- 7 files changed, 34 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b17fd87..4af7d8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -630,6 +630,7 @@ dependencies = [ "librashader-runtime-d3d11", "librashader-runtime-gl", "librashader-runtime-vk", + "spirv_cross", "windows", ] diff --git a/librashader-preprocess/src/error.rs b/librashader-preprocess/src/error.rs index bdaebe8..3d38cdf 100644 --- a/librashader-preprocess/src/error.rs +++ b/librashader-preprocess/src/error.rs @@ -23,7 +23,7 @@ pub enum PreprocessError { /// The given pragma was declared multiple times with differing values. #[error("duplicate pragma found")] 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")] UnknownImageFormat, /// The stage declared by the shader source was not `vertex` or `fragment`. diff --git a/librashader-reflect/src/back/cross.rs b/librashader-reflect/src/back/cross.rs index cdb2fdf..6ef9a87 100644 --- a/librashader-reflect/src/back/cross.rs +++ b/librashader-reflect/src/back/cross.rs @@ -20,7 +20,7 @@ impl FromCompilation for GLSL { type Target = GLSL; type Options = GlslVersion; type Context = CrossGlslContext; - type Output = impl CompileShader + type Output = impl CompileShader + ReflectShader; fn from_compilation( @@ -34,7 +34,8 @@ impl FromCompilation for GLSL { /// The context for a HLSL compilation via spirv-cross. pub struct CrossHlslContext { - pub compiler: CompiledProgram, + /// The compiled HLSL program. + pub artifact: CompiledProgram, } impl FromCompilation for HLSL { diff --git a/librashader-reflect/src/back/spirv.rs b/librashader-reflect/src/back/spirv.rs index 295895b..c94fee3 100644 --- a/librashader-reflect/src/back/spirv.rs +++ b/librashader-reflect/src/back/spirv.rs @@ -17,8 +17,8 @@ impl FromCompilation for SPIRV { type Target = SPIRV; type Options = Option<()>; type Context = (); - type Output = - impl CompileShader, Context = ()> + ReflectShader; + type Output = impl CompileShader + + ReflectShader; fn from_compilation( compile: GlslangCompilation, diff --git a/librashader-reflect/src/reflect/cross.rs b/librashader-reflect/src/reflect/cross.rs index 2aa695f..eb4256a 100644 --- a/librashader-reflect/src/reflect/cross.rs +++ b/librashader-reflect/src/reflect/cross.rs @@ -18,7 +18,7 @@ use crate::back::targets::{GLSL, HLSL}; use crate::back::{CompileShader, ShaderCompilerOutput}; use crate::reflect::helper::{SemanticErrorBlame, TextureData, UboData}; -pub struct CrossReflect +pub(crate) struct CrossReflect where T: spirv_cross::spirv::Target, { @@ -26,9 +26,9 @@ where fragment: Ast, } -///! The output of the SPIR-V AST after compilation. -///! -///! This output is immutable and can not be recompiled later. +///The output of the SPIR-V AST after compilation. +/// +/// This output is immutable and can not be recompiled later. pub struct CompiledAst(Ast); impl Deref for CompiledAst { type Target = Ast; @@ -38,7 +38,7 @@ impl Deref for CompiledAst { } } -///! The compiled SPIR-V program after compilation. +/// The compiled SPIR-V program after compilation. pub struct CompiledProgram where T: spirv_cross::spirv::Target, @@ -820,7 +820,7 @@ impl CompileShader for CrossReflect { vertex: self.vertex.compile()?, fragment: self.fragment.compile()?, context: CrossHlslContext { - compiler: CompiledProgram { + artifact: CompiledProgram { vertex: CompiledAst(self.vertex), fragment: CompiledAst(self.fragment), }, diff --git a/librashader/Cargo.toml b/librashader/Cargo.toml index 9211469..06a2ef8 100644 --- a/librashader/Cargo.toml +++ b/librashader/Cargo.toml @@ -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 } ash = { version = "0.37.1+1.3.235", optional = true } +spirv_cross = { version = "0.23.1", optional = true, features = ["glsl"] } [dependencies.windows] version = "0.44.0" @@ -33,7 +34,7 @@ optional = true [features] 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" ] vk = ["runtime", "librashader-common/vulkan", "librashader-runtime-vk", "ash" ] runtime = [] diff --git a/librashader/src/lib.rs b/librashader/src/lib.rs index 717099a..ed7910c 100644 --- a/librashader/src/lib.rs +++ b/librashader/src/lib.rs @@ -82,7 +82,25 @@ pub mod reflect { targets::OutputTarget, CompileShader, CompilerBackend, FromCompilation, 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; /// Helpers to deal with image loading.