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;
|
2024-02-11 18:23:01 +11:00
|
|
|
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};
|
2022-12-01 17:50:16 +11:00
|
|
|
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
|
|
|
|
2022-12-01 17:50:16 +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 = ()> {
|
2022-12-01 17:50:16 +11:00
|
|
|
/// The output for the vertex shader.
|
2022-11-11 17:53:02 +11:00
|
|
|
pub vertex: T,
|
2022-12-01 17:50:16 +11:00
|
|
|
/// The output for the fragment shader.
|
2022-11-11 17:53:02 +11:00
|
|
|
pub fragment: T,
|
2022-12-01 17:50:16 +11:00
|
|
|
/// 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
|
|
|
|
2022-12-01 17:50:16 +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> {
|
2022-12-01 17:50:16 +11:00
|
|
|
/// Options provided to the compiler.
|
2022-11-22 08:53:36 +11:00
|
|
|
type Options;
|
2022-12-01 17:50:16 +11:00
|
|
|
/// Additional context returned by the compiler after compilation.
|
2022-11-22 08:53:36 +11:00
|
|
|
type Context;
|
|
|
|
|
2022-12-01 17:50:16 +11:00
|
|
|
/// 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>;
|
|
|
|
}
|
|
|
|
|
2023-01-20 10:44:08 +11:00
|
|
|
/// 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.
|
2024-02-11 13:17:58 +11:00
|
|
|
pub trait CompileReflectShader<T: OutputTarget, C, S>:
|
2023-01-20 10:44:08 +11:00
|
|
|
CompileShader<
|
|
|
|
T,
|
2024-02-11 13:17:58 +11:00
|
|
|
Options = <T as FromCompilation<C, S>>::Options,
|
|
|
|
Context = <T as FromCompilation<C, S>>::Context,
|
2023-01-20 10:44:08 +11:00
|
|
|
> + ReflectShader
|
|
|
|
where
|
2024-02-11 13:17:58 +11:00
|
|
|
T: FromCompilation<C, S>,
|
2023-01-20 10:44:08 +11:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-02-11 13:17:58 +11:00
|
|
|
impl<T, C, O, S> CompileReflectShader<T, C, S> for O
|
2023-01-20 10:44:08 +11:00
|
|
|
where
|
|
|
|
T: OutputTarget,
|
2024-02-11 13:17:58 +11:00
|
|
|
T: FromCompilation<C, S>,
|
2023-01-20 10:44:08 +11:00
|
|
|
O: ReflectShader,
|
|
|
|
O: CompileShader<
|
|
|
|
T,
|
2024-02-11 13:17:58 +11:00
|
|
|
Options = <T as FromCompilation<C, S>>::Options,
|
|
|
|
Context = <T as FromCompilation<C, S>>::Context,
|
2023-01-20 10:44:08 +11:00
|
|
|
>,
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-11 13:17:58 +11:00
|
|
|
/// 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> {
|
2022-12-01 17:50:16 +11:00
|
|
|
/// The target that the transformed object is expected to compile for.
|
2022-11-22 08:53:36 +11:00
|
|
|
type Target: OutputTarget;
|
2022-12-01 17:50:16 +11:00
|
|
|
/// Options provided to the compiler.
|
2022-11-22 08:53:36 +11:00
|
|
|
type Options;
|
2022-12-01 17:50:16 +11:00
|
|
|
/// Additional context returned by the compiler after compilation.
|
2022-11-22 08:53:36 +11:00
|
|
|
type Context;
|
|
|
|
|
2022-12-01 17:50:16 +11:00
|
|
|
/// 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;
|
2022-11-22 10:19:43 +11:00
|
|
|
|
2022-12-01 17:50:16 +11:00
|
|
|
/// 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
|
|
|
}
|
|
|
|
|
2022-12-01 17:50:16 +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,
|
2022-12-01 17:50:16 +11:00
|
|
|
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();
|
2024-02-11 09:47:05 +11:00
|
|
|
let _cross = Glslang::compile(&result).unwrap();
|
2023-12-13 11:02:49 +11:00
|
|
|
}
|
|
|
|
}
|