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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use notify::{self, Watcher};
use std::cell::RefCell;
use std::collections::HashSet;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::mpsc;
use thiserror::Error;
pub struct Watch {
event_rx: mpsc::Receiver<notify::Result<notify::Event>>,
pending_paths: RefCell<Vec<PathBuf>>,
_watchers: Vec<notify::RecommendedWatcher>,
_watched_paths: Vec<PathBuf>,
}
#[derive(Debug, Error)]
#[error("failed to setup a notify watcher: {err}")]
pub struct CreationError {
#[from]
err: notify::Error,
}
#[derive(Debug, Error)]
pub enum NextPathError {
#[error("the channel used to receive file system events was closed")]
ChannelClosed,
#[error("a notify event signalled an error: {err}")]
Notify {
#[from]
err: notify::Error,
},
}
#[derive(Debug, Error)]
pub enum AwaitEventError {
#[error("the channel used to receive file system events was closed")]
ChannelClosed,
#[error("a notify event signalled an error: {err}")]
Notify {
#[from]
err: notify::Error,
},
}
#[derive(Debug, Error)]
pub enum CompileError {
#[error("an I/O error occurred: {err}")]
Io {
#[from]
err: std::io::Error,
},
#[error("an error occurred during `glsl_to_spirv::compile`: {err}")]
GlslToSpirv { err: String },
}
pub const GLSL_EXTENSIONS: &[&str] = &["vert", "frag", "comp", "vs", "fs", "cs"];
impl Watch {
pub fn await_event(&self) -> Result<(), AwaitEventError> {
let res = match self.event_rx.recv() {
Ok(res) => res,
_ => return Err(AwaitEventError::ChannelClosed),
};
let event = res?;
let paths = shaders_related_to_event(&event);
self.pending_paths.borrow_mut().extend(paths);
Ok(())
}
pub fn try_next_path(&self) -> Result<Option<PathBuf>, NextPathError> {
let mut pending_paths = self.pending_paths.borrow_mut();
loop {
if !pending_paths.is_empty() {
return Ok(Some(pending_paths.remove(0)));
}
match self.event_rx.try_recv() {
Err(mpsc::TryRecvError::Disconnected) => return Err(NextPathError::ChannelClosed),
Err(mpsc::TryRecvError::Empty) => (),
Ok(res) => {
let event = res?;
pending_paths.extend(shaders_related_to_event(&event));
continue;
}
}
return Ok(None);
}
}
pub fn paths_touched(&self) -> Result<HashSet<PathBuf>, NextPathError> {
let mut paths = HashSet::new();
loop {
match self.try_next_path() {
Err(err) => return Err(err),
Ok(None) => break,
Ok(Some(path)) => {
paths.insert(path);
}
}
}
Ok(paths)
}
pub fn compile_touched(
&self,
) -> Result<impl Iterator<Item = (PathBuf, Result<Vec<u8>, CompileError>)>, NextPathError> {
let paths = self.paths_touched()?;
let iter = paths.into_iter().map(|path| {
let result = compile(&path);
(path, result)
});
Ok(iter)
}
}
pub fn watch<P>(path: P) -> Result<Watch, CreationError>
where
P: AsRef<Path>,
{
watch_paths(Some(path))
}
pub fn watch_paths<I>(paths: I) -> Result<Watch, CreationError>
where
I: IntoIterator,
I::Item: AsRef<Path>,
{
let (tx, event_rx) = mpsc::channel();
let mut watchers = vec![];
let mut watched_paths = vec![];
for path in paths {
let path = path.as_ref().to_path_buf();
let tx = tx.clone();
let mut watcher = notify::RecommendedWatcher::new_immediate(move |res| {
tx.send(res).ok();
})?;
if path.is_dir() {
watcher.watch(&path, notify::RecursiveMode::Recursive)?;
} else {
watcher.watch(&path, notify::RecursiveMode::NonRecursive)?;
}
watchers.push(watcher);
watched_paths.push(path);
}
let pending_paths = RefCell::new(vec![]);
Ok(Watch {
event_rx,
pending_paths,
_watchers: watchers,
_watched_paths: watched_paths,
})
}
fn shaders_related_to_event<'a>(event: &'a notify::Event) -> impl 'a + Iterator<Item = PathBuf> {
event.paths.iter().filter_map(|p| {
if path_is_shader_file(p) {
Some(p.to_path_buf())
} else {
None
}
})
}
fn path_is_shader_file(path: &Path) -> bool {
if path.is_file() {
let path_ext = match path.extension().and_then(|s| s.to_str()) {
None => return false,
Some(ext) => ext,
};
for ext in GLSL_EXTENSIONS {
if &path_ext == ext {
return true;
}
}
}
false
}
pub fn compile(glsl_path: &Path) -> Result<Vec<u8>, CompileError> {
let shader_ty = glsl_path
.extension()
.and_then(|s| s.to_str())
.and_then(extension_to_shader_ty)
.expect("");
let glsl_string = std::fs::read_to_string(glsl_path)?;
let spirv_file = glsl_to_spirv::compile(&glsl_string, shader_ty)
.map_err(|err| CompileError::GlslToSpirv { err })?;
let mut buf_reader = std::io::BufReader::new(spirv_file);
let mut spirv_bytes = vec![];
buf_reader.read_to_end(&mut spirv_bytes)?;
Ok(spirv_bytes)
}
fn extension_to_shader_ty(ext: &str) -> Option<glsl_to_spirv::ShaderType> {
let ty = match ext {
"vert" => glsl_to_spirv::ShaderType::Vertex,
"frag" => glsl_to_spirv::ShaderType::Fragment,
"comp" => glsl_to_spirv::ShaderType::Compute,
"vs" => glsl_to_spirv::ShaderType::Vertex,
"fs" => glsl_to_spirv::ShaderType::Fragment,
"cs" => glsl_to_spirv::ShaderType::Compute,
_ => return None,
};
Some(ty)
}