librashader/librashader-reflect/src/back/mod.rs

137 lines
4 KiB
Rust
Raw Normal View History

2023-11-29 18:06:20 +11:00
#[cfg(all(target_os = "windows", feature = "dxil"))]
2023-02-06 08:17:23 +11:00
pub mod dxil;
pub mod glsl;
pub mod hlsl;
pub mod msl;
pub mod spirv;
2022-12-22 13:39:31 +11:00
pub mod targets;
2023-12-13 11:02:49 +11:00
pub mod wgsl;
2022-11-07 16:25:11 +11:00
2022-11-22 08:53:36 +11:00
use crate::back::targets::OutputTarget;
use crate::error::{ShaderCompileError, ShaderReflectError};
use crate::reflect::semantics::ShaderSemantics;
2022-11-30 17:38:05 +11:00
use crate::reflect::{ReflectShader, ShaderReflection};
use std::fmt::Debug;
2022-11-07 16:25:11 +11:00
/// The output of the shader compiler.
2022-11-07 16:25:11 +11:00
#[derive(Debug)]
2022-11-11 17:53:02 +11:00
pub struct ShaderCompilerOutput<T, Context = ()> {
/// The output for the vertex shader.
2022-11-11 17:53:02 +11:00
pub vertex: T,
/// The output for the fragment shader.
2022-11-11 17:53:02 +11:00
pub fragment: T,
/// Additional context provided by the shader compiler.
2022-11-09 17:11:25 +11:00
pub context: Context,
2022-11-07 16:25:11 +11:00
}
2022-11-22 08:53:36 +11:00
/// A trait for objects that can be compiled into a shader.
2022-11-22 08:53:36 +11:00
pub trait CompileShader<T: OutputTarget> {
/// Options provided to the compiler.
2022-11-22 08:53:36 +11:00
type Options;
/// Additional context returned by the compiler after compilation.
2022-11-22 08:53:36 +11:00
type Context;
/// Consume the object and return the compiled output of the shader.
2022-11-22 08:53:36 +11:00
fn compile(
self,
options: Self::Options,
) -> Result<ShaderCompilerOutput<T::Output, Self::Context>, ShaderCompileError>;
}
/// Marker trait for combinations of targets and compilations that can be reflected and compiled
/// successfully.
///
/// This trait is automatically implemented for reflected outputs that have [`FromCompilation`](crate::back::FromCompilation) implement
/// for a given target that also implement [`CompileShader`](crate::back::CompileShader) for that target.
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,
>,
{
}
2022-11-22 08:53:36 +11:00
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)
}
}
/// A trait for reflectable compilations that can be transformed
/// into an object ready for reflection or compilation.
///
/// `T` is the compiled reflectable form of the shader.
/// `S` is the semantics under which the shader is reflected.
///
/// librashader currently supports two semantics, [`SpirvCross`](crate::reflect::cross::SpirvCross)
pub trait FromCompilation<T, S> {
/// The target that the transformed object is expected to compile for.
2022-11-22 08:53:36 +11:00
type Target: OutputTarget;
/// Options provided to the compiler.
2022-11-22 08:53:36 +11:00
type Options;
/// Additional context returned by the compiler after compilation.
2022-11-22 08:53:36 +11:00
type Context;
/// The output type after conversion.
2022-11-30 17:38:05 +11:00
type Output: CompileShader<Self::Target, Context = Self::Context, Options = Self::Options>
+ ReflectShader;
/// Tries to convert the input object into an object ready for compilation.
2022-11-30 17:38:05 +11:00
fn from_compilation(compile: T) -> Result<CompilerBackend<Self::Output>, ShaderReflectError>;
2022-11-22 08:53:36 +11:00
}
/// A wrapper for a compiler backend.
2022-11-22 08:53:36 +11:00
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,
2022-11-22 08:53:36 +11:00
) -> Result<ShaderReflection, ShaderReflectError> {
self.backend.reflect(pass_number, semantics)
}
}
2023-12-13 11:02:49 +11:00
#[cfg(test)]
mod test {
2024-02-12 07:39:11 +11:00
use crate::front::{Glslang, ShaderInputCompiler};
2023-12-13 11:02:49 +11:00
use librashader_preprocess::ShaderSource;
pub fn test() {
let result = ShaderSource::load("../test/basic.slang").unwrap();
let _cross = Glslang::compile(&result).unwrap();
2023-12-13 11:02:49 +11:00
}
}