#[cfg(all(target_os = "windows", feature = "dxil"))]
pub mod dxil;
pub mod glsl;
pub mod hlsl;
pub mod msl;
pub mod spirv;
pub mod targets;
pub mod wgsl;
use crate::back::targets::OutputTarget;
use crate::error::{ShaderCompileError, ShaderReflectError};
use crate::reflect::semantics::ShaderSemantics;
use crate::reflect::{ReflectShader, ShaderReflection};
use std::fmt::Debug;
#[derive(Debug)]
pub struct ShaderCompilerOutput<T, Context = ()> {
pub vertex: T,
pub fragment: T,
pub context: Context,
}
pub trait CompileShader<T: OutputTarget> {
type Options;
type Context;
fn compile(
self,
options: Self::Options,
) -> Result<ShaderCompilerOutput<T::Output, Self::Context>, ShaderCompileError>;
}
pub trait CompileReflectShader<T: OutputTarget, C, S>:
CompileShader<
T,
Options = <T as FromCompilation<C, S>>::Options,
Context = <T as FromCompilation<C, S>>::Context,
> + ReflectShader
where
T: FromCompilation<C, S>,
{
}
impl<T, C, O, S> CompileReflectShader<T, C, S> for O
where
T: OutputTarget,
T: FromCompilation<C, S>,
O: ReflectShader,
O: CompileShader<
T,
Options = <T as FromCompilation<C, S>>::Options,
Context = <T as FromCompilation<C, S>>::Context,
>,
{
}
impl<T, E> CompileShader<E> for CompilerBackend<T>
where
T: CompileShader<E>,
E: OutputTarget,
{
type Options = T::Options;
type Context = T::Context;
fn compile(
self,
options: Self::Options,
) -> Result<ShaderCompilerOutput<E::Output, Self::Context>, ShaderCompileError> {
self.backend.compile(options)
}
}
pub trait FromCompilation<T, S> {
type Target: OutputTarget;
type Options;
type Context;
type Output: CompileShader<Self::Target, Context = Self::Context, Options = Self::Options>
+ ReflectShader;
fn from_compilation(compile: T) -> Result<CompilerBackend<Self::Output>, ShaderReflectError>;
}
pub struct CompilerBackend<T> {
pub(crate) backend: T,
}
impl<T> ReflectShader for CompilerBackend<T>
where
T: ReflectShader,
{
fn reflect(
&mut self,
pass_number: usize,
semantics: &ShaderSemantics,
) -> Result<ShaderReflection, ShaderReflectError> {
self.backend.reflect(pass_number, semantics)
}
}
#[cfg(test)]
mod test {
use crate::front::{Glslang, ShaderInputCompiler};
use librashader_preprocess::ShaderSource;
pub fn test() {
let result = ShaderSource::load("../test/basic.slang").unwrap();
let _cross = Glslang::compile(&result).unwrap();
}
}