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

116 lines
3.4 KiB
Rust
Raw Normal View History

pub mod cross;
2023-02-06 08:17:23 +11:00
pub mod dxil;
mod spirv;
2022-12-22 13:39:31 +11:00
pub mod targets;
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>:
CompileShader<
T,
Options = <T as FromCompilation<C>>::Options,
Context = <T as FromCompilation<C>>::Context,
> + ReflectShader
where
T: FromCompilation<C>,
{
}
impl<T, C, O> CompileReflectShader<T, C> for O
where
T: OutputTarget,
T: FromCompilation<C>,
O: ReflectShader,
O: CompileShader<
T,
Options = <T as FromCompilation<C>>::Options,
Context = <T as FromCompilation<C>>::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.
2022-11-22 08:53:36 +11:00
pub trait FromCompilation<T> {
/// 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)
}
}