1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
mod error;
mod include;
mod pragma;
mod stage;
use crate::include::read_source;
pub use error::*;
use librashader_common::ImageFormat;
use rustc_hash::FxHashMap;
use std::path::Path;
#[derive(Debug, Clone, PartialEq)]
pub struct ShaderSource {
pub vertex: String,
pub fragment: String,
pub name: Option<String>,
pub parameters: FxHashMap<String, ShaderParameter>,
pub format: ImageFormat,
}
#[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) {
#[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)?;
let parameters = FxHashMap::from_iter(meta.parameters.into_iter().map(|p| (p.id.clone(), p)));
Ok(ShaderSource {
vertex: text.vertex,
fragment: text.fragment,
name: meta.name,
parameters,
format: meta.format,
})
}
#[cfg(test)]
mod test {
use crate::include::read_source;
use crate::{load_shader_source, pragma};
#[test]
pub fn load_file() {
let result = load_shader_source(
"../test/slang-shaders/blurs/shaders/royale/blur3x3-last-pass.slang",
)
.unwrap();
eprintln!("{:#}", result.vertex)
}
#[test]
pub fn preprocess_file() {
let result =
read_source("../test/slang-shaders/blurs/shaders/royale/blur3x3-last-pass.slang")
.unwrap();
eprintln!("{result}")
}
#[test]
pub fn get_param_pragmas() {
let result = read_source(
"../test/slang-shaders/crt/shaders/crt-maximus-royale/src/ntsc_pass1.slang",
)
.unwrap();
let params = pragma::parse_pragma_meta(result).unwrap();
eprintln!("{params:?}")
}
}