2022-10-22 17:54:06 +11:00
|
|
|
use crate::{PreprocessError, SourceOutput};
|
2022-10-23 15:59:18 +11:00
|
|
|
use std::str::FromStr;
|
2022-10-22 17:54:06 +11:00
|
|
|
|
|
|
|
enum ActiveStage {
|
|
|
|
Both,
|
|
|
|
Fragment,
|
2022-10-23 15:59:18 +11:00
|
|
|
Vertex,
|
2022-10-22 17:54:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
2022-10-22 17:54:06 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub(crate) struct ShaderOutput {
|
|
|
|
pub(crate) fragment: String,
|
2022-10-23 15:59:18 +11:00
|
|
|
pub(crate) vertex: String,
|
2022-10-22 17:54:06 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2022-10-22 17:54:06 +11:00
|
|
|
active_stage = ActiveStage::from_str(stage)?;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if line.starts_with("#pragma name ") || line.starts_with("#pragma format ") {
|
|
|
|
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),
|
2022-10-22 17:54:06 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(output)
|
2022-10-23 15:59:18 +11:00
|
|
|
}
|