librashader/librashader-preprocess/src/stage.rs

60 lines
1.5 KiB
Rust
Raw Normal View History

use crate::{PreprocessError, SourceOutput};
2022-10-23 15:59:18 +11:00
use std::str::FromStr;
enum ActiveStage {
Both,
Fragment,
2022-10-23 15:59:18 +11:00
Vertex,
}
impl FromStr for ActiveStage {
type Err = PreprocessError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"vertex" => Ok(ActiveStage::Vertex),
"fragment" => Ok(ActiveStage::Fragment),
2022-10-23 15:59:18 +11:00
_ => Err(PreprocessError::InvalidStage),
}
}
}
#[derive(Default)]
pub(crate) struct ShaderOutput {
pub(crate) fragment: String,
2022-10-23 15:59:18 +11:00
pub(crate) vertex: String,
}
pub(crate) fn process_stages(source: &str) -> Result<ShaderOutput, PreprocessError> {
let mut active_stage = ActiveStage::Both;
let mut output = ShaderOutput::default();
for line in source.lines() {
2022-12-02 11:16:13 +11:00
if let Some(stage) = line.strip_prefix("#pragma stage ") {
let stage = stage.trim();
active_stage = ActiveStage::from_str(stage)?;
continue;
}
2024-02-15 17:05:01 +11:00
if line.starts_with("#pragma name ")
|| line.starts_with("#pragma format ")
|| line.starts_with("#pragma parameter ")
{
continue;
}
match active_stage {
ActiveStage::Both => {
output.fragment.push_line(line);
output.vertex.push_line(line);
}
ActiveStage::Fragment => {
output.fragment.push_line(line);
}
2022-10-23 15:59:18 +11:00
ActiveStage::Vertex => output.vertex.push_line(line),
}
}
Ok(output)
2022-10-23 15:59:18 +11:00
}