reflect: make compilation an associated function

This commit is contained in:
chyyran 2022-11-21 16:28:28 -05:00
parent 1579516d25
commit 38ce621664
5 changed files with 32 additions and 9 deletions

View file

@ -10,6 +10,7 @@ pub struct GlslangGlslContext {
pub sampler_bindings: Vec<u32>,
pub compiler: CompiledAst<spirv_cross::glsl::Target>,
}
impl FromCompilation<GlslangCompilation> for GLSL {
type Target = GLSL;
type Options = GlVersion;

View file

@ -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())

View file

@ -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<Self, ShaderCompileError> {
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)
}
}
fn get_shaderc_options() -> Result<CompileOptions<'static>, ShaderCompileError> {
let mut options = CompileOptions::new().ok_or(ShaderCompileError::ShaderCInitError)?;
options.set_include_callback(|_, _, _, _| {
@ -99,7 +116,7 @@ fn get_shaderc_options() -> Result<CompileOptions<'static>, ShaderCompileError>
Ok(options)
}
pub fn compile_spirv(source: &ShaderSource) -> Result<GlslangCompilation, ShaderCompileError> {
fn compile_spirv(source: &ShaderSource) -> Result<GlslangCompilation, ShaderCompileError> {
let compiler = shaderc::Compiler::new().ok_or(ShaderCompileError::ShaderCInitError)?;
let name = source.name.as_deref().unwrap_or("shader.slang");
let options = get_shaderc_options()?;

View file

@ -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<Path>) -> Result<(), Box<dyn Error>> {
.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)
})

View file

@ -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() {