support for include directive in shaders
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use anyhow::*;
|
||||
use glob::glob;
|
||||
use std::fs::{read_to_string, write, create_dir_all};
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
use shaderc::{ResolvedInclude, ShaderKind, IncludeType};
|
||||
|
||||
struct ShaderData {
|
||||
source: String,
|
||||
@@ -12,55 +12,55 @@ struct ShaderData {
|
||||
|
||||
impl ShaderData {
|
||||
// Load data from file
|
||||
pub fn load(src_path: PathBuf) -> Result<Self> {
|
||||
pub fn load(src_path: PathBuf, kind: ShaderKind) -> Option<ShaderData> {
|
||||
|
||||
// Get extension and filename
|
||||
let filename = src_path.file_name().unwrap().to_str().unwrap();
|
||||
let extension = src_path.extension().context("File has no extension")?.to_str().unwrap();
|
||||
|
||||
// Figure out kind from extension
|
||||
let kind = match extension {
|
||||
"vert" => shaderc::ShaderKind::Vertex,
|
||||
"frag" => shaderc::ShaderKind::Fragment,
|
||||
"comp" => shaderc::ShaderKind::Compute,
|
||||
_ => bail!("Unsupported shader: {}", src_path.display()),
|
||||
};
|
||||
let extension = src_path.extension().expect("File has no extension").to_str().unwrap();
|
||||
|
||||
// Create output path
|
||||
let directory = src_path.parent().unwrap().join(".bin");
|
||||
create_dir_all(directory.clone()).context("Failed to create output directory")?;
|
||||
create_dir_all(directory.clone()).expect("Failed to create output directory");
|
||||
let spv_path = directory.join(filename).with_extension(format!("{}.spv", extension));
|
||||
|
||||
// Read file
|
||||
let source = read_to_string(src_path.clone())?;
|
||||
let source = read_to_string(src_path.clone()).expect("Failed to read shader file content");
|
||||
|
||||
// Return struct
|
||||
Ok(Self{ source, src_path, spv_path, kind })
|
||||
Some(Self { source, src_path, spv_path, kind })
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
|
||||
// Collect all shaders recursively within /shaders/
|
||||
let mut shader_paths = [
|
||||
glob("./src/**/*.vert")?,
|
||||
glob("./src/**/*.frag")?,
|
||||
glob("./src/**/*.comp")?,
|
||||
];
|
||||
let shaders = shader_paths.iter_mut().flatten()
|
||||
.map(|glob_result| ShaderData::load(glob_result?))
|
||||
.collect::<Vec<Result<_>>>().into_iter()
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
|
||||
// Compile all shaders
|
||||
let mut compiler = shaderc::Compiler::new().context("Unable to create shader compiler")?;
|
||||
for shader in shaders {
|
||||
println!("cargo:rerun-if-changed={}", shader.src_path.as_os_str().to_str().unwrap());
|
||||
let compiled = compiler.compile_into_spirv(&shader.source, shader.kind, &shader.src_path.to_str().unwrap(), "main", None, )?;
|
||||
write(shader.spv_path, compiled.as_binary_u8())?;
|
||||
fn resolve_include(inc_name: &str, _inc_type: IncludeType, src_name: &str, _depth: usize) -> Result<ResolvedInclude, String> {
|
||||
|
||||
let path = Path::new(src_name).parent().unwrap().join(inc_name);
|
||||
if path.is_file() {
|
||||
let resolved_name = path.to_str().unwrap().to_owned();
|
||||
let content = read_to_string(path.clone()).unwrap();
|
||||
return Ok(ResolvedInclude { resolved_name, content })
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Err("".to_string())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Load all shaders
|
||||
let vert = glob("./src/**/*.vert").unwrap().filter_map(|p| ShaderData::load(p.unwrap(), ShaderKind::Vertex));
|
||||
let frag = glob("./src/**/*.frag").unwrap().filter_map(|p| ShaderData::load(p.unwrap(), ShaderKind::Fragment));
|
||||
let comp = glob("./src/**/*.comp").unwrap().filter_map(|p| ShaderData::load(p.unwrap(), ShaderKind::Compute));
|
||||
let shaders = vert.chain(frag).chain(comp);
|
||||
|
||||
// Options
|
||||
let mut options = shaderc::CompileOptions::new().expect("Unable to create compile options");
|
||||
options.set_include_callback(resolve_include);
|
||||
|
||||
// Compile all shaders
|
||||
let mut compiler = shaderc::Compiler::new().expect("Unable to create shader compiler");
|
||||
for shader in shaders {
|
||||
println!("cargo:rerun-if-changed={}", shader.src_path.as_os_str().to_str().unwrap());
|
||||
let compiled = compiler.compile_into_spirv(&shader.source, shader.kind, &shader.src_path.to_str().unwrap(), "main", Some(&options)).unwrap();
|
||||
write(shader.spv_path, compiled.as_binary_u8()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user