forked from philpax/wgpu-openxr-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
124 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include blit_common.wgsl | ||
|
||
var<push_constant> view_index: u32; | ||
@fragment | ||
fn blit_fs_main(in: BlitVertexOutput) -> @location(0) vec4<f32> { | ||
return textureSample(blit_texture, blit_sampler, in.uv_coords, i32(view_index)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use std::{ | ||
collections::HashMap, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use anyhow::Context; | ||
|
||
/// This is _not_ a robust preprocessor. It's the bare minimum to make this example work. | ||
/// This *will* fall down at the first hurdle. | ||
pub fn preprocess(files: &HashMap<PathBuf, String>, current_file: &str) -> anyhow::Result<String> { | ||
let mut current_file = current_file.to_string(); | ||
while current_file.contains("#include") { | ||
current_file = current_file | ||
.lines() | ||
.map(|l| match l.strip_prefix("#include ") { | ||
Some(filename) => Ok(files | ||
.get(&PathBuf::from(filename)) | ||
.context("failed to find file")? | ||
.as_ref()), | ||
None => Ok(l), | ||
}) | ||
.collect::<anyhow::Result<Vec<&str>>>()? | ||
.join("\n"); | ||
} | ||
Ok(current_file) | ||
} | ||
|
||
pub struct Preprocessor { | ||
files: HashMap<PathBuf, String>, | ||
} | ||
impl Preprocessor { | ||
pub fn from_directory(path: &Path) -> std::io::Result<Self> { | ||
Ok(Self { | ||
files: std::fs::read_dir(path)? | ||
.filter_map(Result::ok) | ||
.map(|de| de.path()) | ||
.filter(|p| p.extension().unwrap_or_default() == "wgsl") | ||
.map(|p| { | ||
Ok(( | ||
PathBuf::from( | ||
p.file_name() | ||
.ok_or(std::io::Error::from(std::io::ErrorKind::NotFound))?, | ||
), | ||
std::fs::read_to_string(&p)?, | ||
)) | ||
}) | ||
.collect::<std::io::Result<_>>()?, | ||
}) | ||
} | ||
|
||
pub fn preprocess(&self, filename: impl AsRef<Path>) -> anyhow::Result<String> { | ||
preprocess( | ||
&self.files, | ||
self.files | ||
.get(filename.as_ref()) | ||
.context("File not present!")? | ||
.as_str(), | ||
) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::path::PathBuf; | ||
|
||
use super::preprocess; | ||
|
||
#[test] | ||
fn preprocess_can_include() { | ||
let main_file = "#include blah.wgsl\n// and good night!"; | ||
let files = [ | ||
(PathBuf::from("foo.wgsl"), "// first file!".to_string()), | ||
( | ||
PathBuf::from("blah.wgsl"), | ||
"#include foo.wgsl\n// hello world!".to_string(), | ||
), | ||
(PathBuf::from("main.wgsl"), main_file.to_string()), | ||
] | ||
.into_iter() | ||
.collect(); | ||
|
||
let expected_output = r#"// first file! | ||
// hello world! | ||
// and good night!"#; | ||
|
||
assert_eq!(preprocess(&files, main_file).unwrap(), expected_output); | ||
} | ||
} |