diff --git a/librashader-reflect/src/back/cross.rs b/librashader-reflect/src/back/cross.rs index ee87b3e..cb5804c 100644 --- a/librashader-reflect/src/back/cross.rs +++ b/librashader-reflect/src/back/cross.rs @@ -10,6 +10,7 @@ pub struct GlslangGlslContext { pub sampler_bindings: Vec, pub compiler: CompiledAst, } + impl FromCompilation for GLSL { type Target = GLSL; type Options = GlVersion; diff --git a/librashader-reflect/src/front/naga.rs b/librashader-reflect/src/front/naga.rs index cc095b3..aa5904e 100644 --- a/librashader-reflect/src/front/naga.rs +++ b/librashader-reflect/src/front/naga.rs @@ -26,9 +26,12 @@ mod test { use naga::valid::{Capabilities, ValidationFlags}; use naga::{FastHashSet, ShaderStage}; use rspirv::binary::Disassemble; + use librashader_preprocess::ShaderSource; + use crate::front::shaderc::GlslangCompilation; + #[test] pub fn compile_naga_test() { - let result = librashader_preprocess::load_shader_source( + let result = ShaderSource::load( "../test/slang-shaders/blurs/shaders/royale/blur3x3-last-pass.slang", ) .unwrap(); @@ -43,7 +46,7 @@ mod test { #[test] pub fn compile_shader() { - let result = librashader_preprocess::load_shader_source( + let result = ShaderSource::load( "../test/slang-shaders/blurs/shaders/royale/blur3x3-last-pass.slang", ) .unwrap(); @@ -53,8 +56,8 @@ mod test { #[test] pub fn compile_shader_roundtrip() { - let result = librashader_preprocess::load_shader_source("../test/basic.slang").unwrap(); - let cross = crate::front::shaderc::compile_spirv(&result).unwrap(); + let result = ShaderSource::load("../test/basic.slang").unwrap(); + let cross = GlslangCompilation::compile(&result).unwrap(); let naga_fragment = naga::front::spv::parse_u8_slice(cross.fragment.as_binary_u8(), &SpvOptions::default()) .unwrap(); @@ -65,8 +68,8 @@ mod test { #[test] pub fn naga_playground() { - let result = librashader_preprocess::load_shader_source("../test/basic.slang").unwrap(); - let spirv = crate::front::shaderc::compile_spirv(&result).unwrap(); + let result = ShaderSource::load("../test/basic.slang").unwrap(); + let spirv = GlslangCompilation::compile(&result).unwrap(); let module = naga::front::spv::parse_u8_slice(spirv.fragment.as_binary_u8(), &SpvOptions::default()) diff --git a/librashader-reflect/src/front/shaderc.rs b/librashader-reflect/src/front/shaderc.rs index 721fd72..4cd449d 100644 --- a/librashader-reflect/src/front/shaderc.rs +++ b/librashader-reflect/src/front/shaderc.rs @@ -7,6 +7,23 @@ pub struct GlslangCompilation { pub(crate) fragment: CompilationArtifact, } +impl GlslangCompilation { + + /// Tries to compile SPIR-V from the provided shader source. + pub fn compile(source: &ShaderSource) -> Result { + 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 { + GlslangCompilation::compile(source) + } +} + fn get_shaderc_options() -> Result, ShaderCompileError> { let mut options = CompileOptions::new().ok_or(ShaderCompileError::ShaderCInitError)?; options.set_include_callback(|_, _, _, _| { @@ -99,7 +116,7 @@ fn get_shaderc_options() -> Result, ShaderCompileError> Ok(options) } -pub fn compile_spirv(source: &ShaderSource) -> Result { +fn compile_spirv(source: &ShaderSource) -> Result { let compiler = shaderc::Compiler::new().ok_or(ShaderCompileError::ShaderCInitError)?; let name = source.name.as_deref().unwrap_or("shader.slang"); let options = get_shaderc_options()?; diff --git a/librashader-runtime-dx11/src/lib.rs b/librashader-runtime-dx11/src/lib.rs index f148d88..2b8de57 100644 --- a/librashader-runtime-dx11/src/lib.rs +++ b/librashader-runtime-dx11/src/lib.rs @@ -5,6 +5,7 @@ use librashader_reflect::back::CompileShader; use rustc_hash::FxHashMap; use std::error::Error; use std::path::Path; +use librashader_reflect::front::shaderc::GlslangCompilation; use librashader_reflect::reflect::semantics::{SemanticMap, TextureSemantics, VariableSemantics}; use librashader_reflect::reflect::{ReflectSemantics, ReflectShader, UniformSemantic}; @@ -65,7 +66,7 @@ pub fn load(path: impl AsRef) -> Result<(), Box> { .iter() .map(|shader| { let source = ShaderSource::load(&shader.name).unwrap(); - let spirv = librashader_reflect::front::shaderc::compile_spirv(&source).unwrap(); + let spirv = GlslangCompilation::compile(&source).unwrap(); let reflect = HLSL::from_compilation(spirv).unwrap(); (shader, source, reflect) }) diff --git a/librashader-runtime-gl/src/filter_chain.rs b/librashader-runtime-gl/src/filter_chain.rs index fbf6424..9cf253b 100644 --- a/librashader-runtime-gl/src/filter_chain.rs +++ b/librashader-runtime-gl/src/filter_chain.rs @@ -22,6 +22,7 @@ use spirv_cross::spirv::Decoration; use std::collections::VecDeque; use std::error::Error; use std::path::Path; +use librashader_reflect::front::shaderc::GlslangCompilation; pub struct FilterChain { passes: Box<[FilterPass]>, @@ -207,7 +208,7 @@ impl FilterChain { eprintln!("[gl] loading {}", &shader.name.display()); let source: ShaderSource = ShaderSource::load(&shader.name)?; - let spirv = librashader_reflect::front::shaderc::compile_spirv(&source)?; + let spirv = GlslangCompilation::compile(&source)?; let reflect = GLSL::from_compilation(spirv)?; for parameter in source.parameters.iter() {