2023-01-19 12:54:57 +11:00
|
|
|
use crate::error::ShaderCompileError;
|
|
|
|
use librashader_preprocess::ShaderSource;
|
2024-02-11 09:47:05 +11:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-01-19 12:54:57 +11:00
|
|
|
|
2024-02-08 14:26:17 +11:00
|
|
|
mod glslang;
|
2023-01-19 12:54:57 +11:00
|
|
|
|
2024-02-14 09:37:48 +11:00
|
|
|
pub trait ShaderReflectObject: Sized {
|
|
|
|
/// The compiler that produces this reflect object.
|
|
|
|
type Compiler;
|
|
|
|
}
|
2024-02-11 09:47:05 +11:00
|
|
|
|
2024-02-11 13:17:58 +11:00
|
|
|
pub use crate::front::glslang::Glslang;
|
2023-01-19 17:06:17 +11:00
|
|
|
|
|
|
|
/// Trait for types that can compile shader sources into a compilation unit.
|
2024-02-11 09:47:05 +11:00
|
|
|
pub trait ShaderInputCompiler<O: ShaderReflectObject>: Sized {
|
2023-01-19 12:54:57 +11:00
|
|
|
/// Compile the input shader source file into a compilation unit.
|
2024-02-11 09:47:05 +11:00
|
|
|
fn compile(source: &ShaderSource) -> Result<O, ShaderCompileError>;
|
|
|
|
}
|
|
|
|
|
2024-02-12 02:21:31 +11:00
|
|
|
/// Marker trait for types that are the reflectable outputs of a shader compilation.
|
2024-02-14 09:37:48 +11:00
|
|
|
impl ShaderReflectObject for SpirvCompilation {
|
|
|
|
type Compiler = Glslang;
|
|
|
|
}
|
2024-02-12 02:21:31 +11:00
|
|
|
|
2024-02-11 09:47:05 +11:00
|
|
|
/// A reflectable shader compilation via glslang.
|
|
|
|
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct SpirvCompilation {
|
|
|
|
pub(crate) vertex: Vec<u32>,
|
|
|
|
pub(crate) fragment: Vec<u32>,
|
2023-01-19 12:54:57 +11:00
|
|
|
}
|
2023-01-19 17:06:17 +11:00
|
|
|
|
2024-02-11 09:47:05 +11:00
|
|
|
impl SpirvCompilation {
|
|
|
|
/// Tries to compile SPIR-V from the provided shader source.
|
|
|
|
pub fn compile(source: &ShaderSource) -> Result<Self, ShaderCompileError> {
|
|
|
|
glslang::compile_spirv(source)
|
2023-01-19 17:06:17 +11:00
|
|
|
}
|
|
|
|
}
|
2024-02-11 09:47:05 +11:00
|
|
|
|
|
|
|
impl TryFrom<&ShaderSource> for SpirvCompilation {
|
|
|
|
type Error = ShaderCompileError;
|
|
|
|
|
|
|
|
/// Tries to compile SPIR-V from the provided shader source.
|
|
|
|
fn try_from(source: &ShaderSource) -> Result<Self, Self::Error> {
|
|
|
|
Glslang::compile(source)
|
|
|
|
}
|
|
|
|
}
|