librashader/librashader-preprocess/src/lib.rs

98 lines
2.3 KiB
Rust
Raw Normal View History

mod error;
mod include;
2022-10-22 12:04:00 +11:00
mod pragma;
mod stage;
2022-10-23 15:59:18 +11:00
use crate::include::read_source;
pub use error::*;
use librashader_common::ShaderFormat;
2022-10-23 15:59:18 +11:00
use std::path::Path;
#[derive(Debug, Clone, PartialEq)]
pub struct ShaderSource {
pub vertex: String,
pub fragment: String,
pub name: Option<String>,
pub parameters: Vec<ShaderParameter>,
pub format: ShaderFormat,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ShaderParameter {
pub id: String,
pub description: String,
pub initial: f32,
pub minimum: f32,
pub maximum: f32,
pub step: f32,
}
impl ShaderSource {
pub fn load(path: impl AsRef<Path>) -> Result<ShaderSource, PreprocessError> {
load_shader_source(path)
}
}
pub(crate) trait SourceOutput {
fn push_line(&mut self, str: &str);
fn mark_line(&mut self, line_no: usize, comment: &str) {
2022-10-23 15:59:18 +11:00
#[cfg(feature = "line_directives")]
self.push_line(&format!("#line {} \"{}\"", line_no, comment))
}
}
impl SourceOutput for String {
fn push_line(&mut self, str: &str) {
self.push_str(str);
self.push('\n');
}
}
pub(crate) fn load_shader_source(path: impl AsRef<Path>) -> Result<ShaderSource, PreprocessError> {
let source = read_source(path)?;
let meta = pragma::parse_pragma_meta(&source)?;
let text = stage::process_stages(&source)?;
Ok(ShaderSource {
vertex: text.vertex,
fragment: text.fragment,
name: meta.name,
parameters: meta.parameters,
format: meta.format,
})
}
#[cfg(test)]
mod test {
use crate::include::read_source;
use crate::{load_shader_source, pragma};
#[test]
pub fn load_file() {
2022-10-23 15:59:18 +11:00
let result = load_shader_source(
"../test/slang-shaders/blurs/shaders/royale/blur3x3-last-pass.slang",
)
.unwrap();
eprintln!("{:#}", result.vertex)
}
2022-10-22 14:37:47 +11:00
#[test]
pub fn preprocess_file() {
2022-10-22 12:04:00 +11:00
let result =
read_source("../test/slang-shaders/blurs/shaders/royale/blur3x3-last-pass.slang")
.unwrap();
eprintln!("{result}")
}
2022-10-22 14:37:47 +11:00
#[test]
pub fn get_param_pragmas() {
2022-10-23 15:59:18 +11:00
let result = read_source(
"../test/slang-shaders/crt/shaders/crt-maximus-royale/src/ntsc_pass1.slang",
)
.unwrap();
2022-10-22 14:37:47 +11:00
2022-10-23 15:59:18 +11:00
let params = pragma::parse_pragma_meta(result).unwrap();
2022-10-22 14:37:47 +11:00
eprintln!("{params:?}")
}
2022-10-22 12:04:00 +11:00
}